hugetlb.c 56 KB

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