From patchwork Fri Mar 29 05:33:52 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Ho-Ren (Jack) Chuang" X-Patchwork-Id: 13610117 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 6A789C6FD1F for ; Fri, 29 Mar 2024 05:34:06 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 50DFD6B0095; Fri, 29 Mar 2024 01:34:05 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 46EAD6B0096; Fri, 29 Mar 2024 01:34:05 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 30EB86B0098; Fri, 29 Mar 2024 01:34:05 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id 160BB6B0095 for ; Fri, 29 Mar 2024 01:34:05 -0400 (EDT) Received: from smtpin21.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id 7A61B1211D7 for ; Fri, 29 Mar 2024 05:34:04 +0000 (UTC) X-FDA: 81948960408.21.61F65FE Received: from mail-qt1-f178.google.com (mail-qt1-f178.google.com [209.85.160.178]) by imf05.hostedemail.com (Postfix) with ESMTP id A3C97100008 for ; Fri, 29 Mar 2024 05:34:02 +0000 (UTC) Authentication-Results: imf05.hostedemail.com; dkim=pass header.d=bytedance.com header.s=google header.b="W/DF+CKD"; dmarc=pass (policy=quarantine) header.from=bytedance.com; spf=pass (imf05.hostedemail.com: domain of horenchuang@bytedance.com designates 209.85.160.178 as permitted sender) smtp.mailfrom=horenchuang@bytedance.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1711690442; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=nM2K0D/X61aNkdsdtPeKZ1y9TeOUsh6nA3RvZQfcaBY=; b=cwuJ7rBEdbjfdsj6X3oeVE6+xMZHpgnSgTY8Xgp8Vd2F/0IldDEaQOTcYr+qiiKwEzYDkL axtYJ/xzZM3drPUzz5b8sHI8MpXeukR7yqix8rU3XcNcNVUvz7ruolhRcjeQRtjKw2+ae5 W+Hk0jQdAmfikcklwAy3kzqYPs8lJkA= ARC-Authentication-Results: i=1; imf05.hostedemail.com; dkim=pass header.d=bytedance.com header.s=google header.b="W/DF+CKD"; dmarc=pass (policy=quarantine) header.from=bytedance.com; spf=pass (imf05.hostedemail.com: domain of horenchuang@bytedance.com designates 209.85.160.178 as permitted sender) smtp.mailfrom=horenchuang@bytedance.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1711690442; a=rsa-sha256; cv=none; b=Xmgic3eCEawPaUEcTt7vLUtOoTPJ6A/MI3BMQ8yaVyj/4rgnSYA4b5jU+1W+iI/OR6jj9+ qCksPSQNOu5IRe7yusdIn0iPra2EpOHlMr1VIrfeCpcypcUtGcWvjJ0J0f1bVIkLQsRlme SolP9VgE5pzBy8dkbpWUexI+JORxMnM= Received: by mail-qt1-f178.google.com with SMTP id d75a77b69052e-430c45ab240so10249011cf.0 for ; Thu, 28 Mar 2024 22:34:02 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance.com; s=google; t=1711690442; x=1712295242; darn=kvack.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=nM2K0D/X61aNkdsdtPeKZ1y9TeOUsh6nA3RvZQfcaBY=; b=W/DF+CKDrc/fcl8s4R4OZq9U1A1bHmjrA5s6TiG6mF/qvI9NZXb+etzwXaz0M3NKAT 8KYtkWZaUH7wpC5ZLJfNocntCnq6FY8MgE5qJt+w9aHeT+DXwwKObFtOyogqkjf8srOf J1Y5yv598YiQ1f2DmDP2ZfdpcTAqML4ZOr6t4bSXEApJeyG4+renYYAXUr6mCxz7EzQX MQn8+RrotANW8EpK05TSe5ya9M7vMNgPtE6z64iC+gj2jbKRug6MOjUZbNVIl/CzNtky LuPCFtg/ngQuIIWcIK8Olj9rEs5A3C7ArR9Tk10Y8asFLCJI+ZqUOJUmeY+cEKBhIHG/ 2TwQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1711690442; x=1712295242; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=nM2K0D/X61aNkdsdtPeKZ1y9TeOUsh6nA3RvZQfcaBY=; b=hTr6fcWwvdrQ89yCN4NYFnpph2Rb7zLCbxjup7gR7sY6MSntntu6kfoJQyLgwiPOt0 Q3YvBxpSRpj4cUvd26q9X2d2VrdV1Su33ruEmb3Eq46xzQSUgx5nGRQAJ7pUzpwIXeZC xfWuqFNPDdRrZZ809tjo0wF8Uj9EQzVBiVwTSyOSeH1Hh2B1gDr0XY6hFoBijWvxPXXa g8Kzwb9S5FLlUJRvSxcATEMENHie/EEYyDbxyIYPrZKGjJ5y0a7s5cKtxJWokuyYboJz 5N6+gRdE9Y+oVVnNBQvIeuYQWczpjj1Tsj3gcu3a5noN6siN6AY6KYKUYMkVFXfhAfEi RuZw== X-Forwarded-Encrypted: i=1; AJvYcCWM1Bj6sY49ljIMY68y9qSBce4slGAPoNvPlLp5U5ZMs+Qe5eFPfnRPnaRPOqzPVhiue64doJQGZ84RIkz8QNLRik8= X-Gm-Message-State: AOJu0YxPYigi3xUWz7etWx1z+jut02ElEGKTyGh+qLYY80YHlZjZR41Z cROnBr5DWya0hYhSy2WeOS9EkNGaqp5SRFYUW0WEsxCi6dRpE5/QZ3nhJAcHr1g= X-Google-Smtp-Source: AGHT+IFNoqNydjaxWSpQbLOw9NSrhWxbMGVfkX5oUHJM8lI3o7LuokzE9FNy1hUFSqYBfeSHSyDMdw== X-Received: by 2002:a05:622a:10:b0:431:756b:c48f with SMTP id x16-20020a05622a001000b00431756bc48fmr1092286qtw.27.1711690441708; Thu, 28 Mar 2024 22:34:01 -0700 (PDT) Received: from n231-228-171.byted.org ([147.160.184.85]) by smtp.gmail.com with ESMTPSA id jd25-20020a05622a719900b00430bf59ebccsm1293700qtb.11.2024.03.28.22.34.01 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 28 Mar 2024 22:34:01 -0700 (PDT) From: "Ho-Ren (Jack) Chuang" To: "Huang, Ying" , "Gregory Price" , aneesh.kumar@linux.ibm.com, mhocko@suse.com, tj@kernel.org, john@jagalactic.com, "Eishan Mirakhur" , "Vinicius Tavares Petrucci" , "Ravis OpenSrc" , "Alistair Popple" , "Srinivasulu Thanneeru" , Dan Williams , Vishal Verma , Dave Jiang , Andrew Morton , nvdimm@lists.linux.dev, linux-cxl@vger.kernel.org, linux-kernel@vger.kernel.org, linux-mm@kvack.org Cc: "Ho-Ren (Jack) Chuang" , "Ho-Ren (Jack) Chuang" , "Ho-Ren (Jack) Chuang" , qemu-devel@nongnu.org Subject: [PATCH v9 1/2] memory tier: dax/kmem: introduce an abstract layer for finding, allocating, and putting memory types Date: Fri, 29 Mar 2024 05:33:52 +0000 Message-Id: <20240329053353.309557-2-horenchuang@bytedance.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20240329053353.309557-1-horenchuang@bytedance.com> References: <20240329053353.309557-1-horenchuang@bytedance.com> MIME-Version: 1.0 X-Rspamd-Queue-Id: A3C97100008 X-Rspam-User: X-Rspamd-Server: rspam05 X-Stat-Signature: d4ngqsy3epyf6k8d5a8a88z4osta5jfc X-HE-Tag: 1711690442-230935 X-HE-Meta: U2FsdGVkX19uYmriLFR/BlEFpaaXp61CO+3uxbJM1QE1A80gFU5twtmTb2fa0A28f2x4IAAcWnzOqTijpBQtUZgQzmlS22mW72Svjq5kFUTnnXSZ2tEvTMfNTrpng6R6C6Ttd8V9nbBEzNSu3KAF/m8A+qZ9Uz5Rj/fQRTr46fWGYsY+UOgRSA5t10BJzxRBRLhp4TnePi4mBhVG0o8rYLCUJfQL7vswFLXOjVu4xfgc5w7aogkzbGtrBCl7klrpTVe0xARWW/+F0lxDxJzszo94LZtiP13aMBipwbxXEDH7/73Br8Pp7nzUCSYDEIk+3v9abtro5oC2QbEIiYVlvttd77cRlLziKblXLe1yH4F7G+m+beZXyeqqy7lZKRNQ8mUuD7Hm+E7bBquI8dKCYRZDEHyXeHLFZqugWE+2w+0NUMXDqSYoMzUVp1VEO3EaybakM9FZ9NRqtrUewv2CCof1DfeELAWeRR0eMGrArm0IYKVz3QN1DxAnguKDz3OjvvDxPXeLijwWF9G9gFa9VBidysgsfuLF56U8uXp+aBsto1fzwCTInOYhNi33PUFoSjAIPEcGZyOtQ0STJe+WM263kxk0S9V5f1an+0Sh5i2VOvY//ViRe0Re8bpypd2jeiL5vRhPWWSF9kAT/KiR1YuGSqj3qc06C9DcCMt28tK1osfl5+PAEpxxEcOtLjcU0FlAf2Oe9j7DA8BPDx8IItqEiqu3WfZfeCR/05qzqyDutUKi2/wMowZ+U+FMH5J1yaIbWzWRCP3TrhNbGd0z9Uo0G/SysLZAuog+GGBAMQxn+2aD2lVxKifnE3EqJB9KScMd+aAPNVmE9mOXLQAPg6avNHCxXDuULXf+BPdQkni0TP1U7mwg3Es2ARJeG527OqaxftMlSKafOuDuMaTSQlxank7St8QAOgYLs59ZGnzBfHGiifsPuz9Cr53lpkNB7CyMBaaIA/JYaOQ9Fmk hFfqjc3n VzXqJfa2vNrHR6S7CorM17g3RGWt5jnFPDmXcmefh6wF/ZoN/FY4XXZlc8ALHAO0VzVfq3MAVKzf857kn2uI6NSrdSKCo8Xy3RRI4HqK9AMCHt2U9Wu6UG3sN/Rn4sTiwVg0EdSVU7zF4/uO7WTxy9BhKALcS9tJ25oW1qS9kfCfCIzhNCHIywcm1DlSca0dmCQfnVXkLlQk4k3Y6vxINNlfwh7G0h00qmFpynBLyFNK4MRW4CLKRpitgEuzEhh0X/rS+idwjIzEHsnvO/NVaOGfhjy8C4/8vaMpsB7plBDxrm/sMQ/QcmDVZF7C90Y3oLoZwPxAjcaKsOFLsqQVyyVsY3nkUCvfdcjrRJorX/hbwAupzCVCvRUnNGPKzy3WaEyA6IQQrW+aseNFp6dsa/+G3cKmv0tfkIqIn2Tmy3A/jrsu62O4wBrdTiJXH1kEsjtyCbF3+Lf9mtsDOzgsYCgBcDYZEyeT3wmIHe87FtvOMl0c+EtaczTK44dPf48PBPfGd X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: Since different memory devices require finding, allocating, and putting memory types, these common steps are abstracted in this patch, enhancing the scalability and conciseness of the code. Signed-off-by: Ho-Ren (Jack) Chuang Reviewed-by: "Huang, Ying" --- drivers/dax/kmem.c | 20 ++------------------ include/linux/memory-tiers.h | 13 +++++++++++++ mm/memory-tiers.c | 32 ++++++++++++++++++++++++++++++++ 3 files changed, 47 insertions(+), 18 deletions(-) diff --git a/drivers/dax/kmem.c b/drivers/dax/kmem.c index 42ee360cf4e3..01399e5b53b2 100644 --- a/drivers/dax/kmem.c +++ b/drivers/dax/kmem.c @@ -55,21 +55,10 @@ static LIST_HEAD(kmem_memory_types); static struct memory_dev_type *kmem_find_alloc_memory_type(int adist) { - bool found = false; struct memory_dev_type *mtype; mutex_lock(&kmem_memory_type_lock); - list_for_each_entry(mtype, &kmem_memory_types, list) { - if (mtype->adistance == adist) { - found = true; - break; - } - } - if (!found) { - mtype = alloc_memory_type(adist); - if (!IS_ERR(mtype)) - list_add(&mtype->list, &kmem_memory_types); - } + mtype = mt_find_alloc_memory_type(adist, &kmem_memory_types); mutex_unlock(&kmem_memory_type_lock); return mtype; @@ -77,13 +66,8 @@ static struct memory_dev_type *kmem_find_alloc_memory_type(int adist) static void kmem_put_memory_types(void) { - struct memory_dev_type *mtype, *mtn; - mutex_lock(&kmem_memory_type_lock); - list_for_each_entry_safe(mtype, mtn, &kmem_memory_types, list) { - list_del(&mtype->list); - put_memory_type(mtype); - } + mt_put_memory_types(&kmem_memory_types); mutex_unlock(&kmem_memory_type_lock); } diff --git a/include/linux/memory-tiers.h b/include/linux/memory-tiers.h index 69e781900082..a44c03c2ba3a 100644 --- a/include/linux/memory-tiers.h +++ b/include/linux/memory-tiers.h @@ -48,6 +48,9 @@ int mt_calc_adistance(int node, int *adist); int mt_set_default_dram_perf(int nid, struct access_coordinate *perf, const char *source); int mt_perf_to_adistance(struct access_coordinate *perf, int *adist); +struct memory_dev_type *mt_find_alloc_memory_type(int adist, + struct list_head *memory_types); +void mt_put_memory_types(struct list_head *memory_types); #ifdef CONFIG_MIGRATION int next_demotion_node(int node); void node_get_allowed_targets(pg_data_t *pgdat, nodemask_t *targets); @@ -136,5 +139,15 @@ static inline int mt_perf_to_adistance(struct access_coordinate *perf, int *adis { return -EIO; } + +struct memory_dev_type *mt_find_alloc_memory_type(int adist, struct list_head *memory_types) +{ + return NULL; +} + +void mt_put_memory_types(struct list_head *memory_types) +{ + +} #endif /* CONFIG_NUMA */ #endif /* _LINUX_MEMORY_TIERS_H */ diff --git a/mm/memory-tiers.c b/mm/memory-tiers.c index 0537664620e5..974af10cfdd8 100644 --- a/mm/memory-tiers.c +++ b/mm/memory-tiers.c @@ -623,6 +623,38 @@ void clear_node_memory_type(int node, struct memory_dev_type *memtype) } EXPORT_SYMBOL_GPL(clear_node_memory_type); +struct memory_dev_type *mt_find_alloc_memory_type(int adist, struct list_head *memory_types) +{ + bool found = false; + struct memory_dev_type *mtype; + + list_for_each_entry(mtype, memory_types, list) { + if (mtype->adistance == adist) { + found = true; + break; + } + } + if (!found) { + mtype = alloc_memory_type(adist); + if (!IS_ERR(mtype)) + list_add(&mtype->list, memory_types); + } + + return mtype; +} +EXPORT_SYMBOL_GPL(mt_find_alloc_memory_type); + +void mt_put_memory_types(struct list_head *memory_types) +{ + struct memory_dev_type *mtype, *mtn; + + list_for_each_entry_safe(mtype, mtn, memory_types, list) { + list_del(&mtype->list); + put_memory_type(mtype); + } +} +EXPORT_SYMBOL_GPL(mt_put_memory_types); + static void dump_hmem_attrs(struct access_coordinate *coord, const char *prefix) { pr_info( From patchwork Fri Mar 29 05:33:53 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Ho-Ren (Jack) Chuang" X-Patchwork-Id: 13610118 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 7C655CD128E for ; Fri, 29 Mar 2024 05:34:08 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 4BE7D6B0096; Fri, 29 Mar 2024 01:34:06 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 46EDE6B0098; Fri, 29 Mar 2024 01:34:06 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 2E78F6B009C; Fri, 29 Mar 2024 01:34:06 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id 1457A6B0096 for ; Fri, 29 Mar 2024 01:34:06 -0400 (EDT) Received: from smtpin15.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id C9149141114 for ; Fri, 29 Mar 2024 05:34:05 +0000 (UTC) X-FDA: 81948960450.15.06D172E Received: from mail-qt1-f175.google.com (mail-qt1-f175.google.com [209.85.160.175]) by imf04.hostedemail.com (Postfix) with ESMTP id 0845040004 for ; Fri, 29 Mar 2024 05:34:03 +0000 (UTC) Authentication-Results: imf04.hostedemail.com; dkim=pass header.d=bytedance.com header.s=google header.b=Oz2OyLtW; spf=pass (imf04.hostedemail.com: domain of horenchuang@bytedance.com designates 209.85.160.175 as permitted sender) smtp.mailfrom=horenchuang@bytedance.com; dmarc=pass (policy=quarantine) header.from=bytedance.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1711690444; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=7gywZcWj8Uy1UfLQdLtf6937dGqAomoCBcBBPX1Jltg=; b=CWAD5MCS7jyrQoBwc+hQwMkrfsGr/S1xK9IAirb/sIZkji14tYPwikCzcOMtffwKqcPbP2 ubLE1h29ZJCfZLm4Hmt407/t+yVwxtKcMxy9WvThWjObB6TEM2Om9I+yjxmPUAvKdyZQ3o dIVIYu6OgB1G9rbrNhnFXzpOQqWavOk= ARC-Authentication-Results: i=1; imf04.hostedemail.com; dkim=pass header.d=bytedance.com header.s=google header.b=Oz2OyLtW; spf=pass (imf04.hostedemail.com: domain of horenchuang@bytedance.com designates 209.85.160.175 as permitted sender) smtp.mailfrom=horenchuang@bytedance.com; dmarc=pass (policy=quarantine) header.from=bytedance.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1711690444; a=rsa-sha256; cv=none; b=aVN0AGMMR8DKNhtpOVJsVSmvJzjk9PxSvb0JUB/aWJ9gOAljyqQRXrcSvKMmfTsBxnDw93 FW+R3zVApKC4x6wECqaV0ZNT6zOupsnoOjXpJiY7tB0M/sKIS2dkj+fZjKIZ4i/ONQICsU p9Zaglw1hkggtCJytLFOKBfSM27PaVU= Received: by mail-qt1-f175.google.com with SMTP id d75a77b69052e-42f52226f9bso6364191cf.3 for ; Thu, 28 Mar 2024 22:34:03 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance.com; s=google; t=1711690443; x=1712295243; darn=kvack.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=7gywZcWj8Uy1UfLQdLtf6937dGqAomoCBcBBPX1Jltg=; b=Oz2OyLtWo88xi4ZgHIGXoxUHhw0Nk7GZh4BOHAy7fSfGEW6BRJepME+ppmKVTvxY0O WW4Yut+339r6Ofdkm7mqzKg7VxEr4J6ME/F0uJJ1bOd8oXYukljMDc3/A2yR8QM819Rx QrXemSCV9RnF0sjUI/3iPpfAgCtJtNXZ6p3C8k95n7V78vvbBwZwmIhYvQthKnc973o2 DlpDEg+Joh3bUV9nqrj7uXQuf4epVmpO8E9A6Ysw3oG7PwFY3iclExkmwNKwUuu8V3Xk qtecH7q1lE2wVBT3Relf3JK2bF2DOQ+qtzs1jL/So7rWSumUbOKNae2u1txOurRunH0j fCoQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1711690443; x=1712295243; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=7gywZcWj8Uy1UfLQdLtf6937dGqAomoCBcBBPX1Jltg=; b=YM7VMLzcNHAiGtDxL3H9pH1iPtRqYe5Wttnfw7vTJ+Ovz+JaYegewhXNVjxhxqoBFL AlMxCnhqyEZi3icCi0hcHemfl9tH59/gGvXuM45cNvk0TemjvG+D6JLuDJUEXrwOm7Tn xhbvi/SG0HahNo1lVIFSfZsv7fJZTxwoqgOsHZh8Ed/3T1DGKDrOEpT5jikohUtzaWh5 bFy3HtuNpC2ruJFNiiRzGs5rRAlz7LWFaSEusSpONGwUZSX3cbFqqKtkSsQX3BhJrPsS 6gQY2yN5yotqU5lRMCZ+Orq1TWUrbQ/x4X0hbqUAPKHz700uopJwZnehor01a9uXulKP 70cQ== X-Forwarded-Encrypted: i=1; AJvYcCUZ04FPdBAXGxLW+liH+WtTLACLzopY/JN5E/GhMi2nDs85xJI8Ivs/lbaR0IUDmdM1pMkN2YpW1n3dQPI7eTly1pU= X-Gm-Message-State: AOJu0Ywv/xAhNWtN4qsy5j70ForiCc1AQlYs4zruYsOUjIxEr9nxbsG2 8NfznDQAoFHUyWAMTlFVYNkJhz2cNv0u7Y0GoQwYhRBgRXOnxy6ZLYTOTAsi9eg= X-Google-Smtp-Source: AGHT+IHMKi8jQpAaT6v24K2SIK8/4KC+bbaOA6pAb143bozl04FLNTZDjzodC6fB+ReD22deVOL3iA== X-Received: by 2002:a05:622a:5e89:b0:432:c1b3:7985 with SMTP id er9-20020a05622a5e8900b00432c1b37985mr849659qtb.55.1711690442938; Thu, 28 Mar 2024 22:34:02 -0700 (PDT) Received: from n231-228-171.byted.org ([147.160.184.85]) by smtp.gmail.com with ESMTPSA id jd25-20020a05622a719900b00430bf59ebccsm1293700qtb.11.2024.03.28.22.34.02 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 28 Mar 2024 22:34:02 -0700 (PDT) From: "Ho-Ren (Jack) Chuang" To: "Huang, Ying" , "Gregory Price" , aneesh.kumar@linux.ibm.com, mhocko@suse.com, tj@kernel.org, john@jagalactic.com, "Eishan Mirakhur" , "Vinicius Tavares Petrucci" , "Ravis OpenSrc" , "Alistair Popple" , "Srinivasulu Thanneeru" , Dan Williams , Vishal Verma , Dave Jiang , Andrew Morton , nvdimm@lists.linux.dev, linux-cxl@vger.kernel.org, linux-kernel@vger.kernel.org, linux-mm@kvack.org Cc: "Ho-Ren (Jack) Chuang" , "Ho-Ren (Jack) Chuang" , "Ho-Ren (Jack) Chuang" , qemu-devel@nongnu.org, Hao Xiang Subject: [PATCH v9 2/2] memory tier: create CPUless memory tiers after obtaining HMAT info Date: Fri, 29 Mar 2024 05:33:53 +0000 Message-Id: <20240329053353.309557-3-horenchuang@bytedance.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20240329053353.309557-1-horenchuang@bytedance.com> References: <20240329053353.309557-1-horenchuang@bytedance.com> MIME-Version: 1.0 X-Rspamd-Queue-Id: 0845040004 X-Rspam-User: X-Stat-Signature: 8b486quqha5g4yq37tfr6t9z4ghzfdb7 X-Rspamd-Server: rspam01 X-HE-Tag: 1711690443-883811 X-HE-Meta: U2FsdGVkX191vDY+vcqxCdQKXIzKDdSX/49SPffcblMbfHYEiRFkFZCGOt949PU8J6EYB6psEwWGGfJP5xH80cYK4CLdal6Wht7dtAaIfjNyDJNs4jj8DSH7IzVvAI6u5exW1VmKfn3t8mNqpsfth2XTozqdKbGnP4kr7GR7IWeHYvu2H00UX7NB2awiaBapcvBTbjL+JcQ94UwReKNNACixANf6HjjxpVZMY1hUd1tux11xK3kDo+UAE2yleQ8FLfZ0VKa/QodUu50JounV9ZeVHEfH+IDSazJ6e6+3OT5ezGM1ewydEY19RobRoDvJjai9sBpLTCjfkOoTcsNqLHUAEuL7byyNK3vSgg+aTTRiUHgGV+YrguBY2CczxVAcrKNn8HGo/0ynbJfSeHujgDUfWs0dKT/YZyuMtfBm6zo5s9XhMO3UHpguMykTPjAz92jSLJ4YlfxwqhTrEYE79AaUFv0DItxI28URpMz8m+zqD5vvVIMPhUw1YAQ33SMxKrd5gsegPTj+4rJEIId9ZboKPtAzSLkBhCoRMMCZnZj8BfI/5USG0dr9jkvaFwnWjel4D3kJKxq6GVc50WPgwnaAV14i0AxSuDUQn8CpOJvpEUYAwuOPVZTl3wwFMgcBR+p/RhIiNld5M0QRDJLb6vaeHx6iwzkQziXtru7jjXdkxIPGAQaHhd3xqDQhfOMJqmVB+/xfcUTNv7FLPlV6BdrSvQtAVAtYIFg7Z2rOZvXNWf5diEtBXT0ekNpZw3LEQrU0hK94wiNs0SZQdfE8DlKxr2tvYbjQF8U6jVGoghopQ8DZN9k6vv4UV94NeDm1OI7F1n1HTe4Ywo3cA2UmWclDqvu8m2/y2qHTuDN7Qlrl9B0lBF9NqdPR3BjEstxcjf+orTC31Jg5U6ffgtMdYiTKP5pUjK9/xpDhKJEyikUSxGN56PYpRT6HzEXZptqj+xi7nHv+4JByfHMT5mJ NTx71RXp H0wgsy1RKVvfsaGQ2EHcOh1nUWsYgmWbI0OiJZ1fFBKlETjox+Y0IZaayypq2G1msfwNvuo51wnekZyn3/0grZ6JlrLyv7w5eB22aR2pieOWvs8Mg0c3h5NsPyarOObZwmWc3TnKpcTppOCMT4DAP8WyhkMzu045ZncgmVpS9Ee83+aFe29YKXiZp/KB0DyhZzT7xCl7R0SuI4C45dNODULqYZ2E+QOH90GaC2sn5Z5yvlZAsipDZyW5le7WjpVBRTcDkR9TyRiwFQ/8BVJLOMBTesx9eU9vmOad2aDNpAnhHbm70R1oGKl0Q8IWBYf9jb7AYCtnj2+6Pra6Z9fWdTaaIXR6HcelhhSUKMNwmpDFqKr4c+Owp/7Tc76oD3gPO5L9jly8LmkI0sBWaYNze9wrVE3+AoxqJYWXpOOeDRWOjsCiTIBEpghACgpyPj4GbtyOgIzsLU1jiW0t7RbvPPR7Mgj9sPNhK/9nvji2Vc20STdFzPt6mqVqeYdyQgwGGmHNh X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: The current implementation treats emulated memory devices, such as CXL1.1 type3 memory, as normal DRAM when they are emulated as normal memory (E820_TYPE_RAM). However, these emulated devices have different characteristics than traditional DRAM, making it important to distinguish them. Thus, we modify the tiered memory initialization process to introduce a delay specifically for CPUless NUMA nodes. This delay ensures that the memory tier initialization for these nodes is deferred until HMAT information is obtained during the boot process. Finally, demotion tables are recalculated at the end. * late_initcall(memory_tier_late_init); Some device drivers may have initialized memory tiers between `memory_tier_init()` and `memory_tier_late_init()`, potentially bringing online memory nodes and configuring memory tiers. They should be excluded in the late init. * Handle cases where there is no HMAT when creating memory tiers There is a scenario where a CPUless node does not provide HMAT information. If no HMAT is specified, it falls back to using the default DRAM tier. * Introduce another new lock `default_dram_perf_lock` for adist calculation In the current implementation, iterating through CPUlist nodes requires holding the `memory_tier_lock`. However, `mt_calc_adistance()` will end up trying to acquire the same lock, leading to a potential deadlock. Therefore, we propose introducing a standalone `default_dram_perf_lock` to protect `default_dram_perf_*`. This approach not only avoids deadlock but also prevents holding a large lock simultaneously. * Upgrade `set_node_memory_tier` to support additional cases, including default DRAM, late CPUless, and hot-plugged initializations. To cover hot-plugged memory nodes, `mt_calc_adistance()` and `mt_find_alloc_memory_type()` are moved into `set_node_memory_tier()` to handle cases where memtype is not initialized and where HMAT information is available. * Introduce `default_memory_types` for those memory types that are not initialized by device drivers. Because late initialized memory and default DRAM memory need to be managed, a default memory type is created for storing all memory types that are not initialized by device drivers and as a fallback. Signed-off-by: Ho-Ren (Jack) Chuang Signed-off-by: Hao Xiang Reviewed-by: "Huang, Ying" --- mm/memory-tiers.c | 93 +++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 77 insertions(+), 16 deletions(-) diff --git a/mm/memory-tiers.c b/mm/memory-tiers.c index 974af10cfdd8..9f8ae99e8e6e 100644 --- a/mm/memory-tiers.c +++ b/mm/memory-tiers.c @@ -36,6 +36,11 @@ struct node_memory_type_map { static DEFINE_MUTEX(memory_tier_lock); static LIST_HEAD(memory_tiers); +/* + * The list is used to store all memory types that are not created + * by a device driver. + */ +static LIST_HEAD(default_memory_types); static struct node_memory_type_map node_memory_types[MAX_NUMNODES]; struct memory_dev_type *default_dram_type; @@ -108,6 +113,8 @@ static struct demotion_nodes *node_demotion __read_mostly; static BLOCKING_NOTIFIER_HEAD(mt_adistance_algorithms); +/* The lock is used to protect `default_dram_perf*` info and nid. */ +static DEFINE_MUTEX(default_dram_perf_lock); static bool default_dram_perf_error; static struct access_coordinate default_dram_perf; static int default_dram_perf_ref_nid = NUMA_NO_NODE; @@ -505,7 +512,8 @@ static inline void __init_node_memory_type(int node, struct memory_dev_type *mem static struct memory_tier *set_node_memory_tier(int node) { struct memory_tier *memtier; - struct memory_dev_type *memtype; + struct memory_dev_type *mtype = default_dram_type; + int adist = MEMTIER_ADISTANCE_DRAM; pg_data_t *pgdat = NODE_DATA(node); @@ -514,11 +522,20 @@ static struct memory_tier *set_node_memory_tier(int node) if (!node_state(node, N_MEMORY)) return ERR_PTR(-EINVAL); - __init_node_memory_type(node, default_dram_type); + mt_calc_adistance(node, &adist); + if (node_memory_types[node].memtype == NULL) { + mtype = mt_find_alloc_memory_type(adist, &default_memory_types); + if (IS_ERR(mtype)) { + mtype = default_dram_type; + pr_info("Failed to allocate a memory type. Fall back.\n"); + } + } + + __init_node_memory_type(node, mtype); - memtype = node_memory_types[node].memtype; - node_set(node, memtype->nodes); - memtier = find_create_memory_tier(memtype); + mtype = node_memory_types[node].memtype; + node_set(node, mtype->nodes); + memtier = find_create_memory_tier(mtype); if (!IS_ERR(memtier)) rcu_assign_pointer(pgdat->memtier, memtier); return memtier; @@ -655,6 +672,33 @@ void mt_put_memory_types(struct list_head *memory_types) } EXPORT_SYMBOL_GPL(mt_put_memory_types); +/* + * This is invoked via `late_initcall()` to initialize memory tiers for + * CPU-less memory nodes after driver initialization, which is + * expected to provide `adistance` algorithms. + */ +static int __init memory_tier_late_init(void) +{ + int nid; + + mutex_lock(&memory_tier_lock); + for_each_node_state(nid, N_MEMORY) + if (node_memory_types[nid].memtype == NULL) + /* + * Some device drivers may have initialized memory tiers + * between `memory_tier_init()` and `memory_tier_late_init()`, + * potentially bringing online memory nodes and + * configuring memory tiers. Exclude them here. + */ + set_node_memory_tier(nid); + + establish_demotion_targets(); + mutex_unlock(&memory_tier_lock); + + return 0; +} +late_initcall(memory_tier_late_init); + static void dump_hmem_attrs(struct access_coordinate *coord, const char *prefix) { pr_info( @@ -668,7 +712,7 @@ int mt_set_default_dram_perf(int nid, struct access_coordinate *perf, { int rc = 0; - mutex_lock(&memory_tier_lock); + mutex_lock(&default_dram_perf_lock); if (default_dram_perf_error) { rc = -EIO; goto out; @@ -716,23 +760,30 @@ int mt_set_default_dram_perf(int nid, struct access_coordinate *perf, } out: - mutex_unlock(&memory_tier_lock); + mutex_unlock(&default_dram_perf_lock); return rc; } int mt_perf_to_adistance(struct access_coordinate *perf, int *adist) { - if (default_dram_perf_error) - return -EIO; + int rc = 0; - if (default_dram_perf_ref_nid == NUMA_NO_NODE) - return -ENOENT; + mutex_lock(&default_dram_perf_lock); + if (default_dram_perf_error) { + rc = -EIO; + goto out; + } if (perf->read_latency + perf->write_latency == 0 || - perf->read_bandwidth + perf->write_bandwidth == 0) - return -EINVAL; + perf->read_bandwidth + perf->write_bandwidth == 0) { + rc = -EINVAL; + goto out; + } - mutex_lock(&memory_tier_lock); + if (default_dram_perf_ref_nid == NUMA_NO_NODE) { + rc = -ENOENT; + goto out; + } /* * The abstract distance of a memory node is in direct proportion to * its memory latency (read + write) and inversely proportional to its @@ -745,8 +796,9 @@ int mt_perf_to_adistance(struct access_coordinate *perf, int *adist) (default_dram_perf.read_latency + default_dram_perf.write_latency) * (default_dram_perf.read_bandwidth + default_dram_perf.write_bandwidth) / (perf->read_bandwidth + perf->write_bandwidth); - mutex_unlock(&memory_tier_lock); +out: + mutex_unlock(&default_dram_perf_lock); return 0; } EXPORT_SYMBOL_GPL(mt_perf_to_adistance); @@ -858,7 +910,8 @@ static int __init memory_tier_init(void) * For now we can have 4 faster memory tiers with smaller adistance * than default DRAM tier. */ - default_dram_type = alloc_memory_type(MEMTIER_ADISTANCE_DRAM); + default_dram_type = mt_find_alloc_memory_type(MEMTIER_ADISTANCE_DRAM, + &default_memory_types); if (IS_ERR(default_dram_type)) panic("%s() failed to allocate default DRAM tier\n", __func__); @@ -868,6 +921,14 @@ static int __init memory_tier_init(void) * types assigned. */ for_each_node_state(node, N_MEMORY) { + if (!node_state(node, N_CPU)) + /* + * Defer memory tier initialization on CPUless numa nodes. + * These will be initialized after firmware and devices are + * initialized. + */ + continue; + memtier = set_node_memory_tier(node); if (IS_ERR(memtier)) /*