Received: by 2002:a05:6a10:1287:0:0:0:0 with SMTP id d7csp5216625pxv; Wed, 28 Jul 2021 06:04:01 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxBfGFc4pDemhP2qc1biRwd/qv0+NQKYxTcnANOv+owg+5G7wNUnpzdos05W+SZ01cOmUdp X-Received: by 2002:ac2:4943:: with SMTP id o3mr20030993lfi.414.1627477441121; Wed, 28 Jul 2021 06:04:01 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1627477441; cv=none; d=google.com; s=arc-20160816; b=op/KuObmmZKuIXMnGhOGOYkaNEmNSVd7JyIAHouhmLWBWD1vLjm8lPKDu/8un3iXKN bDDRyo8yXgtqbr7zBDdHIw0qMmeAhefRbogCKJsdEpUcpPn0h0y8nc12Tzmstv40D2ag RPHqZuaplAldaYqz+phTtMbkyeS7vgf2unnl/AEcqKhyvPOnsm2rI501Z/jQIwIglwtq BETQppI8gmn4pJaGw8iLDhIQmm8v4i1XZE7lu2cdVj6Cqkfr28gcF/yATbCmt1FLLfxU 7LyAGbO6yQNWwE5qv39tURjBfAy2GRa6MnqaeG0HvXE4Ai8T0Dtiy55KCrMC0QqpGKcH ZaWQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:user-agent:in-reply-to:content-disposition :mime-version:references:mail-followup-to:reply-to:message-id :subject:cc:to:from:date:dkim-signature:dkim-signature; bh=qBVxtrSmWVlOscO72s3n1kpnLDIq1UYxTJT1qArgmmQ=; b=ulygDC0N+eD/drqXqWzhkq+AKR94OxA/bo4ObyUzrYPFmQNr4mDM0zaWz+FYEXFjYS NjRDr466eRWBVcVT9Z3LIWqZeI0kxyKCfOTr23dhsvX1ydTjqkEccQRWVK3dCn4DmGs4 4hGgoDC7IinKbNqfYaFYGArI7Zob3A8kSt+BB2iRiGk6WfnpdzusOHiYUIV4BCGVGPK0 zuUXkgq9ui2qjaBgCyr91RrZK20G1ICvU6pTGzEfnt8PVmqMuOifZmTXWuV7zzBSHUVS V7t9eun3jjpYHoHXZuY1bZVlwpLegbEXDcD/QaLzJeYodAqEQ6sOnBgrLvH+lix+gJo3 cUSA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@suse.cz header.s=susede2_rsa header.b=v9G2MRBI; dkim=neutral (no key) header.i=@suse.cz; 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 ee47si5848402edb.7.2021.07.28.06.03.35; Wed, 28 Jul 2021 06:04:01 -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; dkim=pass header.i=@suse.cz header.s=susede2_rsa header.b=v9G2MRBI; dkim=neutral (no key) header.i=@suse.cz; 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 S236108AbhG1NAy (ORCPT + 99 others); Wed, 28 Jul 2021 09:00:54 -0400 Received: from smtp-out2.suse.de ([195.135.220.29]:43784 "EHLO smtp-out2.suse.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235954AbhG1NAw (ORCPT ); Wed, 28 Jul 2021 09:00:52 -0400 Received: from relay2.suse.de (relay2.suse.de [149.44.160.134]) by smtp-out2.suse.de (Postfix) with ESMTP id 351D91FFAB; Wed, 28 Jul 2021 13:00:50 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.cz; s=susede2_rsa; t=1627477250; h=from:from:reply-to:reply-to:date:date:message-id:message-id:to:to: cc:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=qBVxtrSmWVlOscO72s3n1kpnLDIq1UYxTJT1qArgmmQ=; b=v9G2MRBI9ZC4oh0wXp76nkFZulbeIh/X9Kl1ZGYf/vBtLRwjYZPKCWMjGaRiT7QKYSZ90s i2rYnube6FOkbpHqj9DPbp0cx+VKOZb1YjHEpJqCBf25YPC8FoasiJ5BPJ1X7OLwnWRPBA sU8h+anD6jFn6PPXXnDbdwGz2vDQy+M= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.cz; s=susede2_ed25519; t=1627477250; h=from:from:reply-to:reply-to:date:date:message-id:message-id:to:to: cc:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=qBVxtrSmWVlOscO72s3n1kpnLDIq1UYxTJT1qArgmmQ=; b=+dVFHCK5TxbazeQB9uIkp8YvmgS+oT8ugRDKduJ54Ig3smLynu17XhzYkBjIKnruYW57R2 r6oB1pCP3NPKd9Bg== Received: from ds.suse.cz (ds.suse.cz [10.100.12.205]) by relay2.suse.de (Postfix) with ESMTP id 1D3ACA3B93; Wed, 28 Jul 2021 13:00:50 +0000 (UTC) Received: by ds.suse.cz (Postfix, from userid 10065) id 49595DA8A7; Wed, 28 Jul 2021 14:58:05 +0200 (CEST) Date: Wed, 28 Jul 2021 14:58:05 +0200 From: David Sterba To: Desmond Cheong Zhi Xi Cc: clm@fb.com, josef@toxicpanda.com, dsterba@suse.com, anand.jain@oracle.com, linux-btrfs@vger.kernel.org, linux-kernel@vger.kernel.org, skhan@linuxfoundation.org, gregkh@linuxfoundation.org, linux-kernel-mentees@lists.linuxfoundation.org, syzbot+a70e2ad0879f160b9217@syzkaller.appspotmail.com Subject: Re: [PATCH v2] btrfs: fix rw device counting in __btrfs_free_extra_devids Message-ID: <20210728125805.GE5047@twin.jikos.cz> Reply-To: dsterba@suse.cz Mail-Followup-To: dsterba@suse.cz, Desmond Cheong Zhi Xi , clm@fb.com, josef@toxicpanda.com, dsterba@suse.com, anand.jain@oracle.com, linux-btrfs@vger.kernel.org, linux-kernel@vger.kernel.org, skhan@linuxfoundation.org, gregkh@linuxfoundation.org, linux-kernel-mentees@lists.linuxfoundation.org, syzbot+a70e2ad0879f160b9217@syzkaller.appspotmail.com References: <20210727071303.113876-1-desmondcheongzx@gmail.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20210727071303.113876-1-desmondcheongzx@gmail.com> User-Agent: Mutt/1.5.23.1-rc1 (2014-03-12) Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Tue, Jul 27, 2021 at 03:13:03PM +0800, Desmond Cheong Zhi Xi wrote: > When removing a writeable device in __btrfs_free_extra_devids, the rw > device count should be decremented. > > This error was caught by Syzbot which reported a warning in > close_fs_devices because fs_devices->rw_devices was not 0 after > closing all devices. Here is the call trace that was observed: > > btrfs_mount_root(): > btrfs_scan_one_device(): > device_list_add(); <---------------- device added > btrfs_open_devices(): > open_fs_devices(): > btrfs_open_one_device(); <-------- writable device opened, > rw device count ++ > btrfs_fill_super(): > open_ctree(): > btrfs_free_extra_devids(): > __btrfs_free_extra_devids(); <--- writable device removed, > rw device count not decremented > fail_tree_roots: > btrfs_close_devices(): > close_fs_devices(); <------- rw device count off by 1 > > As a note, prior to commit cf89af146b7e ("btrfs: dev-replace: fail > mount if we don't have replace item with target device"), rw_devices > was decremented on removing a writable device in > __btrfs_free_extra_devids only if the BTRFS_DEV_STATE_REPLACE_TGT bit > was not set for the device. However, this check does not need to be > reinstated as it is now redundant and incorrect. > > In __btrfs_free_extra_devids, we skip removing the device if it is the > target for replacement. This is done by checking whether device->devid > == BTRFS_DEV_REPLACE_DEVID. Since BTRFS_DEV_STATE_REPLACE_TGT is set > only on the device with devid BTRFS_DEV_REPLACE_DEVID, no devices > should have the BTRFS_DEV_STATE_REPLACE_TGT bit set after the check, > and so it's redundant to test for that bit. > > Additionally, following commit 82372bc816d7 ("Btrfs: make > the logic of source device removing more clear"), rw_devices is > incremented whenever a writeable device is added to the alloc > list (including the target device in btrfs_dev_replace_finishing), so > all removals of writable devices from the alloc list should also be > accompanied by a decrement to rw_devices. > > Fixes: cf89af146b7e ("btrfs: dev-replace: fail mount if we don't have replace item with target device") > Reported-by: syzbot+a70e2ad0879f160b9217@syzkaller.appspotmail.com > Tested-by: syzbot+a70e2ad0879f160b9217@syzkaller.appspotmail.com > Signed-off-by: Desmond Cheong Zhi Xi > Reviewed-by: Anand Jain Added to misc-next, thanks.