net: bonding: use atomic instead of rtnl_mutex, to make sure peer notify updated
Subsystems (3)
PATCH v4 1/1 net: bonding: use atomic instead of rtnl_mutex, to make sure peer notify updated
Using atomic to protect the send_peer_notif instead of rtnl_mutex. This approach allows safe updates in both interrupt and process contexts, while avoiding code complexity.
In lacp mode, the rtnl might be locked, preventing ad_cond_set_peer_notif() from acquiring the lock and updating send_peer_notif. This patch addresses the issue by using a atomic. Since updating send_peer_notif does not require high real-time performance, such atomic updates are acceptable.
By the way, send_peer_notif is reset atomically. For avoiding peer notify event loss, we should invoke bond_should_notify_peers() to check whether to send peer notify in bond_mii_monitor() and bond_activebackup_arp_mon().
More importantly, send_peer_notif-- should be placed with send_peer_notif *if* block [2]. Otherwise [1], if atomic_dec is executed immediately after resetting the send_peer_notif, it is very likely that the event will be lost.
- [1]:
if (send_peer_notif) {
...
}
// if reset and then atomic_dec, event will be lost.
atomic_dec()- [2] should be changed:
if (send_peer_notif) {
...
// if reset and then atomic_dec, event may be lost.
// but we are already in notify context.
atomic_dec()
}---
v4:
- fix race in bond_activebackup_arp_mon()
- check send_peer_notif instead of should_notify_peers.
- move send_peer_notif-- in if block.
v3:
- add the comment, *_dec_if_positive is safe.
v2:
- refine the codes
- check bond_should_notify_peers again in bond_mii_monitor(), to avoid event loss. v1:
- https://patchwork.kernel.org/project/netdevbpf/patch/[email protected]/
--- drivers/net/bonding/bond_3ad.c | 7 +--- drivers/net/bonding/bond_main.c | 71 +++++++++++++++++---------------- include/net/bonding.h | 9 ++++- 3 files changed, 47 insertions(+), 40 deletions(-)
drivers/net/bonding/bond_3ad.c +2 -5
@@ -999,11 +999,8 @@ static void ad_cond_set_peer_notif(struct port *port) {struct bonding *bond = port->slave->bond; -if (bond->params.broadcast_neighbor &&rtnl_trylock ()) { - bond->send_peer_notif = bond->params.num_peer_notif * -max (1 , bond->params.peer_notif_delay); -rtnl_unlock (); - } +if (bond->params.broadcast_neighbor) +bond_peer_notify_reset (bond); }/**
drivers/net/bonding/bond_main.c +37 -34
165 lines not on this page · raw message
include/net/bonding.h +8 -1
@@ -236,7 +236,7 @@ struct bonding {*/ spinlock_t mode_lock;spinlock_t stats_lock; -u32 send_peer_notif; +atomic_t send_peer_notif;u8 igmp_retrans;#ifdef CONFIG_PROC_FS struct proc_dir_entry *proc_entry;
@@ -814,4 +814,11 @@ static inline netdev_tx_t bond_tx_drop(struct net_device *dev, struct sk_buff *s return NET_XMIT_DROP; } +static inline void bond_peer_notify_reset (struct bonding *bond) +{ +atomic_set (&bond->send_peer_notif, + bond->params.num_peer_notif * +max (1 , bond->params.peer_notif_delay)); +} +#endif /* _NET_BONDING_H */
Signature · 1 line
2.34.1
Paolo Abeni
maintainerinclude/net/bonding.h static inline netdev_tx_t bond_tx_drop(struct net_device *dev, struct sk_buff *s · line 821
Quoted 9 lines · Tonghao Zhang
include/net/bonding.h +5
-0@@ -814,4 +814,11 @@ static inline netdev_tx_t bond_tx_drop(struct net_device *dev, struct sk_buff *sreturn NET_XMIT_DROP ; } +static inline void bond_peer_notify_reset (struct bonding *bond) +{ +atomic_set (&bond->send_peer_notif, + bond->params.num_peer_notif * +max (1 , bond->params.peer_notif_delay));
The above reads params.* without any due lock; at very least it should include READ_ONCE() annotation here (and WRITE_ONCE on the param update side).
But it also mean it could observe inconsistent values for params.num_peer_notif and params.peer_notif_delay in case of concurrent update.
The possible race between bond_mii_monitor() and bond_change_active_slave() concurrently updating send_peer_notif is still avoided by the rtnl lock, so the changelog is IMHO confusing WRT the actual code semantic.
/P
Quoted 9 lines · Paolo Abeni
Quoted 4 lines · Tonghao Zhang
include/net/bonding.h +0
-0@@ -814,4 +814,11 @@ static inline netdev_tx_t bond_tx_drop(struct net_device *dev, struct sk_buff *sreturn NET_XMIT_DROP ; }
Quoted 9 lines
include/net/bonding.h static inline netdev_tx_t bond_tx_drop(struct net_device *dev, struct sk_buff *s · line 821
Quoted 5 lines
include/net/bonding.h +5
-0+static inline void bond_peer_notify_reset (struct bonding *bond) +{ +atomic_set (&bond->send_peer_notif, + bond->params.num_peer_notif * +max (1 , bond->params.peer_notif_delay));The above reads params.* without any due lock; at very least it should include
READ_ONCE()annotation here (andWRITE_ONCEon the param update side).
Yes, the rtnl lock is necessary.
Quoted 9 lines
But it also mean it could observe inconsistent values for params.num_peer_notif and params.peer_notif_delay in case of concurrent update.
The possible race between
bond_mii_monitor()andbond_change_active_slave()concurrently updatingsend_peer_notifis still avoided by the rtnl lock, so the changelog is IMHO confusing WRT the actual code semantic.
In fact, replacing rtnl lock with atomic is not easy (bonding parameters updates and call_netdevice_notifiers still require rtnl lock), and atomic makes the update logic more complicated. I think the workqueue can solve the problem of updating send_peer_notif.
Hi Jay, can I repost the patch? https://patchwork.kernel.org/project/netdevbpf/patch/[email protected]/
Quoted 4 lines
/P