Received: by 2002:a05:6a10:f347:0:0:0:0 with SMTP id d7csp407368pxu; Tue, 1 Dec 2020 14:29:46 -0800 (PST) X-Google-Smtp-Source: ABdhPJz96z1BhsoKyS5wy8t+SDaQcXVCVF6zPuMvIhSi05nEXT2oaF2ptNiwEEa8JYXkb7DqDOU6 X-Received: by 2002:a17:906:2e55:: with SMTP id r21mr5395065eji.46.1606861786407; Tue, 01 Dec 2020 14:29:46 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1606861786; cv=none; d=google.com; s=arc-20160816; b=YIdLvHc0q3MrXseYCoOGVySw5+5XhWS3Yq282cMPMtErNmARLaJkTuPvaOVqUHhV4a H/K/EOh5B9Vk7ObgjMLwK74rKZW1luzARnEVVloShNSF5jzB9P6o8KbKf52ID+i8GdHi AHfIICv+bo86yxYZ7iPpZbKAy3A+bHQR3DXHRY5iQW2dfHo4j3IE4eTssqaP3yYWS57t klWQVxBlpj41YN5iVPceOErR9f+ULo3o/Axbgb2VihteJkA6rWHQcg8cFoMeneaecW5d rqqiKoh8YM57Owmz/JibxTkSeaKwquzdq2tpplWjFtU+49zbmI0fz7TnzOqyxpspolvO OQMg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=0w3u1bpsNQDLLEt7TBvmYcPfGo8cP18xjcSKjHOsEAk=; b=n9h+WGt8Xl2PvIA2I1xvoObMOr/mix38wB6JU/diFnyIEjJf9H0OX00GXYb7rc+h3s GWh5RsBlvVY2Prguw8A78GOavvS300TV7vWIUIgnNm92mDvkdRBu8bxNa/mHYDojFjKp TcEFs72M2CsGFG9fBrBkBNCgTTLPQjNkc7oDsrH+dQ0UL+Vaiwgq4T+mwzYn0IsK5cKI UxiumPrjdDapUlJN/Hy9WMEAi1rrf53HSS5IDhbEkyR78GAQ170kPFftORPxWhEeRecq Y6S0glaRbb+kXxXBVZ/bhBOfXrvA/oVpfx/lwe3vtQZ4syHtqs9z6EMI5XehlVB7xzxg WUaw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=fiVBQhem; 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=linuxfoundation.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id dg28si780206edb.167.2020.12.01.14.29.24; Tue, 01 Dec 2020 14:29:46 -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=@linuxfoundation.org header.s=korg header.b=fiVBQhem; 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=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2389897AbgLAJR1 (ORCPT + 99 others); Tue, 1 Dec 2020 04:17:27 -0500 Received: from mail.kernel.org ([198.145.29.99]:49266 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2389872AbgLAJLx (ORCPT ); Tue, 1 Dec 2020 04:11:53 -0500 Received: from localhost (83-86-74-64.cable.dynamic.v4.ziggo.nl [83.86.74.64]) (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 A201721D7F; Tue, 1 Dec 2020 09:11:31 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1606813892; bh=Xgr2hT6XQ5MFZyBnqpU1BTkWXg00+fF+xr2EtuvpldI=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=fiVBQhemKtMbfx6+pADfdBIOHS9hJutwubTcmBII7mVC1Q4Odu4SmruqUbkxUElOW IRjABZAVWQS9wgZqtc50uJkqgqNepLK0InvlCYQfHaOjafUBO5eYAn6S7c8QZvM6KI YroltZDbcy2lvdfs861IZqLUoeAatCXNUjTZiY3E= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Filipe Manana , Qu Wenruo , David Sterba , Sasha Levin Subject: [PATCH 5.9 078/152] btrfs: qgroup: dont commit transaction when we already hold the handle Date: Tue, 1 Dec 2020 09:53:13 +0100 Message-Id: <20201201084722.110625990@linuxfoundation.org> X-Mailer: git-send-email 2.29.2 In-Reply-To: <20201201084711.707195422@linuxfoundation.org> References: <20201201084711.707195422@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Qu Wenruo [ Upstream commit 6f23277a49e68f8a9355385c846939ad0b1261e7 ] [BUG] When running the following script, btrfs will trigger an ASSERT(): #/bin/bash mkfs.btrfs -f $dev mount $dev $mnt xfs_io -f -c "pwrite 0 1G" $mnt/file sync btrfs quota enable $mnt btrfs quota rescan -w $mnt # Manually set the limit below current usage btrfs qgroup limit 512M $mnt $mnt # Crash happens touch $mnt/file The dmesg looks like this: assertion failed: refcount_read(&trans->use_count) == 1, in fs/btrfs/transaction.c:2022 ------------[ cut here ]------------ kernel BUG at fs/btrfs/ctree.h:3230! invalid opcode: 0000 [#1] SMP PTI RIP: 0010:assertfail.constprop.0+0x18/0x1a [btrfs] btrfs_commit_transaction.cold+0x11/0x5d [btrfs] try_flush_qgroup+0x67/0x100 [btrfs] __btrfs_qgroup_reserve_meta+0x3a/0x60 [btrfs] btrfs_delayed_update_inode+0xaa/0x350 [btrfs] btrfs_update_inode+0x9d/0x110 [btrfs] btrfs_dirty_inode+0x5d/0xd0 [btrfs] touch_atime+0xb5/0x100 iterate_dir+0xf1/0x1b0 __x64_sys_getdents64+0x78/0x110 do_syscall_64+0x33/0x80 entry_SYSCALL_64_after_hwframe+0x44/0xa9 RIP: 0033:0x7fb5afe588db [CAUSE] In try_flush_qgroup(), we assume we don't hold a transaction handle at all. This is true for data reservation and mostly true for metadata. Since data space reservation always happens before we start a transaction, and for most metadata operation we reserve space in start_transaction(). But there is an exception, btrfs_delayed_inode_reserve_metadata(). It holds a transaction handle, while still trying to reserve extra metadata space. When we hit EDQUOT inside btrfs_delayed_inode_reserve_metadata(), we will join current transaction and commit, while we still have transaction handle from qgroup code. [FIX] Let's check current->journal before we join the transaction. If current->journal is unset or BTRFS_SEND_TRANS_STUB, it means we are not holding a transaction, thus are able to join and then commit transaction. If current->journal is a valid transaction handle, we avoid committing transaction and just end it This is less effective than committing current transaction, as it won't free metadata reserved space, but we may still free some data space before new data writes. Bugzilla: https://bugzilla.suse.com/show_bug.cgi?id=1178634 Fixes: c53e9653605d ("btrfs: qgroup: try to flush qgroup space when we get -EDQUOT") Reviewed-by: Filipe Manana Signed-off-by: Qu Wenruo Signed-off-by: David Sterba Signed-off-by: Sasha Levin --- fs/btrfs/qgroup.c | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/fs/btrfs/qgroup.c b/fs/btrfs/qgroup.c index 9af010131d589..9205a88f2a881 100644 --- a/fs/btrfs/qgroup.c +++ b/fs/btrfs/qgroup.c @@ -3516,6 +3516,7 @@ static int try_flush_qgroup(struct btrfs_root *root) { struct btrfs_trans_handle *trans; int ret; + bool can_commit = true; /* * We don't want to run flush again and again, so if there is a running @@ -3527,6 +3528,20 @@ static int try_flush_qgroup(struct btrfs_root *root) return 0; } + /* + * If current process holds a transaction, we shouldn't flush, as we + * assume all space reservation happens before a transaction handle is + * held. + * + * But there are cases like btrfs_delayed_item_reserve_metadata() where + * we try to reserve space with one transction handle already held. + * In that case we can't commit transaction, but at least try to end it + * and hope the started data writes can free some space. + */ + if (current->journal_info && + current->journal_info != BTRFS_SEND_TRANS_STUB) + can_commit = false; + ret = btrfs_start_delalloc_snapshot(root); if (ret < 0) goto out; @@ -3538,7 +3553,10 @@ static int try_flush_qgroup(struct btrfs_root *root) goto out; } - ret = btrfs_commit_transaction(trans); + if (can_commit) + ret = btrfs_commit_transaction(trans); + else + ret = btrfs_end_transaction(trans); out: clear_bit(BTRFS_ROOT_QGROUP_FLUSHING, &root->state); wake_up(&root->qgroup_flush_wait); -- 2.27.0