From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-oi1-f180.google.com (mail-oi1-f180.google.com [209.85.167.180]) (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 7A6602931CD for ; Tue, 14 Jul 2026 19:20:10 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.167.180 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1784056811; cv=none; b=AFQ42ZLkKl6RTKkRGxGxrcU4tW5VWpe6mCLskzCZPjtyweTMeH0X1nk6bP0haf6N02T7VLusCGKGkfhhzxkoHx0uw4gfz5+G0nkwfH0dmDhUzWUQl7Br41FUShNhNgYdRSfFeD/JfawC8KMkkDRiVS2cF5ItSjToPCSa7UvbWfg= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1784056811; c=relaxed/simple; bh=UQ3VqloxTtjHddxhoyr8yuq6mpk+BKnqm0uwK0fj8Rs=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=DrLDnkKm1XWd6bQAgXyC1M3JJYngQmINEkl4czcxUotd/Gfum38DWVtaDOMsIc2ukm0cs4ow1DLzE5VT0lWpsLM3p7IAZ8f4Jn2fxBrvJ0IsR0MloCjbAD7mQBoBXaPJk/l5M9qongPtX1UiChZzeM/3P/2A31HicK1diZRLzi8= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=mOpFrFaP; arc=none smtp.client-ip=209.85.167.180 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="mOpFrFaP" Received: by mail-oi1-f180.google.com with SMTP id 5614622812f47-4a466f81a24so1215504b6e.2 for ; Tue, 14 Jul 2026 12:20:10 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1784056809; x=1784661609; darn=vger.kernel.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:content-type; bh=kpEjCH9LBPCggUHvnEgL+T6kr5Wgb5W3kwCbUpos+tg=; b=mOpFrFaPPn5YgQ/w2I44MPCSDgT3CD39cNGabh/KOSq4njB7rkbIwIR5+FpmON2XKV qWUCBCHQ5Djheyozir8skF6n7ALkvIq8BOuU0XpuU3gR4fMxwfRs6nYi6x/p9vlIIEuU wNdjEUAmu+PosP+hkljkJ6v/VwCDQD3E7+cX6fl5gQniuN2BoKDOAVDYfStYnCSGspDH uqXauu3P6RXBYq+ouJN9wzeFBzyN4F6oMhZhY6qo0U5hLR6WJePRs7UQbi0BhHuDxpBG Av+TS2hC6Fiey0q3i+O+OVlUtvrB9zJAygFpdhFSuIQG0ju/m3uUQMcKcBOuN2JaNvlW ctkA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1784056809; x=1784661609; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to:content-type; bh=kpEjCH9LBPCggUHvnEgL+T6kr5Wgb5W3kwCbUpos+tg=; b=OTPZnyf6UwCNrkA+Xze74unKNia4dDh7sQZuR6HEfWc4CsWKtIzOFwNSfMOZC2Txds ovgQoBgQn3uLRF5yfJlQR3/FIlGvgeJaNd12wzpuTBVdUMKjtAcn9gOjbtuutWe+JCdW 1Ybr9TjZJbG7XTyd+nLH8pDfd2OtvFY7rm+XvWiqA7VTxYLrQ0bG8cxFz0Ghn8GGFJw9 d7d6CBg5emY/k9oHZHWwH2tzDaOdFsFRaqsH8L5rueuJzqOnenVa9eMaYo96e4BNryOK 13QQWbgFlK0oT97z5GifgLjYaF/eTFpe4a5SlU68aeWXlrraL7qQYxN02aDrwVrh/vuf Hu+w== X-Forwarded-Encrypted: i=1; AFNElJ9MfZTiBMV1txB1DBbR03EhFehekSdHddnglY8FAkBx8Jke4bJ9xzglSX5UAbGJ5JqGX9QyKcTmsSiukpg=@vger.kernel.org X-Gm-Message-State: AOJu0YyEr6cjXjjQCaP6PyKhQykRUryt7y1iRIwCMKgn8RvX+vInYZJO 7VDD6xNczrQkRaIXohu5Y4QEtDDE4hCdk69ARsdwl9L/LELrZSHhvrU+ X-Gm-Gg: AfdE7ckEdydeHoOV2eN8Kpt0gy642Yb0vGYFS1gkh59uzB1mNTwnqiyTGuT1u8otnVR H2vU0b47sg+N8tO3zr13BbDZMWv5RpWZeh9fGHd1esAQheKzP16gArTHhX7Shyw9NaBy/Q4/9bj 0HGvSdc8+TStgM3ZO+EA5o89zw8Kuj64sLZKfWKS6TRnTmgcFrPpGkSv1G1H518G4TlAPYPMJuP d5xcVHW6HGQQYoxAI6FDIldic8M98QnVijTS1jOYo2lD/FPgUBhP5qy6XwIocwn3oFyRljm0n1v Ine9NNYjseaDkyCQ/99b55ECz6gy5rmQFj1OmtC1dOgdSXWF+EqxHHYXqZP7C5Rv3ecu6ys4tLc DYK348SahmxtjR29DQHzXlLey/nTUerfZkRFamXRKLttw3sUvkLFZxje6txsPOSxEFiozVeKTjn qiMaoYaSylezkiiiuCoHz5yUggge5LgpaaDypzOgNNKCemIksuK5HiKg== X-Received: by 2002:a05:6808:1b21:b0:489:a387:1e29 with SMTP id 5614622812f47-4a47a5e2f3fmr2017784b6e.20.1784056809226; Tue, 14 Jul 2026 12:20:09 -0700 (PDT) Received: from localhost ([2a03:2880:10ff:5a::]) by smtp.gmail.com with ESMTPSA id 5614622812f47-4a1acc82489sm15100591b6e.1.2026.07.14.12.20.08 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 14 Jul 2026 12:20:08 -0700 (PDT) From: Joshua Hahn To: Ritesh Harjani Cc: linux-mm@kvack.org, Andrew Morton , David Hildenbrand , Lorenzo Stoakes , "Liam R. Howlett" , Vlastimil Babka , Mike Rapoport , Suren Baghdasaryan , Michal Hocko , linux-kernel@vger.kernel.org, kernel-team@meta.com, Donet Tom Subject: Re: [PATCH] mm/memory-tiers: cache top tier nodes Date: Tue, 14 Jul 2026 12:20:07 -0700 Message-ID: <20260714192007.919161-1-joshua.hahnjy@gmail.com> X-Mailer: git-send-email 2.53.0 In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit On Tue, 14 Jul 2026 10:17:00 +0530 Ritesh Harjani (IBM) wrote: > Joshua Hahn writes: > > > node_is_toptier() is called in a few hot paths: task_numa_fault(), > > should_numa_migrate_memory(), folio_migrate_flags(), etc. Each call > > takes an RCU read section and performs the tier distance check again. > > > > Tieredness for all nodes only changes on memory node hotplugs. Instead > > of recomputing toptier nodes inside each of the hot paths above, compute > > it only on memory node hotplug events and cache the results. > > > > Did we observe this function "node_is_toptier()" in any of the perf > reports? How much perf benefit we see with this, if at all? > It will be worth adding that info to the commit msg. > > > > Signed-off-by: Joshua Hahn > > --- > > mm/memory-tiers.c | 36 ++++++++++-------------------------- > > 1 file changed, 10 insertions(+), 26 deletions(-) > > > > diff --git a/mm/memory-tiers.c b/mm/memory-tiers.c > > index 54851d8a195b0..2e6e02ec1fce4 100644 > > --- a/mm/memory-tiers.c > > +++ b/mm/memory-tiers.c > > @@ -71,6 +71,7 @@ bool folio_use_access_time(struct folio *folio) > > > > #ifdef CONFIG_NUMA_MIGRATION > > static int top_tier_adistance; > > +static nodemask_t toptier_nodes __read_mostly = NODE_MASK_ALL; > > /* > > * node_demotion[] examples: > > * > > @@ -276,27 +277,7 @@ static struct memory_tier *__node_get_memory_tier(int node) > > #ifdef CONFIG_NUMA_MIGRATION > > bool node_is_toptier(int node) > > { > > - bool toptier; > > - pg_data_t *pgdat; > > - struct memory_tier *memtier; > > - > > - pgdat = NODE_DATA(node); > > - if (!pgdat) > > - return false; > > - > > - rcu_read_lock(); > > - memtier = rcu_dereference(pgdat->memtier); > > - if (!memtier) { > > - toptier = true; > > - goto out; > > - } > > - if (memtier->adistance_start <= top_tier_adistance) > > - toptier = true; > > - else > > - toptier = false; > > -out: > > - rcu_read_unlock(); > > - return toptier; > > + return node_isset(node, toptier_nodes); > > } > > > > void node_get_allowed_targets(pg_data_t *pgdat, nodemask_t *targets) > > @@ -497,19 +478,22 @@ static void establish_demotion_targets(void) > > } > > } > > /* > > - * Now build the lower_tier mask for each node collecting node mask from > > - * all memory tier below it. This allows us to fallback demotion page > > - * allocation to a set of nodes that is closer the above selected > > - * preferred node. > > + * A node stays toptier unless it belongs to a tier below > > + * top_tier_adistance, while each tier's lower_tier_mask collects the > > + * nodes of every tier below it so demotion page allocation can fall > > + * back to nodes closer to the selected preferred node. > > */ > > + toptier_nodes = node_states[N_MEMORY]; > > lower_tier = node_states[N_MEMORY]; > > list_for_each_entry(memtier, &memory_tiers, list) { > > + tier_nodes = get_memtier_nodemask(memtier); > > + if (memtier->adistance_start > top_tier_adistance) > > + nodes_andnot(toptier_nodes, toptier_nodes, tier_nodes); Hello Ritesh, Thank you very much for your review! > I looked into this and I don't see any major issue with this patch. > > However few things worth checking are: > 1. From what I understood say if we add a pmem node as system ram, then > due to the above logic, it will go through these transition states.. > 0 -> 1 -> 0 > This is because we update toptier_nodes to nodes_state[N_MEMORY], which > means, pmem's nid becomes top tier and then we quickly transition that > back to lower tier by clearing the mask. > Instead maybe updating toptier_nodes in one shot at the end would avoid > this intermediate state. Yes, this is true. One thing that I think could be a problem for that is there is not a really good way to make the transition, since nodemask_t can be wider than a single word and I think that would make the transitions have to happen in word chunks, which could leave the nodemask in a partially torn state. data_race() works but it does not capture the nuance that node_is_toptier will always only check one bit at a time so I'm actually not entirely sure what the best approach is. > 2. Also IIRC, after the nid is online, numa balancing for tiered memory > can always work. And we store different values (I guess in > folio->flags), depending upon whether the nid is toptier v/s lower tier. > (_last_cpupid v/s folio last access time). It's worth checking that path > once. > ...So, I looked at it and I think the same transient discripancy (of > whether the node is toptier or lower tier) is true even with the > original code. So I don't think that it should be an issue. Thank you for checking! I think I'll need to think about the above concern for a bit more. I'll also profile whether this is really even a performance concern or not. I just thought it would be good to cache this calculation since it's such a rare operation to update which node is toptier or not, and it tends not to happen when there is much happening on the system anyways. Thank you again for the review, have a great day! Joshua