Skip to contentSkip to conversation

bond: fix xfrm offload feature during init

PATCH In review Hangbin Liu 6 participants 29 messages Dec 11, 2024 – Feb 20, 2025

Subsystems (5)
lore
Share & tools
lore
#0 lore raw

cover bond: fix xfrm offload feature during init

The first patch fixes the xfrm offload feature during setup active-backup mode. The second patch add a ipsec offload testing.

Hangbin Liu (2):
bonding: fix xfrm offload feature setup on active-backup mode
selftests: bonding: add ipsec offload test

 drivers/net/bonding/bond_main.c               |   2 +-
 drivers/net/bonding/bond_netlink.c            |  17 +-
 include/net/bonding.h                         |   1 +
 .../selftests/drivers/net/bonding/Makefile    |   3 +-
 .../drivers/net/bonding/bond_ipsec_offload.sh | 155 ++++++++++++++++++
 .../selftests/drivers/net/bonding/config      |   4 +
 6 files changed, 173 insertions(+), 9 deletions(-)
 create mode 100755 tools/testing/selftests/drivers/net/bonding/bond_ipsec_offload.sh
Signature · 1 line
2.39.5 (Apple Git-154)
#1 lore raw

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

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 | 17 ++++++++++-------
 include/net/bonding.h              |  1 +
 3 files changed, 12 insertions(+), 8 deletions(-)
drivers/net/bonding/bond_main.c +1 -1
index 49dd4fe195e5..7daeab67e7b5 100644
@@ -4389,7 +4389,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 +10 -7
index 2a6a424806aa..7fe8c62366eb 100644
@@ -568,18 +568,21 @@ 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);
+		netif_carrier_on(bond_dev);
+		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.39.5 (Apple Git-154)
#6 lore raw

PATCH 2/2 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.

---
 .../selftests/drivers/net/bonding/Makefile    |   3 +-
 .../drivers/net/bonding/bond_ipsec_offload.sh | 155 ++++++++++++++++++
 .../selftests/drivers/net/bonding/config      |   4 +
 3 files changed, 161 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 03a089165d3f..c938475fdefa 100644
@@ -10,7 +10,8 @@ TEST_PROGS := \
 	mode-2-recovery-updelay.sh \
 	bond_options.sh \
 	bond-eth-type-change.sh \
-	bond_macvlan.sh
+	bond_macvlan.sh \
+	bond_ipsec_offload.sh
 
 TEST_FILES := \
 	lag_lib.sh \
tools/testing/selftests/drivers/net/bonding/bond_ipsec_offload.sh new file +155 -0

155 lines not on this page · raw message

tools/testing/selftests/drivers/net/bonding/config +4 -0
index 899d7fb6ea8e..91c581abe79c 100644
@@ -8,3 +8,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.39.5 (Apple Git-154)
#2 lore raw

Re: [PATCH net 1/2] bonding: fix xfrm offload feature setup on active-backup mode

In reply to Hangbin Liu
Quoted 53 lines · 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 | 17 ++++++++++-------
 include/net/bonding.h              |  1 +
 3 files changed, 12 insertions(+), 8 deletions(-)
drivers/net/bonding/bond_main.c +1 -1
index 49dd4fe195e5..7daeab67e7b5 100644
@@ -4389,7 +4389,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 +3 -4
index 2a6a424806aa..7fe8c62366eb 100644
@@ -568,18 +568,21 @@ 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) {

drivers/net/bonding/bond_netlink.c static int bond_newlink(struct net *src_net, struct net_device *bond_dev, · line 584

Quoted 10 lines
drivers/net/bonding/bond_netlink.c +6 -3
-		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);
+		netif_carrier_on(bond_dev);

The patch looks good, but I'm curious why the carrier on here?

Quoted 16 lines
drivers/net/bonding/bond_netlink.c +1 -0
+		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);
#3 lore raw

Re: [PATCH net 1/2] bonding: fix xfrm offload feature setup on active-backup mode

Quoted 36 lines · Nikolay Aleksandrov
Quoted 36 lines
drivers/net/bonding/bond_main.c +1 -1
index 49dd4fe195e5..7daeab67e7b5 100644
@@ -4389,7 +4389,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 +5 -5
index 2a6a424806aa..7fe8c62366eb 100644
@@ -568,18 +568,21 @@ 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);
 
Quoted 8 lines

drivers/net/bonding/bond_netlink.c static int bond_newlink(struct net *src_net, struct net_device *bond_dev, · line 584

Quoted 6 lines
drivers/net/bonding/bond_netlink.c +4 -2
-		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);
+		netif_carrier_on(bond_dev);

The patch looks good, but I'm curious why the carrier on here?

The current code set netif_carrier_off(bond_dev) after register_netdevice() success, So I make it on if register failed.

Thanks
hangbin

Quoted 5 lines
Quoted 5 lines
drivers/net/bonding/bond_netlink.c +1 -0
+		unregister_netdevice(bond_dev);
 	}
 
 	return err;
#4 lore raw

Re: [PATCH net 1/2] bonding: fix xfrm offload feature setup on active-backup mode

Quoted 46 lines · Hangbin Liu
Quoted 36 lines · Nikolay Aleksandrov
Quoted 36 lines
drivers/net/bonding/bond_main.c +1 -1
index 49dd4fe195e5..7daeab67e7b5 100644
@@ -4389,7 +4389,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 +5 -5
index 2a6a424806aa..7fe8c62366eb 100644
@@ -568,18 +568,21 @@ 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);
 
Quoted 8 lines

drivers/net/bonding/bond_netlink.c static int bond_newlink(struct net *src_net, struct net_device *bond_dev, · line 584

Quoted 6 lines
drivers/net/bonding/bond_netlink.c +4 -2
-		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);
+		netif_carrier_on(bond_dev);

The patch looks good, but I'm curious why the carrier on here?

Quoted 5 lines

The current code set netif_carrier_off(bond_dev) after register_netdevice() success, So I make it on if register failed.

Thanks
hangbin

I don't like adding code just for symmetry alone, I think you should drop it unless there is an actual reason to turn carrier on.

Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
drivers/net/bonding/bond_netlink.c +1 -0
+		unregister_netdevice(bond_dev);
 	}
 
 	return err;
maintainer
#7 lore raw
In reply to Hangbin Liu
Quoted 2 lines · On Wed, 11 Dec 2024 07:11:25 +0000 Hangbin Liu wrote:

The first patch fixes the xfrm offload feature during setup active-backup mode. The second patch add a ipsec offload testing.

Looks like the test is too good, is there a fix pending somewhere for the BUG below? We can't merge the test before that:

https://netdev-3.bots.linux.dev/vmksft-bonding-dbg/results/900082/11-bond-ipsec-offload-sh/stderr

