From: Christian Brauner <brauner@kernel.org>
To: linux-fsdevel@vger.kernel.org
Cc: Linus Torvalds <torvalds@linux-foundation.org>,
Jann Horn <jannh@google.com>, Josef Bacik <josef@toxicpanda.com>,
Chris Mason <mason@kernel.org>, Jeff Layton <jlayton@kernel.org>,
NeilBrown <neil@brown.name>, Eric Dumazet <edumazet@kernel.org>,
Thomas Gleixner <tglx@kernel.org>,
Al Viro <viro@zeniv.linux.org.uk>,
Jonathan Corbet <corbet@lwn.net>, Christoph Hellwig <hch@lst.de>,
Greg Kroah-Hartman <gregkh@suse.de>,
Joel Granados <joel.granados@kernel.org>,
Kees Cook <kees@kernel.org>, Jan Kara <jack@suse.cz>,
Jan Kara <jack@suse.com>,
linux-kernel@vger.kernel.org, linux-nfs@vger.kernel.org,
linux-acpi@vger.kernel.org, linux-kselftest@vger.kernel.org,
linux-s390@vger.kernel.org, linux-scsi@vger.kernel.org,
linux-um@lists.infradead.org, linux-wireless@vger.kernel.org,
linuxppc-dev@lists.ozlabs.org, netdev@vger.kernel.org,
ntfs3@lists.linux.dev, platform-driver-x86@vger.kernel.org,
coreteam@netfilter.org, kexec@lists.infradead.org,
linux-pci@vger.kernel.org, linux-sound@vger.kernel.org,
netfilter-devel@vger.kernel.org, dri-devel@lists.freedesktop.org,
ibm-acpi-devel@lists.sourceforge.net,
jfs-discussion@lists.sourceforge.net,
linux-alpha@vger.kernel.org,
linux-arm-kernel@lists.infradead.org, linux-cifs@vger.kernel.org,
linux-fbdev@vger.kernel.org, linux-hwmon@vger.kernel.org,
linux-mm@kvack.org, linux-sh@vger.kernel.org,
linux-usb@vger.kernel.org, samba-technical@lists.samba.org,
sparclinux@vger.kernel.org, linux-m68k@lists.linux-m68k.org,
linux-doc@vger.kernel.org, linux-serial@vger.kernel.org,
linux-atm-general@lists.sourceforge.net,
Takashi Iwai <tiwai@suse.de>,
linux-input@vger.kernel.org, linux-raid@vger.kernel.org,
linux-ext4@vger.kernel.org, linux-modules@vger.kernel.org,
linux-debuggers@vger.kernel.org,
"Christian Brauner (Amutable)" <brauner@kernel.org>,
Chuck Lever <cel@kernel.org>
Subject: [PATCH v2 33/51] selftests/proc: check a /proc listing while entries come and go
Date: Wed, 4 Nov 2026 18:47:19 +0100 [thread overview]
Message-ID: <20261104-work-procfs-bugfixes-v2-33-bbf7d5546809@kernel.org> (raw)
In-Reply-To: <20261104-work-procfs-bugfixes-v2-0-bbf7d5546809@kernel.org>
List /proc/net/dev_snmp6 of a new network namespace while a tun device
comes and goes:
- with a buffer that holds the dots and "lo", add a device whose name
sorts first, finish the listing: "lo" is listed once
- with that device added and a buffer that holds the dots and it,
remove it, finish the listing: "lo" is still listed
- with that device added, stall getdents64() on a userfaultfd page
while it writes the device's entry, remove the device, finish the
listing: "lo" is still listed
All three fail without the fix, the first lists "lo" twice, the other
two not at all.
Signed-off-by: Christian Brauner (Amutable) <brauner@kernel.org>
---
tools/testing/selftests/proc/.gitignore | 1 +
tools/testing/selftests/proc/Makefile | 1 +
tools/testing/selftests/proc/config | 4 +
tools/testing/selftests/proc/proc-readdir-churn.c | 240 ++++++++++++++++++++++
4 files changed, 246 insertions(+)
diff --git a/tools/testing/selftests/proc/.gitignore b/tools/testing/selftests/proc/.gitignore
index 9c9735570abf..8520c7878ea2 100644
--- a/tools/testing/selftests/proc/.gitignore
+++ b/tools/testing/selftests/proc/.gitignore
@@ -8,6 +8,7 @@
/proc-maps-race
/proc-multiple-procfs
/proc-net-dev-lseek
+/proc-readdir-churn
/proc-empty-vm
/proc-pid-vm
/proc-self-map-files-001
diff --git a/tools/testing/selftests/proc/Makefile b/tools/testing/selftests/proc/Makefile
index a7de2bb6d8be..e0c0d313f4d0 100644
--- a/tools/testing/selftests/proc/Makefile
+++ b/tools/testing/selftests/proc/Makefile
@@ -11,6 +11,7 @@ TEST_GEN_PROGS += proc-2-is-kthread
TEST_GEN_PROGS += proc-loadavg-001
TEST_GEN_PROGS += proc-maps-race
TEST_GEN_PROGS += proc-net-dev-lseek
+TEST_GEN_PROGS += proc-readdir-churn
TEST_GEN_PROGS += proc-empty-vm
TEST_GEN_PROGS += proc-pid-vm
TEST_GEN_PROGS += proc-self-map-files-001
diff --git a/tools/testing/selftests/proc/config b/tools/testing/selftests/proc/config
index 68fbd2b35884..052e365af5a9 100644
--- a/tools/testing/selftests/proc/config
+++ b/tools/testing/selftests/proc/config
@@ -1 +1,5 @@
CONFIG_PROC_FS=y
+CONFIG_NET_NS=y
+CONFIG_IPV6=y
+CONFIG_TUN=y
+CONFIG_USERFAULTFD=y
diff --git a/tools/testing/selftests/proc/proc-readdir-churn.c b/tools/testing/selftests/proc/proc-readdir-churn.c
new file mode 100644
index 000000000000..fd9b504b60f5
--- /dev/null
+++ b/tools/testing/selftests/proc/proc-readdir-churn.c
@@ -0,0 +1,240 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * A listing of a /proc directory that takes more than one getdents64()
+ * must not show an entry twice or skip one when an entry is added or
+ * removed in between, and a listing must go on after an entry that is
+ * removed while it is being emitted. /proc/net/dev_snmp6/<dev> of a tun
+ * device comes and goes with the device.
+ */
+#define _GNU_SOURCE
+#include <dirent.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <limits.h>
+#include <linux/if.h>
+#include <linux/if_tun.h>
+#include <linux/userfaultfd.h>
+#include <poll.h>
+#include <pthread.h>
+#include <sched.h>
+#include <string.h>
+#include <sys/ioctl.h>
+#include <sys/mman.h>
+#include <sys/syscall.h>
+#include <unistd.h>
+
+#include "../kselftest_harness.h"
+
+#define SNMP6_DIR "/proc/net/dev_snmp6"
+#define TUN_NAME "a0" /* sorts before "lo" */
+#define TUN_PATH SNMP6_DIR "/" TUN_NAME
+#define SMALL_BUF 80 /* ".", ".." and one short name */
+#define NAMES 64
+
+static int tun_open(const char *name)
+{
+ struct ifreq ifr = {};
+ int fd;
+
+ fd = open("/dev/net/tun", O_RDWR | O_CLOEXEC);
+ if (fd < 0)
+ return -1;
+ strncpy(ifr.ifr_name, name, IFNAMSIZ - 1);
+ ifr.ifr_flags = IFF_TUN | IFF_NO_PI;
+ if (ioctl(fd, TUNSETIFF, &ifr)) {
+ close(fd);
+ return -1;
+ }
+ return fd;
+}
+
+/* one getdents64() into @buf, the names appended to @names */
+static int list(int fd, char *buf, size_t size, char names[][NAME_MAX + 1],
+ int *n)
+{
+ long ret, off;
+ int got = 0;
+
+ ret = syscall(__NR_getdents64, fd, buf, size);
+ if (ret < 0)
+ return -errno;
+ for (off = 0; off < ret;) {
+ struct dirent64 *d = (void *)(buf + off);
+
+ if (*n < NAMES)
+ strcpy(names[(*n)++], d->d_name);
+ got++;
+ off += d->d_reclen;
+ }
+ return got;
+}
+
+static int count(char names[][NAME_MAX + 1], int n, const char *name)
+{
+ int i, c = 0;
+
+ for (i = 0; i < n; i++)
+ if (!strcmp(names[i], name))
+ c++;
+ return c;
+}
+
+/* removes the device once getdents64() faults on its entry, then resolves */
+struct stall {
+ int uffd;
+ int tun;
+ unsigned long addr;
+ unsigned long len;
+ int removed;
+};
+
+static void *stall_resolve(void *arg)
+{
+ struct stall *s = arg;
+ struct pollfd pfd = { .fd = s->uffd, .events = POLLIN };
+ struct uffdio_zeropage zero = {
+ .range = { .start = s->addr, .len = s->len },
+ };
+ struct uffd_msg msg;
+
+ if (poll(&pfd, 1, 10000) != 1)
+ return NULL;
+ if (read(s->uffd, &msg, sizeof(msg)) != sizeof(msg) ||
+ msg.event != UFFD_EVENT_PAGEFAULT)
+ return NULL;
+ close(s->tun);
+ s->removed = access(TUN_PATH, F_OK) == -1;
+ ioctl(s->uffd, UFFDIO_ZEROPAGE, &zero);
+ return NULL;
+}
+
+FIXTURE(proc_readdir) {
+ int dir;
+ int n;
+ char names[NAMES][NAME_MAX + 1];
+ char buf[65536];
+};
+
+FIXTURE_SETUP(proc_readdir)
+{
+ int tun;
+
+ if (unshare(CLONE_NEWNET))
+ SKIP(return, "unshare(CLONE_NEWNET): %s", strerror(errno));
+ tun = open("/dev/net/tun", O_RDWR | O_CLOEXEC);
+ if (tun < 0)
+ SKIP(return, "/dev/net/tun: %s", strerror(errno));
+ close(tun);
+ self->dir = open(SNMP6_DIR, O_RDONLY | O_DIRECTORY | O_CLOEXEC);
+ if (self->dir < 0)
+ SKIP(return, SNMP6_DIR ": %s", strerror(errno));
+ self->n = 0;
+}
+
+FIXTURE_TEARDOWN(proc_readdir)
+{
+ close(self->dir);
+}
+
+/* a device added in front of the position doesn't repeat an entry */
+TEST_F(proc_readdir, add_in_front)
+{
+ int tun;
+
+ /* ".", ".." and "lo" */
+ ASSERT_EQ(list(self->dir, self->buf, SMALL_BUF, self->names, &self->n), 3);
+ ASSERT_EQ(count(self->names, self->n, "lo"), 1);
+
+ tun = tun_open(TUN_NAME);
+ ASSERT_GE(tun, 0);
+ ASSERT_EQ(access(TUN_PATH, F_OK), 0);
+
+ while (list(self->dir, self->buf, sizeof(self->buf), self->names,
+ &self->n) > 0)
+ ;
+ EXPECT_EQ(count(self->names, self->n, "lo"), 1);
+ close(tun);
+}
+
+/* a device removed in front of the position doesn't skip an entry */
+TEST_F(proc_readdir, remove_in_front)
+{
+ int tun;
+
+ tun = tun_open(TUN_NAME);
+ ASSERT_GE(tun, 0);
+ ASSERT_EQ(access(TUN_PATH, F_OK), 0);
+
+ /* ".", ".." and the device, "lo" doesn't fit */
+ ASSERT_EQ(list(self->dir, self->buf, SMALL_BUF, self->names, &self->n), 3);
+ ASSERT_EQ(count(self->names, self->n, TUN_NAME), 1);
+
+ /* closing the device unregisters it and removes its entry */
+ close(tun);
+ ASSERT_EQ(access(TUN_PATH, F_OK), -1);
+
+ while (list(self->dir, self->buf, sizeof(self->buf), self->names,
+ &self->n) > 0)
+ ;
+ EXPECT_EQ(count(self->names, self->n, "lo"), 1);
+}
+
+/* the entry being emitted is removed meanwhile, the listing goes on */
+TEST_F(proc_readdir, remove_current)
+{
+ struct uffdio_api api = { .api = UFFD_API };
+ struct uffdio_register reg = {};
+ long page = sysconf(_SC_PAGESIZE);
+ struct stall s = {};
+ pthread_t thread;
+ char *map;
+ long ret, off;
+
+ s.uffd = syscall(__NR_userfaultfd, O_CLOEXEC | O_NONBLOCK);
+ if (s.uffd < 0)
+ SKIP(return, "userfaultfd: %s", strerror(errno));
+ ASSERT_EQ(ioctl(s.uffd, UFFDIO_API, &api), 0);
+
+ /* the first page is there, the second one faults */
+ map = mmap(NULL, 2 * page, PROT_READ | PROT_WRITE,
+ MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
+ ASSERT_NE(map, MAP_FAILED);
+ map[0] = 0;
+ s.addr = (unsigned long)map + page;
+ s.len = page;
+ reg.range.start = s.addr;
+ reg.range.len = s.len;
+ reg.mode = UFFDIO_REGISTER_MODE_MISSING;
+ ASSERT_EQ(ioctl(s.uffd, UFFDIO_REGISTER, ®), 0);
+
+ s.tun = tun_open(TUN_NAME);
+ ASSERT_GE(s.tun, 0);
+ ASSERT_EQ(access(TUN_PATH, F_OK), 0);
+ ASSERT_EQ(pthread_create(&thread, NULL, stall_resolve, &s), 0);
+
+ /*
+ * ".." ends right before the second page, so writing the record of
+ * the device stalls until it has been removed.
+ */
+ ret = syscall(__NR_getdents64, self->dir, map + page - 48, 48 + page);
+ ASSERT_EQ(pthread_join(thread, NULL), 0);
+ ASSERT_TRUE(s.removed);
+ ASSERT_GT(ret, 0);
+ for (off = 0; off < ret;) {
+ struct dirent64 *d = (void *)(map + page - 48 + off);
+
+ if (self->n < NAMES)
+ strcpy(self->names[self->n++], d->d_name);
+ off += d->d_reclen;
+ }
+ ASSERT_EQ(count(self->names, self->n, TUN_NAME), 1);
+
+ while (list(self->dir, self->buf, sizeof(self->buf), self->names,
+ &self->n) > 0)
+ ;
+ EXPECT_EQ(count(self->names, self->n, "lo"), 1);
+ munmap(map, 2 * page);
+ close(s.uffd);
+}
+
+TEST_HARNESS_MAIN
--
2.53.0
next prev parent reply other threads:[~2026-11-04 17:54 UTC|newest]
Thread overview: 42+ messages / expand[flat|nested] mbox.gz Atom feed top
[not found] <20261104-work-procfs-v2-0-288f0ce32927@kernel.org>
2026-11-04 17:27 ` [PATCH v2 00/51] proc: fix a few old bugs in /proc handlers (Part 1/7) Christian Brauner
2026-11-04 17:27 ` [PATCH v2 01/51] alloc_tag: don't remove /proc/allocinfo under the codetag module lock Christian Brauner
2026-11-04 17:27 ` [PATCH v2 02/51] scsi: esas2r: don't copy kernel pointers out in EXPRESS_IOCTL_GET_MOD_INFO Christian Brauner
2026-11-04 17:27 ` [PATCH v2 03/51] ALSA: cs46xx: hand the region to the region proc files Christian Brauner
2026-11-04 17:27 ` [PATCH v2 04/51] PNP: isapnp: don't go on when isapnp_cfg_begin() fails Christian Brauner
2026-11-04 17:34 ` Rafael J. Wysocki (Intel)
2026-11-04 17:27 ` [PATCH v2 05/51] xtensa: simdisk: remove the proc entry before the disk Christian Brauner
2026-11-04 17:27 ` [PATCH v2 06/51] powerpc/pseries: put the node that the ofdt property commands look up Christian Brauner
2026-11-04 17:27 ` [PATCH v2 07/51] lockd: don't hand out the terminator of nlm_end_grace Christian Brauner
2026-11-04 17:27 ` [PATCH v2 08/51] powerpc/pseries/lparcfg: don't drop the last byte of a write Christian Brauner
2026-11-04 17:27 ` [PATCH v2 09/51] profiling: report it when /proc/profile can't be created Christian Brauner
2026-11-04 17:27 ` [PATCH v2 10/51] zorro: size the proc files like what they serve Christian Brauner
2026-11-04 17:27 ` [PATCH v2 11/51] sparc: led: consume the whole write Christian Brauner
2026-11-04 17:45 ` [PATCH v2 12/51] powerpc/rtas: consume the whole write to /proc/powerpc/rtas/progress Christian Brauner
2026-11-04 17:46 ` [PATCH v2 13/51] sched/psi: don't drop the last byte of a trigger write Christian Brauner
2026-11-04 17:47 ` [PATCH v2 14/51] proc: sysctl: don't stop readdir at an entry that is being unregistered Christian Brauner
2026-11-04 17:47 ` [PATCH v2 15/51] PCI/proc: don't truncate the position of config space accesses Christian Brauner
2026-11-04 17:47 ` [PATCH v2 16/51] PNP: isapnp: don't read past the end of the buffer on a large pread() Christian Brauner
2026-11-04 17:47 ` [PATCH v2 17/51] s390/dasd: don't look in front of the buffer on an empty write Christian Brauner
2026-11-04 17:47 ` [PATCH v2 18/51] platform/x86: toshiba_acpi: reject malformed brightness and fan writes Christian Brauner
2026-11-04 17:47 ` [PATCH v2 19/51] xtensa: simdisk: don't read the file name without the lock Christian Brauner
2026-11-04 17:47 ` [PATCH v2 20/51] powerpc/rtas_flash: don't read past the validate message Christian Brauner
2026-11-04 17:47 ` [PATCH v2 21/51] powerpc/rtasd: zero the error log buffer Christian Brauner
2026-11-04 17:47 ` [PATCH v2 22/51] ALSA: info: don't write past the end of a data entry Christian Brauner
2026-11-04 17:47 ` [PATCH v2 23/51] scsi: BusLogic: don't return 0 from writes to the proc file Christian Brauner
2026-11-04 17:47 ` [PATCH v2 24/51] scsi: ips: " Christian Brauner
2026-11-04 17:47 ` [PATCH v2 25/51] wifi: libipw: don't return 0 from writes to debug_level Christian Brauner
2026-11-04 17:47 ` [PATCH v2 26/51] scsi: core: NUL-terminate what is written to the host proc files Christian Brauner
2026-11-04 17:47 ` [PATCH v2 27/51] um: NUL-terminate what is written to /proc/exitcode Christian Brauner
2026-11-04 17:47 ` [PATCH v2 28/51] ntfs3: don't warn when a huge label is written Christian Brauner
2026-11-04 17:47 ` [PATCH v2 29/51] proc: don't walk through a non-directory when resolving a name Christian Brauner
2026-11-04 17:47 ` [PATCH v2 30/51] net: pktgen: reject device names with a '/' Christian Brauner
2026-11-04 17:47 ` [PATCH v2 31/51] proc: don't WARN about a name that is already registered Christian Brauner
2026-11-04 17:47 ` [PATCH v2 32/51] proc: resume readdir after the last name it returned Christian Brauner
2026-11-04 17:47 ` Christian Brauner [this message]
2026-11-04 17:47 ` [PATCH v2 34/51] proc: let proc_remove() remove the entry it was given Christian Brauner
2026-11-04 17:47 ` [PATCH v2 35/51] proc: warn about entries left in /proc/net of a dying namespace Christian Brauner
2026-11-04 17:47 ` [PATCH v2 36/51] proc: don't let NOMMU map procfs files Christian Brauner
2026-11-04 17:47 ` [PATCH v2 37/51] proc/vmcore: don't let vmcore_cleanup() run twice at once Christian Brauner
2026-11-04 17:47 ` [PATCH v2 38/51] powerpc/crash_dump: check ioremap_cache() in copy_oldmem_page() Christian Brauner
2026-11-04 17:47 ` [PATCH v2 39/51] powerpc/crash_dump: recover from a machine check " Christian Brauner
2026-11-04 18:40 ` [PATCH v2 00/51] proc: fix a few old bugs in /proc handlers (Part 1/7) Christian Brauner
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261104-work-procfs-bugfixes-v2-33-bbf7d5546809@kernel.org \
--to=brauner@kernel.org \
--cc=cel@kernel.org \
--cc=corbet@lwn.net \
--cc=coreteam@netfilter.org \
--cc=dri-devel@lists.freedesktop.org \
--cc=edumazet@kernel.org \
--cc=gregkh@suse.de \
--cc=hch@lst.de \
--cc=ibm-acpi-devel@lists.sourceforge.net \
--cc=jack@suse.com \
--cc=jack@suse.cz \
--cc=jannh@google.com \
--cc=jfs-discussion@lists.sourceforge.net \
--cc=jlayton@kernel.org \
--cc=joel.granados@kernel.org \
--cc=josef@toxicpanda.com \
--cc=kees@kernel.org \
--cc=kexec@lists.infradead.org \
--cc=linux-acpi@vger.kernel.org \
--cc=linux-alpha@vger.kernel.org \
--cc=linux-arm-kernel@lists.infradead.org \
--cc=linux-atm-general@lists.sourceforge.net \
--cc=linux-cifs@vger.kernel.org \
--cc=linux-debuggers@vger.kernel.org \
--cc=linux-doc@vger.kernel.org \
--cc=linux-ext4@vger.kernel.org \
--cc=linux-fbdev@vger.kernel.org \
--cc=linux-fsdevel@vger.kernel.org \
--cc=linux-hwmon@vger.kernel.org \
--cc=linux-input@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-kselftest@vger.kernel.org \
--cc=linux-m68k@lists.linux-m68k.org \
--cc=linux-mm@kvack.org \
--cc=linux-modules@vger.kernel.org \
--cc=linux-nfs@vger.kernel.org \
--cc=linux-pci@vger.kernel.org \
--cc=linux-raid@vger.kernel.org \
--cc=linux-s390@vger.kernel.org \
--cc=linux-scsi@vger.kernel.org \
--cc=linux-serial@vger.kernel.org \
--cc=linux-sh@vger.kernel.org \
--cc=linux-sound@vger.kernel.org \
--cc=linux-um@lists.infradead.org \
--cc=linux-usb@vger.kernel.org \
--cc=linux-wireless@vger.kernel.org \
--cc=linuxppc-dev@lists.ozlabs.org \
--cc=mason@kernel.org \
--cc=neil@brown.name \
--cc=netdev@vger.kernel.org \
--cc=netfilter-devel@vger.kernel.org \
--cc=ntfs3@lists.linux.dev \
--cc=platform-driver-x86@vger.kernel.org \
--cc=samba-technical@lists.samba.org \
--cc=sparclinux@vger.kernel.org \
--cc=tglx@kernel.org \
--cc=tiwai@suse.de \
--cc=torvalds@linux-foundation.org \
--cc=viro@zeniv.linux.org.uk \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox