socket poll related cleanups
Subsystems (12)
A couple of cleanups I stumbled upon when studying the networking poll code.
PATCH 1/5 net: remove bogus RCU annotations on socket.wq
We never use RCU protection for it, just a lot of cargo-cult rcu_deference_protects calls.
Note that we do keep the kfree_rcu call for it, as the references through struct sock are RCU protected and thus might require a grace period before freeing.
--- include/linux/net.h | 2 +- include/net/sock.h | 2 +- net/socket.c | 10 ++++------ 3 files changed, 6 insertions(+), 8 deletions(-)
include/linux/net.h +1 -1
@@ -114,7 +114,7 @@ struct socket {unsigned long flags; -struct socket_wq __rcu *wq; +struct socket_wq *wq;struct file *file;struct sock *sk;
include/net/sock.h +1 -1
@@ -1725,7 +1725,7 @@ static inline void sock_graft(struct sock *sk, struct socket *parent) {WARN_ON (parent->sk);write_lock_bh (&sk->sk_callback_lock); - sk->sk_wq = parent->wq; +rcu_assign_pointer (sk->sk_wq, parent->wq); parent->sk = sk;sk_set_socket (sk, parent); sk->sk_uid =SOCK_INODE (parent)->i_uid;
net/socket.c +4 -6
@@ -251,7 +251,7 @@ static struct inode *sock_alloc_inode(struct super_block *sb)init_waitqueue_head (&wq->wait); wq->fasync_list =NULL ; wq->flags =0 ; -RCU_INIT_POINTER (ei->socket.wq, wq); + ei->socket.wq = wq; ei->socket.state =SS_UNCONNECTED ; ei->socket.flags =0 ;
@@ -265,11 +265,9 @@ static struct inode *sock_alloc_inode(struct super_block *sb)static void sock_destroy_inode (struct inode *inode) {struct socket_alloc *ei; -struct socket_wq *wq; ei = container_of(inode, struct socket_alloc, vfs_inode); - wq =rcu_dereference_protected (ei->socket.wq,1 ); -kfree_rcu (wq, rcu); +kfree_rcu (ei->socket.wq, rcu);kmem_cache_free (sock_inode_cachep, ei); }
@@ -603,7 +601,7 @@ static void __sock_release(struct socket *sock, struct inode *inode)module_put (owner); } -if (rcu_dereference_protected (sock->wq,1 )->fasync_list) +if (sock->wq->fasync_list)pr_err ("%s: fasync list not empty!\n" , __func__);if (!sock->file) {
@@ -1172,7 +1170,7 @@ static int sock_fasync(int fd, struct file *filp, int on)return -EINVAL ;lock_sock (sk); - wq =rcu_dereference_protected (sock->wq,lockdep_sock_is_held (sk)); + wq = sock->wq;fasync_helper (fd, filp, on, &wq->fasync_list);if (!wq->fasync_list)
Signature · 1 line
2.18.0
PATCH 2/5 net: simplify sock_poll_wait
In reply to Christoph HellwigThe wait_address argument is always directly derived from the filp argument, so remove it.
--- crypto/af_alg.c | 2 +- include/net/sock.h | 11 ++++++----- net/atm/common.c | 2 +- net/caif/caif_socket.c | 2 +- net/core/datagram.c | 2 +- net/dccp/proto.c | 2 +- net/ipv4/tcp.c | 2 +- net/iucv/af_iucv.c | 2 +- net/nfc/llcp_sock.c | 2 +- net/rxrpc/af_rxrpc.c | 2 +- net/smc/af_smc.c | 2 +- net/tipc/socket.c | 2 +- net/unix/af_unix.c | 4 ++-- 13 files changed, 19 insertions(+), 18 deletions(-)
crypto/af_alg.c +1 -1
@@ -1071,7 +1071,7 @@ __poll_t af_alg_poll(struct file *file, struct socket *sock,struct af_alg_ctx *ctx = ask->private;__poll_t mask; -sock_poll_wait (file,sk_sleep (sk), wait); +sock_poll_wait (file, wait); mask =0 ;if (!ctx->more || ctx->used)
include/net/sock.h +6 -5
@@ -1994,16 +1994,17 @@ static inline bool skwq_has_sleeper(struct socket_wq *wq)/** * sock_poll_wait - place memory barrier behind the poll_wait call. * @filp: file -* @wait_address: socket wait queue * @p: poll_table * * See the comments in the wq_has_sleeper function. */ -static inline void sock_poll_wait (struct file *filp, -wait_queue_head_t *wait_address,poll_table *p) +static inline void sock_poll_wait (struct file *filp,poll_table *p) { -if (!poll_does_not_wait (p) && wait_address) { -poll_wait (filp, wait_address, p); +struct socket *sock = filp->private_data; +wait_queue_head_t *wq =sk_sleep (sock->sk); + +if (!poll_does_not_wait (p) && wq) { +poll_wait (filp, wq, p);/* We need to be sure we are in sync with the * socket flags modification. *
net/atm/common.c +1 -1
@@ -653,7 +653,7 @@ __poll_t vcc_poll(struct file *file, struct socket *sock, poll_table *wait)struct atm_vcc *vcc;__poll_t mask; -sock_poll_wait (file,sk_sleep (sk), wait); +sock_poll_wait (file, wait); mask =0 ; vcc =ATM_SD (sock);
net/caif/caif_socket.c +1 -1
@@ -941,7 +941,7 @@ static __poll_t caif_poll(struct file *file,__poll_t mask; struct caifsock *cf_sk = container_of(sk, struct caifsock, sk); -sock_poll_wait (file,sk_sleep (sk), wait); +sock_poll_wait (file, wait); mask =0 ;/* exceptional events? */
net/core/datagram.c +1 -1
@@ -837,7 +837,7 @@ __poll_t datagram_poll(struct file *file, struct socket *sock,struct sock *sk = sock->sk;__poll_t mask; -sock_poll_wait (file,sk_sleep (sk), wait); +sock_poll_wait (file, wait); mask =0 ;/* exceptional events? */
net/dccp/proto.c +1 -1
@@ -325,7 +325,7 @@ __poll_t dccp_poll(struct file *file, struct socket *sock,__poll_t mask;struct sock *sk = sock->sk; -sock_poll_wait (file,sk_sleep (sk), wait); +sock_poll_wait (file, wait);if (sk->sk_state ==DCCP_LISTEN )return inet_csk_listen_poll (sk);
net/ipv4/tcp.c +1 -1
@@ -507,7 +507,7 @@ __poll_t tcp_poll(struct file *file, struct socket *sock, poll_table *wait)const struct tcp_sock *tp =tcp_sk (sk);int state; -sock_poll_wait (file,sk_sleep (sk), wait); +sock_poll_wait (file, wait); state =inet_sk_state_load (sk);if (state ==TCP_LISTEN )
net/iucv/af_iucv.c +1 -1
@@ -1494,7 +1494,7 @@ __poll_t iucv_sock_poll(struct file *file, struct socket *sock,struct sock *sk = sock->sk;__poll_t mask =0 ; -sock_poll_wait (file,sk_sleep (sk), wait); +sock_poll_wait (file, wait);if (sk->sk_state ==IUCV_LISTEN )return iucv_accept_poll (sk);
net/nfc/llcp_sock.c +1 -1
@@ -556,7 +556,7 @@ static __poll_t llcp_sock_poll(struct file *file, struct socket *sock,pr_debug ("%p\n" , sk); -sock_poll_wait (file,sk_sleep (sk), wait); +sock_poll_wait (file, wait);if (sk->sk_state ==LLCP_LISTEN )return llcp_accept_poll (sk);
net/rxrpc/af_rxrpc.c +1 -1
@@ -741,7 +741,7 @@ static __poll_t rxrpc_poll(struct file *file, struct socket *sock,struct rxrpc_sock *rx =rxrpc_sk (sk);__poll_t mask; -sock_poll_wait (file,sk_sleep (sk), wait); +sock_poll_wait (file, wait); mask =0 ;/* the socket is readable if there are any messages waiting on the Rx
net/smc/af_smc.c +1 -1
@@ -1351,7 +1351,7 @@ static __poll_t smc_poll(struct file *file, struct socket *sock, mask |=EPOLLERR ; }else {if (sk->sk_state !=SMC_CLOSED ) -sock_poll_wait (file,sk_sleep (sk), wait); +sock_poll_wait (file, wait);if (sk->sk_err) mask |=EPOLLERR ; if ((sk->sk_shutdown == SHUTDOWN_MASK) ||
net/tipc/socket.c +1 -1
@@ -716,7 +716,7 @@ static __poll_t tipc_poll(struct file *file, struct socket *sock,struct tipc_sock *tsk =tipc_sk (sk);__poll_t revents =0 ; -sock_poll_wait (file,sk_sleep (sk), wait); +sock_poll_wait (file, wait);if (sk->sk_shutdown &RCV_SHUTDOWN ) revents |=EPOLLRDHUP |EPOLLIN |EPOLLRDNORM ;
net/unix/af_unix.c +2 -2
@@ -2635,7 +2635,7 @@ static __poll_t unix_poll(struct file *file, struct socket *sock, poll_table *wastruct sock *sk = sock->sk;__poll_t mask; -sock_poll_wait (file,sk_sleep (sk), wait); +sock_poll_wait (file, wait); mask =0 ;/* exceptional events? */
@@ -2672,7 +2672,7 @@ static __poll_t unix_dgram_poll(struct file *file, struct socket *sock,unsigned int writable;__poll_t mask; -sock_poll_wait (file,sk_sleep (sk), wait); +sock_poll_wait (file, wait); mask =0 ;/* exceptional events? */
Signature · 1 line
2.18.0
PATCH 3/5 net: don not detour through struct sock to find the poll waitqueue
In reply to Christoph HellwigFor any open socket file descriptor sock->sk->sk_wq->wait will always point to sock->wq->wait. That means we can do the shorter dereference and removal a NULL check and don't have to not worry about any RCU protection.
--- include/net/sock.h | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-)
include/net/sock.h +2 -3
@@ -2001,10 +2001,9 @@ static inline bool skwq_has_sleeper(struct socket_wq *wq)static inline void sock_poll_wait (struct file *filp,poll_table *p) {struct socket *sock = filp->private_data; -wait_queue_head_t *wq =sk_sleep (sock->sk); -if (!poll_does_not_wait (p) && wq) { -poll_wait (filp, wq, p); +if (!poll_does_not_wait (p)) { +poll_wait (filp, &sock->wq->wait, p);/* We need to be sure we are in sync with the * socket flags modification. *
Signature · 1 line
2.18.0
PATCH 4/5 net: remove sock_poll_busy_loop
In reply to Christoph HellwigThere is no point in hiding this logic in a helper. Also remove the useless events != 0 check and only busy loop once we know we actually have a poll method.
--- include/net/busy_poll.h | 9 --------- net/socket.c | 5 ++++- 2 files changed, 4 insertions(+), 10 deletions(-)
include/net/busy_poll.h +0 -9
@@ -121,15 +121,6 @@ static inline void sk_busy_loop(struct sock *sk, int nonblock)#endif } -static inline void sock_poll_busy_loop (struct socket *sock,__poll_t events) -{ -if (sk_can_busy_loop (sock->sk) && - events && (events &POLL_BUSY_LOOP )) { -/* once, only if requested by syscall */ -sk_busy_loop (sock->sk,1 ); - } -} -/* if this socket can poll_ll, tell the system call */ static inline __poll_t sock_poll_busy_flag (struct socket *sock) {
net/socket.c +4 -1
@@ -1130,9 +1130,12 @@ static __poll_t sock_poll(struct file *file, poll_table *wait)struct socket *sock = file->private_data;__poll_t events =poll_requested_events (wait); -sock_poll_busy_loop (sock, events);if (!sock->ops->poll)return 0 ; + +/* poll once if requested by the syscall */ +if (sk_can_busy_loop (sock->sk) && (events &POLL_BUSY_LOOP )) +sk_busy_loop (sock->sk,1 );return sock->ops->poll (file, sock, wait) |sock_poll_busy_flag (sock); }
Signature · 1 line
2.18.0
PATCH 5/5 net: remove sock_poll_busy_flag
In reply to Christoph HellwigFold it into the only caller to make the code simpler and easier to read.
--- include/net/busy_poll.h | 6 ------ net/socket.c | 16 +++++++++++----- 2 files changed, 11 insertions(+), 11 deletions(-)
include/net/busy_poll.h +0 -6
@@ -121,12 +121,6 @@ static inline void sk_busy_loop(struct sock *sk, int nonblock)#endif } -/* if this socket can poll_ll, tell the system call */ -static inline __poll_t sock_poll_busy_flag (struct socket *sock) -{ -return sk_can_busy_loop (sock->sk) ?POLL_BUSY_LOOP :0 ; -} -/* used in the NIC receive handler to mark the skb */ static inline void skb_mark_napi_id (struct sk_buff *skb,struct napi_struct *napi)
net/socket.c +11 -5
@@ -1128,15 +1128,21 @@ EXPORT_SYMBOL(sock_create_lite);static __poll_t sock_poll (struct file *file,poll_table *wait) {struct socket *sock = file->private_data; -__poll_t events =poll_requested_events (wait); +__poll_t events =poll_requested_events (wait), flag =0 ;if (!sock->ops->poll)return 0 ; -/* poll once if requested by the syscall */ -if (sk_can_busy_loop (sock->sk) && (events &POLL_BUSY_LOOP )) -sk_busy_loop (sock->sk,1 ); -return sock->ops->poll (file, sock, wait) |sock_poll_busy_flag (sock); +if (sk_can_busy_loop (sock->sk)) { +/* poll once if requested by the syscall */ +if (events &POLL_BUSY_LOOP ) +sk_busy_loop (sock->sk,1 ); + +/* if this socket can poll_ll, tell the system call */ + flag =POLL_BUSY_LOOP ; + } + +return sock->ops->poll (file, sock, wait) | flag; }static int sock_mmap (struct file *file,struct vm_area_struct *vma)
Signature · 1 line
2.18.0
David Miller
maintainerRe: [PATCH 1/5] net: remove bogus RCU annotations on socket.wq
In reply to Christoph HellwigFrom: Christoph Hellwig <[email protected]>
Date: Fri, 27 Jul 2018 16:02:10 +0200
Quoted 8 lines
We never use RCU protection for it, just a lot of cargo-cult
rcu_deference_protectscalls.Note that we do keep the
kfree_rcucall for it, as the references through struct sock are RCU protected and thus might require a grace period before freeing.Signed-off-by Christoph Hellwig <[email protected]>
These were added by Eric Dumazet and I would never accuse him of cargo cult programming.
All of the rcu_dereference_protects() calls are legit, even though some of them use '1' as the protects condition because in fact we know the object is dead and gone through an RCU cycle at that point.
Let's skip this for now. The rest of your series looks fine so why don't you resubmit this series with just #2-#5?
Thanks.
Re: [PATCH 1/5] net: remove bogus RCU annotations on socket.wq
Quoted 10 lines · David Miller
From: Christoph Hellwig <[email protected]>
Date: Fri, 27 Jul 2018 16:02:10 +0200Quoted 7 lines
We never use RCU protection for it, just a lot of cargo-cult
rcu_deference_protectscalls.Note that we do keep the
kfree_rcucall for it, as the references through struct sock are RCU protected and thus might require a grace period before freeing.
Quoted 8 lines
Quoted 1 line
Signed-off-by Christoph Hellwig <[email protected]>These were added by Eric Dumazet and I would never accuse him of cargo cult programming.
All of the
rcu_dereference_protects()calls are legit, even though some of them use '1' as the protects condition because in fact we know the object is dead and gone through an RCU cycle at that point.
I disagree, but I'll resend it the patch with Eric and Paul in CC to settle the argument.
David Miller
maintainerRe: [PATCH 1/5] net: remove bogus RCU annotations on socket.wq
From: Christoph Hellwig <[email protected]>
Date: Mon, 30 Jul 2018 09:44:54 +0200
Quoted 16 lines · David Miller
Quoted 15 lines
From: Christoph Hellwig <[email protected]>
Date: Fri, 27 Jul 2018 16:02:10 +0200Quoted 8 lines
We never use RCU protection for it, just a lot of cargo-cult
rcu_deference_protectscalls.Note that we do keep the
kfree_rcucall for it, as the references through struct sock are RCU protected and thus might require a grace period before freeing.Signed-off-by Christoph Hellwig <[email protected]>These were added by Eric Dumazet and I would never accuse him of cargo cult programming.
Quoted 6 lines
Quoted 3 lines
All of the
rcu_dereference_protects()calls are legit, even though some of them use '1' as the protects condition because in fact we know the object is dead and gone through an RCU cycle at that point.I disagree, but I'll resend it the patch with Eric and Paul in CC to settle the argument.
Thank you.