page_alloc.c 182 KB

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