Received: by 2002:a05:7412:5112:b0:fa:6e18:a558 with SMTP id fm18csp45263rdb; Mon, 22 Jan 2024 11:21:52 -0800 (PST) X-Google-Smtp-Source: AGHT+IG645M95J0vTg+rt+q2ubn7Q6lWHIzceQxg/Ddw22V/e7oxBNeiDNEprZDvyVXLWOGs13bx X-Received: by 2002:ac2:418b:0:b0:50e:7b2c:9013 with SMTP id z11-20020ac2418b000000b0050e7b2c9013mr1912145lfh.132.1705951312217; Mon, 22 Jan 2024 11:21:52 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1705951312; cv=pass; d=google.com; s=arc-20160816; b=1KBNfNWBJ5BYaZ/UJPXiLA1SybDswk9fQ2aqR1m74JYmq3n2mlNMe6zySuclnOzmLv Ar9i+wbfTi3rsB3Na6ZfTv5RKiw8SM0qp23WYR93RdsgQjuphsB0mN1jIg5O/yNiSKP9 KyEuKFDLYuNh/Q9kcmf8qbG/4WLlgYZI4mC6eemjuFB47N3R4ErUlyGYSCWFIVHtYp0q 4j+jBENl/kpuCCb9NlkTT+RBMGuuw8db8UMqMLYUIGNsTWecu3pxy7YaYImiVVmmIpQ+ H2flcJZhmebJt6s588Eo8UwBeQf/JBFPQ8ntiJXvxK0tmdkV96nPbrZJ17eCHFgPhddJ Jq+g== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from:dkim-signature; bh=AnaLavBomV7JiNQ6WVb5NIWReee2zgTZBIB6WARqylo=; fh=Vgrzu9fr1cYGsDqSNq8PcOKm1OcJf2aYsOF+33ACqic=; b=xwacJwtApPvdee/oqwv0zrfm1uAXEbq3EBulrICHHDt84qmMo0xaJZA4UJRNpIR181 xemY00UE7N5Y5iXiiXkYsnN/9k2xvawyDe+0bp3anJZjQ+hlE/HfR9pEllx1tlSRXeWo i/mm/2T6Bb5oWPs9oBqmtt44Attzeb0C2NKvTcB2GuYyvd0t/I1uZkrKItFch3cezJ1F SOvKcIMaTpMvNICq72zQmcL3gwGA4rgsHNgXKIBid/gl77unwUOAgx5Dg0LNsSUqNHYd 7+yGnTsISIEivf10Nl/9c0E55eQqosbvwZPl6asxNeC7yC3+ucD37P3RiHyDQ1qPxQ3J jBdw== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=II14L2Zl; arc=pass (i=1 dkim=pass dkdomain=intel.com dmarc=pass fromdomain=linux.intel.com); spf=pass (google.com: domain of linux-kernel+bounces-33778-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-33778-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Return-Path: Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [2604:1380:4601:e00::3]) by mx.google.com with ESMTPS id m22-20020a1709062b9600b00a289311396fsi10619688ejg.244.2024.01.22.11.21.52 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 22 Jan 2024 11:21:52 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-33778-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) client-ip=2604:1380:4601:e00::3; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=II14L2Zl; arc=pass (i=1 dkim=pass dkdomain=intel.com dmarc=pass fromdomain=linux.intel.com); spf=pass (google.com: domain of linux-kernel+bounces-33778-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-33778-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by am.mirrors.kernel.org (Postfix) with ESMTPS id F2AA31F2A615 for ; Mon, 22 Jan 2024 17:53:26 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 3A73E5024A; Mon, 22 Jan 2024 17:20:56 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="II14L2Zl" Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.15]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 485D44F61B; Mon, 22 Jan 2024 17:20:53 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=198.175.65.15 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1705944054; cv=none; b=q3nEPw1eIQoxxze37MO7xsq3DcccHBQCkPwXv47hgkH2wxV/+63UznZeX7TqaJ6CshsmLnaLKjh/fc2omLJdI+Pqk2j7POkfxVwfVh4JdXiInQw0nrI62U+YnjlO/qG3FyYKwvGBVBPkZcQ+nl08ubUj1eZmdV5+cDc//cd8s1A= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1705944054; c=relaxed/simple; bh=CVMeTI0EEUIW4t1MiZMgp2IxrBR4Ca0Dr+0LB8bRvAI=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=Tj6gvS3kvykOI55yxA+FRGgQ5XoxziPQ2ZXkpRu9c17IKz+YFmK054enxQn7zLFKRrIwzlP9969BiNQF7vt5Q3e+K/fhiJCl7uDEk9yIJfJXbUTz1VUHLDMcAG8eYkHCN2POcvZvFlrDCZpe5vUSFyW8Nx0KDMcaSGVKrboNd5o= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.intel.com; spf=none smtp.mailfrom=linux.intel.com; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b=II14L2Zl; arc=none smtp.client-ip=198.175.65.15 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.intel.com Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=linux.intel.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1705944054; x=1737480054; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=CVMeTI0EEUIW4t1MiZMgp2IxrBR4Ca0Dr+0LB8bRvAI=; b=II14L2ZlmlVE39Trl8kXWnOxIw0nxwT8kDy/6DBZeoHBvv1gnxBZKqyw ZIx1puJWXmx39WsTcDjVw4PuaesGkgLeZKXansCtfNcydfhTpbr0F9vpS z00EYsTPxEVXwMhxxnc+BGJ493sXMQdG864HNn8W3yZ+7lnjFIj0FMnEN ayArzIjtJvcbKqGeV9nFwwWiuaGRwsEDYLNe+RFORAB2kqOpXtDyMh7N6 j3x/aEqKdtTO7uv5/XxAdiRpi7fdihr6EWNb+o/jjCA3dHrC69i69tOtP GIgJya/a9Tl+t68iK207453MChc01wquLIAiwKfyJ3WbP74KIC/gdvMRK A==; X-IronPort-AV: E=McAfee;i="6600,9927,10961"; a="1150147" X-IronPort-AV: E=Sophos;i="6.05,211,1701158400"; d="scan'208";a="1150147" Received: from orviesa005.jf.intel.com ([10.64.159.145]) by orvoesa107.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 22 Jan 2024 09:20:50 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.05,211,1701158400"; d="scan'208";a="1262872" Received: from b4969161e530.jf.intel.com ([10.165.56.46]) by orviesa005.jf.intel.com with ESMTP; 22 Jan 2024 09:20:49 -0800 From: Haitao Huang To: jarkko@kernel.org, dave.hansen@linux.intel.com, tj@kernel.org, mkoutny@suse.com, 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 v7 06/15] x86/sgx: Abstract tracking reclaimable pages in LRU Date: Mon, 22 Jan 2024 09:20:39 -0800 Message-Id: <20240122172048.11953-7-haitao.huang@linux.intel.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20240122172048.11953-1-haitao.huang@linux.intel.com> References: <20240122172048.11953-1-haitao.huang@linux.intel.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit From: Kristen Carlson Accardi The functions, sgx_{mark,unmark}_page_reclaimable(), manage the tracking of reclaimable EPC pages: sgx_mark_page_reclaimable() adds a newly allocated page into the global LRU list while sgx_unmark_page_reclaimable() does the opposite. Abstract the hard coded global LRU references in these functions to make them reusable when pages are tracked in per-cgroup LRUs. Create a helper, sgx_lru_list(), that returns the LRU that tracks a given EPC page. It simply returns the global LRU now, and will later return the LRU of the cgroup within which the EPC page was allocated. Replace the hard coded global LRU with a call to this helper. Next patches will first get the cgroup reclamation flow ready while keeping pages tracked in the global LRU and reclaimed by ksgxd before we make the switch in the end for sgx_lru_list() to return per-cgroup LRU. Co-developed-by: Sean Christopherson Signed-off-by: Sean Christopherson Signed-off-by: Kristen Carlson Accardi Co-developed-by: Haitao Huang Signed-off-by: Haitao Huang --- V7: - Split this out from the big patch, #10 in V6. (Dave, Kai) --- arch/x86/kernel/cpu/sgx/main.c | 27 +++++++++++++++++---------- 1 file changed, 17 insertions(+), 10 deletions(-) diff --git a/arch/x86/kernel/cpu/sgx/main.c b/arch/x86/kernel/cpu/sgx/main.c index 912959c7ecc9..cde750688e62 100644 --- a/arch/x86/kernel/cpu/sgx/main.c +++ b/arch/x86/kernel/cpu/sgx/main.c @@ -32,6 +32,11 @@ static DEFINE_XARRAY(sgx_epc_address_space); */ static struct sgx_epc_lru_list sgx_global_lru; +static inline struct sgx_epc_lru_list *sgx_lru_list(struct sgx_epc_page *epc_page) +{ + return &sgx_global_lru; +} + static atomic_long_t sgx_nr_free_pages = ATOMIC_LONG_INIT(0); /* Nodes with one or more EPC sections. */ @@ -500,25 +505,25 @@ struct sgx_epc_page *__sgx_alloc_epc_page(void) } /** - * sgx_mark_page_reclaimable() - Mark a page as reclaimable + * sgx_mark_page_reclaimable() - Mark a page as reclaimable and add it to an appropriate LRU * @page: EPC page * - * Mark a page as reclaimable and add it to the active page list. Pages - * are automatically removed from the active list when freed. */ void sgx_mark_page_reclaimable(struct sgx_epc_page *page) { - spin_lock(&sgx_global_lru.lock); + struct sgx_epc_lru_list *lru = sgx_lru_list(page); + + spin_lock(&lru->lock); page->flags |= SGX_EPC_PAGE_RECLAIMER_TRACKED; - list_add_tail(&page->list, &sgx_global_lru.reclaimable); - spin_unlock(&sgx_global_lru.lock); + list_add_tail(&page->list, &lru->reclaimable); + spin_unlock(&lru->lock); } /** * sgx_unmark_page_reclaimable() - Remove a page from the reclaim list * @page: EPC page * - * Clear the reclaimable flag and remove the page from the active page list. + * Clear the reclaimable flag if set and remove the page from its LRU. * * Return: * 0 on success, @@ -526,18 +531,20 @@ void sgx_mark_page_reclaimable(struct sgx_epc_page *page) */ int sgx_unmark_page_reclaimable(struct sgx_epc_page *page) { - spin_lock(&sgx_global_lru.lock); + struct sgx_epc_lru_list *lru = sgx_lru_list(page); + + spin_lock(&lru->lock); if (page->flags & SGX_EPC_PAGE_RECLAIMER_TRACKED) { /* The page is being reclaimed. */ if (list_empty(&page->list)) { - spin_unlock(&sgx_global_lru.lock); + spin_unlock(&lru->lock); return -EBUSY; } list_del(&page->list); page->flags &= ~SGX_EPC_PAGE_RECLAIMER_TRACKED; } - spin_unlock(&sgx_global_lru.lock); + spin_unlock(&lru->lock); return 0; } -- 2.25.1