Skip to contentSkip to conversation

x86/sgx and selftests/sgx: Support SGX2

PATCH Superseded Reinette Chatre 6 participants 155 messages Dec 1, 2021 – Mar 1, 2022 Page 2 of 2

Subsystems (3)

v1 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#151 lore raw

Re: [PATCH 24/25] x86/sgx: Free up EPC pages directly to support large page ranges

In reply to Jarkko Sakkinen

Hi Jarkko,

Quoted 16 lines · Jarkko Sakkinen
Quoted 15 lines · Reinette Chatre

The page reclaimer ensures availability of EPC pages across all enclaves. In support of this it runs independently from the individual enclaves in order to take locks from the different enclaves as it writes pages to swap.

When needing to load a page from swap an EPC page needs to be available for its contents to be loaded into. Loading an existing enclave page from swap does not reclaim EPC pages directly if none are available, instead the reclaimer is woken when the available EPC pages are found to be below a watermark.

When iterating over a large number of pages in an oversubscribed environment there is a race between the reclaimer woken up and EPC pages reclaimed fast enough for the page operations to proceed.

Quoted 6 lines
Quoted 4 lines

Instead of tuning the race between the page operations and the reclaimer the page operations instead makes sure that there are EPC pages available.

Why this needs to be part of this patch set?

When pages are modified they are required to be in the EPC and thus potentially need to be loaded from swap. When needing to modify a large number of pages in an oversubscribed environment there is a problem with the reclaimer providing free EPC pages fast enough for all the page modification operations to proceed.

What that means is that if a user attempts to modify a large range of pages in an oversubscribed environment it is likely that the operation will fail to complete but instead it would result in partial success of as many pages as was on the free list. This is because the reclaimer may not run fast enough to free up sufficient EPC pages in a dynamic way.

This becomes complicated for user space. It could increase the priority of the reclaimer but that has been found to be insufficient*. There would still not be a guarantee that after one page modification call fails enough pages would have been freed up in support of a second page modification call.

With this change it would be ensured that when pages are being modified that there are sufficient EPC pages available to support the modifications.

Reinette

* The test that follows this patch was used to explore this scenario.

#152 lore raw

PATCH 25/25 selftests/sgx: Page removal stress test

In reply to Reinette Chatre

Create enclave with additional heap that consumes all physical SGX memory and then remove it.

Depending on the available SGX memory this test could take a significant time to run (several minutes) as it (1) creates the enclave, (2) changes the type of every page to be trimmed, (3) enters the enclave once per page to run EACCEPT, before (4) the pages are finally removed.

---
 tools/testing/selftests/sgx/main.c | 98 ++++++++++++++++++++++++++++++
 1 file changed, 98 insertions(+)
tools/testing/selftests/sgx/main.c +98 -0
index 618f5ff0601b..c1495eaafe79 100644
@@ -393,7 +393,105 @@ TEST_F(enclave, unclobbered_vdso_oversubscribed)
 	EXPECT_EQ(get_op.value, MAGIC);
 	EXPECT_EEXIT(&self->run);
 	EXPECT_EQ(self->run.user_data, 0);
