mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Shakeel Butt <shakeel.butt@linux.dev>
To: Tejun Heo <tj@kernel.org>, Andrew Morton <akpm@linux-foundation.org>
Cc: Johannes Weiner <hannes@cmpxchg.org>,
	Michal Koutny <mkoutny@suse.com>,
	Christian Brauner <brauner@kernel.org>,
	Michal Hocko <mhocko@kernel.org>,
	Roman Gushchin <roman.gushchin@linux.dev>,
	Muchun Song <muchun.song@linux.dev>, Jan Kara <jack@suse.cz>,
	Matthew Wilcox <willy@infradead.org>,
	David Hildenbrand <david@kernel.org>,
	Vlastimil Babka <vbabka@kernel.org>, Zi Yan <ziy@nvidia.com>,
	Baolin Wang <baolin.wang@linux.alibaba.com>,
	Nico Pache <nico.pache@linux.dev>,
	Usama Arif <usama.arif@linux.dev>,
	Kiryl Shutsemau <kas@kernel.org>,
	Lorenzo Stoakes <ljs@kernel.org>,
	Suren Baghdasaryan <surenb@google.com>,
	Meta kernel team <kernel-team@meta.com>,
	cgroups@vger.kernel.org, linux-mm@kvack.org,
	linux-fsdevel@vger.kernel.org, linux-btrfs@vger.kernel.org,
	linux-kernel@vger.kernel.org
Subject: [RFC PATCH 1/7] memcg: add helpers to handle memory.high overage in long operations
Date: Sat, 21 Nov 2026 18:47:31 -0800	[thread overview]
Message-ID: <20261122024737.2741569-2-shakeel.butt@linux.dev> (raw)
In-Reply-To: <20261122024737.2741569-1-shakeel.butt@linux.dev>

Since commit c9afe31ec443 ("memcg: synchronously enforce memory.high
for large overcharges"), a charge that can block handles memory.high
overage right away once more than a batch is pending. It reclaims and
sleeps with whatever locks the caller holds, so tasks in other cgroups
that need those locks wait too.

A later patch removes that step. Operations that can charge a lot in
one call then need another place to handle the overage: a point in
their loop where they hold no locks, or only the locks of their file.

Add two helpers for them. mem_cgroup_over_high_pending() says whether
more than a batch is pending, the threshold the synchronous step uses.
mem_cgroup_over_high_throttle() handles the overage if so; its comment
says where to call it and which gfp mask to pass. Export
__mem_cgroup_handle_over_high() for filesystems built as modules.

Signed-off-by: Shakeel Butt <shakeel.butt@linux.dev>
---
 include/linux/memcontrol.h | 34 ++++++++++++++++++++++++++++++++++
 mm/memcontrol.c            |  1 +
 2 files changed, 35 insertions(+)

diff --git a/include/linux/memcontrol.h b/include/linux/memcontrol.h
index 980358bc6f49..77115fe1873e 100644
--- a/include/linux/memcontrol.h
+++ b/include/linux/memcontrol.h
@@ -942,6 +942,31 @@ static inline void mem_cgroup_handle_over_high(gfp_t gfp_mask)
 		__mem_cgroup_handle_over_high(gfp_mask);
 }
 
+/*
+ * True if the current task has more than a batch of memory.high or
+ * memory.swap.high overage pending. Each charge that finds overage
+ * counts at least a batch.
+ */
+static inline bool mem_cgroup_over_high_pending(void)
+{
+	return unlikely(current->memcg_nr_pages_over_high > MEMCG_CHARGE_BATCH);
+}
+
+/*
+ * Handle the current task's memory.high overage if more than a batch is
+ * pending, for operations that can charge a lot before they return to
+ * userspace. The task may reclaim and sleep here, so call it only where
+ * other tasks won't wait for it: with no locks held anywhere up the call
+ * chain, or only the locks of the file being worked on. Pass GFP_KERNEL
+ * if no filesystem locks are held, and mapping_gfp_constraint() in file
+ * read and write paths.
+ */
+static inline void mem_cgroup_over_high_throttle(gfp_t gfp_mask)
+{
+	if (mem_cgroup_over_high_pending())
+		__mem_cgroup_handle_over_high(gfp_mask);
+}
+
 unsigned long mem_cgroup_get_max(const struct mem_cgroup *memcg);
 
 void mem_cgroup_print_oom_context(const struct mem_cgroup *memcg,
@@ -1376,6 +1401,15 @@ static inline void mem_cgroup_handle_over_high(gfp_t gfp_mask)
 {
 }
 
+static inline bool mem_cgroup_over_high_pending(void)
+{
+	return false;
+}
+
+static inline void mem_cgroup_over_high_throttle(gfp_t gfp_mask)
+{
+}
+
 static inline struct mem_cgroup *mem_cgroup_get_oom_group(
 	struct task_struct *victim, struct mem_cgroup *oom_domain)
 {
diff --git a/mm/memcontrol.c b/mm/memcontrol.c
index 091401cecf39..6d147153bd06 100644
--- a/mm/memcontrol.c
+++ b/mm/memcontrol.c
@@ -2707,6 +2707,7 @@ void __mem_cgroup_handle_over_high(gfp_t gfp_mask)
 out:
 	css_put(&memcg->css);
 }
+EXPORT_SYMBOL_GPL(__mem_cgroup_handle_over_high);
 
 /*
  * Called after @nr_pages were charged to @memcg. If @memcg or one of its
-- 
2.53.1-Meta


  reply	other threads:[~2026-11-22  2:47 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-22  2:47 [RFC PATCH 0/7] memcg: effective high limit that avoids isolation issues Shakeel Butt
2026-11-22  2:47 ` Shakeel Butt [this message]
2026-11-22  2:47 ` [RFC PATCH 2/7] mm/gup: handle memory.high overage while populating Shakeel Butt
2026-11-22  2:47 ` [RFC PATCH 3/7] mm, fs: handle memory.high overage in buffered reads and writes Shakeel Butt
2026-11-22  2:47 ` [RFC PATCH 4/7] mm/shmem: handle memory.high overage in fallocate and reads Shakeel Butt
2026-11-22  2:47 ` [RFC PATCH 5/7] mm/khugepaged: handle memory.high overage in MADV_COLLAPSE Shakeel Butt
2026-11-22  2:47 ` [RFC PATCH 6/7] memcg: remove synchronous memory.high enforcement Shakeel Butt
2026-11-22  2:47 ` [RFC PATCH 7/7] selftests/cgroup: make test_memcg_high_sync check that throttling holds Shakeel Butt

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=20261122024737.2741569-2-shakeel.butt@linux.dev \
    --to=shakeel.butt@linux.dev \
    --cc=akpm@linux-foundation.org \
    --cc=baolin.wang@linux.alibaba.com \
    --cc=brauner@kernel.org \
    --cc=cgroups@vger.kernel.org \
    --cc=david@kernel.org \
    --cc=hannes@cmpxchg.org \
    --cc=jack@suse.cz \
    --cc=kas@kernel.org \
    --cc=kernel-team@meta.com \
    --cc=linux-btrfs@vger.kernel.org \
    --cc=linux-fsdevel@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=ljs@kernel.org \
    --cc=mhocko@kernel.org \
    --cc=mkoutny@suse.com \
    --cc=muchun.song@linux.dev \
    --cc=nico.pache@linux.dev \
    --cc=roman.gushchin@linux.dev \
    --cc=surenb@google.com \
    --cc=tj@kernel.org \
    --cc=usama.arif@linux.dev \
    --cc=vbabka@kernel.org \
    --cc=willy@infradead.org \
    --cc=ziy@nvidia.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

all inboxes | Powered by JetHome®