page_alloc.c 182 KB

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