From patchwork Mon May 22 05:06:47 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Peng Zhang X-Patchwork-Id: 13249766 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id C9F09C77B73 for ; Mon, 22 May 2023 05:07:24 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 678D090000C; Mon, 22 May 2023 01:07:24 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 62930900002; Mon, 22 May 2023 01:07:24 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 51B4F90000C; Mon, 22 May 2023 01:07:24 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id 43EFA900002 for ; Mon, 22 May 2023 01:07:24 -0400 (EDT) Received: from smtpin01.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id 0FD2BC0420 for ; Mon, 22 May 2023 05:07:24 +0000 (UTC) X-FDA: 80816707608.01.3F96749 Received: from mail-pf1-f179.google.com (mail-pf1-f179.google.com [209.85.210.179]) by imf19.hostedemail.com (Postfix) with ESMTP id 3AF3B1A000F for ; Mon, 22 May 2023 05:07:22 +0000 (UTC) Authentication-Results: imf19.hostedemail.com; dkim=pass header.d=bytedance.com header.s=google header.b=E+sf9AoJ; dmarc=pass (policy=quarantine) header.from=bytedance.com; spf=pass (imf19.hostedemail.com: domain of zhangpeng.00@bytedance.com designates 209.85.210.179 as permitted sender) smtp.mailfrom=zhangpeng.00@bytedance.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1684732042; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=ryAjJK/jvO3d1/ZbH/fm/OkRmr2azDVw5I6QoB1NED8=; b=LPdf1s20fM9QXXWV/qWQ2yXtWBdDWFZKqcqhGZ2fWz4YFrIS6cTyEx6GAlFtrvfPnV/xpm TMqB+cmLZNHZ7Koztwfu6LJVBXam3UsbsuuA7wURO51BcCbqjxHMidZVujEnadaD+xeb6n JmFWOPfAsjydbnXEAbBZ+V+cllj0zis= ARC-Authentication-Results: i=1; imf19.hostedemail.com; dkim=pass header.d=bytedance.com header.s=google header.b=E+sf9AoJ; dmarc=pass (policy=quarantine) header.from=bytedance.com; spf=pass (imf19.hostedemail.com: domain of zhangpeng.00@bytedance.com designates 209.85.210.179 as permitted sender) smtp.mailfrom=zhangpeng.00@bytedance.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1684732042; a=rsa-sha256; cv=none; b=GRiRFJXc/SThNx37taoOXiMHcg2y5kvMvPe4gilZeRKW0ttojkcBLjMwShENEU/MOeu9WK wEZRjMsjEis2RyiX4sjU/o/asJy6NAvSDgmQwg9vKidxdy6qLR6HdHr8wDLHFc1j/teh3T CyUKQhrYi4XuF3ty74oEjLgo+uvtXi4= Received: by mail-pf1-f179.google.com with SMTP id d2e1a72fcca58-64d2467d640so4440449b3a.1 for ; Sun, 21 May 2023 22:07:21 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance.com; s=google; t=1684732041; x=1687324041; 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=ryAjJK/jvO3d1/ZbH/fm/OkRmr2azDVw5I6QoB1NED8=; b=E+sf9AoJVl+JV6FsfkR4Ym5XfHy/qlWHgn7H3ghjaIqtwrmDk4FnwTgtCboieHks5a 7Xju63Rz4/hQXtTjifn3Q9wRx22XGUz+pCJWITfM6TaSYKsoLU1m7GIB/8cxEfraWRhS 8ce7qTdNdx0Rx5AX64dHcGo1GeiRMKueARhVfIFMquyHtvURi2LrwtmBx+f+ZN0H25Jn IR5e5SLGTGTpYPbpRZriNanszs4xQ2Sv8FTZCoWCx2LI56hrA+MREB3TjANB0qTaIm9G tPlFNj6QITqQQlWtPss9HM0QZsSkFNpWPI7R91rjhIRzTZw+TQz16JYxWv25eGrOJKY7 bY8A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1684732041; x=1687324041; 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=ryAjJK/jvO3d1/ZbH/fm/OkRmr2azDVw5I6QoB1NED8=; b=AkszpQJWlawSn/gNnPZxTdZSo+8Wh0Dc0zfjVd60QVTgZPCc8+4Zb1SnxWrAizETuF UCH7txyCjvTXDhXoJmJvfoC5vpuPJH24HKKJMzGC9yZKN/DqrscyzSNVi07kcX6lrrdp 9DzYVNsZa23do86W5XoJOhRY8x6+mkqlHx+aib14vhOqX3Bvou8XnuyTVsXyAJDi7jp8 3c915v4k3jkGQikhkG8vvNP5n3y8Hrok7iwYxfnOGhifRSI5rpwI8icDep7t1WvjYB2F 5a59Q52xMnKjwDVvpsnEMYHbExNXK13Fkya2TkKL76bzUN5J7hgM7p9qPB/Kl62tbgjS O1cg== X-Gm-Message-State: AC+VfDzsHluNfdFsqcoUjSNOvwkc5x3abvmZfLtPZiJl+mWjbcNthviR MwU3IKL3L0e7a5fFcROqdWSeIA== X-Google-Smtp-Source: ACHHUZ6Xjaki5a0wpYv6HPrx9PPhoRm0BZ5h3RO2p2wQiWSIZBHDnR5shbpMqf2VKAtyiIKtmnXE3Q== X-Received: by 2002:a05:6a00:845:b0:634:7ba3:d140 with SMTP id q5-20020a056a00084500b006347ba3d140mr13038011pfk.15.1684732040942; Sun, 21 May 2023 22:07:20 -0700 (PDT) Received: from GL4FX4PXWL.bytedance.net ([139.177.225.249]) by smtp.gmail.com with ESMTPSA id t10-20020a62ea0a000000b0063b5776b073sm3247519pfh.117.2023.05.21.22.07.18 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Sun, 21 May 2023 22:07:20 -0700 (PDT) From: Peng Zhang 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 Subject: [PATCH v3 01/10] maple_tree: Rework mtree_alloc_{range,rrange}() Date: Mon, 22 May 2023 13:06:47 +0800 Message-Id: <20230522050656.96215-2-zhangpeng.00@bytedance.com> X-Mailer: git-send-email 2.37.0 (Apple Git-136) In-Reply-To: <20230522050656.96215-1-zhangpeng.00@bytedance.com> References: <20230522050656.96215-1-zhangpeng.00@bytedance.com> MIME-Version: 1.0 X-Rspam-User: X-Stat-Signature: 9uad5amtx4o4og5gd5z337p8cahmsc8q X-Rspamd-Server: rspam07 X-Rspamd-Queue-Id: 3AF3B1A000F X-HE-Tag: 1684732042-242092 X-HE-Meta: U2FsdGVkX1+Y8nfEqFYYqGJYIr3XsXqRduJ/hYfn6+IQDgKhBmCQroOGqD+U+3mKfViP2tBoMxoQK1qdcG9vA74FNlwdWA7dbTPbDLWUEui8b8ChGPe6vbnViRSaKO39nyko6PxmmnDhCqvQnDm5wXEyR6kgSNDWepSNnaTNBWJx8RRQgIie1mY//JzW/QdRvANduqWlNP1IFLL1FUR3fIUU/VIHN85oHqEa7xv7BWO7KZJAwDXo0CWl7K6MMt+RkNX1OaL4IKDtTHZqWTC4eqbaq1fNRyp3A1aFx58ewvhpKF3CacecL3VKYiOuI6Svey+CdyvmQyuzzfP3nzvJr5Dn5boOwPOiZYkviTG1XyrUsFGR10AQiN29BeCnr5WTLzKVmuT/GrADUGRGt+PVo4Ir0dw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Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: Use mas_empty_area{_rev}() to refactor mtree_alloc_{range,rrange}() Signed-off-by: Peng Zhang Reviewed-by: Liam R. Howlett --- lib/maple_tree.c | 57 +++++++++++++++++++++++++++--------------------- 1 file changed, 32 insertions(+), 25 deletions(-) diff --git a/lib/maple_tree.c b/lib/maple_tree.c index 4eb220008f72..96d102d60b4e 100644 --- a/lib/maple_tree.c +++ b/lib/maple_tree.c @@ -6493,31 +6493,33 @@ 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); + ret = mas_empty_area(&mas, min, max, size); + if (ret) + goto unlock; + + 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; + if (mas_is_err(&mas)) + ret = xa_err(mas.node); + else + *startp = mas.index; + +unlock: mtree_unlock(mt); return ret; } @@ -6529,28 +6531,33 @@ 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); + ret = mas_empty_area_rev(&mas, min, max, size); + if (ret) + goto unlock; + + 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; + if (mas_is_err(&mas)) + ret = xa_err(mas.node); + else + *startp = mas.index; + +unlock: mtree_unlock(mt); return ret; }