ovpn: only signal sk readable if user_queue has packets
Subsystems (2)
PATCH 1/1 ovpn: only signal sk readable if user_queue has packets
datagram_poll marks a socket as readable whenever there are packets in the standard sk_receive_queue. However, with TCP encapsulation, another queue is used to hold packets destined for userspace.
To ensure correct EPOLLIN signaling, reset EPOLLIN and EPOLLRDNORM after calling datagram_poll, and set them only if user_queue is not empty.
--- drivers/net/ovpn/tcp.c | 5 +++++ 1 file changed, 5 insertions(+)
drivers/net/ovpn/tcp.c +5 -0
@@ -563,6 +563,11 @@ static __poll_t ovpn_tcp_poll(struct file *file, struct socket *sock,__poll_t mask =datagram_poll (file, sock, wait);struct ovpn_socket *ovpn_sock; +/* Mark socket as readable for userspace only if there are packets in +* the user_queue, not in sk_receive_queue. +*/ + mask &= ~(EPOLLIN |EPOLLRDNORM ); +rcu_read_lock (); ovpn_sock =rcu_dereference_sk_user_data (sock->sk); if (ovpn_sock && ovpn_sock->peer &&
Signature · 1 line
2.51.0
Quoted 17 lines · Ralf Lici
datagram_pollmarks a socket as readable whenever there are packets in the standardsk_receive_queue. However, with TCP encapsulation, another queue is used to hold packets destined for userspace.To ensure correct EPOLLIN signaling, reset EPOLLIN and EPOLLRDNORM after calling
datagram_poll, and set them only ifuser_queueis not empty.Signed-off-by Ralf Lici <[email protected]...>--- drivers/net/ovpn/tcp.c | 5 +++++ 1 file changed, 5 insertions(+)drivers/net/ovpn/tcp.c +0
-0
drivers/net/ovpn/tcp.c static __poll_t ovpn_tcp_poll(struct file *file, struct socket *sock, · line 569
Quoted 7 lines
drivers/net/ovpn/tcp.c +4
-0@@ -563,6 +563,11 @@ static __poll_t ovpn_tcp_poll(struct file *file, struct socket *sock,__poll_t mask =datagram_poll (file, sock, wait);struct ovpn_socket *ovpn_sock; +/* Mark socket as readable for userspace only if there are packets in +* the user_queue, not in sk_receive_queue. +*/ + mask &= ~(EPOLLIN |EPOLLRDNORM );
This would clear EPOLLIN and EPOLLRDNORM if they were set (alongside EPOLLRDHUP) because the socket was shutdown():
// https://git.kernel.org/pub/scm/linux/kernel/git/netdev/net.git/tree/net/core/datagram.c?h=v6.17#n952
shutdown = READ_ONCE(sk->sk_shutdown); if (shutdown & RCV_SHUTDOWN) mask |= EPOLLRDHUP | EPOLLIN | EPOLLRDNORM;
which may not be desireable?
Maybe another way to deal with it would be to add a variant of datagram_poll that takes the "real" RX queue as extra argument, and make datagram_poll a wrapper (using sk_receive_queue) around it. Then ovpn could use this new __datagram_poll helper with tcp.user_queue (but I'm not sure we can call datagram_poll under rcu_read_lock).
Quoted 6 lines
drivers/net/ovpn/tcp.c +0
-0rcu_read_lock (); ovpn_sock =rcu_dereference_sk_user_data (sock->sk); if (ovpn_sock && ovpn_sock->peer &&Signature · 1 line
2.51.0
Signature · 1 line
Sabrina
Quoted 9 lines · Sabrina Dubroca
This would clear EPOLLIN and EPOLLRDNORM if they were set (alongside EPOLLRDHUP) because the socket was
shutdown()://
https://git.kernel.org/pub/scm/linux/kernel/git/netdev/net.git/tree/net/core/datagram.c?h=v6.17#n952shutdown = READ_ONCE(sk->sk_shutdown); if (shutdown & RCV_SHUTDOWN) mask |= EPOLLRDHUP | EPOLLIN | EPOLLRDNORM;
Quoted 7 lines
which may not be desireable?
Maybe another way to deal with it would be to add a variant of
datagram_pollthat takes the "real" RX queue as extra argument, and makedatagram_polla wrapper (usingsk_receive_queue) around it. Then ovpn could use this new__datagram_pollhelper with tcp.user_queue
I totally agree.
Quoted 1 line
(but I'm not sure we can call
datagram_pollunderrcu_read_lock).
I see the problem may arise from the call to sock_poll_wait, which isn't safe inside an rcu_read_lock() section. Perhaps the __datagram_poll helper could delegate the responsibility of calling sock_poll_wait to the caller, allowing it to perform the rest of the polling logic safely within the RCU read-side critical section.
Quoted 1 line
Signature · 2 lines
Ralf Lici Mandelbit Srl
Quoted 11 lines · On Tue, 2025-10-14 at 18:29 +0200, [email protected]... wrote:
Quoted 1 line · Sabrina Dubroca
(but I'm not sure we can call
datagram_pollunderrcu_read_lock).I see the problem may arise from the call to
sock_poll_wait, which isn't
safe inside anrcu_read_lock()section. Perhaps the__datagram_pollhelper could delegate the responsibility of callingsock_poll_waitto the caller, allowing it to perform the rest of the polling logic safely
within the RCU read-side critical section.
After discussing this with Antonio, we realized that we can safely take a reference to the queue and increment the peer's refcounter, allowing us to call __datagram_poll outside the RCU section. This approach lets us avoid modifying the original kernel function.
I'll send a patch shortly.
Thanks for your help!
Signature · 2 lines
Ralf Lici Mandelbit Srl