Re: [PATCH RESEND v2 16/23] proc: let procfs copy out reads that don't go through seq_file

From
Geert Uytterhoeven <geert@linux-m68k.org>
Date
2026-11-05 10:08:40
Also in
linux-alpha, linux-fsdevel, linux-mm, linux-serial, linux-sh, linux-sound, linux-um
Reply to
Christian Brauner, 2026-11-04
Hi Christian,

On Wed, 4 Nov 2026 at 20:33, Christian Brauner [off-list ref] wrote:
proc_reg_read() and proc_reg_read_iter() pin the entry and call
->proc_read() or ->proc_read_iter() which copy to userspace
themselves. Like for seq_file reads and for writes, a reader can make
a page fault in that copy take as long as it wants and
remove_proc_entry() waits for it.

So do what kernfs does for these reads as well. An entry that implements
->proc_read_buf() fills a kernel buffer of at most ->proc_read_size
bytes, a page if the entry doesn't care, and procfs copies it to
userspace after unpinning the entry. The entry moves the position like
it did for ->proc_read() but that only sticks if the copy to userspace
worked, like in kernfs_file_read_iter(). If the copy fails halfway the
read fails with -EFAULT and the position stays, where
simple_read_from_buffer() returned what it had copied until then.
procfs remembers ->proc_read_size in the entry like ->proc_write_size.

Larger reads come up short, like with kernfs. An entry whose readers
want all of a large file in one read asks for PROC_ENTRY_FULL_READS and
procfs fills the read in pieces of ->proc_read_size, unpinning the entry
in between. A piece whose copy fails ends the read with what the pieces
before it returned. kexec-tools treats any short read of /proc/vmcore
as fatal for example (cf. [1]).

These entries are read through ->read_iter() so readv(), splice(), aio,
the RWF flags, kernel_read() and io_uring work for them like for the
->proc_read_iter() ones. io_uring's nonblocking attempt gets -EAGAIN
though, like loop_rw_iter() gave it for ->proc_read() entries. Neither
the allocation nor the entry can honor IOCB_NOWAIT, and io_uring would
take a short read of a record file for an unfinished one and glue the
next record to it. So it retries from a worker like before.

An entry that implements ->proc_read_buf() and also declares a seq_file
or one of the other read methods fails to register.

No users yet.

[1]: https://git.kernel.org/pub/scm/utils/kernel/kexec/kexec-tools.git/tree/util_lib/elf_info.c

Signed-off-by: Christian Brauner (Amutable) <brauner@kernel.org>
Thanks for your patch!
quoted hunk ↗ jump to hunk
--- a/include/linux/proc_fs.h
+++ b/include/linux/proc_fs.h
quoted hunk ↗ jump to hunk
@@ -84,6 +91,15 @@ struct proc_ops {
         */
        ssize_t (*proc_write_buf)(struct file *, char *, size_t, loff_t *);
        unsigned int proc_write_size;
+       /*
+        * procfs calls ->proc_read_buf() to fill a kernel buffer of at most
+        * ->proc_read_size bytes, PAGE_SIZE if that's 0, and copies it to
+        * userspace afterwards. Larger reads come up short unless the entry
+        * asks for PROC_ENTRY_FULL_READS. The position the entry moves to only
+        * sticks if the copy works.
+        */
+       ssize_t (*proc_read_buf)(struct file *, char *, size_t, loff_t *);
As for proc_write_buf, I think it would be good to use this conversion
as an opportunity to replace "char *" by "void *", which will probably
let you drop several casts from users.
quoted hunk ↗ jump to hunk
+       unsigned int proc_read_size;
 } __randomize_layout;

 /* definitions for hide_pid field */
Gr{oetje,eeting}s,

                        Geert


--
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- geert@linux-m68k.org

In personal conversations with technical people, I call myself a hacker. But
when I'm talking to journalists I just say "programmer" or something like that.
                                -- Linus Torvalds
Thread (202 messages) 202 messages, 11 authors, last reply 18d ago
Keyboard shortcuts
hback out one level
jnext message in thread / next series-diff position
kprevious message in thread / previous series-diff position
ldrill in
Escclose help / fold thread tree
?toggle this help