[PATCH] refscale: Fix possible oops in the typesafe_cleanup()
From: Zqiang
Date: Fri Nov 20 2026 - 05:05:06 EST
If typesafe_alloc_one() fails partway through populating rtsarray[],
the typesafe_init() returns false but leaves rtsarray_size set to the
full requested count. The unwind path then calls typesafe_cleanup(),
which loops over rtsarray_size and passes the NULL pointer to the
kmem_cache_free(). unlike kfree(), kmem_cache_free() does not accept
a NULL pointer, this will cause oops.
This commit set rtsarray_size to the number of successfully allocated
elements before returning false, so that typesafe_cleanup() only frees
the objects that were actually allocated.
Signed-off-by: Zqiang <qiang.zhang@xxxxxxxxx>
---
kernel/rcu/refscale.c | 4 +++-
1 file changed, 3 insertions(+), 1 deletion(-)
diff --git a/kernel/rcu/refscale.c b/kernel/rcu/refscale.c
index 4157774f01ca..0843bff9c3da 100644
--- a/kernel/rcu/refscale.c
+++ b/kernel/rcu/refscale.c
@@ -1143,8 +1143,10 @@ static bool typesafe_init(void)
return false;
for (idx = 0; idx < rtsarray_size; idx++) {
rtsarray[idx] = typesafe_alloc_one();
- if (!rtsarray[idx])
+ if (!rtsarray[idx]) {
+ rtsarray_size = idx;
return false;
+ }
}
if (cur_ops == &typesafe_ref_ops) {
rts_acquire = typesafe_ref_acquire;
--
2.17.1