page_alloc.c 159 KB

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