[PATCH 069/437] bpf: convert to read/write iterators

From: Jens Axboe
Date: Thu Apr 11 2024 - 11:54:28 EST


Signed-off-by: Jens Axboe <axboe@xxxxxxxxx>
---
kernel/bpf/bpf_iter.c | 14 +++++++-------
kernel/bpf/inode.c | 2 +-
kernel/bpf/syscall.c | 18 ++++++++----------
3 files changed, 16 insertions(+), 18 deletions(-)

diff --git a/kernel/bpf/bpf_iter.c b/kernel/bpf/bpf_iter.c
index 112581cf97e7..f2bbe98234df 100644
--- a/kernel/bpf/bpf_iter.c
+++ b/kernel/bpf/bpf_iter.c
@@ -91,10 +91,10 @@ static bool bpf_iter_support_resched(struct seq_file *seq)
* . assuming NULL ->llseek()
* . stop() may call bpf program, handling potential overflow there
*/
-static ssize_t bpf_seq_read(struct file *file, char __user *buf, size_t size,
- loff_t *ppos)
+static ssize_t bpf_seq_read_iter(struct kiocb *iocb, struct iov_iter *to)
{
- struct seq_file *seq = file->private_data;
+ struct seq_file *seq = iocb->ki_filp->private_data;
+ size_t size = iov_iter_count(to);
size_t n, offs, copied = 0;
int err = 0, num_objs = 0;
bool can_resched;
@@ -113,7 +113,7 @@ static ssize_t bpf_seq_read(struct file *file, char __user *buf, size_t size,

if (seq->count) {
n = min(seq->count, size);
- err = copy_to_user(buf, seq->buf + seq->from, n);
+ err = !copy_to_iter_full(seq->buf + seq->from, n, to);
if (err) {
err = -EFAULT;
goto done;
@@ -222,7 +222,7 @@ static ssize_t bpf_seq_read(struct file *file, char __user *buf, size_t size,
}

n = min(seq->count, size);
- err = copy_to_user(buf, seq->buf, n);
+ err = !copy_to_iter_full(seq->buf, n, to);
if (err) {
err = -EFAULT;
goto done;
@@ -234,7 +234,7 @@ static ssize_t bpf_seq_read(struct file *file, char __user *buf, size_t size,
if (!copied)
copied = err;
else
- *ppos += copied;
+ iocb->ki_pos += copied;
mutex_unlock(&seq->lock);
return copied;
}
@@ -284,7 +284,7 @@ static int iter_release(struct inode *inode, struct file *file)
const struct file_operations bpf_iter_fops = {
.open = iter_open,
.llseek = no_llseek,
- .read = bpf_seq_read,
+ .read_iter = bpf_seq_read_iter,
.release = iter_release,
};

diff --git a/kernel/bpf/inode.c b/kernel/bpf/inode.c
index af5d2ffadd70..5e35f1eb1dec 100644
--- a/kernel/bpf/inode.c
+++ b/kernel/bpf/inode.c
@@ -313,7 +313,7 @@ static int bpffs_map_release(struct inode *inode, struct file *file)
*/
static const struct file_operations bpffs_map_fops = {
.open = bpffs_map_open,
- .read = seq_read,
+ .read_iter = seq_read_iter,
.release = bpffs_map_release,
};

diff --git a/kernel/bpf/syscall.c b/kernel/bpf/syscall.c
index c287925471f6..385995dc69a0 100644
--- a/kernel/bpf/syscall.c
+++ b/kernel/bpf/syscall.c
@@ -872,8 +872,7 @@ static void bpf_map_show_fdinfo(struct seq_file *m, struct file *filp)
}
#endif

-static ssize_t bpf_dummy_read(struct file *filp, char __user *buf, size_t siz,
- loff_t *ppos)
+static ssize_t bpf_dummy_read(struct kiocb *iocb, struct iov_iter *to)
{
/* We need this handler such that alloc_file() enables
* f_mode with FMODE_CAN_READ.
@@ -881,8 +880,7 @@ static ssize_t bpf_dummy_read(struct file *filp, char __user *buf, size_t siz,
return -EINVAL;
}

-static ssize_t bpf_dummy_write(struct file *filp, const char __user *buf,
- size_t siz, loff_t *ppos)
+static ssize_t bpf_dummy_write(struct kiocb *iocb, struct iov_iter *to)
{
/* We need this handler such that alloc_file() enables
* f_mode with FMODE_CAN_WRITE.
@@ -991,8 +989,8 @@ const struct file_operations bpf_map_fops = {
.show_fdinfo = bpf_map_show_fdinfo,
#endif
.release = bpf_map_release,
- .read = bpf_dummy_read,
- .write = bpf_dummy_write,
+ .read_iter = bpf_dummy_read,
+ .write_iter = bpf_dummy_write,
.mmap = bpf_map_mmap,
.poll = bpf_map_poll,
.get_unmapped_area = bpf_get_unmapped_area,
@@ -2378,8 +2376,8 @@ const struct file_operations bpf_prog_fops = {
.show_fdinfo = bpf_prog_show_fdinfo,
#endif
.release = bpf_prog_release,
- .read = bpf_dummy_read,
- .write = bpf_dummy_write,
+ .read_iter = bpf_dummy_read,
+ .write_iter = bpf_dummy_write,
};

int bpf_prog_new_fd(struct bpf_prog *prog)
@@ -3142,8 +3140,8 @@ static const struct file_operations bpf_link_fops = {
.show_fdinfo = bpf_link_show_fdinfo,
#endif
.release = bpf_link_release,
- .read = bpf_dummy_read,
- .write = bpf_dummy_write,
+ .read_iter = bpf_dummy_read,
+ .write_iter = bpf_dummy_write,
};

static int bpf_link_alloc_id(struct bpf_link *link)
--
2.43.0