Received: by 2002:a05:7412:2a8c:b0:e2:908c:2ebd with SMTP id u12csp985152rdh; Sun, 24 Sep 2023 22:28:19 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHX6tK9RaY6tsHUepuZo4M5MM4U/e6vi1Tl6ePxC58VUWLUxiNmFe+OWm2M7VfWClG69V/z X-Received: by 2002:a05:6a21:3e07:b0:153:d71f:cd23 with SMTP id bk7-20020a056a213e0700b00153d71fcd23mr3668411pzc.26.1695619699593; Sun, 24 Sep 2023 22:28:19 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1695619699; cv=none; d=google.com; s=arc-20160816; b=DXMU9jRtIvnYg+UCsp3SQ6teutvUoL6MmanSBJ1zSyAtw0gLh+iJ2HW/vyX+boN/rP VB9fudrDqCGB1ASS1yAiD0yf1nGgiy/wxTLizL1nydFwXaHKGaq9KJwJiKMnXHIl+bY+ ehRNNlaj0AiZ+8bNMCk12BB96iz0ki2H0zatFnH+fTouf+eTmxb4yiW6tzhHbkItD4M3 vy19d8lcOiLfTxQjAlgwOA5fTKP2go5vL6nEJPHd7N2VJYbp6/F2alwvaIUhEDXlOMyS 7jaMpMdU+1eKhP+XvEqGKItUZ0BZibTGh9l/O9jCyL00MpJbcyoh3JUUj8UUG4iHy5pM bboA== 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 :message-id:date:subject:cc:to:from:dkim-signature; bh=ZL8oS6h/SMWhCHncu4CSFCmNusM3zUoiub5E91Pn6TA=; fh=uq6KVc+CzZ80ucpwrp8f3d+Fe7Ue0w5J0Au2lk064nU=; b=hJtraC2onleMbO1+jYJ673Lcg5Xlxf10El346d71dD8wyAbAd8tqeA9udO9gXT2uPg 9k/X2kQesYMb2TipCdnrNo6pwYw/nbfoSNKA5GoUF9EyL6i9Aqlpxh22vdMW10CxyPTZ +2lTUuv9VRugKwvzE++gzA4V72480sXivDwqm0E+1cz2bP79SYI+a37s2c363QkoWnd2 WRgiscE5nK9CNg9tXeALCHgxE/ooqvYXew1MTEnF+5/nVu+cGIZ8625zdGRHnRtJzVtm Tze7ot/xUcsvljn3bkX2UY1JUsuN8T7MQUR8ikSOVWle0wrdJBQ7U2Oue3aaxsWbHDxR YEtA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@bytedance.com header.s=google header.b=X05cR9gb; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:3 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 lipwig.vger.email (lipwig.vger.email. [2620:137:e000::3:3]) by mx.google.com with ESMTPS id kl14-20020a170903074e00b001c56bd3f787si3875859plb.103.2023.09.24.22.28.19 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 24 Sep 2023 22:28:19 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:3 as permitted sender) client-ip=2620:137:e000::3:3; Authentication-Results: mx.google.com; dkim=pass header.i=@bytedance.com header.s=google header.b=X05cR9gb; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:3 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 (depot.vger.email [IPv6:2620:137:e000::3:0]) by lipwig.vger.email (Postfix) with ESMTP id 9F1A08160396; Sun, 24 Sep 2023 20:58:19 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at lipwig.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231681AbjIYD6R (ORCPT + 99 others); Sun, 24 Sep 2023 23:58:17 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39808 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231642AbjIYD6O (ORCPT ); Sun, 24 Sep 2023 23:58:14 -0400 Received: from mail-pf1-x42e.google.com (mail-pf1-x42e.google.com [IPv6:2607:f8b0:4864:20::42e]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 93249E1 for ; Sun, 24 Sep 2023 20:57:45 -0700 (PDT) Received: by mail-pf1-x42e.google.com with SMTP id d2e1a72fcca58-690d8c05784so3890471b3a.2 for ; Sun, 24 Sep 2023 20:57:45 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance.com; s=google; t=1695614265; x=1696219065; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=ZL8oS6h/SMWhCHncu4CSFCmNusM3zUoiub5E91Pn6TA=; b=X05cR9gbgpWsd0WxF7LzGEEkNoOB2lSNWyHrgCKsLdSkUQ9GPgk24OxyjUfmJWf+5s X+wclKE2/DDG+aH5gtS8YgXFfLgsezLkaLA2Q+xHgmd4FP8o0P7l4pRe1wlNj/HrlG7M uNRHOHBniyUAQ5pKWf+Lvl51Dan7sRanpbPU35oWE8HMrBuPJjJMsXEX05vLeSEKFrL4 tADUkI6Eiyh7QQ/FMZGb3G3yOP9+Vb3i/P8xb06KjZ0uVxVAx1g1CjFHPgUMoqy9ujnd 0c6jUq5F4xQ+bIBFtrEWXFS3BP8A+HzNt8nVnfAo8bJUkvfNiyp1/SPde6pqk6PpTvIl FTgw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1695614265; x=1696219065; 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=ZL8oS6h/SMWhCHncu4CSFCmNusM3zUoiub5E91Pn6TA=; b=bOOmahBcHCFUZNTh799yzFpCJuh1N1pzUMpugVyaReiI9yfKwhqd0F/h7YOfn6XGMz W8yxB4pUSiOZE/w9uDoMqKSWpPPcjx+FaCLMyrb38PQ05EWNsjhsiN0BVH+qWcS+tMdR BsERQ+44S42L1xkStiJcCwhYR+jm+mU/OU1Tg4KIRNYywCksPjXjnP45gf3fRotWZced 62gv6fKuFIuPIwIeTSPTAMmYxuH6KZrtixF8huIDFW4qQcY9UhFOKBG50ZnEHUc04EBD qSzN5UThbkWhTPnS7lACZALK1QIKdN01MDdGZBfZN9OSnvWtBsULsSBtZqVqf7L9hudg 5uhw== X-Gm-Message-State: AOJu0Yz/2COGooZdgzp8zDtrrbolD1m9YRPU/xnVbA+7K7bcp+TG6qEl Pr40YSmOvLDqumOvXevV83nfCDo6kggl8To3doM= X-Received: by 2002:a05:6a00:1990:b0:68b:fdfe:76c2 with SMTP id d16-20020a056a00199000b0068bfdfe76c2mr4715925pfl.20.1695614264958; Sun, 24 Sep 2023 20:57:44 -0700 (PDT) Received: from GL4FX4PXWL.bytedance.net ([203.208.167.146]) by smtp.gmail.com with ESMTPSA id fm1-20020a056a002f8100b00679a4b56e41sm7025387pfb.43.2023.09.24.20.57.37 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Sun, 24 Sep 2023 20:57:43 -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 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 v3 0/9] Introduce __mt_dup() to improve the performance of fork() Date: Mon, 25 Sep 2023 11:56:08 +0800 Message-Id: <20230925035617.84767-1-zhangpeng.00@bytedance.com> X-Mailer: git-send-email 2.37.0 (Apple Git-136) MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-0.9 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI, SPF_HELO_NONE,SPF_PASS autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lipwig.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (lipwig.vger.email [0.0.0.0]); Sun, 24 Sep 2023 20:58:19 -0700 (PDT) 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. By applying __mt_dup() to dup_mmap(), better performance is achieved compared to the original method. After using this method, the average time complexity decreases from O(n * log(n)) to O(n). 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. Some previous discussions can be referred to as [1]. 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. By default, there are 21 VMAs. The first row shows the number of additional VMAs added on top of the default. The last two rows show the number of fork() calls per ten seconds. 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. Increment of VMAs: 0 100 200 400 800 1600 3200 6400 next-20230921: 112326 75469 54529 34619 20750 11355 6115 3183 Apply this: 116505 85971 67121 46080 29722 16665 9050 4805 +3.72% +13.92% +23.09% +33.11% +43.24% +46.76% +48.00% +50.96% Thanks to kernel test robot for reporting the warning about nested locks. Thanks to Liam for all the suggestions. Changes since v2: - Some minor modifications to mtree_dup(), __mt_dup() and their test code. - Introduce {mtree, mas}_lock_nested() to address lockdep warnings. - Update the documentation for maple tree. - Introduce undo_dup_mmap() to address the failure of dup_mmap(). - Performance data was retested based on the latest next-20230921, and there were some fluctuations in the results which were expected. [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/ Peng Zhang (9): 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() 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 | 1 + kernel/fork.c | 34 ++- lib/maple_tree.c | 300 ++++++++++++++++++++- lib/test_maple_tree.c | 69 +++-- mm/internal.h | 3 +- mm/memory.c | 7 +- mm/mmap.c | 52 +++- tools/include/linux/spinlock.h | 1 + tools/testing/radix-tree/maple.c | 363 ++++++++++++++++++++++++++ 11 files changed, 787 insertions(+), 54 deletions(-) -- 2.20.1