From patchwork Wed Apr 26 17:13:00 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Filipe Manana X-Patchwork-Id: 13224789 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id DF9CBC77B78 for ; Wed, 26 Apr 2023 17:13:09 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234609AbjDZRNI (ORCPT ); Wed, 26 Apr 2023 13:13:08 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37974 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234594AbjDZRNH (ORCPT ); Wed, 26 Apr 2023 13:13:07 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 20E7E3A8C for ; Wed, 26 Apr 2023 10:13:06 -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 B0033619AC for ; Wed, 26 Apr 2023 17:13:05 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id A4478C433D2 for ; Wed, 26 Apr 2023 17:13:04 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1682529185; bh=jhnh5a5O6TALrGMdHSlrjYMX+EKfyylCWdlIXnKv4/0=; h=From:To:Subject:Date:In-Reply-To:References:From; b=PEXaahowU0FTDb5llb0OLO8iREk95mvCmbXwcb36FyXBSTxbwLXUa4excLezydQCJ 7VR3zFN3M/WWrfLuKduZpipuQw0da81fG8LpR2Sm77hduHeBuYh4ZORH1HTXjTuFVU Tws61m/Xbjg0rkOAEwwDoAbpjrirCOCNVIeH71HX8Ryy23dBhmMo5PfSzLaY5eXPpF 6EkTm5joqcKTTBpl5XXeqNvwa7evg/81eM/8tpPgoedRl/LfhsOmgBjIkkcymIdC+z m121+CtM0giI1/mrngkL/WwWyf754Fu+vZktU+X2nxfqhCStlnWVdg/VnplW9KYMsW RLjVQrfvm4FnQ== From: fdmanana@kernel.org To: linux-btrfs@vger.kernel.org Subject: [PATCH 1/2] btrfs: fix leak of source device allocation state after device replace Date: Wed, 26 Apr 2023 18:13:00 +0100 Message-Id: X-Mailer: git-send-email 2.25.1 In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-btrfs@vger.kernel.org From: Filipe Manana When a device replace finishes, the source device is freed by calling btrfs_free_device() at btrfs_rm_dev_replace_free_srcdev(), but the allocation state, tracked in the device's alloc_state io tree, is never freed. This is a regression recently introduced by commit f0bb5474cff0 ("btrfs: remove redundant release of btrfs_device::alloc_state"), which removed a call to extent_io_tree_release() from btrfs_free_device(), with the rationale that btrfs_close_one_device() already releases the allocation state from a device and btrfs_close_one_device() is always called before a device is freed with btrfs_free_device(). However that is not true for the device replace case, as btrfs_free_device() is called without any previous call to btrfs_close_one_device(). The issue is trivial to reproduce, for example, by running test btrfs/027 from fstests: $ ./check btrfs/027 $ rmmod btrfs $ dmesg (...) [84519.395485] BTRFS info (device sdc): dev_replace from (devid 2) to /dev/sdg started [84519.466224] BTRFS info (device sdc): dev_replace from (devid 2) to /dev/sdg finished [84519.552251] BTRFS info (device sdc): scrub: started on devid 1 [84519.552277] BTRFS info (device sdc): scrub: started on devid 2 [84519.552332] BTRFS info (device sdc): scrub: started on devid 3 [84519.552705] BTRFS info (device sdc): scrub: started on devid 4 [84519.604261] BTRFS info (device sdc): scrub: finished on devid 4 with status: 0 [84519.609374] BTRFS info (device sdc): scrub: finished on devid 3 with status: 0 [84519.610818] BTRFS info (device sdc): scrub: finished on devid 1 with status: 0 [84519.610927] BTRFS info (device sdc): scrub: finished on devid 2 with status: 0 [84559.503795] BTRFS: state leak: start 1048576 end 1351614463 state 1 in tree 1 refs 1 [84559.506764] BTRFS: state leak: start 1048576 end 1347420159 state 1 in tree 1 refs 1 [84559.510294] BTRFS: state leak: start 1048576 end 1351614463 state 1 in tree 1 refs 1 So fix this by adding back the call to extent_io_tree_release() at btrfs_free_device(). Fixes: f0bb5474cff0 ("btrfs: remove redundant release of btrfs_device::alloc_state") Signed-off-by: Filipe Manana Reviewed-by: Qu Wenruo --- fs/btrfs/volumes.c | 1 + 1 file changed, 1 insertion(+) diff --git a/fs/btrfs/volumes.c b/fs/btrfs/volumes.c index 03f52e4a20aa..841e799dece5 100644 --- a/fs/btrfs/volumes.c +++ b/fs/btrfs/volumes.c @@ -395,6 +395,7 @@ void btrfs_free_device(struct btrfs_device *device) { WARN_ON(!list_empty(&device->post_commit_list)); rcu_string_free(device->name); + extent_io_tree_release(&device->alloc_state); btrfs_destroy_dev_zone_info(device); kfree(device); } From patchwork Wed Apr 26 17:13:01 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Filipe Manana X-Patchwork-Id: 13224790 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id F2591C7EE20 for ; Wed, 26 Apr 2023 17:13:09 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234622AbjDZRNI (ORCPT ); Wed, 26 Apr 2023 13:13:08 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37980 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234608AbjDZRNH (ORCPT ); Wed, 26 Apr 2023 13:13:07 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 066363A8E for ; Wed, 26 Apr 2023 10:13:07 -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 9327061B36 for ; Wed, 26 Apr 2023 17:13:06 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 8773CC4339B for ; Wed, 26 Apr 2023 17:13:05 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1682529186; bh=8nKX/PpOQ/QCMxB2J5FxrOINvphypg6PtXQYv7EQuuw=; h=From:To:Subject:Date:In-Reply-To:References:From; b=H6JwRFE/hOfqigWosrt0Jh/JUBBTVODYFTHwcO0/OXBSshrIkMVGXOc+YOglp/6mW snIhZtiUHYPxNIUt3amertQimJZd3wBIXqUfJ2hcD48wlK7Lpn39wj70G7Ong2NgXz cg1CKJ5T1kZtgugi+ztDqJU6UsD3Ye9Gy58Y8S42ZANnIrR4DtAvRK9Z635tL1xhRk Wl3JjUkTs+I9fk3Mqqh4Sf+zG01/u1H413SCxp8p1UpkqSncIkM8YN6++eZE+G/rcH McF7YOL9wnMCmUoUtQmaIRXEorXnq4Os6yBDgN4ybEuLdHa5VOFCwJwqniyS9iQXGp wSA0ukPo5BTug== From: fdmanana@kernel.org To: linux-btrfs@vger.kernel.org Subject: [PATCH 2/2] btrfs: make btrfs_free_device() static Date: Wed, 26 Apr 2023 18:13:01 +0100 Message-Id: <9182a6f15c0d7ea2395e9c9588eb8fa31d4525f9.1682528751.git.fdmanana@suse.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-btrfs@vger.kernel.org From: Filipe Manana The function btrfs_free_device() is never used outside of volumes.c, so make it static and remove its prototype declaration at volumes.h. Signed-off-by: Filipe Manana Reviewed-by: Qu Wenruo Reviewed-by: Anand Jain --- fs/btrfs/volumes.c | 2 +- fs/btrfs/volumes.h | 1 - 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/fs/btrfs/volumes.c b/fs/btrfs/volumes.c index 841e799dece5..1a7620680f50 100644 --- a/fs/btrfs/volumes.c +++ b/fs/btrfs/volumes.c @@ -391,7 +391,7 @@ static struct btrfs_fs_devices *alloc_fs_devices(const u8 *fsid, return fs_devs; } -void btrfs_free_device(struct btrfs_device *device) +static void btrfs_free_device(struct btrfs_device *device) { WARN_ON(!list_empty(&device->post_commit_list)); rcu_string_free(device->name); diff --git a/fs/btrfs/volumes.h b/fs/btrfs/volumes.h index bf47a1a70813..5cbbee32748c 100644 --- a/fs/btrfs/volumes.h +++ b/fs/btrfs/volumes.h @@ -617,7 +617,6 @@ struct btrfs_device *btrfs_alloc_device(struct btrfs_fs_info *fs_info, const u64 *devid, const u8 *uuid, const char *path); void btrfs_put_dev_args_from_path(struct btrfs_dev_lookup_args *args); -void btrfs_free_device(struct btrfs_device *device); int btrfs_rm_device(struct btrfs_fs_info *fs_info, struct btrfs_dev_lookup_args *args, struct block_device **bdev, fmode_t *mode);