Received: by 2002:a05:6358:53a8:b0:117:f937:c515 with SMTP id z40csp523078rwe; Wed, 19 Apr 2023 02:06:11 -0700 (PDT) X-Google-Smtp-Source: AKy350YjE9al9Ch5iHOdO7iivwKCep1yy/6y1mwXdsow1JEI7BGB8XLmXo8/7xNhaXSQdpl6WA8n X-Received: by 2002:a05:6a00:21d5:b0:638:7c22:6fd with SMTP id t21-20020a056a0021d500b006387c2206fdmr2933303pfj.1.1681895171394; Wed, 19 Apr 2023 02:06:11 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1681895171; cv=none; d=google.com; s=arc-20160816; b=zb/HnjOaIlnEnEDYsbHPW0DUDv8UJXzx6QEG6vai60h72HYHEme0iKDExY6S97tUz2 PKqZ4d2pamWldqVQY+ajIq++KD/SIMaA6ZSnAaEBu/f/SwM8xl36akR7m2ZnyxrZO43g E3S/mAn+uNdFO/IktTfP9lOXP/p6H38UIfP32gltIlgGxf0Pzqf/89EiyWhdEUOoWPmr NJkwfYebq4esOCAjdSpjBy+OVif2yGaRSVjX0rkcxJ26pzrhSMMMoXrur5GMdCKFtMFd A50o+i5d4R8jAnAUOAGpQm5kKRKQNUFn1GHXAegtlJ0uUmSOVqlLReiEiPkrV5zAUm3l fzQQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:in-reply-to:from :references:cc:to:subject:user-agent:mime-version:date:message-id :dkim-signature; bh=79PwKIpTwwaVU82r+U4/sK48h5bR61sbuArdiDnpPEo=; b=xL1iqsIUDhTHeoso1dr5vk8lmNT3TVupg2L009SRXDA/8TCegZyYh+7Ba0vhdoPNpl Xt3ucL8cqR5OzroqtZrO8YePVoKlLVqlkahw+/oM63ADGIQCsHRM10D1Bfh4aUHG/TOL 6gWwyM8u0mO/ksLdEwHn+5zry2wGu29hY2M6Ot+r6kHkcyAHiVMffcs+LfZ3gkz5BOUV KhtPAZQH5HhbEppzoy+optFNOqZ4Ihte6g52Uyq3Rx2o8E4ja25Jao6HN09BHMN1x8bZ XgZB2e+sMtdoRXdCyKgsrh6c/YFyjpPnA17zTxCqkM7k8EJ0rACU+cFkVff+M1nGT9md +3RQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@bytedance.com header.s=google header.b=VqMdtfuj; 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=QUARANTINE sp=QUARANTINE dis=NONE) header.from=bytedance.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id p184-20020a625bc1000000b0063b669ec9a0si13458013pfb.103.2023.04.19.02.05.59; Wed, 19 Apr 2023 02:06:11 -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=@bytedance.com header.s=google header.b=VqMdtfuj; 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=QUARANTINE sp=QUARANTINE dis=NONE) header.from=bytedance.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232129AbjDSJC4 (ORCPT + 99 others); Wed, 19 Apr 2023 05:02:56 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40288 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232815AbjDSJCp (ORCPT ); Wed, 19 Apr 2023 05:02:45 -0400 Received: from mail-pj1-x1035.google.com (mail-pj1-x1035.google.com [IPv6:2607:f8b0:4864:20::1035]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6F2E530E0 for ; Wed, 19 Apr 2023 02:02:12 -0700 (PDT) Received: by mail-pj1-x1035.google.com with SMTP id 98e67ed59e1d1-246f856d751so2105975a91.0 for ; Wed, 19 Apr 2023 02:02:12 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance.com; s=google; t=1681894932; x=1684486932; h=content-transfer-encoding:in-reply-to:from:references:cc:to:subject :user-agent:mime-version:date:message-id:from:to:cc:subject:date :message-id:reply-to; bh=79PwKIpTwwaVU82r+U4/sK48h5bR61sbuArdiDnpPEo=; b=VqMdtfuj6C2mrAI6yBezO+vvPulybbVN53rDsaeUShooyCwrV3Iz0fFfj07Utjp0cY mYqM4WGvsXfcjOFUlDUGwRwLZlh5xRogbtYBDh4E/TL2IDVIS8BKmwOiSIHdvXKIeXFK niMvIVIXWrzMxiw3/LcIdW4lZn4NgUZIw0i/1225WGpyMnIBrkJceWOg2AmJFiV4vS38 W/b7q/KIVcvEsgGBkn4BChla5oOdA6TcS2a9WEH2+j6HLVg2qgVTU5O/1B0WjRcPeCK+ lzmmBh+gaTXkzrb48EVnC1+V/aznAVBNVBY8QVw/fUDIH3JiZ9k3TRmmvhi5cp3XgjI8 KMqg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1681894932; x=1684486932; h=content-transfer-encoding:in-reply-to:from:references:cc:to:subject :user-agent:mime-version:date:message-id:x-gm-message-state:from:to :cc:subject:date:message-id:reply-to; bh=79PwKIpTwwaVU82r+U4/sK48h5bR61sbuArdiDnpPEo=; b=YY7TRfPwzDOcuJDfG3OiCf9aiqWm/g2wlxyM7Htp5BL6/KQC7T6mHlcpjJHHRzQa01 6GTt51PPjImE1u9OGV7/VlSJV9chxBjtRHLcsQTA1mB0up0ybSvOLDZj0oLFTS7EwT8j B+FClrlNyYOMN+WJ643CzWhDX3xbUillk519gPJ2LXVk50GucSPOj8DOVD6vfUzfvrIw 5brW+q/rsVc2CZ7haf3LuM/2Pto7JDwAoXnuhbranLTvtnWxYZ+miR4BWZ2bZTbL3PRr dB6KPQn8R6+S4K7okaF9h5nr78v+ltWUxCrzbjVS+wpoc941fRh5tS1jzIki/SWxwDAK BJFg== X-Gm-Message-State: AAQBX9ev2CUHqVAEgGax2WiXr5s3InacFgRzQJcnrgBnDSDkBVVONSFh rHQZ4/Q3huoZBKHQ5+lCq/iHdw== X-Received: by 2002:a17:90a:38a5:b0:246:818c:d8e4 with SMTP id x34-20020a17090a38a500b00246818cd8e4mr2493452pjb.11.1681894931889; Wed, 19 Apr 2023 02:02:11 -0700 (PDT) Received: from [10.200.10.217] ([139.177.225.248]) by smtp.gmail.com with ESMTPSA id lx3-20020a17090b4b0300b00246cc751c6bsm929701pjb.46.2023.04.19.02.02.09 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Wed, 19 Apr 2023 02:02:11 -0700 (PDT) Message-ID: <90fd5cfa-5ed9-495f-fdef-0130adaf591b@bytedance.com> Date: Wed, 19 Apr 2023 17:02:06 +0800 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:102.0) Gecko/20100101 Thunderbird/102.9.1 Subject: Re: [PATCH 1/3] maple_tree: Make maple state reusable after mas_empty_area_rev() To: "Liam R. Howlett" Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org, Rick Edgecombe , Andrew Morton References: <20230414145728.4067069-1-Liam.Howlett@oracle.com> From: Peng Zhang In-Reply-To: <20230414145728.4067069-1-Liam.Howlett@oracle.com> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-4.7 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,NICE_REPLY_A,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE,URIBL_BLOCKED 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 在 2023/4/14 22:57, Liam R. Howlett 写道: > Stop using maple state min/max for the range by passing through pointers > for those values. This will allow the maple state to be reused without > resetting. > > Also add some logic to fail out early on searching with invalid > arguments. > > Fixes: 54a611b60590 ("Maple Tree: add new data structure") > Signed-off-by: Liam R. Howlett > --- > lib/maple_tree.c | 27 +++++++++++++-------------- > 1 file changed, 13 insertions(+), 14 deletions(-) > > diff --git a/lib/maple_tree.c b/lib/maple_tree.c > index 4df6a0ce1c1b..ed350aa293b2 100644 > --- a/lib/maple_tree.c > +++ b/lib/maple_tree.c > @@ -4938,7 +4938,8 @@ static inline void *mas_prev_entry(struct ma_state *mas, unsigned long min) > * Return: True if found in a leaf, false otherwise. > * > */ > -static bool mas_rev_awalk(struct ma_state *mas, unsigned long size) > +static bool mas_rev_awalk(struct ma_state *mas, unsigned long size, > + unsigned long *gap_min, unsigned long *gap_max) > { > enum maple_type type = mte_node_type(mas->node); > struct maple_node *node = mas_mn(mas); > @@ -5003,8 +5004,8 @@ static bool mas_rev_awalk(struct ma_state *mas, unsigned long size) > > if (unlikely(ma_is_leaf(type))) { > mas->offset = offset; > - mas->min = min; > - mas->max = min + gap - 1; > + *gap_min = min; > + *gap_max = min + gap - 1; > return true; > } > > @@ -5280,6 +5281,9 @@ int mas_empty_area(struct ma_state *mas, unsigned long min, > unsigned long *pivots; > enum maple_type mt; > > + if (min >= max) This can lead to errors, min == max is valid. I think it's better to change it to this: if (min > max || size == 0 || max - min < size - 1) > + return -EINVAL; > + > if (mas_is_start(mas)) > mas_start(mas); > else if (mas->offset >= 2) > @@ -5334,6 +5338,9 @@ int mas_empty_area_rev(struct ma_state *mas, unsigned long min, > { > struct maple_enode *last = mas->node; > > + if (min >= max) ditto. > + return -EINVAL; > + > if (mas_is_start(mas)) { > mas_start(mas); > mas->offset = mas_data_end(mas); > @@ -5353,7 +5360,7 @@ int mas_empty_area_rev(struct ma_state *mas, unsigned long min, > mas->index = min; > mas->last = max; > > - while (!mas_rev_awalk(mas, size)) { > + while (!mas_rev_awalk(mas, size, &min, &max)) { > if (last == mas->node) { > if (!mas_rewind_node(mas)) > return -EBUSY; > @@ -5368,17 +5375,9 @@ int mas_empty_area_rev(struct ma_state *mas, unsigned long min, > if (unlikely(mas->offset == MAPLE_NODE_SLOTS)) > return -EBUSY; > > - /* > - * mas_rev_awalk() has set mas->min and mas->max to the gap values. If > - * the maximum is outside the window we are searching, then use the last > - * location in the search. > - * mas->max and mas->min is the range of the gap. > - * mas->index and mas->last are currently set to the search range. > - */ > - > /* Trim the upper limit to the max. */ > - if (mas->max <= mas->last) > - mas->last = mas->max; > + if (max <= mas->last) > + mas->last = max; We can get max as follows, without using pointers to track min, max in mas_rev_awalk(). mt = mte_node_type(mas->node); pivots = ma_pivots(mas_mn(mas), mt); max = mas_logical_pivot(mas, pivots, mas->offset, mt); if (max < mas->last) /* The equal sign here can be removed */ mas->last = max; > > mas->index = mas->last - size + 1; > return 0;