From patchwork Fri Nov 11 18:35:11 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: 18948 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:6687:0:0:0:0:0 with SMTP id l7csp905118wru; Fri, 11 Nov 2022 10:37:24 -0800 (PST) X-Google-Smtp-Source: AA0mqf7NlMq4C8Lp9U+mkbCFfKgHLmcFXzWfhcsdAMWosctlyOCFk+bOOfcqKooVKYt7zb745MOB X-Received: by 2002:a17:902:700a:b0:188:4f86:e4ea with SMTP id y10-20020a170902700a00b001884f86e4eamr3784654plk.59.1668191843856; Fri, 11 Nov 2022 10:37:23 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1668191843; cv=none; d=google.com; s=arc-20160816; b=EZOQ/NtcApGV/PDkFx+MZK9Tliir89KSPGjKA2gMU0L2jqIKhdMDETj85aLyRo4Q9f mmelMLzIQWZxMHvno4a7X/wfMlDUT3KWlzYLd0EufxNDehCxC/6AuZZLQ8lJzOoa54rT rsYnOH83hMp01C7pfmgPPeJkEayq4rmFiSbGaEeFH3kcodtw7PphpQWlsbndJr0K/Pla aMax3eSAlxSWPH/a0AgMLbqeRBsdVF/v3blYVv+uYAdazOWZC8us3+U68tCyGxyp+cQz w0BG37MLm5XlngTPOOBAdu9SVoKRxhxaITEBQhKqe/7/pzvYKWcH0mLGP/AkVVSGWWzo 3Hvg== 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=GN+d7eZw4XiHq2refBh44IzPqCchVeshDopz1IF+4Tk=; b=ArrlvccIyEJ/PCujAZWON2CCcVMEKblwb41uvH57kxBAwIGB2/CekD40H/qpifka3i D3Dh7CgB1/w7V9s1lIi5kGx3JUU0wk4J1Q5VJqvSC+niVFVeULORQ+V4UBGhnOJDAuRz sMORoPy5RpRayPdL9azysvKjTP8WAbqNfZsKvtbdqcRZmKTVDPmIUmzFygw6cNBkUaQY TQcQrtkdpOLevB07NUtiMczd6JJ+xO9Q9lIR4FgANh+58XXqC1RKpfLPWCgJ13ReWI/F 2POU9SVSKmW1wO6xEuHSvdEN6Tk09SVeId3Nu7szN0n5yIDio41HGqrteZCgDEIg0fP8 FQCQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=BJInnXeB; 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 k4-20020a17090a39c400b001fd9be4fb6csi6947494pjf.39.2022.11.11.10.37.10; Fri, 11 Nov 2022 10:37:23 -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=BJInnXeB; 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 S234427AbiKKSgl (ORCPT + 99 others); Fri, 11 Nov 2022 13:36:41 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37292 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234042AbiKKSgA (ORCPT ); Fri, 11 Nov 2022 13:36:00 -0500 Received: from mga17.intel.com (mga17.intel.com [192.55.52.151]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E5EDC76FA8; Fri, 11 Nov 2022 10:35:58 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1668191758; x=1699727758; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=CrS+kKaSo6YqmfqiKR19e0LuugWCU+rOlWI2gpswICU=; b=BJInnXeBTVEjceo2PbkEAXbpQ/E04cHV9zHi83yhei+RNq0ajrfXyLMW CNYau8sVqniBHgL3KZ80ifJ6xsJSOs1cI2ldvMJDsq8WQHd+b2noFvZGy E4IgVqF6DxGCjFPh/P2/LpwnR1UjU1hKRT+9lP1mV84NcCtQqbENxRhAf 3/3N+Ek9IoxK50wsGp2SKEVis1XtBL+uznkVjSiIHCyRazYnhlPBk9T3I OI5n/XYjdNweR+qSDYh/GD6EgkPfnvDk5B9byN30//LXJ3//VphwOlp7V ud1sBgRnHZ8TfeqHWwQBxr4sg862Ycx0jj0GXF6sDVhbwGFIAgMdm+v/G Q==; X-IronPort-AV: E=McAfee;i="6500,9779,10528"; a="292050322" X-IronPort-AV: E=Sophos;i="5.96,157,1665471600"; d="scan'208";a="292050322" Received: from fmsmga007.fm.intel.com ([10.253.24.52]) by fmsmga107.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 11 Nov 2022 10:35:51 -0800 X-IronPort-AV: E=McAfee;i="6500,9779,10528"; a="640089207" X-IronPort-AV: E=Sophos;i="5.96,157,1665471600"; d="scan'208";a="640089207" Received: from hermesli-mobl.amr.corp.intel.com (HELO kcaccard-desk.amr.corp.intel.com) ([10.212.218.5]) by fmsmga007-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 11 Nov 2022 10:35:49 -0800 From: Kristen Carlson Accardi To: jarkko@kernel.org, dave.hansen@linux.kernel.org, tj@kernel.org, linux-kernel@vger.kernel.org, linux-sgx@vger.kernel.org, cgroups@vger.kernel.org, Dave Hansen , Thomas Gleixner , Ingo Molnar , Borislav Petkov , x86@kernel.org, "H. Peter Anvin" Cc: zhiquan1.li@intel.com, Kristen Carlson Accardi , Sean Christopherson Subject: [PATCH 06/26] x86/sgx: Introduce RECLAIM_IN_PROGRESS flag for EPC pages Date: Fri, 11 Nov 2022 10:35:11 -0800 Message-Id: <20221111183532.3676646-7-kristen@linux.intel.com> X-Mailer: git-send-email 2.37.3 In-Reply-To: <20221111183532.3676646-1-kristen@linux.intel.com> References: <20221111183532.3676646-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?1749225931317570346?= X-GMAIL-MSGID: =?utf-8?q?1749225931317570346?= From: Sean Christopherson Keep track of whether the EPC page is in the middle of being reclaimed and do not delete the page off the it's LRU 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 | 14 +++++++++----- arch/x86/kernel/cpu/sgx/sgx.h | 4 ++++ 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/arch/x86/kernel/cpu/sgx/main.c b/arch/x86/kernel/cpu/sgx/main.c index 3b09433ffd85..8c451071fa91 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,7 @@ 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_RECLAIM_FLAGS); page->flags |= flags; if (flags & SGX_EPC_PAGE_RECLAIMER_TRACKED) sgx_epc_push_reclaimable(&sgx_global_lru, page); @@ -532,7 +536,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 969606615211..04ca644928a8 100644 --- a/arch/x86/kernel/cpu/sgx/sgx.h +++ b/arch/x86/kernel/cpu/sgx/sgx.h @@ -30,6 +30,10 @@ #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) +#define SGX_EPC_PAGE_RECLAIM_FLAGS (SGX_EPC_PAGE_RECLAIMER_TRACKED | \ + SGX_EPC_PAGE_RECLAIM_IN_PROGRESS) struct sgx_epc_page { unsigned int section;