Skip to contentSkip to conversation

v3: F_OFD_GETLK extension to read lock info

PATCH In review Stas Sergeev 2 participants 7 messages Jun 22, 2023 – Jul 2, 2023

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

cover v3: F_OFD_GETLK extension to read lock info

This extension allows to use F_UNLCK on query, which currently returns EINVAL. Instead it can be used to query the locks on a particular fd - something that is not currently possible. The basic idea is that on F_OFD_GETLK, F_UNLCK would "conflict" with (or query) any types of the lock on the same fd, and ignore any locks on other fds.

Use-cases:

1. CRIU-alike scenario when you want to read the locking info from an fd for the later reconstruction. This can now be done by setting l_start and l_len to 0 to cover entire file range, and do F_OFD_GETLK. In the loop you need to advance l_start past the returned lock ranges, to eventually collect all locked ranges.

2. Implementing the lock checking/enforcing policy.
Say you want to implement an "auditor" module in your program, that checks that the I/O is done only after the proper locking is applied on a file region. In this case you need to know if the particular region is locked on that fd, and if so - with what type of the lock. If you would do that currently (without this extension) then you can only check for the write locks, and for that you need to probe the lock on your fd and then open the same file via another fd and probe there. That way you can identify the write lock on a particular fd, but such trick is non-atomic and complex. As for finding out the read lock on a particular fd - impossible.
This extension allows to do such queries without any extra efforts.

3. Implementing the mandatory locking policy.
Suppose you want to make a policy where the write lock inhibits any unlocked readers and writers. Currently you need to check if the write lock is present on some other fd, and if it is not there - allow the I/O operation. But because the write lock can appear at any moment, you need to do that under some global lock, which can be released only when the I/O operation is finished.
With the proposed extension you can instead just check the write lock on your own fd first, and if it is there - allow the I/O operation on that fd without using any global lock. Only if there is no write lock on this fd, then you need to take global lock and check for a write lock on other fds.

The second patch adds a test-case for OFD locks. It tests both the generic things and the proposed extension.

The third patch is a proposed man page update for fcntl(2) (not for the linux source tree)

Changes in v3:
- Move selftest to selftests/filelock

Changes in v2:
- Dropped the l_pid extension patch and updated test-case accordingly.

Stas Sergeev (2):
fs/locks: F_UNLCK extension for F_OFD_GETLK
selftests: add OFD lock tests

 fs/locks.c                                  |  23 +++-
 tools/testing/selftests/filelock/Makefile   |   5 +
 tools/testing/selftests/filelock/ofdlocks.c | 132 ++++++++++++++++++++
 3 files changed, 157 insertions(+), 3 deletions(-)
 create mode 100644 tools/testing/selftests/filelock/Makefile
 create mode 100644 tools/testing/selftests/filelock/ofdlocks.c
Signature · 1 line
2.39.2
#1 lore raw

PATCH 1/2 fs/locks: F_UNLCK extension for F_OFD_GETLK

Currently F_UNLCK with F_OFD_GETLK returns -EINVAL. The proposed extension allows to use it for getting the lock information from the particular fd.

---
 fs/locks.c | 23 ++++++++++++++++++++---
 1 file changed, 20 insertions(+), 3 deletions(-)
fs/locks.c +20 -3
index df8b26a42524..210766007e63 100644
@@ -868,6 +868,21 @@ static bool posix_locks_conflict(struct file_lock *caller_fl,
 	return locks_conflict(caller_fl, sys_fl);
 }
 
+/* Determine if lock sys_fl blocks lock caller_fl. Used on xx_GETLK
+ * path so checks for additional GETLK-specific things like F_UNLCK.
+ */
+static bool posix_test_locks_conflict(struct file_lock *caller_fl,
+				      struct file_lock *sys_fl)
+{
+	/* F_UNLCK checks any locks on the same fd. */
+	if (caller_fl->fl_type == F_UNLCK) {
+		if (!posix_same_owner(caller_fl, sys_fl))
+			return false;
+		return locks_overlap(caller_fl, sys_fl);
+	}
+	return posix_locks_conflict(caller_fl, sys_fl);
+}
+
 /* Determine if lock sys_fl blocks lock caller_fl. FLOCK specific
  * checking before calling the locks_conflict().
  */
