linux-mm.kvack.org archive mirror
 help / color / mirror / Atom feed
From: Avi Kivity <avi@scylladb.com>
To: David Hildenbrand <david@redhat.com>, linux-mm <linux-mm@kvack.org>
Subject: Re: memfd and transparent hugepages
Date: Mon, 18 Nov 2024 22:05:08 +0200	[thread overview]
Message-ID: <3853f2df4e9b488d705f13a48f15105989fb45d9.camel@scylladb.com> (raw)
In-Reply-To: <e73ef8d0-b02d-43df-8632-7f425011419e@redhat.com>

On Mon, 2024-11-18 at 20:42 +0100, David Hildenbrand wrote:
> On 18.11.24 15:13, Avi Kivity wrote:
> > #define _GNU_SOURCE
> > #include <sys/mman.h>
> > #include <linux/memfd.h>
> > #include <unistd.h>
> > #include <stddef.h>
> > 
> > int main(int ac, char** av) {
> >      size_t memsz = (size_t)2048 << 20;
> >      int fd = memfd_create("memory", MFD_CLOEXEC);
> >      ftruncate(fd, memsz);
> >      void* p = mmap((void*)0x40000000, memsz, PROT_READ|PROT_WRITE,
> > MAP_PRIVATE|MAP_FILE, fd, 0);
> >      madvise(p, memsz, MADV_HUGEPAGE);
> >      madvise(p, memsz, MADV_POPULATE_WRITE);
> >      madvise(p, memsz, MADV_COLLAPSE);
> >      pause();
> >      return 0;
> > }
> 
> Hi,
> 
> > 
> > While memfd is documented as using anonymous pages, AnonHugePages
> > shows
> > as zero.
> 
> it's an anonymous file, not anonymous memory. memfd is shmem in
> diguise.
> 
> > 
> > If memfd incompatible with transparent hugepages?
> 
> Shmem supports THP.
> 
> But you have one flaws in your program: you have to mmap it
> MAP_SHARED. 
> It's shmem.
> 
> Otherwise during MADV_POPULATE_WRITE you're simply allocating a shmem
> page (pageache), and the COW it to map an anonymous page in your page
> tables, resulting in a double memory consumption (pagecache+anonymous
> memory). khugepaged does currently not operate on such VMAs.
> 
> If you use MAP_SHARED in your example above:
> 


Thanks a lot - this works.

In case anyone's interested, I'm using this to provide the equivalent
of vmalloc() in my application. It usually uses a large THP arena
(consuming all of the machine's memory) to allocate from, but sometimes
I need large contiguous memory areas for third-party libraries. I can't
easily allocate large contiguous areas, but 128kB allocations are fine.
So I allocate a few of those, and use mmap(MAP_FIXED) to stitch
together several 128kB allocations into a single large contiguous map.




      reply	other threads:[~2024-11-18 20:05 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-11-18 14:13 Avi Kivity
2024-11-18 19:42 ` David Hildenbrand
2024-11-18 20:05   ` Avi Kivity [this message]

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=3853f2df4e9b488d705f13a48f15105989fb45d9.camel@scylladb.com \
    --to=avi@scylladb.com \
    --cc=david@redhat.com \
    --cc=linux-mm@kvack.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