[  859.672652][    C3] bond_xfrm_update_stats: eth0 doesn't support xdo_dev_state_update_stats
[  860.467189][ T8677] bond0: (slave eth0): link status definitely down, disabling slave
[  860.467664][ T8677] bond0: (slave eth1): making interface the new active one
[  860.831042][ T9677] bond_xfrm_update_stats: eth1 doesn't support xdo_dev_state_update_stats
[  862.195271][ T9683] BUG: sleeping function called from invalid context at kernel/locking/mutex.c:562
[  862.195880][ T9683] in_atomic(): 1, irqs_disabled(): 0, non_block: 0, pid: 9683, name: ip
[  862.196189][ T9683] preempt_count: 201, expected: 0
[  862.196396][ T9683] RCU nest depth: 0, expected: 0
[  862.196591][ T9683] 2 locks held by ip/9683:
[  862.196818][ T9683]  #0: ffff88800a829558 (&net->xfrm.xfrm_cfg_mutex){+.+.}-{4:4}, at: xfrm_netlink_rcv+0x65/0x90 [xfrm_user]
[  862.197264][ T9683]  #1: ffff88800f460548 (&x->lock){+.-.}-{3:3}, at: xfrm_state_flush+0x1b3/0x3a0
[  862.197629][ T9683] CPU: 3 UID: 0 PID: 9683 Comm: ip Not tainted 6.13.0-rc1-virtme #1
[  862.197967][ T9683] Hardware name: Bochs Bochs, BIOS Bochs 01/01/2011
[  862.198204][ T9683] Call Trace:
[  862.198352][ T9683]  <TASK>
[  862.198458][ T9683]  dump_stack_lvl+0xb0/0xd0
[  862.198659][ T9683]  __might_resched+0x2f8/0x530
[  862.198852][ T9683]  ? kfree+0x2d/0x330
[  862.199005][ T9683]  __mutex_lock+0xd9/0xbc0
[  862.199202][ T9683]  ? ref_tracker_free+0x35e/0x910
[  862.199401][ T9683]  ? bond_ipsec_del_sa+0x2c1/0x790
[  862.199937][ T9683]  ? find_held_lock+0x2c/0x110
[  862.200133][ T9683]  ? __pfx___mutex_lock+0x10/0x10
[  862.200329][ T9683]  ? bond_ipsec_del_sa+0x280/0x790
[  862.200519][ T9683]  ? xfrm_dev_state_delete+0x97/0x170
[  862.200711][ T9683]  ? __xfrm_state_delete+0x681/0x8e0
[  862.200907][ T9683]  ? xfrm_user_rcv_msg+0x4f8/0x920 [xfrm_user]
[  862.201151][ T9683]  ? netlink_rcv_skb+0x130/0x360
[  862.201347][ T9683]  ? xfrm_netlink_rcv+0x74/0x90 [xfrm_user]
[  862.201587][ T9683]  ? netlink_unicast+0x44b/0x710
[  862.201780][ T9683]  ? netlink_sendmsg+0x723/0xbe0
[  862.201973][ T9683]  ? ____sys_sendmsg+0x7ac/0xa10
[  862.202164][ T9683]  ? ___sys_sendmsg+0xee/0x170
[  862.202355][ T9683]  ? __sys_sendmsg+0x109/0x1a0
[  862.202546][ T9683]  ? do_syscall_64+0xc1/0x1d0
[  862.202738][ T9683]  ? entry_SYSCALL_64_after_hwframe+0x77/0x7f
[  862.202986][ T9683]  ? __pfx_nsim_ipsec_del_sa+0x10/0x10 [netdevsim]
[  862.203251][ T9683]  ? bond_ipsec_del_sa+0x2c1/0x790
[  862.203457][ T9683]  bond_ipsec_del_sa+0x2c1/0x790
[  862.203648][ T9683]  ? __pfx_lock_acquire.part.0+0x10/0x10
[  862.203845][ T9683]  ? __pfx_bond_ipsec_del_sa+0x10/0x10
[  862.204034][ T9683]  ? do_raw_spin_lock+0x131/0x270
[  862.204225][ T9683]  ? __pfx_do_raw_spin_lock+0x10/0x10
[  862.204468][ T9683]  xfrm_dev_state_delete+0x97/0x170
[  862.204665][ T9683]  __xfrm_state_delete+0x681/0x8e0
[  862.204858][ T9683]  xfrm_state_flush+0x1bb/0x3a0
[  862.205057][ T9683]  xfrm_flush_sa+0xf0/0x270 [xfrm_user]
[  862.205290][ T9683]  ? __pfx_xfrm_flush_sa+0x10/0x10 [xfrm_user]
[  862.205537][ T9683]  ? __nla_validate_parse+0x48/0x3d0
[  862.205744][ T9683]  xfrm_user_rcv_msg+0x4f8/0x920 [xfrm_user]
[  862.205985][ T9683]  ? __pfx___lock_release+0x10/0x10
[  862.206174][ T9683]  ? __pfx_xfrm_user_rcv_msg+0x10/0x10 [xfrm_user]
[  862.206412][ T9683]  ? __pfx_validate_chain+0x10/0x10
[  862.206614][ T9683]  ? hlock_class+0x4e/0x130
[  862.206807][ T9683]  ? mark_lock+0x38/0x3e0
[  862.206986][ T9683]  ? __mutex_trylock_common+0xfa/0x260
[  862.207181][ T9683]  ? __pfx___mutex_trylock_common+0x10/0x10
[  862.207425][ T9683]  netlink_rcv_skb+0x130/0x360
#5 lore raw

Re: [PATCH net 1/2] bonding: fix xfrm offload feature setup on active-backup mode

In reply to Nikolay Aleksandrov
Quoted 30 lines · Nikolay Aleksandrov
Quoted 30 lines
Quoted 29 lines
Quoted 17 lines
drivers/net/bonding/bond_netlink.c +3 -4
@@ -568,18 +568,21 @@ 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) {

drivers/net/bonding/bond_netlink.c static int bond_newlink(struct net *src_net, struct net_device *bond_dev, · line 584

Quoted 10 lines
drivers/net/bonding/bond_netlink.c +6 -3
-		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);
+		netif_carrier_on(bond_dev);

The patch looks good, but I'm curious why the carrier on here?

Quoted 8 lines
Quoted 5 lines

The current code set netif_carrier_off(bond_dev) after register_netdevice() success, So I make it on if register failed.

Thanks
hangbin

I don't like adding code just for symmetry alone, I think you should drop it unless there is an actual reason to turn carrier on.

OK, I will drop it.

Thanks
Hangbin

#8 lore raw
In reply to Jakub Kicinski
Quoted 6 lines · Jakub Kicinski
Quoted 2 lines · On Wed, 11 Dec 2024 07:11:25 +0000 Hangbin Liu wrote:

The first patch fixes the xfrm offload feature during setup active-backup mode. The second patch add a ipsec offload testing.

Looks like the test is too good, is there a fix pending somewhere for the BUG below? We can't merge the test before that:

This should be a regression of 2aeeef906d5a ("bonding: change ipsec_lock from spin lock to mutex"). As in xfrm_state_delete we called spin_lock_bh(&x->lock) for the xfrm state delete.

But I'm not sure if it's proper to release the spin lock in bond code. This seems too specific.

drivers/net/bonding/bond_main.c +2 -0
index 7daeab67e7b5..69563bc958ca 100644
@@ -592,6 +592,7 @@ 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);
+	spin_unlock_bh(&xs->lock);
 	mutex_lock(&bond->ipsec_lock);
 	list_for_each_entry(ipsec, &bond->ipsec_list, list) {
 		if (ipsec->xs == xs) {
@@ -601,6 +602,7 @@ static void bond_ipsec_del_sa(struct xfrm_state *xs)
 		}
 	}
 	mutex_unlock(&bond->ipsec_lock);
+	spin_lock_bh(&xs->lock);
 }
 
 

What do you think?

Thanks
Hangbin

Quoted 61 lines

https://netdev-3.bots.linux.dev/vmksft-bonding-dbg/results/900082/11-bond-ipsec-offload-sh/stderr

