* [PATCH bpf-next 0/2] Support RENAME_EXCHANGE on bpffs
@ 2021-10-20 8:29 Lorenz Bauer
2021-10-20 8:29 ` [PATCH bpf-next 1/2] libfs: support RENAME_EXCHANGE in simple_rename() Lorenz Bauer
` (2 more replies)
0 siblings, 3 replies; 5+ messages in thread
From: Lorenz Bauer @ 2021-10-20 8:29 UTC (permalink / raw)
To: Alexei Starovoitov, Daniel Borkmann, Andrii Nakryiko
Cc: kernel-team, Lorenz Bauer, netdev, bpf
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
^ permalink raw reply [flat|nested] 5+ messages in thread* [PATCH bpf-next 1/2] libfs: support RENAME_EXCHANGE in simple_rename() 2021-10-20 8:29 [PATCH bpf-next 0/2] Support RENAME_EXCHANGE on bpffs Lorenz Bauer @ 2021-10-20 8:29 ` Lorenz Bauer 2021-10-20 8:29 ` [PATCH bpf-next 2/2] selftests: bpf: test RENAME_EXCHANGE and RENAME_NOREPLACE on bpffs Lorenz Bauer 2021-10-21 0:14 ` [PATCH bpf-next 0/2] Support RENAME_EXCHANGE " Alexei Starovoitov 2 siblings, 0 replies; 5+ messages in thread From: Lorenz Bauer @ 2021-10-20 8:29 UTC (permalink / raw) To: Alexander Viro, Alexei Starovoitov, Daniel Borkmann, Andrii Nakryiko Cc: kernel-team, Lorenz Bauer, linux-fsdevel, linux-kernel, netdev, bpf 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 <lmb@cloudflare.com> --- 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 ^ permalink raw reply related [flat|nested] 5+ messages in thread
* [PATCH bpf-next 2/2] selftests: bpf: test RENAME_EXCHANGE and RENAME_NOREPLACE on bpffs 2021-10-20 8:29 [PATCH bpf-next 0/2] Support RENAME_EXCHANGE on bpffs Lorenz Bauer 2021-10-20 8:29 ` [PATCH bpf-next 1/2] libfs: support RENAME_EXCHANGE in simple_rename() Lorenz Bauer @ 2021-10-20 8:29 ` Lorenz Bauer 2021-10-20 21:43 ` Andrii Nakryiko 2021-10-21 0:14 ` [PATCH bpf-next 0/2] Support RENAME_EXCHANGE " Alexei Starovoitov 2 siblings, 1 reply; 5+ messages in thread From: Lorenz Bauer @ 2021-10-20 8:29 UTC (permalink / raw) To: Shuah Khan, Alexei Starovoitov, Daniel Borkmann, Andrii Nakryiko Cc: kernel-team, Lorenz Bauer, linux-kselftest, netdev, bpf, linux-kernel 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 <lmb@cloudflare.com> --- .../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 ^ permalink raw reply related [flat|nested] 5+ messages in thread
* Re: [PATCH bpf-next 2/2] selftests: bpf: test RENAME_EXCHANGE and RENAME_NOREPLACE on bpffs 2021-10-20 8:29 ` [PATCH bpf-next 2/2] selftests: bpf: test RENAME_EXCHANGE and RENAME_NOREPLACE on bpffs Lorenz Bauer @ 2021-10-20 21:43 ` Andrii Nakryiko 0 siblings, 0 replies; 5+ messages in thread From: Andrii Nakryiko @ 2021-10-20 21:43 UTC (permalink / raw) To: Lorenz Bauer Cc: Shuah Khan, Alexei Starovoitov, Daniel Borkmann, Andrii Nakryiko, kernel-team, open list:KERNEL SELFTEST FRAMEWORK, Networking, bpf, open list On Wed, Oct 20, 2021 at 1:30 AM Lorenz Bauer <lmb@cloudflare.com> wrote: > > 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 <lmb@cloudflare.com> > --- > .../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 > ^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH bpf-next 0/2] Support RENAME_EXCHANGE on bpffs 2021-10-20 8:29 [PATCH bpf-next 0/2] Support RENAME_EXCHANGE on bpffs Lorenz Bauer 2021-10-20 8:29 ` [PATCH bpf-next 1/2] libfs: support RENAME_EXCHANGE in simple_rename() Lorenz Bauer 2021-10-20 8:29 ` [PATCH bpf-next 2/2] selftests: bpf: test RENAME_EXCHANGE and RENAME_NOREPLACE on bpffs Lorenz Bauer @ 2021-10-21 0:14 ` Alexei Starovoitov 2 siblings, 0 replies; 5+ messages in thread From: Alexei Starovoitov @ 2021-10-21 0:14 UTC (permalink / raw) To: Lorenz Bauer Cc: Alexei Starovoitov, Daniel Borkmann, Andrii Nakryiko, kernel-team, netdev, bpf 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? ^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2021-10-21 0:14 UTC | newest] Thread overview: 5+ messages (download: mbox.gz follow: Atom feed -- links below jump to the message on this page -- 2021-10-20 8:29 [PATCH bpf-next 0/2] Support RENAME_EXCHANGE on bpffs Lorenz Bauer 2021-10-20 8:29 ` [PATCH bpf-next 1/2] libfs: support RENAME_EXCHANGE in simple_rename() Lorenz Bauer 2021-10-20 8:29 ` [PATCH bpf-next 2/2] selftests: bpf: test RENAME_EXCHANGE and RENAME_NOREPLACE on bpffs Lorenz Bauer 2021-10-20 21:43 ` Andrii Nakryiko 2021-10-21 0:14 ` [PATCH bpf-next 0/2] Support RENAME_EXCHANGE " Alexei Starovoitov
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox; as well as URLs for NNTP newsgroup(s).