2

It is my understanding that currently, on linux, there is no way to mmap a file (either on xfs or ext4) and then write to it and also somehow use huge pages.

Is this correct or is this outdated information and there is a way to do so now?

Thanks.

MK.
  • 3,907
  • 5
  • 34
  • 46
  • What makes you think that? You could [mmap(2)](http://man7.org/linux/man-pages/man2/mmap.2.html) with `MAP_HUGETLB` & `MAP_HUGE_1GB`. Please show some [MCVE] in your question – Basile Starynkevitch Aug 07 '18 at 04:27
  • @BasileStarynkevitch If you try mmap with MAP_HUGETLB | MAP_SHARED , it fails with EINVAL. (You need MAP_SHARED when dealing with disk files.) – MK. Aug 07 '18 at 05:39
  • That might depend on the kernel and the file system you are using (and their configuration and settings). And that might not matter much for performance (e.g. using [posix_fadvise(2)](http://man7.org/linux/man-pages/man2/posix_fadvise.2.html) might matter a lot more) – Basile Starynkevitch Aug 07 '18 at 05:42
  • Do you know of any filesystem that supports this combination (MAP_HUGETLB | MAP_SHARED)? – MK. Aug 07 '18 at 05:44
  • That is a different question. I don't know. Perhaps the setting (e.g. block size, at `mkfs` time) of the file system is important. You should edit your question to motivate it (it looks like some [XY problem](http://xyproblem.info/)...) – Basile Starynkevitch Aug 07 '18 at 05:44

1 Answers1

1

If you mmap a file with MAP_HUGETLB that is not from 'hugetlbfs', the mmap will fail. From ksys_mmap_pgoff()

if (!(flags & MAP_ANONYMOUS)) {
    ...
    if (unlikely(flags & MAP_HUGETLB && !is_file_hugepages(file)))
         goto out_fput;

is_file_hugepages() checks if the file ops is hugetlbfs_file_operations, which won't be true for e.g. ext4.

However, you might be able to use transparent huge pages. Currently (4.19 or so), you also have to use DAX (direct access, often used with nvdimms and persistent memory). I haven't done it yet, but that's from tracing through the code. Specifically, to get huge pages working, you'll at least need a huge-page-aligned address, which comes from thp_get_unmapped_area(), which bails out if you aren't using DAX:

 if (!IS_DAX(filp->f_mapping->host) || !IS_ENABLED(CONFIG_FS_DAX_PMD))
    goto out;
noname
  • 31
  • 2