Received: by 2002:a05:6a10:2726:0:0:0:0 with SMTP id ib38csp80613pxb; Tue, 5 Apr 2022 00:42:46 -0700 (PDT) X-Google-Smtp-Source: ABdhPJy+oZ9nqQM4VZwa8XM2NKya9ikFDVLywMMpyS/2bIEVuv6U1tuBe3r568CjLI/15WFI10I6 X-Received: by 2002:a17:90b:3908:b0:1c7:7a14:2083 with SMTP id ob8-20020a17090b390800b001c77a142083mr2617396pjb.230.1649144566551; Tue, 05 Apr 2022 00:42:46 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1649144566; cv=none; d=google.com; s=arc-20160816; b=RNrb1FJTNYK7tBHRAWsaKuopMuQ0htW3ETXq7/3vMtEUOsglquynBlMIMN5LIFgCAR WAfN4gdGrHK7crKkDXA3yun5w04w8x/SkDNje0CYVyFI42jRV+BU5E1rWqdEbGjL83cc Cxgmf6JWgnRpUPL+CIsK2NewwqV/DGEWajtAgAHqxzA9edY05u1PwmhqgC22/q2rWHUc cKb3JnXoD23DYEkZvob324qSsT0lGHuqMJvj0Qsy+QDR/XuO9PGFfkjNAIWY3WW6LTdl SRl7eFa2AgJsdIfZxmF7j2lsIxvkN0Zkky7PTVF4tImwLL7UYI4Hm71Dri136+lM0NT7 VYdw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature; bh=kY3ulExSvdzw+MIS9F3uzzNaw842qe9S1KPqX94VcLo=; b=fooukOMvjFuQTgfY8Surulz4o1tbSLN9lvQPjV0QbbCHnMKEdwVwOZe1QlN+sgX8j1 05e/FFJXQ4k0/vT+ojeAaVD0/T0upIuFXIW2UkhD3UGoKuJ2CoVqSjqZiIzbhlSBwYYo GFWFSIls7TAwW9/zyyNbMn/5tzNnfMm8vbTkHH/nC2ZGnSUSfsW/DSTXY1740/f/lCGy fTiej7g7/nBc5CRRY5jmZOa/C3vzX6d/YKVEbfU2aF0tdWsUMHQOWRn6s14CQagteqTT Po1siFVHjzVXJXaSYYAIW2SbnvlO8rGNegi+4Du1SzB6RMdgl9By3EI/0qosv7wH7o60 MFEg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=d9Eevjye; 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=kernel.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id kk8-20020a17090b4a0800b001c6a149a6dasi1334962pjb.115.2022.04.05.00.42.34; Tue, 05 Apr 2022 00:42:46 -0700 (PDT) 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=@kernel.org header.s=k20201202 header.b=d9Eevjye; 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=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231315AbiDEHMG (ORCPT + 99 others); Tue, 5 Apr 2022 03:12:06 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42126 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229963AbiDEHME (ORCPT ); Tue, 5 Apr 2022 03:12:04 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [IPv6:2604:1380:4641:c500::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 757D54B1E2; Tue, 5 Apr 2022 00:10:05 -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 dfw.source.kernel.org (Postfix) with ESMTPS id 05957615FE; Tue, 5 Apr 2022 07:10:05 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id DEBBAC340EE; Tue, 5 Apr 2022 07:10:03 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1649142604; bh=R0qfjYmhRyfgzwAdAmpXuqarK6SmFDQ1XokwxK9loxQ=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=d9Eevjye9Rlmz7JV6Frf1lwqsLbud2Y9zSDCIowIUYts0TKrl5ypkN4QO1cgmJaen WMi20mJZrflA2PBUknzisE9r4iZ58n6vkXrUtxSJ/uLjAz52FlmrKvznyxBIJtEDh1 tvSP4CGqk8mPiXsxaHeW742OPj+G5lloQf//ajwlrZoZ7pn7OXc0vyU9u6zyNke7MV Wo0smeuYEGKeqjvRfzrMEi3ipSzRYKTA4POp2wPhTHj0112AxsaxrtrvpZrkCtz4R7 LAVXEEdp/OEPrXz1uboo0n22UqMFXCWxjlCnAndVVsJDVPEDj+yPkMgxGpBkC+qN6p LfLjiIYihFTHg== Date: Tue, 5 Apr 2022 10:11:15 +0300 From: Jarkko Sakkinen To: Reinette Chatre Cc: 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, 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: Re: [PATCH V3 19/30] x86/sgx: Free up EPC pages directly to support large page ranges Message-ID: References: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: X-Spam-Status: No, score=-7.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_HI, SPF_HELO_NONE,SPF_PASS,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 On Mon, Apr 04, 2022 at 09:49:27AM -0700, Reinette Chatre wrote: > 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 > --- > No changes since V2 > > 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 515e1961cc02..f88bc1236276 100644 > --- a/arch/x86/kernel/cpu/sgx/ioctl.c > +++ b/arch/x86/kernel/cpu/sgx/ioctl.c > @@ -777,6 +777,8 @@ sgx_enclave_restrict_permissions(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); > @@ -934,6 +936,8 @@ static long sgx_enclave_modify_type(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); > @@ -1129,6 +1133,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(); > +} Please, instead open code this to both locations - not enough redundancy to be worth of new function. Causes only unnecessary cross-referencing when maintaining. Otherwise, I agree with the idea. BR, Jarkko