From patchwork Fri Mar 22 07:03:55 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: 13599689 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 03708C47DD9 for ; Fri, 22 Mar 2024 07:04:15 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id F04EC6B0088; Fri, 22 Mar 2024 03:04:12 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id E3F046B0089; Fri, 22 Mar 2024 03:04:12 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id C92016B008A; Fri, 22 Mar 2024 03:04:12 -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 B57636B0088 for ; Fri, 22 Mar 2024 03:04:12 -0400 (EDT) Received: from smtpin27.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id 7C232A1B3D for ; Fri, 22 Mar 2024 07:04:12 +0000 (UTC) X-FDA: 81923785944.27.E398C14 Received: from mail-qk1-f177.google.com (mail-qk1-f177.google.com [209.85.222.177]) by imf05.hostedemail.com (Postfix) with ESMTP id B42FB10001E for ; Fri, 22 Mar 2024 07:04:10 +0000 (UTC) Authentication-Results: imf05.hostedemail.com; dkim=pass header.d=bytedance.com header.s=google header.b=EJ3nTgFp; dmarc=pass (policy=quarantine) header.from=bytedance.com; spf=pass (imf05.hostedemail.com: domain of horenchuang@bytedance.com designates 209.85.222.177 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=1711091050; 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=m4oJ5v+nslG9mPCKKSS3iL6rhlCdTIoZrlCMIeTNwBs=; b=oUexnc9ZWescvHMv4mElQ9/1gBwAxgoNs8t1qDlQ7CpPtBXDjTyRUHwUnFN0WoODRKeu4s H3CysoAH+0AL+7CsguuNA9AWETUzi4zhTu31pebUB4afvMUaSRPR3078lgy6j1WT8d2G82 9ib01HIx0ECQw4lEiR2H/wIoRU5k3kk= ARC-Authentication-Results: i=1; imf05.hostedemail.com; dkim=pass header.d=bytedance.com header.s=google header.b=EJ3nTgFp; dmarc=pass (policy=quarantine) header.from=bytedance.com; spf=pass (imf05.hostedemail.com: domain of horenchuang@bytedance.com designates 209.85.222.177 as permitted sender) smtp.mailfrom=horenchuang@bytedance.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1711091050; a=rsa-sha256; cv=none; b=0uzX8n6+YtqLeYm85uRdFVyGGMeXRUp956hYgtMQ/k2eor5htG7bgnYDqfcHqekxqw3nO2 AvEFkEEG+Ne9cY6aZ24cD6W6o7RPMgpiwKbtb7PQAOywiyBMvekZw3s/GvPcj3+Z5YizOW Xp44SdmN+e7wfC3vcfuzGeXMRcREGfk= Received: by mail-qk1-f177.google.com with SMTP id af79cd13be357-789d76ad270so127295785a.0 for ; Fri, 22 Mar 2024 00:04:10 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance.com; s=google; t=1711091050; x=1711695850; 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=m4oJ5v+nslG9mPCKKSS3iL6rhlCdTIoZrlCMIeTNwBs=; b=EJ3nTgFpWDD3ISq8nS2QnOzALIdgJ+6LclDqgUEHN3rWId4TLoT8rr/B4a1hnWJeSI XGYZROLL0C8W2BupQLHNxJTAsYjiPj4V71MyVp7/m/DUIZQcW/7c35XBzCxykXKVGWmh r+Vfu5kpppOi7PO/ORKPFIeu47SEt5LGQK+bkDRarfkKPa/860jFkg8PejnQI/4A8YE3 CwHpLlpvcewt6KWuO3ZuoyXHGTXAImeoN0Y9HJEIZkhaEE44ANT58L9nYL6jXBt0d5I+ XvHug8PigwUo2FwbM5jBLBVEjrI7VW8saZfeqF02Chi1Or7fYKESuQmg2y26j98L2s8S XjaQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1711091050; x=1711695850; 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=m4oJ5v+nslG9mPCKKSS3iL6rhlCdTIoZrlCMIeTNwBs=; b=exapyWXNNECJON69pdtLHHPY96AGHvIgjWUW+XozTM+u1vpPVTY1k9NJDHTIAmUUaI T99RwgMm6vz2OyGP5+2B7L2msLMVlFZYOXZyuKmiBwBpmC+nfIur0OqK7/yN/HblDAcn polTIiymIFbj/21EqKOZk8h04d4ttLuK2brS2oCXOXSrP1QWPbu60nKklbrSONlnePlI 7mzHoPgy8PpuZXM83AL7ndQ4wOytbsunCEwbHjbC+ML1ZDNv4HnyOmGNme0gqgJL2P3D Q6C+LLKbDLtcsCGaaIeHzXJoMgXzSvKDYIev32Mf8ZyRAZVtPWKne4FZP2ImlvmlK/9S 67Ww== X-Forwarded-Encrypted: i=1; AJvYcCXK6na54MiwRq7mZCBmxUuRoaHREwfUuFyJNtHULW/FNRqDCIrisq7VNvyGOvB0fIAl8ogfHd1/MClyhz6KLSowgbA= X-Gm-Message-State: AOJu0YwCY5yKq6VrxL0EATmiUPqVRHieUwbMLqoFpVvE9i7B5tX6vlsu y7FxeBW2ZtYxA8NGMLVoZuhJqW6ixrvVCDNxdFzRjD88B/U63sJnPzg6qAAJZT4= X-Google-Smtp-Source: AGHT+IG2/2KUjXXnpsB4a3UMqSeeADhWzLgXT2U4SsyBcwgnJiQV2CDihJeZx7ts6cPvHFe1v+lRXg== X-Received: by 2002:a05:620a:1920:b0:788:41c4:4a0 with SMTP id bj32-20020a05620a192000b0078841c404a0mr1828892qkb.32.1711091049736; Fri, 22 Mar 2024 00:04:09 -0700 (PDT) Received: from n231-228-171.byted.org ([130.44.215.108]) by smtp.gmail.com with ESMTPSA id k1-20020ae9f101000000b00789fc794dbesm553974qkg.45.2024.03.22.00.04.09 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 22 Mar 2024 00:04:09 -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 v4 2/2] memory tier: create CPUless memory tiers after obtaining HMAT info Date: Fri, 22 Mar 2024 07:03:55 +0000 Message-Id: <20240322070356.315922-3-horenchuang@bytedance.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20240322070356.315922-1-horenchuang@bytedance.com> References: <20240322070356.315922-1-horenchuang@bytedance.com> MIME-Version: 1.0 X-Rspamd-Queue-Id: B42FB10001E X-Rspam-User: X-Rspamd-Server: rspam04 X-Stat-Signature: umi5q1p3kea1i1b9yoon14pzqn8s9gjx X-HE-Tag: 1711091050-813905 X-HE-Meta: U2FsdGVkX1/MYZ9QGT7lR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Vef8+lxd4IeUj5F2u/ER+kwKhSiEEzlqkp3/ideFSrQy4IfLJDNi2ggIiJYBq82ERjQqa7GOWSf/BGI4ZHYEDkDEQm9S8DlFiW4yuex9CtMCw56lqHKRGeLEYhnftEGvjck9eh8F4MHx/WC9jrxEIfQCykWlP54FXQXnXRCViYg2fhP/p3MdDRxttDd4BQShJ4x9tzJuEWzfwtzJ5GQcsldCEuvsq4osE+dM67bMShOorAz4igKyMwtXHOmIGqK8snoG1UytlK6QF7MmsV6/ZDFDvt0Zz+DlIfka41DSxjXKzsdPCIhcz0chObA5z53IGXzYiOYWo9V/Fx+6vVVAokgH+J4Ss0RF04ila1hB/sEtqAj/fKEK4o/Ef0XU21JujPT3/I= 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 | 73 ++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 63 insertions(+), 10 deletions(-) diff --git a/mm/memory-tiers.c b/mm/memory-tiers.c index 974af10cfdd8..9396330fa162 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,7 @@ static struct demotion_nodes *node_demotion __read_mostly; static BLOCKING_NOTIFIER_HEAD(mt_adistance_algorithms); +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 +511,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; + int adist = MEMTIER_ADISTANCE_DRAM; pg_data_t *pgdat = NODE_DATA(node); @@ -514,11 +521,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"); + } + } - memtype = node_memory_types[node].memtype; - node_set(node, memtype->nodes); - memtier = find_create_memory_tier(memtype); + __init_node_memory_type(node, mtype); + + 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 +671,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 +712,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,7 +760,7 @@ 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; } @@ -732,7 +776,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(&default_dram_perf_lock); /* * The abstract distance of a memory node is in direct proportion to * its memory latency (read + write) and inversely proportional to its @@ -745,7 +789,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(&default_dram_perf_lock); return 0; } @@ -858,7 +902,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 +913,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)) /*