From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from lists.sourceforge.net (lists.sourceforge.net [216.105.38.7]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id A0DB9C433EF for ; Fri, 22 Apr 2022 17:07:15 +0000 (UTC) Received: from [127.0.0.1] (helo=sfs-ml-2.v29.lw.sourceforge.com) by sfs-ml-2.v29.lw.sourceforge.com with esmtp (Exim 4.94.2) (envelope-from ) id 1nhwkQ-0002V2-Sd; Fri, 22 Apr 2022 17:07:11 +0000 Received: from [172.30.20.202] (helo=mx.sourceforge.net) by sfs-ml-2.v29.lw.sourceforge.com with esmtps (TLS1.2) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1nhwkP-0002Uv-Ct for linux-f2fs-devel@lists.sourceforge.net; Fri, 22 Apr 2022 17:07:10 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=sourceforge.net; s=x; h=In-Reply-To:Content-Type:MIME-Version:References: Message-ID:Subject:Cc:To:From:Date:Sender:Reply-To:Content-Transfer-Encoding: Content-ID:Content-Description:Resent-Date:Resent-From:Resent-Sender: Resent-To:Resent-Cc:Resent-Message-ID:List-Id:List-Help:List-Unsubscribe: List-Subscribe:List-Post:List-Owner:List-Archive; bh=bUnOalO935k0JJsJQhRhk55CFalmW4UqHTehykQy6Gs=; b=Dpt9u4KCwh5nLqYrOqa0T5+uni lIz39ELhOBWVU0nBGzVJYPU6ry7HZyKcj6dz7rG1eMtEIpy7UGlEratDTcNVr1EYXHjgKCUQf13tE 6TmRDGcrrgZYIbeGCq//WnssCu1hMRKBohXSG6X1WT5e0TJFFT80kLX54cynD3hYKnYI=; DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=sf.net; s=x ; h=In-Reply-To:Content-Type:MIME-Version:References:Message-ID:Subject:Cc:To :From:Date:Sender:Reply-To:Content-Transfer-Encoding:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:List-Id:List-Help:List-Unsubscribe:List-Subscribe: List-Post:List-Owner:List-Archive; bh=bUnOalO935k0JJsJQhRhk55CFalmW4UqHTehykQy6Gs=; b=gS9LsAdPySLYsx466yB4fg9M6J bhsFeIsHuR9B6L+YhrIP7NOjUSn31A9uOzKo4tIh57CsvfoLIS0bXgifPIDxb3z93A4SjXMYknDC9 NJ9wTQrAxv3aTZMVs2ng9KtKpv1f3J04wqSEFxQ7jp0YbeOJZpsBB2M2TO3nesdIlwiU=; Received: from dfw.source.kernel.org ([139.178.84.217]) by sfi-mx-1.v28.lw.sourceforge.com with esmtps (TLS1.2:ECDHE-RSA-AES256-GCM-SHA384:256) (Exim 4.94.2) id 1nhwkN-006isb-Dg for linux-f2fs-devel@lists.sourceforge.net; Fri, 22 Apr 2022 17:07:10 +0000 Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id 2FE6B6215F; Fri, 22 Apr 2022 17:06:57 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 6E6EDC385A0; Fri, 22 Apr 2022 17:06:56 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1650647216; bh=K6Uor3nWfDQnKtHBIqsfPkXJHzFiKuJkXPNepFuhfiI=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=MnsFS2DnzvvtqTtcGPdZF34MXSR9rabN8/W0NhI5aBYIe4Ltc1tDX2DmeNHqcPApL IO1Jq5L1Kx0Qd5iuaASctI6lO00dcDVsa0jucLWdgqGWXg4D4yTl9aULtmmNk1pK/3 vQUGCIuglL7IhYhTbaeOrk7IcQBtVfzXYYH12P5tgw25+fYRm0Xbatv6TK2gIHtiz/ iElJiBOyrfmzVVmgcMsjt+8MKCezTBeR4np/lpMClS/SIAdXd5DzmT8z4Z3462X1ia pG+ODn/p2iXg7q0Xt3CeanXcJe0WbfRabTCkym1oLB0i0v2LHDcYai2ay8yFlLn+h/ A3yAkd7v9Nagw== Date: Fri, 22 Apr 2022 10:06:54 -0700 From: Jaegeuk Kim To: Niels Dossche Message-ID: References: <20220421210948.73198-1-dossche.niels@gmail.com> MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: <20220421210948.73198-1-dossche.niels@gmail.com> X-Headers-End: 1nhwkN-006isb-Dg Subject: Re: [f2fs-dev] [PATCH] f2fs: extend stat_lock to avoid potential race in statfs X-BeenThere: linux-f2fs-devel@lists.sourceforge.net X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: linux-f2fs-devel@lists.sourceforge.net Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Errors-To: linux-f2fs-devel-bounces@lists.sourceforge.net On 04/21, Niels Dossche wrote: > The calculation for f_bfree performs a calculation involving > current_reserved_blocks and total_valid_user_blocks. Both can be > modified under stat_lock. As stat_lock is not used to read both these > values in statfs, this can lead to inconsistent results. Extend the > locking to prevent this issue. > Commit c9c8ed50d94c ("f2fs: fix to avoid potential race on sbi->unusable_block_count access/update") > already added the use of sbi->stat_lock in statfs in > order to make the calculation of multiple, different fields atomic so > that results are consistent. This is similar to that patch regarding the > change in statfs. Is this enough? It seems we also need to cover sbi->user_block_count and sbi->total_node_count by stat_lock. > > Signed-off-by: Niels Dossche > --- > > Note: > I am currently working on a static analyser to detect missing locks > using type-based static analysis as my master's thesis > in order to obtain my master's degree. > If you would like to have more details, please let me know. > This was a reported case. I manually verified the report by looking > at the code, so that I do not send wrong information or patches. > After concluding that this seems to be a true positive, I created > this patch. This was compile-tested and runtime-tested on x86_64. > This issue was found on Linux v5.17.4. > > fs/f2fs/super.c | 4 +++- > 1 file changed, 3 insertions(+), 1 deletion(-) > > diff --git a/fs/f2fs/super.c b/fs/f2fs/super.c > index ea939db18f88..ece768869187 100644 > --- a/fs/f2fs/super.c > +++ b/fs/f2fs/super.c > @@ -1739,10 +1739,12 @@ static int f2fs_statfs(struct dentry *dentry, struct kstatfs *buf) > buf->f_bsize = sbi->blocksize; > > buf->f_blocks = total_count - start_count; > + > + spin_lock(&sbi->stat_lock); > + > buf->f_bfree = user_block_count - valid_user_blocks(sbi) - > sbi->current_reserved_blocks; > > - spin_lock(&sbi->stat_lock); > if (unlikely(buf->f_bfree <= sbi->unusable_block_count)) > buf->f_bfree = 0; > else > -- > 2.35.2 _______________________________________________ Linux-f2fs-devel mailing list Linux-f2fs-devel@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel