public inbox for stable@vger.kernel.org
 help / color / mirror / Atom feed
From: Jarkko Sakkinen <jarkko@kernel.org>
To: Dave Hansen <dave.hansen@intel.com>
Cc: linux-sgx@vger.kernel.org,
	Dave Hansen <dave.hansen@linux.intel.com>,
	stable@vger.kernel.org, Thomas Gleixner <tglx@linutronix.de>,
	Ingo Molnar <mingo@redhat.com>, Borislav Petkov <bp@alien8.de>,
	"maintainer:X86 ARCHITECTURE (32-BIT AND 64-BIT)"
	<x86@kernel.org>, "H. Peter Anvin" <hpa@zytor.com>,
	Sean Christopherson <seanjc@google.com>,
	Jethro Beekman <jethro@fortanix.com>,
	"open list:X86 ARCHITECTURE (32-BIT AND 64-BIT)" 
	<linux-kernel@vger.kernel.org>
Subject: Re: [PATCH v4] x86/sgx: Free backing memory after faulting the enclave page
Date: Mon, 28 Feb 2022 13:55:39 +0100	[thread overview]
Message-ID: <YhzGS+x0eNoc3gyN@iki.fi> (raw)
In-Reply-To: <33646f1e-da44-503a-c454-02658d512926@intel.com>

On Thu, Feb 24, 2022 at 09:14:05AM -0800, Dave Hansen wrote:
> On 2/22/22 04:03, Jarkko Sakkinen wrote:
> > +	if (pcmd_page_empty) {
> > +		pgoff_t pcmd_off = encl->size + PAGE_SIZE /* SECS */ +
> > +				   page_index * sizeof(struct sgx_pcmd);
> > +
> > +		sgx_encl_truncate_backing_page(encl, PFN_DOWN(pcmd_off));
> > +	}
> > +
> >  	return ret;
> >  }
> >  
> > @@ -583,7 +613,7 @@ static struct page *sgx_encl_get_backing_page(struct sgx_encl *encl,
> >  static int sgx_encl_get_backing(struct sgx_encl *encl, unsigned long page_index,
> >  				struct sgx_backing *backing)
> >  {
> > -	pgoff_t pcmd_index = PFN_DOWN(encl->size) + 1 + (page_index >> 5);
> > +	pgoff_t pcmd_off = encl->size + PAGE_SIZE /* SECS */ + page_index * sizeof(struct sgx_pcmd);
> 
> Jarkko, I really don't like how this looks.  The '/* SECS */' thing is
> pretty ugly and the comment in the middle of an arithmetic operation is
> just really hard to read.
> 
> Then, there's the fact that this gem is copied-and-pasted.  Oh, and it
> looks a wee bit over 80 columns.

Today you can have 100.

> 
> I went to the trouble of writing a nice, fully-fleshed-out helper
> function for this with a comment included:
> 
> > https://lore.kernel.org/all/8afec431-4dfc-d8df-152b-76cca0e17ccb@intel.com/

Keeping full byte offset up until parts of it are required for something
makes the formula just a simple equation of additions and multiplications,
e.g. nothing like "/ sizeof(struct sgx_pcmd)" is required.

Then you get the PCMD page index will be just PFN_DOWN(pcmd_off) and offset
inside that page is pcmd_off & PAGE_MASK. At least fro me this is more 
intuitive way to do the calculations.
 
I thought that the formula is so simple that it does not matter if it is
just in two sites open coded but I can wrap it too, if required, e.g.

/* 
 * Calculate byte offset of a PCMD struct associated to an enclave page.
 * PCMD's follow right after the EPC data in the backing storage. In
 * addition to the visible enclave pages, there's one extra page slot
 * for SECS, before PCMD data.
 */
static pgoff_t *sgx_encl_page_index_to_pcmd_offset(struct sgx_encl *encl, unsigned long page_index)
{
        return encl->size + PAGE_SIZE + page_index * sizeof(struct sgx_pcmd);
}

> 
> Was there a problem using that?  The change from the last version is:
> 
> * Sanitized the offset calculations.
> 
> Given that there have been multiple different calculations over the four
> versions so far, which version was right?  v3 or v4?

This one has correct and tested calculations but for peer test probably
Reinette should verify that. I tested this with my laptop in bare metal.

BR, Jarkko

  reply	other threads:[~2022-02-28 12:55 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-02-22 12:03 [PATCH v4] x86/sgx: Free backing memory after faulting the enclave page Jarkko Sakkinen
2022-02-22 12:24 ` Jarkko Sakkinen
2022-02-24 17:14 ` Dave Hansen
2022-02-28 12:55   ` Jarkko Sakkinen [this message]
2022-02-28 15:32     ` Dave Hansen
2022-03-01 11:03       ` 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=YhzGS+x0eNoc3gyN@iki.fi \
    --to=jarkko@kernel.org \
    --cc=bp@alien8.de \
    --cc=dave.hansen@intel.com \
    --cc=dave.hansen@linux.intel.com \
    --cc=hpa@zytor.com \
    --cc=jethro@fortanix.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-sgx@vger.kernel.org \
    --cc=mingo@redhat.com \
    --cc=seanjc@google.com \
    --cc=stable@vger.kernel.org \
    --cc=tglx@linutronix.de \
    --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