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 D00D42A1BF for ; Mon, 30 Mar 2026 01:25:36 +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=1774833936; cv=none; b=GB83BOn0oVKpfLedzF34Jn9wBy/Km9B02hl11vw/DJm683yLi683UfE/Jc37jQru4HnwDCMXtA+asflDyMFRvUcT3FQXQcHpjVA/dIb1sSSY7qBl++LIDD+xzOPqPpuwtulJhyU/e3z0Uh6IsBMgRC6YgJOFsgTHrj4LB+DFPKY= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1774833936; c=relaxed/simple; bh=QtbwGcQO/wcZZuRBp5gT6Lg9A1uakp8JwUHtWhFR+vk=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=XcLDfIN4lPaEKCjcFDwo7PJyucHRoqdKsQUUXRM5aiYQSqhRqbkHWVrghlWVzHBEf14855M0JF4bt0NhIx+JyVxHH6IEB7/qlbq2bsrP/LwCwVdFC1v1992bHR224E9kbwWX3k1QOo/SHgRrbZyYIOhkJ7yh0U+zzuO+R0iO854= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=ZH7CN959; 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="ZH7CN959" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 0C1EAC116C6; Mon, 30 Mar 2026 01:25:36 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1774833936; bh=QtbwGcQO/wcZZuRBp5gT6Lg9A1uakp8JwUHtWhFR+vk=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=ZH7CN959O/UCUttT+rAmnTcDabGv8rmF4GAfRkbtFbrvozjuijYT2iEk0/traecfs fcvmoZWTH3i76Zk0BhsMvPhE15pPuoYxYD2c1Dv+KJWovnXaGvtY1S7i1xczSf9Wj4 74OYcx7avu+hxYPudjdl8CpmH87zfbmqvH/yiJPVrYjKr6Gw3D6Nz4VDlIannkMQ6k 47LgN9vgRIFD69KmDo41UbqcLYCX3xObcTTY5s6ZxLiEkRc5qbcE8sSiv9nLpJP9tl ieDepI/b2F/YJFbDHX3d63+yyECxW1w40L/xhfl23CJKjoehhn4NvpUYNCX/om1dNX hI5rzfElOS3pQ== Date: Mon, 30 Mar 2026 10:25:34 +0900 From: "Harry Yoo (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, 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 Subject: Re: [PATCH v3 2/3] mm: memcontrol: change val type to long in __mod_memcg_{lruvec_}state() Message-ID: References: <70a9440e49c464b4dca88bcabc6b491bd335c9f0.1774604356.git.zhengqi.arch@bytedance.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: <70a9440e49c464b4dca88bcabc6b491bd335c9f0.1774604356.git.zhengqi.arch@bytedance.com> 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) > @@ -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 as reparenting is not involved in the path and could be cleaned up later. -- Cheers, Harry / Hyeonggon