Received: by 2002:a05:6902:102b:0:0:0:0 with SMTP id x11csp1122744ybt; Tue, 7 Jul 2020 08:16:21 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyd02wry4ml9xKMQdDrpqIR+i+6VpOMrVt54Q+rz0aQY92sAm8YK/K0m0YexGA3vw3fM4jN X-Received: by 2002:a17:906:6d0e:: with SMTP id m14mr47231492ejr.251.1594134981104; Tue, 07 Jul 2020 08:16:21 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1594134981; cv=none; d=google.com; s=arc-20160816; b=YURIw/LgMRjdnN6Jq70HzfjRVFcJeYavS48X7/KHRyPZOfoddMJYVDzhaTR9CWGzAD 0wFhME9vZrvS7T78ZqSqONtudYzVyjO4P8z2Ugw+cmNQ18zFV3qalaRJoplgdZssJH5Y 6ZvFIoH3TSJvsbw5KU/rt7lJK9YcLTS2eGp5Unpr4tbxot1y9CfX+efm5FVMVyIAi3nQ 3stqWhxF+MCDMt0ROhmsUNq/siUlI89ZAY0RIkeIkVjeO/KknIzhQAkNZgebnoguDVa3 h45APeaZrD97pfJf8v7T5kWEBezEnfdKNztLDezzJUYq5VaDeYxEaQNLUAVWN+BC4fkt 5WtA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=mTRIs+eq45dx8hjaLt11jntyX3ydY/Gq4wKQgcZ+e3s=; b=nWqr3ZWOaJqLi+z4Lp41K3WsmRwOxfMc4CvIb9aUsZEBD1CtoNeMfAaPBpyjJO9N8f AeAdH/iYvlaXHxrhLoKSvHAYVNL0VeM7KFA5ExCnUwtDG2jfN2zmDDUkekPfyhASBo0a 91C/6FVr+1OWhvxSYAQM/kjjusmarnDLRvtCA12u2DZRdmSvAMU6IviakuAY1a65f9NE jTuRih+1uY9ur7eEifn++oqMwEtX/FAP6IHbtnIDXdbe8/UyoTWjE0nuzRvHhvzLBTfd hFkDlucu46ahrcdqJKQnaazL5uoElDAS3kHlESHLOa0419WZlQsNCj6N1SQLkvekBS9G KP3Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=GGN5qIVC; 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 j13si15149498eds.407.2020.07.07.08.15.56; Tue, 07 Jul 2020 08:16:21 -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=@kernel.org header.s=default header.b=GGN5qIVC; 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 S1728834AbgGGPPI (ORCPT + 99 others); Tue, 7 Jul 2020 11:15:08 -0400 Received: from mail.kernel.org ([198.145.29.99]:54630 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725944AbgGGPPE (ORCPT ); Tue, 7 Jul 2020 11:15:04 -0400 Received: from localhost (83-86-89-107.cable.dynamic.v4.ziggo.nl [83.86.89.107]) (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 70A7D2065D; Tue, 7 Jul 2020 15:15:02 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1594134903; bh=UovBvNl176vXXkoshjlCvEe5FE1uU/GFN9ITd37nFdY=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=GGN5qIVCQ45zJl1ka2kskibMJQthReFlJNJtljuA/1Up6iwKKo4fQyTtHvOmFmROA oVPcz1Hwl/Jj9hSxZUeX/lbjU/cz4FCpZ+VjP2VnVECIGbbrnRh5w2aoBrS3lxrYYr royQafYI6Zmgm1AP0Y4+60AIHuVMK7JEyJh9gLEg= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Nikolay Borisov , Anand Jain , Filipe Manana , David Sterba , Sasha Levin Subject: [PATCH 4.9 01/24] btrfs: fix a block group ref counter leak after failure to remove block group Date: Tue, 7 Jul 2020 17:13:33 +0200 Message-Id: <20200707145749.020067363@linuxfoundation.org> X-Mailer: git-send-email 2.27.0 In-Reply-To: <20200707145748.952502272@linuxfoundation.org> References: <20200707145748.952502272@linuxfoundation.org> User-Agent: quilt/0.66 X-stable: review X-Patchwork-Hint: ignore MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Filipe Manana [ Upstream commit 9fecd13202f520f3f25d5b1c313adb740fe19773 ] When removing a block group, if we fail to delete the block group's item from the extent tree, we jump to the 'out' label and end up decrementing the block group's reference count once only (by 1), resulting in a counter leak because the block group at that point was already removed from the block group cache rbtree - so we have to decrement the reference count twice, once for the rbtree and once for our lookup at the start of the function. There is a second bug where if removing the free space tree entries (the call to remove_block_group_free_space()) fails we end up jumping to the 'out_put_group' label but end up decrementing the reference count only once, when we should have done it twice, since we have already removed the block group from the block group cache rbtree. This happens because the reference count decrement for the rbtree reference happens after attempting to remove the free space tree entries, which is far away from the place where we remove the block group from the rbtree. To make things less error prone, decrement the reference count for the rbtree immediately after removing the block group from it. This also eleminates the need for two different exit labels on error, renaming 'out_put_label' to just 'out' and removing the old 'out'. Fixes: f6033c5e333238 ("btrfs: fix block group leak when removing fails") CC: stable@vger.kernel.org # 4.4+ Reviewed-by: Nikolay Borisov Reviewed-by: Anand Jain Signed-off-by: Filipe Manana Reviewed-by: David Sterba Signed-off-by: David Sterba Signed-off-by: Sasha Levin --- fs/btrfs/extent-tree.c | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/fs/btrfs/extent-tree.c b/fs/btrfs/extent-tree.c index a83f353e44188..c0033a0d00787 100644 --- a/fs/btrfs/extent-tree.c +++ b/fs/btrfs/extent-tree.c @@ -10645,7 +10645,7 @@ int btrfs_remove_block_group(struct btrfs_trans_handle *trans, path = btrfs_alloc_path(); if (!path) { ret = -ENOMEM; - goto out_put_group; + goto out; } /* @@ -10684,7 +10684,7 @@ int btrfs_remove_block_group(struct btrfs_trans_handle *trans, ret = btrfs_orphan_add(trans, inode); if (ret) { btrfs_add_delayed_iput(inode); - goto out_put_group; + goto out; } clear_nlink(inode); /* One for the block groups ref */ @@ -10707,13 +10707,13 @@ int btrfs_remove_block_group(struct btrfs_trans_handle *trans, ret = btrfs_search_slot(trans, tree_root, &key, path, -1, 1); if (ret < 0) - goto out_put_group; + goto out; if (ret > 0) btrfs_release_path(path); if (ret == 0) { ret = btrfs_del_item(trans, tree_root, path); if (ret) - goto out_put_group; + goto out; btrfs_release_path(path); } @@ -10722,6 +10722,9 @@ int btrfs_remove_block_group(struct btrfs_trans_handle *trans, &root->fs_info->block_group_cache_tree); RB_CLEAR_NODE(&block_group->cache_node); + /* Once for the block groups rbtree */ + btrfs_put_block_group(block_group); + if (root->fs_info->first_logical_byte == block_group->key.objectid) root->fs_info->first_logical_byte = (u64)-1; spin_unlock(&root->fs_info->block_group_cache_lock); @@ -10871,10 +10874,7 @@ int btrfs_remove_block_group(struct btrfs_trans_handle *trans, ret = remove_block_group_free_space(trans, root->fs_info, block_group); if (ret) - goto out_put_group; - - /* Once for the block groups rbtree */ - btrfs_put_block_group(block_group); + goto out; ret = btrfs_search_slot(trans, root, &key, path, -1, 1); if (ret > 0) @@ -10884,10 +10884,9 @@ int btrfs_remove_block_group(struct btrfs_trans_handle *trans, ret = btrfs_del_item(trans, root, path); -out_put_group: +out: /* Once for the lookup reference */ btrfs_put_block_group(block_group); -out: btrfs_free_path(path); return ret; } -- 2.25.1