[PATCH bpf-next 0/2] Support RENAME_EXCHANGE on bpffs

STALE1761d

Revision v1 of 3 in this series.

5 messages, 3 authors, 2021-10-21 · open the first message on its own page

[PATCH bpf-next 0/2] Support RENAME_EXCHANGE on bpffs

From: Lorenz Bauer <hidden>
Date: 2021-10-20 08:30:09

Add support for renameat2(RENAME_EXCHANGE) on bpffs. This is useful
for atomic upgrades of our sk_lookup control plane.

* Create a temporary directory on bpffs
* Migrate maps and pin them into temporary directory
* Load new sk_lookup BPF, attach it and pin the link into temp dir
* renameat2(temp dir, state dir, RENAME_EXCHANGE)
* rmdir temp dir

Due to the sk_lookup semantics this means we can never end up in a
situation where an upgrade breaks the existing control plane.

Lorenz Bauer (2):
  libfs: support RENAME_EXCHANGE in simple_rename()
  selftests: bpf: test RENAME_EXCHANGE and RENAME_NOREPLACE on bpffs

 fs/libfs.c                                    |  6 ++-
 .../selftests/bpf/prog_tests/test_bpffs.c     | 39 +++++++++++++++++++
 2 files changed, 44 insertions(+), 1 deletion(-)

-- 
2.30.2

[PATCH bpf-next 1/2] libfs: support RENAME_EXCHANGE in simple_rename()

From: Lorenz Bauer <hidden>
Date: 2021-10-20 08:30:16

Allow atomic exchange via RENAME_EXCHANGE when using simple_rename.
This affects binderfs, ramfs, hubetlbfs and bpffs. There isn't much
to do except update the various *time fields.

Signed-off-by: Lorenz Bauer <redacted>
---
 fs/libfs.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
diff --git a/fs/libfs.c b/fs/libfs.c
index 51b4de3b3447..93c03d593749 100644
--- a/fs/libfs.c
+++ b/fs/libfs.c
@@ -455,9 +455,12 @@ int simple_rename(struct user_namespace *mnt_userns, struct inode *old_dir,
 	struct inode *inode = d_inode(old_dentry);
 	int they_are_dirs = d_is_dir(old_dentry);
 
-	if (flags & ~RENAME_NOREPLACE)
+	if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE))
 		return -EINVAL;
 
+	if (flags & RENAME_EXCHANGE)
+		goto done;
+
 	if (!simple_empty(new_dentry))
 		return -ENOTEMPTY;
 
@@ -472,6 +475,7 @@ int simple_rename(struct user_namespace *mnt_userns, struct inode *old_dir,
 		inc_nlink(new_dir);
 	}
 
+done:
 	old_dir->i_ctime = old_dir->i_mtime = new_dir->i_ctime =
 		new_dir->i_mtime = inode->i_ctime = current_time(old_dir);
 
-- 
2.30.2

[PATCH bpf-next 2/2] selftests: bpf: test RENAME_EXCHANGE and RENAME_NOREPLACE on bpffs

From: Lorenz Bauer <hidden>
Date: 2021-10-20 08:30:20

Add tests to exercise the behaviour of RENAME_EXCHANGE and RENAME_NOREPLACE
on bpffs. The former checks that after an exchange the inode of two
directories has changed. The latter checks that the source still exists
after a failed rename.

Signed-off-by: Lorenz Bauer <redacted>
---
 .../selftests/bpf/prog_tests/test_bpffs.c     | 39 +++++++++++++++++++
 1 file changed, 39 insertions(+)
diff --git a/tools/testing/selftests/bpf/prog_tests/test_bpffs.c b/tools/testing/selftests/bpf/prog_tests/test_bpffs.c
index 172c999e523c..9c28ae9589bf 100644
--- a/tools/testing/selftests/bpf/prog_tests/test_bpffs.c
+++ b/tools/testing/selftests/bpf/prog_tests/test_bpffs.c
@@ -1,6 +1,7 @@
 // SPDX-License-Identifier: GPL-2.0
 /* Copyright (c) 2020 Facebook */
 #define _GNU_SOURCE
