From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1753239AbcCDNVv (ORCPT ); Fri, 4 Mar 2016 08:21:51 -0500 Received: from szxga03-in.huawei.com ([119.145.14.66]:15536 "EHLO szxga03-in.huawei.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751213AbcCDNVr (ORCPT ); Fri, 4 Mar 2016 08:21:47 -0500 Message-ID: <56D98BDD.3060806@huawei.com> Date: Fri, 4 Mar 2016 21:21:33 +0800 From: zhong jiang User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:12.0) Gecko/20120428 Thunderbird/12.0.1 MIME-Version: 1.0 To: Matthew Wilcox CC: Andrew Morton , Hugh Dickins , Ohad Ben-Cohen , Matthew Wilcox , Konstantin Khlebnikov , , , , Subject: Re: [PATCH 1/5] radix-tree: Fix race in gang lookup References: <1453929472-25566-1-git-send-email-matthew.r.wilcox@intel.com> <1453929472-25566-2-git-send-email-matthew.r.wilcox@intel.com> In-Reply-To: <1453929472-25566-2-git-send-email-matthew.r.wilcox@intel.com> Content-Type: text/plain; charset="ISO-8859-1" Content-Transfer-Encoding: 7bit X-Originating-IP: [10.177.29.68] X-CFilter-Loop: Reflected X-Mirapoint-Virus-RAPID-Raw: score=unknown(0), refid=str=0001.0A020202.56D98BE4.01B7,ss=1,re=0.000,recu=0.000,reip=0.000,cl=1,cld=1,fgs=0, ip=0.0.0.0, so=2013-05-26 15:14:31, dmn=2013-03-21 17:37:32 X-Mirapoint-Loop-Id: 63376380497a385fc0dc94fc0b81dcc7 Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 2016/1/28 5:17, Matthew Wilcox wrote: > From: Matthew Wilcox > > If the indirect_ptr bit is set on a slot, that indicates we need to > redo the lookup. Introduce a new function radix_tree_iter_retry() > which forces the loop to retry the lookup by setting 'slot' to NULL and > turning the iterator back to point at the problematic entry. > > This is a pretty rare problem to hit at the moment; the lookup has to > race with a grow of the radix tree from a height of 0. The consequences > of hitting this race are that gang lookup could return a pointer to a > radix_tree_node instead of a pointer to whatever the user had inserted > in the tree. > > Fixes: cebbd29e1c2f ("radix-tree: rewrite gang lookup using iterator") > Signed-off-by: Matthew Wilcox > Cc: stable@vger.kernel.org > --- > include/linux/radix-tree.h | 16 ++++++++++++++++ > lib/radix-tree.c | 12 ++++++++++-- > 2 files changed, 26 insertions(+), 2 deletions(-) > > diff --git a/include/linux/radix-tree.h b/include/linux/radix-tree.h > index f9a3da5bf892..db0ed595749b 100644 > --- a/include/linux/radix-tree.h > +++ b/include/linux/radix-tree.h > @@ -387,6 +387,22 @@ void **radix_tree_next_chunk(struct radix_tree_root *root, > struct radix_tree_iter *iter, unsigned flags); > > /** > + * radix_tree_iter_retry - retry this chunk of the iteration > + * @iter: iterator state > + * > + * If we iterate over a tree protected only by the RCU lock, a race > + * against deletion or creation may result in seeing a slot for which > + * radix_tree_deref_retry() returns true. If so, call this function > + * and continue the iteration. > + */ > +static inline __must_check > +void **radix_tree_iter_retry(struct radix_tree_iter *iter) > +{ > + iter->next_index = iter->index; > + return NULL; > +} > + > +/** > * radix_tree_chunk_size - get current chunk size > * > * @iter: pointer to radix tree iterator > diff --git a/lib/radix-tree.c b/lib/radix-tree.c > index a25f635dcc56..65422ac17114 100644 > --- a/lib/radix-tree.c > +++ b/lib/radix-tree.c > @@ -1105,9 +1105,13 @@ radix_tree_gang_lookup(struct radix_tree_root *root, void **results, > return 0; > > radix_tree_for_each_slot(slot, root, &iter, first_index) { > - results[ret] = indirect_to_ptr(rcu_dereference_raw(*slot)); > + results[ret] = rcu_dereference_raw(*slot); > if (!results[ret]) > continue; > + if (radix_tree_is_indirect_ptr(results[ret])) { > + slot = radix_tree_iter_retry(&iter); > + continue; > + } > if (++ret == max_items) > break; > } according to your patch, after A race occur, slot equals to null. radix_tree_next_slot() will continue to work. Therefore, it will not return the problematic entry. > @@ -1184,9 +1188,13 @@ radix_tree_gang_lookup_tag(struct radix_tree_root *root, void **results, > return 0; > > radix_tree_for_each_tagged(slot, root, &iter, first_index, tag) { > - results[ret] = indirect_to_ptr(rcu_dereference_raw(*slot)); > + results[ret] = rcu_dereference_raw(*slot); > if (!results[ret]) > continue; > + if (radix_tree_is_indirect_ptr(results[ret])) { > + slot = radix_tree_iter_retry(&iter); > + continue; > + } > if (++ret == max_items) > break; > }