page_alloc.c 91 KB

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