hugetlb.c 129 KB

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