linux-mm.kvack.org archive mirror
 help / color / mirror / Atom feed
* [Issue] mprotect+madvise may be better than mmap for permission changes and page zeroing
@ 2024-01-25 12:28 Lance Yang
  2024-01-25 12:44 ` David Hildenbrand
  0 siblings, 1 reply; 3+ messages in thread
From: Lance Yang @ 2024-01-25 12:28 UTC (permalink / raw)
  To: linux-kernel; +Cc: Michal Hocko, linux-mm

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


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [Issue] mprotect+madvise may be better than mmap for permission changes and page zeroing
  2024-01-25 12:28 [Issue] mprotect+madvise may be better than mmap for permission changes and page zeroing Lance Yang
@ 2024-01-25 12:44 ` David Hildenbrand
  2024-01-25 13:03   ` Lance Yang
  0 siblings, 1 reply; 3+ messages in thread
From: David Hildenbrand @ 2024-01-25 12:44 UTC (permalink / raw)
  To: Lance Yang, linux-kernel; +Cc: Michal Hocko, linux-mm

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



^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [Issue] mprotect+madvise may be better than mmap for permission changes and page zeroing
  2024-01-25 12:44 ` David Hildenbrand
@ 2024-01-25 13:03   ` Lance Yang
  0 siblings, 0 replies; 3+ messages in thread
From: Lance Yang @ 2024-01-25 13:03 UTC (permalink / raw)
  To: David Hildenbrand; +Cc: linux-kernel, Michal Hocko, linux-mm

Thanks!

On Thu, Jan 25, 2024 at 8:44 PM David Hildenbrand <david@redhat.com> wrote:
>
> 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
>


^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2024-01-25 13:03 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-01-25 12:28 [Issue] mprotect+madvise may be better than mmap for permission changes and page zeroing Lance Yang
2024-01-25 12:44 ` David Hildenbrand
2024-01-25 13:03   ` Lance Yang

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox