hugetlb.c 60 KB

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