hugetlb.c 89 KB

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