Received: by 2002:a25:8b91:0:0:0:0:0 with SMTP id j17csp871692ybl; Fri, 6 Dec 2019 07:34:57 -0800 (PST) X-Google-Smtp-Source: APXvYqzmiJsLudHAYh5AbJ/JHSazRDustDMyulclS768ugxBQZAcN/6hAlVqUIv3QPbirJx3XOwF X-Received: by 2002:aca:49d0:: with SMTP id w199mr12229076oia.14.1575646497669; Fri, 06 Dec 2019 07:34:57 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1575646497; cv=none; d=google.com; s=arc-20160816; b=Agssa0JMfd2fIXRzxpUX/LqbtlAqme3ZLBCmicYDXY+0f9l6z2yYhR7WAFJGqHKkpW 9s+QNvLooVqXkVJDsk3URGhSt6gtX/6UOYmXSJiBr08olC03ptb/m8nyXz5LxviePLxn YCx7BSayi/a97d5QxH0sz6mfiCIMeJSMv+HGsft9cZU4k5qNWRdBlnrO6sFjNUv2JK/E ze8EIt5gPp1CE2s2IgsykLF1emKdrzt2hbNwv99I0RaOGmCZ6t7JF+hhNRTbyZ5eo/Al YYbP/TYPdv/fkLeFcGV2O2/0hvM8G+mRiQjnIbIKMb8Vt2+uw9j6HWl45iZlfmQnF5BB tKjQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:user-agent:in-reply-to :content-disposition:mime-version:references:reply-to:message-id :subject:cc:to:from:date:dkim-signature; bh=NvQedABB+TEKGw7Icx262jHTIMPMNTPrsIOmCajbxl4=; b=XRHUnOkeHC+mvGDtLeDeo7Zf3H+aw0OEbbfpqfxjKzY/AP0HdAWK9hBWLYQ/wQo6E0 epOBjV1HC+Ac9m7DN+QYHKAQ1rgiLGN9e831LZ7akBXIzKdlc0viiKEA0QpBFU3jXFi2 r6ARfkJukthwtMe+L9n7ZkYPtWkO/0cPGiVMpPHBF1fPfsYmtOJKFc/jTfe70Ap22YKe ExhZzvFIhK2KnRF4CYN+rL+QYUU3baVqXXjh/UMhjTiZi52d9m0UI+HVk3axuqKCUP38 N0QZMUnrJHvXxZr4HeyJRHFUDbbGVUqFT+GQRpjs2Ll+LGjPpz96Qy1eTOeAk0gR9o6z IhjA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=s1rFLJC3; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 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. [209.132.180.67]) by mx.google.com with ESMTP id v25si6000870oth.136.2019.12.06.07.34.45; Fri, 06 Dec 2019 07:34:57 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=s1rFLJC3; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 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 S1726374AbfLFPc7 (ORCPT + 99 others); Fri, 6 Dec 2019 10:32:59 -0500 Received: from mail.kernel.org ([198.145.29.99]:42106 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726278AbfLFPc7 (ORCPT ); Fri, 6 Dec 2019 10:32:59 -0500 Received: from paulmck-ThinkPad-P72.home (50-39-105-78.bvtn.or.frontiernet.net [50.39.105.78]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 74ACC24659; Fri, 6 Dec 2019 15:32:58 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1575646378; bh=vTiMrBfut/S6BEjd18jjqG0EurY6xZg1X9AuYzIDWg0=; h=Date:From:To:Cc:Subject:Reply-To:References:In-Reply-To:From; b=s1rFLJC3rvjv9uzsleLrBv1lo0EvJRcyq15xbefAjAt5ZR31RHjZc1L+XoSVeUqCD NpVOh5ZuInhakwL0crFV/Fmu4a1KmR/eIq8Jo8bVBdmPcNOnp1TfuH+ERQavpB6cjZ Fk39bZoZ9T/Gk0YVGhISf6wNU5xICQf+dBcCrAHg= Received: by paulmck-ThinkPad-P72.home (Postfix, from userid 1000) id 4940535206AB; Fri, 6 Dec 2019 07:32:58 -0800 (PST) Date: Fri, 6 Dec 2019 07:32:58 -0800 From: "Paul E. McKenney" To: madhuparnabhowmik04@gmail.com Cc: rostedt@goodmis.org, joel@joelfernandes.org, rcu@vger.kernel.org, linux-kernel@vger.kernel.org, linux-kernel-mentees@lists.linuxfoundation.org Subject: Re: [PATCH] rculist: Add macro list_prev_rcu Message-ID: <20191206153258.GD2889@paulmck-ThinkPad-P72> Reply-To: paulmck@kernel.org References: <20191206150554.10479-1-madhuparnabhowmik04@gmail.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20191206150554.10479-1-madhuparnabhowmik04@gmail.com> User-Agent: Mutt/1.9.4 (2018-02-28) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Fri, Dec 06, 2019 at 08:35:54PM +0530, madhuparnabhowmik04@gmail.com wrote: > From: Madhuparna Bhowmik > > There are instances in the linux kernel where the prev pointer > of a list is accessed. > Unlike list_next_rcu, a similar macro for accessing the prev > pointer was not present. Interesting patch, but... You lost me on this one. The list_head ->prev pointer is not marked __rcu, so why is sparse complaining? Or is someone trying to use rcu_dereference() or similar on ->prev? If so, it is important to note that both list_del() and list_del_rcu() poision ->prev, so it is not usually safe to access ->prev within an RCU read-side critical section. At the very least, this restriction needs to be called out in the list_prev_rcu() comment header. And that use of rcu_dereference() and friends on the ->prev pointer is almost always the result of confusion, if not a bug. (Or is this some new-to-me use case?) Either way, the big question is how we are sure that the uses of ->prev that sparse is complaining about are in fact safe. More specifically, what have those use cases done to ensure that there will be no invocation of either list_del() or list_del_rcu() on the current element just before the use of ->prev? Here are a couple of possibilities: 1. The list only grows, so list_del() and list_del_rcu() are never ever invoked on it. But even this is not safe because __list_add_rcu() does smp_store_release() only on ->next. The initialization of ->prev is completely unordered with any other initialization, which can result in bugs on lookup/insertion concurrency. So this instead becomes the list being constant. 2. The ->prev pointer is never actually dereferenced, but only compared. One example use case is determining whether the current element is first in the list by comparing its ->prev pointer to the address of the list header. But this use case needs a READ_ONCE(). 3. These accesses are single-threaded, for example while the list is being initialized but before it is exposed to readers or after the list has been rendered inaccessible to readers (and following at least one grace period after that). But in this case, there is no need for rcu_dereference(), so sparse should not be complaining. 4. #3 above, but code is shared with the non-single-threaded case. But then the non-single-threaded code needs to be safe with respect to concurrent insertions and deletions, as called out above. So what am I missing here? Thanx, Paul > Therefore, directly accessing the prev pointer was causing > sparse errors. > One such example is the sparse error in fs/nfs/dir.c > > error: > fs/nfs/dir.c:2353:14: error: incompatible types in comparison expression (different address spaces): > fs/nfs/dir.c:2353:14: struct list_head [noderef] * > fs/nfs/dir.c:2353:14: struct list_head * > > The error is caused due to the following line: > > lh = rcu_dereference(nfsi->access_cache_entry_lru.prev); > > After adding the macro, this error can be fixed as follows: > > lh = rcu_dereference(list_prev_rcu(&nfsi->access_cache_entry_lru)); > > Therefore, we think there is a need to add this macro to rculist.h. > > Suggested-by: Joel Fernandes (Google) > Signed-off-by: Madhuparna Bhowmik > --- > include/linux/rculist.h | 6 ++++++ > 1 file changed, 6 insertions(+) > > diff --git a/include/linux/rculist.h b/include/linux/rculist.h > index 4b7ae1bf50b3..49eef8437753 100644 > --- a/include/linux/rculist.h > +++ b/include/linux/rculist.h > @@ -40,6 +40,12 @@ static inline void INIT_LIST_HEAD_RCU(struct list_head *list) > */ > #define list_next_rcu(list) (*((struct list_head __rcu **)(&(list)->next))) > > +/* > + * return the prev pointer of a list_head in an rcu safe > + * way, we must not access it directly > + */ > +#define list_prev_rcu(list) (*((struct list_head __rcu **)(&(list)->prev))) > + > /* > * Check during list traversal that we are within an RCU reader > */ > -- > 2.17.1 >