page_alloc.c 181 KB

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