page_alloc.c 152 KB

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