Received: by 2002:a05:6a10:16a7:0:0:0:0 with SMTP id gp39csp104594pxb; Wed, 11 Nov 2020 21:43:56 -0800 (PST) X-Google-Smtp-Source: ABdhPJy+Zvu0f+ALwmcFuMAg+GcoaWUiEuUvAy4txIY0Hep3Ox51TIn/DSuUviC6tnSvYLR4Xn0m X-Received: by 2002:a50:d616:: with SMTP id x22mr3344958edi.221.1605159836226; Wed, 11 Nov 2020 21:43:56 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1605159836; cv=none; d=google.com; s=arc-20160816; b=ia4XlKrwoOnr+P5oYYduTJZK78bJHpV0Bu0hQx4JQgAkgln132Hn+N8UrDmg86XYcy GpoyuKiC9JUZ+nw9ubjqx9XD9+cUE6X2BEtZTwHB7PcYxs/qOGx7lJWaHoZEAp3Zs0iq 0/cWVDBuItOXfHWBt/U6IHBSOPY898ZVKopfpho+1uD293ysohhqcHTMTOj9BfcfeZaC maOWIyf29ZG8kaxER5nzdpgM85wPm7e2qGp+5wUhdES6NmwtyzSbgbSSTa2MNvI630sb smGJ17dQ0iC11YoK5CwaFjXrnchGffabmgXmVUZpasWhK+aOQv6nQS6ebYCL+vYNhwEA 3yFQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature; bh=yYXntx3pkgKGAM654zCcHKHjA0Bf7KMW3fJU5Pqeuko=; b=COkpm37+gVkWAGZA0AU6wBS68Rk7JrkIfnfHnvDkLz/SJPbns58b/ThPZYzM974niX M1gcU3yAHkMd5LJeoJudDH7lnzmbzm4LRjmsryqtih6nHV7+A/f8xzWtGgZ4bQlLEv6u 47c4IRbH6LDMEOOGDP+IQWNbJyaCHVQ1xtHWOaDvp9reqeVOECFCOPmME+wRtLpfmqPc 9X5vGidkdTBCYrnnHxpOCB014YmItN8xl84MZ3DFYJrdqOV1/Vhi4CmVOAFnpFk3wh/5 aYJdt21wDkzV/k8TZNg98VGHfEu57jlpVDUfJIY01CqZ9EBuKsiDExwfrMMTjjRrxfFc C/ww== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=d3AR2qLK; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id nw21si3110090ejb.708.2020.11.11.21.43.33; Wed, 11 Nov 2020 21:43:56 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=d3AR2qLK; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1729601AbgKLFjU (ORCPT + 99 others); Thu, 12 Nov 2020 00:39:20 -0500 Received: from mail.kernel.org ([198.145.29.99]:52646 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725979AbgKLFb0 (ORCPT ); Thu, 12 Nov 2020 00:31:26 -0500 Received: from google.com (unknown [104.132.1.66]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 1E7AC2076E; Thu, 12 Nov 2020 05:31:25 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1605159085; bh=zRwrA+HVZcRHjHL8H5aKqesuUNcgOMvjAT62h6bxZok=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=d3AR2qLK1zbYHW6qXTgphkNgujefkn3Ak2YgPdDfkWVcPx8Mfi9dGl411u4cbvM+j NgwcrHeFYf/4emPwMF8whc0//GvmBSA4HU8qUjfD9ovy8ReIEFzMe2Jz3JN38ClS1M PW1wsNBhyOmCZZ2CZMM6ZXs+7BbvrdDHS0dvGDkk= Date: Wed, 11 Nov 2020 21:31:22 -0800 From: Jaegeuk Kim To: Chao Yu Cc: linux-kernel@vger.kernel.org, linux-f2fs-devel@lists.sourceforge.net, kernel-team@android.com, Light Hsieh Subject: Re: [f2fs-dev] [PATCH] f2fs: avoid race condition for shinker count Message-ID: <20201112053122.GA3826485@google.com> References: <20201109170012.2129411-1-jaegeuk@kernel.org> <20201110041958.GA1598246@google.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 11/10, Chao Yu wrote: > On 2020/11/10 12:19, Jaegeuk Kim wrote: > > On 11/10, Chao Yu wrote: > > > On 2020/11/10 1:00, Jaegeuk Kim wrote: > > > > Light reported sometimes shinker gets nat_cnt < dirty_nat_cnt resulting in > > > > > > I didn't get the problem clearly, did you mean __count_nat_entries() will > > > give the wrong shrink count due to race condition? should there be a lock > > > while reading these two variables? > > > > > > > wrong do_shinker work. Basically the two counts should not happen like that. > > > > > > > > So, I suspect this race condtion where: > > > > - f2fs_try_to_free_nats __flush_nat_entry_set > > > > nat_cnt=2, dirty_nat_cnt=2 > > > > __clear_nat_cache_dirty > > > > spin_lock(nat_list_lock) > > > > list_move() > > > > spin_unlock(nat_list_lock) > > > > spin_lock(nat_list_lock) > > > > list_del() > > > > spin_unlock(nat_list_lock) > > > > nat_cnt=1, dirty_nat_cnt=2 > > > > nat_cnt=1, dirty_nat_cnt=1 > > > > > > nm_i->nat_cnt and nm_i->dirty_nat_cnt were protected by > > > nm_i->nat_tree_lock, I didn't see why expanding nat_list_lock range > > > will help... since there are still places nat_list_lock() didn't > > > cover these two reference counts. > > > > Yeah, I missed nat_tree_lock, and indeed it should cover this. So, the problem > > is Light reported subtle case of nat_cnt < dirty_nat_cnt in shrink_count. > > We may need to use nat_tree_lock in shrink_count? > > change like this? > Yup. > __count_nat_entries() > > down_read(&nm_i->nat_tree_lock); > count = NM_I(sbi)->nat_cnt - NM_I(sbi)->dirty_nat_cnt; > up_read(&nm_i->nat_tree_lock); > > Thanks, > > > > > > > > > Thanks, > > > > > > > > > > > Reported-by: Light Hsieh > > > > Signed-off-by: Jaegeuk Kim > > > > --- > > > > fs/f2fs/node.c | 3 +-- > > > > 1 file changed, 1 insertion(+), 2 deletions(-) > > > > > > > > diff --git a/fs/f2fs/node.c b/fs/f2fs/node.c > > > > index 42394de6c7eb..e8ec65e40f06 100644 > > > > --- a/fs/f2fs/node.c > > > > +++ b/fs/f2fs/node.c > > > > @@ -269,11 +269,10 @@ static void __clear_nat_cache_dirty(struct f2fs_nm_info *nm_i, > > > > { > > > > spin_lock(&nm_i->nat_list_lock); > > > > list_move_tail(&ne->list, &nm_i->nat_entries); > > > > - spin_unlock(&nm_i->nat_list_lock); > > > > - > > > > set_nat_flag(ne, IS_DIRTY, false); > > > > set->entry_cnt--; > > > > nm_i->dirty_nat_cnt--; > > > > + spin_unlock(&nm_i->nat_list_lock); > > > > } > > > > static unsigned int __gang_lookup_nat_set(struct f2fs_nm_info *nm_i, > > > > > > . > >