page_alloc.c 83 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121
  1. /*
  2. * linux/mm/page_alloc.c
  3. *
  4. * Manages the free list, the system allocates free pages here.
  5. * Note that kmalloc() lives in slab.c
  6. *
  7. * Copyright (C) 1991, 1992, 1993, 1994 Linus Torvalds
  8. * Swap reorganised 29.12.95, Stephen Tweedie
  9. * Support of BIGMEM added by Gerhard Wichert, Siemens AG, July 1999
  10. * Reshaped it to be a zoned allocator, Ingo Molnar, Red Hat, 1999
  11. * Discontiguous memory support, Kanoj Sarcar, SGI, Nov 1999
  12. * Zone balancing, Kanoj Sarcar, SGI, Jan 2000
  13. * Per cpu hot/cold page lists, bulk allocation, Martin J. Bligh, Sept 2002
  14. * (lots of bits borrowed from Ingo Molnar & Andrew Morton)
  15. */
  16. #include <linux/stddef.h>
  17. #include <linux/mm.h>
  18. #include <linux/swap.h>
  19. #include <linux/interrupt.h>
  20. #include <linux/pagemap.h>
  21. #include <linux/bootmem.h>
  22. #include <linux/compiler.h>
  23. #include <linux/kernel.h>
  24. #include <linux/module.h>
  25. #include <linux/suspend.h>
  26. #include <linux/pagevec.h>
  27. #include <linux/blkdev.h>
  28. #include <linux/slab.h>
  29. #include <linux/notifier.h>
  30. #include <linux/topology.h>
  31. #include <linux/sysctl.h>
  32. #include <linux/cpu.h>
  33. #include <linux/cpuset.h>
  34. #include <linux/memory_hotplug.h>
  35. #include <linux/nodemask.h>
  36. #include <linux/vmalloc.h>
  37. #include <linux/mempolicy.h>
  38. #include <linux/stop_machine.h>
  39. #include <linux/sort.h>
  40. #include <linux/pfn.h>
  41. #include <asm/tlbflush.h>
  42. #include <asm/div64.h>
  43. #include "internal.h"
  44. /*
  45. * MCD - HACK: Find somewhere to initialize this EARLY, or make this
  46. * initializer cleaner
  47. */
  48. nodemask_t node_online_map __read_mostly = { { [0] = 1UL } };
  49. EXPORT_SYMBOL(node_online_map);
  50. nodemask_t node_possible_map __read_mostly = NODE_MASK_ALL;
  51. EXPORT_SYMBOL(node_possible_map);
  52. unsigned long totalram_pages __read_mostly;
  53. unsigned long totalreserve_pages __read_mostly;
  54. long nr_swap_pages;
  55. int percpu_pagelist_fraction;
  56. static void __free_pages_ok(struct page *page, unsigned int order);
  57. /*
  58. * results with 256, 32 in the lowmem_reserve sysctl:
  59. * 1G machine -> (16M dma, 800M-16M normal, 1G-800M high)
  60. * 1G machine -> (16M dma, 784M normal, 224M high)
  61. * NORMAL allocation will leave 784M/256 of ram reserved in the ZONE_DMA
  62. * HIGHMEM allocation will leave 224M/32 of ram reserved in ZONE_NORMAL
  63. * HIGHMEM allocation will (224M+784M)/256 of ram reserved in ZONE_DMA
  64. *
  65. * TBD: should special case ZONE_DMA32 machines here - in those we normally
  66. * don't need any ZONE_NORMAL reservation
  67. */
  68. int sysctl_lowmem_reserve_ratio[MAX_NR_ZONES-1] = {
  69. 256,
  70. #ifdef CONFIG_ZONE_DMA32
  71. 256,
  72. #endif
  73. #ifdef CONFIG_HIGHMEM
  74. 32
  75. #endif
  76. };
  77. EXPORT_SYMBOL(totalram_pages);
  78. /*
  79. * Used by page_zone() to look up the address of the struct zone whose
  80. * id is encoded in the upper bits of page->flags
  81. */
  82. struct zone *zone_table[1 << ZONETABLE_SHIFT] __read_mostly;
  83. EXPORT_SYMBOL(zone_table);
  84. static char *zone_names[MAX_NR_ZONES] = {
  85. "DMA",
  86. #ifdef CONFIG_ZONE_DMA32
  87. "DMA32",
  88. #endif
  89. "Normal",
  90. #ifdef CONFIG_HIGHMEM
  91. "HighMem"
  92. #endif
  93. };
  94. int min_free_kbytes = 1024;
  95. unsigned long __meminitdata nr_kernel_pages;
  96. unsigned long __meminitdata nr_all_pages;
  97. static unsigned long __initdata dma_reserve;
  98. #ifdef CONFIG_ARCH_POPULATES_NODE_MAP
  99. /*
  100. * MAX_ACTIVE_REGIONS determines the maxmimum number of distinct
  101. * ranges of memory (RAM) that may be registered with add_active_range().
  102. * Ranges passed to add_active_range() will be merged if possible
  103. * so the number of times add_active_range() can be called is
  104. * related to the number of nodes and the number of holes
  105. */
  106. #ifdef CONFIG_MAX_ACTIVE_REGIONS
  107. /* Allow an architecture to set MAX_ACTIVE_REGIONS to save memory */
  108. #define MAX_ACTIVE_REGIONS CONFIG_MAX_ACTIVE_REGIONS
  109. #else
  110. #if MAX_NUMNODES >= 32
  111. /* If there can be many nodes, allow up to 50 holes per node */
  112. #define MAX_ACTIVE_REGIONS (MAX_NUMNODES*50)
  113. #else
  114. /* By default, allow up to 256 distinct regions */
  115. #define MAX_ACTIVE_REGIONS 256
  116. #endif
  117. #endif
  118. struct node_active_region __initdata early_node_map[MAX_ACTIVE_REGIONS];
  119. int __initdata nr_nodemap_entries;
  120. unsigned long __initdata arch_zone_lowest_possible_pfn[MAX_NR_ZONES];
  121. unsigned long __initdata arch_zone_highest_possible_pfn[MAX_NR_ZONES];
  122. #ifdef CONFIG_MEMORY_HOTPLUG_RESERVE
  123. unsigned long __initdata node_boundary_start_pfn[MAX_NUMNODES];
  124. unsigned long __initdata node_boundary_end_pfn[MAX_NUMNODES];
  125. #endif /* CONFIG_MEMORY_HOTPLUG_RESERVE */
  126. #endif /* CONFIG_ARCH_POPULATES_NODE_MAP */
  127. #ifdef CONFIG_DEBUG_VM
  128. static int page_outside_zone_boundaries(struct zone *zone, struct page *page)
  129. {
  130. int ret = 0;
  131. unsigned seq;
  132. unsigned long pfn = page_to_pfn(page);
  133. do {
  134. seq = zone_span_seqbegin(zone);
  135. if (pfn >= zone->zone_start_pfn + zone->spanned_pages)
  136. ret = 1;
  137. else if (pfn < zone->zone_start_pfn)
  138. ret = 1;
  139. } while (zone_span_seqretry(zone, seq));
  140. return ret;
  141. }
  142. static int page_is_consistent(struct zone *zone, struct page *page)
  143. {
  144. #ifdef CONFIG_HOLES_IN_ZONE
  145. if (!pfn_valid(page_to_pfn(page)))
  146. return 0;
  147. #endif
  148. if (zone != page_zone(page))
  149. return 0;
  150. return 1;
  151. }
  152. /*
  153. * Temporary debugging check for pages not lying within a given zone.
  154. */
  155. static int bad_range(struct zone *zone, struct page *page)
  156. {
  157. if (page_outside_zone_boundaries(zone, page))
  158. return 1;
  159. if (!page_is_consistent(zone, page))
  160. return 1;
  161. return 0;
  162. }
  163. #else
  164. static inline int bad_range(struct zone *zone, struct page *page)
  165. {
  166. return 0;
  167. }
  168. #endif
  169. static void bad_page(struct page *page)
  170. {
  171. printk(KERN_EMERG "Bad page state in process '%s'\n"
  172. KERN_EMERG "page:%p flags:0x%0*lx mapping:%p mapcount:%d count:%d\n"
  173. KERN_EMERG "Trying to fix it up, but a reboot is needed\n"
  174. KERN_EMERG "Backtrace:\n",
  175. current->comm, page, (int)(2*sizeof(unsigned long)),
  176. (unsigned long)page->flags, page->mapping,
  177. page_mapcount(page), page_count(page));
  178. dump_stack();
  179. page->flags &= ~(1 << PG_lru |
  180. 1 << PG_private |
  181. 1 << PG_locked |
  182. 1 << PG_active |
  183. 1 << PG_dirty |
  184. 1 << PG_reclaim |
  185. 1 << PG_slab |
  186. 1 << PG_swapcache |
  187. 1 << PG_writeback |
  188. 1 << PG_buddy );
  189. set_page_count(page, 0);
  190. reset_page_mapcount(page);
  191. page->mapping = NULL;
  192. add_taint(TAINT_BAD_PAGE);
  193. }
  194. /*
  195. * Higher-order pages are called "compound pages". They are structured thusly:
  196. *
  197. * The first PAGE_SIZE page is called the "head page".
  198. *
  199. * The remaining PAGE_SIZE pages are called "tail pages".
  200. *
  201. * All pages have PG_compound set. All pages have their ->private pointing at
  202. * the head page (even the head page has this).
  203. *
  204. * The first tail page's ->lru.next holds the address of the compound page's
  205. * put_page() function. Its ->lru.prev holds the order of allocation.
  206. * This usage means that zero-order pages may not be compound.
  207. */
  208. static void free_compound_page(struct page *page)
  209. {
  210. __free_pages_ok(page, (unsigned long)page[1].lru.prev);
  211. }
  212. static void prep_compound_page(struct page *page, unsigned long order)
  213. {
  214. int i;
  215. int nr_pages = 1 << order;
  216. page[1].lru.next = (void *)free_compound_page; /* set dtor */
  217. page[1].lru.prev = (void *)order;
  218. for (i = 0; i < nr_pages; i++) {
  219. struct page *p = page + i;
  220. __SetPageCompound(p);
  221. set_page_private(p, (unsigned long)page);
  222. }
  223. }
  224. static void destroy_compound_page(struct page *page, unsigned long order)
  225. {
  226. int i;
  227. int nr_pages = 1 << order;
  228. if (unlikely((unsigned long)page[1].lru.prev != order))
  229. bad_page(page);
  230. for (i = 0; i < nr_pages; i++) {
  231. struct page *p = page + i;
  232. if (unlikely(!PageCompound(p) |
  233. (page_private(p) != (unsigned long)page)))
  234. bad_page(page);
  235. __ClearPageCompound(p);
  236. }
  237. }
  238. static inline void prep_zero_page(struct page *page, int order, gfp_t gfp_flags)
  239. {
  240. int i;
  241. VM_BUG_ON((gfp_flags & (__GFP_WAIT | __GFP_HIGHMEM)) == __GFP_HIGHMEM);
  242. /*
  243. * clear_highpage() will use KM_USER0, so it's a bug to use __GFP_ZERO
  244. * and __GFP_HIGHMEM from hard or soft interrupt context.
  245. */
  246. VM_BUG_ON((gfp_flags & __GFP_HIGHMEM) && in_interrupt());
  247. for (i = 0; i < (1 << order); i++)
  248. clear_highpage(page + i);
  249. }
  250. /*
  251. * function for dealing with page's order in buddy system.
  252. * zone->lock is already acquired when we use these.
  253. * So, we don't need atomic page->flags operations here.
  254. */
  255. static inline unsigned long page_order(struct page *page)
  256. {
  257. return page_private(page);
  258. }
  259. static inline void set_page_order(struct page *page, int order)
  260. {
  261. set_page_private(page, order);
  262. __SetPageBuddy(page);
  263. }
  264. static inline void rmv_page_order(struct page *page)
  265. {
  266. __ClearPageBuddy(page);
  267. set_page_private(page, 0);
  268. }
  269. /*
  270. * Locate the struct page for both the matching buddy in our
  271. * pair (buddy1) and the combined O(n+1) page they form (page).
  272. *
  273. * 1) Any buddy B1 will have an order O twin B2 which satisfies
  274. * the following equation:
  275. * B2 = B1 ^ (1 << O)
  276. * For example, if the starting buddy (buddy2) is #8 its order
  277. * 1 buddy is #10:
  278. * B2 = 8 ^ (1 << 1) = 8 ^ 2 = 10
  279. *
  280. * 2) Any buddy B will have an order O+1 parent P which
  281. * satisfies the following equation:
  282. * P = B & ~(1 << O)
  283. *
  284. * Assumption: *_mem_map is contiguous at least up to MAX_ORDER
  285. */
  286. static inline struct page *
  287. __page_find_buddy(struct page *page, unsigned long page_idx, unsigned int order)
  288. {
  289. unsigned long buddy_idx = page_idx ^ (1 << order);
  290. return page + (buddy_idx - page_idx);
  291. }
  292. static inline unsigned long
  293. __find_combined_index(unsigned long page_idx, unsigned int order)
  294. {
  295. return (page_idx & ~(1 << order));
  296. }
  297. /*
  298. * This function checks whether a page is free && is the buddy
  299. * we can do coalesce a page and its buddy if
  300. * (a) the buddy is not in a hole &&
  301. * (b) the buddy is in the buddy system &&
  302. * (c) a page and its buddy have the same order &&
  303. * (d) a page and its buddy are in the same zone.
  304. *
  305. * For recording whether a page is in the buddy system, we use PG_buddy.
  306. * Setting, clearing, and testing PG_buddy is serialized by zone->lock.
  307. *
  308. * For recording page's order, we use page_private(page).
  309. */
  310. static inline int page_is_buddy(struct page *page, struct page *buddy,
  311. int order)
  312. {
  313. #ifdef CONFIG_HOLES_IN_ZONE
  314. if (!pfn_valid(page_to_pfn(buddy)))
  315. return 0;
  316. #endif
  317. if (page_zone_id(page) != page_zone_id(buddy))
  318. return 0;
  319. if (PageBuddy(buddy) && page_order(buddy) == order) {
  320. BUG_ON(page_count(buddy) != 0);
  321. return 1;
  322. }
  323. return 0;
  324. }
  325. /*
  326. * Freeing function for a buddy system allocator.
  327. *
  328. * The concept of a buddy system is to maintain direct-mapped table
  329. * (containing bit values) for memory blocks of various "orders".
  330. * The bottom level table contains the map for the smallest allocatable
  331. * units of memory (here, pages), and each level above it describes
  332. * pairs of units from the levels below, hence, "buddies".
  333. * At a high level, all that happens here is marking the table entry
  334. * at the bottom level available, and propagating the changes upward
  335. * as necessary, plus some accounting needed to play nicely with other
  336. * parts of the VM system.
  337. * At each level, we keep a list of pages, which are heads of continuous
  338. * free pages of length of (1 << order) and marked with PG_buddy. Page's
  339. * order is recorded in page_private(page) field.
  340. * So when we are allocating or freeing one, we can derive the state of the
  341. * other. That is, if we allocate a small block, and both were
  342. * free, the remainder of the region must be split into blocks.
  343. * If a block is freed, and its buddy is also free, then this
  344. * triggers coalescing into a block of larger size.
  345. *
  346. * -- wli
  347. */
  348. static inline void __free_one_page(struct page *page,
  349. struct zone *zone, unsigned int order)
  350. {
  351. unsigned long page_idx;
  352. int order_size = 1 << order;
  353. if (unlikely(PageCompound(page)))
  354. destroy_compound_page(page, order);
  355. page_idx = page_to_pfn(page) & ((1 << MAX_ORDER) - 1);
  356. VM_BUG_ON(page_idx & (order_size - 1));
  357. VM_BUG_ON(bad_range(zone, page));
  358. zone->free_pages += order_size;
  359. while (order < MAX_ORDER-1) {
  360. unsigned long combined_idx;
  361. struct free_area *area;
  362. struct page *buddy;
  363. buddy = __page_find_buddy(page, page_idx, order);
  364. if (!page_is_buddy(page, buddy, order))
  365. break; /* Move the buddy up one level. */
  366. list_del(&buddy->lru);
  367. area = zone->free_area + order;
  368. area->nr_free--;
  369. rmv_page_order(buddy);
  370. combined_idx = __find_combined_index(page_idx, order);
  371. page = page + (combined_idx - page_idx);
  372. page_idx = combined_idx;
  373. order++;
  374. }
  375. set_page_order(page, order);
  376. list_add(&page->lru, &zone->free_area[order].free_list);
  377. zone->free_area[order].nr_free++;
  378. }
  379. static inline int free_pages_check(struct page *page)
  380. {
  381. if (unlikely(page_mapcount(page) |
  382. (page->mapping != NULL) |
  383. (page_count(page) != 0) |
  384. (page->flags & (
  385. 1 << PG_lru |
  386. 1 << PG_private |
  387. 1 << PG_locked |
  388. 1 << PG_active |
  389. 1 << PG_reclaim |
  390. 1 << PG_slab |
  391. 1 << PG_swapcache |
  392. 1 << PG_writeback |
  393. 1 << PG_reserved |
  394. 1 << PG_buddy ))))
  395. bad_page(page);
  396. if (PageDirty(page))
  397. __ClearPageDirty(page);
  398. /*
  399. * For now, we report if PG_reserved was found set, but do not
  400. * clear it, and do not free the page. But we shall soon need
  401. * to do more, for when the ZERO_PAGE count wraps negative.
  402. */
  403. return PageReserved(page);
  404. }
  405. /*
  406. * Frees a list of pages.
  407. * Assumes all pages on list are in same zone, and of same order.
  408. * count is the number of pages to free.
  409. *
  410. * If the zone was previously in an "all pages pinned" state then look to
  411. * see if this freeing clears that state.
  412. *
  413. * And clear the zone's pages_scanned counter, to hold off the "all pages are
  414. * pinned" detection logic.
  415. */
  416. static void free_pages_bulk(struct zone *zone, int count,
  417. struct list_head *list, int order)
  418. {
  419. spin_lock(&zone->lock);
  420. zone->all_unreclaimable = 0;
  421. zone->pages_scanned = 0;
  422. while (count--) {
  423. struct page *page;
  424. VM_BUG_ON(list_empty(list));
  425. page = list_entry(list->prev, struct page, lru);
  426. /* have to delete it as __free_one_page list manipulates */
  427. list_del(&page->lru);
  428. __free_one_page(page, zone, order);
  429. }
  430. spin_unlock(&zone->lock);
  431. }
  432. static void free_one_page(struct zone *zone, struct page *page, int order)
  433. {
  434. spin_lock(&zone->lock);
  435. zone->all_unreclaimable = 0;
  436. zone->pages_scanned = 0;
  437. __free_one_page(page, zone ,order);
  438. spin_unlock(&zone->lock);
  439. }
  440. static void __free_pages_ok(struct page *page, unsigned int order)
  441. {
  442. unsigned long flags;
  443. int i;
  444. int reserved = 0;
  445. for (i = 0 ; i < (1 << order) ; ++i)
  446. reserved += free_pages_check(page + i);
  447. if (reserved)
  448. return;
  449. if (!PageHighMem(page))
  450. debug_check_no_locks_freed(page_address(page),PAGE_SIZE<<order);
  451. arch_free_page(page, order);
  452. kernel_map_pages(page, 1 << order, 0);
  453. local_irq_save(flags);
  454. __count_vm_events(PGFREE, 1 << order);
  455. free_one_page(page_zone(page), page, order);
  456. local_irq_restore(flags);
  457. }
  458. /*
  459. * permit the bootmem allocator to evade page validation on high-order frees
  460. */
  461. void fastcall __init __free_pages_bootmem(struct page *page, unsigned int order)
  462. {
  463. if (order == 0) {
  464. __ClearPageReserved(page);
  465. set_page_count(page, 0);
  466. set_page_refcounted(page);
  467. __free_page(page);
  468. } else {
  469. int loop;
  470. prefetchw(page);
  471. for (loop = 0; loop < BITS_PER_LONG; loop++) {
  472. struct page *p = &page[loop];
  473. if (loop + 1 < BITS_PER_LONG)
  474. prefetchw(p + 1);
  475. __ClearPageReserved(p);
  476. set_page_count(p, 0);
  477. }
  478. set_page_refcounted(page);
  479. __free_pages(page, order);
  480. }
  481. }
  482. /*
  483. * The order of subdivision here is critical for the IO subsystem.
  484. * Please do not alter this order without good reasons and regression
  485. * testing. Specifically, as large blocks of memory are subdivided,
  486. * the order in which smaller blocks are delivered depends on the order
  487. * they're subdivided in this function. This is the primary factor
  488. * influencing the order in which pages are delivered to the IO
  489. * subsystem according to empirical testing, and this is also justified
  490. * by considering the behavior of a buddy system containing a single
  491. * large block of memory acted on by a series of small allocations.
  492. * This behavior is a critical factor in sglist merging's success.
  493. *
  494. * -- wli
  495. */
  496. static inline void expand(struct zone *zone, struct page *page,
  497. int low, int high, struct free_area *area)
  498. {
  499. unsigned long size = 1 << high;
  500. while (high > low) {
  501. area--;
  502. high--;
  503. size >>= 1;
  504. VM_BUG_ON(bad_range(zone, &page[size]));
  505. list_add(&page[size].lru, &area->free_list);
  506. area->nr_free++;
  507. set_page_order(&page[size], high);
  508. }
  509. }
  510. /*
  511. * This page is about to be returned from the page allocator
  512. */
  513. static int prep_new_page(struct page *page, int order, gfp_t gfp_flags)
  514. {
  515. if (unlikely(page_mapcount(page) |
  516. (page->mapping != NULL) |
  517. (page_count(page) != 0) |
  518. (page->flags & (
  519. 1 << PG_lru |
  520. 1 << PG_private |
  521. 1 << PG_locked |
  522. 1 << PG_active |
  523. 1 << PG_dirty |
  524. 1 << PG_reclaim |
  525. 1 << PG_slab |
  526. 1 << PG_swapcache |
  527. 1 << PG_writeback |
  528. 1 << PG_reserved |
  529. 1 << PG_buddy ))))
  530. bad_page(page);
  531. /*
  532. * For now, we report if PG_reserved was found set, but do not
  533. * clear it, and do not allocate the page: as a safety net.
  534. */
  535. if (PageReserved(page))
  536. return 1;
  537. page->flags &= ~(1 << PG_uptodate | 1 << PG_error |
  538. 1 << PG_referenced | 1 << PG_arch_1 |
  539. 1 << PG_checked | 1 << PG_mappedtodisk);
  540. set_page_private(page, 0);
  541. set_page_refcounted(page);
  542. kernel_map_pages(page, 1 << order, 1);
  543. if (gfp_flags & __GFP_ZERO)
  544. prep_zero_page(page, order, gfp_flags);
  545. if (order && (gfp_flags & __GFP_COMP))
  546. prep_compound_page(page, order);
  547. return 0;
  548. }
  549. /*
  550. * Do the hard work of removing an element from the buddy allocator.
  551. * Call me with the zone->lock already held.
  552. */
  553. static struct page *__rmqueue(struct zone *zone, unsigned int order)
  554. {
  555. struct free_area * area;
  556. unsigned int current_order;
  557. struct page *page;
  558. for (current_order = order; current_order < MAX_ORDER; ++current_order) {
  559. area = zone->free_area + current_order;
  560. if (list_empty(&area->free_list))
  561. continue;
  562. page = list_entry(area->free_list.next, struct page, lru);
  563. list_del(&page->lru);
  564. rmv_page_order(page);
  565. area->nr_free--;
  566. zone->free_pages -= 1UL << order;
  567. expand(zone, page, order, current_order, area);
  568. return page;
  569. }
  570. return NULL;
  571. }
  572. /*
  573. * Obtain a specified number of elements from the buddy allocator, all under
  574. * a single hold of the lock, for efficiency. Add them to the supplied list.
  575. * Returns the number of new pages which were placed at *list.
  576. */
  577. static int rmqueue_bulk(struct zone *zone, unsigned int order,
  578. unsigned long count, struct list_head *list)
  579. {
  580. int i;
  581. spin_lock(&zone->lock);
  582. for (i = 0; i < count; ++i) {
  583. struct page *page = __rmqueue(zone, order);
  584. if (unlikely(page == NULL))
  585. break;
  586. list_add_tail(&page->lru, list);
  587. }
  588. spin_unlock(&zone->lock);
  589. return i;
  590. }
  591. #ifdef CONFIG_NUMA
  592. /*
  593. * Called from the slab reaper to drain pagesets on a particular node that
  594. * belongs to the currently executing processor.
  595. * Note that this function must be called with the thread pinned to
  596. * a single processor.
  597. */
  598. void drain_node_pages(int nodeid)
  599. {
  600. int i;
  601. enum zone_type z;
  602. unsigned long flags;
  603. for (z = 0; z < MAX_NR_ZONES; z++) {
  604. struct zone *zone = NODE_DATA(nodeid)->node_zones + z;
  605. struct per_cpu_pageset *pset;
  606. if (!populated_zone(zone))
  607. continue;
  608. pset = zone_pcp(zone, smp_processor_id());
  609. for (i = 0; i < ARRAY_SIZE(pset->pcp); i++) {
  610. struct per_cpu_pages *pcp;
  611. pcp = &pset->pcp[i];
  612. if (pcp->count) {
  613. local_irq_save(flags);
  614. free_pages_bulk(zone, pcp->count, &pcp->list, 0);
  615. pcp->count = 0;
  616. local_irq_restore(flags);
  617. }
  618. }
  619. }
  620. }
  621. #endif
  622. #if defined(CONFIG_PM) || defined(CONFIG_HOTPLUG_CPU)
  623. static void __drain_pages(unsigned int cpu)
  624. {
  625. unsigned long flags;
  626. struct zone *zone;
  627. int i;
  628. for_each_zone(zone) {
  629. struct per_cpu_pageset *pset;
  630. pset = zone_pcp(zone, cpu);
  631. for (i = 0; i < ARRAY_SIZE(pset->pcp); i++) {
  632. struct per_cpu_pages *pcp;
  633. pcp = &pset->pcp[i];
  634. local_irq_save(flags);
  635. free_pages_bulk(zone, pcp->count, &pcp->list, 0);
  636. pcp->count = 0;
  637. local_irq_restore(flags);
  638. }
  639. }
  640. }
  641. #endif /* CONFIG_PM || CONFIG_HOTPLUG_CPU */
  642. #ifdef CONFIG_PM
  643. void mark_free_pages(struct zone *zone)
  644. {
  645. unsigned long pfn, max_zone_pfn;
  646. unsigned long flags;
  647. int order;
  648. struct list_head *curr;
  649. if (!zone->spanned_pages)
  650. return;
  651. spin_lock_irqsave(&zone->lock, flags);
  652. max_zone_pfn = zone->zone_start_pfn + zone->spanned_pages;
  653. for (pfn = zone->zone_start_pfn; pfn < max_zone_pfn; pfn++)
  654. if (pfn_valid(pfn)) {
  655. struct page *page = pfn_to_page(pfn);
  656. if (!PageNosave(page))
  657. ClearPageNosaveFree(page);
  658. }
  659. for (order = MAX_ORDER - 1; order >= 0; --order)
  660. list_for_each(curr, &zone->free_area[order].free_list) {
  661. unsigned long i;
  662. pfn = page_to_pfn(list_entry(curr, struct page, lru));
  663. for (i = 0; i < (1UL << order); i++)
  664. SetPageNosaveFree(pfn_to_page(pfn + i));
  665. }
  666. spin_unlock_irqrestore(&zone->lock, flags);
  667. }
  668. /*
  669. * Spill all of this CPU's per-cpu pages back into the buddy allocator.
  670. */
  671. void drain_local_pages(void)
  672. {
  673. unsigned long flags;
  674. local_irq_save(flags);
  675. __drain_pages(smp_processor_id());
  676. local_irq_restore(flags);
  677. }
  678. #endif /* CONFIG_PM */
  679. /*
  680. * Free a 0-order page
  681. */
  682. static void fastcall free_hot_cold_page(struct page *page, int cold)
  683. {
  684. struct zone *zone = page_zone(page);
  685. struct per_cpu_pages *pcp;
  686. unsigned long flags;
  687. if (PageAnon(page))
  688. page->mapping = NULL;
  689. if (free_pages_check(page))
  690. return;
  691. if (!PageHighMem(page))
  692. debug_check_no_locks_freed(page_address(page), PAGE_SIZE);
  693. arch_free_page(page, 0);
  694. kernel_map_pages(page, 1, 0);
  695. pcp = &zone_pcp(zone, get_cpu())->pcp[cold];
  696. local_irq_save(flags);
  697. __count_vm_event(PGFREE);
  698. list_add(&page->lru, &pcp->list);
  699. pcp->count++;
  700. if (pcp->count >= pcp->high) {
  701. free_pages_bulk(zone, pcp->batch, &pcp->list, 0);
  702. pcp->count -= pcp->batch;
  703. }
  704. local_irq_restore(flags);
  705. put_cpu();
  706. }
  707. void fastcall free_hot_page(struct page *page)
  708. {
  709. free_hot_cold_page(page, 0);
  710. }
  711. void fastcall free_cold_page(struct page *page)
  712. {
  713. free_hot_cold_page(page, 1);
  714. }
  715. /*
  716. * split_page takes a non-compound higher-order page, and splits it into
  717. * n (1<<order) sub-pages: page[0..n]
  718. * Each sub-page must be freed individually.
  719. *
  720. * Note: this is probably too low level an operation for use in drivers.
  721. * Please consult with lkml before using this in your driver.
  722. */
  723. void split_page(struct page *page, unsigned int order)
  724. {
  725. int i;
  726. VM_BUG_ON(PageCompound(page));
  727. VM_BUG_ON(!page_count(page));
  728. for (i = 1; i < (1 << order); i++)
  729. set_page_refcounted(page + i);
  730. }
  731. /*
  732. * Really, prep_compound_page() should be called from __rmqueue_bulk(). But
  733. * we cheat by calling it from here, in the order > 0 path. Saves a branch
  734. * or two.
  735. */
  736. static struct page *buffered_rmqueue(struct zonelist *zonelist,
  737. struct zone *zone, int order, gfp_t gfp_flags)
  738. {
  739. unsigned long flags;
  740. struct page *page;
  741. int cold = !!(gfp_flags & __GFP_COLD);
  742. int cpu;
  743. again:
  744. cpu = get_cpu();
  745. if (likely(order == 0)) {
  746. struct per_cpu_pages *pcp;
  747. pcp = &zone_pcp(zone, cpu)->pcp[cold];
  748. local_irq_save(flags);
  749. if (!pcp->count) {
  750. pcp->count += rmqueue_bulk(zone, 0,
  751. pcp->batch, &pcp->list);
  752. if (unlikely(!pcp->count))
  753. goto failed;
  754. }
  755. page = list_entry(pcp->list.next, struct page, lru);
  756. list_del(&page->lru);
  757. pcp->count--;
  758. } else {
  759. spin_lock_irqsave(&zone->lock, flags);
  760. page = __rmqueue(zone, order);
  761. spin_unlock(&zone->lock);
  762. if (!page)
  763. goto failed;
  764. }
  765. __count_zone_vm_events(PGALLOC, zone, 1 << order);
  766. zone_statistics(zonelist, zone);
  767. local_irq_restore(flags);
  768. put_cpu();
  769. VM_BUG_ON(bad_range(zone, page));
  770. if (prep_new_page(page, order, gfp_flags))
  771. goto again;
  772. return page;
  773. failed:
  774. local_irq_restore(flags);
  775. put_cpu();
  776. return NULL;
  777. }
  778. #define ALLOC_NO_WATERMARKS 0x01 /* don't check watermarks at all */
  779. #define ALLOC_WMARK_MIN 0x02 /* use pages_min watermark */
  780. #define ALLOC_WMARK_LOW 0x04 /* use pages_low watermark */
  781. #define ALLOC_WMARK_HIGH 0x08 /* use pages_high watermark */
  782. #define ALLOC_HARDER 0x10 /* try to alloc harder */
  783. #define ALLOC_HIGH 0x20 /* __GFP_HIGH set */
  784. #define ALLOC_CPUSET 0x40 /* check for correct cpuset */
  785. /*
  786. * Return 1 if free pages are above 'mark'. This takes into account the order
  787. * of the allocation.
  788. */
  789. int zone_watermark_ok(struct zone *z, int order, unsigned long mark,
  790. int classzone_idx, int alloc_flags)
  791. {
  792. /* free_pages my go negative - that's OK */
  793. unsigned long min = mark;
  794. long free_pages = z->free_pages - (1 << order) + 1;
  795. int o;
  796. if (alloc_flags & ALLOC_HIGH)
  797. min -= min / 2;
  798. if (alloc_flags & ALLOC_HARDER)
  799. min -= min / 4;
  800. if (free_pages <= min + z->lowmem_reserve[classzone_idx])
  801. return 0;
  802. for (o = 0; o < order; o++) {
  803. /* At the next order, this order's pages become unavailable */
  804. free_pages -= z->free_area[o].nr_free << o;
  805. /* Require fewer higher order pages to be free */
  806. min >>= 1;
  807. if (free_pages <= min)
  808. return 0;
  809. }
  810. return 1;
  811. }
  812. /*
  813. * get_page_from_freeliest goes through the zonelist trying to allocate
  814. * a page.
  815. */
  816. static struct page *
  817. get_page_from_freelist(gfp_t gfp_mask, unsigned int order,
  818. struct zonelist *zonelist, int alloc_flags)
  819. {
  820. struct zone **z = zonelist->zones;
  821. struct page *page = NULL;
  822. int classzone_idx = zone_idx(*z);
  823. struct zone *zone;
  824. /*
  825. * Go through the zonelist once, looking for a zone with enough free.
  826. * See also cpuset_zone_allowed() comment in kernel/cpuset.c.
  827. */
  828. do {
  829. zone = *z;
  830. if (unlikely(NUMA_BUILD && (gfp_mask & __GFP_THISNODE) &&
  831. zone->zone_pgdat != zonelist->zones[0]->zone_pgdat))
  832. break;
  833. if ((alloc_flags & ALLOC_CPUSET) &&
  834. !cpuset_zone_allowed(zone, gfp_mask))
  835. continue;
  836. if (!(alloc_flags & ALLOC_NO_WATERMARKS)) {
  837. unsigned long mark;
  838. if (alloc_flags & ALLOC_WMARK_MIN)
  839. mark = zone->pages_min;
  840. else if (alloc_flags & ALLOC_WMARK_LOW)
  841. mark = zone->pages_low;
  842. else
  843. mark = zone->pages_high;
  844. if (!zone_watermark_ok(zone , order, mark,
  845. classzone_idx, alloc_flags))
  846. if (!zone_reclaim_mode ||
  847. !zone_reclaim(zone, gfp_mask, order))
  848. continue;
  849. }
  850. page = buffered_rmqueue(zonelist, zone, order, gfp_mask);
  851. if (page) {
  852. break;
  853. }
  854. } while (*(++z) != NULL);
  855. return page;
  856. }
  857. /*
  858. * This is the 'heart' of the zoned buddy allocator.
  859. */
  860. struct page * fastcall
  861. __alloc_pages(gfp_t gfp_mask, unsigned int order,
  862. struct zonelist *zonelist)
  863. {
  864. const gfp_t wait = gfp_mask & __GFP_WAIT;
  865. struct zone **z;
  866. struct page *page;
  867. struct reclaim_state reclaim_state;
  868. struct task_struct *p = current;
  869. int do_retry;
  870. int alloc_flags;
  871. int did_some_progress;
  872. might_sleep_if(wait);
  873. restart:
  874. z = zonelist->zones; /* the list of zones suitable for gfp_mask */
  875. if (unlikely(*z == NULL)) {
  876. /* Should this ever happen?? */
  877. return NULL;
  878. }
  879. page = get_page_from_freelist(gfp_mask|__GFP_HARDWALL, order,
  880. zonelist, ALLOC_WMARK_LOW|ALLOC_CPUSET);
  881. if (page)
  882. goto got_pg;
  883. do {
  884. wakeup_kswapd(*z, order);
  885. } while (*(++z));
  886. /*
  887. * OK, we're below the kswapd watermark and have kicked background
  888. * reclaim. Now things get more complex, so set up alloc_flags according
  889. * to how we want to proceed.
  890. *
  891. * The caller may dip into page reserves a bit more if the caller
  892. * cannot run direct reclaim, or if the caller has realtime scheduling
  893. * policy or is asking for __GFP_HIGH memory. GFP_ATOMIC requests will
  894. * set both ALLOC_HARDER (!wait) and ALLOC_HIGH (__GFP_HIGH).
  895. */
  896. alloc_flags = ALLOC_WMARK_MIN;
  897. if ((unlikely(rt_task(p)) && !in_interrupt()) || !wait)
  898. alloc_flags |= ALLOC_HARDER;
  899. if (gfp_mask & __GFP_HIGH)
  900. alloc_flags |= ALLOC_HIGH;
  901. if (wait)
  902. alloc_flags |= ALLOC_CPUSET;
  903. /*
  904. * Go through the zonelist again. Let __GFP_HIGH and allocations
  905. * coming from realtime tasks go deeper into reserves.
  906. *
  907. * This is the last chance, in general, before the goto nopage.
  908. * Ignore cpuset if GFP_ATOMIC (!wait) rather than fail alloc.
  909. * See also cpuset_zone_allowed() comment in kernel/cpuset.c.
  910. */
  911. page = get_page_from_freelist(gfp_mask, order, zonelist, alloc_flags);
  912. if (page)
  913. goto got_pg;
  914. /* This allocation should allow future memory freeing. */
  915. if (((p->flags & PF_MEMALLOC) || unlikely(test_thread_flag(TIF_MEMDIE)))
  916. && !in_interrupt()) {
  917. if (!(gfp_mask & __GFP_NOMEMALLOC)) {
  918. nofail_alloc:
  919. /* go through the zonelist yet again, ignoring mins */
  920. page = get_page_from_freelist(gfp_mask, order,
  921. zonelist, ALLOC_NO_WATERMARKS);
  922. if (page)
  923. goto got_pg;
  924. if (gfp_mask & __GFP_NOFAIL) {
  925. blk_congestion_wait(WRITE, HZ/50);
  926. goto nofail_alloc;
  927. }
  928. }
  929. goto nopage;
  930. }
  931. /* Atomic allocations - we can't balance anything */
  932. if (!wait)
  933. goto nopage;
  934. rebalance:
  935. cond_resched();
  936. /* We now go into synchronous reclaim */
  937. cpuset_memory_pressure_bump();
  938. p->flags |= PF_MEMALLOC;
  939. reclaim_state.reclaimed_slab = 0;
  940. p->reclaim_state = &reclaim_state;
  941. did_some_progress = try_to_free_pages(zonelist->zones, gfp_mask);
  942. p->reclaim_state = NULL;
  943. p->flags &= ~PF_MEMALLOC;
  944. cond_resched();
  945. if (likely(did_some_progress)) {
  946. page = get_page_from_freelist(gfp_mask, order,
  947. zonelist, alloc_flags);
  948. if (page)
  949. goto got_pg;
  950. } else if ((gfp_mask & __GFP_FS) && !(gfp_mask & __GFP_NORETRY)) {
  951. /*
  952. * Go through the zonelist yet one more time, keep
  953. * very high watermark here, this is only to catch
  954. * a parallel oom killing, we must fail if we're still
  955. * under heavy pressure.
  956. */
  957. page = get_page_from_freelist(gfp_mask|__GFP_HARDWALL, order,
  958. zonelist, ALLOC_WMARK_HIGH|ALLOC_CPUSET);
  959. if (page)
  960. goto got_pg;
  961. out_of_memory(zonelist, gfp_mask, order);
  962. goto restart;
  963. }
  964. /*
  965. * Don't let big-order allocations loop unless the caller explicitly
  966. * requests that. Wait for some write requests to complete then retry.
  967. *
  968. * In this implementation, __GFP_REPEAT means __GFP_NOFAIL for order
  969. * <= 3, but that may not be true in other implementations.
  970. */
  971. do_retry = 0;
  972. if (!(gfp_mask & __GFP_NORETRY)) {
  973. if ((order <= 3) || (gfp_mask & __GFP_REPEAT))
  974. do_retry = 1;
  975. if (gfp_mask & __GFP_NOFAIL)
  976. do_retry = 1;
  977. }
  978. if (do_retry) {
  979. blk_congestion_wait(WRITE, HZ/50);
  980. goto rebalance;
  981. }
  982. nopage:
  983. if (!(gfp_mask & __GFP_NOWARN) && printk_ratelimit()) {
  984. printk(KERN_WARNING "%s: page allocation failure."
  985. " order:%d, mode:0x%x\n",
  986. p->comm, order, gfp_mask);
  987. dump_stack();
  988. show_mem();
  989. }
  990. got_pg:
  991. return page;
  992. }
  993. EXPORT_SYMBOL(__alloc_pages);
  994. /*
  995. * Common helper functions.
  996. */
  997. fastcall unsigned long __get_free_pages(gfp_t gfp_mask, unsigned int order)
  998. {
  999. struct page * page;
  1000. page = alloc_pages(gfp_mask, order);
  1001. if (!page)
  1002. return 0;
  1003. return (unsigned long) page_address(page);
  1004. }
  1005. EXPORT_SYMBOL(__get_free_pages);
  1006. fastcall unsigned long get_zeroed_page(gfp_t gfp_mask)
  1007. {
  1008. struct page * page;
  1009. /*
  1010. * get_zeroed_page() returns a 32-bit address, which cannot represent
  1011. * a highmem page
  1012. */
  1013. VM_BUG_ON((gfp_mask & __GFP_HIGHMEM) != 0);
  1014. page = alloc_pages(gfp_mask | __GFP_ZERO, 0);
  1015. if (page)
  1016. return (unsigned long) page_address(page);
  1017. return 0;
  1018. }
  1019. EXPORT_SYMBOL(get_zeroed_page);
  1020. void __pagevec_free(struct pagevec *pvec)
  1021. {
  1022. int i = pagevec_count(pvec);
  1023. while (--i >= 0)
  1024. free_hot_cold_page(pvec->pages[i], pvec->cold);
  1025. }
  1026. fastcall void __free_pages(struct page *page, unsigned int order)
  1027. {
  1028. if (put_page_testzero(page)) {
  1029. if (order == 0)
  1030. free_hot_page(page);
  1031. else
  1032. __free_pages_ok(page, order);
  1033. }
  1034. }
  1035. EXPORT_SYMBOL(__free_pages);
  1036. fastcall void free_pages(unsigned long addr, unsigned int order)
  1037. {
  1038. if (addr != 0) {
  1039. VM_BUG_ON(!virt_addr_valid((void *)addr));
  1040. __free_pages(virt_to_page((void *)addr), order);
  1041. }
  1042. }
  1043. EXPORT_SYMBOL(free_pages);
  1044. /*
  1045. * Total amount of free (allocatable) RAM:
  1046. */
  1047. unsigned int nr_free_pages(void)
  1048. {
  1049. unsigned int sum = 0;
  1050. struct zone *zone;
  1051. for_each_zone(zone)
  1052. sum += zone->free_pages;
  1053. return sum;
  1054. }
  1055. EXPORT_SYMBOL(nr_free_pages);
  1056. #ifdef CONFIG_NUMA
  1057. unsigned int nr_free_pages_pgdat(pg_data_t *pgdat)
  1058. {
  1059. unsigned int sum = 0;
  1060. enum zone_type i;
  1061. for (i = 0; i < MAX_NR_ZONES; i++)
  1062. sum += pgdat->node_zones[i].free_pages;
  1063. return sum;
  1064. }
  1065. #endif
  1066. static unsigned int nr_free_zone_pages(int offset)
  1067. {
  1068. /* Just pick one node, since fallback list is circular */
  1069. pg_data_t *pgdat = NODE_DATA(numa_node_id());
  1070. unsigned int sum = 0;
  1071. struct zonelist *zonelist = pgdat->node_zonelists + offset;
  1072. struct zone **zonep = zonelist->zones;
  1073. struct zone *zone;
  1074. for (zone = *zonep++; zone; zone = *zonep++) {
  1075. unsigned long size = zone->present_pages;
  1076. unsigned long high = zone->pages_high;
  1077. if (size > high)
  1078. sum += size - high;
  1079. }
  1080. return sum;
  1081. }
  1082. /*
  1083. * Amount of free RAM allocatable within ZONE_DMA and ZONE_NORMAL
  1084. */
  1085. unsigned int nr_free_buffer_pages(void)
  1086. {
  1087. return nr_free_zone_pages(gfp_zone(GFP_USER));
  1088. }
  1089. /*
  1090. * Amount of free RAM allocatable within all zones
  1091. */
  1092. unsigned int nr_free_pagecache_pages(void)
  1093. {
  1094. return nr_free_zone_pages(gfp_zone(GFP_HIGHUSER));
  1095. }
  1096. static inline void show_node(struct zone *zone)
  1097. {
  1098. if (NUMA_BUILD)
  1099. printk("Node %ld ", zone_to_nid(zone));
  1100. }
  1101. void si_meminfo(struct sysinfo *val)
  1102. {
  1103. val->totalram = totalram_pages;
  1104. val->sharedram = 0;
  1105. val->freeram = nr_free_pages();
  1106. val->bufferram = nr_blockdev_pages();
  1107. val->totalhigh = totalhigh_pages;
  1108. val->freehigh = nr_free_highpages();
  1109. val->mem_unit = PAGE_SIZE;
  1110. }
  1111. EXPORT_SYMBOL(si_meminfo);
  1112. #ifdef CONFIG_NUMA
  1113. void si_meminfo_node(struct sysinfo *val, int nid)
  1114. {
  1115. pg_data_t *pgdat = NODE_DATA(nid);
  1116. val->totalram = pgdat->node_present_pages;
  1117. val->freeram = nr_free_pages_pgdat(pgdat);
  1118. #ifdef CONFIG_HIGHMEM
  1119. val->totalhigh = pgdat->node_zones[ZONE_HIGHMEM].present_pages;
  1120. val->freehigh = pgdat->node_zones[ZONE_HIGHMEM].free_pages;
  1121. #else
  1122. val->totalhigh = 0;
  1123. val->freehigh = 0;
  1124. #endif
  1125. val->mem_unit = PAGE_SIZE;
  1126. }
  1127. #endif
  1128. #define K(x) ((x) << (PAGE_SHIFT-10))
  1129. /*
  1130. * Show free area list (used inside shift_scroll-lock stuff)
  1131. * We also calculate the percentage fragmentation. We do this by counting the
  1132. * memory on each free list with the exception of the first item on the list.
  1133. */
  1134. void show_free_areas(void)
  1135. {
  1136. int cpu;
  1137. unsigned long active;
  1138. unsigned long inactive;
  1139. unsigned long free;
  1140. struct zone *zone;
  1141. for_each_zone(zone) {
  1142. if (!populated_zone(zone))
  1143. continue;
  1144. show_node(zone);
  1145. printk("%s per-cpu:\n", zone->name);
  1146. for_each_online_cpu(cpu) {
  1147. struct per_cpu_pageset *pageset;
  1148. pageset = zone_pcp(zone, cpu);
  1149. printk("CPU %4d: Hot: hi:%5d, btch:%4d usd:%4d "
  1150. "Cold: hi:%5d, btch:%4d usd:%4d\n",
  1151. cpu, pageset->pcp[0].high,
  1152. pageset->pcp[0].batch, pageset->pcp[0].count,
  1153. pageset->pcp[1].high, pageset->pcp[1].batch,
  1154. pageset->pcp[1].count);
  1155. }
  1156. }
  1157. get_zone_counts(&active, &inactive, &free);
  1158. printk("Active:%lu inactive:%lu dirty:%lu writeback:%lu "
  1159. "unstable:%lu free:%u slab:%lu mapped:%lu pagetables:%lu\n",
  1160. active,
  1161. inactive,
  1162. global_page_state(NR_FILE_DIRTY),
  1163. global_page_state(NR_WRITEBACK),
  1164. global_page_state(NR_UNSTABLE_NFS),
  1165. nr_free_pages(),
  1166. global_page_state(NR_SLAB_RECLAIMABLE) +
  1167. global_page_state(NR_SLAB_UNRECLAIMABLE),
  1168. global_page_state(NR_FILE_MAPPED),
  1169. global_page_state(NR_PAGETABLE));
  1170. for_each_zone(zone) {
  1171. int i;
  1172. if (!populated_zone(zone))
  1173. continue;
  1174. show_node(zone);
  1175. printk("%s"
  1176. " free:%lukB"
  1177. " min:%lukB"
  1178. " low:%lukB"
  1179. " high:%lukB"
  1180. " active:%lukB"
  1181. " inactive:%lukB"
  1182. " present:%lukB"
  1183. " pages_scanned:%lu"
  1184. " all_unreclaimable? %s"
  1185. "\n",
  1186. zone->name,
  1187. K(zone->free_pages),
  1188. K(zone->pages_min),
  1189. K(zone->pages_low),
  1190. K(zone->pages_high),
  1191. K(zone->nr_active),
  1192. K(zone->nr_inactive),
  1193. K(zone->present_pages),
  1194. zone->pages_scanned,
  1195. (zone->all_unreclaimable ? "yes" : "no")
  1196. );
  1197. printk("lowmem_reserve[]:");
  1198. for (i = 0; i < MAX_NR_ZONES; i++)
  1199. printk(" %lu", zone->lowmem_reserve[i]);
  1200. printk("\n");
  1201. }
  1202. for_each_zone(zone) {
  1203. unsigned long nr[MAX_ORDER], flags, order, total = 0;
  1204. if (!populated_zone(zone))
  1205. continue;
  1206. show_node(zone);
  1207. printk("%s: ", zone->name);
  1208. spin_lock_irqsave(&zone->lock, flags);
  1209. for (order = 0; order < MAX_ORDER; order++) {
  1210. nr[order] = zone->free_area[order].nr_free;
  1211. total += nr[order] << order;
  1212. }
  1213. spin_unlock_irqrestore(&zone->lock, flags);
  1214. for (order = 0; order < MAX_ORDER; order++)
  1215. printk("%lu*%lukB ", nr[order], K(1UL) << order);
  1216. printk("= %lukB\n", K(total));
  1217. }
  1218. show_swap_cache_info();
  1219. }
  1220. /*
  1221. * Builds allocation fallback zone lists.
  1222. *
  1223. * Add all populated zones of a node to the zonelist.
  1224. */
  1225. static int __meminit build_zonelists_node(pg_data_t *pgdat,
  1226. struct zonelist *zonelist, int nr_zones, enum zone_type zone_type)
  1227. {
  1228. struct zone *zone;
  1229. BUG_ON(zone_type >= MAX_NR_ZONES);
  1230. zone_type++;
  1231. do {
  1232. zone_type--;
  1233. zone = pgdat->node_zones + zone_type;
  1234. if (populated_zone(zone)) {
  1235. zonelist->zones[nr_zones++] = zone;
  1236. check_highest_zone(zone_type);
  1237. }
  1238. } while (zone_type);
  1239. return nr_zones;
  1240. }
  1241. #ifdef CONFIG_NUMA
  1242. #define MAX_NODE_LOAD (num_online_nodes())
  1243. static int __meminitdata node_load[MAX_NUMNODES];
  1244. /**
  1245. * find_next_best_node - find the next node that should appear in a given node's fallback list
  1246. * @node: node whose fallback list we're appending
  1247. * @used_node_mask: nodemask_t of already used nodes
  1248. *
  1249. * We use a number of factors to determine which is the next node that should
  1250. * appear on a given node's fallback list. The node should not have appeared
  1251. * already in @node's fallback list, and it should be the next closest node
  1252. * according to the distance array (which contains arbitrary distance values
  1253. * from each node to each node in the system), and should also prefer nodes
  1254. * with no CPUs, since presumably they'll have very little allocation pressure
  1255. * on them otherwise.
  1256. * It returns -1 if no node is found.
  1257. */
  1258. static int __meminit find_next_best_node(int node, nodemask_t *used_node_mask)
  1259. {
  1260. int n, val;
  1261. int min_val = INT_MAX;
  1262. int best_node = -1;
  1263. /* Use the local node if we haven't already */
  1264. if (!node_isset(node, *used_node_mask)) {
  1265. node_set(node, *used_node_mask);
  1266. return node;
  1267. }
  1268. for_each_online_node(n) {
  1269. cpumask_t tmp;
  1270. /* Don't want a node to appear more than once */
  1271. if (node_isset(n, *used_node_mask))
  1272. continue;
  1273. /* Use the distance array to find the distance */
  1274. val = node_distance(node, n);
  1275. /* Penalize nodes under us ("prefer the next node") */
  1276. val += (n < node);
  1277. /* Give preference to headless and unused nodes */
  1278. tmp = node_to_cpumask(n);
  1279. if (!cpus_empty(tmp))
  1280. val += PENALTY_FOR_NODE_WITH_CPUS;
  1281. /* Slight preference for less loaded node */
  1282. val *= (MAX_NODE_LOAD*MAX_NUMNODES);
  1283. val += node_load[n];
  1284. if (val < min_val) {
  1285. min_val = val;
  1286. best_node = n;
  1287. }
  1288. }
  1289. if (best_node >= 0)
  1290. node_set(best_node, *used_node_mask);
  1291. return best_node;
  1292. }
  1293. static void __meminit build_zonelists(pg_data_t *pgdat)
  1294. {
  1295. int j, node, local_node;
  1296. enum zone_type i;
  1297. int prev_node, load;
  1298. struct zonelist *zonelist;
  1299. nodemask_t used_mask;
  1300. /* initialize zonelists */
  1301. for (i = 0; i < MAX_NR_ZONES; i++) {
  1302. zonelist = pgdat->node_zonelists + i;
  1303. zonelist->zones[0] = NULL;
  1304. }
  1305. /* NUMA-aware ordering of nodes */
  1306. local_node = pgdat->node_id;
  1307. load = num_online_nodes();
  1308. prev_node = local_node;
  1309. nodes_clear(used_mask);
  1310. while ((node = find_next_best_node(local_node, &used_mask)) >= 0) {
  1311. int distance = node_distance(local_node, node);
  1312. /*
  1313. * If another node is sufficiently far away then it is better
  1314. * to reclaim pages in a zone before going off node.
  1315. */
  1316. if (distance > RECLAIM_DISTANCE)
  1317. zone_reclaim_mode = 1;
  1318. /*
  1319. * We don't want to pressure a particular node.
  1320. * So adding penalty to the first node in same
  1321. * distance group to make it round-robin.
  1322. */
  1323. if (distance != node_distance(local_node, prev_node))
  1324. node_load[node] += load;
  1325. prev_node = node;
  1326. load--;
  1327. for (i = 0; i < MAX_NR_ZONES; i++) {
  1328. zonelist = pgdat->node_zonelists + i;
  1329. for (j = 0; zonelist->zones[j] != NULL; j++);
  1330. j = build_zonelists_node(NODE_DATA(node), zonelist, j, i);
  1331. zonelist->zones[j] = NULL;
  1332. }
  1333. }
  1334. }
  1335. #else /* CONFIG_NUMA */
  1336. static void __meminit build_zonelists(pg_data_t *pgdat)
  1337. {
  1338. int node, local_node;
  1339. enum zone_type i,j;
  1340. local_node = pgdat->node_id;
  1341. for (i = 0; i < MAX_NR_ZONES; i++) {
  1342. struct zonelist *zonelist;
  1343. zonelist = pgdat->node_zonelists + i;
  1344. j = build_zonelists_node(pgdat, zonelist, 0, i);
  1345. /*
  1346. * Now we build the zonelist so that it contains the zones
  1347. * of all the other nodes.
  1348. * We don't want to pressure a particular node, so when
  1349. * building the zones for node N, we make sure that the
  1350. * zones coming right after the local ones are those from
  1351. * node N+1 (modulo N)
  1352. */
  1353. for (node = local_node + 1; node < MAX_NUMNODES; node++) {
  1354. if (!node_online(node))
  1355. continue;
  1356. j = build_zonelists_node(NODE_DATA(node), zonelist, j, i);
  1357. }
  1358. for (node = 0; node < local_node; node++) {
  1359. if (!node_online(node))
  1360. continue;
  1361. j = build_zonelists_node(NODE_DATA(node), zonelist, j, i);
  1362. }
  1363. zonelist->zones[j] = NULL;
  1364. }
  1365. }
  1366. #endif /* CONFIG_NUMA */
  1367. /* return values int ....just for stop_machine_run() */
  1368. static int __meminit __build_all_zonelists(void *dummy)
  1369. {
  1370. int nid;
  1371. for_each_online_node(nid)
  1372. build_zonelists(NODE_DATA(nid));
  1373. return 0;
  1374. }
  1375. void __meminit build_all_zonelists(void)
  1376. {
  1377. if (system_state == SYSTEM_BOOTING) {
  1378. __build_all_zonelists(NULL);
  1379. cpuset_init_current_mems_allowed();
  1380. } else {
  1381. /* we have to stop all cpus to guaranntee there is no user
  1382. of zonelist */
  1383. stop_machine_run(__build_all_zonelists, NULL, NR_CPUS);
  1384. /* cpuset refresh routine should be here */
  1385. }
  1386. vm_total_pages = nr_free_pagecache_pages();
  1387. printk("Built %i zonelists. Total pages: %ld\n",
  1388. num_online_nodes(), vm_total_pages);
  1389. }
  1390. /*
  1391. * Helper functions to size the waitqueue hash table.
  1392. * Essentially these want to choose hash table sizes sufficiently
  1393. * large so that collisions trying to wait on pages are rare.
  1394. * But in fact, the number of active page waitqueues on typical
  1395. * systems is ridiculously low, less than 200. So this is even
  1396. * conservative, even though it seems large.
  1397. *
  1398. * The constant PAGES_PER_WAITQUEUE specifies the ratio of pages to
  1399. * waitqueues, i.e. the size of the waitq table given the number of pages.
  1400. */
  1401. #define PAGES_PER_WAITQUEUE 256
  1402. #ifndef CONFIG_MEMORY_HOTPLUG
  1403. static inline unsigned long wait_table_hash_nr_entries(unsigned long pages)
  1404. {
  1405. unsigned long size = 1;
  1406. pages /= PAGES_PER_WAITQUEUE;
  1407. while (size < pages)
  1408. size <<= 1;
  1409. /*
  1410. * Once we have dozens or even hundreds of threads sleeping
  1411. * on IO we've got bigger problems than wait queue collision.
  1412. * Limit the size of the wait table to a reasonable size.
  1413. */
  1414. size = min(size, 4096UL);
  1415. return max(size, 4UL);
  1416. }
  1417. #else
  1418. /*
  1419. * A zone's size might be changed by hot-add, so it is not possible to determine
  1420. * a suitable size for its wait_table. So we use the maximum size now.
  1421. *
  1422. * The max wait table size = 4096 x sizeof(wait_queue_head_t). ie:
  1423. *
  1424. * i386 (preemption config) : 4096 x 16 = 64Kbyte.
  1425. * ia64, x86-64 (no preemption): 4096 x 20 = 80Kbyte.
  1426. * ia64, x86-64 (preemption) : 4096 x 24 = 96Kbyte.
  1427. *
  1428. * The maximum entries are prepared when a zone's memory is (512K + 256) pages
  1429. * or more by the traditional way. (See above). It equals:
  1430. *
  1431. * i386, x86-64, powerpc(4K page size) : = ( 2G + 1M)byte.
  1432. * ia64(16K page size) : = ( 8G + 4M)byte.
  1433. * powerpc (64K page size) : = (32G +16M)byte.
  1434. */
  1435. static inline unsigned long wait_table_hash_nr_entries(unsigned long pages)
  1436. {
  1437. return 4096UL;
  1438. }
  1439. #endif
  1440. /*
  1441. * This is an integer logarithm so that shifts can be used later
  1442. * to extract the more random high bits from the multiplicative
  1443. * hash function before the remainder is taken.
  1444. */
  1445. static inline unsigned long wait_table_bits(unsigned long size)
  1446. {
  1447. return ffz(~size);
  1448. }
  1449. #define LONG_ALIGN(x) (((x)+(sizeof(long))-1)&~((sizeof(long))-1))
  1450. /*
  1451. * Initially all pages are reserved - free ones are freed
  1452. * up by free_all_bootmem() once the early boot process is
  1453. * done. Non-atomic initialization, single-pass.
  1454. */
  1455. void __meminit memmap_init_zone(unsigned long size, int nid, unsigned long zone,
  1456. unsigned long start_pfn)
  1457. {
  1458. struct page *page;
  1459. unsigned long end_pfn = start_pfn + size;
  1460. unsigned long pfn;
  1461. for (pfn = start_pfn; pfn < end_pfn; pfn++) {
  1462. if (!early_pfn_valid(pfn))
  1463. continue;
  1464. page = pfn_to_page(pfn);
  1465. set_page_links(page, zone, nid, pfn);
  1466. init_page_count(page);
  1467. reset_page_mapcount(page);
  1468. SetPageReserved(page);
  1469. INIT_LIST_HEAD(&page->lru);
  1470. #ifdef WANT_PAGE_VIRTUAL
  1471. /* The shift won't overflow because ZONE_NORMAL is below 4G. */
  1472. if (!is_highmem_idx(zone))
  1473. set_page_address(page, __va(pfn << PAGE_SHIFT));
  1474. #endif
  1475. }
  1476. }
  1477. void zone_init_free_lists(struct pglist_data *pgdat, struct zone *zone,
  1478. unsigned long size)
  1479. {
  1480. int order;
  1481. for (order = 0; order < MAX_ORDER ; order++) {
  1482. INIT_LIST_HEAD(&zone->free_area[order].free_list);
  1483. zone->free_area[order].nr_free = 0;
  1484. }
  1485. }
  1486. #define ZONETABLE_INDEX(x, zone_nr) ((x << ZONES_SHIFT) | zone_nr)
  1487. void zonetable_add(struct zone *zone, int nid, enum zone_type zid,
  1488. unsigned long pfn, unsigned long size)
  1489. {
  1490. unsigned long snum = pfn_to_section_nr(pfn);
  1491. unsigned long end = pfn_to_section_nr(pfn + size);
  1492. if (FLAGS_HAS_NODE)
  1493. zone_table[ZONETABLE_INDEX(nid, zid)] = zone;
  1494. else
  1495. for (; snum <= end; snum++)
  1496. zone_table[ZONETABLE_INDEX(snum, zid)] = zone;
  1497. }
  1498. #ifndef __HAVE_ARCH_MEMMAP_INIT
  1499. #define memmap_init(size, nid, zone, start_pfn) \
  1500. memmap_init_zone((size), (nid), (zone), (start_pfn))
  1501. #endif
  1502. static int __cpuinit zone_batchsize(struct zone *zone)
  1503. {
  1504. int batch;
  1505. /*
  1506. * The per-cpu-pages pools are set to around 1000th of the
  1507. * size of the zone. But no more than 1/2 of a meg.
  1508. *
  1509. * OK, so we don't know how big the cache is. So guess.
  1510. */
  1511. batch = zone->present_pages / 1024;
  1512. if (batch * PAGE_SIZE > 512 * 1024)
  1513. batch = (512 * 1024) / PAGE_SIZE;
  1514. batch /= 4; /* We effectively *= 4 below */
  1515. if (batch < 1)
  1516. batch = 1;
  1517. /*
  1518. * Clamp the batch to a 2^n - 1 value. Having a power
  1519. * of 2 value was found to be more likely to have
  1520. * suboptimal cache aliasing properties in some cases.
  1521. *
  1522. * For example if 2 tasks are alternately allocating
  1523. * batches of pages, one task can end up with a lot
  1524. * of pages of one half of the possible page colors
  1525. * and the other with pages of the other colors.
  1526. */
  1527. batch = (1 << (fls(batch + batch/2)-1)) - 1;
  1528. return batch;
  1529. }
  1530. inline void setup_pageset(struct per_cpu_pageset *p, unsigned long batch)
  1531. {
  1532. struct per_cpu_pages *pcp;
  1533. memset(p, 0, sizeof(*p));
  1534. pcp = &p->pcp[0]; /* hot */
  1535. pcp->count = 0;
  1536. pcp->high = 6 * batch;
  1537. pcp->batch = max(1UL, 1 * batch);
  1538. INIT_LIST_HEAD(&pcp->list);
  1539. pcp = &p->pcp[1]; /* cold*/
  1540. pcp->count = 0;
  1541. pcp->high = 2 * batch;
  1542. pcp->batch = max(1UL, batch/2);
  1543. INIT_LIST_HEAD(&pcp->list);
  1544. }
  1545. /*
  1546. * setup_pagelist_highmark() sets the high water mark for hot per_cpu_pagelist
  1547. * to the value high for the pageset p.
  1548. */
  1549. static void setup_pagelist_highmark(struct per_cpu_pageset *p,
  1550. unsigned long high)
  1551. {
  1552. struct per_cpu_pages *pcp;
  1553. pcp = &p->pcp[0]; /* hot list */
  1554. pcp->high = high;
  1555. pcp->batch = max(1UL, high/4);
  1556. if ((high/4) > (PAGE_SHIFT * 8))
  1557. pcp->batch = PAGE_SHIFT * 8;
  1558. }
  1559. #ifdef CONFIG_NUMA
  1560. /*
  1561. * Boot pageset table. One per cpu which is going to be used for all
  1562. * zones and all nodes. The parameters will be set in such a way
  1563. * that an item put on a list will immediately be handed over to
  1564. * the buddy list. This is safe since pageset manipulation is done
  1565. * with interrupts disabled.
  1566. *
  1567. * Some NUMA counter updates may also be caught by the boot pagesets.
  1568. *
  1569. * The boot_pagesets must be kept even after bootup is complete for
  1570. * unused processors and/or zones. They do play a role for bootstrapping
  1571. * hotplugged processors.
  1572. *
  1573. * zoneinfo_show() and maybe other functions do
  1574. * not check if the processor is online before following the pageset pointer.
  1575. * Other parts of the kernel may not check if the zone is available.
  1576. */
  1577. static struct per_cpu_pageset boot_pageset[NR_CPUS];
  1578. /*
  1579. * Dynamically allocate memory for the
  1580. * per cpu pageset array in struct zone.
  1581. */
  1582. static int __cpuinit process_zones(int cpu)
  1583. {
  1584. struct zone *zone, *dzone;
  1585. for_each_zone(zone) {
  1586. if (!populated_zone(zone))
  1587. continue;
  1588. zone_pcp(zone, cpu) = kmalloc_node(sizeof(struct per_cpu_pageset),
  1589. GFP_KERNEL, cpu_to_node(cpu));
  1590. if (!zone_pcp(zone, cpu))
  1591. goto bad;
  1592. setup_pageset(zone_pcp(zone, cpu), zone_batchsize(zone));
  1593. if (percpu_pagelist_fraction)
  1594. setup_pagelist_highmark(zone_pcp(zone, cpu),
  1595. (zone->present_pages / percpu_pagelist_fraction));
  1596. }
  1597. return 0;
  1598. bad:
  1599. for_each_zone(dzone) {
  1600. if (dzone == zone)
  1601. break;
  1602. kfree(zone_pcp(dzone, cpu));
  1603. zone_pcp(dzone, cpu) = NULL;
  1604. }
  1605. return -ENOMEM;
  1606. }
  1607. static inline void free_zone_pagesets(int cpu)
  1608. {
  1609. struct zone *zone;
  1610. for_each_zone(zone) {
  1611. struct per_cpu_pageset *pset = zone_pcp(zone, cpu);
  1612. /* Free per_cpu_pageset if it is slab allocated */
  1613. if (pset != &boot_pageset[cpu])
  1614. kfree(pset);
  1615. zone_pcp(zone, cpu) = NULL;
  1616. }
  1617. }
  1618. static int __cpuinit pageset_cpuup_callback(struct notifier_block *nfb,
  1619. unsigned long action,
  1620. void *hcpu)
  1621. {
  1622. int cpu = (long)hcpu;
  1623. int ret = NOTIFY_OK;
  1624. switch (action) {
  1625. case CPU_UP_PREPARE:
  1626. if (process_zones(cpu))
  1627. ret = NOTIFY_BAD;
  1628. break;
  1629. case CPU_UP_CANCELED:
  1630. case CPU_DEAD:
  1631. free_zone_pagesets(cpu);
  1632. break;
  1633. default:
  1634. break;
  1635. }
  1636. return ret;
  1637. }
  1638. static struct notifier_block __cpuinitdata pageset_notifier =
  1639. { &pageset_cpuup_callback, NULL, 0 };
  1640. void __init setup_per_cpu_pageset(void)
  1641. {
  1642. int err;
  1643. /* Initialize per_cpu_pageset for cpu 0.
  1644. * A cpuup callback will do this for every cpu
  1645. * as it comes online
  1646. */
  1647. err = process_zones(smp_processor_id());
  1648. BUG_ON(err);
  1649. register_cpu_notifier(&pageset_notifier);
  1650. }
  1651. #endif
  1652. static __meminit
  1653. int zone_wait_table_init(struct zone *zone, unsigned long zone_size_pages)
  1654. {
  1655. int i;
  1656. struct pglist_data *pgdat = zone->zone_pgdat;
  1657. size_t alloc_size;
  1658. /*
  1659. * The per-page waitqueue mechanism uses hashed waitqueues
  1660. * per zone.
  1661. */
  1662. zone->wait_table_hash_nr_entries =
  1663. wait_table_hash_nr_entries(zone_size_pages);
  1664. zone->wait_table_bits =
  1665. wait_table_bits(zone->wait_table_hash_nr_entries);
  1666. alloc_size = zone->wait_table_hash_nr_entries
  1667. * sizeof(wait_queue_head_t);
  1668. if (system_state == SYSTEM_BOOTING) {
  1669. zone->wait_table = (wait_queue_head_t *)
  1670. alloc_bootmem_node(pgdat, alloc_size);
  1671. } else {
  1672. /*
  1673. * This case means that a zone whose size was 0 gets new memory
  1674. * via memory hot-add.
  1675. * But it may be the case that a new node was hot-added. In
  1676. * this case vmalloc() will not be able to use this new node's
  1677. * memory - this wait_table must be initialized to use this new
  1678. * node itself as well.
  1679. * To use this new node's memory, further consideration will be
  1680. * necessary.
  1681. */
  1682. zone->wait_table = (wait_queue_head_t *)vmalloc(alloc_size);
  1683. }
  1684. if (!zone->wait_table)
  1685. return -ENOMEM;
  1686. for(i = 0; i < zone->wait_table_hash_nr_entries; ++i)
  1687. init_waitqueue_head(zone->wait_table + i);
  1688. return 0;
  1689. }
  1690. static __meminit void zone_pcp_init(struct zone *zone)
  1691. {
  1692. int cpu;
  1693. unsigned long batch = zone_batchsize(zone);
  1694. for (cpu = 0; cpu < NR_CPUS; cpu++) {
  1695. #ifdef CONFIG_NUMA
  1696. /* Early boot. Slab allocator not functional yet */
  1697. zone_pcp(zone, cpu) = &boot_pageset[cpu];
  1698. setup_pageset(&boot_pageset[cpu],0);
  1699. #else
  1700. setup_pageset(zone_pcp(zone,cpu), batch);
  1701. #endif
  1702. }
  1703. if (zone->present_pages)
  1704. printk(KERN_DEBUG " %s zone: %lu pages, LIFO batch:%lu\n",
  1705. zone->name, zone->present_pages, batch);
  1706. }
  1707. __meminit int init_currently_empty_zone(struct zone *zone,
  1708. unsigned long zone_start_pfn,
  1709. unsigned long size)
  1710. {
  1711. struct pglist_data *pgdat = zone->zone_pgdat;
  1712. int ret;
  1713. ret = zone_wait_table_init(zone, size);
  1714. if (ret)
  1715. return ret;
  1716. pgdat->nr_zones = zone_idx(zone) + 1;
  1717. zone->zone_start_pfn = zone_start_pfn;
  1718. memmap_init(size, pgdat->node_id, zone_idx(zone), zone_start_pfn);
  1719. zone_init_free_lists(pgdat, zone, zone->spanned_pages);
  1720. return 0;
  1721. }
  1722. #ifdef CONFIG_ARCH_POPULATES_NODE_MAP
  1723. /*
  1724. * Basic iterator support. Return the first range of PFNs for a node
  1725. * Note: nid == MAX_NUMNODES returns first region regardless of node
  1726. */
  1727. static int __init first_active_region_index_in_nid(int nid)
  1728. {
  1729. int i;
  1730. for (i = 0; i < nr_nodemap_entries; i++)
  1731. if (nid == MAX_NUMNODES || early_node_map[i].nid == nid)
  1732. return i;
  1733. return -1;
  1734. }
  1735. /*
  1736. * Basic iterator support. Return the next active range of PFNs for a node
  1737. * Note: nid == MAX_NUMNODES returns next region regardles of node
  1738. */
  1739. static int __init next_active_region_index_in_nid(int index, int nid)
  1740. {
  1741. for (index = index + 1; index < nr_nodemap_entries; index++)
  1742. if (nid == MAX_NUMNODES || early_node_map[index].nid == nid)
  1743. return index;
  1744. return -1;
  1745. }
  1746. #ifndef CONFIG_HAVE_ARCH_EARLY_PFN_TO_NID
  1747. /*
  1748. * Required by SPARSEMEM. Given a PFN, return what node the PFN is on.
  1749. * Architectures may implement their own version but if add_active_range()
  1750. * was used and there are no special requirements, this is a convenient
  1751. * alternative
  1752. */
  1753. int __init early_pfn_to_nid(unsigned long pfn)
  1754. {
  1755. int i;
  1756. for (i = 0; i < nr_nodemap_entries; i++) {
  1757. unsigned long start_pfn = early_node_map[i].start_pfn;
  1758. unsigned long end_pfn = early_node_map[i].end_pfn;
  1759. if (start_pfn <= pfn && pfn < end_pfn)
  1760. return early_node_map[i].nid;
  1761. }
  1762. return 0;
  1763. }
  1764. #endif /* CONFIG_HAVE_ARCH_EARLY_PFN_TO_NID */
  1765. /* Basic iterator support to walk early_node_map[] */
  1766. #define for_each_active_range_index_in_nid(i, nid) \
  1767. for (i = first_active_region_index_in_nid(nid); i != -1; \
  1768. i = next_active_region_index_in_nid(i, nid))
  1769. /**
  1770. * free_bootmem_with_active_regions - Call free_bootmem_node for each active range
  1771. * @nid: The node to free memory on. If MAX_NUMNODES, all nodes are freed.
  1772. * @max_low_pfn: The highest PFN that will be passed to free_bootmem_node
  1773. *
  1774. * If an architecture guarantees that all ranges registered with
  1775. * add_active_ranges() contain no holes and may be freed, this
  1776. * this function may be used instead of calling free_bootmem() manually.
  1777. */
  1778. void __init free_bootmem_with_active_regions(int nid,
  1779. unsigned long max_low_pfn)
  1780. {
  1781. int i;
  1782. for_each_active_range_index_in_nid(i, nid) {
  1783. unsigned long size_pages = 0;
  1784. unsigned long end_pfn = early_node_map[i].end_pfn;
  1785. if (early_node_map[i].start_pfn >= max_low_pfn)
  1786. continue;
  1787. if (end_pfn > max_low_pfn)
  1788. end_pfn = max_low_pfn;
  1789. size_pages = end_pfn - early_node_map[i].start_pfn;
  1790. free_bootmem_node(NODE_DATA(early_node_map[i].nid),
  1791. PFN_PHYS(early_node_map[i].start_pfn),
  1792. size_pages << PAGE_SHIFT);
  1793. }
  1794. }
  1795. /**
  1796. * sparse_memory_present_with_active_regions - Call memory_present for each active range
  1797. * @nid: The node to call memory_present for. If MAX_NUMNODES, all nodes will be used.
  1798. *
  1799. * If an architecture guarantees that all ranges registered with
  1800. * add_active_ranges() contain no holes and may be freed, this
  1801. * function may be used instead of calling memory_present() manually.
  1802. */
  1803. void __init sparse_memory_present_with_active_regions(int nid)
  1804. {
  1805. int i;
  1806. for_each_active_range_index_in_nid(i, nid)
  1807. memory_present(early_node_map[i].nid,
  1808. early_node_map[i].start_pfn,
  1809. early_node_map[i].end_pfn);
  1810. }
  1811. /**
  1812. * push_node_boundaries - Push node boundaries to at least the requested boundary
  1813. * @nid: The nid of the node to push the boundary for
  1814. * @start_pfn: The start pfn of the node
  1815. * @end_pfn: The end pfn of the node
  1816. *
  1817. * In reserve-based hot-add, mem_map is allocated that is unused until hotadd
  1818. * time. Specifically, on x86_64, SRAT will report ranges that can potentially
  1819. * be hotplugged even though no physical memory exists. This function allows
  1820. * an arch to push out the node boundaries so mem_map is allocated that can
  1821. * be used later.
  1822. */
  1823. #ifdef CONFIG_MEMORY_HOTPLUG_RESERVE
  1824. void __init push_node_boundaries(unsigned int nid,
  1825. unsigned long start_pfn, unsigned long end_pfn)
  1826. {
  1827. printk(KERN_DEBUG "Entering push_node_boundaries(%u, %lu, %lu)\n",
  1828. nid, start_pfn, end_pfn);
  1829. /* Initialise the boundary for this node if necessary */
  1830. if (node_boundary_end_pfn[nid] == 0)
  1831. node_boundary_start_pfn[nid] = -1UL;
  1832. /* Update the boundaries */
  1833. if (node_boundary_start_pfn[nid] > start_pfn)
  1834. node_boundary_start_pfn[nid] = start_pfn;
  1835. if (node_boundary_end_pfn[nid] < end_pfn)
  1836. node_boundary_end_pfn[nid] = end_pfn;
  1837. }
  1838. /* If necessary, push the node boundary out for reserve hotadd */
  1839. static void __init account_node_boundary(unsigned int nid,
  1840. unsigned long *start_pfn, unsigned long *end_pfn)
  1841. {
  1842. printk(KERN_DEBUG "Entering account_node_boundary(%u, %lu, %lu)\n",
  1843. nid, *start_pfn, *end_pfn);
  1844. /* Return if boundary information has not been provided */
  1845. if (node_boundary_end_pfn[nid] == 0)
  1846. return;
  1847. /* Check the boundaries and update if necessary */
  1848. if (node_boundary_start_pfn[nid] < *start_pfn)
  1849. *start_pfn = node_boundary_start_pfn[nid];
  1850. if (node_boundary_end_pfn[nid] > *end_pfn)
  1851. *end_pfn = node_boundary_end_pfn[nid];
  1852. }
  1853. #else
  1854. void __init push_node_boundaries(unsigned int nid,
  1855. unsigned long start_pfn, unsigned long end_pfn) {}
  1856. static void __init account_node_boundary(unsigned int nid,
  1857. unsigned long *start_pfn, unsigned long *end_pfn) {}
  1858. #endif
  1859. /**
  1860. * get_pfn_range_for_nid - Return the start and end page frames for a node
  1861. * @nid: The nid to return the range for. If MAX_NUMNODES, the min and max PFN are returned.
  1862. * @start_pfn: Passed by reference. On return, it will have the node start_pfn.
  1863. * @end_pfn: Passed by reference. On return, it will have the node end_pfn.
  1864. *
  1865. * It returns the start and end page frame of a node based on information
  1866. * provided by an arch calling add_active_range(). If called for a node
  1867. * with no available memory, a warning is printed and the start and end
  1868. * PFNs will be 0.
  1869. */
  1870. void __init get_pfn_range_for_nid(unsigned int nid,
  1871. unsigned long *start_pfn, unsigned long *end_pfn)
  1872. {
  1873. int i;
  1874. *start_pfn = -1UL;
  1875. *end_pfn = 0;
  1876. for_each_active_range_index_in_nid(i, nid) {
  1877. *start_pfn = min(*start_pfn, early_node_map[i].start_pfn);
  1878. *end_pfn = max(*end_pfn, early_node_map[i].end_pfn);
  1879. }
  1880. if (*start_pfn == -1UL) {
  1881. printk(KERN_WARNING "Node %u active with no memory\n", nid);
  1882. *start_pfn = 0;
  1883. }
  1884. /* Push the node boundaries out if requested */
  1885. account_node_boundary(nid, start_pfn, end_pfn);
  1886. }
  1887. /*
  1888. * Return the number of pages a zone spans in a node, including holes
  1889. * present_pages = zone_spanned_pages_in_node() - zone_absent_pages_in_node()
  1890. */
  1891. unsigned long __init zone_spanned_pages_in_node(int nid,
  1892. unsigned long zone_type,
  1893. unsigned long *ignored)
  1894. {
  1895. unsigned long node_start_pfn, node_end_pfn;
  1896. unsigned long zone_start_pfn, zone_end_pfn;
  1897. /* Get the start and end of the node and zone */
  1898. get_pfn_range_for_nid(nid, &node_start_pfn, &node_end_pfn);
  1899. zone_start_pfn = arch_zone_lowest_possible_pfn[zone_type];
  1900. zone_end_pfn = arch_zone_highest_possible_pfn[zone_type];
  1901. /* Check that this node has pages within the zone's required range */
  1902. if (zone_end_pfn < node_start_pfn || zone_start_pfn > node_end_pfn)
  1903. return 0;
  1904. /* Move the zone boundaries inside the node if necessary */
  1905. zone_end_pfn = min(zone_end_pfn, node_end_pfn);
  1906. zone_start_pfn = max(zone_start_pfn, node_start_pfn);
  1907. /* Return the spanned pages */
  1908. return zone_end_pfn - zone_start_pfn;
  1909. }
  1910. /*
  1911. * Return the number of holes in a range on a node. If nid is MAX_NUMNODES,
  1912. * then all holes in the requested range will be accounted for.
  1913. */
  1914. unsigned long __init __absent_pages_in_range(int nid,
  1915. unsigned long range_start_pfn,
  1916. unsigned long range_end_pfn)
  1917. {
  1918. int i = 0;
  1919. unsigned long prev_end_pfn = 0, hole_pages = 0;
  1920. unsigned long start_pfn;
  1921. /* Find the end_pfn of the first active range of pfns in the node */
  1922. i = first_active_region_index_in_nid(nid);
  1923. if (i == -1)
  1924. return 0;
  1925. /* Account for ranges before physical memory on this node */
  1926. if (early_node_map[i].start_pfn > range_start_pfn)
  1927. hole_pages = early_node_map[i].start_pfn - range_start_pfn;
  1928. prev_end_pfn = early_node_map[i].start_pfn;
  1929. /* Find all holes for the zone within the node */
  1930. for (; i != -1; i = next_active_region_index_in_nid(i, nid)) {
  1931. /* No need to continue if prev_end_pfn is outside the zone */
  1932. if (prev_end_pfn >= range_end_pfn)
  1933. break;
  1934. /* Make sure the end of the zone is not within the hole */
  1935. start_pfn = min(early_node_map[i].start_pfn, range_end_pfn);
  1936. prev_end_pfn = max(prev_end_pfn, range_start_pfn);
  1937. /* Update the hole size cound and move on */
  1938. if (start_pfn > range_start_pfn) {
  1939. BUG_ON(prev_end_pfn > start_pfn);
  1940. hole_pages += start_pfn - prev_end_pfn;
  1941. }
  1942. prev_end_pfn = early_node_map[i].end_pfn;
  1943. }
  1944. /* Account for ranges past physical memory on this node */
  1945. if (range_end_pfn > prev_end_pfn)
  1946. hole_pages = range_end_pfn -
  1947. max(range_start_pfn, prev_end_pfn);
  1948. return hole_pages;
  1949. }
  1950. /**
  1951. * absent_pages_in_range - Return number of page frames in holes within a range
  1952. * @start_pfn: The start PFN to start searching for holes
  1953. * @end_pfn: The end PFN to stop searching for holes
  1954. *
  1955. * It returns the number of pages frames in memory holes within a range.
  1956. */
  1957. unsigned long __init absent_pages_in_range(unsigned long start_pfn,
  1958. unsigned long end_pfn)
  1959. {
  1960. return __absent_pages_in_range(MAX_NUMNODES, start_pfn, end_pfn);
  1961. }
  1962. /* Return the number of page frames in holes in a zone on a node */
  1963. unsigned long __init zone_absent_pages_in_node(int nid,
  1964. unsigned long zone_type,
  1965. unsigned long *ignored)
  1966. {
  1967. unsigned long node_start_pfn, node_end_pfn;
  1968. unsigned long zone_start_pfn, zone_end_pfn;
  1969. get_pfn_range_for_nid(nid, &node_start_pfn, &node_end_pfn);
  1970. zone_start_pfn = max(arch_zone_lowest_possible_pfn[zone_type],
  1971. node_start_pfn);
  1972. zone_end_pfn = min(arch_zone_highest_possible_pfn[zone_type],
  1973. node_end_pfn);
  1974. return __absent_pages_in_range(nid, zone_start_pfn, zone_end_pfn);
  1975. }
  1976. #else
  1977. static inline unsigned long zone_spanned_pages_in_node(int nid,
  1978. unsigned long zone_type,
  1979. unsigned long *zones_size)
  1980. {
  1981. return zones_size[zone_type];
  1982. }
  1983. static inline unsigned long zone_absent_pages_in_node(int nid,
  1984. unsigned long zone_type,
  1985. unsigned long *zholes_size)
  1986. {
  1987. if (!zholes_size)
  1988. return 0;
  1989. return zholes_size[zone_type];
  1990. }
  1991. #endif
  1992. static void __init calculate_node_totalpages(struct pglist_data *pgdat,
  1993. unsigned long *zones_size, unsigned long *zholes_size)
  1994. {
  1995. unsigned long realtotalpages, totalpages = 0;
  1996. enum zone_type i;
  1997. for (i = 0; i < MAX_NR_ZONES; i++)
  1998. totalpages += zone_spanned_pages_in_node(pgdat->node_id, i,
  1999. zones_size);
  2000. pgdat->node_spanned_pages = totalpages;
  2001. realtotalpages = totalpages;
  2002. for (i = 0; i < MAX_NR_ZONES; i++)
  2003. realtotalpages -=
  2004. zone_absent_pages_in_node(pgdat->node_id, i,
  2005. zholes_size);
  2006. pgdat->node_present_pages = realtotalpages;
  2007. printk(KERN_DEBUG "On node %d totalpages: %lu\n", pgdat->node_id,
  2008. realtotalpages);
  2009. }
  2010. /*
  2011. * Set up the zone data structures:
  2012. * - mark all pages reserved
  2013. * - mark all memory queues empty
  2014. * - clear the memory bitmaps
  2015. */
  2016. static void __meminit free_area_init_core(struct pglist_data *pgdat,
  2017. unsigned long *zones_size, unsigned long *zholes_size)
  2018. {
  2019. enum zone_type j;
  2020. int nid = pgdat->node_id;
  2021. unsigned long zone_start_pfn = pgdat->node_start_pfn;
  2022. int ret;
  2023. pgdat_resize_init(pgdat);
  2024. pgdat->nr_zones = 0;
  2025. init_waitqueue_head(&pgdat->kswapd_wait);
  2026. pgdat->kswapd_max_order = 0;
  2027. for (j = 0; j < MAX_NR_ZONES; j++) {
  2028. struct zone *zone = pgdat->node_zones + j;
  2029. unsigned long size, realsize, memmap_pages;
  2030. size = zone_spanned_pages_in_node(nid, j, zones_size);
  2031. realsize = size - zone_absent_pages_in_node(nid, j,
  2032. zholes_size);
  2033. /*
  2034. * Adjust realsize so that it accounts for how much memory
  2035. * is used by this zone for memmap. This affects the watermark
  2036. * and per-cpu initialisations
  2037. */
  2038. memmap_pages = (size * sizeof(struct page)) >> PAGE_SHIFT;
  2039. if (realsize >= memmap_pages) {
  2040. realsize -= memmap_pages;
  2041. printk(KERN_DEBUG
  2042. " %s zone: %lu pages used for memmap\n",
  2043. zone_names[j], memmap_pages);
  2044. } else
  2045. printk(KERN_WARNING
  2046. " %s zone: %lu pages exceeds realsize %lu\n",
  2047. zone_names[j], memmap_pages, realsize);
  2048. /* Account for reserved DMA pages */
  2049. if (j == ZONE_DMA && realsize > dma_reserve) {
  2050. realsize -= dma_reserve;
  2051. printk(KERN_DEBUG " DMA zone: %lu pages reserved\n",
  2052. dma_reserve);
  2053. }
  2054. if (!is_highmem_idx(j))
  2055. nr_kernel_pages += realsize;
  2056. nr_all_pages += realsize;
  2057. zone->spanned_pages = size;
  2058. zone->present_pages = realsize;
  2059. #ifdef CONFIG_NUMA
  2060. zone->node = nid;
  2061. zone->min_unmapped_pages = (realsize*sysctl_min_unmapped_ratio)
  2062. / 100;
  2063. zone->min_slab_pages = (realsize * sysctl_min_slab_ratio) / 100;
  2064. #endif
  2065. zone->name = zone_names[j];
  2066. spin_lock_init(&zone->lock);
  2067. spin_lock_init(&zone->lru_lock);
  2068. zone_seqlock_init(zone);
  2069. zone->zone_pgdat = pgdat;
  2070. zone->free_pages = 0;
  2071. zone->temp_priority = zone->prev_priority = DEF_PRIORITY;
  2072. zone_pcp_init(zone);
  2073. INIT_LIST_HEAD(&zone->active_list);
  2074. INIT_LIST_HEAD(&zone->inactive_list);
  2075. zone->nr_scan_active = 0;
  2076. zone->nr_scan_inactive = 0;
  2077. zone->nr_active = 0;
  2078. zone->nr_inactive = 0;
  2079. zap_zone_vm_stats(zone);
  2080. atomic_set(&zone->reclaim_in_progress, 0);
  2081. if (!size)
  2082. continue;
  2083. zonetable_add(zone, nid, j, zone_start_pfn, size);
  2084. ret = init_currently_empty_zone(zone, zone_start_pfn, size);
  2085. BUG_ON(ret);
  2086. zone_start_pfn += size;
  2087. }
  2088. }
  2089. static void __init alloc_node_mem_map(struct pglist_data *pgdat)
  2090. {
  2091. /* Skip empty nodes */
  2092. if (!pgdat->node_spanned_pages)
  2093. return;
  2094. #ifdef CONFIG_FLAT_NODE_MEM_MAP
  2095. /* ia64 gets its own node_mem_map, before this, without bootmem */
  2096. if (!pgdat->node_mem_map) {
  2097. unsigned long size, start, end;
  2098. struct page *map;
  2099. /*
  2100. * The zone's endpoints aren't required to be MAX_ORDER
  2101. * aligned but the node_mem_map endpoints must be in order
  2102. * for the buddy allocator to function correctly.
  2103. */
  2104. start = pgdat->node_start_pfn & ~(MAX_ORDER_NR_PAGES - 1);
  2105. end = pgdat->node_start_pfn + pgdat->node_spanned_pages;
  2106. end = ALIGN(end, MAX_ORDER_NR_PAGES);
  2107. size = (end - start) * sizeof(struct page);
  2108. map = alloc_remap(pgdat->node_id, size);
  2109. if (!map)
  2110. map = alloc_bootmem_node(pgdat, size);
  2111. pgdat->node_mem_map = map + (pgdat->node_start_pfn - start);
  2112. }
  2113. #ifdef CONFIG_FLATMEM
  2114. /*
  2115. * With no DISCONTIG, the global mem_map is just set as node 0's
  2116. */
  2117. if (pgdat == NODE_DATA(0)) {
  2118. mem_map = NODE_DATA(0)->node_mem_map;
  2119. #ifdef CONFIG_ARCH_POPULATES_NODE_MAP
  2120. if (page_to_pfn(mem_map) != pgdat->node_start_pfn)
  2121. mem_map -= pgdat->node_start_pfn;
  2122. #endif /* CONFIG_ARCH_POPULATES_NODE_MAP */
  2123. }
  2124. #endif
  2125. #endif /* CONFIG_FLAT_NODE_MEM_MAP */
  2126. }
  2127. void __meminit free_area_init_node(int nid, struct pglist_data *pgdat,
  2128. unsigned long *zones_size, unsigned long node_start_pfn,
  2129. unsigned long *zholes_size)
  2130. {
  2131. pgdat->node_id = nid;
  2132. pgdat->node_start_pfn = node_start_pfn;
  2133. calculate_node_totalpages(pgdat, zones_size, zholes_size);
  2134. alloc_node_mem_map(pgdat);
  2135. free_area_init_core(pgdat, zones_size, zholes_size);
  2136. }
  2137. #ifdef CONFIG_ARCH_POPULATES_NODE_MAP
  2138. /**
  2139. * add_active_range - Register a range of PFNs backed by physical memory
  2140. * @nid: The node ID the range resides on
  2141. * @start_pfn: The start PFN of the available physical memory
  2142. * @end_pfn: The end PFN of the available physical memory
  2143. *
  2144. * These ranges are stored in an early_node_map[] and later used by
  2145. * free_area_init_nodes() to calculate zone sizes and holes. If the
  2146. * range spans a memory hole, it is up to the architecture to ensure
  2147. * the memory is not freed by the bootmem allocator. If possible
  2148. * the range being registered will be merged with existing ranges.
  2149. */
  2150. void __init add_active_range(unsigned int nid, unsigned long start_pfn,
  2151. unsigned long end_pfn)
  2152. {
  2153. int i;
  2154. printk(KERN_DEBUG "Entering add_active_range(%d, %lu, %lu) "
  2155. "%d entries of %d used\n",
  2156. nid, start_pfn, end_pfn,
  2157. nr_nodemap_entries, MAX_ACTIVE_REGIONS);
  2158. /* Merge with existing active regions if possible */
  2159. for (i = 0; i < nr_nodemap_entries; i++) {
  2160. if (early_node_map[i].nid != nid)
  2161. continue;
  2162. /* Skip if an existing region covers this new one */
  2163. if (start_pfn >= early_node_map[i].start_pfn &&
  2164. end_pfn <= early_node_map[i].end_pfn)
  2165. return;
  2166. /* Merge forward if suitable */
  2167. if (start_pfn <= early_node_map[i].end_pfn &&
  2168. end_pfn > early_node_map[i].end_pfn) {
  2169. early_node_map[i].end_pfn = end_pfn;
  2170. return;
  2171. }
  2172. /* Merge backward if suitable */
  2173. if (start_pfn < early_node_map[i].end_pfn &&
  2174. end_pfn >= early_node_map[i].start_pfn) {
  2175. early_node_map[i].start_pfn = start_pfn;
  2176. return;
  2177. }
  2178. }
  2179. /* Check that early_node_map is large enough */
  2180. if (i >= MAX_ACTIVE_REGIONS) {
  2181. printk(KERN_CRIT "More than %d memory regions, truncating\n",
  2182. MAX_ACTIVE_REGIONS);
  2183. return;
  2184. }
  2185. early_node_map[i].nid = nid;
  2186. early_node_map[i].start_pfn = start_pfn;
  2187. early_node_map[i].end_pfn = end_pfn;
  2188. nr_nodemap_entries = i + 1;
  2189. }
  2190. /**
  2191. * shrink_active_range - Shrink an existing registered range of PFNs
  2192. * @nid: The node id the range is on that should be shrunk
  2193. * @old_end_pfn: The old end PFN of the range
  2194. * @new_end_pfn: The new PFN of the range
  2195. *
  2196. * i386 with NUMA use alloc_remap() to store a node_mem_map on a local node.
  2197. * The map is kept at the end physical page range that has already been
  2198. * registered with add_active_range(). This function allows an arch to shrink
  2199. * an existing registered range.
  2200. */
  2201. void __init shrink_active_range(unsigned int nid, unsigned long old_end_pfn,
  2202. unsigned long new_end_pfn)
  2203. {
  2204. int i;
  2205. /* Find the old active region end and shrink */
  2206. for_each_active_range_index_in_nid(i, nid)
  2207. if (early_node_map[i].end_pfn == old_end_pfn) {
  2208. early_node_map[i].end_pfn = new_end_pfn;
  2209. break;
  2210. }
  2211. }
  2212. /**
  2213. * remove_all_active_ranges - Remove all currently registered regions
  2214. *
  2215. * During discovery, it may be found that a table like SRAT is invalid
  2216. * and an alternative discovery method must be used. This function removes
  2217. * all currently registered regions.
  2218. */
  2219. void __init remove_all_active_ranges(void)
  2220. {
  2221. memset(early_node_map, 0, sizeof(early_node_map));
  2222. nr_nodemap_entries = 0;
  2223. #ifdef CONFIG_MEMORY_HOTPLUG_RESERVE
  2224. memset(node_boundary_start_pfn, 0, sizeof(node_boundary_start_pfn));
  2225. memset(node_boundary_end_pfn, 0, sizeof(node_boundary_end_pfn));
  2226. #endif /* CONFIG_MEMORY_HOTPLUG_RESERVE */
  2227. }
  2228. /* Compare two active node_active_regions */
  2229. static int __init cmp_node_active_region(const void *a, const void *b)
  2230. {
  2231. struct node_active_region *arange = (struct node_active_region *)a;
  2232. struct node_active_region *brange = (struct node_active_region *)b;
  2233. /* Done this way to avoid overflows */
  2234. if (arange->start_pfn > brange->start_pfn)
  2235. return 1;
  2236. if (arange->start_pfn < brange->start_pfn)
  2237. return -1;
  2238. return 0;
  2239. }
  2240. /* sort the node_map by start_pfn */
  2241. static void __init sort_node_map(void)
  2242. {
  2243. sort(early_node_map, (size_t)nr_nodemap_entries,
  2244. sizeof(struct node_active_region),
  2245. cmp_node_active_region, NULL);
  2246. }
  2247. /* Find the lowest pfn for a node. This depends on a sorted early_node_map */
  2248. unsigned long __init find_min_pfn_for_node(unsigned long nid)
  2249. {
  2250. int i;
  2251. /* Assuming a sorted map, the first range found has the starting pfn */
  2252. for_each_active_range_index_in_nid(i, nid)
  2253. return early_node_map[i].start_pfn;
  2254. printk(KERN_WARNING "Could not find start_pfn for node %lu\n", nid);
  2255. return 0;
  2256. }
  2257. /**
  2258. * find_min_pfn_with_active_regions - Find the minimum PFN registered
  2259. *
  2260. * It returns the minimum PFN based on information provided via
  2261. * add_active_range().
  2262. */
  2263. unsigned long __init find_min_pfn_with_active_regions(void)
  2264. {
  2265. return find_min_pfn_for_node(MAX_NUMNODES);
  2266. }
  2267. /**
  2268. * find_max_pfn_with_active_regions - Find the maximum PFN registered
  2269. *
  2270. * It returns the maximum PFN based on information provided via
  2271. * add_active_range().
  2272. */
  2273. unsigned long __init find_max_pfn_with_active_regions(void)
  2274. {
  2275. int i;
  2276. unsigned long max_pfn = 0;
  2277. for (i = 0; i < nr_nodemap_entries; i++)
  2278. max_pfn = max(max_pfn, early_node_map[i].end_pfn);
  2279. return max_pfn;
  2280. }
  2281. /**
  2282. * free_area_init_nodes - Initialise all pg_data_t and zone data
  2283. * @max_zone_pfn: an array of max PFNs for each zone
  2284. *
  2285. * This will call free_area_init_node() for each active node in the system.
  2286. * Using the page ranges provided by add_active_range(), the size of each
  2287. * zone in each node and their holes is calculated. If the maximum PFN
  2288. * between two adjacent zones match, it is assumed that the zone is empty.
  2289. * For example, if arch_max_dma_pfn == arch_max_dma32_pfn, it is assumed
  2290. * that arch_max_dma32_pfn has no pages. It is also assumed that a zone
  2291. * starts where the previous one ended. For example, ZONE_DMA32 starts
  2292. * at arch_max_dma_pfn.
  2293. */
  2294. void __init free_area_init_nodes(unsigned long *max_zone_pfn)
  2295. {
  2296. unsigned long nid;
  2297. enum zone_type i;
  2298. /* Record where the zone boundaries are */
  2299. memset(arch_zone_lowest_possible_pfn, 0,
  2300. sizeof(arch_zone_lowest_possible_pfn));
  2301. memset(arch_zone_highest_possible_pfn, 0,
  2302. sizeof(arch_zone_highest_possible_pfn));
  2303. arch_zone_lowest_possible_pfn[0] = find_min_pfn_with_active_regions();
  2304. arch_zone_highest_possible_pfn[0] = max_zone_pfn[0];
  2305. for (i = 1; i < MAX_NR_ZONES; i++) {
  2306. arch_zone_lowest_possible_pfn[i] =
  2307. arch_zone_highest_possible_pfn[i-1];
  2308. arch_zone_highest_possible_pfn[i] =
  2309. max(max_zone_pfn[i], arch_zone_lowest_possible_pfn[i]);
  2310. }
  2311. /* Regions in the early_node_map can be in any order */
  2312. sort_node_map();
  2313. /* Print out the zone ranges */
  2314. printk("Zone PFN ranges:\n");
  2315. for (i = 0; i < MAX_NR_ZONES; i++)
  2316. printk(" %-8s %8lu -> %8lu\n",
  2317. zone_names[i],
  2318. arch_zone_lowest_possible_pfn[i],
  2319. arch_zone_highest_possible_pfn[i]);
  2320. /* Print out the early_node_map[] */
  2321. printk("early_node_map[%d] active PFN ranges\n", nr_nodemap_entries);
  2322. for (i = 0; i < nr_nodemap_entries; i++)
  2323. printk(" %3d: %8lu -> %8lu\n", early_node_map[i].nid,
  2324. early_node_map[i].start_pfn,
  2325. early_node_map[i].end_pfn);
  2326. /* Initialise every node */
  2327. for_each_online_node(nid) {
  2328. pg_data_t *pgdat = NODE_DATA(nid);
  2329. free_area_init_node(nid, pgdat, NULL,
  2330. find_min_pfn_for_node(nid), NULL);
  2331. }
  2332. }
  2333. #endif /* CONFIG_ARCH_POPULATES_NODE_MAP */
  2334. /**
  2335. * set_dma_reserve - set the specified number of pages reserved in the first zone
  2336. * @new_dma_reserve: The number of pages to mark reserved
  2337. *
  2338. * The per-cpu batchsize and zone watermarks are determined by present_pages.
  2339. * In the DMA zone, a significant percentage may be consumed by kernel image
  2340. * and other unfreeable allocations which can skew the watermarks badly. This
  2341. * function may optionally be used to account for unfreeable pages in the
  2342. * first zone (e.g., ZONE_DMA). The effect will be lower watermarks and
  2343. * smaller per-cpu batchsize.
  2344. */
  2345. void __init set_dma_reserve(unsigned long new_dma_reserve)
  2346. {
  2347. dma_reserve = new_dma_reserve;
  2348. }
  2349. #ifndef CONFIG_NEED_MULTIPLE_NODES
  2350. static bootmem_data_t contig_bootmem_data;
  2351. struct pglist_data contig_page_data = { .bdata = &contig_bootmem_data };
  2352. EXPORT_SYMBOL(contig_page_data);
  2353. #endif
  2354. void __init free_area_init(unsigned long *zones_size)
  2355. {
  2356. free_area_init_node(0, NODE_DATA(0), zones_size,
  2357. __pa(PAGE_OFFSET) >> PAGE_SHIFT, NULL);
  2358. }
  2359. #ifdef CONFIG_HOTPLUG_CPU
  2360. static int page_alloc_cpu_notify(struct notifier_block *self,
  2361. unsigned long action, void *hcpu)
  2362. {
  2363. int cpu = (unsigned long)hcpu;
  2364. if (action == CPU_DEAD) {
  2365. local_irq_disable();
  2366. __drain_pages(cpu);
  2367. vm_events_fold_cpu(cpu);
  2368. local_irq_enable();
  2369. refresh_cpu_vm_stats(cpu);
  2370. }
  2371. return NOTIFY_OK;
  2372. }
  2373. #endif /* CONFIG_HOTPLUG_CPU */
  2374. void __init page_alloc_init(void)
  2375. {
  2376. hotcpu_notifier(page_alloc_cpu_notify, 0);
  2377. }
  2378. /*
  2379. * calculate_totalreserve_pages - called when sysctl_lower_zone_reserve_ratio
  2380. * or min_free_kbytes changes.
  2381. */
  2382. static void calculate_totalreserve_pages(void)
  2383. {
  2384. struct pglist_data *pgdat;
  2385. unsigned long reserve_pages = 0;
  2386. enum zone_type i, j;
  2387. for_each_online_pgdat(pgdat) {
  2388. for (i = 0; i < MAX_NR_ZONES; i++) {
  2389. struct zone *zone = pgdat->node_zones + i;
  2390. unsigned long max = 0;
  2391. /* Find valid and maximum lowmem_reserve in the zone */
  2392. for (j = i; j < MAX_NR_ZONES; j++) {
  2393. if (zone->lowmem_reserve[j] > max)
  2394. max = zone->lowmem_reserve[j];
  2395. }
  2396. /* we treat pages_high as reserved pages. */
  2397. max += zone->pages_high;
  2398. if (max > zone->present_pages)
  2399. max = zone->present_pages;
  2400. reserve_pages += max;
  2401. }
  2402. }
  2403. totalreserve_pages = reserve_pages;
  2404. }
  2405. /*
  2406. * setup_per_zone_lowmem_reserve - called whenever
  2407. * sysctl_lower_zone_reserve_ratio changes. Ensures that each zone
  2408. * has a correct pages reserved value, so an adequate number of
  2409. * pages are left in the zone after a successful __alloc_pages().
  2410. */
  2411. static void setup_per_zone_lowmem_reserve(void)
  2412. {
  2413. struct pglist_data *pgdat;
  2414. enum zone_type j, idx;
  2415. for_each_online_pgdat(pgdat) {
  2416. for (j = 0; j < MAX_NR_ZONES; j++) {
  2417. struct zone *zone = pgdat->node_zones + j;
  2418. unsigned long present_pages = zone->present_pages;
  2419. zone->lowmem_reserve[j] = 0;
  2420. idx = j;
  2421. while (idx) {
  2422. struct zone *lower_zone;
  2423. idx--;
  2424. if (sysctl_lowmem_reserve_ratio[idx] < 1)
  2425. sysctl_lowmem_reserve_ratio[idx] = 1;
  2426. lower_zone = pgdat->node_zones + idx;
  2427. lower_zone->lowmem_reserve[j] = present_pages /
  2428. sysctl_lowmem_reserve_ratio[idx];
  2429. present_pages += lower_zone->present_pages;
  2430. }
  2431. }
  2432. }
  2433. /* update totalreserve_pages */
  2434. calculate_totalreserve_pages();
  2435. }
  2436. /**
  2437. * setup_per_zone_pages_min - called when min_free_kbytes changes.
  2438. *
  2439. * Ensures that the pages_{min,low,high} values for each zone are set correctly
  2440. * with respect to min_free_kbytes.
  2441. */
  2442. void setup_per_zone_pages_min(void)
  2443. {
  2444. unsigned long pages_min = min_free_kbytes >> (PAGE_SHIFT - 10);
  2445. unsigned long lowmem_pages = 0;
  2446. struct zone *zone;
  2447. unsigned long flags;
  2448. /* Calculate total number of !ZONE_HIGHMEM pages */
  2449. for_each_zone(zone) {
  2450. if (!is_highmem(zone))
  2451. lowmem_pages += zone->present_pages;
  2452. }
  2453. for_each_zone(zone) {
  2454. u64 tmp;
  2455. spin_lock_irqsave(&zone->lru_lock, flags);
  2456. tmp = (u64)pages_min * zone->present_pages;
  2457. do_div(tmp, lowmem_pages);
  2458. if (is_highmem(zone)) {
  2459. /*
  2460. * __GFP_HIGH and PF_MEMALLOC allocations usually don't
  2461. * need highmem pages, so cap pages_min to a small
  2462. * value here.
  2463. *
  2464. * The (pages_high-pages_low) and (pages_low-pages_min)
  2465. * deltas controls asynch page reclaim, and so should
  2466. * not be capped for highmem.
  2467. */
  2468. int min_pages;
  2469. min_pages = zone->present_pages / 1024;
  2470. if (min_pages < SWAP_CLUSTER_MAX)
  2471. min_pages = SWAP_CLUSTER_MAX;
  2472. if (min_pages > 128)
  2473. min_pages = 128;
  2474. zone->pages_min = min_pages;
  2475. } else {
  2476. /*
  2477. * If it's a lowmem zone, reserve a number of pages
  2478. * proportionate to the zone's size.
  2479. */
  2480. zone->pages_min = tmp;
  2481. }
  2482. zone->pages_low = zone->pages_min + (tmp >> 2);
  2483. zone->pages_high = zone->pages_min + (tmp >> 1);
  2484. spin_unlock_irqrestore(&zone->lru_lock, flags);
  2485. }
  2486. /* update totalreserve_pages */
  2487. calculate_totalreserve_pages();
  2488. }
  2489. /*
  2490. * Initialise min_free_kbytes.
  2491. *
  2492. * For small machines we want it small (128k min). For large machines
  2493. * we want it large (64MB max). But it is not linear, because network
  2494. * bandwidth does not increase linearly with machine size. We use
  2495. *
  2496. * min_free_kbytes = 4 * sqrt(lowmem_kbytes), for better accuracy:
  2497. * min_free_kbytes = sqrt(lowmem_kbytes * 16)
  2498. *
  2499. * which yields
  2500. *
  2501. * 16MB: 512k
  2502. * 32MB: 724k
  2503. * 64MB: 1024k
  2504. * 128MB: 1448k
  2505. * 256MB: 2048k
  2506. * 512MB: 2896k
  2507. * 1024MB: 4096k
  2508. * 2048MB: 5792k
  2509. * 4096MB: 8192k
  2510. * 8192MB: 11584k
  2511. * 16384MB: 16384k
  2512. */
  2513. static int __init init_per_zone_pages_min(void)
  2514. {
  2515. unsigned long lowmem_kbytes;
  2516. lowmem_kbytes = nr_free_buffer_pages() * (PAGE_SIZE >> 10);
  2517. min_free_kbytes = int_sqrt(lowmem_kbytes * 16);
  2518. if (min_free_kbytes < 128)
  2519. min_free_kbytes = 128;
  2520. if (min_free_kbytes > 65536)
  2521. min_free_kbytes = 65536;
  2522. setup_per_zone_pages_min();
  2523. setup_per_zone_lowmem_reserve();
  2524. return 0;
  2525. }
  2526. module_init(init_per_zone_pages_min)
  2527. /*
  2528. * min_free_kbytes_sysctl_handler - just a wrapper around proc_dointvec() so
  2529. * that we can call two helper functions whenever min_free_kbytes
  2530. * changes.
  2531. */
  2532. int min_free_kbytes_sysctl_handler(ctl_table *table, int write,
  2533. struct file *file, void __user *buffer, size_t *length, loff_t *ppos)
  2534. {
  2535. proc_dointvec(table, write, file, buffer, length, ppos);
  2536. setup_per_zone_pages_min();
  2537. return 0;
  2538. }
  2539. #ifdef CONFIG_NUMA
  2540. int sysctl_min_unmapped_ratio_sysctl_handler(ctl_table *table, int write,
  2541. struct file *file, void __user *buffer, size_t *length, loff_t *ppos)
  2542. {
  2543. struct zone *zone;
  2544. int rc;
  2545. rc = proc_dointvec_minmax(table, write, file, buffer, length, ppos);
  2546. if (rc)
  2547. return rc;
  2548. for_each_zone(zone)
  2549. zone->min_unmapped_pages = (zone->present_pages *
  2550. sysctl_min_unmapped_ratio) / 100;
  2551. return 0;
  2552. }
  2553. int sysctl_min_slab_ratio_sysctl_handler(ctl_table *table, int write,
  2554. struct file *file, void __user *buffer, size_t *length, loff_t *ppos)
  2555. {
  2556. struct zone *zone;
  2557. int rc;
  2558. rc = proc_dointvec_minmax(table, write, file, buffer, length, ppos);
  2559. if (rc)
  2560. return rc;
  2561. for_each_zone(zone)
  2562. zone->min_slab_pages = (zone->present_pages *
  2563. sysctl_min_slab_ratio) / 100;
  2564. return 0;
  2565. }
  2566. #endif
  2567. /*
  2568. * lowmem_reserve_ratio_sysctl_handler - just a wrapper around
  2569. * proc_dointvec() so that we can call setup_per_zone_lowmem_reserve()
  2570. * whenever sysctl_lowmem_reserve_ratio changes.
  2571. *
  2572. * The reserve ratio obviously has absolutely no relation with the
  2573. * pages_min watermarks. The lowmem reserve ratio can only make sense
  2574. * if in function of the boot time zone sizes.
  2575. */
  2576. int lowmem_reserve_ratio_sysctl_handler(ctl_table *table, int write,
  2577. struct file *file, void __user *buffer, size_t *length, loff_t *ppos)
  2578. {
  2579. proc_dointvec_minmax(table, write, file, buffer, length, ppos);
  2580. setup_per_zone_lowmem_reserve();
  2581. return 0;
  2582. }
  2583. /*
  2584. * percpu_pagelist_fraction - changes the pcp->high for each zone on each
  2585. * cpu. It is the fraction of total pages in each zone that a hot per cpu pagelist
  2586. * can have before it gets flushed back to buddy allocator.
  2587. */
  2588. int percpu_pagelist_fraction_sysctl_handler(ctl_table *table, int write,
  2589. struct file *file, void __user *buffer, size_t *length, loff_t *ppos)
  2590. {
  2591. struct zone *zone;
  2592. unsigned int cpu;
  2593. int ret;
  2594. ret = proc_dointvec_minmax(table, write, file, buffer, length, ppos);
  2595. if (!write || (ret == -EINVAL))
  2596. return ret;
  2597. for_each_zone(zone) {
  2598. for_each_online_cpu(cpu) {
  2599. unsigned long high;
  2600. high = zone->present_pages / percpu_pagelist_fraction;
  2601. setup_pagelist_highmark(zone_pcp(zone, cpu), high);
  2602. }
  2603. }
  2604. return 0;
  2605. }
  2606. int hashdist = HASHDIST_DEFAULT;
  2607. #ifdef CONFIG_NUMA
  2608. static int __init set_hashdist(char *str)
  2609. {
  2610. if (!str)
  2611. return 0;
  2612. hashdist = simple_strtoul(str, &str, 0);
  2613. return 1;
  2614. }
  2615. __setup("hashdist=", set_hashdist);
  2616. #endif
  2617. /*
  2618. * allocate a large system hash table from bootmem
  2619. * - it is assumed that the hash table must contain an exact power-of-2
  2620. * quantity of entries
  2621. * - limit is the number of hash buckets, not the total allocation size
  2622. */
  2623. void *__init alloc_large_system_hash(const char *tablename,
  2624. unsigned long bucketsize,
  2625. unsigned long numentries,
  2626. int scale,
  2627. int flags,
  2628. unsigned int *_hash_shift,
  2629. unsigned int *_hash_mask,
  2630. unsigned long limit)
  2631. {
  2632. unsigned long long max = limit;
  2633. unsigned long log2qty, size;
  2634. void *table = NULL;
  2635. /* allow the kernel cmdline to have a say */
  2636. if (!numentries) {
  2637. /* round applicable memory size up to nearest megabyte */
  2638. numentries = (flags & HASH_HIGHMEM) ? nr_all_pages : nr_kernel_pages;
  2639. numentries += (1UL << (20 - PAGE_SHIFT)) - 1;
  2640. numentries >>= 20 - PAGE_SHIFT;
  2641. numentries <<= 20 - PAGE_SHIFT;
  2642. /* limit to 1 bucket per 2^scale bytes of low memory */
  2643. if (scale > PAGE_SHIFT)
  2644. numentries >>= (scale - PAGE_SHIFT);
  2645. else
  2646. numentries <<= (PAGE_SHIFT - scale);
  2647. }
  2648. numentries = roundup_pow_of_two(numentries);
  2649. /* limit allocation size to 1/16 total memory by default */
  2650. if (max == 0) {
  2651. max = ((unsigned long long)nr_all_pages << PAGE_SHIFT) >> 4;
  2652. do_div(max, bucketsize);
  2653. }
  2654. if (numentries > max)
  2655. numentries = max;
  2656. log2qty = long_log2(numentries);
  2657. do {
  2658. size = bucketsize << log2qty;
  2659. if (flags & HASH_EARLY)
  2660. table = alloc_bootmem(size);
  2661. else if (hashdist)
  2662. table = __vmalloc(size, GFP_ATOMIC, PAGE_KERNEL);
  2663. else {
  2664. unsigned long order;
  2665. for (order = 0; ((1UL << order) << PAGE_SHIFT) < size; order++)
  2666. ;
  2667. table = (void*) __get_free_pages(GFP_ATOMIC, order);
  2668. }
  2669. } while (!table && size > PAGE_SIZE && --log2qty);
  2670. if (!table)
  2671. panic("Failed to allocate %s hash table\n", tablename);
  2672. printk("%s hash table entries: %d (order: %d, %lu bytes)\n",
  2673. tablename,
  2674. (1U << log2qty),
  2675. long_log2(size) - PAGE_SHIFT,
  2676. size);
  2677. if (_hash_shift)
  2678. *_hash_shift = log2qty;
  2679. if (_hash_mask)
  2680. *_hash_mask = (1 << log2qty) - 1;
  2681. return table;
  2682. }
  2683. #ifdef CONFIG_OUT_OF_LINE_PFN_TO_PAGE
  2684. struct page *pfn_to_page(unsigned long pfn)
  2685. {
  2686. return __pfn_to_page(pfn);
  2687. }
  2688. unsigned long page_to_pfn(struct page *page)
  2689. {
  2690. return __page_to_pfn(page);
  2691. }
  2692. EXPORT_SYMBOL(pfn_to_page);
  2693. EXPORT_SYMBOL(page_to_pfn);
  2694. #endif /* CONFIG_OUT_OF_LINE_PFN_TO_PAGE */