hugetlb.c 113 KB

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