From patchwork Fri Oct 27 03:38:35 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Peng Zhang X-Patchwork-Id: 13438093 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 9E926C25B47 for ; Fri, 27 Oct 2023 03:39:04 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 92F636B0357; Thu, 26 Oct 2023 23:39:03 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 8DF356B0358; Thu, 26 Oct 2023 23:39:03 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 7A6916B0386; Thu, 26 Oct 2023 23:39:03 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id 6AD966B0357 for ; Thu, 26 Oct 2023 23:39:03 -0400 (EDT) Received: from smtpin23.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id 195B9141048 for ; Fri, 27 Oct 2023 03:39:03 +0000 (UTC) X-FDA: 81389835366.23.0C5820A Received: from mail-pj1-f41.google.com (mail-pj1-f41.google.com [209.85.216.41]) by imf27.hostedemail.com (Postfix) with ESMTP id 914D74000B for ; Fri, 27 Oct 2023 03:39:00 +0000 (UTC) Authentication-Results: imf27.hostedemail.com; dkim=pass header.d=bytedance.com header.s=google header.b=FI029HkN; dmarc=pass (policy=quarantine) header.from=bytedance.com; spf=pass (imf27.hostedemail.com: domain of zhangpeng.00@bytedance.com designates 209.85.216.41 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=1698377941; 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:references:dkim-signature; bh=MFgGxXvNH1IVbbEq5A6jp4J1ULe7/977xw27+McLJW4=; b=pNipw0ozchFYFJEjI6kwuamJ/hfYArGaztHHH70l3OiXent3tUcm3uwQwK+ETaSwXP83p4 DQP2lcR2yB9AV9EBFZTOG5F7fbXTN8uvvjlPLv1widyXldgb4fZGk29JqgizkchxNLIH7z j9FbrjS8dtP4Sji+HYuvgr3+ITVGtjQ= ARC-Authentication-Results: i=1; imf27.hostedemail.com; dkim=pass header.d=bytedance.com header.s=google header.b=FI029HkN; dmarc=pass (policy=quarantine) header.from=bytedance.com; spf=pass (imf27.hostedemail.com: domain of zhangpeng.00@bytedance.com designates 209.85.216.41 as permitted sender) smtp.mailfrom=zhangpeng.00@bytedance.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1698377941; a=rsa-sha256; cv=none; b=8Z6YDVomOU2U84siQ3MqFQDepu1Bm/iUPwsT4isYbiwFGve7NNZJNw9/zidbHmTTbrMFJf WG5Uedn9Dh+32HxfBcH5gIQUMj1vQTH05V6aCpqJowLfL5uOEkLU5GMTGFSpAoHBM5x1ox nqAx85y3SlRzAO1uikC8t5W6UYRWmMc= Received: by mail-pj1-f41.google.com with SMTP id 98e67ed59e1d1-2800783bb19so578353a91.3 for ; Thu, 26 Oct 2023 20:39:00 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance.com; s=google; t=1698377939; x=1698982739; darn=kvack.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=MFgGxXvNH1IVbbEq5A6jp4J1ULe7/977xw27+McLJW4=; b=FI029HkN/tOYsuBygeGB4Ni8EF4kvsiZSX3Z1q8F9gE70DV7Nkef6Fmc4Pt7VRCKFR hofrgjvbTuKvSphPqCCBKm3FXZqCAoqEhH0gALvtGkrJ2XyMKESNnX+GMNnjWxC3qhxi i8TaEvOkzQ9Z+0sh3UNV50omfFGf86dzbfzrLiIOSWSWUgEuVihiVy6KL/ZmquK/cjWB DToSCr/IUKo9MTzYdfPF+hcosuVWuFzIyBWipg0zC3C/4OqGi8hdZjBm7y/fLxwQdTgg 8CH7Y5suHbBVo0mzPuWNf4NEC2slgD5weRl1/SK6uJa84PTw9hSdr9RsDQcLzN5bltY1 pXlA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1698377939; x=1698982739; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=MFgGxXvNH1IVbbEq5A6jp4J1ULe7/977xw27+McLJW4=; b=lHuBXZaTpoDX5PvjMi4WHPAZTCoDh2PvvncdjocfpEmmMJ1X0uImaZHR1HKiDVPq+C og19RRnAd2+ovoVURzDgm/37mDfy9Dt7Dp1to4eaS3aRW/SjgTzbt3Cw3raFNoXwR1ox +NG/gNgbFVF0oxRzCxpqE/1Ulk4ffsc0WgPcSYP8+HzvShDRd8MgOgJUjTA6O4hWr3Dl ohRonhKOPEe14jOe8ivR1lo9oB0mXrxpKdE3wYJc18HJUEV//obSXrJcbDbAPPrUk9vo ttWmDKidYNWh7ziOUeqVn46gny3n20/FPsgGZvPxcOztBzmUWWTDI079uaz5E/YWoBlm 3JDA== X-Gm-Message-State: AOJu0YxKe4sYRVzWOGHi0FjXmHRxrbIn1mrP9BxlcN+3i8DhlMdXIvfw cER8/Om2NceL8RuqDNPCayxYPA== X-Google-Smtp-Source: AGHT+IEEmEZXvlSfsc+xnqK4JaSioeT+GyhDkRyFZipkY36OlXg+1cEEFCqP5RPa0hVBvmajvf6Few== X-Received: by 2002:a17:90a:fd01:b0:27f:ebfa:1112 with SMTP id cv1-20020a17090afd0100b0027febfa1112mr1401619pjb.5.1698377939105; Thu, 26 Oct 2023 20:38:59 -0700 (PDT) Received: from GL4FX4PXWL.bytedance.net ([203.208.189.7]) by smtp.gmail.com with ESMTPSA id ms19-20020a17090b235300b00267d9f4d340sm2345676pjb.44.2023.10.26.20.38.51 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Thu, 26 Oct 2023 20:38:58 -0700 (PDT) From: Peng Zhang To: Liam.Howlett@oracle.com, corbet@lwn.net, akpm@linux-foundation.org, willy@infradead.org, brauner@kernel.org, surenb@google.com, michael.christie@oracle.com, mjguzik@gmail.com, mathieu.desnoyers@efficios.com, npiggin@gmail.com, peterz@infradead.org, oliver.sang@intel.com, mst@redhat.com Cc: zhangpeng.00@bytedance.com, maple-tree@lists.infradead.org, linux-mm@kvack.org, linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, linux-fsdevel@vger.kernel.org Subject: [PATCH v7 00/10] Introduce __mt_dup() to improve the performance of fork() Date: Fri, 27 Oct 2023 11:38:35 +0800 Message-Id: <20231027033845.90608-1-zhangpeng.00@bytedance.com> X-Mailer: git-send-email 2.39.3 (Apple Git-145) MIME-Version: 1.0 X-Rspamd-Queue-Id: 914D74000B X-Rspam-User: X-Rspamd-Server: rspam04 X-Stat-Signature: 5z8muokjdtozr1gz8gg5pf5qk7q5iyom X-HE-Tag: 1698377940-581914 X-HE-Meta: U2FsdGVkX18ko9rM7LOWTRGUHbspioCHvZSN0MMiFZ3mOiza7Nu9YwyiTijObE3sSP2c/XkOGCb0lepr0NEzuOpQCunpeYJwwmyBAoHVMuHfM8rPm6awHhGMxYNXt/M+9Ek4kvpp8WV3h2d+UCIEbMkEuH+eqpqxq5R1ZX9oJy85kyioM6bxyQA7TcjsPeTGD2V2LpPsevPEwkRHdrmy6GRW+e+6Kmo93FP/ji21QBPKKZAvx0zxhszB+SRnKybw8SjaldNaGKjpnfYtfc5gDPqFi9YDK4Qj4UGeSeRic9ue3ghX84TvKkdHbFc1RzEq3VX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dcGktBHEkh2Iy59CQHO7oA8VZKlHcjwEy96tHC8GP6HU2GAf2Q7SsH1aweiZeWxiZBj8DDYIbhWJIuDFD2ZMgbl2MdqNeSyoGUaAgbQ0+tn2piTjCsqF1CGSCohNj9qL/SCX0mDIg6XgAs+2HG8OxAnGPgla3VJRLP0A3A6MpV3 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: Hi all, This series introduces __mt_dup() to improve the performance of fork(). During the duplication process of mmap, all VMAs are traversed and inserted one by one into the new maple tree, causing the maple tree to be rebalanced multiple times. Balancing the maple tree is a costly operation. To duplicate VMAs more efficiently, mtree_dup() and __mt_dup() are introduced for the maple tree. They can efficiently duplicate a maple tree. Here are some algorithmic details about {mtree,__mt}_dup(). We perform a DFS pre-order traversal of all nodes in the source maple tree. During this process, we fully copy the nodes from the source tree to the new tree. This involves memory allocation, and when encountering a new node, if it is a non-leaf node, all its child nodes are allocated at once. This idea was originally from Liam R. Howlett's Maple Tree Work email, and I added some of my own ideas to implement it. Some previous discussions can be found in [1]. For a more detailed analysis of the algorithm, please refer to the logs for patch [3/10] and patch [10/10]. There is a "spawn" in byte-unixbench[2], which can be used to test the performance of fork(). I modified it slightly to make it work with different number of VMAs. Below are the test results. The first row shows the number of VMAs. The second and third rows show the number of fork() calls per ten seconds, corresponding to next-20231006 and the this patchset, respectively. The test results were obtained with CPU binding to avoid scheduler load balancing that could cause unstable results. There are still some fluctuations in the test results, but at least they are better than the original performance. 21 121 221 421 821 1621 3221 6421 12821 25621 51221 112100 76261 54227 34035 20195 11112 6017 3161 1606 802 393 114558 83067 65008 45824 28751 16072 8922 4747 2436 1233 599 2.19% 8.92% 19.88% 34.64% 42.37% 44.64% 48.28% 50.17% 51.68% 53.74% 52.42% Thanks to Liam and Matthew for the review. Changes since v6: - Add Liam's 'Reviewed-by' tag to all patches except for patch [3/10]. - Modify the copyright statement according to Matthew's opinion. [1] https://lore.kernel.org/lkml/463899aa-6cbd-f08e-0aca-077b0e4e4475@bytedance.com/ [2] https://github.com/kdlucas/byte-unixbench/tree/master v1: https://lore.kernel.org/lkml/20230726080916.17454-1-zhangpeng.00@bytedance.com/ v2: https://lore.kernel.org/lkml/20230830125654.21257-1-zhangpeng.00@bytedance.com/ v3: https://lore.kernel.org/lkml/20230925035617.84767-1-zhangpeng.00@bytedance.com/ v4: https://lore.kernel.org/lkml/20231009090320.64565-1-zhangpeng.00@bytedance.com/ v5: https://lore.kernel.org/lkml/20231016032226.59199-1-zhangpeng.00@bytedance.com/ v6: https://lore.kernel.org/lkml/20231024083258.65750-1-zhangpeng.00@bytedance.com/ Peng Zhang (10): maple_tree: Add mt_free_one() and mt_attr() helpers maple_tree: Introduce {mtree,mas}_lock_nested() maple_tree: Introduce interfaces __mt_dup() and mtree_dup() radix tree test suite: Align kmem_cache_alloc_bulk() with kernel behavior. maple_tree: Add test for mtree_dup() maple_tree: Update the documentation of maple tree maple_tree: Skip other tests when BENCH is enabled maple_tree: Update check_forking() and bench_forking() maple_tree: Preserve the tree attributes when destroying maple tree fork: Use __mt_dup() to duplicate maple tree in dup_mmap() Documentation/core-api/maple_tree.rst | 4 + include/linux/maple_tree.h | 7 + include/linux/mm.h | 11 + kernel/fork.c | 40 ++- lib/maple_tree.c | 288 +++++++++++++++++++- lib/test_maple_tree.c | 123 +++++---- mm/internal.h | 11 - mm/memory.c | 7 +- mm/mmap.c | 9 +- tools/include/linux/rwsem.h | 4 + tools/include/linux/spinlock.h | 1 + tools/testing/radix-tree/linux.c | 45 +++- tools/testing/radix-tree/maple.c | 363 ++++++++++++++++++++++++++ 13 files changed, 811 insertions(+), 102 deletions(-)