hugetlb.c 59 KB

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