The implementation of the giant page involves two modules: Hugetlb and HUGETLBFS.
Hugetlb equivalent to huge page manager, HUGETLBFS is used to provide users with a set of file system-based giant page interface, the implementation of its lower functionality, it is dependent on hugetlb.
1. Hugetlb Module
struct Hstate hstates[huge_max_hstate];
A hstate array is defined above.
Each hstate is equivalent to a huge_page pool. Different hstate, the page size is not the same, such as 2M, 1G and so on.
Max_hstate identifies how many hstate are currently present, that is, the number of elements that are valid for the first array.
By default, a hstate is created in Hugetlb_init, whose page size is the default size, and this hstate is the default hstate.
If the HUGETLB.C is compiled into the kernel and the kernel is booted, there is a hugepagesz= option in the command line arguments.
Then the Setup_hugepagesz will be called for processing, and Setup_hugepagesz should be executed before hugetlb_init.
Setup_hugepagesz calls Hugetlb_add_hstate to add a hstate whose pagesize size is the size specified in the command-line arguments.
2. HUGETLBFS Module
HUGETLBFS is loaded, the Hugetlbfs file system is registered to the kernel, and the Mount Hugetlbfs file system is stored in the kernel, and the results are saved to hugetlbfs_vfsmount. This mount, which does not use any parameters, therefore corresponds to the default hstate.
3. Use the giant page
There are two ways of mmap and sharing memory (Shmget/shmat).
However, either way, the end result is achieved by making a memory map of a HUGETLBFS type of file (done by File->f_op->mmap).
A. Mmap Way
This way, you need to mount a HUGETLBFS file system with the following command, specifying the page size by pagesize.
Mount-t Hugetlbfs None/mnt/path/to/hugetlbfs-o pagesize=2048k
In this case, the newly mounted file system is associated with a page size of 2048K hstate.
Next, create the file under/mnt/path/to/hugetlbfs, then open the file and make a memory map through Mmap.
B. Shared memory mode
This way, the above mentioned mount and create file operations are not required. Using Shmget and Shmat directly, you can use the giant page memory.
Although the user does not have mount and creates the file, a file is created inside Shmget, and is below the Hugetlbfs_vfsmount mount point mentioned above. In this way, it is the same as the mmap. The Hugetlbfs_vfsmount mount point corresponds to the default hstate, so the page size of the giant page used is also the default.
Linux kernel giant page code learning