page_alloc.c 179 KB

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