hugetlb.c 62 KB

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