Skip to contentSkip to conversation

xfrm: misc fixes

PATCH In review Sabrina Dubroca 4 participants 12 messages Oct 16, 2025 – Oct 22, 2025

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

cover xfrm: misc fixes

These are mostly independent fixes for small issues for state creation/modification/deletion (except for the 2 migrate patches).

Sabrina Dubroca (6):
xfrm: drop SA reference in xfrm_state_update if dir doesn't match
xfrm: also call xfrm_state_delete_tunnel at destroy time for states
that were never added
xfrm: make state as DEAD before final put when migrate fails
xfrm: call xfrm_dev_state_delete when xfrm_state_migrate fails to add
the state
xfrm: set err and extack on failure to create pcpu SA
xfrm: check all hash buckets for leftover states during netns deletion

 net/xfrm/xfrm_state.c | 30 ++++++++++++++++++++++--------
 net/xfrm/xfrm_user.c  |  5 ++++-
 2 files changed, 26 insertions(+), 9 deletions(-)
Signature · 1 line
2.51.0
#1 lore raw

PATCH 1/6 xfrm: drop SA reference in xfrm_state_update if dir doesn't match

We're not updating x1, but we still need to put() it.

Fixes a4a87fa4e96c ("xfrm: Add Direction to the SA in or out")
---
 net/xfrm/xfrm_state.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
net/xfrm/xfrm_state.c +6 -2
index d213ca3653a8..e4736d1ebb44 100644
@@ -2191,14 +2191,18 @@ int xfrm_state_update(struct xfrm_state *x)
 	}
 
 	if (x1->km.state == XFRM_STATE_ACQ) {
-		if (x->dir && x1->dir != x->dir)
+		if (x->dir && x1->dir != x->dir) {
+			to_put = x1;
 			goto out;
+		}
 
 		__xfrm_state_insert(x);
 		x = NULL;
 	} else {
-		if (x1->dir != x->dir)
+		if (x1->dir != x->dir) {
+			to_put = x1;
 			goto out;
+		}
 	}
 	err = 0;
 
Signature · 1 line
2.51.0
#2 lore raw

PATCH 2/6 xfrm: also call xfrm_state_delete_tunnel at destroy time for states that were never added

In reply to Sabrina Dubroca

In commit b441cf3f8c4b ("xfrm: delete x->tunnel as we delete x"), I missed the case where state creation fails between full initialization (->init_state has been called) and being inserted on the lists.

In this situation, ->init_state has been called, so for IPcomp tunnels, the fallback tunnel has been created and added onto the lists, but the user state never gets added, because we fail before that. The user state doesn't go through __xfrm_state_delete, so we don't call xfrm_state_delete_tunnel for those states, and we end up leaking the FB tunnel.

There are several codepaths affected by this: the add/update paths, in both net/key and xfrm, and the migrate code (xfrm_migrate, xfrm_state_migrate). A "proper" rollback of the init_state work would probably be doable in the add/update code, but for migrate it gets more complicated as multiple states may be involved.

At some point, the new (not-inserted) state will be destroyed, so call xfrm_state_delete_tunnel during xfrm_state_gc_destroy. Most states will have their fallback tunnel cleaned up during __xfrm_state_delete, which solves the issue that b441cf3f8c4b (and other patches before it) aimed at. All states (including FB tunnels) will be removed from the lists once xfrm_state_fini has called flush_work(&xfrm_state_gc_work).

Reported-by [email protected]
Fixes b441cf3f8c4b ("xfrm: delete x->tunnel as we delete x")
---
 net/xfrm/xfrm_state.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
net/xfrm/xfrm_state.c +2 -1
index e4736d1ebb44..721ef0f409b5 100644
@@ -592,6 +592,7 @@ void xfrm_state_free(struct xfrm_state *x)
 }
 EXPORT_SYMBOL(xfrm_state_free);
 
