From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=95.215.58.136 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1795315678; b=Df8yJ+zicjmSRGCtDOdwL/ZfQyXbJDD6HvCMOF2+sLs6zciPFX4zNeuGCIpSnUlwjGEH OuMTZux4eWjTaU0uENyWoEmkGVGKsqZTrxd8GjTuwOOBsmqOZjXzqns3v87q7nxfN4J3p 6YzhN6XVQZgGZjBDCRYhkw4kdmbqBMEZKMxtcJNGBTt913GVNCJpyhQYeE9bRwxb1TvJD Df1L8h+3KVQpPwz/Lc1RxqBSDxQP6fLUVCItT1tnq0LKYAvrwIYmP0ZILx9V6LG67fPZH 8MTirR7ReMHs0LYSQfS5sVRG5jLGD0+yVj+R3L+MpMZu0yLTp3Jw05EuF+7su11jMbw== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795315678; h=DMARC-Filter:X-Envelope-To:DKIM-Signature:X-Envelope-To:Received: X-Mizu-Trace-ID:X-Migadu-Flow:From:To:Cc:Subject:Date:Message-ID: X-Mailer:In-Reply-To:References:MIME-Version: Content-Transfer-Encoding; bh=UXpp8sp/GRVRsUtSepKaptGcuvVFgYldxI029IWKwww=; b=wqFjXYRerPqECKVWunoICkgxeTUHB9ZFwyzQ7+lW9DHpDMrt0hfD3CtwY8nayj1SOY6q WVOErYJfQtwV+zbzIZcpWkQi8ZpsFGKjfz0hab53WItqcZFPxDyN8FT6hqZfHfEk/eT1y tE5+XOlLFAQtunetKGcLCpHvcvhJzIdyMRSbWbkv3+S0rZPgHctwwEZcK74grdTTX1AnU fJ2tUecrAA/zF7F+xWR6iFpJEvUN3uHM00fKr1x0kPjzwF4Gi2ZwwhvV+0ni4UlPvuZmZ Mn5W6vS0xOkX2n2ISqeecBOw/MiiSv3CFt+4Z6qGoAdhJcPKghGj7zbc5GqlUqprvvQ== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=linux.dev; spf=pass smtp.mailfrom=linux.dev; dkim=pass header.d=linux.dev header.i=@linux.dev header.a=rsa-sha256 header.s=key1 header.b=SB3XWdtX; arc=none smtp.remote-ip=95.215.58.136 Received: from mta1.migadu.com (out-136.mta1.migadu.com [95.215.58.136]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 057C8606A333 for ; Sun, 22 Nov 2026 02:47:57 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 057C8606A333 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.dev Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key, unprotected) header.d=linux.dev header.i=@linux.dev header.a=rsa-sha256 header.s=key1 header.b=SB3XWdtX X-Envelope-To: linux-btrfs@vger.kernel.org DKIM-Signature: a=rsa-sha256; bh=4VmPcRtrp5VsXyo2a9LQORInZeHKK19ZabzxdfHobDU=; c=simple/simple; d=linux.dev; h=from:to:subject:date:message-id:mime-version:content-type; s=key1; t=1795315676; v=1; x=1795920476; b=SB3XWdtX9vVAhV2yqIyXR+Z3FAT3/zOTjYraEr2dZNCwA6Bf6Tyn/9wM94ZqX1O3zQFQDyHK w+qv72ybnzH9+o2vqiQ+uVhC/3MoHj0fm0ZzoW+xl/oBGO1XZ8JfBvM2xoNMl5z7VKJu3Odj5Cv /kJz20WwIl4HstMbzIeJdiL0= X-Envelope-To: linux-btrfs@vger.kernel.org Received: by smtp.migadu.com with ESMTPS id 6514b70c0cb31dcf; Sun, 22 Nov 2026 02:47:56 +0000 X-Mizu-Trace-ID: 6514b70c0cb31dcf X-Migadu-Flow: FLOW_OUT From: Shakeel Butt To: Tejun Heo , Andrew Morton Cc: Johannes Weiner , Michal Koutny , Christian Brauner , Michal Hocko , Roman Gushchin , Muchun Song , Jan Kara , Matthew Wilcox , David Hildenbrand , Vlastimil Babka , Zi Yan , Baolin Wang , Nico Pache , Usama Arif , Kiryl Shutsemau , Lorenzo Stoakes , Suren Baghdasaryan , Meta kernel team , 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 Message-ID: <20261122024737.2741569-2-shakeel.butt@linux.dev> X-Mailer: git-send-email 2.53.1 In-Reply-To: <20261122024737.2741569-1-shakeel.butt@linux.dev> References: <20261122024737.2741569-1-shakeel.butt@linux.dev> X-Mailing-List: linux-btrfs@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 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 --- 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