page_alloc.c 130 KB

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