From patchwork Fri Mar 29 00:48:14 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: 13609828 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 B4B43CD128A for ; Fri, 29 Mar 2024 00:48:25 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 4AB426B008C; Thu, 28 Mar 2024 20:48:24 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 45C1A6B0092; Thu, 28 Mar 2024 20:48:24 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 2AE036B0093; Thu, 28 Mar 2024 20:48:24 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id 0A6DF6B008C for ; Thu, 28 Mar 2024 20:48:24 -0400 (EDT) Received: from smtpin03.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id CC670C0FB2 for ; Fri, 29 Mar 2024 00:48:23 +0000 (UTC) X-FDA: 81948240486.03.E873A53 Received: from mail-qt1-f174.google.com (mail-qt1-f174.google.com [209.85.160.174]) by imf04.hostedemail.com (Postfix) with ESMTP id 04DDF4000E for ; Fri, 29 Mar 2024 00:48:21 +0000 (UTC) Authentication-Results: imf04.hostedemail.com; dkim=pass header.d=bytedance.com header.s=google header.b=VG4Y5Iko; dmarc=pass (policy=quarantine) header.from=bytedance.com; spf=pass (imf04.hostedemail.com: domain of horenchuang@bytedance.com designates 209.85.160.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=1711673302; 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=0xgOS8isMxKHzHyotyIcBuD4wYWux/bS/U8DbDxoJU0=; b=S5HG+mUeWXpZ1FnGe13BoA2Ffav/JM1hd41coK+H907w1ibUzR8Tf4oEp/ekFyI0jD5y1y Q8UAkDJIpY4uAdwk84N9jR0lhsKzT5ult0PwClxQyWS7TBAN1C9j1Mwsr21DR+zBp7Gp1N xYpGlPulambiY2hWuujIdNCI19kSrzA= ARC-Authentication-Results: i=1; imf04.hostedemail.com; dkim=pass header.d=bytedance.com header.s=google header.b=VG4Y5Iko; dmarc=pass (policy=quarantine) header.from=bytedance.com; spf=pass (imf04.hostedemail.com: domain of horenchuang@bytedance.com designates 209.85.160.174 as permitted sender) smtp.mailfrom=horenchuang@bytedance.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1711673302; a=rsa-sha256; cv=none; b=0eQkeIRE7PiFdkserVjIfcb2lLy4Zrst/h6JZzA0XIua4pojD5LqjJvQU8f29RoRG1QReQ ddzPkQ6awjRzAx4oL2qLcXlTrPK09sH488RDbTSIG5Nmxb8XTAlnCCslOIrhQ2vyj30YS8 I3KLIukD3FdZWG5maAj9c9CyHCWE77s= Received: by mail-qt1-f174.google.com with SMTP id d75a77b69052e-430c63d4da9so9471351cf.0 for ; Thu, 28 Mar 2024 17:48:21 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance.com; s=google; t=1711673301; x=1712278101; 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=0xgOS8isMxKHzHyotyIcBuD4wYWux/bS/U8DbDxoJU0=; b=VG4Y5IkoeM7LLqOyTmop7mkC1CMoHgW3e/ZSioWCOdNGMUVLEYXbCz46aTar1TkF/A +d4OlA6unjb2pqHF9yLVMp11ZxXYmeaPPGTc73PIBMJHIOXP6hozXorRxjNRbSqpR1UL dLJfn034zE/CMyGa5YtqhCoDojZCcwCyL+AKI31kWs7gQh17bri4USWirYKANm3hcAuy X5fHfBZxJL9LLm6tp485jIawwosmnPrSZK7XpFAjfSbgsnnc1+iKopSObaKNqA9NFK6x nmZ/LBQXOyrivqkQFgU43AQANWY0j/9ng8us7y+hX4w3LW3Hdzjkng0OROsEDN2z9HiR rB6Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1711673301; x=1712278101; 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=0xgOS8isMxKHzHyotyIcBuD4wYWux/bS/U8DbDxoJU0=; b=X3dUToYBDB5gE6RY97at1LADnO7fk1yWXV2xGcTG6ihoFo3C2bZ/aec8bAdKG+kb38 Fkru/jhivI69ZWh6bM0vMwYGQpBR1/olpakmF54EV9T0Io4vEs+8+dVL5XUgimGVXxE1 FKG7EIoMWavJfGFraYI4iDqQyOIvutix4Q8/Io4aWK3gEkuMKQui0hCtAj1kbbn+84PK FxlHNcHVjbI3k/Ps7hP0a1/KiHqIfqCRAppmWmFXe9F2ODHATM8t333MaCeLJKVyACmd xEXkeqvwHe3wJp/yXh6X8+YH8ecqelP4I8GgHXfZmWo9faCSZEtby5jWXmyPg33cDYAV jm0w== X-Forwarded-Encrypted: i=1; AJvYcCX+/1LrqniLBjhOSYEJ+Rt0y8YdKhd1RpONfhts/vVYtHviQxs/KdbwSL1PsfV00wW9M4iRTGtRvaitgTyUsXmmCF4= X-Gm-Message-State: AOJu0YzYQ+g58+i0tFaJ3aJjUTLesWk3xB/I2iwlReAftjz8Zxn7TxSl IEOpATuWE42hzH/dX17RmTmGKhXrGFi7XxnzWEmgV+ZE8kPdqcfTWoYWLlIAico= X-Google-Smtp-Source: AGHT+IGRQkIbG4fv+uoIg3/nxd1eFfIFzArVzfxtSMxfeYkTUQ5hMztZBEvA8x2mPlQTJlfj6KU89Q== X-Received: by 2002:a05:622a:1492:b0:432:b84b:ac1 with SMTP id t18-20020a05622a149200b00432b84b0ac1mr1357113qtx.19.1711673301149; Thu, 28 Mar 2024 17:48:21 -0700 (PDT) Received: from n231-228-171.byted.org ([130.44.212.125]) by smtp.gmail.com with ESMTPSA id v26-20020ac8749a000000b00430afbb6b50sm1102414qtq.51.2024.03.28.17.48.20 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 28 Mar 2024 17:48:20 -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 v8 2/2] memory tier: create CPUless memory tiers after obtaining HMAT info Date: Fri, 29 Mar 2024 00:48:14 +0000 Message-Id: <20240329004815.195476-3-horenchuang@bytedance.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20240329004815.195476-1-horenchuang@bytedance.com> References: <20240329004815.195476-1-horenchuang@bytedance.com> MIME-Version: 1.0 X-Rspamd-Queue-Id: 04DDF4000E X-Rspam-User: X-Rspamd-Server: rspam02 X-Stat-Signature: ci11nd418o5aaz5ba4uixskg5pc1e946 X-HE-Tag: 1711673301-6758 X-HE-Meta: U2FsdGVkX190GFvn+pCZhwkPEuEZ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xy0tQvaTnJdLWxAfV35H9TPApQ0kU36wbQx5ZuWI0WYywycVf40EpsPSckN/zeXF2QGLc1VFhWgPN2lJDdPlQq5phY2YgEcBVi/Wk+leGNmZklkMVN2V21jLJGjEtP4eKQTfWb7Xcj03VVU5oEsZLDtDP32onxC1jaU5EDZo5sFnAyS4C1zYjhth4IJqaG5XTMy592mksh8fwjC3h4LtezJWQznoC78euOBSlVcMzCaNx68hUm5nVRl/K6eHyLIdN+jFG/nf8yGyIQRK5nDSECUSAaFLvEfXLCf2fCHGAxsXEDQFuhjWhiV+BntBtUP9PMAjKEnO9WPUFLaSzAjdv0lDfYeAws7rEsNFg0dcxZR0gSsIQ7c91M0ViGiRfdN3niIjjzCmFO1fq07 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 | 94 +++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 78 insertions(+), 16 deletions(-) diff --git a/mm/memory-tiers.c b/mm/memory-tiers.c index 974af10cfdd8..e24fc3bebae4 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,34 @@ 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_state(nid, N_CPU) && + 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 +713,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 +761,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 +797,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 +911,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 +922,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)) /*