Received: by 2002:a05:6a10:6d10:0:0:0:0 with SMTP id gq16csp3248664pxb; Mon, 18 Apr 2022 20:34:26 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyAy83EyF7p0S4/6PKNpFkQk7nA3LM7eFoylwBMU7WqDHtBIpYeWq8Nwndc8BShQeRzNz9H X-Received: by 2002:a17:902:8ec9:b0:14f:11f7:db77 with SMTP id x9-20020a1709028ec900b0014f11f7db77mr13878936plo.136.1650339266128; Mon, 18 Apr 2022 20:34:26 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1650339266; cv=none; d=google.com; s=arc-20160816; b=kmKnhWkUt2H3RnOQiKuTeYI1w5SNVGQtEKFCD+K4ooM62dC97xaQVwX3SWttGYN+ej lJfjJanNllEMvnH/AgkFyYzmKs1flIJdrpu25JwBGPJfhcDahgPoHb5Qc1pHAynontMl wUyYoLhLwsq+4um1jxhGowV2dHSgLd8eZZwllBvNdG4CPowqF+r3BaX5+jdNdNMPQ+T2 V7/ULvCXxBDvy9RvgQe9bLVV+iVOuPSAL8mPcEmFEhIyb/aeUEPU7dmrkmAaUsFC445y JZV4Wl9n0A4bueq2o88b7agE9PmjzNmWNJOMKoHLRxtXkrAEwP80EsBbZV4wCZ9Iderd d0Ag== 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=bH8Ma/+h/XsE9QZh6wP6k0c5UvqmhCJB08jQwbXNBxg=; b=Mb/h6/PiccR2/Z4Tfzw3Q1dbaFNl6djyf27PlfwXnhUNGUBiqRNMRgeeVkNkpuzGAL F0Dn4gQ2fimC+79ZEO2shshNqCfKgelrEOzYsFTG+Nbf25R0ci35PbC04ek9zdFvRBSA swU+siFOO6WghzvXD4pUayremev0RzhGSTgsNq+wNYmMx1RU70EqYck9PeTETtTYkazE Uxg0jlr/m5NsJTHWdRbu4wPbyfNxpqC34c68a0VI5eqq9YQ+OYtaBL0OFd0P1tRnUSkR lTRWu5FpoW5/X3XyEI7d62byKU4hr9g/szbx/rsuW7FL/vSJcf4fecRwJvHXG9lt5nVy yaIA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=2lSsysgH; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 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 out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id mw7-20020a17090b4d0700b001d2a904dad6si1334962pjb.19.2022.04.18.20.34.08; Mon, 18 Apr 2022 20:34:26 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=2lSsysgH; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 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 S239551AbiDRMki (ORCPT + 99 others); Mon, 18 Apr 2022 08:40:38 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54024 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S239451AbiDRMdD (ORCPT ); Mon, 18 Apr 2022 08:33:03 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 9378326AEE; Mon, 18 Apr 2022 05:24:38 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id A105560FB0; Mon, 18 Apr 2022 12:24:37 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id B1900C385A1; Mon, 18 Apr 2022 12:24:36 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1650284677; bh=41f3L+KWlQpUhp8T1RZqJOl6hTJ05WVA7/JAMZdnwWA=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=2lSsysgHCvdDSSGp2xDmUkzPxsC4ONmWZHx6xs4kSs6PpRo/iyI6/uh5oyIv41u2c IkXnT5+IgtRUSaNYPgW+lsPxFUDuPgmZS4aImZnz9YMR5DEM9OIVeCUNZBdSTVfbwr XEi8DhmxbtQzkoBCmelWbc8NvI2rXUS+N6R7+wOw= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, TOTE Robot , Jia-Ju Bai , David Sterba Subject: [PATCH 5.17 192/219] btrfs: fix root ref counts in error handling in btrfs_get_root_ref Date: Mon, 18 Apr 2022 14:12:41 +0200 Message-Id: <20220418121212.249736255@linuxfoundation.org> X-Mailer: git-send-email 2.35.3 In-Reply-To: <20220418121203.462784814@linuxfoundation.org> References: <20220418121203.462784814@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-7.7 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_HI, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Jia-Ju Bai commit 168a2f776b9762f4021421008512dd7ab7474df1 upstream. In btrfs_get_root_ref(), when btrfs_insert_fs_root() fails, btrfs_put_root() can happen for two reasons: - the root already exists in the tree, in that case it returns the reference obtained in btrfs_lookup_fs_root() - another error so the cleanup is done in the fail label Calling btrfs_put_root() unconditionally would lead to double decrement of the root reference possibly freeing it in the second case. Reported-by: TOTE Robot Fixes: bc44d7c4b2b1 ("btrfs: push btrfs_grab_fs_root into btrfs_get_fs_root") CC: stable@vger.kernel.org # 5.10+ Signed-off-by: Jia-Ju Bai Reviewed-by: David Sterba Signed-off-by: David Sterba Signed-off-by: Greg Kroah-Hartman --- fs/btrfs/disk-io.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) --- a/fs/btrfs/disk-io.c +++ b/fs/btrfs/disk-io.c @@ -1826,9 +1826,10 @@ again: ret = btrfs_insert_fs_root(fs_info, root); if (ret) { - btrfs_put_root(root); - if (ret == -EEXIST) + if (ret == -EEXIST) { + btrfs_put_root(root); goto again; + } goto fail; } return root;