hugetlb.c 93 KB

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