linux-sgx.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: "Huang, Kai" <kai.huang@intel.com>
To: "linux-sgx@vger.kernel.org" <linux-sgx@vger.kernel.org>,
	"Chatre, Reinette" <reinette.chatre@intel.com>,
	"jarkko@kernel.org" <jarkko@kernel.org>,
	"Dhanraj, Vijay" <vijay.dhanraj@intel.com>,
	"haitao.huang@linux.intel.com" <haitao.huang@linux.intel.com>,
	"dave.hansen@linux.intel.com" <dave.hansen@linux.intel.com>
Subject: Re: [RFC PATCH v4 2/4] x86/sgx: Implement support for MADV_WILLNEED
Date: Tue, 14 Feb 2023 22:36:40 +0000	[thread overview]
Message-ID: <c37bb20995f234aa724724a06c96c76f18252e66.camel@intel.com> (raw)
In-Reply-To: <op.10dk8lsjwjvjmi@hhuan26-mobl.amr.corp.intel.com>

On Tue, 2023-02-14 at 15:42 -0600, Haitao Huang wrote:
> On Tue, 14 Feb 2023 14:54:53 -0600, Huang, Kai <kai.huang@intel.com> wrote:
> 
> > On Tue, 2023-02-14 at 13:18 -0600, Haitao Huang wrote:
> > > Hi Kai
> > > 
> > > On Tue, 14 Feb 2023 03:47:24 -0600, Huang, Kai <kai.huang@intel.com>  
> > > wrote:
> > > 
> > > > On Fri, 2023-01-27 at 20:55 -0800, Haitao Huang wrote:
> > > > > @@ -97,10 +99,81 @@ 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;
> > > > > +	vma->vm_pgoff = PFN_DOWN(vma->vm_start - encl->base);
> > > > >   	return 0;
> > > > >  }
> > > > 
> > > > Perhaps I am missing something, but above change looks weird.
> > > > Conceptually, it doesn't/shouldn't belong to this series, which
> > > > essentially
> > > > preallocates and does EAUG EPC pages for a (or part of) given enclave.
> > > > The EAUG
> > > > logic should already be working for the normal fault path, which means
> > > > the code
> > > > change above either: 1) has been done at other place; 2) isn't needed.
> > > > 
> > > > I have kinda forgotten the userspace sequence to create an enclave.   
> > > If
> > > > I recall
> > > > correctly, you do below to create an enclave:
> > > > 
> > > > 	1) encl_fd = open("/dev/sgx_enclave");
> > > > 	2) encl_addr = mmap(encl_fd, encl_size, 0 /* pgoff */);
> > > > 	3) IOCTL(ECREATE, encl_addr, encl_size);
> > > > 
> > > > Would the above code change break the "mmap()" in above step 2?
> > > > 	
> > > 
> > > No, vm_pgoff was not used previously for enclave VMAs. I had to add this
> > > because the offset passed to sgx_fadvise is relative to file base and
> > > calculated in mm/madvise.c like this:
> > > 
> > >          offset = (loff_t)(start - vma->vm_start)
> > >                          + ((loff_t)vma->vm_pgoff << PAGE_SHIFT);
> > 
> > But shouldn't 'offset is relative to the file base' be conceptually  
> > correct from
> > the fadvice()'s point of view?
> > 
> > I think you should do:
> > 
> > 	encl_offset = offset + encl->base;
> > 
> > inside sgx_fadvice()?
> > 
> > > 
> If we don't set vma->vm_pgoff (default to zero), then offset will be  
> calculated as (start - vma->vm_start). Then the above calculation is wrong  
> if we have multiple VMAs for the same enclave, which is usually the case.

do_mmap() -> mmap_region() itself sets vma->vm_pgoff:

	vma = vm_area_alloc();
	...
	vma->vm_pgoff = pgoff;

	if (file)
		call_mmap(file, vma);	<- sgx_mmap()

I think you will always call mmap() against enclave's fd with 'pgoff' being set
to the offset relative to the file?

