From nobody Mon Feb 9 17:22:12 2026 Received: from mail-dl1-f73.google.com (mail-dl1-f73.google.com [74.125.82.73]) (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 C75B337416C for ; Wed, 14 Jan 2026 07:01:00 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=74.125.82.73 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1768374063; cv=none; b=bM7LUNu26LucesNf572adI4VGv9MIsp0cjW9KRf8L2TFfTFIvZJxMGWEdeKzwJw8mLqkWhG0OPR9mREMEouB1ziHnb7E6H1Iwdt4bcwdyxq73lxgI1+hHeQ06AnUmKZ/GRkjUjE7oehOfQ7NzyFUGnSEqOkOTO+d7fZbejUIcXA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1768374063; c=relaxed/simple; bh=r4kuuW+bMF1nQ3YTU38KPHbda+RH0kY+6CsCC0d5mhE=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=Bk7bh32xDFoEvqyjb92kMFP2M3W3TWD+PhQiCsq8ug7iAezFqEw58sYgY7Mz+m9W8aohe2sso1qMJaxUiTxa+t4w3xGmM3zp8YLgwk1PzUvGAhbfRZv1WOgPnmqKTnNnmAB1ZIMBgev++g4j093HPq9+D7jJvbl09N5aL4jmxNc= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--bingjiao.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=g1VIzcr3; arc=none smtp.client-ip=74.125.82.73 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--bingjiao.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="g1VIzcr3" Received: by mail-dl1-f73.google.com with SMTP id a92af1059eb24-11b7dbce216so12345040c88.1 for ; Tue, 13 Jan 2026 23:00:59 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1768374057; x=1768978857; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=t/KY0VdnopC2hmLm4xwT6Iw+Xhnz6LnQa/Hsf1VK0I8=; b=g1VIzcr3ndBl6QC54RrmB1fGghmTOAa+lPaS4L/8JnyxySM2dGocdh2m66ljwRcPaY yV8wKS6Iv2BQnMVt3NhkgjEtBi1PQmr5rTLglt7lhxDVp1k5e/l49hcaqc+knSZDAFdc cZcQmZNrCmCAvXBnwNrtFWJhmV11Sm5cFMLjcjvVV7+YKAvBShiaunHPfFx+XxrdblEn n3Gbcbj2CIivNwEp3+6n+zxuMySnzyttc20birCP4Q6nrVlwgSATy8S1UNc9CTFRTPjP 9Vg1vf4d7DlWk8euqLhglYnP84zzJDsf7/9u8EPuPYFDYV4UtKybj8N0qCLvaKmncB8S XjgQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1768374057; x=1768978857; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=t/KY0VdnopC2hmLm4xwT6Iw+Xhnz6LnQa/Hsf1VK0I8=; b=CTq8BmtMVNMPqz7ouagsdD4IRf6KwrgxF0ElqLlQ1YYRbA3NlENr/jZRn76iU+DO2U vSkk9Ab8xCM19zXEAuiQAfo29Q2eaKkamlWdPuqHDKZxqNUUjlvIYtLz5xk7m/ETFtLx CEWagyJi1Mphcg4QibNjWIyenELaXVdIDgiFru+bG6Ym3eIzqokrfeVGlFN6/RHg9x5a x1TMMsR4YaQT7W8b00mlvbAYv6jkK6bvTF59B78jTzcEPRONSFk70PyMyvPNm67K3GC5 IeQhHq8vw336XWzmqUthq3g/oD2HTfvf4qs2N4E9C44SKf69cJkVY/Ud0nPji5fvV6f9 Z2Zg== X-Forwarded-Encrypted: i=1; AJvYcCVuBxEzEkVBFNaYNX6isJBfCWXmtWYcRp4IAHKkdgzR9FcAZPcRvLMCINPJmesusC5w7ztK0PaJVzzK3so=@vger.kernel.org X-Gm-Message-State: AOJu0YzX/KktOQQYt9ju7YT19rSunHE1zPjsR/LTukDzqG1mKGaVqZAb H/JcFl2pPwLatxTqb2ouXJjWMz2xMcnVM3sk87MvoLB86+0qWpvtIGAvsafE6MF3dKzSO5CpB5t zhnf2ydVfoiKdOA== X-Received: from dltt13.prod.google.com ([2002:a05:701a:c96d:b0:121:79a7:ec2d]) (user=bingjiao job=prod-delivery.src-stubby-dispatcher) by 2002:a05:7022:f30a:b0:119:e56b:91d1 with SMTP id a92af1059eb24-12336a13555mr1101663c88.2.1768374056893; Tue, 13 Jan 2026 23:00:56 -0800 (PST) Date: Wed, 14 Jan 2026 06:59:49 +0000 In-Reply-To: <20260114070053.2446770-1-bingjiao@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20260108033248.2791579-1-bingjiao@google.com> <20260114070053.2446770-1-bingjiao@google.com> X-Mailer: git-send-email 2.52.0.457.g6b5491de43-goog Message-ID: <20260114070053.2446770-2-bingjiao@google.com> Subject: [PATCH v8 2/2] mm/vmscan: select the closest preferred node in demote_folio_list() From: Bing Jiao To: bingjiao@google.com Cc: Andrew Morton , David Hildenbrand , Lorenzo Stoakes , "Liam R. Howlett" , Vlastimil Babka , Mike Rapoport , Suren Baghdasaryan , Michal Hocko , Axel Rasmussen , Yuanchu Xie , Wei Xu , Johannes Weiner , Qi Zheng , Shakeel Butt , Gregory Price , Joshua Hahn , chenridong@huaweicloud.com, linux-mm@kvack.org, linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" The preferred demotion node (migration_target_control.nid) should be the one closest to the source node to minimize migration latency. Currently, a discrepancy exists where demote_folio_list() randomly selects an allowed node if the preferred node from next_demotion_node() is not set in mems_allowed. To address it, update next_demotion_node() to return preferred nodes, allowing the caller to select the preferred one. Also update demote_folio_list() to get the closest node from allowed if all preferred nodes are not set in mems_allowed. It ensures that the preferred demotion target is consistently the closest available node to the source node. Signed-off-by: Bing Jiao --- include/linux/memory-tiers.h | 6 +++--- mm/memory-tiers.c | 11 +++++++---- mm/vmscan.c | 30 +++++++++++++++++++++++------- 3 files changed, 33 insertions(+), 14 deletions(-) diff --git a/include/linux/memory-tiers.h b/include/linux/memory-tiers.h index 7a805796fcfd..87652042f2c2 100644 --- a/include/linux/memory-tiers.h +++ b/include/linux/memory-tiers.h @@ -53,11 +53,11 @@ struct memory_dev_type *mt_find_alloc_memory_type(int a= dist, struct list_head *memory_types); void mt_put_memory_types(struct list_head *memory_types); #ifdef CONFIG_MIGRATION -int next_demotion_node(int node); +int next_demotion_node(int node, nodemask_t *preferred_nodes); void node_get_allowed_targets(pg_data_t *pgdat, nodemask_t *targets); bool node_is_toptier(int node); #else -static inline int next_demotion_node(int node) +static inline int next_demotion_node(int node, nodemask_t *preferred_nodes) { return NUMA_NO_NODE; } @@ -101,7 +101,7 @@ static inline void clear_node_memory_type(int node, str= uct memory_dev_type *memt } -static inline int next_demotion_node(int node) +static inline int next_demotion_node(int node, nodemask_t *preferred_nodes) { return NUMA_NO_NODE; } diff --git a/mm/memory-tiers.c b/mm/memory-tiers.c index 20aab9c19c5e..c71a865e9dd8 100644 --- a/mm/memory-tiers.c +++ b/mm/memory-tiers.c @@ -320,13 +320,14 @@ void node_get_allowed_targets(pg_data_t *pgdat, nodem= ask_t *targets) /** * next_demotion_node() - Get the next node in the demotion path * @node: The starting node to lookup the next node + * @preferred_nodes: The pointer to nodemask of all preferred nodes to ret= urn * * Return: node id for next memory node in the demotion path hierarchy - * from @node; NUMA_NO_NODE if @node is terminal. This does not keep - * @node online or guarantee that it *continues* to be the next demotion - * target. + * from @node; NUMA_NO_NODE if @node is terminal. Also returns all preferr= ed + * nodes in @preferred_nodes. This does not keep @node online or guarantee + * that it *continues* to be the next demotion target. */ -int next_demotion_node(int node) +int next_demotion_node(int node, nodemask_t *preferred_nodes) { struct demotion_nodes *nd; int target; @@ -357,6 +358,8 @@ int next_demotion_node(int node) * target node randomly seems better until now. */ target =3D node_random(&nd->preferred); + if (preferred_nodes) + nodes_copy(*preferred_nodes, nd->preferred); rcu_read_unlock(); return target; diff --git a/mm/vmscan.c b/mm/vmscan.c index 71617891bcde..18f4425bd750 100644 --- a/mm/vmscan.c +++ b/mm/vmscan.c @@ -1023,9 +1023,10 @@ static unsigned int demote_folio_list(struct list_he= ad *demote_folios, struct pglist_data *pgdat, struct mem_cgroup *memcg) { - int target_nid =3D next_demotion_node(pgdat->node_id); + int target_nid; unsigned int nr_succeeded; nodemask_t allowed_mask; + nodemask_t preferred; struct migration_target_control mtc =3D { /* @@ -1042,17 +1043,32 @@ static unsigned int demote_folio_list(struct list_h= ead *demote_folios, if (list_empty(demote_folios)) return 0; - if (target_nid =3D=3D NUMA_NO_NODE) - /* No lower-tier nodes or nodes were hot-unplugged. */ - return 0; - node_get_allowed_targets(pgdat, &allowed_mask); mem_cgroup_node_filter_allowed(memcg, &allowed_mask); if (nodes_empty(allowed_mask)) return 0; - if (!node_isset(target_nid, allowed_mask)) - target_nid =3D node_random(&allowed_mask); + target_nid =3D next_demotion_node(pgdat->node_id, &preferred); + if (target_nid =3D=3D NUMA_NO_NODE) + /* No lower-tier nodes (e.g., nodes were hot-unplugged) */ + return 0; + + if (!node_isset(target_nid, allowed_mask)) { + /* Filter out preferred nodes that are not in allowed. */ + nodes_and(preferred, preferred, allowed_mask); + if (!nodes_empty(preferred)) { + /* Randomly select one node from preferred. */ + target_nid =3D node_random(&preferred); + } else { + /* Get the closest node from allowed. */ + nodes_complement(preferred, allowed_mask); + target_nid =3D find_next_best_node(pgdat->node_id, + &preferred); + if (target_nid =3D=3D NUMA_NO_NODE) + return 0; + } + } + mtc.nid =3D target_nid; /* Demotion ignores all cpuset and mempolicy settings */ -- 2.52.0.457.g6b5491de43-goog