page_alloc.c 159 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817
  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. int err;
  1220. err = init_fault_attr_dentries(&fail_page_alloc.attr,
  1221. "fail_page_alloc");
  1222. if (err)
  1223. return err;
  1224. dir = fail_page_alloc.attr.dir;
  1225. if (!debugfs_create_bool("ignore-gfp-wait", mode, dir,
  1226. &fail_page_alloc.ignore_gfp_wait))
  1227. goto fail;
  1228. if (!debugfs_create_bool("ignore-gfp-highmem", mode, dir,
  1229. &fail_page_alloc.ignore_gfp_highmem))
  1230. goto fail;
  1231. if (!debugfs_create_u32("min-order", mode, dir,
  1232. &fail_page_alloc.min_order))
  1233. goto fail;
  1234. return 0;
  1235. fail:
  1236. cleanup_fault_attr_dentries(&fail_page_alloc.attr);
  1237. return -ENOMEM;
  1238. }
  1239. late_initcall(fail_page_alloc_debugfs);
  1240. #endif /* CONFIG_FAULT_INJECTION_DEBUG_FS */
  1241. #else /* CONFIG_FAIL_PAGE_ALLOC */
  1242. static inline int should_fail_alloc_page(gfp_t gfp_mask, unsigned int order)
  1243. {
  1244. return 0;
  1245. }
  1246. #endif /* CONFIG_FAIL_PAGE_ALLOC */
  1247. /*
  1248. * Return true if free pages are above 'mark'. This takes into account the order
  1249. * of the allocation.
  1250. */
  1251. static bool __zone_watermark_ok(struct zone *z, int order, unsigned long mark,
  1252. int classzone_idx, int alloc_flags, long free_pages)
  1253. {
  1254. /* free_pages my go negative - that's OK */
  1255. long min = mark;
  1256. int o;
  1257. free_pages -= (1 << order) + 1;
  1258. if (alloc_flags & ALLOC_HIGH)
  1259. min -= min / 2;
  1260. if (alloc_flags & ALLOC_HARDER)
  1261. min -= min / 4;
  1262. if (free_pages <= min + z->lowmem_reserve[classzone_idx])
  1263. return false;
  1264. for (o = 0; o < order; o++) {
  1265. /* At the next order, this order's pages become unavailable */
  1266. free_pages -= z->free_area[o].nr_free << o;
  1267. /* Require fewer higher order pages to be free */
  1268. min >>= 1;
  1269. if (free_pages <= min)
  1270. return false;
  1271. }
  1272. return true;
  1273. }
  1274. bool zone_watermark_ok(struct zone *z, int order, unsigned long mark,
  1275. int classzone_idx, int alloc_flags)
  1276. {
  1277. return __zone_watermark_ok(z, order, mark, classzone_idx, alloc_flags,
  1278. zone_page_state(z, NR_FREE_PAGES));
  1279. }
  1280. bool zone_watermark_ok_safe(struct zone *z, int order, unsigned long mark,
  1281. int classzone_idx, int alloc_flags)
  1282. {
  1283. long free_pages = zone_page_state(z, NR_FREE_PAGES);
  1284. if (z->percpu_drift_mark && free_pages < z->percpu_drift_mark)
  1285. free_pages = zone_page_state_snapshot(z, NR_FREE_PAGES);
  1286. return __zone_watermark_ok(z, order, mark, classzone_idx, alloc_flags,
  1287. free_pages);
  1288. }
  1289. #ifdef CONFIG_NUMA
  1290. /*
  1291. * zlc_setup - Setup for "zonelist cache". Uses cached zone data to
  1292. * skip over zones that are not allowed by the cpuset, or that have
  1293. * been recently (in last second) found to be nearly full. See further
  1294. * comments in mmzone.h. Reduces cache footprint of zonelist scans
  1295. * that have to skip over a lot of full or unallowed zones.
  1296. *
  1297. * If the zonelist cache is present in the passed in zonelist, then
  1298. * returns a pointer to the allowed node mask (either the current
  1299. * tasks mems_allowed, or node_states[N_HIGH_MEMORY].)
  1300. *
  1301. * If the zonelist cache is not available for this zonelist, does
  1302. * nothing and returns NULL.
  1303. *
  1304. * If the fullzones BITMAP in the zonelist cache is stale (more than
  1305. * a second since last zap'd) then we zap it out (clear its bits.)
  1306. *
  1307. * We hold off even calling zlc_setup, until after we've checked the
  1308. * first zone in the zonelist, on the theory that most allocations will
  1309. * be satisfied from that first zone, so best to examine that zone as
  1310. * quickly as we can.
  1311. */
  1312. static nodemask_t *zlc_setup(struct zonelist *zonelist, int alloc_flags)
  1313. {
  1314. struct zonelist_cache *zlc; /* cached zonelist speedup info */
  1315. nodemask_t *allowednodes; /* zonelist_cache approximation */
  1316. zlc = zonelist->zlcache_ptr;
  1317. if (!zlc)
  1318. return NULL;
  1319. if (time_after(jiffies, zlc->last_full_zap + HZ)) {
  1320. bitmap_zero(zlc->fullzones, MAX_ZONES_PER_ZONELIST);
  1321. zlc->last_full_zap = jiffies;
  1322. }
  1323. allowednodes = !in_interrupt() && (alloc_flags & ALLOC_CPUSET) ?
  1324. &cpuset_current_mems_allowed :
  1325. &node_states[N_HIGH_MEMORY];
  1326. return allowednodes;
  1327. }
  1328. /*
  1329. * Given 'z' scanning a zonelist, run a couple of quick checks to see
  1330. * if it is worth looking at further for free memory:
  1331. * 1) Check that the zone isn't thought to be full (doesn't have its
  1332. * bit set in the zonelist_cache fullzones BITMAP).
  1333. * 2) Check that the zones node (obtained from the zonelist_cache
  1334. * z_to_n[] mapping) is allowed in the passed in allowednodes mask.
  1335. * Return true (non-zero) if zone is worth looking at further, or
  1336. * else return false (zero) if it is not.
  1337. *
  1338. * This check -ignores- the distinction between various watermarks,
  1339. * such as GFP_HIGH, GFP_ATOMIC, PF_MEMALLOC, ... If a zone is
  1340. * found to be full for any variation of these watermarks, it will
  1341. * be considered full for up to one second by all requests, unless
  1342. * we are so low on memory on all allowed nodes that we are forced
  1343. * into the second scan of the zonelist.
  1344. *
  1345. * In the second scan we ignore this zonelist cache and exactly
  1346. * apply the watermarks to all zones, even it is slower to do so.
  1347. * We are low on memory in the second scan, and should leave no stone
  1348. * unturned looking for a free page.
  1349. */
  1350. static int zlc_zone_worth_trying(struct zonelist *zonelist, struct zoneref *z,
  1351. nodemask_t *allowednodes)
  1352. {
  1353. struct zonelist_cache *zlc; /* cached zonelist speedup info */
  1354. int i; /* index of *z in zonelist zones */
  1355. int n; /* node that zone *z is on */
  1356. zlc = zonelist->zlcache_ptr;
  1357. if (!zlc)
  1358. return 1;
  1359. i = z - zonelist->_zonerefs;
  1360. n = zlc->z_to_n[i];
  1361. /* This zone is worth trying if it is allowed but not full */
  1362. return node_isset(n, *allowednodes) && !test_bit(i, zlc->fullzones);
  1363. }
  1364. /*
  1365. * Given 'z' scanning a zonelist, set the corresponding bit in
  1366. * zlc->fullzones, so that subsequent attempts to allocate a page
  1367. * from that zone don't waste time re-examining it.
  1368. */
  1369. static void zlc_mark_zone_full(struct zonelist *zonelist, struct zoneref *z)
  1370. {
  1371. struct zonelist_cache *zlc; /* cached zonelist speedup info */
  1372. int i; /* index of *z in zonelist zones */
  1373. zlc = zonelist->zlcache_ptr;
  1374. if (!zlc)
  1375. return;
  1376. i = z - zonelist->_zonerefs;
  1377. set_bit(i, zlc->fullzones);
  1378. }
  1379. /*
  1380. * clear all zones full, called after direct reclaim makes progress so that
  1381. * a zone that was recently full is not skipped over for up to a second
  1382. */
  1383. static void zlc_clear_zones_full(struct zonelist *zonelist)
  1384. {
  1385. struct zonelist_cache *zlc; /* cached zonelist speedup info */
  1386. zlc = zonelist->zlcache_ptr;
  1387. if (!zlc)
  1388. return;
  1389. bitmap_zero(zlc->fullzones, MAX_ZONES_PER_ZONELIST);
  1390. }
  1391. #else /* CONFIG_NUMA */
  1392. static nodemask_t *zlc_setup(struct zonelist *zonelist, int alloc_flags)
  1393. {
  1394. return NULL;
  1395. }
  1396. static int zlc_zone_worth_trying(struct zonelist *zonelist, struct zoneref *z,
  1397. nodemask_t *allowednodes)
  1398. {
  1399. return 1;
  1400. }
  1401. static void zlc_mark_zone_full(struct zonelist *zonelist, struct zoneref *z)
  1402. {
  1403. }
  1404. static void zlc_clear_zones_full(struct zonelist *zonelist)
  1405. {
  1406. }
  1407. #endif /* CONFIG_NUMA */
  1408. /*
  1409. * get_page_from_freelist goes through the zonelist trying to allocate
  1410. * a page.
  1411. */
  1412. static struct page *
  1413. get_page_from_freelist(gfp_t gfp_mask, nodemask_t *nodemask, unsigned int order,
  1414. struct zonelist *zonelist, int high_zoneidx, int alloc_flags,
  1415. struct zone *preferred_zone, int migratetype)
  1416. {
  1417. struct zoneref *z;
  1418. struct page *page = NULL;
  1419. int classzone_idx;
  1420. struct zone *zone;
  1421. nodemask_t *allowednodes = NULL;/* zonelist_cache approximation */
  1422. int zlc_active = 0; /* set if using zonelist_cache */
  1423. int did_zlc_setup = 0; /* just call zlc_setup() one time */
  1424. classzone_idx = zone_idx(preferred_zone);
  1425. zonelist_scan:
  1426. /*
  1427. * Scan zonelist, looking for a zone with enough free.
  1428. * See also cpuset_zone_allowed() comment in kernel/cpuset.c.
  1429. */
  1430. for_each_zone_zonelist_nodemask(zone, z, zonelist,
  1431. high_zoneidx, nodemask) {
  1432. if (NUMA_BUILD && zlc_active &&
  1433. !zlc_zone_worth_trying(zonelist, z, allowednodes))
  1434. continue;
  1435. if ((alloc_flags & ALLOC_CPUSET) &&
  1436. !cpuset_zone_allowed_softwall(zone, gfp_mask))
  1437. continue;
  1438. BUILD_BUG_ON(ALLOC_NO_WATERMARKS < NR_WMARK);
  1439. if (!(alloc_flags & ALLOC_NO_WATERMARKS)) {
  1440. unsigned long mark;
  1441. int ret;
  1442. mark = zone->watermark[alloc_flags & ALLOC_WMARK_MASK];
  1443. if (zone_watermark_ok(zone, order, mark,
  1444. classzone_idx, alloc_flags))
  1445. goto try_this_zone;
  1446. if (NUMA_BUILD && !did_zlc_setup && nr_online_nodes > 1) {
  1447. /*
  1448. * we do zlc_setup if there are multiple nodes
  1449. * and before considering the first zone allowed
  1450. * by the cpuset.
  1451. */
  1452. allowednodes = zlc_setup(zonelist, alloc_flags);
  1453. zlc_active = 1;
  1454. did_zlc_setup = 1;
  1455. }
  1456. if (zone_reclaim_mode == 0)
  1457. goto this_zone_full;
  1458. /*
  1459. * As we may have just activated ZLC, check if the first
  1460. * eligible zone has failed zone_reclaim recently.
  1461. */
  1462. if (NUMA_BUILD && zlc_active &&
  1463. !zlc_zone_worth_trying(zonelist, z, allowednodes))
  1464. continue;
  1465. ret = zone_reclaim(zone, gfp_mask, order);
  1466. switch (ret) {
  1467. case ZONE_RECLAIM_NOSCAN:
  1468. /* did not scan */
  1469. continue;
  1470. case ZONE_RECLAIM_FULL:
  1471. /* scanned but unreclaimable */
  1472. continue;
  1473. default:
  1474. /* did we reclaim enough */
  1475. if (!zone_watermark_ok(zone, order, mark,
  1476. classzone_idx, alloc_flags))
  1477. goto this_zone_full;
  1478. }
  1479. }
  1480. try_this_zone:
  1481. page = buffered_rmqueue(preferred_zone, zone, order,
  1482. gfp_mask, migratetype);
  1483. if (page)
  1484. break;
  1485. this_zone_full:
  1486. if (NUMA_BUILD)
  1487. zlc_mark_zone_full(zonelist, z);
  1488. }
  1489. if (unlikely(NUMA_BUILD && page == NULL && zlc_active)) {
  1490. /* Disable zlc cache for second zonelist scan */
  1491. zlc_active = 0;
  1492. goto zonelist_scan;
  1493. }
  1494. return page;
  1495. }
  1496. /*
  1497. * Large machines with many possible nodes should not always dump per-node
  1498. * meminfo in irq context.
  1499. */
  1500. static inline bool should_suppress_show_mem(void)
  1501. {
  1502. bool ret = false;
  1503. #if NODES_SHIFT > 8
  1504. ret = in_interrupt();
  1505. #endif
  1506. return ret;
  1507. }
  1508. static DEFINE_RATELIMIT_STATE(nopage_rs,
  1509. DEFAULT_RATELIMIT_INTERVAL,
  1510. DEFAULT_RATELIMIT_BURST);
  1511. void warn_alloc_failed(gfp_t gfp_mask, int order, const char *fmt, ...)
  1512. {
  1513. va_list args;
  1514. unsigned int filter = SHOW_MEM_FILTER_NODES;
  1515. if ((gfp_mask & __GFP_NOWARN) || !__ratelimit(&nopage_rs))
  1516. return;
  1517. /*
  1518. * This documents exceptions given to allocations in certain
  1519. * contexts that are allowed to allocate outside current's set
  1520. * of allowed nodes.
  1521. */
  1522. if (!(gfp_mask & __GFP_NOMEMALLOC))
  1523. if (test_thread_flag(TIF_MEMDIE) ||
  1524. (current->flags & (PF_MEMALLOC | PF_EXITING)))
  1525. filter &= ~SHOW_MEM_FILTER_NODES;
  1526. if (in_interrupt() || !(gfp_mask & __GFP_WAIT))
  1527. filter &= ~SHOW_MEM_FILTER_NODES;
  1528. if (fmt) {
  1529. printk(KERN_WARNING);
  1530. va_start(args, fmt);
  1531. vprintk(fmt, args);
  1532. va_end(args);
  1533. }
  1534. pr_warning("%s: page allocation failure: order:%d, mode:0x%x\n",
  1535. current->comm, order, gfp_mask);
  1536. dump_stack();
  1537. if (!should_suppress_show_mem())
  1538. show_mem(filter);
  1539. }
  1540. static inline int
  1541. should_alloc_retry(gfp_t gfp_mask, unsigned int order,
  1542. unsigned long pages_reclaimed)
  1543. {
  1544. /* Do not loop if specifically requested */
  1545. if (gfp_mask & __GFP_NORETRY)
  1546. return 0;
  1547. /*
  1548. * In this implementation, order <= PAGE_ALLOC_COSTLY_ORDER
  1549. * means __GFP_NOFAIL, but that may not be true in other
  1550. * implementations.
  1551. */
  1552. if (order <= PAGE_ALLOC_COSTLY_ORDER)
  1553. return 1;
  1554. /*
  1555. * For order > PAGE_ALLOC_COSTLY_ORDER, if __GFP_REPEAT is
  1556. * specified, then we retry until we no longer reclaim any pages
  1557. * (above), or we've reclaimed an order of pages at least as
  1558. * large as the allocation's order. In both cases, if the
  1559. * allocation still fails, we stop retrying.
  1560. */
  1561. if (gfp_mask & __GFP_REPEAT && pages_reclaimed < (1 << order))
  1562. return 1;
  1563. /*
  1564. * Don't let big-order allocations loop unless the caller
  1565. * explicitly requests that.
  1566. */
  1567. if (gfp_mask & __GFP_NOFAIL)
  1568. return 1;
  1569. return 0;
  1570. }
  1571. static inline struct page *
  1572. __alloc_pages_may_oom(gfp_t gfp_mask, unsigned int order,
  1573. struct zonelist *zonelist, enum zone_type high_zoneidx,
  1574. nodemask_t *nodemask, struct zone *preferred_zone,
  1575. int migratetype)
  1576. {
  1577. struct page *page;
  1578. /* Acquire the OOM killer lock for the zones in zonelist */
  1579. if (!try_set_zonelist_oom(zonelist, gfp_mask)) {
  1580. schedule_timeout_uninterruptible(1);
  1581. return NULL;
  1582. }
  1583. /*
  1584. * Go through the zonelist yet one more time, keep very high watermark
  1585. * here, this is only to catch a parallel oom killing, we must fail if
  1586. * we're still under heavy pressure.
  1587. */
  1588. page = get_page_from_freelist(gfp_mask|__GFP_HARDWALL, nodemask,
  1589. order, zonelist, high_zoneidx,
  1590. ALLOC_WMARK_HIGH|ALLOC_CPUSET,
  1591. preferred_zone, migratetype);
  1592. if (page)
  1593. goto out;
  1594. if (!(gfp_mask & __GFP_NOFAIL)) {
  1595. /* The OOM killer will not help higher order allocs */
  1596. if (order > PAGE_ALLOC_COSTLY_ORDER)
  1597. goto out;
  1598. /* The OOM killer does not needlessly kill tasks for lowmem */
  1599. if (high_zoneidx < ZONE_NORMAL)
  1600. goto out;
  1601. /*
  1602. * GFP_THISNODE contains __GFP_NORETRY and we never hit this.
  1603. * Sanity check for bare calls of __GFP_THISNODE, not real OOM.
  1604. * The caller should handle page allocation failure by itself if
  1605. * it specifies __GFP_THISNODE.
  1606. * Note: Hugepage uses it but will hit PAGE_ALLOC_COSTLY_ORDER.
  1607. */
  1608. if (gfp_mask & __GFP_THISNODE)
  1609. goto out;
  1610. }
  1611. /* Exhausted what can be done so it's blamo time */
  1612. out_of_memory(zonelist, gfp_mask, order, nodemask);
  1613. out:
  1614. clear_zonelist_oom(zonelist, gfp_mask);
  1615. return page;
  1616. }
  1617. #ifdef CONFIG_COMPACTION
  1618. /* Try memory compaction for high-order allocations before reclaim */
  1619. static struct page *
  1620. __alloc_pages_direct_compact(gfp_t gfp_mask, unsigned int order,
  1621. struct zonelist *zonelist, enum zone_type high_zoneidx,
  1622. nodemask_t *nodemask, int alloc_flags, struct zone *preferred_zone,
  1623. int migratetype, unsigned long *did_some_progress,
  1624. bool sync_migration)
  1625. {
  1626. struct page *page;
  1627. if (!order || compaction_deferred(preferred_zone))
  1628. return NULL;
  1629. current->flags |= PF_MEMALLOC;
  1630. *did_some_progress = try_to_compact_pages(zonelist, order, gfp_mask,
  1631. nodemask, sync_migration);
  1632. current->flags &= ~PF_MEMALLOC;
  1633. if (*did_some_progress != COMPACT_SKIPPED) {
  1634. /* Page migration frees to the PCP lists but we want merging */
  1635. drain_pages(get_cpu());
  1636. put_cpu();
  1637. page = get_page_from_freelist(gfp_mask, nodemask,
  1638. order, zonelist, high_zoneidx,
  1639. alloc_flags, preferred_zone,
  1640. migratetype);
  1641. if (page) {
  1642. preferred_zone->compact_considered = 0;
  1643. preferred_zone->compact_defer_shift = 0;
  1644. count_vm_event(COMPACTSUCCESS);
  1645. return page;
  1646. }
  1647. /*
  1648. * It's bad if compaction run occurs and fails.
  1649. * The most likely reason is that pages exist,
  1650. * but not enough to satisfy watermarks.
  1651. */
  1652. count_vm_event(COMPACTFAIL);
  1653. defer_compaction(preferred_zone);
  1654. cond_resched();
  1655. }
  1656. return NULL;
  1657. }
  1658. #else
  1659. static inline struct page *
  1660. __alloc_pages_direct_compact(gfp_t gfp_mask, unsigned int order,
  1661. struct zonelist *zonelist, enum zone_type high_zoneidx,
  1662. nodemask_t *nodemask, int alloc_flags, struct zone *preferred_zone,
  1663. int migratetype, unsigned long *did_some_progress,
  1664. bool sync_migration)
  1665. {
  1666. return NULL;
  1667. }
  1668. #endif /* CONFIG_COMPACTION */
  1669. /* The really slow allocator path where we enter direct reclaim */
  1670. static inline struct page *
  1671. __alloc_pages_direct_reclaim(gfp_t gfp_mask, unsigned int order,
  1672. struct zonelist *zonelist, enum zone_type high_zoneidx,
  1673. nodemask_t *nodemask, int alloc_flags, struct zone *preferred_zone,
  1674. int migratetype, unsigned long *did_some_progress)
  1675. {
  1676. struct page *page = NULL;
  1677. struct reclaim_state reclaim_state;
  1678. bool drained = false;
  1679. cond_resched();
  1680. /* We now go into synchronous reclaim */
  1681. cpuset_memory_pressure_bump();
  1682. current->flags |= PF_MEMALLOC;
  1683. lockdep_set_current_reclaim_state(gfp_mask);
  1684. reclaim_state.reclaimed_slab = 0;
  1685. current->reclaim_state = &reclaim_state;
  1686. *did_some_progress = try_to_free_pages(zonelist, order, gfp_mask, nodemask);
  1687. current->reclaim_state = NULL;
  1688. lockdep_clear_current_reclaim_state();
  1689. current->flags &= ~PF_MEMALLOC;
  1690. cond_resched();
  1691. if (unlikely(!(*did_some_progress)))
  1692. return NULL;
  1693. /* After successful reclaim, reconsider all zones for allocation */
  1694. if (NUMA_BUILD)
  1695. zlc_clear_zones_full(zonelist);
  1696. retry:
  1697. page = get_page_from_freelist(gfp_mask, nodemask, order,
  1698. zonelist, high_zoneidx,
  1699. alloc_flags, preferred_zone,
  1700. migratetype);
  1701. /*
  1702. * If an allocation failed after direct reclaim, it could be because
  1703. * pages are pinned on the per-cpu lists. Drain them and try again
  1704. */
  1705. if (!page && !drained) {
  1706. drain_all_pages();
  1707. drained = true;
  1708. goto retry;
  1709. }
  1710. return page;
  1711. }
  1712. /*
  1713. * This is called in the allocator slow-path if the allocation request is of
  1714. * sufficient urgency to ignore watermarks and take other desperate measures
  1715. */
  1716. static inline struct page *
  1717. __alloc_pages_high_priority(gfp_t gfp_mask, unsigned int order,
  1718. struct zonelist *zonelist, enum zone_type high_zoneidx,
  1719. nodemask_t *nodemask, struct zone *preferred_zone,
  1720. int migratetype)
  1721. {
  1722. struct page *page;
  1723. do {
  1724. page = get_page_from_freelist(gfp_mask, nodemask, order,
  1725. zonelist, high_zoneidx, ALLOC_NO_WATERMARKS,
  1726. preferred_zone, migratetype);
  1727. if (!page && gfp_mask & __GFP_NOFAIL)
  1728. wait_iff_congested(preferred_zone, BLK_RW_ASYNC, HZ/50);
  1729. } while (!page && (gfp_mask & __GFP_NOFAIL));
  1730. return page;
  1731. }
  1732. static inline
  1733. void wake_all_kswapd(unsigned int order, struct zonelist *zonelist,
  1734. enum zone_type high_zoneidx,
  1735. enum zone_type classzone_idx)
  1736. {
  1737. struct zoneref *z;
  1738. struct zone *zone;
  1739. for_each_zone_zonelist(zone, z, zonelist, high_zoneidx)
  1740. wakeup_kswapd(zone, order, classzone_idx);
  1741. }
  1742. static inline int
  1743. gfp_to_alloc_flags(gfp_t gfp_mask)
  1744. {
  1745. int alloc_flags = ALLOC_WMARK_MIN | ALLOC_CPUSET;
  1746. const gfp_t wait = gfp_mask & __GFP_WAIT;
  1747. /* __GFP_HIGH is assumed to be the same as ALLOC_HIGH to save a branch. */
  1748. BUILD_BUG_ON(__GFP_HIGH != (__force gfp_t) ALLOC_HIGH);
  1749. /*
  1750. * The caller may dip into page reserves a bit more if the caller
  1751. * cannot run direct reclaim, or if the caller has realtime scheduling
  1752. * policy or is asking for __GFP_HIGH memory. GFP_ATOMIC requests will
  1753. * set both ALLOC_HARDER (!wait) and ALLOC_HIGH (__GFP_HIGH).
  1754. */
  1755. alloc_flags |= (__force int) (gfp_mask & __GFP_HIGH);
  1756. if (!wait) {
  1757. /*
  1758. * Not worth trying to allocate harder for
  1759. * __GFP_NOMEMALLOC even if it can't schedule.
  1760. */
  1761. if (!(gfp_mask & __GFP_NOMEMALLOC))
  1762. alloc_flags |= ALLOC_HARDER;
  1763. /*
  1764. * Ignore cpuset if GFP_ATOMIC (!wait) rather than fail alloc.
  1765. * See also cpuset_zone_allowed() comment in kernel/cpuset.c.
  1766. */
  1767. alloc_flags &= ~ALLOC_CPUSET;
  1768. } else if (unlikely(rt_task(current)) && !in_interrupt())
  1769. alloc_flags |= ALLOC_HARDER;
  1770. if (likely(!(gfp_mask & __GFP_NOMEMALLOC))) {
  1771. if (!in_interrupt() &&
  1772. ((current->flags & PF_MEMALLOC) ||
  1773. unlikely(test_thread_flag(TIF_MEMDIE))))
  1774. alloc_flags |= ALLOC_NO_WATERMARKS;
  1775. }
  1776. return alloc_flags;
  1777. }
  1778. static inline struct page *
  1779. __alloc_pages_slowpath(gfp_t gfp_mask, unsigned int order,
  1780. struct zonelist *zonelist, enum zone_type high_zoneidx,
  1781. nodemask_t *nodemask, struct zone *preferred_zone,
  1782. int migratetype)
  1783. {
  1784. const gfp_t wait = gfp_mask & __GFP_WAIT;
  1785. struct page *page = NULL;
  1786. int alloc_flags;
  1787. unsigned long pages_reclaimed = 0;
  1788. unsigned long did_some_progress;
  1789. bool sync_migration = false;
  1790. /*
  1791. * In the slowpath, we sanity check order to avoid ever trying to
  1792. * reclaim >= MAX_ORDER areas which will never succeed. Callers may
  1793. * be using allocators in order of preference for an area that is
  1794. * too large.
  1795. */
  1796. if (order >= MAX_ORDER) {
  1797. WARN_ON_ONCE(!(gfp_mask & __GFP_NOWARN));
  1798. return NULL;
  1799. }
  1800. /*
  1801. * GFP_THISNODE (meaning __GFP_THISNODE, __GFP_NORETRY and
  1802. * __GFP_NOWARN set) should not cause reclaim since the subsystem
  1803. * (f.e. slab) using GFP_THISNODE may choose to trigger reclaim
  1804. * using a larger set of nodes after it has established that the
  1805. * allowed per node queues are empty and that nodes are
  1806. * over allocated.
  1807. */
  1808. if (NUMA_BUILD && (gfp_mask & GFP_THISNODE) == GFP_THISNODE)
  1809. goto nopage;
  1810. restart:
  1811. if (!(gfp_mask & __GFP_NO_KSWAPD))
  1812. wake_all_kswapd(order, zonelist, high_zoneidx,
  1813. zone_idx(preferred_zone));
  1814. /*
  1815. * OK, we're below the kswapd watermark and have kicked background
  1816. * reclaim. Now things get more complex, so set up alloc_flags according
  1817. * to how we want to proceed.
  1818. */
  1819. alloc_flags = gfp_to_alloc_flags(gfp_mask);
  1820. /*
  1821. * Find the true preferred zone if the allocation is unconstrained by
  1822. * cpusets.
  1823. */
  1824. if (!(alloc_flags & ALLOC_CPUSET) && !nodemask)
  1825. first_zones_zonelist(zonelist, high_zoneidx, NULL,
  1826. &preferred_zone);
  1827. rebalance:
  1828. /* This is the last chance, in general, before the goto nopage. */
  1829. page = get_page_from_freelist(gfp_mask, nodemask, order, zonelist,
  1830. high_zoneidx, alloc_flags & ~ALLOC_NO_WATERMARKS,
  1831. preferred_zone, migratetype);
  1832. if (page)
  1833. goto got_pg;
  1834. /* Allocate without watermarks if the context allows */
  1835. if (alloc_flags & ALLOC_NO_WATERMARKS) {
  1836. page = __alloc_pages_high_priority(gfp_mask, order,
  1837. zonelist, high_zoneidx, nodemask,
  1838. preferred_zone, migratetype);
  1839. if (page)
  1840. goto got_pg;
  1841. }
  1842. /* Atomic allocations - we can't balance anything */
  1843. if (!wait)
  1844. goto nopage;
  1845. /* Avoid recursion of direct reclaim */
  1846. if (current->flags & PF_MEMALLOC)
  1847. goto nopage;
  1848. /* Avoid allocations with no watermarks from looping endlessly */
  1849. if (test_thread_flag(TIF_MEMDIE) && !(gfp_mask & __GFP_NOFAIL))
  1850. goto nopage;
  1851. /*
  1852. * Try direct compaction. The first pass is asynchronous. Subsequent
  1853. * attempts after direct reclaim are synchronous
  1854. */
  1855. page = __alloc_pages_direct_compact(gfp_mask, order,
  1856. zonelist, high_zoneidx,
  1857. nodemask,
  1858. alloc_flags, preferred_zone,
  1859. migratetype, &did_some_progress,
  1860. sync_migration);
  1861. if (page)
  1862. goto got_pg;
  1863. sync_migration = true;
  1864. /* Try direct reclaim and then allocating */
  1865. page = __alloc_pages_direct_reclaim(gfp_mask, order,
  1866. zonelist, high_zoneidx,
  1867. nodemask,
  1868. alloc_flags, preferred_zone,
  1869. migratetype, &did_some_progress);
  1870. if (page)
  1871. goto got_pg;
  1872. /*
  1873. * If we failed to make any progress reclaiming, then we are
  1874. * running out of options and have to consider going OOM
  1875. */
  1876. if (!did_some_progress) {
  1877. if ((gfp_mask & __GFP_FS) && !(gfp_mask & __GFP_NORETRY)) {
  1878. if (oom_killer_disabled)
  1879. goto nopage;
  1880. page = __alloc_pages_may_oom(gfp_mask, order,
  1881. zonelist, high_zoneidx,
  1882. nodemask, preferred_zone,
  1883. migratetype);
  1884. if (page)
  1885. goto got_pg;
  1886. if (!(gfp_mask & __GFP_NOFAIL)) {
  1887. /*
  1888. * The oom killer is not called for high-order
  1889. * allocations that may fail, so if no progress
  1890. * is being made, there are no other options and
  1891. * retrying is unlikely to help.
  1892. */
  1893. if (order > PAGE_ALLOC_COSTLY_ORDER)
  1894. goto nopage;
  1895. /*
  1896. * The oom killer is not called for lowmem
  1897. * allocations to prevent needlessly killing
  1898. * innocent tasks.
  1899. */
  1900. if (high_zoneidx < ZONE_NORMAL)
  1901. goto nopage;
  1902. }
  1903. goto restart;
  1904. }
  1905. }
  1906. /* Check if we should retry the allocation */
  1907. pages_reclaimed += did_some_progress;
  1908. if (should_alloc_retry(gfp_mask, order, pages_reclaimed)) {
  1909. /* Wait for some write requests to complete then retry */
  1910. wait_iff_congested(preferred_zone, BLK_RW_ASYNC, HZ/50);
  1911. goto rebalance;
  1912. } else {
  1913. /*
  1914. * High-order allocations do not necessarily loop after
  1915. * direct reclaim and reclaim/compaction depends on compaction
  1916. * being called after reclaim so call directly if necessary
  1917. */
  1918. page = __alloc_pages_direct_compact(gfp_mask, order,
  1919. zonelist, high_zoneidx,
  1920. nodemask,
  1921. alloc_flags, preferred_zone,
  1922. migratetype, &did_some_progress,
  1923. sync_migration);
  1924. if (page)
  1925. goto got_pg;
  1926. }
  1927. nopage:
  1928. warn_alloc_failed(gfp_mask, order, NULL);
  1929. return page;
  1930. got_pg:
  1931. if (kmemcheck_enabled)
  1932. kmemcheck_pagealloc_alloc(page, order, gfp_mask);
  1933. return page;
  1934. }
  1935. /*
  1936. * This is the 'heart' of the zoned buddy allocator.
  1937. */
  1938. struct page *
  1939. __alloc_pages_nodemask(gfp_t gfp_mask, unsigned int order,
  1940. struct zonelist *zonelist, nodemask_t *nodemask)
  1941. {
  1942. enum zone_type high_zoneidx = gfp_zone(gfp_mask);
  1943. struct zone *preferred_zone;
  1944. struct page *page;
  1945. int migratetype = allocflags_to_migratetype(gfp_mask);
  1946. gfp_mask &= gfp_allowed_mask;
  1947. lockdep_trace_alloc(gfp_mask);
  1948. might_sleep_if(gfp_mask & __GFP_WAIT);
  1949. if (should_fail_alloc_page(gfp_mask, order))
  1950. return NULL;
  1951. /*
  1952. * Check the zones suitable for the gfp_mask contain at least one
  1953. * valid zone. It's possible to have an empty zonelist as a result
  1954. * of GFP_THISNODE and a memoryless node
  1955. */
  1956. if (unlikely(!zonelist->_zonerefs->zone))
  1957. return NULL;
  1958. get_mems_allowed();
  1959. /* The preferred zone is used for statistics later */
  1960. first_zones_zonelist(zonelist, high_zoneidx,
  1961. nodemask ? : &cpuset_current_mems_allowed,
  1962. &preferred_zone);
  1963. if (!preferred_zone) {
  1964. put_mems_allowed();
  1965. return NULL;
  1966. }
  1967. /* First allocation attempt */
  1968. page = get_page_from_freelist(gfp_mask|__GFP_HARDWALL, nodemask, order,
  1969. zonelist, high_zoneidx, ALLOC_WMARK_LOW|ALLOC_CPUSET,
  1970. preferred_zone, migratetype);
  1971. if (unlikely(!page))
  1972. page = __alloc_pages_slowpath(gfp_mask, order,
  1973. zonelist, high_zoneidx, nodemask,
  1974. preferred_zone, migratetype);
  1975. put_mems_allowed();
  1976. trace_mm_page_alloc(page, order, gfp_mask, migratetype);
  1977. return page;
  1978. }
  1979. EXPORT_SYMBOL(__alloc_pages_nodemask);
  1980. /*
  1981. * Common helper functions.
  1982. */
  1983. unsigned long __get_free_pages(gfp_t gfp_mask, unsigned int order)
  1984. {
  1985. struct page *page;
  1986. /*
  1987. * __get_free_pages() returns a 32-bit address, which cannot represent
  1988. * a highmem page
  1989. */
  1990. VM_BUG_ON((gfp_mask & __GFP_HIGHMEM) != 0);
  1991. page = alloc_pages(gfp_mask, order);
  1992. if (!page)
  1993. return 0;
  1994. return (unsigned long) page_address(page);
  1995. }
  1996. EXPORT_SYMBOL(__get_free_pages);
  1997. unsigned long get_zeroed_page(gfp_t gfp_mask)
  1998. {
  1999. return __get_free_pages(gfp_mask | __GFP_ZERO, 0);
  2000. }
  2001. EXPORT_SYMBOL(get_zeroed_page);
  2002. void __pagevec_free(struct pagevec *pvec)
  2003. {
  2004. int i = pagevec_count(pvec);
  2005. while (--i >= 0) {
  2006. trace_mm_pagevec_free(pvec->pages[i], pvec->cold);
  2007. free_hot_cold_page(pvec->pages[i], pvec->cold);
  2008. }
  2009. }
  2010. void __free_pages(struct page *page, unsigned int order)
  2011. {
  2012. if (put_page_testzero(page)) {
  2013. if (order == 0)
  2014. free_hot_cold_page(page, 0);
  2015. else
  2016. __free_pages_ok(page, order);
  2017. }
  2018. }
  2019. EXPORT_SYMBOL(__free_pages);
  2020. void free_pages(unsigned long addr, unsigned int order)
  2021. {
  2022. if (addr != 0) {
  2023. VM_BUG_ON(!virt_addr_valid((void *)addr));
  2024. __free_pages(virt_to_page((void *)addr), order);
  2025. }
  2026. }
  2027. EXPORT_SYMBOL(free_pages);
  2028. static void *make_alloc_exact(unsigned long addr, unsigned order, size_t size)
  2029. {
  2030. if (addr) {
  2031. unsigned long alloc_end = addr + (PAGE_SIZE << order);
  2032. unsigned long used = addr + PAGE_ALIGN(size);
  2033. split_page(virt_to_page((void *)addr), order);
  2034. while (used < alloc_end) {
  2035. free_page(used);
  2036. used += PAGE_SIZE;
  2037. }
  2038. }
  2039. return (void *)addr;
  2040. }
  2041. /**
  2042. * alloc_pages_exact - allocate an exact number physically-contiguous pages.
  2043. * @size: the number of bytes to allocate
  2044. * @gfp_mask: GFP flags for the allocation
  2045. *
  2046. * This function is similar to alloc_pages(), except that it allocates the
  2047. * minimum number of pages to satisfy the request. alloc_pages() can only
  2048. * allocate memory in power-of-two pages.
  2049. *
  2050. * This function is also limited by MAX_ORDER.
  2051. *
  2052. * Memory allocated by this function must be released by free_pages_exact().
  2053. */
  2054. void *alloc_pages_exact(size_t size, gfp_t gfp_mask)
  2055. {
  2056. unsigned int order = get_order(size);
  2057. unsigned long addr;
  2058. addr = __get_free_pages(gfp_mask, order);
  2059. return make_alloc_exact(addr, order, size);
  2060. }
  2061. EXPORT_SYMBOL(alloc_pages_exact);
  2062. /**
  2063. * alloc_pages_exact_nid - allocate an exact number of physically-contiguous
  2064. * pages on a node.
  2065. * @nid: the preferred node ID where memory should be allocated
  2066. * @size: the number of bytes to allocate
  2067. * @gfp_mask: GFP flags for the allocation
  2068. *
  2069. * Like alloc_pages_exact(), but try to allocate on node nid first before falling
  2070. * back.
  2071. * Note this is not alloc_pages_exact_node() which allocates on a specific node,
  2072. * but is not exact.
  2073. */
  2074. void *alloc_pages_exact_nid(int nid, size_t size, gfp_t gfp_mask)
  2075. {
  2076. unsigned order = get_order(size);
  2077. struct page *p = alloc_pages_node(nid, gfp_mask, order);
  2078. if (!p)
  2079. return NULL;
  2080. return make_alloc_exact((unsigned long)page_address(p), order, size);
  2081. }
  2082. EXPORT_SYMBOL(alloc_pages_exact_nid);
  2083. /**
  2084. * free_pages_exact - release memory allocated via alloc_pages_exact()
  2085. * @virt: the value returned by alloc_pages_exact.
  2086. * @size: size of allocation, same value as passed to alloc_pages_exact().
  2087. *
  2088. * Release the memory allocated by a previous call to alloc_pages_exact.
  2089. */
  2090. void free_pages_exact(void *virt, size_t size)
  2091. {
  2092. unsigned long addr = (unsigned long)virt;
  2093. unsigned long end = addr + PAGE_ALIGN(size);
  2094. while (addr < end) {
  2095. free_page(addr);
  2096. addr += PAGE_SIZE;
  2097. }
  2098. }
  2099. EXPORT_SYMBOL(free_pages_exact);
  2100. static unsigned int nr_free_zone_pages(int offset)
  2101. {
  2102. struct zoneref *z;
  2103. struct zone *zone;
  2104. /* Just pick one node, since fallback list is circular */
  2105. unsigned int sum = 0;
  2106. struct zonelist *zonelist = node_zonelist(numa_node_id(), GFP_KERNEL);
  2107. for_each_zone_zonelist(zone, z, zonelist, offset) {
  2108. unsigned long size = zone->present_pages;
  2109. unsigned long high = high_wmark_pages(zone);
  2110. if (size > high)
  2111. sum += size - high;
  2112. }
  2113. return sum;
  2114. }
  2115. /*
  2116. * Amount of free RAM allocatable within ZONE_DMA and ZONE_NORMAL
  2117. */
  2118. unsigned int nr_free_buffer_pages(void)
  2119. {
  2120. return nr_free_zone_pages(gfp_zone(GFP_USER));
  2121. }
  2122. EXPORT_SYMBOL_GPL(nr_free_buffer_pages);
  2123. /*
  2124. * Amount of free RAM allocatable within all zones
  2125. */
  2126. unsigned int nr_free_pagecache_pages(void)
  2127. {
  2128. return nr_free_zone_pages(gfp_zone(GFP_HIGHUSER_MOVABLE));
  2129. }
  2130. static inline void show_node(struct zone *zone)
  2131. {
  2132. if (NUMA_BUILD)
  2133. printk("Node %d ", zone_to_nid(zone));
  2134. }
  2135. void si_meminfo(struct sysinfo *val)
  2136. {
  2137. val->totalram = totalram_pages;
  2138. val->sharedram = 0;
  2139. val->freeram = global_page_state(NR_FREE_PAGES);
  2140. val->bufferram = nr_blockdev_pages();
  2141. val->totalhigh = totalhigh_pages;
  2142. val->freehigh = nr_free_highpages();
  2143. val->mem_unit = PAGE_SIZE;
  2144. }
  2145. EXPORT_SYMBOL(si_meminfo);
  2146. #ifdef CONFIG_NUMA
  2147. void si_meminfo_node(struct sysinfo *val, int nid)
  2148. {
  2149. pg_data_t *pgdat = NODE_DATA(nid);
  2150. val->totalram = pgdat->node_present_pages;
  2151. val->freeram = node_page_state(nid, NR_FREE_PAGES);
  2152. #ifdef CONFIG_HIGHMEM
  2153. val->totalhigh = pgdat->node_zones[ZONE_HIGHMEM].present_pages;
  2154. val->freehigh = zone_page_state(&pgdat->node_zones[ZONE_HIGHMEM],
  2155. NR_FREE_PAGES);
  2156. #else
  2157. val->totalhigh = 0;
  2158. val->freehigh = 0;
  2159. #endif
  2160. val->mem_unit = PAGE_SIZE;
  2161. }
  2162. #endif
  2163. /*
  2164. * Determine whether the node should be displayed or not, depending on whether
  2165. * SHOW_MEM_FILTER_NODES was passed to show_free_areas().
  2166. */
  2167. bool skip_free_areas_node(unsigned int flags, int nid)
  2168. {
  2169. bool ret = false;
  2170. if (!(flags & SHOW_MEM_FILTER_NODES))
  2171. goto out;
  2172. get_mems_allowed();
  2173. ret = !node_isset(nid, cpuset_current_mems_allowed);
  2174. put_mems_allowed();
  2175. out:
  2176. return ret;
  2177. }
  2178. #define K(x) ((x) << (PAGE_SHIFT-10))
  2179. /*
  2180. * Show free area list (used inside shift_scroll-lock stuff)
  2181. * We also calculate the percentage fragmentation. We do this by counting the
  2182. * memory on each free list with the exception of the first item on the list.
  2183. * Suppresses nodes that are not allowed by current's cpuset if
  2184. * SHOW_MEM_FILTER_NODES is passed.
  2185. */
  2186. void show_free_areas(unsigned int filter)
  2187. {
  2188. int cpu;
  2189. struct zone *zone;
  2190. for_each_populated_zone(zone) {
  2191. if (skip_free_areas_node(filter, zone_to_nid(zone)))
  2192. continue;
  2193. show_node(zone);
  2194. printk("%s per-cpu:\n", zone->name);
  2195. for_each_online_cpu(cpu) {
  2196. struct per_cpu_pageset *pageset;
  2197. pageset = per_cpu_ptr(zone->pageset, cpu);
  2198. printk("CPU %4d: hi:%5d, btch:%4d usd:%4d\n",
  2199. cpu, pageset->pcp.high,
  2200. pageset->pcp.batch, pageset->pcp.count);
  2201. }
  2202. }
  2203. printk("active_anon:%lu inactive_anon:%lu isolated_anon:%lu\n"
  2204. " active_file:%lu inactive_file:%lu isolated_file:%lu\n"
  2205. " unevictable:%lu"
  2206. " dirty:%lu writeback:%lu unstable:%lu\n"
  2207. " free:%lu slab_reclaimable:%lu slab_unreclaimable:%lu\n"
  2208. " mapped:%lu shmem:%lu pagetables:%lu bounce:%lu\n",
  2209. global_page_state(NR_ACTIVE_ANON),
  2210. global_page_state(NR_INACTIVE_ANON),
  2211. global_page_state(NR_ISOLATED_ANON),
  2212. global_page_state(NR_ACTIVE_FILE),
  2213. global_page_state(NR_INACTIVE_FILE),
  2214. global_page_state(NR_ISOLATED_FILE),
  2215. global_page_state(NR_UNEVICTABLE),
  2216. global_page_state(NR_FILE_DIRTY),
  2217. global_page_state(NR_WRITEBACK),
  2218. global_page_state(NR_UNSTABLE_NFS),
  2219. global_page_state(NR_FREE_PAGES),
  2220. global_page_state(NR_SLAB_RECLAIMABLE),
  2221. global_page_state(NR_SLAB_UNRECLAIMABLE),
  2222. global_page_state(NR_FILE_MAPPED),
  2223. global_page_state(NR_SHMEM),
  2224. global_page_state(NR_PAGETABLE),
  2225. global_page_state(NR_BOUNCE));
  2226. for_each_populated_zone(zone) {
  2227. int i;
  2228. if (skip_free_areas_node(filter, zone_to_nid(zone)))
  2229. continue;
  2230. show_node(zone);
  2231. printk("%s"
  2232. " free:%lukB"
  2233. " min:%lukB"
  2234. " low:%lukB"
  2235. " high:%lukB"
  2236. " active_anon:%lukB"
  2237. " inactive_anon:%lukB"
  2238. " active_file:%lukB"
  2239. " inactive_file:%lukB"
  2240. " unevictable:%lukB"
  2241. " isolated(anon):%lukB"
  2242. " isolated(file):%lukB"
  2243. " present:%lukB"
  2244. " mlocked:%lukB"
  2245. " dirty:%lukB"
  2246. " writeback:%lukB"
  2247. " mapped:%lukB"
  2248. " shmem:%lukB"
  2249. " slab_reclaimable:%lukB"
  2250. " slab_unreclaimable:%lukB"
  2251. " kernel_stack:%lukB"
  2252. " pagetables:%lukB"
  2253. " unstable:%lukB"
  2254. " bounce:%lukB"
  2255. " writeback_tmp:%lukB"
  2256. " pages_scanned:%lu"
  2257. " all_unreclaimable? %s"
  2258. "\n",
  2259. zone->name,
  2260. K(zone_page_state(zone, NR_FREE_PAGES)),
  2261. K(min_wmark_pages(zone)),
  2262. K(low_wmark_pages(zone)),
  2263. K(high_wmark_pages(zone)),
  2264. K(zone_page_state(zone, NR_ACTIVE_ANON)),
  2265. K(zone_page_state(zone, NR_INACTIVE_ANON)),
  2266. K(zone_page_state(zone, NR_ACTIVE_FILE)),
  2267. K(zone_page_state(zone, NR_INACTIVE_FILE)),
  2268. K(zone_page_state(zone, NR_UNEVICTABLE)),
  2269. K(zone_page_state(zone, NR_ISOLATED_ANON)),
  2270. K(zone_page_state(zone, NR_ISOLATED_FILE)),
  2271. K(zone->present_pages),
  2272. K(zone_page_state(zone, NR_MLOCK)),
  2273. K(zone_page_state(zone, NR_FILE_DIRTY)),
  2274. K(zone_page_state(zone, NR_WRITEBACK)),
  2275. K(zone_page_state(zone, NR_FILE_MAPPED)),
  2276. K(zone_page_state(zone, NR_SHMEM)),
  2277. K(zone_page_state(zone, NR_SLAB_RECLAIMABLE)),
  2278. K(zone_page_state(zone, NR_SLAB_UNRECLAIMABLE)),
  2279. zone_page_state(zone, NR_KERNEL_STACK) *
  2280. THREAD_SIZE / 1024,
  2281. K(zone_page_state(zone, NR_PAGETABLE)),
  2282. K(zone_page_state(zone, NR_UNSTABLE_NFS)),
  2283. K(zone_page_state(zone, NR_BOUNCE)),
  2284. K(zone_page_state(zone, NR_WRITEBACK_TEMP)),
  2285. zone->pages_scanned,
  2286. (zone->all_unreclaimable ? "yes" : "no")
  2287. );
  2288. printk("lowmem_reserve[]:");
  2289. for (i = 0; i < MAX_NR_ZONES; i++)
  2290. printk(" %lu", zone->lowmem_reserve[i]);
  2291. printk("\n");
  2292. }
  2293. for_each_populated_zone(zone) {
  2294. unsigned long nr[MAX_ORDER], flags, order, total = 0;
  2295. if (skip_free_areas_node(filter, zone_to_nid(zone)))
  2296. continue;
  2297. show_node(zone);
  2298. printk("%s: ", zone->name);
  2299. spin_lock_irqsave(&zone->lock, flags);
  2300. for (order = 0; order < MAX_ORDER; order++) {
  2301. nr[order] = zone->free_area[order].nr_free;
  2302. total += nr[order] << order;
  2303. }
  2304. spin_unlock_irqrestore(&zone->lock, flags);
  2305. for (order = 0; order < MAX_ORDER; order++)
  2306. printk("%lu*%lukB ", nr[order], K(1UL) << order);
  2307. printk("= %lukB\n", K(total));
  2308. }
  2309. printk("%ld total pagecache pages\n", global_page_state(NR_FILE_PAGES));
  2310. show_swap_cache_info();
  2311. }
  2312. static void zoneref_set_zone(struct zone *zone, struct zoneref *zoneref)
  2313. {
  2314. zoneref->zone = zone;
  2315. zoneref->zone_idx = zone_idx(zone);
  2316. }
  2317. /*
  2318. * Builds allocation fallback zone lists.
  2319. *
  2320. * Add all populated zones of a node to the zonelist.
  2321. */
  2322. static int build_zonelists_node(pg_data_t *pgdat, struct zonelist *zonelist,
  2323. int nr_zones, enum zone_type zone_type)
  2324. {
  2325. struct zone *zone;
  2326. BUG_ON(zone_type >= MAX_NR_ZONES);
  2327. zone_type++;
  2328. do {
  2329. zone_type--;
  2330. zone = pgdat->node_zones + zone_type;
  2331. if (populated_zone(zone)) {
  2332. zoneref_set_zone(zone,
  2333. &zonelist->_zonerefs[nr_zones++]);
  2334. check_highest_zone(zone_type);
  2335. }
  2336. } while (zone_type);
  2337. return nr_zones;
  2338. }
  2339. /*
  2340. * zonelist_order:
  2341. * 0 = automatic detection of better ordering.
  2342. * 1 = order by ([node] distance, -zonetype)
  2343. * 2 = order by (-zonetype, [node] distance)
  2344. *
  2345. * If not NUMA, ZONELIST_ORDER_ZONE and ZONELIST_ORDER_NODE will create
  2346. * the same zonelist. So only NUMA can configure this param.
  2347. */
  2348. #define ZONELIST_ORDER_DEFAULT 0
  2349. #define ZONELIST_ORDER_NODE 1
  2350. #define ZONELIST_ORDER_ZONE 2
  2351. /* zonelist order in the kernel.
  2352. * set_zonelist_order() will set this to NODE or ZONE.
  2353. */
  2354. static int current_zonelist_order = ZONELIST_ORDER_DEFAULT;
  2355. static char zonelist_order_name[3][8] = {"Default", "Node", "Zone"};
  2356. #ifdef CONFIG_NUMA
  2357. /* The value user specified ....changed by config */
  2358. static int user_zonelist_order = ZONELIST_ORDER_DEFAULT;
  2359. /* string for sysctl */
  2360. #define NUMA_ZONELIST_ORDER_LEN 16
  2361. char numa_zonelist_order[16] = "default";
  2362. /*
  2363. * interface for configure zonelist ordering.
  2364. * command line option "numa_zonelist_order"
  2365. * = "[dD]efault - default, automatic configuration.
  2366. * = "[nN]ode - order by node locality, then by zone within node
  2367. * = "[zZ]one - order by zone, then by locality within zone
  2368. */
  2369. static int __parse_numa_zonelist_order(char *s)
  2370. {
  2371. if (*s == 'd' || *s == 'D') {
  2372. user_zonelist_order = ZONELIST_ORDER_DEFAULT;
  2373. } else if (*s == 'n' || *s == 'N') {
  2374. user_zonelist_order = ZONELIST_ORDER_NODE;
  2375. } else if (*s == 'z' || *s == 'Z') {
  2376. user_zonelist_order = ZONELIST_ORDER_ZONE;
  2377. } else {
  2378. printk(KERN_WARNING
  2379. "Ignoring invalid numa_zonelist_order value: "
  2380. "%s\n", s);
  2381. return -EINVAL;
  2382. }
  2383. return 0;
  2384. }
  2385. static __init int setup_numa_zonelist_order(char *s)
  2386. {
  2387. int ret;
  2388. if (!s)
  2389. return 0;
  2390. ret = __parse_numa_zonelist_order(s);
  2391. if (ret == 0)
  2392. strlcpy(numa_zonelist_order, s, NUMA_ZONELIST_ORDER_LEN);
  2393. return ret;
  2394. }
  2395. early_param("numa_zonelist_order", setup_numa_zonelist_order);
  2396. /*
  2397. * sysctl handler for numa_zonelist_order
  2398. */
  2399. int numa_zonelist_order_handler(ctl_table *table, int write,
  2400. void __user *buffer, size_t *length,
  2401. loff_t *ppos)
  2402. {
  2403. char saved_string[NUMA_ZONELIST_ORDER_LEN];
  2404. int ret;
  2405. static DEFINE_MUTEX(zl_order_mutex);
  2406. mutex_lock(&zl_order_mutex);
  2407. if (write)
  2408. strcpy(saved_string, (char*)table->data);
  2409. ret = proc_dostring(table, write, buffer, length, ppos);
  2410. if (ret)
  2411. goto out;
  2412. if (write) {
  2413. int oldval = user_zonelist_order;
  2414. if (__parse_numa_zonelist_order((char*)table->data)) {
  2415. /*
  2416. * bogus value. restore saved string
  2417. */
  2418. strncpy((char*)table->data, saved_string,
  2419. NUMA_ZONELIST_ORDER_LEN);
  2420. user_zonelist_order = oldval;
  2421. } else if (oldval != user_zonelist_order) {
  2422. mutex_lock(&zonelists_mutex);
  2423. build_all_zonelists(NULL);
  2424. mutex_unlock(&zonelists_mutex);
  2425. }
  2426. }
  2427. out:
  2428. mutex_unlock(&zl_order_mutex);
  2429. return ret;
  2430. }
  2431. #define MAX_NODE_LOAD (nr_online_nodes)
  2432. static int node_load[MAX_NUMNODES];
  2433. /**
  2434. * find_next_best_node - find the next node that should appear in a given node's fallback list
  2435. * @node: node whose fallback list we're appending
  2436. * @used_node_mask: nodemask_t of already used nodes
  2437. *
  2438. * We use a number of factors to determine which is the next node that should
  2439. * appear on a given node's fallback list. The node should not have appeared
  2440. * already in @node's fallback list, and it should be the next closest node
  2441. * according to the distance array (which contains arbitrary distance values
  2442. * from each node to each node in the system), and should also prefer nodes
  2443. * with no CPUs, since presumably they'll have very little allocation pressure
  2444. * on them otherwise.
  2445. * It returns -1 if no node is found.
  2446. */
  2447. static int find_next_best_node(int node, nodemask_t *used_node_mask)
  2448. {
  2449. int n, val;
  2450. int min_val = INT_MAX;
  2451. int best_node = -1;
  2452. const struct cpumask *tmp = cpumask_of_node(0);
  2453. /* Use the local node if we haven't already */
  2454. if (!node_isset(node, *used_node_mask)) {
  2455. node_set(node, *used_node_mask);
  2456. return node;
  2457. }
  2458. for_each_node_state(n, N_HIGH_MEMORY) {
  2459. /* Don't want a node to appear more than once */
  2460. if (node_isset(n, *used_node_mask))
  2461. continue;
  2462. /* Use the distance array to find the distance */
  2463. val = node_distance(node, n);
  2464. /* Penalize nodes under us ("prefer the next node") */
  2465. val += (n < node);
  2466. /* Give preference to headless and unused nodes */
  2467. tmp = cpumask_of_node(n);
  2468. if (!cpumask_empty(tmp))
  2469. val += PENALTY_FOR_NODE_WITH_CPUS;
  2470. /* Slight preference for less loaded node */
  2471. val *= (MAX_NODE_LOAD*MAX_NUMNODES);
  2472. val += node_load[n];
  2473. if (val < min_val) {
  2474. min_val = val;
  2475. best_node = n;
  2476. }
  2477. }
  2478. if (best_node >= 0)
  2479. node_set(best_node, *used_node_mask);
  2480. return best_node;
  2481. }
  2482. /*
  2483. * Build zonelists ordered by node and zones within node.
  2484. * This results in maximum locality--normal zone overflows into local
  2485. * DMA zone, if any--but risks exhausting DMA zone.
  2486. */
  2487. static void build_zonelists_in_node_order(pg_data_t *pgdat, int node)
  2488. {
  2489. int j;
  2490. struct zonelist *zonelist;
  2491. zonelist = &pgdat->node_zonelists[0];
  2492. for (j = 0; zonelist->_zonerefs[j].zone != NULL; j++)
  2493. ;
  2494. j = build_zonelists_node(NODE_DATA(node), zonelist, j,
  2495. MAX_NR_ZONES - 1);
  2496. zonelist->_zonerefs[j].zone = NULL;
  2497. zonelist->_zonerefs[j].zone_idx = 0;
  2498. }
  2499. /*
  2500. * Build gfp_thisnode zonelists
  2501. */
  2502. static void build_thisnode_zonelists(pg_data_t *pgdat)
  2503. {
  2504. int j;
  2505. struct zonelist *zonelist;
  2506. zonelist = &pgdat->node_zonelists[1];
  2507. j = build_zonelists_node(pgdat, zonelist, 0, MAX_NR_ZONES - 1);
  2508. zonelist->_zonerefs[j].zone = NULL;
  2509. zonelist->_zonerefs[j].zone_idx = 0;
  2510. }
  2511. /*
  2512. * Build zonelists ordered by zone and nodes within zones.
  2513. * This results in conserving DMA zone[s] until all Normal memory is
  2514. * exhausted, but results in overflowing to remote node while memory
  2515. * may still exist in local DMA zone.
  2516. */
  2517. static int node_order[MAX_NUMNODES];
  2518. static void build_zonelists_in_zone_order(pg_data_t *pgdat, int nr_nodes)
  2519. {
  2520. int pos, j, node;
  2521. int zone_type; /* needs to be signed */
  2522. struct zone *z;
  2523. struct zonelist *zonelist;
  2524. zonelist = &pgdat->node_zonelists[0];
  2525. pos = 0;
  2526. for (zone_type = MAX_NR_ZONES - 1; zone_type >= 0; zone_type--) {
  2527. for (j = 0; j < nr_nodes; j++) {
  2528. node = node_order[j];
  2529. z = &NODE_DATA(node)->node_zones[zone_type];
  2530. if (populated_zone(z)) {
  2531. zoneref_set_zone(z,
  2532. &zonelist->_zonerefs[pos++]);
  2533. check_highest_zone(zone_type);
  2534. }
  2535. }
  2536. }
  2537. zonelist->_zonerefs[pos].zone = NULL;
  2538. zonelist->_zonerefs[pos].zone_idx = 0;
  2539. }
  2540. static int default_zonelist_order(void)
  2541. {
  2542. int nid, zone_type;
  2543. unsigned long low_kmem_size,total_size;
  2544. struct zone *z;
  2545. int average_size;
  2546. /*
  2547. * ZONE_DMA and ZONE_DMA32 can be very small area in the system.
  2548. * If they are really small and used heavily, the system can fall
  2549. * into OOM very easily.
  2550. * This function detect ZONE_DMA/DMA32 size and configures zone order.
  2551. */
  2552. /* Is there ZONE_NORMAL ? (ex. ppc has only DMA zone..) */
  2553. low_kmem_size = 0;
  2554. total_size = 0;
  2555. for_each_online_node(nid) {
  2556. for (zone_type = 0; zone_type < MAX_NR_ZONES; zone_type++) {
  2557. z = &NODE_DATA(nid)->node_zones[zone_type];
  2558. if (populated_zone(z)) {
  2559. if (zone_type < ZONE_NORMAL)
  2560. low_kmem_size += z->present_pages;
  2561. total_size += z->present_pages;
  2562. } else if (zone_type == ZONE_NORMAL) {
  2563. /*
  2564. * If any node has only lowmem, then node order
  2565. * is preferred to allow kernel allocations
  2566. * locally; otherwise, they can easily infringe
  2567. * on other nodes when there is an abundance of
  2568. * lowmem available to allocate from.
  2569. */
  2570. return ZONELIST_ORDER_NODE;
  2571. }
  2572. }
  2573. }
  2574. if (!low_kmem_size || /* there are no DMA area. */
  2575. low_kmem_size > total_size/2) /* DMA/DMA32 is big. */
  2576. return ZONELIST_ORDER_NODE;
  2577. /*
  2578. * look into each node's config.
  2579. * If there is a node whose DMA/DMA32 memory is very big area on
  2580. * local memory, NODE_ORDER may be suitable.
  2581. */
  2582. average_size = total_size /
  2583. (nodes_weight(node_states[N_HIGH_MEMORY]) + 1);
  2584. for_each_online_node(nid) {
  2585. low_kmem_size = 0;
  2586. total_size = 0;
  2587. for (zone_type = 0; zone_type < MAX_NR_ZONES; zone_type++) {
  2588. z = &NODE_DATA(nid)->node_zones[zone_type];
  2589. if (populated_zone(z)) {
  2590. if (zone_type < ZONE_NORMAL)
  2591. low_kmem_size += z->present_pages;
  2592. total_size += z->present_pages;
  2593. }
  2594. }
  2595. if (low_kmem_size &&
  2596. total_size > average_size && /* ignore small node */
  2597. low_kmem_size > total_size * 70/100)
  2598. return ZONELIST_ORDER_NODE;
  2599. }
  2600. return ZONELIST_ORDER_ZONE;
  2601. }
  2602. static void set_zonelist_order(void)
  2603. {
  2604. if (user_zonelist_order == ZONELIST_ORDER_DEFAULT)
  2605. current_zonelist_order = default_zonelist_order();
  2606. else
  2607. current_zonelist_order = user_zonelist_order;
  2608. }
  2609. static void build_zonelists(pg_data_t *pgdat)
  2610. {
  2611. int j, node, load;
  2612. enum zone_type i;
  2613. nodemask_t used_mask;
  2614. int local_node, prev_node;
  2615. struct zonelist *zonelist;
  2616. int order = current_zonelist_order;
  2617. /* initialize zonelists */
  2618. for (i = 0; i < MAX_ZONELISTS; i++) {
  2619. zonelist = pgdat->node_zonelists + i;
  2620. zonelist->_zonerefs[0].zone = NULL;
  2621. zonelist->_zonerefs[0].zone_idx = 0;
  2622. }
  2623. /* NUMA-aware ordering of nodes */
  2624. local_node = pgdat->node_id;
  2625. load = nr_online_nodes;
  2626. prev_node = local_node;
  2627. nodes_clear(used_mask);
  2628. memset(node_order, 0, sizeof(node_order));
  2629. j = 0;
  2630. while ((node = find_next_best_node(local_node, &used_mask)) >= 0) {
  2631. int distance = node_distance(local_node, node);
  2632. /*
  2633. * If another node is sufficiently far away then it is better
  2634. * to reclaim pages in a zone before going off node.
  2635. */
  2636. if (distance > RECLAIM_DISTANCE)
  2637. zone_reclaim_mode = 1;
  2638. /*
  2639. * We don't want to pressure a particular node.
  2640. * So adding penalty to the first node in same
  2641. * distance group to make it round-robin.
  2642. */
  2643. if (distance != node_distance(local_node, prev_node))
  2644. node_load[node] = load;
  2645. prev_node = node;
  2646. load--;
  2647. if (order == ZONELIST_ORDER_NODE)
  2648. build_zonelists_in_node_order(pgdat, node);
  2649. else
  2650. node_order[j++] = node; /* remember order */
  2651. }
  2652. if (order == ZONELIST_ORDER_ZONE) {
  2653. /* calculate node order -- i.e., DMA last! */
  2654. build_zonelists_in_zone_order(pgdat, j);
  2655. }
  2656. build_thisnode_zonelists(pgdat);
  2657. }
  2658. /* Construct the zonelist performance cache - see further mmzone.h */
  2659. static void build_zonelist_cache(pg_data_t *pgdat)
  2660. {
  2661. struct zonelist *zonelist;
  2662. struct zonelist_cache *zlc;
  2663. struct zoneref *z;
  2664. zonelist = &pgdat->node_zonelists[0];
  2665. zonelist->zlcache_ptr = zlc = &zonelist->zlcache;
  2666. bitmap_zero(zlc->fullzones, MAX_ZONES_PER_ZONELIST);
  2667. for (z = zonelist->_zonerefs; z->zone; z++)
  2668. zlc->z_to_n[z - zonelist->_zonerefs] = zonelist_node_idx(z);
  2669. }
  2670. #ifdef CONFIG_HAVE_MEMORYLESS_NODES
  2671. /*
  2672. * Return node id of node used for "local" allocations.
  2673. * I.e., first node id of first zone in arg node's generic zonelist.
  2674. * Used for initializing percpu 'numa_mem', which is used primarily
  2675. * for kernel allocations, so use GFP_KERNEL flags to locate zonelist.
  2676. */
  2677. int local_memory_node(int node)
  2678. {
  2679. struct zone *zone;
  2680. (void)first_zones_zonelist(node_zonelist(node, GFP_KERNEL),
  2681. gfp_zone(GFP_KERNEL),
  2682. NULL,
  2683. &zone);
  2684. return zone->node;
  2685. }
  2686. #endif
  2687. #else /* CONFIG_NUMA */
  2688. static void set_zonelist_order(void)
  2689. {
  2690. current_zonelist_order = ZONELIST_ORDER_ZONE;
  2691. }
  2692. static void build_zonelists(pg_data_t *pgdat)
  2693. {
  2694. int node, local_node;
  2695. enum zone_type j;
  2696. struct zonelist *zonelist;
  2697. local_node = pgdat->node_id;
  2698. zonelist = &pgdat->node_zonelists[0];
  2699. j = build_zonelists_node(pgdat, zonelist, 0, MAX_NR_ZONES - 1);
  2700. /*
  2701. * Now we build the zonelist so that it contains the zones
  2702. * of all the other nodes.
  2703. * We don't want to pressure a particular node, so when
  2704. * building the zones for node N, we make sure that the
  2705. * zones coming right after the local ones are those from
  2706. * node N+1 (modulo N)
  2707. */
  2708. for (node = local_node + 1; node < MAX_NUMNODES; node++) {
  2709. if (!node_online(node))
  2710. continue;
  2711. j = build_zonelists_node(NODE_DATA(node), zonelist, j,
  2712. MAX_NR_ZONES - 1);
  2713. }
  2714. for (node = 0; node < local_node; node++) {
  2715. if (!node_online(node))
  2716. continue;
  2717. j = build_zonelists_node(NODE_DATA(node), zonelist, j,
  2718. MAX_NR_ZONES - 1);
  2719. }
  2720. zonelist->_zonerefs[j].zone = NULL;
  2721. zonelist->_zonerefs[j].zone_idx = 0;
  2722. }
  2723. /* non-NUMA variant of zonelist performance cache - just NULL zlcache_ptr */
  2724. static void build_zonelist_cache(pg_data_t *pgdat)
  2725. {
  2726. pgdat->node_zonelists[0].zlcache_ptr = NULL;
  2727. }
  2728. #endif /* CONFIG_NUMA */
  2729. /*
  2730. * Boot pageset table. One per cpu which is going to be used for all
  2731. * zones and all nodes. The parameters will be set in such a way
  2732. * that an item put on a list will immediately be handed over to
  2733. * the buddy list. This is safe since pageset manipulation is done
  2734. * with interrupts disabled.
  2735. *
  2736. * The boot_pagesets must be kept even after bootup is complete for
  2737. * unused processors and/or zones. They do play a role for bootstrapping
  2738. * hotplugged processors.
  2739. *
  2740. * zoneinfo_show() and maybe other functions do
  2741. * not check if the processor is online before following the pageset pointer.
  2742. * Other parts of the kernel may not check if the zone is available.
  2743. */
  2744. static void setup_pageset(struct per_cpu_pageset *p, unsigned long batch);
  2745. static DEFINE_PER_CPU(struct per_cpu_pageset, boot_pageset);
  2746. static void setup_zone_pageset(struct zone *zone);
  2747. /*
  2748. * Global mutex to protect against size modification of zonelists
  2749. * as well as to serialize pageset setup for the new populated zone.
  2750. */
  2751. DEFINE_MUTEX(zonelists_mutex);
  2752. /* return values int ....just for stop_machine() */
  2753. static __init_refok int __build_all_zonelists(void *data)
  2754. {
  2755. int nid;
  2756. int cpu;
  2757. #ifdef CONFIG_NUMA
  2758. memset(node_load, 0, sizeof(node_load));
  2759. #endif
  2760. for_each_online_node(nid) {
  2761. pg_data_t *pgdat = NODE_DATA(nid);
  2762. build_zonelists(pgdat);
  2763. build_zonelist_cache(pgdat);
  2764. }
  2765. /*
  2766. * Initialize the boot_pagesets that are going to be used
  2767. * for bootstrapping processors. The real pagesets for
  2768. * each zone will be allocated later when the per cpu
  2769. * allocator is available.
  2770. *
  2771. * boot_pagesets are used also for bootstrapping offline
  2772. * cpus if the system is already booted because the pagesets
  2773. * are needed to initialize allocators on a specific cpu too.
  2774. * F.e. the percpu allocator needs the page allocator which
  2775. * needs the percpu allocator in order to allocate its pagesets
  2776. * (a chicken-egg dilemma).
  2777. */
  2778. for_each_possible_cpu(cpu) {
  2779. setup_pageset(&per_cpu(boot_pageset, cpu), 0);
  2780. #ifdef CONFIG_HAVE_MEMORYLESS_NODES
  2781. /*
  2782. * We now know the "local memory node" for each node--
  2783. * i.e., the node of the first zone in the generic zonelist.
  2784. * Set up numa_mem percpu variable for on-line cpus. During
  2785. * boot, only the boot cpu should be on-line; we'll init the
  2786. * secondary cpus' numa_mem as they come on-line. During
  2787. * node/memory hotplug, we'll fixup all on-line cpus.
  2788. */
  2789. if (cpu_online(cpu))
  2790. set_cpu_numa_mem(cpu, local_memory_node(cpu_to_node(cpu)));
  2791. #endif
  2792. }
  2793. return 0;
  2794. }
  2795. /*
  2796. * Called with zonelists_mutex held always
  2797. * unless system_state == SYSTEM_BOOTING.
  2798. */
  2799. void __ref build_all_zonelists(void *data)
  2800. {
  2801. set_zonelist_order();
  2802. if (system_state == SYSTEM_BOOTING) {
  2803. __build_all_zonelists(NULL);
  2804. mminit_verify_zonelist();
  2805. cpuset_init_current_mems_allowed();
  2806. } else {
  2807. /* we have to stop all cpus to guarantee there is no user
  2808. of zonelist */
  2809. #ifdef CONFIG_MEMORY_HOTPLUG
  2810. if (data)
  2811. setup_zone_pageset((struct zone *)data);
  2812. #endif
  2813. stop_machine(__build_all_zonelists, NULL, NULL);
  2814. /* cpuset refresh routine should be here */
  2815. }
  2816. vm_total_pages = nr_free_pagecache_pages();
  2817. /*
  2818. * Disable grouping by mobility if the number of pages in the
  2819. * system is too low to allow the mechanism to work. It would be
  2820. * more accurate, but expensive to check per-zone. This check is
  2821. * made on memory-hotadd so a system can start with mobility
  2822. * disabled and enable it later
  2823. */
  2824. if (vm_total_pages < (pageblock_nr_pages * MIGRATE_TYPES))
  2825. page_group_by_mobility_disabled = 1;
  2826. else
  2827. page_group_by_mobility_disabled = 0;
  2828. printk("Built %i zonelists in %s order, mobility grouping %s. "
  2829. "Total pages: %ld\n",
  2830. nr_online_nodes,
  2831. zonelist_order_name[current_zonelist_order],
  2832. page_group_by_mobility_disabled ? "off" : "on",
  2833. vm_total_pages);
  2834. #ifdef CONFIG_NUMA
  2835. printk("Policy zone: %s\n", zone_names[policy_zone]);
  2836. #endif
  2837. }
  2838. /*
  2839. * Helper functions to size the waitqueue hash table.
  2840. * Essentially these want to choose hash table sizes sufficiently
  2841. * large so that collisions trying to wait on pages are rare.
  2842. * But in fact, the number of active page waitqueues on typical
  2843. * systems is ridiculously low, less than 200. So this is even
  2844. * conservative, even though it seems large.
  2845. *
  2846. * The constant PAGES_PER_WAITQUEUE specifies the ratio of pages to
  2847. * waitqueues, i.e. the size of the waitq table given the number of pages.
  2848. */
  2849. #define PAGES_PER_WAITQUEUE 256
  2850. #ifndef CONFIG_MEMORY_HOTPLUG
  2851. static inline unsigned long wait_table_hash_nr_entries(unsigned long pages)
  2852. {
  2853. unsigned long size = 1;
  2854. pages /= PAGES_PER_WAITQUEUE;
  2855. while (size < pages)
  2856. size <<= 1;
  2857. /*
  2858. * Once we have dozens or even hundreds of threads sleeping
  2859. * on IO we've got bigger problems than wait queue collision.
  2860. * Limit the size of the wait table to a reasonable size.
  2861. */
  2862. size = min(size, 4096UL);
  2863. return max(size, 4UL);
  2864. }
  2865. #else
  2866. /*
  2867. * A zone's size might be changed by hot-add, so it is not possible to determine
  2868. * a suitable size for its wait_table. So we use the maximum size now.
  2869. *
  2870. * The max wait table size = 4096 x sizeof(wait_queue_head_t). ie:
  2871. *
  2872. * i386 (preemption config) : 4096 x 16 = 64Kbyte.
  2873. * ia64, x86-64 (no preemption): 4096 x 20 = 80Kbyte.
  2874. * ia64, x86-64 (preemption) : 4096 x 24 = 96Kbyte.
  2875. *
  2876. * The maximum entries are prepared when a zone's memory is (512K + 256) pages
  2877. * or more by the traditional way. (See above). It equals:
  2878. *
  2879. * i386, x86-64, powerpc(4K page size) : = ( 2G + 1M)byte.
  2880. * ia64(16K page size) : = ( 8G + 4M)byte.
  2881. * powerpc (64K page size) : = (32G +16M)byte.
  2882. */
  2883. static inline unsigned long wait_table_hash_nr_entries(unsigned long pages)
  2884. {
  2885. return 4096UL;
  2886. }
  2887. #endif
  2888. /*
  2889. * This is an integer logarithm so that shifts can be used later
  2890. * to extract the more random high bits from the multiplicative
  2891. * hash function before the remainder is taken.
  2892. */
  2893. static inline unsigned long wait_table_bits(unsigned long size)
  2894. {
  2895. return ffz(~size);
  2896. }
  2897. #define LONG_ALIGN(x) (((x)+(sizeof(long))-1)&~((sizeof(long))-1))
  2898. /*
  2899. * Check if a pageblock contains reserved pages
  2900. */
  2901. static int pageblock_is_reserved(unsigned long start_pfn, unsigned long end_pfn)
  2902. {
  2903. unsigned long pfn;
  2904. for (pfn = start_pfn; pfn < end_pfn; pfn++) {
  2905. if (!pfn_valid_within(pfn) || PageReserved(pfn_to_page(pfn)))
  2906. return 1;
  2907. }
  2908. return 0;
  2909. }
  2910. /*
  2911. * Mark a number of pageblocks as MIGRATE_RESERVE. The number
  2912. * of blocks reserved is based on min_wmark_pages(zone). The memory within
  2913. * the reserve will tend to store contiguous free pages. Setting min_free_kbytes
  2914. * higher will lead to a bigger reserve which will get freed as contiguous
  2915. * blocks as reclaim kicks in
  2916. */
  2917. static void setup_zone_migrate_reserve(struct zone *zone)
  2918. {
  2919. unsigned long start_pfn, pfn, end_pfn, block_end_pfn;
  2920. struct page *page;
  2921. unsigned long block_migratetype;
  2922. int reserve;
  2923. /* Get the start pfn, end pfn and the number of blocks to reserve */
  2924. start_pfn = zone->zone_start_pfn;
  2925. end_pfn = start_pfn + zone->spanned_pages;
  2926. reserve = roundup(min_wmark_pages(zone), pageblock_nr_pages) >>
  2927. pageblock_order;
  2928. /*
  2929. * Reserve blocks are generally in place to help high-order atomic
  2930. * allocations that are short-lived. A min_free_kbytes value that
  2931. * would result in more than 2 reserve blocks for atomic allocations
  2932. * is assumed to be in place to help anti-fragmentation for the
  2933. * future allocation of hugepages at runtime.
  2934. */
  2935. reserve = min(2, reserve);
  2936. for (pfn = start_pfn; pfn < end_pfn; pfn += pageblock_nr_pages) {
  2937. if (!pfn_valid(pfn))
  2938. continue;
  2939. page = pfn_to_page(pfn);
  2940. /* Watch out for overlapping nodes */
  2941. if (page_to_nid(page) != zone_to_nid(zone))
  2942. continue;
  2943. /* Blocks with reserved pages will never free, skip them. */
  2944. block_end_pfn = min(pfn + pageblock_nr_pages, end_pfn);
  2945. if (pageblock_is_reserved(pfn, block_end_pfn))
  2946. continue;
  2947. block_migratetype = get_pageblock_migratetype(page);
  2948. /* If this block is reserved, account for it */
  2949. if (reserve > 0 && block_migratetype == MIGRATE_RESERVE) {
  2950. reserve--;
  2951. continue;
  2952. }
  2953. /* Suitable for reserving if this block is movable */
  2954. if (reserve > 0 && block_migratetype == MIGRATE_MOVABLE) {
  2955. set_pageblock_migratetype(page, MIGRATE_RESERVE);
  2956. move_freepages_block(zone, page, MIGRATE_RESERVE);
  2957. reserve--;
  2958. continue;
  2959. }
  2960. /*
  2961. * If the reserve is met and this is a previous reserved block,
  2962. * take it back
  2963. */
  2964. if (block_migratetype == MIGRATE_RESERVE) {
  2965. set_pageblock_migratetype(page, MIGRATE_MOVABLE);
  2966. move_freepages_block(zone, page, MIGRATE_MOVABLE);
  2967. }
  2968. }
  2969. }
  2970. /*
  2971. * Initially all pages are reserved - free ones are freed
  2972. * up by free_all_bootmem() once the early boot process is
  2973. * done. Non-atomic initialization, single-pass.
  2974. */
  2975. void __meminit memmap_init_zone(unsigned long size, int nid, unsigned long zone,
  2976. unsigned long start_pfn, enum memmap_context context)
  2977. {
  2978. struct page *page;
  2979. unsigned long end_pfn = start_pfn + size;
  2980. unsigned long pfn;
  2981. struct zone *z;
  2982. if (highest_memmap_pfn < end_pfn - 1)
  2983. highest_memmap_pfn = end_pfn - 1;
  2984. z = &NODE_DATA(nid)->node_zones[zone];
  2985. for (pfn = start_pfn; pfn < end_pfn; pfn++) {
  2986. /*
  2987. * There can be holes in boot-time mem_map[]s
  2988. * handed to this function. They do not
  2989. * exist on hotplugged memory.
  2990. */
  2991. if (context == MEMMAP_EARLY) {
  2992. if (!early_pfn_valid(pfn))
  2993. continue;
  2994. if (!early_pfn_in_nid(pfn, nid))
  2995. continue;
  2996. }
  2997. page = pfn_to_page(pfn);
  2998. set_page_links(page, zone, nid, pfn);
  2999. mminit_verify_page_links(page, zone, nid, pfn);
  3000. init_page_count(page);
  3001. reset_page_mapcount(page);
  3002. SetPageReserved(page);
  3003. /*
  3004. * Mark the block movable so that blocks are reserved for
  3005. * movable at startup. This will force kernel allocations
  3006. * to reserve their blocks rather than leaking throughout
  3007. * the address space during boot when many long-lived
  3008. * kernel allocations are made. Later some blocks near
  3009. * the start are marked MIGRATE_RESERVE by
  3010. * setup_zone_migrate_reserve()
  3011. *
  3012. * bitmap is created for zone's valid pfn range. but memmap
  3013. * can be created for invalid pages (for alignment)
  3014. * check here not to call set_pageblock_migratetype() against
  3015. * pfn out of zone.
  3016. */
  3017. if ((z->zone_start_pfn <= pfn)
  3018. && (pfn < z->zone_start_pfn + z->spanned_pages)
  3019. && !(pfn & (pageblock_nr_pages - 1)))
  3020. set_pageblock_migratetype(page, MIGRATE_MOVABLE);
  3021. INIT_LIST_HEAD(&page->lru);
  3022. #ifdef WANT_PAGE_VIRTUAL
  3023. /* The shift won't overflow because ZONE_NORMAL is below 4G. */
  3024. if (!is_highmem_idx(zone))
  3025. set_page_address(page, __va(pfn << PAGE_SHIFT));
  3026. #endif
  3027. }
  3028. }
  3029. static void __meminit zone_init_free_lists(struct zone *zone)
  3030. {
  3031. int order, t;
  3032. for_each_migratetype_order(order, t) {
  3033. INIT_LIST_HEAD(&zone->free_area[order].free_list[t]);
  3034. zone->free_area[order].nr_free = 0;
  3035. }
  3036. }
  3037. #ifndef __HAVE_ARCH_MEMMAP_INIT
  3038. #define memmap_init(size, nid, zone, start_pfn) \
  3039. memmap_init_zone((size), (nid), (zone), (start_pfn), MEMMAP_EARLY)
  3040. #endif
  3041. static int zone_batchsize(struct zone *zone)
  3042. {
  3043. #ifdef CONFIG_MMU
  3044. int batch;
  3045. /*
  3046. * The per-cpu-pages pools are set to around 1000th of the
  3047. * size of the zone. But no more than 1/2 of a meg.
  3048. *
  3049. * OK, so we don't know how big the cache is. So guess.
  3050. */
  3051. batch = zone->present_pages / 1024;
  3052. if (batch * PAGE_SIZE > 512 * 1024)
  3053. batch = (512 * 1024) / PAGE_SIZE;
  3054. batch /= 4; /* We effectively *= 4 below */
  3055. if (batch < 1)
  3056. batch = 1;
  3057. /*
  3058. * Clamp the batch to a 2^n - 1 value. Having a power
  3059. * of 2 value was found to be more likely to have
  3060. * suboptimal cache aliasing properties in some cases.
  3061. *
  3062. * For example if 2 tasks are alternately allocating
  3063. * batches of pages, one task can end up with a lot
  3064. * of pages of one half of the possible page colors
  3065. * and the other with pages of the other colors.
  3066. */
  3067. batch = rounddown_pow_of_two(batch + batch/2) - 1;
  3068. return batch;
  3069. #else
  3070. /* The deferral and batching of frees should be suppressed under NOMMU
  3071. * conditions.
  3072. *
  3073. * The problem is that NOMMU needs to be able to allocate large chunks
  3074. * of contiguous memory as there's no hardware page translation to
  3075. * assemble apparent contiguous memory from discontiguous pages.
  3076. *
  3077. * Queueing large contiguous runs of pages for batching, however,
  3078. * causes the pages to actually be freed in smaller chunks. As there
  3079. * can be a significant delay between the individual batches being
  3080. * recycled, this leads to the once large chunks of space being
  3081. * fragmented and becoming unavailable for high-order allocations.
  3082. */
  3083. return 0;
  3084. #endif
  3085. }
  3086. static void setup_pageset(struct per_cpu_pageset *p, unsigned long batch)
  3087. {
  3088. struct per_cpu_pages *pcp;
  3089. int migratetype;
  3090. memset(p, 0, sizeof(*p));
  3091. pcp = &p->pcp;
  3092. pcp->count = 0;
  3093. pcp->high = 6 * batch;
  3094. pcp->batch = max(1UL, 1 * batch);
  3095. for (migratetype = 0; migratetype < MIGRATE_PCPTYPES; migratetype++)
  3096. INIT_LIST_HEAD(&pcp->lists[migratetype]);
  3097. }
  3098. /*
  3099. * setup_pagelist_highmark() sets the high water mark for hot per_cpu_pagelist
  3100. * to the value high for the pageset p.
  3101. */
  3102. static void setup_pagelist_highmark(struct per_cpu_pageset *p,
  3103. unsigned long high)
  3104. {
  3105. struct per_cpu_pages *pcp;
  3106. pcp = &p->pcp;
  3107. pcp->high = high;
  3108. pcp->batch = max(1UL, high/4);
  3109. if ((high/4) > (PAGE_SHIFT * 8))
  3110. pcp->batch = PAGE_SHIFT * 8;
  3111. }
  3112. static void setup_zone_pageset(struct zone *zone)
  3113. {
  3114. int cpu;
  3115. zone->pageset = alloc_percpu(struct per_cpu_pageset);
  3116. for_each_possible_cpu(cpu) {
  3117. struct per_cpu_pageset *pcp = per_cpu_ptr(zone->pageset, cpu);
  3118. setup_pageset(pcp, zone_batchsize(zone));
  3119. if (percpu_pagelist_fraction)
  3120. setup_pagelist_highmark(pcp,
  3121. (zone->present_pages /
  3122. percpu_pagelist_fraction));
  3123. }
  3124. }
  3125. /*
  3126. * Allocate per cpu pagesets and initialize them.
  3127. * Before this call only boot pagesets were available.
  3128. */
  3129. void __init setup_per_cpu_pageset(void)
  3130. {
  3131. struct zone *zone;
  3132. for_each_populated_zone(zone)
  3133. setup_zone_pageset(zone);
  3134. }
  3135. static noinline __init_refok
  3136. int zone_wait_table_init(struct zone *zone, unsigned long zone_size_pages)
  3137. {
  3138. int i;
  3139. struct pglist_data *pgdat = zone->zone_pgdat;
  3140. size_t alloc_size;
  3141. /*
  3142. * The per-page waitqueue mechanism uses hashed waitqueues
  3143. * per zone.
  3144. */
  3145. zone->wait_table_hash_nr_entries =
  3146. wait_table_hash_nr_entries(zone_size_pages);
  3147. zone->wait_table_bits =
  3148. wait_table_bits(zone->wait_table_hash_nr_entries);
  3149. alloc_size = zone->wait_table_hash_nr_entries
  3150. * sizeof(wait_queue_head_t);
  3151. if (!slab_is_available()) {
  3152. zone->wait_table = (wait_queue_head_t *)
  3153. alloc_bootmem_node_nopanic(pgdat, alloc_size);
  3154. } else {
  3155. /*
  3156. * This case means that a zone whose size was 0 gets new memory
  3157. * via memory hot-add.
  3158. * But it may be the case that a new node was hot-added. In
  3159. * this case vmalloc() will not be able to use this new node's
  3160. * memory - this wait_table must be initialized to use this new
  3161. * node itself as well.
  3162. * To use this new node's memory, further consideration will be
  3163. * necessary.
  3164. */
  3165. zone->wait_table = vmalloc(alloc_size);
  3166. }
  3167. if (!zone->wait_table)
  3168. return -ENOMEM;
  3169. for(i = 0; i < zone->wait_table_hash_nr_entries; ++i)
  3170. init_waitqueue_head(zone->wait_table + i);
  3171. return 0;
  3172. }
  3173. static int __zone_pcp_update(void *data)
  3174. {
  3175. struct zone *zone = data;
  3176. int cpu;
  3177. unsigned long batch = zone_batchsize(zone), flags;
  3178. for_each_possible_cpu(cpu) {
  3179. struct per_cpu_pageset *pset;
  3180. struct per_cpu_pages *pcp;
  3181. pset = per_cpu_ptr(zone->pageset, cpu);
  3182. pcp = &pset->pcp;
  3183. local_irq_save(flags);
  3184. free_pcppages_bulk(zone, pcp->count, pcp);
  3185. setup_pageset(pset, batch);
  3186. local_irq_restore(flags);
  3187. }
  3188. return 0;
  3189. }
  3190. void zone_pcp_update(struct zone *zone)
  3191. {
  3192. stop_machine(__zone_pcp_update, zone, NULL);
  3193. }
  3194. static __meminit void zone_pcp_init(struct zone *zone)
  3195. {
  3196. /*
  3197. * per cpu subsystem is not up at this point. The following code
  3198. * relies on the ability of the linker to provide the
  3199. * offset of a (static) per cpu variable into the per cpu area.
  3200. */
  3201. zone->pageset = &boot_pageset;
  3202. if (zone->present_pages)
  3203. printk(KERN_DEBUG " %s zone: %lu pages, LIFO batch:%u\n",
  3204. zone->name, zone->present_pages,
  3205. zone_batchsize(zone));
  3206. }
  3207. __meminit int init_currently_empty_zone(struct zone *zone,
  3208. unsigned long zone_start_pfn,
  3209. unsigned long size,
  3210. enum memmap_context context)
  3211. {
  3212. struct pglist_data *pgdat = zone->zone_pgdat;
  3213. int ret;
  3214. ret = zone_wait_table_init(zone, size);
  3215. if (ret)
  3216. return ret;
  3217. pgdat->nr_zones = zone_idx(zone) + 1;
  3218. zone->zone_start_pfn = zone_start_pfn;
  3219. mminit_dprintk(MMINIT_TRACE, "memmap_init",
  3220. "Initialising map node %d zone %lu pfns %lu -> %lu\n",
  3221. pgdat->node_id,
  3222. (unsigned long)zone_idx(zone),
  3223. zone_start_pfn, (zone_start_pfn + size));
  3224. zone_init_free_lists(zone);
  3225. return 0;
  3226. }
  3227. #ifdef CONFIG_ARCH_POPULATES_NODE_MAP
  3228. /*
  3229. * Basic iterator support. Return the first range of PFNs for a node
  3230. * Note: nid == MAX_NUMNODES returns first region regardless of node
  3231. */
  3232. static int __meminit first_active_region_index_in_nid(int nid)
  3233. {
  3234. int i;
  3235. for (i = 0; i < nr_nodemap_entries; i++)
  3236. if (nid == MAX_NUMNODES || early_node_map[i].nid == nid)
  3237. return i;
  3238. return -1;
  3239. }
  3240. /*
  3241. * Basic iterator support. Return the next active range of PFNs for a node
  3242. * Note: nid == MAX_NUMNODES returns next region regardless of node
  3243. */
  3244. static int __meminit next_active_region_index_in_nid(int index, int nid)
  3245. {
  3246. for (index = index + 1; index < nr_nodemap_entries; index++)
  3247. if (nid == MAX_NUMNODES || early_node_map[index].nid == nid)
  3248. return index;
  3249. return -1;
  3250. }
  3251. #ifndef CONFIG_HAVE_ARCH_EARLY_PFN_TO_NID
  3252. /*
  3253. * Required by SPARSEMEM. Given a PFN, return what node the PFN is on.
  3254. * Architectures may implement their own version but if add_active_range()
  3255. * was used and there are no special requirements, this is a convenient
  3256. * alternative
  3257. */
  3258. int __meminit __early_pfn_to_nid(unsigned long pfn)
  3259. {
  3260. int i;
  3261. for (i = 0; i < nr_nodemap_entries; i++) {
  3262. unsigned long start_pfn = early_node_map[i].start_pfn;
  3263. unsigned long end_pfn = early_node_map[i].end_pfn;
  3264. if (start_pfn <= pfn && pfn < end_pfn)
  3265. return early_node_map[i].nid;
  3266. }
  3267. /* This is a memory hole */
  3268. return -1;
  3269. }
  3270. #endif /* CONFIG_HAVE_ARCH_EARLY_PFN_TO_NID */
  3271. int __meminit early_pfn_to_nid(unsigned long pfn)
  3272. {
  3273. int nid;
  3274. nid = __early_pfn_to_nid(pfn);
  3275. if (nid >= 0)
  3276. return nid;
  3277. /* just returns 0 */
  3278. return 0;
  3279. }
  3280. #ifdef CONFIG_NODES_SPAN_OTHER_NODES
  3281. bool __meminit early_pfn_in_nid(unsigned long pfn, int node)
  3282. {
  3283. int nid;
  3284. nid = __early_pfn_to_nid(pfn);
  3285. if (nid >= 0 && nid != node)
  3286. return false;
  3287. return true;
  3288. }
  3289. #endif
  3290. /* Basic iterator support to walk early_node_map[] */
  3291. #define for_each_active_range_index_in_nid(i, nid) \
  3292. for (i = first_active_region_index_in_nid(nid); i != -1; \
  3293. i = next_active_region_index_in_nid(i, nid))
  3294. /**
  3295. * free_bootmem_with_active_regions - Call free_bootmem_node for each active range
  3296. * @nid: The node to free memory on. If MAX_NUMNODES, all nodes are freed.
  3297. * @max_low_pfn: The highest PFN that will be passed to free_bootmem_node
  3298. *
  3299. * If an architecture guarantees that all ranges registered with
  3300. * add_active_ranges() contain no holes and may be freed, this
  3301. * this function may be used instead of calling free_bootmem() manually.
  3302. */
  3303. void __init free_bootmem_with_active_regions(int nid,
  3304. unsigned long max_low_pfn)
  3305. {
  3306. int i;
  3307. for_each_active_range_index_in_nid(i, nid) {
  3308. unsigned long size_pages = 0;
  3309. unsigned long end_pfn = early_node_map[i].end_pfn;
  3310. if (early_node_map[i].start_pfn >= max_low_pfn)
  3311. continue;
  3312. if (end_pfn > max_low_pfn)
  3313. end_pfn = max_low_pfn;
  3314. size_pages = end_pfn - early_node_map[i].start_pfn;
  3315. free_bootmem_node(NODE_DATA(early_node_map[i].nid),
  3316. PFN_PHYS(early_node_map[i].start_pfn),
  3317. size_pages << PAGE_SHIFT);
  3318. }
  3319. }
  3320. #ifdef CONFIG_HAVE_MEMBLOCK
  3321. /*
  3322. * Basic iterator support. Return the last range of PFNs for a node
  3323. * Note: nid == MAX_NUMNODES returns last region regardless of node
  3324. */
  3325. static int __meminit last_active_region_index_in_nid(int nid)
  3326. {
  3327. int i;
  3328. for (i = nr_nodemap_entries - 1; i >= 0; i--)
  3329. if (nid == MAX_NUMNODES || early_node_map[i].nid == nid)
  3330. return i;
  3331. return -1;
  3332. }
  3333. /*
  3334. * Basic iterator support. Return the previous active range of PFNs for a node
  3335. * Note: nid == MAX_NUMNODES returns next region regardless of node
  3336. */
  3337. static int __meminit previous_active_region_index_in_nid(int index, int nid)
  3338. {
  3339. for (index = index - 1; index >= 0; index--)
  3340. if (nid == MAX_NUMNODES || early_node_map[index].nid == nid)
  3341. return index;
  3342. return -1;
  3343. }
  3344. #define for_each_active_range_index_in_nid_reverse(i, nid) \
  3345. for (i = last_active_region_index_in_nid(nid); i != -1; \
  3346. i = previous_active_region_index_in_nid(i, nid))
  3347. u64 __init find_memory_core_early(int nid, u64 size, u64 align,
  3348. u64 goal, u64 limit)
  3349. {
  3350. int i;
  3351. /* Need to go over early_node_map to find out good range for node */
  3352. for_each_active_range_index_in_nid_reverse(i, nid) {
  3353. u64 addr;
  3354. u64 ei_start, ei_last;
  3355. u64 final_start, final_end;
  3356. ei_last = early_node_map[i].end_pfn;
  3357. ei_last <<= PAGE_SHIFT;
  3358. ei_start = early_node_map[i].start_pfn;
  3359. ei_start <<= PAGE_SHIFT;
  3360. final_start = max(ei_start, goal);
  3361. final_end = min(ei_last, limit);
  3362. if (final_start >= final_end)
  3363. continue;
  3364. addr = memblock_find_in_range(final_start, final_end, size, align);
  3365. if (addr == MEMBLOCK_ERROR)
  3366. continue;
  3367. return addr;
  3368. }
  3369. return MEMBLOCK_ERROR;
  3370. }
  3371. #endif
  3372. int __init add_from_early_node_map(struct range *range, int az,
  3373. int nr_range, int nid)
  3374. {
  3375. int i;
  3376. u64 start, end;
  3377. /* need to go over early_node_map to find out good range for node */
  3378. for_each_active_range_index_in_nid(i, nid) {
  3379. start = early_node_map[i].start_pfn;
  3380. end = early_node_map[i].end_pfn;
  3381. nr_range = add_range(range, az, nr_range, start, end);
  3382. }
  3383. return nr_range;
  3384. }
  3385. void __init work_with_active_regions(int nid, work_fn_t work_fn, void *data)
  3386. {
  3387. int i;
  3388. int ret;
  3389. for_each_active_range_index_in_nid(i, nid) {
  3390. ret = work_fn(early_node_map[i].start_pfn,
  3391. early_node_map[i].end_pfn, data);
  3392. if (ret)
  3393. break;
  3394. }
  3395. }
  3396. /**
  3397. * sparse_memory_present_with_active_regions - Call memory_present for each active range
  3398. * @nid: The node to call memory_present for. If MAX_NUMNODES, all nodes will be used.
  3399. *
  3400. * If an architecture guarantees that all ranges registered with
  3401. * add_active_ranges() contain no holes and may be freed, this
  3402. * function may be used instead of calling memory_present() manually.
  3403. */
  3404. void __init sparse_memory_present_with_active_regions(int nid)
  3405. {
  3406. int i;
  3407. for_each_active_range_index_in_nid(i, nid)
  3408. memory_present(early_node_map[i].nid,
  3409. early_node_map[i].start_pfn,
  3410. early_node_map[i].end_pfn);
  3411. }
  3412. /**
  3413. * get_pfn_range_for_nid - Return the start and end page frames for a node
  3414. * @nid: The nid to return the range for. If MAX_NUMNODES, the min and max PFN are returned.
  3415. * @start_pfn: Passed by reference. On return, it will have the node start_pfn.
  3416. * @end_pfn: Passed by reference. On return, it will have the node end_pfn.
  3417. *
  3418. * It returns the start and end page frame of a node based on information
  3419. * provided by an arch calling add_active_range(). If called for a node
  3420. * with no available memory, a warning is printed and the start and end
  3421. * PFNs will be 0.
  3422. */
  3423. void __meminit get_pfn_range_for_nid(unsigned int nid,
  3424. unsigned long *start_pfn, unsigned long *end_pfn)
  3425. {
  3426. int i;
  3427. *start_pfn = -1UL;
  3428. *end_pfn = 0;
  3429. for_each_active_range_index_in_nid(i, nid) {
  3430. *start_pfn = min(*start_pfn, early_node_map[i].start_pfn);
  3431. *end_pfn = max(*end_pfn, early_node_map[i].end_pfn);
  3432. }
  3433. if (*start_pfn == -1UL)
  3434. *start_pfn = 0;
  3435. }
  3436. /*
  3437. * This finds a zone that can be used for ZONE_MOVABLE pages. The
  3438. * assumption is made that zones within a node are ordered in monotonic
  3439. * increasing memory addresses so that the "highest" populated zone is used
  3440. */
  3441. static void __init find_usable_zone_for_movable(void)
  3442. {
  3443. int zone_index;
  3444. for (zone_index = MAX_NR_ZONES - 1; zone_index >= 0; zone_index--) {
  3445. if (zone_index == ZONE_MOVABLE)
  3446. continue;
  3447. if (arch_zone_highest_possible_pfn[zone_index] >
  3448. arch_zone_lowest_possible_pfn[zone_index])
  3449. break;
  3450. }
  3451. VM_BUG_ON(zone_index == -1);
  3452. movable_zone = zone_index;
  3453. }
  3454. /*
  3455. * The zone ranges provided by the architecture do not include ZONE_MOVABLE
  3456. * because it is sized independent of architecture. Unlike the other zones,
  3457. * the starting point for ZONE_MOVABLE is not fixed. It may be different
  3458. * in each node depending on the size of each node and how evenly kernelcore
  3459. * is distributed. This helper function adjusts the zone ranges
  3460. * provided by the architecture for a given node by using the end of the
  3461. * highest usable zone for ZONE_MOVABLE. This preserves the assumption that
  3462. * zones within a node are in order of monotonic increases memory addresses
  3463. */
  3464. static void __meminit adjust_zone_range_for_zone_movable(int nid,
  3465. unsigned long zone_type,
  3466. unsigned long node_start_pfn,
  3467. unsigned long node_end_pfn,
  3468. unsigned long *zone_start_pfn,
  3469. unsigned long *zone_end_pfn)
  3470. {
  3471. /* Only adjust if ZONE_MOVABLE is on this node */
  3472. if (zone_movable_pfn[nid]) {
  3473. /* Size ZONE_MOVABLE */
  3474. if (zone_type == ZONE_MOVABLE) {
  3475. *zone_start_pfn = zone_movable_pfn[nid];
  3476. *zone_end_pfn = min(node_end_pfn,
  3477. arch_zone_highest_possible_pfn[movable_zone]);
  3478. /* Adjust for ZONE_MOVABLE starting within this range */
  3479. } else if (*zone_start_pfn < zone_movable_pfn[nid] &&
  3480. *zone_end_pfn > zone_movable_pfn[nid]) {
  3481. *zone_end_pfn = zone_movable_pfn[nid];
  3482. /* Check if this whole range is within ZONE_MOVABLE */
  3483. } else if (*zone_start_pfn >= zone_movable_pfn[nid])
  3484. *zone_start_pfn = *zone_end_pfn;
  3485. }
  3486. }
  3487. /*
  3488. * Return the number of pages a zone spans in a node, including holes
  3489. * present_pages = zone_spanned_pages_in_node() - zone_absent_pages_in_node()
  3490. */
  3491. static unsigned long __meminit zone_spanned_pages_in_node(int nid,
  3492. unsigned long zone_type,
  3493. unsigned long *ignored)
  3494. {
  3495. unsigned long node_start_pfn, node_end_pfn;
  3496. unsigned long zone_start_pfn, zone_end_pfn;
  3497. /* Get the start and end of the node and zone */
  3498. get_pfn_range_for_nid(nid, &node_start_pfn, &node_end_pfn);
  3499. zone_start_pfn = arch_zone_lowest_possible_pfn[zone_type];
  3500. zone_end_pfn = arch_zone_highest_possible_pfn[zone_type];
  3501. adjust_zone_range_for_zone_movable(nid, zone_type,
  3502. node_start_pfn, node_end_pfn,
  3503. &zone_start_pfn, &zone_end_pfn);
  3504. /* Check that this node has pages within the zone's required range */
  3505. if (zone_end_pfn < node_start_pfn || zone_start_pfn > node_end_pfn)
  3506. return 0;
  3507. /* Move the zone boundaries inside the node if necessary */
  3508. zone_end_pfn = min(zone_end_pfn, node_end_pfn);
  3509. zone_start_pfn = max(zone_start_pfn, node_start_pfn);
  3510. /* Return the spanned pages */
  3511. return zone_end_pfn - zone_start_pfn;
  3512. }
  3513. /*
  3514. * Return the number of holes in a range on a node. If nid is MAX_NUMNODES,
  3515. * then all holes in the requested range will be accounted for.
  3516. */
  3517. unsigned long __meminit __absent_pages_in_range(int nid,
  3518. unsigned long range_start_pfn,
  3519. unsigned long range_end_pfn)
  3520. {
  3521. int i = 0;
  3522. unsigned long prev_end_pfn = 0, hole_pages = 0;
  3523. unsigned long start_pfn;
  3524. /* Find the end_pfn of the first active range of pfns in the node */
  3525. i = first_active_region_index_in_nid(nid);
  3526. if (i == -1)
  3527. return 0;
  3528. prev_end_pfn = min(early_node_map[i].start_pfn, range_end_pfn);
  3529. /* Account for ranges before physical memory on this node */
  3530. if (early_node_map[i].start_pfn > range_start_pfn)
  3531. hole_pages = prev_end_pfn - range_start_pfn;
  3532. /* Find all holes for the zone within the node */
  3533. for (; i != -1; i = next_active_region_index_in_nid(i, nid)) {
  3534. /* No need to continue if prev_end_pfn is outside the zone */
  3535. if (prev_end_pfn >= range_end_pfn)
  3536. break;
  3537. /* Make sure the end of the zone is not within the hole */
  3538. start_pfn = min(early_node_map[i].start_pfn, range_end_pfn);
  3539. prev_end_pfn = max(prev_end_pfn, range_start_pfn);
  3540. /* Update the hole size cound and move on */
  3541. if (start_pfn > range_start_pfn) {
  3542. BUG_ON(prev_end_pfn > start_pfn);
  3543. hole_pages += start_pfn - prev_end_pfn;
  3544. }
  3545. prev_end_pfn = early_node_map[i].end_pfn;
  3546. }
  3547. /* Account for ranges past physical memory on this node */
  3548. if (range_end_pfn > prev_end_pfn)
  3549. hole_pages += range_end_pfn -
  3550. max(range_start_pfn, prev_end_pfn);
  3551. return hole_pages;
  3552. }
  3553. /**
  3554. * absent_pages_in_range - Return number of page frames in holes within a range
  3555. * @start_pfn: The start PFN to start searching for holes
  3556. * @end_pfn: The end PFN to stop searching for holes
  3557. *
  3558. * It returns the number of pages frames in memory holes within a range.
  3559. */
  3560. unsigned long __init absent_pages_in_range(unsigned long start_pfn,
  3561. unsigned long end_pfn)
  3562. {
  3563. return __absent_pages_in_range(MAX_NUMNODES, start_pfn, end_pfn);
  3564. }
  3565. /* Return the number of page frames in holes in a zone on a node */
  3566. static unsigned long __meminit zone_absent_pages_in_node(int nid,
  3567. unsigned long zone_type,
  3568. unsigned long *ignored)
  3569. {
  3570. unsigned long node_start_pfn, node_end_pfn;
  3571. unsigned long zone_start_pfn, zone_end_pfn;
  3572. get_pfn_range_for_nid(nid, &node_start_pfn, &node_end_pfn);
  3573. zone_start_pfn = max(arch_zone_lowest_possible_pfn[zone_type],
  3574. node_start_pfn);
  3575. zone_end_pfn = min(arch_zone_highest_possible_pfn[zone_type],
  3576. node_end_pfn);
  3577. adjust_zone_range_for_zone_movable(nid, zone_type,
  3578. node_start_pfn, node_end_pfn,
  3579. &zone_start_pfn, &zone_end_pfn);
  3580. return __absent_pages_in_range(nid, zone_start_pfn, zone_end_pfn);
  3581. }
  3582. #else
  3583. static inline unsigned long __meminit zone_spanned_pages_in_node(int nid,
  3584. unsigned long zone_type,
  3585. unsigned long *zones_size)
  3586. {
  3587. return zones_size[zone_type];
  3588. }
  3589. static inline unsigned long __meminit zone_absent_pages_in_node(int nid,
  3590. unsigned long zone_type,
  3591. unsigned long *zholes_size)
  3592. {
  3593. if (!zholes_size)
  3594. return 0;
  3595. return zholes_size[zone_type];
  3596. }
  3597. #endif
  3598. static void __meminit calculate_node_totalpages(struct pglist_data *pgdat,
  3599. unsigned long *zones_size, unsigned long *zholes_size)
  3600. {
  3601. unsigned long realtotalpages, totalpages = 0;
  3602. enum zone_type i;
  3603. for (i = 0; i < MAX_NR_ZONES; i++)
  3604. totalpages += zone_spanned_pages_in_node(pgdat->node_id, i,
  3605. zones_size);
  3606. pgdat->node_spanned_pages = totalpages;
  3607. realtotalpages = totalpages;
  3608. for (i = 0; i < MAX_NR_ZONES; i++)
  3609. realtotalpages -=
  3610. zone_absent_pages_in_node(pgdat->node_id, i,
  3611. zholes_size);
  3612. pgdat->node_present_pages = realtotalpages;
  3613. printk(KERN_DEBUG "On node %d totalpages: %lu\n", pgdat->node_id,
  3614. realtotalpages);
  3615. }
  3616. #ifndef CONFIG_SPARSEMEM
  3617. /*
  3618. * Calculate the size of the zone->blockflags rounded to an unsigned long
  3619. * Start by making sure zonesize is a multiple of pageblock_order by rounding
  3620. * up. Then use 1 NR_PAGEBLOCK_BITS worth of bits per pageblock, finally
  3621. * round what is now in bits to nearest long in bits, then return it in
  3622. * bytes.
  3623. */
  3624. static unsigned long __init usemap_size(unsigned long zonesize)
  3625. {
  3626. unsigned long usemapsize;
  3627. usemapsize = roundup(zonesize, pageblock_nr_pages);
  3628. usemapsize = usemapsize >> pageblock_order;
  3629. usemapsize *= NR_PAGEBLOCK_BITS;
  3630. usemapsize = roundup(usemapsize, 8 * sizeof(unsigned long));
  3631. return usemapsize / 8;
  3632. }
  3633. static void __init setup_usemap(struct pglist_data *pgdat,
  3634. struct zone *zone, unsigned long zonesize)
  3635. {
  3636. unsigned long usemapsize = usemap_size(zonesize);
  3637. zone->pageblock_flags = NULL;
  3638. if (usemapsize)
  3639. zone->pageblock_flags = alloc_bootmem_node_nopanic(pgdat,
  3640. usemapsize);
  3641. }
  3642. #else
  3643. static inline void setup_usemap(struct pglist_data *pgdat,
  3644. struct zone *zone, unsigned long zonesize) {}
  3645. #endif /* CONFIG_SPARSEMEM */
  3646. #ifdef CONFIG_HUGETLB_PAGE_SIZE_VARIABLE
  3647. /* Return a sensible default order for the pageblock size. */
  3648. static inline int pageblock_default_order(void)
  3649. {
  3650. if (HPAGE_SHIFT > PAGE_SHIFT)
  3651. return HUGETLB_PAGE_ORDER;
  3652. return MAX_ORDER-1;
  3653. }
  3654. /* Initialise the number of pages represented by NR_PAGEBLOCK_BITS */
  3655. static inline void __init set_pageblock_order(unsigned int order)
  3656. {
  3657. /* Check that pageblock_nr_pages has not already been setup */
  3658. if (pageblock_order)
  3659. return;
  3660. /*
  3661. * Assume the largest contiguous order of interest is a huge page.
  3662. * This value may be variable depending on boot parameters on IA64
  3663. */
  3664. pageblock_order = order;
  3665. }
  3666. #else /* CONFIG_HUGETLB_PAGE_SIZE_VARIABLE */
  3667. /*
  3668. * When CONFIG_HUGETLB_PAGE_SIZE_VARIABLE is not set, set_pageblock_order()
  3669. * and pageblock_default_order() are unused as pageblock_order is set
  3670. * at compile-time. See include/linux/pageblock-flags.h for the values of
  3671. * pageblock_order based on the kernel config
  3672. */
  3673. static inline int pageblock_default_order(unsigned int order)
  3674. {
  3675. return MAX_ORDER-1;
  3676. }
  3677. #define set_pageblock_order(x) do {} while (0)
  3678. #endif /* CONFIG_HUGETLB_PAGE_SIZE_VARIABLE */
  3679. /*
  3680. * Set up the zone data structures:
  3681. * - mark all pages reserved
  3682. * - mark all memory queues empty
  3683. * - clear the memory bitmaps
  3684. */
  3685. static void __paginginit free_area_init_core(struct pglist_data *pgdat,
  3686. unsigned long *zones_size, unsigned long *zholes_size)
  3687. {
  3688. enum zone_type j;
  3689. int nid = pgdat->node_id;
  3690. unsigned long zone_start_pfn = pgdat->node_start_pfn;
  3691. int ret;
  3692. pgdat_resize_init(pgdat);
  3693. pgdat->nr_zones = 0;
  3694. init_waitqueue_head(&pgdat->kswapd_wait);
  3695. pgdat->kswapd_max_order = 0;
  3696. pgdat_page_cgroup_init(pgdat);
  3697. for (j = 0; j < MAX_NR_ZONES; j++) {
  3698. struct zone *zone = pgdat->node_zones + j;
  3699. unsigned long size, realsize, memmap_pages;
  3700. enum lru_list l;
  3701. size = zone_spanned_pages_in_node(nid, j, zones_size);
  3702. realsize = size - zone_absent_pages_in_node(nid, j,
  3703. zholes_size);
  3704. /*
  3705. * Adjust realsize so that it accounts for how much memory
  3706. * is used by this zone for memmap. This affects the watermark
  3707. * and per-cpu initialisations
  3708. */
  3709. memmap_pages =
  3710. PAGE_ALIGN(size * sizeof(struct page)) >> PAGE_SHIFT;
  3711. if (realsize >= memmap_pages) {
  3712. realsize -= memmap_pages;
  3713. if (memmap_pages)
  3714. printk(KERN_DEBUG
  3715. " %s zone: %lu pages used for memmap\n",
  3716. zone_names[j], memmap_pages);
  3717. } else
  3718. printk(KERN_WARNING
  3719. " %s zone: %lu pages exceeds realsize %lu\n",
  3720. zone_names[j], memmap_pages, realsize);
  3721. /* Account for reserved pages */
  3722. if (j == 0 && realsize > dma_reserve) {
  3723. realsize -= dma_reserve;
  3724. printk(KERN_DEBUG " %s zone: %lu pages reserved\n",
  3725. zone_names[0], dma_reserve);
  3726. }
  3727. if (!is_highmem_idx(j))
  3728. nr_kernel_pages += realsize;
  3729. nr_all_pages += realsize;
  3730. zone->spanned_pages = size;
  3731. zone->present_pages = realsize;
  3732. #ifdef CONFIG_NUMA
  3733. zone->node = nid;
  3734. zone->min_unmapped_pages = (realsize*sysctl_min_unmapped_ratio)
  3735. / 100;
  3736. zone->min_slab_pages = (realsize * sysctl_min_slab_ratio) / 100;
  3737. #endif
  3738. zone->name = zone_names[j];
  3739. spin_lock_init(&zone->lock);
  3740. spin_lock_init(&zone->lru_lock);
  3741. zone_seqlock_init(zone);
  3742. zone->zone_pgdat = pgdat;
  3743. zone_pcp_init(zone);
  3744. for_each_lru(l)
  3745. INIT_LIST_HEAD(&zone->lru[l].list);
  3746. zone->reclaim_stat.recent_rotated[0] = 0;
  3747. zone->reclaim_stat.recent_rotated[1] = 0;
  3748. zone->reclaim_stat.recent_scanned[0] = 0;
  3749. zone->reclaim_stat.recent_scanned[1] = 0;
  3750. zap_zone_vm_stats(zone);
  3751. zone->flags = 0;
  3752. if (!size)
  3753. continue;
  3754. set_pageblock_order(pageblock_default_order());
  3755. setup_usemap(pgdat, zone, size);
  3756. ret = init_currently_empty_zone(zone, zone_start_pfn,
  3757. size, MEMMAP_EARLY);
  3758. BUG_ON(ret);
  3759. memmap_init(size, nid, j, zone_start_pfn);
  3760. zone_start_pfn += size;
  3761. }
  3762. }
  3763. static void __init_refok alloc_node_mem_map(struct pglist_data *pgdat)
  3764. {
  3765. /* Skip empty nodes */
  3766. if (!pgdat->node_spanned_pages)
  3767. return;
  3768. #ifdef CONFIG_FLAT_NODE_MEM_MAP
  3769. /* ia64 gets its own node_mem_map, before this, without bootmem */
  3770. if (!pgdat->node_mem_map) {
  3771. unsigned long size, start, end;
  3772. struct page *map;
  3773. /*
  3774. * The zone's endpoints aren't required to be MAX_ORDER
  3775. * aligned but the node_mem_map endpoints must be in order
  3776. * for the buddy allocator to function correctly.
  3777. */
  3778. start = pgdat->node_start_pfn & ~(MAX_ORDER_NR_PAGES - 1);
  3779. end = pgdat->node_start_pfn + pgdat->node_spanned_pages;
  3780. end = ALIGN(end, MAX_ORDER_NR_PAGES);
  3781. size = (end - start) * sizeof(struct page);
  3782. map = alloc_remap(pgdat->node_id, size);
  3783. if (!map)
  3784. map = alloc_bootmem_node_nopanic(pgdat, size);
  3785. pgdat->node_mem_map = map + (pgdat->node_start_pfn - start);
  3786. }
  3787. #ifndef CONFIG_NEED_MULTIPLE_NODES
  3788. /*
  3789. * With no DISCONTIG, the global mem_map is just set as node 0's
  3790. */
  3791. if (pgdat == NODE_DATA(0)) {
  3792. mem_map = NODE_DATA(0)->node_mem_map;
  3793. #ifdef CONFIG_ARCH_POPULATES_NODE_MAP
  3794. if (page_to_pfn(mem_map) != pgdat->node_start_pfn)
  3795. mem_map -= (pgdat->node_start_pfn - ARCH_PFN_OFFSET);
  3796. #endif /* CONFIG_ARCH_POPULATES_NODE_MAP */
  3797. }
  3798. #endif
  3799. #endif /* CONFIG_FLAT_NODE_MEM_MAP */
  3800. }
  3801. void __paginginit free_area_init_node(int nid, unsigned long *zones_size,
  3802. unsigned long node_start_pfn, unsigned long *zholes_size)
  3803. {
  3804. pg_data_t *pgdat = NODE_DATA(nid);
  3805. pgdat->node_id = nid;
  3806. pgdat->node_start_pfn = node_start_pfn;
  3807. calculate_node_totalpages(pgdat, zones_size, zholes_size);
  3808. alloc_node_mem_map(pgdat);
  3809. #ifdef CONFIG_FLAT_NODE_MEM_MAP
  3810. printk(KERN_DEBUG "free_area_init_node: node %d, pgdat %08lx, node_mem_map %08lx\n",
  3811. nid, (unsigned long)pgdat,
  3812. (unsigned long)pgdat->node_mem_map);
  3813. #endif
  3814. free_area_init_core(pgdat, zones_size, zholes_size);
  3815. }
  3816. #ifdef CONFIG_ARCH_POPULATES_NODE_MAP
  3817. #if MAX_NUMNODES > 1
  3818. /*
  3819. * Figure out the number of possible node ids.
  3820. */
  3821. static void __init setup_nr_node_ids(void)
  3822. {
  3823. unsigned int node;
  3824. unsigned int highest = 0;
  3825. for_each_node_mask(node, node_possible_map)
  3826. highest = node;
  3827. nr_node_ids = highest + 1;
  3828. }
  3829. #else
  3830. static inline void setup_nr_node_ids(void)
  3831. {
  3832. }
  3833. #endif
  3834. /**
  3835. * add_active_range - Register a range of PFNs backed by physical memory
  3836. * @nid: The node ID the range resides on
  3837. * @start_pfn: The start PFN of the available physical memory
  3838. * @end_pfn: The end PFN of the available physical memory
  3839. *
  3840. * These ranges are stored in an early_node_map[] and later used by
  3841. * free_area_init_nodes() to calculate zone sizes and holes. If the
  3842. * range spans a memory hole, it is up to the architecture to ensure
  3843. * the memory is not freed by the bootmem allocator. If possible
  3844. * the range being registered will be merged with existing ranges.
  3845. */
  3846. void __init add_active_range(unsigned int nid, unsigned long start_pfn,
  3847. unsigned long end_pfn)
  3848. {
  3849. int i;
  3850. mminit_dprintk(MMINIT_TRACE, "memory_register",
  3851. "Entering add_active_range(%d, %#lx, %#lx) "
  3852. "%d entries of %d used\n",
  3853. nid, start_pfn, end_pfn,
  3854. nr_nodemap_entries, MAX_ACTIVE_REGIONS);
  3855. mminit_validate_memmodel_limits(&start_pfn, &end_pfn);
  3856. /* Merge with existing active regions if possible */
  3857. for (i = 0; i < nr_nodemap_entries; i++) {
  3858. if (early_node_map[i].nid != nid)
  3859. continue;
  3860. /* Skip if an existing region covers this new one */
  3861. if (start_pfn >= early_node_map[i].start_pfn &&
  3862. end_pfn <= early_node_map[i].end_pfn)
  3863. return;
  3864. /* Merge forward if suitable */
  3865. if (start_pfn <= early_node_map[i].end_pfn &&
  3866. end_pfn > early_node_map[i].end_pfn) {
  3867. early_node_map[i].end_pfn = end_pfn;
  3868. return;
  3869. }
  3870. /* Merge backward if suitable */
  3871. if (start_pfn < early_node_map[i].start_pfn &&
  3872. end_pfn >= early_node_map[i].start_pfn) {
  3873. early_node_map[i].start_pfn = start_pfn;
  3874. return;
  3875. }
  3876. }
  3877. /* Check that early_node_map is large enough */
  3878. if (i >= MAX_ACTIVE_REGIONS) {
  3879. printk(KERN_CRIT "More than %d memory regions, truncating\n",
  3880. MAX_ACTIVE_REGIONS);
  3881. return;
  3882. }
  3883. early_node_map[i].nid = nid;
  3884. early_node_map[i].start_pfn = start_pfn;
  3885. early_node_map[i].end_pfn = end_pfn;
  3886. nr_nodemap_entries = i + 1;
  3887. }
  3888. /**
  3889. * remove_active_range - Shrink an existing registered range of PFNs
  3890. * @nid: The node id the range is on that should be shrunk
  3891. * @start_pfn: The new PFN of the range
  3892. * @end_pfn: The new PFN of the range
  3893. *
  3894. * i386 with NUMA use alloc_remap() to store a node_mem_map on a local node.
  3895. * The map is kept near the end physical page range that has already been
  3896. * registered. This function allows an arch to shrink an existing registered
  3897. * range.
  3898. */
  3899. void __init remove_active_range(unsigned int nid, unsigned long start_pfn,
  3900. unsigned long end_pfn)
  3901. {
  3902. int i, j;
  3903. int removed = 0;
  3904. printk(KERN_DEBUG "remove_active_range (%d, %lu, %lu)\n",
  3905. nid, start_pfn, end_pfn);
  3906. /* Find the old active region end and shrink */
  3907. for_each_active_range_index_in_nid(i, nid) {
  3908. if (early_node_map[i].start_pfn >= start_pfn &&
  3909. early_node_map[i].end_pfn <= end_pfn) {
  3910. /* clear it */
  3911. early_node_map[i].start_pfn = 0;
  3912. early_node_map[i].end_pfn = 0;
  3913. removed = 1;
  3914. continue;
  3915. }
  3916. if (early_node_map[i].start_pfn < start_pfn &&
  3917. early_node_map[i].end_pfn > start_pfn) {
  3918. unsigned long temp_end_pfn = early_node_map[i].end_pfn;
  3919. early_node_map[i].end_pfn = start_pfn;
  3920. if (temp_end_pfn > end_pfn)
  3921. add_active_range(nid, end_pfn, temp_end_pfn);
  3922. continue;
  3923. }
  3924. if (early_node_map[i].start_pfn >= start_pfn &&
  3925. early_node_map[i].end_pfn > end_pfn &&
  3926. early_node_map[i].start_pfn < end_pfn) {
  3927. early_node_map[i].start_pfn = end_pfn;
  3928. continue;
  3929. }
  3930. }
  3931. if (!removed)
  3932. return;
  3933. /* remove the blank ones */
  3934. for (i = nr_nodemap_entries - 1; i > 0; i--) {
  3935. if (early_node_map[i].nid != nid)
  3936. continue;
  3937. if (early_node_map[i].end_pfn)
  3938. continue;
  3939. /* we found it, get rid of it */
  3940. for (j = i; j < nr_nodemap_entries - 1; j++)
  3941. memcpy(&early_node_map[j], &early_node_map[j+1],
  3942. sizeof(early_node_map[j]));
  3943. j = nr_nodemap_entries - 1;
  3944. memset(&early_node_map[j], 0, sizeof(early_node_map[j]));
  3945. nr_nodemap_entries--;
  3946. }
  3947. }
  3948. /**
  3949. * remove_all_active_ranges - Remove all currently registered regions
  3950. *
  3951. * During discovery, it may be found that a table like SRAT is invalid
  3952. * and an alternative discovery method must be used. This function removes
  3953. * all currently registered regions.
  3954. */
  3955. void __init remove_all_active_ranges(void)
  3956. {
  3957. memset(early_node_map, 0, sizeof(early_node_map));
  3958. nr_nodemap_entries = 0;
  3959. }
  3960. /* Compare two active node_active_regions */
  3961. static int __init cmp_node_active_region(const void *a, const void *b)
  3962. {
  3963. struct node_active_region *arange = (struct node_active_region *)a;
  3964. struct node_active_region *brange = (struct node_active_region *)b;
  3965. /* Done this way to avoid overflows */
  3966. if (arange->start_pfn > brange->start_pfn)
  3967. return 1;
  3968. if (arange->start_pfn < brange->start_pfn)
  3969. return -1;
  3970. return 0;
  3971. }
  3972. /* sort the node_map by start_pfn */
  3973. void __init sort_node_map(void)
  3974. {
  3975. sort(early_node_map, (size_t)nr_nodemap_entries,
  3976. sizeof(struct node_active_region),
  3977. cmp_node_active_region, NULL);
  3978. }
  3979. /**
  3980. * node_map_pfn_alignment - determine the maximum internode alignment
  3981. *
  3982. * This function should be called after node map is populated and sorted.
  3983. * It calculates the maximum power of two alignment which can distinguish
  3984. * all the nodes.
  3985. *
  3986. * For example, if all nodes are 1GiB and aligned to 1GiB, the return value
  3987. * would indicate 1GiB alignment with (1 << (30 - PAGE_SHIFT)). If the
  3988. * nodes are shifted by 256MiB, 256MiB. Note that if only the last node is
  3989. * shifted, 1GiB is enough and this function will indicate so.
  3990. *
  3991. * This is used to test whether pfn -> nid mapping of the chosen memory
  3992. * model has fine enough granularity to avoid incorrect mapping for the
  3993. * populated node map.
  3994. *
  3995. * Returns the determined alignment in pfn's. 0 if there is no alignment
  3996. * requirement (single node).
  3997. */
  3998. unsigned long __init node_map_pfn_alignment(void)
  3999. {
  4000. unsigned long accl_mask = 0, last_end = 0;
  4001. int last_nid = -1;
  4002. int i;
  4003. for_each_active_range_index_in_nid(i, MAX_NUMNODES) {
  4004. int nid = early_node_map[i].nid;
  4005. unsigned long start = early_node_map[i].start_pfn;
  4006. unsigned long end = early_node_map[i].end_pfn;
  4007. unsigned long mask;
  4008. if (!start || last_nid < 0 || last_nid == nid) {
  4009. last_nid = nid;
  4010. last_end = end;
  4011. continue;
  4012. }
  4013. /*
  4014. * Start with a mask granular enough to pin-point to the
  4015. * start pfn and tick off bits one-by-one until it becomes
  4016. * too coarse to separate the current node from the last.
  4017. */
  4018. mask = ~((1 << __ffs(start)) - 1);
  4019. while (mask && last_end <= (start & (mask << 1)))
  4020. mask <<= 1;
  4021. /* accumulate all internode masks */
  4022. accl_mask |= mask;
  4023. }
  4024. /* convert mask to number of pages */
  4025. return ~accl_mask + 1;
  4026. }
  4027. /* Find the lowest pfn for a node */
  4028. static unsigned long __init find_min_pfn_for_node(int nid)
  4029. {
  4030. int i;
  4031. unsigned long min_pfn = ULONG_MAX;
  4032. /* Assuming a sorted map, the first range found has the starting pfn */
  4033. for_each_active_range_index_in_nid(i, nid)
  4034. min_pfn = min(min_pfn, early_node_map[i].start_pfn);
  4035. if (min_pfn == ULONG_MAX) {
  4036. printk(KERN_WARNING
  4037. "Could not find start_pfn for node %d\n", nid);
  4038. return 0;
  4039. }
  4040. return min_pfn;
  4041. }
  4042. /**
  4043. * find_min_pfn_with_active_regions - Find the minimum PFN registered
  4044. *
  4045. * It returns the minimum PFN based on information provided via
  4046. * add_active_range().
  4047. */
  4048. unsigned long __init find_min_pfn_with_active_regions(void)
  4049. {
  4050. return find_min_pfn_for_node(MAX_NUMNODES);
  4051. }
  4052. /*
  4053. * early_calculate_totalpages()
  4054. * Sum pages in active regions for movable zone.
  4055. * Populate N_HIGH_MEMORY for calculating usable_nodes.
  4056. */
  4057. static unsigned long __init early_calculate_totalpages(void)
  4058. {
  4059. int i;
  4060. unsigned long totalpages = 0;
  4061. for (i = 0; i < nr_nodemap_entries; i++) {
  4062. unsigned long pages = early_node_map[i].end_pfn -
  4063. early_node_map[i].start_pfn;
  4064. totalpages += pages;
  4065. if (pages)
  4066. node_set_state(early_node_map[i].nid, N_HIGH_MEMORY);
  4067. }
  4068. return totalpages;
  4069. }
  4070. /*
  4071. * Find the PFN the Movable zone begins in each node. Kernel memory
  4072. * is spread evenly between nodes as long as the nodes have enough
  4073. * memory. When they don't, some nodes will have more kernelcore than
  4074. * others
  4075. */
  4076. static void __init find_zone_movable_pfns_for_nodes(unsigned long *movable_pfn)
  4077. {
  4078. int i, nid;
  4079. unsigned long usable_startpfn;
  4080. unsigned long kernelcore_node, kernelcore_remaining;
  4081. /* save the state before borrow the nodemask */
  4082. nodemask_t saved_node_state = node_states[N_HIGH_MEMORY];
  4083. unsigned long totalpages = early_calculate_totalpages();
  4084. int usable_nodes = nodes_weight(node_states[N_HIGH_MEMORY]);
  4085. /*
  4086. * If movablecore was specified, calculate what size of
  4087. * kernelcore that corresponds so that memory usable for
  4088. * any allocation type is evenly spread. If both kernelcore
  4089. * and movablecore are specified, then the value of kernelcore
  4090. * will be used for required_kernelcore if it's greater than
  4091. * what movablecore would have allowed.
  4092. */
  4093. if (required_movablecore) {
  4094. unsigned long corepages;
  4095. /*
  4096. * Round-up so that ZONE_MOVABLE is at least as large as what
  4097. * was requested by the user
  4098. */
  4099. required_movablecore =
  4100. roundup(required_movablecore, MAX_ORDER_NR_PAGES);
  4101. corepages = totalpages - required_movablecore;
  4102. required_kernelcore = max(required_kernelcore, corepages);
  4103. }
  4104. /* If kernelcore was not specified, there is no ZONE_MOVABLE */
  4105. if (!required_kernelcore)
  4106. goto out;
  4107. /* usable_startpfn is the lowest possible pfn ZONE_MOVABLE can be at */
  4108. find_usable_zone_for_movable();
  4109. usable_startpfn = arch_zone_lowest_possible_pfn[movable_zone];
  4110. restart:
  4111. /* Spread kernelcore memory as evenly as possible throughout nodes */
  4112. kernelcore_node = required_kernelcore / usable_nodes;
  4113. for_each_node_state(nid, N_HIGH_MEMORY) {
  4114. /*
  4115. * Recalculate kernelcore_node if the division per node
  4116. * now exceeds what is necessary to satisfy the requested
  4117. * amount of memory for the kernel
  4118. */
  4119. if (required_kernelcore < kernelcore_node)
  4120. kernelcore_node = required_kernelcore / usable_nodes;
  4121. /*
  4122. * As the map is walked, we track how much memory is usable
  4123. * by the kernel using kernelcore_remaining. When it is
  4124. * 0, the rest of the node is usable by ZONE_MOVABLE
  4125. */
  4126. kernelcore_remaining = kernelcore_node;
  4127. /* Go through each range of PFNs within this node */
  4128. for_each_active_range_index_in_nid(i, nid) {
  4129. unsigned long start_pfn, end_pfn;
  4130. unsigned long size_pages;
  4131. start_pfn = max(early_node_map[i].start_pfn,
  4132. zone_movable_pfn[nid]);
  4133. end_pfn = early_node_map[i].end_pfn;
  4134. if (start_pfn >= end_pfn)
  4135. continue;
  4136. /* Account for what is only usable for kernelcore */
  4137. if (start_pfn < usable_startpfn) {
  4138. unsigned long kernel_pages;
  4139. kernel_pages = min(end_pfn, usable_startpfn)
  4140. - start_pfn;
  4141. kernelcore_remaining -= min(kernel_pages,
  4142. kernelcore_remaining);
  4143. required_kernelcore -= min(kernel_pages,
  4144. required_kernelcore);
  4145. /* Continue if range is now fully accounted */
  4146. if (end_pfn <= usable_startpfn) {
  4147. /*
  4148. * Push zone_movable_pfn to the end so
  4149. * that if we have to rebalance
  4150. * kernelcore across nodes, we will
  4151. * not double account here
  4152. */
  4153. zone_movable_pfn[nid] = end_pfn;
  4154. continue;
  4155. }
  4156. start_pfn = usable_startpfn;
  4157. }
  4158. /*
  4159. * The usable PFN range for ZONE_MOVABLE is from
  4160. * start_pfn->end_pfn. Calculate size_pages as the
  4161. * number of pages used as kernelcore
  4162. */
  4163. size_pages = end_pfn - start_pfn;
  4164. if (size_pages > kernelcore_remaining)
  4165. size_pages = kernelcore_remaining;
  4166. zone_movable_pfn[nid] = start_pfn + size_pages;
  4167. /*
  4168. * Some kernelcore has been met, update counts and
  4169. * break if the kernelcore for this node has been
  4170. * satisified
  4171. */
  4172. required_kernelcore -= min(required_kernelcore,
  4173. size_pages);
  4174. kernelcore_remaining -= size_pages;
  4175. if (!kernelcore_remaining)
  4176. break;
  4177. }
  4178. }
  4179. /*
  4180. * If there is still required_kernelcore, we do another pass with one
  4181. * less node in the count. This will push zone_movable_pfn[nid] further
  4182. * along on the nodes that still have memory until kernelcore is
  4183. * satisified
  4184. */
  4185. usable_nodes--;
  4186. if (usable_nodes && required_kernelcore > usable_nodes)
  4187. goto restart;
  4188. /* Align start of ZONE_MOVABLE on all nids to MAX_ORDER_NR_PAGES */
  4189. for (nid = 0; nid < MAX_NUMNODES; nid++)
  4190. zone_movable_pfn[nid] =
  4191. roundup(zone_movable_pfn[nid], MAX_ORDER_NR_PAGES);
  4192. out:
  4193. /* restore the node_state */
  4194. node_states[N_HIGH_MEMORY] = saved_node_state;
  4195. }
  4196. /* Any regular memory on that node ? */
  4197. static void check_for_regular_memory(pg_data_t *pgdat)
  4198. {
  4199. #ifdef CONFIG_HIGHMEM
  4200. enum zone_type zone_type;
  4201. for (zone_type = 0; zone_type <= ZONE_NORMAL; zone_type++) {
  4202. struct zone *zone = &pgdat->node_zones[zone_type];
  4203. if (zone->present_pages)
  4204. node_set_state(zone_to_nid(zone), N_NORMAL_MEMORY);
  4205. }
  4206. #endif
  4207. }
  4208. /**
  4209. * free_area_init_nodes - Initialise all pg_data_t and zone data
  4210. * @max_zone_pfn: an array of max PFNs for each zone
  4211. *
  4212. * This will call free_area_init_node() for each active node in the system.
  4213. * Using the page ranges provided by add_active_range(), the size of each
  4214. * zone in each node and their holes is calculated. If the maximum PFN
  4215. * between two adjacent zones match, it is assumed that the zone is empty.
  4216. * For example, if arch_max_dma_pfn == arch_max_dma32_pfn, it is assumed
  4217. * that arch_max_dma32_pfn has no pages. It is also assumed that a zone
  4218. * starts where the previous one ended. For example, ZONE_DMA32 starts
  4219. * at arch_max_dma_pfn.
  4220. */
  4221. void __init free_area_init_nodes(unsigned long *max_zone_pfn)
  4222. {
  4223. unsigned long nid;
  4224. int i;
  4225. /* Sort early_node_map as initialisation assumes it is sorted */
  4226. sort_node_map();
  4227. /* Record where the zone boundaries are */
  4228. memset(arch_zone_lowest_possible_pfn, 0,
  4229. sizeof(arch_zone_lowest_possible_pfn));
  4230. memset(arch_zone_highest_possible_pfn, 0,
  4231. sizeof(arch_zone_highest_possible_pfn));
  4232. arch_zone_lowest_possible_pfn[0] = find_min_pfn_with_active_regions();
  4233. arch_zone_highest_possible_pfn[0] = max_zone_pfn[0];
  4234. for (i = 1; i < MAX_NR_ZONES; i++) {
  4235. if (i == ZONE_MOVABLE)
  4236. continue;
  4237. arch_zone_lowest_possible_pfn[i] =
  4238. arch_zone_highest_possible_pfn[i-1];
  4239. arch_zone_highest_possible_pfn[i] =
  4240. max(max_zone_pfn[i], arch_zone_lowest_possible_pfn[i]);
  4241. }
  4242. arch_zone_lowest_possible_pfn[ZONE_MOVABLE] = 0;
  4243. arch_zone_highest_possible_pfn[ZONE_MOVABLE] = 0;
  4244. /* Find the PFNs that ZONE_MOVABLE begins at in each node */
  4245. memset(zone_movable_pfn, 0, sizeof(zone_movable_pfn));
  4246. find_zone_movable_pfns_for_nodes(zone_movable_pfn);
  4247. /* Print out the zone ranges */
  4248. printk("Zone PFN ranges:\n");
  4249. for (i = 0; i < MAX_NR_ZONES; i++) {
  4250. if (i == ZONE_MOVABLE)
  4251. continue;
  4252. printk(" %-8s ", zone_names[i]);
  4253. if (arch_zone_lowest_possible_pfn[i] ==
  4254. arch_zone_highest_possible_pfn[i])
  4255. printk("empty\n");
  4256. else
  4257. printk("%0#10lx -> %0#10lx\n",
  4258. arch_zone_lowest_possible_pfn[i],
  4259. arch_zone_highest_possible_pfn[i]);
  4260. }
  4261. /* Print out the PFNs ZONE_MOVABLE begins at in each node */
  4262. printk("Movable zone start PFN for each node\n");
  4263. for (i = 0; i < MAX_NUMNODES; i++) {
  4264. if (zone_movable_pfn[i])
  4265. printk(" Node %d: %lu\n", i, zone_movable_pfn[i]);
  4266. }
  4267. /* Print out the early_node_map[] */
  4268. printk("early_node_map[%d] active PFN ranges\n", nr_nodemap_entries);
  4269. for (i = 0; i < nr_nodemap_entries; i++)
  4270. printk(" %3d: %0#10lx -> %0#10lx\n", early_node_map[i].nid,
  4271. early_node_map[i].start_pfn,
  4272. early_node_map[i].end_pfn);
  4273. /* Initialise every node */
  4274. mminit_verify_pageflags_layout();
  4275. setup_nr_node_ids();
  4276. for_each_online_node(nid) {
  4277. pg_data_t *pgdat = NODE_DATA(nid);
  4278. free_area_init_node(nid, NULL,
  4279. find_min_pfn_for_node(nid), NULL);
  4280. /* Any memory on that node */
  4281. if (pgdat->node_present_pages)
  4282. node_set_state(nid, N_HIGH_MEMORY);
  4283. check_for_regular_memory(pgdat);
  4284. }
  4285. }
  4286. static int __init cmdline_parse_core(char *p, unsigned long *core)
  4287. {
  4288. unsigned long long coremem;
  4289. if (!p)
  4290. return -EINVAL;
  4291. coremem = memparse(p, &p);
  4292. *core = coremem >> PAGE_SHIFT;
  4293. /* Paranoid check that UL is enough for the coremem value */
  4294. WARN_ON((coremem >> PAGE_SHIFT) > ULONG_MAX);
  4295. return 0;
  4296. }
  4297. /*
  4298. * kernelcore=size sets the amount of memory for use for allocations that
  4299. * cannot be reclaimed or migrated.
  4300. */
  4301. static int __init cmdline_parse_kernelcore(char *p)
  4302. {
  4303. return cmdline_parse_core(p, &required_kernelcore);
  4304. }
  4305. /*
  4306. * movablecore=size sets the amount of memory for use for allocations that
  4307. * can be reclaimed or migrated.
  4308. */
  4309. static int __init cmdline_parse_movablecore(char *p)
  4310. {
  4311. return cmdline_parse_core(p, &required_movablecore);
  4312. }
  4313. early_param("kernelcore", cmdline_parse_kernelcore);
  4314. early_param("movablecore", cmdline_parse_movablecore);
  4315. #endif /* CONFIG_ARCH_POPULATES_NODE_MAP */
  4316. /**
  4317. * set_dma_reserve - set the specified number of pages reserved in the first zone
  4318. * @new_dma_reserve: The number of pages to mark reserved
  4319. *
  4320. * The per-cpu batchsize and zone watermarks are determined by present_pages.
  4321. * In the DMA zone, a significant percentage may be consumed by kernel image
  4322. * and other unfreeable allocations which can skew the watermarks badly. This
  4323. * function may optionally be used to account for unfreeable pages in the
  4324. * first zone (e.g., ZONE_DMA). The effect will be lower watermarks and
  4325. * smaller per-cpu batchsize.
  4326. */
  4327. void __init set_dma_reserve(unsigned long new_dma_reserve)
  4328. {
  4329. dma_reserve = new_dma_reserve;
  4330. }
  4331. void __init free_area_init(unsigned long *zones_size)
  4332. {
  4333. free_area_init_node(0, zones_size,
  4334. __pa(PAGE_OFFSET) >> PAGE_SHIFT, NULL);
  4335. }
  4336. static int page_alloc_cpu_notify(struct notifier_block *self,
  4337. unsigned long action, void *hcpu)
  4338. {
  4339. int cpu = (unsigned long)hcpu;
  4340. if (action == CPU_DEAD || action == CPU_DEAD_FROZEN) {
  4341. drain_pages(cpu);
  4342. /*
  4343. * Spill the event counters of the dead processor
  4344. * into the current processors event counters.
  4345. * This artificially elevates the count of the current
  4346. * processor.
  4347. */
  4348. vm_events_fold_cpu(cpu);
  4349. /*
  4350. * Zero the differential counters of the dead processor
  4351. * so that the vm statistics are consistent.
  4352. *
  4353. * This is only okay since the processor is dead and cannot
  4354. * race with what we are doing.
  4355. */
  4356. refresh_cpu_vm_stats(cpu);
  4357. }
  4358. return NOTIFY_OK;
  4359. }
  4360. void __init page_alloc_init(void)
  4361. {
  4362. hotcpu_notifier(page_alloc_cpu_notify, 0);
  4363. }
  4364. /*
  4365. * calculate_totalreserve_pages - called when sysctl_lower_zone_reserve_ratio
  4366. * or min_free_kbytes changes.
  4367. */
  4368. static void calculate_totalreserve_pages(void)
  4369. {
  4370. struct pglist_data *pgdat;
  4371. unsigned long reserve_pages = 0;
  4372. enum zone_type i, j;
  4373. for_each_online_pgdat(pgdat) {
  4374. for (i = 0; i < MAX_NR_ZONES; i++) {
  4375. struct zone *zone = pgdat->node_zones + i;
  4376. unsigned long max = 0;
  4377. /* Find valid and maximum lowmem_reserve in the zone */
  4378. for (j = i; j < MAX_NR_ZONES; j++) {
  4379. if (zone->lowmem_reserve[j] > max)
  4380. max = zone->lowmem_reserve[j];
  4381. }
  4382. /* we treat the high watermark as reserved pages. */
  4383. max += high_wmark_pages(zone);
  4384. if (max > zone->present_pages)
  4385. max = zone->present_pages;
  4386. reserve_pages += max;
  4387. }
  4388. }
  4389. totalreserve_pages = reserve_pages;
  4390. }
  4391. /*
  4392. * setup_per_zone_lowmem_reserve - called whenever
  4393. * sysctl_lower_zone_reserve_ratio changes. Ensures that each zone
  4394. * has a correct pages reserved value, so an adequate number of
  4395. * pages are left in the zone after a successful __alloc_pages().
  4396. */
  4397. static void setup_per_zone_lowmem_reserve(void)
  4398. {
  4399. struct pglist_data *pgdat;
  4400. enum zone_type j, idx;
  4401. for_each_online_pgdat(pgdat) {
  4402. for (j = 0; j < MAX_NR_ZONES; j++) {
  4403. struct zone *zone = pgdat->node_zones + j;
  4404. unsigned long present_pages = zone->present_pages;
  4405. zone->lowmem_reserve[j] = 0;
  4406. idx = j;
  4407. while (idx) {
  4408. struct zone *lower_zone;
  4409. idx--;
  4410. if (sysctl_lowmem_reserve_ratio[idx] < 1)
  4411. sysctl_lowmem_reserve_ratio[idx] = 1;
  4412. lower_zone = pgdat->node_zones + idx;
  4413. lower_zone->lowmem_reserve[j] = present_pages /
  4414. sysctl_lowmem_reserve_ratio[idx];
  4415. present_pages += lower_zone->present_pages;
  4416. }
  4417. }
  4418. }
  4419. /* update totalreserve_pages */
  4420. calculate_totalreserve_pages();
  4421. }
  4422. /**
  4423. * setup_per_zone_wmarks - called when min_free_kbytes changes
  4424. * or when memory is hot-{added|removed}
  4425. *
  4426. * Ensures that the watermark[min,low,high] values for each zone are set
  4427. * correctly with respect to min_free_kbytes.
  4428. */
  4429. void setup_per_zone_wmarks(void)
  4430. {
  4431. unsigned long pages_min = min_free_kbytes >> (PAGE_SHIFT - 10);
  4432. unsigned long lowmem_pages = 0;
  4433. struct zone *zone;
  4434. unsigned long flags;
  4435. /* Calculate total number of !ZONE_HIGHMEM pages */
  4436. for_each_zone(zone) {
  4437. if (!is_highmem(zone))
  4438. lowmem_pages += zone->present_pages;
  4439. }
  4440. for_each_zone(zone) {
  4441. u64 tmp;
  4442. spin_lock_irqsave(&zone->lock, flags);
  4443. tmp = (u64)pages_min * zone->present_pages;
  4444. do_div(tmp, lowmem_pages);
  4445. if (is_highmem(zone)) {
  4446. /*
  4447. * __GFP_HIGH and PF_MEMALLOC allocations usually don't
  4448. * need highmem pages, so cap pages_min to a small
  4449. * value here.
  4450. *
  4451. * The WMARK_HIGH-WMARK_LOW and (WMARK_LOW-WMARK_MIN)
  4452. * deltas controls asynch page reclaim, and so should
  4453. * not be capped for highmem.
  4454. */
  4455. int min_pages;
  4456. min_pages = zone->present_pages / 1024;
  4457. if (min_pages < SWAP_CLUSTER_MAX)
  4458. min_pages = SWAP_CLUSTER_MAX;
  4459. if (min_pages > 128)
  4460. min_pages = 128;
  4461. zone->watermark[WMARK_MIN] = min_pages;
  4462. } else {
  4463. /*
  4464. * If it's a lowmem zone, reserve a number of pages
  4465. * proportionate to the zone's size.
  4466. */
  4467. zone->watermark[WMARK_MIN] = tmp;
  4468. }
  4469. zone->watermark[WMARK_LOW] = min_wmark_pages(zone) + (tmp >> 2);
  4470. zone->watermark[WMARK_HIGH] = min_wmark_pages(zone) + (tmp >> 1);
  4471. setup_zone_migrate_reserve(zone);
  4472. spin_unlock_irqrestore(&zone->lock, flags);
  4473. }
  4474. /* update totalreserve_pages */
  4475. calculate_totalreserve_pages();
  4476. }
  4477. /*
  4478. * The inactive anon list should be small enough that the VM never has to
  4479. * do too much work, but large enough that each inactive page has a chance
  4480. * to be referenced again before it is swapped out.
  4481. *
  4482. * The inactive_anon ratio is the target ratio of ACTIVE_ANON to
  4483. * INACTIVE_ANON pages on this zone's LRU, maintained by the
  4484. * pageout code. A zone->inactive_ratio of 3 means 3:1 or 25% of
  4485. * the anonymous pages are kept on the inactive list.
  4486. *
  4487. * total target max
  4488. * memory ratio inactive anon
  4489. * -------------------------------------
  4490. * 10MB 1 5MB
  4491. * 100MB 1 50MB
  4492. * 1GB 3 250MB
  4493. * 10GB 10 0.9GB
  4494. * 100GB 31 3GB
  4495. * 1TB 101 10GB
  4496. * 10TB 320 32GB
  4497. */
  4498. static void __meminit calculate_zone_inactive_ratio(struct zone *zone)
  4499. {
  4500. unsigned int gb, ratio;
  4501. /* Zone size in gigabytes */
  4502. gb = zone->present_pages >> (30 - PAGE_SHIFT);
  4503. if (gb)
  4504. ratio = int_sqrt(10 * gb);
  4505. else
  4506. ratio = 1;
  4507. zone->inactive_ratio = ratio;
  4508. }
  4509. static void __meminit setup_per_zone_inactive_ratio(void)
  4510. {
  4511. struct zone *zone;
  4512. for_each_zone(zone)
  4513. calculate_zone_inactive_ratio(zone);
  4514. }
  4515. /*
  4516. * Initialise min_free_kbytes.
  4517. *
  4518. * For small machines we want it small (128k min). For large machines
  4519. * we want it large (64MB max). But it is not linear, because network
  4520. * bandwidth does not increase linearly with machine size. We use
  4521. *
  4522. * min_free_kbytes = 4 * sqrt(lowmem_kbytes), for better accuracy:
  4523. * min_free_kbytes = sqrt(lowmem_kbytes * 16)
  4524. *
  4525. * which yields
  4526. *
  4527. * 16MB: 512k
  4528. * 32MB: 724k
  4529. * 64MB: 1024k
  4530. * 128MB: 1448k
  4531. * 256MB: 2048k
  4532. * 512MB: 2896k
  4533. * 1024MB: 4096k
  4534. * 2048MB: 5792k
  4535. * 4096MB: 8192k
  4536. * 8192MB: 11584k
  4537. * 16384MB: 16384k
  4538. */
  4539. int __meminit init_per_zone_wmark_min(void)
  4540. {
  4541. unsigned long lowmem_kbytes;
  4542. lowmem_kbytes = nr_free_buffer_pages() * (PAGE_SIZE >> 10);
  4543. min_free_kbytes = int_sqrt(lowmem_kbytes * 16);
  4544. if (min_free_kbytes < 128)
  4545. min_free_kbytes = 128;
  4546. if (min_free_kbytes > 65536)
  4547. min_free_kbytes = 65536;
  4548. setup_per_zone_wmarks();
  4549. refresh_zone_stat_thresholds();
  4550. setup_per_zone_lowmem_reserve();
  4551. setup_per_zone_inactive_ratio();
  4552. return 0;
  4553. }
  4554. module_init(init_per_zone_wmark_min)
  4555. /*
  4556. * min_free_kbytes_sysctl_handler - just a wrapper around proc_dointvec() so
  4557. * that we can call two helper functions whenever min_free_kbytes
  4558. * changes.
  4559. */
  4560. int min_free_kbytes_sysctl_handler(ctl_table *table, int write,
  4561. void __user *buffer, size_t *length, loff_t *ppos)
  4562. {
  4563. proc_dointvec(table, write, buffer, length, ppos);
  4564. if (write)
  4565. setup_per_zone_wmarks();
  4566. return 0;
  4567. }
  4568. #ifdef CONFIG_NUMA
  4569. int sysctl_min_unmapped_ratio_sysctl_handler(ctl_table *table, int write,
  4570. void __user *buffer, size_t *length, loff_t *ppos)
  4571. {
  4572. struct zone *zone;
  4573. int rc;
  4574. rc = proc_dointvec_minmax(table, write, buffer, length, ppos);
  4575. if (rc)
  4576. return rc;
  4577. for_each_zone(zone)
  4578. zone->min_unmapped_pages = (zone->present_pages *
  4579. sysctl_min_unmapped_ratio) / 100;
  4580. return 0;
  4581. }
  4582. int sysctl_min_slab_ratio_sysctl_handler(ctl_table *table, int write,
  4583. void __user *buffer, size_t *length, loff_t *ppos)
  4584. {
  4585. struct zone *zone;
  4586. int rc;
  4587. rc = proc_dointvec_minmax(table, write, buffer, length, ppos);
  4588. if (rc)
  4589. return rc;
  4590. for_each_zone(zone)
  4591. zone->min_slab_pages = (zone->present_pages *
  4592. sysctl_min_slab_ratio) / 100;
  4593. return 0;
  4594. }
  4595. #endif
  4596. /*
  4597. * lowmem_reserve_ratio_sysctl_handler - just a wrapper around
  4598. * proc_dointvec() so that we can call setup_per_zone_lowmem_reserve()
  4599. * whenever sysctl_lowmem_reserve_ratio changes.
  4600. *
  4601. * The reserve ratio obviously has absolutely no relation with the
  4602. * minimum watermarks. The lowmem reserve ratio can only make sense
  4603. * if in function of the boot time zone sizes.
  4604. */
  4605. int lowmem_reserve_ratio_sysctl_handler(ctl_table *table, int write,
  4606. void __user *buffer, size_t *length, loff_t *ppos)
  4607. {
  4608. proc_dointvec_minmax(table, write, buffer, length, ppos);
  4609. setup_per_zone_lowmem_reserve();
  4610. return 0;
  4611. }
  4612. /*
  4613. * percpu_pagelist_fraction - changes the pcp->high for each zone on each
  4614. * cpu. It is the fraction of total pages in each zone that a hot per cpu pagelist
  4615. * can have before it gets flushed back to buddy allocator.
  4616. */
  4617. int percpu_pagelist_fraction_sysctl_handler(ctl_table *table, int write,
  4618. void __user *buffer, size_t *length, loff_t *ppos)
  4619. {
  4620. struct zone *zone;
  4621. unsigned int cpu;
  4622. int ret;
  4623. ret = proc_dointvec_minmax(table, write, buffer, length, ppos);
  4624. if (!write || (ret == -EINVAL))
  4625. return ret;
  4626. for_each_populated_zone(zone) {
  4627. for_each_possible_cpu(cpu) {
  4628. unsigned long high;
  4629. high = zone->present_pages / percpu_pagelist_fraction;
  4630. setup_pagelist_highmark(
  4631. per_cpu_ptr(zone->pageset, cpu), high);
  4632. }
  4633. }
  4634. return 0;
  4635. }
  4636. int hashdist = HASHDIST_DEFAULT;
  4637. #ifdef CONFIG_NUMA
  4638. static int __init set_hashdist(char *str)
  4639. {
  4640. if (!str)
  4641. return 0;
  4642. hashdist = simple_strtoul(str, &str, 0);
  4643. return 1;
  4644. }
  4645. __setup("hashdist=", set_hashdist);
  4646. #endif
  4647. /*
  4648. * allocate a large system hash table from bootmem
  4649. * - it is assumed that the hash table must contain an exact power-of-2
  4650. * quantity of entries
  4651. * - limit is the number of hash buckets, not the total allocation size
  4652. */
  4653. void *__init alloc_large_system_hash(const char *tablename,
  4654. unsigned long bucketsize,
  4655. unsigned long numentries,
  4656. int scale,
  4657. int flags,
  4658. unsigned int *_hash_shift,
  4659. unsigned int *_hash_mask,
  4660. unsigned long limit)
  4661. {
  4662. unsigned long long max = limit;
  4663. unsigned long log2qty, size;
  4664. void *table = NULL;
  4665. /* allow the kernel cmdline to have a say */
  4666. if (!numentries) {
  4667. /* round applicable memory size up to nearest megabyte */
  4668. numentries = nr_kernel_pages;
  4669. numentries += (1UL << (20 - PAGE_SHIFT)) - 1;
  4670. numentries >>= 20 - PAGE_SHIFT;
  4671. numentries <<= 20 - PAGE_SHIFT;
  4672. /* limit to 1 bucket per 2^scale bytes of low memory */
  4673. if (scale > PAGE_SHIFT)
  4674. numentries >>= (scale - PAGE_SHIFT);
  4675. else
  4676. numentries <<= (PAGE_SHIFT - scale);
  4677. /* Make sure we've got at least a 0-order allocation.. */
  4678. if (unlikely(flags & HASH_SMALL)) {
  4679. /* Makes no sense without HASH_EARLY */
  4680. WARN_ON(!(flags & HASH_EARLY));
  4681. if (!(numentries >> *_hash_shift)) {
  4682. numentries = 1UL << *_hash_shift;
  4683. BUG_ON(!numentries);
  4684. }
  4685. } else if (unlikely((numentries * bucketsize) < PAGE_SIZE))
  4686. numentries = PAGE_SIZE / bucketsize;
  4687. }
  4688. numentries = roundup_pow_of_two(numentries);
  4689. /* limit allocation size to 1/16 total memory by default */
  4690. if (max == 0) {
  4691. max = ((unsigned long long)nr_all_pages << PAGE_SHIFT) >> 4;
  4692. do_div(max, bucketsize);
  4693. }
  4694. if (numentries > max)
  4695. numentries = max;
  4696. log2qty = ilog2(numentries);
  4697. do {
  4698. size = bucketsize << log2qty;
  4699. if (flags & HASH_EARLY)
  4700. table = alloc_bootmem_nopanic(size);
  4701. else if (hashdist)
  4702. table = __vmalloc(size, GFP_ATOMIC, PAGE_KERNEL);
  4703. else {
  4704. /*
  4705. * If bucketsize is not a power-of-two, we may free
  4706. * some pages at the end of hash table which
  4707. * alloc_pages_exact() automatically does
  4708. */
  4709. if (get_order(size) < MAX_ORDER) {
  4710. table = alloc_pages_exact(size, GFP_ATOMIC);
  4711. kmemleak_alloc(table, size, 1, GFP_ATOMIC);
  4712. }
  4713. }
  4714. } while (!table && size > PAGE_SIZE && --log2qty);
  4715. if (!table)
  4716. panic("Failed to allocate %s hash table\n", tablename);
  4717. printk(KERN_INFO "%s hash table entries: %ld (order: %d, %lu bytes)\n",
  4718. tablename,
  4719. (1UL << log2qty),
  4720. ilog2(size) - PAGE_SHIFT,
  4721. size);
  4722. if (_hash_shift)
  4723. *_hash_shift = log2qty;
  4724. if (_hash_mask)
  4725. *_hash_mask = (1 << log2qty) - 1;
  4726. return table;
  4727. }
  4728. /* Return a pointer to the bitmap storing bits affecting a block of pages */
  4729. static inline unsigned long *get_pageblock_bitmap(struct zone *zone,
  4730. unsigned long pfn)
  4731. {
  4732. #ifdef CONFIG_SPARSEMEM
  4733. return __pfn_to_section(pfn)->pageblock_flags;
  4734. #else
  4735. return zone->pageblock_flags;
  4736. #endif /* CONFIG_SPARSEMEM */
  4737. }
  4738. static inline int pfn_to_bitidx(struct zone *zone, unsigned long pfn)
  4739. {
  4740. #ifdef CONFIG_SPARSEMEM
  4741. pfn &= (PAGES_PER_SECTION-1);
  4742. return (pfn >> pageblock_order) * NR_PAGEBLOCK_BITS;
  4743. #else
  4744. pfn = pfn - zone->zone_start_pfn;
  4745. return (pfn >> pageblock_order) * NR_PAGEBLOCK_BITS;
  4746. #endif /* CONFIG_SPARSEMEM */
  4747. }
  4748. /**
  4749. * get_pageblock_flags_group - Return the requested group of flags for the pageblock_nr_pages block of pages
  4750. * @page: The page within the block of interest
  4751. * @start_bitidx: The first bit of interest to retrieve
  4752. * @end_bitidx: The last bit of interest
  4753. * returns pageblock_bits flags
  4754. */
  4755. unsigned long get_pageblock_flags_group(struct page *page,
  4756. int start_bitidx, int end_bitidx)
  4757. {
  4758. struct zone *zone;
  4759. unsigned long *bitmap;
  4760. unsigned long pfn, bitidx;
  4761. unsigned long flags = 0;
  4762. unsigned long value = 1;
  4763. zone = page_zone(page);
  4764. pfn = page_to_pfn(page);
  4765. bitmap = get_pageblock_bitmap(zone, pfn);
  4766. bitidx = pfn_to_bitidx(zone, pfn);
  4767. for (; start_bitidx <= end_bitidx; start_bitidx++, value <<= 1)
  4768. if (test_bit(bitidx + start_bitidx, bitmap))
  4769. flags |= value;
  4770. return flags;
  4771. }
  4772. /**
  4773. * set_pageblock_flags_group - Set the requested group of flags for a pageblock_nr_pages block of pages
  4774. * @page: The page within the block of interest
  4775. * @start_bitidx: The first bit of interest
  4776. * @end_bitidx: The last bit of interest
  4777. * @flags: The flags to set
  4778. */
  4779. void set_pageblock_flags_group(struct page *page, unsigned long flags,
  4780. int start_bitidx, int end_bitidx)
  4781. {
  4782. struct zone *zone;
  4783. unsigned long *bitmap;
  4784. unsigned long pfn, bitidx;
  4785. unsigned long value = 1;
  4786. zone = page_zone(page);
  4787. pfn = page_to_pfn(page);
  4788. bitmap = get_pageblock_bitmap(zone, pfn);
  4789. bitidx = pfn_to_bitidx(zone, pfn);
  4790. VM_BUG_ON(pfn < zone->zone_start_pfn);
  4791. VM_BUG_ON(pfn >= zone->zone_start_pfn + zone->spanned_pages);
  4792. for (; start_bitidx <= end_bitidx; start_bitidx++, value <<= 1)
  4793. if (flags & value)
  4794. __set_bit(bitidx + start_bitidx, bitmap);
  4795. else
  4796. __clear_bit(bitidx + start_bitidx, bitmap);
  4797. }
  4798. /*
  4799. * This is designed as sub function...plz see page_isolation.c also.
  4800. * set/clear page block's type to be ISOLATE.
  4801. * page allocater never alloc memory from ISOLATE block.
  4802. */
  4803. static int
  4804. __count_immobile_pages(struct zone *zone, struct page *page, int count)
  4805. {
  4806. unsigned long pfn, iter, found;
  4807. /*
  4808. * For avoiding noise data, lru_add_drain_all() should be called
  4809. * If ZONE_MOVABLE, the zone never contains immobile pages
  4810. */
  4811. if (zone_idx(zone) == ZONE_MOVABLE)
  4812. return true;
  4813. if (get_pageblock_migratetype(page) == MIGRATE_MOVABLE)
  4814. return true;
  4815. pfn = page_to_pfn(page);
  4816. for (found = 0, iter = 0; iter < pageblock_nr_pages; iter++) {
  4817. unsigned long check = pfn + iter;
  4818. if (!pfn_valid_within(check))
  4819. continue;
  4820. page = pfn_to_page(check);
  4821. if (!page_count(page)) {
  4822. if (PageBuddy(page))
  4823. iter += (1 << page_order(page)) - 1;
  4824. continue;
  4825. }
  4826. if (!PageLRU(page))
  4827. found++;
  4828. /*
  4829. * If there are RECLAIMABLE pages, we need to check it.
  4830. * But now, memory offline itself doesn't call shrink_slab()
  4831. * and it still to be fixed.
  4832. */
  4833. /*
  4834. * If the page is not RAM, page_count()should be 0.
  4835. * we don't need more check. This is an _used_ not-movable page.
  4836. *
  4837. * The problematic thing here is PG_reserved pages. PG_reserved
  4838. * is set to both of a memory hole page and a _used_ kernel
  4839. * page at boot.
  4840. */
  4841. if (found > count)
  4842. return false;
  4843. }
  4844. return true;
  4845. }
  4846. bool is_pageblock_removable_nolock(struct page *page)
  4847. {
  4848. struct zone *zone = page_zone(page);
  4849. return __count_immobile_pages(zone, page, 0);
  4850. }
  4851. int set_migratetype_isolate(struct page *page)
  4852. {
  4853. struct zone *zone;
  4854. unsigned long flags, pfn;
  4855. struct memory_isolate_notify arg;
  4856. int notifier_ret;
  4857. int ret = -EBUSY;
  4858. zone = page_zone(page);
  4859. spin_lock_irqsave(&zone->lock, flags);
  4860. pfn = page_to_pfn(page);
  4861. arg.start_pfn = pfn;
  4862. arg.nr_pages = pageblock_nr_pages;
  4863. arg.pages_found = 0;
  4864. /*
  4865. * It may be possible to isolate a pageblock even if the
  4866. * migratetype is not MIGRATE_MOVABLE. The memory isolation
  4867. * notifier chain is used by balloon drivers to return the
  4868. * number of pages in a range that are held by the balloon
  4869. * driver to shrink memory. If all the pages are accounted for
  4870. * by balloons, are free, or on the LRU, isolation can continue.
  4871. * Later, for example, when memory hotplug notifier runs, these
  4872. * pages reported as "can be isolated" should be isolated(freed)
  4873. * by the balloon driver through the memory notifier chain.
  4874. */
  4875. notifier_ret = memory_isolate_notify(MEM_ISOLATE_COUNT, &arg);
  4876. notifier_ret = notifier_to_errno(notifier_ret);
  4877. if (notifier_ret)
  4878. goto out;
  4879. /*
  4880. * FIXME: Now, memory hotplug doesn't call shrink_slab() by itself.
  4881. * We just check MOVABLE pages.
  4882. */
  4883. if (__count_immobile_pages(zone, page, arg.pages_found))
  4884. ret = 0;
  4885. /*
  4886. * immobile means "not-on-lru" paes. If immobile is larger than
  4887. * removable-by-driver pages reported by notifier, we'll fail.
  4888. */
  4889. out:
  4890. if (!ret) {
  4891. set_pageblock_migratetype(page, MIGRATE_ISOLATE);
  4892. move_freepages_block(zone, page, MIGRATE_ISOLATE);
  4893. }
  4894. spin_unlock_irqrestore(&zone->lock, flags);
  4895. if (!ret)
  4896. drain_all_pages();
  4897. return ret;
  4898. }
  4899. void unset_migratetype_isolate(struct page *page)
  4900. {
  4901. struct zone *zone;
  4902. unsigned long flags;
  4903. zone = page_zone(page);
  4904. spin_lock_irqsave(&zone->lock, flags);
  4905. if (get_pageblock_migratetype(page) != MIGRATE_ISOLATE)
  4906. goto out;
  4907. set_pageblock_migratetype(page, MIGRATE_MOVABLE);
  4908. move_freepages_block(zone, page, MIGRATE_MOVABLE);
  4909. out:
  4910. spin_unlock_irqrestore(&zone->lock, flags);
  4911. }
  4912. #ifdef CONFIG_MEMORY_HOTREMOVE
  4913. /*
  4914. * All pages in the range must be isolated before calling this.
  4915. */
  4916. void
  4917. __offline_isolated_pages(unsigned long start_pfn, unsigned long end_pfn)
  4918. {
  4919. struct page *page;
  4920. struct zone *zone;
  4921. int order, i;
  4922. unsigned long pfn;
  4923. unsigned long flags;
  4924. /* find the first valid pfn */
  4925. for (pfn = start_pfn; pfn < end_pfn; pfn++)
  4926. if (pfn_valid(pfn))
  4927. break;
  4928. if (pfn == end_pfn)
  4929. return;
  4930. zone = page_zone(pfn_to_page(pfn));
  4931. spin_lock_irqsave(&zone->lock, flags);
  4932. pfn = start_pfn;
  4933. while (pfn < end_pfn) {
  4934. if (!pfn_valid(pfn)) {
  4935. pfn++;
  4936. continue;
  4937. }
  4938. page = pfn_to_page(pfn);
  4939. BUG_ON(page_count(page));
  4940. BUG_ON(!PageBuddy(page));
  4941. order = page_order(page);
  4942. #ifdef CONFIG_DEBUG_VM
  4943. printk(KERN_INFO "remove from free list %lx %d %lx\n",
  4944. pfn, 1 << order, end_pfn);
  4945. #endif
  4946. list_del(&page->lru);
  4947. rmv_page_order(page);
  4948. zone->free_area[order].nr_free--;
  4949. __mod_zone_page_state(zone, NR_FREE_PAGES,
  4950. - (1UL << order));
  4951. for (i = 0; i < (1 << order); i++)
  4952. SetPageReserved((page+i));
  4953. pfn += (1 << order);
  4954. }
  4955. spin_unlock_irqrestore(&zone->lock, flags);
  4956. }
  4957. #endif
  4958. #ifdef CONFIG_MEMORY_FAILURE
  4959. bool is_free_buddy_page(struct page *page)
  4960. {
  4961. struct zone *zone = page_zone(page);
  4962. unsigned long pfn = page_to_pfn(page);
  4963. unsigned long flags;
  4964. int order;
  4965. spin_lock_irqsave(&zone->lock, flags);
  4966. for (order = 0; order < MAX_ORDER; order++) {
  4967. struct page *page_head = page - (pfn & ((1 << order) - 1));
  4968. if (PageBuddy(page_head) && page_order(page_head) >= order)
  4969. break;
  4970. }
  4971. spin_unlock_irqrestore(&zone->lock, flags);
  4972. return order < MAX_ORDER;
  4973. }
  4974. #endif
  4975. static struct trace_print_flags pageflag_names[] = {
  4976. {1UL << PG_locked, "locked" },
  4977. {1UL << PG_error, "error" },
  4978. {1UL << PG_referenced, "referenced" },
  4979. {1UL << PG_uptodate, "uptodate" },
  4980. {1UL << PG_dirty, "dirty" },
  4981. {1UL << PG_lru, "lru" },
  4982. {1UL << PG_active, "active" },
  4983. {1UL << PG_slab, "slab" },
  4984. {1UL << PG_owner_priv_1, "owner_priv_1" },
  4985. {1UL << PG_arch_1, "arch_1" },
  4986. {1UL << PG_reserved, "reserved" },
  4987. {1UL << PG_private, "private" },
  4988. {1UL << PG_private_2, "private_2" },
  4989. {1UL << PG_writeback, "writeback" },
  4990. #ifdef CONFIG_PAGEFLAGS_EXTENDED
  4991. {1UL << PG_head, "head" },
  4992. {1UL << PG_tail, "tail" },
  4993. #else
  4994. {1UL << PG_compound, "compound" },
  4995. #endif
  4996. {1UL << PG_swapcache, "swapcache" },
  4997. {1UL << PG_mappedtodisk, "mappedtodisk" },
  4998. {1UL << PG_reclaim, "reclaim" },
  4999. {1UL << PG_swapbacked, "swapbacked" },
  5000. {1UL << PG_unevictable, "unevictable" },
  5001. #ifdef CONFIG_MMU
  5002. {1UL << PG_mlocked, "mlocked" },
  5003. #endif
  5004. #ifdef CONFIG_ARCH_USES_PG_UNCACHED
  5005. {1UL << PG_uncached, "uncached" },
  5006. #endif
  5007. #ifdef CONFIG_MEMORY_FAILURE
  5008. {1UL << PG_hwpoison, "hwpoison" },
  5009. #endif
  5010. {-1UL, NULL },
  5011. };
  5012. static void dump_page_flags(unsigned long flags)
  5013. {
  5014. const char *delim = "";
  5015. unsigned long mask;
  5016. int i;
  5017. printk(KERN_ALERT "page flags: %#lx(", flags);
  5018. /* remove zone id */
  5019. flags &= (1UL << NR_PAGEFLAGS) - 1;
  5020. for (i = 0; pageflag_names[i].name && flags; i++) {
  5021. mask = pageflag_names[i].mask;
  5022. if ((flags & mask) != mask)
  5023. continue;
  5024. flags &= ~mask;
  5025. printk("%s%s", delim, pageflag_names[i].name);
  5026. delim = "|";
  5027. }
  5028. /* check for left over flags */
  5029. if (flags)
  5030. printk("%s%#lx", delim, flags);
  5031. printk(")\n");
  5032. }
  5033. void dump_page(struct page *page)
  5034. {
  5035. printk(KERN_ALERT
  5036. "page:%p count:%d mapcount:%d mapping:%p index:%#lx\n",
  5037. page, atomic_read(&page->_count), page_mapcount(page),
  5038. page->mapping, page->index);
  5039. dump_page_flags(page->flags);
  5040. mem_cgroup_print_bad_page(page);
  5041. }