linux-mm.kvack.org archive mirror
 help / color / mirror / Atom feed
From: Mina Almasry <almasrymina@google.com>
To: Huang Ying <ying.huang@intel.com>,
	Yang Shi <yang.shi@linux.alibaba.com>,
	 Yosry Ahmed <yosryahmed@google.com>,
	Tim Chen <tim.c.chen@linux.intel.com>,
	weixugc@google.com,  shakeelb@google.com, gthelen@google.com,
	fvdl@google.com,  Johannes Weiner <hannes@cmpxchg.org>,
	Michal Hocko <mhocko@kernel.org>,
	 Roman Gushchin <roman.gushchin@linux.dev>,
	Muchun Song <songmuchun@bytedance.com>,
	 Andrew Morton <akpm@linux-foundation.org>
Cc: Mina Almasry <almasrymina@google.com>,
	linux-kernel@vger.kernel.org,  cgroups@vger.kernel.org,
	linux-mm@kvack.org
Subject: [RFC PATCH v1] mm: Add memory.demote for proactive demotion only
Date: Tue, 22 Nov 2022 12:38:46 -0800	[thread overview]
Message-ID: <20221122203850.2765015-2-almasrymina@google.com> (raw)
In-Reply-To: <20221122203850.2765015-1-almasrymina@google.com>

Add the proactive demotion interface memory.demote. This interface can
be used as follows:

echo "1m" > memory.demote

At this command the kernel will attempt to demote 1m of memory from this
cgroup. The kernel may not be able to demote the full amount requested
by the userspace and in that case EAGAIN would be returned to the user
(similar to memory.reclaim).

The kernel will only attempt to demote pages with this interface. It
will not attempt any other kind of reclaim (swap, writeback or
reclaiming clean file pages).

Signed-off-by: Mina Almasry <almasrymina@google.com>
---
 mm/memcontrol.c | 38 ++++++++++++++++++++++++++++++++++++++
 mm/vmscan.c     | 18 ++++++++++++++----
 2 files changed, 52 insertions(+), 4 deletions(-)

diff --git a/mm/memcontrol.c b/mm/memcontrol.c
index fd4ff1c865a2..427c79e467eb 100644
--- a/mm/memcontrol.c
+++ b/mm/memcontrol.c
@@ -6623,6 +6623,39 @@ static ssize_t memory_reclaim(struct kernfs_open_file *of, char *buf,
 	return nbytes;
 }

+static ssize_t memory_demote(struct kernfs_open_file *of, char *buf,
+			     size_t nbytes, loff_t off)
+{
+	struct mem_cgroup *memcg = mem_cgroup_from_css(of_css(of));
+	unsigned int nr_retries = MAX_RECLAIM_RETRIES;
+	unsigned long nr_to_demote, nr_demoted = 0;
+	unsigned int reclaim_options = MEMCG_RECLAIM_ONLY_DEMOTE;
+	int err;
+
+	buf = strstrip(buf);
+	err = page_counter_memparse(buf, "", &nr_to_demote);
+	if (err)
+		return err;
+
+	while (nr_demoted < nr_to_demote) {
+		unsigned long demoted;
+
+		if (signal_pending(current))
+			return -EINTR;
+
+		demoted = try_to_free_mem_cgroup_pages(
+			memcg, nr_to_demote - nr_demoted, GFP_KERNEL,
+			reclaim_options);
+
+		if (!demoted && !nr_retries--)
+			return -EAGAIN;
+
+		nr_demoted += demoted;
+	}
+
+	return nbytes;
+}
+
 static struct cftype memory_files[] = {
 	{
 		.name = "current",
@@ -6691,6 +6724,11 @@ static struct cftype memory_files[] = {
 		.flags = CFTYPE_NS_DELEGATABLE,
 		.write = memory_reclaim,
 	},
+	{
+		.name = "demote",
+		.flags = CFTYPE_NS_DELEGATABLE,
+		.write = memory_demote,
+	},
 	{ }	/* terminate */
 };

diff --git a/mm/vmscan.c b/mm/vmscan.c
index dea05ad8ece5..8c1f5416d789 100644
--- a/mm/vmscan.c
+++ b/mm/vmscan.c
@@ -1657,12 +1657,13 @@ static unsigned int shrink_folio_list(struct list_head *folio_list,
 	LIST_HEAD(demote_folios);
 	unsigned int nr_reclaimed = 0;
 	unsigned int pgactivate = 0;
-	bool do_demote_pass;
+	bool do_demote_pass, only_demote_pass;
 	struct swap_iocb *plug = NULL;

 	memset(stat, 0, sizeof(*stat));
 	cond_resched();
 	do_demote_pass = can_demote(pgdat->node_id, sc);
+	only_demote_pass = sc->demotion == 2;

 retry:
 	while (!list_empty(folio_list)) {
@@ -2091,10 +2092,19 @@ static unsigned int shrink_folio_list(struct list_head *folio_list,
 	nr_reclaimed += demote_folio_list(&demote_folios, pgdat);
 	/* Folios that could not be demoted are still in @demote_folios */
 	if (!list_empty(&demote_folios)) {
-		/* Folios which weren't demoted go back on @folio_list for retry: */
+		/*
+		 * Folios which weren't demoted go back on @folio_list.
+		 */
 		list_splice_init(&demote_folios, folio_list);
-		do_demote_pass = false;
-		goto retry;
+
+		/*
+		 * goto retry to reclaim the undemoted folios in folio_list if
+		 * desired.
+		 */
+		if (!only_demote_pass) {
+			do_demote_pass = false;
+			goto retry;
+		}
 	}

 	pgactivate = stat->nr_activate[0] + stat->nr_activate[1];
--
2.38.1.584.g0f3c55d4c2-goog


  reply	other threads:[~2022-11-22 20:39 UTC|newest]

Thread overview: 29+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-11-22 20:38 [RFC PATCH V1] mm: Disable demotion from proactive reclaim Mina Almasry
2022-11-22 20:38 ` Mina Almasry [this message]
2022-11-22 20:38 ` [RFC PATCH v1 3/4] mm: Fix demotion-only scanning anon pages Mina Almasry
2022-11-24  5:27   ` Huang, Ying
2022-11-22 20:38 ` [RFC PATCH v1 4/4] mm: Add nodes= arg to memory.demote Mina Almasry
2022-11-23 18:00 ` [RFC PATCH V1] mm: Disable demotion from proactive reclaim Johannes Weiner
2022-11-23 21:20   ` Mina Almasry
2022-11-23 21:35     ` Yosry Ahmed
2022-11-23 22:30       ` Johannes Weiner
2022-11-23 23:47         ` Yosry Ahmed
2022-11-23 21:58     ` Johannes Weiner
2022-11-23 22:37       ` Mina Almasry
2022-11-24  5:51       ` Huang, Ying
2022-11-28 22:24         ` Yang Shi
2022-11-29  0:53           ` Huang, Ying
2022-11-29 17:27             ` Yang Shi
2022-11-30  5:31               ` Huang, Ying
2022-11-30 18:49                 ` Yang Shi
2022-12-01  1:51                   ` Huang, Ying
2022-12-01 22:45                     ` Yang Shi
2022-12-02  1:57                       ` Huang, Ying
2022-11-29 18:08         ` Johannes Weiner
2022-11-30  3:55           ` Huang, Ying
2022-12-01 20:40             ` Mina Almasry
2022-12-02  2:01               ` Huang, Ying
2022-12-02  2:06                 ` Mina Almasry
2022-11-30  2:14         ` Mina Almasry
2022-11-30  5:39           ` Huang, Ying
2022-11-30  6:06             ` Mina Almasry

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=20221122203850.2765015-2-almasrymina@google.com \
    --to=almasrymina@google.com \
    --cc=akpm@linux-foundation.org \
    --cc=cgroups@vger.kernel.org \
    --cc=fvdl@google.com \
    --cc=gthelen@google.com \
    --cc=hannes@cmpxchg.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=mhocko@kernel.org \
    --cc=roman.gushchin@linux.dev \
    --cc=shakeelb@google.com \
    --cc=songmuchun@bytedance.com \
    --cc=tim.c.chen@linux.intel.com \
    --cc=weixugc@google.com \
    --cc=yang.shi@linux.alibaba.com \
    --cc=ying.huang@intel.com \
    --cc=yosryahmed@google.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