From patchwork Wed Jul 12 07:49:00 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Huang, Ying" X-Patchwork-Id: 118905 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:a6b2:0:b0:3e4:2afc:c1 with SMTP id c18csp983767vqm; Wed, 12 Jul 2023 01:02:52 -0700 (PDT) X-Google-Smtp-Source: APBJJlEJwJJtXsjYRrs9d88O1xl8AMbgd3RI5CzZr7mJ0LLjRmqFXyOihApRKP9vT13iwdvWwbVw X-Received: by 2002:a05:6808:1923:b0:3a1:9540:feb1 with SMTP id bf35-20020a056808192300b003a19540feb1mr18699257oib.14.1689148972001; Wed, 12 Jul 2023 01:02:52 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1689148971; cv=none; d=google.com; s=arc-20160816; b=NDqe9Zav+8QsakjKrpagnhg+/YaeitTgKeCK8vgDtX0DWW7TJKr/f8M+OWkTjOI2EG ULR5Qe7i+u1bYzOT0davRg3og1mKQKQDaikmQnwbKQ19v1mo+sVJZoFE8cfQ1B/i3Ebi MOzubhpdj+3WtLLxibc2cFYRU5y4M9JpWelQSTHd+nNJTUkOr2p6WC0OZ+dPeVcXq6O3 vfEZzlRhbyirM3zfbGZHAvjb24GlOL1FOQ7mW1uL3a8nIq3coAv/ycmxXF9056TJFOXD lDIe1ywzLLbe11vqZRFti6V0rm0ZJtDiAchJDN3janf9EOLflXeTlkqy2ZhZnnVYMFKO wpbA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=QV8Teo1j0CFA6btKhSsxBE6pLS/TyY9NOfA3+UQFqbU=; fh=Xr6SCA0sZkk+kgDr9C2Ugn8MI7grTbNuIFF2Y8Jvxmw=; b=Ea+UmyUBqKLUGaZP9QOEa3So1JoJ+AqHeWjdQtQEyEo6UTqFSWPfMfE0YJdXWnHmhY zEREotN2t4yeJm492wrVCAOZe+DB3VMaQ9Uc4Jflmcs8OLq6KuEff366zw3qeRpSyAde C/wRm9rLKYA9Tch9DX8Qmd+4KNtOreyCOdWM60O+ohKcHMkyqp9M+oaJO9OFRHI2uUol 8+G3qN4gkjb/xUrzsL328W7YTfjDpT2WYJixwnJTdv5+hJfc1SHTaiEQu0CbT73bpPp6 AQRTRznsurJUwz3NNclKF9HkLg5DyGPHSl1jVQZDqV5gME8focwKGLvtrbAJGRlnSzVE h1BA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=frTpFi+h; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id n127-20020a632785000000b00533fce755adsi759981pgn.130.2023.07.12.01.02.38; Wed, 12 Jul 2023 01:02:51 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=frTpFi+h; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231847AbjGLHtz (ORCPT + 99 others); Wed, 12 Jul 2023 03:49:55 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35096 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231836AbjGLHtp (ORCPT ); Wed, 12 Jul 2023 03:49:45 -0400 Received: from mga02.intel.com (mga02.intel.com [134.134.136.20]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 9EDB21BE5 for ; Wed, 12 Jul 2023 00:49:33 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1689148173; x=1720684173; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=WgH5xLOkBW2Y/oZqJhHhP4ppJv8kSHk1fii5X3wpokk=; b=frTpFi+hRbB7igPVzLdH8Dl29Gnm342nilq9/Hd+EJzahUlRi+w98OIz f15La4HTkRhNZD0aqtxl5fBNLccgwY16Xdr424t5qIKPf5i7MOQtzFGst F3YOW7/dMbfau5yjmSSd4MSmz90a2efZ3qaU8ZmPQxLnHynvLhTZzEVte e5Olwoa9mAdXZgdPBNjlkOSz2K0z6jaf0Z0QniW7vTa5uIICI11MECQvd ZAVrk8l7zTCqrItEYrS+te/TSsQnkBd0LC4SkYnnR+y145E5c/OMtRH5d 9UK85e+jc5gNtFZvaPDSER8uhkGtU4/te7/1c/oWVQZbJylxzL0a4ofVz w==; X-IronPort-AV: E=McAfee;i="6600,9927,10768"; a="354734622" X-IronPort-AV: E=Sophos;i="6.01,199,1684825200"; d="scan'208";a="354734622" Received: from orsmga006.jf.intel.com ([10.7.209.51]) by orsmga101.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 12 Jul 2023 00:49:32 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10768"; a="698744941" X-IronPort-AV: E=Sophos;i="6.01,199,1684825200"; d="scan'208";a="698744941" Received: from yhuang6-mobl2.sh.intel.com ([10.238.6.30]) by orsmga006-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 12 Jul 2023 00:49:28 -0700 From: Huang Ying To: Andrew Morton Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org, Huang Ying , "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 4/4] dax, kmem: calculate abstract distance with general interface Date: Wed, 12 Jul 2023 15:49:00 +0800 Message-Id: <20230712074900.404064-5-ying.huang@intel.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230712074900.404064-1-ying.huang@intel.com> References: <20230712074900.404064-1-ying.huang@intel.com> MIME-Version: 1.0 X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF, RCVD_IN_DNSWL_BLOCKED,RCVD_IN_MSPIKE_H3,RCVD_IN_MSPIKE_WL, SPF_HELO_NONE,SPF_NONE,T_SCC_BODY_TEXT_LINE,URIBL_BLOCKED autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1771201072432505058 X-GMAIL-MSGID: 1771201072432505058 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" 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 --- drivers/dax/kmem.c | 54 +++++++++++++++++++++++++++--------- include/linux/memory-tiers.h | 2 ++ mm/memory-tiers.c | 2 +- 3 files changed, 44 insertions(+), 14 deletions(-) diff --git a/drivers/dax/kmem.c b/drivers/dax/kmem.c index 898ca9505754..837165037231 100644 --- a/drivers/dax/kmem.c +++ b/drivers/dax/kmem.c @@ -49,14 +49,40 @@ 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_memorty_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 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 +97,11 @@ static int dev_dax_kmem_probe(struct dev_dax *dev_dax) return -EINVAL; } + mt_calc_adistance(numa_node, &adist); + mtype = kmem_find_alloc_memorty_type(adist); + if (IS_ERR(mtype)) + return PTR_ERR(mtype); + for (i = 0; i < dev_dax->nr_range; i++) { struct range range; @@ -88,7 +119,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 +198,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 +250,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 +282,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,18 +289,21 @@ static int __init dax_kmem_init(void) return rc; error_dax_driver: - destroy_memory_type(dax_slowmem_type); -err_dax_slowmem_type: kfree_const(kmem_name); return rc; } static void __exit dax_kmem_exit(void) { + struct memory_dev_type *mtype, *mtn; + dax_driver_unregister(&device_dax_kmem_driver); if (!any_hotremove_failed) kfree_const(kmem_name); - destroy_memory_type(dax_slowmem_type); + list_for_each_entry_safe(mtype, mtn, &kmem_memory_types, list) { + list_del(&mtype->list); + destroy_memory_type(mtype); + } } MODULE_AUTHOR("Intel Corporation"); diff --git a/include/linux/memory-tiers.h b/include/linux/memory-tiers.h index 9377239c8d34..aca22220cb5c 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 9a734ef2edfb..38005c60fa2d 100644 --- a/mm/memory-tiers.c +++ b/mm/memory-tiers.c @@ -581,7 +581,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,