From: David Hildenbrand <david@redhat.com>
To: Lance Yang <ioworker0@gmail.com>, linux-kernel@vger.kernel.org
Cc: Michal Hocko <mhocko@suse.com>, linux-mm@kvack.org
Subject: Re: [Issue] mprotect+madvise may be better than mmap for permission changes and page zeroing
Date: Thu, 25 Jan 2024 13:44:36 +0100 [thread overview]
Message-ID: <c9b4dfc0-19b9-4e19-b681-e2fab63f41e8@redhat.com> (raw)
In-Reply-To: <CAK1f24mZcxdxKA5hyNaMy-jX=dGtEoZEY8imXKrTnZ0EC61_gw@mail.gmail.com>
On 25.01.24 13:28, Lance Yang wrote:
> Hello Everyone,
>
> I've noticed that using mprotect(PROT_NONE) with
> madvise(MADV_DONTNEED) is much faster than
> mmap(PROT_NONE, MAP_FIXED) alone for changing
> permissions and zeroing pages.
>
> I have maintained a chunk-allocator internally at the
> company. It allocates a chunk using
> mmap(PROT_READ|PROT_WRITE, MAP_ANON|MAP_PRIVATE, -1, 0),
> releases a chunk using mmap(PROT_NONE, MAP_ANON|MAP_FIXED|MAP_PRIVATE, -1, 0),
> and reuses a chunk using mprotect(PROT_READ|PROT_WRITE).
> Recently, I replaced mmap(PROT_NONE, MAP_FIXED) with
> mprotect(PROT_NONE) + madvise(MADV_DONTNEED) to
> reduce the latency of releasing chunks.
>
> Test code:
> https://github.com/ioworker0/mmapvsmprotect/blob/main/test2.c
>
> Here are the test results on my machine:
> CPU: AMD EPYC 7R13 Processor
> Kernel: 6.2.0
> Elapsed Time for mprotect+madvise: 3670 nanoseconds
> Elapsed Time for mmap: 5520 nanoseconds
mprotect+madvise won't free page tables, mmap will. That's the biggest
difference.
--
Cheers,
David / dhildenb
next prev parent reply other threads:[~2024-01-25 12:44 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-01-25 12:28 Lance Yang
2024-01-25 12:44 ` David Hildenbrand [this message]
2024-01-25 13:03 ` Lance Yang
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=c9b4dfc0-19b9-4e19-b681-e2fab63f41e8@redhat.com \
--to=david@redhat.com \
--cc=ioworker0@gmail.com \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=mhocko@suse.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