From mboxrd@z Thu Jan 1 00:00:00 1970 From: Paolo Abeni Subject: Re: [PATCH 1/3] rhashtable: further improve stability of rhashtable_walk Date: Fri, 06 Jul 2018 10:59:32 +0200 Message-ID: <0a44916eacea6c3899152a07321ff69d96ed8c52.camel@redhat.com> References: <153086101070.2825.6850140624411927465.stgit@noble> <153086109256.2825.15329014177598382684.stgit@noble> Mime-Version: 1.0 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: 7bit Cc: netdev@vger.kernel.org, linux-kernel@vger.kernel.org To: NeilBrown , Thomas Graf , Herbert Xu , Tom Herbert Return-path: Received: from mx3-rdu2.redhat.com ([66.187.233.73]:48248 "EHLO mx1.redhat.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1753535AbeGFI7f (ORCPT ); Fri, 6 Jul 2018 04:59:35 -0400 In-Reply-To: <153086109256.2825.15329014177598382684.stgit@noble> Sender: netdev-owner@vger.kernel.org List-ID: On Fri, 2018-07-06 at 17:11 +1000, NeilBrown wrote: > If the sequence: > obj = rhashtable_walk_next(iter); > rhashtable_walk_stop(iter); > rhashtable_remove_fast(ht, &obj->head, params); > rhashtable_walk_start(iter); > > races with another thread inserting or removing > an object on the same hash chain, a subsequent > rhashtable_walk_next() is not guaranteed to get the "next" > object. It is possible that an object could be > repeated, or missed. The above scenario is very similar to the one I'm running: rhashtable_walk_next(iter); rhashtable_walk_stop(iter); // rhashtable change not yet identified, could be either // remove, insert or even rehash rhashtable_walk_start(iter); rhashtable_walk_next(iter); but I'm seeing use-after-free there. I'll try this patch to see if solves my issue. Note: the code under test is a pending new patch I'm holding due to the above issue, I can send it as RFC to share the code if you think it may help. > @@ -867,15 +866,39 @@ void *rhashtable_walk_next(struct rhashtable_iter *iter) > bool rhlist = ht->rhlist; > > if (p) { > - if (!rhlist || !(list = rcu_dereference(list->next))) { > - p = rcu_dereference(p->next); > - list = container_of(p, struct rhlist_head, rhead); > - } > - if (!rht_is_a_nulls(p)) { > - iter->skip++; > - iter->p = p; > - iter->list = list; > - return rht_obj(ht, rhlist ? &list->rhead : p); > + if (!rhlist && iter->p_is_unsafe) { > + /* > + * First time next() was called after start(). > + * Need to find location of 'p' in the list. > + */ > + struct rhash_head *p; > + > + iter->skip = 0; > + rht_for_each_rcu(p, iter->walker.tbl, iter->slot) { > + iter->skip++; > + if (p <= iter->p) > + continue; Out of sheer ignorance, I really don't understand the goal of the above conditional ?!? Should it possibly be something like: if (p != iter->p->next) instead? But I think we can't safely dereference 'p' yet ?!? I'm sorry for the possibly dumb comments, rhashtable internals are somewhat obscure to me, but I'm really interested in this topic. Cheers, Paolo