From mboxrd@z Thu Jan 1 00:00:00 1970 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 3B8BDEE57ED for ; Fri, 8 Sep 2023 09:59:10 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id ACD566B00AC; Fri, 8 Sep 2023 05:59:09 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id A7E716B00AD; Fri, 8 Sep 2023 05:59:09 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 91F876B00AE; Fri, 8 Sep 2023 05:59:09 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id 7DB096B00AC for ; Fri, 8 Sep 2023 05:59:09 -0400 (EDT) Received: from smtpin08.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id 4095B1CA106 for ; Fri, 8 Sep 2023 09:59:09 +0000 (UTC) X-FDA: 81212982018.08.DFC4BD9 Received: from mail-pl1-f169.google.com (mail-pl1-f169.google.com [209.85.214.169]) by imf26.hostedemail.com (Postfix) with ESMTP id 6F66814000E for ; Fri, 8 Sep 2023 09:59:06 +0000 (UTC) Authentication-Results: imf26.hostedemail.com; dkim=pass header.d=bytedance.com header.s=google header.b=ixtZhwHf; spf=pass (imf26.hostedemail.com: domain of zhangpeng.00@bytedance.com designates 209.85.214.169 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=1694167147; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=3bXTeQpdjQRua5NdbqlMCWj/ycDFicrMQeBdM0bMGMw=; b=QYKGLXOgbXqD7loztfDYzGlsq9+8Qprrkld0tm5HVKaHwutaoi45fxK1Y9GTBT4KviVHll QwQoLaqn1I2uTKpXwNlHL3Vz8FdmiRaKvvqZHPy5rx4DHo7tPuf0v6c2Qh3SLU4BDpawwT OZQx1gxsjyXl5nb5MgqWo3LzpPogVb4= ARC-Authentication-Results: i=1; imf26.hostedemail.com; dkim=pass header.d=bytedance.com header.s=google header.b=ixtZhwHf; spf=pass (imf26.hostedemail.com: domain of zhangpeng.00@bytedance.com designates 209.85.214.169 as permitted sender) smtp.mailfrom=zhangpeng.00@bytedance.com; dmarc=pass (policy=quarantine) header.from=bytedance.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1694167147; a=rsa-sha256; cv=none; b=joQyzvYy+w9mQ4szHA/0dkZEIdNrv6hzacCQGd4iWISPs1tjnXV8L8mrs8WHqTgZ23Zkr4 Rgq/gzAkjRlUQaBTh/E/0djhK3ZjpmBubypCiK9EDL4uMquh23+zpmhCCDaTynnZaAzCfk GDVa7sQWMUlzhXY+G5c0Qp3FzAd2Ji4= Received: by mail-pl1-f169.google.com with SMTP id d9443c01a7336-1bf1935f6c2so13158545ad.1 for ; Fri, 08 Sep 2023 02:59:05 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance.com; s=google; t=1694167145; x=1694771945; darn=kvack.org; h=content-transfer-encoding:in-reply-to:from:references:to:subject :user-agent:mime-version:date:message-id:from:to:cc:subject:date :message-id:reply-to; bh=3bXTeQpdjQRua5NdbqlMCWj/ycDFicrMQeBdM0bMGMw=; b=ixtZhwHfI4/FtDLSRkb2/bYKaj17mqKofcmQ/n564Cba0jIG+6e1FQJQslmjwiYm63 yXkYNbj8znwZ7XJqRZhxc7Av3piF/TezOoOfyuGvIWRgaActhIWD+/G7nJ0hhG9+a6E1 takxS2nW4k0pO6f1eueLqxPZv+Lh0dOe6tKwnT7JuuTG5clIa7++bcVoy0nxAJI9ARuF 4HW/ojbDTLs6pxo/aCvhRqXJVZUjf9ixDtYoJS23mwdN3l++wNl0zk6jTXA2rXDXpzou MaBE1JKwYZzwe5rgPVjjFhYZWpcx/XwikmiTSe3SA9ViJLvvzVfD/iVajsWScIgDwxkq EkYg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1694167145; x=1694771945; h=content-transfer-encoding:in-reply-to:from:references:to:subject :user-agent:mime-version:date:message-id:x-gm-message-state:from:to :cc:subject:date:message-id:reply-to; bh=3bXTeQpdjQRua5NdbqlMCWj/ycDFicrMQeBdM0bMGMw=; b=UI73/qXFyahuitWEBkBfeq5J5I5FF+SnMJENixgKa/6mL4+cxRz5vBmiuj4lK26ppm qxY5hMs5vs6k8GX7/90a4E81SC+xyCmUl2i591fkGlX5DZNqQl7Hh/2sWVVZREI1r/LG HbIz1igUx0vThJCw34XGu7fHtgrMu2jBawUEef+I8QKOkcJkDe+hX5gKcgMF7AyLTtcv AJN9xEMdPznpWaAO26aC1AJ+sNg3dr42riHvhbYVJ2gcWC9eVxyLSZIClZQu/JbyvKX8 vAuZDOOq8Xgp3TGALVkLSYmNrM2uIILKvsl9rsBpvW/s/6IosAi7qs7ezz+GyFbGMqkz 4TOQ== X-Gm-Message-State: AOJu0Ywx2fcoZGoWS+uF9Fqgbj85I9sztTAFiT7ws6csv1ZDnCQO6bzm JnOuisPy3Ykdpa5jFyGzPdwYVQ== X-Google-Smtp-Source: AGHT+IGedR76hVXK1WBcaqFuQUXJzaKlwoEvzzggHuGYxkzUO76lUrRz9GzNXg+wcSGY1vog4i33bg== X-Received: by 2002:a17:902:e54e:b0:1c0:bcbc:d67 with SMTP id n14-20020a170902e54e00b001c0bcbc0d67mr6761333plf.22.1694167144904; Fri, 08 Sep 2023 02:59:04 -0700 (PDT) Received: from [10.254.232.87] ([139.177.225.246]) by smtp.gmail.com with ESMTPSA id bi4-20020a170902bf0400b001bc45408d26sm1199081plb.36.2023.09.08.02.58.58 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Fri, 08 Sep 2023 02:59:04 -0700 (PDT) Message-ID: Date: Fri, 8 Sep 2023 17:58:56 +0800 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:102.0) Gecko/20100101 Thunderbird/102.15.0 Subject: Re: [PATCH v2 6/6] fork: Use __mt_dup() to duplicate maple tree in dup_mmap() To: "Liam R. Howlett" , Peng Zhang , corbet@lwn.net, akpm@linux-foundation.org, willy@infradead.org, brauner@kernel.org, surenb@google.com, michael.christie@oracle.com, peterz@infradead.org, mathieu.desnoyers@efficios.com, npiggin@gmail.com, avagin@gmail.com, linux-mm@kvack.org, linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, linux-fsdevel@vger.kernel.org References: <20230830125654.21257-1-zhangpeng.00@bytedance.com> <20230830125654.21257-7-zhangpeng.00@bytedance.com> <20230907201414.dagnqxfnu7f7qzxd@revolver> From: Peng Zhang In-Reply-To: <20230907201414.dagnqxfnu7f7qzxd@revolver> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit X-Rspamd-Queue-Id: 6F66814000E X-Rspam-User: X-Stat-Signature: sjgspt7jdodojpr1rw4m7h5qhukfd7zs X-Rspamd-Server: rspam01 X-HE-Tag: 1694167146-632769 X-HE-Meta: U2FsdGVkX19QpqBOejdWqK60OUl72c7kPtr+pPHnvFgz2man/EHO1Z7LBj32JUPPDCjHjfOTZ66d1aF8IB4DGBXd1Uyk9B2zzUSkViW8b92v71CkzNFq1qREJiOQLwwiNV6YYfd9Lm37ulvO3e6Eu12YZILp/ei+quFs6M0/mdWRrzcifrDBvZalr7L5EkYNarK765kupgYZFBNhGW+z0F5rNKDxK13d8eRlba86d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c5Up+av4PFnLyfGzxYxEjDBgoX/EkQ2+S5sXpHZWgNryP9ujb/eUTCWhQXeXKgcDT28ZJuPfmTMVeuoTEb2/jYWXB2CQP9i5Ei6SOl2LVcf5FEdtHGoXhlInLW8FCAns9XPzdWnB1C7BtstqUgB+k1ZfnoLrzKjcu1A3xOYF6wM2j/+rWnsXZ46cCMPBd49YnCM1uN1f81CPyswAoyx/7xHw== 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: 在 2023/9/8 04:14, Liam R. Howlett 写道: > * Peng Zhang [230830 08:58]: >> Use __mt_dup() to duplicate the old maple tree in dup_mmap(), and then >> directly modify the entries of VMAs in the new maple tree, which can >> get better performance. The optimization effect is proportional to the >> number of VMAs. >> >> There is a "spawn" in byte-unixbench[1], 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 numbers. There are 21 VMAs by default. The first row >> indicates the number of added VMAs. The following two lines are the >> number of fork() calls every 10 seconds. These numbers are different >> from the test results in v1 because this time the benchmark is bound to >> a CPU. This way the numbers are more stable. >> >> Increment of VMAs: 0 100 200 400 800 1600 3200 6400 >> 6.5.0-next-20230829: 111878 75531 53683 35282 20741 11317 6110 3158 >> Apply this patchset: 114531 85420 64541 44592 28660 16371 9038 4831 >> +2.37% +13.09% +20.23% +26.39% +38.18% +44.66% +47.92% +52.98% > > Thanks! > > Can you include 21 in this table since it's the default? Maybe I didn't express clearly, "Increment of VMAs" means the number of VMAs added on the basis of 21 VMAs. > >> >> [1] https://github.com/kdlucas/byte-unixbench/tree/master >> >> Signed-off-by: Peng Zhang >> --- >> kernel/fork.c | 34 ++++++++++++++++++++++++++-------- >> mm/mmap.c | 14 ++++++++++++-- >> 2 files changed, 38 insertions(+), 10 deletions(-) >> >> diff --git a/kernel/fork.c b/kernel/fork.c >> index 3b6d20dfb9a8..e6299adefbd8 100644 >> --- a/kernel/fork.c >> +++ b/kernel/fork.c >> @@ -650,7 +650,6 @@ static __latent_entropy int dup_mmap(struct mm_struct *mm, >> int retval; >> unsigned long charge = 0; >> LIST_HEAD(uf); >> - VMA_ITERATOR(old_vmi, oldmm, 0); >> VMA_ITERATOR(vmi, mm, 0); >> >> uprobe_start_dup_mmap(); >> @@ -678,17 +677,39 @@ static __latent_entropy int dup_mmap(struct mm_struct *mm, >> goto out; >> khugepaged_fork(mm, oldmm); >> >> - retval = vma_iter_bulk_alloc(&vmi, oldmm->map_count); >> - if (retval) >> + /* Use __mt_dup() to efficiently build an identical maple tree. */ >> + retval = __mt_dup(&oldmm->mm_mt, &mm->mm_mt, GFP_NOWAIT | __GFP_NOWARN); > > Apparently the flags should be GFP_KERNEL here so that compaction can > run. OK, I'll change it to GFP_KERNEL. > >> + if (unlikely(retval)) >> goto out; >> >> mt_clear_in_rcu(vmi.mas.tree); >> - for_each_vma(old_vmi, mpnt) { >> + for_each_vma(vmi, mpnt) { >> struct file *file; >> >> vma_start_write(mpnt); >> if (mpnt->vm_flags & VM_DONTCOPY) { >> vm_stat_account(mm, mpnt->vm_flags, -vma_pages(mpnt)); >> + >> + /* >> + * Since the new tree is exactly the same as the old one, >> + * we need to remove the unneeded VMAs. >> + */ >> + mas_store(&vmi.mas, NULL); >> + >> + /* >> + * Even removing an entry may require memory allocation, >> + * and if removal fails, we use XA_ZERO_ENTRY to mark >> + * from which VMA it failed. The case of encountering >> + * XA_ZERO_ENTRY will be handled in exit_mmap(). >> + */ >> + if (unlikely(mas_is_err(&vmi.mas))) { >> + retval = xa_err(vmi.mas.node); >> + mas_reset(&vmi.mas); >> + if (mas_find(&vmi.mas, ULONG_MAX)) >> + mas_store(&vmi.mas, XA_ZERO_ENTRY); >> + goto loop_out; >> + } >> + > > Storing NULL may need extra space as you noted, so we need to be careful > what happens if we don't have that space. We should have a testcase to > test this scenario. > > mas_store_gfp() should be used with GFP_KERNEL. The VMAs use GFP_KERNEL > in this function, see vm_area_dup(). > > Don't use the exit_mmap() path to undo a failed fork. You've added > checks and complications to the exit path for all tasks in the very > unlikely event that we run out of memory when we hit a very unlikely > VM_DONTCOPY flag. > > I see the issue with having a portion of the tree with new VMAs that are > accounted and a portion of the tree that has old VMAs that should not be > looked at. It was clever to use the XA_ZERO_ENTRY as a stop point, but > we cannot add that complication to the exit path and then there is the > OOM race to worry about (maybe, I am not sure since this MM isn't > active yet). > > Using what is done in exit_mmap() and do_vmi_align_munmap() as a > prototype, we can do something like the *untested* code below: > > if (unlikely(mas_is_err(&vmi.mas))) { > unsigned long max = vmi.index; > > retval = xa_err(vmi.mas.node); > mas_set(&vmi.mas, 0); > tmp = mas_find(&vmi.mas, ULONG_MAX); > if (tmp) { /* Not the first VMA failed */ > unsigned long nr_accounted = 0; > > unmap_region(mm, &vmi.mas, vma, NULL, mpnt, 0, max, max, > true); > do { > if (vma->vm_flags & VM_ACCOUNT) > nr_accounted += vma_pages(vma); > remove_vma(vma, true); > cond_resched(); > vma = mas_find(&vmi.mas, max - 1); > } while (vma != NULL); > > vm_unacct_memory(nr_accounted); > } > __mt_destroy(&mm->mm_mt); > goto loop_out; > } > > Once exit_mmap() is called, the check for OOM (no vma) will catch that > nothing is left to do. > > It might be worth making an inline function to do this to keep the fork > code clean. We should test this by detecting a specific task name and > returning a failure at a given interval: > > if (!strcmp(current->comm, "fork_test") { > ... > } Thank you for your suggestion, I will do this in the next version. > > >> continue; >> } >> charge = 0; >> @@ -750,8 +771,7 @@ static __latent_entropy int dup_mmap(struct mm_struct *mm, >> hugetlb_dup_vma_private(tmp); >> >> /* Link the vma into the MT */ >> - if (vma_iter_bulk_store(&vmi, tmp)) >> - goto fail_nomem_vmi_store; >> + mas_store(&vmi.mas, tmp); >> >> mm->map_count++; >> if (!(tmp->vm_flags & VM_WIPEONFORK)) >> @@ -778,8 +798,6 @@ static __latent_entropy int dup_mmap(struct mm_struct *mm, >> uprobe_end_dup_mmap(); >> return retval; >> >> -fail_nomem_vmi_store: >> - unlink_anon_vmas(tmp); >> fail_nomem_anon_vma_fork: >> mpol_put(vma_policy(tmp)); >> fail_nomem_policy: >> diff --git a/mm/mmap.c b/mm/mmap.c >> index b56a7f0c9f85..dfc6881be81c 100644 >> --- a/mm/mmap.c >> +++ b/mm/mmap.c >> @@ -3196,7 +3196,11 @@ void exit_mmap(struct mm_struct *mm) >> arch_exit_mmap(mm); >> >> vma = mas_find(&mas, ULONG_MAX); >> - if (!vma) { >> + /* >> + * If dup_mmap() fails to remove a VMA marked VM_DONTCOPY, >> + * xa_is_zero(vma) may be true. >> + */ >> + if (!vma || xa_is_zero(vma)) { >> /* Can happen if dup_mmap() received an OOM */ >> mmap_read_unlock(mm); >> return; >> @@ -3234,7 +3238,13 @@ void exit_mmap(struct mm_struct *mm) >> remove_vma(vma, true); >> count++; >> cond_resched(); >> - } while ((vma = mas_find(&mas, ULONG_MAX)) != NULL); >> + vma = mas_find(&mas, ULONG_MAX); >> + /* >> + * If xa_is_zero(vma) is true, it means that subsequent VMAs >> + * donot need to be removed. Can happen if dup_mmap() fails to >> + * remove a VMA marked VM_DONTCOPY. >> + */ >> + } while (vma != NULL && !xa_is_zero(vma)); >> >> BUG_ON(count != mm->map_count); >> >> -- >> 2.20.1 >> >