From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-ot1-f48.google.com (mail-ot1-f48.google.com [209.85.210.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 93BB9298CA3 for ; Wed, 24 Jun 2026 18:24:05 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.48 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1782325446; cv=none; b=JeezhwWSagIak4bC8XaKqr3u0xoLpFRz/wuihtybLXeEqqdaMowKsV/sCk16LXPiibuRI69lMuf8Sp8IVh92F1oXvc7Se98257SItZzPYfpDXb86lZi2anQBy9XJhsGPfYKpUSjoNY/2yXlQz5mpO3rGx+uorucFSIOishdkgTw= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1782325446; c=relaxed/simple; bh=uHdiXSn8cjy3mkD+G0U0UxMKN2AyKB6KfcMTyvdtR6M=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=BocrrhWwcMtEZdtqe3XZ46g7QgAfD4M2i2XTR6UArcIiHOHW75kVg+zT06TwkuUreNfus59o4kTz9PMdWuGAMhC1pumDjrdSxFuvtNHTkhBKZjeDTJNLM2H8Cq5sqa7zNBGR4GO55rjYkKqnGAUa5k5zclWnwVkjfZkqbOjRhR0= 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=bjERdRTm; arc=none smtp.client-ip=209.85.210.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="bjERdRTm" Received: by mail-ot1-f48.google.com with SMTP id 46e09a7af769-7e6d991991dso1109352a34.3 for ; Wed, 24 Jun 2026 11:24:05 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1782325444; x=1782930244; 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=FP6al2QbtqiBNwNbl9L6yaRaxgcZf//eTD2veoNh+pw=; b=bjERdRTmOJi4yO6bEGmWDIFZMz1tj2BgJ57bMuE1XH6QmQVV+Ow11QB+O3BsnmyeBk UWlUhQVNMgNVl6UxS+ihWvAD69R0bDkBaU734InRfXxKi9jzLnxMIYdwgbH6NXS4Cp08 9s3tD2tv+G0twnclp5rWGab/idiMCK+7m689IE2vFZ/WnCaFhrOUWlf3GPquZBlKedsx 8INdWs/hao2fqGU+RLNwYlsgAiZolzqRCIbt6UHr4YRQN/ZonB20gihZ4dUbnIKtdb4Q HQ6/1DgmrhbT+0wT2/mmocyudjNbD2SH3juguOpDCHmPlViLjExx77t7fFp+U90eyw8P DA+Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1782325444; x=1782930244; 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=FP6al2QbtqiBNwNbl9L6yaRaxgcZf//eTD2veoNh+pw=; b=hB7TiCn59Kx3HggifQEhwL7jjBEK/vU4bENIe4HjidZ6EujyBbIk4ej4hsOvqcOY9j xvDfjOvSae9SwwKM+B6pC83yCNliYwmA04XJDj7cguLcTLj9Zsh/mqBiA6oYTga8Uimk l/C4YmTKD//QYLxPgbMh+0+eRaxczDdh8jf6Aag36k5oKJoSACtE9J7QrLz6KYJQZXZl xZkllqde4wfe3VWo0ztGNqyg3Wa6qMocc3KfJgOAfjSk3QiVYXVdzD4QQq4YXc5v4P5O Mhxb/1T7JiCiYeZ0BQYdAILUDkHsRkq69sMjH26mPEe1Hm/fB7vj3cDS2iDXwuhdvRd7 +vAw== X-Forwarded-Encrypted: i=1; AFNElJ8Uku325vNfBO7DXfuXKcoVdcwU8GZrRxpdX8OSNY/PfG9kIL4nd2nxFzA1CW9I8Af769fBRZBa2fhaKac=@vger.kernel.org X-Gm-Message-State: AOJu0Yx1KF5I4uZvdKkRNkr/7foTzb6jYGl89FRbtv032KnB+q5c8M45 SuUvHzwY4Ww99liLkmmLpUhtjEIcDLzTCjRi+HQ0juPVV+BA4Xce2O/Y X-Gm-Gg: AfdE7ckeiUZnri+dkqG8Z0zUxZe6arH9V5JeiJmA44EnliDfLIiEQNCHR4InPsxrqUm fxEuVZz2PKudgd7pK4bld20nB9RAWH4VR1gG+UdHMMoG//3Jr/2WT52eDoVoeDgRQLM8niZ6vSz 4xlFxB1FqWRnCporjXWJ0lGSu+Zd12F80hEm6QQdr4IQeqgn67++ePFnCCMSyHdPlbT1tAcVo2v bP9qsvXDakyqV1J7QCLmlx0JKZPbTDm7yWOPXjOOeKudm4huwmQNUaYE/YgYCdzL4se0yKSnym8 UijzsymPZMTVgxeqX5qOZX4HU401Yf/va77uUaqLpOASTfoLi70zOIqJOFbtoyR3Ny4FZcQTfXb RZvRW5gKOdYPqQeX4i7qlCcpF8U34Y0cHFJ9bsHChGq2Zd+B8piThIwfdtiOg+DVbVMeBDvl5fm EDqg25sEH6G9NHBWg0KsgWGm8/CRSgU4RYMRssyavPDbE= X-Received: by 2002:a05:6830:4409:b0:7de:5612:d420 with SMTP id 46e09a7af769-7e986c7513bmr4005787a34.18.1782325444557; Wed, 24 Jun 2026 11:24:04 -0700 (PDT) Received: from localhost ([2a03:2880:10ff:4e::]) by smtp.gmail.com with ESMTPSA id 46e09a7af769-7e9442e98e9sm11837257a34.25.2026.06.24.11.24.04 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 24 Jun 2026 11:24:04 -0700 (PDT) From: Joshua Hahn To: Usama Arif Cc: Johannes Weiner , 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: Re: [PATCH v4 4/5] mm/memcontrol: convert memcg to use page_counter_stock Date: Wed, 24 Jun 2026 11:24:01 -0700 Message-ID: <20260624182402.962094-1-joshua.hahnjy@gmail.com> X-Mailer: git-send-email 2.53.0 In-Reply-To: <120367a5-0a3c-40ba-a821-f46f8494ef85@linux.dev> References: Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit On Wed, 24 Jun 2026 17:43:56 +0100 Usama Arif wrote: > > > On 24/06/2026 16:23, Joshua Hahn wrote: > > On Wed, 24 Jun 2026 07:43:47 -0700 Usama Arif wrote: > > > >> On Tue, 23 Jun 2026 11:01:22 -0700 Joshua Hahn wrote: > > > > Hello Usama!! > > > > Thank you for reviewing the patch : -) > > > > [...snip...] > > > >>> @@ -2595,7 +2596,6 @@ void __mem_cgroup_handle_over_high(gfp_t gfp_mask) > >>> static int try_charge_memcg(struct mem_cgroup *memcg, gfp_t gfp_mask, > >>> unsigned int nr_pages) > >>> { > >>> - unsigned int batch = max(MEMCG_CHARGE_BATCH, nr_pages); > >>> int nr_retries = MAX_RECLAIM_RETRIES; > >>> struct mem_cgroup *mem_over_limit; > >>> struct page_counter *counter; > >>> @@ -2606,36 +2606,30 @@ static int try_charge_memcg(struct mem_cgroup *memcg, gfp_t gfp_mask, > >>> bool raised_max_event = false; > >>> unsigned long pflags; > >>> bool allow_spinning = gfpflags_allow_spinning(gfp_mask); > >>> + unsigned long nr_charged = 0; > >>> > >>> retry: > >>> - if (consume_stock(memcg, nr_pages)) > >>> - return 0; > >>> - > >>> - if (!allow_spinning) > >>> - /* Avoid the refill and flush of the older stock */ > >>> - batch = nr_pages; > >>> - > >>> reclaim_options = MEMCG_RECLAIM_MAY_SWAP; > >>> if (do_memsw_account() && > >>> - !page_counter_try_charge(&memcg->memsw, batch, &counter)) { > >>> + !page_counter_try_charge_stock(&memcg->memsw, nr_pages, > >>> + &counter, NULL)) { > >>> mem_over_limit = mem_cgroup_from_counter(counter, memsw); > >>> reclaim_options &= ~MEMCG_RECLAIM_MAY_SWAP; > >>> goto reclaim; > >>> } > >>> > >>> - if (page_counter_try_charge(&memcg->memory, batch, &counter)) > >>> - goto done_restock; > >>> + if (page_counter_try_charge_stock(&memcg->memory, nr_pages, > >>> + &counter, &nr_charged)) { > >>> + if (!nr_charged) > >>> + return 0; > >>> + goto handle_high; > >>> + } > >>> > >>> if (do_memsw_account()) > >>> - page_counter_uncharge(&memcg->memsw, batch); > >>> + page_counter_uncharge(&memcg->memsw, nr_pages); > >> > >> This needs a transactional rollback. page_counter_try_charge_stock() can > >> succeed by consuming memsw stock and charging 0 new pages, but the > >> memory-failure path unconditionally uncharges nr_pages from memsw. > >> That turns a failed allocation into a real memsw usage decrement. > > > > Hmmmmmmmmmm....... I'm not sure. > > > > At this point in the code, we are either (1) using cgroup v1 with memsw > > and charged successfully, or (2) not using cgroup v1 with memsw. So I'm > > not sure if this really is unconditional, we're just distinguishing > > between cases (1) and (2) by checking if we're using cgroupv1. > > > > Or is your concern with taking a charge via stock, but uncharging with > > a hierarchical page_counter walk? > > This was my concern. But I re-read the page_counter stock invariant, > and the stock-hit case is not an undercount? Consuming stock transfers > already-charged credit to the pending allocation; if the later memory charge > fails, page_counter_uncharge() discards that consumed credit from the > hierarchy. That should keeps usage equal to real charges plus remaining stock? Yes, stock-hit case just does some math without doing any actual charging. It's stuff that was pre-charged before, so we're not doing any undercounting or leaking any charges. What do you mean by "consumed credit"? From what I can see page_counter_uncharge --> page_counter_cancel subtracts from counter->usage, which should be the real charge + hierarchy walk. Am I missing something :p please feel free to let me know! Joshua