page_alloc.c 210 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744
  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/kasan.h>
  28. #include <linux/module.h>
  29. #include <linux/suspend.h>
  30. #include <linux/pagevec.h>
  31. #include <linux/blkdev.h>
  32. #include <linux/slab.h>
  33. #include <linux/ratelimit.h>
  34. #include <linux/oom.h>
  35. #include <linux/notifier.h>
  36. #include <linux/topology.h>
  37. #include <linux/sysctl.h>
  38. #include <linux/cpu.h>
  39. #include <linux/cpuset.h>
  40. #include <linux/memory_hotplug.h>
  41. #include <linux/nodemask.h>
  42. #include <linux/vmalloc.h>
  43. #include <linux/vmstat.h>
  44. #include <linux/mempolicy.h>
  45. #include <linux/memremap.h>
  46. #include <linux/stop_machine.h>
  47. #include <linux/sort.h>
  48. #include <linux/pfn.h>
  49. #include <linux/backing-dev.h>
  50. #include <linux/fault-inject.h>
  51. #include <linux/page-isolation.h>
  52. #include <linux/page_ext.h>
  53. #include <linux/debugobjects.h>
  54. #include <linux/kmemleak.h>
  55. #include <linux/compaction.h>
  56. #include <trace/events/kmem.h>
  57. #include <trace/events/oom.h>
  58. #include <linux/prefetch.h>
  59. #include <linux/mm_inline.h>
  60. #include <linux/migrate.h>
  61. #include <linux/hugetlb.h>
  62. #include <linux/sched/rt.h>
  63. #include <linux/sched/mm.h>
  64. #include <linux/page_owner.h>
  65. #include <linux/kthread.h>
  66. #include <linux/memcontrol.h>
  67. #include <linux/ftrace.h>
  68. #include <linux/lockdep.h>
  69. #include <linux/nmi.h>
  70. #include <asm/sections.h>
  71. #include <asm/tlbflush.h>
  72. #include <asm/div64.h>
  73. #include "internal.h"
  74. /* prevent >1 _updater_ of zone percpu pageset ->high and ->batch fields */
  75. static DEFINE_MUTEX(pcp_batch_high_lock);
  76. #define MIN_PERCPU_PAGELIST_FRACTION (8)
  77. #ifdef CONFIG_USE_PERCPU_NUMA_NODE_ID
  78. DEFINE_PER_CPU(int, numa_node);
  79. EXPORT_PER_CPU_SYMBOL(numa_node);
  80. #endif
  81. #ifdef CONFIG_HAVE_MEMORYLESS_NODES
  82. /*
  83. * N.B., Do NOT reference the '_numa_mem_' per cpu variable directly.
  84. * It will not be defined when CONFIG_HAVE_MEMORYLESS_NODES is not defined.
  85. * Use the accessor functions set_numa_mem(), numa_mem_id() and cpu_to_mem()
  86. * defined in <linux/topology.h>.
  87. */
  88. DEFINE_PER_CPU(int, _numa_mem_); /* Kernel "local memory" node */
  89. EXPORT_PER_CPU_SYMBOL(_numa_mem_);
  90. int _node_numa_mem_[MAX_NUMNODES];
  91. #endif
  92. /* work_structs for global per-cpu drains */
  93. DEFINE_MUTEX(pcpu_drain_mutex);
  94. DEFINE_PER_CPU(struct work_struct, pcpu_drain);
  95. #ifdef CONFIG_GCC_PLUGIN_LATENT_ENTROPY
  96. volatile unsigned long latent_entropy __latent_entropy;
  97. EXPORT_SYMBOL(latent_entropy);
  98. #endif
  99. /*
  100. * Array of node states.
  101. */
  102. nodemask_t node_states[NR_NODE_STATES] __read_mostly = {
  103. [N_POSSIBLE] = NODE_MASK_ALL,
  104. [N_ONLINE] = { { [0] = 1UL } },
  105. #ifndef CONFIG_NUMA
  106. [N_NORMAL_MEMORY] = { { [0] = 1UL } },
  107. #ifdef CONFIG_HIGHMEM
  108. [N_HIGH_MEMORY] = { { [0] = 1UL } },
  109. #endif
  110. [N_MEMORY] = { { [0] = 1UL } },
  111. [N_CPU] = { { [0] = 1UL } },
  112. #endif /* NUMA */
  113. };
  114. EXPORT_SYMBOL(node_states);
  115. /* Protect totalram_pages and zone->managed_pages */
  116. static DEFINE_SPINLOCK(managed_page_count_lock);
  117. unsigned long totalram_pages __read_mostly;
  118. unsigned long totalreserve_pages __read_mostly;
  119. unsigned long totalcma_pages __read_mostly;
  120. int percpu_pagelist_fraction;
  121. gfp_t gfp_allowed_mask __read_mostly = GFP_BOOT_MASK;
  122. /*
  123. * A cached value of the page's pageblock's migratetype, used when the page is
  124. * put on a pcplist. Used to avoid the pageblock migratetype lookup when
  125. * freeing from pcplists in most cases, at the cost of possibly becoming stale.
  126. * Also the migratetype set in the page does not necessarily match the pcplist
  127. * index, e.g. page might have MIGRATE_CMA set but be on a pcplist with any
  128. * other index - this ensures that it will be put on the correct CMA freelist.
  129. */
  130. static inline int get_pcppage_migratetype(struct page *page)
  131. {
  132. return page->index;
  133. }
  134. static inline void set_pcppage_migratetype(struct page *page, int migratetype)
  135. {
  136. page->index = migratetype;
  137. }
  138. #ifdef CONFIG_PM_SLEEP
  139. /*
  140. * The following functions are used by the suspend/hibernate code to temporarily
  141. * change gfp_allowed_mask in order to avoid using I/O during memory allocations
  142. * while devices are suspended. To avoid races with the suspend/hibernate code,
  143. * they should always be called with pm_mutex held (gfp_allowed_mask also should
  144. * only be modified with pm_mutex held, unless the suspend/hibernate code is
  145. * guaranteed not to run in parallel with that modification).
  146. */
  147. static gfp_t saved_gfp_mask;
  148. void pm_restore_gfp_mask(void)
  149. {
  150. WARN_ON(!mutex_is_locked(&pm_mutex));
  151. if (saved_gfp_mask) {
  152. gfp_allowed_mask = saved_gfp_mask;
  153. saved_gfp_mask = 0;
  154. }
  155. }
  156. void pm_restrict_gfp_mask(void)
  157. {
  158. WARN_ON(!mutex_is_locked(&pm_mutex));
  159. WARN_ON(saved_gfp_mask);
  160. saved_gfp_mask = gfp_allowed_mask;
  161. gfp_allowed_mask &= ~(__GFP_IO | __GFP_FS);
  162. }
  163. bool pm_suspended_storage(void)
  164. {
  165. if ((gfp_allowed_mask & (__GFP_IO | __GFP_FS)) == (__GFP_IO | __GFP_FS))
  166. return false;
  167. return true;
  168. }
  169. #endif /* CONFIG_PM_SLEEP */
  170. #ifdef CONFIG_HUGETLB_PAGE_SIZE_VARIABLE
  171. unsigned int pageblock_order __read_mostly;
  172. #endif
  173. static void __free_pages_ok(struct page *page, unsigned int order);
  174. /*
  175. * results with 256, 32 in the lowmem_reserve sysctl:
  176. * 1G machine -> (16M dma, 800M-16M normal, 1G-800M high)
  177. * 1G machine -> (16M dma, 784M normal, 224M high)
  178. * NORMAL allocation will leave 784M/256 of ram reserved in the ZONE_DMA
  179. * HIGHMEM allocation will leave 224M/32 of ram reserved in ZONE_NORMAL
  180. * HIGHMEM allocation will leave (224M+784M)/256 of ram reserved in ZONE_DMA
  181. *
  182. * TBD: should special case ZONE_DMA32 machines here - in those we normally
  183. * don't need any ZONE_NORMAL reservation
  184. */
  185. int sysctl_lowmem_reserve_ratio[MAX_NR_ZONES-1] = {
  186. #ifdef CONFIG_ZONE_DMA
  187. 256,
  188. #endif
  189. #ifdef CONFIG_ZONE_DMA32
  190. 256,
  191. #endif
  192. #ifdef CONFIG_HIGHMEM
  193. 32,
  194. #endif
  195. 32,
  196. };
  197. EXPORT_SYMBOL(totalram_pages);
  198. static char * const zone_names[MAX_NR_ZONES] = {
  199. #ifdef CONFIG_ZONE_DMA
  200. "DMA",
  201. #endif
  202. #ifdef CONFIG_ZONE_DMA32
  203. "DMA32",
  204. #endif
  205. "Normal",
  206. #ifdef CONFIG_HIGHMEM
  207. "HighMem",
  208. #endif
  209. "Movable",
  210. #ifdef CONFIG_ZONE_DEVICE
  211. "Device",
  212. #endif
  213. };
  214. char * const migratetype_names[MIGRATE_TYPES] = {
  215. "Unmovable",
  216. "Movable",
  217. "Reclaimable",
  218. "HighAtomic",
  219. #ifdef CONFIG_CMA
  220. "CMA",
  221. #endif
  222. #ifdef CONFIG_MEMORY_ISOLATION
  223. "Isolate",
  224. #endif
  225. };
  226. compound_page_dtor * const compound_page_dtors[] = {
  227. NULL,
  228. free_compound_page,
  229. #ifdef CONFIG_HUGETLB_PAGE
  230. free_huge_page,
  231. #endif
  232. #ifdef CONFIG_TRANSPARENT_HUGEPAGE
  233. free_transhuge_page,
  234. #endif
  235. };
  236. int min_free_kbytes = 1024;
  237. int user_min_free_kbytes = -1;
  238. int watermark_scale_factor = 10;
  239. static unsigned long __meminitdata nr_kernel_pages;
  240. static unsigned long __meminitdata nr_all_pages;
  241. static unsigned long __meminitdata dma_reserve;
  242. #ifdef CONFIG_HAVE_MEMBLOCK_NODE_MAP
  243. static unsigned long __meminitdata arch_zone_lowest_possible_pfn[MAX_NR_ZONES];
  244. static unsigned long __meminitdata arch_zone_highest_possible_pfn[MAX_NR_ZONES];
  245. static unsigned long __initdata required_kernelcore;
  246. static unsigned long __initdata required_movablecore;
  247. static unsigned long __meminitdata zone_movable_pfn[MAX_NUMNODES];
  248. static bool mirrored_kernelcore;
  249. /* movable_zone is the "real" zone pages in ZONE_MOVABLE are taken from */
  250. int movable_zone;
  251. EXPORT_SYMBOL(movable_zone);
  252. #endif /* CONFIG_HAVE_MEMBLOCK_NODE_MAP */
  253. #if MAX_NUMNODES > 1
  254. int nr_node_ids __read_mostly = MAX_NUMNODES;
  255. int nr_online_nodes __read_mostly = 1;
  256. EXPORT_SYMBOL(nr_node_ids);
  257. EXPORT_SYMBOL(nr_online_nodes);
  258. #endif
  259. int page_group_by_mobility_disabled __read_mostly;
  260. #ifdef CONFIG_DEFERRED_STRUCT_PAGE_INIT
  261. static inline void reset_deferred_meminit(pg_data_t *pgdat)
  262. {
  263. unsigned long max_initialise;
  264. unsigned long reserved_lowmem;
  265. /*
  266. * Initialise at least 2G of a node but also take into account that
  267. * two large system hashes that can take up 1GB for 0.25TB/node.
  268. */
  269. max_initialise = max(2UL << (30 - PAGE_SHIFT),
  270. (pgdat->node_spanned_pages >> 8));
  271. /*
  272. * Compensate the all the memblock reservations (e.g. crash kernel)
  273. * from the initial estimation to make sure we will initialize enough
  274. * memory to boot.
  275. */
  276. reserved_lowmem = memblock_reserved_memory_within(pgdat->node_start_pfn,
  277. pgdat->node_start_pfn + max_initialise);
  278. max_initialise += reserved_lowmem;
  279. pgdat->static_init_size = min(max_initialise, pgdat->node_spanned_pages);
  280. pgdat->first_deferred_pfn = ULONG_MAX;
  281. }
  282. /* Returns true if the struct page for the pfn is uninitialised */
  283. static inline bool __meminit early_page_uninitialised(unsigned long pfn)
  284. {
  285. int nid = early_pfn_to_nid(pfn);
  286. if (node_online(nid) && pfn >= NODE_DATA(nid)->first_deferred_pfn)
  287. return true;
  288. return false;
  289. }
  290. /*
  291. * Returns false when the remaining initialisation should be deferred until
  292. * later in the boot cycle when it can be parallelised.
  293. */
  294. static inline bool update_defer_init(pg_data_t *pgdat,
  295. unsigned long pfn, unsigned long zone_end,
  296. unsigned long *nr_initialised)
  297. {
  298. /* Always populate low zones for address-contrained allocations */
  299. if (zone_end < pgdat_end_pfn(pgdat))
  300. return true;
  301. (*nr_initialised)++;
  302. if ((*nr_initialised > pgdat->static_init_size) &&
  303. (pfn & (PAGES_PER_SECTION - 1)) == 0) {
  304. pgdat->first_deferred_pfn = pfn;
  305. return false;
  306. }
  307. return true;
  308. }
  309. #else
  310. static inline void reset_deferred_meminit(pg_data_t *pgdat)
  311. {
  312. }
  313. static inline bool early_page_uninitialised(unsigned long pfn)
  314. {
  315. return false;
  316. }
  317. static inline bool update_defer_init(pg_data_t *pgdat,
  318. unsigned long pfn, unsigned long zone_end,
  319. unsigned long *nr_initialised)
  320. {
  321. return true;
  322. }
  323. #endif
  324. /* Return a pointer to the bitmap storing bits affecting a block of pages */
  325. static inline unsigned long *get_pageblock_bitmap(struct page *page,
  326. unsigned long pfn)
  327. {
  328. #ifdef CONFIG_SPARSEMEM
  329. return __pfn_to_section(pfn)->pageblock_flags;
  330. #else
  331. return page_zone(page)->pageblock_flags;
  332. #endif /* CONFIG_SPARSEMEM */
  333. }
  334. static inline int pfn_to_bitidx(struct page *page, unsigned long pfn)
  335. {
  336. #ifdef CONFIG_SPARSEMEM
  337. pfn &= (PAGES_PER_SECTION-1);
  338. return (pfn >> pageblock_order) * NR_PAGEBLOCK_BITS;
  339. #else
  340. pfn = pfn - round_down(page_zone(page)->zone_start_pfn, pageblock_nr_pages);
  341. return (pfn >> pageblock_order) * NR_PAGEBLOCK_BITS;
  342. #endif /* CONFIG_SPARSEMEM */
  343. }
  344. /**
  345. * get_pfnblock_flags_mask - Return the requested group of flags for the pageblock_nr_pages block of pages
  346. * @page: The page within the block of interest
  347. * @pfn: The target page frame number
  348. * @end_bitidx: The last bit of interest to retrieve
  349. * @mask: mask of bits that the caller is interested in
  350. *
  351. * Return: pageblock_bits flags
  352. */
  353. static __always_inline unsigned long __get_pfnblock_flags_mask(struct page *page,
  354. unsigned long pfn,
  355. unsigned long end_bitidx,
  356. unsigned long mask)
  357. {
  358. unsigned long *bitmap;
  359. unsigned long bitidx, word_bitidx;
  360. unsigned long word;
  361. bitmap = get_pageblock_bitmap(page, pfn);
  362. bitidx = pfn_to_bitidx(page, pfn);
  363. word_bitidx = bitidx / BITS_PER_LONG;
  364. bitidx &= (BITS_PER_LONG-1);
  365. word = bitmap[word_bitidx];
  366. bitidx += end_bitidx;
  367. return (word >> (BITS_PER_LONG - bitidx - 1)) & mask;
  368. }
  369. unsigned long get_pfnblock_flags_mask(struct page *page, unsigned long pfn,
  370. unsigned long end_bitidx,
  371. unsigned long mask)
  372. {
  373. return __get_pfnblock_flags_mask(page, pfn, end_bitidx, mask);
  374. }
  375. static __always_inline int get_pfnblock_migratetype(struct page *page, unsigned long pfn)
  376. {
  377. return __get_pfnblock_flags_mask(page, pfn, PB_migrate_end, MIGRATETYPE_MASK);
  378. }
  379. /**
  380. * set_pfnblock_flags_mask - Set the requested group of flags for a pageblock_nr_pages block of pages
  381. * @page: The page within the block of interest
  382. * @flags: The flags to set
  383. * @pfn: The target page frame number
  384. * @end_bitidx: The last bit of interest
  385. * @mask: mask of bits that the caller is interested in
  386. */
  387. void set_pfnblock_flags_mask(struct page *page, unsigned long flags,
  388. unsigned long pfn,
  389. unsigned long end_bitidx,
  390. unsigned long mask)
  391. {
  392. unsigned long *bitmap;
  393. unsigned long bitidx, word_bitidx;
  394. unsigned long old_word, word;
  395. BUILD_BUG_ON(NR_PAGEBLOCK_BITS != 4);
  396. bitmap = get_pageblock_bitmap(page, pfn);
  397. bitidx = pfn_to_bitidx(page, pfn);
  398. word_bitidx = bitidx / BITS_PER_LONG;
  399. bitidx &= (BITS_PER_LONG-1);
  400. VM_BUG_ON_PAGE(!zone_spans_pfn(page_zone(page), pfn), page);
  401. bitidx += end_bitidx;
  402. mask <<= (BITS_PER_LONG - bitidx - 1);
  403. flags <<= (BITS_PER_LONG - bitidx - 1);
  404. word = READ_ONCE(bitmap[word_bitidx]);
  405. for (;;) {
  406. old_word = cmpxchg(&bitmap[word_bitidx], word, (word & ~mask) | flags);
  407. if (word == old_word)
  408. break;
  409. word = old_word;
  410. }
  411. }
  412. void set_pageblock_migratetype(struct page *page, int migratetype)
  413. {
  414. if (unlikely(page_group_by_mobility_disabled &&
  415. migratetype < MIGRATE_PCPTYPES))
  416. migratetype = MIGRATE_UNMOVABLE;
  417. set_pageblock_flags_group(page, (unsigned long)migratetype,
  418. PB_migrate, PB_migrate_end);
  419. }
  420. #ifdef CONFIG_DEBUG_VM
  421. static int page_outside_zone_boundaries(struct zone *zone, struct page *page)
  422. {
  423. int ret = 0;
  424. unsigned seq;
  425. unsigned long pfn = page_to_pfn(page);
  426. unsigned long sp, start_pfn;
  427. do {
  428. seq = zone_span_seqbegin(zone);
  429. start_pfn = zone->zone_start_pfn;
  430. sp = zone->spanned_pages;
  431. if (!zone_spans_pfn(zone, pfn))
  432. ret = 1;
  433. } while (zone_span_seqretry(zone, seq));
  434. if (ret)
  435. pr_err("page 0x%lx outside node %d zone %s [ 0x%lx - 0x%lx ]\n",
  436. pfn, zone_to_nid(zone), zone->name,
  437. start_pfn, start_pfn + sp);
  438. return ret;
  439. }
  440. static int page_is_consistent(struct zone *zone, struct page *page)
  441. {
  442. if (!pfn_valid_within(page_to_pfn(page)))
  443. return 0;
  444. if (zone != page_zone(page))
  445. return 0;
  446. return 1;
  447. }
  448. /*
  449. * Temporary debugging check for pages not lying within a given zone.
  450. */
  451. static int __maybe_unused bad_range(struct zone *zone, struct page *page)
  452. {
  453. if (page_outside_zone_boundaries(zone, page))
  454. return 1;
  455. if (!page_is_consistent(zone, page))
  456. return 1;
  457. return 0;
  458. }
  459. #else
  460. static inline int __maybe_unused bad_range(struct zone *zone, struct page *page)
  461. {
  462. return 0;
  463. }
  464. #endif
  465. static void bad_page(struct page *page, const char *reason,
  466. unsigned long bad_flags)
  467. {
  468. static unsigned long resume;
  469. static unsigned long nr_shown;
  470. static unsigned long nr_unshown;
  471. /*
  472. * Allow a burst of 60 reports, then keep quiet for that minute;
  473. * or allow a steady drip of one report per second.
  474. */
  475. if (nr_shown == 60) {
  476. if (time_before(jiffies, resume)) {
  477. nr_unshown++;
  478. goto out;
  479. }
  480. if (nr_unshown) {
  481. pr_alert(
  482. "BUG: Bad page state: %lu messages suppressed\n",
  483. nr_unshown);
  484. nr_unshown = 0;
  485. }
  486. nr_shown = 0;
  487. }
  488. if (nr_shown++ == 0)
  489. resume = jiffies + 60 * HZ;
  490. pr_alert("BUG: Bad page state in process %s pfn:%05lx\n",
  491. current->comm, page_to_pfn(page));
  492. __dump_page(page, reason);
  493. bad_flags &= page->flags;
  494. if (bad_flags)
  495. pr_alert("bad because of flags: %#lx(%pGp)\n",
  496. bad_flags, &bad_flags);
  497. dump_page_owner(page);
  498. print_modules();
  499. dump_stack();
  500. out:
  501. /* Leave bad fields for debug, except PageBuddy could make trouble */
  502. page_mapcount_reset(page); /* remove PageBuddy */
  503. add_taint(TAINT_BAD_PAGE, LOCKDEP_NOW_UNRELIABLE);
  504. }
  505. /*
  506. * Higher-order pages are called "compound pages". They are structured thusly:
  507. *
  508. * The first PAGE_SIZE page is called the "head page" and have PG_head set.
  509. *
  510. * The remaining PAGE_SIZE pages are called "tail pages". PageTail() is encoded
  511. * in bit 0 of page->compound_head. The rest of bits is pointer to head page.
  512. *
  513. * The first tail page's ->compound_dtor holds the offset in array of compound
  514. * page destructors. See compound_page_dtors.
  515. *
  516. * The first tail page's ->compound_order holds the order of allocation.
  517. * This usage means that zero-order pages may not be compound.
  518. */
  519. void free_compound_page(struct page *page)
  520. {
  521. __free_pages_ok(page, compound_order(page));
  522. }
  523. void prep_compound_page(struct page *page, unsigned int order)
  524. {
  525. int i;
  526. int nr_pages = 1 << order;
  527. set_compound_page_dtor(page, COMPOUND_PAGE_DTOR);
  528. set_compound_order(page, order);
  529. __SetPageHead(page);
  530. for (i = 1; i < nr_pages; i++) {
  531. struct page *p = page + i;
  532. set_page_count(p, 0);
  533. p->mapping = TAIL_MAPPING;
  534. set_compound_head(p, page);
  535. }
  536. atomic_set(compound_mapcount_ptr(page), -1);
  537. }
  538. #ifdef CONFIG_DEBUG_PAGEALLOC
  539. unsigned int _debug_guardpage_minorder;
  540. bool _debug_pagealloc_enabled __read_mostly
  541. = IS_ENABLED(CONFIG_DEBUG_PAGEALLOC_ENABLE_DEFAULT);
  542. EXPORT_SYMBOL(_debug_pagealloc_enabled);
  543. bool _debug_guardpage_enabled __read_mostly;
  544. static int __init early_debug_pagealloc(char *buf)
  545. {
  546. if (!buf)
  547. return -EINVAL;
  548. return kstrtobool(buf, &_debug_pagealloc_enabled);
  549. }
  550. early_param("debug_pagealloc", early_debug_pagealloc);
  551. static bool need_debug_guardpage(void)
  552. {
  553. /* If we don't use debug_pagealloc, we don't need guard page */
  554. if (!debug_pagealloc_enabled())
  555. return false;
  556. if (!debug_guardpage_minorder())
  557. return false;
  558. return true;
  559. }
  560. static void init_debug_guardpage(void)
  561. {
  562. if (!debug_pagealloc_enabled())
  563. return;
  564. if (!debug_guardpage_minorder())
  565. return;
  566. _debug_guardpage_enabled = true;
  567. }
  568. struct page_ext_operations debug_guardpage_ops = {
  569. .need = need_debug_guardpage,
  570. .init = init_debug_guardpage,
  571. };
  572. static int __init debug_guardpage_minorder_setup(char *buf)
  573. {
  574. unsigned long res;
  575. if (kstrtoul(buf, 10, &res) < 0 || res > MAX_ORDER / 2) {
  576. pr_err("Bad debug_guardpage_minorder value\n");
  577. return 0;
  578. }
  579. _debug_guardpage_minorder = res;
  580. pr_info("Setting debug_guardpage_minorder to %lu\n", res);
  581. return 0;
  582. }
  583. early_param("debug_guardpage_minorder", debug_guardpage_minorder_setup);
  584. static inline bool set_page_guard(struct zone *zone, struct page *page,
  585. unsigned int order, int migratetype)
  586. {
  587. struct page_ext *page_ext;
  588. if (!debug_guardpage_enabled())
  589. return false;
  590. if (order >= debug_guardpage_minorder())
  591. return false;
  592. page_ext = lookup_page_ext(page);
  593. if (unlikely(!page_ext))
  594. return false;
  595. __set_bit(PAGE_EXT_DEBUG_GUARD, &page_ext->flags);
  596. INIT_LIST_HEAD(&page->lru);
  597. set_page_private(page, order);
  598. /* Guard pages are not available for any usage */
  599. __mod_zone_freepage_state(zone, -(1 << order), migratetype);
  600. return true;
  601. }
  602. static inline void clear_page_guard(struct zone *zone, struct page *page,
  603. unsigned int order, int migratetype)
  604. {
  605. struct page_ext *page_ext;
  606. if (!debug_guardpage_enabled())
  607. return;
  608. page_ext = lookup_page_ext(page);
  609. if (unlikely(!page_ext))
  610. return;
  611. __clear_bit(PAGE_EXT_DEBUG_GUARD, &page_ext->flags);
  612. set_page_private(page, 0);
  613. if (!is_migrate_isolate(migratetype))
  614. __mod_zone_freepage_state(zone, (1 << order), migratetype);
  615. }
  616. #else
  617. struct page_ext_operations debug_guardpage_ops;
  618. static inline bool set_page_guard(struct zone *zone, struct page *page,
  619. unsigned int order, int migratetype) { return false; }
  620. static inline void clear_page_guard(struct zone *zone, struct page *page,
  621. unsigned int order, int migratetype) {}
  622. #endif
  623. static inline void set_page_order(struct page *page, unsigned int order)
  624. {
  625. set_page_private(page, order);
  626. __SetPageBuddy(page);
  627. }
  628. static inline void rmv_page_order(struct page *page)
  629. {
  630. __ClearPageBuddy(page);
  631. set_page_private(page, 0);
  632. }
  633. /*
  634. * This function checks whether a page is free && is the buddy
  635. * we can do coalesce a page and its buddy if
  636. * (a) the buddy is not in a hole (check before calling!) &&
  637. * (b) the buddy is in the buddy system &&
  638. * (c) a page and its buddy have the same order &&
  639. * (d) a page and its buddy are in the same zone.
  640. *
  641. * For recording whether a page is in the buddy system, we set ->_mapcount
  642. * PAGE_BUDDY_MAPCOUNT_VALUE.
  643. * Setting, clearing, and testing _mapcount PAGE_BUDDY_MAPCOUNT_VALUE is
  644. * serialized by zone->lock.
  645. *
  646. * For recording page's order, we use page_private(page).
  647. */
  648. static inline int page_is_buddy(struct page *page, struct page *buddy,
  649. unsigned int order)
  650. {
  651. if (page_is_guard(buddy) && page_order(buddy) == order) {
  652. if (page_zone_id(page) != page_zone_id(buddy))
  653. return 0;
  654. VM_BUG_ON_PAGE(page_count(buddy) != 0, buddy);
  655. return 1;
  656. }
  657. if (PageBuddy(buddy) && page_order(buddy) == order) {
  658. /*
  659. * zone check is done late to avoid uselessly
  660. * calculating zone/node ids for pages that could
  661. * never merge.
  662. */
  663. if (page_zone_id(page) != page_zone_id(buddy))
  664. return 0;
  665. VM_BUG_ON_PAGE(page_count(buddy) != 0, buddy);
  666. return 1;
  667. }
  668. return 0;
  669. }
  670. /*
  671. * Freeing function for a buddy system allocator.
  672. *
  673. * The concept of a buddy system is to maintain direct-mapped table
  674. * (containing bit values) for memory blocks of various "orders".
  675. * The bottom level table contains the map for the smallest allocatable
  676. * units of memory (here, pages), and each level above it describes
  677. * pairs of units from the levels below, hence, "buddies".
  678. * At a high level, all that happens here is marking the table entry
  679. * at the bottom level available, and propagating the changes upward
  680. * as necessary, plus some accounting needed to play nicely with other
  681. * parts of the VM system.
  682. * At each level, we keep a list of pages, which are heads of continuous
  683. * free pages of length of (1 << order) and marked with _mapcount
  684. * PAGE_BUDDY_MAPCOUNT_VALUE. Page's order is recorded in page_private(page)
  685. * field.
  686. * So when we are allocating or freeing one, we can derive the state of the
  687. * other. That is, if we allocate a small block, and both were
  688. * free, the remainder of the region must be split into blocks.
  689. * If a block is freed, and its buddy is also free, then this
  690. * triggers coalescing into a block of larger size.
  691. *
  692. * -- nyc
  693. */
  694. static inline void __free_one_page(struct page *page,
  695. unsigned long pfn,
  696. struct zone *zone, unsigned int order,
  697. int migratetype)
  698. {
  699. unsigned long combined_pfn;
  700. unsigned long uninitialized_var(buddy_pfn);
  701. struct page *buddy;
  702. unsigned int max_order;
  703. max_order = min_t(unsigned int, MAX_ORDER, pageblock_order + 1);
  704. VM_BUG_ON(!zone_is_initialized(zone));
  705. VM_BUG_ON_PAGE(page->flags & PAGE_FLAGS_CHECK_AT_PREP, page);
  706. VM_BUG_ON(migratetype == -1);
  707. if (likely(!is_migrate_isolate(migratetype)))
  708. __mod_zone_freepage_state(zone, 1 << order, migratetype);
  709. VM_BUG_ON_PAGE(pfn & ((1 << order) - 1), page);
  710. VM_BUG_ON_PAGE(bad_range(zone, page), page);
  711. continue_merging:
  712. while (order < max_order - 1) {
  713. buddy_pfn = __find_buddy_pfn(pfn, order);
  714. buddy = page + (buddy_pfn - pfn);
  715. if (!pfn_valid_within(buddy_pfn))
  716. goto done_merging;
  717. if (!page_is_buddy(page, buddy, order))
  718. goto done_merging;
  719. /*
  720. * Our buddy is free or it is CONFIG_DEBUG_PAGEALLOC guard page,
  721. * merge with it and move up one order.
  722. */
  723. if (page_is_guard(buddy)) {
  724. clear_page_guard(zone, buddy, order, migratetype);
  725. } else {
  726. list_del(&buddy->lru);
  727. zone->free_area[order].nr_free--;
  728. rmv_page_order(buddy);
  729. }
  730. combined_pfn = buddy_pfn & pfn;
  731. page = page + (combined_pfn - pfn);
  732. pfn = combined_pfn;
  733. order++;
  734. }
  735. if (max_order < MAX_ORDER) {
  736. /* If we are here, it means order is >= pageblock_order.
  737. * We want to prevent merge between freepages on isolate
  738. * pageblock and normal pageblock. Without this, pageblock
  739. * isolation could cause incorrect freepage or CMA accounting.
  740. *
  741. * We don't want to hit this code for the more frequent
  742. * low-order merging.
  743. */
  744. if (unlikely(has_isolate_pageblock(zone))) {
  745. int buddy_mt;
  746. buddy_pfn = __find_buddy_pfn(pfn, order);
  747. buddy = page + (buddy_pfn - pfn);
  748. buddy_mt = get_pageblock_migratetype(buddy);
  749. if (migratetype != buddy_mt
  750. && (is_migrate_isolate(migratetype) ||
  751. is_migrate_isolate(buddy_mt)))
  752. goto done_merging;
  753. }
  754. max_order++;
  755. goto continue_merging;
  756. }
  757. done_merging:
  758. set_page_order(page, order);
  759. /*
  760. * If this is not the largest possible page, check if the buddy
  761. * of the next-highest order is free. If it is, it's possible
  762. * that pages are being freed that will coalesce soon. In case,
  763. * that is happening, add the free page to the tail of the list
  764. * so it's less likely to be used soon and more likely to be merged
  765. * as a higher order page
  766. */
  767. if ((order < MAX_ORDER-2) && pfn_valid_within(buddy_pfn)) {
  768. struct page *higher_page, *higher_buddy;
  769. combined_pfn = buddy_pfn & pfn;
  770. higher_page = page + (combined_pfn - pfn);
  771. buddy_pfn = __find_buddy_pfn(combined_pfn, order + 1);
  772. higher_buddy = higher_page + (buddy_pfn - combined_pfn);
  773. if (pfn_valid_within(buddy_pfn) &&
  774. page_is_buddy(higher_page, higher_buddy, order + 1)) {
  775. list_add_tail(&page->lru,
  776. &zone->free_area[order].free_list[migratetype]);
  777. goto out;
  778. }
  779. }
  780. list_add(&page->lru, &zone->free_area[order].free_list[migratetype]);
  781. out:
  782. zone->free_area[order].nr_free++;
  783. }
  784. /*
  785. * A bad page could be due to a number of fields. Instead of multiple branches,
  786. * try and check multiple fields with one check. The caller must do a detailed
  787. * check if necessary.
  788. */
  789. static inline bool page_expected_state(struct page *page,
  790. unsigned long check_flags)
  791. {
  792. if (unlikely(atomic_read(&page->_mapcount) != -1))
  793. return false;
  794. if (unlikely((unsigned long)page->mapping |
  795. page_ref_count(page) |
  796. #ifdef CONFIG_MEMCG
  797. (unsigned long)page->mem_cgroup |
  798. #endif
  799. (page->flags & check_flags)))
  800. return false;
  801. return true;
  802. }
  803. static void free_pages_check_bad(struct page *page)
  804. {
  805. const char *bad_reason;
  806. unsigned long bad_flags;
  807. bad_reason = NULL;
  808. bad_flags = 0;
  809. if (unlikely(atomic_read(&page->_mapcount) != -1))
  810. bad_reason = "nonzero mapcount";
  811. if (unlikely(page->mapping != NULL))
  812. bad_reason = "non-NULL mapping";
  813. if (unlikely(page_ref_count(page) != 0))
  814. bad_reason = "nonzero _refcount";
  815. if (unlikely(page->flags & PAGE_FLAGS_CHECK_AT_FREE)) {
  816. bad_reason = "PAGE_FLAGS_CHECK_AT_FREE flag(s) set";
  817. bad_flags = PAGE_FLAGS_CHECK_AT_FREE;
  818. }
  819. #ifdef CONFIG_MEMCG
  820. if (unlikely(page->mem_cgroup))
  821. bad_reason = "page still charged to cgroup";
  822. #endif
  823. bad_page(page, bad_reason, bad_flags);
  824. }
  825. static inline int free_pages_check(struct page *page)
  826. {
  827. if (likely(page_expected_state(page, PAGE_FLAGS_CHECK_AT_FREE)))
  828. return 0;
  829. /* Something has gone sideways, find it */
  830. free_pages_check_bad(page);
  831. return 1;
  832. }
  833. static int free_tail_pages_check(struct page *head_page, struct page *page)
  834. {
  835. int ret = 1;
  836. /*
  837. * We rely page->lru.next never has bit 0 set, unless the page
  838. * is PageTail(). Let's make sure that's true even for poisoned ->lru.
  839. */
  840. BUILD_BUG_ON((unsigned long)LIST_POISON1 & 1);
  841. if (!IS_ENABLED(CONFIG_DEBUG_VM)) {
  842. ret = 0;
  843. goto out;
  844. }
  845. switch (page - head_page) {
  846. case 1:
  847. /* the first tail page: ->mapping is compound_mapcount() */
  848. if (unlikely(compound_mapcount(page))) {
  849. bad_page(page, "nonzero compound_mapcount", 0);
  850. goto out;
  851. }
  852. break;
  853. case 2:
  854. /*
  855. * the second tail page: ->mapping is
  856. * page_deferred_list().next -- ignore value.
  857. */
  858. break;
  859. default:
  860. if (page->mapping != TAIL_MAPPING) {
  861. bad_page(page, "corrupted mapping in tail page", 0);
  862. goto out;
  863. }
  864. break;
  865. }
  866. if (unlikely(!PageTail(page))) {
  867. bad_page(page, "PageTail not set", 0);
  868. goto out;
  869. }
  870. if (unlikely(compound_head(page) != head_page)) {
  871. bad_page(page, "compound_head not consistent", 0);
  872. goto out;
  873. }
  874. ret = 0;
  875. out:
  876. page->mapping = NULL;
  877. clear_compound_head(page);
  878. return ret;
  879. }
  880. static __always_inline bool free_pages_prepare(struct page *page,
  881. unsigned int order, bool check_free)
  882. {
  883. int bad = 0;
  884. VM_BUG_ON_PAGE(PageTail(page), page);
  885. trace_mm_page_free(page, order);
  886. kmemcheck_free_shadow(page, order);
  887. /*
  888. * Check tail pages before head page information is cleared to
  889. * avoid checking PageCompound for order-0 pages.
  890. */
  891. if (unlikely(order)) {
  892. bool compound = PageCompound(page);
  893. int i;
  894. VM_BUG_ON_PAGE(compound && compound_order(page) != order, page);
  895. if (compound)
  896. ClearPageDoubleMap(page);
  897. for (i = 1; i < (1 << order); i++) {
  898. if (compound)
  899. bad += free_tail_pages_check(page, page + i);
  900. if (unlikely(free_pages_check(page + i))) {
  901. bad++;
  902. continue;
  903. }
  904. (page + i)->flags &= ~PAGE_FLAGS_CHECK_AT_PREP;
  905. }
  906. }
  907. if (PageMappingFlags(page))
  908. page->mapping = NULL;
  909. if (memcg_kmem_enabled() && PageKmemcg(page))
  910. memcg_kmem_uncharge(page, order);
  911. if (check_free)
  912. bad += free_pages_check(page);
  913. if (bad)
  914. return false;
  915. page_cpupid_reset_last(page);
  916. page->flags &= ~PAGE_FLAGS_CHECK_AT_PREP;
  917. reset_page_owner(page, order);
  918. if (!PageHighMem(page)) {
  919. debug_check_no_locks_freed(page_address(page),
  920. PAGE_SIZE << order);
  921. debug_check_no_obj_freed(page_address(page),
  922. PAGE_SIZE << order);
  923. }
  924. arch_free_page(page, order);
  925. kernel_poison_pages(page, 1 << order, 0);
  926. kernel_map_pages(page, 1 << order, 0);
  927. kasan_free_pages(page, order);
  928. return true;
  929. }
  930. #ifdef CONFIG_DEBUG_VM
  931. static inline bool free_pcp_prepare(struct page *page)
  932. {
  933. return free_pages_prepare(page, 0, true);
  934. }
  935. static inline bool bulkfree_pcp_prepare(struct page *page)
  936. {
  937. return false;
  938. }
  939. #else
  940. static bool free_pcp_prepare(struct page *page)
  941. {
  942. return free_pages_prepare(page, 0, false);
  943. }
  944. static bool bulkfree_pcp_prepare(struct page *page)
  945. {
  946. return free_pages_check(page);
  947. }
  948. #endif /* CONFIG_DEBUG_VM */
  949. /*
  950. * Frees a number of pages from the PCP lists
  951. * Assumes all pages on list are in same zone, and of same order.
  952. * count is the number of pages to free.
  953. *
  954. * If the zone was previously in an "all pages pinned" state then look to
  955. * see if this freeing clears that state.
  956. *
  957. * And clear the zone's pages_scanned counter, to hold off the "all pages are
  958. * pinned" detection logic.
  959. */
  960. static void free_pcppages_bulk(struct zone *zone, int count,
  961. struct per_cpu_pages *pcp)
  962. {
  963. int migratetype = 0;
  964. int batch_free = 0;
  965. bool isolated_pageblocks;
  966. spin_lock(&zone->lock);
  967. isolated_pageblocks = has_isolate_pageblock(zone);
  968. while (count) {
  969. struct page *page;
  970. struct list_head *list;
  971. /*
  972. * Remove pages from lists in a round-robin fashion. A
  973. * batch_free count is maintained that is incremented when an
  974. * empty list is encountered. This is so more pages are freed
  975. * off fuller lists instead of spinning excessively around empty
  976. * lists
  977. */
  978. do {
  979. batch_free++;
  980. if (++migratetype == MIGRATE_PCPTYPES)
  981. migratetype = 0;
  982. list = &pcp->lists[migratetype];
  983. } while (list_empty(list));
  984. /* This is the only non-empty list. Free them all. */
  985. if (batch_free == MIGRATE_PCPTYPES)
  986. batch_free = count;
  987. do {
  988. int mt; /* migratetype of the to-be-freed page */
  989. page = list_last_entry(list, struct page, lru);
  990. /* must delete as __free_one_page list manipulates */
  991. list_del(&page->lru);
  992. mt = get_pcppage_migratetype(page);
  993. /* MIGRATE_ISOLATE page should not go to pcplists */
  994. VM_BUG_ON_PAGE(is_migrate_isolate(mt), page);
  995. /* Pageblock could have been isolated meanwhile */
  996. if (unlikely(isolated_pageblocks))
  997. mt = get_pageblock_migratetype(page);
  998. if (bulkfree_pcp_prepare(page))
  999. continue;
  1000. __free_one_page(page, page_to_pfn(page), zone, 0, mt);
  1001. trace_mm_page_pcpu_drain(page, 0, mt);
  1002. } while (--count && --batch_free && !list_empty(list));
  1003. }
  1004. spin_unlock(&zone->lock);
  1005. }
  1006. static void free_one_page(struct zone *zone,
  1007. struct page *page, unsigned long pfn,
  1008. unsigned int order,
  1009. int migratetype)
  1010. {
  1011. spin_lock(&zone->lock);
  1012. if (unlikely(has_isolate_pageblock(zone) ||
  1013. is_migrate_isolate(migratetype))) {
  1014. migratetype = get_pfnblock_migratetype(page, pfn);
  1015. }
  1016. __free_one_page(page, pfn, zone, order, migratetype);
  1017. spin_unlock(&zone->lock);
  1018. }
  1019. static void __meminit __init_single_page(struct page *page, unsigned long pfn,
  1020. unsigned long zone, int nid)
  1021. {
  1022. set_page_links(page, zone, nid, pfn);
  1023. init_page_count(page);
  1024. page_mapcount_reset(page);
  1025. page_cpupid_reset_last(page);
  1026. INIT_LIST_HEAD(&page->lru);
  1027. #ifdef WANT_PAGE_VIRTUAL
  1028. /* The shift won't overflow because ZONE_NORMAL is below 4G. */
  1029. if (!is_highmem_idx(zone))
  1030. set_page_address(page, __va(pfn << PAGE_SHIFT));
  1031. #endif
  1032. }
  1033. static void __meminit __init_single_pfn(unsigned long pfn, unsigned long zone,
  1034. int nid)
  1035. {
  1036. return __init_single_page(pfn_to_page(pfn), pfn, zone, nid);
  1037. }
  1038. #ifdef CONFIG_DEFERRED_STRUCT_PAGE_INIT
  1039. static void init_reserved_page(unsigned long pfn)
  1040. {
  1041. pg_data_t *pgdat;
  1042. int nid, zid;
  1043. if (!early_page_uninitialised(pfn))
  1044. return;
  1045. nid = early_pfn_to_nid(pfn);
  1046. pgdat = NODE_DATA(nid);
  1047. for (zid = 0; zid < MAX_NR_ZONES; zid++) {
  1048. struct zone *zone = &pgdat->node_zones[zid];
  1049. if (pfn >= zone->zone_start_pfn && pfn < zone_end_pfn(zone))
  1050. break;
  1051. }
  1052. __init_single_pfn(pfn, zid, nid);
  1053. }
  1054. #else
  1055. static inline void init_reserved_page(unsigned long pfn)
  1056. {
  1057. }
  1058. #endif /* CONFIG_DEFERRED_STRUCT_PAGE_INIT */
  1059. /*
  1060. * Initialised pages do not have PageReserved set. This function is
  1061. * called for each range allocated by the bootmem allocator and
  1062. * marks the pages PageReserved. The remaining valid pages are later
  1063. * sent to the buddy page allocator.
  1064. */
  1065. void __meminit reserve_bootmem_region(phys_addr_t start, phys_addr_t end)
  1066. {
  1067. unsigned long start_pfn = PFN_DOWN(start);
  1068. unsigned long end_pfn = PFN_UP(end);
  1069. for (; start_pfn < end_pfn; start_pfn++) {
  1070. if (pfn_valid(start_pfn)) {
  1071. struct page *page = pfn_to_page(start_pfn);
  1072. init_reserved_page(start_pfn);
  1073. /* Avoid false-positive PageTail() */
  1074. INIT_LIST_HEAD(&page->lru);
  1075. SetPageReserved(page);
  1076. }
  1077. }
  1078. }
  1079. static void __free_pages_ok(struct page *page, unsigned int order)
  1080. {
  1081. unsigned long flags;
  1082. int migratetype;
  1083. unsigned long pfn = page_to_pfn(page);
  1084. if (!free_pages_prepare(page, order, true))
  1085. return;
  1086. migratetype = get_pfnblock_migratetype(page, pfn);
  1087. local_irq_save(flags);
  1088. __count_vm_events(PGFREE, 1 << order);
  1089. free_one_page(page_zone(page), page, pfn, order, migratetype);
  1090. local_irq_restore(flags);
  1091. }
  1092. static void __init __free_pages_boot_core(struct page *page, unsigned int order)
  1093. {
  1094. unsigned int nr_pages = 1 << order;
  1095. struct page *p = page;
  1096. unsigned int loop;
  1097. prefetchw(p);
  1098. for (loop = 0; loop < (nr_pages - 1); loop++, p++) {
  1099. prefetchw(p + 1);
  1100. __ClearPageReserved(p);
  1101. set_page_count(p, 0);
  1102. }
  1103. __ClearPageReserved(p);
  1104. set_page_count(p, 0);
  1105. page_zone(page)->managed_pages += nr_pages;
  1106. set_page_refcounted(page);
  1107. __free_pages(page, order);
  1108. }
  1109. #if defined(CONFIG_HAVE_ARCH_EARLY_PFN_TO_NID) || \
  1110. defined(CONFIG_HAVE_MEMBLOCK_NODE_MAP)
  1111. static struct mminit_pfnnid_cache early_pfnnid_cache __meminitdata;
  1112. int __meminit early_pfn_to_nid(unsigned long pfn)
  1113. {
  1114. static DEFINE_SPINLOCK(early_pfn_lock);
  1115. int nid;
  1116. spin_lock(&early_pfn_lock);
  1117. nid = __early_pfn_to_nid(pfn, &early_pfnnid_cache);
  1118. if (nid < 0)
  1119. nid = first_online_node;
  1120. spin_unlock(&early_pfn_lock);
  1121. return nid;
  1122. }
  1123. #endif
  1124. #ifdef CONFIG_NODES_SPAN_OTHER_NODES
  1125. static inline bool __meminit __maybe_unused
  1126. meminit_pfn_in_nid(unsigned long pfn, int node,
  1127. struct mminit_pfnnid_cache *state)
  1128. {
  1129. int nid;
  1130. nid = __early_pfn_to_nid(pfn, state);
  1131. if (nid >= 0 && nid != node)
  1132. return false;
  1133. return true;
  1134. }
  1135. /* Only safe to use early in boot when initialisation is single-threaded */
  1136. static inline bool __meminit early_pfn_in_nid(unsigned long pfn, int node)
  1137. {
  1138. return meminit_pfn_in_nid(pfn, node, &early_pfnnid_cache);
  1139. }
  1140. #else
  1141. static inline bool __meminit early_pfn_in_nid(unsigned long pfn, int node)
  1142. {
  1143. return true;
  1144. }
  1145. static inline bool __meminit __maybe_unused
  1146. meminit_pfn_in_nid(unsigned long pfn, int node,
  1147. struct mminit_pfnnid_cache *state)
  1148. {
  1149. return true;
  1150. }
  1151. #endif
  1152. void __init __free_pages_bootmem(struct page *page, unsigned long pfn,
  1153. unsigned int order)
  1154. {
  1155. if (early_page_uninitialised(pfn))
  1156. return;
  1157. return __free_pages_boot_core(page, order);
  1158. }
  1159. /*
  1160. * Check that the whole (or subset of) a pageblock given by the interval of
  1161. * [start_pfn, end_pfn) is valid and within the same zone, before scanning it
  1162. * with the migration of free compaction scanner. The scanners then need to
  1163. * use only pfn_valid_within() check for arches that allow holes within
  1164. * pageblocks.
  1165. *
  1166. * Return struct page pointer of start_pfn, or NULL if checks were not passed.
  1167. *
  1168. * It's possible on some configurations to have a setup like node0 node1 node0
  1169. * i.e. it's possible that all pages within a zones range of pages do not
  1170. * belong to a single zone. We assume that a border between node0 and node1
  1171. * can occur within a single pageblock, but not a node0 node1 node0
  1172. * interleaving within a single pageblock. It is therefore sufficient to check
  1173. * the first and last page of a pageblock and avoid checking each individual
  1174. * page in a pageblock.
  1175. */
  1176. struct page *__pageblock_pfn_to_page(unsigned long start_pfn,
  1177. unsigned long end_pfn, struct zone *zone)
  1178. {
  1179. struct page *start_page;
  1180. struct page *end_page;
  1181. /* end_pfn is one past the range we are checking */
  1182. end_pfn--;
  1183. if (!pfn_valid(start_pfn) || !pfn_valid(end_pfn))
  1184. return NULL;
  1185. start_page = pfn_to_online_page(start_pfn);
  1186. if (!start_page)
  1187. return NULL;
  1188. if (page_zone(start_page) != zone)
  1189. return NULL;
  1190. end_page = pfn_to_page(end_pfn);
  1191. /* This gives a shorter code than deriving page_zone(end_page) */
  1192. if (page_zone_id(start_page) != page_zone_id(end_page))
  1193. return NULL;
  1194. return start_page;
  1195. }
  1196. void set_zone_contiguous(struct zone *zone)
  1197. {
  1198. unsigned long block_start_pfn = zone->zone_start_pfn;
  1199. unsigned long block_end_pfn;
  1200. block_end_pfn = ALIGN(block_start_pfn + 1, pageblock_nr_pages);
  1201. for (; block_start_pfn < zone_end_pfn(zone);
  1202. block_start_pfn = block_end_pfn,
  1203. block_end_pfn += pageblock_nr_pages) {
  1204. block_end_pfn = min(block_end_pfn, zone_end_pfn(zone));
  1205. if (!__pageblock_pfn_to_page(block_start_pfn,
  1206. block_end_pfn, zone))
  1207. return;
  1208. }
  1209. /* We confirm that there is no hole */
  1210. zone->contiguous = true;
  1211. }
  1212. void clear_zone_contiguous(struct zone *zone)
  1213. {
  1214. zone->contiguous = false;
  1215. }
  1216. #ifdef CONFIG_DEFERRED_STRUCT_PAGE_INIT
  1217. static void __init deferred_free_range(struct page *page,
  1218. unsigned long pfn, int nr_pages)
  1219. {
  1220. int i;
  1221. if (!page)
  1222. return;
  1223. /* Free a large naturally-aligned chunk if possible */
  1224. if (nr_pages == pageblock_nr_pages &&
  1225. (pfn & (pageblock_nr_pages - 1)) == 0) {
  1226. set_pageblock_migratetype(page, MIGRATE_MOVABLE);
  1227. __free_pages_boot_core(page, pageblock_order);
  1228. return;
  1229. }
  1230. for (i = 0; i < nr_pages; i++, page++, pfn++) {
  1231. if ((pfn & (pageblock_nr_pages - 1)) == 0)
  1232. set_pageblock_migratetype(page, MIGRATE_MOVABLE);
  1233. __free_pages_boot_core(page, 0);
  1234. }
  1235. }
  1236. /* Completion tracking for deferred_init_memmap() threads */
  1237. static atomic_t pgdat_init_n_undone __initdata;
  1238. static __initdata DECLARE_COMPLETION(pgdat_init_all_done_comp);
  1239. static inline void __init pgdat_init_report_one_done(void)
  1240. {
  1241. if (atomic_dec_and_test(&pgdat_init_n_undone))
  1242. complete(&pgdat_init_all_done_comp);
  1243. }
  1244. /* Initialise remaining memory on a node */
  1245. static int __init deferred_init_memmap(void *data)
  1246. {
  1247. pg_data_t *pgdat = data;
  1248. int nid = pgdat->node_id;
  1249. struct mminit_pfnnid_cache nid_init_state = { };
  1250. unsigned long start = jiffies;
  1251. unsigned long nr_pages = 0;
  1252. unsigned long walk_start, walk_end;
  1253. int i, zid;
  1254. struct zone *zone;
  1255. unsigned long first_init_pfn = pgdat->first_deferred_pfn;
  1256. const struct cpumask *cpumask = cpumask_of_node(pgdat->node_id);
  1257. if (first_init_pfn == ULONG_MAX) {
  1258. pgdat_init_report_one_done();
  1259. return 0;
  1260. }
  1261. /* Bind memory initialisation thread to a local node if possible */
  1262. if (!cpumask_empty(cpumask))
  1263. set_cpus_allowed_ptr(current, cpumask);
  1264. /* Sanity check boundaries */
  1265. BUG_ON(pgdat->first_deferred_pfn < pgdat->node_start_pfn);
  1266. BUG_ON(pgdat->first_deferred_pfn > pgdat_end_pfn(pgdat));
  1267. pgdat->first_deferred_pfn = ULONG_MAX;
  1268. /* Only the highest zone is deferred so find it */
  1269. for (zid = 0; zid < MAX_NR_ZONES; zid++) {
  1270. zone = pgdat->node_zones + zid;
  1271. if (first_init_pfn < zone_end_pfn(zone))
  1272. break;
  1273. }
  1274. for_each_mem_pfn_range(i, nid, &walk_start, &walk_end, NULL) {
  1275. unsigned long pfn, end_pfn;
  1276. struct page *page = NULL;
  1277. struct page *free_base_page = NULL;
  1278. unsigned long free_base_pfn = 0;
  1279. int nr_to_free = 0;
  1280. end_pfn = min(walk_end, zone_end_pfn(zone));
  1281. pfn = first_init_pfn;
  1282. if (pfn < walk_start)
  1283. pfn = walk_start;
  1284. if (pfn < zone->zone_start_pfn)
  1285. pfn = zone->zone_start_pfn;
  1286. for (; pfn < end_pfn; pfn++) {
  1287. if (!pfn_valid_within(pfn))
  1288. goto free_range;
  1289. /*
  1290. * Ensure pfn_valid is checked every
  1291. * pageblock_nr_pages for memory holes
  1292. */
  1293. if ((pfn & (pageblock_nr_pages - 1)) == 0) {
  1294. if (!pfn_valid(pfn)) {
  1295. page = NULL;
  1296. goto free_range;
  1297. }
  1298. }
  1299. if (!meminit_pfn_in_nid(pfn, nid, &nid_init_state)) {
  1300. page = NULL;
  1301. goto free_range;
  1302. }
  1303. /* Minimise pfn page lookups and scheduler checks */
  1304. if (page && (pfn & (pageblock_nr_pages - 1)) != 0) {
  1305. page++;
  1306. } else {
  1307. nr_pages += nr_to_free;
  1308. deferred_free_range(free_base_page,
  1309. free_base_pfn, nr_to_free);
  1310. free_base_page = NULL;
  1311. free_base_pfn = nr_to_free = 0;
  1312. page = pfn_to_page(pfn);
  1313. cond_resched();
  1314. }
  1315. if (page->flags) {
  1316. VM_BUG_ON(page_zone(page) != zone);
  1317. goto free_range;
  1318. }
  1319. __init_single_page(page, pfn, zid, nid);
  1320. if (!free_base_page) {
  1321. free_base_page = page;
  1322. free_base_pfn = pfn;
  1323. nr_to_free = 0;
  1324. }
  1325. nr_to_free++;
  1326. /* Where possible, batch up pages for a single free */
  1327. continue;
  1328. free_range:
  1329. /* Free the current block of pages to allocator */
  1330. nr_pages += nr_to_free;
  1331. deferred_free_range(free_base_page, free_base_pfn,
  1332. nr_to_free);
  1333. free_base_page = NULL;
  1334. free_base_pfn = nr_to_free = 0;
  1335. }
  1336. /* Free the last block of pages to allocator */
  1337. nr_pages += nr_to_free;
  1338. deferred_free_range(free_base_page, free_base_pfn, nr_to_free);
  1339. first_init_pfn = max(end_pfn, first_init_pfn);
  1340. }
  1341. /* Sanity check that the next zone really is unpopulated */
  1342. WARN_ON(++zid < MAX_NR_ZONES && populated_zone(++zone));
  1343. pr_info("node %d initialised, %lu pages in %ums\n", nid, nr_pages,
  1344. jiffies_to_msecs(jiffies - start));
  1345. pgdat_init_report_one_done();
  1346. return 0;
  1347. }
  1348. #endif /* CONFIG_DEFERRED_STRUCT_PAGE_INIT */
  1349. void __init page_alloc_init_late(void)
  1350. {
  1351. struct zone *zone;
  1352. #ifdef CONFIG_DEFERRED_STRUCT_PAGE_INIT
  1353. int nid;
  1354. /* There will be num_node_state(N_MEMORY) threads */
  1355. atomic_set(&pgdat_init_n_undone, num_node_state(N_MEMORY));
  1356. for_each_node_state(nid, N_MEMORY) {
  1357. kthread_run(deferred_init_memmap, NODE_DATA(nid), "pgdatinit%d", nid);
  1358. }
  1359. /* Block until all are initialised */
  1360. wait_for_completion(&pgdat_init_all_done_comp);
  1361. /* Reinit limits that are based on free pages after the kernel is up */
  1362. files_maxfiles_init();
  1363. #endif
  1364. #ifdef CONFIG_ARCH_DISCARD_MEMBLOCK
  1365. /* Discard memblock private memory */
  1366. memblock_discard();
  1367. #endif
  1368. for_each_populated_zone(zone)
  1369. set_zone_contiguous(zone);
  1370. }
  1371. #ifdef CONFIG_CMA
  1372. /* Free whole pageblock and set its migration type to MIGRATE_CMA. */
  1373. void __init init_cma_reserved_pageblock(struct page *page)
  1374. {
  1375. unsigned i = pageblock_nr_pages;
  1376. struct page *p = page;
  1377. do {
  1378. __ClearPageReserved(p);
  1379. set_page_count(p, 0);
  1380. } while (++p, --i);
  1381. set_pageblock_migratetype(page, MIGRATE_CMA);
  1382. if (pageblock_order >= MAX_ORDER) {
  1383. i = pageblock_nr_pages;
  1384. p = page;
  1385. do {
  1386. set_page_refcounted(p);
  1387. __free_pages(p, MAX_ORDER - 1);
  1388. p += MAX_ORDER_NR_PAGES;
  1389. } while (i -= MAX_ORDER_NR_PAGES);
  1390. } else {
  1391. set_page_refcounted(page);
  1392. __free_pages(page, pageblock_order);
  1393. }
  1394. adjust_managed_page_count(page, pageblock_nr_pages);
  1395. }
  1396. #endif
  1397. /*
  1398. * The order of subdivision here is critical for the IO subsystem.
  1399. * Please do not alter this order without good reasons and regression
  1400. * testing. Specifically, as large blocks of memory are subdivided,
  1401. * the order in which smaller blocks are delivered depends on the order
  1402. * they're subdivided in this function. This is the primary factor
  1403. * influencing the order in which pages are delivered to the IO
  1404. * subsystem according to empirical testing, and this is also justified
  1405. * by considering the behavior of a buddy system containing a single
  1406. * large block of memory acted on by a series of small allocations.
  1407. * This behavior is a critical factor in sglist merging's success.
  1408. *
  1409. * -- nyc
  1410. */
  1411. static inline void expand(struct zone *zone, struct page *page,
  1412. int low, int high, struct free_area *area,
  1413. int migratetype)
  1414. {
  1415. unsigned long size = 1 << high;
  1416. while (high > low) {
  1417. area--;
  1418. high--;
  1419. size >>= 1;
  1420. VM_BUG_ON_PAGE(bad_range(zone, &page[size]), &page[size]);
  1421. /*
  1422. * Mark as guard pages (or page), that will allow to
  1423. * merge back to allocator when buddy will be freed.
  1424. * Corresponding page table entries will not be touched,
  1425. * pages will stay not present in virtual address space
  1426. */
  1427. if (set_page_guard(zone, &page[size], high, migratetype))
  1428. continue;
  1429. list_add(&page[size].lru, &area->free_list[migratetype]);
  1430. area->nr_free++;
  1431. set_page_order(&page[size], high);
  1432. }
  1433. }
  1434. static void check_new_page_bad(struct page *page)
  1435. {
  1436. const char *bad_reason = NULL;
  1437. unsigned long bad_flags = 0;
  1438. if (unlikely(atomic_read(&page->_mapcount) != -1))
  1439. bad_reason = "nonzero mapcount";
  1440. if (unlikely(page->mapping != NULL))
  1441. bad_reason = "non-NULL mapping";
  1442. if (unlikely(page_ref_count(page) != 0))
  1443. bad_reason = "nonzero _count";
  1444. if (unlikely(page->flags & __PG_HWPOISON)) {
  1445. bad_reason = "HWPoisoned (hardware-corrupted)";
  1446. bad_flags = __PG_HWPOISON;
  1447. /* Don't complain about hwpoisoned pages */
  1448. page_mapcount_reset(page); /* remove PageBuddy */
  1449. return;
  1450. }
  1451. if (unlikely(page->flags & PAGE_FLAGS_CHECK_AT_PREP)) {
  1452. bad_reason = "PAGE_FLAGS_CHECK_AT_PREP flag set";
  1453. bad_flags = PAGE_FLAGS_CHECK_AT_PREP;
  1454. }
  1455. #ifdef CONFIG_MEMCG
  1456. if (unlikely(page->mem_cgroup))
  1457. bad_reason = "page still charged to cgroup";
  1458. #endif
  1459. bad_page(page, bad_reason, bad_flags);
  1460. }
  1461. /*
  1462. * This page is about to be returned from the page allocator
  1463. */
  1464. static inline int check_new_page(struct page *page)
  1465. {
  1466. if (likely(page_expected_state(page,
  1467. PAGE_FLAGS_CHECK_AT_PREP|__PG_HWPOISON)))
  1468. return 0;
  1469. check_new_page_bad(page);
  1470. return 1;
  1471. }
  1472. static inline bool free_pages_prezeroed(void)
  1473. {
  1474. return IS_ENABLED(CONFIG_PAGE_POISONING_ZERO) &&
  1475. page_poisoning_enabled();
  1476. }
  1477. #ifdef CONFIG_DEBUG_VM
  1478. static bool check_pcp_refill(struct page *page)
  1479. {
  1480. return false;
  1481. }
  1482. static bool check_new_pcp(struct page *page)
  1483. {
  1484. return check_new_page(page);
  1485. }
  1486. #else
  1487. static bool check_pcp_refill(struct page *page)
  1488. {
  1489. return check_new_page(page);
  1490. }
  1491. static bool check_new_pcp(struct page *page)
  1492. {
  1493. return false;
  1494. }
  1495. #endif /* CONFIG_DEBUG_VM */
  1496. static bool check_new_pages(struct page *page, unsigned int order)
  1497. {
  1498. int i;
  1499. for (i = 0; i < (1 << order); i++) {
  1500. struct page *p = page + i;
  1501. if (unlikely(check_new_page(p)))
  1502. return true;
  1503. }
  1504. return false;
  1505. }
  1506. inline void post_alloc_hook(struct page *page, unsigned int order,
  1507. gfp_t gfp_flags)
  1508. {
  1509. set_page_private(page, 0);
  1510. set_page_refcounted(page);
  1511. arch_alloc_page(page, order);
  1512. kernel_map_pages(page, 1 << order, 1);
  1513. kernel_poison_pages(page, 1 << order, 1);
  1514. kasan_alloc_pages(page, order);
  1515. set_page_owner(page, order, gfp_flags);
  1516. }
  1517. static void prep_new_page(struct page *page, unsigned int order, gfp_t gfp_flags,
  1518. unsigned int alloc_flags)
  1519. {
  1520. int i;
  1521. post_alloc_hook(page, order, gfp_flags);
  1522. if (!free_pages_prezeroed() && (gfp_flags & __GFP_ZERO))
  1523. for (i = 0; i < (1 << order); i++)
  1524. clear_highpage(page + i);
  1525. if (order && (gfp_flags & __GFP_COMP))
  1526. prep_compound_page(page, order);
  1527. /*
  1528. * page is set pfmemalloc when ALLOC_NO_WATERMARKS was necessary to
  1529. * allocate the page. The expectation is that the caller is taking
  1530. * steps that will free more memory. The caller should avoid the page
  1531. * being used for !PFMEMALLOC purposes.
  1532. */
  1533. if (alloc_flags & ALLOC_NO_WATERMARKS)
  1534. set_page_pfmemalloc(page);
  1535. else
  1536. clear_page_pfmemalloc(page);
  1537. }
  1538. /*
  1539. * Go through the free lists for the given migratetype and remove
  1540. * the smallest available page from the freelists
  1541. */
  1542. static inline
  1543. struct page *__rmqueue_smallest(struct zone *zone, unsigned int order,
  1544. int migratetype)
  1545. {
  1546. unsigned int current_order;
  1547. struct free_area *area;
  1548. struct page *page;
  1549. /* Find a page of the appropriate size in the preferred list */
  1550. for (current_order = order; current_order < MAX_ORDER; ++current_order) {
  1551. area = &(zone->free_area[current_order]);
  1552. page = list_first_entry_or_null(&area->free_list[migratetype],
  1553. struct page, lru);
  1554. if (!page)
  1555. continue;
  1556. list_del(&page->lru);
  1557. rmv_page_order(page);
  1558. area->nr_free--;
  1559. expand(zone, page, order, current_order, area, migratetype);
  1560. set_pcppage_migratetype(page, migratetype);
  1561. return page;
  1562. }
  1563. return NULL;
  1564. }
  1565. /*
  1566. * This array describes the order lists are fallen back to when
  1567. * the free lists for the desirable migrate type are depleted
  1568. */
  1569. static int fallbacks[MIGRATE_TYPES][4] = {
  1570. [MIGRATE_UNMOVABLE] = { MIGRATE_RECLAIMABLE, MIGRATE_MOVABLE, MIGRATE_TYPES },
  1571. [MIGRATE_RECLAIMABLE] = { MIGRATE_UNMOVABLE, MIGRATE_MOVABLE, MIGRATE_TYPES },
  1572. [MIGRATE_MOVABLE] = { MIGRATE_RECLAIMABLE, MIGRATE_UNMOVABLE, MIGRATE_TYPES },
  1573. #ifdef CONFIG_CMA
  1574. [MIGRATE_CMA] = { MIGRATE_TYPES }, /* Never used */
  1575. #endif
  1576. #ifdef CONFIG_MEMORY_ISOLATION
  1577. [MIGRATE_ISOLATE] = { MIGRATE_TYPES }, /* Never used */
  1578. #endif
  1579. };
  1580. #ifdef CONFIG_CMA
  1581. static struct page *__rmqueue_cma_fallback(struct zone *zone,
  1582. unsigned int order)
  1583. {
  1584. return __rmqueue_smallest(zone, order, MIGRATE_CMA);
  1585. }
  1586. #else
  1587. static inline struct page *__rmqueue_cma_fallback(struct zone *zone,
  1588. unsigned int order) { return NULL; }
  1589. #endif
  1590. /*
  1591. * Move the free pages in a range to the free lists of the requested type.
  1592. * Note that start_page and end_pages are not aligned on a pageblock
  1593. * boundary. If alignment is required, use move_freepages_block()
  1594. */
  1595. static int move_freepages(struct zone *zone,
  1596. struct page *start_page, struct page *end_page,
  1597. int migratetype, int *num_movable)
  1598. {
  1599. struct page *page;
  1600. unsigned int order;
  1601. int pages_moved = 0;
  1602. #ifndef CONFIG_HOLES_IN_ZONE
  1603. /*
  1604. * page_zone is not safe to call in this context when
  1605. * CONFIG_HOLES_IN_ZONE is set. This bug check is probably redundant
  1606. * anyway as we check zone boundaries in move_freepages_block().
  1607. * Remove at a later date when no bug reports exist related to
  1608. * grouping pages by mobility
  1609. */
  1610. VM_BUG_ON(page_zone(start_page) != page_zone(end_page));
  1611. #endif
  1612. if (num_movable)
  1613. *num_movable = 0;
  1614. for (page = start_page; page <= end_page;) {
  1615. if (!pfn_valid_within(page_to_pfn(page))) {
  1616. page++;
  1617. continue;
  1618. }
  1619. /* Make sure we are not inadvertently changing nodes */
  1620. VM_BUG_ON_PAGE(page_to_nid(page) != zone_to_nid(zone), page);
  1621. if (!PageBuddy(page)) {
  1622. /*
  1623. * We assume that pages that could be isolated for
  1624. * migration are movable. But we don't actually try
  1625. * isolating, as that would be expensive.
  1626. */
  1627. if (num_movable &&
  1628. (PageLRU(page) || __PageMovable(page)))
  1629. (*num_movable)++;
  1630. page++;
  1631. continue;
  1632. }
  1633. order = page_order(page);
  1634. list_move(&page->lru,
  1635. &zone->free_area[order].free_list[migratetype]);
  1636. page += 1 << order;
  1637. pages_moved += 1 << order;
  1638. }
  1639. return pages_moved;
  1640. }
  1641. int move_freepages_block(struct zone *zone, struct page *page,
  1642. int migratetype, int *num_movable)
  1643. {
  1644. unsigned long start_pfn, end_pfn;
  1645. struct page *start_page, *end_page;
  1646. start_pfn = page_to_pfn(page);
  1647. start_pfn = start_pfn & ~(pageblock_nr_pages-1);
  1648. start_page = pfn_to_page(start_pfn);
  1649. end_page = start_page + pageblock_nr_pages - 1;
  1650. end_pfn = start_pfn + pageblock_nr_pages - 1;
  1651. /* Do not cross zone boundaries */
  1652. if (!zone_spans_pfn(zone, start_pfn))
  1653. start_page = page;
  1654. if (!zone_spans_pfn(zone, end_pfn))
  1655. return 0;
  1656. return move_freepages(zone, start_page, end_page, migratetype,
  1657. num_movable);
  1658. }
  1659. static void change_pageblock_range(struct page *pageblock_page,
  1660. int start_order, int migratetype)
  1661. {
  1662. int nr_pageblocks = 1 << (start_order - pageblock_order);
  1663. while (nr_pageblocks--) {
  1664. set_pageblock_migratetype(pageblock_page, migratetype);
  1665. pageblock_page += pageblock_nr_pages;
  1666. }
  1667. }
  1668. /*
  1669. * When we are falling back to another migratetype during allocation, try to
  1670. * steal extra free pages from the same pageblocks to satisfy further
  1671. * allocations, instead of polluting multiple pageblocks.
  1672. *
  1673. * If we are stealing a relatively large buddy page, it is likely there will
  1674. * be more free pages in the pageblock, so try to steal them all. For
  1675. * reclaimable and unmovable allocations, we steal regardless of page size,
  1676. * as fragmentation caused by those allocations polluting movable pageblocks
  1677. * is worse than movable allocations stealing from unmovable and reclaimable
  1678. * pageblocks.
  1679. */
  1680. static bool can_steal_fallback(unsigned int order, int start_mt)
  1681. {
  1682. /*
  1683. * Leaving this order check is intended, although there is
  1684. * relaxed order check in next check. The reason is that
  1685. * we can actually steal whole pageblock if this condition met,
  1686. * but, below check doesn't guarantee it and that is just heuristic
  1687. * so could be changed anytime.
  1688. */
  1689. if (order >= pageblock_order)
  1690. return true;
  1691. if (order >= pageblock_order / 2 ||
  1692. start_mt == MIGRATE_RECLAIMABLE ||
  1693. start_mt == MIGRATE_UNMOVABLE ||
  1694. page_group_by_mobility_disabled)
  1695. return true;
  1696. return false;
  1697. }
  1698. /*
  1699. * This function implements actual steal behaviour. If order is large enough,
  1700. * we can steal whole pageblock. If not, we first move freepages in this
  1701. * pageblock to our migratetype and determine how many already-allocated pages
  1702. * are there in the pageblock with a compatible migratetype. If at least half
  1703. * of pages are free or compatible, we can change migratetype of the pageblock
  1704. * itself, so pages freed in the future will be put on the correct free list.
  1705. */
  1706. static void steal_suitable_fallback(struct zone *zone, struct page *page,
  1707. int start_type, bool whole_block)
  1708. {
  1709. unsigned int current_order = page_order(page);
  1710. struct free_area *area;
  1711. int free_pages, movable_pages, alike_pages;
  1712. int old_block_type;
  1713. old_block_type = get_pageblock_migratetype(page);
  1714. /*
  1715. * This can happen due to races and we want to prevent broken
  1716. * highatomic accounting.
  1717. */
  1718. if (is_migrate_highatomic(old_block_type))
  1719. goto single_page;
  1720. /* Take ownership for orders >= pageblock_order */
  1721. if (current_order >= pageblock_order) {
  1722. change_pageblock_range(page, current_order, start_type);
  1723. goto single_page;
  1724. }
  1725. /* We are not allowed to try stealing from the whole block */
  1726. if (!whole_block)
  1727. goto single_page;
  1728. free_pages = move_freepages_block(zone, page, start_type,
  1729. &movable_pages);
  1730. /*
  1731. * Determine how many pages are compatible with our allocation.
  1732. * For movable allocation, it's the number of movable pages which
  1733. * we just obtained. For other types it's a bit more tricky.
  1734. */
  1735. if (start_type == MIGRATE_MOVABLE) {
  1736. alike_pages = movable_pages;
  1737. } else {
  1738. /*
  1739. * If we are falling back a RECLAIMABLE or UNMOVABLE allocation
  1740. * to MOVABLE pageblock, consider all non-movable pages as
  1741. * compatible. If it's UNMOVABLE falling back to RECLAIMABLE or
  1742. * vice versa, be conservative since we can't distinguish the
  1743. * exact migratetype of non-movable pages.
  1744. */
  1745. if (old_block_type == MIGRATE_MOVABLE)
  1746. alike_pages = pageblock_nr_pages
  1747. - (free_pages + movable_pages);
  1748. else
  1749. alike_pages = 0;
  1750. }
  1751. /* moving whole block can fail due to zone boundary conditions */
  1752. if (!free_pages)
  1753. goto single_page;
  1754. /*
  1755. * If a sufficient number of pages in the block are either free or of
  1756. * comparable migratability as our allocation, claim the whole block.
  1757. */
  1758. if (free_pages + alike_pages >= (1 << (pageblock_order-1)) ||
  1759. page_group_by_mobility_disabled)
  1760. set_pageblock_migratetype(page, start_type);
  1761. return;
  1762. single_page:
  1763. area = &zone->free_area[current_order];
  1764. list_move(&page->lru, &area->free_list[start_type]);
  1765. }
  1766. /*
  1767. * Check whether there is a suitable fallback freepage with requested order.
  1768. * If only_stealable is true, this function returns fallback_mt only if
  1769. * we can steal other freepages all together. This would help to reduce
  1770. * fragmentation due to mixed migratetype pages in one pageblock.
  1771. */
  1772. int find_suitable_fallback(struct free_area *area, unsigned int order,
  1773. int migratetype, bool only_stealable, bool *can_steal)
  1774. {
  1775. int i;
  1776. int fallback_mt;
  1777. if (area->nr_free == 0)
  1778. return -1;
  1779. *can_steal = false;
  1780. for (i = 0;; i++) {
  1781. fallback_mt = fallbacks[migratetype][i];
  1782. if (fallback_mt == MIGRATE_TYPES)
  1783. break;
  1784. if (list_empty(&area->free_list[fallback_mt]))
  1785. continue;
  1786. if (can_steal_fallback(order, migratetype))
  1787. *can_steal = true;
  1788. if (!only_stealable)
  1789. return fallback_mt;
  1790. if (*can_steal)
  1791. return fallback_mt;
  1792. }
  1793. return -1;
  1794. }
  1795. /*
  1796. * Reserve a pageblock for exclusive use of high-order atomic allocations if
  1797. * there are no empty page blocks that contain a page with a suitable order
  1798. */
  1799. static void reserve_highatomic_pageblock(struct page *page, struct zone *zone,
  1800. unsigned int alloc_order)
  1801. {
  1802. int mt;
  1803. unsigned long max_managed, flags;
  1804. /*
  1805. * Limit the number reserved to 1 pageblock or roughly 1% of a zone.
  1806. * Check is race-prone but harmless.
  1807. */
  1808. max_managed = (zone->managed_pages / 100) + pageblock_nr_pages;
  1809. if (zone->nr_reserved_highatomic >= max_managed)
  1810. return;
  1811. spin_lock_irqsave(&zone->lock, flags);
  1812. /* Recheck the nr_reserved_highatomic limit under the lock */
  1813. if (zone->nr_reserved_highatomic >= max_managed)
  1814. goto out_unlock;
  1815. /* Yoink! */
  1816. mt = get_pageblock_migratetype(page);
  1817. if (!is_migrate_highatomic(mt) && !is_migrate_isolate(mt)
  1818. && !is_migrate_cma(mt)) {
  1819. zone->nr_reserved_highatomic += pageblock_nr_pages;
  1820. set_pageblock_migratetype(page, MIGRATE_HIGHATOMIC);
  1821. move_freepages_block(zone, page, MIGRATE_HIGHATOMIC, NULL);
  1822. }
  1823. out_unlock:
  1824. spin_unlock_irqrestore(&zone->lock, flags);
  1825. }
  1826. /*
  1827. * Used when an allocation is about to fail under memory pressure. This
  1828. * potentially hurts the reliability of high-order allocations when under
  1829. * intense memory pressure but failed atomic allocations should be easier
  1830. * to recover from than an OOM.
  1831. *
  1832. * If @force is true, try to unreserve a pageblock even though highatomic
  1833. * pageblock is exhausted.
  1834. */
  1835. static bool unreserve_highatomic_pageblock(const struct alloc_context *ac,
  1836. bool force)
  1837. {
  1838. struct zonelist *zonelist = ac->zonelist;
  1839. unsigned long flags;
  1840. struct zoneref *z;
  1841. struct zone *zone;
  1842. struct page *page;
  1843. int order;
  1844. bool ret;
  1845. for_each_zone_zonelist_nodemask(zone, z, zonelist, ac->high_zoneidx,
  1846. ac->nodemask) {
  1847. /*
  1848. * Preserve at least one pageblock unless memory pressure
  1849. * is really high.
  1850. */
  1851. if (!force && zone->nr_reserved_highatomic <=
  1852. pageblock_nr_pages)
  1853. continue;
  1854. spin_lock_irqsave(&zone->lock, flags);
  1855. for (order = 0; order < MAX_ORDER; order++) {
  1856. struct free_area *area = &(zone->free_area[order]);
  1857. page = list_first_entry_or_null(
  1858. &area->free_list[MIGRATE_HIGHATOMIC],
  1859. struct page, lru);
  1860. if (!page)
  1861. continue;
  1862. /*
  1863. * In page freeing path, migratetype change is racy so
  1864. * we can counter several free pages in a pageblock
  1865. * in this loop althoug we changed the pageblock type
  1866. * from highatomic to ac->migratetype. So we should
  1867. * adjust the count once.
  1868. */
  1869. if (is_migrate_highatomic_page(page)) {
  1870. /*
  1871. * It should never happen but changes to
  1872. * locking could inadvertently allow a per-cpu
  1873. * drain to add pages to MIGRATE_HIGHATOMIC
  1874. * while unreserving so be safe and watch for
  1875. * underflows.
  1876. */
  1877. zone->nr_reserved_highatomic -= min(
  1878. pageblock_nr_pages,
  1879. zone->nr_reserved_highatomic);
  1880. }
  1881. /*
  1882. * Convert to ac->migratetype and avoid the normal
  1883. * pageblock stealing heuristics. Minimally, the caller
  1884. * is doing the work and needs the pages. More
  1885. * importantly, if the block was always converted to
  1886. * MIGRATE_UNMOVABLE or another type then the number
  1887. * of pageblocks that cannot be completely freed
  1888. * may increase.
  1889. */
  1890. set_pageblock_migratetype(page, ac->migratetype);
  1891. ret = move_freepages_block(zone, page, ac->migratetype,
  1892. NULL);
  1893. if (ret) {
  1894. spin_unlock_irqrestore(&zone->lock, flags);
  1895. return ret;
  1896. }
  1897. }
  1898. spin_unlock_irqrestore(&zone->lock, flags);
  1899. }
  1900. return false;
  1901. }
  1902. /*
  1903. * Try finding a free buddy page on the fallback list and put it on the free
  1904. * list of requested migratetype, possibly along with other pages from the same
  1905. * block, depending on fragmentation avoidance heuristics. Returns true if
  1906. * fallback was found so that __rmqueue_smallest() can grab it.
  1907. *
  1908. * The use of signed ints for order and current_order is a deliberate
  1909. * deviation from the rest of this file, to make the for loop
  1910. * condition simpler.
  1911. */
  1912. static inline bool
  1913. __rmqueue_fallback(struct zone *zone, int order, int start_migratetype)
  1914. {
  1915. struct free_area *area;
  1916. int current_order;
  1917. struct page *page;
  1918. int fallback_mt;
  1919. bool can_steal;
  1920. /*
  1921. * Find the largest available free page in the other list. This roughly
  1922. * approximates finding the pageblock with the most free pages, which
  1923. * would be too costly to do exactly.
  1924. */
  1925. for (current_order = MAX_ORDER - 1; current_order >= order;
  1926. --current_order) {
  1927. area = &(zone->free_area[current_order]);
  1928. fallback_mt = find_suitable_fallback(area, current_order,
  1929. start_migratetype, false, &can_steal);
  1930. if (fallback_mt == -1)
  1931. continue;
  1932. /*
  1933. * We cannot steal all free pages from the pageblock and the
  1934. * requested migratetype is movable. In that case it's better to
  1935. * steal and split the smallest available page instead of the
  1936. * largest available page, because even if the next movable
  1937. * allocation falls back into a different pageblock than this
  1938. * one, it won't cause permanent fragmentation.
  1939. */
  1940. if (!can_steal && start_migratetype == MIGRATE_MOVABLE
  1941. && current_order > order)
  1942. goto find_smallest;
  1943. goto do_steal;
  1944. }
  1945. return false;
  1946. find_smallest:
  1947. for (current_order = order; current_order < MAX_ORDER;
  1948. current_order++) {
  1949. area = &(zone->free_area[current_order]);
  1950. fallback_mt = find_suitable_fallback(area, current_order,
  1951. start_migratetype, false, &can_steal);
  1952. if (fallback_mt != -1)
  1953. break;
  1954. }
  1955. /*
  1956. * This should not happen - we already found a suitable fallback
  1957. * when looking for the largest page.
  1958. */
  1959. VM_BUG_ON(current_order == MAX_ORDER);
  1960. do_steal:
  1961. page = list_first_entry(&area->free_list[fallback_mt],
  1962. struct page, lru);
  1963. steal_suitable_fallback(zone, page, start_migratetype, can_steal);
  1964. trace_mm_page_alloc_extfrag(page, order, current_order,
  1965. start_migratetype, fallback_mt);
  1966. return true;
  1967. }
  1968. /*
  1969. * Do the hard work of removing an element from the buddy allocator.
  1970. * Call me with the zone->lock already held.
  1971. */
  1972. static struct page *__rmqueue(struct zone *zone, unsigned int order,
  1973. int migratetype)
  1974. {
  1975. struct page *page;
  1976. retry:
  1977. page = __rmqueue_smallest(zone, order, migratetype);
  1978. if (unlikely(!page)) {
  1979. if (migratetype == MIGRATE_MOVABLE)
  1980. page = __rmqueue_cma_fallback(zone, order);
  1981. if (!page && __rmqueue_fallback(zone, order, migratetype))
  1982. goto retry;
  1983. }
  1984. trace_mm_page_alloc_zone_locked(page, order, migratetype);
  1985. return page;
  1986. }
  1987. /*
  1988. * Obtain a specified number of elements from the buddy allocator, all under
  1989. * a single hold of the lock, for efficiency. Add them to the supplied list.
  1990. * Returns the number of new pages which were placed at *list.
  1991. */
  1992. static int rmqueue_bulk(struct zone *zone, unsigned int order,
  1993. unsigned long count, struct list_head *list,
  1994. int migratetype, bool cold)
  1995. {
  1996. int i, alloced = 0;
  1997. spin_lock(&zone->lock);
  1998. for (i = 0; i < count; ++i) {
  1999. struct page *page = __rmqueue(zone, order, migratetype);
  2000. if (unlikely(page == NULL))
  2001. break;
  2002. if (unlikely(check_pcp_refill(page)))
  2003. continue;
  2004. /*
  2005. * Split buddy pages returned by expand() are received here
  2006. * in physical page order. The page is added to the callers and
  2007. * list and the list head then moves forward. From the callers
  2008. * perspective, the linked list is ordered by page number in
  2009. * some conditions. This is useful for IO devices that can
  2010. * merge IO requests if the physical pages are ordered
  2011. * properly.
  2012. */
  2013. if (likely(!cold))
  2014. list_add(&page->lru, list);
  2015. else
  2016. list_add_tail(&page->lru, list);
  2017. list = &page->lru;
  2018. alloced++;
  2019. if (is_migrate_cma(get_pcppage_migratetype(page)))
  2020. __mod_zone_page_state(zone, NR_FREE_CMA_PAGES,
  2021. -(1 << order));
  2022. }
  2023. /*
  2024. * i pages were removed from the buddy list even if some leak due
  2025. * to check_pcp_refill failing so adjust NR_FREE_PAGES based
  2026. * on i. Do not confuse with 'alloced' which is the number of
  2027. * pages added to the pcp list.
  2028. */
  2029. __mod_zone_page_state(zone, NR_FREE_PAGES, -(i << order));
  2030. spin_unlock(&zone->lock);
  2031. return alloced;
  2032. }
  2033. #ifdef CONFIG_NUMA
  2034. /*
  2035. * Called from the vmstat counter updater to drain pagesets of this
  2036. * currently executing processor on remote nodes after they have
  2037. * expired.
  2038. *
  2039. * Note that this function must be called with the thread pinned to
  2040. * a single processor.
  2041. */
  2042. void drain_zone_pages(struct zone *zone, struct per_cpu_pages *pcp)
  2043. {
  2044. unsigned long flags;
  2045. int to_drain, batch;
  2046. local_irq_save(flags);
  2047. batch = READ_ONCE(pcp->batch);
  2048. to_drain = min(pcp->count, batch);
  2049. if (to_drain > 0) {
  2050. free_pcppages_bulk(zone, to_drain, pcp);
  2051. pcp->count -= to_drain;
  2052. }
  2053. local_irq_restore(flags);
  2054. }
  2055. #endif
  2056. /*
  2057. * Drain pcplists of the indicated processor and zone.
  2058. *
  2059. * The processor must either be the current processor and the
  2060. * thread pinned to the current processor or a processor that
  2061. * is not online.
  2062. */
  2063. static void drain_pages_zone(unsigned int cpu, struct zone *zone)
  2064. {
  2065. unsigned long flags;
  2066. struct per_cpu_pageset *pset;
  2067. struct per_cpu_pages *pcp;
  2068. local_irq_save(flags);
  2069. pset = per_cpu_ptr(zone->pageset, cpu);
  2070. pcp = &pset->pcp;
  2071. if (pcp->count) {
  2072. free_pcppages_bulk(zone, pcp->count, pcp);
  2073. pcp->count = 0;
  2074. }
  2075. local_irq_restore(flags);
  2076. }
  2077. /*
  2078. * Drain pcplists of all zones on the indicated processor.
  2079. *
  2080. * The processor must either be the current processor and the
  2081. * thread pinned to the current processor or a processor that
  2082. * is not online.
  2083. */
  2084. static void drain_pages(unsigned int cpu)
  2085. {
  2086. struct zone *zone;
  2087. for_each_populated_zone(zone) {
  2088. drain_pages_zone(cpu, zone);
  2089. }
  2090. }
  2091. /*
  2092. * Spill all of this CPU's per-cpu pages back into the buddy allocator.
  2093. *
  2094. * The CPU has to be pinned. When zone parameter is non-NULL, spill just
  2095. * the single zone's pages.
  2096. */
  2097. void drain_local_pages(struct zone *zone)
  2098. {
  2099. int cpu = smp_processor_id();
  2100. if (zone)
  2101. drain_pages_zone(cpu, zone);
  2102. else
  2103. drain_pages(cpu);
  2104. }
  2105. static void drain_local_pages_wq(struct work_struct *work)
  2106. {
  2107. /*
  2108. * drain_all_pages doesn't use proper cpu hotplug protection so
  2109. * we can race with cpu offline when the WQ can move this from
  2110. * a cpu pinned worker to an unbound one. We can operate on a different
  2111. * cpu which is allright but we also have to make sure to not move to
  2112. * a different one.
  2113. */
  2114. preempt_disable();
  2115. drain_local_pages(NULL);
  2116. preempt_enable();
  2117. }
  2118. /*
  2119. * Spill all the per-cpu pages from all CPUs back into the buddy allocator.
  2120. *
  2121. * When zone parameter is non-NULL, spill just the single zone's pages.
  2122. *
  2123. * Note that this can be extremely slow as the draining happens in a workqueue.
  2124. */
  2125. void drain_all_pages(struct zone *zone)
  2126. {
  2127. int cpu;
  2128. /*
  2129. * Allocate in the BSS so we wont require allocation in
  2130. * direct reclaim path for CONFIG_CPUMASK_OFFSTACK=y
  2131. */
  2132. static cpumask_t cpus_with_pcps;
  2133. /*
  2134. * Make sure nobody triggers this path before mm_percpu_wq is fully
  2135. * initialized.
  2136. */
  2137. if (WARN_ON_ONCE(!mm_percpu_wq))
  2138. return;
  2139. /* Workqueues cannot recurse */
  2140. if (current->flags & PF_WQ_WORKER)
  2141. return;
  2142. /*
  2143. * Do not drain if one is already in progress unless it's specific to
  2144. * a zone. Such callers are primarily CMA and memory hotplug and need
  2145. * the drain to be complete when the call returns.
  2146. */
  2147. if (unlikely(!mutex_trylock(&pcpu_drain_mutex))) {
  2148. if (!zone)
  2149. return;
  2150. mutex_lock(&pcpu_drain_mutex);
  2151. }
  2152. /*
  2153. * We don't care about racing with CPU hotplug event
  2154. * as offline notification will cause the notified
  2155. * cpu to drain that CPU pcps and on_each_cpu_mask
  2156. * disables preemption as part of its processing
  2157. */
  2158. for_each_online_cpu(cpu) {
  2159. struct per_cpu_pageset *pcp;
  2160. struct zone *z;
  2161. bool has_pcps = false;
  2162. if (zone) {
  2163. pcp = per_cpu_ptr(zone->pageset, cpu);
  2164. if (pcp->pcp.count)
  2165. has_pcps = true;
  2166. } else {
  2167. for_each_populated_zone(z) {
  2168. pcp = per_cpu_ptr(z->pageset, cpu);
  2169. if (pcp->pcp.count) {
  2170. has_pcps = true;
  2171. break;
  2172. }
  2173. }
  2174. }
  2175. if (has_pcps)
  2176. cpumask_set_cpu(cpu, &cpus_with_pcps);
  2177. else
  2178. cpumask_clear_cpu(cpu, &cpus_with_pcps);
  2179. }
  2180. for_each_cpu(cpu, &cpus_with_pcps) {
  2181. struct work_struct *work = per_cpu_ptr(&pcpu_drain, cpu);
  2182. INIT_WORK(work, drain_local_pages_wq);
  2183. queue_work_on(cpu, mm_percpu_wq, work);
  2184. }
  2185. for_each_cpu(cpu, &cpus_with_pcps)
  2186. flush_work(per_cpu_ptr(&pcpu_drain, cpu));
  2187. mutex_unlock(&pcpu_drain_mutex);
  2188. }
  2189. #ifdef CONFIG_HIBERNATION
  2190. /*
  2191. * Touch the watchdog for every WD_PAGE_COUNT pages.
  2192. */
  2193. #define WD_PAGE_COUNT (128*1024)
  2194. void mark_free_pages(struct zone *zone)
  2195. {
  2196. unsigned long pfn, max_zone_pfn, page_count = WD_PAGE_COUNT;
  2197. unsigned long flags;
  2198. unsigned int order, t;
  2199. struct page *page;
  2200. if (zone_is_empty(zone))
  2201. return;
  2202. spin_lock_irqsave(&zone->lock, flags);
  2203. max_zone_pfn = zone_end_pfn(zone);
  2204. for (pfn = zone->zone_start_pfn; pfn < max_zone_pfn; pfn++)
  2205. if (pfn_valid(pfn)) {
  2206. page = pfn_to_page(pfn);
  2207. if (!--page_count) {
  2208. touch_nmi_watchdog();
  2209. page_count = WD_PAGE_COUNT;
  2210. }
  2211. if (page_zone(page) != zone)
  2212. continue;
  2213. if (!swsusp_page_is_forbidden(page))
  2214. swsusp_unset_page_free(page);
  2215. }
  2216. for_each_migratetype_order(order, t) {
  2217. list_for_each_entry(page,
  2218. &zone->free_area[order].free_list[t], lru) {
  2219. unsigned long i;
  2220. pfn = page_to_pfn(page);
  2221. for (i = 0; i < (1UL << order); i++) {
  2222. if (!--page_count) {
  2223. touch_nmi_watchdog();
  2224. page_count = WD_PAGE_COUNT;
  2225. }
  2226. swsusp_set_page_free(pfn_to_page(pfn + i));
  2227. }
  2228. }
  2229. }
  2230. spin_unlock_irqrestore(&zone->lock, flags);
  2231. }
  2232. #endif /* CONFIG_PM */
  2233. /*
  2234. * Free a 0-order page
  2235. * cold == true ? free a cold page : free a hot page
  2236. */
  2237. void free_hot_cold_page(struct page *page, bool cold)
  2238. {
  2239. struct zone *zone = page_zone(page);
  2240. struct per_cpu_pages *pcp;
  2241. unsigned long flags;
  2242. unsigned long pfn = page_to_pfn(page);
  2243. int migratetype;
  2244. if (!free_pcp_prepare(page))
  2245. return;
  2246. migratetype = get_pfnblock_migratetype(page, pfn);
  2247. set_pcppage_migratetype(page, migratetype);
  2248. local_irq_save(flags);
  2249. __count_vm_event(PGFREE);
  2250. /*
  2251. * We only track unmovable, reclaimable and movable on pcp lists.
  2252. * Free ISOLATE pages back to the allocator because they are being
  2253. * offlined but treat HIGHATOMIC as movable pages so we can get those
  2254. * areas back if necessary. Otherwise, we may have to free
  2255. * excessively into the page allocator
  2256. */
  2257. if (migratetype >= MIGRATE_PCPTYPES) {
  2258. if (unlikely(is_migrate_isolate(migratetype))) {
  2259. free_one_page(zone, page, pfn, 0, migratetype);
  2260. goto out;
  2261. }
  2262. migratetype = MIGRATE_MOVABLE;
  2263. }
  2264. pcp = &this_cpu_ptr(zone->pageset)->pcp;
  2265. if (!cold)
  2266. list_add(&page->lru, &pcp->lists[migratetype]);
  2267. else
  2268. list_add_tail(&page->lru, &pcp->lists[migratetype]);
  2269. pcp->count++;
  2270. if (pcp->count >= pcp->high) {
  2271. unsigned long batch = READ_ONCE(pcp->batch);
  2272. free_pcppages_bulk(zone, batch, pcp);
  2273. pcp->count -= batch;
  2274. }
  2275. out:
  2276. local_irq_restore(flags);
  2277. }
  2278. /*
  2279. * Free a list of 0-order pages
  2280. */
  2281. void free_hot_cold_page_list(struct list_head *list, bool cold)
  2282. {
  2283. struct page *page, *next;
  2284. list_for_each_entry_safe(page, next, list, lru) {
  2285. trace_mm_page_free_batched(page, cold);
  2286. free_hot_cold_page(page, cold);
  2287. }
  2288. }
  2289. /*
  2290. * split_page takes a non-compound higher-order page, and splits it into
  2291. * n (1<<order) sub-pages: page[0..n]
  2292. * Each sub-page must be freed individually.
  2293. *
  2294. * Note: this is probably too low level an operation for use in drivers.
  2295. * Please consult with lkml before using this in your driver.
  2296. */
  2297. void split_page(struct page *page, unsigned int order)
  2298. {
  2299. int i;
  2300. VM_BUG_ON_PAGE(PageCompound(page), page);
  2301. VM_BUG_ON_PAGE(!page_count(page), page);
  2302. #ifdef CONFIG_KMEMCHECK
  2303. /*
  2304. * Split shadow pages too, because free(page[0]) would
  2305. * otherwise free the whole shadow.
  2306. */
  2307. if (kmemcheck_page_is_tracked(page))
  2308. split_page(virt_to_page(page[0].shadow), order);
  2309. #endif
  2310. for (i = 1; i < (1 << order); i++)
  2311. set_page_refcounted(page + i);
  2312. split_page_owner(page, order);
  2313. }
  2314. EXPORT_SYMBOL_GPL(split_page);
  2315. int __isolate_free_page(struct page *page, unsigned int order)
  2316. {
  2317. unsigned long watermark;
  2318. struct zone *zone;
  2319. int mt;
  2320. BUG_ON(!PageBuddy(page));
  2321. zone = page_zone(page);
  2322. mt = get_pageblock_migratetype(page);
  2323. if (!is_migrate_isolate(mt)) {
  2324. /*
  2325. * Obey watermarks as if the page was being allocated. We can
  2326. * emulate a high-order watermark check with a raised order-0
  2327. * watermark, because we already know our high-order page
  2328. * exists.
  2329. */
  2330. watermark = min_wmark_pages(zone) + (1UL << order);
  2331. if (!zone_watermark_ok(zone, 0, watermark, 0, ALLOC_CMA))
  2332. return 0;
  2333. __mod_zone_freepage_state(zone, -(1UL << order), mt);
  2334. }
  2335. /* Remove page from free list */
  2336. list_del(&page->lru);
  2337. zone->free_area[order].nr_free--;
  2338. rmv_page_order(page);
  2339. /*
  2340. * Set the pageblock if the isolated page is at least half of a
  2341. * pageblock
  2342. */
  2343. if (order >= pageblock_order - 1) {
  2344. struct page *endpage = page + (1 << order) - 1;
  2345. for (; page < endpage; page += pageblock_nr_pages) {
  2346. int mt = get_pageblock_migratetype(page);
  2347. if (!is_migrate_isolate(mt) && !is_migrate_cma(mt)
  2348. && !is_migrate_highatomic(mt))
  2349. set_pageblock_migratetype(page,
  2350. MIGRATE_MOVABLE);
  2351. }
  2352. }
  2353. return 1UL << order;
  2354. }
  2355. /*
  2356. * Update NUMA hit/miss statistics
  2357. *
  2358. * Must be called with interrupts disabled.
  2359. */
  2360. static inline void zone_statistics(struct zone *preferred_zone, struct zone *z)
  2361. {
  2362. #ifdef CONFIG_NUMA
  2363. enum zone_stat_item local_stat = NUMA_LOCAL;
  2364. if (z->node != numa_node_id())
  2365. local_stat = NUMA_OTHER;
  2366. if (z->node == preferred_zone->node)
  2367. __inc_zone_state(z, NUMA_HIT);
  2368. else {
  2369. __inc_zone_state(z, NUMA_MISS);
  2370. __inc_zone_state(preferred_zone, NUMA_FOREIGN);
  2371. }
  2372. __inc_zone_state(z, local_stat);
  2373. #endif
  2374. }
  2375. /* Remove page from the per-cpu list, caller must protect the list */
  2376. static struct page *__rmqueue_pcplist(struct zone *zone, int migratetype,
  2377. bool cold, struct per_cpu_pages *pcp,
  2378. struct list_head *list)
  2379. {
  2380. struct page *page;
  2381. do {
  2382. if (list_empty(list)) {
  2383. pcp->count += rmqueue_bulk(zone, 0,
  2384. pcp->batch, list,
  2385. migratetype, cold);
  2386. if (unlikely(list_empty(list)))
  2387. return NULL;
  2388. }
  2389. if (cold)
  2390. page = list_last_entry(list, struct page, lru);
  2391. else
  2392. page = list_first_entry(list, struct page, lru);
  2393. list_del(&page->lru);
  2394. pcp->count--;
  2395. } while (check_new_pcp(page));
  2396. return page;
  2397. }
  2398. /* Lock and remove page from the per-cpu list */
  2399. static struct page *rmqueue_pcplist(struct zone *preferred_zone,
  2400. struct zone *zone, unsigned int order,
  2401. gfp_t gfp_flags, int migratetype)
  2402. {
  2403. struct per_cpu_pages *pcp;
  2404. struct list_head *list;
  2405. bool cold = ((gfp_flags & __GFP_COLD) != 0);
  2406. struct page *page;
  2407. unsigned long flags;
  2408. local_irq_save(flags);
  2409. pcp = &this_cpu_ptr(zone->pageset)->pcp;
  2410. list = &pcp->lists[migratetype];
  2411. page = __rmqueue_pcplist(zone, migratetype, cold, pcp, list);
  2412. if (page) {
  2413. __count_zid_vm_events(PGALLOC, page_zonenum(page), 1 << order);
  2414. zone_statistics(preferred_zone, zone);
  2415. }
  2416. local_irq_restore(flags);
  2417. return page;
  2418. }
  2419. /*
  2420. * Allocate a page from the given zone. Use pcplists for order-0 allocations.
  2421. */
  2422. static inline
  2423. struct page *rmqueue(struct zone *preferred_zone,
  2424. struct zone *zone, unsigned int order,
  2425. gfp_t gfp_flags, unsigned int alloc_flags,
  2426. int migratetype)
  2427. {
  2428. unsigned long flags;
  2429. struct page *page;
  2430. if (likely(order == 0)) {
  2431. page = rmqueue_pcplist(preferred_zone, zone, order,
  2432. gfp_flags, migratetype);
  2433. goto out;
  2434. }
  2435. /*
  2436. * We most definitely don't want callers attempting to
  2437. * allocate greater than order-1 page units with __GFP_NOFAIL.
  2438. */
  2439. WARN_ON_ONCE((gfp_flags & __GFP_NOFAIL) && (order > 1));
  2440. spin_lock_irqsave(&zone->lock, flags);
  2441. do {
  2442. page = NULL;
  2443. if (alloc_flags & ALLOC_HARDER) {
  2444. page = __rmqueue_smallest(zone, order, MIGRATE_HIGHATOMIC);
  2445. if (page)
  2446. trace_mm_page_alloc_zone_locked(page, order, migratetype);
  2447. }
  2448. if (!page)
  2449. page = __rmqueue(zone, order, migratetype);
  2450. } while (page && check_new_pages(page, order));
  2451. spin_unlock(&zone->lock);
  2452. if (!page)
  2453. goto failed;
  2454. __mod_zone_freepage_state(zone, -(1 << order),
  2455. get_pcppage_migratetype(page));
  2456. __count_zid_vm_events(PGALLOC, page_zonenum(page), 1 << order);
  2457. zone_statistics(preferred_zone, zone);
  2458. local_irq_restore(flags);
  2459. out:
  2460. VM_BUG_ON_PAGE(page && bad_range(zone, page), page);
  2461. return page;
  2462. failed:
  2463. local_irq_restore(flags);
  2464. return NULL;
  2465. }
  2466. #ifdef CONFIG_FAIL_PAGE_ALLOC
  2467. static struct {
  2468. struct fault_attr attr;
  2469. bool ignore_gfp_highmem;
  2470. bool ignore_gfp_reclaim;
  2471. u32 min_order;
  2472. } fail_page_alloc = {
  2473. .attr = FAULT_ATTR_INITIALIZER,
  2474. .ignore_gfp_reclaim = true,
  2475. .ignore_gfp_highmem = true,
  2476. .min_order = 1,
  2477. };
  2478. static int __init setup_fail_page_alloc(char *str)
  2479. {
  2480. return setup_fault_attr(&fail_page_alloc.attr, str);
  2481. }
  2482. __setup("fail_page_alloc=", setup_fail_page_alloc);
  2483. static bool should_fail_alloc_page(gfp_t gfp_mask, unsigned int order)
  2484. {
  2485. if (order < fail_page_alloc.min_order)
  2486. return false;
  2487. if (gfp_mask & __GFP_NOFAIL)
  2488. return false;
  2489. if (fail_page_alloc.ignore_gfp_highmem && (gfp_mask & __GFP_HIGHMEM))
  2490. return false;
  2491. if (fail_page_alloc.ignore_gfp_reclaim &&
  2492. (gfp_mask & __GFP_DIRECT_RECLAIM))
  2493. return false;
  2494. return should_fail(&fail_page_alloc.attr, 1 << order);
  2495. }
  2496. #ifdef CONFIG_FAULT_INJECTION_DEBUG_FS
  2497. static int __init fail_page_alloc_debugfs(void)
  2498. {
  2499. umode_t mode = S_IFREG | S_IRUSR | S_IWUSR;
  2500. struct dentry *dir;
  2501. dir = fault_create_debugfs_attr("fail_page_alloc", NULL,
  2502. &fail_page_alloc.attr);
  2503. if (IS_ERR(dir))
  2504. return PTR_ERR(dir);
  2505. if (!debugfs_create_bool("ignore-gfp-wait", mode, dir,
  2506. &fail_page_alloc.ignore_gfp_reclaim))
  2507. goto fail;
  2508. if (!debugfs_create_bool("ignore-gfp-highmem", mode, dir,
  2509. &fail_page_alloc.ignore_gfp_highmem))
  2510. goto fail;
  2511. if (!debugfs_create_u32("min-order", mode, dir,
  2512. &fail_page_alloc.min_order))
  2513. goto fail;
  2514. return 0;
  2515. fail:
  2516. debugfs_remove_recursive(dir);
  2517. return -ENOMEM;
  2518. }
  2519. late_initcall(fail_page_alloc_debugfs);
  2520. #endif /* CONFIG_FAULT_INJECTION_DEBUG_FS */
  2521. #else /* CONFIG_FAIL_PAGE_ALLOC */
  2522. static inline bool should_fail_alloc_page(gfp_t gfp_mask, unsigned int order)
  2523. {
  2524. return false;
  2525. }
  2526. #endif /* CONFIG_FAIL_PAGE_ALLOC */
  2527. /*
  2528. * Return true if free base pages are above 'mark'. For high-order checks it
  2529. * will return true of the order-0 watermark is reached and there is at least
  2530. * one free page of a suitable size. Checking now avoids taking the zone lock
  2531. * to check in the allocation paths if no pages are free.
  2532. */
  2533. bool __zone_watermark_ok(struct zone *z, unsigned int order, unsigned long mark,
  2534. int classzone_idx, unsigned int alloc_flags,
  2535. long free_pages)
  2536. {
  2537. long min = mark;
  2538. int o;
  2539. const bool alloc_harder = (alloc_flags & ALLOC_HARDER);
  2540. /* free_pages may go negative - that's OK */
  2541. free_pages -= (1 << order) - 1;
  2542. if (alloc_flags & ALLOC_HIGH)
  2543. min -= min / 2;
  2544. /*
  2545. * If the caller does not have rights to ALLOC_HARDER then subtract
  2546. * the high-atomic reserves. This will over-estimate the size of the
  2547. * atomic reserve but it avoids a search.
  2548. */
  2549. if (likely(!alloc_harder))
  2550. free_pages -= z->nr_reserved_highatomic;
  2551. else
  2552. min -= min / 4;
  2553. #ifdef CONFIG_CMA
  2554. /* If allocation can't use CMA areas don't use free CMA pages */
  2555. if (!(alloc_flags & ALLOC_CMA))
  2556. free_pages -= zone_page_state(z, NR_FREE_CMA_PAGES);
  2557. #endif
  2558. /*
  2559. * Check watermarks for an order-0 allocation request. If these
  2560. * are not met, then a high-order request also cannot go ahead
  2561. * even if a suitable page happened to be free.
  2562. */
  2563. if (free_pages <= min + z->lowmem_reserve[classzone_idx])
  2564. return false;
  2565. /* If this is an order-0 request then the watermark is fine */
  2566. if (!order)
  2567. return true;
  2568. /* For a high-order request, check at least one suitable page is free */
  2569. for (o = order; o < MAX_ORDER; o++) {
  2570. struct free_area *area = &z->free_area[o];
  2571. int mt;
  2572. if (!area->nr_free)
  2573. continue;
  2574. if (alloc_harder)
  2575. return true;
  2576. for (mt = 0; mt < MIGRATE_PCPTYPES; mt++) {
  2577. if (!list_empty(&area->free_list[mt]))
  2578. return true;
  2579. }
  2580. #ifdef CONFIG_CMA
  2581. if ((alloc_flags & ALLOC_CMA) &&
  2582. !list_empty(&area->free_list[MIGRATE_CMA])) {
  2583. return true;
  2584. }
  2585. #endif
  2586. }
  2587. return false;
  2588. }
  2589. bool zone_watermark_ok(struct zone *z, unsigned int order, unsigned long mark,
  2590. int classzone_idx, unsigned int alloc_flags)
  2591. {
  2592. return __zone_watermark_ok(z, order, mark, classzone_idx, alloc_flags,
  2593. zone_page_state(z, NR_FREE_PAGES));
  2594. }
  2595. static inline bool zone_watermark_fast(struct zone *z, unsigned int order,
  2596. unsigned long mark, int classzone_idx, unsigned int alloc_flags)
  2597. {
  2598. long free_pages = zone_page_state(z, NR_FREE_PAGES);
  2599. long cma_pages = 0;
  2600. #ifdef CONFIG_CMA
  2601. /* If allocation can't use CMA areas don't use free CMA pages */
  2602. if (!(alloc_flags & ALLOC_CMA))
  2603. cma_pages = zone_page_state(z, NR_FREE_CMA_PAGES);
  2604. #endif
  2605. /*
  2606. * Fast check for order-0 only. If this fails then the reserves
  2607. * need to be calculated. There is a corner case where the check
  2608. * passes but only the high-order atomic reserve are free. If
  2609. * the caller is !atomic then it'll uselessly search the free
  2610. * list. That corner case is then slower but it is harmless.
  2611. */
  2612. if (!order && (free_pages - cma_pages) > mark + z->lowmem_reserve[classzone_idx])
  2613. return true;
  2614. return __zone_watermark_ok(z, order, mark, classzone_idx, alloc_flags,
  2615. free_pages);
  2616. }
  2617. bool zone_watermark_ok_safe(struct zone *z, unsigned int order,
  2618. unsigned long mark, int classzone_idx)
  2619. {
  2620. long free_pages = zone_page_state(z, NR_FREE_PAGES);
  2621. if (z->percpu_drift_mark && free_pages < z->percpu_drift_mark)
  2622. free_pages = zone_page_state_snapshot(z, NR_FREE_PAGES);
  2623. return __zone_watermark_ok(z, order, mark, classzone_idx, 0,
  2624. free_pages);
  2625. }
  2626. #ifdef CONFIG_NUMA
  2627. static bool zone_allows_reclaim(struct zone *local_zone, struct zone *zone)
  2628. {
  2629. return node_distance(zone_to_nid(local_zone), zone_to_nid(zone)) <=
  2630. RECLAIM_DISTANCE;
  2631. }
  2632. #else /* CONFIG_NUMA */
  2633. static bool zone_allows_reclaim(struct zone *local_zone, struct zone *zone)
  2634. {
  2635. return true;
  2636. }
  2637. #endif /* CONFIG_NUMA */
  2638. /*
  2639. * get_page_from_freelist goes through the zonelist trying to allocate
  2640. * a page.
  2641. */
  2642. static struct page *
  2643. get_page_from_freelist(gfp_t gfp_mask, unsigned int order, int alloc_flags,
  2644. const struct alloc_context *ac)
  2645. {
  2646. struct zoneref *z = ac->preferred_zoneref;
  2647. struct zone *zone;
  2648. struct pglist_data *last_pgdat_dirty_limit = NULL;
  2649. /*
  2650. * Scan zonelist, looking for a zone with enough free.
  2651. * See also __cpuset_node_allowed() comment in kernel/cpuset.c.
  2652. */
  2653. for_next_zone_zonelist_nodemask(zone, z, ac->zonelist, ac->high_zoneidx,
  2654. ac->nodemask) {
  2655. struct page *page;
  2656. unsigned long mark;
  2657. if (cpusets_enabled() &&
  2658. (alloc_flags & ALLOC_CPUSET) &&
  2659. !__cpuset_zone_allowed(zone, gfp_mask))
  2660. continue;
  2661. /*
  2662. * When allocating a page cache page for writing, we
  2663. * want to get it from a node that is within its dirty
  2664. * limit, such that no single node holds more than its
  2665. * proportional share of globally allowed dirty pages.
  2666. * The dirty limits take into account the node's
  2667. * lowmem reserves and high watermark so that kswapd
  2668. * should be able to balance it without having to
  2669. * write pages from its LRU list.
  2670. *
  2671. * XXX: For now, allow allocations to potentially
  2672. * exceed the per-node dirty limit in the slowpath
  2673. * (spread_dirty_pages unset) before going into reclaim,
  2674. * which is important when on a NUMA setup the allowed
  2675. * nodes are together not big enough to reach the
  2676. * global limit. The proper fix for these situations
  2677. * will require awareness of nodes in the
  2678. * dirty-throttling and the flusher threads.
  2679. */
  2680. if (ac->spread_dirty_pages) {
  2681. if (last_pgdat_dirty_limit == zone->zone_pgdat)
  2682. continue;
  2683. if (!node_dirty_ok(zone->zone_pgdat)) {
  2684. last_pgdat_dirty_limit = zone->zone_pgdat;
  2685. continue;
  2686. }
  2687. }
  2688. mark = zone->watermark[alloc_flags & ALLOC_WMARK_MASK];
  2689. if (!zone_watermark_fast(zone, order, mark,
  2690. ac_classzone_idx(ac), alloc_flags)) {
  2691. int ret;
  2692. /* Checked here to keep the fast path fast */
  2693. BUILD_BUG_ON(ALLOC_NO_WATERMARKS < NR_WMARK);
  2694. if (alloc_flags & ALLOC_NO_WATERMARKS)
  2695. goto try_this_zone;
  2696. if (node_reclaim_mode == 0 ||
  2697. !zone_allows_reclaim(ac->preferred_zoneref->zone, zone))
  2698. continue;
  2699. ret = node_reclaim(zone->zone_pgdat, gfp_mask, order);
  2700. switch (ret) {
  2701. case NODE_RECLAIM_NOSCAN:
  2702. /* did not scan */
  2703. continue;
  2704. case NODE_RECLAIM_FULL:
  2705. /* scanned but unreclaimable */
  2706. continue;
  2707. default:
  2708. /* did we reclaim enough */
  2709. if (zone_watermark_ok(zone, order, mark,
  2710. ac_classzone_idx(ac), alloc_flags))
  2711. goto try_this_zone;
  2712. continue;
  2713. }
  2714. }
  2715. try_this_zone:
  2716. page = rmqueue(ac->preferred_zoneref->zone, zone, order,
  2717. gfp_mask, alloc_flags, ac->migratetype);
  2718. if (page) {
  2719. prep_new_page(page, order, gfp_mask, alloc_flags);
  2720. /*
  2721. * If this is a high-order atomic allocation then check
  2722. * if the pageblock should be reserved for the future
  2723. */
  2724. if (unlikely(order && (alloc_flags & ALLOC_HARDER)))
  2725. reserve_highatomic_pageblock(page, zone, order);
  2726. return page;
  2727. }
  2728. }
  2729. return NULL;
  2730. }
  2731. /*
  2732. * Large machines with many possible nodes should not always dump per-node
  2733. * meminfo in irq context.
  2734. */
  2735. static inline bool should_suppress_show_mem(void)
  2736. {
  2737. bool ret = false;
  2738. #if NODES_SHIFT > 8
  2739. ret = in_interrupt();
  2740. #endif
  2741. return ret;
  2742. }
  2743. static void warn_alloc_show_mem(gfp_t gfp_mask, nodemask_t *nodemask)
  2744. {
  2745. unsigned int filter = SHOW_MEM_FILTER_NODES;
  2746. static DEFINE_RATELIMIT_STATE(show_mem_rs, HZ, 1);
  2747. if (should_suppress_show_mem() || !__ratelimit(&show_mem_rs))
  2748. return;
  2749. /*
  2750. * This documents exceptions given to allocations in certain
  2751. * contexts that are allowed to allocate outside current's set
  2752. * of allowed nodes.
  2753. */
  2754. if (!(gfp_mask & __GFP_NOMEMALLOC))
  2755. if (test_thread_flag(TIF_MEMDIE) ||
  2756. (current->flags & (PF_MEMALLOC | PF_EXITING)))
  2757. filter &= ~SHOW_MEM_FILTER_NODES;
  2758. if (in_interrupt() || !(gfp_mask & __GFP_DIRECT_RECLAIM))
  2759. filter &= ~SHOW_MEM_FILTER_NODES;
  2760. show_mem(filter, nodemask);
  2761. }
  2762. void warn_alloc(gfp_t gfp_mask, nodemask_t *nodemask, const char *fmt, ...)
  2763. {
  2764. struct va_format vaf;
  2765. va_list args;
  2766. static DEFINE_RATELIMIT_STATE(nopage_rs, DEFAULT_RATELIMIT_INTERVAL,
  2767. DEFAULT_RATELIMIT_BURST);
  2768. if ((gfp_mask & __GFP_NOWARN) || !__ratelimit(&nopage_rs))
  2769. return;
  2770. pr_warn("%s: ", current->comm);
  2771. va_start(args, fmt);
  2772. vaf.fmt = fmt;
  2773. vaf.va = &args;
  2774. pr_cont("%pV", &vaf);
  2775. va_end(args);
  2776. pr_cont(", mode:%#x(%pGg), nodemask=", gfp_mask, &gfp_mask);
  2777. if (nodemask)
  2778. pr_cont("%*pbl\n", nodemask_pr_args(nodemask));
  2779. else
  2780. pr_cont("(null)\n");
  2781. cpuset_print_current_mems_allowed();
  2782. dump_stack();
  2783. warn_alloc_show_mem(gfp_mask, nodemask);
  2784. }
  2785. static inline struct page *
  2786. __alloc_pages_cpuset_fallback(gfp_t gfp_mask, unsigned int order,
  2787. unsigned int alloc_flags,
  2788. const struct alloc_context *ac)
  2789. {
  2790. struct page *page;
  2791. page = get_page_from_freelist(gfp_mask, order,
  2792. alloc_flags|ALLOC_CPUSET, ac);
  2793. /*
  2794. * fallback to ignore cpuset restriction if our nodes
  2795. * are depleted
  2796. */
  2797. if (!page)
  2798. page = get_page_from_freelist(gfp_mask, order,
  2799. alloc_flags, ac);
  2800. return page;
  2801. }
  2802. static inline struct page *
  2803. __alloc_pages_may_oom(gfp_t gfp_mask, unsigned int order,
  2804. const struct alloc_context *ac, unsigned long *did_some_progress)
  2805. {
  2806. struct oom_control oc = {
  2807. .zonelist = ac->zonelist,
  2808. .nodemask = ac->nodemask,
  2809. .memcg = NULL,
  2810. .gfp_mask = gfp_mask,
  2811. .order = order,
  2812. };
  2813. struct page *page;
  2814. *did_some_progress = 0;
  2815. /*
  2816. * Acquire the oom lock. If that fails, somebody else is
  2817. * making progress for us.
  2818. */
  2819. if (!mutex_trylock(&oom_lock)) {
  2820. *did_some_progress = 1;
  2821. schedule_timeout_uninterruptible(1);
  2822. return NULL;
  2823. }
  2824. /*
  2825. * Go through the zonelist yet one more time, keep very high watermark
  2826. * here, this is only to catch a parallel oom killing, we must fail if
  2827. * we're still under heavy pressure. But make sure that this reclaim
  2828. * attempt shall not depend on __GFP_DIRECT_RECLAIM && !__GFP_NORETRY
  2829. * allocation which will never fail due to oom_lock already held.
  2830. */
  2831. page = get_page_from_freelist((gfp_mask | __GFP_HARDWALL) &
  2832. ~__GFP_DIRECT_RECLAIM, order,
  2833. ALLOC_WMARK_HIGH|ALLOC_CPUSET, ac);
  2834. if (page)
  2835. goto out;
  2836. /* Coredumps can quickly deplete all memory reserves */
  2837. if (current->flags & PF_DUMPCORE)
  2838. goto out;
  2839. /* The OOM killer will not help higher order allocs */
  2840. if (order > PAGE_ALLOC_COSTLY_ORDER)
  2841. goto out;
  2842. /*
  2843. * We have already exhausted all our reclaim opportunities without any
  2844. * success so it is time to admit defeat. We will skip the OOM killer
  2845. * because it is very likely that the caller has a more reasonable
  2846. * fallback than shooting a random task.
  2847. */
  2848. if (gfp_mask & __GFP_RETRY_MAYFAIL)
  2849. goto out;
  2850. /* The OOM killer does not needlessly kill tasks for lowmem */
  2851. if (ac->high_zoneidx < ZONE_NORMAL)
  2852. goto out;
  2853. if (pm_suspended_storage())
  2854. goto out;
  2855. /*
  2856. * XXX: GFP_NOFS allocations should rather fail than rely on
  2857. * other request to make a forward progress.
  2858. * We are in an unfortunate situation where out_of_memory cannot
  2859. * do much for this context but let's try it to at least get
  2860. * access to memory reserved if the current task is killed (see
  2861. * out_of_memory). Once filesystems are ready to handle allocation
  2862. * failures more gracefully we should just bail out here.
  2863. */
  2864. /* The OOM killer may not free memory on a specific node */
  2865. if (gfp_mask & __GFP_THISNODE)
  2866. goto out;
  2867. /* Exhausted what can be done so it's blamo time */
  2868. if (out_of_memory(&oc) || WARN_ON_ONCE(gfp_mask & __GFP_NOFAIL)) {
  2869. *did_some_progress = 1;
  2870. /*
  2871. * Help non-failing allocations by giving them access to memory
  2872. * reserves
  2873. */
  2874. if (gfp_mask & __GFP_NOFAIL)
  2875. page = __alloc_pages_cpuset_fallback(gfp_mask, order,
  2876. ALLOC_NO_WATERMARKS, ac);
  2877. }
  2878. out:
  2879. mutex_unlock(&oom_lock);
  2880. return page;
  2881. }
  2882. /*
  2883. * Maximum number of compaction retries wit a progress before OOM
  2884. * killer is consider as the only way to move forward.
  2885. */
  2886. #define MAX_COMPACT_RETRIES 16
  2887. #ifdef CONFIG_COMPACTION
  2888. /* Try memory compaction for high-order allocations before reclaim */
  2889. static struct page *
  2890. __alloc_pages_direct_compact(gfp_t gfp_mask, unsigned int order,
  2891. unsigned int alloc_flags, const struct alloc_context *ac,
  2892. enum compact_priority prio, enum compact_result *compact_result)
  2893. {
  2894. struct page *page;
  2895. unsigned int noreclaim_flag;
  2896. if (!order)
  2897. return NULL;
  2898. noreclaim_flag = memalloc_noreclaim_save();
  2899. *compact_result = try_to_compact_pages(gfp_mask, order, alloc_flags, ac,
  2900. prio);
  2901. memalloc_noreclaim_restore(noreclaim_flag);
  2902. if (*compact_result <= COMPACT_INACTIVE)
  2903. return NULL;
  2904. /*
  2905. * At least in one zone compaction wasn't deferred or skipped, so let's
  2906. * count a compaction stall
  2907. */
  2908. count_vm_event(COMPACTSTALL);
  2909. page = get_page_from_freelist(gfp_mask, order, alloc_flags, ac);
  2910. if (page) {
  2911. struct zone *zone = page_zone(page);
  2912. zone->compact_blockskip_flush = false;
  2913. compaction_defer_reset(zone, order, true);
  2914. count_vm_event(COMPACTSUCCESS);
  2915. return page;
  2916. }
  2917. /*
  2918. * It's bad if compaction run occurs and fails. The most likely reason
  2919. * is that pages exist, but not enough to satisfy watermarks.
  2920. */
  2921. count_vm_event(COMPACTFAIL);
  2922. cond_resched();
  2923. return NULL;
  2924. }
  2925. static inline bool
  2926. should_compact_retry(struct alloc_context *ac, int order, int alloc_flags,
  2927. enum compact_result compact_result,
  2928. enum compact_priority *compact_priority,
  2929. int *compaction_retries)
  2930. {
  2931. int max_retries = MAX_COMPACT_RETRIES;
  2932. int min_priority;
  2933. bool ret = false;
  2934. int retries = *compaction_retries;
  2935. enum compact_priority priority = *compact_priority;
  2936. if (!order)
  2937. return false;
  2938. if (compaction_made_progress(compact_result))
  2939. (*compaction_retries)++;
  2940. /*
  2941. * compaction considers all the zone as desperately out of memory
  2942. * so it doesn't really make much sense to retry except when the
  2943. * failure could be caused by insufficient priority
  2944. */
  2945. if (compaction_failed(compact_result))
  2946. goto check_priority;
  2947. /*
  2948. * make sure the compaction wasn't deferred or didn't bail out early
  2949. * due to locks contention before we declare that we should give up.
  2950. * But do not retry if the given zonelist is not suitable for
  2951. * compaction.
  2952. */
  2953. if (compaction_withdrawn(compact_result)) {
  2954. ret = compaction_zonelist_suitable(ac, order, alloc_flags);
  2955. goto out;
  2956. }
  2957. /*
  2958. * !costly requests are much more important than __GFP_RETRY_MAYFAIL
  2959. * costly ones because they are de facto nofail and invoke OOM
  2960. * killer to move on while costly can fail and users are ready
  2961. * to cope with that. 1/4 retries is rather arbitrary but we
  2962. * would need much more detailed feedback from compaction to
  2963. * make a better decision.
  2964. */
  2965. if (order > PAGE_ALLOC_COSTLY_ORDER)
  2966. max_retries /= 4;
  2967. if (*compaction_retries <= max_retries) {
  2968. ret = true;
  2969. goto out;
  2970. }
  2971. /*
  2972. * Make sure there are attempts at the highest priority if we exhausted
  2973. * all retries or failed at the lower priorities.
  2974. */
  2975. check_priority:
  2976. min_priority = (order > PAGE_ALLOC_COSTLY_ORDER) ?
  2977. MIN_COMPACT_COSTLY_PRIORITY : MIN_COMPACT_PRIORITY;
  2978. if (*compact_priority > min_priority) {
  2979. (*compact_priority)--;
  2980. *compaction_retries = 0;
  2981. ret = true;
  2982. }
  2983. out:
  2984. trace_compact_retry(order, priority, compact_result, retries, max_retries, ret);
  2985. return ret;
  2986. }
  2987. #else
  2988. static inline struct page *
  2989. __alloc_pages_direct_compact(gfp_t gfp_mask, unsigned int order,
  2990. unsigned int alloc_flags, const struct alloc_context *ac,
  2991. enum compact_priority prio, enum compact_result *compact_result)
  2992. {
  2993. *compact_result = COMPACT_SKIPPED;
  2994. return NULL;
  2995. }
  2996. static inline bool
  2997. should_compact_retry(struct alloc_context *ac, unsigned int order, int alloc_flags,
  2998. enum compact_result compact_result,
  2999. enum compact_priority *compact_priority,
  3000. int *compaction_retries)
  3001. {
  3002. struct zone *zone;
  3003. struct zoneref *z;
  3004. if (!order || order > PAGE_ALLOC_COSTLY_ORDER)
  3005. return false;
  3006. /*
  3007. * There are setups with compaction disabled which would prefer to loop
  3008. * inside the allocator rather than hit the oom killer prematurely.
  3009. * Let's give them a good hope and keep retrying while the order-0
  3010. * watermarks are OK.
  3011. */
  3012. for_each_zone_zonelist_nodemask(zone, z, ac->zonelist, ac->high_zoneidx,
  3013. ac->nodemask) {
  3014. if (zone_watermark_ok(zone, 0, min_wmark_pages(zone),
  3015. ac_classzone_idx(ac), alloc_flags))
  3016. return true;
  3017. }
  3018. return false;
  3019. }
  3020. #endif /* CONFIG_COMPACTION */
  3021. #ifdef CONFIG_LOCKDEP
  3022. struct lockdep_map __fs_reclaim_map =
  3023. STATIC_LOCKDEP_MAP_INIT("fs_reclaim", &__fs_reclaim_map);
  3024. static bool __need_fs_reclaim(gfp_t gfp_mask)
  3025. {
  3026. gfp_mask = current_gfp_context(gfp_mask);
  3027. /* no reclaim without waiting on it */
  3028. if (!(gfp_mask & __GFP_DIRECT_RECLAIM))
  3029. return false;
  3030. /* this guy won't enter reclaim */
  3031. if ((current->flags & PF_MEMALLOC) && !(gfp_mask & __GFP_NOMEMALLOC))
  3032. return false;
  3033. /* We're only interested __GFP_FS allocations for now */
  3034. if (!(gfp_mask & __GFP_FS))
  3035. return false;
  3036. if (gfp_mask & __GFP_NOLOCKDEP)
  3037. return false;
  3038. return true;
  3039. }
  3040. void fs_reclaim_acquire(gfp_t gfp_mask)
  3041. {
  3042. if (__need_fs_reclaim(gfp_mask))
  3043. lock_map_acquire(&__fs_reclaim_map);
  3044. }
  3045. EXPORT_SYMBOL_GPL(fs_reclaim_acquire);
  3046. void fs_reclaim_release(gfp_t gfp_mask)
  3047. {
  3048. if (__need_fs_reclaim(gfp_mask))
  3049. lock_map_release(&__fs_reclaim_map);
  3050. }
  3051. EXPORT_SYMBOL_GPL(fs_reclaim_release);
  3052. #endif
  3053. /* Perform direct synchronous page reclaim */
  3054. static int
  3055. __perform_reclaim(gfp_t gfp_mask, unsigned int order,
  3056. const struct alloc_context *ac)
  3057. {
  3058. struct reclaim_state reclaim_state;
  3059. int progress;
  3060. unsigned int noreclaim_flag;
  3061. cond_resched();
  3062. /* We now go into synchronous reclaim */
  3063. cpuset_memory_pressure_bump();
  3064. noreclaim_flag = memalloc_noreclaim_save();
  3065. fs_reclaim_acquire(gfp_mask);
  3066. reclaim_state.reclaimed_slab = 0;
  3067. current->reclaim_state = &reclaim_state;
  3068. progress = try_to_free_pages(ac->zonelist, order, gfp_mask,
  3069. ac->nodemask);
  3070. current->reclaim_state = NULL;
  3071. fs_reclaim_release(gfp_mask);
  3072. memalloc_noreclaim_restore(noreclaim_flag);
  3073. cond_resched();
  3074. return progress;
  3075. }
  3076. /* The really slow allocator path where we enter direct reclaim */
  3077. static inline struct page *
  3078. __alloc_pages_direct_reclaim(gfp_t gfp_mask, unsigned int order,
  3079. unsigned int alloc_flags, const struct alloc_context *ac,
  3080. unsigned long *did_some_progress)
  3081. {
  3082. struct page *page = NULL;
  3083. bool drained = false;
  3084. *did_some_progress = __perform_reclaim(gfp_mask, order, ac);
  3085. if (unlikely(!(*did_some_progress)))
  3086. return NULL;
  3087. retry:
  3088. page = get_page_from_freelist(gfp_mask, order, alloc_flags, ac);
  3089. /*
  3090. * If an allocation failed after direct reclaim, it could be because
  3091. * pages are pinned on the per-cpu lists or in high alloc reserves.
  3092. * Shrink them them and try again
  3093. */
  3094. if (!page && !drained) {
  3095. unreserve_highatomic_pageblock(ac, false);
  3096. drain_all_pages(NULL);
  3097. drained = true;
  3098. goto retry;
  3099. }
  3100. return page;
  3101. }
  3102. static void wake_all_kswapds(unsigned int order, const struct alloc_context *ac)
  3103. {
  3104. struct zoneref *z;
  3105. struct zone *zone;
  3106. pg_data_t *last_pgdat = NULL;
  3107. for_each_zone_zonelist_nodemask(zone, z, ac->zonelist,
  3108. ac->high_zoneidx, ac->nodemask) {
  3109. if (last_pgdat != zone->zone_pgdat)
  3110. wakeup_kswapd(zone, order, ac->high_zoneidx);
  3111. last_pgdat = zone->zone_pgdat;
  3112. }
  3113. }
  3114. static inline unsigned int
  3115. gfp_to_alloc_flags(gfp_t gfp_mask)
  3116. {
  3117. unsigned int alloc_flags = ALLOC_WMARK_MIN | ALLOC_CPUSET;
  3118. /* __GFP_HIGH is assumed to be the same as ALLOC_HIGH to save a branch. */
  3119. BUILD_BUG_ON(__GFP_HIGH != (__force gfp_t) ALLOC_HIGH);
  3120. /*
  3121. * The caller may dip into page reserves a bit more if the caller
  3122. * cannot run direct reclaim, or if the caller has realtime scheduling
  3123. * policy or is asking for __GFP_HIGH memory. GFP_ATOMIC requests will
  3124. * set both ALLOC_HARDER (__GFP_ATOMIC) and ALLOC_HIGH (__GFP_HIGH).
  3125. */
  3126. alloc_flags |= (__force int) (gfp_mask & __GFP_HIGH);
  3127. if (gfp_mask & __GFP_ATOMIC) {
  3128. /*
  3129. * Not worth trying to allocate harder for __GFP_NOMEMALLOC even
  3130. * if it can't schedule.
  3131. */
  3132. if (!(gfp_mask & __GFP_NOMEMALLOC))
  3133. alloc_flags |= ALLOC_HARDER;
  3134. /*
  3135. * Ignore cpuset mems for GFP_ATOMIC rather than fail, see the
  3136. * comment for __cpuset_node_allowed().
  3137. */
  3138. alloc_flags &= ~ALLOC_CPUSET;
  3139. } else if (unlikely(rt_task(current)) && !in_interrupt())
  3140. alloc_flags |= ALLOC_HARDER;
  3141. #ifdef CONFIG_CMA
  3142. if (gfpflags_to_migratetype(gfp_mask) == MIGRATE_MOVABLE)
  3143. alloc_flags |= ALLOC_CMA;
  3144. #endif
  3145. return alloc_flags;
  3146. }
  3147. bool gfp_pfmemalloc_allowed(gfp_t gfp_mask)
  3148. {
  3149. if (unlikely(gfp_mask & __GFP_NOMEMALLOC))
  3150. return false;
  3151. if (gfp_mask & __GFP_MEMALLOC)
  3152. return true;
  3153. if (in_serving_softirq() && (current->flags & PF_MEMALLOC))
  3154. return true;
  3155. if (!in_interrupt() &&
  3156. ((current->flags & PF_MEMALLOC) ||
  3157. unlikely(test_thread_flag(TIF_MEMDIE))))
  3158. return true;
  3159. return false;
  3160. }
  3161. /*
  3162. * Checks whether it makes sense to retry the reclaim to make a forward progress
  3163. * for the given allocation request.
  3164. *
  3165. * We give up when we either have tried MAX_RECLAIM_RETRIES in a row
  3166. * without success, or when we couldn't even meet the watermark if we
  3167. * reclaimed all remaining pages on the LRU lists.
  3168. *
  3169. * Returns true if a retry is viable or false to enter the oom path.
  3170. */
  3171. static inline bool
  3172. should_reclaim_retry(gfp_t gfp_mask, unsigned order,
  3173. struct alloc_context *ac, int alloc_flags,
  3174. bool did_some_progress, int *no_progress_loops)
  3175. {
  3176. struct zone *zone;
  3177. struct zoneref *z;
  3178. /*
  3179. * Costly allocations might have made a progress but this doesn't mean
  3180. * their order will become available due to high fragmentation so
  3181. * always increment the no progress counter for them
  3182. */
  3183. if (did_some_progress && order <= PAGE_ALLOC_COSTLY_ORDER)
  3184. *no_progress_loops = 0;
  3185. else
  3186. (*no_progress_loops)++;
  3187. /*
  3188. * Make sure we converge to OOM if we cannot make any progress
  3189. * several times in the row.
  3190. */
  3191. if (*no_progress_loops > MAX_RECLAIM_RETRIES) {
  3192. /* Before OOM, exhaust highatomic_reserve */
  3193. return unreserve_highatomic_pageblock(ac, true);
  3194. }
  3195. /*
  3196. * Keep reclaiming pages while there is a chance this will lead
  3197. * somewhere. If none of the target zones can satisfy our allocation
  3198. * request even if all reclaimable pages are considered then we are
  3199. * screwed and have to go OOM.
  3200. */
  3201. for_each_zone_zonelist_nodemask(zone, z, ac->zonelist, ac->high_zoneidx,
  3202. ac->nodemask) {
  3203. unsigned long available;
  3204. unsigned long reclaimable;
  3205. unsigned long min_wmark = min_wmark_pages(zone);
  3206. bool wmark;
  3207. available = reclaimable = zone_reclaimable_pages(zone);
  3208. available += zone_page_state_snapshot(zone, NR_FREE_PAGES);
  3209. /*
  3210. * Would the allocation succeed if we reclaimed all
  3211. * reclaimable pages?
  3212. */
  3213. wmark = __zone_watermark_ok(zone, order, min_wmark,
  3214. ac_classzone_idx(ac), alloc_flags, available);
  3215. trace_reclaim_retry_zone(z, order, reclaimable,
  3216. available, min_wmark, *no_progress_loops, wmark);
  3217. if (wmark) {
  3218. /*
  3219. * If we didn't make any progress and have a lot of
  3220. * dirty + writeback pages then we should wait for
  3221. * an IO to complete to slow down the reclaim and
  3222. * prevent from pre mature OOM
  3223. */
  3224. if (!did_some_progress) {
  3225. unsigned long write_pending;
  3226. write_pending = zone_page_state_snapshot(zone,
  3227. NR_ZONE_WRITE_PENDING);
  3228. if (2 * write_pending > reclaimable) {
  3229. congestion_wait(BLK_RW_ASYNC, HZ/10);
  3230. return true;
  3231. }
  3232. }
  3233. /*
  3234. * Memory allocation/reclaim might be called from a WQ
  3235. * context and the current implementation of the WQ
  3236. * concurrency control doesn't recognize that
  3237. * a particular WQ is congested if the worker thread is
  3238. * looping without ever sleeping. Therefore we have to
  3239. * do a short sleep here rather than calling
  3240. * cond_resched().
  3241. */
  3242. if (current->flags & PF_WQ_WORKER)
  3243. schedule_timeout_uninterruptible(1);
  3244. else
  3245. cond_resched();
  3246. return true;
  3247. }
  3248. }
  3249. return false;
  3250. }
  3251. static inline bool
  3252. check_retry_cpuset(int cpuset_mems_cookie, struct alloc_context *ac)
  3253. {
  3254. /*
  3255. * It's possible that cpuset's mems_allowed and the nodemask from
  3256. * mempolicy don't intersect. This should be normally dealt with by
  3257. * policy_nodemask(), but it's possible to race with cpuset update in
  3258. * such a way the check therein was true, and then it became false
  3259. * before we got our cpuset_mems_cookie here.
  3260. * This assumes that for all allocations, ac->nodemask can come only
  3261. * from MPOL_BIND mempolicy (whose documented semantics is to be ignored
  3262. * when it does not intersect with the cpuset restrictions) or the
  3263. * caller can deal with a violated nodemask.
  3264. */
  3265. if (cpusets_enabled() && ac->nodemask &&
  3266. !cpuset_nodemask_valid_mems_allowed(ac->nodemask)) {
  3267. ac->nodemask = NULL;
  3268. return true;
  3269. }
  3270. /*
  3271. * When updating a task's mems_allowed or mempolicy nodemask, it is
  3272. * possible to race with parallel threads in such a way that our
  3273. * allocation can fail while the mask is being updated. If we are about
  3274. * to fail, check if the cpuset changed during allocation and if so,
  3275. * retry.
  3276. */
  3277. if (read_mems_allowed_retry(cpuset_mems_cookie))
  3278. return true;
  3279. return false;
  3280. }
  3281. static inline struct page *
  3282. __alloc_pages_slowpath(gfp_t gfp_mask, unsigned int order,
  3283. struct alloc_context *ac)
  3284. {
  3285. bool can_direct_reclaim = gfp_mask & __GFP_DIRECT_RECLAIM;
  3286. const bool costly_order = order > PAGE_ALLOC_COSTLY_ORDER;
  3287. struct page *page = NULL;
  3288. unsigned int alloc_flags;
  3289. unsigned long did_some_progress;
  3290. enum compact_priority compact_priority;
  3291. enum compact_result compact_result;
  3292. int compaction_retries;
  3293. int no_progress_loops;
  3294. unsigned long alloc_start = jiffies;
  3295. unsigned int stall_timeout = 10 * HZ;
  3296. unsigned int cpuset_mems_cookie;
  3297. /*
  3298. * In the slowpath, we sanity check order to avoid ever trying to
  3299. * reclaim >= MAX_ORDER areas which will never succeed. Callers may
  3300. * be using allocators in order of preference for an area that is
  3301. * too large.
  3302. */
  3303. if (order >= MAX_ORDER) {
  3304. WARN_ON_ONCE(!(gfp_mask & __GFP_NOWARN));
  3305. return NULL;
  3306. }
  3307. /*
  3308. * We also sanity check to catch abuse of atomic reserves being used by
  3309. * callers that are not in atomic context.
  3310. */
  3311. if (WARN_ON_ONCE((gfp_mask & (__GFP_ATOMIC|__GFP_DIRECT_RECLAIM)) ==
  3312. (__GFP_ATOMIC|__GFP_DIRECT_RECLAIM)))
  3313. gfp_mask &= ~__GFP_ATOMIC;
  3314. retry_cpuset:
  3315. compaction_retries = 0;
  3316. no_progress_loops = 0;
  3317. compact_priority = DEF_COMPACT_PRIORITY;
  3318. cpuset_mems_cookie = read_mems_allowed_begin();
  3319. /*
  3320. * The fast path uses conservative alloc_flags to succeed only until
  3321. * kswapd needs to be woken up, and to avoid the cost of setting up
  3322. * alloc_flags precisely. So we do that now.
  3323. */
  3324. alloc_flags = gfp_to_alloc_flags(gfp_mask);
  3325. /*
  3326. * We need to recalculate the starting point for the zonelist iterator
  3327. * because we might have used different nodemask in the fast path, or
  3328. * there was a cpuset modification and we are retrying - otherwise we
  3329. * could end up iterating over non-eligible zones endlessly.
  3330. */
  3331. ac->preferred_zoneref = first_zones_zonelist(ac->zonelist,
  3332. ac->high_zoneidx, ac->nodemask);
  3333. if (!ac->preferred_zoneref->zone)
  3334. goto nopage;
  3335. if (gfp_mask & __GFP_KSWAPD_RECLAIM)
  3336. wake_all_kswapds(order, ac);
  3337. /*
  3338. * The adjusted alloc_flags might result in immediate success, so try
  3339. * that first
  3340. */
  3341. page = get_page_from_freelist(gfp_mask, order, alloc_flags, ac);
  3342. if (page)
  3343. goto got_pg;
  3344. /*
  3345. * For costly allocations, try direct compaction first, as it's likely
  3346. * that we have enough base pages and don't need to reclaim. For non-
  3347. * movable high-order allocations, do that as well, as compaction will
  3348. * try prevent permanent fragmentation by migrating from blocks of the
  3349. * same migratetype.
  3350. * Don't try this for allocations that are allowed to ignore
  3351. * watermarks, as the ALLOC_NO_WATERMARKS attempt didn't yet happen.
  3352. */
  3353. if (can_direct_reclaim &&
  3354. (costly_order ||
  3355. (order > 0 && ac->migratetype != MIGRATE_MOVABLE))
  3356. && !gfp_pfmemalloc_allowed(gfp_mask)) {
  3357. page = __alloc_pages_direct_compact(gfp_mask, order,
  3358. alloc_flags, ac,
  3359. INIT_COMPACT_PRIORITY,
  3360. &compact_result);
  3361. if (page)
  3362. goto got_pg;
  3363. /*
  3364. * Checks for costly allocations with __GFP_NORETRY, which
  3365. * includes THP page fault allocations
  3366. */
  3367. if (costly_order && (gfp_mask & __GFP_NORETRY)) {
  3368. /*
  3369. * If compaction is deferred for high-order allocations,
  3370. * it is because sync compaction recently failed. If
  3371. * this is the case and the caller requested a THP
  3372. * allocation, we do not want to heavily disrupt the
  3373. * system, so we fail the allocation instead of entering
  3374. * direct reclaim.
  3375. */
  3376. if (compact_result == COMPACT_DEFERRED)
  3377. goto nopage;
  3378. /*
  3379. * Looks like reclaim/compaction is worth trying, but
  3380. * sync compaction could be very expensive, so keep
  3381. * using async compaction.
  3382. */
  3383. compact_priority = INIT_COMPACT_PRIORITY;
  3384. }
  3385. }
  3386. retry:
  3387. /* Ensure kswapd doesn't accidentally go to sleep as long as we loop */
  3388. if (gfp_mask & __GFP_KSWAPD_RECLAIM)
  3389. wake_all_kswapds(order, ac);
  3390. if (gfp_pfmemalloc_allowed(gfp_mask))
  3391. alloc_flags = ALLOC_NO_WATERMARKS;
  3392. /*
  3393. * Reset the zonelist iterators if memory policies can be ignored.
  3394. * These allocations are high priority and system rather than user
  3395. * orientated.
  3396. */
  3397. if (!(alloc_flags & ALLOC_CPUSET) || (alloc_flags & ALLOC_NO_WATERMARKS)) {
  3398. ac->zonelist = node_zonelist(numa_node_id(), gfp_mask);
  3399. ac->preferred_zoneref = first_zones_zonelist(ac->zonelist,
  3400. ac->high_zoneidx, ac->nodemask);
  3401. }
  3402. /* Attempt with potentially adjusted zonelist and alloc_flags */
  3403. page = get_page_from_freelist(gfp_mask, order, alloc_flags, ac);
  3404. if (page)
  3405. goto got_pg;
  3406. /* Caller is not willing to reclaim, we can't balance anything */
  3407. if (!can_direct_reclaim)
  3408. goto nopage;
  3409. /* Make sure we know about allocations which stall for too long */
  3410. if (time_after(jiffies, alloc_start + stall_timeout)) {
  3411. warn_alloc(gfp_mask & ~__GFP_NOWARN, ac->nodemask,
  3412. "page allocation stalls for %ums, order:%u",
  3413. jiffies_to_msecs(jiffies-alloc_start), order);
  3414. stall_timeout += 10 * HZ;
  3415. }
  3416. /* Avoid recursion of direct reclaim */
  3417. if (current->flags & PF_MEMALLOC)
  3418. goto nopage;
  3419. /* Try direct reclaim and then allocating */
  3420. page = __alloc_pages_direct_reclaim(gfp_mask, order, alloc_flags, ac,
  3421. &did_some_progress);
  3422. if (page)
  3423. goto got_pg;
  3424. /* Try direct compaction and then allocating */
  3425. page = __alloc_pages_direct_compact(gfp_mask, order, alloc_flags, ac,
  3426. compact_priority, &compact_result);
  3427. if (page)
  3428. goto got_pg;
  3429. /* Do not loop if specifically requested */
  3430. if (gfp_mask & __GFP_NORETRY)
  3431. goto nopage;
  3432. /*
  3433. * Do not retry costly high order allocations unless they are
  3434. * __GFP_RETRY_MAYFAIL
  3435. */
  3436. if (costly_order && !(gfp_mask & __GFP_RETRY_MAYFAIL))
  3437. goto nopage;
  3438. if (should_reclaim_retry(gfp_mask, order, ac, alloc_flags,
  3439. did_some_progress > 0, &no_progress_loops))
  3440. goto retry;
  3441. /*
  3442. * It doesn't make any sense to retry for the compaction if the order-0
  3443. * reclaim is not able to make any progress because the current
  3444. * implementation of the compaction depends on the sufficient amount
  3445. * of free memory (see __compaction_suitable)
  3446. */
  3447. if (did_some_progress > 0 &&
  3448. should_compact_retry(ac, order, alloc_flags,
  3449. compact_result, &compact_priority,
  3450. &compaction_retries))
  3451. goto retry;
  3452. /* Deal with possible cpuset update races before we start OOM killing */
  3453. if (check_retry_cpuset(cpuset_mems_cookie, ac))
  3454. goto retry_cpuset;
  3455. /* Reclaim has failed us, start killing things */
  3456. page = __alloc_pages_may_oom(gfp_mask, order, ac, &did_some_progress);
  3457. if (page)
  3458. goto got_pg;
  3459. /* Avoid allocations with no watermarks from looping endlessly */
  3460. if (test_thread_flag(TIF_MEMDIE) &&
  3461. (alloc_flags == ALLOC_NO_WATERMARKS ||
  3462. (gfp_mask & __GFP_NOMEMALLOC)))
  3463. goto nopage;
  3464. /* Retry as long as the OOM killer is making progress */
  3465. if (did_some_progress) {
  3466. no_progress_loops = 0;
  3467. goto retry;
  3468. }
  3469. nopage:
  3470. /* Deal with possible cpuset update races before we fail */
  3471. if (check_retry_cpuset(cpuset_mems_cookie, ac))
  3472. goto retry_cpuset;
  3473. /*
  3474. * Make sure that __GFP_NOFAIL request doesn't leak out and make sure
  3475. * we always retry
  3476. */
  3477. if (gfp_mask & __GFP_NOFAIL) {
  3478. /*
  3479. * All existing users of the __GFP_NOFAIL are blockable, so warn
  3480. * of any new users that actually require GFP_NOWAIT
  3481. */
  3482. if (WARN_ON_ONCE(!can_direct_reclaim))
  3483. goto fail;
  3484. /*
  3485. * PF_MEMALLOC request from this context is rather bizarre
  3486. * because we cannot reclaim anything and only can loop waiting
  3487. * for somebody to do a work for us
  3488. */
  3489. WARN_ON_ONCE(current->flags & PF_MEMALLOC);
  3490. /*
  3491. * non failing costly orders are a hard requirement which we
  3492. * are not prepared for much so let's warn about these users
  3493. * so that we can identify them and convert them to something
  3494. * else.
  3495. */
  3496. WARN_ON_ONCE(order > PAGE_ALLOC_COSTLY_ORDER);
  3497. /*
  3498. * Help non-failing allocations by giving them access to memory
  3499. * reserves but do not use ALLOC_NO_WATERMARKS because this
  3500. * could deplete whole memory reserves which would just make
  3501. * the situation worse
  3502. */
  3503. page = __alloc_pages_cpuset_fallback(gfp_mask, order, ALLOC_HARDER, ac);
  3504. if (page)
  3505. goto got_pg;
  3506. cond_resched();
  3507. goto retry;
  3508. }
  3509. fail:
  3510. warn_alloc(gfp_mask, ac->nodemask,
  3511. "page allocation failure: order:%u", order);
  3512. got_pg:
  3513. return page;
  3514. }
  3515. static inline bool prepare_alloc_pages(gfp_t gfp_mask, unsigned int order,
  3516. int preferred_nid, nodemask_t *nodemask,
  3517. struct alloc_context *ac, gfp_t *alloc_mask,
  3518. unsigned int *alloc_flags)
  3519. {
  3520. ac->high_zoneidx = gfp_zone(gfp_mask);
  3521. ac->zonelist = node_zonelist(preferred_nid, gfp_mask);
  3522. ac->nodemask = nodemask;
  3523. ac->migratetype = gfpflags_to_migratetype(gfp_mask);
  3524. if (cpusets_enabled()) {
  3525. *alloc_mask |= __GFP_HARDWALL;
  3526. if (!ac->nodemask)
  3527. ac->nodemask = &cpuset_current_mems_allowed;
  3528. else
  3529. *alloc_flags |= ALLOC_CPUSET;
  3530. }
  3531. fs_reclaim_acquire(gfp_mask);
  3532. fs_reclaim_release(gfp_mask);
  3533. might_sleep_if(gfp_mask & __GFP_DIRECT_RECLAIM);
  3534. if (should_fail_alloc_page(gfp_mask, order))
  3535. return false;
  3536. if (IS_ENABLED(CONFIG_CMA) && ac->migratetype == MIGRATE_MOVABLE)
  3537. *alloc_flags |= ALLOC_CMA;
  3538. return true;
  3539. }
  3540. /* Determine whether to spread dirty pages and what the first usable zone */
  3541. static inline void finalise_ac(gfp_t gfp_mask,
  3542. unsigned int order, struct alloc_context *ac)
  3543. {
  3544. /* Dirty zone balancing only done in the fast path */
  3545. ac->spread_dirty_pages = (gfp_mask & __GFP_WRITE);
  3546. /*
  3547. * The preferred zone is used for statistics but crucially it is
  3548. * also used as the starting point for the zonelist iterator. It
  3549. * may get reset for allocations that ignore memory policies.
  3550. */
  3551. ac->preferred_zoneref = first_zones_zonelist(ac->zonelist,
  3552. ac->high_zoneidx, ac->nodemask);
  3553. }
  3554. /*
  3555. * This is the 'heart' of the zoned buddy allocator.
  3556. */
  3557. struct page *
  3558. __alloc_pages_nodemask(gfp_t gfp_mask, unsigned int order, int preferred_nid,
  3559. nodemask_t *nodemask)
  3560. {
  3561. struct page *page;
  3562. unsigned int alloc_flags = ALLOC_WMARK_LOW;
  3563. gfp_t alloc_mask = gfp_mask; /* The gfp_t that was actually used for allocation */
  3564. struct alloc_context ac = { };
  3565. gfp_mask &= gfp_allowed_mask;
  3566. if (!prepare_alloc_pages(gfp_mask, order, preferred_nid, nodemask, &ac, &alloc_mask, &alloc_flags))
  3567. return NULL;
  3568. finalise_ac(gfp_mask, order, &ac);
  3569. /* First allocation attempt */
  3570. page = get_page_from_freelist(alloc_mask, order, alloc_flags, &ac);
  3571. if (likely(page))
  3572. goto out;
  3573. /*
  3574. * Apply scoped allocation constraints. This is mainly about GFP_NOFS
  3575. * resp. GFP_NOIO which has to be inherited for all allocation requests
  3576. * from a particular context which has been marked by
  3577. * memalloc_no{fs,io}_{save,restore}.
  3578. */
  3579. alloc_mask = current_gfp_context(gfp_mask);
  3580. ac.spread_dirty_pages = false;
  3581. /*
  3582. * Restore the original nodemask if it was potentially replaced with
  3583. * &cpuset_current_mems_allowed to optimize the fast-path attempt.
  3584. */
  3585. if (unlikely(ac.nodemask != nodemask))
  3586. ac.nodemask = nodemask;
  3587. page = __alloc_pages_slowpath(alloc_mask, order, &ac);
  3588. out:
  3589. if (memcg_kmem_enabled() && (gfp_mask & __GFP_ACCOUNT) && page &&
  3590. unlikely(memcg_kmem_charge(page, gfp_mask, order) != 0)) {
  3591. __free_pages(page, order);
  3592. page = NULL;
  3593. }
  3594. if (kmemcheck_enabled && page)
  3595. kmemcheck_pagealloc_alloc(page, order, gfp_mask);
  3596. trace_mm_page_alloc(page, order, alloc_mask, ac.migratetype);
  3597. return page;
  3598. }
  3599. EXPORT_SYMBOL(__alloc_pages_nodemask);
  3600. /*
  3601. * Common helper functions.
  3602. */
  3603. unsigned long __get_free_pages(gfp_t gfp_mask, unsigned int order)
  3604. {
  3605. struct page *page;
  3606. /*
  3607. * __get_free_pages() returns a 32-bit address, which cannot represent
  3608. * a highmem page
  3609. */
  3610. VM_BUG_ON((gfp_mask & __GFP_HIGHMEM) != 0);
  3611. page = alloc_pages(gfp_mask, order);
  3612. if (!page)
  3613. return 0;
  3614. return (unsigned long) page_address(page);
  3615. }
  3616. EXPORT_SYMBOL(__get_free_pages);
  3617. unsigned long get_zeroed_page(gfp_t gfp_mask)
  3618. {
  3619. return __get_free_pages(gfp_mask | __GFP_ZERO, 0);
  3620. }
  3621. EXPORT_SYMBOL(get_zeroed_page);
  3622. void __free_pages(struct page *page, unsigned int order)
  3623. {
  3624. if (put_page_testzero(page)) {
  3625. if (order == 0)
  3626. free_hot_cold_page(page, false);
  3627. else
  3628. __free_pages_ok(page, order);
  3629. }
  3630. }
  3631. EXPORT_SYMBOL(__free_pages);
  3632. void free_pages(unsigned long addr, unsigned int order)
  3633. {
  3634. if (addr != 0) {
  3635. VM_BUG_ON(!virt_addr_valid((void *)addr));
  3636. __free_pages(virt_to_page((void *)addr), order);
  3637. }
  3638. }
  3639. EXPORT_SYMBOL(free_pages);
  3640. /*
  3641. * Page Fragment:
  3642. * An arbitrary-length arbitrary-offset area of memory which resides
  3643. * within a 0 or higher order page. Multiple fragments within that page
  3644. * are individually refcounted, in the page's reference counter.
  3645. *
  3646. * The page_frag functions below provide a simple allocation framework for
  3647. * page fragments. This is used by the network stack and network device
  3648. * drivers to provide a backing region of memory for use as either an
  3649. * sk_buff->head, or to be used in the "frags" portion of skb_shared_info.
  3650. */
  3651. static struct page *__page_frag_cache_refill(struct page_frag_cache *nc,
  3652. gfp_t gfp_mask)
  3653. {
  3654. struct page *page = NULL;
  3655. gfp_t gfp = gfp_mask;
  3656. #if (PAGE_SIZE < PAGE_FRAG_CACHE_MAX_SIZE)
  3657. gfp_mask |= __GFP_COMP | __GFP_NOWARN | __GFP_NORETRY |
  3658. __GFP_NOMEMALLOC;
  3659. page = alloc_pages_node(NUMA_NO_NODE, gfp_mask,
  3660. PAGE_FRAG_CACHE_MAX_ORDER);
  3661. nc->size = page ? PAGE_FRAG_CACHE_MAX_SIZE : PAGE_SIZE;
  3662. #endif
  3663. if (unlikely(!page))
  3664. page = alloc_pages_node(NUMA_NO_NODE, gfp, 0);
  3665. nc->va = page ? page_address(page) : NULL;
  3666. return page;
  3667. }
  3668. void __page_frag_cache_drain(struct page *page, unsigned int count)
  3669. {
  3670. VM_BUG_ON_PAGE(page_ref_count(page) == 0, page);
  3671. if (page_ref_sub_and_test(page, count)) {
  3672. unsigned int order = compound_order(page);
  3673. if (order == 0)
  3674. free_hot_cold_page(page, false);
  3675. else
  3676. __free_pages_ok(page, order);
  3677. }
  3678. }
  3679. EXPORT_SYMBOL(__page_frag_cache_drain);
  3680. void *page_frag_alloc(struct page_frag_cache *nc,
  3681. unsigned int fragsz, gfp_t gfp_mask)
  3682. {
  3683. unsigned int size = PAGE_SIZE;
  3684. struct page *page;
  3685. int offset;
  3686. if (unlikely(!nc->va)) {
  3687. refill:
  3688. page = __page_frag_cache_refill(nc, gfp_mask);
  3689. if (!page)
  3690. return NULL;
  3691. #if (PAGE_SIZE < PAGE_FRAG_CACHE_MAX_SIZE)
  3692. /* if size can vary use size else just use PAGE_SIZE */
  3693. size = nc->size;
  3694. #endif
  3695. /* Even if we own the page, we do not use atomic_set().
  3696. * This would break get_page_unless_zero() users.
  3697. */
  3698. page_ref_add(page, size - 1);
  3699. /* reset page count bias and offset to start of new frag */
  3700. nc->pfmemalloc = page_is_pfmemalloc(page);
  3701. nc->pagecnt_bias = size;
  3702. nc->offset = size;
  3703. }
  3704. offset = nc->offset - fragsz;
  3705. if (unlikely(offset < 0)) {
  3706. page = virt_to_page(nc->va);
  3707. if (!page_ref_sub_and_test(page, nc->pagecnt_bias))
  3708. goto refill;
  3709. #if (PAGE_SIZE < PAGE_FRAG_CACHE_MAX_SIZE)
  3710. /* if size can vary use size else just use PAGE_SIZE */
  3711. size = nc->size;
  3712. #endif
  3713. /* OK, page count is 0, we can safely set it */
  3714. set_page_count(page, size);
  3715. /* reset page count bias and offset to start of new frag */
  3716. nc->pagecnt_bias = size;
  3717. offset = size - fragsz;
  3718. }
  3719. nc->pagecnt_bias--;
  3720. nc->offset = offset;
  3721. return nc->va + offset;
  3722. }
  3723. EXPORT_SYMBOL(page_frag_alloc);
  3724. /*
  3725. * Frees a page fragment allocated out of either a compound or order 0 page.
  3726. */
  3727. void page_frag_free(void *addr)
  3728. {
  3729. struct page *page = virt_to_head_page(addr);
  3730. if (unlikely(put_page_testzero(page)))
  3731. __free_pages_ok(page, compound_order(page));
  3732. }
  3733. EXPORT_SYMBOL(page_frag_free);
  3734. static void *make_alloc_exact(unsigned long addr, unsigned int order,
  3735. size_t size)
  3736. {
  3737. if (addr) {
  3738. unsigned long alloc_end = addr + (PAGE_SIZE << order);
  3739. unsigned long used = addr + PAGE_ALIGN(size);
  3740. split_page(virt_to_page((void *)addr), order);
  3741. while (used < alloc_end) {
  3742. free_page(used);
  3743. used += PAGE_SIZE;
  3744. }
  3745. }
  3746. return (void *)addr;
  3747. }
  3748. /**
  3749. * alloc_pages_exact - allocate an exact number physically-contiguous pages.
  3750. * @size: the number of bytes to allocate
  3751. * @gfp_mask: GFP flags for the allocation
  3752. *
  3753. * This function is similar to alloc_pages(), except that it allocates the
  3754. * minimum number of pages to satisfy the request. alloc_pages() can only
  3755. * allocate memory in power-of-two pages.
  3756. *
  3757. * This function is also limited by MAX_ORDER.
  3758. *
  3759. * Memory allocated by this function must be released by free_pages_exact().
  3760. */
  3761. void *alloc_pages_exact(size_t size, gfp_t gfp_mask)
  3762. {
  3763. unsigned int order = get_order(size);
  3764. unsigned long addr;
  3765. addr = __get_free_pages(gfp_mask, order);
  3766. return make_alloc_exact(addr, order, size);
  3767. }
  3768. EXPORT_SYMBOL(alloc_pages_exact);
  3769. /**
  3770. * alloc_pages_exact_nid - allocate an exact number of physically-contiguous
  3771. * pages on a node.
  3772. * @nid: the preferred node ID where memory should be allocated
  3773. * @size: the number of bytes to allocate
  3774. * @gfp_mask: GFP flags for the allocation
  3775. *
  3776. * Like alloc_pages_exact(), but try to allocate on node nid first before falling
  3777. * back.
  3778. */
  3779. void * __meminit alloc_pages_exact_nid(int nid, size_t size, gfp_t gfp_mask)
  3780. {
  3781. unsigned int order = get_order(size);
  3782. struct page *p = alloc_pages_node(nid, gfp_mask, order);
  3783. if (!p)
  3784. return NULL;
  3785. return make_alloc_exact((unsigned long)page_address(p), order, size);
  3786. }
  3787. /**
  3788. * free_pages_exact - release memory allocated via alloc_pages_exact()
  3789. * @virt: the value returned by alloc_pages_exact.
  3790. * @size: size of allocation, same value as passed to alloc_pages_exact().
  3791. *
  3792. * Release the memory allocated by a previous call to alloc_pages_exact.
  3793. */
  3794. void free_pages_exact(void *virt, size_t size)
  3795. {
  3796. unsigned long addr = (unsigned long)virt;
  3797. unsigned long end = addr + PAGE_ALIGN(size);
  3798. while (addr < end) {
  3799. free_page(addr);
  3800. addr += PAGE_SIZE;
  3801. }
  3802. }
  3803. EXPORT_SYMBOL(free_pages_exact);
  3804. /**
  3805. * nr_free_zone_pages - count number of pages beyond high watermark
  3806. * @offset: The zone index of the highest zone
  3807. *
  3808. * nr_free_zone_pages() counts the number of counts pages which are beyond the
  3809. * high watermark within all zones at or below a given zone index. For each
  3810. * zone, the number of pages is calculated as:
  3811. *
  3812. * nr_free_zone_pages = managed_pages - high_pages
  3813. */
  3814. static unsigned long nr_free_zone_pages(int offset)
  3815. {
  3816. struct zoneref *z;
  3817. struct zone *zone;
  3818. /* Just pick one node, since fallback list is circular */
  3819. unsigned long sum = 0;
  3820. struct zonelist *zonelist = node_zonelist(numa_node_id(), GFP_KERNEL);
  3821. for_each_zone_zonelist(zone, z, zonelist, offset) {
  3822. unsigned long size = zone->managed_pages;
  3823. unsigned long high = high_wmark_pages(zone);
  3824. if (size > high)
  3825. sum += size - high;
  3826. }
  3827. return sum;
  3828. }
  3829. /**
  3830. * nr_free_buffer_pages - count number of pages beyond high watermark
  3831. *
  3832. * nr_free_buffer_pages() counts the number of pages which are beyond the high
  3833. * watermark within ZONE_DMA and ZONE_NORMAL.
  3834. */
  3835. unsigned long nr_free_buffer_pages(void)
  3836. {
  3837. return nr_free_zone_pages(gfp_zone(GFP_USER));
  3838. }
  3839. EXPORT_SYMBOL_GPL(nr_free_buffer_pages);
  3840. /**
  3841. * nr_free_pagecache_pages - count number of pages beyond high watermark
  3842. *
  3843. * nr_free_pagecache_pages() counts the number of pages which are beyond the
  3844. * high watermark within all zones.
  3845. */
  3846. unsigned long nr_free_pagecache_pages(void)
  3847. {
  3848. return nr_free_zone_pages(gfp_zone(GFP_HIGHUSER_MOVABLE));
  3849. }
  3850. static inline void show_node(struct zone *zone)
  3851. {
  3852. if (IS_ENABLED(CONFIG_NUMA))
  3853. printk("Node %d ", zone_to_nid(zone));
  3854. }
  3855. long si_mem_available(void)
  3856. {
  3857. long available;
  3858. unsigned long pagecache;
  3859. unsigned long wmark_low = 0;
  3860. unsigned long pages[NR_LRU_LISTS];
  3861. struct zone *zone;
  3862. int lru;
  3863. for (lru = LRU_BASE; lru < NR_LRU_LISTS; lru++)
  3864. pages[lru] = global_node_page_state(NR_LRU_BASE + lru);
  3865. for_each_zone(zone)
  3866. wmark_low += zone->watermark[WMARK_LOW];
  3867. /*
  3868. * Estimate the amount of memory available for userspace allocations,
  3869. * without causing swapping.
  3870. */
  3871. available = global_zone_page_state(NR_FREE_PAGES) - totalreserve_pages;
  3872. /*
  3873. * Not all the page cache can be freed, otherwise the system will
  3874. * start swapping. Assume at least half of the page cache, or the
  3875. * low watermark worth of cache, needs to stay.
  3876. */
  3877. pagecache = pages[LRU_ACTIVE_FILE] + pages[LRU_INACTIVE_FILE];
  3878. pagecache -= min(pagecache / 2, wmark_low);
  3879. available += pagecache;
  3880. /*
  3881. * Part of the reclaimable slab consists of items that are in use,
  3882. * and cannot be freed. Cap this estimate at the low watermark.
  3883. */
  3884. available += global_node_page_state(NR_SLAB_RECLAIMABLE) -
  3885. min(global_node_page_state(NR_SLAB_RECLAIMABLE) / 2,
  3886. wmark_low);
  3887. if (available < 0)
  3888. available = 0;
  3889. return available;
  3890. }
  3891. EXPORT_SYMBOL_GPL(si_mem_available);
  3892. void si_meminfo(struct sysinfo *val)
  3893. {
  3894. val->totalram = totalram_pages;
  3895. val->sharedram = global_node_page_state(NR_SHMEM);
  3896. val->freeram = global_zone_page_state(NR_FREE_PAGES);
  3897. val->bufferram = nr_blockdev_pages();
  3898. val->totalhigh = totalhigh_pages;
  3899. val->freehigh = nr_free_highpages();
  3900. val->mem_unit = PAGE_SIZE;
  3901. }
  3902. EXPORT_SYMBOL(si_meminfo);
  3903. #ifdef CONFIG_NUMA
  3904. void si_meminfo_node(struct sysinfo *val, int nid)
  3905. {
  3906. int zone_type; /* needs to be signed */
  3907. unsigned long managed_pages = 0;
  3908. unsigned long managed_highpages = 0;
  3909. unsigned long free_highpages = 0;
  3910. pg_data_t *pgdat = NODE_DATA(nid);
  3911. for (zone_type = 0; zone_type < MAX_NR_ZONES; zone_type++)
  3912. managed_pages += pgdat->node_zones[zone_type].managed_pages;
  3913. val->totalram = managed_pages;
  3914. val->sharedram = node_page_state(pgdat, NR_SHMEM);
  3915. val->freeram = sum_zone_node_page_state(nid, NR_FREE_PAGES);
  3916. #ifdef CONFIG_HIGHMEM
  3917. for (zone_type = 0; zone_type < MAX_NR_ZONES; zone_type++) {
  3918. struct zone *zone = &pgdat->node_zones[zone_type];
  3919. if (is_highmem(zone)) {
  3920. managed_highpages += zone->managed_pages;
  3921. free_highpages += zone_page_state(zone, NR_FREE_PAGES);
  3922. }
  3923. }
  3924. val->totalhigh = managed_highpages;
  3925. val->freehigh = free_highpages;
  3926. #else
  3927. val->totalhigh = managed_highpages;
  3928. val->freehigh = free_highpages;
  3929. #endif
  3930. val->mem_unit = PAGE_SIZE;
  3931. }
  3932. #endif
  3933. /*
  3934. * Determine whether the node should be displayed or not, depending on whether
  3935. * SHOW_MEM_FILTER_NODES was passed to show_free_areas().
  3936. */
  3937. static bool show_mem_node_skip(unsigned int flags, int nid, nodemask_t *nodemask)
  3938. {
  3939. if (!(flags & SHOW_MEM_FILTER_NODES))
  3940. return false;
  3941. /*
  3942. * no node mask - aka implicit memory numa policy. Do not bother with
  3943. * the synchronization - read_mems_allowed_begin - because we do not
  3944. * have to be precise here.
  3945. */
  3946. if (!nodemask)
  3947. nodemask = &cpuset_current_mems_allowed;
  3948. return !node_isset(nid, *nodemask);
  3949. }
  3950. #define K(x) ((x) << (PAGE_SHIFT-10))
  3951. static void show_migration_types(unsigned char type)
  3952. {
  3953. static const char types[MIGRATE_TYPES] = {
  3954. [MIGRATE_UNMOVABLE] = 'U',
  3955. [MIGRATE_MOVABLE] = 'M',
  3956. [MIGRATE_RECLAIMABLE] = 'E',
  3957. [MIGRATE_HIGHATOMIC] = 'H',
  3958. #ifdef CONFIG_CMA
  3959. [MIGRATE_CMA] = 'C',
  3960. #endif
  3961. #ifdef CONFIG_MEMORY_ISOLATION
  3962. [MIGRATE_ISOLATE] = 'I',
  3963. #endif
  3964. };
  3965. char tmp[MIGRATE_TYPES + 1];
  3966. char *p = tmp;
  3967. int i;
  3968. for (i = 0; i < MIGRATE_TYPES; i++) {
  3969. if (type & (1 << i))
  3970. *p++ = types[i];
  3971. }
  3972. *p = '\0';
  3973. printk(KERN_CONT "(%s) ", tmp);
  3974. }
  3975. /*
  3976. * Show free area list (used inside shift_scroll-lock stuff)
  3977. * We also calculate the percentage fragmentation. We do this by counting the
  3978. * memory on each free list with the exception of the first item on the list.
  3979. *
  3980. * Bits in @filter:
  3981. * SHOW_MEM_FILTER_NODES: suppress nodes that are not allowed by current's
  3982. * cpuset.
  3983. */
  3984. void show_free_areas(unsigned int filter, nodemask_t *nodemask)
  3985. {
  3986. unsigned long free_pcp = 0;
  3987. int cpu;
  3988. struct zone *zone;
  3989. pg_data_t *pgdat;
  3990. for_each_populated_zone(zone) {
  3991. if (show_mem_node_skip(filter, zone_to_nid(zone), nodemask))
  3992. continue;
  3993. for_each_online_cpu(cpu)
  3994. free_pcp += per_cpu_ptr(zone->pageset, cpu)->pcp.count;
  3995. }
  3996. printk("active_anon:%lu inactive_anon:%lu isolated_anon:%lu\n"
  3997. " active_file:%lu inactive_file:%lu isolated_file:%lu\n"
  3998. " unevictable:%lu dirty:%lu writeback:%lu unstable:%lu\n"
  3999. " slab_reclaimable:%lu slab_unreclaimable:%lu\n"
  4000. " mapped:%lu shmem:%lu pagetables:%lu bounce:%lu\n"
  4001. " free:%lu free_pcp:%lu free_cma:%lu\n",
  4002. global_node_page_state(NR_ACTIVE_ANON),
  4003. global_node_page_state(NR_INACTIVE_ANON),
  4004. global_node_page_state(NR_ISOLATED_ANON),
  4005. global_node_page_state(NR_ACTIVE_FILE),
  4006. global_node_page_state(NR_INACTIVE_FILE),
  4007. global_node_page_state(NR_ISOLATED_FILE),
  4008. global_node_page_state(NR_UNEVICTABLE),
  4009. global_node_page_state(NR_FILE_DIRTY),
  4010. global_node_page_state(NR_WRITEBACK),
  4011. global_node_page_state(NR_UNSTABLE_NFS),
  4012. global_node_page_state(NR_SLAB_RECLAIMABLE),
  4013. global_node_page_state(NR_SLAB_UNRECLAIMABLE),
  4014. global_node_page_state(NR_FILE_MAPPED),
  4015. global_node_page_state(NR_SHMEM),
  4016. global_zone_page_state(NR_PAGETABLE),
  4017. global_zone_page_state(NR_BOUNCE),
  4018. global_zone_page_state(NR_FREE_PAGES),
  4019. free_pcp,
  4020. global_zone_page_state(NR_FREE_CMA_PAGES));
  4021. for_each_online_pgdat(pgdat) {
  4022. if (show_mem_node_skip(filter, pgdat->node_id, nodemask))
  4023. continue;
  4024. printk("Node %d"
  4025. " active_anon:%lukB"
  4026. " inactive_anon:%lukB"
  4027. " active_file:%lukB"
  4028. " inactive_file:%lukB"
  4029. " unevictable:%lukB"
  4030. " isolated(anon):%lukB"
  4031. " isolated(file):%lukB"
  4032. " mapped:%lukB"
  4033. " dirty:%lukB"
  4034. " writeback:%lukB"
  4035. " shmem:%lukB"
  4036. #ifdef CONFIG_TRANSPARENT_HUGEPAGE
  4037. " shmem_thp: %lukB"
  4038. " shmem_pmdmapped: %lukB"
  4039. " anon_thp: %lukB"
  4040. #endif
  4041. " writeback_tmp:%lukB"
  4042. " unstable:%lukB"
  4043. " all_unreclaimable? %s"
  4044. "\n",
  4045. pgdat->node_id,
  4046. K(node_page_state(pgdat, NR_ACTIVE_ANON)),
  4047. K(node_page_state(pgdat, NR_INACTIVE_ANON)),
  4048. K(node_page_state(pgdat, NR_ACTIVE_FILE)),
  4049. K(node_page_state(pgdat, NR_INACTIVE_FILE)),
  4050. K(node_page_state(pgdat, NR_UNEVICTABLE)),
  4051. K(node_page_state(pgdat, NR_ISOLATED_ANON)),
  4052. K(node_page_state(pgdat, NR_ISOLATED_FILE)),
  4053. K(node_page_state(pgdat, NR_FILE_MAPPED)),
  4054. K(node_page_state(pgdat, NR_FILE_DIRTY)),
  4055. K(node_page_state(pgdat, NR_WRITEBACK)),
  4056. K(node_page_state(pgdat, NR_SHMEM)),
  4057. #ifdef CONFIG_TRANSPARENT_HUGEPAGE
  4058. K(node_page_state(pgdat, NR_SHMEM_THPS) * HPAGE_PMD_NR),
  4059. K(node_page_state(pgdat, NR_SHMEM_PMDMAPPED)
  4060. * HPAGE_PMD_NR),
  4061. K(node_page_state(pgdat, NR_ANON_THPS) * HPAGE_PMD_NR),
  4062. #endif
  4063. K(node_page_state(pgdat, NR_WRITEBACK_TEMP)),
  4064. K(node_page_state(pgdat, NR_UNSTABLE_NFS)),
  4065. pgdat->kswapd_failures >= MAX_RECLAIM_RETRIES ?
  4066. "yes" : "no");
  4067. }
  4068. for_each_populated_zone(zone) {
  4069. int i;
  4070. if (show_mem_node_skip(filter, zone_to_nid(zone), nodemask))
  4071. continue;
  4072. free_pcp = 0;
  4073. for_each_online_cpu(cpu)
  4074. free_pcp += per_cpu_ptr(zone->pageset, cpu)->pcp.count;
  4075. show_node(zone);
  4076. printk(KERN_CONT
  4077. "%s"
  4078. " free:%lukB"
  4079. " min:%lukB"
  4080. " low:%lukB"
  4081. " high:%lukB"
  4082. " active_anon:%lukB"
  4083. " inactive_anon:%lukB"
  4084. " active_file:%lukB"
  4085. " inactive_file:%lukB"
  4086. " unevictable:%lukB"
  4087. " writepending:%lukB"
  4088. " present:%lukB"
  4089. " managed:%lukB"
  4090. " mlocked:%lukB"
  4091. " kernel_stack:%lukB"
  4092. " pagetables:%lukB"
  4093. " bounce:%lukB"
  4094. " free_pcp:%lukB"
  4095. " local_pcp:%ukB"
  4096. " free_cma:%lukB"
  4097. "\n",
  4098. zone->name,
  4099. K(zone_page_state(zone, NR_FREE_PAGES)),
  4100. K(min_wmark_pages(zone)),
  4101. K(low_wmark_pages(zone)),
  4102. K(high_wmark_pages(zone)),
  4103. K(zone_page_state(zone, NR_ZONE_ACTIVE_ANON)),
  4104. K(zone_page_state(zone, NR_ZONE_INACTIVE_ANON)),
  4105. K(zone_page_state(zone, NR_ZONE_ACTIVE_FILE)),
  4106. K(zone_page_state(zone, NR_ZONE_INACTIVE_FILE)),
  4107. K(zone_page_state(zone, NR_ZONE_UNEVICTABLE)),
  4108. K(zone_page_state(zone, NR_ZONE_WRITE_PENDING)),
  4109. K(zone->present_pages),
  4110. K(zone->managed_pages),
  4111. K(zone_page_state(zone, NR_MLOCK)),
  4112. zone_page_state(zone, NR_KERNEL_STACK_KB),
  4113. K(zone_page_state(zone, NR_PAGETABLE)),
  4114. K(zone_page_state(zone, NR_BOUNCE)),
  4115. K(free_pcp),
  4116. K(this_cpu_read(zone->pageset->pcp.count)),
  4117. K(zone_page_state(zone, NR_FREE_CMA_PAGES)));
  4118. printk("lowmem_reserve[]:");
  4119. for (i = 0; i < MAX_NR_ZONES; i++)
  4120. printk(KERN_CONT " %ld", zone->lowmem_reserve[i]);
  4121. printk(KERN_CONT "\n");
  4122. }
  4123. for_each_populated_zone(zone) {
  4124. unsigned int order;
  4125. unsigned long nr[MAX_ORDER], flags, total = 0;
  4126. unsigned char types[MAX_ORDER];
  4127. if (show_mem_node_skip(filter, zone_to_nid(zone), nodemask))
  4128. continue;
  4129. show_node(zone);
  4130. printk(KERN_CONT "%s: ", zone->name);
  4131. spin_lock_irqsave(&zone->lock, flags);
  4132. for (order = 0; order < MAX_ORDER; order++) {
  4133. struct free_area *area = &zone->free_area[order];
  4134. int type;
  4135. nr[order] = area->nr_free;
  4136. total += nr[order] << order;
  4137. types[order] = 0;
  4138. for (type = 0; type < MIGRATE_TYPES; type++) {
  4139. if (!list_empty(&area->free_list[type]))
  4140. types[order] |= 1 << type;
  4141. }
  4142. }
  4143. spin_unlock_irqrestore(&zone->lock, flags);
  4144. for (order = 0; order < MAX_ORDER; order++) {
  4145. printk(KERN_CONT "%lu*%lukB ",
  4146. nr[order], K(1UL) << order);
  4147. if (nr[order])
  4148. show_migration_types(types[order]);
  4149. }
  4150. printk(KERN_CONT "= %lukB\n", K(total));
  4151. }
  4152. hugetlb_show_meminfo();
  4153. printk("%ld total pagecache pages\n", global_node_page_state(NR_FILE_PAGES));
  4154. show_swap_cache_info();
  4155. }
  4156. static void zoneref_set_zone(struct zone *zone, struct zoneref *zoneref)
  4157. {
  4158. zoneref->zone = zone;
  4159. zoneref->zone_idx = zone_idx(zone);
  4160. }
  4161. /*
  4162. * Builds allocation fallback zone lists.
  4163. *
  4164. * Add all populated zones of a node to the zonelist.
  4165. */
  4166. static int build_zonerefs_node(pg_data_t *pgdat, struct zoneref *zonerefs)
  4167. {
  4168. struct zone *zone;
  4169. enum zone_type zone_type = MAX_NR_ZONES;
  4170. int nr_zones = 0;
  4171. do {
  4172. zone_type--;
  4173. zone = pgdat->node_zones + zone_type;
  4174. if (managed_zone(zone)) {
  4175. zoneref_set_zone(zone, &zonerefs[nr_zones++]);
  4176. check_highest_zone(zone_type);
  4177. }
  4178. } while (zone_type);
  4179. return nr_zones;
  4180. }
  4181. #ifdef CONFIG_NUMA
  4182. static int __parse_numa_zonelist_order(char *s)
  4183. {
  4184. /*
  4185. * We used to support different zonlists modes but they turned
  4186. * out to be just not useful. Let's keep the warning in place
  4187. * if somebody still use the cmd line parameter so that we do
  4188. * not fail it silently
  4189. */
  4190. if (!(*s == 'd' || *s == 'D' || *s == 'n' || *s == 'N')) {
  4191. pr_warn("Ignoring unsupported numa_zonelist_order value: %s\n", s);
  4192. return -EINVAL;
  4193. }
  4194. return 0;
  4195. }
  4196. static __init int setup_numa_zonelist_order(char *s)
  4197. {
  4198. if (!s)
  4199. return 0;
  4200. return __parse_numa_zonelist_order(s);
  4201. }
  4202. early_param("numa_zonelist_order", setup_numa_zonelist_order);
  4203. char numa_zonelist_order[] = "Node";
  4204. /*
  4205. * sysctl handler for numa_zonelist_order
  4206. */
  4207. int numa_zonelist_order_handler(struct ctl_table *table, int write,
  4208. void __user *buffer, size_t *length,
  4209. loff_t *ppos)
  4210. {
  4211. char *str;
  4212. int ret;
  4213. if (!write)
  4214. return proc_dostring(table, write, buffer, length, ppos);
  4215. str = memdup_user_nul(buffer, 16);
  4216. if (IS_ERR(str))
  4217. return PTR_ERR(str);
  4218. ret = __parse_numa_zonelist_order(str);
  4219. kfree(str);
  4220. return ret;
  4221. }
  4222. #define MAX_NODE_LOAD (nr_online_nodes)
  4223. static int node_load[MAX_NUMNODES];
  4224. /**
  4225. * find_next_best_node - find the next node that should appear in a given node's fallback list
  4226. * @node: node whose fallback list we're appending
  4227. * @used_node_mask: nodemask_t of already used nodes
  4228. *
  4229. * We use a number of factors to determine which is the next node that should
  4230. * appear on a given node's fallback list. The node should not have appeared
  4231. * already in @node's fallback list, and it should be the next closest node
  4232. * according to the distance array (which contains arbitrary distance values
  4233. * from each node to each node in the system), and should also prefer nodes
  4234. * with no CPUs, since presumably they'll have very little allocation pressure
  4235. * on them otherwise.
  4236. * It returns -1 if no node is found.
  4237. */
  4238. static int find_next_best_node(int node, nodemask_t *used_node_mask)
  4239. {
  4240. int n, val;
  4241. int min_val = INT_MAX;
  4242. int best_node = NUMA_NO_NODE;
  4243. const struct cpumask *tmp = cpumask_of_node(0);
  4244. /* Use the local node if we haven't already */
  4245. if (!node_isset(node, *used_node_mask)) {
  4246. node_set(node, *used_node_mask);
  4247. return node;
  4248. }
  4249. for_each_node_state(n, N_MEMORY) {
  4250. /* Don't want a node to appear more than once */
  4251. if (node_isset(n, *used_node_mask))
  4252. continue;
  4253. /* Use the distance array to find the distance */
  4254. val = node_distance(node, n);
  4255. /* Penalize nodes under us ("prefer the next node") */
  4256. val += (n < node);
  4257. /* Give preference to headless and unused nodes */
  4258. tmp = cpumask_of_node(n);
  4259. if (!cpumask_empty(tmp))
  4260. val += PENALTY_FOR_NODE_WITH_CPUS;
  4261. /* Slight preference for less loaded node */
  4262. val *= (MAX_NODE_LOAD*MAX_NUMNODES);
  4263. val += node_load[n];
  4264. if (val < min_val) {
  4265. min_val = val;
  4266. best_node = n;
  4267. }
  4268. }
  4269. if (best_node >= 0)
  4270. node_set(best_node, *used_node_mask);
  4271. return best_node;
  4272. }
  4273. /*
  4274. * Build zonelists ordered by node and zones within node.
  4275. * This results in maximum locality--normal zone overflows into local
  4276. * DMA zone, if any--but risks exhausting DMA zone.
  4277. */
  4278. static void build_zonelists_in_node_order(pg_data_t *pgdat, int *node_order,
  4279. unsigned nr_nodes)
  4280. {
  4281. struct zoneref *zonerefs;
  4282. int i;
  4283. zonerefs = pgdat->node_zonelists[ZONELIST_FALLBACK]._zonerefs;
  4284. for (i = 0; i < nr_nodes; i++) {
  4285. int nr_zones;
  4286. pg_data_t *node = NODE_DATA(node_order[i]);
  4287. nr_zones = build_zonerefs_node(node, zonerefs);
  4288. zonerefs += nr_zones;
  4289. }
  4290. zonerefs->zone = NULL;
  4291. zonerefs->zone_idx = 0;
  4292. }
  4293. /*
  4294. * Build gfp_thisnode zonelists
  4295. */
  4296. static void build_thisnode_zonelists(pg_data_t *pgdat)
  4297. {
  4298. struct zoneref *zonerefs;
  4299. int nr_zones;
  4300. zonerefs = pgdat->node_zonelists[ZONELIST_NOFALLBACK]._zonerefs;
  4301. nr_zones = build_zonerefs_node(pgdat, zonerefs);
  4302. zonerefs += nr_zones;
  4303. zonerefs->zone = NULL;
  4304. zonerefs->zone_idx = 0;
  4305. }
  4306. /*
  4307. * Build zonelists ordered by zone and nodes within zones.
  4308. * This results in conserving DMA zone[s] until all Normal memory is
  4309. * exhausted, but results in overflowing to remote node while memory
  4310. * may still exist in local DMA zone.
  4311. */
  4312. static void build_zonelists(pg_data_t *pgdat)
  4313. {
  4314. static int node_order[MAX_NUMNODES];
  4315. int node, load, nr_nodes = 0;
  4316. nodemask_t used_mask;
  4317. int local_node, prev_node;
  4318. /* NUMA-aware ordering of nodes */
  4319. local_node = pgdat->node_id;
  4320. load = nr_online_nodes;
  4321. prev_node = local_node;
  4322. nodes_clear(used_mask);
  4323. memset(node_order, 0, sizeof(node_order));
  4324. while ((node = find_next_best_node(local_node, &used_mask)) >= 0) {
  4325. /*
  4326. * We don't want to pressure a particular node.
  4327. * So adding penalty to the first node in same
  4328. * distance group to make it round-robin.
  4329. */
  4330. if (node_distance(local_node, node) !=
  4331. node_distance(local_node, prev_node))
  4332. node_load[node] = load;
  4333. node_order[nr_nodes++] = node;
  4334. prev_node = node;
  4335. load--;
  4336. }
  4337. build_zonelists_in_node_order(pgdat, node_order, nr_nodes);
  4338. build_thisnode_zonelists(pgdat);
  4339. }
  4340. #ifdef CONFIG_HAVE_MEMORYLESS_NODES
  4341. /*
  4342. * Return node id of node used for "local" allocations.
  4343. * I.e., first node id of first zone in arg node's generic zonelist.
  4344. * Used for initializing percpu 'numa_mem', which is used primarily
  4345. * for kernel allocations, so use GFP_KERNEL flags to locate zonelist.
  4346. */
  4347. int local_memory_node(int node)
  4348. {
  4349. struct zoneref *z;
  4350. z = first_zones_zonelist(node_zonelist(node, GFP_KERNEL),
  4351. gfp_zone(GFP_KERNEL),
  4352. NULL);
  4353. return z->zone->node;
  4354. }
  4355. #endif
  4356. static void setup_min_unmapped_ratio(void);
  4357. static void setup_min_slab_ratio(void);
  4358. #else /* CONFIG_NUMA */
  4359. static void build_zonelists(pg_data_t *pgdat)
  4360. {
  4361. int node, local_node;
  4362. struct zoneref *zonerefs;
  4363. int nr_zones;
  4364. local_node = pgdat->node_id;
  4365. zonerefs = pgdat->node_zonelists[ZONELIST_FALLBACK]._zonerefs;
  4366. nr_zones = build_zonerefs_node(pgdat, zonerefs);
  4367. zonerefs += nr_zones;
  4368. /*
  4369. * Now we build the zonelist so that it contains the zones
  4370. * of all the other nodes.
  4371. * We don't want to pressure a particular node, so when
  4372. * building the zones for node N, we make sure that the
  4373. * zones coming right after the local ones are those from
  4374. * node N+1 (modulo N)
  4375. */
  4376. for (node = local_node + 1; node < MAX_NUMNODES; node++) {
  4377. if (!node_online(node))
  4378. continue;
  4379. nr_zones = build_zonerefs_node(NODE_DATA(node), zonerefs);
  4380. zonerefs += nr_zones;
  4381. }
  4382. for (node = 0; node < local_node; node++) {
  4383. if (!node_online(node))
  4384. continue;
  4385. nr_zones = build_zonerefs_node(NODE_DATA(node), zonerefs);
  4386. zonerefs += nr_zones;
  4387. }
  4388. zonerefs->zone = NULL;
  4389. zonerefs->zone_idx = 0;
  4390. }
  4391. #endif /* CONFIG_NUMA */
  4392. /*
  4393. * Boot pageset table. One per cpu which is going to be used for all
  4394. * zones and all nodes. The parameters will be set in such a way
  4395. * that an item put on a list will immediately be handed over to
  4396. * the buddy list. This is safe since pageset manipulation is done
  4397. * with interrupts disabled.
  4398. *
  4399. * The boot_pagesets must be kept even after bootup is complete for
  4400. * unused processors and/or zones. They do play a role for bootstrapping
  4401. * hotplugged processors.
  4402. *
  4403. * zoneinfo_show() and maybe other functions do
  4404. * not check if the processor is online before following the pageset pointer.
  4405. * Other parts of the kernel may not check if the zone is available.
  4406. */
  4407. static void setup_pageset(struct per_cpu_pageset *p, unsigned long batch);
  4408. static DEFINE_PER_CPU(struct per_cpu_pageset, boot_pageset);
  4409. static DEFINE_PER_CPU(struct per_cpu_nodestat, boot_nodestats);
  4410. static void __build_all_zonelists(void *data)
  4411. {
  4412. int nid;
  4413. int __maybe_unused cpu;
  4414. pg_data_t *self = data;
  4415. static DEFINE_SPINLOCK(lock);
  4416. spin_lock(&lock);
  4417. #ifdef CONFIG_NUMA
  4418. memset(node_load, 0, sizeof(node_load));
  4419. #endif
  4420. /*
  4421. * This node is hotadded and no memory is yet present. So just
  4422. * building zonelists is fine - no need to touch other nodes.
  4423. */
  4424. if (self && !node_online(self->node_id)) {
  4425. build_zonelists(self);
  4426. } else {
  4427. for_each_online_node(nid) {
  4428. pg_data_t *pgdat = NODE_DATA(nid);
  4429. build_zonelists(pgdat);
  4430. }
  4431. #ifdef CONFIG_HAVE_MEMORYLESS_NODES
  4432. /*
  4433. * We now know the "local memory node" for each node--
  4434. * i.e., the node of the first zone in the generic zonelist.
  4435. * Set up numa_mem percpu variable for on-line cpus. During
  4436. * boot, only the boot cpu should be on-line; we'll init the
  4437. * secondary cpus' numa_mem as they come on-line. During
  4438. * node/memory hotplug, we'll fixup all on-line cpus.
  4439. */
  4440. for_each_online_cpu(cpu)
  4441. set_cpu_numa_mem(cpu, local_memory_node(cpu_to_node(cpu)));
  4442. #endif
  4443. }
  4444. spin_unlock(&lock);
  4445. }
  4446. static noinline void __init
  4447. build_all_zonelists_init(void)
  4448. {
  4449. int cpu;
  4450. __build_all_zonelists(NULL);
  4451. /*
  4452. * Initialize the boot_pagesets that are going to be used
  4453. * for bootstrapping processors. The real pagesets for
  4454. * each zone will be allocated later when the per cpu
  4455. * allocator is available.
  4456. *
  4457. * boot_pagesets are used also for bootstrapping offline
  4458. * cpus if the system is already booted because the pagesets
  4459. * are needed to initialize allocators on a specific cpu too.
  4460. * F.e. the percpu allocator needs the page allocator which
  4461. * needs the percpu allocator in order to allocate its pagesets
  4462. * (a chicken-egg dilemma).
  4463. */
  4464. for_each_possible_cpu(cpu)
  4465. setup_pageset(&per_cpu(boot_pageset, cpu), 0);
  4466. mminit_verify_zonelist();
  4467. cpuset_init_current_mems_allowed();
  4468. }
  4469. /*
  4470. * unless system_state == SYSTEM_BOOTING.
  4471. *
  4472. * __ref due to call of __init annotated helper build_all_zonelists_init
  4473. * [protected by SYSTEM_BOOTING].
  4474. */
  4475. void __ref build_all_zonelists(pg_data_t *pgdat)
  4476. {
  4477. if (system_state == SYSTEM_BOOTING) {
  4478. build_all_zonelists_init();
  4479. } else {
  4480. __build_all_zonelists(pgdat);
  4481. /* cpuset refresh routine should be here */
  4482. }
  4483. vm_total_pages = nr_free_pagecache_pages();
  4484. /*
  4485. * Disable grouping by mobility if the number of pages in the
  4486. * system is too low to allow the mechanism to work. It would be
  4487. * more accurate, but expensive to check per-zone. This check is
  4488. * made on memory-hotadd so a system can start with mobility
  4489. * disabled and enable it later
  4490. */
  4491. if (vm_total_pages < (pageblock_nr_pages * MIGRATE_TYPES))
  4492. page_group_by_mobility_disabled = 1;
  4493. else
  4494. page_group_by_mobility_disabled = 0;
  4495. pr_info("Built %i zonelists, mobility grouping %s. Total pages: %ld\n",
  4496. nr_online_nodes,
  4497. page_group_by_mobility_disabled ? "off" : "on",
  4498. vm_total_pages);
  4499. #ifdef CONFIG_NUMA
  4500. pr_info("Policy zone: %s\n", zone_names[policy_zone]);
  4501. #endif
  4502. }
  4503. /*
  4504. * Initially all pages are reserved - free ones are freed
  4505. * up by free_all_bootmem() once the early boot process is
  4506. * done. Non-atomic initialization, single-pass.
  4507. */
  4508. void __meminit memmap_init_zone(unsigned long size, int nid, unsigned long zone,
  4509. unsigned long start_pfn, enum memmap_context context)
  4510. {
  4511. struct vmem_altmap *altmap = to_vmem_altmap(__pfn_to_phys(start_pfn));
  4512. unsigned long end_pfn = start_pfn + size;
  4513. pg_data_t *pgdat = NODE_DATA(nid);
  4514. unsigned long pfn;
  4515. unsigned long nr_initialised = 0;
  4516. #ifdef CONFIG_HAVE_MEMBLOCK_NODE_MAP
  4517. struct memblock_region *r = NULL, *tmp;
  4518. #endif
  4519. if (highest_memmap_pfn < end_pfn - 1)
  4520. highest_memmap_pfn = end_pfn - 1;
  4521. /*
  4522. * Honor reservation requested by the driver for this ZONE_DEVICE
  4523. * memory
  4524. */
  4525. if (altmap && start_pfn == altmap->base_pfn)
  4526. start_pfn += altmap->reserve;
  4527. for (pfn = start_pfn; pfn < end_pfn; pfn++) {
  4528. /*
  4529. * There can be holes in boot-time mem_map[]s handed to this
  4530. * function. They do not exist on hotplugged memory.
  4531. */
  4532. if (context != MEMMAP_EARLY)
  4533. goto not_early;
  4534. if (!early_pfn_valid(pfn)) {
  4535. #ifdef CONFIG_HAVE_MEMBLOCK_NODE_MAP
  4536. /*
  4537. * Skip to the pfn preceding the next valid one (or
  4538. * end_pfn), such that we hit a valid pfn (or end_pfn)
  4539. * on our next iteration of the loop.
  4540. */
  4541. pfn = memblock_next_valid_pfn(pfn, end_pfn) - 1;
  4542. #endif
  4543. continue;
  4544. }
  4545. if (!early_pfn_in_nid(pfn, nid))
  4546. continue;
  4547. if (!update_defer_init(pgdat, pfn, end_pfn, &nr_initialised))
  4548. break;
  4549. #ifdef CONFIG_HAVE_MEMBLOCK_NODE_MAP
  4550. /*
  4551. * Check given memblock attribute by firmware which can affect
  4552. * kernel memory layout. If zone==ZONE_MOVABLE but memory is
  4553. * mirrored, it's an overlapped memmap init. skip it.
  4554. */
  4555. if (mirrored_kernelcore && zone == ZONE_MOVABLE) {
  4556. if (!r || pfn >= memblock_region_memory_end_pfn(r)) {
  4557. for_each_memblock(memory, tmp)
  4558. if (pfn < memblock_region_memory_end_pfn(tmp))
  4559. break;
  4560. r = tmp;
  4561. }
  4562. if (pfn >= memblock_region_memory_base_pfn(r) &&
  4563. memblock_is_mirror(r)) {
  4564. /* already initialized as NORMAL */
  4565. pfn = memblock_region_memory_end_pfn(r);
  4566. continue;
  4567. }
  4568. }
  4569. #endif
  4570. not_early:
  4571. /*
  4572. * Mark the block movable so that blocks are reserved for
  4573. * movable at startup. This will force kernel allocations
  4574. * to reserve their blocks rather than leaking throughout
  4575. * the address space during boot when many long-lived
  4576. * kernel allocations are made.
  4577. *
  4578. * bitmap is created for zone's valid pfn range. but memmap
  4579. * can be created for invalid pages (for alignment)
  4580. * check here not to call set_pageblock_migratetype() against
  4581. * pfn out of zone.
  4582. */
  4583. if (!(pfn & (pageblock_nr_pages - 1))) {
  4584. struct page *page = pfn_to_page(pfn);
  4585. __init_single_page(page, pfn, zone, nid);
  4586. set_pageblock_migratetype(page, MIGRATE_MOVABLE);
  4587. } else {
  4588. __init_single_pfn(pfn, zone, nid);
  4589. }
  4590. }
  4591. }
  4592. static void __meminit zone_init_free_lists(struct zone *zone)
  4593. {
  4594. unsigned int order, t;
  4595. for_each_migratetype_order(order, t) {
  4596. INIT_LIST_HEAD(&zone->free_area[order].free_list[t]);
  4597. zone->free_area[order].nr_free = 0;
  4598. }
  4599. }
  4600. #ifndef __HAVE_ARCH_MEMMAP_INIT
  4601. #define memmap_init(size, nid, zone, start_pfn) \
  4602. memmap_init_zone((size), (nid), (zone), (start_pfn), MEMMAP_EARLY)
  4603. #endif
  4604. static int zone_batchsize(struct zone *zone)
  4605. {
  4606. #ifdef CONFIG_MMU
  4607. int batch;
  4608. /*
  4609. * The per-cpu-pages pools are set to around 1000th of the
  4610. * size of the zone. But no more than 1/2 of a meg.
  4611. *
  4612. * OK, so we don't know how big the cache is. So guess.
  4613. */
  4614. batch = zone->managed_pages / 1024;
  4615. if (batch * PAGE_SIZE > 512 * 1024)
  4616. batch = (512 * 1024) / PAGE_SIZE;
  4617. batch /= 4; /* We effectively *= 4 below */
  4618. if (batch < 1)
  4619. batch = 1;
  4620. /*
  4621. * Clamp the batch to a 2^n - 1 value. Having a power
  4622. * of 2 value was found to be more likely to have
  4623. * suboptimal cache aliasing properties in some cases.
  4624. *
  4625. * For example if 2 tasks are alternately allocating
  4626. * batches of pages, one task can end up with a lot
  4627. * of pages of one half of the possible page colors
  4628. * and the other with pages of the other colors.
  4629. */
  4630. batch = rounddown_pow_of_two(batch + batch/2) - 1;
  4631. return batch;
  4632. #else
  4633. /* The deferral and batching of frees should be suppressed under NOMMU
  4634. * conditions.
  4635. *
  4636. * The problem is that NOMMU needs to be able to allocate large chunks
  4637. * of contiguous memory as there's no hardware page translation to
  4638. * assemble apparent contiguous memory from discontiguous pages.
  4639. *
  4640. * Queueing large contiguous runs of pages for batching, however,
  4641. * causes the pages to actually be freed in smaller chunks. As there
  4642. * can be a significant delay between the individual batches being
  4643. * recycled, this leads to the once large chunks of space being
  4644. * fragmented and becoming unavailable for high-order allocations.
  4645. */
  4646. return 0;
  4647. #endif
  4648. }
  4649. /*
  4650. * pcp->high and pcp->batch values are related and dependent on one another:
  4651. * ->batch must never be higher then ->high.
  4652. * The following function updates them in a safe manner without read side
  4653. * locking.
  4654. *
  4655. * Any new users of pcp->batch and pcp->high should ensure they can cope with
  4656. * those fields changing asynchronously (acording the the above rule).
  4657. *
  4658. * mutex_is_locked(&pcp_batch_high_lock) required when calling this function
  4659. * outside of boot time (or some other assurance that no concurrent updaters
  4660. * exist).
  4661. */
  4662. static void pageset_update(struct per_cpu_pages *pcp, unsigned long high,
  4663. unsigned long batch)
  4664. {
  4665. /* start with a fail safe value for batch */
  4666. pcp->batch = 1;
  4667. smp_wmb();
  4668. /* Update high, then batch, in order */
  4669. pcp->high = high;
  4670. smp_wmb();
  4671. pcp->batch = batch;
  4672. }
  4673. /* a companion to pageset_set_high() */
  4674. static void pageset_set_batch(struct per_cpu_pageset *p, unsigned long batch)
  4675. {
  4676. pageset_update(&p->pcp, 6 * batch, max(1UL, 1 * batch));
  4677. }
  4678. static void pageset_init(struct per_cpu_pageset *p)
  4679. {
  4680. struct per_cpu_pages *pcp;
  4681. int migratetype;
  4682. memset(p, 0, sizeof(*p));
  4683. pcp = &p->pcp;
  4684. pcp->count = 0;
  4685. for (migratetype = 0; migratetype < MIGRATE_PCPTYPES; migratetype++)
  4686. INIT_LIST_HEAD(&pcp->lists[migratetype]);
  4687. }
  4688. static void setup_pageset(struct per_cpu_pageset *p, unsigned long batch)
  4689. {
  4690. pageset_init(p);
  4691. pageset_set_batch(p, batch);
  4692. }
  4693. /*
  4694. * pageset_set_high() sets the high water mark for hot per_cpu_pagelist
  4695. * to the value high for the pageset p.
  4696. */
  4697. static void pageset_set_high(struct per_cpu_pageset *p,
  4698. unsigned long high)
  4699. {
  4700. unsigned long batch = max(1UL, high / 4);
  4701. if ((high / 4) > (PAGE_SHIFT * 8))
  4702. batch = PAGE_SHIFT * 8;
  4703. pageset_update(&p->pcp, high, batch);
  4704. }
  4705. static void pageset_set_high_and_batch(struct zone *zone,
  4706. struct per_cpu_pageset *pcp)
  4707. {
  4708. if (percpu_pagelist_fraction)
  4709. pageset_set_high(pcp,
  4710. (zone->managed_pages /
  4711. percpu_pagelist_fraction));
  4712. else
  4713. pageset_set_batch(pcp, zone_batchsize(zone));
  4714. }
  4715. static void __meminit zone_pageset_init(struct zone *zone, int cpu)
  4716. {
  4717. struct per_cpu_pageset *pcp = per_cpu_ptr(zone->pageset, cpu);
  4718. pageset_init(pcp);
  4719. pageset_set_high_and_batch(zone, pcp);
  4720. }
  4721. void __meminit setup_zone_pageset(struct zone *zone)
  4722. {
  4723. int cpu;
  4724. zone->pageset = alloc_percpu(struct per_cpu_pageset);
  4725. for_each_possible_cpu(cpu)
  4726. zone_pageset_init(zone, cpu);
  4727. }
  4728. /*
  4729. * Allocate per cpu pagesets and initialize them.
  4730. * Before this call only boot pagesets were available.
  4731. */
  4732. void __init setup_per_cpu_pageset(void)
  4733. {
  4734. struct pglist_data *pgdat;
  4735. struct zone *zone;
  4736. for_each_populated_zone(zone)
  4737. setup_zone_pageset(zone);
  4738. for_each_online_pgdat(pgdat)
  4739. pgdat->per_cpu_nodestats =
  4740. alloc_percpu(struct per_cpu_nodestat);
  4741. }
  4742. static __meminit void zone_pcp_init(struct zone *zone)
  4743. {
  4744. /*
  4745. * per cpu subsystem is not up at this point. The following code
  4746. * relies on the ability of the linker to provide the
  4747. * offset of a (static) per cpu variable into the per cpu area.
  4748. */
  4749. zone->pageset = &boot_pageset;
  4750. if (populated_zone(zone))
  4751. printk(KERN_DEBUG " %s zone: %lu pages, LIFO batch:%u\n",
  4752. zone->name, zone->present_pages,
  4753. zone_batchsize(zone));
  4754. }
  4755. void __meminit init_currently_empty_zone(struct zone *zone,
  4756. unsigned long zone_start_pfn,
  4757. unsigned long size)
  4758. {
  4759. struct pglist_data *pgdat = zone->zone_pgdat;
  4760. pgdat->nr_zones = zone_idx(zone) + 1;
  4761. zone->zone_start_pfn = zone_start_pfn;
  4762. mminit_dprintk(MMINIT_TRACE, "memmap_init",
  4763. "Initialising map node %d zone %lu pfns %lu -> %lu\n",
  4764. pgdat->node_id,
  4765. (unsigned long)zone_idx(zone),
  4766. zone_start_pfn, (zone_start_pfn + size));
  4767. zone_init_free_lists(zone);
  4768. zone->initialized = 1;
  4769. }
  4770. #ifdef CONFIG_HAVE_MEMBLOCK_NODE_MAP
  4771. #ifndef CONFIG_HAVE_ARCH_EARLY_PFN_TO_NID
  4772. /*
  4773. * Required by SPARSEMEM. Given a PFN, return what node the PFN is on.
  4774. */
  4775. int __meminit __early_pfn_to_nid(unsigned long pfn,
  4776. struct mminit_pfnnid_cache *state)
  4777. {
  4778. unsigned long start_pfn, end_pfn;
  4779. int nid;
  4780. if (state->last_start <= pfn && pfn < state->last_end)
  4781. return state->last_nid;
  4782. nid = memblock_search_pfn_nid(pfn, &start_pfn, &end_pfn);
  4783. if (nid != -1) {
  4784. state->last_start = start_pfn;
  4785. state->last_end = end_pfn;
  4786. state->last_nid = nid;
  4787. }
  4788. return nid;
  4789. }
  4790. #endif /* CONFIG_HAVE_ARCH_EARLY_PFN_TO_NID */
  4791. /**
  4792. * free_bootmem_with_active_regions - Call memblock_free_early_nid for each active range
  4793. * @nid: The node to free memory on. If MAX_NUMNODES, all nodes are freed.
  4794. * @max_low_pfn: The highest PFN that will be passed to memblock_free_early_nid
  4795. *
  4796. * If an architecture guarantees that all ranges registered contain no holes
  4797. * and may be freed, this this function may be used instead of calling
  4798. * memblock_free_early_nid() manually.
  4799. */
  4800. void __init free_bootmem_with_active_regions(int nid, unsigned long max_low_pfn)
  4801. {
  4802. unsigned long start_pfn, end_pfn;
  4803. int i, this_nid;
  4804. for_each_mem_pfn_range(i, nid, &start_pfn, &end_pfn, &this_nid) {
  4805. start_pfn = min(start_pfn, max_low_pfn);
  4806. end_pfn = min(end_pfn, max_low_pfn);
  4807. if (start_pfn < end_pfn)
  4808. memblock_free_early_nid(PFN_PHYS(start_pfn),
  4809. (end_pfn - start_pfn) << PAGE_SHIFT,
  4810. this_nid);
  4811. }
  4812. }
  4813. /**
  4814. * sparse_memory_present_with_active_regions - Call memory_present for each active range
  4815. * @nid: The node to call memory_present for. If MAX_NUMNODES, all nodes will be used.
  4816. *
  4817. * If an architecture guarantees that all ranges registered contain no holes and may
  4818. * be freed, this function may be used instead of calling memory_present() manually.
  4819. */
  4820. void __init sparse_memory_present_with_active_regions(int nid)
  4821. {
  4822. unsigned long start_pfn, end_pfn;
  4823. int i, this_nid;
  4824. for_each_mem_pfn_range(i, nid, &start_pfn, &end_pfn, &this_nid)
  4825. memory_present(this_nid, start_pfn, end_pfn);
  4826. }
  4827. /**
  4828. * get_pfn_range_for_nid - Return the start and end page frames for a node
  4829. * @nid: The nid to return the range for. If MAX_NUMNODES, the min and max PFN are returned.
  4830. * @start_pfn: Passed by reference. On return, it will have the node start_pfn.
  4831. * @end_pfn: Passed by reference. On return, it will have the node end_pfn.
  4832. *
  4833. * It returns the start and end page frame of a node based on information
  4834. * provided by memblock_set_node(). If called for a node
  4835. * with no available memory, a warning is printed and the start and end
  4836. * PFNs will be 0.
  4837. */
  4838. void __meminit get_pfn_range_for_nid(unsigned int nid,
  4839. unsigned long *start_pfn, unsigned long *end_pfn)
  4840. {
  4841. unsigned long this_start_pfn, this_end_pfn;
  4842. int i;
  4843. *start_pfn = -1UL;
  4844. *end_pfn = 0;
  4845. for_each_mem_pfn_range(i, nid, &this_start_pfn, &this_end_pfn, NULL) {
  4846. *start_pfn = min(*start_pfn, this_start_pfn);
  4847. *end_pfn = max(*end_pfn, this_end_pfn);
  4848. }
  4849. if (*start_pfn == -1UL)
  4850. *start_pfn = 0;
  4851. }
  4852. /*
  4853. * This finds a zone that can be used for ZONE_MOVABLE pages. The
  4854. * assumption is made that zones within a node are ordered in monotonic
  4855. * increasing memory addresses so that the "highest" populated zone is used
  4856. */
  4857. static void __init find_usable_zone_for_movable(void)
  4858. {
  4859. int zone_index;
  4860. for (zone_index = MAX_NR_ZONES - 1; zone_index >= 0; zone_index--) {
  4861. if (zone_index == ZONE_MOVABLE)
  4862. continue;
  4863. if (arch_zone_highest_possible_pfn[zone_index] >
  4864. arch_zone_lowest_possible_pfn[zone_index])
  4865. break;
  4866. }
  4867. VM_BUG_ON(zone_index == -1);
  4868. movable_zone = zone_index;
  4869. }
  4870. /*
  4871. * The zone ranges provided by the architecture do not include ZONE_MOVABLE
  4872. * because it is sized independent of architecture. Unlike the other zones,
  4873. * the starting point for ZONE_MOVABLE is not fixed. It may be different
  4874. * in each node depending on the size of each node and how evenly kernelcore
  4875. * is distributed. This helper function adjusts the zone ranges
  4876. * provided by the architecture for a given node by using the end of the
  4877. * highest usable zone for ZONE_MOVABLE. This preserves the assumption that
  4878. * zones within a node are in order of monotonic increases memory addresses
  4879. */
  4880. static void __meminit adjust_zone_range_for_zone_movable(int nid,
  4881. unsigned long zone_type,
  4882. unsigned long node_start_pfn,
  4883. unsigned long node_end_pfn,
  4884. unsigned long *zone_start_pfn,
  4885. unsigned long *zone_end_pfn)
  4886. {
  4887. /* Only adjust if ZONE_MOVABLE is on this node */
  4888. if (zone_movable_pfn[nid]) {
  4889. /* Size ZONE_MOVABLE */
  4890. if (zone_type == ZONE_MOVABLE) {
  4891. *zone_start_pfn = zone_movable_pfn[nid];
  4892. *zone_end_pfn = min(node_end_pfn,
  4893. arch_zone_highest_possible_pfn[movable_zone]);
  4894. /* Adjust for ZONE_MOVABLE starting within this range */
  4895. } else if (!mirrored_kernelcore &&
  4896. *zone_start_pfn < zone_movable_pfn[nid] &&
  4897. *zone_end_pfn > zone_movable_pfn[nid]) {
  4898. *zone_end_pfn = zone_movable_pfn[nid];
  4899. /* Check if this whole range is within ZONE_MOVABLE */
  4900. } else if (*zone_start_pfn >= zone_movable_pfn[nid])
  4901. *zone_start_pfn = *zone_end_pfn;
  4902. }
  4903. }
  4904. /*
  4905. * Return the number of pages a zone spans in a node, including holes
  4906. * present_pages = zone_spanned_pages_in_node() - zone_absent_pages_in_node()
  4907. */
  4908. static unsigned long __meminit zone_spanned_pages_in_node(int nid,
  4909. unsigned long zone_type,
  4910. unsigned long node_start_pfn,
  4911. unsigned long node_end_pfn,
  4912. unsigned long *zone_start_pfn,
  4913. unsigned long *zone_end_pfn,
  4914. unsigned long *ignored)
  4915. {
  4916. /* When hotadd a new node from cpu_up(), the node should be empty */
  4917. if (!node_start_pfn && !node_end_pfn)
  4918. return 0;
  4919. /* Get the start and end of the zone */
  4920. *zone_start_pfn = arch_zone_lowest_possible_pfn[zone_type];
  4921. *zone_end_pfn = arch_zone_highest_possible_pfn[zone_type];
  4922. adjust_zone_range_for_zone_movable(nid, zone_type,
  4923. node_start_pfn, node_end_pfn,
  4924. zone_start_pfn, zone_end_pfn);
  4925. /* Check that this node has pages within the zone's required range */
  4926. if (*zone_end_pfn < node_start_pfn || *zone_start_pfn > node_end_pfn)
  4927. return 0;
  4928. /* Move the zone boundaries inside the node if necessary */
  4929. *zone_end_pfn = min(*zone_end_pfn, node_end_pfn);
  4930. *zone_start_pfn = max(*zone_start_pfn, node_start_pfn);
  4931. /* Return the spanned pages */
  4932. return *zone_end_pfn - *zone_start_pfn;
  4933. }
  4934. /*
  4935. * Return the number of holes in a range on a node. If nid is MAX_NUMNODES,
  4936. * then all holes in the requested range will be accounted for.
  4937. */
  4938. unsigned long __meminit __absent_pages_in_range(int nid,
  4939. unsigned long range_start_pfn,
  4940. unsigned long range_end_pfn)
  4941. {
  4942. unsigned long nr_absent = range_end_pfn - range_start_pfn;
  4943. unsigned long start_pfn, end_pfn;
  4944. int i;
  4945. for_each_mem_pfn_range(i, nid, &start_pfn, &end_pfn, NULL) {
  4946. start_pfn = clamp(start_pfn, range_start_pfn, range_end_pfn);
  4947. end_pfn = clamp(end_pfn, range_start_pfn, range_end_pfn);
  4948. nr_absent -= end_pfn - start_pfn;
  4949. }
  4950. return nr_absent;
  4951. }
  4952. /**
  4953. * absent_pages_in_range - Return number of page frames in holes within a range
  4954. * @start_pfn: The start PFN to start searching for holes
  4955. * @end_pfn: The end PFN to stop searching for holes
  4956. *
  4957. * It returns the number of pages frames in memory holes within a range.
  4958. */
  4959. unsigned long __init absent_pages_in_range(unsigned long start_pfn,
  4960. unsigned long end_pfn)
  4961. {
  4962. return __absent_pages_in_range(MAX_NUMNODES, start_pfn, end_pfn);
  4963. }
  4964. /* Return the number of page frames in holes in a zone on a node */
  4965. static unsigned long __meminit zone_absent_pages_in_node(int nid,
  4966. unsigned long zone_type,
  4967. unsigned long node_start_pfn,
  4968. unsigned long node_end_pfn,
  4969. unsigned long *ignored)
  4970. {
  4971. unsigned long zone_low = arch_zone_lowest_possible_pfn[zone_type];
  4972. unsigned long zone_high = arch_zone_highest_possible_pfn[zone_type];
  4973. unsigned long zone_start_pfn, zone_end_pfn;
  4974. unsigned long nr_absent;
  4975. /* When hotadd a new node from cpu_up(), the node should be empty */
  4976. if (!node_start_pfn && !node_end_pfn)
  4977. return 0;
  4978. zone_start_pfn = clamp(node_start_pfn, zone_low, zone_high);
  4979. zone_end_pfn = clamp(node_end_pfn, zone_low, zone_high);
  4980. adjust_zone_range_for_zone_movable(nid, zone_type,
  4981. node_start_pfn, node_end_pfn,
  4982. &zone_start_pfn, &zone_end_pfn);
  4983. nr_absent = __absent_pages_in_range(nid, zone_start_pfn, zone_end_pfn);
  4984. /*
  4985. * ZONE_MOVABLE handling.
  4986. * Treat pages to be ZONE_MOVABLE in ZONE_NORMAL as absent pages
  4987. * and vice versa.
  4988. */
  4989. if (mirrored_kernelcore && zone_movable_pfn[nid]) {
  4990. unsigned long start_pfn, end_pfn;
  4991. struct memblock_region *r;
  4992. for_each_memblock(memory, r) {
  4993. start_pfn = clamp(memblock_region_memory_base_pfn(r),
  4994. zone_start_pfn, zone_end_pfn);
  4995. end_pfn = clamp(memblock_region_memory_end_pfn(r),
  4996. zone_start_pfn, zone_end_pfn);
  4997. if (zone_type == ZONE_MOVABLE &&
  4998. memblock_is_mirror(r))
  4999. nr_absent += end_pfn - start_pfn;
  5000. if (zone_type == ZONE_NORMAL &&
  5001. !memblock_is_mirror(r))
  5002. nr_absent += end_pfn - start_pfn;
  5003. }
  5004. }
  5005. return nr_absent;
  5006. }
  5007. #else /* CONFIG_HAVE_MEMBLOCK_NODE_MAP */
  5008. static inline unsigned long __meminit zone_spanned_pages_in_node(int nid,
  5009. unsigned long zone_type,
  5010. unsigned long node_start_pfn,
  5011. unsigned long node_end_pfn,
  5012. unsigned long *zone_start_pfn,
  5013. unsigned long *zone_end_pfn,
  5014. unsigned long *zones_size)
  5015. {
  5016. unsigned int zone;
  5017. *zone_start_pfn = node_start_pfn;
  5018. for (zone = 0; zone < zone_type; zone++)
  5019. *zone_start_pfn += zones_size[zone];
  5020. *zone_end_pfn = *zone_start_pfn + zones_size[zone_type];
  5021. return zones_size[zone_type];
  5022. }
  5023. static inline unsigned long __meminit zone_absent_pages_in_node(int nid,
  5024. unsigned long zone_type,
  5025. unsigned long node_start_pfn,
  5026. unsigned long node_end_pfn,
  5027. unsigned long *zholes_size)
  5028. {
  5029. if (!zholes_size)
  5030. return 0;
  5031. return zholes_size[zone_type];
  5032. }
  5033. #endif /* CONFIG_HAVE_MEMBLOCK_NODE_MAP */
  5034. static void __meminit calculate_node_totalpages(struct pglist_data *pgdat,
  5035. unsigned long node_start_pfn,
  5036. unsigned long node_end_pfn,
  5037. unsigned long *zones_size,
  5038. unsigned long *zholes_size)
  5039. {
  5040. unsigned long realtotalpages = 0, totalpages = 0;
  5041. enum zone_type i;
  5042. for (i = 0; i < MAX_NR_ZONES; i++) {
  5043. struct zone *zone = pgdat->node_zones + i;
  5044. unsigned long zone_start_pfn, zone_end_pfn;
  5045. unsigned long size, real_size;
  5046. size = zone_spanned_pages_in_node(pgdat->node_id, i,
  5047. node_start_pfn,
  5048. node_end_pfn,
  5049. &zone_start_pfn,
  5050. &zone_end_pfn,
  5051. zones_size);
  5052. real_size = size - zone_absent_pages_in_node(pgdat->node_id, i,
  5053. node_start_pfn, node_end_pfn,
  5054. zholes_size);
  5055. if (size)
  5056. zone->zone_start_pfn = zone_start_pfn;
  5057. else
  5058. zone->zone_start_pfn = 0;
  5059. zone->spanned_pages = size;
  5060. zone->present_pages = real_size;
  5061. totalpages += size;
  5062. realtotalpages += real_size;
  5063. }
  5064. pgdat->node_spanned_pages = totalpages;
  5065. pgdat->node_present_pages = realtotalpages;
  5066. printk(KERN_DEBUG "On node %d totalpages: %lu\n", pgdat->node_id,
  5067. realtotalpages);
  5068. }
  5069. #ifndef CONFIG_SPARSEMEM
  5070. /*
  5071. * Calculate the size of the zone->blockflags rounded to an unsigned long
  5072. * Start by making sure zonesize is a multiple of pageblock_order by rounding
  5073. * up. Then use 1 NR_PAGEBLOCK_BITS worth of bits per pageblock, finally
  5074. * round what is now in bits to nearest long in bits, then return it in
  5075. * bytes.
  5076. */
  5077. static unsigned long __init usemap_size(unsigned long zone_start_pfn, unsigned long zonesize)
  5078. {
  5079. unsigned long usemapsize;
  5080. zonesize += zone_start_pfn & (pageblock_nr_pages-1);
  5081. usemapsize = roundup(zonesize, pageblock_nr_pages);
  5082. usemapsize = usemapsize >> pageblock_order;
  5083. usemapsize *= NR_PAGEBLOCK_BITS;
  5084. usemapsize = roundup(usemapsize, 8 * sizeof(unsigned long));
  5085. return usemapsize / 8;
  5086. }
  5087. static void __init setup_usemap(struct pglist_data *pgdat,
  5088. struct zone *zone,
  5089. unsigned long zone_start_pfn,
  5090. unsigned long zonesize)
  5091. {
  5092. unsigned long usemapsize = usemap_size(zone_start_pfn, zonesize);
  5093. zone->pageblock_flags = NULL;
  5094. if (usemapsize)
  5095. zone->pageblock_flags =
  5096. memblock_virt_alloc_node_nopanic(usemapsize,
  5097. pgdat->node_id);
  5098. }
  5099. #else
  5100. static inline void setup_usemap(struct pglist_data *pgdat, struct zone *zone,
  5101. unsigned long zone_start_pfn, unsigned long zonesize) {}
  5102. #endif /* CONFIG_SPARSEMEM */
  5103. #ifdef CONFIG_HUGETLB_PAGE_SIZE_VARIABLE
  5104. /* Initialise the number of pages represented by NR_PAGEBLOCK_BITS */
  5105. void __paginginit set_pageblock_order(void)
  5106. {
  5107. unsigned int order;
  5108. /* Check that pageblock_nr_pages has not already been setup */
  5109. if (pageblock_order)
  5110. return;
  5111. if (HPAGE_SHIFT > PAGE_SHIFT)
  5112. order = HUGETLB_PAGE_ORDER;
  5113. else
  5114. order = MAX_ORDER - 1;
  5115. /*
  5116. * Assume the largest contiguous order of interest is a huge page.
  5117. * This value may be variable depending on boot parameters on IA64 and
  5118. * powerpc.
  5119. */
  5120. pageblock_order = order;
  5121. }
  5122. #else /* CONFIG_HUGETLB_PAGE_SIZE_VARIABLE */
  5123. /*
  5124. * When CONFIG_HUGETLB_PAGE_SIZE_VARIABLE is not set, set_pageblock_order()
  5125. * is unused as pageblock_order is set at compile-time. See
  5126. * include/linux/pageblock-flags.h for the values of pageblock_order based on
  5127. * the kernel config
  5128. */
  5129. void __paginginit set_pageblock_order(void)
  5130. {
  5131. }
  5132. #endif /* CONFIG_HUGETLB_PAGE_SIZE_VARIABLE */
  5133. static unsigned long __paginginit calc_memmap_size(unsigned long spanned_pages,
  5134. unsigned long present_pages)
  5135. {
  5136. unsigned long pages = spanned_pages;
  5137. /*
  5138. * Provide a more accurate estimation if there are holes within
  5139. * the zone and SPARSEMEM is in use. If there are holes within the
  5140. * zone, each populated memory region may cost us one or two extra
  5141. * memmap pages due to alignment because memmap pages for each
  5142. * populated regions may not be naturally aligned on page boundary.
  5143. * So the (present_pages >> 4) heuristic is a tradeoff for that.
  5144. */
  5145. if (spanned_pages > present_pages + (present_pages >> 4) &&
  5146. IS_ENABLED(CONFIG_SPARSEMEM))
  5147. pages = present_pages;
  5148. return PAGE_ALIGN(pages * sizeof(struct page)) >> PAGE_SHIFT;
  5149. }
  5150. /*
  5151. * Set up the zone data structures:
  5152. * - mark all pages reserved
  5153. * - mark all memory queues empty
  5154. * - clear the memory bitmaps
  5155. *
  5156. * NOTE: pgdat should get zeroed by caller.
  5157. */
  5158. static void __paginginit free_area_init_core(struct pglist_data *pgdat)
  5159. {
  5160. enum zone_type j;
  5161. int nid = pgdat->node_id;
  5162. pgdat_resize_init(pgdat);
  5163. #ifdef CONFIG_NUMA_BALANCING
  5164. spin_lock_init(&pgdat->numabalancing_migrate_lock);
  5165. pgdat->numabalancing_migrate_nr_pages = 0;
  5166. pgdat->numabalancing_migrate_next_window = jiffies;
  5167. #endif
  5168. #ifdef CONFIG_TRANSPARENT_HUGEPAGE
  5169. spin_lock_init(&pgdat->split_queue_lock);
  5170. INIT_LIST_HEAD(&pgdat->split_queue);
  5171. pgdat->split_queue_len = 0;
  5172. #endif
  5173. init_waitqueue_head(&pgdat->kswapd_wait);
  5174. init_waitqueue_head(&pgdat->pfmemalloc_wait);
  5175. #ifdef CONFIG_COMPACTION
  5176. init_waitqueue_head(&pgdat->kcompactd_wait);
  5177. #endif
  5178. pgdat_page_ext_init(pgdat);
  5179. spin_lock_init(&pgdat->lru_lock);
  5180. lruvec_init(node_lruvec(pgdat));
  5181. pgdat->per_cpu_nodestats = &boot_nodestats;
  5182. for (j = 0; j < MAX_NR_ZONES; j++) {
  5183. struct zone *zone = pgdat->node_zones + j;
  5184. unsigned long size, realsize, freesize, memmap_pages;
  5185. unsigned long zone_start_pfn = zone->zone_start_pfn;
  5186. size = zone->spanned_pages;
  5187. realsize = freesize = zone->present_pages;
  5188. /*
  5189. * Adjust freesize so that it accounts for how much memory
  5190. * is used by this zone for memmap. This affects the watermark
  5191. * and per-cpu initialisations
  5192. */
  5193. memmap_pages = calc_memmap_size(size, realsize);
  5194. if (!is_highmem_idx(j)) {
  5195. if (freesize >= memmap_pages) {
  5196. freesize -= memmap_pages;
  5197. if (memmap_pages)
  5198. printk(KERN_DEBUG
  5199. " %s zone: %lu pages used for memmap\n",
  5200. zone_names[j], memmap_pages);
  5201. } else
  5202. pr_warn(" %s zone: %lu pages exceeds freesize %lu\n",
  5203. zone_names[j], memmap_pages, freesize);
  5204. }
  5205. /* Account for reserved pages */
  5206. if (j == 0 && freesize > dma_reserve) {
  5207. freesize -= dma_reserve;
  5208. printk(KERN_DEBUG " %s zone: %lu pages reserved\n",
  5209. zone_names[0], dma_reserve);
  5210. }
  5211. if (!is_highmem_idx(j))
  5212. nr_kernel_pages += freesize;
  5213. /* Charge for highmem memmap if there are enough kernel pages */
  5214. else if (nr_kernel_pages > memmap_pages * 2)
  5215. nr_kernel_pages -= memmap_pages;
  5216. nr_all_pages += freesize;
  5217. /*
  5218. * Set an approximate value for lowmem here, it will be adjusted
  5219. * when the bootmem allocator frees pages into the buddy system.
  5220. * And all highmem pages will be managed by the buddy system.
  5221. */
  5222. zone->managed_pages = is_highmem_idx(j) ? realsize : freesize;
  5223. #ifdef CONFIG_NUMA
  5224. zone->node = nid;
  5225. #endif
  5226. zone->name = zone_names[j];
  5227. zone->zone_pgdat = pgdat;
  5228. spin_lock_init(&zone->lock);
  5229. zone_seqlock_init(zone);
  5230. zone_pcp_init(zone);
  5231. if (!size)
  5232. continue;
  5233. set_pageblock_order();
  5234. setup_usemap(pgdat, zone, zone_start_pfn, size);
  5235. init_currently_empty_zone(zone, zone_start_pfn, size);
  5236. memmap_init(size, nid, j, zone_start_pfn);
  5237. }
  5238. }
  5239. static void __ref alloc_node_mem_map(struct pglist_data *pgdat)
  5240. {
  5241. unsigned long __maybe_unused start = 0;
  5242. unsigned long __maybe_unused offset = 0;
  5243. /* Skip empty nodes */
  5244. if (!pgdat->node_spanned_pages)
  5245. return;
  5246. #ifdef CONFIG_FLAT_NODE_MEM_MAP
  5247. start = pgdat->node_start_pfn & ~(MAX_ORDER_NR_PAGES - 1);
  5248. offset = pgdat->node_start_pfn - start;
  5249. /* ia64 gets its own node_mem_map, before this, without bootmem */
  5250. if (!pgdat->node_mem_map) {
  5251. unsigned long size, end;
  5252. struct page *map;
  5253. /*
  5254. * The zone's endpoints aren't required to be MAX_ORDER
  5255. * aligned but the node_mem_map endpoints must be in order
  5256. * for the buddy allocator to function correctly.
  5257. */
  5258. end = pgdat_end_pfn(pgdat);
  5259. end = ALIGN(end, MAX_ORDER_NR_PAGES);
  5260. size = (end - start) * sizeof(struct page);
  5261. map = alloc_remap(pgdat->node_id, size);
  5262. if (!map)
  5263. map = memblock_virt_alloc_node_nopanic(size,
  5264. pgdat->node_id);
  5265. pgdat->node_mem_map = map + offset;
  5266. }
  5267. #ifndef CONFIG_NEED_MULTIPLE_NODES
  5268. /*
  5269. * With no DISCONTIG, the global mem_map is just set as node 0's
  5270. */
  5271. if (pgdat == NODE_DATA(0)) {
  5272. mem_map = NODE_DATA(0)->node_mem_map;
  5273. #if defined(CONFIG_HAVE_MEMBLOCK_NODE_MAP) || defined(CONFIG_FLATMEM)
  5274. if (page_to_pfn(mem_map) != pgdat->node_start_pfn)
  5275. mem_map -= offset;
  5276. #endif /* CONFIG_HAVE_MEMBLOCK_NODE_MAP */
  5277. }
  5278. #endif
  5279. #endif /* CONFIG_FLAT_NODE_MEM_MAP */
  5280. }
  5281. void __paginginit free_area_init_node(int nid, unsigned long *zones_size,
  5282. unsigned long node_start_pfn, unsigned long *zholes_size)
  5283. {
  5284. pg_data_t *pgdat = NODE_DATA(nid);
  5285. unsigned long start_pfn = 0;
  5286. unsigned long end_pfn = 0;
  5287. /* pg_data_t should be reset to zero when it's allocated */
  5288. WARN_ON(pgdat->nr_zones || pgdat->kswapd_classzone_idx);
  5289. pgdat->node_id = nid;
  5290. pgdat->node_start_pfn = node_start_pfn;
  5291. pgdat->per_cpu_nodestats = NULL;
  5292. #ifdef CONFIG_HAVE_MEMBLOCK_NODE_MAP
  5293. get_pfn_range_for_nid(nid, &start_pfn, &end_pfn);
  5294. pr_info("Initmem setup node %d [mem %#018Lx-%#018Lx]\n", nid,
  5295. (u64)start_pfn << PAGE_SHIFT,
  5296. end_pfn ? ((u64)end_pfn << PAGE_SHIFT) - 1 : 0);
  5297. #else
  5298. start_pfn = node_start_pfn;
  5299. #endif
  5300. calculate_node_totalpages(pgdat, start_pfn, end_pfn,
  5301. zones_size, zholes_size);
  5302. alloc_node_mem_map(pgdat);
  5303. #ifdef CONFIG_FLAT_NODE_MEM_MAP
  5304. printk(KERN_DEBUG "free_area_init_node: node %d, pgdat %08lx, node_mem_map %08lx\n",
  5305. nid, (unsigned long)pgdat,
  5306. (unsigned long)pgdat->node_mem_map);
  5307. #endif
  5308. reset_deferred_meminit(pgdat);
  5309. free_area_init_core(pgdat);
  5310. }
  5311. #ifdef CONFIG_HAVE_MEMBLOCK_NODE_MAP
  5312. #if MAX_NUMNODES > 1
  5313. /*
  5314. * Figure out the number of possible node ids.
  5315. */
  5316. void __init setup_nr_node_ids(void)
  5317. {
  5318. unsigned int highest;
  5319. highest = find_last_bit(node_possible_map.bits, MAX_NUMNODES);
  5320. nr_node_ids = highest + 1;
  5321. }
  5322. #endif
  5323. /**
  5324. * node_map_pfn_alignment - determine the maximum internode alignment
  5325. *
  5326. * This function should be called after node map is populated and sorted.
  5327. * It calculates the maximum power of two alignment which can distinguish
  5328. * all the nodes.
  5329. *
  5330. * For example, if all nodes are 1GiB and aligned to 1GiB, the return value
  5331. * would indicate 1GiB alignment with (1 << (30 - PAGE_SHIFT)). If the
  5332. * nodes are shifted by 256MiB, 256MiB. Note that if only the last node is
  5333. * shifted, 1GiB is enough and this function will indicate so.
  5334. *
  5335. * This is used to test whether pfn -> nid mapping of the chosen memory
  5336. * model has fine enough granularity to avoid incorrect mapping for the
  5337. * populated node map.
  5338. *
  5339. * Returns the determined alignment in pfn's. 0 if there is no alignment
  5340. * requirement (single node).
  5341. */
  5342. unsigned long __init node_map_pfn_alignment(void)
  5343. {
  5344. unsigned long accl_mask = 0, last_end = 0;
  5345. unsigned long start, end, mask;
  5346. int last_nid = -1;
  5347. int i, nid;
  5348. for_each_mem_pfn_range(i, MAX_NUMNODES, &start, &end, &nid) {
  5349. if (!start || last_nid < 0 || last_nid == nid) {
  5350. last_nid = nid;
  5351. last_end = end;
  5352. continue;
  5353. }
  5354. /*
  5355. * Start with a mask granular enough to pin-point to the
  5356. * start pfn and tick off bits one-by-one until it becomes
  5357. * too coarse to separate the current node from the last.
  5358. */
  5359. mask = ~((1 << __ffs(start)) - 1);
  5360. while (mask && last_end <= (start & (mask << 1)))
  5361. mask <<= 1;
  5362. /* accumulate all internode masks */
  5363. accl_mask |= mask;
  5364. }
  5365. /* convert mask to number of pages */
  5366. return ~accl_mask + 1;
  5367. }
  5368. /* Find the lowest pfn for a node */
  5369. static unsigned long __init find_min_pfn_for_node(int nid)
  5370. {
  5371. unsigned long min_pfn = ULONG_MAX;
  5372. unsigned long start_pfn;
  5373. int i;
  5374. for_each_mem_pfn_range(i, nid, &start_pfn, NULL, NULL)
  5375. min_pfn = min(min_pfn, start_pfn);
  5376. if (min_pfn == ULONG_MAX) {
  5377. pr_warn("Could not find start_pfn for node %d\n", nid);
  5378. return 0;
  5379. }
  5380. return min_pfn;
  5381. }
  5382. /**
  5383. * find_min_pfn_with_active_regions - Find the minimum PFN registered
  5384. *
  5385. * It returns the minimum PFN based on information provided via
  5386. * memblock_set_node().
  5387. */
  5388. unsigned long __init find_min_pfn_with_active_regions(void)
  5389. {
  5390. return find_min_pfn_for_node(MAX_NUMNODES);
  5391. }
  5392. /*
  5393. * early_calculate_totalpages()
  5394. * Sum pages in active regions for movable zone.
  5395. * Populate N_MEMORY for calculating usable_nodes.
  5396. */
  5397. static unsigned long __init early_calculate_totalpages(void)
  5398. {
  5399. unsigned long totalpages = 0;
  5400. unsigned long start_pfn, end_pfn;
  5401. int i, nid;
  5402. for_each_mem_pfn_range(i, MAX_NUMNODES, &start_pfn, &end_pfn, &nid) {
  5403. unsigned long pages = end_pfn - start_pfn;
  5404. totalpages += pages;
  5405. if (pages)
  5406. node_set_state(nid, N_MEMORY);
  5407. }
  5408. return totalpages;
  5409. }
  5410. /*
  5411. * Find the PFN the Movable zone begins in each node. Kernel memory
  5412. * is spread evenly between nodes as long as the nodes have enough
  5413. * memory. When they don't, some nodes will have more kernelcore than
  5414. * others
  5415. */
  5416. static void __init find_zone_movable_pfns_for_nodes(void)
  5417. {
  5418. int i, nid;
  5419. unsigned long usable_startpfn;
  5420. unsigned long kernelcore_node, kernelcore_remaining;
  5421. /* save the state before borrow the nodemask */
  5422. nodemask_t saved_node_state = node_states[N_MEMORY];
  5423. unsigned long totalpages = early_calculate_totalpages();
  5424. int usable_nodes = nodes_weight(node_states[N_MEMORY]);
  5425. struct memblock_region *r;
  5426. /* Need to find movable_zone earlier when movable_node is specified. */
  5427. find_usable_zone_for_movable();
  5428. /*
  5429. * If movable_node is specified, ignore kernelcore and movablecore
  5430. * options.
  5431. */
  5432. if (movable_node_is_enabled()) {
  5433. for_each_memblock(memory, r) {
  5434. if (!memblock_is_hotpluggable(r))
  5435. continue;
  5436. nid = r->nid;
  5437. usable_startpfn = PFN_DOWN(r->base);
  5438. zone_movable_pfn[nid] = zone_movable_pfn[nid] ?
  5439. min(usable_startpfn, zone_movable_pfn[nid]) :
  5440. usable_startpfn;
  5441. }
  5442. goto out2;
  5443. }
  5444. /*
  5445. * If kernelcore=mirror is specified, ignore movablecore option
  5446. */
  5447. if (mirrored_kernelcore) {
  5448. bool mem_below_4gb_not_mirrored = false;
  5449. for_each_memblock(memory, r) {
  5450. if (memblock_is_mirror(r))
  5451. continue;
  5452. nid = r->nid;
  5453. usable_startpfn = memblock_region_memory_base_pfn(r);
  5454. if (usable_startpfn < 0x100000) {
  5455. mem_below_4gb_not_mirrored = true;
  5456. continue;
  5457. }
  5458. zone_movable_pfn[nid] = zone_movable_pfn[nid] ?
  5459. min(usable_startpfn, zone_movable_pfn[nid]) :
  5460. usable_startpfn;
  5461. }
  5462. if (mem_below_4gb_not_mirrored)
  5463. pr_warn("This configuration results in unmirrored kernel memory.");
  5464. goto out2;
  5465. }
  5466. /*
  5467. * If movablecore=nn[KMG] was specified, calculate what size of
  5468. * kernelcore that corresponds so that memory usable for
  5469. * any allocation type is evenly spread. If both kernelcore
  5470. * and movablecore are specified, then the value of kernelcore
  5471. * will be used for required_kernelcore if it's greater than
  5472. * what movablecore would have allowed.
  5473. */
  5474. if (required_movablecore) {
  5475. unsigned long corepages;
  5476. /*
  5477. * Round-up so that ZONE_MOVABLE is at least as large as what
  5478. * was requested by the user
  5479. */
  5480. required_movablecore =
  5481. roundup(required_movablecore, MAX_ORDER_NR_PAGES);
  5482. required_movablecore = min(totalpages, required_movablecore);
  5483. corepages = totalpages - required_movablecore;
  5484. required_kernelcore = max(required_kernelcore, corepages);
  5485. }
  5486. /*
  5487. * If kernelcore was not specified or kernelcore size is larger
  5488. * than totalpages, there is no ZONE_MOVABLE.
  5489. */
  5490. if (!required_kernelcore || required_kernelcore >= totalpages)
  5491. goto out;
  5492. /* usable_startpfn is the lowest possible pfn ZONE_MOVABLE can be at */
  5493. usable_startpfn = arch_zone_lowest_possible_pfn[movable_zone];
  5494. restart:
  5495. /* Spread kernelcore memory as evenly as possible throughout nodes */
  5496. kernelcore_node = required_kernelcore / usable_nodes;
  5497. for_each_node_state(nid, N_MEMORY) {
  5498. unsigned long start_pfn, end_pfn;
  5499. /*
  5500. * Recalculate kernelcore_node if the division per node
  5501. * now exceeds what is necessary to satisfy the requested
  5502. * amount of memory for the kernel
  5503. */
  5504. if (required_kernelcore < kernelcore_node)
  5505. kernelcore_node = required_kernelcore / usable_nodes;
  5506. /*
  5507. * As the map is walked, we track how much memory is usable
  5508. * by the kernel using kernelcore_remaining. When it is
  5509. * 0, the rest of the node is usable by ZONE_MOVABLE
  5510. */
  5511. kernelcore_remaining = kernelcore_node;
  5512. /* Go through each range of PFNs within this node */
  5513. for_each_mem_pfn_range(i, nid, &start_pfn, &end_pfn, NULL) {
  5514. unsigned long size_pages;
  5515. start_pfn = max(start_pfn, zone_movable_pfn[nid]);
  5516. if (start_pfn >= end_pfn)
  5517. continue;
  5518. /* Account for what is only usable for kernelcore */
  5519. if (start_pfn < usable_startpfn) {
  5520. unsigned long kernel_pages;
  5521. kernel_pages = min(end_pfn, usable_startpfn)
  5522. - start_pfn;
  5523. kernelcore_remaining -= min(kernel_pages,
  5524. kernelcore_remaining);
  5525. required_kernelcore -= min(kernel_pages,
  5526. required_kernelcore);
  5527. /* Continue if range is now fully accounted */
  5528. if (end_pfn <= usable_startpfn) {
  5529. /*
  5530. * Push zone_movable_pfn to the end so
  5531. * that if we have to rebalance
  5532. * kernelcore across nodes, we will
  5533. * not double account here
  5534. */
  5535. zone_movable_pfn[nid] = end_pfn;
  5536. continue;
  5537. }
  5538. start_pfn = usable_startpfn;
  5539. }
  5540. /*
  5541. * The usable PFN range for ZONE_MOVABLE is from
  5542. * start_pfn->end_pfn. Calculate size_pages as the
  5543. * number of pages used as kernelcore
  5544. */
  5545. size_pages = end_pfn - start_pfn;
  5546. if (size_pages > kernelcore_remaining)
  5547. size_pages = kernelcore_remaining;
  5548. zone_movable_pfn[nid] = start_pfn + size_pages;
  5549. /*
  5550. * Some kernelcore has been met, update counts and
  5551. * break if the kernelcore for this node has been
  5552. * satisfied
  5553. */
  5554. required_kernelcore -= min(required_kernelcore,
  5555. size_pages);
  5556. kernelcore_remaining -= size_pages;
  5557. if (!kernelcore_remaining)
  5558. break;
  5559. }
  5560. }
  5561. /*
  5562. * If there is still required_kernelcore, we do another pass with one
  5563. * less node in the count. This will push zone_movable_pfn[nid] further
  5564. * along on the nodes that still have memory until kernelcore is
  5565. * satisfied
  5566. */
  5567. usable_nodes--;
  5568. if (usable_nodes && required_kernelcore > usable_nodes)
  5569. goto restart;
  5570. out2:
  5571. /* Align start of ZONE_MOVABLE on all nids to MAX_ORDER_NR_PAGES */
  5572. for (nid = 0; nid < MAX_NUMNODES; nid++)
  5573. zone_movable_pfn[nid] =
  5574. roundup(zone_movable_pfn[nid], MAX_ORDER_NR_PAGES);
  5575. out:
  5576. /* restore the node_state */
  5577. node_states[N_MEMORY] = saved_node_state;
  5578. }
  5579. /* Any regular or high memory on that node ? */
  5580. static void check_for_memory(pg_data_t *pgdat, int nid)
  5581. {
  5582. enum zone_type zone_type;
  5583. if (N_MEMORY == N_NORMAL_MEMORY)
  5584. return;
  5585. for (zone_type = 0; zone_type <= ZONE_MOVABLE - 1; zone_type++) {
  5586. struct zone *zone = &pgdat->node_zones[zone_type];
  5587. if (populated_zone(zone)) {
  5588. node_set_state(nid, N_HIGH_MEMORY);
  5589. if (N_NORMAL_MEMORY != N_HIGH_MEMORY &&
  5590. zone_type <= ZONE_NORMAL)
  5591. node_set_state(nid, N_NORMAL_MEMORY);
  5592. break;
  5593. }
  5594. }
  5595. }
  5596. /**
  5597. * free_area_init_nodes - Initialise all pg_data_t and zone data
  5598. * @max_zone_pfn: an array of max PFNs for each zone
  5599. *
  5600. * This will call free_area_init_node() for each active node in the system.
  5601. * Using the page ranges provided by memblock_set_node(), the size of each
  5602. * zone in each node and their holes is calculated. If the maximum PFN
  5603. * between two adjacent zones match, it is assumed that the zone is empty.
  5604. * For example, if arch_max_dma_pfn == arch_max_dma32_pfn, it is assumed
  5605. * that arch_max_dma32_pfn has no pages. It is also assumed that a zone
  5606. * starts where the previous one ended. For example, ZONE_DMA32 starts
  5607. * at arch_max_dma_pfn.
  5608. */
  5609. void __init free_area_init_nodes(unsigned long *max_zone_pfn)
  5610. {
  5611. unsigned long start_pfn, end_pfn;
  5612. int i, nid;
  5613. /* Record where the zone boundaries are */
  5614. memset(arch_zone_lowest_possible_pfn, 0,
  5615. sizeof(arch_zone_lowest_possible_pfn));
  5616. memset(arch_zone_highest_possible_pfn, 0,
  5617. sizeof(arch_zone_highest_possible_pfn));
  5618. start_pfn = find_min_pfn_with_active_regions();
  5619. for (i = 0; i < MAX_NR_ZONES; i++) {
  5620. if (i == ZONE_MOVABLE)
  5621. continue;
  5622. end_pfn = max(max_zone_pfn[i], start_pfn);
  5623. arch_zone_lowest_possible_pfn[i] = start_pfn;
  5624. arch_zone_highest_possible_pfn[i] = end_pfn;
  5625. start_pfn = end_pfn;
  5626. }
  5627. /* Find the PFNs that ZONE_MOVABLE begins at in each node */
  5628. memset(zone_movable_pfn, 0, sizeof(zone_movable_pfn));
  5629. find_zone_movable_pfns_for_nodes();
  5630. /* Print out the zone ranges */
  5631. pr_info("Zone ranges:\n");
  5632. for (i = 0; i < MAX_NR_ZONES; i++) {
  5633. if (i == ZONE_MOVABLE)
  5634. continue;
  5635. pr_info(" %-8s ", zone_names[i]);
  5636. if (arch_zone_lowest_possible_pfn[i] ==
  5637. arch_zone_highest_possible_pfn[i])
  5638. pr_cont("empty\n");
  5639. else
  5640. pr_cont("[mem %#018Lx-%#018Lx]\n",
  5641. (u64)arch_zone_lowest_possible_pfn[i]
  5642. << PAGE_SHIFT,
  5643. ((u64)arch_zone_highest_possible_pfn[i]
  5644. << PAGE_SHIFT) - 1);
  5645. }
  5646. /* Print out the PFNs ZONE_MOVABLE begins at in each node */
  5647. pr_info("Movable zone start for each node\n");
  5648. for (i = 0; i < MAX_NUMNODES; i++) {
  5649. if (zone_movable_pfn[i])
  5650. pr_info(" Node %d: %#018Lx\n", i,
  5651. (u64)zone_movable_pfn[i] << PAGE_SHIFT);
  5652. }
  5653. /* Print out the early node map */
  5654. pr_info("Early memory node ranges\n");
  5655. for_each_mem_pfn_range(i, MAX_NUMNODES, &start_pfn, &end_pfn, &nid)
  5656. pr_info(" node %3d: [mem %#018Lx-%#018Lx]\n", nid,
  5657. (u64)start_pfn << PAGE_SHIFT,
  5658. ((u64)end_pfn << PAGE_SHIFT) - 1);
  5659. /* Initialise every node */
  5660. mminit_verify_pageflags_layout();
  5661. setup_nr_node_ids();
  5662. for_each_online_node(nid) {
  5663. pg_data_t *pgdat = NODE_DATA(nid);
  5664. free_area_init_node(nid, NULL,
  5665. find_min_pfn_for_node(nid), NULL);
  5666. /* Any memory on that node */
  5667. if (pgdat->node_present_pages)
  5668. node_set_state(nid, N_MEMORY);
  5669. check_for_memory(pgdat, nid);
  5670. }
  5671. }
  5672. static int __init cmdline_parse_core(char *p, unsigned long *core)
  5673. {
  5674. unsigned long long coremem;
  5675. if (!p)
  5676. return -EINVAL;
  5677. coremem = memparse(p, &p);
  5678. *core = coremem >> PAGE_SHIFT;
  5679. /* Paranoid check that UL is enough for the coremem value */
  5680. WARN_ON((coremem >> PAGE_SHIFT) > ULONG_MAX);
  5681. return 0;
  5682. }
  5683. /*
  5684. * kernelcore=size sets the amount of memory for use for allocations that
  5685. * cannot be reclaimed or migrated.
  5686. */
  5687. static int __init cmdline_parse_kernelcore(char *p)
  5688. {
  5689. /* parse kernelcore=mirror */
  5690. if (parse_option_str(p, "mirror")) {
  5691. mirrored_kernelcore = true;
  5692. return 0;
  5693. }
  5694. return cmdline_parse_core(p, &required_kernelcore);
  5695. }
  5696. /*
  5697. * movablecore=size sets the amount of memory for use for allocations that
  5698. * can be reclaimed or migrated.
  5699. */
  5700. static int __init cmdline_parse_movablecore(char *p)
  5701. {
  5702. return cmdline_parse_core(p, &required_movablecore);
  5703. }
  5704. early_param("kernelcore", cmdline_parse_kernelcore);
  5705. early_param("movablecore", cmdline_parse_movablecore);
  5706. #endif /* CONFIG_HAVE_MEMBLOCK_NODE_MAP */
  5707. void adjust_managed_page_count(struct page *page, long count)
  5708. {
  5709. spin_lock(&managed_page_count_lock);
  5710. page_zone(page)->managed_pages += count;
  5711. totalram_pages += count;
  5712. #ifdef CONFIG_HIGHMEM
  5713. if (PageHighMem(page))
  5714. totalhigh_pages += count;
  5715. #endif
  5716. spin_unlock(&managed_page_count_lock);
  5717. }
  5718. EXPORT_SYMBOL(adjust_managed_page_count);
  5719. unsigned long free_reserved_area(void *start, void *end, int poison, char *s)
  5720. {
  5721. void *pos;
  5722. unsigned long pages = 0;
  5723. start = (void *)PAGE_ALIGN((unsigned long)start);
  5724. end = (void *)((unsigned long)end & PAGE_MASK);
  5725. for (pos = start; pos < end; pos += PAGE_SIZE, pages++) {
  5726. if ((unsigned int)poison <= 0xFF)
  5727. memset(pos, poison, PAGE_SIZE);
  5728. free_reserved_page(virt_to_page(pos));
  5729. }
  5730. if (pages && s)
  5731. pr_info("Freeing %s memory: %ldK\n",
  5732. s, pages << (PAGE_SHIFT - 10));
  5733. return pages;
  5734. }
  5735. EXPORT_SYMBOL(free_reserved_area);
  5736. #ifdef CONFIG_HIGHMEM
  5737. void free_highmem_page(struct page *page)
  5738. {
  5739. __free_reserved_page(page);
  5740. totalram_pages++;
  5741. page_zone(page)->managed_pages++;
  5742. totalhigh_pages++;
  5743. }
  5744. #endif
  5745. void __init mem_init_print_info(const char *str)
  5746. {
  5747. unsigned long physpages, codesize, datasize, rosize, bss_size;
  5748. unsigned long init_code_size, init_data_size;
  5749. physpages = get_num_physpages();
  5750. codesize = _etext - _stext;
  5751. datasize = _edata - _sdata;
  5752. rosize = __end_rodata - __start_rodata;
  5753. bss_size = __bss_stop - __bss_start;
  5754. init_data_size = __init_end - __init_begin;
  5755. init_code_size = _einittext - _sinittext;
  5756. /*
  5757. * Detect special cases and adjust section sizes accordingly:
  5758. * 1) .init.* may be embedded into .data sections
  5759. * 2) .init.text.* may be out of [__init_begin, __init_end],
  5760. * please refer to arch/tile/kernel/vmlinux.lds.S.
  5761. * 3) .rodata.* may be embedded into .text or .data sections.
  5762. */
  5763. #define adj_init_size(start, end, size, pos, adj) \
  5764. do { \
  5765. if (start <= pos && pos < end && size > adj) \
  5766. size -= adj; \
  5767. } while (0)
  5768. adj_init_size(__init_begin, __init_end, init_data_size,
  5769. _sinittext, init_code_size);
  5770. adj_init_size(_stext, _etext, codesize, _sinittext, init_code_size);
  5771. adj_init_size(_sdata, _edata, datasize, __init_begin, init_data_size);
  5772. adj_init_size(_stext, _etext, codesize, __start_rodata, rosize);
  5773. adj_init_size(_sdata, _edata, datasize, __start_rodata, rosize);
  5774. #undef adj_init_size
  5775. pr_info("Memory: %luK/%luK available (%luK kernel code, %luK rwdata, %luK rodata, %luK init, %luK bss, %luK reserved, %luK cma-reserved"
  5776. #ifdef CONFIG_HIGHMEM
  5777. ", %luK highmem"
  5778. #endif
  5779. "%s%s)\n",
  5780. nr_free_pages() << (PAGE_SHIFT - 10),
  5781. physpages << (PAGE_SHIFT - 10),
  5782. codesize >> 10, datasize >> 10, rosize >> 10,
  5783. (init_data_size + init_code_size) >> 10, bss_size >> 10,
  5784. (physpages - totalram_pages - totalcma_pages) << (PAGE_SHIFT - 10),
  5785. totalcma_pages << (PAGE_SHIFT - 10),
  5786. #ifdef CONFIG_HIGHMEM
  5787. totalhigh_pages << (PAGE_SHIFT - 10),
  5788. #endif
  5789. str ? ", " : "", str ? str : "");
  5790. }
  5791. /**
  5792. * set_dma_reserve - set the specified number of pages reserved in the first zone
  5793. * @new_dma_reserve: The number of pages to mark reserved
  5794. *
  5795. * The per-cpu batchsize and zone watermarks are determined by managed_pages.
  5796. * In the DMA zone, a significant percentage may be consumed by kernel image
  5797. * and other unfreeable allocations which can skew the watermarks badly. This
  5798. * function may optionally be used to account for unfreeable pages in the
  5799. * first zone (e.g., ZONE_DMA). The effect will be lower watermarks and
  5800. * smaller per-cpu batchsize.
  5801. */
  5802. void __init set_dma_reserve(unsigned long new_dma_reserve)
  5803. {
  5804. dma_reserve = new_dma_reserve;
  5805. }
  5806. void __init free_area_init(unsigned long *zones_size)
  5807. {
  5808. free_area_init_node(0, zones_size,
  5809. __pa(PAGE_OFFSET) >> PAGE_SHIFT, NULL);
  5810. }
  5811. static int page_alloc_cpu_dead(unsigned int cpu)
  5812. {
  5813. lru_add_drain_cpu(cpu);
  5814. drain_pages(cpu);
  5815. /*
  5816. * Spill the event counters of the dead processor
  5817. * into the current processors event counters.
  5818. * This artificially elevates the count of the current
  5819. * processor.
  5820. */
  5821. vm_events_fold_cpu(cpu);
  5822. /*
  5823. * Zero the differential counters of the dead processor
  5824. * so that the vm statistics are consistent.
  5825. *
  5826. * This is only okay since the processor is dead and cannot
  5827. * race with what we are doing.
  5828. */
  5829. cpu_vm_stats_fold(cpu);
  5830. return 0;
  5831. }
  5832. void __init page_alloc_init(void)
  5833. {
  5834. int ret;
  5835. ret = cpuhp_setup_state_nocalls(CPUHP_PAGE_ALLOC_DEAD,
  5836. "mm/page_alloc:dead", NULL,
  5837. page_alloc_cpu_dead);
  5838. WARN_ON(ret < 0);
  5839. }
  5840. /*
  5841. * calculate_totalreserve_pages - called when sysctl_lowmem_reserve_ratio
  5842. * or min_free_kbytes changes.
  5843. */
  5844. static void calculate_totalreserve_pages(void)
  5845. {
  5846. struct pglist_data *pgdat;
  5847. unsigned long reserve_pages = 0;
  5848. enum zone_type i, j;
  5849. for_each_online_pgdat(pgdat) {
  5850. pgdat->totalreserve_pages = 0;
  5851. for (i = 0; i < MAX_NR_ZONES; i++) {
  5852. struct zone *zone = pgdat->node_zones + i;
  5853. long max = 0;
  5854. /* Find valid and maximum lowmem_reserve in the zone */
  5855. for (j = i; j < MAX_NR_ZONES; j++) {
  5856. if (zone->lowmem_reserve[j] > max)
  5857. max = zone->lowmem_reserve[j];
  5858. }
  5859. /* we treat the high watermark as reserved pages. */
  5860. max += high_wmark_pages(zone);
  5861. if (max > zone->managed_pages)
  5862. max = zone->managed_pages;
  5863. pgdat->totalreserve_pages += max;
  5864. reserve_pages += max;
  5865. }
  5866. }
  5867. totalreserve_pages = reserve_pages;
  5868. }
  5869. /*
  5870. * setup_per_zone_lowmem_reserve - called whenever
  5871. * sysctl_lowmem_reserve_ratio changes. Ensures that each zone
  5872. * has a correct pages reserved value, so an adequate number of
  5873. * pages are left in the zone after a successful __alloc_pages().
  5874. */
  5875. static void setup_per_zone_lowmem_reserve(void)
  5876. {
  5877. struct pglist_data *pgdat;
  5878. enum zone_type j, idx;
  5879. for_each_online_pgdat(pgdat) {
  5880. for (j = 0; j < MAX_NR_ZONES; j++) {
  5881. struct zone *zone = pgdat->node_zones + j;
  5882. unsigned long managed_pages = zone->managed_pages;
  5883. zone->lowmem_reserve[j] = 0;
  5884. idx = j;
  5885. while (idx) {
  5886. struct zone *lower_zone;
  5887. idx--;
  5888. if (sysctl_lowmem_reserve_ratio[idx] < 1)
  5889. sysctl_lowmem_reserve_ratio[idx] = 1;
  5890. lower_zone = pgdat->node_zones + idx;
  5891. lower_zone->lowmem_reserve[j] = managed_pages /
  5892. sysctl_lowmem_reserve_ratio[idx];
  5893. managed_pages += lower_zone->managed_pages;
  5894. }
  5895. }
  5896. }
  5897. /* update totalreserve_pages */
  5898. calculate_totalreserve_pages();
  5899. }
  5900. static void __setup_per_zone_wmarks(void)
  5901. {
  5902. unsigned long pages_min = min_free_kbytes >> (PAGE_SHIFT - 10);
  5903. unsigned long lowmem_pages = 0;
  5904. struct zone *zone;
  5905. unsigned long flags;
  5906. /* Calculate total number of !ZONE_HIGHMEM pages */
  5907. for_each_zone(zone) {
  5908. if (!is_highmem(zone))
  5909. lowmem_pages += zone->managed_pages;
  5910. }
  5911. for_each_zone(zone) {
  5912. u64 tmp;
  5913. spin_lock_irqsave(&zone->lock, flags);
  5914. tmp = (u64)pages_min * zone->managed_pages;
  5915. do_div(tmp, lowmem_pages);
  5916. if (is_highmem(zone)) {
  5917. /*
  5918. * __GFP_HIGH and PF_MEMALLOC allocations usually don't
  5919. * need highmem pages, so cap pages_min to a small
  5920. * value here.
  5921. *
  5922. * The WMARK_HIGH-WMARK_LOW and (WMARK_LOW-WMARK_MIN)
  5923. * deltas control asynch page reclaim, and so should
  5924. * not be capped for highmem.
  5925. */
  5926. unsigned long min_pages;
  5927. min_pages = zone->managed_pages / 1024;
  5928. min_pages = clamp(min_pages, SWAP_CLUSTER_MAX, 128UL);
  5929. zone->watermark[WMARK_MIN] = min_pages;
  5930. } else {
  5931. /*
  5932. * If it's a lowmem zone, reserve a number of pages
  5933. * proportionate to the zone's size.
  5934. */
  5935. zone->watermark[WMARK_MIN] = tmp;
  5936. }
  5937. /*
  5938. * Set the kswapd watermarks distance according to the
  5939. * scale factor in proportion to available memory, but
  5940. * ensure a minimum size on small systems.
  5941. */
  5942. tmp = max_t(u64, tmp >> 2,
  5943. mult_frac(zone->managed_pages,
  5944. watermark_scale_factor, 10000));
  5945. zone->watermark[WMARK_LOW] = min_wmark_pages(zone) + tmp;
  5946. zone->watermark[WMARK_HIGH] = min_wmark_pages(zone) + tmp * 2;
  5947. spin_unlock_irqrestore(&zone->lock, flags);
  5948. }
  5949. /* update totalreserve_pages */
  5950. calculate_totalreserve_pages();
  5951. }
  5952. /**
  5953. * setup_per_zone_wmarks - called when min_free_kbytes changes
  5954. * or when memory is hot-{added|removed}
  5955. *
  5956. * Ensures that the watermark[min,low,high] values for each zone are set
  5957. * correctly with respect to min_free_kbytes.
  5958. */
  5959. void setup_per_zone_wmarks(void)
  5960. {
  5961. static DEFINE_SPINLOCK(lock);
  5962. spin_lock(&lock);
  5963. __setup_per_zone_wmarks();
  5964. spin_unlock(&lock);
  5965. }
  5966. /*
  5967. * Initialise min_free_kbytes.
  5968. *
  5969. * For small machines we want it small (128k min). For large machines
  5970. * we want it large (64MB max). But it is not linear, because network
  5971. * bandwidth does not increase linearly with machine size. We use
  5972. *
  5973. * min_free_kbytes = 4 * sqrt(lowmem_kbytes), for better accuracy:
  5974. * min_free_kbytes = sqrt(lowmem_kbytes * 16)
  5975. *
  5976. * which yields
  5977. *
  5978. * 16MB: 512k
  5979. * 32MB: 724k
  5980. * 64MB: 1024k
  5981. * 128MB: 1448k
  5982. * 256MB: 2048k
  5983. * 512MB: 2896k
  5984. * 1024MB: 4096k
  5985. * 2048MB: 5792k
  5986. * 4096MB: 8192k
  5987. * 8192MB: 11584k
  5988. * 16384MB: 16384k
  5989. */
  5990. int __meminit init_per_zone_wmark_min(void)
  5991. {
  5992. unsigned long lowmem_kbytes;
  5993. int new_min_free_kbytes;
  5994. lowmem_kbytes = nr_free_buffer_pages() * (PAGE_SIZE >> 10);
  5995. new_min_free_kbytes = int_sqrt(lowmem_kbytes * 16);
  5996. if (new_min_free_kbytes > user_min_free_kbytes) {
  5997. min_free_kbytes = new_min_free_kbytes;
  5998. if (min_free_kbytes < 128)
  5999. min_free_kbytes = 128;
  6000. if (min_free_kbytes > 65536)
  6001. min_free_kbytes = 65536;
  6002. } else {
  6003. pr_warn("min_free_kbytes is not updated to %d because user defined value %d is preferred\n",
  6004. new_min_free_kbytes, user_min_free_kbytes);
  6005. }
  6006. setup_per_zone_wmarks();
  6007. refresh_zone_stat_thresholds();
  6008. setup_per_zone_lowmem_reserve();
  6009. #ifdef CONFIG_NUMA
  6010. setup_min_unmapped_ratio();
  6011. setup_min_slab_ratio();
  6012. #endif
  6013. return 0;
  6014. }
  6015. core_initcall(init_per_zone_wmark_min)
  6016. /*
  6017. * min_free_kbytes_sysctl_handler - just a wrapper around proc_dointvec() so
  6018. * that we can call two helper functions whenever min_free_kbytes
  6019. * changes.
  6020. */
  6021. int min_free_kbytes_sysctl_handler(struct ctl_table *table, int write,
  6022. void __user *buffer, size_t *length, loff_t *ppos)
  6023. {
  6024. int rc;
  6025. rc = proc_dointvec_minmax(table, write, buffer, length, ppos);
  6026. if (rc)
  6027. return rc;
  6028. if (write) {
  6029. user_min_free_kbytes = min_free_kbytes;
  6030. setup_per_zone_wmarks();
  6031. }
  6032. return 0;
  6033. }
  6034. int watermark_scale_factor_sysctl_handler(struct ctl_table *table, int write,
  6035. void __user *buffer, size_t *length, loff_t *ppos)
  6036. {
  6037. int rc;
  6038. rc = proc_dointvec_minmax(table, write, buffer, length, ppos);
  6039. if (rc)
  6040. return rc;
  6041. if (write)
  6042. setup_per_zone_wmarks();
  6043. return 0;
  6044. }
  6045. #ifdef CONFIG_NUMA
  6046. static void setup_min_unmapped_ratio(void)
  6047. {
  6048. pg_data_t *pgdat;
  6049. struct zone *zone;
  6050. for_each_online_pgdat(pgdat)
  6051. pgdat->min_unmapped_pages = 0;
  6052. for_each_zone(zone)
  6053. zone->zone_pgdat->min_unmapped_pages += (zone->managed_pages *
  6054. sysctl_min_unmapped_ratio) / 100;
  6055. }
  6056. int sysctl_min_unmapped_ratio_sysctl_handler(struct ctl_table *table, int write,
  6057. void __user *buffer, size_t *length, loff_t *ppos)
  6058. {
  6059. int rc;
  6060. rc = proc_dointvec_minmax(table, write, buffer, length, ppos);
  6061. if (rc)
  6062. return rc;
  6063. setup_min_unmapped_ratio();
  6064. return 0;
  6065. }
  6066. static void setup_min_slab_ratio(void)
  6067. {
  6068. pg_data_t *pgdat;
  6069. struct zone *zone;
  6070. for_each_online_pgdat(pgdat)
  6071. pgdat->min_slab_pages = 0;
  6072. for_each_zone(zone)
  6073. zone->zone_pgdat->min_slab_pages += (zone->managed_pages *
  6074. sysctl_min_slab_ratio) / 100;
  6075. }
  6076. int sysctl_min_slab_ratio_sysctl_handler(struct ctl_table *table, int write,
  6077. void __user *buffer, size_t *length, loff_t *ppos)
  6078. {
  6079. int rc;
  6080. rc = proc_dointvec_minmax(table, write, buffer, length, ppos);
  6081. if (rc)
  6082. return rc;
  6083. setup_min_slab_ratio();
  6084. return 0;
  6085. }
  6086. #endif
  6087. /*
  6088. * lowmem_reserve_ratio_sysctl_handler - just a wrapper around
  6089. * proc_dointvec() so that we can call setup_per_zone_lowmem_reserve()
  6090. * whenever sysctl_lowmem_reserve_ratio changes.
  6091. *
  6092. * The reserve ratio obviously has absolutely no relation with the
  6093. * minimum watermarks. The lowmem reserve ratio can only make sense
  6094. * if in function of the boot time zone sizes.
  6095. */
  6096. int lowmem_reserve_ratio_sysctl_handler(struct ctl_table *table, int write,
  6097. void __user *buffer, size_t *length, loff_t *ppos)
  6098. {
  6099. proc_dointvec_minmax(table, write, buffer, length, ppos);
  6100. setup_per_zone_lowmem_reserve();
  6101. return 0;
  6102. }
  6103. /*
  6104. * percpu_pagelist_fraction - changes the pcp->high for each zone on each
  6105. * cpu. It is the fraction of total pages in each zone that a hot per cpu
  6106. * pagelist can have before it gets flushed back to buddy allocator.
  6107. */
  6108. int percpu_pagelist_fraction_sysctl_handler(struct ctl_table *table, int write,
  6109. void __user *buffer, size_t *length, loff_t *ppos)
  6110. {
  6111. struct zone *zone;
  6112. int old_percpu_pagelist_fraction;
  6113. int ret;
  6114. mutex_lock(&pcp_batch_high_lock);
  6115. old_percpu_pagelist_fraction = percpu_pagelist_fraction;
  6116. ret = proc_dointvec_minmax(table, write, buffer, length, ppos);
  6117. if (!write || ret < 0)
  6118. goto out;
  6119. /* Sanity checking to avoid pcp imbalance */
  6120. if (percpu_pagelist_fraction &&
  6121. percpu_pagelist_fraction < MIN_PERCPU_PAGELIST_FRACTION) {
  6122. percpu_pagelist_fraction = old_percpu_pagelist_fraction;
  6123. ret = -EINVAL;
  6124. goto out;
  6125. }
  6126. /* No change? */
  6127. if (percpu_pagelist_fraction == old_percpu_pagelist_fraction)
  6128. goto out;
  6129. for_each_populated_zone(zone) {
  6130. unsigned int cpu;
  6131. for_each_possible_cpu(cpu)
  6132. pageset_set_high_and_batch(zone,
  6133. per_cpu_ptr(zone->pageset, cpu));
  6134. }
  6135. out:
  6136. mutex_unlock(&pcp_batch_high_lock);
  6137. return ret;
  6138. }
  6139. #ifdef CONFIG_NUMA
  6140. int hashdist = HASHDIST_DEFAULT;
  6141. static int __init set_hashdist(char *str)
  6142. {
  6143. if (!str)
  6144. return 0;
  6145. hashdist = simple_strtoul(str, &str, 0);
  6146. return 1;
  6147. }
  6148. __setup("hashdist=", set_hashdist);
  6149. #endif
  6150. #ifndef __HAVE_ARCH_RESERVED_KERNEL_PAGES
  6151. /*
  6152. * Returns the number of pages that arch has reserved but
  6153. * is not known to alloc_large_system_hash().
  6154. */
  6155. static unsigned long __init arch_reserved_kernel_pages(void)
  6156. {
  6157. return 0;
  6158. }
  6159. #endif
  6160. /*
  6161. * Adaptive scale is meant to reduce sizes of hash tables on large memory
  6162. * machines. As memory size is increased the scale is also increased but at
  6163. * slower pace. Starting from ADAPT_SCALE_BASE (64G), every time memory
  6164. * quadruples the scale is increased by one, which means the size of hash table
  6165. * only doubles, instead of quadrupling as well.
  6166. * Because 32-bit systems cannot have large physical memory, where this scaling
  6167. * makes sense, it is disabled on such platforms.
  6168. */
  6169. #if __BITS_PER_LONG > 32
  6170. #define ADAPT_SCALE_BASE (64ul << 30)
  6171. #define ADAPT_SCALE_SHIFT 2
  6172. #define ADAPT_SCALE_NPAGES (ADAPT_SCALE_BASE >> PAGE_SHIFT)
  6173. #endif
  6174. /*
  6175. * allocate a large system hash table from bootmem
  6176. * - it is assumed that the hash table must contain an exact power-of-2
  6177. * quantity of entries
  6178. * - limit is the number of hash buckets, not the total allocation size
  6179. */
  6180. void *__init alloc_large_system_hash(const char *tablename,
  6181. unsigned long bucketsize,
  6182. unsigned long numentries,
  6183. int scale,
  6184. int flags,
  6185. unsigned int *_hash_shift,
  6186. unsigned int *_hash_mask,
  6187. unsigned long low_limit,
  6188. unsigned long high_limit)
  6189. {
  6190. unsigned long long max = high_limit;
  6191. unsigned long log2qty, size;
  6192. void *table = NULL;
  6193. gfp_t gfp_flags;
  6194. /* allow the kernel cmdline to have a say */
  6195. if (!numentries) {
  6196. /* round applicable memory size up to nearest megabyte */
  6197. numentries = nr_kernel_pages;
  6198. numentries -= arch_reserved_kernel_pages();
  6199. /* It isn't necessary when PAGE_SIZE >= 1MB */
  6200. if (PAGE_SHIFT < 20)
  6201. numentries = round_up(numentries, (1<<20)/PAGE_SIZE);
  6202. #if __BITS_PER_LONG > 32
  6203. if (!high_limit) {
  6204. unsigned long adapt;
  6205. for (adapt = ADAPT_SCALE_NPAGES; adapt < numentries;
  6206. adapt <<= ADAPT_SCALE_SHIFT)
  6207. scale++;
  6208. }
  6209. #endif
  6210. /* limit to 1 bucket per 2^scale bytes of low memory */
  6211. if (scale > PAGE_SHIFT)
  6212. numentries >>= (scale - PAGE_SHIFT);
  6213. else
  6214. numentries <<= (PAGE_SHIFT - scale);
  6215. /* Make sure we've got at least a 0-order allocation.. */
  6216. if (unlikely(flags & HASH_SMALL)) {
  6217. /* Makes no sense without HASH_EARLY */
  6218. WARN_ON(!(flags & HASH_EARLY));
  6219. if (!(numentries >> *_hash_shift)) {
  6220. numentries = 1UL << *_hash_shift;
  6221. BUG_ON(!numentries);
  6222. }
  6223. } else if (unlikely((numentries * bucketsize) < PAGE_SIZE))
  6224. numentries = PAGE_SIZE / bucketsize;
  6225. }
  6226. numentries = roundup_pow_of_two(numentries);
  6227. /* limit allocation size to 1/16 total memory by default */
  6228. if (max == 0) {
  6229. max = ((unsigned long long)nr_all_pages << PAGE_SHIFT) >> 4;
  6230. do_div(max, bucketsize);
  6231. }
  6232. max = min(max, 0x80000000ULL);
  6233. if (numentries < low_limit)
  6234. numentries = low_limit;
  6235. if (numentries > max)
  6236. numentries = max;
  6237. log2qty = ilog2(numentries);
  6238. /*
  6239. * memblock allocator returns zeroed memory already, so HASH_ZERO is
  6240. * currently not used when HASH_EARLY is specified.
  6241. */
  6242. gfp_flags = (flags & HASH_ZERO) ? GFP_ATOMIC | __GFP_ZERO : GFP_ATOMIC;
  6243. do {
  6244. size = bucketsize << log2qty;
  6245. if (flags & HASH_EARLY)
  6246. table = memblock_virt_alloc_nopanic(size, 0);
  6247. else if (hashdist)
  6248. table = __vmalloc(size, gfp_flags, PAGE_KERNEL);
  6249. else {
  6250. /*
  6251. * If bucketsize is not a power-of-two, we may free
  6252. * some pages at the end of hash table which
  6253. * alloc_pages_exact() automatically does
  6254. */
  6255. if (get_order(size) < MAX_ORDER) {
  6256. table = alloc_pages_exact(size, gfp_flags);
  6257. kmemleak_alloc(table, size, 1, gfp_flags);
  6258. }
  6259. }
  6260. } while (!table && size > PAGE_SIZE && --log2qty);
  6261. if (!table)
  6262. panic("Failed to allocate %s hash table\n", tablename);
  6263. pr_info("%s hash table entries: %ld (order: %d, %lu bytes)\n",
  6264. tablename, 1UL << log2qty, ilog2(size) - PAGE_SHIFT, size);
  6265. if (_hash_shift)
  6266. *_hash_shift = log2qty;
  6267. if (_hash_mask)
  6268. *_hash_mask = (1 << log2qty) - 1;
  6269. return table;
  6270. }
  6271. /*
  6272. * This function checks whether pageblock includes unmovable pages or not.
  6273. * If @count is not zero, it is okay to include less @count unmovable pages
  6274. *
  6275. * PageLRU check without isolation or lru_lock could race so that
  6276. * MIGRATE_MOVABLE block might include unmovable pages. And __PageMovable
  6277. * check without lock_page also may miss some movable non-lru pages at
  6278. * race condition. So you can't expect this function should be exact.
  6279. */
  6280. bool has_unmovable_pages(struct zone *zone, struct page *page, int count,
  6281. bool skip_hwpoisoned_pages)
  6282. {
  6283. unsigned long pfn, iter, found;
  6284. int mt;
  6285. /*
  6286. * For avoiding noise data, lru_add_drain_all() should be called
  6287. * If ZONE_MOVABLE, the zone never contains unmovable pages
  6288. */
  6289. if (zone_idx(zone) == ZONE_MOVABLE)
  6290. return false;
  6291. mt = get_pageblock_migratetype(page);
  6292. if (mt == MIGRATE_MOVABLE || is_migrate_cma(mt))
  6293. return false;
  6294. pfn = page_to_pfn(page);
  6295. for (found = 0, iter = 0; iter < pageblock_nr_pages; iter++) {
  6296. unsigned long check = pfn + iter;
  6297. if (!pfn_valid_within(check))
  6298. continue;
  6299. page = pfn_to_page(check);
  6300. /*
  6301. * Hugepages are not in LRU lists, but they're movable.
  6302. * We need not scan over tail pages bacause we don't
  6303. * handle each tail page individually in migration.
  6304. */
  6305. if (PageHuge(page)) {
  6306. iter = round_up(iter + 1, 1<<compound_order(page)) - 1;
  6307. continue;
  6308. }
  6309. /*
  6310. * We can't use page_count without pin a page
  6311. * because another CPU can free compound page.
  6312. * This check already skips compound tails of THP
  6313. * because their page->_refcount is zero at all time.
  6314. */
  6315. if (!page_ref_count(page)) {
  6316. if (PageBuddy(page))
  6317. iter += (1 << page_order(page)) - 1;
  6318. continue;
  6319. }
  6320. /*
  6321. * The HWPoisoned page may be not in buddy system, and
  6322. * page_count() is not 0.
  6323. */
  6324. if (skip_hwpoisoned_pages && PageHWPoison(page))
  6325. continue;
  6326. if (__PageMovable(page))
  6327. continue;
  6328. if (!PageLRU(page))
  6329. found++;
  6330. /*
  6331. * If there are RECLAIMABLE pages, we need to check
  6332. * it. But now, memory offline itself doesn't call
  6333. * shrink_node_slabs() and it still to be fixed.
  6334. */
  6335. /*
  6336. * If the page is not RAM, page_count()should be 0.
  6337. * we don't need more check. This is an _used_ not-movable page.
  6338. *
  6339. * The problematic thing here is PG_reserved pages. PG_reserved
  6340. * is set to both of a memory hole page and a _used_ kernel
  6341. * page at boot.
  6342. */
  6343. if (found > count)
  6344. return true;
  6345. }
  6346. return false;
  6347. }
  6348. bool is_pageblock_removable_nolock(struct page *page)
  6349. {
  6350. struct zone *zone;
  6351. unsigned long pfn;
  6352. /*
  6353. * We have to be careful here because we are iterating over memory
  6354. * sections which are not zone aware so we might end up outside of
  6355. * the zone but still within the section.
  6356. * We have to take care about the node as well. If the node is offline
  6357. * its NODE_DATA will be NULL - see page_zone.
  6358. */
  6359. if (!node_online(page_to_nid(page)))
  6360. return false;
  6361. zone = page_zone(page);
  6362. pfn = page_to_pfn(page);
  6363. if (!zone_spans_pfn(zone, pfn))
  6364. return false;
  6365. return !has_unmovable_pages(zone, page, 0, true);
  6366. }
  6367. #if (defined(CONFIG_MEMORY_ISOLATION) && defined(CONFIG_COMPACTION)) || defined(CONFIG_CMA)
  6368. static unsigned long pfn_max_align_down(unsigned long pfn)
  6369. {
  6370. return pfn & ~(max_t(unsigned long, MAX_ORDER_NR_PAGES,
  6371. pageblock_nr_pages) - 1);
  6372. }
  6373. static unsigned long pfn_max_align_up(unsigned long pfn)
  6374. {
  6375. return ALIGN(pfn, max_t(unsigned long, MAX_ORDER_NR_PAGES,
  6376. pageblock_nr_pages));
  6377. }
  6378. /* [start, end) must belong to a single zone. */
  6379. static int __alloc_contig_migrate_range(struct compact_control *cc,
  6380. unsigned long start, unsigned long end)
  6381. {
  6382. /* This function is based on compact_zone() from compaction.c. */
  6383. unsigned long nr_reclaimed;
  6384. unsigned long pfn = start;
  6385. unsigned int tries = 0;
  6386. int ret = 0;
  6387. migrate_prep();
  6388. while (pfn < end || !list_empty(&cc->migratepages)) {
  6389. if (fatal_signal_pending(current)) {
  6390. ret = -EINTR;
  6391. break;
  6392. }
  6393. if (list_empty(&cc->migratepages)) {
  6394. cc->nr_migratepages = 0;
  6395. pfn = isolate_migratepages_range(cc, pfn, end);
  6396. if (!pfn) {
  6397. ret = -EINTR;
  6398. break;
  6399. }
  6400. tries = 0;
  6401. } else if (++tries == 5) {
  6402. ret = ret < 0 ? ret : -EBUSY;
  6403. break;
  6404. }
  6405. nr_reclaimed = reclaim_clean_pages_from_list(cc->zone,
  6406. &cc->migratepages);
  6407. cc->nr_migratepages -= nr_reclaimed;
  6408. ret = migrate_pages(&cc->migratepages, alloc_migrate_target,
  6409. NULL, 0, cc->mode, MR_CMA);
  6410. }
  6411. if (ret < 0) {
  6412. putback_movable_pages(&cc->migratepages);
  6413. return ret;
  6414. }
  6415. return 0;
  6416. }
  6417. /**
  6418. * alloc_contig_range() -- tries to allocate given range of pages
  6419. * @start: start PFN to allocate
  6420. * @end: one-past-the-last PFN to allocate
  6421. * @migratetype: migratetype of the underlaying pageblocks (either
  6422. * #MIGRATE_MOVABLE or #MIGRATE_CMA). All pageblocks
  6423. * in range must have the same migratetype and it must
  6424. * be either of the two.
  6425. * @gfp_mask: GFP mask to use during compaction
  6426. *
  6427. * The PFN range does not have to be pageblock or MAX_ORDER_NR_PAGES
  6428. * aligned, however it's the caller's responsibility to guarantee that
  6429. * we are the only thread that changes migrate type of pageblocks the
  6430. * pages fall in.
  6431. *
  6432. * The PFN range must belong to a single zone.
  6433. *
  6434. * Returns zero on success or negative error code. On success all
  6435. * pages which PFN is in [start, end) are allocated for the caller and
  6436. * need to be freed with free_contig_range().
  6437. */
  6438. int alloc_contig_range(unsigned long start, unsigned long end,
  6439. unsigned migratetype, gfp_t gfp_mask)
  6440. {
  6441. unsigned long outer_start, outer_end;
  6442. unsigned int order;
  6443. int ret = 0;
  6444. struct compact_control cc = {
  6445. .nr_migratepages = 0,
  6446. .order = -1,
  6447. .zone = page_zone(pfn_to_page(start)),
  6448. .mode = MIGRATE_SYNC,
  6449. .ignore_skip_hint = true,
  6450. .gfp_mask = current_gfp_context(gfp_mask),
  6451. };
  6452. INIT_LIST_HEAD(&cc.migratepages);
  6453. /*
  6454. * What we do here is we mark all pageblocks in range as
  6455. * MIGRATE_ISOLATE. Because pageblock and max order pages may
  6456. * have different sizes, and due to the way page allocator
  6457. * work, we align the range to biggest of the two pages so
  6458. * that page allocator won't try to merge buddies from
  6459. * different pageblocks and change MIGRATE_ISOLATE to some
  6460. * other migration type.
  6461. *
  6462. * Once the pageblocks are marked as MIGRATE_ISOLATE, we
  6463. * migrate the pages from an unaligned range (ie. pages that
  6464. * we are interested in). This will put all the pages in
  6465. * range back to page allocator as MIGRATE_ISOLATE.
  6466. *
  6467. * When this is done, we take the pages in range from page
  6468. * allocator removing them from the buddy system. This way
  6469. * page allocator will never consider using them.
  6470. *
  6471. * This lets us mark the pageblocks back as
  6472. * MIGRATE_CMA/MIGRATE_MOVABLE so that free pages in the
  6473. * aligned range but not in the unaligned, original range are
  6474. * put back to page allocator so that buddy can use them.
  6475. */
  6476. ret = start_isolate_page_range(pfn_max_align_down(start),
  6477. pfn_max_align_up(end), migratetype,
  6478. false);
  6479. if (ret)
  6480. return ret;
  6481. /*
  6482. * In case of -EBUSY, we'd like to know which page causes problem.
  6483. * So, just fall through. We will check it in test_pages_isolated().
  6484. */
  6485. ret = __alloc_contig_migrate_range(&cc, start, end);
  6486. if (ret && ret != -EBUSY)
  6487. goto done;
  6488. /*
  6489. * Pages from [start, end) are within a MAX_ORDER_NR_PAGES
  6490. * aligned blocks that are marked as MIGRATE_ISOLATE. What's
  6491. * more, all pages in [start, end) are free in page allocator.
  6492. * What we are going to do is to allocate all pages from
  6493. * [start, end) (that is remove them from page allocator).
  6494. *
  6495. * The only problem is that pages at the beginning and at the
  6496. * end of interesting range may be not aligned with pages that
  6497. * page allocator holds, ie. they can be part of higher order
  6498. * pages. Because of this, we reserve the bigger range and
  6499. * once this is done free the pages we are not interested in.
  6500. *
  6501. * We don't have to hold zone->lock here because the pages are
  6502. * isolated thus they won't get removed from buddy.
  6503. */
  6504. lru_add_drain_all();
  6505. drain_all_pages(cc.zone);
  6506. order = 0;
  6507. outer_start = start;
  6508. while (!PageBuddy(pfn_to_page(outer_start))) {
  6509. if (++order >= MAX_ORDER) {
  6510. outer_start = start;
  6511. break;
  6512. }
  6513. outer_start &= ~0UL << order;
  6514. }
  6515. if (outer_start != start) {
  6516. order = page_order(pfn_to_page(outer_start));
  6517. /*
  6518. * outer_start page could be small order buddy page and
  6519. * it doesn't include start page. Adjust outer_start
  6520. * in this case to report failed page properly
  6521. * on tracepoint in test_pages_isolated()
  6522. */
  6523. if (outer_start + (1UL << order) <= start)
  6524. outer_start = start;
  6525. }
  6526. /* Make sure the range is really isolated. */
  6527. if (test_pages_isolated(outer_start, end, false)) {
  6528. pr_info_ratelimited("%s: [%lx, %lx) PFNs busy\n",
  6529. __func__, outer_start, end);
  6530. ret = -EBUSY;
  6531. goto done;
  6532. }
  6533. /* Grab isolated pages from freelists. */
  6534. outer_end = isolate_freepages_range(&cc, outer_start, end);
  6535. if (!outer_end) {
  6536. ret = -EBUSY;
  6537. goto done;
  6538. }
  6539. /* Free head and tail (if any) */
  6540. if (start != outer_start)
  6541. free_contig_range(outer_start, start - outer_start);
  6542. if (end != outer_end)
  6543. free_contig_range(end, outer_end - end);
  6544. done:
  6545. undo_isolate_page_range(pfn_max_align_down(start),
  6546. pfn_max_align_up(end), migratetype);
  6547. return ret;
  6548. }
  6549. void free_contig_range(unsigned long pfn, unsigned nr_pages)
  6550. {
  6551. unsigned int count = 0;
  6552. for (; nr_pages--; pfn++) {
  6553. struct page *page = pfn_to_page(pfn);
  6554. count += page_count(page) != 1;
  6555. __free_page(page);
  6556. }
  6557. WARN(count != 0, "%d pages are still in use!\n", count);
  6558. }
  6559. #endif
  6560. #ifdef CONFIG_MEMORY_HOTPLUG
  6561. /*
  6562. * The zone indicated has a new number of managed_pages; batch sizes and percpu
  6563. * page high values need to be recalulated.
  6564. */
  6565. void __meminit zone_pcp_update(struct zone *zone)
  6566. {
  6567. unsigned cpu;
  6568. mutex_lock(&pcp_batch_high_lock);
  6569. for_each_possible_cpu(cpu)
  6570. pageset_set_high_and_batch(zone,
  6571. per_cpu_ptr(zone->pageset, cpu));
  6572. mutex_unlock(&pcp_batch_high_lock);
  6573. }
  6574. #endif
  6575. void zone_pcp_reset(struct zone *zone)
  6576. {
  6577. unsigned long flags;
  6578. int cpu;
  6579. struct per_cpu_pageset *pset;
  6580. /* avoid races with drain_pages() */
  6581. local_irq_save(flags);
  6582. if (zone->pageset != &boot_pageset) {
  6583. for_each_online_cpu(cpu) {
  6584. pset = per_cpu_ptr(zone->pageset, cpu);
  6585. drain_zonestat(zone, pset);
  6586. }
  6587. free_percpu(zone->pageset);
  6588. zone->pageset = &boot_pageset;
  6589. }
  6590. local_irq_restore(flags);
  6591. }
  6592. #ifdef CONFIG_MEMORY_HOTREMOVE
  6593. /*
  6594. * All pages in the range must be in a single zone and isolated
  6595. * before calling this.
  6596. */
  6597. void
  6598. __offline_isolated_pages(unsigned long start_pfn, unsigned long end_pfn)
  6599. {
  6600. struct page *page;
  6601. struct zone *zone;
  6602. unsigned int order, i;
  6603. unsigned long pfn;
  6604. unsigned long flags;
  6605. /* find the first valid pfn */
  6606. for (pfn = start_pfn; pfn < end_pfn; pfn++)
  6607. if (pfn_valid(pfn))
  6608. break;
  6609. if (pfn == end_pfn)
  6610. return;
  6611. offline_mem_sections(pfn, end_pfn);
  6612. zone = page_zone(pfn_to_page(pfn));
  6613. spin_lock_irqsave(&zone->lock, flags);
  6614. pfn = start_pfn;
  6615. while (pfn < end_pfn) {
  6616. if (!pfn_valid(pfn)) {
  6617. pfn++;
  6618. continue;
  6619. }
  6620. page = pfn_to_page(pfn);
  6621. /*
  6622. * The HWPoisoned page may be not in buddy system, and
  6623. * page_count() is not 0.
  6624. */
  6625. if (unlikely(!PageBuddy(page) && PageHWPoison(page))) {
  6626. pfn++;
  6627. SetPageReserved(page);
  6628. continue;
  6629. }
  6630. BUG_ON(page_count(page));
  6631. BUG_ON(!PageBuddy(page));
  6632. order = page_order(page);
  6633. #ifdef CONFIG_DEBUG_VM
  6634. pr_info("remove from free list %lx %d %lx\n",
  6635. pfn, 1 << order, end_pfn);
  6636. #endif
  6637. list_del(&page->lru);
  6638. rmv_page_order(page);
  6639. zone->free_area[order].nr_free--;
  6640. for (i = 0; i < (1 << order); i++)
  6641. SetPageReserved((page+i));
  6642. pfn += (1 << order);
  6643. }
  6644. spin_unlock_irqrestore(&zone->lock, flags);
  6645. }
  6646. #endif
  6647. bool is_free_buddy_page(struct page *page)
  6648. {
  6649. struct zone *zone = page_zone(page);
  6650. unsigned long pfn = page_to_pfn(page);
  6651. unsigned long flags;
  6652. unsigned int order;
  6653. spin_lock_irqsave(&zone->lock, flags);
  6654. for (order = 0; order < MAX_ORDER; order++) {
  6655. struct page *page_head = page - (pfn & ((1 << order) - 1));
  6656. if (PageBuddy(page_head) && page_order(page_head) >= order)
  6657. break;
  6658. }
  6659. spin_unlock_irqrestore(&zone->lock, flags);
  6660. return order < MAX_ORDER;
  6661. }