Received: by 2002:ab2:b82:0:b0:1f3:401:3cfb with SMTP id 2csp120471lqh; Wed, 27 Mar 2024 17:24:38 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCX92kt169WJwqRTEVwQmbIBRHLKFekiSybQkrEjLT51QcWGRZVV12PmsZkTqXn67zuvyLIueJTRGsHyh4ef124wQK2vYpEr3OPnolgZLQ== X-Google-Smtp-Source: AGHT+IGryWQDN3HJBz+OtgW7yrNuP4ZS4R3tyo9LH8c6gTOdBu3w34IwglXFWGlGqFQisznlHvqs X-Received: by 2002:a05:6214:519b:b0:691:57c4:b918 with SMTP id kl27-20020a056214519b00b0069157c4b918mr1345653qvb.37.1711585478639; Wed, 27 Mar 2024 17:24:38 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1711585478; cv=pass; d=google.com; s=arc-20160816; b=LpVrGYpZ2yTJ8hxSowcZ958jwPwwz25aJ4NOAHck6lK2LJmffP3nTjbKgnzg2EtaKg fK7htCNhw7IO4yqnTpxgy5L2/GLt+OXyFbNMuupz95fuw9TMcaM68SHHhZz73tyQ3T4m hu7mwQ0W+aug8PZ10kSNrcKZZTMRnmEebwPouyqopzw5PkuxnHuE17mFpJNX2yGH07gt Zx6fxTTapQCPZKARRms3ihTHYEWGcDrOHfcC9AsXjCuwepIuzn/guuIB3B0mROyq1k4F 3wHo+vXzYK4SMVmpbWAXRraUFagUPVliZIy5VhO4cpHv0LAACyj/kBw+UbIFekNUE9Wm omYg== 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=bqpMcvlCcDcbLQskhovRX9He8Enr/FSJ7560iMkP+30=; fh=G1r0sJ0F/0v82zumykPUwC7a5Du5vVkWNEWao5ifD3o=; b=uFolcLC6LgAjjXp81a+zSv4fs4rvPh15Nv3qkTK15L+exIFgBj9Mq5H06vgLWuplvC SWA4B3YEx27v5d5G12THgyRF/xQLjpheJsibc0AQi1TQo3PRR1RD2+B93Xz6vVFb2Muy sPLkVhPeE/zGvmaRXjRs+2NJiqnK6EV8jniigTLRCH6WgiXfSB1sB+UtGSheln5zvcE9 l/LoZ2CV2pdI7wEx4SKtH+UrNqP4fiNmzU3hFWgDs6XiNuuV7jvWegLm77KOR5pYamWx 5KuWxasS84VWe7PT/ZOJTTa7QKzc1tGkWBJg/knWFRPRklEDj0p9b58nlcxw5hDM1Wnd betw==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b="n5/UmN1f"; arc=pass (i=1 dkim=pass dkdomain=intel.com dmarc=pass fromdomain=linux.intel.com); spf=pass (google.com: domain of linux-kernel+bounces-122221-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-122221-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [2604:1380:45d1:ec00::1]) by mx.google.com with ESMTPS id w13-20020a0cef8d000000b0069673f5e527si291608qvr.557.2024.03.27.17.24.38 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 27 Mar 2024 17:24:38 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-122221-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) client-ip=2604:1380:45d1:ec00::1; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b="n5/UmN1f"; arc=pass (i=1 dkim=pass dkdomain=intel.com dmarc=pass fromdomain=linux.intel.com); spf=pass (google.com: domain of linux-kernel+bounces-122221-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-122221-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 ny.mirrors.kernel.org (Postfix) with ESMTPS id 608511C2D500 for ; Thu, 28 Mar 2024 00:24:38 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 286952C856; Thu, 28 Mar 2024 00:22:52 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="n5/UmN1f" Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.19]) (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 3FDDD208D4; Thu, 28 Mar 2024 00:22:47 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=192.198.163.19 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711585369; cv=none; b=MM+H/CLtFldmf85Xpu2FCGz8er8cRqjy4wdo1bXUS3Qz5Xl8XNgPwBBwNd1gu0Fj2SwMAvH3Ifmm8pA1CzeXBNB7hXA3k6bXM68Iuh6TdS14wRSd28IisuhcQTVs2zg3TVZ9QdOxnAhjYmRGfaPmm47zfMF+x+zGjoD8ORVFUJU= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711585369; c=relaxed/simple; bh=9AfNXNzb9gkZWObDUorhxnUuNXJUJymdxcB0irgcYK4=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=TnlBeIk6uFFYIwEtVMiGsoksWSFJpBn0PGGpq9lnLOWm2YAb9Y2CvgX4XK3UwPtszgOJzS6DAk4SRLWCa16tvCXHMDG05XrdUO5dmNYfogJiCvOj+bC4q61KIgHRK7rkte4BhdL9hrZ2Yt2ZZJy5nGwv7cjAJlqjhgvUVIsvuCs= 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=n5/UmN1f; arc=none smtp.client-ip=192.198.163.19 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=1711585367; x=1743121367; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=9AfNXNzb9gkZWObDUorhxnUuNXJUJymdxcB0irgcYK4=; b=n5/UmN1fBVADVGujoF/VBk4EelmR3qjVdmkdQVxnrkoPMtKISiFJJ73A zPncWHX/K2jJYXzYs+SKs0UZXJe1FwWDzPiO7p5gWt+q+4cG9stj/neiT COVoaLJ4PNoxt3gmcRZKvHqK0USZKyxv2JvPRSkRdrHQL0V5mXbW3NNDr XK29Ms67H5b0LkYl2o+2JbLSjyMKZukboc98XgFgdJPVuASGxgke4/bcE JZ8X+hXynsLhsr9vrWXsMj3wRLv+ys1F2F+BkGrBjbInLjWLLFNv2N4xE 5FNkcH/1QbLte5oHvn96gtT8geQzqpmQcO9B1SXuTOf6OqF2+gGe1DsZt Q==; X-CSE-ConnectionGUID: /P9b/X7tRay3SWEwyDSHjQ== X-CSE-MsgGUID: SUkpeHr0QKCuGfLR+fdvEg== X-IronPort-AV: E=McAfee;i="6600,9927,11026"; a="6580692" X-IronPort-AV: E=Sophos;i="6.07,160,1708416000"; d="scan'208";a="6580692" Received: from orviesa009.jf.intel.com ([10.64.159.149]) by fmvoesa113.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 27 Mar 2024 17:22:31 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.07,160,1708416000"; d="scan'208";a="16411800" Received: from b4969161e530.jf.intel.com ([10.165.56.46]) by orviesa009.jf.intel.com with ESMTP; 27 Mar 2024 17:22:31 -0700 From: Haitao Huang To: jarkko@kernel.org, dave.hansen@linux.intel.com, kai.huang@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, tim.c.chen@linux.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, chrisyan@microsoft.com Subject: [PATCH v10 07/14] x86/sgx: Abstract tracking reclaimable pages in LRU Date: Wed, 27 Mar 2024 17:22:22 -0700 Message-Id: <20240328002229.30264-8-haitao.huang@linux.intel.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20240328002229.30264-1-haitao.huang@linux.intel.com> References: <20240328002229.30264-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 Reviewed-by: Jarkko Sakkinen --- V7: - Split this out from the big patch, #10 in V6. (Dave, Kai) --- arch/x86/kernel/cpu/sgx/main.c | 30 ++++++++++++++++++------------ 1 file changed, 18 insertions(+), 12 deletions(-) diff --git a/arch/x86/kernel/cpu/sgx/main.c b/arch/x86/kernel/cpu/sgx/main.c index 4991eb0af748..8f83f7ac386e 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,24 @@ static 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 track it in a 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 + * sgx_unmark_page_reclaimable() - Remove a page from its tracking LRU * @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 +530,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