[  859.672652][    C3] bond_xfrm_update_stats: eth0 doesn't support xdo_dev_state_update_stats
[  860.467189][ T8677] bond0: (slave eth0): link status definitely down, disabling slave
[  860.467664][ T8677] bond0: (slave eth1): making interface the new active one
[  860.831042][ T9677] bond_xfrm_update_stats: eth1 doesn't support xdo_dev_state_update_stats
[  862.195271][ T9683] BUG: sleeping function called from invalid context at kernel/locking/mutex.c:562
[  862.195880][ T9683] in_atomic(): 1, irqs_disabled(): 0, non_block: 0, pid: 9683, name: ip
[  862.196189][ T9683] preempt_count: 201, expected: 0
[  862.196396][ T9683] RCU nest depth: 0, expected: 0
[  862.196591][ T9683] 2 locks held by ip/9683:
[  862.196818][ T9683]  #0: ffff88800a829558 (&net->xfrm.xfrm_cfg_mutex){+.+.}-{4:4}, at: xfrm_netlink_rcv+0x65/0x90 [xfrm_user]
[  862.197264][ T9683]  #1: ffff88800f460548 (&x->lock){+.-.}-{3:3}, at: xfrm_state_flush+0x1b3/0x3a0
[  862.197629][ T9683] CPU: 3 UID: 0 PID: 9683 Comm: ip Not tainted 6.13.0-rc1-virtme #1
[  862.197967][ T9683] Hardware name: Bochs Bochs, BIOS Bochs 01/01/2011
[  862.198204][ T9683] Call Trace:
[  862.198352][ T9683]  <TASK>
[  862.198458][ T9683]  dump_stack_lvl+0xb0/0xd0
[  862.198659][ T9683]  __might_resched+0x2f8/0x530
[  862.198852][ T9683]  ? kfree+0x2d/0x330
[  862.199005][ T9683]  __mutex_lock+0xd9/0xbc0
[  862.199202][ T9683]  ? ref_tracker_free+0x35e/0x910
[  862.199401][ T9683]  ? bond_ipsec_del_sa+0x2c1/0x790
[  862.199937][ T9683]  ? find_held_lock+0x2c/0x110
[  862.200133][ T9683]  ? __pfx___mutex_lock+0x10/0x10
[  862.200329][ T9683]  ? bond_ipsec_del_sa+0x280/0x790
[  862.200519][ T9683]  ? xfrm_dev_state_delete+0x97/0x170
[  862.200711][ T9683]  ? __xfrm_state_delete+0x681/0x8e0
[  862.200907][ T9683]  ? xfrm_user_rcv_msg+0x4f8/0x920 [xfrm_user]
[  862.201151][ T9683]  ? netlink_rcv_skb+0x130/0x360
[  862.201347][ T9683]  ? xfrm_netlink_rcv+0x74/0x90 [xfrm_user]
[  862.201587][ T9683]  ? netlink_unicast+0x44b/0x710
[  862.201780][ T9683]  ? netlink_sendmsg+0x723/0xbe0
[  862.201973][ T9683]  ? ____sys_sendmsg+0x7ac/0xa10
[  862.202164][ T9683]  ? ___sys_sendmsg+0xee/0x170
[  862.202355][ T9683]  ? __sys_sendmsg+0x109/0x1a0
[  862.202546][ T9683]  ? do_syscall_64+0xc1/0x1d0
[  862.202738][ T9683]  ? entry_SYSCALL_64_after_hwframe+0x77/0x7f
[  862.202986][ T9683]  ? __pfx_nsim_ipsec_del_sa+0x10/0x10 [netdevsim]
[  862.203251][ T9683]  ? bond_ipsec_del_sa+0x2c1/0x790
[  862.203457][ T9683]  bond_ipsec_del_sa+0x2c1/0x790
[  862.203648][ T9683]  ? __pfx_lock_acquire.part.0+0x10/0x10
[  862.203845][ T9683]  ? __pfx_bond_ipsec_del_sa+0x10/0x10
[  862.204034][ T9683]  ? do_raw_spin_lock+0x131/0x270
[  862.204225][ T9683]  ? __pfx_do_raw_spin_lock+0x10/0x10
[  862.204468][ T9683]  xfrm_dev_state_delete+0x97/0x170
[  862.204665][ T9683]  __xfrm_state_delete+0x681/0x8e0
[  862.204858][ T9683]  xfrm_state_flush+0x1bb/0x3a0
[  862.205057][ T9683]  xfrm_flush_sa+0xf0/0x270 [xfrm_user]
[  862.205290][ T9683]  ? __pfx_xfrm_flush_sa+0x10/0x10 [xfrm_user]
[  862.205537][ T9683]  ? __nla_validate_parse+0x48/0x3d0
[  862.205744][ T9683]  xfrm_user_rcv_msg+0x4f8/0x920 [xfrm_user]
[  862.205985][ T9683]  ? __pfx___lock_release+0x10/0x10
[  862.206174][ T9683]  ? __pfx_xfrm_user_rcv_msg+0x10/0x10 [xfrm_user]
[  862.206412][ T9683]  ? __pfx_validate_chain+0x10/0x10
[  862.206614][ T9683]  ? hlock_class+0x4e/0x130
[  862.206807][ T9683]  ? mark_lock+0x38/0x3e0
[  862.206986][ T9683]  ? __mutex_trylock_common+0xfa/0x260
[  862.207181][ T9683]  ? __pfx___mutex_trylock_common+0x10/0x10
[  862.207425][ T9683]  netlink_rcv_skb+0x130/0x360
maintainer
#9 lore raw
Quoted 35 lines · On Fri, 13 Dec 2024 07:18:08 +0000 Hangbin Liu wrote:
Quoted 6 lines · Jakub Kicinski
Quoted 2 lines · On Wed, 11 Dec 2024 07:11:25 +0000 Hangbin Liu wrote:

The first patch fixes the xfrm offload feature during setup active-backup mode. The second patch add a ipsec offload testing.

Looks like the test is too good, is there a fix pending somewhere for the BUG below? We can't merge the test before that:

This should be a regression of 2aeeef906d5a ("bonding: change ipsec_lock from spin lock to mutex"). As in xfrm_state_delete we called spin_lock_bh(&x->lock) for the xfrm state delete.

But I'm not sure if it's proper to release the spin lock in bond code. This seems too specific.

drivers/net/bonding/bond_main.c +2 -0
index 7daeab67e7b5..69563bc958ca 100644
@@ -592,6 +592,7 @@ 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);
+	spin_unlock_bh(&xs->lock);
 	mutex_lock(&bond->ipsec_lock);
 	list_for_each_entry(ipsec, &bond->ipsec_list, list) {
 		if (ipsec->xs == xs) {
@@ -601,6 +602,7 @@ static void bond_ipsec_del_sa(struct xfrm_state *xs)
 		}
 	}
 	mutex_unlock(&bond->ipsec_lock);
+	spin_lock_bh(&xs->lock);
 }
 
 
Quoted 1 line

What do you think?

Re-locking doesn't look great, glancing at the code I don't see any obvious better workarounds. Easiest fix would be to don't let the drivers sleep in the callbacks and then we can go back to a spin lock. Maybe nvidia people have better ideas, I'm not familiar with this offload.

#10 lore raw
Quoted 36 lines · Jakub Kicinski
Quoted 35 lines · On Fri, 13 Dec 2024 07:18:08 +0000 Hangbin Liu wrote:
Quoted 6 lines · Jakub Kicinski
Quoted 2 lines · On Wed, 11 Dec 2024 07:11:25 +0000 Hangbin Liu wrote:

The first patch fixes the xfrm offload feature during setup active-backup mode. The second patch add a ipsec offload testing.

Looks like the test is too good, is there a fix pending somewhere for the BUG below? We can't merge the test before that:

This should be a regression of 2aeeef906d5a ("bonding: change ipsec_lock from spin lock to mutex"). As in xfrm_state_delete we called spin_lock_bh(&x->lock) for the xfrm state delete.

But I'm not sure if it's proper to release the spin lock in bond code. This seems too specific.

drivers/net/bonding/bond_main.c +2 -0
index 7daeab67e7b5..69563bc958ca 100644
@@ -592,6 +592,7 @@ 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);
+	spin_unlock_bh(&xs->lock);
 	mutex_lock(&bond->ipsec_lock);
 	list_for_each_entry(ipsec, &bond->ipsec_list, list) {
 		if (ipsec->xs == xs) {
@@ -601,6 +602,7 @@ static void bond_ipsec_del_sa(struct xfrm_state *xs)
 		}
 	}
 	mutex_unlock(&bond->ipsec_lock);
+	spin_lock_bh(&xs->lock);
 }
 
 
Quoted 7 lines
Quoted 1 line

What do you think?

Re-locking doesn't look great, glancing at the code I don't see any obvious better workarounds. Easiest fix would be to don't let the drivers sleep in the callbacks and then we can go back to a spin lock. Maybe nvidia people have better ideas, I'm not familiar with this offload.

I don't know how to disable bonding sleeping since we use mutex_lock now. Hi Jianbo, do you have any idea?

Thanks
Hangbin

#11 lore raw
Quoted 45 lines · Hangbin Liu
Quoted 36 lines · Jakub Kicinski
Quoted 35 lines · On Fri, 13 Dec 2024 07:18:08 +0000 Hangbin Liu wrote:
Quoted 6 lines · Jakub Kicinski
Quoted 2 lines · On Wed, 11 Dec 2024 07:11:25 +0000 Hangbin Liu wrote:

The first patch fixes the xfrm offload feature during setup active-backup mode. The second patch add a ipsec offload testing.

Looks like the test is too good, is there a fix pending somewhere for the BUG below? We can't merge the test before that:

This should be a regression of 2aeeef906d5a ("bonding: change ipsec_lock from spin lock to mutex"). As in xfrm_state_delete we called spin_lock_bh(&x->lock) for the xfrm state delete.

But I'm not sure if it's proper to release the spin lock in bond code. This seems too specific.

drivers/net/bonding/bond_main.c +2 -0
index 7daeab67e7b5..69563bc958ca 100644
@@ -592,6 +592,7 @@ 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);
+	spin_unlock_bh(&xs->lock);
  	mutex_lock(&bond->ipsec_lock);
  	list_for_each_entry(ipsec, &bond->ipsec_list, list) {
  		if (ipsec->xs == xs) {
@@ -601,6 +602,7 @@ static void bond_ipsec_del_sa(struct xfrm_state *xs)
  		}
  	}
  	mutex_unlock(&bond->ipsec_lock);
+	spin_lock_bh(&xs->lock);
  }
 
 
Quoted 7 lines
Quoted 1 line

What do you think?

Re-locking doesn't look great, glancing at the code I don't see any obvious better workarounds. Easiest fix would be to don't let the drivers sleep in the callbacks and then we can go back to a spin lock. Maybe nvidia people have better ideas, I'm not familiar with this offload.

Quoted 3 lines

I don't know how to disable bonding sleeping since we use mutex_lock now. Hi Jianbo, do you have any idea?

I think we should allow drivers to sleep in the callbacks. So, maybe it's better to move driver's xdo_dev_state_delete out of state's spin lock.

Thanks!
Jianbo

#12 lore raw
Quoted 6 lines · Jianbo Liu
Quoted 6 lines
Quoted 5 lines

Re-locking doesn't look great, glancing at the code I don't see any obvious better workarounds. Easiest fix would be to don't let the drivers sleep in the callbacks and then we can go back to a spin lock. Maybe nvidia people have better ideas, I'm not familiar with this offload.

Quoted 6 lines
Quoted 3 lines

I don't know how to disable bonding sleeping since we use mutex_lock now. Hi Jianbo, do you have any idea?

I think we should allow drivers to sleep in the callbacks. So, maybe it's better to move driver's xdo_dev_state_delete out of state's spin lock.

Thanks for the suggestion, let me have a try first.

Hangbin

#13 lore raw
In reply to Jianbo Liu
Quoted 6 lines · Jianbo Liu
Quoted 6 lines
Quoted 5 lines

Re-locking doesn't look great, glancing at the code I don't see any obvious better workarounds. Easiest fix would be to don't let the drivers sleep in the callbacks and then we can go back to a spin lock. Maybe nvidia people have better ideas, I'm not familiar with this offload.

Quoted 6 lines
Quoted 3 lines

I don't know how to disable bonding sleeping since we use mutex_lock now. Hi Jianbo, do you have any idea?

I think we should allow drivers to sleep in the callbacks. So, maybe it's better to move driver's xdo_dev_state_delete out of state's spin lock.

I just check the code, xfrm_dev_state_delete() and later dev->xfrmdev_ops->xdo_dev_state_delete(x) have too many xfrm_state x checks. Can we really move it out of spin lock from xfrm_state_delete()

Thanks
Hangbin

#14 lore raw
Quoted 11 lines · Hangbin Liu
Quoted 10 lines · Jianbo Liu
Quoted 9 lines
Quoted 5 lines

Re-locking doesn't look great, glancing at the code I don't see any obvious better workarounds. Easiest fix would be to don't let the drivers sleep in the callbacks and then we can go back to a spin lock. Maybe nvidia people have better ideas, I'm not familiar with this offload.

I don't know how to disable bonding sleeping since we use mutex_lock now. Hi Jianbo, do you have any idea?

Quoted 6 lines
Quoted 2 lines

I think we should allow drivers to sleep in the callbacks. So, maybe it's better to move driver's xdo_dev_state_delete out of state's spin lock.

I just check the code, xfrm_dev_state_delete() and later dev->xfrmdev_ops->xdo_dev_state_delete(x) have too many xfrm_state x checks. Can we really move it out of spin lock from xfrm_state_delete()

I tried to move the mutex lock code to a work queue, but found we need to check (ipsec->xs == xs) in bonding. So we still need xfrm_state x during bond ipsec gc.

So either we add a new lock for xfrm_state, or we need to unlock spin lock in bonding bond_ipsec_del_sa().

Cc IPsec experts to see if they have any comments.

Background: The xfrm_dev_state_delete() in xfrm_state_delete() is protected by spin lock. But the driver delete ops dev->xfrmdev_ops->xdo_dev_state_delete(x) may sleep, e.g. bond_ipsec_del_sa(). What we should deal with this issue?

Thanks
Hangbin

#15 lore raw
Quoted 15 lines · Hangbin Liu
Quoted 14 lines · Hangbin Liu
Quoted 12 lines · Jianbo Liu
Quoted 9 lines
Quoted 5 lines

Re-locking doesn't look great, glancing at the code I don't see any obvious better workarounds. Easiest fix would be to don't let the drivers sleep in the callbacks and then we can go back to a spin lock. Maybe nvidia people have better ideas, I'm not familiar with this offload.

I don't know how to disable bonding sleeping since we use mutex_lock now. Hi Jianbo, do you have any idea?

I think we should allow drivers to sleep in the callbacks. So, maybe it's better to move driver's xdo_dev_state_delete out of state's spin lock.

Quoted 6 lines
Quoted 3 lines

I just check the code, xfrm_dev_state_delete() and later dev->xfrmdev_ops->xdo_dev_state_delete(x) have too many xfrm_state x checks. Can we really move it out of spin lock from xfrm_state_delete()

I tried to move the mutex lock code to a work queue, but found we need to check (ipsec->xs == xs) in bonding. So we still need xfrm_state x during bond

Maybe I miss something, but why need to hold spin lock. You can keep xfrm state by its refcnt.

Quoted 13 lines

ipsec gc.

So either we add a new lock for xfrm_state, or we need to unlock spin lock in bonding bond_ipsec_del_sa().

Cc IPsec experts to see if they have any comments.

Background: The xfrm_dev_state_delete() in xfrm_state_delete() is protected by spin lock. But the driver delete ops dev->xfrmdev_ops->xdo_dev_state_delete(x) may sleep, e.g. bond_ipsec_del_sa(). What we should deal with this issue?

Thanks
Hangbin

