[RFC -next 04/10] splice: Add SPLICE_F_ZC and attach ubuf

Joe Damato
2025-03-19T00:15:43Z

Add the SPLICE_F_ZC flag and when it is set, allocate a ubuf and attach
it to generate zerocopy notifications.

Signed-off-by: Joe Damato <jdamato@fastly.com>
---
fs/splice.c | 20 ++++++++++++++++++++
include/linux/splice.h | 3 ++-
2 files changed, 22 insertions(+), 1 deletion(-)

diff --git a/fs/splice.c b/fs/splice.c
index 1f27ce6d1c34..6dc60f47f84e 100644
--- a/fs/splice.c
+++ b/fs/splice.c
@@ -875,6 +875,11 @@ static ssize_t splice_socket_generic(struct pipe_inode_info *pipe,
if (out->f_flags & O_NONBLOCK)
msg.msg_flags |= MSG_DONTWAIT;

+ if (unlikely(flags & SPLICE_F_ZC) && ubuf_info) {
+ msg.msg_flags = MSG_ZEROCOPY;
+ msg.msg_ubuf = ubuf_info;
+ }
+
iov_iter_bvec(&msg.msg_iter, ITER_SOURCE, bvec, bc,
len - remain);
ret = sock_sendmsg(sock, &msg);
@@ -1223,12 +1228,27 @@ static ssize_t do_splice_direct_actor(struct file *in, loff_t *ppos,
if (unlikely(out->f_flags & O_APPEND))
return -EINVAL;

+ if (unlikely(flags & SPLICE_F_ZC)) {
+ struct socket *sock = sock_from_file(out);
+ struct sock *sk = sock->sk;
+ struct ubuf_info *ubuf_info;
+
+ ubuf_info = msg_zerocopy_realloc(sk, len, NULL);
+ if (!ubuf_info)
+ return -ENOMEM;
+ sd.ubuf_info = ubuf_info;
+ }
+
ret = splice_direct_to_actor(in, &sd, actor);
if (ret > 0)
*ppos = sd.pos;

+ if (unlikely(flags & SPLICE_F_ZC))
+ refcount_dec(&sd.ubuf_info->refcnt);
+
return ret;
}
+
/**
* do_splice_direct - splices data directly between two files
* @in: file to splice from
diff --git a/include/linux/splice.h b/include/linux/splice.h
index 7477df3916e2..a88588cf2754 100644
--- a/include/linux/splice.h
+++ b/include/linux/splice.h
@@ -21,8 +21,9 @@
/* from/to, of course */
#define SPLICE_F_MORE (0x04) /* expect more data */
#define SPLICE_F_GIFT (0x08) /* pages passed in are a gift */
+#define SPLICE_F_ZC (0x10) /* generate zero copy notifications */

-#define SPLICE_F_ALL (SPLICE_F_MOVE|SPLICE_F_NONBLOCK|SPLICE_F_MORE|SPLICE_F_GIFT)
+#define SPLICE_F_ALL (SPLICE_F_MOVE|SPLICE_F_NONBLOCK|SPLICE_F_MORE|SPLICE_F_GIFT|SPLICE_F_ZC)

/*
* Passed to the actors
--
2.43.0