hugetlb.c 76 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959
  1. /*
  2. * Generic hugetlb support.
  3. * (C) William Irwin, April 2004
  4. */
  5. #include <linux/list.h>
  6. #include <linux/init.h>
  7. #include <linux/module.h>
  8. #include <linux/mm.h>
  9. #include <linux/seq_file.h>
  10. #include <linux/sysctl.h>
  11. #include <linux/highmem.h>
  12. #include <linux/mmu_notifier.h>
  13. #include <linux/nodemask.h>
  14. #include <linux/pagemap.h>
  15. #include <linux/mempolicy.h>
  16. #include <linux/cpuset.h>
  17. #include <linux/mutex.h>
  18. #include <linux/bootmem.h>
  19. #include <linux/sysfs.h>
  20. #include <linux/slab.h>
  21. #include <linux/rmap.h>
  22. #include <linux/swap.h>
  23. #include <linux/swapops.h>
  24. #include <asm/page.h>
  25. #include <asm/pgtable.h>
  26. #include <asm/io.h>
  27. #include <linux/hugetlb.h>
  28. #include <linux/node.h>
  29. #include "internal.h"
  30. const unsigned long hugetlb_zero = 0, hugetlb_infinity = ~0UL;
  31. static gfp_t htlb_alloc_mask = GFP_HIGHUSER;
  32. unsigned long hugepages_treat_as_movable;
  33. static int max_hstate;
  34. unsigned int default_hstate_idx;
  35. struct hstate hstates[HUGE_MAX_HSTATE];
  36. __initdata LIST_HEAD(huge_boot_pages);
  37. /* for command line parsing */
  38. static struct hstate * __initdata parsed_hstate;
  39. static unsigned long __initdata default_hstate_max_huge_pages;
  40. static unsigned long __initdata default_hstate_size;
  41. #define for_each_hstate(h) \
  42. for ((h) = hstates; (h) < &hstates[max_hstate]; (h)++)
  43. /*
  44. * Protects updates to hugepage_freelists, nr_huge_pages, and free_huge_pages
  45. */
  46. static DEFINE_SPINLOCK(hugetlb_lock);
  47. /*
  48. * Region tracking -- allows tracking of reservations and instantiated pages
  49. * across the pages in a mapping.
  50. *
  51. * The region data structures are protected by a combination of the mmap_sem
  52. * and the hugetlb_instantion_mutex. To access or modify a region the caller
  53. * must either hold the mmap_sem for write, or the mmap_sem for read and
  54. * the hugetlb_instantiation mutex:
  55. *
  56. * down_write(&mm->mmap_sem);
  57. * or
  58. * down_read(&mm->mmap_sem);
  59. * mutex_lock(&hugetlb_instantiation_mutex);
  60. */
  61. struct file_region {
  62. struct list_head link;
  63. long from;
  64. long to;
  65. };
  66. static long region_add(struct list_head *head, long f, long t)
  67. {
  68. struct file_region *rg, *nrg, *trg;
  69. /* Locate the region we are either in or before. */
  70. list_for_each_entry(rg, head, link)
  71. if (f <= rg->to)
  72. break;
  73. /* Round our left edge to the current segment if it encloses us. */
  74. if (f > rg->from)
  75. f = rg->from;
  76. /* Check for and consume any regions we now overlap with. */
  77. nrg = rg;
  78. list_for_each_entry_safe(rg, trg, rg->link.prev, link) {
  79. if (&rg->link == head)
  80. break;
  81. if (rg->from > t)
  82. break;
  83. /* If this area reaches higher then extend our area to
  84. * include it completely. If this is not the first area
  85. * which we intend to reuse, free it. */
  86. if (rg->to > t)
  87. t = rg->to;
  88. if (rg != nrg) {
  89. list_del(&rg->link);
  90. kfree(rg);
  91. }
  92. }
  93. nrg->from = f;
  94. nrg->to = t;
  95. return 0;
  96. }
  97. static long region_chg(struct list_head *head, long f, long t)
  98. {
  99. struct file_region *rg, *nrg;
  100. long chg = 0;
  101. /* Locate the region we are before or in. */
  102. list_for_each_entry(rg, head, link)
  103. if (f <= rg->to)
  104. break;
  105. /* If we are below the current region then a new region is required.
  106. * Subtle, allocate a new region at the position but make it zero
  107. * size such that we can guarantee to record the reservation. */
  108. if (&rg->link == head || t < rg->from) {
  109. nrg = kmalloc(sizeof(*nrg), GFP_KERNEL);
  110. if (!nrg)
  111. return -ENOMEM;
  112. nrg->from = f;
  113. nrg->to = f;
  114. INIT_LIST_HEAD(&nrg->link);
  115. list_add(&nrg->link, rg->link.prev);
  116. return t - f;
  117. }
  118. /* Round our left edge to the current segment if it encloses us. */
  119. if (f > rg->from)
  120. f = rg->from;
  121. chg = t - f;
  122. /* Check for and consume any regions we now overlap with. */
  123. list_for_each_entry(rg, rg->link.prev, link) {
  124. if (&rg->link == head)
  125. break;
  126. if (rg->from > t)
  127. return chg;
  128. /* We overlap with this area, if it extends further than
  129. * us then we must extend ourselves. Account for its
  130. * existing reservation. */
  131. if (rg->to > t) {
  132. chg += rg->to - t;
  133. t = rg->to;
  134. }
  135. chg -= rg->to - rg->from;
  136. }
  137. return chg;
  138. }
  139. static long region_truncate(struct list_head *head, long end)
  140. {
  141. struct file_region *rg, *trg;
  142. long chg = 0;
  143. /* Locate the region we are either in or before. */
  144. list_for_each_entry(rg, head, link)
  145. if (end <= rg->to)
  146. break;
  147. if (&rg->link == head)
  148. return 0;
  149. /* If we are in the middle of a region then adjust it. */
  150. if (end > rg->from) {
  151. chg = rg->to - end;
  152. rg->to = end;
  153. rg = list_entry(rg->link.next, typeof(*rg), link);
  154. }
  155. /* Drop any remaining regions. */
  156. list_for_each_entry_safe(rg, trg, rg->link.prev, link) {
  157. if (&rg->link == head)
  158. break;
  159. chg += rg->to - rg->from;
  160. list_del(&rg->link);
  161. kfree(rg);
  162. }
  163. return chg;
  164. }
  165. static long region_count(struct list_head *head, long f, long t)
  166. {
  167. struct file_region *rg;
  168. long chg = 0;
  169. /* Locate each segment we overlap with, and count that overlap. */
  170. list_for_each_entry(rg, head, link) {
  171. int seg_from;
  172. int seg_to;
  173. if (rg->to <= f)
  174. continue;
  175. if (rg->from >= t)
  176. break;
  177. seg_from = max(rg->from, f);
  178. seg_to = min(rg->to, t);
  179. chg += seg_to - seg_from;
  180. }
  181. return chg;
  182. }
  183. /*
  184. * Convert the address within this vma to the page offset within
  185. * the mapping, in pagecache page units; huge pages here.
  186. */
  187. static pgoff_t vma_hugecache_offset(struct hstate *h,
  188. struct vm_area_struct *vma, unsigned long address)
  189. {
  190. return ((address - vma->vm_start) >> huge_page_shift(h)) +
  191. (vma->vm_pgoff >> huge_page_order(h));
  192. }
  193. pgoff_t linear_hugepage_index(struct vm_area_struct *vma,
  194. unsigned long address)
  195. {
  196. return vma_hugecache_offset(hstate_vma(vma), vma, address);
  197. }
  198. /*
  199. * Return the size of the pages allocated when backing a VMA. In the majority
  200. * cases this will be same size as used by the page table entries.
  201. */
  202. unsigned long vma_kernel_pagesize(struct vm_area_struct *vma)
  203. {
  204. struct hstate *hstate;
  205. if (!is_vm_hugetlb_page(vma))
  206. return PAGE_SIZE;
  207. hstate = hstate_vma(vma);
  208. return 1UL << (hstate->order + PAGE_SHIFT);
  209. }
  210. EXPORT_SYMBOL_GPL(vma_kernel_pagesize);
  211. /*
  212. * Return the page size being used by the MMU to back a VMA. In the majority
  213. * of cases, the page size used by the kernel matches the MMU size. On
  214. * architectures where it differs, an architecture-specific version of this
  215. * function is required.
  216. */
  217. #ifndef vma_mmu_pagesize
  218. unsigned long vma_mmu_pagesize(struct vm_area_struct *vma)
  219. {
  220. return vma_kernel_pagesize(vma);
  221. }
  222. #endif
  223. /*
  224. * Flags for MAP_PRIVATE reservations. These are stored in the bottom
  225. * bits of the reservation map pointer, which are always clear due to
  226. * alignment.
  227. */
  228. #define HPAGE_RESV_OWNER (1UL << 0)
  229. #define HPAGE_RESV_UNMAPPED (1UL << 1)
  230. #define HPAGE_RESV_MASK (HPAGE_RESV_OWNER | HPAGE_RESV_UNMAPPED)
  231. /*
  232. * These helpers are used to track how many pages are reserved for
  233. * faults in a MAP_PRIVATE mapping. Only the process that called mmap()
  234. * is guaranteed to have their future faults succeed.
  235. *
  236. * With the exception of reset_vma_resv_huge_pages() which is called at fork(),
  237. * the reserve counters are updated with the hugetlb_lock held. It is safe
  238. * to reset the VMA at fork() time as it is not in use yet and there is no
  239. * chance of the global counters getting corrupted as a result of the values.
  240. *
  241. * The private mapping reservation is represented in a subtly different
  242. * manner to a shared mapping. A shared mapping has a region map associated
  243. * with the underlying file, this region map represents the backing file
  244. * pages which have ever had a reservation assigned which this persists even
  245. * after the page is instantiated. A private mapping has a region map
  246. * associated with the original mmap which is attached to all VMAs which
  247. * reference it, this region map represents those offsets which have consumed
  248. * reservation ie. where pages have been instantiated.
  249. */
  250. static unsigned long get_vma_private_data(struct vm_area_struct *vma)
  251. {
  252. return (unsigned long)vma->vm_private_data;
  253. }
  254. static void set_vma_private_data(struct vm_area_struct *vma,
  255. unsigned long value)
  256. {
  257. vma->vm_private_data = (void *)value;
  258. }
  259. struct resv_map {
  260. struct kref refs;
  261. struct list_head regions;
  262. };
  263. static struct resv_map *resv_map_alloc(void)
  264. {
  265. struct resv_map *resv_map = kmalloc(sizeof(*resv_map), GFP_KERNEL);
  266. if (!resv_map)
  267. return NULL;
  268. kref_init(&resv_map->refs);
  269. INIT_LIST_HEAD(&resv_map->regions);
  270. return resv_map;
  271. }
  272. static void resv_map_release(struct kref *ref)
  273. {
  274. struct resv_map *resv_map = container_of(ref, struct resv_map, refs);
  275. /* Clear out any active regions before we release the map. */
  276. region_truncate(&resv_map->regions, 0);
  277. kfree(resv_map);
  278. }
  279. static struct resv_map *vma_resv_map(struct vm_area_struct *vma)
  280. {
  281. VM_BUG_ON(!is_vm_hugetlb_page(vma));
  282. if (!(vma->vm_flags & VM_MAYSHARE))
  283. return (struct resv_map *)(get_vma_private_data(vma) &
  284. ~HPAGE_RESV_MASK);
  285. return NULL;
  286. }
  287. static void set_vma_resv_map(struct vm_area_struct *vma, struct resv_map *map)
  288. {
  289. VM_BUG_ON(!is_vm_hugetlb_page(vma));
  290. VM_BUG_ON(vma->vm_flags & VM_MAYSHARE);
  291. set_vma_private_data(vma, (get_vma_private_data(vma) &
  292. HPAGE_RESV_MASK) | (unsigned long)map);
  293. }
  294. static void set_vma_resv_flags(struct vm_area_struct *vma, unsigned long flags)
  295. {
  296. VM_BUG_ON(!is_vm_hugetlb_page(vma));
  297. VM_BUG_ON(vma->vm_flags & VM_MAYSHARE);
  298. set_vma_private_data(vma, get_vma_private_data(vma) | flags);
  299. }
  300. static int is_vma_resv_set(struct vm_area_struct *vma, unsigned long flag)
  301. {
  302. VM_BUG_ON(!is_vm_hugetlb_page(vma));
  303. return (get_vma_private_data(vma) & flag) != 0;
  304. }
  305. /* Decrement the reserved pages in the hugepage pool by one */
  306. static void decrement_hugepage_resv_vma(struct hstate *h,
  307. struct vm_area_struct *vma)
  308. {
  309. if (vma->vm_flags & VM_NORESERVE)
  310. return;
  311. if (vma->vm_flags & VM_MAYSHARE) {
  312. /* Shared mappings always use reserves */
  313. h->resv_huge_pages--;
  314. } else if (is_vma_resv_set(vma, HPAGE_RESV_OWNER)) {
  315. /*
  316. * Only the process that called mmap() has reserves for
  317. * private mappings.
  318. */
  319. h->resv_huge_pages--;
  320. }
  321. }
  322. /* Reset counters to 0 and clear all HPAGE_RESV_* flags */
  323. void reset_vma_resv_huge_pages(struct vm_area_struct *vma)
  324. {
  325. VM_BUG_ON(!is_vm_hugetlb_page(vma));
  326. if (!(vma->vm_flags & VM_MAYSHARE))
  327. vma->vm_private_data = (void *)0;
  328. }
  329. /* Returns true if the VMA has associated reserve pages */
  330. static int vma_has_reserves(struct vm_area_struct *vma)
  331. {
  332. if (vma->vm_flags & VM_MAYSHARE)
  333. return 1;
  334. if (is_vma_resv_set(vma, HPAGE_RESV_OWNER))
  335. return 1;
  336. return 0;
  337. }
  338. static void copy_gigantic_page(struct page *dst, struct page *src)
  339. {
  340. int i;
  341. struct hstate *h = page_hstate(src);
  342. struct page *dst_base = dst;
  343. struct page *src_base = src;
  344. for (i = 0; i < pages_per_huge_page(h); ) {
  345. cond_resched();
  346. copy_highpage(dst, src);
  347. i++;
  348. dst = mem_map_next(dst, dst_base, i);
  349. src = mem_map_next(src, src_base, i);
  350. }
  351. }
  352. void copy_huge_page(struct page *dst, struct page *src)
  353. {
  354. int i;
  355. struct hstate *h = page_hstate(src);
  356. if (unlikely(pages_per_huge_page(h) > MAX_ORDER_NR_PAGES)) {
  357. copy_gigantic_page(dst, src);
  358. return;
  359. }
  360. might_sleep();
  361. for (i = 0; i < pages_per_huge_page(h); i++) {
  362. cond_resched();
  363. copy_highpage(dst + i, src + i);
  364. }
  365. }
  366. static void enqueue_huge_page(struct hstate *h, struct page *page)
  367. {
  368. int nid = page_to_nid(page);
  369. list_add(&page->lru, &h->hugepage_freelists[nid]);
  370. h->free_huge_pages++;
  371. h->free_huge_pages_node[nid]++;
  372. }
  373. static struct page *dequeue_huge_page_node(struct hstate *h, int nid)
  374. {
  375. struct page *page;
  376. if (list_empty(&h->hugepage_freelists[nid]))
  377. return NULL;
  378. page = list_entry(h->hugepage_freelists[nid].next, struct page, lru);
  379. list_del(&page->lru);
  380. set_page_refcounted(page);
  381. h->free_huge_pages--;
  382. h->free_huge_pages_node[nid]--;
  383. return page;
  384. }
  385. static struct page *dequeue_huge_page_vma(struct hstate *h,
  386. struct vm_area_struct *vma,
  387. unsigned long address, int avoid_reserve)
  388. {
  389. struct page *page = NULL;
  390. struct mempolicy *mpol;
  391. nodemask_t *nodemask;
  392. struct zonelist *zonelist;
  393. struct zone *zone;
  394. struct zoneref *z;
  395. get_mems_allowed();
  396. zonelist = huge_zonelist(vma, address,
  397. htlb_alloc_mask, &mpol, &nodemask);
  398. /*
  399. * A child process with MAP_PRIVATE mappings created by their parent
  400. * have no page reserves. This check ensures that reservations are
  401. * not "stolen". The child may still get SIGKILLed
  402. */
  403. if (!vma_has_reserves(vma) &&
  404. h->free_huge_pages - h->resv_huge_pages == 0)
  405. goto err;
  406. /* If reserves cannot be used, ensure enough pages are in the pool */
  407. if (avoid_reserve && h->free_huge_pages - h->resv_huge_pages == 0)
  408. goto err;
  409. for_each_zone_zonelist_nodemask(zone, z, zonelist,
  410. MAX_NR_ZONES - 1, nodemask) {
  411. if (cpuset_zone_allowed_softwall(zone, htlb_alloc_mask)) {
  412. page = dequeue_huge_page_node(h, zone_to_nid(zone));
  413. if (page) {
  414. if (!avoid_reserve)
  415. decrement_hugepage_resv_vma(h, vma);
  416. break;
  417. }
  418. }
  419. }
  420. err:
  421. mpol_cond_put(mpol);
  422. put_mems_allowed();
  423. return page;
  424. }
  425. static void update_and_free_page(struct hstate *h, struct page *page)
  426. {
  427. int i;
  428. VM_BUG_ON(h->order >= MAX_ORDER);
  429. h->nr_huge_pages--;
  430. h->nr_huge_pages_node[page_to_nid(page)]--;
  431. for (i = 0; i < pages_per_huge_page(h); i++) {
  432. page[i].flags &= ~(1 << PG_locked | 1 << PG_error | 1 << PG_referenced |
  433. 1 << PG_dirty | 1 << PG_active | 1 << PG_reserved |
  434. 1 << PG_private | 1<< PG_writeback);
  435. }
  436. set_compound_page_dtor(page, NULL);
  437. set_page_refcounted(page);
  438. arch_release_hugepage(page);
  439. __free_pages(page, huge_page_order(h));
  440. }
  441. struct hstate *size_to_hstate(unsigned long size)
  442. {
  443. struct hstate *h;
  444. for_each_hstate(h) {
  445. if (huge_page_size(h) == size)
  446. return h;
  447. }
  448. return NULL;
  449. }
  450. static void free_huge_page(struct page *page)
  451. {
  452. /*
  453. * Can't pass hstate in here because it is called from the
  454. * compound page destructor.
  455. */
  456. struct hstate *h = page_hstate(page);
  457. int nid = page_to_nid(page);
  458. struct address_space *mapping;
  459. mapping = (struct address_space *) page_private(page);
  460. set_page_private(page, 0);
  461. page->mapping = NULL;
  462. BUG_ON(page_count(page));
  463. BUG_ON(page_mapcount(page));
  464. INIT_LIST_HEAD(&page->lru);
  465. spin_lock(&hugetlb_lock);
  466. if (h->surplus_huge_pages_node[nid] && huge_page_order(h) < MAX_ORDER) {
  467. update_and_free_page(h, page);
  468. h->surplus_huge_pages--;
  469. h->surplus_huge_pages_node[nid]--;
  470. } else {
  471. enqueue_huge_page(h, page);
  472. }
  473. spin_unlock(&hugetlb_lock);
  474. if (mapping)
  475. hugetlb_put_quota(mapping, 1);
  476. }
  477. static void prep_new_huge_page(struct hstate *h, struct page *page, int nid)
  478. {
  479. set_compound_page_dtor(page, free_huge_page);
  480. spin_lock(&hugetlb_lock);
  481. h->nr_huge_pages++;
  482. h->nr_huge_pages_node[nid]++;
  483. spin_unlock(&hugetlb_lock);
  484. put_page(page); /* free it into the hugepage allocator */
  485. }
  486. static void prep_compound_gigantic_page(struct page *page, unsigned long order)
  487. {
  488. int i;
  489. int nr_pages = 1 << order;
  490. struct page *p = page + 1;
  491. /* we rely on prep_new_huge_page to set the destructor */
  492. set_compound_order(page, order);
  493. __SetPageHead(page);
  494. for (i = 1; i < nr_pages; i++, p = mem_map_next(p, page, i)) {
  495. __SetPageTail(p);
  496. p->first_page = page;
  497. }
  498. }
  499. int PageHuge(struct page *page)
  500. {
  501. compound_page_dtor *dtor;
  502. if (!PageCompound(page))
  503. return 0;
  504. page = compound_head(page);
  505. dtor = get_compound_page_dtor(page);
  506. return dtor == free_huge_page;
  507. }
  508. EXPORT_SYMBOL_GPL(PageHuge);
  509. static struct page *alloc_fresh_huge_page_node(struct hstate *h, int nid)
  510. {
  511. struct page *page;
  512. if (h->order >= MAX_ORDER)
  513. return NULL;
  514. page = alloc_pages_exact_node(nid,
  515. htlb_alloc_mask|__GFP_COMP|__GFP_THISNODE|
  516. __GFP_REPEAT|__GFP_NOWARN,
  517. huge_page_order(h));
  518. if (page) {
  519. if (arch_prepare_hugepage(page)) {
  520. __free_pages(page, huge_page_order(h));
  521. return NULL;
  522. }
  523. prep_new_huge_page(h, page, nid);
  524. }
  525. return page;
  526. }
  527. /*
  528. * common helper functions for hstate_next_node_to_{alloc|free}.
  529. * We may have allocated or freed a huge page based on a different
  530. * nodes_allowed previously, so h->next_node_to_{alloc|free} might
  531. * be outside of *nodes_allowed. Ensure that we use an allowed
  532. * node for alloc or free.
  533. */
  534. static int next_node_allowed(int nid, nodemask_t *nodes_allowed)
  535. {
  536. nid = next_node(nid, *nodes_allowed);
  537. if (nid == MAX_NUMNODES)
  538. nid = first_node(*nodes_allowed);
  539. VM_BUG_ON(nid >= MAX_NUMNODES);
  540. return nid;
  541. }
  542. static int get_valid_node_allowed(int nid, nodemask_t *nodes_allowed)
  543. {
  544. if (!node_isset(nid, *nodes_allowed))
  545. nid = next_node_allowed(nid, nodes_allowed);
  546. return nid;
  547. }
  548. /*
  549. * returns the previously saved node ["this node"] from which to
  550. * allocate a persistent huge page for the pool and advance the
  551. * next node from which to allocate, handling wrap at end of node
  552. * mask.
  553. */
  554. static int hstate_next_node_to_alloc(struct hstate *h,
  555. nodemask_t *nodes_allowed)
  556. {
  557. int nid;
  558. VM_BUG_ON(!nodes_allowed);
  559. nid = get_valid_node_allowed(h->next_nid_to_alloc, nodes_allowed);
  560. h->next_nid_to_alloc = next_node_allowed(nid, nodes_allowed);
  561. return nid;
  562. }
  563. static int alloc_fresh_huge_page(struct hstate *h, nodemask_t *nodes_allowed)
  564. {
  565. struct page *page;
  566. int start_nid;
  567. int next_nid;
  568. int ret = 0;
  569. start_nid = hstate_next_node_to_alloc(h, nodes_allowed);
  570. next_nid = start_nid;
  571. do {
  572. page = alloc_fresh_huge_page_node(h, next_nid);
  573. if (page) {
  574. ret = 1;
  575. break;
  576. }
  577. next_nid = hstate_next_node_to_alloc(h, nodes_allowed);
  578. } while (next_nid != start_nid);
  579. if (ret)
  580. count_vm_event(HTLB_BUDDY_PGALLOC);
  581. else
  582. count_vm_event(HTLB_BUDDY_PGALLOC_FAIL);
  583. return ret;
  584. }
  585. /*
  586. * helper for free_pool_huge_page() - return the previously saved
  587. * node ["this node"] from which to free a huge page. Advance the
  588. * next node id whether or not we find a free huge page to free so
  589. * that the next attempt to free addresses the next node.
  590. */
  591. static int hstate_next_node_to_free(struct hstate *h, nodemask_t *nodes_allowed)
  592. {
  593. int nid;
  594. VM_BUG_ON(!nodes_allowed);
  595. nid = get_valid_node_allowed(h->next_nid_to_free, nodes_allowed);
  596. h->next_nid_to_free = next_node_allowed(nid, nodes_allowed);
  597. return nid;
  598. }
  599. /*
  600. * Free huge page from pool from next node to free.
  601. * Attempt to keep persistent huge pages more or less
  602. * balanced over allowed nodes.
  603. * Called with hugetlb_lock locked.
  604. */
  605. static int free_pool_huge_page(struct hstate *h, nodemask_t *nodes_allowed,
  606. bool acct_surplus)
  607. {
  608. int start_nid;
  609. int next_nid;
  610. int ret = 0;
  611. start_nid = hstate_next_node_to_free(h, nodes_allowed);
  612. next_nid = start_nid;
  613. do {
  614. /*
  615. * If we're returning unused surplus pages, only examine
  616. * nodes with surplus pages.
  617. */
  618. if ((!acct_surplus || h->surplus_huge_pages_node[next_nid]) &&
  619. !list_empty(&h->hugepage_freelists[next_nid])) {
  620. struct page *page =
  621. list_entry(h->hugepage_freelists[next_nid].next,
  622. struct page, lru);
  623. list_del(&page->lru);
  624. h->free_huge_pages--;
  625. h->free_huge_pages_node[next_nid]--;
  626. if (acct_surplus) {
  627. h->surplus_huge_pages--;
  628. h->surplus_huge_pages_node[next_nid]--;
  629. }
  630. update_and_free_page(h, page);
  631. ret = 1;
  632. break;
  633. }
  634. next_nid = hstate_next_node_to_free(h, nodes_allowed);
  635. } while (next_nid != start_nid);
  636. return ret;
  637. }
  638. static struct page *alloc_buddy_huge_page(struct hstate *h, int nid)
  639. {
  640. struct page *page;
  641. unsigned int r_nid;
  642. if (h->order >= MAX_ORDER)
  643. return NULL;
  644. /*
  645. * Assume we will successfully allocate the surplus page to
  646. * prevent racing processes from causing the surplus to exceed
  647. * overcommit
  648. *
  649. * This however introduces a different race, where a process B
  650. * tries to grow the static hugepage pool while alloc_pages() is
  651. * called by process A. B will only examine the per-node
  652. * counters in determining if surplus huge pages can be
  653. * converted to normal huge pages in adjust_pool_surplus(). A
  654. * won't be able to increment the per-node counter, until the
  655. * lock is dropped by B, but B doesn't drop hugetlb_lock until
  656. * no more huge pages can be converted from surplus to normal
  657. * state (and doesn't try to convert again). Thus, we have a
  658. * case where a surplus huge page exists, the pool is grown, and
  659. * the surplus huge page still exists after, even though it
  660. * should just have been converted to a normal huge page. This
  661. * does not leak memory, though, as the hugepage will be freed
  662. * once it is out of use. It also does not allow the counters to
  663. * go out of whack in adjust_pool_surplus() as we don't modify
  664. * the node values until we've gotten the hugepage and only the
  665. * per-node value is checked there.
  666. */
  667. spin_lock(&hugetlb_lock);
  668. if (h->surplus_huge_pages >= h->nr_overcommit_huge_pages) {
  669. spin_unlock(&hugetlb_lock);
  670. return NULL;
  671. } else {
  672. h->nr_huge_pages++;
  673. h->surplus_huge_pages++;
  674. }
  675. spin_unlock(&hugetlb_lock);
  676. if (nid == NUMA_NO_NODE)
  677. page = alloc_pages(htlb_alloc_mask|__GFP_COMP|
  678. __GFP_REPEAT|__GFP_NOWARN,
  679. huge_page_order(h));
  680. else
  681. page = alloc_pages_exact_node(nid,
  682. htlb_alloc_mask|__GFP_COMP|__GFP_THISNODE|
  683. __GFP_REPEAT|__GFP_NOWARN, huge_page_order(h));
  684. if (page && arch_prepare_hugepage(page)) {
  685. __free_pages(page, huge_page_order(h));
  686. return NULL;
  687. }
  688. spin_lock(&hugetlb_lock);
  689. if (page) {
  690. r_nid = page_to_nid(page);
  691. set_compound_page_dtor(page, free_huge_page);
  692. /*
  693. * We incremented the global counters already
  694. */
  695. h->nr_huge_pages_node[r_nid]++;
  696. h->surplus_huge_pages_node[r_nid]++;
  697. __count_vm_event(HTLB_BUDDY_PGALLOC);
  698. } else {
  699. h->nr_huge_pages--;
  700. h->surplus_huge_pages--;
  701. __count_vm_event(HTLB_BUDDY_PGALLOC_FAIL);
  702. }
  703. spin_unlock(&hugetlb_lock);
  704. return page;
  705. }
  706. /*
  707. * This allocation function is useful in the context where vma is irrelevant.
  708. * E.g. soft-offlining uses this function because it only cares physical
  709. * address of error page.
  710. */
  711. struct page *alloc_huge_page_node(struct hstate *h, int nid)
  712. {
  713. struct page *page;
  714. spin_lock(&hugetlb_lock);
  715. page = dequeue_huge_page_node(h, nid);
  716. spin_unlock(&hugetlb_lock);
  717. if (!page)
  718. page = alloc_buddy_huge_page(h, nid);
  719. return page;
  720. }
  721. /*
  722. * Increase the hugetlb pool such that it can accommodate a reservation
  723. * of size 'delta'.
  724. */
  725. static int gather_surplus_pages(struct hstate *h, int delta)
  726. {
  727. struct list_head surplus_list;
  728. struct page *page, *tmp;
  729. int ret, i;
  730. int needed, allocated;
  731. needed = (h->resv_huge_pages + delta) - h->free_huge_pages;
  732. if (needed <= 0) {
  733. h->resv_huge_pages += delta;
  734. return 0;
  735. }
  736. allocated = 0;
  737. INIT_LIST_HEAD(&surplus_list);
  738. ret = -ENOMEM;
  739. retry:
  740. spin_unlock(&hugetlb_lock);
  741. for (i = 0; i < needed; i++) {
  742. page = alloc_buddy_huge_page(h, NUMA_NO_NODE);
  743. if (!page)
  744. /*
  745. * We were not able to allocate enough pages to
  746. * satisfy the entire reservation so we free what
  747. * we've allocated so far.
  748. */
  749. goto free;
  750. list_add(&page->lru, &surplus_list);
  751. }
  752. allocated += needed;
  753. /*
  754. * After retaking hugetlb_lock, we need to recalculate 'needed'
  755. * because either resv_huge_pages or free_huge_pages may have changed.
  756. */
  757. spin_lock(&hugetlb_lock);
  758. needed = (h->resv_huge_pages + delta) -
  759. (h->free_huge_pages + allocated);
  760. if (needed > 0)
  761. goto retry;
  762. /*
  763. * The surplus_list now contains _at_least_ the number of extra pages
  764. * needed to accommodate the reservation. Add the appropriate number
  765. * of pages to the hugetlb pool and free the extras back to the buddy
  766. * allocator. Commit the entire reservation here to prevent another
  767. * process from stealing the pages as they are added to the pool but
  768. * before they are reserved.
  769. */
  770. needed += allocated;
  771. h->resv_huge_pages += delta;
  772. ret = 0;
  773. spin_unlock(&hugetlb_lock);
  774. /* Free the needed pages to the hugetlb pool */
  775. list_for_each_entry_safe(page, tmp, &surplus_list, lru) {
  776. if ((--needed) < 0)
  777. break;
  778. list_del(&page->lru);
  779. /*
  780. * This page is now managed by the hugetlb allocator and has
  781. * no users -- drop the buddy allocator's reference.
  782. */
  783. put_page_testzero(page);
  784. VM_BUG_ON(page_count(page));
  785. enqueue_huge_page(h, page);
  786. }
  787. /* Free unnecessary surplus pages to the buddy allocator */
  788. free:
  789. if (!list_empty(&surplus_list)) {
  790. list_for_each_entry_safe(page, tmp, &surplus_list, lru) {
  791. list_del(&page->lru);
  792. put_page(page);
  793. }
  794. }
  795. spin_lock(&hugetlb_lock);
  796. return ret;
  797. }
  798. /*
  799. * When releasing a hugetlb pool reservation, any surplus pages that were
  800. * allocated to satisfy the reservation must be explicitly freed if they were
  801. * never used.
  802. * Called with hugetlb_lock held.
  803. */
  804. static void return_unused_surplus_pages(struct hstate *h,
  805. unsigned long unused_resv_pages)
  806. {
  807. unsigned long nr_pages;
  808. /* Uncommit the reservation */
  809. h->resv_huge_pages -= unused_resv_pages;
  810. /* Cannot return gigantic pages currently */
  811. if (h->order >= MAX_ORDER)
  812. return;
  813. nr_pages = min(unused_resv_pages, h->surplus_huge_pages);
  814. /*
  815. * We want to release as many surplus pages as possible, spread
  816. * evenly across all nodes with memory. Iterate across these nodes
  817. * until we can no longer free unreserved surplus pages. This occurs
  818. * when the nodes with surplus pages have no free pages.
  819. * free_pool_huge_page() will balance the the freed pages across the
  820. * on-line nodes with memory and will handle the hstate accounting.
  821. */
  822. while (nr_pages--) {
  823. if (!free_pool_huge_page(h, &node_states[N_HIGH_MEMORY], 1))
  824. break;
  825. }
  826. }
  827. /*
  828. * Determine if the huge page at addr within the vma has an associated
  829. * reservation. Where it does not we will need to logically increase
  830. * reservation and actually increase quota before an allocation can occur.
  831. * Where any new reservation would be required the reservation change is
  832. * prepared, but not committed. Once the page has been quota'd allocated
  833. * an instantiated the change should be committed via vma_commit_reservation.
  834. * No action is required on failure.
  835. */
  836. static long vma_needs_reservation(struct hstate *h,
  837. struct vm_area_struct *vma, unsigned long addr)
  838. {
  839. struct address_space *mapping = vma->vm_file->f_mapping;
  840. struct inode *inode = mapping->host;
  841. if (vma->vm_flags & VM_MAYSHARE) {
  842. pgoff_t idx = vma_hugecache_offset(h, vma, addr);
  843. return region_chg(&inode->i_mapping->private_list,
  844. idx, idx + 1);
  845. } else if (!is_vma_resv_set(vma, HPAGE_RESV_OWNER)) {
  846. return 1;
  847. } else {
  848. long err;
  849. pgoff_t idx = vma_hugecache_offset(h, vma, addr);
  850. struct resv_map *reservations = vma_resv_map(vma);
  851. err = region_chg(&reservations->regions, idx, idx + 1);
  852. if (err < 0)
  853. return err;
  854. return 0;
  855. }
  856. }
  857. static void vma_commit_reservation(struct hstate *h,
  858. struct vm_area_struct *vma, unsigned long addr)
  859. {
  860. struct address_space *mapping = vma->vm_file->f_mapping;
  861. struct inode *inode = mapping->host;
  862. if (vma->vm_flags & VM_MAYSHARE) {
  863. pgoff_t idx = vma_hugecache_offset(h, vma, addr);
  864. region_add(&inode->i_mapping->private_list, idx, idx + 1);
  865. } else if (is_vma_resv_set(vma, HPAGE_RESV_OWNER)) {
  866. pgoff_t idx = vma_hugecache_offset(h, vma, addr);
  867. struct resv_map *reservations = vma_resv_map(vma);
  868. /* Mark this page used in the map. */
  869. region_add(&reservations->regions, idx, idx + 1);
  870. }
  871. }
  872. static struct page *alloc_huge_page(struct vm_area_struct *vma,
  873. unsigned long addr, int avoid_reserve)
  874. {
  875. struct hstate *h = hstate_vma(vma);
  876. struct page *page;
  877. struct address_space *mapping = vma->vm_file->f_mapping;
  878. struct inode *inode = mapping->host;
  879. long chg;
  880. /*
  881. * Processes that did not create the mapping will have no reserves and
  882. * will not have accounted against quota. Check that the quota can be
  883. * made before satisfying the allocation
  884. * MAP_NORESERVE mappings may also need pages and quota allocated
  885. * if no reserve mapping overlaps.
  886. */
  887. chg = vma_needs_reservation(h, vma, addr);
  888. if (chg < 0)
  889. return ERR_PTR(-VM_FAULT_OOM);
  890. if (chg)
  891. if (hugetlb_get_quota(inode->i_mapping, chg))
  892. return ERR_PTR(-VM_FAULT_SIGBUS);
  893. spin_lock(&hugetlb_lock);
  894. page = dequeue_huge_page_vma(h, vma, addr, avoid_reserve);
  895. spin_unlock(&hugetlb_lock);
  896. if (!page) {
  897. page = alloc_buddy_huge_page(h, NUMA_NO_NODE);
  898. if (!page) {
  899. hugetlb_put_quota(inode->i_mapping, chg);
  900. return ERR_PTR(-VM_FAULT_SIGBUS);
  901. }
  902. }
  903. set_page_private(page, (unsigned long) mapping);
  904. vma_commit_reservation(h, vma, addr);
  905. return page;
  906. }
  907. int __weak alloc_bootmem_huge_page(struct hstate *h)
  908. {
  909. struct huge_bootmem_page *m;
  910. int nr_nodes = nodes_weight(node_states[N_HIGH_MEMORY]);
  911. while (nr_nodes) {
  912. void *addr;
  913. addr = __alloc_bootmem_node_nopanic(
  914. NODE_DATA(hstate_next_node_to_alloc(h,
  915. &node_states[N_HIGH_MEMORY])),
  916. huge_page_size(h), huge_page_size(h), 0);
  917. if (addr) {
  918. /*
  919. * Use the beginning of the huge page to store the
  920. * huge_bootmem_page struct (until gather_bootmem
  921. * puts them into the mem_map).
  922. */
  923. m = addr;
  924. goto found;
  925. }
  926. nr_nodes--;
  927. }
  928. return 0;
  929. found:
  930. BUG_ON((unsigned long)virt_to_phys(m) & (huge_page_size(h) - 1));
  931. /* Put them into a private list first because mem_map is not up yet */
  932. list_add(&m->list, &huge_boot_pages);
  933. m->hstate = h;
  934. return 1;
  935. }
  936. static void prep_compound_huge_page(struct page *page, int order)
  937. {
  938. if (unlikely(order > (MAX_ORDER - 1)))
  939. prep_compound_gigantic_page(page, order);
  940. else
  941. prep_compound_page(page, order);
  942. }
  943. /* Put bootmem huge pages into the standard lists after mem_map is up */
  944. static void __init gather_bootmem_prealloc(void)
  945. {
  946. struct huge_bootmem_page *m;
  947. list_for_each_entry(m, &huge_boot_pages, list) {
  948. struct page *page = virt_to_page(m);
  949. struct hstate *h = m->hstate;
  950. __ClearPageReserved(page);
  951. WARN_ON(page_count(page) != 1);
  952. prep_compound_huge_page(page, h->order);
  953. prep_new_huge_page(h, page, page_to_nid(page));
  954. /*
  955. * If we had gigantic hugepages allocated at boot time, we need
  956. * to restore the 'stolen' pages to totalram_pages in order to
  957. * fix confusing memory reports from free(1) and another
  958. * side-effects, like CommitLimit going negative.
  959. */
  960. if (h->order > (MAX_ORDER - 1))
  961. totalram_pages += 1 << h->order;
  962. }
  963. }
  964. static void __init hugetlb_hstate_alloc_pages(struct hstate *h)
  965. {
  966. unsigned long i;
  967. for (i = 0; i < h->max_huge_pages; ++i) {
  968. if (h->order >= MAX_ORDER) {
  969. if (!alloc_bootmem_huge_page(h))
  970. break;
  971. } else if (!alloc_fresh_huge_page(h,
  972. &node_states[N_HIGH_MEMORY]))
  973. break;
  974. }
  975. h->max_huge_pages = i;
  976. }
  977. static void __init hugetlb_init_hstates(void)
  978. {
  979. struct hstate *h;
  980. for_each_hstate(h) {
  981. /* oversize hugepages were init'ed in early boot */
  982. if (h->order < MAX_ORDER)
  983. hugetlb_hstate_alloc_pages(h);
  984. }
  985. }
  986. static char * __init memfmt(char *buf, unsigned long n)
  987. {
  988. if (n >= (1UL << 30))
  989. sprintf(buf, "%lu GB", n >> 30);
  990. else if (n >= (1UL << 20))
  991. sprintf(buf, "%lu MB", n >> 20);
  992. else
  993. sprintf(buf, "%lu KB", n >> 10);
  994. return buf;
  995. }
  996. static void __init report_hugepages(void)
  997. {
  998. struct hstate *h;
  999. for_each_hstate(h) {
  1000. char buf[32];
  1001. printk(KERN_INFO "HugeTLB registered %s page size, "
  1002. "pre-allocated %ld pages\n",
  1003. memfmt(buf, huge_page_size(h)),
  1004. h->free_huge_pages);
  1005. }
  1006. }
  1007. #ifdef CONFIG_HIGHMEM
  1008. static void try_to_free_low(struct hstate *h, unsigned long count,
  1009. nodemask_t *nodes_allowed)
  1010. {
  1011. int i;
  1012. if (h->order >= MAX_ORDER)
  1013. return;
  1014. for_each_node_mask(i, *nodes_allowed) {
  1015. struct page *page, *next;
  1016. struct list_head *freel = &h->hugepage_freelists[i];
  1017. list_for_each_entry_safe(page, next, freel, lru) {
  1018. if (count >= h->nr_huge_pages)
  1019. return;
  1020. if (PageHighMem(page))
  1021. continue;
  1022. list_del(&page->lru);
  1023. update_and_free_page(h, page);
  1024. h->free_huge_pages--;
  1025. h->free_huge_pages_node[page_to_nid(page)]--;
  1026. }
  1027. }
  1028. }
  1029. #else
  1030. static inline void try_to_free_low(struct hstate *h, unsigned long count,
  1031. nodemask_t *nodes_allowed)
  1032. {
  1033. }
  1034. #endif
  1035. /*
  1036. * Increment or decrement surplus_huge_pages. Keep node-specific counters
  1037. * balanced by operating on them in a round-robin fashion.
  1038. * Returns 1 if an adjustment was made.
  1039. */
  1040. static int adjust_pool_surplus(struct hstate *h, nodemask_t *nodes_allowed,
  1041. int delta)
  1042. {
  1043. int start_nid, next_nid;
  1044. int ret = 0;
  1045. VM_BUG_ON(delta != -1 && delta != 1);
  1046. if (delta < 0)
  1047. start_nid = hstate_next_node_to_alloc(h, nodes_allowed);
  1048. else
  1049. start_nid = hstate_next_node_to_free(h, nodes_allowed);
  1050. next_nid = start_nid;
  1051. do {
  1052. int nid = next_nid;
  1053. if (delta < 0) {
  1054. /*
  1055. * To shrink on this node, there must be a surplus page
  1056. */
  1057. if (!h->surplus_huge_pages_node[nid]) {
  1058. next_nid = hstate_next_node_to_alloc(h,
  1059. nodes_allowed);
  1060. continue;
  1061. }
  1062. }
  1063. if (delta > 0) {
  1064. /*
  1065. * Surplus cannot exceed the total number of pages
  1066. */
  1067. if (h->surplus_huge_pages_node[nid] >=
  1068. h->nr_huge_pages_node[nid]) {
  1069. next_nid = hstate_next_node_to_free(h,
  1070. nodes_allowed);
  1071. continue;
  1072. }
  1073. }
  1074. h->surplus_huge_pages += delta;
  1075. h->surplus_huge_pages_node[nid] += delta;
  1076. ret = 1;
  1077. break;
  1078. } while (next_nid != start_nid);
  1079. return ret;
  1080. }
  1081. #define persistent_huge_pages(h) (h->nr_huge_pages - h->surplus_huge_pages)
  1082. static unsigned long set_max_huge_pages(struct hstate *h, unsigned long count,
  1083. nodemask_t *nodes_allowed)
  1084. {
  1085. unsigned long min_count, ret;
  1086. if (h->order >= MAX_ORDER)
  1087. return h->max_huge_pages;
  1088. /*
  1089. * Increase the pool size
  1090. * First take pages out of surplus state. Then make up the
  1091. * remaining difference by allocating fresh huge pages.
  1092. *
  1093. * We might race with alloc_buddy_huge_page() here and be unable
  1094. * to convert a surplus huge page to a normal huge page. That is
  1095. * not critical, though, it just means the overall size of the
  1096. * pool might be one hugepage larger than it needs to be, but
  1097. * within all the constraints specified by the sysctls.
  1098. */
  1099. spin_lock(&hugetlb_lock);
  1100. while (h->surplus_huge_pages && count > persistent_huge_pages(h)) {
  1101. if (!adjust_pool_surplus(h, nodes_allowed, -1))
  1102. break;
  1103. }
  1104. while (count > persistent_huge_pages(h)) {
  1105. /*
  1106. * If this allocation races such that we no longer need the
  1107. * page, free_huge_page will handle it by freeing the page
  1108. * and reducing the surplus.
  1109. */
  1110. spin_unlock(&hugetlb_lock);
  1111. ret = alloc_fresh_huge_page(h, nodes_allowed);
  1112. spin_lock(&hugetlb_lock);
  1113. if (!ret)
  1114. goto out;
  1115. /* Bail for signals. Probably ctrl-c from user */
  1116. if (signal_pending(current))
  1117. goto out;
  1118. }
  1119. /*
  1120. * Decrease the pool size
  1121. * First return free pages to the buddy allocator (being careful
  1122. * to keep enough around to satisfy reservations). Then place
  1123. * pages into surplus state as needed so the pool will shrink
  1124. * to the desired size as pages become free.
  1125. *
  1126. * By placing pages into the surplus state independent of the
  1127. * overcommit value, we are allowing the surplus pool size to
  1128. * exceed overcommit. There are few sane options here. Since
  1129. * alloc_buddy_huge_page() is checking the global counter,
  1130. * though, we'll note that we're not allowed to exceed surplus
  1131. * and won't grow the pool anywhere else. Not until one of the
  1132. * sysctls are changed, or the surplus pages go out of use.
  1133. */
  1134. min_count = h->resv_huge_pages + h->nr_huge_pages - h->free_huge_pages;
  1135. min_count = max(count, min_count);
  1136. try_to_free_low(h, min_count, nodes_allowed);
  1137. while (min_count < persistent_huge_pages(h)) {
  1138. if (!free_pool_huge_page(h, nodes_allowed, 0))
  1139. break;
  1140. }
  1141. while (count < persistent_huge_pages(h)) {
  1142. if (!adjust_pool_surplus(h, nodes_allowed, 1))
  1143. break;
  1144. }
  1145. out:
  1146. ret = persistent_huge_pages(h);
  1147. spin_unlock(&hugetlb_lock);
  1148. return ret;
  1149. }
  1150. #define HSTATE_ATTR_RO(_name) \
  1151. static struct kobj_attribute _name##_attr = __ATTR_RO(_name)
  1152. #define HSTATE_ATTR(_name) \
  1153. static struct kobj_attribute _name##_attr = \
  1154. __ATTR(_name, 0644, _name##_show, _name##_store)
  1155. static struct kobject *hugepages_kobj;
  1156. static struct kobject *hstate_kobjs[HUGE_MAX_HSTATE];
  1157. static struct hstate *kobj_to_node_hstate(struct kobject *kobj, int *nidp);
  1158. static struct hstate *kobj_to_hstate(struct kobject *kobj, int *nidp)
  1159. {
  1160. int i;
  1161. for (i = 0; i < HUGE_MAX_HSTATE; i++)
  1162. if (hstate_kobjs[i] == kobj) {
  1163. if (nidp)
  1164. *nidp = NUMA_NO_NODE;
  1165. return &hstates[i];
  1166. }
  1167. return kobj_to_node_hstate(kobj, nidp);
  1168. }
  1169. static ssize_t nr_hugepages_show_common(struct kobject *kobj,
  1170. struct kobj_attribute *attr, char *buf)
  1171. {
  1172. struct hstate *h;
  1173. unsigned long nr_huge_pages;
  1174. int nid;
  1175. h = kobj_to_hstate(kobj, &nid);
  1176. if (nid == NUMA_NO_NODE)
  1177. nr_huge_pages = h->nr_huge_pages;
  1178. else
  1179. nr_huge_pages = h->nr_huge_pages_node[nid];
  1180. return sprintf(buf, "%lu\n", nr_huge_pages);
  1181. }
  1182. static ssize_t nr_hugepages_store_common(bool obey_mempolicy,
  1183. struct kobject *kobj, struct kobj_attribute *attr,
  1184. const char *buf, size_t len)
  1185. {
  1186. int err;
  1187. int nid;
  1188. unsigned long count;
  1189. struct hstate *h;
  1190. NODEMASK_ALLOC(nodemask_t, nodes_allowed, GFP_KERNEL | __GFP_NORETRY);
  1191. err = strict_strtoul(buf, 10, &count);
  1192. if (err)
  1193. goto out;
  1194. h = kobj_to_hstate(kobj, &nid);
  1195. if (h->order >= MAX_ORDER) {
  1196. err = -EINVAL;
  1197. goto out;
  1198. }
  1199. if (nid == NUMA_NO_NODE) {
  1200. /*
  1201. * global hstate attribute
  1202. */
  1203. if (!(obey_mempolicy &&
  1204. init_nodemask_of_mempolicy(nodes_allowed))) {
  1205. NODEMASK_FREE(nodes_allowed);
  1206. nodes_allowed = &node_states[N_HIGH_MEMORY];
  1207. }
  1208. } else if (nodes_allowed) {
  1209. /*
  1210. * per node hstate attribute: adjust count to global,
  1211. * but restrict alloc/free to the specified node.
  1212. */
  1213. count += h->nr_huge_pages - h->nr_huge_pages_node[nid];
  1214. init_nodemask_of_node(nodes_allowed, nid);
  1215. } else
  1216. nodes_allowed = &node_states[N_HIGH_MEMORY];
  1217. h->max_huge_pages = set_max_huge_pages(h, count, nodes_allowed);
  1218. if (nodes_allowed != &node_states[N_HIGH_MEMORY])
  1219. NODEMASK_FREE(nodes_allowed);
  1220. return len;
  1221. out:
  1222. NODEMASK_FREE(nodes_allowed);
  1223. return err;
  1224. }
  1225. static ssize_t nr_hugepages_show(struct kobject *kobj,
  1226. struct kobj_attribute *attr, char *buf)
  1227. {
  1228. return nr_hugepages_show_common(kobj, attr, buf);
  1229. }
  1230. static ssize_t nr_hugepages_store(struct kobject *kobj,
  1231. struct kobj_attribute *attr, const char *buf, size_t len)
  1232. {
  1233. return nr_hugepages_store_common(false, kobj, attr, buf, len);
  1234. }
  1235. HSTATE_ATTR(nr_hugepages);
  1236. #ifdef CONFIG_NUMA
  1237. /*
  1238. * hstate attribute for optionally mempolicy-based constraint on persistent
  1239. * huge page alloc/free.
  1240. */
  1241. static ssize_t nr_hugepages_mempolicy_show(struct kobject *kobj,
  1242. struct kobj_attribute *attr, char *buf)
  1243. {
  1244. return nr_hugepages_show_common(kobj, attr, buf);
  1245. }
  1246. static ssize_t nr_hugepages_mempolicy_store(struct kobject *kobj,
  1247. struct kobj_attribute *attr, const char *buf, size_t len)
  1248. {
  1249. return nr_hugepages_store_common(true, kobj, attr, buf, len);
  1250. }
  1251. HSTATE_ATTR(nr_hugepages_mempolicy);
  1252. #endif
  1253. static ssize_t nr_overcommit_hugepages_show(struct kobject *kobj,
  1254. struct kobj_attribute *attr, char *buf)
  1255. {
  1256. struct hstate *h = kobj_to_hstate(kobj, NULL);
  1257. return sprintf(buf, "%lu\n", h->nr_overcommit_huge_pages);
  1258. }
  1259. static ssize_t nr_overcommit_hugepages_store(struct kobject *kobj,
  1260. struct kobj_attribute *attr, const char *buf, size_t count)
  1261. {
  1262. int err;
  1263. unsigned long input;
  1264. struct hstate *h = kobj_to_hstate(kobj, NULL);
  1265. if (h->order >= MAX_ORDER)
  1266. return -EINVAL;
  1267. err = strict_strtoul(buf, 10, &input);
  1268. if (err)
  1269. return err;
  1270. spin_lock(&hugetlb_lock);
  1271. h->nr_overcommit_huge_pages = input;
  1272. spin_unlock(&hugetlb_lock);
  1273. return count;
  1274. }
  1275. HSTATE_ATTR(nr_overcommit_hugepages);
  1276. static ssize_t free_hugepages_show(struct kobject *kobj,
  1277. struct kobj_attribute *attr, char *buf)
  1278. {
  1279. struct hstate *h;
  1280. unsigned long free_huge_pages;
  1281. int nid;
  1282. h = kobj_to_hstate(kobj, &nid);
  1283. if (nid == NUMA_NO_NODE)
  1284. free_huge_pages = h->free_huge_pages;
  1285. else
  1286. free_huge_pages = h->free_huge_pages_node[nid];
  1287. return sprintf(buf, "%lu\n", free_huge_pages);
  1288. }
  1289. HSTATE_ATTR_RO(free_hugepages);
  1290. static ssize_t resv_hugepages_show(struct kobject *kobj,
  1291. struct kobj_attribute *attr, char *buf)
  1292. {
  1293. struct hstate *h = kobj_to_hstate(kobj, NULL);
  1294. return sprintf(buf, "%lu\n", h->resv_huge_pages);
  1295. }
  1296. HSTATE_ATTR_RO(resv_hugepages);
  1297. static ssize_t surplus_hugepages_show(struct kobject *kobj,
  1298. struct kobj_attribute *attr, char *buf)
  1299. {
  1300. struct hstate *h;
  1301. unsigned long surplus_huge_pages;
  1302. int nid;
  1303. h = kobj_to_hstate(kobj, &nid);
  1304. if (nid == NUMA_NO_NODE)
  1305. surplus_huge_pages = h->surplus_huge_pages;
  1306. else
  1307. surplus_huge_pages = h->surplus_huge_pages_node[nid];
  1308. return sprintf(buf, "%lu\n", surplus_huge_pages);
  1309. }
  1310. HSTATE_ATTR_RO(surplus_hugepages);
  1311. static struct attribute *hstate_attrs[] = {
  1312. &nr_hugepages_attr.attr,
  1313. &nr_overcommit_hugepages_attr.attr,
  1314. &free_hugepages_attr.attr,
  1315. &resv_hugepages_attr.attr,
  1316. &surplus_hugepages_attr.attr,
  1317. #ifdef CONFIG_NUMA
  1318. &nr_hugepages_mempolicy_attr.attr,
  1319. #endif
  1320. NULL,
  1321. };
  1322. static struct attribute_group hstate_attr_group = {
  1323. .attrs = hstate_attrs,
  1324. };
  1325. static int hugetlb_sysfs_add_hstate(struct hstate *h, struct kobject *parent,
  1326. struct kobject **hstate_kobjs,
  1327. struct attribute_group *hstate_attr_group)
  1328. {
  1329. int retval;
  1330. int hi = h - hstates;
  1331. hstate_kobjs[hi] = kobject_create_and_add(h->name, parent);
  1332. if (!hstate_kobjs[hi])
  1333. return -ENOMEM;
  1334. retval = sysfs_create_group(hstate_kobjs[hi], hstate_attr_group);
  1335. if (retval)
  1336. kobject_put(hstate_kobjs[hi]);
  1337. return retval;
  1338. }
  1339. static void __init hugetlb_sysfs_init(void)
  1340. {
  1341. struct hstate *h;
  1342. int err;
  1343. hugepages_kobj = kobject_create_and_add("hugepages", mm_kobj);
  1344. if (!hugepages_kobj)
  1345. return;
  1346. for_each_hstate(h) {
  1347. err = hugetlb_sysfs_add_hstate(h, hugepages_kobj,
  1348. hstate_kobjs, &hstate_attr_group);
  1349. if (err)
  1350. printk(KERN_ERR "Hugetlb: Unable to add hstate %s",
  1351. h->name);
  1352. }
  1353. }
  1354. #ifdef CONFIG_NUMA
  1355. /*
  1356. * node_hstate/s - associate per node hstate attributes, via their kobjects,
  1357. * with node sysdevs in node_devices[] using a parallel array. The array
  1358. * index of a node sysdev or _hstate == node id.
  1359. * This is here to avoid any static dependency of the node sysdev driver, in
  1360. * the base kernel, on the hugetlb module.
  1361. */
  1362. struct node_hstate {
  1363. struct kobject *hugepages_kobj;
  1364. struct kobject *hstate_kobjs[HUGE_MAX_HSTATE];
  1365. };
  1366. struct node_hstate node_hstates[MAX_NUMNODES];
  1367. /*
  1368. * A subset of global hstate attributes for node sysdevs
  1369. */
  1370. static struct attribute *per_node_hstate_attrs[] = {
  1371. &nr_hugepages_attr.attr,
  1372. &free_hugepages_attr.attr,
  1373. &surplus_hugepages_attr.attr,
  1374. NULL,
  1375. };
  1376. static struct attribute_group per_node_hstate_attr_group = {
  1377. .attrs = per_node_hstate_attrs,
  1378. };
  1379. /*
  1380. * kobj_to_node_hstate - lookup global hstate for node sysdev hstate attr kobj.
  1381. * Returns node id via non-NULL nidp.
  1382. */
  1383. static struct hstate *kobj_to_node_hstate(struct kobject *kobj, int *nidp)
  1384. {
  1385. int nid;
  1386. for (nid = 0; nid < nr_node_ids; nid++) {
  1387. struct node_hstate *nhs = &node_hstates[nid];
  1388. int i;
  1389. for (i = 0; i < HUGE_MAX_HSTATE; i++)
  1390. if (nhs->hstate_kobjs[i] == kobj) {
  1391. if (nidp)
  1392. *nidp = nid;
  1393. return &hstates[i];
  1394. }
  1395. }
  1396. BUG();
  1397. return NULL;
  1398. }
  1399. /*
  1400. * Unregister hstate attributes from a single node sysdev.
  1401. * No-op if no hstate attributes attached.
  1402. */
  1403. void hugetlb_unregister_node(struct node *node)
  1404. {
  1405. struct hstate *h;
  1406. struct node_hstate *nhs = &node_hstates[node->sysdev.id];
  1407. if (!nhs->hugepages_kobj)
  1408. return; /* no hstate attributes */
  1409. for_each_hstate(h)
  1410. if (nhs->hstate_kobjs[h - hstates]) {
  1411. kobject_put(nhs->hstate_kobjs[h - hstates]);
  1412. nhs->hstate_kobjs[h - hstates] = NULL;
  1413. }
  1414. kobject_put(nhs->hugepages_kobj);
  1415. nhs->hugepages_kobj = NULL;
  1416. }
  1417. /*
  1418. * hugetlb module exit: unregister hstate attributes from node sysdevs
  1419. * that have them.
  1420. */
  1421. static void hugetlb_unregister_all_nodes(void)
  1422. {
  1423. int nid;
  1424. /*
  1425. * disable node sysdev registrations.
  1426. */
  1427. register_hugetlbfs_with_node(NULL, NULL);
  1428. /*
  1429. * remove hstate attributes from any nodes that have them.
  1430. */
  1431. for (nid = 0; nid < nr_node_ids; nid++)
  1432. hugetlb_unregister_node(&node_devices[nid]);
  1433. }
  1434. /*
  1435. * Register hstate attributes for a single node sysdev.
  1436. * No-op if attributes already registered.
  1437. */
  1438. void hugetlb_register_node(struct node *node)
  1439. {
  1440. struct hstate *h;
  1441. struct node_hstate *nhs = &node_hstates[node->sysdev.id];
  1442. int err;
  1443. if (nhs->hugepages_kobj)
  1444. return; /* already allocated */
  1445. nhs->hugepages_kobj = kobject_create_and_add("hugepages",
  1446. &node->sysdev.kobj);
  1447. if (!nhs->hugepages_kobj)
  1448. return;
  1449. for_each_hstate(h) {
  1450. err = hugetlb_sysfs_add_hstate(h, nhs->hugepages_kobj,
  1451. nhs->hstate_kobjs,
  1452. &per_node_hstate_attr_group);
  1453. if (err) {
  1454. printk(KERN_ERR "Hugetlb: Unable to add hstate %s"
  1455. " for node %d\n",
  1456. h->name, node->sysdev.id);
  1457. hugetlb_unregister_node(node);
  1458. break;
  1459. }
  1460. }
  1461. }
  1462. /*
  1463. * hugetlb init time: register hstate attributes for all registered node
  1464. * sysdevs of nodes that have memory. All on-line nodes should have
  1465. * registered their associated sysdev by this time.
  1466. */
  1467. static void hugetlb_register_all_nodes(void)
  1468. {
  1469. int nid;
  1470. for_each_node_state(nid, N_HIGH_MEMORY) {
  1471. struct node *node = &node_devices[nid];
  1472. if (node->sysdev.id == nid)
  1473. hugetlb_register_node(node);
  1474. }
  1475. /*
  1476. * Let the node sysdev driver know we're here so it can
  1477. * [un]register hstate attributes on node hotplug.
  1478. */
  1479. register_hugetlbfs_with_node(hugetlb_register_node,
  1480. hugetlb_unregister_node);
  1481. }
  1482. #else /* !CONFIG_NUMA */
  1483. static struct hstate *kobj_to_node_hstate(struct kobject *kobj, int *nidp)
  1484. {
  1485. BUG();
  1486. if (nidp)
  1487. *nidp = -1;
  1488. return NULL;
  1489. }
  1490. static void hugetlb_unregister_all_nodes(void) { }
  1491. static void hugetlb_register_all_nodes(void) { }
  1492. #endif
  1493. static void __exit hugetlb_exit(void)
  1494. {
  1495. struct hstate *h;
  1496. hugetlb_unregister_all_nodes();
  1497. for_each_hstate(h) {
  1498. kobject_put(hstate_kobjs[h - hstates]);
  1499. }
  1500. kobject_put(hugepages_kobj);
  1501. }
  1502. module_exit(hugetlb_exit);
  1503. static int __init hugetlb_init(void)
  1504. {
  1505. /* Some platform decide whether they support huge pages at boot
  1506. * time. On these, such as powerpc, HPAGE_SHIFT is set to 0 when
  1507. * there is no such support
  1508. */
  1509. if (HPAGE_SHIFT == 0)
  1510. return 0;
  1511. if (!size_to_hstate(default_hstate_size)) {
  1512. default_hstate_size = HPAGE_SIZE;
  1513. if (!size_to_hstate(default_hstate_size))
  1514. hugetlb_add_hstate(HUGETLB_PAGE_ORDER);
  1515. }
  1516. default_hstate_idx = size_to_hstate(default_hstate_size) - hstates;
  1517. if (default_hstate_max_huge_pages)
  1518. default_hstate.max_huge_pages = default_hstate_max_huge_pages;
  1519. hugetlb_init_hstates();
  1520. gather_bootmem_prealloc();
  1521. report_hugepages();
  1522. hugetlb_sysfs_init();
  1523. hugetlb_register_all_nodes();
  1524. return 0;
  1525. }
  1526. module_init(hugetlb_init);
  1527. /* Should be called on processing a hugepagesz=... option */
  1528. void __init hugetlb_add_hstate(unsigned order)
  1529. {
  1530. struct hstate *h;
  1531. unsigned long i;
  1532. if (size_to_hstate(PAGE_SIZE << order)) {
  1533. printk(KERN_WARNING "hugepagesz= specified twice, ignoring\n");
  1534. return;
  1535. }
  1536. BUG_ON(max_hstate >= HUGE_MAX_HSTATE);
  1537. BUG_ON(order == 0);
  1538. h = &hstates[max_hstate++];
  1539. h->order = order;
  1540. h->mask = ~((1ULL << (order + PAGE_SHIFT)) - 1);
  1541. h->nr_huge_pages = 0;
  1542. h->free_huge_pages = 0;
  1543. for (i = 0; i < MAX_NUMNODES; ++i)
  1544. INIT_LIST_HEAD(&h->hugepage_freelists[i]);
  1545. h->next_nid_to_alloc = first_node(node_states[N_HIGH_MEMORY]);
  1546. h->next_nid_to_free = first_node(node_states[N_HIGH_MEMORY]);
  1547. snprintf(h->name, HSTATE_NAME_LEN, "hugepages-%lukB",
  1548. huge_page_size(h)/1024);
  1549. parsed_hstate = h;
  1550. }
  1551. static int __init hugetlb_nrpages_setup(char *s)
  1552. {
  1553. unsigned long *mhp;
  1554. static unsigned long *last_mhp;
  1555. /*
  1556. * !max_hstate means we haven't parsed a hugepagesz= parameter yet,
  1557. * so this hugepages= parameter goes to the "default hstate".
  1558. */
  1559. if (!max_hstate)
  1560. mhp = &default_hstate_max_huge_pages;
  1561. else
  1562. mhp = &parsed_hstate->max_huge_pages;
  1563. if (mhp == last_mhp) {
  1564. printk(KERN_WARNING "hugepages= specified twice without "
  1565. "interleaving hugepagesz=, ignoring\n");
  1566. return 1;
  1567. }
  1568. if (sscanf(s, "%lu", mhp) <= 0)
  1569. *mhp = 0;
  1570. /*
  1571. * Global state is always initialized later in hugetlb_init.
  1572. * But we need to allocate >= MAX_ORDER hstates here early to still
  1573. * use the bootmem allocator.
  1574. */
  1575. if (max_hstate && parsed_hstate->order >= MAX_ORDER)
  1576. hugetlb_hstate_alloc_pages(parsed_hstate);
  1577. last_mhp = mhp;
  1578. return 1;
  1579. }
  1580. __setup("hugepages=", hugetlb_nrpages_setup);
  1581. static int __init hugetlb_default_setup(char *s)
  1582. {
  1583. default_hstate_size = memparse(s, &s);
  1584. return 1;
  1585. }
  1586. __setup("default_hugepagesz=", hugetlb_default_setup);
  1587. static unsigned int cpuset_mems_nr(unsigned int *array)
  1588. {
  1589. int node;
  1590. unsigned int nr = 0;
  1591. for_each_node_mask(node, cpuset_current_mems_allowed)
  1592. nr += array[node];
  1593. return nr;
  1594. }
  1595. #ifdef CONFIG_SYSCTL
  1596. static int hugetlb_sysctl_handler_common(bool obey_mempolicy,
  1597. struct ctl_table *table, int write,
  1598. void __user *buffer, size_t *length, loff_t *ppos)
  1599. {
  1600. struct hstate *h = &default_hstate;
  1601. unsigned long tmp;
  1602. int ret;
  1603. tmp = h->max_huge_pages;
  1604. if (write && h->order >= MAX_ORDER)
  1605. return -EINVAL;
  1606. table->data = &tmp;
  1607. table->maxlen = sizeof(unsigned long);
  1608. ret = proc_doulongvec_minmax(table, write, buffer, length, ppos);
  1609. if (ret)
  1610. goto out;
  1611. if (write) {
  1612. NODEMASK_ALLOC(nodemask_t, nodes_allowed,
  1613. GFP_KERNEL | __GFP_NORETRY);
  1614. if (!(obey_mempolicy &&
  1615. init_nodemask_of_mempolicy(nodes_allowed))) {
  1616. NODEMASK_FREE(nodes_allowed);
  1617. nodes_allowed = &node_states[N_HIGH_MEMORY];
  1618. }
  1619. h->max_huge_pages = set_max_huge_pages(h, tmp, nodes_allowed);
  1620. if (nodes_allowed != &node_states[N_HIGH_MEMORY])
  1621. NODEMASK_FREE(nodes_allowed);
  1622. }
  1623. out:
  1624. return ret;
  1625. }
  1626. int hugetlb_sysctl_handler(struct ctl_table *table, int write,
  1627. void __user *buffer, size_t *length, loff_t *ppos)
  1628. {
  1629. return hugetlb_sysctl_handler_common(false, table, write,
  1630. buffer, length, ppos);
  1631. }
  1632. #ifdef CONFIG_NUMA
  1633. int hugetlb_mempolicy_sysctl_handler(struct ctl_table *table, int write,
  1634. void __user *buffer, size_t *length, loff_t *ppos)
  1635. {
  1636. return hugetlb_sysctl_handler_common(true, table, write,
  1637. buffer, length, ppos);
  1638. }
  1639. #endif /* CONFIG_NUMA */
  1640. int hugetlb_treat_movable_handler(struct ctl_table *table, int write,
  1641. void __user *buffer,
  1642. size_t *length, loff_t *ppos)
  1643. {
  1644. proc_dointvec(table, write, buffer, length, ppos);
  1645. if (hugepages_treat_as_movable)
  1646. htlb_alloc_mask = GFP_HIGHUSER_MOVABLE;
  1647. else
  1648. htlb_alloc_mask = GFP_HIGHUSER;
  1649. return 0;
  1650. }
  1651. int hugetlb_overcommit_handler(struct ctl_table *table, int write,
  1652. void __user *buffer,
  1653. size_t *length, loff_t *ppos)
  1654. {
  1655. struct hstate *h = &default_hstate;
  1656. unsigned long tmp;
  1657. int ret;
  1658. tmp = h->nr_overcommit_huge_pages;
  1659. if (write && h->order >= MAX_ORDER)
  1660. return -EINVAL;
  1661. table->data = &tmp;
  1662. table->maxlen = sizeof(unsigned long);
  1663. ret = proc_doulongvec_minmax(table, write, buffer, length, ppos);
  1664. if (ret)
  1665. goto out;
  1666. if (write) {
  1667. spin_lock(&hugetlb_lock);
  1668. h->nr_overcommit_huge_pages = tmp;
  1669. spin_unlock(&hugetlb_lock);
  1670. }
  1671. out:
  1672. return ret;
  1673. }
  1674. #endif /* CONFIG_SYSCTL */
  1675. void hugetlb_report_meminfo(struct seq_file *m)
  1676. {
  1677. struct hstate *h = &default_hstate;
  1678. seq_printf(m,
  1679. "HugePages_Total: %5lu\n"
  1680. "HugePages_Free: %5lu\n"
  1681. "HugePages_Rsvd: %5lu\n"
  1682. "HugePages_Surp: %5lu\n"
  1683. "Hugepagesize: %8lu kB\n",
  1684. h->nr_huge_pages,
  1685. h->free_huge_pages,
  1686. h->resv_huge_pages,
  1687. h->surplus_huge_pages,
  1688. 1UL << (huge_page_order(h) + PAGE_SHIFT - 10));
  1689. }
  1690. int hugetlb_report_node_meminfo(int nid, char *buf)
  1691. {
  1692. struct hstate *h = &default_hstate;
  1693. return sprintf(buf,
  1694. "Node %d HugePages_Total: %5u\n"
  1695. "Node %d HugePages_Free: %5u\n"
  1696. "Node %d HugePages_Surp: %5u\n",
  1697. nid, h->nr_huge_pages_node[nid],
  1698. nid, h->free_huge_pages_node[nid],
  1699. nid, h->surplus_huge_pages_node[nid]);
  1700. }
  1701. /* Return the number pages of memory we physically have, in PAGE_SIZE units. */
  1702. unsigned long hugetlb_total_pages(void)
  1703. {
  1704. struct hstate *h = &default_hstate;
  1705. return h->nr_huge_pages * pages_per_huge_page(h);
  1706. }
  1707. static int hugetlb_acct_memory(struct hstate *h, long delta)
  1708. {
  1709. int ret = -ENOMEM;
  1710. spin_lock(&hugetlb_lock);
  1711. /*
  1712. * When cpuset is configured, it breaks the strict hugetlb page
  1713. * reservation as the accounting is done on a global variable. Such
  1714. * reservation is completely rubbish in the presence of cpuset because
  1715. * the reservation is not checked against page availability for the
  1716. * current cpuset. Application can still potentially OOM'ed by kernel
  1717. * with lack of free htlb page in cpuset that the task is in.
  1718. * Attempt to enforce strict accounting with cpuset is almost
  1719. * impossible (or too ugly) because cpuset is too fluid that
  1720. * task or memory node can be dynamically moved between cpusets.
  1721. *
  1722. * The change of semantics for shared hugetlb mapping with cpuset is
  1723. * undesirable. However, in order to preserve some of the semantics,
  1724. * we fall back to check against current free page availability as
  1725. * a best attempt and hopefully to minimize the impact of changing
  1726. * semantics that cpuset has.
  1727. */
  1728. if (delta > 0) {
  1729. if (gather_surplus_pages(h, delta) < 0)
  1730. goto out;
  1731. if (delta > cpuset_mems_nr(h->free_huge_pages_node)) {
  1732. return_unused_surplus_pages(h, delta);
  1733. goto out;
  1734. }
  1735. }
  1736. ret = 0;
  1737. if (delta < 0)
  1738. return_unused_surplus_pages(h, (unsigned long) -delta);
  1739. out:
  1740. spin_unlock(&hugetlb_lock);
  1741. return ret;
  1742. }
  1743. static void hugetlb_vm_op_open(struct vm_area_struct *vma)
  1744. {
  1745. struct resv_map *reservations = vma_resv_map(vma);
  1746. /*
  1747. * This new VMA should share its siblings reservation map if present.
  1748. * The VMA will only ever have a valid reservation map pointer where
  1749. * it is being copied for another still existing VMA. As that VMA
  1750. * has a reference to the reservation map it cannot disappear until
  1751. * after this open call completes. It is therefore safe to take a
  1752. * new reference here without additional locking.
  1753. */
  1754. if (reservations)
  1755. kref_get(&reservations->refs);
  1756. }
  1757. static void hugetlb_vm_op_close(struct vm_area_struct *vma)
  1758. {
  1759. struct hstate *h = hstate_vma(vma);
  1760. struct resv_map *reservations = vma_resv_map(vma);
  1761. unsigned long reserve;
  1762. unsigned long start;
  1763. unsigned long end;
  1764. if (reservations) {
  1765. start = vma_hugecache_offset(h, vma, vma->vm_start);
  1766. end = vma_hugecache_offset(h, vma, vma->vm_end);
  1767. reserve = (end - start) -
  1768. region_count(&reservations->regions, start, end);
  1769. kref_put(&reservations->refs, resv_map_release);
  1770. if (reserve) {
  1771. hugetlb_acct_memory(h, -reserve);
  1772. hugetlb_put_quota(vma->vm_file->f_mapping, reserve);
  1773. }
  1774. }
  1775. }
  1776. /*
  1777. * We cannot handle pagefaults against hugetlb pages at all. They cause
  1778. * handle_mm_fault() to try to instantiate regular-sized pages in the
  1779. * hugegpage VMA. do_page_fault() is supposed to trap this, so BUG is we get
  1780. * this far.
  1781. */
  1782. static int hugetlb_vm_op_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
  1783. {
  1784. BUG();
  1785. return 0;
  1786. }
  1787. const struct vm_operations_struct hugetlb_vm_ops = {
  1788. .fault = hugetlb_vm_op_fault,
  1789. .open = hugetlb_vm_op_open,
  1790. .close = hugetlb_vm_op_close,
  1791. };
  1792. static pte_t make_huge_pte(struct vm_area_struct *vma, struct page *page,
  1793. int writable)
  1794. {
  1795. pte_t entry;
  1796. if (writable) {
  1797. entry =
  1798. pte_mkwrite(pte_mkdirty(mk_pte(page, vma->vm_page_prot)));
  1799. } else {
  1800. entry = huge_pte_wrprotect(mk_pte(page, vma->vm_page_prot));
  1801. }
  1802. entry = pte_mkyoung(entry);
  1803. entry = pte_mkhuge(entry);
  1804. return entry;
  1805. }
  1806. static void set_huge_ptep_writable(struct vm_area_struct *vma,
  1807. unsigned long address, pte_t *ptep)
  1808. {
  1809. pte_t entry;
  1810. entry = pte_mkwrite(pte_mkdirty(huge_ptep_get(ptep)));
  1811. if (huge_ptep_set_access_flags(vma, address, ptep, entry, 1)) {
  1812. update_mmu_cache(vma, address, ptep);
  1813. }
  1814. }
  1815. int copy_hugetlb_page_range(struct mm_struct *dst, struct mm_struct *src,
  1816. struct vm_area_struct *vma)
  1817. {
  1818. pte_t *src_pte, *dst_pte, entry;
  1819. struct page *ptepage;
  1820. unsigned long addr;
  1821. int cow;
  1822. struct hstate *h = hstate_vma(vma);
  1823. unsigned long sz = huge_page_size(h);
  1824. cow = (vma->vm_flags & (VM_SHARED | VM_MAYWRITE)) == VM_MAYWRITE;
  1825. for (addr = vma->vm_start; addr < vma->vm_end; addr += sz) {
  1826. src_pte = huge_pte_offset(src, addr);
  1827. if (!src_pte)
  1828. continue;
  1829. dst_pte = huge_pte_alloc(dst, addr, sz);
  1830. if (!dst_pte)
  1831. goto nomem;
  1832. /* If the pagetables are shared don't copy or take references */
  1833. if (dst_pte == src_pte)
  1834. continue;
  1835. spin_lock(&dst->page_table_lock);
  1836. spin_lock_nested(&src->page_table_lock, SINGLE_DEPTH_NESTING);
  1837. if (!huge_pte_none(huge_ptep_get(src_pte))) {
  1838. if (cow)
  1839. huge_ptep_set_wrprotect(src, addr, src_pte);
  1840. entry = huge_ptep_get(src_pte);
  1841. ptepage = pte_page(entry);
  1842. get_page(ptepage);
  1843. page_dup_rmap(ptepage);
  1844. set_huge_pte_at(dst, addr, dst_pte, entry);
  1845. }
  1846. spin_unlock(&src->page_table_lock);
  1847. spin_unlock(&dst->page_table_lock);
  1848. }
  1849. return 0;
  1850. nomem:
  1851. return -ENOMEM;
  1852. }
  1853. static int is_hugetlb_entry_migration(pte_t pte)
  1854. {
  1855. swp_entry_t swp;
  1856. if (huge_pte_none(pte) || pte_present(pte))
  1857. return 0;
  1858. swp = pte_to_swp_entry(pte);
  1859. if (non_swap_entry(swp) && is_migration_entry(swp)) {
  1860. return 1;
  1861. } else
  1862. return 0;
  1863. }
  1864. static int is_hugetlb_entry_hwpoisoned(pte_t pte)
  1865. {
  1866. swp_entry_t swp;
  1867. if (huge_pte_none(pte) || pte_present(pte))
  1868. return 0;
  1869. swp = pte_to_swp_entry(pte);
  1870. if (non_swap_entry(swp) && is_hwpoison_entry(swp)) {
  1871. return 1;
  1872. } else
  1873. return 0;
  1874. }
  1875. void __unmap_hugepage_range(struct vm_area_struct *vma, unsigned long start,
  1876. unsigned long end, struct page *ref_page)
  1877. {
  1878. struct mm_struct *mm = vma->vm_mm;
  1879. unsigned long address;
  1880. pte_t *ptep;
  1881. pte_t pte;
  1882. struct page *page;
  1883. struct page *tmp;
  1884. struct hstate *h = hstate_vma(vma);
  1885. unsigned long sz = huge_page_size(h);
  1886. /*
  1887. * A page gathering list, protected by per file i_mmap_mutex. The
  1888. * lock is used to avoid list corruption from multiple unmapping
  1889. * of the same page since we are using page->lru.
  1890. */
  1891. LIST_HEAD(page_list);
  1892. WARN_ON(!is_vm_hugetlb_page(vma));
  1893. BUG_ON(start & ~huge_page_mask(h));
  1894. BUG_ON(end & ~huge_page_mask(h));
  1895. mmu_notifier_invalidate_range_start(mm, start, end);
  1896. spin_lock(&mm->page_table_lock);
  1897. for (address = start; address < end; address += sz) {
  1898. ptep = huge_pte_offset(mm, address);
  1899. if (!ptep)
  1900. continue;
  1901. if (huge_pmd_unshare(mm, &address, ptep))
  1902. continue;
  1903. /*
  1904. * If a reference page is supplied, it is because a specific
  1905. * page is being unmapped, not a range. Ensure the page we
  1906. * are about to unmap is the actual page of interest.
  1907. */
  1908. if (ref_page) {
  1909. pte = huge_ptep_get(ptep);
  1910. if (huge_pte_none(pte))
  1911. continue;
  1912. page = pte_page(pte);
  1913. if (page != ref_page)
  1914. continue;
  1915. /*
  1916. * Mark the VMA as having unmapped its page so that
  1917. * future faults in this VMA will fail rather than
  1918. * looking like data was lost
  1919. */
  1920. set_vma_resv_flags(vma, HPAGE_RESV_UNMAPPED);
  1921. }
  1922. pte = huge_ptep_get_and_clear(mm, address, ptep);
  1923. if (huge_pte_none(pte))
  1924. continue;
  1925. /*
  1926. * HWPoisoned hugepage is already unmapped and dropped reference
  1927. */
  1928. if (unlikely(is_hugetlb_entry_hwpoisoned(pte)))
  1929. continue;
  1930. page = pte_page(pte);
  1931. if (pte_dirty(pte))
  1932. set_page_dirty(page);
  1933. list_add(&page->lru, &page_list);
  1934. }
  1935. spin_unlock(&mm->page_table_lock);
  1936. flush_tlb_range(vma, start, end);
  1937. mmu_notifier_invalidate_range_end(mm, start, end);
  1938. list_for_each_entry_safe(page, tmp, &page_list, lru) {
  1939. page_remove_rmap(page);
  1940. list_del(&page->lru);
  1941. put_page(page);
  1942. }
  1943. }
  1944. void unmap_hugepage_range(struct vm_area_struct *vma, unsigned long start,
  1945. unsigned long end, struct page *ref_page)
  1946. {
  1947. mutex_lock(&vma->vm_file->f_mapping->i_mmap_mutex);
  1948. __unmap_hugepage_range(vma, start, end, ref_page);
  1949. mutex_unlock(&vma->vm_file->f_mapping->i_mmap_mutex);
  1950. }
  1951. /*
  1952. * This is called when the original mapper is failing to COW a MAP_PRIVATE
  1953. * mappping it owns the reserve page for. The intention is to unmap the page
  1954. * from other VMAs and let the children be SIGKILLed if they are faulting the
  1955. * same region.
  1956. */
  1957. static int unmap_ref_private(struct mm_struct *mm, struct vm_area_struct *vma,
  1958. struct page *page, unsigned long address)
  1959. {
  1960. struct hstate *h = hstate_vma(vma);
  1961. struct vm_area_struct *iter_vma;
  1962. struct address_space *mapping;
  1963. struct prio_tree_iter iter;
  1964. pgoff_t pgoff;
  1965. /*
  1966. * vm_pgoff is in PAGE_SIZE units, hence the different calculation
  1967. * from page cache lookup which is in HPAGE_SIZE units.
  1968. */
  1969. address = address & huge_page_mask(h);
  1970. pgoff = ((address - vma->vm_start) >> PAGE_SHIFT)
  1971. + (vma->vm_pgoff >> PAGE_SHIFT);
  1972. mapping = (struct address_space *)page_private(page);
  1973. /*
  1974. * Take the mapping lock for the duration of the table walk. As
  1975. * this mapping should be shared between all the VMAs,
  1976. * __unmap_hugepage_range() is called as the lock is already held
  1977. */
  1978. mutex_lock(&mapping->i_mmap_mutex);
  1979. vma_prio_tree_foreach(iter_vma, &iter, &mapping->i_mmap, pgoff, pgoff) {
  1980. /* Do not unmap the current VMA */
  1981. if (iter_vma == vma)
  1982. continue;
  1983. /*
  1984. * Unmap the page from other VMAs without their own reserves.
  1985. * They get marked to be SIGKILLed if they fault in these
  1986. * areas. This is because a future no-page fault on this VMA
  1987. * could insert a zeroed page instead of the data existing
  1988. * from the time of fork. This would look like data corruption
  1989. */
  1990. if (!is_vma_resv_set(iter_vma, HPAGE_RESV_OWNER))
  1991. __unmap_hugepage_range(iter_vma,
  1992. address, address + huge_page_size(h),
  1993. page);
  1994. }
  1995. mutex_unlock(&mapping->i_mmap_mutex);
  1996. return 1;
  1997. }
  1998. /*
  1999. * Hugetlb_cow() should be called with page lock of the original hugepage held.
  2000. */
  2001. static int hugetlb_cow(struct mm_struct *mm, struct vm_area_struct *vma,
  2002. unsigned long address, pte_t *ptep, pte_t pte,
  2003. struct page *pagecache_page)
  2004. {
  2005. struct hstate *h = hstate_vma(vma);
  2006. struct page *old_page, *new_page;
  2007. int avoidcopy;
  2008. int outside_reserve = 0;
  2009. old_page = pte_page(pte);
  2010. retry_avoidcopy:
  2011. /* If no-one else is actually using this page, avoid the copy
  2012. * and just make the page writable */
  2013. avoidcopy = (page_mapcount(old_page) == 1);
  2014. if (avoidcopy) {
  2015. if (PageAnon(old_page))
  2016. page_move_anon_rmap(old_page, vma, address);
  2017. set_huge_ptep_writable(vma, address, ptep);
  2018. return 0;
  2019. }
  2020. /*
  2021. * If the process that created a MAP_PRIVATE mapping is about to
  2022. * perform a COW due to a shared page count, attempt to satisfy
  2023. * the allocation without using the existing reserves. The pagecache
  2024. * page is used to determine if the reserve at this address was
  2025. * consumed or not. If reserves were used, a partial faulted mapping
  2026. * at the time of fork() could consume its reserves on COW instead
  2027. * of the full address range.
  2028. */
  2029. if (!(vma->vm_flags & VM_MAYSHARE) &&
  2030. is_vma_resv_set(vma, HPAGE_RESV_OWNER) &&
  2031. old_page != pagecache_page)
  2032. outside_reserve = 1;
  2033. page_cache_get(old_page);
  2034. /* Drop page_table_lock as buddy allocator may be called */
  2035. spin_unlock(&mm->page_table_lock);
  2036. new_page = alloc_huge_page(vma, address, outside_reserve);
  2037. if (IS_ERR(new_page)) {
  2038. page_cache_release(old_page);
  2039. /*
  2040. * If a process owning a MAP_PRIVATE mapping fails to COW,
  2041. * it is due to references held by a child and an insufficient
  2042. * huge page pool. To guarantee the original mappers
  2043. * reliability, unmap the page from child processes. The child
  2044. * may get SIGKILLed if it later faults.
  2045. */
  2046. if (outside_reserve) {
  2047. BUG_ON(huge_pte_none(pte));
  2048. if (unmap_ref_private(mm, vma, old_page, address)) {
  2049. BUG_ON(page_count(old_page) != 1);
  2050. BUG_ON(huge_pte_none(pte));
  2051. spin_lock(&mm->page_table_lock);
  2052. goto retry_avoidcopy;
  2053. }
  2054. WARN_ON_ONCE(1);
  2055. }
  2056. /* Caller expects lock to be held */
  2057. spin_lock(&mm->page_table_lock);
  2058. return -PTR_ERR(new_page);
  2059. }
  2060. /*
  2061. * When the original hugepage is shared one, it does not have
  2062. * anon_vma prepared.
  2063. */
  2064. if (unlikely(anon_vma_prepare(vma))) {
  2065. /* Caller expects lock to be held */
  2066. spin_lock(&mm->page_table_lock);
  2067. return VM_FAULT_OOM;
  2068. }
  2069. copy_user_huge_page(new_page, old_page, address, vma,
  2070. pages_per_huge_page(h));
  2071. __SetPageUptodate(new_page);
  2072. /*
  2073. * Retake the page_table_lock to check for racing updates
  2074. * before the page tables are altered
  2075. */
  2076. spin_lock(&mm->page_table_lock);
  2077. ptep = huge_pte_offset(mm, address & huge_page_mask(h));
  2078. if (likely(pte_same(huge_ptep_get(ptep), pte))) {
  2079. /* Break COW */
  2080. mmu_notifier_invalidate_range_start(mm,
  2081. address & huge_page_mask(h),
  2082. (address & huge_page_mask(h)) + huge_page_size(h));
  2083. huge_ptep_clear_flush(vma, address, ptep);
  2084. set_huge_pte_at(mm, address, ptep,
  2085. make_huge_pte(vma, new_page, 1));
  2086. page_remove_rmap(old_page);
  2087. hugepage_add_new_anon_rmap(new_page, vma, address);
  2088. /* Make the old page be freed below */
  2089. new_page = old_page;
  2090. mmu_notifier_invalidate_range_end(mm,
  2091. address & huge_page_mask(h),
  2092. (address & huge_page_mask(h)) + huge_page_size(h));
  2093. }
  2094. page_cache_release(new_page);
  2095. page_cache_release(old_page);
  2096. return 0;
  2097. }
  2098. /* Return the pagecache page at a given address within a VMA */
  2099. static struct page *hugetlbfs_pagecache_page(struct hstate *h,
  2100. struct vm_area_struct *vma, unsigned long address)
  2101. {
  2102. struct address_space *mapping;
  2103. pgoff_t idx;
  2104. mapping = vma->vm_file->f_mapping;
  2105. idx = vma_hugecache_offset(h, vma, address);
  2106. return find_lock_page(mapping, idx);
  2107. }
  2108. /*
  2109. * Return whether there is a pagecache page to back given address within VMA.
  2110. * Caller follow_hugetlb_page() holds page_table_lock so we cannot lock_page.
  2111. */
  2112. static bool hugetlbfs_pagecache_present(struct hstate *h,
  2113. struct vm_area_struct *vma, unsigned long address)
  2114. {
  2115. struct address_space *mapping;
  2116. pgoff_t idx;
  2117. struct page *page;
  2118. mapping = vma->vm_file->f_mapping;
  2119. idx = vma_hugecache_offset(h, vma, address);
  2120. page = find_get_page(mapping, idx);
  2121. if (page)
  2122. put_page(page);
  2123. return page != NULL;
  2124. }
  2125. static int hugetlb_no_page(struct mm_struct *mm, struct vm_area_struct *vma,
  2126. unsigned long address, pte_t *ptep, unsigned int flags)
  2127. {
  2128. struct hstate *h = hstate_vma(vma);
  2129. int ret = VM_FAULT_SIGBUS;
  2130. pgoff_t idx;
  2131. unsigned long size;
  2132. struct page *page;
  2133. struct address_space *mapping;
  2134. pte_t new_pte;
  2135. /*
  2136. * Currently, we are forced to kill the process in the event the
  2137. * original mapper has unmapped pages from the child due to a failed
  2138. * COW. Warn that such a situation has occurred as it may not be obvious
  2139. */
  2140. if (is_vma_resv_set(vma, HPAGE_RESV_UNMAPPED)) {
  2141. printk(KERN_WARNING
  2142. "PID %d killed due to inadequate hugepage pool\n",
  2143. current->pid);
  2144. return ret;
  2145. }
  2146. mapping = vma->vm_file->f_mapping;
  2147. idx = vma_hugecache_offset(h, vma, address);
  2148. /*
  2149. * Use page lock to guard against racing truncation
  2150. * before we get page_table_lock.
  2151. */
  2152. retry:
  2153. page = find_lock_page(mapping, idx);
  2154. if (!page) {
  2155. size = i_size_read(mapping->host) >> huge_page_shift(h);
  2156. if (idx >= size)
  2157. goto out;
  2158. page = alloc_huge_page(vma, address, 0);
  2159. if (IS_ERR(page)) {
  2160. ret = -PTR_ERR(page);
  2161. goto out;
  2162. }
  2163. clear_huge_page(page, address, pages_per_huge_page(h));
  2164. __SetPageUptodate(page);
  2165. if (vma->vm_flags & VM_MAYSHARE) {
  2166. int err;
  2167. struct inode *inode = mapping->host;
  2168. err = add_to_page_cache(page, mapping, idx, GFP_KERNEL);
  2169. if (err) {
  2170. put_page(page);
  2171. if (err == -EEXIST)
  2172. goto retry;
  2173. goto out;
  2174. }
  2175. spin_lock(&inode->i_lock);
  2176. inode->i_blocks += blocks_per_huge_page(h);
  2177. spin_unlock(&inode->i_lock);
  2178. page_dup_rmap(page);
  2179. } else {
  2180. lock_page(page);
  2181. if (unlikely(anon_vma_prepare(vma))) {
  2182. ret = VM_FAULT_OOM;
  2183. goto backout_unlocked;
  2184. }
  2185. hugepage_add_new_anon_rmap(page, vma, address);
  2186. }
  2187. } else {
  2188. /*
  2189. * If memory error occurs between mmap() and fault, some process
  2190. * don't have hwpoisoned swap entry for errored virtual address.
  2191. * So we need to block hugepage fault by PG_hwpoison bit check.
  2192. */
  2193. if (unlikely(PageHWPoison(page))) {
  2194. ret = VM_FAULT_HWPOISON |
  2195. VM_FAULT_SET_HINDEX(h - hstates);
  2196. goto backout_unlocked;
  2197. }
  2198. page_dup_rmap(page);
  2199. }
  2200. /*
  2201. * If we are going to COW a private mapping later, we examine the
  2202. * pending reservations for this page now. This will ensure that
  2203. * any allocations necessary to record that reservation occur outside
  2204. * the spinlock.
  2205. */
  2206. if ((flags & FAULT_FLAG_WRITE) && !(vma->vm_flags & VM_SHARED))
  2207. if (vma_needs_reservation(h, vma, address) < 0) {
  2208. ret = VM_FAULT_OOM;
  2209. goto backout_unlocked;
  2210. }
  2211. spin_lock(&mm->page_table_lock);
  2212. size = i_size_read(mapping->host) >> huge_page_shift(h);
  2213. if (idx >= size)
  2214. goto backout;
  2215. ret = 0;
  2216. if (!huge_pte_none(huge_ptep_get(ptep)))
  2217. goto backout;
  2218. new_pte = make_huge_pte(vma, page, ((vma->vm_flags & VM_WRITE)
  2219. && (vma->vm_flags & VM_SHARED)));
  2220. set_huge_pte_at(mm, address, ptep, new_pte);
  2221. if ((flags & FAULT_FLAG_WRITE) && !(vma->vm_flags & VM_SHARED)) {
  2222. /* Optimization, do the COW without a second fault */
  2223. ret = hugetlb_cow(mm, vma, address, ptep, new_pte, page);
  2224. }
  2225. spin_unlock(&mm->page_table_lock);
  2226. unlock_page(page);
  2227. out:
  2228. return ret;
  2229. backout:
  2230. spin_unlock(&mm->page_table_lock);
  2231. backout_unlocked:
  2232. unlock_page(page);
  2233. put_page(page);
  2234. goto out;
  2235. }
  2236. int hugetlb_fault(struct mm_struct *mm, struct vm_area_struct *vma,
  2237. unsigned long address, unsigned int flags)
  2238. {
  2239. pte_t *ptep;
  2240. pte_t entry;
  2241. int ret;
  2242. struct page *page = NULL;
  2243. struct page *pagecache_page = NULL;
  2244. static DEFINE_MUTEX(hugetlb_instantiation_mutex);
  2245. struct hstate *h = hstate_vma(vma);
  2246. ptep = huge_pte_offset(mm, address);
  2247. if (ptep) {
  2248. entry = huge_ptep_get(ptep);
  2249. if (unlikely(is_hugetlb_entry_migration(entry))) {
  2250. migration_entry_wait(mm, (pmd_t *)ptep, address);
  2251. return 0;
  2252. } else if (unlikely(is_hugetlb_entry_hwpoisoned(entry)))
  2253. return VM_FAULT_HWPOISON_LARGE |
  2254. VM_FAULT_SET_HINDEX(h - hstates);
  2255. }
  2256. ptep = huge_pte_alloc(mm, address, huge_page_size(h));
  2257. if (!ptep)
  2258. return VM_FAULT_OOM;
  2259. /*
  2260. * Serialize hugepage allocation and instantiation, so that we don't
  2261. * get spurious allocation failures if two CPUs race to instantiate
  2262. * the same page in the page cache.
  2263. */
  2264. mutex_lock(&hugetlb_instantiation_mutex);
  2265. entry = huge_ptep_get(ptep);
  2266. if (huge_pte_none(entry)) {
  2267. ret = hugetlb_no_page(mm, vma, address, ptep, flags);
  2268. goto out_mutex;
  2269. }
  2270. ret = 0;
  2271. /*
  2272. * If we are going to COW the mapping later, we examine the pending
  2273. * reservations for this page now. This will ensure that any
  2274. * allocations necessary to record that reservation occur outside the
  2275. * spinlock. For private mappings, we also lookup the pagecache
  2276. * page now as it is used to determine if a reservation has been
  2277. * consumed.
  2278. */
  2279. if ((flags & FAULT_FLAG_WRITE) && !pte_write(entry)) {
  2280. if (vma_needs_reservation(h, vma, address) < 0) {
  2281. ret = VM_FAULT_OOM;
  2282. goto out_mutex;
  2283. }
  2284. if (!(vma->vm_flags & VM_MAYSHARE))
  2285. pagecache_page = hugetlbfs_pagecache_page(h,
  2286. vma, address);
  2287. }
  2288. /*
  2289. * hugetlb_cow() requires page locks of pte_page(entry) and
  2290. * pagecache_page, so here we need take the former one
  2291. * when page != pagecache_page or !pagecache_page.
  2292. * Note that locking order is always pagecache_page -> page,
  2293. * so no worry about deadlock.
  2294. */
  2295. page = pte_page(entry);
  2296. if (page != pagecache_page)
  2297. lock_page(page);
  2298. spin_lock(&mm->page_table_lock);
  2299. /* Check for a racing update before calling hugetlb_cow */
  2300. if (unlikely(!pte_same(entry, huge_ptep_get(ptep))))
  2301. goto out_page_table_lock;
  2302. if (flags & FAULT_FLAG_WRITE) {
  2303. if (!pte_write(entry)) {
  2304. ret = hugetlb_cow(mm, vma, address, ptep, entry,
  2305. pagecache_page);
  2306. goto out_page_table_lock;
  2307. }
  2308. entry = pte_mkdirty(entry);
  2309. }
  2310. entry = pte_mkyoung(entry);
  2311. if (huge_ptep_set_access_flags(vma, address, ptep, entry,
  2312. flags & FAULT_FLAG_WRITE))
  2313. update_mmu_cache(vma, address, ptep);
  2314. out_page_table_lock:
  2315. spin_unlock(&mm->page_table_lock);
  2316. if (pagecache_page) {
  2317. unlock_page(pagecache_page);
  2318. put_page(pagecache_page);
  2319. }
  2320. if (page != pagecache_page)
  2321. unlock_page(page);
  2322. out_mutex:
  2323. mutex_unlock(&hugetlb_instantiation_mutex);
  2324. return ret;
  2325. }
  2326. /* Can be overriden by architectures */
  2327. __attribute__((weak)) struct page *
  2328. follow_huge_pud(struct mm_struct *mm, unsigned long address,
  2329. pud_t *pud, int write)
  2330. {
  2331. BUG();
  2332. return NULL;
  2333. }
  2334. int follow_hugetlb_page(struct mm_struct *mm, struct vm_area_struct *vma,
  2335. struct page **pages, struct vm_area_struct **vmas,
  2336. unsigned long *position, int *length, int i,
  2337. unsigned int flags)
  2338. {
  2339. unsigned long pfn_offset;
  2340. unsigned long vaddr = *position;
  2341. int remainder = *length;
  2342. struct hstate *h = hstate_vma(vma);
  2343. spin_lock(&mm->page_table_lock);
  2344. while (vaddr < vma->vm_end && remainder) {
  2345. pte_t *pte;
  2346. int absent;
  2347. struct page *page;
  2348. /*
  2349. * Some archs (sparc64, sh*) have multiple pte_ts to
  2350. * each hugepage. We have to make sure we get the
  2351. * first, for the page indexing below to work.
  2352. */
  2353. pte = huge_pte_offset(mm, vaddr & huge_page_mask(h));
  2354. absent = !pte || huge_pte_none(huge_ptep_get(pte));
  2355. /*
  2356. * When coredumping, it suits get_dump_page if we just return
  2357. * an error where there's an empty slot with no huge pagecache
  2358. * to back it. This way, we avoid allocating a hugepage, and
  2359. * the sparse dumpfile avoids allocating disk blocks, but its
  2360. * huge holes still show up with zeroes where they need to be.
  2361. */
  2362. if (absent && (flags & FOLL_DUMP) &&
  2363. !hugetlbfs_pagecache_present(h, vma, vaddr)) {
  2364. remainder = 0;
  2365. break;
  2366. }
  2367. if (absent ||
  2368. ((flags & FOLL_WRITE) && !pte_write(huge_ptep_get(pte)))) {
  2369. int ret;
  2370. spin_unlock(&mm->page_table_lock);
  2371. ret = hugetlb_fault(mm, vma, vaddr,
  2372. (flags & FOLL_WRITE) ? FAULT_FLAG_WRITE : 0);
  2373. spin_lock(&mm->page_table_lock);
  2374. if (!(ret & VM_FAULT_ERROR))
  2375. continue;
  2376. remainder = 0;
  2377. break;
  2378. }
  2379. pfn_offset = (vaddr & ~huge_page_mask(h)) >> PAGE_SHIFT;
  2380. page = pte_page(huge_ptep_get(pte));
  2381. same_page:
  2382. if (pages) {
  2383. pages[i] = mem_map_offset(page, pfn_offset);
  2384. get_page(pages[i]);
  2385. }
  2386. if (vmas)
  2387. vmas[i] = vma;
  2388. vaddr += PAGE_SIZE;
  2389. ++pfn_offset;
  2390. --remainder;
  2391. ++i;
  2392. if (vaddr < vma->vm_end && remainder &&
  2393. pfn_offset < pages_per_huge_page(h)) {
  2394. /*
  2395. * We use pfn_offset to avoid touching the pageframes
  2396. * of this compound page.
  2397. */
  2398. goto same_page;
  2399. }
  2400. }
  2401. spin_unlock(&mm->page_table_lock);
  2402. *length = remainder;
  2403. *position = vaddr;
  2404. return i ? i : -EFAULT;
  2405. }
  2406. void hugetlb_change_protection(struct vm_area_struct *vma,
  2407. unsigned long address, unsigned long end, pgprot_t newprot)
  2408. {
  2409. struct mm_struct *mm = vma->vm_mm;
  2410. unsigned long start = address;
  2411. pte_t *ptep;
  2412. pte_t pte;
  2413. struct hstate *h = hstate_vma(vma);
  2414. BUG_ON(address >= end);
  2415. flush_cache_range(vma, address, end);
  2416. mutex_lock(&vma->vm_file->f_mapping->i_mmap_mutex);
  2417. spin_lock(&mm->page_table_lock);
  2418. for (; address < end; address += huge_page_size(h)) {
  2419. ptep = huge_pte_offset(mm, address);
  2420. if (!ptep)
  2421. continue;
  2422. if (huge_pmd_unshare(mm, &address, ptep))
  2423. continue;
  2424. if (!huge_pte_none(huge_ptep_get(ptep))) {
  2425. pte = huge_ptep_get_and_clear(mm, address, ptep);
  2426. pte = pte_mkhuge(pte_modify(pte, newprot));
  2427. set_huge_pte_at(mm, address, ptep, pte);
  2428. }
  2429. }
  2430. spin_unlock(&mm->page_table_lock);
  2431. mutex_unlock(&vma->vm_file->f_mapping->i_mmap_mutex);
  2432. flush_tlb_range(vma, start, end);
  2433. }
  2434. int hugetlb_reserve_pages(struct inode *inode,
  2435. long from, long to,
  2436. struct vm_area_struct *vma,
  2437. vm_flags_t vm_flags)
  2438. {
  2439. long ret, chg;
  2440. struct hstate *h = hstate_inode(inode);
  2441. /*
  2442. * Only apply hugepage reservation if asked. At fault time, an
  2443. * attempt will be made for VM_NORESERVE to allocate a page
  2444. * and filesystem quota without using reserves
  2445. */
  2446. if (vm_flags & VM_NORESERVE)
  2447. return 0;
  2448. /*
  2449. * Shared mappings base their reservation on the number of pages that
  2450. * are already allocated on behalf of the file. Private mappings need
  2451. * to reserve the full area even if read-only as mprotect() may be
  2452. * called to make the mapping read-write. Assume !vma is a shm mapping
  2453. */
  2454. if (!vma || vma->vm_flags & VM_MAYSHARE)
  2455. chg = region_chg(&inode->i_mapping->private_list, from, to);
  2456. else {
  2457. struct resv_map *resv_map = resv_map_alloc();
  2458. if (!resv_map)
  2459. return -ENOMEM;
  2460. chg = to - from;
  2461. set_vma_resv_map(vma, resv_map);
  2462. set_vma_resv_flags(vma, HPAGE_RESV_OWNER);
  2463. }
  2464. if (chg < 0)
  2465. return chg;
  2466. /* There must be enough filesystem quota for the mapping */
  2467. if (hugetlb_get_quota(inode->i_mapping, chg))
  2468. return -ENOSPC;
  2469. /*
  2470. * Check enough hugepages are available for the reservation.
  2471. * Hand back the quota if there are not
  2472. */
  2473. ret = hugetlb_acct_memory(h, chg);
  2474. if (ret < 0) {
  2475. hugetlb_put_quota(inode->i_mapping, chg);
  2476. return ret;
  2477. }
  2478. /*
  2479. * Account for the reservations made. Shared mappings record regions
  2480. * that have reservations as they are shared by multiple VMAs.
  2481. * When the last VMA disappears, the region map says how much
  2482. * the reservation was and the page cache tells how much of
  2483. * the reservation was consumed. Private mappings are per-VMA and
  2484. * only the consumed reservations are tracked. When the VMA
  2485. * disappears, the original reservation is the VMA size and the
  2486. * consumed reservations are stored in the map. Hence, nothing
  2487. * else has to be done for private mappings here
  2488. */
  2489. if (!vma || vma->vm_flags & VM_MAYSHARE)
  2490. region_add(&inode->i_mapping->private_list, from, to);
  2491. return 0;
  2492. }
  2493. void hugetlb_unreserve_pages(struct inode *inode, long offset, long freed)
  2494. {
  2495. struct hstate *h = hstate_inode(inode);
  2496. long chg = region_truncate(&inode->i_mapping->private_list, offset);
  2497. spin_lock(&inode->i_lock);
  2498. inode->i_blocks -= (blocks_per_huge_page(h) * freed);
  2499. spin_unlock(&inode->i_lock);
  2500. hugetlb_put_quota(inode->i_mapping, (chg - freed));
  2501. hugetlb_acct_memory(h, -(chg - freed));
  2502. }
  2503. #ifdef CONFIG_MEMORY_FAILURE
  2504. /* Should be called in hugetlb_lock */
  2505. static int is_hugepage_on_freelist(struct page *hpage)
  2506. {
  2507. struct page *page;
  2508. struct page *tmp;
  2509. struct hstate *h = page_hstate(hpage);
  2510. int nid = page_to_nid(hpage);
  2511. list_for_each_entry_safe(page, tmp, &h->hugepage_freelists[nid], lru)
  2512. if (page == hpage)
  2513. return 1;
  2514. return 0;
  2515. }
  2516. /*
  2517. * This function is called from memory failure code.
  2518. * Assume the caller holds page lock of the head page.
  2519. */
  2520. int dequeue_hwpoisoned_huge_page(struct page *hpage)
  2521. {
  2522. struct hstate *h = page_hstate(hpage);
  2523. int nid = page_to_nid(hpage);
  2524. int ret = -EBUSY;
  2525. spin_lock(&hugetlb_lock);
  2526. if (is_hugepage_on_freelist(hpage)) {
  2527. list_del(&hpage->lru);
  2528. set_page_refcounted(hpage);
  2529. h->free_huge_pages--;
  2530. h->free_huge_pages_node[nid]--;
  2531. ret = 0;
  2532. }
  2533. spin_unlock(&hugetlb_lock);
  2534. return ret;
  2535. }
  2536. #endif