hugetlb.c 92 KB

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