Skip to content

Commit 56440d7

Browse files
edumazetkuba-moo
authored andcommitted
genetlink: hold RCU in genlmsg_mcast()
While running net selftests with CONFIG_PROVE_RCU_LIST=y I saw one lockdep splat [1]. genlmsg_mcast() uses for_each_net_rcu(), and must therefore hold RCU. Instead of letting all callers guard genlmsg_multicast_allns() with a rcu_read_lock()/rcu_read_unlock() pair, do it in genlmsg_mcast(). This also means the @flags parameter is useless, we need to always use GFP_ATOMIC. [1] [10882.424136] ============================= [10882.424166] WARNING: suspicious RCU usage [10882.424309] 6.12.0-rc2-virtme #1156 Not tainted [10882.424400] ----------------------------- [10882.424423] net/netlink/genetlink.c:1940 RCU-list traversed in non-reader section!! [10882.424469] other info that might help us debug this: [10882.424500] rcu_scheduler_active = 2, debug_locks = 1 [10882.424744] 2 locks held by ip/15677: [10882.424791] #0: ffffffffb6b491b0 (cb_lock){++++}-{3:3}, at: genl_rcv (net/netlink/genetlink.c:1219) [10882.426334] #1: ffffffffb6b49248 (genl_mutex){+.+.}-{3:3}, at: genl_rcv_msg (net/netlink/genetlink.c:61 net/netlink/genetlink.c:57 net/netlink/genetlink.c:1209) [10882.426465] stack backtrace: [10882.426805] CPU: 14 UID: 0 PID: 15677 Comm: ip Not tainted 6.12.0-rc2-virtme #1156 [10882.426919] Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS 1.16.3-debian-1.16.3-2 04/01/2014 [10882.427046] Call Trace: [10882.427131] <TASK> [10882.427244] dump_stack_lvl (lib/dump_stack.c:123) [10882.427335] lockdep_rcu_suspicious (kernel/locking/lockdep.c:6822) [10882.427387] genlmsg_multicast_allns (net/netlink/genetlink.c:1940 (discriminator 7) net/netlink/genetlink.c:1977 (discriminator 7)) [10882.427436] l2tp_tunnel_notify.constprop.0 (net/l2tp/l2tp_netlink.c:119) l2tp_netlink [10882.427683] l2tp_nl_cmd_tunnel_create (net/l2tp/l2tp_netlink.c:253) l2tp_netlink [10882.427748] genl_family_rcv_msg_doit (net/netlink/genetlink.c:1115) [10882.427834] genl_rcv_msg (net/netlink/genetlink.c:1195 net/netlink/genetlink.c:1210) [10882.427877] ? __pfx_l2tp_nl_cmd_tunnel_create (net/l2tp/l2tp_netlink.c:186) l2tp_netlink [10882.427927] ? __pfx_genl_rcv_msg (net/netlink/genetlink.c:1201) [10882.427959] netlink_rcv_skb (net/netlink/af_netlink.c:2551) [10882.428069] genl_rcv (net/netlink/genetlink.c:1220) [10882.428095] netlink_unicast (net/netlink/af_netlink.c:1332 net/netlink/af_netlink.c:1357) [10882.428140] netlink_sendmsg (net/netlink/af_netlink.c:1901) [10882.428210] ____sys_sendmsg (net/socket.c:729 (discriminator 1) net/socket.c:744 (discriminator 1) net/socket.c:2607 (discriminator 1)) Fixes: 33f72e6 ("l2tp : multicast notification to the registered listeners") Signed-off-by: Eric Dumazet <[email protected]> Cc: James Chapman <[email protected]> Cc: Tom Parkin <[email protected]> Cc: Johannes Berg <[email protected]> Link: https://patch.msgid.link/[email protected] Signed-off-by: Jakub Kicinski <[email protected]>
1 parent 1833d8a commit 56440d7

File tree

5 files changed

+20
-25
lines changed

5 files changed

+20
-25
lines changed

drivers/target/target_core_user.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2130,7 +2130,7 @@ static int tcmu_netlink_event_send(struct tcmu_dev *udev,
21302130
}
21312131

21322132
ret = genlmsg_multicast_allns(&tcmu_genl_family, skb, 0,
2133-
TCMU_MCGRP_CONFIG, GFP_KERNEL);
2133+
TCMU_MCGRP_CONFIG);
21342134

