[PATCH 004/437] fs: add simple_copy_{to,from}_iter() helpers

Jens Axboe
2024-04-11T15:31:36Z

While doing so, rename the networking helper that has the same name.

Signed-off-by: Jens Axboe <axboe@kernel.dk>
---
fs/libfs.c | 73 ++++++++++++++++++++++++++++++++++++++++++++-
include/linux/fs.h | 5 ++++
net/core/datagram.c | 10 +++----
3 files changed, 82 insertions(+), 6 deletions(-)

diff --git a/fs/libfs.c b/fs/libfs.c
index 3a6f2cb364f8..074cfacc377f 100644
--- a/fs/libfs.c
+++ b/fs/libfs.c
@@ -1102,13 +1102,84 @@ ssize_t simple_write_to_buffer(void *to, size_t available, loff_t *ppos,
if (count > available - pos)
count = available - pos;
res = copy_from_user(to + pos, from, count);
+ if (res < 0)
+ return res;
+ count -= res;
+ *ppos = pos + count;
+ return count;
+}
+EXPORT_SYMBOL(simple_write_to_buffer);
+
+/**
+ * simple_copy_to_iter - copy data userspace to iter
+ * @from: the buffer to read from
+ * @ppos: the current position in the buffer
+ * @count: the maximum number of bytes to copy
+ * @to: the iov_iter to copy to
+ *
+ * The simple_copy_to_iter() function reads up to @count bytes from the
+ * buffer @from at offset @ppos into the user space address starting at @to.
+ *
+ * On success, the number of bytes copied is returned, or negative value is
+ * returned on error.
+ **/
+ssize_t simple_copy_to_iter(const void *from, loff_t *ppos, size_t count,
+ struct iov_iter *to)
+{
+ size_t available = iov_iter_count(to);
+ loff_t pos = *ppos;
+ size_t ret;
+
+ if (pos < 0)
+ return -EINVAL;
+ if (pos >= available || !count)
+ return 0;
+ if (count > available - pos)
+ count = available - pos;
+ ret = copy_to_iter(from + pos, count, to);
+ if (ret < 0)
+ return ret;
+ count -= ret;
+ *ppos = pos + count;
+ return count;
+}
+EXPORT_SYMBOL(simple_copy_to_iter);
+
+/**
+ * simple_copy_from_iter - copy data from iter to user space buffer
+ * @to: the buffer to write to
+ * @available: the size of the buffer
+ * @ppos: the current position in the buffer
+ * @from: the user space buffer to read from
+ * @count: the maximum number of bytes to read
+ *
+ * The simple_write_to_buffer() function reads up to @count bytes from the user
+ * space address starting at @from into the buffer @to at offset @ppos.
+ *
+ * On success, the number of bytes written is returned and the offset @ppos is
+ * advanced by this number, or negative value is returned on error.
+ **/
+ssize_t simple_copy_from_iter(void *to, loff_t *ppos, size_t count,
+ struct iov_iter *from)
+{
+ size_t available = iov_iter_count(to);
+ loff_t pos = *ppos;
+ size_t res;
+
+ if (pos < 0)
+ return -EINVAL;
+ if (pos >= available || !count)
+ return 0;
+ if (count > available - pos)
+ count = available - pos;
+ res = copy_from_iter(to + pos, count, from);
if (res == count)
return -EFAULT;
count -= res;
*ppos = pos + count;
return count;
}
-EXPORT_SYMBOL(simple_write_to_buffer);
+EXPORT_SYMBOL(simple_copy_from_iter);

/**
* memory_read_from_buffer - copy data from the buffer
diff --git a/include/linux/fs.h b/include/linux/fs.h
index 72de0b1d5647..f75049fa1d50 100644
--- a/include/linux/fs.h
+++ b/include/linux/fs.h
@@ -3353,6 +3353,11 @@ extern ssize_t simple_read_from_buffer(void __user *to, size_t count,
extern ssize_t simple_write_to_buffer(void *to, size_t available, loff_t *ppos,
const void __user *from, size_t count);

+ssize_t simple_copy_to_iter(const void *from, loff_t *ppos, size_t count,
+ struct iov_iter *to);
+ssize_t simple_copy_from_iter(void *to, loff_t *ppos, size_t count,
+ struct iov_iter *from);
+
struct offset_ctx {
struct maple_tree mt;
unsigned long next_offset;
diff --git a/net/core/datagram.c b/net/core/datagram.c
index a8b625abe242..3dfdddeaf695 100644
--- a/net/core/datagram.c
+++ b/net/core/datagram.c
@@ -399,7 +399,7 @@ int skb_kill_datagram(struct sock *sk, struct sk_buff *skb, unsigned int flags)
}
EXPORT_SYMBOL(skb_kill_datagram);

-INDIRECT_CALLABLE_DECLARE(static size_t simple_copy_to_iter(const void *addr,
+INDIRECT_CALLABLE_DECLARE(static size_t __simple_copy_to_iter(const void *addr,
size_t bytes,
void *data __always_unused,
struct iov_iter *i));
@@ -417,7 +417,7 @@ static int __skb_datagram_iter(const struct sk_buff *skb, int offset,
if (copy > 0) {
if (copy > len)
copy = len;
- n = INDIRECT_CALL_1(cb, simple_copy_to_iter,
+ n = INDIRECT_CALL_1(cb, __simple_copy_to_iter,
skb->data + offset, copy, data, to);
offset += n;
if (n != copy)
@@ -440,7 +440,7 @@ static int __skb_datagram_iter(const struct sk_buff *skb, int offset,

if (copy > len)
copy = len;
- n = INDIRECT_CALL_1(cb, simple_copy_to_iter,
+ n = INDIRECT_CALL_1(cb, __simple_copy_to_iter,
vaddr + skb_frag_off(frag) + offset - start,
copy, data, to);
kunmap(page);
@@ -526,7 +526,7 @@ int skb_copy_and_hash_datagram_iter(const struct sk_buff *skb, int offset,
}
EXPORT_SYMBOL(skb_copy_and_hash_datagram_iter);

-static size_t simple_copy_to_iter(const void *addr, size_t bytes,
+static size_t __simple_copy_to_iter(const void *addr, size_t bytes,
void *data __always_unused, struct iov_iter *i)
{
return copy_to_iter(addr, bytes, i);
@@ -544,7 +544,7 @@ int skb_copy_datagram_iter(const struct sk_buff *skb, int offset,
{
trace_skb_copy_datagram_iovec(skb, len);
return __skb_datagram_iter(skb, offset, to, len, false,
- simple_copy_to_iter, NULL);
+ __simple_copy_to_iter, NULL);
}
EXPORT_SYMBOL(skb_copy_datagram_iter);

--
2.43.0