Received: by 2002:a05:6a10:1a4d:0:0:0:0 with SMTP id nk13csp695704pxb; Mon, 7 Feb 2022 23:44:26 -0800 (PST) X-Google-Smtp-Source: ABdhPJxNnVoxleemg5/SaW7/WbrLyrBt216prl+dFSve+17h+icmuFeJZHpQCCGq5wbrWxpvpvSz X-Received: by 2002:aa7:df16:: with SMTP id c22mr3096620edy.177.1644306266639; Mon, 07 Feb 2022 23:44:26 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1644306266; cv=none; d=google.com; s=arc-20160816; b=0Ie9qlyNu7rfc95it+fJWF4bxkOS4ozre0lbzLvWt2D3vuwtvXd+jOoaUO9CGMVvR3 e8ZatCwL12xDqtmN3bs711QxLEZqI+zf9bllsYQVBdST28BVEzESrc6V+xRMknPn1ldr TpoZsgSvd1UJT3jS1V/nMr0UKgmWqVeXvAoW3I1B7k2Le1sR7TGnYq3dAAtVk/nz2Zl9 h6yaQdfBIWIynGfS7yKgDeTF5Fi8bw7MODCcHVNXiSeFJXsksL40hxFjslqGcFIZvxnL EfSTqwh0mLmKYAqc84VOysSZ6k1g+BmvDGdb4SLnwNQjR3IxstXpgiWDGGWB30hbFLrR 0pEA== 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=GD7yJxb+l6w4l23hoTdTZOGm2YHB2aTPDP5YFoi1lw0=; b=L2rqTZIQuz4NFR1XdbEVby5adjvcjvwDyDrq3KzlfuNARgCCFTn2rUNLQXkcf76JzD k1OBqnkkiJ8Yyslox2h8GippaKMz1wv47UKF3RioLXt0bGEeixiBPkJK3c2BaIWWeoFD a1ERBnZRWZsrTuTy8hvoZbgsMgwaAb4JFOB2vy8TkWxe0BdPxpVSCLag+xVCUBfPwmi4 ROjAPLBMb2f3CqMqJ5CpiKR2fjYd8iYqX8Ur5KIUxr564v9A2TYz2Ug9zrYUOekZp5WQ 2xocdwTkNtH9N/8jYFvinlICKXxI9VNSG4d6R2AbQviCvmG5K9AuCreV0vfC1aSUaUfl YaoA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=M6VIg3+U; 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 Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id qf18si8998982ejc.854.2022.02.07.23.44.01; Mon, 07 Feb 2022 23:44:26 -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=M6VIg3+U; 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 S1345413AbiBHBLT (ORCPT + 99 others); Mon, 7 Feb 2022 20:11:19 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59486 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1344225AbiBHAqS (ORCPT ); Mon, 7 Feb 2022 19:46:18 -0500 Received: from mga18.intel.com (mga18.intel.com [134.134.136.126]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 4C982C043180; Mon, 7 Feb 2022 16:46:17 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1644281177; x=1675817177; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=/rJMW2flOWfkg1MprTvQqgBi1ZLUHOzTS97piUEK1S4=; b=M6VIg3+UHMXOdquaW6IsO1g0pB0KJ/IW+dct/SX/mIlTr8LMBOZ8xDXB vP0uPk3S7IAmGuBY2sYdJUBCUBANJPVa5dllSJbooh/6dDDI7R1lrbjEP lj65w8ytpa1FzsMpAffTxOtL7yDnUvuXgvJnn9TM6VhAgqpX4ulHMQ+2H 52Cm1DH8wgNoON8DA8qiqSwuuZ+FHfnGtKU9YoiyiIzosBwzG1Io/KjYc ei/Eqq3TsY/aog40rzIjl2pzKJDCxeuQeJGeOshOnuIPzWOhPyYUbmvv+ 5nLCTL6OqxtClBoSKZdg2WjbIc5bPVVeOAE/64cnDpS9LMCHsmQNXJmDc Q==; X-IronPort-AV: E=McAfee;i="6200,9189,10251"; a="232407996" X-IronPort-AV: E=Sophos;i="5.88,351,1635231600"; d="scan'208";a="232407996" Received: from orsmga002.jf.intel.com ([10.7.209.21]) by orsmga106.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 07 Feb 2022 16:46:10 -0800 X-IronPort-AV: E=Sophos;i="5.88,351,1635231600"; d="scan'208";a="499389563" Received: from rchatre-ws.ostc.intel.com ([10.54.69.144]) by orsmga002-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 07 Feb 2022 16:46:09 -0800 From: Reinette Chatre To: dave.hansen@linux.intel.com, jarkko@kernel.org, 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 Subject: [PATCH V2 31/32] x86/sgx: Free up EPC pages directly to support large page ranges Date: Mon, 7 Feb 2022 16:45:53 -0800 Message-Id: <5abcc02a9b6c7bf5fbfb301c3311ff755965d861.1644274683.git.reinette.chatre@intel.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,SPF_HELO_NONE, SPF_NONE,T_SCC_BODY_TEXT_LINE 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 The page reclaimer ensures availability of EPC pages across all enclaves. In support of this it runs independently from the individual enclaves in order to take locks from the different enclaves as it writes pages to swap. When needing to load a page from swap an EPC page needs to be available for its contents to be loaded into. Loading an existing enclave page from swap does not reclaim EPC pages directly if none are available, instead the reclaimer is woken when the available EPC pages are found to be below a watermark. When iterating over a large number of pages in an oversubscribed environment there is a race between the reclaimer woken up and EPC pages reclaimed fast enough for the page operations to proceed. Ensure there are EPC pages available before attempting to load a page that may potentially be pulled from swap into an available EPC page. Signed-off-by: Reinette Chatre --- Changes since v1: - Reword commit message. arch/x86/kernel/cpu/sgx/ioctl.c | 6 ++++++ arch/x86/kernel/cpu/sgx/main.c | 6 ++++++ arch/x86/kernel/cpu/sgx/sgx.h | 1 + 3 files changed, 13 insertions(+) diff --git a/arch/x86/kernel/cpu/sgx/ioctl.c b/arch/x86/kernel/cpu/sgx/ioctl.c index 0ffb07095a80..d8c3c07badb3 100644 --- a/arch/x86/kernel/cpu/sgx/ioctl.c +++ b/arch/x86/kernel/cpu/sgx/ioctl.c @@ -962,6 +962,8 @@ static long sgx_enclave_restrict_perm(struct sgx_encl *encl, for (c = 0 ; c < modp->length; c += PAGE_SIZE) { addr = encl->base + modp->offset + c; + sgx_direct_reclaim(); + mutex_lock(&encl->lock); entry = sgx_encl_load_page(encl, addr); @@ -1156,6 +1158,8 @@ static long sgx_enclave_modt(struct sgx_encl *encl, for (c = 0 ; c < modt->length; c += PAGE_SIZE) { addr = encl->base + modt->offset + c; + sgx_direct_reclaim(); + mutex_lock(&encl->lock); entry = sgx_encl_load_page(encl, addr); @@ -1354,6 +1358,8 @@ static long sgx_encl_remove_pages(struct sgx_encl *encl, for (c = 0 ; c < params->length; c += PAGE_SIZE) { addr = encl->base + params->offset + c; + sgx_direct_reclaim(); + mutex_lock(&encl->lock); entry = sgx_encl_load_page(encl, addr); diff --git a/arch/x86/kernel/cpu/sgx/main.c b/arch/x86/kernel/cpu/sgx/main.c index 6e2cb7564080..545da16bb3ea 100644 --- a/arch/x86/kernel/cpu/sgx/main.c +++ b/arch/x86/kernel/cpu/sgx/main.c @@ -370,6 +370,12 @@ static bool sgx_should_reclaim(unsigned long watermark) !list_empty(&sgx_active_page_list); } +void sgx_direct_reclaim(void) +{ + if (sgx_should_reclaim(SGX_NR_LOW_PAGES)) + sgx_reclaim_pages(); +} + static int ksgxd(void *p) { set_freezable(); diff --git a/arch/x86/kernel/cpu/sgx/sgx.h b/arch/x86/kernel/cpu/sgx/sgx.h index b30cee4de903..85cbf103b0dd 100644 --- a/arch/x86/kernel/cpu/sgx/sgx.h +++ b/arch/x86/kernel/cpu/sgx/sgx.h @@ -86,6 +86,7 @@ static inline void *sgx_get_epc_virt_addr(struct sgx_epc_page *page) struct sgx_epc_page *__sgx_alloc_epc_page(void); void sgx_free_epc_page(struct sgx_epc_page *page); +void sgx_direct_reclaim(void); void sgx_mark_page_reclaimable(struct sgx_epc_page *page); int sgx_unmark_page_reclaimable(struct sgx_epc_page *page); struct sgx_epc_page *sgx_alloc_epc_page(void *owner, bool reclaim); -- 2.25.1