hugetlb.c 128 KB

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