hugetlb.c 82 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188
  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_instantion_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 << (hstate->order + PAGE_SHIFT);
  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. retry_cpuset:
  459. cpuset_mems_cookie = get_mems_allowed();
  460. zonelist = huge_zonelist(vma, address,
  461. htlb_alloc_mask, &mpol, &nodemask);
  462. /*
  463. * A child process with MAP_PRIVATE mappings created by their parent
  464. * have no page reserves. This check ensures that reservations are
  465. * not "stolen". The child may still get SIGKILLed
  466. */
  467. if (!vma_has_reserves(vma) &&
  468. h->free_huge_pages - h->resv_huge_pages == 0)
  469. goto err;
  470. /* If reserves cannot be used, ensure enough pages are in the pool */
  471. if (avoid_reserve && h->free_huge_pages - h->resv_huge_pages == 0)
  472. goto err;
  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. mpol_cond_put(mpol);
  490. return NULL;
  491. }
  492. static void update_and_free_page(struct hstate *h, struct page *page)
  493. {
  494. int i;
  495. VM_BUG_ON(h->order >= MAX_ORDER);
  496. h->nr_huge_pages--;
  497. h->nr_huge_pages_node[page_to_nid(page)]--;
  498. for (i = 0; i < pages_per_huge_page(h); i++) {
  499. page[i].flags &= ~(1 << PG_locked | 1 << PG_error |
  500. 1 << PG_referenced | 1 << PG_dirty |
  501. 1 << PG_active | 1 << PG_reserved |
  502. 1 << PG_private | 1 << PG_writeback);
  503. }
  504. VM_BUG_ON(hugetlb_cgroup_from_page(page));
  505. set_compound_page_dtor(page, NULL);
  506. set_page_refcounted(page);
  507. arch_release_hugepage(page);
  508. __free_pages(page, huge_page_order(h));
  509. }
  510. struct hstate *size_to_hstate(unsigned long size)
  511. {
  512. struct hstate *h;
  513. for_each_hstate(h) {
  514. if (huge_page_size(h) == size)
  515. return h;
  516. }
  517. return NULL;
  518. }
  519. static void free_huge_page(struct page *page)
  520. {
  521. /*
  522. * Can't pass hstate in here because it is called from the
  523. * compound page destructor.
  524. */
  525. struct hstate *h = page_hstate(page);
  526. int nid = page_to_nid(page);
  527. struct hugepage_subpool *spool =
  528. (struct hugepage_subpool *)page_private(page);
  529. set_page_private(page, 0);
  530. page->mapping = NULL;
  531. BUG_ON(page_count(page));
  532. BUG_ON(page_mapcount(page));
  533. spin_lock(&hugetlb_lock);
  534. hugetlb_cgroup_uncharge_page(hstate_index(h),
  535. pages_per_huge_page(h), page);
  536. if (h->surplus_huge_pages_node[nid] && huge_page_order(h) < MAX_ORDER) {
  537. /* remove the page from active list */
  538. list_del(&page->lru);
  539. update_and_free_page(h, page);
  540. h->surplus_huge_pages--;
  541. h->surplus_huge_pages_node[nid]--;
  542. } else {
  543. arch_clear_hugepage_flags(page);
  544. enqueue_huge_page(h, page);
  545. }
  546. spin_unlock(&hugetlb_lock);
  547. hugepage_subpool_put_pages(spool, 1);
  548. }
  549. static void prep_new_huge_page(struct hstate *h, struct page *page, int nid)
  550. {
  551. INIT_LIST_HEAD(&page->lru);
  552. set_compound_page_dtor(page, free_huge_page);
  553. spin_lock(&hugetlb_lock);
  554. set_hugetlb_cgroup(page, NULL);
  555. h->nr_huge_pages++;
  556. h->nr_huge_pages_node[nid]++;
  557. spin_unlock(&hugetlb_lock);
  558. put_page(page); /* free it into the hugepage allocator */
  559. }
  560. static void prep_compound_gigantic_page(struct page *page, unsigned long order)
  561. {
  562. int i;
  563. int nr_pages = 1 << order;
  564. struct page *p = page + 1;
  565. /* we rely on prep_new_huge_page to set the destructor */
  566. set_compound_order(page, order);
  567. __SetPageHead(page);
  568. for (i = 1; i < nr_pages; i++, p = mem_map_next(p, page, i)) {
  569. __SetPageTail(p);
  570. set_page_count(p, 0);
  571. p->first_page = page;
  572. }
  573. }
  574. /*
  575. * PageHuge() only returns true for hugetlbfs pages, but not for normal or
  576. * transparent huge pages. See the PageTransHuge() documentation for more
  577. * details.
  578. */
  579. int PageHuge(struct page *page)
  580. {
  581. compound_page_dtor *dtor;
  582. if (!PageCompound(page))
  583. return 0;
  584. page = compound_head(page);
  585. dtor = get_compound_page_dtor(page);
  586. return dtor == free_huge_page;
  587. }
  588. EXPORT_SYMBOL_GPL(PageHuge);
  589. static struct page *alloc_fresh_huge_page_node(struct hstate *h, int nid)
  590. {
  591. struct page *page;
  592. if (h->order >= MAX_ORDER)
  593. return NULL;
  594. page = alloc_pages_exact_node(nid,
  595. htlb_alloc_mask|__GFP_COMP|__GFP_THISNODE|
  596. __GFP_REPEAT|__GFP_NOWARN,
  597. huge_page_order(h));
  598. if (page) {
  599. if (arch_prepare_hugepage(page)) {
  600. __free_pages(page, huge_page_order(h));
  601. return NULL;
  602. }
  603. prep_new_huge_page(h, page, nid);
  604. }
  605. return page;
  606. }
  607. /*
  608. * common helper functions for hstate_next_node_to_{alloc|free}.
  609. * We may have allocated or freed a huge page based on a different
  610. * nodes_allowed previously, so h->next_node_to_{alloc|free} might
  611. * be outside of *nodes_allowed. Ensure that we use an allowed
  612. * node for alloc or free.
  613. */
  614. static int next_node_allowed(int nid, nodemask_t *nodes_allowed)
  615. {
  616. nid = next_node(nid, *nodes_allowed);
  617. if (nid == MAX_NUMNODES)
  618. nid = first_node(*nodes_allowed);
  619. VM_BUG_ON(nid >= MAX_NUMNODES);
  620. return nid;
  621. }
  622. static int get_valid_node_allowed(int nid, nodemask_t *nodes_allowed)
  623. {
  624. if (!node_isset(nid, *nodes_allowed))
  625. nid = next_node_allowed(nid, nodes_allowed);
  626. return nid;
  627. }
  628. /*
  629. * returns the previously saved node ["this node"] from which to
  630. * allocate a persistent huge page for the pool and advance the
  631. * next node from which to allocate, handling wrap at end of node
  632. * mask.
  633. */
  634. static int hstate_next_node_to_alloc(struct hstate *h,
  635. nodemask_t *nodes_allowed)
  636. {
  637. int nid;
  638. VM_BUG_ON(!nodes_allowed);
  639. nid = get_valid_node_allowed(h->next_nid_to_alloc, nodes_allowed);
  640. h->next_nid_to_alloc = next_node_allowed(nid, nodes_allowed);
  641. return nid;
  642. }
  643. static int alloc_fresh_huge_page(struct hstate *h, nodemask_t *nodes_allowed)
  644. {
  645. struct page *page;
  646. int start_nid;
  647. int next_nid;
  648. int ret = 0;
  649. start_nid = hstate_next_node_to_alloc(h, nodes_allowed);
  650. next_nid = start_nid;
  651. do {
  652. page = alloc_fresh_huge_page_node(h, next_nid);
  653. if (page) {
  654. ret = 1;
  655. break;
  656. }
  657. next_nid = hstate_next_node_to_alloc(h, nodes_allowed);
  658. } while (next_nid != start_nid);
  659. if (ret)
  660. count_vm_event(HTLB_BUDDY_PGALLOC);
  661. else
  662. count_vm_event(HTLB_BUDDY_PGALLOC_FAIL);
  663. return ret;
  664. }
  665. /*
  666. * helper for free_pool_huge_page() - return the previously saved
  667. * node ["this node"] from which to free a huge page. Advance the
  668. * next node id whether or not we find a free huge page to free so
  669. * that the next attempt to free addresses the next node.
  670. */
  671. static int hstate_next_node_to_free(struct hstate *h, nodemask_t *nodes_allowed)
  672. {
  673. int nid;
  674. VM_BUG_ON(!nodes_allowed);
  675. nid = get_valid_node_allowed(h->next_nid_to_free, nodes_allowed);
  676. h->next_nid_to_free = next_node_allowed(nid, nodes_allowed);
  677. return nid;
  678. }
  679. /*
  680. * Free huge page from pool from next node to free.
  681. * Attempt to keep persistent huge pages more or less
  682. * balanced over allowed nodes.
  683. * Called with hugetlb_lock locked.
  684. */
  685. static int free_pool_huge_page(struct hstate *h, nodemask_t *nodes_allowed,
  686. bool acct_surplus)
  687. {
  688. int start_nid;
  689. int next_nid;
  690. int ret = 0;
  691. start_nid = hstate_next_node_to_free(h, nodes_allowed);
  692. next_nid = start_nid;
  693. do {
  694. /*
  695. * If we're returning unused surplus pages, only examine
  696. * nodes with surplus pages.
  697. */
  698. if ((!acct_surplus || h->surplus_huge_pages_node[next_nid]) &&
  699. !list_empty(&h->hugepage_freelists[next_nid])) {
  700. struct page *page =
  701. list_entry(h->hugepage_freelists[next_nid].next,
  702. struct page, lru);
  703. list_del(&page->lru);
  704. h->free_huge_pages--;
  705. h->free_huge_pages_node[next_nid]--;
  706. if (acct_surplus) {
  707. h->surplus_huge_pages--;
  708. h->surplus_huge_pages_node[next_nid]--;
  709. }
  710. update_and_free_page(h, page);
  711. ret = 1;
  712. break;
  713. }
  714. next_nid = hstate_next_node_to_free(h, nodes_allowed);
  715. } while (next_nid != start_nid);
  716. return ret;
  717. }
  718. static struct page *alloc_buddy_huge_page(struct hstate *h, int nid)
  719. {
  720. struct page *page;
  721. unsigned int r_nid;
  722. if (h->order >= MAX_ORDER)
  723. return NULL;
  724. /*
  725. * Assume we will successfully allocate the surplus page to
  726. * prevent racing processes from causing the surplus to exceed
  727. * overcommit
  728. *
  729. * This however introduces a different race, where a process B
  730. * tries to grow the static hugepage pool while alloc_pages() is
  731. * called by process A. B will only examine the per-node
  732. * counters in determining if surplus huge pages can be
  733. * converted to normal huge pages in adjust_pool_surplus(). A
  734. * won't be able to increment the per-node counter, until the
  735. * lock is dropped by B, but B doesn't drop hugetlb_lock until
  736. * no more huge pages can be converted from surplus to normal
  737. * state (and doesn't try to convert again). Thus, we have a
  738. * case where a surplus huge page exists, the pool is grown, and
  739. * the surplus huge page still exists after, even though it
  740. * should just have been converted to a normal huge page. This
  741. * does not leak memory, though, as the hugepage will be freed
  742. * once it is out of use. It also does not allow the counters to
  743. * go out of whack in adjust_pool_surplus() as we don't modify
  744. * the node values until we've gotten the hugepage and only the
  745. * per-node value is checked there.
  746. */
  747. spin_lock(&hugetlb_lock);
  748. if (h->surplus_huge_pages >= h->nr_overcommit_huge_pages) {
  749. spin_unlock(&hugetlb_lock);
  750. return NULL;
  751. } else {
  752. h->nr_huge_pages++;
  753. h->surplus_huge_pages++;
  754. }
  755. spin_unlock(&hugetlb_lock);
  756. if (nid == NUMA_NO_NODE)
  757. page = alloc_pages(htlb_alloc_mask|__GFP_COMP|
  758. __GFP_REPEAT|__GFP_NOWARN,
  759. huge_page_order(h));
  760. else
  761. page = alloc_pages_exact_node(nid,
  762. htlb_alloc_mask|__GFP_COMP|__GFP_THISNODE|
  763. __GFP_REPEAT|__GFP_NOWARN, huge_page_order(h));
  764. if (page && arch_prepare_hugepage(page)) {
  765. __free_pages(page, huge_page_order(h));
  766. page = NULL;
  767. }
  768. spin_lock(&hugetlb_lock);
  769. if (page) {
  770. INIT_LIST_HEAD(&page->lru);
  771. r_nid = page_to_nid(page);
  772. set_compound_page_dtor(page, free_huge_page);
  773. set_hugetlb_cgroup(page, NULL);
  774. /*
  775. * We incremented the global counters already
  776. */
  777. h->nr_huge_pages_node[r_nid]++;
  778. h->surplus_huge_pages_node[r_nid]++;
  779. __count_vm_event(HTLB_BUDDY_PGALLOC);
  780. } else {
  781. h->nr_huge_pages--;
  782. h->surplus_huge_pages--;
  783. __count_vm_event(HTLB_BUDDY_PGALLOC_FAIL);
  784. }
  785. spin_unlock(&hugetlb_lock);
  786. return page;
  787. }
  788. /*
  789. * This allocation function is useful in the context where vma is irrelevant.
  790. * E.g. soft-offlining uses this function because it only cares physical
  791. * address of error page.
  792. */
  793. struct page *alloc_huge_page_node(struct hstate *h, int nid)
  794. {
  795. struct page *page;
  796. spin_lock(&hugetlb_lock);
  797. page = dequeue_huge_page_node(h, nid);
  798. spin_unlock(&hugetlb_lock);
  799. if (!page)
  800. page = alloc_buddy_huge_page(h, nid);
  801. return page;
  802. }
  803. /*
  804. * Increase the hugetlb pool such that it can accommodate a reservation
  805. * of size 'delta'.
  806. */
  807. static int gather_surplus_pages(struct hstate *h, int delta)
  808. {
  809. struct list_head surplus_list;
  810. struct page *page, *tmp;
  811. int ret, i;
  812. int needed, allocated;
  813. bool alloc_ok = true;
  814. needed = (h->resv_huge_pages + delta) - h->free_huge_pages;
  815. if (needed <= 0) {
  816. h->resv_huge_pages += delta;
  817. return 0;
  818. }
  819. allocated = 0;
  820. INIT_LIST_HEAD(&surplus_list);
  821. ret = -ENOMEM;
  822. retry:
  823. spin_unlock(&hugetlb_lock);
  824. for (i = 0; i < needed; i++) {
  825. page = alloc_buddy_huge_page(h, NUMA_NO_NODE);
  826. if (!page) {
  827. alloc_ok = false;
  828. break;
  829. }
  830. list_add(&page->lru, &surplus_list);
  831. }
  832. allocated += i;
  833. /*
  834. * After retaking hugetlb_lock, we need to recalculate 'needed'
  835. * because either resv_huge_pages or free_huge_pages may have changed.
  836. */
  837. spin_lock(&hugetlb_lock);
  838. needed = (h->resv_huge_pages + delta) -
  839. (h->free_huge_pages + allocated);
  840. if (needed > 0) {
  841. if (alloc_ok)
  842. goto retry;
  843. /*
  844. * We were not able to allocate enough pages to
  845. * satisfy the entire reservation so we free what
  846. * we've allocated so far.
  847. */
  848. goto free;
  849. }
  850. /*
  851. * The surplus_list now contains _at_least_ the number of extra pages
  852. * needed to accommodate the reservation. Add the appropriate number
  853. * of pages to the hugetlb pool and free the extras back to the buddy
  854. * allocator. Commit the entire reservation here to prevent another
  855. * process from stealing the pages as they are added to the pool but
  856. * before they are reserved.
  857. */
  858. needed += allocated;
  859. h->resv_huge_pages += delta;
  860. ret = 0;
  861. /* Free the needed pages to the hugetlb pool */
  862. list_for_each_entry_safe(page, tmp, &surplus_list, lru) {
  863. if ((--needed) < 0)
  864. break;
  865. /*
  866. * This page is now managed by the hugetlb allocator and has
  867. * no users -- drop the buddy allocator's reference.
  868. */
  869. put_page_testzero(page);
  870. VM_BUG_ON(page_count(page));
  871. enqueue_huge_page(h, page);
  872. }
  873. free:
  874. spin_unlock(&hugetlb_lock);
  875. /* Free unnecessary surplus pages to the buddy allocator */
  876. if (!list_empty(&surplus_list)) {
  877. list_for_each_entry_safe(page, tmp, &surplus_list, lru) {
  878. put_page(page);
  879. }
  880. }
  881. spin_lock(&hugetlb_lock);
  882. return ret;
  883. }
  884. /*
  885. * When releasing a hugetlb pool reservation, any surplus pages that were
  886. * allocated to satisfy the reservation must be explicitly freed if they were
  887. * never used.
  888. * Called with hugetlb_lock held.
  889. */
  890. static void return_unused_surplus_pages(struct hstate *h,
  891. unsigned long unused_resv_pages)
  892. {
  893. unsigned long nr_pages;
  894. /* Uncommit the reservation */
  895. h->resv_huge_pages -= unused_resv_pages;
  896. /* Cannot return gigantic pages currently */
  897. if (h->order >= MAX_ORDER)
  898. return;
  899. nr_pages = min(unused_resv_pages, h->surplus_huge_pages);
  900. /*
  901. * We want to release as many surplus pages as possible, spread
  902. * evenly across all nodes with memory. Iterate across these nodes
  903. * until we can no longer free unreserved surplus pages. This occurs
  904. * when the nodes with surplus pages have no free pages.
  905. * free_pool_huge_page() will balance the the freed pages across the
  906. * on-line nodes with memory and will handle the hstate accounting.
  907. */
  908. while (nr_pages--) {
  909. if (!free_pool_huge_page(h, &node_states[N_MEMORY], 1))
  910. break;
  911. }
  912. }
  913. /*
  914. * Determine if the huge page at addr within the vma has an associated
  915. * reservation. Where it does not we will need to logically increase
  916. * reservation and actually increase subpool usage before an allocation
  917. * can occur. Where any new reservation would be required the
  918. * reservation change is prepared, but not committed. Once the page
  919. * has been allocated from the subpool and instantiated the change should
  920. * be committed via vma_commit_reservation. No action is required on
  921. * failure.
  922. */
  923. static long vma_needs_reservation(struct hstate *h,
  924. struct vm_area_struct *vma, unsigned long addr)
  925. {
  926. struct address_space *mapping = vma->vm_file->f_mapping;
  927. struct inode *inode = mapping->host;
  928. if (vma->vm_flags & VM_MAYSHARE) {
  929. pgoff_t idx = vma_hugecache_offset(h, vma, addr);
  930. return region_chg(&inode->i_mapping->private_list,
  931. idx, idx + 1);
  932. } else if (!is_vma_resv_set(vma, HPAGE_RESV_OWNER)) {
  933. return 1;
  934. } else {
  935. long err;
  936. pgoff_t idx = vma_hugecache_offset(h, vma, addr);
  937. struct resv_map *reservations = vma_resv_map(vma);
  938. err = region_chg(&reservations->regions, idx, idx + 1);
  939. if (err < 0)
  940. return err;
  941. return 0;
  942. }
  943. }
  944. static void vma_commit_reservation(struct hstate *h,
  945. struct vm_area_struct *vma, unsigned long addr)
  946. {
  947. struct address_space *mapping = vma->vm_file->f_mapping;
  948. struct inode *inode = mapping->host;
  949. if (vma->vm_flags & VM_MAYSHARE) {
  950. pgoff_t idx = vma_hugecache_offset(h, vma, addr);
  951. region_add(&inode->i_mapping->private_list, idx, idx + 1);
  952. } else if (is_vma_resv_set(vma, HPAGE_RESV_OWNER)) {
  953. pgoff_t idx = vma_hugecache_offset(h, vma, addr);
  954. struct resv_map *reservations = vma_resv_map(vma);
  955. /* Mark this page used in the map. */
  956. region_add(&reservations->regions, idx, idx + 1);
  957. }
  958. }
  959. static struct page *alloc_huge_page(struct vm_area_struct *vma,
  960. unsigned long addr, int avoid_reserve)
  961. {
  962. struct hugepage_subpool *spool = subpool_vma(vma);
  963. struct hstate *h = hstate_vma(vma);
  964. struct page *page;
  965. long chg;
  966. int ret, idx;
  967. struct hugetlb_cgroup *h_cg;
  968. idx = hstate_index(h);
  969. /*
  970. * Processes that did not create the mapping will have no
  971. * reserves and will not have accounted against subpool
  972. * limit. Check that the subpool limit can be made before
  973. * satisfying the allocation MAP_NORESERVE mappings may also
  974. * need pages and subpool limit allocated allocated if no reserve
  975. * mapping overlaps.
  976. */
  977. chg = vma_needs_reservation(h, vma, addr);
  978. if (chg < 0)
  979. return ERR_PTR(-ENOMEM);
  980. if (chg)
  981. if (hugepage_subpool_get_pages(spool, chg))
  982. return ERR_PTR(-ENOSPC);
  983. ret = hugetlb_cgroup_charge_cgroup(idx, pages_per_huge_page(h), &h_cg);
  984. if (ret) {
  985. hugepage_subpool_put_pages(spool, chg);
  986. return ERR_PTR(-ENOSPC);
  987. }
  988. spin_lock(&hugetlb_lock);
  989. page = dequeue_huge_page_vma(h, vma, addr, avoid_reserve);
  990. if (page) {
  991. /* update page cgroup details */
  992. hugetlb_cgroup_commit_charge(idx, pages_per_huge_page(h),
  993. h_cg, page);
  994. spin_unlock(&hugetlb_lock);
  995. } else {
  996. spin_unlock(&hugetlb_lock);
  997. page = alloc_buddy_huge_page(h, NUMA_NO_NODE);
  998. if (!page) {
  999. hugetlb_cgroup_uncharge_cgroup(idx,
  1000. pages_per_huge_page(h),
  1001. h_cg);
  1002. hugepage_subpool_put_pages(spool, chg);
  1003. return ERR_PTR(-ENOSPC);
  1004. }
  1005. spin_lock(&hugetlb_lock);
  1006. hugetlb_cgroup_commit_charge(idx, pages_per_huge_page(h),
  1007. h_cg, page);
  1008. list_move(&page->lru, &h->hugepage_activelist);
  1009. spin_unlock(&hugetlb_lock);
  1010. }
  1011. set_page_private(page, (unsigned long)spool);
  1012. vma_commit_reservation(h, vma, addr);
  1013. return page;
  1014. }
  1015. int __weak alloc_bootmem_huge_page(struct hstate *h)
  1016. {
  1017. struct huge_bootmem_page *m;
  1018. int nr_nodes = nodes_weight(node_states[N_MEMORY]);
  1019. while (nr_nodes) {
  1020. void *addr;
  1021. addr = __alloc_bootmem_node_nopanic(
  1022. NODE_DATA(hstate_next_node_to_alloc(h,
  1023. &node_states[N_MEMORY])),
  1024. huge_page_size(h), huge_page_size(h), 0);
  1025. if (addr) {
  1026. /*
  1027. * Use the beginning of the huge page to store the
  1028. * huge_bootmem_page struct (until gather_bootmem
  1029. * puts them into the mem_map).
  1030. */
  1031. m = addr;
  1032. goto found;
  1033. }
  1034. nr_nodes--;
  1035. }
  1036. return 0;
  1037. found:
  1038. BUG_ON((unsigned long)virt_to_phys(m) & (huge_page_size(h) - 1));
  1039. /* Put them into a private list first because mem_map is not up yet */
  1040. list_add(&m->list, &huge_boot_pages);
  1041. m->hstate = h;
  1042. return 1;
  1043. }
  1044. static void prep_compound_huge_page(struct page *page, int order)
  1045. {
  1046. if (unlikely(order > (MAX_ORDER - 1)))
  1047. prep_compound_gigantic_page(page, order);
  1048. else
  1049. prep_compound_page(page, order);
  1050. }
  1051. /* Put bootmem huge pages into the standard lists after mem_map is up */
  1052. static void __init gather_bootmem_prealloc(void)
  1053. {
  1054. struct huge_bootmem_page *m;
  1055. list_for_each_entry(m, &huge_boot_pages, list) {
  1056. struct hstate *h = m->hstate;
  1057. struct page *page;
  1058. #ifdef CONFIG_HIGHMEM
  1059. page = pfn_to_page(m->phys >> PAGE_SHIFT);
  1060. free_bootmem_late((unsigned long)m,
  1061. sizeof(struct huge_bootmem_page));
  1062. #else
  1063. page = virt_to_page(m);
  1064. #endif
  1065. __ClearPageReserved(page);
  1066. WARN_ON(page_count(page) != 1);
  1067. prep_compound_huge_page(page, h->order);
  1068. prep_new_huge_page(h, page, page_to_nid(page));
  1069. /*
  1070. * If we had gigantic hugepages allocated at boot time, we need
  1071. * to restore the 'stolen' pages to totalram_pages in order to
  1072. * fix confusing memory reports from free(1) and another
  1073. * side-effects, like CommitLimit going negative.
  1074. */
  1075. if (h->order > (MAX_ORDER - 1))
  1076. totalram_pages += 1 << h->order;
  1077. }
  1078. }
  1079. static void __init hugetlb_hstate_alloc_pages(struct hstate *h)
  1080. {
  1081. unsigned long i;
  1082. for (i = 0; i < h->max_huge_pages; ++i) {
  1083. if (h->order >= MAX_ORDER) {
  1084. if (!alloc_bootmem_huge_page(h))
  1085. break;
  1086. } else if (!alloc_fresh_huge_page(h,
  1087. &node_states[N_MEMORY]))
  1088. break;
  1089. }
  1090. h->max_huge_pages = i;
  1091. }
  1092. static void __init hugetlb_init_hstates(void)
  1093. {
  1094. struct hstate *h;
  1095. for_each_hstate(h) {
  1096. /* oversize hugepages were init'ed in early boot */
  1097. if (h->order < MAX_ORDER)
  1098. hugetlb_hstate_alloc_pages(h);
  1099. }
  1100. }
  1101. static char * __init memfmt(char *buf, unsigned long n)
  1102. {
  1103. if (n >= (1UL << 30))
  1104. sprintf(buf, "%lu GB", n >> 30);
  1105. else if (n >= (1UL << 20))
  1106. sprintf(buf, "%lu MB", n >> 20);
  1107. else
  1108. sprintf(buf, "%lu KB", n >> 10);
  1109. return buf;
  1110. }
  1111. static void __init report_hugepages(void)
  1112. {
  1113. struct hstate *h;
  1114. for_each_hstate(h) {
  1115. char buf[32];
  1116. pr_info("HugeTLB registered %s page size, pre-allocated %ld pages\n",
  1117. memfmt(buf, huge_page_size(h)),
  1118. h->free_huge_pages);
  1119. }
  1120. }
  1121. #ifdef CONFIG_HIGHMEM
  1122. static void try_to_free_low(struct hstate *h, unsigned long count,
  1123. nodemask_t *nodes_allowed)
  1124. {
  1125. int i;
  1126. if (h->order >= MAX_ORDER)
  1127. return;
  1128. for_each_node_mask(i, *nodes_allowed) {
  1129. struct page *page, *next;
  1130. struct list_head *freel = &h->hugepage_freelists[i];
  1131. list_for_each_entry_safe(page, next, freel, lru) {
  1132. if (count >= h->nr_huge_pages)
  1133. return;
  1134. if (PageHighMem(page))
  1135. continue;
  1136. list_del(&page->lru);
  1137. update_and_free_page(h, page);
  1138. h->free_huge_pages--;
  1139. h->free_huge_pages_node[page_to_nid(page)]--;
  1140. }
  1141. }
  1142. }
  1143. #else
  1144. static inline void try_to_free_low(struct hstate *h, unsigned long count,
  1145. nodemask_t *nodes_allowed)
  1146. {
  1147. }
  1148. #endif
  1149. /*
  1150. * Increment or decrement surplus_huge_pages. Keep node-specific counters
  1151. * balanced by operating on them in a round-robin fashion.
  1152. * Returns 1 if an adjustment was made.
  1153. */
  1154. static int adjust_pool_surplus(struct hstate *h, nodemask_t *nodes_allowed,
  1155. int delta)
  1156. {
  1157. int start_nid, next_nid;
  1158. int ret = 0;
  1159. VM_BUG_ON(delta != -1 && delta != 1);
  1160. if (delta < 0)
  1161. start_nid = hstate_next_node_to_alloc(h, nodes_allowed);
  1162. else
  1163. start_nid = hstate_next_node_to_free(h, nodes_allowed);
  1164. next_nid = start_nid;
  1165. do {
  1166. int nid = next_nid;
  1167. if (delta < 0) {
  1168. /*
  1169. * To shrink on this node, there must be a surplus page
  1170. */
  1171. if (!h->surplus_huge_pages_node[nid]) {
  1172. next_nid = hstate_next_node_to_alloc(h,
  1173. nodes_allowed);
  1174. continue;
  1175. }
  1176. }
  1177. if (delta > 0) {
  1178. /*
  1179. * Surplus cannot exceed the total number of pages
  1180. */
  1181. if (h->surplus_huge_pages_node[nid] >=
  1182. h->nr_huge_pages_node[nid]) {
  1183. next_nid = hstate_next_node_to_free(h,
  1184. nodes_allowed);
  1185. continue;
  1186. }
  1187. }
  1188. h->surplus_huge_pages += delta;
  1189. h->surplus_huge_pages_node[nid] += delta;
  1190. ret = 1;
  1191. break;
  1192. } while (next_nid != start_nid);
  1193. return ret;
  1194. }
  1195. #define persistent_huge_pages(h) (h->nr_huge_pages - h->surplus_huge_pages)
  1196. static unsigned long set_max_huge_pages(struct hstate *h, unsigned long count,
  1197. nodemask_t *nodes_allowed)
  1198. {
  1199. unsigned long min_count, ret;
  1200. if (h->order >= MAX_ORDER)
  1201. return h->max_huge_pages;
  1202. /*
  1203. * Increase the pool size
  1204. * First take pages out of surplus state. Then make up the
  1205. * remaining difference by allocating fresh huge pages.
  1206. *
  1207. * We might race with alloc_buddy_huge_page() here and be unable
  1208. * to convert a surplus huge page to a normal huge page. That is
  1209. * not critical, though, it just means the overall size of the
  1210. * pool might be one hugepage larger than it needs to be, but
  1211. * within all the constraints specified by the sysctls.
  1212. */
  1213. spin_lock(&hugetlb_lock);
  1214. while (h->surplus_huge_pages && count > persistent_huge_pages(h)) {
  1215. if (!adjust_pool_surplus(h, nodes_allowed, -1))
  1216. break;
  1217. }
  1218. while (count > persistent_huge_pages(h)) {
  1219. /*
  1220. * If this allocation races such that we no longer need the
  1221. * page, free_huge_page will handle it by freeing the page
  1222. * and reducing the surplus.
  1223. */
  1224. spin_unlock(&hugetlb_lock);
  1225. ret = alloc_fresh_huge_page(h, nodes_allowed);
  1226. spin_lock(&hugetlb_lock);
  1227. if (!ret)
  1228. goto out;
  1229. /* Bail for signals. Probably ctrl-c from user */
  1230. if (signal_pending(current))
  1231. goto out;
  1232. }
  1233. /*
  1234. * Decrease the pool size
  1235. * First return free pages to the buddy allocator (being careful
  1236. * to keep enough around to satisfy reservations). Then place
  1237. * pages into surplus state as needed so the pool will shrink
  1238. * to the desired size as pages become free.
  1239. *
  1240. * By placing pages into the surplus state independent of the
  1241. * overcommit value, we are allowing the surplus pool size to
  1242. * exceed overcommit. There are few sane options here. Since
  1243. * alloc_buddy_huge_page() is checking the global counter,
  1244. * though, we'll note that we're not allowed to exceed surplus
  1245. * and won't grow the pool anywhere else. Not until one of the
  1246. * sysctls are changed, or the surplus pages go out of use.
  1247. */
  1248. min_count = h->resv_huge_pages + h->nr_huge_pages - h->free_huge_pages;
  1249. min_count = max(count, min_count);
  1250. try_to_free_low(h, min_count, nodes_allowed);
  1251. while (min_count < persistent_huge_pages(h)) {
  1252. if (!free_pool_huge_page(h, nodes_allowed, 0))
  1253. break;
  1254. }
  1255. while (count < persistent_huge_pages(h)) {
  1256. if (!adjust_pool_surplus(h, nodes_allowed, 1))
  1257. break;
  1258. }
  1259. out:
  1260. ret = persistent_huge_pages(h);
  1261. spin_unlock(&hugetlb_lock);
  1262. return ret;
  1263. }
  1264. #define HSTATE_ATTR_RO(_name) \
  1265. static struct kobj_attribute _name##_attr = __ATTR_RO(_name)
  1266. #define HSTATE_ATTR(_name) \
  1267. static struct kobj_attribute _name##_attr = \
  1268. __ATTR(_name, 0644, _name##_show, _name##_store)
  1269. static struct kobject *hugepages_kobj;
  1270. static struct kobject *hstate_kobjs[HUGE_MAX_HSTATE];
  1271. static struct hstate *kobj_to_node_hstate(struct kobject *kobj, int *nidp);
  1272. static struct hstate *kobj_to_hstate(struct kobject *kobj, int *nidp)
  1273. {
  1274. int i;
  1275. for (i = 0; i < HUGE_MAX_HSTATE; i++)
  1276. if (hstate_kobjs[i] == kobj) {
  1277. if (nidp)
  1278. *nidp = NUMA_NO_NODE;
  1279. return &hstates[i];
  1280. }
  1281. return kobj_to_node_hstate(kobj, nidp);
  1282. }
  1283. static ssize_t nr_hugepages_show_common(struct kobject *kobj,
  1284. struct kobj_attribute *attr, char *buf)
  1285. {
  1286. struct hstate *h;
  1287. unsigned long nr_huge_pages;
  1288. int nid;
  1289. h = kobj_to_hstate(kobj, &nid);
  1290. if (nid == NUMA_NO_NODE)
  1291. nr_huge_pages = h->nr_huge_pages;
  1292. else
  1293. nr_huge_pages = h->nr_huge_pages_node[nid];
  1294. return sprintf(buf, "%lu\n", nr_huge_pages);
  1295. }
  1296. static ssize_t nr_hugepages_store_common(bool obey_mempolicy,
  1297. struct kobject *kobj, struct kobj_attribute *attr,
  1298. const char *buf, size_t len)
  1299. {
  1300. int err;
  1301. int nid;
  1302. unsigned long count;
  1303. struct hstate *h;
  1304. NODEMASK_ALLOC(nodemask_t, nodes_allowed, GFP_KERNEL | __GFP_NORETRY);
  1305. err = strict_strtoul(buf, 10, &count);
  1306. if (err)
  1307. goto out;
  1308. h = kobj_to_hstate(kobj, &nid);
  1309. if (h->order >= MAX_ORDER) {
  1310. err = -EINVAL;
  1311. goto out;
  1312. }
  1313. if (nid == NUMA_NO_NODE) {
  1314. /*
  1315. * global hstate attribute
  1316. */
  1317. if (!(obey_mempolicy &&
  1318. init_nodemask_of_mempolicy(nodes_allowed))) {
  1319. NODEMASK_FREE(nodes_allowed);
  1320. nodes_allowed = &node_states[N_MEMORY];
  1321. }
  1322. } else if (nodes_allowed) {
  1323. /*
  1324. * per node hstate attribute: adjust count to global,
  1325. * but restrict alloc/free to the specified node.
  1326. */
  1327. count += h->nr_huge_pages - h->nr_huge_pages_node[nid];
  1328. init_nodemask_of_node(nodes_allowed, nid);
  1329. } else
  1330. nodes_allowed = &node_states[N_MEMORY];
  1331. h->max_huge_pages = set_max_huge_pages(h, count, nodes_allowed);
  1332. if (nodes_allowed != &node_states[N_MEMORY])
  1333. NODEMASK_FREE(nodes_allowed);
  1334. return len;
  1335. out:
  1336. NODEMASK_FREE(nodes_allowed);
  1337. return err;
  1338. }
  1339. static ssize_t nr_hugepages_show(struct kobject *kobj,
  1340. struct kobj_attribute *attr, char *buf)
  1341. {
  1342. return nr_hugepages_show_common(kobj, attr, buf);
  1343. }
  1344. static ssize_t nr_hugepages_store(struct kobject *kobj,
  1345. struct kobj_attribute *attr, const char *buf, size_t len)
  1346. {
  1347. return nr_hugepages_store_common(false, kobj, attr, buf, len);
  1348. }
  1349. HSTATE_ATTR(nr_hugepages);
  1350. #ifdef CONFIG_NUMA
  1351. /*
  1352. * hstate attribute for optionally mempolicy-based constraint on persistent
  1353. * huge page alloc/free.
  1354. */
  1355. static ssize_t nr_hugepages_mempolicy_show(struct kobject *kobj,
  1356. struct kobj_attribute *attr, char *buf)
  1357. {
  1358. return nr_hugepages_show_common(kobj, attr, buf);
  1359. }
  1360. static ssize_t nr_hugepages_mempolicy_store(struct kobject *kobj,
  1361. struct kobj_attribute *attr, const char *buf, size_t len)
  1362. {
  1363. return nr_hugepages_store_common(true, kobj, attr, buf, len);
  1364. }
  1365. HSTATE_ATTR(nr_hugepages_mempolicy);
  1366. #endif
  1367. static ssize_t nr_overcommit_hugepages_show(struct kobject *kobj,
  1368. struct kobj_attribute *attr, char *buf)
  1369. {
  1370. struct hstate *h = kobj_to_hstate(kobj, NULL);
  1371. return sprintf(buf, "%lu\n", h->nr_overcommit_huge_pages);
  1372. }
  1373. static ssize_t nr_overcommit_hugepages_store(struct kobject *kobj,
  1374. struct kobj_attribute *attr, const char *buf, size_t count)
  1375. {
  1376. int err;
  1377. unsigned long input;
  1378. struct hstate *h = kobj_to_hstate(kobj, NULL);
  1379. if (h->order >= MAX_ORDER)
  1380. return -EINVAL;
  1381. err = strict_strtoul(buf, 10, &input);
  1382. if (err)
  1383. return err;
  1384. spin_lock(&hugetlb_lock);
  1385. h->nr_overcommit_huge_pages = input;
  1386. spin_unlock(&hugetlb_lock);
  1387. return count;
  1388. }
  1389. HSTATE_ATTR(nr_overcommit_hugepages);
  1390. static ssize_t free_hugepages_show(struct kobject *kobj,
  1391. struct kobj_attribute *attr, char *buf)
  1392. {
  1393. struct hstate *h;
  1394. unsigned long free_huge_pages;
  1395. int nid;
  1396. h = kobj_to_hstate(kobj, &nid);
  1397. if (nid == NUMA_NO_NODE)
  1398. free_huge_pages = h->free_huge_pages;
  1399. else
  1400. free_huge_pages = h->free_huge_pages_node[nid];
  1401. return sprintf(buf, "%lu\n", free_huge_pages);
  1402. }
  1403. HSTATE_ATTR_RO(free_hugepages);
  1404. static ssize_t resv_hugepages_show(struct kobject *kobj,
  1405. struct kobj_attribute *attr, char *buf)
  1406. {
  1407. struct hstate *h = kobj_to_hstate(kobj, NULL);
  1408. return sprintf(buf, "%lu\n", h->resv_huge_pages);
  1409. }
  1410. HSTATE_ATTR_RO(resv_hugepages);
  1411. static ssize_t surplus_hugepages_show(struct kobject *kobj,
  1412. struct kobj_attribute *attr, char *buf)
  1413. {
  1414. struct hstate *h;
  1415. unsigned long surplus_huge_pages;
  1416. int nid;
  1417. h = kobj_to_hstate(kobj, &nid);
  1418. if (nid == NUMA_NO_NODE)
  1419. surplus_huge_pages = h->surplus_huge_pages;
  1420. else
  1421. surplus_huge_pages = h->surplus_huge_pages_node[nid];
  1422. return sprintf(buf, "%lu\n", surplus_huge_pages);
  1423. }
  1424. HSTATE_ATTR_RO(surplus_hugepages);
  1425. static struct attribute *hstate_attrs[] = {
  1426. &nr_hugepages_attr.attr,
  1427. &nr_overcommit_hugepages_attr.attr,
  1428. &free_hugepages_attr.attr,
  1429. &resv_hugepages_attr.attr,
  1430. &surplus_hugepages_attr.attr,
  1431. #ifdef CONFIG_NUMA
  1432. &nr_hugepages_mempolicy_attr.attr,
  1433. #endif
  1434. NULL,
  1435. };
  1436. static struct attribute_group hstate_attr_group = {
  1437. .attrs = hstate_attrs,
  1438. };
  1439. static int hugetlb_sysfs_add_hstate(struct hstate *h, struct kobject *parent,
  1440. struct kobject **hstate_kobjs,
  1441. struct attribute_group *hstate_attr_group)
  1442. {
  1443. int retval;
  1444. int hi = hstate_index(h);
  1445. hstate_kobjs[hi] = kobject_create_and_add(h->name, parent);
  1446. if (!hstate_kobjs[hi])
  1447. return -ENOMEM;
  1448. retval = sysfs_create_group(hstate_kobjs[hi], hstate_attr_group);
  1449. if (retval)
  1450. kobject_put(hstate_kobjs[hi]);
  1451. return retval;
  1452. }
  1453. static void __init hugetlb_sysfs_init(void)
  1454. {
  1455. struct hstate *h;
  1456. int err;
  1457. hugepages_kobj = kobject_create_and_add("hugepages", mm_kobj);
  1458. if (!hugepages_kobj)
  1459. return;
  1460. for_each_hstate(h) {
  1461. err = hugetlb_sysfs_add_hstate(h, hugepages_kobj,
  1462. hstate_kobjs, &hstate_attr_group);
  1463. if (err)
  1464. pr_err("Hugetlb: Unable to add hstate %s", h->name);
  1465. }
  1466. }
  1467. #ifdef CONFIG_NUMA
  1468. /*
  1469. * node_hstate/s - associate per node hstate attributes, via their kobjects,
  1470. * with node devices in node_devices[] using a parallel array. The array
  1471. * index of a node device or _hstate == node id.
  1472. * This is here to avoid any static dependency of the node device driver, in
  1473. * the base kernel, on the hugetlb module.
  1474. */
  1475. struct node_hstate {
  1476. struct kobject *hugepages_kobj;
  1477. struct kobject *hstate_kobjs[HUGE_MAX_HSTATE];
  1478. };
  1479. struct node_hstate node_hstates[MAX_NUMNODES];
  1480. /*
  1481. * A subset of global hstate attributes for node devices
  1482. */
  1483. static struct attribute *per_node_hstate_attrs[] = {
  1484. &nr_hugepages_attr.attr,
  1485. &free_hugepages_attr.attr,
  1486. &surplus_hugepages_attr.attr,
  1487. NULL,
  1488. };
  1489. static struct attribute_group per_node_hstate_attr_group = {
  1490. .attrs = per_node_hstate_attrs,
  1491. };
  1492. /*
  1493. * kobj_to_node_hstate - lookup global hstate for node device hstate attr kobj.
  1494. * Returns node id via non-NULL nidp.
  1495. */
  1496. static struct hstate *kobj_to_node_hstate(struct kobject *kobj, int *nidp)
  1497. {
  1498. int nid;
  1499. for (nid = 0; nid < nr_node_ids; nid++) {
  1500. struct node_hstate *nhs = &node_hstates[nid];
  1501. int i;
  1502. for (i = 0; i < HUGE_MAX_HSTATE; i++)
  1503. if (nhs->hstate_kobjs[i] == kobj) {
  1504. if (nidp)
  1505. *nidp = nid;
  1506. return &hstates[i];
  1507. }
  1508. }
  1509. BUG();
  1510. return NULL;
  1511. }
  1512. /*
  1513. * Unregister hstate attributes from a single node device.
  1514. * No-op if no hstate attributes attached.
  1515. */
  1516. void hugetlb_unregister_node(struct node *node)
  1517. {
  1518. struct hstate *h;
  1519. struct node_hstate *nhs = &node_hstates[node->dev.id];
  1520. if (!nhs->hugepages_kobj)
  1521. return; /* no hstate attributes */
  1522. for_each_hstate(h) {
  1523. int idx = hstate_index(h);
  1524. if (nhs->hstate_kobjs[idx]) {
  1525. kobject_put(nhs->hstate_kobjs[idx]);
  1526. nhs->hstate_kobjs[idx] = NULL;
  1527. }
  1528. }
  1529. kobject_put(nhs->hugepages_kobj);
  1530. nhs->hugepages_kobj = NULL;
  1531. }
  1532. /*
  1533. * hugetlb module exit: unregister hstate attributes from node devices
  1534. * that have them.
  1535. */
  1536. static void hugetlb_unregister_all_nodes(void)
  1537. {
  1538. int nid;
  1539. /*
  1540. * disable node device registrations.
  1541. */
  1542. register_hugetlbfs_with_node(NULL, NULL);
  1543. /*
  1544. * remove hstate attributes from any nodes that have them.
  1545. */
  1546. for (nid = 0; nid < nr_node_ids; nid++)
  1547. hugetlb_unregister_node(node_devices[nid]);
  1548. }
  1549. /*
  1550. * Register hstate attributes for a single node device.
  1551. * No-op if attributes already registered.
  1552. */
  1553. void hugetlb_register_node(struct node *node)
  1554. {
  1555. struct hstate *h;
  1556. struct node_hstate *nhs = &node_hstates[node->dev.id];
  1557. int err;
  1558. if (nhs->hugepages_kobj)
  1559. return; /* already allocated */
  1560. nhs->hugepages_kobj = kobject_create_and_add("hugepages",
  1561. &node->dev.kobj);
  1562. if (!nhs->hugepages_kobj)
  1563. return;
  1564. for_each_hstate(h) {
  1565. err = hugetlb_sysfs_add_hstate(h, nhs->hugepages_kobj,
  1566. nhs->hstate_kobjs,
  1567. &per_node_hstate_attr_group);
  1568. if (err) {
  1569. pr_err("Hugetlb: Unable to add hstate %s for node %d\n",
  1570. h->name, node->dev.id);
  1571. hugetlb_unregister_node(node);
  1572. break;
  1573. }
  1574. }
  1575. }
  1576. /*
  1577. * hugetlb init time: register hstate attributes for all registered node
  1578. * devices of nodes that have memory. All on-line nodes should have
  1579. * registered their associated device by this time.
  1580. */
  1581. static void hugetlb_register_all_nodes(void)
  1582. {
  1583. int nid;
  1584. for_each_node_state(nid, N_MEMORY) {
  1585. struct node *node = node_devices[nid];
  1586. if (node->dev.id == nid)
  1587. hugetlb_register_node(node);
  1588. }
  1589. /*
  1590. * Let the node device driver know we're here so it can
  1591. * [un]register hstate attributes on node hotplug.
  1592. */
  1593. register_hugetlbfs_with_node(hugetlb_register_node,
  1594. hugetlb_unregister_node);
  1595. }
  1596. #else /* !CONFIG_NUMA */
  1597. static struct hstate *kobj_to_node_hstate(struct kobject *kobj, int *nidp)
  1598. {
  1599. BUG();
  1600. if (nidp)
  1601. *nidp = -1;
  1602. return NULL;
  1603. }
  1604. static void hugetlb_unregister_all_nodes(void) { }
  1605. static void hugetlb_register_all_nodes(void) { }
  1606. #endif
  1607. static void __exit hugetlb_exit(void)
  1608. {
  1609. struct hstate *h;
  1610. hugetlb_unregister_all_nodes();
  1611. for_each_hstate(h) {
  1612. kobject_put(hstate_kobjs[hstate_index(h)]);
  1613. }
  1614. kobject_put(hugepages_kobj);
  1615. }
  1616. module_exit(hugetlb_exit);
  1617. static int __init hugetlb_init(void)
  1618. {
  1619. /* Some platform decide whether they support huge pages at boot
  1620. * time. On these, such as powerpc, HPAGE_SHIFT is set to 0 when
  1621. * there is no such support
  1622. */
  1623. if (HPAGE_SHIFT == 0)
  1624. return 0;
  1625. if (!size_to_hstate(default_hstate_size)) {
  1626. default_hstate_size = HPAGE_SIZE;
  1627. if (!size_to_hstate(default_hstate_size))
  1628. hugetlb_add_hstate(HUGETLB_PAGE_ORDER);
  1629. }
  1630. default_hstate_idx = hstate_index(size_to_hstate(default_hstate_size));
  1631. if (default_hstate_max_huge_pages)
  1632. default_hstate.max_huge_pages = default_hstate_max_huge_pages;
  1633. hugetlb_init_hstates();
  1634. gather_bootmem_prealloc();
  1635. report_hugepages();
  1636. hugetlb_sysfs_init();
  1637. hugetlb_register_all_nodes();
  1638. hugetlb_cgroup_file_init();
  1639. return 0;
  1640. }
  1641. module_init(hugetlb_init);
  1642. /* Should be called on processing a hugepagesz=... option */
  1643. void __init hugetlb_add_hstate(unsigned order)
  1644. {
  1645. struct hstate *h;
  1646. unsigned long i;
  1647. if (size_to_hstate(PAGE_SIZE << order)) {
  1648. pr_warning("hugepagesz= specified twice, ignoring\n");
  1649. return;
  1650. }
  1651. BUG_ON(hugetlb_max_hstate >= HUGE_MAX_HSTATE);
  1652. BUG_ON(order == 0);
  1653. h = &hstates[hugetlb_max_hstate++];
  1654. h->order = order;
  1655. h->mask = ~((1ULL << (order + PAGE_SHIFT)) - 1);
  1656. h->nr_huge_pages = 0;
  1657. h->free_huge_pages = 0;
  1658. for (i = 0; i < MAX_NUMNODES; ++i)
  1659. INIT_LIST_HEAD(&h->hugepage_freelists[i]);
  1660. INIT_LIST_HEAD(&h->hugepage_activelist);
  1661. h->next_nid_to_alloc = first_node(node_states[N_MEMORY]);
  1662. h->next_nid_to_free = first_node(node_states[N_MEMORY]);
  1663. snprintf(h->name, HSTATE_NAME_LEN, "hugepages-%lukB",
  1664. huge_page_size(h)/1024);
  1665. parsed_hstate = h;
  1666. }
  1667. static int __init hugetlb_nrpages_setup(char *s)
  1668. {
  1669. unsigned long *mhp;
  1670. static unsigned long *last_mhp;
  1671. /*
  1672. * !hugetlb_max_hstate means we haven't parsed a hugepagesz= parameter yet,
  1673. * so this hugepages= parameter goes to the "default hstate".
  1674. */
  1675. if (!hugetlb_max_hstate)
  1676. mhp = &default_hstate_max_huge_pages;
  1677. else
  1678. mhp = &parsed_hstate->max_huge_pages;
  1679. if (mhp == last_mhp) {
  1680. pr_warning("hugepages= specified twice without "
  1681. "interleaving hugepagesz=, ignoring\n");
  1682. return 1;
  1683. }
  1684. if (sscanf(s, "%lu", mhp) <= 0)
  1685. *mhp = 0;
  1686. /*
  1687. * Global state is always initialized later in hugetlb_init.
  1688. * But we need to allocate >= MAX_ORDER hstates here early to still
  1689. * use the bootmem allocator.
  1690. */
  1691. if (hugetlb_max_hstate && parsed_hstate->order >= MAX_ORDER)
  1692. hugetlb_hstate_alloc_pages(parsed_hstate);
  1693. last_mhp = mhp;
  1694. return 1;
  1695. }
  1696. __setup("hugepages=", hugetlb_nrpages_setup);
  1697. static int __init hugetlb_default_setup(char *s)
  1698. {
  1699. default_hstate_size = memparse(s, &s);
  1700. return 1;
  1701. }
  1702. __setup("default_hugepagesz=", hugetlb_default_setup);
  1703. static unsigned int cpuset_mems_nr(unsigned int *array)
  1704. {
  1705. int node;
  1706. unsigned int nr = 0;
  1707. for_each_node_mask(node, cpuset_current_mems_allowed)
  1708. nr += array[node];
  1709. return nr;
  1710. }
  1711. #ifdef CONFIG_SYSCTL
  1712. static int hugetlb_sysctl_handler_common(bool obey_mempolicy,
  1713. struct ctl_table *table, int write,
  1714. void __user *buffer, size_t *length, loff_t *ppos)
  1715. {
  1716. struct hstate *h = &default_hstate;
  1717. unsigned long tmp;
  1718. int ret;
  1719. tmp = h->max_huge_pages;
  1720. if (write && h->order >= MAX_ORDER)
  1721. return -EINVAL;
  1722. table->data = &tmp;
  1723. table->maxlen = sizeof(unsigned long);
  1724. ret = proc_doulongvec_minmax(table, write, buffer, length, ppos);
  1725. if (ret)
  1726. goto out;
  1727. if (write) {
  1728. NODEMASK_ALLOC(nodemask_t, nodes_allowed,
  1729. GFP_KERNEL | __GFP_NORETRY);
  1730. if (!(obey_mempolicy &&
  1731. init_nodemask_of_mempolicy(nodes_allowed))) {
  1732. NODEMASK_FREE(nodes_allowed);
  1733. nodes_allowed = &node_states[N_MEMORY];
  1734. }
  1735. h->max_huge_pages = set_max_huge_pages(h, tmp, nodes_allowed);
  1736. if (nodes_allowed != &node_states[N_MEMORY])
  1737. NODEMASK_FREE(nodes_allowed);
  1738. }
  1739. out:
  1740. return ret;
  1741. }
  1742. int hugetlb_sysctl_handler(struct ctl_table *table, int write,
  1743. void __user *buffer, size_t *length, loff_t *ppos)
  1744. {
  1745. return hugetlb_sysctl_handler_common(false, table, write,
  1746. buffer, length, ppos);
  1747. }
  1748. #ifdef CONFIG_NUMA
  1749. int hugetlb_mempolicy_sysctl_handler(struct ctl_table *table, int write,
  1750. void __user *buffer, size_t *length, loff_t *ppos)
  1751. {
  1752. return hugetlb_sysctl_handler_common(true, table, write,
  1753. buffer, length, ppos);
  1754. }
  1755. #endif /* CONFIG_NUMA */
  1756. int hugetlb_treat_movable_handler(struct ctl_table *table, int write,
  1757. void __user *buffer,
  1758. size_t *length, loff_t *ppos)
  1759. {
  1760. proc_dointvec(table, write, buffer, length, ppos);
  1761. if (hugepages_treat_as_movable)
  1762. htlb_alloc_mask = GFP_HIGHUSER_MOVABLE;
  1763. else
  1764. htlb_alloc_mask = GFP_HIGHUSER;
  1765. return 0;
  1766. }
  1767. int hugetlb_overcommit_handler(struct ctl_table *table, int write,
  1768. void __user *buffer,
  1769. size_t *length, loff_t *ppos)
  1770. {
  1771. struct hstate *h = &default_hstate;
  1772. unsigned long tmp;
  1773. int ret;
  1774. tmp = h->nr_overcommit_huge_pages;
  1775. if (write && h->order >= MAX_ORDER)
  1776. return -EINVAL;
  1777. table->data = &tmp;
  1778. table->maxlen = sizeof(unsigned long);
  1779. ret = proc_doulongvec_minmax(table, write, buffer, length, ppos);
  1780. if (ret)
  1781. goto out;
  1782. if (write) {
  1783. spin_lock(&hugetlb_lock);
  1784. h->nr_overcommit_huge_pages = tmp;
  1785. spin_unlock(&hugetlb_lock);
  1786. }
  1787. out:
  1788. return ret;
  1789. }
  1790. #endif /* CONFIG_SYSCTL */
  1791. void hugetlb_report_meminfo(struct seq_file *m)
  1792. {
  1793. struct hstate *h = &default_hstate;
  1794. seq_printf(m,
  1795. "HugePages_Total: %5lu\n"
  1796. "HugePages_Free: %5lu\n"
  1797. "HugePages_Rsvd: %5lu\n"
  1798. "HugePages_Surp: %5lu\n"
  1799. "Hugepagesize: %8lu kB\n",
  1800. h->nr_huge_pages,
  1801. h->free_huge_pages,
  1802. h->resv_huge_pages,
  1803. h->surplus_huge_pages,
  1804. 1UL << (huge_page_order(h) + PAGE_SHIFT - 10));
  1805. }
  1806. int hugetlb_report_node_meminfo(int nid, char *buf)
  1807. {
  1808. struct hstate *h = &default_hstate;
  1809. return sprintf(buf,
  1810. "Node %d HugePages_Total: %5u\n"
  1811. "Node %d HugePages_Free: %5u\n"
  1812. "Node %d HugePages_Surp: %5u\n",
  1813. nid, h->nr_huge_pages_node[nid],
  1814. nid, h->free_huge_pages_node[nid],
  1815. nid, h->surplus_huge_pages_node[nid]);
  1816. }
  1817. /* Return the number pages of memory we physically have, in PAGE_SIZE units. */
  1818. unsigned long hugetlb_total_pages(void)
  1819. {
  1820. struct hstate *h;
  1821. unsigned long nr_total_pages = 0;
  1822. for_each_hstate(h)
  1823. nr_total_pages += h->nr_huge_pages * pages_per_huge_page(h);
  1824. return nr_total_pages;
  1825. }
  1826. static int hugetlb_acct_memory(struct hstate *h, long delta)
  1827. {
  1828. int ret = -ENOMEM;
  1829. spin_lock(&hugetlb_lock);
  1830. /*
  1831. * When cpuset is configured, it breaks the strict hugetlb page
  1832. * reservation as the accounting is done on a global variable. Such
  1833. * reservation is completely rubbish in the presence of cpuset because
  1834. * the reservation is not checked against page availability for the
  1835. * current cpuset. Application can still potentially OOM'ed by kernel
  1836. * with lack of free htlb page in cpuset that the task is in.
  1837. * Attempt to enforce strict accounting with cpuset is almost
  1838. * impossible (or too ugly) because cpuset is too fluid that
  1839. * task or memory node can be dynamically moved between cpusets.
  1840. *
  1841. * The change of semantics for shared hugetlb mapping with cpuset is
  1842. * undesirable. However, in order to preserve some of the semantics,
  1843. * we fall back to check against current free page availability as
  1844. * a best attempt and hopefully to minimize the impact of changing
  1845. * semantics that cpuset has.
  1846. */
  1847. if (delta > 0) {
  1848. if (gather_surplus_pages(h, delta) < 0)
  1849. goto out;
  1850. if (delta > cpuset_mems_nr(h->free_huge_pages_node)) {
  1851. return_unused_surplus_pages(h, delta);
  1852. goto out;
  1853. }
  1854. }
  1855. ret = 0;
  1856. if (delta < 0)
  1857. return_unused_surplus_pages(h, (unsigned long) -delta);
  1858. out:
  1859. spin_unlock(&hugetlb_lock);
  1860. return ret;
  1861. }
  1862. static void hugetlb_vm_op_open(struct vm_area_struct *vma)
  1863. {
  1864. struct resv_map *reservations = vma_resv_map(vma);
  1865. /*
  1866. * This new VMA should share its siblings reservation map if present.
  1867. * The VMA will only ever have a valid reservation map pointer where
  1868. * it is being copied for another still existing VMA. As that VMA
  1869. * has a reference to the reservation map it cannot disappear until
  1870. * after this open call completes. It is therefore safe to take a
  1871. * new reference here without additional locking.
  1872. */
  1873. if (reservations)
  1874. kref_get(&reservations->refs);
  1875. }
  1876. static void resv_map_put(struct vm_area_struct *vma)
  1877. {
  1878. struct resv_map *reservations = vma_resv_map(vma);
  1879. if (!reservations)
  1880. return;
  1881. kref_put(&reservations->refs, resv_map_release);
  1882. }
  1883. static void hugetlb_vm_op_close(struct vm_area_struct *vma)
  1884. {
  1885. struct hstate *h = hstate_vma(vma);
  1886. struct resv_map *reservations = vma_resv_map(vma);
  1887. struct hugepage_subpool *spool = subpool_vma(vma);
  1888. unsigned long reserve;
  1889. unsigned long start;
  1890. unsigned long end;
  1891. if (reservations) {
  1892. start = vma_hugecache_offset(h, vma, vma->vm_start);
  1893. end = vma_hugecache_offset(h, vma, vma->vm_end);
  1894. reserve = (end - start) -
  1895. region_count(&reservations->regions, start, end);
  1896. resv_map_put(vma);
  1897. if (reserve) {
  1898. hugetlb_acct_memory(h, -reserve);
  1899. hugepage_subpool_put_pages(spool, reserve);
  1900. }
  1901. }
  1902. }
  1903. /*
  1904. * We cannot handle pagefaults against hugetlb pages at all. They cause
  1905. * handle_mm_fault() to try to instantiate regular-sized pages in the
  1906. * hugegpage VMA. do_page_fault() is supposed to trap this, so BUG is we get
  1907. * this far.
  1908. */
  1909. static int hugetlb_vm_op_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
  1910. {
  1911. BUG();
  1912. return 0;
  1913. }
  1914. const struct vm_operations_struct hugetlb_vm_ops = {
  1915. .fault = hugetlb_vm_op_fault,
  1916. .open = hugetlb_vm_op_open,
  1917. .close = hugetlb_vm_op_close,
  1918. };
  1919. static pte_t make_huge_pte(struct vm_area_struct *vma, struct page *page,
  1920. int writable)
  1921. {
  1922. pte_t entry;
  1923. if (writable) {
  1924. entry =
  1925. pte_mkwrite(pte_mkdirty(mk_pte(page, vma->vm_page_prot)));
  1926. } else {
  1927. entry = huge_pte_wrprotect(mk_pte(page, vma->vm_page_prot));
  1928. }
  1929. entry = pte_mkyoung(entry);
  1930. entry = pte_mkhuge(entry);
  1931. entry = arch_make_huge_pte(entry, vma, page, writable);
  1932. return entry;
  1933. }
  1934. static void set_huge_ptep_writable(struct vm_area_struct *vma,
  1935. unsigned long address, pte_t *ptep)
  1936. {
  1937. pte_t entry;
  1938. entry = pte_mkwrite(pte_mkdirty(huge_ptep_get(ptep)));
  1939. if (huge_ptep_set_access_flags(vma, address, ptep, entry, 1))
  1940. update_mmu_cache(vma, address, ptep);
  1941. }
  1942. int copy_hugetlb_page_range(struct mm_struct *dst, struct mm_struct *src,
  1943. struct vm_area_struct *vma)
  1944. {
  1945. pte_t *src_pte, *dst_pte, entry;
  1946. struct page *ptepage;
  1947. unsigned long addr;
  1948. int cow;
  1949. struct hstate *h = hstate_vma(vma);
  1950. unsigned long sz = huge_page_size(h);
  1951. cow = (vma->vm_flags & (VM_SHARED | VM_MAYWRITE)) == VM_MAYWRITE;
  1952. for (addr = vma->vm_start; addr < vma->vm_end; addr += sz) {
  1953. src_pte = huge_pte_offset(src, addr);
  1954. if (!src_pte)
  1955. continue;
  1956. dst_pte = huge_pte_alloc(dst, addr, sz);
  1957. if (!dst_pte)
  1958. goto nomem;
  1959. /* If the pagetables are shared don't copy or take references */
  1960. if (dst_pte == src_pte)
  1961. continue;
  1962. spin_lock(&dst->page_table_lock);
  1963. spin_lock_nested(&src->page_table_lock, SINGLE_DEPTH_NESTING);
  1964. if (!huge_pte_none(huge_ptep_get(src_pte))) {
  1965. if (cow)
  1966. huge_ptep_set_wrprotect(src, addr, src_pte);
  1967. entry = huge_ptep_get(src_pte);
  1968. ptepage = pte_page(entry);
  1969. get_page(ptepage);
  1970. page_dup_rmap(ptepage);
  1971. set_huge_pte_at(dst, addr, dst_pte, entry);
  1972. }
  1973. spin_unlock(&src->page_table_lock);
  1974. spin_unlock(&dst->page_table_lock);
  1975. }
  1976. return 0;
  1977. nomem:
  1978. return -ENOMEM;
  1979. }
  1980. static int is_hugetlb_entry_migration(pte_t pte)
  1981. {
  1982. swp_entry_t swp;
  1983. if (huge_pte_none(pte) || pte_present(pte))
  1984. return 0;
  1985. swp = pte_to_swp_entry(pte);
  1986. if (non_swap_entry(swp) && is_migration_entry(swp))
  1987. return 1;
  1988. else
  1989. return 0;
  1990. }
  1991. static int is_hugetlb_entry_hwpoisoned(pte_t pte)
  1992. {
  1993. swp_entry_t swp;
  1994. if (huge_pte_none(pte) || pte_present(pte))
  1995. return 0;
  1996. swp = pte_to_swp_entry(pte);
  1997. if (non_swap_entry(swp) && is_hwpoison_entry(swp))
  1998. return 1;
  1999. else
  2000. return 0;
  2001. }
  2002. void __unmap_hugepage_range(struct mmu_gather *tlb, struct vm_area_struct *vma,
  2003. unsigned long start, unsigned long end,
  2004. struct page *ref_page)
  2005. {
  2006. int force_flush = 0;
  2007. struct mm_struct *mm = vma->vm_mm;
  2008. unsigned long address;
  2009. pte_t *ptep;
  2010. pte_t pte;
  2011. struct page *page;
  2012. struct hstate *h = hstate_vma(vma);
  2013. unsigned long sz = huge_page_size(h);
  2014. const unsigned long mmun_start = start; /* For mmu_notifiers */
  2015. const unsigned long mmun_end = end; /* For mmu_notifiers */
  2016. WARN_ON(!is_vm_hugetlb_page(vma));
  2017. BUG_ON(start & ~huge_page_mask(h));
  2018. BUG_ON(end & ~huge_page_mask(h));
  2019. tlb_start_vma(tlb, vma);
  2020. mmu_notifier_invalidate_range_start(mm, mmun_start, mmun_end);
  2021. again:
  2022. spin_lock(&mm->page_table_lock);
  2023. for (address = start; address < end; address += sz) {
  2024. ptep = huge_pte_offset(mm, address);
  2025. if (!ptep)
  2026. continue;
  2027. if (huge_pmd_unshare(mm, &address, ptep))
  2028. continue;
  2029. pte = huge_ptep_get(ptep);
  2030. if (huge_pte_none(pte))
  2031. continue;
  2032. /*
  2033. * HWPoisoned hugepage is already unmapped and dropped reference
  2034. */
  2035. if (unlikely(is_hugetlb_entry_hwpoisoned(pte))) {
  2036. pte_clear(mm, address, ptep);
  2037. continue;
  2038. }
  2039. page = pte_page(pte);
  2040. /*
  2041. * If a reference page is supplied, it is because a specific
  2042. * page is being unmapped, not a range. Ensure the page we
  2043. * are about to unmap is the actual page of interest.
  2044. */
  2045. if (ref_page) {
  2046. if (page != ref_page)
  2047. continue;
  2048. /*
  2049. * Mark the VMA as having unmapped its page so that
  2050. * future faults in this VMA will fail rather than
  2051. * looking like data was lost
  2052. */
  2053. set_vma_resv_flags(vma, HPAGE_RESV_UNMAPPED);
  2054. }
  2055. pte = huge_ptep_get_and_clear(mm, address, ptep);
  2056. tlb_remove_tlb_entry(tlb, ptep, address);
  2057. if (pte_dirty(pte))
  2058. set_page_dirty(page);
  2059. page_remove_rmap(page);
  2060. force_flush = !__tlb_remove_page(tlb, page);
  2061. if (force_flush)
  2062. break;
  2063. /* Bail out after unmapping reference page if supplied */
  2064. if (ref_page)
  2065. break;
  2066. }
  2067. spin_unlock(&mm->page_table_lock);
  2068. /*
  2069. * mmu_gather ran out of room to batch pages, we break out of
  2070. * the PTE lock to avoid doing the potential expensive TLB invalidate
  2071. * and page-free while holding it.
  2072. */
  2073. if (force_flush) {
  2074. force_flush = 0;
  2075. tlb_flush_mmu(tlb);
  2076. if (address < end && !ref_page)
  2077. goto again;
  2078. }
  2079. mmu_notifier_invalidate_range_end(mm, mmun_start, mmun_end);
  2080. tlb_end_vma(tlb, vma);
  2081. }
  2082. void __unmap_hugepage_range_final(struct mmu_gather *tlb,
  2083. struct vm_area_struct *vma, unsigned long start,
  2084. unsigned long end, struct page *ref_page)
  2085. {
  2086. __unmap_hugepage_range(tlb, vma, start, end, ref_page);
  2087. /*
  2088. * Clear this flag so that x86's huge_pmd_share page_table_shareable
  2089. * test will fail on a vma being torn down, and not grab a page table
  2090. * on its way out. We're lucky that the flag has such an appropriate
  2091. * name, and can in fact be safely cleared here. We could clear it
  2092. * before the __unmap_hugepage_range above, but all that's necessary
  2093. * is to clear it before releasing the i_mmap_mutex. This works
  2094. * because in the context this is called, the VMA is about to be
  2095. * destroyed and the i_mmap_mutex is held.
  2096. */
  2097. vma->vm_flags &= ~VM_MAYSHARE;
  2098. }
  2099. void unmap_hugepage_range(struct vm_area_struct *vma, unsigned long start,
  2100. unsigned long end, struct page *ref_page)
  2101. {
  2102. struct mm_struct *mm;
  2103. struct mmu_gather tlb;
  2104. mm = vma->vm_mm;
  2105. tlb_gather_mmu(&tlb, mm, 0);
  2106. __unmap_hugepage_range(&tlb, vma, start, end, ref_page);
  2107. tlb_finish_mmu(&tlb, start, end);
  2108. }
  2109. /*
  2110. * This is called when the original mapper is failing to COW a MAP_PRIVATE
  2111. * mappping it owns the reserve page for. The intention is to unmap the page
  2112. * from other VMAs and let the children be SIGKILLed if they are faulting the
  2113. * same region.
  2114. */
  2115. static int unmap_ref_private(struct mm_struct *mm, struct vm_area_struct *vma,
  2116. struct page *page, unsigned long address)
  2117. {
  2118. struct hstate *h = hstate_vma(vma);
  2119. struct vm_area_struct *iter_vma;
  2120. struct address_space *mapping;
  2121. pgoff_t pgoff;
  2122. /*
  2123. * vm_pgoff is in PAGE_SIZE units, hence the different calculation
  2124. * from page cache lookup which is in HPAGE_SIZE units.
  2125. */
  2126. address = address & huge_page_mask(h);
  2127. pgoff = ((address - vma->vm_start) >> PAGE_SHIFT) +
  2128. vma->vm_pgoff;
  2129. mapping = file_inode(vma->vm_file)->i_mapping;
  2130. /*
  2131. * Take the mapping lock for the duration of the table walk. As
  2132. * this mapping should be shared between all the VMAs,
  2133. * __unmap_hugepage_range() is called as the lock is already held
  2134. */
  2135. mutex_lock(&mapping->i_mmap_mutex);
  2136. vma_interval_tree_foreach(iter_vma, &mapping->i_mmap, pgoff, pgoff) {
  2137. /* Do not unmap the current VMA */
  2138. if (iter_vma == vma)
  2139. continue;
  2140. /*
  2141. * Unmap the page from other VMAs without their own reserves.
  2142. * They get marked to be SIGKILLed if they fault in these
  2143. * areas. This is because a future no-page fault on this VMA
  2144. * could insert a zeroed page instead of the data existing
  2145. * from the time of fork. This would look like data corruption
  2146. */
  2147. if (!is_vma_resv_set(iter_vma, HPAGE_RESV_OWNER))
  2148. unmap_hugepage_range(iter_vma, address,
  2149. address + huge_page_size(h), page);
  2150. }
  2151. mutex_unlock(&mapping->i_mmap_mutex);
  2152. return 1;
  2153. }
  2154. /*
  2155. * Hugetlb_cow() should be called with page lock of the original hugepage held.
  2156. * Called with hugetlb_instantiation_mutex held and pte_page locked so we
  2157. * cannot race with other handlers or page migration.
  2158. * Keep the pte_same checks anyway to make transition from the mutex easier.
  2159. */
  2160. static int hugetlb_cow(struct mm_struct *mm, struct vm_area_struct *vma,
  2161. unsigned long address, pte_t *ptep, pte_t pte,
  2162. struct page *pagecache_page)
  2163. {
  2164. struct hstate *h = hstate_vma(vma);
  2165. struct page *old_page, *new_page;
  2166. int avoidcopy;
  2167. int outside_reserve = 0;
  2168. unsigned long mmun_start; /* For mmu_notifiers */
  2169. unsigned long mmun_end; /* For mmu_notifiers */
  2170. old_page = pte_page(pte);
  2171. retry_avoidcopy:
  2172. /* If no-one else is actually using this page, avoid the copy
  2173. * and just make the page writable */
  2174. avoidcopy = (page_mapcount(old_page) == 1);
  2175. if (avoidcopy) {
  2176. if (PageAnon(old_page))
  2177. page_move_anon_rmap(old_page, vma, address);
  2178. set_huge_ptep_writable(vma, address, ptep);
  2179. return 0;
  2180. }
  2181. /*
  2182. * If the process that created a MAP_PRIVATE mapping is about to
  2183. * perform a COW due to a shared page count, attempt to satisfy
  2184. * the allocation without using the existing reserves. The pagecache
  2185. * page is used to determine if the reserve at this address was
  2186. * consumed or not. If reserves were used, a partial faulted mapping
  2187. * at the time of fork() could consume its reserves on COW instead
  2188. * of the full address range.
  2189. */
  2190. if (!(vma->vm_flags & VM_MAYSHARE) &&
  2191. is_vma_resv_set(vma, HPAGE_RESV_OWNER) &&
  2192. old_page != pagecache_page)
  2193. outside_reserve = 1;
  2194. page_cache_get(old_page);
  2195. /* Drop page_table_lock as buddy allocator may be called */
  2196. spin_unlock(&mm->page_table_lock);
  2197. new_page = alloc_huge_page(vma, address, outside_reserve);
  2198. if (IS_ERR(new_page)) {
  2199. long err = PTR_ERR(new_page);
  2200. page_cache_release(old_page);
  2201. /*
  2202. * If a process owning a MAP_PRIVATE mapping fails to COW,
  2203. * it is due to references held by a child and an insufficient
  2204. * huge page pool. To guarantee the original mappers
  2205. * reliability, unmap the page from child processes. The child
  2206. * may get SIGKILLed if it later faults.
  2207. */
  2208. if (outside_reserve) {
  2209. BUG_ON(huge_pte_none(pte));
  2210. if (unmap_ref_private(mm, vma, old_page, address)) {
  2211. BUG_ON(huge_pte_none(pte));
  2212. spin_lock(&mm->page_table_lock);
  2213. ptep = huge_pte_offset(mm, address & huge_page_mask(h));
  2214. if (likely(pte_same(huge_ptep_get(ptep), pte)))
  2215. goto retry_avoidcopy;
  2216. /*
  2217. * race occurs while re-acquiring page_table_lock, and
  2218. * our job is done.
  2219. */
  2220. return 0;
  2221. }
  2222. WARN_ON_ONCE(1);
  2223. }
  2224. /* Caller expects lock to be held */
  2225. spin_lock(&mm->page_table_lock);
  2226. if (err == -ENOMEM)
  2227. return VM_FAULT_OOM;
  2228. else
  2229. return VM_FAULT_SIGBUS;
  2230. }
  2231. /*
  2232. * When the original hugepage is shared one, it does not have
  2233. * anon_vma prepared.
  2234. */
  2235. if (unlikely(anon_vma_prepare(vma))) {
  2236. page_cache_release(new_page);
  2237. page_cache_release(old_page);
  2238. /* Caller expects lock to be held */
  2239. spin_lock(&mm->page_table_lock);
  2240. return VM_FAULT_OOM;
  2241. }
  2242. copy_user_huge_page(new_page, old_page, address, vma,
  2243. pages_per_huge_page(h));
  2244. __SetPageUptodate(new_page);
  2245. mmun_start = address & huge_page_mask(h);
  2246. mmun_end = mmun_start + huge_page_size(h);
  2247. mmu_notifier_invalidate_range_start(mm, mmun_start, mmun_end);
  2248. /*
  2249. * Retake the page_table_lock to check for racing updates
  2250. * before the page tables are altered
  2251. */
  2252. spin_lock(&mm->page_table_lock);
  2253. ptep = huge_pte_offset(mm, address & huge_page_mask(h));
  2254. if (likely(pte_same(huge_ptep_get(ptep), pte))) {
  2255. /* Break COW */
  2256. huge_ptep_clear_flush(vma, address, ptep);
  2257. set_huge_pte_at(mm, address, ptep,
  2258. make_huge_pte(vma, new_page, 1));
  2259. page_remove_rmap(old_page);
  2260. hugepage_add_new_anon_rmap(new_page, vma, address);
  2261. /* Make the old page be freed below */
  2262. new_page = old_page;
  2263. }
  2264. spin_unlock(&mm->page_table_lock);
  2265. mmu_notifier_invalidate_range_end(mm, mmun_start, mmun_end);
  2266. /* Caller expects lock to be held */
  2267. spin_lock(&mm->page_table_lock);
  2268. page_cache_release(new_page);
  2269. page_cache_release(old_page);
  2270. return 0;
  2271. }
  2272. /* Return the pagecache page at a given address within a VMA */
  2273. static struct page *hugetlbfs_pagecache_page(struct hstate *h,
  2274. struct vm_area_struct *vma, unsigned long address)
  2275. {
  2276. struct address_space *mapping;
  2277. pgoff_t idx;
  2278. mapping = vma->vm_file->f_mapping;
  2279. idx = vma_hugecache_offset(h, vma, address);
  2280. return find_lock_page(mapping, idx);
  2281. }
  2282. /*
  2283. * Return whether there is a pagecache page to back given address within VMA.
  2284. * Caller follow_hugetlb_page() holds page_table_lock so we cannot lock_page.
  2285. */
  2286. static bool hugetlbfs_pagecache_present(struct hstate *h,
  2287. struct vm_area_struct *vma, unsigned long address)
  2288. {
  2289. struct address_space *mapping;
  2290. pgoff_t idx;
  2291. struct page *page;
  2292. mapping = vma->vm_file->f_mapping;
  2293. idx = vma_hugecache_offset(h, vma, address);
  2294. page = find_get_page(mapping, idx);
  2295. if (page)
  2296. put_page(page);
  2297. return page != NULL;
  2298. }
  2299. static int hugetlb_no_page(struct mm_struct *mm, struct vm_area_struct *vma,
  2300. unsigned long address, pte_t *ptep, unsigned int flags)
  2301. {
  2302. struct hstate *h = hstate_vma(vma);
  2303. int ret = VM_FAULT_SIGBUS;
  2304. int anon_rmap = 0;
  2305. pgoff_t idx;
  2306. unsigned long size;
  2307. struct page *page;
  2308. struct address_space *mapping;
  2309. pte_t new_pte;
  2310. /*
  2311. * Currently, we are forced to kill the process in the event the
  2312. * original mapper has unmapped pages from the child due to a failed
  2313. * COW. Warn that such a situation has occurred as it may not be obvious
  2314. */
  2315. if (is_vma_resv_set(vma, HPAGE_RESV_UNMAPPED)) {
  2316. pr_warning("PID %d killed due to inadequate hugepage pool\n",
  2317. current->pid);
  2318. return ret;
  2319. }
  2320. mapping = vma->vm_file->f_mapping;
  2321. idx = vma_hugecache_offset(h, vma, address);
  2322. /*
  2323. * Use page lock to guard against racing truncation
  2324. * before we get page_table_lock.
  2325. */
  2326. retry:
  2327. page = find_lock_page(mapping, idx);
  2328. if (!page) {
  2329. size = i_size_read(mapping->host) >> huge_page_shift(h);
  2330. if (idx >= size)
  2331. goto out;
  2332. page = alloc_huge_page(vma, address, 0);
  2333. if (IS_ERR(page)) {
  2334. ret = PTR_ERR(page);
  2335. if (ret == -ENOMEM)
  2336. ret = VM_FAULT_OOM;
  2337. else
  2338. ret = VM_FAULT_SIGBUS;
  2339. goto out;
  2340. }
  2341. clear_huge_page(page, address, pages_per_huge_page(h));
  2342. __SetPageUptodate(page);
  2343. if (vma->vm_flags & VM_MAYSHARE) {
  2344. int err;
  2345. struct inode *inode = mapping->host;
  2346. err = add_to_page_cache(page, mapping, idx, GFP_KERNEL);
  2347. if (err) {
  2348. put_page(page);
  2349. if (err == -EEXIST)
  2350. goto retry;
  2351. goto out;
  2352. }
  2353. spin_lock(&inode->i_lock);
  2354. inode->i_blocks += blocks_per_huge_page(h);
  2355. spin_unlock(&inode->i_lock);
  2356. } else {
  2357. lock_page(page);
  2358. if (unlikely(anon_vma_prepare(vma))) {
  2359. ret = VM_FAULT_OOM;
  2360. goto backout_unlocked;
  2361. }
  2362. anon_rmap = 1;
  2363. }
  2364. } else {
  2365. /*
  2366. * If memory error occurs between mmap() and fault, some process
  2367. * don't have hwpoisoned swap entry for errored virtual address.
  2368. * So we need to block hugepage fault by PG_hwpoison bit check.
  2369. */
  2370. if (unlikely(PageHWPoison(page))) {
  2371. ret = VM_FAULT_HWPOISON |
  2372. VM_FAULT_SET_HINDEX(hstate_index(h));
  2373. goto backout_unlocked;
  2374. }
  2375. }
  2376. /*
  2377. * If we are going to COW a private mapping later, we examine the
  2378. * pending reservations for this page now. This will ensure that
  2379. * any allocations necessary to record that reservation occur outside
  2380. * the spinlock.
  2381. */
  2382. if ((flags & FAULT_FLAG_WRITE) && !(vma->vm_flags & VM_SHARED))
  2383. if (vma_needs_reservation(h, vma, address) < 0) {
  2384. ret = VM_FAULT_OOM;
  2385. goto backout_unlocked;
  2386. }
  2387. spin_lock(&mm->page_table_lock);
  2388. size = i_size_read(mapping->host) >> huge_page_shift(h);
  2389. if (idx >= size)
  2390. goto backout;
  2391. ret = 0;
  2392. if (!huge_pte_none(huge_ptep_get(ptep)))
  2393. goto backout;
  2394. if (anon_rmap)
  2395. hugepage_add_new_anon_rmap(page, vma, address);
  2396. else
  2397. page_dup_rmap(page);
  2398. new_pte = make_huge_pte(vma, page, ((vma->vm_flags & VM_WRITE)
  2399. && (vma->vm_flags & VM_SHARED)));
  2400. set_huge_pte_at(mm, address, ptep, new_pte);
  2401. if ((flags & FAULT_FLAG_WRITE) && !(vma->vm_flags & VM_SHARED)) {
  2402. /* Optimization, do the COW without a second fault */
  2403. ret = hugetlb_cow(mm, vma, address, ptep, new_pte, page);
  2404. }
  2405. spin_unlock(&mm->page_table_lock);
  2406. unlock_page(page);
  2407. out:
  2408. return ret;
  2409. backout:
  2410. spin_unlock(&mm->page_table_lock);
  2411. backout_unlocked:
  2412. unlock_page(page);
  2413. put_page(page);
  2414. goto out;
  2415. }
  2416. int hugetlb_fault(struct mm_struct *mm, struct vm_area_struct *vma,
  2417. unsigned long address, unsigned int flags)
  2418. {
  2419. pte_t *ptep;
  2420. pte_t entry;
  2421. int ret;
  2422. struct page *page = NULL;
  2423. struct page *pagecache_page = NULL;
  2424. static DEFINE_MUTEX(hugetlb_instantiation_mutex);
  2425. struct hstate *h = hstate_vma(vma);
  2426. address &= huge_page_mask(h);
  2427. ptep = huge_pte_offset(mm, address);
  2428. if (ptep) {
  2429. entry = huge_ptep_get(ptep);
  2430. if (unlikely(is_hugetlb_entry_migration(entry))) {
  2431. migration_entry_wait(mm, (pmd_t *)ptep, address);
  2432. return 0;
  2433. } else if (unlikely(is_hugetlb_entry_hwpoisoned(entry)))
  2434. return VM_FAULT_HWPOISON_LARGE |
  2435. VM_FAULT_SET_HINDEX(hstate_index(h));
  2436. }
  2437. ptep = huge_pte_alloc(mm, address, huge_page_size(h));
  2438. if (!ptep)
  2439. return VM_FAULT_OOM;
  2440. /*
  2441. * Serialize hugepage allocation and instantiation, so that we don't
  2442. * get spurious allocation failures if two CPUs race to instantiate
  2443. * the same page in the page cache.
  2444. */
  2445. mutex_lock(&hugetlb_instantiation_mutex);
  2446. entry = huge_ptep_get(ptep);
  2447. if (huge_pte_none(entry)) {
  2448. ret = hugetlb_no_page(mm, vma, address, ptep, flags);
  2449. goto out_mutex;
  2450. }
  2451. ret = 0;
  2452. /*
  2453. * If we are going to COW the mapping later, we examine the pending
  2454. * reservations for this page now. This will ensure that any
  2455. * allocations necessary to record that reservation occur outside the
  2456. * spinlock. For private mappings, we also lookup the pagecache
  2457. * page now as it is used to determine if a reservation has been
  2458. * consumed.
  2459. */
  2460. if ((flags & FAULT_FLAG_WRITE) && !pte_write(entry)) {
  2461. if (vma_needs_reservation(h, vma, address) < 0) {
  2462. ret = VM_FAULT_OOM;
  2463. goto out_mutex;
  2464. }
  2465. if (!(vma->vm_flags & VM_MAYSHARE))
  2466. pagecache_page = hugetlbfs_pagecache_page(h,
  2467. vma, address);
  2468. }
  2469. /*
  2470. * hugetlb_cow() requires page locks of pte_page(entry) and
  2471. * pagecache_page, so here we need take the former one
  2472. * when page != pagecache_page or !pagecache_page.
  2473. * Note that locking order is always pagecache_page -> page,
  2474. * so no worry about deadlock.
  2475. */
  2476. page = pte_page(entry);
  2477. get_page(page);
  2478. if (page != pagecache_page)
  2479. lock_page(page);
  2480. spin_lock(&mm->page_table_lock);
  2481. /* Check for a racing update before calling hugetlb_cow */
  2482. if (unlikely(!pte_same(entry, huge_ptep_get(ptep))))
  2483. goto out_page_table_lock;
  2484. if (flags & FAULT_FLAG_WRITE) {
  2485. if (!pte_write(entry)) {
  2486. ret = hugetlb_cow(mm, vma, address, ptep, entry,
  2487. pagecache_page);
  2488. goto out_page_table_lock;
  2489. }
  2490. entry = pte_mkdirty(entry);
  2491. }
  2492. entry = pte_mkyoung(entry);
  2493. if (huge_ptep_set_access_flags(vma, address, ptep, entry,
  2494. flags & FAULT_FLAG_WRITE))
  2495. update_mmu_cache(vma, address, ptep);
  2496. out_page_table_lock:
  2497. spin_unlock(&mm->page_table_lock);
  2498. if (pagecache_page) {
  2499. unlock_page(pagecache_page);
  2500. put_page(pagecache_page);
  2501. }
  2502. if (page != pagecache_page)
  2503. unlock_page(page);
  2504. put_page(page);
  2505. out_mutex:
  2506. mutex_unlock(&hugetlb_instantiation_mutex);
  2507. return ret;
  2508. }
  2509. /* Can be overriden by architectures */
  2510. __attribute__((weak)) struct page *
  2511. follow_huge_pud(struct mm_struct *mm, unsigned long address,
  2512. pud_t *pud, int write)
  2513. {
  2514. BUG();
  2515. return NULL;
  2516. }
  2517. long follow_hugetlb_page(struct mm_struct *mm, struct vm_area_struct *vma,
  2518. struct page **pages, struct vm_area_struct **vmas,
  2519. unsigned long *position, unsigned long *nr_pages,
  2520. long i, unsigned int flags)
  2521. {
  2522. unsigned long pfn_offset;
  2523. unsigned long vaddr = *position;
  2524. unsigned long remainder = *nr_pages;
  2525. struct hstate *h = hstate_vma(vma);
  2526. spin_lock(&mm->page_table_lock);
  2527. while (vaddr < vma->vm_end && remainder) {
  2528. pte_t *pte;
  2529. int absent;
  2530. struct page *page;
  2531. /*
  2532. * Some archs (sparc64, sh*) have multiple pte_ts to
  2533. * each hugepage. We have to make sure we get the
  2534. * first, for the page indexing below to work.
  2535. */
  2536. pte = huge_pte_offset(mm, vaddr & huge_page_mask(h));
  2537. absent = !pte || huge_pte_none(huge_ptep_get(pte));
  2538. /*
  2539. * When coredumping, it suits get_dump_page if we just return
  2540. * an error where there's an empty slot with no huge pagecache
  2541. * to back it. This way, we avoid allocating a hugepage, and
  2542. * the sparse dumpfile avoids allocating disk blocks, but its
  2543. * huge holes still show up with zeroes where they need to be.
  2544. */
  2545. if (absent && (flags & FOLL_DUMP) &&
  2546. !hugetlbfs_pagecache_present(h, vma, vaddr)) {
  2547. remainder = 0;
  2548. break;
  2549. }
  2550. if (absent ||
  2551. ((flags & FOLL_WRITE) && !pte_write(huge_ptep_get(pte)))) {
  2552. int ret;
  2553. spin_unlock(&mm->page_table_lock);
  2554. ret = hugetlb_fault(mm, vma, vaddr,
  2555. (flags & FOLL_WRITE) ? FAULT_FLAG_WRITE : 0);
  2556. spin_lock(&mm->page_table_lock);
  2557. if (!(ret & VM_FAULT_ERROR))
  2558. continue;
  2559. remainder = 0;
  2560. break;
  2561. }
  2562. pfn_offset = (vaddr & ~huge_page_mask(h)) >> PAGE_SHIFT;
  2563. page = pte_page(huge_ptep_get(pte));
  2564. same_page:
  2565. if (pages) {
  2566. pages[i] = mem_map_offset(page, pfn_offset);
  2567. get_page(pages[i]);
  2568. }
  2569. if (vmas)
  2570. vmas[i] = vma;
  2571. vaddr += PAGE_SIZE;
  2572. ++pfn_offset;
  2573. --remainder;
  2574. ++i;
  2575. if (vaddr < vma->vm_end && remainder &&
  2576. pfn_offset < pages_per_huge_page(h)) {
  2577. /*
  2578. * We use pfn_offset to avoid touching the pageframes
  2579. * of this compound page.
  2580. */
  2581. goto same_page;
  2582. }
  2583. }
  2584. spin_unlock(&mm->page_table_lock);
  2585. *nr_pages = remainder;
  2586. *position = vaddr;
  2587. return i ? i : -EFAULT;
  2588. }
  2589. unsigned long hugetlb_change_protection(struct vm_area_struct *vma,
  2590. unsigned long address, unsigned long end, pgprot_t newprot)
  2591. {
  2592. struct mm_struct *mm = vma->vm_mm;
  2593. unsigned long start = address;
  2594. pte_t *ptep;
  2595. pte_t pte;
  2596. struct hstate *h = hstate_vma(vma);
  2597. unsigned long pages = 0;
  2598. BUG_ON(address >= end);
  2599. flush_cache_range(vma, address, end);
  2600. mutex_lock(&vma->vm_file->f_mapping->i_mmap_mutex);
  2601. spin_lock(&mm->page_table_lock);
  2602. for (; address < end; address += huge_page_size(h)) {
  2603. ptep = huge_pte_offset(mm, address);
  2604. if (!ptep)
  2605. continue;
  2606. if (huge_pmd_unshare(mm, &address, ptep)) {
  2607. pages++;
  2608. continue;
  2609. }
  2610. if (!huge_pte_none(huge_ptep_get(ptep))) {
  2611. pte = huge_ptep_get_and_clear(mm, address, ptep);
  2612. pte = pte_mkhuge(pte_modify(pte, newprot));
  2613. pte = arch_make_huge_pte(pte, vma, NULL, 0);
  2614. set_huge_pte_at(mm, address, ptep, pte);
  2615. pages++;
  2616. }
  2617. }
  2618. spin_unlock(&mm->page_table_lock);
  2619. /*
  2620. * Must flush TLB before releasing i_mmap_mutex: x86's huge_pmd_unshare
  2621. * may have cleared our pud entry and done put_page on the page table:
  2622. * once we release i_mmap_mutex, another task can do the final put_page
  2623. * and that page table be reused and filled with junk.
  2624. */
  2625. flush_tlb_range(vma, start, end);
  2626. mutex_unlock(&vma->vm_file->f_mapping->i_mmap_mutex);
  2627. return pages << h->order;
  2628. }
  2629. int hugetlb_reserve_pages(struct inode *inode,
  2630. long from, long to,
  2631. struct vm_area_struct *vma,
  2632. vm_flags_t vm_flags)
  2633. {
  2634. long ret, chg;
  2635. struct hstate *h = hstate_inode(inode);
  2636. struct hugepage_subpool *spool = subpool_inode(inode);
  2637. /*
  2638. * Only apply hugepage reservation if asked. At fault time, an
  2639. * attempt will be made for VM_NORESERVE to allocate a page
  2640. * without using reserves
  2641. */
  2642. if (vm_flags & VM_NORESERVE)
  2643. return 0;
  2644. /*
  2645. * Shared mappings base their reservation on the number of pages that
  2646. * are already allocated on behalf of the file. Private mappings need
  2647. * to reserve the full area even if read-only as mprotect() may be
  2648. * called to make the mapping read-write. Assume !vma is a shm mapping
  2649. */
  2650. if (!vma || vma->vm_flags & VM_MAYSHARE)
  2651. chg = region_chg(&inode->i_mapping->private_list, from, to);
  2652. else {
  2653. struct resv_map *resv_map = resv_map_alloc();
  2654. if (!resv_map)
  2655. return -ENOMEM;
  2656. chg = to - from;
  2657. set_vma_resv_map(vma, resv_map);
  2658. set_vma_resv_flags(vma, HPAGE_RESV_OWNER);
  2659. }
  2660. if (chg < 0) {
  2661. ret = chg;
  2662. goto out_err;
  2663. }
  2664. /* There must be enough pages in the subpool for the mapping */
  2665. if (hugepage_subpool_get_pages(spool, chg)) {
  2666. ret = -ENOSPC;
  2667. goto out_err;
  2668. }
  2669. /*
  2670. * Check enough hugepages are available for the reservation.
  2671. * Hand the pages back to the subpool if there are not
  2672. */
  2673. ret = hugetlb_acct_memory(h, chg);
  2674. if (ret < 0) {
  2675. hugepage_subpool_put_pages(spool, chg);
  2676. goto out_err;
  2677. }
  2678. /*
  2679. * Account for the reservations made. Shared mappings record regions
  2680. * that have reservations as they are shared by multiple VMAs.
  2681. * When the last VMA disappears, the region map says how much
  2682. * the reservation was and the page cache tells how much of
  2683. * the reservation was consumed. Private mappings are per-VMA and
  2684. * only the consumed reservations are tracked. When the VMA
  2685. * disappears, the original reservation is the VMA size and the
  2686. * consumed reservations are stored in the map. Hence, nothing
  2687. * else has to be done for private mappings here
  2688. */
  2689. if (!vma || vma->vm_flags & VM_MAYSHARE)
  2690. region_add(&inode->i_mapping->private_list, from, to);
  2691. return 0;
  2692. out_err:
  2693. if (vma)
  2694. resv_map_put(vma);
  2695. return ret;
  2696. }
  2697. void hugetlb_unreserve_pages(struct inode *inode, long offset, long freed)
  2698. {
  2699. struct hstate *h = hstate_inode(inode);
  2700. long chg = region_truncate(&inode->i_mapping->private_list, offset);
  2701. struct hugepage_subpool *spool = subpool_inode(inode);
  2702. spin_lock(&inode->i_lock);
  2703. inode->i_blocks -= (blocks_per_huge_page(h) * freed);
  2704. spin_unlock(&inode->i_lock);
  2705. hugepage_subpool_put_pages(spool, (chg - freed));
  2706. hugetlb_acct_memory(h, -(chg - freed));
  2707. }
  2708. #ifdef CONFIG_MEMORY_FAILURE
  2709. /* Should be called in hugetlb_lock */
  2710. static int is_hugepage_on_freelist(struct page *hpage)
  2711. {
  2712. struct page *page;
  2713. struct page *tmp;
  2714. struct hstate *h = page_hstate(hpage);
  2715. int nid = page_to_nid(hpage);
  2716. list_for_each_entry_safe(page, tmp, &h->hugepage_freelists[nid], lru)
  2717. if (page == hpage)
  2718. return 1;
  2719. return 0;
  2720. }
  2721. /*
  2722. * This function is called from memory failure code.
  2723. * Assume the caller holds page lock of the head page.
  2724. */
  2725. int dequeue_hwpoisoned_huge_page(struct page *hpage)
  2726. {
  2727. struct hstate *h = page_hstate(hpage);
  2728. int nid = page_to_nid(hpage);
  2729. int ret = -EBUSY;
  2730. spin_lock(&hugetlb_lock);
  2731. if (is_hugepage_on_freelist(hpage)) {
  2732. /*
  2733. * Hwpoisoned hugepage isn't linked to activelist or freelist,
  2734. * but dangling hpage->lru can trigger list-debug warnings
  2735. * (this happens when we call unpoison_memory() on it),
  2736. * so let it point to itself with list_del_init().
  2737. */
  2738. list_del_init(&hpage->lru);
  2739. set_page_refcounted(hpage);
  2740. h->free_huge_pages--;
  2741. h->free_huge_pages_node[nid]--;
  2742. ret = 0;
  2743. }
  2744. spin_unlock(&hugetlb_lock);
  2745. return ret;
  2746. }
  2747. #endif