hugetlb.c 102 KB

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