Article View: linux.kernel.netdev
Article #9699[PATCH RFC 38/38] selftests/proc: check that a faulting readlink() doesn't hold up removal
From: Christian Brauner
Date: Mon, 02 Nov 2026 14:43
Date: Mon, 02 Nov 2026 14:43
257 lines
16768 bytes
16768 bytes
Add the symlink case:
- readlink() /proc/asound/<id> of a dummy card into a buffer backed by
userfaultfd
- change the card's id through sysfs while the reader faults, which
removes the symlink with snd_card_mutex and info_mutex held
- expect that to finish without waiting for the reader
Skipped without snd-dummy.
Signed-off-by: Christian Brauner (Amutable) <brauner@kernel.org>
---
tools/testing/selftests/proc/.gitignore | 2 +
tools/testing/selftests/proc/Makefile | 2 +
tools/testing/selftests/proc/config | 3 +
.../selftests/proc/proc-remove-slow-readlink.c | 191 +++++++++++++++++++++
4 files changed, 198 insertions(+)
diff --git a/tools/testing/selftests/proc/.gitignore b/tools/testing/selftests/proc/.gitignore
index 7b8133492782..e2566cc0ae07 100644
--- a/tools/testing/selftests/proc/.gitignore
+++ b/tools/testing/selftests/proc/.gitignore
@@ -22,6 +22,8 @@
/proc-uptime-002
/proc-pidns
/proc-remove-slow-sysctl
+/proc-remove-slow-reader
+/proc-remove-slow-readlink
/read
/self
/setns-dcache
diff --git a/tools/testing/selftests/proc/Makefile b/tools/testing/selftests/proc/Makefile
index bce6beca7274..6a9bdc999f54 100644
--- a/tools/testing/selftests/proc/Makefile
+++ b/tools/testing/selftests/proc/Makefile
@@ -33,5 +33,7 @@ TEST_GEN_PROGS += proc-fsconfig-hidepid
TEST_GEN_PROGS += proc-sysctl-readdir-churn
TEST_GEN_PROGS += proc-pidns
TEST_GEN_PROGS += proc-remove-slow-sysctl
+TEST_GEN_PROGS += proc-remove-slow-reader
+TEST_GEN_PROGS += proc-remove-slow-readlink
include ../lib.mk
diff --git a/tools/testing/selftests/proc/config b/tools/testing/selftests/proc/config
index 3edd3c437fe4..7b0e1d9be1a2 100644
--- a/tools/testing/selftests/proc/config
+++ b/tools/testing/selftests/proc/config
@@ -2,3 +2,6 @@ CONFIG_PROC_FS=y
CONFIG_IPV6=y
CONFIG_TUN=y
CONFIG_USERFAULTFD=y
+CONFIG_SOUND=y
+CONFIG_SND=y
+CONFIG_SND_DUMMY=m
diff --git a/tools/testing/selftests/proc/proc-remove-slow-readlink.c b/tools/testing/selftests/proc/proc-remove-slow-readlink.c
new file mode 100644
index 000000000000..3290225daecd
--- /dev/null
+++ b/tools/testing/selftests/proc/proc-remove-slow-readlink.c
@@ -0,0 +1,191 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * A readlink() that faults while copying out the link must not hold up
+ * removal of the /proc symlink. Changing a sound card's id removes
+ * /proc/asound/<id> with snd_card_mutex and info_mutex held.
+ */
+#define _GNU_SOURCE
+#include <dirent.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <limits.h>
+#include <linux/userfaultfd.h>
+#include <poll.h>
+#include <pthread.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/ioctl.h>
+#include <sys/mman.h>
+#include <sys/syscall.h>
+#include <unistd.h>
+
+#include "../kselftest_harness.h"
+
+#define CARD_ID "Dummy"
+#define NEW_ID "procslow"
+#define LINK_PATH "/proc/asound/" CARD_ID
+#define TIMEOUT_MS 5000
+
+struct reader {
+ char *buf;
+ size_t len;
+ ssize_t ret;
+};
+
+struct renamer {
+ const char *id_path;
+ const char *id;
+ int done;
+ int ret;
+};
+
+static void *readlink_thread(void *arg)
+{
+ struct reader *r = arg;
+
+ r->ret = readlink(LINK_PATH, r->buf, r->len);
+ return NULL;
+}
+
+/* a new id removes the card's old /proc/asound/<id> link */
+static int set_id(const char *id_path, const char *id)
+{
+ int fd, ret;
+
+ fd = open(id_path, O_WRONLY | O_CLOEXEC);
+ if (fd < 0)
+ return -errno;
+ ret = write(fd, id, strlen(id)) < 0 ? -errno : 0;
+ close(fd);
+ return ret;
+}
+
+static void *rename_thread(void *arg)
+{
+ struct renamer *r = arg;
+ ssize_t ret;
+
+ r->ret = set_id(r->id_path, r->id);
+ ret = write(r->done, "x", 1);
+ return (void *)ret;
+}
+
+/* the sysfs id file of the card with @id */
+static int find_card(const char *id, char *id_path, size_t size)
+{
+ struct dirent *d;
+ int ret = -1;
+ DIR *dir;
+
+ dir = opendir("/sys/class/sound");
+ if (!dir)
+ return -1;
+ while (ret && (d = readdir(dir))) {
+ char cur[32];
+ FILE *f;
+
+ if (strncmp(d->d_name, "card", 4))
+ continue;
+ snprintf(id_path, size, "/sys/class/sound/%s/id", d->d_name);
+ f = fopen(id_path, "r");
+ if (!f)
+ continue;
+ if (fgets(cur, sizeof(cur), f)) {
+ cur[strcspn(cur, "\n")] = '\0';
+ if (!strcmp(cur, id))
+ ret = 0;
+ }
+ fclose(f);
+ }
+ closedir(dir);
+ return ret;
+}
+
+FIXTURE(dummy) {
+ char id_path[PATH_MAX];
+ bool renamed;
+};
+
+FIXTURE_SETUP(dummy)
+{
+ self->renamed = false;
+ if (access(LINK_PATH, F_OK) && system("modprobe -q snd-dummy"))
+ SKIP(return, "no snd-dummy");
+ if (access(LINK_PATH, F_OK))
+ SKIP(return, LINK_PATH ": %s", strerror(errno));
+ if (find_card(CARD_ID, self->id_path, sizeof(self->id_path)))
+ SKIP(return, "no sysfs id file for " CARD_ID);
+ if (access(self->id_path, W_OK))
+ SKIP(return, "%s: %s", self->id_path, strerror(errno));
+}
+
+FIXTURE_TEARDOWN(dummy)
+{
+ if (self->renamed && set_id(self->id_path, CARD_ID))
+ TH_LOG("couldn't name the card " CARD_ID " again");
+}
+
+TEST_F(dummy, remove_while_readlink_faults)
+{
+ struct uffdio_api api = { .api = UFFD_API };
+ struct uffdio_register reg = {};
+ struct uffdio_zeropage zero = {};
+ long psz = sysconf(_SC_PAGESIZE);
+ struct renamer rn = {};
+ struct reader r = {};
+ struct uffd_msg msg;
+ struct pollfd pfd;
+ pthread_t rt, nt;
+ int uffd, done[2];
+ char *buf;
+
+ uffd = syscall(__NR_userfaultfd, O_CLOEXEC | O_NONBLOCK);
+ if (uffd < 0)
+ SKIP(return, "userfaultfd: %s", strerror(errno));
+ ASSERT_EQ(ioctl(uffd, UFFDIO_API, &api), 0);
+
+ buf = mmap(NULL, psz, PROT_READ | PROT_WRITE,
+ MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
+ ASSERT_NE(buf, MAP_FAILED);
+ reg.range.start = (unsigned long)buf;
+ reg.range.len = psz;
+ reg.mode = UFFDIO_REGISTER_MODE_MISSING;
+ ASSERT_EQ(ioctl(uffd, UFFDIO_REGISTER, ®), 0);
+
+ r.buf = buf;
+ r.len = psz;
+ ASSERT_EQ(pthread_create(&rt, NULL, readlink_thread, &r), 0);
+
+ /* The reader now sleeps in the copy of the link to userspace. */
+ pfd = (struct pollfd){ .fd = uffd, .events = POLLIN };
+ ASSERT_EQ(poll(&pfd, 1, TIMEOUT_MS), 1);
+ ASSERT_EQ(read(uffd, &msg, sizeof(msg)), sizeof(msg));
+ ASSERT_EQ(msg.event, UFFD_EVENT_PAGEFAULT);
+
+ /* Renaming the card removes the link that is being read. */
+ ASSERT_EQ(pipe2(done, O_CLOEXEC), 0);
+ rn.id_path = self->id_path;
+ rn.id = NEW_ID;
+ rn.done = done[1];
+ self->renamed = true;
+ ASSERT_EQ(pthread_create(&nt, NULL, rename_thread, &rn), 0);
+ pfd = (struct pollfd){ .fd = done[0], .events = POLLIN };
+ EXPECT_EQ(poll(&pfd, 1, TIMEOUT_MS), 1) {
+ TH_LOG("removing " LINK_PATH " waited for the reader");
+ }
+
+ zero.range.start = (unsigned long)buf;
+ zero.range.len = psz;
+ ASSERT_EQ(ioctl(uffd, UFFDIO_ZEROPAGE, &zero), 0);
+ ASSERT_EQ(pthread_join(rt, NULL), 0);
+ ASSERT_EQ(pthread_join(nt, NULL), 0);
+ EXPECT_EQ(rn.ret, 0);
+
+ /* The link was read before it went away. */
+ EXPECT_GT(r.ret, 0);
+ EXPECT_EQ(readlink(LINK_PATH, buf, psz), -1);
+ EXPECT_EQ(errno, ENOENT);
+}
+
+TEST_HARNESS_MAIN
--
2.53.0
Technical Details
Message-ID:
<20261102-work-procfs-bugfixes-v1-38-59422ed3e465@kernel.org>Path:
mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!usenet.network!.POSTED!vger.kernel.org!netdev+bounces-354774-linux-kernel-netdev=dewy.ruReferences:
<20261102-work-procfs-bugfixes-v1-0-59422ed3e465@kernel.org>Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!usenet.network!.POSTED!vger.kernel.org!netdev+bounces-354774-linux-kernel-netdev=dewy.ru From: brauner@kernel.org (Christian Brauner) Newsgroups: linux.kernel.netdev Subject: [PATCH RFC 38/38] selftests/proc: check that a faulting readlink() doesn't hold up removal Date: Mon, 02 Nov 2026 15:43:29 +0100 Organization: Usenet Network Message-ID: <20261102-work-procfs-bugfixes-v1-38-59422ed3e465@kernel.org> References: <20261102-work-procfs-bugfixes-v1-0-59422ed3e465@kernel.org> MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit Injection-Info: usenet.network; logging-data="2952933"; mail-complaints-to="abuse@usenet.network" Cancel-Lock: sha1:o1mXIWb95HcKvWUDlByic0CN4eI= sha256:6GVbH+nCd9pVZii0WmXzJS2qOG0o//4BMqbza/Utsvk= sha1:kpiE+2BvzPTNvNreN9H1iXPzSnQ= sha256:2DARZe+s0U7iKx03SAaLWu9HR5ZeveDYUtdU8DiLDSM= List-ID: <netdev.vger.kernel.org> X-Mailpost-Empty-Hdrs: X-Spam-Level X-Received: from localhost (localhost [127.0.0.1]) by mail.dewy.ru (Postfix) with ESMTP id 90F4A112550; Mon, 2 Nov 2026 18:11:08 +0300 (MSK) X-Virus-Scanned: Debian amavisd-new at usenet.network X-Spam-Flag: NO X-Spam-Score: -2.3 X-Spam-Status: No, score=-2.3 tagged_above=-9999 required=6.31 tests=[DCC_CHECK=1.1, DCC_REPUT_70_89=0.1, DKIMWL_WL_HIGH=-0.001, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, MAILING_LIST_MULTI=-1, RCVD_IN_DNSWL_MED=-2.3, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, URIBL_BLOCKED=0.001] autolearn=ham autolearn_force=no X-Authentication-Results: mail.dewy.ru (amavisd-new); dkim=pass (2048-bit key) header.d=kernel.org X-Received: from mail.dewy.ru ([127.0.0.1]) by localhost (mail.dewy.ru [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id 9g06Z8Wbh719; Mon, 2 Nov 2026 18:11:01 +0300 (MSK) X-Received-SPF: Pass (mailfrom) identity=mailfrom; client-ip=172.234.253.10; helo=sea.lore.kernel.org; envelope-from=netdev+bounces-354774-linux-kernel-netdev=dewy.ru@vger.kernel.org; receiver=<UNKNOWN> X-Authentication-Results: OpenDMARC; dmarc=pass (p=quarantine dis=none) header.from=kernel.org X-Received: from sea.lore.kernel.org (sea.lore.kernel.org [172.234.253.10]) by mail.dewy.ru (Postfix) with ESMTPS id 74B3011254D; Mon, 2 Nov 2026 18:10:52 +0300 (MSK) X-Received: from smtp.subspace.kernel.org (conduit.subspace.kernel.org [100.77.114.92]) by sea.lore.kernel.org (Postfix) with ESMTP id 1642E2C22A; Mon, 2 Nov 2026 15:03:12 +0000 (UTC) X-Authentication-Results: smtp.subspace.kernel.org; arc=pass header.oldest-pass=0 smtp.remote-ip=127.0.0.1 X-ARC-Seal: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=pass; t=1793630839; b=Sbi2a6flPO7sgdbf4Q5COpOcOjiqa3soRuQB++81Gy25oSDpN+F01VGWzAGregm7wPSK QzKTBC4DmpDqo1I2LgOZjEV00wklDZsQfrDXrzxxlhHWNTRJHuKw+BigbvwFHQD+zxpdX CU+98HVRIAfxtmc91+dubEQBrk4Bova00gf+P87Oa0TGXhcRYAEI3MI83m0ThuqXi2qLp 9OyHvxF6sVJYAR2uBL8B0J/WBwvprZ3/BqdxTGt+vIfbAY9A2gDMiB6mKbb5VTK4zYE8X 4+4Xwpxrg4CPV+ZNM/eAmdvGCqjQU/bYGfFWYl8fifO6/L5opUcFS0dnUWx1BgP0JZg== X-ARC-Message-Signature: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1793630839; h=X-Original-To:Received:Received:DKIM-Signature:From:Date:Subject: X-Mailing-List:List-Id:List-Post:List-Help:List-Subscribe: List-Unsubscribe:List-Owner:Precedence:MIME-Version:Content-Type: Content-Transfer-Encoding:Message-Id:References:In-Reply-To:To:Cc: X-Mailer; bh=HVagn+HE6BgGgxgbr+5XC5Y5Qcbb33m4Jb7EnpZPFUg=; b=tTmE3BMgswVtt5xG2DKb0/nFRuuQGRnZxsI+2pJBBEO8f3X0IvpH+EsmF6tY6Q8WdJ2j WKIAmbnP2Pddq5jYflyNJvwY4UuErvADGpkW966IrbNU79HYp3Z3Z5GZmG/BjEHfVl7ZH 5Y6xqrBxILsN9KVoTzuSM9avucTT4tPM/4crupMG68m4aam6FW9X9Rngho4jS2Z335bze up+6Jfr8BCvyivoRre/jTVb8EHLdDVgjjMrIwXZ7cpkivIFygrr9LOxUzuFTDG9a0+npO 0XP+INk4usk6ZjBXyOphK3yQZZoAnAaVt4P7sj9oPnvRYUWZVvmNywN2pT+0njvvoVA== X-ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b="ZfP/Kahw"; arc=pass header.oldest-pass=0 smtp.remote-ip=127.0.0.1 X-Received: from localhost.localdomain (localhost [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 5DADC6082624; Mon, 2 Nov 2026 14:47:19 +0000 (UTC) X-Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key, unprotected) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=ZfP/Kahw X-Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 X-ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1793630838; b=VFAwIfkApgRiSs6NFKFS8lrdH7jOaHFWekZouMDMCfe/UhNSkg5ml0izoeqh1xbLLZo1 JY1wxfXyiXD8PEvLGBOg0TYjqpCObbW5S9loQqe0i4VlO7e7SF6In+MglsjW19gotElew Ic7gaPl5KtsXf26+AHswPLOsexMAXXrWJC8SSht2erBDrzBhML03uOyrBEvcoKUxi9PF4 J2cqNobH8dRr1Ae18ws8SD4wcxreFvTPDWAfhMKHYTF23EDXVEysENWusXqrBvRpioUCr cl5XKzfoaMlw2poQbNk7Depqk6DqsKPe6znS6cIEdMDrIRuEnKvmcZAWbpmIsaDbPAA== X-ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1793630838; h=Received:DKIM-Signature:From:Date:Subject:MIME-Version:Content-Type: Content-Transfer-Encoding:Message-Id:References:In-Reply-To:To:Cc: X-Mailer; bh=HVagn+HE6BgGgxgbr+5XC5Y5Qcbb33m4Jb7EnpZPFUg=; b=OOrX+5RutX2ZUn/YCZrMWgNIS0lOl6SM/TyDyGz2Dz0dWKU8sLZpgoHrptH+UlHTnyRr LZjay4Lt11J6CyFxAOPRBh9Xtc8amh5FTqB99zYy7mZPoPvo4otOt6UXxFI2DXPIYq4cE vehG4VkxjGdeMi1I1l+ulnYQFgY5/RcUG9pVZ90z7Qqy8YluakNZhTSFfNeu9hyEMO7tI sZzoDgVGw8UhK1GCHSllPQSBl1LNN0mY7w71Ok0E/FoOUUrQvV71egAiB0ygNXfExrRlB ni/qgu3/eE+nnBcAjMm/Q/fEj1NVXznLf/IOkgmNt0+xRm894m4eifFskwXz7wapfOA== X-ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b="ZfP/Kahw"; arc=none smtp.remote-ip=100.103.45.18 X-Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id F39186070CA8; Mon, 2 Nov 2026 14:47:13 +0000 (UTC) X-Received: by smtp.kernel.org (Postfix) with ESMTPSA id 26EE81F00A1A; Mon, 2 Nov 2026 14:46:55 +0000 (UTC) X-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1793630833; bh=HVagn+HE6BgGgxgbr+5XC5Y5Qcbb33m4Jb7EnpZPFUg=; h=From:Date:Subject:References:In-Reply-To:To:Cc; b=ZfP/KahwDm10wm2TSGrtLVkg7xDUNnpz+rBBaInyM45cZNagos2iYqnrDed5HxIBA K+//mFWxDsWpOWe9Buj12+quSzkpcQzj2ngYZ9ZsEW0oSIiCRi7l1s6PLMfANxGQPz ES6j9Tp68ZCoex9QLLCdMUr2RP+/gA7nMafoF8zKWkoSoj6HU61hIGfIek8HtzVsMz WP7zxeoWb2ywthOOE6hEK8rHNlm6c5T599z/Oyvz7z8nNJFpq2Xxjua62S/Ez/3caV aKvdxGOQzTpkPCeB1AmIzk8MgvSvtRVAT8gs8GThhJQslYhMWBFvphDQuEoGKj/fJB b5NG8uh8YTrhw== X-Mailing-List: netdev@vger.kernel.org X-List-Post: <mailto:netdev@vger.kernel.org> X-List-Help: <mailto:netdev+help@vger.kernel.org> X-List-Subscribe: <mailto:netdev+subscribe@vger.kernel.org> X-List-Unsubscribe: <mailto:netdev+unsubscribe@vger.kernel.org> X-List-Owner: <mailto:netdev+owner@vger.kernel.org> X-Precedence: list X-In-Reply-To: <20261102-work-procfs-bugfixes-v1-0-59422ed3e465@kernel.org> X-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> X-Mailer: b4 0.17-dev