[RFC PATCH 1/4] mm/numa: introduce nearest_nodes_nodemask()
From: Rakie Kim
Date: Mon Mar 16 2026 - 01:13:31 EST
Add a new NUMA helper, nearest_nodes_nodemask(), to find all nodes in a
given nodemask that are located at the minimum distance from a specified
source node.
Unlike nearest_node_nodemask(), which returns only a single node, this
function identifies all nodes that share the closest distance value. This
is useful when multiple nodes are equally near in the NUMA topology and
a complete set of nearest candidates is required.
The helper clears the output nodemask and sets all nodes that meet the
minimum distance condition. It returns 0 on success or -EINVAL if the
output argument is invalid.
Signed-off-by: Rakie Kim <rakie.kim@xxxxxx>
---
include/linux/numa.h | 8 ++++++++
mm/mempolicy.c | 41 +++++++++++++++++++++++++++++++++++++++++
2 files changed, 49 insertions(+)
diff --git a/include/linux/numa.h b/include/linux/numa.h
index e6baaf6051bc..aa9526e9078b 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,12 @@ 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)
+{
+ 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 68a98ba57882..a3f0fde6c626 100644
--- a/mm/mempolicy.c
+++ b/mm/mempolicy.c
@@ -338,6 +338,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.34.1