Linux-Sgx Archive mirror
 help / color / mirror / Atom feed
From: Jarkko Sakkinen <jarkko@kernel.org>
To: linux-sgx@vger.kernel.org
Cc: Haitao Huang <haitao.huang@linux.intel.com>,
	Vijay Dhanraj <vijay.dhanraj@intel.com>,
	Reinette Chatre <reinette.chatre@intel.com>,
	Dave Hansen <dave.hansen@linux.intel.com>,
	Kai Huang <kai.huang@intel.com>,
	Jarkko Sakkinen <jarkko@kernel.org>,
	Thomas Gleixner <tglx@linutronix.de>,
	Ingo Molnar <mingo@redhat.com>, Borislav Petkov <bp@alien8.de>,
	x86@kernel.org (maintainer:X86 ARCHITECTURE (32-BIT AND 64-BIT)),
	"H. Peter Anvin" <hpa@zytor.com>,
	linux-kernel@vger.kernel.org (open list:X86 ARCHITECTURE (32-BIT
	AND 64-BIT))
Subject: [PATCH RFC] x86/sgx: Use a heap allocated list head for unsanitized pages
Date: Tue,  6 Sep 2022 06:12:30 +0300	[thread overview]
Message-ID: <20220906031230.107108-1-jarkko@kernel.org> (raw)

Allocate the list head for the unsanitized pages from heap, and transfer
its to ownership to ksgxd, which takes care of destroying it. Remove
sgx_dirty_page_list, as a global list is no longer required.

Signed-off-by: Jarkko Sakkinen <jarkko@kernel.org>
---
Depends on https://lore.kernel.org/linux-sgx/20220906000221.34286-1-jarkko@kernel.org/T/#t
Would this be plausible?
---
 arch/x86/kernel/cpu/sgx/main.c | 44 ++++++++++++++++++++++++----------
 1 file changed, 31 insertions(+), 13 deletions(-)

diff --git a/arch/x86/kernel/cpu/sgx/main.c b/arch/x86/kernel/cpu/sgx/main.c
index 0aad028f04d4..6d0e38078d28 100644
--- a/arch/x86/kernel/cpu/sgx/main.c
+++ b/arch/x86/kernel/cpu/sgx/main.c
@@ -43,8 +43,6 @@ static nodemask_t sgx_numa_mask;
  */
 static struct sgx_numa_node *sgx_numa_nodes;
 
