From patchwork Wed Sep 13 04:06:30 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Haitao Huang X-Patchwork-Id: 139112 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:612c:a8d:b0:3f2:4152:657d with SMTP id gr13csp112443vqb; Wed, 13 Sep 2023 15:05:32 -0700 (PDT) X-Google-Smtp-Source: AGHT+IG8TjQ9TKAc+2yxoHUu39Nebv1lfyMjQkXtI+8GEXt5yPpDz6ZRoOyT0p1fl2Z3dg/mHLc+ X-Received: by 2002:a17:902:ea0a:b0:1c3:7628:fca8 with SMTP id s10-20020a170902ea0a00b001c37628fca8mr5047441plg.49.1694642732371; Wed, 13 Sep 2023 15:05:32 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1694642732; cv=none; d=google.com; s=arc-20160816; b=NJcPZvZse3o5nZa7ftTJWM0rBVYUFYgpVgbXkufM6nrVP1l7D73EI9odtvAdVvIcZD 2dCAEEawg+LBOrF3+rQ6cVTqwDTCrKBRs0ATAAsmojtCfBfhSH/8Eh3Hd9raQ8WwhNIu pZMDkULd0c8pvU9Q6EN/lVimbMnit7nP+grRDsdIAqCfZOFbEtHIIn8vmJX7Sqrka8hn n5bLaftgU4qiUaJ/1t5AxC5ziiNngGZ10Hh6kfuDOh3gkdt0TQczZTKgPFQDHIWgorm/ A5KaGaQXkIo41Zvp65FFRVP+BaDug3Jp0bBQ3j3M4Em8k6JnMP33cEJNF9HghAGm+Ish X8oA== 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=/Ee5YRWpuLiU1kReRKa4CQgvVcU220H1zskYBSC1Dzs=; fh=j8PE345l5Ydlo3KwK7JeWnjqRgjiq4AteUoOZeOwa0I=; b=tqjyeP075MvMm98NeqkZNKCK0+E8tU7LFzkSaTxna8ZQoAIQ2vKt1mvNpf6SMO8Qpz kcDZSAH0kYxMIsfmJs52CZmwOc+ryPbnPDnp6thM42BDLJr6vZ9vy1LhW8WbQIt9Hxnq sb6mfddU9F5OUjSA/UAKQKI/vEkZVOLhKohgP5QldVa3O6yeSwHQR3xtbtYaXNIXAGu3 nWbKoq/sLVlUjPcmI8oQYJl0wZxDU8Y2bDDqVVoS4Hl2EuAg63U2d3Grx6AlK6yPYFlE KPlLIfvgxnJGYoxz52g431yGkciqlSPXz0ulCSSpY7+PBmQ/+Ig0PgoctNFGjzHqDOws 5qUw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=UGw7Qlqk; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.34 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 howler.vger.email (howler.vger.email. [23.128.96.34]) by mx.google.com with ESMTPS id i13-20020a17090332cd00b001c38666f77dsi232431plr.596.2023.09.13.15.05.31 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 13 Sep 2023 15:05:32 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.34 as permitted sender) client-ip=23.128.96.34; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=UGw7Qlqk; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.34 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 howler.vger.email (Postfix) with ESMTP id C132582CBA5E; Tue, 12 Sep 2023 21:09:38 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at howler.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S238461AbjIMEJe (ORCPT + 36 others); Wed, 13 Sep 2023 00:09:34 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38962 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S238282AbjIMEJC (ORCPT ); Wed, 13 Sep 2023 00:09:02 -0400 Received: from mgamail.intel.com (mgamail.intel.com [192.55.52.136]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1DEAF1736; Tue, 12 Sep 2023 21:08:58 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1694578138; x=1726114138; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=qtSM03QbJLI7zt9hAzmFYzEy+EU72cnHWti1yqOHIcQ=; b=UGw7Qlqklhh+x0ywTYO8u42BLmOrzpQSOYZD3TMfy4RYD8VN4n3Umyua 6JmFWuu+fXgoK7gz5oBTxb4OOGiIeKmLJNQvMPdumbz8zBTkG8bjF3N0i xNeSOfzp2yyMEJ8C1zqSawOKL6eYBDIhARLG55YmyD6QJN0oOoFEKPI4g TX831V9HDXGsFAOWAPlRvOXb8/isDhZsjMHReRLBG/3DL+xFsa2xeeg0L j5M7JgqXqjVXlMiDTGu2beCiMq+myNlhuX+sSsQN3sfPXhwCs45Bgvakm jnKC06iZ0lblaE55O2KDYZLyWddAoPeWluUK2JgiWql8Cxasfqp2ef5e+ w==; X-IronPort-AV: E=McAfee;i="6600,9927,10831"; a="357990479" X-IronPort-AV: E=Sophos;i="6.02,142,1688454000"; d="scan'208";a="357990479" Received: from fmsmga007.fm.intel.com ([10.253.24.52]) by fmsmga106.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 12 Sep 2023 21:06:52 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10831"; a="747155978" X-IronPort-AV: E=Sophos;i="6.02,142,1688454000"; d="scan'208";a="747155978" Received: from b4969161e530.jf.intel.com ([10.165.56.46]) by fmsmga007.fm.intel.com with ESMTP; 12 Sep 2023 21:06:50 -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 v4 13/18] x86/sgx: Expose sgx_reclaim_pages() for use by EPC cgroup Date: Tue, 12 Sep 2023 21:06:30 -0700 Message-Id: <20230913040635.28815-14-haitao.huang@linux.intel.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20230913040635.28815-1-haitao.huang@linux.intel.com> References: <20230913040635.28815-1-haitao.huang@linux.intel.com> MIME-Version: 1.0 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 (howler.vger.email [0.0.0.0]); Tue, 12 Sep 2023 21:09:38 -0700 (PDT) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1776937666716831506 X-GMAIL-MSGID: 1776961697792389177 From: Sean Christopherson Adjust and expose the top-level reclaim function as sgx_reclaim_epc_pages() for use by the upcoming EPC cgroup, which will initiate reclaim to enforce the max limit. Make these adjustments to the function signature. 1) To take a parameter that specifies the number of pages to scan for reclaiming. Define a max value of 32, but scan 16 in the case for the global reclaimer (ksgxd). The EPC cgroup will use it to specify a desired number of pages to be reclaimed up to the max value of 32. 2) To take a flag to force reclaiming a page regardless of its age. The EPC cgroup will use the flag to enforce its limits by draining the reclaimable lists before resorting to other measures, e.g. forcefully kill enclaves. 3) Return the number of reclaimed pages. The EPC cgroup will use the result to track reclaiming progress and escalate to a more forceful reclaiming mode, e.g., calling this function with the flag to ignore age of pages. Signed-off-by: Sean Christopherson Signed-off-by: Kristen Carlson Accardi Signed-off-by: Haitao Huang Cc: Sean Christopherson --- V4: - Combined the 3 patches that made the individual changes to the function signature. - Removed 'high' limit in commit message. --- arch/x86/kernel/cpu/sgx/main.c | 30 ++++++++++++++++++++---------- arch/x86/kernel/cpu/sgx/sgx.h | 1 + 2 files changed, 21 insertions(+), 10 deletions(-) diff --git a/arch/x86/kernel/cpu/sgx/main.c b/arch/x86/kernel/cpu/sgx/main.c index c8900d62cfff..e1dde431a400 100644 --- a/arch/x86/kernel/cpu/sgx/main.c +++ b/arch/x86/kernel/cpu/sgx/main.c @@ -17,6 +17,10 @@ #include "driver.h" #include "encl.h" #include "encls.h" +/** + * Maximum number of pages to scan for reclaiming. + */ +#define SGX_NR_TO_SCAN_MAX 32 struct sgx_epc_section sgx_epc_sections[SGX_MAX_EPC_SECTIONS]; static int sgx_nr_epc_sections; @@ -279,7 +283,11 @@ static void sgx_reclaimer_write(struct sgx_epc_page *epc_page, mutex_unlock(&encl->lock); } -/* +/** + * sgx_reclaim_epc_pages() - Reclaim EPC pages from the consumers + * @nr_to_scan: Number of EPC pages to scan for reclaim + * @ignore_age: Reclaim a page even if it is young + * * Take a fixed number of pages from the head of the active page pool and * reclaim them to the enclave's private shmem files. Skip the pages, which have * been accessed since the last scan. Move those pages to the tail of active @@ -292,15 +300,15 @@ static void sgx_reclaimer_write(struct sgx_epc_page *epc_page, * problematic as it would increase the lock contention too much, which would * halt forward progress. */ -static void sgx_reclaim_pages(void) +size_t sgx_reclaim_epc_pages(size_t nr_to_scan, bool ignore_age) { - struct sgx_backing backing[SGX_NR_TO_SCAN]; + struct sgx_backing backing[SGX_NR_TO_SCAN_MAX]; struct sgx_epc_page *epc_page, *tmp; struct sgx_encl_page *encl_page; pgoff_t page_index; LIST_HEAD(iso); - int ret; - int i; + size_t ret; + size_t i; spin_lock(&sgx_global_lru.lock); for (i = 0; i < SGX_NR_TO_SCAN; i++) { @@ -326,13 +334,14 @@ static void sgx_reclaim_pages(void) spin_unlock(&sgx_global_lru.lock); if (list_empty(&iso)) - return; + return 0; i = 0; list_for_each_entry_safe(epc_page, tmp, &iso, list) { encl_page = epc_page->encl_page; - if (!sgx_reclaimer_age(epc_page)) + if (i == SGX_NR_TO_SCAN_MAX || + (!ignore_age && !sgx_reclaimer_age(epc_page))) goto skip; page_index = PFN_DOWN(encl_page->desc - encl_page->encl->base); @@ -371,6 +380,7 @@ static void sgx_reclaim_pages(void) sgx_free_epc_page(epc_page); } + return i; } static bool sgx_should_reclaim(unsigned long watermark) @@ -387,7 +397,7 @@ static bool sgx_should_reclaim(unsigned long watermark) void sgx_reclaim_direct(void) { if (sgx_should_reclaim(SGX_NR_LOW_PAGES)) - sgx_reclaim_pages(); + sgx_reclaim_epc_pages(SGX_NR_TO_SCAN, false); } static int ksgxd(void *p) @@ -410,7 +420,7 @@ static int ksgxd(void *p) sgx_should_reclaim(SGX_NR_HIGH_PAGES)); if (sgx_should_reclaim(SGX_NR_HIGH_PAGES)) - sgx_reclaim_pages(); + sgx_reclaim_epc_pages(SGX_NR_TO_SCAN, false); cond_resched(); } @@ -582,7 +592,7 @@ struct sgx_epc_page *sgx_alloc_epc_page(void *owner, bool reclaim) break; } - sgx_reclaim_pages(); + sgx_reclaim_epc_pages(SGX_NR_TO_SCAN, false); cond_resched(); } diff --git a/arch/x86/kernel/cpu/sgx/sgx.h b/arch/x86/kernel/cpu/sgx/sgx.h index 3818be5a8bd3..aa4ec2c0ce96 100644 --- a/arch/x86/kernel/cpu/sgx/sgx.h +++ b/arch/x86/kernel/cpu/sgx/sgx.h @@ -177,6 +177,7 @@ void sgx_record_epc_page(struct sgx_epc_page *page, unsigned long flags); 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_ipi_cb(void *info);