From patchwork Tue Oct 24 08:32:48 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Peng Zhang X-Patchwork-Id: 13434034 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 3D860C00A8F for ; Tue, 24 Oct 2023 08:33:17 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id A06626B0197; Tue, 24 Oct 2023 04:33:16 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 98E3E6B0198; Tue, 24 Oct 2023 04:33:16 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 82E446B0199; Tue, 24 Oct 2023 04:33:16 -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 6FBEF6B0197 for ; Tue, 24 Oct 2023 04:33:16 -0400 (EDT) Received: from smtpin28.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id 43573A0DD6 for ; Tue, 24 Oct 2023 08:33:16 +0000 (UTC) X-FDA: 81379690392.28.AC2F63C Received: from mail-pf1-f174.google.com (mail-pf1-f174.google.com [209.85.210.174]) by imf24.hostedemail.com (Postfix) with ESMTP id AA29B18001F for ; Tue, 24 Oct 2023 08:33:13 +0000 (UTC) Authentication-Results: imf24.hostedemail.com; dkim=pass header.d=bytedance.com header.s=google header.b=cp4eCZMS; spf=pass (imf24.hostedemail.com: domain of zhangpeng.00@bytedance.com designates 209.85.210.174 as permitted sender) smtp.mailfrom=zhangpeng.00@bytedance.com; dmarc=pass (policy=quarantine) header.from=bytedance.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1698136394; 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=4opiNsnZZqBguAp7l0z4RxGxoCkwHsLxMKsqrGAlKDc=; b=BpDIAHicSx6yTntjDd8e1VLFNRsRbQtAD0t2DOmVf2wFI3SycPcQL8JDoSPel4NuFhvUEG r9DR6G+526UbQnnV9vEw8x9GLTdABcF5fmiRnEDTKrI3EUbI1dn7OLVBXGZim+MlIGOpMz xs3D+8OwXw8YrTFa1H6jLXyNAp9ny7c= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1698136394; a=rsa-sha256; cv=none; b=SvqcY790CMBgIjgjL7wJu1K3n48d3HJ/c6XMmcldUQxvsm4omua8tdLzwiY1m9qtK0Zk/A yRVDC9NolIJGQKeV0OOd5Hr9fraUHH6hXGdJNUc6nej5xrHFcLIrYjOC0h3h+oR8ulYi01 xoJuVCCpq2r4lYVvU7YdP/I4wpFJW2c= ARC-Authentication-Results: i=1; imf24.hostedemail.com; dkim=pass header.d=bytedance.com header.s=google header.b=cp4eCZMS; spf=pass (imf24.hostedemail.com: domain of zhangpeng.00@bytedance.com designates 209.85.210.174 as permitted sender) smtp.mailfrom=zhangpeng.00@bytedance.com; dmarc=pass (policy=quarantine) header.from=bytedance.com Received: by mail-pf1-f174.google.com with SMTP id d2e1a72fcca58-6b77ab73c6fso3054779b3a.1 for ; Tue, 24 Oct 2023 01:33:11 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance.com; s=google; t=1698136391; x=1698741191; 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=4opiNsnZZqBguAp7l0z4RxGxoCkwHsLxMKsqrGAlKDc=; b=cp4eCZMSFlcCVYjSF7exuJ35BgRlI82GHrcq/Na0Wbjs53VdvMg1201BBKEZTrXq05 XTxAzmoAmJgeWvuDc4jiK00c1Y8GNoetIkLJTRk+SUTDh7RHR6KNpC8k6ge8Ah7R9/Ro dv/XoRM/gl/p+u/wDyOISZRjFp96fKp6Bj6uWqpQTfePWesaS7cDH5T4beni8m/iKFyz /mVBmOwiTAqOwlqxxiaUtx7Umgmcv1XOoZ1h2v3RvxILWABUIJg6TAYJBJoAz8xhQZVl iHpZXnU1KcOwoC9pa/aAHqqPwHYARr8XvCRaqbybJ0sHii9uBhEcnjWTJSzl+o2q/mcR kwsw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1698136391; x=1698741191; 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=4opiNsnZZqBguAp7l0z4RxGxoCkwHsLxMKsqrGAlKDc=; b=Hq2hcc8MoU7+18+ZzYpjqDhInnpe8pnuC9VDIV0yuK+4mJRBiAFoygzJCGGK1FIKZ3 pfRgZMyLIirfIzUZxgIrkFYC9UXv5a+6dFW+qkBJvda0YBhaMV1ASjXb3/hF+wlJNkJm 7lzl3ztSpU8uIXVhTZeLb2ZXwVtPUVCgOuhmaq4rmX/r0yoY8zcHyb3G4cR9cQD6W4+l w/5kV1Rund9+aa/ez3pJwECZXERUpfXUloDOk2lGyFAAGuBCYKxZxM1Y+1N6otGPaMH2 VH75bSV9opr+QxpH86QPJaBgN9EGwR+fDhBZbcK9xFEqIZdYTPrWHiThswod/pWwEpiI Lz/w== X-Gm-Message-State: AOJu0YyCYl3kdbe9C2PWfbC1n/kwAgdka1XWv5GHV4JrXF1Ep0rYNFnP SSX064/QkoutEAwlGxqRwC9eJg== X-Google-Smtp-Source: AGHT+IFXSjkx04QR9nnz2TOFagUSULaL4sE18aVqctpq0U3SrAkmlMjZs5t6jtsN6WvK4/eUHQsGBg== X-Received: by 2002:aa7:8395:0:b0:68f:cbd3:5b01 with SMTP id u21-20020aa78395000000b0068fcbd35b01mr19416967pfm.13.1698136390755; Tue, 24 Oct 2023 01:33:10 -0700 (PDT) Received: from GL4FX4PXWL.bytedance.net ([203.208.167.147]) by smtp.gmail.com with ESMTPSA id y21-20020aa79af5000000b0068be348e35fsm7236977pfp.166.2023.10.24.01.33.04 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Tue, 24 Oct 2023 01:33:10 -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 v6 00/10] Introduce __mt_dup() to improve the performance of fork() Date: Tue, 24 Oct 2023 16:32:48 +0800 Message-Id: <20231024083258.65750-1-zhangpeng.00@bytedance.com> X-Mailer: git-send-email 2.39.3 (Apple Git-145) MIME-Version: 1.0 X-Rspamd-Queue-Id: AA29B18001F X-Rspam-User: X-Rspamd-Server: rspam11 X-Stat-Signature: g4z7rgd3xdygy7ubiocsqd336nmmzpoy X-HE-Tag: 1698136393-208653 X-HE-Meta: U2FsdGVkX18JZNZgb50gMUikMmQHwzJoOMn0kMNB6ke33tGOQaTQ37B3EkOBl75F4JijEIEM20Lv5V8L8jVJOevt9YK3DSeA/luVwPV6M2t18RGsaBT8XSJZO0qCvNIjuKbhR9h1krO+iEpbHVRAhX7SLVCDJlQb88oArCdeVR2p25VpNy4v7Lo5r+jUopeESs5d6/McmMxd4cAxvRR7L2Cl7KsRe1TTBcJUXWWmOnNQloy4vhw7jo4gxsRcNInB1zYifDrxfdsjWB90Lx91dDYLwf2x3Vat8Os/a5C6J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s1yLHQ2AH3xHzrfrksz+vaLgU+TGDJwjAFgf5vnOW4O36vjpwyUBxTG/6e68RzqaTPoZT9PSA4smRSi4sfYQewoSW8vXZvXHmwxp9T+OtKf174kyJSEX52YlIP1S0Fu+bSYg4jMQnBw/9U3JW5C+oFDxSIcSty/14v908LZd+jfarAz/eRud6Er3ExL9m88rjuJ7BQldVGZ+NvKXxO1xAfok3EBBLSa5rIV3eShyXHL 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 v5: - Correct the copyright statement. - Add Suggested-by tag in patch [3/10] and [10/10], this work was originally proposed by Liam R. Howlett. - Some cleanup and comment corrections for patch [3/10]. - Use vma_iter* series interfaces as much as possible in [10/10]. [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/ 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 | 290 +++++++++++++++++++- 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, 813 insertions(+), 102 deletions(-) Reviewed-by: Liam R. Howlett