Received: by 2002:a05:6358:9144:b0:117:f937:c515 with SMTP id r4csp4352267rwr; Mon, 8 May 2023 06:37:44 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ6KiGvCBp95VaZtj4wM8E0GdI4zsvemuNVwCdOF+HlazZTXyoXNDm3FF/7vb3oD8nYIc9tH X-Received: by 2002:a05:6a00:2296:b0:63d:39a5:5bb9 with SMTP id f22-20020a056a00229600b0063d39a55bb9mr11815066pfe.7.1683553064618; Mon, 08 May 2023 06:37:44 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1683553064; cv=none; d=google.com; s=arc-20160816; b=Vo+PwIJnBYc8qyiVOdM9VcdN8q/fMswrU6cl4EJX1+3sqsl/Xv3JCKLt74hZTeKPtk Y8QIhN1HUu5eKrJAadgmEB4Jgt8Ybf+gs3iZ2rgTjV04i4Pgx/d5TS6siOV58l6Ky2+m /eodS8qqBc1ycyqZ9e4wh0G+P/po7qSsiF3rXyv5y0rr6wAL508RiRMlVkH7V6b3X0Ho HuiqkBo6AwElbeINQyNZRvYAUKU2wFq8kRDKJ+lS2q9eszDl0A+S9r4mai1uISNDHaHc VF7oBciRlj9TvCLiW047Y/AVGb3VC2LT26+CR0M5bpsG9wJDL7hFZapvaNOk8NwR3eXj 5qpw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature; bh=fQCOJg3W+uzuBK1RqiTQ11fYeTUuvn8Ma1oUc2qRxTs=; b=NKQ6GV910hQONsP9HPJaCFV5MUsqTv0EzOC/1MOgM52dEudZzwf3ke2csramsAHkte TjJqP+jqaL1CdTFV84tqBmpyLq0VHkKKYJXNG0c5jLX6QvMKYfcyMYwMBO/eATuZUmYB NqP3Ss0u130NkEzhf0CdYq8sJ8N7CXhTkiZtrT2aSSopv+p7NPf6uM+52MdtthOGK0ez 8ofKspl4b3VkQcri8gAqsZFKp7fX6BYGbWnSGBHfzKHDGCsSWHPMJ89eQQOzMJsyPf70 O5b0qze3wxLPnsgkdRCeiCt0oml19JVSuZB5gSMNpCHZeTEYcLJsbEyXMKB7Qejd6s7A GDWQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20221208 header.b=KlZIFceK; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id k185-20020a6284c2000000b0063b7b7712a6si8532696pfd.255.2023.05.08.06.37.24; Mon, 08 May 2023 06:37:44 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20221208 header.b=KlZIFceK; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232968AbjEHN07 (ORCPT + 99 others); Mon, 8 May 2023 09:26:59 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44114 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229457AbjEHN06 (ORCPT ); Mon, 8 May 2023 09:26:58 -0400 Received: from mail-pf1-x42a.google.com (mail-pf1-x42a.google.com [IPv6:2607:f8b0:4864:20::42a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0F70A26EB1 for ; Mon, 8 May 2023 06:26:57 -0700 (PDT) Received: by mail-pf1-x42a.google.com with SMTP id d2e1a72fcca58-64115eef620so34393932b3a.1 for ; Mon, 08 May 2023 06:26:57 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1683552416; x=1686144416; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:from:to:cc:subject:date:message-id:reply-to; bh=fQCOJg3W+uzuBK1RqiTQ11fYeTUuvn8Ma1oUc2qRxTs=; b=KlZIFceKK/yUt1yZ2w8IWHrzddZd+q+0DLvinVyhw10Yvr8b7ShPD9U6KEoq2V08tJ sPUraAFwu4rVe1UwQ5Gi1dDzz6Gu4u0lyW3yhoeDm5oef5WNSzB8oPaRqiDAoITsQsP/ C+fXcpwGtzl55NZ1QFpnviufpFnguPyo/eMm41aVCaWOhGivGXrv7unxCZjTxV3YKVDx 7+ZUqtxEuOZAq51FH55m1OIA66WyVoj5pSuoeiQfVg2MLtpLCaMHx9zxN1q9Bu6r3Oo2 aFpWcncg95OfCZ/wdF59c1giSsDwfsNaoOEqgGK+GstMDgV3sEuTwGuI/4lHih/mu4lo +6kw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1683552416; x=1686144416; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=fQCOJg3W+uzuBK1RqiTQ11fYeTUuvn8Ma1oUc2qRxTs=; b=JaQaZglc3YmI8zfzy2Qs51HXqho6zRgvmOsZjDJzIolmkFidQ4B9R5EAYjuRZ97Juc Rgkd2Nc0+afRAY/8Yeqxzm+dWJKxqNwr7luzukV1+DG5OwSVN0Z3bJyBNLVmDveHDcMp sNnxbyIXsslMu3tQuw0POZtUuKWgXcSIaHBsxKXZJMYIlrgwS8LauWBnr9A9aWYT/9+9 Lo48Z+P7qk3siRC8uBFCbpOsHCKZSwJfMOANGL5PqSaemiGlhm54TmUpLNl0Ln21ZDyO +Y7E5jo6h4H+QwDKts5swzpe+W7Zb88JSTY4EXwQukwJqGTAl5PZL4Ar6ZkVKHShDd1Y N80g== X-Gm-Message-State: AC+VfDw5s254HhFNeYW9Y0D4FjmakII74wyj/yLZVp+pNbRiO5nXer0z 1oVhjnMrsV34LEH7NuH8BoIpfTOzuIVJxQ== X-Received: by 2002:a17:902:c409:b0:1a9:778b:c1a8 with SMTP id k9-20020a170902c40900b001a9778bc1a8mr22710917plk.12.1683552416296; Mon, 08 May 2023 06:26:56 -0700 (PDT) Received: from vernon-pc ([49.67.182.217]) by smtp.gmail.com with ESMTPSA id o9-20020a170902d4c900b001a98f844e60sm3209582plg.263.2023.05.08.06.26.54 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 08 May 2023 06:26:55 -0700 (PDT) Date: Mon, 8 May 2023 21:26:44 +0800 From: Vernon Yang To: "Liam R. Howlett" Cc: Andrew Morton , maple-tree@lists.infradead.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org Subject: Re: [PATCH v2 31/36] maple_tree: Add mas_prev_range() and mas_find_range_rev interface Message-ID: References: <20230505174204.2665599-1-Liam.Howlett@oracle.com> <20230505174204.2665599-32-Liam.Howlett@oracle.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20230505174204.2665599-32-Liam.Howlett@oracle.com> X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM, RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Fri, May 05, 2023 at 01:41:59PM -0400, Liam R. Howlett wrote: > Some users of the maple tree may want to move to the previous range > regardless of the value stored there. Add this interface as well as the > 'find' variant to support walking to the first value, then iterating > over the previous ranges. > > Signed-off-by: Liam R. Howlett > --- > include/linux/maple_tree.h | 1 + > lib/maple_tree.c | 160 +++++++++++++++++++++++++++---------- > 2 files changed, 121 insertions(+), 40 deletions(-) > > diff --git a/include/linux/maple_tree.h b/include/linux/maple_tree.h > index a4cd8f891a090..542b09118a09f 100644 > --- a/include/linux/maple_tree.h > +++ b/include/linux/maple_tree.h > @@ -467,6 +467,7 @@ void mas_destroy(struct ma_state *mas); > int mas_expected_entries(struct ma_state *mas, unsigned long nr_entries); > > void *mas_prev(struct ma_state *mas, unsigned long min); > +void *mas_prev_range(struct ma_state *mas, unsigned long max); > void *mas_next(struct ma_state *mas, unsigned long max); > void *mas_next_range(struct ma_state *mas, unsigned long max); > > diff --git a/lib/maple_tree.c b/lib/maple_tree.c > index e050fd1f7cce8..f060c71965c0d 100644 > --- a/lib/maple_tree.c > +++ b/lib/maple_tree.c > @@ -5924,18 +5924,8 @@ void *mt_next(struct maple_tree *mt, unsigned long index, unsigned long max) > } > EXPORT_SYMBOL_GPL(mt_next); > > -/** > - * mas_prev() - Get the previous entry > - * @mas: The maple state > - * @min: The minimum value to check. > - * > - * Must hold rcu_read_lock or the write lock. > - * Will reset mas to MAS_START if the node is MAS_NONE. Will stop on not > - * searchable nodes. > - * > - * Return: the previous value or %NULL. > - */ > -void *mas_prev(struct ma_state *mas, unsigned long min) > +static inline bool mas_prev_setup(struct ma_state *mas, unsigned long min, > + void **entry) > { > if (mas->index <= min) > goto none; > @@ -5953,7 +5943,8 @@ void *mas_prev(struct ma_state *mas, unsigned long min) > if (!mas->index) > goto none; > mas->index = mas->last = 0; > - return mas_root(mas); > + *entry = mas_root(mas); > + return true; > } > > if (mas_is_none(mas)) { > @@ -5961,18 +5952,64 @@ void *mas_prev(struct ma_state *mas, unsigned long min) > /* Walked to out-of-range pointer? */ > mas->index = mas->last = 0; > mas->node = MAS_ROOT; > - return mas_root(mas); > + *entry = mas_root(mas); > + return true; > } > - return NULL; > + return true; > } > - return mas_prev_slot(mas, min, false); > + > + return false; > > none: > mas->node = MAS_NONE; > - return NULL; > + return true; > +} > + > +/** > + * mas_prev() - Get the previous entry > + * @mas: The maple state > + * @min: The minimum value to check. > + * > + * Must hold rcu_read_lock or the write lock. > + * Will reset mas to MAS_START if the node is MAS_NONE. Will stop on not > + * searchable nodes. > + * > + * Return: the previous value or %NULL. > + */ > +void *mas_prev(struct ma_state *mas, unsigned long min) > +{ > + void *entry = NULL; > + > + if (mas_prev_setup(mas, min, &entry)) > + return entry; > + > + return mas_prev_slot(mas, min, false); > } > EXPORT_SYMBOL_GPL(mas_prev); > > +/** > + * mas_prev_range() - Advance to the previous range > + * @mas: The maple state > + * @min: The minimum value to check. > + * > + * Sets @mas->index and @mas->last to the range. > + * Must hold rcu_read_lock or the write lock. > + * Will reset mas to MAS_START if the node is MAS_NONE. Will stop on not > + * searchable nodes. > + * > + * Return: the previous value or %NULL. > + */ > +void *mas_prev_range(struct ma_state *mas, unsigned long min) > +{ > + void *entry = NULL; > + > + if (mas_prev_setup(mas, min, &entry)) > + return entry; > + > + return mas_prev_slot(mas, min, true); > +} > +EXPORT_SYMBOL_GPL(mas_prev_slot); Hi Liam, I guess you want to export mas_prev_range symbol instead of mas_prev_slot. > + > /** > * mt_prev() - get the previous value in the maple tree > * @mt: The maple tree > @@ -6116,20 +6153,15 @@ void *mas_find_range(struct ma_state *mas, unsigned long max) > EXPORT_SYMBOL_GPL(mas_find_range); > > /** > - * mas_find_rev: On the first call, find the first non-null entry at or below > - * mas->index down to %min. Otherwise find the first non-null entry below > - * mas->index down to %min. > - * @mas: The maple state > - * @min: The minimum value to check. > + * mas_find_rev_setup() - Internal function to set up mas_find_*_rev() > * > - * Must hold rcu_read_lock or the write lock. > - * If an entry exists, last and index are updated accordingly. > - * May set @mas->node to MAS_NONE. > - * > - * Return: The entry or %NULL. > + * Returns: True if entry is the answer, false otherwise. > */ > -void *mas_find_rev(struct ma_state *mas, unsigned long min) > +static inline bool mas_find_rev_setup(struct ma_state *mas, unsigned long min, > + void **entry) > { > + *entry = NULL; > + > if (unlikely(mas_is_none(mas))) { > if (mas->index <= min) > goto none; > @@ -6141,7 +6173,7 @@ void *mas_find_rev(struct ma_state *mas, unsigned long min) > if (unlikely(mas_is_paused(mas))) { > if (unlikely(mas->index <= min)) { > mas->node = MAS_NONE; > - return NULL; > + return true; > } > mas->node = MAS_START; > mas->last = --mas->index; > @@ -6149,14 +6181,12 @@ void *mas_find_rev(struct ma_state *mas, unsigned long min) > > if (unlikely(mas_is_start(mas))) { > /* First run or continue */ > - void *entry; > - > if (mas->index < min) > - return NULL; > + return true; > > - entry = mas_walk(mas); > - if (entry) > - return entry; > + *entry = mas_walk(mas); > + if (*entry) > + return true; > } > > if (unlikely(!mas_searchable(mas))) { > @@ -6170,22 +6200,72 @@ void *mas_find_rev(struct ma_state *mas, unsigned long min) > */ > mas->last = mas->index = 0; > mas->node = MAS_ROOT; > - return mas_root(mas); > + *entry = mas_root(mas); > + return true; > } > } > > if (mas->index < min) > - return NULL; > + return true; > > - /* Retries on dead nodes handled by mas_prev_slot */ > - return mas_prev_slot(mas, min, false); > + return false; > > none: > mas->node = MAS_NONE; > - return NULL; > + return true; > +} > + > +/** > + * mas_find_rev: On the first call, find the first non-null entry at or below > + * mas->index down to %min. Otherwise find the first non-null entry below > + * mas->index down to %min. > + * @mas: The maple state > + * @min: The minimum value to check. > + * > + * Must hold rcu_read_lock or the write lock. > + * If an entry exists, last and index are updated accordingly. > + * May set @mas->node to MAS_NONE. > + * > + * Return: The entry or %NULL. > + */ > +void *mas_find_rev(struct ma_state *mas, unsigned long min) > +{ > + void *entry; > + > + if (mas_find_rev_setup(mas, min, &entry)) > + return entry; > + > + /* Retries on dead nodes handled by mas_prev_slot */ > + return mas_prev_slot(mas, min, false); > + > } > EXPORT_SYMBOL_GPL(mas_find_rev); > > +/** > + * mas_find_range_rev: On the first call, find the first non-null entry at or > + * below mas->index down to %min. Otherwise advance to the previous slot after > + * mas->index down to %min. > + * @mas: The maple state > + * @min: The minimum value to check. > + * > + * Must hold rcu_read_lock or the write lock. > + * If an entry exists, last and index are updated accordingly. > + * May set @mas->node to MAS_NONE. > + * > + * Return: The entry or %NULL. > + */ > +void *mas_find_range_rev(struct ma_state *mas, unsigned long min) > +{ > + void *entry; > + > + if (mas_find_rev_setup(mas, min, &entry)) > + return entry; > + > + /* Retries on dead nodes handled by mas_prev_slot */ > + return mas_prev_slot(mas, min, true); > +} > +EXPORT_SYMBOL_GPL(mas_find_range_rev); > + > /** > * mas_erase() - Find the range in which index resides and erase the entire > * range. > > -- > 2.39.2 >