+static void xfrm_state_delete_tunnel(struct xfrm_state *x);
 static void xfrm_state_gc_destroy(struct xfrm_state *x)
 {
 	if (x->mode_cbs && x->mode_cbs->destroy_state)
@@ -607,6 +608,7 @@ static void xfrm_state_gc_destroy(struct xfrm_state *x)
 	kfree(x->replay_esn);
 	kfree(x->preplay_esn);
 	xfrm_unset_type_offload(x);
+	xfrm_state_delete_tunnel(x);
 	if (x->type) {
 		x->type->destructor(x);
 		xfrm_put_type(x->type);
@@ -806,7 +808,6 @@ void __xfrm_state_destroy(struct xfrm_state *x)
 }
 EXPORT_SYMBOL(__xfrm_state_destroy);
 
-static void xfrm_state_delete_tunnel(struct xfrm_state *x);
 int __xfrm_state_delete(struct xfrm_state *x)
 {
 	struct net *net = xs_net(x);
Signature · 1 line
2.51.0
#3 lore raw

PATCH 3/6 xfrm: make state as DEAD before final put when migrate fails

In reply to Sabrina Dubroca

xfrm_state_migrate/xfrm_state_clone_and_setup create a new state, and call xfrm_state_put to destroy it in case of
failure. __xfrm_state_destroy expects the state to be in XFRM_STATE_DEAD, but we currently don't do that.

Reported-by [email protected]
Fixes 78347c8c6b2d ("xfrm: Fix xfrm_state_migrate leak")
---
 net/xfrm/xfrm_state.c | 2 ++
 1 file changed, 2 insertions(+)
net/xfrm/xfrm_state.c +2 -0
index 721ef0f409b5..1ab19ca007de 100644
@@ -2074,6 +2074,7 @@ static struct xfrm_state *xfrm_state_clone_and_setup(struct xfrm_state *orig,
 	return x;
 
  error:
+	x->km.state = XFRM_STATE_DEAD;
 	xfrm_state_put(x);
 out:
 	return NULL;
@@ -2163,6 +2164,7 @@ struct xfrm_state *xfrm_state_migrate(struct xfrm_state *x,
 
 	return xc;
 error:
+	xc->km.state = XFRM_STATE_DEAD;
 	xfrm_state_put(xc);
 	return NULL;
 }
Signature · 1 line
2.51.0
#4 lore raw

PATCH 4/6 xfrm: call xfrm_dev_state_delete when xfrm_state_migrate fails to add the state

In reply to Sabrina Dubroca

In case xfrm_state_migrate fails after calling xfrm_dev_state_add, we directly release the last reference and destroy the new state, without calling xfrm_dev_state_delete (this only happens in __xfrm_state_delete, which we're not calling on this path, since the state was never added).

Call xfrm_dev_state_delete on error when an offload configuration was provided.

Fixes ab244a394c7f ("xfrm: Migrate offload configuration")
---
 net/xfrm/xfrm_state.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
net/xfrm/xfrm_state.c +4 -1
index 1ab19ca007de..c3518d1498cd 100644
@@ -2159,10 +2159,13 @@ struct xfrm_state *xfrm_state_migrate(struct xfrm_state *x,
 		xfrm_state_insert(xc);
 	} else {
 		if (xfrm_state_add(xc) < 0)
-			goto error;
+			goto error_add;
 	}
 
 	return xc;
+error_add:
+	if (xuo)
+		xfrm_dev_state_delete(xc);
 error:
 	xc->km.state = XFRM_STATE_DEAD;
 	xfrm_state_put(xc);
Signature · 1 line
2.51.0
Reviewed-by on v1 4/6
#5 lore raw

Re: [PATCH ipsec 4/6] xfrm: call xfrm_dev_state_delete when xfrm_state_migrate fails to add the state

Quoted 6 lines · Sabrina Dubroca

In case xfrm_state_migrate fails after calling xfrm_dev_state_add, we directly release the last reference and destroy the new state, without calling xfrm_dev_state_delete (this only happens in __xfrm_state_delete, which we're not calling on this path, since the state was never added).

Quoted 9 lines

Call xfrm_dev_state_delete on error when an offload configuration was provided.

Fixes ab244a394c7f ("xfrm: Migrate offload configuration")
---
 net/xfrm/xfrm_state.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)

Thanks,

#6 lore raw

PATCH 5/6 xfrm: set err and extack on failure to create pcpu SA

In reply to Sabrina Dubroca

xfrm_state_construct can fail without setting an error if the requested pcpu_num value is too big. Set err and add an extack message to avoid confusing userspace.

Fixes 1ddf9916ac09 ("xfrm: Add support for per cpu xfrm state handling.")
---
 net/xfrm/xfrm_user.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
net/xfrm/xfrm_user.c +4 -1
index 010c9e6638c0..9d98cc9daa37 100644
@@ -947,8 +947,11 @@ static struct xfrm_state *xfrm_state_construct(struct net *net,
 
 	if (attrs[XFRMA_SA_PCPU]) {
 		x->pcpu_num = nla_get_u32(attrs[XFRMA_SA_PCPU]);
-		if (x->pcpu_num >= num_possible_cpus())
+		if (x->pcpu_num >= num_possible_cpus()) {
+			err = -ERANGE;
+			NL_SET_ERR_MSG(extack, "pCPU number too big");
 			goto error;
+		}
 	}
 
 	err = __xfrm_init_state(x, extack);
Signature · 1 line
2.51.0
#7 lore raw

PATCH 6/6 xfrm: check all hash buckets for leftover states during netns deletion

In reply to Sabrina Dubroca

The current hlist_empty checks only test the first bucket of each hashtable, ignoring any other bucket. They should be caught by the WARN_ON for state_all, but better to make all the checks accurate.

Fixes 73d189dce486 ("netns xfrm: per-netns xfrm_state_bydst hash")
Fixes d320bbb306f2 ("netns xfrm: per-netns xfrm_state_bysrc hash")
Fixes b754a4fd8f58 ("netns xfrm: per-netns xfrm_state_byspi hash")
Fixes fe9f1d8779cb ("xfrm: add state hashtable keyed by seq")
---
 net/xfrm/xfrm_state.c | 12 ++++++++----
 1 file changed, 8 insertions(+), 4 deletions(-)
net/xfrm/xfrm_state.c +8 -4
index c3518d1498cd..9e14e453b55c 100644
@@ -3308,6 +3308,7 @@ int __net_init xfrm_state_init(struct net *net)
 void xfrm_state_fini(struct net *net)
 {
 	unsigned int sz;
+	int i;
 
 	flush_work(&net->xfrm.state_hash_work);
 	xfrm_state_flush(net, 0, false);
@@ -3315,14 +3316,17 @@ void xfrm_state_fini(struct net *net)
 
 	WARN_ON(!list_empty(&net->xfrm.state_all));
 
+	for (i = 0; i <= net->xfrm.state_hmask; i++) {
+		WARN_ON(!hlist_empty(net->xfrm.state_byseq + i));
+		WARN_ON(!hlist_empty(net->xfrm.state_byspi + i));
+		WARN_ON(!hlist_empty(net->xfrm.state_bysrc + i));
+		WARN_ON(!hlist_empty(net->xfrm.state_bydst + i));
+	}
+
 	sz = (net->xfrm.state_hmask + 1) * sizeof(struct hlist_head);
-	WARN_ON(!hlist_empty(net->xfrm.state_byseq));
 	xfrm_hash_free(net->xfrm.state_byseq, sz);
-	WARN_ON(!hlist_empty(net->xfrm.state_byspi));
 	xfrm_hash_free(net->xfrm.state_byspi, sz);
-	WARN_ON(!hlist_empty(net->xfrm.state_bysrc));
 	xfrm_hash_free(net->xfrm.state_bysrc, sz);
-	WARN_ON(!hlist_empty(net->xfrm.state_bydst));
 	xfrm_hash_free(net->xfrm.state_bydst, sz);
 	free_percpu(net->xfrm.state_cache_input);
 }
Signature · 1 line
2.51.0
#8 lore raw

Re: [PATCH ipsec 6/6] xfrm: check all hash buckets for leftover states during netns deletion

kernel test robot noticed the following build warnings:

Hi Sabrina,

kernel test robot noticed the following build warnings:

[auto build test WARNING on klassert-ipsec-next/master]
[also build test WARNING on klassert-ipsec/master net/main net-next/main linus/master v6.18-rc1 next-20251016] [If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]

url:    https://github.com/intel-lab-lkp/linux/commits/Sabrina-Dubroca/xfrm-drop-SA-reference-in-xfrm_state_update-if-dir-doesn-t-match/20251016-184507
base:   https://git.kernel.org/pub/scm/linux/kernel/git/klassert/ipsec-next.git master
patch link:    https://lore.kernel.org/r/2a743a05bbad7ebdc36c2c86a5fcbb9e99071c7b.1760610268.git.sd%40queasysnail.net
patch subject: [PATCH ipsec 6/6] xfrm: check all hash buckets for leftover states during netns deletion
config: x86_64-randconfig-r123-20251017 (https://download.01.org/0day-ci/archive/20251017/[email protected]/config)
compiler: clang version 20.1.8 (https://github.com/llvm/llvm-project 87f0227cb60147a26a1eeb4fb06e3b505e9c7261)
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20251017/[email protected]/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/

sparse warnings: (new ones prefixed by >>)
net/xfrm/xfrm_state.c:1737:9: sparse: got struct hlist_head [noderef] __rcu *
net/xfrm/xfrm_state.c:1744:17: sparse: sparse: cast removes address space '__rcu' of expression net/xfrm/xfrm_state.c:1744:17: sparse: sparse: cast removes address space '__rcu' of expression net/xfrm/xfrm_state.c:1744:17: sparse: sparse: cast removes address space '__rcu' of expression net/xfrm/xfrm_state.c:1744:17: sparse: sparse: cast removes address space '__rcu' of expression net/xfrm/xfrm_state.c:1744:17: sparse: sparse: cast removes address space '__rcu' of expression net/xfrm/xfrm_state.c:1744:17: sparse: sparse: cast removes address space '__rcu' of expression net/xfrm/xfrm_state.c:1744:17: sparse: sparse: cast removes address space '__rcu' of expression net/xfrm/xfrm_state.c:1744:17: sparse: sparse: cast removes address space '__rcu' of expression net/xfrm/xfrm_state.c:1744:17: sparse: sparse: cast removes address space '__rcu' of expression net/xfrm/xfrm_state.c:1744:17: sparse: sparse: cast removes address space '__rcu' of expression
net/xfrm/xfrm_state.c:1744:17: sparse: sparse: incorrect type in argument 2 (different address spaces) @@ expected struct hlist_head *h @@ got struct hlist_head [noderef] __rcu * @@
net/xfrm/xfrm_state.c:1744:17: sparse: expected struct hlist_head *h
net/xfrm/xfrm_state.c:1744:17: sparse: got struct hlist_head [noderef] __rcu *
net/xfrm/xfrm_state.c:1751:17: sparse: sparse: cast removes address space '__rcu' of expression net/xfrm/xfrm_state.c:1751:17: sparse: sparse: cast removes address space '__rcu' of expression net/xfrm/xfrm_state.c:1751:17: sparse: sparse: cast removes address space '__rcu' of expression net/xfrm/xfrm_state.c:1751:17: sparse: sparse: cast removes address space '__rcu' of expression net/xfrm/xfrm_state.c:1751:17: sparse: sparse: cast removes address space '__rcu' of expression net/xfrm/xfrm_state.c:1751:17: sparse: sparse: cast removes address space '__rcu' of expression net/xfrm/xfrm_state.c:1751:17: sparse: sparse: cast removes address space '__rcu' of expression net/xfrm/xfrm_state.c:1751:17: sparse: sparse: cast removes address space '__rcu' of expression net/xfrm/xfrm_state.c:1751:17: sparse: sparse: cast removes address space '__rcu' of expression net/xfrm/xfrm_state.c:1751:17: sparse: sparse: cast removes address space '__rcu' of expression
net/xfrm/xfrm_state.c:1751:17: sparse: sparse: incorrect type in argument 2 (different address spaces) @@ expected struct hlist_head *h @@ got struct hlist_head [noderef] __rcu * @@
net/xfrm/xfrm_state.c:1751:17: sparse: expected struct hlist_head *h
net/xfrm/xfrm_state.c:1751:17: sparse: got struct hlist_head [noderef] __rcu *
net/xfrm/xfrm_state.c:1871:17: sparse: sparse: cast removes address space '__rcu' of expression net/xfrm/xfrm_state.c:1871:17: sparse: sparse: cast removes address space '__rcu' of expression net/xfrm/xfrm_state.c:1871:17: sparse: sparse: cast removes address space '__rcu' of expression net/xfrm/xfrm_state.c:1871:17: sparse: sparse: cast removes address space '__rcu' of expression net/xfrm/xfrm_state.c:1871:17: sparse: sparse: cast removes address space '__rcu' of expression net/xfrm/xfrm_state.c:1871:17: sparse: sparse: cast removes address space '__rcu' of expression net/xfrm/xfrm_state.c:1871:17: sparse: sparse: cast removes address space '__rcu' of expression net/xfrm/xfrm_state.c:1871:17: sparse: sparse: cast removes address space '__rcu' of expression net/xfrm/xfrm_state.c:1871:17: sparse: sparse: cast removes address space '__rcu' of expression net/xfrm/xfrm_state.c:1871:17: sparse: sparse: cast removes address space '__rcu' of expression
net/xfrm/xfrm_state.c:1871:17: sparse: sparse: incorrect type in argument 2 (different address spaces) @@ expected struct hlist_head *h @@ got struct hlist_head [noderef] __rcu * @@
net/xfrm/xfrm_state.c:1871:17: sparse: expected struct hlist_head *h
net/xfrm/xfrm_state.c:1871:17: sparse: got struct hlist_head [noderef] __rcu *
net/xfrm/xfrm_state.c:1874:17: sparse: sparse: cast removes address space '__rcu' of expression net/xfrm/xfrm_state.c:1874:17: sparse: sparse: cast removes address space '__rcu' of expression net/xfrm/xfrm_state.c:1874:17: sparse: sparse: cast removes address space '__rcu' of expression net/xfrm/xfrm_state.c:1874:17: sparse: sparse: cast removes address space '__rcu' of expression net/xfrm/xfrm_state.c:1874:17: sparse: sparse: cast removes address space '__rcu' of expression net/xfrm/xfrm_state.c:1874:17: sparse: sparse: cast removes address space '__rcu' of expression net/xfrm/xfrm_state.c:1874:17: sparse: sparse: cast removes address space '__rcu' of expression net/xfrm/xfrm_state.c:1874:17: sparse: sparse: cast removes address space '__rcu' of expression net/xfrm/xfrm_state.c:1874:17: sparse: sparse: cast removes address space '__rcu' of expression net/xfrm/xfrm_state.c:1874:17: sparse: sparse: cast removes address space '__rcu' of expression
net/xfrm/xfrm_state.c:1874:17: sparse: sparse: incorrect type in argument 2 (different address spaces) @@ expected struct hlist_head *h @@ got struct hlist_head [noderef] __rcu * @@
net/xfrm/xfrm_state.c:1874:17: sparse: expected struct hlist_head *h
net/xfrm/xfrm_state.c:1874:17: sparse: got struct hlist_head [noderef] __rcu *
net/xfrm/xfrm_state.c:2506:9: sparse: sparse: cast removes address space '__rcu' of expression net/xfrm/xfrm_state.c:2506:9: sparse: sparse: cast removes address space '__rcu' of expression net/xfrm/xfrm_state.c:2506:9: sparse: sparse: cast removes address space '__rcu' of expression net/xfrm/xfrm_state.c:2506:9: sparse: sparse: cast removes address space '__rcu' of expression net/xfrm/xfrm_state.c:2506:9: sparse: sparse: cast removes address space '__rcu' of expression net/xfrm/xfrm_state.c:2506:9: sparse: sparse: cast removes address space '__rcu' of expression net/xfrm/xfrm_state.c:2506:9: sparse: sparse: cast removes address space '__rcu' of expression net/xfrm/xfrm_state.c:2506:9: sparse: sparse: cast removes address space '__rcu' of expression net/xfrm/xfrm_state.c:2506:9: sparse: sparse: cast removes address space '__rcu' of expression net/xfrm/xfrm_state.c:2506:9: sparse: sparse: cast removes address space '__rcu' of expression net/xfrm/xfrm_state.c:2605:25: sparse: sparse: cast removes address space '__rcu' of expression net/xfrm/xfrm_state.c:2605:25: sparse: sparse: cast removes address space '__rcu' of expression net/xfrm/xfrm_state.c:2605:25: sparse: sparse: cast removes address space '__rcu' of expression net/xfrm/xfrm_state.c:2605:25: sparse: sparse: cast removes address space '__rcu' of expression net/xfrm/xfrm_state.c:2605:25: sparse: sparse: cast removes address space '__rcu' of expression net/xfrm/xfrm_state.c:2605:25: sparse: sparse: cast removes address space '__rcu' of expression net/xfrm/xfrm_state.c:2605:25: sparse: sparse: cast removes address space '__rcu' of expression net/xfrm/xfrm_state.c:2605:25: sparse: sparse: cast removes address space '__rcu' of expression net/xfrm/xfrm_state.c:2605:25: sparse: sparse: cast removes address space '__rcu' of expression net/xfrm/xfrm_state.c:2605:25: sparse: sparse: cast removes address space '__rcu' of expression
net/xfrm/xfrm_state.c:2605:25: sparse: sparse: incorrect type in argument 2 (different address spaces) @@ expected struct hlist_head *h @@ got struct hlist_head [noderef] __rcu * @@
net/xfrm/xfrm_state.c:2605:25: sparse: expected struct hlist_head *h
net/xfrm/xfrm_state.c:2605:25: sparse: got struct hlist_head [noderef] __rcu *
net/xfrm/xfrm_state.c:3270:31: sparse: sparse: incorrect type in assignment (different address spaces) @@ expected struct hlist_head [noderef] __rcu *state_bydst @@ got struct hlist_head * @@
net/xfrm/xfrm_state.c:3270:31: sparse: expected struct hlist_head [noderef] __rcu *state_bydst
net/xfrm/xfrm_state.c:3270:31: sparse: got struct hlist_head *
net/xfrm/xfrm_state.c:3273:31: sparse: sparse: incorrect type in assignment (different address spaces) @@ expected struct hlist_head [noderef] __rcu *state_bysrc @@ got struct hlist_head * @@
net/xfrm/xfrm_state.c:3273:31: sparse: expected struct hlist_head [noderef] __rcu *state_bysrc
net/xfrm/xfrm_state.c:3273:31: sparse: got struct hlist_head *
net/xfrm/xfrm_state.c:3276:31: sparse: sparse: incorrect type in assignment (different address spaces) @@ expected struct hlist_head [noderef] __rcu *state_byspi @@ got struct hlist_head * @@
net/xfrm/xfrm_state.c:3276:31: sparse: expected struct hlist_head [noderef] __rcu *state_byspi
net/xfrm/xfrm_state.c:3276:31: sparse: got struct hlist_head *
net/xfrm/xfrm_state.c:3279:31: sparse: sparse: incorrect type in assignment (different address spaces) @@ expected struct hlist_head [noderef] __rcu *state_byseq @@ got struct hlist_head * @@
net/xfrm/xfrm_state.c:3279:31: sparse: expected struct hlist_head [noderef] __rcu *state_byseq
net/xfrm/xfrm_state.c:3279:31: sparse: got struct hlist_head *
net/xfrm/xfrm_state.c:3297:33: sparse: sparse: incorrect type in argument 1 (different address spaces) @@ expected struct hlist_head *n @@ got struct hlist_head [noderef] __rcu *state_byseq @@
net/xfrm/xfrm_state.c:3297:33: sparse: expected struct hlist_head *n
net/xfrm/xfrm_state.c:3297:33: sparse: got struct hlist_head [noderef] __rcu *state_byseq
net/xfrm/xfrm_state.c:3299:33: sparse: sparse: incorrect type in argument 1 (different address spaces) @@ expected struct hlist_head *n @@ got struct hlist_head [noderef] __rcu *state_byspi @@
net/xfrm/xfrm_state.c:3299:33: sparse: expected struct hlist_head *n
net/xfrm/xfrm_state.c:3299:33: sparse: got struct hlist_head [noderef] __rcu *state_byspi
net/xfrm/xfrm_state.c:3301:33: sparse: sparse: incorrect type in argument 1 (different address spaces) @@ expected struct hlist_head *n @@ got struct hlist_head [noderef] __rcu *state_bysrc @@
net/xfrm/xfrm_state.c:3301:33: sparse: expected struct hlist_head *n
net/xfrm/xfrm_state.c:3301:33: sparse: got struct hlist_head [noderef] __rcu *state_bysrc
net/xfrm/xfrm_state.c:3303:33: sparse: sparse: incorrect type in argument 1 (different address spaces) @@ expected struct hlist_head *n @@ got struct hlist_head [noderef] __rcu *state_bydst @@
net/xfrm/xfrm_state.c:3303:33: sparse: expected struct hlist_head *n
net/xfrm/xfrm_state.c:3303:33: sparse: got struct hlist_head [noderef] __rcu *state_bydst

>> net/xfrm/xfrm_state.c:3320:17: sparse: sparse: incorrect type in argument 1 (different address spaces) @@     expected struct hlist_head const *h @@     got struct hlist_head [noderef] __rcu * @@

net/xfrm/xfrm_state.c:3320:17: sparse: expected struct hlist_head const *h
net/xfrm/xfrm_state.c:3320:17: sparse: got struct hlist_head [noderef] __rcu *
net/xfrm/xfrm_state.c:3321:17: sparse: sparse: incorrect type in argument 1 (different address spaces) @@ expected struct hlist_head const *h @@ got struct hlist_head [noderef] __rcu * @@
net/xfrm/xfrm_state.c:3321:17: sparse: expected struct hlist_head const *h
net/xfrm/xfrm_state.c:3321:17: sparse: got struct hlist_head [noderef] __rcu *
net/xfrm/xfrm_state.c:3322:17: sparse: sparse: incorrect type in argument 1 (different address spaces) @@ expected struct hlist_head const *h @@ got struct hlist_head [noderef] __rcu * @@
net/xfrm/xfrm_state.c:3322:17: sparse: expected struct hlist_head const *h
net/xfrm/xfrm_state.c:3322:17: sparse: got struct hlist_head [noderef] __rcu *
net/xfrm/xfrm_state.c:3323:17: sparse: sparse: incorrect type in argument 1 (different address spaces) @@ expected struct hlist_head const *h @@ got struct hlist_head [noderef] __rcu * @@
net/xfrm/xfrm_state.c:3323:17: sparse: expected struct hlist_head const *h
net/xfrm/xfrm_state.c:3323:17: sparse: got struct hlist_head [noderef] __rcu *
net/xfrm/xfrm_state.c:3327:33: sparse: sparse: incorrect type in argument 1 (different address spaces) @@ expected struct hlist_head *n @@ got struct hlist_head [noderef] __rcu *state_byseq @@
net/xfrm/xfrm_state.c:3327:33: sparse: expected struct hlist_head *n
net/xfrm/xfrm_state.c:3327:33: sparse: got struct hlist_head [noderef] __rcu *state_byseq
net/xfrm/xfrm_state.c:3328:33: sparse: sparse: incorrect type in argument 1 (different address spaces) @@ expected struct hlist_head *n @@ got struct hlist_head [noderef] __rcu *state_byspi @@
net/xfrm/xfrm_state.c:3328:33: sparse: expected struct hlist_head *n
net/xfrm/xfrm_state.c:3328:33: sparse: got struct hlist_head [noderef] __rcu *state_byspi
net/xfrm/xfrm_state.c:3329:33: sparse: sparse: incorrect type in argument 1 (different address spaces) @@ expected struct hlist_head *n @@ got struct hlist_head [noderef] __rcu *state_bysrc @@
net/xfrm/xfrm_state.c:3329:33: sparse: expected struct hlist_head *n
net/xfrm/xfrm_state.c:3329:33: sparse: got struct hlist_head [noderef] __rcu *state_bysrc
net/xfrm/xfrm_state.c:3330:33: sparse: sparse: incorrect type in argument 1 (different address spaces) @@ expected struct hlist_head *n @@ got struct hlist_head [noderef] __rcu *state_bydst @@
net/xfrm/xfrm_state.c:3330:33: sparse: expected struct hlist_head *n
net/xfrm/xfrm_state.c:3330:33: sparse: got struct hlist_head [noderef] __rcu *state_bydst
net/xfrm/xfrm_state.c: note: in included file (through include/linux/rbtree.h, include/linux/mm_types.h, include/linux/uio.h, ...):
include/linux/rcupdate.h:897:25: sparse: sparse: context imbalance in 'xfrm_register_type' - unexpected unlock include/linux/rcupdate.h:897:25: sparse: sparse: context imbalance in 'xfrm_unregister_type' - unexpected unlock include/linux/rcupdate.h:897:25: sparse: sparse: context imbalance in 'xfrm_get_type' - unexpected unlock include/linux/rcupdate.h:897:25: sparse: sparse: context imbalance in 'xfrm_register_type_offload' - unexpected unlock include/linux/rcupdate.h:897:25: sparse: sparse: context imbalance in 'xfrm_unregister_type_offload' - unexpected unlock include/linux/rcupdate.h:897:25: sparse: sparse: context imbalance in 'xfrm_set_type_offload' - unexpected unlock net/xfrm/xfrm_state.c:934:17: sparse: sparse: dereference of noderef expression
net/xfrm/xfrm_state.c:976:17: sparse: sparse: dereference of noderef expression
net/xfrm/xfrm_state.c:58:39: sparse: sparse: incorrect type in argument 1 (different address spaces) @@ expected struct refcount_struct [usertype] *r @@ got struct refcount_struct [noderef] __rcu * @@
net/xfrm/xfrm_state.c:58:39: sparse: expected struct refcount_struct [usertype] *r
net/xfrm/xfrm_state.c:58:39: sparse: got struct refcount_struct [noderef] __rcu *
net/xfrm/xfrm_state.c:58:39: sparse: sparse: incorrect type in argument 1 (different address spaces) @@ expected struct refcount_struct [usertype] *r @@ got struct refcount_struct [noderef] __rcu * @@
net/xfrm/xfrm_state.c:58:39: sparse: expected struct refcount_struct [usertype] *r
net/xfrm/xfrm_state.c:58:39: sparse: got struct refcount_struct [noderef] __rcu *
net/xfrm/xfrm_state.c:58:39: sparse: sparse: incorrect type in argument 1 (different address spaces) @@ expected struct refcount_struct [usertype] *r @@ got struct refcount_struct [noderef] __rcu * @@
net/xfrm/xfrm_state.c:58:39: sparse: expected struct refcount_struct [usertype] *r
net/xfrm/xfrm_state.c:58:39: sparse: got struct refcount_struct [noderef] __rcu *
net/xfrm/xfrm_state.c:58:39: sparse: sparse: incorrect type in argument 1 (different address spaces) @@ expected struct refcount_struct [usertype] *r @@ got struct refcount_struct [noderef] __rcu * @@
net/xfrm/xfrm_state.c:58:39: sparse: expected struct refcount_struct [usertype] *r
net/xfrm/xfrm_state.c:58:39: sparse: got struct refcount_struct [noderef] __rcu *
net/xfrm/xfrm_state.c:58:39: sparse: sparse: incorrect type in argument 1 (different address spaces) @@ expected struct refcount_struct [usertype] *r @@ got struct refcount_struct [noderef] __rcu * @@
net/xfrm/xfrm_state.c:58:39: sparse: expected struct refcount_struct [usertype] *r
net/xfrm/xfrm_state.c:58:39: sparse: got struct refcount_struct [noderef] __rcu *
net/xfrm/xfrm_state.c:1655:9: sparse: sparse: dereference of noderef expression
net/xfrm/xfrm_state.c:58:39: sparse: sparse: incorrect type in argument 1 (different address spaces) @@ expected struct refcount_struct [usertype] *r @@ got struct refcount_struct [noderef] __rcu * @@
net/xfrm/xfrm_state.c:58:39: sparse: expected struct refcount_struct [usertype] *r
net/xfrm/xfrm_state.c:58:39: sparse: got struct refcount_struct [noderef] __rcu *
net/xfrm/xfrm_state.c:1778:9: sparse: sparse: dereference of noderef expression
net/xfrm/xfrm_state.c:1814:9: sparse: sparse: dereference of noderef expression net/xfrm/xfrm_state.c:2094:17: sparse: sparse: dereference of noderef expression net/xfrm/xfrm_state.c:2113:17: sparse: sparse: dereference of noderef expression net/xfrm/xfrm_state.c:2315:17: sparse: sparse: dereference of noderef expression
net/xfrm/xfrm_state.c: note: in included file:
include/net/xfrm.h:1971:16: sparse: sparse: incompatible types in comparison expression (different address spaces):
include/net/xfrm.h:1971:16: sparse: struct sock [noderef] __rcu *
include/net/xfrm.h:1971:16: sparse: struct sock *

vim +3320 net/xfrm/xfrm_state.c

3307
3308 void xfrm_state_fini(struct net *net)
3309 {
3310 unsigned int sz;
3311 int i;
3312
3313 flush_work(&net->xfrm.state_hash_work);
3314 xfrm_state_flush(net, 0, false);
3315 flush_work(&xfrm_state_gc_work);
3316
3317 WARN_ON(!list_empty(&net->xfrm.state_all));
3318
3319 for (i = 0; i <= net->xfrm.state_hmask; i++) {

> 3320			WARN_ON(!hlist_empty(net->xfrm.state_byseq + i));

3321 WARN_ON(!hlist_empty(net->xfrm.state_byspi + i));
3322 WARN_ON(!hlist_empty(net->xfrm.state_bysrc + i));
3323 WARN_ON(!hlist_empty(net->xfrm.state_bydst + i));
3324 }
3325
3326 sz = (net->xfrm.state_hmask + 1) * sizeof(struct hlist_head);
3327 xfrm_hash_free(net->xfrm.state_byseq, sz);
3328 xfrm_hash_free(net->xfrm.state_byspi, sz);
3329 xfrm_hash_free(net->xfrm.state_bysrc, sz);
3330 xfrm_hash_free(net->xfrm.state_bydst, sz);
3331 free_percpu(net->xfrm.state_cache_input);
3332 }
3333

Signature · 2 lines
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
#9 lore raw

Re: [PATCH ipsec 6/6] xfrm: check all hash buckets for leftover states during netns deletion

Hi Steffen,

Quoted 24 lines · kernel test robot

Hi Sabrina,

kernel test robot noticed the following build warnings:

[auto build test WARNING on klassert-ipsec-next/master]
[also build test WARNING on klassert-ipsec/master net/main net-next/main linus/master v6.18-rc1 next-20251016] [If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]

url:    https://github.com/intel-lab-lkp/linux/commits/Sabrina-Dubroca/xfrm-drop-SA-reference-in-xfrm_state_update-if-dir-doesn-t-match/20251016-184507
base:   https://git.kernel.org/pub/scm/linux/kernel/git/klassert/ipsec-next.git master
patch link:    https://lore.kernel.org/r/2a743a05bbad7ebdc36c2c86a5fcbb9e99071c7b.1760610268.git.sd%40queasysnail.net
patch subject: [PATCH ipsec 6/6] xfrm: check all hash buckets for leftover states during netns deletion
config: x86_64-randconfig-r123-20251017 (https://download.01.org/0day-ci/archive/20251017/[email protected]/config)
compiler: clang version 20.1.8 (https://github.com/llvm/llvm-project 87f0227cb60147a26a1eeb4fb06e3b505e9c7261)
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20251017/[email protected]/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/

sparse warnings: (new ones prefixed by >>)

[...]

Quoted 4 lines

3308 void xfrm_state_fini(struct net *net)
3309 {
3310 unsigned int sz;
3311 int i;

Quoted 9 lines

3312
3313 flush_work(&net->xfrm.state_hash_work);
3314 xfrm_state_flush(net, 0, false);
3315 flush_work(&xfrm_state_gc_work);
3316
3317 WARN_ON(!list_empty(&net->xfrm.state_all));
3318
3319 for (i = 0; i <= net->xfrm.state_hmask; i++) {

Quoted 1 line

3320 WARN_ON(!hlist_empty(net->xfrm.state_byseq + i));

So, before my patch there was a sparse waraning on the

	WARN_ON(!hlist_empty(net->xfrm.state_by*));

lines, and now there's a sparse warning on the loop. (and plenty on other lines in net/xfrm/xfrm_state.c)

This bot message gave me the push to finally take a look at all the sparse warnings in net/xfrm/xfrm_state.c, I have solutions for a big chunk of them (and a few in other files).

If you want to drop this patch from the set, I'll re-send it later, on top of the sparse stuff. The rest of the series works without it. If you want to take it as is, it doesn't change the sparse situation in this file (a few warnings moved around) and I'll do the sparse cleanups on top of it.

Thanks,

Signature · 1 line
Sabrina
maintainer
#10 lore raw

Re: [PATCH ipsec 6/6] xfrm: check all hash buckets for leftover states during netns deletion

Hi Sabrina,

Quoted 53 lines · Sabrina Dubroca

Hi Steffen,

Quoted 24 lines · kernel test robot

Hi Sabrina,

kernel test robot noticed the following build warnings:

[auto build test WARNING on klassert-ipsec-next/master]
[also build test WARNING on klassert-ipsec/master net/main net-next/main linus/master v6.18-rc1 next-20251016] [If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]

url:    https://github.com/intel-lab-lkp/linux/commits/Sabrina-Dubroca/xfrm-drop-SA-reference-in-xfrm_state_update-if-dir-doesn-t-match/20251016-184507
base:   https://git.kernel.org/pub/scm/linux/kernel/git/klassert/ipsec-next.git master
patch link:    https://lore.kernel.org/r/2a743a05bbad7ebdc36c2c86a5fcbb9e99071c7b.1760610268.git.sd%40queasysnail.net
patch subject: [PATCH ipsec 6/6] xfrm: check all hash buckets for leftover states during netns deletion
config: x86_64-randconfig-r123-20251017 (https://download.01.org/0day-ci/archive/20251017/[email protected]/config)
compiler: clang version 20.1.8 (https://github.com/llvm/llvm-project 87f0227cb60147a26a1eeb4fb06e3b505e9c7261)
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20251017/[email protected]/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/

sparse warnings: (new ones prefixed by >>)

[...]

Quoted 4 lines

3308 void xfrm_state_fini(struct net *net)
3309 {
3310 unsigned int sz;
3311 int i;

Quoted 9 lines

3312
3313 flush_work(&net->xfrm.state_hash_work);
3314 xfrm_state_flush(net, 0, false);
3315 flush_work(&xfrm_state_gc_work);
3316
3317 WARN_ON(!list_empty(&net->xfrm.state_all));
3318
3319 for (i = 0; i <= net->xfrm.state_hmask; i++) {

Quoted 1 line

3320 WARN_ON(!hlist_empty(net->xfrm.state_byseq + i));

So, before my patch there was a sparse waraning on the

	WARN_ON(!hlist_empty(net->xfrm.state_by*));

lines, and now there's a sparse warning on the loop. (and plenty on other lines in net/xfrm/xfrm_state.c)

This bot message gave me the push to finally take a look at all the sparse warnings in net/xfrm/xfrm_state.c, I have solutions for a big chunk of them (and a few in other files).

Quoted 5 lines

If you want to drop this patch from the set, I'll re-send it later, on top of the sparse stuff. The rest of the series works without it. If you want to take it as is, it doesn't change the sparse situation in this file (a few warnings moved around) and I'll do the sparse cleanups on top of it.

I'll take the patchset as is and wait for your sparse fixes on top.

Thanks!

maintainer
#11 lore raw
In reply to Sabrina Dubroca
Quoted 12 lines · Sabrina Dubroca

These are mostly independent fixes for small issues for state creation/modification/deletion (except for the 2 migrate patches).

Sabrina Dubroca (6):
xfrm: drop SA reference in xfrm_state_update if dir doesn't match
xfrm: also call xfrm_state_delete_tunnel at destroy time for states
that were never added
xfrm: make state as DEAD before final put when migrate fails
xfrm: call xfrm_dev_state_delete when xfrm_state_migrate fails to add
the state
xfrm: set err and extack on failure to create pcpu SA
xfrm: check all hash buckets for leftover states during netns deletion

Series applied, thanks a lot Sabrina!