Bug 294832 - lockf: does not block when locking on fd
Summary: lockf: does not block when locking on fd
Status: Closed FIXED
Alias: None
Product: Base System
Classification: Unclassified
Component: bin (show other bugs)
Version: 16.0-CURRENT
Hardware: Any Any
: Normal Affects Some People
Assignee: Dag-Erling Smørgrav
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2026-04-27 19:53 UTC by Christian Ullrich
Modified: 2026-05-06 16:31 UTC (History)
2 users (show)

See Also:
des: mfc-stable15+
des: mfc-stable14+


Attachments
Patch (1.04 KB, patch)
2026-04-27 19:54 UTC, Christian Ullrich
no flags Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Christian Ullrich 2026-04-27 19:53:33 UTC
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.
Comment 1 Christian Ullrich 2026-04-27 19:54:47 UTC
Created attachment 270189 [details]
Patch

The patch is against releng/15.0, but that is currently identical to main.
Comment 2 commit-hook freebsd_committer freebsd_triage 2026-05-03 15:36:34 UTC
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(+)
Comment 3 commit-hook freebsd_committer freebsd_triage 2026-05-03 15:36:39 UTC
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(-)
Comment 4 commit-hook freebsd_committer freebsd_triage 2026-05-06 05:39:07 UTC
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(+)
Comment 5 commit-hook freebsd_committer freebsd_triage 2026-05-06 05:39:18 UTC
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(-)
Comment 6 commit-hook freebsd_committer freebsd_triage 2026-05-06 05:39:22 UTC
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(-)
Comment 7 commit-hook freebsd_committer freebsd_triage 2026-05-06 05:39:23 UTC
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(+)
Comment 8 commit-hook freebsd_committer freebsd_triage 2026-05-06 05:39:24 UTC
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(+)
Comment 9 commit-hook freebsd_committer freebsd_triage 2026-05-06 05:39:25 UTC
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(-)
Comment 10 commit-hook freebsd_committer freebsd_triage 2026-05-06 16:31:12 UTC
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(+)
Comment 11 commit-hook freebsd_committer freebsd_triage 2026-05-06 16:31:15 UTC
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(-)