From: Rakie Kim <rakie.kim@sk.com>
To: akpm@linux-foundation.org
Cc: gourry@gourry.net, linux-mm@kvack.org,
linux-kernel@vger.kernel.org, linux-cxl@vger.kernel.org,
nvdimm@lists.linux.dev, ziy@nvidia.com, matthew.brost@intel.com,
joshua.hahnjy@gmail.com, byungchul@sk.com,
ying.huang@linux.alibaba.com, apopple@nvidia.com,
david@kernel.org, ljs@kernel.org, liam@infradead.org,
vbabka@kernel.org, rppt@kernel.org, surenb@google.com,
mhocko@suse.com, dave@stgolabs.net, jic23@kernel.org,
dave.jiang@intel.com, alison.schofield@intel.com,
vishal.l.verma@intel.com, ira.weiny@intel.com, harry@kernel.org,
kernel_team@skhynix.com, honggyu.kim@sk.com, yunjeong.mun@sk.com,
rakie.kim@sk.com
Subject: [PATCH 1/4] mm/numa: introduce nearest_nodes_nodemask()
Date: Thu, 6 Aug 2026 17:09:32 +0900 [thread overview]
Message-ID: <20260806080936.421-2-rakie.kim@sk.com> (raw)
In-Reply-To: <20260806080936.421-1-rakie.kim@sk.com>
Add a NUMA helper, nearest_nodes_nodemask(), that returns every node in a
given nodemask located at the minimum distance from a source node.
Unlike nearest_node_nodemask(), which returns only a single node, this
helper reports the complete set of nodes that share the closest distance.
This is needed when several nodes are equally near and all of the nearest
candidates must be considered together.
The helper clears the output nodemask and sets every node that meets the
minimum-distance condition. It returns 0 on success, or -EINVAL when the
output argument is invalid.
Signed-off-by: Rakie Kim <rakie.kim@sk.com>
---
include/linux/numa.h | 11 +++++++++++
mm/mempolicy.c | 41 +++++++++++++++++++++++++++++++++++++++++
2 files changed, 52 insertions(+)
diff --git a/include/linux/numa.h b/include/linux/numa.h
index e6baaf6051bc..4f2a0c344122 100644
--- a/include/linux/numa.h
+++ b/include/linux/numa.h
@@ -33,6 +33,8 @@ int numa_nearest_node(int node, unsigned int state);
int nearest_node_nodemask(int node, nodemask_t *mask);
+int nearest_nodes_nodemask(int node, const nodemask_t *mask, nodemask_t *out);
+
#ifndef memory_add_physaddr_to_nid
int memory_add_physaddr_to_nid(u64 start);
#endif
@@ -54,6 +56,15 @@ static inline int nearest_node_nodemask(int node, nodemask_t *mask)
return NUMA_NO_NODE;
}
+static inline int nearest_nodes_nodemask(int node, const nodemask_t *mask,
+ nodemask_t *out)
+{
+ if (!out)
+ return -EINVAL;
+ nodes_clear(*out);
+ return 0;
+}
+
static inline int memory_add_physaddr_to_nid(u64 start)
{
return 0;
diff --git a/mm/mempolicy.c b/mm/mempolicy.c
index 4e4421b22b59..19417b0afc30 100644
--- a/mm/mempolicy.c
+++ b/mm/mempolicy.c
@@ -337,6 +337,47 @@ int nearest_node_nodemask(int node, nodemask_t *mask)
}
EXPORT_SYMBOL_GPL(nearest_node_nodemask);
+/**
+ * nearest_nodes_nodemask - Find all nodes in @mask that are nearest to @node
+ * @node: The reference node ID to measure distance from
+ * @mask: The set of candidate nodes to compare against
+ * @out: Pointer to a nodemask that will store the nearest node(s)
+ *
+ * This function iterates over all nodes in @mask and measures the distance
+ * between each candidate node and the given @node using node_distance().
+ * It finds the minimum distance and then records all nodes in @mask that
+ * share that same minimum distance into the output mask @out.
+ *
+ * For example, if multiple nodes have equal minimal distance to @node, all
+ * of them are included in @out.
+ *
+ * Return: 0 on success, or -EINVAL if @out is NULL.
+ */
+int nearest_nodes_nodemask(int node, const nodemask_t *mask, nodemask_t *out)
+{
+ int dist, n, min_dist = INT_MAX;
+
+ if (!out)
+ return -EINVAL;
+
+ nodes_clear(*out);
+
+ for_each_node_mask(n, *mask) {
+ dist = node_distance(node, n);
+
+ if (dist < min_dist) {
+ min_dist = dist;
+ nodes_clear(*out);
+ node_set(n, *out);
+ } else if (dist == min_dist) {
+ node_set(n, *out);
+ }
+ }
+
+ return 0;
+}
+EXPORT_SYMBOL_GPL(nearest_nodes_nodemask);
+
struct mempolicy *get_task_policy(struct task_struct *p)
{
struct mempolicy *pol = p->mempolicy;
--
2.25.1
next prev parent reply other threads:[~2026-08-06 8:10 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-08-06 8:09 [PATCH 0/4] mm/mempolicy: introduce package-aware weighted interleave Rakie Kim
2026-08-06 8:09 ` Rakie Kim [this message]
2026-08-06 8:09 ` [PATCH 2/4] mm/memory-tiers: introduce package-aware topology management for NUMA nodes Rakie Kim
2026-08-06 8:09 ` [PATCH 3/4] mm/memory-tiers: register CXL nodes to memory packages via initiator Rakie Kim
2026-08-06 8:09 ` [PATCH 4/4] mm/mempolicy: enhance weighted interleave with package-aware locality Rakie Kim
2026-08-06 21:38 ` [PATCH 0/4] mm/mempolicy: introduce package-aware weighted interleave Andrew Morton
2026-08-07 4:07 ` Rakie Kim
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20260806080936.421-2-rakie.kim@sk.com \
--to=rakie.kim@sk.com \
--cc=akpm@linux-foundation.org \
--cc=alison.schofield@intel.com \
--cc=apopple@nvidia.com \
--cc=byungchul@sk.com \
--cc=dave.jiang@intel.com \
--cc=dave@stgolabs.net \
--cc=david@kernel.org \
--cc=gourry@gourry.net \
--cc=harry@kernel.org \
--cc=honggyu.kim@sk.com \
--cc=ira.weiny@intel.com \
--cc=jic23@kernel.org \
--cc=joshua.hahnjy@gmail.com \
--cc=kernel_team@skhynix.com \
--cc=liam@infradead.org \
--cc=linux-cxl@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=ljs@kernel.org \
--cc=matthew.brost@intel.com \
--cc=mhocko@suse.com \
--cc=nvdimm@lists.linux.dev \
--cc=rppt@kernel.org \
--cc=surenb@google.com \
--cc=vbabka@kernel.org \
--cc=vishal.l.verma@intel.com \
--cc=ying.huang@linux.alibaba.com \
--cc=yunjeong.mun@sk.com \
--cc=ziy@nvidia.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox