page_alloc.c 170 KB

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