page_alloc.c 182 KB

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