hugetlb.c 56 KB

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