Received: by 2002:a05:6a10:f347:0:0:0:0 with SMTP id d7csp256803pxu; Wed, 2 Dec 2020 22:11:59 -0800 (PST) X-Google-Smtp-Source: ABdhPJwcjPkXjRLEMkl/jlcFNJ4wwnhC333te1BGfaM/fEc0XsMONZCW+C2TrSHZlTT7gL6/6nfT X-Received: by 2002:aa7:c5d7:: with SMTP id h23mr1428147eds.149.1606975919308; Wed, 02 Dec 2020 22:11:59 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1606975919; cv=none; d=google.com; s=arc-20160816; b=WRv0sMNAAOHmDu8fP54AUJozsvSLLv56IkqI80EMdUNSXPyyF0kOMDxnrqnMJlp5fV aUORDRaBOh6K0Rq2gr2GhJfUsy0hRkVrpK2P8Lri91fvzuPVXkLhTiWqQx9fY2XbGUZR ABJqWQRnx9T/n6Haz+OiDz0ullSd8s931BnnzhT+wB+qW6rnX//IGIsE8HK/eFz6mGDD QoeEDgBcom7jAk3fkXTTHe0fhbfM5FDcVKRE1IPuH0L724mCPua3a6p8Dc8VGTQsUjqe hpSGZ5zeMyxK4k00Q7z6jWysvlcmE5fgkaOukhDuOIfI54o6hSrIxK/s5Uy0GrkqObwi oJqw== 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:dkim-signature:date; bh=Z92OVo44RqEr+pK+EWF4aEiJh9A7Ij2ah0rKWdjFbr0=; b=UNUhZ1Ek/mC8d/srPtAgKk70Frk3L8/5nHlw+QzVloz+wuCjRQ2OGnGU8HrIOuny52 lCn3NAGEG8/7fivYXSKRxPV546pGxyP2ISdNL84tCAWLEEDjtBSwKOzzXuJYj+1mzHhi BGmi84IfxYUXgNLQdFmYae0940W6VLKcKwYI0NtNDDD6B9AKAsFnD1+xJQRKlmz3HADT oqzEKdERfkM73OBcaxiLiu5e3Fr5yCpPcAT9cEba0iB7udnJq+E7xguhsD3WRwQJBHNS uruciNZGBMu9TS3OVNHtAhT1BbfhCJcmRjCfFh4Tdbq75P3oZtuTxZcyTIt1ZYyyVfIQ lk5Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=CNLKdNI9; 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 y9si348925edq.205.2020.12.02.22.11.36; Wed, 02 Dec 2020 22:11:59 -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=k20201202 header.b=CNLKdNI9; 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 S1728309AbgLCGHr (ORCPT + 99 others); Thu, 3 Dec 2020 01:07:47 -0500 Received: from mail.kernel.org ([198.145.29.99]:42226 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726707AbgLCGHp (ORCPT ); Thu, 3 Dec 2020 01:07:45 -0500 Date: Wed, 2 Dec 2020 22:07:03 -0800 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1606975625; bh=4kjEtMFrjeavxN2mKqcqGSsSXeUIsDMbjS/Wf+Mx0Pg=; h=From:To:Cc:Subject:References:In-Reply-To:From; b=CNLKdNI9nav5vjFiwqpHfrqa7SXQ6v+4dePorn5Eg16hdtZTBTzPd+tYZWN3U9l44 vZSmWU1IJVsGnGqIx8p87LRPDCxyE7BkqxfI21iqJjY8lDT1q0CV+3ZMFAiio9gPdC lwxr/vFiSI/xOanTRDPbsEQWC8RzrN5438XfqhvGtR2ol0GhOWTVFvdZKYaEzHTye1 8h18Igg0otkF9koO/n1u1r6ZZ88Pc+1Z4Zjmh2hN5A6Tld/6gWbZ6b2WvFrk7GSnlf wBmycrI6GhoEXGUyAbi+Eh0MalQcEuLbLL740WeKgfZGxnhfOMD+4GOOt1zyc7wyGV ej9vku+173V2Q== From: Jaegeuk Kim To: linux-kernel@vger.kernel.org, linux-f2fs-devel@lists.sourceforge.net, kernel-team@android.com Cc: Light Hsieh Subject: Re: [f2fs-dev] [PATCH v3] f2fs: avoid race condition for shinker count Message-ID: References: <20201109170012.2129411-1-jaegeuk@kernel.org> <20201112053414.GB3826485@google.com> <20201112054051.GA4092972@google.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20201112054051.GA4092972@google.com> Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 11/11, Jaegeuk Kim wrote: > Light reported sometimes shinker gets nat_cnt < dirty_nat_cnt resulting in > wrong do_shinker work. Let's avoid to get stale data by using nat_tree_lock. > > Reported-by: Light Hsieh > Signed-off-by: Jaegeuk Kim > --- > v3: > - fix to use NM_I(sbi) > > fs/f2fs/shrinker.c | 6 +++++- > 1 file changed, 5 insertions(+), 1 deletion(-) > > diff --git a/fs/f2fs/shrinker.c b/fs/f2fs/shrinker.c > index d66de5999a26..555712ba06d8 100644 > --- a/fs/f2fs/shrinker.c > +++ b/fs/f2fs/shrinker.c > @@ -18,7 +18,11 @@ static unsigned int shrinker_run_no; > > static unsigned long __count_nat_entries(struct f2fs_sb_info *sbi) > { > - long count = NM_I(sbi)->nat_cnt - NM_I(sbi)->dirty_nat_cnt; > + long count; > + > + down_read(&NM_I(sbi)->nat_tree_lock); > + count = NM_I(sbi)->nat_cnt - NM_I(sbi)->dirty_nat_cnt; > + up_read(&NM_I(sbi)->nat_tree_lock); I just fosund this can give kernel hang due to the following backtrace. f2fs_shrink_count shrink_slab shrink_node do_try_to_free_pages try_to_free_pages __alloc_pages_nodemask alloc_pages_current allocate_slab __slab_alloc __slab_alloc kmem_cache_alloc add_free_nid f2fs_flush_nat_entries f2fs_write_checkpoint Let me just check like this. From 971163330224449d90aac90957ea38f77d494f0f Mon Sep 17 00:00:00 2001 From: Jaegeuk Kim Date: Fri, 6 Nov 2020 13:22:05 -0800 Subject: [PATCH] f2fs: avoid race condition for shrinker count Light reported sometimes shinker gets nat_cnt < dirty_nat_cnt resulting in wrong do_shinker work. Let's avoid to return insane overflowed value. Reported-by: Light Hsieh Signed-off-by: Jaegeuk Kim --- fs/f2fs/shrinker.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/fs/f2fs/shrinker.c b/fs/f2fs/shrinker.c index d66de5999a26..75b5b4aaed99 100644 --- a/fs/f2fs/shrinker.c +++ b/fs/f2fs/shrinker.c @@ -18,9 +18,9 @@ static unsigned int shrinker_run_no; static unsigned long __count_nat_entries(struct f2fs_sb_info *sbi) { - long count = NM_I(sbi)->nat_cnt - NM_I(sbi)->dirty_nat_cnt; - - return count > 0 ? count : 0; + if (NM_I(sbi)->nat_cnt > NM_I(sbi)->dirty_nat_cnt) + return NM_I(sbi)->nat_cnt - NM_I(sbi)->dirty_nat_cnt; + return 0; } static unsigned long __count_free_nids(struct f2fs_sb_info *sbi) -- 2.29.2.454.gaff20da3a2-goog > > return count > 0 ? count : 0; > } > -- > 2.29.2.299.gdc1121823c-goog > > > > _______________________________________________ > Linux-f2fs-devel mailing list > Linux-f2fs-devel@lists.sourceforge.net > https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel