[PATCH 03/11] fs: add new read_uptr and write_uptr file operations
From: Christoph Hellwig
Date: Wed Jun 24 2020 - 12:30:03 EST
Add two new file operations that are identical to ->read and ->write
except that they can also safely take kernel pointers using the uptr_t
type.
Signed-off-by: Christoph Hellwig <hch@xxxxxx>
---
fs/internal.h | 4 ++--
fs/read_write.c | 18 ++++++++++++++----
include/linux/fs.h | 3 +++
3 files changed, 19 insertions(+), 6 deletions(-)
diff --git a/fs/internal.h b/fs/internal.h
index 242f2845b3428b..b6777a47b05163 100644
--- a/fs/internal.h
+++ b/fs/internal.h
@@ -189,9 +189,9 @@ int do_statx(int dfd, const char __user *filename, unsigned flags,
static inline void set_fmode_can_read_write(struct file *f)
{
if ((f->f_mode & FMODE_READ) &&
- (f->f_op->read || f->f_op->read_iter))
+ (f->f_op->read || f->f_op->read_uptr || f->f_op->read_iter))
f->f_mode |= FMODE_CAN_READ;
if ((f->f_mode & FMODE_WRITE) &&
- (f->f_op->write || f->f_op->write_iter))
+ (f->f_op->write || f->f_op->write_uptr || f->f_op->write_iter))
f->f_mode |= FMODE_CAN_WRITE;
}
diff --git a/fs/read_write.c b/fs/read_write.c
index e7f36b15683049..24ffbf3cbda243 100644
--- a/fs/read_write.c
+++ b/fs/read_write.c
@@ -430,7 +430,9 @@ ssize_t __kernel_read(struct file *file, void *buf, size_t count, loff_t *pos)
if (count > MAX_RW_COUNT)
count = MAX_RW_COUNT;
- if (file->f_op->read) {
+ if (file->f_op->read_uptr) {
+ ret = file->f_op->read_uptr(file, KERNEL_UPTR(buf), count, pos);
+ } else if (file->f_op->read) {
mm_segment_t old_fs = get_fs();
set_fs(KERNEL_DS);
@@ -485,7 +487,9 @@ ssize_t vfs_read(struct file *file, char __user *buf, size_t count, loff_t *pos)
if (count > MAX_RW_COUNT)
count = MAX_RW_COUNT;
- if (file->f_op->read)
+ if (file->f_op->read_uptr)
+ ret = file->f_op->read_uptr(file, USER_UPTR(buf), count, pos);
+ else if (file->f_op->read)
ret = file->f_op->read(file, buf, count, pos);
else if (file->f_op->read_iter)
ret = new_sync_read(file, buf, count, pos);
@@ -530,7 +534,10 @@ ssize_t __kernel_write(struct file *file, const void *buf, size_t count,
if (count > MAX_RW_COUNT)
count = MAX_RW_COUNT;
- if (file->f_op->write) {
+ if (file->f_op->write_uptr) {
+ ret = file->f_op->write_uptr(file, KERNEL_UPTR((void *)buf),
+ count, pos);
+ } else if (file->f_op->write) {
mm_segment_t old_fs = get_fs();
set_fs(KERNEL_DS);
@@ -592,7 +599,10 @@ ssize_t vfs_write(struct file *file, const char __user *buf, size_t count, loff_
if (count > MAX_RW_COUNT)
count = MAX_RW_COUNT;
file_start_write(file);
- if (file->f_op->write)
+ if (file->f_op->write_uptr)
+ ret = file->f_op->write_uptr(file,
+ USER_UPTR((char __user *)buf), count, pos);
+ else if (file->f_op->write)
ret = file->f_op->write(file, buf, count, pos);
else if (file->f_op->write_iter)
ret = new_sync_write(file, buf, count, pos);
diff --git a/include/linux/fs.h b/include/linux/fs.h
index fac6aead402a98..d8fc3015f5a197 100644
--- a/include/linux/fs.h
+++ b/include/linux/fs.h
@@ -39,6 +39,7 @@
#include <linux/fs_types.h>
#include <linux/build_bug.h>
#include <linux/stddef.h>
+#include <linux/uptr.h>
#include <asm/byteorder.h>
#include <uapi/linux/fs.h>
@@ -1830,6 +1831,8 @@ struct file_operations {
ssize_t (*write) (struct file *, const char __user *, size_t, loff_t *);
ssize_t (*read_iter) (struct kiocb *, struct iov_iter *);
ssize_t (*write_iter) (struct kiocb *, struct iov_iter *);
+ ssize_t (*read_uptr) (struct file *, uptr_t, size_t, loff_t *);
+ ssize_t (*write_uptr) (struct file *, uptr_t, size_t, loff_t *);
int (*iopoll)(struct kiocb *kiocb, bool spin);
int (*iterate) (struct file *, struct dir_context *);
int (*iterate_shared) (struct file *, struct dir_context *);
--
2.26.2