Received: by 2002:a05:6a10:eb17:0:0:0:0 with SMTP id hx23csp455458pxb; Thu, 9 Sep 2021 05:01:11 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyldRE/yIbzUzfArm/Z/uHWBp+ElK1JNjOFwxWCuw7KPu/awtSRa7iDflN7xkiOcUyfelTX X-Received: by 2002:a17:906:7e49:: with SMTP id z9mr3004444ejr.380.1631188866745; Thu, 09 Sep 2021 05:01:06 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1631188866; cv=none; d=google.com; s=arc-20160816; b=k51D8yiftwt6c1VRvdo8P2e5F+w3yVynEpAj12kdOxs0toG/eM/hbbc4mBLqJeev4N cv9ylMrNAzDvo6rGrq3Gtu0qMv2xJ0lUahpuz12tG6MdcJwMKuB2XfesJYkUVilQaa32 FyDoZZNCzLuBAjK2jhoQzpsGSPtgEQtN+uVr6qeeJJ0onLUTUWA96Vh3G7+QnGIjOuwO roDngpeOZAuDJ+o8QDP9lBMVwlKq55GphS1daS8tHWLuqP02+gCnjxxwLs0AfEuLCwYZ hcNvnGGynNkaxKNfQiIbQmojDQuVQF9JfhdaXJUH7o/AHic2ovuiRx9unjKsOxNNXxMK /7nA== 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 :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=0TJHcTn895lsPMvvw2JMxI5ugnW0T7TvhO9sH8XQkMM=; b=EcSXAb+tjdAx3TNB9Y1YbwLUZTA0DqpqXqsRNKqXetGBQWU8+pZ4mxhm4RlNNohhRF bY2Cnd9IlYqmJXzKN233qwgeGo6RD8SCBALmExlr/CKNC3FmXrgwe2pddseyFK0tLdE+ KhJYnh8tYzzcwQtuXmwf460qpxtMnsbABuZ4BlUqQLvDIKOXOQ6tz7ve65ueCRWJ2sTO bRqtNtWB87BtWLT1f2ysOV3Ov0yjh5FCB8ZA/Br0HeQu8EryUrHdgK5x6ijI6Z2rVw7N 6/grpKdT/WGQtuneJeRb+LslGYZbHcrzueMoMNyfFRhg/x7uk4a0ROut1V4GW53mHQxX WA/w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=iHI6E7b1; 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=kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id n10si2024491ejk.210.2021.09.09.05.00.39; Thu, 09 Sep 2021 05:01:06 -0700 (PDT) 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=@kernel.org header.s=k20201202 header.b=iHI6E7b1; 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=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S240488AbhIIMA3 (ORCPT + 99 others); Thu, 9 Sep 2021 08:00:29 -0400 Received: from mail.kernel.org ([198.145.29.99]:34238 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S243040AbhIIL4K (ORCPT ); Thu, 9 Sep 2021 07:56:10 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id EB27C613A2; Thu, 9 Sep 2021 11:45:13 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1631187914; bh=jnOqQ6KW9/2844WvO0e8zDichRbeAVLyXq2HEwGI1qU=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=iHI6E7b1I+YvxxEhq1FsUnxo8IHGEM7fO6b7k+YvJY644Q65H5l1mdJzKOkec93bN 6Tr2uNlDqejqbqFhZy8c6n6IHAiAsDFIbvRCSKG5fg6zbOd4kIHl0fxOENoDzHVHcI 0jpiLrLJhjFdrGkaNTut9C5vZl2O3xCw49hP5Ie+EOe9u+/RcRRdowSdX0NI2UPAao u8hmhpbo2P1/httOHK2WgKpyLqJJAYGar9+pNJ8P5XefNiyGtsAXKKrzIqRHcRGswD lLN9qkPyMK2i1Y4tkjH+XWGaAq4NPmcaKLNOc+ye5ft7HAHf0dNTx6qsPyx63JGdWX NhpcWRoucRC0A== From: Sasha Levin To: linux-kernel@vger.kernel.org, stable@vger.kernel.org Cc: Filipe Manana , Josef Bacik , David Sterba , Sasha Levin , linux-btrfs@vger.kernel.org Subject: [PATCH AUTOSEL 5.14 190/252] btrfs: remove racy and unnecessary inode transaction update when using no-holes Date: Thu, 9 Sep 2021 07:40:04 -0400 Message-Id: <20210909114106.141462-190-sashal@kernel.org> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20210909114106.141462-1-sashal@kernel.org> References: <20210909114106.141462-1-sashal@kernel.org> MIME-Version: 1.0 X-stable: review X-Patchwork-Hint: Ignore Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Filipe Manana [ Upstream commit cceaa89f02f15f232391ae4be214137b0a0285c0 ] When using the NO_HOLES feature and expanding the size of an inode, we update the inode's last_trans, last_sub_trans and last_log_commit fields at maybe_insert_hole() so that a fsync does know that the inode needs to be logged (by making sure that btrfs_inode_in_log() returns false). This happens for expanding truncate operations, buffered writes, direct IO writes and when cloning extents to an offset greater than the inode's i_size. However the way we do it is racy, because in between setting the inode's last_sub_trans and last_log_commit fields, the log transaction ID that was assigned to last_sub_trans might be committed before we read the root's last_log_commit and assign that value to last_log_commit. If that happens it would make a future call to btrfs_inode_in_log() return true. This is a race that should be extremely unlikely to be hit in practice, and it is the same that was described by commit bc0939fcfab0d7 ("btrfs: fix race between marking inode needs to be logged and log syncing"). The fix would simply be to set last_log_commit to the value we assigned to last_sub_trans minus 1, like it was done in that commit. However updating these two fields plus the last_trans field is pointless here because all the callers of btrfs_cont_expand() (which is the only caller of maybe_insert_hole()) always call btrfs_set_inode_last_trans() or btrfs_update_inode() after calling btrfs_cont_expand(). Calling either btrfs_set_inode_last_trans() or btrfs_update_inode() guarantees that the next fsync will log the inode, as it makes btrfs_inode_in_log() return false. So just remove the code that explicitly sets the inode's last_trans, last_sub_trans and last_log_commit fields. Reviewed-by: Josef Bacik Signed-off-by: Filipe Manana Signed-off-by: David Sterba Signed-off-by: Sasha Levin --- fs/btrfs/inode.c | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/fs/btrfs/inode.c b/fs/btrfs/inode.c index bd5689fa290e..ab04f9dd465e 100644 --- a/fs/btrfs/inode.c +++ b/fs/btrfs/inode.c @@ -5088,15 +5088,13 @@ static int maybe_insert_hole(struct btrfs_root *root, struct btrfs_inode *inode, int ret; /* - * Still need to make sure the inode looks like it's been updated so - * that any holes get logged if we fsync. + * If NO_HOLES is enabled, we don't need to do anything. + * Later, up in the call chain, either btrfs_set_inode_last_sub_trans() + * or btrfs_update_inode() will be called, which guarantee that the next + * fsync will know this inode was changed and needs to be logged. */ - if (btrfs_fs_incompat(fs_info, NO_HOLES)) { - inode->last_trans = fs_info->generation; - inode->last_sub_trans = root->log_transid; - inode->last_log_commit = root->last_log_commit; + if (btrfs_fs_incompat(fs_info, NO_HOLES)) return 0; - } /* * 1 - for the one we're dropping -- 2.30.2