Article View: linux.kernel.netdev
Article #11354[PATCH RESEND v2 20/23] selftests/proc: check that a mapped entry can be removed
From: Christian Brauner
Date: Wed, 04 Nov 2026 19:34
Date: Wed, 04 Nov 2026 19:34
221 lines
16527 bytes
16527 bytes
Map a memory BAR of a PCI device through /proc/bus/pci and remove the
device through sysfs:
- the removal doesn't wait for the mapped file
- the entry is gone
- and reading the file fails with -EIO
The mapping itself survives. /proc/bus/pci maps through the iomem
address space of /dev/mem and procfs leaves that alone, like the sysfs
resource files leave theirs, so the test doesn't touch the mapping
after the removal. What the BAR of a removed device reads like is
between the reader and the hardware.
Nothing in the test can know which device is safe to remove, so it
takes the device from PROC_PCI_DEV, skips without it and rescans the bus
afterwards. It also skips where PCI files can't be mapped or that isn't
allowed.
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/proc-remove-mapped.c | 160 ++++++++++++++++++++++
3 files changed, 162 insertions(+)
diff --git a/tools/testing/selftests/proc/.gitignore b/tools/testing/selftests/proc/.gitignore
index e2566cc0ae07..cafd8c263c9a 100644
--- a/tools/testing/selftests/proc/.gitignore
+++ b/tools/testing/selftests/proc/.gitignore
@@ -23,6 +23,7 @@
/proc-pidns
/proc-remove-slow-sysctl
/proc-remove-slow-reader
+/proc-remove-mapped
/proc-remove-slow-readlink
/read
/self
diff --git a/tools/testing/selftests/proc/Makefile b/tools/testing/selftests/proc/Makefile
index 6a9bdc999f54..c38297afc244 100644
--- a/tools/testing/selftests/proc/Makefile
+++ b/tools/testing/selftests/proc/Makefile
@@ -34,6 +34,7 @@ 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-mapped
TEST_GEN_PROGS += proc-remove-slow-readlink
include ../lib.mk
diff --git a/tools/testing/selftests/proc/proc-remove-mapped.c b/tools/testing/selftests/proc/proc-remove-mapped.c
new file mode 100644
index 000000000000..92860fb2a8f4
--- /dev/null
+++ b/tools/testing/selftests/proc/proc-remove-mapped.c
@@ -0,0 +1,160 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Removing a /proc entry with a mapped file open must not wait for that file
+ * and must leave the file dead. Map a memory BAR of a PCI device through
+ * /proc/bus/pci and remove the device, which removes its entry. Nothing here
+ * can know which device is safe to remove and rescan, so it is named in
+ * PROC_PCI_DEV, e.g. PROC_PCI_DEV=0000:00:03.0.
+ */
+#define _GNU_SOURCE
+#include <errno.h>
+#include <fcntl.h>
+#include <limits.h>
+#include <linux/pci.h>
+#include <setjmp.h>
+#include <signal.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/ioctl.h>
+#include <sys/mman.h>
+#include <unistd.h>
+
+#include "../kselftest_harness.h"
+
+#define DEV_ENV "PROC_PCI_DEV"
+#define SYSFS_DEVICES "/sys/bus/pci/devices/"
+#define SYSFS_RESCAN "/sys/bus/pci/rescan"
+#define PROC_PCI "/proc/bus/pci/"
+#define IORESOURCE_MEM 0x00000200
+
+static sigjmp_buf fault;
+
+static void on_sigbus(int sig)
+{
+ siglongjmp(fault, 1);
+}
+
+/* the first memory BAR of @dev */
+static int mem_bar(const char *dev, unsigned long long *start,
+ unsigned long long *len)
+{
+ unsigned long long s, e, flags;
+ char path[PATH_MAX];
+ int ret = -ENODEV;
+ FILE *f;
+
+ snprintf(path, sizeof(path), SYSFS_DEVICES "%s/resource", dev);
+ f = fopen(path, "r");
+ if (!f)
+ return -errno;
+ while (fscanf(f, "%llx %llx %llx", &s, &e, &flags) == 3) {
+ if ((flags & IORESOURCE_MEM) && e >= s) {
+ *start = s;
+ *len = e - s + 1;
+ ret = 0;
+ break;
+ }
+ }
+ fclose(f);
+ return ret;
+}
+
+static int write_one(const char *path)
+{
+ int fd, ret;
+
+ fd = open(path, O_WRONLY | O_CLOEXEC);
+ if (fd < 0)
+ return -errno;
+ ret = write(fd, "1", 1) < 0 ? -errno : 0;
+ close(fd);
+ return ret;
+}
+
+FIXTURE(pci) {
+ char dev[16];
+ char path[64];
+ bool removed;
+};
+
+FIXTURE_SETUP(pci)
+{
+ const char *dev = getenv(DEV_ENV);
+
+ self->removed = false;
+ if (!dev)
+ SKIP(return, DEV_ENV " isn't set");
+ /* 0000:BB:DD.F is /proc/bus/pci/BB/DD.F */
+ if (strlen(dev) != 12 || strncmp(dev, "0000:", 5))
+ SKIP(return, "%s: not a device in PCI domain 0", dev);
+ strcpy(self->dev, dev);
+ snprintf(self->path, sizeof(self->path), PROC_PCI "%.2s/%s",
+ dev + 5, dev + 8);
+ if (access(self->path, R_OK))
+ SKIP(return, "%s: %s", self->path, strerror(errno));
+}
+
+FIXTURE_TEARDOWN(pci)
+{
+ if (self->removed && write_one(SYSFS_RESCAN))
+ TH_LOG("couldn't rescan the PCI bus for %s", self->dev);
+}
+
+TEST_F(pci, remove_while_mapped)
+{
+ long psz = sysconf(_SC_PAGESIZE);
+ unsigned long long start = 0, len = 0;
+ char path[PATH_MAX];
+ volatile unsigned int *p;
+ unsigned int v;
+ int fd;
+
+ if (mem_bar(self->dev, &start, &len))
+ SKIP(return, "%s has no memory BAR", self->dev);
+
+ fd = open(self->path, O_RDONLY | O_CLOEXEC);
+ ASSERT_GE(fd, 0);
+ if (ioctl(fd, PCIIOC_MMAP_IS_MEM)) {
+ /* no PCI mmap on this architecture, or locked down */
+ if (errno == EINVAL || errno == EPERM) {
+ close(fd);
+ SKIP(return, "PCIIOC_MMAP_IS_MEM: %s", strerror(errno));
+ }
+ ASSERT_TRUE(false) {
+ TH_LOG("PCIIOC_MMAP_IS_MEM: %s", strerror(errno));
+ }
+ }
+ p = mmap(NULL, psz, PROT_READ, MAP_SHARED, fd, start);
+ if (p == MAP_FAILED)
+ SKIP(return, "mmap: %s", strerror(errno));
+
+ /* The BAR can be read while the device is there. */
+ ASSERT_NE(signal(SIGBUS, on_sigbus), SIG_ERR);
+ if (sigsetjmp(fault, 1) == 0) {
+ v = *p;
+ } else {
+ ASSERT_TRUE(false) {
+ TH_LOG("reading the mapping faulted before the removal");
+ }
+ }
+
+ /* Removing the device removes its entry. */
+ snprintf(path, sizeof(path), SYSFS_DEVICES "%s/remove", self->dev);
+ ASSERT_EQ(write_one(path), 0);
+ self->removed = true;
+ EXPECT_EQ(access(self->path, F_OK), -1);
+
+ /*
+ * The file is dead. The mapping isn't procfs's to unmap, it lives in
+ * the iomem address space of /dev/mem, see proc_bus_pci_open(), and
+ * is left alone like a sysfs resource file's.
+ */
+ EXPECT_EQ(pread(fd, &v, sizeof(v), 0), -1);
+ EXPECT_EQ(errno, EIO);
+
+ munmap((void *)p, psz);
+ close(fd);
+}
+
+TEST_HARNESS_MAIN
--
2.53.0
Technical Details
Message-ID:
<20261104-work-procfs-core-v2-20-76ace653a773@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-356799-linux-kernel-netdev=dewy.ruReferences:
<20261104-work-procfs-core-v2-0-76ace653a773@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-356799-linux-kernel-netdev=dewy.ru From: brauner@kernel.org (Christian Brauner) Newsgroups: linux.kernel.netdev Subject: [PATCH RESEND v2 20/23] selftests/proc: check that a mapped entry can be removed Date: Wed, 4 Nov 2026 20:34:05 +0100 Organization: Usenet Network Message-ID: <20261104-work-procfs-core-v2-20-76ace653a773@kernel.org> References: <20261104-work-procfs-core-v2-0-76ace653a773@kernel.org> MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 8bit Injection-Info: usenet.network; logging-data="3510831"; mail-complaints-to="abuse@usenet.network" Cancel-Lock: sha1:QUEX9mc8u651OH6KsIsQsG+Oqm8= sha256:CzPI9nnUefnJLBzA3tRJi7gIcg6PQZ5h5SN3hj6uX9U= sha1:YIF9dhVfCfZdoWj+N+/i0k8iVqY= sha256:eicB+c8Vsbpky8ciapPRV/jgLeNRnxCgF3ZeVrNSQO4= 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 257F5112DDB; Thu, 5 Nov 2026 02:07:55 +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 XSquU3AWfVtk; Thu, 5 Nov 2026 02:07:50 +0300 (MSK) X-Received-SPF: Pass (mailfrom) identity=mailfrom; client-ip=172.105.105.114; helo=tor.lore.kernel.org; envelope-from=netdev+bounces-356799-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 tor.lore.kernel.org (tor.lore.kernel.org [172.105.105.114]) by mail.dewy.ru (Postfix) with ESMTPS id 8A18D112D3B; Thu, 5 Nov 2026 02:07:38 +0300 (MSK) X-Received: from smtp.subspace.kernel.org (conduit.subspace.kernel.org [100.77.114.92]) by tor.lore.kernel.org (Postfix) with ESMTP id 8BE5667BCB; Wed, 4 Nov 2026 20:01:39 +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=1793820871; b=owP4fTAVAQ+C2aIKKL4yKiXGR1fuC2+MVHunjesRGjz3RGMR0noQMc3Mg8ZqCcWiTRlk fYSP4XLGWVPHBMV+Kt9r2LNLrSd4tXrMD16Xx0pODeGAUDZjxsOek9VH40UPcdfG8PyMX ewcPdO6UPNnPTXX+HQOT8nltmaF8EopK679bASYIPCAnGZqe9npQqJaD2nVmAE6YCtkod NpGgk2ak6TAbJX90q/SwIOffAAIl1kR5XJLGNsA5BVjOq5+AYrZ05VfeuDTV6ZP14NWSF krLXFNHHd29NkbDjmrIftowudbYHymcchzNB7s3BXvkBKxwkkwDHF/eFqd9QgmamaFg== X-ARC-Message-Signature: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1793820871; h=X-Original-To:Received:Received:DKIM-Signature:From:To:Cc:Subject: Date:Message-ID:In-Reply-To:References:X-Mailing-List:List-Id: List-Post:List-Help:List-Subscribe:List-Unsubscribe:List-Owner: Precedence:MIME-Version:Content-Type:X-Mailer:X-Developer-Signature: X-Developer-Key:Content-Transfer-Encoding; bh=XPRk8xa2iNqVsjjcUeI0mWPc+8Y/qNwvFsdP9QcHm+Y=; b=f3MSwbzIHR+BCBeckD58cWv58Hl0sexJaIYsJAZQudy8ep0Hm9SUjLqjIA/NYjeVwuPR CQF30V4wiwa7KAdetwxudf6RE5/IQE/MleUZBQ5obYZJUjlSCAKiQV2Deq8nFw0Bdoamc ArVPUSxtjXQujcJ7V7K0NTVmXxKf8jXIhQtZdDGvcyXoZ9+sOd5iyTX8Xu7F7q6AVdpNB vCIC94qS41PQ0AB2SraJ43sUu9Zusg9C/8a/hXP2DsFbrFt4BkK8DF6iCU6yM9j66CfYI pH15DhW1bTnth2tqJUd+3Y73aYz29Ygra+Tz1BDfyPIeyQDuTnrvUs2QkNwj6uLb7Iw== 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=UNhMpYi9; 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 021C1606E821; Wed, 4 Nov 2026 19:34:31 +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=UNhMpYi9 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=1793820860; b=QO+KDV9/JEYtMqhhsBEhh198aPKHBkfZqEZCk9HHWIIe+SDm3dga6oI43zZhotoagQ24 fSwB0GmIptvUOyAFU8hLh7MXiWrQVLz03z2vzBYT/GiQ6/U8qXufudiPrucnG2XnERy5j s6iqJGBL4kM81m9co/IiQKJ7TK6sWLh39guIZlMNEAiwzO5xiGGt1uRbkcj7yqjsT5uLY uQCSvpctCS4UFIiGj0JivGrDp0KSVCuuWDMKqrNQWnZZ/NocqwYazbBhl/aURVTYEvkkn v5ftumZQM2vsvtAcfhzxbUzz7GDU3oIgXEydUZZ6DzS1GDtOFY80X8PsuM1/FWyyrvA== X-ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1793820860; h=Received:DKIM-Signature:From:To:Cc:Subject:Date:Message-ID: In-Reply-To:References:MIME-Version:Content-Type:X-Mailer: X-Developer-Signature:X-Developer-Key:Content-Transfer-Encoding; bh=XPRk8xa2iNqVsjjcUeI0mWPc+8Y/qNwvFsdP9QcHm+Y=; b=INv6CAvfcDL9tL0tBubmouI8P585iiqN7jEfBBiPAIsYD5FDqzQvCDbbj/svpAP/2okW 8Qsy9o5uksZwDa8nbQciIv3lC5qi794qwTDAvPwbhvT3VRS3OXQ7x1aAvMVp66QMOyfhn TCXM9r72J/P5XL/D0+FZiOWXh5C8UBbJTjjl1ZzD57SPCx+u/tJJaAmVDobKkAeuaC77q LpwDC4Mz2BTWMzjtNbwTmQe51MZN0y+zZ0aTZshAXhxHonMqOZWrmt1wuPfummUNkEXxN wQ+9Y13/tVIVXi13hWpxHvWiQQ77riXW2hO3dslQEE64pQRALLVxFVUjOSx6r/KUgeg== 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=UNhMpYi9; 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 9B403606E813; Wed, 4 Nov 2026 19:34:19 +0000 (UTC) X-Received: by smtp.kernel.org (Postfix) with ESMTPSA id 6EAED1F000C1; Wed, 4 Nov 2026 19:34:07 +0000 (UTC) X-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1793820859; bh=XPRk8xa2iNqVsjjcUeI0mWPc+8Y/qNwvFsdP9QcHm+Y=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=UNhMpYi9iBi3sSb8K6vRL3AVudJYWbzQuMGR/ICND1KoaHjOskUuqvumDcfcjfd6K ijYufZXYFtMMXKypD4aHqfrjcDJ5P9E7mGA88Bbc5XKwZS4J2G6bgAUbtjzgydCj5c CMbGq978DMlUEIVBNwLUkmICilkcpIuILUWlDTO9oBqCop6Q2rT2BrBKj9tSMvpI5f mIfr0sAmHMehrxEf8R1Vxd1nxFT9mY8+v+LmVChZXn8FLaY6Ek9gVtZVyHvJ7Cd5NB lpPZDeCXrjw4w6xWu6pGAbvluUnAnWZKtALorygHi4AAnDmt2IczUeBmgec1AG7dqW U2/sWDkdd6LGA== 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-In-Reply-To: <20261104-work-procfs-core-v2-0-76ace653a773@kernel.org> 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-Mailer: b4 0.17-dev X-Developer-Signature: v=1; a=openpgp-sha256; l=6487; i=brauner@kernel.org; h=from:subject:message-id; bh=jPDbXGY4cbCvO3GA1X2/Ctdyj/e7Mw7ZeqOfGQfxULg=; b=owGbwMvMwCU28Zj0gdSKO4sYT6slMWS9bu/8xLCt3Udlh9rJxEtBLOuvKWxb7B30qu+8xJIGm /Db96fUd5SyMIhxMciKKbI4tJuEyy3nqdhslKkBM4eVCWQIAxenAEwkQ4mR4fwquSyvtz3Ne746 HTn1fX1AAaOl3YVTD7n+Heo0WvX+kQPDfyf/Xm+TiXFyJoacvlEsv149+Fb4tz9xPvfn/NiLR3d aMwAA X-Developer-Key: i=brauner@kernel.org; a=openpgp; fpr=4880B8C9BD0E5106FC070F4F7B3C391EFEA93624