hugetlb.c 92 KB

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