page_alloc.c 159 KB

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