page_alloc.c 151 KB

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