linux-mm.kvack.org archive mirror
 help / color / mirror / Atom feed
From: Gregory Price <gourry.memverge@gmail.com>
To: linux-mm@kvack.org
Cc: linux-doc@vger.kernel.org, linux-fsdevel@vger.kernel.org,
	linux-api@vger.kernel.org, linux-arch@vger.kernel.org,
	linux-kernel@vger.kernel.org, akpm@linux-foundation.org,
	arnd@arndb.de, tglx@linutronix.de, luto@kernel.org,
	mingo@redhat.com, bp@alien8.de, dave.hansen@linux.intel.com,
	x86@kernel.org, hpa@zytor.com, mhocko@kernel.org, tj@kernel.org,
	ying.huang@intel.com, Gregory Price <gregory.price@memverge.com>
Subject: [RFC PATCH 11/11] fs/proc: Add mempolicy attribute to allow read/write of task mempolicy
Date: Wed, 22 Nov 2023 16:12:00 -0500	[thread overview]
Message-ID: <20231122211200.31620-12-gregory.price@memverge.com> (raw)
In-Reply-To: <20231122211200.31620-1-gregory.price@memverge.com>

Expose mempolicy via procfs, and utilize the existing mpol_parse_str
format to allow external tasks to change the policies of another task.

mpol_parse_str format:
	<mode>[=<flags>][:<nodelist>]

valid settings:
  "prefer"	(without a nodemask, aliases to 'local')
  "prefer:node"
  "interleave:nodelist"
  "local"
  "default"
  "prefer (many):nodelist"
  "bind:nodelist"

flags are either "=static" or "=relative", and cannot be used with
"prefer" or "local"  ("prefer=flag:nodelist" is valid).

Signed-off-by: Gregory Price <gregory.price@memverge.com>
---
 fs/proc/Makefile    |   1 +
 fs/proc/base.c      |   1 +
 fs/proc/internal.h  |   1 +
 fs/proc/mempolicy.c | 117 ++++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 120 insertions(+)
 create mode 100644 fs/proc/mempolicy.c

diff --git a/fs/proc/Makefile b/fs/proc/Makefile
index bd08616ed8ba..272d22d9022f 100644
--- a/fs/proc/Makefile
+++ b/fs/proc/Makefile
@@ -27,6 +27,7 @@ proc-y	+= softirqs.o
 proc-y	+= namespaces.o
 proc-y	+= self.o
 proc-y	+= thread_self.o
+proc-y	+= mempolicy.o
 proc-$(CONFIG_PROC_SYSCTL)	+= proc_sysctl.o
 proc-$(CONFIG_NET)		+= proc_net.o
 proc-$(CONFIG_PROC_KCORE)	+= kcore.o
