diff mbox series

Support SGX2 V5: Seg-fault with EACCEPT for large number of EPC pages

Message ID DM8PR11MB55912A7F47A84EC9913A6352F6999@DM8PR11MB5591.namprd11.prod.outlook.com (mailing list archive)
State New, archived
Headers show
Series Support SGX2 V5: Seg-fault with EACCEPT for large number of EPC pages | expand

Commit Message

Dhanraj, Vijay July 29, 2022, 4:01 p.m. UTC
Hi All,

I recently tested the V5 version of the patch with Gramine and ran into a seg-fault during EPC allocation that is `EAUG`ing via `EACCEPT`. Allocation worked fine for smaller requests and even up to 2GBs. But when I tried with 4GB allocation I got a seg-fault.
Huang, Haitao and I created a simple patch to repro this issue using the SGX selftests and we do see the issue when using V5 (5.18.0-rc5) but cannot repro the issue in V4 (5.18.0-rc2). Not sure if this is a driver issue or kernel, can you please check?

Results with V5 using modified `augment_via_eaccept` test:
#  RUN           enclave.augment_via_eaccept ...
# main.c:1135:augment_via_eaccept:test enclave: total_size = 8192, seg->size = 8192
# main.c:1135:augment_via_eaccept:test enclave: total_size = 12288, seg->size = 4096
# main.c:1135:augment_via_eaccept:test enclave: total_size = 36864, seg->size = 24576
# main.c:1135:augment_via_eaccept:test enclave: total_size = 40960, seg->size = 4096
# main.c:1153:augment_via_eaccept:mmaping pages at end of enclave...
# main.c:1167:augment_via_eaccept:Entering enclave to run EACCEPT for each page of 8589934592 bytes may take a while ...
# main.c:1184:augment_via_eaccept:Expected self->run.exception_vector (14) == 0 (0)
# main.c:1185:augment_via_eaccept:Expected self->run.exception_error_code (4) == 0 (0)
# main.c:1186:augment_via_eaccept:Expected self->run.exception_addr (140106113478656) == 0 (0)
# main.c:1188:augment_via_eaccept:Expected self->run.function (3) == EEXIT (4)
# augment_via_eaccept: Test terminated by assertion

Results with V4 using modified `augment_via_eaccept` test:
#  RUN           enclave.augment_via_eaccept ...
# main.c:1135:augment_via_eaccept:test enclave: total_size = 8192, seg->size = 8192
# main.c:1135:augment_via_eaccept:test enclave: total_size = 12288, seg->size = 4096
# main.c:1135:augment_via_eaccept:test enclave: total_size = 36864, seg->size = 24576
# main.c:1135:augment_via_eaccept:test enclave: total_size = 40960, seg->size = 4096
# main.c:1153:augment_via_eaccept:mmaping pages at end of enclave...
# main.c:1167:augment_via_eaccept:Entering enclave to run EACCEPT for each page of 8589934592 bytes may take a while ...
#            OK  enclave.augment_via_eaccept


Test Patch:

Comments

Dave Hansen July 29, 2022, 4:37 p.m. UTC | #1
On 7/29/22 09:01, Dhanraj, Vijay wrote:
> Huang, Haitao and I created a simple patch to repro this issue using the SGX selftests and we do see the issue when using V5 (5.18.0-rc5) but cannot repro the issue in V4 (5.18.0-rc2). Not sure if this is a driver issue or kernel, can you please check?

Thanks for the report.

Could you please try the code that's going to go to Linus shortly?  It's
really the only version that matters at this point?

https://git.kernel.org/pub/scm/linux/kernel/git/tip/tip.git/log/?h=x86/sgx
Dhanraj, Vijay July 29, 2022, 7:14 p.m. UTC | #2
Hi Dave,

Tried with the latest tip and I still observe the issue. I tried 2G, 4G and 8G sized requests and as before, 2GB size requests works fine but 4GB and 8GB requests failed.

Commit I have applied my test patch: 
commit 038ef9928e1acb37bbe808c23a421189f1fd96cb (origin/x86/sgx)
Merge: e0a5915f1cca e0dccc3b76fb
Author: Ingo Molnar <mingo@kernel.org>
Date:   Tue Jul 26 09:14:28 2022 +0200

    Merge tag 'v5.19-rc8' into x86/sgx, to resolve conflicts

Kernel version:
Linux sdp 5.19.0-rc8-custom #1 SMP PREEMPT_DYNAMIC Fri Jul 29 10:28:25 PDT 2022 x86_64 x86_64 x86_64 GNU/Linux

Thanks,
-Vijay


> -----Original Message-----
> From: Hansen, Dave <dave.hansen@intel.com>
> Sent: Friday, July 29, 2022 9:38 AM
> To: Dhanraj, Vijay <vijay.dhanraj@intel.com>; Chatre, Reinette
> <reinette.chatre@intel.com>; dave.hansen@linux.intel.com;
> jarkko@kernel.org; linux-sgx@vger.kernel.org
> Cc: Huang, Haitao <haitao.huang@intel.com>
> Subject: Re: Support SGX2 V5: Seg-fault with EACCEPT for large number of
> EPC pages
> 
> On 7/29/22 09:01, Dhanraj, Vijay wrote:
> > Huang, Haitao and I created a simple patch to repro this issue using the SGX
> selftests and we do see the issue when using V5 (5.18.0-rc5) but cannot
> repro the issue in V4 (5.18.0-rc2). Not sure if this is a driver issue or kernel,
> can you please check?
> 
> Thanks for the report.
> 
> Could you please try the code that's going to go to Linus shortly?  It's really
> the only version that matters at this point?
> 
> https://git.kernel.org/pub/scm/linux/kernel/git/tip/tip.git/log/?h=x86/sgx
Haitao Huang July 29, 2022, 7:37 p.m. UTC | #3
On Fri, 29 Jul 2022 14:14:15 -0500, Dhanraj, Vijay  
<vijay.dhanraj@intel.com> wrote:

> Hi Dave,
>
> Tried with the latest tip and I still observe the issue. I tried 2G, 4G  
> and 8G sized requests and as before, 2GB size requests works fine but  
> 4GB and 8GB requests failed.
>
> Commit I have applied my test patch:
> commit 038ef9928e1acb37bbe808c23a421189f1fd96cb (origin/x86/sgx)
> Merge: e0a5915f1cca e0dccc3b76fb
> Author: Ingo Molnar <mingo@kernel.org>
> Date:   Tue Jul 26 09:14:28 2022 +0200
>
>     Merge tag 'v5.19-rc8' into x86/sgx, to resolve conflicts
>
> Kernel version:
> Linux sdp 5.19.0-rc8-custom #1 SMP PREEMPT_DYNAMIC Fri Jul 29 10:28:25  
> PDT 2022 x86_64 x86_64 x86_64 GNU/Linux
>

Using a kernel built at the same commit, I did the same test on another  
machine with 4G EPC reported by the kernel log and reproduced the failures  
only on 5th run for both 4G and 8G size requests (set by edmm_size). The  
failure seems to stick for subsequent runs but I did not run more than 7  
times for each case so can't say definitely about that.

Thanks
Haitao

