Skip to contentSkip to conversation

mm/zswap: fix error pointer free in zswap_cpu_comp_prepare()

PATCH Applied Pavel Butsykin 4 participants 6 messages Dec 31, 2025 – Jan 4

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

PATCH 1/1 mm/zswap: fix error pointer free in zswap_cpu_comp_prepare()

crypto_alloc_acomp_node() may return ERR_PTR(), but the fail path checks only for NULL and can pass an error pointer to crypto_free_acomp(). Use IS_ERR_OR_NULL() to only free valid acomp instances.

Fixes 779b9955f643 ("mm: zswap: move allocations during CPU init outside the lock")
---
 mm/zswap.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
mm/zswap.c +1 -1
index 5d0f8b13a958..ac9b7a60736b 100644
@@ -787,7 +787,7 @@ static int zswap_cpu_comp_prepare(unsigned int cpu, struct hlist_node *node)
 	return 0;
 
 fail:
-	if (acomp)
+	if (!IS_ERR_OR_NULL(acomp))
 		crypto_free_acomp(acomp);
 	kfree(buffer);
 	return ret;
Signature · 1 line
2.52.0
#1 lore raw
Quoted 16 lines · On Wed, 31 Dec 2025 11:46:38 +0400 Pavel Butsykin <[email protected]>…

crypto_alloc_acomp_node() may return ERR_PTR(), but the fail path checks only for NULL and can pass an error pointer to crypto_free_acomp(). Use IS_ERR_OR_NULL() to only free valid acomp instances.

Fixes 779b9955f643 ("mm: zswap: move allocations during CPU init outside the lock")
---
 mm/zswap.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
mm/zswap.c +0 -0
index 5d0f8b13a958..ac9b7a60736b 100644

mm/zswap.c static int zswap_cpu_comp_prepare(unsigned int cpu, struct hlist_node *node) · line 793

Quoted 8 lines
mm/zswap.c +1 -1
@@ -787,7 +787,7 @@ static int zswap_cpu_comp_prepare(unsigned int cpu, struct hlist_node *node)
 	return 0;
 
 fail:
-	if (acomp)
+	if (!IS_ERR_OR_NULL(acomp))
 		crypto_free_acomp(acomp);
 	kfree(buffer);
 	return ret;

I understand you are keeping NULL case to keep the old behavior. But, seems the case cannot happen to me for following reasons.

First of all, the old NULL check was only for crypto_alloc_acomp_node() failure. But crypto_alloc_acomp_node() seems not returning NULL, to by breif look of the code. And the failure check of crypto_alloc_acomp_node() is actually doing only IS_ERR() check.

So, it seems IS_ERR() here is enough. Or, if I missed a case that crypto_alloc_acomp_node() returns NULL, the above crypto_alloc_acomp_node() failure check should be updated to use IS_ERR_OR_NULL()?

Thanks,
SJ

[...]

#2 lore raw
Quoted 30 lines · SeongJae Park
Quoted 16 lines · On Wed, 31 Dec 2025 11:46:38 +0400 Pavel Butsykin <[email protected]>…

crypto_alloc_acomp_node() may return ERR_PTR(), but the fail path checks only for NULL and can pass an error pointer to crypto_free_acomp(). Use IS_ERR_OR_NULL() to only free valid acomp instances.

Fixes 779b9955f643 ("mm: zswap: move allocations during CPU init outside the lock")
---
  mm/zswap.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
mm/zswap.c +0 -0
index 5d0f8b13a958..ac9b7a60736b 100644

mm/zswap.c static int zswap_cpu_comp_prepare(unsigned int cpu, struct hlist_node *node) · line 793

Quoted 8 lines
mm/zswap.c +1 -1
@@ -787,7 +787,7 @@ static int zswap_cpu_comp_prepare(unsigned int cpu, struct hlist_node *node)
  	return 0;
 
  fail:
-	if (acomp)
+	if (!IS_ERR_OR_NULL(acomp))
  		crypto_free_acomp(acomp);
  	kfree(buffer);
  	return ret;

I understand you are keeping NULL case to keep the old behavior. But, seems the case cannot happen to me for following reasons.

Quoted 9 lines

