hugetlb.c 63 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428
  1. /*
  2. * Generic hugetlb support.
  3. * (C) William Irwin, April 2004
  4. */
  5. #include <linux/gfp.h>
  6. #include <linux/list.h>
  7. #include <linux/init.h>
  8. #include <linux/module.h>
  9. #include <linux/mm.h>
  10. #include <linux/seq_file.h>
  11. #include <linux/sysctl.h>
  12. #include <linux/highmem.h>
  13. #include <linux/mmu_notifier.h>
  14. #include <linux/nodemask.h>
  15. #include <linux/pagemap.h>
  16. #include <linux/mempolicy.h>
  17. #include <linux/cpuset.h>
  18. #include <linux/mutex.h>
  19. #include <linux/bootmem.h>
  20. #include <linux/sysfs.h>
  21. #include <asm/page.h>
  22. #include <asm/pgtable.h>
  23. #include <asm/io.h>
  24. #include <linux/hugetlb.h>
  25. #include "internal.h"
  26. const unsigned long hugetlb_zero = 0, hugetlb_infinity = ~0UL;
  27. static gfp_t htlb_alloc_mask = GFP_HIGHUSER;
  28. unsigned long hugepages_treat_as_movable;
  29. static int max_hstate;
  30. unsigned int default_hstate_idx;
  31. struct hstate hstates[HUGE_MAX_HSTATE];
  32. __initdata LIST_HEAD(huge_boot_pages);
  33. /* for command line parsing */
  34. static struct hstate * __initdata parsed_hstate;
  35. static unsigned long __initdata default_hstate_max_huge_pages;
  36. static unsigned long __initdata default_hstate_size;
  37. #define for_each_hstate(h) \
  38. for ((h) = hstates; (h) < &hstates[max_hstate]; (h)++)
  39. /*
  40. * Protects updates to hugepage_freelists, nr_huge_pages, and free_huge_pages
  41. */
  42. static DEFINE_SPINLOCK(hugetlb_lock);
  43. /*
  44. * Region tracking -- allows tracking of reservations and instantiated pages
  45. * across the pages in a mapping.
  46. *
  47. * The region data structures are protected by a combination of the mmap_sem
  48. * and the hugetlb_instantion_mutex. To access or modify a region the caller
  49. * must either hold the mmap_sem for write, or the mmap_sem for read and
  50. * the hugetlb_instantiation mutex:
  51. *
  52. * down_write(&mm->mmap_sem);
  53. * or
  54. * down_read(&mm->mmap_sem);
  55. * mutex_lock(&hugetlb_instantiation_mutex);
  56. */
  57. struct file_region {
  58. struct list_head link;
  59. long from;
  60. long to;
  61. };
  62. static long region_add(struct list_head *head, long f, long t)
  63. {
  64. struct file_region *rg, *nrg, *trg;
  65. /* Locate the region we are either in or before. */
  66. list_for_each_entry(rg, head, link)
  67. if (f <= rg->to)
  68. break;
  69. /* Round our left edge to the current segment if it encloses us. */
  70. if (f > rg->from)
  71. f = rg->from;
  72. /* Check for and consume any regions we now overlap with. */
  73. nrg = rg;
  74. list_for_each_entry_safe(rg, trg, rg->link.prev, link) {
  75. if (&rg->link == head)
  76. break;
  77. if (rg->from > t)
  78. break;
  79. /* If this area reaches higher then extend our area to
  80. * include it completely. If this is not the first area
  81. * which we intend to reuse, free it. */
  82. if (rg->to > t)
  83. t = rg->to;
  84. if (rg != nrg) {
  85. list_del(&rg->link);
  86. kfree(rg);
  87. }
  88. }
  89. nrg->from = f;
  90. nrg->to = t;
  91. return 0;
  92. }
  93. static long region_chg(struct list_head *head, long f, long t)
  94. {
  95. struct file_region *rg, *nrg;
  96. long chg = 0;
  97. /* Locate the region we are before or in. */
  98. list_for_each_entry(rg, head, link)
  99. if (f <= rg->to)
  100. break;
  101. /* If we are below the current region then a new region is required.
  102. * Subtle, allocate a new region at the position but make it zero
  103. * size such that we can guarantee to record the reservation. */
  104. if (&rg->link == head || t < rg->from) {
  105. nrg = kmalloc(sizeof(*nrg), GFP_KERNEL);
  106. if (!nrg)
  107. return -ENOMEM;
  108. nrg->from = f;
  109. nrg->to = f;
  110. INIT_LIST_HEAD(&nrg->link);
  111. list_add(&nrg->link, rg->link.prev);
  112. return t - f;
  113. }
  114. /* Round our left edge to the current segment if it encloses us. */
  115. if (f > rg->from)
  116. f = rg->from;
  117. chg = t - f;
  118. /* Check for and consume any regions we now overlap with. */
  119. list_for_each_entry(rg, rg->link.prev, link) {
  120. if (&rg->link == head)
  121. break;
  122. if (rg->from > t)
  123. return chg;
  124. /* We overlap with this area, if it extends futher than
  125. * us then we must extend ourselves. Account for its
  126. * existing reservation. */
  127. if (rg->to > t) {
  128. chg += rg->to - t;
  129. t = rg->to;
  130. }
  131. chg -= rg->to - rg->from;
  132. }
  133. return chg;
  134. }
  135. static long region_truncate(struct list_head *head, long end)
  136. {
  137. struct file_region *rg, *trg;
  138. long chg = 0;
  139. /* Locate the region we are either in or before. */
  140. list_for_each_entry(rg, head, link)
  141. if (end <= rg->to)
  142. break;
  143. if (&rg->link == head)
  144. return 0;
  145. /* If we are in the middle of a region then adjust it. */
  146. if (end > rg->from) {
  147. chg = rg->to - end;
  148. rg->to = end;
  149. rg = list_entry(rg->link.next, typeof(*rg), link);
  150. }
  151. /* Drop any remaining regions. */
  152. list_for_each_entry_safe(rg, trg, rg->link.prev, link) {
  153. if (&rg->link == head)
  154. break;
  155. chg += rg->to - rg->from;
  156. list_del(&rg->link);
  157. kfree(rg);
  158. }
  159. return chg;
  160. }
  161. static long region_count(struct list_head *head, long f, long t)
  162. {
  163. struct file_region *rg;
  164. long chg = 0;
  165. /* Locate each segment we overlap with, and count that overlap. */
  166. list_for_each_entry(rg, head, link) {
  167. int seg_from;
  168. int seg_to;
  169. if (rg->to <= f)
  170. continue;
  171. if (rg->from >= t)
  172. break;
  173. seg_from = max(rg->from, f);
  174. seg_to = min(rg->to, t);
  175. chg += seg_to - seg_from;
  176. }
  177. return chg;
  178. }
  179. /*
  180. * Convert the address within this vma to the page offset within
  181. * the mapping, in pagecache page units; huge pages here.
  182. */
  183. static pgoff_t vma_hugecache_offset(struct hstate *h,
  184. struct vm_area_struct *vma, unsigned long address)
  185. {
  186. return ((address - vma->vm_start) >> huge_page_shift(h)) +
  187. (vma->vm_pgoff >> huge_page_order(h));
  188. }
  189. /*
  190. * Return the size of the pages allocated when backing a VMA. In the majority
  191. * cases this will be same size as used by the page table entries.
  192. */
  193. unsigned long vma_kernel_pagesize(struct vm_area_struct *vma)
  194. {
  195. struct hstate *hstate;
  196. if (!is_vm_hugetlb_page(vma))
  197. return PAGE_SIZE;
  198. hstate = hstate_vma(vma);
  199. return 1UL << (hstate->order + PAGE_SHIFT);
  200. }
  201. EXPORT_SYMBOL_GPL(vma_kernel_pagesize);
  202. /*
  203. * Return the page size being used by the MMU to back a VMA. In the majority
  204. * of cases, the page size used by the kernel matches the MMU size. On
  205. * architectures where it differs, an architecture-specific version of this
  206. * function is required.
  207. */
  208. #ifndef vma_mmu_pagesize
  209. unsigned long vma_mmu_pagesize(struct vm_area_struct *vma)
  210. {
  211. return vma_kernel_pagesize(vma);
  212. }
  213. #endif
  214. /*
  215. * Flags for MAP_PRIVATE reservations. These are stored in the bottom
  216. * bits of the reservation map pointer, which are always clear due to
  217. * alignment.
  218. */
  219. #define HPAGE_RESV_OWNER (1UL << 0)
  220. #define HPAGE_RESV_UNMAPPED (1UL << 1)
  221. #define HPAGE_RESV_MASK (HPAGE_RESV_OWNER | HPAGE_RESV_UNMAPPED)
  222. /*
  223. * These helpers are used to track how many pages are reserved for
  224. * faults in a MAP_PRIVATE mapping. Only the process that called mmap()
  225. * is guaranteed to have their future faults succeed.
  226. *
  227. * With the exception of reset_vma_resv_huge_pages() which is called at fork(),
  228. * the reserve counters are updated with the hugetlb_lock held. It is safe
  229. * to reset the VMA at fork() time as it is not in use yet and there is no
  230. * chance of the global counters getting corrupted as a result of the values.
  231. *
  232. * The private mapping reservation is represented in a subtly different
  233. * manner to a shared mapping. A shared mapping has a region map associated
  234. * with the underlying file, this region map represents the backing file
  235. * pages which have ever had a reservation assigned which this persists even
  236. * after the page is instantiated. A private mapping has a region map
  237. * associated with the original mmap which is attached to all VMAs which
  238. * reference it, this region map represents those offsets which have consumed
  239. * reservation ie. where pages have been instantiated.
  240. */
  241. static unsigned long get_vma_private_data(struct vm_area_struct *vma)
  242. {
  243. return (unsigned long)vma->vm_private_data;
  244. }
  245. static void set_vma_private_data(struct vm_area_struct *vma,
  246. unsigned long value)
  247. {
  248. vma->vm_private_data = (void *)value;
  249. }
  250. struct resv_map {
  251. struct kref refs;
  252. struct list_head regions;
  253. };
  254. static struct resv_map *resv_map_alloc(void)
  255. {
  256. struct resv_map *resv_map = kmalloc(sizeof(*resv_map), GFP_KERNEL);
  257. if (!resv_map)
  258. return NULL;
  259. kref_init(&resv_map->refs);
  260. INIT_LIST_HEAD(&resv_map->regions);
  261. return resv_map;
  262. }
  263. static void resv_map_release(struct kref *ref)
  264. {
  265. struct resv_map *resv_map = container_of(ref, struct resv_map, refs);
  266. /* Clear out any active regions before we release the map. */
  267. region_truncate(&resv_map->regions, 0);
  268. kfree(resv_map);
  269. }
  270. static struct resv_map *vma_resv_map(struct vm_area_struct *vma)
  271. {
  272. VM_BUG_ON(!is_vm_hugetlb_page(vma));
  273. if (!(vma->vm_flags & VM_MAYSHARE))
  274. return (struct resv_map *)(get_vma_private_data(vma) &
  275. ~HPAGE_RESV_MASK);
  276. return NULL;
  277. }
  278. static void set_vma_resv_map(struct vm_area_struct *vma, struct resv_map *map)
  279. {
  280. VM_BUG_ON(!is_vm_hugetlb_page(vma));
  281. VM_BUG_ON(vma->vm_flags & VM_MAYSHARE);
  282. set_vma_private_data(vma, (get_vma_private_data(vma) &
  283. HPAGE_RESV_MASK) | (unsigned long)map);
  284. }
  285. static void set_vma_resv_flags(struct vm_area_struct *vma, unsigned long flags)
  286. {
  287. VM_BUG_ON(!is_vm_hugetlb_page(vma));
  288. VM_BUG_ON(vma->vm_flags & VM_MAYSHARE);
  289. set_vma_private_data(vma, get_vma_private_data(vma) | flags);
  290. }
  291. static int is_vma_resv_set(struct vm_area_struct *vma, unsigned long flag)
  292. {
  293. VM_BUG_ON(!is_vm_hugetlb_page(vma));
  294. return (get_vma_private_data(vma) & flag) != 0;
  295. }
  296. /* Decrement the reserved pages in the hugepage pool by one */
  297. static void decrement_hugepage_resv_vma(struct hstate *h,
  298. struct vm_area_struct *vma)
  299. {
  300. if (vma->vm_flags & VM_NORESERVE)
  301. return;
  302. if (vma->vm_flags & VM_MAYSHARE) {
  303. /* Shared mappings always use reserves */
  304. h->resv_huge_pages--;
  305. } else if (is_vma_resv_set(vma, HPAGE_RESV_OWNER)) {
  306. /*
  307. * Only the process that called mmap() has reserves for
  308. * private mappings.
  309. */
  310. h->resv_huge_pages--;
  311. }
  312. }
  313. /* Reset counters to 0 and clear all HPAGE_RESV_* flags */
  314. void reset_vma_resv_huge_pages(struct vm_area_struct *vma)
  315. {
  316. VM_BUG_ON(!is_vm_hugetlb_page(vma));
  317. if (!(vma->vm_flags & VM_MAYSHARE))
  318. vma->vm_private_data = (void *)0;
  319. }
  320. /* Returns true if the VMA has associated reserve pages */
  321. static int vma_has_reserves(struct vm_area_struct *vma)
  322. {
  323. if (vma->vm_flags & VM_MAYSHARE)
  324. return 1;
  325. if (is_vma_resv_set(vma, HPAGE_RESV_OWNER))
  326. return 1;
  327. return 0;
  328. }
  329. static void clear_gigantic_page(struct page *page,
  330. unsigned long addr, unsigned long sz)
  331. {
  332. int i;
  333. struct page *p = page;
  334. might_sleep();
  335. for (i = 0; i < sz/PAGE_SIZE; i++, p = mem_map_next(p, page, i)) {
  336. cond_resched();
  337. clear_user_highpage(p, addr + i * PAGE_SIZE);
  338. }
  339. }
  340. static void clear_huge_page(struct page *page,
  341. unsigned long addr, unsigned long sz)
  342. {
  343. int i;
  344. if (unlikely(sz > MAX_ORDER_NR_PAGES)) {
  345. clear_gigantic_page(page, addr, sz);
  346. return;
  347. }
  348. might_sleep();
  349. for (i = 0; i < sz/PAGE_SIZE; i++) {
  350. cond_resched();
  351. clear_user_highpage(page + i, addr + i * PAGE_SIZE);
  352. }
  353. }
  354. static void copy_gigantic_page(struct page *dst, struct page *src,
  355. unsigned long addr, struct vm_area_struct *vma)
  356. {
  357. int i;
  358. struct hstate *h = hstate_vma(vma);
  359. struct page *dst_base = dst;
  360. struct page *src_base = src;
  361. might_sleep();
  362. for (i = 0; i < pages_per_huge_page(h); ) {
  363. cond_resched();
  364. copy_user_highpage(dst, src, addr + i*PAGE_SIZE, vma);
  365. i++;
  366. dst = mem_map_next(dst, dst_base, i);
  367. src = mem_map_next(src, src_base, i);
  368. }
  369. }
  370. static void copy_huge_page(struct page *dst, struct page *src,
  371. unsigned long addr, struct vm_area_struct *vma)
  372. {
  373. int i;
  374. struct hstate *h = hstate_vma(vma);
  375. if (unlikely(pages_per_huge_page(h) > MAX_ORDER_NR_PAGES)) {
  376. copy_gigantic_page(dst, src, addr, vma);
  377. return;
  378. }
  379. might_sleep();
  380. for (i = 0; i < pages_per_huge_page(h); i++) {
  381. cond_resched();
  382. copy_user_highpage(dst + i, src + i, addr + i*PAGE_SIZE, vma);
  383. }
  384. }
  385. static void enqueue_huge_page(struct hstate *h, struct page *page)
  386. {
  387. int nid = page_to_nid(page);
  388. list_add(&page->lru, &h->hugepage_freelists[nid]);
  389. h->free_huge_pages++;
  390. h->free_huge_pages_node[nid]++;
  391. }
  392. static struct page *dequeue_huge_page_vma(struct hstate *h,
  393. struct vm_area_struct *vma,
  394. unsigned long address, int avoid_reserve)
  395. {
  396. int nid;
  397. struct page *page = NULL;
  398. struct mempolicy *mpol;
  399. nodemask_t *nodemask;
  400. struct zonelist *zonelist = huge_zonelist(vma, address,
  401. htlb_alloc_mask, &mpol, &nodemask);
  402. struct zone *zone;
  403. struct zoneref *z;
  404. /*
  405. * A child process with MAP_PRIVATE mappings created by their parent
  406. * have no page reserves. This check ensures that reservations are
  407. * not "stolen". The child may still get SIGKILLed
  408. */
  409. if (!vma_has_reserves(vma) &&
  410. h->free_huge_pages - h->resv_huge_pages == 0)
  411. return NULL;
  412. /* If reserves cannot be used, ensure enough pages are in the pool */
  413. if (avoid_reserve && h->free_huge_pages - h->resv_huge_pages == 0)
  414. return NULL;
  415. for_each_zone_zonelist_nodemask(zone, z, zonelist,
  416. MAX_NR_ZONES - 1, nodemask) {
  417. nid = zone_to_nid(zone);
  418. if (cpuset_zone_allowed_softwall(zone, htlb_alloc_mask) &&
  419. !list_empty(&h->hugepage_freelists[nid])) {
  420. page = list_entry(h->hugepage_freelists[nid].next,
  421. struct page, lru);
  422. list_del(&page->lru);
  423. h->free_huge_pages--;
  424. h->free_huge_pages_node[nid]--;
  425. if (!avoid_reserve)
  426. decrement_hugepage_resv_vma(h, vma);
  427. break;
  428. }
  429. }
  430. mpol_cond_put(mpol);
  431. return page;
  432. }
  433. static void update_and_free_page(struct hstate *h, struct page *page)
  434. {
  435. int i;
  436. VM_BUG_ON(h->order >= MAX_ORDER);
  437. h->nr_huge_pages--;
  438. h->nr_huge_pages_node[page_to_nid(page)]--;
  439. for (i = 0; i < pages_per_huge_page(h); i++) {
  440. page[i].flags &= ~(1 << PG_locked | 1 << PG_error | 1 << PG_referenced |
  441. 1 << PG_dirty | 1 << PG_active | 1 << PG_reserved |
  442. 1 << PG_private | 1<< PG_writeback);
  443. }
  444. set_compound_page_dtor(page, NULL);
  445. set_page_refcounted(page);
  446. arch_release_hugepage(page);
  447. __free_pages(page, huge_page_order(h));
  448. }
  449. struct hstate *size_to_hstate(unsigned long size)
  450. {
  451. struct hstate *h;
  452. for_each_hstate(h) {
  453. if (huge_page_size(h) == size)
  454. return h;
  455. }
  456. return NULL;
  457. }
  458. static void free_huge_page(struct page *page)
  459. {
  460. /*
  461. * Can't pass hstate in here because it is called from the
  462. * compound page destructor.
  463. */
  464. struct hstate *h = page_hstate(page);
  465. int nid = page_to_nid(page);
  466. struct address_space *mapping;
  467. mapping = (struct address_space *) page_private(page);
  468. set_page_private(page, 0);
  469. BUG_ON(page_count(page));
  470. INIT_LIST_HEAD(&page->lru);
  471. spin_lock(&hugetlb_lock);
  472. if (h->surplus_huge_pages_node[nid] && huge_page_order(h) < MAX_ORDER) {
  473. update_and_free_page(h, page);
  474. h->surplus_huge_pages--;
  475. h->surplus_huge_pages_node[nid]--;
  476. } else {
  477. enqueue_huge_page(h, page);
  478. }
  479. spin_unlock(&hugetlb_lock);
  480. if (mapping)
  481. hugetlb_put_quota(mapping, 1);
  482. }
  483. static void prep_new_huge_page(struct hstate *h, struct page *page, int nid)
  484. {
  485. set_compound_page_dtor(page, free_huge_page);
  486. spin_lock(&hugetlb_lock);
  487. h->nr_huge_pages++;
  488. h->nr_huge_pages_node[nid]++;
  489. spin_unlock(&hugetlb_lock);
  490. put_page(page); /* free it into the hugepage allocator */
  491. }
  492. static void prep_compound_gigantic_page(struct page *page, unsigned long order)
  493. {
  494. int i;
  495. int nr_pages = 1 << order;
  496. struct page *p = page + 1;
  497. /* we rely on prep_new_huge_page to set the destructor */
  498. set_compound_order(page, order);
  499. __SetPageHead(page);
  500. for (i = 1; i < nr_pages; i++, p = mem_map_next(p, page, i)) {
  501. __SetPageTail(p);
  502. p->first_page = page;
  503. }
  504. }
  505. int PageHuge(struct page *page)
  506. {
  507. compound_page_dtor *dtor;
  508. if (!PageCompound(page))
  509. return 0;
  510. page = compound_head(page);
  511. dtor = get_compound_page_dtor(page);
  512. return dtor == free_huge_page;
  513. }
  514. static struct page *alloc_fresh_huge_page_node(struct hstate *h, int nid)
  515. {
  516. struct page *page;
  517. if (h->order >= MAX_ORDER)
  518. return NULL;
  519. page = alloc_pages_exact_node(nid,
  520. htlb_alloc_mask|__GFP_COMP|__GFP_THISNODE|
  521. __GFP_REPEAT|__GFP_NOWARN,
  522. huge_page_order(h));
  523. if (page) {
  524. if (arch_prepare_hugepage(page)) {
  525. __free_pages(page, huge_page_order(h));
  526. return NULL;
  527. }
  528. prep_new_huge_page(h, page, nid);
  529. }
  530. return page;
  531. }
  532. /*
  533. * Use a helper variable to find the next node and then
  534. * copy it back to next_nid_to_alloc afterwards:
  535. * otherwise there's a window in which a racer might
  536. * pass invalid nid MAX_NUMNODES to alloc_pages_exact_node.
  537. * But we don't need to use a spin_lock here: it really
  538. * doesn't matter if occasionally a racer chooses the
  539. * same nid as we do. Move nid forward in the mask even
  540. * if we just successfully allocated a hugepage so that
  541. * the next caller gets hugepages on the next node.
  542. */
  543. static int hstate_next_node_to_alloc(struct hstate *h)
  544. {
  545. int next_nid;
  546. next_nid = next_node(h->next_nid_to_alloc, node_online_map);
  547. if (next_nid == MAX_NUMNODES)
  548. next_nid = first_node(node_online_map);
  549. h->next_nid_to_alloc = next_nid;
  550. return next_nid;
  551. }
  552. static int alloc_fresh_huge_page(struct hstate *h)
  553. {
  554. struct page *page;
  555. int start_nid;
  556. int next_nid;
  557. int ret = 0;
  558. start_nid = h->next_nid_to_alloc;
  559. next_nid = start_nid;
  560. do {
  561. page = alloc_fresh_huge_page_node(h, next_nid);
  562. if (page)
  563. ret = 1;
  564. next_nid = hstate_next_node_to_alloc(h);
  565. } while (!page && next_nid != start_nid);
  566. if (ret)
  567. count_vm_event(HTLB_BUDDY_PGALLOC);
  568. else
  569. count_vm_event(HTLB_BUDDY_PGALLOC_FAIL);
  570. return ret;
  571. }
  572. /*
  573. * helper for free_pool_huge_page() - find next node
  574. * from which to free a huge page
  575. */
  576. static int hstate_next_node_to_free(struct hstate *h)
  577. {
  578. int next_nid;
  579. next_nid = next_node(h->next_nid_to_free, node_online_map);
  580. if (next_nid == MAX_NUMNODES)
  581. next_nid = first_node(node_online_map);
  582. h->next_nid_to_free = next_nid;
  583. return next_nid;
  584. }
  585. /*
  586. * Free huge page from pool from next node to free.
  587. * Attempt to keep persistent huge pages more or less
  588. * balanced over allowed nodes.
  589. * Called with hugetlb_lock locked.
  590. */
  591. static int free_pool_huge_page(struct hstate *h, bool acct_surplus)
  592. {
  593. int start_nid;
  594. int next_nid;
  595. int ret = 0;
  596. start_nid = h->next_nid_to_free;
  597. next_nid = start_nid;
  598. do {
  599. /*
  600. * If we're returning unused surplus pages, only examine
  601. * nodes with surplus pages.
  602. */
  603. if ((!acct_surplus || h->surplus_huge_pages_node[next_nid]) &&
  604. !list_empty(&h->hugepage_freelists[next_nid])) {
  605. struct page *page =
  606. list_entry(h->hugepage_freelists[next_nid].next,
  607. struct page, lru);
  608. list_del(&page->lru);
  609. h->free_huge_pages--;
  610. h->free_huge_pages_node[next_nid]--;
  611. if (acct_surplus) {
  612. h->surplus_huge_pages--;
  613. h->surplus_huge_pages_node[next_nid]--;
  614. }
  615. update_and_free_page(h, page);
  616. ret = 1;
  617. }
  618. next_nid = hstate_next_node_to_free(h);
  619. } while (!ret && next_nid != start_nid);
  620. return ret;
  621. }
  622. static struct page *alloc_buddy_huge_page(struct hstate *h,
  623. struct vm_area_struct *vma, unsigned long address)
  624. {
  625. struct page *page;
  626. unsigned int nid;
  627. if (h->order >= MAX_ORDER)
  628. return NULL;
  629. /*
  630. * Assume we will successfully allocate the surplus page to
  631. * prevent racing processes from causing the surplus to exceed
  632. * overcommit
  633. *
  634. * This however introduces a different race, where a process B
  635. * tries to grow the static hugepage pool while alloc_pages() is
  636. * called by process A. B will only examine the per-node
  637. * counters in determining if surplus huge pages can be
  638. * converted to normal huge pages in adjust_pool_surplus(). A
  639. * won't be able to increment the per-node counter, until the
  640. * lock is dropped by B, but B doesn't drop hugetlb_lock until
  641. * no more huge pages can be converted from surplus to normal
  642. * state (and doesn't try to convert again). Thus, we have a
  643. * case where a surplus huge page exists, the pool is grown, and
  644. * the surplus huge page still exists after, even though it
  645. * should just have been converted to a normal huge page. This
  646. * does not leak memory, though, as the hugepage will be freed
  647. * once it is out of use. It also does not allow the counters to
  648. * go out of whack in adjust_pool_surplus() as we don't modify
  649. * the node values until we've gotten the hugepage and only the
  650. * per-node value is checked there.
  651. */
  652. spin_lock(&hugetlb_lock);
  653. if (h->surplus_huge_pages >= h->nr_overcommit_huge_pages) {
  654. spin_unlock(&hugetlb_lock);
  655. return NULL;
  656. } else {
  657. h->nr_huge_pages++;
  658. h->surplus_huge_pages++;
  659. }
  660. spin_unlock(&hugetlb_lock);
  661. page = alloc_pages(htlb_alloc_mask|__GFP_COMP|
  662. __GFP_REPEAT|__GFP_NOWARN,
  663. huge_page_order(h));
  664. if (page && arch_prepare_hugepage(page)) {
  665. __free_pages(page, huge_page_order(h));
  666. return NULL;
  667. }
  668. spin_lock(&hugetlb_lock);
  669. if (page) {
  670. /*
  671. * This page is now managed by the hugetlb allocator and has
  672. * no users -- drop the buddy allocator's reference.
  673. */
  674. put_page_testzero(page);
  675. VM_BUG_ON(page_count(page));
  676. nid = page_to_nid(page);
  677. set_compound_page_dtor(page, free_huge_page);
  678. /*
  679. * We incremented the global counters already
  680. */
  681. h->nr_huge_pages_node[nid]++;
  682. h->surplus_huge_pages_node[nid]++;
  683. __count_vm_event(HTLB_BUDDY_PGALLOC);
  684. } else {
  685. h->nr_huge_pages--;
  686. h->surplus_huge_pages--;
  687. __count_vm_event(HTLB_BUDDY_PGALLOC_FAIL);
  688. }
  689. spin_unlock(&hugetlb_lock);
  690. return page;
  691. }
  692. /*
  693. * Increase the hugetlb pool such that it can accomodate a reservation
  694. * of size 'delta'.
  695. */
  696. static int gather_surplus_pages(struct hstate *h, int delta)
  697. {
  698. struct list_head surplus_list;
  699. struct page *page, *tmp;
  700. int ret, i;
  701. int needed, allocated;
  702. needed = (h->resv_huge_pages + delta) - h->free_huge_pages;
  703. if (needed <= 0) {
  704. h->resv_huge_pages += delta;
  705. return 0;
  706. }
  707. allocated = 0;
  708. INIT_LIST_HEAD(&surplus_list);
  709. ret = -ENOMEM;
  710. retry:
  711. spin_unlock(&hugetlb_lock);
  712. for (i = 0; i < needed; i++) {
  713. page = alloc_buddy_huge_page(h, NULL, 0);
  714. if (!page) {
  715. /*
  716. * We were not able to allocate enough pages to
  717. * satisfy the entire reservation so we free what
  718. * we've allocated so far.
  719. */
  720. spin_lock(&hugetlb_lock);
  721. needed = 0;
  722. goto free;
  723. }
  724. list_add(&page->lru, &surplus_list);
  725. }
  726. allocated += needed;
  727. /*
  728. * After retaking hugetlb_lock, we need to recalculate 'needed'
  729. * because either resv_huge_pages or free_huge_pages may have changed.
  730. */
  731. spin_lock(&hugetlb_lock);
  732. needed = (h->resv_huge_pages + delta) -
  733. (h->free_huge_pages + allocated);
  734. if (needed > 0)
  735. goto retry;
  736. /*
  737. * The surplus_list now contains _at_least_ the number of extra pages
  738. * needed to accomodate the reservation. Add the appropriate number
  739. * of pages to the hugetlb pool and free the extras back to the buddy
  740. * allocator. Commit the entire reservation here to prevent another
  741. * process from stealing the pages as they are added to the pool but
  742. * before they are reserved.
  743. */
  744. needed += allocated;
  745. h->resv_huge_pages += delta;
  746. ret = 0;
  747. free:
  748. /* Free the needed pages to the hugetlb pool */
  749. list_for_each_entry_safe(page, tmp, &surplus_list, lru) {
  750. if ((--needed) < 0)
  751. break;
  752. list_del(&page->lru);
  753. enqueue_huge_page(h, page);
  754. }
  755. /* Free unnecessary surplus pages to the buddy allocator */
  756. if (!list_empty(&surplus_list)) {
  757. spin_unlock(&hugetlb_lock);
  758. list_for_each_entry_safe(page, tmp, &surplus_list, lru) {
  759. list_del(&page->lru);
  760. /*
  761. * The page has a reference count of zero already, so
  762. * call free_huge_page directly instead of using
  763. * put_page. This must be done with hugetlb_lock
  764. * unlocked which is safe because free_huge_page takes
  765. * hugetlb_lock before deciding how to free the page.
  766. */
  767. free_huge_page(page);
  768. }
  769. spin_lock(&hugetlb_lock);
  770. }
  771. return ret;
  772. }
  773. /*
  774. * When releasing a hugetlb pool reservation, any surplus pages that were
  775. * allocated to satisfy the reservation must be explicitly freed if they were
  776. * never used.
  777. * Called with hugetlb_lock held.
  778. */
  779. static void return_unused_surplus_pages(struct hstate *h,
  780. unsigned long unused_resv_pages)
  781. {
  782. unsigned long nr_pages;
  783. /* Uncommit the reservation */
  784. h->resv_huge_pages -= unused_resv_pages;
  785. /* Cannot return gigantic pages currently */
  786. if (h->order >= MAX_ORDER)
  787. return;
  788. nr_pages = min(unused_resv_pages, h->surplus_huge_pages);
  789. /*
  790. * We want to release as many surplus pages as possible, spread
  791. * evenly across all nodes. Iterate across all nodes until we
  792. * can no longer free unreserved surplus pages. This occurs when
  793. * the nodes with surplus pages have no free pages.
  794. * free_pool_huge_page() will balance the the frees across the
  795. * on-line nodes for us and will handle the hstate accounting.
  796. */
  797. while (nr_pages--) {
  798. if (!free_pool_huge_page(h, 1))
  799. break;
  800. }
  801. }
  802. /*
  803. * Determine if the huge page at addr within the vma has an associated
  804. * reservation. Where it does not we will need to logically increase
  805. * reservation and actually increase quota before an allocation can occur.
  806. * Where any new reservation would be required the reservation change is
  807. * prepared, but not committed. Once the page has been quota'd allocated
  808. * an instantiated the change should be committed via vma_commit_reservation.
  809. * No action is required on failure.
  810. */
  811. static long vma_needs_reservation(struct hstate *h,
  812. struct vm_area_struct *vma, unsigned long addr)
  813. {
  814. struct address_space *mapping = vma->vm_file->f_mapping;
  815. struct inode *inode = mapping->host;
  816. if (vma->vm_flags & VM_MAYSHARE) {
  817. pgoff_t idx = vma_hugecache_offset(h, vma, addr);
  818. return region_chg(&inode->i_mapping->private_list,
  819. idx, idx + 1);
  820. } else if (!is_vma_resv_set(vma, HPAGE_RESV_OWNER)) {
  821. return 1;
  822. } else {
  823. long err;
  824. pgoff_t idx = vma_hugecache_offset(h, vma, addr);
  825. struct resv_map *reservations = vma_resv_map(vma);
  826. err = region_chg(&reservations->regions, idx, idx + 1);
  827. if (err < 0)
  828. return err;
  829. return 0;
  830. }
  831. }
  832. static void vma_commit_reservation(struct hstate *h,
  833. struct vm_area_struct *vma, unsigned long addr)
  834. {
  835. struct address_space *mapping = vma->vm_file->f_mapping;
  836. struct inode *inode = mapping->host;
  837. if (vma->vm_flags & VM_MAYSHARE) {
  838. pgoff_t idx = vma_hugecache_offset(h, vma, addr);
  839. region_add(&inode->i_mapping->private_list, idx, idx + 1);
  840. } else if (is_vma_resv_set(vma, HPAGE_RESV_OWNER)) {
  841. pgoff_t idx = vma_hugecache_offset(h, vma, addr);
  842. struct resv_map *reservations = vma_resv_map(vma);
  843. /* Mark this page used in the map. */
  844. region_add(&reservations->regions, idx, idx + 1);
  845. }
  846. }
  847. static struct page *alloc_huge_page(struct vm_area_struct *vma,
  848. unsigned long addr, int avoid_reserve)
  849. {
  850. struct hstate *h = hstate_vma(vma);
  851. struct page *page;
  852. struct address_space *mapping = vma->vm_file->f_mapping;
  853. struct inode *inode = mapping->host;
  854. long chg;
  855. /*
  856. * Processes that did not create the mapping will have no reserves and
  857. * will not have accounted against quota. Check that the quota can be
  858. * made before satisfying the allocation
  859. * MAP_NORESERVE mappings may also need pages and quota allocated
  860. * if no reserve mapping overlaps.
  861. */
  862. chg = vma_needs_reservation(h, vma, addr);
  863. if (chg < 0)
  864. return ERR_PTR(chg);
  865. if (chg)
  866. if (hugetlb_get_quota(inode->i_mapping, chg))
  867. return ERR_PTR(-ENOSPC);
  868. spin_lock(&hugetlb_lock);
  869. page = dequeue_huge_page_vma(h, vma, addr, avoid_reserve);
  870. spin_unlock(&hugetlb_lock);
  871. if (!page) {
  872. page = alloc_buddy_huge_page(h, vma, addr);
  873. if (!page) {
  874. hugetlb_put_quota(inode->i_mapping, chg);
  875. return ERR_PTR(-VM_FAULT_OOM);
  876. }
  877. }
  878. set_page_refcounted(page);
  879. set_page_private(page, (unsigned long) mapping);
  880. vma_commit_reservation(h, vma, addr);
  881. return page;
  882. }
  883. int __weak alloc_bootmem_huge_page(struct hstate *h)
  884. {
  885. struct huge_bootmem_page *m;
  886. int nr_nodes = nodes_weight(node_online_map);
  887. while (nr_nodes) {
  888. void *addr;
  889. addr = __alloc_bootmem_node_nopanic(
  890. NODE_DATA(h->next_nid_to_alloc),
  891. huge_page_size(h), huge_page_size(h), 0);
  892. hstate_next_node_to_alloc(h);
  893. if (addr) {
  894. /*
  895. * Use the beginning of the huge page to store the
  896. * huge_bootmem_page struct (until gather_bootmem
  897. * puts them into the mem_map).
  898. */
  899. m = addr;
  900. goto found;
  901. }
  902. nr_nodes--;
  903. }
  904. return 0;
  905. found:
  906. BUG_ON((unsigned long)virt_to_phys(m) & (huge_page_size(h) - 1));
  907. /* Put them into a private list first because mem_map is not up yet */
  908. list_add(&m->list, &huge_boot_pages);
  909. m->hstate = h;
  910. return 1;
  911. }
  912. static void prep_compound_huge_page(struct page *page, int order)
  913. {
  914. if (unlikely(order > (MAX_ORDER - 1)))
  915. prep_compound_gigantic_page(page, order);
  916. else
  917. prep_compound_page(page, order);
  918. }
  919. /* Put bootmem huge pages into the standard lists after mem_map is up */
  920. static void __init gather_bootmem_prealloc(void)
  921. {
  922. struct huge_bootmem_page *m;
  923. list_for_each_entry(m, &huge_boot_pages, list) {
  924. struct page *page = virt_to_page(m);
  925. struct hstate *h = m->hstate;
  926. __ClearPageReserved(page);
  927. WARN_ON(page_count(page) != 1);
  928. prep_compound_huge_page(page, h->order);
  929. prep_new_huge_page(h, page, page_to_nid(page));
  930. }
  931. }
  932. static void __init hugetlb_hstate_alloc_pages(struct hstate *h)
  933. {
  934. unsigned long i;
  935. for (i = 0; i < h->max_huge_pages; ++i) {
  936. if (h->order >= MAX_ORDER) {
  937. if (!alloc_bootmem_huge_page(h))
  938. break;
  939. } else if (!alloc_fresh_huge_page(h))
  940. break;
  941. }
  942. h->max_huge_pages = i;
  943. }
  944. static void __init hugetlb_init_hstates(void)
  945. {
  946. struct hstate *h;
  947. for_each_hstate(h) {
  948. /* oversize hugepages were init'ed in early boot */
  949. if (h->order < MAX_ORDER)
  950. hugetlb_hstate_alloc_pages(h);
  951. }
  952. }
  953. static char * __init memfmt(char *buf, unsigned long n)
  954. {
  955. if (n >= (1UL << 30))
  956. sprintf(buf, "%lu GB", n >> 30);
  957. else if (n >= (1UL << 20))
  958. sprintf(buf, "%lu MB", n >> 20);
  959. else
  960. sprintf(buf, "%lu KB", n >> 10);
  961. return buf;
  962. }
  963. static void __init report_hugepages(void)
  964. {
  965. struct hstate *h;
  966. for_each_hstate(h) {
  967. char buf[32];
  968. printk(KERN_INFO "HugeTLB registered %s page size, "
  969. "pre-allocated %ld pages\n",
  970. memfmt(buf, huge_page_size(h)),
  971. h->free_huge_pages);
  972. }
  973. }
  974. #ifdef CONFIG_HIGHMEM
  975. static void try_to_free_low(struct hstate *h, unsigned long count)
  976. {
  977. int i;
  978. if (h->order >= MAX_ORDER)
  979. return;
  980. for (i = 0; i < MAX_NUMNODES; ++i) {
  981. struct page *page, *next;
  982. struct list_head *freel = &h->hugepage_freelists[i];
  983. list_for_each_entry_safe(page, next, freel, lru) {
  984. if (count >= h->nr_huge_pages)
  985. return;
  986. if (PageHighMem(page))
  987. continue;
  988. list_del(&page->lru);
  989. update_and_free_page(h, page);
  990. h->free_huge_pages--;
  991. h->free_huge_pages_node[page_to_nid(page)]--;
  992. }
  993. }
  994. }
  995. #else
  996. static inline void try_to_free_low(struct hstate *h, unsigned long count)
  997. {
  998. }
  999. #endif
  1000. /*
  1001. * Increment or decrement surplus_huge_pages. Keep node-specific counters
  1002. * balanced by operating on them in a round-robin fashion.
  1003. * Returns 1 if an adjustment was made.
  1004. */
  1005. static int adjust_pool_surplus(struct hstate *h, int delta)
  1006. {
  1007. int start_nid, next_nid;
  1008. int ret = 0;
  1009. VM_BUG_ON(delta != -1 && delta != 1);
  1010. if (delta < 0)
  1011. start_nid = h->next_nid_to_alloc;
  1012. else
  1013. start_nid = h->next_nid_to_free;
  1014. next_nid = start_nid;
  1015. do {
  1016. int nid = next_nid;
  1017. if (delta < 0) {
  1018. next_nid = hstate_next_node_to_alloc(h);
  1019. /*
  1020. * To shrink on this node, there must be a surplus page
  1021. */
  1022. if (!h->surplus_huge_pages_node[nid])
  1023. continue;
  1024. }
  1025. if (delta > 0) {
  1026. next_nid = hstate_next_node_to_free(h);
  1027. /*
  1028. * Surplus cannot exceed the total number of pages
  1029. */
  1030. if (h->surplus_huge_pages_node[nid] >=
  1031. h->nr_huge_pages_node[nid])
  1032. continue;
  1033. }
  1034. h->surplus_huge_pages += delta;
  1035. h->surplus_huge_pages_node[nid] += delta;
  1036. ret = 1;
  1037. break;
  1038. } while (next_nid != start_nid);
  1039. return ret;
  1040. }
  1041. #define persistent_huge_pages(h) (h->nr_huge_pages - h->surplus_huge_pages)
  1042. static unsigned long set_max_huge_pages(struct hstate *h, unsigned long count)
  1043. {
  1044. unsigned long min_count, ret;
  1045. if (h->order >= MAX_ORDER)
  1046. return h->max_huge_pages;
  1047. /*
  1048. * Increase the pool size
  1049. * First take pages out of surplus state. Then make up the
  1050. * remaining difference by allocating fresh huge pages.
  1051. *
  1052. * We might race with alloc_buddy_huge_page() here and be unable
  1053. * to convert a surplus huge page to a normal huge page. That is
  1054. * not critical, though, it just means the overall size of the
  1055. * pool might be one hugepage larger than it needs to be, but
  1056. * within all the constraints specified by the sysctls.
  1057. */
  1058. spin_lock(&hugetlb_lock);
  1059. while (h->surplus_huge_pages && count > persistent_huge_pages(h)) {
  1060. if (!adjust_pool_surplus(h, -1))
  1061. break;
  1062. }
  1063. while (count > persistent_huge_pages(h)) {
  1064. /*
  1065. * If this allocation races such that we no longer need the
  1066. * page, free_huge_page will handle it by freeing the page
  1067. * and reducing the surplus.
  1068. */
  1069. spin_unlock(&hugetlb_lock);
  1070. ret = alloc_fresh_huge_page(h);
  1071. spin_lock(&hugetlb_lock);
  1072. if (!ret)
  1073. goto out;
  1074. }
  1075. /*
  1076. * Decrease the pool size
  1077. * First return free pages to the buddy allocator (being careful
  1078. * to keep enough around to satisfy reservations). Then place
  1079. * pages into surplus state as needed so the pool will shrink
  1080. * to the desired size as pages become free.
  1081. *
  1082. * By placing pages into the surplus state independent of the
  1083. * overcommit value, we are allowing the surplus pool size to
  1084. * exceed overcommit. There are few sane options here. Since
  1085. * alloc_buddy_huge_page() is checking the global counter,
  1086. * though, we'll note that we're not allowed to exceed surplus
  1087. * and won't grow the pool anywhere else. Not until one of the
  1088. * sysctls are changed, or the surplus pages go out of use.
  1089. */
  1090. min_count = h->resv_huge_pages + h->nr_huge_pages - h->free_huge_pages;
  1091. min_count = max(count, min_count);
  1092. try_to_free_low(h, min_count);
  1093. while (min_count < persistent_huge_pages(h)) {
  1094. if (!free_pool_huge_page(h, 0))
  1095. break;
  1096. }
  1097. while (count < persistent_huge_pages(h)) {
  1098. if (!adjust_pool_surplus(h, 1))
  1099. break;
  1100. }
  1101. out:
  1102. ret = persistent_huge_pages(h);
  1103. spin_unlock(&hugetlb_lock);
  1104. return ret;
  1105. }
  1106. #define HSTATE_ATTR_RO(_name) \
  1107. static struct kobj_attribute _name##_attr = __ATTR_RO(_name)
  1108. #define HSTATE_ATTR(_name) \
  1109. static struct kobj_attribute _name##_attr = \
  1110. __ATTR(_name, 0644, _name##_show, _name##_store)
  1111. static struct kobject *hugepages_kobj;
  1112. static struct kobject *hstate_kobjs[HUGE_MAX_HSTATE];
  1113. static struct hstate *kobj_to_hstate(struct kobject *kobj)
  1114. {
  1115. int i;
  1116. for (i = 0; i < HUGE_MAX_HSTATE; i++)
  1117. if (hstate_kobjs[i] == kobj)
  1118. return &hstates[i];
  1119. BUG();
  1120. return NULL;
  1121. }
  1122. static ssize_t nr_hugepages_show(struct kobject *kobj,
  1123. struct kobj_attribute *attr, char *buf)
  1124. {
  1125. struct hstate *h = kobj_to_hstate(kobj);
  1126. return sprintf(buf, "%lu\n", h->nr_huge_pages);
  1127. }
  1128. static ssize_t nr_hugepages_store(struct kobject *kobj,
  1129. struct kobj_attribute *attr, const char *buf, size_t count)
  1130. {
  1131. int err;
  1132. unsigned long input;
  1133. struct hstate *h = kobj_to_hstate(kobj);
  1134. err = strict_strtoul(buf, 10, &input);
  1135. if (err)
  1136. return 0;
  1137. h->max_huge_pages = set_max_huge_pages(h, input);
  1138. return count;
  1139. }
  1140. HSTATE_ATTR(nr_hugepages);
  1141. static ssize_t nr_overcommit_hugepages_show(struct kobject *kobj,
  1142. struct kobj_attribute *attr, char *buf)
  1143. {
  1144. struct hstate *h = kobj_to_hstate(kobj);
  1145. return sprintf(buf, "%lu\n", h->nr_overcommit_huge_pages);
  1146. }
  1147. static ssize_t nr_overcommit_hugepages_store(struct kobject *kobj,
  1148. struct kobj_attribute *attr, const char *buf, size_t count)
  1149. {
  1150. int err;
  1151. unsigned long input;
  1152. struct hstate *h = kobj_to_hstate(kobj);
  1153. err = strict_strtoul(buf, 10, &input);
  1154. if (err)
  1155. return 0;
  1156. spin_lock(&hugetlb_lock);
  1157. h->nr_overcommit_huge_pages = input;
  1158. spin_unlock(&hugetlb_lock);
  1159. return count;
  1160. }
  1161. HSTATE_ATTR(nr_overcommit_hugepages);
  1162. static ssize_t free_hugepages_show(struct kobject *kobj,
  1163. struct kobj_attribute *attr, char *buf)
  1164. {
  1165. struct hstate *h = kobj_to_hstate(kobj);
  1166. return sprintf(buf, "%lu\n", h->free_huge_pages);
  1167. }
  1168. HSTATE_ATTR_RO(free_hugepages);
  1169. static ssize_t resv_hugepages_show(struct kobject *kobj,
  1170. struct kobj_attribute *attr, char *buf)
  1171. {
  1172. struct hstate *h = kobj_to_hstate(kobj);
  1173. return sprintf(buf, "%lu\n", h->resv_huge_pages);
  1174. }
  1175. HSTATE_ATTR_RO(resv_hugepages);
  1176. static ssize_t surplus_hugepages_show(struct kobject *kobj,
  1177. struct kobj_attribute *attr, char *buf)
  1178. {
  1179. struct hstate *h = kobj_to_hstate(kobj);
  1180. return sprintf(buf, "%lu\n", h->surplus_huge_pages);
  1181. }
  1182. HSTATE_ATTR_RO(surplus_hugepages);
  1183. static struct attribute *hstate_attrs[] = {
  1184. &nr_hugepages_attr.attr,
  1185. &nr_overcommit_hugepages_attr.attr,
  1186. &free_hugepages_attr.attr,
  1187. &resv_hugepages_attr.attr,
  1188. &surplus_hugepages_attr.attr,
  1189. NULL,
  1190. };
  1191. static struct attribute_group hstate_attr_group = {
  1192. .attrs = hstate_attrs,
  1193. };
  1194. static int __init hugetlb_sysfs_add_hstate(struct hstate *h)
  1195. {
  1196. int retval;
  1197. hstate_kobjs[h - hstates] = kobject_create_and_add(h->name,
  1198. hugepages_kobj);
  1199. if (!hstate_kobjs[h - hstates])
  1200. return -ENOMEM;
  1201. retval = sysfs_create_group(hstate_kobjs[h - hstates],
  1202. &hstate_attr_group);
  1203. if (retval)
  1204. kobject_put(hstate_kobjs[h - hstates]);
  1205. return retval;
  1206. }
  1207. static void __init hugetlb_sysfs_init(void)
  1208. {
  1209. struct hstate *h;
  1210. int err;
  1211. hugepages_kobj = kobject_create_and_add("hugepages", mm_kobj);
  1212. if (!hugepages_kobj)
  1213. return;
  1214. for_each_hstate(h) {
  1215. err = hugetlb_sysfs_add_hstate(h);
  1216. if (err)
  1217. printk(KERN_ERR "Hugetlb: Unable to add hstate %s",
  1218. h->name);
  1219. }
  1220. }
  1221. static void __exit hugetlb_exit(void)
  1222. {
  1223. struct hstate *h;
  1224. for_each_hstate(h) {
  1225. kobject_put(hstate_kobjs[h - hstates]);
  1226. }
  1227. kobject_put(hugepages_kobj);
  1228. }
  1229. module_exit(hugetlb_exit);
  1230. static int __init hugetlb_init(void)
  1231. {
  1232. /* Some platform decide whether they support huge pages at boot
  1233. * time. On these, such as powerpc, HPAGE_SHIFT is set to 0 when
  1234. * there is no such support
  1235. */
  1236. if (HPAGE_SHIFT == 0)
  1237. return 0;
  1238. if (!size_to_hstate(default_hstate_size)) {
  1239. default_hstate_size = HPAGE_SIZE;
  1240. if (!size_to_hstate(default_hstate_size))
  1241. hugetlb_add_hstate(HUGETLB_PAGE_ORDER);
  1242. }
  1243. default_hstate_idx = size_to_hstate(default_hstate_size) - hstates;
  1244. if (default_hstate_max_huge_pages)
  1245. default_hstate.max_huge_pages = default_hstate_max_huge_pages;
  1246. hugetlb_init_hstates();
  1247. gather_bootmem_prealloc();
  1248. report_hugepages();
  1249. hugetlb_sysfs_init();
  1250. return 0;
  1251. }
  1252. module_init(hugetlb_init);
  1253. /* Should be called on processing a hugepagesz=... option */
  1254. void __init hugetlb_add_hstate(unsigned order)
  1255. {
  1256. struct hstate *h;
  1257. unsigned long i;
  1258. if (size_to_hstate(PAGE_SIZE << order)) {
  1259. printk(KERN_WARNING "hugepagesz= specified twice, ignoring\n");
  1260. return;
  1261. }
  1262. BUG_ON(max_hstate >= HUGE_MAX_HSTATE);
  1263. BUG_ON(order == 0);
  1264. h = &hstates[max_hstate++];
  1265. h->order = order;
  1266. h->mask = ~((1ULL << (order + PAGE_SHIFT)) - 1);
  1267. h->nr_huge_pages = 0;
  1268. h->free_huge_pages = 0;
  1269. for (i = 0; i < MAX_NUMNODES; ++i)
  1270. INIT_LIST_HEAD(&h->hugepage_freelists[i]);
  1271. h->next_nid_to_alloc = first_node(node_online_map);
  1272. h->next_nid_to_free = first_node(node_online_map);
  1273. snprintf(h->name, HSTATE_NAME_LEN, "hugepages-%lukB",
  1274. huge_page_size(h)/1024);
  1275. parsed_hstate = h;
  1276. }
  1277. static int __init hugetlb_nrpages_setup(char *s)
  1278. {
  1279. unsigned long *mhp;
  1280. static unsigned long *last_mhp;
  1281. /*
  1282. * !max_hstate means we haven't parsed a hugepagesz= parameter yet,
  1283. * so this hugepages= parameter goes to the "default hstate".
  1284. */
  1285. if (!max_hstate)
  1286. mhp = &default_hstate_max_huge_pages;
  1287. else
  1288. mhp = &parsed_hstate->max_huge_pages;
  1289. if (mhp == last_mhp) {
  1290. printk(KERN_WARNING "hugepages= specified twice without "
  1291. "interleaving hugepagesz=, ignoring\n");
  1292. return 1;
  1293. }
  1294. if (sscanf(s, "%lu", mhp) <= 0)
  1295. *mhp = 0;
  1296. /*
  1297. * Global state is always initialized later in hugetlb_init.
  1298. * But we need to allocate >= MAX_ORDER hstates here early to still
  1299. * use the bootmem allocator.
  1300. */
  1301. if (max_hstate && parsed_hstate->order >= MAX_ORDER)
  1302. hugetlb_hstate_alloc_pages(parsed_hstate);
  1303. last_mhp = mhp;
  1304. return 1;
  1305. }
  1306. __setup("hugepages=", hugetlb_nrpages_setup);
  1307. static int __init hugetlb_default_setup(char *s)
  1308. {
  1309. default_hstate_size = memparse(s, &s);
  1310. return 1;
  1311. }
  1312. __setup("default_hugepagesz=", hugetlb_default_setup);
  1313. static unsigned int cpuset_mems_nr(unsigned int *array)
  1314. {
  1315. int node;
  1316. unsigned int nr = 0;
  1317. for_each_node_mask(node, cpuset_current_mems_allowed)
  1318. nr += array[node];
  1319. return nr;
  1320. }
  1321. #ifdef CONFIG_SYSCTL
  1322. int hugetlb_sysctl_handler(struct ctl_table *table, int write,
  1323. void __user *buffer,
  1324. size_t *length, loff_t *ppos)
  1325. {
  1326. struct hstate *h = &default_hstate;
  1327. unsigned long tmp;
  1328. if (!write)
  1329. tmp = h->max_huge_pages;
  1330. table->data = &tmp;
  1331. table->maxlen = sizeof(unsigned long);
  1332. proc_doulongvec_minmax(table, write, buffer, length, ppos);
  1333. if (write)
  1334. h->max_huge_pages = set_max_huge_pages(h, tmp);
  1335. return 0;
  1336. }
  1337. int hugetlb_treat_movable_handler(struct ctl_table *table, int write,
  1338. void __user *buffer,
  1339. size_t *length, loff_t *ppos)
  1340. {
  1341. proc_dointvec(table, write, buffer, length, ppos);
  1342. if (hugepages_treat_as_movable)
  1343. htlb_alloc_mask = GFP_HIGHUSER_MOVABLE;
  1344. else
  1345. htlb_alloc_mask = GFP_HIGHUSER;
  1346. return 0;
  1347. }
  1348. int hugetlb_overcommit_handler(struct ctl_table *table, int write,
  1349. void __user *buffer,
  1350. size_t *length, loff_t *ppos)
  1351. {
  1352. struct hstate *h = &default_hstate;
  1353. unsigned long tmp;
  1354. if (!write)
  1355. tmp = h->nr_overcommit_huge_pages;
  1356. table->data = &tmp;
  1357. table->maxlen = sizeof(unsigned long);
  1358. proc_doulongvec_minmax(table, write, buffer, length, ppos);
  1359. if (write) {
  1360. spin_lock(&hugetlb_lock);
  1361. h->nr_overcommit_huge_pages = tmp;
  1362. spin_unlock(&hugetlb_lock);
  1363. }
  1364. return 0;
  1365. }
  1366. #endif /* CONFIG_SYSCTL */
  1367. void hugetlb_report_meminfo(struct seq_file *m)
  1368. {
  1369. struct hstate *h = &default_hstate;
  1370. seq_printf(m,
  1371. "HugePages_Total: %5lu\n"
  1372. "HugePages_Free: %5lu\n"
  1373. "HugePages_Rsvd: %5lu\n"
  1374. "HugePages_Surp: %5lu\n"
  1375. "Hugepagesize: %8lu kB\n",
  1376. h->nr_huge_pages,
  1377. h->free_huge_pages,
  1378. h->resv_huge_pages,
  1379. h->surplus_huge_pages,
  1380. 1UL << (huge_page_order(h) + PAGE_SHIFT - 10));
  1381. }
  1382. int hugetlb_report_node_meminfo(int nid, char *buf)
  1383. {
  1384. struct hstate *h = &default_hstate;
  1385. return sprintf(buf,
  1386. "Node %d HugePages_Total: %5u\n"
  1387. "Node %d HugePages_Free: %5u\n"
  1388. "Node %d HugePages_Surp: %5u\n",
  1389. nid, h->nr_huge_pages_node[nid],
  1390. nid, h->free_huge_pages_node[nid],
  1391. nid, h->surplus_huge_pages_node[nid]);
  1392. }
  1393. /* Return the number pages of memory we physically have, in PAGE_SIZE units. */
  1394. unsigned long hugetlb_total_pages(void)
  1395. {
  1396. struct hstate *h = &default_hstate;
  1397. return h->nr_huge_pages * pages_per_huge_page(h);
  1398. }
  1399. static int hugetlb_acct_memory(struct hstate *h, long delta)
  1400. {
  1401. int ret = -ENOMEM;
  1402. spin_lock(&hugetlb_lock);
  1403. /*
  1404. * When cpuset is configured, it breaks the strict hugetlb page
  1405. * reservation as the accounting is done on a global variable. Such
  1406. * reservation is completely rubbish in the presence of cpuset because
  1407. * the reservation is not checked against page availability for the
  1408. * current cpuset. Application can still potentially OOM'ed by kernel
  1409. * with lack of free htlb page in cpuset that the task is in.
  1410. * Attempt to enforce strict accounting with cpuset is almost
  1411. * impossible (or too ugly) because cpuset is too fluid that
  1412. * task or memory node can be dynamically moved between cpusets.
  1413. *
  1414. * The change of semantics for shared hugetlb mapping with cpuset is
  1415. * undesirable. However, in order to preserve some of the semantics,
  1416. * we fall back to check against current free page availability as
  1417. * a best attempt and hopefully to minimize the impact of changing
  1418. * semantics that cpuset has.
  1419. */
  1420. if (delta > 0) {
  1421. if (gather_surplus_pages(h, delta) < 0)
  1422. goto out;
  1423. if (delta > cpuset_mems_nr(h->free_huge_pages_node)) {
  1424. return_unused_surplus_pages(h, delta);
  1425. goto out;
  1426. }
  1427. }
  1428. ret = 0;
  1429. if (delta < 0)
  1430. return_unused_surplus_pages(h, (unsigned long) -delta);
  1431. out:
  1432. spin_unlock(&hugetlb_lock);
  1433. return ret;
  1434. }
  1435. static void hugetlb_vm_op_open(struct vm_area_struct *vma)
  1436. {
  1437. struct resv_map *reservations = vma_resv_map(vma);
  1438. /*
  1439. * This new VMA should share its siblings reservation map if present.
  1440. * The VMA will only ever have a valid reservation map pointer where
  1441. * it is being copied for another still existing VMA. As that VMA
  1442. * has a reference to the reservation map it cannot dissappear until
  1443. * after this open call completes. It is therefore safe to take a
  1444. * new reference here without additional locking.
  1445. */
  1446. if (reservations)
  1447. kref_get(&reservations->refs);
  1448. }
  1449. static void hugetlb_vm_op_close(struct vm_area_struct *vma)
  1450. {
  1451. struct hstate *h = hstate_vma(vma);
  1452. struct resv_map *reservations = vma_resv_map(vma);
  1453. unsigned long reserve;
  1454. unsigned long start;
  1455. unsigned long end;
  1456. if (reservations) {
  1457. start = vma_hugecache_offset(h, vma, vma->vm_start);
  1458. end = vma_hugecache_offset(h, vma, vma->vm_end);
  1459. reserve = (end - start) -
  1460. region_count(&reservations->regions, start, end);
  1461. kref_put(&reservations->refs, resv_map_release);
  1462. if (reserve) {
  1463. hugetlb_acct_memory(h, -reserve);
  1464. hugetlb_put_quota(vma->vm_file->f_mapping, reserve);
  1465. }
  1466. }
  1467. }
  1468. /*
  1469. * We cannot handle pagefaults against hugetlb pages at all. They cause
  1470. * handle_mm_fault() to try to instantiate regular-sized pages in the
  1471. * hugegpage VMA. do_page_fault() is supposed to trap this, so BUG is we get
  1472. * this far.
  1473. */
  1474. static int hugetlb_vm_op_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
  1475. {
  1476. BUG();
  1477. return 0;
  1478. }
  1479. const struct vm_operations_struct hugetlb_vm_ops = {
  1480. .fault = hugetlb_vm_op_fault,
  1481. .open = hugetlb_vm_op_open,
  1482. .close = hugetlb_vm_op_close,
  1483. };
  1484. static pte_t make_huge_pte(struct vm_area_struct *vma, struct page *page,
  1485. int writable)
  1486. {
  1487. pte_t entry;
  1488. if (writable) {
  1489. entry =
  1490. pte_mkwrite(pte_mkdirty(mk_pte(page, vma->vm_page_prot)));
  1491. } else {
  1492. entry = huge_pte_wrprotect(mk_pte(page, vma->vm_page_prot));
  1493. }
  1494. entry = pte_mkyoung(entry);
  1495. entry = pte_mkhuge(entry);
  1496. return entry;
  1497. }
  1498. static void set_huge_ptep_writable(struct vm_area_struct *vma,
  1499. unsigned long address, pte_t *ptep)
  1500. {
  1501. pte_t entry;
  1502. entry = pte_mkwrite(pte_mkdirty(huge_ptep_get(ptep)));
  1503. if (huge_ptep_set_access_flags(vma, address, ptep, entry, 1)) {
  1504. update_mmu_cache(vma, address, entry);
  1505. }
  1506. }
  1507. int copy_hugetlb_page_range(struct mm_struct *dst, struct mm_struct *src,
  1508. struct vm_area_struct *vma)
  1509. {
  1510. pte_t *src_pte, *dst_pte, entry;
  1511. struct page *ptepage;
  1512. unsigned long addr;
  1513. int cow;
  1514. struct hstate *h = hstate_vma(vma);
  1515. unsigned long sz = huge_page_size(h);
  1516. cow = (vma->vm_flags & (VM_SHARED | VM_MAYWRITE)) == VM_MAYWRITE;
  1517. for (addr = vma->vm_start; addr < vma->vm_end; addr += sz) {
  1518. src_pte = huge_pte_offset(src, addr);
  1519. if (!src_pte)
  1520. continue;
  1521. dst_pte = huge_pte_alloc(dst, addr, sz);
  1522. if (!dst_pte)
  1523. goto nomem;
  1524. /* If the pagetables are shared don't copy or take references */
  1525. if (dst_pte == src_pte)
  1526. continue;
  1527. spin_lock(&dst->page_table_lock);
  1528. spin_lock_nested(&src->page_table_lock, SINGLE_DEPTH_NESTING);
  1529. if (!huge_pte_none(huge_ptep_get(src_pte))) {
  1530. if (cow)
  1531. huge_ptep_set_wrprotect(src, addr, src_pte);
  1532. entry = huge_ptep_get(src_pte);
  1533. ptepage = pte_page(entry);
  1534. get_page(ptepage);
  1535. set_huge_pte_at(dst, addr, dst_pte, entry);
  1536. }
  1537. spin_unlock(&src->page_table_lock);
  1538. spin_unlock(&dst->page_table_lock);
  1539. }
  1540. return 0;
  1541. nomem:
  1542. return -ENOMEM;
  1543. }
  1544. void __unmap_hugepage_range(struct vm_area_struct *vma, unsigned long start,
  1545. unsigned long end, struct page *ref_page)
  1546. {
  1547. struct mm_struct *mm = vma->vm_mm;
  1548. unsigned long address;
  1549. pte_t *ptep;
  1550. pte_t pte;
  1551. struct page *page;
  1552. struct page *tmp;
  1553. struct hstate *h = hstate_vma(vma);
  1554. unsigned long sz = huge_page_size(h);
  1555. /*
  1556. * A page gathering list, protected by per file i_mmap_lock. The
  1557. * lock is used to avoid list corruption from multiple unmapping
  1558. * of the same page since we are using page->lru.
  1559. */
  1560. LIST_HEAD(page_list);
  1561. WARN_ON(!is_vm_hugetlb_page(vma));
  1562. BUG_ON(start & ~huge_page_mask(h));
  1563. BUG_ON(end & ~huge_page_mask(h));
  1564. mmu_notifier_invalidate_range_start(mm, start, end);
  1565. spin_lock(&mm->page_table_lock);
  1566. for (address = start; address < end; address += sz) {
  1567. ptep = huge_pte_offset(mm, address);
  1568. if (!ptep)
  1569. continue;
  1570. if (huge_pmd_unshare(mm, &address, ptep))
  1571. continue;
  1572. /*
  1573. * If a reference page is supplied, it is because a specific
  1574. * page is being unmapped, not a range. Ensure the page we
  1575. * are about to unmap is the actual page of interest.
  1576. */
  1577. if (ref_page) {
  1578. pte = huge_ptep_get(ptep);
  1579. if (huge_pte_none(pte))
  1580. continue;
  1581. page = pte_page(pte);
  1582. if (page != ref_page)
  1583. continue;
  1584. /*
  1585. * Mark the VMA as having unmapped its page so that
  1586. * future faults in this VMA will fail rather than
  1587. * looking like data was lost
  1588. */
  1589. set_vma_resv_flags(vma, HPAGE_RESV_UNMAPPED);
  1590. }
  1591. pte = huge_ptep_get_and_clear(mm, address, ptep);
  1592. if (huge_pte_none(pte))
  1593. continue;
  1594. page = pte_page(pte);
  1595. if (pte_dirty(pte))
  1596. set_page_dirty(page);
  1597. list_add(&page->lru, &page_list);
  1598. }
  1599. spin_unlock(&mm->page_table_lock);
  1600. flush_tlb_range(vma, start, end);
  1601. mmu_notifier_invalidate_range_end(mm, start, end);
  1602. list_for_each_entry_safe(page, tmp, &page_list, lru) {
  1603. list_del(&page->lru);
  1604. put_page(page);
  1605. }
  1606. }
  1607. void unmap_hugepage_range(struct vm_area_struct *vma, unsigned long start,
  1608. unsigned long end, struct page *ref_page)
  1609. {
  1610. spin_lock(&vma->vm_file->f_mapping->i_mmap_lock);
  1611. __unmap_hugepage_range(vma, start, end, ref_page);
  1612. spin_unlock(&vma->vm_file->f_mapping->i_mmap_lock);
  1613. }
  1614. /*
  1615. * This is called when the original mapper is failing to COW a MAP_PRIVATE
  1616. * mappping it owns the reserve page for. The intention is to unmap the page
  1617. * from other VMAs and let the children be SIGKILLed if they are faulting the
  1618. * same region.
  1619. */
  1620. static int unmap_ref_private(struct mm_struct *mm, struct vm_area_struct *vma,
  1621. struct page *page, unsigned long address)
  1622. {
  1623. struct hstate *h = hstate_vma(vma);
  1624. struct vm_area_struct *iter_vma;
  1625. struct address_space *mapping;
  1626. struct prio_tree_iter iter;
  1627. pgoff_t pgoff;
  1628. /*
  1629. * vm_pgoff is in PAGE_SIZE units, hence the different calculation
  1630. * from page cache lookup which is in HPAGE_SIZE units.
  1631. */
  1632. address = address & huge_page_mask(h);
  1633. pgoff = ((address - vma->vm_start) >> PAGE_SHIFT)
  1634. + (vma->vm_pgoff >> PAGE_SHIFT);
  1635. mapping = (struct address_space *)page_private(page);
  1636. vma_prio_tree_foreach(iter_vma, &iter, &mapping->i_mmap, pgoff, pgoff) {
  1637. /* Do not unmap the current VMA */
  1638. if (iter_vma == vma)
  1639. continue;
  1640. /*
  1641. * Unmap the page from other VMAs without their own reserves.
  1642. * They get marked to be SIGKILLed if they fault in these
  1643. * areas. This is because a future no-page fault on this VMA
  1644. * could insert a zeroed page instead of the data existing
  1645. * from the time of fork. This would look like data corruption
  1646. */
  1647. if (!is_vma_resv_set(iter_vma, HPAGE_RESV_OWNER))
  1648. unmap_hugepage_range(iter_vma,
  1649. address, address + huge_page_size(h),
  1650. page);
  1651. }
  1652. return 1;
  1653. }
  1654. static int hugetlb_cow(struct mm_struct *mm, struct vm_area_struct *vma,
  1655. unsigned long address, pte_t *ptep, pte_t pte,
  1656. struct page *pagecache_page)
  1657. {
  1658. struct hstate *h = hstate_vma(vma);
  1659. struct page *old_page, *new_page;
  1660. int avoidcopy;
  1661. int outside_reserve = 0;
  1662. old_page = pte_page(pte);
  1663. retry_avoidcopy:
  1664. /* If no-one else is actually using this page, avoid the copy
  1665. * and just make the page writable */
  1666. avoidcopy = (page_count(old_page) == 1);
  1667. if (avoidcopy) {
  1668. set_huge_ptep_writable(vma, address, ptep);
  1669. return 0;
  1670. }
  1671. /*
  1672. * If the process that created a MAP_PRIVATE mapping is about to
  1673. * perform a COW due to a shared page count, attempt to satisfy
  1674. * the allocation without using the existing reserves. The pagecache
  1675. * page is used to determine if the reserve at this address was
  1676. * consumed or not. If reserves were used, a partial faulted mapping
  1677. * at the time of fork() could consume its reserves on COW instead
  1678. * of the full address range.
  1679. */
  1680. if (!(vma->vm_flags & VM_MAYSHARE) &&
  1681. is_vma_resv_set(vma, HPAGE_RESV_OWNER) &&
  1682. old_page != pagecache_page)
  1683. outside_reserve = 1;
  1684. page_cache_get(old_page);
  1685. new_page = alloc_huge_page(vma, address, outside_reserve);
  1686. if (IS_ERR(new_page)) {
  1687. page_cache_release(old_page);
  1688. /*
  1689. * If a process owning a MAP_PRIVATE mapping fails to COW,
  1690. * it is due to references held by a child and an insufficient
  1691. * huge page pool. To guarantee the original mappers
  1692. * reliability, unmap the page from child processes. The child
  1693. * may get SIGKILLed if it later faults.
  1694. */
  1695. if (outside_reserve) {
  1696. BUG_ON(huge_pte_none(pte));
  1697. if (unmap_ref_private(mm, vma, old_page, address)) {
  1698. BUG_ON(page_count(old_page) != 1);
  1699. BUG_ON(huge_pte_none(pte));
  1700. goto retry_avoidcopy;
  1701. }
  1702. WARN_ON_ONCE(1);
  1703. }
  1704. return -PTR_ERR(new_page);
  1705. }
  1706. spin_unlock(&mm->page_table_lock);
  1707. copy_huge_page(new_page, old_page, address, vma);
  1708. __SetPageUptodate(new_page);
  1709. spin_lock(&mm->page_table_lock);
  1710. ptep = huge_pte_offset(mm, address & huge_page_mask(h));
  1711. if (likely(pte_same(huge_ptep_get(ptep), pte))) {
  1712. /* Break COW */
  1713. huge_ptep_clear_flush(vma, address, ptep);
  1714. set_huge_pte_at(mm, address, ptep,
  1715. make_huge_pte(vma, new_page, 1));
  1716. /* Make the old page be freed below */
  1717. new_page = old_page;
  1718. }
  1719. page_cache_release(new_page);
  1720. page_cache_release(old_page);
  1721. return 0;
  1722. }
  1723. /* Return the pagecache page at a given address within a VMA */
  1724. static struct page *hugetlbfs_pagecache_page(struct hstate *h,
  1725. struct vm_area_struct *vma, unsigned long address)
  1726. {
  1727. struct address_space *mapping;
  1728. pgoff_t idx;
  1729. mapping = vma->vm_file->f_mapping;
  1730. idx = vma_hugecache_offset(h, vma, address);
  1731. return find_lock_page(mapping, idx);
  1732. }
  1733. /*
  1734. * Return whether there is a pagecache page to back given address within VMA.
  1735. * Caller follow_hugetlb_page() holds page_table_lock so we cannot lock_page.
  1736. */
  1737. static bool hugetlbfs_pagecache_present(struct hstate *h,
  1738. struct vm_area_struct *vma, unsigned long address)
  1739. {
  1740. struct address_space *mapping;
  1741. pgoff_t idx;
  1742. struct page *page;
  1743. mapping = vma->vm_file->f_mapping;
  1744. idx = vma_hugecache_offset(h, vma, address);
  1745. page = find_get_page(mapping, idx);
  1746. if (page)
  1747. put_page(page);
  1748. return page != NULL;
  1749. }
  1750. static int hugetlb_no_page(struct mm_struct *mm, struct vm_area_struct *vma,
  1751. unsigned long address, pte_t *ptep, unsigned int flags)
  1752. {
  1753. struct hstate *h = hstate_vma(vma);
  1754. int ret = VM_FAULT_SIGBUS;
  1755. pgoff_t idx;
  1756. unsigned long size;
  1757. struct page *page;
  1758. struct address_space *mapping;
  1759. pte_t new_pte;
  1760. /*
  1761. * Currently, we are forced to kill the process in the event the
  1762. * original mapper has unmapped pages from the child due to a failed
  1763. * COW. Warn that such a situation has occured as it may not be obvious
  1764. */
  1765. if (is_vma_resv_set(vma, HPAGE_RESV_UNMAPPED)) {
  1766. printk(KERN_WARNING
  1767. "PID %d killed due to inadequate hugepage pool\n",
  1768. current->pid);
  1769. return ret;
  1770. }
  1771. mapping = vma->vm_file->f_mapping;
  1772. idx = vma_hugecache_offset(h, vma, address);
  1773. /*
  1774. * Use page lock to guard against racing truncation
  1775. * before we get page_table_lock.
  1776. */
  1777. retry:
  1778. page = find_lock_page(mapping, idx);
  1779. if (!page) {
  1780. size = i_size_read(mapping->host) >> huge_page_shift(h);
  1781. if (idx >= size)
  1782. goto out;
  1783. page = alloc_huge_page(vma, address, 0);
  1784. if (IS_ERR(page)) {
  1785. ret = -PTR_ERR(page);
  1786. goto out;
  1787. }
  1788. clear_huge_page(page, address, huge_page_size(h));
  1789. __SetPageUptodate(page);
  1790. if (vma->vm_flags & VM_MAYSHARE) {
  1791. int err;
  1792. struct inode *inode = mapping->host;
  1793. err = add_to_page_cache(page, mapping, idx, GFP_KERNEL);
  1794. if (err) {
  1795. put_page(page);
  1796. if (err == -EEXIST)
  1797. goto retry;
  1798. goto out;
  1799. }
  1800. spin_lock(&inode->i_lock);
  1801. inode->i_blocks += blocks_per_huge_page(h);
  1802. spin_unlock(&inode->i_lock);
  1803. } else
  1804. lock_page(page);
  1805. }
  1806. /*
  1807. * If we are going to COW a private mapping later, we examine the
  1808. * pending reservations for this page now. This will ensure that
  1809. * any allocations necessary to record that reservation occur outside
  1810. * the spinlock.
  1811. */
  1812. if ((flags & FAULT_FLAG_WRITE) && !(vma->vm_flags & VM_SHARED))
  1813. if (vma_needs_reservation(h, vma, address) < 0) {
  1814. ret = VM_FAULT_OOM;
  1815. goto backout_unlocked;
  1816. }
  1817. spin_lock(&mm->page_table_lock);
  1818. size = i_size_read(mapping->host) >> huge_page_shift(h);
  1819. if (idx >= size)
  1820. goto backout;
  1821. ret = 0;
  1822. if (!huge_pte_none(huge_ptep_get(ptep)))
  1823. goto backout;
  1824. new_pte = make_huge_pte(vma, page, ((vma->vm_flags & VM_WRITE)
  1825. && (vma->vm_flags & VM_SHARED)));
  1826. set_huge_pte_at(mm, address, ptep, new_pte);
  1827. if ((flags & FAULT_FLAG_WRITE) && !(vma->vm_flags & VM_SHARED)) {
  1828. /* Optimization, do the COW without a second fault */
  1829. ret = hugetlb_cow(mm, vma, address, ptep, new_pte, page);
  1830. }
  1831. spin_unlock(&mm->page_table_lock);
  1832. unlock_page(page);
  1833. out:
  1834. return ret;
  1835. backout:
  1836. spin_unlock(&mm->page_table_lock);
  1837. backout_unlocked:
  1838. unlock_page(page);
  1839. put_page(page);
  1840. goto out;
  1841. }
  1842. int hugetlb_fault(struct mm_struct *mm, struct vm_area_struct *vma,
  1843. unsigned long address, unsigned int flags)
  1844. {
  1845. pte_t *ptep;
  1846. pte_t entry;
  1847. int ret;
  1848. struct page *pagecache_page = NULL;
  1849. static DEFINE_MUTEX(hugetlb_instantiation_mutex);
  1850. struct hstate *h = hstate_vma(vma);
  1851. ptep = huge_pte_alloc(mm, address, huge_page_size(h));
  1852. if (!ptep)
  1853. return VM_FAULT_OOM;
  1854. /*
  1855. * Serialize hugepage allocation and instantiation, so that we don't
  1856. * get spurious allocation failures if two CPUs race to instantiate
  1857. * the same page in the page cache.
  1858. */
  1859. mutex_lock(&hugetlb_instantiation_mutex);
  1860. entry = huge_ptep_get(ptep);
  1861. if (huge_pte_none(entry)) {
  1862. ret = hugetlb_no_page(mm, vma, address, ptep, flags);
  1863. goto out_mutex;
  1864. }
  1865. ret = 0;
  1866. /*
  1867. * If we are going to COW the mapping later, we examine the pending
  1868. * reservations for this page now. This will ensure that any
  1869. * allocations necessary to record that reservation occur outside the
  1870. * spinlock. For private mappings, we also lookup the pagecache
  1871. * page now as it is used to determine if a reservation has been
  1872. * consumed.
  1873. */
  1874. if ((flags & FAULT_FLAG_WRITE) && !pte_write(entry)) {
  1875. if (vma_needs_reservation(h, vma, address) < 0) {
  1876. ret = VM_FAULT_OOM;
  1877. goto out_mutex;
  1878. }
  1879. if (!(vma->vm_flags & VM_MAYSHARE))
  1880. pagecache_page = hugetlbfs_pagecache_page(h,
  1881. vma, address);
  1882. }
  1883. spin_lock(&mm->page_table_lock);
  1884. /* Check for a racing update before calling hugetlb_cow */
  1885. if (unlikely(!pte_same(entry, huge_ptep_get(ptep))))
  1886. goto out_page_table_lock;
  1887. if (flags & FAULT_FLAG_WRITE) {
  1888. if (!pte_write(entry)) {
  1889. ret = hugetlb_cow(mm, vma, address, ptep, entry,
  1890. pagecache_page);
  1891. goto out_page_table_lock;
  1892. }
  1893. entry = pte_mkdirty(entry);
  1894. }
  1895. entry = pte_mkyoung(entry);
  1896. if (huge_ptep_set_access_flags(vma, address, ptep, entry,
  1897. flags & FAULT_FLAG_WRITE))
  1898. update_mmu_cache(vma, address, entry);
  1899. out_page_table_lock:
  1900. spin_unlock(&mm->page_table_lock);
  1901. if (pagecache_page) {
  1902. unlock_page(pagecache_page);
  1903. put_page(pagecache_page);
  1904. }
  1905. out_mutex:
  1906. mutex_unlock(&hugetlb_instantiation_mutex);
  1907. return ret;
  1908. }
  1909. /* Can be overriden by architectures */
  1910. __attribute__((weak)) struct page *
  1911. follow_huge_pud(struct mm_struct *mm, unsigned long address,
  1912. pud_t *pud, int write)
  1913. {
  1914. BUG();
  1915. return NULL;
  1916. }
  1917. int follow_hugetlb_page(struct mm_struct *mm, struct vm_area_struct *vma,
  1918. struct page **pages, struct vm_area_struct **vmas,
  1919. unsigned long *position, int *length, int i,
  1920. unsigned int flags)
  1921. {
  1922. unsigned long pfn_offset;
  1923. unsigned long vaddr = *position;
  1924. int remainder = *length;
  1925. struct hstate *h = hstate_vma(vma);
  1926. spin_lock(&mm->page_table_lock);
  1927. while (vaddr < vma->vm_end && remainder) {
  1928. pte_t *pte;
  1929. int absent;
  1930. struct page *page;
  1931. /*
  1932. * Some archs (sparc64, sh*) have multiple pte_ts to
  1933. * each hugepage. We have to make sure we get the
  1934. * first, for the page indexing below to work.
  1935. */
  1936. pte = huge_pte_offset(mm, vaddr & huge_page_mask(h));
  1937. absent = !pte || huge_pte_none(huge_ptep_get(pte));
  1938. /*
  1939. * When coredumping, it suits get_dump_page if we just return
  1940. * an error where there's an empty slot with no huge pagecache
  1941. * to back it. This way, we avoid allocating a hugepage, and
  1942. * the sparse dumpfile avoids allocating disk blocks, but its
  1943. * huge holes still show up with zeroes where they need to be.
  1944. */
  1945. if (absent && (flags & FOLL_DUMP) &&
  1946. !hugetlbfs_pagecache_present(h, vma, vaddr)) {
  1947. remainder = 0;
  1948. break;
  1949. }
  1950. if (absent ||
  1951. ((flags & FOLL_WRITE) && !pte_write(huge_ptep_get(pte)))) {
  1952. int ret;
  1953. spin_unlock(&mm->page_table_lock);
  1954. ret = hugetlb_fault(mm, vma, vaddr,
  1955. (flags & FOLL_WRITE) ? FAULT_FLAG_WRITE : 0);
  1956. spin_lock(&mm->page_table_lock);
  1957. if (!(ret & VM_FAULT_ERROR))
  1958. continue;
  1959. remainder = 0;
  1960. break;
  1961. }
  1962. pfn_offset = (vaddr & ~huge_page_mask(h)) >> PAGE_SHIFT;
  1963. page = pte_page(huge_ptep_get(pte));
  1964. same_page:
  1965. if (pages) {
  1966. pages[i] = mem_map_offset(page, pfn_offset);
  1967. get_page(pages[i]);
  1968. }
  1969. if (vmas)
  1970. vmas[i] = vma;
  1971. vaddr += PAGE_SIZE;
  1972. ++pfn_offset;
  1973. --remainder;
  1974. ++i;
  1975. if (vaddr < vma->vm_end && remainder &&
  1976. pfn_offset < pages_per_huge_page(h)) {
  1977. /*
  1978. * We use pfn_offset to avoid touching the pageframes
  1979. * of this compound page.
  1980. */
  1981. goto same_page;
  1982. }
  1983. }
  1984. spin_unlock(&mm->page_table_lock);
  1985. *length = remainder;
  1986. *position = vaddr;
  1987. return i ? i : -EFAULT;
  1988. }
  1989. void hugetlb_change_protection(struct vm_area_struct *vma,
  1990. unsigned long address, unsigned long end, pgprot_t newprot)
  1991. {
  1992. struct mm_struct *mm = vma->vm_mm;
  1993. unsigned long start = address;
  1994. pte_t *ptep;
  1995. pte_t pte;
  1996. struct hstate *h = hstate_vma(vma);
  1997. BUG_ON(address >= end);
  1998. flush_cache_range(vma, address, end);
  1999. spin_lock(&vma->vm_file->f_mapping->i_mmap_lock);
  2000. spin_lock(&mm->page_table_lock);
  2001. for (; address < end; address += huge_page_size(h)) {
  2002. ptep = huge_pte_offset(mm, address);
  2003. if (!ptep)
  2004. continue;
  2005. if (huge_pmd_unshare(mm, &address, ptep))
  2006. continue;
  2007. if (!huge_pte_none(huge_ptep_get(ptep))) {
  2008. pte = huge_ptep_get_and_clear(mm, address, ptep);
  2009. pte = pte_mkhuge(pte_modify(pte, newprot));
  2010. set_huge_pte_at(mm, address, ptep, pte);
  2011. }
  2012. }
  2013. spin_unlock(&mm->page_table_lock);
  2014. spin_unlock(&vma->vm_file->f_mapping->i_mmap_lock);
  2015. flush_tlb_range(vma, start, end);
  2016. }
  2017. int hugetlb_reserve_pages(struct inode *inode,
  2018. long from, long to,
  2019. struct vm_area_struct *vma,
  2020. int acctflag)
  2021. {
  2022. long ret, chg;
  2023. struct hstate *h = hstate_inode(inode);
  2024. /*
  2025. * Only apply hugepage reservation if asked. At fault time, an
  2026. * attempt will be made for VM_NORESERVE to allocate a page
  2027. * and filesystem quota without using reserves
  2028. */
  2029. if (acctflag & VM_NORESERVE)
  2030. return 0;
  2031. /*
  2032. * Shared mappings base their reservation on the number of pages that
  2033. * are already allocated on behalf of the file. Private mappings need
  2034. * to reserve the full area even if read-only as mprotect() may be
  2035. * called to make the mapping read-write. Assume !vma is a shm mapping
  2036. */
  2037. if (!vma || vma->vm_flags & VM_MAYSHARE)
  2038. chg = region_chg(&inode->i_mapping->private_list, from, to);
  2039. else {
  2040. struct resv_map *resv_map = resv_map_alloc();
  2041. if (!resv_map)
  2042. return -ENOMEM;
  2043. chg = to - from;
  2044. set_vma_resv_map(vma, resv_map);
  2045. set_vma_resv_flags(vma, HPAGE_RESV_OWNER);
  2046. }
  2047. if (chg < 0)
  2048. return chg;
  2049. /* There must be enough filesystem quota for the mapping */
  2050. if (hugetlb_get_quota(inode->i_mapping, chg))
  2051. return -ENOSPC;
  2052. /*
  2053. * Check enough hugepages are available for the reservation.
  2054. * Hand back the quota if there are not
  2055. */
  2056. ret = hugetlb_acct_memory(h, chg);
  2057. if (ret < 0) {
  2058. hugetlb_put_quota(inode->i_mapping, chg);
  2059. return ret;
  2060. }
  2061. /*
  2062. * Account for the reservations made. Shared mappings record regions
  2063. * that have reservations as they are shared by multiple VMAs.
  2064. * When the last VMA disappears, the region map says how much
  2065. * the reservation was and the page cache tells how much of
  2066. * the reservation was consumed. Private mappings are per-VMA and
  2067. * only the consumed reservations are tracked. When the VMA
  2068. * disappears, the original reservation is the VMA size and the
  2069. * consumed reservations are stored in the map. Hence, nothing
  2070. * else has to be done for private mappings here
  2071. */
  2072. if (!vma || vma->vm_flags & VM_MAYSHARE)
  2073. region_add(&inode->i_mapping->private_list, from, to);
  2074. return 0;
  2075. }
  2076. void hugetlb_unreserve_pages(struct inode *inode, long offset, long freed)
  2077. {
  2078. struct hstate *h = hstate_inode(inode);
  2079. long chg = region_truncate(&inode->i_mapping->private_list, offset);
  2080. spin_lock(&inode->i_lock);
  2081. inode->i_blocks -= (blocks_per_huge_page(h) * freed);
  2082. spin_unlock(&inode->i_lock);
  2083. hugetlb_put_quota(inode->i_mapping, (chg - freed));
  2084. hugetlb_acct_memory(h, -(chg - freed));
  2085. }