hugetlb.c 100 KB

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