Implement ->proc_write_buf() so procfs copies in what is written before
it pins the entry, and put that copy into the fifo with kfifo_in()
instead of kfifo_from_user(). Writes larger than PAGE_SIZE - 1 bytes
fail with -EINVAL now. kfifo_from_user() cut a record to 255 bytes,
what the one byte that holds its length can say, so the record sample
keeps doing that with PROC_ENTRY_SHORT_WRITES.

Signed-off-by: Christian Brauner (Amutable) <[email protected]>
---
 samples/kfifo/bytestream-example.c |  9 +++------
 samples/kfifo/inttype-example.c    | 10 ++++------
 samples/kfifo/record-example.c     | 12 ++++++------
 3 files changed, 13 insertions(+), 18 deletions(-)

diff --git a/samples/kfifo/bytestream-example.c 
b/samples/kfifo/bytestream-example.c
index 4ae29a12cc8a..8c58fb46a2bf 100644
--- a/samples/kfifo/bytestream-example.c
+++ b/samples/kfifo/bytestream-example.c
@@ -110,20 +110,17 @@ static int __init testfunc(void)
        return 0;
 }
 
-static ssize_t fifo_write(struct file *file, const char __user *buf,
+static ssize_t fifo_write(struct file *file, char *buf,
                                                size_t count, loff_t *ppos)
 {
-       int ret;
        unsigned int copied;
 
        if (mutex_lock_interruptible(&write_access))
                return -ERESTARTSYS;
 
-       ret = kfifo_from_user(&test, buf, count, &copied);
+       copied = kfifo_in(&test, buf, count);
 
        mutex_unlock(&write_access);
-       if (ret)
-               return ret;
 
        return copied;
 }
@@ -148,7 +145,7 @@ static ssize_t fifo_read(struct file *file, char __user 
*buf,
 
 static const struct proc_ops fifo_proc_ops = {
        .proc_read      = fifo_read,
-       .proc_write     = fifo_write,
+       .proc_write_buf = fifo_write,
        .proc_lseek     = noop_llseek,
 };
 
diff --git a/samples/kfifo/inttype-example.c b/samples/kfifo/inttype-example.c
index e4f93317c5d0..c5d16b815e73 100644
--- a/samples/kfifo/inttype-example.c
+++ b/samples/kfifo/inttype-example.c
@@ -103,20 +103,18 @@ static int __init testfunc(void)
        return 0;
 }
 
-static ssize_t fifo_write(struct file *file, const char __user *buf,
+static ssize_t fifo_write(struct file *file, char *buf,
                                                size_t count, loff_t *ppos)
 {
-       int ret;
        unsigned int copied;
 
        if (mutex_lock_interruptible(&write_access))
                return -ERESTARTSYS;
 
-       ret = kfifo_from_user(&test, buf, count, &copied);
+       copied = kfifo_in(&test, (const int *)buf, count / sizeof(int)) *
+                sizeof(int);
 
        mutex_unlock(&write_access);
-       if (ret)
-               return ret;
 
        return copied;
 }
@@ -141,7 +139,7 @@ static ssize_t fifo_read(struct file *file, char __user 
*buf,
 
 static const struct proc_ops fifo_proc_ops = {
        .proc_read      = fifo_read,
-       .proc_write     = fifo_write,
+       .proc_write_buf = fifo_write,
        .proc_lseek     = noop_llseek,
 };
 
diff --git a/samples/kfifo/record-example.c b/samples/kfifo/record-example.c
index e4d1a2d7983c..dcc749247c64 100644
--- a/samples/kfifo/record-example.c
+++ b/samples/kfifo/record-example.c
@@ -117,20 +117,17 @@ static int __init testfunc(void)
        return 0;
 }
 
-static ssize_t fifo_write(struct file *file, const char __user *buf,
+static ssize_t fifo_write(struct file *file, char *buf,
                                                size_t count, loff_t *ppos)
 {
-       int ret;
        unsigned int copied;
 
        if (mutex_lock_interruptible(&write_access))
                return -ERESTARTSYS;
 
-       ret = kfifo_from_user(&test, buf, count, &copied);
+       copied = kfifo_in(&test, buf, count);
 
        mutex_unlock(&write_access);
-       if (ret)
-               return ret;
 
        return copied;
 }
@@ -155,8 +152,11 @@ static ssize_t fifo_read(struct file *file, char __user 
*buf,
 
 static const struct proc_ops fifo_proc_ops = {
        .proc_read      = fifo_read,
-       .proc_write     = fifo_write,
+       .proc_write_buf = fifo_write,
        .proc_lseek     = noop_llseek,
+       /* kfifo_from_user() cut a record to what its length byte holds */
+       .proc_flags     = PROC_ENTRY_SHORT_WRITES,
+       .proc_write_size = U8_MAX,
 };
 
 static int __init example_init(void)

-- 
2.53.0


Reply via email to