andreas
andreas

Reputation: 153

Allocating huge pages in kernel modules

I'm looking for a way to allocate huge pages (2M or 1G) in a kernel module (I'm using kernel version 4.15.0).

In user space, I can mount the hugetlbfs file system, and then allocate huge pages using mmap (see, e.g., https://blog.kevinhu.me/2018/07/01/01-Linux-Hugepages/). Is there a similar way to do this in kernel space?

I'm aware that I could allocate them in user space first, and then pass them to the kernel using get_user_pages, as described in Sequential access to hugepages in kernel driver. However, I'm looking for a more direct way to allocate them, as I only need them in kernel space.

Upvotes: 5

Views: 1616

Answers (1)

steve
steve

Reputation: 6020

Something similar to

kmalloc(0x200000, GFP_KERNEL | __GFP_COMP)

should work.

As explained in this LWN article:

A compound page is simply a grouping of two or more physically contiguous pages into a unit that can, in many ways, be treated as a single, larger page. They are most commonly used to create huge pages, used within hugetlbfs or the transparent huge pages subsystem, but they show up in other contexts as well. Compound pages can serve as anonymous memory or be used as buffers within the kernel; they cannot, however, appear in the page cache, which is only prepared to deal with singleton pages.

This makes the assumption that huge pages are configured and available.

Upvotes: 2

Related Questions