Received: by 2002:a05:6a10:af89:0:0:0:0 with SMTP id iu9csp720609pxb; Fri, 14 Jan 2022 14:59:33 -0800 (PST) X-Google-Smtp-Source: ABdhPJwJqOgELnBN4Ufgb/N50LVEBX1/z3UiNpRV7358KSQZPEWwsN8aDsXFWaSVY9FbyBPjvIt1 X-Received: by 2002:a05:6402:b12:: with SMTP id bm18mr8258634edb.235.1642201173164; Fri, 14 Jan 2022 14:59:33 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1642201173; cv=none; d=google.com; s=arc-20160816; b=CTx46eC+yKsH1r6PTpyEdQXLAYWlNDo07UzS3I4FtKWH7nvq7VoN+abzgR1wobX5kC bSSCfKonTbU8iuNdXOXIbgr5FZ5gSUogLgX6vUM8AEmOsn849SdPoOav1sUmJt+BiIad OOPWFRrI8K9C7OYtvBLueX/3M+h0SytEjlhto6k3DGLCUvG2pkGM472+qkG5zMdvJF5/ Qmqinko9v/svTpT+VMgbij/fZy/qcC92IdcPxcgYwZzAtkoWAvUqohnfX8vjB1oqtrZb OSdtz9oxM5/fyJWezx2E73vR8j5Fkl54ZTqkNeuC2Zk0fAGgl8i+DoSh23wGIv42arH8 2Zyw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:date:cc:to:from:subject :message-id:dkim-signature; bh=rfhZRMr1ysBaxgi45mLbHapNhicT3fwuRPPLcokFQNY=; b=O7GqeeBemLkbzpzWrWV1jRHSsX9DrwaY1aYwDOY2RZVqGaeDmjQfFa0v+5a3to5DSL jGvUbqq+Wg9z6C1O5mtGDOqME0uTw3rjFseEPX8SuT0knmxakXX5sPZKe1EuJ33CE0UR uSFF7XPt33jnCkSI4JH4xdEdlpm512VjNqVdVRZKpQ37sI9zG5z3EuFhH57UcZ43rPtt Upfx2y3r82Z6bNeKOEYYunHZnYQO4QRbzlKU/Pb0Y4iIUO3fWJ0Swe3DD7t9j88xClx9 2IrfIMsmgijD8PBbHgPYY8MeWODM+tcXxYZQ5ziFO7IEuKwkPtqG/0h5BjCpd0vxxy/4 88MQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=lesRs14L; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id w8si4285195edd.165.2022.01.14.14.59.08; Fri, 14 Jan 2022 14:59:33 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=lesRs14L; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S243898AbiANRvR (ORCPT + 99 others); Fri, 14 Jan 2022 12:51:17 -0500 Received: from mga02.intel.com ([134.134.136.20]:24959 "EHLO mga02.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229449AbiANRvR (ORCPT ); Fri, 14 Jan 2022 12:51:17 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1642182677; x=1673718677; h=message-id:subject:from:to:cc:date:in-reply-to: references:mime-version:content-transfer-encoding; bh=oA7p8R5TuXBUb23c1P3IgwuuI4bHsWSCIwT6D7uo+GM=; b=lesRs14LN48luz9W2pXyLpliKHa6W1sInG/omZA95IgSIQguYmUp0Z/L H+ZBhxz1iHwk6+3rKFGeizySWQmaKI4SqAFazEoEKYwT8Q0roGaVFq0NY n8mdbiXYIBq7/YdgZiV85DUxfNzPlBxZ+osov3XAxLfulFywkql36ECnV t/bAEsPqUqswNyxcSgZV2rhykpfPz/uoo94vs4nhyWsuM6MoyOm4lew79 E6NDz2AJ1bZli0UigOWvuiAm1u711tgyF+jZKzUwjCVFv5F5tLsnIQJiK mVNczIWQAPqTx2TxXgm8yf8+dTJTBwAUfBBepJQ0fNZx1dFNJemyPp7z7 w==; X-IronPort-AV: E=McAfee;i="6200,9189,10227"; a="231644316" X-IronPort-AV: E=Sophos;i="5.88,289,1635231600"; d="scan'208";a="231644316" Received: from fmsmga001.fm.intel.com ([10.253.24.23]) by orsmga101.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 14 Jan 2022 09:51:16 -0800 X-IronPort-AV: E=Sophos;i="5.88,289,1635231600"; d="scan'208";a="670922797" Received: from agonz1-mobl3.amr.corp.intel.com (HELO kcaccard-mobl1.jf.intel.com) ([10.212.237.206]) by fmsmga001-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 14 Jan 2022 09:51:15 -0800 Message-ID: Subject: Re: [PATCH v2 2/2] x86/sgx: account backing pages From: Kristen Carlson Accardi To: Jarkko Sakkinen Cc: linux-sgx@vger.kernel.org, Dave Hansen , Thomas Gleixner , Ingo Molnar , Borislav Petkov , x86@kernel.org, "H. Peter Anvin" , linux-kernel@vger.kernel.org Date: Fri, 14 Jan 2022 09:51:14 -0800 In-Reply-To: References: <20220107181618.6597-1-kristen@linux.intel.com> <20220107181618.6597-3-kristen@linux.intel.com> Content-Type: text/plain; charset="UTF-8" User-Agent: Evolution 3.30.5 (3.30.5-1.fc29) MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Sat, 2022-01-08 at 18:05 +0200, Jarkko Sakkinen wrote: > On Fri, Jan 07, 2022 at 10:16:17AM -0800, Kristen Carlson Accardi > wrote: > > SGX may allow EPC pages to be overcommitted. If the system is > > out of enclave memory, EPC pages are swapped to normal RAM via > > a per enclave shared memory area. This shared memory is not > > charged to the enclave or the task mapping it, making it hard > > to account for using normal methods. > > > > In order to avoid unlimited usage of normal RAM, enclaves must be > > charged for each new page used for backing storage, and uncharged > > when they are no longer using a backing page. > > > > Modify the existing flow for requesting backing pages to reduce the > > available backing page counter and confirm that the limit has not > > been exceeded. Backing page usage for loading EPC pages back out of > > the shared memory do not incur a charge. > > > > When a backing page is released from usage, increment the available > > backing page counter. > > > > When swapping EPC pages to RAM, in addition to storing the page > > contents, SGX must store some additional metadata to protect > > against a malicious kernel when the page is swapped back in. This > > additional metadata is called Paging Crypto MetaData. PCMD is > > allocated from the same shared memory area as the backing page > > contents and consumes RAM the same way. > > > > PCMD is 128 bytes in size, and there is one PCMD structure per > > page written to shared RAM. The page index for the PCMD page is > > calculated from the page index of the backing page, so it is > > possible > > that the PCMD structures are not packed into the minimum number of > > pages possible. If 32 PCMDs can fit onto a single page, then PCMD > > usage is 1/32 of total EPC pages. In the worst case, PCMD can > > consume the same amount of RAM as EPC backing pages (1:1). For > > simplicity, this implementation does not account for PCMD page > > usage. > > > > Signed-off-by: Kristen Carlson Accardi > > --- > > arch/x86/kernel/cpu/sgx/encl.c | 76 > > ++++++++++++++++++++++++++++++++-- > > arch/x86/kernel/cpu/sgx/encl.h | 6 ++- > > arch/x86/kernel/cpu/sgx/main.c | 6 +-- > > 3 files changed, 80 insertions(+), 8 deletions(-) > > > > diff --git a/arch/x86/kernel/cpu/sgx/encl.c > > b/arch/x86/kernel/cpu/sgx/encl.c > > index 001808e3901c..8be6f0592bdc 100644 > > --- a/arch/x86/kernel/cpu/sgx/encl.c > > +++ b/arch/x86/kernel/cpu/sgx/encl.c > > @@ -32,7 +32,7 @@ static int __sgx_encl_eldu(struct sgx_encl_page > > *encl_page, > > else > > page_index = PFN_DOWN(encl->size); > > > > - ret = sgx_encl_get_backing(encl, page_index, &b); > > + ret = sgx_encl_lookup_backing(encl, page_index, &b); > > if (ret) > > return ret; > > > > @@ -407,6 +407,12 @@ void sgx_encl_release(struct kref *ref) > > sgx_encl_free_epc_page(entry->epc_page); > > encl->secs_child_cnt--; > > entry->epc_page = NULL; > > + } else { > > + /* > > + * If there is no epc_page, it means it has > > been > > + * swapped out. Uncharge the backing storage. > > + */ > > + sgx_uncharge_mem(); > > } > > > > kfree(entry); > > @@ -574,8 +580,8 @@ static struct page > > *sgx_encl_get_backing_page(struct sgx_encl *encl, > > * 0 on success, > > * -errno otherwise. > > */ > > -int sgx_encl_get_backing(struct sgx_encl *encl, unsigned long > > page_index, > > - struct sgx_backing *backing) > > +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); > > struct page *contents; > > @@ -601,6 +607,62 @@ int sgx_encl_get_backing(struct sgx_encl > > *encl, unsigned long page_index, > > return 0; > > } > > > > +/** > > + * sgx_encl_alloc_backing() - allocate a new backing storage page > > + * @encl: an enclave pointer > > + * @page_index: enclave page index > > + * @backing: data for accessing backing storage for the page > > + * > > + * Confirm that the global overcommit limit has not been reached > > before > > + * requesting a new backing storage page for storing the encrypted > > contents > > + * and Paging Crypto MetaData (PCMD) of an enclave page. This is > > called when > > + * there is no existing backing page, just before writing to the > > backing > > + * storage with EWB. > > + * > > + * Return: > > + * 0 on success, > > + * -errno otherwise. > > + */ > > +int sgx_encl_alloc_backing(struct sgx_encl *encl, unsigned long > > page_index, > > + struct sgx_backing *backing) > > +{ > > + int ret; > > + > > + if (sgx_charge_mem()) > > + return -ENOMEM; > > + > > + ret = sgx_encl_get_backing(encl, page_index, backing); > > + if (ret) > > + sgx_uncharge_mem(); > > + > > + return ret; > > +} > > + > > +/** > > + * sgx_encl_lookup_backing() - retrieve an existing backing > > storage page > > + * @encl: an enclave pointer > > + * @page_index: enclave page index > > + * @backing: data for accessing backing storage for the page > > + * > > + * Retrieve a backing page for loading data back into an EPC page > > with ELDU. > > + * This call does not cause a charge to the overcommit limit > > because a page > > + * has already been allocated, but has been swapped out or is in > > RAM > > + * > > + * It is the caller's responsibility to ensure that it is > > appropriate to > > + * use sgx_encl_lookup_backing() rather than > > sgx_encl_alloc_backing(). If > > + * lookup is not used correctly, this will cause an allocation > > that is > > + * not accounted for. > > + * > > + * Return: > > + * 0 on success, > > + * -errno otherwise. > > + */ > > +int sgx_encl_lookup_backing(struct sgx_encl *encl, unsigned long > > page_index, > > + struct sgx_backing *backing) > > +{ > > + return sgx_encl_get_backing(encl, page_index, backing); > > +} > > IMHO, sgx_encl_backing() should be open-coded here. I can understand your hesitation, but I agree with Dave here that wrapping the function makes the code more clear. I would prefer to keep this the way it is.