Received: by 2002:a05:6a10:8c0a:0:0:0:0 with SMTP id go10csp916196pxb; Wed, 3 Mar 2021 20:57:15 -0800 (PST) X-Google-Smtp-Source: ABdhPJzdXfIDLhpgrUlnQs6OX1+CXyVXvbWE+CU0zdoa+Z///Uhh7dsxaBPvSrdnUsna8oaSq2im X-Received: by 2002:a17:907:d8a:: with SMTP id go10mr2314290ejc.46.1614833835559; Wed, 03 Mar 2021 20:57:15 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1614833835; cv=none; d=google.com; s=arc-20160816; b=l9BTa3khiUoB+if+7uLTs61eWf59rm/1eZc9rGm0khJPmbh/MDnBvA8bYD1JXyvyBa XABpvJOXXa5wLoqh07mamwx2SLNi39gFIbhxSPT7sL6V133ugbwhVOC0duNI3VfONsMb E4S+xOLVufXEUb0NiBtE4+uY9KvTK0ARLpDYJrFsknnXdLD4FnuNKK1dN0Xitp2vdtKi W23R4xlnYhiffoGWBsAQnrmSYCoZ2jgWLQx1wEB2mPCpuReop2jOAXxPizjDGmOe1cj+ 54kXZ/cRXUYJkuoal645HoZ59izDETKaEcGDy5MKgrzPNcb8VxNMt13wdzey8sxwkxFN Iegw== 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=B39txOUvhVCXu92TY9OgrLp0GQAL2ZPiiEdT8tXAlZg=; b=hLVeCSzpHsPZPMZoh5UTVF1SLAqLXxfoTnHab1p0JkUuvj+xuS7h6qTg2LGdsDC7rg AnHY8DJ6irUHkz0vQH3PLhrKFlRbbFrZ/NqpAezCRuJ7D9SlgnTbMt5XkB8jZq/gPKpf 2GdlUevjCnb6ZIHiBMUxYxT87/1+c1e+yl2VWaRqPe0p1Y2WFf54l7SVtgxqrO+DeiyY cnKKXbtzE6Nvw69jEY2tsWrVPGDGYFEgF6ztyZO8B24ugtRZTuTkh1nd3ymnAYglKW11 rCQtVmhUIxmcHZs0Zs1bGbLmdRgHIJ6hhX0xVQu/op/3r2phH88D3jznV36yOUA2rwRC Bhpw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=UiX7CB3a; 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 p8si18038401edj.93.2021.03.03.20.56.53; Wed, 03 Mar 2021 20:57:15 -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=UiX7CB3a; 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 S1442957AbhCBCRV (ORCPT + 99 others); Mon, 1 Mar 2021 21:17:21 -0500 Received: from mail.kernel.org ([198.145.29.99]:55158 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S242310AbhCATo2 (ORCPT ); Mon, 1 Mar 2021 14:44:28 -0500 Received: by mail.kernel.org (Postfix) with ESMTPSA id CB5206524C; Mon, 1 Mar 2021 17:27:46 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1614619667; bh=p5nuNUD/3W6qhFpJpTknVkK9pkmt4N7lCzU8Qdop+PQ=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=UiX7CB3adcWLEeX8gS6h5eJWw3a3cXuryBKk98lxhsZ+LyQ0cpfpb5qhCS0aI2tA3 DmtzRRdcjMcwGmLJKD0cgVoK42lNl82w1Unodu8DDA0y5p3tDy5vB5PPMaN0gJjgAm 2tTYEZen7oiypz6nuflnMsgu6gvgb+9wk4Pm3RF4= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Nikolay Borisov , Josef Bacik , David Sterba Subject: [PATCH 5.10 538/663] btrfs: account for new extents being deleted in total_bytes_pinned Date: Mon, 1 Mar 2021 17:13:06 +0100 Message-Id: <20210301161208.476036599@linuxfoundation.org> X-Mailer: git-send-email 2.30.1 In-Reply-To: <20210301161141.760350206@linuxfoundation.org> References: <20210301161141.760350206@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: Josef Bacik commit 81e75ac74ecba929d1e922bf93f9fc467232e39f upstream. My recent patch set "A variety of lock contention fixes", found here https://lore.kernel.org/linux-btrfs/cover.1608319304.git.josef@toxicpanda.com/ (Tracked in https://github.com/btrfs/linux/issues/86) that reduce lock contention on the extent root by running delayed refs less often resulted in a regression in generic/371. This test fallocate()'s the fs until it's full, deletes all the files, and then tries to fallocate() until full again. Before these patches we would run all of the delayed refs during flushing, and then would commit the transaction because we had plenty of pinned space to recover in order to allocate. However my patches made it so we weren't running the delayed refs as aggressively, which meant that we appeared to have less pinned space when we were deciding to commit the transaction. We use the space_info->total_bytes_pinned to approximate how much space we have pinned. It's approximate because if we remove a reference to an extent we may free it, but there may be more references to it than we know of at that point, but we account it as pinned at the creation time, and then it's properly accounted when the delayed ref runs. The way we account for pinned space is if the delayed_ref_head->total_ref_mod is < 0, because that is clearly a freeing option. However there is another case, and that is where ->total_ref_mod == 0 && ->must_insert_reserved == 1. When we allocate a new extent, we have ->total_ref_mod == 1 and we have ->must_insert_reserved == 1. This is used to indicate that it is a brand new extent and will need to have its extent entry added before we modify any references on the delayed ref head. But if we subsequently remove that extent reference, our ->total_ref_mod will be 0, and that space will be pinned and freed. Accounting for this case properly allows for generic/371 to pass with my delayed refs patches applied. It's important to note that this problem exists without the referenced patches, it just was uncovered by them. CC: stable@vger.kernel.org # 5.10 Reviewed-by: Nikolay Borisov Signed-off-by: Josef Bacik Signed-off-by: David Sterba Signed-off-by: Greg Kroah-Hartman --- fs/btrfs/delayed-ref.c | 5 +++++ fs/btrfs/extent-tree.c | 33 +++++++++++++++++++-------------- 2 files changed, 24 insertions(+), 14 deletions(-) --- a/fs/btrfs/delayed-ref.c +++ b/fs/btrfs/delayed-ref.c @@ -732,11 +732,16 @@ static noinline void update_existing_hea * 2. We were negative and went to 0 or positive, so no longer can say * that the space would be pinned, decrement our counter from the * total_bytes_pinned counter. + * 3. We are now at 0 and have ->must_insert_reserved set, which means + * this was a new allocation and then we dropped it, and thus must + * add our space to the total_bytes_pinned counter. */ if (existing->total_ref_mod < 0 && old_ref_mod >= 0) btrfs_mod_total_bytes_pinned(fs_info, flags, existing->num_bytes); else if (existing->total_ref_mod >= 0 && old_ref_mod < 0) btrfs_mod_total_bytes_pinned(fs_info, flags, -existing->num_bytes); + else if (existing->total_ref_mod == 0 && existing->must_insert_reserved) + btrfs_mod_total_bytes_pinned(fs_info, flags, existing->num_bytes); spin_unlock(&existing->lock); } --- a/fs/btrfs/extent-tree.c +++ b/fs/btrfs/extent-tree.c @@ -1755,23 +1755,28 @@ void btrfs_cleanup_ref_head_accounting(s { int nr_items = 1; /* Dropping this ref head update. */ - if (head->total_ref_mod < 0) { + /* + * We had csum deletions accounted for in our delayed refs rsv, we need + * to drop the csum leaves for this update from our delayed_refs_rsv. + */ + if (head->total_ref_mod < 0 && head->is_data) { + spin_lock(&delayed_refs->lock); + delayed_refs->pending_csums -= head->num_bytes; + spin_unlock(&delayed_refs->lock); + nr_items += btrfs_csum_bytes_to_leaves(fs_info, head->num_bytes); + } + + /* + * We were dropping refs, or had a new ref and dropped it, and thus must + * adjust down our total_bytes_pinned, the space may or may not have + * been pinned and so is accounted for properly in the pinned space by + * now. + */ + if (head->total_ref_mod < 0 || + (head->total_ref_mod == 0 && head->must_insert_reserved)) { u64 flags = btrfs_ref_head_to_space_flags(head); btrfs_mod_total_bytes_pinned(fs_info, flags, -head->num_bytes); - - /* - * We had csum deletions accounted for in our delayed refs rsv, - * we need to drop the csum leaves for this update from our - * delayed_refs_rsv. - */ - if (head->is_data) { - spin_lock(&delayed_refs->lock); - delayed_refs->pending_csums -= head->num_bytes; - spin_unlock(&delayed_refs->lock); - nr_items += btrfs_csum_bytes_to_leaves(fs_info, - head->num_bytes); - } } btrfs_delayed_refs_rsv_release(fs_info, nr_items);