From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1754220Ab3EUKmA (ORCPT ); Tue, 21 May 2013 06:42:00 -0400 Received: from mx0.aculab.com ([213.249.233.131]:54432 "HELO mx0.aculab.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with SMTP id S1753750Ab3EUKlz convert rfc822-to-8bit (ORCPT ); Tue, 21 May 2013 06:41:55 -0400 X-MimeOLE: Produced By Microsoft Exchange V6.5 Content-class: urn:content-classes:message MIME-Version: 1.0 Content-Type: text/plain; charset="Windows-1252" Content-Transfer-Encoding: 8BIT Subject: RE: [PATCH] rcu: fix a race in hlist_nulls_for_each_entry_rcu macro Date: Tue, 21 May 2013 11:40:47 +0100 Message-ID: In-Reply-To: <519B38EC.90401@yandex-team.ru> X-MS-Has-Attach: X-MS-TNEF-Correlator: Thread-Topic: [PATCH] rcu: fix a race in hlist_nulls_for_each_entry_rcu macro Thread-Index: Ac5WBCIi7zwLo+dSQp+rgRWDJ9MARAAAyZTA References: <519B38EC.90401@yandex-team.ru> From: "David Laight" To: "Roman Gushchin" , "Dipankar Sarma" , "Paul E. McKenney" Cc: , , , "David S. Miller" , "Eric Dumazet" , "Alexey Kuznetsov" , "James Morris" , "Hideaki YOSHIFUJI" , "Patrick McHardy" Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org > 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. Hmmm.... if either inet_ehashfn() or next_pseudo_random32() is called gcc must reread it anyway. I'm surprised gcc is generating separate code for all the conditional loop endings. So why is it caching head->first. The 'list empty' might be short-circuited - but that would only be relevant after a rescan. I suspect something else is going on. I'd also have thought that this code needs to scan the entire hash list. If things are moved under its feet this won't happen. If it can end up on a different list (because a node got moved) it is also possible for a later node to move it back. In that case it would end up on the correct list ... > -#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)) I'd have thought it would be better to change hlist_nulls_first_rcu(). David