hugetlb.c 118 KB

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