From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-oa1-f49.google.com (mail-oa1-f49.google.com [209.85.160.49]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 7E29D3A9D8D for ; Fri, 10 Apr 2026 21:07:52 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.160.49 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1775855274; cv=none; b=H0lRYoIZhRSiJFP53AaHV8z+Gf9Ovw4NAhcFyzW9XrTopa05vP7yYb/XbwqfTuBFz9ILPFuIQHO99cab6b7Sskh2112/Q0hTI17ZaHggD1YQ4N/cMZ8pBeU6106Bohsk9LdbmS+dE794rZWruokbxdRt4VtrrJckP+gRM+WpdKM= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1775855274; c=relaxed/simple; bh=3w6H/F3uWcnpSwsuRvgJu5YTfmoXTOuwZ4SFgkURGe0=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=qy0+EJ0eJbBEfHOtjlyrmVgeatWbMbl/rl8JGHKLUyg9chWMjM+ZdulFcm6bmWn8QoAizfmZoCIQewAIU02jj9sw7HW6jXurKIbm3Ll+2iVwamZQpW2GoD86EOKcTDTFIYniyGsIuaChhAfayObnrxQt/+grqj/fyp1q4GtOQP0= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=rQZUHWeo; arc=none smtp.client-ip=209.85.160.49 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="rQZUHWeo" Received: by mail-oa1-f49.google.com with SMTP id 586e51a60fabf-4042fe53946so1145418fac.3 for ; Fri, 10 Apr 2026 14:07:52 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1775855271; x=1776460071; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=LPG2mb1u3XIYy2E/F4fu0GfPhkkeNWm+Yupk0vqgtxA=; b=rQZUHWeo0zsjE+s50VZLIc5rCefbkEo4bZWsqlkxy0MxSRE/CZPE7noxSQu4fq4uUZ BpQYi4l78e+uJ0oUAZ2K3o/6FlltwtsoLhdpWC+DVjO0W1rAJ/olOVsJJ3LO54Mwu7bR l6U+Ka8ovXZda8yQ0dEhMBvSzeb7fFZnts8i+kZwhUNgzpEPrYKCEMl4UnW3uynhiOYI Tv7HlVQiR44WiitiD7AnbA8cCo42EvVqRMO7XgV/hc+FLQ8wXz0pvd6z3E/1l5iAeb9s 5KkTYTc8c4r6JXX+urqI0i/UBwoyE6qrj2PRIV8SIIJrgKSFaoTiEGRdupb3K38x7Hqk +FZw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1775855271; x=1776460071; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=LPG2mb1u3XIYy2E/F4fu0GfPhkkeNWm+Yupk0vqgtxA=; b=lyWymv+eEPe8WdLVCIr5m2gmcPUi5MHV2rHBWmXKnVONQyJLCPn2vBnf5vjZhXy1mW 7IQGr+ubCZrpPnQDWDtRId9WV4Zv62dFY058oZdLreFAwScusV0TRxPivIgLNTs23g7I 32HLNihP/1n2bRd9EEXXMMBxiqvXP1XF8eCb/yTDcDFFafGespCkyfbpVTFz9d4AQwDZ 6qB9x9GVzhJHtq3V8Zl5yQx9SQkMsrHT4VSeTZpixSsIWEpw1nhJOm9c976fLAf5oqX6 Nub9yCYLv8djgihFMsb+43JMlPBQVzWYaRANHe89jWYVcfqR6Zg8zckLCW2pUc1TQfLu 40dA== X-Forwarded-Encrypted: i=1; AJvYcCUO1UHeBEe21LcriHkHJn98QH+USmFDz9z2ONMnnR07MTjr20Z0B+uLBosYrQDqymPwkCskQR86tSaFxNE=@vger.kernel.org X-Gm-Message-State: AOJu0Yze2JCTP+kwcPbAwcRJ4GNjbcVyPV0dHS5gsNWVdsEv9XJoeB4w JJ9YMOQa6+aYR7znsL+4AnFRW7I61HkBEcnl4Cd67XXlHltnOj0N2Q01 X-Gm-Gg: AeBDiesjFftU7lc2rCIdrvWyt7JC1fO8GDu5bJl2GiZN8zLX8xabi4WpTyftDq3oaK7 CQc2W4cw/TYwpwkyxan6/D8VQx0cpMreNAVecQR6NsVUjNny4hnhCPsbc09r25sdGwx3/MySffc G7MtpfIP3lA+pTkiC42iDdsOEe7gw/jlh7lIKuXUFwCIE5tTdA2fhcs5ai8yeFMpp/1MwaVETfJ ASGbV1rEhdoA55Fgt2JU7mSKYlikO05F3pY7T/85l49T2YI6lIy7sp2pGTs82xL/DNUp/LJffYY keQd8LpiB2F7ns1lexpK3sNtB4d2463YhpQTpy5Od8Sp955vUZnE+rgGvaalaEZN6rUiLEuf1Qv GrB6OOMl9AWE1LmYKJ5nsAlBKo3yi2MPYFgOdQP5vSlanMLPDZarU7Nx7GWQ5sNfXObiEoS2le3 qhe+SLJzPRTcYRZ3ha7Rw0 X-Received: by 2002:a05:6870:6986:b0:417:2a17:285 with SMTP id 586e51a60fabf-423e10a1761mr2566760fac.30.1775855271475; Fri, 10 Apr 2026 14:07:51 -0700 (PDT) Received: from localhost ([2a03:2880:10ff:4::]) by smtp.gmail.com with ESMTPSA id 586e51a60fabf-423ddcf0376sm3027396fac.18.2026.04.10.14.07.49 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 10 Apr 2026 14:07:50 -0700 (PDT) From: Joshua Hahn To: Johannes Weiner Cc: Michal Hocko , Roman Gushchin , Shakeel Butt , Muchun Song , Andrew Morton , David Hildenbrand , Lorenzo Stoakes , "Liam R . Howlett" , Vlastimil Babka , Mike Rapoport , Suren Baghdasaryan , cgroups@vger.kernel.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org, kernel-team@meta.com Subject: [PATCH 4/8 RFC] mm/page_counter: introduce stock drain APIs Date: Fri, 10 Apr 2026 14:06:58 -0700 Message-ID: <20260410210742.550489-5-joshua.hahnjy@gmail.com> X-Mailer: git-send-email 2.52.0 In-Reply-To: <20260410210742.550489-1-joshua.hahnjy@gmail.com> References: <20260410210742.550489-1-joshua.hahnjy@gmail.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Introduce page_counter_drain_stock() and page_counter_drain_cpu() to replace memcg stock draining functions. page_counter_drain_stock() runs from drain_all_stock, which is called when the system is under memory pressure or a cgroup is dying. Because it is a rare operation, it uses work_on_cpu() to synchronously drain each online CPU's stock and synchronizes with concurrent charge/uncharge via local_lock. page_counter_drain_cpu() handles the CPU hotplug dead path, where the stock can be accessed directly without locking since the CPU is dead. Suggested-by: Johannes Weiner Signed-off-by: Joshua Hahn --- include/linux/page_counter.h | 2 ++ mm/page_counter.c | 51 ++++++++++++++++++++++++++++++++++++ 2 files changed, 53 insertions(+) diff --git a/include/linux/page_counter.h b/include/linux/page_counter.h index c7e3ab3356d20..c6772531074b5 100644 --- a/include/linux/page_counter.h +++ b/include/linux/page_counter.h @@ -111,6 +111,8 @@ static inline void page_counter_reset_watermark(struct page_counter *counter) int page_counter_enable_stock(struct page_counter *counter, unsigned int batch); void page_counter_disable_stock(struct page_counter *counter); void page_counter_free_stock(struct page_counter *counter); +void page_counter_drain_stock(struct page_counter *counter); +void page_counter_drain_cpu(struct page_counter *counter, unsigned int cpu); #if IS_ENABLED(CONFIG_MEMCG) || IS_ENABLED(CONFIG_CGROUP_DMEM) void page_counter_calculate_protection(struct page_counter *root, diff --git a/mm/page_counter.c b/mm/page_counter.c index 7be214034bfad..28c2e6442f7d3 100644 --- a/mm/page_counter.c +++ b/mm/page_counter.c @@ -12,6 +12,8 @@ #include #include #include +#include +#include #include static bool track_protection(struct page_counter *c) @@ -402,6 +404,55 @@ void page_counter_free_stock(struct page_counter *counter) counter->stock = NULL; } +static long page_counter_drain_stock_cpu(void *arg) +{ + struct page_counter *counter = arg; + struct page_counter_stock *stock; + unsigned long nr_pages; + + local_lock(&counter->stock->lock); + stock = this_cpu_ptr(counter->stock); + nr_pages = stock->nr_pages; + stock->nr_pages = 0; + local_unlock(&counter->stock->lock); + + if (nr_pages) + page_counter_cancel_hierarchy(counter, nr_pages); + + return 0; +} +/* + * Drain per-cpu stock across all online CPUs. Caller (drain_all_stock) is + * already protected by a mutex, all future callers must serialize as well. + */ +void page_counter_drain_stock(struct page_counter *counter) +{ + int cpu; + + if (!counter->stock) + return; + + cpus_read_lock(); + for_each_online_cpu(cpu) + work_on_cpu(cpu, page_counter_drain_stock_cpu, counter); + cpus_read_unlock(); +} + +void page_counter_drain_cpu(struct page_counter *counter, unsigned int cpu) +{ + struct page_counter_stock *stock; + unsigned long nr_pages; + + if (!counter->stock) + return; + + stock = per_cpu_ptr(counter->stock, cpu); + nr_pages = stock->nr_pages; + if (nr_pages) { + stock->nr_pages = 0; + page_counter_cancel_hierarchy(counter, nr_pages); + } +} #if IS_ENABLED(CONFIG_MEMCG) || IS_ENABLED(CONFIG_CGROUP_DMEM) /* -- 2.52.0