From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-qv1-f45.google.com (mail-qv1-f45.google.com [209.85.219.45]) (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 98DBC28507B for ; Wed, 10 Dec 2025 16:36:37 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.219.45 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1765384600; cv=none; b=C5NmTYsknPntPYQ6lPE4Fwsg6ZGW3HvTzBKEZWPXXEtC2dx8o5TO6xeIEQtBZGv893RKwOximGj0x3PY59DxFKFwaW++2MfcWB6uV/eGwklrkXmOPTAGL8WKQJuUz27uYEzpIRBgyjLJFSQ7Ts9Aywyt8tfwF52OssDBxU0FGws= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1765384600; c=relaxed/simple; bh=jJ1w8aiXZ2sC5XEOtzK057fJgKCo9eY/bTmp3pryYuU=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=WqcNeRubc4MVMzg69qmUU9ke+8xkm4aGoDGrKUfoAd9V58x2KxYj3AmacnzcFZnm5vbCYBsz91VavN9ddlTXzlKyytnEzMcfW/QgiWIhbkH6ZnxaZSUjSH0hzxkKxF+SodSeDFOuwAOcbF2BV5BzsI04CiQN7dj41LbGHvN0rec= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=cmpxchg.org; spf=pass smtp.mailfrom=cmpxchg.org; dkim=pass (2048-bit key) header.d=cmpxchg.org header.i=@cmpxchg.org header.b=lMNhi+7X; arc=none smtp.client-ip=209.85.219.45 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=cmpxchg.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=cmpxchg.org Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=cmpxchg.org header.i=@cmpxchg.org header.b="lMNhi+7X" Received: by mail-qv1-f45.google.com with SMTP id 6a1803df08f44-88056cab4eeso147156d6.2 for ; Wed, 10 Dec 2025 08:36:37 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cmpxchg.org; s=google; t=1765384596; x=1765989396; darn=vger.kernel.org; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:from:to:cc:subject:date:message-id:reply-to; bh=p8agRBKmWPH4WtPmE+5IIMo3OzvlggOYwQwERk0higo=; b=lMNhi+7XJo2GSnCnIBqCmOa1lgK7cGsw8rxZ/X23oJYYm1ufhHAJPjxkC97RLefAcz JUI4htTEedroUXgw0n7oEDRnx+b7NChGEYpb5fnk+cRqpUtA6rkX63N0GJoWsKuQpFWR yQ+CL54I0ctb/259k8iqiZNmhcvSUNp2Z7IFTp9P4Bbm9YvR/A7OadmNSgO5ov5zEX7X 385cGGlltlBYQzDkRm3yyzjCzeVcC6p1s2QtwJ2S0vubEZGcVeAJLvAl/RZHAkviJQ8I ujO/vyCsYKNNmylyKORe0Y7nH+eQjAe0+SpA8uVv/iTRMDKHlJiGWEni4MfQU6U6t5ns 25UQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1765384596; x=1765989396; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:x-gm-gg:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=p8agRBKmWPH4WtPmE+5IIMo3OzvlggOYwQwERk0higo=; b=RpoTtFaFK+neuj4CdrdP5ghobeZPyjk8gEdlvOIopzFqROVYF/arU3Lu3mqWaiFPx1 ZocHtF1a3Vl78wPV4EvO+Wsowqq3Lfk3x9vWJcUVrqY9nNzz+M6CsMSOWUZXjRyq5527 x96h1aID0xTQZlbG7UjBqiV75hurVpUB5nwVMljZfpqle20G/3woLkH78OD/Z2OXDwtM jHo7aJrHz1GrqKAMBMMFcnS84mAVysIEIlgrEmb3XCFi7V8t7XtaN44eBaCcrmekB2tb fyPtj7c4QLU2jJfWxJ1gUkRozo7m+hTEheA0sxud2DNfcNc1TQ0bttAeVRVXuPzLgoQE Dvxg== X-Forwarded-Encrypted: i=1; AJvYcCWC07anSPIpcr+53++acm4NUMTaCkGR3yOqY5Xb0aCDg5FujkKGJpwgtsgXXn8K74keNsTDJrNxHkmiYGA=@vger.kernel.org X-Gm-Message-State: AOJu0YzxkRPGEFeU0YizjWjqU5/IzV1Ka1SPO/iZnXm9VktunLg4Rvy7 NBq7feRnxBd3Eo7P4DBoRr0a/16IKn1deiI+5uxXBcD7MubOsw+LpJRPmQYkYnfQinA= X-Gm-Gg: AY/fxX73XZNmuooONKbXJVW+aiJXuniK5oaqvNFEf3HLcKO8KQT5r+PLyWa2PbBDKLp Y+KqQ1tRqAyxJfAI+h7A4FOePLP7/tlYw3quZRnFXjendvUvXoD4bmoGVUwW6q6GmPWvGNGlpMC 6y5wpV5Vxco1QtMMil5a7WQCbGt0/yIbwu4Lyn2gcRQ+4H00XGSCkHFfOr0bN9qwsBdA+zwX1+I e1Xg9L09/dORGwiZWaZ73UHseScEt7G2LBIU7aXwo2dkMc2lX0T4YDwxg97217HyPmCObImSuVJ /jjqzWDfn5Ccyoqfz8hnNc+eCHQgY4lkvHCHAvizNb8sMqY2Rd3u5hUW0ruxFqFUws7/DtXLo6i Jom9jo/2D/d7HC+9I7M3/f0mWUczhX+DMShONxedhI8nSm2GLlqMTCYJHIrsCEhv3+kA7k9benu YyIlfVxwj2tw== X-Google-Smtp-Source: AGHT+IFVI/xOKQYJhESjIuCfEpKQpuoNTM6MQ4mD11zbQuJPwB9HOkC9UT6xUK7xl7kYv/i4CQfBUA== X-Received: by 2002:a05:6214:4385:b0:880:88fa:d742 with SMTP id 6a1803df08f44-88863ad4d22mr43775186d6.65.1765384596326; Wed, 10 Dec 2025 08:36:36 -0800 (PST) Received: from localhost ([2603:7000:c01:2716:929a:4aff:fe16:c778]) by smtp.gmail.com with ESMTPSA id 6a1803df08f44-8886ec54e4fsm993946d6.19.2025.12.10.08.36.35 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 10 Dec 2025 08:36:35 -0800 (PST) Date: Wed, 10 Dec 2025 11:36:34 -0500 From: Johannes Weiner To: Chen Ridong Cc: mhocko@kernel.org, roman.gushchin@linux.dev, shakeel.butt@linux.dev, muchun.song@linux.dev, akpm@linux-foundation.org, axelrasmussen@google.com, yuanchu@google.com, weixugc@google.com, david@kernel.org, zhengqi.arch@bytedance.com, lorenzo.stoakes@oracle.com, cgroups@vger.kernel.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org, lujialin4@huawei.com Subject: Re: [PATCH -next v2 2/2] memcg: remove mem_cgroup_size() Message-ID: <20251210163634.GB643576@cmpxchg.org> References: <20251210071142.2043478-1-chenridong@huaweicloud.com> <20251210071142.2043478-3-chenridong@huaweicloud.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20251210071142.2043478-3-chenridong@huaweicloud.com> On Wed, Dec 10, 2025 at 07:11:42AM +0000, Chen Ridong wrote: > From: Chen Ridong > > The mem_cgroup_size helper is used only in apply_proportional_protection > to read the current memory usage. Its semantics are unclear and > inconsistent with other sites, which directly call page_counter_read for > the same purpose. > > Remove this helper and replace its usage with page_counter_read for > clarity. Additionally, rename the local variable 'cgroup_size' to 'usage' > to better reflect its meaning. +1 I don't think the helper adds much. > --- a/mm/vmscan.c > +++ b/mm/vmscan.c > @@ -2451,6 +2451,7 @@ static inline void calculate_pressure_balance(struct scan_control *sc, > static unsigned long apply_proportional_protection(struct mem_cgroup *memcg, > struct scan_control *sc, unsigned long scan) > { > +#ifdef CONFIG_MEMCG > unsigned long min, low; > > mem_cgroup_protection(sc->target_mem_cgroup, memcg, &min, &low); > @@ -2485,7 +2486,7 @@ static unsigned long apply_proportional_protection(struct mem_cgroup *memcg, > * again by how much of the total memory used is under > * hard protection. > */ > - unsigned long cgroup_size = mem_cgroup_size(memcg); > + unsigned long usage = page_counter_read(&memcg->memory); > unsigned long protection; > > /* memory.low scaling, make sure we retry before OOM */ > @@ -2497,9 +2498,9 @@ static unsigned long apply_proportional_protection(struct mem_cgroup *memcg, > } > > /* Avoid TOCTOU with earlier protection check */ > - cgroup_size = max(cgroup_size, protection); > + usage = max(usage, protection); > > - scan -= scan * protection / (cgroup_size + 1); > + scan -= scan * protection / (usage + 1); > > /* > * Minimally target SWAP_CLUSTER_MAX pages to keep > @@ -2508,6 +2509,7 @@ static unsigned long apply_proportional_protection(struct mem_cgroup *memcg, > */ > scan = max(scan, SWAP_CLUSTER_MAX); > } > +#endif To avoid the ifdef, how about making it bool mem_cgroup_protection(root, memcg, &min, &low, &usage) and branch the scaling on that return value. The compiler should be able to eliminate the entire branch in the !CONFIG_MEMCG case. And it keeps a cleaner split between memcg logic and reclaim logic.