#16 lore raw
Quoted 22 lines · Jianbo Liu
Quoted 19 lines · Hangbin Liu
Quoted 17 lines · Hangbin Liu
Quoted 6 lines · Jianbo Liu
Quoted 6 lines
Quoted 5 lines

Re-locking doesn't look great, glancing at the code I don't see any obvious better workarounds. Easiest fix would be to don't let the drivers sleep in the callbacks and then we can go back to a spin lock. Maybe nvidia people have better ideas, I'm not familiar with this offload.

Quoted 6 lines
Quoted 3 lines

I don't know how to disable bonding sleeping since we use mutex_lock now. Hi Jianbo, do you have any idea?

I think we should allow drivers to sleep in the callbacks. So, maybe it's better to move driver's xdo_dev_state_delete out of state's spin lock.

I just check the code, xfrm_dev_state_delete() and later dev->xfrmdev_ops->xdo_dev_state_delete(x) have too many xfrm_state x checks. Can we really move it out of spin lock from xfrm_state_delete()

Quoted 5 lines
Quoted 2 lines

I tried to move the mutex lock code to a work queue, but found we need to check (ipsec->xs == xs) in bonding. So we still need xfrm_state x during bond

Maybe I miss something, but why need to hold spin lock. You can keep xfrm state by its refcnt.

Do you mean move the xfrm_dev_state_delete() out of spin lock directly like:

net/xfrm/xfrm_state.c +13 -8
index 67ca7ac955a3..6881ddeb4360 100644
@@ -766,13 +766,6 @@ int __xfrm_state_delete(struct xfrm_state *x)
 		if (x->encap_sk)
 			sock_put(rcu_dereference_raw(x->encap_sk));
 
-		xfrm_dev_state_delete(x);
-
-		/* All xfrm_state objects are created by xfrm_state_alloc.
-		 * The xfrm_state_alloc call gives a reference, and that
-		 * is what we are dropping here.
-		 */
-		xfrm_state_put(x);
 		err = 0;
 	}
 
@@ -787,8 +780,20 @@ int xfrm_state_delete(struct xfrm_state *x)
 	spin_lock_bh(&x->lock);
 	err = __xfrm_state_delete(x);
 	spin_unlock_bh(&x->lock);
+	if (err)
+		return err;
 
-	return err;
+	if (x->km.state == XFRM_STATE_DEAD) {
+		xfrm_dev_state_delete(x);
+
+		/* All xfrm_state objects are created by xfrm_state_alloc.
+		 * The xfrm_state_alloc call gives a reference, and that
+		 * is what we are dropping here.
+		 */
+		xfrm_state_put(x);
+	}
+
+	return 0;
 }
 EXPORT_SYMBOL(xfrm_state_delete);
 

Then why we need the spin lock in xfrm_state_delete?

Hangbin

#17 lore raw
Quoted 23 lines · Hangbin Liu
Quoted 22 lines · Jianbo Liu
Quoted 19 lines · Hangbin Liu
Quoted 17 lines · Hangbin Liu
Quoted 6 lines · Jianbo Liu
Quoted 6 lines
Quoted 5 lines

Re-locking doesn't look great, glancing at the code I don't see any obvious better workarounds. Easiest fix would be to don't let the drivers sleep in the callbacks and then we can go back to a spin lock. Maybe nvidia people have better ideas, I'm not familiar with this offload.

Quoted 6 lines
Quoted 3 lines

I don't know how to disable bonding sleeping since we use mutex_lock now. Hi Jianbo, do you have any idea?

I think we should allow drivers to sleep in the callbacks. So, maybe it's better to move driver's xdo_dev_state_delete out of state's spin lock.

I just check the code, xfrm_dev_state_delete() and later dev->xfrmdev_ops->xdo_dev_state_delete(x) have too many xfrm_state x checks. Can we really move it out of spin lock from xfrm_state_delete()

Quoted 8 lines
Quoted 5 lines
Quoted 2 lines

I tried to move the mutex lock code to a work queue, but found we need to check (ipsec->xs == xs) in bonding. So we still need xfrm_state x during bond

Maybe I miss something, but why need to hold spin lock. You can keep xfrm state by its refcnt.

Do you mean move the xfrm_dev_state_delete() out of spin lock directly like:

Yes. Not feasible?

Quoted 41 lines
net/xfrm/xfrm_state.c +13 -8
index 67ca7ac955a3..6881ddeb4360 100644
@@ -766,13 +766,6 @@ int __xfrm_state_delete(struct xfrm_state *x)
  		if (x->encap_sk)
  			sock_put(rcu_dereference_raw(x->encap_sk));
 
-		xfrm_dev_state_delete(x);
-
-		/* All xfrm_state objects are created by xfrm_state_alloc.
-		 * The xfrm_state_alloc call gives a reference, and that
-		 * is what we are dropping here.
-		 */
-		xfrm_state_put(x);
  		err = 0;
  	}
 
@@ -787,8 +780,20 @@ int xfrm_state_delete(struct xfrm_state *x)
  	spin_lock_bh(&x->lock);
  	err = __xfrm_state_delete(x);
  	spin_unlock_bh(&x->lock);
+	if (err)
+		return err;
 
-	return err;
+	if (x->km.state == XFRM_STATE_DEAD) {
+		xfrm_dev_state_delete(x);
+
+		/* All xfrm_state objects are created by xfrm_state_alloc.
+		 * The xfrm_state_alloc call gives a reference, and that
+		 * is what we are dropping here.
+		 */
+		xfrm_state_put(x);
+	}
+
+	return 0;
  }
  EXPORT_SYMBOL(xfrm_state_delete);
 
Quoted 2 lines

Then why we need the spin lock in xfrm_state_delete?

No, we don't need. But I am trying to understand what you said in your last email about adding a new lock, or unlocking spin lock in bond_ipsec_del_sa(). Anything I missed?

Thanks!
Jianbo

#18 lore raw
Quoted 74 lines · Jianbo Liu
Quoted 23 lines · Hangbin Liu
Quoted 22 lines · Jianbo Liu
Quoted 19 lines · Hangbin Liu
Quoted 17 lines · Hangbin Liu
Quoted 6 lines · Jianbo Liu
Quoted 6 lines
Quoted 5 lines

Re-locking doesn't look great, glancing at the code I don't see any obvious better workarounds. Easiest fix would be to don't let the drivers sleep in the callbacks and then we can go back to a spin lock. Maybe nvidia people have better ideas, I'm not familiar with this offload.

Quoted 6 lines
Quoted 3 lines

I don't know how to disable bonding sleeping since we use mutex_lock now. Hi Jianbo, do you have any idea?

I think we should allow drivers to sleep in the callbacks. So, maybe it's better to move driver's xdo_dev_state_delete out of state's spin lock.

I just check the code, xfrm_dev_state_delete() and later dev->xfrmdev_ops->xdo_dev_state_delete(x) have too many xfrm_state x checks. Can we really move it out of spin lock from xfrm_state_delete()

Quoted 8 lines
Quoted 5 lines
Quoted 2 lines

I tried to move the mutex lock code to a work queue, but found we need to check (ipsec->xs == xs) in bonding. So we still need xfrm_state x during bond

Maybe I miss something, but why need to hold spin lock. You can keep xfrm state by its refcnt.

Do you mean move the xfrm_dev_state_delete() out of spin lock directly like:

Yes. Not feasible?

Quoted 37 lines
net/xfrm/xfrm_state.c +13 -8
index 67ca7ac955a3..6881ddeb4360 100644
@@ -766,13 +766,6 @@ int __xfrm_state_delete(struct xfrm_state *x)
  		if (x->encap_sk)
  			sock_put(rcu_dereference_raw(x->encap_sk));
-		xfrm_dev_state_delete(x);
-
-		/* All xfrm_state objects are created by xfrm_state_alloc.
-		 * The xfrm_state_alloc call gives a reference, and that
-		 * is what we are dropping here.
-		 */
-		xfrm_state_put(x);
  		err = 0;
  	}
@@ -787,8 +780,20 @@ int xfrm_state_delete(struct xfrm_state *x)
  	spin_lock_bh(&x->lock);
  	err = __xfrm_state_delete(x);
  	spin_unlock_bh(&x->lock);
+	if (err)
+		return err;
-	return err;
+	if (x->km.state == XFRM_STATE_DEAD) {
+		xfrm_dev_state_delete(x);
+
+		/* All xfrm_state objects are created by xfrm_state_alloc.
+		 * The xfrm_state_alloc call gives a reference, and that
+		 * is what we are dropping here.
+		 */
+		xfrm_state_put(x);
+	}
+
+	return 0;
  }
  EXPORT_SYMBOL(xfrm_state_delete);
 
Quoted 5 lines
Quoted 2 lines

Then why we need the spin lock in xfrm_state_delete?

No, we don't need. But I am trying to understand what you said in your last email about adding a new lock, or unlocking spin lock in

I *thought* we need the spin lock in xfrm_state_delete(). So to protect xfrm_state, we need a new lock. Although it looks redundant. e.g.

int xfrm_state_delete(struct xfrm_state *x)

{
        int err;

        spin_lock_bh(&x->lock);
        err = __xfrm_state_delete(x);
        spin_unlock_bh(&x->lock);
        if (err)
                return err;

	another_lock(&x->other_lock)
        if (x->km.state == XFRM_STATE_DEAD) {
                xfrm_dev_state_delete(x);
                xfrm_state_put(x);
        }
	another_unlock(&x->other_lock)

        return 0;
}
Quoted 1 line

bond_ipsec_del_sa(). Anything I missed?

The unlock spin lock in bond_ipsec_del_sa looks like
https://lore.kernel.org/netdev/Z1vfsAyuxcohT7th@fedora/

Thanks
Hangbin

#19 lore raw
Quoted 75 lines · Hangbin Liu
Quoted 74 lines · Jianbo Liu
Quoted 23 lines · Hangbin Liu
Quoted 22 lines · Jianbo Liu
Quoted 19 lines · Hangbin Liu
Quoted 17 lines · Hangbin Liu
Quoted 6 lines · Jianbo Liu
Quoted 6 lines
Quoted 5 lines

Re-locking doesn't look great, glancing at the code I don't see any obvious better workarounds. Easiest fix would be to don't let the drivers sleep in the callbacks and then we can go back to a spin lock. Maybe nvidia people have better ideas, I'm not familiar with this offload.

Quoted 6 lines
Quoted 3 lines

I don't know how to disable bonding sleeping since we use mutex_lock now. Hi Jianbo, do you have any idea?

I think we should allow drivers to sleep in the callbacks. So, maybe it's better to move driver's xdo_dev_state_delete out of state's spin lock.

I just check the code, xfrm_dev_state_delete() and later dev->xfrmdev_ops->xdo_dev_state_delete(x) have too many xfrm_state x checks. Can we really move it out of spin lock from xfrm_state_delete()

Quoted 8 lines
Quoted 5 lines
Quoted 2 lines

I tried to move the mutex lock code to a work queue, but found we need to check (ipsec->xs == xs) in bonding. So we still need xfrm_state x during bond

Maybe I miss something, but why need to hold spin lock. You can keep xfrm state by its refcnt.

Do you mean move the xfrm_dev_state_delete() out of spin lock directly like:

Yes. Not feasible?

Quoted 37 lines
net/xfrm/xfrm_state.c +13 -8
index 67ca7ac955a3..6881ddeb4360 100644
@@ -766,13 +766,6 @@ int __xfrm_state_delete(struct xfrm_state *x)
   		if (x->encap_sk)
   			sock_put(rcu_dereference_raw(x->encap_sk));
-		xfrm_dev_state_delete(x);
-
-		/* All xfrm_state objects are created by xfrm_state_alloc.
-		 * The xfrm_state_alloc call gives a reference, and that
-		 * is what we are dropping here.
-		 */
-		xfrm_state_put(x);
   		err = 0;
   	}
@@ -787,8 +780,20 @@ int xfrm_state_delete(struct xfrm_state *x)
   	spin_lock_bh(&x->lock);
   	err = __xfrm_state_delete(x);
   	spin_unlock_bh(&x->lock);
+	if (err)
+		return err;
-	return err;
+	if (x->km.state == XFRM_STATE_DEAD) {
+		xfrm_dev_state_delete(x);
+
+		/* All xfrm_state objects are created by xfrm_state_alloc.
+		 * The xfrm_state_alloc call gives a reference, and that
+		 * is what we are dropping here.
+		 */
+		xfrm_state_put(x);
+	}
+
+	return 0;
   }
   EXPORT_SYMBOL(xfrm_state_delete);
 
Quoted 7 lines
Quoted 5 lines
Quoted 2 lines

Then why we need the spin lock in xfrm_state_delete?

No, we don't need. But I am trying to understand what you said in your last email about adding a new lock, or unlocking spin lock in

I *thought* we need the spin lock in xfrm_state_delete(). So to protect xfrm_state,

But not need in bond_ipsec_del_sa() because the state still hold by xfrm_state_hold(), right?

Quoted 28 lines

we need a new lock. Although it looks redundant. e.g.

int xfrm_state_delete(struct xfrm_state *x)

{
         int err;

         spin_lock_bh(&x->lock);
         err = __xfrm_state_delete(x);
         spin_unlock_bh(&x->lock);
         if (err)
                 return err;

	another_lock(&x->other_lock)
         if (x->km.state == XFRM_STATE_DEAD) {
                 xfrm_dev_state_delete(x);
                 xfrm_state_put(x);
         }
	another_unlock(&x->other_lock)

         return 0;
}
Quoted 1 line

bond_ipsec_del_sa(). Anything I missed?

The unlock spin lock in bond_ipsec_del_sa looks like
https://lore.kernel.org/netdev/Z1vfsAyuxcohT7th@fedora/

Thanks
Hangbin

#20 lore raw
Quoted 7 lines · Jianbo Liu
Quoted 4 lines
Quoted 2 lines

No, we don't need. But I am trying to understand what you said in your last email about adding a new lock, or unlocking spin lock in

I *thought* we need the spin lock in xfrm_state_delete(). So to protect xfrm_state,

But not need in bond_ipsec_del_sa() because the state still hold by xfrm_state_hold(), right?

Hmm, I'm not sure. If xfrm_state_hold() is safe. Why not just remove the spin lock in xfrm_state_delete(). This is more straightforward. e.g.

net/xfrm/xfrm_state.c +0 -2
index 67ca7ac955a3..150562abf513 100644
@@ -784,9 +784,7 @@ int xfrm_state_delete(struct xfrm_state *x)
 {
        int err;
 
-       spin_lock_bh(&x->lock);
        err = __xfrm_state_delete(x);
-       spin_unlock_bh(&x->lock);
 
        return err;
 }

We can even rename xfrm_state_delete() to xfrm_state_delete() directly.

Thanks
Hangbin

#21 lore raw
Quoted 4 lines · Hangbin Liu
Quoted 3 lines · Jianbo Liu
Quoted 3 lines
Quoted 2 lines

No, we don't need. But I am trying to understand what you said in your last email about adding a new lock, or unlocking spin lock in

Quoted 8 lines
Quoted 4 lines
Quoted 1 line

I *thought* we need the spin lock in xfrm_state_delete(). So to protect xfrm_state,

But not need in bond_ipsec_del_sa() because the state still hold by xfrm_state_hold(), right?

