Rocksolid Light

Welcome to RetroBBS

mail  files  register  newsreader  groups  login

Message-ID:  

Invest in physics -- own a piece of Dirac!


computers / comp.os.linux.misc / Linux HugeTLB: What is the advantage of the filesystem approach?

SubjectAuthor
o Linux HugeTLB: What is the advantage of the filesystem approach?Lukas Barth

1
Linux HugeTLB: What is the advantage of the filesystem approach?

<ub80g3$1b192$1@dont-email.me>

  copy mid

https://www.rocksolidbbs.com/computers/article-flat.php?id=13340&group=comp.os.linux.misc#13340

  copy link   Newsgroups: comp.os.linux.misc
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: usenet23@tinloaf.de (Lukas Barth)
Newsgroups: comp.os.linux.misc
Subject: Linux HugeTLB: What is the advantage of the filesystem approach?
Date: Sat, 12 Aug 2023 13:12:35 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 76
Message-ID: <ub80g3$1b192$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Injection-Date: Sat, 12 Aug 2023 13:12:35 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="29193981ee74d6d9ae6a5ad53ec620c3";
logging-data="1410338"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1//hEVb80nX22e/IgSefPb8"
User-Agent: Pan/0.149 (Bellevue; 4c157ba)
Cancel-Lock: sha1:YzLWg7woSnOr1i3aP+v4e5w9keU=
 by: Lukas Barth - Sat, 12 Aug 2023 13:12 UTC

Hi,

I am currently exploring the various ways of allocating memory in huge
pages under Linux. I somehow can not wrap my head around the concept of
the HugeTLB 'filesystem'. Note that I'm not talking about transparent huge
pages - those are a whole different beast.

The Conventional Way
--------------------

The conventional wisdom (as e.g. presented in the Debian Wiki[0] or the
Kernel docs[1]) seems to be:

- Make sure set your kernel configuration correctly
- set various kernel parameters right
- mount a special filesystem (`hugetlbfs`) to some arbitrary directory,
say `/dev/hugepages/` (that seems to be the default on Fedora…)
- `mmap()` a file within that directory into your address space, i.e.,
something like:

```
int fd = open("/dev/hugepages/myfile, O_CREAT | O_RDWR, 0755);
void * addr = mmap(0, 10*1024*1024, (PROT_READ | PROT_WRITE), MAP_SHARED,
fd, 0);
```

… and if these two calls succeed, I should have `addr` pointing to 10 MB
of memory allocated in five 2 MB huge pages. Nice.

The Easy Way
------------

However, this seems awfully overcomplicated?

At least on Linux 5.15 the whole filesystem thing seems to be completely
unnecessary. I just tried this:

* kernel configured with HugeTLBfs
* kernel parameters set correctly (i.e., `vm.nr_hugepages > 0`)
* no `hugetlbfs` mounted anywhere

And then just do an `mmap` of anonymous memory:

```
void *addr = mmap(0, 10*1024*1024, (PROT_READ | PROT_WRITE),
(MAP_PRIVATE | MAP_ANONYMOUS | MAP_HUGETLB), 0, 0);
```

This gives me 10 MB of memory allocated in huge pages (at least if I don't
fail at interpreting the flags in the page table).

Why the Filesystem?
-------------------

So my question is: Why the filesystem? Is it actually "necessary" to go
via the filesystem, as the various guides suggest, and my attempt above
was just lucky? Does the filesystem approach have other advantages (aside
from having a file which represents parts of your RAM, which seems like a
huge footgun…)? Or is this maybe just a remnant from some previous time,
when `MAP_ANONYMOUS | MAP_HUGETLB` was not allowed?

Thanks for any hints!

Lukas

P.S.: If you collect internet points, you can also anwser this question on
the Unix StackExchange at[2].

[0] https://wiki.debian.org/Hugepages#Enabling_HugeTlbPage

[1] https://www.kernel.org/doc/html/latest/admin-guide/mm/
hugetlbpage.html#using-huge-pages

[2] https://unix.stackexchange.com/questions/753039/linux-hugetlb-what-is-
the-advantage-of-the-filesystem-approach

1
server_pubkey.txt

rocksolid light 0.9.81
clearnet tor