hugetlb.c 73 KB

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