Linux-Sgx Archive mirror
 help / color / mirror / Atom feed
From: "Haitao Huang" <haitao.huang@linux.intel.com>
To: "Jarkko Sakkinen" <jarkko@kernel.org>
Cc: linux-sgx@vger.kernel.org, dave.hansen@linux.intel.com,
	reinette.chatre@intel.com, vijay.dhanraj@intel.com
Subject: Re: [RFC PATCH v2 2/4] x86/sgx: Implement support for MADV_WILLNEED
Date: Tue, 01 Nov 2022 13:24:24 -0500	[thread overview]
Message-ID: <op.1uyv2yc5wjvjmi@hhuan26-mobl1.mshome.net> (raw)
In-Reply-To: <Y2B0jBsG6HE4KVk7@kernel.org>

Hi Jarkko,

On Mon, 31 Oct 2022 20:21:16 -0500, Jarkko Sakkinen <jarkko@kernel.org>  
wrote:

> On Thu, Oct 27, 2022 at 12:45:30PM -0700, Haitao Huang wrote:
>> Support madvise(..., MADV_WILLNEED) by adding EPC pages with EAUG in
>> the newly added fops->fadvise() callback implementation, sgx_fadvise().
>>
>> Change the return type and values of the sgx_encl_eaug_page function
>> so that more specific error codes are returned for different treatment
>> by the page fault handler and the fadvise callback.
>> On any error, sgx_fadvise() will discontinue further operations
>> and return as normal. The page fault handler allows a PF retried
>> by returning VM_FAULT_NOPAGE in handling -EBUSY returned from
>> sgx_encl_eaug_page.
>>
>> Signed-off-by: Haitao Huang <haitao.huang@linux.intel.com>
>> ---
>>  arch/x86/kernel/cpu/sgx/driver.c | 81 ++++++++++++++++++++++++++++++++
>>  arch/x86/kernel/cpu/sgx/encl.c   | 46 +++++++++++-------
>>  arch/x86/kernel/cpu/sgx/encl.h   |  4 +-
>>  3 files changed, 113 insertions(+), 18 deletions(-)
>>
>> diff --git a/arch/x86/kernel/cpu/sgx/driver.c  
>> b/arch/x86/kernel/cpu/sgx/driver.c
>> index aa9b8b868867..54b24897605b 100644
>> --- a/arch/x86/kernel/cpu/sgx/driver.c
>> +++ b/arch/x86/kernel/cpu/sgx/driver.c
>> @@ -2,6 +2,7 @@
>>  /*  Copyright(c) 2016-20 Intel Corporation. */
>>
>>  #include <linux/acpi.h>
>> +#include <linux/fadvise.h>
>>  #include <linux/miscdevice.h>
>>  #include <linux/mman.h>
>>  #include <linux/security.h>
>> @@ -9,6 +10,7 @@
>>  #include <asm/traps.h>
>>  #include "driver.h"
>>  #include "encl.h"
>> +#include "encls.h"
>>
>>  u64 sgx_attributes_reserved_mask;
>>  u64 sgx_xfrm_reserved_mask = ~0x3;
>> @@ -97,10 +99,88 @@ static int sgx_mmap(struct file *file, struct  
>> vm_area_struct *vma)
>>  	vma->vm_ops = &sgx_vm_ops;
>>  	vma->vm_flags |= VM_PFNMAP | VM_DONTEXPAND | VM_DONTDUMP | VM_IO;
>>  	vma->vm_private_data = encl;
>> +	/* Anchor vm_pgoff to the enclave base.
>> +	 * So offset passed back to sgx_fadvise hook
>> +	 * is relative to the enclave base
>> +	 */
>
> This comment only convolutes code because it does not tell anything
> that is non-intuitive to understand from the code.
>
>> +	vma->vm_pgoff = (vma->vm_start - encl->base) >> PAGE_SHIFT;
>
> vma->vm_pgoff = PFN_DOWN(vma->vm_start - encl->base);
>
>>
>>  	return 0;
>>  }
>>
>> +/*
>> + * Add new pages to the enclave sequentially with ENCLS[EAUG] for the  
>> WILLNEED advice.
>> + * Only do this to existing VMAs in the same enclave and reject the  
>> request.
>
> Keep this.
>
>> + * Returns:	0 if EAUG done with best effort, -EINVAL if any sub-range  
>> given
>> + * is not in the enclave, or enclave is not initialized..
>> + */
>
> Please remove this.
>
> It is useless and also incorrect, given that the function can also return
> other return values.
>
>> +static int sgx_fadvise(struct file *file, loff_t offset, loff_t len,  
>> int advice)
>> +{
>> +	struct sgx_encl *encl = file->private_data;
>> +	unsigned long start, end, pos;
>> +	int ret = -EINVAL;
>> +	struct vm_area_struct *vma = NULL;
>
> Reverse christmas tree order.
>
>> +
>> +	/* Only support WILLNEED */
>> +	if (advice != POSIX_FADV_WILLNEED)
>> +		return -EINVAL;
>> +	if (!encl)
>> +		return -EINVAL;
...
>> +
>> +	/* EAUG works only for initialized enclaves. */
>
> Please, remove this comment.
>
>> +	if (!test_bit(SGX_ENCL_INITIALIZED, &encl->flags))
>> +		return -EINVAL;
...
>> +		/* Don't allow any gaps */
>> +		goto unlock;
>> +	}
>
> Empty line.
>
>> +	/* Here: vm_start <= pos < end <= vm_end */
>
...
>> +		if (ret)
>> +			break;
>> +		pos = pos + PAGE_SIZE;
>> +		cond_resched();
>> +	}
>> +	ret = 0;
>
> Empty line.
>

Thanks for the review. Will fix all above in next version.
BR
Haitao

  reply	other threads:[~2022-11-01 18:24 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-10-27 19:45 [RFC PATCH v2 0/4] x86/sgx: implement support for MADV_WILLNEED Haitao Huang
2022-10-27 19:45 ` [RFC PATCH v2 1/4] x86/sgx: Export sgx_encl_eaug_page Haitao Huang
2022-10-27 19:45   ` [RFC PATCH v2 2/4] x86/sgx: Implement support for MADV_WILLNEED Haitao Huang
2022-10-27 19:45     ` [RFC PATCH v2 3/4] selftests/sgx: add len field for EACCEPT op Haitao Huang
2022-10-27 19:45       ` [RFC PATCH v2 4/4] selftests/sgx: Add test for madvise(..., WILLNEED) Haitao Huang
2022-11-01  1:21     ` [RFC PATCH v2 2/4] x86/sgx: Implement support for MADV_WILLNEED Jarkko Sakkinen
2022-11-01 18:24       ` Haitao Huang [this message]
2022-11-01  1:12   ` [RFC PATCH v2 1/4] x86/sgx: Export sgx_encl_eaug_page 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=op.1uyv2yc5wjvjmi@hhuan26-mobl1.mshome.net \
    --to=haitao.huang@linux.intel.com \
    --cc=dave.hansen@linux.intel.com \
    --cc=jarkko@kernel.org \
    --cc=linux-sgx@vger.kernel.org \
    --cc=reinette.chatre@intel.com \
    --cc=vijay.dhanraj@intel.com \
    /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).