linux-mm.kvack.org archive mirror
 help / color / mirror / Atom feed
From: Lance Yang <ioworker0@gmail.com>
To: linux-kernel@vger.kernel.org
Cc: Michal Hocko <mhocko@suse.com>, linux-mm@kvack.org
Subject: [Issue] mprotect+madvise may be better than mmap for permission changes and page zeroing
Date: Thu, 25 Jan 2024 20:28:30 +0800	[thread overview]
Message-ID: <CAK1f24mZcxdxKA5hyNaMy-jX=dGtEoZEY8imXKrTnZ0EC61_gw@mail.gmail.com> (raw)

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

Thanks for your time!
Lance Yang


             reply	other threads:[~2024-01-25 12:28 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-01-25 12:28 Lance Yang [this message]
2024-01-25 12:44 ` David Hildenbrand
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='CAK1f24mZcxdxKA5hyNaMy-jX=dGtEoZEY8imXKrTnZ0EC61_gw@mail.gmail.com' \
    --to=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