From mboxrd@z Thu Jan 1 00:00:00 1970 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 8902FCD1280 for ; Wed, 27 Mar 2024 07:27:39 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 449126B009C; Wed, 27 Mar 2024 03:27:37 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 3F9366B009D; Wed, 27 Mar 2024 03:27:37 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 24C7F6B009E; Wed, 27 Mar 2024 03:27:37 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by kanga.kvack.org (Postfix) with ESMTP id 176D06B009C for ; Wed, 27 Mar 2024 03:27:37 -0400 (EDT) Received: from smtpin03.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id B9898120AD5 for ; Wed, 27 Mar 2024 07:27:36 +0000 (UTC) X-FDA: 81941988912.03.270EFB0 Received: from mail-qt1-f180.google.com (mail-qt1-f180.google.com [209.85.160.180]) by imf11.hostedemail.com (Postfix) with ESMTP id EBD3E40008 for ; Wed, 27 Mar 2024 07:27:34 +0000 (UTC) Authentication-Results: imf11.hostedemail.com; dkim=pass header.d=bytedance.com header.s=google header.b=I9nObt2w; dmarc=pass (policy=quarantine) header.from=bytedance.com; spf=pass (imf11.hostedemail.com: domain of horenchuang@bytedance.com designates 209.85.160.180 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=1711524455; 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=hoi2UuTOqcZlOQM8FxGyuRaBX9j/Jb0dGURfbchHGSo=; b=Ad6eBfDtQKuL+OJdK6it7SUZ4BGTIQsejZJuUisyew+gZBLGu/m+8Ldgf3JmDjRyXEp42E N7nzMcJC7MJz15PsFEa87vyFMPBJTimzeOtrkX+27qDT+1zr3a5nvggTWa9KNT8Fbm55O0 eiRoAae0CLYXsZcGZs3inMNMiNbF47E= ARC-Authentication-Results: i=1; imf11.hostedemail.com; dkim=pass header.d=bytedance.com header.s=google header.b=I9nObt2w; dmarc=pass (policy=quarantine) header.from=bytedance.com; spf=pass (imf11.hostedemail.com: domain of horenchuang@bytedance.com designates 209.85.160.180 as permitted sender) smtp.mailfrom=horenchuang@bytedance.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1711524455; a=rsa-sha256; cv=none; b=hBc4wB5ceHirz10xi/traInd/tEZlBZmWHtN9oy8J9cXKGy+69WeJ57EF/epIOA/XnMwC0 x/e1SjFb6RsZaf3XjvGhpccXeWNyP3nLqLcgeu8RqUK2XdiVOP1zXHtWu/62KhoV3q+ujw smMu0JzvnCBFx5/rOlV28K8MBk+1A2U= Received: by mail-qt1-f180.google.com with SMTP id d75a77b69052e-4317fbb669cso1899041cf.2 for ; Wed, 27 Mar 2024 00:27:34 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance.com; s=google; t=1711524454; x=1712129254; 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=hoi2UuTOqcZlOQM8FxGyuRaBX9j/Jb0dGURfbchHGSo=; b=I9nObt2wNXCc62cPZA0K3GsOvtLezszA6VGjq1uDHVPR+kiIOVGS36gZX5FXxIPWP3 rSIrRudz+aWg9l2mm1rpY5Rum0mzseEpgq1iPAculNmSjNgU6bhFzyO1CFrWpbNDkgAQ iXjf1FXT/lbbrtYUeK4nG+1vKD/OpNnE8b3szGbxl1Lk5+mn7iu6q9T+lUShkv+WppV7 utWeVRHNl/pz9iElsfYle6/e3xRw87/3kG78pCK7zTSqAB6YT5ONRZAt7EH7q80qxR5H nnAX+ktpvT0IJcCm35ZMWAEuMC75N95fLdftDGn+wql31kbibZ1hzEv9VEtTR78hQfmd aO5A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1711524454; x=1712129254; 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=hoi2UuTOqcZlOQM8FxGyuRaBX9j/Jb0dGURfbchHGSo=; b=LHWVWAzJmyiblaGuQWe3nlq/f1iw3xseKERCAJc9CROIK9/guWaX6nhNrwuyOgfZtl 86RgRbA8s9BH2BxPlyUOCIPXZ0rDlAvXyCb26IHicNMiOFhdwI0A+cJumtA1T/kVUzkZ yBjN0cQzJPijWy/2u0rqngUdPU779xGFGw4Bl1APvHOY20b0Pz9n4DSL1zGvx9myVSoP nUSIaGJtcCUzg7W390DL+wLi2BBM39AZ1agAb8/h3KPXwxvRExFX/Mw1TjgghpcIQWKe RdESxroVwJCEaDI05+Tsp9SEVkjAsLRyoAyGdA0dPTLIQ4Fb7N7a7Ff1RVYgPJpLEKF7 05eg== X-Forwarded-Encrypted: i=1; AJvYcCWV0fYNxRQfxM55vsr+JSeHTm2pnZGd6qfRX3oMWwUpd6hCUN7yk8mxDaM2p4UckN1dXJVR9t/WB6m0hgoZ/TdFI9M= X-Gm-Message-State: AOJu0Yyxwdg41ehBz/kLN6r++vuLnr8SaWPO87d6K7K5kC6c3cuLdm27 gLiudbJqCDs/asP1TJoZo7L6x4Pes1YtV6zApETNhycib96CgTFylAMSI7jucpM= X-Google-Smtp-Source: AGHT+IGjktT33ln7PjMJGaA8Upa0mPJv9Hg7AL6FK+l/CQOxYgTZ12tbYI58cje8dW1fEU1o5EY4og== X-Received: by 2002:ac8:5bc6:0:b0:431:7ed1:4d2e with SMTP id b6-20020ac85bc6000000b004317ed14d2emr1018982qtb.68.1711524454053; Wed, 27 Mar 2024 00:27:34 -0700 (PDT) Received: from n231-228-171.byted.org ([130.44.212.102]) by smtp.gmail.com with ESMTPSA id bq15-20020a05622a1c0f00b00431604af3c1sm2383555qtb.20.2024.03.27.00.27.33 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 27 Mar 2024 00:27:33 -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 v6 2/2] memory tier: create CPUless memory tiers after obtaining HMAT info Date: Wed, 27 Mar 2024 07:27:28 +0000 Message-Id: <20240327072729.3381685-3-horenchuang@bytedance.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20240327072729.3381685-1-horenchuang@bytedance.com> References: <20240327072729.3381685-1-horenchuang@bytedance.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Rspam-User: X-Stat-Signature: wtb7a1f4pufbo6tekdfdft561df5cc1d X-Rspamd-Server: rspam07 X-Rspamd-Queue-Id: EBD3E40008 X-HE-Tag: 1711524454-909354 X-HE-Meta: U2FsdGVkX19tCF6OMTdNWskkArA9UnxYeerkuyZEOhmYN7c5jN31Llw3YboiirHOnwQS4OQ/yDI/rWnAmRGGfbKulsIbHeI70t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gBi1Gx5c4en42jeXeOqTREqZ9pOlgvLkufEqmT6N75WGydZK40cLKieWXf8DXF8sEarkyNSji3CHqx0exWCbFETZVoX9YXuyOKmhKO0wT4SjI/IO0mGHIHFRt5QbYW35rDJDf54tzEe79R1GYnGEgJSfMTJJeI0twDxmN0HLa8zZAQyw0SGTvqk2GC2tKDmb0TjvRe8t6V0G165rqWp1Rex0xGnWPZnlTXW1I8Q8kxv/S44bMG7rNUzdg4XGnHurWJdm2BrYyhTojo7nlMS+hXlvWCVYZ8NvC/+Vl/Cu5iYi1iH/nHJWcf+c= 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 --- 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)) /* -- Ho-Ren (Jack) Chuang