page_alloc.c 115 KB

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