From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1754367Ab3EUM7I (ORCPT ); Tue, 21 May 2013 08:59:08 -0400 Received: from e38.co.us.ibm.com ([32.97.110.159]:57505 "EHLO e38.co.us.ibm.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1750886Ab3EUM7F (ORCPT ); Tue, 21 May 2013 08:59:05 -0400 Date: Tue, 21 May 2013 05:58:56 -0700 From: "Paul E. McKenney" To: Roman Gushchin Cc: Dipankar Sarma , zhmurov@yandex-team.ru, linux-kernel@vger.kernel.org, netdev@vger.kernel.org, "David S. Miller" , Eric Dumazet , Alexey Kuznetsov , James Morris , Hideaki YOSHIFUJI , Patrick McHardy Subject: Re: [PATCH] rcu: fix a race in hlist_nulls_for_each_entry_rcu macro Message-ID: <20130521125856.GH3578@linux.vnet.ibm.com> Reply-To: paulmck@linux.vnet.ibm.com References: <519B38EC.90401@yandex-team.ru> <20130521120906.GD3578@linux.vnet.ibm.com> <519B6CBE.1020002@yandex-team.ru> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <519B6CBE.1020002@yandex-team.ru> User-Agent: Mutt/1.5.21 (2010-09-15) X-TM-AS-MML: No X-Content-Scanned: Fidelis XPS MAILER x-cbid: 13052112-5518-0000-0000-00000EDA4070 Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Tue, May 21, 2013 at 04:46:54PM +0400, Roman Gushchin wrote: > On 21.05.2013 16:09, Paul E. McKenney wrote: > >On Tue, May 21, 2013 at 01:05:48PM +0400, Roman Gushchin wrote: > >>Hi, all! > >> > >>This is a fix for a problem described here: > >>https://lkml.org/lkml/2013/4/16/371 . > >>--- > >> > >>Some network functions (udp4_lib_lookup2(), for instance) use the > >>hlist_nulls_for_each_entry_rcu macro in a way that assumes restarting > >>of a loop. In this case, it is strictly necessary to reread the head->first > >>value from the memory before each scan. > >>Without additional hints, gcc caches this value in a register. In this case, > >>if a cached node is moved to another chain during the scan, we can loop > >>forever getting wrong nulls values and restarting the loop uninterruptedly. > >> > >>Signed-off-by: Roman Gushchin > >>Reported-by: Boris Zhmurov > >>--- > >> include/linux/rculist_nulls.h | 5 +++-- > >> 1 file changed, 3 insertions(+), 2 deletions(-) > >> > >>diff --git a/include/linux/rculist_nulls.h b/include/linux/rculist_nulls.h > >>index 2ae1371..efd51bf 100644 > >>--- a/include/linux/rculist_nulls.h > >>+++ b/include/linux/rculist_nulls.h > >>@@ -37,8 +37,9 @@ static inline void hlist_nulls_del_init_rcu(struct > >>hlist_nulls_node *n) > >> } > >> } > >> > >>-#define hlist_nulls_first_rcu(head) \ > >>- (*((struct hlist_nulls_node __rcu __force **)&(head)->first)) > >>+#define hlist_nulls_first_rcu(head) \ > >>+ (*((struct hlist_nulls_node __rcu __force **) \ > >>+ &((volatile typeof(*head) *)head)->first)) > > > >Why not use ACCESS_ONCE() or (better) rcu_dereference_raw() here? > > It will be nice, but will require to keep the old variant too (for > using in hlist_nulls_add_head_rcu() as in rcu_assign_pointer() > argument). Do you think, it's better? Both ACCESS_ONCE() and rcu_dereference_raw() can be used by updaters as well as readers, so yes, I do think that it is better. Better to keep the encapsulation rather than having to search for lots of volatile casts should this idiom ever need to change. Thanx, Paul