From nobody Sun Apr 5 21:29:49 2026 Received: from mail-oi1-f179.google.com (mail-oi1-f179.google.com [209.85.167.179]) (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 65F7537A49D for ; Mon, 23 Feb 2026 22:38:41 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.167.179 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1771886322; cv=none; b=Xf7tZcJLIsXrVcMeb4fm3CRAm0+t5z4qe4ursG8D2SDLfbIqJ3gCE8YT1EjaQUDerTup6Qy3NZc3AuVy//Kxco/Cm3CAeSVhdXvto2UCAJOdE1u+oWImN4w+FMYXZosnLc0+Ehtjz+yeKEMLPwJCcQSTLLmc0Qfyn1jRGQrnGTw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1771886322; c=relaxed/simple; bh=J+BT9JHVO5MqWgVBXYMCvRaAsNpI/lKiHSmM/7rTRko=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=CuCWFD8b7diHx7ixImfBgN7f03LA1/40IdjPnPik2M7uYch5iCEjsL2CT7Klken6g2SsRldN1hb7na+aMtKio/g9k4DcZJ6GSO0HbwvObqY2dv8ZaXuZcMQKJImFzNFI4uNJvN39NJDljYjj7voVQQP80AndGCC7iy5YGTnc+z0= 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=ZLsqMBTk; arc=none smtp.client-ip=209.85.167.179 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="ZLsqMBTk" Received: by mail-oi1-f179.google.com with SMTP id 5614622812f47-463a94f8475so3076398b6e.0 for ; Mon, 23 Feb 2026 14:38:41 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1771886320; x=1772491120; 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=FgNKK4IJNslBJZZOngZoyEbar9yDv1r70Pc6CR+o2yQ=; b=ZLsqMBTkUrxPmUXniY20pVQ3VuNzlsDGv/KaLU50Bv2rJG8/0Jh/04kbFDxHYegmdR 4u1hwYOP5Oc3Rvw1j2GjMGquFZP7BA3MPlaViY99SkbC7IYYPjus46+pzr3NSDFF3t7f TvFGRz451M5xbBgjviQnb+/5TYarK/4dP6qqEGPf/b9uTrye8+FPbkxf33U1JK+thch9 F0yMafUZcYBA3uHoMfwzbEBqDmCMd0hLbbhkqp4VuU20/xtZGyeALnsXDOyxBz6BIbWY r1RvXO/WkWbnwcEDrfHWOOsvDwg/UDCz2lFijwafkK0xsr65RMIQh71cLi58yR45y3UL 0hIA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1771886320; x=1772491120; 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=FgNKK4IJNslBJZZOngZoyEbar9yDv1r70Pc6CR+o2yQ=; b=QBZzvK2XthOpGjyw2E2pAzmOH4buH8zx4KB3ociShcHqzMvIQwhRpc0JSsYJ73lGoz d11SUyK0IX1XlW7twsu1d6YRJ7P7RIl4LM6RZI1PHqQlwbrUQH4q56VUw0Ytwu5Oc8K4 J5kVvP/EJp7MjrOkdWHv67GeV0IZTyCrsn7EIr/uSw0qHavfQxzsIUUz9wRG13Zfg2W9 bV/6ziKBQIDdZlv/6VQmp0sDEk8mB4cdqVuXCxxgw333w/wN8Z7xFxr6LmGBhLMrWJqP XHXJmn2q3Y95sKJhRCUAebzzsZW4fW74mKHz3s2hrytSq+o76uDRaORO4k4TOtK/Oejx nLYQ== X-Forwarded-Encrypted: i=1; AJvYcCVZqOJKa3GkClOECjqijR3cIIDBqFrr3r1Mh67FaAo165mdSLIISj3apinBZH9EiYDWVt6f0AveqTL0Qww=@vger.kernel.org X-Gm-Message-State: AOJu0YxS96blVmn30U3QImHHJMEtVcMBsLmdtJHeptIelOp8egKPeywv THz60htsU0/rbDFhTtpvZFyvglGPMB3tDSdLZSs08pT38/DRsG5eqrD871uM5w== X-Gm-Gg: AZuq6aJkU9CrhHViM33O24Wa2yMREB0aSPDgqRWiIuI5QERwcnBEoLfy3oGGGmfyR+W V43/no/xU+ntDWHYa7xEwNBxoVvTjLY7J/0OLicvJW2qErL9Pc8pXAfVMzq8h52CFFlvNfxuAVD cFwX40leHYCx2AgmVWAXx0bu6RyAR5ZxPngW4T2h8Qo61Aqa0zdSMYiFKf/4dw1/xVJPWeqsToo igxJFqQxCBC/z6dQ/mKAtt/VrlvFo4EPIRq8u4BDVZKQ53NR+MdMZQQCPyDesZoxJaXz8UNJZFa HDSpcwIdcJVf8IzGB6xTuhBbAM9EAf5JNELguKj8UfPAOlzLWErBGT+9OHt5+st5APJkRSmeKW2 9V8Fm5x+B/esmsirqv74KJuRW7myYqNt7AEkQ+xnheqcsVvrDugUtn7r0Mddax9aIeVHVBS7hz0 LKLBxnVS6z62dHy7ZdPMN+NNR3nDj+n9c= X-Received: by 2002:a05:6808:3442:b0:453:7cad:63be with SMTP id 5614622812f47-464276813fbmr8219044b6e.31.1771886320460; Mon, 23 Feb 2026 14:38:40 -0800 (PST) Received: from localhost ([2a03:2880:10ff:2::]) by smtp.gmail.com with ESMTPSA id 5614622812f47-4644a1afa44sm5819928b6e.16.2026.02.23.14.38.39 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 23 Feb 2026 14:38:39 -0800 (PST) From: Joshua Hahn To: Joshua Hahn Cc: Andrew Morton , Johannes Weiner , Michal Hocko , Roman Gushchin , Shakeel Butt , Muchun Song , linux-mm@kvack.org, cgroups@vger.kernel.org, linux-kernel@vger.kernel.org, kernel-team@meta.com Subject: [RFC PATCH 4/6] mm/memcontrol: Charge and uncharge from toptier Date: Mon, 23 Feb 2026 14:38:27 -0800 Message-ID: <20260223223830.586018-5-joshua.hahnjy@gmail.com> X-Mailer: git-send-email 2.47.3 In-Reply-To: <20260223223830.586018-1-joshua.hahnjy@gmail.com> References: <20260223223830.586018-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: quoted-printable Content-Type: text/plain; charset="utf-8" Modify memcg charging and uncharging sites to also update toptier statistics. Unfortunately, try_charge_memcg is unaware of the physical folio being charged; it only deals with nr_pages. Instead of modifying try_charge_memcg, instead adjust the toptier fields once try_charge_memcg succeeds, inside charge_memcg. Signed-off-by: Joshua Hahn --- mm/memcontrol.c | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/mm/memcontrol.c b/mm/memcontrol.c index f3e4a6ce7181..07464f02c529 100644 --- a/mm/memcontrol.c +++ b/mm/memcontrol.c @@ -1948,6 +1948,24 @@ static void memcg_uncharge(struct mem_cgroup *memcg,= unsigned int nr_pages) page_counter_uncharge(&memcg->memsw, nr_pages); } =20 +static void memcg_charge_toptier(struct mem_cgroup *memcg, + unsigned long nr_pages) +{ + struct page_counter *c; + + for (c =3D &memcg->memory; c; c =3D c->parent) + atomic_long_add(nr_pages, &c->toptier_usage); +} + +static void memcg_uncharge_toptier(struct mem_cgroup *memcg, + unsigned long nr_pages) +{ + struct page_counter *c; + + for (c =3D &memcg->memory; c; c =3D c->parent) + atomic_long_sub(nr_pages, &c->toptier_usage); +} + /* * Returns stocks cached in percpu and reset cached information. */ @@ -4830,6 +4848,9 @@ static int charge_memcg(struct folio *folio, struct m= em_cgroup *memcg, if (ret) goto out; =20 + if (node_is_toptier(folio_nid(folio))) + memcg_charge_toptier(memcg, folio_nr_pages(folio)); + css_get(&memcg->css); commit_charge(folio, memcg); memcg1_commit_charge(folio, memcg); @@ -4921,6 +4942,7 @@ int mem_cgroup_swapin_charge_folio(struct folio *foli= o, struct mm_struct *mm, struct uncharge_gather { struct mem_cgroup *memcg; unsigned long nr_memory; + unsigned long nr_toptier; unsigned long pgpgout; unsigned long nr_kmem; int nid; @@ -4941,6 +4963,8 @@ static void uncharge_batch(const struct uncharge_gath= er *ug) } memcg1_oom_recover(ug->memcg); } + if (ug->nr_toptier) + memcg_uncharge_toptier(ug->memcg, ug->nr_toptier); =20 memcg1_uncharge_batch(ug->memcg, ug->pgpgout, ug->nr_memory, ug->nid); =20 @@ -4989,6 +5013,9 @@ static void uncharge_folio(struct folio *folio, struc= t uncharge_gather *ug) =20 nr_pages =3D folio_nr_pages(folio); =20 + if (node_is_toptier(folio_nid(folio))) + ug->nr_toptier +=3D nr_pages; + if (folio_memcg_kmem(folio)) { ug->nr_memory +=3D nr_pages; ug->nr_kmem +=3D nr_pages; @@ -5072,6 +5099,10 @@ void mem_cgroup_replace_folio(struct folio *old, str= uct folio *new) page_counter_charge(&memcg->memsw, nr_pages); } =20 + /* The old folio's toptier_usage will be decremented when it is freed */ + if (node_is_toptier(folio_nid(new))) + memcg_charge_toptier(memcg, nr_pages); + css_get(&memcg->css); commit_charge(new, memcg); memcg1_commit_charge(new, memcg); @@ -5091,6 +5122,7 @@ void mem_cgroup_replace_folio(struct folio *old, stru= ct folio *new) void mem_cgroup_migrate(struct folio *old, struct folio *new) { struct mem_cgroup *memcg; + int old_toptier, new_toptier; =20 VM_BUG_ON_FOLIO(!folio_test_locked(old), old); VM_BUG_ON_FOLIO(!folio_test_locked(new), new); @@ -5111,6 +5143,13 @@ void mem_cgroup_migrate(struct folio *old, struct fo= lio *new) if (!memcg) return; =20 + old_toptier =3D node_is_toptier(folio_nid(old)); + new_toptier =3D node_is_toptier(folio_nid(new)); + if (old_toptier && !new_toptier) + memcg_uncharge_toptier(memcg, folio_nr_pages(old)); + else if (!old_toptier && new_toptier) + memcg_charge_toptier(memcg, folio_nr_pages(old)); + /* Transfer the charge and the css ref */ commit_charge(new, memcg); =20 --=20 2.47.3