page_alloc.c 98 KB

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