refscale: Fix possible oops in the typesafe_cleanup()
Subsystems (2)
Zqiang
reviewerPATCH 1/1 refscale: Fix possible oops in the typesafe_cleanup()
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.
--- kernel/rcu/refscale.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)
kernel/rcu/refscale.c +3 -1
@@ -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;
Signature · 1 line
2.17.1
Zqiang
reviewerPATCH 1/1 rcuscale: Handle cur_ops->init() failures for srcu testing
The init_srcu_struct() and init_srcu_struct_atomic() can fail, but rcuscale ignored their return values, proceeding to run the test on an uninitialized srcu_struct and to invoke cleanup_srcu_struct() on it during unwind. this will cause kernel panic.
This commit make the rcu_scale_ops->init() return bool, check the return values, reset srcu_ctlp to NULL on failure so that the cleanup path skips cleanup_srcu_struct(), and return -ENOMEM when cur_ops->init() fails.
--- kernel/rcu/rcuscale.c | 31 +++++++++++++++++++++++-------- 1 file changed, 23 insertions(+), 8 deletions(-)
kernel/rcu/rcuscale.c +23 -8
@@ -147,7 +147,7 @@ static unsigned long b_rcu_gp_test_finished;struct rcu_scale_ops {int ptype; -void (*init)(void ); +bool (*init)(void );void (*cleanup)(void );int (*readlock)(void );void (*readunlock)(int idx);
@@ -185,8 +185,9 @@ static unsigned long __maybe_unused rcu_no_completed(void)return 0 ; } -static void rcu_sync_scale_init (void ) +static bool rcu_sync_scale_init (void ) { +return true; }static struct rcu_scale_ops rcu_ops = {
@@ -269,15 +270,21 @@ static struct rcu_scale_ops srcu_ops = {static struct srcu_struct srcud; -static void srcu_sync_scale_init (void ) +static bool srcu_sync_scale_init (void ) { srcu_ctlp = &srcud; -init_srcu_struct (srcu_ctlp); +if (init_srcu_struct (srcu_ctlp)) { + srcu_ctlp =NULL ; +return false; + } + +return true; }static void srcu_sync_scale_cleanup (void ) { -cleanup_srcu_struct (srcu_ctlp); +if (srcu_ctlp) +cleanup_srcu_struct (srcu_ctlp); }static struct rcu_scale_ops srcud_ops = {
@@ -314,10 +321,15 @@ static void srcu_scale_atomic_synchronize(void)synchronize_srcu_atomic (srcu_ctlp); } -static void srcu_atomic_scale_init (void ) +static bool srcu_atomic_scale_init (void ) { srcu_ctlp = &srcua; -init_srcu_struct_atomic (srcu_ctlp); +if (init_srcu_struct_atomic (srcu_ctlp)) { + srcu_ctlp =NULL ; +return false; + } + +return true; }static struct rcu_scale_ops srcua_ops = {
@@ -1162,7 +1174,10 @@ rcu_scale_init(void) goto unwind; }if (cur_ops->init) - cur_ops->init (); +if (!cur_ops->init ()) { + firsterr = -ENOMEM ; + goto unwind; + }if (cur_ops->rso_gp_kthread) { kthread_tp = cur_ops->rso_gp_kthread ();
Signature · 1 line
2.17.1
Zqiang
reviewerPATCH 1/1 rcuscale: Disable gp_exp when cur_ops->exp_completed is not implemented
In reply to ZqiangThe tasks, tasks-rude and tasks-tracing scale ops provide exp_sync but no exp_completed. Passing gp_exp=1 with one of these scale types makes the reader, writer and kfree paths call cur_ops->exp_completed(), which is NULL, resulting in a NULL pointer dereference.
This commit disable gp_exp in rcu_scale_init() when cur_ops->exp_completed is not implemented.
---
# insmod rcuscale.ko scale_type=tasks kfree_rcu_test=1 gp_exp=1
[ 957.313922][ T1311] BUG: kernel NULL pointer dereference, address: 0000000000000000 [ 957.315073][ T1311] #PF: supervisor instruction fetch in kernel mode [ 957.315918][ T1311] #PF: error_code(0x0010) - not-present page [ 957.316649][ T1311] PGD 0 P4D 0 [ 957.317082][ T1311] Oops: Oops: 0010 [#1] SMP KASAN PTI [ 957.317799][ T1311] CPU: 3 UID: 0 PID: 1311 Comm: kfree_scale_thr Tainted: G W E 7.4.0-rc1+ #118 PREEMPT(full) d93f260d09ad39f5830a6d4abcacc3510ff31cf2 [ 957.319925][ T1311] Tainted: [W]=WARN, [E]=UNSIGNED_MODULE [ 957.321343][ T1311] Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS 1.10.2-1ubuntu1 04/01/2014 [ 957.322763][ T1311] RIP: 0010:0x0 [ 957.325961][ T1311] Code: Unable to access opcode bytes at 0xffffffffffffffd6. [ 957.327030][ T1311] RSP: 0018:ffff888110a67d88 EFLAGS: 00010246 [ 957.334010][ T1311] RAX: 0000000000000000 RBX: ffffffffc00b8d80 RCX: ffffffffc0605ee8 [ 957.335015][ T1311] RDX: 1ffffffff80171b7 RSI: 0000000000000004 RDI: ffffffffc00b8db8 [ 957.336000][ T1311] RBP: ffff888110a67e48 R08: fffffbfff8017068 R09: fffffbfff8017068 [ 957.337000][ T1311] R10: fffffbfff8017068 R11: 0000000000000003 R12: 0000000000000000 [ 957.337973][ T1311] R13: 0000000000000000 R14: ffffffffc0605da0 R15: 0000000000000003 [ 957.338979][ T1311] FS: 0000000000000000(0000) GS:ffff8882658e2000(0000) knlGS:0000000000000000 [ 957.340092][ T1311] CS: 0010 DS: 0000 ES: 0000 CR0: 0000000080050033 [ 957.340884][ T1311] CR2: ffffffffffffffd6 CR3: 00000001caaec000 CR4: 00000000000026f0 [ 957.343924][ T1311] Call Trace: [ 957.344354][ T1311] <TASK> [ 957.344739][ T1311] kfree_scale_thread+0x19c/0x5a0 [rcuscale 4b3bd1c4339388c164c4923083b9e192f124c200] [ 957.345977][ T1311] ? __pfx_kfree_scale_thread+0x10/0x10 [rcuscale 4b3bd1c4339388c164c4923083b9e192f124c200] [ 957.347251][ T1311] ? __kasan_check_read+0x15/0x20 [ 957.351496][ T1311] ? __kthread_parkme+0x11e/0x230 [ 957.352127][ T1311] ? __pfx_kfree_scale_thread+0x10/0x10 [rcuscale 4b3bd1c4339388c164c4923083b9e192f124c200] [ 957.353924][ T1311] kthread+0x3ca/0x500 [ 957.354484][ T1311] ? __pfx_kthread+0x10/0x10 [ 957.358092][ T1311] ret_from_fork+0x7be/0xaf0 [ 957.358652][ T1311] ? __pfx_ret_from_fork+0x10/0x10 [ 957.359209][ T1311] ? __kasan_check_read+0x15/0x20 [ 957.359737][ T1311] ? __switch_to+0x199/0xe90 [ 957.360224][ T1311] ? __switch_to_asm+0x39/0x70 [ 957.360725][ T1311] ? __pfx_kthread+0x10/0x10 [ 957.361207][ T1311] ret_from_fork_asm+0x1a/0x30 [ 957.362616][ T1311] </TASK>
kernel/rcu/rcuscale.c | 3 +++ 1 file changed, 3 insertions(+)
kernel/rcu/rcuscale.c +3 -0
@@ -1186,6 +1186,9 @@ rcu_scale_init(void)if (kthread_tp) kthread_stime = kthread_tp->stime; } + +if (gp_exp && !cur_ops->exp_completed) + gp_exp = false; nrealexp = nexp;if (kfree_rcu_test)return kfree_scale_init ();
Signature · 1 line
2.17.1
Zqiang
reviewerPATCH 1/1 rcuscale: Invoke cur_ops->cleanup() in kfree_scale_cleanup()
In reply to ZqiangWhen the kfree_rcu test is selected, rcu_scale_cleanup() delegates to kfree_scale_cleanup() and returns, so cur_ops->cleanup() is never invoked, for the srcud and srcua flavors, this fails to release the srcu_struct which allocated by cur_ops->init().
This commit therefore call cur_ops->cleanup() before kfree_scale_cleanup() ends, and also guard the cur_ops is not NULL.
---
insmod rcuscale.ko scale_type=srcua kfree_rcu_test=1 gp_exp=1
unreferenced object (percpu) 0x607d981476c0 (size 576):
comm "insmod", pid 1151, jiffies 4294997383 hex dump (first 32 bytes on cpu 11):
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
backtrace (crc ad6c346):
kmemleak_alloc_percpu+0x89/0xa0
pcpu_alloc_noprof+0x69c/0xd30
init_srcu_struct_fields+0xb45/0x1070
__init_srcu_struct_common+0x4d/0x60
__init_srcu_struct_atomic+0x45/0x70
0xffffffffc060380e
0xffffffffc06071d8
do_one_initcall+0xd9/0x620
do_init_module+0x2b6/0x830
load_module+0x7b5b/0x81d0
init_module_from_file+0x15f/0x180
idempotent_init_module+0x3b1/0x800
__x64_sys_finit_module+0x11a/0x170
x64_sys_call+0x3e0/0x1fb0
do_syscall_64+0xd2/0x750
entry_SYSCALL_64_after_hwframe+0x76/0x7ekernel/rcu/rcuscale.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)
kernel/rcu/rcuscale.c +3 -1
@@ -901,6 +901,8 @@ kfree_scale_cleanup(void) kfree_reader_tasks =NULL ; } +if (cur_ops && cur_ops->cleanup !=NULL ) + cur_ops->cleanup ();torture_cleanup_end (); }
@@ -1036,7 +1038,7 @@ rcu_scale_cleanup(void)SCALEOUT_ERRSTRING ("No expedited async GPs, so went with async!" );// If built-in, just report all of the GP kthread's CPU time. -if (IS_BUILTIN (CONFIG_RCU_SCALE_TEST ) && !kthread_tp && cur_ops->rso_gp_kthread) +if (IS_BUILTIN (CONFIG_RCU_SCALE_TEST ) && cur_ops && !kthread_tp && cur_ops->rso_gp_kthread) kthread_tp = cur_ops->rso_gp_kthread ();if (kthread_tp) {u32 ns;
Signature · 1 line
2.17.1