bond: fix xfrm offload issues
Subsystems (4)
v5 cover bond: fix xfrm offload issues
The first patch fixes the incorrect locks using in bond driver. The second patch fixes the xfrm offload feature during setup active-backup mode. The third patch add a ipsec offload testing.
v5: use list_for_each_entry_safe() when del item in list (Nikolay Aleksandrov)
do not call spin_lock_bh in sleep function xdo_dev_state_free (Nikolay Aleksandrov) set xso.real_dev = NULL to avoid __xfrm_state_delete() is called in parallel() (Cosmin Ratiu) remove spin lock in bond_ipsec_add_sa_all() as it doesn't resolve the race condition.
v4: hold xs->lock for bond_ipsec_{del, add}_sa_all (Cosmin Ratiu)
use the defer helpers in lib.sh for selftest (Petr Machata)
v3: move the ipsec deletion to bond_ipsec_free_sa (Cosmin Ratiu)
v2: do not turn carrier on if bond change link failed (Nikolay Aleksandrov)
move the mutex lock to a work queue (Cosmin Ratiu)
Hangbin Liu (3):
bonding: fix calling sleeping function in spin lock and some race
conditions
bonding: fix xfrm offload feature setup on active-backup mode
selftests: bonding: add ipsec offload test
drivers/net/bonding/bond_main.c | 71 +++++--- drivers/net/bonding/bond_netlink.c | 16 +- include/net/bonding.h | 1 + .../selftests/drivers/net/bonding/Makefile | 3 +- .../drivers/net/bonding/bond_ipsec_offload.sh | 154 ++++++++++++++++++ .../selftests/drivers/net/bonding/config | 4 + 6 files changed, 222 insertions(+), 27 deletions(-) create mode 100755 tools/testing/selftests/drivers/net/bonding/bond_ipsec_offload.sh
Signature · 1 line
2.46.0
PATCH v5 1/3 bonding: fix calling sleeping function in spin lock and some race conditions
The fixed commit placed mutex_lock() inside spin_lock_bh(), which triggers a warning:
BUG: sleeping function called from invalid context at...
Fix this by moving the IPsec deletion operation to bond_ipsec_free_sa, which is not held by spin_lock_bh().
Additionally, there are also some race conditions as bond_ipsec_del_sa_all() and __xfrm_state_delete could running in parallel without any lock. e.g.
bond_ipsec_del_sa_all() __xfrm_state_delete()
- .xdo_dev_state_delete - bond_ipsec_del_sa()
- .xdo_dev_state_free - .xdo_dev_state_delete()
- bond_ipsec_free_sa()
bond active_slave changes - .xdo_dev_state_free()
bond_ipsec_add_sa_all()
- ipsec->xs->xso.real_dev = real_dev;
- xdo_dev_state_add
To fix this, let's add xs->lock during bond_ipsec_del_sa_all(), and delete the IPsec list when the XFRM state is DEAD, which could prevent xdo_dev_state_free() from being triggered again in bond_ipsec_free_sa().
In bond_ipsec_add_sa(), if .xdo_dev_state_add() failed, the xso.real_dev is set without clean. Which will cause trouble if __xfrm_state_delete is called at the same time. Reset the xso.real_dev to NULL if state add failed.
Despite the above fixes, there are still races in bond_ipsec_add_sa() and bond_ipsec_add_sa_all(). If __xfrm_state_delete() is called immediately after we set the xso.real_dev and before .xdo_dev_state_add() is finished, like
ipsec->xs->xso.real_dev = real_dev;
__xfrm_state_delete
- bond_ipsec_del_sa()
- .xdo_dev_state_delete()
- bond_ipsec_free_sa()
- .xdo_dev_state_free() .xdo_dev_state_add()
But there is no good solution yet. So I just added a FIXME note in here and hope we can fix it in future.
--- drivers/net/bonding/bond_main.c | 69 ++++++++++++++++++++++++--------- 1 file changed, 51 insertions(+), 18 deletions(-)
drivers/net/bonding/bond_main.c +51 -18
@@ -506,6 +506,7 @@ static int bond_ipsec_add_sa(struct xfrm_state *xs,list_add (&ipsec->list, &bond->ipsec_list);mutex_unlock (&bond->ipsec_lock); }else { + xs->xso.real_dev =NULL ;kfree (ipsec); } out:
@@ -541,7 +542,15 @@ static void bond_ipsec_add_sa_all(struct bonding *bond)if (ipsec->xs->xso.real_dev == real_dev)continue ; +/* Skip dead xfrm states, they'll be freed later. */ +if (ipsec->xs->km.state ==XFRM_STATE_DEAD ) +continue ; + ipsec->xs->xso.real_dev = real_dev; +/* FIXME: there is a race that before .xdo_dev_state_add() +* is called, the __xfrm_state_delete() is called in parallel, +* which will call .xdo_dev_state_delete() and xdo_dev_state_free() +*/ if (real_dev->xfrmdev_ops->xdo_dev_state_add (ipsec->xs,NULL )) {slave_warn (bond_dev, real_dev,"%s: failed to add SA\n" , __func__); ipsec->xs->xso.real_dev =NULL ;
@@ -560,7 +569,6 @@ static void bond_ipsec_del_sa(struct xfrm_state *xs)struct net_device *bond_dev = xs->xso.dev;struct net_device *real_dev;netdevice_tracker tracker; -struct bond_ipsec *ipsec;struct bonding *bond;struct slave *slave;
@@ -592,22 +600,13 @@ static void bond_ipsec_del_sa(struct xfrm_state *xs) real_dev->xfrmdev_ops->xdo_dev_state_delete (xs); out:netdev_put (real_dev, &tracker); -mutex_lock (&bond->ipsec_lock); -list_for_each_entry (ipsec, &bond->ipsec_list, list) { -if (ipsec->xs == xs) { -list_del (&ipsec->list); -kfree (ipsec); -break ; - } - } -mutex_unlock (&bond->ipsec_lock); }static void bond_ipsec_del_sa_all (struct bonding *bond) {struct net_device *bond_dev = bond->dev; +struct bond_ipsec *ipsec, *tmp_ipsec;struct net_device *real_dev; -struct bond_ipsec *ipsec;struct slave *slave; slave =rtnl_dereference (bond->curr_active_slave);
@@ -616,9 +615,22 @@ static void bond_ipsec_del_sa_all(struct bonding *bond)return ;mutex_lock (&bond->ipsec_lock); -list_for_each_entry (ipsec, &bond->ipsec_list, list) { -if (!ipsec->xs->xso.real_dev) +list_for_each_entry_safe (ipsec, tmp_ipsec, &bond->ipsec_list, list) { +spin_lock_bh (&ipsec->xs->lock); +if (!ipsec->xs->xso.real_dev) { +spin_unlock_bh (&ipsec->xs->lock);continue ; + } + +if (ipsec->xs->km.state ==XFRM_STATE_DEAD ) { +list_del (&ipsec->list); +kfree (ipsec); +/* Need to free device here, or the xs->xso.real_dev +* may changed in bond_ipsec_add_sa_all and free +* on old device will never be called. +*/ + goto next; + } if (!real_dev->xfrmdev_ops || !real_dev->xfrmdev_ops->xdo_dev_state_delete ||
@@ -626,11 +638,20 @@ static void bond_ipsec_del_sa_all(struct bonding *bond)slave_warn (bond_dev, real_dev,"%s: no slave xdo_dev_state_delete\n" , __func__); - }else { - real_dev->xfrmdev_ops->xdo_dev_state_delete (ipsec->xs); -if (real_dev->xfrmdev_ops->xdo_dev_state_free) - real_dev->xfrmdev_ops->xdo_dev_state_free (ipsec->xs); +spin_unlock_bh (&ipsec->xs->lock); +continue ; } + + real_dev->xfrmdev_ops->xdo_dev_state_delete (ipsec->xs); + +next: +/* set real_dev to NULL in case __xfrm_state_delete() is called in parallel */ + ipsec->xs->xso.real_dev =NULL ; + +/* Unlock before freeing device state, it could sleep. */ +spin_unlock_bh (&ipsec->xs->lock); +if (real_dev->xfrmdev_ops->xdo_dev_state_free) + real_dev->xfrmdev_ops->xdo_dev_state_free (ipsec->xs); }mutex_unlock (&bond->ipsec_lock); }
@@ -638,6 +659,7 @@ static void bond_ipsec_del_sa_all(struct bonding *bond)static void bond_ipsec_free_sa (struct xfrm_state *xs) {struct net_device *bond_dev = xs->xso.dev; +struct bond_ipsec *ipsec, *tmp_ipsec;struct net_device *real_dev;netdevice_tracker tracker;struct bonding *bond;
@@ -659,13 +681,24 @@ static void bond_ipsec_free_sa(struct xfrm_state *xs)if (!xs->xso.real_dev) goto out; -WARN_ON (xs->xso.real_dev != real_dev); +if (xs->xso.real_dev != real_dev) + goto out;if (real_dev && real_dev->xfrmdev_ops && real_dev->xfrmdev_ops->xdo_dev_state_free) real_dev->xfrmdev_ops->xdo_dev_state_free (xs); out:netdev_put (real_dev, &tracker); + +mutex_lock (&bond->ipsec_lock); +list_for_each_entry_safe (ipsec, tmp_ipsec, &bond->ipsec_list, list) { +if (ipsec->xs == xs) { +list_del (&ipsec->list); +kfree (ipsec); +break ; + } + } +mutex_unlock (&bond->ipsec_lock); }/**
Signature · 1 line
2.46.0
Re: [PATCHv5 net 1/3] bonding: fix calling sleeping function in spin lock and some race conditions
Quoted 68 lines · Hangbin Liu
The fixed commit placed
mutex_lock()insidespin_lock_bh(), which triggers a warning:BUG: sleeping function called from invalid context at...Fix this by moving the IPsec deletion operation to
bond_ipsec_free_sa, which is not held byspin_lock_bh().Additionally, there are also some race conditions as
bond_ipsec_del_sa_all()and__xfrm_state_deletecould running in parallel without any lock. e.g.
bond_ipsec_del_sa_all()__xfrm_state_delete()
- .xdo_dev_state_delete -bond_ipsec_del_sa()
- .xdo_dev_state_free - .xdo_dev_state_delete()
-bond_ipsec_free_sa()
bondactive_slavechanges - .xdo_dev_state_free()
bond_ipsec_add_sa_all()
-ipsec->xs->xso.real_dev=real_dev;
-xdo_dev_state_addTo fix this, let's add
xs->lockduringbond_ipsec_del_sa_all(), and delete the IPsec list when the XFRM state is DEAD, which could preventxdo_dev_state_free()from being triggered again inbond_ipsec_free_sa().In
bond_ipsec_add_sa(), if .xdo_dev_state_add() failed, the xso.real_dev is set without clean. Which will cause trouble if__xfrm_state_deleteis called at the same time. Reset the xso.real_dev to NULL if state add failed.Despite the above fixes, there are still races in
bond_ipsec_add_sa()andbond_ipsec_add_sa_all(). If__xfrm_state_delete()is called immediately after we set the xso.real_dev and before .xdo_dev_state_add() is finished, like
ipsec->xs->xso.real_dev=real_dev;
__xfrm_state_delete
-bond_ipsec_del_sa()
- .xdo_dev_state_delete()
-bond_ipsec_free_sa()
- .xdo_dev_state_free() .xdo_dev_state_add()But there is no good solution yet. So I just added a FIXME note in here and hope we can fix it in future.
Fixes 2aeeef906d5a ("bonding: change ipsec_lock from spin lock to mutex")Reported-by Jakub Kicinski <[email protected]>Suggested-by Cosmin Ratiu <[email protected]>Signed-off-by Hangbin Liu <[email protected]>--- drivers/net/bonding/bond_main.c | 69 ++++++++++++++++++++++++--------- 1 file changed, 51 insertions(+), 18 deletions(-)drivers/net/bonding/bond_main.c +1
-0@@ -506,6 +506,7 @@ static int bond_ipsec_add_sa(struct xfrm_state *xs,list_add (&ipsec->list, &bond->ipsec_list);mutex_unlock (&bond->ipsec_lock); }else { + xs->xso.real_dev =NULL ;kfree (ipsec); } out:
drivers/net/bonding/bond_main.c static void bond_ipsec_add_sa_all(struct bonding *bond) · line 547
Quoted 6 lines
drivers/net/bonding/bond_main.c +3
-0@@ -541,7 +542,15 @@ static void bond_ipsec_add_sa_all(struct bonding *bond)if (ipsec->xs->xso.real_dev == real_dev)continue ; +/* Skip dead xfrm states, they'll be freed later. */ +if (ipsec->xs->km.state ==XFRM_STATE_DEAD ) +continue ;
As we commented earlier, reading this state without x->lock is wrong.
drivers/net/bonding/bond_main.c static void bond_ipsec_add_sa_all(struct bonding *bond) · line 556
Quoted 9 lines
drivers/net/bonding/bond_main.c +5
-0+ ipsec->xs->xso.real_dev = real_dev; +/* FIXME: there is a race that before .xdo_dev_state_add() +* is called, the __xfrm_state_delete() is called in parallel, +* which will call .xdo_dev_state_delete() and xdo_dev_state_free() +*/ if (real_dev->xfrmdev_ops->xdo_dev_state_add (ipsec->xs,NULL )) {slave_warn (bond_dev, real_dev,"%s: failed to add SA\n" , __func__); ipsec->xs->xso.real_dev =NULL ;
[snip]
TBH, keeping buggy code with a comment doesn't sound good to me. I'd rather remove this support than tell people "good luck, it might crash". It's better to be safe until a correct design is in place which takes care of these issues.
Cheers,
Nik
Re: [PATCHv5 net 1/3] bonding: fix calling sleeping function in spin lock and some race conditions
Hi Nikolay,
Quoted 72 lines · Nikolay Aleksandrov
Quoted 71 lines · Hangbin Liu
The fixed commit placed
mutex_lock()insidespin_lock_bh(), which triggers a warning:BUG: sleeping function called from invalid context at...Fix this by moving the IPsec deletion operation to
bond_ipsec_free_sa, which is not held byspin_lock_bh().Additionally, there are also some race conditions as
bond_ipsec_del_sa_all()and__xfrm_state_deletecould running in parallel without any lock. e.g.
bond_ipsec_del_sa_all()__xfrm_state_delete()
- .xdo_dev_state_delete -bond_ipsec_del_sa()
- .xdo_dev_state_free - .xdo_dev_state_delete()
-bond_ipsec_free_sa()
bondactive_slavechanges - .xdo_dev_state_free()
bond_ipsec_add_sa_all()
-ipsec->xs->xso.real_dev=real_dev;
-xdo_dev_state_addTo fix this, let's add
xs->lockduringbond_ipsec_del_sa_all(), and delete the IPsec list when the XFRM state is DEAD, which could preventxdo_dev_state_free()from being triggered again inbond_ipsec_free_sa().In
bond_ipsec_add_sa(), if .xdo_dev_state_add() failed, the xso.real_dev is set without clean. Which will cause trouble if__xfrm_state_deleteis called at the same time. Reset the xso.real_dev to NULL if state add failed.Despite the above fixes, there are still races in
bond_ipsec_add_sa()andbond_ipsec_add_sa_all(). If__xfrm_state_delete()is called immediately after we set the xso.real_dev and before .xdo_dev_state_add() is finished, like
ipsec->xs->xso.real_dev=real_dev;
__xfrm_state_delete
-bond_ipsec_del_sa()
- .xdo_dev_state_delete()
-bond_ipsec_free_sa()
- .xdo_dev_state_free() .xdo_dev_state_add()But there is no good solution yet. So I just added a FIXME note in here and hope we can fix it in future.
Fixes 2aeeef906d5a ("bonding: change ipsec_lock from spin lock to mutex")Reported-by Jakub Kicinski <[email protected]>Suggested-by Cosmin Ratiu <[email protected]>Signed-off-by Hangbin Liu <[email protected]>--- drivers/net/bonding/bond_main.c | 69 ++++++++++++++++++++++++--------- 1 file changed, 51 insertions(+), 18 deletions(-)drivers/net/bonding/bond_main.c +1
-0@@ -506,6 +506,7 @@ static int bond_ipsec_add_sa(struct xfrm_state *xs,list_add (&ipsec->list, &bond->ipsec_list);mutex_unlock (&bond->ipsec_lock); }else { + xs->xso.real_dev =NULL ;kfree (ipsec); } out:@@ -541,7 +542,15 @@ static void bond_ipsec_add_sa_all(struct bonding *bond)if (ipsec->xs->xso.real_dev == real_dev)continue ;
Quoted 5 lines
drivers/net/bonding/bond_main.c static void bond_ipsec_add_sa_all(struct bonding *bond) · line 547
Quoted 3 lines
drivers/net/bonding/bond_main.c +3
-0+/* Skip dead xfrm states, they'll be freed later. */ +if (ipsec->xs->km.state ==XFRM_STATE_DEAD ) +continue ;As we commented earlier, reading this state without
x->lockis wrong.
But even we add the lock, like
spin_lock_bh(&ipsec->xs->lock);
if (ipsec->xs->km.state == XFRM_STATE_DEAD) {
spin_unlock_bh(&ipsec->xs->lock);
continue;
}We still may got the race condition. Like the following note said. So I just leave it as the current status. But I can add the spin lock if you insist.
Quoted 11 lines
Quoted 9 lines
drivers/net/bonding/bond_main.c +5
-0+ ipsec->xs->xso.real_dev = real_dev; +/* FIXME: there is a race that before .xdo_dev_state_add() +* is called, the __xfrm_state_delete() is called in parallel, +* which will call .xdo_dev_state_delete() and xdo_dev_state_free() +*/ if (real_dev->xfrmdev_ops->xdo_dev_state_add (ipsec->xs,NULL )) {slave_warn (bond_dev, real_dev,"%s: failed to add SA\n" , __func__); ipsec->xs->xso.real_dev =NULL ;[snip]
Quoted 3 lines
TBH, keeping buggy code with a comment doesn't sound good to me. I'd rather remove this support than tell people "good luck, it might crash". It's better to be safe until a correct design is in place which takes care of these issues.
I agree it's not a good experience to let users using an unstable feature. But this is a race condition, although we don't have a good fix yet.
On the other hand, I think we can't remove a feature people is using, can we? What I can do is try fix the issues as my best.
By the way, I started this patch because my patch 2/3 is blocked by the selftest results from patch 3/3...
Thanks
Hangbin
Re: [PATCHv5 net 1/3] bonding: fix calling sleeping function in spin lock and some race conditions
Quoted 88 lines · Hangbin Liu
Hi Nikolay,
Quoted 72 lines · Nikolay Aleksandrov
Quoted 71 lines · Hangbin Liu
The fixed commit placed
mutex_lock()insidespin_lock_bh(), which triggers a warning:BUG: sleeping function called from invalid context at...Fix this by moving the IPsec deletion operation to
bond_ipsec_free_sa, which is not held byspin_lock_bh().Additionally, there are also some race conditions as
bond_ipsec_del_sa_all()and__xfrm_state_deletecould running in parallel without any lock. e.g.
bond_ipsec_del_sa_all()__xfrm_state_delete()
- .xdo_dev_state_delete -bond_ipsec_del_sa()
- .xdo_dev_state_free - .xdo_dev_state_delete()
-bond_ipsec_free_sa()
bondactive_slavechanges - .xdo_dev_state_free()
bond_ipsec_add_sa_all()
-ipsec->xs->xso.real_dev=real_dev;
-xdo_dev_state_addTo fix this, let's add
xs->lockduringbond_ipsec_del_sa_all(), and delete the IPsec list when the XFRM state is DEAD, which could preventxdo_dev_state_free()from being triggered again inbond_ipsec_free_sa().In
bond_ipsec_add_sa(), if .xdo_dev_state_add() failed, the xso.real_dev is set without clean. Which will cause trouble if__xfrm_state_deleteis called at the same time. Reset the xso.real_dev to NULL if state add failed.Despite the above fixes, there are still races in
bond_ipsec_add_sa()andbond_ipsec_add_sa_all(). If__xfrm_state_delete()is called immediately after we set the xso.real_dev and before .xdo_dev_state_add() is finished, like
ipsec->xs->xso.real_dev=real_dev;
__xfrm_state_delete
-bond_ipsec_del_sa()
- .xdo_dev_state_delete()
-bond_ipsec_free_sa()
- .xdo_dev_state_free() .xdo_dev_state_add()But there is no good solution yet. So I just added a FIXME note in here and hope we can fix it in future.
Fixes 2aeeef906d5a ("bonding: change ipsec_lock from spin lock to mutex")Reported-by Jakub Kicinski <[email protected]>Suggested-by Cosmin Ratiu <[email protected]>Signed-off-by Hangbin Liu <[email protected]>--- drivers/net/bonding/bond_main.c | 69 ++++++++++++++++++++++++--------- 1 file changed, 51 insertions(+), 18 deletions(-)drivers/net/bonding/bond_main.c +1
-0@@ -506,6 +506,7 @@ static int bond_ipsec_add_sa(struct xfrm_state *xs,list_add (&ipsec->list, &bond->ipsec_list);mutex_unlock (&bond->ipsec_lock); }else { + xs->xso.real_dev =NULL ;kfree (ipsec); } out:@@ -541,7 +542,15 @@ static void bond_ipsec_add_sa_all(struct bonding *bond)if (ipsec->xs->xso.real_dev == real_dev)continue ;Quoted 5 lines
drivers/net/bonding/bond_main.c static void bond_ipsec_add_sa_all(struct bonding *bond) · line 547
Quoted 3 lines
drivers/net/bonding/bond_main.c +3
-0+/* Skip dead xfrm states, they'll be freed later. */ +if (ipsec->xs->km.state ==XFRM_STATE_DEAD ) +continue ;As we commented earlier, reading this state without
x->lockis wrong.But even we add the lock, like
spin_lock_bh(&ipsec->xs->lock); if (ipsec->xs->km.state == XFRM_STATE_DEAD) { spin_unlock_bh(&ipsec->xs->lock); continue; }
Quoted 4 lines
We still may got the race condition. Like the following note said. So I just leave it as the current status. But I can add the spin lock if you insist.
I don't insist at all, I just pointed out that this is buggy and the value doesn't make sense used like that. Adding more bugs to the existing code wouldn't make it better.
Quoted 15 lines
Quoted 11 lines
Quoted 9 lines
drivers/net/bonding/bond_main.c +5
-0+ ipsec->xs->xso.real_dev = real_dev; +/* FIXME: there is a race that before .xdo_dev_state_add() +* is called, the __xfrm_state_delete() is called in parallel, +* which will call .xdo_dev_state_delete() and xdo_dev_state_free() +*/ if (real_dev->xfrmdev_ops->xdo_dev_state_add (ipsec->xs,NULL )) {slave_warn (bond_dev, real_dev,"%s: failed to add SA\n" , __func__); ipsec->xs->xso.real_dev =NULL ;[snip]
Quoted 3 lines
TBH, keeping buggy code with a comment doesn't sound good to me. I'd rather remove this support than tell people "good luck, it might crash". It's better to be safe until a correct design is in place which takes care of these issues.
Quoted 6 lines
I agree it's not a good experience to let users using an unstable feature. But this is a race condition, although we don't have a good fix yet.
On the other hand, I think we can't remove a feature people is using, can we? What I can do is try fix the issues as my best.
I do appreciate the hard work you've been doing on this, don't get me wrong, but this is not really uapi, it's an optimization. The path will become slower as it won't be offloaded, but it will still work and will be stable until a proper fix or new design comes in.
Are you suggesting to knowingly leave a race condition that might lead to a number of problems in place with a comment?
IMO that is not ok, but ultimately it's up to the maintainers to decide if they can live with it. :)
Quoted 5 lines
By the way, I started this patch because my patch 2/3 is blocked by the selftest results from patch 3/3...
Thanks
Hangbin
Re: [PATCHv5 net 1/3] bonding: fix calling sleeping function in spin lock and some race conditions
Quoted 89 lines · Nikolay Aleksandrov
Quoted 88 lines · Hangbin Liu
Hi Nikolay,
Quoted 72 lines · Nikolay Aleksandrov
Quoted 71 lines · Hangbin Liu
The fixed commit placed
mutex_lock()insidespin_lock_bh(), which triggers a warning:BUG: sleeping function called from invalid context at...Fix this by moving the IPsec deletion operation to
bond_ipsec_free_sa, which is not held byspin_lock_bh().Additionally, there are also some race conditions as
bond_ipsec_del_sa_all()and__xfrm_state_deletecould running in parallel without any lock. e.g.
bond_ipsec_del_sa_all()__xfrm_state_delete()
- .xdo_dev_state_delete -bond_ipsec_del_sa()
- .xdo_dev_state_free - .xdo_dev_state_delete()
-bond_ipsec_free_sa()
bondactive_slavechanges - .xdo_dev_state_free()
bond_ipsec_add_sa_all()
-ipsec->xs->xso.real_dev=real_dev;
-xdo_dev_state_addTo fix this, let's add
xs->lockduringbond_ipsec_del_sa_all(), and delete the IPsec list when the XFRM state is DEAD, which could preventxdo_dev_state_free()from being triggered again inbond_ipsec_free_sa().In
bond_ipsec_add_sa(), if .xdo_dev_state_add() failed, the xso.real_dev is set without clean. Which will cause trouble if__xfrm_state_deleteis called at the same time. Reset the xso.real_dev to NULL if state add failed.Despite the above fixes, there are still races in
bond_ipsec_add_sa()andbond_ipsec_add_sa_all(). If__xfrm_state_delete()is called immediately after we set the xso.real_dev and before .xdo_dev_state_add() is finished, like
ipsec->xs->xso.real_dev=real_dev;
__xfrm_state_delete
-bond_ipsec_del_sa()
- .xdo_dev_state_delete()
-bond_ipsec_free_sa()
- .xdo_dev_state_free() .xdo_dev_state_add()But there is no good solution yet. So I just added a FIXME note in here and hope we can fix it in future.
Fixes 2aeeef906d5a ("bonding: change ipsec_lock from spin lock to mutex")Reported-by Jakub Kicinski <[email protected]>Suggested-by Cosmin Ratiu <[email protected]>Signed-off-by Hangbin Liu <[email protected]>--- drivers/net/bonding/bond_main.c | 69 ++++++++++++++++++++++++--------- 1 file changed, 51 insertions(+), 18 deletions(-)drivers/net/bonding/bond_main.c +1
-0@@ -506,6 +506,7 @@ static int bond_ipsec_add_sa(struct xfrm_state *xs,list_add (&ipsec->list, &bond->ipsec_list);mutex_unlock (&bond->ipsec_lock); }else { + xs->xso.real_dev =NULL ;kfree (ipsec); } out:@@ -541,7 +542,15 @@ static void bond_ipsec_add_sa_all(struct bonding *bond)if (ipsec->xs->xso.real_dev == real_dev)continue ;Quoted 5 lines
drivers/net/bonding/bond_main.c static void bond_ipsec_add_sa_all(struct bonding *bond) · line 547
Quoted 3 lines
drivers/net/bonding/bond_main.c +3
-0+/* Skip dead xfrm states, they'll be freed later. */ +if (ipsec->xs->km.state ==XFRM_STATE_DEAD ) +continue ;As we commented earlier, reading this state without
x->lockis wrong.But even we add the lock, like
spin_lock_bh(&ipsec->xs->lock); if (ipsec->xs->km.state == XFRM_STATE_DEAD) { spin_unlock_bh(&ipsec->xs->lock); continue; }
Quoted 7 lines
Quoted 4 lines
We still may got the race condition. Like the following note said. So I just leave it as the current status. But I can add the spin lock if you insist.
I don't insist at all, I just pointed out that this is buggy and the value doesn't make sense used like that. Adding more bugs to the existing code wouldn't make it better.
I'm a little lost here. Do you mean we should hold spin lock for all xs readings or just the km.state. The current bonding code didn't hold any xs lock for xs readings. And I saw in xfrm code it only hold rcu read lock when check the km.state.
Quoted 16 lines
Quoted 16 lines
Quoted 15 lines
drivers/net/bonding/bond_main.c static void bond_ipsec_add_sa_all(struct bonding *bond) · line 556
Quoted 10 lines
drivers/net/bonding/bond_main.c +5
-0+ ipsec->xs->xso.real_dev = real_dev; +/* FIXME: there is a race that before .xdo_dev_state_add() +* is called, the __xfrm_state_delete() is called in parallel, +* which will call .xdo_dev_state_delete() and xdo_dev_state_free() +*/ if (real_dev->xfrmdev_ops->xdo_dev_state_add (ipsec->xs,NULL )) {slave_warn (bond_dev, real_dev,"%s: failed to add SA\n" , __func__); ipsec->xs->xso.real_dev =NULL ;[snip]
TBH, keeping buggy code with a comment doesn't sound good to me. I'd rather remove this support than tell people "good luck, it might crash". It's better to be safe until a correct design is in place which takes care of these issues.
Quoted 8 lines
Quoted 6 lines
I agree it's not a good experience to let users using an unstable feature. But this is a race condition, although we don't have a good fix yet.
On the other hand, I think we can't remove a feature people is using, can we? What I can do is try fix the issues as my best.
I do appreciate the hard work you've been doing on this, don't get me wrong, but this is
I also appreciate your reviews. You helped find a lot of issues :)
Quoted 2 lines
not really uapi, it's an optimization. The path will become slower as it won't be offloaded, but it will still work and will be stable until a proper fix or new design comes in.
I'm afraid it doesn't work correctly before this patch. The race condition here has a long time. What I do is fix part of the races. And leave bond_ipsec_add_sa() and bond_ipsec_add_sa_all() almost not changed.
The comment in the code is just to notify others there is something wrong. Instead of just hide it there and only we know this.
Do I miss something?
Thanks
hangbin
Quoted 13 lines
Are you suggesting to knowingly leave a race condition that might lead to a number of problems in place with a comment?
IMO that is not ok, but ultimately it's up to the maintainers to decide if they can live with it. :)Quoted 5 lines
By the way, I started this patch because my patch 2/3 is blocked by the selftest results from patch 3/3...
Thanks
Hangbin
Jakub Kicinski
maintainerRe: [PATCHv5 net 1/3] bonding: fix calling sleeping function in spin lock and some race conditions
In reply to Nikolay AleksandrovQuoted 3 lines · On Fri, 7 Mar 2025 09:42:49 +0200 Nikolay Aleksandrov wrote:
TBH, keeping buggy code with a comment doesn't sound good to me. I'd rather remove this support than tell people "good luck, it might crash". It's better to be safe until a correct design is in place which takes care of these issues.
That's my feeling too, FWIW. I think we knew about this issue for a while now, the longer we wait the more users we may disrupt with the revert.
Re: [PATCHv5 net 1/3] bonding: fix calling sleeping function in spin lock and some race conditions
Quoted 8 lines · Jakub Kicinski
Quoted 3 lines · On Fri, 7 Mar 2025 09:42:49 +0200 Nikolay Aleksandrov wrote:
TBH, keeping buggy code with a comment doesn't sound good to me. I'd rather remove this support than tell people "good luck, it might crash". It's better to be safe until a correct design is in place which takes care of these issues.
That's my feeling too, FWIW. I think we knew about this issue for a while now, the longer we wait the more users we may disrupt with the revert.
Steffen said we can't sleep in xfrm_timer_handler(), which calls __xfrm_state_delete(). So I can't find a way to handle the race condition between bond_ipsec_add_sa_all() -> xdo_dev_state_add, which may sleep. And __xfrm_state_delete() -> xdo_dev_state_delete, which can't sleep.
Hi Jay, do you have any comments?
Thanks
Hangbin
Re: [PATCHv5 net 1/3] bonding: fix calling sleeping function in spin lock and some race conditions
In reply to Jakub KicinskiQuoted 10 lines · Jakub Kicinski
Quoted 5 lines · On Fri, 7 Mar 2025 09:42:49 +0200 Nikolay Aleksandrov wrote:
TBH, keeping buggy code with a comment doesn't sound good to me. I'd rather remove this
support than tell people "good luck, it might crash". It's better to be safe until a
correct design is in place which takes care of these issues.That's my feeling too, FWIW. I think we knew about this issue for a while now, the longer we wait the more users we may disrupt with the revert.
These are preexisting races between the bond link failover and the user removing the xfrm states. Unless the user wants to intentionally trigger these bugs, chances are nobody has ever encountered them in the wild in normal operation. In steady state, bond link failover works, and adding/removing states works. It's the combination of the two control plane events that may have a chance to double free or leak states.
I would not pull everything out just yet.
Today, I managed to find a solution for these races (I think), based on a patch series I am preparing against ipsec-next with other changes related to real_dev.
Hangbin, do you mind if I take over fixing the locking issue as part of my series? I plan to send it upstream the following days.
Cosmin.
Re: [PATCHv5 net 1/3] bonding: fix calling sleeping function in spin lock and some race conditions
Quoted 20 lines · Cosmin Ratiu
Quoted 10 lines · Jakub Kicinski
Quoted 5 lines · On Fri, 7 Mar 2025 09:42:49 +0200 Nikolay Aleksandrov wrote:
TBH, keeping buggy code with a comment doesn't sound good to me. I'd rather remove this
support than tell people "good luck, it might crash". It's better to be safe until a
correct design is in place which takes care of these issues.That's my feeling too, FWIW. I think we knew about this issue for a while now, the longer we wait the more users we may disrupt with the revert.
These are preexisting races between the bond link failover and the user removing the xfrm states. Unless the user wants to intentionally trigger these bugs, chances are nobody has ever encountered them in the wild in normal operation. In steady state, bond link failover works, and adding/removing states works. It's the combination of the two control plane events that may have a chance to double free or leak states.
Quoted 8 lines
I would not pull everything out just yet.
Today, I managed to find a solution for these races (I think), based on a patch series I am preparing against ipsec-next with other changes related to
real_dev.Hangbin, do you mind if I take over fixing the locking issue as part of my series? I plan to send it upstream the following days.
No, I don't mind. Please go ahead to fixing the locking issue. And thanks a lot for your reviewing.
Regards
Hangbin
Simon Horman
reviewerRe: [PATCHv5 net 1/3] bonding: fix calling sleeping function in spin lock and some race conditions
In reply to Hangbin LiuOn Fri, Mar 07, 2025 at 03:19:01AM +0000, Hangbin Liu wrote:
...
Quoted 6 lines
drivers/net/bonding/bond_main.c +0
-2@@ -616,9 +615,22 @@ static void bond_ipsec_del_sa_all(struct bonding *bond)return ;mutex_lock (&bond->ipsec_lock); -list_for_each_entry (ipsec, &bond->ipsec_list, list) { -if (!ipsec->xs->xso.real_dev)
drivers/net/bonding/bond_main.c static void bond_ipsec_del_sa_all(struct bonding *bond) · line 627
Quoted 10 lines
drivers/net/bonding/bond_main.c +9
-0+list_for_each_entry_safe (ipsec, tmp_ipsec, &bond->ipsec_list, list) { +spin_lock_bh (&ipsec->xs->lock); +if (!ipsec->xs->xso.real_dev) { +spin_unlock_bh (&ipsec->xs->lock);continue ; + } + +if (ipsec->xs->km.state ==XFRM_STATE_DEAD ) { +list_del (&ipsec->list); +kfree (ipsec);
Hi Hangbin,
Apologies if this was covered elsewhere, but ipsec is kfree'd here...
Quoted 16 lines
drivers/net/bonding/bond_main.c +6
-3+/* Need to free device here, or the xs->xso.real_dev +* may changed in bond_ipsec_add_sa_all and free +* on old device will never be called. +*/ + goto next; + } if (!real_dev->xfrmdev_ops || !real_dev->xfrmdev_ops->xdo_dev_state_delete ||@@ -626,11 +638,20 @@ static void bond_ipsec_del_sa_all(struct bonding *bond)slave_warn (bond_dev, real_dev,"%s: no slave xdo_dev_state_delete\n" , __func__); - }else { - real_dev->xfrmdev_ops->xdo_dev_state_delete (ipsec->xs); -if (real_dev->xfrmdev_ops->xdo_dev_state_free)
drivers/net/bonding/bond_main.c static void bond_ipsec_del_sa_all(struct bonding *bond) · line 649
Quoted 10 lines
drivers/net/bonding/bond_main.c +8
-1- real_dev->xfrmdev_ops->xdo_dev_state_free (ipsec->xs); +spin_unlock_bh (&ipsec->xs->lock); +continue ; } + + real_dev->xfrmdev_ops->xdo_dev_state_delete (ipsec->xs); + +next: +/* set real_dev to NULL in case __xfrm_state_delete() is called in parallel */ + ipsec->xs->xso.real_dev =NULL ;
... and the dereferenced here.
Flagged by Smatch.
Quoted 8 lines
drivers/net/bonding/bond_main.c +5
-0+ +/* Unlock before freeing device state, it could sleep. */ +spin_unlock_bh (&ipsec->xs->lock); +if (real_dev->xfrmdev_ops->xdo_dev_state_free) + real_dev->xfrmdev_ops->xdo_dev_state_free (ipsec->xs); }mutex_unlock (&bond->ipsec_lock); }
...
Re: [PATCHv5 net 1/3] bonding: fix calling sleeping function in spin lock and some race conditions
Quoted 23 lines · Simon Horman
On Fri, Mar 07, 2025 at 03:19:01AM +0000, Hangbin Liu wrote:
...
Quoted 6 lines
drivers/net/bonding/bond_main.c +0
-2@@ -616,9 +615,22 @@ static void bond_ipsec_del_sa_all(struct bonding *bond)return ;mutex_lock (&bond->ipsec_lock); -list_for_each_entry (ipsec, &bond->ipsec_list, list) { -if (!ipsec->xs->xso.real_dev)drivers/net/bonding/bond_main.c static void bond_ipsec_del_sa_all(struct bonding *bond) · line 627
Quoted 10 lines
drivers/net/bonding/bond_main.c +9
-0+list_for_each_entry_safe (ipsec, tmp_ipsec, &bond->ipsec_list, list) { +spin_lock_bh (&ipsec->xs->lock); +if (!ipsec->xs->xso.real_dev) { +spin_unlock_bh (&ipsec->xs->lock);continue ; + } + +if (ipsec->xs->km.state ==XFRM_STATE_DEAD ) { +list_del (&ipsec->list); +kfree (ipsec);Hi Hangbin,
Quoted 1 line
Apologies if this was covered elsewhere, but ipsec is kfree'd here...
Oh.. I need to get the xs with xs = ipsec->xs, then hold the xs lock.
Thanks
Hangbin
Quoted 43 lines
Quoted 18 lines
drivers/net/bonding/bond_main.c +6
-3+/* Need to free device here, or the xs->xso.real_dev +* may changed in bond_ipsec_add_sa_all and free +* on old device will never be called. +*/ + goto next; + } if (!real_dev->xfrmdev_ops || !real_dev->xfrmdev_ops->xdo_dev_state_delete ||@@ -626,11 +638,20 @@ static void bond_ipsec_del_sa_all(struct bonding *bond)slave_warn (bond_dev, real_dev,"%s: no slave xdo_dev_state_delete\n" , __func__); - }else { - real_dev->xfrmdev_ops->xdo_dev_state_delete (ipsec->xs); -if (real_dev->xfrmdev_ops->xdo_dev_state_free)drivers/net/bonding/bond_main.c static void bond_ipsec_del_sa_all(struct bonding *bond) · line 649
Quoted 10 lines
drivers/net/bonding/bond_main.c +8
-1- real_dev->xfrmdev_ops->xdo_dev_state_free (ipsec->xs); +spin_unlock_bh (&ipsec->xs->lock); +continue ; } + + real_dev->xfrmdev_ops->xdo_dev_state_delete (ipsec->xs); + +next: +/* set real_dev to NULL in case __xfrm_state_delete() is called in parallel */ + ipsec->xs->xso.real_dev =NULL ;... and the dereferenced here.
Flagged by Smatch.
Quoted 8 lines
drivers/net/bonding/bond_main.c +5
-0+ +/* Unlock before freeing device state, it could sleep. */ +spin_unlock_bh (&ipsec->xs->lock); +if (real_dev->xfrmdev_ops->xdo_dev_state_free) + real_dev->xfrmdev_ops->xdo_dev_state_free (ipsec->xs); }mutex_unlock (&bond->ipsec_lock); }...
PATCH v5 2/3 bonding: fix xfrm offload feature setup on active-backup mode
In reply to Hangbin LiuThe active-backup bonding mode supports XFRM ESP offload. However, when a bond is added using command like ip link add bond0 type bond mode 1 miimon 100, the ethtool -k command shows that the XFRM ESP offload is disabled. This occurs because, in bond_newlink(), we change bond link first and register bond device later. So the XFRM feature update in bond_option_mode_set() is not called as the bond device is not yet registered, leading to the offload feature not being set successfully.
To resolve this issue, we can modify the code order in bond_newlink() to ensure that the bond device is registered first before changing the bond link parameters. This change will allow the XFRM ESP offload feature to be correctly enabled.
--- drivers/net/bonding/bond_main.c | 2 +- drivers/net/bonding/bond_netlink.c | 16 +++++++++------- include/net/bonding.h | 1 + 3 files changed, 11 insertions(+), 8 deletions(-)
drivers/net/bonding/bond_main.c +1 -1
@@ -4422,7 +4422,7 @@ void bond_work_init_all(struct bonding *bond)INIT_DELAYED_WORK (&bond->slave_arr_work, bond_slave_arr_handler); } -static void bond_work_cancel_all (struct bonding *bond) +void bond_work_cancel_all (struct bonding *bond) {cancel_delayed_work_sync (&bond->mii_work);cancel_delayed_work_sync (&bond->arp_work);
drivers/net/bonding/bond_netlink.c +9 -7
@@ -568,18 +568,20 @@ static int bond_newlink(struct net *src_net, struct net_device *bond_dev, struct nlattr *tb[], struct nlattr *data[], struct netlink_ext_ack *extack) { + struct bonding *bond = netdev_priv(bond_dev);int err; - err =bond_changelink (bond_dev, tb, data, extack); -if (err <0 ) + err =register_netdevice (bond_dev); +if (err)return err; - err =register_netdevice (bond_dev); -if (!err) { -struct bonding *bond =netdev_priv (bond_dev); +netif_carrier_off (bond_dev); +bond_work_init_all (bond); -netif_carrier_off (bond_dev); -bond_work_init_all (bond); + err =bond_changelink (bond_dev, tb, data, extack); +if (err) { +bond_work_cancel_all (bond); +unregister_netdevice (bond_dev); }return err;
include/net/bonding.h +1 -0
@@ -707,6 +707,7 @@ struct bond_vlan_tag *bond_verify_device_path(struct net_device *start_dev,int bond_update_slave_arr (struct bonding *bond,struct slave *skipslave);void bond_slave_arr_work_rearm (struct bonding *bond,unsigned long delay);void bond_work_init_all (struct bonding *bond); +void bond_work_cancel_all (struct bonding *bond);#ifdef CONFIG_PROC_FS void bond_create_proc_entry (struct bonding *bond);
Signature · 1 line
2.46.0
PATCH v5 3/3 selftests: bonding: add ipsec offload test
In reply to Hangbin LiuThis introduces a test for IPSec offload over bonding, utilizing netdevsim for the testing process, as veth interfaces do not support IPSec offload. The test will ensure that the IPSec offload functionality remains operational even after a failover event occurs in the bonding configuration.
Here is the test result:
TEST: bond_ipsec_offload (active_slave eth0) [ OK ]
TEST: bond_ipsec_offload (active_slave eth1) [ OK ]
--- .../selftests/drivers/net/bonding/Makefile | 3 +- .../drivers/net/bonding/bond_ipsec_offload.sh | 154 ++++++++++++++++++ .../selftests/drivers/net/bonding/config | 4 + 3 files changed, 160 insertions(+), 1 deletion(-) create mode 100755 tools/testing/selftests/drivers/net/bonding/bond_ipsec_offload.sh
tools/testing/selftests/drivers/net/bonding/Makefile +2 -1
@@ -10,7 +10,8 @@ TEST_PROGS := \ mode-2-recovery-updelay.sh \ bond_options.sh \ bond-eth-type-change.sh \ - bond_macvlan_ipvlan.sh + bond_macvlan_ipvlan.sh \ + bond_ipsec_offload.sh TEST_FILES := \ lag_lib.sh \
tools/testing/selftests/drivers/net/bonding/bond_ipsec_offload.sh new file +154 -0
154 lines not on this page · raw message
tools/testing/selftests/drivers/net/bonding/config +4 -0
@@ -9,3 +9,7 @@ CONFIG_NET_CLS_FLOWER=y CONFIG_NET_SCH_INGRESS=y CONFIG_NLMON=y CONFIG_VETH=y +CONFIG_INET_ESP=y +CONFIG_INET_ESP_OFFLOAD=y +CONFIG_XFRM_USER=m +CONFIG_NETDEVSIM=m
Signature · 1 line
2.46.0