Skip to contentSkip to conversation

bond: fix xfrm offload issues

PATCH Superseded Hangbin Liu 4 participants 14 messages Mar 4, 2025 – Mar 7, 2025

Subsystems (4)

v4 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

v4 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.

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: move IPsec deletion to bond_ipsec_free_sa
bonding: fix xfrm offload feature setup on active-backup mode
selftests: bonding: add ipsec offload test

 drivers/net/bonding/bond_main.c               |  55 +++++--
 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, 208 insertions(+), 25 deletions(-)
 create mode 100755 tools/testing/selftests/drivers/net/bonding/bond_ipsec_offload.sh
Signature · 1 line
2.46.0
#1 lore raw

PATCH v4 1/3 bonding: move IPsec deletion to bond_ipsec_free_sa

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, delete the IPsec list in bond_ipsec_del_sa_all() when the XFRM state is DEAD to prevent xdo_dev_state_free() from being triggered again in bond_ipsec_free_sa().

For bond_ipsec_free_sa(), there are now three conditions:

1. if (!slave): When no active device exists.
2. if (!xs->xso.real_dev): When xdo_dev_state_add() fails.
3. if (xs->xso.real_dev != real_dev): When an xs has already been freed by bond_ipsec_del_sa_all() due to migration, and the active slave has changed to a new device. At the same time, the xs is marked as DEAD due to the XFRM entry is removed, triggering xfrm_state_gc_task() and bond_ipsec_free_sa().

In all three cases, xdo_dev_state_free() should not be called, only xs should be removed from bond->ipsec list.

At the same time, protect bond_ipsec_del_sa_all and bond_ipsec_add_sa_all with x->lock for each xs being processed. This prevents XFRM from concurrently initiating add/delete operations on the managed states.

Fixes 2aeeef906d5a ("bonding: change ipsec_lock from spin lock to mutex")
---
 drivers/net/bonding/bond_main.c | 53 +++++++++++++++++++++++----------
 1 file changed, 37 insertions(+), 16 deletions(-)
drivers/net/bonding/bond_main.c +37 -16
index e45bba240cbc..06b060d9b031 100644
@@ -537,15 +537,22 @@ static void bond_ipsec_add_sa_all(struct bonding *bond)
 	}
 
 	list_for_each_entry(ipsec, &bond->ipsec_list, list) {
+		spin_lock_bh(&ipsec->xs->lock);
+		/* Skip dead xfrm states, they'll be freed later. */
+		if (ipsec->xs->km.state == XFRM_STATE_DEAD)
+			goto next;
+
 		/* If new state is added before ipsec_lock acquired */
 		if (ipsec->xs->xso.real_dev == real_dev)
-			continue;
+			goto next;
 
 		ipsec->xs->xso.real_dev = real_dev;
 		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;
 		}
+next:
+		spin_unlock_bh(&ipsec->xs->lock);
 	}
 out:
 	mutex_unlock(&bond->ipsec_lock);
@@ -560,7 +567,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,15 +598,6 @@ 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)
@@ -617,8 +614,18 @@ static void bond_ipsec_del_sa_all(struct bonding *bond)
 
 	mutex_lock(&bond->ipsec_lock);
 	list_for_each_entry(ipsec, &bond->ipsec_list, list) {
+		spin_lock_bh(&ipsec->xs->lock);
 		if (!ipsec->xs->xso.real_dev)
-			continue;
+			goto next;
+
+		if (ipsec->xs->km.state == XFRM_STATE_DEAD) {
+			/* already dead no need to delete again */
+			if (real_dev->xfrmdev_ops->xdo_dev_state_free)
+				real_dev->xfrmdev_ops->xdo_dev_state_free(ipsec->xs);
+			list_del(&ipsec->list);
+			kfree(ipsec);
+			goto next;
+		}
 
 		if (!real_dev->xfrmdev_ops ||
 		    !real_dev->xfrmdev_ops->xdo_dev_state_delete ||
@@ -631,6 +638,8 @@ static void bond_ipsec_del_sa_all(struct bonding *bond)
 			if (real_dev->xfrmdev_ops->xdo_dev_state_free)
 				real_dev->xfrmdev_ops->xdo_dev_state_free(ipsec->xs);
 		}
+next:
+		spin_unlock_bh(&ipsec->xs->lock);
 	}
 	mutex_unlock(&bond->ipsec_lock);
 }
@@ -640,6 +649,7 @@ static void bond_ipsec_free_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;
 
@@ -659,11 +669,22 @@ 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);
+	mutex_lock(&bond->ipsec_lock);
+	list_for_each_entry(ipsec, &bond->ipsec_list, list) {
+		if (ipsec->xs == xs) {
+			/* do xdo_dev_state_free if real_dev matches,
+			 * otherwise only remove the list
+			 */
+			if (real_dev && real_dev->xfrmdev_ops &&
+			    real_dev->xfrmdev_ops->xdo_dev_state_free)
+				real_dev->xfrmdev_ops->xdo_dev_state_free(xs);
+			list_del(&ipsec->list);
+			kfree(ipsec);
+			break;
+		}
+	}
+	mutex_unlock(&bond->ipsec_lock);
 
-	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);
 }
Signature · 1 line
2.46.0
#2 lore raw

Re: [PATCHv4 net 1/3] bonding: move IPsec deletion to bond_ipsec_free_sa

Quoted 91 lines · Hangbin Liu

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, delete the IPsec list in bond_ipsec_del_sa_all() when the XFRM state is DEAD to prevent xdo_dev_state_free() from being triggered again in bond_ipsec_free_sa().

For bond_ipsec_free_sa(), there are now three conditions:

1. if (!slave): When no active device exists.
2. if (!xs->xso.real_dev): When xdo_dev_state_add() fails.
3. if (xs->xso.real_dev != real_dev): When an xs has already been freed by bond_ipsec_del_sa_all() due to migration, and the active slave has changed to a new device. At the same time, the xs is marked as DEAD due to the XFRM entry is removed, triggering xfrm_state_gc_task() and bond_ipsec_free_sa().

In all three cases, xdo_dev_state_free() should not be called, only xs should be removed from bond->ipsec list.

At the same time, protect bond_ipsec_del_sa_all and bond_ipsec_add_sa_all with x->lock for each xs being processed. This prevents XFRM from concurrently initiating add/delete operations on the managed states.

Fixes 2aeeef906d5a ("bonding: change ipsec_lock from spin lock to mutex")
---
 drivers/net/bonding/bond_main.c | 53 +++++++++++++++++++++++----------
 1 file changed, 37 insertions(+), 16 deletions(-)
drivers/net/bonding/bond_main.c +8 -11
index e45bba240cbc..06b060d9b031 100644
@@ -537,15 +537,22 @@ static void bond_ipsec_add_sa_all(struct bonding *bond)
 	}
 
 	list_for_each_entry(ipsec, &bond->ipsec_list, list) {
+		spin_lock_bh(&ipsec->xs->lock);
+		/* Skip dead xfrm states, they'll be freed later. */
+		if (ipsec->xs->km.state == XFRM_STATE_DEAD)
+			goto next;
+
 		/* If new state is added before ipsec_lock acquired */
 		if (ipsec->xs->xso.real_dev == real_dev)
-			continue;
+			goto next;
 
 		ipsec->xs->xso.real_dev = real_dev;
 		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;
 		}
+next:
+		spin_unlock_bh(&ipsec->xs->lock);
 	}
 out:
 	mutex_unlock(&bond->ipsec_lock);
@@ -560,7 +567,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,15 +598,6 @@ 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)

drivers/net/bonding/bond_main.c static void bond_ipsec_del_sa_all(struct bonding *bond) · line 616

Quoted 3 lines
drivers/net/bonding/bond_main.c +0 -0
@@ -617,8 +614,18 @@ static void bond_ipsec_del_sa_all(struct bonding *bond)
 
 	mutex_lock(&bond->ipsec_lock);
 	list_for_each_entry(ipsec, &bond->ipsec_list, list) {

Second time - you should use list_for_each_entry_safe if you're walking and deleting elements from the list.

drivers/net/bonding/bond_main.c static void bond_ipsec_del_sa_all(struct bonding *bond) · line 624

Quoted 9 lines
drivers/net/bonding/bond_main.c +7 -1
+		spin_lock_bh(&ipsec->xs->lock);
 		if (!ipsec->xs->xso.real_dev)
-			continue;
+			goto next;
+
+		if (ipsec->xs->km.state == XFRM_STATE_DEAD) {
+			/* already dead no need to delete again */
+			if (real_dev->xfrmdev_ops->xdo_dev_state_free)
+				real_dev->xfrmdev_ops->xdo_dev_state_free(ipsec->xs);

Have you checked if .xdo_dev_state_free can sleep? I see at least one that can: mlx5e_xfrm_free_state().

Quoted 51 lines
drivers/net/bonding/bond_main.c +22 -4
+			list_del(&ipsec->list);
+			kfree(ipsec);
+			goto next;
+		}
 
 		if (!real_dev->xfrmdev_ops ||
 		    !real_dev->xfrmdev_ops->xdo_dev_state_delete ||
@@ -631,6 +638,8 @@ static void bond_ipsec_del_sa_all(struct bonding *bond)
 			if (real_dev->xfrmdev_ops->xdo_dev_state_free)
 				real_dev->xfrmdev_ops->xdo_dev_state_free(ipsec->xs);
 		}
+next:
+		spin_unlock_bh(&ipsec->xs->lock);
 	}
 	mutex_unlock(&bond->ipsec_lock);
 }
