page_alloc.c 184 KB

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