hugetlb.c 61 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349
  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 long 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. long 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. long 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. int __weak 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. goto found;
  887. }
  888. hstate_next_node(h);
  889. nr_nodes--;
  890. }
  891. return 0;
  892. found:
  893. BUG_ON((unsigned long)virt_to_phys(m) & (huge_page_size(h) - 1));
  894. /* Put them into a private list first because mem_map is not up yet */
  895. list_add(&m->list, &huge_boot_pages);
  896. m->hstate = h;
  897. return 1;
  898. }
  899. static void prep_compound_huge_page(struct page *page, int order)
  900. {
  901. if (unlikely(order > (MAX_ORDER - 1)))
  902. prep_compound_gigantic_page(page, order);
  903. else
  904. prep_compound_page(page, order);
  905. }
  906. /* Put bootmem huge pages into the standard lists after mem_map is up */
  907. static void __init gather_bootmem_prealloc(void)
  908. {
  909. struct huge_bootmem_page *m;
  910. list_for_each_entry(m, &huge_boot_pages, list) {
  911. struct page *page = virt_to_page(m);
  912. struct hstate *h = m->hstate;
  913. __ClearPageReserved(page);
  914. WARN_ON(page_count(page) != 1);
  915. prep_compound_huge_page(page, h->order);
  916. prep_new_huge_page(h, page, page_to_nid(page));
  917. }
  918. }
  919. static void __init hugetlb_hstate_alloc_pages(struct hstate *h)
  920. {
  921. unsigned long i;
  922. for (i = 0; i < h->max_huge_pages; ++i) {
  923. if (h->order >= MAX_ORDER) {
  924. if (!alloc_bootmem_huge_page(h))
  925. break;
  926. } else if (!alloc_fresh_huge_page(h))
  927. break;
  928. }
  929. h->max_huge_pages = i;
  930. }
  931. static void __init hugetlb_init_hstates(void)
  932. {
  933. struct hstate *h;
  934. for_each_hstate(h) {
  935. /* oversize hugepages were init'ed in early boot */
  936. if (h->order < MAX_ORDER)
  937. hugetlb_hstate_alloc_pages(h);
  938. }
  939. }
  940. static char * __init memfmt(char *buf, unsigned long n)
  941. {
  942. if (n >= (1UL << 30))
  943. sprintf(buf, "%lu GB", n >> 30);
  944. else if (n >= (1UL << 20))
  945. sprintf(buf, "%lu MB", n >> 20);
  946. else
  947. sprintf(buf, "%lu KB", n >> 10);
  948. return buf;
  949. }
  950. static void __init report_hugepages(void)
  951. {
  952. struct hstate *h;
  953. for_each_hstate(h) {
  954. char buf[32];
  955. printk(KERN_INFO "HugeTLB registered %s page size, "
  956. "pre-allocated %ld pages\n",
  957. memfmt(buf, huge_page_size(h)),
  958. h->free_huge_pages);
  959. }
  960. }
  961. #ifdef CONFIG_HIGHMEM
  962. static void try_to_free_low(struct hstate *h, unsigned long count)
  963. {
  964. int i;
  965. if (h->order >= MAX_ORDER)
  966. return;
  967. for (i = 0; i < MAX_NUMNODES; ++i) {
  968. struct page *page, *next;
  969. struct list_head *freel = &h->hugepage_freelists[i];
  970. list_for_each_entry_safe(page, next, freel, lru) {
  971. if (count >= h->nr_huge_pages)
  972. return;
  973. if (PageHighMem(page))
  974. continue;
  975. list_del(&page->lru);
  976. update_and_free_page(h, page);
  977. h->free_huge_pages--;
  978. h->free_huge_pages_node[page_to_nid(page)]--;
  979. }
  980. }
  981. }
  982. #else
  983. static inline void try_to_free_low(struct hstate *h, unsigned long count)
  984. {
  985. }
  986. #endif
  987. #define persistent_huge_pages(h) (h->nr_huge_pages - h->surplus_huge_pages)
  988. static unsigned long set_max_huge_pages(struct hstate *h, unsigned long count)
  989. {
  990. unsigned long min_count, ret;
  991. if (h->order >= MAX_ORDER)
  992. return h->max_huge_pages;
  993. /*
  994. * Increase the pool size
  995. * First take pages out of surplus state. Then make up the
  996. * remaining difference by allocating fresh huge pages.
  997. *
  998. * We might race with alloc_buddy_huge_page() here and be unable
  999. * to convert a surplus huge page to a normal huge page. That is
  1000. * not critical, though, it just means the overall size of the
  1001. * pool might be one hugepage larger than it needs to be, but
  1002. * within all the constraints specified by the sysctls.
  1003. */
  1004. spin_lock(&hugetlb_lock);
  1005. while (h->surplus_huge_pages && count > persistent_huge_pages(h)) {
  1006. if (!adjust_pool_surplus(h, -1))
  1007. break;
  1008. }
  1009. while (count > persistent_huge_pages(h)) {
  1010. /*
  1011. * If this allocation races such that we no longer need the
  1012. * page, free_huge_page will handle it by freeing the page
  1013. * and reducing the surplus.
  1014. */
  1015. spin_unlock(&hugetlb_lock);
  1016. ret = alloc_fresh_huge_page(h);
  1017. spin_lock(&hugetlb_lock);
  1018. if (!ret)
  1019. goto out;
  1020. }
  1021. /*
  1022. * Decrease the pool size
  1023. * First return free pages to the buddy allocator (being careful
  1024. * to keep enough around to satisfy reservations). Then place
  1025. * pages into surplus state as needed so the pool will shrink
  1026. * to the desired size as pages become free.
  1027. *
  1028. * By placing pages into the surplus state independent of the
  1029. * overcommit value, we are allowing the surplus pool size to
  1030. * exceed overcommit. There are few sane options here. Since
  1031. * alloc_buddy_huge_page() is checking the global counter,
  1032. * though, we'll note that we're not allowed to exceed surplus
  1033. * and won't grow the pool anywhere else. Not until one of the
  1034. * sysctls are changed, or the surplus pages go out of use.
  1035. */
  1036. min_count = h->resv_huge_pages + h->nr_huge_pages - h->free_huge_pages;
  1037. min_count = max(count, min_count);
  1038. try_to_free_low(h, min_count);
  1039. while (min_count < persistent_huge_pages(h)) {
  1040. struct page *page = dequeue_huge_page(h);
  1041. if (!page)
  1042. break;
  1043. update_and_free_page(h, page);
  1044. }
  1045. while (count < persistent_huge_pages(h)) {
  1046. if (!adjust_pool_surplus(h, 1))
  1047. break;
  1048. }
  1049. out:
  1050. ret = persistent_huge_pages(h);
  1051. spin_unlock(&hugetlb_lock);
  1052. return ret;
  1053. }
  1054. #define HSTATE_ATTR_RO(_name) \
  1055. static struct kobj_attribute _name##_attr = __ATTR_RO(_name)
  1056. #define HSTATE_ATTR(_name) \
  1057. static struct kobj_attribute _name##_attr = \
  1058. __ATTR(_name, 0644, _name##_show, _name##_store)
  1059. static struct kobject *hugepages_kobj;
  1060. static struct kobject *hstate_kobjs[HUGE_MAX_HSTATE];
  1061. static struct hstate *kobj_to_hstate(struct kobject *kobj)
  1062. {
  1063. int i;
  1064. for (i = 0; i < HUGE_MAX_HSTATE; i++)
  1065. if (hstate_kobjs[i] == kobj)
  1066. return &hstates[i];
  1067. BUG();
  1068. return NULL;
  1069. }
  1070. static ssize_t nr_hugepages_show(struct kobject *kobj,
  1071. struct kobj_attribute *attr, char *buf)
  1072. {
  1073. struct hstate *h = kobj_to_hstate(kobj);
  1074. return sprintf(buf, "%lu\n", h->nr_huge_pages);
  1075. }
  1076. static ssize_t nr_hugepages_store(struct kobject *kobj,
  1077. struct kobj_attribute *attr, const char *buf, size_t count)
  1078. {
  1079. int err;
  1080. unsigned long input;
  1081. struct hstate *h = kobj_to_hstate(kobj);
  1082. err = strict_strtoul(buf, 10, &input);
  1083. if (err)
  1084. return 0;
  1085. h->max_huge_pages = set_max_huge_pages(h, input);
  1086. return count;
  1087. }
  1088. HSTATE_ATTR(nr_hugepages);
  1089. static ssize_t nr_overcommit_hugepages_show(struct kobject *kobj,
  1090. struct kobj_attribute *attr, char *buf)
  1091. {
  1092. struct hstate *h = kobj_to_hstate(kobj);
  1093. return sprintf(buf, "%lu\n", h->nr_overcommit_huge_pages);
  1094. }
  1095. static ssize_t nr_overcommit_hugepages_store(struct kobject *kobj,
  1096. struct kobj_attribute *attr, const char *buf, size_t count)
  1097. {
  1098. int err;
  1099. unsigned long input;
  1100. struct hstate *h = kobj_to_hstate(kobj);
  1101. err = strict_strtoul(buf, 10, &input);
  1102. if (err)
  1103. return 0;
  1104. spin_lock(&hugetlb_lock);
  1105. h->nr_overcommit_huge_pages = input;
  1106. spin_unlock(&hugetlb_lock);
  1107. return count;
  1108. }
  1109. HSTATE_ATTR(nr_overcommit_hugepages);
  1110. static ssize_t free_hugepages_show(struct kobject *kobj,
  1111. struct kobj_attribute *attr, char *buf)
  1112. {
  1113. struct hstate *h = kobj_to_hstate(kobj);
  1114. return sprintf(buf, "%lu\n", h->free_huge_pages);
  1115. }
  1116. HSTATE_ATTR_RO(free_hugepages);
  1117. static ssize_t resv_hugepages_show(struct kobject *kobj,
  1118. struct kobj_attribute *attr, char *buf)
  1119. {
  1120. struct hstate *h = kobj_to_hstate(kobj);
  1121. return sprintf(buf, "%lu\n", h->resv_huge_pages);
  1122. }
  1123. HSTATE_ATTR_RO(resv_hugepages);
  1124. static ssize_t surplus_hugepages_show(struct kobject *kobj,
  1125. struct kobj_attribute *attr, char *buf)
  1126. {
  1127. struct hstate *h = kobj_to_hstate(kobj);
  1128. return sprintf(buf, "%lu\n", h->surplus_huge_pages);
  1129. }
  1130. HSTATE_ATTR_RO(surplus_hugepages);
  1131. static struct attribute *hstate_attrs[] = {
  1132. &nr_hugepages_attr.attr,
  1133. &nr_overcommit_hugepages_attr.attr,
  1134. &free_hugepages_attr.attr,
  1135. &resv_hugepages_attr.attr,
  1136. &surplus_hugepages_attr.attr,
  1137. NULL,
  1138. };
  1139. static struct attribute_group hstate_attr_group = {
  1140. .attrs = hstate_attrs,
  1141. };
  1142. static int __init hugetlb_sysfs_add_hstate(struct hstate *h)
  1143. {
  1144. int retval;
  1145. hstate_kobjs[h - hstates] = kobject_create_and_add(h->name,
  1146. hugepages_kobj);
  1147. if (!hstate_kobjs[h - hstates])
  1148. return -ENOMEM;
  1149. retval = sysfs_create_group(hstate_kobjs[h - hstates],
  1150. &hstate_attr_group);
  1151. if (retval)
  1152. kobject_put(hstate_kobjs[h - hstates]);
  1153. return retval;
  1154. }
  1155. static void __init hugetlb_sysfs_init(void)
  1156. {
  1157. struct hstate *h;
  1158. int err;
  1159. hugepages_kobj = kobject_create_and_add("hugepages", mm_kobj);
  1160. if (!hugepages_kobj)
  1161. return;
  1162. for_each_hstate(h) {
  1163. err = hugetlb_sysfs_add_hstate(h);
  1164. if (err)
  1165. printk(KERN_ERR "Hugetlb: Unable to add hstate %s",
  1166. h->name);
  1167. }
  1168. }
  1169. static void __exit hugetlb_exit(void)
  1170. {
  1171. struct hstate *h;
  1172. for_each_hstate(h) {
  1173. kobject_put(hstate_kobjs[h - hstates]);
  1174. }
  1175. kobject_put(hugepages_kobj);
  1176. }
  1177. module_exit(hugetlb_exit);
  1178. static int __init hugetlb_init(void)
  1179. {
  1180. /* Some platform decide whether they support huge pages at boot
  1181. * time. On these, such as powerpc, HPAGE_SHIFT is set to 0 when
  1182. * there is no such support
  1183. */
  1184. if (HPAGE_SHIFT == 0)
  1185. return 0;
  1186. if (!size_to_hstate(default_hstate_size)) {
  1187. default_hstate_size = HPAGE_SIZE;
  1188. if (!size_to_hstate(default_hstate_size))
  1189. hugetlb_add_hstate(HUGETLB_PAGE_ORDER);
  1190. }
  1191. default_hstate_idx = size_to_hstate(default_hstate_size) - hstates;
  1192. if (default_hstate_max_huge_pages)
  1193. default_hstate.max_huge_pages = default_hstate_max_huge_pages;
  1194. hugetlb_init_hstates();
  1195. gather_bootmem_prealloc();
  1196. report_hugepages();
  1197. hugetlb_sysfs_init();
  1198. return 0;
  1199. }
  1200. module_init(hugetlb_init);
  1201. /* Should be called on processing a hugepagesz=... option */
  1202. void __init hugetlb_add_hstate(unsigned order)
  1203. {
  1204. struct hstate *h;
  1205. unsigned long i;
  1206. if (size_to_hstate(PAGE_SIZE << order)) {
  1207. printk(KERN_WARNING "hugepagesz= specified twice, ignoring\n");
  1208. return;
  1209. }
  1210. BUG_ON(max_hstate >= HUGE_MAX_HSTATE);
  1211. BUG_ON(order == 0);
  1212. h = &hstates[max_hstate++];
  1213. h->order = order;
  1214. h->mask = ~((1ULL << (order + PAGE_SHIFT)) - 1);
  1215. h->nr_huge_pages = 0;
  1216. h->free_huge_pages = 0;
  1217. for (i = 0; i < MAX_NUMNODES; ++i)
  1218. INIT_LIST_HEAD(&h->hugepage_freelists[i]);
  1219. h->hugetlb_next_nid = first_node(node_online_map);
  1220. snprintf(h->name, HSTATE_NAME_LEN, "hugepages-%lukB",
  1221. huge_page_size(h)/1024);
  1222. parsed_hstate = h;
  1223. }
  1224. static int __init hugetlb_nrpages_setup(char *s)
  1225. {
  1226. unsigned long *mhp;
  1227. static unsigned long *last_mhp;
  1228. /*
  1229. * !max_hstate means we haven't parsed a hugepagesz= parameter yet,
  1230. * so this hugepages= parameter goes to the "default hstate".
  1231. */
  1232. if (!max_hstate)
  1233. mhp = &default_hstate_max_huge_pages;
  1234. else
  1235. mhp = &parsed_hstate->max_huge_pages;
  1236. if (mhp == last_mhp) {
  1237. printk(KERN_WARNING "hugepages= specified twice without "
  1238. "interleaving hugepagesz=, ignoring\n");
  1239. return 1;
  1240. }
  1241. if (sscanf(s, "%lu", mhp) <= 0)
  1242. *mhp = 0;
  1243. /*
  1244. * Global state is always initialized later in hugetlb_init.
  1245. * But we need to allocate >= MAX_ORDER hstates here early to still
  1246. * use the bootmem allocator.
  1247. */
  1248. if (max_hstate && parsed_hstate->order >= MAX_ORDER)
  1249. hugetlb_hstate_alloc_pages(parsed_hstate);
  1250. last_mhp = mhp;
  1251. return 1;
  1252. }
  1253. __setup("hugepages=", hugetlb_nrpages_setup);
  1254. static int __init hugetlb_default_setup(char *s)
  1255. {
  1256. default_hstate_size = memparse(s, &s);
  1257. return 1;
  1258. }
  1259. __setup("default_hugepagesz=", hugetlb_default_setup);
  1260. static unsigned int cpuset_mems_nr(unsigned int *array)
  1261. {
  1262. int node;
  1263. unsigned int nr = 0;
  1264. for_each_node_mask(node, cpuset_current_mems_allowed)
  1265. nr += array[node];
  1266. return nr;
  1267. }
  1268. #ifdef CONFIG_SYSCTL
  1269. int hugetlb_sysctl_handler(struct ctl_table *table, int write,
  1270. struct file *file, void __user *buffer,
  1271. size_t *length, loff_t *ppos)
  1272. {
  1273. struct hstate *h = &default_hstate;
  1274. unsigned long tmp;
  1275. if (!write)
  1276. tmp = h->max_huge_pages;
  1277. table->data = &tmp;
  1278. table->maxlen = sizeof(unsigned long);
  1279. proc_doulongvec_minmax(table, write, file, buffer, length, ppos);
  1280. if (write)
  1281. h->max_huge_pages = set_max_huge_pages(h, tmp);
  1282. return 0;
  1283. }
  1284. int hugetlb_treat_movable_handler(struct ctl_table *table, int write,
  1285. struct file *file, void __user *buffer,
  1286. size_t *length, loff_t *ppos)
  1287. {
  1288. proc_dointvec(table, write, file, buffer, length, ppos);
  1289. if (hugepages_treat_as_movable)
  1290. htlb_alloc_mask = GFP_HIGHUSER_MOVABLE;
  1291. else
  1292. htlb_alloc_mask = GFP_HIGHUSER;
  1293. return 0;
  1294. }
  1295. int hugetlb_overcommit_handler(struct ctl_table *table, int write,
  1296. struct file *file, void __user *buffer,
  1297. size_t *length, loff_t *ppos)
  1298. {
  1299. struct hstate *h = &default_hstate;
  1300. unsigned long tmp;
  1301. if (!write)
  1302. tmp = h->nr_overcommit_huge_pages;
  1303. table->data = &tmp;
  1304. table->maxlen = sizeof(unsigned long);
  1305. proc_doulongvec_minmax(table, write, file, buffer, length, ppos);
  1306. if (write) {
  1307. spin_lock(&hugetlb_lock);
  1308. h->nr_overcommit_huge_pages = tmp;
  1309. spin_unlock(&hugetlb_lock);
  1310. }
  1311. return 0;
  1312. }
  1313. #endif /* CONFIG_SYSCTL */
  1314. void hugetlb_report_meminfo(struct seq_file *m)
  1315. {
  1316. struct hstate *h = &default_hstate;
  1317. seq_printf(m,
  1318. "HugePages_Total: %5lu\n"
  1319. "HugePages_Free: %5lu\n"
  1320. "HugePages_Rsvd: %5lu\n"
  1321. "HugePages_Surp: %5lu\n"
  1322. "Hugepagesize: %8lu kB\n",
  1323. h->nr_huge_pages,
  1324. h->free_huge_pages,
  1325. h->resv_huge_pages,
  1326. h->surplus_huge_pages,
  1327. 1UL << (huge_page_order(h) + PAGE_SHIFT - 10));
  1328. }
  1329. int hugetlb_report_node_meminfo(int nid, char *buf)
  1330. {
  1331. struct hstate *h = &default_hstate;
  1332. return sprintf(buf,
  1333. "Node %d HugePages_Total: %5u\n"
  1334. "Node %d HugePages_Free: %5u\n"
  1335. "Node %d HugePages_Surp: %5u\n",
  1336. nid, h->nr_huge_pages_node[nid],
  1337. nid, h->free_huge_pages_node[nid],
  1338. nid, h->surplus_huge_pages_node[nid]);
  1339. }
  1340. /* Return the number pages of memory we physically have, in PAGE_SIZE units. */
  1341. unsigned long hugetlb_total_pages(void)
  1342. {
  1343. struct hstate *h = &default_hstate;
  1344. return h->nr_huge_pages * pages_per_huge_page(h);
  1345. }
  1346. static int hugetlb_acct_memory(struct hstate *h, long delta)
  1347. {
  1348. int ret = -ENOMEM;
  1349. spin_lock(&hugetlb_lock);
  1350. /*
  1351. * When cpuset is configured, it breaks the strict hugetlb page
  1352. * reservation as the accounting is done on a global variable. Such
  1353. * reservation is completely rubbish in the presence of cpuset because
  1354. * the reservation is not checked against page availability for the
  1355. * current cpuset. Application can still potentially OOM'ed by kernel
  1356. * with lack of free htlb page in cpuset that the task is in.
  1357. * Attempt to enforce strict accounting with cpuset is almost
  1358. * impossible (or too ugly) because cpuset is too fluid that
  1359. * task or memory node can be dynamically moved between cpusets.
  1360. *
  1361. * The change of semantics for shared hugetlb mapping with cpuset is
  1362. * undesirable. However, in order to preserve some of the semantics,
  1363. * we fall back to check against current free page availability as
  1364. * a best attempt and hopefully to minimize the impact of changing
  1365. * semantics that cpuset has.
  1366. */
  1367. if (delta > 0) {
  1368. if (gather_surplus_pages(h, delta) < 0)
  1369. goto out;
  1370. if (delta > cpuset_mems_nr(h->free_huge_pages_node)) {
  1371. return_unused_surplus_pages(h, delta);
  1372. goto out;
  1373. }
  1374. }
  1375. ret = 0;
  1376. if (delta < 0)
  1377. return_unused_surplus_pages(h, (unsigned long) -delta);
  1378. out:
  1379. spin_unlock(&hugetlb_lock);
  1380. return ret;
  1381. }
  1382. static void hugetlb_vm_op_open(struct vm_area_struct *vma)
  1383. {
  1384. struct resv_map *reservations = vma_resv_map(vma);
  1385. /*
  1386. * This new VMA should share its siblings reservation map if present.
  1387. * The VMA will only ever have a valid reservation map pointer where
  1388. * it is being copied for another still existing VMA. As that VMA
  1389. * has a reference to the reservation map it cannot dissappear until
  1390. * after this open call completes. It is therefore safe to take a
  1391. * new reference here without additional locking.
  1392. */
  1393. if (reservations)
  1394. kref_get(&reservations->refs);
  1395. }
  1396. static void hugetlb_vm_op_close(struct vm_area_struct *vma)
  1397. {
  1398. struct hstate *h = hstate_vma(vma);
  1399. struct resv_map *reservations = vma_resv_map(vma);
  1400. unsigned long reserve;
  1401. unsigned long start;
  1402. unsigned long end;
  1403. if (reservations) {
  1404. start = vma_hugecache_offset(h, vma, vma->vm_start);
  1405. end = vma_hugecache_offset(h, vma, vma->vm_end);
  1406. reserve = (end - start) -
  1407. region_count(&reservations->regions, start, end);
  1408. kref_put(&reservations->refs, resv_map_release);
  1409. if (reserve) {
  1410. hugetlb_acct_memory(h, -reserve);
  1411. hugetlb_put_quota(vma->vm_file->f_mapping, reserve);
  1412. }
  1413. }
  1414. }
  1415. /*
  1416. * We cannot handle pagefaults against hugetlb pages at all. They cause
  1417. * handle_mm_fault() to try to instantiate regular-sized pages in the
  1418. * hugegpage VMA. do_page_fault() is supposed to trap this, so BUG is we get
  1419. * this far.
  1420. */
  1421. static int hugetlb_vm_op_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
  1422. {
  1423. BUG();
  1424. return 0;
  1425. }
  1426. struct vm_operations_struct hugetlb_vm_ops = {
  1427. .fault = hugetlb_vm_op_fault,
  1428. .open = hugetlb_vm_op_open,
  1429. .close = hugetlb_vm_op_close,
  1430. };
  1431. static pte_t make_huge_pte(struct vm_area_struct *vma, struct page *page,
  1432. int writable)
  1433. {
  1434. pte_t entry;
  1435. if (writable) {
  1436. entry =
  1437. pte_mkwrite(pte_mkdirty(mk_pte(page, vma->vm_page_prot)));
  1438. } else {
  1439. entry = huge_pte_wrprotect(mk_pte(page, vma->vm_page_prot));
  1440. }
  1441. entry = pte_mkyoung(entry);
  1442. entry = pte_mkhuge(entry);
  1443. return entry;
  1444. }
  1445. static void set_huge_ptep_writable(struct vm_area_struct *vma,
  1446. unsigned long address, pte_t *ptep)
  1447. {
  1448. pte_t entry;
  1449. entry = pte_mkwrite(pte_mkdirty(huge_ptep_get(ptep)));
  1450. if (huge_ptep_set_access_flags(vma, address, ptep, entry, 1)) {
  1451. update_mmu_cache(vma, address, entry);
  1452. }
  1453. }
  1454. int copy_hugetlb_page_range(struct mm_struct *dst, struct mm_struct *src,
  1455. struct vm_area_struct *vma)
  1456. {
  1457. pte_t *src_pte, *dst_pte, entry;
  1458. struct page *ptepage;
  1459. unsigned long addr;
  1460. int cow;
  1461. struct hstate *h = hstate_vma(vma);
  1462. unsigned long sz = huge_page_size(h);
  1463. cow = (vma->vm_flags & (VM_SHARED | VM_MAYWRITE)) == VM_MAYWRITE;
  1464. for (addr = vma->vm_start; addr < vma->vm_end; addr += sz) {
  1465. src_pte = huge_pte_offset(src, addr);
  1466. if (!src_pte)
  1467. continue;
  1468. dst_pte = huge_pte_alloc(dst, addr, sz);
  1469. if (!dst_pte)
  1470. goto nomem;
  1471. /* If the pagetables are shared don't copy or take references */
  1472. if (dst_pte == src_pte)
  1473. continue;
  1474. spin_lock(&dst->page_table_lock);
  1475. spin_lock_nested(&src->page_table_lock, SINGLE_DEPTH_NESTING);
  1476. if (!huge_pte_none(huge_ptep_get(src_pte))) {
  1477. if (cow)
  1478. huge_ptep_set_wrprotect(src, addr, src_pte);
  1479. entry = huge_ptep_get(src_pte);
  1480. ptepage = pte_page(entry);
  1481. get_page(ptepage);
  1482. set_huge_pte_at(dst, addr, dst_pte, entry);
  1483. }
  1484. spin_unlock(&src->page_table_lock);
  1485. spin_unlock(&dst->page_table_lock);
  1486. }
  1487. return 0;
  1488. nomem:
  1489. return -ENOMEM;
  1490. }
  1491. void __unmap_hugepage_range(struct vm_area_struct *vma, unsigned long start,
  1492. unsigned long end, struct page *ref_page)
  1493. {
  1494. struct mm_struct *mm = vma->vm_mm;
  1495. unsigned long address;
  1496. pte_t *ptep;
  1497. pte_t pte;
  1498. struct page *page;
  1499. struct page *tmp;
  1500. struct hstate *h = hstate_vma(vma);
  1501. unsigned long sz = huge_page_size(h);
  1502. /*
  1503. * A page gathering list, protected by per file i_mmap_lock. The
  1504. * lock is used to avoid list corruption from multiple unmapping
  1505. * of the same page since we are using page->lru.
  1506. */
  1507. LIST_HEAD(page_list);
  1508. WARN_ON(!is_vm_hugetlb_page(vma));
  1509. BUG_ON(start & ~huge_page_mask(h));
  1510. BUG_ON(end & ~huge_page_mask(h));
  1511. mmu_notifier_invalidate_range_start(mm, start, end);
  1512. spin_lock(&mm->page_table_lock);
  1513. for (address = start; address < end; address += sz) {
  1514. ptep = huge_pte_offset(mm, address);
  1515. if (!ptep)
  1516. continue;
  1517. if (huge_pmd_unshare(mm, &address, ptep))
  1518. continue;
  1519. /*
  1520. * If a reference page is supplied, it is because a specific
  1521. * page is being unmapped, not a range. Ensure the page we
  1522. * are about to unmap is the actual page of interest.
  1523. */
  1524. if (ref_page) {
  1525. pte = huge_ptep_get(ptep);
  1526. if (huge_pte_none(pte))
  1527. continue;
  1528. page = pte_page(pte);
  1529. if (page != ref_page)
  1530. continue;
  1531. /*
  1532. * Mark the VMA as having unmapped its page so that
  1533. * future faults in this VMA will fail rather than
  1534. * looking like data was lost
  1535. */
  1536. set_vma_resv_flags(vma, HPAGE_RESV_UNMAPPED);
  1537. }
  1538. pte = huge_ptep_get_and_clear(mm, address, ptep);
  1539. if (huge_pte_none(pte))
  1540. continue;
  1541. page = pte_page(pte);
  1542. if (pte_dirty(pte))
  1543. set_page_dirty(page);
  1544. list_add(&page->lru, &page_list);
  1545. }
  1546. spin_unlock(&mm->page_table_lock);
  1547. flush_tlb_range(vma, start, end);
  1548. mmu_notifier_invalidate_range_end(mm, start, end);
  1549. list_for_each_entry_safe(page, tmp, &page_list, lru) {
  1550. list_del(&page->lru);
  1551. put_page(page);
  1552. }
  1553. }
  1554. void unmap_hugepage_range(struct vm_area_struct *vma, unsigned long start,
  1555. unsigned long end, struct page *ref_page)
  1556. {
  1557. spin_lock(&vma->vm_file->f_mapping->i_mmap_lock);
  1558. __unmap_hugepage_range(vma, start, end, ref_page);
  1559. spin_unlock(&vma->vm_file->f_mapping->i_mmap_lock);
  1560. }
  1561. /*
  1562. * This is called when the original mapper is failing to COW a MAP_PRIVATE
  1563. * mappping it owns the reserve page for. The intention is to unmap the page
  1564. * from other VMAs and let the children be SIGKILLed if they are faulting the
  1565. * same region.
  1566. */
  1567. static int unmap_ref_private(struct mm_struct *mm, struct vm_area_struct *vma,
  1568. struct page *page, unsigned long address)
  1569. {
  1570. struct hstate *h = hstate_vma(vma);
  1571. struct vm_area_struct *iter_vma;
  1572. struct address_space *mapping;
  1573. struct prio_tree_iter iter;
  1574. pgoff_t pgoff;
  1575. /*
  1576. * vm_pgoff is in PAGE_SIZE units, hence the different calculation
  1577. * from page cache lookup which is in HPAGE_SIZE units.
  1578. */
  1579. address = address & huge_page_mask(h);
  1580. pgoff = ((address - vma->vm_start) >> PAGE_SHIFT)
  1581. + (vma->vm_pgoff >> PAGE_SHIFT);
  1582. mapping = (struct address_space *)page_private(page);
  1583. vma_prio_tree_foreach(iter_vma, &iter, &mapping->i_mmap, pgoff, pgoff) {
  1584. /* Do not unmap the current VMA */
  1585. if (iter_vma == vma)
  1586. continue;
  1587. /*
  1588. * Unmap the page from other VMAs without their own reserves.
  1589. * They get marked to be SIGKILLed if they fault in these
  1590. * areas. This is because a future no-page fault on this VMA
  1591. * could insert a zeroed page instead of the data existing
  1592. * from the time of fork. This would look like data corruption
  1593. */
  1594. if (!is_vma_resv_set(iter_vma, HPAGE_RESV_OWNER))
  1595. unmap_hugepage_range(iter_vma,
  1596. address, address + huge_page_size(h),
  1597. page);
  1598. }
  1599. return 1;
  1600. }
  1601. static int hugetlb_cow(struct mm_struct *mm, struct vm_area_struct *vma,
  1602. unsigned long address, pte_t *ptep, pte_t pte,
  1603. struct page *pagecache_page)
  1604. {
  1605. struct hstate *h = hstate_vma(vma);
  1606. struct page *old_page, *new_page;
  1607. int avoidcopy;
  1608. int outside_reserve = 0;
  1609. old_page = pte_page(pte);
  1610. retry_avoidcopy:
  1611. /* If no-one else is actually using this page, avoid the copy
  1612. * and just make the page writable */
  1613. avoidcopy = (page_count(old_page) == 1);
  1614. if (avoidcopy) {
  1615. set_huge_ptep_writable(vma, address, ptep);
  1616. return 0;
  1617. }
  1618. /*
  1619. * If the process that created a MAP_PRIVATE mapping is about to
  1620. * perform a COW due to a shared page count, attempt to satisfy
  1621. * the allocation without using the existing reserves. The pagecache
  1622. * page is used to determine if the reserve at this address was
  1623. * consumed or not. If reserves were used, a partial faulted mapping
  1624. * at the time of fork() could consume its reserves on COW instead
  1625. * of the full address range.
  1626. */
  1627. if (!(vma->vm_flags & VM_SHARED) &&
  1628. is_vma_resv_set(vma, HPAGE_RESV_OWNER) &&
  1629. old_page != pagecache_page)
  1630. outside_reserve = 1;
  1631. page_cache_get(old_page);
  1632. new_page = alloc_huge_page(vma, address, outside_reserve);
  1633. if (IS_ERR(new_page)) {
  1634. page_cache_release(old_page);
  1635. /*
  1636. * If a process owning a MAP_PRIVATE mapping fails to COW,
  1637. * it is due to references held by a child and an insufficient
  1638. * huge page pool. To guarantee the original mappers
  1639. * reliability, unmap the page from child processes. The child
  1640. * may get SIGKILLed if it later faults.
  1641. */
  1642. if (outside_reserve) {
  1643. BUG_ON(huge_pte_none(pte));
  1644. if (unmap_ref_private(mm, vma, old_page, address)) {
  1645. BUG_ON(page_count(old_page) != 1);
  1646. BUG_ON(huge_pte_none(pte));
  1647. goto retry_avoidcopy;
  1648. }
  1649. WARN_ON_ONCE(1);
  1650. }
  1651. return -PTR_ERR(new_page);
  1652. }
  1653. spin_unlock(&mm->page_table_lock);
  1654. copy_huge_page(new_page, old_page, address, vma);
  1655. __SetPageUptodate(new_page);
  1656. spin_lock(&mm->page_table_lock);
  1657. ptep = huge_pte_offset(mm, address & huge_page_mask(h));
  1658. if (likely(pte_same(huge_ptep_get(ptep), pte))) {
  1659. /* Break COW */
  1660. huge_ptep_clear_flush(vma, address, ptep);
  1661. set_huge_pte_at(mm, address, ptep,
  1662. make_huge_pte(vma, new_page, 1));
  1663. /* Make the old page be freed below */
  1664. new_page = old_page;
  1665. }
  1666. page_cache_release(new_page);
  1667. page_cache_release(old_page);
  1668. return 0;
  1669. }
  1670. /* Return the pagecache page at a given address within a VMA */
  1671. static struct page *hugetlbfs_pagecache_page(struct hstate *h,
  1672. struct vm_area_struct *vma, unsigned long address)
  1673. {
  1674. struct address_space *mapping;
  1675. pgoff_t idx;
  1676. mapping = vma->vm_file->f_mapping;
  1677. idx = vma_hugecache_offset(h, vma, address);
  1678. return find_lock_page(mapping, idx);
  1679. }
  1680. static int hugetlb_no_page(struct mm_struct *mm, struct vm_area_struct *vma,
  1681. unsigned long address, pte_t *ptep, int write_access)
  1682. {
  1683. struct hstate *h = hstate_vma(vma);
  1684. int ret = VM_FAULT_SIGBUS;
  1685. pgoff_t idx;
  1686. unsigned long size;
  1687. struct page *page;
  1688. struct address_space *mapping;
  1689. pte_t new_pte;
  1690. /*
  1691. * Currently, we are forced to kill the process in the event the
  1692. * original mapper has unmapped pages from the child due to a failed
  1693. * COW. Warn that such a situation has occured as it may not be obvious
  1694. */
  1695. if (is_vma_resv_set(vma, HPAGE_RESV_UNMAPPED)) {
  1696. printk(KERN_WARNING
  1697. "PID %d killed due to inadequate hugepage pool\n",
  1698. current->pid);
  1699. return ret;
  1700. }
  1701. mapping = vma->vm_file->f_mapping;
  1702. idx = vma_hugecache_offset(h, vma, address);
  1703. /*
  1704. * Use page lock to guard against racing truncation
  1705. * before we get page_table_lock.
  1706. */
  1707. retry:
  1708. page = find_lock_page(mapping, idx);
  1709. if (!page) {
  1710. size = i_size_read(mapping->host) >> huge_page_shift(h);
  1711. if (idx >= size)
  1712. goto out;
  1713. page = alloc_huge_page(vma, address, 0);
  1714. if (IS_ERR(page)) {
  1715. ret = -PTR_ERR(page);
  1716. goto out;
  1717. }
  1718. clear_huge_page(page, address, huge_page_size(h));
  1719. __SetPageUptodate(page);
  1720. if (vma->vm_flags & VM_SHARED) {
  1721. int err;
  1722. struct inode *inode = mapping->host;
  1723. err = add_to_page_cache(page, mapping, idx, GFP_KERNEL);
  1724. if (err) {
  1725. put_page(page);
  1726. if (err == -EEXIST)
  1727. goto retry;
  1728. goto out;
  1729. }
  1730. spin_lock(&inode->i_lock);
  1731. inode->i_blocks += blocks_per_huge_page(h);
  1732. spin_unlock(&inode->i_lock);
  1733. } else
  1734. lock_page(page);
  1735. }
  1736. /*
  1737. * If we are going to COW a private mapping later, we examine the
  1738. * pending reservations for this page now. This will ensure that
  1739. * any allocations necessary to record that reservation occur outside
  1740. * the spinlock.
  1741. */
  1742. if (write_access && !(vma->vm_flags & VM_SHARED))
  1743. if (vma_needs_reservation(h, vma, address) < 0) {
  1744. ret = VM_FAULT_OOM;
  1745. goto backout_unlocked;
  1746. }
  1747. spin_lock(&mm->page_table_lock);
  1748. size = i_size_read(mapping->host) >> huge_page_shift(h);
  1749. if (idx >= size)
  1750. goto backout;
  1751. ret = 0;
  1752. if (!huge_pte_none(huge_ptep_get(ptep)))
  1753. goto backout;
  1754. new_pte = make_huge_pte(vma, page, ((vma->vm_flags & VM_WRITE)
  1755. && (vma->vm_flags & VM_SHARED)));
  1756. set_huge_pte_at(mm, address, ptep, new_pte);
  1757. if (write_access && !(vma->vm_flags & VM_SHARED)) {
  1758. /* Optimization, do the COW without a second fault */
  1759. ret = hugetlb_cow(mm, vma, address, ptep, new_pte, page);
  1760. }
  1761. spin_unlock(&mm->page_table_lock);
  1762. unlock_page(page);
  1763. out:
  1764. return ret;
  1765. backout:
  1766. spin_unlock(&mm->page_table_lock);
  1767. backout_unlocked:
  1768. unlock_page(page);
  1769. put_page(page);
  1770. goto out;
  1771. }
  1772. int hugetlb_fault(struct mm_struct *mm, struct vm_area_struct *vma,
  1773. unsigned long address, int write_access)
  1774. {
  1775. pte_t *ptep;
  1776. pte_t entry;
  1777. int ret;
  1778. struct page *pagecache_page = NULL;
  1779. static DEFINE_MUTEX(hugetlb_instantiation_mutex);
  1780. struct hstate *h = hstate_vma(vma);
  1781. ptep = huge_pte_alloc(mm, address, huge_page_size(h));
  1782. if (!ptep)
  1783. return VM_FAULT_OOM;
  1784. /*
  1785. * Serialize hugepage allocation and instantiation, so that we don't
  1786. * get spurious allocation failures if two CPUs race to instantiate
  1787. * the same page in the page cache.
  1788. */
  1789. mutex_lock(&hugetlb_instantiation_mutex);
  1790. entry = huge_ptep_get(ptep);
  1791. if (huge_pte_none(entry)) {
  1792. ret = hugetlb_no_page(mm, vma, address, ptep, write_access);
  1793. goto out_mutex;
  1794. }
  1795. ret = 0;
  1796. /*
  1797. * If we are going to COW the mapping later, we examine the pending
  1798. * reservations for this page now. This will ensure that any
  1799. * allocations necessary to record that reservation occur outside the
  1800. * spinlock. For private mappings, we also lookup the pagecache
  1801. * page now as it is used to determine if a reservation has been
  1802. * consumed.
  1803. */
  1804. if (write_access && !pte_write(entry)) {
  1805. if (vma_needs_reservation(h, vma, address) < 0) {
  1806. ret = VM_FAULT_OOM;
  1807. goto out_mutex;
  1808. }
  1809. if (!(vma->vm_flags & VM_SHARED))
  1810. pagecache_page = hugetlbfs_pagecache_page(h,
  1811. vma, address);
  1812. }
  1813. spin_lock(&mm->page_table_lock);
  1814. /* Check for a racing update before calling hugetlb_cow */
  1815. if (unlikely(!pte_same(entry, huge_ptep_get(ptep))))
  1816. goto out_page_table_lock;
  1817. if (write_access) {
  1818. if (!pte_write(entry)) {
  1819. ret = hugetlb_cow(mm, vma, address, ptep, entry,
  1820. pagecache_page);
  1821. goto out_page_table_lock;
  1822. }
  1823. entry = pte_mkdirty(entry);
  1824. }
  1825. entry = pte_mkyoung(entry);
  1826. if (huge_ptep_set_access_flags(vma, address, ptep, entry, write_access))
  1827. update_mmu_cache(vma, address, entry);
  1828. out_page_table_lock:
  1829. spin_unlock(&mm->page_table_lock);
  1830. if (pagecache_page) {
  1831. unlock_page(pagecache_page);
  1832. put_page(pagecache_page);
  1833. }
  1834. out_mutex:
  1835. mutex_unlock(&hugetlb_instantiation_mutex);
  1836. return ret;
  1837. }
  1838. /* Can be overriden by architectures */
  1839. __attribute__((weak)) struct page *
  1840. follow_huge_pud(struct mm_struct *mm, unsigned long address,
  1841. pud_t *pud, int write)
  1842. {
  1843. BUG();
  1844. return NULL;
  1845. }
  1846. static int huge_zeropage_ok(pte_t *ptep, int write, int shared)
  1847. {
  1848. if (!ptep || write || shared)
  1849. return 0;
  1850. else
  1851. return huge_pte_none(huge_ptep_get(ptep));
  1852. }
  1853. int follow_hugetlb_page(struct mm_struct *mm, struct vm_area_struct *vma,
  1854. struct page **pages, struct vm_area_struct **vmas,
  1855. unsigned long *position, int *length, int i,
  1856. int write)
  1857. {
  1858. unsigned long pfn_offset;
  1859. unsigned long vaddr = *position;
  1860. int remainder = *length;
  1861. struct hstate *h = hstate_vma(vma);
  1862. int zeropage_ok = 0;
  1863. int shared = vma->vm_flags & VM_SHARED;
  1864. spin_lock(&mm->page_table_lock);
  1865. while (vaddr < vma->vm_end && remainder) {
  1866. pte_t *pte;
  1867. struct page *page;
  1868. /*
  1869. * Some archs (sparc64, sh*) have multiple pte_ts to
  1870. * each hugepage. We have to make * sure we get the
  1871. * first, for the page indexing below to work.
  1872. */
  1873. pte = huge_pte_offset(mm, vaddr & huge_page_mask(h));
  1874. if (huge_zeropage_ok(pte, write, shared))
  1875. zeropage_ok = 1;
  1876. if (!pte ||
  1877. (huge_pte_none(huge_ptep_get(pte)) && !zeropage_ok) ||
  1878. (write && !pte_write(huge_ptep_get(pte)))) {
  1879. int ret;
  1880. spin_unlock(&mm->page_table_lock);
  1881. ret = hugetlb_fault(mm, vma, vaddr, write);
  1882. spin_lock(&mm->page_table_lock);
  1883. if (!(ret & VM_FAULT_ERROR))
  1884. continue;
  1885. remainder = 0;
  1886. if (!i)
  1887. i = -EFAULT;
  1888. break;
  1889. }
  1890. pfn_offset = (vaddr & ~huge_page_mask(h)) >> PAGE_SHIFT;
  1891. page = pte_page(huge_ptep_get(pte));
  1892. same_page:
  1893. if (pages) {
  1894. if (zeropage_ok)
  1895. pages[i] = ZERO_PAGE(0);
  1896. else
  1897. pages[i] = mem_map_offset(page, pfn_offset);
  1898. get_page(pages[i]);
  1899. }
  1900. if (vmas)
  1901. vmas[i] = vma;
  1902. vaddr += PAGE_SIZE;
  1903. ++pfn_offset;
  1904. --remainder;
  1905. ++i;
  1906. if (vaddr < vma->vm_end && remainder &&
  1907. pfn_offset < pages_per_huge_page(h)) {
  1908. /*
  1909. * We use pfn_offset to avoid touching the pageframes
  1910. * of this compound page.
  1911. */
  1912. goto same_page;
  1913. }
  1914. }
  1915. spin_unlock(&mm->page_table_lock);
  1916. *length = remainder;
  1917. *position = vaddr;
  1918. return i;
  1919. }
  1920. void hugetlb_change_protection(struct vm_area_struct *vma,
  1921. unsigned long address, unsigned long end, pgprot_t newprot)
  1922. {
  1923. struct mm_struct *mm = vma->vm_mm;
  1924. unsigned long start = address;
  1925. pte_t *ptep;
  1926. pte_t pte;
  1927. struct hstate *h = hstate_vma(vma);
  1928. BUG_ON(address >= end);
  1929. flush_cache_range(vma, address, end);
  1930. spin_lock(&vma->vm_file->f_mapping->i_mmap_lock);
  1931. spin_lock(&mm->page_table_lock);
  1932. for (; address < end; address += huge_page_size(h)) {
  1933. ptep = huge_pte_offset(mm, address);
  1934. if (!ptep)
  1935. continue;
  1936. if (huge_pmd_unshare(mm, &address, ptep))
  1937. continue;
  1938. if (!huge_pte_none(huge_ptep_get(ptep))) {
  1939. pte = huge_ptep_get_and_clear(mm, address, ptep);
  1940. pte = pte_mkhuge(pte_modify(pte, newprot));
  1941. set_huge_pte_at(mm, address, ptep, pte);
  1942. }
  1943. }
  1944. spin_unlock(&mm->page_table_lock);
  1945. spin_unlock(&vma->vm_file->f_mapping->i_mmap_lock);
  1946. flush_tlb_range(vma, start, end);
  1947. }
  1948. int hugetlb_reserve_pages(struct inode *inode,
  1949. long from, long to,
  1950. struct vm_area_struct *vma,
  1951. int acctflag)
  1952. {
  1953. long ret, chg;
  1954. struct hstate *h = hstate_inode(inode);
  1955. /*
  1956. * Only apply hugepage reservation if asked. At fault time, an
  1957. * attempt will be made for VM_NORESERVE to allocate a page
  1958. * and filesystem quota without using reserves
  1959. */
  1960. if (acctflag & VM_NORESERVE)
  1961. return 0;
  1962. /*
  1963. * Shared mappings base their reservation on the number of pages that
  1964. * are already allocated on behalf of the file. Private mappings need
  1965. * to reserve the full area even if read-only as mprotect() may be
  1966. * called to make the mapping read-write. Assume !vma is a shm mapping
  1967. */
  1968. if (!vma || vma->vm_flags & VM_SHARED)
  1969. chg = region_chg(&inode->i_mapping->private_list, from, to);
  1970. else {
  1971. struct resv_map *resv_map = resv_map_alloc();
  1972. if (!resv_map)
  1973. return -ENOMEM;
  1974. chg = to - from;
  1975. set_vma_resv_map(vma, resv_map);
  1976. set_vma_resv_flags(vma, HPAGE_RESV_OWNER);
  1977. }
  1978. if (chg < 0)
  1979. return chg;
  1980. /* There must be enough filesystem quota for the mapping */
  1981. if (hugetlb_get_quota(inode->i_mapping, chg))
  1982. return -ENOSPC;
  1983. /*
  1984. * Check enough hugepages are available for the reservation.
  1985. * Hand back the quota if there are not
  1986. */
  1987. ret = hugetlb_acct_memory(h, chg);
  1988. if (ret < 0) {
  1989. hugetlb_put_quota(inode->i_mapping, chg);
  1990. return ret;
  1991. }
  1992. /*
  1993. * Account for the reservations made. Shared mappings record regions
  1994. * that have reservations as they are shared by multiple VMAs.
  1995. * When the last VMA disappears, the region map says how much
  1996. * the reservation was and the page cache tells how much of
  1997. * the reservation was consumed. Private mappings are per-VMA and
  1998. * only the consumed reservations are tracked. When the VMA
  1999. * disappears, the original reservation is the VMA size and the
  2000. * consumed reservations are stored in the map. Hence, nothing
  2001. * else has to be done for private mappings here
  2002. */
  2003. if (!vma || vma->vm_flags & VM_SHARED)
  2004. region_add(&inode->i_mapping->private_list, from, to);
  2005. return 0;
  2006. }
  2007. void hugetlb_unreserve_pages(struct inode *inode, long offset, long freed)
  2008. {
  2009. struct hstate *h = hstate_inode(inode);
  2010. long chg = region_truncate(&inode->i_mapping->private_list, offset);
  2011. spin_lock(&inode->i_lock);
  2012. inode->i_blocks -= blocks_per_huge_page(h);
  2013. spin_unlock(&inode->i_lock);
  2014. hugetlb_put_quota(inode->i_mapping, (chg - freed));
  2015. hugetlb_acct_memory(h, -(chg - freed));
  2016. }