page_alloc.c 193 KB

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