Skip to content

Commit 3440fa3

Browse files
edumazetkuba-moo
authored andcommitted
inet: ipmr: fix data-races
Following fields of 'struct mr_mfc' can be updated concurrently (no lock protection) from ip_mr_forward() and ip6_mr_forward() - bytes - pkt - wrong_if - lastuse They also can be read from other functions. Convert bytes, pkt and wrong_if to atomic_long_t, and use READ_ONCE()/WRITE_ONCE() for lastuse. Fixes: 1da177e4c3f4 ("Linux-2.6.12-rc2") Signed-off-by: Eric Dumazet <edumazet@google.com> Reviewed-by: David Ahern <dsahern@kernel.org> Link: https://patch.msgid.link/20250114221049.1190631-1-edumazet@google.com Signed-off-by: Jakub Kicinski <kuba@kernel.org>
1 parent bf38a2f commit 3440fa3

File tree

5 files changed

+38
-38
lines changed

5 files changed

+38
-38
lines changed

drivers/net/ethernet/mellanox/mlxsw/spectrum_mr.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1003,10 +1003,10 @@ static void mlxsw_sp_mr_route_stats_update(struct mlxsw_sp *mlxsw_sp,
10031003
mr->mr_ops->route_stats(mlxsw_sp, mr_route->route_priv, &packets,
10041004
&bytes);
10051005

1006-
if (mr_route->mfc->mfc_un.res.pkt != packets)
1007-
mr_route->mfc->mfc_un.res.lastuse = jiffies;
1008-
mr_route->mfc->mfc_un.res.pkt = packets;
1009-
mr_route->mfc->mfc_un.res.bytes = bytes;
1006+
if (atomic_long_read(&mr_route->mfc->mfc_un.res.pkt) != packets)
1007+
WRITE_ONCE(mr_route->mfc->mfc_un.res.lastuse, jiffies);
1008+
atomic_long_set(&mr_route->mfc->mfc_un.res.pkt, packets);
1009+
atomic_long_set(&mr_route->mfc->mfc_un.res.bytes, bytes);
10101010
}
10111011

10121012
static void mlxsw_sp_mr_stats_update(struct work_struct *work)

include/linux/mroute_base.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -146,9 +146,9 @@ struct mr_mfc {
146146
unsigned long last_assert;
147147
int minvif;
148148
int maxvif;
149-
unsigned long bytes;
150-
unsigned long pkt;
151-
unsigned long wrong_if;
149+
atomic_long_t bytes;
150+
atomic_long_t pkt;
151+
atomic_long_t wrong_if;
152152
unsigned long lastuse;
153153
unsigned char ttls[MAXVIFS];
154154
refcount_t refcount;

net/ipv4/ipmr.c

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -831,7 +831,7 @@ static void ipmr_update_thresholds(struct mr_table *mrt, struct mr_mfc *cache,
831831
cache->mfc_un.res.maxvif = vifi + 1;
832832
}
833833
}
834-
cache->mfc_un.res.lastuse = jiffies;
834+
WRITE_ONCE(cache->mfc_un.res.lastuse, jiffies);
835835
}
836836

