From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-10.5 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER, MAILING_LIST_MULTI,NICE_REPLY_A,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED, USER_AGENT_SANE_1 autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 4A11AC433DB for ; Wed, 10 Feb 2021 15:18:51 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 0102564DEC for ; Wed, 10 Feb 2021 15:18:50 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231205AbhBJPSp (ORCPT ); Wed, 10 Feb 2021 10:18:45 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51978 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232009AbhBJPSW (ORCPT ); Wed, 10 Feb 2021 10:18:22 -0500 Received: from mail-qk1-x731.google.com (mail-qk1-x731.google.com [IPv6:2607:f8b0:4864:20::731]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6495CC061574 for ; Wed, 10 Feb 2021 07:17:41 -0800 (PST) Received: by mail-qk1-x731.google.com with SMTP id w19so1485814qki.13 for ; Wed, 10 Feb 2021 07:17:41 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=toxicpanda-com.20150623.gappssmtp.com; s=20150623; h=subject:to:cc:references:from:message-id:date:user-agent :mime-version:in-reply-to:content-language:content-transfer-encoding; bh=aSkGMnKtz5CAmYrCQE+eVfJ11ex2TNLAHLDE8rue78w=; b=qlip1vsum0hitme/1fqSD2Eq+Y8ew+m1YPpX7K0ixA7kupXlbxqXZiKnkvwMr8pLT7 49AYJNoWefi1ioeIVV7x6PVhSTKF2ppuhXgfDvRG6DKvM8m10Nl8FAwauy4q6e+gaV6u lDeo0Q1Fo6AIIDoywK2Uxr1VJ9L0h+NyKXF8wP8w3iBCdmVXgFi9zwDUMO3WAaeKiK3W eibfqZPSl8MOghx0BXDXYMMv688nfhMYJo2Wdl+I3tm8zjjZd++bySNz1aP/H5zBOOI9 xTv2v1lEDfywELybbyRgPib+c+ooeQCV3qoC6iJoYvATH01bCknj7BZiWM/CKUQm3ImE Zt0g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:subject:to:cc:references:from:message-id:date :user-agent:mime-version:in-reply-to:content-language :content-transfer-encoding; bh=aSkGMnKtz5CAmYrCQE+eVfJ11ex2TNLAHLDE8rue78w=; b=Tqw9FfseDXZh4tw7O0YpQyj/+MDuPV0e0NlCklCbQF3ydL4MQro1cvwk+ekMHdtss7 RQFgeSBTJ8Sja3J3S2wwb2Y4ivOlmsPr1ujwVUlOkzWsKuxI4rLj9VspyHS1u1bVHExM Ay2ObRe4VnD5/zyvR9wU2lhSxSh9URLbN5V4dWLg43DbI8J1kC6TvaSbaRBuDVxSb7zL zwvNKZL/OdXjG8w83iq5nqCKtj710vQBklzulaaQVlmNtJTRia7JZFkRzgSaTaC6OooZ YFTaGfADJAVeUz/9Zu//fSRHmuf1ZzSdOCcAbpjLDh1Q1G06eE/FOL84JxgLyJK2F5qV j5oA== X-Gm-Message-State: AOAM530zTIPIRjo79U08425CZHUbfEia5CNmyGPNCkb8KCC/2Oy+kxjI /vLxV9ULZIp/2s4UKkkJXjMXnWJa+EHSQ3R3 X-Google-Smtp-Source: ABdhPJz8p3/Se1+wEkQ6nJtPK5Ks0fFYaXxKzBhGFIOPsKXDLon0geQP+eMhFX02Zmpouo529eJn3g== X-Received: by 2002:a37:644f:: with SMTP id y76mr3748353qkb.105.1612970260381; Wed, 10 Feb 2021 07:17:40 -0800 (PST) Received: from [192.168.1.45] (cpe-174-109-172-136.nc.res.rr.com. [174.109.172.136]) by smtp.gmail.com with ESMTPSA id 90sm1403487qtb.45.2021.02.10.07.17.39 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Wed, 10 Feb 2021 07:17:39 -0800 (PST) Subject: Re: [PATCH] btrfs: Fix race between extent freeing/allocation when using bitmaps To: Nikolay Borisov , linux-btrfs@vger.kernel.org Cc: stable@vger.kernel.org References: <20210208082652.2654024-1-nborisov@suse.com> From: Josef Bacik Message-ID: <3e2f0922-f492-0cc7-d76f-ed1dcaa86dfd@toxicpanda.com> Date: Wed, 10 Feb 2021 10:17:38 -0500 User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:78.0) Gecko/20100101 Thunderbird/78.7.0 MIME-Version: 1.0 In-Reply-To: <20210208082652.2654024-1-nborisov@suse.com> Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US Content-Transfer-Encoding: 7bit Precedence: bulk List-ID: X-Mailing-List: linux-btrfs@vger.kernel.org On 2/8/21 3:26 AM, Nikolay Borisov wrote: > During allocation the allocator will try to allocate an extent using > cluster policy. Once the current cluster is exhausted it will remove the > its entry under btrfs_free_cluster::lock and subsequently acquire > btrfs_free_space_ctl::tree_lock to dispose of the already-deleted > entry and adjust btrfs_free_space_ctl::total_bitmap. This poses a > problem because there exists a race condition between removing the > entry under one lock and doing the necessary accounting holding a > different lock since extent freeing only uses the 2nd lock. This can > result in the following situation: > > T1: T2: > btrfs_alloc_from_cluster insert_into_bitmap > if (entry->bytes == 0) if (block_group && !list_empty(&block_group->cluster_list)) { > rb_erase(entry) > > spin_unlock(&cluster->lock); > (total_bitmaps is still 4) spin_lock(&cluster->lock); > root> > spin_lock(&ctl->tree_lock); > recalculate_thresholds due to total_bitmaps > becoming 5 and triggering > an ASSERT> > > To fix this ensure that once depleted, the cluster entry is deleted when > both cluster lock and tree locks are held in the allocator (T1), this > ensures that even if there is a race with a concurrent > insert_into_bitmap call it will correctly find the entry in the cluster > and add the new space to it. > > Signed-off-by: Nikolay Borisov > Cc: Reviewed-by: Josef Bacik Thanks, Josef