page_alloc.c 134 KB

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