x86/sgx: Report RCU-Tasks quiescent state in EPC sanitization loop
PATCH v7 1/1 x86/sgx: Report RCU-Tasks quiescent state in EPC sanitization loop
When the kernel boots from kexec, the EPC pages may have a stale state. The kernel sanitizes all EPC pages to reset them to a clean state before their first use in any enclave. The EPC size could be several GBs and resetting them could take a significant amount of time. Because of that, the kernel performs the reset in a loop through a kernel thread ksgxd() at early boot, and there's a cond_resched() after resetting each EPC page.
This is fine in most cases, but becomes a problem when there's other kernel code waiting for an RCU-Tasks grace period but the cond_resched() in ksgxd() never triggers rescheduling. Because cond_resched() doesn't report a quiescent state when it doesn't trigger rescheduling, the thread that is waiting for an RCU-Tasks grace period will wait until all EPC pages are reset.
For instance, BPF LSM subsystem can invoke synchronize_rcu_tasks() at kernel boot time. A VM with a large EPC assigned and BPF LSM enabled can take a long time to boot, with a call trace triggered:
rcu_tasks_wait_gp: rcu_tasks grace period number 1 (since boot) is
130631 jiffies old.
INFO: task systemd:1 blocked for more than 122 seconds.
...
task:systemd state:D stack:0 pid:1 tpid:1 ppid:0 flags:0x00000002
Call Trace:
...
schedule_timeout+0x157/0x170
wait_for_completion+0x88/0x150
__wait_rcu_gp+0x17e/0x190
synchronize_rcu_tasks_generic+0x64/0x60
...
synchronize_rcu_tasks+0x15/0x20
register_ftrace_direct+0x31f/0x350
...
bpf_trampoline_link_prog+0x33/0x60
bpf_tracing_prog_attach+0x3c5/0x5f0Replace cond_resched() with cond_resched_tasks_rcu_qs() which explicitly reports quiescent state regardless of whether actual rescheduling is triggered. Resetting all EPC pages in ksgxd() isn't performance critical so the extra cost of cond_resched_tasks_rcu_qs() isn't a problem.
Tests showed this reduced the VM kernel boot time from ~50s to ~700ms.
---
v1 -> v2:
- Clarify the RCU Tasks stall root cause.
- Use cond_resched_rcu_qs() following the Kai`s suggestion.
v2 -> v3:
- cee439398933 ("rcu: Rename cond_resched_rcu_qs() to cond_resched_tasks_rcu_qs()")
v3 -> v4:
- Trim down/rewrite changelog following Kai`s suggestion.
v4 -> v5:
- Change the title, not state the problem directly
- Corrected spelling and grammatical errors by Kai
- Add "Reviewed-by: Kai Huang"
v5 -> v6:
- Add the exactly kind of reminder that why cond_resched() does not trigger rescheduling.
v6 -> v7:
- Based on Dave's suggestion, add Jarkko's tag:
--- arch/x86/kernel/cpu/sgx/main.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-)
arch/x86/kernel/cpu/sgx/main.c +7 -1
@@ -106,7 +106,13 @@ static unsigned long __sgx_sanitize_pages(struct list_head *dirty_page_list) left_dirty++; } -cond_resched (); +/* +* cond_resched() only schedules when TIF_NEED_RESCHED is set. +* During this boot-time loop that condition may not happen for a +* long time, so report an RCU-Tasks quiescent state explicitly. +* Therefore, change cond_resched() to cond_resched_tasks_rcu_qs(). +*/ +cond_resched_tasks_rcu_qs (); }list_splice (&dirty, dirty_page_list);
Signature · 1 line
2.43.0
The following commit has been merged into the x86/sgx branch of tip:
The following commit has been merged into the x86/sgx branch of tip:
Commit-ID: 993b65a2fc6f3b2d61428ed2f2d10f94e94e6f3c Gitweb: https://git.kernel.org/tip/993b65a2fc6f3b2d61428ed2f2d10f94e94e6f3c Author: Jun Miao <[email protected]> AuthorDate: Wed, 02 Sep 2026 09:36:53 +08:00 Committer: Dave Hansen <[email protected]> CommitterDate: Wed, 02 Sep 2026 13:52:59 -07:00
x86/sgx: Report RCU-Tasks quiescent state in EPC sanitization loop
When the kernel boots from kexec, the EPC pages may have a stale state. The kernel sanitizes all EPC pages to reset them to a clean state before their first use in any enclave. The EPC size could be several GBs and resetting them could take a significant amount of time. Because of that, the kernel performs the reset in a loop through a kernel thread ksgxd() at early boot, and there's a cond_resched() after resetting each EPC page.
This is fine in most cases, but becomes a problem when there's other kernel code waiting for an RCU-Tasks grace period but the cond_resched() in ksgxd() never triggers rescheduling. Because cond_resched() doesn't report a quiescent state when it doesn't trigger rescheduling, the thread that is waiting for an RCU-Tasks grace period will wait until all EPC pages are reset.
For instance, BPF LSM subsystem can invoke synchronize_rcu_tasks() at kernel boot time. A VM with a large EPC assigned and BPF LSM enabled can take a long time to boot, with a call trace triggered:
rcu_tasks_wait_gp: rcu_tasks grace period number 1 (since boot) is
130631 jiffies old.
INFO: task systemd:1 blocked for more than 122 seconds.
...
task:systemd state:D stack:0 pid:1 tpid:1 ppid:0 flags:0x00000002
Call Trace:
...
schedule_timeout+0x157/0x170
wait_for_completion+0x88/0x150
__wait_rcu_gp+0x17e/0x190
synchronize_rcu_tasks_generic+0x64/0x60
...
synchronize_rcu_tasks+0x15/0x20
register_ftrace_direct+0x31f/0x350
...
bpf_trampoline_link_prog+0x33/0x60
bpf_tracing_prog_attach+0x3c5/0x5f0Replace cond_resched() with cond_resched_tasks_rcu_qs() which explicitly reports quiescent state regardless of whether actual rescheduling is triggered. Resetting all EPC pages in ksgxd() isn't performance critical so the extra cost of cond_resched_tasks_rcu_qs() isn't a problem.
Tests showed this reduced the VM kernel boot time from ~50s to ~700ms.
--- arch/x86/kernel/cpu/sgx/main.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-)
arch/x86/kernel/cpu/sgx/main.c +7 -1
@@ -106,7 +106,13 @@ static unsigned long __sgx_sanitize_pages(struct list_head *dirty_page_list) left_dirty++; } -cond_resched (); +/* +* cond_resched() only schedules when TIF_NEED_RESCHED is set. +* During this boot-time loop that condition may not happen for a +* long time, so report an RCU-Tasks quiescent state explicitly. +* Therefore, change cond_resched() to cond_resched_tasks_rcu_qs(). +*/ +cond_resched_tasks_rcu_qs (); }list_splice (&dirty, dirty_page_list);
arch/x86/kernel/cpu/sgx/main.c static unsigned long __sgx_sanitize_pages(struct list_head *dirty_page_list) · line 116
Quoted 9 lines · Jun Miao
arch/x86/kernel/cpu/sgx/main.c +7
-1-cond_resched (); +/* +* cond_resched() only schedules when TIF_NEED_RESCHED is set. +* During this boot-time loop that condition may not happen for a +* long time, so report an RCU-Tasks quiescent state explicitly. +* Therefore, change cond_resched() to cond_resched_tasks_rcu_qs(). +*/ +cond_resched_tasks_rcu_qs (); }
I dropped this from x86/sgx for the moment. That comment reads like a changelog and needs at least a minimal fixup. I'll look at it after I get the rest of the merge window work done.