Skip to contentSkip to conversation

ovpn: only signal sk readable if user_queue has packets

PATCH In review Ralf Lici 2 participants 4 messages Oct 14, 2025 – Oct 15, 2025

Subsystems (2)
lore
Share & tools
lore

Ralf Lici

author
#0 lore raw

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
index 289f62c5d2c7..4dbdd59a0b1c 100644
@@ -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
#1 lore raw
Quoted 17 lines · Ralf Lici

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 +0 -0
index 289f62c5d2c7..4dbdd59a0b1c 100644

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 -0
 	rcu_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
#2 lore raw
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#n952

	shutdown = 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_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

I totally agree.

Quoted 1 line

(but I'm not sure we can call datagram_poll under rcu_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
author
#3 lore raw
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_poll under rcu_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.

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