BPF List
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Jun Miao" <jun.miao@intel.com>
Cc: bpf@vger.kernel.org
Subject: Re: [PATCH v5] x86/sgx: Report RCU-Tasks quiescent state in EPC sanitization loop
Date: Fri, 03 Jul 2026 09:00:34 +0000	[thread overview]
Message-ID: <20260703090034.8EC611F000E9@smtp.kernel.org> (raw)
In-Reply-To: <20260703084810.145567-1-jun.miao@intel.com>

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:

Pre-existing issues:
- [High] The cond_resched() call in the main loop of ksgxd() was not updated to explicitly report RCU-tasks quiescent states, leaving it vulnerable to stalls.
- [High] SGX initialization error path frees EPC page arrays without removing their physical addresses from the global sgx_epc_address_space XArray, leading to Use-After-Free on hardware memory errors.
--

commit 71875f4f4ed3656160e4c510add3ac4a68c4cd86
Author: Jun Miao <jun.miao@intel.com>

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.

Link: https://github.com/systemd/systemd/issues/40423

> diff --git a/arch/x86/kernel/cpu/sgx/main.c b/arch/x86/kernel/cpu/sgx/main.c
> index dc73194416ac8..545415ee98177 100644
> --- a/arch/x86/kernel/cpu/sgx/main.c
> +++ b/arch/x86/kernel/cpu/sgx/main.c
> @@ -103,7 +103,7 @@ static unsigned long __sgx_sanitize_pages(struct list_head *dirty_page_list)
>  			left_dirty++;
>  		}
>  
> -		cond_resched();
> +		cond_resched_tasks_rcu_qs();
>  	}
>  
>  	list_splice(&dirty, dirty_page_list);

[Severity: High]
This isn't a bug introduced by this patch, but looking at the main loop
in ksgxd() in the same file:

arch/x86/kernel/cpu/sgx/main.c:ksgxd() {
...
		if (sgx_should_reclaim(SGX_NR_HIGH_PAGES))
			sgx_reclaim_pages();

		cond_resched();
...
}

Could this cond_resched() also starve RCU-tasks grace periods if
sgx_should_reclaim() remains continuously true under heavy SGX memory pressure?

Since wait_event_freezable() won't sleep in this case, would it make sense
to update this to cond_resched_tasks_rcu_qs() as well?


[Severity: High]
This is a pre-existing issue, but does the error path in sgx_init() leave
dangling pointers in the global sgx_epc_address_space XArray?

arch/x86/kernel/cpu/sgx/main.c:sgx_init() {
...
err_page_cache:
	for (i = 0; i < sgx_nr_epc_sections; i++) {
		vfree(sgx_epc_sections[i].pages);
		memunmap(sgx_epc_sections[i].virt_addr);
	}

	return ret;
}

If sgx_page_cache_init() succeeds (which populates the XArray) but a later
initialization step fails, the section->pages arrays are freed here without
being removed from the XArray.

If a hardware memory error occurs later, arch_memory_failure() looks up the
PFN via sgx_paddr_to_page():

arch/x86/kernel/cpu/sgx/main.c:sgx_paddr_to_page() {
...
	section = xa_load(&sgx_epc_address_space, paddr);
	if (!section)
		return NULL;

	return &section->pages[PFN_DOWN(paddr - section->phys_addr)];
}

Could this lead to a use-after-free in arch_memory_failure() when it
dereferences the returned page pointer?

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20260703084810.145567-1-jun.miao@intel.com?part=1

      reply	other threads:[~2026-07-03  9:00 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-07-03  8:48 [PATCH v5] x86/sgx: Report RCU-Tasks quiescent state in EPC sanitization loop Jun Miao
2026-07-03  9:00 ` sashiko-bot [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20260703090034.8EC611F000E9@smtp.kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=jun.miao@intel.com \
    --cc=sashiko-reviews@lists.linux.dev \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox