Skip to contentSkip to conversation

sched/numa, mm/numa: Soft Affinity via numa_preferred_nid.

PATCH New chris hyser 1 participant 3 messages Jun 22, 2025

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

v3 cover sched/numa, mm/numa: Soft Affinity via numa_preferred_nid.

Soft Affinity (value of hard affinity with graceful handling of overload) as a concept has been around for years. The original implementation was rejected

https://lore.kernel.org/lkml/[email protected]/

with an alternative, using numa_preferred_nid, suggested by Peter Zijlstra.

This is a simple implementation with most of the changes associated with a prctl() to set/get the value. It does not modify the scheduler's behavior but simply exploits the current NUMA balancing behavior.

The intent is to provide a mechanism whereby a knowledgble user, system admin, or importantly, a NUMA aware application can force Auto NUMA Balancing to prefer the "correct" node, for example pinned memory like RDMA buffers or other scenarios where heavily accessed memory ranges are pinned and not subject to NUMA hint faults.

v2 to v3 Changes:
- removed confusing comments.
- combined a nested if into a single statement.

[PATCH v3 1/2] sched/numa: Add ability to override task's numa_preferred_nid.

 include/linux/sched.h |  1 +
 init/init_task.c      |  1 +
 kernel/sched/core.c   |  5 ++++-
 kernel/sched/debug.c  |  1 +
 kernel/sched/fair.c   | 11 +++++++++--
 5 files changed, 16 insertions(+), 3 deletions(-)

[PATCH v3 2/2] sched/numa: prctl to set/override task's numa_preferred_nid

 Documentation/scheduler/sched-preferred-node.rst | 67 ++++++++++++++++++++++++
 include/linux/sched.h                            |  9 ++++
 include/uapi/linux/prctl.h                       |  8 +++
 kernel/sched/fair.c                              | 65 +++++++++++++++++++++++
 kernel/sys.c                                     |  5 ++
 tools/include/uapi/linux/prctl.h                 |  6 +++
 6 files changed, 160 insertions(+)
#1 lore raw

PATCH v3 1/2 sched/numa: Add ability to override task's numa_preferred_nid.

This patch allows directly setting and subsequent overriding of a task's "Preferred Node Affinity" by setting the task's numa_preferred_nid and relying on the existing NUMA balancing infrastructure.

NUMA balancing introduced the notion of tracking and using a task's preferred memory node for both migrating/consolidating the physical pages accessed by a task and to assist the scheduler in making NUMA aware placement and load balancing decisions.

The existing mechanism for determining this, Auto NUMA Balancing, relies on periodic removal of virtual mappings for blocks of a task's address space. The resulting faults can indicate a task's preference for an accessed node.

This has two issues that this patch seeks to overcome:

- there is a trade-off between faulting overhead and the ability to detect dynamic access patterns. In cases where the task or user understand the NUMA sensitivities, this patch can enable the benefits of setting a preferred node used either in conjunction with Auto NUMA Balancing's default parameters or adjusting the NUMA balance parameters to reduce the faulting rate (potentially to 0).

- memory pinned to nodes or to physical addresses such as RDMA cannot be migrated and have thus far been excluded from the scanning. Not taking those faults however can prevent Auto NUMA Balancing from reliably detecting a node preference with the scheduler load balancer then possibly operating with incorrect NUMA information.

The following results were from TPCC runs on an Oracle Database. The system was a 2-node AMD machine with a database running on each node with local memory allocations. No tasks or memory were pinned.

There are four scenarios of interest:

- Auto NUMA Balancing OFF.
base value

- Auto NUMA Balancing ON.
1.2% - ANB ON better than ANB OFF.

- Use the prctl(), ANB ON, parameters set to prevent faulting. 2.4% - prctl() better then ANB OFF.
1.2% - prctl() better than ANB ON.

- Use the prctl(), ANB parameters normal.
3.1% - prctl() and ANB ON better than ANB OFF.
1.9% - prctl() and ANB ON better than just ANB ON.
0.7% - prctl() and ANB ON better than prctl() and ANB ON/faulting off

The primary advantage of PNA and ANB on is that the resulting NUMA hint faults are also used to periodically check that a task is on it's preferred node perhaps having been migrated during load balancing.

In benchmarks pinning large regions of heavily accessed memory, the advantages of the prctl() over Auto NUMA Balancing alone is significantly higher.

---
 include/linux/sched.h |  1 +
 init/init_task.c      |  1 +
 kernel/sched/core.c   |  5 ++++-
 kernel/sched/debug.c  |  1 +
 kernel/sched/fair.c   | 11 +++++++++--
 5 files changed, 16 insertions(+), 3 deletions(-)
include/linux/sched.h +1 -0
index eec6b225e9d1..bd5a307fed1e 100644
@@ -1345,6 +1345,7 @@ struct task_struct {
 	short				pref_node_fork;
 #endif
 #ifdef CONFIG_NUMA_BALANCING
+	int				numa_preferred_nid_force;
 	int				numa_scan_seq;
 	unsigned int			numa_scan_period;
 	unsigned int			numa_scan_period_max;
init/init_task.c +1 -0
index e557f622bd90..1921a87326db 100644
@@ -184,6 +184,7 @@ struct task_struct init_task __aligned(L1_CACHE_BYTES) = {
 	.vtime.state	= VTIME_SYS,
 #endif
 #ifdef CONFIG_NUMA_BALANCING
+	.numa_preferred_nid_force = NUMA_NO_NODE,
 	.numa_preferred_nid = NUMA_NO_NODE,
 	.numa_group	= NULL,
 	.numa_faults	= NULL,
kernel/sched/core.c +4 -1
index 2f8caa9db78d..f11755a54afd 100644
@@ -7861,7 +7861,10 @@ void sched_setnuma(struct task_struct *p, int nid)
 	if (running)
 		put_prev_task(rq, p);
 
-	p->numa_preferred_nid = nid;
+	if (unlikely(p->numa_preferred_nid_force != NUMA_NO_NODE))
+		p->numa_preferred_nid = p->numa_preferred_nid_force;
+	else
+		p->numa_preferred_nid = nid;
 
 	if (queued)
 		enqueue_task(rq, p, ENQUEUE_RESTORE | ENQUEUE_NOCLOCK);
kernel/sched/debug.c +1 -0
index f16d53928bb9..590310b8cf7a 100644
@@ -1137,6 +1137,7 @@ static void sched_show_numa(struct task_struct *p, struct seq_file *m)
 		P(mm->numa_scan_seq);
 
 	P(numa_pages_migrated);
+	P(numa_preferred_nid_force);
 	P(numa_preferred_nid);
 	P(total_numa_faults);
 	SEQ_printf(m, "current_node=%d, numa_group_id=%d\n",
kernel/sched/fair.c +9 -2
index 7e2963efe800..b5c4a2507021 100644
@@ -2626,7 +2626,10 @@ static void numa_migrate_preferred(struct task_struct *p)
 	unsigned long interval = HZ;
 
 	/* This task has no NUMA fault statistics yet */
-	if (unlikely(p->numa_preferred_nid == NUMA_NO_NODE || !p->numa_faults))
+	if (unlikely(p->numa_preferred_nid == NUMA_NO_NODE))
+		return;
+
+	if (p->numa_preferred_nid_force == NUMA_NO_NODE && !p->numa_faults)
 		return;
 
 	/* Periodically retry migrating the task to the preferred node */
@@ -3571,6 +3574,7 @@ void init_numa_balancing(unsigned long clone_flags, struct task_struct *p)
 
 	/* New address space, reset the preferred nid */
 	if (!(clone_flags & CLONE_VM)) {
+		p->numa_preferred_nid_force = NUMA_NO_NODE;
 		p->numa_preferred_nid = NUMA_NO_NODE;
 		return;
 	}
@@ -9210,7 +9214,10 @@ static long migrate_degrades_locality(struct task_struct *p, struct lb_env *env)
 	if (!static_branch_likely(&sched_numa_balancing))
 		return 0;
 
-	if (!p->numa_faults || !(env->sd->flags & SD_NUMA))
+	if (p->numa_preferred_nid_force == NUMA_NO_NODE && !p->numa_faults)
+		return 0;
+
+	if (!(env->sd->flags & SD_NUMA))
 		return 0;
 
 	src_nid = cpu_to_node(env->src_cpu);
Signature · 1 line
2.43.5
#2 lore raw

PATCH v3 2/2 sched/numa: prctl to set/override task's numa_preferred_nid

In reply to chris hyser

Adds a simple prctl() interface to enable setting or reading a task's numa_preferred_nid. Once set this value will override any value set by auto NUMA balancing.

---
 .../scheduler/sched-preferred-node.rst        | 67 +++++++++++++++++++
 include/linux/sched.h                         |  9 +++
 include/uapi/linux/prctl.h                    |  8 +++
 kernel/sched/fair.c                           | 65 ++++++++++++++++++
 kernel/sys.c                                  |  5 ++
 tools/include/uapi/linux/prctl.h              |  6 ++
 6 files changed, 160 insertions(+)
 create mode 100644 Documentation/scheduler/sched-preferred-node.rst
Documentation/scheduler/sched-preferred-node.rst new file +67 -0
new file mode 100644
index 000000000000..753fd0b20993
@@ -0,0 +1,67 @@
+.. SPDX-License-Identifier: GPL-2.0
+
+Prctl for Explicitly Setting Task's Preferred Node
+####################################################
+
+This feature is an addition to Auto NUMA Balancing. Auto NUMA balancing by
+default scans a task's address space removing address translations such that
+subsequent faults can indicate the predominant node from which memory is being
+accessed. A task's numa_preferred_nid is set to the node ID.
+
+The numa_preferred_nid is used to both consolidate physical pages and assist the
+scheduler in making NUMA friendly load balancing decisions.
+
+While quite useful for some workloads, this has two issues that this prctl() can
+help solve:
+
+- There is a trade-off between faulting overhead and the ability to detect
+dynamic access patterns. In cases where the task or user understand the NUMA
+sensitivities, this patch can enable the benefits of setting a preferred node
+used either in conjunction with Auto NUMA Balancing's default parameters or
+adjusting the NUMA balance parameters to reduce the faulting rate
+(potentially to 0).
+
+- Memory pinned to nodes or to physical addresses such as RDMA cannot be
+migrated and have thus far been excluded from the scanning. Not taking
+those faults however can prevent Auto NUMA Balancing from reliably detecting a
+node preference with the scheduler load balancer then possibly operating with
+incorrect NUMA information.
+
+
+Usage
+*******
+
+    Note: Auto NUMA Balancing must be enabled to get the effects.
+
+    #include <sys/prctl.h>
+
+    int prctl(int option, unsigned long arg2, unsigned long arg3, unsigned long arg4, unsigned long arg5);
+
+option:
+    ``PR_PREFERRED_NID``
+
+arg2:
+    Command for operation, must be one of:
+
+    - ``PR_PREFERRED_NID_GET`` -- get the forced preferred node ID for ``pid``.
+    - ``PR_PREFERRED_NID_SET`` -- set the forced preferred node ID for ``pid``.
+
+    Returns ERANGE for an illegal command.
+
+arg3:
+    ``pid`` of the task for which the operation applies. ``0`` implies current.
+
+    Returns ESRCH if ``pid`` is not found.
+
+arg4:
+    ``node_id`` for PR_PREFERRED_NID_SET. Between ``-1`` and ``num_possible_nodes()``.
+    ``-1`` indicates no preference.
+
+    Returns EINVAL for an illegal command.
+
+arg5:
+    userspace pointer to an integer for returning the Node ID from
+    ``PR_PREFERRED_NID_GET``. Should be 0 for all other commands.
+
+Must have the ptrace access mode: `PTRACE_MODE_READ_REALCREDS` to get/set
+the preferred node ID to a process otherwise returns EPERM.
include/linux/sched.h +9 -0
index bd5a307fed1e..f9f317114b32 100644
@@ -2202,6 +2202,15 @@ static inline void sched_core_fork(struct task_struct *p) { }
 static inline int sched_core_idle_cpu(int cpu) { return idle_cpu(cpu); }
 #endif
 
+#ifdef CONFIG_NUMA_BALANCING
+/* Change a task's numa_preferred_nid */
+int prctl_chg_pref_nid(unsigned long cmd, int nid, pid_t pid,
+		       unsigned long uaddr);
+#else
+static inline int prctl_chg_pref_nid(unsigned long cmd, int nid, pid_t pid,
+				     unsigned long uaddr) { return -ERANGE; }
+#endif
+
 extern void sched_set_stop_task(int cpu, struct task_struct *stop);
 
 #ifdef CONFIG_MEM_ALLOC_PROFILING
include/uapi/linux/prctl.h +8 -0
index 43dec6eed559..020f64938d70 100644
@@ -371,4 +371,12 @@ struct prctl_mm_map {
 # define PR_FUTEX_HASH_GET_SLOTS	2
 # define PR_FUTEX_HASH_GET_IMMUTABLE	3
 
+/*
+ * Set or get a task's numa_preferred_nid
+ */
+#define PR_PREFERRED_NID		79
+# define PR_PREFERRED_NID_GET		0
+# define PR_PREFERRED_NID_SET		1
+# define PR_PREFERRED_NID_CMD_MAX	2
+
 #endif /* _LINUX_PRCTL_H */
kernel/sched/fair.c +65 -0

77 lines not on this page · raw message

kernel/sys.c +5 -0
index adc0de0aa364..0bb0df4ac70f 100644
@@ -2747,6 +2747,11 @@ SYSCALL_DEFINE5(prctl, int, option, unsigned long, arg2, unsigned long, arg3,
 	case PR_SCHED_CORE:
 		error = sched_core_share_pid(arg2, arg3, arg4, arg5);
 		break;
+#endif
+#ifdef CONFIG_NUMA_BALANCING
+	case PR_PREFERRED_NID:
+		error = prctl_chg_pref_nid(arg2, arg3, arg4, arg5);
+		break;
 #endif
 	case PR_SET_MDWE:
 		error = prctl_set_mdwe(arg2, arg3, arg4, arg5);
tools/include/uapi/linux/prctl.h +6 -0
index 43dec6eed559..789d06e5cb2b 100644
@@ -371,4 +371,10 @@ struct prctl_mm_map {
 # define PR_FUTEX_HASH_GET_SLOTS	2
 # define PR_FUTEX_HASH_GET_IMMUTABLE	3
 
+/* Set or get a task's numa_preferred_nid
+ */
+#define PR_PREFERRED_NID		79
+# define PR_PREFERRED_NID_GET		0
+# define PR_PREFERRED_NID_SET		1
+# define PR_PREFERRED_NID_CMD_MAX	2
 #endif /* _LINUX_PRCTL_H */
Signature · 1 line
2.43.5