1*ee65728eSMike Rapoport.. SPDX-License-Identifier: GPL-2.0 2*ee65728eSMike Rapoport 3*ee65728eSMike Rapoport========================================= 4*ee65728eSMike RapoportA vmemmap diet for HugeTLB and Device DAX 5*ee65728eSMike Rapoport========================================= 6*ee65728eSMike Rapoport 7*ee65728eSMike RapoportHugeTLB 8*ee65728eSMike Rapoport======= 9*ee65728eSMike Rapoport 10*ee65728eSMike RapoportThe struct page structures (page structs) are used to describe a physical 11*ee65728eSMike Rapoportpage frame. By default, there is a one-to-one mapping from a page frame to 12*ee65728eSMike Rapoportit's corresponding page struct. 13*ee65728eSMike Rapoport 14*ee65728eSMike RapoportHugeTLB pages consist of multiple base page size pages and is supported by many 15*ee65728eSMike Rapoportarchitectures. See Documentation/admin-guide/mm/hugetlbpage.rst for more 16*ee65728eSMike Rapoportdetails. On the x86-64 architecture, HugeTLB pages of size 2MB and 1GB are 17*ee65728eSMike Rapoportcurrently supported. Since the base page size on x86 is 4KB, a 2MB HugeTLB page 18*ee65728eSMike Rapoportconsists of 512 base pages and a 1GB HugeTLB page consists of 4096 base pages. 19*ee65728eSMike RapoportFor each base page, there is a corresponding page struct. 20*ee65728eSMike Rapoport 21*ee65728eSMike RapoportWithin the HugeTLB subsystem, only the first 4 page structs are used to 22*ee65728eSMike Rapoportcontain unique information about a HugeTLB page. __NR_USED_SUBPAGE provides 23*ee65728eSMike Rapoportthis upper limit. The only 'useful' information in the remaining page structs 24*ee65728eSMike Rapoportis the compound_head field, and this field is the same for all tail pages. 25*ee65728eSMike Rapoport 26*ee65728eSMike RapoportBy removing redundant page structs for HugeTLB pages, memory can be returned 27*ee65728eSMike Rapoportto the buddy allocator for other uses. 28*ee65728eSMike Rapoport 29*ee65728eSMike RapoportDifferent architectures support different HugeTLB pages. For example, the 30*ee65728eSMike Rapoportfollowing table is the HugeTLB page size supported by x86 and arm64 31*ee65728eSMike Rapoportarchitectures. Because arm64 supports 4k, 16k, and 64k base pages and 32*ee65728eSMike Rapoportsupports contiguous entries, so it supports many kinds of sizes of HugeTLB 33*ee65728eSMike Rapoportpage. 34*ee65728eSMike Rapoport 35*ee65728eSMike Rapoport+--------------+-----------+-----------------------------------------------+ 36*ee65728eSMike Rapoport| Architecture | Page Size | HugeTLB Page Size | 37*ee65728eSMike Rapoport+--------------+-----------+-----------+-----------+-----------+-----------+ 38*ee65728eSMike Rapoport| x86-64 | 4KB | 2MB | 1GB | | | 39*ee65728eSMike Rapoport+--------------+-----------+-----------+-----------+-----------+-----------+ 40*ee65728eSMike Rapoport| | 4KB | 64KB | 2MB | 32MB | 1GB | 41*ee65728eSMike Rapoport| +-----------+-----------+-----------+-----------+-----------+ 42*ee65728eSMike Rapoport| arm64 | 16KB | 2MB | 32MB | 1GB | | 43*ee65728eSMike Rapoport| +-----------+-----------+-----------+-----------+-----------+ 44*ee65728eSMike Rapoport| | 64KB | 2MB | 512MB | 16GB | | 45*ee65728eSMike Rapoport+--------------+-----------+-----------+-----------+-----------+-----------+ 46*ee65728eSMike Rapoport 47*ee65728eSMike RapoportWhen the system boot up, every HugeTLB page has more than one struct page 48*ee65728eSMike Rapoportstructs which size is (unit: pages):: 49*ee65728eSMike Rapoport 50*ee65728eSMike Rapoport struct_size = HugeTLB_Size / PAGE_SIZE * sizeof(struct page) / PAGE_SIZE 51*ee65728eSMike Rapoport 52*ee65728eSMike RapoportWhere HugeTLB_Size is the size of the HugeTLB page. We know that the size 53*ee65728eSMike Rapoportof the HugeTLB page is always n times PAGE_SIZE. So we can get the following 54*ee65728eSMike Rapoportrelationship:: 55*ee65728eSMike Rapoport 56*ee65728eSMike Rapoport HugeTLB_Size = n * PAGE_SIZE 57*ee65728eSMike Rapoport 58*ee65728eSMike RapoportThen:: 59*ee65728eSMike Rapoport 60*ee65728eSMike Rapoport struct_size = n * PAGE_SIZE / PAGE_SIZE * sizeof(struct page) / PAGE_SIZE 61*ee65728eSMike Rapoport = n * sizeof(struct page) / PAGE_SIZE 62*ee65728eSMike Rapoport 63*ee65728eSMike RapoportWe can use huge mapping at the pud/pmd level for the HugeTLB page. 64*ee65728eSMike Rapoport 65*ee65728eSMike RapoportFor the HugeTLB page of the pmd level mapping, then:: 66*ee65728eSMike Rapoport 67*ee65728eSMike Rapoport struct_size = n * sizeof(struct page) / PAGE_SIZE 68*ee65728eSMike Rapoport = PAGE_SIZE / sizeof(pte_t) * sizeof(struct page) / PAGE_SIZE 69*ee65728eSMike Rapoport = sizeof(struct page) / sizeof(pte_t) 70*ee65728eSMike Rapoport = 64 / 8 71*ee65728eSMike Rapoport = 8 (pages) 72*ee65728eSMike Rapoport 73*ee65728eSMike RapoportWhere n is how many pte entries which one page can contains. So the value of 74*ee65728eSMike Rapoportn is (PAGE_SIZE / sizeof(pte_t)). 75*ee65728eSMike Rapoport 76*ee65728eSMike RapoportThis optimization only supports 64-bit system, so the value of sizeof(pte_t) 77*ee65728eSMike Rapoportis 8. And this optimization also applicable only when the size of struct page 78*ee65728eSMike Rapoportis a power of two. In most cases, the size of struct page is 64 bytes (e.g. 79*ee65728eSMike Rapoportx86-64 and arm64). So if we use pmd level mapping for a HugeTLB page, the 80*ee65728eSMike Rapoportsize of struct page structs of it is 8 page frames which size depends on the 81*ee65728eSMike Rapoportsize of the base page. 82*ee65728eSMike Rapoport 83*ee65728eSMike RapoportFor the HugeTLB page of the pud level mapping, then:: 84*ee65728eSMike Rapoport 85*ee65728eSMike Rapoport struct_size = PAGE_SIZE / sizeof(pmd_t) * struct_size(pmd) 86*ee65728eSMike Rapoport = PAGE_SIZE / 8 * 8 (pages) 87*ee65728eSMike Rapoport = PAGE_SIZE (pages) 88*ee65728eSMike Rapoport 89*ee65728eSMike RapoportWhere the struct_size(pmd) is the size of the struct page structs of a 90*ee65728eSMike RapoportHugeTLB page of the pmd level mapping. 91*ee65728eSMike Rapoport 92*ee65728eSMike RapoportE.g.: A 2MB HugeTLB page on x86_64 consists in 8 page frames while 1GB 93*ee65728eSMike RapoportHugeTLB page consists in 4096. 94*ee65728eSMike Rapoport 95*ee65728eSMike RapoportNext, we take the pmd level mapping of the HugeTLB page as an example to 96*ee65728eSMike Rapoportshow the internal implementation of this optimization. There are 8 pages 97*ee65728eSMike Rapoportstruct page structs associated with a HugeTLB page which is pmd mapped. 98*ee65728eSMike Rapoport 99*ee65728eSMike RapoportHere is how things look before optimization:: 100*ee65728eSMike Rapoport 101*ee65728eSMike Rapoport HugeTLB struct pages(8 pages) page frame(8 pages) 102*ee65728eSMike Rapoport +-----------+ ---virt_to_page---> +-----------+ mapping to +-----------+ 103*ee65728eSMike Rapoport | | | 0 | -------------> | 0 | 104*ee65728eSMike Rapoport | | +-----------+ +-----------+ 105*ee65728eSMike Rapoport | | | 1 | -------------> | 1 | 106*ee65728eSMike Rapoport | | +-----------+ +-----------+ 107*ee65728eSMike Rapoport | | | 2 | -------------> | 2 | 108*ee65728eSMike Rapoport | | +-----------+ +-----------+ 109*ee65728eSMike Rapoport | | | 3 | -------------> | 3 | 110*ee65728eSMike Rapoport | | +-----------+ +-----------+ 111*ee65728eSMike Rapoport | | | 4 | -------------> | 4 | 112*ee65728eSMike Rapoport | PMD | +-----------+ +-----------+ 113*ee65728eSMike Rapoport | level | | 5 | -------------> | 5 | 114*ee65728eSMike Rapoport | mapping | +-----------+ +-----------+ 115*ee65728eSMike Rapoport | | | 6 | -------------> | 6 | 116*ee65728eSMike Rapoport | | +-----------+ +-----------+ 117*ee65728eSMike Rapoport | | | 7 | -------------> | 7 | 118*ee65728eSMike Rapoport | | +-----------+ +-----------+ 119*ee65728eSMike Rapoport | | 120*ee65728eSMike Rapoport | | 121*ee65728eSMike Rapoport | | 122*ee65728eSMike Rapoport +-----------+ 123*ee65728eSMike Rapoport 124*ee65728eSMike RapoportThe value of page->compound_head is the same for all tail pages. The first 125*ee65728eSMike Rapoportpage of page structs (page 0) associated with the HugeTLB page contains the 4 126*ee65728eSMike Rapoportpage structs necessary to describe the HugeTLB. The only use of the remaining 127*ee65728eSMike Rapoportpages of page structs (page 1 to page 7) is to point to page->compound_head. 128*ee65728eSMike RapoportTherefore, we can remap pages 1 to 7 to page 0. Only 1 page of page structs 129*ee65728eSMike Rapoportwill be used for each HugeTLB page. This will allow us to free the remaining 130*ee65728eSMike Rapoport7 pages to the buddy allocator. 131*ee65728eSMike Rapoport 132*ee65728eSMike RapoportHere is how things look after remapping:: 133*ee65728eSMike Rapoport 134*ee65728eSMike Rapoport HugeTLB struct pages(8 pages) page frame(8 pages) 135*ee65728eSMike Rapoport +-----------+ ---virt_to_page---> +-----------+ mapping to +-----------+ 136*ee65728eSMike Rapoport | | | 0 | -------------> | 0 | 137*ee65728eSMike Rapoport | | +-----------+ +-----------+ 138*ee65728eSMike Rapoport | | | 1 | ---------------^ ^ ^ ^ ^ ^ ^ 139*ee65728eSMike Rapoport | | +-----------+ | | | | | | 140*ee65728eSMike Rapoport | | | 2 | -----------------+ | | | | | 141*ee65728eSMike Rapoport | | +-----------+ | | | | | 142*ee65728eSMike Rapoport | | | 3 | -------------------+ | | | | 143*ee65728eSMike Rapoport | | +-----------+ | | | | 144*ee65728eSMike Rapoport | | | 4 | ---------------------+ | | | 145*ee65728eSMike Rapoport | PMD | +-----------+ | | | 146*ee65728eSMike Rapoport | level | | 5 | -----------------------+ | | 147*ee65728eSMike Rapoport | mapping | +-----------+ | | 148*ee65728eSMike Rapoport | | | 6 | -------------------------+ | 149*ee65728eSMike Rapoport | | +-----------+ | 150*ee65728eSMike Rapoport | | | 7 | ---------------------------+ 151*ee65728eSMike Rapoport | | +-----------+ 152*ee65728eSMike Rapoport | | 153*ee65728eSMike Rapoport | | 154*ee65728eSMike Rapoport | | 155*ee65728eSMike Rapoport +-----------+ 156*ee65728eSMike Rapoport 157*ee65728eSMike RapoportWhen a HugeTLB is freed to the buddy system, we should allocate 7 pages for 158*ee65728eSMike Rapoportvmemmap pages and restore the previous mapping relationship. 159*ee65728eSMike Rapoport 160*ee65728eSMike RapoportFor the HugeTLB page of the pud level mapping. It is similar to the former. 161*ee65728eSMike RapoportWe also can use this approach to free (PAGE_SIZE - 1) vmemmap pages. 162*ee65728eSMike Rapoport 163*ee65728eSMike RapoportApart from the HugeTLB page of the pmd/pud level mapping, some architectures 164*ee65728eSMike Rapoport(e.g. aarch64) provides a contiguous bit in the translation table entries 165*ee65728eSMike Rapoportthat hints to the MMU to indicate that it is one of a contiguous set of 166*ee65728eSMike Rapoportentries that can be cached in a single TLB entry. 167*ee65728eSMike Rapoport 168*ee65728eSMike RapoportThe contiguous bit is used to increase the mapping size at the pmd and pte 169*ee65728eSMike Rapoport(last) level. So this type of HugeTLB page can be optimized only when its 170*ee65728eSMike Rapoportsize of the struct page structs is greater than 1 page. 171*ee65728eSMike Rapoport 172*ee65728eSMike RapoportNotice: The head vmemmap page is not freed to the buddy allocator and all 173*ee65728eSMike Rapoporttail vmemmap pages are mapped to the head vmemmap page frame. So we can see 174*ee65728eSMike Rapoportmore than one struct page struct with PG_head (e.g. 8 per 2 MB HugeTLB page) 175*ee65728eSMike Rapoportassociated with each HugeTLB page. The compound_head() can handle this 176*ee65728eSMike Rapoportcorrectly (more details refer to the comment above compound_head()). 177*ee65728eSMike Rapoport 178*ee65728eSMike RapoportDevice DAX 179*ee65728eSMike Rapoport========== 180*ee65728eSMike Rapoport 181*ee65728eSMike RapoportThe device-dax interface uses the same tail deduplication technique explained 182*ee65728eSMike Rapoportin the previous chapter, except when used with the vmemmap in 183*ee65728eSMike Rapoportthe device (altmap). 184*ee65728eSMike Rapoport 185*ee65728eSMike RapoportThe following page sizes are supported in DAX: PAGE_SIZE (4K on x86_64), 186*ee65728eSMike RapoportPMD_SIZE (2M on x86_64) and PUD_SIZE (1G on x86_64). 187*ee65728eSMike Rapoport 188*ee65728eSMike RapoportThe differences with HugeTLB are relatively minor. 189*ee65728eSMike Rapoport 190*ee65728eSMike RapoportIt only use 3 page structs for storing all information as opposed 191*ee65728eSMike Rapoportto 4 on HugeTLB pages. 192*ee65728eSMike Rapoport 193*ee65728eSMike RapoportThere's no remapping of vmemmap given that device-dax memory is not part of 194*ee65728eSMike RapoportSystem RAM ranges initialized at boot. Thus the tail page deduplication 195*ee65728eSMike Rapoporthappens at a later stage when we populate the sections. HugeTLB reuses the 196*ee65728eSMike Rapoportthe head vmemmap page representing, whereas device-dax reuses the tail 197*ee65728eSMike Rapoportvmemmap page. This results in only half of the savings compared to HugeTLB. 198*ee65728eSMike Rapoport 199*ee65728eSMike RapoportDeduplicated tail pages are not mapped read-only. 200*ee65728eSMike Rapoport 201*ee65728eSMike RapoportHere's how things look like on device-dax after the sections are populated:: 202*ee65728eSMike Rapoport 203*ee65728eSMike Rapoport +-----------+ ---virt_to_page---> +-----------+ mapping to +-----------+ 204*ee65728eSMike Rapoport | | | 0 | -------------> | 0 | 205*ee65728eSMike Rapoport | | +-----------+ +-----------+ 206*ee65728eSMike Rapoport | | | 1 | -------------> | 1 | 207*ee65728eSMike Rapoport | | +-----------+ +-----------+ 208*ee65728eSMike Rapoport | | | 2 | ----------------^ ^ ^ ^ ^ ^ 209*ee65728eSMike Rapoport | | +-----------+ | | | | | 210*ee65728eSMike Rapoport | | | 3 | ------------------+ | | | | 211*ee65728eSMike Rapoport | | +-----------+ | | | | 212*ee65728eSMike Rapoport | | | 4 | --------------------+ | | | 213*ee65728eSMike Rapoport | PMD | +-----------+ | | | 214*ee65728eSMike Rapoport | level | | 5 | ----------------------+ | | 215*ee65728eSMike Rapoport | mapping | +-----------+ | | 216*ee65728eSMike Rapoport | | | 6 | ------------------------+ | 217*ee65728eSMike Rapoport | | +-----------+ | 218*ee65728eSMike Rapoport | | | 7 | --------------------------+ 219*ee65728eSMike Rapoport | | +-----------+ 220*ee65728eSMike Rapoport | | 221*ee65728eSMike Rapoport | | 222*ee65728eSMike Rapoport | | 223*ee65728eSMike Rapoport +-----------+ 224