From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1756599Ab2DIVZO (ORCPT ); Mon, 9 Apr 2012 17:25:14 -0400 Received: from e34.co.us.ibm.com ([32.97.110.152]:60759 "EHLO e34.co.us.ibm.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1750940Ab2DIVZM (ORCPT ); Mon, 9 Apr 2012 17:25:12 -0400 Date: Mon, 9 Apr 2012 14:24:40 -0700 From: "Paul E. McKenney" To: Michel Machado Cc: Dipankar Sarma , linux-kernel@vger.kernel.org Subject: Re: [PATCH 1/1] rculist: Made list_first_entry_rcu usable Message-ID: <20120409212440.GL2430@linux.vnet.ibm.com> Reply-To: paulmck@linux.vnet.ibm.com References: <1333417354.2412.7.camel@Thor> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <1333417354.2412.7.camel@Thor> User-Agent: Mutt/1.5.21 (2010-09-15) X-Content-Scanned: Fidelis XPS MAILER x-cbid: 12040921-1780-0000-0000-000004A1A796 X-IBM-ISS-SpamDetectors: X-IBM-ISS-DetailInfo: BY=3.00000264; HX=3.00000186; KW=3.00000007; PH=3.00000001; SC=3.00000001; SDB=6.00129470; UDB=6.00030627; UTC=2012-04-09 21:25:09 Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Mon, Apr 02, 2012 at 09:42:34PM -0400, Michel Machado wrote: > The macro list_first_entry_rcu assumed that the passed list is not empty > as its counterpart list_first_entry does. However, one can test that a > list is not empty with list_empty before calling list_first_entry, > whereas neither exists list_empty_rcu, nor is advisable to add it as the > example below shows. > > Assuming that list_empty_rcu is available, one could write the following > snippet: > > if (!list_empty_rcu(mylist)) { > struct foo *bar = list_first_entry_rcu(mylist, struct foo, > list_member); > do_something(bar); > } > > The problem with this snippet is the following racing condition: 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. > > This patch cannot break any upstream code because list_first_entry_rcu > is not being used anywhere in the kernel (tested with grep(1)), and > external code that uses it is probably broken already. Hello, Michel, Interesting point! Are you intending to use list_first_entry_rcu()? If not, perhaps the best thing to do is to remove it. Thanx, Paul > Signed-off-by: Michel Machado > CC: Dipankar Sarma > CC: "Paul E. McKenney" > --- > Please CC my e-mail address while replying this message because I don't > subscribe this mailing list due to its high volume; thanks. > > diff --git a/include/linux/rculist.h b/include/linux/rculist.h > index d079290..866d3ec 100644 > --- a/include/linux/rculist.h > +++ b/include/linux/rculist.h > @@ -233,13 +233,16 @@ static inline void list_splice_init_rcu(struct > list_head *list, > * @type: the type of the struct this is embedded in. > * @member: the name of the list_struct within the struct. > * > - * Note, that list is expected to be not empty. > + * Note that if the list is empty, it returns NULL. > * > * This primitive may safely run concurrently with the _rcu > list-mutation > * primitives such as list_add_rcu() as long as it's guarded by > rcu_read_lock(). > */ > #define list_first_entry_rcu(ptr, type, member) \ > - list_entry_rcu((ptr)->next, type, member) > + ({struct list_head *__ptr = ptr; \ > + struct list_head __rcu *__next = list_next_rcu(__ptr); \ > + likely(__ptr != __next) ? container_of(__next, type, member) : NULL; > \ > + }) > > /** > * list_for_each_entry_rcu - iterate over rcu list of given type > >