tuntap: synchronize through tfiles array instead of tun->numqueues
Subsystems (3)
PATCH v2 1/1 tuntap: synchronize through tfiles array instead of tun->numqueues
When a queue(tfile) is detached through __tun_detach(), we move the last enabled tfile to the position where detached one sit but don't NULL out last position. We expect to synchronize the datapath through tun->numqueues. Unfortunately, this won't work since we're lacking sufficient mechanism to order or synchronize the access to tun->numqueues.
To fix this, NULL out the last position during detaching and check RCU protected tfile against NULL instead of checking tun->numqueues in datapath.
---
Changes from V1:
- keep the check in tun_xdp_xmit()
--- drivers/net/tun.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-)
drivers/net/tun.c +7 -1
@@ -700,6 +700,8 @@ static void __tun_detach(struct tun_file *tfile, bool clean) tun->tfiles[tun->numqueues - 1]); ntfile =rtnl_dereference (tun->tfiles[index]); ntfile->queue_index = index; +rcu_assign_pointer (tun->tfiles[tun->numqueues -1 ], +NULL ); --tun->numqueues;if (clean) {
@@ -1082,7 +1084,7 @@ static netdev_tx_t tun_net_xmit(struct sk_buff *skb, struct net_device *dev) tfile =rcu_dereference (tun->tfiles[txq]);/* Drop packet if interface is not attached */ -if (txq >= tun->numqueues) +if (!tfile) goto drop;if (!rcu_dereference (tun->steering_prog))
@@ -1313,6 +1315,10 @@ static int tun_xdp_xmit(struct net_device *dev, int n, tfile =rcu_dereference (tun->tfiles[smp_processor_id () % numqueues]); +if (!tfile) { +rcu_read_unlock (); +return -ENXIO ;/* Caller will free/return all frames */ + }spin_lock (&tfile->tx_ring.producer_lock);for (i =0 ; i < n; i++) {
Signature · 1 line
1.8.3.1
drivers/net/tun.c static void __tun_detach(struct tun_file *tfile, bool clean) · line 705
Quoted 11 lines · Jason Wang
drivers/net/tun.c +2
-0@@ -700,6 +700,8 @@ static void __tun_detach(struct tun_file *tfile, bool clean) tun->tfiles[tun->numqueues - 1]); ntfile =rtnl_dereference (tun->tfiles[index]); ntfile->queue_index = index; +rcu_assign_pointer (tun->tfiles[tun->numqueues -1 ], +NULL );
How does this work? Existing readers could still read this tun->tfiles[tun->numqueues - 1] before you NULL it. And, _if_ the following sock_put() is the one frees it, you still miss a RCU grace period.
if (clean) {
RCU_INIT_POINTER(tfile->tun, NULL);
sock_put(&tfile->sk);Thanks.
Quoted 17 lines · Cong Wang
drivers/net/tun.c static void __tun_detach(struct tun_file *tfile, bool clean) · line 705
Quoted 11 lines · Jason Wang
drivers/net/tun.c +2
-0@@ -700,6 +700,8 @@ static void __tun_detach(struct tun_file *tfile, bool clean) tun->tfiles[tun->numqueues - 1]); ntfile =rtnl_dereference (tun->tfiles[index]); ntfile->queue_index = index; +rcu_assign_pointer (tun->tfiles[tun->numqueues -1 ], +NULL );How does this work? Existing readers could still read this tun->tfiles[tun->numqueues - 1] before you NULL it. And, _if_ the following
sock_put()is the one frees it, you still miss a RCU grace period.
Quoted 6 lines
if (clean) { RCU_INIT_POINTER(tfile->tun, NULL); sock_put(&tfile->sk);Thanks.
My understanding is the socket will never be freed for this sock_put(). We just drop an extra reference count we held when the socket was attached to the netdevice (there's a sock_hold() in tun_attach()). The real free should happen at another sock_put() in the end of this function.
Thanks
Quoted 25 lines · Jason Wang
Quoted 22 lines · Cong Wang
drivers/net/tun.c static void __tun_detach(struct tun_file *tfile, bool clean) · line 705
Quoted 11 lines · Jason Wang
drivers/net/tun.c +2
-0@@ -700,6 +700,8 @@ static void __tun_detach(struct tun_file *tfile, bool clean) tun->tfiles[tun->numqueues - 1]); ntfile =rtnl_dereference (tun->tfiles[index]); ntfile->queue_index = index; +rcu_assign_pointer (tun->tfiles[tun->numqueues -1 ], +NULL );How does this work? Existing readers could still read this tun->tfiles[tun->numqueues - 1] before you NULL it. And, _if_ the following
sock_put()is the one frees it, you still miss a RCU grace period.if (clean) { RCU_INIT_POINTER(tfile->tun, NULL); sock_put(&tfile->sk);
Quoted 7 lines
Quoted 1 line
Thanks.
My understanding is the socket will never be freed for this
sock_put(). We just drop an extra reference count we held when the socket was attached to the netdevice (there's asock_hold()intun_attach()). The real free should happen at anothersock_put()in the end of this function.
So you are saying readers will never read this sock after free, then what are you fixing with this patch? Nothing, right?
As I said, reading a stale tun->numqueues is fine, you just keep believing it is a problem.
Quoted 25 lines · Cong Wang
Quoted 24 lines · Jason Wang
Quoted 22 lines · Cong Wang
drivers/net/tun.c static void __tun_detach(struct tun_file *tfile, bool clean) · line 705
Quoted 11 lines · Jason Wang
drivers/net/tun.c +2
-0@@ -700,6 +700,8 @@ static void __tun_detach(struct tun_file *tfile, bool clean) tun->tfiles[tun->numqueues - 1]); ntfile =rtnl_dereference (tun->tfiles[index]); ntfile->queue_index = index; +rcu_assign_pointer (tun->tfiles[tun->numqueues -1 ], +NULL );How does this work? Existing readers could still read this tun->tfiles[tun->numqueues - 1] before you NULL it. And, _if_ the following
sock_put()is the one frees it, you still miss a RCU grace period.if (clean) { RCU_INIT_POINTER(tfile->tun, NULL); sock_put(&tfile->sk);
Quoted 8 lines
Quoted 6 lines
Quoted 1 line
Thanks.
My understanding is the socket will never be freed for this
sock_put(). We just drop an extra reference count we held when the socket was attached to the netdevice (there's asock_hold()intun_attach()). The real free should happen at anothersock_put()in the end of this function.So you are saying readers will never read this sock after free, then what are you fixing with this patch? Nothing, right?
It's the issue of the second sock_put() in tun_detach() not the first one. Without proper synchronization for tun->numqueues, tun_net_xmit() can actually dereference a tfile which has been freed by second sock_put(). The synchornize_net() doesn't help since we're trying to synchronize through tun->numqueues.
Quoted 3 lines
As I said, reading a stale
tun->numqueuesis fine, you just keep believing it is a problem.
This is only true if you can make sure tfile[tun->numqueues] is not freed. Either my patch or SOCK_RCU_FREE can solve this, but for SOCK_RCU_FREE we need do extra careful audit to make sure it doesn't break someting. So synchronize through pointers in tfiles[] which is already protected by RCU is much more easier. It can make sure no dereference from xmit path after synchornize_net(). And this matches the assumptions of the codes after synchronize_net().
Thanks
Quoted 8 lines · Jason Wang
This is only true if you can make sure tfile[tun->numqueues] is not freed. Either my patch or
SOCK_RCU_FREEcan solve this, but forSOCK_RCU_FREEwe need do extra careful audit to make sure it doesn't break someting. So synchronize through pointers in tfiles[] which is already protected by RCU is much more easier. It can make sure no dereference from xmit path aftersynchornize_net(). And this matches the assumptions of the codes aftersynchronize_net().
It is hard to tell which sock_put() matches with this synchronize_net() given the call path is complicated.
With SOCK_RCU_FREE, no such a problem, all sock_put() will be safe. So to me SOCK_RCU_FREE is much easier to understand and audit.
Thanks.
Quoted 9 lines · Cong Wang
Quoted 8 lines · Jason Wang
This is only true if you can make sure tfile[tun->numqueues] is not freed. Either my patch or
SOCK_RCU_FREEcan solve this, but forSOCK_RCU_FREEwe need do extra careful audit to make sure it doesn't break someting. So synchronize through pointers in tfiles[] which is already protected by RCU is much more easier. It can make sure no dereference from xmit path aftersynchornize_net(). And this matches the assumptions of the codes aftersynchronize_net().
Quoted 5 lines
It is hard to tell which
sock_put()matches with thissynchronize_net()given the call path is complicated.With
SOCK_RCU_FREE, no such a problem, allsock_put()will be safe. So to meSOCK_RCU_FREEis much easier to understand and audit.
The problem is not tfile itself but the data structure associated. As I mentioned earlier, the xdp_rxq_info_unreg() looks racy if tun_net_xmit() can read stale value of numqueues. It's just one example, we may meet similar issues in the future when adding more features.
Thanks
Quoted 2 lines
Thanks.
Quoted 38 lines · Jason Wang
When a queue(tfile) is detached through
__tun_detach(), we move the last enabled tfile to the position where detached one sit but don't NULL out last position. We expect to synchronize the datapath throughtun->numqueues. Unfortunately, this won't work since we're lacking sufficient mechanism to order or synchronize the access totun->numqueues.To fix this, NULL out the last position during detaching and check RCU protected tfile against NULL instead of checking
tun->numqueuesin datapath.Cc YueHaibing <[email protected]>Cc Cong Wang <[email protected]>Cc weiyongjun (A) <[email protected]>Cc Eric Dumazet <[email protected]>Fixes c8d68e6be1c3b ("tuntap: multiqueue support")Signed-off-by Jason Wang <[email protected]>---
Changes from V1:
- keep the check intun_xdp_xmit()--- drivers/net/tun.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-)drivers/net/tun.c +2
-0@@ -700,6 +700,8 @@ static void __tun_detach(struct tun_file *tfile, bool clean) tun->tfiles[tun->numqueues - 1]); ntfile =rtnl_dereference (tun->tfiles[index]); ntfile->queue_index = index; +rcu_assign_pointer (tun->tfiles[tun->numqueues -1 ], +NULL ); --tun->numqueues;if (clean) {
drivers/net/tun.c static netdev_tx_t tun_net_xmit(struct sk_buff *skb, struct net_device *dev) · line 1090
Quoted 8 lines
drivers/net/tun.c +1
-1@@ -1082,7 +1084,7 @@ static netdev_tx_t tun_net_xmit(struct sk_buff *skb, struct net_device *dev) tfile =rcu_dereference (tun->tfiles[txq]);/* Drop packet if interface is not attached */ -if (txq >= tun->numqueues) +if (!tfile) goto drop;if (!rcu_dereference (tun->steering_prog))
Hmm don't we need to range check txq?
Quoted 13 lines
drivers/net/tun.c +4
-0@@ -1313,6 +1315,10 @@ static int tun_xdp_xmit(struct net_device *dev, int n, tfile =rcu_dereference (tun->tfiles[smp_processor_id () % numqueues]); +if (!tfile) { +rcu_read_unlock (); +return -ENXIO ;/* Caller will free/return all frames */ + }spin_lock (&tfile->tx_ring.producer_lock);for (i =0 ; i < n; i++) {Signature · 1 line
1.8.3.1
Quoted 41 lines · Michael S. Tsirkin
Quoted 40 lines · Jason Wang
When a queue(tfile) is detached through
__tun_detach(), we move the last enabled tfile to the position where detached one sit but don't NULL out last position. We expect to synchronize the datapath throughtun->numqueues. Unfortunately, this won't work since we're lacking sufficient mechanism to order or synchronize the access totun->numqueues.To fix this, NULL out the last position during detaching and check RCU protected tfile against NULL instead of checking
tun->numqueuesin datapath.Cc YueHaibing <[email protected]>Cc Cong Wang <[email protected]>Cc weiyongjun (A) <[email protected]>Cc Eric Dumazet <[email protected]>Fixes c8d68e6be1c3b ("tuntap: multiqueue support")Signed-off-by Jason Wang <[email protected]>---
Changes from V1:
- keep the check intun_xdp_xmit()--- drivers/net/tun.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-)drivers/net/tun.c +2
-0@@ -700,6 +700,8 @@ static void __tun_detach(struct tun_file *tfile, bool clean) tun->tfiles[tun->numqueues - 1]); ntfile =rtnl_dereference (tun->tfiles[index]); ntfile->queue_index = index; +rcu_assign_pointer (tun->tfiles[tun->numqueues -1 ], +NULL ); --tun->numqueues;if (clean) {@@ -1082,7 +1084,7 @@ static netdev_tx_t tun_net_xmit(struct sk_buff *skb, struct net_device *dev) tfile =rcu_dereference (tun->tfiles[txq]);
Quoted 7 lines
drivers/net/tun.c static netdev_tx_t tun_net_xmit(struct sk_buff *skb, struct net_device *dev) · line 1090
Quoted 6 lines
drivers/net/tun.c +1
-1/* Drop packet if interface is not attached */ -if (txq >= tun->numqueues) +if (!tfile) goto drop;if (!rcu_dereference (tun->steering_prog))Hmm don't we need to range check txq?
Looks not since tun_select_queue will always return a value which is less than MAX_TAP_QUEUES. And we NULL out the last enabled queue in tun_detach().
Thanks
Quoted 15 lines
Quoted 15 lines
drivers/net/tun.c +4
-0@@ -1313,6 +1315,10 @@ static int tun_xdp_xmit(struct net_device *dev, int n, tfile =rcu_dereference (tun->tfiles[smp_processor_id () % numqueues]); +if (!tfile) { +rcu_read_unlock (); +return -ENXIO ;/* Caller will free/return all frames */ + }spin_lock (&tfile->tx_ring.producer_lock);for (i =0 ; i < n; i++) {Signature · 1 line
1.8.3.1
David Miller
maintainerFrom: Jason Wang <[email protected]>
Date: Tue, 7 May 2019 00:03:36 -0400
drivers/net/tun.c static int tun_xdp_xmit(struct net_device *dev, int n, · line 1324
Quoted 11 lines
drivers/net/tun.c +4
-0@@ -1313,6 +1315,10 @@ static int tun_xdp_xmit(struct net_device *dev, int n, tfile =rcu_dereference (tun->tfiles[smp_processor_id () % numqueues]); +if (!tfile) { +rcu_read_unlock (); +return -ENXIO ;/* Caller will free/return all frames */ + }spin_lock (&tfile->tx_ring.producer_lock);for (i =0 ; i < n; i++) {
The only way we can see a NULL here is if a detach happened in parallel, and if that happens we should retry the tfile[] indexing after resampling numqueues rather than dropping the packet.
Quoted 12 lines · David Miller
From: Jason Wang <[email protected]>
Date: Tue, 7 May 2019 00:03:36 -0400Quoted 9 lines
drivers/net/tun.c +4
-0@@ -1313,6 +1315,10 @@ static int tun_xdp_xmit(struct net_device *dev, int n, tfile =rcu_dereference (tun->tfiles[smp_processor_id () % numqueues]); +if (!tfile) { +rcu_read_unlock (); +return -ENXIO ;/* Caller will free/return all frames */ + }
Quoted 5 lines
drivers/net/tun.c static int tun_xdp_xmit(struct net_device *dev, int n, · line 1324
Quoted 2 lines
drivers/net/tun.c +0
-0spin_lock (&tfile->tx_ring.producer_lock);for (i =0 ; i < n; i++) {The only way we can see a NULL here is if a detach happened in parallel, and if that happens we should retry the tfile[] indexing after resampling numqueues rather than dropping the packet.
Ok, will fix this in V3.
Thanks