From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-17.3 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER, INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_AGENT_SANE_1 autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 5EB05C4338F for ; Wed, 25 Aug 2021 11:53:36 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 427B360C3E for ; Wed, 25 Aug 2021 11:53:36 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S240440AbhHYLyU (ORCPT ); Wed, 25 Aug 2021 07:54:20 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33012 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S240405AbhHYLyT (ORCPT ); Wed, 25 Aug 2021 07:54:19 -0400 Received: from server.lespinasse.org (server.lespinasse.org [IPv6:2001:470:82ab::100:0]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 38BE8C061757 for ; Wed, 25 Aug 2021 04:53:34 -0700 (PDT) DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=lespinasse.org; i=@lespinasse.org; q=dns/txt; s=srv-30-ed; t=1629892412; h=date : from : to : cc : subject : message-id : references : mime-version : content-type : in-reply-to : from; bh=F52fen2ss8GLg3DqmHwQUgF1KeZ9yP3yjDp0L7IYMzA=; b=cCzKgH+ETPVMW8IvDXAajwFev9kHvEnBserO0HqSXr+WkGyVjngBwEl9xKzBGXChv7EF9 qdXP5Iw6xciYfeQDQ== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=lespinasse.org; i=@lespinasse.org; q=dns/txt; s=srv-30-rsa; t=1629892412; h=date : from : to : cc : subject : message-id : references : mime-version : content-type : in-reply-to : from; bh=F52fen2ss8GLg3DqmHwQUgF1KeZ9yP3yjDp0L7IYMzA=; b=I6UGMBHWVdxSlFMi9DGx8Mt+//KROxs0BqUv7KCyjDDeKf7htPZZIH0ZYOuerKXKdvwTc ovotaDdVCVJDxWU6QOf8yQ4iQQblAa5iD/eiuYs1+JPCPOHpYePtxPf74ePkJDPERY+njT3 xKbmsTOghq1QkSJZaxAqDdSX3LdxJ+7y3oJzMhGLhFYrl8wkMYa648nzuliouSb2y6FGdcU Wj2LmdQ2L4znC2he0DntiLFySyYx+j2mTPnnbuVY9w1j7dzlgugFS0S2Du9/HHOeq0u793E KdRDN2odw3A9JnwpZOUgNwoRlO2raKMYkODM636j6nimwsCJi75FqbRfvInA== Received: by server.lespinasse.org (Postfix, from userid 1000) id A48EE16099D; Wed, 25 Aug 2021 04:53:32 -0700 (PDT) Date: Wed, 25 Aug 2021 04:53:32 -0700 From: Michel Lespinasse To: Peter Zijlstra Cc: Li RongQing , dbueso@suse.de, mingo@kernel.org, michel@lespinasse.org, linux-kernel@vger.kernel.org Subject: Re: [PATCH] rbtree: stop iteration early in rb_find_first Message-ID: <20210825115332.GA4645@lespinasse.org> References: <1629885588-10590-1-git-send-email-lirongqing@baidu.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: User-Agent: Mutt/1.10.1 (2018-07-13) Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Wed, Aug 25, 2021 at 01:39:26PM +0200, Peter Zijlstra wrote: > On Wed, Aug 25, 2021 at 05:59:48PM +0800, Li RongQing wrote: > > stop iteration if match is not NULL and result of cmp is > > not zero, this means the matched node has been found, and > > the node with same key has been passed > > > > Signed-off-by: Li RongQing > > --- > > include/linux/rbtree.h | 3 +++ > > 1 file changed, 3 insertions(+) > > > > diff --git a/include/linux/rbtree.h b/include/linux/rbtree.h > > index d31ecaf4fdd3..2689771df9bb 100644 > > --- a/include/linux/rbtree.h > > +++ b/include/linux/rbtree.h > > @@ -324,6 +324,9 @@ rb_find_first(const void *key, const struct rb_root *tree, > > } else if (c > 0) { > > node = node->rb_right; > > } > > + > > + if (match && c) > > + break; > > } > > > > return match; > > Acked-by: Peter Zijlstra (Intel) NAK. This looked slightly wrong before, and is more wrong after. Before: there was this weird condition if (c <= 0) {} else if (c > 0) {} , making you wonder what the third possibility may be. Easy fix would be to remove the second condition. After: say the key is equal the root, so the code sets match=root and goes left. Then it stops searching because match is set and c<0. This doesn't work, the code needs to keep searching for the leftmost match. -- Michel "walken" Lespinasse