@@ -640,6 +649,7 @@ static void bond_ipsec_free_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;
 
@@ -659,11 +669,22 @@ 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);
+	mutex_lock(&bond->ipsec_lock);
+	list_for_each_entry(ipsec, &bond->ipsec_list, list) {
+		if (ipsec->xs == xs) {
+			/* do xdo_dev_state_free if real_dev matches,
+			 * otherwise only remove the list
+			 */
+			if (real_dev && real_dev->xfrmdev_ops &&
+			    real_dev->xfrmdev_ops->xdo_dev_state_free)
+				real_dev->xfrmdev_ops->xdo_dev_state_free(xs);
+			list_del(&ipsec->list);
+			kfree(ipsec);
+			break;
+		}
+	}
+	mutex_unlock(&bond->ipsec_lock);
 
-	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);
 }
#3 lore raw

Re: [PATCHv4 net 1/3] bonding: move IPsec deletion to bond_ipsec_free_sa

Quoted 7 lines · Nikolay Aleksandrov

drivers/net/bonding/bond_main.c static void bond_ipsec_del_sa_all(struct bonding *bond) · line 616

Quoted 4 lines
drivers/net/bonding/bond_main.c +0 -0
@@ -617,8 +614,18 @@ static void bond_ipsec_del_sa_all(struct bonding *bond)
 
 	mutex_lock(&bond->ipsec_lock);
 	list_for_each_entry(ipsec, &bond->ipsec_list, list) {

Second time - you should use list_for_each_entry_safe if you're walking and deleting elements from the list.

Sorry, I missed this comment. I will update in next version.

Quoted 13 lines

drivers/net/bonding/bond_main.c static void bond_ipsec_del_sa_all(struct bonding *bond) · line 624

Quoted 10 lines
drivers/net/bonding/bond_main.c +7 -1
+		spin_lock_bh(&ipsec->xs->lock);
 		if (!ipsec->xs->xso.real_dev)
-			continue;
+			goto next;
+
+		if (ipsec->xs->km.state == XFRM_STATE_DEAD) {
+			/* already dead no need to delete again */
+			if (real_dev->xfrmdev_ops->xdo_dev_state_free)
+				real_dev->xfrmdev_ops->xdo_dev_state_free(ipsec->xs);

Have you checked if .xdo_dev_state_free can sleep? I see at least one that can: mlx5e_xfrm_free_state().

Hmm, This brings us back to the initial problem. We tried to avoid calling a spin lock in a sleep context (bond_ipsec_del_sa), but now the new code encounters this issue again.

With your reply, I also checked the xdo_dev_state_add() in bond_ipsec_add_sa_all(), which may also sleep, e.g. mlx5e_xfrm_add_state(),

If we unlock the spin lock, then the race came back again.

Any idea about this?

thanks
Hangbin

#4 lore raw

Re: [PATCHv4 net 1/3] bonding: move IPsec deletion to bond_ipsec_free_sa

Quoted 27 lines · Hangbin Liu
Quoted 9 lines · Nikolay Aleksandrov
Quoted 5 lines
(quoted diff) +0 -0
@@ -617,8 +614,18 @@ static void bond_ipsec_del_sa_all(struct
 bonding *bond)


  mutex_lock(&bond->ipsec_lock); list_for_each_entry(ipsec, &bond->ipsec_list, list) {

Second time - you should use list_for_each_entry_safe if you're walking and deleting
elements from the list.

Sorry, I missed this comment. I will update in next version.

Quoted 14 lines
Quoted 13 lines
(quoted diff) +1 -0
+		spin_lock_bh(&ipsec->xs->lock);

  if (!ipsec->xs->xso.real_dev)

(quoted diff) +4 -1
-			continue;
+			goto next;
+
+		if (ipsec->xs->km.state == XFRM_STATE_DEAD) {
+			/* already dead no need to delete again

*/

(quoted diff) +1 -0
+			if (real_dev->xfrmdev_ops-
Quoted 1 line

xdo_dev_state_free)

(quoted diff) +1 -0
+				real_dev->xfrmdev_ops-
Quoted 1 line

xdo_dev_state_free(ipsec->xs);

Quoted 8 lines
Quoted 2 lines

Have you checked if .xdo_dev_state_free can sleep? I see at least one that can: mlx5e_xfrm_free_state().

Hmm, This brings us back to the initial problem. We tried to avoid calling
a spin lock in a sleep context (bond_ipsec_del_sa), but now the new code
encounters this issue again.

The reason the mutex was added (instead of the spinlock used before) was exactly because the add and free offload operations could sleep.

Quoted 7 lines

With your reply, I also checked the xdo_dev_state_add() in bond_ipsec_add_sa_all(), which may also sleep, e.g. mlx5e_xfrm_add_state(),

If we unlock the spin lock, then the race came back again.

Any idea about this?

The race is between bond_ipsec_del_sa_all and bond_ipsec_del_sa (plus bond_ipsec_free_sa). The issue is that when bond_ipsec_del_sa_all releases x->lock, bond_ipsec_del_sa can immediately be called, followed by bond_ipsec_free_sa.
Maybe dropping x->lock after setting real_dev to NULL? I checked, real_dev is not used anywhere on the free calls, I think. I have another series refactoring things around real_dev, I hope to be able to send it soon.

Here's a sketch of this idea:

drivers/net/bonding/bond_main.c +0 -0
@@ -613,8 +613,11 @@ static void bond_ipsec_del_sa_all(struct bonding

*bond)

        mutex_lock(&bond->ipsec_lock);
        list_for_each_entry(ipsec, &bond->ipsec_list, list) {
-               if (!ipsec->xs->xso.real_dev)
+               spin_lock(&ipsec->x->lock);
+               if (!ipsec->xs->xso.real_dev) {
+                       spin_unlock(&ipsec->x->lock);
                        continue;

+ }

                if (!real_dev->xfrmdev_ops ||
                    !real_dev->xfrmdev_ops->xdo_dev_state_delete ||

@@ -622,12 +625,16 @@ 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-

Quoted 1 line

xdo_dev_state_delete(real_dev, ipsec->xs);

- if (real_dev->xfrmdev_ops->xdo_dev_state_free)
- real_dev->xfrmdev_ops-

Quoted 1 line

xdo_dev_state_free(ipsec->xs);

-                       ipsec->xs->xso.real_dev = NULL;
+                       spin_unlock(&ipsec->x->lock);
+                       continue;
                }

+
+ real_dev->xfrmdev_ops->xdo_dev_state_delete(real_dev,
ipsec->xs);
+ ipsec->xs->xso.real_dev = NULL;
+ /* Unlock before freeing device state, it could sleep.
*/
+ spin_unlock(&ipsec->x->lock);
+ if (real_dev->xfrmdev_ops->xdo_dev_state_free)
+ real_dev->xfrmdev_ops-

Quoted 1 line

xdo_dev_state_free(ipsec->xs);

Cosmin.

#5 lore raw

Re: [PATCHv4 net 1/3] bonding: move IPsec deletion to bond_ipsec_free_sa

Quoted 93 lines · Cosmin Ratiu
Quoted 27 lines · Hangbin Liu
Quoted 9 lines · Nikolay Aleksandrov
Quoted 5 lines
(quoted diff) +0 -0
@@ -617,8 +614,18 @@ static void bond_ipsec_del_sa_all(struct
 bonding *bond)


  mutex_lock(&bond->ipsec_lock); list_for_each_entry(ipsec, &bond->ipsec_list, list) {

Second time - you should use list_for_each_entry_safe if you're walking and deleting
elements from the list.

Sorry, I missed this comment. I will update in next version.

Quoted 14 lines
Quoted 13 lines
(quoted diff) +1 -0
+		spin_lock_bh(&ipsec->xs->lock);

  if (!ipsec->xs->xso.real_dev)

(quoted diff) +4 -1
-			continue;
+			goto next;
+
+		if (ipsec->xs->km.state == XFRM_STATE_DEAD) {
+			/* already dead no need to delete again

*/

(quoted diff) +1 -0
+			if (real_dev->xfrmdev_ops-
Quoted 1 line

xdo_dev_state_free)

(quoted diff) +1 -0
+				real_dev->xfrmdev_ops-
Quoted 1 line

xdo_dev_state_free(ipsec->xs);

Quoted 8 lines
Quoted 2 lines

Have you checked if .xdo_dev_state_free can sleep? I see at least one that can: mlx5e_xfrm_free_state().

Hmm, This brings us back to the initial problem. We tried to avoid calling
a spin lock in a sleep context (bond_ipsec_del_sa), but now the new code
encounters this issue again.

The reason the mutex was added (instead of the spinlock used before) was exactly because the add and free offload operations could sleep.

Quoted 7 lines

With your reply, I also checked the xdo_dev_state_add() in bond_ipsec_add_sa_all(), which may also sleep, e.g. mlx5e_xfrm_add_state(),

If we unlock the spin lock, then the race came back again.

Any idea about this?

The race is between bond_ipsec_del_sa_all and bond_ipsec_del_sa (plus bond_ipsec_free_sa). The issue is that when bond_ipsec_del_sa_all releases x->lock, bond_ipsec_del_sa can immediately be called, followed by bond_ipsec_free_sa.
Maybe dropping x->lock after setting real_dev to NULL? I checked, real_dev is not used anywhere on the free calls, I think. I have another series refactoring things around real_dev, I hope to be able to send it soon.

Here's a sketch of this idea:

drivers/net/bonding/bond_main.c +4 -1
@@ -613,8 +613,11 @@ static void bond_ipsec_del_sa_all(struct bonding
 *bond)
 
        mutex_lock(&bond->ipsec_lock);
        list_for_each_entry(ipsec, &bond->ipsec_list, list) {
-               if (!ipsec->xs->xso.real_dev)
+               spin_lock(&ipsec->x->lock);
+               if (!ipsec->xs->xso.real_dev) {
+                       spin_unlock(&ipsec->x->lock);
                        continue;
+               }
 
                if (!real_dev->xfrmdev_ops ||
                    !real_dev->xfrmdev_ops->xdo_dev_state_delete ||
drivers/net/bonding/bond_main.c +4 -5
@@ -622,12 +625,16 @@ 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(real_dev, ipsec->xs);
-                       if (real_dev->xfrmdev_ops->xdo_dev_state_free)
-                               real_dev->xfrmdev_ops-
 >xdo_dev_state_free(ipsec->xs);
-                       ipsec->xs->xso.real_dev = NULL;
+                       spin_unlock(&ipsec->x->lock);
+                       continue;
                }
+
+               real_dev->xfrmdev_ops->xdo_dev_state_delete(real_dev,
Quoted 2 lines

ipsec->xs);
+ ipsec->xs->xso.real_dev = NULL;

Set xs->xso.real_dev = NULL is a good idea. As we will break in bond_ipsec_del_sa()/bond_ipsec_free_sa() when there is no xs->xso.real_dev.

For bond_ipsec_add_sa_all(), I will move the xso.real_dev = real_dev after .xdo_dev_state_add() in case the following situation.

bond_ipsec_add_sa_all()
spin_unlock(&ipsec->x->lock);
ipsec->xs->xso.real_dev = real_dev;
__xfrm_state_delete x->state = DEAD
- bond_ipsec_del_sa()
- .xdo_dev_state_delete()
.xdo_dev_state_add()

Thanks
Hangbin

Quoted 8 lines

+ /* Unlock before freeing device state, it could sleep.
*/

drivers/net/bonding/bond_main.c +3 -0
+               spin_unlock(&ipsec->x->lock);
+               if (real_dev->xfrmdev_ops->xdo_dev_state_free)
+                       real_dev->xfrmdev_ops-
Quoted 1 line

xdo_dev_state_free(ipsec->xs);

Cosmin.

#6 lore raw

Re: [PATCHv4 net 1/3] bonding: move IPsec deletion to bond_ipsec_free_sa

Quoted 67 lines · Hangbin Liu
Quoted 57 lines

The reason the mutex was added (instead of the spinlock used before) was exactly because the add and free offload operations could sleep.

Quoted 7 lines

With your reply, I also checked the xdo_dev_state_add() in bond_ipsec_add_sa_all(), which may also sleep, e.g. mlx5e_xfrm_add_state(),

If we unlock the spin lock, then the race came back again.

Any idea about this?

The race is between bond_ipsec_del_sa_all and bond_ipsec_del_sa (plus bond_ipsec_free_sa). The issue is that when bond_ipsec_del_sa_all releases x->lock, bond_ipsec_del_sa can immediately be called, followed by bond_ipsec_free_sa.
Maybe dropping x->lock after setting real_dev to NULL? I checked, real_dev is not used anywhere on the free calls, I think. I have another series refactoring things around real_dev, I hope to be able to send it soon.

Here's a sketch of this idea:

drivers/net/bonding/bond_main.c +4 -1
@@ -613,8 +613,11 @@ static void bond_ipsec_del_sa_all(struct bonding
 *bond)
 
        mutex_lock(&bond->ipsec_lock);
        list_for_each_entry(ipsec, &bond->ipsec_list, list) {
-               if (!ipsec->xs->xso.real_dev)
+               spin_lock(&ipsec->x->lock);
+               if (!ipsec->xs->xso.real_dev) {
+                       spin_unlock(&ipsec->x->lock);
                        continue;
+               }
 
                if (!real_dev->xfrmdev_ops ||
                    !real_dev->xfrmdev_ops->xdo_dev_state_delete ||
drivers/net/bonding/bond_main.c +4 -5
@@ -622,12 +625,16 @@ 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(real_dev, ipsec->xs);
-                       if (real_dev->xfrmdev_ops->xdo_dev_state_free)
-                               real_dev->xfrmdev_ops-
 >xdo_dev_state_free(ipsec->xs);
-                       ipsec->xs->xso.real_dev = NULL;
+                       spin_unlock(&ipsec->x->lock);
+                       continue;
                }
+
+               real_dev->xfrmdev_ops->xdo_dev_state_delete(real_dev,
Quoted 2 lines

ipsec->xs);
+ ipsec->xs->xso.real_dev = NULL;

Set xs->xso.real_dev = NULL is a good idea. As we will break in bond_ipsec_del_sa()/bond_ipsec_free_sa() when there is no xs->xso.real_dev.

For bond_ipsec_add_sa_all(), I will move the xso.real_dev = real_dev after .xdo_dev_state_add() in case the following situation.

Quoted 7 lines

bond_ipsec_add_sa_all()
spin_unlock(&ipsec->x->lock);
ipsec->xs->xso.real_dev = real_dev;
__xfrm_state_delete x->state = DEAD
- bond_ipsec_del_sa()
- .xdo_dev_state_delete()
.xdo_dev_state_add()

Hmm, do we still need to the spin_lock in bond_ipsec_add_sa_all()? With xs->xso.real_dev = NULL after bond_ipsec_del_sa_all(), it looks there is no need the spin_lock in bond_ipsec_add_sa_all(). e.g.

drivers/net/bonding/bond_main.c +14 -2
index 04b677d0c45b..3ada51c63207 100644
@@ -537,15 +537,27 @@ static void bond_ipsec_add_sa_all(struct bonding *bond)
 	}
 
 	list_for_each_entry(ipsec, &bond->ipsec_list, list) {
+		spin_lock_bh(&ipsec->xs->lock);
+		/* Skip dead xfrm states, they'll be freed later. */
+		if (ipsec->xs->km.state == XFRM_STATE_DEAD) {
+			spin_unlock_bh(&ipsec->xs->lock);
+			continue;
+		}
+
 		/* If new state is added before ipsec_lock acquired */
-		if (ipsec->xs->xso.real_dev == real_dev)
+		if (ipsec->xs->xso.real_dev == real_dev) {
+			spin_unlock_bh(&ipsec->xs->lock);
 			continue;
+		}
 
-		ipsec->xs->xso.real_dev = real_dev;
 		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;
 		}
+		/* Set real_dev after .xdo_dev_state_add in case
+		 * __xfrm_state_delete() is called in parallel
+		 */
+		ipsec->xs->xso.real_dev = real_dev;
 	}
 

The spin_lock here seems useless now. What do you think?

Thanks
Hangbin

#7 lore raw

Re: [PATCHv4 net 1/3] bonding: move IPsec deletion to bond_ipsec_free_sa

Quoted 34 lines · Hangbin Liu
Quoted 7 lines

Set xs->xso.real_dev = NULL is a good idea. As we will break in bond_ipsec_del_sa()/bond_ipsec_free_sa() when there is no xs->xso.real_dev.

For bond_ipsec_add_sa_all(), I will move the xso.real_dev = real_dev after .xdo_dev_state_add() in case the following situation.

Hmm, do we still need to the spin_lock in bond_ipsec_add_sa_all()? With xs->xso.real_dev = NULL after bond_ipsec_del_sa_all(), it looks there is no need the spin_lock in bond_ipsec_add_sa_all(). e.g.

drivers/net/bonding/bond_main.c +10 -1
index 04b677d0c45b..3ada51c63207 100644
@@ -537,15 +537,27 @@ static void bond_ipsec_add_sa_all(struct bonding *bond)
 	}
 
 	list_for_each_entry(ipsec, &bond->ipsec_list, list) {
+		spin_lock_bh(&ipsec->xs->lock);
+		/* Skip dead xfrm states, they'll be freed later. */
+		if (ipsec->xs->km.state == XFRM_STATE_DEAD) {
+			spin_unlock_bh(&ipsec->xs->lock);
+			continue;
+		}
+
 		/* If new state is added before ipsec_lock acquired */
-		if (ipsec->xs->xso.real_dev == real_dev)
+		if (ipsec->xs->xso.real_dev == real_dev) {
+			spin_unlock_bh(&ipsec->xs->lock);
 			continue;
+		}
 

drivers/net/bonding/bond_main.c static void bond_ipsec_add_sa_all(struct bonding *bond) · line 561

Quoted 10 lines
drivers/net/bonding/bond_main.c +4 -1
-		ipsec->xs->xso.real_dev = real_dev;
 		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;
 		}
+		/* Set real_dev after .xdo_dev_state_add in case
+		 * __xfrm_state_delete() is called in parallel
+		 */
+		ipsec->xs->xso.real_dev = real_dev;
 	}

OK, please ignore this, the .xdo_dev_state_add() need xso.real_dev to be set first. Then I'm still wonder how to avoid the race before .xdo_dev_state_add() is called, e.g.

bond_ipsec_add_sa_all()
spin_lock_bh(&ipsec->xs->lock);
ipsec->xs->xso.real_dev = real_dev; spin_unlock(&ipsec->x->lock);
__xfrm_state_delete
- bond_ipsec_del_sa()
- .xdo_dev_state_delete()
- bond_ipsec_free_sa()
- .xdo_dev_state_free()
.xdo_dev_state_add()

Thanks
Hangbin

#8 lore raw

Re: [PATCHv4 net 1/3] bonding: move IPsec deletion to bond_ipsec_free_sa

In reply to Hangbin Liu
Quoted 3 lines · Hangbin Liu
Quoted 3 lines

For bond_ipsec_add_sa_all(), I will move the xso.real_dev = real_dev
after .xdo_dev_state_add() in case the following situation.

xso.real_dev needs to be initialized before the call to xdo_dev_state_add, since many of the implementations look in xso.real_dev to determine on which device to operate on. So the ordering should be:
- get the lock
- set xso.real_dev to real_dev
- release the lock
- call xdo_dev_state_add
- if it fails, reacquire the lock and set the device to NULL.

Unfortunately, this doesn't seem to protect against the scenario below, as after dropping the spinlock from bond_ipsec_add_sa_all, bond_ipsec_del_sa can freely call xdo_dev_state_delete() on real_dev before xdo_dev_state_add happens.

I don't know what to do in this case...

Quoted 8 lines
Quoted 8 lines

bond_ipsec_add_sa_all()
spin_unlock(&ipsec->x->lock);
ipsec->xs->xso.real_dev = real_dev;
                                __xfrm_state_delete x->state = DEAD
                                 - bond_ipsec_del_sa()
                                  - .xdo_dev_state_delete() .xdo_dev_state_add()

Cosmin.

#9 lore raw

Re: [PATCHv4 net 1/3] bonding: move IPsec deletion to bond_ipsec_free_sa

Quoted 15 lines · Cosmin Ratiu
Quoted 3 lines · Hangbin Liu
Quoted 3 lines

For bond_ipsec_add_sa_all(), I will move the xso.real_dev = real_dev
after .xdo_dev_state_add() in case the following situation.

xso.real_dev needs to be initialized before the call to xdo_dev_state_add, since many of the implementations look in xso.real_dev to determine on which device to operate on. So the ordering should be:
- get the lock
- set xso.real_dev to real_dev
- release the lock
- call xdo_dev_state_add
- if it fails, reacquire the lock and set the device to NULL.

Quoted 6 lines

Unfortunately, this doesn't seem to protect against the scenario below, as after dropping the spinlock from bond_ipsec_add_sa_all, bond_ipsec_del_sa can freely call xdo_dev_state_delete() on real_dev before xdo_dev_state_add happens.

I don't know what to do in this case...

Yes, me neither. How about add a note and leave it there until we have a solution?

Regards
Hangbin

Quoted 11 lines
Quoted 9 lines
Quoted 9 lines

bond_ipsec_add_sa_all()
spin_unlock(&ipsec->x->lock);
ipsec->xs->xso.real_dev = real_dev;
                                __xfrm_state_delete x->state = DEAD
                                 - bond_ipsec_del_sa()
                                  - .xdo_dev_state_delete() .xdo_dev_state_add()

Cosmin.

#10 lore raw

Re: [PATCHv4 net 1/3] bonding: move IPsec deletion to bond_ipsec_free_sa

In reply to Cosmin Ratiu
Quoted 40 lines · Cosmin Ratiu

+++ b/drivers/net/bonding/bond_main.c

(quoted diff) +4 -1
@@ -613,8 +613,11 @@ static void bond_ipsec_del_sa_all(struct bonding
 *bond)
 
        mutex_lock(&bond->ipsec_lock);
        list_for_each_entry(ipsec, &bond->ipsec_list, list) {
-               if (!ipsec->xs->xso.real_dev)
+               spin_lock(&ipsec->x->lock);
+               if (!ipsec->xs->xso.real_dev) {
+                       spin_unlock(&ipsec->x->lock);
                        continue;
+               }
 
                if (!real_dev->xfrmdev_ops ||
                    !real_dev->xfrmdev_ops->xdo_dev_state_delete ||
(quoted diff) +4 -5
@@ -622,12 +625,16 @@ 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(real_dev, ipsec->xs);
-                       if (real_dev->xfrmdev_ops->xdo_dev_state_free)
-                               real_dev->xfrmdev_ops-
 >xdo_dev_state_free(ipsec->xs);
-                       ipsec->xs->xso.real_dev = NULL;
+                       spin_unlock(&ipsec->x->lock);
+                       continue;
                }
+
+               real_dev->xfrmdev_ops->xdo_dev_state_delete(real_dev,

ipsec->xs);

(quoted diff) +2 -0
+               ipsec->xs->xso.real_dev = NULL;
+               /* Unlock before freeing device state, it could sleep.

*/

(quoted diff) +3 -0
+               spin_unlock(&ipsec->x->lock);
+               if (real_dev->xfrmdev_ops->xdo_dev_state_free)
+                       real_dev->xfrmdev_ops-
Quoted 1 line
Quoted 1 line

xdo_dev_state_free(ipsec->xs);

BTW, with setting real_dev = NULL here, I think

Quoted 9 lines

To fix that, these entries should be freed here and the WARN_ON in bond_ipsec_free_sa() should be converted to an if...goto out, so that bond_ipsec_free_sa() calls would hit one of these conditions:
1. "if (!slave)", when no active device exists.
2. "if (!xs->xso.real_dev)", when xdo_dev_state_add() failed.
3. "if (xs->xso.real_dev != real_dev)", when a DEAD xs was already freed by bond_ipsec_del_sa_all() migration to a new device. In all 3 cases, xdo_dev_state_free() shouldn't be called, only xs removed from the bond->ipsec list.

The if (xs->xso.real_dev != real_dev) should never happen again. As the real_dev = NULL, it will trigger 2 "if (!xs->xso.real_dev)" directly.

And in bond_ipsec_add_sa_all(), it will set ipsec->xs->xso.real_dev = real_dev, which the active slave already finished changing.

Thanks
Hangbin

#11 lore raw

PATCH v4 2/3 bonding: fix xfrm offload feature setup on active-backup mode

In reply to Hangbin Liu

The 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.

Fixes 007ab5345545 ("bonding: fix feature flag setting at init time")
---
 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
index 06b060d9b031..1fd2c0a5b13d 100644
@@ -4410,7 +4410,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
index 2a6a424806aa..ed16af6db557 100644
@@ -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
index 8bb5f016969f..e5e005cd2e17 100644
@@ -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
#12 lore raw

PATCH v4 3/3 selftests: bonding: add ipsec offload test

In reply to Hangbin Liu

This 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
index 2b10854e4b1e..d5a7de16d33a 100644
@@ -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
index dad4e5fda4db..054fb772846f 100644
@@ -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
Reviewed-by on v4 3/3
#13 lore raw

Re: [PATCHv4 net 3/3] selftests: bonding: add ipsec offload test

Quoted 11 lines · Hangbin Liu

This 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 ]