2023-06-22 17:07:27

by stsp

[permalink] [raw]
Subject: [PATCH 0/2] 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

CC: Jeff Layton <[email protected]>
CC: Chuck Lever <[email protected]>
CC: Alexander Viro <[email protected]>
CC: Christian Brauner <[email protected]>
CC: [email protected]
CC: [email protected]
CC: Shuah Khan <[email protected]>
CC: [email protected]
CC: [email protected]

--
2.39.2



2023-06-22 17:15:18

by stsp

[permalink] [raw]
Subject: [PATCH 2/2] selftests: add OFD lock tests

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.

Signed-off-by: Stas Sergeev <[email protected]>

CC: Jeff Layton <[email protected]>
CC: Chuck Lever <[email protected]>
CC: Alexander Viro <[email protected]>
CC: Christian Brauner <[email protected]>
CC: [email protected]
CC: [email protected]
CC: Shuah Khan <[email protected]>
CC: [email protected]
CC: [email protected]

---
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

diff --git a/tools/testing/selftests/filelock/Makefile b/tools/testing/selftests/filelock/Makefile
new file mode 100644
index 000000000000..478e82f8b464
--- /dev/null
+++ b/tools/testing/selftests/filelock/Makefile
@@ -0,0 +1,5 @@
+# SPDX-License-Identifier: GPL-2.0
+
+TEST_GEN_PROGS := ofdlocks
+
+include ../lib.mk
diff --git a/tools/testing/selftests/filelock/ofdlocks.c b/tools/testing/selftests/filelock/ofdlocks.c
new file mode 100644
index 000000000000..1ccb2b9b5ead
--- /dev/null
+++ b/tools/testing/selftests/filelock/ofdlocks.c
@@ -0,0 +1,132 @@
+// SPDX-License-Identifier: GPL-2.0
+
+#define _GNU_SOURCE
+#include <fcntl.h>
+#include <assert.h>
+#include <stdio.h>
+#include <unistd.h>
+#include <string.h>
+#include "../kselftest.h"
+
+static int lock_set(int fd, struct flock *fl)
+{
+ int ret;
+
+ fl->l_pid = 0; // needed for OFD locks
+ fl->l_whence = SEEK_SET;
+ ret = fcntl(fd, F_OFD_SETLK, fl);
+ if (ret)
+ perror("fcntl()");
+ return ret;
+}
+
+static int lock_get(int fd, struct flock *fl)
+{
+ int ret;
+
+ fl->l_pid = 0; // needed for OFD locks
+ fl->l_whence = SEEK_SET;
+ ret = fcntl(fd, F_OFD_GETLK, fl);
+ if (ret)
+ perror("fcntl()");
+ return ret;
+}
+
+int main(void)
+{
+ int rc;
+ struct flock fl, fl2;
+ int fd = open("/tmp/aa", O_RDWR | O_CREAT | O_EXCL, 0600);
+ int fd2 = open("/tmp/aa", O_RDONLY);
+
+ unlink("aa");
+ assert(fd != -1);
+ assert(fd2 != -1);
+ ksft_print_msg("[INFO] opened fds %i %i\n", fd, fd2);
+
+ /* Set some read lock */
+ fl.l_type = F_RDLCK;
+ fl.l_start = 5;
+ fl.l_len = 3;
+ rc = lock_set(fd, &fl);
+ if (rc == 0) {
+ ksft_print_msg
+ ("[SUCCESS] set OFD read lock on first fd\n");
+ } else {
+ ksft_print_msg("[FAIL] to set OFD read lock on first fd\n");
+ return -1;
+ }
+ /* Make sure read locks do not conflict on different fds. */
+ fl.l_type = F_RDLCK;
+ fl.l_start = 5;
+ fl.l_len = 1;
+ rc = lock_get(fd2, &fl);
+ if (rc != 0)
+ return -1;
+ if (fl.l_type != F_UNLCK) {
+ ksft_print_msg("[FAIL] read locks conflicted\n");
+ return -1;
+ }
+ /* Make sure read/write locks do conflict on different fds. */
+ fl.l_type = F_WRLCK;
+ fl.l_start = 5;
+ fl.l_len = 1;
+ rc = lock_get(fd2, &fl);
+ if (rc != 0)
+ return -1;
+ if (fl.l_type != F_UNLCK) {
+ ksft_print_msg
+ ("[SUCCESS] read and write locks conflicted\n");
+ } else {
+ ksft_print_msg
+ ("[SUCCESS] read and write locks not conflicted\n");
+ return -1;
+ }
+ /* Get info about the lock on first fd. */
+ fl.l_type = F_UNLCK;
+ fl.l_start = 5;
+ fl.l_len = 1;
+ rc = lock_get(fd, &fl);
+ if (rc != 0) {
+ ksft_print_msg
+ ("[FAIL] F_OFD_GETLK with F_UNLCK not supported\n");
+ return -1;
+ }
+ if (fl.l_type != F_UNLCK) {
+ ksft_print_msg
+ ("[SUCCESS] F_UNLCK test returns: locked, type %i pid %i len %zi\n",
+ fl.l_type, fl.l_pid, fl.l_len);
+ } else {
+ ksft_print_msg
+ ("[FAIL] F_OFD_GETLK with F_UNLCK did not return lock info\n");
+ return -1;
+ }
+ /* Try the same but by locking everything by len==0. */
+ fl2.l_type = F_UNLCK;
+ fl2.l_start = 0;
+ fl2.l_len = 0;
+ rc = lock_get(fd, &fl2);
+ if (rc != 0) {
+ ksft_print_msg
+ ("[FAIL] F_OFD_GETLK with F_UNLCK not supported\n");
+ return -1;
+ }
+ if (memcmp(&fl, &fl2, sizeof(fl))) {
+ ksft_print_msg
+ ("[FAIL] F_UNLCK test returns: locked, type %i pid %i len %zi\n",
+ fl.l_type, fl.l_pid, fl.l_len);
+ return -1;
+ }
+ ksft_print_msg("[SUCCESS] F_UNLCK with len==0 returned the same\n");
+ /* Get info about the lock on second fd - no locks on it. */
+ fl.l_type = F_UNLCK;
+ fl.l_start = 0;
+ fl.l_len = 0;
+ lock_get(fd2, &fl);
+ if (fl.l_type != F_UNLCK) {
+ ksft_print_msg
+ ("[FAIL] F_OFD_GETLK with F_UNLCK return lock info from another fd\n");
+ return -1;
+ }
+ return 0;
+}
--
2.39.2


2023-06-27 17:00:54

by stsp

[permalink] [raw]
Subject: Re: [PATCH 0/2] v3: F_OFD_GETLK extension to read lock info


27.06.2023 21:23, Jeff Layton пишет:
> 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.


2023-06-27 17:01:54

by Jeffrey Layton

[permalink] [raw]
Subject: Re: [PATCH 0/2] v3: F_OFD_GETLK extension to read lock info

On Thu, 2023-06-22 at 21:52 +0500, Stas Sergeev wrote:
> 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
>
> CC: Jeff Layton <[email protected]>
> CC: Chuck Lever <[email protected]>
> CC: Alexander Viro <[email protected]>
> CC: Christian Brauner <[email protected]>
> CC: [email protected]
> CC: [email protected]
> CC: Shuah Khan <[email protected]>
> CC: [email protected]
> CC: [email protected]
>

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,
--
Jeff Layton <[email protected]>

2023-07-02 07:41:04

by stsp

[permalink] [raw]
Subject: Re: [PATCH 0/2] v3: F_OFD_GETLK extension to read lock info


27.06.2023 21:23, Jeff Layton пишет:
> 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.