+#include <stdio.h>
 #include <sched.h>
 #include <sys/mount.h>
 #include <sys/stat.h>
@@ -29,6 +30,7 @@ static int read_iter(char *file)
 
 static int fn(void)
 {
+	struct stat a, b;
 	int err, duration = 0;
 
 	err = unshare(CLONE_NEWNS);
@@ -67,6 +69,43 @@ static int fn(void)
 	err = read_iter(TDIR "/fs2/progs.debug");
 	if (CHECK(err, "reading " TDIR "/fs2/progs.debug", "failed\n"))
 		goto out;
+
+	err = mkdir(TDIR "/fs1/a", 0777);
+	if (CHECK(err, "creating " TDIR "/fs1/a", "failed\n"))
+		goto out;
+	err = mkdir(TDIR "/fs1/a/1", 0777);
+	if (CHECK(err, "creating " TDIR "/fs1/a/1", "failed\n"))
+		goto out;
+	err = mkdir(TDIR "/fs1/b", 0777);
+	if (CHECK(err, "creating " TDIR "/fs1/b", "failed\n"))
+		goto out;
+
+	/* Check that RENAME_EXCHANGE works. */
+	err = stat(TDIR "/fs1/a", &a);
+	if (CHECK(err, "stat(" TDIR "/fs1/a)", "failed\n"))
+		goto out;
+	err = renameat2(0, TDIR "/fs1/a", 0, TDIR "/fs1/b", RENAME_EXCHANGE);
+	if (CHECK(err, "renameat2(RENAME_EXCHANGE)", "failed\n"))
+		goto out;
+	err = stat(TDIR "/fs1/b", &b);
+	if (CHECK(err, "stat(" TDIR "/fs1/b)", "failed\n"))
+		goto out;
+	if (CHECK(a.st_ino != b.st_ino, "b should have a's inode", "failed\n"))
+		goto out;
+	err = access(TDIR "/fs1/b/1", F_OK);
+	if (CHECK(err, "access(" TDIR "/fs1/b/1)", "failed\n"))
+		goto out;
+
+	/* Check that RENAME_NOREPLACE works. */
+	err = renameat2(0, TDIR "/fs1/b", 0, TDIR "/fs1/a", RENAME_NOREPLACE);
+	if (CHECK(!err, "renameat2(RENAME_NOREPLACE)", "succeeded\n")) {
+		err = -EINVAL;
+		goto out;
+	}
+	err = access(TDIR "/fs1/b", F_OK);
+	if (CHECK(err, "access(" TDIR "/fs1/b)", "failed\n"))
+		goto out;
+
 out:
 	umount(TDIR "/fs1");
 	umount(TDIR "/fs2");
-- 
2.30.2

Re: [PATCH bpf-next 2/2] selftests: bpf: test RENAME_EXCHANGE and RENAME_NOREPLACE on bpffs

From: Andrii Nakryiko <hidden>
Date: 2021-10-20 21:43:19

On Wed, Oct 20, 2021 at 1:30 AM Lorenz Bauer [off-list ref] wrote:
quoted hunk
Add tests to exercise the behaviour of RENAME_EXCHANGE and RENAME_NOREPLACE
on bpffs. The former checks that after an exchange the inode of two
directories has changed. The latter checks that the source still exists
after a failed rename.

Signed-off-by: Lorenz Bauer <redacted>
---
 .../selftests/bpf/prog_tests/test_bpffs.c     | 39 +++++++++++++++++++
 1 file changed, 39 insertions(+)
diff --git a/tools/testing/selftests/bpf/prog_tests/test_bpffs.c b/tools/testing/selftests/bpf/prog_tests/test_bpffs.c
index 172c999e523c..9c28ae9589bf 100644
--- a/tools/testing/selftests/bpf/prog_tests/test_bpffs.c
+++ b/tools/testing/selftests/bpf/prog_tests/test_bpffs.c
@@ -1,6 +1,7 @@
 // SPDX-License-Identifier: GPL-2.0
 /* Copyright (c) 2020 Facebook */
 #define _GNU_SOURCE
+#include <stdio.h>
 #include <sched.h>
 #include <sys/mount.h>
 #include <sys/stat.h>
@@ -29,6 +30,7 @@ static int read_iter(char *file)

 static int fn(void)
 {
+       struct stat a, b;
        int err, duration = 0;

        err = unshare(CLONE_NEWNS);
@@ -67,6 +69,43 @@ static int fn(void)
        err = read_iter(TDIR "/fs2/progs.debug");
        if (CHECK(err, "reading " TDIR "/fs2/progs.debug", "failed\n"))
                goto out;
+
+       err = mkdir(TDIR "/fs1/a", 0777);
+       if (CHECK(err, "creating " TDIR "/fs1/a", "failed\n"))
+               goto out;
+       err = mkdir(TDIR "/fs1/a/1", 0777);
+       if (CHECK(err, "creating " TDIR "/fs1/a/1", "failed\n"))
+               goto out;
+       err = mkdir(TDIR "/fs1/b", 0777);
+       if (CHECK(err, "creating " TDIR "/fs1/b", "failed\n"))
+               goto out;
+
+       /* Check that RENAME_EXCHANGE works. */
+       err = stat(TDIR "/fs1/a", &a);
+       if (CHECK(err, "stat(" TDIR "/fs1/a)", "failed\n"))
+               goto out;
+       err = renameat2(0, TDIR "/fs1/a", 0, TDIR "/fs1/b", RENAME_EXCHANGE);
+       if (CHECK(err, "renameat2(RENAME_EXCHANGE)", "failed\n"))
+               goto out;
+       err = stat(TDIR "/fs1/b", &b);
+       if (CHECK(err, "stat(" TDIR "/fs1/b)", "failed\n"))
+               goto out;
+       if (CHECK(a.st_ino != b.st_ino, "b should have a's inode", "failed\n"))
+               goto out;
+       err = access(TDIR "/fs1/b/1", F_OK);
+       if (CHECK(err, "access(" TDIR "/fs1/b/1)", "failed\n"))
+               goto out;
+
+       /* Check that RENAME_NOREPLACE works. */
+       err = renameat2(0, TDIR "/fs1/b", 0, TDIR "/fs1/a", RENAME_NOREPLACE);
+       if (CHECK(!err, "renameat2(RENAME_NOREPLACE)", "succeeded\n")) {
+               err = -EINVAL;
+               goto out;
+       }
+       err = access(TDIR "/fs1/b", F_OK);
+       if (CHECK(err, "access(" TDIR "/fs1/b)", "failed\n"))
+               goto out;
+
Please use ASSERT_xxx() for new code.
 out:
        umount(TDIR "/fs1");
        umount(TDIR "/fs2");
--
2.30.2

Re: [PATCH bpf-next 0/2] Support RENAME_EXCHANGE on bpffs

From: Alexei Starovoitov <hidden>
Date: 2021-10-21 00:14:06

On Wed, Oct 20, 2021 at 09:29:54AM +0100, Lorenz Bauer wrote:
Add support for renameat2(RENAME_EXCHANGE) on bpffs. This is useful
for atomic upgrades of our sk_lookup control plane.

* Create a temporary directory on bpffs
* Migrate maps and pin them into temporary directory
* Load new sk_lookup BPF, attach it and pin the link into temp dir
* renameat2(temp dir, state dir, RENAME_EXCHANGE)
* rmdir temp dir

Due to the sk_lookup semantics this means we can never end up in a
situation where an upgrade breaks the existing control plane.
That is a cool idea.
The selftest doesn't exercise all of this logic.
It's only testing that RENAME_EXCHANGE works on directories within bpffs.
Do we need a test for other types of paths?
Keyboard shortcuts
hback out one level
jnext message in thread
kprevious message in thread
ldrill in
Escclose help / fold thread tree
?toggle this help