-static LIST_HEAD(sgx_dirty_page_list);
-
 /*
  * 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
@@ -392,16 +390,23 @@ void sgx_reclaim_direct(void)
 		sgx_reclaim_pages();
 }
 
-static int ksgxd(void *p)
+/*
+ * The page list head must be allocated from the heap, and its ownership is
+ * transferred to ksgxd, which takes care of destroying it.
+ */
+static int ksgxd(void *page_list_ptr)
 {
+	struct list_head *page_list = page_list_ptr;
+
 	set_freezable();
 
 	/*
 	 * Sanitize pages in order to recover from kexec(). The 2nd pass is
 	 * required for SECS pages, whose child pages blocked EREMOVE.
 	 */
-	__sgx_sanitize_pages(&sgx_dirty_page_list);
-	WARN_ON(__sgx_sanitize_pages(&sgx_dirty_page_list));
+	__sgx_sanitize_pages(page_list);
+	WARN_ON(__sgx_sanitize_pages(page_list));
+	kfree(page_list);
 
 	while (!kthread_should_stop()) {
 		if (try_to_freeze())
@@ -420,11 +425,11 @@ static int ksgxd(void *p)
 	return 0;
 }
 
-static bool __init sgx_page_reclaimer_init(void)
+static bool __init sgx_page_reclaimer_init(struct list_head *page_list)
 {
 	struct task_struct *tsk;
 
-	tsk = kthread_run(ksgxd, NULL, "ksgxd");
+	tsk = kthread_run(ksgxd, page_list, "ksgxd");
 	if (IS_ERR(tsk))
 		return false;
 
@@ -619,7 +624,8 @@ void sgx_free_epc_page(struct sgx_epc_page *page)
 
 static bool __init sgx_setup_epc_section(u64 phys_addr, u64 size,
 					 unsigned long index,
-					 struct sgx_epc_section *section)
+					 struct sgx_epc_section *section,
+					 struct list_head *page_list)
 {
 	unsigned long nr_pages = size >> PAGE_SHIFT;
 	unsigned long i;
@@ -643,7 +649,7 @@ static bool __init sgx_setup_epc_section(u64 phys_addr, u64 size,
 		section->pages[i].flags = 0;
 		section->pages[i].owner = NULL;
 		section->pages[i].poison = 0;
-		list_add_tail(&section->pages[i].list, &sgx_dirty_page_list);
+		list_add_tail(&section->pages[i].list, page_list);
 	}
 
 	return true;
@@ -784,7 +790,7 @@ static void __init arch_update_sysfs_visibility(int nid)
 static void __init arch_update_sysfs_visibility(int nid) {}
 #endif
 
-static bool __init sgx_page_cache_init(void)
+static bool __init sgx_page_cache_init(struct list_head *page_list)
 {
 	u32 eax, ebx, ecx, edx, type;
 	u64 pa, size;
@@ -812,7 +818,7 @@ static bool __init sgx_page_cache_init(void)
 
 		pr_info("EPC section 0x%llx-0x%llx\n", pa, pa + size - 1);
 
-		if (!sgx_setup_epc_section(pa, size, i, &sgx_epc_sections[i])) {
+		if (!sgx_setup_epc_section(pa, size, i, &sgx_epc_sections[i], page_list)) {
 			pr_err("No free memory for an EPC section\n");
 			break;
 		}
@@ -912,20 +918,32 @@ EXPORT_SYMBOL_GPL(sgx_set_attribute);
 
 static int __init sgx_init(void)
 {
+	struct list_head *page_list;
 	int ret;
 	int i;
 
 	if (!cpu_feature_enabled(X86_FEATURE_SGX))
 		return -ENODEV;
 
-	if (!sgx_page_cache_init())
+	page_list = kzalloc(sizeof(*page_list), GFP_KERNEL);
+	if (!page_list)
 		return -ENOMEM;
 
-	if (!sgx_page_reclaimer_init()) {
+	INIT_LIST_HEAD(page_list);
+
+	if (!sgx_page_cache_init(page_list)) {
+		kfree(page_list);
+		return -ENOMEM;
+	}
+
+	if (!sgx_page_reclaimer_init(page_list)) {
+		kfree(page_list);
 		ret = -ENOMEM;
 		goto err_page_cache;
 	}
 
+	/* page_list is now owned by ksgxd. */
+
 	ret = misc_register(&sgx_dev_provision);
 	if (ret)
 		goto err_kthread;
-- 
2.37.2


             reply	other threads:[~2022-09-06  3:12 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-09-06  3:12 Jarkko Sakkinen [this message]
2022-09-06  3:20 ` [PATCH RFC] x86/sgx: Use a heap allocated list head for unsanitized pages Dave Hansen
2022-09-06  3:52   ` Jarkko Sakkinen

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=20220906031230.107108-1-jarkko@kernel.org \
    --to=jarkko@kernel.org \
    --cc=bp@alien8.de \
    --cc=dave.hansen@linux.intel.com \
    --cc=haitao.huang@linux.intel.com \
    --cc=hpa@zytor.com \
    --cc=kai.huang@intel.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-sgx@vger.kernel.org \
    --cc=mingo@redhat.com \
    --cc=reinette.chatre@intel.com \
    --cc=tglx@linutronix.de \
    --cc=vijay.dhanraj@intel.com \
    --cc=x86@kernel.org \
    /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;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).