Hmm, I'm not sure. If xfrm_state_hold() is safe. Why not just remove the spin lock in xfrm_state_delete(). This is more straightforward. e.g.

We can't remove the spin lock in xfrm_state_delete(), but I think we can access the state while holding it, for example, checking (ipsec->xs == xs) as you mentioned before, because memory is not freed yet.

Quoted 19 lines
net/xfrm/xfrm_state.c +0 -2
index 67ca7ac955a3..150562abf513 100644
@@ -784,9 +784,7 @@ int xfrm_state_delete(struct xfrm_state *x)
  {
         int err;
 
-       spin_lock_bh(&x->lock);
         err = __xfrm_state_delete(x);
-       spin_unlock_bh(&x->lock);
 
         return err;
  }

We can even rename xfrm_state_delete() to xfrm_state_delete() directly.

Thanks
Hangbin

#22 lore raw
In reply to Hangbin Liu
Quoted 49 lines · Hangbin Liu
Quoted 11 lines
Quoted 11 lines
Quoted 10 lines
Quoted 6 lines
Quoted 3 lines

I don't know how to disable bonding sleeping since we use mutex_lock now. Hi Jianbo, do you have any idea?

I think we should allow drivers to sleep in the callbacks. So, maybe it's better to move driver's xdo_dev_state_delete out of state's spin lock.

I just check the code, xfrm_dev_state_delete() and later dev->xfrmdev_ops->xdo_dev_state_delete(x) have too many xfrm_state x checks. Can we really move it out of spin lock from xfrm_state_delete()

Quoted 5 lines
Quoted 2 lines

I tried to move the mutex lock code to a work queue, but found we need to check (ipsec->xs == xs) in bonding. So we still need xfrm_state x during bond

Maybe I miss something, but why need to hold spin lock. You can keep xfrm state by its refcnt.

Do you mean move the xfrm_dev_state_delete() out of spin lock directly like:

net/xfrm/xfrm_state.c +6 -8
index 67ca7ac955a3..6881ddeb4360 100644
@@ -766,13 +766,6 @@ int __xfrm_state_delete(struct xfrm_state *x)
 		if (x->encap_sk)
 			sock_put(rcu_dereference_raw(x->encap_sk));
 
-		xfrm_dev_state_delete(x);
-
-		/* All xfrm_state objects are created by xfrm_state_alloc.
-		 * The xfrm_state_alloc call gives a reference, and that
-		 * is what we are dropping here.
-		 */
-		xfrm_state_put(x);
 		err = 0;
 	}
 
@@ -787,8 +780,20 @@ int xfrm_state_delete(struct xfrm_state *x)
 	spin_lock_bh(&x->lock);
 	err = __xfrm_state_delete(x);
 	spin_unlock_bh(&x->lock);
+	if (err)
+		return err;
 
-	return err;
+	if (x->km.state == XFRM_STATE_DEAD) {
+		xfrm_dev_state_delete(x);
+
+		/* All xfrm_state objects are created by xfrm_state_alloc.

net/xfrm/xfrm_state.c int xfrm_state_delete(struct xfrm_state *x) · line 799

Quoted 10 lines
net/xfrm/xfrm_state.c +7 -0
+		 * The xfrm_state_alloc call gives a reference, and that
+		 * is what we are dropping here.
+		 */
+		xfrm_state_put(x);
+	}
+
+	return 0;
 }
 EXPORT_SYMBOL(xfrm_state_delete);
 

Hi Jianbo,

I talked with Sabrina and it looks we can't simply do this. Because both xfrm_add_sa_expire() and xfrm_timer_handler() calling __xfrm_state_delete() under spin lock. If we move the xfrm_dev_state_delete() out of __xfrm_state_delete(), all the places need to be handled correctly.

At the same time xfrm_timer_handler() calling xfrm_dev_state_update_stats before __xfrm_state_delete(). Should we also take care of it to make sure the state change and delete are called at the same time?

Hi Steffen, do you have any comments?

Thanks
Hangbin

maintainer
#23 lore raw
Quoted 68 lines · Hangbin Liu
Quoted 49 lines · Hangbin Liu
Quoted 11 lines
Quoted 11 lines
Quoted 10 lines
Quoted 6 lines
Quoted 3 lines

I don't know how to disable bonding sleeping since we use mutex_lock now. Hi Jianbo, do you have any idea?

I think we should allow drivers to sleep in the callbacks. So, maybe it's better to move driver's xdo_dev_state_delete out of state's spin lock.

I just check the code, xfrm_dev_state_delete() and later dev->xfrmdev_ops->xdo_dev_state_delete(x) have too many xfrm_state x checks. Can we really move it out of spin lock from xfrm_state_delete()

Quoted 5 lines
Quoted 2 lines

I tried to move the mutex lock code to a work queue, but found we need to check (ipsec->xs == xs) in bonding. So we still need xfrm_state x during bond

Maybe I miss something, but why need to hold spin lock. You can keep xfrm state by its refcnt.

Do you mean move the xfrm_dev_state_delete() out of spin lock directly like:

net/xfrm/xfrm_state.c +6 -8
index 67ca7ac955a3..6881ddeb4360 100644
@@ -766,13 +766,6 @@ int __xfrm_state_delete(struct xfrm_state *x)
 		if (x->encap_sk)
 			sock_put(rcu_dereference_raw(x->encap_sk));
 
-		xfrm_dev_state_delete(x);
-
-		/* All xfrm_state objects are created by xfrm_state_alloc.
-		 * The xfrm_state_alloc call gives a reference, and that
-		 * is what we are dropping here.
-		 */
-		xfrm_state_put(x);
 		err = 0;
 	}
 
@@ -787,8 +780,20 @@ int xfrm_state_delete(struct xfrm_state *x)
 	spin_lock_bh(&x->lock);
 	err = __xfrm_state_delete(x);
 	spin_unlock_bh(&x->lock);
+	if (err)
+		return err;
 
-	return err;
+	if (x->km.state == XFRM_STATE_DEAD) {
+		xfrm_dev_state_delete(x);
+
+		/* All xfrm_state objects are created by xfrm_state_alloc.

net/xfrm/xfrm_state.c int xfrm_state_delete(struct xfrm_state *x) · line 799

Quoted 10 lines
net/xfrm/xfrm_state.c +7 -0
+		 * The xfrm_state_alloc call gives a reference, and that
+		 * is what we are dropping here.
+		 */
+		xfrm_state_put(x);
+	}
+
+	return 0;
 }
 EXPORT_SYMBOL(xfrm_state_delete);
 

Hi Jianbo,

I talked with Sabrina and it looks we can't simply do this. Because both xfrm_add_sa_expire() and xfrm_timer_handler() calling __xfrm_state_delete() under spin lock. If we move the xfrm_dev_state_delete() out of __xfrm_state_delete(), all the places need to be handled correctly.

Quoted 5 lines

At the same time xfrm_timer_handler() calling xfrm_dev_state_update_stats before __xfrm_state_delete(). Should we also take care of it to make sure the state change and delete are called at the same time?

Hi Steffen, do you have any comments?

Can't you just fix this in bonding? xfrm_timer_handler() can't sleep anyway, even if you remove the spinlock, it is a timer function.

#24 lore raw
Quoted 17 lines · Steffen Klassert
Quoted 17 lines

Hi Jianbo,

I talked with Sabrina and it looks we can't simply do this. Because both
xfrm_add_sa_expire() and xfrm_timer_handler() calling __xfrm_state_delete() under
spin lock. If we move the xfrm_dev_state_delete() out of __xfrm_state_delete(),
all the places need to be handled correctly.

At the same time xfrm_timer_handler() calling xfrm_dev_state_update_stats before __xfrm_state_delete(). Should we also take care of it to make sure the state
change and delete are called at the same time?

Quoted 5 lines
Quoted 1 line

