hugetlbfs_reserv.txt 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530
  1. Hugetlbfs Reservation Overview
  2. ------------------------------
  3. Huge pages as described at 'Documentation/vm/hugetlbpage.txt' are typically
  4. preallocated for application use. These huge pages are instantiated in a
  5. task's address space at page fault time if the VMA indicates huge pages are
  6. to be used. If no huge page exists at page fault time, the task is sent
  7. a SIGBUS and often dies an unhappy death. Shortly after huge page support
  8. was added, it was determined that it would be better to detect a shortage
  9. of huge pages at mmap() time. The idea is that if there were not enough
  10. huge pages to cover the mapping, the mmap() would fail. This was first
  11. done with a simple check in the code at mmap() time to determine if there
  12. were enough free huge pages to cover the mapping. Like most things in the
  13. kernel, the code has evolved over time. However, the basic idea was to
  14. 'reserve' huge pages at mmap() time to ensure that huge pages would be
  15. available for page faults in that mapping. The description below attempts to
  16. describe how huge page reserve processing is done in the v4.10 kernel.
  17. Audience
  18. --------
  19. This description is primarily targeted at kernel developers who are modifying
  20. hugetlbfs code.
  21. The Data Structures
  22. -------------------
  23. resv_huge_pages
  24. This is a global (per-hstate) count of reserved huge pages. Reserved
  25. huge pages are only available to the task which reserved them.
  26. Therefore, the number of huge pages generally available is computed
  27. as (free_huge_pages - resv_huge_pages).
  28. Reserve Map
  29. A reserve map is described by the structure:
  30. struct resv_map {
  31. struct kref refs;
  32. spinlock_t lock;
  33. struct list_head regions;
  34. long adds_in_progress;
  35. struct list_head region_cache;
  36. long region_cache_count;
  37. };
  38. There is one reserve map for each huge page mapping in the system.
  39. The regions list within the resv_map describes the regions within
  40. the mapping. A region is described as:
  41. struct file_region {
  42. struct list_head link;
  43. long from;
  44. long to;
  45. };
  46. The 'from' and 'to' fields of the file region structure are huge page
  47. indices into the mapping. Depending on the type of mapping, a
  48. region in the reserv_map may indicate reservations exist for the
  49. range, or reservations do not exist.
  50. Flags for MAP_PRIVATE Reservations
  51. These are stored in the bottom bits of the reservation map pointer.
  52. #define HPAGE_RESV_OWNER (1UL << 0) Indicates this task is the
  53. owner of the reservations associated with the mapping.
  54. #define HPAGE_RESV_UNMAPPED (1UL << 1) Indicates task originally
  55. mapping this range (and creating reserves) has unmapped a
  56. page from this task (the child) due to a failed COW.
  57. Page Flags
  58. The PagePrivate page flag is used to indicate that a huge page
  59. reservation must be restored when the huge page is freed. More
  60. details will be discussed in the "Freeing huge pages" section.
  61. Reservation Map Location (Private or Shared)
  62. --------------------------------------------
  63. A huge page mapping or segment is either private or shared. If private,
  64. it is typically only available to a single address space (task). If shared,
  65. it can be mapped into multiple address spaces (tasks). The location and
  66. semantics of the reservation map is significantly different for two types
  67. of mappings. Location differences are:
  68. - For private mappings, the reservation map hangs off the the VMA structure.
  69. Specifically, vma->vm_private_data. This reserve map is created at the
  70. time the mapping (mmap(MAP_PRIVATE)) is created.
  71. - For shared mappings, the reservation map hangs off the inode. Specifically,
  72. inode->i_mapping->private_data. Since shared mappings are always backed
  73. by files in the hugetlbfs filesystem, the hugetlbfs code ensures each inode
  74. contains a reservation map. As a result, the reservation map is allocated
  75. when the inode is created.
  76. Creating Reservations
  77. ---------------------
  78. Reservations are created when a huge page backed shared memory segment is
  79. created (shmget(SHM_HUGETLB)) or a mapping is created via mmap(MAP_HUGETLB).
  80. These operations result in a call to the routine hugetlb_reserve_pages()
  81. int hugetlb_reserve_pages(struct inode *inode,
  82. long from, long to,
  83. struct vm_area_struct *vma,
  84. vm_flags_t vm_flags)
  85. The first thing hugetlb_reserve_pages() does is check for the NORESERVE
  86. flag was specified in either the shmget() or mmap() call. If NORESERVE
  87. was specified, then this routine returns immediately as no reservation
  88. are desired.
  89. The arguments 'from' and 'to' are huge page indices into the mapping or
  90. underlying file. For shmget(), 'from' is always 0 and 'to' corresponds to
  91. the length of the segment/mapping. For mmap(), the offset argument could
  92. be used to specify the offset into the underlying file. In such a case
  93. the 'from' and 'to' arguments have been adjusted by this offset.
  94. One of the big differences between PRIVATE and SHARED mappings is the way
  95. in which reservations are represented in the reservation map.
  96. - For shared mappings, an entry in the reservation map indicates a reservation
  97. exists or did exist for the corresponding page. As reservations are
  98. consumed, the reservation map is not modified.
  99. - For private mappings, the lack of an entry in the reservation map indicates
  100. a reservation exists for the corresponding page. As reservations are
  101. consumed, entries are added to the reservation map. Therefore, the
  102. reservation map can also be used to determine which reservations have
  103. been consumed.
  104. For private mappings, hugetlb_reserve_pages() creates the reservation map and
  105. hangs it off the VMA structure. In addition, the HPAGE_RESV_OWNER flag is set
  106. to indicate this VMA owns the reservations.
  107. The reservation map is consulted to determine how many huge page reservations
  108. are needed for the current mapping/segment. For private mappings, this is
  109. always the value (to - from). However, for shared mappings it is possible that some reservations may already exist within the range (to - from). See the
  110. section "Reservation Map Modifications" for details on how this is accomplished.
  111. The mapping may be associated with a subpool. If so, the subpool is consulted
  112. to ensure there is sufficient space for the mapping. It is possible that the
  113. subpool has set aside reservations that can be used for the mapping. See the
  114. section "Subpool Reservations" for more details.
  115. After consulting the reservation map and subpool, the number of needed new
  116. reservations is known. The routine hugetlb_acct_memory() is called to check
  117. for and take the requested number of reservations. hugetlb_acct_memory()
  118. calls into routines that potentially allocate and adjust surplus page counts.
  119. However, within those routines the code is simply checking to ensure there
  120. are enough free huge pages to accommodate the reservation. If there are,
  121. the global reservation count resv_huge_pages is adjusted something like the
  122. following.
  123. if (resv_needed <= (resv_huge_pages - free_huge_pages))
  124. resv_huge_pages += resv_needed;
  125. Note that the global lock hugetlb_lock is held when checking and adjusting
  126. these counters.
  127. If there were enough free huge pages and the global count resv_huge_pages
  128. was adjusted, then the reservation map associated with the mapping is
  129. modified to reflect the reservations. In the case of a shared mapping, a
  130. file_region will exist that includes the range 'from' 'to'. For private
  131. mappings, no modifications are made to the reservation map as lack of an
  132. entry indicates a reservation exists.
  133. If hugetlb_reserve_pages() was successful, the global reservation count and
  134. reservation map associated with the mapping will be modified as required to
  135. ensure reservations exist for the range 'from' - 'to'.
  136. Consuming Reservations/Allocating a Huge Page
  137. ---------------------------------------------
  138. Reservations are consumed when huge pages associated with the reservations
  139. are allocated and instantiated in the corresponding mapping. The allocation
  140. is performed within the routine alloc_huge_page().
  141. struct page *alloc_huge_page(struct vm_area_struct *vma,
  142. unsigned long addr, int avoid_reserve)
  143. alloc_huge_page is passed a VMA pointer and a virtual address, so it can
  144. consult the reservation map to determine if a reservation exists. In addition,
  145. alloc_huge_page takes the argument avoid_reserve which indicates reserves
  146. should not be used even if it appears they have been set aside for the
  147. specified address. The avoid_reserve argument is most often used in the case
  148. of Copy on Write and Page Migration where additional copies of an existing
  149. page are being allocated.
  150. The helper routine vma_needs_reservation() is called to determine if a
  151. reservation exists for the address within the mapping(vma). See the section
  152. "Reservation Map Helper Routines" for detailed information on what this
  153. routine does. The value returned from vma_needs_reservation() is generally
  154. 0 or 1. 0 if a reservation exists for the address, 1 if no reservation exists.
  155. If a reservation does not exist, and there is a subpool associated with the
  156. mapping the subpool is consulted to determine if it contains reservations.
  157. If the subpool contains reservations, one can be used for this allocation.
  158. However, in every case the avoid_reserve argument overrides the use of
  159. a reservation for the allocation. After determining whether a reservation
  160. exists and can be used for the allocation, the routine dequeue_huge_page_vma()
  161. is called. This routine takes two arguments related to reservations:
  162. - avoid_reserve, this is the same value/argument passed to alloc_huge_page()
  163. - chg, even though this argument is of type long only the values 0 or 1 are
  164. passed to dequeue_huge_page_vma. If the value is 0, it indicates a
  165. reservation exists (see the section "Memory Policy and Reservations" for
  166. possible issues). If the value is 1, it indicates a reservation does not
  167. exist and the page must be taken from the global free pool if possible.
  168. The free lists associated with the memory policy of the VMA are searched for
  169. a free page. If a page is found, the value free_huge_pages is decremented
  170. when the page is removed from the free list. If there was a reservation
  171. associated with the page, the following adjustments are made:
  172. SetPagePrivate(page); /* Indicates allocating this page consumed
  173. * a reservation, and if an error is
  174. * encountered such that the page must be
  175. * freed, the reservation will be restored. */
  176. resv_huge_pages--; /* Decrement the global reservation count */
  177. Note, if no huge page can be found that satisfies the VMA's memory policy
  178. an attempt will be made to allocate one using the buddy allocator. This
  179. brings up the issue of surplus huge pages and overcommit which is beyond
  180. the scope reservations. Even if a surplus page is allocated, the same
  181. reservation based adjustments as above will be made: SetPagePrivate(page) and
  182. resv_huge_pages--.
  183. After obtaining a new huge page, (page)->private is set to the value of
  184. the subpool associated with the page if it exists. This will be used for
  185. subpool accounting when the page is freed.
  186. The routine vma_commit_reservation() is then called to adjust the reserve
  187. map based on the consumption of the reservation. In general, this involves
  188. ensuring the page is represented within a file_region structure of the region
  189. map. For shared mappings where the the reservation was present, an entry
  190. in the reserve map already existed so no change is made. However, if there
  191. was no reservation in a shared mapping or this was a private mapping a new
  192. entry must be created.
  193. It is possible that the reserve map could have been changed between the call
  194. to vma_needs_reservation() at the beginning of alloc_huge_page() and the
  195. call to vma_commit_reservation() after the page was allocated. This would
  196. be possible if hugetlb_reserve_pages was called for the same page in a shared
  197. mapping. In such cases, the reservation count and subpool free page count
  198. will be off by one. This rare condition can be identified by comparing the
  199. return value from vma_needs_reservation and vma_commit_reservation. If such
  200. a race is detected, the subpool and global reserve counts are adjusted to
  201. compensate. See the section "Reservation Map Helper Routines" for more
  202. information on these routines.
  203. Instantiate Huge Pages
  204. ----------------------
  205. After huge page allocation, the page is typically added to the page tables
  206. of the allocating task. Before this, pages in a shared mapping are added
  207. to the page cache and pages in private mappings are added to an anonymous
  208. reverse mapping. In both cases, the PagePrivate flag is cleared. Therefore,
  209. when a huge page that has been instantiated is freed no adjustment is made
  210. to the global reservation count (resv_huge_pages).
  211. Freeing Huge Pages
  212. ------------------
  213. Huge page freeing is performed by the routine free_huge_page(). This routine
  214. is the destructor for hugetlbfs compound pages. As a result, it is only
  215. passed a pointer to the page struct. When a huge page is freed, reservation
  216. accounting may need to be performed. This would be the case if the page was
  217. associated with a subpool that contained reserves, or the page is being freed
  218. on an error path where a global reserve count must be restored.
  219. The page->private field points to any subpool associated with the page.
  220. If the PagePrivate flag is set, it indicates the global reserve count should
  221. be adjusted (see the section "Consuming Reservations/Allocating a Huge Page"
  222. for information on how these are set).
  223. The routine first calls hugepage_subpool_put_pages() for the page. If this
  224. routine returns a value of 0 (which does not equal the value passed 1) it
  225. indicates reserves are associated with the subpool, and this newly free page
  226. must be used to keep the number of subpool reserves above the minimum size.
  227. Therefore, the global resv_huge_pages counter is incremented in this case.
  228. If the PagePrivate flag was set in the page, the global resv_huge_pages counter
  229. will always be incremented.
  230. Subpool Reservations
  231. --------------------
  232. There is a struct hstate associated with each huge page size. The hstate
  233. tracks all huge pages of the specified size. A subpool represents a subset
  234. of pages within a hstate that is associated with a mounted hugetlbfs
  235. filesystem.
  236. When a hugetlbfs filesystem is mounted a min_size option can be specified
  237. which indicates the minimum number of huge pages required by the filesystem.
  238. If this option is specified, the number of huge pages corresponding to
  239. min_size are reserved for use by the filesystem. This number is tracked in
  240. the min_hpages field of a struct hugepage_subpool. At mount time,
  241. hugetlb_acct_memory(min_hpages) is called to reserve the specified number of
  242. huge pages. If they can not be reserved, the mount fails.
  243. The routines hugepage_subpool_get/put_pages() are called when pages are
  244. obtained from or released back to a subpool. They perform all subpool
  245. accounting, and track any reservations associated with the subpool.
  246. hugepage_subpool_get/put_pages are passed the number of huge pages by which
  247. to adjust the subpool 'used page' count (down for get, up for put). Normally,
  248. they return the same value that was passed or an error if not enough pages
  249. exist in the subpool.
  250. However, if reserves are associated with the subpool a return value less
  251. than the passed value may be returned. This return value indicates the
  252. number of additional global pool adjustments which must be made. For example,
  253. suppose a subpool contains 3 reserved huge pages and someone asks for 5.
  254. The 3 reserved pages associated with the subpool can be used to satisfy part
  255. of the request. But, 2 pages must be obtained from the global pools. To
  256. relay this information to the caller, the value 2 is returned. The caller
  257. is then responsible for attempting to obtain the additional two pages from
  258. the global pools.
  259. COW and Reservations
  260. --------------------
  261. Since shared mappings all point to and use the same underlying pages, the
  262. biggest reservation concern for COW is private mappings. In this case,
  263. two tasks can be pointing at the same previously allocated page. One task
  264. attempts to write to the page, so a new page must be allocated so that each
  265. task points to its own page.
  266. When the page was originally allocated, the reservation for that page was
  267. consumed. When an attempt to allocate a new page is made as a result of
  268. COW, it is possible that no free huge pages are free and the allocation
  269. will fail.
  270. When the private mapping was originally created, the owner of the mapping
  271. was noted by setting the HPAGE_RESV_OWNER bit in the pointer to the reservation
  272. map of the owner. Since the owner created the mapping, the owner owns all
  273. the reservations associated with the mapping. Therefore, when a write fault
  274. occurs and there is no page available, different action is taken for the owner
  275. and non-owner of the reservation.
  276. In the case where the faulting task is not the owner, the fault will fail and
  277. the task will typically receive a SIGBUS.
  278. If the owner is the faulting task, we want it to succeed since it owned the
  279. original reservation. To accomplish this, the page is unmapped from the
  280. non-owning task. In this way, the only reference is from the owning task.
  281. In addition, the HPAGE_RESV_UNMAPPED bit is set in the reservation map pointer
  282. of the non-owning task. The non-owning task may receive a SIGBUS if it later
  283. faults on a non-present page. But, the original owner of the
  284. mapping/reservation will behave as expected.
  285. Reservation Map Modifications
  286. -----------------------------
  287. The following low level routines are used to make modifications to a
  288. reservation map. Typically, these routines are not called directly. Rather,
  289. a reservation map helper routine is called which calls one of these low level
  290. routines. These low level routines are fairly well documented in the source
  291. code (mm/hugetlb.c). These routines are:
  292. long region_chg(struct resv_map *resv, long f, long t);
  293. long region_add(struct resv_map *resv, long f, long t);
  294. void region_abort(struct resv_map *resv, long f, long t);
  295. long region_count(struct resv_map *resv, long f, long t);
  296. Operations on the reservation map typically involve two operations:
  297. 1) region_chg() is called to examine the reserve map and determine how
  298. many pages in the specified range [f, t) are NOT currently represented.
  299. The calling code performs global checks and allocations to determine if
  300. there are enough huge pages for the operation to succeed.
  301. 2a) If the operation can succeed, region_add() is called to actually modify
  302. the reservation map for the same range [f, t) previously passed to
  303. region_chg().
  304. 2b) If the operation can not succeed, region_abort is called for the same range
  305. [f, t) to abort the operation.
  306. Note that this is a two step process where region_add() and region_abort()
  307. are guaranteed to succeed after a prior call to region_chg() for the same
  308. range. region_chg() is responsible for pre-allocating any data structures
  309. necessary to ensure the subsequent operations (specifically region_add()))
  310. will succeed.
  311. As mentioned above, region_chg() determines the number of pages in the range
  312. which are NOT currently represented in the map. This number is returned to
  313. the caller. region_add() returns the number of pages in the range added to
  314. the map. In most cases, the return value of region_add() is the same as the
  315. return value of region_chg(). However, in the case of shared mappings it is
  316. possible for changes to the reservation map to be made between the calls to
  317. region_chg() and region_add(). In this case, the return value of region_add()
  318. will not match the return value of region_chg(). It is likely that in such
  319. cases global counts and subpool accounting will be incorrect and in need of
  320. adjustment. It is the responsibility of the caller to check for this condition
  321. and make the appropriate adjustments.
  322. The routine region_del() is called to remove regions from a reservation map.
  323. It is typically called in the following situations:
  324. - When a file in the hugetlbfs filesystem is being removed, the inode will
  325. be released and the reservation map freed. Before freeing the reservation
  326. map, all the individual file_region structures must be freed. In this case
  327. region_del is passed the range [0, LONG_MAX).
  328. - When a hugetlbfs file is being truncated. In this case, all allocated pages
  329. after the new file size must be freed. In addition, any file_region entries
  330. in the reservation map past the new end of file must be deleted. In this
  331. case, region_del is passed the range [new_end_of_file, LONG_MAX).
  332. - When a hole is being punched in a hugetlbfs file. In this case, huge pages
  333. are removed from the middle of the file one at a time. As the pages are
  334. removed, region_del() is called to remove the corresponding entry from the
  335. reservation map. In this case, region_del is passed the range
  336. [page_idx, page_idx + 1).
  337. In every case, region_del() will return the number of pages removed from the
  338. reservation map. In VERY rare cases, region_del() can fail. This can only
  339. happen in the hole punch case where it has to split an existing file_region
  340. entry and can not allocate a new structure. In this error case, region_del()
  341. will return -ENOMEM. The problem here is that the reservation map will
  342. indicate that there is a reservation for the page. However, the subpool and
  343. global reservation counts will not reflect the reservation. To handle this
  344. situation, the routine hugetlb_fix_reserve_counts() is called to adjust the
  345. counters so that they correspond with the reservation map entry that could
  346. not be deleted.
  347. region_count() is called when unmapping a private huge page mapping. In
  348. private mappings, the lack of a entry in the reservation map indicates that
  349. a reservation exists. Therefore, by counting the number of entries in the
  350. reservation map we know how many reservations were consumed and how many are
  351. outstanding (outstanding = (end - start) - region_count(resv, start, end)).
  352. Since the mapping is going away, the subpool and global reservation counts
  353. are decremented by the number of outstanding reservations.
  354. Reservation Map Helper Routines
  355. -------------------------------
  356. Several helper routines exist to query and modify the reservation maps.
  357. These routines are only interested with reservations for a specific huge
  358. page, so they just pass in an address instead of a range. In addition,
  359. they pass in the associated VMA. From the VMA, the type of mapping (private
  360. or shared) and the location of the reservation map (inode or VMA) can be
  361. determined. These routines simply call the underlying routines described
  362. in the section "Reservation Map Modifications". However, they do take into
  363. account the 'opposite' meaning of reservation map entries for private and
  364. shared mappings and hide this detail from the caller.
  365. long vma_needs_reservation(struct hstate *h,
  366. struct vm_area_struct *vma, unsigned long addr)
  367. This routine calls region_chg() for the specified page. If no reservation
  368. exists, 1 is returned. If a reservation exists, 0 is returned.
  369. long vma_commit_reservation(struct hstate *h,
  370. struct vm_area_struct *vma, unsigned long addr)
  371. This calls region_add() for the specified page. As in the case of region_chg
  372. and region_add, this routine is to be called after a previous call to
  373. vma_needs_reservation. It will add a reservation entry for the page. It
  374. returns 1 if the reservation was added and 0 if not. The return value should
  375. be compared with the return value of the previous call to
  376. vma_needs_reservation. An unexpected difference indicates the reservation
  377. map was modified between calls.
  378. void vma_end_reservation(struct hstate *h,
  379. struct vm_area_struct *vma, unsigned long addr)
  380. This calls region_abort() for the specified page. As in the case of region_chg
  381. and region_abort, this routine is to be called after a previous call to
  382. vma_needs_reservation. It will abort/end the in progress reservation add
  383. operation.
  384. long vma_add_reservation(struct hstate *h,
  385. struct vm_area_struct *vma, unsigned long addr)
  386. This is a special wrapper routine to help facilitate reservation cleanup
  387. on error paths. It is only called from the routine restore_reserve_on_error().
  388. This routine is used in conjunction with vma_needs_reservation in an attempt
  389. to add a reservation to the reservation map. It takes into account the
  390. different reservation map semantics for private and shared mappings. Hence,
  391. region_add is called for shared mappings (as an entry present in the map
  392. indicates a reservation), and region_del is called for private mappings (as
  393. the absence of an entry in the map indicates a reservation). See the section
  394. "Reservation cleanup in error paths" for more information on what needs to
  395. be done on error paths.
  396. Reservation Cleanup in Error Paths
  397. ----------------------------------
  398. As mentioned in the section "Reservation Map Helper Routines", reservation
  399. map modifications are performed in two steps. First vma_needs_reservation
  400. is called before a page is allocated. If the allocation is successful,
  401. then vma_commit_reservation is called. If not, vma_end_reservation is called.
  402. Global and subpool reservation counts are adjusted based on success or failure
  403. of the operation and all is well.
  404. Additionally, after a huge page is instantiated the PagePrivate flag is
  405. cleared so that accounting when the page is ultimately freed is correct.
  406. However, there are several instances where errors are encountered after a huge
  407. page is allocated but before it is instantiated. In this case, the page
  408. allocation has consumed the reservation and made the appropriate subpool,
  409. reservation map and global count adjustments. If the page is freed at this
  410. time (before instantiation and clearing of PagePrivate), then free_huge_page
  411. will increment the global reservation count. However, the reservation map
  412. indicates the reservation was consumed. This resulting inconsistent state
  413. will cause the 'leak' of a reserved huge page. The global reserve count will
  414. be higher than it should and prevent allocation of a pre-allocated page.
  415. The routine restore_reserve_on_error() attempts to handle this situation. It
  416. is fairly well documented. The intention of this routine is to restore
  417. the reservation map to the way it was before the page allocation. In this
  418. way, the state of the reservation map will correspond to the global reservation
  419. count after the page is freed.
  420. The routine restore_reserve_on_error itself may encounter errors while
  421. attempting to restore the reservation map entry. In this case, it will
  422. simply clear the PagePrivate flag of the page. In this way, the global
  423. reserve count will not be incremented when the page is freed. However, the
  424. reservation map will continue to look as though the reservation was consumed.
  425. A page can still be allocated for the address, but it will not use a reserved
  426. page as originally intended.
  427. There is some code (most notably userfaultfd) which can not call
  428. restore_reserve_on_error. In this case, it simply modifies the PagePrivate
  429. so that a reservation will not be leaked when the huge page is freed.
  430. Reservations and Memory Policy
  431. ------------------------------
  432. Per-node huge page lists existed in struct hstate when git was first used
  433. to manage Linux code. The concept of reservations was added some time later.
  434. When reservations were added, no attempt was made to take memory policy
  435. into account. While cpusets are not exactly the same as memory policy, this
  436. comment in hugetlb_acct_memory sums up the interaction between reservations
  437. and cpusets/memory policy.
  438. /*
  439. * When cpuset is configured, it breaks the strict hugetlb page
  440. * reservation as the accounting is done on a global variable. Such
  441. * reservation is completely rubbish in the presence of cpuset because
  442. * the reservation is not checked against page availability for the
  443. * current cpuset. Application can still potentially OOM'ed by kernel
  444. * with lack of free htlb page in cpuset that the task is in.
  445. * Attempt to enforce strict accounting with cpuset is almost
  446. * impossible (or too ugly) because cpuset is too fluid that
  447. * task or memory node can be dynamically moved between cpusets.
  448. *
  449. * The change of semantics for shared hugetlb mapping with cpuset is
  450. * undesirable. However, in order to preserve some of the semantics,
  451. * we fall back to check against current free page availability as
  452. * a best attempt and hopefully to minimize the impact of changing
  453. * semantics that cpuset has.
  454. */
  455. Huge page reservations were added to prevent unexpected page allocation
  456. failures (OOM) at page fault time. However, if an application makes use
  457. of cpusets or memory policy there is no guarantee that huge pages will be
  458. available on the required nodes. This is true even if there are a sufficient
  459. number of global reservations.
  460. Mike Kravetz, 7 April 2017