mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: chris hyser <chris.hyser@oracle.com>
To: "Chris Hyser" <chris.hyser@oracle.com>,
	"Peter Zijlstra" <peterz@infradead.org>,
	"Mel Gorman" <mgorman@suse.de>,
	linux-kernel@vger.kernel.org
Cc: "Konrad Wilk" <konrad.wilk@oracle.com>
Subject: [RFC/POC 2/2] sched/numa: Adds simple prctl for setting task's preferred node affinity.
Date: Fri, 15 Dec 2023 19:18:01 -0500	[thread overview]
Message-ID: <20231216001801.3015832-3-chris.hyser@oracle.com> (raw)
In-Reply-To: <20231216001801.3015832-1-chris.hyser@oracle.com>

EXPERIMENTAL - NOT INTENDED FOR SUBMISSION

Adds a simple prctl() interface to the preferred node affinity test code.

Signed-off-by: Chris Hyser <chris.hyser@oracle.com>
---
 include/uapi/linux/prctl.h |  9 ++++++
 kernel/sys.c               | 66 ++++++++++++++++++++++++++++++++++++++
 2 files changed, 75 insertions(+)

diff --git a/include/uapi/linux/prctl.h b/include/uapi/linux/prctl.h
index 370ed14b1ae0..6c8f6c0156d8 100644
--- a/include/uapi/linux/prctl.h
+++ b/include/uapi/linux/prctl.h
@@ -293,6 +293,15 @@ struct prctl_mm_map {
 
 #define PR_GET_AUXV			0x41555856
 
+/*
+ * This is experimental and placed out of order to keep surrounding context
+ * the same in the presence of new prctls. Thus the patch should just apply.
+ */
+#define PR_PREFERRED_NID		101
+# define PR_PREFERRED_NID_GET		0
+# define PR_PREFERRED_NID_SET		1
+# define PR_PREFERRED_NID_CMD_MAX	2
+
 #define PR_SET_MEMORY_MERGE		67
 #define PR_GET_MEMORY_MERGE		68
 
diff --git a/kernel/sys.c b/kernel/sys.c
index 420d9cb9cc8e..6dca12da6ade 100644
--- a/kernel/sys.c
+++ b/kernel/sys.c
@@ -2406,6 +2406,67 @@ static inline int prctl_set_mdwe(unsigned long bits, unsigned long arg3,
 	return 0;
 }
 
+#ifdef CONFIG_NUMA_BALANCING
+
+void sched_setnuma(struct task_struct *p, int node);
+
+static int prctl_chg_pref_nid(unsigned long cmd, int nid, pid_t pid, unsigned long uaddr)
+{
+	struct task_struct *task;
+	int err = 0;
+
+	if (cmd >= PR_PREFERRED_NID_CMD_MAX)
+		return -ERANGE;
+
+	rcu_read_lock();
+	if (pid == 0) {
+		task = current;
+	} else {
+		task = find_task_by_vpid((pid_t)pid);
+		if (!task) {
+			rcu_read_unlock();
+			return -ESRCH;
+		}
+	}
+	get_task_struct(task);
+	rcu_read_unlock();
+
+	/*
+	 * Check if this process has the right to modify the specified
+	 * process. Use the regular "ptrace_may_access()" checks.
+	 */
+	if (!ptrace_may_access(task, PTRACE_MODE_READ_REALCREDS)) {
+		err = -EPERM;
+		goto out;
+	}
+
+	switch (cmd) {
+	case PR_PREFERRED_NID_GET:
+		if (uaddr & 0x3) {
+			err = -EINVAL;
+			goto out;
+		}
+		err = put_user(task->numa_preferred_nid_force, (int __user *)uaddr);
+		break;
+
+	case PR_PREFERRED_NID_SET:
+		if (!(-1 <= nid && nid < num_possible_nodes())) {
+			pr_err("prctl_chg_pref_nid: %d error\n", nid);
+			err = -EINVAL;
+			goto out;
+		}
+
+		task->numa_preferred_nid_force = nid;
+		sched_setnuma(task, nid);
+		break;
+	}
+
+out:
+	put_task_struct(task);
+	return err;
+}
+#endif /* CONFIG_NUMA_BALANCING */
+
 static inline int prctl_get_mdwe(unsigned long arg2, unsigned long arg3,
 				 unsigned long arg4, unsigned long arg5)
 {
@@ -2698,6 +2759,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);
-- 
2.39.3


  parent reply	other threads:[~2023-12-16  0:18 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-12-16  0:17 [RFC 0/2] How effective is numa_preferred_nid w.r.t. NUMA performance? chris hyser
2023-12-16  0:18 ` [RFC/POC 1/2] sched/numa: Adds ability to over-ride a tasks numa_preferred_nid chris hyser
2023-12-16  0:18 ` chris hyser [this message]
2024-02-27  0:47 ` [RFC 0/2] How effective is numa_preferred_nid w.r.t. NUMA performance? Chris Hyser

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=20231216001801.3015832-3-chris.hyser@oracle.com \
    --to=chris.hyser@oracle.com \
    --cc=konrad.wilk@oracle.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mgorman@suse.de \
    --cc=peterz@infradead.org \
    /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

all inboxes | Powered by JetHome®