When lockf(1) is not set to delete the lock file upon releasing the lock, it should block in the flock() call for performance reasons. This is also the case when it is operating on an fd to an existing file. The acquire_lock() function, however, does not consider whether blocking or non-blocking mode is requested in the fdlock branch. On one of my systems this behavior burns ~50 % CPU time according to top when a shell script uses lockf to serialize with another instance of itself.
Created attachment 270189 [details] Patch The patch is against releng/15.0, but that is currently identical to main.
A commit in branch main references this bug: URL: https://cgit.FreeBSD.org/src/commit/?id=0733afdb4dad58cfe7ad7f5994e26bf148d78199 commit 0733afdb4dad58cfe7ad7f5994e26bf148d78199 Author: Dag-Erling Smørgrav <des@FreeBSD.org> AuthorDate: 2026-05-03 15:35:15 +0000 Commit: Dag-Erling Smørgrav <des@FreeBSD.org> CommitDate: 2026-05-03 15:35:15 +0000 lockf: Test that lockf does not spin on fd PR: 294832 MFC after: 1 week Reviewed by: markj Differential Revision: https://reviews.freebsd.org/D56723 usr.bin/lockf/tests/lockf_test.sh | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+)
A commit in branch main references this bug: URL: https://cgit.FreeBSD.org/src/commit/?id=d90513ea85693da0ca5955173609f4e81e38ae16 commit d90513ea85693da0ca5955173609f4e81e38ae16 Author: Christian Ullrich <chris@chrullrich.net> AuthorDate: 2026-05-03 15:35:10 +0000 Commit: Dag-Erling Smørgrav <des@FreeBSD.org> CommitDate: 2026-05-03 15:35:10 +0000 lockf: Avoid spinning when operating on an fd When operating on a file descriptor, acquire_lock() would ignore the flags argument and always operate in non-blocking mode, resulting in unnecessary busy-looping. PR: 294832 MFC after: 1 week Reviewed by: markj Differential Revision: https://reviews.freebsd.org/D56722 usr.bin/lockf/lockf.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-)
A commit in branch stable/15 references this bug: URL: https://cgit.FreeBSD.org/src/commit/?id=8e5f92b6b22c82af1192575781788eb2c8fed448 commit 8e5f92b6b22c82af1192575781788eb2c8fed448 Author: Dag-Erling Smørgrav <des@FreeBSD.org> AuthorDate: 2026-05-03 15:35:15 +0000 Commit: Dag-Erling Smørgrav <des@FreeBSD.org> CommitDate: 2026-05-06 05:37:26 +0000 lockf: Test that lockf does not spin on fd PR: 294832 MFC after: 1 week Reviewed by: markj Differential Revision: https://reviews.freebsd.org/D56723 (cherry picked from commit 0733afdb4dad58cfe7ad7f5994e26bf148d78199) usr.bin/lockf/tests/lockf_test.sh | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+)
A commit in branch stable/15 references this bug: URL: https://cgit.FreeBSD.org/src/commit/?id=651b82e64ef4f753d8f4c1793f359de39744526c commit 651b82e64ef4f753d8f4c1793f359de39744526c Author: Christian Ullrich <chris@chrullrich.net> AuthorDate: 2026-05-03 15:35:10 +0000 Commit: Dag-Erling Smørgrav <des@FreeBSD.org> CommitDate: 2026-05-06 05:37:26 +0000 lockf: Avoid spinning when operating on an fd When operating on a file descriptor, acquire_lock() would ignore the flags argument and always operate in non-blocking mode, resulting in unnecessary busy-looping. PR: 294832 MFC after: 1 week Reviewed by: markj Differential Revision: https://reviews.freebsd.org/D56722 (cherry picked from commit d90513ea85693da0ca5955173609f4e81e38ae16) usr.bin/lockf/lockf.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-)
A commit in branch stable/14 references this bug: URL: https://cgit.FreeBSD.org/src/commit/?id=4a6c4f0265ac7234cf31a3c4494e3354caf629d3 commit 4a6c4f0265ac7234cf31a3c4494e3354caf629d3 Author: Christian Ullrich <chris@chrullrich.net> AuthorDate: 2026-05-03 15:35:10 +0000 Commit: Dag-Erling Smørgrav <des@FreeBSD.org> CommitDate: 2026-05-06 05:37:39 +0000 lockf: Avoid spinning when operating on an fd When operating on a file descriptor, acquire_lock() would ignore the flags argument and always operate in non-blocking mode, resulting in unnecessary busy-looping. PR: 294832 MFC after: 1 week Reviewed by: markj Differential Revision: https://reviews.freebsd.org/D56722 (cherry picked from commit d90513ea85693da0ca5955173609f4e81e38ae16) usr.bin/lockf/lockf.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-)
A commit in branch stable/14 references this bug: URL: https://cgit.FreeBSD.org/src/commit/?id=5158575d924b911aff759af62de642ed38a7a2b3 commit 5158575d924b911aff759af62de642ed38a7a2b3 Author: Dag-Erling Smørgrav <des@FreeBSD.org> AuthorDate: 2026-05-03 15:35:15 +0000 Commit: Dag-Erling Smørgrav <des@FreeBSD.org> CommitDate: 2026-05-06 05:37:40 +0000 lockf: Test that lockf does not spin on fd PR: 294832 MFC after: 1 week Reviewed by: markj Differential Revision: https://reviews.freebsd.org/D56723 (cherry picked from commit 0733afdb4dad58cfe7ad7f5994e26bf148d78199) usr.bin/lockf/tests/lockf_test.sh | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+)
A commit in branch stable/13 references this bug: URL: https://cgit.FreeBSD.org/src/commit/?id=bafb055ab1c5a6e8262fb26731f864bf7a071d9d commit bafb055ab1c5a6e8262fb26731f864bf7a071d9d Author: Dag-Erling Smørgrav <des@FreeBSD.org> AuthorDate: 2026-05-03 15:35:15 +0000 Commit: Dag-Erling Smørgrav <des@FreeBSD.org> CommitDate: 2026-05-06 05:37:46 +0000 lockf: Test that lockf does not spin on fd PR: 294832 MFC after: 1 week Reviewed by: markj Differential Revision: https://reviews.freebsd.org/D56723 (cherry picked from commit 0733afdb4dad58cfe7ad7f5994e26bf148d78199) usr.bin/lockf/tests/lockf_test.sh | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+)
A commit in branch stable/13 references this bug: URL: https://cgit.FreeBSD.org/src/commit/?id=2052d09b7a5f55447b5106489731e229f93d8bda commit 2052d09b7a5f55447b5106489731e229f93d8bda Author: Christian Ullrich <chris@chrullrich.net> AuthorDate: 2026-05-03 15:35:10 +0000 Commit: Dag-Erling Smørgrav <des@FreeBSD.org> CommitDate: 2026-05-06 05:37:46 +0000 lockf: Avoid spinning when operating on an fd When operating on a file descriptor, acquire_lock() would ignore the flags argument and always operate in non-blocking mode, resulting in unnecessary busy-looping. PR: 294832 MFC after: 1 week Reviewed by: markj Differential Revision: https://reviews.freebsd.org/D56722 (cherry picked from commit d90513ea85693da0ca5955173609f4e81e38ae16) usr.bin/lockf/lockf.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-)
A commit in branch releng/15.1 references this bug: URL: https://cgit.FreeBSD.org/src/commit/?id=0ae114f07f5a8d6048459f8dc37ed4132b02dcc9 commit 0ae114f07f5a8d6048459f8dc37ed4132b02dcc9 Author: Dag-Erling Smørgrav <des@FreeBSD.org> AuthorDate: 2026-05-03 15:35:15 +0000 Commit: Colin Percival <cperciva@FreeBSD.org> CommitDate: 2026-05-06 16:30:21 +0000 lockf: Test that lockf does not spin on fd Approved by: re (cperciva) PR: 294832 MFC after: 1 week Reviewed by: markj Differential Revision: https://reviews.freebsd.org/D56723 (cherry picked from commit 0733afdb4dad58cfe7ad7f5994e26bf148d78199) (cherry picked from commit 8e5f92b6b22c82af1192575781788eb2c8fed448) usr.bin/lockf/tests/lockf_test.sh | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+)
A commit in branch releng/15.1 references this bug: URL: https://cgit.FreeBSD.org/src/commit/?id=d0f618e7ae955af283cb03a805e130e6c03d0d38 commit d0f618e7ae955af283cb03a805e130e6c03d0d38 Author: Christian Ullrich <chris@chrullrich.net> AuthorDate: 2026-05-03 15:35:10 +0000 Commit: Colin Percival <cperciva@FreeBSD.org> CommitDate: 2026-05-06 16:30:18 +0000 lockf: Avoid spinning when operating on an fd When operating on a file descriptor, acquire_lock() would ignore the flags argument and always operate in non-blocking mode, resulting in unnecessary busy-looping. Approved by: re (cperciva) PR: 294832 MFC after: 1 week Reviewed by: markj Differential Revision: https://reviews.freebsd.org/D56722 (cherry picked from commit d90513ea85693da0ca5955173609f4e81e38ae16) (cherry picked from commit 651b82e64ef4f753d8f4c1793f359de39744526c) usr.bin/lockf/lockf.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-)