Skip to content

Commit 5260dd3

Browse files
edumazetdavem330
authored andcommitted
tun: switch to u64_stats_t
In order to fix this data-race found by KCSAN [1], switch to u64_stats_t helpers. They provide all the needed annotations, without adding extra cost. [1] BUG: KCSAN: data-race in tun_get_user / tun_net_get_stats64 read to 0xffffe8ffffd8aca8 of 8 bytes by task 4882 on cpu 0: tun_net_get_stats64+0x9b/0x230 drivers/net/tun.c:1171 dev_get_stats+0x89/0x1e0 net/core/dev.c:9103 rtnl_fill_stats+0x56/0x370 net/core/rtnetlink.c:1177 rtnl_fill_ifinfo+0xd3b/0x2100 net/core/rtnetlink.c:1667 rtmsg_ifinfo_build_skb+0xb0/0x150 net/core/rtnetlink.c:3472 rtmsg_ifinfo_event.part.0+0x4e/0xb0 net/core/rtnetlink.c:3504 rtmsg_ifinfo_event net/core/rtnetlink.c:3515 [inline] rtmsg_ifinfo+0x85/0x90 net/core/rtnetlink.c:3513 __dev_notify_flags+0x18b/0x200 net/core/dev.c:7649 dev_change_flags+0xb8/0xe0 net/core/dev.c:7691 dev_ifsioc+0x201/0x6a0 net/core/dev_ioctl.c:237 dev_ioctl+0x149/0x660 net/core/dev_ioctl.c:489 sock_do_ioctl+0xdb/0x230 net/socket.c:1061 sock_ioctl+0x3a3/0x5e0 net/socket.c:1189 vfs_ioctl fs/ioctl.c:46 [inline] file_ioctl fs/ioctl.c:509 [inline] do_vfs_ioctl+0x991/0xc60 fs/ioctl.c:696 write to 0xffffe8ffffd8aca8 of 8 bytes by task 4883 on cpu 1: tun_get_user+0x1d94/0x2ba0 drivers/net/tun.c:2002 tun_chr_write_iter+0x79/0xd0 drivers/net/tun.c:2022 call_write_iter include/linux/fs.h:1895 [inline] new_sync_write+0x388/0x4a0 fs/read_write.c:483 __vfs_write+0xb1/0xc0 fs/read_write.c:496 __kernel_write+0xb8/0x240 fs/read_write.c:515 write_pipe_buf+0xb6/0xf0 fs/splice.c:794 splice_from_pipe_feed fs/splice.c:500 [inline] __splice_from_pipe+0x248/0x480 fs/splice.c:624 splice_from_pipe+0xbb/0x100 fs/splice.c:659 default_file_splice_write+0x45/0x90 fs/splice.c:806 do_splice_from fs/splice.c:848 [inline] direct_splice_actor+0xa0/0xc0 fs/splice.c:1020 splice_direct_to_actor+0x215/0x510 fs/splice.c:975 do_splice_direct+0x161/0x1e0 fs/splice.c:1063 do_sendfile+0x384/0x7f0 fs/read_write.c:1464 Reported by Kernel Concurrency Sanitizer on: CPU: 1 PID: 4883 Comm: syz-executor.1 Not tainted 5.4.0-rc3+ #0 Hardware name: Google Google Compute Engine/Google Compute Engine, BIOS Google 01/01/2011 Signed-off-by: Eric Dumazet <edumazet@google.com> Signed-off-by: David S. Miller <davem@davemloft.net>
1 parent 316580b commit 5260dd3

1 file changed

Lines changed: 16 additions & 16 deletions

File tree

‎drivers/net/tun.c‎

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -136,10 +136,10 @@ struct tap_filter {
136136
#define TUN_FLOW_EXPIRE (3 * HZ)
137137

138138
struct tun_pcpu_stats {
139-
u64 rx_packets;
140-
u64 rx_bytes;
141-
u64 tx_packets;
142-
u64 tx_bytes;
139+
u64_stats_t rx_packets;
140+
u64_stats_t rx_bytes;
141+
u64_stats_t tx_packets;
142+
u64_stats_t tx_bytes;
143143
struct u64_stats_sync syncp;
144144
u32 rx_dropped;
145145
u32 tx_dropped;
@@ -1167,10 +1167,10 @@ tun_net_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats)
11671167
p = per_cpu_ptr(tun->pcpu_stats, i);
11681168
do {
11691169
start = u64_stats_fetch_begin(&p->syncp);
1170-
rxpackets = p->rx_packets;
1171-
rxbytes = p->rx_bytes;
1172-
txpackets = p->tx_packets;
1173-
txbytes = p->tx_bytes;
1170+
rxpackets = u64_stats_read(&p->rx_packets);
1171+
rxbytes = u64_stats_read(&p->rx_bytes);
1172+
txpackets = u64_stats_read(&p->tx_packets);
1173+
txbytes = u64_stats_read(&p->tx_bytes);
11741174
} while (u64_stats_fetch_retry(&p->syncp, start));
11751175

11761176
stats->rx_packets += rxpackets;
@@ -1998,8 +1998,8 @@ static ssize_t tun_get_user(struct tun_struct *tun, struct tun_file *tfile,
19981998

19991999
stats = get_cpu_ptr(tun->pcpu_stats);
20002000
u64_stats_update_begin(&stats->syncp);
2001-
stats->rx_packets++;
2002-
stats->rx_bytes += len;
2001+
u64_stats_inc(&stats->rx_packets);
2002+
u64_stats_add(&stats->rx_bytes, len);
20032003
u64_stats_update_end(&stats->syncp);
20042004
put_cpu_ptr(stats);
20052005

@@ -2052,8 +2052,8 @@ static ssize_t tun_put_user_xdp(struct tun_struct *tun,
20522052

20532053
stats = get_cpu_ptr(tun->pcpu_stats);
20542054
u64_stats_update_begin(&stats->syncp);
2055-
stats->tx_packets++;
2056-
stats->tx_bytes += ret;
2055+
u64_stats_inc(&stats->tx_packets);
2056+
u64_stats_add(&stats->tx_bytes, ret);
20572057
u64_stats_update_end(&stats->syncp);
20582058
put_cpu_ptr(tun->pcpu_stats);
20592059

@@ -2147,8 +2147,8 @@ static ssize_t tun_put_user(struct tun_struct *tun,
21472147
/* caller is in process context, */
21482148
stats = get_cpu_ptr(tun->pcpu_stats);
21492149
u64_stats_update_begin(&stats->syncp);
2150-
stats->tx_packets++;
2151-
stats->tx_bytes += skb->len + vlan_hlen;
2150+
u64_stats_inc(&stats->tx_packets);
2151+
u64_stats_add(&stats->tx_bytes, skb->len + vlan_hlen);
21522152
u64_stats_update_end(&stats->syncp);
21532153
put_cpu_ptr(tun->pcpu_stats);
21542154

@@ -2511,8 +2511,8 @@ static int tun_xdp_one(struct tun_struct *tun,
25112511
*/
25122512
stats = this_cpu_ptr(tun->pcpu_stats);
25132513
u64_stats_update_begin(&stats->syncp);
2514-
stats->rx_packets++;
2515-
stats->rx_bytes += datasize;
2514+
u64_stats_inc(&stats->rx_packets);
2515+
u64_stats_add(&stats->rx_bytes, datasize);
25162516
u64_stats_update_end(&stats->syncp);
25172517

25182518
if (rxhash)

0 commit comments

Comments
 (0)