[PATCH RESEND v2 12/13] samples/kfifo: let procfs copy out reads

From: Christian Brauner

Date: Wed Nov 04 2026 - 21:52:08 EST


Implement ->proc_read_buf() so procfs copies what is read to userspace
after it unpinned the entry, and take the data out of the fifo with
kfifo_out() instead of kfifo_to_user(). A reader that passes a bad
buffer loses what was taken out for it now, kfifo_to_user() left that in
the fifo. The samples can be spliced now too, and a sendfile() from them
into a target that fails or takes less than was read drops what was
taken out in between, like for the rtas error log.

Signed-off-by: Christian Brauner (Amutable) <brauner@xxxxxxxxxx>
---
samples/kfifo/bytestream-example.c | 9 +++------
samples/kfifo/inttype-example.c | 10 ++++------
samples/kfifo/record-example.c | 9 +++------
3 files changed, 10 insertions(+), 18 deletions(-)

diff --git a/samples/kfifo/bytestream-example.c b/samples/kfifo/bytestream-example.c
index 8c58fb46a2bf..214e46a64cc1 100644
--- a/samples/kfifo/bytestream-example.c
+++ b/samples/kfifo/bytestream-example.c
@@ -125,26 +125,23 @@ static ssize_t fifo_write(struct file *file, char *buf,
return copied;
}

-static ssize_t fifo_read(struct file *file, char __user *buf,
+static ssize_t fifo_read(struct file *file, char *buf,
size_t count, loff_t *ppos)
{
- int ret;
unsigned int copied;

if (mutex_lock_interruptible(&read_access))
return -ERESTARTSYS;

- ret = kfifo_to_user(&test, buf, count, &copied);
+ copied = kfifo_out(&test, buf, count);

mutex_unlock(&read_access);
- if (ret)
- return ret;

return copied;
}

static const struct proc_ops fifo_proc_ops = {
- .proc_read = fifo_read,
+ .proc_read_buf = fifo_read,
.proc_write_buf = fifo_write,
.proc_lseek = noop_llseek,
};
diff --git a/samples/kfifo/inttype-example.c b/samples/kfifo/inttype-example.c
index c5d16b815e73..8384d7f8de6c 100644
--- a/samples/kfifo/inttype-example.c
+++ b/samples/kfifo/inttype-example.c
@@ -119,26 +119,24 @@ static ssize_t fifo_write(struct file *file, char *buf,
return copied;
}

-static ssize_t fifo_read(struct file *file, char __user *buf,
+static ssize_t fifo_read(struct file *file, char *buf,
size_t count, loff_t *ppos)
{
- int ret;
unsigned int copied;

if (mutex_lock_interruptible(&read_access))
return -ERESTARTSYS;

- ret = kfifo_to_user(&test, buf, count, &copied);
+ copied = kfifo_out(&test, (int *)buf, count / sizeof(int)) *
+ sizeof(int);

mutex_unlock(&read_access);
- if (ret)
- return ret;

return copied;
}

static const struct proc_ops fifo_proc_ops = {
- .proc_read = fifo_read,
+ .proc_read_buf = fifo_read,
.proc_write_buf = fifo_write,
.proc_lseek = noop_llseek,
};
diff --git a/samples/kfifo/record-example.c b/samples/kfifo/record-example.c
index dcc749247c64..29d08e90a63d 100644
--- a/samples/kfifo/record-example.c
+++ b/samples/kfifo/record-example.c
@@ -132,26 +132,23 @@ static ssize_t fifo_write(struct file *file, char *buf,
return copied;
}

-static ssize_t fifo_read(struct file *file, char __user *buf,
+static ssize_t fifo_read(struct file *file, char *buf,
size_t count, loff_t *ppos)
{
- int ret;
unsigned int copied;

if (mutex_lock_interruptible(&read_access))
return -ERESTARTSYS;

- ret = kfifo_to_user(&test, buf, count, &copied);
+ copied = kfifo_out(&test, buf, count);

mutex_unlock(&read_access);
- if (ret)
- return ret;

return copied;
}

static const struct proc_ops fifo_proc_ops = {
- .proc_read = fifo_read,
+ .proc_read_buf = fifo_read,
.proc_write_buf = fifo_write,
.proc_lseek = noop_llseek,
/* kfifo_from_user() cut a record to what its length byte holds */

--
2.53.0