From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail.burntcomma.com (unknown [62.3.69.246]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id C1A1415B971 for ; Thu, 9 Oct 2025 16:30:30 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=62.3.69.246 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1760027435; cv=none; b=rkEHazQx+6yOK85HX9JXktNKjFmO+UFKBGUjzAI62tfsR/AMMFIssXrkYAPvT61WgExIbwiA726AJX/ayI4J80nm3rkELu3LSDVUnDUDy22NTnnuwmPH//QWRMR4JXc+Gml4nXOKBZ+M1tT8eUTD+NFUp04+jCQNNZ7IW/4GCuc= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1760027435; c=relaxed/simple; bh=xrmQwtnPUbm2TJwqp+uDshrpU/4g7maM0f4BgK8p3/I=; h=Message-ID:Date:Mime-Version:Subject:To:Cc:References:From: In-Reply-To:Content-Type; b=XJ5G9ovZK+yaEEoXAsONYVZ/uwOuGi730ialttEzjOc4SW5ZzykPjq7PRwbZdBNBiBnGoGDbSW03ktB8IwtnD5X9JbLgJ892AMilldkUblid8WIkc8GyIBg+5AsIwQZxpNJ4FcuUbBrmwB05BTg0SH54mz6eCYvkY1PCsF69l5Y= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=harmstone.com; spf=fail smtp.mailfrom=harmstone.com; dkim=pass (1024-bit key) header.d=harmstone.com header.i=@harmstone.com header.b=v/E9RPyt; arc=none smtp.client-ip=62.3.69.246 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=harmstone.com Authentication-Results: smtp.subspace.kernel.org; spf=fail smtp.mailfrom=harmstone.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=harmstone.com header.i=@harmstone.com header.b="v/E9RPyt" Received: from [IPV6:2a02:8012:8cf0:0:ce28:aaff:fe0d:6db2] (beren.burntcomma.com [IPv6:2a02:8012:8cf0:0:ce28:aaff:fe0d:6db2]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256 client-signature RSA-PSS (2048 bits) client-digest SHA256) (Client CN "hellas", Issuer "burntcomma.com" (verified OK)) by mail.burntcomma.com (Postfix) with ESMTPS id C17132C5969; Thu, 9 Oct 2025 17:30:28 +0100 (BST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=harmstone.com; s=mail; t=1760027428; bh=E8UlryQnf8x0GNNFAvu21IrLC/Xsm5rk5PpgZygWmgc=; h=Date:Subject:To:Cc:References:From:In-Reply-To; b=v/E9RPytlQ2xn74H6ViiX3pLZw0461aCz6w0K5wldkk4ijbWY598s+YPYkB0P/qG7 w3ZOvcfNSByiQOEKncZA67WxwdgiBSu/HRPi0Rv+lo5fKYdJsL7RYVJhtiGTklOqby 3nT5lqwomPa6KqoJudyiCZJi7HnANJUPrXaGIZys= Message-ID: <06ac2db3-d7e9-4cd3-97be-f059beb4af6b@harmstone.com> Date: Thu, 9 Oct 2025 17:30:35 +0100 Precedence: bulk X-Mailing-List: linux-btrfs@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 Subject: Re: [PATCH v3 09/17] btrfs: release BG lock before calling btrfs_link_bg_list() To: Filipe Manana Cc: linux-btrfs@vger.kernel.org References: <20251009112814.13942-1-mark@harmstone.com> <20251009112814.13942-10-mark@harmstone.com> <9e72962e-3d4b-4e1a-b206-512904d701ff@harmstone.com> Content-Language: en-US From: Mark Harmstone Autocrypt: addr=mark@harmstone.com; keydata= xsBNBFp/GMsBCACtFsuHZqHWpHtHuFkNZhMpiZMChyou4X8Ueur3XyF8KM2j6TKkZ5M/72qT EycEM0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6 ejiO7RyC0llKXk78t0VQPdGS6HspVhYGJJt21c5vwSzIeZaneKULaxXGwzgYFTroHD9n+QAR AQABwsGsBBgBCAAgFiEEG2JgKYgV0WRwIJAqbKyhHeAWK+0FAlp/GMsCGy4BQAkQbKyhHeAW K+3AdCAEGQEIAB0WIQR6bEAu0hwk2Q9ibSlt5UHXRQtUiwUCWn8YywAKCRBt5UHXRQtUiwdE B/9OpyjmrshY40kwpmPwUfode2Azufd3QRdthnNPAY8Tv9erwsMS3sMh+M9EP+iYJh+AIRO7 fDN/u0AWIqZhHFzCndqZp8JRYULnspXSKPmVSVRIagylKew406XcAVFpEjloUtDhziBN7ykk srAMoLASaBHZpAfp8UAGDrr8Fx1on46rDxsWbh1K1h4LEmkkVooDELjsbN9jvxr8ym8Bkt54 FcpypTOd8jkt/lJRvnKXoL3rZ83HFiUFtp/ZkveZKi53ANUaqy5/U5v0Q0Ppz9ujcRA9I/V3 B66DKMg1UjiigJG6espeIPjXjw0n9BCa9jqGICyJTIZhnbEs1yEpsM87eUIH/0UFLv0b8IZe pL/3QfiFoYSqMEAwCVDFkCt4uUVFZczKTDXTFkwm7zflvRHdy5QyVFDWMyGnTN+Bq48Gwn1M uRT/Sg37LIjAUmKRJPDkVr/DQDbyL6rTvNbA3hTBu392v0CXFsvpgRNYaT8oz7DDBUUWj2Ny 6bZCBtwr/O+CwVVqWRzKDQgVo4t1xk2ts1F0R1uHHLsX7mIgfXBYdo/y4UgFBAJH5NYUcBR+ QQcOgUUZeF2MC9i0oUaHJOIuuN2q+m9eMpnJdxVKAUQcZxDDvNjZwZh+ejsgG4Ejd2XR/T0y XFoR/dLFIhf2zxRylN1xq27M9P2t1xfQFocuYToPsVk= In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit On 09/10/2025 4.16 pm, Filipe Manana wrote: > On Thu, Oct 9, 2025 at 3:58 PM Mark Harmstone wrote: >> >> On 09/10/2025 12.56 pm, Filipe Manana wrote: >>> On Thu, Oct 9, 2025 at 12:29 PM Mark Harmstone wrote: >>>> >>>> Release block_group->lock before calling btrfs_link_bg_list() in >>>> btrfs_delete_unused_bgs(), as this was causing lockdep issues. >>> >>> I believe this was asked before: >>> >>> What issues? >>> Do we have for example any other place where we have a different >>> locking order and can cause a deadlock? >>> >>> Can you please paste the lockdep splat? >> >> I didn't take a copy the first time, and I've not been able to replicate it >> since. >> >> But you can see the issue in patch 4, "btrfs: remove remapped block groups >> from the free-space tree". In btrfs_discard_punt_unused_bgs_list() we acquire >> unused_bgs_lock to loop through the unused_bgs list, then take the individual >> BG lock so we can check its flags. > > So then the problem is caused by patch 4, a potential ABBA deadlock. > > In that case this change should be squashed into that patch or at > least come before and say it's preparation work to avoid a lockdep > splat and potential deadlock, without the Fixes tag since, as far as I > can see, we currently don't have any code where we take the locks in a > different order. That's fine. If there's a version 4 of the patchset I'll resend it like that, otherwise I'll squash patch 9 into patch 4 when I push it. >> In btrfs_delete_unused_bgs() we're acquiring the unused_bgs lock through >> btrfs_link_bg_list(), while still unnecessarily holding the BG lock. >> >> The reason it's in this patchset is that a minor existing bug (holding a >> spinlock longer than we strictly need to) becomes a potential deadlock because >> of patch 4. > > I don't think we can call that an existing bug... > If before patch 4 of this patchset it didn't cause any problems as > mentioned before. > >> >>> >>>> >>>> This lock isn't held in any other place that we call btrfs_link_bg_list(), as >>>> the block group lists are manipulated while holding fs_info->unused_bgs_lock. >>>> >>>> Signed-off-by: Mark Harmstone >>>> Fixes: 0497dfba98c0 ("btrfs: codify pattern for adding block_group to bg_list") >>> >>> Also as told before, this doesn't seem related to the rest of the >>> patchset (the new remap tree feature). >>> So instead of dragging this along in every new version of the >>> patchset, can you please make it a standalone patch and remove it from >>> future versions of the patchset? >>> >>> Thanks. >>> >>>> --- >>>> fs/btrfs/block-group.c | 3 ++- >>>> 1 file changed, 2 insertions(+), 1 deletion(-) >>>> >>>> diff --git a/fs/btrfs/block-group.c b/fs/btrfs/block-group.c >>>> index d3433a5b169f..a3c984f905fc 100644 >>>> --- a/fs/btrfs/block-group.c >>>> +++ b/fs/btrfs/block-group.c >>>> @@ -1620,6 +1620,8 @@ void btrfs_delete_unused_bgs(struct btrfs_fs_info *fs_info) >>>> if ((space_info->total_bytes - block_group->length < used && >>>> block_group->zone_unusable < block_group->length) || >>>> has_unwritten_metadata(block_group)) { >>>> + spin_unlock(&block_group->lock); >>>> + >>>> /* >>>> * Add a reference for the list, compensate for the ref >>>> * drop under the "next" label for the >>>> @@ -1628,7 +1630,6 @@ void btrfs_delete_unused_bgs(struct btrfs_fs_info *fs_info) >>>> btrfs_link_bg_list(block_group, &retry_list); >>>> >>>> trace_btrfs_skip_unused_block_group(block_group); >>>> - spin_unlock(&block_group->lock); >>>> spin_unlock(&space_info->lock); >>>> up_write(&space_info->groups_sem); >>>> goto next; >>>> -- >>>> 2.49.1 >>>> >>>> >>