Received: by 2002:a05:6358:d09b:b0:dc:cd0c:909e with SMTP id jc27csp2508714rwb; Fri, 2 Dec 2022 10:43:11 -0800 (PST) X-Google-Smtp-Source: AA0mqf4jqs4HirXmcMom1sEem3izNKdfN0xJbZIR65djT1ZRtNxdLEQ/Jyj/T3++vbWFNC28Ht3J X-Received: by 2002:a63:fa12:0:b0:456:d859:2145 with SMTP id y18-20020a63fa12000000b00456d8592145mr50303117pgh.56.1670006591504; Fri, 02 Dec 2022 10:43:11 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1670006591; cv=none; d=google.com; s=arc-20160816; b=Wnw2bHIvDAqaDD7XlONqfnjKmDQXVoeKCIjgOmfH0JGcNrbX67epRlW+AWGEGUxob/ dYMbHndPNz9LtbZGDKR1yR7qvsyAik9e4D13lqPOJzSTJYGPsZ0VfumkYl9PXJbTyFi8 vX9PkrQlYw+wMgCyLlshwuxmJPhvdTTj3p0+Qff9sBVVGyig58ddOaLCmw4PYIjksO3y QlvNWAnsoIIBw+HkE44tsGMpnsW4Gokf7dxyjERgghlFz9WUy4OL3n1IHpKdjfKL0Xvd 2orij/97JnTH/2J48nFilJ81RW6Xz7yjMVFaYx3Pw7DrjMM4zQHYyCFxx7wFyU9k4JGZ 9VDg== 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=ijfOVYEj5nexjtxd0isF62SGW/p2VDBhJ/h3aHOhorg=; b=vzZUjMtGhOLysFUAWy0hjOzRNdULIXaJsAUdrCt12IKIpLLGlJEMGIEUnAiDwWd6lK Vit+QYHSRZUvMSCP4NGL5kOmhsnGdtN1s3EnJW1m1qMz6TF269svMIx/uHYeltaHREnO 9zGgkik2TDH3YMnt4JiJWpijymCRnKVPiLxUrMOo0GaY55hxBkAOXrckdfpG4uSL94hb iJkq16G4N0MH4TRGqoWDC0q4JUKLDu6iZu/dpGwaswkLEjKHUM03yPm572Txr0k4C4iS aidhQ/qZNNkQwslpjrx+F1Phd9fGZbhU0mGYWszUGx25NyNpRLSMEjGoNLTqc5c0RuSJ DHqA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=CAz3Zzbs; 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 on17-20020a17090b1d1100b00218de7c19efsi12728624pjb.108.2022.12.02.10.43.00; Fri, 02 Dec 2022 10:43:11 -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=CAz3Zzbs; 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 S234377AbiLBShg (ORCPT + 82 others); Fri, 2 Dec 2022 13:37:36 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43900 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234490AbiLBSh2 (ORCPT ); Fri, 2 Dec 2022 13:37:28 -0500 Received: from mga04.intel.com (mga04.intel.com [192.55.52.120]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 2CB3BEFD01; Fri, 2 Dec 2022 10:37: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=1670006237; x=1701542237; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=XCIiHboLqkGB7+8+/nwb/H+5rbZwuSnGijn3+bFrq9g=; b=CAz3ZzbsfFL4r45Kb8TWwr8PKzA1t8wzyJx/28cE3Hdn8bZtqnx0wHuS ls5aJKfjuUErwVsylRzTtV0VMi8+MuELeplpu+EkaCOREQqPT00la9I4N qAMfujU8qsgBFLnVc476RvS22UPjzq02jXqpj2nR2TdokuTC4QXqRuy6W iIlCFQ69I2L4LDaxyzxIw1RSz6hqhj+3EZOxJtGjjFhQVRmUu9M7uU06l UYtUStsYHqVu0wjEDTrQbrfNJTc5IfHF0Ctz0V2VQLT5Qec9NFQSjqmTb RlkooN39AJKywDEjKl3IL8KmNFfVqQ+Sd/PgXgDYWeAbFBz4mmYLIIm/K A==; X-IronPort-AV: E=McAfee;i="6500,9779,10549"; a="314724549" X-IronPort-AV: E=Sophos;i="5.96,213,1665471600"; d="scan'208";a="314724549" 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:16 -0800 X-IronPort-AV: E=McAfee;i="6500,9779,10549"; a="713717427" X-IronPort-AV: E=Sophos;i="5.96,213,1665471600"; d="scan'208";a="713717427" 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:15 -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 06/18] x86/sgx: Introduce RECLAIM_IN_PROGRESS flag for EPC pages Date: Fri, 2 Dec 2022 10:36:42 -0800 Message-Id: <20221202183655.3767674-7-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 Content-Transfer-Encoding: 8bit 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 From: Sean Christopherson When selecting pages to be reclaimed from the page pool (sgx_global_lru), the list of reclaimable pages is walked, and any page that is both reclaimable and not in the process of being freed is added to a list of potential candidates to be reclaimed. After that, this separate list is further examined and may or may not ultimately be reclaimed. In order to prevent this page from being removed from the sgx_epc_lru_lists struct in a separate thread by sgx_drop_epc_page(), keep track of whether the EPC page is in the middle of being reclaimed with the addtion of a RECLAIM_IN_PROGRESS flag, and do not delete the page off the LRU in sgx_drop_epc_page() if it has not yet finished being reclaimed. Signed-off-by: Sean Christopherson Signed-off-by: Kristen Carlson Accardi Cc: Sean Christopherson --- arch/x86/kernel/cpu/sgx/main.c | 15 ++++++++++----- arch/x86/kernel/cpu/sgx/sgx.h | 2 ++ 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/arch/x86/kernel/cpu/sgx/main.c b/arch/x86/kernel/cpu/sgx/main.c index ecd7f8e704cc..bad72498b0a7 100644 --- a/arch/x86/kernel/cpu/sgx/main.c +++ b/arch/x86/kernel/cpu/sgx/main.c @@ -305,13 +305,15 @@ static void __sgx_reclaim_pages(void) encl_page = epc_page->encl_owner; - if (kref_get_unless_zero(&encl_page->encl->refcount) != 0) + if (kref_get_unless_zero(&encl_page->encl->refcount) != 0) { + epc_page->flags |= SGX_EPC_PAGE_RECLAIM_IN_PROGRESS; chunk[cnt++] = epc_page; - else + } else { /* The owner is freeing the page. No need to add the * page back to the list of reclaimable pages. */ epc_page->flags &= ~SGX_EPC_PAGE_RECLAIMER_TRACKED; + } } spin_unlock(&sgx_global_lru.lock); @@ -337,6 +339,7 @@ static void __sgx_reclaim_pages(void) skip: spin_lock(&sgx_global_lru.lock); + epc_page->flags &= ~SGX_EPC_PAGE_RECLAIM_IN_PROGRESS; sgx_epc_push_reclaimable(&sgx_global_lru, epc_page); spin_unlock(&sgx_global_lru.lock); @@ -360,7 +363,8 @@ static void __sgx_reclaim_pages(void) sgx_reclaimer_write(epc_page, &backing[i]); kref_put(&encl_page->encl->refcount, sgx_encl_release); - epc_page->flags &= ~SGX_EPC_PAGE_RECLAIMER_TRACKED; + epc_page->flags &= ~(SGX_EPC_PAGE_RECLAIMER_TRACKED | + SGX_EPC_PAGE_RECLAIM_IN_PROGRESS); sgx_free_epc_page(epc_page); } @@ -508,7 +512,8 @@ struct sgx_epc_page *__sgx_alloc_epc_page(void) void sgx_record_epc_page(struct sgx_epc_page *page, unsigned long flags) { spin_lock(&sgx_global_lru.lock); - WARN_ON(page->flags & SGX_EPC_PAGE_RECLAIMER_TRACKED); + WARN_ON(page->flags & (SGX_EPC_PAGE_RECLAIMER_TRACKED | + SGX_EPC_PAGE_RECLAIM_IN_PROGRESS)); page->flags |= flags; if (flags & SGX_EPC_PAGE_RECLAIMER_TRACKED) sgx_epc_push_reclaimable(&sgx_global_lru, page); @@ -532,7 +537,7 @@ int sgx_drop_epc_page(struct sgx_epc_page *page) spin_lock(&sgx_global_lru.lock); if (page->flags & SGX_EPC_PAGE_RECLAIMER_TRACKED) { /* The page is being reclaimed. */ - if (list_empty(&page->list)) { + if (page->flags & SGX_EPC_PAGE_RECLAIM_IN_PROGRESS) { spin_unlock(&sgx_global_lru.lock); return -EBUSY; } diff --git a/arch/x86/kernel/cpu/sgx/sgx.h b/arch/x86/kernel/cpu/sgx/sgx.h index ba4338b7303f..37d66bc6ca27 100644 --- a/arch/x86/kernel/cpu/sgx/sgx.h +++ b/arch/x86/kernel/cpu/sgx/sgx.h @@ -30,6 +30,8 @@ #define SGX_EPC_PAGE_IS_FREE BIT(1) /* Pages allocated for KVM guest */ #define SGX_EPC_PAGE_KVM_GUEST BIT(2) +/* page flag to indicate reclaim is in progress */ +#define SGX_EPC_PAGE_RECLAIM_IN_PROGRESS BIT(3) struct sgx_epc_page { unsigned int section; -- 2.38.1