Received: by 2002:a05:6a10:8c0a:0:0:0:0 with SMTP id go10csp1709821pxb; Mon, 8 Mar 2021 04:37:21 -0800 (PST) X-Google-Smtp-Source: ABdhPJwU3PEpO63DOuMVIMCHiFTV4RNGF5iHi5gCTFMmiouGk70fLVAj0pCP/dQ/Om0Nf9h/6t71 X-Received: by 2002:a50:fd15:: with SMTP id i21mr20185369eds.384.1615207041474; Mon, 08 Mar 2021 04:37:21 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1615207041; cv=none; d=google.com; s=arc-20160816; b=e1+FgGEKzKweF21xbJLcCfUpJUrlH7Hr0tszFCIlw8vtEdF5lcWZrxVq+6fIFkgdlj bzAZsvovcCerxo/EHDRqcRpSmojeA8LriPH0lmmT9JDZ4SPNuYDztKccyEx0NHBCqmob uMGkB6C8aqW4FEH1WKO/75eGu2LwRQbgnaXxz1WuDFmVfV1sAte74XiOWqrJICOLNA3E yX1VSHUpd8EUc6NPEGaKKnCbLcNir0EZH4FYfKI4JIYLaEgyBNHDfHsAMoHEtwAHjIDm wWx0d3TPwzPnc9aur2AdMdmhWw2i+bUZl/kvB2VyCfgP+Lrp0rwJzDNlrbK+GyCZPzZC E8VQ== 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=KdBr3m5pq8Qyjv4TCnc8k8egexS7RtG9bCrFub5Q9vM=; b=yd+JE790nveZIYCegeGGOVZT9aTCKnLD+LBdXfFCGl/F6OoCF0xhAaPrrcdLfsDMxf 52HQB0LLvp1f6iVab/dekBKpyUKUmWVFtWjoun35KPBE01EQ44UZkZIIPXIeWihyK+az TtWmWw/lvQS3/7EAAXK28xne2ZLh+1r461BfizT70qunqipL9xnCYexVBjCP0JEcvzFp e7T+ANbTl45zR3hx3Nkztml0n2Ig1namFZ0RFGRY8jDsC+p2XoMqOPtXGzuf9mlb2xxo hsTz9OUwf9tyklL1OOpDA7BULzasmrkOn65nhQSO8K6ZhVqaNURc2E/MLKfQVMWsFdAM 7Mew== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=Y91ypQcZ; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 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 vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id s19si6791962ejd.449.2021.03.08.04.36.58; Mon, 08 Mar 2021 04:37:21 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=Y91ypQcZ; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 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 S231259AbhCHMdo (ORCPT + 99 others); Mon, 8 Mar 2021 07:33:44 -0500 Received: from mail.kernel.org ([198.145.29.99]:42350 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229965AbhCHMdX (ORCPT ); Mon, 8 Mar 2021 07:33:23 -0500 Received: by mail.kernel.org (Postfix) with ESMTPSA id 21521651C9; Mon, 8 Mar 2021 12:33:21 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1615206802; bh=heEBG8itY4ZSEXCC+u/JqWQ5Vqd79AS4CIm2jrp1OXg=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Y91ypQcZzJ5nmfM8DHTOFLraxCdcPfnlKoLSFXmoePxkzSn3NNcb+n5DLiciq1Kgk pbqg2yOnBgslq4Tm65N+cqKDWpOjh69ZfDwU6jC+aiZpbAG2+BuODkGSdSn1mnv/c3 +SAECaXm8wKZGSdi0RbeWIvzvIigPRnaooqZHZcc= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Josef Bacik , Nikolay Borisov , David Sterba Subject: [PATCH 5.10 11/42] btrfs: fix race between extent freeing/allocation when using bitmaps Date: Mon, 8 Mar 2021 13:30:37 +0100 Message-Id: <20210308122718.681086694@linuxfoundation.org> X-Mailer: git-send-email 2.30.1 In-Reply-To: <20210308122718.120213856@linuxfoundation.org> References: <20210308122718.120213856@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Nikolay Borisov commit 3c17916510428dbccdf657de050c34e208347089 upstream. During allocation the allocator will try to allocate an extent using cluster policy. Once the current cluster is exhausted it will remove the 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 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. CC: # 4.4+ Reviewed-by: Josef Bacik Signed-off-by: Nikolay Borisov Signed-off-by: David Sterba Signed-off-by: Greg Kroah-Hartman --- fs/btrfs/free-space-cache.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) --- a/fs/btrfs/free-space-cache.c +++ b/fs/btrfs/free-space-cache.c @@ -3034,8 +3034,6 @@ u64 btrfs_alloc_from_cluster(struct btrf entry->bytes -= bytes; } - if (entry->bytes == 0) - rb_erase(&entry->offset_index, &cluster->root); break; } out: @@ -3052,7 +3050,10 @@ out: ctl->free_space -= bytes; if (!entry->bitmap && !btrfs_free_space_trimmed(entry)) ctl->discardable_bytes[BTRFS_STAT_CURR] -= bytes; + + spin_lock(&cluster->lock); if (entry->bytes == 0) { + rb_erase(&entry->offset_index, &cluster->root); ctl->free_extents--; if (entry->bitmap) { kmem_cache_free(btrfs_free_space_bitmap_cachep, @@ -3065,6 +3066,7 @@ out: kmem_cache_free(btrfs_free_space_cachep, entry); } + spin_unlock(&cluster->lock); spin_unlock(&ctl->tree_lock); return ret;