sched/scs: Reset the shadow stack when idle_task_exit
PATCH 1/1 sched/scs: Reset the shadow stack when idle_task_exit
There was a 'init_idle' that resets scs sp to base, but is removed by f1a0a376ca0c. Without the resetting, the hot-plugging implemented by cpu_psci_cpu_boot will use the previous scs sp as new base when starting up a CPU core, so the usage on scs page is being stacked up until overflow.
This only happens on idle task since __cpu_up is using idle task as the main thread to start up a CPU core, so the overflow can be fixed by resetting scs sp to base in 'idle_task_exit'.
--- kernel/sched/core.c | 1 + 1 file changed, 1 insertion(+)
kernel/sched/core.c +1 -0
@@ -8795,6 +8795,7 @@ void idle_task_exit(void)finish_arch_post_lock_switch (); } +scs_task_reset (current);/* finish_cpu(), as ran on the BP, will clean up the active_mm state */ }
Signature · 1 line
2.33.0.882.g93a45727a2-goog
Quoted 10 lines · Woody Lin
There was a '
init_idle' that resets scs sp to base, but is removed byf1a0a376ca0c. Without the resetting, the hot-plugging implemented bycpu_psci_cpu_bootwill use the previous scs sp as new base when starting up a CPU core, so the usage on scs page is being stacked up until overflow.This only happens on idle task since
__cpu_upis using idle task as the main thread to start up a CPU core, so the overflow can be fixed by resetting scs sp to base in 'idle_task_exit'.
Looking at init_idle() for similar issues, it looks like we might also want to re-issue kasan_unpoison_task_stack() on the idle task upon hotplug.
Quoted 11 lines
Fixes f1a0a376ca0c ("sched/core: Initialize the idle task with preemption disabled")Signed-off-by Woody Lin <[email protected]>--- kernel/sched/core.c | 1 + 1 file changed, 1 insertion(+)kernel/sched/core.c +0
-0
kernel/sched/core.c void idle_task_exit(void) · line 8799
Quoted 5 lines
kernel/sched/core.c +1
-0@@ -8795,6 +8795,7 @@ void idle_task_exit(void)finish_arch_post_lock_switch (); } +scs_task_reset (current);/* finish_cpu(), as ran on the BP, will clean up the active_mm state */
So AIUI for SCS that works just fine - one thing I'm unclear on is how the following pops are going to work given the SP reset happens in the middle of a call stack, but AFAICT that was already the case before I messed about with init_idle(), so that must already be handled.
I'm not familiar enough with KASAN to say whether that kasan_unpoison_task_stack() should rather happen upon hotplugging the CPU back (rather than on hot-unplug). If that is the case, then maybe somewhere around cpu_startup_entry() might work (and then you could bunch these two "needs to be re-run at init for the idle task" functions into a common helper).
Quoted 4 lines
kernel/sched/core.c +0
-1} --2.33.0.882.g93a45727a2-goog
Quoted 30 lines · Valentin Schneider
Quoted 10 lines · Woody Lin
There was a '
init_idle' that resets scs sp to base, but is removed byf1a0a376ca0c. Without the resetting, the hot-plugging implemented bycpu_psci_cpu_bootwill use the previous scs sp as new base when starting up a CPU core, so the usage on scs page is being stacked up until overflow.This only happens on idle task since
__cpu_upis using idle task as the main thread to start up a CPU core, so the overflow can be fixed by resetting scs sp to base in 'idle_task_exit'.Looking at
init_idle()for similar issues, it looks like we might also want to re-issuekasan_unpoison_task_stack()on the idle task upon hotplug.Quoted 14 lines
Fixes f1a0a376ca0c ("sched/core: Initialize the idle task with preemption disabled")Signed-off-by Woody Lin <[email protected]>--- kernel/sched/core.c | 1 + 1 file changed, 1 insertion(+)kernel/sched/core.c +0
-0@@ -8795,6 +8795,7 @@ void idle_task_exit(void)finish_arch_post_lock_switch (); }
Quoted 7 lines
kernel/sched/core.c void idle_task_exit(void) · line 8799
Quoted 2 lines
kernel/sched/core.c +1
-0+scs_task_reset (current);/* finish_cpu(), as ran on the BP, will clean up the active_mm state */ So AIUI for SCS that works just fine - one thing I'm unclear on is how the following pops are going to work given the SP reset happens in the middle of a call stack, but AFAICT that was already the case before I messed about with
init_idle(), so that must already be handled.
Hi Valentin,
Thanks for the question. The 'scs_task_reset' here resets only the '.thread_info.scs_sp' of the task, so the register (on arm64 it's x18) is still pointing to the same location for popping and storing call frames. The register will be updated to '.thread_info.scs_sp' in '__secondary_switched', which starts a new core and there is no popping after the updating, so it won't introduce an underflow.
Quoted 7 lines
I'm not familiar enough with KASAN to say whether that
kasan_unpoison_task_stack()should rather happen upon hotplugging the CPU back (rather than on hot-unplug). If that is the case, then maybe somewhere aroundcpu_startup_entry()might work (and then you could bunch these two "needs to be re-run at init for the idle task" functions into a common helper).
unpoison looks more like an one-time thing to me; the idle tasks will reuse the same stack pages until system resets, so I think we don't need to re-unpoison that during hotplugging as long as it's unpoisoned in 'init_idle'.
Quoted 5 lines
Quoted 5 lines
kernel/sched/core.c +0
-1} --2.33.0.882.g93a45727a2-goog
Quoted 8 lines · Woody Lin
Quoted 5 lines · Valentin Schneider
So AIUI for SCS that works just fine - one thing I'm unclear on is how the following pops are going to work given the SP reset happens in the middle of a call stack, but AFAICT that was already the case before I messed about with
init_idle(), so that must already be handled.
Quoted 9 lines
Hi Valentin,
Thanks for the question. The '
scs_task_reset' here resets only the '.thread_info.scs_sp' of the task, so the register (on arm64 it's x18) is still pointing to the same location for popping and storing call frames. The register will be updated to '.thread_info.scs_sp' in '__secondary_switched', which starts a new core and there is no popping after the updating, so it won't introduce an underflow.
I think I got it; __secondary_switched() -> init_cpu_task() -> scs_load()
Thanks!
Quoted 8 lines
Quoted 7 lines
I'm not familiar enough with KASAN to say whether that
kasan_unpoison_task_stack()should rather happen upon hotplugging the CPU back (rather than on hot-unplug). If that is the case, then maybe somewhere aroundcpu_startup_entry()might work (and then you could bunch these two "needs to be re-run at init for the idle task" functions into a common helper).
Quoted 5 lines
unpoison looks more like an one-time thing to me; the idle tasks will reuse the same stack pages until system resets, so I think we don't need to re-unpoison that during hotplugging as long as it's unpoisoned in '
init_idle'.
I would tend to agree, but was bitten by s390 freeing some memory on hot-unplug and re-allocating it upon hotplug:
6a942f578054 ("s390: preempt: Fix preempt_count initialization")
This makes me doubt whether we can assert the idle task stack pages are perennial vs hotplug on all architectures.
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
}
--
2.33.0.882.g93a45727a2-goog
Quoted 38 lines · Valentin Schneider
Quoted 8 lines · Woody Lin
Quoted 5 lines · Valentin Schneider
So AIUI for SCS that works just fine - one thing I'm unclear on is how the following pops are going to work given the SP reset happens in the middle of a call stack, but AFAICT that was already the case before I messed about with
init_idle(), so that must already be handled.Quoted 9 lines
Hi Valentin,
Thanks for the question. The '
scs_task_reset' here resets only the '.thread_info.scs_sp' of the task, so the register (on arm64 it's x18) is still pointing to the same location for popping and storing call frames. The register will be updated to '.thread_info.scs_sp' in '__secondary_switched', which starts a new core and there is no popping after the updating, so it won't introduce an underflow.I think I got it;
__secondary_switched()->init_cpu_task()->scs_load()Thanks!
Quoted 8 lines
Quoted 7 lines
I'm not familiar enough with KASAN to say whether that
kasan_unpoison_task_stack()should rather happen upon hotplugging the CPU back (rather than on hot-unplug). If that is the case, then maybe somewhere aroundcpu_startup_entry()might work (and then you could bunch these two "needs to be re-run at init for the idle task" functions into a common helper).Quoted 5 lines
unpoison looks more like an one-time thing to me; the idle tasks will reuse the same stack pages until system resets, so I think we don't need to re-unpoison that during hotplugging as long as it's unpoisoned in '
init_idle'.
Quoted 7 lines
I would tend to agree, but was bitten by s390 freeing some memory on hot-unplug and re-allocating it upon hotplug:
6a942f578054("s390: preempt: Fixpreempt_countinitialization")This makes me doubt whether we can assert the idle task stack pages are perennial vs hotplug on all architectures.
I made a quick study on memory-hotplug and seems that only memory contains nothing other than migratable pages can be unplugged. So process stack pages should not be a concern for this, since which is an unmovable memory.
However I don't have a chance to work on a system that enables memory-hotplug so far, so couldn't verify this assumption further. Guess we can create a separate thread to clarify this more.
Regards,
Woody
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
}
--
2.33.0.882.g93a45727a2-goog
Valentin Schneider
Reviewed-byQuoted 16 lines · Woody Lin
Quoted 7 lines · Valentin Schneider
Quoted 5 lines · Woody Lin
unpoison looks more like an one-time thing to me; the idle tasks will reuse the same stack pages until system resets, so I think we don't need to re-unpoison that during hotplugging as long as it's unpoisoned in '
init_idle'.Quoted 7 lines
I would tend to agree, but was bitten by s390 freeing some memory on hot-unplug and re-allocating it upon hotplug:
6a942f578054("s390: preempt: Fixpreempt_countinitialization")This makes me doubt whether we can assert the idle task stack pages are perennial vs hotplug on all architectures.
Quoted 9 lines
I made a quick study on memory-hotplug and seems that only memory contains nothing other than migratable pages can be unplugged. So process stack pages should not be a concern for this, since which is an unmovable memory.
However I don't have a chance to work on a system that enables memory-hotplug so far, so couldn't verify this assumption further. Guess we can create a separate thread to clarify this more.
That sounds sensible; I'll try to dig some more into this.
As for the SCS change, someone might argue for placing this elsewhere in the hotplug path, but that looks fine to me:
Quoted 9 lines
Regards,
WoodyQuoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
}
--
2.33.0.882.g93a45727a2-goog
Peter Zijlstra
maintainerQuoted 4 lines · Valentin Schneider
As for the SCS change, someone might argue for placing this elsewhere in the hotplug path, but that looks fine to me:
Reviewed-by Valentin Schneider <[email protected]>
Thanks!
The following commit has been merged into the sched/urgent branch of tip:
The following commit has been merged into the sched/urgent branch of tip:
Commit-ID: 45dfb89b8f96643268449c25d7025b17de46717c Gitweb: https://git.kernel.org/tip/45dfb89b8f96643268449c25d7025b17de46717c Author: Woody Lin <[email protected]> AuthorDate: Tue, 12 Oct 2021 16:35:21 +08:00 Committer: Peter Zijlstra <[email protected]> CommitterDate: Mon, 18 Oct 2021 16:58:41 +02:00
sched/scs: Reset the shadow stack when idle_task_exit
There was a 'init_idle' that resets scs sp to base, but is removed by f1a0a376ca0c. Without the resetting, the hot-plugging implemented by cpu_psci_cpu_boot will use the previous scs sp as new base when starting up a CPU core, so the usage on scs page is being stacked up until overflow.
This only happens on idle task since __cpu_up is using idle task as the main thread to start up a CPU core, so the overflow can be fixed by resetting scs sp to base in 'idle_task_exit'.
--- kernel/sched/core.c | 1 + 1 file changed, 1 insertion(+)
kernel/sched/core.c +1 -0
@@ -8795,6 +8795,7 @@ void idle_task_exit(void)finish_arch_post_lock_switch (); } +scs_task_reset (current);/* finish_cpu(), as ran on the BP, will clean up the active_mm state */ }
The following commit has been merged into the sched/urgent branch of tip:
The following commit has been merged into the sched/urgent branch of tip:
Commit-ID: 63acd42c0d4942f74710b11c38602fb14dea7320 Gitweb: https://git.kernel.org/tip/63acd42c0d4942f74710b11c38602fb14dea7320 Author: Woody Lin <[email protected]> AuthorDate: Tue, 12 Oct 2021 16:35:21 +08:00 Committer: Peter Zijlstra <[email protected]> CommitterDate: Tue, 19 Oct 2021 17:46:11 +02:00
sched/scs: Reset the shadow stack when idle_task_exit
Commit f1a0a376ca0c ("sched/core: Initialize the idle task with preemption disabled") removed the init_idle() call from idle_thread_get(). This was the sole call-path on hotplug that resets the Shadow Call Stack (scs) Stack Pointer (sp).
Not resetting the scs-sp leads to scs overflow after enough hotplug cycles. Therefore add an explicit scs_task_reset() to the hotplug code to make sure the scs-sp does get reset on hotplug.
--- kernel/sched/core.c | 1 + 1 file changed, 1 insertion(+)
kernel/sched/core.c +1 -0
@@ -8795,6 +8795,7 @@ void idle_task_exit(void)finish_arch_post_lock_switch (); } +scs_task_reset (current);/* finish_cpu(), as ran on the BP, will clean up the active_mm state */ }