Thread View: linux.kernel.netdev
177 messages
177 total messages
Page 1 of 4
Started by Christian Brauner
Mon, 02 Nov 2026 14:19
Page 1 of 4 • 177 total messages
[PATCH RFC 0/7] proc: move procfs to what kernfs does
Author: Christian Brauner
Date: Mon, 02 Nov 2026 14:19
Date: Mon, 02 Nov 2026 14:19
122 lines
16107 bytes
16107 bytes
Ok, as mentioned off-list I have an RFC version of getting rid of page
fault induced stalls in procfs when removing entry which plagues
networking devices among others.
There was a hackish very narrow AI solution to a part of this bug class
that was floated off-list. It used a second buffering mechanism on top
of seqfiles which was really nasty. Plus, it didn't fix the whole bug
class, it only eliminated a small portion. If we want to get rid of the
whole page fault issue we need a bigger hammer. So I tried to work
through that over the last couple of weeks. The series in a shape where
it's not totally insane to have someone else look at it. So here it is.
I understand it's big but I thought it's probably nicer to look at the
end result than just making a promise of what it might end up looking
like.
It's not super polished yet and I haven't gotten into sorting out all
the warts as there were some really gnarly bugs in there like the whole
readdir mess that repeated entries or terminated early. I think we could
go a lot further there if we wanted to but I think that blows this out
of proportion and at some point it becomes difficult to grasp the scope.
So I'd rather delay that until I have a sense of how acceptable this all
is.
remove_proc_entry() waits until every call into the entry has returned,
and most of those calls copy to or from userspace with the entry pinned.
A reader or writer can make a page fault in that copy take as long as
it wants, with userfaultfd or by punching holes into a memfd over and
over. And the callers of remove_proc_entry() like to hold locks while
they wait. addrconf holds RTNL when it removes /proc/net/dev_snmp6/<dev>
and xt_hashlimit holds a mutex that all network namespaces share.
Every network device with IPv6 gets a /proc/net/dev_snmp6/<dev> that
everyone can read, and addrconf_ifdown() removes it with RTNL held. So
any local user can keep RTNL for as long as their read stays stalled.
Until then no device can be registered, removed or renamed and no
network namespace can be created or torn down.
Without privileges a FUSE mount in a user namespace whose daemon never
answers is enough to stall the copy. Killing the reader ends it, but
until then whoever removes the file sits in D state.
xt_hashlimit removes the proc file of a table with hashlimit_mutex
held. Every rule insert in every network namespace takes that mutex and
so does network namespace cleanup. cleanup_net() runs on a single
workqueue with pernet_ops_rwsem held, so a stalled reader also stops
every later network namespace teardown and every module load that
registers pernet operations. xt_recent does the same with recent_mutex.
And it's not specific to networking. remove_proc_entry(), proc_remove()
and remove_proc_subtree() have over 200 callers and plenty of them hold
locks. A stalled reader of /proc/fs/ext4/<dev>/options holds up the
unmount of that filesystem with s_umount held, and every sync(2) with
it. The same goes for PCI hot-removal, for freeing IRQ descriptors,
which CPU bring-up waits on, and for unplugging a USB sound card.
syzbot has hit removal under locks before (cf. [1]), and the xt_recent
deadlock fixed in (1) below is another one.
Stalling the kernel in a copy to or from userspace is a well-known
exploitation primitive. It's why unprivileged userfaultfd can't do it
by default anymore since v5.11 (d0d4730ac2e4), and FUSE has been used
the same way.
kernfs doesn't have that problem. The code of a kernfs file only ever
runs with the node pinned and never touches userspace memory, kernfs
copies in and out itself. This moves procfs over to the same scheme.
sysfs has only held its active reference around ->show() and ->store()
since v2.6.23, the same release that made remove_proc_entry() wait for
the entry's callers. A copy to userspace couldn't be stalled for long
back then. userfaultfd only came in v4.3 and FUSE in user namespaces in
v4.18. configfs moved its copies out of its active reference in v5.3
and DRM copies ioctl arguments in and out around the driver's handler
the same way. debugfs still holds its reference across the copies,
ran into a deadlock with wifi drivers because of it and grew an API to
cancel operations instead (cf. [2]).
That touches just about every ->proc_read(), ->proc_write() and
->proc_ioctl() in the tree, so I've split it up into seven series. They
go out as replies to this mail:
(1) proc: fix a few old bugs in /proc handlers (38 patches)
Bugs in the handlers and in procfs itself that are a lot older than
the conversion and that reviewing it turned up. Most of them are for
stable. They come first so they can go there on their own and the
conversion doesn't have to bother with that crap.
(2) proc: let procfs own the data path (16 patches)
The procfs side. procfs owns the seq_file of an entry, copies in
writes, copies out reads and copies ioctl arguments in and out, and
only calls into an entry with it pinned. proc_create_seq() and
friends and the plain seq_file users are switched over here too.
(3) proc: convert the arch and platform entries (22 patches)
(4) proc: convert the driver entries (23 patches)
(5) proc: convert the fs, kernel and mm entries (27 patches)
(6) proc: convert the net entries and the kfifo samples (11 patches)
(7) proc: drop the __user methods (5 patches)
Drops the old methods for every entry that can be removed and gives
the new ones the names kernfs uses, ->proc_read(), ->proc_write()
and ->proc_ioctl(). This needs all four conversion series.
The series should all indicate cleanly what dependencies they have so b4
should be able to pull it for review. But I could be wrong.
With all of it on a KASAN and lockdep kernel the removal selftests of
every part pass, the proc, pktgen and allocinfo selftests behave like
without it and the bugs from (1) that reproduce in a VM are gone. Every
patch builds on its own on x86_64 and i386, and with W=1 on the
architectures it touches.
[1]: https://lore.kernel.org/r/000000000000f24f4705b6988f3e@google.com
[2]: https://lore.kernel.org/r/20231109212251.213873-7-johannes@sipsolutions.net
--
Christian Brauner <brauner@kernel.org>
[PATCH RFC 00/38] proc: fix a few old bugs in /proc handlers
Author: Christian Brauner
Date: Mon, 02 Nov 2026 14:21
Date: Mon, 02 Nov 2026 14:21
211 lines
22150 bytes
22150 bytes
Moving procfs to what kernfs does touches just about every
->proc_read(), ->proc_write() and ->proc_ioctl() in the tree. Reviewing
that turned up a bunch of bugs that have nothing to do with the
conversion and are a lot older. They're fixed here first so they can go
to stable on their own and the conversion doesn't have to carry them.
Two of them matter for the conversion itself:
- proc_bus_pci_write() truncates the file position to an int. A
pwrite() at an offset like 0xfffffc00 makes it negative and the loop
then writes a lot more than it was handed into config space at
negative offsets. Once procfs hands it a kernel copy of the write
that's a heap out-of-bounds read, so this has to be fixed before the
PCI conversion:
BUG: KASAN: slab-out-of-bounds in proc_bus_pci_write+0x528/0x5c0
Read of size 4 at addr ff11000112103b20 by task pci-check/241
Call Trace:
proc_bus_pci_write+0x528/0x5c0
proc_reg_write+0x2d7/0x730
vfs_write+0x1cd/0xfa0
__x64_sys_pwrite64+0x191/0x250
isapnp has the same bug on the read side.
- xt_recent removes the proc entries of its tables with a spinlock held
when a network namespace goes away. remove_proc_entry() waits for the
readers and a reader takes that very lock. That's a deadlock.
/proc/sys readdir resumes a listing at a counted position. The entries
are sorted by name, so a device that comes or goes in front of that
position shifts them and the next getdents64() repeats an entry or
skips one. Even ls needs a second call to find the end of a directory.
It resumes after the name of the last entry it returned now, the way
kernfs resumes after its last node, and a selftest lists a directory in
two pieces around a device coming and going. That one is first, it
goes to stable on its own.
/proc/sys also has the bug the conversion fixes for the rest of /proc.
proc_sys_call_handler() copies what is read and written and
proc_sys_readdir() copies the names of the entries to and from userspace
while the sysctl tables are in use. Unregistering a table waits until
nobody uses it anymore. So a read(), write() or getdents64() whose
buffer faults for as long as userspace wants keeps
unregister_sysctl_table() waiting. addrconf unregisters the ipv6 sysctls
of a device under RTNL, so that stalls every RTNL user. The handler
copies in before it grabs the table and out after letting go of it.
readdir doesn't take the tables into use at all anymore. It pins the
memory of the entry it is at, which unregistering doesn't wait for,
finds the next one under sysctl_lock and emits with nothing held, the
way kernfs_fop_readdir() and proc_readdir_de() work. That's the same
one trip through sysctl_lock per entry as before. A selftest covers all
three.
/proc symlinks have it as well. proc_get_link() pins the entry and
vfs_readlink() copies the link to userspace before the delayed call
unpins it. ALSA removes /proc/asound/<id> with snd_card_mutex and
info_mutex held when a card changes its id, so a faulting readlink()
holds those up. The pin keeps nothing alive, the inode holds
the entry, so it goes. A selftest covers that too.
Chris' review of the conversion turned up more old bugs, in procfs
itself and in its users. Those are here as well:
- __xlate_proc_name() walks through whatever it finds for the
components of a name, regular files included, and pktgen hands it the
names that the writer of "add_device" picks. So an unprivileged user
in namespaces of its own can hang an entry below another file and get
it freed behind pktgen's back. procfs doesn't walk through anything
but directories anymore and pktgen refuses names with a '/'.
- SUNRPC moved the waitqueue that poll() and epoll use for its channel
files into the cache_detail of a network namespace. That's freed when
the namespace goes away, but procfs leaves the waiters of a removed
file queued. That's a use-after-free an unprivileged user can trigger
too. It goes back to a single static waitqueue. procfs should really
own the waitqueues of its files like kernfs does. I'll do that
separately, it's nothing for stable.
- proc_register() WARNs when a name is already registered, and pktgen
lets userspace pick names that collide with its own files. It reports
the duplicate with pr_warn() and a stack dump now, like sysfs does.
- proc_readdir_de() resumes at a counted position just like /proc/sys
and on top of that ends a listing early when the entry it is emitting
goes away. It resumes after the last name now too, and a selftest
covers both.
- proc_remove() removes by name, so removing an entry that is already
gone can take out a different one. It removes the entry it's given
now. And procfs warns about entries that a dying network namespace
leaves in /proc/net instead of silently freeing their parent.
- On NOMMU every /proc file can be mapped privately, which reads it with
nommu_region_sem held, and reading /proc/maps takes that lock too. Any
user can hang the machine with that. procfs files can't be mapped on
NOMMU anymore.
- esas2r takes the lengths of its HBA, SMP and firmware ioctls from
userspace without checking them against the buffer they're copied
into, through a file everyone can open.
- vmcore_cleanup() can run twice at once. powerpc's copy_oldmem_page()
doesn't check ioremap_cache() and only recovers from a machine check
for read(), not for splice() or vmcore's own reads. The pseries ofdt
parser can read past its input and lparcfg reports a failed hcall as
a huge write.
The rest is small: dasd looks in front of its buffer on an empty write,
toshiba_acpi's input checks can't fail, simdisk reads the file name
without the lock that protects it, rtas_flash can read past its message
buffer, rtasd hands out whatever vmalloc() left in its log buffer,
ALSA's info layer doesn't clamp writes past the end of a data entry,
BusLogic, ips and libipw return 0 from writes so stdio writers spin
forever, the scsi host files and um's exitcode parse what is written
without NUL-terminating it, and ntfs3 warns when a huge label is
written.
The PCI, xt_recent, sysctl, readdir, readlink, pktgen, SUNRPC,
duplicate name and /proc readdir bugs reproduce on a KASAN and lockdep
kernel in a VM and are gone with the series. The rest is build tested
only.
The procfs conversion is on top of this, starting with "proc: let
procfs own the data path".
Signed-off-by: Christian Brauner (Amutable) <brauner@kernel.org>
---
Christian Brauner (38):
PCI/proc: don't truncate the position of config space accesses
PNP: isapnp: don't read past the end of the buffer on a large pread()
netfilter: xt_recent: don't remove the proc entries under a spinlock
s390/dasd: don't look in front of the buffer on an empty write
platform/x86: toshiba_acpi: reject malformed brightness and fan writes
xtensa: simdisk: don't read the file name without the lock
powerpc/rtas_flash: don't read past the validate message
powerpc/rtasd: zero the error log buffer
ALSA: info: don't write past the end of a data entry
scsi: BusLogic: don't return 0 from writes to the proc file
scsi: ips: don't return 0 from writes to the proc file
wifi: libipw: don't return 0 from writes to debug_level
scsi: core: NUL-terminate what is written to the host proc files
um: NUL-terminate what is written to /proc/exitcode
ntfs3: don't warn when a huge label is written
proc: don't walk through a non-directory when resolving a name
net: pktgen: reject device names with a '/'
proc: don't WARN about a name that is already registered
proc: resume readdir after the last name it returned
selftests/proc: check a /proc listing while entries come and go
proc: let proc_remove() remove the entry it was given
proc: warn about entries left in /proc/net of a dying namespace
proc: don't let NOMMU map procfs files
proc/vmcore: don't let vmcore_cleanup() run twice at once
powerpc/crash_dump: check ioremap_cache() in copy_oldmem_page()
powerpc/crash_dump: recover from a machine check in copy_oldmem_page()
sunrpc: make queue_wait a global wait queue again
scsi: esas2r: bound the lengths of the HBA, SMP and firmware ioctls
powerpc/pseries: bound the lengths in the ofdt property parser
powerpc/pseries/lparcfg: return h_get_ppp()'s status as a long
proc: sysctl: resume readdir after the last name it returned
selftests/proc: check a sysctl listing while entries come and go
proc: sysctl: copy to and from userspace without holding the table
proc: sysctl: look at the table under sysctl_lock when creating an inode
proc: sysctl: don't hold the tables while readdir copies to userspace
selftests/proc: check that faulting sysctl accesses don't hold up removal
proc: don't pin a symlink's entry while readlink() copies it out
selftests/proc: check that a faulting readlink() doesn't hold up removal
arch/powerpc/kernel/crash_dump.c | 6 +-
arch/powerpc/kernel/rtas_flash.c | 2 +-
arch/powerpc/kernel/rtasd.c | 2 +-
arch/powerpc/platforms/pseries/lparcfg.c | 2 +-
arch/powerpc/platforms/pseries/reconfig.c | 18 +-
arch/um/kernel/exitcode.c | 3 +-
arch/xtensa/platforms/iss/simdisk.c | 25 +-
drivers/net/wireless/intel/ipw2x00/libipw_module.c | 2 +-
drivers/pci/proc.c | 15 +-
drivers/platform/x86/toshiba_acpi.c | 8 +-
drivers/pnp/isapnp/proc.c | 6 +-
drivers/s390/block/dasd.c | 2 +-
drivers/scsi/BusLogic.c | 2 +-
drivers/scsi/esas2r/esas2r.h | 2 +-
drivers/scsi/esas2r/esas2r_ioctl.c | 41 ++-
drivers/scsi/esas2r/esas2r_main.c | 3 +-
drivers/scsi/ips.c | 2 +-
drivers/scsi/scsi_proc.c | 1 +
fs/ntfs3/super.c | 11 +-
fs/proc/generic.c | 174 ++++++---
fs/proc/inode.c | 38 +-
fs/proc/internal.h | 1 +
fs/proc/proc_net.c | 7 +
fs/proc/proc_sysctl.c | 389 ++++++++++++++-------
fs/proc/root.c | 1 +
fs/proc/vmcore.c | 5 +
include/linux/sunrpc/cache.h | 1 -
net/core/pktgen.c | 4 +
net/netfilter/xt_recent.c | 4 +-
net/sunrpc/cache.c | 12 +-
sound/core/info.c | 2 +
tools/testing/selftests/proc/.gitignore | 5 +
tools/testing/selftests/proc/Makefile | 5 +
tools/testing/selftests/proc/config | 6 +
tools/testing/selftests/proc/proc-readdir-churn.c | 236 +++++++++++++
.../selftests/proc/proc-remove-slow-readlink.c | 191 ++++++++++
.../selftests/proc/proc-remove-slow-sysctl.c | 213 +++++++++++
.../selftests/proc/proc-sysctl-readdir-churn.c | 147 ++++++++
38 files changed, 1342 insertions(+), 252 deletions(-)
---
base-commit: 65ba8f34ec886ec72b2c07ddc8cf3d815c32869d
change-id: 20261101-work-procfs-bugfixes-054d00028491
[PATCH RFC 01/38] PCI/proc: don't truncate the position of config space accesses
Author: Christian Brauner
Date: Mon, 02 Nov 2026 14:21
Date: Mon, 02 Nov 2026 14:21
83 lines
12723 bytes
12723 bytes
proc_bus_pci_write() truncates the file position to an int. A pwrite()
at an offset like 0xfffff000 turns it negative, the check against the
size of the config space passes and the count it clamps to ends up
larger than what was written. The loop then reads past the end of the
caller's buffer and writes all of it into config space at negative
offsets, which pci_user_write_config_*() pass on to the config
accessors unchecked. The write also returns more than it was asked to
write.
An offset above 4G that truncates to a small positive value writes to
the start of config space instead. proc_bus_pci_read() keeps the
position in an unsigned int, so a read at such an offset returns the
start of config space instead of hitting EOF.
Check the position against the size of the config space before
narrowing it.
Fixes: 1da177e4c3f4 ("Linux-2.6.12-rc2")
Cc: stable@vger.kernel.org
Signed-off-by: Christian Brauner (Amutable) <brauner@kernel.org>
---
drivers/pci/proc.c | 15 ++++++++-------
1 file changed, 8 insertions(+), 7 deletions(-)
diff --git a/drivers/pci/proc.c b/drivers/pci/proc.c
index 3d7a9ed5fc22..e0dc70123f82 100644
--- a/drivers/pci/proc.c
+++ b/drivers/pci/proc.c
@@ -32,8 +32,7 @@ static ssize_t proc_bus_pci_read(struct file *file, char __user *buf,
size_t nbytes, loff_t *ppos)
{
struct pci_dev *dev = pde_data(file_inode(file));
- unsigned int pos = *ppos;
- unsigned int cnt, size;
+ unsigned int pos, cnt, size;
/*
* Normal users can read only the standardized portion of the
@@ -51,8 +50,9 @@ static ssize_t proc_bus_pci_read(struct file *file, char __user *buf,
if (!nbytes)
return 0;
- if (pos >= size)
+ if (*ppos >= size)
return 0;
+ pos = *ppos;
if (nbytes >= size)
nbytes = size;
if (pos + nbytes > size)
@@ -119,9 +119,8 @@ static ssize_t proc_bus_pci_write(struct file *file, const char __user *buf,
{
struct inode *ino = file_inode(file);
struct pci_dev *dev = pde_data(ino);
- int pos = *ppos;
int size = dev->cfg_size;
- int cnt, ret;
+ int pos, cnt, ret;
ret = security_locked_down(LOCKDOWN_PCI_ACCESS);
if (ret)
@@ -130,14 +129,16 @@ static ssize_t proc_bus_pci_write(struct file *file, const char __user *buf,
if (!nbytes)
return 0;
+ if (*ppos >= size)
+ return 0;
+ pos = *ppos;
+
if (resource_is_exclusive(&dev->driver_exclusive_resource, pos, nbytes)) {
pci_warn_once(dev, "%s: Unexpected write to kernel-exclusive config offset %x",
current->comm, pos);
add_taint(TAINT_USER, LOCKDEP_STILL_OK);
}
- if (pos >= size)
- return 0;
if (nbytes >= size)
nbytes = size;
if (pos + nbytes > size)
--
2.53.0
[PATCH RFC 02/38] PNP: isapnp: don't read past the end of the buffer on a large pread()
Author: Christian Brauner
Date: Mon, 02 Nov 2026 14:21
Date: Mon, 02 Nov 2026 14:21
41 lines
11329 bytes
11329 bytes
isapnp_proc_bus_read() truncates the position to an int. A pread() at
an offset like 0xfffffc00 turns it negative, the check against the size
of the configuration space passes, and the loop then puts everything
from that position up to byte 256 into the reader's buffer, no matter
how few bytes were asked for.
Keep the position as a loff_t and clamp the count against what is left
of the configuration space.
Fixes: 1da177e4c3f4 ("Linux-2.6.12-rc2")
Cc: stable@vger.kernel.org
Signed-off-by: Christian Brauner (Amutable) <brauner@kernel.org>
---
drivers/pnp/isapnp/proc.c | 6 ++----
1 file changed, 2 insertions(+), 4 deletions(-)
diff --git a/drivers/pnp/isapnp/proc.c b/drivers/pnp/isapnp/proc.c
index c1f6e6bd0eb2..8197c7f29743 100644
--- a/drivers/pnp/isapnp/proc.c
+++ b/drivers/pnp/isapnp/proc.c
@@ -23,14 +23,12 @@ static ssize_t isapnp_proc_bus_read(struct file *file, char __user * buf,
size_t nbytes, loff_t * ppos)
{
struct pnp_dev *dev = pde_data(file_inode(file));
- int pos = *ppos;
+ loff_t pos = *ppos;
int cnt, size = 256;
if (pos >= size)
return 0;
- if (nbytes >= size)
- nbytes = size;
- if (pos + nbytes > size)
+ if (nbytes > size - pos)
nbytes = size - pos;
cnt = nbytes;
--
2.53.0
[PATCH RFC 03/38] netfilter: xt_recent: don't remove the proc entries under a spinlock
Author: Christian Brauner
Date: Mon, 02 Nov 2026 14:21
Date: Mon, 02 Nov 2026 14:21
68 lines
12679 bytes
12679 bytes
recent_proc_net_exit() removes the proc entries of all tables with
recent_lock held. remove_proc_entry() waits until nobody calls into the
entry anymore, and reading one of them takes recent_lock in
recent_seq_start() while procfs has the entry pinned. So when a reader
gets there while the network namespace goes away, the exit path sleeps
with a spinlock held and bh disabled, waiting for a reader that spins
on that very lock:
BUG: sleeping function called from invalid context at kernel/sched/completion.c:116
in_atomic(): 1, irqs_disabled(): 0, non_block: 0, pid: 12, name: kworker/u16:0
locks held by kworker/u16:0/12: 4, last CPU#2:
#2: (pernet_ops_rwsem){++++}-{4:4}, at: cleanup_net+0xbf/0x860
#3: (recent_lock){+...}-{3:3}, at: recent_net_exit+0x3d/0x170 [xt_recent]
Workqueue: netns cleanup_net
Call Trace:
wait_for_completion+0x8e/0x400
proc_entry_rundown+0x1ab/0x1f0
remove_proc_entry+0x1ab/0x530
recent_net_exit+0x9f/0x170 [xt_recent]
ops_undo_list+0x271/0x8d0
cleanup_net+0x3f3/0x860
process_one_work+0x946/0x1820
BUG: scheduling while atomic: kworker/u16:0/12/0x00000202
watchdog: BUG: soft lockup - CPU#0 stuck for 26s! [rloop:252]
Call Trace:
_raw_spin_lock_bh+0x7a/0x80
recent_seq_start+0x86/0x1e0 [xt_recent]
seq_read_iter+0x26d/0x1230
seq_read+0x2a9/0x560
proc_reg_read+0x1c2/0x3c0
vfs_read+0x176/0xb40
__x64_sys_pread64+0x191/0x250
Tables are added and removed with recent_mutex held and that's also
what recent_mt_destroy() looks at ->xt_recent under. Nothing that runs
with a proc entry pinned takes it. So take recent_mutex instead.
Fixes: 665e205c16c1 ("netfilter: xt_recent: fix namespace destroy path")
Cc: stable@vger.kernel.org
Signed-off-by: Christian Brauner (Amutable) <brauner@kernel.org>
---
net/netfilter/xt_recent.c | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/net/netfilter/xt_recent.c b/net/netfilter/xt_recent.c
index 55f1f2d89952..11acb9a50d4b 100644
--- a/net/netfilter/xt_recent.c
+++ b/net/netfilter/xt_recent.c
@@ -652,12 +652,12 @@ static void __net_exit recent_proc_net_exit(struct net *net)
* that the parent xt_recent proc entry is empty before trying to
* remove it.
*/
- spin_lock_bh(&recent_lock);
+ mutex_lock(&recent_mutex);
list_for_each_entry(t, &recent_net->tables, list)
remove_proc_entry(t->name, recent_net->xt_recent);
recent_net->xt_recent = NULL;
- spin_unlock_bh(&recent_lock);
+ mutex_unlock(&recent_mutex);
remove_proc_entry("xt_recent", net->proc_net);
}
--
2.53.0
[PATCH RFC 04/38] s390/dasd: don't look in front of the buffer on an empty write
Author: Christian Brauner
Date: Mon, 02 Nov 2026 14:21
Date: Mon, 02 Nov 2026 14:21
28 lines
10995 bytes
10995 bytes
dasd_get_user_string() strips a trailing newline by looking at the last
byte that was written. For an empty write that's the byte in front of
the buffer it just allocated. Only look if something was written.
Fixes: 1da177e4c3f4 ("Linux-2.6.12-rc2")
Cc: stable@vger.kernel.org
Signed-off-by: Christian Brauner (Amutable) <brauner@kernel.org>
---
drivers/s390/block/dasd.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/drivers/s390/block/dasd.c b/drivers/s390/block/dasd.c
index 2ffe404b890f..e97891e58957 100644
--- a/drivers/s390/block/dasd.c
+++ b/drivers/s390/block/dasd.c
@@ -933,7 +933,7 @@ char *dasd_get_user_string(const char __user *user_buf, size_t user_len)
return ERR_PTR(-EFAULT);
}
/* got the string, now strip linefeed. */
- if (buffer[user_len - 1] == '\n')
+ if (user_len && buffer[user_len - 1] == '\n')
buffer[user_len - 1] = 0;
else
buffer[user_len] = 0;
--
2.53.0
[PATCH RFC 05/38] platform/x86: toshiba_acpi: reject malformed brightness and fan writes
Author: Christian Brauner
Date: Mon, 02 Nov 2026 14:21
Date: Mon, 02 Nov 2026 14:21
47 lines
11904 bytes
11904 bytes
When lcd_proc_write() and fan_proc_write() were refactored their checks
were inverted but the && stayed. They only fail if sscanf() didn't
parse anything and the value is out of range at the same time, and for
the brightness value < 0 && value > levels can't be true at all. So an
out of range brightness or fan setting is passed on to the hardware,
and so is the uninitialized value if nothing could be parsed.
Restore the checks as they were before the refactoring.
Fixes: e1a949c1b988 ("toshiba_acpi: Refactor *{get, set} functions return value")
Fixes: 3e07e5baa2af ("toshiba_acpi: Add set_fan_status function")
Cc: stable@vger.kernel.org
Signed-off-by: Christian Brauner (Amutable) <brauner@kernel.org>
---
drivers/platform/x86/toshiba_acpi.c | 8 ++++----
1 file changed, 4 insertions(+), 4 deletions(-)
diff --git a/drivers/platform/x86/toshiba_acpi.c b/drivers/platform/x86/toshiba_acpi.c
index a0b8060836d0..9eb8f1e5aeda 100644
--- a/drivers/platform/x86/toshiba_acpi.c
+++ b/drivers/platform/x86/toshiba_acpi.c
@@ -1498,8 +1498,8 @@ static ssize_t lcd_proc_write(struct file *file, const char __user *buf,
cmd[len] = '\0';
levels = dev->backlight_dev->props.max_brightness + 1;
- if (sscanf(cmd, " brightness : %i", &value) != 1 &&
- value < 0 && value > levels)
+ if (sscanf(cmd, " brightness : %i", &value) != 1 ||
+ value < 0 || value >= levels)
return -EINVAL;
if (set_lcd_brightness(dev, value))
@@ -1678,8 +1678,8 @@ static ssize_t fan_proc_write(struct file *file, const char __user *buf,
return -EFAULT;
cmd[len] = '\0';
- if (sscanf(cmd, " force_on : %i", &value) != 1 &&
- value != 0 && value != 1)
+ if (sscanf(cmd, " force_on : %i", &value) != 1 ||
+ value < 0 || value > 1)
return -EINVAL;
if (set_fan_status(dev, value))
--
2.53.0
[PATCH RFC 06/38] xtensa: simdisk: don't read the file name without the lock
Author: Christian Brauner
Date: Mon, 02 Nov 2026 14:21
Date: Mon, 02 Nov 2026 14:21
55 lines
11831 bytes
11831 bytes
proc_read_simdisk() looks at dev->filename without holding dev->lock. A
write to the same file detaches the disk and frees the name under the
lock, so a concurrent reader can copy out freed memory. Copy the name
with the lock held.
Fixes: b6c7e873daf7 ("xtensa: ISS: add host file-based simulated disk")
Cc: stable@vger.kernel.org
Signed-off-by: Christian Brauner (Amutable) <brauner@kernel.org>
---
arch/xtensa/platforms/iss/simdisk.c | 25 ++++++++++++-------------
1 file changed, 12 insertions(+), 13 deletions(-)
diff --git a/arch/xtensa/platforms/iss/simdisk.c b/arch/xtensa/platforms/iss/simdisk.c
index a2ddb49c269d..7b115e023de9 100644
--- a/arch/xtensa/platforms/iss/simdisk.c
+++ b/arch/xtensa/platforms/iss/simdisk.c
@@ -238,22 +238,21 @@ static ssize_t proc_read_simdisk(struct file *file, char __user *buf,
size_t size, loff_t *ppos)
{
struct simdisk *dev = pde_data(file_inode(file));
- const char *s = dev->filename;
- if (s) {
- ssize_t len = strlen(s);
- char *temp = kmalloc(len + 2, GFP_KERNEL);
+ ssize_t len;
+ char *temp;
- if (!temp)
- return -ENOMEM;
+ /* a write sets at most a page, see proc_write_simdisk() */
+ temp = kmalloc(PAGE_SIZE + 2, GFP_KERNEL);
+ if (!temp)
+ return -ENOMEM;
- len = scnprintf(temp, len + 2, "%s\n", s);
- len = simple_read_from_buffer(buf, size, ppos,
- temp, len);
+ spin_lock(&dev->lock);
+ len = scnprintf(temp, PAGE_SIZE + 2, "%s\n", dev->filename ?: "");
+ spin_unlock(&dev->lock);
- kfree(temp);
- return len;
- }
- return simple_read_from_buffer(buf, size, ppos, "\n", 1);
+ len = simple_read_from_buffer(buf, size, ppos, temp, len);
+ kfree(temp);
+ return len;
}
static ssize_t proc_write_simdisk(struct file *file, const char __user *buf,
--
2.53.0
[PATCH RFC 07/38] powerpc/rtas_flash: don't read past the validate message
Author: Christian Brauner
Date: Mon, 02 Nov 2026 14:21
Date: Mon, 02 Nov 2026 14:21
30 lines
11337 bytes
11337 bytes
get_validate_flash_msg() adds up what snprintf() returns, which is the
length of the whole string and not what fit into msg. If the update
results the firmware hands back don't fit into what is left of the 256
bytes, the length ends up larger than msg and validate_flash_read()
copies whatever follows it on the stack to userspace. Use scnprintf().
Fixes: a94a14720eaf ("powerpc/rtas_flash: Fix validate_flash buffer overflow issue")
Cc: stable@vger.kernel.org
Signed-off-by: Christian Brauner (Amutable) <brauner@kernel.org>
---
arch/powerpc/kernel/rtas_flash.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/arch/powerpc/kernel/rtas_flash.c b/arch/powerpc/kernel/rtas_flash.c
index 722dbfb6fbf8..f1cd105f7426 100644
--- a/arch/powerpc/kernel/rtas_flash.c
+++ b/arch/powerpc/kernel/rtas_flash.c
@@ -448,7 +448,7 @@ static int get_validate_flash_msg(struct rtas_validate_flash_t *args_buf,
n = sprintf(msg, "%d\n", args_buf->update_results);
if ((args_buf->update_results >= VALIDATE_CUR_UNKNOWN) ||
(args_buf->update_results == VALIDATE_TMP_UPDATE))
- n += snprintf(msg + n, msglen - n, "%s\n",
+ n += scnprintf(msg + n, msglen - n, "%s\n",
args_buf->buf);
} else {
n = sprintf(msg, "%d\n", args_buf->status);
--
2.53.0
[PATCH RFC 08/38] powerpc/rtasd: zero the error log buffer
Author: Christian Brauner
Date: Mon, 02 Nov 2026 14:21
Date: Mon, 02 Nov 2026 14:21
29 lines
11072 bytes
11072 bytes
rtas_log_read() hands out a whole slot of the error log but
pSeries_log_error() only fills in the sequence number and the event,
which is usually a lot shorter. The rest of the slot is whatever
vmalloc() left there. Use vzalloc().
Fixes: 1da177e4c3f4 ("Linux-2.6.12-rc2")
Cc: stable@vger.kernel.org
Signed-off-by: Christian Brauner (Amutable) <brauner@kernel.org>
---
arch/powerpc/kernel/rtasd.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/arch/powerpc/kernel/rtasd.c b/arch/powerpc/kernel/rtasd.c
index fd40864bdb70..dd0df5e164a3 100644
--- a/arch/powerpc/kernel/rtasd.c
+++ b/arch/powerpc/kernel/rtasd.c
@@ -523,7 +523,7 @@ static int __init rtas_event_scan_init(void)
rtas_error_log_max = rtas_get_error_log_max();
rtas_error_log_buffer_max = rtas_error_log_max + sizeof(int);
- rtas_log_buf = vmalloc(array_size(LOG_NUMBER,
+ rtas_log_buf = vzalloc(array_size(LOG_NUMBER,
rtas_error_log_buffer_max));
if (!rtas_log_buf) {
printk(KERN_ERR "rtasd: no memory\n");
--
2.53.0
[PATCH RFC 09/38] ALSA: info: don't write past the end of a data entry
Author: Christian Brauner
Date: Mon, 02 Nov 2026 14:21
Date: Mon, 02 Nov 2026 14:21
33 lines
11322 bytes
11322 bytes
snd_info_entry_write() clamps the count to what is left of the entry
by computing entry->size - pos as a size_t. For a position past the end
that wraps and the write goes to the driver unclamped, for opl4 into
sample memory outside of what the entry covers. At the end of the entry
the driver gets a count of 0 and vmalloc(0) warns. Before the clamp
moved into the common code the driver ignored such writes. Fail them
with -ENOSPC instead, like a block device does at the end of the
device. Returning 0 would make writers that use stdio retry forever.
Fixes: d97e1b78239c ("ALSA: info - Check file position validity in common layer")
Cc: stable@vger.kernel.org
Signed-off-by: Christian Brauner (Amutable) <brauner@kernel.org>
---
sound/core/info.c | 2 ++
1 file changed, 2 insertions(+)
diff --git a/sound/core/info.c b/sound/core/info.c
index c4d57d0809ad..982087bc8575 100644
--- a/sound/core/info.c
+++ b/sound/core/info.c
@@ -171,6 +171,8 @@ static ssize_t snd_info_entry_write(struct file *file, const char __user *buffer
pos = *offset;
if (!valid_pos(pos, count))
return -EIO;
+ if (pos >= entry->size)
+ return count ? -ENOSPC : 0;
if (count > 0) {
size_t maxsize = entry->size - pos;
count = min(count, maxsize);
--
2.53.0
[PATCH RFC 10/38] scsi: BusLogic: don't return 0 from writes to the proc file
Author: Christian Brauner
Date: Mon, 02 Nov 2026 14:21
Date: Mon, 02 Nov 2026 14:21
27 lines
10951 bytes
10951 bytes
A write to /proc/scsi/BusLogic/<n> resets the statistics and returns 0.
Writers that use stdio, echo for example, take that as no progress and
retry forever. Return the number of bytes that were written.
Fixes: 1da177e4c3f4 ("Linux-2.6.12-rc2")
Signed-off-by: Christian Brauner (Amutable) <brauner@kernel.org>
---
drivers/scsi/BusLogic.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/drivers/scsi/BusLogic.c b/drivers/scsi/BusLogic.c
index 27b446c0c5d0..7ad814f549ae 100644
--- a/drivers/scsi/BusLogic.c
+++ b/drivers/scsi/BusLogic.c
@@ -3328,7 +3328,7 @@ static int blogic_write_info(struct Scsi_Host *shost, char *procbuf,
adapter->ext_resets = 0;
adapter->adapter_intern_errors = 0;
memset(tgt_stats, 0, BLOGIC_MAXDEV * sizeof(struct blogic_tgt_stats));
- return 0;
+ return bytes_avail;
}
static int blogic_show_info(struct seq_file *m, struct Scsi_Host *shost)
--
2.53.0
[PATCH RFC 11/38] scsi: ips: don't return 0 from writes to the proc file
Author: Christian Brauner
Date: Mon, 02 Nov 2026 14:21
Date: Mon, 02 Nov 2026 14:21
28 lines
10759 bytes
10759 bytes
ips_write_info() ignores what is written to /proc/scsi/ips/<n> and
returns 0. Writers that use stdio, echo for example, take that as no
progress and retry forever. Return the number of bytes that were
written.
Fixes: 1da177e4c3f4 ("Linux-2.6.12-rc2")
Signed-off-by: Christian Brauner (Amutable) <brauner@kernel.org>
---
drivers/scsi/ips.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/drivers/scsi/ips.c b/drivers/scsi/ips.c
index 45c5b9acb664..bf3a961834fb 100644
--- a/drivers/scsi/ips.c
+++ b/drivers/scsi/ips.c
@@ -1428,7 +1428,7 @@ ips_write_info(struct Scsi_Host *host, char *buffer, int length)
if (!ha)
return (-EINVAL);
- return 0;
+ return length;
}
static int
--
2.53.0
[PATCH RFC 12/38] wifi: libipw: don't return 0 from writes to debug_level
Author: Christian Brauner
Date: Mon, 02 Nov 2026 14:21
Date: Mon, 02 Nov 2026 14:21
29 lines
11075 bytes
11075 bytes
debug_level_proc_write() returns the length of the string it parsed. A
write that starts with a NUL byte thus returns 0 and writers that use
stdio retry it forever. Return the number of bytes that were written
instead, which also stops writes longer than the 11 bytes it looks at
from coming up short.
Fixes: b453872c35cf ("[NET] ieee80211 subsystem")
Signed-off-by: Christian Brauner (Amutable) <brauner@kernel.org>
---
drivers/net/wireless/intel/ipw2x00/libipw_module.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/drivers/net/wireless/intel/ipw2x00/libipw_module.c b/drivers/net/wireless/intel/ipw2x00/libipw_module.c
index 22358b2c2ecd..552c8ff3928b 100644
--- a/drivers/net/wireless/intel/ipw2x00/libipw_module.c
+++ b/drivers/net/wireless/intel/ipw2x00/libipw_module.c
@@ -236,7 +236,7 @@ static ssize_t debug_level_proc_write(struct file *file,
else
libipw_debug_level = val;
- return strnlen(buf, len);
+ return count;
}
static const struct proc_ops debug_level_proc_ops = {
--
2.53.0
[PATCH RFC 13/38] scsi: core: NUL-terminate what is written to the host proc files
Author: Christian Brauner
Date: Mon, 02 Nov 2026 14:21
Date: Mon, 02 Nov 2026 14:21
29 lines
11060 bytes
11060 bytes
proc_scsi_host_write() copies what is written into a page and hands it
to ->write_info() without terminating it. ppa and imm parse it with
strncmp() and simple_strtoul(), which read on past what was written into
whatever the page held before. Terminate the copy. A write is at most
PROC_BLOCK_SIZE bytes, so the page always has room for that.
Fixes: 1da177e4c3f4 ("Linux-2.6.12-rc2")
Cc: stable@vger.kernel.org
Signed-off-by: Christian Brauner (Amutable) <brauner@kernel.org>
---
drivers/scsi/scsi_proc.c | 1 +
1 file changed, 1 insertion(+)
diff --git a/drivers/scsi/scsi_proc.c b/drivers/scsi/scsi_proc.c
index fdc355d783da..dbd03360b5a4 100644
--- a/drivers/scsi/scsi_proc.c
+++ b/drivers/scsi/scsi_proc.c
@@ -80,6 +80,7 @@ static ssize_t proc_scsi_host_write(struct file *file, const char __user *buf,
ret = -EFAULT;
if (copy_from_user(page, buf, count))
goto out;
+ page[count] = '\0';
ret = shost->hostt->write_info(shost, page, count);
}
out:
--
2.53.0
[PATCH RFC 15/38] ntfs3: don't warn when a huge label is written
Author: Christian Brauner
Date: Mon, 02 Nov 2026 14:21
Date: Mon, 02 Nov 2026 14:21
40 lines
11244 bytes
11244 bytes
ntfs3_label_write() allocates as many bytes as are written with plain
kmalloc(). A write that is larger than what kmalloc() can hand out then
trips the page allocator's warning, which panics a panic_on_warn kernel.
Use memdup_user(), which fails such an allocation quietly with -ENOMEM
like before.
Fixes: 7832e123490a ("fs/ntfs3: Add support /proc/fs/ntfs3/<dev>/volinfo and /proc/fs/ntfs3/<dev>/label")
Cc: stable@vger.kernel.org
Signed-off-by: Christian Brauner (Amutable) <brauner@kernel.org>
---
fs/ntfs3/super.c | 11 +++--------
1 file changed, 3 insertions(+), 8 deletions(-)
diff --git a/fs/ntfs3/super.c b/fs/ntfs3/super.c
index 58ef6be3286b..b4479cd6bd65 100644
--- a/fs/ntfs3/super.c
+++ b/fs/ntfs3/super.c
@@ -552,15 +552,10 @@ static ssize_t ntfs3_label_write(struct file *file, const char __user *buffer,
if (sb_rdonly(sb))
return -EROFS;
- label = kmalloc(count, GFP_NOFS);
+ label = memdup_user(buffer, count);
+ if (IS_ERR(label))
+ return PTR_ERR(label);
- if (!label)
- return -ENOMEM;
-
- if (copy_from_user(label, buffer, ret)) {
- ret = -EFAULT;
- goto out;
- }
while (ret > 0 && label[ret - 1] == '\n')
ret -= 1;
--
2.53.0
[PATCH RFC 16/38] proc: don't walk through a non-directory when resolving a name
Author: Christian Brauner
Date: Mon, 02 Nov 2026 14:21
Date: Mon, 02 Nov 2026 14:21
62 lines
12450 bytes
12450 bytes
__xlate_proc_name() walks the components of a name like "a/b/c" and
takes whatever it finds for "a" and "b" as the directory to go on in.
It never checks that they actually are directories. So
proc_create("file/x") hangs x into the subdir tree of the regular file
"file". Nobody can look x up there, but remove_proc_subtree() on the
file removes and frees x along with it, behind the back of whoever
registered x.
pktgen passes the name the writer of "add_device" picks to
proc_create_data(), and only the part in front of the '@' has to name
a network device:
echo "add_device lo@" > /proc/net/pktgen/kpktgend_0
echo "add_device lo@/x" > /proc/net/pktgen/kpktgend_1
echo "rem_device_all" > /proc/net/pktgen/kpktgend_0
echo "rem_device_all" > /proc/net/pktgen/kpktgend_1
The third write frees both entries and the fourth one makes kpktgend_1
call proc_remove() on the freed second one:
BUG: KASAN: slab-use-after-free in proc_remove+0x8f/0xa0
Read of size 8 at addr ff110001134c8e70 by task kpktgend_1/243
Call Trace:
proc_remove+0x8f/0xa0
pktgen_remove_device.isra.0+0x118/0x500
pktgen_rem_all_ifs+0xeb/0x1a0
pktgen_thread_worker+0x1b42/0x7280
The files of a network namespace belong to the root of its user
namespace and these writes don't require a capability. So once pktgen
is loaded an unprivileged user can do this in namespaces of its own.
Treat a component that isn't a directory like one that doesn't exist.
All names with a '/' that the tree passes in as literals only walk
through directories. pktgen is fixed separately to not pass such names
in the first place.
Reported-by: Chris Mason <mason@kernel.org>
Fixes: 1da177e4c3f4 ("Linux-2.6.12-rc2")
Cc: stable@vger.kernel.org
Signed-off-by: Christian Brauner (Amutable) <brauner@kernel.org>
---
fs/proc/generic.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/fs/proc/generic.c b/fs/proc/generic.c
index 2b1971da4a85..c560c96a64eb 100644
--- a/fs/proc/generic.c
+++ b/fs/proc/generic.c
@@ -170,7 +170,7 @@ static int __xlate_proc_name(const char *name, struct proc_dir_entry **ret,
de = *ret ?: &proc_root;
while ((next = strchr(cp, '/')) != NULL) {
de = pde_subdir_find(de, cp, next - cp);
- if (!de) {
+ if (!de || !S_ISDIR(de->mode)) {
WARN(1, "name '%s'\n", name);
return -ENOENT;
}
--
2.53.0
[PATCH RFC 17/38] net: pktgen: reject device names with a '/'
Author: Christian Brauner
Date: Mon, 02 Nov 2026 14:21
Date: Mon, 02 Nov 2026 14:21
48 lines
11695 bytes
11695 bytes
"add_device" takes a name like eth0@1. Only the part in front of the
'@' has to name a network device. The rest is free text and all of it
becomes the name of a file in /proc/net/pktgen, where procfs takes a
'/' as a directory separator. So "add_device lo@a/b" makes procfs look
for a directory "lo@a" that doesn't exist and warn about it. Any user
can do that in a network namespace of its own:
name 'lo@a/b'
WARNING: fs/proc/generic.c:174 at __xlate_proc_name+0xb1/0xf0, CPU#3: bash/241
Call Trace:
__proc_create+0xd0/0x9b0
proc_create_reg+0x45/0xf0
proc_create_data+0x77/0xf0
pktgen_thread_write+0xd8d/0x1750
With panic_on_warn that takes the machine down. And if "lo@a" exists
because somebody added a device of that name, procfs used to register
the new entry below that regular file, and removing "lo@a" freed both.
Reject names with a '/'.
Reported-by: Chris Mason <mason@kernel.org>
Fixes: e6fce5b916cd ("pktgen: multiqueue etc.")
Cc: stable@vger.kernel.org
Signed-off-by: Christian Brauner (Amutable) <brauner@kernel.org>
---
net/core/pktgen.c | 4 ++++
1 file changed, 4 insertions(+)
diff --git a/net/core/pktgen.c b/net/core/pktgen.c
index d63eeadddfcc..02af70d360bc 100644
--- a/net/core/pktgen.c
+++ b/net/core/pktgen.c
@@ -3831,6 +3831,10 @@ static int pktgen_add_device(struct pktgen_thread *t, const char *ifname)
int err;
int node = cpu_to_node(t->cpu);
+ /* The name becomes the name of a file in /proc/net/pktgen. */
+ if (strchr(ifname, '/'))
+ return -EINVAL;
+
/* We don't allow a device to be on several threads */
if (__pktgen_NN_threads(t->net, ifname, FIND)) {
--
2.53.0
[PATCH RFC 18/38] proc: don't WARN about a name that is already registered
Author: Christian Brauner
Date: Mon, 02 Nov 2026 14:21
Date: Mon, 02 Nov 2026 14:21
61 lines
12456 bytes
12456 bytes
proc_register() WARNs when the directory already has an entry of the
same name and fails the registration. Callers that pick their names
themselves should never get there but some take names from userspace.
pktgen creates a file for each device a writer of "add_device" adds,
named after the device. A device can have any name, including "pgctrl"
or "kpktgend_0", the names of pktgen's own files:
ip link set lo name pgctrl
ip link set pgctrl up
echo "add_device pgctrl" > /proc/net/pktgen/kpktgend_0
proc_dir_entry 'pktgen/pgctrl' already registered
WARNING: fs/proc/generic.c:404 at proc_register+0x555/0x8b0, CPU#0: bash/241
Call Trace:
proc_create_data+0x9f/0xf0
pktgen_thread_write+0xd9b/0x1780
proc_reg_write+0x1c2/0x3c0
vfs_write+0x1cd/0xfa0
The files of a network namespace belong to the root of its user
namespace and none of this requires a capability. So once pktgen is
loaded an unprivileged user can taint the kernel, or crash it with
panic_on_warn, from namespaces of its own. pktgen itself copes with
the failed registration just fine.
So go back to what proc_register() did before commit 6c2f91e077f1
("proc: use WARN() rather than printk+backtrace") and what
sysfs_warn_dup() does for sysfs: report the name with pr_warn() and
dump the stack. While at it, do that after dropping proc_subdir_lock
instead of printing a backtrace while holding the lock that every
lookup and readdir in procfs needs.
Reported-by: Chris Mason <mason@kernel.org>
Fixes: 6c2f91e077f1 ("proc: use WARN() rather than printk+backtrace")
Cc: stable@vger.kernel.org
Signed-off-by: Christian Brauner (Amutable) <brauner@kernel.org>
---
fs/proc/generic.c | 5 +++--
1 file changed, 3 insertions(+), 2 deletions(-)
diff --git a/fs/proc/generic.c b/fs/proc/generic.c
index c560c96a64eb..ef804cd3ec45 100644
--- a/fs/proc/generic.c
+++ b/fs/proc/generic.c
@@ -401,9 +401,10 @@ struct proc_dir_entry *proc_register(struct proc_dir_entry *dir,
write_lock(&proc_subdir_lock);
dp->parent = dir;
if (pde_subdir_insert(dir, dp) == false) {
- WARN(1, "proc_dir_entry '%s/%s' already registered\n",
- dir->name, dp->name);
write_unlock(&proc_subdir_lock);
+ pr_warn("proc_dir_entry '%s/%s' already registered\n",
+ dir->name, dp->name);
+ dump_stack();
goto out_free_inum;
}
write_unlock(&proc_subdir_lock);
--
2.53.0
[PATCH RFC 19/38] proc: resume readdir after the last name it returned
Author: Christian Brauner
Date: Mon, 02 Nov 2026 14:21
Date: Mon, 02 Nov 2026 14:21
210 lines
17328 bytes
17328 bytes
proc_readdir_de() counts the entries of a directory and resumes a
listing at the number getdents64() stopped at. The entries are sorted
by name, so one that is added or removed in between shifts everything
behind it by one. Listing /proc/net/dev_snmp6 with a buffer that holds
".", ".." and "lo" and adding a device whose name sorts before "lo"
makes the next call return "lo" a second time. Removing one that sorted
before the position makes the next call skip an entry instead.
Since commit 895b4c0c79b0 ("fs/proc: fix uaf in proc_readdir_de()") an
entry that is removed while it is being emitted also ends the listing
right there. pde_subdir_next() finds nothing after an erased entry, so
proc_readdir_de() reports the end of the directory and the next call
resumes by number, skipping another entry. For /proc itself
proc_root_readdir() takes that as the end of the static entries and
moves on to the pids, so the rest of the static entries is missing.
And if the position is behind the last entry when a call starts
proc_readdir_de() returns 0, so /proc then lists no pids at all.
So remember the name of the last entry a call emitted in the file and
resume at the first entry after it, like kernfs resumes after the node
it stopped at. An entry that was removed while it was emitted is
continued after by its name as well. If the position was moved by a
seek in between the listing counts from the start like before.
Fixes: 1da177e4c3f4 ("Linux-2.6.12-rc2")
Fixes: 895b4c0c79b0 ("fs/proc: fix uaf in proc_readdir_de()")
Cc: stable@vger.kernel.org
Signed-off-by: Christian Brauner (Amutable) <brauner@kernel.org>
---
fs/proc/generic.c | 89 ++++++++++++++++++++++++++++++++++++++++++++++--------
fs/proc/internal.h | 1 +
fs/proc/proc_net.c | 1 +
fs/proc/root.c | 1 +
4 files changed, 80 insertions(+), 12 deletions(-)
diff --git a/fs/proc/generic.c b/fs/proc/generic.c
index ef804cd3ec45..47830d6af255 100644
--- a/fs/proc/generic.c
+++ b/fs/proc/generic.c
@@ -87,6 +87,28 @@ static struct proc_dir_entry *pde_subdir_find(struct proc_dir_entry *dir,
return NULL;
}
+/* The first entry of @dir that sorts after @name. */
+static struct proc_dir_entry *pde_subdir_after(struct proc_dir_entry *dir,
+ const char *name,
+ unsigned int len)
+{
+ struct rb_node *node = dir->subdir.rb_node, *after = NULL;
+
+ while (node) {
+ struct proc_dir_entry *de = rb_entry(node,
+ struct proc_dir_entry,
+ subdir_node);
+
+ if (proc_match(name, de, len) < 0) {
+ after = node;
+ node = node->rb_left;
+ } else {
+ node = node->rb_right;
+ }
+ }
+ return rb_entry_safe(after, struct proc_dir_entry, subdir_node);
+}
+
static bool pde_subdir_insert(struct proc_dir_entry *dir,
struct proc_dir_entry *de)
{
@@ -277,6 +299,40 @@ struct dentry *proc_lookup(struct inode *dir, struct dentry *dentry,
return proc_lookup_de(dir, dentry, PDE(dir));
}
+/*
+ * Where the next readdir resumes. The entries are counted, so one that is
+ * added or removed in front of the position would shift what the next call
+ * returns. The name of the last entry emitted doesn't move.
+ */
+struct proc_dir_cursor {
+ loff_t pos; /* the position behind the entry */
+ unsigned int len;
+ char name[NAME_MAX + 1];
+};
+
+/* If this can't remember the entry the next call counts from the start. */
+static void proc_dir_cursor_set(struct file *file, struct proc_dir_entry *de,
+ loff_t pos)
+{
+ struct proc_dir_cursor *cursor = file->private_data;
+
+ if (!cursor) {
+ cursor = kmalloc(sizeof(*cursor), GFP_KERNEL);
+ if (!cursor)
+ return;
+ file->private_data = cursor;
+ }
+ cursor->pos = pos;
+ cursor->len = de->namelen;
+ memcpy(cursor->name, de->name, de->namelen);
+}
+
+int proc_dir_release(struct inode *inode, struct file *file)
+{
+ kfree(file->private_data);
+ return 0;
+}
+
/*
* This returns non-zero if at EOF, so that the /proc
* root directory can use this and check if it should
@@ -289,23 +345,26 @@ struct dentry *proc_lookup(struct inode *dir, struct dentry *dentry,
int proc_readdir_de(struct file *file, struct dir_context *ctx,
struct proc_dir_entry *de)
{
+ struct proc_dir_cursor *cursor = file->private_data;
+ struct proc_dir_entry *dir = de;
int i;
if (!dir_emit_dots(file, ctx))
return 0;
- i = ctx->pos - 2;
read_lock(&proc_subdir_lock);
- de = pde_subdir_first(de);
- for (;;) {
- if (!de) {
- read_unlock(&proc_subdir_lock);
- return 0;
- }
- if (!i)
- break;
- de = pde_subdir_next(de);
- i--;
+ if (cursor && cursor->pos == ctx->pos) {
+ /* Resume after the last entry emitted, wherever it is now. */
+ de = pde_subdir_after(dir, cursor->name, cursor->len);
+ } else {
+ /* Moved by a seek, count from the start like always. */
+ de = pde_subdir_first(dir);
+ for (i = ctx->pos - 2; de && i; i--)
+ de = pde_subdir_next(de);
+ }
+ if (!de) {
+ read_unlock(&proc_subdir_lock);
+ return 1;
}
do {
@@ -318,8 +377,13 @@ int proc_readdir_de(struct file *file, struct dir_context *ctx,
return 0;
}
ctx->pos++;
+ proc_dir_cursor_set(file, de, ctx->pos);
read_lock(&proc_subdir_lock);
- next = pde_subdir_next(de);
+ /* Removed in the meantime, go on after its name. */
+ if (RB_EMPTY_NODE(&de->subdir_node))
+ next = pde_subdir_after(dir, de->name, de->namelen);
+ else
+ next = pde_subdir_next(de);
pde_put(de);
de = next;
} while (de);
@@ -347,6 +411,7 @@ static const struct file_operations proc_dir_operations = {
.llseek = generic_file_llseek,
.read = generic_read_dir,
.iterate_shared = proc_readdir,
+ .release = proc_dir_release,
};
static int proc_net_d_revalidate(struct inode *dir, const struct qstr *name,
diff --git a/fs/proc/internal.h b/fs/proc/internal.h
index df873df9afc5..04f7ce2b24ed 100644
--- a/fs/proc/internal.h
+++ b/fs/proc/internal.h
@@ -287,6 +287,7 @@ extern struct dentry *proc_lookup(struct inode *, struct dentry *, unsigned int)
struct dentry *proc_lookup_de(struct inode *, struct dentry *, struct proc_dir_entry *);
extern int proc_readdir(struct file *, struct dir_context *);
int proc_readdir_de(struct file *, struct dir_context *, struct proc_dir_entry *);
+int proc_dir_release(struct inode *, struct file *);
static inline void pde_get(struct proc_dir_entry *pde)
{
diff --git a/fs/proc/proc_net.c b/fs/proc/proc_net.c
index b1f5eafb069a..dbcf87a257e1 100644
--- a/fs/proc/proc_net.c
+++ b/fs/proc/proc_net.c
@@ -351,6 +351,7 @@ const struct file_operations proc_net_operations = {
.llseek = generic_file_llseek,
.read = generic_read_dir,
.iterate_shared = proc_tgid_net_readdir,
+ .release = proc_dir_release,
};
static __net_init int proc_net_ns_init(struct net *net)
diff --git a/fs/proc/root.c b/fs/proc/root.c
index 7fbbe92bf73a..dfee1caa26f7 100644
--- a/fs/proc/root.c
+++ b/fs/proc/root.c
@@ -441,6 +441,7 @@ static const struct file_operations proc_root_operations = {
.read = generic_read_dir,
.iterate_shared = proc_root_readdir,
.llseek = generic_file_llseek,
+ .release = proc_dir_release,
};
/*
--
2.53.0
[PATCH RFC 20/38] selftests/proc: check a /proc listing while entries come and go
Author: Christian Brauner
Date: Mon, 02 Nov 2026 14:21
Date: Mon, 02 Nov 2026 14:21
293 lines
18595 bytes
18595 bytes
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/proc-readdir-churn.c | 236 ++++++++++++++++++++++
3 files changed, 238 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/proc-readdir-churn.c b/tools/testing/selftests/proc/proc-readdir-churn.c
new file mode 100644
index 000000000000..223b8a6698ee
--- /dev/null
+++ b/tools/testing/selftests/proc/proc-readdir-churn.c
@@ -0,0 +1,236 @@
+// 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)
+{
+ if (unshare(CLONE_NEWNET))
+ SKIP(return, "unshare(CLONE_NEWNET): %s", strerror(errno));
+ if (access("/dev/net/tun", F_OK))
+ SKIP(return, "/dev/net/tun: %s", strerror(errno));
+ 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
[PATCH RFC 21/38] proc: let proc_remove() remove the entry it was given
Author: Christian Brauner
Date: Mon, 02 Nov 2026 14:21
Date: Mon, 02 Nov 2026 14:21
134 lines
14134 bytes
14134 bytes
proc_remove() looks the entry up by its name in its parent and removes
whatever it finds there:
remove_proc_subtree(de->name, de->parent);
If the entry was removed already, say along with its parent, that is
either nothing or a different entry that got the same name in the
meantime, and de->parent may well be gone. Two removals of the same
entry are a bug in the caller, but going by the name turns it into
removing an entry that belongs to somebody else.
Since commit 895b4c0c79b0 ("fs/proc: fix uaf in proc_readdir_de()")
an entry that was unlinked has an empty rb node. So check for that
under proc_subdir_lock and otherwise remove the entry itself, with the
loop of remove_proc_subtree() moved into a helper both of them use.
Nothing can help a caller that passes an entry that has been freed
already.
Reported-by: Chris Mason <mason@kernel.org>
Fixes: a8ca16ea7b0a ("proc: Supply a function to remove a proc entry by PDE")
Signed-off-by: Christian Brauner (Amutable) <brauner@kernel.org>
---
fs/proc/generic.c | 78 ++++++++++++++++++++++++++++++++++++-------------------
1 file changed, 51 insertions(+), 27 deletions(-)
diff --git a/fs/proc/generic.c b/fs/proc/generic.c
index 47830d6af255..23fc026d7bbb 100644
--- a/fs/proc/generic.c
+++ b/fs/proc/generic.c
@@ -817,33 +817,14 @@ void remove_proc_entry(const char *name, struct proc_dir_entry *parent)
}
EXPORT_SYMBOL(remove_proc_entry);
-int remove_proc_subtree(const char *name, struct proc_dir_entry *parent)
+/*
+ * Remove @root, already erased from its parent, and everything below it.
+ * Called with proc_subdir_lock held for writing, returns with it dropped.
+ */
+static int __remove_proc_subtree(struct proc_dir_entry *root)
{
- struct proc_dir_entry *root = NULL, *de, *next;
- const char *fn = name;
- unsigned int len;
-
- write_lock(&proc_subdir_lock);
- if (__xlate_proc_name(name, &parent, &fn) != 0) {
- write_unlock(&proc_subdir_lock);
- return -ENOENT;
- }
- len = strlen(fn);
+ struct proc_dir_entry *de = root, *next;
- root = pde_subdir_find(parent, fn, len);
- if (!root) {
- write_unlock(&proc_subdir_lock);
- return -ENOENT;
- }
- if (unlikely(pde_is_permanent(root))) {
- write_unlock(&proc_subdir_lock);
- WARN(1, "removing permanent /proc entry '%s/%s'",
- root->parent->name, root->name);
- return -EINVAL;
- }
- pde_erase(root, parent);
-
- de = root;
while (1) {
next = pde_subdir_first(de);
if (next) {
@@ -871,6 +852,34 @@ int remove_proc_subtree(const char *name, struct proc_dir_entry *parent)
pde_put(root);
return 0;
}
+
+int remove_proc_subtree(const char *name, struct proc_dir_entry *parent)
+{
+ struct proc_dir_entry *root = NULL;
+ const char *fn = name;
+ unsigned int len;
+
+ write_lock(&proc_subdir_lock);
+ if (__xlate_proc_name(name, &parent, &fn) != 0) {
+ write_unlock(&proc_subdir_lock);
+ return -ENOENT;
+ }
+ len = strlen(fn);
+
+ root = pde_subdir_find(parent, fn, len);
+ if (!root) {
+ write_unlock(&proc_subdir_lock);
+ return -ENOENT;
+ }
+ if (unlikely(pde_is_permanent(root))) {
+ write_unlock(&proc_subdir_lock);
+ WARN(1, "removing permanent /proc entry '%s/%s'",
+ root->parent->name, root->name);
+ return -EINVAL;
+ }
+ pde_erase(root, parent);
+ return __remove_proc_subtree(root);
+}
EXPORT_SYMBOL(remove_proc_subtree);
void *proc_get_parent_data(const struct inode *inode)
@@ -882,8 +891,23 @@ EXPORT_SYMBOL_GPL(proc_get_parent_data);
void proc_remove(struct proc_dir_entry *de)
{
- if (de)
- remove_proc_subtree(de->name, de->parent);
+ if (!de)
+ return;
+
+ write_lock(&proc_subdir_lock);
+ /* Removed already, maybe along with its parent. */
+ if (RB_EMPTY_NODE(&de->subdir_node)) {
+ write_unlock(&proc_subdir_lock);
+ return;
+ }
+ if (unlikely(pde_is_permanent(de))) {
+ write_unlock(&proc_subdir_lock);
+ WARN(1, "removing permanent /proc entry '%s/%s'",
+ de->parent->name, de->name);
+ return;
+ }
+ pde_erase(de, de->parent);
+ __remove_proc_subtree(de);
}
EXPORT_SYMBOL(proc_remove);
--
2.53.0
[PATCH RFC 22/38] proc: warn about entries left in /proc/net of a dying namespace
Author: Christian Brauner
Date: Mon, 02 Nov 2026 14:21
Date: Mon, 02 Nov 2026 14:21
39 lines
11463 bytes
11463 bytes
proc_net_ns_exit() runs after all the other pernet exits and frees
net->proc_net right after removing "stat" from it. An entry that some
pernet exit forgot to remove is left with a freed ->parent, and so
PDE_NET() hands seq_open_net() and single_open_net() a freed struct net
when the entry is opened through a file descriptor that someone kept.
There is no such entry in the tree that we know of, but nothing would
tell us about one either. So warn about it like remove_proc_entry()
does for a directory that isn't empty.
Reported-by: Chris Mason <mason@kernel.org>
Fixes: e9720acd728a ("[NET]: Make /proc/net a symlink on /proc/self/net (v3)")
Signed-off-by: Christian Brauner (Amutable) <brauner@kernel.org>
---
fs/proc/proc_net.c | 6 ++++++
1 file changed, 6 insertions(+)
diff --git a/fs/proc/proc_net.c b/fs/proc/proc_net.c
index dbcf87a257e1..df5484813d5d 100644
--- a/fs/proc/proc_net.c
+++ b/fs/proc/proc_net.c
@@ -410,7 +410,13 @@ static __net_init int proc_net_ns_init(struct net *net)
static __net_exit void proc_net_ns_exit(struct net *net)
{
+ struct proc_dir_entry *left;
+
remove_proc_entry("stat", net->proc_net);
+ /* Whatever is left still points at net->proc_net and so at net. */
+ left = rb_entry_safe(rb_first(&net->proc_net->subdir),
+ struct proc_dir_entry, subdir_node);
+ WARN(left, "/proc/net: leaking at least '%s'\n", left->name);
pde_free(net->proc_net);
}
--
2.53.0
[PATCH RFC 24/38] proc/vmcore: don't let vmcore_cleanup() run twice at once
Author: Christian Brauner
Date: Mon, 02 Nov 2026 14:21
Date: Mon, 02 Nov 2026 14:21
45 lines
11800 bytes
11800 bytes
vmcore_cleanup() has a single caller, fadump's release_mem_store() on
powerpc. That checks fw_dump.dump_active without a lock and calls
vmcore_cleanup() without one, and kernfs only serializes the writers of
one open file. So two tasks that write to
/sys/kernel/fadump/release_mem through different files run
vmcore_cleanup() at the same time.
The first one removes /proc/vmcore and waits for its readers in
proc_remove(). The second one finds the entry already unlinked, returns
from proc_remove() right away and frees vmcore_list, the ELF headers
and the device dumps while those readers still walk them. Or both of
them free the same lists at once.
Serialize vmcore_cleanup() with a mutex of its own. It can't be
vmcore_mutex, removing /proc/vmcore calls release_vmcore() which takes
it. The second caller then finds nothing left to free.
Reported-by: Chris Mason <mason@kernel.org>
Fixes: b500afff11f6 ("fadump: Invalidate registration and release reserved memory for general use.")
Cc: stable@vger.kernel.org
Signed-off-by: Christian Brauner (Amutable) <brauner@kernel.org>
---
fs/proc/vmcore.c | 5 +++++
1 file changed, 5 insertions(+)
diff --git a/fs/proc/vmcore.c b/fs/proc/vmcore.c
index 1fe70c4ecc54..ccb795b85509 100644
--- a/fs/proc/vmcore.c
+++ b/fs/proc/vmcore.c
@@ -1764,6 +1764,11 @@ fs_initcall(vmcore_init);
/* Cleanup function for vmcore module. */
void vmcore_cleanup(void)
{
+ /* Not vmcore_mutex, removing /proc/vmcore calls release_vmcore(). */
+ static DEFINE_MUTEX(vmcore_cleanup_mutex);
+
+ /* Two writers of fadump's release_mem can get here at once. */
+ guard(mutex)(&vmcore_cleanup_mutex);
if (proc_vmcore) {
proc_remove(proc_vmcore);
proc_vmcore = NULL;
--
2.53.0
[PATCH RFC 25/38] powerpc/crash_dump: check ioremap_cache() in copy_oldmem_page()
Author: Christian Brauner
Date: Mon, 02 Nov 2026 14:21
Date: Mon, 02 Nov 2026 14:21
34 lines
11354 bytes
11354 bytes
copy_oldmem_page() maps a page of the old kernel that isn't in the
linear mapping with ioremap_cache() and copies from it without checking
whether the mapping worked. If it didn't, the copy reads from an offset
into the NULL page. That oopses unless the copy happens to be a user
copy, whose exception table entries turn the fault into a short copy.
Return -ENOMEM like the other architectures do. read_from_oldmem()
turns that into -EFAULT like any other short copy.
Reported-by: Chris Mason <mason@kernel.org>
Fixes: 54c32021eb6f ("[PATCH] powerpc: Add arch-dependent copy_oldmem_page")
Cc: stable@vger.kernel.org
Signed-off-by: Christian Brauner (Amutable) <brauner@kernel.org>
---
arch/powerpc/kernel/crash_dump.c | 2 ++
1 file changed, 2 insertions(+)
diff --git a/arch/powerpc/kernel/crash_dump.c b/arch/powerpc/kernel/crash_dump.c
index 103b6605dd68..1543917bd3e1 100644
--- a/arch/powerpc/kernel/crash_dump.c
+++ b/arch/powerpc/kernel/crash_dump.c
@@ -86,6 +86,8 @@ ssize_t copy_oldmem_page(struct iov_iter *iter, unsigned long pfn,
csize = copy_to_iter(vaddr + offset, csize, iter);
} else {
vaddr = ioremap_cache(paddr, PAGE_SIZE);
+ if (!vaddr)
+ return -ENOMEM;
csize = copy_to_iter(vaddr + offset, csize, iter);
iounmap(vaddr);
}
--
2.53.0
[PATCH RFC 26/38] powerpc/crash_dump: recover from a machine check in copy_oldmem_page()
Author: Christian Brauner
Date: Mon, 02 Nov 2026 14:21
Date: Mon, 02 Nov 2026 14:21
51 lines
12215 bytes
12215 bytes
A capture kernel reads the memory of the crashed kernel, and that memory
may well have uncorrectable errors. On powerpc a machine check in the
kernel is only recovered from if the instruction that took it has an
exception table entry (cf. mce_common_process_ue()). copy_oldmem_page()
copies with copy_to_iter(). For a read() of /proc/vmcore that is a user
copy, all of whose loads have exception table entries, so the machine
check turns into a short copy and read_from_oldmem() returns -EFAULT.
But since commit 4a22fd20379c ("vmcore: convert __read_vmcore to use an
iov_iter") /proc/vmcore can be spliced too, and splice(), sendfile()
and copy_file_range() copy into kernel pages with memcpy(), which has
no exception table entries. A machine check there panics the capture
kernel. The same goes for the reads of the ELF headers and notes that
vmcore does itself.
Copy with _copy_mc_to_iter() instead. PPC64 selects ARCH_HAS_COPY_MC
and there it uses copy_mc_generic() for kernel and user destinations
alike, which has the exception table entries. Everywhere else it is
_copy_to_iter().
Reported-by: Chris Mason <mason@kernel.org>
Fixes: 4a22fd20379c ("vmcore: convert __read_vmcore to use an iov_iter")
Cc: stable@vger.kernel.org
Signed-off-by: Christian Brauner (Amutable) <brauner@kernel.org>
---
arch/powerpc/kernel/crash_dump.c | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/arch/powerpc/kernel/crash_dump.c b/arch/powerpc/kernel/crash_dump.c
index 1543917bd3e1..ca7e019ae3e9 100644
--- a/arch/powerpc/kernel/crash_dump.c
+++ b/arch/powerpc/kernel/crash_dump.c
@@ -83,12 +83,12 @@ ssize_t copy_oldmem_page(struct iov_iter *iter, unsigned long pfn,
if (memblock_is_region_memory(paddr, csize)) {
vaddr = __va(paddr);
- csize = copy_to_iter(vaddr + offset, csize, iter);
+ csize = _copy_mc_to_iter(vaddr + offset, csize, iter);
} else {
vaddr = ioremap_cache(paddr, PAGE_SIZE);
if (!vaddr)
return -ENOMEM;
- csize = copy_to_iter(vaddr + offset, csize, iter);
+ csize = _copy_mc_to_iter(vaddr + offset, csize, iter);
iounmap(vaddr);
}
--
2.53.0
[PATCH RFC 27/38] sunrpc: make queue_wait a global wait queue again
Author: Christian Brauner
Date: Mon, 02 Nov 2026 14:21
Date: Mon, 02 Nov 2026 14:21
118 lines
14429 bytes
14429 bytes
Since commit 552d0e17ea04 ("sunrpc: convert queue_wait from global to
per-cache-detail waitqueue") poll() and epoll queue their waiters for a
channel file on the queue_wait head in struct cache_detail. But the
cache_detail can be freed while its channel file is still open and
waited on.
The procfs channel files of a network namespace are removed when the
namespace goes away. Removing a procfs file waits for the callers that
are in one of its methods right now and then calls ->release on every
file that is still open (cf. close_pdeo()). The waiters that poll() and
epoll left on the head stay queued until their file is really closed.
So:
sunrpc_exit_net()
-> ip_map_cache_destroy()
-> cache_unregister_net()
-> remove_cache_proc_entries()
-> proc_remove() // ->release, the waiters stay queued
-> cache_destroy_net() // kfree(cd)
and when poll() returns remove_wait_queue() writes to the freed
cache_detail:
BUG: KASAN: slab-use-after-free in _raw_spin_lock_irqsave+0x4b/0xa0
Read of size 1 at addr ff110001131bb570 by task sunrpc-poll-uaf/242
Call Trace:
remove_wait_queue+0x22/0x1c0
poll_freewait+0xc2/0x230
do_sys_poll+0x9ba/0xd50
__x64_sys_poll+0x165/0x470
Freed by task 12:
kfree+0x267/0x620
sunrpc_exit_net+0x34/0x80
ops_undo_list+0x271/0x8d0
cleanup_net+0x3f3/0x860
Closing an epoll instance that watches the file gets there through
ep_unregister_pollwait() instead.
The channel files of a network namespace belong to the root of its
user namespace and cache_open() doesn't check for a capability. So an
unprivileged user can open the channel file of a namespace of its own
from the outside, poll it and let the namespace die.
Go back to a single static wait queue that never goes away. A wakeup
for one cache wakes the waiters of all caches again but cache_poll()
reports per cache_detail so that's just a spurious wakeup. procfs
should really own the wait queue of its files like kernfs does but
that's nothing for stable.
Reported-by: Chris Mason <mason@kernel.org>
Fixes: 552d0e17ea04 ("sunrpc: convert queue_wait from global to per-cache-detail waitqueue")
Cc: stable@vger.kernel.org
Signed-off-by: Christian Brauner (Amutable) <brauner@kernel.org>
---
include/linux/sunrpc/cache.h | 1 -
net/sunrpc/cache.c | 12 +++++++++---
2 files changed, 9 insertions(+), 4 deletions(-)
diff --git a/include/linux/sunrpc/cache.h b/include/linux/sunrpc/cache.h
index 2735c332ddb7..bf4fc543e1e5 100644
--- a/include/linux/sunrpc/cache.h
+++ b/include/linux/sunrpc/cache.h
@@ -119,7 +119,6 @@ struct cache_detail {
struct list_head requests;
struct list_head readers;
spinlock_t queue_lock;
- wait_queue_head_t queue_wait;
u64 next_seqno;
atomic_t writers; /* how many time is /channel open */
diff --git a/net/sunrpc/cache.c b/net/sunrpc/cache.c
index 1bc04109d213..1ceaea7e01b9 100644
--- a/net/sunrpc/cache.c
+++ b/net/sunrpc/cache.c
@@ -404,7 +404,6 @@ void sunrpc_init_cache_detail(struct cache_detail *cd)
INIT_LIST_HEAD(&cd->requests);
INIT_LIST_HEAD(&cd->readers);
spin_lock_init(&cd->queue_lock);
- init_waitqueue_head(&cd->queue_wait);
cd->next_seqno = 1;
spin_lock(&cache_list_lock);
cd->nextcheck = 0;
@@ -965,13 +964,20 @@ static ssize_t cache_write(struct file *filp, const char __user *buf,
return ret;
}
+/*
+ * Not in struct cache_detail: poll and epoll waiters stay queued until
+ * their file is closed, but a channel file in procfs can stay open after
+ * the cache_detail of its dying network namespace has been freed.
+ */
+static DECLARE_WAIT_QUEUE_HEAD(queue_wait);
+
static __poll_t cache_poll(struct file *filp, poll_table *wait,
struct cache_detail *cd)
{
__poll_t mask;
struct cache_reader *rp = filp->private_data;
- poll_wait(filp, &cd->queue_wait, wait);
+ poll_wait(filp, &queue_wait, wait);
/* alway allow write */
mask = EPOLLOUT | EPOLLWRNORM;
@@ -1236,7 +1242,7 @@ static int cache_do_upcall(struct cache_detail *detail, struct cache_head *h)
spin_unlock(&detail->queue_lock);
if (ret != -EAGAIN && detail->cache_notify)
detail->cache_notify(detail, h);
- wake_up(&detail->queue_wait);
+ wake_up(&queue_wait);
if (ret == -EAGAIN) {
kfree(buf);
kfree(crq);
--
2.53.0
[PATCH RFC 29/38] powerpc/pseries: bound the lengths in the ofdt property parser
Author: Christian Brauner
Date: Mon, 02 Nov 2026 14:21
Date: Mon, 02 Nov 2026 14:21
105 lines
14083 bytes
14083 bytes
The commands for /proc/powerpc/ofdt that carry properties, "add_node",
"add_property" and "update_property", can make the parser read past the
buffer they were copied into:
(1) do_update_property() computes the end of the buffer after
parse_node() moved buf past the phandle. So the end is off by the
length of the phandle string, which can be padded with zeroes, and
parse_next_property() accepts a value that reaches that far past
the command. new_property() then copies those bytes into the
property, which everyone can read in /proc/device-tree.
(2) parse_next_property() parses the length with simple_strtoul() into
an int and only rejects -1. A length above INT_MAX turns negative,
tmp += *length moves backwards and passes the check against the
end, so *tmp is read up to 2G in front of the buffer.
(3) Updating "slb-size" or "ibm,slb-size" reads an int from the value
whatever its length.
Compute the end before parsing the phandle, reject lengths that don't
fit into an int or into what is left of the buffer, and only take the
slb size from a value that holds one. The file is root-only and
refused under lockdown but the parser still shouldn't read past its
input.
Reported-by: Chris Mason <mason@kernel.org>
Fixes: 1da177e4c3f4 ("Linux-2.6.12-rc2")
Fixes: 610d91511f99 ("[PATCH] powerpc: Add support for changing properties from userspace")
Fixes: 46db2f86a3b2 ("powerpc/pseries: Fix to handle slb resize across migration")
Cc: stable@vger.kernel.org
Signed-off-by: Christian Brauner (Amutable) <brauner@kernel.org>
---
arch/powerpc/platforms/pseries/reconfig.c | 18 ++++++++++--------
1 file changed, 10 insertions(+), 8 deletions(-)
diff --git a/arch/powerpc/platforms/pseries/reconfig.c b/arch/powerpc/platforms/pseries/reconfig.c
index 18e3f1a036e3..b41c5db726c9 100644
--- a/arch/powerpc/platforms/pseries/reconfig.c
+++ b/arch/powerpc/platforms/pseries/reconfig.c
@@ -114,6 +114,7 @@ static void release_prop_list(const struct property *prop)
static char * parse_next_property(char *buf, char *end, char **name, int *length,
unsigned char **value)
{
+ unsigned long len;
char *tmp;
*name = buf;
@@ -133,13 +134,13 @@ static char * parse_next_property(char *buf, char *end, char **name, int *length
}
/* now we're on the length */
- *length = -1;
- *length = simple_strtoul(tmp, &tmp, 10);
- if (*length == -1) {
+ len = simple_strtoul(tmp, &tmp, 10);
+ if (len > INT_MAX) {
printk(KERN_ERR "property parse failed in %s at line %d\n",
__func__, __LINE__);
return NULL;
}
+ *length = len;
if (*tmp != ' ' || ++tmp >= end) {
printk(KERN_ERR "property parse failed in %s at line %d\n",
__func__, __LINE__);
@@ -148,13 +149,13 @@ static char * parse_next_property(char *buf, char *end, char **name, int *length
/* now we're on the value */
*value = tmp;
- tmp += *length;
- if (tmp > end) {
+ if (len > end - tmp) {
printk(KERN_ERR "property parse failed in %s at line %d\n",
__func__, __LINE__);
return NULL;
}
- else if (tmp < end && *tmp != ' ' && *tmp != '\0') {
+ tmp += len;
+ if (tmp < end && *tmp != ' ' && *tmp != '\0') {
printk(KERN_ERR "property parse failed in %s at line %d\n",
__func__, __LINE__);
return NULL;
@@ -320,8 +321,8 @@ static int do_update_property(char *buf, size_t bufsize)
char *name, *end, *next_prop;
int length;
struct property *newprop;
- buf = parse_node(buf, bufsize, &np);
end = buf + bufsize;
+ buf = parse_node(buf, bufsize, &np);
if (!np)
return -ENODEV;
@@ -337,7 +338,8 @@ static int do_update_property(char *buf, size_t bufsize)
if (!newprop)
return -ENOMEM;
- if (!strcmp(name, "slb-size") || !strcmp(name, "ibm,slb-size"))
+ if ((!strcmp(name, "slb-size") || !strcmp(name, "ibm,slb-size")) &&
+ length >= (int)sizeof(int))
slb_set_size(*(int *)value);
return of_update_property(np, newprop);
--
2.53.0
[PATCH RFC 31/38] proc: sysctl: resume readdir after the last name it returned
Author: Christian Brauner
Date: Mon, 02 Nov 2026 14:21
Date: Mon, 02 Nov 2026 14:21
195 lines
16783 bytes
16783 bytes
proc_sys_readdir() counts the entries of a directory and resumes a
listing at the number getdents64() stopped at. The entries are sorted
by name, so one that is registered or unregistered in between shifts
everything behind it by one. Listing /proc/sys/net/ipv4/conf with a
buffer that holds ".", ".." and "all" and registering a device whose
name sorts before "all" makes the next call return "all" a second time.
Unregistering one that sorted before the position makes it skip an
entry instead. So a listing that takes more than one call shows a name
twice, or not at all. Even the 64k buffer of ls needs a second call to
find the end of the directory, so a device that comes up right then
repeats the last entry.
So remember the name of the last entry a call emitted in the file and
resume at the first usable entry after it, like kernfs resumes after
the node it stopped at. The entry itself can't be pinned across calls,
unregistering waits for that. If the position was moved by a seek in
between the listing counts from the start like before, seeking to an
arbitrary position has never been better than that.
That also takes one walk down the tree to resume instead of a walk over
every entry in front of the position with a round trip through
sysctl_lock for each of them.
Fixes: 77b14db502cb ("[PATCH] sysctl: reimplement the sysctl proc support")
Cc: stable@vger.kernel.org
Signed-off-by: Christian Brauner (Amutable) <brauner@kernel.org>
---
fs/proc/proc_sysctl.c | 106 ++++++++++++++++++++++++++++++++++++++++++++++++--
1 file changed, 103 insertions(+), 3 deletions(-)
diff --git a/fs/proc/proc_sysctl.c b/fs/proc/proc_sysctl.c
index 6c0859c66563..38b61887b59c 100644
--- a/fs/proc/proc_sysctl.c
+++ b/fs/proc/proc_sysctl.c
@@ -417,6 +417,54 @@ static void next_entry(struct ctl_table_header **phead, const struct ctl_table *
*pentry = entry;
}
+/*
+ * The first usable entry of @dir after @name, or the first one at all for a
+ * NULL @name. The entries are sorted by name, so that is where a listing
+ * resumes when entries in front of it came or went in between. -ENOENT if
+ * the directory is being unregistered.
+ */
+static int entry_after(struct ctl_dir *dir, const char *name, int namelen,
+ struct ctl_table_header **phead,
+ const struct ctl_table **pentry)
+{
+ struct ctl_table_header *head = NULL;
+ const struct ctl_table *entry = NULL;
+ struct rb_node *node, *after = NULL;
+ struct ctl_node *ctl_node;
+
+ spin_lock(&sysctl_lock);
+ if (unlikely(dir->header.unregistering)) {
+ spin_unlock(&sysctl_lock);
+ return -ENOENT;
+ }
+ if (!name) {
+ after = rb_first(&dir->root);
+ } else for (node = dir->root.rb_node; node;) {
+ const struct ctl_table *table;
+ struct ctl_table_header *h;
+
+ ctl_node = rb_entry(node, struct ctl_node, node);
+ h = ctl_node->header;
+ table = &h->ctl_table[ctl_node - h->node];
+ if (namecmp(table->procname, strlen(table->procname),
+ name, namelen) > 0) {
+ after = node;
+ node = node->rb_left;
+ } else {
+ node = node->rb_right;
+ }
+ }
+ ctl_node = first_usable_entry(after);
+ spin_unlock(&sysctl_lock);
+ if (ctl_node) {
+ head = ctl_node->header;
+ entry = &head->ctl_table[ctl_node - head->node];
+ }
+ *phead = head;
+ *pentry = entry;
+ return 0;
+}
+
/*
* sysctl_perm does NOT grant the superuser all rights automatically, because
* some sysctl variables are readonly even to root.
@@ -675,6 +723,36 @@ static __poll_t proc_sys_poll(struct file *filp, poll_table *wait)
return ret;
}
+/*
+ * Where the next readdir resumes. The entries are counted, so one that is
+ * registered or unregistered in front of the position would shift what
+ * the next call returns. The name of the last entry emitted doesn't move.
+ */
+struct sysctl_cursor {
+ loff_t pos; /* the position behind the entry */
+ int len;
+ char name[NAME_MAX + 1];
+};
+
+/* If this can't remember the entry the next call counts from the start. */
+static void sysctl_cursor_set(struct file *file, const char *name, int len,
+ loff_t pos)
+{
+ struct sysctl_cursor *cursor = file->private_data;
+
+ if (len > NAME_MAX)
+ return;
+ if (!cursor) {
+ cursor = kmalloc(sizeof(*cursor), GFP_KERNEL);
+ if (!cursor)
+ return;
+ file->private_data = cursor;
+ }
+ cursor->pos = pos;
+ cursor->len = len;
+ memcpy(cursor->name, name, len);
+}
+
static bool proc_sys_fill_cache(struct file *file,
struct dir_context *ctx,
struct ctl_table_header *head,
@@ -753,8 +831,11 @@ static int scan(struct ctl_table_header *head, const struct ctl_table *table,
else
res = proc_sys_fill_cache(file, ctx, head, table);
- if (res)
+ if (res) {
ctx->pos = *pos;
+ sysctl_cursor_set(file, table->procname, strlen(table->procname),
+ ctx->pos);
+ }
return res;
}
@@ -762,6 +843,7 @@ static int scan(struct ctl_table_header *head, const struct ctl_table *table,
static int proc_sys_readdir(struct file *file, struct dir_context *ctx)
{
struct ctl_table_header *head = grab_header(file_inode(file));
+ struct sysctl_cursor *cursor = file->private_data;
struct ctl_table_header *h = NULL;
const struct ctl_table *entry;
struct ctl_dir *ctl_dir;
@@ -775,9 +857,20 @@ static int proc_sys_readdir(struct file *file, struct dir_context *ctx)
if (!dir_emit_dots(file, ctx))
goto out;
- pos = 2;
+ /*
+ * Resume after the last entry the previous call emitted. Only a seek
+ * that moved the position away from it counts from the start, like
+ * seeking to an arbitrary position always did.
+ */
+ if (cursor && cursor->pos == ctx->pos) {
+ entry_after(ctl_dir, cursor->name, cursor->len, &h, &entry);
+ pos = ctx->pos;
+ } else {
+ first_entry(ctl_dir, &h, &entry);
+ pos = 2;
+ }
- for (first_entry(ctl_dir, &h, &entry); h; next_entry(&h, &entry)) {
+ for (; h; next_entry(&h, &entry)) {
if (!scan(h, entry, &pos, file, ctx)) {
sysctl_head_finish(h);
break;
@@ -788,6 +881,12 @@ static int proc_sys_readdir(struct file *file, struct dir_context *ctx)
return 0;
}
+static int proc_sys_dir_release(struct inode *inode, struct file *file)
+{
+ kfree(file->private_data);
+ return 0;
+}
+
static int proc_sys_permission(const struct mnt_idmap *idmap,
struct inode *inode, int mask)
{
@@ -867,6 +966,7 @@ static const struct file_operations proc_sys_dir_file_operations = {
.read = generic_read_dir,
.iterate_shared = proc_sys_readdir,
.llseek = generic_file_llseek,
+ .release = proc_sys_dir_release,
};
static const struct inode_operations proc_sys_inode_operations = {
--
2.53.0
[PATCH RFC 32/38] selftests/proc: check a sysctl listing while entries come and go
Author: Christian Brauner
Date: Mon, 02 Nov 2026 14:21
Date: Mon, 02 Nov 2026 14:21
197 lines
15820 bytes
15820 bytes
List /proc/sys/net/ipv4/conf of a new network namespace in two pieces:
- with a buffer that holds the dots and "all", register a tun device
whose name sorts first, finish the listing: "all" is listed once
- with that device registered and a buffer that holds the dots and it,
unregister it, finish the listing: "all" is still listed
Both fail without the fix, the first lists "all" twice, the second not
at all.
Signed-off-by: Christian Brauner (Amutable) <brauner@kernel.org>
---
tools/testing/selftests/proc/.gitignore | 1 +
tools/testing/selftests/proc/Makefile | 1 +
.../selftests/proc/proc-sysctl-readdir-churn.c | 147 +++++++++++++++++++++
3 files changed, 149 insertions(+)
diff --git a/tools/testing/selftests/proc/.gitignore b/tools/testing/selftests/proc/.gitignore
index 8520c7878ea2..5b5e24e39cbd 100644
--- a/tools/testing/selftests/proc/.gitignore
+++ b/tools/testing/selftests/proc/.gitignore
@@ -25,4 +25,5 @@
/self
/setns-dcache
/setns-sysvipc
+/proc-sysctl-readdir-churn
/thread-self
diff --git a/tools/testing/selftests/proc/Makefile b/tools/testing/selftests/proc/Makefile
index e0c0d313f4d0..dae09f333455 100644
--- a/tools/testing/selftests/proc/Makefile
+++ b/tools/testing/selftests/proc/Makefile
@@ -30,6 +30,7 @@ TEST_GEN_PROGS += setns-sysvipc
TEST_GEN_PROGS += thread-self
TEST_GEN_PROGS += proc-multiple-procfs
TEST_GEN_PROGS += proc-fsconfig-hidepid
+TEST_GEN_PROGS += proc-sysctl-readdir-churn
TEST_GEN_PROGS += proc-pidns
include ../lib.mk
diff --git a/tools/testing/selftests/proc/proc-sysctl-readdir-churn.c b/tools/testing/selftests/proc/proc-sysctl-readdir-churn.c
new file mode 100644
index 000000000000..0e067a3619c4
--- /dev/null
+++ b/tools/testing/selftests/proc/proc-sysctl-readdir-churn.c
@@ -0,0 +1,147 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * A listing of a /proc/sys directory that takes more than one getdents64()
+ * must not show an entry twice or skip one when an entry is registered or
+ * unregistered in between. /proc/sys/net/ipv4/conf/<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 <sched.h>
+#include <string.h>
+#include <sys/ioctl.h>
+#include <sys/syscall.h>
+#include <unistd.h>
+
+#include "../kselftest_harness.h"
+
+#define CONF_DIR "/proc/sys/net/ipv4/conf"
+#define TUN_NAME "a0" /* sorts before "all" */
+#define TUN_PATH CONF_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;
+}
+
+FIXTURE(sysctl_readdir) {
+ int dir;
+ int n;
+ char names[NAMES][NAME_MAX + 1];
+ char buf[65536];
+};
+
+FIXTURE_SETUP(sysctl_readdir)
+{
+ if (unshare(CLONE_NEWNET))
+ SKIP(return, "unshare(CLONE_NEWNET): %s", strerror(errno));
+ if (access("/dev/net/tun", F_OK))
+ SKIP(return, "/dev/net/tun: %s", strerror(errno));
+ self->dir = open(CONF_DIR, O_RDONLY | O_DIRECTORY | O_CLOEXEC);
+ ASSERT_GE(self->dir, 0);
+ self->n = 0;
+}
+
+FIXTURE_TEARDOWN(sysctl_readdir)
+{
+ close(self->dir);
+}
+
+/* a device registered in front of the position doesn't repeat an entry */
+TEST_F(sysctl_readdir, register_in_front)
+{
+ int tun;
+
+ /* ".", ".." and "all", "default" doesn't fit */
+ ASSERT_EQ(list(self->dir, self->buf, SMALL_BUF, self->names, &self->n), 3);
+ ASSERT_EQ(count(self->names, self->n, "all"), 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, "all"), 1);
+ EXPECT_EQ(count(self->names, self->n, "default"), 1);
+ EXPECT_EQ(count(self->names, self->n, "lo"), 1);
+ close(tun);
+}
+
+/* a device unregistered in front of the position doesn't skip an entry */
+TEST_F(sysctl_readdir, unregister_in_front)
+{
+ int tun;
+
+ tun = tun_open(TUN_NAME);
+ ASSERT_GE(tun, 0);
+ ASSERT_EQ(access(TUN_PATH, F_OK), 0);
+
+ /* ".", ".." and the device, "all" 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 its sysctls under RTNL */
+ 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, "all"), 1);
+ EXPECT_EQ(count(self->names, self->n, "default"), 1);
+ EXPECT_EQ(count(self->names, self->n, "lo"), 1);
+}
+
+TEST_HARNESS_MAIN
--
2.53.0
[PATCH RFC 33/38] proc: sysctl: copy to and from userspace without holding the table
Author: Christian Brauner
Date: Mon, 02 Nov 2026 14:21
Date: Mon, 02 Nov 2026 14:21
115 lines
13731 bytes
13731 bytes
proc_sys_call_handler() grabs the table, copies in what is written,
calls the handler and copies what it produced out to userspace before
it lets go of the table again. Unregistering a table waits until nobody
uses it anymore. So a reader or writer whose copy faults for as long as
it wants, with userfaultfd or a stalled FUSE mapping, keeps
unregister_sysctl_table() waiting, and its callers like to hold locks
while they do that. addrconf unregisters the ipv6 sysctls of a device
with RTNL held, so a stalled read of /proc/sys/net/ipv6/conf/<dev>/mtu
holds up every RTNL user until the reader gives up.
Copy in before grabbing the table and copy out after letting go of it,
like procfs and kernfs do for their files. The buffer is ours, nothing
needs the table for the copies.
That moves the allocation and the copy in before the permission check.
A write that can't be copied in, or a request that is too large, now
fails with -EFAULT or -ENOMEM where it used to fail with -EPERM.
Fixes: 330d57fb98a9 ("[PATCH] Fix sysctl unregistration oops (CVE-2005-2709)")
Cc: stable@vger.kernel.org
Signed-off-by: Christian Brauner (Amutable) <brauner@kernel.org>
---
fs/proc/proc_sysctl.c | 50 ++++++++++++++++++++++++++++----------------------
1 file changed, 28 insertions(+), 22 deletions(-)
diff --git a/fs/proc/proc_sysctl.c b/fs/proc/proc_sysctl.c
index 38b61887b59c..2150cf842919 100644
--- a/fs/proc/proc_sysctl.c
+++ b/fs/proc/proc_sysctl.c
@@ -602,14 +602,36 @@ static ssize_t proc_sys_call_handler(struct kiocb *iocb, struct iov_iter *iter,
int write)
{
struct inode *inode = file_inode(iocb->ki_filp);
- struct ctl_table_header *head = grab_header(inode);
const struct ctl_table *table = PROC_I(inode)->sysctl_entry;
+ struct ctl_table_header *head;
size_t count = iov_iter_count(iter);
char *kbuf;
ssize_t error;
- if (IS_ERR(head))
- return PTR_ERR(head);
+ /* don't even try if the size is too large */
+ if (count >= KMALLOC_MAX_SIZE)
+ return -ENOMEM;
+ kbuf = kvzalloc(count + 1, GFP_KERNEL);
+ if (!kbuf)
+ return -ENOMEM;
+
+ /*
+ * Copy in before grabbing the table and copy out after letting go of
+ * it. Unregistering the table waits for us and a copy that faults can
+ * take as long as userspace wants.
+ */
+ if (write) {
+ error = -EFAULT;
+ if (!copy_from_iter_full(kbuf, count, iter))
+ goto out_free_buf;
+ kbuf[count] = '\0';
+ }
+
+ head = grab_header(inode);
+ if (IS_ERR(head)) {
+ error = PTR_ERR(head);
+ goto out_free_buf;
+ }
/*
* At this point we know that the sysctl was not unregistered
@@ -624,28 +646,15 @@ static ssize_t proc_sys_call_handler(struct kiocb *iocb, struct iov_iter *iter,
if (!table->proc_handler)
goto out;
- /* don't even try if the size is too large */
- error = -ENOMEM;
- if (count >= KMALLOC_MAX_SIZE)
- goto out;
- kbuf = kvzalloc(count + 1, GFP_KERNEL);
- if (!kbuf)
- goto out;
-
- if (write) {
- error = -EFAULT;
- if (!copy_from_iter_full(kbuf, count, iter))
- goto out_free_buf;
- kbuf[count] = '\0';
- }
-
error = BPF_CGROUP_RUN_PROG_SYSCTL(head, table, write, &kbuf, &count,
&iocb->ki_pos);
if (error)
- goto out_free_buf;
+ goto out;
/* careful: calling conventions are nasty here */
error = table->proc_handler(table, write, kbuf, &count, &iocb->ki_pos);
+out:
+ sysctl_head_finish(head);
if (error)
goto out_free_buf;
@@ -658,9 +667,6 @@ static ssize_t proc_sys_call_handler(struct kiocb *iocb, struct iov_iter *iter,
error = count;
out_free_buf:
kvfree(kbuf);
-out:
- sysctl_head_finish(head);
-
return error;
}
--
2.53.0
[PATCH RFC 34/38] proc: sysctl: look at the table under sysctl_lock when creating an inode
Author: Christian Brauner
Date: Mon, 02 Nov 2026 14:21
Date: Mon, 02 Nov 2026 14:21
80 lines
12816 bytes
12816 bytes
proc_sys_make_inode() checks under sysctl_lock that @head isn't being
unregistered, lets go of the lock, and then reads the mode of @table
and calls ->set_ownership() with @head. That's fine as long as the
caller holds @head in use, which keeps unregistering from getting past
that check. The next patch has a caller that only pins the memory of
@head. For that one the table, and whatever the owner of the header
keeps behind it, is only there for as long as nobody unregisters the
header, which happens under sysctl_lock. So read the mode and set the
ownership before letting go of the lock. The ->set_ownership()
callbacks only look up ids in a user namespace.
No functional changes.
Signed-off-by: Christian Brauner (Amutable) <brauner@kernel.org>
---
fs/proc/proc_sysctl.c | 20 +++++++++++++-------
1 file changed, 13 insertions(+), 7 deletions(-)
diff --git a/fs/proc/proc_sysctl.c b/fs/proc/proc_sysctl.c
index 2150cf842919..a1242247f492 100644
--- a/fs/proc/proc_sysctl.c
+++ b/fs/proc/proc_sysctl.c
@@ -500,6 +500,7 @@ static struct inode *proc_sys_make_inode(struct super_block *sb,
struct ctl_table_root *root = head->root;
struct inode *inode;
struct proc_inode *ei;
+ umode_t mode;
inode = new_inode(sb);
if (!inode)
@@ -509,12 +510,22 @@ static struct inode *proc_sys_make_inode(struct super_block *sb,
ei = PROC_I(inode);
+ inode->i_uid = GLOBAL_ROOT_UID;
+ inode->i_gid = GLOBAL_ROOT_GID;
spin_lock(&sysctl_lock);
if (unlikely(head->unregistering)) {
spin_unlock(&sysctl_lock);
iput(inode);
return ERR_PTR(-ENOENT);
}
+ /*
+ * The table, and whatever the owner of the header keeps behind it,
+ * is only there for as long as nobody unregisters the header, which
+ * happens under sysctl_lock. So look at them before letting go of it.
+ */
+ mode = table->mode;
+ if (root->set_ownership)
+ root->set_ownership(head, &inode->i_uid, &inode->i_gid);
ei->sysctl = head;
ei->sysctl_entry = table;
hlist_add_head_rcu(&ei->sibling_inodes, &head->inodes);
@@ -522,8 +533,8 @@ static struct inode *proc_sys_make_inode(struct super_block *sb,
spin_unlock(&sysctl_lock);
simple_inode_init_ts(inode);
- inode->i_mode = table->mode;
- if (!S_ISDIR(table->mode)) {
+ inode->i_mode = mode;
+ if (!S_ISDIR(mode)) {
inode->i_mode |= S_IFREG;
inode->i_op = &proc_sys_inode_operations;
inode->i_fop = &proc_sys_file_operations;
@@ -535,11 +546,6 @@ static struct inode *proc_sys_make_inode(struct super_block *sb,
make_empty_dir_inode(inode);
}
- inode->i_uid = GLOBAL_ROOT_UID;
- inode->i_gid = GLOBAL_ROOT_GID;
- if (root->set_ownership)
- root->set_ownership(head, &inode->i_uid, &inode->i_gid);
-
return inode;
}
--
2.53.0
[PATCH RFC 35/38] proc: sysctl: don't hold the tables while readdir copies to userspace
Author: Christian Brauner
Date: Mon, 02 Nov 2026 14:21
Date: Mon, 02 Nov 2026 14:21
458 lines
25399 bytes
25399 bytes
proc_sys_readdir() holds the directory's table for the whole readdir
and the table of each entry while dir_emit() copies its name to
userspace. Unregistering a table waits until nobody uses it anymore. So
a getdents64() whose buffer faults for as long as it wants keeps
unregister_sysctl_table() waiting, just like a stalled read() does.
Listing /proc/sys/net/ipv6/conf/<dev>/ that way holds up the removal of
the device under RTNL.
So don't take any table into use for a listing. A header counts what
refers to its memory, the inodes made for it, and unregistering doesn't
wait for those. readdir takes one of those references on the entry it
is at instead, which keeps the header around and nothing else. Under
sysctl_lock, where unregistering erases an entry, it finds the entry,
copies its name and mode, follows a link to the entry it stands for and
pins that. Then it looks the dentry up, creates the inode if there is
none, which checks for itself under sysctl_lock that the table is still
registered, and emits the entry with nothing held at all. The next
entry is the first live one after that name, which is where a listing
resumes between calls since "proc: sysctl: resume readdir after the
last name it returned" anyway. kernfs_fop_readdir() and
proc_readdir_de() work the same way, a reference on the entry and the
lock only to find the next one.
While the entry is still registered its node is still in the tree and
the next one is right behind it, like kernfs_dir_next_pos() continues
from the node it holds. Only once it went away, and to resume, is the
next entry found by name. That's one trip through sysctl_lock per entry
like before, and the link entries of /proc/sys/net no longer take two
more. The directory isn't
taken into use either, an entry keeps its directory registered while
it is used and readdir looks at the directory under the lock. So the
dots don't wait for anything, a directory that went away lists them and
then fails with -ENOENT. A seek to an arbitrary position counts the
entries from the start like before, one trip per entry.
Fixes: 77b14db502cb ("[PATCH] sysctl: reimplement the sysctl proc support")
Cc: stable@vger.kernel.org
Signed-off-by: Christian Brauner (Amutable) <brauner@kernel.org>
---
fs/proc/proc_sysctl.c | 307 +++++++++++++++++++++++++++-----------------------
1 file changed, 166 insertions(+), 141 deletions(-)
diff --git a/fs/proc/proc_sysctl.c b/fs/proc/proc_sysctl.c
index a1242247f492..0b7a1e487ed6 100644
--- a/fs/proc/proc_sysctl.c
+++ b/fs/proc/proc_sysctl.c
@@ -91,6 +91,8 @@ static void drop_sysctl_table(struct ctl_table_header *header);
static int sysctl_follow_link(struct ctl_table_header **phead,
const struct ctl_table **pentry);
static int insert_links(struct ctl_table_header *head);
+static struct ctl_table_set *lookup_header_set(struct ctl_table_root *root);
+static struct ctl_dir *xlate_dir(struct ctl_table_set *set, struct ctl_dir *dir);
static void put_links(struct ctl_table_header *header);
static void sysctl_print_dir(struct ctl_dir *dir)
@@ -367,86 +369,97 @@ static const struct ctl_table *lookup_entry(struct ctl_table_header **phead,
return entry;
}
-static struct ctl_node *first_usable_entry(struct rb_node *node)
-{
- struct ctl_node *ctl_node;
+/*
+ * What readdir takes along for an entry between the trips through
+ * sysctl_lock: the header pinned, which keeps its memory and nothing else,
+ * the entry, its name and mode. For a link the header and entry are the
+ * ones it stands for in the caller's set.
+ */
+struct sysctl_dirent {
+ struct ctl_table_header *head;
+ const struct ctl_table *table;
+ struct ctl_node *node; /* the entry's node in the directory, NULL for a link */
+ ino_t ino;
+ unsigned int type;
+ umode_t mode;
+ bool skip;
+ int len;
+ char name[NAME_MAX + 1];
+};
- for (;node; node = rb_next(node)) {
- ctl_node = rb_entry(node, struct ctl_node, node);
- if (use_table(ctl_node->header))
- return ctl_node;
- }
- return NULL;
+/* A reference on the memory of @head, like an inode made for it holds. */
+static void sysctl_pin(struct ctl_table_header *head)
+{
+ lockdep_assert_held(&sysctl_lock);
+ head->count++;
}
-static void first_entry(struct ctl_dir *dir,
- struct ctl_table_header **phead, const struct ctl_table **pentry)
+static void sysctl_unpin(struct ctl_table_header *head)
{
- struct ctl_table_header *head = NULL;
- const struct ctl_table *entry = NULL;
- struct ctl_node *ctl_node;
-
- spin_lock(&sysctl_lock);
- ctl_node = first_usable_entry(rb_first(&dir->root));
- spin_unlock(&sysctl_lock);
- if (ctl_node) {
- head = ctl_node->header;
- entry = &head->ctl_table[ctl_node - head->node];
- }
- *phead = head;
- *pentry = entry;
+ lockdep_assert_held(&sysctl_lock);
+ if (!--head->count)
+ kfree_rcu(head, rcu);
}
-static void next_entry(struct ctl_table_header **phead, const struct ctl_table **pentry)
+/* the first entry from @node on whose table isn't being unregistered */
+static struct ctl_node *first_live_entry(struct rb_node *node)
{
- struct ctl_table_header *head = *phead;
- const struct ctl_table *entry = *pentry;
- struct ctl_node *ctl_node = &head->node[entry - head->ctl_table];
-
- spin_lock(&sysctl_lock);
- unuse_table(head);
+ struct ctl_node *ctl_node;
- ctl_node = first_usable_entry(rb_next(&ctl_node->node));
- spin_unlock(&sysctl_lock);
- head = NULL;
- if (ctl_node) {
- head = ctl_node->header;
- entry = &head->ctl_table[ctl_node - head->node];
+ lockdep_assert_held(&sysctl_lock);
+ for (; node; node = rb_next(node)) {
+ ctl_node = rb_entry(node, struct ctl_node, node);
+ if (!ctl_node->header->unregistering)
+ return ctl_node;
}
- *phead = head;
- *pentry = entry;
+ return NULL;
}
/*
- * The first usable entry of @dir after @name, or the first one at all for a
- * NULL @name. The entries are sorted by name, so that is where a listing
- * resumes when entries in front of it came or went in between. -ENOENT if
- * the directory is being unregistered.
+ * Fill @d with the entry of @dir behind the one it holds, letting go of that
+ * one, or with the first live entry after @name, or the first one at all for
+ * a NULL @name. While the entry @d holds is still registered its node is
+ * still in the tree and the next one is right behind it. Once it is gone the
+ * entries are found by name, they are sorted by it, so that is also where a
+ * listing resumes when entries in front of it came or went in between.
+ * 1 with the next entry pinned in @d, 0 at the end of the directory and
+ * -ENOENT once @dir itself is being unregistered.
+ *
+ * Everything that reads the tables happens under sysctl_lock, where an entry
+ * is erased when it is unregistered, since nothing holds the tables in use.
*/
-static int entry_after(struct ctl_dir *dir, const char *name, int namelen,
- struct ctl_table_header **phead,
- const struct ctl_table **pentry)
+static int sysctl_dir_next(struct ctl_dir *dir, struct sysctl_dirent *d,
+ const char *name, int namelen)
{
- struct ctl_table_header *head = NULL;
- const struct ctl_table *entry = NULL;
struct rb_node *node, *after = NULL;
+ struct ctl_table_header *head;
+ const struct ctl_table *entry;
struct ctl_node *ctl_node;
+ bool by_name = true;
+ int ret = 0;
spin_lock(&sysctl_lock);
+ if (d->head) {
+ if (d->node && !d->head->unregistering) {
+ after = rb_next(&d->node->node);
+ by_name = false;
+ }
+ sysctl_unpin(d->head);
+ d->head = NULL;
+ }
if (unlikely(dir->header.unregistering)) {
- spin_unlock(&sysctl_lock);
- return -ENOENT;
+ ret = -ENOENT;
+ goto out;
}
- if (!name) {
+ if (!by_name) {
+ /* behind the entry @d held */
+ } else if (!name) {
after = rb_first(&dir->root);
} else for (node = dir->root.rb_node; node;) {
- const struct ctl_table *table;
- struct ctl_table_header *h;
-
ctl_node = rb_entry(node, struct ctl_node, node);
- h = ctl_node->header;
- table = &h->ctl_table[ctl_node - h->node];
- if (namecmp(table->procname, strlen(table->procname),
+ head = ctl_node->header;
+ entry = &head->ctl_table[ctl_node - head->node];
+ if (namecmp(entry->procname, strlen(entry->procname),
name, namelen) > 0) {
after = node;
node = node->rb_left;
@@ -454,15 +467,56 @@ static int entry_after(struct ctl_dir *dir, const char *name, int namelen,
node = node->rb_right;
}
}
- ctl_node = first_usable_entry(after);
- spin_unlock(&sysctl_lock);
- if (ctl_node) {
- head = ctl_node->header;
- entry = &head->ctl_table[ctl_node - head->node];
+ ctl_node = first_live_entry(after);
+ if (!ctl_node)
+ goto out;
+ head = ctl_node->header;
+ entry = &head->ctl_table[ctl_node - head->node];
+
+ d->len = strlen(entry->procname);
+ if (d->len > NAME_MAX) {
+ ret = -ENAMETOOLONG;
+ goto out;
}
- *phead = head;
- *pentry = entry;
- return 0;
+ memcpy(d->name, entry->procname, d->len);
+ d->mode = entry->mode;
+ d->skip = false;
+ d->node = ctl_node;
+ if (unlikely(S_ISLNK(entry->mode))) {
+ /* the entry it stands for in the caller's set, or nothing */
+ struct ctl_table_root *root = entry->data;
+ struct ctl_table_set *set = lookup_header_set(root);
+ struct ctl_dir *xdir = xlate_dir(set, head->parent);
+ struct ctl_table_header *xhead = NULL;
+ const struct ctl_table *xentry = NULL;
+
+ if (!IS_ERR(xdir))
+ xentry = find_entry(&xhead, xdir, d->name, d->len);
+ if (!xentry || xhead->unregistering) {
+ d->skip = true;
+ } else {
+ head = xhead;
+ entry = xentry;
+ }
+ d->node = NULL;
+ }
+ sysctl_pin(head);
+ d->head = head;
+ d->table = entry;
+ ret = 1;
+out:
+ spin_unlock(&sysctl_lock);
+ return ret;
+}
+
+static void sysctl_dir_done(struct sysctl_dirent *d)
+{
+ if (!d->head)
+ return;
+ spin_lock(&sysctl_lock);
+ sysctl_unpin(d->head);
+ spin_unlock(&sysctl_lock);
+ d->head = NULL;
}
/*
@@ -765,29 +819,29 @@ static void sysctl_cursor_set(struct file *file, const char *name, int len,
memcpy(cursor->name, name, len);
}
-static bool proc_sys_fill_cache(struct file *file,
- struct dir_context *ctx,
- struct ctl_table_header *head,
- const struct ctl_table *table)
+/*
+ * The inode number and type of the entry @d holds, instantiating its dentry
+ * if there is none. Needs the header pinned, proc_sys_make_inode() checks
+ * for itself that the table is still registered.
+ */
+static int proc_sys_fill_dirent(struct file *file, struct sysctl_dirent *d)
{
struct dentry *child, *dir = file->f_path.dentry;
struct inode *inode;
struct qstr qname;
- ino_t ino = 0;
- unsigned type = DT_UNKNOWN;
- qname.name = table->procname;
- qname.len = strlen(table->procname);
+ qname.name = d->name;
+ qname.len = d->len;
qname.hash = full_name_hash(dir, qname.name, qname.len);
child = d_lookup(dir, &qname);
if (!child) {
child = d_alloc_parallel(dir, &qname);
if (IS_ERR(child))
- return false;
+ return PTR_ERR(child);
if (d_in_lookup(child)) {
struct dentry *res;
- inode = proc_sys_make_inode(dir->d_sb, head, table);
+ inode = proc_sys_make_inode(dir->d_sb, d->head, d->table);
res = d_splice_alias_ops(inode, child,
&proc_sys_dentry_operations);
d_lookup_done(child);
@@ -795,79 +849,41 @@ static bool proc_sys_fill_cache(struct file *file,
dput(child);
if (IS_ERR(res))
- return false;
+ return PTR_ERR(res);
child = res;
}
}
}
inode = d_inode(child);
- ino = inode->i_ino;
- type = inode->i_mode >> 12;
+ d->ino = inode->i_ino;
+ d->type = inode->i_mode >> 12;
dput(child);
- return dir_emit(ctx, qname.name, qname.len, ino, type);
-}
-
-static bool proc_sys_link_fill_cache(struct file *file,
- struct dir_context *ctx,
- struct ctl_table_header *head,
- const struct ctl_table *table)
-{
- bool ret = true;
-
- head = sysctl_head_grab(head);
- if (IS_ERR(head))
- return false;
-
- /* It is not an error if we can not follow the link ignore it */
- if (sysctl_follow_link(&head, &table))
- goto out;
-
- ret = proc_sys_fill_cache(file, ctx, head, table);
-out:
- sysctl_head_finish(head);
- return ret;
-}
-
-static int scan(struct ctl_table_header *head, const struct ctl_table *table,
- unsigned long *pos, struct file *file,
- struct dir_context *ctx)
-{
- bool res;
-
- if ((*pos)++ < ctx->pos)
- return true;
-
- if (unlikely(S_ISLNK(table->mode)))
- res = proc_sys_link_fill_cache(file, ctx, head, table);
- else
- res = proc_sys_fill_cache(file, ctx, head, table);
-
- if (res) {
- ctx->pos = *pos;
- sysctl_cursor_set(file, table->procname, strlen(table->procname),
- ctx->pos);
- }
-
- return res;
+ return 0;
}
+/*
+ * Unregistering a table waits until nobody uses it anymore and a copy to
+ * userspace can take as long as userspace wants. So a listing doesn't take
+ * any table into use. It pins the memory of the entry it is at, finds the
+ * next one under sysctl_lock and emits with nothing held, like
+ * kernfs_fop_readdir() and proc_readdir_de() do. Between calls a listing
+ * resumes after the name of the last entry it emitted.
+ */
static int proc_sys_readdir(struct file *file, struct dir_context *ctx)
{
- struct ctl_table_header *head = grab_header(file_inode(file));
+ struct ctl_table_header *head = PROC_I(file_inode(file))->sysctl;
struct sysctl_cursor *cursor = file->private_data;
- struct ctl_table_header *h = NULL;
- const struct ctl_table *entry;
+ struct sysctl_dirent d = {};
struct ctl_dir *ctl_dir;
- unsigned long pos;
-
- if (IS_ERR(head))
- return PTR_ERR(head);
+ int err;
+ if (!head)
+ head = &sysctl_table_root.default_set.dir.header;
ctl_dir = container_of(head, struct ctl_dir, header);
if (!dir_emit_dots(file, ctx))
- goto out;
+ return 0;
/*
* Resume after the last entry the previous call emitted. Only a seek
@@ -875,21 +891,30 @@ static int proc_sys_readdir(struct file *file, struct dir_context *ctx)
* seeking to an arbitrary position always did.
*/
if (cursor && cursor->pos == ctx->pos) {
- entry_after(ctl_dir, cursor->name, cursor->len, &h, &entry);
- pos = ctx->pos;
+ err = sysctl_dir_next(ctl_dir, &d, cursor->name, cursor->len);
} else {
- first_entry(ctl_dir, &h, &entry);
- pos = 2;
+ loff_t pos = 2;
+
+ err = sysctl_dir_next(ctl_dir, &d, NULL, 0);
+ while (err > 0 && pos++ < ctx->pos)
+ err = sysctl_dir_next(ctl_dir, &d, d.name, d.len);
}
+ if (err < 0)
+ return err;
- for (; h; next_entry(&h, &entry)) {
- if (!scan(h, entry, &pos, file, ctx)) {
- sysctl_head_finish(h);
- break;
+ while (err > 0) {
+ if (!d.skip) {
+ err = proc_sys_fill_dirent(file, &d);
+ if (err)
+ break;
+ if (!dir_emit(ctx, d.name, d.len, d.ino, d.type))
+ break;
}
+ ctx->pos++;
+ sysctl_cursor_set(file, d.name, d.len, ctx->pos);
+ err = sysctl_dir_next(ctl_dir, &d, d.name, d.len);
}
-out:
- sysctl_head_finish(head);
+ sysctl_dir_done(&d);
return 0;
}
--
2.53.0
Re: [PATCH RFC 03/38] netfilter: xt_recent: don't remove the proc entries under a spinlock
Author: Florian Westphal
Date: Mon, 02 Nov 2026 14:41
Date: Mon, 02 Nov 2026 14:41
12 lines
9270 bytes
9270 bytes
Christian Brauner <brauner@kernel.org> wrote: > recent_proc_net_exit() removes the proc entries of all tables with > recent_lock held. remove_proc_entry() waits until nobody calls into the > entry anymore, and reading one of them takes recent_lock in > recent_seq_start() while procfs has the entry pinned. So when a reader > gets there while the network namespace goes away, the exit path sleeps > with a spinlock held and bh disabled, waiting for a reader that spins > on that very lock: Yes, but note that this change is already part of a pending nf -> net merge request.
[PATCH RFC 36/38] selftests/proc: check that faulting sysctl accesses don't hold up removal
Author: Christian Brauner
Date: Mon, 02 Nov 2026 14:43
Date: Mon, 02 Nov 2026 14:43
281 lines
17587 bytes
17587 bytes
Read, write and list /proc/sys/net/ipv6/conf/<dev>/ of a tun device
with a buffer whose first page is backed by userfaultfd and remove the
device while the task sleeps in its copy from or to userspace:
- unregistering the table doesn't wait for a faulting read(), write()
or getdents64()
- the read still gets the value that was produced before the removal
and the next read fails with -ENOENT
- the write is copied in after the removal and fails with -ENOENT
- getdents64() still lists what it looked up before the removal and
fails with -ENOENT afterwards
The tables are unregistered under RTNL, so a regression stalls each
test for five seconds before it fails.
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 | 3 +
.../selftests/proc/proc-remove-slow-sysctl.c | 213 +++++++++++++++++++++
4 files changed, 218 insertions(+)
diff --git a/tools/testing/selftests/proc/.gitignore b/tools/testing/selftests/proc/.gitignore
index 5b5e24e39cbd..7b8133492782 100644
--- a/tools/testing/selftests/proc/.gitignore
+++ b/tools/testing/selftests/proc/.gitignore
@@ -21,6 +21,7 @@
/proc-uptime-001
/proc-uptime-002
/proc-pidns
+/proc-remove-slow-sysctl
/read
/self
/setns-dcache
diff --git a/tools/testing/selftests/proc/Makefile b/tools/testing/selftests/proc/Makefile
index dae09f333455..bce6beca7274 100644
--- a/tools/testing/selftests/proc/Makefile
+++ b/tools/testing/selftests/proc/Makefile
@@ -32,5 +32,6 @@ TEST_GEN_PROGS += proc-multiple-procfs
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
include ../lib.mk
diff --git a/tools/testing/selftests/proc/config b/tools/testing/selftests/proc/config
index 68fbd2b35884..3edd3c437fe4 100644
--- a/tools/testing/selftests/proc/config
+++ b/tools/testing/selftests/proc/config
@@ -1 +1,4 @@
CONFIG_PROC_FS=y
+CONFIG_IPV6=y
+CONFIG_TUN=y
+CONFIG_USERFAULTFD=y
diff --git a/tools/testing/selftests/proc/proc-remove-slow-sysctl.c b/tools/testing/selftests/proc/proc-remove-slow-sysctl.c
new file mode 100644
index 000000000000..e1b1fd19a07a
--- /dev/null
+++ b/tools/testing/selftests/proc/proc-remove-slow-sysctl.c
@@ -0,0 +1,213 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * A read(), write() or getdents64() of /proc/sys that faults while copying
+ * from or to userspace must not hold up unregistering the sysctl table.
+ * /proc/sys/net/ipv6/conf/<dev>/ of a tun device is unregistered under RTNL
+ * when the device's file is closed.
+ */
+#define _GNU_SOURCE
+#include <errno.h>
+#include <fcntl.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 TUN_NAME "procslow0"
+#define CONF_PATH "/proc/sys/net/ipv6/conf/" TUN_NAME
+#define MTU_PATH CONF_PATH "/mtu"
+#define BUF_PAGES 16
+#define TIMEOUT_MS 5000
+
+enum sysctl_op {
+ SYSCTL_READ,
+ SYSCTL_WRITE,
+ SYSCTL_GETDENTS,
+};
+
+struct worker {
+ enum sysctl_op op;
+ int fd;
+ char *buf;
+ size_t len;
+ ssize_t ret;
+ int err;
+};
+
+struct closer {
+ int tun;
+ int done;
+};
+
+static void *work_thread(void *arg)
+{
+ struct worker *w = arg;
+
+ switch (w->op) {
+ case SYSCTL_READ:
+ w->ret = read(w->fd, w->buf, w->len);
+ break;
+ case SYSCTL_WRITE:
+ w->ret = write(w->fd, w->buf, w->len);
+ break;
+ case SYSCTL_GETDENTS:
+ w->ret = syscall(__NR_getdents64, w->fd, w->buf, w->len);
+ break;
+ }
+ w->err = errno;
+ return NULL;
+}
+
+static void *close_thread(void *arg)
+{
+ struct closer *c = arg;
+ ssize_t ret;
+
+ close(c->tun);
+ ret = write(c->done, "x", 1);
+ return (void *)ret;
+}
+
+FIXTURE(proc_sys_slow) {
+ int tun;
+ int uffd;
+ char *buf;
+ long psz;
+};
+
+FIXTURE_SETUP(proc_sys_slow)
+{
+ struct uffdio_api api = { .api = UFFD_API };
+ struct uffdio_register reg = {};
+ struct ifreq ifr = {};
+
+ if (unshare(CLONE_NEWNET))
+ SKIP(return, "unshare(CLONE_NEWNET): %s", strerror(errno));
+
+ self->tun = open("/dev/net/tun", O_RDWR | O_CLOEXEC);
+ if (self->tun < 0)
+ SKIP(return, "/dev/net/tun: %s", strerror(errno));
+ strncpy(ifr.ifr_name, TUN_NAME, IFNAMSIZ - 1);
+ ifr.ifr_flags = IFF_TUN | IFF_NO_PI;
+ ASSERT_EQ(ioctl(self->tun, TUNSETIFF, &ifr), 0);
+ if (access(CONF_PATH, F_OK))
+ SKIP(return, CONF_PATH ": %s", strerror(errno));
+
+ self->uffd = syscall(__NR_userfaultfd, O_CLOEXEC | O_NONBLOCK);
+ if (self->uffd < 0)
+ SKIP(return, "userfaultfd: %s", strerror(errno));
+ ASSERT_EQ(ioctl(self->uffd, UFFDIO_API, &api), 0);
+
+ /* Only the first page of the buffer goes through userfaultfd. */
+ self->psz = sysconf(_SC_PAGESIZE);
+ self->buf = mmap(NULL, BUF_PAGES * self->psz, PROT_READ | PROT_WRITE,
+ MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
+ ASSERT_NE(self->buf, MAP_FAILED);
+ reg.range.start = (unsigned long)self->buf;
+ reg.range.len = self->psz;
+ reg.mode = UFFDIO_REGISTER_MODE_MISSING;
+ ASSERT_EQ(ioctl(self->uffd, UFFDIO_REGISTER, ®), 0);
+}
+
+FIXTURE_TEARDOWN(proc_sys_slow)
+{
+}
+
+/*
+ * Let @w sleep in its copy from or to the first page of the buffer, then
+ * close the device. Unregistering the table must not wait for @w.
+ */
+static void stall_and_remove(struct __test_metadata *_metadata,
+ FIXTURE_DATA(proc_sys_slow) *self,
+ struct worker *w)
+{
+ struct closer c = { .tun = self->tun };
+ struct uffdio_zeropage zero = {};
+ struct uffd_msg msg;
+ struct pollfd pfd;
+ pthread_t wt, ct;
+ int done[2];
+
+ w->buf = self->buf;
+ ASSERT_EQ(pthread_create(&wt, NULL, work_thread, w), 0);
+
+ pfd = (struct pollfd){ .fd = self->uffd, .events = POLLIN };
+ ASSERT_EQ(poll(&pfd, 1, TIMEOUT_MS), 1);
+ ASSERT_EQ(read(self->uffd, &msg, sizeof(msg)), sizeof(msg));
+ ASSERT_EQ(msg.event, UFFD_EVENT_PAGEFAULT);
+
+ ASSERT_EQ(pipe2(done, O_CLOEXEC), 0);
+ c.done = done[1];
+ ASSERT_EQ(pthread_create(&ct, NULL, close_thread, &c), 0);
+ pfd = (struct pollfd){ .fd = done[0], .events = POLLIN };
+ EXPECT_EQ(poll(&pfd, 1, TIMEOUT_MS), 1) {
+ TH_LOG("unregistering " CONF_PATH " waited for the faulting task");
+ }
+
+ zero.range.start = (unsigned long)self->buf;
+ zero.range.len = self->psz;
+ ASSERT_EQ(ioctl(self->uffd, UFFDIO_ZEROPAGE, &zero), 0);
+ ASSERT_EQ(pthread_join(wt, NULL), 0);
+ ASSERT_EQ(pthread_join(ct, NULL), 0);
+}
+
+TEST_F(proc_sys_slow, read)
+{
+ struct worker w = {
+ .op = SYSCTL_READ,
+ .len = BUF_PAGES * self->psz,
+ };
+
+ w.fd = open(MTU_PATH, O_RDONLY | O_CLOEXEC);
+ ASSERT_GE(w.fd, 0);
+ stall_and_remove(_metadata, self, &w);
+
+ /* The value was produced before the table went away, then nothing. */
+ EXPECT_GT(w.ret, 0);
+ EXPECT_EQ(read(w.fd, self->buf, self->psz), -1);
+ EXPECT_EQ(errno, ENOENT);
+}
+
+TEST_F(proc_sys_slow, write)
+{
+ struct worker w = {
+ .op = SYSCTL_WRITE,
+ .len = 5,
+ };
+
+ w.fd = open(MTU_PATH, O_WRONLY | O_CLOEXEC);
+ ASSERT_GE(w.fd, 0);
+ stall_and_remove(_metadata, self, &w);
+
+ /* The table went away while the value was copied in. */
+ EXPECT_EQ(w.ret, -1);
+ EXPECT_EQ(w.err, ENOENT);
+}
+
+TEST_F(proc_sys_slow, getdents)
+{
+ struct worker w = {
+ .op = SYSCTL_GETDENTS,
+ .len = BUF_PAGES * self->psz,
+ };
+
+ w.fd = open(CONF_PATH, O_RDONLY | O_DIRECTORY | O_CLOEXEC);
+ ASSERT_GE(w.fd, 0);
+ stall_and_remove(_metadata, self, &w);
+
+ /* What was looked up before the removal still arrives, then nothing. */
+ EXPECT_GT(w.ret, 0);
+ EXPECT_EQ(syscall(__NR_getdents64, w.fd, self->buf, self->psz), -1);
+ EXPECT_EQ(errno, ENOENT);
+}
+
+TEST_HARNESS_MAIN
--
2.53.0
[PATCH RFC 37/38] proc: don't pin a symlink's entry while readlink() copies it out
Author: Christian Brauner
Date: Mon, 02 Nov 2026 14:43
Date: Mon, 02 Nov 2026 14:43
56 lines
11350 bytes
11350 bytes
proc_get_link() pins the entry with use_pde() and the delayed call
unpins it. vfs_readlink() copies the link to userspace in between. So a
page fault in that copy holds up remove_proc_entry() just like the
copies of reads and writes do, and the callers hold locks while they
wait. /proc/asound/<id> is removed with snd_card_mutex and info_mutex
held when a card changes its id.
The pin doesn't keep anything alive. proc_symlink() copies the target
and pde_free() frees it with the entry, and the inode holds a reference
to the entry. What the pin does is keep a removed symlink from being
followed. So check ->in_use instead and drop the delayed call. That
also takes an atomic pair off every traversal of /proc/net and
/proc/mounts.
Fixes: 7e0e953bb0cf ("procfs: fix race between symlink removals and traversals")
Cc: stable@vger.kernel.org
Signed-off-by: Christian Brauner (Amutable) <brauner@kernel.org>
---
fs/proc/inode.c | 14 +++++++-------
1 file changed, 7 insertions(+), 7 deletions(-)
diff --git a/fs/proc/inode.c b/fs/proc/inode.c
index 9933f03ab9fb..2d65f3c8ec03 100644
--- a/fs/proc/inode.c
+++ b/fs/proc/inode.c
@@ -629,19 +629,19 @@ static const struct file_operations proc_iter_file_ops_compat = {
};
#endif
-static void proc_put_link(void *p)
-{
- unuse_pde(p);
-}
-
static const char *proc_get_link(struct dentry *dentry,
struct inode *inode,
struct delayed_call *done)
{
struct proc_dir_entry *pde = PDE(inode);
- if (!use_pde(pde))
+
+ /*
+ * No pin. The link lives with the entry, which the inode holds, and a
+ * reader could fault while vfs_readlink() copies it out. Just don't
+ * follow a removed one.
+ */
+ if (unlikely(atomic_read(&pde->in_use) < 0))
return ERR_PTR(-EINVAL);
- set_delayed_call(done, proc_put_link, pde);
return pde->data;
}
--
2.53.0
[PATCH RFC 38/38] selftests/proc: check that a faulting readlink() doesn't hold up removal
Author: 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
[PATCH RFC 00/16] proc: let procfs own the data path
Author: Christian Brauner
Date: Mon, 02 Nov 2026 14:48
Date: Mon, 02 Nov 2026 14:48
195 lines
21351 bytes
21351 bytes
remove_proc_entry() waits until every call into the entry has returned.
For a seq_file entry that's all of seq_read_iter() including the copy
to userspace. For everything else it's ->proc_read(), ->proc_write()
and ->proc_ioctl(), which copy to and from userspace themselves. A
reader or writer can make a page fault in that copy take as long as it
wants, e.g. with userfaultfd or by punching holes into a memfd over and
over. And callers of remove_proc_entry() like to hold locks while they
wait. addrconf holds RTNL when it removes /proc/net/dev_snmp6/<dev> and
xt_hashlimit holds hashlimit_mutex which is shared by all network
namespaces.
An earlier AI generated version bounced the text through a second buffer
in seq_file so the copy to userspace could happen with the entry
unpinned. That second buffering is really nasty and complicates
everything unnecessarily. So after chatting with Jann a bit I decided to
do what kernfs does instead: the entry's code only ever runs with the
entry pinned and never touches userspace memory. This series is the
procfs side of that. The entries are converted in four series on top of
it, one per area of the tree, which don't depend on each other, and a
last one drops the __user methods.
seq_file reads: procfs owns the seq_file and only calls into the entry
with it pinned. An entry declares its seq_operations, or just a show
function, and the size of its private state in struct proc_ops. procfs
opens and frees the seq_file and reads and seeks through seq_file
directly. ->proc_open() and ->proc_release() only deal with whatever
the entry needs on top of that. proc_create_seq() and friends already
keep the seq_operations in the entry so they're switched over in one
go here. That covers most of /proc/net. The open coded single_open()
and seq_open() users that don't do anything else are converted with a
coccinelle script, also here. A nice side effect is that opening one of
these entries no longer allocates a pde_opener, they have no
->proc_release() of their own anymore.
Writes: procfs copies in what is written before it pins the entry and
hands the entry a NUL-terminated kernel copy through ->proc_write_buf().
The copy is capped at PAGE_SIZE - 1 bytes unless the entry asks for
more in ->proc_write_size, which is what proc_simple_write() always
did. Entries that take binary data in pieces, the PCI config space,
ALSA's data entries, cio_ignore and the rtas flash files, ask for
PROC_ENTRY_SHORT_WRITES and their writes come up short. Since procfs
copies in the whole write first, a write that is too large or whose
buffer can't be read fails before the entry's own checks now,
permission checks included.
Reads that don't go through seq_file: an entry fills a kernel buffer in
->proc_read_buf(), procfs copies that to userspace after unpinning the
entry and only moves the position if that copy worked, like
kernfs_file_read_iter(). The buffer is a page unless the entry asks for
more in ->proc_read_size, and like with kernfs a larger read comes up
short. Entries whose readers want all of a large file in one read,
/proc/vmcore, /proc/config.gz and ALSA's data entries, ask for
PROC_ENTRY_FULL_READS and procfs fills their reads in pieces. These
entries are read through ->read_iter() now, so sendfile() and splice()
work for them.
ioctls: the command number doesn't reliably say what an ioctl copies.
The PCI ioctls aren't encoded at all, i8k's encode the wrong sizes and
esas2r uses a range of plain numbers. So an entry lists its commands in
->proc_ioctl_args with what goes in and what comes back out, like DRM's
ioctl tables. procfs copies the argument in before it pins the entry,
calls ->proc_ioctl_buf() with that copy and copies it back out after
unpinning the entry. A command that isn't in the table fails with
-ENOTTY. 32-bit tasks go through the same table, so the compat hooks go
away.
The limits of reads and writes are remembered in the entry when it is
registered, the proc_ops go away with the entry and procfs shouldn't
have to pin it just to look up a number. An entry that declares a
seq_file and a read or seek method of its own, or one of the new
methods next to the old one it replaces, fails to register.
Entries that are never removed, kcore, kmsg, the kpage files and
/proc/profile, keep reading straight into userspace. procfs never pins
those, so nothing can wait for them. The last series enforces that.
What this doesn't cover is mmap(). procfs pins the entry around
->proc_mmap() only and a page fault into the mapping runs the entry's
vm_ops unpinned. kernfs wraps those. That's for another day.
On a KASAN and lockdep kernel the reader selftest fails without the
series and passes with it. The other proc selftests behave the same
with and without the series and so do the entries converted here that I
could get at in a VM.
This is on top of "proc: fix a few old bugs in /proc handlers".
Signed-off-by: Christian Brauner (Amutable) <brauner@kernel.org>
---
Christian Brauner (16):
proc: drop the compat file operations
proc: add pde_pin() and pde_unpin()
proc: make room for more entry flags
proc: let procfs own the seq_file of seq_file entries
proc: let procfs own the seq_file of proc_create_seq() and friends
proc: let procfs own the seq_file of DEFINE_PROC_SHOW_ATTRIBUTE() files
treewide: let procfs own the seq_file of simple proc entries
selftests/proc: check that a faulting reader doesn't hold up removal
seq_file: add seq_preallocate()
proc: let procfs own the seq_file of /proc/stat
proc: let procfs copy in writes before pinning the entry
proc: copy in writes to the network namespace entries before pinning
proc: let procfs copy out reads that don't go through seq_file
fs/proc/vmcore: let procfs copy out reads
proc: let procfs copy ioctl arguments in and out
Documentation: seq_file: describe how a /proc file uses seq_file
Documentation/filesystems/seq_file.rst | 42 +-
arch/alpha/kernel/srm_env.c | 10 +-
arch/arm/mm/alignment.c | 10 +-
arch/powerpc/kernel/rtas-proc.c | 50 +--
arch/powerpc/platforms/pseries/lpar.c | 20 +-
arch/powerpc/platforms/pseries/lparcfg.c | 10 +-
arch/sh/mm/alignment.c | 10 +-
arch/sparc/kernel/led.c | 10 +-
arch/um/kernel/exitcode.c | 10 +-
drivers/acpi/proc.c | 12 +-
drivers/hwmon/dell-smm-hwmon.c | 10 +-
drivers/input/input.c | 24 +-
drivers/macintosh/via-pmu.c | 10 +-
drivers/net/wireless/intel/ipw2x00/libipw_module.c | 10 +-
drivers/platform/x86/lenovo/thinkpad_acpi.c | 10 +-
drivers/platform/x86/toshiba_acpi.c | 40 +-
drivers/pnp/pnpbios/proc.c | 10 +-
drivers/s390/block/dasd_proc.c | 10 +-
drivers/s390/cio/blacklist.c | 13 +-
drivers/scsi/scsi_devinfo.c | 10 +-
drivers/scsi/scsi_proc.c | 21 +-
drivers/usb/gadget/function/rndis.c | 10 +-
drivers/video/fbdev/via/viafbdev.c | 70 +---
fs/jfs/jfs_debug.c | 10 +-
fs/ntfs3/super.c | 20 +-
fs/proc/cpuinfo.c | 10 +-
fs/proc/generic.c | 126 +++---
fs/proc/inode.c | 457 ++++++++++++++++-----
fs/proc/internal.h | 54 ++-
fs/proc/proc_net.c | 44 +-
fs/proc/stat.c | 6 +-
fs/proc/vmcore.c | 14 +-
fs/seq_file.c | 36 +-
fs/smb/client/cifs_debug.c | 79 +---
fs/smb/client/dfs_cache.c | 10 +-
include/linux/proc_fs.h | 91 +++-
include/linux/seq_file.h | 11 +-
kernel/irq/proc.c | 30 +-
kernel/latencytop.c | 10 +-
lib/dynamic_debug.c | 6 +-
mm/slab_common.c | 10 +-
net/core/pktgen.c | 30 +-
net/sunrpc/stats.c | 10 +-
.../selftests/proc/proc-remove-slow-reader.c | 133 ++++++
44 files changed, 837 insertions(+), 792 deletions(-)
---
base-commit: 65ba8f34ec886ec72b2c07ddc8cf3d815c32869d
change-id: 20261101-work-procfs-core-8fa4a4f412cf
prerequisite-change-id: 20261101-work-procfs-bugfixes-054d00028491:v1
prerequisite-patch-id: 0474af12959624910718795bf86684383664c685
prerequisite-patch-id: 540aa9ea28adcbc54d8264f66314e29e3b8c9d49
prerequisite-patch-id: 2f7139d1c972c4439dccca610caa5f1288987a3d
prerequisite-patch-id: b0031a7c4855b7ec38259c59f30f65101a8fc4b5
prerequisite-patch-id: 0b7f226abf2b9ff41873a1aa55cf97747647d56b
prerequisite-patch-id: f63e345f897619af54813958046617c655fb7daf
prerequisite-patch-id: c0e21e762a2960e3324c5ab04634a7035e515fa6
prerequisite-patch-id: fd59c71eadd8f39c58b3baea93567bc093bc9b0e
prerequisite-patch-id: 5d52cc8a5c73e4c5ba16b8e24f51408c17583bf9
prerequisite-patch-id: 1f9179284e2f3314d4b46ac5e989089a21c7576c
prerequisite-patch-id: 10397bbf696bee16dc71eb79fff6c5e73842bcbc
prerequisite-patch-id: 9b44279dcf08912238d2f6438a3328a778a79a0c
prerequisite-patch-id: 16854b621a3a71dfc365aaf7abaa7f65adfe9f7e
prerequisite-patch-id: ca42eea21fae1fd1cd3518ab057c74b61ffd68fe
prerequisite-patch-id: 23255dc2be76b0479c083dbd4c897bfbb533aed5
prerequisite-patch-id: 82129c687ebf0a41b369893b92f2c038420ab801
prerequisite-patch-id: 56ad57a99a5bda075a9f4e844368974296744934
prerequisite-patch-id: 288b3cbaa11e01a778cc3d7beb6c0549aacf59f2
prerequisite-patch-id: a163b1549608652bfa5bd9ff93efa55b541010c4
prerequisite-patch-id: 78f067c343177e52d49ddfe9ee2992e602549869
prerequisite-patch-id: 19c12bc6e3fbed35acc1f657196b1a997f48cdfe
prerequisite-patch-id: 5800c3076694bad719954b2ad4d7448c6e013bfd
prerequisite-patch-id: 2a2f63d8843b159594b9eee6ddea028ce76a6270
prerequisite-patch-id: 40a1951d57906c4a1b98a4bd96a814b80339b9c5
prerequisite-patch-id: e2141077e69f20a2b27fab69c8d61d0686682f53
prerequisite-patch-id: c4d6018c26c40dc549d88330d23ce48f872c2b7f
prerequisite-patch-id: 6de8ec17746031bc6d1d9ac262c5be0f905fcd79
prerequisite-patch-id: b87bcb167884605bff1fd0f2ed8a88942d07efd5
prerequisite-patch-id: cfa9a8b4c67e20cfd22d77035bfd21ee96ab3298
prerequisite-patch-id: abf3482ed2399b7b2e1ee72e937af05bb9200667
prerequisite-patch-id: 02c175b2dee565a85b3308ecf2ec8e3c9a029e0f
prerequisite-patch-id: 4ed6b3d3d45de49d4967e88ba4e7f03372775f55
prerequisite-patch-id: dbe02fe3f784776fda5e7ae99f077bef0cd4b48d
prerequisite-patch-id: 53edc9a95afff57c4acba40095a8054924261ea2
prerequisite-patch-id: cf9ce0099005fd3bc31a1e15131d48e315ac3bee
prerequisite-patch-id: 16a657a5d0046068dbace29e7ea046ac1441f46f
prerequisite-patch-id: efeb2d6f7df71d5dede2c1d12fe999a28e38aab9
prerequisite-patch-id: e6ffe2a5c3ee5645f56c8366b568fbc9f140848c
[PATCH RFC 01/16] proc: drop the compat file operations
Author: Christian Brauner
Date: Mon, 02 Nov 2026 14:48
Date: Mon, 02 Nov 2026 14:48
159 lines
14849 bytes
14849 bytes
procfs has both sets of file operations twice, with and without
->compat_ioctl(), and picks the one with it for entries that have
->proc_compat_ioctl(). That's from commit 778f3dd5a13c ("Fix procfs
compat_ioctl regression") when a file without ->compat_ioctl() went
through the translation tables in fs/compat_ioctl.c and one with it
didn't. Those tables are gone since commit 77b9040195de ("compat_ioctl:
simplify the implementation"). compat_sys_ioctl() answers -ENOTTY for a
file without ->compat_ioctl() now, which is what
proc_reg_compat_ioctl() answers for an entry without
->proc_compat_ioctl().
So always set ->compat_ioctl() and drop the second set of file
operations and the flag that picked it.
No functional changes.
Signed-off-by: Christian Brauner (Amutable) <brauner@kernel.org>
---
fs/proc/generic.c | 4 ----
fs/proc/inode.c | 45 +++------------------------------------------
fs/proc/internal.h | 9 ---------
include/linux/proc_fs.h | 1 -
4 files changed, 3 insertions(+), 56 deletions(-)
diff --git a/fs/proc/generic.c b/fs/proc/generic.c
index 23fc026d7bbb..761f9958558f 100644
--- a/fs/proc/generic.c
+++ b/fs/proc/generic.c
@@ -445,10 +445,6 @@ static void pde_set_flags(struct proc_dir_entry *pde)
pde->flags |= PROC_ENTRY_PERMANENT;
if (proc_ops->proc_read_iter)
pde->flags |= PROC_ENTRY_proc_read_iter;
-#ifdef CONFIG_COMPAT
- if (proc_ops->proc_compat_ioctl)
- pde->flags |= PROC_ENTRY_proc_compat_ioctl;
-#endif
if (proc_ops->proc_lseek)
pde->flags |= PROC_ENTRY_proc_lseek;
}
diff --git a/fs/proc/inode.c b/fs/proc/inode.c
index 2d65f3c8ec03..18f6490bfe77 100644
--- a/fs/proc/inode.c
+++ b/fs/proc/inode.c
@@ -410,6 +410,8 @@ static long proc_reg_compat_ioctl(struct file *file, unsigned int cmd, unsigned
}
return rv;
}
+#else
+#define proc_reg_compat_ioctl NULL
#endif
static int pde_mmap(struct proc_dir_entry *pde, struct file *file, struct vm_area_struct *vma)
@@ -569,6 +571,7 @@ static const struct file_operations proc_reg_file_ops = {
.write = proc_reg_write,
.poll = proc_reg_poll,
.unlocked_ioctl = proc_reg_unlocked_ioctl,
+ .compat_ioctl = proc_reg_compat_ioctl,
.mmap = proc_reg_mmap,
.get_unmapped_area = proc_reg_get_unmapped_area,
#ifndef CONFIG_MMU
@@ -585,39 +588,6 @@ static const struct file_operations proc_iter_file_ops = {
.splice_read = copy_splice_read,
.poll = proc_reg_poll,
.unlocked_ioctl = proc_reg_unlocked_ioctl,
- .mmap = proc_reg_mmap,
- .get_unmapped_area = proc_reg_get_unmapped_area,
-#ifndef CONFIG_MMU
- .mmap_capabilities = proc_reg_mmap_capabilities,
-#endif
- .open = proc_reg_open,
- .release = proc_reg_release,
-};
-
-#ifdef CONFIG_COMPAT
-static const struct file_operations proc_reg_file_ops_compat = {
- .llseek = proc_reg_llseek,
- .read = proc_reg_read,
- .write = proc_reg_write,
- .poll = proc_reg_poll,
- .unlocked_ioctl = proc_reg_unlocked_ioctl,
- .compat_ioctl = proc_reg_compat_ioctl,
- .mmap = proc_reg_mmap,
- .get_unmapped_area = proc_reg_get_unmapped_area,
-#ifndef CONFIG_MMU
- .mmap_capabilities = proc_reg_mmap_capabilities,
-#endif
- .open = proc_reg_open,
- .release = proc_reg_release,
-};
-
-static const struct file_operations proc_iter_file_ops_compat = {
- .llseek = proc_reg_llseek,
- .read_iter = proc_reg_read_iter,
- .splice_read = copy_splice_read,
- .write = proc_reg_write,
- .poll = proc_reg_poll,
- .unlocked_ioctl = proc_reg_unlocked_ioctl,
.compat_ioctl = proc_reg_compat_ioctl,
.mmap = proc_reg_mmap,
.get_unmapped_area = proc_reg_get_unmapped_area,
@@ -627,7 +597,6 @@ static const struct file_operations proc_iter_file_ops_compat = {
.open = proc_reg_open,
.release = proc_reg_release,
};
-#endif
static const char *proc_get_link(struct dentry *dentry,
struct inode *inode,
@@ -683,14 +652,6 @@ struct inode *proc_get_inode(struct super_block *sb, struct proc_dir_entry *de)
inode->i_fop = &proc_iter_file_ops;
else
inode->i_fop = &proc_reg_file_ops;
-#ifdef CONFIG_COMPAT
- if (pde_has_proc_compat_ioctl(de)) {
- if (pde_has_proc_read_iter(de))
- inode->i_fop = &proc_iter_file_ops_compat;
- else
- inode->i_fop = &proc_reg_file_ops_compat;
- }
-#endif
} else if (S_ISDIR(inode->i_mode)) {
inode->i_op = de->proc_iops;
inode->i_fop = de->proc_dir_ops;
diff --git a/fs/proc/internal.h b/fs/proc/internal.h
index 04f7ce2b24ed..268a0523be12 100644
--- a/fs/proc/internal.h
+++ b/fs/proc/internal.h
@@ -92,15 +92,6 @@ static inline bool pde_has_proc_read_iter(const struct proc_dir_entry *pde)
return pde->flags & PROC_ENTRY_proc_read_iter;
}
-static inline bool pde_has_proc_compat_ioctl(const struct proc_dir_entry *pde)
-{
-#ifdef CONFIG_COMPAT
- return pde->flags & PROC_ENTRY_proc_compat_ioctl;
-#else
- return false;
-#endif
-}
-
static inline bool pde_has_proc_lseek(const struct proc_dir_entry *pde)
{
return pde->flags & PROC_ENTRY_proc_lseek;
diff --git a/include/linux/proc_fs.h b/include/linux/proc_fs.h
index 47d7deaeed8f..6ad5150020bf 100644
--- a/include/linux/proc_fs.h
+++ b/include/linux/proc_fs.h
@@ -26,7 +26,6 @@ enum {
#endif
PROC_ENTRY_proc_read_iter = 1U << 1,
- PROC_ENTRY_proc_compat_ioctl = 1U << 2,
PROC_ENTRY_proc_lseek = 1U << 3,
PROC_ENTRY_FORCE_LOOKUP = 1U << 7,
--
2.53.0
[PATCH RFC 03/16] proc: make room for more entry flags
Author: Christian Brauner
Date: Mon, 02 Nov 2026 14:48
Date: Mon, 02 Nov 2026 14:48
28 lines
10128 bytes
10128 bytes
->flags of struct proc_dir_entry is a u8 and the next patches need more
flags than it has room for, so make it a u16. Bit 6 stays free,
vfs-7.5.mount takes it for PROC_ENTRY_CANT_MOUNT.
No functional changes.
Signed-off-by: Christian Brauner (Amutable) <brauner@kernel.org>
---
fs/proc/internal.h | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/fs/proc/internal.h b/fs/proc/internal.h
index 268a0523be12..41e00c978ddd 100644
--- a/fs/proc/internal.h
+++ b/fs/proc/internal.h
@@ -61,7 +61,7 @@ struct proc_dir_entry {
struct rb_node subdir_node;
char *name;
umode_t mode;
- u8 flags;
+ u16 flags;
u8 namelen;
char inline_name[];
} __randomize_layout;
--
2.53.0
[PATCH RFC 04/16] proc: let procfs own the seq_file of seq_file entries
Author: Christian Brauner
Date: Mon, 02 Nov 2026 14:48
Date: Mon, 02 Nov 2026 14:48
420 lines
23492 bytes
23492 bytes
remove_proc_entry() waits until every call into the entry has returned.
For a seq_file entry that's all of seq_read_iter(), including the copy
to userspace. A page fault in that copy can be made to take as long as
the reader wants, e.g. with userfaultfd or by punching holes into a
memfd over and over. And callers of remove_proc_entry() like to hold
locks while they wait. xt_hashlimit holds hashlimit_mutex, which all
network namespaces share, and addrconf holds RTNL.
But the entry's code only runs between ->start() and ->stop() and the
copies to userspace happen outside of that. So let procfs own the
seq_file the way kernfs does. An entry declares its seq_operations, or
just a show function, and the size of its private state in struct
proc_ops. procfs then opens the seq_file with seq_operations of its own
that pin the entry around every call into the entry, and reads and
seeks through seq_file directly. The copies to userspace happen with
the entry unpinned.
That changes who owns the seq_file. remove_proc_entry() can now finish
while a reader is still copying out of the buffer, so the seq_file
can't go away with the entry. procfs frees it once the file is
released. ->proc_open() finds the seq_file in ->private_data and sets
up whatever the entry needs and ->proc_release() releases just that. It
still runs when the entry is removed, or when the file is closed before
that.
Since procfs reads these entries through seq_read_iter() itself they
all have ->splice_read() now, so sendfile(), splice(), aio, the RWF
flags and kernel_read() work on them, and readv() is one read instead
of one per segment. Note that a removed entry still hands out what it
had already formatted and then answers -EIO to reads, and to seeks that
have to walk the records. Seeking it failed with -EINVAL right away
before.
The seq_operations of procfs serve both kinds of entries. For an entry
that only has a show function they do what single_open() does, like
kernfs_seq_start(). seq_file's complaint about a ->next() that doesn't
move the position would name procfs' wrapper, so the wrapper checks
that itself and names the entry's. An entry that declares a seq_file
and also a read or seek method of its own, or one that releases the
seq_file with seq_release() itself, doesn't make sense, procfs reads,
seeks and releases the seq_file itself, so registering it fails.
No users yet.
Suggested-by: Jann Horn <jannh@google.com>
Signed-off-by: Christian Brauner (Amutable) <brauner@kernel.org>
---
fs/proc/generic.c | 36 +++++++++--
fs/proc/inode.c | 167 ++++++++++++++++++++++++++++++++++++++++++++----
fs/proc/internal.h | 16 +++++
include/linux/proc_fs.h | 22 ++++++-
4 files changed, 219 insertions(+), 22 deletions(-)
diff --git a/fs/proc/generic.c b/fs/proc/generic.c
index 761f9958558f..eff5c38e2219 100644
--- a/fs/proc/generic.c
+++ b/fs/proc/generic.c
@@ -434,19 +434,41 @@ static const struct inode_operations proc_dir_inode_operations = {
.setattr = proc_setattr,
};
-static void pde_set_flags(struct proc_dir_entry *pde)
+static int pde_set_flags(struct proc_dir_entry *pde)
{
const struct proc_ops *proc_ops = pde->proc_ops;
if (!proc_ops)
- return;
+ return 0;
if (proc_ops->proc_flags & PROC_ENTRY_PERMANENT)
pde->flags |= PROC_ENTRY_PERMANENT;
- if (proc_ops->proc_read_iter)
- pde->flags |= PROC_ENTRY_proc_read_iter;
- if (proc_ops->proc_lseek)
+ if (proc_ops->proc_seq_ops) {
+ pde->seq_ops = proc_ops->proc_seq_ops;
+ pde->state_size = proc_ops->proc_state_size;
+ pde->flags |= PROC_ENTRY_proc_seq_ops;
+ } else if (proc_ops->proc_show) {
+ pde->single_show = proc_ops->proc_show;
+ pde->flags |= PROC_ENTRY_proc_show;
+ }
+ if (pde_is_seq_file(pde)) {
+ /* procfs reads and seeks through the seq_file itself */
+ if (WARN_ON_ONCE(proc_ops->proc_read || proc_ops->proc_read_iter ||
+ proc_ops->proc_lseek))
+ return -EINVAL;
+ /* and frees it, see pde_seq_release() */
+ if (WARN_ON_ONCE(proc_ops->proc_release == seq_release ||
+ proc_ops->proc_release == seq_release_private ||
+ proc_ops->proc_release == single_release))
+ return -EINVAL;
pde->flags |= PROC_ENTRY_proc_lseek;
+ } else {
+ if (proc_ops->proc_read_iter)
+ pde->flags |= PROC_ENTRY_proc_read_iter;
+ if (proc_ops->proc_lseek)
+ pde->flags |= PROC_ENTRY_proc_lseek;
+ }
+ return 0;
}
/* returns the registered entry, or frees dp and returns NULL on failure */
@@ -456,8 +478,8 @@ struct proc_dir_entry *proc_register(struct proc_dir_entry *dir,
if (proc_alloc_inum(&dp->low_ino))
goto out_free_entry;
- if (!S_ISDIR(dp->mode))
- pde_set_flags(dp);
+ if (!S_ISDIR(dp->mode) && pde_set_flags(dp))
+ goto out_free_inum;
write_lock(&proc_subdir_lock);
dp->parent = dir;
diff --git a/fs/proc/inode.c b/fs/proc/inode.c
index 66a3688757dd..8425465aae5d 100644
--- a/fs/proc/inode.c
+++ b/fs/proc/inode.c
@@ -218,6 +218,110 @@ static void pde_unpin(struct proc_dir_entry *pde)
unuse_pde(pde);
}
+/*
+ * procfs owns the seq_file of a seq_file entry and only calls into the entry
+ * with it pinned. A read copies to userspace without the pin, so a reader
+ * that faults on its buffer doesn't hold up remove_proc_entry().
+ *
+ * An entry that only has a show function is iterated like single_open() does
+ * it, see kernfs_seq_start().
+ *
+ * Like ERR_PTR(-ENODEV) in kernfs_seq_start(), ERR_PTR(-EIO) means the entry
+ * isn't pinned. If the entry returns it itself, it is unpinned right away.
+ */
+static struct proc_dir_entry *seq_pde(struct seq_file *m)
+{
+ return PDE(file_inode(m->file));
+}
+
+static void pde_seq_stop_pinned(struct seq_file *m, void *v)
+{
+ struct proc_dir_entry *pde = seq_pde(m);
+
+ if (pde_has_proc_seq_ops(pde))
+ pde->seq_ops->stop(m, v);
+ pde_unpin(pde);
+}
+
+static void *pde_seq_start(struct seq_file *m, loff_t *pos)
+{
+ struct proc_dir_entry *pde = seq_pde(m);
+ void *v;
+
+ if (!pde_pin(pde))
+ return ERR_PTR(-EIO);
+ if (!pde_has_proc_seq_ops(pde))
+ return single_start(m, pos);
+ v = pde->seq_ops->start(m, pos);
+ if (v == ERR_PTR(-EIO))
+ pde_seq_stop_pinned(m, v);
+ return v;
+}
+
+static void *pde_seq_next(struct seq_file *m, void *v, loff_t *pos)
+{
+ struct proc_dir_entry *pde = seq_pde(m);
+ loff_t old = *pos;
+
+ if (!pde_has_proc_seq_ops(pde)) {
+ ++*pos;
+ return NULL;
+ }
+ v = pde->seq_ops->next(m, v, pos);
+ if (v == ERR_PTR(-EIO))
+ pde_seq_stop_pinned(m, v);
+ /* seq_file would blame these seq_operations, blame the entry's */
+ if (*pos == old) {
+ pr_info_ratelimited("buggy .next function %ps did not update position index\n",
+ pde->seq_ops->next);
+ ++*pos;
+ }
+ return v;
+}
+
+static void pde_seq_stop(struct seq_file *m, void *v)
+{
+ if (v != ERR_PTR(-EIO))
+ pde_seq_stop_pinned(m, v);
+}
+
+static int pde_seq_show(struct seq_file *m, void *v)
+{
+ struct proc_dir_entry *pde = seq_pde(m);
+
+ if (pde_has_proc_seq_ops(pde))
+ return pde->seq_ops->show(m, v);
+ return pde->single_show(m, v);
+}
+
+static const struct seq_operations pde_seq_ops = {
+ .start = pde_seq_start,
+ .next = pde_seq_next,
+ .stop = pde_seq_stop,
+ .show = pde_seq_show,
+};
+
+static int pde_seq_open(struct proc_dir_entry *pde, struct file *file)
+{
+ int err;
+
+ if (pde->state_size)
+ return seq_open_private(file, &pde_seq_ops, pde->state_size);
+ err = seq_open(file, &pde_seq_ops);
+ if (!err && pde_has_proc_show(pde))
+ ((struct seq_file *)file->private_data)->private = pde->data;
+ return err;
+}
+
+static void pde_seq_release(struct proc_dir_entry *pde, struct inode *inode,
+ struct file *file)
+{
+ if (pde->state_size)
+ seq_release_private(inode, file);
+ else
+ seq_release(inode, file);
+}
+
/*
* At most 2 contexts can enter this function: the one doing the last
* close on the descriptor and whoever is deleting PDE itself.
@@ -474,22 +578,35 @@ static unsigned int proc_reg_mmap_capabilities(struct file *file)
}
#endif
+static int pde_open(struct proc_dir_entry *pde, struct inode *inode,
+ struct file *file)
+{
+ const auto open = pde->proc_ops->proc_open;
+ int rv;
+
+ if (!pde_is_seq_file(pde))
+ return open ? open(inode, file) : 0;
+
+ rv = pde_seq_open(pde, file);
+ if (rv || !open)
+ return rv;
+ rv = open(inode, file);
+ if (rv)
+ pde_seq_release(pde, inode, file);
+ return rv;
+}
+
static int proc_reg_open(struct inode *inode, struct file *file)
{
struct proc_dir_entry *pde = PDE(inode);
int rv = 0;
- typeof_member(struct proc_ops, proc_open) open;
struct pde_opener *pdeo;
if (!pde_has_proc_lseek(pde))
file->f_mode &= ~FMODE_LSEEK;
- if (pde_is_permanent(pde)) {
- open = pde->proc_ops->proc_open;
- if (open)
- rv = open(inode, file);
- return rv;
- }
+ if (pde_is_permanent(pde))
+ return pde_open(pde, inode, file);
/*
* Ensure that
@@ -514,9 +631,7 @@ static int proc_reg_open(struct inode *inode, struct file *file)
}
}
- open = pde->proc_ops->proc_open;
- if (open)
- rv = open(inode, file);
+ rv = pde_open(pde, inode, file);
if (release) {
if (rv == 0) {
@@ -544,21 +659,43 @@ static int proc_reg_release(struct inode *inode, struct file *file)
if (pde_is_permanent(pde)) {
const auto release = pde->proc_ops->proc_release;
if (release)
- return release(inode, file);
- return 0;
+ release(inode, file);
+ goto out;
}
spin_lock(&pde->pde_unload_lock);
list_for_each_entry(pdeo, &pde->pde_openers, lh) {
if (pdeo->file == file) {
close_pdeo(pde, pdeo);
- return 0;
+ goto out;
}
}
spin_unlock(&pde->pde_unload_lock);
+out:
+ /* ->proc_release() has run, here or in remove_proc_entry() */
+ if (pde_is_seq_file(pde))
+ pde_seq_release(pde, inode, file);
return 0;
}
+/* For seq_file entries, see pde_seq_ops. */
+static const struct file_operations proc_seq_file_ops = {
+ .llseek = seq_lseek,
+ .read_iter = seq_read_iter,
+ .splice_read = copy_splice_read,
+ .write = proc_reg_write,
+ .poll = proc_reg_poll,
+ .unlocked_ioctl = proc_reg_unlocked_ioctl,
+ .compat_ioctl = proc_reg_compat_ioctl,
+ .mmap = proc_reg_mmap,
+ .get_unmapped_area = proc_reg_get_unmapped_area,
+#ifndef CONFIG_MMU
+ .mmap_capabilities = proc_reg_mmap_capabilities,
+#endif
+ .open = proc_reg_open,
+ .release = proc_reg_release,
+};
+
static const struct file_operations proc_reg_file_ops = {
.llseek = proc_reg_llseek,
.read = proc_reg_read,
@@ -642,7 +779,9 @@ struct inode *proc_get_inode(struct super_block *sb, struct proc_dir_entry *de)
if (S_ISREG(inode->i_mode)) {
inode->i_op = de->proc_iops;
- if (pde_has_proc_read_iter(de))
+ if (pde_is_seq_file(de))
+ inode->i_fop = &proc_seq_file_ops;
+ else if (pde_has_proc_read_iter(de))
inode->i_fop = &proc_iter_file_ops;
else
inode->i_fop = &proc_reg_file_ops;
diff --git a/fs/proc/internal.h b/fs/proc/internal.h
index 41e00c978ddd..d8093451524a 100644
--- a/fs/proc/internal.h
+++ b/fs/proc/internal.h
@@ -97,6 +97,22 @@ static inline bool pde_has_proc_lseek(const struct proc_dir_entry *pde)
return pde->flags & PROC_ENTRY_proc_lseek;
}
+static inline bool pde_has_proc_seq_ops(const struct proc_dir_entry *pde)
+{
+ return pde->flags & PROC_ENTRY_proc_seq_ops;
+}
+
+static inline bool pde_has_proc_show(const struct proc_dir_entry *pde)
+{
+ return pde->flags & PROC_ENTRY_proc_show;
+}
+
+/* procfs owns the seq_file, see pde_seq_ops */
+static inline bool pde_is_seq_file(const struct proc_dir_entry *pde)
+{
+ return pde_has_proc_seq_ops(pde) || pde_has_proc_show(pde);
+}
+
extern struct kmem_cache *proc_dir_entry_cache;
void pde_free(struct proc_dir_entry *pde);
diff --git a/include/linux/proc_fs.h b/include/linux/proc_fs.h
index 6ad5150020bf..ea55ed85a2d6 100644
--- a/include/linux/proc_fs.h
+++ b/include/linux/proc_fs.h
@@ -27,6 +27,8 @@ enum {
PROC_ENTRY_proc_read_iter = 1U << 1,
PROC_ENTRY_proc_lseek = 1U << 3,
+ PROC_ENTRY_proc_seq_ops = 1U << 4,
+ PROC_ENTRY_proc_show = 1U << 5,
PROC_ENTRY_FORCE_LOOKUP = 1U << 7,
};
@@ -37,7 +39,10 @@ struct proc_ops {
ssize_t (*proc_read)(struct file *, char __user *, size_t, loff_t *);
ssize_t (*proc_read_iter)(struct kiocb *, struct iov_iter *);
ssize_t (*proc_write)(struct file *, const char __user *, size_t, loff_t *);
- /* mandatory unless nonseekable_open() or equivalent is used */
+ /*
+ * mandatory unless nonseekable_open() or equivalent is used, procfs
+ * seeks seq_file entries itself
+ */
loff_t (*proc_lseek)(struct file *, loff_t, int);
int (*proc_release)(struct inode *, struct file *);
__poll_t (*proc_poll)(struct file *, struct poll_table_struct *);
@@ -47,6 +52,21 @@ struct proc_ops {
#endif
int (*proc_mmap)(struct file *, struct vm_area_struct *);
unsigned long (*proc_get_unmapped_area)(struct file *, unsigned long, unsigned long, unsigned long, unsigned long);
+ /*
+ * For seq_file entries procfs opens the seq_file, reads and seeks
+ * through it and frees it once the file is released. ->proc_open()
+ * finds the seq_file in ->private_data and ->proc_release() only
+ * releases what ->proc_open() set up. It can run while a reader still
+ * copies out of the seq_file, so it must leave that alone. procfs
+ * allocates ->proc_state_size bytes of ->private state for
+ * ->proc_seq_ops and points ->private at the entry's data for
+ * ->proc_show(). Nothing that runs with the entry pinned may wait for
+ * something a copy to or from userspace can hold, like the seq_file's
+ * lock.
+ */
+ const struct seq_operations *proc_seq_ops;
+ int (*proc_show)(struct seq_file *, void *);
+ unsigned int proc_state_size;
} __randomize_layout;
/* definitions for hide_pid field */
--
2.53.0
[PATCH RFC 05/16] proc: let procfs own the seq_file of proc_create_seq() and friends
Author: Christian Brauner
Date: Mon, 02 Nov 2026 14:48
Date: Mon, 02 Nov 2026 14:48
220 lines
16619 bytes
16619 bytes
proc_create_seq_private(), proc_create_single_data() and their network
namespace variants keep the seq_operations or the show function in the
entry already. Let procfs own the seq_file for all of them. For the
network namespace variants ->proc_open() and ->proc_release() keep
taking and dropping the reference to the network namespace.
That's most of /proc/net, among them the files of xt_hashlimit and
/proc/net/dev_snmp6/<dev>. A reader that faults on its buffer no longer
holds up their removal.
Reported-by: Chris Mason <mason@kernel.org>
Fixes: 786d7e1612f0 ("Fix rmmod/read/write races in /proc entries")
Signed-off-by: Christian Brauner (Amutable) <brauner@kernel.org>
---
fs/proc/generic.c | 42 ++++--------------------------------------
fs/proc/proc_net.c | 40 +++++++++++++---------------------------
2 files changed, 17 insertions(+), 65 deletions(-)
diff --git a/fs/proc/generic.c b/fs/proc/generic.c
index eff5c38e2219..6ed8bc313db0 100644
--- a/fs/proc/generic.c
+++ b/fs/proc/generic.c
@@ -694,30 +694,9 @@ struct proc_dir_entry *proc_create(const char *name, umode_t mode,
}
EXPORT_SYMBOL(proc_create);
-static int proc_seq_open(struct inode *inode, struct file *file)
-{
- struct proc_dir_entry *de = PDE(inode);
-
- if (de->state_size)
- return seq_open_private(file, de->seq_ops, de->state_size);
- return seq_open(file, de->seq_ops);
-}
-
-static int proc_seq_release(struct inode *inode, struct file *file)
-{
- struct proc_dir_entry *de = PDE(inode);
-
- if (de->state_size)
- return seq_release_private(inode, file);
- return seq_release(inode, file);
-}
-
+/* procfs owns the seq_file of these, see pde_seq_ops */
static const struct proc_ops proc_seq_ops = {
/* not permanent -- can call into arbitrary seq_operations */
- .proc_open = proc_seq_open,
- .proc_read_iter = seq_read_iter,
- .proc_lseek = seq_lseek,
- .proc_release = proc_seq_release,
};
struct proc_dir_entry *proc_create_seq_private(const char *name, umode_t mode,
@@ -732,25 +711,11 @@ struct proc_dir_entry *proc_create_seq_private(const char *name, umode_t mode,
p->proc_ops = &proc_seq_ops;
p->seq_ops = ops;
p->state_size = state_size;
+ p->flags |= PROC_ENTRY_proc_seq_ops;
return proc_register(parent, p);
}
EXPORT_SYMBOL(proc_create_seq_private);
-static int proc_single_open(struct inode *inode, struct file *file)
-{
- struct proc_dir_entry *de = PDE(inode);
-
- return single_open(file, de->single_show, de->data);
-}
-
-static const struct proc_ops proc_single_ops = {
- /* not permanent -- can call into arbitrary ->single_show */
- .proc_open = proc_single_open,
- .proc_read_iter = seq_read_iter,
- .proc_lseek = seq_lseek,
- .proc_release = single_release,
-};
-
struct proc_dir_entry *proc_create_single_data(const char *name, umode_t mode,
struct proc_dir_entry *parent,
int (*show)(struct seq_file *, void *), void *data)
@@ -760,8 +725,9 @@ struct proc_dir_entry *proc_create_single_data(const char *name, umode_t mode,
p = proc_create_reg(name, mode, &parent, data);
if (!p)
return NULL;
- p->proc_ops = &proc_single_ops;
+ p->proc_ops = &proc_seq_ops;
p->single_show = show;
+ p->flags |= PROC_ENTRY_proc_show;
return proc_register(parent, p);
}
EXPORT_SYMBOL(proc_create_single_data);
diff --git a/fs/proc/proc_net.c b/fs/proc/proc_net.c
index df5484813d5d..fa485581d05e 100644
--- a/fs/proc/proc_net.c
+++ b/fs/proc/proc_net.c
@@ -37,13 +37,13 @@ static struct net *get_proc_net(const struct inode *inode)
return maybe_get_net(PDE_NET(PDE(inode)));
}
+/* procfs opened the seq_file and allocated its state, see pde_seq_ops */
static int seq_open_net(struct inode *inode, struct file *file)
{
- unsigned int state_size = PDE(inode)->state_size;
- struct seq_net_private *p;
+ struct seq_net_private *p = ((struct seq_file *)file->private_data)->private;
struct net *net;
- WARN_ON_ONCE(state_size < sizeof(*p));
+ WARN_ON_ONCE(PDE(inode)->state_size < sizeof(*p));
if (file->f_mode & FMODE_WRITE && !PDE(inode)->write)
return -EACCES;
@@ -51,12 +51,6 @@ static int seq_open_net(struct inode *inode, struct file *file)
net = get_proc_net(inode);
if (!net)
return -ENXIO;
-
- p = __seq_open_private(file, PDE(inode)->seq_ops, state_size);
- if (!p) {
- put_net(net);
- return -ENOMEM;
- }
#ifdef CONFIG_NET_NS
p->net = net;
netns_tracker_alloc(net, &p->ns_tracker, GFP_KERNEL);
@@ -77,18 +71,13 @@ static void seq_file_net_put_net(struct seq_file *seq)
static int seq_release_net(struct inode *ino, struct file *f)
{
- struct seq_file *seq = f->private_data;
-
- seq_file_net_put_net(seq);
- seq_release_private(ino, f);
+ seq_file_net_put_net(f->private_data);
return 0;
}
static const struct proc_ops proc_net_seq_ops = {
.proc_open = seq_open_net,
- .proc_read = seq_read,
.proc_write = proc_simple_write,
- .proc_lseek = seq_lseek,
.proc_release = seq_release_net,
};
@@ -125,6 +114,7 @@ struct proc_dir_entry *proc_create_net_data(const char *name, umode_t mode,
p->proc_ops = &proc_net_seq_ops;
p->seq_ops = ops;
p->state_size = state_size;
+ p->flags |= PROC_ENTRY_proc_seq_ops;
return proc_register(parent, p);
}
EXPORT_SYMBOL_GPL(proc_create_net_data);
@@ -170,6 +160,7 @@ struct proc_dir_entry *proc_create_net_data_write(const char *name, umode_t mode
p->proc_ops = &proc_net_seq_ops;
p->seq_ops = ops;
p->state_size = state_size;
+ p->flags |= PROC_ENTRY_proc_seq_ops;
p->write = write;
return proc_register(parent, p);
}
@@ -177,32 +168,25 @@ EXPORT_SYMBOL_GPL(proc_create_net_data_write);
static int single_open_net(struct inode *inode, struct file *file)
{
- struct proc_dir_entry *de = PDE(inode);
- struct net *net;
- int err;
+ struct net *net = get_proc_net(inode);
- net = get_proc_net(inode);
if (!net)
return -ENXIO;
-
- err = single_open(file, de->single_show, net);
- if (err)
- put_net(net);
- return err;
+ ((struct seq_file *)file->private_data)->private = net;
+ return 0;
}
static int single_release_net(struct inode *ino, struct file *f)
{
struct seq_file *seq = f->private_data;
+
put_net(seq->private);
- return single_release(ino, f);
+ return 0;
}
static const struct proc_ops proc_net_single_ops = {
.proc_open = single_open_net,
- .proc_read = seq_read,
.proc_write = proc_simple_write,
- .proc_lseek = seq_lseek,
.proc_release = single_release_net,
};
@@ -218,6 +202,7 @@ struct proc_dir_entry *proc_create_net_single(const char *name, umode_t mode,
pde_force_lookup(p);
p->proc_ops = &proc_net_single_ops;
p->single_show = show;
+ p->flags |= PROC_ENTRY_proc_show;
return proc_register(parent, p);
}
EXPORT_SYMBOL_GPL(proc_create_net_single);
@@ -261,6 +246,7 @@ struct proc_dir_entry *proc_create_net_single_write(const char *name, umode_t mo
pde_force_lookup(p);
p->proc_ops = &proc_net_single_ops;
p->single_show = show;
+ p->flags |= PROC_ENTRY_proc_show;
p->write = write;
return proc_register(parent, p);
}
--
2.53.0
[PATCH RFC 06/16] proc: let procfs own the seq_file of DEFINE_PROC_SHOW_ATTRIBUTE() files
Author: Christian Brauner
Date: Mon, 02 Nov 2026 14:48
Date: Mon, 02 Nov 2026 14:48
33 lines
10493 bytes
10493 bytes
Declare the show function and let procfs open the seq_file.
Signed-off-by: Christian Brauner (Amutable) <brauner@kernel.org>
---
include/linux/seq_file.h | 10 +---------
1 file changed, 1 insertion(+), 9 deletions(-)
diff --git a/include/linux/seq_file.h b/include/linux/seq_file.h
index dc0e8c62d9e0..b813b7e63d89 100644
--- a/include/linux/seq_file.h
+++ b/include/linux/seq_file.h
@@ -233,16 +233,8 @@ static const struct file_operations __name ## _fops = { \
}
#define DEFINE_PROC_SHOW_ATTRIBUTE(__name) \
-static int __name ## _open(struct inode *inode, struct file *file) \
-{ \
- return single_open(file, __name ## _show, pde_data(inode)); \
-} \
- \
static const struct proc_ops __name ## _proc_ops = { \
- .proc_open = __name ## _open, \
- .proc_read = seq_read, \
- .proc_lseek = seq_lseek, \
- .proc_release = single_release, \
+ .proc_show = __name ## _show, \
}
static inline struct user_namespace *seq_user_ns(struct seq_file *seq)
--
2.53.0
[PATCH RFC 07/16] treewide: let procfs own the seq_file of simple proc entries
Author: Christian Brauner
Date: Mon, 02 Nov 2026 14:48
Date: Mon, 02 Nov 2026 14:48
1560 lines
58684 bytes
58684 bytes
Most proc entries that bring their own struct proc_ops only call
single_open(), seq_open() or seq_open_private() in ->proc_open() and
free the seq_file with the matching helper. Declare the show function
or the seq_operations instead and let procfs open and free the
seq_file. Reading these files then no longer holds up their removal.
Done with the following semantic patch. The new initializers were
aligned like their neighbours afterwards:
// SPDX-License-Identifier: GPL-2.0-only
/// Let procfs open the seq_file of proc entries whose ->proc_open() only
/// calls single_open(), seq_open() or seq_open_private().
//
// Confidence: High
// Options: --no-includes --include-headers
virtual patch
@single depends on patch@
identifier open_fn, show_fn, i, f;
@@
static int open_fn(struct inode *i, struct file *f)
{
return single_open(f, show_fn, \(NULL\|pde_data(i)\));
}
@single_ops depends on single@
identifier single.open_fn, single.show_fn, ops;
symbol single_release;
@@
const struct proc_ops ops = {
- .proc_open = open_fn,
+ .proc_show = show_fn,
...,
- .proc_release = single_release,
...
};
@seq depends on patch@
identifier open_fn, sops, i, f;
@@
static int open_fn(struct inode *i, struct file *f)
{
return seq_open(f, &sops);
}
@seq_ops depends on seq@
identifier seq.open_fn, seq.sops, ops;
symbol seq_release;
@@
const struct proc_ops ops = {
- .proc_open = open_fn,
+ .proc_seq_ops = &sops,
...,
- .proc_release = seq_release,
...
};
@priv depends on patch@
identifier open_fn, sops, i, f;
expression size;
@@
static int open_fn(struct inode *i, struct file *f)
{
return seq_open_private(f, &sops, size);
}
@priv_ops depends on priv@
identifier priv.open_fn, priv.sops, ops;
expression priv.size;
symbol seq_release_private;
@@
const struct proc_ops ops = {
- .proc_open = open_fn,
+ .proc_seq_ops = &sops,
+ .proc_state_size = size,
...,
- .proc_release = seq_release_private,
...
};
// procfs reads and seeks through the seq_file itself
@depends on single_ops || seq_ops || priv_ops@
identifier ops = {single_ops.ops, seq_ops.ops, priv_ops.ops};
symbol seq_read_iter;
@@
const struct proc_ops ops = {
...,
- .proc_read_iter = seq_read_iter,
...
};
@depends on single_ops || seq_ops || priv_ops@
identifier ops = {single_ops.ops, seq_ops.ops, priv_ops.ops};
symbol seq_read;
@@
const struct proc_ops ops = {
...,
- .proc_read = seq_read,
...
};
@depends on single_ops || seq_ops || priv_ops@
identifier ops = {single_ops.ops, seq_ops.ops, priv_ops.ops};
symbol seq_lseek;
@@
const struct proc_ops ops = {
...,
- .proc_lseek = seq_lseek,
...
};
// drop the open function unless something else still uses it
@open_used@
identifier open_fn = {single.open_fn, seq.open_fn, priv.open_fn};
@@
open_fn
@depends on !open_used && (single_ops || seq_ops || priv_ops)@
identifier open_fn = {single.open_fn, seq.open_fn, priv.open_fn};
@@
- static int open_fn(...) { ... }
Signed-off-by: Christian Brauner (Amutable) <brauner@kernel.org>
---
arch/alpha/kernel/srm_env.c | 10 +--
arch/arm/mm/alignment.c | 10 +--
arch/powerpc/kernel/rtas-proc.c | 50 ++------------
arch/powerpc/platforms/pseries/lpar.c | 20 +-----
arch/powerpc/platforms/pseries/lparcfg.c | 10 +--
arch/sh/mm/alignment.c | 10 +--
arch/sparc/kernel/led.c | 10 +--
arch/um/kernel/exitcode.c | 10 +--
drivers/acpi/proc.c | 12 +---
drivers/hwmon/dell-smm-hwmon.c | 10 +--
drivers/input/input.c | 24 ++-----
drivers/macintosh/via-pmu.c | 10 +--
drivers/net/wireless/intel/ipw2x00/libipw_module.c | 10 +--
drivers/platform/x86/lenovo/thinkpad_acpi.c | 10 +--
drivers/platform/x86/toshiba_acpi.c | 40 ++---------
drivers/pnp/pnpbios/proc.c | 10 +--
drivers/s390/block/dasd_proc.c | 10 +--
drivers/s390/cio/blacklist.c | 13 +---
drivers/scsi/scsi_devinfo.c | 10 +--
drivers/scsi/scsi_proc.c | 21 +-----
drivers/usb/gadget/function/rndis.c | 10 +--
drivers/video/fbdev/via/viafbdev.c | 70 ++-----------------
fs/jfs/jfs_debug.c | 10 +--
fs/ntfs3/super.c | 20 +-----
fs/proc/cpuinfo.c | 10 +--
fs/smb/client/cifs_debug.c | 79 +++-------------------
fs/smb/client/dfs_cache.c | 10 +--
kernel/irq/proc.c | 30 +-------
kernel/latencytop.c | 10 +--
lib/dynamic_debug.c | 6 +-
mm/slab_common.c | 10 +--
net/core/pktgen.c | 30 +-------
net/sunrpc/stats.c | 10 +--
33 files changed, 64 insertions(+), 551 deletions(-)
diff --git a/arch/alpha/kernel/srm_env.c b/arch/alpha/kernel/srm_env.c
index 217b4dca51dd..faafc2df7886 100644
--- a/arch/alpha/kernel/srm_env.c
+++ b/arch/alpha/kernel/srm_env.c
@@ -81,11 +81,6 @@ static int srm_env_proc_show(struct seq_file *m, void *v)
return ret;
}
-static int srm_env_proc_open(struct inode *inode, struct file *file)
-{
- return single_open(file, srm_env_proc_show, pde_data(inode));
-}
-
static ssize_t srm_env_proc_write(struct file *file, const char __user *buffer,
size_t count, loff_t *pos)
{
@@ -120,10 +115,7 @@ static ssize_t srm_env_proc_write(struct file *file, const char __user *buffer,
}
static const struct proc_ops srm_env_proc_ops = {
- .proc_open = srm_env_proc_open,
- .proc_read = seq_read,
- .proc_lseek = seq_lseek,
- .proc_release = single_release,
+ .proc_show = srm_env_proc_show,
.proc_write = srm_env_proc_write,
};
diff --git a/arch/arm/mm/alignment.c b/arch/arm/mm/alignment.c
index f6c1e6b61179..3e5e30968634 100644
--- a/arch/arm/mm/alignment.c
+++ b/arch/arm/mm/alignment.c
@@ -144,11 +144,6 @@ static int alignment_proc_show(struct seq_file *m, void *v)
return 0;
}
-static int alignment_proc_open(struct inode *inode, struct file *file)
-{
- return single_open(file, alignment_proc_show, NULL);
-}
-
static ssize_t alignment_proc_write(struct file *file, const char __user *buffer,
size_t count, loff_t *pos)
{
@@ -164,10 +159,7 @@ static ssize_t alignment_proc_write(struct file *file, const char __user *buffer
}
static const struct proc_ops alignment_proc_ops = {
- .proc_open = alignment_proc_open,
- .proc_read = seq_read,
- .proc_lseek = seq_lseek,
- .proc_release = single_release,
+ .proc_show = alignment_proc_show,
.proc_write = alignment_proc_write,
};
#endif /* CONFIG_PROC_FS */
diff --git a/arch/powerpc/kernel/rtas-proc.c b/arch/powerpc/kernel/rtas-proc.c
index f38df72e64b8..ebf416840b3c 100644
--- a/arch/powerpc/kernel/rtas-proc.c
+++ b/arch/powerpc/kernel/rtas-proc.c
@@ -154,69 +154,29 @@ static ssize_t ppc_rtas_tone_volume_write(struct file *file,
static int ppc_rtas_tone_volume_show(struct seq_file *m, void *v);
static int ppc_rtas_rmo_buf_show(struct seq_file *m, void *v);
-static int poweron_open(struct inode *inode, struct file *file)
-{
- return single_open(file, ppc_rtas_poweron_show, NULL);
-}
-
static const struct proc_ops ppc_rtas_poweron_proc_ops = {
- .proc_open = poweron_open,
- .proc_read = seq_read,
- .proc_lseek = seq_lseek,
+ .proc_show = ppc_rtas_poweron_show,
.proc_write = ppc_rtas_poweron_write,
- .proc_release = single_release,
};
-static int progress_open(struct inode *inode, struct file *file)
-{
- return single_open(file, ppc_rtas_progress_show, NULL);
-}
-
static const struct proc_ops ppc_rtas_progress_proc_ops = {
- .proc_open = progress_open,
- .proc_read = seq_read,
- .proc_lseek = seq_lseek,
+ .proc_show = ppc_rtas_progress_show,
.proc_write = ppc_rtas_progress_write,
- .proc_release = single_release,
};
-static int clock_open(struct inode *inode, struct file *file)
-{
- return single_open(file, ppc_rtas_clock_show, NULL);
-}
-
static const struct proc_ops ppc_rtas_clock_proc_ops = {
- .proc_open = clock_open,
- .proc_read = seq_read,
- .proc_lseek = seq_lseek,
+ .proc_show = ppc_rtas_clock_show,
.proc_write = ppc_rtas_clock_write,
- .proc_release = single_release,
};
-static int tone_freq_open(struct inode *inode, struct file *file)
-{
- return single_open(file, ppc_rtas_tone_freq_show, NULL);
-}
-
static const struct proc_ops ppc_rtas_tone_freq_proc_ops = {
- .proc_open = tone_freq_open,
- .proc_read = seq_read,
- .proc_lseek = seq_lseek,
+ .proc_show = ppc_rtas_tone_freq_show,
.proc_write = ppc_rtas_tone_freq_write,
- .proc_release = single_release,
};
-static int tone_volume_open(struct inode *inode, struct file *file)
-{
- return single_open(file, ppc_rtas_tone_volume_show, NULL);
-}
-
static const struct proc_ops ppc_rtas_tone_volume_proc_ops = {
- .proc_open = tone_volume_open,
- .proc_read = seq_read,
- .proc_lseek = seq_lseek,
+ .proc_show = ppc_rtas_tone_volume_show,
.proc_write = ppc_rtas_tone_volume_write,
- .proc_release = single_release,
};
static int ppc_rtas_find_all_sensors(void);
diff --git a/arch/powerpc/platforms/pseries/lpar.c b/arch/powerpc/platforms/pseries/lpar.c
index 6a415febc53b..48fe0be71a89 100644
--- a/arch/powerpc/platforms/pseries/lpar.c
+++ b/arch/powerpc/platforms/pseries/lpar.c
@@ -584,17 +584,9 @@ static int vcpudispatch_stats_display(struct seq_file *p, void *v)
return 0;
}
-static int vcpudispatch_stats_open(struct inode *inode, struct file *file)
-{
- return single_open(file, vcpudispatch_stats_display, NULL);
-}
-
static const struct proc_ops vcpudispatch_stats_proc_ops = {
- .proc_open = vcpudispatch_stats_open,
- .proc_read = seq_read,
+ .proc_show = vcpudispatch_stats_display,
.proc_write = vcpudispatch_stats_write,
- .proc_lseek = seq_lseek,
- .proc_release = single_release,
};
static ssize_t vcpudispatch_stats_freq_write(struct file *file,
@@ -628,17 +620,9 @@ static int vcpudispatch_stats_freq_display(struct seq_file *p, void *v)
return 0;
}
-static int vcpudispatch_stats_freq_open(struct inode *inode, struct file *file)
-{
- return single_open(file, vcpudispatch_stats_freq_display, NULL);
-}
-
static const struct proc_ops vcpudispatch_stats_freq_proc_ops = {
- .proc_open = vcpudispatch_stats_freq_open,
- .proc_read = seq_read,
+ .proc_show = vcpudispatch_stats_freq_display,
.proc_write = vcpudispatch_stats_freq_write,
- .proc_lseek = seq_lseek,
- .proc_release = single_release,
};
static int __init vcpudispatch_stats_procfs_init(void)
diff --git a/arch/powerpc/platforms/pseries/lparcfg.c b/arch/powerpc/platforms/pseries/lparcfg.c
index aa9b5b141434..a6a137ce44ce 100644
--- a/arch/powerpc/platforms/pseries/lparcfg.c
+++ b/arch/powerpc/platforms/pseries/lparcfg.c
@@ -798,17 +798,9 @@ static int lparcfg_data(struct seq_file *m, void *v)
return pseries_lparcfg_data(m, v);
}
-static int lparcfg_open(struct inode *inode, struct file *file)
-{
- return single_open(file, lparcfg_data, NULL);
-}
-
static const struct proc_ops lparcfg_proc_ops = {
- .proc_read = seq_read,
.proc_write = lparcfg_write,
- .proc_open = lparcfg_open,
- .proc_release = single_release,
- .proc_lseek = seq_lseek,
+ .proc_show = lparcfg_data,
};
static int __init lparcfg_init(void)
diff --git a/arch/sh/mm/alignment.c b/arch/sh/mm/alignment.c
index 3a76a766f423..2198c6f2ea63 100644
--- a/arch/sh/mm/alignment.c
+++ b/arch/sh/mm/alignment.c
@@ -132,11 +132,6 @@ static int alignment_proc_show(struct seq_file *m, void *v)
return 0;
}
-static int alignment_proc_open(struct inode *inode, struct file *file)
-{
- return single_open(file, alignment_proc_show, NULL);
-}
-
static ssize_t alignment_proc_write(struct file *file,
const char __user *buffer, size_t count, loff_t *pos)
{
@@ -153,10 +148,7 @@ static ssize_t alignment_proc_write(struct file *file,
}
static const struct proc_ops alignment_proc_ops = {
- .proc_open = alignment_proc_open,
- .proc_read = seq_read,
- .proc_lseek = seq_lseek,
- .proc_release = single_release,
+ .proc_show = alignment_proc_show,
.proc_write = alignment_proc_write,
};
diff --git a/arch/sparc/kernel/led.c b/arch/sparc/kernel/led.c
index 9b53ac1fe533..12400bb59ab8 100644
--- a/arch/sparc/kernel/led.c
+++ b/arch/sparc/kernel/led.c
@@ -60,11 +60,6 @@ static int led_proc_show(struct seq_file *m, void *v)
return 0;
}
-static int led_proc_open(struct inode *inode, struct file *file)
-{
- return single_open(file, led_proc_show, NULL);
-}
-
static ssize_t led_proc_write(struct file *file, const char __user *buffer,
size_t count, loff_t *ppos)
{
@@ -106,10 +101,7 @@ static ssize_t led_proc_write(struct file *file, const char __user *buffer,
}
static const struct proc_ops led_proc_ops = {
- .proc_open = led_proc_open,
- .proc_read = seq_read,
- .proc_lseek = seq_lseek,
- .proc_release = single_release,
+ .proc_show = led_proc_show,
.proc_write = led_proc_write,
};
#endif
diff --git a/arch/um/kernel/exitcode.c b/arch/um/kernel/exitcode.c
index 0230354f4812..c71657df38f5 100644
--- a/arch/um/kernel/exitcode.c
+++ b/arch/um/kernel/exitcode.c
@@ -31,11 +31,6 @@ static int exitcode_proc_show(struct seq_file *m, void *v)
return 0;
}
-static int exitcode_proc_open(struct inode *inode, struct file *file)
-{
- return single_open(file, exitcode_proc_show, NULL);
-}
-
static ssize_t exitcode_proc_write(struct file *file,
const char __user *buffer, size_t count, loff_t *pos)
{
@@ -57,10 +52,7 @@ static ssize_t exitcode_proc_write(struct file *file,
}
static const struct proc_ops exitcode_proc_ops = {
- .proc_open = exitcode_proc_open,
- .proc_read = seq_read,
- .proc_lseek = seq_lseek,
- .proc_release = single_release,
+ .proc_show = exitcode_proc_show,
.proc_write = exitcode_proc_write,
};
diff --git a/drivers/acpi/proc.c b/drivers/acpi/proc.c
index c08ead07252b..b8a0bd4580e0 100644
--- a/drivers/acpi/proc.c
+++ b/drivers/acpi/proc.c
@@ -121,19 +121,9 @@ acpi_system_write_wakeup_device(struct file *file,
return count;
}
-static int
-acpi_system_wakeup_device_open_fs(struct inode *inode, struct file *file)
-{
- return single_open(file, acpi_system_wakeup_device_seq_show,
- pde_data(inode));
-}
-
static const struct proc_ops acpi_system_wakeup_device_proc_ops = {
- .proc_open = acpi_system_wakeup_device_open_fs,
- .proc_read = seq_read,
+ .proc_show = acpi_system_wakeup_device_seq_show,
.proc_write = acpi_system_write_wakeup_device,
- .proc_lseek = seq_lseek,
- .proc_release = single_release,
};
void __init acpi_sleep_proc_init(void)
diff --git a/drivers/hwmon/dell-smm-hwmon.c b/drivers/hwmon/dell-smm-hwmon.c
index 88837f028405..492b2b6c6d7e 100644
--- a/drivers/hwmon/dell-smm-hwmon.c
+++ b/drivers/hwmon/dell-smm-hwmon.c
@@ -694,16 +694,8 @@ static int i8k_proc_show(struct seq_file *seq, void *offset)
return 0;
}
-static int i8k_open_fs(struct inode *inode, struct file *file)
-{
- return single_open(file, i8k_proc_show, pde_data(inode));
-}
-
static const struct proc_ops i8k_proc_ops = {
- .proc_open = i8k_open_fs,
- .proc_read = seq_read,
- .proc_lseek = seq_lseek,
- .proc_release = single_release,
+ .proc_show = i8k_proc_show,
.proc_ioctl = i8k_ioctl,
};
diff --git a/drivers/input/input.c b/drivers/input/input.c
index 0859b60ca924..d27eecbf4a03 100644
--- a/drivers/input/input.c
+++ b/drivers/input/input.c
@@ -1218,18 +1218,10 @@ static const struct seq_operations input_devices_seq_ops = {
.show = input_devices_seq_show,
};
-static int input_proc_devices_open(struct inode *inode, struct file *file)
-{
- return seq_open_private(file, &input_devices_seq_ops,
- sizeof(struct input_seq_state));
-}
-
static const struct proc_ops input_devices_proc_ops = {
- .proc_open = input_proc_devices_open,
+ .proc_seq_ops = &input_devices_seq_ops,
+ .proc_state_size = sizeof(struct input_seq_state),
.proc_poll = input_proc_devices_poll,
- .proc_read = seq_read,
- .proc_lseek = seq_lseek,
- .proc_release = seq_release_private,
};
static void *input_handlers_seq_start(struct seq_file *seq, loff_t *pos)
@@ -1279,17 +1271,9 @@ static const struct seq_operations input_handlers_seq_ops = {
.show = input_handlers_seq_show,
};
-static int input_proc_handlers_open(struct inode *inode, struct file *file)
-{
- return seq_open_private(file, &input_handlers_seq_ops,
- sizeof(struct input_seq_state));
-}
-
static const struct proc_ops input_handlers_proc_ops = {
- .proc_open = input_proc_handlers_open,
- .proc_read = seq_read,
- .proc_lseek = seq_lseek,
- .proc_release = seq_release_private,
+ .proc_seq_ops = &input_handlers_seq_ops,
+ .proc_state_size = sizeof(struct input_seq_state),
};
static int __init input_proc_init(void)
diff --git a/drivers/macintosh/via-pmu.c b/drivers/macintosh/via-pmu.c
index 8c652c15018c..49f929ae10e4 100644
--- a/drivers/macintosh/via-pmu.c
+++ b/drivers/macintosh/via-pmu.c
@@ -912,11 +912,6 @@ static int pmu_options_proc_show(struct seq_file *m, void *v)
return 0;
}
-static int pmu_options_proc_open(struct inode *inode, struct file *file)
-{
- return single_open(file, pmu_options_proc_show, NULL);
-}
-
static ssize_t pmu_options_proc_write(struct file *file,
const char __user *buffer, size_t count, loff_t *pos)
{
@@ -962,10 +957,7 @@ static ssize_t pmu_options_proc_write(struct file *file,
}
static const struct proc_ops pmu_options_proc_ops = {
- .proc_open = pmu_options_proc_open,
- .proc_read = seq_read,
- .proc_lseek = seq_lseek,
- .proc_release = single_release,
+ .proc_show = pmu_options_proc_show,
.proc_write = pmu_options_proc_write,
};
#endif
diff --git a/drivers/net/wireless/intel/ipw2x00/libipw_module.c b/drivers/net/wireless/intel/ipw2x00/libipw_module.c
index 552c8ff3928b..15d9a5efb581 100644
--- a/drivers/net/wireless/intel/ipw2x00/libipw_module.c
+++ b/drivers/net/wireless/intel/ipw2x00/libipw_module.c
@@ -215,11 +215,6 @@ static int debug_level_proc_show(struct seq_file *m, void *v)
return 0;
}
-static int debug_level_proc_open(struct inode *inode, struct file *file)
-{
- return single_open(file, debug_level_proc_show, NULL);
-}
-
static ssize_t debug_level_proc_write(struct file *file,
const char __user *buffer, size_t count, loff_t *pos)
{
@@ -240,10 +235,7 @@ static ssize_t debug_level_proc_write(struct file *file,
}
static const struct proc_ops debug_level_proc_ops = {
- .proc_open = debug_level_proc_open,
- .proc_read = seq_read,
- .proc_lseek = seq_lseek,
- .proc_release = single_release,
+ .proc_show = debug_level_proc_show,
.proc_write = debug_level_proc_write,
};
#endif /* CONFIG_LIBIPW_DEBUG */
diff --git a/drivers/platform/x86/lenovo/thinkpad_acpi.c b/drivers/platform/x86/lenovo/thinkpad_acpi.c
index 1661f070c571..01e4c980fad6 100644
--- a/drivers/platform/x86/lenovo/thinkpad_acpi.c
+++ b/drivers/platform/x86/lenovo/thinkpad_acpi.c
@@ -880,11 +880,6 @@ static int dispatch_proc_show(struct seq_file *m, void *v)
return ibm->read(m);
}
-static int dispatch_proc_open(struct inode *inode, struct file *file)
-{
- return single_open(file, dispatch_proc_show, pde_data(inode));
-}
-
static ssize_t dispatch_proc_write(struct file *file,
const char __user *userbuf,
size_t count, loff_t *pos)
@@ -911,10 +906,7 @@ static ssize_t dispatch_proc_write(struct file *file,
}
static const struct proc_ops dispatch_proc_ops = {
- .proc_open = dispatch_proc_open,
- .proc_read = seq_read,
- .proc_lseek = seq_lseek,
- .proc_release = single_release,
+ .proc_show = dispatch_proc_show,
.proc_write = dispatch_proc_write,
};
diff --git a/drivers/platform/x86/toshiba_acpi.c b/drivers/platform/x86/toshiba_acpi.c
index 9eb8f1e5aeda..560e62da05a9 100644
--- a/drivers/platform/x86/toshiba_acpi.c
+++ b/drivers/platform/x86/toshiba_acpi.c
@@ -1448,11 +1448,6 @@ static int lcd_proc_show(struct seq_file *m, void *v)
return 0;
}
-static int lcd_proc_open(struct inode *inode, struct file *file)
-{
- return single_open(file, lcd_proc_show, pde_data(inode));
-}
-
static int set_lcd_brightness(struct toshiba_acpi_dev *dev, int value)
{
u32 result;
@@ -1509,10 +1504,7 @@ static ssize_t lcd_proc_write(struct file *file, const char __user *buf,
}
static const struct proc_ops lcd_proc_ops = {
- .proc_open = lcd_proc_open,
- .proc_read = seq_read,
- .proc_lseek = seq_lseek,
- .proc_release = single_release,
+ .proc_show = lcd_proc_show,
.proc_write = lcd_proc_write,
};
@@ -1549,11 +1541,6 @@ static int video_proc_show(struct seq_file *m, void *v)
return 0;
}
-static int video_proc_open(struct inode *inode, struct file *file)
-{
- return single_open(file, video_proc_show, pde_data(inode));
-}
-
static ssize_t video_proc_write(struct file *file, const char __user *buf,
size_t count, loff_t *pos)
{
@@ -1614,10 +1601,7 @@ static ssize_t video_proc_write(struct file *file, const char __user *buf,
}
static const struct proc_ops video_proc_ops = {
- .proc_open = video_proc_open,
- .proc_read = seq_read,
- .proc_lseek = seq_lseek,
- .proc_release = single_release,
+ .proc_show = video_proc_show,
.proc_write = video_proc_write,
};
@@ -1660,11 +1644,6 @@ static int fan_proc_show(struct seq_file *m, void *v)
return 0;
}
-static int fan_proc_open(struct inode *inode, struct file *file)
-{
- return single_open(file, fan_proc_show, pde_data(inode));
-}
-
static ssize_t fan_proc_write(struct file *file, const char __user *buf,
size_t count, loff_t *pos)
{
@@ -1691,10 +1670,7 @@ static ssize_t fan_proc_write(struct file *file, const char __user *buf,
}
static const struct proc_ops fan_proc_ops = {
- .proc_open = fan_proc_open,
- .proc_read = seq_read,
- .proc_lseek = seq_lseek,
- .proc_release = single_release,
+ .proc_show = fan_proc_show,
.proc_write = fan_proc_write,
};
@@ -1731,11 +1707,6 @@ static int keys_proc_show(struct seq_file *m, void *v)
return 0;
}
-static int keys_proc_open(struct inode *inode, struct file *file)
-{
- return single_open(file, keys_proc_show, pde_data(inode));
-}
-
static ssize_t keys_proc_write(struct file *file, const char __user *buf,
size_t count, loff_t *pos)
{
@@ -1758,10 +1729,7 @@ static ssize_t keys_proc_write(struct file *file, const char __user *buf,
}
static const struct proc_ops keys_proc_ops = {
- .proc_open = keys_proc_open,
- .proc_read = seq_read,
- .proc_lseek = seq_lseek,
- .proc_release = single_release,
+ .proc_show = keys_proc_show,
.proc_write = keys_proc_write,
};
diff --git a/drivers/pnp/pnpbios/proc.c b/drivers/pnp/pnpbios/proc.c
index 0f0d819b157f..14632a8d0da0 100644
--- a/drivers/pnp/pnpbios/proc.c
+++ b/drivers/pnp/pnpbios/proc.c
@@ -171,11 +171,6 @@ static int pnpbios_proc_show(struct seq_file *m, void *v)
return 0;
}
-static int pnpbios_proc_open(struct inode *inode, struct file *file)
-{
- return single_open(file, pnpbios_proc_show, pde_data(inode));
-}
-
static ssize_t pnpbios_proc_write(struct file *file, const char __user *buf,
size_t count, loff_t *pos)
{
@@ -211,10 +206,7 @@ out:
}
static const struct proc_ops pnpbios_proc_ops = {
- .proc_open = pnpbios_proc_open,
- .proc_read = seq_read,
- .proc_lseek = seq_lseek,
- .proc_release = single_release,
+ .proc_show = pnpbios_proc_show,
.proc_write = pnpbios_proc_write,
};
diff --git a/drivers/s390/block/dasd_proc.c b/drivers/s390/block/dasd_proc.c
index 48e12e81df00..86399427f01d 100644
--- a/drivers/s390/block/dasd_proc.c
+++ b/drivers/s390/block/dasd_proc.c
@@ -246,11 +246,6 @@ static int dasd_stats_proc_show(struct seq_file *m, void *v)
return 0;
}
-static int dasd_stats_proc_open(struct inode *inode, struct file *file)
-{
- return single_open(file, dasd_stats_proc_show, NULL);
-}
-
static ssize_t dasd_stats_proc_write(struct file *file,
const char __user *user_buf, size_t user_len, loff_t *pos)
{
@@ -316,10 +311,7 @@ out_error:
}
static const struct proc_ops dasd_stats_proc_ops = {
- .proc_open = dasd_stats_proc_open,
- .proc_read = seq_read,
- .proc_lseek = seq_lseek,
- .proc_release = single_release,
+ .proc_show = dasd_stats_proc_show,
.proc_write = dasd_stats_proc_write,
};
diff --git a/drivers/s390/cio/blacklist.c b/drivers/s390/cio/blacklist.c
index 020d210bde9f..4e02f78f6102 100644
--- a/drivers/s390/cio/blacklist.c
+++ b/drivers/s390/cio/blacklist.c
@@ -397,18 +397,9 @@ static const struct seq_operations cio_ignore_proc_seq_ops = {
.show = cio_ignore_proc_seq_show,
};
-static int
-cio_ignore_proc_open(struct inode *inode, struct file *file)
-{
- return seq_open_private(file, &cio_ignore_proc_seq_ops,
- sizeof(struct ccwdev_iter));
-}
-
static const struct proc_ops cio_ignore_proc_ops = {
- .proc_open = cio_ignore_proc_open,
- .proc_read = seq_read,
- .proc_lseek = seq_lseek,
- .proc_release = seq_release_private,
+ .proc_seq_ops = &cio_ignore_proc_seq_ops,
+ .proc_state_size = sizeof(struct ccwdev_iter),
.proc_write = cio_ignore_write,
};
diff --git a/drivers/scsi/scsi_devinfo.c b/drivers/scsi/scsi_devinfo.c
index cfc259fd0141..0b8f5ff40543 100644
--- a/drivers/scsi/scsi_devinfo.c
+++ b/drivers/scsi/scsi_devinfo.c
@@ -670,11 +670,6 @@ static const struct seq_operations scsi_devinfo_seq_ops = {
.show = devinfo_seq_show,
};
-static int proc_scsi_devinfo_open(struct inode *inode, struct file *file)
-{
- return seq_open(file, &scsi_devinfo_seq_ops);
-}
-
/*
* proc_scsi_dev_info_write - allow additions to scsi_dev_info_list via /proc.
*
@@ -714,11 +709,8 @@ out:
}
static const struct proc_ops scsi_devinfo_proc_ops = {
- .proc_open = proc_scsi_devinfo_open,
- .proc_read = seq_read,
+ .proc_seq_ops = &scsi_devinfo_seq_ops,
.proc_write = proc_scsi_devinfo_write,
- .proc_lseek = seq_lseek,
- .proc_release = seq_release,
};
#endif /* CONFIG_SCSI_PROC_FS */
diff --git a/drivers/scsi/scsi_proc.c b/drivers/scsi/scsi_proc.c
index dbd03360b5a4..3e49c4c8704f 100644
--- a/drivers/scsi/scsi_proc.c
+++ b/drivers/scsi/scsi_proc.c
@@ -521,28 +521,9 @@ static const struct seq_operations scsi_seq_ops = {
.show = scsi_seq_show
};
-/**
- * proc_scsi_open - glue function
- * @inode: not used
- * @file: passed to single_open()
- *
- * Associates proc_scsi_show with this file
- */
-static int proc_scsi_open(struct inode *inode, struct file *file)
-{
- /*
- * We don't really need this for the write case but it doesn't
- * harm either.
- */
- return seq_open(file, &scsi_seq_ops);
-}
-
static const struct proc_ops scsi_scsi_proc_ops = {
- .proc_open = proc_scsi_open,
- .proc_read = seq_read,
+ .proc_seq_ops = &scsi_seq_ops,
.proc_write = proc_scsi_write,
- .proc_lseek = seq_lseek,
- .proc_release = seq_release,
};
/**
diff --git a/drivers/usb/gadget/function/rndis.c b/drivers/usb/gadget/function/rndis.c
index a2fd239b7ad3..3c7bd30b8a3a 100644
--- a/drivers/usb/gadget/function/rndis.c
+++ b/drivers/usb/gadget/function/rndis.c
@@ -1183,16 +1183,8 @@ static ssize_t rndis_proc_write(struct file *file, const char __user *buffer,
return count;
}
-static int rndis_proc_open(struct inode *inode, struct file *file)
-{
- return single_open(file, rndis_proc_show, pde_data(inode));
-}
-
static const struct proc_ops rndis_proc_ops = {
- .proc_open = rndis_proc_open,
- .proc_read = seq_read,
- .proc_lseek = seq_lseek,
- .proc_release = single_release,
+ .proc_show = rndis_proc_show,
.proc_write = rndis_proc_write,
};
diff --git a/drivers/video/fbdev/via/viafbdev.c b/drivers/video/fbdev/via/viafbdev.c
index 2c67a18d16ad..4b617e80a77c 100644
--- a/drivers/video/fbdev/via/viafbdev.c
+++ b/drivers/video/fbdev/via/viafbdev.c
@@ -1120,11 +1120,6 @@ static int viafb_dvp0_proc_show(struct seq_file *m, void *v)
return 0;
}
-static int viafb_dvp0_proc_open(struct inode *inode, struct file *file)
-{
- return single_open(file, viafb_dvp0_proc_show, NULL);
-}
-
static ssize_t viafb_dvp0_proc_write(struct file *file,
const char __user *buffer, size_t count, loff_t *pos)
{
@@ -1173,10 +1168,7 @@ static ssize_t viafb_dvp0_proc_write(struct file *file,
}
static const struct proc_ops viafb_dvp0_proc_ops = {
- .proc_open = viafb_dvp0_proc_open,
- .proc_read = seq_read,
- .proc_lseek = seq_lseek,
- .proc_release = single_release,
+ .proc_show = viafb_dvp0_proc_show,
.proc_write = viafb_dvp0_proc_write,
};
@@ -1190,11 +1182,6 @@ static int viafb_dvp1_proc_show(struct seq_file *m, void *v)
return 0;
}
-static int viafb_dvp1_proc_open(struct inode *inode, struct file *file)
-{
- return single_open(file, viafb_dvp1_proc_show, NULL);
-}
-
static ssize_t viafb_dvp1_proc_write(struct file *file,
const char __user *buffer, size_t count, loff_t *pos)
{
@@ -1237,10 +1224,7 @@ static ssize_t viafb_dvp1_proc_write(struct file *file,
}
static const struct proc_ops viafb_dvp1_proc_ops = {
- .proc_open = viafb_dvp1_proc_open,
- .proc_read = seq_read,
- .proc_lseek = seq_lseek,
- .proc_release = single_release,
+ .proc_show = viafb_dvp1_proc_show,
.proc_write = viafb_dvp1_proc_write,
};
@@ -1252,11 +1236,6 @@ static int viafb_dfph_proc_show(struct seq_file *m, void *v)
return 0;
}
-static int viafb_dfph_proc_open(struct inode *inode, struct file *file)
-{
- return single_open(file, viafb_dfph_proc_show, NULL);
-}
-
static ssize_t viafb_dfph_proc_write(struct file *file,
const char __user *buffer, size_t count, loff_t *pos)
{
@@ -1271,10 +1250,7 @@ static ssize_t viafb_dfph_proc_write(struct file *file,
}
static const struct proc_ops viafb_dfph_proc_ops = {
- .proc_open = viafb_dfph_proc_open,
- .proc_read = seq_read,
- .proc_lseek = seq_lseek,
- .proc_release = single_release,
+ .proc_show = viafb_dfph_proc_show,
.proc_write = viafb_dfph_proc_write,
};
@@ -1286,11 +1262,6 @@ static int viafb_dfpl_proc_show(struct seq_file *m, void *v)
return 0;
}
-static int viafb_dfpl_proc_open(struct inode *inode, struct file *file)
-{
- return single_open(file, viafb_dfpl_proc_show, NULL);
-}
-
static ssize_t viafb_dfpl_proc_write(struct file *file,
const char __user *buffer, size_t count, loff_t *pos)
{
@@ -1305,10 +1276,7 @@ static ssize_t viafb_dfpl_proc_write(struct file *file,
}
static const struct proc_ops viafb_dfpl_proc_ops = {
- .proc_open = viafb_dfpl_proc_open,
- .proc_read = seq_read,
- .proc_lseek = seq_lseek,
- .proc_release = single_release,
+ .proc_show = viafb_dfpl_proc_show,
.proc_write = viafb_dfpl_proc_write,
};
@@ -1344,11 +1312,6 @@ static int viafb_vt1636_proc_show(struct seq_file *m, void *v)
return 0;
}
-static int viafb_vt1636_proc_open(struct inode *inode, struct file *file)
-{
- return single_open(file, viafb_vt1636_proc_show, NULL);
-}
-
static ssize_t viafb_vt1636_proc_write(struct file *file,
const char __user *buffer, size_t count, loff_t *pos)
{
@@ -1440,10 +1403,7 @@ static ssize_t viafb_vt1636_proc_write(struct file *file,
}
static const struct proc_ops viafb_vt1636_proc_ops = {
- .proc_open = viafb_vt1636_proc_open,
- .proc_read = seq_read,
- .proc_lseek = seq_lseek,
- .proc_release = single_release,
+ .proc_show = viafb_vt1636_proc_show,
.proc_write = viafb_vt1636_proc_write,
};
@@ -1491,11 +1451,6 @@ static int viafb_iga1_odev_proc_show(struct seq_file *m, void *v)
return 0;
}
-static int viafb_iga1_odev_proc_open(struct inode *inode, struct file *file)
-{
- return single_open(file, viafb_iga1_odev_proc_show, NULL);
-}
-
static ssize_t viafb_iga1_odev_proc_write(struct file *file,
const char __user *buffer, size_t count, loff_t *pos)
{
@@ -1517,10 +1472,7 @@ static ssize_t viafb_iga1_odev_proc_write(struct file *file,
}
static const struct proc_ops viafb_iga1_odev_proc_ops = {
- .proc_open = viafb_iga1_odev_proc_open,
- .proc_read = seq_read,
- .proc_lseek = seq_lseek,
- .proc_release = single_release,
+ .proc_show = viafb_iga1_odev_proc_show,
.proc_write = viafb_iga1_odev_proc_write,
};
@@ -1530,11 +1482,6 @@ static int viafb_iga2_odev_proc_show(struct seq_file *m, void *v)
return 0;
}
-static int viafb_iga2_odev_proc_open(struct inode *inode, struct file *file)
-{
- return single_open(file, viafb_iga2_odev_proc_show, NULL);
-}
-
static ssize_t viafb_iga2_odev_proc_write(struct file *file,
const char __user *buffer, size_t count, loff_t *pos)
{
@@ -1556,10 +1503,7 @@ static ssize_t viafb_iga2_odev_proc_write(struct file *file,
}
static const struct proc_ops viafb_iga2_odev_proc_ops = {
- .proc_open = viafb_iga2_odev_proc_open,
- .proc_read = seq_read,
- .proc_lseek = seq_lseek,
- .proc_release = single_release,
+ .proc_show = viafb_iga2_odev_proc_show,
.proc_write = viafb_iga2_odev_proc_write,
};
diff --git a/fs/jfs/jfs_debug.c b/fs/jfs/jfs_debug.c
index 44b62b3c322e..5eb35b0d420f 100644
--- a/fs/jfs/jfs_debug.c
+++ b/fs/jfs/jfs_debug.c
@@ -23,11 +23,6 @@ static int jfs_loglevel_proc_show(struct seq_file *m, void *v)
return 0;
}
-static int jfs_loglevel_proc_open(struct inode *inode, struct file *file)
-{
- return single_open(file, jfs_loglevel_proc_show, NULL);
-}
-
static ssize_t jfs_loglevel_proc_write(struct file *file,
const char __user *buffer, size_t count, loff_t *ppos)
{
@@ -44,10 +39,7 @@ static ssize_t jfs_loglevel_proc_write(struct file *file,
}
static const struct proc_ops jfs_loglevel_proc_ops = {
- .proc_open = jfs_loglevel_proc_open,
- .proc_read = seq_read,
- .proc_lseek = seq_lseek,
- .proc_release = single_release,
+ .proc_show = jfs_loglevel_proc_show,
.proc_write = jfs_loglevel_proc_write,
};
#endif
diff --git a/fs/ntfs3/super.c b/fs/ntfs3/super.c
index b4479cd6bd65..3d669e957ea4 100644
--- a/fs/ntfs3/super.c
+++ b/fs/ntfs3/super.c
@@ -524,11 +524,6 @@ static int ntfs3_volinfo(struct seq_file *m, void *o)
return 0;
}
-static int ntfs3_volinfo_open(struct inode *inode, struct file *file)
-{
- return single_open(file, ntfs3_volinfo, pde_data(inode));
-}
-
/* read /proc/fs/ntfs3/<dev>/label */
static int ntfs3_label_show(struct seq_file *m, void *o)
{
@@ -574,23 +569,12 @@ out:
return ret;
}
-static int ntfs3_label_open(struct inode *inode, struct file *file)
-{
- return single_open(file, ntfs3_label_show, pde_data(inode));
-}
-
static const struct proc_ops ntfs3_volinfo_fops = {
- .proc_read = seq_read,
- .proc_lseek = seq_lseek,
- .proc_release = single_release,
- .proc_open = ntfs3_volinfo_open,
+ .proc_show = ntfs3_volinfo,
};
static const struct proc_ops ntfs3_label_fops = {
- .proc_read = seq_read,
- .proc_lseek = seq_lseek,
- .proc_release = single_release,
- .proc_open = ntfs3_label_open,
+ .proc_show = ntfs3_label_show,
.proc_write = ntfs3_label_write,
};
diff --git a/fs/proc/cpuinfo.c b/fs/proc/cpuinfo.c
index f38bda5b83ec..d8cd718c6030 100644
--- a/fs/proc/cpuinfo.c
+++ b/fs/proc/cpuinfo.c
@@ -7,17 +7,9 @@
extern const struct seq_operations cpuinfo_op;
-static int cpuinfo_open(struct inode *inode, struct file *file)
-{
- return seq_open(file, &cpuinfo_op);
-}
-
static const struct proc_ops cpuinfo_proc_ops = {
.proc_flags = PROC_ENTRY_PERMANENT,
- .proc_open = cpuinfo_open,
- .proc_read_iter = seq_read_iter,
- .proc_lseek = seq_lseek,
- .proc_release = seq_release,
+ .proc_seq_ops = &cpuinfo_op,
};
static int __init proc_cpuinfo_init(void)
diff --git a/fs/smb/client/cifs_debug.c b/fs/smb/client/cifs_debug.c
index 577f0ddede90..dfcd2cdbb2a8 100644
--- a/fs/smb/client/cifs_debug.c
+++ b/fs/smb/client/cifs_debug.c
@@ -357,10 +357,6 @@ static int cifs_debug_dirs_proc_show(struct seq_file *m, void *v)
}
#ifdef CONFIG_CIFS_DEBUG
-static int cifs_debug_dirs_proc_open(struct inode *inode, struct file *file)
-{
- return single_open(file, cifs_debug_dirs_proc_show, NULL);
-}
/* Drop all cached directory entries across all CIFS mounts. */
static ssize_t cifs_debug_dirs_proc_write(struct file *file, const char __user *buffer,
@@ -393,10 +389,7 @@ static ssize_t cifs_debug_dirs_proc_write(struct file *file, const char __user *
}
static const struct proc_ops cifs_debug_dirs_proc_ops = {
- .proc_open = cifs_debug_dirs_proc_open,
- .proc_read = seq_read,
- .proc_lseek = seq_lseek,
- .proc_release = single_release,
+ .proc_show = cifs_debug_dirs_proc_show,
.proc_write = cifs_debug_dirs_proc_write,
};
#endif /* CONFIG_CIFS_DEBUG */
@@ -859,16 +852,8 @@ static int cifs_stats_proc_show(struct seq_file *m, void *v)
return 0;
}
-static int cifs_stats_proc_open(struct inode *inode, struct file *file)
-{
- return single_open(file, cifs_stats_proc_show, NULL);
-}
-
static const struct proc_ops cifs_stats_proc_ops = {
- .proc_open = cifs_stats_proc_open,
- .proc_read = seq_read,
- .proc_lseek = seq_lseek,
- .proc_release = single_release,
+ .proc_show = cifs_stats_proc_show,
.proc_write = cifs_stats_proc_write,
};
@@ -1012,11 +997,6 @@ static int cifsFYI_proc_show(struct seq_file *m, void *v)
return 0;
}
-static int cifsFYI_proc_open(struct inode *inode, struct file *file)
-{
- return single_open(file, cifsFYI_proc_show, NULL);
-}
-
static ssize_t cifsFYI_proc_write(struct file *file, const char __user *buffer,
size_t count, loff_t *ppos)
{
@@ -1038,10 +1018,7 @@ static ssize_t cifsFYI_proc_write(struct file *file, const char __user *buffer,
}
static const struct proc_ops cifsFYI_proc_ops = {
- .proc_open = cifsFYI_proc_open,
- .proc_read = seq_read,
- .proc_lseek = seq_lseek,
- .proc_release = single_release,
+ .proc_show = cifsFYI_proc_show,
.proc_write = cifsFYI_proc_write,
};
@@ -1051,11 +1028,6 @@ static int cifs_linux_ext_proc_show(struct seq_file *m, void *v)
return 0;
}
-static int cifs_linux_ext_proc_open(struct inode *inode, struct file *file)
-{
- return single_open(file, cifs_linux_ext_proc_show, NULL);
-}
-
static ssize_t cifs_linux_ext_proc_write(struct file *file,
const char __user *buffer, size_t count, loff_t *ppos)
{
@@ -1069,10 +1041,7 @@ static ssize_t cifs_linux_ext_proc_write(struct file *file,
}
static const struct proc_ops cifs_linux_ext_proc_ops = {
- .proc_open = cifs_linux_ext_proc_open,
- .proc_read = seq_read,
- .proc_lseek = seq_lseek,
- .proc_release = single_release,
+ .proc_show = cifs_linux_ext_proc_show,
.proc_write = cifs_linux_ext_proc_write,
};
@@ -1082,11 +1051,6 @@ static int cifs_lookup_cache_proc_show(struct seq_file *m, void *v)
return 0;
}
-static int cifs_lookup_cache_proc_open(struct inode *inode, struct file *file)
-{
- return single_open(file, cifs_lookup_cache_proc_show, NULL);
-}
-
static ssize_t cifs_lookup_cache_proc_write(struct file *file,
const char __user *buffer, size_t count, loff_t *ppos)
{
@@ -1100,10 +1064,7 @@ static ssize_t cifs_lookup_cache_proc_write(struct file *file,
}
static const struct proc_ops cifs_lookup_cache_proc_ops = {
- .proc_open = cifs_lookup_cache_proc_open,
- .proc_read = seq_read,
- .proc_lseek = seq_lseek,
- .proc_release = single_release,
+ .proc_show = cifs_lookup_cache_proc_show,
.proc_write = cifs_lookup_cache_proc_write,
};
@@ -1113,11 +1074,6 @@ static int traceSMB_proc_show(struct seq_file *m, void *v)
return 0;
}
-static int traceSMB_proc_open(struct inode *inode, struct file *file)
-{
- return single_open(file, traceSMB_proc_show, NULL);
-}
-
static ssize_t traceSMB_proc_write(struct file *file, const char __user *buffer,
size_t count, loff_t *ppos)
{
@@ -1131,10 +1087,7 @@ static ssize_t traceSMB_proc_write(struct file *file, const char __user *buffer,
}
static const struct proc_ops traceSMB_proc_ops = {
- .proc_open = traceSMB_proc_open,
- .proc_read = seq_read,
- .proc_lseek = seq_lseek,
- .proc_release = single_release,
+ .proc_show = traceSMB_proc_show,
.proc_write = traceSMB_proc_write,
};
@@ -1144,11 +1097,6 @@ static int cifs_security_flags_proc_show(struct seq_file *m, void *v)
return 0;
}
-static int cifs_security_flags_proc_open(struct inode *inode, struct file *file)
-{
- return single_open(file, cifs_security_flags_proc_show, NULL);
-}
-
/*
* Ensure that if someone sets a MUST flag, that we disable all other MAY
* flags except for the ones corresponding to the given MUST flag. If there are
@@ -1234,10 +1182,7 @@ static ssize_t cifs_security_flags_proc_write(struct file *file,
}
static const struct proc_ops cifs_security_flags_proc_ops = {
- .proc_open = cifs_security_flags_proc_open,
- .proc_read = seq_read,
- .proc_lseek = seq_lseek,
- .proc_release = single_release,
+ .proc_show = cifs_security_flags_proc_show,
.proc_write = cifs_security_flags_proc_write,
};
@@ -1271,16 +1216,8 @@ static int cifs_mount_params_proc_show(struct seq_file *m, void *v)
return 0;
}
-static int cifs_mount_params_proc_open(struct inode *inode, struct file *file)
-{
- return single_open(file, cifs_mount_params_proc_show, NULL);
-}
-
static const struct proc_ops cifs_mount_params_proc_ops = {
- .proc_open = cifs_mount_params_proc_open,
- .proc_read = seq_read,
- .proc_lseek = seq_lseek,
- .proc_release = single_release,
+ .proc_show = cifs_mount_params_proc_show,
/* No need for write for now */
/* .proc_write = cifs_mount_params_proc_write, */
};
diff --git a/fs/smb/client/dfs_cache.c b/fs/smb/client/dfs_cache.c
index 26956e33037e..58f16cf83d36 100644
--- a/fs/smb/client/dfs_cache.c
+++ b/fs/smb/client/dfs_cache.c
@@ -213,16 +213,8 @@ static ssize_t dfscache_proc_write(struct file *file, const char __user *buffer,
return count;
}
-static int dfscache_proc_open(struct inode *inode, struct file *file)
-{
- return single_open(file, dfscache_proc_show, NULL);
-}
-
const struct proc_ops dfscache_proc_ops = {
- .proc_open = dfscache_proc_open,
- .proc_read = seq_read,
- .proc_lseek = seq_lseek,
- .proc_release = single_release,
+ .proc_show = dfscache_proc_show,
.proc_write = dfscache_proc_write,
};
diff --git a/kernel/irq/proc.c b/kernel/irq/proc.c
index 0c0df16de581..aa3d5544cfc1 100644
--- a/kernel/irq/proc.c
+++ b/kernel/irq/proc.c
@@ -188,29 +188,13 @@ static ssize_t irq_affinity_list_proc_write(struct file *file,
return write_irq_affinity(1, file, buffer, count, pos);
}
-static int irq_affinity_proc_open(struct inode *inode, struct file *file)
-{
- return single_open(file, irq_affinity_proc_show, pde_data(inode));
-}
-
-static int irq_affinity_list_proc_open(struct inode *inode, struct file *file)
-{
- return single_open(file, irq_affinity_list_proc_show, pde_data(inode));
-}
-
static const struct proc_ops irq_affinity_proc_ops = {
- .proc_open = irq_affinity_proc_open,
- .proc_read = seq_read,
- .proc_lseek = seq_lseek,
- .proc_release = single_release,
+ .proc_show = irq_affinity_proc_show,
.proc_write = irq_affinity_proc_write,
};
static const struct proc_ops irq_affinity_list_proc_ops = {
- .proc_open = irq_affinity_list_proc_open,
- .proc_read = seq_read,
- .proc_lseek = seq_lseek,
- .proc_release = single_release,
+ .proc_show = irq_affinity_list_proc_show,
.proc_write = irq_affinity_list_proc_write,
};
@@ -263,16 +247,8 @@ out:
return err;
}
-static int default_affinity_open(struct inode *inode, struct file *file)
-{
- return single_open(file, default_affinity_show, pde_data(inode));
-}
-
static const struct proc_ops default_affinity_proc_ops = {
- .proc_open = default_affinity_open,
- .proc_read = seq_read,
- .proc_lseek = seq_lseek,
- .proc_release = single_release,
+ .proc_show = default_affinity_show,
.proc_write = default_affinity_write,
};
diff --git a/kernel/latencytop.c b/kernel/latencytop.c
index d4281d1e13a6..b764f47606d3 100644
--- a/kernel/latencytop.c
+++ b/kernel/latencytop.c
@@ -275,17 +275,9 @@ lstats_write(struct file *file, const char __user *buf, size_t count,
return count;
}
-static int lstats_open(struct inode *inode, struct file *filp)
-{
- return single_open(filp, lstats_show, NULL);
-}
-
static const struct proc_ops lstats_proc_ops = {
- .proc_open = lstats_open,
- .proc_read = seq_read,
+ .proc_show = lstats_show,
.proc_write = lstats_write,
- .proc_lseek = seq_lseek,
- .proc_release = single_release,
};
static int __init init_lstats_procfs(void)
diff --git a/lib/dynamic_debug.c b/lib/dynamic_debug.c
index 18a71a9108d3..64a211290872 100644
--- a/lib/dynamic_debug.c
+++ b/lib/dynamic_debug.c
@@ -1194,10 +1194,8 @@ static const struct file_operations ddebug_proc_fops = {
};
static const struct proc_ops proc_fops = {
- .proc_open = ddebug_proc_open,
- .proc_read = seq_read,
- .proc_lseek = seq_lseek,
- .proc_release = seq_release_private,
+ .proc_seq_ops = &ddebug_proc_seqops,
+ .proc_state_size = sizeof(struct ddebug_iter),
.proc_write = ddebug_proc_write
};
diff --git a/mm/slab_common.c b/mm/slab_common.c
index 01c64ef661e7..fbf303b45858 100644
--- a/mm/slab_common.c
+++ b/mm/slab_common.c
@@ -1233,17 +1233,9 @@ static const struct seq_operations slabinfo_op = {
.show = slab_show,
};
-static int slabinfo_open(struct inode *inode, struct file *file)
-{
- return seq_open(file, &slabinfo_op);
-}
-
static const struct proc_ops slabinfo_proc_ops = {
.proc_flags = PROC_ENTRY_PERMANENT,
- .proc_open = slabinfo_open,
- .proc_read = seq_read,
- .proc_lseek = seq_lseek,
- .proc_release = seq_release,
+ .proc_seq_ops = &slabinfo_op,
};
static int __init slab_proc_init(void)
diff --git a/net/core/pktgen.c b/net/core/pktgen.c
index 02af70d360bc..12363a373fb8 100644
--- a/net/core/pktgen.c
+++ b/net/core/pktgen.c
@@ -550,17 +550,9 @@ static ssize_t pgctrl_write(struct file *file, const char __user *buf,
return count;
}
-static int pgctrl_open(struct inode *inode, struct file *file)
-{
- return single_open(file, pgctrl_show, pde_data(inode));
-}
-
static const struct proc_ops pktgen_proc_ops = {
- .proc_open = pgctrl_open,
- .proc_read = seq_read,
- .proc_lseek = seq_lseek,
+ .proc_show = pgctrl_show,
.proc_write = pgctrl_write,
- .proc_release = single_release,
};
static int pktgen_if_show(struct seq_file *seq, void *v)
@@ -1870,17 +1862,9 @@ static ssize_t pktgen_if_write(struct file *file,
return -EINVAL;
}
-static int pktgen_if_open(struct inode *inode, struct file *file)
-{
- return single_open(file, pktgen_if_show, pde_data(inode));
-}
-
static const struct proc_ops pktgen_if_proc_ops = {
- .proc_open = pktgen_if_open,
- .proc_read = seq_read,
- .proc_lseek = seq_lseek,
+ .proc_show = pktgen_if_show,
.proc_write = pktgen_if_write,
- .proc_release = single_release,
};
static int pktgen_thread_show(struct seq_file *seq, void *v)
@@ -2010,17 +1994,9 @@ out:
return ret;
}
-static int pktgen_thread_open(struct inode *inode, struct file *file)
-{
- return single_open(file, pktgen_thread_show, pde_data(inode));
-}
-
static const struct proc_ops pktgen_thread_proc_ops = {
- .proc_open = pktgen_thread_open,
- .proc_read = seq_read,
- .proc_lseek = seq_lseek,
+ .proc_show = pktgen_thread_show,
.proc_write = pktgen_thread_write,
- .proc_release = single_release,
};
/* Think find or remove for NN */
diff --git a/net/sunrpc/stats.c b/net/sunrpc/stats.c
index d08711bee18e..f5ee18a0eb37 100644
--- a/net/sunrpc/stats.c
+++ b/net/sunrpc/stats.c
@@ -64,16 +64,8 @@ static int rpc_proc_show(struct seq_file *seq, void *v) {
return 0;
}
-static int rpc_proc_open(struct inode *inode, struct file *file)
-{
- return single_open(file, rpc_proc_show, pde_data(inode));
-}
-
static const struct proc_ops rpc_proc_ops = {
- .proc_open = rpc_proc_open,
- .proc_read = seq_read,
- .proc_lseek = seq_lseek,
- .proc_release = single_release,
+ .proc_show = rpc_proc_show,
};
/*
--
2.53.0
[PATCH RFC 09/16] seq_file: add seq_preallocate()
Author: Christian Brauner
Date: Mon, 02 Nov 2026 14:48
Date: Mon, 02 Nov 2026 14:48
83 lines
12150 bytes
12150 bytes
single_open_size() allocates the buffer itself because it does the
single_open() too. A file whose seq_file is opened by somebody else,
like procfs does now, needs to do that after the fact. Add
seq_preallocate() for that and use it in single_open_size().
No functional changes.
Signed-off-by: Christian Brauner (Amutable) <brauner@kernel.org>
---
fs/seq_file.c | 36 ++++++++++++++++++++++++++++--------
include/linux/seq_file.h | 1 +
2 files changed, 29 insertions(+), 8 deletions(-)
diff --git a/fs/seq_file.c b/fs/seq_file.c
index 456c78719fd0..a4644cdda7e6 100644
--- a/fs/seq_file.c
+++ b/fs/seq_file.c
@@ -590,21 +590,41 @@ int single_open(struct file *file, int (*show)(struct seq_file *, void *),
}
EXPORT_SYMBOL(single_open);
-int single_open_size(struct file *file, int (*show)(struct seq_file *, void *),
- void *data, size_t size)
+/**
+ * seq_preallocate - start out with a buffer of @size
+ * @m: the seq_file, before it is first read
+ * @size: the size of the buffer
+ *
+ * For files whose records don't fit into a page, so that the first read
+ * doesn't have to work its way up to the right buffer size.
+ */
+int seq_preallocate(struct seq_file *m, size_t size)
{
char *buf = seq_buf_alloc(size);
- int ret;
+
if (!buf)
return -ENOMEM;
+ kvfree(m->buf);
+ m->buf = buf;
+ m->size = size;
+ return 0;
+}
+EXPORT_SYMBOL(seq_preallocate);
+
+int single_open_size(struct file *file, int (*show)(struct seq_file *, void *),
+ void *data, size_t size)
+{
+ int ret;
+
ret = single_open(file, show, data);
- if (ret) {
- kvfree(buf);
+ if (ret)
return ret;
+ ret = seq_preallocate(file->private_data, size);
+ if (ret) {
+ single_release(file_inode(file), file);
+ file->private_data = NULL;
}
- ((struct seq_file *)file->private_data)->buf = buf;
- ((struct seq_file *)file->private_data)->size = size;
- return 0;
+ return ret;
}
EXPORT_SYMBOL(single_open_size);
diff --git a/include/linux/seq_file.h b/include/linux/seq_file.h
index b813b7e63d89..834daff48b7d 100644
--- a/include/linux/seq_file.h
+++ b/include/linux/seq_file.h
@@ -175,6 +175,7 @@ int seq_path_root(struct seq_file *m, const struct path *path,
void *single_start(struct seq_file *, loff_t *);
int single_open(struct file *, int (*)(struct seq_file *, void *), void *);
int single_open_size(struct file *, int (*)(struct seq_file *, void *), void *, size_t);
+int seq_preallocate(struct seq_file *m, size_t size);
int single_release(struct inode *, struct file *);
void *__seq_open_private(struct file *, const struct seq_operations *, int);
int seq_open_private(struct file *, const struct seq_operations *, int);
--
2.53.0
[PATCH RFC 11/16] proc: let procfs copy in writes before pinning the entry
Author: Christian Brauner
Date: Mon, 02 Nov 2026 14:48
Date: Mon, 02 Nov 2026 14:48
176 lines
15437 bytes
15437 bytes
proc_reg_write() pins the entry and calls ->proc_write() which copies
from userspace itself. A page fault in that copy can take as long as
the writer wants, just like the copy to userspace of a read, and
remove_proc_entry() waits for it.
So do what kernfs does for writes as well. An entry that implements
->proc_write_buf() gets a NUL-terminated kernel copy of what was
written. procfs makes that copy before it pins the entry. The copy is
capped at ->proc_write_size bytes, or at PAGE_SIZE - 1 like
proc_simple_write() if the entry doesn't care. Larger writes fail with
-EINVAL. The proc_ops go away with the entry, so procfs remembers the
limit in the entry when it is registered, like the size of the seq_file
state, instead of pinning the entry just to look up a number.
Entries that take binary data in pieces, like the PCI config space,
ALSA's data entries or a firmware image, can ask for
PROC_ENTRY_SHORT_WRITES instead. procfs then copies in the first
->proc_write_size bytes and the write comes up short.
->proc_write_buf() gets the position like ->proc_write() so entries
that write at an offset keep working.
Note that procfs copies in the whole write before the entry looks at any
of it. So a write that is too large or whose buffer can't be read fails
before the entry's own checks run, permission checks included, even
where the entry only ever looked at the first few bytes.
An entry that implements ->proc_write_buf() and still has a
->proc_write() fails to register.
No users yet.
Signed-off-by: Christian Brauner (Amutable) <brauner@kernel.org>
---
fs/proc/generic.c | 8 ++++++++
fs/proc/inode.c | 33 +++++++++++++++++++++++++++++++++
fs/proc/internal.h | 11 +++++++++++
include/linux/proc_fs.h | 17 +++++++++++++++++
4 files changed, 69 insertions(+)
diff --git a/fs/proc/generic.c b/fs/proc/generic.c
index 6ed8bc313db0..7dc7df101286 100644
--- a/fs/proc/generic.c
+++ b/fs/proc/generic.c
@@ -468,6 +468,14 @@ static int pde_set_flags(struct proc_dir_entry *pde)
if (proc_ops->proc_lseek)
pde->flags |= PROC_ENTRY_proc_lseek;
}
+ if (proc_ops->proc_write_buf) {
+ if (WARN_ON_ONCE(proc_ops->proc_write))
+ return -EINVAL;
+ pde->flags |= PROC_ENTRY_proc_write_buf;
+ pde->write_size = proc_ops->proc_write_size ?: PAGE_SIZE - 1;
+ if (proc_ops->proc_flags & PROC_ENTRY_SHORT_WRITES)
+ pde->flags |= PROC_ENTRY_SHORT_WRITES;
+ }
return 0;
}
diff --git a/fs/proc/inode.c b/fs/proc/inode.c
index 8425465aae5d..c0bb40e7ff96 100644
--- a/fs/proc/inode.c
+++ b/fs/proc/inode.c
@@ -440,11 +440,44 @@ static ssize_t pde_write(struct proc_dir_entry *pde, struct file *file, const ch
return -EIO;
}
+/*
+ * procfs copies in what is written before it pins the entry. A writer that
+ * faults on its buffer then doesn't hold up remove_proc_entry().
+ */
+static ssize_t pde_write_buf(struct proc_dir_entry *pde, struct file *file,
+ const char __user *ubuf, size_t count, loff_t *ppos)
+{
+ char *buf __free(kvfree) = NULL;
+ size_t size;
+ ssize_t rv;
+
+ size = pde->write_size;
+ if (count > size) {
+ if (!pde_has_short_writes(pde))
+ return -EINVAL;
+ count = size;
+ }
+ buf = kvmalloc(count + 1, GFP_KERNEL);
+ if (!buf)
+ return -ENOMEM;
+ if (copy_from_user(buf, ubuf, count))
+ return -EFAULT;
+ buf[count] = '\0';
+
+ if (!pde_pin(pde))
+ return -EIO;
+ rv = pde->proc_ops->proc_write_buf(file, buf, count, ppos);
+ pde_unpin(pde);
+ return rv;
+}
+
static ssize_t proc_reg_write(struct file *file, const char __user *buf, size_t count, loff_t *ppos)
{
struct proc_dir_entry *pde = PDE(file_inode(file));
ssize_t rv;
+ if (pde_has_proc_write_buf(pde))
+ return pde_write_buf(pde, file, buf, count, ppos);
if (!pde_pin(pde))
return -EIO;
rv = pde_write(pde, file, buf, count, ppos);
diff --git a/fs/proc/internal.h b/fs/proc/internal.h
index d8093451524a..80b346a3299d 100644
--- a/fs/proc/internal.h
+++ b/fs/proc/internal.h
@@ -51,6 +51,7 @@ struct proc_dir_entry {
proc_write_t write;
void *data;
unsigned int state_size;
+ unsigned int write_size;
unsigned int low_ino;
nlink_t nlink;
kuid_t uid;
@@ -107,6 +108,16 @@ static inline bool pde_has_proc_show(const struct proc_dir_entry *pde)
return pde->flags & PROC_ENTRY_proc_show;
}
+static inline bool pde_has_proc_write_buf(const struct proc_dir_entry *pde)
+{
+ return pde->flags & PROC_ENTRY_proc_write_buf;
+}
+
+static inline bool pde_has_short_writes(const struct proc_dir_entry *pde)
+{
+ return pde->flags & PROC_ENTRY_SHORT_WRITES;
+}
+
/* procfs owns the seq_file, see pde_seq_ops */
static inline bool pde_is_seq_file(const struct proc_dir_entry *pde)
{
diff --git a/include/linux/proc_fs.h b/include/linux/proc_fs.h
index ea55ed85a2d6..c81c93325044 100644
--- a/include/linux/proc_fs.h
+++ b/include/linux/proc_fs.h
@@ -31,6 +31,13 @@ enum {
PROC_ENTRY_proc_show = 1U << 5,
PROC_ENTRY_FORCE_LOOKUP = 1U << 7,
+ PROC_ENTRY_proc_write_buf = 1U << 8,
+
+ /*
+ * Writes larger than ->proc_write_size come up short instead of
+ * failing with -EINVAL.
+ */
+ PROC_ENTRY_SHORT_WRITES = 1U << 9,
};
struct proc_ops {
@@ -67,6 +74,16 @@ struct proc_ops {
const struct seq_operations *proc_seq_ops;
int (*proc_show)(struct seq_file *, void *);
unsigned int proc_state_size;
+ /*
+ * procfs copies what is written into a kernel buffer and calls
+ * ->proc_write_buf() with it. The buffer is NUL-terminated and holds
+ * at most ->proc_write_size bytes, PAGE_SIZE - 1 if that's 0. Larger
+ * writes fail with -EINVAL unless the entry asks for
+ * PROC_ENTRY_SHORT_WRITES. The position is NULL for a stream, like
+ * for ->write().
+ */
+ ssize_t (*proc_write_buf)(struct file *, char *, size_t, loff_t *);
+ unsigned int proc_write_size;
} __randomize_layout;
/* definitions for hide_pid field */
--
2.53.0
[PATCH RFC 12/16] proc: copy in writes to the network namespace entries before pinning
Author: Christian Brauner
Date: Mon, 02 Nov 2026 14:48
Date: Mon, 02 Nov 2026 14:48
91 lines
12495 bytes
12495 bytes
proc_simple_write() already hands ->write() a kernel copy of what was
written but makes that copy with the entry pinned. Let procfs make it
before it pins the entry instead. That covers everything created with
proc_create_net_data_write() and proc_create_net_single_write(), the
afs files and the rxrpc stats.
So a write to a proc_create_net_single() file without ->write() can
fail with -EINVAL or -EFAULT now before proc_simple_write() gets to
refuse it with -EACCES.
Signed-off-by: Christian Brauner (Amutable) <brauner@kernel.org>
---
fs/proc/generic.c | 15 ++++-----------
fs/proc/internal.h | 2 +-
fs/proc/proc_net.c | 4 ++--
3 files changed, 7 insertions(+), 14 deletions(-)
diff --git a/fs/proc/generic.c b/fs/proc/generic.c
index 7dc7df101286..83d273778a60 100644
--- a/fs/proc/generic.c
+++ b/fs/proc/generic.c
@@ -904,26 +904,19 @@ void proc_remove(struct proc_dir_entry *de)
EXPORT_SYMBOL(proc_remove);
/*
- * Pull a user buffer into memory and pass it to the file's write handler if
- * one is supplied. The ->write() method is permitted to modify the
- * kernel-side buffer.
+ * Pass what was written to the file's write handler if one is supplied. The
+ * ->write() method is permitted to modify the buffer.
*/
-ssize_t proc_simple_write(struct file *f, const char __user *ubuf, size_t size,
- loff_t *_pos)
+ssize_t proc_simple_write(struct file *f, char *buf, size_t size, loff_t *_pos)
{
struct proc_dir_entry *pde = PDE(file_inode(f));
- char *buf;
int ret;
if (!pde->write)
return -EACCES;
- if (size == 0 || size > PAGE_SIZE - 1)
+ if (size == 0)
return -EINVAL;
- buf = memdup_user_nul(ubuf, size);
- if (IS_ERR(buf))
- return PTR_ERR(buf);
ret = pde->write(f, buf, size);
- kfree(buf);
return ret == 0 ? size : ret;
}
diff --git a/fs/proc/internal.h b/fs/proc/internal.h
index 80b346a3299d..ed22c6dfb0bf 100644
--- a/fs/proc/internal.h
+++ b/fs/proc/internal.h
@@ -317,7 +317,7 @@ static inline bool is_empty_pde(const struct proc_dir_entry *pde)
{
return S_ISDIR(pde->mode) && !pde->proc_iops;
}
-extern ssize_t proc_simple_write(struct file *, const char __user *, size_t, loff_t *);
+extern ssize_t proc_simple_write(struct file *, char *, size_t, loff_t *);
/*
* inode.c
diff --git a/fs/proc/proc_net.c b/fs/proc/proc_net.c
index fa485581d05e..cdeb2e025d2e 100644
--- a/fs/proc/proc_net.c
+++ b/fs/proc/proc_net.c
@@ -77,7 +77,7 @@ static int seq_release_net(struct inode *ino, struct file *f)
static const struct proc_ops proc_net_seq_ops = {
.proc_open = seq_open_net,
- .proc_write = proc_simple_write,
+ .proc_write_buf = proc_simple_write,
.proc_release = seq_release_net,
};
@@ -186,7 +186,7 @@ static int single_release_net(struct inode *ino, struct file *f)
static const struct proc_ops proc_net_single_ops = {
.proc_open = single_open_net,
- .proc_write = proc_simple_write,
+ .proc_write_buf = proc_simple_write,
.proc_release = single_release_net,
};
--
2.53.0
[PATCH RFC 16/16] Documentation: seq_file: describe how a /proc file uses seq_file
Author: Christian Brauner
Date: Mon, 02 Nov 2026 14:48
Date: Mon, 02 Nov 2026 14:48
95 lines
13929 bytes
13929 bytes
The example in seq_file.rst hands proc_create() file operations built
from an open method that calls seq_open() and from seq_read(),
seq_lseek() and seq_release(). procfs doesn't work that way anymore. It
opens and releases the seq_file itself when the entry declares its
seq_operations or its show function in struct proc_ops. Say so, and
point the extra-simple version at ->proc_show() for /proc files.
Signed-off-by: Christian Brauner (Amutable) <brauner@kernel.org>
---
Documentation/filesystems/seq_file.rst | 42 ++++++++++++++++++++++++++--------
1 file changed, 33 insertions(+), 9 deletions(-)
diff --git a/Documentation/filesystems/seq_file.rst b/Documentation/filesystems/seq_file.rst
index d753d8177bcb..8cfb5fdabe99 100644
--- a/Documentation/filesystems/seq_file.rst
+++ b/Documentation/filesystems/seq_file.rst
@@ -68,7 +68,7 @@ kernel 3.10. Current versions require the following update::
- entry = create_proc_entry("sequence", 0, NULL);
- if (entry)
- entry->proc_fops = &ct_file_ops;
- + entry = proc_create("sequence", 0, NULL, &ct_file_ops);
+ + entry = proc_create("sequence", 0, NULL, &ct_proc_ops);
The iterator interface
======================
@@ -277,9 +277,10 @@ seq_file system, but we have not yet turned them into a file that a user
can see. Creating a file within the kernel requires, of course, the
creation of a set of file_operations which implement the operations on that
file. The seq_file interface provides a set of canned operations which do
-most of the work. The virtual file author still must implement the open()
-method, however, to hook everything up. The open function is often a single
-line, as in the example module::
+most of the work. A file in /proc is special, procfs hooks the seq_file up
+itself, see below. Everywhere else the virtual file author still must
+implement the open() method to do that. The open function is often a
+single line::
static int ct_open(struct inode *inode, struct file *file)
{
@@ -343,19 +344,38 @@ file_operations structure will look like::
There is also a seq_release_private() which passes the contents of the
seq_file private field to kfree() before releasing the structure.
-The final step is the creation of the /proc file itself. In the example
-code, that is done in the initialization code in the usual way::
+The final step is the creation of the file itself. A file in /proc is
+special here: procfs opens and releases the seq_file itself, so there is
+no open method and no file_operations for it. The entry declares its
+seq_operations in struct proc_ops instead, and the size of a private
+block in proc_state_size if it wants procfs to allocate one like
+seq_open_private() would::
+
+ static const struct proc_ops ct_proc_ops = {
+ .proc_seq_ops = &ct_seq_ops,
+ };
static int ct_init(void)
{
- struct proc_dir_entry *entry;
-
- proc_create("sequence", 0, NULL, &ct_file_ops);
+ proc_create("sequence", 0, NULL, &ct_proc_ops);
return 0;
}
module_init(ct_init);
+A ->proc_open() method of such an entry finds the seq_file in
+file->private_data and may set up more state; ->proc_release() releases
+only that. It runs when the entry is removed, which can be while a reader
+is still copying out of the seq_file, so it must leave the seq_file
+alone. procfs only calls into the entry between start() and stop() with
+the entry pinned, so nothing that runs there may wait for something a
+reader can hold while it copies to userspace, like the seq_file's lock.
+start() and next() may return ERR_PTR(-EIO) to say that the entry is
+gone; procfs unpins it right away then. An entry that wants to start out
+with a larger buffer, like single_open_size() gives, calls
+seq_preallocate() from ->proc_open(). The file_operations above are what
+a file elsewhere, in debugfs for example, uses.
+
And that is pretty much it.
@@ -394,3 +414,7 @@ value given to single_open() can be found in the private field of the
seq_file structure. When using single_open(), the programmer should use
single_release() instead of seq_release() in the file_operations structure
to avoid a memory leak.
+
+A file in /proc puts the show() function into the proc_show field of its
+struct proc_ops instead and procfs does the rest, with the entry's data in
+the private field of the seq_file structure.
--
2.53.0
Re: [PATCH RFC 02/38] PNP: isapnp: don't read past the end of the buffer on a large pread()
Author: "Rafael J. Wysocki (Intel)"
Date: Mon, 02 Nov 2026 14:51
Date: Mon, 02 Nov 2026 14:51
50 lines
12257 bytes
12257 bytes
On Mon, Nov 2, 2026 at 3:22 PM Christian Brauner <brauner@kernel.org> wrote:
>
> isapnp_proc_bus_read() truncates the position to an int. A pread() at
> an offset like 0xfffffc00 turns it negative, the check against the size
> of the configuration space passes, and the loop then puts everything
> from that position up to byte 256 into the reader's buffer, no matter
> how few bytes were asked for.
>
> Keep the position as a loff_t and clamp the count against what is left
> of the configuration space.
>
> Fixes: 1da177e4c3f4 ("Linux-2.6.12-rc2")
> Cc: stable@vger.kernel.org
> Signed-off-by: Christian Brauner (Amutable) <brauner@kernel.org>
Reviewed-by: Rafael J. Wysocki (Intel) <rafael@kernel.org>
> ---
> drivers/pnp/isapnp/proc.c | 6 ++----
> 1 file changed, 2 insertions(+), 4 deletions(-)
>
> diff --git a/drivers/pnp/isapnp/proc.c b/drivers/pnp/isapnp/proc.c
> index c1f6e6bd0eb2..8197c7f29743 100644
> --- a/drivers/pnp/isapnp/proc.c
> +++ b/drivers/pnp/isapnp/proc.c
> @@ -23,14 +23,12 @@ static ssize_t isapnp_proc_bus_read(struct file *file, char __user * buf,
> size_t nbytes, loff_t * ppos)
> {
> struct pnp_dev *dev = pde_data(file_inode(file));
> - int pos = *ppos;
> + loff_t pos = *ppos;
> int cnt, size = 256;
>
> if (pos >= size)
> return 0;
> - if (nbytes >= size)
> - nbytes = size;
> - if (pos + nbytes > size)
> + if (nbytes > size - pos)
> nbytes = size - pos;
> cnt = nbytes;
>
>
> --
> 2.53.0
>
>
[PATCH RFC 00/22] proc: convert the arch and platform entries
Author: Christian Brauner
Date: Mon, 02 Nov 2026 15:01
Date: Mon, 02 Nov 2026 15:01
134 lines
17665 bytes
17665 bytes
This converts the proc entries under arch/ and the ones of ACPI, the
platform drivers, via-pmu and PNP to what "proc: let procfs own the
data path" set up: procfs owns the seq_file, copies in writes, copies
out reads and copies ioctl arguments in and out, so the entries' code
only ever runs with the entry pinned and never touches userspace
memory.
One patch per subsystem and method, in the order seq_file, write, read,
ioctl. Most of them just stop copying in or out themselves, or switch
from kstrto*_from_user() to kstrto*() and from simple_read_from_buffer()
to memory_read_from_buffer(). Where a write that is larger than
anything the entry parses now fails with -EINVAL from procfs I've called
that out in the commit. The rtas flash files take their image in 4k
pieces and keep doing that with PROC_ENTRY_SHORT_WRITES, the rtas error
log is a stream and hands out a record before procfs copies it out, so
a reader that passes a bad buffer loses it. /proc/mtrr's ioctls are
table driven now, the native and the 32-bit commands share one table.
Build tested per commit on x86_64 and with W=1 on the touched objects on
alpha, arm, m68k, powerpc, sh, sparc, um, x86 and xtensa. The entries I
could get at in an x86 VM behave the same with and without the series.
This is on top of "proc: let procfs own the data path". The other three
conversion series don't depend on it and it doesn't depend on them.
Signed-off-by: Christian Brauner (Amutable) <brauner@kernel.org>
---
Christian Brauner (22):
alpha: srm_env: let procfs copy in writes
ARM: alignment: let procfs copy in writes
ARM: atags: let procfs copy out reads of /proc/atags
m68k/bootinfo: let procfs copy out reads of /proc/bootinfo
powerpc/rtas: let procfs copy in writes
powerpc/rtas: let procfs copy out reads
powerpc/pseries: let procfs copy in writes
powerpc/procfs: let procfs copy out reads of /proc/powerpc/systemcfg
sh: alignment: let procfs copy in writes
sparc: led: let procfs copy in writes
um: let procfs copy in writes
x86/mtrr: let procfs own the seq_file of /proc/mtrr
x86/mtrr: let procfs copy in writes to /proc/mtrr
x86/mtrr: let procfs copy the ioctl arguments of /proc/mtrr
xtensa: simdisk: let procfs copy in writes
xtensa: simdisk: let procfs copy out reads
ACPI: proc: let procfs copy in writes to /proc/acpi/wakeup
macintosh/via-pmu: let procfs copy in writes to the options file
platform/x86: thinkpad_acpi: let procfs copy in writes
platform/x86: toshiba_acpi: let procfs copy in writes
PNP: pnpbios: let procfs copy in writes
PNP: isapnp: let procfs copy out reads
arch/alpha/kernel/srm_env.c | 12 +---
arch/arm/kernel/atags_proc.c | 6 +-
arch/arm/mm/alignment.c | 7 +--
arch/m68k/kernel/bootinfo_proc.c | 6 +-
arch/powerpc/kernel/proc_powerpc.c | 6 +-
arch/powerpc/kernel/rtas-proc.c | 40 ++++++------
arch/powerpc/kernel/rtas_flash.c | 62 +++++++++----------
arch/powerpc/kernel/rtasd.c | 19 ++----
arch/powerpc/platforms/pseries/lpar.c | 14 ++---
arch/powerpc/platforms/pseries/lparcfg.c | 7 +--
arch/powerpc/platforms/pseries/reconfig.c | 22 +++----
arch/sh/mm/alignment.c | 7 +--
arch/sparc/kernel/led.c | 13 +---
arch/um/drivers/mconsole_kern.c | 11 +---
arch/um/kernel/exitcode.c | 14 ++---
arch/x86/kernel/cpu/mtrr/if.c | 95 ++++++++++++++++-------------
arch/xtensa/platforms/iss/simdisk.c | 32 ++++------
drivers/acpi/proc.c | 7 +--
drivers/macintosh/via-pmu.c | 7 +--
drivers/platform/x86/lenovo/thinkpad_acpi.c | 14 +----
drivers/platform/x86/toshiba_acpi.c | 36 ++++-------
drivers/pnp/isapnp/proc.c | 20 +++---
drivers/pnp/pnpbios/proc.c | 9 +--
23 files changed, 193 insertions(+), 273 deletions(-)
---
base-commit: 65ba8f34ec886ec72b2c07ddc8cf3d815c32869d
change-id: 20261101-work-procfs-arch-8d4ade48be31
prerequisite-change-id: 20261101-work-procfs-core-8fa4a4f412cf:v1
prerequisite-patch-id: 0474af12959624910718795bf86684383664c685
prerequisite-patch-id: 540aa9ea28adcbc54d8264f66314e29e3b8c9d49
prerequisite-patch-id: 2f7139d1c972c4439dccca610caa5f1288987a3d
prerequisite-patch-id: b0031a7c4855b7ec38259c59f30f65101a8fc4b5
prerequisite-patch-id: 0b7f226abf2b9ff41873a1aa55cf97747647d56b
prerequisite-patch-id: f63e345f897619af54813958046617c655fb7daf
prerequisite-patch-id: c0e21e762a2960e3324c5ab04634a7035e515fa6
prerequisite-patch-id: fd59c71eadd8f39c58b3baea93567bc093bc9b0e
prerequisite-patch-id: 5d52cc8a5c73e4c5ba16b8e24f51408c17583bf9
prerequisite-patch-id: 1f9179284e2f3314d4b46ac5e989089a21c7576c
prerequisite-patch-id: 10397bbf696bee16dc71eb79fff6c5e73842bcbc
prerequisite-patch-id: 9b44279dcf08912238d2f6438a3328a778a79a0c
prerequisite-patch-id: 16854b621a3a71dfc365aaf7abaa7f65adfe9f7e
prerequisite-patch-id: ca42eea21fae1fd1cd3518ab057c74b61ffd68fe
prerequisite-patch-id: 23255dc2be76b0479c083dbd4c897bfbb533aed5
prerequisite-patch-id: 82129c687ebf0a41b369893b92f2c038420ab801
prerequisite-patch-id: 56ad57a99a5bda075a9f4e844368974296744934
prerequisite-patch-id: 288b3cbaa11e01a778cc3d7beb6c0549aacf59f2
prerequisite-patch-id: a163b1549608652bfa5bd9ff93efa55b541010c4
prerequisite-patch-id: 78f067c343177e52d49ddfe9ee2992e602549869
prerequisite-patch-id: 19c12bc6e3fbed35acc1f657196b1a997f48cdfe
prerequisite-patch-id: 5800c3076694bad719954b2ad4d7448c6e013bfd
prerequisite-patch-id: 2a2f63d8843b159594b9eee6ddea028ce76a6270
prerequisite-patch-id: 40a1951d57906c4a1b98a4bd96a814b80339b9c5
prerequisite-patch-id: e2141077e69f20a2b27fab69c8d61d0686682f53
prerequisite-patch-id: c4d6018c26c40dc549d88330d23ce48f872c2b7f
prerequisite-patch-id: 6de8ec17746031bc6d1d9ac262c5be0f905fcd79
prerequisite-patch-id: b87bcb167884605bff1fd0f2ed8a88942d07efd5
prerequisite-patch-id: cfa9a8b4c67e20cfd22d77035bfd21ee96ab3298
prerequisite-patch-id: abf3482ed2399b7b2e1ee72e937af05bb9200667
prerequisite-patch-id: 02c175b2dee565a85b3308ecf2ec8e3c9a029e0f
prerequisite-patch-id: 4ed6b3d3d45de49d4967e88ba4e7f03372775f55
prerequisite-patch-id: dbe02fe3f784776fda5e7ae99f077bef0cd4b48d
prerequisite-patch-id: 53edc9a95afff57c4acba40095a8054924261ea2
prerequisite-patch-id: cf9ce0099005fd3bc31a1e15131d48e315ac3bee
prerequisite-patch-id: 16a657a5d0046068dbace29e7ea046ac1441f46f
prerequisite-patch-id: efeb2d6f7df71d5dede2c1d12fe999a28e38aab9
prerequisite-patch-id: e6ffe2a5c3ee5645f56c8366b568fbc9f140848c
prerequisite-patch-id: 117db27dcdef433de7c6be5e95973cac1e373927
prerequisite-patch-id: a0844aa26ffb65ccb362b88ef6c2f4758e15edbf
prerequisite-patch-id: 143fb91bddf33068b6ee11e205818c4c2815e23d
prerequisite-patch-id: 6490e4da1cb2f543683bd164bc9a5a1c943057e7
prerequisite-patch-id: cdefca57d3c552fb6f6b928699edd85f33be25fd
prerequisite-patch-id: 2f0fdb970ddd760d85ff5d2caad0b96348270182
prerequisite-patch-id: f2aa468ff5cbb1cabab3f70e1b8eeee0286cfb4a
prerequisite-patch-id: 037030d95eeb7c49b1e43f638ec954f2cebbcfb1
prerequisite-patch-id: 487141c49e2548310bfb6c2a0964a78f08b285a9
prerequisite-patch-id: c3d3008819fa5629a18142ec9ba6d7024ade7fdb
prerequisite-patch-id: ba15e7c86b60ccc2f0f7ee4c9bc99073068757bc
prerequisite-patch-id: 9423f410b99853693db06c29f12f1c08278b050d
prerequisite-patch-id: 4e7966978e4b11c4d843831f312cdcd5b848f00c
prerequisite-patch-id: 3d24be1897a624d519ef144801f76d152c7528a9
prerequisite-patch-id: 3b4d9fe7b26f6b7b9b3f7725ea333df357107c44
prerequisite-patch-id: 7d9dd345428baeb88c862ef902612f7160dc1d8c
[PATCH RFC 07/22] powerpc/pseries: let procfs copy in writes
Author: Christian Brauner
Date: Mon, 02 Nov 2026 15:01
Date: Mon, 02 Nov 2026 15:01
166 lines
14942 bytes
14942 bytes
Implement ->proc_write_buf() for lparcfg, ofdt and the vcpudispatch
files so procfs copies in what is written before it pins the entry, and
parse what was written from that copy. ofdt takes as much as
memdup_user_nul() could copy before since device tree updates can be
large.
Signed-off-by: Christian Brauner (Amutable) <brauner@kernel.org>
---
arch/powerpc/platforms/pseries/lpar.c | 14 ++++++--------
arch/powerpc/platforms/pseries/lparcfg.c | 7 +++----
arch/powerpc/platforms/pseries/reconfig.c | 22 ++++++++--------------
3 files changed, 17 insertions(+), 26 deletions(-)
diff --git a/arch/powerpc/platforms/pseries/lpar.c b/arch/powerpc/platforms/pseries/lpar.c
index 48fe0be71a89..dfd027cc9047 100644
--- a/arch/powerpc/platforms/pseries/lpar.c
+++ b/arch/powerpc/platforms/pseries/lpar.c
@@ -498,7 +498,7 @@ static void dtl_worker_disable(unsigned long *time_limit)
up_write(&dtl_access_lock);
}
-static ssize_t vcpudispatch_stats_write(struct file *file, const char __user *p,
+static ssize_t vcpudispatch_stats_write(struct file *file, char *p,
size_t count, loff_t *ppos)
{
unsigned long time_limit = jiffies + HZ;
@@ -509,8 +509,7 @@ static ssize_t vcpudispatch_stats_write(struct file *file, const char __user *p,
if (count > 15)
return -EINVAL;
- if (copy_from_user(buf, p, count))
- return -EFAULT;
+ memcpy(buf, p, count);
buf[count] = 0;
rc = kstrtoint(buf, 0, &cmd);
@@ -586,11 +585,11 @@ static int vcpudispatch_stats_display(struct seq_file *p, void *v)
static const struct proc_ops vcpudispatch_stats_proc_ops = {
.proc_show = vcpudispatch_stats_display,
- .proc_write = vcpudispatch_stats_write,
+ .proc_write_buf = vcpudispatch_stats_write,
};
static ssize_t vcpudispatch_stats_freq_write(struct file *file,
- const char __user *p, size_t count, loff_t *ppos)
+ char *p, size_t count, loff_t *ppos)
{
int rc, freq;
char buf[16];
@@ -598,8 +597,7 @@ static ssize_t vcpudispatch_stats_freq_write(struct file *file,
if (count > 15)
return -EINVAL;
- if (copy_from_user(buf, p, count))
- return -EFAULT;
+ memcpy(buf, p, count);
buf[count] = 0;
rc = kstrtoint(buf, 0, &freq);
@@ -622,7 +620,7 @@ static int vcpudispatch_stats_freq_display(struct seq_file *p, void *v)
static const struct proc_ops vcpudispatch_stats_freq_proc_ops = {
.proc_show = vcpudispatch_stats_freq_display,
- .proc_write = vcpudispatch_stats_freq_write,
+ .proc_write_buf = vcpudispatch_stats_freq_write,
};
static int __init vcpudispatch_stats_procfs_init(void)
diff --git a/arch/powerpc/platforms/pseries/lparcfg.c b/arch/powerpc/platforms/pseries/lparcfg.c
index a6a137ce44ce..43af7f820fab 100644
--- a/arch/powerpc/platforms/pseries/lparcfg.c
+++ b/arch/powerpc/platforms/pseries/lparcfg.c
@@ -687,7 +687,7 @@ static ssize_t update_mpp(u64 *entitlement, u8 *weight)
* This function should be invoked only on systems with
* FW_FEATURE_SPLPAR.
*/
-static ssize_t lparcfg_write(struct file *file, const char __user * buf,
+static ssize_t lparcfg_write(struct file *file, char *buf,
size_t count, loff_t * off)
{
char kbuf[64];
@@ -702,8 +702,7 @@ static ssize_t lparcfg_write(struct file *file, const char __user * buf,
if (count == 0 || count > sizeof(kbuf))
return -EINVAL;
- if (copy_from_user(kbuf, buf, count))
- return -EFAULT;
+ memcpy(kbuf, buf, count);
kbuf[count - 1] = '\0';
tmp = strchr(kbuf, '=');
@@ -799,7 +798,7 @@ static int lparcfg_data(struct seq_file *m, void *v)
}
static const struct proc_ops lparcfg_proc_ops = {
- .proc_write = lparcfg_write,
+ .proc_write_buf = lparcfg_write,
.proc_show = lparcfg_data,
};
diff --git a/arch/powerpc/platforms/pseries/reconfig.c b/arch/powerpc/platforms/pseries/reconfig.c
index b41c5db726c9..f406e826a03e 100644
--- a/arch/powerpc/platforms/pseries/reconfig.c
+++ b/arch/powerpc/platforms/pseries/reconfig.c
@@ -349,7 +349,7 @@ static int do_update_property(char *buf, size_t bufsize)
* ofdt_write - perform operations on the Open Firmware device tree
*
* @file: not used
- * @buf: command and arguments
+ * @kbuf: command and arguments
* @count: size of the command buffer
* @off: not used
*
@@ -357,26 +357,19 @@ static int do_update_property(char *buf, size_t bufsize)
* whole nodes along with their properties. Operations on individual
* properties are not implemented (yet).
*/
-static ssize_t ofdt_write(struct file *file, const char __user *buf, size_t count,
+static ssize_t ofdt_write(struct file *file, char *kbuf, size_t count,
loff_t *off)
{
int rv;
- char *kbuf;
char *tmp;
rv = security_locked_down(LOCKDOWN_DEVICE_TREE);
if (rv)
return rv;
- kbuf = memdup_user_nul(buf, count);
- if (IS_ERR(kbuf))
- return PTR_ERR(kbuf);
-
tmp = strchr(kbuf, ' ');
- if (!tmp) {
- rv = -EINVAL;
- goto out;
- }
+ if (!tmp)
+ return -EINVAL;
*tmp = '\0';
tmp++;
@@ -392,13 +385,14 @@ static ssize_t ofdt_write(struct file *file, const char __user *buf, size_t coun
rv = do_update_property(tmp, count - (tmp - kbuf));
else
rv = -EINVAL;
-out:
- kfree(kbuf);
+
return rv ? rv : count;
}
static const struct proc_ops ofdt_proc_ops = {
- .proc_write = ofdt_write,
+ .proc_write_buf = ofdt_write,
+ /* what memdup_user_nul() could copy in */
+ .proc_write_size = KMALLOC_MAX_SIZE - 1,
.proc_lseek = noop_llseek,
};
--
2.53.0
Page 1 of 4 • 177 total messages
Thread Navigation
This is a paginated view of messages in the thread with full content displayed inline.
Messages are displayed in chronological order, with the original post highlighted in green.
Use pagination controls to navigate through all messages in large threads.
Back to All Threads