hugetlb.c 92 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559
  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/cpuset.h>
  17. #include <linux/mutex.h>
  18. #include <linux/bootmem.h>
  19. #include <linux/sysfs.h>
  20. #include <linux/slab.h>
  21. #include <linux/rmap.h>
  22. #include <linux/swap.h>
  23. #include <linux/swapops.h>
  24. #include <linux/page-isolation.h>
  25. #include <asm/page.h>
  26. #include <asm/pgtable.h>
  27. #include <asm/tlb.h>
  28. #include <linux/io.h>
  29. #include <linux/hugetlb.h>
  30. #include <linux/hugetlb_cgroup.h>
  31. #include <linux/node.h>
  32. #include "internal.h"
  33. const unsigned long hugetlb_zero = 0, hugetlb_infinity = ~0UL;
  34. unsigned long hugepages_treat_as_movable;
  35. int hugetlb_max_hstate __read_mostly;
  36. unsigned int default_hstate_idx;
  37. struct hstate hstates[HUGE_MAX_HSTATE];
  38. __initdata LIST_HEAD(huge_boot_pages);
  39. /* for command line parsing */
  40. static struct hstate * __initdata parsed_hstate;
  41. static unsigned long __initdata default_hstate_max_huge_pages;
  42. static unsigned long __initdata default_hstate_size;
  43. /*
  44. * Protects updates to hugepage_freelists, hugepage_activelist, nr_huge_pages,
  45. * free_huge_pages, and surplus_huge_pages.
  46. */
  47. DEFINE_SPINLOCK(hugetlb_lock);
  48. static inline void unlock_or_release_subpool(struct hugepage_subpool *spool)
  49. {
  50. bool free = (spool->count == 0) && (spool->used_hpages == 0);
  51. spin_unlock(&spool->lock);
  52. /* If no pages are used, and no other handles to the subpool
  53. * remain, free the subpool the subpool remain */
  54. if (free)
  55. kfree(spool);
  56. }
  57. struct hugepage_subpool *hugepage_new_subpool(long nr_blocks)
  58. {
  59. struct hugepage_subpool *spool;
  60. spool = kmalloc(sizeof(*spool), GFP_KERNEL);
  61. if (!spool)
  62. return NULL;
  63. spin_lock_init(&spool->lock);
  64. spool->count = 1;
  65. spool->max_hpages = nr_blocks;
  66. spool->used_hpages = 0;
  67. return spool;
  68. }
  69. void hugepage_put_subpool(struct hugepage_subpool *spool)
  70. {
  71. spin_lock(&spool->lock);
  72. BUG_ON(!spool->count);
  73. spool->count--;
  74. unlock_or_release_subpool(spool);
  75. }
  76. static int hugepage_subpool_get_pages(struct hugepage_subpool *spool,
  77. long delta)
  78. {
  79. int ret = 0;
  80. if (!spool)
  81. return 0;
  82. spin_lock(&spool->lock);
  83. if ((spool->used_hpages + delta) <= spool->max_hpages) {
  84. spool->used_hpages += delta;
  85. } else {
  86. ret = -ENOMEM;
  87. }
  88. spin_unlock(&spool->lock);
  89. return ret;
  90. }
  91. static void hugepage_subpool_put_pages(struct hugepage_subpool *spool,
  92. long delta)
  93. {
  94. if (!spool)
  95. return;
  96. spin_lock(&spool->lock);
  97. spool->used_hpages -= delta;
  98. /* If hugetlbfs_put_super couldn't free spool due to
  99. * an outstanding quota reference, free it now. */
  100. unlock_or_release_subpool(spool);
  101. }
  102. static inline struct hugepage_subpool *subpool_inode(struct inode *inode)
  103. {
  104. return HUGETLBFS_SB(inode->i_sb)->spool;
  105. }
  106. static inline struct hugepage_subpool *subpool_vma(struct vm_area_struct *vma)
  107. {
  108. return subpool_inode(file_inode(vma->vm_file));
  109. }
  110. /*
  111. * Region tracking -- allows tracking of reservations and instantiated pages
  112. * across the pages in a mapping.
  113. *
  114. * The region data structures are protected by a combination of the mmap_sem
  115. * and the hugetlb_instantiation_mutex. To access or modify a region the caller
  116. * must either hold the mmap_sem for write, or the mmap_sem for read and
  117. * the hugetlb_instantiation_mutex:
  118. *
  119. * down_write(&mm->mmap_sem);
  120. * or
  121. * down_read(&mm->mmap_sem);
  122. * mutex_lock(&hugetlb_instantiation_mutex);
  123. */
  124. struct file_region {
  125. struct list_head link;
  126. long from;
  127. long to;
  128. };
  129. static long region_add(struct list_head *head, long f, long t)
  130. {
  131. struct file_region *rg, *nrg, *trg;
  132. /* Locate the region we are either in or before. */
  133. list_for_each_entry(rg, head, link)
  134. if (f <= rg->to)
  135. break;
  136. /* Round our left edge to the current segment if it encloses us. */
  137. if (f > rg->from)
  138. f = rg->from;
  139. /* Check for and consume any regions we now overlap with. */
  140. nrg = rg;
  141. list_for_each_entry_safe(rg, trg, rg->link.prev, link) {
  142. if (&rg->link == head)
  143. break;
  144. if (rg->from > t)
  145. break;
  146. /* If this area reaches higher then extend our area to
  147. * include it completely. If this is not the first area
  148. * which we intend to reuse, free it. */
  149. if (rg->to > t)
  150. t = rg->to;
  151. if (rg != nrg) {
  152. list_del(&rg->link);
  153. kfree(rg);
  154. }
  155. }
  156. nrg->from = f;
  157. nrg->to = t;
  158. return 0;
  159. }
  160. static long region_chg(struct list_head *head, long f, long t)
  161. {
  162. struct file_region *rg, *nrg;
  163. long chg = 0;
  164. /* Locate the region we are before or in. */
  165. list_for_each_entry(rg, head, link)
  166. if (f <= rg->to)
  167. break;
  168. /* If we are below the current region then a new region is required.
  169. * Subtle, allocate a new region at the position but make it zero
  170. * size such that we can guarantee to record the reservation. */
  171. if (&rg->link == head || t < rg->from) {
  172. nrg = kmalloc(sizeof(*nrg), GFP_KERNEL);
  173. if (!nrg)
  174. return -ENOMEM;
  175. nrg->from = f;
  176. nrg->to = f;
  177. INIT_LIST_HEAD(&nrg->link);
  178. list_add(&nrg->link, rg->link.prev);
  179. return t - f;
  180. }
  181. /* Round our left edge to the current segment if it encloses us. */
  182. if (f > rg->from)
  183. f = rg->from;
  184. chg = t - f;
  185. /* Check for and consume any regions we now overlap with. */
  186. list_for_each_entry(rg, rg->link.prev, link) {
  187. if (&rg->link == head)
  188. break;
  189. if (rg->from > t)
  190. return chg;
  191. /* We overlap with this area, if it extends further than
  192. * us then we must extend ourselves. Account for its
  193. * existing reservation. */
  194. if (rg->to > t) {
  195. chg += rg->to - t;
  196. t = rg->to;
  197. }
  198. chg -= rg->to - rg->from;
  199. }
  200. return chg;
  201. }
  202. static long region_truncate(struct list_head *head, long end)
  203. {
  204. struct file_region *rg, *trg;
  205. long chg = 0;
  206. /* Locate the region we are either in or before. */
  207. list_for_each_entry(rg, head, link)
  208. if (end <= rg->to)
  209. break;
  210. if (&rg->link == head)
  211. return 0;
  212. /* If we are in the middle of a region then adjust it. */
  213. if (end > rg->from) {
  214. chg = rg->to - end;
  215. rg->to = end;
  216. rg = list_entry(rg->link.next, typeof(*rg), link);
  217. }
  218. /* Drop any remaining regions. */
  219. list_for_each_entry_safe(rg, trg, rg->link.prev, link) {
  220. if (&rg->link == head)
  221. break;
  222. chg += rg->to - rg->from;
  223. list_del(&rg->link);
  224. kfree(rg);
  225. }
  226. return chg;
  227. }
  228. static long region_count(struct list_head *head, long f, long t)
  229. {
  230. struct file_region *rg;
  231. long chg = 0;
  232. /* Locate each segment we overlap with, and count that overlap. */
  233. list_for_each_entry(rg, head, link) {
  234. long seg_from;
  235. long seg_to;
  236. if (rg->to <= f)
  237. continue;
  238. if (rg->from >= t)
  239. break;
  240. seg_from = max(rg->from, f);
  241. seg_to = min(rg->to, t);
  242. chg += seg_to - seg_from;
  243. }
  244. return chg;
  245. }
  246. /*
  247. * Convert the address within this vma to the page offset within
  248. * the mapping, in pagecache page units; huge pages here.
  249. */
  250. static pgoff_t vma_hugecache_offset(struct hstate *h,
  251. struct vm_area_struct *vma, unsigned long address)
  252. {
  253. return ((address - vma->vm_start) >> huge_page_shift(h)) +
  254. (vma->vm_pgoff >> huge_page_order(h));
  255. }
  256. pgoff_t linear_hugepage_index(struct vm_area_struct *vma,
  257. unsigned long address)
  258. {
  259. return vma_hugecache_offset(hstate_vma(vma), vma, address);
  260. }
  261. /*
  262. * Return the size of the pages allocated when backing a VMA. In the majority
  263. * cases this will be same size as used by the page table entries.
  264. */
  265. unsigned long vma_kernel_pagesize(struct vm_area_struct *vma)
  266. {
  267. struct hstate *hstate;
  268. if (!is_vm_hugetlb_page(vma))
  269. return PAGE_SIZE;
  270. hstate = hstate_vma(vma);
  271. return 1UL << huge_page_shift(hstate);
  272. }
  273. EXPORT_SYMBOL_GPL(vma_kernel_pagesize);
  274. /*
  275. * Return the page size being used by the MMU to back a VMA. In the majority
  276. * of cases, the page size used by the kernel matches the MMU size. On
  277. * architectures where it differs, an architecture-specific version of this
  278. * function is required.
  279. */
  280. #ifndef vma_mmu_pagesize
  281. unsigned long vma_mmu_pagesize(struct vm_area_struct *vma)
  282. {
  283. return vma_kernel_pagesize(vma);
  284. }
  285. #endif
  286. /*
  287. * Flags for MAP_PRIVATE reservations. These are stored in the bottom
  288. * bits of the reservation map pointer, which are always clear due to
  289. * alignment.
  290. */
  291. #define HPAGE_RESV_OWNER (1UL << 0)
  292. #define HPAGE_RESV_UNMAPPED (1UL << 1)
  293. #define HPAGE_RESV_MASK (HPAGE_RESV_OWNER | HPAGE_RESV_UNMAPPED)
  294. /*
  295. * These helpers are used to track how many pages are reserved for
  296. * faults in a MAP_PRIVATE mapping. Only the process that called mmap()
  297. * is guaranteed to have their future faults succeed.
  298. *
  299. * With the exception of reset_vma_resv_huge_pages() which is called at fork(),
  300. * the reserve counters are updated with the hugetlb_lock held. It is safe
  301. * to reset the VMA at fork() time as it is not in use yet and there is no
  302. * chance of the global counters getting corrupted as a result of the values.
  303. *
  304. * The private mapping reservation is represented in a subtly different
  305. * manner to a shared mapping. A shared mapping has a region map associated
  306. * with the underlying file, this region map represents the backing file
  307. * pages which have ever had a reservation assigned which this persists even
  308. * after the page is instantiated. A private mapping has a region map
  309. * associated with the original mmap which is attached to all VMAs which
  310. * reference it, this region map represents those offsets which have consumed
  311. * reservation ie. where pages have been instantiated.
  312. */
  313. static unsigned long get_vma_private_data(struct vm_area_struct *vma)
  314. {
  315. return (unsigned long)vma->vm_private_data;
  316. }
  317. static void set_vma_private_data(struct vm_area_struct *vma,
  318. unsigned long value)
  319. {
  320. vma->vm_private_data = (void *)value;
  321. }
  322. struct resv_map {
  323. struct kref refs;
  324. struct list_head regions;
  325. };
  326. static struct resv_map *resv_map_alloc(void)
  327. {
  328. struct resv_map *resv_map = kmalloc(sizeof(*resv_map), GFP_KERNEL);
  329. if (!resv_map)
  330. return NULL;
  331. kref_init(&resv_map->refs);
  332. INIT_LIST_HEAD(&resv_map->regions);
  333. return resv_map;
  334. }
  335. static void resv_map_release(struct kref *ref)
  336. {
  337. struct resv_map *resv_map = container_of(ref, struct resv_map, refs);
  338. /* Clear out any active regions before we release the map. */
  339. region_truncate(&resv_map->regions, 0);
  340. kfree(resv_map);
  341. }
  342. static struct resv_map *vma_resv_map(struct vm_area_struct *vma)
  343. {
  344. VM_BUG_ON(!is_vm_hugetlb_page(vma));
  345. if (!(vma->vm_flags & VM_MAYSHARE))
  346. return (struct resv_map *)(get_vma_private_data(vma) &
  347. ~HPAGE_RESV_MASK);
  348. return NULL;
  349. }
  350. static void set_vma_resv_map(struct vm_area_struct *vma, struct resv_map *map)
  351. {
  352. VM_BUG_ON(!is_vm_hugetlb_page(vma));
  353. VM_BUG_ON(vma->vm_flags & VM_MAYSHARE);
  354. set_vma_private_data(vma, (get_vma_private_data(vma) &
  355. HPAGE_RESV_MASK) | (unsigned long)map);
  356. }
  357. static void set_vma_resv_flags(struct vm_area_struct *vma, unsigned long flags)
  358. {
  359. VM_BUG_ON(!is_vm_hugetlb_page(vma));
  360. VM_BUG_ON(vma->vm_flags & VM_MAYSHARE);
  361. set_vma_private_data(vma, get_vma_private_data(vma) | flags);
  362. }
  363. static int is_vma_resv_set(struct vm_area_struct *vma, unsigned long flag)
  364. {
  365. VM_BUG_ON(!is_vm_hugetlb_page(vma));
  366. return (get_vma_private_data(vma) & flag) != 0;
  367. }
  368. /* Reset counters to 0 and clear all HPAGE_RESV_* flags */
  369. void reset_vma_resv_huge_pages(struct vm_area_struct *vma)
  370. {
  371. VM_BUG_ON(!is_vm_hugetlb_page(vma));
  372. if (!(vma->vm_flags & VM_MAYSHARE))
  373. vma->vm_private_data = (void *)0;
  374. }
  375. /* Returns true if the VMA has associated reserve pages */
  376. static int vma_has_reserves(struct vm_area_struct *vma, long chg)
  377. {
  378. if (vma->vm_flags & VM_NORESERVE) {
  379. /*
  380. * This address is already reserved by other process(chg == 0),
  381. * so, we should decrement reserved count. Without decrementing,
  382. * reserve count remains after releasing inode, because this
  383. * allocated page will go into page cache and is regarded as
  384. * coming from reserved pool in releasing step. Currently, we
  385. * don't have any other solution to deal with this situation
  386. * properly, so add work-around here.
  387. */
  388. if (vma->vm_flags & VM_MAYSHARE && chg == 0)
  389. return 1;
  390. else
  391. return 0;
  392. }
  393. /* Shared mappings always use reserves */
  394. if (vma->vm_flags & VM_MAYSHARE)
  395. return 1;
  396. /*
  397. * Only the process that called mmap() has reserves for
  398. * private mappings.
  399. */
  400. if (is_vma_resv_set(vma, HPAGE_RESV_OWNER))
  401. return 1;
  402. return 0;
  403. }
  404. static void copy_gigantic_page(struct page *dst, struct page *src)
  405. {
  406. int i;
  407. struct hstate *h = page_hstate(src);
  408. struct page *dst_base = dst;
  409. struct page *src_base = src;
  410. for (i = 0; i < pages_per_huge_page(h); ) {
  411. cond_resched();
  412. copy_highpage(dst, src);
  413. i++;
  414. dst = mem_map_next(dst, dst_base, i);
  415. src = mem_map_next(src, src_base, i);
  416. }
  417. }
  418. void copy_huge_page(struct page *dst, struct page *src)
  419. {
  420. int i;
  421. struct hstate *h = page_hstate(src);
  422. if (unlikely(pages_per_huge_page(h) > MAX_ORDER_NR_PAGES)) {
  423. copy_gigantic_page(dst, src);
  424. return;
  425. }
  426. might_sleep();
  427. for (i = 0; i < pages_per_huge_page(h); i++) {
  428. cond_resched();
  429. copy_highpage(dst + i, src + i);
  430. }
  431. }
  432. static void enqueue_huge_page(struct hstate *h, struct page *page)
  433. {
  434. int nid = page_to_nid(page);
  435. list_move(&page->lru, &h->hugepage_freelists[nid]);
  436. h->free_huge_pages++;
  437. h->free_huge_pages_node[nid]++;
  438. }
  439. static struct page *dequeue_huge_page_node(struct hstate *h, int nid)
  440. {
  441. struct page *page;
  442. list_for_each_entry(page, &h->hugepage_freelists[nid], lru)
  443. if (!is_migrate_isolate_page(page))
  444. break;
  445. /*
  446. * if 'non-isolated free hugepage' not found on the list,
  447. * the allocation fails.
  448. */
  449. if (&h->hugepage_freelists[nid] == &page->lru)
  450. return NULL;
  451. list_move(&page->lru, &h->hugepage_activelist);
  452. set_page_refcounted(page);
  453. h->free_huge_pages--;
  454. h->free_huge_pages_node[nid]--;
  455. return page;
  456. }
  457. /* Movability of hugepages depends on migration support. */
  458. static inline gfp_t htlb_alloc_mask(struct hstate *h)
  459. {
  460. if (hugepages_treat_as_movable || hugepage_migration_support(h))
  461. return GFP_HIGHUSER_MOVABLE;
  462. else
  463. return GFP_HIGHUSER;
  464. }
  465. static struct page *dequeue_huge_page_vma(struct hstate *h,
  466. struct vm_area_struct *vma,
  467. unsigned long address, int avoid_reserve,
  468. long chg)
  469. {
  470. struct page *page = NULL;
  471. struct mempolicy *mpol;
  472. nodemask_t *nodemask;
  473. struct zonelist *zonelist;
  474. struct zone *zone;
  475. struct zoneref *z;
  476. unsigned int cpuset_mems_cookie;
  477. /*
  478. * A child process with MAP_PRIVATE mappings created by their parent
  479. * have no page reserves. This check ensures that reservations are
  480. * not "stolen". The child may still get SIGKILLed
  481. */
  482. if (!vma_has_reserves(vma, chg) &&
  483. h->free_huge_pages - h->resv_huge_pages == 0)
  484. goto err;
  485. /* If reserves cannot be used, ensure enough pages are in the pool */
  486. if (avoid_reserve && h->free_huge_pages - h->resv_huge_pages == 0)
  487. goto err;
  488. retry_cpuset:
  489. cpuset_mems_cookie = get_mems_allowed();
  490. zonelist = huge_zonelist(vma, address,
  491. htlb_alloc_mask(h), &mpol, &nodemask);
  492. for_each_zone_zonelist_nodemask(zone, z, zonelist,
  493. MAX_NR_ZONES - 1, nodemask) {
  494. if (cpuset_zone_allowed_softwall(zone, htlb_alloc_mask(h))) {
  495. page = dequeue_huge_page_node(h, zone_to_nid(zone));
  496. if (page) {
  497. if (avoid_reserve)
  498. break;
  499. if (!vma_has_reserves(vma, chg))
  500. break;
  501. SetPagePrivate(page);
  502. h->resv_huge_pages--;
  503. break;
  504. }
  505. }
  506. }
  507. mpol_cond_put(mpol);
  508. if (unlikely(!put_mems_allowed(cpuset_mems_cookie) && !page))
  509. goto retry_cpuset;
  510. return page;
  511. err:
  512. return NULL;
  513. }
  514. static void update_and_free_page(struct hstate *h, struct page *page)
  515. {
  516. int i;
  517. VM_BUG_ON(h->order >= MAX_ORDER);
  518. h->nr_huge_pages--;
  519. h->nr_huge_pages_node[page_to_nid(page)]--;
  520. for (i = 0; i < pages_per_huge_page(h); i++) {
  521. page[i].flags &= ~(1 << PG_locked | 1 << PG_error |
  522. 1 << PG_referenced | 1 << PG_dirty |
  523. 1 << PG_active | 1 << PG_reserved |
  524. 1 << PG_private | 1 << PG_writeback);
  525. }
  526. VM_BUG_ON(hugetlb_cgroup_from_page(page));
  527. set_compound_page_dtor(page, NULL);
  528. set_page_refcounted(page);
  529. arch_release_hugepage(page);
  530. __free_pages(page, huge_page_order(h));
  531. }
  532. struct hstate *size_to_hstate(unsigned long size)
  533. {
  534. struct hstate *h;
  535. for_each_hstate(h) {
  536. if (huge_page_size(h) == size)
  537. return h;
  538. }
  539. return NULL;
  540. }
  541. static void free_huge_page(struct page *page)
  542. {
  543. /*
  544. * Can't pass hstate in here because it is called from the
  545. * compound page destructor.
  546. */
  547. struct hstate *h = page_hstate(page);
  548. int nid = page_to_nid(page);
  549. struct hugepage_subpool *spool =
  550. (struct hugepage_subpool *)page_private(page);
  551. bool restore_reserve;
  552. set_page_private(page, 0);
  553. page->mapping = NULL;
  554. BUG_ON(page_count(page));
  555. BUG_ON(page_mapcount(page));
  556. restore_reserve = PagePrivate(page);
  557. ClearPagePrivate(page);
  558. spin_lock(&hugetlb_lock);
  559. hugetlb_cgroup_uncharge_page(hstate_index(h),
  560. pages_per_huge_page(h), page);
  561. if (restore_reserve)
  562. h->resv_huge_pages++;
  563. if (h->surplus_huge_pages_node[nid] && huge_page_order(h) < MAX_ORDER) {
  564. /* remove the page from active list */
  565. list_del(&page->lru);
  566. update_and_free_page(h, page);
  567. h->surplus_huge_pages--;
  568. h->surplus_huge_pages_node[nid]--;
  569. } else {
  570. arch_clear_hugepage_flags(page);
  571. enqueue_huge_page(h, page);
  572. }
  573. spin_unlock(&hugetlb_lock);
  574. hugepage_subpool_put_pages(spool, 1);
  575. }
  576. static void prep_new_huge_page(struct hstate *h, struct page *page, int nid)
  577. {
  578. INIT_LIST_HEAD(&page->lru);
  579. set_compound_page_dtor(page, free_huge_page);
  580. spin_lock(&hugetlb_lock);
  581. set_hugetlb_cgroup(page, NULL);
  582. h->nr_huge_pages++;
  583. h->nr_huge_pages_node[nid]++;
  584. spin_unlock(&hugetlb_lock);
  585. put_page(page); /* free it into the hugepage allocator */
  586. }
  587. static void prep_compound_gigantic_page(struct page *page, unsigned long order)
  588. {
  589. int i;
  590. int nr_pages = 1 << order;
  591. struct page *p = page + 1;
  592. /* we rely on prep_new_huge_page to set the destructor */
  593. set_compound_order(page, order);
  594. __SetPageHead(page);
  595. __ClearPageReserved(page);
  596. for (i = 1; i < nr_pages; i++, p = mem_map_next(p, page, i)) {
  597. __SetPageTail(p);
  598. /*
  599. * For gigantic hugepages allocated through bootmem at
  600. * boot, it's safer to be consistent with the not-gigantic
  601. * hugepages and clear the PG_reserved bit from all tail pages
  602. * too. Otherwse drivers using get_user_pages() to access tail
  603. * pages may get the reference counting wrong if they see
  604. * PG_reserved set on a tail page (despite the head page not
  605. * having PG_reserved set). Enforcing this consistency between
  606. * head and tail pages allows drivers to optimize away a check
  607. * on the head page when they need know if put_page() is needed
  608. * after get_user_pages().
  609. */
  610. __ClearPageReserved(p);
  611. set_page_count(p, 0);
  612. p->first_page = page;
  613. }
  614. }
  615. /*
  616. * PageHuge() only returns true for hugetlbfs pages, but not for normal or
  617. * transparent huge pages. See the PageTransHuge() documentation for more
  618. * details.
  619. */
  620. int PageHuge(struct page *page)
  621. {
  622. compound_page_dtor *dtor;
  623. if (!PageCompound(page))
  624. return 0;
  625. page = compound_head(page);
  626. dtor = get_compound_page_dtor(page);
  627. return dtor == free_huge_page;
  628. }
  629. EXPORT_SYMBOL_GPL(PageHuge);
  630. pgoff_t __basepage_index(struct page *page)
  631. {
  632. struct page *page_head = compound_head(page);
  633. pgoff_t index = page_index(page_head);
  634. unsigned long compound_idx;
  635. if (!PageHuge(page_head))
  636. return page_index(page);
  637. if (compound_order(page_head) >= MAX_ORDER)
  638. compound_idx = page_to_pfn(page) - page_to_pfn(page_head);
  639. else
  640. compound_idx = page - page_head;
  641. return (index << compound_order(page_head)) + compound_idx;
  642. }
  643. static struct page *alloc_fresh_huge_page_node(struct hstate *h, int nid)
  644. {
  645. struct page *page;
  646. if (h->order >= MAX_ORDER)
  647. return NULL;
  648. page = alloc_pages_exact_node(nid,
  649. htlb_alloc_mask(h)|__GFP_COMP|__GFP_THISNODE|
  650. __GFP_REPEAT|__GFP_NOWARN,
  651. huge_page_order(h));
  652. if (page) {
  653. if (arch_prepare_hugepage(page)) {
  654. __free_pages(page, huge_page_order(h));
  655. return NULL;
  656. }
  657. prep_new_huge_page(h, page, nid);
  658. }
  659. return page;
  660. }
  661. /*
  662. * common helper functions for hstate_next_node_to_{alloc|free}.
  663. * We may have allocated or freed a huge page based on a different
  664. * nodes_allowed previously, so h->next_node_to_{alloc|free} might
  665. * be outside of *nodes_allowed. Ensure that we use an allowed
  666. * node for alloc or free.
  667. */
  668. static int next_node_allowed(int nid, nodemask_t *nodes_allowed)
  669. {
  670. nid = next_node(nid, *nodes_allowed);
  671. if (nid == MAX_NUMNODES)
  672. nid = first_node(*nodes_allowed);
  673. VM_BUG_ON(nid >= MAX_NUMNODES);
  674. return nid;
  675. }
  676. static int get_valid_node_allowed(int nid, nodemask_t *nodes_allowed)
  677. {
  678. if (!node_isset(nid, *nodes_allowed))
  679. nid = next_node_allowed(nid, nodes_allowed);
  680. return nid;
  681. }
  682. /*
  683. * returns the previously saved node ["this node"] from which to
  684. * allocate a persistent huge page for the pool and advance the
  685. * next node from which to allocate, handling wrap at end of node
  686. * mask.
  687. */
  688. static int hstate_next_node_to_alloc(struct hstate *h,
  689. nodemask_t *nodes_allowed)
  690. {
  691. int nid;
  692. VM_BUG_ON(!nodes_allowed);
  693. nid = get_valid_node_allowed(h->next_nid_to_alloc, nodes_allowed);
  694. h->next_nid_to_alloc = next_node_allowed(nid, nodes_allowed);
  695. return nid;
  696. }
  697. /*
  698. * helper for free_pool_huge_page() - return the previously saved
  699. * node ["this node"] from which to free a huge page. Advance the
  700. * next node id whether or not we find a free huge page to free so
  701. * that the next attempt to free addresses the next node.
  702. */
  703. static int hstate_next_node_to_free(struct hstate *h, nodemask_t *nodes_allowed)
  704. {
  705. int nid;
  706. VM_BUG_ON(!nodes_allowed);
  707. nid = get_valid_node_allowed(h->next_nid_to_free, nodes_allowed);
  708. h->next_nid_to_free = next_node_allowed(nid, nodes_allowed);
  709. return nid;
  710. }
  711. #define for_each_node_mask_to_alloc(hs, nr_nodes, node, mask) \
  712. for (nr_nodes = nodes_weight(*mask); \
  713. nr_nodes > 0 && \
  714. ((node = hstate_next_node_to_alloc(hs, mask)) || 1); \
  715. nr_nodes--)
  716. #define for_each_node_mask_to_free(hs, nr_nodes, node, mask) \
  717. for (nr_nodes = nodes_weight(*mask); \
  718. nr_nodes > 0 && \
  719. ((node = hstate_next_node_to_free(hs, mask)) || 1); \
  720. nr_nodes--)
  721. static int alloc_fresh_huge_page(struct hstate *h, nodemask_t *nodes_allowed)
  722. {
  723. struct page *page;
  724. int nr_nodes, node;
  725. int ret = 0;
  726. for_each_node_mask_to_alloc(h, nr_nodes, node, nodes_allowed) {
  727. page = alloc_fresh_huge_page_node(h, node);
  728. if (page) {
  729. ret = 1;
  730. break;
  731. }
  732. }
  733. if (ret)
  734. count_vm_event(HTLB_BUDDY_PGALLOC);
  735. else
  736. count_vm_event(HTLB_BUDDY_PGALLOC_FAIL);
  737. return ret;
  738. }
  739. /*
  740. * Free huge page from pool from next node to free.
  741. * Attempt to keep persistent huge pages more or less
  742. * balanced over allowed nodes.
  743. * Called with hugetlb_lock locked.
  744. */
  745. static int free_pool_huge_page(struct hstate *h, nodemask_t *nodes_allowed,
  746. bool acct_surplus)
  747. {
  748. int nr_nodes, node;
  749. int ret = 0;
  750. for_each_node_mask_to_free(h, nr_nodes, node, nodes_allowed) {
  751. /*
  752. * If we're returning unused surplus pages, only examine
  753. * nodes with surplus pages.
  754. */
  755. if ((!acct_surplus || h->surplus_huge_pages_node[node]) &&
  756. !list_empty(&h->hugepage_freelists[node])) {
  757. struct page *page =
  758. list_entry(h->hugepage_freelists[node].next,
  759. struct page, lru);
  760. list_del(&page->lru);
  761. h->free_huge_pages--;
  762. h->free_huge_pages_node[node]--;
  763. if (acct_surplus) {
  764. h->surplus_huge_pages--;
  765. h->surplus_huge_pages_node[node]--;
  766. }
  767. update_and_free_page(h, page);
  768. ret = 1;
  769. break;
  770. }
  771. }
  772. return ret;
  773. }
  774. /*
  775. * Dissolve a given free hugepage into free buddy pages. This function does
  776. * nothing for in-use (including surplus) hugepages.
  777. */
  778. static void dissolve_free_huge_page(struct page *page)
  779. {
  780. spin_lock(&hugetlb_lock);
  781. if (PageHuge(page) && !page_count(page)) {
  782. struct hstate *h = page_hstate(page);
  783. int nid = page_to_nid(page);
  784. list_del(&page->lru);
  785. h->free_huge_pages--;
  786. h->free_huge_pages_node[nid]--;
  787. update_and_free_page(h, page);
  788. }
  789. spin_unlock(&hugetlb_lock);
  790. }
  791. /*
  792. * Dissolve free hugepages in a given pfn range. Used by memory hotplug to
  793. * make specified memory blocks removable from the system.
  794. * Note that start_pfn should aligned with (minimum) hugepage size.
  795. */
  796. void dissolve_free_huge_pages(unsigned long start_pfn, unsigned long end_pfn)
  797. {
  798. unsigned int order = 8 * sizeof(void *);
  799. unsigned long pfn;
  800. struct hstate *h;
  801. /* Set scan step to minimum hugepage size */
  802. for_each_hstate(h)
  803. if (order > huge_page_order(h))
  804. order = huge_page_order(h);
  805. VM_BUG_ON(!IS_ALIGNED(start_pfn, 1 << order));
  806. for (pfn = start_pfn; pfn < end_pfn; pfn += 1 << order)
  807. dissolve_free_huge_page(pfn_to_page(pfn));
  808. }
  809. static struct page *alloc_buddy_huge_page(struct hstate *h, int nid)
  810. {
  811. struct page *page;
  812. unsigned int r_nid;
  813. if (h->order >= MAX_ORDER)
  814. return NULL;
  815. /*
  816. * Assume we will successfully allocate the surplus page to
  817. * prevent racing processes from causing the surplus to exceed
  818. * overcommit
  819. *
  820. * This however introduces a different race, where a process B
  821. * tries to grow the static hugepage pool while alloc_pages() is
  822. * called by process A. B will only examine the per-node
  823. * counters in determining if surplus huge pages can be
  824. * converted to normal huge pages in adjust_pool_surplus(). A
  825. * won't be able to increment the per-node counter, until the
  826. * lock is dropped by B, but B doesn't drop hugetlb_lock until
  827. * no more huge pages can be converted from surplus to normal
  828. * state (and doesn't try to convert again). Thus, we have a
  829. * case where a surplus huge page exists, the pool is grown, and
  830. * the surplus huge page still exists after, even though it
  831. * should just have been converted to a normal huge page. This
  832. * does not leak memory, though, as the hugepage will be freed
  833. * once it is out of use. It also does not allow the counters to
  834. * go out of whack in adjust_pool_surplus() as we don't modify
  835. * the node values until we've gotten the hugepage and only the
  836. * per-node value is checked there.
  837. */
  838. spin_lock(&hugetlb_lock);
  839. if (h->surplus_huge_pages >= h->nr_overcommit_huge_pages) {
  840. spin_unlock(&hugetlb_lock);
  841. return NULL;
  842. } else {
  843. h->nr_huge_pages++;
  844. h->surplus_huge_pages++;
  845. }
  846. spin_unlock(&hugetlb_lock);
  847. if (nid == NUMA_NO_NODE)
  848. page = alloc_pages(htlb_alloc_mask(h)|__GFP_COMP|
  849. __GFP_REPEAT|__GFP_NOWARN,
  850. huge_page_order(h));
  851. else
  852. page = alloc_pages_exact_node(nid,
  853. htlb_alloc_mask(h)|__GFP_COMP|__GFP_THISNODE|
  854. __GFP_REPEAT|__GFP_NOWARN, huge_page_order(h));
  855. if (page && arch_prepare_hugepage(page)) {
  856. __free_pages(page, huge_page_order(h));
  857. page = NULL;
  858. }
  859. spin_lock(&hugetlb_lock);
  860. if (page) {
  861. INIT_LIST_HEAD(&page->lru);
  862. r_nid = page_to_nid(page);
  863. set_compound_page_dtor(page, free_huge_page);
  864. set_hugetlb_cgroup(page, NULL);
  865. /*
  866. * We incremented the global counters already
  867. */
  868. h->nr_huge_pages_node[r_nid]++;
  869. h->surplus_huge_pages_node[r_nid]++;
  870. __count_vm_event(HTLB_BUDDY_PGALLOC);
  871. } else {
  872. h->nr_huge_pages--;
  873. h->surplus_huge_pages--;
  874. __count_vm_event(HTLB_BUDDY_PGALLOC_FAIL);
  875. }
  876. spin_unlock(&hugetlb_lock);
  877. return page;
  878. }
  879. /*
  880. * This allocation function is useful in the context where vma is irrelevant.
  881. * E.g. soft-offlining uses this function because it only cares physical
  882. * address of error page.
  883. */
  884. struct page *alloc_huge_page_node(struct hstate *h, int nid)
  885. {
  886. struct page *page = NULL;
  887. spin_lock(&hugetlb_lock);
  888. if (h->free_huge_pages - h->resv_huge_pages > 0)
  889. page = dequeue_huge_page_node(h, nid);
  890. spin_unlock(&hugetlb_lock);
  891. if (!page)
  892. page = alloc_buddy_huge_page(h, nid);
  893. return page;
  894. }
  895. /*
  896. * Increase the hugetlb pool such that it can accommodate a reservation
  897. * of size 'delta'.
  898. */
  899. static int gather_surplus_pages(struct hstate *h, int delta)
  900. {
  901. struct list_head surplus_list;
  902. struct page *page, *tmp;
  903. int ret, i;
  904. int needed, allocated;
  905. bool alloc_ok = true;
  906. needed = (h->resv_huge_pages + delta) - h->free_huge_pages;
  907. if (needed <= 0) {
  908. h->resv_huge_pages += delta;
  909. return 0;
  910. }
  911. allocated = 0;
  912. INIT_LIST_HEAD(&surplus_list);
  913. ret = -ENOMEM;
  914. retry:
  915. spin_unlock(&hugetlb_lock);
  916. for (i = 0; i < needed; i++) {
  917. page = alloc_buddy_huge_page(h, NUMA_NO_NODE);
  918. if (!page) {
  919. alloc_ok = false;
  920. break;
  921. }
  922. list_add(&page->lru, &surplus_list);
  923. }
  924. allocated += i;
  925. /*
  926. * After retaking hugetlb_lock, we need to recalculate 'needed'
  927. * because either resv_huge_pages or free_huge_pages may have changed.
  928. */
  929. spin_lock(&hugetlb_lock);
  930. needed = (h->resv_huge_pages + delta) -
  931. (h->free_huge_pages + allocated);
  932. if (needed > 0) {
  933. if (alloc_ok)
  934. goto retry;
  935. /*
  936. * We were not able to allocate enough pages to
  937. * satisfy the entire reservation so we free what
  938. * we've allocated so far.
  939. */
  940. goto free;
  941. }
  942. /*
  943. * The surplus_list now contains _at_least_ the number of extra pages
  944. * needed to accommodate the reservation. Add the appropriate number
  945. * of pages to the hugetlb pool and free the extras back to the buddy
  946. * allocator. Commit the entire reservation here to prevent another
  947. * process from stealing the pages as they are added to the pool but
  948. * before they are reserved.
  949. */
  950. needed += allocated;
  951. h->resv_huge_pages += delta;
  952. ret = 0;
  953. /* Free the needed pages to the hugetlb pool */
  954. list_for_each_entry_safe(page, tmp, &surplus_list, lru) {
  955. if ((--needed) < 0)
  956. break;
  957. /*
  958. * This page is now managed by the hugetlb allocator and has
  959. * no users -- drop the buddy allocator's reference.
  960. */
  961. put_page_testzero(page);
  962. VM_BUG_ON(page_count(page));
  963. enqueue_huge_page(h, page);
  964. }
  965. free:
  966. spin_unlock(&hugetlb_lock);
  967. /* Free unnecessary surplus pages to the buddy allocator */
  968. list_for_each_entry_safe(page, tmp, &surplus_list, lru)
  969. put_page(page);
  970. spin_lock(&hugetlb_lock);
  971. return ret;
  972. }
  973. /*
  974. * When releasing a hugetlb pool reservation, any surplus pages that were
  975. * allocated to satisfy the reservation must be explicitly freed if they were
  976. * never used.
  977. * Called with hugetlb_lock held.
  978. */
  979. static void return_unused_surplus_pages(struct hstate *h,
  980. unsigned long unused_resv_pages)
  981. {
  982. unsigned long nr_pages;
  983. /* Uncommit the reservation */
  984. h->resv_huge_pages -= unused_resv_pages;
  985. /* Cannot return gigantic pages currently */
  986. if (h->order >= MAX_ORDER)
  987. return;
  988. nr_pages = min(unused_resv_pages, h->surplus_huge_pages);
  989. /*
  990. * We want to release as many surplus pages as possible, spread
  991. * evenly across all nodes with memory. Iterate across these nodes
  992. * until we can no longer free unreserved surplus pages. This occurs
  993. * when the nodes with surplus pages have no free pages.
  994. * free_pool_huge_page() will balance the the freed pages across the
  995. * on-line nodes with memory and will handle the hstate accounting.
  996. */
  997. while (nr_pages--) {
  998. if (!free_pool_huge_page(h, &node_states[N_MEMORY], 1))
  999. break;
  1000. }
  1001. }
  1002. /*
  1003. * Determine if the huge page at addr within the vma has an associated
  1004. * reservation. Where it does not we will need to logically increase
  1005. * reservation and actually increase subpool usage before an allocation
  1006. * can occur. Where any new reservation would be required the
  1007. * reservation change is prepared, but not committed. Once the page
  1008. * has been allocated from the subpool and instantiated the change should
  1009. * be committed via vma_commit_reservation. No action is required on
  1010. * failure.
  1011. */
  1012. static long vma_needs_reservation(struct hstate *h,
  1013. struct vm_area_struct *vma, unsigned long addr)
  1014. {
  1015. struct address_space *mapping = vma->vm_file->f_mapping;
  1016. struct inode *inode = mapping->host;
  1017. if (vma->vm_flags & VM_MAYSHARE) {
  1018. pgoff_t idx = vma_hugecache_offset(h, vma, addr);
  1019. return region_chg(&inode->i_mapping->private_list,
  1020. idx, idx + 1);
  1021. } else if (!is_vma_resv_set(vma, HPAGE_RESV_OWNER)) {
  1022. return 1;
  1023. } else {
  1024. long err;
  1025. pgoff_t idx = vma_hugecache_offset(h, vma, addr);
  1026. struct resv_map *resv = vma_resv_map(vma);
  1027. err = region_chg(&resv->regions, idx, idx + 1);
  1028. if (err < 0)
  1029. return err;
  1030. return 0;
  1031. }
  1032. }
  1033. static void vma_commit_reservation(struct hstate *h,
  1034. struct vm_area_struct *vma, unsigned long addr)
  1035. {
  1036. struct address_space *mapping = vma->vm_file->f_mapping;
  1037. struct inode *inode = mapping->host;
  1038. if (vma->vm_flags & VM_MAYSHARE) {
  1039. pgoff_t idx = vma_hugecache_offset(h, vma, addr);
  1040. region_add(&inode->i_mapping->private_list, idx, idx + 1);
  1041. } else if (is_vma_resv_set(vma, HPAGE_RESV_OWNER)) {
  1042. pgoff_t idx = vma_hugecache_offset(h, vma, addr);
  1043. struct resv_map *resv = vma_resv_map(vma);
  1044. /* Mark this page used in the map. */
  1045. region_add(&resv->regions, idx, idx + 1);
  1046. }
  1047. }
  1048. static struct page *alloc_huge_page(struct vm_area_struct *vma,
  1049. unsigned long addr, int avoid_reserve)
  1050. {
  1051. struct hugepage_subpool *spool = subpool_vma(vma);
  1052. struct hstate *h = hstate_vma(vma);
  1053. struct page *page;
  1054. long chg;
  1055. int ret, idx;
  1056. struct hugetlb_cgroup *h_cg;
  1057. idx = hstate_index(h);
  1058. /*
  1059. * Processes that did not create the mapping will have no
  1060. * reserves and will not have accounted against subpool
  1061. * limit. Check that the subpool limit can be made before
  1062. * satisfying the allocation MAP_NORESERVE mappings may also
  1063. * need pages and subpool limit allocated allocated if no reserve
  1064. * mapping overlaps.
  1065. */
  1066. chg = vma_needs_reservation(h, vma, addr);
  1067. if (chg < 0)
  1068. return ERR_PTR(-ENOMEM);
  1069. if (chg || avoid_reserve)
  1070. if (hugepage_subpool_get_pages(spool, 1))
  1071. return ERR_PTR(-ENOSPC);
  1072. ret = hugetlb_cgroup_charge_cgroup(idx, pages_per_huge_page(h), &h_cg);
  1073. if (ret) {
  1074. if (chg || avoid_reserve)
  1075. hugepage_subpool_put_pages(spool, 1);
  1076. return ERR_PTR(-ENOSPC);
  1077. }
  1078. spin_lock(&hugetlb_lock);
  1079. page = dequeue_huge_page_vma(h, vma, addr, avoid_reserve, chg);
  1080. if (!page) {
  1081. spin_unlock(&hugetlb_lock);
  1082. page = alloc_buddy_huge_page(h, NUMA_NO_NODE);
  1083. if (!page) {
  1084. hugetlb_cgroup_uncharge_cgroup(idx,
  1085. pages_per_huge_page(h),
  1086. h_cg);
  1087. if (chg || avoid_reserve)
  1088. hugepage_subpool_put_pages(spool, 1);
  1089. return ERR_PTR(-ENOSPC);
  1090. }
  1091. spin_lock(&hugetlb_lock);
  1092. list_move(&page->lru, &h->hugepage_activelist);
  1093. /* Fall through */
  1094. }
  1095. hugetlb_cgroup_commit_charge(idx, pages_per_huge_page(h), h_cg, page);
  1096. spin_unlock(&hugetlb_lock);
  1097. set_page_private(page, (unsigned long)spool);
  1098. vma_commit_reservation(h, vma, addr);
  1099. return page;
  1100. }
  1101. /*
  1102. * alloc_huge_page()'s wrapper which simply returns the page if allocation
  1103. * succeeds, otherwise NULL. This function is called from new_vma_page(),
  1104. * where no ERR_VALUE is expected to be returned.
  1105. */
  1106. struct page *alloc_huge_page_noerr(struct vm_area_struct *vma,
  1107. unsigned long addr, int avoid_reserve)
  1108. {
  1109. struct page *page = alloc_huge_page(vma, addr, avoid_reserve);
  1110. if (IS_ERR(page))
  1111. page = NULL;
  1112. return page;
  1113. }
  1114. int __weak alloc_bootmem_huge_page(struct hstate *h)
  1115. {
  1116. struct huge_bootmem_page *m;
  1117. int nr_nodes, node;
  1118. for_each_node_mask_to_alloc(h, nr_nodes, node, &node_states[N_MEMORY]) {
  1119. void *addr;
  1120. addr = __alloc_bootmem_node_nopanic(NODE_DATA(node),
  1121. huge_page_size(h), huge_page_size(h), 0);
  1122. if (addr) {
  1123. /*
  1124. * Use the beginning of the huge page to store the
  1125. * huge_bootmem_page struct (until gather_bootmem
  1126. * puts them into the mem_map).
  1127. */
  1128. m = addr;
  1129. goto found;
  1130. }
  1131. }
  1132. return 0;
  1133. found:
  1134. BUG_ON((unsigned long)virt_to_phys(m) & (huge_page_size(h) - 1));
  1135. /* Put them into a private list first because mem_map is not up yet */
  1136. list_add(&m->list, &huge_boot_pages);
  1137. m->hstate = h;
  1138. return 1;
  1139. }
  1140. static void prep_compound_huge_page(struct page *page, int order)
  1141. {
  1142. if (unlikely(order > (MAX_ORDER - 1)))
  1143. prep_compound_gigantic_page(page, order);
  1144. else
  1145. prep_compound_page(page, order);
  1146. }
  1147. /* Put bootmem huge pages into the standard lists after mem_map is up */
  1148. static void __init gather_bootmem_prealloc(void)
  1149. {
  1150. struct huge_bootmem_page *m;
  1151. list_for_each_entry(m, &huge_boot_pages, list) {
  1152. struct hstate *h = m->hstate;
  1153. struct page *page;
  1154. #ifdef CONFIG_HIGHMEM
  1155. page = pfn_to_page(m->phys >> PAGE_SHIFT);
  1156. free_bootmem_late((unsigned long)m,
  1157. sizeof(struct huge_bootmem_page));
  1158. #else
  1159. page = virt_to_page(m);
  1160. #endif
  1161. WARN_ON(page_count(page) != 1);
  1162. prep_compound_huge_page(page, h->order);
  1163. WARN_ON(PageReserved(page));
  1164. prep_new_huge_page(h, page, page_to_nid(page));
  1165. /*
  1166. * If we had gigantic hugepages allocated at boot time, we need
  1167. * to restore the 'stolen' pages to totalram_pages in order to
  1168. * fix confusing memory reports from free(1) and another
  1169. * side-effects, like CommitLimit going negative.
  1170. */
  1171. if (h->order > (MAX_ORDER - 1))
  1172. adjust_managed_page_count(page, 1 << h->order);
  1173. }
  1174. }
  1175. static void __init hugetlb_hstate_alloc_pages(struct hstate *h)
  1176. {
  1177. unsigned long i;
  1178. for (i = 0; i < h->max_huge_pages; ++i) {
  1179. if (h->order >= MAX_ORDER) {
  1180. if (!alloc_bootmem_huge_page(h))
  1181. break;
  1182. } else if (!alloc_fresh_huge_page(h,
  1183. &node_states[N_MEMORY]))
  1184. break;
  1185. }
  1186. h->max_huge_pages = i;
  1187. }
  1188. static void __init hugetlb_init_hstates(void)
  1189. {
  1190. struct hstate *h;
  1191. for_each_hstate(h) {
  1192. /* oversize hugepages were init'ed in early boot */
  1193. if (h->order < MAX_ORDER)
  1194. hugetlb_hstate_alloc_pages(h);
  1195. }
  1196. }
  1197. static char * __init memfmt(char *buf, unsigned long n)
  1198. {
  1199. if (n >= (1UL << 30))
  1200. sprintf(buf, "%lu GB", n >> 30);
  1201. else if (n >= (1UL << 20))
  1202. sprintf(buf, "%lu MB", n >> 20);
  1203. else
  1204. sprintf(buf, "%lu KB", n >> 10);
  1205. return buf;
  1206. }
  1207. static void __init report_hugepages(void)
  1208. {
  1209. struct hstate *h;
  1210. for_each_hstate(h) {
  1211. char buf[32];
  1212. pr_info("HugeTLB registered %s page size, pre-allocated %ld pages\n",
  1213. memfmt(buf, huge_page_size(h)),
  1214. h->free_huge_pages);
  1215. }
  1216. }
  1217. #ifdef CONFIG_HIGHMEM
  1218. static void try_to_free_low(struct hstate *h, unsigned long count,
  1219. nodemask_t *nodes_allowed)
  1220. {
  1221. int i;
  1222. if (h->order >= MAX_ORDER)
  1223. return;
  1224. for_each_node_mask(i, *nodes_allowed) {
  1225. struct page *page, *next;
  1226. struct list_head *freel = &h->hugepage_freelists[i];
  1227. list_for_each_entry_safe(page, next, freel, lru) {
  1228. if (count >= h->nr_huge_pages)
  1229. return;
  1230. if (PageHighMem(page))
  1231. continue;
  1232. list_del(&page->lru);
  1233. update_and_free_page(h, page);
  1234. h->free_huge_pages--;
  1235. h->free_huge_pages_node[page_to_nid(page)]--;
  1236. }
  1237. }
  1238. }
  1239. #else
  1240. static inline void try_to_free_low(struct hstate *h, unsigned long count,
  1241. nodemask_t *nodes_allowed)
  1242. {
  1243. }
  1244. #endif
  1245. /*
  1246. * Increment or decrement surplus_huge_pages. Keep node-specific counters
  1247. * balanced by operating on them in a round-robin fashion.
  1248. * Returns 1 if an adjustment was made.
  1249. */
  1250. static int adjust_pool_surplus(struct hstate *h, nodemask_t *nodes_allowed,
  1251. int delta)
  1252. {
  1253. int nr_nodes, node;
  1254. VM_BUG_ON(delta != -1 && delta != 1);
  1255. if (delta < 0) {
  1256. for_each_node_mask_to_alloc(h, nr_nodes, node, nodes_allowed) {
  1257. if (h->surplus_huge_pages_node[node])
  1258. goto found;
  1259. }
  1260. } else {
  1261. for_each_node_mask_to_free(h, nr_nodes, node, nodes_allowed) {
  1262. if (h->surplus_huge_pages_node[node] <
  1263. h->nr_huge_pages_node[node])
  1264. goto found;
  1265. }
  1266. }
  1267. return 0;
  1268. found:
  1269. h->surplus_huge_pages += delta;
  1270. h->surplus_huge_pages_node[node] += delta;
  1271. return 1;
  1272. }
  1273. #define persistent_huge_pages(h) (h->nr_huge_pages - h->surplus_huge_pages)
  1274. static unsigned long set_max_huge_pages(struct hstate *h, unsigned long count,
  1275. nodemask_t *nodes_allowed)
  1276. {
  1277. unsigned long min_count, ret;
  1278. if (h->order >= MAX_ORDER)
  1279. return h->max_huge_pages;
  1280. /*
  1281. * Increase the pool size
  1282. * First take pages out of surplus state. Then make up the
  1283. * remaining difference by allocating fresh huge pages.
  1284. *
  1285. * We might race with alloc_buddy_huge_page() here and be unable
  1286. * to convert a surplus huge page to a normal huge page. That is
  1287. * not critical, though, it just means the overall size of the
  1288. * pool might be one hugepage larger than it needs to be, but
  1289. * within all the constraints specified by the sysctls.
  1290. */
  1291. spin_lock(&hugetlb_lock);
  1292. while (h->surplus_huge_pages && count > persistent_huge_pages(h)) {
  1293. if (!adjust_pool_surplus(h, nodes_allowed, -1))
  1294. break;
  1295. }
  1296. while (count > persistent_huge_pages(h)) {
  1297. /*
  1298. * If this allocation races such that we no longer need the
  1299. * page, free_huge_page will handle it by freeing the page
  1300. * and reducing the surplus.
  1301. */
  1302. spin_unlock(&hugetlb_lock);
  1303. ret = alloc_fresh_huge_page(h, nodes_allowed);
  1304. spin_lock(&hugetlb_lock);
  1305. if (!ret)
  1306. goto out;
  1307. /* Bail for signals. Probably ctrl-c from user */
  1308. if (signal_pending(current))
  1309. goto out;
  1310. }
  1311. /*
  1312. * Decrease the pool size
  1313. * First return free pages to the buddy allocator (being careful
  1314. * to keep enough around to satisfy reservations). Then place
  1315. * pages into surplus state as needed so the pool will shrink
  1316. * to the desired size as pages become free.
  1317. *
  1318. * By placing pages into the surplus state independent of the
  1319. * overcommit value, we are allowing the surplus pool size to
  1320. * exceed overcommit. There are few sane options here. Since
  1321. * alloc_buddy_huge_page() is checking the global counter,
  1322. * though, we'll note that we're not allowed to exceed surplus
  1323. * and won't grow the pool anywhere else. Not until one of the
  1324. * sysctls are changed, or the surplus pages go out of use.
  1325. */
  1326. min_count = h->resv_huge_pages + h->nr_huge_pages - h->free_huge_pages;
  1327. min_count = max(count, min_count);
  1328. try_to_free_low(h, min_count, nodes_allowed);
  1329. while (min_count < persistent_huge_pages(h)) {
  1330. if (!free_pool_huge_page(h, nodes_allowed, 0))
  1331. break;
  1332. }
  1333. while (count < persistent_huge_pages(h)) {
  1334. if (!adjust_pool_surplus(h, nodes_allowed, 1))
  1335. break;
  1336. }
  1337. out:
  1338. ret = persistent_huge_pages(h);
  1339. spin_unlock(&hugetlb_lock);
  1340. return ret;
  1341. }
  1342. #define HSTATE_ATTR_RO(_name) \
  1343. static struct kobj_attribute _name##_attr = __ATTR_RO(_name)
  1344. #define HSTATE_ATTR(_name) \
  1345. static struct kobj_attribute _name##_attr = \
  1346. __ATTR(_name, 0644, _name##_show, _name##_store)
  1347. static struct kobject *hugepages_kobj;
  1348. static struct kobject *hstate_kobjs[HUGE_MAX_HSTATE];
  1349. static struct hstate *kobj_to_node_hstate(struct kobject *kobj, int *nidp);
  1350. static struct hstate *kobj_to_hstate(struct kobject *kobj, int *nidp)
  1351. {
  1352. int i;
  1353. for (i = 0; i < HUGE_MAX_HSTATE; i++)
  1354. if (hstate_kobjs[i] == kobj) {
  1355. if (nidp)
  1356. *nidp = NUMA_NO_NODE;
  1357. return &hstates[i];
  1358. }
  1359. return kobj_to_node_hstate(kobj, nidp);
  1360. }
  1361. static ssize_t nr_hugepages_show_common(struct kobject *kobj,
  1362. struct kobj_attribute *attr, char *buf)
  1363. {
  1364. struct hstate *h;
  1365. unsigned long nr_huge_pages;
  1366. int nid;
  1367. h = kobj_to_hstate(kobj, &nid);
  1368. if (nid == NUMA_NO_NODE)
  1369. nr_huge_pages = h->nr_huge_pages;
  1370. else
  1371. nr_huge_pages = h->nr_huge_pages_node[nid];
  1372. return sprintf(buf, "%lu\n", nr_huge_pages);
  1373. }
  1374. static ssize_t nr_hugepages_store_common(bool obey_mempolicy,
  1375. struct kobject *kobj, struct kobj_attribute *attr,
  1376. const char *buf, size_t len)
  1377. {
  1378. int err;
  1379. int nid;
  1380. unsigned long count;
  1381. struct hstate *h;
  1382. NODEMASK_ALLOC(nodemask_t, nodes_allowed, GFP_KERNEL | __GFP_NORETRY);
  1383. err = kstrtoul(buf, 10, &count);
  1384. if (err)
  1385. goto out;
  1386. h = kobj_to_hstate(kobj, &nid);
  1387. if (h->order >= MAX_ORDER) {
  1388. err = -EINVAL;
  1389. goto out;
  1390. }
  1391. if (nid == NUMA_NO_NODE) {
  1392. /*
  1393. * global hstate attribute
  1394. */
  1395. if (!(obey_mempolicy &&
  1396. init_nodemask_of_mempolicy(nodes_allowed))) {
  1397. NODEMASK_FREE(nodes_allowed);
  1398. nodes_allowed = &node_states[N_MEMORY];
  1399. }
  1400. } else if (nodes_allowed) {
  1401. /*
  1402. * per node hstate attribute: adjust count to global,
  1403. * but restrict alloc/free to the specified node.
  1404. */
  1405. count += h->nr_huge_pages - h->nr_huge_pages_node[nid];
  1406. init_nodemask_of_node(nodes_allowed, nid);
  1407. } else
  1408. nodes_allowed = &node_states[N_MEMORY];
  1409. h->max_huge_pages = set_max_huge_pages(h, count, nodes_allowed);
  1410. if (nodes_allowed != &node_states[N_MEMORY])
  1411. NODEMASK_FREE(nodes_allowed);
  1412. return len;
  1413. out:
  1414. NODEMASK_FREE(nodes_allowed);
  1415. return err;
  1416. }
  1417. static ssize_t nr_hugepages_show(struct kobject *kobj,
  1418. struct kobj_attribute *attr, char *buf)
  1419. {
  1420. return nr_hugepages_show_common(kobj, attr, buf);
  1421. }
  1422. static ssize_t nr_hugepages_store(struct kobject *kobj,
  1423. struct kobj_attribute *attr, const char *buf, size_t len)
  1424. {
  1425. return nr_hugepages_store_common(false, kobj, attr, buf, len);
  1426. }
  1427. HSTATE_ATTR(nr_hugepages);
  1428. #ifdef CONFIG_NUMA
  1429. /*
  1430. * hstate attribute for optionally mempolicy-based constraint on persistent
  1431. * huge page alloc/free.
  1432. */
  1433. static ssize_t nr_hugepages_mempolicy_show(struct kobject *kobj,
  1434. struct kobj_attribute *attr, char *buf)
  1435. {
  1436. return nr_hugepages_show_common(kobj, attr, buf);
  1437. }
  1438. static ssize_t nr_hugepages_mempolicy_store(struct kobject *kobj,
  1439. struct kobj_attribute *attr, const char *buf, size_t len)
  1440. {
  1441. return nr_hugepages_store_common(true, kobj, attr, buf, len);
  1442. }
  1443. HSTATE_ATTR(nr_hugepages_mempolicy);
  1444. #endif
  1445. static ssize_t nr_overcommit_hugepages_show(struct kobject *kobj,
  1446. struct kobj_attribute *attr, char *buf)
  1447. {
  1448. struct hstate *h = kobj_to_hstate(kobj, NULL);
  1449. return sprintf(buf, "%lu\n", h->nr_overcommit_huge_pages);
  1450. }
  1451. static ssize_t nr_overcommit_hugepages_store(struct kobject *kobj,
  1452. struct kobj_attribute *attr, const char *buf, size_t count)
  1453. {
  1454. int err;
  1455. unsigned long input;
  1456. struct hstate *h = kobj_to_hstate(kobj, NULL);
  1457. if (h->order >= MAX_ORDER)
  1458. return -EINVAL;
  1459. err = kstrtoul(buf, 10, &input);
  1460. if (err)
  1461. return err;
  1462. spin_lock(&hugetlb_lock);
  1463. h->nr_overcommit_huge_pages = input;
  1464. spin_unlock(&hugetlb_lock);
  1465. return count;
  1466. }
  1467. HSTATE_ATTR(nr_overcommit_hugepages);
  1468. static ssize_t free_hugepages_show(struct kobject *kobj,
  1469. struct kobj_attribute *attr, char *buf)
  1470. {
  1471. struct hstate *h;
  1472. unsigned long free_huge_pages;
  1473. int nid;
  1474. h = kobj_to_hstate(kobj, &nid);
  1475. if (nid == NUMA_NO_NODE)
  1476. free_huge_pages = h->free_huge_pages;
  1477. else
  1478. free_huge_pages = h->free_huge_pages_node[nid];
  1479. return sprintf(buf, "%lu\n", free_huge_pages);
  1480. }
  1481. HSTATE_ATTR_RO(free_hugepages);
  1482. static ssize_t resv_hugepages_show(struct kobject *kobj,
  1483. struct kobj_attribute *attr, char *buf)
  1484. {
  1485. struct hstate *h = kobj_to_hstate(kobj, NULL);
  1486. return sprintf(buf, "%lu\n", h->resv_huge_pages);
  1487. }
  1488. HSTATE_ATTR_RO(resv_hugepages);
  1489. static ssize_t surplus_hugepages_show(struct kobject *kobj,
  1490. struct kobj_attribute *attr, char *buf)
  1491. {
  1492. struct hstate *h;
  1493. unsigned long surplus_huge_pages;
  1494. int nid;
  1495. h = kobj_to_hstate(kobj, &nid);
  1496. if (nid == NUMA_NO_NODE)
  1497. surplus_huge_pages = h->surplus_huge_pages;
  1498. else
  1499. surplus_huge_pages = h->surplus_huge_pages_node[nid];
  1500. return sprintf(buf, "%lu\n", surplus_huge_pages);
  1501. }
  1502. HSTATE_ATTR_RO(surplus_hugepages);
  1503. static struct attribute *hstate_attrs[] = {
  1504. &nr_hugepages_attr.attr,
  1505. &nr_overcommit_hugepages_attr.attr,
  1506. &free_hugepages_attr.attr,
  1507. &resv_hugepages_attr.attr,
  1508. &surplus_hugepages_attr.attr,
  1509. #ifdef CONFIG_NUMA
  1510. &nr_hugepages_mempolicy_attr.attr,
  1511. #endif
  1512. NULL,
  1513. };
  1514. static struct attribute_group hstate_attr_group = {
  1515. .attrs = hstate_attrs,
  1516. };
  1517. static int hugetlb_sysfs_add_hstate(struct hstate *h, struct kobject *parent,
  1518. struct kobject **hstate_kobjs,
  1519. struct attribute_group *hstate_attr_group)
  1520. {
  1521. int retval;
  1522. int hi = hstate_index(h);
  1523. hstate_kobjs[hi] = kobject_create_and_add(h->name, parent);
  1524. if (!hstate_kobjs[hi])
  1525. return -ENOMEM;
  1526. retval = sysfs_create_group(hstate_kobjs[hi], hstate_attr_group);
  1527. if (retval)
  1528. kobject_put(hstate_kobjs[hi]);
  1529. return retval;
  1530. }
  1531. static void __init hugetlb_sysfs_init(void)
  1532. {
  1533. struct hstate *h;
  1534. int err;
  1535. hugepages_kobj = kobject_create_and_add("hugepages", mm_kobj);
  1536. if (!hugepages_kobj)
  1537. return;
  1538. for_each_hstate(h) {
  1539. err = hugetlb_sysfs_add_hstate(h, hugepages_kobj,
  1540. hstate_kobjs, &hstate_attr_group);
  1541. if (err)
  1542. pr_err("Hugetlb: Unable to add hstate %s", h->name);
  1543. }
  1544. }
  1545. #ifdef CONFIG_NUMA
  1546. /*
  1547. * node_hstate/s - associate per node hstate attributes, via their kobjects,
  1548. * with node devices in node_devices[] using a parallel array. The array
  1549. * index of a node device or _hstate == node id.
  1550. * This is here to avoid any static dependency of the node device driver, in
  1551. * the base kernel, on the hugetlb module.
  1552. */
  1553. struct node_hstate {
  1554. struct kobject *hugepages_kobj;
  1555. struct kobject *hstate_kobjs[HUGE_MAX_HSTATE];
  1556. };
  1557. struct node_hstate node_hstates[MAX_NUMNODES];
  1558. /*
  1559. * A subset of global hstate attributes for node devices
  1560. */
  1561. static struct attribute *per_node_hstate_attrs[] = {
  1562. &nr_hugepages_attr.attr,
  1563. &free_hugepages_attr.attr,
  1564. &surplus_hugepages_attr.attr,
  1565. NULL,
  1566. };
  1567. static struct attribute_group per_node_hstate_attr_group = {
  1568. .attrs = per_node_hstate_attrs,
  1569. };
  1570. /*
  1571. * kobj_to_node_hstate - lookup global hstate for node device hstate attr kobj.
  1572. * Returns node id via non-NULL nidp.
  1573. */
  1574. static struct hstate *kobj_to_node_hstate(struct kobject *kobj, int *nidp)
  1575. {
  1576. int nid;
  1577. for (nid = 0; nid < nr_node_ids; nid++) {
  1578. struct node_hstate *nhs = &node_hstates[nid];
  1579. int i;
  1580. for (i = 0; i < HUGE_MAX_HSTATE; i++)
  1581. if (nhs->hstate_kobjs[i] == kobj) {
  1582. if (nidp)
  1583. *nidp = nid;
  1584. return &hstates[i];
  1585. }
  1586. }
  1587. BUG();
  1588. return NULL;
  1589. }
  1590. /*
  1591. * Unregister hstate attributes from a single node device.
  1592. * No-op if no hstate attributes attached.
  1593. */
  1594. static void hugetlb_unregister_node(struct node *node)
  1595. {
  1596. struct hstate *h;
  1597. struct node_hstate *nhs = &node_hstates[node->dev.id];
  1598. if (!nhs->hugepages_kobj)
  1599. return; /* no hstate attributes */
  1600. for_each_hstate(h) {
  1601. int idx = hstate_index(h);
  1602. if (nhs->hstate_kobjs[idx]) {
  1603. kobject_put(nhs->hstate_kobjs[idx]);
  1604. nhs->hstate_kobjs[idx] = NULL;
  1605. }
  1606. }
  1607. kobject_put(nhs->hugepages_kobj);
  1608. nhs->hugepages_kobj = NULL;
  1609. }
  1610. /*
  1611. * hugetlb module exit: unregister hstate attributes from node devices
  1612. * that have them.
  1613. */
  1614. static void hugetlb_unregister_all_nodes(void)
  1615. {
  1616. int nid;
  1617. /*
  1618. * disable node device registrations.
  1619. */
  1620. register_hugetlbfs_with_node(NULL, NULL);
  1621. /*
  1622. * remove hstate attributes from any nodes that have them.
  1623. */
  1624. for (nid = 0; nid < nr_node_ids; nid++)
  1625. hugetlb_unregister_node(node_devices[nid]);
  1626. }
  1627. /*
  1628. * Register hstate attributes for a single node device.
  1629. * No-op if attributes already registered.
  1630. */
  1631. static void hugetlb_register_node(struct node *node)
  1632. {
  1633. struct hstate *h;
  1634. struct node_hstate *nhs = &node_hstates[node->dev.id];
  1635. int err;
  1636. if (nhs->hugepages_kobj)
  1637. return; /* already allocated */
  1638. nhs->hugepages_kobj = kobject_create_and_add("hugepages",
  1639. &node->dev.kobj);
  1640. if (!nhs->hugepages_kobj)
  1641. return;
  1642. for_each_hstate(h) {
  1643. err = hugetlb_sysfs_add_hstate(h, nhs->hugepages_kobj,
  1644. nhs->hstate_kobjs,
  1645. &per_node_hstate_attr_group);
  1646. if (err) {
  1647. pr_err("Hugetlb: Unable to add hstate %s for node %d\n",
  1648. h->name, node->dev.id);
  1649. hugetlb_unregister_node(node);
  1650. break;
  1651. }
  1652. }
  1653. }
  1654. /*
  1655. * hugetlb init time: register hstate attributes for all registered node
  1656. * devices of nodes that have memory. All on-line nodes should have
  1657. * registered their associated device by this time.
  1658. */
  1659. static void hugetlb_register_all_nodes(void)
  1660. {
  1661. int nid;
  1662. for_each_node_state(nid, N_MEMORY) {
  1663. struct node *node = node_devices[nid];
  1664. if (node->dev.id == nid)
  1665. hugetlb_register_node(node);
  1666. }
  1667. /*
  1668. * Let the node device driver know we're here so it can
  1669. * [un]register hstate attributes on node hotplug.
  1670. */
  1671. register_hugetlbfs_with_node(hugetlb_register_node,
  1672. hugetlb_unregister_node);
  1673. }
  1674. #else /* !CONFIG_NUMA */
  1675. static struct hstate *kobj_to_node_hstate(struct kobject *kobj, int *nidp)
  1676. {
  1677. BUG();
  1678. if (nidp)
  1679. *nidp = -1;
  1680. return NULL;
  1681. }
  1682. static void hugetlb_unregister_all_nodes(void) { }
  1683. static void hugetlb_register_all_nodes(void) { }
  1684. #endif
  1685. static void __exit hugetlb_exit(void)
  1686. {
  1687. struct hstate *h;
  1688. hugetlb_unregister_all_nodes();
  1689. for_each_hstate(h) {
  1690. kobject_put(hstate_kobjs[hstate_index(h)]);
  1691. }
  1692. kobject_put(hugepages_kobj);
  1693. }
  1694. module_exit(hugetlb_exit);
  1695. static int __init hugetlb_init(void)
  1696. {
  1697. /* Some platform decide whether they support huge pages at boot
  1698. * time. On these, such as powerpc, HPAGE_SHIFT is set to 0 when
  1699. * there is no such support
  1700. */
  1701. if (HPAGE_SHIFT == 0)
  1702. return 0;
  1703. if (!size_to_hstate(default_hstate_size)) {
  1704. default_hstate_size = HPAGE_SIZE;
  1705. if (!size_to_hstate(default_hstate_size))
  1706. hugetlb_add_hstate(HUGETLB_PAGE_ORDER);
  1707. }
  1708. default_hstate_idx = hstate_index(size_to_hstate(default_hstate_size));
  1709. if (default_hstate_max_huge_pages)
  1710. default_hstate.max_huge_pages = default_hstate_max_huge_pages;
  1711. hugetlb_init_hstates();
  1712. gather_bootmem_prealloc();
  1713. report_hugepages();
  1714. hugetlb_sysfs_init();
  1715. hugetlb_register_all_nodes();
  1716. hugetlb_cgroup_file_init();
  1717. return 0;
  1718. }
  1719. module_init(hugetlb_init);
  1720. /* Should be called on processing a hugepagesz=... option */
  1721. void __init hugetlb_add_hstate(unsigned order)
  1722. {
  1723. struct hstate *h;
  1724. unsigned long i;
  1725. if (size_to_hstate(PAGE_SIZE << order)) {
  1726. pr_warning("hugepagesz= specified twice, ignoring\n");
  1727. return;
  1728. }
  1729. BUG_ON(hugetlb_max_hstate >= HUGE_MAX_HSTATE);
  1730. BUG_ON(order == 0);
  1731. h = &hstates[hugetlb_max_hstate++];
  1732. h->order = order;
  1733. h->mask = ~((1ULL << (order + PAGE_SHIFT)) - 1);
  1734. h->nr_huge_pages = 0;
  1735. h->free_huge_pages = 0;
  1736. for (i = 0; i < MAX_NUMNODES; ++i)
  1737. INIT_LIST_HEAD(&h->hugepage_freelists[i]);
  1738. INIT_LIST_HEAD(&h->hugepage_activelist);
  1739. h->next_nid_to_alloc = first_node(node_states[N_MEMORY]);
  1740. h->next_nid_to_free = first_node(node_states[N_MEMORY]);
  1741. snprintf(h->name, HSTATE_NAME_LEN, "hugepages-%lukB",
  1742. huge_page_size(h)/1024);
  1743. parsed_hstate = h;
  1744. }
  1745. static int __init hugetlb_nrpages_setup(char *s)
  1746. {
  1747. unsigned long *mhp;
  1748. static unsigned long *last_mhp;
  1749. /*
  1750. * !hugetlb_max_hstate means we haven't parsed a hugepagesz= parameter yet,
  1751. * so this hugepages= parameter goes to the "default hstate".
  1752. */
  1753. if (!hugetlb_max_hstate)
  1754. mhp = &default_hstate_max_huge_pages;
  1755. else
  1756. mhp = &parsed_hstate->max_huge_pages;
  1757. if (mhp == last_mhp) {
  1758. pr_warning("hugepages= specified twice without "
  1759. "interleaving hugepagesz=, ignoring\n");
  1760. return 1;
  1761. }
  1762. if (sscanf(s, "%lu", mhp) <= 0)
  1763. *mhp = 0;
  1764. /*
  1765. * Global state is always initialized later in hugetlb_init.
  1766. * But we need to allocate >= MAX_ORDER hstates here early to still
  1767. * use the bootmem allocator.
  1768. */
  1769. if (hugetlb_max_hstate && parsed_hstate->order >= MAX_ORDER)
  1770. hugetlb_hstate_alloc_pages(parsed_hstate);
  1771. last_mhp = mhp;
  1772. return 1;
  1773. }
  1774. __setup("hugepages=", hugetlb_nrpages_setup);
  1775. static int __init hugetlb_default_setup(char *s)
  1776. {
  1777. default_hstate_size = memparse(s, &s);
  1778. return 1;
  1779. }
  1780. __setup("default_hugepagesz=", hugetlb_default_setup);
  1781. static unsigned int cpuset_mems_nr(unsigned int *array)
  1782. {
  1783. int node;
  1784. unsigned int nr = 0;
  1785. for_each_node_mask(node, cpuset_current_mems_allowed)
  1786. nr += array[node];
  1787. return nr;
  1788. }
  1789. #ifdef CONFIG_SYSCTL
  1790. static int hugetlb_sysctl_handler_common(bool obey_mempolicy,
  1791. struct ctl_table *table, int write,
  1792. void __user *buffer, size_t *length, loff_t *ppos)
  1793. {
  1794. struct hstate *h = &default_hstate;
  1795. unsigned long tmp;
  1796. int ret;
  1797. tmp = h->max_huge_pages;
  1798. if (write && h->order >= MAX_ORDER)
  1799. return -EINVAL;
  1800. table->data = &tmp;
  1801. table->maxlen = sizeof(unsigned long);
  1802. ret = proc_doulongvec_minmax(table, write, buffer, length, ppos);
  1803. if (ret)
  1804. goto out;
  1805. if (write) {
  1806. NODEMASK_ALLOC(nodemask_t, nodes_allowed,
  1807. GFP_KERNEL | __GFP_NORETRY);
  1808. if (!(obey_mempolicy &&
  1809. init_nodemask_of_mempolicy(nodes_allowed))) {
  1810. NODEMASK_FREE(nodes_allowed);
  1811. nodes_allowed = &node_states[N_MEMORY];
  1812. }
  1813. h->max_huge_pages = set_max_huge_pages(h, tmp, nodes_allowed);
  1814. if (nodes_allowed != &node_states[N_MEMORY])
  1815. NODEMASK_FREE(nodes_allowed);
  1816. }
  1817. out:
  1818. return ret;
  1819. }
  1820. int hugetlb_sysctl_handler(struct ctl_table *table, int write,
  1821. void __user *buffer, size_t *length, loff_t *ppos)
  1822. {
  1823. return hugetlb_sysctl_handler_common(false, table, write,
  1824. buffer, length, ppos);
  1825. }
  1826. #ifdef CONFIG_NUMA
  1827. int hugetlb_mempolicy_sysctl_handler(struct ctl_table *table, int write,
  1828. void __user *buffer, size_t *length, loff_t *ppos)
  1829. {
  1830. return hugetlb_sysctl_handler_common(true, table, write,
  1831. buffer, length, ppos);
  1832. }
  1833. #endif /* CONFIG_NUMA */
  1834. int hugetlb_overcommit_handler(struct ctl_table *table, int write,
  1835. void __user *buffer,
  1836. size_t *length, loff_t *ppos)
  1837. {
  1838. struct hstate *h = &default_hstate;
  1839. unsigned long tmp;
  1840. int ret;
  1841. tmp = h->nr_overcommit_huge_pages;
  1842. if (write && h->order >= MAX_ORDER)
  1843. return -EINVAL;
  1844. table->data = &tmp;
  1845. table->maxlen = sizeof(unsigned long);
  1846. ret = proc_doulongvec_minmax(table, write, buffer, length, ppos);
  1847. if (ret)
  1848. goto out;
  1849. if (write) {
  1850. spin_lock(&hugetlb_lock);
  1851. h->nr_overcommit_huge_pages = tmp;
  1852. spin_unlock(&hugetlb_lock);
  1853. }
  1854. out:
  1855. return ret;
  1856. }
  1857. #endif /* CONFIG_SYSCTL */
  1858. void hugetlb_report_meminfo(struct seq_file *m)
  1859. {
  1860. struct hstate *h = &default_hstate;
  1861. seq_printf(m,
  1862. "HugePages_Total: %5lu\n"
  1863. "HugePages_Free: %5lu\n"
  1864. "HugePages_Rsvd: %5lu\n"
  1865. "HugePages_Surp: %5lu\n"
  1866. "Hugepagesize: %8lu kB\n",
  1867. h->nr_huge_pages,
  1868. h->free_huge_pages,
  1869. h->resv_huge_pages,
  1870. h->surplus_huge_pages,
  1871. 1UL << (huge_page_order(h) + PAGE_SHIFT - 10));
  1872. }
  1873. int hugetlb_report_node_meminfo(int nid, char *buf)
  1874. {
  1875. struct hstate *h = &default_hstate;
  1876. return sprintf(buf,
  1877. "Node %d HugePages_Total: %5u\n"
  1878. "Node %d HugePages_Free: %5u\n"
  1879. "Node %d HugePages_Surp: %5u\n",
  1880. nid, h->nr_huge_pages_node[nid],
  1881. nid, h->free_huge_pages_node[nid],
  1882. nid, h->surplus_huge_pages_node[nid]);
  1883. }
  1884. void hugetlb_show_meminfo(void)
  1885. {
  1886. struct hstate *h;
  1887. int nid;
  1888. for_each_node_state(nid, N_MEMORY)
  1889. for_each_hstate(h)
  1890. pr_info("Node %d hugepages_total=%u hugepages_free=%u hugepages_surp=%u hugepages_size=%lukB\n",
  1891. nid,
  1892. h->nr_huge_pages_node[nid],
  1893. h->free_huge_pages_node[nid],
  1894. h->surplus_huge_pages_node[nid],
  1895. 1UL << (huge_page_order(h) + PAGE_SHIFT - 10));
  1896. }
  1897. /* Return the number pages of memory we physically have, in PAGE_SIZE units. */
  1898. unsigned long hugetlb_total_pages(void)
  1899. {
  1900. struct hstate *h;
  1901. unsigned long nr_total_pages = 0;
  1902. for_each_hstate(h)
  1903. nr_total_pages += h->nr_huge_pages * pages_per_huge_page(h);
  1904. return nr_total_pages;
  1905. }
  1906. static int hugetlb_acct_memory(struct hstate *h, long delta)
  1907. {
  1908. int ret = -ENOMEM;
  1909. spin_lock(&hugetlb_lock);
  1910. /*
  1911. * When cpuset is configured, it breaks the strict hugetlb page
  1912. * reservation as the accounting is done on a global variable. Such
  1913. * reservation is completely rubbish in the presence of cpuset because
  1914. * the reservation is not checked against page availability for the
  1915. * current cpuset. Application can still potentially OOM'ed by kernel
  1916. * with lack of free htlb page in cpuset that the task is in.
  1917. * Attempt to enforce strict accounting with cpuset is almost
  1918. * impossible (or too ugly) because cpuset is too fluid that
  1919. * task or memory node can be dynamically moved between cpusets.
  1920. *
  1921. * The change of semantics for shared hugetlb mapping with cpuset is
  1922. * undesirable. However, in order to preserve some of the semantics,
  1923. * we fall back to check against current free page availability as
  1924. * a best attempt and hopefully to minimize the impact of changing
  1925. * semantics that cpuset has.
  1926. */
  1927. if (delta > 0) {
  1928. if (gather_surplus_pages(h, delta) < 0)
  1929. goto out;
  1930. if (delta > cpuset_mems_nr(h->free_huge_pages_node)) {
  1931. return_unused_surplus_pages(h, delta);
  1932. goto out;
  1933. }
  1934. }
  1935. ret = 0;
  1936. if (delta < 0)
  1937. return_unused_surplus_pages(h, (unsigned long) -delta);
  1938. out:
  1939. spin_unlock(&hugetlb_lock);
  1940. return ret;
  1941. }
  1942. static void hugetlb_vm_op_open(struct vm_area_struct *vma)
  1943. {
  1944. struct resv_map *resv = vma_resv_map(vma);
  1945. /*
  1946. * This new VMA should share its siblings reservation map if present.
  1947. * The VMA will only ever have a valid reservation map pointer where
  1948. * it is being copied for another still existing VMA. As that VMA
  1949. * has a reference to the reservation map it cannot disappear until
  1950. * after this open call completes. It is therefore safe to take a
  1951. * new reference here without additional locking.
  1952. */
  1953. if (resv)
  1954. kref_get(&resv->refs);
  1955. }
  1956. static void resv_map_put(struct vm_area_struct *vma)
  1957. {
  1958. struct resv_map *resv = vma_resv_map(vma);
  1959. if (!resv)
  1960. return;
  1961. kref_put(&resv->refs, resv_map_release);
  1962. }
  1963. static void hugetlb_vm_op_close(struct vm_area_struct *vma)
  1964. {
  1965. struct hstate *h = hstate_vma(vma);
  1966. struct resv_map *resv = vma_resv_map(vma);
  1967. struct hugepage_subpool *spool = subpool_vma(vma);
  1968. unsigned long reserve;
  1969. unsigned long start;
  1970. unsigned long end;
  1971. if (resv) {
  1972. start = vma_hugecache_offset(h, vma, vma->vm_start);
  1973. end = vma_hugecache_offset(h, vma, vma->vm_end);
  1974. reserve = (end - start) -
  1975. region_count(&resv->regions, start, end);
  1976. resv_map_put(vma);
  1977. if (reserve) {
  1978. hugetlb_acct_memory(h, -reserve);
  1979. hugepage_subpool_put_pages(spool, reserve);
  1980. }
  1981. }
  1982. }
  1983. /*
  1984. * We cannot handle pagefaults against hugetlb pages at all. They cause
  1985. * handle_mm_fault() to try to instantiate regular-sized pages in the
  1986. * hugegpage VMA. do_page_fault() is supposed to trap this, so BUG is we get
  1987. * this far.
  1988. */
  1989. static int hugetlb_vm_op_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
  1990. {
  1991. BUG();
  1992. return 0;
  1993. }
  1994. const struct vm_operations_struct hugetlb_vm_ops = {
  1995. .fault = hugetlb_vm_op_fault,
  1996. .open = hugetlb_vm_op_open,
  1997. .close = hugetlb_vm_op_close,
  1998. };
  1999. static pte_t make_huge_pte(struct vm_area_struct *vma, struct page *page,
  2000. int writable)
  2001. {
  2002. pte_t entry;
  2003. if (writable) {
  2004. entry = huge_pte_mkwrite(huge_pte_mkdirty(mk_huge_pte(page,
  2005. vma->vm_page_prot)));
  2006. } else {
  2007. entry = huge_pte_wrprotect(mk_huge_pte(page,
  2008. vma->vm_page_prot));
  2009. }
  2010. entry = pte_mkyoung(entry);
  2011. entry = pte_mkhuge(entry);
  2012. entry = arch_make_huge_pte(entry, vma, page, writable);
  2013. return entry;
  2014. }
  2015. static void set_huge_ptep_writable(struct vm_area_struct *vma,
  2016. unsigned long address, pte_t *ptep)
  2017. {
  2018. pte_t entry;
  2019. entry = huge_pte_mkwrite(huge_pte_mkdirty(huge_ptep_get(ptep)));
  2020. if (huge_ptep_set_access_flags(vma, address, ptep, entry, 1))
  2021. update_mmu_cache(vma, address, ptep);
  2022. }
  2023. int copy_hugetlb_page_range(struct mm_struct *dst, struct mm_struct *src,
  2024. struct vm_area_struct *vma)
  2025. {
  2026. pte_t *src_pte, *dst_pte, entry;
  2027. struct page *ptepage;
  2028. unsigned long addr;
  2029. int cow;
  2030. struct hstate *h = hstate_vma(vma);
  2031. unsigned long sz = huge_page_size(h);
  2032. cow = (vma->vm_flags & (VM_SHARED | VM_MAYWRITE)) == VM_MAYWRITE;
  2033. for (addr = vma->vm_start; addr < vma->vm_end; addr += sz) {
  2034. spinlock_t *src_ptl, *dst_ptl;
  2035. src_pte = huge_pte_offset(src, addr);
  2036. if (!src_pte)
  2037. continue;
  2038. dst_pte = huge_pte_alloc(dst, addr, sz);
  2039. if (!dst_pte)
  2040. goto nomem;
  2041. /* If the pagetables are shared don't copy or take references */
  2042. if (dst_pte == src_pte)
  2043. continue;
  2044. dst_ptl = huge_pte_lock(h, dst, dst_pte);
  2045. src_ptl = huge_pte_lockptr(h, src, src_pte);
  2046. spin_lock_nested(src_ptl, SINGLE_DEPTH_NESTING);
  2047. if (!huge_pte_none(huge_ptep_get(src_pte))) {
  2048. if (cow)
  2049. huge_ptep_set_wrprotect(src, addr, src_pte);
  2050. entry = huge_ptep_get(src_pte);
  2051. ptepage = pte_page(entry);
  2052. get_page(ptepage);
  2053. page_dup_rmap(ptepage);
  2054. set_huge_pte_at(dst, addr, dst_pte, entry);
  2055. }
  2056. spin_unlock(src_ptl);
  2057. spin_unlock(dst_ptl);
  2058. }
  2059. return 0;
  2060. nomem:
  2061. return -ENOMEM;
  2062. }
  2063. static int is_hugetlb_entry_migration(pte_t pte)
  2064. {
  2065. swp_entry_t swp;
  2066. if (huge_pte_none(pte) || pte_present(pte))
  2067. return 0;
  2068. swp = pte_to_swp_entry(pte);
  2069. if (non_swap_entry(swp) && is_migration_entry(swp))
  2070. return 1;
  2071. else
  2072. return 0;
  2073. }
  2074. static int is_hugetlb_entry_hwpoisoned(pte_t pte)
  2075. {
  2076. swp_entry_t swp;
  2077. if (huge_pte_none(pte) || pte_present(pte))
  2078. return 0;
  2079. swp = pte_to_swp_entry(pte);
  2080. if (non_swap_entry(swp) && is_hwpoison_entry(swp))
  2081. return 1;
  2082. else
  2083. return 0;
  2084. }
  2085. void __unmap_hugepage_range(struct mmu_gather *tlb, struct vm_area_struct *vma,
  2086. unsigned long start, unsigned long end,
  2087. struct page *ref_page)
  2088. {
  2089. int force_flush = 0;
  2090. struct mm_struct *mm = vma->vm_mm;
  2091. unsigned long address;
  2092. pte_t *ptep;
  2093. pte_t pte;
  2094. spinlock_t *ptl;
  2095. struct page *page;
  2096. struct hstate *h = hstate_vma(vma);
  2097. unsigned long sz = huge_page_size(h);
  2098. const unsigned long mmun_start = start; /* For mmu_notifiers */
  2099. const unsigned long mmun_end = end; /* For mmu_notifiers */
  2100. WARN_ON(!is_vm_hugetlb_page(vma));
  2101. BUG_ON(start & ~huge_page_mask(h));
  2102. BUG_ON(end & ~huge_page_mask(h));
  2103. tlb_start_vma(tlb, vma);
  2104. mmu_notifier_invalidate_range_start(mm, mmun_start, mmun_end);
  2105. again:
  2106. for (address = start; address < end; address += sz) {
  2107. ptep = huge_pte_offset(mm, address);
  2108. if (!ptep)
  2109. continue;
  2110. ptl = huge_pte_lock(h, mm, ptep);
  2111. if (huge_pmd_unshare(mm, &address, ptep))
  2112. goto unlock;
  2113. pte = huge_ptep_get(ptep);
  2114. if (huge_pte_none(pte))
  2115. goto unlock;
  2116. /*
  2117. * HWPoisoned hugepage is already unmapped and dropped reference
  2118. */
  2119. if (unlikely(is_hugetlb_entry_hwpoisoned(pte))) {
  2120. huge_pte_clear(mm, address, ptep);
  2121. goto unlock;
  2122. }
  2123. page = pte_page(pte);
  2124. /*
  2125. * If a reference page is supplied, it is because a specific
  2126. * page is being unmapped, not a range. Ensure the page we
  2127. * are about to unmap is the actual page of interest.
  2128. */
  2129. if (ref_page) {
  2130. if (page != ref_page)
  2131. goto unlock;
  2132. /*
  2133. * Mark the VMA as having unmapped its page so that
  2134. * future faults in this VMA will fail rather than
  2135. * looking like data was lost
  2136. */
  2137. set_vma_resv_flags(vma, HPAGE_RESV_UNMAPPED);
  2138. }
  2139. pte = huge_ptep_get_and_clear(mm, address, ptep);
  2140. tlb_remove_tlb_entry(tlb, ptep, address);
  2141. if (huge_pte_dirty(pte))
  2142. set_page_dirty(page);
  2143. page_remove_rmap(page);
  2144. force_flush = !__tlb_remove_page(tlb, page);
  2145. if (force_flush) {
  2146. spin_unlock(ptl);
  2147. break;
  2148. }
  2149. /* Bail out after unmapping reference page if supplied */
  2150. if (ref_page) {
  2151. spin_unlock(ptl);
  2152. break;
  2153. }
  2154. unlock:
  2155. spin_unlock(ptl);
  2156. }
  2157. /*
  2158. * mmu_gather ran out of room to batch pages, we break out of
  2159. * the PTE lock to avoid doing the potential expensive TLB invalidate
  2160. * and page-free while holding it.
  2161. */
  2162. if (force_flush) {
  2163. force_flush = 0;
  2164. tlb_flush_mmu(tlb);
  2165. if (address < end && !ref_page)
  2166. goto again;
  2167. }
  2168. mmu_notifier_invalidate_range_end(mm, mmun_start, mmun_end);
  2169. tlb_end_vma(tlb, vma);
  2170. }
  2171. void __unmap_hugepage_range_final(struct mmu_gather *tlb,
  2172. struct vm_area_struct *vma, unsigned long start,
  2173. unsigned long end, struct page *ref_page)
  2174. {
  2175. __unmap_hugepage_range(tlb, vma, start, end, ref_page);
  2176. /*
  2177. * Clear this flag so that x86's huge_pmd_share page_table_shareable
  2178. * test will fail on a vma being torn down, and not grab a page table
  2179. * on its way out. We're lucky that the flag has such an appropriate
  2180. * name, and can in fact be safely cleared here. We could clear it
  2181. * before the __unmap_hugepage_range above, but all that's necessary
  2182. * is to clear it before releasing the i_mmap_mutex. This works
  2183. * because in the context this is called, the VMA is about to be
  2184. * destroyed and the i_mmap_mutex is held.
  2185. */
  2186. vma->vm_flags &= ~VM_MAYSHARE;
  2187. }
  2188. void unmap_hugepage_range(struct vm_area_struct *vma, unsigned long start,
  2189. unsigned long end, struct page *ref_page)
  2190. {
  2191. struct mm_struct *mm;
  2192. struct mmu_gather tlb;
  2193. mm = vma->vm_mm;
  2194. tlb_gather_mmu(&tlb, mm, start, end);
  2195. __unmap_hugepage_range(&tlb, vma, start, end, ref_page);
  2196. tlb_finish_mmu(&tlb, start, end);
  2197. }
  2198. /*
  2199. * This is called when the original mapper is failing to COW a MAP_PRIVATE
  2200. * mappping it owns the reserve page for. The intention is to unmap the page
  2201. * from other VMAs and let the children be SIGKILLed if they are faulting the
  2202. * same region.
  2203. */
  2204. static int unmap_ref_private(struct mm_struct *mm, struct vm_area_struct *vma,
  2205. struct page *page, unsigned long address)
  2206. {
  2207. struct hstate *h = hstate_vma(vma);
  2208. struct vm_area_struct *iter_vma;
  2209. struct address_space *mapping;
  2210. pgoff_t pgoff;
  2211. /*
  2212. * vm_pgoff is in PAGE_SIZE units, hence the different calculation
  2213. * from page cache lookup which is in HPAGE_SIZE units.
  2214. */
  2215. address = address & huge_page_mask(h);
  2216. pgoff = ((address - vma->vm_start) >> PAGE_SHIFT) +
  2217. vma->vm_pgoff;
  2218. mapping = file_inode(vma->vm_file)->i_mapping;
  2219. /*
  2220. * Take the mapping lock for the duration of the table walk. As
  2221. * this mapping should be shared between all the VMAs,
  2222. * __unmap_hugepage_range() is called as the lock is already held
  2223. */
  2224. mutex_lock(&mapping->i_mmap_mutex);
  2225. vma_interval_tree_foreach(iter_vma, &mapping->i_mmap, pgoff, pgoff) {
  2226. /* Do not unmap the current VMA */
  2227. if (iter_vma == vma)
  2228. continue;
  2229. /*
  2230. * Unmap the page from other VMAs without their own reserves.
  2231. * They get marked to be SIGKILLed if they fault in these
  2232. * areas. This is because a future no-page fault on this VMA
  2233. * could insert a zeroed page instead of the data existing
  2234. * from the time of fork. This would look like data corruption
  2235. */
  2236. if (!is_vma_resv_set(iter_vma, HPAGE_RESV_OWNER))
  2237. unmap_hugepage_range(iter_vma, address,
  2238. address + huge_page_size(h), page);
  2239. }
  2240. mutex_unlock(&mapping->i_mmap_mutex);
  2241. return 1;
  2242. }
  2243. /*
  2244. * Hugetlb_cow() should be called with page lock of the original hugepage held.
  2245. * Called with hugetlb_instantiation_mutex held and pte_page locked so we
  2246. * cannot race with other handlers or page migration.
  2247. * Keep the pte_same checks anyway to make transition from the mutex easier.
  2248. */
  2249. static int hugetlb_cow(struct mm_struct *mm, struct vm_area_struct *vma,
  2250. unsigned long address, pte_t *ptep, pte_t pte,
  2251. struct page *pagecache_page, spinlock_t *ptl)
  2252. {
  2253. struct hstate *h = hstate_vma(vma);
  2254. struct page *old_page, *new_page;
  2255. int outside_reserve = 0;
  2256. unsigned long mmun_start; /* For mmu_notifiers */
  2257. unsigned long mmun_end; /* For mmu_notifiers */
  2258. old_page = pte_page(pte);
  2259. retry_avoidcopy:
  2260. /* If no-one else is actually using this page, avoid the copy
  2261. * and just make the page writable */
  2262. if (page_mapcount(old_page) == 1 && PageAnon(old_page)) {
  2263. page_move_anon_rmap(old_page, vma, address);
  2264. set_huge_ptep_writable(vma, address, ptep);
  2265. return 0;
  2266. }
  2267. /*
  2268. * If the process that created a MAP_PRIVATE mapping is about to
  2269. * perform a COW due to a shared page count, attempt to satisfy
  2270. * the allocation without using the existing reserves. The pagecache
  2271. * page is used to determine if the reserve at this address was
  2272. * consumed or not. If reserves were used, a partial faulted mapping
  2273. * at the time of fork() could consume its reserves on COW instead
  2274. * of the full address range.
  2275. */
  2276. if (is_vma_resv_set(vma, HPAGE_RESV_OWNER) &&
  2277. old_page != pagecache_page)
  2278. outside_reserve = 1;
  2279. page_cache_get(old_page);
  2280. /* Drop page table lock as buddy allocator may be called */
  2281. spin_unlock(ptl);
  2282. new_page = alloc_huge_page(vma, address, outside_reserve);
  2283. if (IS_ERR(new_page)) {
  2284. long err = PTR_ERR(new_page);
  2285. page_cache_release(old_page);
  2286. /*
  2287. * If a process owning a MAP_PRIVATE mapping fails to COW,
  2288. * it is due to references held by a child and an insufficient
  2289. * huge page pool. To guarantee the original mappers
  2290. * reliability, unmap the page from child processes. The child
  2291. * may get SIGKILLed if it later faults.
  2292. */
  2293. if (outside_reserve) {
  2294. BUG_ON(huge_pte_none(pte));
  2295. if (unmap_ref_private(mm, vma, old_page, address)) {
  2296. BUG_ON(huge_pte_none(pte));
  2297. spin_lock(ptl);
  2298. ptep = huge_pte_offset(mm, address & huge_page_mask(h));
  2299. if (likely(pte_same(huge_ptep_get(ptep), pte)))
  2300. goto retry_avoidcopy;
  2301. /*
  2302. * race occurs while re-acquiring page table
  2303. * lock, and our job is done.
  2304. */
  2305. return 0;
  2306. }
  2307. WARN_ON_ONCE(1);
  2308. }
  2309. /* Caller expects lock to be held */
  2310. spin_lock(ptl);
  2311. if (err == -ENOMEM)
  2312. return VM_FAULT_OOM;
  2313. else
  2314. return VM_FAULT_SIGBUS;
  2315. }
  2316. /*
  2317. * When the original hugepage is shared one, it does not have
  2318. * anon_vma prepared.
  2319. */
  2320. if (unlikely(anon_vma_prepare(vma))) {
  2321. page_cache_release(new_page);
  2322. page_cache_release(old_page);
  2323. /* Caller expects lock to be held */
  2324. spin_lock(ptl);
  2325. return VM_FAULT_OOM;
  2326. }
  2327. copy_user_huge_page(new_page, old_page, address, vma,
  2328. pages_per_huge_page(h));
  2329. __SetPageUptodate(new_page);
  2330. mmun_start = address & huge_page_mask(h);
  2331. mmun_end = mmun_start + huge_page_size(h);
  2332. mmu_notifier_invalidate_range_start(mm, mmun_start, mmun_end);
  2333. /*
  2334. * Retake the page table lock to check for racing updates
  2335. * before the page tables are altered
  2336. */
  2337. spin_lock(ptl);
  2338. ptep = huge_pte_offset(mm, address & huge_page_mask(h));
  2339. if (likely(pte_same(huge_ptep_get(ptep), pte))) {
  2340. ClearPagePrivate(new_page);
  2341. /* Break COW */
  2342. huge_ptep_clear_flush(vma, address, ptep);
  2343. set_huge_pte_at(mm, address, ptep,
  2344. make_huge_pte(vma, new_page, 1));
  2345. page_remove_rmap(old_page);
  2346. hugepage_add_new_anon_rmap(new_page, vma, address);
  2347. /* Make the old page be freed below */
  2348. new_page = old_page;
  2349. }
  2350. spin_unlock(ptl);
  2351. mmu_notifier_invalidate_range_end(mm, mmun_start, mmun_end);
  2352. page_cache_release(new_page);
  2353. page_cache_release(old_page);
  2354. /* Caller expects lock to be held */
  2355. spin_lock(ptl);
  2356. return 0;
  2357. }
  2358. /* Return the pagecache page at a given address within a VMA */
  2359. static struct page *hugetlbfs_pagecache_page(struct hstate *h,
  2360. struct vm_area_struct *vma, unsigned long address)
  2361. {
  2362. struct address_space *mapping;
  2363. pgoff_t idx;
  2364. mapping = vma->vm_file->f_mapping;
  2365. idx = vma_hugecache_offset(h, vma, address);
  2366. return find_lock_page(mapping, idx);
  2367. }
  2368. /*
  2369. * Return whether there is a pagecache page to back given address within VMA.
  2370. * Caller follow_hugetlb_page() holds page_table_lock so we cannot lock_page.
  2371. */
  2372. static bool hugetlbfs_pagecache_present(struct hstate *h,
  2373. struct vm_area_struct *vma, unsigned long address)
  2374. {
  2375. struct address_space *mapping;
  2376. pgoff_t idx;
  2377. struct page *page;
  2378. mapping = vma->vm_file->f_mapping;
  2379. idx = vma_hugecache_offset(h, vma, address);
  2380. page = find_get_page(mapping, idx);
  2381. if (page)
  2382. put_page(page);
  2383. return page != NULL;
  2384. }
  2385. static int hugetlb_no_page(struct mm_struct *mm, struct vm_area_struct *vma,
  2386. unsigned long address, pte_t *ptep, unsigned int flags)
  2387. {
  2388. struct hstate *h = hstate_vma(vma);
  2389. int ret = VM_FAULT_SIGBUS;
  2390. int anon_rmap = 0;
  2391. pgoff_t idx;
  2392. unsigned long size;
  2393. struct page *page;
  2394. struct address_space *mapping;
  2395. pte_t new_pte;
  2396. spinlock_t *ptl;
  2397. /*
  2398. * Currently, we are forced to kill the process in the event the
  2399. * original mapper has unmapped pages from the child due to a failed
  2400. * COW. Warn that such a situation has occurred as it may not be obvious
  2401. */
  2402. if (is_vma_resv_set(vma, HPAGE_RESV_UNMAPPED)) {
  2403. pr_warning("PID %d killed due to inadequate hugepage pool\n",
  2404. current->pid);
  2405. return ret;
  2406. }
  2407. mapping = vma->vm_file->f_mapping;
  2408. idx = vma_hugecache_offset(h, vma, address);
  2409. /*
  2410. * Use page lock to guard against racing truncation
  2411. * before we get page_table_lock.
  2412. */
  2413. retry:
  2414. page = find_lock_page(mapping, idx);
  2415. if (!page) {
  2416. size = i_size_read(mapping->host) >> huge_page_shift(h);
  2417. if (idx >= size)
  2418. goto out;
  2419. page = alloc_huge_page(vma, address, 0);
  2420. if (IS_ERR(page)) {
  2421. ret = PTR_ERR(page);
  2422. if (ret == -ENOMEM)
  2423. ret = VM_FAULT_OOM;
  2424. else
  2425. ret = VM_FAULT_SIGBUS;
  2426. goto out;
  2427. }
  2428. clear_huge_page(page, address, pages_per_huge_page(h));
  2429. __SetPageUptodate(page);
  2430. if (vma->vm_flags & VM_MAYSHARE) {
  2431. int err;
  2432. struct inode *inode = mapping->host;
  2433. err = add_to_page_cache(page, mapping, idx, GFP_KERNEL);
  2434. if (err) {
  2435. put_page(page);
  2436. if (err == -EEXIST)
  2437. goto retry;
  2438. goto out;
  2439. }
  2440. ClearPagePrivate(page);
  2441. spin_lock(&inode->i_lock);
  2442. inode->i_blocks += blocks_per_huge_page(h);
  2443. spin_unlock(&inode->i_lock);
  2444. } else {
  2445. lock_page(page);
  2446. if (unlikely(anon_vma_prepare(vma))) {
  2447. ret = VM_FAULT_OOM;
  2448. goto backout_unlocked;
  2449. }
  2450. anon_rmap = 1;
  2451. }
  2452. } else {
  2453. /*
  2454. * If memory error occurs between mmap() and fault, some process
  2455. * don't have hwpoisoned swap entry for errored virtual address.
  2456. * So we need to block hugepage fault by PG_hwpoison bit check.
  2457. */
  2458. if (unlikely(PageHWPoison(page))) {
  2459. ret = VM_FAULT_HWPOISON |
  2460. VM_FAULT_SET_HINDEX(hstate_index(h));
  2461. goto backout_unlocked;
  2462. }
  2463. }
  2464. /*
  2465. * If we are going to COW a private mapping later, we examine the
  2466. * pending reservations for this page now. This will ensure that
  2467. * any allocations necessary to record that reservation occur outside
  2468. * the spinlock.
  2469. */
  2470. if ((flags & FAULT_FLAG_WRITE) && !(vma->vm_flags & VM_SHARED))
  2471. if (vma_needs_reservation(h, vma, address) < 0) {
  2472. ret = VM_FAULT_OOM;
  2473. goto backout_unlocked;
  2474. }
  2475. ptl = huge_pte_lockptr(h, mm, ptep);
  2476. spin_lock(ptl);
  2477. size = i_size_read(mapping->host) >> huge_page_shift(h);
  2478. if (idx >= size)
  2479. goto backout;
  2480. ret = 0;
  2481. if (!huge_pte_none(huge_ptep_get(ptep)))
  2482. goto backout;
  2483. if (anon_rmap) {
  2484. ClearPagePrivate(page);
  2485. hugepage_add_new_anon_rmap(page, vma, address);
  2486. }
  2487. else
  2488. page_dup_rmap(page);
  2489. new_pte = make_huge_pte(vma, page, ((vma->vm_flags & VM_WRITE)
  2490. && (vma->vm_flags & VM_SHARED)));
  2491. set_huge_pte_at(mm, address, ptep, new_pte);
  2492. if ((flags & FAULT_FLAG_WRITE) && !(vma->vm_flags & VM_SHARED)) {
  2493. /* Optimization, do the COW without a second fault */
  2494. ret = hugetlb_cow(mm, vma, address, ptep, new_pte, page, ptl);
  2495. }
  2496. spin_unlock(ptl);
  2497. unlock_page(page);
  2498. out:
  2499. return ret;
  2500. backout:
  2501. spin_unlock(ptl);
  2502. backout_unlocked:
  2503. unlock_page(page);
  2504. put_page(page);
  2505. goto out;
  2506. }
  2507. int hugetlb_fault(struct mm_struct *mm, struct vm_area_struct *vma,
  2508. unsigned long address, unsigned int flags)
  2509. {
  2510. pte_t *ptep;
  2511. pte_t entry;
  2512. spinlock_t *ptl;
  2513. int ret;
  2514. struct page *page = NULL;
  2515. struct page *pagecache_page = NULL;
  2516. static DEFINE_MUTEX(hugetlb_instantiation_mutex);
  2517. struct hstate *h = hstate_vma(vma);
  2518. address &= huge_page_mask(h);
  2519. ptep = huge_pte_offset(mm, address);
  2520. if (ptep) {
  2521. entry = huge_ptep_get(ptep);
  2522. if (unlikely(is_hugetlb_entry_migration(entry))) {
  2523. migration_entry_wait_huge(vma, mm, ptep);
  2524. return 0;
  2525. } else if (unlikely(is_hugetlb_entry_hwpoisoned(entry)))
  2526. return VM_FAULT_HWPOISON_LARGE |
  2527. VM_FAULT_SET_HINDEX(hstate_index(h));
  2528. }
  2529. ptep = huge_pte_alloc(mm, address, huge_page_size(h));
  2530. if (!ptep)
  2531. return VM_FAULT_OOM;
  2532. /*
  2533. * Serialize hugepage allocation and instantiation, so that we don't
  2534. * get spurious allocation failures if two CPUs race to instantiate
  2535. * the same page in the page cache.
  2536. */
  2537. mutex_lock(&hugetlb_instantiation_mutex);
  2538. entry = huge_ptep_get(ptep);
  2539. if (huge_pte_none(entry)) {
  2540. ret = hugetlb_no_page(mm, vma, address, ptep, flags);
  2541. goto out_mutex;
  2542. }
  2543. ret = 0;
  2544. /*
  2545. * If we are going to COW the mapping later, we examine the pending
  2546. * reservations for this page now. This will ensure that any
  2547. * allocations necessary to record that reservation occur outside the
  2548. * spinlock. For private mappings, we also lookup the pagecache
  2549. * page now as it is used to determine if a reservation has been
  2550. * consumed.
  2551. */
  2552. if ((flags & FAULT_FLAG_WRITE) && !huge_pte_write(entry)) {
  2553. if (vma_needs_reservation(h, vma, address) < 0) {
  2554. ret = VM_FAULT_OOM;
  2555. goto out_mutex;
  2556. }
  2557. if (!(vma->vm_flags & VM_MAYSHARE))
  2558. pagecache_page = hugetlbfs_pagecache_page(h,
  2559. vma, address);
  2560. }
  2561. /*
  2562. * hugetlb_cow() requires page locks of pte_page(entry) and
  2563. * pagecache_page, so here we need take the former one
  2564. * when page != pagecache_page or !pagecache_page.
  2565. * Note that locking order is always pagecache_page -> page,
  2566. * so no worry about deadlock.
  2567. */
  2568. page = pte_page(entry);
  2569. get_page(page);
  2570. if (page != pagecache_page)
  2571. lock_page(page);
  2572. ptl = huge_pte_lockptr(h, mm, ptep);
  2573. spin_lock(ptl);
  2574. /* Check for a racing update before calling hugetlb_cow */
  2575. if (unlikely(!pte_same(entry, huge_ptep_get(ptep))))
  2576. goto out_ptl;
  2577. if (flags & FAULT_FLAG_WRITE) {
  2578. if (!huge_pte_write(entry)) {
  2579. ret = hugetlb_cow(mm, vma, address, ptep, entry,
  2580. pagecache_page, ptl);
  2581. goto out_ptl;
  2582. }
  2583. entry = huge_pte_mkdirty(entry);
  2584. }
  2585. entry = pte_mkyoung(entry);
  2586. if (huge_ptep_set_access_flags(vma, address, ptep, entry,
  2587. flags & FAULT_FLAG_WRITE))
  2588. update_mmu_cache(vma, address, ptep);
  2589. out_ptl:
  2590. spin_unlock(ptl);
  2591. if (pagecache_page) {
  2592. unlock_page(pagecache_page);
  2593. put_page(pagecache_page);
  2594. }
  2595. if (page != pagecache_page)
  2596. unlock_page(page);
  2597. put_page(page);
  2598. out_mutex:
  2599. mutex_unlock(&hugetlb_instantiation_mutex);
  2600. return ret;
  2601. }
  2602. long follow_hugetlb_page(struct mm_struct *mm, struct vm_area_struct *vma,
  2603. struct page **pages, struct vm_area_struct **vmas,
  2604. unsigned long *position, unsigned long *nr_pages,
  2605. long i, unsigned int flags)
  2606. {
  2607. unsigned long pfn_offset;
  2608. unsigned long vaddr = *position;
  2609. unsigned long remainder = *nr_pages;
  2610. struct hstate *h = hstate_vma(vma);
  2611. while (vaddr < vma->vm_end && remainder) {
  2612. pte_t *pte;
  2613. spinlock_t *ptl = NULL;
  2614. int absent;
  2615. struct page *page;
  2616. /*
  2617. * Some archs (sparc64, sh*) have multiple pte_ts to
  2618. * each hugepage. We have to make sure we get the
  2619. * first, for the page indexing below to work.
  2620. *
  2621. * Note that page table lock is not held when pte is null.
  2622. */
  2623. pte = huge_pte_offset(mm, vaddr & huge_page_mask(h));
  2624. if (pte)
  2625. ptl = huge_pte_lock(h, mm, pte);
  2626. absent = !pte || huge_pte_none(huge_ptep_get(pte));
  2627. /*
  2628. * When coredumping, it suits get_dump_page if we just return
  2629. * an error where there's an empty slot with no huge pagecache
  2630. * to back it. This way, we avoid allocating a hugepage, and
  2631. * the sparse dumpfile avoids allocating disk blocks, but its
  2632. * huge holes still show up with zeroes where they need to be.
  2633. */
  2634. if (absent && (flags & FOLL_DUMP) &&
  2635. !hugetlbfs_pagecache_present(h, vma, vaddr)) {
  2636. if (pte)
  2637. spin_unlock(ptl);
  2638. remainder = 0;
  2639. break;
  2640. }
  2641. /*
  2642. * We need call hugetlb_fault for both hugepages under migration
  2643. * (in which case hugetlb_fault waits for the migration,) and
  2644. * hwpoisoned hugepages (in which case we need to prevent the
  2645. * caller from accessing to them.) In order to do this, we use
  2646. * here is_swap_pte instead of is_hugetlb_entry_migration and
  2647. * is_hugetlb_entry_hwpoisoned. This is because it simply covers
  2648. * both cases, and because we can't follow correct pages
  2649. * directly from any kind of swap entries.
  2650. */
  2651. if (absent || is_swap_pte(huge_ptep_get(pte)) ||
  2652. ((flags & FOLL_WRITE) &&
  2653. !huge_pte_write(huge_ptep_get(pte)))) {
  2654. int ret;
  2655. if (pte)
  2656. spin_unlock(ptl);
  2657. ret = hugetlb_fault(mm, vma, vaddr,
  2658. (flags & FOLL_WRITE) ? FAULT_FLAG_WRITE : 0);
  2659. if (!(ret & VM_FAULT_ERROR))
  2660. continue;
  2661. remainder = 0;
  2662. break;
  2663. }
  2664. pfn_offset = (vaddr & ~huge_page_mask(h)) >> PAGE_SHIFT;
  2665. page = pte_page(huge_ptep_get(pte));
  2666. same_page:
  2667. if (pages) {
  2668. pages[i] = mem_map_offset(page, pfn_offset);
  2669. get_page(pages[i]);
  2670. }
  2671. if (vmas)
  2672. vmas[i] = vma;
  2673. vaddr += PAGE_SIZE;
  2674. ++pfn_offset;
  2675. --remainder;
  2676. ++i;
  2677. if (vaddr < vma->vm_end && remainder &&
  2678. pfn_offset < pages_per_huge_page(h)) {
  2679. /*
  2680. * We use pfn_offset to avoid touching the pageframes
  2681. * of this compound page.
  2682. */
  2683. goto same_page;
  2684. }
  2685. spin_unlock(ptl);
  2686. }
  2687. *nr_pages = remainder;
  2688. *position = vaddr;
  2689. return i ? i : -EFAULT;
  2690. }
  2691. unsigned long hugetlb_change_protection(struct vm_area_struct *vma,
  2692. unsigned long address, unsigned long end, pgprot_t newprot)
  2693. {
  2694. struct mm_struct *mm = vma->vm_mm;
  2695. unsigned long start = address;
  2696. pte_t *ptep;
  2697. pte_t pte;
  2698. struct hstate *h = hstate_vma(vma);
  2699. unsigned long pages = 0;
  2700. BUG_ON(address >= end);
  2701. flush_cache_range(vma, address, end);
  2702. mutex_lock(&vma->vm_file->f_mapping->i_mmap_mutex);
  2703. for (; address < end; address += huge_page_size(h)) {
  2704. spinlock_t *ptl;
  2705. ptep = huge_pte_offset(mm, address);
  2706. if (!ptep)
  2707. continue;
  2708. ptl = huge_pte_lock(h, mm, ptep);
  2709. if (huge_pmd_unshare(mm, &address, ptep)) {
  2710. pages++;
  2711. spin_unlock(ptl);
  2712. continue;
  2713. }
  2714. if (!huge_pte_none(huge_ptep_get(ptep))) {
  2715. pte = huge_ptep_get_and_clear(mm, address, ptep);
  2716. pte = pte_mkhuge(huge_pte_modify(pte, newprot));
  2717. pte = arch_make_huge_pte(pte, vma, NULL, 0);
  2718. set_huge_pte_at(mm, address, ptep, pte);
  2719. pages++;
  2720. }
  2721. spin_unlock(ptl);
  2722. }
  2723. /*
  2724. * Must flush TLB before releasing i_mmap_mutex: x86's huge_pmd_unshare
  2725. * may have cleared our pud entry and done put_page on the page table:
  2726. * once we release i_mmap_mutex, another task can do the final put_page
  2727. * and that page table be reused and filled with junk.
  2728. */
  2729. flush_tlb_range(vma, start, end);
  2730. mutex_unlock(&vma->vm_file->f_mapping->i_mmap_mutex);
  2731. return pages << h->order;
  2732. }
  2733. int hugetlb_reserve_pages(struct inode *inode,
  2734. long from, long to,
  2735. struct vm_area_struct *vma,
  2736. vm_flags_t vm_flags)
  2737. {
  2738. long ret, chg;
  2739. struct hstate *h = hstate_inode(inode);
  2740. struct hugepage_subpool *spool = subpool_inode(inode);
  2741. /*
  2742. * Only apply hugepage reservation if asked. At fault time, an
  2743. * attempt will be made for VM_NORESERVE to allocate a page
  2744. * without using reserves
  2745. */
  2746. if (vm_flags & VM_NORESERVE)
  2747. return 0;
  2748. /*
  2749. * Shared mappings base their reservation on the number of pages that
  2750. * are already allocated on behalf of the file. Private mappings need
  2751. * to reserve the full area even if read-only as mprotect() may be
  2752. * called to make the mapping read-write. Assume !vma is a shm mapping
  2753. */
  2754. if (!vma || vma->vm_flags & VM_MAYSHARE)
  2755. chg = region_chg(&inode->i_mapping->private_list, from, to);
  2756. else {
  2757. struct resv_map *resv_map = resv_map_alloc();
  2758. if (!resv_map)
  2759. return -ENOMEM;
  2760. chg = to - from;
  2761. set_vma_resv_map(vma, resv_map);
  2762. set_vma_resv_flags(vma, HPAGE_RESV_OWNER);
  2763. }
  2764. if (chg < 0) {
  2765. ret = chg;
  2766. goto out_err;
  2767. }
  2768. /* There must be enough pages in the subpool for the mapping */
  2769. if (hugepage_subpool_get_pages(spool, chg)) {
  2770. ret = -ENOSPC;
  2771. goto out_err;
  2772. }
  2773. /*
  2774. * Check enough hugepages are available for the reservation.
  2775. * Hand the pages back to the subpool if there are not
  2776. */
  2777. ret = hugetlb_acct_memory(h, chg);
  2778. if (ret < 0) {
  2779. hugepage_subpool_put_pages(spool, chg);
  2780. goto out_err;
  2781. }
  2782. /*
  2783. * Account for the reservations made. Shared mappings record regions
  2784. * that have reservations as they are shared by multiple VMAs.
  2785. * When the last VMA disappears, the region map says how much
  2786. * the reservation was and the page cache tells how much of
  2787. * the reservation was consumed. Private mappings are per-VMA and
  2788. * only the consumed reservations are tracked. When the VMA
  2789. * disappears, the original reservation is the VMA size and the
  2790. * consumed reservations are stored in the map. Hence, nothing
  2791. * else has to be done for private mappings here
  2792. */
  2793. if (!vma || vma->vm_flags & VM_MAYSHARE)
  2794. region_add(&inode->i_mapping->private_list, from, to);
  2795. return 0;
  2796. out_err:
  2797. if (vma)
  2798. resv_map_put(vma);
  2799. return ret;
  2800. }
  2801. void hugetlb_unreserve_pages(struct inode *inode, long offset, long freed)
  2802. {
  2803. struct hstate *h = hstate_inode(inode);
  2804. long chg = region_truncate(&inode->i_mapping->private_list, offset);
  2805. struct hugepage_subpool *spool = subpool_inode(inode);
  2806. spin_lock(&inode->i_lock);
  2807. inode->i_blocks -= (blocks_per_huge_page(h) * freed);
  2808. spin_unlock(&inode->i_lock);
  2809. hugepage_subpool_put_pages(spool, (chg - freed));
  2810. hugetlb_acct_memory(h, -(chg - freed));
  2811. }
  2812. #ifdef CONFIG_ARCH_WANT_HUGE_PMD_SHARE
  2813. static unsigned long page_table_shareable(struct vm_area_struct *svma,
  2814. struct vm_area_struct *vma,
  2815. unsigned long addr, pgoff_t idx)
  2816. {
  2817. unsigned long saddr = ((idx - svma->vm_pgoff) << PAGE_SHIFT) +
  2818. svma->vm_start;
  2819. unsigned long sbase = saddr & PUD_MASK;
  2820. unsigned long s_end = sbase + PUD_SIZE;
  2821. /* Allow segments to share if only one is marked locked */
  2822. unsigned long vm_flags = vma->vm_flags & ~VM_LOCKED;
  2823. unsigned long svm_flags = svma->vm_flags & ~VM_LOCKED;
  2824. /*
  2825. * match the virtual addresses, permission and the alignment of the
  2826. * page table page.
  2827. */
  2828. if (pmd_index(addr) != pmd_index(saddr) ||
  2829. vm_flags != svm_flags ||
  2830. sbase < svma->vm_start || svma->vm_end < s_end)
  2831. return 0;
  2832. return saddr;
  2833. }
  2834. static int vma_shareable(struct vm_area_struct *vma, unsigned long addr)
  2835. {
  2836. unsigned long base = addr & PUD_MASK;
  2837. unsigned long end = base + PUD_SIZE;
  2838. /*
  2839. * check on proper vm_flags and page table alignment
  2840. */
  2841. if (vma->vm_flags & VM_MAYSHARE &&
  2842. vma->vm_start <= base && end <= vma->vm_end)
  2843. return 1;
  2844. return 0;
  2845. }
  2846. /*
  2847. * Search for a shareable pmd page for hugetlb. In any case calls pmd_alloc()
  2848. * and returns the corresponding pte. While this is not necessary for the
  2849. * !shared pmd case because we can allocate the pmd later as well, it makes the
  2850. * code much cleaner. pmd allocation is essential for the shared case because
  2851. * pud has to be populated inside the same i_mmap_mutex section - otherwise
  2852. * racing tasks could either miss the sharing (see huge_pte_offset) or select a
  2853. * bad pmd for sharing.
  2854. */
  2855. pte_t *huge_pmd_share(struct mm_struct *mm, unsigned long addr, pud_t *pud)
  2856. {
  2857. struct vm_area_struct *vma = find_vma(mm, addr);
  2858. struct address_space *mapping = vma->vm_file->f_mapping;
  2859. pgoff_t idx = ((addr - vma->vm_start) >> PAGE_SHIFT) +
  2860. vma->vm_pgoff;
  2861. struct vm_area_struct *svma;
  2862. unsigned long saddr;
  2863. pte_t *spte = NULL;
  2864. pte_t *pte;
  2865. spinlock_t *ptl;
  2866. if (!vma_shareable(vma, addr))
  2867. return (pte_t *)pmd_alloc(mm, pud, addr);
  2868. mutex_lock(&mapping->i_mmap_mutex);
  2869. vma_interval_tree_foreach(svma, &mapping->i_mmap, idx, idx) {
  2870. if (svma == vma)
  2871. continue;
  2872. saddr = page_table_shareable(svma, vma, addr, idx);
  2873. if (saddr) {
  2874. spte = huge_pte_offset(svma->vm_mm, saddr);
  2875. if (spte) {
  2876. get_page(virt_to_page(spte));
  2877. break;
  2878. }
  2879. }
  2880. }
  2881. if (!spte)
  2882. goto out;
  2883. ptl = huge_pte_lockptr(hstate_vma(vma), mm, spte);
  2884. spin_lock(ptl);
  2885. if (pud_none(*pud))
  2886. pud_populate(mm, pud,
  2887. (pmd_t *)((unsigned long)spte & PAGE_MASK));
  2888. else
  2889. put_page(virt_to_page(spte));
  2890. spin_unlock(ptl);
  2891. out:
  2892. pte = (pte_t *)pmd_alloc(mm, pud, addr);
  2893. mutex_unlock(&mapping->i_mmap_mutex);
  2894. return pte;
  2895. }
  2896. /*
  2897. * unmap huge page backed by shared pte.
  2898. *
  2899. * Hugetlb pte page is ref counted at the time of mapping. If pte is shared
  2900. * indicated by page_count > 1, unmap is achieved by clearing pud and
  2901. * decrementing the ref count. If count == 1, the pte page is not shared.
  2902. *
  2903. * called with page table lock held.
  2904. *
  2905. * returns: 1 successfully unmapped a shared pte page
  2906. * 0 the underlying pte page is not shared, or it is the last user
  2907. */
  2908. int huge_pmd_unshare(struct mm_struct *mm, unsigned long *addr, pte_t *ptep)
  2909. {
  2910. pgd_t *pgd = pgd_offset(mm, *addr);
  2911. pud_t *pud = pud_offset(pgd, *addr);
  2912. BUG_ON(page_count(virt_to_page(ptep)) == 0);
  2913. if (page_count(virt_to_page(ptep)) == 1)
  2914. return 0;
  2915. pud_clear(pud);
  2916. put_page(virt_to_page(ptep));
  2917. *addr = ALIGN(*addr, HPAGE_SIZE * PTRS_PER_PTE) - HPAGE_SIZE;
  2918. return 1;
  2919. }
  2920. #define want_pmd_share() (1)
  2921. #else /* !CONFIG_ARCH_WANT_HUGE_PMD_SHARE */
  2922. pte_t *huge_pmd_share(struct mm_struct *mm, unsigned long addr, pud_t *pud)
  2923. {
  2924. return NULL;
  2925. }
  2926. #define want_pmd_share() (0)
  2927. #endif /* CONFIG_ARCH_WANT_HUGE_PMD_SHARE */
  2928. #ifdef CONFIG_ARCH_WANT_GENERAL_HUGETLB
  2929. pte_t *huge_pte_alloc(struct mm_struct *mm,
  2930. unsigned long addr, unsigned long sz)
  2931. {
  2932. pgd_t *pgd;
  2933. pud_t *pud;
  2934. pte_t *pte = NULL;
  2935. pgd = pgd_offset(mm, addr);
  2936. pud = pud_alloc(mm, pgd, addr);
  2937. if (pud) {
  2938. if (sz == PUD_SIZE) {
  2939. pte = (pte_t *)pud;
  2940. } else {
  2941. BUG_ON(sz != PMD_SIZE);
  2942. if (want_pmd_share() && pud_none(*pud))
  2943. pte = huge_pmd_share(mm, addr, pud);
  2944. else
  2945. pte = (pte_t *)pmd_alloc(mm, pud, addr);
  2946. }
  2947. }
  2948. BUG_ON(pte && !pte_none(*pte) && !pte_huge(*pte));
  2949. return pte;
  2950. }
  2951. pte_t *huge_pte_offset(struct mm_struct *mm, unsigned long addr)
  2952. {
  2953. pgd_t *pgd;
  2954. pud_t *pud;
  2955. pmd_t *pmd = NULL;
  2956. pgd = pgd_offset(mm, addr);
  2957. if (pgd_present(*pgd)) {
  2958. pud = pud_offset(pgd, addr);
  2959. if (pud_present(*pud)) {
  2960. if (pud_huge(*pud))
  2961. return (pte_t *)pud;
  2962. pmd = pmd_offset(pud, addr);
  2963. }
  2964. }
  2965. return (pte_t *) pmd;
  2966. }
  2967. struct page *
  2968. follow_huge_pmd(struct mm_struct *mm, unsigned long address,
  2969. pmd_t *pmd, int write)
  2970. {
  2971. struct page *page;
  2972. page = pte_page(*(pte_t *)pmd);
  2973. if (page)
  2974. page += ((address & ~PMD_MASK) >> PAGE_SHIFT);
  2975. return page;
  2976. }
  2977. struct page *
  2978. follow_huge_pud(struct mm_struct *mm, unsigned long address,
  2979. pud_t *pud, int write)
  2980. {
  2981. struct page *page;
  2982. page = pte_page(*(pte_t *)pud);
  2983. if (page)
  2984. page += ((address & ~PUD_MASK) >> PAGE_SHIFT);
  2985. return page;
  2986. }
  2987. #else /* !CONFIG_ARCH_WANT_GENERAL_HUGETLB */
  2988. /* Can be overriden by architectures */
  2989. __attribute__((weak)) struct page *
  2990. follow_huge_pud(struct mm_struct *mm, unsigned long address,
  2991. pud_t *pud, int write)
  2992. {
  2993. BUG();
  2994. return NULL;
  2995. }
  2996. #endif /* CONFIG_ARCH_WANT_GENERAL_HUGETLB */
  2997. #ifdef CONFIG_MEMORY_FAILURE
  2998. /* Should be called in hugetlb_lock */
  2999. static int is_hugepage_on_freelist(struct page *hpage)
  3000. {
  3001. struct page *page;
  3002. struct page *tmp;
  3003. struct hstate *h = page_hstate(hpage);
  3004. int nid = page_to_nid(hpage);
  3005. list_for_each_entry_safe(page, tmp, &h->hugepage_freelists[nid], lru)
  3006. if (page == hpage)
  3007. return 1;
  3008. return 0;
  3009. }
  3010. /*
  3011. * This function is called from memory failure code.
  3012. * Assume the caller holds page lock of the head page.
  3013. */
  3014. int dequeue_hwpoisoned_huge_page(struct page *hpage)
  3015. {
  3016. struct hstate *h = page_hstate(hpage);
  3017. int nid = page_to_nid(hpage);
  3018. int ret = -EBUSY;
  3019. spin_lock(&hugetlb_lock);
  3020. if (is_hugepage_on_freelist(hpage)) {
  3021. /*
  3022. * Hwpoisoned hugepage isn't linked to activelist or freelist,
  3023. * but dangling hpage->lru can trigger list-debug warnings
  3024. * (this happens when we call unpoison_memory() on it),
  3025. * so let it point to itself with list_del_init().
  3026. */
  3027. list_del_init(&hpage->lru);
  3028. set_page_refcounted(hpage);
  3029. h->free_huge_pages--;
  3030. h->free_huge_pages_node[nid]--;
  3031. ret = 0;
  3032. }
  3033. spin_unlock(&hugetlb_lock);
  3034. return ret;
  3035. }
  3036. #endif
  3037. bool isolate_huge_page(struct page *page, struct list_head *list)
  3038. {
  3039. VM_BUG_ON(!PageHead(page));
  3040. if (!get_page_unless_zero(page))
  3041. return false;
  3042. spin_lock(&hugetlb_lock);
  3043. list_move_tail(&page->lru, list);
  3044. spin_unlock(&hugetlb_lock);
  3045. return true;
  3046. }
  3047. void putback_active_hugepage(struct page *page)
  3048. {
  3049. VM_BUG_ON(!PageHead(page));
  3050. spin_lock(&hugetlb_lock);
  3051. list_move_tail(&page->lru, &(page_hstate(page))->hugepage_activelist);
  3052. spin_unlock(&hugetlb_lock);
  3053. put_page(page);
  3054. }
  3055. bool is_hugepage_active(struct page *page)
  3056. {
  3057. VM_BUG_ON(!PageHuge(page));
  3058. /*
  3059. * This function can be called for a tail page because the caller,
  3060. * scan_movable_pages, scans through a given pfn-range which typically
  3061. * covers one memory block. In systems using gigantic hugepage (1GB
  3062. * for x86_64,) a hugepage is larger than a memory block, and we don't
  3063. * support migrating such large hugepages for now, so return false
  3064. * when called for tail pages.
  3065. */
  3066. if (PageTail(page))
  3067. return false;
  3068. /*
  3069. * Refcount of a hwpoisoned hugepages is 1, but they are not active,
  3070. * so we should return false for them.
  3071. */
  3072. if (unlikely(PageHWPoison(page)))
  3073. return false;
  3074. return page_count(page) > 0;
  3075. }