linux-mm.kvack.org archive mirror
 help / color / mirror / Atom feed
From: Yafang Shao <laoar.shao@gmail.com>
To: hannes@cmpxchg.org, mhocko@kernel.org, roman.gushchin@linux.dev,
	shakeel.butt@linux.dev, muchun.song@linux.dev,
	akpm@linux-foundation.org
Cc: linux-mm@kvack.org, Yafang Shao <laoar.shao@gmail.com>
Subject: [RFC PATCH 1/2] mm/memcontrol: add a new cgroup file memory.nomlock
Date: Sun, 15 Dec 2024 15:34:14 +0800	[thread overview]
Message-ID: <20241215073415.88961-2-laoar.shao@gmail.com> (raw)
In-Reply-To: <20241215073415.88961-1-laoar.shao@gmail.com>

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 --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 */
 };
 
-- 
2.43.5



  reply	other threads:[~2024-12-15  7:34 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-12-15  7:34 [RFC PATCH 0/2] memcg: add nomlock to avoid folios beling mlocked in a memcg Yafang Shao
2024-12-15  7:34 ` Yafang Shao [this message]
2024-12-15  7:34 ` [RFC PATCH 2/2] mm: Add support for " Yafang Shao
2024-12-20 10:23 ` [RFC PATCH 0/2] memcg: add " Michal Hocko
2024-12-20 11:52   ` Yafang Shao
2024-12-21  7:21     ` Michal Hocko
2024-12-22  2:34       ` Yafang Shao
2024-12-25  2:23         ` Yafang Shao
2025-01-06 12:30           ` Michal Hocko
2025-01-06 14:04             ` Yafang Shao
2025-01-07  8:39               ` Michal Hocko
2025-01-07  9:43                 ` Yafang Shao
2025-01-06 12:28         ` Michal Hocko
2025-01-06 13:59           ` Yafang Shao
2025-01-07 10:04             ` Michal Hocko

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=20241215073415.88961-2-laoar.shao@gmail.com \
    --to=laoar.shao@gmail.com \
    --cc=akpm@linux-foundation.org \
    --cc=hannes@cmpxchg.org \
    --cc=linux-mm@kvack.org \
    --cc=mhocko@kernel.org \
    --cc=muchun.song@linux.dev \
    --cc=roman.gushchin@linux.dev \
    --cc=shakeel.butt@linux.dev \
    /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