Received: by 2002:a05:6a10:2726:0:0:0:0 with SMTP id ib38csp773637pxb; Tue, 5 Apr 2022 22:44:44 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyPSVZ5sqekjJWLTBLVh81/6+frukU4FSRGdx3y2S9KwDHAwsiQC58ftwGxKV4zvomyORdT X-Received: by 2002:a17:902:8ec8:b0:154:5ecb:eb05 with SMTP id x8-20020a1709028ec800b001545ecbeb05mr6978642plo.56.1649223884572; Tue, 05 Apr 2022 22:44:44 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1649223884; cv=none; d=google.com; s=arc-20160816; b=lo2TYyc7gLO6caMC9AvQkQFmC2mhK2rpfMPo7OVbRxuxkM8dJ9tUZUVGgOxlW7eTbz 8U7LbqARmCcaIOFP8UhKj/XMWFaDj7X2JsRQI5pOluCH0DVkvYjS7xr/pdTz7MAPo2b3 EjrRLhSOXtq7TENCuqz92hcgQW+VqlGUFNPFVFX5gYziUCWSUY2Jc9rmouW79DFScq7j 4eL4Hb78mtiaSoWNvOPmaUiAgJrPATzD+sxAXGKswT0MZW95QmqpuWwVHQT/1kLacdjw foXc6ZBXt6jRj1GCe1jUqRPhLXnL5cK76p8/QIfywGlZkiDYLGUhzx2tD74wRxRWOyZ3 9B8g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:mime-version:user-agent :content-transfer-encoding:references:in-reply-to:date:cc:to:from :subject:message-id:dkim-signature; bh=hPS68ta6Tx4zhA9DEjz9ol4zuAM1pHbQpkUR5XA8yMs=; b=O29159na8S8hb9mHrSKVCBapnYcqOWxi5i4rj1iTcrhTaJlwvGFCTW7qifcbxngkAM 24uVrjY/JagksgtupDAorxCLh0RX51EjxURaryrK4hL99tsqya48e8IoGiUA6cwJjWJ+ AQu72frHG1FBhuKE+yOUsLWskJMnhaOt/xl+H3CaQgpwNY48WFxsEMuaGw+zMJ3PtciF fojpnszFw/wq4wait+gakp1Hu3NiO8KhKHfUMkiOI4q/elQ9oH/tQfpIwSPf4MWbD3IF MnnbvqZjcGbpvxWy/vw8TfOnUuDjlIY/SNxfQrMEdw2BsByUvutQGdGs9afS1xskFOEi bJLw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=Y22IBL2T; spf=softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net. [23.128.96.19]) by mx.google.com with ESMTPS id i13-20020a655b8d000000b003994c59447csi5986801pgr.104.2022.04.05.22.44.44 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 05 Apr 2022 22:44:44 -0700 (PDT) Received-SPF: softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) client-ip=23.128.96.19; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=Y22IBL2T; spf=softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id A7472467E1F; Tue, 5 Apr 2022 21:57:47 -0700 (PDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1588785AbiDFARU (ORCPT + 99 others); Tue, 5 Apr 2022 20:17:20 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56014 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1354664AbiDEKPD (ORCPT ); Tue, 5 Apr 2022 06:15:03 -0400 Received: from ams.source.kernel.org (ams.source.kernel.org [IPv6:2604:1380:4601:e00::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id F15916C1F8; Tue, 5 Apr 2022 03:02:20 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ams.source.kernel.org (Postfix) with ESMTPS id 7B53CB81C98; Tue, 5 Apr 2022 10:02:19 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id CFDACC385A2; Tue, 5 Apr 2022 10:02:17 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1649152938; bh=hPS68ta6Tx4zhA9DEjz9ol4zuAM1pHbQpkUR5XA8yMs=; h=Subject:From:To:Cc:Date:In-Reply-To:References:From; b=Y22IBL2T6dsXLg6kfNgEus0hgN6dGinZ39wTT6FuQ78x9CrB1kNDeAA2guy7GC2UY oFUVRXKA5o9MKHGPP5m1z9Xvaj75evHZDQ2lrEG0gS/1hO/U47SHD+0U297ejAsUXG KlaRwtyGWubv8gI2IR2KzbvNTvQ8QQIdogCZAwHXtEiIruCxlrgUGZadqw3E7HSkFA 8vZgw35FOH5GL8UqhiVkw5Q6hX764u5+2Xr9/FuhMFOhtF9WiiLnsUbE5ljf/aJhhk etf+PVB1LX+xQzJpv5peYW3GIVflo4lXeznSD2WeWmfD2c4/jUSHPi/oFnsUwWAmua 6CDaiaWoZyVVw== Message-ID: Subject: Re: [PATCH V3 15/30] x86/sgx: Support adding of pages to an initialized enclave From: Jarkko Sakkinen To: Reinette Chatre , dave.hansen@linux.intel.com, tglx@linutronix.de, bp@alien8.de, luto@kernel.org, mingo@redhat.com, linux-sgx@vger.kernel.org, x86@kernel.org Cc: seanjc@google.com, kai.huang@intel.com, cathy.zhang@intel.com, cedric.xing@intel.com, haitao.huang@intel.com, mark.shanahan@intel.com, hpa@zytor.com, linux-kernel@vger.kernel.org Date: Tue, 05 Apr 2022 13:03:28 +0300 In-Reply-To: References: <50b9da1f491eb31d9b038afa0d75871965f474eb.1648847675.git.reinette.chatre@intel.com> Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable User-Agent: Evolution 3.42.4 MIME-Version: 1.0 X-Spam-Status: No, score=-2.3 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,MAILING_LIST_MULTI, RDNS_NONE,SPF_HELO_NONE,T_SCC_BODY_TEXT_LINE autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Tue, 2022-04-05 at 08:05 +0300, Jarkko Sakkinen wrote: > On Mon, 2022-04-04 at 09:49 -0700, Reinette Chatre wrote: > > With SGX1 an enclave needs to be created with its maximum memory demand= s > > allocated. Pages cannot be added to an enclave after it is initialized. > > SGX2 introduces a new function, ENCLS[EAUG], that can be used to add > > pages to an initialized enclave. With SGX2 the enclave still needs to > > set aside address space for its maximum memory demands during enclave > > creation, but all pages need not be added before enclave initialization= . > > Pages can be added during enclave runtime. > >=20 > > Add support for dynamically adding pages to an initialized enclave, > > architecturally limited to RW permission at creation but allowed to > > obtain RWX permissions after enclave runs EMODPE. Add pages via the > > page fault handler at the time an enclave address without a backing > > enclave page is accessed, potentially directly reclaiming pages if > > no free pages are available. > >=20 > > The enclave is still required to run ENCLU[EACCEPT] on the page before > > it can be used. A useful flow is for the enclave to run ENCLU[EACCEPT] > > on an uninitialized address. This will trigger the page fault handler > > that will add the enclave page and return execution to the enclave to > > repeat the ENCLU[EACCEPT] instruction, this time successful. > >=20 > > If the enclave accesses an uninitialized address in another way, for > > example by expanding the enclave stack to a page that has not yet been > > added, then the page fault handler would add the page on the first > > write but upon returning to the enclave the instruction that triggered > > the page fault would be repeated and since ENCLU[EACCEPT] was not run > > yet it would trigger a second page fault, this time with the SGX flag > > set in the page fault error code. This can only be recovered by enterin= g > > the enclave again and directly running the ENCLU[EACCEPT] instruction o= n > > the now initialized address. > >=20 > > Accessing an uninitialized address from outside the enclave also > > triggers this flow but the page will remain inaccessible (access will > > result in #PF) until accepted from within the enclave via > > ENCLU[EACCEPT]. > >=20 > > Signed-off-by: Reinette Chatre > > --- > > Changes since V2: > > - Remove runtime tracking of EPCM permissions > > =C2=A0 (sgx_encl_page->vm_run_prot_bits) (Jarkko). > > - Move export of sgx_encl_{grow,shrink}() to separate patch. (Jarkko) > > - Use sgx_encl_page_alloc(). (Jarkko) > > - Set max allowed permissions to be RWX (Jarkko). Update changelog > > =C2=A0 to indicate the change and use comment in code as > > =C2=A0 created by Jarkko in: > > https://lore.kernel.org/linux-sgx/20220306053211.135762-4-jarkko@kernel= .org > > - Do not set protection bits but let it be inherited by VMA (Jarkko) > >=20 > > Changes since V1: > > - Fix subject line "to initialized" -> "to an initialized" (Jarkko). > > - Move text about hardware's PENDING state to the patch that introduces > > =C2=A0 the ENCLS[EAUG] wrapper (Jarkko). > > - Ensure kernel-doc uses brackets when referring to function. > >=20 > > =C2=A0arch/x86/kernel/cpu/sgx/encl.c | 124 ++++++++++++++++++++++++++++= +++++ > > =C2=A01 file changed, 124 insertions(+) > >=20 > > diff --git a/arch/x86/kernel/cpu/sgx/encl.c b/arch/x86/kernel/cpu/sgx/e= ncl.c > > index 546423753e4c..fa4f947f8496 100644 > > --- a/arch/x86/kernel/cpu/sgx/encl.c > > +++ b/arch/x86/kernel/cpu/sgx/encl.c > > @@ -194,6 +194,119 @@ struct sgx_encl_page *sgx_encl_load_page(struct s= gx_encl *encl, > > =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0return __sgx_encl_load_= page(encl, entry); > > =C2=A0} > > =C2=A0 > > +/** > > + * sgx_encl_eaug_page() - Dynamically add page to initialized enclave > > + * @vma:=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0VMA obtained from fa= ult info from where page is accessed > > + * @encl:=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0enclave accessing the pag= e > > + * @addr:=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0address that triggered th= e page fault > > + * > > + * When an initialized enclave accesses a page with no backing EPC pag= e > > + * on a SGX2 system then the EPC can be added dynamically via the SGX2 > > + * ENCLS[EAUG] instruction. > > + * > > + * Returns: Appropriate vm_fault_t: VM_FAULT_NOPAGE when PTE was insta= lled > > + * successfully, VM_FAULT_SIGBUS or VM_FAULT_OOM as error otherwise. > > + */ > > +static vm_fault_t sgx_encl_eaug_page(struct vm_area_struct *vma, > > +=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2= =A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0= =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 struct s= gx_encl *encl, unsigned long addr) > > +{ > > +=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0struct sgx_pageinfo pginfo = =3D {0}; > > +=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0struct sgx_encl_page *encl_p= age; > > +=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0struct sgx_epc_page *epc_pag= e; > > +=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0struct sgx_va_page *va_page; > > +=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0unsigned long phys_addr; > > +=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0u64 secinfo_flags; > > +=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0vm_fault_t vmret; > > +=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0int ret; > > + > > +=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0if (!test_bit(SGX_ENCL_INITI= ALIZED, &encl->flags)) > > +=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2= =A0=C2=A0=C2=A0=C2=A0return VM_FAULT_SIGBUS; > > + > > +=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0/* > > +=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 * Ignore internal permissio= n checking for dynamically added pages. > > +=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 * They matter only for data= added during the pre-initialization > > +=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 * phase. The enclave decide= s the permissions by the means of > > +=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 * EACCEPT, EACCEPTCOPY and = EMODPE. > > +=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 */ > > +=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0secinfo_flags =3D SGX_SECINF= O_R | SGX_SECINFO_W | SGX_SECINFO_X; > > +=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0encl_page =3D sgx_encl_page_= alloc(encl, addr - encl->base, secinfo_flags); > > +=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0if (IS_ERR(encl_page)) > > +=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2= =A0=C2=A0=C2=A0=C2=A0return VM_FAULT_OOM; > > + > > +=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0epc_page =3D sgx_alloc_epc_p= age(encl_page, true); > > +=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0if (IS_ERR(epc_page)) { > > +=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2= =A0=C2=A0=C2=A0=C2=A0kfree(encl_page); > > +=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2= =A0=C2=A0=C2=A0=C2=A0return VM_FAULT_SIGBUS; > > +=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0} > > + > > +=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0va_page =3D sgx_encl_grow(en= cl); > > +=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0if (IS_ERR(va_page)) { > > +=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2= =A0=C2=A0=C2=A0=C2=A0ret =3D PTR_ERR(va_page); > > +=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2= =A0=C2=A0=C2=A0=C2=A0goto err_out_free; > > +=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0} > > + > > +=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0mutex_lock(&encl->lock); > > + > > +=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0/* > > +=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 * Copy comment from sgx_enc= l_add_page() to maintain guidance in > > +=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 * this similar flow: > > +=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 * Adding to encl->va_pages = must be done under encl->lock.=C2=A0 Ditto for > > +=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 * deleting (via sgx_encl_sh= rink()) in the error path. > > +=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 */ > > +=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0if (va_page) > > +=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2= =A0=C2=A0=C2=A0=C2=A0list_add(&va_page->list, &encl->va_pages); > > + > > +=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0ret =3D xa_insert(&encl->pag= e_array, PFN_DOWN(encl_page->desc), > > +=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2= =A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0encl_p= age, GFP_KERNEL); > > +=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0/* > > +=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 * If ret =3D=3D -EBUSY then= page was created in another flow while > > +=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 * running without encl->loc= k > > +=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 */ > > +=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0if (ret) > > +=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2= =A0=C2=A0=C2=A0=C2=A0goto err_out_unlock; > > + > > +=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0pginfo.secs =3D (unsigned lo= ng)sgx_get_epc_virt_addr(encl->secs.epc_page); > > +=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0pginfo.addr =3D encl_page->d= esc & PAGE_MASK; > > +=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0pginfo.metadata =3D 0; > > + > > +=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0ret =3D __eaug(&pginfo, sgx_= get_epc_virt_addr(epc_page)); > > +=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0if (ret) > > +=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2= =A0=C2=A0=C2=A0=C2=A0goto err_out; > > + > > +=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0encl_page->encl =3D encl; > > +=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0encl_page->epc_page =3D epc_= page; > > +=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0encl_page->type =3D SGX_PAGE= _TYPE_REG; > > +=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0encl->secs_child_cnt++; > > + > > +=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0sgx_mark_page_reclaimable(en= cl_page->epc_page); > > + > > +=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0phys_addr =3D sgx_get_epc_ph= ys_addr(epc_page); > > +=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0/* > > +=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 * Do not undo everything wh= en creating PTE entry fails - next #PF > > +=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 * would find page ready for= a PTE. > > +=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 */ > > +=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0vmret =3D vmf_insert_pfn(vma= , addr, PFN_DOWN(phys_addr)); > > +=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0if (vmret !=3D VM_FAULT_NOPA= GE) { > > +=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2= =A0=C2=A0=C2=A0=C2=A0mutex_unlock(&encl->lock); > > +=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2= =A0=C2=A0=C2=A0=C2=A0return VM_FAULT_SIGBUS; > > +=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0} > > +=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0mutex_unlock(&encl->lock); > > +=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0return VM_FAULT_NOPAGE; > > + > > +err_out: > > +=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0xa_erase(&encl->page_array, = PFN_DOWN(encl_page->desc)); > > + > > +err_out_unlock: > > +=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0sgx_encl_shrink(encl, va_pag= e); > > +=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0mutex_unlock(&encl->lock); > > + > > +err_out_free: > > +=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0sgx_encl_free_epc_page(epc_p= age); > > +=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0kfree(encl_page); > > + > > +=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0return VM_FAULT_SIGBUS; > > +} > > + > > =C2=A0static vm_fault_t sgx_vma_fault(struct vm_fault *vmf) > > =C2=A0{ > > =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0unsigned long addr =3D = (unsigned long)vmf->address; > > @@ -213,6 +326,17 @@ static vm_fault_t sgx_vma_fault(struct vm_fault *v= mf) > > =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0if (unlikely(!encl)) > > =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2= =A0=C2=A0=C2=A0=C2=A0=C2=A0return VM_FAULT_SIGBUS; > > =C2=A0 > > +=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0/* > > +=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 * The page_array keeps trac= k of all enclave pages, whether they > > +=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 * are swapped out or not. I= f there is no entry for this page and > > +=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 * the system supports SGX2 = then it is possible to dynamically add > > +=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 * a new enclave page. This = is only possible for an initialized > > +=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 * enclave that will be chec= ked for right away. > > +=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 */ > > +=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0if (cpu_feature_enabled(X86_= FEATURE_SGX2) && > > +=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 (!xa_load= (&encl->page_array, PFN_DOWN(addr)))) > > +=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2= =A0=C2=A0=C2=A0=C2=A0return sgx_encl_eaug_page(vma, encl, addr); > > + > > =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0mutex_lock(&encl->lock)= ; > > =C2=A0 > > =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0entry =3D sgx_encl_load= _page_in_vma(encl, addr, vma->vm_flags); >=20 > Reviewed-by: Jarkko Sakkinen Tested-by: Jarkko Sakkinen BR, Jarkko