From patchwork Tue Sep 26 06:06:28 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Huang, Ying" X-Patchwork-Id: 13398707 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 04994E7D0C5 for ; Tue, 26 Sep 2023 06:07:09 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 926908D0064; Tue, 26 Sep 2023 02:07:08 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 8D6578D0005; Tue, 26 Sep 2023 02:07:08 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 74EC68D0064; Tue, 26 Sep 2023 02:07:08 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id 6017A8D0005 for ; Tue, 26 Sep 2023 02:07:08 -0400 (EDT) Received: from smtpin13.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id 2F6E440F9D for ; Tue, 26 Sep 2023 06:07:08 +0000 (UTC) X-FDA: 81277715736.13.CEC9401 Received: from mgamail.intel.com (mgamail.intel.com [134.134.136.100]) by imf12.hostedemail.com (Postfix) with ESMTP id 0500040004 for ; Tue, 26 Sep 2023 06:07:05 +0000 (UTC) Authentication-Results: imf12.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=VgdPw6QL; dmarc=pass (policy=none) header.from=intel.com; spf=pass (imf12.hostedemail.com: domain of ying.huang@intel.com designates 134.134.136.100 as permitted sender) smtp.mailfrom=ying.huang@intel.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1695708426; a=rsa-sha256; cv=none; b=QlQza+cxp0ORyPWYPj6+9pHQEoPTqyem4wja/4S4MhHg6oVQTy6SGiI/yXVUANmou/Qt07 39PGsSOHy+4sY+5uL2GgdYWelA86uDnKqHcSPKDQNkxaZjt1t1wtIybF0Ve4ms4DGqHKFu yDNy9KRJSvMKA6W5Gl5kKhos17StJbU= ARC-Authentication-Results: i=1; imf12.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=VgdPw6QL; dmarc=pass (policy=none) header.from=intel.com; spf=pass (imf12.hostedemail.com: domain of ying.huang@intel.com designates 134.134.136.100 as permitted sender) smtp.mailfrom=ying.huang@intel.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1695708426; 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=ejMWpaHQI23M8H2zPM9TNrUkh1HO1AccqncrT0WnHE8=; b=LsW6OmMxEzSCC8yF4HO/f8CXT1MJ4DjKpXUwB2kdL0G2lC7zS8eCY4DRIRNUJmEHLsv9tn KjTKu0uBwgX9qCL8QRVImFuyhodk4g55IdPJrjZTI6qVULvZea6W5zLezbrpBPTDDLENzc nnLYPj25zhR+6AlgeFYuPLx+rc/ZdNI= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1695708426; x=1727244426; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=kfBl53G6+3W4hrsT7f5lhn9OvXRFwRRxK2VozpSoI5M=; b=VgdPw6QLQMqXwGtPPcXgzveZaqJMSsZBpYMYFsrXunrJFyQvzpILWg3l J759AoQw+9Ro2Nxs0qWvI0maFrn+K4d5xXiUHpX7Mn9c+fy7PLhSWDMhQ n+yUfAkHJLPdL+ajDj07RC+haIqf084THkyAtghlb5C0sIV/F8i3bUTvB c083m/yE29/mBU64fdS6cOau4qD2TQUgSz9RjlzBqA3mT1zpuryGZDs4k vAvnOD1Zqm+U5HPxL6lHLKoAxYlPN+2sXRll3ZGAdBDbxwlA/JYJUviGH 8gJh8NCUaYBDnuaPexyT0Y26NhCecdRF1F42XuhErbn1t+OuKB9v1W+fG Q==; X-IronPort-AV: E=McAfee;i="6600,9927,10843"; a="447990870" X-IronPort-AV: E=Sophos;i="6.03,177,1694761200"; d="scan'208";a="447990870" Received: from fmsmga001.fm.intel.com ([10.253.24.23]) by orsmga105.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 25 Sep 2023 23:07:05 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10843"; a="892074807" X-IronPort-AV: E=Sophos;i="6.03,177,1694761200"; d="scan'208";a="892074807" Received: from aozhu-mobl.ccr.corp.intel.com (HELO yhuang6-mobl2.ccr.corp.intel.com) ([10.255.31.94]) by fmsmga001-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 25 Sep 2023 23:05:56 -0700 From: Huang Ying To: Andrew Morton Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org, Huang Ying , Bharata B Rao , Dave Jiang , "Aneesh Kumar K . V" , Wei Xu , Alistair Popple , Dan Williams , Dave Hansen , Davidlohr Bueso , Johannes Weiner , Jonathan Cameron , Michal Hocko , Yang Shi , Rafael J Wysocki Subject: [PATCH -V4 4/4] dax, kmem: calculate abstract distance with general interface Date: Tue, 26 Sep 2023 14:06:28 +0800 Message-Id: <20230926060628.265989-5-ying.huang@intel.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230926060628.265989-1-ying.huang@intel.com> References: <20230926060628.265989-1-ying.huang@intel.com> MIME-Version: 1.0 X-Rspam-User: X-Rspamd-Server: rspam06 X-Rspamd-Queue-Id: 0500040004 X-Stat-Signature: efccrm5psdtxjb57brcgsmizxcdcpyoo X-HE-Tag: 1695708425-107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uJZgt0+4BsL9/fog/uqOPqMBMeQsJiDHWSk8oUA1ZQ02S6Q8WzCWuEM0ci/iX5YthLByc84HdCmCFcmb2qXqLXKSa9QNBt/JfTDQLwJMeSh84W3pDz1xYgDRll1NgP14RW2RAoGgP1vacG9LeuyIVJoBT2VZKo9bhrOG3QUPV3yFij4UNmTuBtZGNk31d8eZF0kHuZtsP7cvpSkvd4SZgKc1tnU5LpdZ64rkvef2dEga7wX9DV+AwyD6Fb9Ez6rG5/wGnRN4N0/KCR+7nZlbAu4jFH4ENYYauKCGp0hbpjavssF5uaA06eE1rOUcvqx9vTq/xR3ycXa3qthEkQhbBvZirsLP9tmRA0leGnFKEe3dNUC8ra1vfOdkmr5zFqbHQZki3lZRamBRBvWKKtGGTBZEL9m1WrsO1+q 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: Previously, a fixed abstract distance MEMTIER_DEFAULT_DAX_ADISTANCE is used for slow memory type in kmem driver. This limits the usage of kmem driver, for example, it cannot be used for HBM (high bandwidth memory). So, we use the general abstract distance calculation mechanism in kmem drivers to get more accurate abstract distance on systems with proper support. The original MEMTIER_DEFAULT_DAX_ADISTANCE is used as fallback only. Now, multiple memory types may be managed by kmem. These memory types are put into the "kmem_memory_types" list and protected by kmem_memory_type_lock. Signed-off-by: "Huang, Ying" Tested-by: Bharata B Rao Reviewed-by: Dave Jiang Cc: Aneesh Kumar K.V Cc: Wei Xu Cc: Alistair Popple Cc: Dan Williams Cc: Dave Hansen Cc: Davidlohr Bueso Cc: Johannes Weiner Cc: Jonathan Cameron Cc: Michal Hocko Cc: Yang Shi Cc: Rafael J Wysocki Reviewed-by: Alistair Popple --- drivers/dax/kmem.c | 62 ++++++++++++++++++++++++++++-------- include/linux/memory-tiers.h | 2 ++ mm/memory-tiers.c | 3 +- 3 files changed, 53 insertions(+), 14 deletions(-) diff --git a/drivers/dax/kmem.c b/drivers/dax/kmem.c index c57acb73e3db..369c698b7706 100644 --- a/drivers/dax/kmem.c +++ b/drivers/dax/kmem.c @@ -49,14 +49,52 @@ struct dax_kmem_data { struct resource *res[]; }; -static struct memory_dev_type *dax_slowmem_type; +static DEFINE_MUTEX(kmem_memory_type_lock); +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); + } + mutex_unlock(&kmem_memory_type_lock); + + return mtype; +} + +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); + } + mutex_unlock(&kmem_memory_type_lock); +} + static int dev_dax_kmem_probe(struct dev_dax *dev_dax) { struct device *dev = &dev_dax->dev; unsigned long total_len = 0; struct dax_kmem_data *data; + struct memory_dev_type *mtype; int i, rc, mapped = 0; int numa_node; + int adist = MEMTIER_DEFAULT_DAX_ADISTANCE; /* * Ensure good NUMA information for the persistent memory. @@ -71,6 +109,11 @@ static int dev_dax_kmem_probe(struct dev_dax *dev_dax) return -EINVAL; } + mt_calc_adistance(numa_node, &adist); + mtype = kmem_find_alloc_memory_type(adist); + if (IS_ERR(mtype)) + return PTR_ERR(mtype); + for (i = 0; i < dev_dax->nr_range; i++) { struct range range; @@ -88,7 +131,7 @@ static int dev_dax_kmem_probe(struct dev_dax *dev_dax) return -EINVAL; } - init_node_memory_type(numa_node, dax_slowmem_type); + init_node_memory_type(numa_node, mtype); rc = -ENOMEM; data = kzalloc(struct_size(data, res, dev_dax->nr_range), GFP_KERNEL); @@ -167,7 +210,7 @@ static int dev_dax_kmem_probe(struct dev_dax *dev_dax) err_res_name: kfree(data); err_dax_kmem_data: - clear_node_memory_type(numa_node, dax_slowmem_type); + clear_node_memory_type(numa_node, mtype); return rc; } @@ -219,7 +262,7 @@ static void dev_dax_kmem_remove(struct dev_dax *dev_dax) * for that. This implies this reference will be around * till next reboot. */ - clear_node_memory_type(node, dax_slowmem_type); + clear_node_memory_type(node, NULL); } } #else @@ -251,12 +294,6 @@ static int __init dax_kmem_init(void) if (!kmem_name) return -ENOMEM; - dax_slowmem_type = alloc_memory_type(MEMTIER_DEFAULT_DAX_ADISTANCE); - if (IS_ERR(dax_slowmem_type)) { - rc = PTR_ERR(dax_slowmem_type); - goto err_dax_slowmem_type; - } - rc = dax_driver_register(&device_dax_kmem_driver); if (rc) goto error_dax_driver; @@ -264,8 +301,7 @@ static int __init dax_kmem_init(void) return rc; error_dax_driver: - put_memory_type(dax_slowmem_type); -err_dax_slowmem_type: + kmem_put_memory_types(); kfree_const(kmem_name); return rc; } @@ -275,7 +311,7 @@ static void __exit dax_kmem_exit(void) dax_driver_unregister(&device_dax_kmem_driver); if (!any_hotremove_failed) kfree_const(kmem_name); - put_memory_type(dax_slowmem_type); + kmem_put_memory_types(); } MODULE_AUTHOR("Intel Corporation"); diff --git a/include/linux/memory-tiers.h b/include/linux/memory-tiers.h index 9d27ca3b143e..ab6651402d7e 100644 --- a/include/linux/memory-tiers.h +++ b/include/linux/memory-tiers.h @@ -24,6 +24,8 @@ struct memory_tier; struct memory_dev_type { /* list of memory types that are part of same tier as this type */ struct list_head tier_sibiling; + /* list of memory types that are managed by one driver */ + struct list_head list; /* abstract distance for this specific memory type */ int adistance; /* Nodes of same abstract distance */ diff --git a/mm/memory-tiers.c b/mm/memory-tiers.c index fa1a8b418f9a..a28606e65ef3 100644 --- a/mm/memory-tiers.c +++ b/mm/memory-tiers.c @@ -586,13 +586,14 @@ EXPORT_SYMBOL_GPL(init_node_memory_type); void clear_node_memory_type(int node, struct memory_dev_type *memtype) { mutex_lock(&memory_tier_lock); - if (node_memory_types[node].memtype == memtype) + if (node_memory_types[node].memtype == memtype || !memtype) node_memory_types[node].map_count--; /* * If we umapped all the attached devices to this node, * clear the node memory type. */ if (!node_memory_types[node].map_count) { + memtype = node_memory_types[node].memtype; node_memory_types[node].memtype = NULL; put_memory_type(memtype); }