page_alloc.c 170 KB

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