837837
static int vif_add(struct net *net, struct mr_table *mrt,
@@ -1681,9 +1681,9 @@ int ipmr_ioctl(struct sock *sk, int cmd, void *arg)
16811681
rcu_read_lock();
16821682
c = ipmr_cache_find(mrt, sr->src.s_addr, sr->grp.s_addr);
16831683
if (c) {
1684-
sr->pktcnt = c->_c.mfc_un.res.pkt;
1685-
sr->bytecnt = c->_c.mfc_un.res.bytes;
1686-
sr->wrong_if = c->_c.mfc_un.res.wrong_if;
1684+
sr->pktcnt = atomic_long_read(&c->_c.mfc_un.res.pkt);
1685+
sr->bytecnt = atomic_long_read(&c->_c.mfc_un.res.bytes);
1686+
sr->wrong_if = atomic_long_read(&c->_c.mfc_un.res.wrong_if);
16871687
rcu_read_unlock();
16881688
return 0;
16891689
}
@@ -1753,9 +1753,9 @@ int ipmr_compat_ioctl(struct sock *sk, unsigned int cmd, void __user *arg)
17531753
rcu_read_lock();
17541754
c = ipmr_cache_find(mrt, sr.src.s_addr, sr.grp.s_addr);
17551755
if (c) {
1756-
sr.pktcnt = c->_c.mfc_un.res.pkt;
1757-
sr.bytecnt = c->_c.mfc_un.res.bytes;
1758-
sr.wrong_if = c->_c.mfc_un.res.wrong_if;
1756+
sr.pktcnt = atomic_long_read(&c->_c.mfc_un.res.pkt);
1757+
sr.bytecnt = atomic_long_read(&c->_c.mfc_un.res.bytes);
1758+
sr.wrong_if = atomic_long_read(&c->_c.mfc_un.res.wrong_if);
17591759
rcu_read_unlock();
17601760

17611761
if (copy_to_user(arg, &sr, sizeof(sr)))
@@ -1988,9 +1988,9 @@ static void ip_mr_forward(struct net *net, struct mr_table *mrt,
19881988
int vif, ct;
19891989

19901990
vif = c->_c.mfc_parent;
1991-
c->_c.mfc_un.res.pkt++;
1992-
c->_c.mfc_un.res.bytes += skb->len;
1993-
c->_c.mfc_un.res.lastuse = jiffies;
1991+
atomic_long_inc(&c->_c.mfc_un.res.pkt);
1992+
atomic_long_add(skb->len, &c->_c.mfc_un.res.bytes);
1993+
WRITE_ONCE(c->_c.mfc_un.res.lastuse, jiffies);
19941994

19951995
if (c->mfc_origin == htonl(INADDR_ANY) && true_vifi >= 0) {
19961996
struct mfc_cache *cache_proxy;
@@ -2021,7 +2021,7 @@ static void ip_mr_forward(struct net *net, struct mr_table *mrt,
20212021
goto dont_forward;
20222022
}
20232023

2024-
c->_c.mfc_un.res.wrong_if++;
2024+
atomic_long_inc(&c->_c.mfc_un.res.wrong_if);
20252025

20262026
if (true_vifi >= 0 && mrt->mroute_do_assert &&
20272027
/* pimsm uses asserts, when switching from RPT to SPT,
@@ -3029,9 +3029,9 @@ static int ipmr_mfc_seq_show(struct seq_file *seq, void *v)
30293029

30303030
if (it->cache != &mrt->mfc_unres_queue) {
30313031
seq_printf(seq, " %8lu %8lu %8lu",
3032-
mfc->_c.mfc_un.res.pkt,
3033-
mfc->_c.mfc_un.res.bytes,
3034-
mfc->_c.mfc_un.res.wrong_if);
3032+
atomic_long_read(&mfc->_c.mfc_un.res.pkt),
3033+
atomic_long_read(&mfc->_c.mfc_un.res.bytes),
3034+
atomic_long_read(&mfc->_c.mfc_un.res.wrong_if));
30353035
for (n = mfc->_c.mfc_un.res.minvif;
30363036
n < mfc->_c.mfc_un.res.maxvif; n++) {
30373037
if (VIF_EXISTS(mrt, n) &&

net/ipv4/ipmr_base.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -263,9 +263,9 @@ int mr_fill_mroute(struct mr_table *mrt, struct sk_buff *skb,
263263
lastuse = READ_ONCE(c->mfc_un.res.lastuse);
264264
lastuse = time_after_eq(jiffies, lastuse) ? jiffies - lastuse : 0;
265265

266-
mfcs.mfcs_packets = c->mfc_un.res.pkt;
267-
mfcs.mfcs_bytes = c->mfc_un.res.bytes;
268-
mfcs.mfcs_wrong_if = c->mfc_un.res.wrong_if;
266+
mfcs.mfcs_packets = atomic_long_read(&c->mfc_un.res.pkt);
267+
mfcs.mfcs_bytes = atomic_long_read(&c->mfc_un.res.bytes);
268+
mfcs.mfcs_wrong_if = atomic_long_read(&c->mfc_un.res.wrong_if);
269269
if (nla_put_64bit(skb, RTA_MFC_STATS, sizeof(mfcs), &mfcs, RTA_PAD) ||
270270
nla_put_u64_64bit(skb, RTA_EXPIRES, jiffies_to_clock_t(lastuse),
271271
RTA_PAD))

net/ipv6/ip6mr.c

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -520,9 +520,9 @@ static int ipmr_mfc_seq_show(struct seq_file *seq, void *v)
520520

521521
if (it->cache != &mrt->mfc_unres_queue) {
522522
seq_printf(seq, " %8lu %8lu %8lu",
523-
mfc->_c.mfc_un.res.pkt,
524-
mfc->_c.mfc_un.res.bytes,
525-
mfc->_c.mfc_un.res.wrong_if);
523+
atomic_long_read(&mfc->_c.mfc_un.res.pkt),
524+
atomic_long_read(&mfc->_c.mfc_un.res.bytes),
525+
atomic_long_read(&mfc->_c.mfc_un.res.wrong_if));
526526
for (n = mfc->_c.mfc_un.res.minvif;
527527
n < mfc->_c.mfc_un.res.maxvif; n++) {
528528
if (VIF_EXISTS(mrt, n) &&
@@ -884,7 +884,7 @@ static void ip6mr_update_thresholds(struct mr_table *mrt,
884884
cache->mfc_un.res.maxvif = vifi + 1;
885885
}
886886
}
887-
cache->mfc_un.res.lastuse = jiffies;
887+
WRITE_ONCE(cache->mfc_un.res.lastuse, jiffies);
888888
}
889889

890890
static int mif6_add(struct net *net, struct mr_table *mrt,
@@ -1945,9 +1945,9 @@ int ip6mr_ioctl(struct sock *sk, int cmd, void *arg)
19451945
c = ip6mr_cache_find(mrt, &sr->src.sin6_addr,
19461946
&sr->grp.sin6_addr);
19471947
if (c) {
1948-
sr->pktcnt = c->_c.mfc_un.res.pkt;
1949-
sr->bytecnt = c->_c.mfc_un.res.bytes;
1950-
sr->wrong_if = c->_c.mfc_un.res.wrong_if;
1948+
sr->pktcnt = atomic_long_read(&c->_c.mfc_un.res.pkt);
1949+
sr->bytecnt = atomic_long_read(&c->_c.mfc_un.res.bytes);
1950+
sr->wrong_if = atomic_long_read(&c->_c.mfc_un.res.wrong_if);
19511951
rcu_read_unlock();
19521952
return 0;
19531953
}
@@ -2017,9 +2017,9 @@ int ip6mr_compat_ioctl(struct sock *sk, unsigned int cmd, void __user *arg)
20172017
rcu_read_lock();
20182018
c = ip6mr_cache_find(mrt, &sr.src.sin6_addr, &sr.grp.sin6_addr);
20192019
if (c) {
2020-
sr.pktcnt = c->_c.mfc_un.res.pkt;
2021-
sr.bytecnt = c->_c.mfc_un.res.bytes;
2022-
sr.wrong_if = c->_c.mfc_un.res.wrong_if;
2020+
sr.pktcnt = atomic_long_read(&c->_c.mfc_un.res.pkt);
2021+
sr.bytecnt = atomic_long_read(&c->_c.mfc_un.res.bytes);
2022+
sr.wrong_if = atomic_long_read(&c->_c.mfc_un.res.wrong_if);
20232023
rcu_read_unlock();
20242024

20252025
if (copy_to_user(arg, &sr, sizeof(sr)))
@@ -2142,9 +2142,9 @@ static void ip6_mr_forward(struct net *net, struct mr_table *mrt,
21422142
int true_vifi = ip6mr_find_vif(mrt, dev);
21432143

21442144
vif = c->_c.mfc_parent;
2145-
c->_c.mfc_un.res.pkt++;
2146-
c->_c.mfc_un.res.bytes += skb->len;
2147-
c->_c.mfc_un.res.lastuse = jiffies;
2145+
atomic_long_inc(&c->_c.mfc_un.res.pkt);
2146+
atomic_long_add(skb->len, &c->_c.mfc_un.res.bytes);
2147+
WRITE_ONCE(c->_c.mfc_un.res.lastuse, jiffies);
21482148

21492149
if (ipv6_addr_any(&c->mf6c_origin) && true_vifi >= 0) {
21502150
struct mfc6_cache *cache_proxy;
@@ -2162,7 +2162,7 @@ static void ip6_mr_forward(struct net *net, struct mr_table *mrt,
21622162
* Wrong interface: drop packet and (maybe) send PIM assert.
21632163
*/
21642164
if (rcu_access_pointer(mrt->vif_table[vif].dev) != dev) {
2165-
c->_c.mfc_un.res.wrong_if++;
2165+
atomic_long_inc(&c->_c.mfc_un.res.wrong_if);
21662166

21672167
if (true_vifi >= 0 && mrt->mroute_do_assert &&
21682168
/* pimsm uses asserts, when switching from RPT to SPT,

0 commit comments

Comments
 (0)