Received: by 2002:a25:868d:0:0:0:0:0 with SMTP id z13csp61363ybk; Tue, 12 May 2020 15:23:14 -0700 (PDT) X-Google-Smtp-Source: APiQypK58lrch4jr2PKjl6/3Y5WLD3EyG2CWGppg6Z0FF1l8V3Dbshw5N0YpPECw4RhTkOLb3E0y X-Received: by 2002:a50:9b58:: with SMTP id a24mr13470275edj.256.1589322194750; Tue, 12 May 2020 15:23:14 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1589322194; cv=none; d=google.com; s=arc-20160816; b=uShM0uOzkTSvjJTGTHC7QhZK9qjeEiLDVW2imkHSt822hEBEJav1k32cSbvo9KXDsl IlQJn3Hbgjh4LtfvmEG3RO+8Zsbe7glhfH+ZRVlcxaFTSDsdTTKDt2cSeU/hiQx6M5i8 cgZ+1xeJ2GQszywGvMKxmxXjCZHyRGUBK/AIy1cNKprpAr6dqj/GzIpNgP3Sl2H9+LWe w3GAwzj+3rufQZbRpwEc9dv/k1GXklsmgNadqbkp7PNBjxMAPxd+qaHFZf4VRah/z7Bn K3GIhVMaK3x8AlY00/GLbaWenyqJ+61S0nQ9TXze7OfLqsAkTkQdvtEA5upGWaleIq19 +czQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:user-agent:in-reply-to :content-disposition:mime-version:references:mail-followup-to :reply-to:message-id:subject:cc:to:from:date; bh=H0h01p3Fj01WMfgc/YIuUTqYJN3gW+hUENOMzACCn/4=; b=hDrXksEPm/NGNqfmQ5bOV/e7Rsi8cQ3nDOx5GX+Iq6VcX0k8k9gGjhzmA8QbVOLBSQ uAPO5vhEdNQjbAk3mtpIEnliGZv79CYUb8xgoL3V0cE0pxklhFa9kITZNUcHQmcio33d +XHzHh61icwev8ZOb09bt1982gJfugJT0bGcGGvx31j/o1b3Xd+ygxSHNwev8/aizrWg xNk0tVfb5ia6RJD+FbTJCGq5sZ+KoZhF7SNvUKgIwa0GZbvty3EI67ZCvgoqMZstOkHE IhLEGJtenaNQZqS30Z+JlyLh/zDbLUfMvA1EFXkrrdXDzb50qugP8CQpRa1gc5znf0es l6tw== ARC-Authentication-Results: i=1; mx.google.com; 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 Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id h5si1986140eji.230.2020.05.12.15.22.50; Tue, 12 May 2020 15:23:14 -0700 (PDT) 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; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1730712AbgELWTN (ORCPT + 99 others); Tue, 12 May 2020 18:19:13 -0400 Received: from mx2.suse.de ([195.135.220.15]:43552 "EHLO mx2.suse.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728314AbgELWTN (ORCPT ); Tue, 12 May 2020 18:19:13 -0400 X-Virus-Scanned: by amavisd-new at test-mx.suse.de Received: from relay2.suse.de (unknown [195.135.220.254]) by mx2.suse.de (Postfix) with ESMTP id 3FA7AAB99; Tue, 12 May 2020 22:19:14 +0000 (UTC) Received: by ds.suse.cz (Postfix, from userid 10065) id 5CB98DA70B; Wed, 13 May 2020 00:18:20 +0200 (CEST) Date: Wed, 13 May 2020 00:18:20 +0200 From: David Sterba To: Jia-Ju Bai Cc: clm@fb.com, josef@toxicpanda.com, dsterba@suse.com, linux-btrfs@vger.kernel.org, linux-kernel@vger.kernel.org Subject: Re: [PATCH 4/4] fs: btrfs: fix a data race in btrfs_block_rsv_release() Message-ID: <20200512221820.GF18421@twin.jikos.cz> Reply-To: dsterba@suse.cz Mail-Followup-To: dsterba@suse.cz, Jia-Ju Bai , clm@fb.com, josef@toxicpanda.com, dsterba@suse.com, linux-btrfs@vger.kernel.org, linux-kernel@vger.kernel.org References: <20200509053431.3860-1-baijiaju1990@gmail.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20200509053431.3860-1-baijiaju1990@gmail.com> User-Agent: Mutt/1.5.23.1-rc1 (2014-03-12) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Sat, May 09, 2020 at 01:34:31PM +0800, Jia-Ju Bai wrote: > The functions btrfs_block_rsv_release() and > btrfs_update_delayed_refs_rsv() are concurrently executed at runtime in > the following call contexts: > > Thread 1: > btrfs_file_write_iter() > btrfs_buffered_write() > btrfs_delalloc_release_extents() > btrfs_inode_rsv_release() > __btrfs_block_rsv_release() > > Thread 2: > finish_ordered_fn() > btrfs_finish_ordered_io() > insert_reserved_file_extent() > __btrfs_drop_extents() > btrfs_free_extent() > btrfs_add_delayed_data_ref() > btrfs_update_delayed_refs_rsv() > > In __btrfs_block_rsv_release(): > else if (... && !delayed_rsv->full) > > In btrfs_update_delayed_refs_rsv(): > spin_lock(&delayed_rsv->lock); > delayed_rsv->size += num_bytes; > delayed_rsv->full = 0; > spin_unlock(&delayed_rsv->lock); > > Thus a data race for delayed_rsv->full can occur. > This race was found and actually reproduced by our conccurency fuzzer. > > To fix this race, the spinlock delayed_rsv->lock is used to > protect the access to delayed_rsv->full in btrfs_block_rsv_release(). > > Signed-off-by: Jia-Ju Bai > --- > fs/btrfs/block-rsv.c | 7 ++++++- > 1 file changed, 6 insertions(+), 1 deletion(-) > > diff --git a/fs/btrfs/block-rsv.c b/fs/btrfs/block-rsv.c > index 27efec8f7c5b..89c53a7137b4 100644 > --- a/fs/btrfs/block-rsv.c > +++ b/fs/btrfs/block-rsv.c > @@ -277,6 +277,11 @@ u64 btrfs_block_rsv_release(struct btrfs_fs_info *fs_info, > struct btrfs_block_rsv *global_rsv = &fs_info->global_block_rsv; > struct btrfs_block_rsv *delayed_rsv = &fs_info->delayed_refs_rsv; > struct btrfs_block_rsv *target = NULL; > + unsigned short full = 0; > + > + spin_lock(&delayed_rsv->lock); > + full = delayed_rsv->full; > + spin_unlock(&delayed_rsv->lock); > > /* > * If we are the delayed_rsv then push to the global rsv, otherwise dump > @@ -284,7 +289,7 @@ u64 btrfs_block_rsv_release(struct btrfs_fs_info *fs_info, > */ > if (block_rsv == delayed_rsv) > target = global_rsv; > - else if (block_rsv != global_rsv && !delayed_rsv->full) > + else if (block_rsv != global_rsv && !full) This has been reported as suspicous https://lore.kernel.org/linux-btrfs/CAAwBoOJDjei5Hnem155N_cJwiEkVwJYvgN-tQrwWbZQGhFU=cA@mail.gmail.com/ and there's an answer that this is racy but does not cause any unexpected behaviour.