diff --git a/fs/proc/base.c b/fs/proc/base.c
index dd31e3b6bf77..3eb3d6d81a8e 100644
--- a/fs/proc/base.c
+++ b/fs/proc/base.c
@@ -3279,6 +3279,7 @@ static const struct pid_entry tgid_base_stuff[] = {
 	REG("maps",       S_IRUGO, proc_pid_maps_operations),
 #ifdef CONFIG_NUMA
 	REG("numa_maps",  S_IRUGO, proc_pid_numa_maps_operations),
+	REG("mempolicy",  S_IRUSR|S_IWUSR, proc_mempolicy_operations),
 #endif
 	REG("mem",        S_IRUSR|S_IWUSR, proc_mem_operations),
 	LNK("cwd",        proc_cwd_link),
diff --git a/fs/proc/internal.h b/fs/proc/internal.h
index 9a8f32f21ff5..e8e81629a8d8 100644
--- a/fs/proc/internal.h
+++ b/fs/proc/internal.h
@@ -303,6 +303,7 @@ extern const struct file_operations proc_pid_smaps_operations;
 extern const struct file_operations proc_pid_smaps_rollup_operations;
 extern const struct file_operations proc_clear_refs_operations;
 extern const struct file_operations proc_pagemap_operations;
+extern const struct file_operations proc_mempolicy_operations;
 
 extern unsigned long task_vsize(struct mm_struct *);
 extern unsigned long task_statm(struct mm_struct *,
diff --git a/fs/proc/mempolicy.c b/fs/proc/mempolicy.c
new file mode 100644
index 000000000000..417c2c8046d9
--- /dev/null
+++ b/fs/proc/mempolicy.c
@@ -0,0 +1,117 @@
+// SPDX-License-Identifier: GPL-2.0
+#ifdef CONFIG_NUMA
+#include <linux/fs.h>
+#include <linux/proc_fs.h>
+#include <linux/sched.h>
+#include <linux/mempolicy.h>
+#include <linux/uaccess.h>
+#include <linux/nodemask.h>
+
+#include "internal.h"
+
+#define MPOL_STR_SIZE 4096
+static ssize_t mempolicy_read_proc(struct file *file, char __user *buf,
+		size_t count, loff_t *ppos)
+{
+	struct task_struct *task;
+	struct mempolicy *policy;
+	char *buffer;
+	ssize_t rv = 0;
+	size_t outlen;
+
+	buffer = kzalloc(MPOL_STR_SIZE, GFP_KERNEL);
+	if (!buffer)
+		return -ENOMEM;
+
+	task = get_proc_task(file_inode(file));
+	if (!task) {
+		rv = -ESRCH;
+		goto freebuf;
+	}
+
+	task_lock(task);
+	policy = get_task_policy(task);
+	mpol_get(policy);
+	task_unlock(task);
+
+	if (!policy)
+		goto out;
+
+	mpol_to_str(buffer, MPOL_STR_SIZE, policy);
+
+	buffer[MPOL_STR_SIZE-1] = '\0';
+	outlen = strlen(buffer);
+	if (outlen < MPOL_STR_SIZE - 1) {
+		buffer[outlen] = '\n';
+		buffer[outlen + 1] = '\0';
+		outlen++;
+	}
+	rv = simple_read_from_buffer(buf, count, ppos, buffer, outlen);
+	mpol_put(policy);
+out:
+	put_task_struct(task);
+freebuf:
+	kfree(buffer);
+	return rv;
+}
+
+static ssize_t mempolicy_write_proc(struct file *file, const char __user *buf,
+				    size_t count, loff_t *ppos)
+{
+	struct task_struct *task;
+	struct mempolicy *new_policy = NULL;
+	char *mempolicy_str, *nl;
+	nodemask_t nodes;
+	int err;
+
+	mempolicy_str = kmalloc(count + 1, GFP_KERNEL);
+	if (!mempolicy_str)
+		return -ENOMEM;
+
+	if (copy_from_user(mempolicy_str, buf, count)) {
+		kfree(mempolicy_str);
+		return -EFAULT;
+	}
+	mempolicy_str[count] = '\0';
+
+	/* strip new line characters for simplicity of handling by parser */
+	nl = strchr(mempolicy_str, '\n');
+	if (nl)
+		*nl = '\0';
+	nl = strchr(mempolicy_str, '\r');
+	if (nl)
+		*nl = '\0';
+
+	err = mpol_parse_str(mempolicy_str, &new_policy);
+	if (err) {
+		kfree(mempolicy_str);
+		return err;
+	}
+
+	/* If no error and no policy, it was 'default', clear node list */
+	if (new_policy)
+		nodes = new_policy->nodes;
+	else
+		nodes_clear(nodes);
+
+	task = get_proc_task(file_inode(file));
+	if (!task) {
+		mpol_put(new_policy);
+		kfree(mempolicy_str);
+		return -ESRCH;
+	}
+
+	err = replace_mempolicy(task, new_policy, &nodes);
+
+	put_task_struct(task);
+	kfree(mempolicy_str);
+
+	return err ? err : count;
+}
+
+const struct file_operations proc_mempolicy_operations = {
+	.read = mempolicy_read_proc,
+	.write = mempolicy_write_proc,
+	.llseek = noop_llseek,
+};
+#endif /* CONFIG_NUMA */
-- 
2.39.1



  parent reply	other threads:[~2023-11-22 21:12 UTC|newest]

Thread overview: 23+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-11-22 21:11 [RFC PATCH 00/11] mm/mempolicy: Make task->mempolicy externally modifiable via syscall and procfs Gregory Price
2023-11-22 21:11 ` [RFC PATCH 01/11] mm/mempolicy: refactor do_set_mempolicy for code re-use Gregory Price
2023-11-22 21:11 ` [RFC PATCH 04/11] mm/mempolicy: modify get_mempolicy call stack to take a task argument Gregory Price
2023-11-28 14:07   ` Michal Hocko
     [not found]     ` <ZWX1U1gCTXC+lFXn@memverge.com>
2023-11-28 14:49       ` Michal Hocko
2023-11-22 21:11 ` [RFC PATCH 07/11] mm/mempolicy: add task mempolicy syscall variants Gregory Price
2023-11-22 21:11 ` [RFC PATCH 08/11] mm/mempolicy: export replace_mempolicy for use by procfs Gregory Price
2023-11-22 21:11 ` [RFC PATCH 10/11] mm/mempolicy: mpol_parse_str should ignore trailing characters in nodelist Gregory Price
2023-11-22 21:12 ` Gregory Price [this message]
2023-11-22 21:33 ` [RFC PATCH 00/11] mm/mempolicy: Make task->mempolicy externally modifiable via syscall and procfs Andrew Morton
2023-11-22 21:35   ` Andrew Morton
2023-11-22 22:24   ` Gregory Price
2023-11-27 15:29     ` Michal Hocko
2023-11-27 16:14       ` Gregory Price
2023-11-28  9:45         ` Michal Hocko
2023-11-28 13:15           ` Gregory Price
     [not found] ` <20231122211200.31620-3-gregory.price@memverge.com>
2023-11-28 14:07   ` [RFC PATCH 02/11] mm/mempolicy: swap cond reference counting logic in do_get_mempolicy Michal Hocko
     [not found]     ` <ZWX0ytAwmOdooHdZ@memverge.com>
2023-11-28 14:28       ` Michal Hocko
     [not found] ` <20231122211200.31620-6-gregory.price@memverge.com>
2023-11-28 14:07   ` [RFC PATCH 05/11] mm/mempolicy: modify set_mempolicy_home_node to take a task argument Michal Hocko
2023-11-28 14:14     ` Gregory Price
     [not found] ` <20231122211200.31620-7-gregory.price@memverge.com>
2023-11-28 14:11   ` [RFC PATCH 06/11] mm/mempolicy: modify do_mbind to operate on task argument instead of current Michal Hocko
2023-11-28 14:51     ` Gregory Price
2023-11-28 18:08     ` Gregory Price

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=20231122211200.31620-12-gregory.price@memverge.com \
    --to=gourry.memverge@gmail.com \
    --cc=akpm@linux-foundation.org \
    --cc=arnd@arndb.de \
    --cc=bp@alien8.de \
    --cc=dave.hansen@linux.intel.com \
    --cc=gregory.price@memverge.com \
    --cc=hpa@zytor.com \
    --cc=linux-api@vger.kernel.org \
    --cc=linux-arch@vger.kernel.org \
    --cc=linux-doc@vger.kernel.org \
    --cc=linux-fsdevel@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=luto@kernel.org \
    --cc=mhocko@kernel.org \
    --cc=mingo@redhat.com \
    --cc=tglx@linutronix.de \
    --cc=tj@kernel.org \
    --cc=x86@kernel.org \
    --cc=ying.huang@intel.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