page_alloc.c 91 KB

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