From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 6FD982C326F for ; Thu, 26 Mar 2026 09:16:50 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1774516610; cv=none; b=ja0nnHsBzkY9bqEq35N0iTjR1t8YuE504s0LXf6RVDyHZSyY5STqsM6OlhxBdqoWm44C6rhXtS6vtv9c96RD07DrITtTmtevV6PXllSl4+iTSHyfLsAJo4ldmb0Br9dnfSgWdrqAOG0sX14oqsAmc9voPLHPZhR/RcF5MRZsJT4= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1774516610; c=relaxed/simple; bh=Ku76ii2QKchQGfJBeF725l3BRUbVjcFPuQ0rkEtPhHo=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=ryO0QMcr7eb36pljfpHNq1PKIwOYBB/mv+c/uVwHKXlXLhb+F5WPtPSo+GtacBR2f7RFT8S1a0xSL6EOHEetY/bZKmqA1cgkwWTanMrpLMGlNKLTiZcKjk1irhfip4Gws+HHg7n4ZzkKyjPGsR4zt/Ku2l01h5ozM52HD6xJXWg= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=iLRmbczE; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="iLRmbczE" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 020AEC116C6; Thu, 26 Mar 2026 09:16:43 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1774516610; bh=Ku76ii2QKchQGfJBeF725l3BRUbVjcFPuQ0rkEtPhHo=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=iLRmbczEYUSNzKMgqjO4/Syj9peJH4QHwMp8UpOz38C2PVCC6XlbD7LdwgrdJu/BP wY54YEyTrEg209MA8DRDOxQnxTj003n6nsEjclfasxeiV46VH8ZC4Br+rRbZnM1ejE 0faqREUGITG80AyRbMq/Jr180uEfRbVeuqSqbyTwFZaXoEEmzgjrgptB1jvwS3HYNG cO8QjL2qc7x2nEKEDY5poS+G0qlpRArr9ELUn9QZ25J2JGU99Dbkawgc/4w9pI2Jpy EosItldIR8MURwxui58vw6hHlsxPKw0OzXsj7EILKKDo+4krLtBodeBaG86X60BTls kQpLPXTSHnZBw== Date: Thu, 26 Mar 2026 09:16:41 +0000 From: "Lorenzo Stoakes (Oracle)" To: Qi Zheng Cc: hannes@cmpxchg.org, hughd@google.com, mhocko@suse.com, roman.gushchin@linux.dev, shakeel.butt@linux.dev, muchun.song@linux.dev, david@kernel.org, ziy@nvidia.com, harry.yoo@oracle.com, yosry.ahmed@linux.dev, imran.f.khan@oracle.com, kamalesh.babulal@oracle.com, axelrasmussen@google.com, yuanchu@google.com, weixugc@google.com, chenridong@huaweicloud.com, mkoutny@suse.com, akpm@linux-foundation.org, hamzamahfooz@linux.microsoft.com, apais@linux.microsoft.com, lance.yang@linux.dev, bhe@redhat.com, usamaarif642@gmail.com, linux-mm@kvack.org, linux-kernel@vger.kernel.org, Qi Zheng , "Harry Yoo (Oracle)" Subject: Re: [PATCH v2 4/4] mm: memcontrol: fix unexpected massive positive number in memcg_state_val_in_pages() Message-ID: <54c2b09c-84f8-4118-96a6-acc13ca2f245@lucifer.local> References: 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: On Wed, Mar 25, 2026 at 10:13:25PM +0800, Qi Zheng wrote: > From: Qi Zheng > > In memcg_state_val_in_pages(), if the passed val is negative, the > expression val * unit / PAGE_SIZE could be implicitly converted to a > massive positive number when compared with 1UL in the max() macro. > This leads to returning an incorrect massive positive value. > > Fix this by using abs(val) to calculate the magnitude first, and then > restoring the sign of the value before returning the result. Additionally, > use mult_frac() to prevent potential overflow during the multiplication of > val and unit. > > Reported-by: Harry Yoo (Oracle) > Signed-off-by: Qi Zheng The logic is correct, but I think this needs rework for better understanding, and obviously this should be squashed into 2/4 as per Andrew. With the below change applied: Reviewed-by: Lorenzo Stoakes (Oracle) > --- > mm/memcontrol.c | 7 +++++-- > 1 file changed, 5 insertions(+), 2 deletions(-) > > diff --git a/mm/memcontrol.c b/mm/memcontrol.c > index 04076a139dbe3..0c249255ebefb 100644 > --- a/mm/memcontrol.c > +++ b/mm/memcontrol.c > @@ -787,11 +787,14 @@ static int memcg_page_state_unit(int item); > static long memcg_state_val_in_pages(int idx, long val) > { > int unit = memcg_page_state_unit(idx); > + long res; > > if (!val || unit == PAGE_SIZE) > return val; > - else > - return max(val * unit / PAGE_SIZE, 1UL); Hm this was already fairly horrid, because we're comparing an unsigned long value of 1 vs. a ULONG_MAX - abs(val) so this was intended to make 0 -> 1UL but not what you'd mathematically think this was which was to make negative values (logically < 1) -> 1. Of course before it was just broken and would promote (val * unit / PAGE_SIZE) to unsigned long first (thus massive number) and return that :) > + > + res = max(mult_frac(abs(val), unit, PAGE_SIZE), 1UL); This is way too compressed into one line and retains the confusing behaviour. Could we split this out and explain what we're doing (sign-extension, integer promotion and all of this stuff is confusing - so let's just accept that and spell it out): /* Get the absolute value of (val * unit / PAGE_SIZE). */ res = mult_frac(abs(val), unit, PAGE_SIZE); /* Round up zero values. */ res = res ?: 1; /* Retain sign. */ return val < 0 ? -res : res; This is functionally identical, but a lot more readable, I think. > + > + return val < 0 ? -res : res; > } > > #ifdef CONFIG_MEMCG_V1 > -- > 2.20.1 > Cheers, Lorenzo