af_packet: move notifier's packet_dev_mc out of rcu critical section
Subsystems (3)
PATCH 1/1 af_packet: move notifier's packet_dev_mc out of rcu critical section
Calling PACKET_ADD_MEMBERSHIP on an ops-locked device can trigger the NETDEV_UNREGISTER notifier, which may require disabling promiscuous and/or allmulti mode. Both of these operations require acquiring the netdev instance lock. Move the call to packet_dev_mc outside of the RCU critical section.
--- net/packet/af_packet.c | 20 +++++++++++++++----- net/packet/internal.h | 1 + 2 files changed, 16 insertions(+), 5 deletions(-)
net/packet/af_packet.c +15 -5
@@ -3713,15 +3713,15 @@ static int packet_dev_mc(struct net_device *dev, struct packet_mclist *i, }static void packet_dev_mclist_delete (struct net_device *dev, -struct packet_mclist **mlp) +struct packet_mclist **mlp, +struct list_head *list) {struct packet_mclist *ml;while ((ml = *mlp) !=NULL ) {if (ml->ifindex == dev->ifindex) { -packet_dev_mc (dev, ml,-1 ); +list_add (&ml->remove_list, list); *mlp = ml->next; -kfree (ml); }else mlp = &ml->next; }
@@ -4233,9 +4233,11 @@ static int packet_getsockopt(struct socket *sock, int level, int optname,static int packet_notifier (struct notifier_block *this,unsigned long msg,void *ptr) { -struct sock *sk;struct net_device *dev =netdev_notifier_info_to_dev (ptr);struct net *net =dev_net (dev); +struct packet_mclist *ml, *tmp; +LIST_HEAD (mclist); +struct sock *sk;rcu_read_lock ();sk_for_each_rcu (sk, &net->packet.sklist) {
@@ -4244,7 +4246,8 @@ static int packet_notifier(struct notifier_block *this,switch (msg) {case NETDEV_UNREGISTER :if (po->mclist) -packet_dev_mclist_delete (dev, &po->mclist); +packet_dev_mclist_delete (dev, &po->mclist, + &mclist); fallthrough;case NETDEV_DOWN :
@@ -4277,6 +4280,13 @@ static int packet_notifier(struct notifier_block *this, } }rcu_read_unlock (); + +/* packet_dev_mc might grab instance locks so can't run under rcu */ +list_for_each_entry_safe (ml, tmp, &mclist, remove_list) { +packet_dev_mc (dev, ml,-1 ); +kfree (ml); + } +return NOTIFY_DONE ; }
net/packet/internal.h +1 -0
@@ -11,6 +11,7 @@ struct packet_mclist {unsigned short type;unsigned short alen;unsigned char addr[MAX_ADDR_LEN ]; +struct list_head remove_list; };/* kbdq - kernel block descriptor queue */
Signature · 1 line
2.49.0
Willem de Bruijn
maintainerQuoted 2 lines · Stanislav Fomichev
Calling
PACKET_ADD_MEMBERSHIPon an ops-locked device can trigger theNETDEV_UNREGISTERnotifier,
This made it sound to me as if the notifier is called as a result of the setsockopt.
If respinning, please rewrite to make clear that the two are independent events. The stack trace in the bug also makes clear that the notifier trigger is a device going away.
Quoted 61 lines
which may require disabling promiscuous
and/or allmulti mode. Both of these operations require acquiring the netdev instance lock. Move the call topacket_dev_mcoutside of the RCU critical section.Reported-by [email protected]Fixes ad7c7b2172c3 ("net: hold netdev instance lock during sysfs operations")Signed-off-by Stanislav Fomichev <[email protected]>--- net/packet/af_packet.c | 20 +++++++++++++++----- net/packet/internal.h | 1 + 2 files changed, 16 insertions(+), 5 deletions(-)net/packet/af_packet.c +8
-5@@ -3713,15 +3713,15 @@ static int packet_dev_mc(struct net_device *dev, struct packet_mclist *i, }static void packet_dev_mclist_delete (struct net_device *dev, -struct packet_mclist **mlp) +struct packet_mclist **mlp, +struct list_head *list) {struct packet_mclist *ml;while ((ml = *mlp) !=NULL ) {if (ml->ifindex == dev->ifindex) { -packet_dev_mc (dev, ml,-1 ); +list_add (&ml->remove_list, list); *mlp = ml->next; -kfree (ml); }else mlp = &ml->next; }@@ -4233,9 +4233,11 @@ static int packet_getsockopt(struct socket *sock, int level, int optname,static int packet_notifier (struct notifier_block *this,unsigned long msg,void *ptr) { -struct sock *sk;struct net_device *dev =netdev_notifier_info_to_dev (ptr);struct net *net =dev_net (dev); +struct packet_mclist *ml, *tmp; +LIST_HEAD (mclist); +struct sock *sk;rcu_read_lock ();sk_for_each_rcu (sk, &net->packet.sklist) {@@ -4244,7 +4246,8 @@ static int packet_notifier(struct notifier_block *this,switch (msg) {case NETDEV_UNREGISTER :if (po->mclist) -packet_dev_mclist_delete (dev, &po->mclist); +packet_dev_mclist_delete (dev, &po->mclist, + &mclist); fallthrough;case NETDEV_DOWN :
net/packet/af_packet.c static int packet_notifier(struct notifier_block *this, · line 4289
Quoted 10 lines
net/packet/af_packet.c +7
-0@@ -4277,6 +4280,13 @@ static int packet_notifier(struct notifier_block *this, } }rcu_read_unlock (); + +/* packet_dev_mc might grab instance locks so can't run under rcu */ +list_for_each_entry_safe (ml, tmp, &mclist, remove_list) { +packet_dev_mc (dev, ml,-1 ); +kfree (ml); + } +
Just verifying my understanding of the not entirely obvious locking:
po->mclist modifications (add, del, flush, unregister) are all protected by the RTNL, not the RCU. The RCU only protects the sklist and by extension the sks on it. So moving the mclist operations out of the RCU is fine.
The delayed operation on the mclist entry is still within the RTNL from unregister_netdevice_notifier. Which matter as it protects not only the list, but also the actual operations in packet_dev_mc, such as inc/dec on dev->promiscuity and associated dev_change_rx_flags. And new packet_mclist.remove_list too.
net/packet/internal.h struct packet_mclist { · line 14
Quoted 12 lines
net/packet/af_packet.c +0
-0return NOTIFY_DONE ; }net/packet/internal.h +1
-0@@ -11,6 +11,7 @@ struct packet_mclist {unsigned short type;unsigned short alen;unsigned char addr[MAX_ADDR_LEN ]; +struct list_head remove_list;
INIT_LIST_HEAD on alloc in packet_mc_add?
Quoted 6 lines
net/packet/internal.h +0
-0};/* kbdq - kernel block descriptor queue */ Signature · 1 line
2.49.0
Jakub Kicinski
maintainerQuoted 19 lines · On Tue, 20 May 2025 22:41:30 -0400 Willem de Bruijn wrote:
net/packet/af_packet.c static int packet_notifier(struct notifier_block *this, · line 4289
Quoted 11 lines
net/packet/af_packet.c +7
-0@@ -4277,6 +4280,13 @@ static int packet_notifier(struct notifier_block *this, } }rcu_read_unlock (); + +/* packet_dev_mc might grab instance locks so can't run under rcu */ +list_for_each_entry_safe (ml, tmp, &mclist, remove_list) { +packet_dev_mc (dev, ml,-1 ); +kfree (ml); + } +Just verifying my understanding of the not entirely obvious locking:
po->mclistmodifications (add, del, flush, unregister) are all protected by the RTNL, not the RCU. The RCU only protects the sklist and by extension the sks on it. So moving the mclist operations out of the RCU is fine.
Quoted 5 lines
The delayed operation on the mclist entry is still within the RTNL from
unregister_netdevice_notifier. Which matter as it protects not only the list, but also the actual operations inpacket_dev_mc, such as inc/dec ondev->promiscuityand associateddev_change_rx_flags. And new packet_mclist.remove_list too.
Matches my understanding FWIW, but this will be a great addition to the commit message. Let's add it in v2..
Quoted 14 lines
net/packet/internal.h struct packet_mclist { · line 14
Quoted 12 lines
net/packet/af_packet.c +0
-0return NOTIFY_DONE ; }net/packet/internal.h +1
-0@@ -11,6 +11,7 @@ struct packet_mclist {unsigned short type;unsigned short alen;unsigned char addr[MAX_ADDR_LEN ]; +struct list_head remove_list;
INIT_LIST_HEADon alloc inpacket_mc_add?
Just to be clear this is an "entry node" not a "head node", is it common to init "entry nodes"?
Signature · 2 lines
for the commit msg: pw-bot: cr
Willem de Bruijn
maintainerQuoted 44 lines · Jakub Kicinski
Quoted 19 lines · On Tue, 20 May 2025 22:41:30 -0400 Willem de Bruijn wrote:
net/packet/af_packet.c static int packet_notifier(struct notifier_block *this, · line 4289
Quoted 11 lines
net/packet/af_packet.c +7
-0@@ -4277,6 +4280,13 @@ static int packet_notifier(struct notifier_block *this, } }rcu_read_unlock (); + +/* packet_dev_mc might grab instance locks so can't run under rcu */ +list_for_each_entry_safe (ml, tmp, &mclist, remove_list) { +packet_dev_mc (dev, ml,-1 ); +kfree (ml); + } +Just verifying my understanding of the not entirely obvious locking:
po->mclistmodifications (add, del, flush, unregister) are all protected by the RTNL, not the RCU. The RCU only protects the sklist and by extension the sks on it. So moving the mclist operations out of the RCU is fine.Quoted 5 lines
The delayed operation on the mclist entry is still within the RTNL from
unregister_netdevice_notifier. Which matter as it protects not only the list, but also the actual operations inpacket_dev_mc, such as inc/dec ondev->promiscuityand associateddev_change_rx_flags. And new packet_mclist.remove_list too.Matches my understanding FWIW, but this will be a great addition to the commit message. Let's add it in v2..
Quoted 14 lines
net/packet/internal.h struct packet_mclist { · line 14
Quoted 12 lines
net/packet/af_packet.c +0
-0return NOTIFY_DONE ; }net/packet/internal.h +1
-0@@ -11,6 +11,7 @@ struct packet_mclist {unsigned short type;unsigned short alen;unsigned char addr[MAX_ADDR_LEN ]; +struct list_head remove_list;
INIT_LIST_HEADon alloc inpacket_mc_add?
Quoted 2 lines
Just to be clear this is an "entry node" not a "head node", is it common to init "entry nodes"?
I wasn't sure. A small sample from net/core showed that many do, e.g., napi->poll_list. But not all, e.g., failover->list just calls list_add_tail immediately.
I suspect, and from that it seems, that it is safe to not explicitly initalize entry nodes if you know what you're doing / how they're used.
But whether that is actually intended to work, especially with more involved debugging (such as LIST_POISON) and invariant checking (__list_add_valid), I don't know.
I did not find any authoritative documentation that says you have too, so I guess it's fine. But not ideal.
Quoted 56 lines · Willem de Bruijn
Quoted 44 lines · Jakub Kicinski
Quoted 19 lines · On Tue, 20 May 2025 22:41:30 -0400 Willem de Bruijn wrote:
net/packet/af_packet.c static int packet_notifier(struct notifier_block *this, · line 4289
Quoted 11 lines
net/packet/af_packet.c +7
-0@@ -4277,6 +4280,13 @@ static int packet_notifier(struct notifier_block *this, } }rcu_read_unlock (); + +/* packet_dev_mc might grab instance locks so can't run under rcu */ +list_for_each_entry_safe (ml, tmp, &mclist, remove_list) { +packet_dev_mc (dev, ml,-1 ); +kfree (ml); + } +Just verifying my understanding of the not entirely obvious locking:
po->mclistmodifications (add, del, flush, unregister) are all protected by the RTNL, not the RCU. The RCU only protects the sklist and by extension the sks on it. So moving the mclist operations out of the RCU is fine.Quoted 5 lines
The delayed operation on the mclist entry is still within the RTNL from
unregister_netdevice_notifier. Which matter as it protects not only the list, but also the actual operations inpacket_dev_mc, such as inc/dec ondev->promiscuityand associateddev_change_rx_flags. And new packet_mclist.remove_list too.Matches my understanding FWIW, but this will be a great addition to the commit message. Let's add it in v2..
Quoted 14 lines
net/packet/internal.h struct packet_mclist { · line 14
Quoted 12 lines
net/packet/af_packet.c +0
-0return NOTIFY_DONE ; }net/packet/internal.h +1
-0@@ -11,6 +11,7 @@ struct packet_mclist {unsigned short type;unsigned short alen;unsigned char addr[MAX_ADDR_LEN ]; +struct list_head remove_list;
INIT_LIST_HEADon alloc inpacket_mc_add?Quoted 2 lines
Just to be clear this is an "entry node" not a "head node", is it common to init "entry nodes"?
I wasn't sure. A small sample from net/core showed that many do, e.g.,
napi->poll_list. But not all, e.g.,failover->listjust callslist_add_tailimmediately.I suspect, and from that it seems, that it is safe to not explicitly initalize entry nodes if you know what you're doing / how they're used.
Quoted 6 lines
But whether that is actually intended to work, especially with more involved debugging (such as
LIST_POISON) and invariant checking (__list_add_valid), I don't know.I did not find any authoritative documentation that says you have too, so I guess it's fine. But not ideal.
I can add the initialization to be safe and add more info to the commit message, thank you both for the feedback!