Message ID | 20230517085809.86522-2-zhangpeng.00@bytedance.com |
---|---|
State | New |
Headers |
Return-Path: <linux-kernel-owner@vger.kernel.org> Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp994327vqo; Wed, 17 May 2023 02:25:45 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ6Cf7ov9Y6XxDM35LUZr/3E9IZ5wv4/Nr1XovcIKA5tF7z3OZz5OEq9VQZOZjVA0wUKXBU8 X-Received: by 2002:a17:902:d4ca:b0:1ac:8384:a7fb with SMTP id o10-20020a170902d4ca00b001ac8384a7fbmr36803491plg.66.1684315545257; Wed, 17 May 2023 02:25:45 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1684315545; cv=none; d=google.com; s=arc-20160816; b=KEa2g6OJ89S4c392gSht1II02I9iTE/xsWTuR+je36ExHZ8etIZHJqRBdeGUzcRDwT uTfN5V7GQfZmc+RM18s3IvBvJ3mbTLF87FfkVrkv/SZM/MLQN1ctdBio7LoxMVP0l5oI NEbtqJFGwHWg601Gb978AyOQXJe/t5kR302uqs09rLnE05IOCJFqYy7U9fWvidBwYelj gGrH+qNH1dDfia+PfWi+J70xPMnFe2OZ2nKF5OVy4mVYdQbAaIL10hiXHVRf9AwvupOo xnO5UYOHo/BqK6x3ytBDHkFhACaXkCDZjIP++OHOk4Ft+MSV5poLTyQzCYniVEf9h70X apBQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=dHQ/4wRfx8YGBT0TyL6p3b/Q07WGij+qVhIqIOji5co=; b=zSFLeMYeNRYiUxcowMWVID9Qm/PyDNZ8UYd/vn+W3/T4r//YwTMjl4NGPeGx5XSWEf e0fxQHT/v20ZFaPOj0EE/RC/p3HK1uO2TEt3F1wGMQ5dfoHxlme27bQZRu6ug2Fjy1UG mDX5FRoNjJxOmvxdyIzGeNFbgFUOdrjrNeKwVJd0f5KGeT169orgRcPkwxBlE6y1Hu+2 V5sWV3kTvG3qCIaKoMfGctqdbmP65IhTSm+euOZaWBkF+bfHom7mM+2e0X/PHLzmtqEC 8VWx+oBHGnkKRSfEar16JLdYVEZitm3PDiNiwKkG7acfNGcANw5PfWyfKi7Sc7xcUo8h pEyg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@bytedance.com header.s=google header.b=QkYpZT6J; 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: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id v5-20020a170902b7c500b001ae5a8856fesi184221plz.189.2023.05.17.02.25.31; Wed, 17 May 2023 02:25:45 -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=QkYpZT6J; 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 S231174AbjEQI73 (ORCPT <rfc822;pacteraone@gmail.com> + 99 others); Wed, 17 May 2023 04:59:29 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44824 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231138AbjEQI7U (ORCPT <rfc822;linux-kernel@vger.kernel.org>); Wed, 17 May 2023 04:59:20 -0400 Received: from mail-pf1-x42b.google.com (mail-pf1-x42b.google.com [IPv6:2607:f8b0:4864:20::42b]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C880F3588 for <linux-kernel@vger.kernel.org>; Wed, 17 May 2023 01:58:49 -0700 (PDT) Received: by mail-pf1-x42b.google.com with SMTP id d2e1a72fcca58-6436e075166so357214b3a.0 for <linux-kernel@vger.kernel.org>; Wed, 17 May 2023 01:58:49 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance.com; s=google; t=1684313929; x=1686905929; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=dHQ/4wRfx8YGBT0TyL6p3b/Q07WGij+qVhIqIOji5co=; b=QkYpZT6JTZgFCW8g+gHwlD3usHqZW7OZl7/3oBK6EGsOSrFnjcS/IsoCdmA13UbZeg EODYTAlK0P20L372ioqTr4+YuTOAr7N9aj7tscBr5rWLf0oHxfBOH7sqej8hccrOpEaq GzVu2hXKgmar5SOACGxyML1twxQVjxzY9EV/y9pT+8ToUqs7vIzbk7yu9KRqwACOMP92 U6P8OWr4V6wAeSEzkzL1zMhergOyQFTCkUCmJaedD2y5sfSLKFOPC4qm317hWlCuNQAy cXRnFGH7Mk0l/7roCte8CKvnYjrTWFs1OgO1YEP9J9n9Iz2LcGFgqRtukM2Rg3YWhnqz qTWQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1684313929; x=1686905929; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=dHQ/4wRfx8YGBT0TyL6p3b/Q07WGij+qVhIqIOji5co=; b=a7edJyos/y88ljiU0zWehXeBvxF2IeW4b6oikfHV/Tn8dQYC19IGVvfv0fPFTlDP7K BXlRfeDRUuiHzCYpO41gKggGSz0fGrRxOuQHLC1f4O7AnjKSb4xstvU4bJUnJSSB6R0V ooHDr6Q1/5Cul0vvtFmfmMFjfeOgpiqWq+fpetTNkSrGKTufRjx2mvpdwrQCHsPKY6TA FRx2zkDpSzvgCa0NMqygmBxYGo7nbgYbCD7feNT3ETXBuBL3jfpwYEU+0YKGqgxC8Vok 4X+Wp4hp4Faa7NPdRRxxyRI13xh3TScO4tx5/W85XXmpleD3VmvFusTctYBvHB09vsBd 0ZCA== X-Gm-Message-State: AC+VfDyBgdceZvK8/DL08qA8yAG2xsjHoT14rdgTMWH2UVQO2GEIJVtG FhHStdO30tlWtRYhfqVv87yF6w== X-Received: by 2002:a05:6a20:1587:b0:101:5171:c880 with SMTP id h7-20020a056a20158700b001015171c880mr35122952pzj.62.1684313929300; Wed, 17 May 2023 01:58:49 -0700 (PDT) Received: from GL4FX4PXWL.bytedance.net ([139.177.225.255]) by smtp.gmail.com with ESMTPSA id m18-20020a63ed52000000b0052c9d1533b6sm15056765pgk.56.2023.05.17.01.58.46 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Wed, 17 May 2023 01:58:49 -0700 (PDT) From: Peng Zhang <zhangpeng.00@bytedance.com> To: Liam.Howlett@oracle.com Cc: akpm@linux-foundation.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org, maple-tree@lists.infradead.org, Peng Zhang <zhangpeng.00@bytedance.com> Subject: [PATCH v2 01/10] maple_tree: Rework mtree_alloc_{range,rrange}() Date: Wed, 17 May 2023 16:58:00 +0800 Message-Id: <20230517085809.86522-2-zhangpeng.00@bytedance.com> X-Mailer: git-send-email 2.37.0 (Apple Git-136) In-Reply-To: <20230517085809.86522-1-zhangpeng.00@bytedance.com> References: <20230517085809.86522-1-zhangpeng.00@bytedance.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,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: <linux-kernel.vger.kernel.org> X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1766132857336965714?= X-GMAIL-MSGID: =?utf-8?q?1766132857336965714?= |
Series |
Clean ups for maple tree
|
|
Commit Message
Peng Zhang
May 17, 2023, 8:58 a.m. UTC
Use mas_empty_area{_rev}() to refactor mtree_alloc_{range,rrange}()
Signed-off-by: Peng Zhang <zhangpeng.00@bytedance.com>
---
lib/maple_tree.c | 63 ++++++++++++++++++++++++------------------------
1 file changed, 32 insertions(+), 31 deletions(-)
Comments
* Peng Zhang <zhangpeng.00@bytedance.com> [230517 04:58]: > Use mas_empty_area{_rev}() to refactor mtree_alloc_{range,rrange}() > > Signed-off-by: Peng Zhang <zhangpeng.00@bytedance.com> > --- > lib/maple_tree.c | 63 ++++++++++++++++++++++++------------------------ > 1 file changed, 32 insertions(+), 31 deletions(-) > > diff --git a/lib/maple_tree.c b/lib/maple_tree.c > index 4eb220008f72..e1820e90f167 100644 > --- a/lib/maple_tree.c > +++ b/lib/maple_tree.c > @@ -6493,32 +6493,31 @@ int mtree_alloc_range(struct maple_tree *mt, unsigned long *startp, > { > int ret = 0; > > - MA_STATE(mas, mt, min, min); > + MA_STATE(mas, mt, 0, 0); > if (!mt_is_alloc(mt)) > return -EINVAL; > > if (WARN_ON_ONCE(mt_is_reserved(entry))) > return -EINVAL; > > - if (min > max) > - return -EINVAL; > - > - if (max < size) > - return -EINVAL; > - > - if (!size) > - return -EINVAL; > - > mtree_lock(mt); > retry: > - mas.offset = 0; > - mas.index = min; > - mas.last = max - size + 1; > - ret = mas_alloc(&mas, entry, size, startp); > - if (mas_nomem(&mas, gfp)) > - goto retry; > - > + ret = mas_empty_area(&mas, min, max, size); > + if (!ret) { > + mas_insert(&mas, entry); > + /* > + * mas_nomem() may release the lock, causing the allocated area > + * to be unavailable, so try to allocate a free area again. > + */ > + if (mas_nomem(&mas, gfp)) > + goto retry; > + } > mtree_unlock(mt); > + if (!ret) { Checking for a mas_is_err() should probably be outside if (!ret) statement. If mas_insert() returns something besides ENOMEM, we will not detect the error. I'm not sure if this is possible today since this should never return an -EEXISTS, but having it this way doesn't add much to the overhead. > + if (mas_is_err(&mas)) > + return xa_err(mas.node); > + *startp = mas.index; > + } > return ret; > } > EXPORT_SYMBOL(mtree_alloc_range); > @@ -6529,29 +6528,31 @@ int mtree_alloc_rrange(struct maple_tree *mt, unsigned long *startp, > { > int ret = 0; > > - MA_STATE(mas, mt, min, max - size + 1); > + MA_STATE(mas, mt, 0, 0); > if (!mt_is_alloc(mt)) > return -EINVAL; > > if (WARN_ON_ONCE(mt_is_reserved(entry))) > return -EINVAL; > > - if (min > max) > - return -EINVAL; > - > - if (max < size - 1) > - return -EINVAL; > - > - if (!size) > - return -EINVAL; > - > mtree_lock(mt); > retry: > - ret = mas_rev_alloc(&mas, min, max, entry, size, startp); > - if (mas_nomem(&mas, gfp)) > - goto retry; > - > + ret = mas_empty_area_rev(&mas, min, max, size); > + if (!ret) { > + mas_insert(&mas, entry); > + /* > + * mas_nomem() may release the lock, causing the allocated area > + * to be unavailable, so try to allocate a free area again. > + */ > + if (mas_nomem(&mas, gfp)) > + goto retry; > + } > mtree_unlock(mt); > + if (!ret) { Same here. > + if (mas_is_err(&mas)) > + return xa_err(mas.node); > + *startp = mas.index; > + } > return ret; > } > EXPORT_SYMBOL(mtree_alloc_rrange); > -- > 2.20.1 >
在 2023/5/18 02:17, Liam R. Howlett 写道: > * Peng Zhang <zhangpeng.00@bytedance.com> [230517 04:58]: >> Use mas_empty_area{_rev}() to refactor mtree_alloc_{range,rrange}() >> >> Signed-off-by: Peng Zhang <zhangpeng.00@bytedance.com> >> --- >> lib/maple_tree.c | 63 ++++++++++++++++++++++++------------------------ >> 1 file changed, 32 insertions(+), 31 deletions(-) >> >> diff --git a/lib/maple_tree.c b/lib/maple_tree.c >> index 4eb220008f72..e1820e90f167 100644 >> --- a/lib/maple_tree.c >> +++ b/lib/maple_tree.c >> @@ -6493,32 +6493,31 @@ int mtree_alloc_range(struct maple_tree *mt, unsigned long *startp, >> { >> int ret = 0; >> >> - MA_STATE(mas, mt, min, min); >> + MA_STATE(mas, mt, 0, 0); >> if (!mt_is_alloc(mt)) >> return -EINVAL; >> >> if (WARN_ON_ONCE(mt_is_reserved(entry))) >> return -EINVAL; >> >> - if (min > max) >> - return -EINVAL; >> - >> - if (max < size) >> - return -EINVAL; >> - >> - if (!size) >> - return -EINVAL; >> - >> mtree_lock(mt); >> retry: >> - mas.offset = 0; >> - mas.index = min; >> - mas.last = max - size + 1; >> - ret = mas_alloc(&mas, entry, size, startp); >> - if (mas_nomem(&mas, gfp)) >> - goto retry; >> - >> + ret = mas_empty_area(&mas, min, max, size); >> + if (!ret) { >> + mas_insert(&mas, entry); >> + /* >> + * mas_nomem() may release the lock, causing the allocated area >> + * to be unavailable, so try to allocate a free area again. >> + */ >> + if (mas_nomem(&mas, gfp)) >> + goto retry; >> + } >> mtree_unlock(mt); >> + if (!ret) { > > Checking for a mas_is_err() should probably be outside if (!ret) > statement. If mas_insert() returns something besides ENOMEM, we will > not detect the error. I'm not sure if this is possible today since this > should never return an -EEXISTS, but having it this way doesn't add much > to the overhead. I don't think there will be error that can't be detected here. In fact, there are two sources of errors: 1. mas_empty_area(), the error number is in the variable ret, and may also be in mas->node, but ret must contain all errors. 2. mas_insert(), the error number is in mas->node When we check errors, we should first check errors from mas_empty_area(). If there is no error in mas_empty_area(), we will check errors from mas_insert(). So, mas_is_err() is inside the if (!ret) statement, no problem here. Of course, even if mas_insert() returns -EEXISTS, it can be detected under the current encoding, because "if (!ret)" is true in this case. But I don't think this can happen, if it happens, it's a bug of maple tree. I don't think it's good to put mas_is_err() outside, because the error number stored in mas->node may come from mas_empty_area(). We should use the ret variable to detect the error from mas_empty_area() first. > >> + if (mas_is_err(&mas)) >> + return xa_err(mas.node); >> + *startp = mas.index; >> + } >> return ret; >> } >> EXPORT_SYMBOL(mtree_alloc_range); >> @@ -6529,29 +6528,31 @@ int mtree_alloc_rrange(struct maple_tree *mt, unsigned long *startp, >> { >> int ret = 0; >> >> - MA_STATE(mas, mt, min, max - size + 1); >> + MA_STATE(mas, mt, 0, 0); >> if (!mt_is_alloc(mt)) >> return -EINVAL; >> >> if (WARN_ON_ONCE(mt_is_reserved(entry))) >> return -EINVAL; >> >> - if (min > max) >> - return -EINVAL; >> - >> - if (max < size - 1) >> - return -EINVAL; >> - >> - if (!size) >> - return -EINVAL; >> - >> mtree_lock(mt); >> retry: >> - ret = mas_rev_alloc(&mas, min, max, entry, size, startp); >> - if (mas_nomem(&mas, gfp)) >> - goto retry; >> - >> + ret = mas_empty_area_rev(&mas, min, max, size); >> + if (!ret) { >> + mas_insert(&mas, entry); >> + /* >> + * mas_nomem() may release the lock, causing the allocated area >> + * to be unavailable, so try to allocate a free area again. >> + */ >> + if (mas_nomem(&mas, gfp)) >> + goto retry; >> + } >> mtree_unlock(mt); >> + if (!ret) { > > Same here. > >> + if (mas_is_err(&mas)) >> + return xa_err(mas.node); >> + *startp = mas.index; >> + } >> return ret; >> } >> EXPORT_SYMBOL(mtree_alloc_rrange); >> -- >> 2.20.1 >>
* Peng Zhang <zhangpeng.00@bytedance.com> [230518 02:10]: > > > 在 2023/5/18 02:17, Liam R. Howlett 写道: > > * Peng Zhang <zhangpeng.00@bytedance.com> [230517 04:58]: > > > Use mas_empty_area{_rev}() to refactor mtree_alloc_{range,rrange}() > > > > > > Signed-off-by: Peng Zhang <zhangpeng.00@bytedance.com> > > > --- > > > lib/maple_tree.c | 63 ++++++++++++++++++++++++------------------------ > > > 1 file changed, 32 insertions(+), 31 deletions(-) > > > > > > diff --git a/lib/maple_tree.c b/lib/maple_tree.c > > > index 4eb220008f72..e1820e90f167 100644 > > > --- a/lib/maple_tree.c > > > +++ b/lib/maple_tree.c > > > @@ -6493,32 +6493,31 @@ int mtree_alloc_range(struct maple_tree *mt, unsigned long *startp, > > > { > > > int ret = 0; > > > - MA_STATE(mas, mt, min, min); > > > + MA_STATE(mas, mt, 0, 0); > > > if (!mt_is_alloc(mt)) > > > return -EINVAL; > > > if (WARN_ON_ONCE(mt_is_reserved(entry))) > > > return -EINVAL; > > > - if (min > max) > > > - return -EINVAL; > > > - > > > - if (max < size) > > > - return -EINVAL; > > > - > > > - if (!size) > > > - return -EINVAL; > > > - > > > mtree_lock(mt); > > > retry: > > > - mas.offset = 0; > > > - mas.index = min; > > > - mas.last = max - size + 1; > > > - ret = mas_alloc(&mas, entry, size, startp); > > > - if (mas_nomem(&mas, gfp)) > > > - goto retry; > > > - > > > + ret = mas_empty_area(&mas, min, max, size); > > > + if (!ret) { > > > + mas_insert(&mas, entry); > > > + /* > > > + * mas_nomem() may release the lock, causing the allocated area > > > + * to be unavailable, so try to allocate a free area again. > > > + */ > > > + if (mas_nomem(&mas, gfp)) > > > + goto retry; > > > + } > > > mtree_unlock(mt); > > > + if (!ret) { > > > > Checking for a mas_is_err() should probably be outside if (!ret) > > statement. If mas_insert() returns something besides ENOMEM, we will > > not detect the error. I'm not sure if this is possible today since this > > should never return an -EEXISTS, but having it this way doesn't add much > > to the overhead. > I don't think there will be error that can't be detected here. > In fact, there are two sources of errors: > > 1. mas_empty_area(), the error number is in the variable ret, > and may also be in mas->node, but ret must contain all errors. > > 2. mas_insert(), the error number is in mas->node > > When we check errors, we should first check errors from > mas_empty_area(). If there is no error in mas_empty_area(), we > will check errors from mas_insert(). > So, mas_is_err() is inside the if (!ret) statement, no problem here. > > Of course, even if mas_insert() returns -EEXISTS, it can be detected > under the current encoding, because "if (!ret)" is true in this case. > But I don't think this can happen, if it happens, it's a bug of maple > tree. Right, thanks. > > I don't think it's good to put mas_is_err() outside, because the error > number stored in mas->node may come from mas_empty_area(). We should use > the ret variable to detect the error from mas_empty_area() first. Yeah, I would structure it as a 'goto' to undo the locking to avoid the if (!ret) nesting, and move the unlock to just before the return: if (ret) goto unlock; ... *startp = mas.index; unlock: mtree_unlock(mt); return ret; mas_empty_area() is an external interface and so already decodes the error in the node using mas_is_err(), so this can be dropped completely. I don't think holding the lock for the one extra assignment will make a difference. > > > > > + if (mas_is_err(&mas)) > > > + return xa_err(mas.node); > > > + *startp = mas.index; > > > + } > > > return ret; > > > } > > > EXPORT_SYMBOL(mtree_alloc_range); > > > @@ -6529,29 +6528,31 @@ int mtree_alloc_rrange(struct maple_tree *mt, unsigned long *startp, > > > { > > > int ret = 0; > > > - MA_STATE(mas, mt, min, max - size + 1); > > > + MA_STATE(mas, mt, 0, 0); > > > if (!mt_is_alloc(mt)) > > > return -EINVAL; > > > if (WARN_ON_ONCE(mt_is_reserved(entry))) > > > return -EINVAL; > > > - if (min > max) > > > - return -EINVAL; > > > - > > > - if (max < size - 1) > > > - return -EINVAL; > > > - > > > - if (!size) > > > - return -EINVAL; > > > - > > > mtree_lock(mt); > > > retry: > > > - ret = mas_rev_alloc(&mas, min, max, entry, size, startp); > > > - if (mas_nomem(&mas, gfp)) > > > - goto retry; > > > - > > > + ret = mas_empty_area_rev(&mas, min, max, size); > > > + if (!ret) { > > > + mas_insert(&mas, entry); > > > + /* > > > + * mas_nomem() may release the lock, causing the allocated area > > > + * to be unavailable, so try to allocate a free area again. > > > + */ > > > + if (mas_nomem(&mas, gfp)) > > > + goto retry; > > > + } > > > mtree_unlock(mt); > > > + if (!ret) { > > > > Same here. > > > > > + if (mas_is_err(&mas)) > > > + return xa_err(mas.node); > > > + *startp = mas.index; > > > + } > > > return ret; > > > } > > > EXPORT_SYMBOL(mtree_alloc_rrange); > > > -- > > > 2.20.1 > > >
diff --git a/lib/maple_tree.c b/lib/maple_tree.c index 4eb220008f72..e1820e90f167 100644 --- a/lib/maple_tree.c +++ b/lib/maple_tree.c @@ -6493,32 +6493,31 @@ int mtree_alloc_range(struct maple_tree *mt, unsigned long *startp, { int ret = 0; - MA_STATE(mas, mt, min, min); + MA_STATE(mas, mt, 0, 0); if (!mt_is_alloc(mt)) return -EINVAL; if (WARN_ON_ONCE(mt_is_reserved(entry))) return -EINVAL; - if (min > max) - return -EINVAL; - - if (max < size) - return -EINVAL; - - if (!size) - return -EINVAL; - mtree_lock(mt); retry: - mas.offset = 0; - mas.index = min; - mas.last = max - size + 1; - ret = mas_alloc(&mas, entry, size, startp); - if (mas_nomem(&mas, gfp)) - goto retry; - + ret = mas_empty_area(&mas, min, max, size); + if (!ret) { + mas_insert(&mas, entry); + /* + * mas_nomem() may release the lock, causing the allocated area + * to be unavailable, so try to allocate a free area again. + */ + if (mas_nomem(&mas, gfp)) + goto retry; + } mtree_unlock(mt); + if (!ret) { + if (mas_is_err(&mas)) + return xa_err(mas.node); + *startp = mas.index; + } return ret; } EXPORT_SYMBOL(mtree_alloc_range); @@ -6529,29 +6528,31 @@ int mtree_alloc_rrange(struct maple_tree *mt, unsigned long *startp, { int ret = 0; - MA_STATE(mas, mt, min, max - size + 1); + MA_STATE(mas, mt, 0, 0); if (!mt_is_alloc(mt)) return -EINVAL; if (WARN_ON_ONCE(mt_is_reserved(entry))) return -EINVAL; - if (min > max) - return -EINVAL; - - if (max < size - 1) - return -EINVAL; - - if (!size) - return -EINVAL; - mtree_lock(mt); retry: - ret = mas_rev_alloc(&mas, min, max, entry, size, startp); - if (mas_nomem(&mas, gfp)) - goto retry; - + ret = mas_empty_area_rev(&mas, min, max, size); + if (!ret) { + mas_insert(&mas, entry); + /* + * mas_nomem() may release the lock, causing the allocated area + * to be unavailable, so try to allocate a free area again. + */ + if (mas_nomem(&mas, gfp)) + goto retry; + } mtree_unlock(mt); + if (!ret) { + if (mas_is_err(&mas)) + return xa_err(mas.node); + *startp = mas.index; + } return ret; } EXPORT_SYMBOL(mtree_alloc_rrange);