Received: by 2002:ab2:710b:0:b0:1ef:a325:1205 with SMTP id z11csp664472lql; Mon, 11 Mar 2024 13:37:13 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCVmZw+rEvJnwARoZtBYmn3/xwinhahYuJrn5L6mgPIqZijUJfWDjCJpo9Eqm6g9Gg1a5GbcqCzfWWZQeYXLSvN1d+ahzVRuu4reZaGtyQ== X-Google-Smtp-Source: AGHT+IED0U0+YjyPPiQ2NsUvILn4AZzsas9EI9xrM3p6HO0+cZwrRigoM0e6yNq9212mbcjslWzy X-Received: by 2002:a2e:a407:0:b0:2d3:ffa4:6287 with SMTP id p7-20020a2ea407000000b002d3ffa46287mr4495086ljn.37.1710189433234; Mon, 11 Mar 2024 13:37:13 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1710189433; cv=pass; d=google.com; s=arc-20160816; b=iUkyV2T3mgkj1vEXK5/ZfqJYfOkDoeaCf148gLTmFx37L+6u/FCZEDsoaI7F4crtMx HR0nLA9kI7N28tL5OwIUJ1JfvRt7zzDa4FATD5bIuenQAL4VWK3eyuMWqrH2peNGUrw9 kRGmrab28oaPp71uQ/8ZlGbzlfyXFqHXxOWajhVMBUuysgBw4nilePU2nzptr4Zp9Am1 o+Qrp9rmGLa7YMOxDefYy8JFOdhYerNpWugNwl3C2EkaJFzBAbkuM3002nv8ZpiFJJQO cBKh6j24t5gbrCnG06vGLHTrEqugUIm/XtKHcdVvs9RD0KYyh+sBEM72MUGd4afiB2H6 Y2VA== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=in-reply-to:content-disposition:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:message-id:subject:cc :to:from:date:dkim-signature; bh=E6KtBtO5D54TAizWr5liUPW0e0XxdxHYOo+otWMZ9+Q=; fh=pktzJshU+TYttGHjyMYoz/1PsoJvwJaokO2WqIq19Uo=; b=DrQVRggw6gF7nOdWrcz6pchFEGP/DeTOX3XGVho41g3yBy4U2BwVt89mbFrkOwzZq1 zqCIwj24YXI2SuOjlmiGoZOWmVgCC9rvpB6HipVhF7w6Ti0UoStydcWzeU9R4xJx7XVs F/HO3EWNxdhnKZrkHDPcD/S7rwcxAnxsAaIh91qq9hKY8N0FJ/TJ7swwvj9GZZSaKZum TPnuSpcmJKJKr4E43tw3KyIIVI7a38Y46A5832Fomaw38L6tfCEtCh8WyswqSY+9BAbj e+2XD4kUbBOC77I3MDeRdf4TlkZCkOn0F2QgVgvhiWLVB+nFX6ufGpnl+uqSAsi3MF2e LzNg==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=Yq0UvV+D; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-99518-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-99518-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [2604:1380:4601:e00::3]) by mx.google.com with ESMTPS id h10-20020a056402280a00b0056838bf8c3esi2939446ede.270.2024.03.11.13.37.13 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 11 Mar 2024 13:37:13 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-99518-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) client-ip=2604:1380:4601:e00::3; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=Yq0UvV+D; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-99518-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-99518-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by am.mirrors.kernel.org (Postfix) with ESMTPS id ED5A11F21C6E for ; Mon, 11 Mar 2024 20:37:12 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id AF87E433CC; Mon, 11 Mar 2024 20:37:06 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="Yq0UvV+D" 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 D23DE1DDFA for ; Mon, 11 Mar 2024 20:37:05 +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=1710189425; cv=none; b=FsbISpOhqTRGtFhOrwqGDz7lw6+I6hhpgfnZrWYysGpwVpcvA8vzeug4ybaMVbgv31TAbu0NKxxfx5HVSIv9sVxITF7pTYeRXS+yGQUZBUUTZ2Jffrvhk/K6LvWEBejFcyUAJzACumTcwkPXzQohYW51n9DcgfgAoOpF/0Kh5Oc= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1710189425; c=relaxed/simple; bh=p40CMsQzYqWdqXPJqvX5xweH+iWvW3fcgjpwVX0YcIw=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=Hhc+rWfv4hhWccDXp8b5IpjKm2E0q+WxUTCEpTyIiq9mebErHpPVnuC84On6FOB7FilsrpdHHkC0+nPMgXnmnyp77NqvV5QHImQxrS2pNxUZbeXVNHozB4lKNdELsdbg9HJjAKqt44H/+n6iwEENBXcGv+651LtvuKVXKhwPYgs= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=Yq0UvV+D; arc=none smtp.client-ip=10.30.226.201 Received: by smtp.kernel.org (Postfix) with ESMTPSA id 64348C433C7; Mon, 11 Mar 2024 20:37:05 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1710189425; bh=p40CMsQzYqWdqXPJqvX5xweH+iWvW3fcgjpwVX0YcIw=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=Yq0UvV+DnaMfrkFiCDvshg0uUt2WjDP33BK0WMWjsxDBQtXlhF2DOPrqfKz33Z2ou Cxqdb9NrOV8DmngHlfZ9QDLBQDcVnqrq1leODKsC7DGfiQ0uaTrQdogJonsKqgbgvm f6bnvI0PQrSmT1cSI9p7+X8OpKD+7SGMbIxVcc54jdR5mqUwOd8ou7GvDASZT2zoOM RvnHIk9btgd+yGGzSRMGGPjX5y0qQMra8eZbHhMOGYtCFKbm7wvN8EG3veEmgzxCR4 xvK/HCQvIwb5pkj5wx0uzZ9hs+SveyEv31T4F7kV07omN11vQ15JrRS5dDqjFs1BRN t8+ArApkXZG9g== Date: Mon, 11 Mar 2024 13:37:03 -0700 From: Jaegeuk Kim To: David Laight Cc: 'Roman Smirnov' , Chao Yu , Sergey Shtylyov , Karina Yankevich , "linux-f2fs-devel@lists.sourceforge.net" , "linux-kernel@vger.kernel.org" , "lvc-project@linuxtesting.org" Subject: Re: [PATCH] f2fs: Cast expression type to unsigned long in __count_extent_cache() Message-ID: References: <20240305080943.6922-1-r.smirnov@omp.ru> 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 03/10, David Laight wrote: > From: Roman Smirnov > > Sent: 05 March 2024 08:10 > > > > Cast expression type to unsigned long in __count_extent_cache() > > to prevent integer overflow. > > > > Found by Linux Verification Center (linuxtesting.org) with Svace. > > Another broken analysis tool :-) > > > Signed-off-by: Roman Smirnov > > Reviewed-by: Sergey Shtylyov > > --- > > fs/f2fs/shrinker.c | 2 +- > > 1 file changed, 1 insertion(+), 1 deletion(-) > > > > diff --git a/fs/f2fs/shrinker.c b/fs/f2fs/shrinker.c > > index 83d6fb97dcae..bb86a06c5d5e 100644 > > --- a/fs/f2fs/shrinker.c > > +++ b/fs/f2fs/shrinker.c > > @@ -33,7 +33,7 @@ static unsigned long __count_extent_cache(struct f2fs_sb_info *sbi, > > { > > struct extent_tree_info *eti = &sbi->extent_tree[type]; > > > > - return atomic_read(&eti->total_zombie_tree) + > > + return (unsigned long)atomic_read(&eti->total_zombie_tree) + > > atomic_read(&eti->total_ext_node); > > Makes diddly-squit difference. > > Both total_zombie_tree and totat_ext_node are 'int'. > If they are large enough that their sum wraps then the values > can individually wrap (to negative values). > > You really don't want to cast 'int' to 'unsigned long' here > at all - implicitly or explicitly. > The cast will first promote 'int' to 'signed long'. > So a negative value will get sign extended to a very big value. I thought, since total_zombie_tree won't get overflowed theoritically, the first cast to (unsigned long) could expand the space to cover the following total_ext_node. > > The best you can hope for is a 33bit result from wrapped 32bit > signed counters. > To get that you need to convert 'int' => 'unsigned int' => 'unsigned long'. > One way would be: > return (atomic_read(&eti->total_zombie_tree) + 0u + 0ul) + > (atomic_read(&eti->total_ext_node) + 0u); > > Although changing the return type to 'unsigned int' would probably > be better. > I don't know what the values are, but if they are stats counters > then that would give a value that nicely wraps at 2^32 rather > that the strange wrap that the sum of two wrapping 32bit counters > has. > > OTOH it may be that they are counts - and just can't get any where > near that big. > > David > > - > Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK > Registration No: 1397386 (Wales)