From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-oo1-f48.google.com (mail-oo1-f48.google.com [209.85.161.48]) (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 630CD346773 for ; Thu, 23 Apr 2026 20:34:52 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.161.48 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1776976495; cv=none; b=NMZvEZ6J4PNJEUvxZYB3f3uRonykeDkEteYy4D1MaI+7oiQlDpyNyRtMqZ3DkhrsgjE5cdtum3gEbE49J3YVkKvB1SEg4ZV1O7OK0dOgWQaTzA2V6W1DByw9e2Bl0lVg8inPkUbycUXmcv9dKSOAQnreiQ67N8YtQtZ6y5p1ZC4= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1776976495; c=relaxed/simple; bh=m2egjhEQC0t2Vv15sybW+Jh3yKaxRsDhuxEWKn//AVM=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=VSdKLtPmnQ93QbgiIEowFY0IZdFslh7OnE3NBj/po77rwcNG0AkGWGCfFnQAIPMAyOoUtkm1QTzmGlSRC1I4BMDFdqultv4Fh9dC0lwqWzRXX7dRkEHSkOTt5fhwtg4DD6UCkuMsiDcxVwvhKzznZEz/1wttWhoGcSQhvw901qA= 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=LqdIkg/p; arc=none smtp.client-ip=209.85.161.48 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="LqdIkg/p" Received: by mail-oo1-f48.google.com with SMTP id 006d021491bc7-673ee2a98b1so3977945eaf.0 for ; Thu, 23 Apr 2026 13:34:52 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1776976491; x=1777581291; 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=FE7KATctX9/flJ2U64Pj9KvPGOu/JEwhawIz5SLby7w=; b=LqdIkg/pl+32/D5WXRVGPF4Cb/fLswyGBJoR1Tkus3GaUzUlDmuS6XrMS0CSTL3cgm GNFizkLsQYdiPPGAphat09/+09J5umE339yZ1q9zR/ATemWDAcmK55cmbJK+kGgIMOvP an36297sY+UTdRk6lDfzoZ/qEek6hVmhvVLnz1z7ORSsjW1JD44auqq1QJ/sZrdv0tpS BIzg79gBVqrHqGu/TEMdP9m0PtqjGPtLwZMPjJcYl7DhXBGnym309smge1ITNSaX3OcM fW/bftW3XnWiEP+hx92cntPuy43khHtAmfi/hfTwJn2IItIxoW8ieDd6aBCR7PV89aNX QUww== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1776976491; x=1777581291; 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=FE7KATctX9/flJ2U64Pj9KvPGOu/JEwhawIz5SLby7w=; b=f5xtevMqIud8eN1bZP/xKyHJbbVWVDYsq7D5NJkAUNB69pGX9hJakSN4nM/o9c3WO3 cLlGrCAQ+FAFTvfHj+dJ/Kwax31PPvs93N4TR0e6LEbcT405xoDWtZXKEL+cVY67m7Np VlLsYdpsE/uVuLYc4IAYThgS3g5oS4stZ+eDLONGmhVToVYXor4JrOHGnf+V7kOLTa7h AeFlDRJKFgyU/6anq4/aT9gkx+xoa4mUU2XZ7m55Snc9g7wkf1gM2dwIyIv6pVCIjB7x eEsAYQQzk5TX8BJPY4kM4cloqw8c4+CGnLvnuTNYISuktEqS7Dh6N5CHIBIhYGVnwmXI tjDQ== X-Forwarded-Encrypted: i=1; AFNElJ+XEOb1T6GXoVm5es1Nh67oZtsHF1icKs4UaEpyCeeU9DxLTciQI4V6x+p8Vfvxpyl0BHMr82KUmg+17ys=@vger.kernel.org X-Gm-Message-State: AOJu0YzFg8xJxA9mhlN/cusexhJ4CeNhTJhZvePKzq9G1EOt0/yl1JtQ t+I3Ak11MBRLAtKtm8W79xaPSzYcWdCSPprvMTkLoBLM/KlotuIJ3pzJ X-Gm-Gg: AeBDietragfi8Cl5K7j2aa2Lxjig/vSwHYwAbJveftPLQqKP6hx1aCxcxPmUlVMKs61 HGZaJfQQl956+g2VWSoosSfPWsZsy3yYhTMiOrfo8Aa3XqGAqYB2riAo7VT0ggAVg07Orey8I4d 0JhE6t8+eXMrZ7QuxocW0yi/Aezj6xI3pkE0H7dLkjCpBQGMvZGT9nr5vNjebyPTiK9B8scD3Ev Ca66vNG+F+2NE2upvujeGWLOSdKasz3MjmWhWq4HhCkKcHZouwANL0qK88gaSyDcGkuZG/FM6k/ YhwnjYs2t9XgoPc87EMDzDK+/NvGs5KooSUrs0N1i/uWB6WiHd4DcMRBwHGtPtLmQCoykOLGQGg CLcWBtCxhqanFHyTvVAsTABxRl5BVUsnqOMVQTT9HmqXDyHPPhazrjWuhbhZwzldLIicMsB2eVA qAzivTB02elnsRFek+0/EXxq2WIGHWdnth X-Received: by 2002:a05:6820:827:b0:694:97ca:9ec5 with SMTP id 006d021491bc7-69497caa1c1mr8146048eaf.51.1776976491332; Thu, 23 Apr 2026 13:34:51 -0700 (PDT) Received: from localhost ([2a03:2880:10ff:48::]) by smtp.gmail.com with ESMTPSA id 006d021491bc7-69464c77c7fsm13161024eaf.0.2026.04.23.13.34.50 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 23 Apr 2026 13:34:50 -0700 (PDT) From: Joshua Hahn To: linux-mm@kvack.org Cc: Johannes Weiner , Michal Hocko , Roman Gushchin , Shakeel Butt , Andrew Morton , Muchun Song , cgroups@vger.kernel.org, linux-kernel@vger.kernel.org, kernel-team@meta.com Subject: [RFC PATCH 3/9 v2] mm/memcontrol: Refactor page_counter charging in try_charge_memcg Date: Thu, 23 Apr 2026 13:34:37 -0700 Message-ID: <20260423203445.2914963-4-joshua.hahnjy@gmail.com> X-Mailer: git-send-email 2.52.0 In-Reply-To: <20260423203445.2914963-1-joshua.hahnjy@gmail.com> References: <20260423203445.2914963-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 preparation for adding charging and uncharging of a new page_counter toptier to try_charge_memcg, refactor the code so that it is easier to roll back partial charges when any one of the three page_counters fail to charge. No functional changes intended. Signed-off-by: Joshua Hahn --- mm/memcontrol.c | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/mm/memcontrol.c b/mm/memcontrol.c index 7de23ecd7cef6..8f7bedb55dbb1 100644 --- a/mm/memcontrol.c +++ b/mm/memcontrol.c @@ -2385,18 +2385,22 @@ static int try_charge_memcg(struct mem_cgroup *memcg, gfp_t gfp_mask, retry: reclaim_options = MEMCG_RECLAIM_MAY_SWAP; - if (!do_memsw_account() || - page_counter_try_charge(&memcg->memsw, nr_pages, &counter)) { - if (page_counter_try_charge(&memcg->memory, nr_pages, &counter)) - goto done_restock; - if (do_memsw_account()) - page_counter_uncharge(&memcg->memsw, nr_pages); - mem_over_limit = mem_cgroup_from_counter(counter, memory); - } else { + + if (do_memsw_account() && + !page_counter_try_charge(&memcg->memsw, nr_pages, &counter)) { mem_over_limit = mem_cgroup_from_counter(counter, memsw); reclaim_options &= ~MEMCG_RECLAIM_MAY_SWAP; + goto reclaim; } + if (page_counter_try_charge(&memcg->memory, nr_pages, &counter)) + goto done_restock; + + if (do_memsw_account()) + page_counter_uncharge(&memcg->memsw, nr_pages); + mem_over_limit = mem_cgroup_from_counter(counter, memory); + +reclaim: /* * Prevent unbounded recursion when reclaim operations need to * allocate memory. This might exceed the limits temporarily, -- 2.52.0