From patchwork Sat Sep 23 03:06:53 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Haitao Huang X-Patchwork-Id: 143832 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:cae8:0:b0:403:3b70:6f57 with SMTP id r8csp24075vqu; Fri, 22 Sep 2023 23:57:53 -0700 (PDT) X-Google-Smtp-Source: AGHT+IFUYblJv+nvIrZxy+JH/n25ur/DFBIPzfl1ouWwK1ddqhazF4d9ULEUN7RV0t5zIK9UELpB X-Received: by 2002:a54:4584:0:b0:3ab:8e86:fc26 with SMTP id z4-20020a544584000000b003ab8e86fc26mr1954274oib.46.1695452272833; Fri, 22 Sep 2023 23:57:52 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1695452272; cv=none; d=google.com; s=arc-20160816; b=dreyyW33CwqsOf9akpwyR90hl8Il83FuNNminpOlWQC+qvCuIDRUQFytrQneexf0U1 /upB1Y1/4Wq2FDmiAD7WTwPQlq0eOwKRP4/KMxsibtjBgN8pJbNKskmgyZrOwq2tU4jS jW0IdWpjL9pzL8OaYRIixyZy+vB5WhqReecZROUHTmR8CEcYt4ZIuS7UCe1jBGCtPFrv NvzQfP7J7UPk4DNS4TDXQFz5xxALaQtJNyPkJ+qZYmxSe1u6OPmXPeG1sfrDluPGQ8Rx 0G6NdNdGBJ56yIl8yMayl2KL9iAxsCsyRDMIFTTwFAKONccokcrU3clat3hJYN86bn09 0K8w== 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=RkiEdAhxXKQf51QyoKC6QgcwXUuh+4/fP5lLrG3NO2I=; fh=j8PE345l5Ydlo3KwK7JeWnjqRgjiq4AteUoOZeOwa0I=; b=IGWq9kOLjY3GGDmOoFf6voIeEyICmR0/NZ9Yhd6gETKODAvuW7sTvgDDU+I/nf8gOU jxqtRj5FOfnD/Q+AfAm2FC1BjwSXpO2+YfLFy0UMzOEQjhqfhK6xsNS7CB6f2c05rMcN upz3NX4xQ/8spkTAcpG8CpSyTr/+Jp3JgrdKlXqHC7CkAz5J0jXqE4tCftRDge7gMhqF NgR5DERJDNpFKmHxA/YQ8bT0g/yFYRbsP8xDzMRrEd6FfQB8hOYKiswzKG9RI2gBcJ++ MP/VC0yVYHRaryVIsGZ1LcDWsV//HoirrzUvwe+EbgFI7F56CfpPlENDwulMBUsdriXZ Ambg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=i2cyNx7w; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:7 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 snail.vger.email (snail.vger.email. [2620:137:e000::3:7]) by mx.google.com with ESMTPS id ku4-20020a170903288400b001b8922e82e3si5103968plb.297.2023.09.22.23.57.52 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 22 Sep 2023 23:57:52 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:7 as permitted sender) client-ip=2620:137:e000::3:7; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=i2cyNx7w; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:7 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 (depot.vger.email [IPv6:2620:137:e000::3:0]) by snail.vger.email (Postfix) with ESMTP id A076080C5902; Fri, 22 Sep 2023 20:08:21 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at snail.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231190AbjIWDIO (ORCPT + 28 others); Fri, 22 Sep 2023 23:08:14 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58370 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230222AbjIWDH0 (ORCPT ); Fri, 22 Sep 2023 23:07:26 -0400 Received: from mgamail.intel.com (mgamail.intel.com [134.134.136.100]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 195F4CCB; Fri, 22 Sep 2023 20:07:10 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1695438430; x=1726974430; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=vokB5ebesnGW9bdWq3wdFib9nSqPQEUUYA5UVblvF4Y=; b=i2cyNx7w4OuNrPwnzw90hbwsWjpyzhoRumyUfjQzWf6LPm/c9W0GBFBD uSnyBJafNEoKYj+MPifWhivwoFkpIHnGCzW7QKtFEmK5PXFARsea7iwT0 iDX27Fa6m0O02RR5QEXTSuI3Z1czTgJq8Tp91388jAFl7I1QUawPkJipQ Zp3eAzM1DtIp/ftNVe/HBfvs5W/ng4sqDr+pTTNUMCjczHTWlSh5QxaKW joSxpRPaBRTEQ+BzXn+rUvdna5hi3NlK7QuzwKVnReQgd62F8WanH57S4 AXodoRFEE939X2/kWwbe/tMH4V8hjp+KOWpCkzr99R2irodh6cU98DTe6 w==; X-IronPort-AV: E=McAfee;i="6600,9927,10841"; a="447466834" X-IronPort-AV: E=Sophos;i="6.03,169,1694761200"; d="scan'208";a="447466834" Received: from fmsmga001.fm.intel.com ([10.253.24.23]) by orsmga105.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 22 Sep 2023 20:07:08 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10841"; a="891048576" X-IronPort-AV: E=Sophos;i="6.03,169,1694761200"; d="scan'208";a="891048576" Received: from b4969161e530.jf.intel.com ([10.165.56.46]) by fmsmga001.fm.intel.com with ESMTP; 22 Sep 2023 20:06:12 -0700 From: Haitao Huang To: jarkko@kernel.org, dave.hansen@linux.intel.com, tj@kernel.org, linux-kernel@vger.kernel.org, linux-sgx@vger.kernel.org, x86@kernel.org, cgroups@vger.kernel.org, tglx@linutronix.de, mingo@redhat.com, bp@alien8.de, hpa@zytor.com, sohil.mehta@intel.com Cc: zhiquan1.li@intel.com, kristen@linux.intel.com, seanjc@google.com, zhanb@microsoft.com, anakrish@microsoft.com, mikko.ylinen@linux.intel.com, yangjie@microsoft.com Subject: [PATCH v5 14/18] x86/sgx: Add helper to grab pages from an arbitrary EPC LRU Date: Fri, 22 Sep 2023 20:06:53 -0700 Message-Id: <20230923030657.16148-15-haitao.huang@linux.intel.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20230923030657.16148-1-haitao.huang@linux.intel.com> References: <20230923030657.16148-1-haitao.huang@linux.intel.com> MIME-Version: 1.0 X-Spam-Status: No, score=-2.0 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_EF,RCVD_IN_DNSWL_BLOCKED, 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-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (snail.vger.email [0.0.0.0]); Fri, 22 Sep 2023 20:08:21 -0700 (PDT) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1777810562291320608 X-GMAIL-MSGID: 1777810562291320608 From: Sean Christopherson Move the isolation loop into a helper, sgx_isolate_pages(), in preparation for existence of multiple LRUs. Expose the helper to other SGX code so that it can be called from the EPC cgroup code, e.g., to isolate pages from a single cgroup LRU. Exposing the isolation loop allows the cgroup iteration logic to be wholly encapsulated within the cgroup code. Signed-off-by: Sean Christopherson Co-developed-by: Kristen Carlson Accardi Signed-off-by: Kristen Carlson Accardi Co-developed-by: Haitao Huang Signed-off-by: Haitao Huang Cc: Sean Christopherson --- V4: - No changes other than reordering the patches --- arch/x86/kernel/cpu/sgx/main.c | 57 +++++++++++++++++++++------------- arch/x86/kernel/cpu/sgx/sgx.h | 2 ++ 2 files changed, 37 insertions(+), 22 deletions(-) diff --git a/arch/x86/kernel/cpu/sgx/main.c b/arch/x86/kernel/cpu/sgx/main.c index 4e1a3e038db5..b34ad3574c81 100644 --- a/arch/x86/kernel/cpu/sgx/main.c +++ b/arch/x86/kernel/cpu/sgx/main.c @@ -284,6 +284,40 @@ static void sgx_reclaimer_write(struct sgx_epc_page *epc_page, mutex_unlock(&encl->lock); } +/** + * sgx_isolate_epc_pages() - Isolate pages from an LRU for reclaim + * @lru: LRU from which to reclaim + * @nr_to_scan: Number of pages to scan for reclaim + * @dst: Destination list to hold the isolated pages + */ +void sgx_isolate_epc_pages(struct sgx_epc_lru_lists *lru, size_t nr_to_scan, + struct list_head *dst) +{ + struct sgx_encl_page *encl_page; + struct sgx_epc_page *epc_page; + + spin_lock(&lru->lock); + for (; nr_to_scan > 0; --nr_to_scan) { + epc_page = list_first_entry_or_null(&lru->reclaimable, struct sgx_epc_page, list); + if (!epc_page) + break; + + encl_page = epc_page->encl_page; + + if (kref_get_unless_zero(&encl_page->encl->refcount)) { + sgx_epc_page_set_state(epc_page, SGX_EPC_PAGE_RECLAIM_IN_PROGRESS); + list_move_tail(&epc_page->list, dst); + } else { + /* The owner is freeing the page, remove it from the + * LRU list + */ + sgx_epc_page_reset_state(epc_page); + list_del_init(&epc_page->list); + } + } + spin_unlock(&lru->lock); +} + /** * sgx_reclaim_epc_pages() - Reclaim EPC pages from the consumers * @nr_to_scan: Number of EPC pages to scan for reclaim @@ -310,28 +344,7 @@ size_t sgx_reclaim_epc_pages(size_t nr_to_scan, bool ignore_age) LIST_HEAD(iso); size_t ret, i; - spin_lock(&sgx_global_lru.lock); - for (i = 0; i < SGX_NR_TO_SCAN; i++) { - epc_page = list_first_entry_or_null(&sgx_global_lru.reclaimable, - struct sgx_epc_page, list); - if (!epc_page) - break; - - list_del_init(&epc_page->list); - encl_page = epc_page->encl_page; - - if (kref_get_unless_zero(&encl_page->encl->refcount) != 0) { - sgx_epc_page_set_state(epc_page, SGX_EPC_PAGE_RECLAIM_IN_PROGRESS); - list_move_tail(&epc_page->list, &iso); - } else { - /* The owner is freeing the page, remove it from the - * LRU list - */ - sgx_epc_page_reset_state(epc_page); - list_del_init(&epc_page->list); - } - } - spin_unlock(&sgx_global_lru.lock); + sgx_isolate_epc_pages(&sgx_global_lru, nr_to_scan, &iso); if (list_empty(&iso)) return 0; diff --git a/arch/x86/kernel/cpu/sgx/sgx.h b/arch/x86/kernel/cpu/sgx/sgx.h index 7e7f1f36d31e..42075762084c 100644 --- a/arch/x86/kernel/cpu/sgx/sgx.h +++ b/arch/x86/kernel/cpu/sgx/sgx.h @@ -180,6 +180,8 @@ int sgx_drop_epc_page(struct sgx_epc_page *page); struct sgx_epc_page *sgx_alloc_epc_page(void *owner, bool reclaim); bool sgx_epc_oom(struct sgx_epc_lru_lists *lrus); size_t sgx_reclaim_epc_pages(size_t nr_to_scan, bool ignore_age); +void sgx_isolate_epc_pages(struct sgx_epc_lru_lists *lrus, size_t nr_to_scan, + struct list_head *dst); void sgx_ipi_cb(void *info);