From patchwork Mon Jan 22 17:20:39 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Haitao Huang X-Patchwork-Id: 190395 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7301:2bc4:b0:101:a8e8:374 with SMTP id hx4csp2825850dyb; Mon, 22 Jan 2024 12:36:48 -0800 (PST) X-Google-Smtp-Source: AGHT+IHCj/3ybJeFleB58IXQx32QCn7gDhV9ps/n9ra0ZvLlMcwqyD32vDSBtgJ0d8rho7HJMGi4 X-Received: by 2002:a17:903:284:b0:1d7:4e9a:b425 with SMTP id j4-20020a170903028400b001d74e9ab425mr2651266plr.57.1705955808481; Mon, 22 Jan 2024 12:36:48 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1705955808; cv=pass; d=google.com; s=arc-20160816; b=n7td8LLezD6n9Ga3sFn1eyx9F/Gf9N5iwp5yr7ZCvgXrZciLWUXycC/Tu3reC1xpMJ Zi6ssgPgYF9iQEzdApz5kqCGsrq1BxfnMAVLsWRgc5KknNFzbewlLs1PXkbYfERtAhhj huAsrg7nnbXZ3Gq+1bNZyRjXxnbBfjy4J7l6UxqvP8AeEZ9kgcd+xqPbHeHgcYTdqJvX yqqgmb2xbQV6Hf6LosCvwYEnGXrFXMqBBuSnGKSkUwHB+77MJo9YHpjQMkRYnxMv3k7U 9Bt/Bz9+ca4iA/D5s1KaN1DIfZihrNJZEfRsV+f0c7ggN/PQb9S/cg2hbXmx32H0LAOO sXNA== 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-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-33778-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [2604:1380:45e3:2400::1]) by mx.google.com with ESMTPS id p14-20020a170902e74e00b001d6ee5c42bdsi8976165plf.54.2024.01.22.12.36.48 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 22 Jan 2024 12:36:48 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-33778-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) client-ip=2604:1380:45e3:2400::1; 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-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-33778-ouuuleilei=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 sv.mirrors.kernel.org (Postfix) with ESMTPS id 7624128C932 for ; Mon, 22 Jan 2024 17:54:13 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 69F9D5FDD5; Mon, 22 Jan 2024 17:20:58 +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 X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1788824317744042243 X-GMAIL-MSGID: 1788824317744042243 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; }