hugetlb.c 134 KB

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