page_alloc.c 169 KB

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