Received: by 2002:a05:6a11:4021:0:0:0:0 with SMTP id ky33csp1040238pxb; Wed, 15 Sep 2021 20:48:09 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwrzk+A95Sa2EyMr2nkpHIGTnqvOaDP7G4sWsJNvbAJFt+/U4n+aRQnUCnF8oq3axCs3L7L X-Received: by 2002:a50:d581:: with SMTP id v1mr4022201edi.210.1631764089544; Wed, 15 Sep 2021 20:48:09 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1631764089; cv=none; d=google.com; s=arc-20160816; b=BqqMusDEeQ9G0L8eUEqnVYPTsRzkQjLy+ea0NbAWSt6lQvJq1EWt6Xy3cKmqtizZfI /u0DpTGuf+LMJjB3J0drCmJ5REAs0dWfcHsELEbBH0GnUqDFHAw/kF7ywwt3IocXcVab TS+syaxwQF1I+B99Ow/Tp/s2k9SsqRQnWsiDn0EvNdCmXnZIEOUMPP34ddOonQr0xGhp BdPLwKvMZv4Mftg8Kl/C53bJHPg/vy/v2DWh4m5Is9V1qZ+RBnvV+3UMTyjoFyaGqdYZ h8jHjpTEKXC8oKHkvI7tW2m1YachD2eA69oM5G5suZLPqBlT1DyQq+s9h/OtKBkcz6gH 1xzg== 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=PandIsADvqsUupHjnSGXxhHD/qBHusaqiIcrYmYtH/4=; b=m4wolVpgreC7/iE9RbrcU6OmOU1q3lrRv3VbebI99cj+dmW8nqVJdlKJTXixunziSo V9Wdkoz0sZUPYkSdia4MOnS424RADa8+E4A4k1LfGFzqwrriLxv74av/QxnTuaDPa1aV Nw7DDBIIA2mS9IrHgUEnzIxD3pzST5laiEYKsyKCcPmcNkXfm4DpHJq2we9c8Ya5GytT CHxpaL3RfWH7zpCyvyF8+Wa4WMfwniaOQYZRNdEPjCuO/yrdY0VITmBbeuEgHrFes8jG 6bR0jkKHKpERhRD6D/MA6QurNbDtsjDly9TybJq/hyYwEx4JqQkiEuFhDfYL8VqZ+mwc YPsg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=P1vsn6if; 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 a93si2127782edf.462.2021.09.15.20.47.45; Wed, 15 Sep 2021 20:48:09 -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=P1vsn6if; 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 S234201AbhIPDqs (ORCPT + 99 others); Wed, 15 Sep 2021 23:46:48 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42520 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233856AbhIPDqr (ORCPT ); Wed, 15 Sep 2021 23:46:47 -0400 Received: from mail-io1-xd30.google.com (mail-io1-xd30.google.com [IPv6:2607:f8b0:4864:20::d30]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id DBC82C061574; Wed, 15 Sep 2021 20:45:27 -0700 (PDT) Received: by mail-io1-xd30.google.com with SMTP id a22so6207730iok.12; Wed, 15 Sep 2021 20:45:27 -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=PandIsADvqsUupHjnSGXxhHD/qBHusaqiIcrYmYtH/4=; b=P1vsn6ifdxH7RpFXkSK6CUHK9RFHfhmfm8/RZLjtc+Cw+iI5Dff46aXEZHD+9GHIXA ipNW+YJpUBOPigSQ+OaWKyAPkt3aPZOex41CPL0d7rxDT0WvniHD5Qg+O20p+lh3fJOf o0pXeDQXY+cdJlaKrBltTP/A1vWgqaKjufmaxZxk0rLrNNwJErwETImQ+1m9ulWbU00Q je2oWDAHAY327lq6MhnqyrJ+33jwwDLX/RFxMvBjppyzDKuV/i//5+ahXtSMJsVHgB03 4eFyfpwMADNu8lgK8lpm9yKnDUz60aaZY2qiPfXX9O2tM3qXq7KDkhibBJ35ht/eiath QSBQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:content-transfer-encoding; bh=PandIsADvqsUupHjnSGXxhHD/qBHusaqiIcrYmYtH/4=; b=4Mrok4obJHWLpP7YCKdoUPgY7VWiwtBX43dBqGvioQTsOm8tK6VtjxceiPvnHehSrG O2tJKM1bbEs8Xq4Rhl5u8N8IWBMnnaLgXMUhKk6sDxqFB7/SSWTv+XTWPzsPskNit2yD EIqdlxr1ocGp0kbkgOY3PHqlraQbjJgDXr9ylMYln3cDKt9eFxsLD+sP5zVqwYjUblDi TMxaAQoszzaHzSdPEMG6rx1V0G87P6s0NsLlWp8BAK/vYuEk0ceAt4XlYwTgAPWTncju 6wiADFsfPY85crjve3xdsA/FrOEKhKRgKHw+/daqBb/nRdM6/skNIAknfZ7f16T0o5D5 NqjQ== X-Gm-Message-State: AOAM532TanGwIuIiMBn3C/PtEjG9PcdNpgR5dW3YDNfiyccP3W+4slbu niyN0ZHR2ZKuQD7E97lxPf/x74yEoCMggRWUcZI= X-Received: by 2002:a5e:d70a:: with SMTP id v10mr2750143iom.10.1631763927102; Wed, 15 Sep 2021 20:45:27 -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: Thu, 16 Sep 2021 13:45:16 +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 Just curious about the status of this patch. :) Let me know if you need further information. Thanks! On Tue, Sep 7, 2021 at 10:44 AM Baptiste Lepers wrote: > > 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->sta= te > >> > 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 misplace= d: > >> > 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_tra= ns_handle *trans, > >> > (unsigned long)root->root_key.objec= tid, > >> > 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_tr= ans > >> > @@ -489,7 +489,7 @@ int btrfs_record_root_in_trans(struct btrfs_tran= s_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_tran= s_handle *trans, > >> > * see record_root_in_trans for comments about IN_TRANS_SETUP = usage > >> > * 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://w= ww.kernel.org/doc/Documentation/memory-barriers.txt =C2=A7SMP BARRIER PAIRI= NG ("When dealing with CPU-CPU interactions..."). You will see that the bar= riers are always between the ordered reads and not before. > > > > I think that Paul, the barrier guru, can confirm that the barrier was m= isplaced in the original code? :)