hugetlb.c 76 KB

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