21352135
/* Wait during an add as the listener may not be up yet */
21362136
if (ret == 0 ||

include/net/genetlink.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -531,13 +531,12 @@ static inline int genlmsg_multicast(const struct genl_family *family,
531531
* @skb: netlink message as socket buffer
532532
* @portid: own netlink portid to avoid sending to yourself
533533
* @group: offset of multicast group in groups array
534-
* @flags: allocation flags
535534
*
536535
* This function must hold the RTNL or rcu_read_lock().
537536
*/
538537
int genlmsg_multicast_allns(const struct genl_family *family,
539538
struct sk_buff *skb, u32 portid,
540-
unsigned int group, gfp_t flags);
539+
unsigned int group);
541540

542541
/**
543542
* genlmsg_unicast - unicast a netlink message

net/l2tp/l2tp_netlink.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ static int l2tp_tunnel_notify(struct genl_family *family,
116116
NLM_F_ACK, tunnel, cmd);
117117

118118
if (ret >= 0) {
119-
ret = genlmsg_multicast_allns(family, msg, 0, 0, GFP_ATOMIC);
119+
ret = genlmsg_multicast_allns(family, msg, 0, 0);
120120
/* We don't care if no one is listening */
121121
if (ret == -ESRCH)
122122
ret = 0;
@@ -144,7 +144,7 @@ static int l2tp_session_notify(struct genl_family *family,
144144
NLM_F_ACK, session, cmd);
145145

146146
if (ret >= 0) {
147-
ret = genlmsg_multicast_allns(family, msg, 0, 0, GFP_ATOMIC);
147+
ret = genlmsg_multicast_allns(family, msg, 0, 0);
148148
/* We don't care if no one is listening */
149149
if (ret == -ESRCH)
150150
ret = 0;

net/netlink/genetlink.c

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1501,15 +1501,11 @@ static int genl_ctrl_event(int event, const struct genl_family *family,
15011501
if (IS_ERR(msg))
15021502
return PTR_ERR(msg);
15031503

1504-
if (!family->netnsok) {
1504+
if (!family->netnsok)
15051505
genlmsg_multicast_netns(&genl_ctrl, &init_net, msg, 0,
15061506
0, GFP_KERNEL);
1507-
} else {
1508-
rcu_read_lock();
1509-
genlmsg_multicast_allns(&genl_ctrl, msg, 0,
1510-
0, GFP_ATOMIC);
1511-
rcu_read_unlock();
1512-
}
1507+
else
1508+
genlmsg_multicast_allns(&genl_ctrl, msg, 0, 0);
15131509

15141510
return 0;
15151511
}
@@ -1929,23 +1925,23 @@ static int __init genl_init(void)
19291925

19301926
core_initcall(genl_init);
19311927

1932-
static int genlmsg_mcast(struct sk_buff *skb, u32 portid, unsigned long group,
1933-
gfp_t flags)
1928+
static int genlmsg_mcast(struct sk_buff *skb, u32 portid, unsigned long group)
19341929
{
19351930
struct sk_buff *tmp;
19361931
struct net *net, *prev = NULL;
19371932
bool delivered = false;
19381933
int err;
19391934

1935+
rcu_read_lock();
19401936
for_each_net_rcu(net) {
19411937
if (prev) {
1942-
tmp = skb_clone(skb, flags);
1938+
tmp = skb_clone(skb, GFP_ATOMIC);
19431939
if (!tmp) {
19441940
err = -ENOMEM;
19451941
goto error;
19461942
}
19471943
err = nlmsg_multicast(prev->genl_sock, tmp,
1948-
portid, group, flags);
1944+
portid, group, GFP_ATOMIC);
19491945
if (!err)
19501946
delivered = true;
19511947
else if (err != -ESRCH)
@@ -1954,27 +1950,31 @@ static int genlmsg_mcast(struct sk_buff *skb, u32 portid, unsigned long group,
19541950

19551951
prev = net;
19561952
}
1953+
err = nlmsg_multicast(prev->genl_sock, skb, portid, group, GFP_ATOMIC);
1954+
1955+
rcu_read_unlock();
19571956

1958-
err = nlmsg_multicast(prev->genl_sock, skb, portid, group, flags);
19591957
if (!err)
19601958
delivered = true;
19611959
else if (err != -ESRCH)
19621960
return err;
19631961
return delivered ? 0 : -ESRCH;
19641962
error:
1963+
rcu_read_unlock();
1964+
19651965
kfree_skb(skb);
19661966
return err;
19671967
}
19681968

19691969
int genlmsg_multicast_allns(const struct genl_family *family,
19701970
struct sk_buff *skb, u32 portid,
1971-
unsigned int group, gfp_t flags)
1971+
unsigned int group)
19721972
{
19731973
if (WARN_ON_ONCE(group >= family->n_mcgrps))
19741974
return -EINVAL;
19751975

19761976
group = family->mcgrp_offset + group;
1977-
return genlmsg_mcast(skb, portid, group, flags);
1977+
return genlmsg_mcast(skb, portid, group);
19781978
}
19791979
EXPORT_SYMBOL(genlmsg_multicast_allns);
19801980

net/wireless/nl80211.c

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17986,10 +17986,8 @@ void nl80211_common_reg_change_event(enum nl80211_commands cmd_id,
1798617986

1798717987
genlmsg_end(msg, hdr);
1798817988

17989-
rcu_read_lock();
1799017989
genlmsg_multicast_allns(&nl80211_fam, msg, 0,
17991-
NL80211_MCGRP_REGULATORY, GFP_ATOMIC);
17992-
rcu_read_unlock();
17990+
NL80211_MCGRP_REGULATORY);
1799317991

1799417992
return;
1799517993

@@ -18722,10 +18720,8 @@ void nl80211_send_beacon_hint_event(struct wiphy *wiphy,
1872218720

1872318721
genlmsg_end(msg, hdr);
1872418722

18725-
rcu_read_lock();
1872618723
genlmsg_multicast_allns(&nl80211_fam, msg, 0,
18727-
NL80211_MCGRP_REGULATORY, GFP_ATOMIC);
18728-
rcu_read_unlock();
18724+
NL80211_MCGRP_REGULATORY);
1872918725

1873018726
return;
1873118727

0 commit comments

Comments
 (0)