From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from out-180.mta1.migadu.com (out-180.mta1.migadu.com [95.215.58.180]) (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 76A49EAC7 for ; Mon, 30 Mar 2026 02:22:46 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=95.215.58.180 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1774837368; cv=none; b=Lh8597sKqDkkBA1tesTYEgARXCiXNBk6HoGSSGBMNmiYDduwf8HiT5Pi/TlwobHGcH5Tc+AwhsYsbrmd8h7su/l8ajICpTz3k28E5yYvvo7O4OM+MI4CdAqFkSc2JVkQF4Q+BfxGz3OF2jTAxSkGYxoOQvjLgBocN7fl2EjjCd0= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1774837368; c=relaxed/simple; bh=UkNpb3/0nTD02sL1f3CLoI/+JYf1qpw/ZKZOgmgnKZQ=; h=Message-ID:Date:MIME-Version:Subject:To:Cc:References:From: In-Reply-To:Content-Type; b=b205jYvSMuF2A/cAWhv78PyVwwGbLnjVe6l9HuoBdzUOCaN/tjgNtVK137dIYCOqceebPdEt59Y9Jjtrw8yhSk3vMh9fSXECaFqckfm5oDkr2RB/bNF8vlEvky6hjzD4Lqb4WAxAWyvCFBqom+NU+V1Q6W4rbjH7K6POiZ/b6+4= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev; spf=pass smtp.mailfrom=linux.dev; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b=jTeCSJl5; arc=none smtp.client-ip=95.215.58.180 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.dev Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b="jTeCSJl5" Message-ID: DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1774837363; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=wwwdgUXzTzQtwufI45UcdtS6s0KoKMxy/JsyfHLNcVQ=; b=jTeCSJl51LLSZGNHjEQ+/4SvIRe6LhNVL8AggyORyEWLvqoCRqa+x4mi8qimwbsqauoEcj OxVXD4a9gsqkNDTvBulgVIuJoBL2TpQ+iSlW5XWepAsLjKit+NBRr23tArgXTywzyEV/DR RUv4p08/hwRMvV3UZ+9ShCWktxLDwys= Date: Mon, 30 Mar 2026 10:22:13 +0800 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Subject: Re: [PATCH v3 2/3] mm: memcontrol: change val type to long in __mod_memcg_{lruvec_}state() To: "Harry Yoo (Oracle)" 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, ljs@kernel.org, ziy@nvidia.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 References: <70a9440e49c464b4dca88bcabc6b491bd335c9f0.1774604356.git.zhengqi.arch@bytedance.com> X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. From: Qi Zheng In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Migadu-Flow: FLOW_OUT On 3/30/26 9:25 AM, Harry Yoo (Oracle) wrote: > On Fri, Mar 27, 2026 at 06:16:29PM +0800, Qi Zheng wrote: >> From: Qi Zheng >> >> The __mod_memcg_state() and __mod_memcg_lruvec_state() functions are also >> used to reparent non-hierarchical stats. In this scenario, the values >> passed to them are accumulated statistics that might be extremely large >> and exceed the upper limit of a 32-bit integer. >> >> Change the val parameter type from int to long in these functions and >> their corresponding tracepoints (memcg_rstat_stats) to prevent potential >> overflow issues. >> >> After that, 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 >> Reviewed-by: Lorenzo Stoakes (Oracle) >> --- > > Looks good to me, > Reviewed-by: Harry Yoo (Oracle) Thanks! > >> @@ -831,7 +837,7 @@ static inline void get_non_dying_memcg_end(void) >> #endif >> >> static void __mod_memcg_state(struct mem_cgroup *memcg, >> - enum memcg_stat_item idx, int val) >> + enum memcg_stat_item idx, long val) >> { >> int i = memcg_stats_index(idx); >> int cpu; >> @@ -896,7 +902,7 @@ void reparent_memcg_state_local(struct mem_cgroup *memcg, >> #endif >> >> static void __mod_memcg_lruvec_state(struct mem_cgroup_per_node *pn, >> - enum node_stat_item idx, int val) >> + enum node_stat_item idx, long val) >> { >> struct mem_cgroup *memcg = pn->memcg; >> int i = memcg_stats_index(idx); > > Some of code paths that calls mod_memcg{,_lruvec}_state still passes > int values (which is quite subtle to notice), but it should be fine Right, it happens in too many places, for example, the callers of mod_lruvec_state(). > as reparenting is not involved in the path and could be cleaned up later. Agree. >