net: af_unix: zerocopy stream bits
Subsystems (6)
v3 cover net: af_unix: zerocopy stream bits
This series implements zerocopy support for AF_UNIX SOCK_STREAM sockets.
Changelog in the specific patches. Thanks to all the reviewers!
Hannes Frederic Sowa (4):
net: skbuff: add skb_append_pagefrags and use it
net: af_unix: implement stream sendpage support
net: make skb_splice_bits more configureable
net: af_unix: implement splice for stream af_unix sockets
fs/splice.c | 1 + include/linux/skbuff.h | 14 ++- net/core/skbuff.c | 64 +++++++++---- net/ipv4/ip_output.c | 15 +--- net/ipv4/tcp.c | 5 +- net/unix/af_unix.c | 239 ++++++++++++++++++++++++++++++++++++++++++++----- 6 files changed, 284 insertions(+), 54 deletions(-)
Signature · 1 line
2.1.0
PATCH v3 1/4 net: skbuff: add skb_append_pagefrags and use it
---
v2:
* remove now unused variable i in ip_append_page (thanks, Cong!)
* switched to EXPORT_SYMBOL_GPL for skb_append_pagefrags
v3:
* unchanged
include/linux/skbuff.h | 3 +++ net/core/skbuff.c | 18 ++++++++++++++++++ net/ipv4/ip_output.c | 15 ++++----------- 3 files changed, 25 insertions(+), 11 deletions(-)
include/linux/skbuff.h +3 -0
@@ -860,6 +860,9 @@ int skb_append_datato_frags(struct sock *sk, struct sk_buff *skb, int len, int odd, struct sk_buff *skb), void *from, int length); +int skb_append_pagefrags (struct sk_buff *skb,struct page *page, +int offset,size_t size); +struct skb_seq_state {__u32 lower_offset;__u32 upper_offset;
net/core/skbuff.c +18 -0
@@ -2915,6 +2915,24 @@ int skb_append_datato_frags(struct sock *sk, struct sk_buff *skb, }EXPORT_SYMBOL (skb_append_datato_frags); +int skb_append_pagefrags (struct sk_buff *skb,struct page *page, +int offset,size_t size) +{ +int i =skb_shinfo (skb)->nr_frags; + +if (skb_can_coalesce (skb, i, page, offset)) { +skb_frag_size_add (&skb_shinfo (skb)->frags[i -1 ], size); + }else if (i <MAX_SKB_FRAGS ) { +get_page (page); +skb_fill_page_desc (skb, i, page, offset, size); + }else { +return -EMSGSIZE ; + } + +return 0 ; +} +EXPORT_SYMBOL_GPL (skb_append_pagefrags); +/** * skb_pull_rcsum - pull skb and update receive checksum * @skb: buffer to update
net/ipv4/ip_output.c +4 -11
@@ -1233,11 +1233,9 @@ ssize_t ip_append_page(struct sock *sk, struct flowi4 *fl4, struct page *page, }while (size >0 ) { -int i; - -if (skb_is_gso (skb)) +if (skb_is_gso (skb)) { len = size; -else { + }else {/* Check if the remaining data fits into current packet. */ len = mtu - skb->len;
@@ -1289,15 +1287,10 @@ ssize_t ip_append_page(struct sock *sk, struct flowi4 *fl4, struct page *page,continue ; } - i =skb_shinfo (skb)->nr_frags;if (len > size) len = size; -if (skb_can_coalesce (skb, i, page, offset)) { -skb_frag_size_add (&skb_shinfo (skb)->frags[i-1 ], len); - }else if (i <MAX_SKB_FRAGS ) { -get_page (page); -skb_fill_page_desc (skb, i, page, offset, len); - }else { + +if (skb_append_pagefrags (skb, page, offset, len)) { err = -EMSGSIZE ; goto error; }
Signature · 1 line
2.1.0
PATCH v3 2/4 net: af_unix: implement stream sendpage support
In reply to Hannes Frederic SowaThis patch implements sendpage support for AF_UNIX SOCK_STREAM sockets. This is also required for a complete splice implementation.
The implementation is a bit tricky because we append to already existing skbs and so have to hold unix_sk->readlock to protect the reading side from either advancing UNIXCB.consumed or freeing the skb at the socket receive tail.
---
v2:
* replaced skb_queue_tail with the unlocked version, __skb_queue_tail (thanks, Eric!)
* folded variable declaration and initialization (thanks, Cong!)
v3:
* unchanged
net/unix/af_unix.c | 99 +++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 98 insertions(+), 1 deletion(-)
net/unix/af_unix.c +98 -1
@@ -518,6 +518,8 @@ static int unix_ioctl(struct socket *, unsigned int, unsigned long);static int unix_shutdown (struct socket *,int );static int unix_stream_sendmsg (struct socket *,struct msghdr *,size_t );static int unix_stream_recvmsg (struct socket *,struct msghdr *,size_t ,int ); +static ssize_t unix_stream_sendpage (struct socket *,struct page *,int offset, +size_t size,int flags);static int unix_dgram_sendmsg (struct socket *,struct msghdr *,size_t );static int unix_dgram_recvmsg (struct socket *,struct msghdr *,size_t ,int ); static int unix_dgram_connect(struct socket *, struct sockaddr *,
@@ -558,7 +560,7 @@ static const struct proto_ops unix_stream_ops = { .sendmsg = unix_stream_sendmsg, .recvmsg = unix_stream_recvmsg, .mmap = sock_no_mmap, - .sendpage = sock_no_sendpage, + .sendpage = unix_stream_sendpage, .set_peek_off = unix_set_peek_off, };
@@ -1720,6 +1722,101 @@ out_err:return sent ? : err; } +static ssize_t unix_stream_sendpage (struct socket *socket,struct page *page, +int offset,size_t size,int flags) +{ +int err =0 ; +bool send_sigpipe = true; +struct sock *other, *sk = socket->sk; +struct sk_buff *skb, *newskb =NULL , *tail =NULL ; + +if (flags &MSG_OOB ) +return -EOPNOTSUPP ; + + other =unix_peer (sk); +if (!other || sk->sk_state !=TCP_ESTABLISHED ) +return -ENOTCONN ; + +if (false) { +alloc_skb: +unix_state_unlock (other); +mutex_unlock (&unix_sk (other)->readlock); + newskb =sock_alloc_send_pskb (sk,0 ,0 , flags &MSG_DONTWAIT , + &err,0 ); +if (!newskb) +return err; + } + +/* we must acquire readlock as we modify already present +* skbs in the sk_receive_queue and mess with skb->len +*/ + err =mutex_lock_interruptible (&unix_sk (other)->readlock); +if (err) { + err = flags &MSG_DONTWAIT ? -EAGAIN : -ERESTARTSYS ; + send_sigpipe = false; + goto err; + } + +if (sk->sk_shutdown &SEND_SHUTDOWN ) { + err = -EPIPE ; + goto err_unlock; + } + +unix_state_lock (other); + +if (sock_flag (other,SOCK_DEAD ) || + other->sk_shutdown &RCV_SHUTDOWN ) { + err = -EPIPE ; + goto err_state_unlock; + } + + skb =skb_peek_tail (&other->sk_receive_queue); +if (tail && tail == skb) { + skb = newskb; + }else if (!skb) { +if (newskb) + skb = newskb; +else + goto alloc_skb; + }else if (newskb) { +/* this is fast path, we don't necessarily need to +* call to kfree_skb even though with newskb == NULL +* this - does no harm +*/ +consume_skb (newskb); + } + +if (skb_append_pagefrags (skb, page, offset, size)) { + tail = skb; + goto alloc_skb; + } + + skb->len += size; + skb->data_len += size; + skb->truesize += size; +atomic_add (size, &sk->sk_wmem_alloc); + +if (newskb) +__skb_queue_tail (&other->sk_receive_queue, newskb); + +unix_state_unlock (other); +mutex_unlock (&unix_sk (other)->readlock); + + other->sk_data_ready (other); + +return size; + +err_state_unlock: +unix_state_unlock (other); +err_unlock: +mutex_unlock (&unix_sk (other)->readlock); +err: +kfree_skb (newskb); +if (send_sigpipe && !(flags &MSG_NOSIGNAL )) +send_sig (SIGPIPE , current,0 ); +return err; +} +static int unix_seqpacket_sendmsg (struct socket *sock,struct msghdr *msg,size_t len) {
Signature · 1 line
2.1.0
PATCH v3 3/4 net: make skb_splice_bits more configureable
In reply to Hannes Frederic SowaPrepare skb_splice_bits to be able to deal with AF_UNIX sockets.
AF_UNIX sockets don't use lock_sock/release_sock and thus we have to use a callback to make the locking and unlocking configureable.
---
v2:
* no changes
v3:
* no changes
include/linux/skbuff.h | 11 +++++++++-- net/core/skbuff.c | 45 ++++++++++++++++++++++++++++----------------- net/ipv4/tcp.c | 5 +++-- 3 files changed, 40 insertions(+), 21 deletions(-)
include/linux/skbuff.h +9 -2
@@ -35,6 +35,7 @@#include <linux/netdev_features.h> #include <linux/sched.h> #include <net/flow_dissector.h> +#include <linux/splice.h> /* A. Checksumming of received packets by device. *
@@ -2698,9 +2699,15 @@ int skb_copy_bits(const struct sk_buff *skb, int offset, void *to, int len);int skb_store_bits (struct sk_buff *skb,int offset,const void *from,int len);__wsum skb_copy_and_csum_bits (const struct sk_buff *skb,int offset,u8 *to,int len,__wsum csum); -int skb_splice_bits (struct sk_buff *skb,unsigned int offset, +ssize_t skb_socket_splice (struct sock *sk, +struct pipe_inode_info *pipe, +struct splice_pipe_desc *spd); +int skb_splice_bits (struct sk_buff *skb,struct sock *sk,unsigned int offset,struct pipe_inode_info *pipe,unsigned int len, -unsigned int flags); +unsigned int flags, +ssize_t (*splice_cb)(struct sock *, +struct pipe_inode_info *, +struct splice_pipe_desc *));void skb_copy_and_csum_dev (const struct sk_buff *skb,u8 *to);unsigned int skb_zerocopy_headlen (const struct sk_buff *from); int skb_zerocopy(struct sk_buff *to, struct sk_buff *from,
net/core/skbuff.c +28 -17
70 lines not on this page · raw message
net/ipv4/tcp.c +3 -2
@@ -694,8 +694,9 @@ static int tcp_splice_data_recv(read_descriptor_t *rd_desc, struct sk_buff *skb,struct tcp_splice_state *tss = rd_desc->arg.data;int ret; - ret =skb_splice_bits (skb, offset, tss->pipe,min (rd_desc->count, len), - tss->flags); + ret =skb_splice_bits (skb, skb->sk, offset, tss->pipe, +min (rd_desc->count, len), tss->flags, + skb_socket_splice);if (ret >0 ) rd_desc->count -= ret;return ret;
Signature · 1 line
2.1.0
PATCH v3 4/4 net: af_unix: implement splice for stream af_unix sockets
In reply to Hannes Frederic Sowaunix_stream_recvmsg is refactored to unix_stream_read_generic in this patch and enhanced to deal with pipe splicing. The refactoring is inneglible, we mostly have to deal with a non-existing struct msghdr argument.
---
v2:
* checked that CONFIG_UNIX=m still works, added necessary EXPORT_SYMBOL_GPLss (thanks, Eric!)
v3:
* removed __always_inline keyword (thanks, Eric!)
fs/splice.c | 1 + net/core/skbuff.c | 1 + net/unix/af_unix.c | 140 +++++++++++++++++++++++++++++++++++++++++++++-------- 3 files changed, 121 insertions(+), 21 deletions(-)
fs/splice.c +1 -0
@@ -261,6 +261,7 @@ ssize_t splice_to_pipe(struct pipe_inode_info *pipe,return ret; } +EXPORT_SYMBOL_GPL (splice_to_pipe);void spd_release_page (struct splice_pipe_desc *spd,unsigned int i) {
net/core/skbuff.c +1 -0
@@ -1942,6 +1942,7 @@ done:return ret; } +EXPORT_SYMBOL_GPL (skb_splice_bits);/** * skb_store_bits - store bits from kernel buffer to skb
net/unix/af_unix.c +119 -21
241 lines not on this page · raw message
Signature · 1 line
2.1.0
Re: [PATCH v3 net-next 4/4] net: af_unix: implement splice for stream af_unix sockets
Quoted 5 lines · Hannes Frederic Sowa
net/unix/af_unix.c +5
-0+ +static ssize_t skb_unix_socket_splice (struct sock *sk, +struct pipe_inode_info *pipe, +struct splice_pipe_desc *spd) +{
net/unix/af_unix.c again: · line 2215
Quoted 10 lines
net/unix/af_unix.c +10
-0+int ret; +struct unix_sock *u =unix_sk (sk); + +mutex_unlock (&u->readlock); + ret =splice_to_pipe (pipe, spd); +mutex_lock (&u->readlock); + +return ret; +} +
Hi Hannes
Since we release u->readlock, what prevents another thread to read() the same af_unix socket and consume the skb while we splice it ?
TCP stack has special code to take care of this possibility.
tcp_read_sock() :
used = recv_actor(desc, skb, offset, len);
if (used <= 0) {
if (!copied)
copied = used;
break;
} else if (used <= len) {
seq += used;
copied += used;
offset += used;
}
/* If recv_actor drops the lock (e.g. TCP splice
* receive) the skb pointer might be invalid when
* getting here: tcp_collapse might have deleted it
* while aggregating skbs from the socket queue.
*/
skb = tcp_recv_skb(sk, seq - 1, &offset);
if (!skb)
break;
/* TCP coalescing might have appended data to the skb.
* Try to splice more frags
*/
if (offset + 1 != skb->len)
continue;Re: [PATCH v3 net-next 4/4] net: af_unix: implement splice for stream af_unix sockets
Hallo,
Quoted 20 lines · Eric Dumazet
Quoted 5 lines · Hannes Frederic Sowa
net/unix/af_unix.c +5
-0+ +static ssize_t skb_unix_socket_splice (struct sock *sk, +struct pipe_inode_info *pipe, +struct splice_pipe_desc *spd) +{net/unix/af_unix.c again: · line 2215
Quoted 10 lines
net/unix/af_unix.c +10
-0+int ret; +struct unix_sock *u =unix_sk (sk); + +mutex_unlock (&u->readlock); + ret =splice_to_pipe (pipe, spd); +mutex_lock (&u->readlock); + +return ret; +} +Hi Hannes
Quoted 4 lines
Since we release
u->readlock, what prevents another thread toread()the sameaf_unixsocket and consume the skb while we splice it ?TCP stack has special code to take care of this possibility.
Hm, I do see the problem, yes. I left a window open for races happening.
Without having a closer look what spontaneously comes to my mind:
increment skb->users before splice_to_pipe, re-peek the socket queue after the splice, recheck if we have the same skb pointer in the front, and then consume_skb the skb properly and indicating a short read.
Let me try tomorrow if that works, but I fear we can read data multiple times with this fix, so I don't know if this is a proper fix. I have to check the TCP code closer. Thanks for the hint!
Thanks for the report,
Hannes