wireguard: use DEV_STATS_INC()
Subsystems (3)
PATCH v2 1/1 wireguard: use DEV_STATS_INC()
wg_xmit() can be called concurrently, KCSAN reported [1] some device stats updates can be lost.
Use DEV_STATS_INC() for this unlikely case.
[1]
BUG: KCSAN: data-race in wg_xmit / wg_xmit
read-write to 0xffff888104239160 of 8 bytes by task 1375 on cpu 0:
wg_xmit+0x60f/0x680 drivers/net/wireguard/device.c:231 __netdev_start_xmit include/linux/netdevice.h:4918 [inline] netdev_start_xmit include/linux/netdevice.h:4932 [inline] xmit_one net/core/dev.c:3543 [inline] dev_hard_start_xmit+0x11b/0x3f0 net/core/dev.c:3559
...
read-write to 0xffff888104239160 of 8 bytes by task 1378 on cpu 1:
wg_xmit+0x60f/0x680 drivers/net/wireguard/device.c:231 __netdev_start_xmit include/linux/netdevice.h:4918 [inline] netdev_start_xmit include/linux/netdevice.h:4932 [inline] xmit_one net/core/dev.c:3543 [inline] dev_hard_start_xmit+0x11b/0x3f0 net/core/dev.c:3559
...
v2: also change wg_packet_consume_data_done() (Hangbin Liu)
and wg_packet_purge_staged_packets()
--- drivers/net/wireguard/device.c | 4 ++-- drivers/net/wireguard/receive.c | 12 ++++++------ drivers/net/wireguard/send.c | 3 ++- 3 files changed, 10 insertions(+), 9 deletions(-)
drivers/net/wireguard/device.c +2 -2
@@ -210,7 +210,7 @@ static netdev_tx_t wg_xmit(struct sk_buff *skb, struct net_device *dev)*/ while (skb_queue_len (&peer->staged_packet_queue) >MAX_STAGED_PACKETS ) {dev_kfree_skb (__skb_dequeue (&peer->staged_packet_queue)); - ++dev->stats.tx_dropped; +DEV_STATS_INC (dev, tx_dropped); }skb_queue_splice_tail (&packets, &peer->staged_packet_queue);spin_unlock_bh (&peer->staged_packet_queue.lock);
@@ -228,7 +228,7 @@ static netdev_tx_t wg_xmit(struct sk_buff *skb, struct net_device *dev)else if (skb->protocol ==htons (ETH_P_IPV6 ))icmpv6_ndo_send (skb,ICMPV6_DEST_UNREACH ,ICMPV6_ADDR_UNREACH ,0 ); err: - ++dev->stats.tx_errors; +DEV_STATS_INC (dev, tx_errors);kfree_skb (skb);return ret; }
drivers/net/wireguard/receive.c +6 -6
@@ -416,20 +416,20 @@ static void wg_packet_consume_data_done(struct wg_peer *peer,net_dbg_skb_ratelimited ("%s: Packet has unallowed src IP (%pISc) from peer %llu (%pISpfsc)\n" , dev->name, skb, peer->internal_id, &peer->endpoint.addr); - ++dev->stats.rx_errors; - ++dev->stats.rx_frame_errors; +DEV_STATS_INC (dev, rx_errors); +DEV_STATS_INC (dev, rx_frame_errors); goto packet_processed; dishonest_packet_type:net_dbg_ratelimited ("%s: Packet is neither ipv4 nor ipv6 from peer %llu (%pISpfsc)\n" , dev->name, peer->internal_id, &peer->endpoint.addr); - ++dev->stats.rx_errors; - ++dev->stats.rx_frame_errors; +DEV_STATS_INC (dev, rx_errors); +DEV_STATS_INC (dev, rx_frame_errors); goto packet_processed; dishonest_packet_size:net_dbg_ratelimited ("%s: Packet has incorrect size from peer %llu (%pISpfsc)\n" , dev->name, peer->internal_id, &peer->endpoint.addr); - ++dev->stats.rx_errors; - ++dev->stats.rx_length_errors; +DEV_STATS_INC (dev, rx_errors); +DEV_STATS_INC (dev, rx_length_errors); goto packet_processed; packet_processed:dev_kfree_skb (skb);
drivers/net/wireguard/send.c +2 -1
@@ -333,7 +333,8 @@ static void wg_packet_create_data(struct wg_peer *peer, struct sk_buff *first)void wg_packet_purge_staged_packets (struct wg_peer *peer) {spin_lock_bh (&peer->staged_packet_queue.lock); - peer->device->dev->stats.tx_dropped += peer->staged_packet_queue.qlen; +DEV_STATS_ADD (peer->device->dev, tx_dropped, + peer->staged_packet_queue.qlen);__skb_queue_purge (&peer->staged_packet_queue);spin_unlock_bh (&peer->staged_packet_queue.lock); }
Signature · 1 line
2.43.0.rc0.421.g78406f8d94-goog
Jason A. Donenfeld
maintainerHi Eric,
Quoted 97 lines · Eric Dumazet
wg_xmit()can be called concurrently, KCSAN reported [1] some device stats updates can be lost.Use
DEV_STATS_INC()for this unlikely case.[1]
BUG: KCSAN: data-race in wg_xmit / wg_xmitread-write to
0xffff888104239160of 8 bytes by task 1375 on cpu 0:wg_xmit+0x60f/0x680 drivers/net/wireguard/device.c:231 __netdev_start_xmit include/linux/netdevice.h:4918 [inline] netdev_start_xmit include/linux/netdevice.h:4932 [inline] xmit_one net/core/dev.c:3543 [inline] dev_hard_start_xmit+0x11b/0x3f0 net/core/dev.c:3559...
read-write to
0xffff888104239160of 8 bytes by task 1378 on cpu 1:wg_xmit+0x60f/0x680 drivers/net/wireguard/device.c:231 __netdev_start_xmit include/linux/netdevice.h:4918 [inline] netdev_start_xmit include/linux/netdevice.h:4932 [inline] xmit_one net/core/dev.c:3543 [inline] dev_hard_start_xmit+0x11b/0x3f0 net/core/dev.c:3559...
v2: also change
wg_packet_consume_data_done()(Hangbin Liu)
andwg_packet_purge_staged_packets()Fixes e7096c131e51 ("net: WireGuard secure network tunnel")Reported-by syzbot <[email protected]>Signed-off-by Eric Dumazet <[email protected]>Cc Jason A. Donenfeld <[email protected]>Cc Hangbin Liu <[email protected]>--- drivers/net/wireguard/device.c | 4 ++-- drivers/net/wireguard/receive.c | 12 ++++++------ drivers/net/wireguard/send.c | 3 ++- 3 files changed, 10 insertions(+), 9 deletions(-)drivers/net/wireguard/device.c +2
-2@@ -210,7 +210,7 @@ static netdev_tx_t wg_xmit(struct sk_buff *skb, struct net_device *dev)*/ while (skb_queue_len (&peer->staged_packet_queue) >MAX_STAGED_PACKETS ) {dev_kfree_skb (__skb_dequeue (&peer->staged_packet_queue)); - ++dev->stats.tx_dropped; +DEV_STATS_INC (dev, tx_dropped); }skb_queue_splice_tail (&packets, &peer->staged_packet_queue);spin_unlock_bh (&peer->staged_packet_queue.lock);@@ -228,7 +228,7 @@ static netdev_tx_t wg_xmit(struct sk_buff *skb, struct net_device *dev)else if (skb->protocol ==htons (ETH_P_IPV6 ))icmpv6_ndo_send (skb,ICMPV6_DEST_UNREACH ,ICMPV6_ADDR_UNREACH ,0 ); err: - ++dev->stats.tx_errors; +DEV_STATS_INC (dev, tx_errors);kfree_skb (skb);return ret; }drivers/net/wireguard/receive.c +6
-6@@ -416,20 +416,20 @@ static void wg_packet_consume_data_done(struct wg_peer *peer,net_dbg_skb_ratelimited ("%s: Packet has unallowed src IP (%pISc) from peer %llu (%pISpfsc)\n" , dev->name, skb, peer->internal_id, &peer->endpoint.addr); - ++dev->stats.rx_errors; - ++dev->stats.rx_frame_errors; +DEV_STATS_INC (dev, rx_errors); +DEV_STATS_INC (dev, rx_frame_errors); goto packet_processed; dishonest_packet_type:net_dbg_ratelimited ("%s: Packet is neither ipv4 nor ipv6 from peer %llu (%pISpfsc)\n" , dev->name, peer->internal_id, &peer->endpoint.addr); - ++dev->stats.rx_errors; - ++dev->stats.rx_frame_errors; +DEV_STATS_INC (dev, rx_errors); +DEV_STATS_INC (dev, rx_frame_errors); goto packet_processed; dishonest_packet_size:net_dbg_ratelimited ("%s: Packet has incorrect size from peer %llu (%pISpfsc)\n" , dev->name, peer->internal_id, &peer->endpoint.addr); - ++dev->stats.rx_errors; - ++dev->stats.rx_length_errors; +DEV_STATS_INC (dev, rx_errors); +DEV_STATS_INC (dev, rx_length_errors); goto packet_processed; packet_processed:dev_kfree_skb (skb);drivers/net/wireguard/send.c +0
-0
drivers/net/wireguard/send.c static void wg_packet_create_data(struct wg_peer *peer, struct sk_buff *first) · line 340
Quoted 9 lines
drivers/net/wireguard/send.c +2
-1@@ -333,7 +333,8 @@ static void wg_packet_create_data(struct wg_peer *peer, struct sk_buff *first)void wg_packet_purge_staged_packets (struct wg_peer *peer) {spin_lock_bh (&peer->staged_packet_queue.lock); - peer->device->dev->stats.tx_dropped += peer->staged_packet_queue.qlen; +DEV_STATS_ADD (peer->device->dev, tx_dropped, + peer->staged_packet_queue.qlen);__skb_queue_purge (&peer->staged_packet_queue);spin_unlock_bh (&peer->staged_packet_queue.lock); }
This is probably fine if you want to do it and feel strongly about it, and you can take this directly into net/net-next with my:
However, I recall evaluating the races here long ago and deliberately deciding not to do anything about it. Sure KCSAN will complain, but these stats being pixel perfect isn't really _that_ important and it really doesn't seem worth it to have the performance hit of several atomics on every packet. There's also peer->{r,t}x_bytes that should probably be adjusted if you're going to change these. But again - is it really worth it to do that? It just seems like such an unnecessary performance hit.
So I think I'd prefer to _not_ fix this. But if you feel really strongly about it, I'll be okay deferring to your judgement.
Jason
Quoted 27 lines · Eric Dumazet
wg_xmit()can be called concurrently, KCSAN reported [1] some device stats updates can be lost.Use
DEV_STATS_INC()for this unlikely case.[1]
BUG: KCSAN: data-race in wg_xmit / wg_xmitread-write to
0xffff888104239160of 8 bytes by task 1375 on cpu 0:wg_xmit+0x60f/0x680 drivers/net/wireguard/device.c:231 __netdev_start_xmit include/linux/netdevice.h:4918 [inline] netdev_start_xmit include/linux/netdevice.h:4932 [inline] xmit_one net/core/dev.c:3543 [inline] dev_hard_start_xmit+0x11b/0x3f0 net/core/dev.c:3559...
read-write to
0xffff888104239160of 8 bytes by task 1378 on cpu 1:wg_xmit+0x60f/0x680 drivers/net/wireguard/device.c:231 __netdev_start_xmit include/linux/netdevice.h:4918 [inline] netdev_start_xmit include/linux/netdevice.h:4932 [inline] xmit_one net/core/dev.c:3543 [inline] dev_hard_start_xmit+0x11b/0x3f0 net/core/dev.c:3559...
v2: also change
wg_packet_consume_data_done()(Hangbin Liu)
andwg_packet_purge_staged_packets()
Quoted 6 lines
Fixes e7096c131e51 ("net: WireGuard secure network tunnel")Reported-by syzbot <[email protected]>Signed-off-by Eric Dumazet <[email protected]>Cc Jason A. Donenfeld <[email protected]>Cc Hangbin Liu <[email protected]>---
I respect Jason's comments.
This patch was applied to netdev/net.git (main) by David S. Miller <[email protected]>:
Hello:
This patch was applied to netdev/net.git (main)
by David S. Miller <[email protected]>:
Quoted 9 lines · On Fri, 17 Nov 2023 14:17:33 +0000 you wrote:
wg_xmit()can be called concurrently, KCSAN reported [1] some device stats updates can be lost.Use
DEV_STATS_INC()for this unlikely case.[1]
BUG: KCSAN: data-race in wg_xmit / wg_xmit[...]
Here is the summary with links:
- [v2,net] wireguard: use DEV_STATS_INC()
https://git.kernel.org/netdev/net/c/93da8d75a665
You are awesome, thank you!
Signature · 2 lines
Deet-doot-dot, I am a bot. https://korg.docs.kernel.org/patchwork/pwbot.html
Jason A. Donenfeld
maintainerHi,
Quoted 15 lines · On Sun, Nov 19, 2023 at 8:50 PM <[email protected]> wrote:
Hello:
This patch was applied to netdev/net.git (main)
by David S. Miller <[email protected]>:Quoted 8 lines · On Fri, 17 Nov 2023 14:17:33 +0000 you wrote:
wg_xmit()can be called concurrently, KCSAN reported [1] some device stats updates can be lost.Use
DEV_STATS_INC()for this unlikely case.[1]
BUG: KCSAN: data-race in wg_xmit / wg_xmit
Quoted 7 lines
Quoted 1 line
[...]
Here is the summary with links:
- [v2,net] wireguard: useDEV_STATS_INC()https://git.kernel.org/netdev/net/c/93da8d75a665You are awesome, thank you!
I thought that, given my concerns, if this was to be committed, at least Eric (or you?) would expand on the rationale in the context of my concerns while (or before) doing so, rather than just applying this without further discussion. As I mentioned, this is fine with me if you feel strongly about it, but I would appreciate some expanded explanation, just for my own understanding of the matter.
Jason
Quoted 27 lines · Jason A. Donenfeld
Hi,
Quoted 15 lines · On Sun, Nov 19, 2023 at 8:50 PM <[email protected]> wrote:
Hello:
This patch was applied to netdev/net.git (main)
by David S. Miller <[email protected]>:Quoted 8 lines · On Fri, 17 Nov 2023 14:17:33 +0000 you wrote:
wg_xmit()can be called concurrently, KCSAN reported [1] some device stats updates can be lost.Use
DEV_STATS_INC()for this unlikely case.[1]
BUG: KCSAN: data-race in wg_xmit / wg_xmitQuoted 7 lines
Quoted 1 line
[...]
Here is the summary with links:
- [v2,net] wireguard: useDEV_STATS_INC()https://git.kernel.org/netdev/net/c/93da8d75a665You are awesome, thank you!
Quoted 8 lines
I thought that, given my concerns, if this was to be committed, at least Eric (or you?) would expand on the rationale in the context of my concerns while (or before) doing so, rather than just applying this without further discussion. As I mentioned, this is fine with me if you feel strongly about it, but I would appreciate some expanded explanation, just for my own understanding of the matter.
Jason
Jason, I was in week end mode, so could not reply to your message.
This fix is rather obvious to me. I do not want to spend too much time on it, and you gave an ACK if I am not mistaken.
If you prefer not letting syzbot find other bugs in wireguard (because hitting this issue first),
this is fine by me. We can ask syzbot team to not include wireguard in their kernels.
Quoted 11 lines · Eric Dumazet
Jason, I was in week end mode, so could not reply to your message.
This fix is rather obvious to me. I do not want to spend too much time on it, and you gave an ACK if I am not mistaken.
If you prefer not letting syzbot find other bugs in wireguard (because hitting this issue first),
this is fine by me. We can ask syzbot team to not include wireguard in their kernels.
BTW, while cooking the patch I found that wireguard was incorrectly using dev_kfree_skb() instead of kfree_skb().
dev_kfree_skb() is really a consume_skb(), which gives different drop monitor signals
(perf record -a -e skb:kfree_skb) vs (perf record -a -e skb:consume_skb)
I would suggest you take a look.
Ideally, using kfree_skb_reason(skb, reason) (for net-next tree) would help future diagnostics.
For net tree I would suggest
drivers/net/wireguard/device.c +0 -0
100644
drivers/net/wireguard/device.c +0 -0
@@ -209,7 +209,7 @@ static netdev_tx_t wg_xmit(struct sk_buff *skb,
struct net_device *dev)
* we don't remove GSO segments that are in excess.
*/
while (skb_queue_len(&peer->staged_packet_queue) > MAX_STAGED_PACKETS) {
- dev_kfree_skb(__skb_dequeue(&peer->staged_packet_queue));
+ kfree_skb(__skb_dequeue(&peer->staged_packet_queue));
++dev->stats.tx_dropped;
}
skb_queue_splice_tail(&packets, &peer->staged_packet_queue);drivers/net/wireguard/receive.c +0 -0
100644
drivers/net/wireguard/receive.c +0 -0
@@ -432,7 +432,7 @@ static void wg_packet_consume_data_done(struct
wg_peer *peer,
++dev->stats.rx_length_errors;
goto packet_processed; packet_processed:
- dev_kfree_skb(skb);
+ kfree_skb(skb);
}
int wg_packet_rx_poll(struct napi_struct *napi, int budget)
@@ -478,7 +478,7 @@ int wg_packet_rx_poll(struct napi_struct *napi, int budget)
wg_noise_keypair_put(keypair, false);
wg_peer_put(peer);
if (unlikely(free))- dev_kfree_skb(skb);
+ kfree_skb(skb);
if (++work_done >= budget)
break;
@@ -536,7 +536,7 @@ static void wg_packet_consume_data(struct
wg_device *wg, struct sk_buff *skb)
err_keypair:
rcu_read_unlock_bh();
wg_peer_put(peer);- dev_kfree_skb(skb);
+ kfree_skb(skb);
}
void wg_packet_receive(struct wg_device *wg, struct sk_buff *skb)
@@ -582,5 +582,5 @@ void wg_packet_receive(struct wg_device *wg, struct sk_buff *skb)
return;
err:
- dev_kfree_skb(skb);
+ kfree_skb(skb);
}
drivers/net/wireguard/socket.c +0 -0
100644
drivers/net/wireguard/socket.c +0 -0
@@ -178,7 +178,7 @@ int wg_socket_send_skb_to_peer(struct wg_peer
*peer, struct sk_buff *skb, u8 ds)
ret = send6(peer->device, skb, &peer->endpoint, ds,
&peer->endpoint_cache);
else
- dev_kfree_skb(skb);
+ kfree_skb(skb);
if (likely(!ret))
peer->tx_bytes += skb_len;
read_unlock_bh(&peer->endpoint_lock);Quoted 11 lines · Eric Dumazet
Quoted 8 lines
I thought that, given my concerns, if this was to be committed, at least Eric (or you?) would expand on the rationale in the context of my concerns while (or before) doing so, rather than just applying this without further discussion. As I mentioned, this is fine with me if you feel strongly about it, but I would appreciate some expanded explanation, just for my own understanding of the matter.
Jason
Jason, I was in week end mode, so could not reply to your message.
Quoted 7 lines
This fix is rather obvious to me. I do not want to spend too much time on it, and you gave an ACK if I am not mistaken.
If you prefer not letting syzbot find other bugs in wireguard (because hitting this issue first),
this is fine by me. We can ask syzbot team to not include wireguard in their kernels.
Some performance test data may helps.
As I don't have good and same configure test machines. I just did a rough test.
Client: RHEL9.2, CPU Intel E5-2620, Memory 4096 MB, NIC I350
Server: 6.7.0-rc1, CPU Intel Xeon Silver 4216, 196608 MB, NIC I350
Before patch:
=== 4in4 TCP_STREAM: 901.05 Mbits/sec ===
=== 4in4 MPTCP_STREAM: 885.22 Mbits/sec ===
=== 4in4 UDP_STREAM: 919.91 Mbits/sec ===
=== 4in4 SCTP_STREAM: 903.12 Mbits/sec ===
After patch:
=== 4in4 TCP_STREAM: 901.07 Mbits/sec ===
=== 4in4 MPTCP_STREAM: 885.24 Mbits/sec ===
=== 4in4 UDP_STREAM: 919.91 Mbits/sec ===
=== 4in4 SCTP_STREAM: 903.14 Mbits/sec ===
Exchange the client/server role:
Before patch:
=== 4in4 TCP_STREAM: 901.08 Mbits/sec ===
=== 4in4 MPTCP_STREAM: 885.12 Mbits/sec ===
=== 4in4 UDP_STREAM: 919.94 Mbits/sec ===
=== 4in4 SCTP_STREAM: 903.09 Mbits/sec ===
After patch:
=== 4in4 TCP_STREAM: 901.04 Mbits/sec ===
=== 4in4 MPTCP_STREAM: 885.24 Mbits/sec ===
=== 4in4 UDP_STREAM: 919.91 Mbits/sec ===
=== 4in4 SCTP_STREAM: 903.11 Mbits/sec ===
The result looks good to me.
Thanks
Hangbin