Received: by 2002:ab2:b82:0:b0:1f3:401:3cfb with SMTP id 2csp827910lqh; Thu, 28 Mar 2024 19:20:23 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCWUEhZs3tP/AEUMXtT9443i59GkzxDwp4cicHc70RDF0i9rkPNGbcrH9VVEh5NLelbzuw5OKYFg1/x1Y1Vql4CrRmMkJG0pB7L16M8rVA== X-Google-Smtp-Source: AGHT+IHk3DFX6KzKmid7BGRL3W9E7+pesurIxgIy9rrLpF9/LknzHbMrY+36LK1TvYx3nWkOT1BK X-Received: by 2002:a05:6870:2421:b0:22a:5629:ac2 with SMTP id n33-20020a056870242100b0022a56290ac2mr1071895oap.37.1711678823588; Thu, 28 Mar 2024 19:20:23 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1711678823; cv=pass; d=google.com; s=arc-20160816; b=KgTexIZsCQqkSFaHm37exesDDj0nGBgRFZOTEDBpz3JACdtPIKTLQpNVCPwMio2Oh6 1GEWLERtzSk0uYW4JnyaEWLpsXlgMZeeQmLuz/bNcIaUoKHEOQTr5Idr/YJl6iswJ7Wb 4T1p9SSCgoqf1G/s92YsTLmtOucbQngqFWOL8u0/4qRRBEer56v5PPmpRgIKSPaiHi39 WAYHXiD8mN0CgTejKYR/DrYaNnCcwELFyt5WBbJoaaHHpF1jZPJP/7p3vONsCNWr2D4P ie0I5dG7KUmxv68fCKUQWvMCQEHiQSieW7DF3fa5QJMCf5zoXb6hLhlHrVkLWqc+QcDs 2xxA== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:list-unsubscribe:list-subscribe :list-id:precedence:dkim-signature; bh=Zz+IFMsejvdgnBBQBI80K+Iq8dC3px8n5nwWBHE6dsg=; fh=3DgCRH4rznD6oFvB57hO5EQk2uMIgNp7gZiwRMBKH6E=; b=D1M9ggAN3guHumb4zNAKjvnLCMbdSCI1nc2xV/5XlCvg10X7BySrE/xysIKzh+WMvZ h6LF/3vv8q02EvbWHHK3gpPkTFY5pp0J89Xipo4PAyBpnzFTY0OiqtOd6lfulndAR/4j oXucjPY83UoU87Uaw2/PZZks3Zej9IYEZIkUJM1xaVL2rzCA0pAUw+DfNiqSs28Ji0qs Re3GCZ4zMj3Ps4FhpKb/iR2+Ftk6rndeFAZN2Ou2pkN28UA3DFtNtZ8Wpt1Bw5W7RARy vsDcIhv3O4UpxZN/aoCXvcWjVS9dREsEdnJ6LGSoc5gwnztIITIussql6woCpG9Hz2Uu b+/g==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@bytedance.com header.s=google header.b=POQDwpVH; arc=pass (i=1 spf=pass spfdomain=bytedance.com dkim=pass dkdomain=bytedance.com dmarc=pass fromdomain=bytedance.com); spf=pass (google.com: domain of linux-kernel+bounces-124055-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-124055-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=bytedance.com Return-Path: Received: from sy.mirrors.kernel.org (sy.mirrors.kernel.org. [2604:1380:40f1:3f00::1]) by mx.google.com with ESMTPS id k185-20020a636fc2000000b005d8b8c682cfsi2549795pgc.582.2024.03.28.19.20.23 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 28 Mar 2024 19:20:23 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-124055-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) client-ip=2604:1380:40f1:3f00::1; Authentication-Results: mx.google.com; dkim=pass header.i=@bytedance.com header.s=google header.b=POQDwpVH; arc=pass (i=1 spf=pass spfdomain=bytedance.com dkim=pass dkdomain=bytedance.com dmarc=pass fromdomain=bytedance.com); spf=pass (google.com: domain of linux-kernel+bounces-124055-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-124055-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=bytedance.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sy.mirrors.kernel.org (Postfix) with ESMTPS id 40CEEB21565 for ; Fri, 29 Mar 2024 02:18:52 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 1A8C622F0A; Fri, 29 Mar 2024 02:18:43 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=bytedance.com header.i=@bytedance.com header.b="POQDwpVH" Received: from mail-yb1-f182.google.com (mail-yb1-f182.google.com [209.85.219.182]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 233AE1DA3A for ; Fri, 29 Mar 2024 02:18:38 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.219.182 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711678721; cv=none; b=IWkoVPPyZS4qB7o+nrr313h64l/4svmzbpWLGX+Q2rb4GNWhUNyEyPtn0/wsJKLbOvPcSJHORAKwwPg50iAw3IZHKDLBjypnpfDIQJ7EpTYB6Wbz2m7khdbCwodBSPjgy/628jrOyJUl60YHPWw4+vKtZ7xTAagFnZFL+hrIbs0= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711678721; c=relaxed/simple; bh=ixXvF5fzYYfqG23SeBJ8LQFvVCfKVlJKg2/L7fNBr4g=; h=MIME-Version:References:In-Reply-To:From:Date:Message-ID:Subject: To:Cc:Content-Type; b=bR7UqMf+BECsVFNI3n/znhg3qFpPWN/w4UCZXfHZxkI/RXqIB3s0ORBQoor0CeuTDhPFWiGFcrV9JydQ7//17jlSzSftTce+JfrkDhAYXV7JTzdMgWj6mKxW66gm4oOwzaNSyJ/kdT64a4hIA6j/Uu6G1Mft1mrgMiDadhpokU4= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=bytedance.com; spf=pass smtp.mailfrom=bytedance.com; dkim=pass (2048-bit key) header.d=bytedance.com header.i=@bytedance.com header.b=POQDwpVH; arc=none smtp.client-ip=209.85.219.182 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=bytedance.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=bytedance.com Received: by mail-yb1-f182.google.com with SMTP id 3f1490d57ef6-dcc80d6006aso1521877276.0 for ; Thu, 28 Mar 2024 19:18:38 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance.com; s=google; t=1711678718; x=1712283518; darn=vger.kernel.org; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=Zz+IFMsejvdgnBBQBI80K+Iq8dC3px8n5nwWBHE6dsg=; b=POQDwpVHEr7iOAkFU1ALKMAzRjqnnFFiNuAL2+t4sENsGxbzvuwZOlOzUguxgE88UJ sWiK8UxihdSNT+/lhxOgFWmiBoFzBioYbUuifPMEQXpP7ixNJGKlURpagmVEPE4ATktx uQWWzP5b7SCBXylBvvmRYpgJZPLFBv7k4TthLxOwOGat2D7UV6MCo372MfLs/wEbASNd sf3qZ4Y2v9b3a5X5tCCDB7xf/ft9k06rMHiyBio8J6tkRPmdDh5w80/I/QLvaPsd4vT3 KLD8a+ENsbRNOaKqcz9FNu8OjCpDn/EegkwwrGzaQ02kbCxGIWPcMB16gZ8RFaY7kecW 7EWA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1711678718; x=1712283518; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=Zz+IFMsejvdgnBBQBI80K+Iq8dC3px8n5nwWBHE6dsg=; b=MSz5PIQsQd6muLKwG4MiYwx8qtdMPbhlOQlo9IhxsugtixxMPnE1a3g5XE7KI3REat EogTR6AebEl6gHHCAdvKn51gxW5vMRsQcBasalImkqEGLEaAB73LaNG2JsT/9hSNMQTq MqbZLzcI3dWoCK4skNME737EKPhMQJ9+Z6tlXRG4o3fMiqFC2aXxXVqR6Bna3NYSjOVI jePZTEjr7MkW+eDhVSkm6WTvYNdS2zdB+rwdpGDsTOnCn+LKK5lYLneQawUJRd0XXnao xQOLfGOowvGOUFHtzogg6eSucFuvBwIffAmbmcBEUIFtN/vyLy3DPjehrfil+Tctf/pd BAFQ== X-Forwarded-Encrypted: i=1; AJvYcCWJgK1NiUHiniJ/u/8/rKPFRD0/JAS1ttzdELccFjpf7XYrRweymNyuiIoVIS5OZsNDDFzmu7vF1Wr3EeCMi7mE9EIRfTf8xGmHZfeD X-Gm-Message-State: AOJu0YxR7PrFxIqFTZj0GmoPmmivPF7z+iPfkWDszRg4y9JJ/3kaVFXA W7DxmyFYW6jYgGqfQk5yz+7fyVXhn5A06kUN2FHbn8+NRuo4WI2xP74831bNx677WdqWVHEfViw AW4z7CduUjsxwaNc+U78qpKDk1GMS4wkUbpEHAQ== X-Received: by 2002:a25:9249:0:b0:dcf:66d4:1766 with SMTP id e9-20020a259249000000b00dcf66d41766mr1221293ybo.52.1711678717879; Thu, 28 Mar 2024 19:18:37 -0700 (PDT) Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 References: <20240329004815.195476-1-horenchuang@bytedance.com> <20240329004815.195476-3-horenchuang@bytedance.com> <87a5mhlus5.fsf@yhuang6-desk2.ccr.corp.intel.com> In-Reply-To: <87a5mhlus5.fsf@yhuang6-desk2.ccr.corp.intel.com> From: "Ho-Ren (Jack) Chuang" Date: Thu, 28 Mar 2024 19:18:27 -0700 Message-ID: Subject: Re: [External] Re: [PATCH v8 2/2] memory tier: create CPUless memory tiers after obtaining HMAT info To: "Huang, Ying" Cc: 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, "Ho-Ren (Jack) Chuang" , "Ho-Ren (Jack) Chuang" , qemu-devel@nongnu.org, Hao Xiang Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable On Thu, Mar 28, 2024 at 5:59=E2=80=AFPM Huang, Ying = wrote: > > "Ho-Ren (Jack) Chuang" writes: > > > The current implementation treats emulated memory devices, such as > > CXL1.1 type3 memory, as normal DRAM when they are emulated as normal me= mory > > (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 proc= ess > > 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 bringin= g > > online memory nodes and configuring memory tiers. They should be exclud= ed > > 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 informat= ion. > > If no HMAT is specified, it falls back to using the default DRAM tier. > > > > * Introduce another new lock `default_dram_perf_lock` for adist calcula= tion > > 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()` t= o > > handle cases where memtype is not initialized and where HMAT informatio= n 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 mana= ged, > > 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 =3D 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 =3D default_dram_type; > > + int adist =3D MEMTIER_ADISTANCE_DRAM; > > pg_data_t *pgdat =3D NODE_DATA(node); > > > > > > @@ -514,11 +522,20 @@ static struct memory_tier *set_node_memory_tier(i= nt 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 =3D=3D NULL) { > > + mtype =3D mt_find_alloc_memory_type(adist, &default_memor= y_types); > > + if (IS_ERR(mtype)) { > > + mtype =3D default_dram_type; > > + pr_info("Failed to allocate a memory type. Fall b= ack.\n"); > > + } > > + } > > + > > + __init_node_memory_type(node, mtype); > > > > - memtype =3D node_memory_types[node].memtype; > > - node_set(node, memtype->nodes); > > - memtier =3D find_create_memory_tier(memtype); > > + mtype =3D node_memory_types[node].memtype; > > + node_set(node, mtype->nodes); > > + memtier =3D 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 fo= r > > + * 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) && > > It appears that you didn't notice my comments about this... > > https://lore.kernel.org/linux-mm/87v857kujp.fsf@yhuang6-desk2.ccr.corp.in= tel.com/ > Oops. I misunderstood your meaning. I will then replace -- if (!node_state(nid, N_CPU) && -- node_memory_types[nid].memtype =3D=3D NULL) with ++ if (node_memory_types[nid].memtype =3D=3D NULL)" > > + node_memory_types[nid].memtype =3D=3D NULL) > > + /* > > + * Some device drivers may have initialized memor= y 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 cha= r *prefix) > > { > > pr_info( > > @@ -668,7 +713,7 @@ int mt_set_default_dram_perf(int nid, struct access= _coordinate *perf, > > { > > int rc =3D 0; > > > > - mutex_lock(&memory_tier_lock); > > + mutex_lock(&default_dram_perf_lock); > > if (default_dram_perf_error) { > > rc =3D -EIO; > > goto out; > > @@ -716,23 +761,30 @@ int mt_set_default_dram_perf(int nid, struct acce= ss_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 =3D 0; > > > > - if (default_dram_perf_ref_nid =3D=3D NUMA_NO_NODE) > > - return -ENOENT; > > + mutex_lock(&default_dram_perf_lock); > > + if (default_dram_perf_error) { > > + rc =3D -EIO; > > + goto out; > > + } > > > > if (perf->read_latency + perf->write_latency =3D=3D 0 || > > - perf->read_bandwidth + perf->write_bandwidth =3D=3D 0) > > - return -EINVAL; > > + perf->read_bandwidth + perf->write_bandwidth =3D=3D 0) { > > + rc =3D -EINVAL; > > + goto out; > > + } > > > > - mutex_lock(&memory_tier_lock); > > + if (default_dram_perf_ref_nid =3D=3D NUMA_NO_NODE) { > > + rc =3D -ENOENT; > > + goto out; > > + } > > /* > > * The abstract distance of a memory node is in direct proportion= to > > * its memory latency (read + write) and inversely proportional t= o 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.wri= te_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 adistan= ce > > * than default DRAM tier. > > */ > > - default_dram_type =3D alloc_memory_type(MEMTIER_ADISTANCE_DRAM); > > + default_dram_type =3D 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", __fu= nc__); > > > > @@ -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 nu= ma nodes. > > + * These will be initialized after firmware and d= evices are > > + * initialized. > > + */ > > + continue; > > + > > memtier =3D set_node_memory_tier(node); > > if (IS_ERR(memtier)) > > /* > > -- > Best Regards, > Huang, Ying --=20 Best regards, Ho-Ren (Jack) Chuang =E8=8E=8A=E8=B3=80=E4=BB=BB