From patchwork Fri Dec 2 18:36:38 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kristen Carlson Accardi X-Patchwork-Id: 29065 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:f944:0:0:0:0:0 with SMTP id q4csp1016057wrr; Fri, 2 Dec 2022 10:43:34 -0800 (PST) X-Google-Smtp-Source: AA0mqf4QmJsuqA9gLV3SScEbCjLJYhD498/JKW4l/G++Lq0JxdFyzjDlAaDw2qpqqlS6ay1+uLF3 X-Received: by 2002:a50:eaca:0:b0:46b:6bc1:1b4a with SMTP id u10-20020a50eaca000000b0046b6bc11b4amr18776605edp.221.1670006614400; Fri, 02 Dec 2022 10:43:34 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1670006614; cv=none; d=google.com; s=arc-20160816; b=MaHfWqlmwyfJ7Jg2REg/LE0xntoJ1BMw4D+VYaGNNplg3wKZvrattcSP02TvvgfQKm deWEefu/dFPHYIoEuai4lyd/49AGNM73OkCg1I2MA1T/gvbJmGwiRul24bvU+J2sd9fN LL/ls1CPvjPhmXoI8JIp2l841j8V5A11esx3EURfKi6ntHUtDhHgigTJpSmK8PmLi3ph jVSgiBHBFwCgL77K6y80Qi+JfNTYVTUeGWZupKgpHnt6ErfPWtLVHZZAOTAcPnZ6xFAu b1flZsWC/1Gm1bnsjfzKVc7xk9u6UT2TOPapNFrXuMuINWcdeXQhpSfTSlWgc3Vnez67 DQ6w== 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 :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=hLAJ7Gg1rSbTxQmdH7VvXNSZor1QA1TjBf5mEOK+8o8=; b=OMFfkq54ss3Vky0/+kNtg/syF0olP5wXsZkflau9M0igwwAq/xZN/7POYZXRBTVWWj hW9dYu+nGyStt6CS0aDMVsjLPL81fR4gpmzvEY3w0O3CW0f/jUy+k5LD80PUzsTiCD5A tQ86/ea4CADMOz6x3hIG78LacxyEYuZ8okCcNAoQp+0trfjBMZEWgCd7Q+iNFfl6I/uW UlHHamWBFd+9GBqc0LFpocwDRcQcogUdEWIZK981vck98JQH+2S1tlJj06budhDACuvD m+BMb0S7vdrAc43Px0hULwvQ7AcaHR7fCOEQRAS7Nafeuko3SSRVXN+3LQ43iZerXW84 i6ng== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=fxbfFMvt; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id jg39-20020a170907972700b007c09fda33c7si6516347ejc.824.2022.12.02.10.43.11; Fri, 02 Dec 2022 10:43:34 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=fxbfFMvt; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 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 S234516AbiLBShQ (ORCPT + 99 others); Fri, 2 Dec 2022 13:37:16 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43776 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234473AbiLBShJ (ORCPT ); Fri, 2 Dec 2022 13:37:09 -0500 Received: from mga04.intel.com (mga04.intel.com [192.55.52.120]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E3830EDD4D; Fri, 2 Dec 2022 10:37:08 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1670006228; x=1701542228; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=Ba88DeV8/njUEzN15e2sj/am8fPP9lPmiVElJCF0C+U=; b=fxbfFMvt9WUMNgypsz/7z/fJyPXW7QBFRkSLf/tQhaARtEdM4IrD/8hP rRPgwIgZsHixQn6kYqx4h+0TbhqPyKUblU5u/muAaTwDKmaVUR9Y4yWST bNGurGrB4V4GoaerfRDi9BEbLH+l8nU6zzlhX+06jbXHQbD2bS9HThx7j 6lhVhslfHoMONlzVYZU/FIqBd20Hm3kLNFtWsMcg0T57kTIbhqQpt5IkP dmRrnGN02g4xvrWocP/y2wIkMD5bcir8aklDkLgC2Z7ny7OQsBMLWtEov kKn4iOOUss1toSYrL08roAsXU9llKBnUkBUZuZuE2WR1/M5kcLCcTSXrL g==; X-IronPort-AV: E=McAfee;i="6500,9779,10549"; a="314724495" X-IronPort-AV: E=Sophos;i="5.96,213,1665471600"; d="scan'208";a="314724495" Received: from fmsmga004.fm.intel.com ([10.253.24.48]) by fmsmga104.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 02 Dec 2022 10:37:08 -0800 X-IronPort-AV: E=McAfee;i="6500,9779,10549"; a="713717366" X-IronPort-AV: E=Sophos;i="5.96,213,1665471600"; d="scan'208";a="713717366" Received: from kcaskeyx-mobl1.amr.corp.intel.com (HELO kcaccard-desk.amr.corp.intel.com) ([10.251.1.207]) by fmsmga004-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 02 Dec 2022 10:37:06 -0800 From: Kristen Carlson Accardi To: jarkko@kernel.org, dave.hansen@linux.intel.com, tj@kernel.org, linux-kernel@vger.kernel.org, linux-sgx@vger.kernel.org, cgroups@vger.kernel.org, Thomas Gleixner , Ingo Molnar , Borislav Petkov , x86@kernel.org, "H. Peter Anvin" Cc: zhiquan1.li@intel.com, Kristen Carlson Accardi , Sean Christopherson Subject: [PATCH v2 02/18] x86/sgx: Store struct sgx_encl when allocating new VA pages Date: Fri, 2 Dec 2022 10:36:38 -0800 Message-Id: <20221202183655.3767674-3-kristen@linux.intel.com> X-Mailer: git-send-email 2.38.1 In-Reply-To: <20221202183655.3767674-1-kristen@linux.intel.com> References: <20221202183655.3767674-1-kristen@linux.intel.com> MIME-Version: 1.0 X-Spam-Status: No, score=-4.3 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_EF,RCVD_IN_DNSWL_MED,SPF_HELO_NONE, SPF_NONE autolearn=ham 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 X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1751128855389057928?= X-GMAIL-MSGID: =?utf-8?q?1751128855389057928?= From: Sean Christopherson When allocating new Version Array (VA) pages, pass the struct sgx_encl of the enclave that is allocating the page. sgx_alloc_epc_page() will store this value in the encl_owner field of the struct sgx_epc_page. In a later patch, VA pages will be placed in an unreclaimable queue, and then when the cgroup max limit is reached and there are no more reclaimable pages and the enclave must be oom killed, all the VA pages associated with that enclave can be uncharged and freed. Signed-off-by: Sean Christopherson Signed-off-by: Kristen Carlson Accardi Cc: Sean Christopherson --- arch/x86/kernel/cpu/sgx/encl.c | 5 +++-- arch/x86/kernel/cpu/sgx/encl.h | 2 +- arch/x86/kernel/cpu/sgx/ioctl.c | 2 +- arch/x86/kernel/cpu/sgx/sgx.h | 1 + 4 files changed, 6 insertions(+), 4 deletions(-) diff --git a/arch/x86/kernel/cpu/sgx/encl.c b/arch/x86/kernel/cpu/sgx/encl.c index f40d64206ded..4eaf9d21e71b 100644 --- a/arch/x86/kernel/cpu/sgx/encl.c +++ b/arch/x86/kernel/cpu/sgx/encl.c @@ -1193,6 +1193,7 @@ void sgx_zap_enclave_ptes(struct sgx_encl *encl, unsigned long addr) /** * sgx_alloc_va_page() - Allocate a Version Array (VA) page + * @encl: The enclave that this page is allocated to. * @reclaim: Reclaim EPC pages directly if none available. Enclave * mutex should not be held if this is set. * @@ -1202,12 +1203,12 @@ void sgx_zap_enclave_ptes(struct sgx_encl *encl, unsigned long addr) * a VA page, * -errno otherwise */ -struct sgx_epc_page *sgx_alloc_va_page(bool reclaim) +struct sgx_epc_page *sgx_alloc_va_page(struct sgx_encl *encl, bool reclaim) { struct sgx_epc_page *epc_page; int ret; - epc_page = sgx_alloc_epc_page(NULL, reclaim); + epc_page = sgx_alloc_epc_page(encl, reclaim); if (IS_ERR(epc_page)) return ERR_CAST(epc_page); diff --git a/arch/x86/kernel/cpu/sgx/encl.h b/arch/x86/kernel/cpu/sgx/encl.h index f94ff14c9486..831d63f80f5a 100644 --- a/arch/x86/kernel/cpu/sgx/encl.h +++ b/arch/x86/kernel/cpu/sgx/encl.h @@ -116,7 +116,7 @@ struct sgx_encl_page *sgx_encl_page_alloc(struct sgx_encl *encl, unsigned long offset, u64 secinfo_flags); void sgx_zap_enclave_ptes(struct sgx_encl *encl, unsigned long addr); -struct sgx_epc_page *sgx_alloc_va_page(bool reclaim); +struct sgx_epc_page *sgx_alloc_va_page(struct sgx_encl *encl, bool reclaim); unsigned int sgx_alloc_va_slot(struct sgx_va_page *va_page); void sgx_free_va_slot(struct sgx_va_page *va_page, unsigned int offset); bool sgx_va_page_full(struct sgx_va_page *va_page); diff --git a/arch/x86/kernel/cpu/sgx/ioctl.c b/arch/x86/kernel/cpu/sgx/ioctl.c index ebe79d60619f..9a1bb3c3211a 100644 --- a/arch/x86/kernel/cpu/sgx/ioctl.c +++ b/arch/x86/kernel/cpu/sgx/ioctl.c @@ -30,7 +30,7 @@ struct sgx_va_page *sgx_encl_grow(struct sgx_encl *encl, bool reclaim) if (!va_page) return ERR_PTR(-ENOMEM); - va_page->epc_page = sgx_alloc_va_page(reclaim); + va_page->epc_page = sgx_alloc_va_page(encl, reclaim); if (IS_ERR(va_page->epc_page)) { err = ERR_CAST(va_page->epc_page); kfree(va_page); diff --git a/arch/x86/kernel/cpu/sgx/sgx.h b/arch/x86/kernel/cpu/sgx/sgx.h index d16a8baa28d4..39cb15a8abcb 100644 --- a/arch/x86/kernel/cpu/sgx/sgx.h +++ b/arch/x86/kernel/cpu/sgx/sgx.h @@ -39,6 +39,7 @@ struct sgx_epc_page { struct sgx_encl_page *encl_owner; /* Use when SGX_EPC_PAGE_KVM_GUEST set in ->flags: */ void __user *vepc_vaddr; + struct sgx_encl *encl; }; struct list_head list; };