From: David Hildenbrand <david@redhat.com>
To: kalyazin@amazon.com, pbonzini@redhat.com, corbet@lwn.net,
kvm@vger.kernel.org, linux-doc@vger.kernel.org,
linux-kernel@vger.kernel.org
Cc: jthoughton@google.com, brijesh.singh@amd.com,
michael.roth@amd.com, graf@amazon.de, jgowans@amazon.com,
roypat@amazon.co.uk, derekmn@amazon.com, nsaenz@amazon.es,
xmarcalx@amazon.com, Sean Christopherson <seanjc@google.com>,
linux-mm@kvack.org
Subject: Re: [RFC PATCH 0/4] KVM: ioctl for populating guest_memfd
Date: Wed, 20 Nov 2024 14:46:09 +0100 [thread overview]
Message-ID: <01b0a528-bec0-41d7-80f6-8afe213bd56b@redhat.com> (raw)
In-Reply-To: <08aeaf6e-dc89-413a-86a6-b9772c9b2faf@amazon.com>
On 20.11.24 13:09, Nikita Kalyazin wrote:
> On 24/10/2024 10:54, Nikita Kalyazin wrote:
>> [2] proposes an alternative to
>> UserfaultFD for intercepting stage-2 faults, while this series
>> conceptually compliments it with the ability to populate guest memory
>> backed by guest_memfd for `KVM_X86_SW_PROTECTED_VM` VMs.
>
> +David
> +Sean
> +mm
Hi!
>
> While measuring memory population performance of guest_memfd using this
> series, I noticed that guest_memfd population takes longer than my
> baseline, which is filling anonymous private memory via UFFDIO_COPY.
>
> I am using x86_64 for my measurements and 3 GiB memory region:
> - anon/private UFFDIO_COPY: 940 ms
> - guest_memfd: 1371 ms (+46%)
>
> It turns out that the effect is observable not only for guest_memfd, but
> also for any type of shared memory, eg memfd or anonymous memory mapped
> as shared.
> Below are measurements of a plain mmap(MAP_POPULATE) operation:>
> mmap(NULL, 3ll * (1 << 30), PROT_READ | PROT_WRITE, MAP_PRIVATE |
> MAP_ANONYMOUS | MAP_POPULATE, -1, 0);
> vs
> mmap(NULL, 3ll * (1 << 30), PROT_READ | PROT_WRITE, MAP_SHARED |
> MAP_ANONYMOUS | MAP_POPULATE, -1, 0);
>
> Results:
> - MAP_PRIVATE: 968 ms
> - MAP_SHARED: 1646 ms
At least here it is expected to some degree: as soon as the page cache
is involved map/unmap gets slower, because we are effectively
maintaining two datastructures (page tables + page cache) instead of
only a single one (page cache)
Can you make sure that THP/large folios don't interfere in your
experiments (e.g., madvise(MADV_NOHUGEPAGE))?
>
> I am seeing this effect on a range of kernels. The oldest I used was
> 5.10, the newest is the current kvm-next (for-linus-2590-gd96c77bd4eeb).
>
> When profiling with perf, I observe the following hottest operations
> (kvm-next). Attaching full distributions at the end of the email.
>
> MAP_PRIVATE:
> - 19.72% clear_page_erms, rep stos %al,%es:(%rdi)
>
> MAP_SHARED:
> - 43.94% shmem_get_folio_gfp, lock orb $0x8,(%rdi), which is atomic
> setting of the PG_uptodate bit
> - 10.98% clear_page_erms, rep stos %al,%es:(%rdi)
Interesting.
>
> Note that MAP_PRIVATE/do_anonymous_page calls __folio_mark_uptodate that
> sets the PG_uptodate bit regularly.
> , while MAP_SHARED/shmem_get_folio_gfp calls folio_mark_uptodate that
> sets the PG_uptodate bit atomically.
>
> While this logic is intuitive, its performance effect is more
> significant that I would expect.
Yes. How much of the performance difference would remain if you hack out
the atomic op just to play with it? I suspect there will still be some
difference.
>
> The questions are:
> - Is this a well-known behaviour?
> - Is there a way to mitigate that, ie make shared memory (including
> guest_memfd) population faster/comparable to private memory?
Likely. But your experiment measures above something different than what
guest_memfd vs. anon does: guest_memfd doesn't update page tables, so I
would assume guest_memfd will be faster than MAP_POPULATE.
How do you end up allocating memory for guest_memfd? Using simple
fallocate()?
Note that we might improve allocation times with guest_memfd when
allocating larger folios.
--
Cheers,
David / dhildenb
next prev parent reply other threads:[~2024-11-20 13:46 UTC|newest]
Thread overview: 11+ messages / expand[flat|nested] mbox.gz Atom feed top
[not found] <20241024095429.54052-1-kalyazin@amazon.com>
2024-11-20 12:09 ` Nikita Kalyazin
2024-11-20 13:46 ` David Hildenbrand [this message]
2024-11-20 15:13 ` David Hildenbrand
2024-11-20 15:58 ` Nikita Kalyazin
2024-11-20 16:20 ` David Hildenbrand
2024-11-20 16:44 ` David Hildenbrand
2024-11-20 17:21 ` Nikita Kalyazin
2024-11-20 18:29 ` David Hildenbrand
2024-11-21 16:46 ` Nikita Kalyazin
2024-11-26 16:04 ` Nikita Kalyazin
2024-11-28 12:11 ` David Hildenbrand
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=01b0a528-bec0-41d7-80f6-8afe213bd56b@redhat.com \
--to=david@redhat.com \
--cc=brijesh.singh@amd.com \
--cc=corbet@lwn.net \
--cc=derekmn@amazon.com \
--cc=graf@amazon.de \
--cc=jgowans@amazon.com \
--cc=jthoughton@google.com \
--cc=kalyazin@amazon.com \
--cc=kvm@vger.kernel.org \
--cc=linux-doc@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=michael.roth@amd.com \
--cc=nsaenz@amazon.es \
--cc=pbonzini@redhat.com \
--cc=roypat@amazon.co.uk \
--cc=seanjc@google.com \
--cc=xmarcalx@amazon.com \
/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