From: Mateusz Guzik <mjguzik@gmail.com>
To: Suren Baghdasaryan <surenb@google.com>
Cc: torvalds@linux-foundation.org, akpm@linux-foundation.org,
regressions@leemhuis.info, bagasdotme@gmail.com,
jacobly.alt@gmail.com, willy@infradead.org,
liam.howlett@oracle.com, david@redhat.com, peterx@redhat.com,
ldufour@linux.ibm.com, linux-kernel@vger.kernel.org,
linux-mm@kvack.org, linuxppc-dev@lists.ozlabs.org,
linux-arm-kernel@lists.infradead.org, gregkh@linuxfoundation.org,
regressions@lists.linux.dev, "Jiri Slaby" <jirislaby@kernel.org>,
"Holger Hoffstätte" <holger@applied-asynchrony.com>,
stable@vger.kernel.org
Subject: Re: [PATCH v2 3/3] fork: lock VMAs of the parent process when forking
Date: Fri, 4 Aug 2023 23:46:20 +0200 [thread overview]
Message-ID: <20230804214620.btgwhsszsd7rh6nf@f> (raw)
In-Reply-To: <20230708191212.4147700-3-surenb@google.com>
On Sat, Jul 08, 2023 at 12:12:12PM -0700, Suren Baghdasaryan wrote:
[..]
> Lock VMAs of the parent process when forking a child, which prevents
> concurrent page faults during fork operation and avoids this issue.
> This fix can potentially regress some fork-heavy workloads. Kernel build
> time did not show noticeable regression on a 56-core machine while a
> stress test mapping 10000 VMAs and forking 5000 times in a tight loop
> shows ~5% regression. If such fork time regression is unacceptable,
> disabling CONFIG_PER_VMA_LOCK should restore its performance. Further
> optimizations are possible if this regression proves to be problematic.
>
> ---
> kernel/fork.c | 1 +
> 1 file changed, 1 insertion(+)
>
> diff --git a/kernel/fork.c b/kernel/fork.c
> index b85814e614a5..d2e12b6d2b18 100644
> --- a/kernel/fork.c
> +++ b/kernel/fork.c
> @@ -686,6 +686,7 @@ static __latent_entropy int dup_mmap(struct mm_struct *mm,
> for_each_vma(old_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));
> continue;
>
I don't see it mentioned in the discussion, so at a risk of ruffling
feathers or looking really bad I'm going to ask: is the locking of any
use if the forking process is single-threaded? The singular thread in
this case is occupied executing this very code, so it can't do any op
in parallel. Is there anyone else who could trigger a page fault? Are
these shared with other processes? Cursory reading suggests a private
copy is made here, so my guess is no. But then again, I landed here
freshly from the interwebz.
Or in short: if nobody can mess up the state if the forking process is
single-threaded, why not check for mm_users or whatever other indicator
to elide the slowdown for the (arguably) most common case?
If the state can be messed up anyway, that's a shame, but short
explanation how would be welcome.
to illustrate (totally untested):
diff --git a/kernel/fork.c b/kernel/fork.c
index d2e12b6d2b18..aac6b08a0b21 100644
--- a/kernel/fork.c
+++ b/kernel/fork.c
@@ -652,6 +652,7 @@ static __latent_entropy int dup_mmap(struct mm_struct *mm,
LIST_HEAD(uf);
VMA_ITERATOR(old_vmi, oldmm, 0);
VMA_ITERATOR(vmi, mm, 0);
+ bool singlethread = READ_ONCE(oldmm->mm_users) == 1;
uprobe_start_dup_mmap();
if (mmap_write_lock_killable(oldmm)) {
@@ -686,7 +687,8 @@ static __latent_entropy int dup_mmap(struct mm_struct *mm,
for_each_vma(old_vmi, mpnt) {
struct file *file;
- vma_start_write(mpnt);
+ if (!singelthreaded)
+ vma_start_write(mpnt);
if (mpnt->vm_flags & VM_DONTCOPY) {
vm_stat_account(mm, mpnt->vm_flags, -vma_pages(mpnt));
continue;
next prev parent reply other threads:[~2023-08-04 21:46 UTC|newest]
Thread overview: 22+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-07-08 19:12 [PATCH v2 1/3] mm: lock a vma before stack expansion Suren Baghdasaryan
2023-07-08 19:12 ` [PATCH v2 2/3] mm: lock newly mapped VMA which can be modified after it becomes visible Suren Baghdasaryan
2023-07-08 19:12 ` [PATCH v2 3/3] fork: lock VMAs of the parent process when forking Suren Baghdasaryan
2023-07-08 19:22 ` Suren Baghdasaryan
2023-07-08 21:18 ` Linus Torvalds
2023-07-08 22:36 ` Suren Baghdasaryan
2023-07-08 22:53 ` Linus Torvalds
2023-07-08 23:03 ` Suren Baghdasaryan
2023-08-04 21:46 ` Mateusz Guzik [this message]
2023-08-04 22:49 ` Linus Torvalds
2023-08-04 23:25 ` Mateusz Guzik
2023-08-05 0:14 ` Linus Torvalds
2023-08-05 0:26 ` Suren Baghdasaryan
2023-08-05 0:34 ` Suren Baghdasaryan
2023-08-05 0:49 ` Mateusz Guzik
2023-08-05 1:06 ` Suren Baghdasaryan
2023-08-05 1:16 ` Mateusz Guzik
2023-08-05 1:36 ` Suren Baghdasaryan
2023-08-05 1:06 ` Mateusz Guzik
2023-08-05 1:42 ` Suren Baghdasaryan
2023-08-09 21:07 ` Mateusz Guzik
2023-08-10 20:31 ` Suren Baghdasaryan
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20230804214620.btgwhsszsd7rh6nf@f \
--to=mjguzik@gmail.com \
--cc=akpm@linux-foundation.org \
--cc=bagasdotme@gmail.com \
--cc=david@redhat.com \
--cc=gregkh@linuxfoundation.org \
--cc=holger@applied-asynchrony.com \
--cc=jacobly.alt@gmail.com \
--cc=jirislaby@kernel.org \
--cc=ldufour@linux.ibm.com \
--cc=liam.howlett@oracle.com \
--cc=linux-arm-kernel@lists.infradead.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=linuxppc-dev@lists.ozlabs.org \
--cc=peterx@redhat.com \
--cc=regressions@leemhuis.info \
--cc=regressions@lists.linux.dev \
--cc=stable@vger.kernel.org \
--cc=surenb@google.com \
--cc=torvalds@linux-foundation.org \
--cc=willy@infradead.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox