Received: by 2002:a05:6a10:1287:0:0:0:0 with SMTP id d7csp13896pxv; Wed, 21 Jul 2021 14:07:07 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyPdavYP5bYF6L50zy2Mbkg51WF+nmZf5dw/qzfKebFbc9VPnpJKyYhq+nSfORO7fIMb++z X-Received: by 2002:a02:956a:: with SMTP id y97mr32383714jah.58.1626901534167; Wed, 21 Jul 2021 14:05:34 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1626901534; cv=none; d=google.com; s=arc-20160816; b=FLtF5JfyzfwO7+TzAT3fxx2O7ClIwGhPoybLF13pyKAs1TIftaDrx+je/G8CysEX8j 97EVSY4hEE/T7g5RiQWexaiWgzqoS9LdxGOHwAmBiMaPloC2jZZJMgvDu28BSInucuEV hBNAtV01uADp1Izf7bniNgktQEK4HDXuzB0yZHktNjKGLB/veu8ZdGLBi9tGuX0tXnXX ykrgOlSLL2WhTcn23LQfozihjGlRGq26PNG62KCu2hBdrP/+AowS6B54QELDwpTgaWqm oXQ59rNF3U0T9GK6fUTtgdww2X+iiRQufSCsCsByt/srhLl0HV7M7HbAcGGxA34QUHki FlCg== 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=jUbG+9sdMs63YgbaeeeSYzbtSxSUYer1TeDcRLcq+EM=; b=G0H11vQ9miMHMmNmHoGdFSQQxtE4+NZCNxrTmbIyBWzaJohgy+k4yjRF51i9PcdaDo yYB7+Vem8+cj4prJC7jAcgjDlXjcLhwnR4PUIAux8aTsX7dhETVH5Y0Dg+0pqAc6HUCM 12y4HdOEJzAqKwW49rlwmkk/Q7lo6WsSOoHjOXaJE50MNjnW7QfHvnOFz17GKIS1sD7K jHXy/gjzp/+auuls9/PN7RWs67W1zvR1zAWhcJ4Cbl02umMLysWNINppzEDBDmX5KdqQ C6/1qzyFqIOi43M2KdVbcKgTD/HFpfNvmQRMgS05PLwo+Uz4/1mUb8EHeu4NvrUOuyUv fN1w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=MBLKLMsL; 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 r23si9835025iot.18.2021.07.21.14.05.22; Wed, 21 Jul 2021 14:05:34 -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=MBLKLMsL; 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 S240467AbhGUTlN (ORCPT + 99 others); Wed, 21 Jul 2021 15:41:13 -0400 Received: from mail.kernel.org ([198.145.29.99]:50114 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S237548AbhGUTkx (ORCPT ); Wed, 21 Jul 2021 15:40:53 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id A691A61355; Wed, 21 Jul 2021 20:21:29 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1626898889; bh=wTmbqBY9AqPvVPQt3asLMeVulJpVoLicw5TeHOv8ij8=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=MBLKLMsLTHOEmPJeBMobuXM0/6i2rpN3SW3IX7B8C8b4OsdvSMmR9uVJbVfE/+JzH EWDlrwUTWhnAVr2p8NVxarsssmiJ8Y7a7LeylsgAPZvzxHdFlBk9Hp6uN22iFQYQWj NkJ9Erj9D+/+eLyznmWeHxxl8sa8w7hOUWh/bEEYKEtKhiWlwiucRk0sCTvz6ddSSb tXoTi36UtD75miFtkFMDqq0qAmjISgiE/8pOR5g8adXcSKcK4t2vioOUFy3sTtO4Eb TkD4xgSPJA6rCyuKl/42fizJe9SmqlCKrPEG8wx1y5ecMlEKhlCdC5kD3ISCYV2hq8 4L+ur6ZYmAjKQ== Received: by paulmck-ThinkPad-P17-Gen-1.home (Postfix, from userid 1000) id 1D87B5C0017; Wed, 21 Jul 2021 13:21:29 -0700 (PDT) From: "Paul E. McKenney" To: rcu@vger.kernel.org Cc: linux-kernel@vger.kernel.org, kernel-team@fb.com, mingo@kernel.org, jiangshanlai@gmail.com, akpm@linux-foundation.org, mathieu.desnoyers@efficios.com, josh@joshtriplett.org, tglx@linutronix.de, peterz@infradead.org, rostedt@goodmis.org, dhowells@redhat.com, edumazet@google.com, fweisbec@gmail.com, oleg@redhat.com, joel@joelfernandes.org, Julian Wiedmann , "Paul E . McKenney" Subject: [PATCH rcu 06/18] rculist: Unify documentation about missing list_empty_rcu() Date: Wed, 21 Jul 2021 13:21:14 -0700 Message-Id: <20210721202127.2129660-6-paulmck@kernel.org> X-Mailer: git-send-email 2.31.1.189.g2e36527f23 In-Reply-To: <20210721202042.GA1472052@paulmck-ThinkPad-P17-Gen-1> References: <20210721202042.GA1472052@paulmck-ThinkPad-P17-Gen-1> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Julian Wiedmann We have two separate sections that talk about why list_empty_rcu() is not needed, so this commit consolidates them. Signed-off-by: Julian Wiedmann [ paulmck: The usual wordsmithing. ] Signed-off-by: Paul E. McKenney --- include/linux/rculist.h | 35 +++++++++++++++++------------------ 1 file changed, 17 insertions(+), 18 deletions(-) diff --git a/include/linux/rculist.h b/include/linux/rculist.h index f8633d37e3581..d29740be4833e 100644 --- a/include/linux/rculist.h +++ b/include/linux/rculist.h @@ -10,15 +10,6 @@ #include #include -/* - * Why is there no list_empty_rcu()? Because list_empty() serves this - * purpose. The list_empty() function fetches the RCU-protected pointer - * and compares it to the address of the list head, but neither dereferences - * this pointer itself nor provides this pointer to the caller. Therefore, - * it is not necessary to use rcu_dereference(), so that list_empty() can - * be used anywhere you would want to use a list_empty_rcu(). - */ - /* * INIT_LIST_HEAD_RCU - Initialize a list_head visible to RCU readers * @list: list to be initialized @@ -318,21 +309,29 @@ static inline void list_splice_tail_init_rcu(struct list_head *list, /* * Where are list_empty_rcu() and list_first_entry_rcu()? * - * Implementing those functions following their counterparts list_empty() and - * list_first_entry() is not advisable because they lead to subtle race - * conditions as the following snippet shows: + * They do not exist because they would lead to subtle race conditions: * * if (!list_empty_rcu(mylist)) { * struct foo *bar = list_first_entry_rcu(mylist, struct foo, list_member); * do_something(bar); * } * - * The list may not be empty when list_empty_rcu checks it, but it may be when - * list_first_entry_rcu rereads the ->next pointer. - * - * Rereading the ->next pointer is not a problem for list_empty() and - * list_first_entry() because they would be protected by a lock that blocks - * writers. + * The list might be non-empty when list_empty_rcu() checks it, but it + * might have become empty by the time that list_first_entry_rcu() rereads + * the ->next pointer, which would result in a SEGV. + * + * When not using RCU, it is OK for list_first_entry() to re-read that + * pointer because both functions should be protected by some lock that + * blocks writers. + * + * When using RCU, list_empty() uses READ_ONCE() to fetch the + * RCU-protected ->next pointer and then compares it to the address of the + * list head. However, it neither dereferences this pointer nor provides + * this pointer to its caller. Thus, READ_ONCE() suffices (that is, + * rcu_dereference() is not needed), which means that list_empty() can be + * used anywhere you would want to use list_empty_rcu(). Just don't + * expect anything useful to happen if you do a subsequent lockless + * call to list_first_entry_rcu()!!! * * See list_first_or_null_rcu for an alternative. */ -- 2.31.1.189.g2e36527f23