Received: by 2002:a05:6358:3188:b0:123:57c1:9b43 with SMTP id q8csp792110rwd; Thu, 15 Jun 2023 02:01:44 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ7IOx30AhABUxPRbK5syJw9PQ03notVh8g4+XwrFzLgGX0/SYIN7N9Cv9/t+Tuf8fqMWZwP X-Received: by 2002:a17:907:8a08:b0:982:870f:9e8f with SMTP id sc8-20020a1709078a0800b00982870f9e8fmr2592203ejc.62.1686819704490; Thu, 15 Jun 2023 02:01:44 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1686819704; cv=none; d=google.com; s=arc-20160816; b=QGtN1oxYlYaxsR/lefOSd7sx7I3Jir2fAiE54gSA4PmRCsvEFOcdhd/3NnseY35+X0 g9EoDzz0w4qZ6OXmdwsKlanI/tWflZZfmMpUIEU3Rq+fJ1iPqYEsSMl2Xuhs1y0BxH/1 W4Td7JXULoYl9+NWUlYewgJE3ca8iLPAoDydeo+doA8IVMQo28qgP11vBJ//6Bgis/WT jnIrb9RAtaX+miQYs/5R2jFx6h8WEmSNNIyZeSINcDBh6iYjyHLNJZQ4hN6AaWKiwcX4 c3FtK/iWtUYZbtGV45MWxOgbnXbITlK7FDFK+CiX3WFcbkEL1itblsQA+vZEy6aMrHRY X/Pg== 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=XtXCNMCToSbvY4lMnhwDqjdw8KEp/J31bkp75I6UIro=; b=s0wET0SjgpTRVWuDs+EVOFo3mPa662dghu2kk+MrtNySQuqbi/e/XvpzzmV3W1lUz3 lzC+VVUHbHXpwKsk47a5qzkgJEjCBq4ZjY/ikXHHVrEpn3OfErYEJEGR26Y+Cd1lck1z QFWyZjKOkUAzE3psOgayWN94IxnJtM0GHY3d6qTU/JqA11IDe0kltJ0YjQ5rU78u18qa V+HG4MhT1uOg0f+5oX7Nqy/+mic5YAXGkLEEFAJOrCKjxxH9cPE+QeLjtCbK+HNcYiHc yry2TF+cMc7vaShqxDZejkNqX8Iu+zfPtUDaWKCoQDXC+M/fjw5wViWccGc9lSoZphUK 2r+A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@bytedance.com header.s=google header.b=l3TDRxu1; 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 t17-20020a1709064f1100b00977ccc61d6fsi9192530eju.21.2023.06.15.02.01.18; Thu, 15 Jun 2023 02:01: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=@bytedance.com header.s=google header.b=l3TDRxu1; 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 S245239AbjFOIoJ (ORCPT + 99 others); Thu, 15 Jun 2023 04:44:09 -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 S244696AbjFOInj (ORCPT ); Thu, 15 Jun 2023 04:43:39 -0400 Received: from mail-pf1-x431.google.com (mail-pf1-x431.google.com [IPv6:2607:f8b0:4864:20::431]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id ABEFF297B for ; Thu, 15 Jun 2023 01:43:18 -0700 (PDT) Received: by mail-pf1-x431.google.com with SMTP id d2e1a72fcca58-666aa1b79a3so80635b3a.3 for ; Thu, 15 Jun 2023 01:43:18 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance.com; s=google; t=1686818598; x=1689410598; 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=XtXCNMCToSbvY4lMnhwDqjdw8KEp/J31bkp75I6UIro=; b=l3TDRxu1IZeyZNvWGdJOFqtHd+LYhlWJeVIRmuLAkF0iPL/Q4KJgpGWeh7yt2KT390 KzCL+z04k59mTjKNc1qkFngmiEIN7AEVfPul0AZh9evDVDjlwvB62qsNc0qk1cODg5op c5ZcfjgPq4iTWVmfJGSM2rtBKFqryJoMm4+M8PPPckeA9qg2gVriJbfcAwUAQBPIeVXu YMm0+kwc4VoVWFYaesVj4e8F5hPwX8K5ZwSWddqC5b8kzgp76myGhJZxg9jm+Ptz3mtX cai+VRJ32w9/dUu3CoeZE60WbitOBrdrPjWnPzWbuqC7KE5tPbmfNbKrOtB+3Y5qgmcQ QROg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1686818598; x=1689410598; 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=XtXCNMCToSbvY4lMnhwDqjdw8KEp/J31bkp75I6UIro=; b=fjidC0WvjVjlwAJUzUig0kyFOPEz2pucLtGXW0uQoBs9OqZd4Js+1ksMMo+TRAZqAE NU3VLpZ/931V0eOpQSAzimHr6eJX+I2aicsD9MTEFCBBt+24wD2jq6RrLfsXK0gjtgxX AGvZIHeJQBs3sA+GJdWbid0/CPDYSnHM9gwfipMUWQKlgZthNaEyw5z2eg4n6VGOAgtJ n9IDZjR8yWJjnO/pKjySyOysnOXqzS1TwKf5PTBygcanYBbn4E1jfmhHE+sxPCJ+A+M2 iUzZ3fBSXGIqSNU5lJ6evQbTmgaG+1A1Zgej6tCU/1PLg12hqh4Q2J4gE0ldxXd2KaHd 6ymA== X-Gm-Message-State: AC+VfDyKE0QvaHTisEraSex+sKO60Kw+1HRr8fWITbzmBiHpBtwUDwvA BMtPCvtc4lPWn5EipVOy6pn9EA== X-Received: by 2002:a05:6a00:391e:b0:643:aa8d:8cd7 with SMTP id fh30-20020a056a00391e00b00643aa8d8cd7mr4292787pfb.32.1686818598107; Thu, 15 Jun 2023 01:43:18 -0700 (PDT) Received: from GL4FX4PXWL.bytedance.net ([139.177.225.249]) by smtp.gmail.com with ESMTPSA id i21-20020aa78b55000000b0064fe06fe712sm11139783pfd.129.2023.06.15.01.43.15 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Thu, 15 Jun 2023 01:43:17 -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 3/4] maple_tree: optimize mas_wr_append(), also improve duplicating VMAs Date: Thu, 15 Jun 2023 16:43:00 +0800 Message-Id: <20230615084301.97701-4-zhangpeng.00@bytedance.com> X-Mailer: git-send-email 2.37.0 (Apple Git-136) In-Reply-To: <20230615084301.97701-1-zhangpeng.00@bytedance.com> References: <20230615084301.97701-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_NONE,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 When the new range can be completely covered by the original last range without touching the boundaries on both sides, two new entries can be appended to the end as a fast path. We update the original last pivot at the end, and the newly appended two entries will not be accessed before this, so it is also safe in RCU mode. This is useful for sequential insertion, which is what we do in dup_mmap(). Enabling BENCH_FORK in test_maple_tree and just running bench_forking() gives the following time-consuming numbers: before: after: 17,874.83 msec 15,738.38 msec It shows about a 12% performance improvement for duplicating VMAs. Signed-off-by: Peng Zhang Reviewed-by: Liam R. Howlett --- lib/maple_tree.c | 33 ++++++++++++++++++++++----------- 1 file changed, 22 insertions(+), 11 deletions(-) diff --git a/lib/maple_tree.c b/lib/maple_tree.c index d2799c69a669..da4af6743b30 100644 --- a/lib/maple_tree.c +++ b/lib/maple_tree.c @@ -4202,10 +4202,10 @@ static inline unsigned char mas_wr_new_end(struct ma_wr_state *wr_mas) * * Return: True if appended, false otherwise */ -static inline bool mas_wr_append(struct ma_wr_state *wr_mas) +static inline bool mas_wr_append(struct ma_wr_state *wr_mas, + unsigned char new_end) { unsigned char end = wr_mas->node_end; - unsigned char new_end = end + 1; struct ma_state *mas = wr_mas->mas; unsigned char node_pivots = mt_pivots[wr_mas->type]; @@ -4217,16 +4217,27 @@ static inline bool mas_wr_append(struct ma_wr_state *wr_mas) ma_set_meta(wr_mas->node, maple_leaf_64, 0, new_end); } - if (mas->last == wr_mas->r_max) { - /* Append to end of range */ - rcu_assign_pointer(wr_mas->slots[new_end], wr_mas->entry); - wr_mas->pivots[end] = mas->index - 1; - mas->offset = new_end; + if (new_end == wr_mas->node_end + 1) { + if (mas->last == wr_mas->r_max) { + /* Append to end of range */ + rcu_assign_pointer(wr_mas->slots[new_end], + wr_mas->entry); + wr_mas->pivots[end] = mas->index - 1; + mas->offset = new_end; + } else { + /* Append to start of range */ + rcu_assign_pointer(wr_mas->slots[new_end], + wr_mas->content); + wr_mas->pivots[end] = mas->last; + rcu_assign_pointer(wr_mas->slots[end], wr_mas->entry); + } } else { - /* Append to start of range */ + /* Append to the range without touching any boundaries. */ rcu_assign_pointer(wr_mas->slots[new_end], wr_mas->content); - wr_mas->pivots[end] = mas->last; - rcu_assign_pointer(wr_mas->slots[end], wr_mas->entry); + wr_mas->pivots[end + 1] = mas->last; + rcu_assign_pointer(wr_mas->slots[end + 1], wr_mas->entry); + wr_mas->pivots[end] = mas->index - 1; + mas->offset = end + 1; } if (!wr_mas->content || !wr_mas->entry) @@ -4273,7 +4284,7 @@ static inline void mas_wr_modify(struct ma_wr_state *wr_mas) goto slow_path; /* Attempt to append */ - if (new_end == wr_mas->node_end + 1 && mas_wr_append(wr_mas)) + if (mas_wr_append(wr_mas, new_end)) return; if (new_end == wr_mas->node_end && mas_wr_slot_store(wr_mas)) -- 2.20.1