Linux-Sgx Archive mirror
 help / color / mirror / Atom feed
From: Jarkko Sakkinen <jarkko@kernel.org>
To: Reinette Chatre <reinette.chatre@intel.com>,
	Dave Hansen <dave.hansen@linux.intel.com>
Cc: linux-sgx@vger.kernel.org, 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>,
	Jethro Beekman <jethro@fortanix.com>,
	Sean Christopherson <seanjc@google.com>,
	"open list:X86 ARCHITECTURE (32-BIT AND 64-BIT)" 
	<linux-kernel@vger.kernel.org>
Subject: Re: [PATCH v6] x86/sgx: Free backing memory after faulting the enclave page
Date: Thu, 10 Mar 2022 20:00:55 +0200	[thread overview]
Message-ID: <Yio814lJVgutumd8@iki.fi> (raw)
In-Reply-To: <YihezQsQca0RBuxT@iki.fi>

On Wed, Mar 09, 2022 at 10:01:17AM +0200, Jarkko Sakkinen wrote:
> On Tue, Mar 08, 2022 at 11:16:19AM -0800, Reinette Chatre wrote:
> > Hi,
> > 
> > On 3/3/2022 2:38 PM, Jarkko Sakkinen wrote:
> > > There is a limited amount of SGX memory (EPC) on each system.  When that
> > > memory is used up, SGX has its own swapping mechanism which is similar
> > > in concept but totally separate from the core mm/* code.  Instead of
> > > swapping to disk, SGX swaps from EPC to normal RAM.  That normal RAM
> > > comes from a shared memory pseudo-file and can itself be swapped by the
> > > core mm code.  There is a hierarchy like this:
> > > 
> > > 	EPC <-> shmem <-> disk
> > > 
> > > After data is swapped back in from shmem to EPC, the shmem backing
> > > storage needs to be freed.  Currently, the backing shmem is not freed.
> > > This effectively wastes the shmem while the enclave is running.  The
> > > memory is recovered when the enclave is destroyed and the backing
> > > storage freed.
> > > 
> > > Sort this out by freeing memory with shmem_truncate_range(), as soon as
> > > a page is faulted back to the EPC.  In addition, free the memory for
> > > PCMD pages as soon as all PCMD's in a page have been marked as unused
> > > by zeroing its contents.
> > > 
> > > Reported-by: Dave Hansen <dave.hansen@linux.intel.com>
> > > Cc: stable@vger.kernel.org
> > > Fixes: 1728ab54b4be ("x86/sgx: Add a page reclaimer")
> > > Signed-off-by: Jarkko Sakkinen <jarkko@kernel.org>
> > 
> > I can reliably reproduce the issue this patch aims to solve by creating
> > a virtual machine that has a significant portion of its memory consumed
> > by EPC:
> > 
> > qemu-system-x86_64 -smp 4 -m 4G\
> >  -enable-kvm \
> >  -cpu host,+sgx-provisionkey \
> >  -object memory-backend-ram,size=2G,host-nodes=0,policy=bind,id=node0 \
> >  -object memory-backend-epc,id=mem0,size=1536M,prealloc=on,host-nodes=0,policy=bind \
> >  -numa node,nodeid=0,cpus=0-1,memdev=node0 \
> >  -object memory-backend-ram,size=2G,host-nodes=1,policy=bind,id=node1 \
> >  -object memory-backend-epc,id=mem1,size=1536M,prealloc=on,host-nodes=1,policy=bind \
> >  -numa node,nodeid=1,cpus=2-3,memdev=node1 \
> >  -M sgx-epc.0.memdev=mem0,sgx-epc.0.node=0,sgx-epc.1.memdev=mem1,sgx-epc.1.node=1 \
> >  ...
> > 
> > Before this patch, running the very stressful SGX2 over subscription test case
> > (unclobbered_vdso_oversubscribed_remove) in this environment always triggers 
> > the oom-killer but no amount of tasks killed can save the system
> > with it always ending deadlocked on memory:
> > 
> > [   58.642719] Tasks state (memory values in pages):
> > [   58.644324] [  pid  ]   uid  tgid total_vm      rss pgtables_bytes swapents oom_score_adj name
> > [   58.647237] [    195]     0   195     3153      197    45056        0         -1000 systemd-udevd
> > [   58.650238] [    281]     0   281  1836367        0 10817536        0             0 test_sgx
> > [   58.653088] Out of memory and no killable processes...
> > [   58.654832] Kernel panic - not syncing: System is deadlocked on memory
> > 
> > After applying this patch I was able to run SGX2 selftest
> > unclobbered_vdso_oversubscribed_remove ten times successfully.
> > 
> > Tested-by: Reinette Chatre <reinette.chatre@intel.com>
> 
> Thank you.
> 
> > Reinette

Dave, can this be picked up?

BR, Jarkko

      reply	other threads:[~2022-03-10 18:01 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-03-03 22:38 [PATCH v6] x86/sgx: Free backing memory after faulting the enclave page Jarkko Sakkinen
2022-03-08 11:36 ` Jarkko Sakkinen
2022-03-08 19:16 ` Reinette Chatre
2022-03-09  8:01   ` Jarkko Sakkinen
2022-03-10 18:00     ` Jarkko Sakkinen [this message]

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=Yio814lJVgutumd8@iki.fi \
    --to=jarkko@kernel.org \
    --cc=bp@alien8.de \
    --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=reinette.chatre@intel.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;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).