From patchwork Thu May 4 11:04:18 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Filipe Manana X-Patchwork-Id: 13231044 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 801C1C7EE23 for ; Thu, 4 May 2023 11:04:47 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230119AbjEDLEq (ORCPT ); Thu, 4 May 2023 07:04:46 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39322 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230167AbjEDLEg (ORCPT ); Thu, 4 May 2023 07:04:36 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 854ED19B7 for ; Thu, 4 May 2023 04:04:33 -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 2252B6176A for ; Thu, 4 May 2023 11:04:33 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 188C3C4339B for ; Thu, 4 May 2023 11:04:31 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1683198272; bh=l638eeBXZrWgfTqHYaOXhZ5Vsvyl6FKBGbB66dRgT2o=; h=From:To:Subject:Date:In-Reply-To:References:From; b=ecDTMV1G42zux+1y50Wa+9eFL9BRWwy5JnWH/sSsApzCgrQIfREClPknb2TQ1Hap7 S8qaeltpXWc4KCzEL06C85z2MtNM3uWxAWTz/QPVughKsdhH4EVyOw75lf0AtxcSAh h9p4m9y+kkJ8fzWLYLZJ3lY7t6bbDdkfwMSs+kadcFOFanQfWVB3ycMKDCGQfu6fvF Qfduoc7HU8lsrK6KZypMVW7iRtvJ/jPnnsXkiruTVXRQZULBrrGS7zfPyvwBNLbrbk b4NiCr4xju593bWWdRwmaqhrJZWyUht92ebBs+buClbYwrokY/r6Ie6jTozkgoR5Xr smxW4x2iaW3dA== From: fdmanana@kernel.org To: linux-btrfs@vger.kernel.org Subject: [PATCH 1/9] btrfs: fix space cache inconsistency after error loading it from disk Date: Thu, 4 May 2023 12:04:18 +0100 Message-Id: <4f6a98cfcca190f03b9a729d7cf9c191c29032da.1683196407.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 When loading a free space cache from disk, at __load_free_space_cache(), if we fail to insert a bitmap entry, we still increment the number of total bitmaps in the btrfs_free_space_ctl structure, which is incorrect since we failed to add the bitmap entry. On error we then empty the cache by calling __btrfs_remove_free_space_cache(), which will result in getting the total bitmaps counter set to 1. A failure to load a free space cache is not critical, so if a failure happens we just rebuild the cache by scanning the extent tree, which happens at block-group.c:caching_thread(). Yet the failure will result in having the total bitmaps of the btrfs_free_space_ctl always bigger by 1 then the number of bitmap entries we have. So fix this by having the total bitmaps counter be incremented only if we successfully added the bitmap entry. Fixes: a67509c30079 ("Btrfs: add a io_ctl struct and helpers for dealing with the space cache") Signed-off-by: Filipe Manana Reviewed-by: Anand Jain --- fs/btrfs/free-space-cache.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/fs/btrfs/free-space-cache.c b/fs/btrfs/free-space-cache.c index d84cef89cdff..cf98a3c05480 100644 --- a/fs/btrfs/free-space-cache.c +++ b/fs/btrfs/free-space-cache.c @@ -870,15 +870,16 @@ static int __load_free_space_cache(struct btrfs_root *root, struct inode *inode, } spin_lock(&ctl->tree_lock); ret = link_free_space(ctl, e); - ctl->total_bitmaps++; - recalculate_thresholds(ctl); - spin_unlock(&ctl->tree_lock); if (ret) { + spin_unlock(&ctl->tree_lock); btrfs_err(fs_info, "Duplicate entries in free space cache, dumping"); kmem_cache_free(btrfs_free_space_cachep, e); goto free_cache; } + ctl->total_bitmaps++; + recalculate_thresholds(ctl); + spin_unlock(&ctl->tree_lock); list_add_tail(&e->list, &bitmaps); } From patchwork Thu May 4 11:04:19 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Filipe Manana X-Patchwork-Id: 13231042 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 8570EC77B78 for ; Thu, 4 May 2023 11:04:46 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230056AbjEDLEp (ORCPT ); Thu, 4 May 2023 07:04:45 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39380 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229545AbjEDLEg (ORCPT ); Thu, 4 May 2023 07:04:36 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 701E2212B for ; Thu, 4 May 2023 04:04:34 -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 0C3016176C for ; Thu, 4 May 2023 11:04:34 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id F3F6AC433D2 for ; Thu, 4 May 2023 11:04:32 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1683198273; bh=8GYL/l6+msrX/iONuyy4y/X13M4oVcbKSlSZPhhWmL8=; h=From:To:Subject:Date:In-Reply-To:References:From; b=hA88ebBIA8UYhTTaFbC4xLXEknlXYFvesISajh8zv478TzCHjDce7XiLFoxSmh5G6 TmUtNpf1czRiSWn9nLKULVUykf7aFP0IzaUOq2fbS8q+yFJoifuTWj+xF/myZhk0ES s/DMx6nk/7QOqCUqKt6TV04b+AL8Pd8tcx/4ffjHcOml4TBdkCAzf3XBVfS+TPbAge Tby3PWXoMuNVUrV6e9iY6nkur3zf7jAKplD4LgbHnS+qwP940WqGMxzV3eC8tMKnv/ YEa17wwNd9IlfIblMqE8gQkU/cwJOJmRA/br4+vmysVaaDd/EQvcMnqW6V94Ty0BGk JxoN0G/UTKfNw== From: fdmanana@kernel.org To: linux-btrfs@vger.kernel.org Subject: [PATCH 2/9] btrfs: avoid extra memory allocation when copying free space cache Date: Thu, 4 May 2023 12:04:19 +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 At copy_free_space_cache(), we add a new entry to the block group's ctl before we free the entry from the temporary ctl. Adding a new entry requires the allocation of a new struct btrfs_free_space, so we can avoid a temporary extra allocation by freeing the entry from the temporary ctl before we add a new entry to the main ctl, which possibly also reduces the chances for a memory allocation failure in case of very high memory pressure. So just do that. Signed-off-by: Filipe Manana Reviewed-by: Anand Jain --- fs/btrfs/free-space-cache.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/fs/btrfs/free-space-cache.c b/fs/btrfs/free-space-cache.c index cf98a3c05480..ec53119d4cfb 100644 --- a/fs/btrfs/free-space-cache.c +++ b/fs/btrfs/free-space-cache.c @@ -923,10 +923,12 @@ static int copy_free_space_cache(struct btrfs_block_group *block_group, while (!ret && (n = rb_first(&ctl->free_space_offset)) != NULL) { info = rb_entry(n, struct btrfs_free_space, offset_index); if (!info->bitmap) { + const u64 offset = info->offset; + const u64 bytes = info->bytes; + unlink_free_space(ctl, info, true); - ret = btrfs_add_free_space(block_group, info->offset, - info->bytes); kmem_cache_free(btrfs_free_space_cachep, info); + ret = btrfs_add_free_space(block_group, offset, bytes); } else { u64 offset = info->offset; u64 bytes = ctl->unit; From patchwork Thu May 4 11:04:20 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Filipe Manana X-Patchwork-Id: 13231046 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 71888C7EE21 for ; Thu, 4 May 2023 11:04:54 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230151AbjEDLEs (ORCPT ); Thu, 4 May 2023 07:04:48 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39328 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230038AbjEDLEi (ORCPT ); Thu, 4 May 2023 07:04:38 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 567D326A4 for ; Thu, 4 May 2023 04:04:35 -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 E6B3763342 for ; Thu, 4 May 2023 11:04:34 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id D91F6C433EF for ; Thu, 4 May 2023 11:04:33 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1683198274; bh=Gw95DJFsrVxwyHMU0ULENXVu9dUtOt4ql6OqP/Ryo1M=; h=From:To:Subject:Date:In-Reply-To:References:From; b=O8ePwTxrxozK+PROvZpO9iKayUlJqAOHtilnshuE5ABWgtnadiqkIcx8gGk3f5Jyy 2N3/7zjczrcsx+ZCKCAXKbbZET7cP4rCc6YZwGtHXSh1cborSscy5M7xuGGeEbH4R3 SgHIW139EGcBUnDo0WFOBA1RJUE2Ll60DtJHOSq6YRmV7TAG5rh/iRTF/Hn8tBIObU BExi5Ac6Z+NYBUMpK1DlmaPo8sdy6fK1YIAxZqcT7/Sp3bBmtiDmcCWEgfQEYn2A+v PdPl7nXBRjPtK31maIKwRYsGV4/eEcfcHLO9F1Gou4O5ZKbVcJicKaVKQDLSUuzNUi fpge/5Q+V2a2A== From: fdmanana@kernel.org To: linux-btrfs@vger.kernel.org Subject: [PATCH 3/9] btrfs: avoid searching twice for previous node when merging free space entries Date: Thu, 4 May 2023 12:04:20 +0100 Message-Id: <93c8890e17eda56eec2c0b5ed7358dc85df5f5d4.1683196407.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 At try_merge_free_space(), avoid calling twice rb_prev() to find the previous node, as that requires looping through the red black tree, so store the result of the rb_prev() call and then use it. Signed-off-by: Filipe Manana --- fs/btrfs/free-space-cache.c | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/fs/btrfs/free-space-cache.c b/fs/btrfs/free-space-cache.c index ec53119d4cfb..7d842d356d9f 100644 --- a/fs/btrfs/free-space-cache.c +++ b/fs/btrfs/free-space-cache.c @@ -2449,6 +2449,7 @@ static bool try_merge_free_space(struct btrfs_free_space_ctl *ctl, u64 offset = info->offset; u64 bytes = info->bytes; const bool is_trimmed = btrfs_free_space_trimmed(info); + struct rb_node *right_prev = NULL; /* * first we want to see if there is free space adjacent to the range we @@ -2456,9 +2457,12 @@ static bool try_merge_free_space(struct btrfs_free_space_ctl *ctl, * cover the entire range */ right_info = tree_search_offset(ctl, offset + bytes, 0, 0); - if (right_info && rb_prev(&right_info->offset_index)) - left_info = rb_entry(rb_prev(&right_info->offset_index), - struct btrfs_free_space, offset_index); + if (right_info) + right_prev = rb_prev(&right_info->offset_index); + + if (right_prev) + left_info = rb_entry(right_prev, struct btrfs_free_space, + offset_index); else if (!right_info) left_info = tree_search_offset(ctl, offset - 1, 0, 0); From patchwork Thu May 4 11:04:21 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Filipe Manana X-Patchwork-Id: 13231043 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 6E171C7EE26 for ; Thu, 4 May 2023 11:04:48 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230124AbjEDLEr (ORCPT ); Thu, 4 May 2023 07:04:47 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39398 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230169AbjEDLEi (ORCPT ); Thu, 4 May 2023 07:04:38 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 3EDE54EF1 for ; Thu, 4 May 2023 04:04:36 -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 CE131617BE for ; Thu, 4 May 2023 11:04:35 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id C03D3C4339B for ; Thu, 4 May 2023 11:04:34 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1683198275; bh=uzLa4oy/9aDF2XBfpYIetgYL3KUdJwxmMH5y8gabBx4=; h=From:To:Subject:Date:In-Reply-To:References:From; b=iDNbJKXUky9zwrhQLN9QCH/ygd4UA9OqaU004TraqLqVV3KAKxHqruYPh5je0YZ8U NXAChSLuPTUo73Ydzlf8vAnme3mtewAR/5f8d7KFD8w+b/WhYd+TFa0Hct43uRRhEE bAv2qnwOG6H6m8O0u+fYxYZ8EdVULSQXPuTrgNrW36y6hwnr4BUmgf44w1AcgEjymZ 3P/KXeTGIOgB1HJehtCU0wW27HMfGASfk/zEI6HcB45XfiRVR1Kf2LAe7x64RPdM2i 9I3N194pj0OZVslAZ0wL7GcVZORj+1s3HKZaQzmHAOGutzZR0naJkQ9+RtC5I9Quay 8P2FCFy7I499w== From: fdmanana@kernel.org To: linux-btrfs@vger.kernel.org Subject: [PATCH 4/9] btrfs: use precomputed end offsets at do_trimming() Date: Thu, 4 May 2023 12:04:21 +0100 Message-Id: <0a9ca970dbc9d8f3109c0ec25cae3227f4cc7e4b.1683196407.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 are two computations of end offsets at do_trimming() that are not necessary, as they were previously computed and stored in local const variables. So just use the variables instead, to make the source code shorter and easier to read. Signed-off-by: Filipe Manana Reviewed-by: Anand Jain --- fs/btrfs/free-space-cache.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/fs/btrfs/free-space-cache.c b/fs/btrfs/free-space-cache.c index 7d842d356d9f..b16ed01c76ff 100644 --- a/fs/btrfs/free-space-cache.c +++ b/fs/btrfs/free-space-cache.c @@ -3677,7 +3677,7 @@ static int do_trimming(struct btrfs_block_group *block_group, __btrfs_add_free_space(block_group, reserved_start, start - reserved_start, reserved_trim_state); - if (start + bytes < reserved_start + reserved_bytes) + if (end < reserved_end) __btrfs_add_free_space(block_group, end, reserved_end - end, reserved_trim_state); __btrfs_add_free_space(block_group, start, bytes, trim_state); From patchwork Thu May 4 11:04:22 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Filipe Manana X-Patchwork-Id: 13231045 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 11EFBC7EE23 for ; Thu, 4 May 2023 11:04:55 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230218AbjEDLEu (ORCPT ); Thu, 4 May 2023 07:04:50 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39408 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230177AbjEDLEk (ORCPT ); Thu, 4 May 2023 07:04:40 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [IPv6:2604:1380:4641:c500::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 2FFCCE42 for ; Thu, 4 May 2023 04:04:37 -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 B4E5C6176A for ; Thu, 4 May 2023 11:04:36 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id A7D7EC433D2 for ; Thu, 4 May 2023 11:04:35 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1683198276; bh=M3A+gurSB3Smm/xbx/c6zhm92Wbfh15vd00c/87XQu0=; h=From:To:Subject:Date:In-Reply-To:References:From; b=A9BQWuTpxUmwTxWbdesYFayUZEyTG5nP0zbXDyDkFAXp3a4Fs5QSVj/HlMsb+IsyO 5CDEvGMeW3i/f44olUzs8qmGmbssdz8WJu/D701bR3ekrl+/6RHisFTEO4iY4SYjHP 8niAhobviV6t1licHF0GP9THps3xbdtuzUzwkFjcGeTgqayZYlDNCs2IUhkGH7iN// 1KKd9Un+85wshROo+BdO1+B/DwtX6nedyaqr9wPeyGtHIBnZva4Go8UZhWBoRcuDuJ uRZ/62S7OHqelqGNMzEguzKW6OfLqhaC+pWpFGFSFBkVOyyXkZHIYYw+fU1aj1zshh LacQq42ug2GEg== From: fdmanana@kernel.org To: linux-btrfs@vger.kernel.org Subject: [PATCH 5/9] btrfs: simplify arguments to tree_insert_offset() Date: Thu, 4 May 2023 12:04:22 +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 For the in memory component of space caching (free space cache and free space tree), three of the arguments passed to tree_insert_offset() can always be taken from the new free space entry that we are about to add. So simplify tree_insert_offset() to take the new entry instead of the 'offset', 'node' and 'bitmap' arguments. This will also allow to make further changes simpler. Signed-off-by: Filipe Manana --- fs/btrfs/free-space-cache.c | 37 ++++++++++++++++--------------------- 1 file changed, 16 insertions(+), 21 deletions(-) diff --git a/fs/btrfs/free-space-cache.c b/fs/btrfs/free-space-cache.c index b16ed01c76ff..4cdc7d857158 100644 --- a/fs/btrfs/free-space-cache.c +++ b/fs/btrfs/free-space-cache.c @@ -1598,20 +1598,21 @@ static inline u64 offset_to_bitmap(struct btrfs_free_space_ctl *ctl, return bitmap_start; } -static int tree_insert_offset(struct rb_root *root, u64 offset, - struct rb_node *node, int bitmap) +static int tree_insert_offset(struct rb_root *root, + struct btrfs_free_space *new_entry) { struct rb_node **p = &root->rb_node; struct rb_node *parent = NULL; - struct btrfs_free_space *info; while (*p) { + struct btrfs_free_space *info; + parent = *p; info = rb_entry(parent, struct btrfs_free_space, offset_index); - if (offset < info->offset) { + if (new_entry->offset < info->offset) { p = &(*p)->rb_left; - } else if (offset > info->offset) { + } else if (new_entry->offset > info->offset) { p = &(*p)->rb_right; } else { /* @@ -1627,7 +1628,7 @@ static int tree_insert_offset(struct rb_root *root, u64 offset, * found a bitmap, we want to go left, or before * logically. */ - if (bitmap) { + if (new_entry->bitmap) { if (info->bitmap) { WARN_ON_ONCE(1); return -EEXIST; @@ -1643,8 +1644,8 @@ static int tree_insert_offset(struct rb_root *root, u64 offset, } } - rb_link_node(node, parent, p); - rb_insert_color(node, root); + rb_link_node(&new_entry->offset_index, parent, p); + rb_insert_color(&new_entry->offset_index, root); return 0; } @@ -1835,8 +1836,7 @@ static int link_free_space(struct btrfs_free_space_ctl *ctl, int ret = 0; ASSERT(info->bytes || info->bitmap); - ret = tree_insert_offset(&ctl->free_space_offset, info->offset, - &info->offset_index, (info->bitmap != NULL)); + ret = tree_insert_offset(&ctl->free_space_offset, info); if (ret) return ret; @@ -2974,8 +2974,6 @@ static void __btrfs_return_cluster_to_free_space( struct btrfs_block_group *block_group, struct btrfs_free_cluster *cluster) { - struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl; - struct btrfs_free_space *entry; struct rb_node *node; spin_lock(&cluster->lock); @@ -2990,15 +2988,15 @@ static void __btrfs_return_cluster_to_free_space( node = rb_first(&cluster->root); while (node) { - bool bitmap; + struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl; + struct btrfs_free_space *entry; entry = rb_entry(node, struct btrfs_free_space, offset_index); node = rb_next(&entry->offset_index); rb_erase(&entry->offset_index, &cluster->root); RB_CLEAR_NODE(&entry->offset_index); - bitmap = (entry->bitmap != NULL); - if (!bitmap) { + if (!entry->bitmap) { /* Merging treats extents as if they were new */ if (!btrfs_free_space_trimmed(entry)) { ctl->discardable_extents[BTRFS_STAT_CURR]--; @@ -3016,8 +3014,7 @@ static void __btrfs_return_cluster_to_free_space( entry->bytes; } } - tree_insert_offset(&ctl->free_space_offset, - entry->offset, &entry->offset_index, bitmap); + tree_insert_offset(&ctl->free_space_offset, entry); rb_add_cached(&entry->bytes_index, &ctl->free_space_bytes, entry_less); } @@ -3391,8 +3388,7 @@ static int btrfs_bitmap_cluster(struct btrfs_block_group *block_group, */ RB_CLEAR_NODE(&entry->bytes_index); - ret = tree_insert_offset(&cluster->root, entry->offset, - &entry->offset_index, 1); + ret = tree_insert_offset(&cluster->root, entry); ASSERT(!ret); /* -EEXIST; Logic error */ trace_btrfs_setup_cluster(block_group, cluster, @@ -3482,8 +3478,7 @@ setup_cluster_no_bitmap(struct btrfs_block_group *block_group, rb_erase(&entry->offset_index, &ctl->free_space_offset); rb_erase_cached(&entry->bytes_index, &ctl->free_space_bytes); - ret = tree_insert_offset(&cluster->root, entry->offset, - &entry->offset_index, 0); + ret = tree_insert_offset(&cluster->root, entry); total_size += entry->bytes; ASSERT(!ret); /* -EEXIST; Logic error */ } while (node && entry != last); From patchwork Thu May 4 11:04:23 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Filipe Manana X-Patchwork-Id: 13231048 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 E6B09C77B78 for ; Thu, 4 May 2023 11:04:54 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230167AbjEDLEt (ORCPT ); Thu, 4 May 2023 07:04:49 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39310 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230183AbjEDLEl (ORCPT ); Thu, 4 May 2023 07:04:41 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [IPv6:2604:1380:4641:c500::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 174973C3D for ; Thu, 4 May 2023 04:04: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 9FA866176C for ; Thu, 4 May 2023 11:04:37 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 8EBFBC433EF for ; Thu, 4 May 2023 11:04:36 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1683198277; bh=uMU/BpmrKjgJLDqlj9eTIHQvSNnLweR2Z1WdEbGiuUQ=; h=From:To:Subject:Date:In-Reply-To:References:From; b=EPmypvwWhkUalek52TnAP7fENk0IrO34NBg6Y9FOujQK/wKdwkzgWYp32IlJbkGEz wAOnkJNVqZgKiM0fSH59j9cSyG9gCNb9fduj2KAcoup2XH+qRpvkAdjx2UJOdCQYMr 0nAgnA4sDyo8QLtH4T/gjrjYXMIftG8hFxZiS+tE8GNzjVPby1KEkLHbEzzCR/5JmY QQR3S7Mi3/0BV08oHGu9UPlhKUuo+5SPU5FLwIJ7lWT3++jR5B6yB2RD+8LuJgAv42 dsqEGR41hUjR++4gDedN3nlp9pxydYE/b+XND2zxJ5RhQ3s+eu9GliPMnk0WVz0bz1 lTzLZb8ILAbUA== From: fdmanana@kernel.org To: linux-btrfs@vger.kernel.org Subject: [PATCH 6/9] btrfs: assert proper locks are held at tree_insert_offset() Date: Thu, 4 May 2023 12:04:23 +0100 Message-Id: <19d432471a2a0c856fc1fc64a3ae683f23444148.1683196407.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 There are multiple code paths leading to tree_insert_offset(), and each path takes the necessary locks before tree_insert_offset() is called, since they do other things that require those locks to be held. This makes it easy to miss the locking somewhere, so make tree_insert_offset() assert that the required locks are being held by the calling task. Signed-off-by: Filipe Manana --- fs/btrfs/free-space-cache.c | 25 +++++++++++++++++++------ 1 file changed, 19 insertions(+), 6 deletions(-) diff --git a/fs/btrfs/free-space-cache.c b/fs/btrfs/free-space-cache.c index 4cdc7d857158..31d9bb958dc7 100644 --- a/fs/btrfs/free-space-cache.c +++ b/fs/btrfs/free-space-cache.c @@ -1598,12 +1598,25 @@ static inline u64 offset_to_bitmap(struct btrfs_free_space_ctl *ctl, return bitmap_start; } -static int tree_insert_offset(struct rb_root *root, +static int tree_insert_offset(struct btrfs_free_space_ctl *ctl, + struct btrfs_free_cluster *cluster, struct btrfs_free_space *new_entry) { - struct rb_node **p = &root->rb_node; + struct rb_root *root; + struct rb_node **p; struct rb_node *parent = NULL; + lockdep_assert_held(&ctl->tree_lock); + + if (cluster) { + lockdep_assert_held(&cluster->lock); + root = &cluster->root; + } else { + root = &ctl->free_space_offset; + } + + p = &root->rb_node; + while (*p) { struct btrfs_free_space *info; @@ -1836,7 +1849,7 @@ static int link_free_space(struct btrfs_free_space_ctl *ctl, int ret = 0; ASSERT(info->bytes || info->bitmap); - ret = tree_insert_offset(&ctl->free_space_offset, info); + ret = tree_insert_offset(ctl, NULL, info); if (ret) return ret; @@ -3014,7 +3027,7 @@ static void __btrfs_return_cluster_to_free_space( entry->bytes; } } - tree_insert_offset(&ctl->free_space_offset, entry); + tree_insert_offset(ctl, NULL, entry); rb_add_cached(&entry->bytes_index, &ctl->free_space_bytes, entry_less); } @@ -3388,7 +3401,7 @@ static int btrfs_bitmap_cluster(struct btrfs_block_group *block_group, */ RB_CLEAR_NODE(&entry->bytes_index); - ret = tree_insert_offset(&cluster->root, entry); + ret = tree_insert_offset(ctl, cluster, entry); ASSERT(!ret); /* -EEXIST; Logic error */ trace_btrfs_setup_cluster(block_group, cluster, @@ -3478,7 +3491,7 @@ setup_cluster_no_bitmap(struct btrfs_block_group *block_group, rb_erase(&entry->offset_index, &ctl->free_space_offset); rb_erase_cached(&entry->bytes_index, &ctl->free_space_bytes); - ret = tree_insert_offset(&cluster->root, entry); + ret = tree_insert_offset(ctl, cluster, entry); total_size += entry->bytes; ASSERT(!ret); /* -EEXIST; Logic error */ } while (node && entry != last); From patchwork Thu May 4 11:04:24 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Filipe Manana X-Patchwork-Id: 13231047 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 3B732C7EE26 for ; Thu, 4 May 2023 11:04:57 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230162AbjEDLEz (ORCPT ); Thu, 4 May 2023 07:04:55 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39416 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230194AbjEDLEm (ORCPT ); Thu, 4 May 2023 07:04:42 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [IPv6:2604:1380:4641:c500::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A56CF5256 for ; Thu, 4 May 2023 04:04: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 854596176A for ; Thu, 4 May 2023 11:04:38 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 76168C4339B for ; Thu, 4 May 2023 11:04:37 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1683198277; bh=Lb8pmXoZIjPQpjaSC8eI7nEo1yoCF67sEhq8bsNHye8=; h=From:To:Subject:Date:In-Reply-To:References:From; b=tofoWcJhE35IKVbtZN7SqkqGSOFGWHepFmQ+H4+7TPNutDVsmzDl6c4gzQEkWameH aXf2BIHoZYW2VbBba2nIJZcp8mdcsw80WIMljpO3ZO+s18stHT1mp7S7/TLL0aUuv0 R6suAK1M34mVZVvE425d8kX4iw75Uqd1mQHiYreBw4KGz8+/c7EYhYIYLkHs0TIWeK +lWwXQN4lPQ/DhtbS2nupwy/8rXxoqpQxyJBRMbp65uZ5xuLGAaVKM6ilBXcjKbVoi vPqRHMPg5VRLh81B713uogGe8tcbksJIFmpdp2c7G7vfWH3FNcbEseJGVs5AagrvGW KvwbEWZOyJYkA== From: fdmanana@kernel.org To: linux-btrfs@vger.kernel.org Subject: [PATCH 7/9] btrfs: assert tree lock is held when searching for free space entries Date: Thu, 4 May 2023 12:04:24 +0100 Message-Id: <524c29c4f899918f9597a6ea6af1e86d99117bc7.1683196407.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 When searching for a free space entry by offset, at tree_search_offset(), we are supposed to have the btrfs_free_space_ctl's 'tree_lock' held, so assert that. We have multiple callers of tree_search_offset(), and all currently hold the necessary lock before calling it. Signed-off-by: Filipe Manana Reviewed-by: Anand Jain --- fs/btrfs/free-space-cache.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/fs/btrfs/free-space-cache.c b/fs/btrfs/free-space-cache.c index 31d9bb958dc7..84e09ac50103 100644 --- a/fs/btrfs/free-space-cache.c +++ b/fs/btrfs/free-space-cache.c @@ -1721,6 +1721,8 @@ tree_search_offset(struct btrfs_free_space_ctl *ctl, struct rb_node *n = ctl->free_space_offset.rb_node; struct btrfs_free_space *entry = NULL, *prev = NULL; + lockdep_assert_held(&ctl->tree_lock); + /* find entry that is closest to the 'offset' */ while (n) { entry = rb_entry(n, struct btrfs_free_space, offset_index); From patchwork Thu May 4 11:04:25 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Filipe Manana X-Patchwork-Id: 13231049 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 3E007C7EE23 for ; Thu, 4 May 2023 11:04:58 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230183AbjEDLE4 (ORCPT ); Thu, 4 May 2023 07:04:56 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39314 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229826AbjEDLEm (ORCPT ); Thu, 4 May 2023 07:04:42 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [IPv6:2604:1380:4641:c500::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 95BE54EDA for ; Thu, 4 May 2023 04:04:39 -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 667BA6129B for ; Thu, 4 May 2023 11:04:39 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 5CEBCC433EF for ; Thu, 4 May 2023 11:04:38 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1683198278; bh=6LvE9M4jg1PtpyKkneV7Kv51VKE0GShkQFksh1Q94yU=; h=From:To:Subject:Date:In-Reply-To:References:From; b=id+v0gdAOcC8OJ9EA5fkRkgTlGzkUxSjCPIl0mYdp0o73hqOtILTqAGAdMokLe3UU cw0tj3blWHjANRUA6Zo7rqQqocyvP04hbOiRSpAsZ4/yo66n4I/48p0PrXqbXscqeW 81laG5bvWUtQQOertnL3/JsYqlgV9lglclTTzrUcULo0w4cTgwRMXMrbceGnfOi+4m rInzpJEpr6ucnq2WrCoKJRdF1NBrPPn2qG8CFfwEEJGnmNnNeuzQByUa68U0aPEXiE jleon9wjXOoYbICTyLc1CWU84KkjgCKDCbEhHQDQ7OJQmprmhu/SxORmN6O9e9L5FF 832jZ10uOo27w== From: fdmanana@kernel.org To: linux-btrfs@vger.kernel.org Subject: [PATCH 8/9] btrfs: assert tree lock is held when linking free space Date: Thu, 4 May 2023 12:04:25 +0100 Message-Id: <018ffcab35457b341195411e85614029f3e25764.1683196407.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 When linking a free space entry, at link_free_space(), the caller should be holding the spinlock 'tree_lock' of the given btrfs_free_space_ctl argument, which is necessary for manipulating the red black tree of free space entries (done by tree_insert_offset(), which already asserts the lock is held) and for manipulating the 'free_space', 'free_extents', 'discardable_extents' and 'discardable_bytes' counters of the given struct btrfs_free_space_ctl. So assert that the spinlock 'tree_lock' of the given btrfs_free_space_ctl is held by the current task. We have multiple code paths that end up calling link_free_space(), and all currently take the lock before calling it. Signed-off-by: Filipe Manana Reviewed-by: Anand Jain --- fs/btrfs/free-space-cache.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/fs/btrfs/free-space-cache.c b/fs/btrfs/free-space-cache.c index 84e09ac50103..f5ddfb2e58a9 100644 --- a/fs/btrfs/free-space-cache.c +++ b/fs/btrfs/free-space-cache.c @@ -1850,6 +1850,8 @@ static int link_free_space(struct btrfs_free_space_ctl *ctl, { int ret = 0; + lockdep_assert_held(&ctl->tree_lock); + ASSERT(info->bytes || info->bitmap); ret = tree_insert_offset(ctl, NULL, info); if (ret) From patchwork Thu May 4 11:04:26 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Filipe Manana X-Patchwork-Id: 13231050 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 EC785C7EE21 for ; Thu, 4 May 2023 11:04:59 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230190AbjEDLE5 (ORCPT ); Thu, 4 May 2023 07:04:57 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39422 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230075AbjEDLEm (ORCPT ); Thu, 4 May 2023 07:04:42 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [IPv6:2604:1380:4641:c500::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6BFFF35B8 for ; Thu, 4 May 2023 04:04:40 -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 4CD206176A for ; Thu, 4 May 2023 11:04:40 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 42C44C4339B for ; Thu, 4 May 2023 11:04:39 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1683198279; bh=8VEapYDS3rVWS3k+U7V9h4mOg5TgBmAa3TgFbBcLPWk=; h=From:To:Subject:Date:In-Reply-To:References:From; b=SbuYXY0gn7Cd+/KZ4qDSf5alUc0kns1XR00m5eNgZSxs9WblH4BcGqE8/9wgkenD9 rdRIa9AzdfoBOZxRpuAAhrwhOp8ESLb0/Gh+jnr8tJ2TS8qbrhhseP02YNEZ4DvnDu 1kLfkt0ResIQQ4VRnnnIddEnRUz6n5gvDQQMplOuwIxOe04IdKgJpPTUvtfI/VY48p FvUuC1yeWTXOG1k4/c6C2kMwbvaOlIaVNJqoFH0YkZnyQTE6gfOgl/npFnKXHdc2be K02bdNNJMNMeGxrmxrictcizDFq9Ijp57gE/Qde+X6gqnscByqhNOtNOqW6Sbfnq+F z8SjCGCU/vbeg== From: fdmanana@kernel.org To: linux-btrfs@vger.kernel.org Subject: [PATCH 9/9] btrfs: assert tree lock is held when removing free space entries Date: Thu, 4 May 2023 12:04:26 +0100 Message-Id: <9bdbe975c02ac41e03512cc334c199602d04a87d.1683196407.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 Removing a free space entry from an in memory space cache requires having the corresponding btrfs_free_space_ctl's 'tree_lock' held. We have several code paths that remove an entry, so add assertions where appropriate to verify we are holding the lock, as the lock is acquired by some other function up in the call chain, which makes it easy to miss in the future. Note: for this to work we need to lock the local btrfs_free_space_ctl at load_free_space_cache(), which was not being done because it's local, declared on the stack, so no other task has access to it. Signed-off-by: Filipe Manana --- fs/btrfs/free-space-cache.c | 34 ++++++++++++++++++++++++---------- 1 file changed, 24 insertions(+), 10 deletions(-) diff --git a/fs/btrfs/free-space-cache.c b/fs/btrfs/free-space-cache.c index f5ddfb2e58a9..fdca565477f3 100644 --- a/fs/btrfs/free-space-cache.c +++ b/fs/btrfs/free-space-cache.c @@ -927,25 +927,27 @@ static int copy_free_space_cache(struct btrfs_block_group *block_group, const u64 bytes = info->bytes; unlink_free_space(ctl, info, true); + spin_unlock(&ctl->tree_lock); kmem_cache_free(btrfs_free_space_cachep, info); ret = btrfs_add_free_space(block_group, offset, bytes); + spin_lock(&ctl->tree_lock); } else { u64 offset = info->offset; u64 bytes = ctl->unit; - while (search_bitmap(ctl, info, &offset, &bytes, - false) == 0) { + ret = search_bitmap(ctl, info, &offset, &bytes, false); + if (ret == 0) { + bitmap_clear_bits(ctl, info, offset, bytes, true); + spin_unlock(&ctl->tree_lock); ret = btrfs_add_free_space(block_group, offset, bytes); - if (ret) - break; - bitmap_clear_bits(ctl, info, offset, bytes, true); - offset = info->offset; - bytes = ctl->unit; + spin_lock(&ctl->tree_lock); + } else { + free_bitmap(ctl, info); + ret = 0; } - free_bitmap(ctl, info); } - cond_resched(); + cond_resched_lock(&ctl->tree_lock); } return ret; } @@ -1039,7 +1041,9 @@ int load_free_space_cache(struct btrfs_block_group *block_group) block_group->bytes_super)); if (matched) { + spin_lock(&tmp_ctl.tree_lock); ret = copy_free_space_cache(block_group, &tmp_ctl); + spin_unlock(&tmp_ctl.tree_lock); /* * ret == 1 means we successfully loaded the free space cache, * so we need to re-set it here. @@ -1832,6 +1836,8 @@ static inline void unlink_free_space(struct btrfs_free_space_ctl *ctl, struct btrfs_free_space *info, bool update_stat) { + lockdep_assert_held(&ctl->tree_lock); + rb_erase(&info->offset_index, &ctl->free_space_offset); rb_erase_cached(&info->bytes_index, &ctl->free_space_bytes); ctl->free_extents--; @@ -1881,6 +1887,8 @@ static void relink_bitmap_entry(struct btrfs_free_space_ctl *ctl, if (RB_EMPTY_NODE(&info->bytes_index)) return; + lockdep_assert_held(&ctl->tree_lock); + rb_erase_cached(&info->bytes_index, &ctl->free_space_bytes); rb_add_cached(&info->bytes_index, &ctl->free_space_bytes, entry_less); } @@ -2991,8 +2999,11 @@ static void __btrfs_return_cluster_to_free_space( struct btrfs_block_group *block_group, struct btrfs_free_cluster *cluster) { + struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl; struct rb_node *node; + lockdep_assert_held(&ctl->tree_lock); + spin_lock(&cluster->lock); if (cluster->block_group != block_group) { spin_unlock(&cluster->lock); @@ -3005,7 +3016,6 @@ static void __btrfs_return_cluster_to_free_space( node = rb_first(&cluster->root); while (node) { - struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl; struct btrfs_free_space *entry; entry = rb_entry(node, struct btrfs_free_space, offset_index); @@ -3344,6 +3354,8 @@ static int btrfs_bitmap_cluster(struct btrfs_block_group *block_group, unsigned long total_found = 0; int ret; + lockdep_assert_held(&ctl->tree_lock); + i = offset_to_bit(entry->offset, ctl->unit, max_t(u64, offset, entry->offset)); want_bits = bytes_to_bits(bytes, ctl->unit); @@ -3433,6 +3445,8 @@ setup_cluster_no_bitmap(struct btrfs_block_group *block_group, u64 max_extent; u64 total_size = 0; + lockdep_assert_held(&ctl->tree_lock); + entry = tree_search_offset(ctl, offset, 0, 1); if (!entry) return -ENOSPC;