hugetlb.c 60 KB

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