hugetlb.c 56 KB

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