Skip to content

Commit 1d2ac20

Browse files
committed
Merge branch 'net-packet-data-races'
Eric Dumazet says: ==================== net/packet: annotate data races KCSAN sent two reports about data races in af_packet. Nothing serious, but worth fixing. ==================== Signed-off-by: David S. Miller <[email protected]>
2 parents e82a35a + e032f7c commit 1d2ac20

File tree

1 file changed

+17
-15
lines changed

1 file changed

+17
-15
lines changed

net/packet/af_packet.c

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2683,7 +2683,7 @@ static int tpacket_snd(struct packet_sock *po, struct msghdr *msg)
26832683
}
26842684
if (likely(saddr == NULL)) {
26852685
dev = packet_cached_dev_get(po);
2686-
proto = po->num;
2686+
proto = READ_ONCE(po->num);
26872687
} else {
26882688
err = -EINVAL;
26892689
if (msg->msg_namelen < sizeof(struct sockaddr_ll))
@@ -2896,7 +2896,7 @@ static int packet_snd(struct socket *sock, struct msghdr *msg, size_t len)
28962896

28972897
if (likely(saddr == NULL)) {
28982898
dev = packet_cached_dev_get(po);
2899-
proto = po->num;
2899+
proto = READ_ONCE(po->num);
29002900
} else {
29012901
err = -EINVAL;
29022902
if (msg->msg_namelen < sizeof(struct sockaddr_ll))
@@ -3171,7 +3171,7 @@ static int packet_do_bind(struct sock *sk, const char *name, int ifindex,
31713171
/* prevents packet_notifier() from calling
31723172
* register_prot_hook()
31733173
*/
3174-
po->num = 0;
3174+
WRITE_ONCE(po->num, 0);
31753175
__unregister_prot_hook(sk, true);
31763176
rcu_read_lock();
31773177
dev_curr = po->prot_hook.dev;
@@ -3181,17 +3181,17 @@ static int packet_do_bind(struct sock *sk, const char *name, int ifindex,
31813181
}
31823182

31833183
BUG_ON(po->running);
3184-
po->num = proto;
3184+
WRITE_ONCE(po->num, proto);
31853185
po->prot_hook.type = proto;
31863186

31873187
if (unlikely(unlisted)) {
31883188
dev_put(dev);
31893189
po->prot_hook.dev = NULL;
3190-
po->ifindex = -1;
3190+
WRITE_ONCE(po->ifindex, -1);
31913191
packet_cached_dev_reset(po);
31923192
} else {
31933193
po->prot_hook.dev = dev;
3194-
po->ifindex = dev ? dev->ifindex : 0;
3194+
WRITE_ONCE(po->ifindex, dev ? dev->ifindex : 0);
31953195
packet_cached_dev_assign(po, dev);
31963196
}
31973197
}
@@ -3505,7 +3505,7 @@ static int packet_getname_spkt(struct socket *sock, struct sockaddr *uaddr,
35053505
uaddr->sa_family = AF_PACKET;
35063506
memset(uaddr->sa_data, 0, sizeof(uaddr->sa_data));
35073507
rcu_read_lock();
3508-
dev = dev_get_by_index_rcu(sock_net(sk), pkt_sk(sk)->ifindex);
3508+
dev = dev_get_by_index_rcu(sock_net(sk), READ_ONCE(pkt_sk(sk)->ifindex));
35093509
if (dev)
35103510
strlcpy(uaddr->sa_data, dev->name, sizeof(uaddr->sa_data));
35113511
rcu_read_unlock();
@@ -3520,16 +3520,18 @@ static int packet_getname(struct socket *sock, struct sockaddr *uaddr,
35203520
struct sock *sk = sock->sk;
35213521
struct packet_sock *po = pkt_sk(sk);
35223522
DECLARE_SOCKADDR(struct sockaddr_ll *, sll, uaddr);
3523+
int ifindex;
35233524

35243525
if (peer)
35253526
return -EOPNOTSUPP;
35263527

3528+
ifindex = READ_ONCE(po->ifindex);
35273529
sll->sll_family = AF_PACKET;
3528-
sll->sll_ifindex = po->ifindex;
3529-
sll->sll_protocol = po->num;
3530+
sll->sll_ifindex = ifindex;
3531+
sll->sll_protocol = READ_ONCE(po->num);
35303532
sll->sll_pkttype = 0;
35313533
rcu_read_lock();
3532-
dev = dev_get_by_index_rcu(sock_net(sk), po->ifindex);
3534+
dev = dev_get_by_index_rcu(sock_net(sk), ifindex);
35333535
if (dev) {
35343536
sll->sll_hatype = dev->type;
35353537
sll->sll_halen = dev->addr_len;
@@ -4108,7 +4110,7 @@ static int packet_notifier(struct notifier_block *this,
41084110
}
41094111
if (msg == NETDEV_UNREGISTER) {
41104112
packet_cached_dev_reset(po);
4111-
po->ifindex = -1;
4113+
WRITE_ONCE(po->ifindex, -1);
41124114
if (po->prot_hook.dev)
41134115
dev_put(po->prot_hook.dev);
41144116
po->prot_hook.dev = NULL;
@@ -4414,7 +4416,7 @@ static int packet_set_ring(struct sock *sk, union tpacket_req_u *req_u,
44144416
was_running = po->running;
44154417
num = po->num;
44164418
if (was_running) {
4417-
po->num = 0;
4419+
WRITE_ONCE(po->num, 0);
44184420
__unregister_prot_hook(sk, false);
44194421
}
44204422
spin_unlock(&po->bind_lock);
@@ -4449,7 +4451,7 @@ static int packet_set_ring(struct sock *sk, union tpacket_req_u *req_u,
44494451

44504452
spin_lock(&po->bind_lock);
44514453
if (was_running) {
4452-
po->num = num;
4454+
WRITE_ONCE(po->num, num);
44534455
register_prot_hook(sk);
44544456
}
44554457
spin_unlock(&po->bind_lock);
@@ -4619,8 +4621,8 @@ static int packet_seq_show(struct seq_file *seq, void *v)
46194621
s,
46204622
refcount_read(&s->sk_refcnt),
46214623
s->sk_type,
4622-
ntohs(po->num),
4623-
po->ifindex,
4624+
ntohs(READ_ONCE(po->num)),
4625+
READ_ONCE(po->ifindex),
46244626
po->running,
46254627
atomic_read(&s->sk_rmem_alloc),
46264628
from_kuid_munged(seq_user_ns(seq), sock_i_uid(s)),

0 commit comments

Comments
 (0)