page_alloc.c 122 KB

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