vsock: keep poll shutdown state consistent
Subsystems (4)
PATCH 1/1 vsock: keep poll shutdown state consistent
vsock_poll() reads vsk->peer_shutdown before taking the socket lock to set EPOLLHUP and EPOLLRDHUP, then reads it again under the lock to report EOF readability. A shutdown packet can update peer_shutdown while poll is waiting for the lock, so one poll invocation can report EPOLLIN without the corresponding HUP/RDHUP bits.
Keep non-connectible sockets on a single lockless READ_ONCE() snapshot. For connectible sockets, defer shutdown-derived poll bits until after lock_sock() and use one READ_ONCE() snapshot for both EOF readability and HUP/RDHUP. This preserves shutdowns that arrive before the lock is acquired and keeps all peer-shutdown-derived bits consistent for a poll pass.
--- net/vmw_vsock/af_vsock.c | 40 ++++++++++++++++++++++++++-------------- 1 file changed, 26 insertions(+), 14 deletions(-)
net/vmw_vsock/af_vsock.c +26 -14
@@ -1122,6 +1122,25 @@ static int vsock_shutdown(struct socket *sock, int mode)return err; } +static __poll_t vsock_poll_shutdown (struct sock *sk,u32 peer_shutdown) +{ +__poll_t mask =0 ; + +/* INET sockets treat local write shutdown and peer write shutdown as a +* case of EPOLLHUP set. +*/ +if (sk->sk_shutdown ==SHUTDOWN_MASK || + ((sk->sk_shutdown &SEND_SHUTDOWN ) && + (peer_shutdown &SEND_SHUTDOWN ))) + mask |=EPOLLHUP ; + +if (sk->sk_shutdown &RCV_SHUTDOWN || + peer_shutdown &SEND_SHUTDOWN ) + mask |=EPOLLRDHUP ; + +return mask; +} +static __poll_t vsock_poll (struct file *file,struct socket *sock,poll_table *wait) {
@@ -1139,19 +1158,9 @@ static __poll_t vsock_poll(struct file *file, struct socket *sock,/* Signify that there has been an error on this socket. */ mask |=EPOLLERR ; -/* INET sockets treat local write shutdown and peer write shutdown as a -* case of EPOLLHUP set. -*/ -if ((sk->sk_shutdown ==SHUTDOWN_MASK ) || - ((sk->sk_shutdown &SEND_SHUTDOWN ) && - (vsk->peer_shutdown &SEND_SHUTDOWN ))) { - mask |=EPOLLHUP ; - } - -if (sk->sk_shutdown &RCV_SHUTDOWN || - vsk->peer_shutdown &SEND_SHUTDOWN ) { - mask |=EPOLLRDHUP ; - } +if (!sock_type_connectible (sk->sk_type)) + mask |=vsock_poll_shutdown (sk, +READ_ONCE (vsk->peer_shutdown));if (sk_is_readable (sk)) mask |=EPOLLIN |EPOLLRDNORM ;
@@ -1171,6 +1180,7 @@ static __poll_t vsock_poll(struct file *file, struct socket *sock, }else if (sock_type_connectible (sk->sk_type)) {const struct vsock_transport *transport; +u32 peer_shutdown;lock_sock (sk);
@@ -1203,10 +1213,12 @@ static __poll_t vsock_poll(struct file *file, struct socket *sock,* terminated should also be considered read, and we check the * shutdown flag for that. */ + peer_shutdown =READ_ONCE (vsk->peer_shutdown);if (sk->sk_shutdown &RCV_SHUTDOWN || - vsk->peer_shutdown &SEND_SHUTDOWN ) { + peer_shutdown &SEND_SHUTDOWN ) { mask |=EPOLLIN |EPOLLRDNORM ; } + mask |=vsock_poll_shutdown (sk, peer_shutdown);/* Connected sockets that can produce data can be written. */ if (transport && sk->sk_state ==TCP_ESTABLISHED ) {
Signature · 1 line
2.43.0
Stefano Garzarella
maintainerQuoted 7 lines · Ziyu Zhang
vsock_poll()readsvsk->peer_shutdownbefore taking the socket lock to set EPOLLHUP and EPOLLRDHUP, then reads it again under the lock to report EOF readability. A shutdown packet can updatepeer_shutdownwhile poll is waiting for the lock, so one poll invocation can report EPOLLIN without the corresponding HUP/RDHUP bits.Keep non-connectible sockets on a single lockless
READ_ONCE()
Should this be paired with WRITE_ONCE() on writers?
Quoted 52 lines
snapshot. For connectible sockets, defer shutdown-derived poll bits until after
lock_sock()and use oneREAD_ONCE()snapshot for both EOF readability and HUP/RDHUP. This preserves shutdowns that arrive before the lock is acquired and keeps all peer-shutdown-derived bits consistent for a poll pass.Fixes d021c344051a ("VSOCK: Introduce VM Sockets")Signed-off-by Ziyu Zhang <[email protected]>---
net/vmw_vsock/af_vsock.c| 40 ++++++++++++++++++++++++++--------------1 file changed, 26 insertions(+), 14 deletions(-)net/vmw_vsock/af_vsock.c +19
-6@@ -1122,6 +1122,25 @@ static int vsock_shutdown(struct socket *sock, int mode)return err; } +static __poll_t vsock_poll_shutdown (struct sock *sk,u32 peer_shutdown) +{ +__poll_t mask =0 ; + +/* INET sockets treat local write shutdown and peer write shutdown as a +* case of EPOLLHUP set. +*/ +if (sk->sk_shutdown ==SHUTDOWN_MASK || + ((sk->sk_shutdown &SEND_SHUTDOWN ) && + (peer_shutdown &SEND_SHUTDOWN ))) + mask |=EPOLLHUP ; + +if (sk->sk_shutdown &RCV_SHUTDOWN || + peer_shutdown &SEND_SHUTDOWN ) + mask |=EPOLLRDHUP ; + +return mask; +} +static __poll_t vsock_poll (struct file *file,struct socket *sock,poll_table *wait) {@@ -1139,19 +1158,9 @@ static __poll_t vsock_poll(struct file *file, struct socket *sock,/* Signify that there has been an error on this socket. */ mask |=EPOLLERR ; -/* INET sockets treat local write shutdown and peer write shutdown as a -* case of EPOLLHUP set. -*/ -if ((sk->sk_shutdown ==SHUTDOWN_MASK ) || - ((sk->sk_shutdown &SEND_SHUTDOWN ) && - (vsk->peer_shutdown &SEND_SHUTDOWN ))) {
net/vmw_vsock/af_vsock.c static __poll_t vsock_poll(struct file *file, struct socket *sock, · line 1163
Quoted 10 lines
net/vmw_vsock/af_vsock.c +3
-7- mask |=EPOLLHUP ; - } - -if (sk->sk_shutdown &RCV_SHUTDOWN || - vsk->peer_shutdown &SEND_SHUTDOWN ) { - mask |=EPOLLRDHUP ; - } +if (!sock_type_connectible (sk->sk_type)) + mask |=vsock_poll_shutdown (sk, +READ_ONCE (vsk->peer_shutdown));
Can we move this in the if (sock->type == SOCK_DGRAM) branch ?
Not a strong opinion about that, but in any case IMO we should add a comment here to explain why we are doing only for not connectible sockets.
That said, if we use WRITE_ONCE in the writers, do we really need to move this after the lock_sock for the connectable ones?
Quoted 12 lines
net/vmw_vsock/af_vsock.c +1
-0if (sk_is_readable (sk)) mask |=EPOLLIN |EPOLLRDNORM ;@@ -1171,6 +1180,7 @@ static __poll_t vsock_poll(struct file *file, struct socket *sock, }else if (sock_type_connectible (sk->sk_type)) {const struct vsock_transport *transport; +u32 peer_shutdown;lock_sock (sk);
net/vmw_vsock/af_vsock.c static __poll_t vsock_poll(struct file *file, struct socket *sock, · line 1221
Quoted 10 lines
net/vmw_vsock/af_vsock.c +3
-1@@ -1203,10 +1213,12 @@ static __poll_t vsock_poll(struct file *file, struct socket *sock,* terminated should also be considered read, and we check the * shutdown flag for that. */ + peer_shutdown =READ_ONCE (vsk->peer_shutdown);if (sk->sk_shutdown &RCV_SHUTDOWN || - vsk->peer_shutdown &SEND_SHUTDOWN ) { + peer_shutdown &SEND_SHUTDOWN ) { mask |=EPOLLIN |EPOLLRDNORM ; } + mask |=vsock_poll_shutdown (sk, peer_shutdown);
nit: to keep the order the same as before, I would move this call just before this if block, but I don't think it makes any difference in the end.
Quoted 6 lines
net/vmw_vsock/af_vsock.c +0
-0/* Connected sockets that can produce data can be written. */ if (transport && sk->sk_state ==TCP_ESTABLISHED ) {Signature · 1 line
2.43.0
Quoted 11 lines · Stefano Garzarella
Quoted 7 lines · Ziyu Zhang
vsock_poll()readsvsk->peer_shutdownbefore taking the socket lock to set EPOLLHUP and EPOLLRDHUP, then reads it again under the lock to report EOF readability. A shutdown packet can updatepeer_shutdownwhile poll is waiting for the lock, so one poll invocation can report EPOLLIN without the corresponding HUP/RDHUP bits.Keep non-connectible sockets on a single lockless
READ_ONCE()Should this be paired with
WRITE_ONCE()on writers?
Yes, since the poll path uses lockless READ_ONCE() snapshots of peer_shutdown, the writer side should be annotated with WRITE_ONCE() as well. I will add that in v2.
Quoted 66 lines
Quoted 53 lines
snapshot. For connectible sockets, defer shutdown-derived poll bits until after
lock_sock()and use oneREAD_ONCE()snapshot for both EOF readability and HUP/RDHUP. This preserves shutdowns that arrive before the lock is acquired and keeps all peer-shutdown-derived bits consistent for a poll pass.Fixes d021c344051a ("VSOCK: Introduce VM Sockets")Signed-off-by Ziyu Zhang <[email protected]>---
net/vmw_vsock/af_vsock.c| 40 ++++++++++++++++++++++++++--------------1 file changed, 26 insertions(+), 14 deletions(-)net/vmw_vsock/af_vsock.c +19
-6@@ -1122,6 +1122,25 @@ static int vsock_shutdown(struct socket *sock, int mode)return err; } +static __poll_t vsock_poll_shutdown (struct sock *sk,u32 peer_shutdown) +{ +__poll_t mask =0 ; + +/* INET sockets treat local write shutdown and peer write shutdown as a +* case of EPOLLHUP set. +*/ +if (sk->sk_shutdown ==SHUTDOWN_MASK || + ((sk->sk_shutdown &SEND_SHUTDOWN ) && + (peer_shutdown &SEND_SHUTDOWN ))) + mask |=EPOLLHUP ; + +if (sk->sk_shutdown &RCV_SHUTDOWN || + peer_shutdown &SEND_SHUTDOWN ) + mask |=EPOLLRDHUP ; + +return mask; +} +static __poll_t vsock_poll (struct file *file,struct socket *sock,poll_table *wait) {@@ -1139,19 +1158,9 @@ static __poll_t vsock_poll(struct file *file, struct socket *sock,/* Signify that there has been an error on this socket. */ mask |=EPOLLERR ; -/* INET sockets treat local write shutdown and peer write shutdown as a -* case of EPOLLHUP set. -*/ -if ((sk->sk_shutdown ==SHUTDOWN_MASK ) || - ((sk->sk_shutdown &SEND_SHUTDOWN ) && - (vsk->peer_shutdown &SEND_SHUTDOWN ))) {net/vmw_vsock/af_vsock.c static __poll_t vsock_poll(struct file *file, struct socket *sock, · line 1163
Quoted 10 lines
net/vmw_vsock/af_vsock.c +3
-7- mask |=EPOLLHUP ; - } - -if (sk->sk_shutdown &RCV_SHUTDOWN || - vsk->peer_shutdown &SEND_SHUTDOWN ) { - mask |=EPOLLRDHUP ; - } +if (!sock_type_connectible (sk->sk_type)) + mask |=vsock_poll_shutdown (sk, +READ_ONCE (vsk->peer_shutdown));Can we move this in the
if (sock->type == SOCK_DGRAM)branch ?
Quoted 6 lines
Not a strong opinion about that, but in any case IMO we should add a comment here to explain why we are doing only for not connectible sockets.
That said, if we use
WRITE_ONCEin the writers, do we really need to move this after thelock_sockfor the connectable ones?
Yes, I will move the non-connectible handling into the SOCK_DGRAM branch and add a comment there.
For connectible sockets, my current understanding is that READ_ONCE()/WRITE_ONCE() make the individual lockless accesses explicit, but they do not make two separate reads in one vsock_poll() invocation observe the same peer_shutdown value. So I still think using one peer_shutdown snapshot after lock_sock() is useful for keeping the returned mask internally consistent. Please let me know if you think WRITE_ONCE() is enough for this case.
Quoted 12 lines
Quoted 12 lines
net/vmw_vsock/af_vsock.c +1
-0if (sk_is_readable (sk)) mask |=EPOLLIN |EPOLLRDNORM ;@@ -1171,6 +1180,7 @@ static __poll_t vsock_poll(struct file *file, struct socket *sock, }else if (sock_type_connectible (sk->sk_type)) {const struct vsock_transport *transport; +u32 peer_shutdown;lock_sock (sk);
Quoted 15 lines
net/vmw_vsock/af_vsock.c static __poll_t vsock_poll(struct file *file, struct socket *sock, · line 1221
Quoted 11 lines
net/vmw_vsock/af_vsock.c +3
-1@@ -1203,10 +1213,12 @@ static __poll_t vsock_poll(struct file *file, struct socket *sock,* terminated should also be considered read, and we check the * shutdown flag for that. */ + peer_shutdown =READ_ONCE (vsk->peer_shutdown);if (sk->sk_shutdown &RCV_SHUTDOWN || - vsk->peer_shutdown &SEND_SHUTDOWN ) { + peer_shutdown &SEND_SHUTDOWN ) { mask |=EPOLLIN |EPOLLRDNORM ; } + mask |=vsock_poll_shutdown (sk, peer_shutdown);nit: to keep the order the same as before, I would move this call just before this
ifblock, but I don't think it makes any difference in the end.
Sure, I will restore the previous ordering in v2.
Thanks,
Ziyu
Quoted 8 lines
Quoted 7 lines
net/vmw_vsock/af_vsock.c +0
-1/* Connected sockets that can produce data can be written. */ if (transport && sk->sk_state ==TCP_ESTABLISHED ) { --2.43.0
Stefano Garzarella
maintainerQuoted 93 lines · ziyu zhang
Quoted 11 lines · Stefano Garzarella
Quoted 7 lines · Ziyu Zhang
vsock_poll()readsvsk->peer_shutdownbefore taking the socket lock to set EPOLLHUP and EPOLLRDHUP, then reads it again under the lock to report EOF readability. A shutdown packet can updatepeer_shutdownwhile poll is waiting for the lock, so one poll invocation can report EPOLLIN without the corresponding HUP/RDHUP bits.Keep non-connectible sockets on a single lockless
READ_ONCE()Should this be paired with
WRITE_ONCE()on writers?Yes, since the poll path uses lockless
READ_ONCE()snapshots ofpeer_shutdown, the writer side should be annotated withWRITE_ONCE()as well. I will add that in v2.Quoted 66 lines
Quoted 53 lines
snapshot. For connectible sockets, defer shutdown-derived poll bits until after
lock_sock()and use oneREAD_ONCE()snapshot for both EOF readability and HUP/RDHUP. This preserves shutdowns that arrive before the lock is acquired and keeps all peer-shutdown-derived bits consistent for a poll pass.Fixes d021c344051a ("VSOCK: Introduce VM Sockets")Signed-off-by Ziyu Zhang <[email protected]>---
net/vmw_vsock/af_vsock.c| 40 ++++++++++++++++++++++++++--------------1 file changed, 26 insertions(+), 14 deletions(-)net/vmw_vsock/af_vsock.c +19
-6@@ -1122,6 +1122,25 @@ static int vsock_shutdown(struct socket *sock, int mode)return err; } +static __poll_t vsock_poll_shutdown (struct sock *sk,u32 peer_shutdown) +{ +__poll_t mask =0 ; + +/* INET sockets treat local write shutdown and peer write shutdown as a +* case of EPOLLHUP set. +*/ +if (sk->sk_shutdown ==SHUTDOWN_MASK || + ((sk->sk_shutdown &SEND_SHUTDOWN ) && + (peer_shutdown &SEND_SHUTDOWN ))) + mask |=EPOLLHUP ; + +if (sk->sk_shutdown &RCV_SHUTDOWN || + peer_shutdown &SEND_SHUTDOWN ) + mask |=EPOLLRDHUP ; + +return mask; +} +static __poll_t vsock_poll (struct file *file,struct socket *sock,poll_table *wait) {@@ -1139,19 +1158,9 @@ static __poll_t vsock_poll(struct file *file, struct socket *sock,/* Signify that there has been an error on this socket. */ mask |=EPOLLERR ; -/* INET sockets treat local write shutdown and peer write shutdown as a -* case of EPOLLHUP set. -*/ -if ((sk->sk_shutdown ==SHUTDOWN_MASK ) || - ((sk->sk_shutdown &SEND_SHUTDOWN ) && - (vsk->peer_shutdown &SEND_SHUTDOWN ))) {net/vmw_vsock/af_vsock.c static __poll_t vsock_poll(struct file *file, struct socket *sock, · line 1163
Quoted 10 lines
net/vmw_vsock/af_vsock.c +3
-7- mask |=EPOLLHUP ; - } - -if (sk->sk_shutdown &RCV_SHUTDOWN || - vsk->peer_shutdown &SEND_SHUTDOWN ) { - mask |=EPOLLRDHUP ; - } +if (!sock_type_connectible (sk->sk_type)) + mask |=vsock_poll_shutdown (sk, +READ_ONCE (vsk->peer_shutdown));Can we move this in the
if (sock->type == SOCK_DGRAM)branch ?Quoted 6 lines
Not a strong opinion about that, but in any case IMO we should add a comment here to explain why we are doing only for not connectible sockets.
That said, if we use
WRITE_ONCEin the writers, do we really need to move this after thelock_sockfor the connectable ones?Yes, I will move the non-connectible handling into the
SOCK_DGRAMbranch and add a comment there.
Quoted 7 lines
For connectible sockets, my current understanding is that READ_ONCE()/WRITE_ONCE() make the individual lockless accesses explicit, but they do not make two separate reads in one
vsock_poll()invocation observe the samepeer_shutdownvalue. So I still think using onepeer_shutdownsnapshot afterlock_sock()is useful for keeping the returned mask internally consistent. Please let me know if you thinkWRITE_ONCE()is enough for this case.
What will be the issue of "do not make two separate reads in one vsock_poll() invocation observe the same peer_shutdown value." ?
In any case, I'm not against it; I just want to understand the issue better.
Thanks,
Stefano
Quoted 103 lines · Stefano Garzarella
Quoted 93 lines · ziyu zhang
Quoted 11 lines · Stefano Garzarella
Quoted 7 lines · Ziyu Zhang
vsock_poll()readsvsk->peer_shutdownbefore taking the socket lock to set EPOLLHUP and EPOLLRDHUP, then reads it again under the lock to report EOF readability. A shutdown packet can updatepeer_shutdownwhile poll is waiting for the lock, so one poll invocation can report EPOLLIN without the corresponding HUP/RDHUP bits.Keep non-connectible sockets on a single lockless
READ_ONCE()Should this be paired with
WRITE_ONCE()on writers?Yes, since the poll path uses lockless
READ_ONCE()snapshots ofpeer_shutdown, the writer side should be annotated withWRITE_ONCE()as well. I will add that in v2.Quoted 66 lines
Quoted 53 lines
snapshot. For connectible sockets, defer shutdown-derived poll bits until after
lock_sock()and use oneREAD_ONCE()snapshot for both EOF readability and HUP/RDHUP. This preserves shutdowns that arrive before the lock is acquired and keeps all peer-shutdown-derived bits consistent for a poll pass.Fixes d021c344051a ("VSOCK: Introduce VM Sockets")Signed-off-by Ziyu Zhang <[email protected]>---
net/vmw_vsock/af_vsock.c| 40 ++++++++++++++++++++++++++--------------1 file changed, 26 insertions(+), 14 deletions(-)net/vmw_vsock/af_vsock.c +19
-6@@ -1122,6 +1122,25 @@ static int vsock_shutdown(struct socket *sock, int mode)return err; } +static __poll_t vsock_poll_shutdown (struct sock *sk,u32 peer_shutdown) +{ +__poll_t mask =0 ; + +/* INET sockets treat local write shutdown and peer write shutdown as a +* case of EPOLLHUP set. +*/ +if (sk->sk_shutdown ==SHUTDOWN_MASK || + ((sk->sk_shutdown &SEND_SHUTDOWN ) && + (peer_shutdown &SEND_SHUTDOWN ))) + mask |=EPOLLHUP ; + +if (sk->sk_shutdown &RCV_SHUTDOWN || + peer_shutdown &SEND_SHUTDOWN ) + mask |=EPOLLRDHUP ; + +return mask; +} +static __poll_t vsock_poll (struct file *file,struct socket *sock,poll_table *wait) {@@ -1139,19 +1158,9 @@ static __poll_t vsock_poll(struct file *file, struct socket *sock,/* Signify that there has been an error on this socket. */ mask |=EPOLLERR ; -/* INET sockets treat local write shutdown and peer write shutdown as a -* case of EPOLLHUP set. -*/ -if ((sk->sk_shutdown ==SHUTDOWN_MASK ) || - ((sk->sk_shutdown &SEND_SHUTDOWN ) && - (vsk->peer_shutdown &SEND_SHUTDOWN ))) {net/vmw_vsock/af_vsock.c static __poll_t vsock_poll(struct file *file, struct socket *sock, · line 1163
Quoted 10 lines
net/vmw_vsock/af_vsock.c +3
-7- mask |=EPOLLHUP ; - } - -if (sk->sk_shutdown &RCV_SHUTDOWN || - vsk->peer_shutdown &SEND_SHUTDOWN ) { - mask |=EPOLLRDHUP ; - } +if (!sock_type_connectible (sk->sk_type)) + mask |=vsock_poll_shutdown (sk, +READ_ONCE (vsk->peer_shutdown));Can we move this in the
if (sock->type == SOCK_DGRAM)branch ?Quoted 6 lines
Not a strong opinion about that, but in any case IMO we should add a comment here to explain why we are doing only for not connectible sockets.
That said, if we use
WRITE_ONCEin the writers, do we really need to move this after thelock_sockfor the connectable ones?Yes, I will move the non-connectible handling into the
SOCK_DGRAMbranch and add a comment there.Quoted 7 lines
For connectible sockets, my current understanding is that READ_ONCE()/WRITE_ONCE() make the individual lockless accesses explicit, but they do not make two separate reads in one
vsock_poll()invocation observe the samepeer_shutdownvalue. So I still think using onepeer_shutdownsnapshot afterlock_sock()is useful for keeping the returned mask internally consistent. Please let me know if you thinkWRITE_ONCE()is enough for this case.
Quoted 5 lines
What will be the issue of "do not make two separate reads in one
vsock_poll()invocation observe the samepeer_shutdownvalue." ?In any case, I'm not against it; I just want to understand the issue better.
The issue I was trying to avoid is a transiently inconsistent poll mask from one vsock_poll() pass.
For example, the early peer_shutdown read can see 0, so the shutdown-derived bits are not set, especially EPOLLRDHUP, and EPOLLHUP in the cases where the existing logic would set it. Then, before lock_sock() succeeds, the peer shutdown can be processed. The later read after lock_sock() can see SEND_SHUTDOWN and set EPOLLIN|EPOLLRDNORM for EOF readability.
So the returned mask can say that EOF is readable, but miss the shutdown-derived indication from the same peer_shutdown state. If userspace is waiting specifically for EPOLLRDHUP, that notification can be missed or delayed for that poll pass.
I agree this is small and transient. A following read() or another poll pass will observe the shutdown state. My intention with the single snapshot is only to avoid mixing old and new peer_shutdown values in one returned mask.
I will send a v2 as a new thread with WRITE_ONCE() on the writers, the DGRAM comment, and your ordering suggestion.
Thanks,
Ziyu
Quoted 4 lines
Thanks,
Stefano