page_alloc.c 91 KB

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