linux-mm.kvack.org archive mirror
 help / color / mirror / Atom feed
From: Zhiguo Jiang <justinjiang@vivo.com>
To: Johannes Weiner <hannes@cmpxchg.org>,
	Michal Hocko <mhocko@kernel.org>,
	Roman Gushchin <roman.gushchin@linux.dev>,
	Shakeel Butt <shakeel.butt@linux.dev>,
	Muchun Song <muchun.song@linux.dev>,
	Andrew Morton <akpm@linux-foundation.org>,
	cgroups@vger.kernel.org, linux-mm@kvack.org,
	linux-kernel@vger.kernel.org
Cc: opensource.kernel@vivo.com, Zhiguo Jiang <justinjiang@vivo.com>
Subject: [PATCH] mm: memcg supports freeing the specified zone's memory
Date: Thu, 16 Jan 2025 22:22:42 +0800	[thread overview]
Message-ID: <20250116142242.615-1-justinjiang@vivo.com> (raw)

Currently, the try_to_free_mem_cgroup_pages interface releases the
memory occupied by the memcg, which defaults to all zones in the system.
However, for multi zone systems, such as when there are both movable zone
and normal zone, it is not possible to release memory that is only in
the normal zone.

This patch is used to implement the try_to_free_mem_cgroup_pages interface
to support for releasing the specified zone's memory occupied by the
memcg in a multi zone systems, in order to optimize the memory usage of
multiple zones.

Signed-off-by: Zhiguo Jiang <justinjiang@vivo.com>
---
 mm/memcontrol-v1.c |  4 ++--
 mm/memcontrol.c    | 11 ++++++-----
 mm/vmscan.c        |  4 ++--
 3 files changed, 10 insertions(+), 9 deletions(-)

diff --git a/mm/memcontrol-v1.c b/mm/memcontrol-v1.c
index 2be6b9112808..9dc398e9d5f9
--- a/mm/memcontrol-v1.c
+++ b/mm/memcontrol-v1.c
@@ -1377,7 +1377,7 @@ static int mem_cgroup_resize_max(struct mem_cgroup *memcg,
 			continue;
 		}
 
-		if (!try_to_free_mem_cgroup_pages(memcg, 1, GFP_KERNEL,
+		if (!try_to_free_mem_cgroup_pages(memcg, 1, GFP_HIGHUSER_MOVABLE,
 				memsw ? 0 : MEMCG_RECLAIM_MAY_SWAP, NULL)) {
 			ret = -EBUSY;
 			break;
@@ -1409,7 +1409,7 @@ static int mem_cgroup_force_empty(struct mem_cgroup *memcg)
 		if (signal_pending(current))
 			return -EINTR;
 
-		if (!try_to_free_mem_cgroup_pages(memcg, 1, GFP_KERNEL,
+		if (!try_to_free_mem_cgroup_pages(memcg, 1, GFP_HIGHUSER_MOVABLE,
 						  MEMCG_RECLAIM_MAY_SWAP, NULL))
 			nr_retries--;
 	}
diff --git a/mm/memcontrol.c b/mm/memcontrol.c
index 46f8b372d212..e0b92edb2f3e
--- a/mm/memcontrol.c
+++ b/mm/memcontrol.c
@@ -1945,7 +1945,7 @@ static unsigned long reclaim_high(struct mem_cgroup *memcg,
 
 		psi_memstall_enter(&pflags);
 		nr_reclaimed += try_to_free_mem_cgroup_pages(memcg, nr_pages,
-							gfp_mask,
+							gfp_mask | __GFP_MOVABLE | __GFP_HIGHMEM,
 							MEMCG_RECLAIM_MAY_SWAP,
 							NULL);
 		psi_memstall_leave(&pflags);
@@ -2253,7 +2253,8 @@ int try_charge_memcg(struct mem_cgroup *memcg, gfp_t gfp_mask,
 
 	psi_memstall_enter(&pflags);
 	nr_reclaimed = try_to_free_mem_cgroup_pages(mem_over_limit, nr_pages,
-						    gfp_mask, reclaim_options, NULL);
+						    gfp_mask | __GFP_MOVABLE | __GFP_HIGHMEM,
+						    reclaim_options, NULL);
 	psi_memstall_leave(&pflags);
 
 	if (mem_cgroup_margin(mem_over_limit) >= nr_pages)
@@ -4109,7 +4110,7 @@ static ssize_t memory_high_write(struct kernfs_open_file *of,
 		}
 
 		reclaimed = try_to_free_mem_cgroup_pages(memcg, nr_pages - high,
-					GFP_KERNEL, MEMCG_RECLAIM_MAY_SWAP, NULL);
+					GFP_HIGHUSER_MOVABLE, MEMCG_RECLAIM_MAY_SWAP, NULL);
 
 		if (!reclaimed && !nr_retries--)
 			break;
@@ -4158,7 +4159,7 @@ static ssize_t memory_max_write(struct kernfs_open_file *of,
 
 		if (nr_reclaims) {
 			if (!try_to_free_mem_cgroup_pages(memcg, nr_pages - max,
-					GFP_KERNEL, MEMCG_RECLAIM_MAY_SWAP, NULL))
+					GFP_HIGHUSER_MOVABLE, MEMCG_RECLAIM_MAY_SWAP, NULL))
 				nr_reclaims--;
 			continue;
 		}
@@ -4351,7 +4352,7 @@ static ssize_t memory_reclaim(struct kernfs_open_file *of, char *buf,
 			lru_add_drain_all();
 
 		reclaimed = try_to_free_mem_cgroup_pages(memcg,
-					batch_size, GFP_KERNEL,
+					batch_size, GFP_HIGHUSER_MOVABLE,
 					reclaim_options,
 					swappiness == -1 ? NULL : &swappiness);
 
diff --git a/mm/vmscan.c b/mm/vmscan.c
index 5b626b4f38af..9d198bc4e543
--- a/mm/vmscan.c
+++ b/mm/vmscan.c
@@ -6610,8 +6610,8 @@ unsigned long try_to_free_mem_cgroup_pages(struct mem_cgroup *memcg,
 		.nr_to_reclaim = max(nr_pages, SWAP_CLUSTER_MAX),
 		.proactive_swappiness = swappiness,
 		.gfp_mask = (current_gfp_context(gfp_mask) & GFP_RECLAIM_MASK) |
-				(GFP_HIGHUSER_MOVABLE & ~GFP_RECLAIM_MASK),
-		.reclaim_idx = MAX_NR_ZONES - 1,
+				(gfp_mask & (__GFP_MOVABLE | __GFP_HIGHMEM)),
+		.reclaim_idx = gfp_zone(gfp_mask),
 		.target_mem_cgroup = memcg,
 		.priority = DEF_PRIORITY,
 		.may_writepage = !laptop_mode,
-- 
2.39.0



             reply	other threads:[~2025-01-16 14:24 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-01-16 14:22 Zhiguo Jiang [this message]
2025-01-16 14:36 ` Michal Hocko
2025-01-17  4:41   ` zhiguojiang
2025-01-17  9:33     ` Michal Hocko
2025-01-17 10:25       ` zhiguojiang
2025-01-17 11:43         ` Michal Hocko
2025-01-20  1:22           ` zhiguojiang
2025-01-20  9:40             ` 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=20250116142242.615-1-justinjiang@vivo.com \
    --to=justinjiang@vivo.com \
    --cc=akpm@linux-foundation.org \
    --cc=cgroups@vger.kernel.org \
    --cc=hannes@cmpxchg.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=mhocko@kernel.org \
    --cc=muchun.song@linux.dev \
    --cc=opensource.kernel@vivo.com \
    --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