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 7F7F9C48BF6 for ; Mon, 4 Mar 2024 02:42:28 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 152C86B009E; Sun, 3 Mar 2024 21:42:28 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 0DCBB6B009F; Sun, 3 Mar 2024 21:42:28 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id E98AD6B00A0; Sun, 3 Mar 2024 21:42:27 -0500 (EST) 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 D71466B009E for ; Sun, 3 Mar 2024 21:42:27 -0500 (EST) Received: from smtpin11.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id B2B19A07EF for ; Mon, 4 Mar 2024 02:42:27 +0000 (UTC) X-FDA: 81857807934.11.6DD8054 Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.9]) by imf17.hostedemail.com (Postfix) with ESMTP id 8B01440018 for ; Mon, 4 Mar 2024 02:42:24 +0000 (UTC) Authentication-Results: imf17.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=cApFg9d4; spf=pass (imf17.hostedemail.com: domain of ying.huang@intel.com designates 192.198.163.9 as permitted sender) smtp.mailfrom=ying.huang@intel.com; dmarc=pass (policy=none) header.from=intel.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1709520146; 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-type:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=A3k3XnZT4HF4OP1IkouPcb3dGVYBkiyNewPccUHdwns=; b=ujsWho0qbfGAGoUxHkGxs41tcj9yVaMKpTvG7DYbSssUSLvCUCtCnPnubnYfglGNRSpoDM 4Sjc3f1uQ2bL78VDlE+xOpn2xdqf4rIsx8yzC0019JDqx6aghnXJsiCmUsE+PcA/9SSCx4 cbyO6jqH6n9THkc6lydQ5j1VRG/QjbM= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1709520146; a=rsa-sha256; cv=none; b=GGLm8OVzDY2t6lrlWY4VVus3qoJ7mocnJ1YiES0m/TyB+4JdeK8L9dTp5sxNSvmiYcbS2y yrXAdklD3ncLQtx+3xnTnFGSdKmGpwSbN4vilez6Y2yPhkNJAJrr0khsh0mXCVahdxV9yP 4TBo/cyaEL/qVsU+5ZIAkkv1BgFa/Nc= ARC-Authentication-Results: i=1; imf17.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=cApFg9d4; spf=pass (imf17.hostedemail.com: domain of ying.huang@intel.com designates 192.198.163.9 as permitted sender) smtp.mailfrom=ying.huang@intel.com; dmarc=pass (policy=none) header.from=intel.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1709520144; x=1741056144; h=from:to:cc:subject:in-reply-to:references:date: message-id:mime-version; bh=p3rlpauy9CcC/96wXB/B3OYstONDXCGXsyOYQKXvKOw=; b=cApFg9d4+1i3N/vlL9jEVD3J+i7Nu452oKsM87d+gBAVU7LFOnHddpUG MRPO8VuDe8TIQoTsMMTQ5DibnoqvEAkhesA8f1ypCM6LksgkanWN0oIC+ zXIohkE0aJiu1EjRf54KzFDOVKHTEL7AtOiulKBXIBrnocDhvhUwi5nx0 /cmeiwXh54MzlIHyapsxWgCrlMmBsNAdvAGcUqryJqTDyhnxrCj+dWIJQ 7DOLan2QF/9bU66RPitWFrvdFF0ECWQVhLcUYR7GIpNQYrap0MvDF62TY RU6jL7I2Vh1Vv/R3gXCj0cLRzecEcFsRq9MNqny1CDJDlBU4cmQZVZjWt Q==; X-IronPort-AV: E=McAfee;i="6600,9927,11002"; a="14706606" X-IronPort-AV: E=Sophos;i="6.06,203,1705392000"; d="scan'208";a="14706606" Received: from fmviesa001.fm.intel.com ([10.60.135.141]) by fmvoesa103.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 03 Mar 2024 18:42:22 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.06,203,1705392000"; d="scan'208";a="39839001" Received: from yhuang6-desk2.sh.intel.com (HELO yhuang6-desk2.ccr.corp.intel.com) ([10.238.208.55]) by smtpauth.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 03 Mar 2024 18:42:16 -0800 From: "Huang, Ying" To: "Ho-Ren (Jack) Chuang" Cc: "Hao Xiang" , "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 , Andrew Morton , Dave Jiang , Dan Williams , Jonathan Cameron , linux-acpi@vger.kernel.org, linux-kernel@vger.kernel.org, linux-mm@kvack.org, "Ho-Ren (Jack) Chuang" , "Ho-Ren (Jack) Chuang" , linux-cxl@vger.kernel.org, qemu-devel@nongnu.org Subject: Re: [PATCH v1 1/1] memory tier: acpi/hmat: create CPUless memory tiers after obtaining HMAT info In-Reply-To: <20240301082248.3456086-2-horenchuang@bytedance.com> (Ho-Ren Chuang's message of "Fri, 1 Mar 2024 08:22:45 +0000") References: <20240301082248.3456086-1-horenchuang@bytedance.com> <20240301082248.3456086-2-horenchuang@bytedance.com> Date: Mon, 04 Mar 2024 10:40:22 +0800 Message-ID: <87jzmibtyh.fsf@yhuang6-desk2.ccr.corp.intel.com> User-Agent: Gnus/5.13 (Gnus v5.13) MIME-Version: 1.0 Content-Type: text/plain; charset=ascii X-Rspamd-Queue-Id: 8B01440018 X-Rspam-User: X-Stat-Signature: kjkap1n3ennkcn78a34m84dn8cgdxtfd X-Rspamd-Server: rspam03 X-HE-Tag: 1709520144-9179 X-HE-Meta: U2FsdGVkX1+aqWpIf4kuSS9Hw4GgLitbSUcClls42xKi+xzDzxpTmphgloEVKwf83/l6j17bV5p3wdJlcDgbgKGPXmtMPp7FQ64W16+DuL7wIDsMNMPAH8Pjxv2K1y9GoAbICda++zRJGjupZ9B0pchPbC3lcaRsX5z3vB2sIje+QmWPOESJUxUvvlNj3TLITLvb2PKKgU7R6vrZSOqp0bxfmhmhlU3FJ03eRP1F6nJETts1nG9uAUQuPg19CLD0gPfhSapD3h1g7Bl4wzqv3idzlx35dPGCxXIrJoyQdcBmiVnXFyq0MrOtPOucrEu3bFlGXPKNyyMgEkMJiiXZkMLIytIHTZR5Sz31tQbdCiFT5BGQJn0MD1wyxa0H+EmA+XwcTLmvVhux86ODC5Z6OroVDx/Ardp/gW++UKprhZkX+oxPRcUvH8n6wX6PwBJ41UZVX3fLsEuhYKB/+4VXMugcsFhFO/YpyAVYHswEKPiC0pKMhdv0cxQaw0QqfwDy8Y9AnWlLC3ToVcL1lLKaPogBsdd5sCs+IowJD2gvMGrml6RloY7A/C1hpjLqH++ns/i7y90x/R66yu2fzDHuTtKXAbriJUNQ6n7sdCNjHDWWPOOWX5612X2FkAPX4yIzLYbzH2eQIVOIgFvFSnc72Wsiasc1kJ8Tzw65hzFr3sLmq98Oqp3UeqFbKDcJdBLQ6WLmJDuObfIHOkuPK+FfeOadw6oAPUYKK2BKUAtpJw700pI7us759eF2A6bpWiVMNDuqdfSeALX+fSyCzM0XAIgVCg1FM9G1VlI7TjR/+aHKwGGh5eqmGi4wZzKmxPBWzARvOdV/sZyAZZ6s6OWrD/a7dhgKZjVn99YPxZ/XdW9fwJ9sOmEqLbeT4kWRGxldxMuaGHqyh6aICpubqK60vGnyVn/r/J9W22cztOrwdGk= 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: Hi, Jack, "Ho-Ren (Jack) Chuang" writes: > * Introduce `mt_init_with_hmat()` > We defer memory tier initialization for those CPUless NUMA nodes > until acquiring HMAT info. `mt_init_with_hmat()` is introduced to > post-create CPUless memory tiers after obtaining HMAT info. > It iterates through each CPUless memory node, creating memory tiers if > necessary. Finally, it calculates demotion tables again at the end. > > * Introduce `hmat_find_alloc_memory_type()` > Find or allocate a memory type in the `hmat_memory_types` list. > > * Make `set_node_memory_tier()` more generic > This function can also be used for setting other memory types for a node. > To do so, a new argument is added to specify a memory type. > > * Handle cases where there is no HMAT when creating memory tiers > If no HMAT is specified, it falls back to using `default_dram_type`. > > * Change adist calculation code to use another new lock, mt_perf_lock. > 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. The patch description is used to described why we need the change, and how we do that, but not what we do. People can tell what is done from the code itself. > Signed-off-by: Ho-Ren (Jack) Chuang > Signed-off-by: Hao Xiang > --- > drivers/acpi/numa/hmat.c | 3 ++ > include/linux/memory-tiers.h | 6 +++ > mm/memory-tiers.c | 76 ++++++++++++++++++++++++++++++++---- > 3 files changed, 77 insertions(+), 8 deletions(-) > > diff --git a/drivers/acpi/numa/hmat.c b/drivers/acpi/numa/hmat.c > index d6b85f0f6082..9f57338b3cb5 100644 > --- a/drivers/acpi/numa/hmat.c > +++ b/drivers/acpi/numa/hmat.c > @@ -1038,6 +1038,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 */ > + mt_init_with_hmat(); > + > return 0; > out_put: > hmat_free_structures(); > diff --git a/include/linux/memory-tiers.h b/include/linux/memory-tiers.h > index 69e781900082..2f845e90c033 100644 > --- a/include/linux/memory-tiers.h > +++ b/include/linux/memory-tiers.h > @@ -48,6 +48,7 @@ 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); > +void mt_init_with_hmat(void); HMAT isn't universally available. It's a driver in fact. So, don't put driver specific code in general code. > #ifdef CONFIG_MIGRATION > int next_demotion_node(int node); > void node_get_allowed_targets(pg_data_t *pgdat, nodemask_t *targets); > @@ -136,5 +137,10 @@ static inline int mt_perf_to_adistance(struct access_coordinate *perf, int *adis > { > return -EIO; > } > + > +static inline void mt_init_with_hmat(void) > +{ > + > +} > #endif /* CONFIG_NUMA */ > #endif /* _LINUX_MEMORY_TIERS_H */ > diff --git a/mm/memory-tiers.c b/mm/memory-tiers.c > index 0537664620e5..7a0a579b3deb 100644 > --- a/mm/memory-tiers.c > +++ b/mm/memory-tiers.c > @@ -35,7 +35,9 @@ struct node_memory_type_map { > }; > > static DEFINE_MUTEX(memory_tier_lock); > +static DEFINE_MUTEX(mt_perf_lock); > static LIST_HEAD(memory_tiers); > +static LIST_HEAD(hmat_memory_types); > static struct node_memory_type_map node_memory_types[MAX_NUMNODES]; > struct memory_dev_type *default_dram_type; > > @@ -502,7 +504,7 @@ static inline void __init_node_memory_type(int node, struct memory_dev_type *mem > } > } > > -static struct memory_tier *set_node_memory_tier(int node) > +static struct memory_tier *set_node_memory_tier(int node, struct memory_dev_type *new_memtype) No. memory_dev_type are passed to the function via node_memory_types[node].memtype. > { > struct memory_tier *memtier; > struct memory_dev_type *memtype; > @@ -514,7 +516,7 @@ 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); > + __init_node_memory_type(node, new_memtype); > > memtype = node_memory_types[node].memtype; > node_set(node, memtype->nodes); > @@ -623,6 +625,56 @@ void clear_node_memory_type(int node, struct memory_dev_type *memtype) > } > EXPORT_SYMBOL_GPL(clear_node_memory_type); > > +static struct memory_dev_type *hmat_find_alloc_memory_type(int adist) Similar function existed in drivers/dax/kmem.c. Please abstract them and move them here. > +{ > + bool found = false; > + struct memory_dev_type *mtype; > + > + list_for_each_entry(mtype, &hmat_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, &hmat_memory_types); > + } > + return mtype; > +} > + > +static void mt_create_with_hmat(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)) > + pr_err("%s() failed to allocate a tier\n", __func__); > + } else { > + mtype = default_dram_type; > + } > + > + set_node_memory_tier(node, mtype); > +} > + > +void mt_init_with_hmat(void) > +{ > + int nid; > + > + mutex_lock(&memory_tier_lock); > + for_each_node_state(nid, N_MEMORY) > + if (!node_state(nid, N_CPU)) > + mt_create_with_hmat(nid); > + > + establish_demotion_targets(); > + mutex_unlock(&memory_tier_lock); > +} > +EXPORT_SYMBOL_GPL(mt_init_with_hmat); > + I guess that we can put most hmat related code above in hmat.c. > static void dump_hmem_attrs(struct access_coordinate *coord, const char *prefix) > { > pr_info( > @@ -636,7 +688,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 +736,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 +752,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 +765,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; > } > @@ -797,7 +849,7 @@ static int __meminit memtier_hotplug_callback(struct notifier_block *self, > break; > case MEM_ONLINE: > mutex_lock(&memory_tier_lock); > - memtier = set_node_memory_tier(arg->status_change_nid); > + memtier = set_node_memory_tier(arg->status_change_nid, default_dram_type); > if (!IS_ERR(memtier)) > establish_demotion_targets(); > mutex_unlock(&memory_tier_lock); > @@ -836,7 +888,15 @@ static int __init memory_tier_init(void) > * types assigned. > */ > for_each_node_state(node, N_MEMORY) { > - memtier = set_node_memory_tier(node); > + 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, default_dram_type); On system with HMAT, how to fall back CPU-less node to default_dram_type? I found your description, but I don't find it in code. > if (IS_ERR(memtier)) > /* > * Continue with memtiers we are able to setup -- Best Regards, Huang, Ying