Received: by 2002:a05:6358:3188:b0:123:57c1:9b43 with SMTP id q8csp8467738rwd; Tue, 20 Jun 2023 15:56:01 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ7a3N5faVcG6L4qHy0ajvRjQe8ttOQl/u8xVMFSUaust3d8Cl2L7Er/YhOMa5AI0Co9Ufu2 X-Received: by 2002:a05:6a20:a305:b0:11f:983f:df06 with SMTP id x5-20020a056a20a30500b0011f983fdf06mr9419362pzk.15.1687301761102; Tue, 20 Jun 2023 15:56:01 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1687301761; cv=none; d=google.com; s=arc-20160816; b=Bmda14bSKIg3UJmfWxS6RtWMkrN7ouHPMxoiGqquKRR4B4cBA0dWJSa6jW8fBF4NPw gbndDlsU+Z16LPlDAOgCwlZWOCB/m/MrEIgofQgscKCMV5aekatmnG6n8+F2xiFsXDTm 1qzOTPC6UUUNhpT7sbiMhUWaBo0t1eKGy5mKqJaFdbh1cnN+zL5LJpt2Kkj0DgjQX3My rvycZ4zdm+eYVw5+s6EBo/5ZnsC3IrXt6+mUsr9hun26NbBg5mCIcUvG471R4bMrrVTX gZiFs9brhPcyUAuayKWMq1xX8Gu1c5kQHuh6StXsBlUW/34eHDydWFAlMq3puoVmN7ax FZwg== 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:reply-to:message-id:subject:cc:to:from:date :dkim-signature; bh=Xvp6WoUk7Mj5y1rLG+PrCYzZJWRCYA0fHrrcXul6vBQ=; b=JtZrSN92e6qYxkUyyidj6aRix1thngTLmLtl/YWwKz6SAfpsIRyd7jj8l2eCgvZibB O8GDxojiCBzV5m9THX/k6y85nE4wBiSTU0QG2ilwa89YQZlwV6reGi5WYpZgYSUFkPJh LKzgjHvOBpNksdtnF5e5RSeOHHMdCU4GldiWHI53yb7v7/wwe3dbrcvQt9bgj0HBwliK v9QiMIfs0rwiyvZgheNlrnqjSFKutLjD/rsfxiOyO7+EfyrY1nquSxS8yQMctRuKPWED 0JdaB0ujCSk7MkVa3mRAamGY02c8O8Px1QK0vZZXKNHtNwCwkzhTiSRiiz1PEUtbDUK9 gKAQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=B3M56n8f; 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=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id y26-20020aa79afa000000b006687406605asi2401343pfp.211.2023.06.20.15.55.47; Tue, 20 Jun 2023 15:56:01 -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=@kernel.org header.s=k20201202 header.b=B3M56n8f; 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=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229961AbjFTW0s (ORCPT + 99 others); Tue, 20 Jun 2023 18:26:48 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40778 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229629AbjFTW0r (ORCPT ); Tue, 20 Jun 2023 18:26:47 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 739121735; Tue, 20 Jun 2023 15:26:46 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id 00DEA61291; Tue, 20 Jun 2023 22:26:46 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 5DF9BC433C0; Tue, 20 Jun 2023 22:26:45 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1687300005; bh=MCSUz/MAQT4r5cMvV8x8YY+jmIPX87xmV2kPzuDjlpg=; h=Date:From:To:Cc:Subject:Reply-To:References:In-Reply-To:From; b=B3M56n8f6vGBjTVCCPyYYm0G7wujWstKyfAKiGUE4pBBIOTpqlFx3AuEPXSsn+Aif raIznNSt2CXbCCNI0+ntn4m5k36dH8L+lkgISR4EEF/mzyxhtJaqgjsC+RZNHn99qD +Ku1EXgl5T8aJ10vNBIRFQBFgbnQx2Gq7eSiJOcupOCoWDm4K8p4+VJcdV17O/LCUk UTXuYboozzdUkTVkzLcgTZU5jpd93Tu0psReMwyqfYU2ia7wzpoCi5Fv/CbNb5nPZc pycNnx28ydtXEt23XI+e7oksux3a74EbX+0Fn2DTVnp4ky86HBggyuej9p5Xt8149h Tc2exysc5l3Fg== Received: by paulmck-ThinkPad-P17-Gen-1.home (Postfix, from userid 1000) id ED053CE1CC5; Tue, 20 Jun 2023 15:26:44 -0700 (PDT) Date: Tue, 20 Jun 2023 15:26:44 -0700 From: "Paul E. McKenney" To: Alan Huang Cc: rcu@vger.kernel.org, linux-kernel@vger.kernel.org Subject: Re: [PATCH v2] rcu: Add necessary WRITE_ONCE() Message-ID: <50c4aa37-388b-449c-8184-00a9d69471fc@paulmck-laptop> Reply-To: paulmck@kernel.org References: <20230620171346.207076-1-mmpgouride@gmail.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20230620171346.207076-1-mmpgouride@gmail.com> X-Spam-Status: No, score=-7.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_HI, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE 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 Tue, Jun 20, 2023 at 05:13:46PM +0000, Alan Huang wrote: > Commit c54a2744497d("list: Add hlist_unhashed_lockless()") and > commit 860c8802ace1("rcu: Use WRITE_ONCE() for assignments to > ->pprev for hlist_nulls") added various WRITE_ONCE() to pair with > the READ_ONCE() in hlist_unhashed_lockless(), but there are still > some places where WRITE_ONCE() was not added, this commit adds that. > > Also add WRITE_ONCE() to pair with the READ_ONCE() in hlist_empty(). > > Signed-off-by: Alan Huang On hlist_nulls_add_tail_rcu(), good catch, thank you! On the others, are there really cases where a lockless read races with the update? At first glance, that sounds like a usage bug. For example, as I understand it, when you use something like hlist_del(), you are supposed to ensure that there are no concurrent readers. Which is the point of the assignment of the special value LIST_POISON2, right? Or is there some use case that I am missing? If I am not missing something, then switching the non-RCU APIs to WRITE_ONCE() would be a step backwards, because it would make it harder for tools like KCSAN to find bugs. Thanx, Paul > --- > Changelog: > V1 -> V2: > Add WRITE_ONCE in hlist_del_init to pair with READ_ONCE in > hlist_unhashed_lockless. > > include/linux/list.h | 9 +++++---- > include/linux/list_nulls.h | 2 +- > include/linux/rculist_nulls.h | 2 +- > 3 files changed, 7 insertions(+), 6 deletions(-) > > diff --git a/include/linux/list.h b/include/linux/list.h > index ac366958ea..3a29b95bfe 100644 > --- a/include/linux/list.h > +++ b/include/linux/list.h > @@ -912,7 +912,7 @@ static inline void hlist_del(struct hlist_node *n) > { > __hlist_del(n); > n->next = LIST_POISON1; > - n->pprev = LIST_POISON2; > + WRITE_ONCE(n->pprev, LIST_POISON2); > } > > /** > @@ -925,7 +925,8 @@ static inline void hlist_del_init(struct hlist_node *n) > { > if (!hlist_unhashed(n)) { > __hlist_del(n); > - INIT_HLIST_NODE(n); > + n->next = NULL; > + WRITE_ONCE(n->pprev, NULL); > } > } > > @@ -1026,8 +1027,8 @@ static inline void hlist_move_list(struct hlist_head *old, > { > new->first = old->first; > if (new->first) > - new->first->pprev = &new->first; > - old->first = NULL; > + WRITE_ONCE(new->first->pprev, &new->first); > + WRITE_ONCE(old->first, NULL); > } > > #define hlist_entry(ptr, type, member) container_of(ptr,type,member) > diff --git a/include/linux/list_nulls.h b/include/linux/list_nulls.h > index fa6e8471bd..b63b0589fa 100644 > --- a/include/linux/list_nulls.h > +++ b/include/linux/list_nulls.h > @@ -95,7 +95,7 @@ static inline void hlist_nulls_add_head(struct hlist_nulls_node *n, > > n->next = first; > WRITE_ONCE(n->pprev, &h->first); > - h->first = n; > + WRITE_ONCE(h->first, n); > if (!is_a_nulls(first)) > WRITE_ONCE(first->pprev, &n->next); > } > diff --git a/include/linux/rculist_nulls.h b/include/linux/rculist_nulls.h > index ba4c00dd80..c65121655b 100644 > --- a/include/linux/rculist_nulls.h > +++ b/include/linux/rculist_nulls.h > @@ -138,7 +138,7 @@ static inline void hlist_nulls_add_tail_rcu(struct hlist_nulls_node *n, > > if (last) { > n->next = last->next; > - n->pprev = &last->next; > + WRITE_ONCE(n->pprev, &last->next); > rcu_assign_pointer(hlist_nulls_next_rcu(last), n); > } else { > hlist_nulls_add_head_rcu(n, h); > -- > 2.34.1 >