@@ -901,7 +916,7 @@ posix_test_lock(struct file *filp, struct file_lock *fl)
 retry:
 	spin_lock(&ctx->flc_lock);
 	list_for_each_entry(cfl, &ctx->flc_posix, fl_list) {
-		if (!posix_locks_conflict(fl, cfl))
+		if (!posix_test_locks_conflict(fl, cfl))
 			continue;
 		if (cfl->fl_lmops && cfl->fl_lmops->lm_lock_expirable
 			&& (*cfl->fl_lmops->lm_lock_expirable)(cfl)) {
@@ -2207,7 +2222,8 @@ int fcntl_getlk(struct file *filp, unsigned int cmd, struct flock *flock)
 	if (fl == NULL)
 		return -ENOMEM;
 	error = -EINVAL;
-	if (flock->l_type != F_RDLCK && flock->l_type != F_WRLCK)
+	if (cmd != F_OFD_GETLK && flock->l_type != F_RDLCK
+			&& flock->l_type != F_WRLCK)
 		goto out;
 
 	error = flock_to_posix_lock(filp, fl, flock);
@@ -2414,7 +2430,8 @@ int fcntl_getlk64(struct file *filp, unsigned int cmd, struct flock64 *flock)
 		return -ENOMEM;
 
 	error = -EINVAL;
-	if (flock->l_type != F_RDLCK && flock->l_type != F_WRLCK)
+	if (cmd != F_OFD_GETLK && flock->l_type != F_RDLCK
+			&& flock->l_type != F_WRLCK)
 		goto out;
 
 	error = flock64_to_posix_lock(filp, fl, flock);
Signature · 1 line
2.39.2
#2 lore raw

PATCH 2/2 selftests: add OFD lock tests

In reply to Stas Sergeev

Test the basic locking stuff on 2 fds: multiple read locks, conflicts between read and write locks, use of len==0 for queries. Also tests for F_UNLCK F_OFD_GETLK extension.

---
 tools/testing/selftests/filelock/Makefile   |   5 +
 tools/testing/selftests/filelock/ofdlocks.c | 132 ++++++++++++++++++++
 2 files changed, 137 insertions(+)
 create mode 100644 tools/testing/selftests/filelock/Makefile
 create mode 100644 tools/testing/selftests/filelock/ofdlocks.c
tools/testing/selftests/filelock/Makefile new file +5 -0
new file mode 100644
index 000000000000..478e82f8b464
@@ -0,0 +1,5 @@
+# SPDX-License-Identifier: GPL-2.0
+
+TEST_GEN_PROGS := ofdlocks
+
+include ../lib.mk
tools/testing/selftests/filelock/ofdlocks.c new file +132 -0

132 lines not on this page · raw message

Signature · 1 line
2.39.2
#3 lore raw

PATCH 1/1 fcntl.2: document F_UNLCK F_OFD_GETLK extension

In reply to Stas Sergeev

F_UNLCK has the special meaning when used as a lock type on input. It returns the information about any lock found in the specified region on that particular file descriptor. Locks on other file descriptors are ignored by F_UNLCK.

---
 man2/fcntl.2 | 7 +++++++
 1 file changed, 7 insertions(+)
man2/fcntl.2 +7 -0
index 7b5604e3a..e3e3e7b8c 100644
@@ -604,6 +604,13 @@ then details about one of these locks are returned via
 .IR lock ,
 as described above for
 .BR F_GETLK .
+.B F_UNLCK
+has the special meaning when put into
+.I l_type
+as an input. It returns the information about any lock in the specified
+range on that particular file descriptor. The locks on other file
+descriptors are ignored by
+.BR F_UNLCK .
 .PP
 In the current implementation,
 .\" commit 57b65325fe34ec4c917bc4e555144b4a94d9e1f7
Signature · 1 line
2.39.2
maintainer
#4 lore raw
In reply to Stas Sergeev
Quoted 66 lines · Stas Sergeev

This extension allows to use F_UNLCK on query, which currently returns EINVAL. Instead it can be used to query the locks on a particular fd - something that is not currently possible. The basic idea is that on F_OFD_GETLK, F_UNLCK would "conflict" with (or query) any types of the lock on the same fd, and ignore any locks on other fds.

Use-cases:

1. CRIU-alike scenario when you want to read the locking info from an fd for the later reconstruction. This can now be done by setting l_start and l_len to 0 to cover entire file range, and do F_OFD_GETLK. In the loop you need to advance l_start past the returned lock ranges, to eventually collect all locked ranges.

2. Implementing the lock checking/enforcing policy.
Say you want to implement an "auditor" module in your program, that checks that the I/O is done only after the proper locking is applied on a file region. In this case you need to know if the particular region is locked on that fd, and if so - with what type of the lock. If you would do that currently (without this extension) then you can only check for the write locks, and for that you need to probe the lock on your fd and then open the same file via another fd and probe there. That way you can identify the write lock on a particular fd, but such trick is non-atomic and complex. As for finding out the read lock on a particular fd - impossible.
This extension allows to do such queries without any extra efforts.

3. Implementing the mandatory locking policy.
Suppose you want to make a policy where the write lock inhibits any unlocked readers and writers. Currently you need to check if the write lock is present on some other fd, and if it is not there - allow the I/O operation. But because the write lock can appear at any moment, you need to do that under some global lock, which can be released only when the I/O operation is finished.
With the proposed extension you can instead just check the write lock on your own fd first, and if it is there - allow the I/O operation on that fd without using any global lock. Only if there is no write lock on this fd, then you need to take global lock and check for a write lock on other fds.

The second patch adds a test-case for OFD locks. It tests both the generic things and the proposed extension.

The third patch is a proposed man page update for fcntl(2) (not for the linux source tree)

Changes in v3:
- Move selftest to selftests/filelock

Changes in v2:
- Dropped the l_pid extension patch and updated test-case accordingly.

Stas Sergeev (2):
fs/locks: F_UNLCK extension for F_OFD_GETLK
selftests: add OFD lock tests

 fs/locks.c                                  |  23 +++-
 tools/testing/selftests/filelock/Makefile   |   5 +
 tools/testing/selftests/filelock/ofdlocks.c | 132 ++++++++++++++++++++
 3 files changed, 157 insertions(+), 3 deletions(-)
 create mode 100644 tools/testing/selftests/filelock/Makefile
 create mode 100644 tools/testing/selftests/filelock/ofdlocks.c
Quoted 10 lines

I've taken the first two patches into my locks-next branch, so they should end up in linux-next soon. Adding support for testing this to fstests is a hard requirement before this will be merged into mainline.

Thanks,

Signature · 1 line
Jeff Layton <[email protected]>
author
#5 lore raw

27.06.2023 21:23, Jeff Layton пишет:

Quoted 3 lines

I've taken the first two patches into my locks-next branch, so they should end up in linux-next soon. Adding support for testing this to fstests is a hard requirement before this will be merged into mainline.

Yes, it is _hard_...
I am still trying to set up the buildroot environment for these tests:

https://bugs.busybox.net/show_bug.cgi?id=15665

And this will take some time, if at all successful.
Additionally, these tests simply compare the output with the pre-defined textual patterns, so I have no idea what to do after I figured "this feature is unsupported on this kernel".
I sketched the tests, but the above problems are holding things.

author
#6 lore raw
In reply to Jeff Layton

27.06.2023 21:23, Jeff Layton пишет:

Quoted 3 lines

I've taken the first two patches into my locks-next branch, so they should end up in linux-next soon. Adding support for testing this to fstests is a hard requirement before this will be merged into mainline.

The test-suite is entirely broken.
I posted the patch:

https://marc.info/?l=fstests&m=168811805324487&w=2

And the question:
https://marc.info/?l=fstests&m=168811862324941&w=2

But no reaction.
Unless someone helps with reviewing,
nothing will likely happen.