> 
> > > I had a comment in first version but removed it based on Jarkko's
> > > suggestion here:  
> > > https://lore.kernel.org/all/Y2B0jBsG6HE4KVk7@kernel.org/
> > > 
> > > The original comments probably seemed redundant to the definitions of  
> > > the
> > > vm_pgoff field and the fadvise interface. But let me know if we need  
> > > add a
> > > more helpful version of comments or any suggestion on the comments.
> > 
> > I still think this code change is wrong.
> > 
> > For instance, IIUC, it at least breaks the case where enclave hasn't been
> > created/initialized, where encl->base == 0 (although normal code path  
> > doesn't
> > use vm_pgoff, conceptually it's still wrong IIUC).
> > 
> > Maybe I am missing something?
> 
> The fadvise interface is only usable for an initialized enclave,  
> sgx_fadvise will return error otherwise. 
> 

True.  But that code change is unconditionally called for all mmap(), even when
enclave hasn't been created.

> Conceptually I view enclave base  
> as "file base", it's just that we don't ever need handle the zero case  
> caused by uninitialized enclave (kind of like a file never mapped). If an  
> initialized enclave happens to have zero base, it would also work.

A little bit confused about what does "enclave base" here.

To me, A file is an enclave, meaning the "file offset" equals to "enclave
offset".  "enclave base" is the base linear address of the enclave, it doesn't
matter whether it is 0 or not.  You get an "enclave address" from "enclave base"
plus "enclave offset" (or "file offset"):

	enclave_addr = enclave_base + enclave_offset/file_offset;

And such calculation is only valid after enclave has been created (enclave_base
is valid -- can be 0 or whatever).

Since sgx_mmap() can happen before enclave is created, calculating the vm_pgoff
from enclave_base is conceptually wrong.  Even if you really want to do it, it
should be:

	if (enclave_has_initialized())
		vma->vm_pgoff = ...;

But again I am not convinced why you cannot get the enclave_addr inside
sgx_fadvice().


  reply	other threads:[~2023-02-14 22:37 UTC|newest]

Thread overview: 60+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-01-28  4:55 [RFC PATCH v4 0/4] x86/sgx: implement support for MADV_WILLNEED Haitao Huang
2023-01-28  4:55 ` [RFC PATCH v4 1/4] x86/sgx: Export sgx_encl_eaug_page Haitao Huang
2023-01-28  4:55   ` [RFC PATCH v4 2/4] x86/sgx: Implement support for MADV_WILLNEED Haitao Huang
2023-01-28  4:55     ` [RFC PATCH v4 3/4] selftests/sgx: add len field for EACCEPT op Haitao Huang
2023-01-28  4:55       ` [RFC PATCH v4 4/4] selftests/sgx: Add test for madvise(..., WILLNEED) Haitao Huang
2023-02-07 23:30         ` Jarkko Sakkinen
2023-02-15  2:38         ` Huang, Kai
2023-02-15  4:42           ` Haitao Huang
2023-02-15  8:46             ` Huang, Kai
2023-02-17 22:29               ` jarkko
2023-02-07 23:29       ` [RFC PATCH v4 3/4] selftests/sgx: add len field for EACCEPT op Jarkko Sakkinen
2023-02-07 23:28     ` [RFC PATCH v4 2/4] x86/sgx: Implement support for MADV_WILLNEED Jarkko Sakkinen
2023-02-14  9:47     ` Huang, Kai
2023-02-14 19:18       ` Haitao Huang
2023-02-14 20:54         ` Huang, Kai
2023-02-14 21:42           ` Haitao Huang
2023-02-14 22:36             ` Huang, Kai [this message]
2023-02-15  3:59               ` Haitao Huang
2023-02-15  8:51                 ` Huang, Kai
2023-02-15 15:42                   ` Haitao Huang
2023-02-16  7:53                     ` Huang, Kai
2023-02-16 17:12                       ` Haitao Huang
2023-02-17 22:32                     ` jarkko
2023-02-17 23:03                       ` Haitao Huang
2023-02-21 22:10                         ` jarkko
2023-02-22  1:37                           ` Haitao Huang
2023-03-07 23:32                             ` Huang, Kai
2023-03-09  0:50                               ` Haitao Huang
2023-03-09 11:31                                 ` Huang, Kai
2023-03-14 14:54                                   ` Haitao Huang
2023-03-19 13:26                                     ` jarkko
2023-03-20  9:36                                       ` Huang, Kai
2023-03-20 14:04                                         ` jarkko
2023-05-27  0:32                                   ` Haitao Huang
2023-06-06  4:11                                     ` Huang, Kai
2023-06-07 16:59                                       ` Haitao Huang
2023-06-16  3:49                                       ` Huang, Kai
2023-06-16 22:05                                         ` Sean Christopherson
2023-06-19 11:17                                           ` Huang, Kai
2023-06-22 22:01                                             ` Sean Christopherson
2023-06-22 23:21                                               ` Huang, Kai
2023-06-26 22:28                                                 ` Sean Christopherson
2023-06-27 11:43                                                   ` Huang, Kai
2023-06-27 14:50                                                     ` Sean Christopherson
2023-06-28  9:37                                                       ` Huang, Kai
2023-06-28 14:57                                                         ` Sean Christopherson
2023-06-29  3:10                                                           ` Huang, Kai
2023-06-29 14:23                                                             ` Sean Christopherson
2023-06-29 23:29                                                               ` Huang, Kai
2023-06-30  0:14                                                                 ` Sean Christopherson
2023-06-30  0:56                                                                   ` Huang, Kai
2023-06-30  1:54                                                                 ` Jarkko Sakkinen
2023-06-30  1:57                                                                   ` Jarkko Sakkinen
2023-06-30  4:26                                                                     ` Huang, Kai
2023-06-30  9:35                                                                       ` Jarkko Sakkinen
2023-03-12  1:25                               ` jarkko
2023-03-12 22:25                                 ` Huang, Kai
2023-02-17 22:07           ` jarkko
2023-02-17 21:50       ` jarkko
2023-02-07 23:26   ` [RFC PATCH v4 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=c37bb20995f234aa724724a06c96c76f18252e66.camel@intel.com \
    --to=kai.huang@intel.com \
    --cc=dave.hansen@linux.intel.com \
    --cc=haitao.huang@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 NNTP newsgroup(s).