+}
+
+TEST_F_TIMEOUT(enclave, unclobbered_vdso_oversubscribed_remove, 900)
+{
+	struct encl_op_get_from_buf get_op;
+	struct encl_op_put_to_buf put_op;
+	struct sgx_page_modt modt_ioc;
+	struct encl_segment *heap;
+	unsigned long total_mem;
+	int ret, errno_save;
+	unsigned long addr;
+	struct encl_op_eaccept eaccept_op;
+	struct sgx_page_remove remove_ioc;
+	unsigned long i;
+
+	/*
+	 * Create enclave with additional heap that is as big as all
+	 * available physical SGX memory.
+	 */
+	total_mem = get_total_epc_mem();
+	ASSERT_NE(total_mem, 0);
+	TH_LOG("Creating an enclave with %lu bytes heap may take a while ...",
+	       total_mem);
+	ASSERT_TRUE(setup_test_encl(total_mem, &self->encl, _metadata));
+
+	memset(&self->run, 0, sizeof(self->run));
+	self->run.tcs = self->encl.encl_base;
+
+	heap = &self->encl.segment_tbl[self->encl.nr_segments - 1];
+
+	put_op.header.type = ENCL_OP_PUT_TO_BUFFER;
+	put_op.value = MAGIC;
+
+	EXPECT_EQ(ENCL_CALL(&put_op, &self->run, false), 0);
+
+	EXPECT_EEXIT(&self->run);
+	EXPECT_EQ(self->run.user_data, 0);
+
+	get_op.header.type = ENCL_OP_GET_FROM_BUFFER;
+	get_op.value = 0;
+
+	EXPECT_EQ(ENCL_CALL(&get_op, &self->run, false), 0);
+
+	EXPECT_EQ(get_op.value, MAGIC);
+	EXPECT_EEXIT(&self->run);
+	EXPECT_EQ(self->run.user_data, 0);
+
+	/* Trim  entire heap */
+	memset(&modt_ioc, 0, sizeof(modt_ioc));
+
+	modt_ioc.offset = heap->offset;
+	modt_ioc.length = heap->size;
+	modt_ioc.type = SGX_PAGE_TYPE_TRIM;
+
+	TH_LOG("Changing type of %zd bytes to trimmed may take a while ...",
+	       heap->size);
+	ret = ioctl(self->encl.fd, SGX_IOC_PAGE_MODT, &modt_ioc);
+	errno_save = ret == -1 ? errno : 0;
+
+	EXPECT_EQ(ret, 0);
+	EXPECT_EQ(errno_save, 0);
+	EXPECT_EQ(modt_ioc.result, 0);
+	EXPECT_EQ(modt_ioc.count, heap->size);
 
+	/* EACCEPT all removed pages */
+	addr = self->encl.encl_base + heap->offset;
+
+	eaccept_op.flags = SGX_SECINFO_TRIM | SGX_SECINFO_MODIFIED;
+	eaccept_op.header.type = ENCL_OP_EACCEPT;
+
+	TH_LOG("Entering enclave to run EACCEPT for each page of %zd bytes may take a while ...",
+	       heap->size);
+	for (i = 0; i < heap->size; i += 4096) {
+		eaccept_op.epc_addr = addr + i;
+		eaccept_op.ret = 0;
+
+		EXPECT_EQ(ENCL_CALL(&eaccept_op, &self->run, true), 0);
+
+		EXPECT_EEXIT(&self->run);
+		EXPECT_EQ(self->run.exception_vector, 0);
+		EXPECT_EQ(self->run.exception_error_code, 0);
+		EXPECT_EQ(self->run.exception_addr, 0);
+		EXPECT_EQ(eaccept_op.ret, 0);
+	}
+
+	/* Complete page removal */
+	memset(&remove_ioc, 0, sizeof(remove_ioc));
+
+	remove_ioc.offset = heap->offset;
+	remove_ioc.length = heap->size;
+
+	TH_LOG("Removing %zd bytes from enclave may take a while ...",
+	       heap->size);
+	ret = ioctl(self->encl.fd, SGX_IOC_PAGE_REMOVE, &remove_ioc);
+	errno_save = ret == -1 ? errno : 0;
+
+	EXPECT_EQ(ret, 0);
+	EXPECT_EQ(errno_save, 0);
+	EXPECT_EQ(remove_ioc.count, heap->size);
 }
 
 TEST_F(enclave, clobbered_vdso)
Signature · 1 line
2.25.1
#153 lore raw
In reply to Reinette Chatre
Quoted 4 lines · Reinette Chatre

* Support modifying permissions of regular enclave pages belonging to an initialized enclave. New permissions are not allowed to exceed the originally vetted permissions. Modifying permissions is accomplished with a new ioctl SGX_IOC_PAGE_MODP.

It's probably also worth noting that this effectively punts on the issue of how to allow enclaves to relax the permissions on pages, like taking a page from R=>RW, or R=>RX. RX isn't allowed unless the page was *added* originally with RX or RWX.

Since dynamically added pages start with initial RW permissions, they can *never* be RX or RWX since they did not start with execute permissions. That's a limitation, of course, but it's one that can be dealt with separately from this set.

Does that sound sane to everyone?

#154 lore raw
Quoted 12 lines · Dave Hansen
Quoted 4 lines · Reinette Chatre

* Support modifying permissions of regular enclave pages belonging to an initialized enclave. New permissions are not allowed to exceed the originally vetted permissions. Modifying permissions is accomplished with a new ioctl SGX_IOC_PAGE_MODP.

It's probably also worth noting that this effectively punts on the issue of how to allow enclaves to relax the permissions on pages, like taking a page from R=>RW, or R=>RX. RX isn't allowed unless the page was *added* originally with RX or RWX.

Quoted 6 lines

Since dynamically added pages start with initial RW permissions, they can *never* be RX or RWX since they did not start with execute permissions. That's a limitation, of course, but it's one that can be dealt with separately from this set.

Does that sound sane to everyone?

We (Enarx) need arbitrary permission modifications. But for now we can just use this patch series and patch the original permissions to be RWX on all new pages. I think that should be sufficient.