rcu: Make sure new krcp free business is handled after the wanted rcu grace period.
PATCH v2 1/1 rcu: Make sure new krcp free business is handled after the wanted rcu grace period.
In kfree_rcu_monitor(), new free business at krcp is attached to any free channel at krwp. kfree_rcu_monitor() is responsible to make sure new free business is handled after the rcu grace period. But if there is any none-free channel at krwp already, that means there is an on-going rcu work, which will cause the kvfree_call_rcu()-triggered free business is done before the wanted rcu grace period ends.
This commit ignore krwp which has non-free channel at kfree_rcu_monitor(), to fix the issue that kvfree_call_rcu() loses effectiveness.
Below is the css_set obj "from_cset" use-after-free case caused by kvfree_call_rcu() losing effectiveness.
CPU 0 calls rcu_read_lock(), then use "from_cset", then hard irq comes, the task is schedule out.
CPU 1 calls kfree_rcu(cset, rcu_head), willing to free "from_cset" after new gp. But "from_cset" is freed right after current gp end. "from_cset" is reallocated. CPU 0 's task arrives back, references "from_cset"'s member, which causes crash.
CPU 0 CPU 1count_memcg_event_mm()
|rcu_read_lock() <---
|mem_cgroup_from_task()
|// css_set_ptr is the "from_cset" mentioned on CPU 1 |css_set_ptr = rcu_dereference((task)->cgroups) |// Hard irq comes, current task is scheduled out.
cgroup_attach_task() |cgroup_migrate() |cgroup_migrate_execute() |css_set_move_task(task, from_cset, to_cset, true) |cgroup_move_task(task, to_cset) |rcu_assign_pointer(.., to_cset) |... |cgroup_migrate_finish() |put_css_set_locked(from_cset) |from_cset->refcount return 0 |kfree_rcu(cset, rcu_head) // means to free from_cset after new gp |add_ptr_to_bulk_krc_lock() |schedule_delayed_work(&krcp->monitor_work, ..) kfree_rcu_monitor() |krcp->bulk_head[0]'s work attached to krwp->bulk_head_free[] |queue_rcu_work(system_wq, &krwp->rcu_work) |if rwork->rcu.work is not in WORK_STRUCT_PENDING_BIT state, |call_rcu(&rwork->rcu, rcu_work_rcufn) <--- request a new gp // There is a perious call_rcu(.., rcu_work_rcufn) // gp end, rcu_work_rcufn() is called. rcu_work_rcufn() |__queue_work(.., rwork->wq, &rwork->work); |kfree_rcu_work() |krwp->bulk_head_free[0] bulk is freed before new gp end!!! |The "from_cset" is freed before new gp end.
// the task is scheduled in after many ms.
|css_set_ptr->subsys[(subsys_id) <--- Caused kernel crash, because css_set_ptr is freed.
v2: Use helper function instead of inserted code block at kfree_rcu_monitor().
--- kernel/rcu/tree.c | 27 +++++++++++++++++++-------- 1 file changed, 19 insertions(+), 8 deletions(-)
kernel/rcu/tree.c +19 -8
@@ -3024,6 +3024,18 @@ static void kfree_rcu_work(struct work_struct *work)return !!READ_ONCE (krcp->head); } +static bool +need_wait_for_krwp_work (struct kfree_rcu_cpu_work *krwp) +{ +int i; + +for (i =0 ; i <FREE_N_CHANNELS ; i++) +if (!list_empty (&krwp->bulk_head_free[i])) +return true; + +return !!krwp->head_free; +} +static int krc_count (struct kfree_rcu_cpu *krcp) {int sum =atomic_read (&krcp->head_count);
@@ -3107,15 +3119,14 @@ static void kfree_rcu_monitor(struct work_struct *work)for (i =0 ; i <KFREE_N_BATCHES ; i++) {struct kfree_rcu_cpu_work *krwp = &(krcp->krw_arr[i]); -// Try to detach bulk_head or head and attach it over any -// available corresponding free channel. It can be that -// a previous RCU batch is in progress, it means that -// immediately to queue another one is not possible so -// in that case the monitor work is rearmed. -if ((!list_empty (&krcp->bulk_head[0 ]) &&list_empty (&krwp->bulk_head_free[0 ])) || - (!list_empty (&krcp->bulk_head[1 ]) &&list_empty (&krwp->bulk_head_free[1 ])) || - (READ_ONCE (krcp->head) && !krwp->head_free)) { +// Try to detach bulk_head or head and attach it, only when +// all channels are free. Any channel is not free means at krwp +// there is on-going rcu work to handle krwp's free business. +if (need_wait_for_krwp_work (krwp)) +continue ; +// kvfree_rcu_drain_ready() might handle this krcp, if so give up. +if (need_offload_krc (krcp)) {// Channel 1 corresponds to the SLAB-pointer bulk path. // Channel 2 corresponds to vmalloc-pointer bulk path. for (j =0 ; j <FREE_N_CHANNELS ; j++) {
Signature · 1 line
1.9.1
Uladzislau Rezki
maintainerQuoted 115 lines · Ziwei Dai
In
kfree_rcu_monitor(), new free business at krcp is attached to any free channel at krwp.kfree_rcu_monitor()is responsible to make sure new free business is handled after the rcu grace period. But if there is any none-free channel at krwp already, that means there is an on-going rcu work, which will cause the kvfree_call_rcu()-triggered free business is done before the wanted rcu grace period ends.This commit ignore krwp which has non-free channel at
kfree_rcu_monitor(), to fix the issue thatkvfree_call_rcu()loses effectiveness.Below is the
css_setobj "from_cset" use-after-free case caused bykvfree_call_rcu()losing effectiveness.
CPU 0 callsrcu_read_lock(), then use "from_cset", then hard irq comes, the task is schedule out.
CPU 1 callskfree_rcu(cset, rcu_head), willing to free "from_cset" after new gp. But "from_cset" is freed right after current gp end. "from_cset" is reallocated. CPU 0 's task arrives back, references "from_cset"'s member, which causes crash.CPU 0 CPU 1
count_memcg_event_mm()
|rcu_read_lock() <---
|mem_cgroup_from_task()|// css_set_ptr is the "from_cset" mentioned on CPU 1 |css_set_ptr = rcu_dereference((task)->cgroups) |// Hard irq comes, current task is scheduled out.cgroup_attach_task() |cgroup_migrate() |cgroup_migrate_execute() |css_set_move_task(task, from_cset, to_cset, true) |cgroup_move_task(task, to_cset) |rcu_assign_pointer(.., to_cset) |... |cgroup_migrate_finish() |put_css_set_locked(from_cset) |from_cset->refcount return 0 |kfree_rcu(cset, rcu_head) // means to free from_cset after new gp |add_ptr_to_bulk_krc_lock() |schedule_delayed_work(&krcp->monitor_work, ..) kfree_rcu_monitor() |krcp->bulk_head[0]'s work attached to krwp->bulk_head_free[] |queue_rcu_work(system_wq, &krwp->rcu_work) |if rwork->rcu.work is not in WORK_STRUCT_PENDING_BIT state, |call_rcu(&rwork->rcu, rcu_work_rcufn) <--- request a new gp // There is a perious call_rcu(.., rcu_work_rcufn) // gp end, rcu_work_rcufn() is called. rcu_work_rcufn() |__queue_work(.., rwork->wq, &rwork->work); |kfree_rcu_work() |krwp->bulk_head_free[0] bulk is freed before new gp end!!! |The "from_cset" is freed before new gp end.// the task is scheduled in after many ms.
|css_set_ptr->subsys[(subsys_id) <--- Caused kernel crash, becausecss_set_ptris freed.v2: Use helper function instead of inserted code block at
kfree_rcu_monitor().Fixes c014efeef76a ("rcu: Add multiple in-flight batches of kfree_rcu() work")Signed-off-by Ziwei Dai <[email protected]>--- kernel/rcu/tree.c | 27 +++++++++++++++++++-------- 1 file changed, 19 insertions(+), 8 deletions(-)kernel/rcu/tree.c +19
-8@@ -3024,6 +3024,18 @@ static void kfree_rcu_work(struct work_struct *work)return !!READ_ONCE (krcp->head); } +static bool +need_wait_for_krwp_work (struct kfree_rcu_cpu_work *krwp) +{ +int i; + +for (i =0 ; i <FREE_N_CHANNELS ; i++) +if (!list_empty (&krwp->bulk_head_free[i])) +return true; + +return !!krwp->head_free; +} +static int krc_count (struct kfree_rcu_cpu *krcp) {int sum =atomic_read (&krcp->head_count);@@ -3107,15 +3119,14 @@ static void kfree_rcu_monitor(struct work_struct *work)for (i =0 ; i <KFREE_N_BATCHES ; i++) {struct kfree_rcu_cpu_work *krwp = &(krcp->krw_arr[i]); -// Try to detach bulk_head or head and attach it over any -// available corresponding free channel. It can be that -// a previous RCU batch is in progress, it means that -// immediately to queue another one is not possible so -// in that case the monitor work is rearmed. -if ((!list_empty (&krcp->bulk_head[0 ]) &&list_empty (&krwp->bulk_head_free[0 ])) || - (!list_empty (&krcp->bulk_head[1 ]) &&list_empty (&krwp->bulk_head_free[1 ])) || - (READ_ONCE (krcp->head) && !krwp->head_free)) { +// Try to detach bulk_head or head and attach it, only when +// all channels are free. Any channel is not free means at krwp +// there is on-going rcu work to handle krwp's free business. +if (need_wait_for_krwp_work (krwp)) +continue ; +// kvfree_rcu_drain_ready() might handle this krcp, if so give up. +if (need_offload_krc (krcp)) {// Channel 1 corresponds to the SLAB-pointer bulk path. // Channel 2 corresponds to vmalloc-pointer bulk path. for (j =0 ; j <FREE_N_CHANNELS ; j++) {Signature · 1 line
1.9.1
It looks correct to me. I will test it over weekend.
--
Uladzislau Rezki
Quoted 108 lines · Uladzislau Rezki
Quoted 107 lines · Ziwei Dai
In
kfree_rcu_monitor(), new free business at krcp is attached to any free channel at krwp.kfree_rcu_monitor()is responsible to make sure new free business is handled after the rcu grace period. But if there is any none-free channel at krwp already, that means there is an on-going rcu work, which will cause the kvfree_call_rcu()-triggered free business is done before the wanted rcu grace period ends.This commit ignore krwp which has non-free channel at
kfree_rcu_monitor(), to fix the issue thatkvfree_call_rcu()loses effectiveness.Below is the
css_setobj "from_cset" use-after-free case caused bykvfree_call_rcu()losing effectiveness.
CPU 0 callsrcu_read_lock(), then use "from_cset", then hard irq comes, the task is schedule out.
CPU 1 callskfree_rcu(cset, rcu_head), willing to free "from_cset" after new gp. But "from_cset" is freed right after current gp end. "from_cset" is reallocated. CPU 0 's task arrives back, references "from_cset"'s member, which causes crash.CPU 0 CPU 1
count_memcg_event_mm()
|rcu_read_lock() <---
|mem_cgroup_from_task()|// css_set_ptr is the "from_cset" mentioned on CPU 1 |css_set_ptr = rcu_dereference((task)->cgroups) |// Hard irq comes, current task is scheduled out.cgroup_attach_task() |cgroup_migrate() |cgroup_migrate_execute() |css_set_move_task(task, from_cset, to_cset, true) |cgroup_move_task(task, to_cset) |rcu_assign_pointer(.., to_cset) |... |cgroup_migrate_finish() |put_css_set_locked(from_cset) |from_cset->refcount return 0 |kfree_rcu(cset, rcu_head) // means to free from_cset after new gp |add_ptr_to_bulk_krc_lock() |schedule_delayed_work(&krcp->monitor_work, ..) kfree_rcu_monitor() |krcp->bulk_head[0]'s work attached to krwp->bulk_head_free[] |queue_rcu_work(system_wq, &krwp->rcu_work) |if rwork->rcu.work is not in WORK_STRUCT_PENDING_BIT state, |call_rcu(&rwork->rcu, rcu_work_rcufn) <--- request a new gp // There is a perious call_rcu(.., rcu_work_rcufn) // gp end, rcu_work_rcufn() is called. rcu_work_rcufn() |__queue_work(.., rwork->wq, &rwork->work); |kfree_rcu_work() |krwp->bulk_head_free[0] bulk is freed before new gp end!!! |The "from_cset" is freed before new gp end.// the task is scheduled in after many ms.
|css_set_ptr->subsys[(subsys_id) <--- Caused kernel crash, becausecss_set_ptris freed.v2: Use helper function instead of inserted code block at
kfree_rcu_monitor().Fixes c014efeef76a ("rcu: Add multiple in-flight batches of kfree_rcu() work")Signed-off-by Ziwei Dai <[email protected]>--- kernel/rcu/tree.c | 27 +++++++++++++++++++-------- 1 file changed, 19 insertions(+), 8 deletions(-)kernel/rcu/tree.c +17
-8@@ -3024,6 +3024,18 @@ static void kfree_rcu_work(struct work_struct *work)return !!READ_ONCE (krcp->head); } +static bool +need_wait_for_krwp_work (struct kfree_rcu_cpu_work *krwp) +{ +int i; + +for (i =0 ; i <FREE_N_CHANNELS ; i++) +if (!list_empty (&krwp->bulk_head_free[i])) +return true; + +return !!krwp->head_free; +} +static int krc_count (struct kfree_rcu_cpu *krcp) {int sum =atomic_read (&krcp->head_count);@@ -3107,15 +3119,14 @@ static void kfree_rcu_monitor(struct work_struct *work)for (i =0 ; i <KFREE_N_BATCHES ; i++) {struct kfree_rcu_cpu_work *krwp = &(krcp->krw_arr[i]); -// Try to detach bulk_head or head and attach it over any -// available corresponding free channel. It can be that -// a previous RCU batch is in progress, it means that -// immediately to queue another one is not possible so -// in that case the monitor work is rearmed. -if ((!list_empty (&krcp->bulk_head[0 ]) &&list_empty (&krwp->bulk_head_free[0 ])) || - (!list_empty (&krcp->bulk_head[1 ]) &&list_empty (&krwp->bulk_head_free[1 ])) || - (READ_ONCE (krcp->head) && !krwp->head_free)) { +// Try to detach bulk_head or head and attach it, only when +// all channels are free. Any channel is not free means at krwp +// there is on-going rcu work to handle krwp's free business. +if (need_wait_for_krwp_work (krwp)) +continue ;
Quoted 10 lines
Quoted 8 lines
kernel/rcu/tree.c +2
-0+// kvfree_rcu_drain_ready() might handle this krcp, if so give up. +if (need_offload_krc (krcp)) {// Channel 1 corresponds to the SLAB-pointer bulk path. // Channel 2 corresponds to vmalloc-pointer bulk path. for (j =0 ; j <FREE_N_CHANNELS ; j++) {Signature · 1 line
1.9.1It looks correct to me. I will test it over weekend.
--
Uladzislau Rezki
Quoted 55 lines · Ziwei Dai
In
kfree_rcu_monitor(), new free business at krcp is attached to any free channel at krwp.kfree_rcu_monitor()is responsible to make sure new free business is handled after the rcu grace period. But if there is any none-free channel at krwp already, that means there is an on-going rcu work, which will cause the kvfree_call_rcu()-triggered free business is done before the wanted rcu grace period ends.This commit ignore krwp which has non-free channel at
kfree_rcu_monitor(), to fix the issue thatkvfree_call_rcu()loses effectiveness.Below is the
css_setobj "from_cset" use-after-free case caused bykvfree_call_rcu()losing effectiveness.
CPU 0 callsrcu_read_lock(), then use "from_cset", then hard irq comes, the task is schedule out.
CPU 1 callskfree_rcu(cset, rcu_head), willing to free "from_cset" after new gp. But "from_cset" is freed right after current gp end. "from_cset" is reallocated. CPU 0 's task arrives back, references "from_cset"'s member, which causes crash.CPU 0 CPU 1
count_memcg_event_mm()
|rcu_read_lock() <---
|mem_cgroup_from_task()|// css_set_ptr is the "from_cset" mentioned on CPU 1 |css_set_ptr = rcu_dereference((task)->cgroups) |// Hard irq comes, current task is scheduled out.cgroup_attach_task() |cgroup_migrate() |cgroup_migrate_execute() |css_set_move_task(task, from_cset, to_cset, true) |cgroup_move_task(task, to_cset) |rcu_assign_pointer(.., to_cset) |... |cgroup_migrate_finish() |put_css_set_locked(from_cset) |from_cset->refcount return 0 |kfree_rcu(cset, rcu_head) // means to free from_cset after new gp |add_ptr_to_bulk_krc_lock() |schedule_delayed_work(&krcp->monitor_work, ..) kfree_rcu_monitor() |krcp->bulk_head[0]'s work attached to krwp->bulk_head_free[] |queue_rcu_work(system_wq, &krwp->rcu_work) |if rwork->rcu.work is not in WORK_STRUCT_PENDING_BIT state, |call_rcu(&rwork->rcu, rcu_work_rcufn) <--- request a new gp // There is a perious call_rcu(.., rcu_work_rcufn) // gp end, rcu_work_rcufn() is called. rcu_work_rcufn() |__queue_work(.., rwork->wq, &rwork->work); |kfree_rcu_work() |krwp->bulk_head_free[0] bulk is freed before new gp end!!! |The "from_cset" is freed before new gp end.
Quoted 3 lines
// the task is scheduled in after many ms.
|css_set_ptr->subsys[(subsys_id) <--- Caused kernel crash, becausecss_set_ptris freed.
I too have observed this issue on 5.15, where i see below callstack. And i see different view of memory and one is this callstack and the other is, i see proper value of memcg from the ramdump.
Looks to be different view of memory between rcu critical section.
119653.424434][T730913] binder: 30858:30913 ioctl c0306201 7633b56a78 returned -14
[119653.426095][T730918] Unable to handle kernel paging request at virtual address 000000034088e7be
-000|mem_cgroup_disabled(inline)
-000|__count_memcg_events([X0] memcg = 0x000000034088D9AE, [X1] idx = PGFAULT = 0x11, [X2] count = 0x1)
| [X0] memcg = 0x000000034088D9AE
| [X1] idx = PGFAULT = 0x11
| [X2] count = 0x1
| [locdesc] __ptr = 0x0
-001|arch_local_irq_restore(inline)
| [X26] flags = 0x0
-001|count_memcg_events(inline)
| [X26] flags = 0x0
-001|count_memcg_event_mm(inline)
| [locdesc] idx = PGFAULT = 0x11
-001|do_handle_mm_fault([X24] vma = 0xFFFFFF89CC704870, [X20] address = 0x000000763395AA78, [X22] flags =
| [X24] vma = 0xFFFFFF89CC704870
| [X20] address = 0x000000763395AA78
| [X22] flags = 0x0215
| [X23] seq = 0x0
| [X19] regs = 0xFFFFFFC05EA73AD0
| [locdesc] ret = 0x0
-002|handle_mm_fault(inline)
| [X22] address = 0x000000763395AA78
| [X24] flags = 0x0215
| [X21] regs = 0xFFFFFFC05EA73AD0
-002|__do_page_fault(inline)
| [X23] mm = 0xFFFFFF8812F65400
| [X22] addr = 0x000000763395AA78
| [X24] mm_flags = 0x0215
| [X27] vm_flags = 0x2
| [X21] regs = 0xFFFFFFC05EA73AD0
[119653.427011][T730918] __count_memcg_events+0x2c/0x274
[119653.427014][T730918] do_handle_mm_fault+0xe4/0x2c8
[119653.427017][T730918] do_page_fault+0x4c0/0x688
[119653.427022][T730918] do_translation_fault+0x48/0x64
[119653.427025][T730918] do_mem_abort+0x68/0x148
[119653.427028][T730918] el1_abort+0x40/0x64
[119653.427032][T730918] el1h_64_sync_handler+0x60/0xa0
[119653.427035][T730918] el1h_64_sync+0x7c/0x80
[119653.427037][T730918] __arch_copy_to_user+0x60/0x224
[119653.427040][T730918] binder_ioctl_write_read+0x28c/0x590
[119653.427046][T730918] binder_ioctl+0x1b0/0xf00
[119653.427049][T730918] __arm64_sys_ioctl+0x184/0x210
[119653.427052][T730918] invoke_syscall+0x60/0x150
[119653.427055][T730918] el0_svc_common+0x8c/0xf8
[119653.427057][T730918] do_el0_svc+0x28/0xa0
[119653.427059][T730918] el0_svc+0x24/0x84
[119653.427061][T730918] el0t_64_sync_handler+0x88/0xec
[119653.427064][T730918] el0t_64_sync+0x1b4/0x1b8
[119653.427067][T730918] Code: a9044ff4 d503201f 71016c3f 54000d42 (f9470808)
[119653.427069][T730918] ---[ end trace a3882cb531ca3dd0 ]---I will try to test this patch.
Thanks,
--Mukesh
Quoted 54 lines
v2: Use helper function instead of inserted code block at
kfree_rcu_monitor().Fixes c014efeef76a ("rcu: Add multiple in-flight batches of kfree_rcu() work")Signed-off-by Ziwei Dai <[email protected]>--- kernel/rcu/tree.c | 27 +++++++++++++++++++-------- 1 file changed, 19 insertions(+), 8 deletions(-)kernel/rcu/tree.c +19
-8@@ -3024,6 +3024,18 @@ static void kfree_rcu_work(struct work_struct *work)return !!READ_ONCE (krcp->head); } +static bool +need_wait_for_krwp_work (struct kfree_rcu_cpu_work *krwp) +{ +int i; + +for (i =0 ; i <FREE_N_CHANNELS ; i++) +if (!list_empty (&krwp->bulk_head_free[i])) +return true; + +return !!krwp->head_free; +} +static int krc_count (struct kfree_rcu_cpu *krcp) {int sum =atomic_read (&krcp->head_count);@@ -3107,15 +3119,14 @@ static void kfree_rcu_monitor(struct work_struct *work)for (i =0 ; i <KFREE_N_BATCHES ; i++) {struct kfree_rcu_cpu_work *krwp = &(krcp->krw_arr[i]); -// Try to detach bulk_head or head and attach it over any -// available corresponding free channel. It can be that -// a previous RCU batch is in progress, it means that -// immediately to queue another one is not possible so -// in that case the monitor work is rearmed. -if ((!list_empty (&krcp->bulk_head[0 ]) &&list_empty (&krwp->bulk_head_free[0 ])) || - (!list_empty (&krcp->bulk_head[1 ]) &&list_empty (&krwp->bulk_head_free[1 ])) || - (READ_ONCE (krcp->head) && !krwp->head_free)) { +// Try to detach bulk_head or head and attach it, only when +// all channels are free. Any channel is not free means at krwp +// there is on-going rcu work to handle krwp's free business. +if (need_wait_for_krwp_work (krwp)) +continue ; +// kvfree_rcu_drain_ready() might handle this krcp, if so give up. +if (need_offload_krc (krcp)) {// Channel 1 corresponds to the SLAB-pointer bulk path. // Channel 2 corresponds to vmalloc-pointer bulk path. for (j =0 ; j <FREE_N_CHANNELS ; j++) {
Quoted 55 lines · Ziwei Dai
In
kfree_rcu_monitor(), new free business at krcp is attached to any free channel at krwp.kfree_rcu_monitor()is responsible to make sure new free business is handled after the rcu grace period. But if there is any none-free channel at krwp already, that means there is an on-going rcu work, which will cause the kvfree_call_rcu()-triggered free business is done before the wanted rcu grace period ends.This commit ignore krwp which has non-free channel at
kfree_rcu_monitor(), to fix the issue thatkvfree_call_rcu()loses effectiveness.Below is the
css_setobj "from_cset" use-after-free case caused bykvfree_call_rcu()losing effectiveness.
CPU 0 callsrcu_read_lock(), then use "from_cset", then hard irq comes, the task is schedule out.
CPU 1 callskfree_rcu(cset, rcu_head), willing to free "from_cset" after new gp. But "from_cset" is freed right after current gp end. "from_cset" is reallocated. CPU 0 's task arrives back, references "from_cset"'s member, which causes crash.CPU 0 CPU 1
count_memcg_event_mm()
|rcu_read_lock() <---
|mem_cgroup_from_task()|// css_set_ptr is the "from_cset" mentioned on CPU 1 |css_set_ptr = rcu_dereference((task)->cgroups) |// Hard irq comes, current task is scheduled out.cgroup_attach_task() |cgroup_migrate() |cgroup_migrate_execute() |css_set_move_task(task, from_cset, to_cset, true) |cgroup_move_task(task, to_cset) |rcu_assign_pointer(.., to_cset) |... |cgroup_migrate_finish() |put_css_set_locked(from_cset) |from_cset->refcount return 0 |kfree_rcu(cset, rcu_head) // means to free from_cset after new gp |add_ptr_to_bulk_krc_lock() |schedule_delayed_work(&krcp->monitor_work, ..) kfree_rcu_monitor() |krcp->bulk_head[0]'s work attached to krwp->bulk_head_free[] |queue_rcu_work(system_wq, &krwp->rcu_work) |if rwork->rcu.work is not in WORK_STRUCT_PENDING_BIT state, |call_rcu(&rwork->rcu, rcu_work_rcufn) <--- request a new gp // There is a perious call_rcu(.., rcu_work_rcufn) // gp end, rcu_work_rcufn() is called. rcu_work_rcufn() |__queue_work(.., rwork->wq, &rwork->work); |kfree_rcu_work() |krwp->bulk_head_free[0] bulk is freed before new gp end!!! |The "from_cset" is freed before new gp end.
Quoted 7 lines
// the task is scheduled in after many ms.
|css_set_ptr->subsys[(subsys_id) <--- Caused kernel crash, becausecss_set_ptris freed.v2: Use helper function instead of inserted code block at
kfree_rcu_monitor().Fixes c014efeef76a ("rcu: Add multiple in-flight batches of kfree_rcu() work")Signed-off-by Ziwei Dai <[email protected]>
Good catch, thank you!!!
How difficult was this to trigger? If it can be triggered easily, this of course needs to go into mainline sooner rather than later.
Longer term, would it make sense to run the three channels through RCU separately, in order to avoid one channel refraining from starting a grace period just because some other channel has callbacks waiting for a grace period to complete? One argument against might be energy efficiency, but perhaps the ->gp_snap field could be used to get the best of both worlds.
Either way, this fixes only one bug of two. The second bug is in the kfree_rcu() tests, which should have caught this bug. Thoughts on a good fix for those tests?
I have applied Uladzislau's and Mukesh's tags, and done the usual wordsmithing as shown at the end of this message. Please let me know if I messed anything up.
Quoted 12 lines
--- kernel/rcu/tree.c | 27 +++++++++++++++++++-------- 1 file changed, 19 insertions(+), 8 deletions(-)kernel/rcu/tree.c +0
-0@@ -3024,6 +3024,18 @@ static void kfree_rcu_work(struct work_struct *work)return !!READ_ONCE (krcp->head); }
kernel/rcu/tree.c static void kfree_rcu_work(struct work_struct *work) · line 3036
Quoted 10 lines
kernel/rcu/tree.c +10
-0+static bool +need_wait_for_krwp_work (struct kfree_rcu_cpu_work *krwp) +{ +int i; + +for (i =0 ; i <FREE_N_CHANNELS ; i++) +if (!list_empty (&krwp->bulk_head_free[i])) +return true; + +return !!krwp->head_free;
This is fixed from v1, good!
Quoted 30 lines
kernel/rcu/tree.c +9
-8+} +static int krc_count (struct kfree_rcu_cpu *krcp) {int sum =atomic_read (&krcp->head_count);@@ -3107,15 +3119,14 @@ static void kfree_rcu_monitor(struct work_struct *work)for (i =0 ; i <KFREE_N_BATCHES ; i++) {struct kfree_rcu_cpu_work *krwp = &(krcp->krw_arr[i]); -// Try to detach bulk_head or head and attach it over any -// available corresponding free channel. It can be that -// a previous RCU batch is in progress, it means that -// immediately to queue another one is not possible so -// in that case the monitor work is rearmed. -if ((!list_empty (&krcp->bulk_head[0 ]) &&list_empty (&krwp->bulk_head_free[0 ])) || - (!list_empty (&krcp->bulk_head[1 ]) &&list_empty (&krwp->bulk_head_free[1 ])) || - (READ_ONCE (krcp->head) && !krwp->head_free)) { +// Try to detach bulk_head or head and attach it, only when +// all channels are free. Any channel is not free means at krwp +// there is on-going rcu work to handle krwp's free business. +if (need_wait_for_krwp_work (krwp)) +continue ; +// kvfree_rcu_drain_ready() might handle this krcp, if so give up. +if (need_offload_krc (krcp)) {// Channel 1 corresponds to the SLAB-pointer bulk path. // Channel 2 corresponds to vmalloc-pointer bulk path. for (j =0 ; j <FREE_N_CHANNELS ; j++) {Signature · 1 line
1.9.1
------------------------------------------------------------------------
commit e222f9a512539c3f4093a55d16624d9da614800b
Author: Ziwei Dai <[email protected]> Date: Fri Mar 31 20:42:09 2023 +0800
rcu: Avoid freeing new kfree_rcu() memory after old grace period
Memory passed to kvfree_rcu() that is to be freed is tracked by a per-CPU kfree_rcu_cpu structure, which in turn contains pointers to kvfree_rcu_bulk_data structures that contain pointers to memory that has not yet been handed to RCU, along with an kfree_rcu_cpu_work structure that tracks the memory that has already been handed to RCU. These structures track three categories of memory: (1) Memory for kfree(), (2) Memory for kvfree(), and (3) Memory for both that arrived during an OOM episode. The first two categories are tracked in a cache-friendly manner involving a dynamically allocated page of pointers (the aforementioned kvfree_rcu_bulk_data structures), while the third uses a simple (but decidedly cache-unfriendly) linked list through the rcu_head structures in each block of memory.
On a given CPU, these three categories are handled as a unit, with that CPU's kfree_rcu_cpu_work structure having one pointer for each of the three categories. Clearly, new memory for a given category cannot be placed in the corresponding kfree_rcu_cpu_work structure until any old memory has had its grace period elapse and thus has been removed. And the kfree_rcu_monitor() function does in fact check for this.
Except that the kfree_rcu_monitor() function checks these pointers one at a time. This means that if the previous kfree_rcu() memory passed to RCU had only category 1 and the current one has only category 2, the kfree_rcu_monitor() function will send that current category-2 memory along immediately. This can result in memory being freed too soon, that is, out from under unsuspecting RCU readers.
To see this, consider the following sequence of events, in which:
o Task A on CPU 0 calls rcu_read_lock(), then uses "from_cset",
then is preempted.
o CPU 1 calls kfree_rcu(cset, rcu_head) in order to free "from_cset"
after a later grace period. Except that "from_cset" is freed right after the previous grace period ended, so that "from_cset" is immediately freed. Task A resumes and references "from_cset"'s member, after which nothing good happens.
In full detail:
CPU 0 CPU 1
---------------------- ----------------------
count_memcg_event_mm()
|rcu_read_lock() <--- |mem_cgroup_from_task()
|// css_set_ptr is the "from_cset" mentioned on CPU 1 |css_set_ptr = rcu_dereference((task)->cgroups) |// Hard irq comes, current task is scheduled out.
cgroup_attach_task()
|cgroup_migrate() |cgroup_migrate_execute() |css_set_move_task(task, from_cset, to_cset, true) |cgroup_move_task(task, to_cset) |rcu_assign_pointer(.., to_cset)
|...
|cgroup_migrate_finish() |put_css_set_locked(from_cset) |from_cset->refcount return 0 |kfree_rcu(cset, rcu_head) // free from_cset after new gp |add_ptr_to_bulk_krc_lock() |schedule_delayed_work(&krcp->monitor_work, ..)
kfree_rcu_monitor()
|krcp->bulk_head[0]'s work attached to krwp->bulk_head_free[] |queue_rcu_work(system_wq, &krwp->rcu_work) |if rwork->rcu.work is not in WORK_STRUCT_PENDING_BIT state, |call_rcu(&rwork->rcu, rcu_work_rcufn) <--- request new gp
// There is a perious call_rcu(.., rcu_work_rcufn) // gp end, rcu_work_rcufn() is called. rcu_work_rcufn()
|__queue_work(.., rwork->wq, &rwork->work);
|kfree_rcu_work()
|krwp->bulk_head_free[0] bulk is freed before new gp end!!! |The "from_cset" is freed before new gp end.
// the task resumes some time later.
|css_set_ptr->subsys[(subsys_id) <--- Caused kernel crash, because css_set_ptr is freed.
This commit therefore causes kfree_rcu_monitor() to refrain from moving kfree_rcu() memory to the kfree_rcu_cpu_work structure until the RCU grace period has completed for all three categories.
v2: Use helper function instead of inserted code block at kfree_rcu_monitor().
kernel/rcu/tree.c +19 -8
@@ -3051,6 +3051,18 @@ need_offload_krc(struct kfree_rcu_cpu *krcp)return !!READ_ONCE (krcp->head); } +static bool +need_wait_for_krwp_work (struct kfree_rcu_cpu_work *krwp) +{ +int i; + +for (i =0 ; i <FREE_N_CHANNELS ; i++) +if (!list_empty (&krwp->bulk_head_free[i])) +return true; + +return !!krwp->head_free; +} +static int krc_count (struct kfree_rcu_cpu *krcp) {int sum =atomic_read (&krcp->head_count);
@@ -3134,15 +3146,14 @@ static void kfree_rcu_monitor(struct work_struct *work)for (i =0 ; i <KFREE_N_BATCHES ; i++) {struct kfree_rcu_cpu_work *krwp = &(krcp->krw_arr[i]); -// Try to detach bulk_head or head and attach it over any -// available corresponding free channel. It can be that -// a previous RCU batch is in progress, it means that -// immediately to queue another one is not possible so -// in that case the monitor work is rearmed. -if ((!list_empty (&krcp->bulk_head[0 ]) &&list_empty (&krwp->bulk_head_free[0 ])) || - (!list_empty (&krcp->bulk_head[1 ]) &&list_empty (&krwp->bulk_head_free[1 ])) || - (READ_ONCE (krcp->head) && !krwp->head_free)) { +// Try to detach bulk_head or head and attach it, only when +// all channels are free. Any channel is not free means at krwp +// there is on-going rcu work to handle krwp's free business. +if (need_wait_for_krwp_work (krwp)) +continue ; +// kvfree_rcu_drain_ready() might handle this krcp, if so give up. +if (need_offload_krc (krcp)) {// Channel 1 corresponds to the SLAB-pointer bulk path. // Channel 2 corresponds to vmalloc-pointer bulk path. for (j =0 ; j <FREE_N_CHANNELS ; j++) {
Hello Paul!
Quoted 106 lines
-----邮件原件-----
发件人: Paul E. McKenney <[email protected]>
发送时间: 2023年4月4日 6:58
收件人: 代子为 (Ziwei Dai) <[email protected]>
抄送: [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; 王双 (Shuang Wang) <[email protected]>; 辛依凡 (Yifan Xin) <[email protected]>; 王科 (Ke Wang) <[email protected]>; 闫学文 (Xuewen Yan) <[email protected]>; 牛志国 (Zhiguo Niu) <[email protected]>; 黄朝阳 (Zhaoyang Huang) <[email protected]>
主题: Re: [PATCH V2] rcu: Make sure new krcp free business is handled after the wanted rcu grace period.注意: 这封邮件来自于外部。除非你确定邮件内容安全,否则不要点击任
何链接和附件。
CAUTION: This email originated from outside of the organization. Do not click links or open attachments unless you recognize the sender and know the content is safe.Quoted 9 lines · Ziwei Dai
In
kfree_rcu_monitor(), new free business at krcp is attached to any free channel at krwp.kfree_rcu_monitor()is responsible to make sure new free business is handled after the rcu grace period. But if there is any none-free channel at krwp already, that means there is an on-going rcu work, which will cause the kvfree_call_rcu()-triggered free business is done before the wanted rcu grace period ends.This commit ignore krwp which has non-free channel at
kfree_rcu_monitor(), to fix the issue thatkvfree_call_rcu()loseseffectiveness.
Quoted 6 lines
Below is the
css_setobj "from_cset" use-after-free case caused bykvfree_call_rcu()losing effectiveness.
CPU 0 callsrcu_read_lock(), then use "from_cset", then hard irq comes, the task is schedule out.
CPU 1 callskfree_rcu(cset, rcu_head), willing to free "from_cset" after newgp.
Quoted 1 line
But "
from_cset" is freed right after current gp end. "from_cset" isreallocated.
Quoted 1 line
CPU 0 's task arrives back, references "from_cset"'s member, which causes
crash.
Quoted 5 lines
CPU 0 CPU 1
count_memcg_event_mm()
|rcu_read_lock() <---
|mem_cgroup_from_task()Quoted 8 lines
|// css_set_ptr is the "from_cset" mentioned on CPU 1 |css_set_ptr = rcu_dereference((task)->cgroups) |// Hard irq comes, current task isscheduled out.
cgroup_attach_task()|cgroup_migrate() |cgroup_migrate_execute() |css_set_move_task(task,
from_cset,to_cset, true)Quoted 1 line
|cgroup_move_task(task,
to_cset)Quoted 4 lines
|rcu_assign_pointer(.., to_cset) |... |cgroup_migrate_finish()|put_css_set_locked(from_cset)
Quoted 2 lines
|from_cset->refcount return 0 |kfree_rcu(cset, rcu_head) //means to free
from_csetafter new gpQuoted 6 lines
|add_ptr_to_bulk_krc_lock()|schedule_delayed_work(&krcp->monitor_work, ..)
kfree_rcu_monitor() |krcp->bulk_head[0]'s workattached to krwp->bulk_head_free[]
Quoted 1 line
|queue_rcu_work(system_wq,
&krwp->rcu_work)Quoted 1 line
|if rwork->rcu.work is not in
WORK_STRUCT_PENDING_BITstate,Quoted 4 lines
|call_rcu(&rwork->rcu,
rcu_work_rcufn) <--- request a new gp// There is a perious call_rcu(..,
rcu_work_rcufn)Quoted 1 line
// gp end, rcu_work_rcufn() iscalled.
Quoted 6 lines
rcu_work_rcufn() |__queue_work(.., rwork->wq,
&rwork->work);|kfree_rcu_work() |krwp->bulk_head_free[0] bulkis freed before new gp end!!!
Quoted 1 line
|The "
from_cset" is freedbefore new gp end.
Quoted 3 lines
// the task is scheduled in after many ms.
|css_set_ptr->subsys[(subsys_id) <--- Caused kernel crash, because
css_set_ptris freed.Quoted 2 lines
v2: Use helper function instead of inserted code block at
kfree_rcu_monitor().Quoted 1 line
Quoted 8 lines
Quoted 3 lines
Fixes c014efeef76a ("rcu: Add multiple in-flight batches of
kfree_rcu()work")Signed-off-by Ziwei Dai <[email protected]>Good catch, thank you!!!
How difficult was this to trigger? If it can be triggered easily, this of course needs to go into mainline sooner rather than later.
Roughly we can reproduce this issue within two rounds of 48h stress test, with 20 k5.15 devices. If KASAN is enabled, the reproduce rate is higher. So I think sooner is better.
Quoted 6 lines
Longer term, would it make sense to run the three channels through RCU separately, in order to avoid one channel refraining from starting a grace period just because some other channel has callbacks waiting for a grace period to complete? One argument against might be energy efficiency, but perhaps the ->gp_snap field could be used to get the best of both worlds.
I see kvfree_rcu_drain_ready(krcp) is already called at the beginning of kfree_rcu_monitor(), which polls the ->gp_snap field, to decide whether to free channel objects immediately or after gp. Both energy efficiency and timing seems be considered?
Quoted 3 lines
Either way, this fixes only one bug of two. The second bug is in the
kfree_rcu()tests, which should have caught this bug. Thoughts on a good fix for those tests?
I inserted a msleep() between "rcu_read_lock(), get pointer via rcu_dereference()" and "reference pointer, using the member", at the rcu scenario, then we can reproduce this issue very soon in stress test. Can kfree_rcu() tests insert msleep()?
Quoted 3 lines
I have applied Uladzislau's and Mukesh's tags, and done the usual wordsmithing as shown at the end of this message. Please let me know if I messed anything up.
Thank you for the improvement on the patch! It seems better now.
Quoted 244 lines
kernel/rcu/tree.c static void kfree_rcu_work(struct work_struct
Quoted 9 lines
--- kernel/rcu/tree.c | 27 +++++++++++++++++++-------- 1 file changed, 19 insertions(+), 8 deletions(-)kernel/rcu/tree.c index renamed from kernel/rcu/tree.c +0
-08e880c0..7b95ee9 100644
kernel/rcu/tree.c +0
-0@@ -3024,6 +3024,18 @@ static void kfree_rcu_work(struct work_struct*work)
kernel/rcu/tree.c need_offload_krc(struct kfree_rcu_cpu *krcp) · line 3063
Quoted 12 lines
kernel/rcu/tree.c +9
-0return !!READ_ONCE (krcp->head); } +static bool +need_wait_for_krwp_work (struct kfree_rcu_cpu_work *krwp) { +int i; + +for (i =0 ; i <FREE_N_CHANNELS ; i++) +if (!list_empty (&krwp->bulk_head_free[i])) +return true; + +return !!krwp->head_free;This is fixed from v1, good!
kernel/rcu/tree.c need_offload_krc(struct kfree_rcu_cpu *krcp)
Quoted 4 lines
kernel/rcu/tree.c +2
-0+} +static int krc_count (struct kfree_rcu_cpu *krcp) { int sum = atomic_read(&krcp->head_count); @@ -3107,15+3119,14
kernel/rcu/tree.c static void kfree_rcu_monitor(struct work_struct *work)
Quoted 10 lines
kernel/rcu/tree.c +0
-6@@ static void kfree_rcu_monitor(struct work_struct *work) for (i = 0; i < KFREE_N_BATCHES; i++) {struct kfree_rcu_cpu_work *krwp = &(krcp->krw_arr[i]); -// Try to detach bulk_head or head and attach it over any -// available corresponding free channel. It can be that -// a previous RCU batch is in progress, it means that -// immediately to queue another one is not possible so -// in that case the monitor work is rearmed. - if ((!list_empty(&krcp->bulk_head[0]) &&
list_empty(&krwp->bulk_head_free[0])) ||Quoted 1 line
- (!list_empty(&krcp->bulk_head[1]) &&
list_empty(&krwp->bulk_head_free[1])) ||Quoted 1 line
- (
READ_ONCE(krcp->head)&& !krwp->head_free)) {
kernel/rcu/tree.c
Quoted 2 lines
kernel/rcu/tree.c +2
-0+// Try to detach bulk_head or head and attach it, only when +// all channels are free. Any channel is not free means at krwp
Quoted 1 line
+ // there is on-going rcu work to handle krwp's free
business.
kernel/rcu/tree.c static void kfree_rcu_monitor(struct work_struct *work)
Quoted 4 lines
kernel/rcu/tree.c +3
-0+if (need_wait_for_krwp_work (krwp)) +continue ; +// kvfree_rcu_drain_ready() might handle this krcp, if so give up.
Quoted 2 lines
+ if (need_offload_krc(krcp)) { // Channel 1 corresponds to the SLAB-pointerbulk path.
Quoted 1 line
// Channel 2 corresponds to vmalloc-pointer bulkpath.
Quoted 3 lines
for (j = 0; j < FREE_N_CHANNELS; j++) {--
1.9.1------------------------------------------------------------------------
commit
e222f9a512539c3f4093a55d16624d9da614800bAuthor: Ziwei Dai <[email protected]> Date: Fri Mar 31 20:42:09 2023 +0800rcu: Avoid freeing new
kfree_rcu()memory after old grace periodMemory passed to
kvfree_rcu()that is to be freed is tracked by a per-CPUkfree_rcu_cpustructure, which in turn contains pointers tokvfree_rcu_bulk_datastructures that contain pointers to memory that has not yet been handed to RCU, along with ankfree_rcu_cpu_workstructure that tracks the memory that has already been handed to RCU. These structures track three categories of memory: (1) Memory forkfree(), (2) Memory forkvfree(), and (3) Memory for both that arrived during an OOM episode. The first two categories are tracked in a cache-friendly manner involving a dynamically allocated page of pointers (the aforementionedkvfree_rcu_bulk_datastructures), while the third uses a simple (but decidedly cache-unfriendly) linked list through thercu_headstructures in each block of memory.On a given CPU, these three categories are handled as a unit, with that CPU's
kfree_rcu_cpu_workstructure having one pointer for each of the three categories. Clearly, new memory for a given category cannot be placed in the correspondingkfree_rcu_cpu_workstructure until any old memory has had its grace period elapse and thus has been removed.
And
thekfree_rcu_monitor()function does in fact check for this.Except that the
kfree_rcu_monitor()function checks these pointers one at a time. This means that if the previouskfree_rcu()memory passed to RCU had only category 1 and the current one has only category 2, thekfree_rcu_monitor()function will send that current category-2 memory along immediately. This can result in memory being freed too soon, that is, out from under unsuspecting RCU readers.To see this, consider the following sequence of events, in which:
o Task A on CPU 0 calls
rcu_read_lock(), then uses "from_cset",
then is preempted.o CPU 1 calls
kfree_rcu(cset, rcu_head)in order to free
"from_cset"
after a later grace period. Except that "from_cset" is freed right after the previous grace period ended, so that
"from_cset"
is immediately freed. Task A resumes and references
"from_cset"'s
member, after which nothing good happens.In full detail:
CPU 0 CPU 1
---------------------- ----------------------
count_memcg_event_mm()
|rcu_read_lock() <--- |mem_cgroup_from_task()
|//css_set_ptris the "from_cset" mentioned on CPU 1 |css_set_ptr = rcu_dereference((task)->cgroups) |// Hard irq comes, current task is scheduled out.
cgroup_attach_task()|cgroup_migrate()|cgroup_migrate_execute()
|css_set_move_task(task,from_cset,to_cset, true)
|cgroup_move_task(task,to_cset)|rcu_assign_pointer(.., to_cset) |... |cgroup_migrate_finish()|put_css_set_locked(from_cset)
|from_cset->refcountreturn 0
|kfree_rcu(cset,rcu_head)
// freefrom_csetafter new gp|add_ptr_to_bulk_krc_lock()
|schedule_delayed_work(&krcp->monitor_work, ..)
kfree_rcu_monitor() |krcp->bulk_head[0]'swork attached to krwp->bulk_head_free[]
|queue_rcu_work(system_wq,
&krwp->rcu_work)|if rwork->rcu.work is not in WORK_STRUCT_PENDING_BIT state, |call_rcu(&rwork->rcu,
rcu_work_rcufn) <--- request new gp// There is a perious call_rcu(.., rcu_work_rcufn) // gp end, rcu_work_rcufn() is called. rcu_work_rcufn() |__queue_work(..,
rwork->wq,&rwork->work);|kfree_rcu_work() |krwp->bulk_head_free[0]bulk is freed before new gp end!!!
|The "from_cset" is freed
before new gp end.// the task resumes some time later. |css_set_ptr->subsys[(subsys_id) <--- Caused kernel crash, because
css_set_ptris freed.This commit therefore causes
kfree_rcu_monitor()to refrain from
moving
kfree_rcu()memory to thekfree_rcu_cpu_workstructure until the RCU grace period has completed for all three categories.v2: Use helper function instead of inserted code block at
kfree_rcu_monitor().Fixes c014efeef76a ("rcu: Add multiple in-flight batches of kfree_rcu()work")
Reported-by Mukesh Ojha <[email protected]>Signed-off-by Ziwei Dai <[email protected]>Reviewed-by Uladzislau Rezki (Sony) <[email protected]>Signed-off-by Paul E. McKenney <[email protected]>kernel/rcu/tree.c index renamed from kernel/rcu/tree.c +0
-0
859ee02f6614..e2dbea6cee4b100644kernel/rcu/tree.c +12
-0@@ -3051,6 +3051,18 @@ need_offload_krc(struct kfree_rcu_cpu *krcp)return !!READ_ONCE (krcp->head); } +static bool +need_wait_for_krwp_work (struct kfree_rcu_cpu_work *krwp) { +int i; + +for (i =0 ; i <FREE_N_CHANNELS ; i++) +if (!list_empty (&krwp->bulk_head_free[i])) +return true; + +return !!krwp->head_free; +} +static int krc_count (struct kfree_rcu_cpu *krcp) { int sum = atomic_read(&krcp->head_count); @@ -3134,15 +3146,14 @@ static void kfree_rcu_monitor(struct work_struct *work) for (i = 0; i < KFREE_N_BATCHES; i++) {struct kfree_rcu_cpu_work *krwp = &(krcp->krw_arr[i]);kernel/rcu/tree.c +0
-6-// Try to detach bulk_head or head and attach it over any -// available corresponding free channel. It can be that -// a previous RCU batch is in progress, it means that -// immediately to queue another one is not possible so -// in that case the monitor work is rearmed. - if ((!list_empty(&krcp->bulk_head[0]) &&
list_empty(&krwp->bulk_head_free[0])) ||
- (!list_empty(&krcp->bulk_head[1]) &&list_empty(&krwp->bulk_head_free[1])) ||
- (READ_ONCE(krcp->head)
&& !krwp->head_free)) {
+ // Try to detachbulk_heador head and attach it, only
when
+ // all channels are free. Any channel is not free means at
krwp
+ // there is on-going rcu work to handle krwp's free
business.kernel/rcu/tree.c +3
-0+if (need_wait_for_krwp_work (krwp)) +continue ; +// kvfree_rcu_drain_ready() might handle this krcp, if so give up.
+ if (need_offload_krc(krcp)) { // Channel 1 corresponds to the SLAB-pointer bulk path. // Channel 2 corresponds to vmalloc-pointer bulk path. for (j = 0; j < FREE_N_CHANNELS; j++) {
Paul E. McKenney
maintainerQuoted 112 lines · 代子为 (Ziwei Dai)
Hello Paul!
Quoted 110 lines
-----邮件原件-----
发件人: Paul E. McKenney <[email protected]>
发送时间: 2023年4月4日 6:58
收件人: 代子为 (Ziwei Dai) <[email protected]>
抄送: [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; 王双 (Shuang Wang) <[email protected]>; 辛依凡 (Yifan Xin) <[email protected]>; 王科 (Ke Wang) <[email protected]>; 闫学文 (Xuewen Yan) <[email protected]>; 牛志国 (Zhiguo Niu) <[email protected]>; 黄朝阳 (Zhaoyang Huang) <[email protected]>
主题: Re: [PATCH V2] rcu: Make sure new krcp free business is handled after the wanted rcu grace period.注意: 这封邮件来自于外部。除非你确定邮件内容安全,否则不要点击任
何链接和附件。
CAUTION: This email originated from outside of the organization. Do not click links or open attachments unless you recognize the sender and know the content is safe.Quoted 9 lines · Ziwei Dai
In
kfree_rcu_monitor(), new free business at krcp is attached to any free channel at krwp.kfree_rcu_monitor()is responsible to make sure new free business is handled after the rcu grace period. But if there is any none-free channel at krwp already, that means there is an on-going rcu work, which will cause the kvfree_call_rcu()-triggered free business is done before the wanted rcu grace period ends.This commit ignore krwp which has non-free channel at
kfree_rcu_monitor(), to fix the issue thatkvfree_call_rcu()loseseffectiveness.
Quoted 6 lines
Below is the
css_setobj "from_cset" use-after-free case caused bykvfree_call_rcu()losing effectiveness.
CPU 0 callsrcu_read_lock(), then use "from_cset", then hard irq comes, the task is schedule out.
CPU 1 callskfree_rcu(cset, rcu_head), willing to free "from_cset" after newgp.
Quoted 1 line
But "
from_cset" is freed right after current gp end. "from_cset" isreallocated.
Quoted 1 line
CPU 0 's task arrives back, references "from_cset"'s member, which causes
crash.
Quoted 5 lines
CPU 0 CPU 1
count_memcg_event_mm()
|rcu_read_lock() <---
|mem_cgroup_from_task()Quoted 8 lines
|// css_set_ptr is the "from_cset" mentioned on CPU 1 |css_set_ptr = rcu_dereference((task)->cgroups) |// Hard irq comes, current task isscheduled out.
cgroup_attach_task()|cgroup_migrate() |cgroup_migrate_execute() |css_set_move_task(task,
from_cset,to_cset, true)Quoted 1 line
|cgroup_move_task(task,
to_cset)Quoted 4 lines
|rcu_assign_pointer(.., to_cset) |... |cgroup_migrate_finish()|put_css_set_locked(from_cset)
Quoted 2 lines
|from_cset->refcount return 0 |kfree_rcu(cset, rcu_head) //means to free
from_csetafter new gpQuoted 6 lines
|add_ptr_to_bulk_krc_lock()|schedule_delayed_work(&krcp->monitor_work, ..)
kfree_rcu_monitor() |krcp->bulk_head[0]'s workattached to krwp->bulk_head_free[]
Quoted 1 line
|queue_rcu_work(system_wq,
&krwp->rcu_work)Quoted 1 line
|if rwork->rcu.work is not in
WORK_STRUCT_PENDING_BITstate,Quoted 4 lines
|call_rcu(&rwork->rcu,
rcu_work_rcufn) <--- request a new gp// There is a perious call_rcu(..,
rcu_work_rcufn)Quoted 1 line
// gp end, rcu_work_rcufn() iscalled.
Quoted 6 lines
rcu_work_rcufn() |__queue_work(.., rwork->wq,
&rwork->work);|kfree_rcu_work() |krwp->bulk_head_free[0] bulkis freed before new gp end!!!
Quoted 1 line
|The "
from_cset" is freedbefore new gp end.
Quoted 3 lines
// the task is scheduled in after many ms.
|css_set_ptr->subsys[(subsys_id) <--- Caused kernel crash, because
css_set_ptris freed.Quoted 2 lines
v2: Use helper function instead of inserted code block at
kfree_rcu_monitor().Quoted 4 lines
Fixes c014efeef76a ("rcu: Add multiple in-flight batches of
kfree_rcu()work")Signed-off-by Ziwei Dai <[email protected]>
Quoted 8 lines
Quoted 4 lines
Good catch, thank you!!!
How difficult was this to trigger? If it can be triggered easily, this of course needs to go into mainline sooner rather than later.
Roughly we can reproduce this issue within two rounds of 48h stress test, with 20 k5.15 devices. If KASAN is enabled, the reproduce rate is higher. So I think sooner is better.
Thank you for the info! This is in theory an old bug, but if you can easily find out, does it trigger for you on v6.2 or earlier?
Quoted 10 lines
Quoted 5 lines
Longer term, would it make sense to run the three channels through RCU separately, in order to avoid one channel refraining from starting a grace period just because some other channel has callbacks waiting for a grace period to complete? One argument against might be energy efficiency, but perhaps the ->gp_snap field could be used to get the best of both worlds.
I see
kvfree_rcu_drain_ready(krcp)is already called at the beginning ofkfree_rcu_monitor(), which polls the ->gp_snap field, to decide whether to free channel objects immediately or after gp. Both energy efficiency and timing seems be considered?
My concern is that running the channels separately might mean more grace periods (and thus more energy draw) on nearly idle devices, such devices usually being the ones for which energy efficiency matters most.
But perhaps Vlad, Neeraj, or Joel has some insight on this, given that they are the ones working on battery-powered devices.
Quoted 7 lines
Quoted 3 lines
Either way, this fixes only one bug of two. The second bug is in the
kfree_rcu()tests, which should have caught this bug. Thoughts on a good fix for those tests?I inserted a
msleep()between "rcu_read_lock(), get pointer viarcu_dereference()" and "reference pointer, using the member", at the rcu scenario, then we can reproduce this issue very soon in stress test. Cankfree_rcu()tests insertmsleep()?
Another approach is to separate concerns, so that readers interact with grace periods in the rcutorture.c tests, and to add the interaction of to-be-freed memory with grace periods in the rcuscale kvfree tests. I took a step in this direction with this commit on the -rcu tree's "dev" branch:
efbe7927f479 ("rcu/kvfree: Add debug to check grace periods")
Given this, might it be possible to make rcuscale.c's kfree_rcu() testing create patterns of usage of the three channels so as to catch this bug that way?
Quoted 5 lines
Quoted 3 lines
I have applied Uladzislau's and Mukesh's tags, and done the usual wordsmithing as shown at the end of this message. Please let me know if I messed anything up.
Thank you for the improvement on the patch! It seems better now.
No problem and thank you again for the debugging and the fix!
Thanx, Paul
Quoted 244 lines
Quoted 244 lines
kernel/rcu/tree.c static void kfree_rcu_work(struct work_struct
Quoted 9 lines
--- kernel/rcu/tree.c | 27 +++++++++++++++++++-------- 1 file changed, 19 insertions(+), 8 deletions(-)kernel/rcu/tree.c index renamed from kernel/rcu/tree.c +0
-08e880c0..7b95ee9 100644
kernel/rcu/tree.c +0
-0@@ -3024,6 +3024,18 @@ static void kfree_rcu_work(struct work_struct*work)
kernel/rcu/tree.c need_offload_krc(struct kfree_rcu_cpu *krcp) · line 3063
Quoted 12 lines
kernel/rcu/tree.c +9
-0return !!READ_ONCE (krcp->head); } +static bool +need_wait_for_krwp_work (struct kfree_rcu_cpu_work *krwp) { +int i; + +for (i =0 ; i <FREE_N_CHANNELS ; i++) +if (!list_empty (&krwp->bulk_head_free[i])) +return true; + +return !!krwp->head_free;This is fixed from v1, good!
kernel/rcu/tree.c need_offload_krc(struct kfree_rcu_cpu *krcp)
Quoted 4 lines
kernel/rcu/tree.c +2
-0+} +static int krc_count (struct kfree_rcu_cpu *krcp) { int sum = atomic_read(&krcp->head_count); @@ -3107,15+3119,14
kernel/rcu/tree.c static void kfree_rcu_monitor(struct work_struct *work)
Quoted 10 lines
kernel/rcu/tree.c +0
-6@@ static void kfree_rcu_monitor(struct work_struct *work) for (i = 0; i < KFREE_N_BATCHES; i++) {struct kfree_rcu_cpu_work *krwp = &(krcp->krw_arr[i]); -// Try to detach bulk_head or head and attach it over any -// available corresponding free channel. It can be that -// a previous RCU batch is in progress, it means that -// immediately to queue another one is not possible so -// in that case the monitor work is rearmed. - if ((!list_empty(&krcp->bulk_head[0]) &&
list_empty(&krwp->bulk_head_free[0])) ||Quoted 1 line
- (!list_empty(&krcp->bulk_head[1]) &&
list_empty(&krwp->bulk_head_free[1])) ||Quoted 1 line
- (
READ_ONCE(krcp->head)&& !krwp->head_free)) {
kernel/rcu/tree.c
Quoted 2 lines
kernel/rcu/tree.c +2
-0+// Try to detach bulk_head or head and attach it, only when +// all channels are free. Any channel is not free means at krwp
Quoted 1 line
+ // there is on-going rcu work to handle krwp's free
business.
kernel/rcu/tree.c static void kfree_rcu_monitor(struct work_struct *work)
Quoted 4 lines
kernel/rcu/tree.c +3
-0+if (need_wait_for_krwp_work (krwp)) +continue ; +// kvfree_rcu_drain_ready() might handle this krcp, if so give up.
Quoted 2 lines
+ if (need_offload_krc(krcp)) { // Channel 1 corresponds to the SLAB-pointerbulk path.
Quoted 1 line
// Channel 2 corresponds to vmalloc-pointer bulkpath.
Quoted 3 lines
for (j = 0; j < FREE_N_CHANNELS; j++) {--
1.9.1------------------------------------------------------------------------
commit
e222f9a512539c3f4093a55d16624d9da614800bAuthor: Ziwei Dai <[email protected]> Date: Fri Mar 31 20:42:09 2023 +0800rcu: Avoid freeing new
kfree_rcu()memory after old grace periodMemory passed to
kvfree_rcu()that is to be freed is tracked by a per-CPUkfree_rcu_cpustructure, which in turn contains pointers tokvfree_rcu_bulk_datastructures that contain pointers to memory that has not yet been handed to RCU, along with ankfree_rcu_cpu_workstructure that tracks the memory that has already been handed to RCU. These structures track three categories of memory: (1) Memory forkfree(), (2) Memory forkvfree(), and (3) Memory for both that arrived during an OOM episode. The first two categories are tracked in a cache-friendly manner involving a dynamically allocated page of pointers (the aforementionedkvfree_rcu_bulk_datastructures), while the third uses a simple (but decidedly cache-unfriendly) linked list through thercu_headstructures in each block of memory.On a given CPU, these three categories are handled as a unit, with that CPU's
kfree_rcu_cpu_workstructure having one pointer for each of the three categories. Clearly, new memory for a given category cannot be placed in the correspondingkfree_rcu_cpu_workstructure until any old memory has had its grace period elapse and thus has been removed.
And
thekfree_rcu_monitor()function does in fact check for this.Except that the
kfree_rcu_monitor()function checks these pointers one at a time. This means that if the previouskfree_rcu()memory passed to RCU had only category 1 and the current one has only category 2, thekfree_rcu_monitor()function will send that current category-2 memory along immediately. This can result in memory being freed too soon, that is, out from under unsuspecting RCU readers.To see this, consider the following sequence of events, in which:
o Task A on CPU 0 calls
rcu_read_lock(), then uses "from_cset",
then is preempted.o CPU 1 calls
kfree_rcu(cset, rcu_head)in order to free
"from_cset"
after a later grace period. Except that "from_cset" is freed right after the previous grace period ended, so that
"from_cset"
is immediately freed. Task A resumes and references
"from_cset"'s
member, after which nothing good happens.In full detail:
CPU 0 CPU 1
---------------------- ----------------------
count_memcg_event_mm()
|rcu_read_lock() <--- |mem_cgroup_from_task()
|//css_set_ptris the "from_cset" mentioned on CPU 1 |css_set_ptr = rcu_dereference((task)->cgroups) |// Hard irq comes, current task is scheduled out.
cgroup_attach_task()|cgroup_migrate()|cgroup_migrate_execute()
|css_set_move_task(task,from_cset,to_cset, true)
|cgroup_move_task(task,to_cset)|rcu_assign_pointer(.., to_cset) |... |cgroup_migrate_finish()|put_css_set_locked(from_cset)
|from_cset->refcountreturn 0
|kfree_rcu(cset,rcu_head)
// freefrom_csetafter new gp|add_ptr_to_bulk_krc_lock()
|schedule_delayed_work(&krcp->monitor_work, ..)
kfree_rcu_monitor() |krcp->bulk_head[0]'swork attached to krwp->bulk_head_free[]
|queue_rcu_work(system_wq,
&krwp->rcu_work)|if rwork->rcu.work is not in WORK_STRUCT_PENDING_BIT state, |call_rcu(&rwork->rcu,
rcu_work_rcufn) <--- request new gp// There is a perious call_rcu(.., rcu_work_rcufn) // gp end, rcu_work_rcufn() is called. rcu_work_rcufn() |__queue_work(..,
rwork->wq,&rwork->work);|kfree_rcu_work() |krwp->bulk_head_free[0]bulk is freed before new gp end!!!
|The "from_cset" is freed
before new gp end.// the task resumes some time later. |css_set_ptr->subsys[(subsys_id) <--- Caused kernel crash, because
css_set_ptris freed.This commit therefore causes
kfree_rcu_monitor()to refrain from
moving
kfree_rcu()memory to thekfree_rcu_cpu_workstructure until the RCU grace period has completed for all three categories.v2: Use helper function instead of inserted code block at
kfree_rcu_monitor().Fixes c014efeef76a ("rcu: Add multiple in-flight batches of kfree_rcu()work")
Reported-by Mukesh Ojha <[email protected]>Signed-off-by Ziwei Dai <[email protected]>Reviewed-by Uladzislau Rezki (Sony) <[email protected]>Signed-off-by Paul E. McKenney <[email protected]>kernel/rcu/tree.c index renamed from kernel/rcu/tree.c +0
-0
859ee02f6614..e2dbea6cee4b100644kernel/rcu/tree.c +12
-0@@ -3051,6 +3051,18 @@ need_offload_krc(struct kfree_rcu_cpu *krcp)return !!READ_ONCE (krcp->head); } +static bool +need_wait_for_krwp_work (struct kfree_rcu_cpu_work *krwp) { +int i; + +for (i =0 ; i <FREE_N_CHANNELS ; i++) +if (!list_empty (&krwp->bulk_head_free[i])) +return true; + +return !!krwp->head_free; +} +static int krc_count (struct kfree_rcu_cpu *krcp) { int sum = atomic_read(&krcp->head_count); @@ -3134,15 +3146,14 @@ static void kfree_rcu_monitor(struct work_struct *work) for (i = 0; i < KFREE_N_BATCHES; i++) {struct kfree_rcu_cpu_work *krwp = &(krcp->krw_arr[i]);kernel/rcu/tree.c +0
-6-// Try to detach bulk_head or head and attach it over any -// available corresponding free channel. It can be that -// a previous RCU batch is in progress, it means that -// immediately to queue another one is not possible so -// in that case the monitor work is rearmed. - if ((!list_empty(&krcp->bulk_head[0]) &&
list_empty(&krwp->bulk_head_free[0])) ||
- (!list_empty(&krcp->bulk_head[1]) &&list_empty(&krwp->bulk_head_free[1])) ||
- (READ_ONCE(krcp->head)
&& !krwp->head_free)) {
+ // Try to detachbulk_heador head and attach it, only
when
+ // all channels are free. Any channel is not free means at
krwp
+ // there is on-going rcu work to handle krwp's free
business.kernel/rcu/tree.c +3
-0+if (need_wait_for_krwp_work (krwp)) +continue ; +// kvfree_rcu_drain_ready() might handle this krcp, if so give up.
+ if (need_offload_krc(krcp)) { // Channel 1 corresponds to the SLAB-pointer bulk path. // Channel 2 corresponds to vmalloc-pointer bulk path. for (j = 0; j < FREE_N_CHANNELS; j++) {
Quoted 56 lines · Ziwei Dai
In
kfree_rcu_monitor(), new free business at krcp is attached to any free channel at krwp.kfree_rcu_monitor()is responsible to make sure new free business is handled after the rcu grace period. But if there is any none-free channel at krwp already, that means there is an on-going rcu work, which will cause the kvfree_call_rcu()-triggered free business is done before the wanted rcu grace period ends.This commit ignore krwp which has non-free channel at
kfree_rcu_monitor(), to fix the issue thatkvfree_call_rcu()loses effectiveness.Below is the
css_setobj "from_cset" use-after-free case caused bykvfree_call_rcu()losing effectiveness.
CPU 0 callsrcu_read_lock(), then use "from_cset", then hard irq comes, the task is schedule out.
CPU 1 callskfree_rcu(cset, rcu_head), willing to free "from_cset" after new gp. But "from_cset" is freed right after current gp end. "from_cset" is reallocated. CPU 0 's task arrives back, references "from_cset"'s member, which causes crash.CPU 0 CPU 1
count_memcg_event_mm()
|rcu_read_lock() <---
|mem_cgroup_from_task()|// css_set_ptr is the "from_cset" mentioned on CPU 1 |css_set_ptr = rcu_dereference((task)->cgroups) |// Hard irq comes, current task is scheduled out.cgroup_attach_task() |cgroup_migrate() |cgroup_migrate_execute() |css_set_move_task(task, from_cset, to_cset, true) |cgroup_move_task(task, to_cset) |rcu_assign_pointer(.., to_cset) |... |cgroup_migrate_finish() |put_css_set_locked(from_cset) |from_cset->refcount return 0 |kfree_rcu(cset, rcu_head) // means to free from_cset after new gp |add_ptr_to_bulk_krc_lock() |schedule_delayed_work(&krcp->monitor_work, ..) kfree_rcu_monitor() |krcp->bulk_head[0]'s work attached to krwp->bulk_head_free[] |queue_rcu_work(system_wq, &krwp->rcu_work) |if rwork->rcu.work is not in WORK_STRUCT_PENDING_BIT state, |call_rcu(&rwork->rcu, rcu_work_rcufn) <--- request a new gp // There is a perious call_rcu(.., rcu_work_rcufn) // gp end, rcu_work_rcufn() is called. rcu_work_rcufn() |__queue_work(.., rwork->wq, &rwork->work); |kfree_rcu_work() |krwp->bulk_head_free[0] bulk is freed before new gp end!!! |The "from_cset" is freed before new gp end.
Quoted 7 lines
// the task is scheduled in after many ms.
|css_set_ptr->subsys[(subsys_id) <--- Caused kernel crash, becausecss_set_ptris freed.v2: Use helper function instead of inserted code block at
kfree_rcu_monitor().Fixes c014efeef76a ("rcu: Add multiple in-flight batches of kfree_rcu() work")Signed-off-by Ziwei Dai <[email protected]>
Please update the fixes tag to:5f3c8d620447 ("rcu/tree: Maintain separate array for vmalloc ptrs")
The issue happened when 5f3c8d620447 started looking at multiple channels at the same time in the same work handler function.
I think a better fix might be to separate out the work handler functions for each channel separately. That way we get more parallelism.
but since this is urgent,
thanks,
- Joel
Quoted 53 lines
--- kernel/rcu/tree.c | 27 +++++++++++++++++++-------- 1 file changed, 19 insertions(+), 8 deletions(-)kernel/rcu/tree.c +19
-9@@ -3024,6 +3024,18 @@ static void kfree_rcu_work(struct work_struct *work)return !!READ_ONCE (krcp->head); } +static bool +need_wait_for_krwp_work (struct kfree_rcu_cpu_work *krwp) +{ +int i; + +for (i =0 ; i <FREE_N_CHANNELS ; i++) +if (!list_empty (&krwp->bulk_head_free[i])) +return true; + +return !!krwp->head_free; +} +static int krc_count (struct kfree_rcu_cpu *krcp) {int sum =atomic_read (&krcp->head_count);@@ -3107,15 +3119,14 @@ static void kfree_rcu_monitor(struct work_struct *work)for (i =0 ; i <KFREE_N_BATCHES ; i++) {struct kfree_rcu_cpu_work *krwp = &(krcp->krw_arr[i]); -// Try to detach bulk_head or head and attach it over any -// available corresponding free channel. It can be that -// a previous RCU batch is in progress, it means that -// immediately to queue another one is not possible so -// in that case the monitor work is rearmed. -if ((!list_empty (&krcp->bulk_head[0 ]) &&list_empty (&krwp->bulk_head_free[0 ])) || - (!list_empty (&krcp->bulk_head[1 ]) &&list_empty (&krwp->bulk_head_free[1 ])) || - (READ_ONCE (krcp->head) && !krwp->head_free)) { +// Try to detach bulk_head or head and attach it, only when +// all channels are free. Any channel is not free means at krwp +// there is on-going rcu work to handle krwp's free business. +if (need_wait_for_krwp_work (krwp)) +continue ; +// kvfree_rcu_drain_ready() might handle this krcp, if so give up. +if (need_offload_krc (krcp)) {// Channel 1 corresponds to the SLAB-pointer bulk path. // Channel 2 corresponds to vmalloc-pointer bulk path. for (j =0 ; j <FREE_N_CHANNELS ; j++) { --1.9.1
Quoted 61 lines · Joel Fernandes
Quoted 59 lines · Ziwei Dai
In
kfree_rcu_monitor(), new free business at krcp is attached to any free channel at krwp.kfree_rcu_monitor()is responsible to make sure new free business is handled after the rcu grace period. But if there is any none-free channel at krwp already, that means there is an on-going rcu work, which will cause the kvfree_call_rcu()-triggered free business is done before the wanted rcu grace period ends.This commit ignore krwp which has non-free channel at
kfree_rcu_monitor(), to fix the issue thatkvfree_call_rcu()loses effectiveness.Below is the
css_setobj "from_cset" use-after-free case caused bykvfree_call_rcu()losing effectiveness.
CPU 0 callsrcu_read_lock(), then use "from_cset", then hard irq comes, the task is schedule out.
CPU 1 callskfree_rcu(cset, rcu_head), willing to free "from_cset" after new gp. But "from_cset" is freed right after current gp end. "from_cset" is reallocated. CPU 0 's task arrives back, references "from_cset"'s member, which causes crash.CPU 0 CPU 1
count_memcg_event_mm()
|rcu_read_lock() <---
|mem_cgroup_from_task()|// css_set_ptr is the "from_cset" mentioned on CPU 1 |css_set_ptr = rcu_dereference((task)->cgroups) |// Hard irq comes, current task is scheduled out.cgroup_attach_task() |cgroup_migrate() |cgroup_migrate_execute() |css_set_move_task(task, from_cset, to_cset, true) |cgroup_move_task(task, to_cset) |rcu_assign_pointer(.., to_cset) |... |cgroup_migrate_finish() |put_css_set_locked(from_cset) |from_cset->refcount return 0 |kfree_rcu(cset, rcu_head) // means to free from_cset after new gp |add_ptr_to_bulk_krc_lock() |schedule_delayed_work(&krcp->monitor_work, ..) kfree_rcu_monitor() |krcp->bulk_head[0]'s work attached to krwp->bulk_head_free[] |queue_rcu_work(system_wq, &krwp->rcu_work) |if rwork->rcu.work is not in WORK_STRUCT_PENDING_BIT state, |call_rcu(&rwork->rcu, rcu_work_rcufn) <--- request a new gp // There is a perious call_rcu(.., rcu_work_rcufn) // gp end, rcu_work_rcufn() is called. rcu_work_rcufn() |__queue_work(.., rwork->wq, &rwork->work); |kfree_rcu_work() |krwp->bulk_head_free[0] bulk is freed before new gp end!!! |The "from_cset" is freed before new gp end.// the task is scheduled in after many ms.
|css_set_ptr->subsys[(subsys_id) <--- Caused kernel crash, becausecss_set_ptris freed.
Quoted 7 lines
Quoted 4 lines
v2: Use helper function instead of inserted code block at
kfree_rcu_monitor().Fixes c014efeef76a ("rcu: Add multiple in-flight batches of kfree_rcu() work")Signed-off-by Ziwei Dai <[email protected]>Please update the fixes tag to:
5f3c8d620447("rcu/tree: Maintain separate array for vmalloc ptrs")
Vlad pointed out in another thread that the fix is actually to 34c881745549.
So just to be sure, it could be updated to:
thanks,
- Joel
Paul E. McKenney
maintainerQuoted 67 lines · Joel Fernandes
Quoted 66 lines · Joel Fernandes
Quoted 63 lines · Ziwei Dai
In
kfree_rcu_monitor(), new free business at krcp is attached to any free channel at krwp.kfree_rcu_monitor()is responsible to make sure new free business is handled after the rcu grace period. But if there is any none-free channel at krwp already, that means there is an on-going rcu work, which will cause the kvfree_call_rcu()-triggered free business is done before the wanted rcu grace period ends.This commit ignore krwp which has non-free channel at
kfree_rcu_monitor(), to fix the issue thatkvfree_call_rcu()loses effectiveness.Below is the
css_setobj "from_cset" use-after-free case caused bykvfree_call_rcu()losing effectiveness.
CPU 0 callsrcu_read_lock(), then use "from_cset", then hard irq comes, the task is schedule out.
CPU 1 callskfree_rcu(cset, rcu_head), willing to free "from_cset" after new gp. But "from_cset" is freed right after current gp end. "from_cset" is reallocated. CPU 0 's task arrives back, references "from_cset"'s member, which causes crash.CPU 0 CPU 1
count_memcg_event_mm()
|rcu_read_lock() <---
|mem_cgroup_from_task()|// css_set_ptr is the "from_cset" mentioned on CPU 1 |css_set_ptr = rcu_dereference((task)->cgroups) |// Hard irq comes, current task is scheduled out.cgroup_attach_task() |cgroup_migrate() |cgroup_migrate_execute() |css_set_move_task(task, from_cset, to_cset, true) |cgroup_move_task(task, to_cset) |rcu_assign_pointer(.., to_cset) |... |cgroup_migrate_finish() |put_css_set_locked(from_cset) |from_cset->refcount return 0 |kfree_rcu(cset, rcu_head) // means to free from_cset after new gp |add_ptr_to_bulk_krc_lock() |schedule_delayed_work(&krcp->monitor_work, ..) kfree_rcu_monitor() |krcp->bulk_head[0]'s work attached to krwp->bulk_head_free[] |queue_rcu_work(system_wq, &krwp->rcu_work) |if rwork->rcu.work is not in WORK_STRUCT_PENDING_BIT state, |call_rcu(&rwork->rcu, rcu_work_rcufn) <--- request a new gp // There is a perious call_rcu(.., rcu_work_rcufn) // gp end, rcu_work_rcufn() is called. rcu_work_rcufn() |__queue_work(.., rwork->wq, &rwork->work); |kfree_rcu_work() |krwp->bulk_head_free[0] bulk is freed before new gp end!!! |The "from_cset" is freed before new gp end.// the task is scheduled in after many ms.
|css_set_ptr->subsys[(subsys_id) <--- Caused kernel crash, becausecss_set_ptris freed.v2: Use helper function instead of inserted code block at
kfree_rcu_monitor().Fixes c014efeef76a ("rcu: Add multiple in-flight batches of kfree_rcu() work")Signed-off-by Ziwei Dai <[email protected]>
Quoted 8 lines
Quoted 2 lines
Please update the fixes tag to:
5f3c8d620447("rcu/tree: Maintain separate array for vmalloc ptrs")Vlad pointed out in another thread that the fix is actually to
34c881745549.So just to be sure, it could be updated to:
Fixes 34c881745549 ("rcu: Support kfree_bulk() interface in kfree_rcu()")Fixes 5f3c8d620447 ("rcu/tree: Maintain separate array for vmalloc ptrs")
Ziwei Dai, does this change in Fixes look good to you?
If so, I will update the commit log in this commit that I am planning to submit into v6.3. It is strictly speaking not a v6.3 regression, but it is starting to show up in the wild and the patch is contained enough to be considered an urgent fix.
Thanx, Paul
Quoted 103 lines
-----邮件原件-----
发件人: Paul E. McKenney <[email protected]>
发送时间: 2023年4月6日 2:46
收件人: Joel Fernandes <[email protected]>
抄送: 代子为 (Ziwei Dai) <[email protected]>; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; 王双 (Shuang Wang) <[email protected]>; 辛依凡 (Yifan Xin) <[email protected]>; 王科 (Ke Wang) <[email protected]>; 闫学文 (Xuewen Yan) <[email protected]>; 牛志国 (Zhiguo Niu) <[email protected]>; 黄朝阳 (Zhaoyang Huang) <[email protected]>
主题: Re: [PATCH V2] rcu: Make sure new krcp free business is handled after the wanted rcu grace period.注意: 这封邮件来自于外部。除非你确定邮件内容安全,否则不要点击任何链接和附件。
CAUTION: This email originated from outside of the organization. Do not click links or open attachments unless you recognize the sender and know the content is safe.Quoted 74 lines · Joel Fernandes
Quoted 64 lines · Joel Fernandes
Quoted 62 lines · Ziwei Dai
In
kfree_rcu_monitor(), new free business at krcp is attached to
any free channel at krwp.kfree_rcu_monitor()is responsible to
make sure new free business is handled after the rcu grace period.
But if there is any none-free channel at krwp already, that means
there is an on-going rcu work, which will cause the kvfree_call_rcu()-triggered free business is done before the wanted rcu grace period ends.This commit ignore krwp which has non-free channel at
kfree_rcu_monitor(), to fix the issue thatkvfree_call_rcu()loses effectiveness.Below is the
css_setobj "from_cset" use-after-free case caused bykvfree_call_rcu()losing effectiveness.
CPU 0 callsrcu_read_lock(), then use "from_cset", then hard irq comes, the task is schedule out.
CPU 1 callskfree_rcu(cset, rcu_head), willing to free "from_cset" after new gp. But "from_cset" is freed right after current gp end. "from_cset" is reallocated. CPU 0 's task arrives back, references "from_cset"'s member, which causes crash.CPU 0 CPU 1
count_memcg_event_mm()
|rcu_read_lock() <---
|mem_cgroup_from_task()
|//css_set_ptris the "from_cset" mentioned on CPU 1
|css_set_ptr = rcu_dereference((task)->cgroups) |// Hard irq comes, current task is scheduled out.cgroup_attach_task() |cgroup_migrate() |cgroup_migrate_execute() |css_set_move_task(task, from_cset, to_cset, true) |cgroup_move_task(task, to_cset) |rcu_assign_pointer(.., to_cset) |... |cgroup_migrate_finish() |put_css_set_locked(from_cset) |from_cset->refcount return 0 |kfree_rcu(cset, rcu_head) // means to free from_cset after new gp |add_ptr_to_bulk_krc_lock()|schedule_delayed_work(&krcp->monitor_work, ..)
kfree_rcu_monitor() |krcp->bulk_head[0]'s work attached to krwp->bulk_head_free[] |queue_rcu_work(system_wq, &krwp->rcu_work) |if rwork->rcu.work is not in WORK_STRUCT_PENDING_BIT state, |call_rcu(&rwork->rcu,
rcu_work_rcufn) <--- request a new gp// There is a perious call_rcu(.., rcu_work_rcufn) // gp end, rcu_work_rcufn() is called. rcu_work_rcufn() |__queue_work(..,
rwork->wq,&rwork->work);|kfree_rcu_work() |krwp->bulk_head_free[0] bulk is freed before new gp end!!! |The "from_cset" is freed before new gp end.// the task is scheduled in after many ms.
|css_set_ptr->subsys[(subsys_id) <--- Caused kernel crash, becausecss_set_ptris freed.Quoted 8 lines
Quoted 5 lines
v2: Use helper function instead of inserted code block at
kfree_rcu_monitor().Fixes c014efeef76a ("rcu: Add multiple in-flight batches of
kfree_rcu()work")Signed-off-by Ziwei Dai <[email protected]>Please update the fixes tag to:
5f3c8d620447("rcu/tree: Maintain separate array for vmalloc ptrs")Quoted 7 lines
Vlad pointed out in another thread that the fix is actually to
34c881745549.So just to be sure, it could be updated to:
Fixes 34c881745549 ("rcu: Support kfree_bulk() interface in
kfree_rcu()")Fixes 5f3c8d620447 ("rcu/tree: Maintain separate array for vmallocptrs")
Quoted 6 lines
Ziwei Dai, does this change in Fixes look good to you?
If so, I will update the commit log in this commit that I am planning to submit into v6.3. It is strictly speaking not a v6.3 regression, but it is starting to show up in the wild and the patch is contained enough to be considered an urgent fix.
Thanx, Paul
Hi Paul, it looks good to me and thanks!
Paul E. McKenney
maintainerQuoted 103 lines · 代子为 (Ziwei Dai)
Quoted 103 lines
-----邮件原件-----
发件人: Paul E. McKenney <[email protected]>
发送时间: 2023年4月6日 2:46
收件人: Joel Fernandes <[email protected]>
抄送: 代子为 (Ziwei Dai) <[email protected]>; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; 王双 (Shuang Wang) <[email protected]>; 辛依凡 (Yifan Xin) <[email protected]>; 王科 (Ke Wang) <[email protected]>; 闫学文 (Xuewen Yan) <[email protected]>; 牛志国 (Zhiguo Niu) <[email protected]>; 黄朝阳 (Zhaoyang Huang) <[email protected]>
主题: Re: [PATCH V2] rcu: Make sure new krcp free business is handled after the wanted rcu grace period.注意: 这封邮件来自于外部。除非你确定邮件内容安全,否则不要点击任何链接和附件。
CAUTION: This email originated from outside of the organization. Do not click links or open attachments unless you recognize the sender and know the content is safe.Quoted 81 lines · Joel Fernandes
Quoted 64 lines · Joel Fernandes
Quoted 62 lines · Ziwei Dai
In
kfree_rcu_monitor(), new free business at krcp is attached to
any free channel at krwp.kfree_rcu_monitor()is responsible to
make sure new free business is handled after the rcu grace period.
But if there is any none-free channel at krwp already, that means
there is an on-going rcu work, which will cause the kvfree_call_rcu()-triggered free business is done before the wanted rcu grace period ends.This commit ignore krwp which has non-free channel at
kfree_rcu_monitor(), to fix the issue thatkvfree_call_rcu()loses effectiveness.Below is the
css_setobj "from_cset" use-after-free case caused bykvfree_call_rcu()losing effectiveness.
CPU 0 callsrcu_read_lock(), then use "from_cset", then hard irq comes, the task is schedule out.
CPU 1 callskfree_rcu(cset, rcu_head), willing to free "from_cset" after new gp. But "from_cset" is freed right after current gp end. "from_cset" is reallocated. CPU 0 's task arrives back, references "from_cset"'s member, which causes crash.CPU 0 CPU 1
count_memcg_event_mm()
|rcu_read_lock() <---
|mem_cgroup_from_task()
|//css_set_ptris the "from_cset" mentioned on CPU 1
|css_set_ptr = rcu_dereference((task)->cgroups) |// Hard irq comes, current task is scheduled out.cgroup_attach_task() |cgroup_migrate() |cgroup_migrate_execute() |css_set_move_task(task, from_cset, to_cset, true) |cgroup_move_task(task, to_cset) |rcu_assign_pointer(.., to_cset) |... |cgroup_migrate_finish() |put_css_set_locked(from_cset) |from_cset->refcount return 0 |kfree_rcu(cset, rcu_head) // means to free from_cset after new gp |add_ptr_to_bulk_krc_lock()|schedule_delayed_work(&krcp->monitor_work, ..)
kfree_rcu_monitor() |krcp->bulk_head[0]'s work attached to krwp->bulk_head_free[] |queue_rcu_work(system_wq, &krwp->rcu_work) |if rwork->rcu.work is not in WORK_STRUCT_PENDING_BIT state, |call_rcu(&rwork->rcu,
rcu_work_rcufn) <--- request a new gp// There is a perious call_rcu(.., rcu_work_rcufn) // gp end, rcu_work_rcufn() is called. rcu_work_rcufn() |__queue_work(..,
rwork->wq,&rwork->work);|kfree_rcu_work() |krwp->bulk_head_free[0] bulk is freed before new gp end!!! |The "from_cset" is freed before new gp end.// the task is scheduled in after many ms.
|css_set_ptr->subsys[(subsys_id) <--- Caused kernel crash, becausecss_set_ptris freed.Quoted 8 lines
Quoted 5 lines
v2: Use helper function instead of inserted code block at
kfree_rcu_monitor().Fixes c014efeef76a ("rcu: Add multiple in-flight batches of
kfree_rcu()work")Signed-off-by Ziwei Dai <[email protected]>Please update the fixes tag to:
5f3c8d620447("rcu/tree: Maintain separate array for vmalloc ptrs")Vlad pointed out in another thread that the fix is actually to
34c881745549.So just to be sure, it could be updated to:
Fixes 34c881745549 ("rcu: Support kfree_bulk() interface in
kfree_rcu()")Fixes 5f3c8d620447 ("rcu/tree: Maintain separate array for vmallocptrs")
Quoted 8 lines
Quoted 6 lines
Ziwei Dai, does this change in Fixes look good to you?
If so, I will update the commit log in this commit that I am planning to submit into v6.3. It is strictly speaking not a v6.3 regression, but it is starting to show up in the wild and the patch is contained enough to be considered an urgent fix.
Thanx, Paul
Hi Paul, it looks good to me and thanks!
Thank you, and I will fix on my next rebase.
Thanx, Paul
Quoted 106 lines · Paul E. McKenney
Quoted 105 lines · 代子为 (Ziwei Dai)
Quoted 105 lines
-----邮件原件-----
发件人: Paul E. McKenney <[email protected]>
发送时间: 2023年4月6日 2:46
收件人: Joel Fernandes <[email protected]>
抄送: 代子为 (Ziwei Dai) <[email protected]>; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; 王双 (Shuang Wang) <[email protected]>; 辛依凡 (Yifan Xin) <[email protected]>; 王科 (Ke Wang) <[email protected]>; 闫学文 (Xuewen Yan) <[email protected]>; 牛志国 (Zhiguo Niu) <[email protected]>; 黄朝阳 (Zhaoyang Huang) <[email protected]>
主题: Re: [PATCH V2] rcu: Make sure new krcp free business is handled after the wanted rcu grace period.注意: 这封邮件来自于外部。除非你确定邮件内容安全,否则不要点击任何链接和附件。
CAUTION: This email originated from outside of the organization. Do not click links or open attachments unless you recognize the sender and know the content is safe.Quoted 74 lines · Joel Fernandes
Quoted 64 lines · Joel Fernandes
Quoted 62 lines · Ziwei Dai
In
kfree_rcu_monitor(), new free business at krcp is attached to
any free channel at krwp.kfree_rcu_monitor()is responsible to
make sure new free business is handled after the rcu grace period.
But if there is any none-free channel at krwp already, that means
there is an on-going rcu work, which will cause the kvfree_call_rcu()-triggered free business is done before the wanted rcu grace period ends.This commit ignore krwp which has non-free channel at
kfree_rcu_monitor(), to fix the issue thatkvfree_call_rcu()loses effectiveness.Below is the
css_setobj "from_cset" use-after-free case caused bykvfree_call_rcu()losing effectiveness.
CPU 0 callsrcu_read_lock(), then use "from_cset", then hard irq comes, the task is schedule out.
CPU 1 callskfree_rcu(cset, rcu_head), willing to free "from_cset" after new gp. But "from_cset" is freed right after current gp end. "from_cset" is reallocated. CPU 0 's task arrives back, references "from_cset"'s member, which causes crash.CPU 0 CPU 1
count_memcg_event_mm()
|rcu_read_lock() <---
|mem_cgroup_from_task()
|//css_set_ptris the "from_cset" mentioned on CPU 1
|css_set_ptr = rcu_dereference((task)->cgroups) |// Hard irq comes, current task is scheduled out.cgroup_attach_task() |cgroup_migrate() |cgroup_migrate_execute() |css_set_move_task(task, from_cset, to_cset, true) |cgroup_move_task(task, to_cset) |rcu_assign_pointer(.., to_cset) |... |cgroup_migrate_finish() |put_css_set_locked(from_cset) |from_cset->refcount return 0 |kfree_rcu(cset, rcu_head) // means to free from_cset after new gp |add_ptr_to_bulk_krc_lock()|schedule_delayed_work(&krcp->monitor_work, ..)
kfree_rcu_monitor() |krcp->bulk_head[0]'s work attached to krwp->bulk_head_free[] |queue_rcu_work(system_wq, &krwp->rcu_work) |if rwork->rcu.work is not in WORK_STRUCT_PENDING_BIT state, |call_rcu(&rwork->rcu,
rcu_work_rcufn) <--- request a new gp// There is a perious call_rcu(.., rcu_work_rcufn) // gp end, rcu_work_rcufn() is called. rcu_work_rcufn() |__queue_work(..,
rwork->wq,&rwork->work);|kfree_rcu_work() |krwp->bulk_head_free[0] bulk is freed before new gp end!!! |The "from_cset" is freed before new gp end.// the task is scheduled in after many ms.
|css_set_ptr->subsys[(subsys_id) <--- Caused kernel crash, becausecss_set_ptris freed.Quoted 8 lines
Quoted 5 lines
v2: Use helper function instead of inserted code block at
kfree_rcu_monitor().Fixes c014efeef76a ("rcu: Add multiple in-flight batches of
kfree_rcu()work")Signed-off-by Ziwei Dai <[email protected]>Please update the fixes tag to:
5f3c8d620447("rcu/tree: Maintain separate array for vmalloc ptrs")Quoted 7 lines
Vlad pointed out in another thread that the fix is actually to
34c881745549.So just to be sure, it could be updated to:
Fixes 34c881745549 ("rcu: Support kfree_bulk() interface in
kfree_rcu()")Fixes 5f3c8d620447 ("rcu/tree: Maintain separate array for vmallocptrs")
Ziwei Dai, does this change in Fixes look good to you?
Quoted 9 lines
Quoted 6 lines
Quoted 4 lines
If so, I will update the commit log in this commit that I am planning to submit into v6.3. It is strictly speaking not a v6.3 regression, but it is starting to show up in the wild and the patch is contained enough to be considered an urgent fix.
Thanx, Paul
Hi Paul, it looks good to me and thanks!
Thank you, and I will fix on my next rebase.
After heavy testing over night i do not see that any warnings are triggered:
--
Uladzislau Rezki
Paul E. McKenney
maintainerQuoted 112 lines · Uladzislau Rezki
Quoted 111 lines · Paul E. McKenney
Quoted 110 lines · 代子为 (Ziwei Dai)
Quoted 109 lines
-----邮件原件-----
发件人: Paul E. McKenney <[email protected]>
发送时间: 2023年4月6日 2:46
收件人: Joel Fernandes <[email protected]>
抄送: 代子为 (Ziwei Dai) <[email protected]>; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; 王双 (Shuang Wang) <[email protected]>; 辛依凡 (Yifan Xin) <[email protected]>; 王科 (Ke Wang) <[email protected]>; 闫学文 (Xuewen Yan) <[email protected]>; 牛志国 (Zhiguo Niu) <[email protected]>; 黄朝阳 (Zhaoyang Huang) <[email protected]>
主题: Re: [PATCH V2] rcu: Make sure new krcp free business is handled after the wanted rcu grace period.注意: 这封邮件来自于外部。除非你确定邮件内容安全,否则不要点击任何链接和附件。
CAUTION: This email originated from outside of the organization. Do not click links or open attachments unless you recognize the sender and know the content is safe.Quoted 74 lines · Joel Fernandes
Quoted 64 lines · Joel Fernandes
Quoted 62 lines · Ziwei Dai
In
kfree_rcu_monitor(), new free business at krcp is attached to
any free channel at krwp.kfree_rcu_monitor()is responsible to
make sure new free business is handled after the rcu grace period.
But if there is any none-free channel at krwp already, that means
there is an on-going rcu work, which will cause the kvfree_call_rcu()-triggered free business is done before the wanted rcu grace period ends.This commit ignore krwp which has non-free channel at
kfree_rcu_monitor(), to fix the issue thatkvfree_call_rcu()loses effectiveness.Below is the
css_setobj "from_cset" use-after-free case caused bykvfree_call_rcu()losing effectiveness.
CPU 0 callsrcu_read_lock(), then use "from_cset", then hard irq comes, the task is schedule out.
CPU 1 callskfree_rcu(cset, rcu_head), willing to free "from_cset" after new gp. But "from_cset" is freed right after current gp end. "from_cset" is reallocated. CPU 0 's task arrives back, references "from_cset"'s member, which causes crash.CPU 0 CPU 1
count_memcg_event_mm()
|rcu_read_lock() <---
|mem_cgroup_from_task()
|//css_set_ptris the "from_cset" mentioned on CPU 1
|css_set_ptr = rcu_dereference((task)->cgroups) |// Hard irq comes, current task is scheduled out.cgroup_attach_task() |cgroup_migrate() |cgroup_migrate_execute() |css_set_move_task(task, from_cset, to_cset, true) |cgroup_move_task(task, to_cset) |rcu_assign_pointer(.., to_cset) |... |cgroup_migrate_finish() |put_css_set_locked(from_cset) |from_cset->refcount return 0 |kfree_rcu(cset, rcu_head) // means to free from_cset after new gp |add_ptr_to_bulk_krc_lock()|schedule_delayed_work(&krcp->monitor_work, ..)
kfree_rcu_monitor() |krcp->bulk_head[0]'s work attached to krwp->bulk_head_free[] |queue_rcu_work(system_wq, &krwp->rcu_work) |if rwork->rcu.work is not in WORK_STRUCT_PENDING_BIT state, |call_rcu(&rwork->rcu,
rcu_work_rcufn) <--- request a new gp// There is a perious call_rcu(.., rcu_work_rcufn) // gp end, rcu_work_rcufn() is called. rcu_work_rcufn() |__queue_work(..,
rwork->wq,&rwork->work);|kfree_rcu_work() |krwp->bulk_head_free[0] bulk is freed before new gp end!!! |The "from_cset" is freed before new gp end.// the task is scheduled in after many ms.
|css_set_ptr->subsys[(subsys_id) <--- Caused kernel crash, becausecss_set_ptris freed.Quoted 8 lines
Quoted 5 lines
v2: Use helper function instead of inserted code block at
kfree_rcu_monitor().Fixes c014efeef76a ("rcu: Add multiple in-flight batches of
kfree_rcu()work")Signed-off-by Ziwei Dai <[email protected]>Please update the fixes tag to:
5f3c8d620447("rcu/tree: Maintain separate array for vmalloc ptrs")Quoted 7 lines
Vlad pointed out in another thread that the fix is actually to
34c881745549.So just to be sure, it could be updated to:
Fixes 34c881745549 ("rcu: Support kfree_bulk() interface in
kfree_rcu()")Fixes 5f3c8d620447 ("rcu/tree: Maintain separate array for vmallocptrs")
Ziwei Dai, does this change in Fixes look good to you?
If so, I will update the commit log in this commit that I am planning to submit into v6.3. It is strictly speaking not a v6.3 regression, but it is starting to show up in the wild and the patch is contained enough to be considered an urgent fix.
Thanx, Paul
Quoted 8 lines
Quoted 4 lines
Quoted 1 line
Hi Paul, it looks good to me and thanks!
Thank you, and I will fix on my next rebase.
After heavy testing over night i do not see that any warnings are triggered:
Thank you very much!!! I will apply this during my upcoming rebase.
Thanx, Paul