page_alloc.c 182 KB

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