Received: by 2002:a05:7412:6592:b0:d7:7d3a:4fe2 with SMTP id m18csp1725588rdg; Sat, 12 Aug 2023 13:39:28 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHLFvM/27sS3SIq54gLCrG0tLLSIMHA5qAQOMqXh8nSLGO0AoT8wO5bAl1EE2UFBJFED1Zl X-Received: by 2002:a17:90b:3d2:b0:25b:c454:a366 with SMTP id go18-20020a17090b03d200b0025bc454a366mr4897410pjb.5.1691872768247; Sat, 12 Aug 2023 13:39:28 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1691872768; cv=none; d=google.com; s=arc-20160816; b=a9lzS7QZVWD7eJ9QuMNRFqjy6brYrLI58ivEAsDvlBy4/NdgqM2ekw7hLgV58rO6AL MPCsjzUAutYUOFLEgq/weeyKn7I86G92tW8aUPPLc/r+XfNGL6wEoUUcT1cbt9su/TTx QlvugOCKffUfx2xA1EZhIKb7w3SUUjD0czcddViSkWjp8Vvh6/aTxiyC9AsiQLKmq3o8 Jstt9n7/kAbKM8ZeEjkO3qDNlZrt3JBzA/xJ6U97uSQVhbyRTHN6QyQOGjE+OuUg9o6m KQEzlULzDosKjCfMyK8iXIV2NHNxsFYicz4lgO1xt23dGVyYs2zZbeL4MhC0Jf9nRnVV pBQA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date:feedback-id :dkim-signature; bh=C8deft9GhCjrCi3zrUeTNCRn20vtzxZ38zbwkbMnefc=; fh=npa2acil34umFe1qpcDgCKgPye6Lm7sq8xq06X+F2SE=; b=ft0tsY/8CYReb6+j2iDqKU9FiTNYFgIJOixJHJdaE3MsdOmiTio2RiC4uZyAv1M12Q SyUtSow3YeC9EPu9VOnpyCBIzvPEUkk0FbfeJyLl8hgHyRELfOmok6LhZTlN+I+jF4EW MUjc1HOyCzX9x+Ta/Aea9n0pFwb/wmOKqlhwtjmZT8mzUnmZArEyr9DBijNdkDh1lnfD 5R/8cuD9GlIJKuhWN0LhzbgRNSbj+quDxrnLJLMnhREY5MqarKo/a5/zQIGbBLI74Lju w80W8ZgNr9sOorZhZsdPX8hDxhwy4pOfXGP0rYw6tRSFaAKztLqd0tt8O5vH2uby8ixS OumQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20221208 header.b=b10+PhSV; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 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 out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id co20-20020a17090afe9400b002534f4ce2b6si7358364pjb.125.2023.08.12.13.39.16; Sat, 12 Aug 2023 13:39:28 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20221208 header.b=b10+PhSV; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 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 S229493AbjHLUAD (ORCPT + 99 others); Sat, 12 Aug 2023 16:00:03 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38216 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229506AbjHLUAB (ORCPT ); Sat, 12 Aug 2023 16:00:01 -0400 Received: from mail-ej1-x629.google.com (mail-ej1-x629.google.com [IPv6:2a00:1450:4864:20::629]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C24001BD8; Sat, 12 Aug 2023 12:59:41 -0700 (PDT) Received: by mail-ej1-x629.google.com with SMTP id a640c23a62f3a-99c1d03e124so383653066b.2; Sat, 12 Aug 2023 12:59:41 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1691870328; x=1692475128; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:feedback-id:from:to:cc:subject:date :message-id:reply-to; bh=C8deft9GhCjrCi3zrUeTNCRn20vtzxZ38zbwkbMnefc=; b=b10+PhSV3gqBnzmaMHOdepoom+EKbw87uP8YKkY/qthU1lZLZ4F3tu1Es8ljYCQnVI bwF1NSKKoKQMEKBRhDzCuX8mPim/73rI0bvBbp+QDDaiTIzMEauE5CVJw0vyZ6GhCHMW elDVxviJSrM+nR8oz5RmgChMigqS+a4CeafHW4kFBlvRk/kAhgaCnELL8IguO2gZnbHG 1fqxcQCiEh5TRlI9wrpzBZfYnwCuVu/k8XcYfmBHbzqHoQtszwPzncKBr6ahK2gy7O3q 6klVNO8Xuk/0ka6zfE/Qcf7vHFfWKtfC7P2RzNelRIKsO3FZelqzl39GwYjHOiJsTN9c mnHA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1691870328; x=1692475128; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:feedback-id:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=C8deft9GhCjrCi3zrUeTNCRn20vtzxZ38zbwkbMnefc=; b=N1AlDXzfmEMVAKbaoDDp/Y8ErQmL484e1ecRyaqqFu+W+OA5WtXbx8omyWPTyd5jdb 3sgh5xbM674grolD8VfONr2P5qu+gPijFGXgWyhQHMHwsqyTOcPm1tbQ9DXlY8j4Ovb2 UyshhHexLdQGRFl0qv6atD/x6LAyH4ggekxs5a4V04Ly0frOsvWCHJ3YbKdwPXBm6nCg Mf0vM5NcO/Hvf0xW9fqpXCZ/2gkSzx0XWQzaQ2rc9GMKab6Hqmdt2abhXyJXJnZaGLNJ kIGg1qVWebVUf1c7J5S5Y4waSpgL4uSOgl/+X0BYDOPi1tbgB4AZENXDvLLTWuqaGtj0 szpQ== X-Gm-Message-State: AOJu0YwlScg2vkASmuT5oeGdalIpq3yl7dYAd5Cm9Quh9Cq2/ZlBL1pd Y8CD1QdfXnPWd2pkQzncJxI= X-Received: by 2002:a17:906:314e:b0:99b:627f:9c0d with SMTP id e14-20020a170906314e00b0099b627f9c0dmr4038716eje.27.1691870327675; Sat, 12 Aug 2023 12:58:47 -0700 (PDT) Received: from auth2-smtp.messagingengine.com (auth2-smtp.messagingengine.com. [66.111.4.228]) by smtp.gmail.com with ESMTPSA id os5-20020a170906af6500b0098e34446464sm3793857ejb.25.2023.08.12.12.58.46 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 12 Aug 2023 12:58:46 -0700 (PDT) Received: from compute1.internal (compute1.nyi.internal [10.202.2.41]) by mailauth.nyi.internal (Postfix) with ESMTP id 8052C27C005B; Sat, 12 Aug 2023 15:58:45 -0400 (EDT) Received: from mailfrontend1 ([10.202.2.162]) by compute1.internal (MEProxy); Sat, 12 Aug 2023 15:58:45 -0400 X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedviedruddttddgudegudcutefuodetggdotefrod ftvfcurfhrohhfihhlvgemucfhrghsthforghilhdpqfgfvfdpuffrtefokffrpgfnqfgh necuuegrihhlohhuthemuceftddtnecusecvtfgvtghiphhivghnthhsucdlqddutddtmd enucfjughrpeffhffvvefukfhfgggtuggjsehttdertddttddvnecuhfhrohhmpeeuohhq uhhnucfhvghnghcuoegsohhquhhnrdhfvghnghesghhmrghilhdrtghomheqnecuggftrf grthhtvghrnhephedugfduffffteeutddvheeuveelvdfhleelieevtdeguefhgeeuveei udffiedvnecuvehluhhsthgvrhfuihiivgeptdenucfrrghrrghmpehmrghilhhfrhhomh epsghoqhhunhdomhgvshhmthhprghuthhhphgvrhhsohhnrghlihhthidqieelvdeghedt ieegqddujeejkeehheehvddqsghoqhhunhdrfhgvnhhgpeepghhmrghilhdrtghomhesfh higihmvgdrnhgrmhgv X-ME-Proxy: Feedback-ID: iad51458e:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Sat, 12 Aug 2023 15:58:44 -0400 (EDT) Date: Sat, 12 Aug 2023 12:58:34 -0700 From: Boqun Feng To: Kent Overstreet Cc: Linus Torvalds , linux-bcachefs@vger.kernel.org, linux-kernel@vger.kernel.org Subject: Re: [PATCH] bcachefs: six locks: Fix missing barrier on wait->lock_acquired Message-ID: References: <20230812192720.2703874-1-kent.overstreet@linux.dev> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20230812192720.2703874-1-kent.overstreet@linux.dev> X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM, RCVD_IN_DNSWL_BLOCKED,SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Sat, Aug 12, 2023 at 03:27:20PM -0400, Kent Overstreet wrote: > Six locks do lock handoff via the wakeup path: the thread doing the > wakeup also takes the lock on behalf of the waiter, which means the > waiter only has to look at its waitlist entry, and doesn't have to touch > the lock cacheline while another thread is using it. > > Linus noticed that this needs a real barrier, which this patch fixes. > > Also add a comment for the should_sleep_fn() error path. > > Signed-off-by: Kent Overstreet > Cc: Linus Torvalds > Cc: Boqun Feng > Cc: linux-bcachefs@vger.kernel.org > Cc: linux-kernel@vger.kernel.org > --- > fs/bcachefs/six.c | 33 +++++++++++++++++++++++++-------- > 1 file changed, 25 insertions(+), 8 deletions(-) > > diff --git a/fs/bcachefs/six.c b/fs/bcachefs/six.c > index 581aee565e..b6ca53c852 100644 > --- a/fs/bcachefs/six.c > +++ b/fs/bcachefs/six.c > @@ -223,14 +223,16 @@ static void __six_lock_wakeup(struct six_lock *lock, enum six_lock_type lock_typ > if (ret <= 0) > goto unlock; > > - __list_del(w->list.prev, w->list.next); > task = w->task; > + __list_del(w->list.prev, w->list.next); > /* > - * Do no writes to @w besides setting lock_acquired - otherwise > - * we would need a memory barrier: > + * The release barrier here ensures the ordering of the > + * __list_del before setting w->lock_acquired; @w is on the > + * stack of the thread doing the waiting and will be reused > + * after it sees w->lock_acquired with no other locking: > + * pairs with smp_load_acquire() in six_lock_slowpath() > */ > - barrier(); > - w->lock_acquired = true; > + smp_store_release(&w->lock_acquired, true); > wake_up_process(task); > } > > @@ -502,17 +504,32 @@ static int six_lock_slowpath(struct six_lock *lock, enum six_lock_type type, > while (1) { > set_current_state(TASK_UNINTERRUPTIBLE); > > - if (wait->lock_acquired) > + /* > + * Ensures that writes to the waitlist entry happen after we see Maybe my English, but "happen after" here is a little confusing: writes happen after the read of ->lock_acquired? How about /* * Ensures once we observe the write to * wait->lock_acquired, we must observe the writes to * the waitlist entry: pairs with smp_store_release in * __six_lock_wakeup */ ? I haven't finished my review on the SIX lock, but this patch looks good to me, feel free to add: Reviewed-by: Boqun Feng Regards, Boqun > + * wait->lock_acquired: pairs with the smp_store_release in > + * __six_lock_wakeup > + */ > + if (smp_load_acquire(&wait->lock_acquired)) > break; > > ret = should_sleep_fn ? should_sleep_fn(lock, p) : 0; > if (unlikely(ret)) { > + bool acquired; > + > + /* > + * If should_sleep_fn() returns an error, we are > + * required to return that error even if we already > + * acquired the lock - should_sleep_fn() might have > + * modified external state (e.g. when the deadlock cycle > + * detector in bcachefs issued a transaction restart) > + */ > raw_spin_lock(&lock->wait_lock); > - if (!wait->lock_acquired) > + acquired = wait->lock_acquired; > + if (!acquired) > list_del(&wait->list); > raw_spin_unlock(&lock->wait_lock); > > - if (unlikely(wait->lock_acquired)) > + if (unlikely(acquired)) > do_six_unlock_type(lock, type); > break; > } > -- > 2.40.1 >