Hi Steffen, do you have any comments?

Can't you just fix this in bonding? xfrm_timer_handler() can't sleep anyway, even if you remove the spinlock, it is a timer function.

I am not sure this can be fixed in bonding given that the xdo_dev_state_delete op could, in the general case, sleep while talking to the hardware. I don't think it's reasonable to expect devices to offload xfrm while the kernel holds a spinlock.
Bonding just exposed this assumption mismatch because of the mutex that was added to replace a spinlock which exhibited the same problem we are talking about here.

Do the dev offload operations need to be synchronous? Couldn't __xfrm_state_delete instead schedule a wq to do the dev offload? I saw there's already an xfrm_state_gc_task that's invoked to call xfrm_dev_state_free, perhaps that could be used to do the delete as well?

Cosmin.

#25 lore raw
Quoted 32 lines · Cosmin Ratiu
Quoted 17 lines · Steffen Klassert
Quoted 17 lines

Hi Jianbo,

I talked with Sabrina and it looks we can't simply do this. Because both
xfrm_add_sa_expire() and xfrm_timer_handler() calling __xfrm_state_delete() under
spin lock. If we move the xfrm_dev_state_delete() out of __xfrm_state_delete(),
all the places need to be handled correctly.

At the same time xfrm_timer_handler() calling xfrm_dev_state_update_stats before __xfrm_state_delete(). Should we also take care of it to make sure the state
change and delete are called at the same time?

Quoted 5 lines
Quoted 1 line

Hi Steffen, do you have any comments?

Can't you just fix this in bonding? xfrm_timer_handler() can't sleep anyway, even if you remove the spinlock, it is a timer function.

I am not sure this can be fixed in bonding given that the xdo_dev_state_delete op could, in the general case, sleep while talking to the hardware. I don't think it's reasonable to expect devices to offload xfrm while the kernel holds a spinlock.
Bonding just exposed this assumption mismatch because of the mutex that was added to replace a spinlock which exhibited the same problem we are talking about here.

Quoted 5 lines

Do the dev offload operations need to be synchronous? Couldn't __xfrm_state_delete instead schedule a wq to do the dev offload? I saw there's already an xfrm_state_gc_task that's invoked to call xfrm_dev_state_free, perhaps that could be used to do the delete as well?

Yes, I have tried to move the bonding gc work in bond_ipsec_del_sa() to a wq in https://lore.kernel.org/netdev/Z33nEKg4PxwReUu_@fedora/. i.e. move the following part out of spin lock via wq.

        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);

But we can see there is an (ipsec->xs == xs). So we still need to make sure the xs is not released. Can we add a xs reference in bond_ipsec_del_sa() to achieve this?

Thanks
Hangbin

#26 lore raw
Quoted 35 lines · Hangbin Liu
Quoted 12 lines

I am not sure this can be fixed in bonding given that the xdo_dev_state_delete op could, in the general case, sleep while talking
to the hardware. I don't think it's reasonable to expect devices to offload xfrm while the kernel holds a spinlock.
Bonding just exposed this assumption mismatch because of the mutex that
was added to replace a spinlock which exhibited the same problem we are
talking about here.

Quoted 6 lines

Do the dev offload operations need to be synchronous? Couldn't __xfrm_state_delete instead schedule a wq to do the dev offload? I saw
there's already an xfrm_state_gc_task that's invoked to call xfrm_dev_state_free, perhaps that could be used to do the delete as well?

Yes, I have tried to move the bonding gc work in bond_ipsec_del_sa() to a wq
in https://lore.kernel.org/netdev/Z33nEKg4PxwReUu_@fedora/. i.e. move the
following part out of spin lock via wq.

        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);

Quoted 5 lines

But we can see there is an (ipsec->xs == xs). So we still need to make
sure the xs is not released. Can we add a xs reference in bond_ipsec_del_sa()
to achieve this?

Hello,

After staring at the issue a while longer, I am also converging on just moving that mutex part from bond_ipsec_del_sa out to a wq. I browsed through all driver implementations of .xdo_dev_state_delete() and found none that sleeps or allocates memory with GFP_KERNEL. So if we only fix bond_ipsec_del_sa, that would be enough to make it all work again.

So it should be perfectly safe to add a ref to xs in bond_ipsec_del_sa before firing up a wq to do the mutex lock + list traversal, before releasing the ref.
xfrm_state is already unlinked from everything by __xfrm_state_delete before xfrm_dev_state_delete is called and the xfrm_state_alloc reference is dropped by the end of xfrm_dev_state_delete, so the only thing keeping it alive would be the reference added in bond_ipsec_del_sa. When that is put after the list traversal, __xfrm_state_destroy gets called with sync == false, which passes on the baton to another wq to do the gc for xs.

This all sounds reasonable.
Will you chase this down or do you prefer me to send the proposed fix?

Cosmin.

#27 lore raw
Quoted 61 lines · Cosmin Ratiu
Quoted 35 lines · Hangbin Liu
Quoted 12 lines

I am not sure this can be fixed in bonding given that the xdo_dev_state_delete op could, in the general case, sleep while talking
to the hardware. I don't think it's reasonable to expect devices to offload xfrm while the kernel holds a spinlock.
Bonding just exposed this assumption mismatch because of the mutex that
was added to replace a spinlock which exhibited the same problem we are
talking about here.

Quoted 6 lines

Do the dev offload operations need to be synchronous? Couldn't __xfrm_state_delete instead schedule a wq to do the dev offload? I saw
there's already an xfrm_state_gc_task that's invoked to call xfrm_dev_state_free, perhaps that could be used to do the delete as well?

Yes, I have tried to move the bonding gc work in bond_ipsec_del_sa() to a wq
in https://lore.kernel.org/netdev/Z33nEKg4PxwReUu_@fedora/. i.e. move the
following part out of spin lock via wq.

        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);

Quoted 5 lines

But we can see there is an (ipsec->xs == xs). So we still need to make
sure the xs is not released. Can we add a xs reference in bond_ipsec_del_sa()
to achieve this?

Hello,

After staring at the issue a while longer, I am also converging on just moving that mutex part from bond_ipsec_del_sa out to a wq. I browsed through all driver implementations of .xdo_dev_state_delete() and found none that sleeps or allocates memory with GFP_KERNEL. So if we only fix bond_ipsec_del_sa, that would be enough to make it all work again.

So it should be perfectly safe to add a ref to xs in bond_ipsec_del_sa before firing up a wq to do the mutex lock + list traversal, before releasing the ref.
xfrm_state is already unlinked from everything by __xfrm_state_delete before xfrm_dev_state_delete is called and the xfrm_state_alloc reference is dropped by the end of xfrm_dev_state_delete, so the only thing keeping it alive would be the reference added in bond_ipsec_del_sa. When that is put after the list traversal, __xfrm_state_destroy gets called with sync == false, which passes on the baton to another wq to do the gc for xs.

Quoted 2 lines

This all sounds reasonable.
Will you chase this down or do you prefer me to send the proposed fix?

Thanks for the feedback and confirmation. Let me try it first. Hope unregistering bond doesn't affect the gc works.

Thanks
Hangbin

#28 lore raw
Quoted 3 lines · Hangbin Liu

Thanks for the feedback and confirmation. Let me try it first. Hope unregistering bond doesn't affect the gc works.

I think this should be handled naturally as part of the bond device tear down. A quick peek shows:
bond_uninit -> (for each slave) __bond_release_one -> bond_change_active_slave (_, NULL) -> bond_ipsec_del_sa_all deletes all matching xfrm_state entries from bond->ipsec_list, with the mutex held.

Presumably, a new step after all slaves are deleted should call drain_workqueue on the new workqueue to wait for any scheduled work to be done before finally destroying the workqueue.

Cosmin.