From patchwork Tue Sep 12 08:21:01 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Huang, Ying" X-Patchwork-Id: 13381039 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 D8693CA0ECA for ; Tue, 12 Sep 2023 08:21:52 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 766146B00CC; Tue, 12 Sep 2023 04:21:52 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 715BC6B00CD; Tue, 12 Sep 2023 04:21:52 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 5B9246B00CE; Tue, 12 Sep 2023 04:21:52 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id 46FCF6B00CC for ; Tue, 12 Sep 2023 04:21:52 -0400 (EDT) Received: from smtpin17.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id 2F955B3F6B for ; Tue, 12 Sep 2023 08:21:52 +0000 (UTC) X-FDA: 81227252064.17.CE0A853 Received: from mgamail.intel.com (mgamail.intel.com [134.134.136.31]) by imf06.hostedemail.com (Postfix) with ESMTP id 05228180005 for ; Tue, 12 Sep 2023 08:21:49 +0000 (UTC) Authentication-Results: imf06.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=nlKfgHNN; dmarc=pass (policy=none) header.from=intel.com; spf=pass (imf06.hostedemail.com: domain of ying.huang@intel.com designates 134.134.136.31 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=1694506910; 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=9UXPZ2hd8r/D2KCGsv3BU7JBNJRwGcw5KD/mPjMQNGY=; b=YIceLKffHZJyqe981EePAhZ7Il8wve2w5tnDiJhCxXHAajLh3yV7vPN4WyeaeFQYtEhafO imJxDujYSCjm7M1qXQvRxadhwpDR3Zv/KKKpymD5jSITMBPcTGmRqfjVfdFMNoOjcW78kE LwK0RxGrOroJNrAVqZXgAAeF6g64JSo= ARC-Authentication-Results: i=1; imf06.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=nlKfgHNN; dmarc=pass (policy=none) header.from=intel.com; spf=pass (imf06.hostedemail.com: domain of ying.huang@intel.com designates 134.134.136.31 as permitted sender) smtp.mailfrom=ying.huang@intel.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1694506910; a=rsa-sha256; cv=none; b=uW2nM3mxfcNow8/UvZsmDaNKYk51L0m2R/169MbpJNf9plnNsG/MYGRgW1nYvSsLyauBev DtIqEOmz9DBc/oASPbKwQMjOzYfWGmT5llIs2NhLA1Rk5AZvWi0PSjIMfEgD9wSi/BRjti DFyJ/CpiLSVeEjuohAJzZi0/+Yfmfo4= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1694506910; x=1726042910; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=9suKmuHrRCqPZ954lWyRT88cx6w5DPJyIVkc7QOTcgM=; b=nlKfgHNN2yjYd+JSBfblqYI0tlgp4pYpGErSdSxN5hAeAXmwLxn88gHd ag+5/Twv8Dwkm+RSJDiOq5QgAcaUqj5GdtqkglJLgtBILKTS682BoEK+q AJjBfbvc8ATKGAUBDwG8dL+Oh6p/jFaf5E0EPxBdxQyn0qrg61Xhl6UiQ 24a70staTdUHL3UG27NBUJyy5BEKeS7Ji4YFXxc7PNWjFIeHOHt0g4Q0i Wirv7z7Z2jNd8SrPilF0h+F/ztHf0jjPbpD51hiV/3nR9lzrMrg+VUkuy SqjaUqU7sErYzKUirIVftIawOiVAF4ZbB9OmvDY3cAT4OAZf+yLIB0rAp g==; X-IronPort-AV: E=McAfee;i="6600,9927,10830"; a="442323077" X-IronPort-AV: E=Sophos;i="6.02,245,1688454000"; d="scan'208";a="442323077" Received: from fmsmga007.fm.intel.com ([10.253.24.52]) by orsmga104.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 12 Sep 2023 01:21:49 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10830"; a="746787712" X-IronPort-AV: E=Sophos;i="6.02,245,1688454000"; d="scan'208";a="746787712" Received: from shicha9x-mobl.ccr.corp.intel.com (HELO yhuang6-mobl2.ccr.corp.intel.com) ([10.255.28.213]) by fmsmga007-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 12 Sep 2023 01:21:43 -0700 From: Huang Ying To: Andrew Morton Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org, Huang Ying , Bharata B Rao , "Aneesh Kumar K . V" , Wei Xu , Alistair Popple , Dan Williams , Dave Hansen , Davidlohr Bueso , Johannes Weiner , Jonathan Cameron , Michal Hocko , Yang Shi , Dave Jiang , Rafael J Wysocki Subject: [PATCH -V3 4/4] dax, kmem: calculate abstract distance with general interface Date: Tue, 12 Sep 2023 16:21:01 +0800 Message-Id: <20230912082101.342002-5-ying.huang@intel.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230912082101.342002-1-ying.huang@intel.com> References: <20230912082101.342002-1-ying.huang@intel.com> MIME-Version: 1.0 X-Rspam-User: X-Stat-Signature: bzc6759i1mih664tp1ar9nyxa55wd71p X-Rspamd-Server: rspam07 X-Rspamd-Queue-Id: 05228180005 X-HE-Tag: 1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Y/NBofRgoVaD43y+ZnHkvvEFXth3fWKj37u7qrvRTklAYqb04/NGP9A0+wOqwDUzTFZe7n9Zxdi9WSdjF7FppETHGGIc9Ydt9l7uVHDVHRb6tr3FQ1/NPiKHmjbMaUp/D4k+LemXZ/fNYxWwxRR9HFiBdsxKjMYVp9q0twQjtODHjBLtIU+BCxfj7PxsvDzufeTGMtEhRL2+ZgEFCWgOKdbPksxNBCnEP4cNtBP3oK0afz/R7FL4JlrAgfWOiUFAqi6dpukziai8H4uy3skktxy/1nzuUyGdOH76rJplBma0AR+clRtmmd1Kfs1yKqfdurH10kaVwBnq/ZMS7wMKDSZeZ1r+BZhsqVciO9Ur9V0BVr4qwdFEsebtW+LpUiqbOiyt/rX19HXjcuKGd2QJw 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 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: Dave Jiang Cc: Rafael J Wysocki Reviewed-by: Dave Jiang --- drivers/dax/kmem.c | 62 ++++++++++++++++++++++++++++-------- include/linux/memory-tiers.h | 2 ++ mm/memory-tiers.c | 2 +- 3 files changed, 52 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..ca68ef17554b 100644 --- a/mm/memory-tiers.c +++ b/mm/memory-tiers.c @@ -586,7 +586,7 @@ 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,