First of all, the old NULL check was only for crypto_alloc_acomp_node() failure. But crypto_alloc_acomp_node() seems not returning NULL, to by breif look of the code. And the failure check of crypto_alloc_acomp_node() is actually doing only IS_ERR() check.

So, it seems IS_ERR() here is enough. Or, if I missed a case that crypto_alloc_acomp_node() returns NULL, the above crypto_alloc_acomp_node() failure check should be updated to use IS_ERR_OR_NULL()?

We have 'goto fail;' right before crypto_alloc_acomp_node() for the case where kmalloc_node fails to allocate memory. In that case, 'acomp' will still be initialized to NULL.

Reviewed-by
#3 lore raw
Quoted 36 lines · On Fri, 2 Jan 2026 10:51:01 +0400 Pavel Butsykin <[email protected]>…
Quoted 35 lines · SeongJae Park
Quoted 16 lines · On Wed, 31 Dec 2025 11:46:38 +0400 Pavel Butsykin <[email protected]>…

crypto_alloc_acomp_node() may return ERR_PTR(), but the fail path checks only for NULL and can pass an error pointer to crypto_free_acomp(). Use IS_ERR_OR_NULL() to only free valid acomp instances.

Fixes 779b9955f643 ("mm: zswap: move allocations during CPU init outside the lock")
---
  mm/zswap.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
mm/zswap.c +0 -0
index 5d0f8b13a958..ac9b7a60736b 100644

mm/zswap.c static int zswap_cpu_comp_prepare(unsigned int cpu, struct hlist_node *node) · line 793

Quoted 8 lines
mm/zswap.c +1 -1
@@ -787,7 +787,7 @@ static int zswap_cpu_comp_prepare(unsigned int cpu, struct hlist_node *node)
  	return 0;
 
  fail:
-	if (acomp)
+	if (!IS_ERR_OR_NULL(acomp))
  		crypto_free_acomp(acomp);
  	kfree(buffer);
  	return ret;

I understand you are keeping NULL case to keep the old behavior. But, seems the case cannot happen to me for following reasons.

First of all, the old NULL check was only for crypto_alloc_acomp_node() failure. But crypto_alloc_acomp_node() seems not returning NULL, to by breif look of the code. And the failure check of crypto_alloc_acomp_node() is actually doing only IS_ERR() check.

Quoted 8 lines
Quoted 4 lines

So, it seems IS_ERR() here is enough. Or, if I missed a case that crypto_alloc_acomp_node() returns NULL, the above crypto_alloc_acomp_node() failure check should be updated to use IS_ERR_OR_NULL()?

We have 'goto fail;' right before crypto_alloc_acomp_node() for the case where kmalloc_node fails to allocate memory. In that case, 'acomp' will still be initialized to NULL.

Ah, you are right. Thank you for fixing this. Please feel free to add

Thanks,
SJ

[...]

Acked-by on v1 1/1
#4 lore raw
In reply to Pavel Butsykin
Quoted 7 lines · Pavel Butsykin

crypto_alloc_acomp_node() may return ERR_PTR(), but the fail path checks only for NULL and can pass an error pointer to crypto_free_acomp(). Use IS_ERR_OR_NULL() to only free valid acomp instances.

Fixes 779b9955f643 ("mm: zswap: move allocations during CPU init outside the lock")

Thanks for the fix!

Quoted 20 lines
---
 mm/zswap.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
mm/zswap.c +1 -1
index 5d0f8b13a958..ac9b7a60736b 100644
@@ -787,7 +787,7 @@ static int zswap_cpu_comp_prepare(unsigned int cpu, struct hlist_node *node)
 	return 0;
 
 fail:
-	if (acomp)
+	if (!IS_ERR_OR_NULL(acomp))
 		crypto_free_acomp(acomp);
 	kfree(buffer);
 	return ret;
Signature · 1 line
2.52.0
maintainer Acked-by on v1 1/1
#5 lore raw
In reply to Pavel Butsykin
Quoted 8 lines · Pavel Butsykin

crypto_alloc_acomp_node() may return ERR_PTR(), but the fail path checks only for NULL and can pass an error pointer to crypto_free_acomp(). Use IS_ERR_OR_NULL() to only free valid acomp instances.

Fixes 779b9955f643 ("mm: zswap: move allocations during CPU init outside the lock")