Received: by 2002:a05:6a10:eb17:0:0:0:0 with SMTP id hx23csp3287851pxb; Mon, 6 Sep 2021 17:47:08 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyvNfeogD4Z6FjTTMxBHNmZHqo2EedV2pdw42dHbJnrDINlwu7EAMw5+atugMbvcANiarLb X-Received: by 2002:a05:6638:2509:: with SMTP id v9mr12844974jat.95.1630975628260; Mon, 06 Sep 2021 17:47:08 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1630975628; cv=none; d=google.com; s=arc-20160816; b=GZTOfWJr+mYNyEOvz95dku0QSoSmlO6VCIm7upgvxC/SIxQXddyt2uT7dmuEhckS1Y qXH4LwPuke/0RpJs/PKAdhj4JQhVlbM/gvXXkv5sSM33hUWHd6w/CbwxCbxAzJa6Wmcn CXdOG0VZQ0f1HthjTvTSmNjkE7JoE+8qrGjLEV1RMs9+ndmHiuJvusv9GJ4Lc1mBGM3N Q/ZOnetAlwoOQvmSkdGVPCV8BW2YqxTB07sPcQ7nwPhbNHSPmmJ2ji3Q0BQ/PKX27csN qGoVelqzKv4ObgbUUHoVcahkSMlVA4ZRxp4bryTmHrMlLywO3IsOF7yEwUd7gGXW8U2x 9McA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:to:subject:message-id :date:from:in-reply-to:references:mime-version:dkim-signature; bh=vDxr7MTqqKSUfCFqjA+zNhlH9fpaOeP/gTRYU5+BByc=; b=j4fu00axb2ww0X1RWm0m+WCqgnivqststjrXYYDaMq1suPN76cqPTRDb2yLjmJO2QJ 9MDGshqfCaJLNnL84JOUUseRpUfl69hQSWiRYUHEcTWLQ0T8mjCufLY89KnvJycdx5px RQ7c36QUTZTiFn88D/fUIt9+50RxG4dNAk/L9un6a1N/ya6CwSwpKHExu+dV2+08fWM/ I/oWVXy+Vz0P6RliYrGzXzQO61OeIDCG5FcIoxZA+7gWF/EufVIT3borlpJhbP801ei3 0su2qsVow3vvY2c8FigjKnDSXjWWRiSjUshGayu2bTo+Nrj6J1wYMJ45NtUJFye8Z6aD cajQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=mtWaSwNo; 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=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id h15si9290386ili.51.2021.09.06.17.46.56; Mon, 06 Sep 2021 17:47:08 -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=@gmail.com header.s=20210112 header.b=mtWaSwNo; 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=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234172AbhIGApq (ORCPT + 99 others); Mon, 6 Sep 2021 20:45:46 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56042 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229842AbhIGApp (ORCPT ); Mon, 6 Sep 2021 20:45:45 -0400 Received: from mail-il1-x12d.google.com (mail-il1-x12d.google.com [IPv6:2607:f8b0:4864:20::12d]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8C93AC061575; Mon, 6 Sep 2021 17:44:40 -0700 (PDT) Received: by mail-il1-x12d.google.com with SMTP id i13so8333993ilm.4; Mon, 06 Sep 2021 17:44:40 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :content-transfer-encoding; bh=vDxr7MTqqKSUfCFqjA+zNhlH9fpaOeP/gTRYU5+BByc=; b=mtWaSwNow04g6vRuxJL3IYZcg4RrThTXMe2SjTc0NRU0WawPXPqTcFpOHdTNnrPBrw KubpkymSbTRaXwcv5shzhtY0HtC174ucUG+dGRiuwAh6d6Fyjs7RfstFhXlthFmo4+R5 +mS/9btLHLAkHEwFUegVBw+3sRD7pMGvsBarC3WB/2yRVOG5l8CSsnflsIYgoCk4cLD4 Esb323nR5oK32sEpqPUVXbuJQ9xYNFtCuxiGfS5O3Fyz9WEXsv3bOaQlwFvAhvKEJn9j uZMnlpsBWG3B/Uu/3fVHpYLpCy+R6XfKG6p8Hsv1yxSqxnBP6NvIXoDhlnGv9yORphne 0NUw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:content-transfer-encoding; bh=vDxr7MTqqKSUfCFqjA+zNhlH9fpaOeP/gTRYU5+BByc=; b=ti0I1TLrc+rnzYCLT7X1WDY9jYlIpP4PdyENjL7a4PPNkp0jwnLhCrqhZeaE5cDb92 kpZQzOc7FPCCLq96WGchUCkuX99wMXlWDycnUM78YlGYhX1gq9F06Arbm/4aGdqcgnJO Ixs/KJ3IPRJMlVL85FaLb4OjEIazzJ1StKiNWIH195rZfBh4UUNgulSV4thB0w6Tlp8V 2C7I3YNtV6uuURbBxqvZxbXx2ui3acVMy10EL8zqK4SEe3sAWIbZINqFRBYvsVK679UY KbnhXBeUmPHbJNUnpjzIq+D4ZwLVuboSGMRm7rPCpP0TwgME5WGhlvG7AhX+FxzYMmd8 MPWQ== X-Gm-Message-State: AOAM532UIo5jIRUcXhRQWDaCUHHxpYlbTDpFUVk3peC+8orKvY+aLbBZ p4vdMURmTF94wJNNCl6unFELe1MXBkbQlg8y3mlIygNMWgo= X-Received: by 2002:a92:6a02:: with SMTP id f2mr7976160ilc.19.1630975479973; Mon, 06 Sep 2021 17:44:39 -0700 (PDT) MIME-Version: 1.0 References: <20210906012559.8605-1-baptiste.lepers@gmail.com> <20210906122747.GG3379@suse.cz> In-Reply-To: From: Baptiste Lepers Date: Tue, 7 Sep 2021 10:44:17 +1000 Message-ID: Subject: Re: [PATCH] btrfs: transaction: Fix misplaced barrier in btrfs_record_root_in_trans To: dsterba@suse.cz, Baptiste Lepers , "Paul E . McKenney" , Chris Mason , Josef Bacik , David Sterba , linux-btrfs@vger.kernel.org, linux-kernel@vger.kernel.org Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org No, they need to be between the reads to have an effect. See https://www.kernel.org/doc/Documentation/memory-barriers.txt =C2=A7SMP BARRIER PAIRING ("When dealing with CPU-CPU interactions..."). You will see that the barriers are always between the ordered reads and not before. I think that Paul, the barrier guru, can confirm that the barrier was misplaced in the original code? :) On Tue, Sep 7, 2021 at 10:43 AM Baptiste Lepers wrote: > > > > On Mon, Sep 6, 2021 at 10:27 PM David Sterba wrote: >> >> On Mon, Sep 06, 2021 at 11:25:59AM +1000, Baptiste Lepers wrote: >> > Per comment, record_root_in_trans orders the writes of the root->state >> > and root->last_trans: >> > set_bit(BTRFS_ROOT_IN_TRANS_SETUP, &root->state); >> > smp_wmb(); >> > root->last_trans =3D trans->transid; >> > >> > But the barrier that enforces the order on the read side is misplaced: >> > smp_rmb(); <-- misplaced >> > if (root->last_trans =3D=3D trans->transid && >> > <-- missing barrier here --> >> > !test_bit(BTRFS_ROOT_IN_TRANS_SETUP, &root->state)) >> > >> > This patches fixes the ordering and wraps the racy accesses with >> > READ_ONCE and WRITE_ONCE calls to avoid load/store tearing. >> > >> > Fixes: 7585717f304f5 ("Btrfs: fix relocation races") >> > Signed-off-by: Baptiste Lepers >> > --- >> > fs/btrfs/transaction.c | 7 ++++--- >> > 1 file changed, 4 insertions(+), 3 deletions(-) >> > >> > diff --git a/fs/btrfs/transaction.c b/fs/btrfs/transaction.c >> > index 14b9fdc8aaa9..a609222e6704 100644 >> > --- a/fs/btrfs/transaction.c >> > +++ b/fs/btrfs/transaction.c >> > @@ -437,7 +437,7 @@ static int record_root_in_trans(struct btrfs_trans= _handle *trans, >> > (unsigned long)root->root_key.objecti= d, >> > BTRFS_ROOT_TRANS_TAG); >> > spin_unlock(&fs_info->fs_roots_radix_lock); >> > - root->last_trans =3D trans->transid; >> > + WRITE_ONCE(root->last_trans, trans->transid); >> > >> > /* this is pretty tricky. We don't want to >> > * take the relocation lock in btrfs_record_root_in_tran= s >> > @@ -489,7 +489,7 @@ int btrfs_record_root_in_trans(struct btrfs_trans_= handle *trans, >> > struct btrfs_root *root) >> > { >> > struct btrfs_fs_info *fs_info =3D root->fs_info; >> > - int ret; >> > + int ret, last_trans; >> > >> > if (!test_bit(BTRFS_ROOT_SHAREABLE, &root->state)) >> > return 0; >> > @@ -498,8 +498,9 @@ int btrfs_record_root_in_trans(struct btrfs_trans_= handle *trans, >> > * see record_root_in_trans for comments about IN_TRANS_SETUP us= age >> > * and barriers >> > */ >> > + last_trans =3D READ_ONCE(root->last_trans); >> > smp_rmb(); >> > - if (root->last_trans =3D=3D trans->transid && >> > + if (last_trans =3D=3D trans->transid && >> > !test_bit(BTRFS_ROOT_IN_TRANS_SETUP, &root->state)) >> >> Aren't the smp_rmb barriers supposed to be used before the condition? > > > No, they need to be between the reads to have an effect. See https://www= .kernel.org/doc/Documentation/memory-barriers.txt =C2=A7SMP BARRIER PAIRING= ("When dealing with CPU-CPU interactions..."). You will see that the barri= ers are always between the ordered reads and not before. > > I think that Paul, the barrier guru, can confirm that the barrier was mis= placed in the original code? :)