* [PATCH] x86/sgx: Periodically yield in EPC sanitization to unblock rcu_tasks GP
@ 2026-06-18 10:04 Jun Miao
2026-06-19 0:41 ` Jarkko Sakkinen
2026-06-19 4:08 ` Huang, Kai
0 siblings, 2 replies; 4+ messages in thread
From: Jun Miao @ 2026-06-18 10:04 UTC (permalink / raw)
To: jarkko, dave.hansen
Cc: linux-sgx, linux-kernel, fan.du, challvy.tee, jun.miao
During early boot, ksgxd (Intel Software Guard Extensions Kernel Thread)
iterates over all post-kexec dirty EPC pages in a tight loop calling
cond_resched() after each page. But, on isolated CPUs
(a common configuration in cloud VMs), cond_resched() never triggers a
real context switch because TIF_NEED_RESCHED is not set when no competing
runnable task exists on that CPU.
synchronize_rcu_tasks(), invoked by BPF LSM during initialization, must
wait for every task that was running at the start of the grace period to
pass through a quiescent state (a voluntary sleep or preemption point).
If ksgxd never leaves the CPU, the rcu_tasks grace period stalls, causing
boot delays exceeding 60 seconds on machines with large EPC regions.
Fix this by introducing SGX_SANITIZE_RESCHED_INTERVAL (32768) and forcing
ksgxd to sleep for one jiffy every that many pages, guaranteeing that an
rcu_tasks quiescent state is reached in bounded time regardless of CPU
isolation. Keep cond_resched() for all other iterations.
Without this patch, instead, virtual machines (VMs) experience a long OS boot times:
[ 4.110549] systemd[1]: Detected architecture x86-64.
[ 4.115279] systemd[1]: Hostname set to <i2bp1g0g0m0i8406er0g1zX2>.
[ 4.115554] systemd[1]: Installed transient /etc/machine-id file.
[ 14.262158] rcu_tasks_wait_gp: rcu_tasks grace period number 1 (since boot) is 10087 jiffies old.
[ 14.374158] rcu_tasks_wait_gp: rcu_tasks grace period number 1 (since boot) is 40199 jiffies old.
[ 134.806157] rcu_tasks_wait_gp: rcu_tasks grace period number 1 (since boot) is 130631 jiffies old.
[ 248.086158] INFO: task systemd:1 blocked for more than 122 seconds.
[ 248.086491] Not tainted 6.8.0-90-generic #91-Ubuntu
[ 248.086739] 'echo 0 > /proc/sys/kernel/hung_task_timeout_secs' disables this message.
[ 248.086993] task:systemd state:D stack:0 pid:1 tpid:1 ppid:0 flags:0x00000002
[ 248.087274] Call Trace:
[ 248.087434] <TASK>
[ 248.087557] __schedule+0x27c/0x6b0
[ 248.087770] schedule+0x33/0x110
[ 248.087939] schedule_timeout+0x157/0x170
[ 248.088120] wait_for_completion+0x88/0x150
[ 248.088304] __wait_rcu_gp+0x17e/0x190
[ 248.088481] synchronize_rcu_tasks_generic+0x64/0x60
[ 248.088672] ? __pfx_call_rcu_tasks+0x10/0x10
[ 248.088858] ? __pfx_wakeme_after_rcu+0x10/0x10
[ 248.089047] synchronize_rcu_tasks+0x15/0x20
[ 248.089260] register_ftrace_direct+0x31f/0x350
[ 248.089445] ? __pfx_bpf_lsm_file_open+0x10/0x10
[ 248.089629] bpf_trampoline_update+0x469/0x650
[ 248.089814] ? 0xffffffffffffffff
[ 248.089988] ? 0xffffffffffffffff
[ 248.090153] __bpf_trampoline_link_prog+0x10d/0x330
[ 248.090339] bpf_trampoline_link_prog+0x33/0x60
[ 248.090518] bpf_tracing_prog_attach+0x3c5/0x5f0
[ 248.090699] link_create+0x1a5/0x280
[ 248.090886] ? security_bpf+0x3c/0x70
[ 248.091101] __sys_bpf+0x4ae/0x10
[ 248.091312] __x64_sys_bpf+0x1a/0x30
[ 248.091477] x64_sys_call+0x199/0x250
[ 248.091647] do_syscall_64+0x7f/0x180
[ 248.091818] ? arch_exit_to_user_mode_prepare.isa.0+0x1a/0x60
[ 248.092022] ? irqentry_exit_to_user_mode+0x38/0x1e0
[ 248.092246] ? irqentry_exit+0x43/0x50
[ 248.092401] entry_SYSCALL_64_after_hwframe+0x78/0x80
[ 248.092590] RIP: 0033:0x7b53e592728d
[ 248.092756] RSP: 002b:00007ffdaa9d696 EFLAGS: 00000246 ORIG_RAX: 0000000000000141
[ 248.092856] RAX: ffffffffffffffda RBX: 00007ffdaa9d696 RCX: 00007b53e592728d
[ 248.092956] RDX: 0000000000000000 RSI: 00007ffdaa9d696 RDI: 0000000000000001
[ 248.093056] RBP: 00007ffdaa9d696 R08: 00007b53e5a03a8 R09: 00007ffdaa9d696
[ 248.093156] R10: 0000000000000000 R11: 0000000000000000 R12: 0000000000000000
[ 248.093256] R13: 0000000000000000 R14: 00005d81ed2cfd0 R15: 00005d81ed2b7ec0
[ 248.093406] </TASK>
Reported-by: challvy <challvy.tee@gmail.com>
Link: https://github.com/systemd/systemd/issues/40423
Fixes: e7e0545299d8 ("x86/sgx: Initialize metadata for Enclave Page Cache (EPC) sections")
Co-developed-by: Fan Du <fan.du@intel.com>
Signed-off-by: Fan Du <fan.du@intel.com>
Signed-off-by: Jun Miao <jun.miao@intel.com>
---
arch/x86/kernel/cpu/sgx/main.c | 22 ++++++++++++++++++++--
1 file changed, 20 insertions(+), 2 deletions(-)
diff --git a/arch/x86/kernel/cpu/sgx/main.c b/arch/x86/kernel/cpu/sgx/main.c
index 4505f808af5e..4642d2d47186 100644
--- a/arch/x86/kernel/cpu/sgx/main.c
+++ b/arch/x86/kernel/cpu/sgx/main.c
@@ -52,6 +52,13 @@ static struct sgx_numa_node *sgx_numa_nodes;
static LIST_HEAD(sgx_dirty_page_list);
+/*
+ * Force a voluntary context switch every SGX_SANITIZE_RESCHED_INTERVAL
+ * iterations to let synchronize_rcu_tasks() (e.g. called by BPF LSM at
+ * init) complete its grace period.
+ */
+#define SGX_SANITIZE_RESCHED_INTERVAL (1 << 15)
+
/*
* Reset post-kexec EPC pages to the uninitialized state. The pages are removed
* from the input list, and made available for the page allocator. SECS pages
@@ -63,6 +70,7 @@ static LIST_HEAD(sgx_dirty_page_list);
static unsigned long __sgx_sanitize_pages(struct list_head *dirty_page_list)
{
unsigned long left_dirty = 0;
+ unsigned long count = 0;
struct sgx_epc_page *page;
LIST_HEAD(dirty);
int ret;
@@ -72,6 +80,18 @@ static unsigned long __sgx_sanitize_pages(struct list_head *dirty_page_list)
if (kthread_should_stop())
return 0;
+ /*
+ * On isolated CPUs cond_resched() does not trigger a real
+ * context switch when no competing runnable task exists.
+ * Periodically force ksgxd to sleep so that synchronize_rcu_tasks()
+ * (e.g. BPF LSM) can complete the grace period in bounded time.
+ * Keep cond_resched() between forced sleeps for higher-priority tasks.
+ */
+ if (!(++count & (SGX_SANITIZE_RESCHED_INTERVAL - 1)))
+ schedule_timeout_interruptible(1);
+ else
+ cond_resched();
+
page = list_first_entry(dirty_page_list, struct sgx_epc_page, list);
/*
@@ -105,8 +125,6 @@ static unsigned long __sgx_sanitize_pages(struct list_head *dirty_page_list)
list_move_tail(&page->list, &dirty);
left_dirty++;
}
-
- cond_resched();
}
list_splice(&dirty, dirty_page_list);
--
2.43.0
^ permalink raw reply related [flat|nested] 4+ messages in thread* Re: [PATCH] x86/sgx: Periodically yield in EPC sanitization to unblock rcu_tasks GP
2026-06-18 10:04 [PATCH] x86/sgx: Periodically yield in EPC sanitization to unblock rcu_tasks GP Jun Miao
@ 2026-06-19 0:41 ` Jarkko Sakkinen
2026-06-19 4:08 ` Huang, Kai
1 sibling, 0 replies; 4+ messages in thread
From: Jarkko Sakkinen @ 2026-06-19 0:41 UTC (permalink / raw)
To: Jun Miao; +Cc: dave.hansen, linux-sgx, linux-kernel, fan.du, challvy.tee
On Thu, Jun 18, 2026 at 06:04:32PM +0800, Jun Miao wrote:
> During early boot, ksgxd (Intel Software Guard Extensions Kernel Thread)
> iterates over all post-kexec dirty EPC pages in a tight loop calling
> cond_resched() after each page. But, on isolated CPUs
> (a common configuration in cloud VMs), cond_resched() never triggers a
> real context switch because TIF_NEED_RESCHED is not set when no competing
> runnable task exists on that CPU.
>
> synchronize_rcu_tasks(), invoked by BPF LSM during initialization, must
> wait for every task that was running at the start of the grace period to
> pass through a quiescent state (a voluntary sleep or preemption point).
> If ksgxd never leaves the CPU, the rcu_tasks grace period stalls, causing
> boot delays exceeding 60 seconds on machines with large EPC regions.
>
> Fix this by introducing SGX_SANITIZE_RESCHED_INTERVAL (32768) and forcing
> ksgxd to sleep for one jiffy every that many pages, guaranteeing that an
> rcu_tasks quiescent state is reached in bounded time regardless of CPU
> isolation. Keep cond_resched() for all other iterations.
>
> Without this patch, instead, virtual machines (VMs) experience a long OS boot times:
>
> [ 4.110549] systemd[1]: Detected architecture x86-64.
> [ 4.115279] systemd[1]: Hostname set to <i2bp1g0g0m0i8406er0g1zX2>.
> [ 4.115554] systemd[1]: Installed transient /etc/machine-id file.
> [ 14.262158] rcu_tasks_wait_gp: rcu_tasks grace period number 1 (since boot) is 10087 jiffies old.
> [ 14.374158] rcu_tasks_wait_gp: rcu_tasks grace period number 1 (since boot) is 40199 jiffies old.
> [ 134.806157] rcu_tasks_wait_gp: rcu_tasks grace period number 1 (since boot) is 130631 jiffies old.
> [ 248.086158] INFO: task systemd:1 blocked for more than 122 seconds.
> [ 248.086491] Not tainted 6.8.0-90-generic #91-Ubuntu
> [ 248.086739] 'echo 0 > /proc/sys/kernel/hung_task_timeout_secs' disables this message.
> [ 248.086993] task:systemd state:D stack:0 pid:1 tpid:1 ppid:0 flags:0x00000002
> [ 248.087274] Call Trace:
> [ 248.087434] <TASK>
> [ 248.087557] __schedule+0x27c/0x6b0
> [ 248.087770] schedule+0x33/0x110
> [ 248.087939] schedule_timeout+0x157/0x170
> [ 248.088120] wait_for_completion+0x88/0x150
> [ 248.088304] __wait_rcu_gp+0x17e/0x190
> [ 248.088481] synchronize_rcu_tasks_generic+0x64/0x60
> [ 248.088672] ? __pfx_call_rcu_tasks+0x10/0x10
> [ 248.088858] ? __pfx_wakeme_after_rcu+0x10/0x10
> [ 248.089047] synchronize_rcu_tasks+0x15/0x20
> [ 248.089260] register_ftrace_direct+0x31f/0x350
> [ 248.089445] ? __pfx_bpf_lsm_file_open+0x10/0x10
> [ 248.089629] bpf_trampoline_update+0x469/0x650
> [ 248.089814] ? 0xffffffffffffffff
> [ 248.089988] ? 0xffffffffffffffff
> [ 248.090153] __bpf_trampoline_link_prog+0x10d/0x330
> [ 248.090339] bpf_trampoline_link_prog+0x33/0x60
> [ 248.090518] bpf_tracing_prog_attach+0x3c5/0x5f0
> [ 248.090699] link_create+0x1a5/0x280
> [ 248.090886] ? security_bpf+0x3c/0x70
> [ 248.091101] __sys_bpf+0x4ae/0x10
> [ 248.091312] __x64_sys_bpf+0x1a/0x30
> [ 248.091477] x64_sys_call+0x199/0x250
> [ 248.091647] do_syscall_64+0x7f/0x180
> [ 248.091818] ? arch_exit_to_user_mode_prepare.isa.0+0x1a/0x60
> [ 248.092022] ? irqentry_exit_to_user_mode+0x38/0x1e0
> [ 248.092246] ? irqentry_exit+0x43/0x50
> [ 248.092401] entry_SYSCALL_64_after_hwframe+0x78/0x80
> [ 248.092590] RIP: 0033:0x7b53e592728d
> [ 248.092756] RSP: 002b:00007ffdaa9d696 EFLAGS: 00000246 ORIG_RAX: 0000000000000141
> [ 248.092856] RAX: ffffffffffffffda RBX: 00007ffdaa9d696 RCX: 00007b53e592728d
> [ 248.092956] RDX: 0000000000000000 RSI: 00007ffdaa9d696 RDI: 0000000000000001
> [ 248.093056] RBP: 00007ffdaa9d696 R08: 00007b53e5a03a8 R09: 00007ffdaa9d696
> [ 248.093156] R10: 0000000000000000 R11: 0000000000000000 R12: 0000000000000000
> [ 248.093256] R13: 0000000000000000 R14: 00005d81ed2cfd0 R15: 00005d81ed2b7ec0
> [ 248.093406] </TASK>
>
> Reported-by: challvy <challvy.tee@gmail.com>
> Link: https://github.com/systemd/systemd/issues/40423
> Fixes: e7e0545299d8 ("x86/sgx: Initialize metadata for Enclave Page Cache (EPC) sections")
> Co-developed-by: Fan Du <fan.du@intel.com>
> Signed-off-by: Fan Du <fan.du@intel.com>
> Signed-off-by: Jun Miao <jun.miao@intel.com>
> ---
> arch/x86/kernel/cpu/sgx/main.c | 22 ++++++++++++++++++++--
> 1 file changed, 20 insertions(+), 2 deletions(-)
>
> diff --git a/arch/x86/kernel/cpu/sgx/main.c b/arch/x86/kernel/cpu/sgx/main.c
> index 4505f808af5e..4642d2d47186 100644
> --- a/arch/x86/kernel/cpu/sgx/main.c
> +++ b/arch/x86/kernel/cpu/sgx/main.c
> @@ -52,6 +52,13 @@ static struct sgx_numa_node *sgx_numa_nodes;
>
> static LIST_HEAD(sgx_dirty_page_list);
>
> +/*
> + * Force a voluntary context switch every SGX_SANITIZE_RESCHED_INTERVAL
> + * iterations to let synchronize_rcu_tasks() (e.g. called by BPF LSM at
> + * init) complete its grace period.
> + */
> +#define SGX_SANITIZE_RESCHED_INTERVAL (1 << 15)
nit: maybe SGX_SANITIZE_PAGES_RECHED_INTERVAL
> +
> /*
> * Reset post-kexec EPC pages to the uninitialized state. The pages are removed
> * from the input list, and made available for the page allocator. SECS pages
> @@ -63,6 +70,7 @@ static LIST_HEAD(sgx_dirty_page_list);
> static unsigned long __sgx_sanitize_pages(struct list_head *dirty_page_list)
> {
> unsigned long left_dirty = 0;
> + unsigned long count = 0;
> struct sgx_epc_page *page;
> LIST_HEAD(dirty);
> int ret;
nit: reverse xmas tree order
> @@ -72,6 +80,18 @@ static unsigned long __sgx_sanitize_pages(struct list_head *dirty_page_list)
> if (kthread_should_stop())
> return 0;
>
> + /*
> + * On isolated CPUs cond_resched() does not trigger a real
> + * context switch when no competing runnable task exists.
> + * Periodically force ksgxd to sleep so that synchronize_rcu_tasks()
> + * (e.g. BPF LSM) can complete the grace period in bounded time.
> + * Keep cond_resched() between forced sleeps for higher-priority tasks.
> + */
> + if (!(++count & (SGX_SANITIZE_RESCHED_INTERVAL - 1)))
> + schedule_timeout_interruptible(1);
> + else
> + cond_resched();
> +
> page = list_first_entry(dirty_page_list, struct sgx_epc_page, list);
>
> /*
> @@ -105,8 +125,6 @@ static unsigned long __sgx_sanitize_pages(struct list_head *dirty_page_list)
> list_move_tail(&page->list, &dirty);
> left_dirty++;
> }
> -
> - cond_resched();
> }
>
> list_splice(&dirty, dirty_page_list);
> --
> 2.43.0
>
>
BR, Jarkko
^ permalink raw reply [flat|nested] 4+ messages in thread* Re: [PATCH] x86/sgx: Periodically yield in EPC sanitization to unblock rcu_tasks GP
2026-06-18 10:04 [PATCH] x86/sgx: Periodically yield in EPC sanitization to unblock rcu_tasks GP Jun Miao
2026-06-19 0:41 ` Jarkko Sakkinen
@ 2026-06-19 4:08 ` Huang, Kai
2026-06-22 4:14 ` Miao, Jun
1 sibling, 1 reply; 4+ messages in thread
From: Huang, Kai @ 2026-06-19 4:08 UTC (permalink / raw)
To: Miao, Jun, jarkko@kernel.org, dave.hansen@linux.intel.com
Cc: linux-sgx@vger.kernel.org, Du, Fan, challvy.tee@gmail.com,
linux-kernel@vger.kernel.org
On Thu, 2026-06-18 at 18:04 +0800, Jun Miao wrote:
> During early boot, ksgxd (Intel Software Guard Extensions Kernel Thread)
> iterates over all post-kexec dirty EPC pages in a tight loop calling
> cond_resched() after each page. But, on isolated CPUs
> (a common configuration in cloud VMs), cond_resched() never triggers a
> real context switch because TIF_NEED_RESCHED is not set when no competing
> runnable task exists on that CPU.
>
> synchronize_rcu_tasks(), invoked by BPF LSM during initialization, must
> wait for every task that was running at the start of the grace period to
> pass through a quiescent state (a voluntary sleep or preemption point).
> If ksgxd never leaves the CPU, the rcu_tasks grace period stalls, causing
> boot delays exceeding 60 seconds on machines with large EPC regions.
>
> Fix this by introducing SGX_SANITIZE_RESCHED_INTERVAL (32768) and forcing
> ksgxd to sleep for one jiffy every that many pages, guaranteeing that an
> rcu_tasks quiescent state is reached in bounded time regardless of CPU
> isolation. Keep cond_resched() for all other iterations.
>
> Without this patch, instead, virtual machines (VMs) experience a long OS boot times:
This seems a common problem at the scheduler and RCU layer, but not specific to
SGX. I originally thought cond_resched() should be able to handle this, but
after reading some history it is not the case.
IIUC, cond_resched_tasks_rcu_qs() was specifically introduced to to address this
issue. See bde6c3aa9930 ("rcu: Provide cond_resched_rcu_qs() to force quiescent
states in long loops").
Could you try this solution?
^ permalink raw reply [flat|nested] 4+ messages in thread* RE: [PATCH] x86/sgx: Periodically yield in EPC sanitization to unblock rcu_tasks GP
2026-06-19 4:08 ` Huang, Kai
@ 2026-06-22 4:14 ` Miao, Jun
0 siblings, 0 replies; 4+ messages in thread
From: Miao, Jun @ 2026-06-22 4:14 UTC (permalink / raw)
To: Huang, Kai, jarkko@kernel.org, dave.hansen@linux.intel.com
Cc: linux-sgx@vger.kernel.org, Du, Fan, challvy.tee@gmail.com,
linux-kernel@vger.kernel.org
>This seems a common problem at the scheduler and RCU layer, but not specific to
>SGX. I originally thought cond_resched() should be able to handle this, but after
>reading some history it is not the case.
>
>IIUC, cond_resched_tasks_rcu_qs() was specifically introduced to to address this
>issue. See bde6c3aa9930 ("rcu: Provide cond_resched_rcu_qs() to force quiescent
>states in long loops").
>
>Could you try this solution?
Hi, Kai
The result is effective, and I will adopt this more common method you suggested by, submit the v2 version.
Thanks Challvy Tee for validate it as follow:
We have tested the suggested fix (replacing cond_resched() with cond_resched_tasks_rcu_qs()) on our production environment and can confirm it resolves the issue.
Test environment:
- Instance type: ecs.r9it.16xlarge (SGX-enabled cloud VM with isolated CPUs) on ECS
- Kernel: 6.6.102-cbp.git.772159d5f.alnx4.x86_64 (PREEMPT_DYNAMIC)
Results:
- Before fix: boot time ~50s (with rcu_tasks grace period stall)
- After fix: boot time ~10.7s (systemd-analyze: 724ms kernel + 1.575s initrd + 8.481s userspace = 10.782s)
```
# uname -a
Linux iZbp1cp63g07kzbp93uzrhZ 6.6.102-cbp.git.772159d5f.alnx4.x86_64 #1 SMP PREEMPT_DYNAMIC Mon Jun 22 03:10:04 UTC 2026 x86_64 x86_64 x86_64 GNU/Linux
# free -h
total used free shared buff/cache available
Mem: 243Gi 4.5Gi 239Gi 2.3Mi 481Mi 239Gi
Swap: 0B 0B 0B
# systemd-analyze
Startup finished in 724ms (kernel) + 1.575s (initrd) + 8.481s (userspace) = 10.782s
multi-user.target reached after 3.939s in userspace.
# systemd-analyze blame | head
5.416s kdump.service
2.743s sys-devices-pci0000:00-0000:00:05.0-virtio1-net-eth0.device
2.743s sys-subsystem-net-devices-eth0.device
2.534s sys-devices-pci0000:00-0000:00:03.0-virtio0-virtio\x2dports-vport0p1.device
2.534s dev-virtio\x2dports-org.qemu.guest_agent.0.device
2.534s dev-vport0p1.device
2.512s sys-module-configfs.device
2.510s dev-ttyS0.device
2.510s sys-devices-pnp0-00:00-tty-ttyS0.device
2.509s sys-devices-platform-serial8250-tty-ttyS1.device
# grep -o 'sgx[^ ]*' /proc/cpuinfo | sort -u
sgx
sgx_lc
# dmesg | grep -i sgx
[ 0.355547] sgx: EPC section 0x4040000000-0x7e3fffffff
# ls -la /dev/sgx*
crw-rw---- 1 root sgx 10, 125 Jun 22 11:36 /dev/sgx_enclave
crw------- 1 root root 10, 126 Jun 22 11:36 /dev/sgx_provision
Warm regards
Jun Miao
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2026-06-22 4:14 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-06-18 10:04 [PATCH] x86/sgx: Periodically yield in EPC sanitization to unblock rcu_tasks GP Jun Miao
2026-06-19 0:41 ` Jarkko Sakkinen
2026-06-19 4:08 ` Huang, Kai
2026-06-22 4:14 ` Miao, Jun
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox