Commit 1d2ac203 authored by David S. Miller's avatar David S. Miller

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: default avatarDavid S. Miller <davem@davemloft.net>
parents e82a35ae e032f7c9
...@@ -2683,7 +2683,7 @@ static int tpacket_snd(struct packet_sock *po, struct msghdr *msg) ...@@ -2683,7 +2683,7 @@ static int tpacket_snd(struct packet_sock *po, struct msghdr *msg)
} }
if (likely(saddr == NULL)) { if (likely(saddr == NULL)) {
dev = packet_cached_dev_get(po); dev = packet_cached_dev_get(po);
proto = po->num; proto = READ_ONCE(po->num);
} else { } else {
err = -EINVAL; err = -EINVAL;
if (msg->msg_namelen < sizeof(struct sockaddr_ll)) 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) ...@@ -2896,7 +2896,7 @@ static int packet_snd(struct socket *sock, struct msghdr *msg, size_t len)
if (likely(saddr == NULL)) { if (likely(saddr == NULL)) {
dev = packet_cached_dev_get(po); dev = packet_cached_dev_get(po);
proto = po->num; proto = READ_ONCE(po->num);
} else { } else {
err = -EINVAL; err = -EINVAL;
if (msg->msg_namelen < sizeof(struct sockaddr_ll)) 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, ...@@ -3171,7 +3171,7 @@ static int packet_do_bind(struct sock *sk, const char *name, int ifindex,
/* prevents packet_notifier() from calling /* prevents packet_notifier() from calling
* register_prot_hook() * register_prot_hook()
*/ */
po->num = 0; WRITE_ONCE(po->num, 0);
__unregister_prot_hook(sk, true); __unregister_prot_hook(sk, true);
rcu_read_lock(); rcu_read_lock();
dev_curr = po->prot_hook.dev; dev_curr = po->prot_hook.dev;
...@@ -3181,17 +3181,17 @@ static int packet_do_bind(struct sock *sk, const char *name, int ifindex, ...@@ -3181,17 +3181,17 @@ static int packet_do_bind(struct sock *sk, const char *name, int ifindex,
} }
BUG_ON(po->running); BUG_ON(po->running);
po->num = proto; WRITE_ONCE(po->num, proto);
po->prot_hook.type = proto; po->prot_hook.type = proto;
if (unlikely(unlisted)) { if (unlikely(unlisted)) {
dev_put(dev); dev_put(dev);
po->prot_hook.dev = NULL; po->prot_hook.dev = NULL;
po->ifindex = -1; WRITE_ONCE(po->ifindex, -1);
packet_cached_dev_reset(po); packet_cached_dev_reset(po);
} else { } else {
po->prot_hook.dev = dev; po->prot_hook.dev = dev;
po->ifindex = dev ? dev->ifindex : 0; WRITE_ONCE(po->ifindex, dev ? dev->ifindex : 0);
packet_cached_dev_assign(po, dev); packet_cached_dev_assign(po, dev);
} }
} }
...@@ -3505,7 +3505,7 @@ static int packet_getname_spkt(struct socket *sock, struct sockaddr *uaddr, ...@@ -3505,7 +3505,7 @@ static int packet_getname_spkt(struct socket *sock, struct sockaddr *uaddr,
uaddr->sa_family = AF_PACKET; uaddr->sa_family = AF_PACKET;
memset(uaddr->sa_data, 0, sizeof(uaddr->sa_data)); memset(uaddr->sa_data, 0, sizeof(uaddr->sa_data));
rcu_read_lock(); rcu_read_lock();
dev = dev_get_by_index_rcu(sock_net(sk), pkt_sk(sk)->ifindex); dev = dev_get_by_index_rcu(sock_net(sk), READ_ONCE(pkt_sk(sk)->ifindex));
if (dev) if (dev)
strlcpy(uaddr->sa_data, dev->name, sizeof(uaddr->sa_data)); strlcpy(uaddr->sa_data, dev->name, sizeof(uaddr->sa_data));
rcu_read_unlock(); rcu_read_unlock();
...@@ -3520,16 +3520,18 @@ static int packet_getname(struct socket *sock, struct sockaddr *uaddr, ...@@ -3520,16 +3520,18 @@ static int packet_getname(struct socket *sock, struct sockaddr *uaddr,
struct sock *sk = sock->sk; struct sock *sk = sock->sk;
struct packet_sock *po = pkt_sk(sk); struct packet_sock *po = pkt_sk(sk);
DECLARE_SOCKADDR(struct sockaddr_ll *, sll, uaddr); DECLARE_SOCKADDR(struct sockaddr_ll *, sll, uaddr);
int ifindex;
if (peer) if (peer)
return -EOPNOTSUPP; return -EOPNOTSUPP;
ifindex = READ_ONCE(po->ifindex);
sll->sll_family = AF_PACKET; sll->sll_family = AF_PACKET;
sll->sll_ifindex = po->ifindex; sll->sll_ifindex = ifindex;
sll->sll_protocol = po->num; sll->sll_protocol = READ_ONCE(po->num);
sll->sll_pkttype = 0; sll->sll_pkttype = 0;
rcu_read_lock(); rcu_read_lock();
dev = dev_get_by_index_rcu(sock_net(sk), po->ifindex); dev = dev_get_by_index_rcu(sock_net(sk), ifindex);
if (dev) { if (dev) {
sll->sll_hatype = dev->type; sll->sll_hatype = dev->type;
sll->sll_halen = dev->addr_len; sll->sll_halen = dev->addr_len;
...@@ -4108,7 +4110,7 @@ static int packet_notifier(struct notifier_block *this, ...@@ -4108,7 +4110,7 @@ static int packet_notifier(struct notifier_block *this,
} }
if (msg == NETDEV_UNREGISTER) { if (msg == NETDEV_UNREGISTER) {
packet_cached_dev_reset(po); packet_cached_dev_reset(po);
po->ifindex = -1; WRITE_ONCE(po->ifindex, -1);
if (po->prot_hook.dev) if (po->prot_hook.dev)
dev_put(po->prot_hook.dev); dev_put(po->prot_hook.dev);
po->prot_hook.dev = NULL; po->prot_hook.dev = NULL;
...@@ -4414,7 +4416,7 @@ static int packet_set_ring(struct sock *sk, union tpacket_req_u *req_u, ...@@ -4414,7 +4416,7 @@ static int packet_set_ring(struct sock *sk, union tpacket_req_u *req_u,
was_running = po->running; was_running = po->running;
num = po->num; num = po->num;
if (was_running) { if (was_running) {
po->num = 0; WRITE_ONCE(po->num, 0);
__unregister_prot_hook(sk, false); __unregister_prot_hook(sk, false);
} }
spin_unlock(&po->bind_lock); spin_unlock(&po->bind_lock);
...@@ -4449,7 +4451,7 @@ static int packet_set_ring(struct sock *sk, union tpacket_req_u *req_u, ...@@ -4449,7 +4451,7 @@ static int packet_set_ring(struct sock *sk, union tpacket_req_u *req_u,
spin_lock(&po->bind_lock); spin_lock(&po->bind_lock);
if (was_running) { if (was_running) {
po->num = num; WRITE_ONCE(po->num, num);
register_prot_hook(sk); register_prot_hook(sk);
} }
spin_unlock(&po->bind_lock); spin_unlock(&po->bind_lock);
...@@ -4619,8 +4621,8 @@ static int packet_seq_show(struct seq_file *seq, void *v) ...@@ -4619,8 +4621,8 @@ static int packet_seq_show(struct seq_file *seq, void *v)
s, s,
refcount_read(&s->sk_refcnt), refcount_read(&s->sk_refcnt),
s->sk_type, s->sk_type,
ntohs(po->num), ntohs(READ_ONCE(po->num)),
po->ifindex, READ_ONCE(po->ifindex),
po->running, po->running,
atomic_read(&s->sk_rmem_alloc), atomic_read(&s->sk_rmem_alloc),
from_kuid_munged(seq_user_ns(seq), sock_i_uid(s)), from_kuid_munged(seq_user_ns(seq), sock_i_uid(s)),
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment