From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-ot1-f54.google.com (mail-ot1-f54.google.com [209.85.210.54]) (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 5E35422F74A for ; Fri, 10 Apr 2026 21:07:47 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.54 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1775855268; cv=none; b=htbEpGgSjJ0Vrjz0u8kdaR9LXXZZc2VgYKNI+qE3jqOEabqOqqY5eCfPvhyY+4hLJHn7d0fHA9eoWVdPhjaHGnwXQcHIPGCc1AN94W1o2CnIKdcY6E99cSeQ4GZIxPqfzGK2BJEtGI2Q/MbZRa5eClQqz0yLzK1rsEnw9l8PGpw= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1775855268; c=relaxed/simple; bh=u9ErvmZeT2BqDjl0OuopUNGSnpUsL3K8g6ZSAxdifZg=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=UAO3qJZEi8k8iwa024SoO1gR7qL6Z3kCL9fLktafd/j5xWdwsZgjBE5RF/r7NIjMZOnaFwFyTgM/P9Y9FwuRWRdTXQZPNqeKQDBfWlMy9NQDP1M60LzAc1825nHLJgqO6C6t+QHDLM/BWqNF6Spxc/HVngItRg74ToafYheAf5A= 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=kDtwKJ1q; arc=none smtp.client-ip=209.85.210.54 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="kDtwKJ1q" Received: by mail-ot1-f54.google.com with SMTP id 46e09a7af769-7d9b21d1461so2377234a34.1 for ; Fri, 10 Apr 2026 14:07:47 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1775855266; x=1776460066; 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=Ra7lyKTz0FXclo/ZAhEfnaPWl8hR2IKs3cjWe/KBKyU=; b=kDtwKJ1qEnskAltVCx0rxmJUKWdMJJnxsWhBeqxd/jncd3cngRWiRWp3HTNBYhK+j5 OD3aSqf9+6GnbqxD5ZTHWPwcHLkbgwnyQcBj4eEuHbTOFB2r+m+igRx3HKRhf3LhoGRV OY5SPdcNblunCkUP0KOqnasi+lW0e0kr8fukvfJ50P7b/pH0rt7CRn7kCbq5niA7d3xl eMn1WqbcbQ+R76v5cA0NJEN25uxfvBh7fhglfdtfTOBIQEh3nBZSrQjsXOqzX83vH9Pv dLnd64oTvz8/6HpDUoggTLK4LQzjzEMFMSSe28toUTPHxWr/ghC2/Fi+5bxLDbeUVixh SReQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1775855266; x=1776460066; 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=Ra7lyKTz0FXclo/ZAhEfnaPWl8hR2IKs3cjWe/KBKyU=; b=GBfe3IjFioKGgKD/FImKpM8l4n+QXvO/l4aUO8svpPDN6fKExsgJvokAiidec2iaBO 2BUeE9EgWp5UyDGRUzaYy1RmcQ8OTAFiOqbCE906saMvukfCA0au2hg+QS/JhkYtFUEy rSUwAOVN8BZFVMC31kadNIxRBAeqFcdabO4JWPMudJNQ0QL5nLGMbhJ3SvaEfb47rL0m D+MTzxGMTVKNsIeDDwQ6GDANhV1K/gnUQksdJnf4bg/kjP2YXezl90srPVAe4kr8g3ln 6lVTTpa/Dn1q3YR/OgMiqNuaSk6l7fitMf22Zr/fTQ/j9qPIy69/ZF7RW3wMTeYExOxT Y2sA== X-Forwarded-Encrypted: i=1; AJvYcCXeKJa1+EYukDuRTfAtsPic/nfsNAbmCLoVS0EPGpamzZAF3EdulglJCycCcOiyw2ggEu208qh6ELvJ3yo=@vger.kernel.org X-Gm-Message-State: AOJu0Yyyz8RUaQUxXnru8bcGojVu7CBMZ4uYdO74QeUcYHHu/8Bcmrcl ppbqvsnB5m9+zdQiDZkh9xFg9ycf5dkUIjbzYorUw2YgDpeIG0nLk4P0 X-Gm-Gg: AeBDieu+F7x7fZQFvYz83TMYOA0Gwpwcp4+cQAmQd3XRp42ckG5cKfsYs4N4T2n/u8q LvA02ExWpL6hZeWSFjHvSItX/OSirIb85PmD0TbwN4sLlW6/HnB7QmxYoIWYWoecbcjve8uCVPD cR9ktu7DsnSj3ieRbrMwJA4zCvVPBhnebFGQD2eGhkTIL7eGgAhk2NXhnswEWGNlFfwtT5mdjZl pNA4VWX1p2svBwKqzykE/b/IXjzJ9sBDRsWBWKqMeQ86MfURwaNhin7Ur0zFDPSpUbcGR1HcXQp oMY9sT4KgTBuz+rUULkNkJOlbZtY/PnF6Ad2RGpQSHWFsLy+1G956cc0olw9sRUyVg47cZpw7U3 kvQsOmk9qidkx+DKZ+/ik0lLuE00lkW/O9LPiJE+AAL2SV3ZNTthKqIJaBKP37c9dA89HGlPYfD aEGKWLYzJpUNB1iaesjL3Mx8VhdnxAPDOx X-Received: by 2002:a05:6830:8312:b0:7d8:7da0:7d8f with SMTP id 46e09a7af769-7dc177a646fmr3456536a34.16.1775855266401; Fri, 10 Apr 2026 14:07:46 -0700 (PDT) Received: from localhost ([2a03:2880:10ff:72::]) by smtp.gmail.com with ESMTPSA id 46e09a7af769-7dc269d52ffsm3136947a34.26.2026.04.10.14.07.45 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 10 Apr 2026 14:07:46 -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 1/8 RFC] mm/page_counter: introduce per-page_counter stock Date: Fri, 10 Apr 2026 14:06:55 -0700 Message-ID: <20260410210742.550489-2-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 In order to avoid expensive hierarchy walks on every memcg charge and limit check, memcontrol uses per-cpu stocks (memcg_stock_pcp) to cache pre-charged pages and introduce a fast path to try_charge_memcg. However, there are a few quirks with the current implementation that could be improved upon. First, each memcg_stock_pcp can only cache the charges of 7 memcgs (defined as NR_MEMCG_STOCK), which means that once a CPU starts handling the charging of more than 7 memcgs, it randomly selects a victim memcg to evict and drain from the cpu, which can cause unnecessarily increased latencies and thrashing as memcgs continually evict each others' stock. Second, stock is tightly coupled with memcg, which means that all page counters in a memcg share the same resource. This may simplify some of the charging logic, but it prevents new page counters from being added and using a separate stock. We can address these concerns by pushing the concept of stock down to the page_counter level, which addresses the random eviction problem by getting rid of the 7 slot limit, and makes enabling separate stock caches for other page_counters simpler. Introduce a generic per-cpu stock directly in struct page_counter. Stock can optionally be enabled per-page_counter, limiting the overhead increase for page_counters who do not benefit greatly from caching charges. This patch introduces the page_counter_stock struct and its enable/disable/free functions, but does not use these yet. Suggested-by: Johannes Weiner Signed-off-by: Joshua Hahn --- include/linux/page_counter.h | 13 ++++++++ mm/page_counter.c | 60 ++++++++++++++++++++++++++++++++++++ 2 files changed, 73 insertions(+) diff --git a/include/linux/page_counter.h b/include/linux/page_counter.h index d649b6bbbc871..c7e3ab3356d20 100644 --- a/include/linux/page_counter.h +++ b/include/linux/page_counter.h @@ -5,8 +5,15 @@ #include #include #include +#include +#include #include +struct page_counter_stock { + local_trylock_t lock; + unsigned long nr_pages; +}; + struct page_counter { /* * Make sure 'usage' does not share cacheline with any other field in @@ -41,6 +48,8 @@ struct page_counter { unsigned long high; unsigned long max; struct page_counter *parent; + struct page_counter_stock __percpu *stock; + unsigned int batch; } ____cacheline_internodealigned_in_smp; #if BITS_PER_LONG == 32 @@ -99,6 +108,10 @@ static inline void page_counter_reset_watermark(struct page_counter *counter) counter->watermark = usage; } +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); + #if IS_ENABLED(CONFIG_MEMCG) || IS_ENABLED(CONFIG_CGROUP_DMEM) void page_counter_calculate_protection(struct page_counter *root, struct page_counter *counter, diff --git a/mm/page_counter.c b/mm/page_counter.c index 661e0f2a5127a..965021993e161 100644 --- a/mm/page_counter.c +++ b/mm/page_counter.c @@ -8,6 +8,7 @@ #include #include #include +#include #include #include #include @@ -289,6 +290,65 @@ int page_counter_memparse(const char *buf, const char *max, return 0; } +int page_counter_enable_stock(struct page_counter *counter, unsigned int batch) +{ + struct page_counter_stock __percpu *stock; + int cpu; + + stock = alloc_percpu(struct page_counter_stock); + if (!stock) + return -ENOMEM; + + for_each_possible_cpu(cpu) { + struct page_counter_stock *s = per_cpu_ptr(stock, cpu); + + local_trylock_init(&s->lock); + } + counter->stock = stock; + counter->batch = batch; + + return 0; +} + +void page_counter_disable_stock(struct page_counter *counter) +{ + unsigned int stock_to_drain = 0; + int cpu; + + if (!counter->stock) + return; + + for_each_possible_cpu(cpu) { + struct page_counter_stock *stock; + + /* + * No need for local lock; this is called during css_offline, + * after the cgroup has already been removed. + */ + stock = per_cpu_ptr(counter->stock, cpu); + stock_to_drain += stock->nr_pages; + } + + if (stock_to_drain) { + struct page_counter *c; + + for (c = counter; c; c = c->parent) + page_counter_cancel(c, stock_to_drain); + } + + /* This prevents future charges from trying to deposit pages */ + counter->batch = 0; +} + +void page_counter_free_stock(struct page_counter *counter) +{ + if (!counter->stock) + return; + + free_percpu(counter->stock); + counter->stock = NULL; +} + #if IS_ENABLED(CONFIG_MEMCG) || IS_ENABLED(CONFIG_CGROUP_DMEM) /* -- 2.52.0