From patchwork Tue Mar 12 06:17:27 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: 13589532 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 EF72AC54E67 for ; Tue, 12 Mar 2024 06:18:02 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 7E8776B0192; Tue, 12 Mar 2024 02:18:02 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 771956B0193; Tue, 12 Mar 2024 02:18:02 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 576976B0194; Tue, 12 Mar 2024 02:18:02 -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 42BFF6B0192 for ; Tue, 12 Mar 2024 02:18:02 -0400 (EDT) Received: from smtpin14.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id 0F61441011 for ; Tue, 12 Mar 2024 06:18:02 +0000 (UTC) X-FDA: 81887381604.14.D4BF8CC Received: from mail-qk1-f174.google.com (mail-qk1-f174.google.com [209.85.222.174]) by imf06.hostedemail.com (Postfix) with ESMTP id 6363D180004 for ; Tue, 12 Mar 2024 06:18:00 +0000 (UTC) Authentication-Results: imf06.hostedemail.com; dkim=pass header.d=bytedance.com header.s=google header.b=MBitKKAy; dmarc=pass (policy=quarantine) header.from=bytedance.com; spf=pass (imf06.hostedemail.com: domain of horenchuang@bytedance.com designates 209.85.222.174 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=1710224280; 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=3g1w+Bq/HKayQEtXVRZT/0IpFF4Q07xqLAcu8q3O3ps=; b=Rq8OFYl9gbghQ98TUD1ZqkGduwHvYVdhgRB0JeY8+mG7WHgblHM6DhN9eU6Y4RPcr/DUWA HRmaR/g2eKYTfpDsEL3m35jr9jMxOPtHBdgxKS+8LwhqSIGZlN/g8fzuF9rcoXTGLDHouc xQ+YxYUWZzurZL09ROx9FzMG+IMA318= ARC-Authentication-Results: i=1; imf06.hostedemail.com; dkim=pass header.d=bytedance.com header.s=google header.b=MBitKKAy; dmarc=pass (policy=quarantine) header.from=bytedance.com; spf=pass (imf06.hostedemail.com: domain of horenchuang@bytedance.com designates 209.85.222.174 as permitted sender) smtp.mailfrom=horenchuang@bytedance.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1710224280; a=rsa-sha256; cv=none; b=VUJtD0s+VVciNbvpqOVX9qSY1Hp57RKqO8wYmybHv5oyEEmixYtdvnoLZH1rOgCbfLlouK qlJqIiC2ttQp7xjkQCnCZr9HuDOn3EOEr/rz4QmatYaTRy6sDzI7GmDxfys6l+U9Pv+T5J vU7DyB4S5kbrSgYcy7My8l1BK8atJjI= Received: by mail-qk1-f174.google.com with SMTP id af79cd13be357-78863dc4247so118006885a.3 for ; Mon, 11 Mar 2024 23:18:00 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance.com; s=google; t=1710224279; x=1710829079; 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=3g1w+Bq/HKayQEtXVRZT/0IpFF4Q07xqLAcu8q3O3ps=; b=MBitKKAyrQnNWqzlV9mG4bGRiY95RdjIBftwtbHtf3lp1x7wCeYex1eatXbz97sej8 9RXV6ufYH7fSTs5K85J4D+R8Xwz/vHs4T3wI/lrd1q/URpOxW8FRg5dbjGiWYqyvsp9k 6DRdaRUAN0Nix8iiG7Ox3EDilZ3WnUeXyt4S3B2kJ9+G0SIj+nOUh8vG5g+l4dgv1H/H AuYfRKswEFKrI+kGJ16mgSMjNsxgBfEs59SK8v+5x1s2oHhkWz2YzySVbgoYnPvD7CW0 CR6f6MdtAkhJWZug7Q04fKcIDENfgY1rttVNO1F1R5/4QIcrxcXkPV4ey15VmF7Dr/AJ 6t2w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1710224279; x=1710829079; 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=3g1w+Bq/HKayQEtXVRZT/0IpFF4Q07xqLAcu8q3O3ps=; b=CCp6NxL7zbL3LhGSkqjYQyKPE/ha7FVkPM3k0226vKwodeDMyn4GOb0ltxMQ1bPNa2 TSPoBWqinkbfEWtpwReoKTW1qD5iz0n70yqz/KeYinT29lshH675brdpUpCUYSbtYfhC /bACX9pu4IDTkRJIUA+/M4GaR41a0Le/xLdPsgKtqrb3Vc1/4hBGwATb6xZA5qgZEO6W MvoK0q2kl6oD/XKxyG83bCz7pM9w+wT7JPs+/RGPaD+hLmX2LzAfFVuhFo1KdVhUQ2nj 0SAfmNq3HTMfYiXRKKuvKcEv/8CMQ5QUiqQgcCt1E5GxeYG10JnBQYvSPRB6I9BiFrZk ECwA== X-Forwarded-Encrypted: i=1; AJvYcCUZ6qy/mnhhTu5mCr7ZJ1VQrdzEBqsX/22C5qdm8WebWs4Kg2uWQRAiCDaK6HKRf04ERtx7FF6jdbc6HBlpvVN4AqY= X-Gm-Message-State: AOJu0YxxpvwiQ4m4SboSoBeoMrEReQZCBYw90CTKbKtx6j+wojWAKFut bab93wDH63ndwiVcic3WZoDVEgv+LijE2egqz9b/Cyu48lPo0mwMGe3oPVXzNrg= X-Google-Smtp-Source: AGHT+IFtueu2hHzThmvc/JKtQbFGEvZGODzMhvBN+JE313AMau+fPRj/Wh1ccpFe+lfa9yfwLLS1Ww== X-Received: by 2002:a05:620a:15bc:b0:788:70b4:159 with SMTP id f28-20020a05620a15bc00b0078870b40159mr4209916qkk.42.1710224279479; Mon, 11 Mar 2024 23:17:59 -0700 (PDT) Received: from n231-228-171.byted.org ([147.160.184.133]) by smtp.gmail.com with ESMTPSA id m18-20020a05620a221200b00787b93d8df1sm3394396qkh.99.2024.03.11.23.17.58 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 11 Mar 2024 23:17:59 -0700 (PDT) From: "Ho-Ren (Jack) Chuang" To: "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" , "Rafael J. Wysocki" , Len Brown , Dan Williams , Vishal Verma , Dave Jiang , Andrew Morton , Jonathan Cameron , Huang Ying , "Ho-Ren (Jack) Chuang" , linux-acpi@vger.kernel.org, linux-kernel@vger.kernel.org, nvdimm@lists.linux.dev, linux-cxl@vger.kernel.org, linux-mm@kvack.org Cc: "Ho-Ren (Jack) Chuang" , "Ho-Ren (Jack) Chuang" , qemu-devel@nongnu.org, Hao Xiang Subject: [PATCH v2 1/1] memory tier: acpi/hmat: create CPUless memory tiers after obtaining HMAT info Date: Tue, 12 Mar 2024 06:17:27 +0000 Message-Id: <20240312061729.1997111-2-horenchuang@bytedance.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20240312061729.1997111-1-horenchuang@bytedance.com> References: <20240312061729.1997111-1-horenchuang@bytedance.com> MIME-Version: 1.0 X-Rspamd-Server: rspam09 X-Rspamd-Queue-Id: 6363D180004 X-Stat-Signature: dm8mspkuwfxkoo346jje4mbuzt7ftf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bi67WWdVAtrsIGwbudSsGFlVzlW1+nqQM737F38jTnIwV4JyhMVHq8XYrbY+/oQ5HJ1nxmOQafjiDU0wtMU7QzSmV654M0P/eWTrVc8dy76VmVXnZGPCS7HUJMJTqp2XPjfIIquyO80Cl8S0Opr5hWd2dPZ0= 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. * Abstract common functions into `find_alloc_memory_type()` Since different memory devices require finding or allocating a memory type, these common steps are abstracted into a single function, `find_alloc_memory_type()`, enhancing code scalability and conciseness. * 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. * Change adist calculation code to use another new lock, mt_perf_lock. 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 `mt_perf_lock` to protect `default_dram_perf`. This approach not only avoids deadlock but also prevents holding a large lock simultaneously. Signed-off-by: Ho-Ren (Jack) Chuang Signed-off-by: Hao Xiang --- drivers/acpi/numa/hmat.c | 11 ++++++ drivers/dax/kmem.c | 13 +------ include/linux/acpi.h | 6 ++++ include/linux/memory-tiers.h | 8 +++++ mm/memory-tiers.c | 70 +++++++++++++++++++++++++++++++++--- 5 files changed, 92 insertions(+), 16 deletions(-) diff --git a/drivers/acpi/numa/hmat.c b/drivers/acpi/numa/hmat.c index d6b85f0f6082..28812ec2c793 100644 --- a/drivers/acpi/numa/hmat.c +++ b/drivers/acpi/numa/hmat.c @@ -38,6 +38,8 @@ static LIST_HEAD(targets); static LIST_HEAD(initiators); static LIST_HEAD(localities); +static LIST_HEAD(hmat_memory_types); + static DEFINE_MUTEX(target_lock); /* @@ -149,6 +151,12 @@ int acpi_get_genport_coordinates(u32 uid, } EXPORT_SYMBOL_NS_GPL(acpi_get_genport_coordinates, CXL); +struct memory_dev_type *hmat_find_alloc_memory_type(int adist) +{ + return find_alloc_memory_type(adist, &hmat_memory_types); +} +EXPORT_SYMBOL_GPL(hmat_find_alloc_memory_type); + static __init void alloc_memory_initiator(unsigned int cpu_pxm) { struct memory_initiator *initiator; @@ -1038,6 +1046,9 @@ static __init int hmat_init(void) if (!hmat_set_default_dram_perf()) register_mt_adistance_algorithm(&hmat_adist_nb); + /* Post-create CPUless memory tiers after getting HMAT info */ + memory_tier_late_init(); + return 0; out_put: hmat_free_structures(); diff --git a/drivers/dax/kmem.c b/drivers/dax/kmem.c index 42ee360cf4e3..aee17ab59f4f 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 = find_alloc_memory_type(adist, &kmem_memory_types); mutex_unlock(&kmem_memory_type_lock); return mtype; diff --git a/include/linux/acpi.h b/include/linux/acpi.h index b7165e52b3c6..3f927ff01f02 100644 --- a/include/linux/acpi.h +++ b/include/linux/acpi.h @@ -434,12 +434,18 @@ int thermal_acpi_critical_trip_temp(struct acpi_device *adev, int *ret_temp); #ifdef CONFIG_ACPI_HMAT int acpi_get_genport_coordinates(u32 uid, struct access_coordinate *coord); +struct memory_dev_type *hmat_find_alloc_memory_type(int adist); #else static inline int acpi_get_genport_coordinates(u32 uid, struct access_coordinate *coord) { return -EOPNOTSUPP; } + +static inline struct memory_dev_type *hmat_find_alloc_memory_type(int adist) +{ + return NULL; +} #endif #ifdef CONFIG_ACPI_NUMA diff --git a/include/linux/memory-tiers.h b/include/linux/memory-tiers.h index 69e781900082..4bc2596c5774 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 *find_alloc_memory_type(int adist, + struct list_head *memory_types); +void memory_tier_late_init(void); #ifdef CONFIG_MIGRATION int next_demotion_node(int node); void node_get_allowed_targets(pg_data_t *pgdat, nodemask_t *targets); @@ -136,5 +139,10 @@ static inline int mt_perf_to_adistance(struct access_coordinate *perf, int *adis { return -EIO; } + +static inline void memory_tier_late_init(void) +{ + +} #endif /* CONFIG_NUMA */ #endif /* _LINUX_MEMORY_TIERS_H */ diff --git a/mm/memory-tiers.c b/mm/memory-tiers.c index 0537664620e5..79f748d60e6f 100644 --- a/mm/memory-tiers.c +++ b/mm/memory-tiers.c @@ -6,6 +6,7 @@ #include #include #include +#include #include "internal.h" @@ -35,6 +36,7 @@ struct node_memory_type_map { }; static DEFINE_MUTEX(memory_tier_lock); +static DEFINE_MUTEX(mt_perf_lock); static LIST_HEAD(memory_tiers); static struct node_memory_type_map node_memory_types[MAX_NUMNODES]; struct memory_dev_type *default_dram_type; @@ -623,6 +625,58 @@ void clear_node_memory_type(int node, struct memory_dev_type *memtype) } EXPORT_SYMBOL_GPL(clear_node_memory_type); +struct memory_dev_type *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(find_alloc_memory_type); + +static void memory_tier_late_create(int node) +{ + struct memory_dev_type *mtype = NULL; + int adist = MEMTIER_ADISTANCE_DRAM; + + mt_calc_adistance(node, &adist); + if (adist != MEMTIER_ADISTANCE_DRAM) { + mtype = hmat_find_alloc_memory_type(adist); + if (!IS_ERR(mtype)) + __init_node_memory_type(node, mtype); + else + pr_err("Failed to allocate a memory type at %s()\n", __func__); + } + + set_node_memory_tier(node); +} + +void memory_tier_late_init(void) +{ + int nid; + + mutex_lock(&memory_tier_lock); + for_each_node_state(nid, N_MEMORY) + if (!node_state(nid, N_CPU)) + memory_tier_late_create(nid); + + establish_demotion_targets(); + mutex_unlock(&memory_tier_lock); +} +EXPORT_SYMBOL_GPL(memory_tier_late_init); + static void dump_hmem_attrs(struct access_coordinate *coord, const char *prefix) { pr_info( @@ -636,7 +690,7 @@ int mt_set_default_dram_perf(int nid, struct access_coordinate *perf, { int rc = 0; - mutex_lock(&memory_tier_lock); + mutex_lock(&mt_perf_lock); if (default_dram_perf_error) { rc = -EIO; goto out; @@ -684,7 +738,7 @@ int mt_set_default_dram_perf(int nid, struct access_coordinate *perf, } out: - mutex_unlock(&memory_tier_lock); + mutex_unlock(&mt_perf_lock); return rc; } @@ -700,7 +754,7 @@ int mt_perf_to_adistance(struct access_coordinate *perf, int *adist) perf->read_bandwidth + perf->write_bandwidth == 0) return -EINVAL; - mutex_lock(&memory_tier_lock); + mutex_lock(&mt_perf_lock); /* * The abstract distance of a memory node is in direct proportion to * its memory latency (read + write) and inversely proportional to its @@ -713,7 +767,7 @@ 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); + mutex_unlock(&mt_perf_lock); return 0; } @@ -836,6 +890,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 when HMAT information is + * available. + */ + continue; + memtier = set_node_memory_tier(node); if (IS_ERR(memtier)) /*