diff mbox series

[RFC,1/2] mm/memcontrol: add a new cgroup file memory.nomlock

Message ID 20241215073415.88961-2-laoar.shao@gmail.com (mailing list archive)
State New
Headers show
Series memcg: add nomlock to avoid folios beling mlocked in a memcg | expand

Commit Message

Yafang Shao Dec. 15, 2024, 7:34 a.m. UTC
Add a new cgroup file memory.nomlock to control the behavior of mlock.
This is a preparation of the followup patch.

  0: Disable nomlock
  1: Enable nomlock

Signed-off-by: Yafang Shao <laoar.shao@gmail.com>
---
 include/linux/memcontrol.h |  3 +++
 mm/memcontrol.c            | 35 +++++++++++++++++++++++++++++++++++
 2 files changed, 38 insertions(+)
diff mbox series

Patch

diff --git a/include/linux/memcontrol.h b/include/linux/memcontrol.h
index b28180269e75..b3a4e28ae0f9 100644
--- a/include/linux/memcontrol.h
+++ b/include/linux/memcontrol.h
@@ -220,6 +220,9 @@  struct mem_cgroup {
 	 */
 	bool oom_group;
 
+	/* Folios won't be mlocked in this memcg. */
+	bool nomlock;
+
 	int swappiness;
 
 	/* memory.events and memory.events.local */
diff --git a/mm/memcontrol.c b/mm/memcontrol.c
index 7b3503d12aaf..08bb65c1612f 100644
--- a/mm/memcontrol.c
+++ b/mm/memcontrol.c
@@ -4374,6 +4374,35 @@  static ssize_t memory_reclaim(struct kernfs_open_file *of, char *buf,
 	return nbytes;
 }
 
+static int memory_nomlock_show(struct seq_file *m, void *v)
+{
+	struct mem_cgroup *memcg = mem_cgroup_from_seq(m);
+
+	seq_printf(m, "%d\n", READ_ONCE(memcg->nomlock));
+	return 0;
+}
+
+static ssize_t memory_nomlock_write(struct kernfs_open_file *of,
+				   char *buf, size_t nbytes, loff_t off)
+{
+	struct mem_cgroup *memcg = mem_cgroup_from_css(of_css(of));
+	int ret, nomlock;
+
+	buf = strstrip(buf);
+	if (!buf)
+		return -EINVAL;
+
+	ret = kstrtoint(buf, 0, &nomlock);
+	if (ret)
+		return ret;
+
+	if (nomlock != 0 && nomlock != 1)
+		return -EINVAL;
+
+	WRITE_ONCE(memcg->nomlock, nomlock);
+	return nbytes;
+}
+
 static struct cftype memory_files[] = {
 	{
 		.name = "current",
@@ -4445,6 +4474,12 @@  static struct cftype memory_files[] = {
 		.flags = CFTYPE_NS_DELEGATABLE,
 		.write = memory_reclaim,
 	},
+	{
+		.name = "nomlock",
+		.flags = CFTYPE_NOT_ON_ROOT,
+		.seq_show = memory_nomlock_show,
+		.write = memory_nomlock_write,
+	},
 	{ }	/* terminate */
 };