>
>> -----Original Message-----
>> From: Hansen, Dave <dave.hansen@intel.com>
>> Sent: Friday, July 29, 2022 9:38 AM
>> To: Dhanraj, Vijay <vijay.dhanraj@intel.com>; Chatre, Reinette
>> <reinette.chatre@intel.com>; dave.hansen@linux.intel.com;
>> jarkko@kernel.org; linux-sgx@vger.kernel.org
>> Cc: Huang, Haitao <haitao.huang@intel.com>
>> Subject: Re: Support SGX2 V5: Seg-fault with EACCEPT for large number of
>> EPC pages
>>
>> On 7/29/22 09:01, Dhanraj, Vijay wrote:
>> > Huang, Haitao and I created a simple patch to repro this issue using  
>> the SGX
>> selftests and we do see the issue when using V5 (5.18.0-rc5) but cannot
>> repro the issue in V4 (5.18.0-rc2). Not sure if this is a driver issue  
>> or kernel,
>> can you please check?
>>
>> Thanks for the report.
>>
>> Could you please try the code that's going to go to Linus shortly?   
>> It's really
>> the only version that matters at this point?
>>
>> https://git.kernel.org/pub/scm/linux/kernel/git/tip/tip.git/log/?h=x86/sgx
Jarkko Sakkinen Aug. 1, 2022, 4:46 p.m. UTC | #4
On Fri, Jul 29, 2022 at 04:01:04PM +0000, Dhanraj, Vijay wrote:
> Hi All,
> 
> I recently tested the V5 version of the patch with Gramine and ran into a seg-fault during EPC allocation that is `EAUG`ing via `EACCEPT`. Allocation worked fine for smaller requests and even up to 2GBs. But when I tried with 4GB allocation I got a seg-fault.
> Huang, Haitao and I created a simple patch to repro this issue using the SGX selftests and we do see the issue when using V5 (5.18.0-rc5) but cannot repro the issue in V4 (5.18.0-rc2). Not sure if this is a driver issue or kernel, can you please check?
> 
> Results with V5 using modified `augment_via_eaccept` test:
> #  RUN           enclave.augment_via_eaccept ...
> # main.c:1135:augment_via_eaccept:test enclave: total_size = 8192, seg->size = 8192
> # main.c:1135:augment_via_eaccept:test enclave: total_size = 12288, seg->size = 4096
> # main.c:1135:augment_via_eaccept:test enclave: total_size = 36864, seg->size = 24576
> # main.c:1135:augment_via_eaccept:test enclave: total_size = 40960, seg->size = 4096
> # main.c:1153:augment_via_eaccept:mmaping pages at end of enclave...
> # main.c:1167:augment_via_eaccept:Entering enclave to run EACCEPT for each page of 8589934592 bytes may take a while ...
> # main.c:1184:augment_via_eaccept:Expected self->run.exception_vector (14) == 0 (0)
> # main.c:1185:augment_via_eaccept:Expected self->run.exception_error_code (4) == 0 (0)
> # main.c:1186:augment_via_eaccept:Expected self->run.exception_addr (140106113478656) == 0 (0)
> # main.c:1188:augment_via_eaccept:Expected self->run.function (3) == EEXIT (4)
> # augment_via_eaccept: Test terminated by assertion
> 
> Results with V4 using modified `augment_via_eaccept` test:
> #  RUN           enclave.augment_via_eaccept ...
> # main.c:1135:augment_via_eaccept:test enclave: total_size = 8192, seg->size = 8192
> # main.c:1135:augment_via_eaccept:test enclave: total_size = 12288, seg->size = 4096
> # main.c:1135:augment_via_eaccept:test enclave: total_size = 36864, seg->size = 24576
> # main.c:1135:augment_via_eaccept:test enclave: total_size = 40960, seg->size = 4096
> # main.c:1153:augment_via_eaccept:mmaping pages at end of enclave...
> # main.c:1167:augment_via_eaccept:Entering enclave to run EACCEPT for each page of 8589934592 bytes may take a while ...
> #            OK  enclave.augment_via_eaccept
> 
> 
> Test Patch:
> diff --git a/tools/testing/selftests/sgx/load.c b/tools/testing/selftests/sgx/load.c
> index 94bdeac1cf04..7de1b15c90b1 100644
> --- a/tools/testing/selftests/sgx/load.c
> +++ b/tools/testing/selftests/sgx/load.c
> @@ -171,7 +171,8 @@ uint64_t encl_get_entry(struct encl *encl, const char *symbol)
>  	return 0;
>  }
>  
> -bool encl_load(const char *path, struct encl *encl, unsigned long heap_size)
> +bool encl_load(const char *path, struct encl *encl, unsigned long heap_size,
> +			   unsigned long edmm_size)
>  {
>  	const char device_path[] = "/dev/sgx_enclave";
>  	struct encl_segment *seg;
> @@ -300,7 +301,7 @@ bool encl_load(const char *path, struct encl *encl, unsigned long heap_size)
>  
>  	encl->src_size = encl->segment_tbl[j].offset + encl->segment_tbl[j].size;
>  
> -	for (encl->encl_size = 4096; encl->encl_size < encl->src_size; )
> +	for (encl->encl_size = 4096; encl->encl_size < encl->src_size + edmm_size;)
>  		encl->encl_size <<= 1;
>  
>  	return true;
> diff --git a/tools/testing/selftests/sgx/main.c b/tools/testing/selftests/sgx/main.c
> index 9820b3809c69..8d7ce9389c8f 100644
> --- a/tools/testing/selftests/sgx/main.c
> +++ b/tools/testing/selftests/sgx/main.c
> @@ -25,6 +25,8 @@ static const uint64_t MAGIC = 0x1122334455667788ULL;
>  static const uint64_t MAGIC2 = 0x8877665544332211ULL;
>  vdso_sgx_enter_enclave_t vdso_sgx_enter_enclave;
>  
> +static const unsigned long edmm_size = 8589934592; //8G
> +
>  /*
>   * Security Information (SECINFO) data structure needed by a few SGX
>   * instructions (eg. ENCLU[EACCEPT] and ENCLU[EMODPE]) holds meta-data
> @@ -183,7 +185,7 @@ static bool setup_test_encl(unsigned long heap_size, struct encl *encl,
>  	unsigned int i;
>  	void *addr;
>  
> -	if (!encl_load("test_encl.elf", encl, heap_size)) {
> +	if (!encl_load("test_encl.elf", encl, heap_size, edmm_size)) {
>  		encl_delete(encl);
>  		TH_LOG("Failed to load the test enclave.");
>  		return false;
> @@ -1104,14 +1106,19 @@ TEST_F(enclave, augment)
>   * Test for the addition of pages to an initialized enclave via a
>   * pre-emptive run of EACCEPT on page to be added.
>   */
> -TEST_F(enclave, augment_via_eaccept)
> +/*
> + * Test for the addition of pages to an initialized enclave via a
> + * pre-emptive run of EACCEPT on page to be added.
> + */
> +/*TEST_F(enclave, augment_via_eaccept)*/
> +TEST_F_TIMEOUT(enclave, augment_via_eaccept, 900)
>  {
>  	struct encl_op_get_from_addr get_addr_op;
>  	struct encl_op_put_to_addr put_addr_op;
>  	struct encl_op_eaccept eaccept_op;
>  	size_t total_size = 0;
>  	void *addr;
> -	int i;
> +	unsigned long i;
>  
>  	if (!sgx2_supported())
>  		SKIP(return, "SGX2 not supported");
> @@ -1125,6 +1132,7 @@ TEST_F(enclave, augment_via_eaccept)
>  		struct encl_segment *seg = &self->encl.segment_tbl[i];
>  
>  		total_size += seg->size;
> +		TH_LOG("test enclave: total_size = %ld, seg->size = %ld", total_size, seg->size);
>  	}
>  
>  	/*
> @@ -1132,7 +1140,7 @@ TEST_F(enclave, augment_via_eaccept)
>  	 * test enclave since enclave size must be a power of 2 in bytes while
>  	 * test_encl does not consume it all.
>  	 */
> -	EXPECT_LT(total_size + PAGE_SIZE, self->encl.encl_size);
> +	EXPECT_LT(total_size + edmm_size, self->encl.encl_size);
>  
>  	/*
>  	 * mmap() a page at end of existing enclave to be used for dynamic
> @@ -1142,10 +1150,10 @@ TEST_F(enclave, augment_via_eaccept)
>  	 * falls into the enclave's address range but not backed
>  	 * by existing enclave pages.
>  	 */
> -
> -	addr = mmap((void *)self->encl.encl_base + total_size, PAGE_SIZE,
> -		    PROT_READ | PROT_WRITE | PROT_EXEC, MAP_SHARED | MAP_FIXED,
> -		    self->encl.fd, 0);
> +	TH_LOG("mmaping pages at end of enclave...");
> +	addr = mmap((void *)self->encl.encl_base + total_size, edmm_size,
> +			PROT_READ | PROT_WRITE | PROT_EXEC, MAP_SHARED | MAP_FIXED,
> +			self->encl.fd, 0);
>  	EXPECT_NE(addr, MAP_FAILED);
>  
>  	self->run.exception_vector = 0;
> @@ -1156,25 +1164,29 @@ TEST_F(enclave, augment_via_eaccept)
>  	 * Run EACCEPT on new page to trigger the #PF->EAUG->EACCEPT(again
>  	 * without a #PF). All should be transparent to userspace.
>  	 */
> -	eaccept_op.epc_addr = self->encl.encl_base + total_size;
> +	TH_LOG("Entering enclave to run EACCEPT for each page of %zd bytes may take a while ...",
> +			edmm_size);
>  	eaccept_op.flags = SGX_SECINFO_R | SGX_SECINFO_W | SGX_SECINFO_REG | SGX_SECINFO_PENDING;
>  	eaccept_op.ret = 0;
>  	eaccept_op.header.type = ENCL_OP_EACCEPT;
>  
> -	EXPECT_EQ(ENCL_CALL(&eaccept_op, &self->run, true), 0);
> +	for (i = 0; i < edmm_size; i += 4096) {
> +		eaccept_op.epc_addr = (uint64_t)(addr + i);
>  
> -	if (self->run.exception_vector == 14 &&
> -	    self->run.exception_error_code == 4 &&
> -	    self->run.exception_addr == self->encl.encl_base + total_size) {
> -		munmap(addr, PAGE_SIZE);
> -		SKIP(return, "Kernel does not support adding pages to initialized enclave");
> -	}
> +		EXPECT_EQ(ENCL_CALL(&eaccept_op, &self->run, true), 0);
> +		if (self->run.exception_vector == 14 &&
> +			self->run.exception_error_code == 4 &&
> +			self->run.exception_addr == self->encl.encl_base) {
> +			munmap(addr, edmm_size);
> +			SKIP(return, "Kernel does not support adding pages to initialized enclave");
> +		}
>  
> -	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);
> +		EXPECT_EQ(self->run.exception_vector, 0);
> +		EXPECT_EQ(self->run.exception_error_code, 0);
> +		EXPECT_EQ(self->run.exception_addr, 0);
> +		ASSERT_EQ(eaccept_op.ret, 0);
> +		ASSERT_EQ(self->run.function, EEXIT);
> +	}
>  
>  	/*
>  	 * New page should be accessible from within enclave - attempt to
> @@ -1207,7 +1219,7 @@ TEST_F(enclave, augment_via_eaccept)
>  	EXPECT_EQ(self->run.exception_error_code, 0);
>  	EXPECT_EQ(self->run.exception_addr, 0);
>  
> -	munmap(addr, PAGE_SIZE);
> +	munmap(addr, edmm_size);
>  }
>  
>  /*
> diff --git a/tools/testing/selftests/sgx/main.h b/tools/testing/selftests/sgx/main.h
> index fc585be97e2f..fe5d39ac0e1e 100644
> --- a/tools/testing/selftests/sgx/main.h
> +++ b/tools/testing/selftests/sgx/main.h
> @@ -35,7 +35,8 @@ extern unsigned char sign_key[];
>  extern unsigned char sign_key_end[];
>  
>  void encl_delete(struct encl *ctx);
> -bool encl_load(const char *path, struct encl *encl, unsigned long heap_size);
> +bool encl_load(const char *path, struct encl *encl, unsigned long heap_size,
> +			   unsigned long edmm_size);
>  bool encl_measure(struct encl *encl);
>  bool encl_build(struct encl *encl);
>  uint64_t encl_get_entry(struct encl *encl, const char *symbol);
> diff --git a/tools/testing/selftests/sgx/sigstruct.c b/tools/testing/selftests/sgx/sigstruct.c
> index 50c5ab1aa6fa..6000cf0e4975 100644
> --- a/tools/testing/selftests/sgx/sigstruct.c
> +++ b/tools/testing/selftests/sgx/sigstruct.c
> @@ -343,7 +343,7 @@ bool encl_measure(struct encl *encl)
>  	if (!ctx)
>  		goto err;
>  
> -	if (!mrenclave_ecreate(ctx, encl->src_size))
> +	if (!mrenclave_ecreate(ctx, encl->encl_size))
>  		goto err;
>  
>  	for (i = 0; i < encl->nr_segments; i++) {
> -- 
> 2.17.1
> 
> 
> Thanks,
> -Vijay

Which hardware was used to produce this bug (e.g. /proc/cpuinfo)?

BR, Jarkko
Dhanraj, Vijay Aug. 1, 2022, 5:47 p.m. UTC | #5
> -----Original Message-----
> From: jarkko@kernel.org <jarkko@kernel.org>
> Sent: Monday, August 1, 2022 9:46 AM
> To: Dhanraj, Vijay <vijay.dhanraj@intel.com>
> Cc: Chatre, Reinette <reinette.chatre@intel.com>;
> dave.hansen@linux.intel.com; linux-sgx@vger.kernel.org; Huang, Haitao
> <haitao.huang@intel.com>
> Subject: Re: Support SGX2 V5: Seg-fault with EACCEPT for large number of
> EPC pages
> 
> On Fri, Jul 29, 2022 at 04:01:04PM +0000, Dhanraj, Vijay wrote:
> > Hi All,
> >
> > I recently tested the V5 version of the patch with Gramine and ran into a
> seg-fault during EPC allocation that is `EAUG`ing via `EACCEPT`. Allocation
> worked fine for smaller requests and even up to 2GBs. But when I tried with
> 4GB allocation I got a seg-fault.
> > Huang, Haitao and I created a simple patch to repro this issue using the SGX
> selftests and we do see the issue when using V5 (5.18.0-rc5) but cannot
> repro the issue in V4 (5.18.0-rc2). Not sure if this is a driver issue or kernel,
> can you please check?
> >
> > Results with V5 using modified `augment_via_eaccept` test:
> > #  RUN           enclave.augment_via_eaccept ...
> > # main.c:1135:augment_via_eaccept:test enclave: total_size = 8192,
> > seg->size = 8192 # main.c:1135:augment_via_eaccept:test enclave:
> > total_size = 12288, seg->size = 4096 #
> > main.c:1135:augment_via_eaccept:test enclave: total_size = 36864,
> > seg->size = 24576 # main.c:1135:augment_via_eaccept:test enclave:
> total_size = 40960, seg->size = 4096 #
> main.c:1153:augment_via_eaccept:mmaping pages at end of enclave...
> > # main.c:1167:augment_via_eaccept:Entering enclave to run EACCEPT for
> each page of 8589934592 bytes may take a while ...
> > # main.c:1184:augment_via_eaccept:Expected self->run.exception_vector
> > (14) == 0 (0) # main.c:1185:augment_via_eaccept:Expected
> > self->run.exception_error_code (4) == 0 (0) #
> > main.c:1186:augment_via_eaccept:Expected self->run.exception_addr
> > (140106113478656) == 0 (0) # main.c:1188:augment_via_eaccept:Expected
> > self->run.function (3) == EEXIT (4) # augment_via_eaccept: Test
> > terminated by assertion
> >
> > Results with V4 using modified `augment_via_eaccept` test:
> > #  RUN           enclave.augment_via_eaccept ...
> > # main.c:1135:augment_via_eaccept:test enclave: total_size = 8192,
> > seg->size = 8192 # main.c:1135:augment_via_eaccept:test enclave:
> > total_size = 12288, seg->size = 4096 #
> > main.c:1135:augment_via_eaccept:test enclave: total_size = 36864,
> > seg->size = 24576 # main.c:1135:augment_via_eaccept:test enclave:
> total_size = 40960, seg->size = 4096 #
> main.c:1153:augment_via_eaccept:mmaping pages at end of enclave...
> > # main.c:1167:augment_via_eaccept:Entering enclave to run EACCEPT for
> each page of 8589934592 bytes may take a while ...
> > #            OK  enclave.augment_via_eaccept
> >
> >
> > Test Patch:
> > diff --git a/tools/testing/selftests/sgx/load.c
> > b/tools/testing/selftests/sgx/load.c
> > index 94bdeac1cf04..7de1b15c90b1 100644
> > --- a/tools/testing/selftests/sgx/load.c
> > +++ b/tools/testing/selftests/sgx/load.c
> > @@ -171,7 +171,8 @@ uint64_t encl_get_entry(struct encl *encl, const
> char *symbol)
> >  	return 0;
> >  }
> >
> > -bool encl_load(const char *path, struct encl *encl, unsigned long
> > heap_size)
> > +bool encl_load(const char *path, struct encl *encl, unsigned long
> heap_size,
> > +			   unsigned long edmm_size)
> >  {
> >  	const char device_path[] = "/dev/sgx_enclave";
> >  	struct encl_segment *seg;
> > @@ -300,7 +301,7 @@ bool encl_load(const char *path, struct encl
> > *encl, unsigned long heap_size)
> >
> >  	encl->src_size = encl->segment_tbl[j].offset +
> > encl->segment_tbl[j].size;
> >
> > -	for (encl->encl_size = 4096; encl->encl_size < encl->src_size; )
> > +	for (encl->encl_size = 4096; encl->encl_size < encl->src_size +
> > +edmm_size;)
> >  		encl->encl_size <<= 1;
> >
> >  	return true;
> > diff --git a/tools/testing/selftests/sgx/main.c
> > b/tools/testing/selftests/sgx/main.c
> > index 9820b3809c69..8d7ce9389c8f 100644
> > --- a/tools/testing/selftests/sgx/main.c
> > +++ b/tools/testing/selftests/sgx/main.c
> > @@ -25,6 +25,8 @@ static const uint64_t MAGIC =
> 0x1122334455667788ULL;
> > static const uint64_t MAGIC2 = 0x8877665544332211ULL;
> > vdso_sgx_enter_enclave_t vdso_sgx_enter_enclave;
> >
> > +static const unsigned long edmm_size = 8589934592; //8G
> > +
> >  /*
> >   * Security Information (SECINFO) data structure needed by a few SGX
> >   * instructions (eg. ENCLU[EACCEPT] and ENCLU[EMODPE]) holds
> > meta-data @@ -183,7 +185,7 @@ static bool setup_test_encl(unsigned
> long heap_size, struct encl *encl,
> >  	unsigned int i;
> >  	void *addr;
> >
> > -	if (!encl_load("test_encl.elf", encl, heap_size)) {
> > +	if (!encl_load("test_encl.elf", encl, heap_size, edmm_size)) {
> >  		encl_delete(encl);
> >  		TH_LOG("Failed to load the test enclave.");
> >  		return false;
> > @@ -1104,14 +1106,19 @@ TEST_F(enclave, augment)
> >   * Test for the addition of pages to an initialized enclave via a
> >   * pre-emptive run of EACCEPT on page to be added.
> >   */
> > -TEST_F(enclave, augment_via_eaccept)
> > +/*
> > + * Test for the addition of pages to an initialized enclave via a
> > + * pre-emptive run of EACCEPT on page to be added.
> > + */
> > +/*TEST_F(enclave, augment_via_eaccept)*/ TEST_F_TIMEOUT(enclave,
> > +augment_via_eaccept, 900)
> >  {
> >  	struct encl_op_get_from_addr get_addr_op;
> >  	struct encl_op_put_to_addr put_addr_op;
> >  	struct encl_op_eaccept eaccept_op;
> >  	size_t total_size = 0;
> >  	void *addr;
> > -	int i;
> > +	unsigned long i;
> >
> >  	if (!sgx2_supported())
> >  		SKIP(return, "SGX2 not supported"); @@ -1125,6 +1132,7
> @@
> > TEST_F(enclave, augment_via_eaccept)
> >  		struct encl_segment *seg = &self->encl.segment_tbl[i];
> >
> >  		total_size += seg->size;
> > +		TH_LOG("test enclave: total_size = %ld, seg->size = %ld",
> > +total_size, seg->size);
> >  	}
> >
> >  	/*
> > @@ -1132,7 +1140,7 @@ TEST_F(enclave, augment_via_eaccept)
> >  	 * test enclave since enclave size must be a power of 2 in bytes while
> >  	 * test_encl does not consume it all.
> >  	 */
> > -	EXPECT_LT(total_size + PAGE_SIZE, self->encl.encl_size);
> > +	EXPECT_LT(total_size + edmm_size, self->encl.encl_size);
> >
> >  	/*
> >  	 * mmap() a page at end of existing enclave to be used for dynamic
> > @@ -1142,10 +1150,10 @@ TEST_F(enclave, augment_via_eaccept)
> >  	 * falls into the enclave's address range but not backed
> >  	 * by existing enclave pages.
> >  	 */
> > -
> > -	addr = mmap((void *)self->encl.encl_base + total_size, PAGE_SIZE,
> > -		    PROT_READ | PROT_WRITE | PROT_EXEC, MAP_SHARED |
> MAP_FIXED,
> > -		    self->encl.fd, 0);
> > +	TH_LOG("mmaping pages at end of enclave...");
> > +	addr = mmap((void *)self->encl.encl_base + total_size, edmm_size,
> > +			PROT_READ | PROT_WRITE | PROT_EXEC,
> MAP_SHARED | MAP_FIXED,
> > +			self->encl.fd, 0);
> >  	EXPECT_NE(addr, MAP_FAILED);
> >
> >  	self->run.exception_vector = 0;
> > @@ -1156,25 +1164,29 @@ TEST_F(enclave, augment_via_eaccept)
> >  	 * Run EACCEPT on new page to trigger the #PF->EAUG-
> >EACCEPT(again
> >  	 * without a #PF). All should be transparent to userspace.
> >  	 */
> > -	eaccept_op.epc_addr = self->encl.encl_base + total_size;
> > +	TH_LOG("Entering enclave to run EACCEPT for each page of %zd
> bytes may take a while ...",
> > +			edmm_size);
> >  	eaccept_op.flags = SGX_SECINFO_R | SGX_SECINFO_W |
> SGX_SECINFO_REG | SGX_SECINFO_PENDING;
> >  	eaccept_op.ret = 0;
> >  	eaccept_op.header.type = ENCL_OP_EACCEPT;
> >
> > -	EXPECT_EQ(ENCL_CALL(&eaccept_op, &self->run, true), 0);
> > +	for (i = 0; i < edmm_size; i += 4096) {
> > +		eaccept_op.epc_addr = (uint64_t)(addr + i);
> >
> > -	if (self->run.exception_vector == 14 &&
> > -	    self->run.exception_error_code == 4 &&
> > -	    self->run.exception_addr == self->encl.encl_base + total_size) {
> > -		munmap(addr, PAGE_SIZE);
> > -		SKIP(return, "Kernel does not support adding pages to
> initialized enclave");
> > -	}
> > +		EXPECT_EQ(ENCL_CALL(&eaccept_op, &self->run, true), 0);
> > +		if (self->run.exception_vector == 14 &&
> > +			self->run.exception_error_code == 4 &&
> > +			self->run.exception_addr == self->encl.encl_base) {
> > +			munmap(addr, edmm_size);
> > +			SKIP(return, "Kernel does not support adding pages
> to initialized enclave");
> > +		}
> >
> > -	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);
> > +		EXPECT_EQ(self->run.exception_vector, 0);
> > +		EXPECT_EQ(self->run.exception_error_code, 0);
> > +		EXPECT_EQ(self->run.exception_addr, 0);
> > +		ASSERT_EQ(eaccept_op.ret, 0);
> > +		ASSERT_EQ(self->run.function, EEXIT);
> > +	}
> >
> >  	/*
> >  	 * New page should be accessible from within enclave - attempt to
> @@
> > -1207,7 +1219,7 @@ TEST_F(enclave, augment_via_eaccept)
> >  	EXPECT_EQ(self->run.exception_error_code, 0);
> >  	EXPECT_EQ(self->run.exception_addr, 0);
> >
> > -	munmap(addr, PAGE_SIZE);
> > +	munmap(addr, edmm_size);
> >  }
> >
> >  /*
> > diff --git a/tools/testing/selftests/sgx/main.h
> > b/tools/testing/selftests/sgx/main.h
> > index fc585be97e2f..fe5d39ac0e1e 100644
> > --- a/tools/testing/selftests/sgx/main.h
> > +++ b/tools/testing/selftests/sgx/main.h
> > @@ -35,7 +35,8 @@ extern unsigned char sign_key[];  extern unsigned
> > char sign_key_end[];
> >
> >  void encl_delete(struct encl *ctx);
> > -bool encl_load(const char *path, struct encl *encl, unsigned long
> > heap_size);
> > +bool encl_load(const char *path, struct encl *encl, unsigned long
> heap_size,
> > +			   unsigned long edmm_size);
> >  bool encl_measure(struct encl *encl);  bool encl_build(struct encl
> > *encl);  uint64_t encl_get_entry(struct encl *encl, const char
> > *symbol); diff --git a/tools/testing/selftests/sgx/sigstruct.c
> > b/tools/testing/selftests/sgx/sigstruct.c
> > index 50c5ab1aa6fa..6000cf0e4975 100644
> > --- a/tools/testing/selftests/sgx/sigstruct.c
> > +++ b/tools/testing/selftests/sgx/sigstruct.c
> > @@ -343,7 +343,7 @@ bool encl_measure(struct encl *encl)
> >  	if (!ctx)
> >  		goto err;
> >
> > -	if (!mrenclave_ecreate(ctx, encl->src_size))
> > +	if (!mrenclave_ecreate(ctx, encl->encl_size))
> >  		goto err;
> >
> >  	for (i = 0; i < encl->nr_segments; i++) {
> > --
> > 2.17.1
> >
> >
> > Thanks,
> > -Vijay
> 
> Which hardware was used to produce this bug (e.g. /proc/cpuinfo)?
> 
> BR, Jarkko

It is an IceLake server with 128 logical processors (64 LPs per socket). But the EPC memory was set to 2GB per socket.

Output from /proc/cpuinfo:
 
processor       : 127
vendor_id       : GenuineIntel
cpu family      : 6
model           : 106
model name      : Intel(R) Xeon(R) Platinum 8352Y CPU @ 2.20GHz
stepping        : 6
microcode       : 0xd0002a0
cpu MHz         : 800.000
cache size      : 49152 KB
physical id     : 1
siblings        : 64
core id         : 31
cpu cores       : 32
apicid          : 191
initial apicid  : 191
fpu             : yes
fpu_exception   : yes
cpuid level     : 27
wp              : yes
flags           : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe syscall nx pdpe1gb rdtscp lm constant_tsc art arch_perfmon pebs bts rep_good nopl xtopology nonstop_tsc cpuid aperfmperf pni pclmulqdq dtes64 ds_cpl vmx smx est tm2 ssse3 sdbg fma cx16 xtpr pdcm pcid dca sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand lahf_lm abm 3dnowprefetch cpuid_fault epb cat_l3 invpcid_single intel_ppin ssbd mba ibrs ibpb stibp ibrs_enhanced tpr_shadow vnmi flexpriority ept vpid ept_ad fsgsbase tsc_adjust sgx bmi1 avx2 smep bmi2 erms invpcid cqm rdt_a avx512f avx512dq rdseed adx smap avx512ifma clflushopt clwb intel_pt avx512cd sha_ni avx512bw avx512vl xsaveopt xsavec xgetbv1 xsaves cqm_llc cqm_occup_llc cqm_mbm_total cqm_mbm_local split_lock_detect wbnoinvd dtherm arat pln pts avx512vbmi umip pku ospke avx512_vbmi2 gfni vaes vpclmulqdq avx512_vnni avx512_bitalg tme avx512_vpopcntdq la57 rdpid sgx_lc fsrm md_clear pconfig flush_l1d arch_capabilities
vmx flags       : vnmi preemption_timer posted_intr invvpid ept_x_only ept_ad ept_1gb flexpriority apicv tsc_offset vtpr mtf vapic ept vpid unrestricted_guest vapic_reg vid ple shadow_vmcs pml ept_mode_based_exec tsc_scaling
bugs            : spectre_v1 spectre_v2 spec_store_bypass swapgs mmio_stale_data
bogomips        : 4427.06
clflush size    : 64
cache_alignment : 64
address sizes   : 46 bits physical, 57 bits virtual
power management:

Thanks,
-Vijay
Haitao Huang Aug. 1, 2022, 6 p.m. UTC | #6
On Fri, 29 Jul 2022 14:37:32 -0500, Haitao Huang  
<haitao.huang@linux.intel.com> wrote:

> On Fri, 29 Jul 2022 14:14:15 -0500, Dhanraj, Vijay  
> <vijay.dhanraj@intel.com> wrote:
>
>> Hi Dave,
>>
>> Tried with the latest tip and I still observe the issue. I tried 2G, 4G  
>> and 8G sized requests and as before, 2GB size requests works fine but  
>> 4GB and 8GB requests failed.
>>
>> Commit I have applied my test patch:
>> commit 038ef9928e1acb37bbe808c23a421189f1fd96cb (origin/x86/sgx)
>> Merge: e0a5915f1cca e0dccc3b76fb
>> Author: Ingo Molnar <mingo@kernel.org>
>> Date:   Tue Jul 26 09:14:28 2022 +0200
>>
>>     Merge tag 'v5.19-rc8' into x86/sgx, to resolve conflicts
>>
>> Kernel version:
>> Linux sdp 5.19.0-rc8-custom #1 SMP PREEMPT_DYNAMIC Fri Jul 29 10:28:25  
>> PDT 2022 x86_64 x86_64 x86_64 GNU/Linux
>>
>
> Using a kernel built at the same commit, I did the same test on another  
> machine with 4G EPC reported by the kernel log and reproduced the  
> failures only on 5th run for both 4G and 8G size requests (set by  
> edmm_size). The failure seems to stick for subsequent runs but I did not  
> run more than 7 times for each case so can't say definitely about that.
>

FYI I have ran more iterations, and there is no hysteresis. Failures are  
random on this platform.
Also I was able to reproduce the same random failures on V4 patches:  
https://github.com/rchatre/linux/commits/sgx/sgx2_submitted_v4_plus_rwx.

Another observation is that those failures are no reproduced when BIOS is  
configured with 64G EPC on each socket.

Failures with v4 were not reproduced on Vijay's machine even with 30 runs.

Note my CPU has a different microcode version from Vijay's though both are  
the same model:

processor       : 127
vendor_id       : GenuineIntel
cpu family      : 6
model           : 106
model name      : Intel(R) Xeon(R) Platinum 8352Y CPU @ 2.20GHz
stepping        : 6
microcode       : 0xd000363
cpu MHz         : 2200.000
cache size      : 49152 KB
physical id     : 1
siblings        : 64
core id         : 31
cpu cores       : 32
apicid          : 191
initial apicid  : 191
fpu             : yes
fpu_exception   : yes
cpuid level     : 27
wp              : yes
flags           : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca  
cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe syscall nx  
pdpe1gb rdtscp lm constant_tsc art arch_perfmon pebs bts rep_good nopl  
xtopology nonstop_tsc cpuid aperfmperf pni pclmulqdq dtes64 monitor ds_cpl  
vmx smx est tm2 ssse3 sdbg fma cx16 xtpr pdcm pcid dca sse4_1 sse4_2  
x2apic movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand lahf_lm  
abm 3dnowprefetch cpuid_fault epb cat_l3 invpcid_single intel_ppin ssbd  
mba ibrs ibpb stibp ibrs_enhanced tpr_shadow vnmi flexpriority ept vpid  
ept_ad fsgsbase tsc_adjust sgx bmi1 avx2 smep bmi2 erms invpcid cqm rdt_a  
avx512f avx512dq rdseed adx smap avx512ifma clflushopt clwb intel_pt  
avx512cd sha_ni avx512bw avx512vl xsaveopt xsavec xgetbv1 xsaves cqm_llc  
cqm_occup_llc cqm_mbm_total cqm_mbm_local split_lock_detect wbnoinvd  
dtherm ida arat pln pts hwp hwp_act_window hwp_epp hwp_pkg_req avx512vbmi  
umip pku ospke avx512_vbmi2 gfni vaes vpclmulqdq avx512_vnni avx512_bitalg  
tme avx512_vpopcntdq rdpid sgx_lc fsrm md_clear pconfig flush_l1d  
arch_capabilities
vmx flags       : vnmi preemption_timer posted_intr invvpid ept_x_only  
ept_ad ept_1gb flexpriority apicv tsc_offset vtpr mtf vapic ept vpid  
unrestricted_guest vapic_reg vid ple shadow_vmcs pml ept_mode_based_exec  
tsc_scaling
bugs            : spectre_v1 spectre_v2 spec_store_bypass swapgs
bogomips        : 4402.06
clflush size    : 64
cache_alignment : 64
address sizes   : 46 bits physical, 57 bits virtual
power management:

Thanks
Haitao

>
>>
>>> -----Original Message-----
>>> From: Hansen, Dave <dave.hansen@intel.com>
>>> Sent: Friday, July 29, 2022 9:38 AM
>>> To: Dhanraj, Vijay <vijay.dhanraj@intel.com>; Chatre, Reinette
>>> <reinette.chatre@intel.com>; dave.hansen@linux.intel.com;
>>> jarkko@kernel.org; linux-sgx@vger.kernel.org
>>> Cc: Huang, Haitao <haitao.huang@intel.com>
>>> Subject: Re: Support SGX2 V5: Seg-fault with EACCEPT for large number  
>>> of
>>> EPC pages
>>>
>>> On 7/29/22 09:01, Dhanraj, Vijay wrote:
>>> > Huang, Haitao and I created a simple patch to repro this issue using  
>>> the SGX
>>> selftests and we do see the issue when using V5 (5.18.0-rc5) but cannot
>>> repro the issue in V4 (5.18.0-rc2). Not sure if this is a driver issue  
>>> or kernel,
>>> can you please check?
>>>
>>> Thanks for the report.
>>>
>>> Could you please try the code that's going to go to Linus shortly?   
>>> It's really
>>> the only version that matters at this point?
>>>
>>> https://git.kernel.org/pub/scm/linux/kernel/git/tip/tip.git/log/?h=x86/sgx
Jarkko Sakkinen Aug. 3, 2022, 5:28 p.m. UTC | #7
On Fri, Jul 29, 2022 at 04:01:04PM +0000, Dhanraj, Vijay wrote:
> Hi All,
> 
> I recently tested the V5 version of the patch with Gramine and ran into a seg-fault during EPC allocation that is `EAUG`ing via `EACCEPT`. Allocation worked fine for smaller requests and even up to 2GBs. But when I tried with 4GB allocation I got a seg-fault.
> Huang, Haitao and I created a simple patch to repro this issue using the SGX selftests and we do see the issue when using V5 (5.18.0-rc5) but cannot repro the issue in V4 (5.18.0-rc2). Not sure if this is a driver issue or kernel, can you please check?
> 
> Results with V5 using modified `augment_via_eaccept` test:
> #  RUN           enclave.augment_via_eaccept ...
> # main.c:1135:augment_via_eaccept:test enclave: total_size = 8192, seg->size = 8192
> # main.c:1135:augment_via_eaccept:test enclave: total_size = 12288, seg->size = 4096
> # main.c:1135:augment_via_eaccept:test enclave: total_size = 36864, seg->size = 24576
> # main.c:1135:augment_via_eaccept:test enclave: total_size = 40960, seg->size = 4096
> # main.c:1153:augment_via_eaccept:mmaping pages at end of enclave...
> # main.c:1167:augment_via_eaccept:Entering enclave to run EACCEPT for each page of 8589934592 bytes may take a while ...
> # main.c:1184:augment_via_eaccept:Expected self->run.exception_vector (14) == 0 (0)
> # main.c:1185:augment_via_eaccept:Expected self->run.exception_error_code (4) == 0 (0)
> # main.c:1186:augment_via_eaccept:Expected self->run.exception_addr (140106113478656) == 0 (0)
> # main.c:1188:augment_via_eaccept:Expected self->run.function (3) == EEXIT (4)
> # augment_via_eaccept: Test terminated by assertion
> 
> Results with V4 using modified `augment_via_eaccept` test:
> #  RUN           enclave.augment_via_eaccept ...
> # main.c:1135:augment_via_eaccept:test enclave: total_size = 8192, seg->size = 8192
> # main.c:1135:augment_via_eaccept:test enclave: total_size = 12288, seg->size = 4096
> # main.c:1135:augment_via_eaccept:test enclave: total_size = 36864, seg->size = 24576
> # main.c:1135:augment_via_eaccept:test enclave: total_size = 40960, seg->size = 4096
> # main.c:1153:augment_via_eaccept:mmaping pages at end of enclave...
> # main.c:1167:augment_via_eaccept:Entering enclave to run EACCEPT for each page of 8589934592 bytes may take a while ...
> #            OK  enclave.augment_via_eaccept
> 
> 
> Test Patch:
> diff --git a/tools/testing/selftests/sgx/load.c b/tools/testing/selftests/sgx/load.c
> index 94bdeac1cf04..7de1b15c90b1 100644
> --- a/tools/testing/selftests/sgx/load.c
> +++ b/tools/testing/selftests/sgx/load.c
> @@ -171,7 +171,8 @@ uint64_t encl_get_entry(struct encl *encl, const char *symbol)
>  	return 0;
>  }
>  
> -bool encl_load(const char *path, struct encl *encl, unsigned long heap_size)
> +bool encl_load(const char *path, struct encl *encl, unsigned long heap_size,
> +			   unsigned long edmm_size)
>  {
>  	const char device_path[] = "/dev/sgx_enclave";
>  	struct encl_segment *seg;
> @@ -300,7 +301,7 @@ bool encl_load(const char *path, struct encl *encl, unsigned long heap_size)
>  
>  	encl->src_size = encl->segment_tbl[j].offset + encl->segment_tbl[j].size;
>  
> -	for (encl->encl_size = 4096; encl->encl_size < encl->src_size; )
> +	for (encl->encl_size = 4096; encl->encl_size < encl->src_size + edmm_size;)
>  		encl->encl_size <<= 1;
>  
>  	return true;
> diff --git a/tools/testing/selftests/sgx/main.c b/tools/testing/selftests/sgx/main.c
> index 9820b3809c69..8d7ce9389c8f 100644
> --- a/tools/testing/selftests/sgx/main.c
> +++ b/tools/testing/selftests/sgx/main.c
> @@ -25,6 +25,8 @@ static const uint64_t MAGIC = 0x1122334455667788ULL;
>  static const uint64_t MAGIC2 = 0x8877665544332211ULL;
>  vdso_sgx_enter_enclave_t vdso_sgx_enter_enclave;
>  
> +static const unsigned long edmm_size = 8589934592; //8G
> +
>  /*
>   * Security Information (SECINFO) data structure needed by a few SGX
>   * instructions (eg. ENCLU[EACCEPT] and ENCLU[EMODPE]) holds meta-data
> @@ -183,7 +185,7 @@ static bool setup_test_encl(unsigned long heap_size, struct encl *encl,
>  	unsigned int i;
>  	void *addr;
>  
> -	if (!encl_load("test_encl.elf", encl, heap_size)) {
> +	if (!encl_load("test_encl.elf", encl, heap_size, edmm_size)) {
>  		encl_delete(encl);
>  		TH_LOG("Failed to load the test enclave.");
>  		return false;
> @@ -1104,14 +1106,19 @@ TEST_F(enclave, augment)
>   * Test for the addition of pages to an initialized enclave via a
>   * pre-emptive run of EACCEPT on page to be added.
>   */
> -TEST_F(enclave, augment_via_eaccept)
> +/*
> + * Test for the addition of pages to an initialized enclave via a
> + * pre-emptive run of EACCEPT on page to be added.
> + */
> +/*TEST_F(enclave, augment_via_eaccept)*/
> +TEST_F_TIMEOUT(enclave, augment_via_eaccept, 900)

Could you make this a proper patch for kselftest?

It looks fine. Only thing you need to do is to add instead a new test add a
new test called augment_via_eaccept_long. Please, also define this before
TEST_F's:

#define TIMEOUT_LONG 900 /* seconds */

Then we can test against a commit instead of attachment and we obviously
want a test case for this, after the issue has been fixed. And I can easily
build a kernel for Geminilake, XPS-20 and Icelake server, and run the
equivalent test.

BR, Jarkko
Dhanraj, Vijay Aug. 4, 2022, 12:38 a.m. UTC | #8
> 
> Could you make this a proper patch for kselftest?
> 
> It looks fine. Only thing you need to do is to add instead a new test add a
> new test called augment_via_eaccept_long. Please, also define this before
> TEST_F's:
> 
> #define TIMEOUT_LONG 900 /* seconds */
> 
> Then we can test against a commit instead of attachment and we obviously
> want a test case for this, after the issue has been fixed. And I can easily build
> a kernel for Geminilake, XPS-20 and Icelake server, and run the equivalent
> test.
> 
> BR, Jarkko

Sure, will work on a patch and submit it.
Jarkko Sakkinen Aug. 4, 2022, 12:57 a.m. UTC | #9
On Thu, Aug 04, 2022 at 12:38:12AM +0000, Dhanraj, Vijay wrote:
> > 
> > Could you make this a proper patch for kselftest?
> > 
> > It looks fine. Only thing you need to do is to add instead a new test add a
> > new test called augment_via_eaccept_long. Please, also define this before

Oops, typos. I meant to say that you could add a new case, instead
of replacing the existing...

> Sure, will work on a patch and submit it.

Thank you! Looking forward to it.

BR, Jarkko
diff mbox series

Patch

diff --git a/tools/testing/selftests/sgx/load.c b/tools/testing/selftests/sgx/load.c
index 94bdeac1cf04..7de1b15c90b1 100644
--- a/tools/testing/selftests/sgx/load.c
+++ b/tools/testing/selftests/sgx/load.c
@@ -171,7 +171,8 @@  uint64_t encl_get_entry(struct encl *encl, const char *symbol)
 	return 0;
 }
 
-bool encl_load(const char *path, struct encl *encl, unsigned long heap_size)
+bool encl_load(const char *path, struct encl *encl, unsigned long heap_size,
+			   unsigned long edmm_size)
 {
 	const char device_path[] = "/dev/sgx_enclave";
 	struct encl_segment *seg;
@@ -300,7 +301,7 @@  bool encl_load(const char *path, struct encl *encl, unsigned long heap_size)
 
 	encl->src_size = encl->segment_tbl[j].offset + encl->segment_tbl[j].size;
 
-	for (encl->encl_size = 4096; encl->encl_size < encl->src_size; )
+	for (encl->encl_size = 4096; encl->encl_size < encl->src_size + edmm_size;)
 		encl->encl_size <<= 1;
 
 	return true;
diff --git a/tools/testing/selftests/sgx/main.c b/tools/testing/selftests/sgx/main.c
index 9820b3809c69..8d7ce9389c8f 100644
--- a/tools/testing/selftests/sgx/main.c
+++ b/tools/testing/selftests/sgx/main.c
@@ -25,6 +25,8 @@  static const uint64_t MAGIC = 0x1122334455667788ULL;
 static const uint64_t MAGIC2 = 0x8877665544332211ULL;
 vdso_sgx_enter_enclave_t vdso_sgx_enter_enclave;
 
+static const unsigned long edmm_size = 8589934592; //8G
+
 /*
  * Security Information (SECINFO) data structure needed by a few SGX
  * instructions (eg. ENCLU[EACCEPT] and ENCLU[EMODPE]) holds meta-data
@@ -183,7 +185,7 @@  static bool setup_test_encl(unsigned long heap_size, struct encl *encl,
 	unsigned int i;
 	void *addr;
 
-	if (!encl_load("test_encl.elf", encl, heap_size)) {
+	if (!encl_load("test_encl.elf", encl, heap_size, edmm_size)) {
 		encl_delete(encl);
 		TH_LOG("Failed to load the test enclave.");
 		return false;
@@ -1104,14 +1106,19 @@  TEST_F(enclave, augment)
  * Test for the addition of pages to an initialized enclave via a
  * pre-emptive run of EACCEPT on page to be added.
  */
-TEST_F(enclave, augment_via_eaccept)
+/*
+ * Test for the addition of pages to an initialized enclave via a
+ * pre-emptive run of EACCEPT on page to be added.
+ */
+/*TEST_F(enclave, augment_via_eaccept)*/
+TEST_F_TIMEOUT(enclave, augment_via_eaccept, 900)
 {
 	struct encl_op_get_from_addr get_addr_op;
 	struct encl_op_put_to_addr put_addr_op;
 	struct encl_op_eaccept eaccept_op;
 	size_t total_size = 0;
 	void *addr;
-	int i;
+	unsigned long i;
 
 	if (!sgx2_supported())
 		SKIP(return, "SGX2 not supported");
@@ -1125,6 +1132,7 @@  TEST_F(enclave, augment_via_eaccept)
 		struct encl_segment *seg = &self->encl.segment_tbl[i];
 
 		total_size += seg->size;
+		TH_LOG("test enclave: total_size = %ld, seg->size = %ld", total_size, seg->size);
 	}
 
 	/*
@@ -1132,7 +1140,7 @@  TEST_F(enclave, augment_via_eaccept)
 	 * test enclave since enclave size must be a power of 2 in bytes while
 	 * test_encl does not consume it all.
 	 */
-	EXPECT_LT(total_size + PAGE_SIZE, self->encl.encl_size);
+	EXPECT_LT(total_size + edmm_size, self->encl.encl_size);
 
 	/*
 	 * mmap() a page at end of existing enclave to be used for dynamic
@@ -1142,10 +1150,10 @@  TEST_F(enclave, augment_via_eaccept)
 	 * falls into the enclave's address range but not backed
 	 * by existing enclave pages.
 	 */
-
-	addr = mmap((void *)self->encl.encl_base + total_size, PAGE_SIZE,
-		    PROT_READ | PROT_WRITE | PROT_EXEC, MAP_SHARED | MAP_FIXED,
-		    self->encl.fd, 0);
+	TH_LOG("mmaping pages at end of enclave...");
+	addr = mmap((void *)self->encl.encl_base + total_size, edmm_size,
+			PROT_READ | PROT_WRITE | PROT_EXEC, MAP_SHARED | MAP_FIXED,
+			self->encl.fd, 0);
 	EXPECT_NE(addr, MAP_FAILED);
 
 	self->run.exception_vector = 0;
@@ -1156,25 +1164,29 @@  TEST_F(enclave, augment_via_eaccept)
 	 * Run EACCEPT on new page to trigger the #PF->EAUG->EACCEPT(again
 	 * without a #PF). All should be transparent to userspace.
 	 */
-	eaccept_op.epc_addr = self->encl.encl_base + total_size;
+	TH_LOG("Entering enclave to run EACCEPT for each page of %zd bytes may take a while ...",
+			edmm_size);
 	eaccept_op.flags = SGX_SECINFO_R | SGX_SECINFO_W | SGX_SECINFO_REG | SGX_SECINFO_PENDING;
 	eaccept_op.ret = 0;
 	eaccept_op.header.type = ENCL_OP_EACCEPT;
 
-	EXPECT_EQ(ENCL_CALL(&eaccept_op, &self->run, true), 0);
+	for (i = 0; i < edmm_size; i += 4096) {
+		eaccept_op.epc_addr = (uint64_t)(addr + i);
 
-	if (self->run.exception_vector == 14 &&
-	    self->run.exception_error_code == 4 &&
-	    self->run.exception_addr == self->encl.encl_base + total_size) {
-		munmap(addr, PAGE_SIZE);
-		SKIP(return, "Kernel does not support adding pages to initialized enclave");
-	}
+		EXPECT_EQ(ENCL_CALL(&eaccept_op, &self->run, true), 0);
+		if (self->run.exception_vector == 14 &&
+			self->run.exception_error_code == 4 &&
+			self->run.exception_addr == self->encl.encl_base) {
+			munmap(addr, edmm_size);
+			SKIP(return, "Kernel does not support adding pages to initialized enclave");
+		}
 
-	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);
+		EXPECT_EQ(self->run.exception_vector, 0);
+		EXPECT_EQ(self->run.exception_error_code, 0);
+		EXPECT_EQ(self->run.exception_addr, 0);
+		ASSERT_EQ(eaccept_op.ret, 0);
+		ASSERT_EQ(self->run.function, EEXIT);
+	}
 
 	/*
 	 * New page should be accessible from within enclave - attempt to
@@ -1207,7 +1219,7 @@  TEST_F(enclave, augment_via_eaccept)
 	EXPECT_EQ(self->run.exception_error_code, 0);
 	EXPECT_EQ(self->run.exception_addr, 0);
 
-	munmap(addr, PAGE_SIZE);
+	munmap(addr, edmm_size);
 }
 
 /*
diff --git a/tools/testing/selftests/sgx/main.h b/tools/testing/selftests/sgx/main.h
index fc585be97e2f..fe5d39ac0e1e 100644
--- a/tools/testing/selftests/sgx/main.h
+++ b/tools/testing/selftests/sgx/main.h
@@ -35,7 +35,8 @@  extern unsigned char sign_key[];
 extern unsigned char sign_key_end[];
 
 void encl_delete(struct encl *ctx);
-bool encl_load(const char *path, struct encl *encl, unsigned long heap_size);
+bool encl_load(const char *path, struct encl *encl, unsigned long heap_size,
+			   unsigned long edmm_size);
 bool encl_measure(struct encl *encl);
 bool encl_build(struct encl *encl);
 uint64_t encl_get_entry(struct encl *encl, const char *symbol);
diff --git a/tools/testing/selftests/sgx/sigstruct.c b/tools/testing/selftests/sgx/sigstruct.c
index 50c5ab1aa6fa..6000cf0e4975 100644
--- a/tools/testing/selftests/sgx/sigstruct.c
+++ b/tools/testing/selftests/sgx/sigstruct.c
@@ -343,7 +343,7 @@  bool encl_measure(struct encl *encl)
 	if (!ctx)
 		goto err;
 
-	if (!mrenclave_ecreate(ctx, encl->src_size))
+	if (!mrenclave_ecreate(ctx, encl->encl_size))
 		goto err;
 
 	for (i = 0; i < encl->nr_segments; i++) {