page_alloc.c 159 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815
  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. va_list args;
  1513. unsigned int filter = SHOW_MEM_FILTER_NODES;
  1514. if ((gfp_mask & __GFP_NOWARN) || !__ratelimit(&nopage_rs))
  1515. return;
  1516. /*
  1517. * This documents exceptions given to allocations in certain
  1518. * contexts that are allowed to allocate outside current's set
  1519. * of allowed nodes.
  1520. */
  1521. if (!(gfp_mask & __GFP_NOMEMALLOC))
  1522. if (test_thread_flag(TIF_MEMDIE) ||
  1523. (current->flags & (PF_MEMALLOC | PF_EXITING)))
  1524. filter &= ~SHOW_MEM_FILTER_NODES;
  1525. if (in_interrupt() || !(gfp_mask & __GFP_WAIT))
  1526. filter &= ~SHOW_MEM_FILTER_NODES;
  1527. if (fmt) {
  1528. printk(KERN_WARNING);
  1529. va_start(args, fmt);
  1530. vprintk(fmt, args);
  1531. va_end(args);
  1532. }
  1533. pr_warning("%s: page allocation failure: order:%d, mode:0x%x\n",
  1534. current->comm, order, gfp_mask);
  1535. dump_stack();
  1536. if (!should_suppress_show_mem())
  1537. show_mem(filter);
  1538. }
  1539. static inline int
  1540. should_alloc_retry(gfp_t gfp_mask, unsigned int order,
  1541. unsigned long pages_reclaimed)
  1542. {
  1543. /* Do not loop if specifically requested */
  1544. if (gfp_mask & __GFP_NORETRY)
  1545. return 0;
  1546. /*
  1547. * In this implementation, order <= PAGE_ALLOC_COSTLY_ORDER
  1548. * means __GFP_NOFAIL, but that may not be true in other
  1549. * implementations.
  1550. */
  1551. if (order <= PAGE_ALLOC_COSTLY_ORDER)
  1552. return 1;
  1553. /*
  1554. * For order > PAGE_ALLOC_COSTLY_ORDER, if __GFP_REPEAT is
  1555. * specified, then we retry until we no longer reclaim any pages
  1556. * (above), or we've reclaimed an order of pages at least as
  1557. * large as the allocation's order. In both cases, if the
  1558. * allocation still fails, we stop retrying.
  1559. */
  1560. if (gfp_mask & __GFP_REPEAT && pages_reclaimed < (1 << order))
  1561. return 1;
  1562. /*
  1563. * Don't let big-order allocations loop unless the caller
  1564. * explicitly requests that.
  1565. */
  1566. if (gfp_mask & __GFP_NOFAIL)
  1567. return 1;
  1568. return 0;
  1569. }
  1570. static inline struct page *
  1571. __alloc_pages_may_oom(gfp_t gfp_mask, unsigned int order,
  1572. struct zonelist *zonelist, enum zone_type high_zoneidx,
  1573. nodemask_t *nodemask, struct zone *preferred_zone,
  1574. int migratetype)
  1575. {
  1576. struct page *page;
  1577. /* Acquire the OOM killer lock for the zones in zonelist */
  1578. if (!try_set_zonelist_oom(zonelist, gfp_mask)) {
  1579. schedule_timeout_uninterruptible(1);
  1580. return NULL;
  1581. }
  1582. /*
  1583. * Go through the zonelist yet one more time, keep very high watermark
  1584. * here, this is only to catch a parallel oom killing, we must fail if
  1585. * we're still under heavy pressure.
  1586. */
  1587. page = get_page_from_freelist(gfp_mask|__GFP_HARDWALL, nodemask,
  1588. order, zonelist, high_zoneidx,
  1589. ALLOC_WMARK_HIGH|ALLOC_CPUSET,
  1590. preferred_zone, migratetype);
  1591. if (page)
  1592. goto out;
  1593. if (!(gfp_mask & __GFP_NOFAIL)) {
  1594. /* The OOM killer will not help higher order allocs */
  1595. if (order > PAGE_ALLOC_COSTLY_ORDER)
  1596. goto out;
  1597. /* The OOM killer does not needlessly kill tasks for lowmem */
  1598. if (high_zoneidx < ZONE_NORMAL)
  1599. goto out;
  1600. /*
  1601. * GFP_THISNODE contains __GFP_NORETRY and we never hit this.
  1602. * Sanity check for bare calls of __GFP_THISNODE, not real OOM.
  1603. * The caller should handle page allocation failure by itself if
  1604. * it specifies __GFP_THISNODE.
  1605. * Note: Hugepage uses it but will hit PAGE_ALLOC_COSTLY_ORDER.
  1606. */
  1607. if (gfp_mask & __GFP_THISNODE)
  1608. goto out;
  1609. }
  1610. /* Exhausted what can be done so it's blamo time */
  1611. out_of_memory(zonelist, gfp_mask, order, nodemask);
  1612. out:
  1613. clear_zonelist_oom(zonelist, gfp_mask);
  1614. return page;
  1615. }
  1616. #ifdef CONFIG_COMPACTION
  1617. /* Try memory compaction for high-order allocations before reclaim */
  1618. static struct page *
  1619. __alloc_pages_direct_compact(gfp_t gfp_mask, unsigned int order,
  1620. struct zonelist *zonelist, enum zone_type high_zoneidx,
  1621. nodemask_t *nodemask, int alloc_flags, struct zone *preferred_zone,
  1622. int migratetype, unsigned long *did_some_progress,
  1623. bool sync_migration)
  1624. {
  1625. struct page *page;
  1626. if (!order || compaction_deferred(preferred_zone))
  1627. return NULL;
  1628. current->flags |= PF_MEMALLOC;
  1629. *did_some_progress = try_to_compact_pages(zonelist, order, gfp_mask,
  1630. nodemask, sync_migration);
  1631. current->flags &= ~PF_MEMALLOC;
  1632. if (*did_some_progress != COMPACT_SKIPPED) {
  1633. /* Page migration frees to the PCP lists but we want merging */
  1634. drain_pages(get_cpu());
  1635. put_cpu();
  1636. page = get_page_from_freelist(gfp_mask, nodemask,
  1637. order, zonelist, high_zoneidx,
  1638. alloc_flags, preferred_zone,
  1639. migratetype);
  1640. if (page) {
  1641. preferred_zone->compact_considered = 0;
  1642. preferred_zone->compact_defer_shift = 0;
  1643. count_vm_event(COMPACTSUCCESS);
  1644. return page;
  1645. }
  1646. /*
  1647. * It's bad if compaction run occurs and fails.
  1648. * The most likely reason is that pages exist,
  1649. * but not enough to satisfy watermarks.
  1650. */
  1651. count_vm_event(COMPACTFAIL);
  1652. defer_compaction(preferred_zone);
  1653. cond_resched();
  1654. }
  1655. return NULL;
  1656. }
  1657. #else
  1658. static inline struct page *
  1659. __alloc_pages_direct_compact(gfp_t gfp_mask, unsigned int order,
  1660. struct zonelist *zonelist, enum zone_type high_zoneidx,
  1661. nodemask_t *nodemask, int alloc_flags, struct zone *preferred_zone,
  1662. int migratetype, unsigned long *did_some_progress,
  1663. bool sync_migration)
  1664. {
  1665. return NULL;
  1666. }
  1667. #endif /* CONFIG_COMPACTION */
  1668. /* The really slow allocator path where we enter direct reclaim */
  1669. static inline struct page *
  1670. __alloc_pages_direct_reclaim(gfp_t gfp_mask, unsigned int order,
  1671. struct zonelist *zonelist, enum zone_type high_zoneidx,
  1672. nodemask_t *nodemask, int alloc_flags, struct zone *preferred_zone,
  1673. int migratetype, unsigned long *did_some_progress)
  1674. {
  1675. struct page *page = NULL;
  1676. struct reclaim_state reclaim_state;
  1677. bool drained = false;
  1678. cond_resched();
  1679. /* We now go into synchronous reclaim */
  1680. cpuset_memory_pressure_bump();
  1681. current->flags |= PF_MEMALLOC;
  1682. lockdep_set_current_reclaim_state(gfp_mask);
  1683. reclaim_state.reclaimed_slab = 0;
  1684. current->reclaim_state = &reclaim_state;
  1685. *did_some_progress = try_to_free_pages(zonelist, order, gfp_mask, nodemask);
  1686. current->reclaim_state = NULL;
  1687. lockdep_clear_current_reclaim_state();
  1688. current->flags &= ~PF_MEMALLOC;
  1689. cond_resched();
  1690. if (unlikely(!(*did_some_progress)))
  1691. return NULL;
  1692. /* After successful reclaim, reconsider all zones for allocation */
  1693. if (NUMA_BUILD)
  1694. zlc_clear_zones_full(zonelist);
  1695. retry:
  1696. page = get_page_from_freelist(gfp_mask, nodemask, order,
  1697. zonelist, high_zoneidx,
  1698. alloc_flags, preferred_zone,
  1699. migratetype);
  1700. /*
  1701. * If an allocation failed after direct reclaim, it could be because
  1702. * pages are pinned on the per-cpu lists. Drain them and try again
  1703. */
  1704. if (!page && !drained) {
  1705. drain_all_pages();
  1706. drained = true;
  1707. goto retry;
  1708. }
  1709. return page;
  1710. }
  1711. /*
  1712. * This is called in the allocator slow-path if the allocation request is of
  1713. * sufficient urgency to ignore watermarks and take other desperate measures
  1714. */
  1715. static inline struct page *
  1716. __alloc_pages_high_priority(gfp_t gfp_mask, unsigned int order,
  1717. struct zonelist *zonelist, enum zone_type high_zoneidx,
  1718. nodemask_t *nodemask, struct zone *preferred_zone,
  1719. int migratetype)
  1720. {
  1721. struct page *page;
  1722. do {
  1723. page = get_page_from_freelist(gfp_mask, nodemask, order,
  1724. zonelist, high_zoneidx, ALLOC_NO_WATERMARKS,
  1725. preferred_zone, migratetype);
  1726. if (!page && gfp_mask & __GFP_NOFAIL)
  1727. wait_iff_congested(preferred_zone, BLK_RW_ASYNC, HZ/50);
  1728. } while (!page && (gfp_mask & __GFP_NOFAIL));
  1729. return page;
  1730. }
  1731. static inline
  1732. void wake_all_kswapd(unsigned int order, struct zonelist *zonelist,
  1733. enum zone_type high_zoneidx,
  1734. enum zone_type classzone_idx)
  1735. {
  1736. struct zoneref *z;
  1737. struct zone *zone;
  1738. for_each_zone_zonelist(zone, z, zonelist, high_zoneidx)
  1739. wakeup_kswapd(zone, order, classzone_idx);
  1740. }
  1741. static inline int
  1742. gfp_to_alloc_flags(gfp_t gfp_mask)
  1743. {
  1744. int alloc_flags = ALLOC_WMARK_MIN | ALLOC_CPUSET;
  1745. const gfp_t wait = gfp_mask & __GFP_WAIT;
  1746. /* __GFP_HIGH is assumed to be the same as ALLOC_HIGH to save a branch. */
  1747. BUILD_BUG_ON(__GFP_HIGH != (__force gfp_t) ALLOC_HIGH);
  1748. /*
  1749. * The caller may dip into page reserves a bit more if the caller
  1750. * cannot run direct reclaim, or if the caller has realtime scheduling
  1751. * policy or is asking for __GFP_HIGH memory. GFP_ATOMIC requests will
  1752. * set both ALLOC_HARDER (!wait) and ALLOC_HIGH (__GFP_HIGH).
  1753. */
  1754. alloc_flags |= (__force int) (gfp_mask & __GFP_HIGH);
  1755. if (!wait) {
  1756. /*
  1757. * Not worth trying to allocate harder for
  1758. * __GFP_NOMEMALLOC even if it can't schedule.
  1759. */
  1760. if (!(gfp_mask & __GFP_NOMEMALLOC))
  1761. alloc_flags |= ALLOC_HARDER;
  1762. /*
  1763. * Ignore cpuset if GFP_ATOMIC (!wait) rather than fail alloc.
  1764. * See also cpuset_zone_allowed() comment in kernel/cpuset.c.
  1765. */
  1766. alloc_flags &= ~ALLOC_CPUSET;
  1767. } else if (unlikely(rt_task(current)) && !in_interrupt())
  1768. alloc_flags |= ALLOC_HARDER;
  1769. if (likely(!(gfp_mask & __GFP_NOMEMALLOC))) {
  1770. if (!in_interrupt() &&
  1771. ((current->flags & PF_MEMALLOC) ||
  1772. unlikely(test_thread_flag(TIF_MEMDIE))))
  1773. alloc_flags |= ALLOC_NO_WATERMARKS;
  1774. }
  1775. return alloc_flags;
  1776. }
  1777. static inline struct page *
  1778. __alloc_pages_slowpath(gfp_t gfp_mask, unsigned int order,
  1779. struct zonelist *zonelist, enum zone_type high_zoneidx,
  1780. nodemask_t *nodemask, struct zone *preferred_zone,
  1781. int migratetype)
  1782. {
  1783. const gfp_t wait = gfp_mask & __GFP_WAIT;
  1784. struct page *page = NULL;
  1785. int alloc_flags;
  1786. unsigned long pages_reclaimed = 0;
  1787. unsigned long did_some_progress;
  1788. bool sync_migration = false;
  1789. /*
  1790. * In the slowpath, we sanity check order to avoid ever trying to
  1791. * reclaim >= MAX_ORDER areas which will never succeed. Callers may
  1792. * be using allocators in order of preference for an area that is
  1793. * too large.
  1794. */
  1795. if (order >= MAX_ORDER) {
  1796. WARN_ON_ONCE(!(gfp_mask & __GFP_NOWARN));
  1797. return NULL;
  1798. }
  1799. /*
  1800. * GFP_THISNODE (meaning __GFP_THISNODE, __GFP_NORETRY and
  1801. * __GFP_NOWARN set) should not cause reclaim since the subsystem
  1802. * (f.e. slab) using GFP_THISNODE may choose to trigger reclaim
  1803. * using a larger set of nodes after it has established that the
  1804. * allowed per node queues are empty and that nodes are
  1805. * over allocated.
  1806. */
  1807. if (NUMA_BUILD && (gfp_mask & GFP_THISNODE) == GFP_THISNODE)
  1808. goto nopage;
  1809. restart:
  1810. if (!(gfp_mask & __GFP_NO_KSWAPD))
  1811. wake_all_kswapd(order, zonelist, high_zoneidx,
  1812. zone_idx(preferred_zone));
  1813. /*
  1814. * OK, we're below the kswapd watermark and have kicked background
  1815. * reclaim. Now things get more complex, so set up alloc_flags according
  1816. * to how we want to proceed.
  1817. */
  1818. alloc_flags = gfp_to_alloc_flags(gfp_mask);
  1819. /*
  1820. * Find the true preferred zone if the allocation is unconstrained by
  1821. * cpusets.
  1822. */
  1823. if (!(alloc_flags & ALLOC_CPUSET) && !nodemask)
  1824. first_zones_zonelist(zonelist, high_zoneidx, NULL,
  1825. &preferred_zone);
  1826. rebalance:
  1827. /* This is the last chance, in general, before the goto nopage. */
  1828. page = get_page_from_freelist(gfp_mask, nodemask, order, zonelist,
  1829. high_zoneidx, alloc_flags & ~ALLOC_NO_WATERMARKS,
  1830. preferred_zone, migratetype);
  1831. if (page)
  1832. goto got_pg;
  1833. /* Allocate without watermarks if the context allows */
  1834. if (alloc_flags & ALLOC_NO_WATERMARKS) {
  1835. page = __alloc_pages_high_priority(gfp_mask, order,
  1836. zonelist, high_zoneidx, nodemask,
  1837. preferred_zone, migratetype);
  1838. if (page)
  1839. goto got_pg;
  1840. }
  1841. /* Atomic allocations - we can't balance anything */
  1842. if (!wait)
  1843. goto nopage;
  1844. /* Avoid recursion of direct reclaim */
  1845. if (current->flags & PF_MEMALLOC)
  1846. goto nopage;
  1847. /* Avoid allocations with no watermarks from looping endlessly */
  1848. if (test_thread_flag(TIF_MEMDIE) && !(gfp_mask & __GFP_NOFAIL))
  1849. goto nopage;
  1850. /*
  1851. * Try direct compaction. The first pass is asynchronous. Subsequent
  1852. * attempts after direct reclaim are synchronous
  1853. */
  1854. page = __alloc_pages_direct_compact(gfp_mask, order,
  1855. zonelist, high_zoneidx,
  1856. nodemask,
  1857. alloc_flags, preferred_zone,
  1858. migratetype, &did_some_progress,
  1859. sync_migration);
  1860. if (page)
  1861. goto got_pg;
  1862. sync_migration = true;
  1863. /* Try direct reclaim and then allocating */
  1864. page = __alloc_pages_direct_reclaim(gfp_mask, order,
  1865. zonelist, high_zoneidx,
  1866. nodemask,
  1867. alloc_flags, preferred_zone,
  1868. migratetype, &did_some_progress);
  1869. if (page)
  1870. goto got_pg;
  1871. /*
  1872. * If we failed to make any progress reclaiming, then we are
  1873. * running out of options and have to consider going OOM
  1874. */
  1875. if (!did_some_progress) {
  1876. if ((gfp_mask & __GFP_FS) && !(gfp_mask & __GFP_NORETRY)) {
  1877. if (oom_killer_disabled)
  1878. goto nopage;
  1879. page = __alloc_pages_may_oom(gfp_mask, order,
  1880. zonelist, high_zoneidx,
  1881. nodemask, preferred_zone,
  1882. migratetype);
  1883. if (page)
  1884. goto got_pg;
  1885. if (!(gfp_mask & __GFP_NOFAIL)) {
  1886. /*
  1887. * The oom killer is not called for high-order
  1888. * allocations that may fail, so if no progress
  1889. * is being made, there are no other options and
  1890. * retrying is unlikely to help.
  1891. */
  1892. if (order > PAGE_ALLOC_COSTLY_ORDER)
  1893. goto nopage;
  1894. /*
  1895. * The oom killer is not called for lowmem
  1896. * allocations to prevent needlessly killing
  1897. * innocent tasks.
  1898. */
  1899. if (high_zoneidx < ZONE_NORMAL)
  1900. goto nopage;
  1901. }
  1902. goto restart;
  1903. }
  1904. }
  1905. /* Check if we should retry the allocation */
  1906. pages_reclaimed += did_some_progress;
  1907. if (should_alloc_retry(gfp_mask, order, pages_reclaimed)) {
  1908. /* Wait for some write requests to complete then retry */
  1909. wait_iff_congested(preferred_zone, BLK_RW_ASYNC, HZ/50);
  1910. goto rebalance;
  1911. } else {
  1912. /*
  1913. * High-order allocations do not necessarily loop after
  1914. * direct reclaim and reclaim/compaction depends on compaction
  1915. * being called after reclaim so call directly if necessary
  1916. */
  1917. page = __alloc_pages_direct_compact(gfp_mask, order,
  1918. zonelist, high_zoneidx,
  1919. nodemask,
  1920. alloc_flags, preferred_zone,
  1921. migratetype, &did_some_progress,
  1922. sync_migration);
  1923. if (page)
  1924. goto got_pg;
  1925. }
  1926. nopage:
  1927. warn_alloc_failed(gfp_mask, order, NULL);
  1928. return page;
  1929. got_pg:
  1930. if (kmemcheck_enabled)
  1931. kmemcheck_pagealloc_alloc(page, order, gfp_mask);
  1932. return page;
  1933. }
  1934. /*
  1935. * This is the 'heart' of the zoned buddy allocator.
  1936. */
  1937. struct page *
  1938. __alloc_pages_nodemask(gfp_t gfp_mask, unsigned int order,
  1939. struct zonelist *zonelist, nodemask_t *nodemask)
  1940. {
  1941. enum zone_type high_zoneidx = gfp_zone(gfp_mask);
  1942. struct zone *preferred_zone;
  1943. struct page *page;
  1944. int migratetype = allocflags_to_migratetype(gfp_mask);
  1945. gfp_mask &= gfp_allowed_mask;
  1946. lockdep_trace_alloc(gfp_mask);
  1947. might_sleep_if(gfp_mask & __GFP_WAIT);
  1948. if (should_fail_alloc_page(gfp_mask, order))
  1949. return NULL;
  1950. /*
  1951. * Check the zones suitable for the gfp_mask contain at least one
  1952. * valid zone. It's possible to have an empty zonelist as a result
  1953. * of GFP_THISNODE and a memoryless node
  1954. */
  1955. if (unlikely(!zonelist->_zonerefs->zone))
  1956. return NULL;
  1957. get_mems_allowed();
  1958. /* The preferred zone is used for statistics later */
  1959. first_zones_zonelist(zonelist, high_zoneidx,
  1960. nodemask ? : &cpuset_current_mems_allowed,
  1961. &preferred_zone);
  1962. if (!preferred_zone) {
  1963. put_mems_allowed();
  1964. return NULL;
  1965. }
  1966. /* First allocation attempt */
  1967. page = get_page_from_freelist(gfp_mask|__GFP_HARDWALL, nodemask, order,
  1968. zonelist, high_zoneidx, ALLOC_WMARK_LOW|ALLOC_CPUSET,
  1969. preferred_zone, migratetype);
  1970. if (unlikely(!page))
  1971. page = __alloc_pages_slowpath(gfp_mask, order,
  1972. zonelist, high_zoneidx, nodemask,
  1973. preferred_zone, migratetype);
  1974. put_mems_allowed();
  1975. trace_mm_page_alloc(page, order, gfp_mask, migratetype);
  1976. return page;
  1977. }
  1978. EXPORT_SYMBOL(__alloc_pages_nodemask);
  1979. /*
  1980. * Common helper functions.
  1981. */
  1982. unsigned long __get_free_pages(gfp_t gfp_mask, unsigned int order)
  1983. {
  1984. struct page *page;
  1985. /*
  1986. * __get_free_pages() returns a 32-bit address, which cannot represent
  1987. * a highmem page
  1988. */
  1989. VM_BUG_ON((gfp_mask & __GFP_HIGHMEM) != 0);
  1990. page = alloc_pages(gfp_mask, order);
  1991. if (!page)
  1992. return 0;
  1993. return (unsigned long) page_address(page);
  1994. }
  1995. EXPORT_SYMBOL(__get_free_pages);
  1996. unsigned long get_zeroed_page(gfp_t gfp_mask)
  1997. {
  1998. return __get_free_pages(gfp_mask | __GFP_ZERO, 0);
  1999. }
  2000. EXPORT_SYMBOL(get_zeroed_page);
  2001. void __pagevec_free(struct pagevec *pvec)
  2002. {
  2003. int i = pagevec_count(pvec);
  2004. while (--i >= 0) {
  2005. trace_mm_pagevec_free(pvec->pages[i], pvec->cold);
  2006. free_hot_cold_page(pvec->pages[i], pvec->cold);
  2007. }
  2008. }
  2009. void __free_pages(struct page *page, unsigned int order)
  2010. {
  2011. if (put_page_testzero(page)) {
  2012. if (order == 0)
  2013. free_hot_cold_page(page, 0);
  2014. else
  2015. __free_pages_ok(page, order);
  2016. }
  2017. }
  2018. EXPORT_SYMBOL(__free_pages);
  2019. void free_pages(unsigned long addr, unsigned int order)
  2020. {
  2021. if (addr != 0) {
  2022. VM_BUG_ON(!virt_addr_valid((void *)addr));
  2023. __free_pages(virt_to_page((void *)addr), order);
  2024. }
  2025. }
  2026. EXPORT_SYMBOL(free_pages);
  2027. static void *make_alloc_exact(unsigned long addr, unsigned order, size_t size)
  2028. {
  2029. if (addr) {
  2030. unsigned long alloc_end = addr + (PAGE_SIZE << order);
  2031. unsigned long used = addr + PAGE_ALIGN(size);
  2032. split_page(virt_to_page((void *)addr), order);
  2033. while (used < alloc_end) {
  2034. free_page(used);
  2035. used += PAGE_SIZE;
  2036. }
  2037. }
  2038. return (void *)addr;
  2039. }
  2040. /**
  2041. * alloc_pages_exact - allocate an exact number physically-contiguous pages.
  2042. * @size: the number of bytes to allocate
  2043. * @gfp_mask: GFP flags for the allocation
  2044. *
  2045. * This function is similar to alloc_pages(), except that it allocates the
  2046. * minimum number of pages to satisfy the request. alloc_pages() can only
  2047. * allocate memory in power-of-two pages.
  2048. *
  2049. * This function is also limited by MAX_ORDER.
  2050. *
  2051. * Memory allocated by this function must be released by free_pages_exact().
  2052. */
  2053. void *alloc_pages_exact(size_t size, gfp_t gfp_mask)
  2054. {
  2055. unsigned int order = get_order(size);
  2056. unsigned long addr;
  2057. addr = __get_free_pages(gfp_mask, order);
  2058. return make_alloc_exact(addr, order, size);
  2059. }
  2060. EXPORT_SYMBOL(alloc_pages_exact);
  2061. /**
  2062. * alloc_pages_exact_nid - allocate an exact number of physically-contiguous
  2063. * pages on a node.
  2064. * @nid: the preferred node ID where memory should be allocated
  2065. * @size: the number of bytes to allocate
  2066. * @gfp_mask: GFP flags for the allocation
  2067. *
  2068. * Like alloc_pages_exact(), but try to allocate on node nid first before falling
  2069. * back.
  2070. * Note this is not alloc_pages_exact_node() which allocates on a specific node,
  2071. * but is not exact.
  2072. */
  2073. void *alloc_pages_exact_nid(int nid, size_t size, gfp_t gfp_mask)
  2074. {
  2075. unsigned order = get_order(size);
  2076. struct page *p = alloc_pages_node(nid, gfp_mask, order);
  2077. if (!p)
  2078. return NULL;
  2079. return make_alloc_exact((unsigned long)page_address(p), order, size);
  2080. }
  2081. EXPORT_SYMBOL(alloc_pages_exact_nid);
  2082. /**
  2083. * free_pages_exact - release memory allocated via alloc_pages_exact()
  2084. * @virt: the value returned by alloc_pages_exact.
  2085. * @size: size of allocation, same value as passed to alloc_pages_exact().
  2086. *
  2087. * Release the memory allocated by a previous call to alloc_pages_exact.
  2088. */
  2089. void free_pages_exact(void *virt, size_t size)
  2090. {
  2091. unsigned long addr = (unsigned long)virt;
  2092. unsigned long end = addr + PAGE_ALIGN(size);
  2093. while (addr < end) {
  2094. free_page(addr);
  2095. addr += PAGE_SIZE;
  2096. }
  2097. }
  2098. EXPORT_SYMBOL(free_pages_exact);
  2099. static unsigned int nr_free_zone_pages(int offset)
  2100. {
  2101. struct zoneref *z;
  2102. struct zone *zone;
  2103. /* Just pick one node, since fallback list is circular */
  2104. unsigned int sum = 0;
  2105. struct zonelist *zonelist = node_zonelist(numa_node_id(), GFP_KERNEL);
  2106. for_each_zone_zonelist(zone, z, zonelist, offset) {
  2107. unsigned long size = zone->present_pages;
  2108. unsigned long high = high_wmark_pages(zone);
  2109. if (size > high)
  2110. sum += size - high;
  2111. }
  2112. return sum;
  2113. }
  2114. /*
  2115. * Amount of free RAM allocatable within ZONE_DMA and ZONE_NORMAL
  2116. */
  2117. unsigned int nr_free_buffer_pages(void)
  2118. {
  2119. return nr_free_zone_pages(gfp_zone(GFP_USER));
  2120. }
  2121. EXPORT_SYMBOL_GPL(nr_free_buffer_pages);
  2122. /*
  2123. * Amount of free RAM allocatable within all zones
  2124. */
  2125. unsigned int nr_free_pagecache_pages(void)
  2126. {
  2127. return nr_free_zone_pages(gfp_zone(GFP_HIGHUSER_MOVABLE));
  2128. }
  2129. static inline void show_node(struct zone *zone)
  2130. {
  2131. if (NUMA_BUILD)
  2132. printk("Node %d ", zone_to_nid(zone));
  2133. }
  2134. void si_meminfo(struct sysinfo *val)
  2135. {
  2136. val->totalram = totalram_pages;
  2137. val->sharedram = 0;
  2138. val->freeram = global_page_state(NR_FREE_PAGES);
  2139. val->bufferram = nr_blockdev_pages();
  2140. val->totalhigh = totalhigh_pages;
  2141. val->freehigh = nr_free_highpages();
  2142. val->mem_unit = PAGE_SIZE;
  2143. }
  2144. EXPORT_SYMBOL(si_meminfo);
  2145. #ifdef CONFIG_NUMA
  2146. void si_meminfo_node(struct sysinfo *val, int nid)
  2147. {
  2148. pg_data_t *pgdat = NODE_DATA(nid);
  2149. val->totalram = pgdat->node_present_pages;
  2150. val->freeram = node_page_state(nid, NR_FREE_PAGES);
  2151. #ifdef CONFIG_HIGHMEM
  2152. val->totalhigh = pgdat->node_zones[ZONE_HIGHMEM].present_pages;
  2153. val->freehigh = zone_page_state(&pgdat->node_zones[ZONE_HIGHMEM],
  2154. NR_FREE_PAGES);
  2155. #else
  2156. val->totalhigh = 0;
  2157. val->freehigh = 0;
  2158. #endif
  2159. val->mem_unit = PAGE_SIZE;
  2160. }
  2161. #endif
  2162. /*
  2163. * Determine whether the node should be displayed or not, depending on whether
  2164. * SHOW_MEM_FILTER_NODES was passed to show_free_areas().
  2165. */
  2166. bool skip_free_areas_node(unsigned int flags, int nid)
  2167. {
  2168. bool ret = false;
  2169. if (!(flags & SHOW_MEM_FILTER_NODES))
  2170. goto out;
  2171. get_mems_allowed();
  2172. ret = !node_isset(nid, cpuset_current_mems_allowed);
  2173. put_mems_allowed();
  2174. out:
  2175. return ret;
  2176. }
  2177. #define K(x) ((x) << (PAGE_SHIFT-10))
  2178. /*
  2179. * Show free area list (used inside shift_scroll-lock stuff)
  2180. * We also calculate the percentage fragmentation. We do this by counting the
  2181. * memory on each free list with the exception of the first item on the list.
  2182. * Suppresses nodes that are not allowed by current's cpuset if
  2183. * SHOW_MEM_FILTER_NODES is passed.
  2184. */
  2185. void show_free_areas(unsigned int filter)
  2186. {
  2187. int cpu;
  2188. struct zone *zone;
  2189. for_each_populated_zone(zone) {
  2190. if (skip_free_areas_node(filter, zone_to_nid(zone)))
  2191. continue;
  2192. show_node(zone);
  2193. printk("%s per-cpu:\n", zone->name);
  2194. for_each_online_cpu(cpu) {
  2195. struct per_cpu_pageset *pageset;
  2196. pageset = per_cpu_ptr(zone->pageset, cpu);
  2197. printk("CPU %4d: hi:%5d, btch:%4d usd:%4d\n",
  2198. cpu, pageset->pcp.high,
  2199. pageset->pcp.batch, pageset->pcp.count);
  2200. }
  2201. }
  2202. printk("active_anon:%lu inactive_anon:%lu isolated_anon:%lu\n"
  2203. " active_file:%lu inactive_file:%lu isolated_file:%lu\n"
  2204. " unevictable:%lu"
  2205. " dirty:%lu writeback:%lu unstable:%lu\n"
  2206. " free:%lu slab_reclaimable:%lu slab_unreclaimable:%lu\n"
  2207. " mapped:%lu shmem:%lu pagetables:%lu bounce:%lu\n",
  2208. global_page_state(NR_ACTIVE_ANON),
  2209. global_page_state(NR_INACTIVE_ANON),
  2210. global_page_state(NR_ISOLATED_ANON),
  2211. global_page_state(NR_ACTIVE_FILE),
  2212. global_page_state(NR_INACTIVE_FILE),
  2213. global_page_state(NR_ISOLATED_FILE),
  2214. global_page_state(NR_UNEVICTABLE),
  2215. global_page_state(NR_FILE_DIRTY),
  2216. global_page_state(NR_WRITEBACK),
  2217. global_page_state(NR_UNSTABLE_NFS),
  2218. global_page_state(NR_FREE_PAGES),
  2219. global_page_state(NR_SLAB_RECLAIMABLE),
  2220. global_page_state(NR_SLAB_UNRECLAIMABLE),
  2221. global_page_state(NR_FILE_MAPPED),
  2222. global_page_state(NR_SHMEM),
  2223. global_page_state(NR_PAGETABLE),
  2224. global_page_state(NR_BOUNCE));
  2225. for_each_populated_zone(zone) {
  2226. int i;
  2227. if (skip_free_areas_node(filter, zone_to_nid(zone)))
  2228. continue;
  2229. show_node(zone);
  2230. printk("%s"
  2231. " free:%lukB"
  2232. " min:%lukB"
  2233. " low:%lukB"
  2234. " high:%lukB"
  2235. " active_anon:%lukB"
  2236. " inactive_anon:%lukB"
  2237. " active_file:%lukB"
  2238. " inactive_file:%lukB"
  2239. " unevictable:%lukB"
  2240. " isolated(anon):%lukB"
  2241. " isolated(file):%lukB"
  2242. " present:%lukB"
  2243. " mlocked:%lukB"
  2244. " dirty:%lukB"
  2245. " writeback:%lukB"
  2246. " mapped:%lukB"
  2247. " shmem:%lukB"
  2248. " slab_reclaimable:%lukB"
  2249. " slab_unreclaimable:%lukB"
  2250. " kernel_stack:%lukB"
  2251. " pagetables:%lukB"
  2252. " unstable:%lukB"
  2253. " bounce:%lukB"
  2254. " writeback_tmp:%lukB"
  2255. " pages_scanned:%lu"
  2256. " all_unreclaimable? %s"
  2257. "\n",
  2258. zone->name,
  2259. K(zone_page_state(zone, NR_FREE_PAGES)),
  2260. K(min_wmark_pages(zone)),
  2261. K(low_wmark_pages(zone)),
  2262. K(high_wmark_pages(zone)),
  2263. K(zone_page_state(zone, NR_ACTIVE_ANON)),
  2264. K(zone_page_state(zone, NR_INACTIVE_ANON)),
  2265. K(zone_page_state(zone, NR_ACTIVE_FILE)),
  2266. K(zone_page_state(zone, NR_INACTIVE_FILE)),
  2267. K(zone_page_state(zone, NR_UNEVICTABLE)),
  2268. K(zone_page_state(zone, NR_ISOLATED_ANON)),
  2269. K(zone_page_state(zone, NR_ISOLATED_FILE)),
  2270. K(zone->present_pages),
  2271. K(zone_page_state(zone, NR_MLOCK)),
  2272. K(zone_page_state(zone, NR_FILE_DIRTY)),
  2273. K(zone_page_state(zone, NR_WRITEBACK)),
  2274. K(zone_page_state(zone, NR_FILE_MAPPED)),
  2275. K(zone_page_state(zone, NR_SHMEM)),
  2276. K(zone_page_state(zone, NR_SLAB_RECLAIMABLE)),
  2277. K(zone_page_state(zone, NR_SLAB_UNRECLAIMABLE)),
  2278. zone_page_state(zone, NR_KERNEL_STACK) *
  2279. THREAD_SIZE / 1024,
  2280. K(zone_page_state(zone, NR_PAGETABLE)),
  2281. K(zone_page_state(zone, NR_UNSTABLE_NFS)),
  2282. K(zone_page_state(zone, NR_BOUNCE)),
  2283. K(zone_page_state(zone, NR_WRITEBACK_TEMP)),
  2284. zone->pages_scanned,
  2285. (zone->all_unreclaimable ? "yes" : "no")
  2286. );
  2287. printk("lowmem_reserve[]:");
  2288. for (i = 0; i < MAX_NR_ZONES; i++)
  2289. printk(" %lu", zone->lowmem_reserve[i]);
  2290. printk("\n");
  2291. }
  2292. for_each_populated_zone(zone) {
  2293. unsigned long nr[MAX_ORDER], flags, order, total = 0;
  2294. if (skip_free_areas_node(filter, zone_to_nid(zone)))
  2295. continue;
  2296. show_node(zone);
  2297. printk("%s: ", zone->name);
  2298. spin_lock_irqsave(&zone->lock, flags);
  2299. for (order = 0; order < MAX_ORDER; order++) {
  2300. nr[order] = zone->free_area[order].nr_free;
  2301. total += nr[order] << order;
  2302. }
  2303. spin_unlock_irqrestore(&zone->lock, flags);
  2304. for (order = 0; order < MAX_ORDER; order++)
  2305. printk("%lu*%lukB ", nr[order], K(1UL) << order);
  2306. printk("= %lukB\n", K(total));
  2307. }
  2308. printk("%ld total pagecache pages\n", global_page_state(NR_FILE_PAGES));
  2309. show_swap_cache_info();
  2310. }
  2311. static void zoneref_set_zone(struct zone *zone, struct zoneref *zoneref)
  2312. {
  2313. zoneref->zone = zone;
  2314. zoneref->zone_idx = zone_idx(zone);
  2315. }
  2316. /*
  2317. * Builds allocation fallback zone lists.
  2318. *
  2319. * Add all populated zones of a node to the zonelist.
  2320. */
  2321. static int build_zonelists_node(pg_data_t *pgdat, struct zonelist *zonelist,
  2322. int nr_zones, enum zone_type zone_type)
  2323. {
  2324. struct zone *zone;
  2325. BUG_ON(zone_type >= MAX_NR_ZONES);
  2326. zone_type++;
  2327. do {
  2328. zone_type--;
  2329. zone = pgdat->node_zones + zone_type;
  2330. if (populated_zone(zone)) {
  2331. zoneref_set_zone(zone,
  2332. &zonelist->_zonerefs[nr_zones++]);
  2333. check_highest_zone(zone_type);
  2334. }
  2335. } while (zone_type);
  2336. return nr_zones;
  2337. }
  2338. /*
  2339. * zonelist_order:
  2340. * 0 = automatic detection of better ordering.
  2341. * 1 = order by ([node] distance, -zonetype)
  2342. * 2 = order by (-zonetype, [node] distance)
  2343. *
  2344. * If not NUMA, ZONELIST_ORDER_ZONE and ZONELIST_ORDER_NODE will create
  2345. * the same zonelist. So only NUMA can configure this param.
  2346. */
  2347. #define ZONELIST_ORDER_DEFAULT 0
  2348. #define ZONELIST_ORDER_NODE 1
  2349. #define ZONELIST_ORDER_ZONE 2
  2350. /* zonelist order in the kernel.
  2351. * set_zonelist_order() will set this to NODE or ZONE.
  2352. */
  2353. static int current_zonelist_order = ZONELIST_ORDER_DEFAULT;
  2354. static char zonelist_order_name[3][8] = {"Default", "Node", "Zone"};
  2355. #ifdef CONFIG_NUMA
  2356. /* The value user specified ....changed by config */
  2357. static int user_zonelist_order = ZONELIST_ORDER_DEFAULT;
  2358. /* string for sysctl */
  2359. #define NUMA_ZONELIST_ORDER_LEN 16
  2360. char numa_zonelist_order[16] = "default";
  2361. /*
  2362. * interface for configure zonelist ordering.
  2363. * command line option "numa_zonelist_order"
  2364. * = "[dD]efault - default, automatic configuration.
  2365. * = "[nN]ode - order by node locality, then by zone within node
  2366. * = "[zZ]one - order by zone, then by locality within zone
  2367. */
  2368. static int __parse_numa_zonelist_order(char *s)
  2369. {
  2370. if (*s == 'd' || *s == 'D') {
  2371. user_zonelist_order = ZONELIST_ORDER_DEFAULT;
  2372. } else if (*s == 'n' || *s == 'N') {
  2373. user_zonelist_order = ZONELIST_ORDER_NODE;
  2374. } else if (*s == 'z' || *s == 'Z') {
  2375. user_zonelist_order = ZONELIST_ORDER_ZONE;
  2376. } else {
  2377. printk(KERN_WARNING
  2378. "Ignoring invalid numa_zonelist_order value: "
  2379. "%s\n", s);
  2380. return -EINVAL;
  2381. }
  2382. return 0;
  2383. }
  2384. static __init int setup_numa_zonelist_order(char *s)
  2385. {
  2386. int ret;
  2387. if (!s)
  2388. return 0;
  2389. ret = __parse_numa_zonelist_order(s);
  2390. if (ret == 0)
  2391. strlcpy(numa_zonelist_order, s, NUMA_ZONELIST_ORDER_LEN);
  2392. return ret;
  2393. }
  2394. early_param("numa_zonelist_order", setup_numa_zonelist_order);
  2395. /*
  2396. * sysctl handler for numa_zonelist_order
  2397. */
  2398. int numa_zonelist_order_handler(ctl_table *table, int write,
  2399. void __user *buffer, size_t *length,
  2400. loff_t *ppos)
  2401. {
  2402. char saved_string[NUMA_ZONELIST_ORDER_LEN];
  2403. int ret;
  2404. static DEFINE_MUTEX(zl_order_mutex);
  2405. mutex_lock(&zl_order_mutex);
  2406. if (write)
  2407. strcpy(saved_string, (char*)table->data);
  2408. ret = proc_dostring(table, write, buffer, length, ppos);
  2409. if (ret)
  2410. goto out;
  2411. if (write) {
  2412. int oldval = user_zonelist_order;
  2413. if (__parse_numa_zonelist_order((char*)table->data)) {
  2414. /*
  2415. * bogus value. restore saved string
  2416. */
  2417. strncpy((char*)table->data, saved_string,
  2418. NUMA_ZONELIST_ORDER_LEN);
  2419. user_zonelist_order = oldval;
  2420. } else if (oldval != user_zonelist_order) {
  2421. mutex_lock(&zonelists_mutex);
  2422. build_all_zonelists(NULL);
  2423. mutex_unlock(&zonelists_mutex);
  2424. }
  2425. }
  2426. out:
  2427. mutex_unlock(&zl_order_mutex);
  2428. return ret;
  2429. }
  2430. #define MAX_NODE_LOAD (nr_online_nodes)
  2431. static int node_load[MAX_NUMNODES];
  2432. /**
  2433. * find_next_best_node - find the next node that should appear in a given node's fallback list
  2434. * @node: node whose fallback list we're appending
  2435. * @used_node_mask: nodemask_t of already used nodes
  2436. *
  2437. * We use a number of factors to determine which is the next node that should
  2438. * appear on a given node's fallback list. The node should not have appeared
  2439. * already in @node's fallback list, and it should be the next closest node
  2440. * according to the distance array (which contains arbitrary distance values
  2441. * from each node to each node in the system), and should also prefer nodes
  2442. * with no CPUs, since presumably they'll have very little allocation pressure
  2443. * on them otherwise.
  2444. * It returns -1 if no node is found.
  2445. */
  2446. static int find_next_best_node(int node, nodemask_t *used_node_mask)
  2447. {
  2448. int n, val;
  2449. int min_val = INT_MAX;
  2450. int best_node = -1;
  2451. const struct cpumask *tmp = cpumask_of_node(0);
  2452. /* Use the local node if we haven't already */
  2453. if (!node_isset(node, *used_node_mask)) {
  2454. node_set(node, *used_node_mask);
  2455. return node;
  2456. }
  2457. for_each_node_state(n, N_HIGH_MEMORY) {
  2458. /* Don't want a node to appear more than once */
  2459. if (node_isset(n, *used_node_mask))
  2460. continue;
  2461. /* Use the distance array to find the distance */
  2462. val = node_distance(node, n);
  2463. /* Penalize nodes under us ("prefer the next node") */
  2464. val += (n < node);
  2465. /* Give preference to headless and unused nodes */
  2466. tmp = cpumask_of_node(n);
  2467. if (!cpumask_empty(tmp))
  2468. val += PENALTY_FOR_NODE_WITH_CPUS;
  2469. /* Slight preference for less loaded node */
  2470. val *= (MAX_NODE_LOAD*MAX_NUMNODES);
  2471. val += node_load[n];
  2472. if (val < min_val) {
  2473. min_val = val;
  2474. best_node = n;
  2475. }
  2476. }
  2477. if (best_node >= 0)
  2478. node_set(best_node, *used_node_mask);
  2479. return best_node;
  2480. }
  2481. /*
  2482. * Build zonelists ordered by node and zones within node.
  2483. * This results in maximum locality--normal zone overflows into local
  2484. * DMA zone, if any--but risks exhausting DMA zone.
  2485. */
  2486. static void build_zonelists_in_node_order(pg_data_t *pgdat, int node)
  2487. {
  2488. int j;
  2489. struct zonelist *zonelist;
  2490. zonelist = &pgdat->node_zonelists[0];
  2491. for (j = 0; zonelist->_zonerefs[j].zone != NULL; j++)
  2492. ;
  2493. j = build_zonelists_node(NODE_DATA(node), zonelist, j,
  2494. MAX_NR_ZONES - 1);
  2495. zonelist->_zonerefs[j].zone = NULL;
  2496. zonelist->_zonerefs[j].zone_idx = 0;
  2497. }
  2498. /*
  2499. * Build gfp_thisnode zonelists
  2500. */
  2501. static void build_thisnode_zonelists(pg_data_t *pgdat)
  2502. {
  2503. int j;
  2504. struct zonelist *zonelist;
  2505. zonelist = &pgdat->node_zonelists[1];
  2506. j = build_zonelists_node(pgdat, zonelist, 0, MAX_NR_ZONES - 1);
  2507. zonelist->_zonerefs[j].zone = NULL;
  2508. zonelist->_zonerefs[j].zone_idx = 0;
  2509. }
  2510. /*
  2511. * Build zonelists ordered by zone and nodes within zones.
  2512. * This results in conserving DMA zone[s] until all Normal memory is
  2513. * exhausted, but results in overflowing to remote node while memory
  2514. * may still exist in local DMA zone.
  2515. */
  2516. static int node_order[MAX_NUMNODES];
  2517. static void build_zonelists_in_zone_order(pg_data_t *pgdat, int nr_nodes)
  2518. {
  2519. int pos, j, node;
  2520. int zone_type; /* needs to be signed */
  2521. struct zone *z;
  2522. struct zonelist *zonelist;
  2523. zonelist = &pgdat->node_zonelists[0];
  2524. pos = 0;
  2525. for (zone_type = MAX_NR_ZONES - 1; zone_type >= 0; zone_type--) {
  2526. for (j = 0; j < nr_nodes; j++) {
  2527. node = node_order[j];
  2528. z = &NODE_DATA(node)->node_zones[zone_type];
  2529. if (populated_zone(z)) {
  2530. zoneref_set_zone(z,
  2531. &zonelist->_zonerefs[pos++]);
  2532. check_highest_zone(zone_type);
  2533. }
  2534. }
  2535. }
  2536. zonelist->_zonerefs[pos].zone = NULL;
  2537. zonelist->_zonerefs[pos].zone_idx = 0;
  2538. }
  2539. static int default_zonelist_order(void)
  2540. {
  2541. int nid, zone_type;
  2542. unsigned long low_kmem_size,total_size;
  2543. struct zone *z;
  2544. int average_size;
  2545. /*
  2546. * ZONE_DMA and ZONE_DMA32 can be very small area in the system.
  2547. * If they are really small and used heavily, the system can fall
  2548. * into OOM very easily.
  2549. * This function detect ZONE_DMA/DMA32 size and configures zone order.
  2550. */
  2551. /* Is there ZONE_NORMAL ? (ex. ppc has only DMA zone..) */
  2552. low_kmem_size = 0;
  2553. total_size = 0;
  2554. for_each_online_node(nid) {
  2555. for (zone_type = 0; zone_type < MAX_NR_ZONES; zone_type++) {
  2556. z = &NODE_DATA(nid)->node_zones[zone_type];
  2557. if (populated_zone(z)) {
  2558. if (zone_type < ZONE_NORMAL)
  2559. low_kmem_size += z->present_pages;
  2560. total_size += z->present_pages;
  2561. } else if (zone_type == ZONE_NORMAL) {
  2562. /*
  2563. * If any node has only lowmem, then node order
  2564. * is preferred to allow kernel allocations
  2565. * locally; otherwise, they can easily infringe
  2566. * on other nodes when there is an abundance of
  2567. * lowmem available to allocate from.
  2568. */
  2569. return ZONELIST_ORDER_NODE;
  2570. }
  2571. }
  2572. }
  2573. if (!low_kmem_size || /* there are no DMA area. */
  2574. low_kmem_size > total_size/2) /* DMA/DMA32 is big. */
  2575. return ZONELIST_ORDER_NODE;
  2576. /*
  2577. * look into each node's config.
  2578. * If there is a node whose DMA/DMA32 memory is very big area on
  2579. * local memory, NODE_ORDER may be suitable.
  2580. */
  2581. average_size = total_size /
  2582. (nodes_weight(node_states[N_HIGH_MEMORY]) + 1);
  2583. for_each_online_node(nid) {
  2584. low_kmem_size = 0;
  2585. total_size = 0;
  2586. for (zone_type = 0; zone_type < MAX_NR_ZONES; zone_type++) {
  2587. z = &NODE_DATA(nid)->node_zones[zone_type];
  2588. if (populated_zone(z)) {
  2589. if (zone_type < ZONE_NORMAL)
  2590. low_kmem_size += z->present_pages;
  2591. total_size += z->present_pages;
  2592. }
  2593. }
  2594. if (low_kmem_size &&
  2595. total_size > average_size && /* ignore small node */
  2596. low_kmem_size > total_size * 70/100)
  2597. return ZONELIST_ORDER_NODE;
  2598. }
  2599. return ZONELIST_ORDER_ZONE;
  2600. }
  2601. static void set_zonelist_order(void)
  2602. {
  2603. if (user_zonelist_order == ZONELIST_ORDER_DEFAULT)
  2604. current_zonelist_order = default_zonelist_order();
  2605. else
  2606. current_zonelist_order = user_zonelist_order;
  2607. }
  2608. static void build_zonelists(pg_data_t *pgdat)
  2609. {
  2610. int j, node, load;
  2611. enum zone_type i;
  2612. nodemask_t used_mask;
  2613. int local_node, prev_node;
  2614. struct zonelist *zonelist;
  2615. int order = current_zonelist_order;
  2616. /* initialize zonelists */
  2617. for (i = 0; i < MAX_ZONELISTS; i++) {
  2618. zonelist = pgdat->node_zonelists + i;
  2619. zonelist->_zonerefs[0].zone = NULL;
  2620. zonelist->_zonerefs[0].zone_idx = 0;
  2621. }
  2622. /* NUMA-aware ordering of nodes */
  2623. local_node = pgdat->node_id;
  2624. load = nr_online_nodes;
  2625. prev_node = local_node;
  2626. nodes_clear(used_mask);
  2627. memset(node_order, 0, sizeof(node_order));
  2628. j = 0;
  2629. while ((node = find_next_best_node(local_node, &used_mask)) >= 0) {
  2630. int distance = node_distance(local_node, node);
  2631. /*
  2632. * If another node is sufficiently far away then it is better
  2633. * to reclaim pages in a zone before going off node.
  2634. */
  2635. if (distance > RECLAIM_DISTANCE)
  2636. zone_reclaim_mode = 1;
  2637. /*
  2638. * We don't want to pressure a particular node.
  2639. * So adding penalty to the first node in same
  2640. * distance group to make it round-robin.
  2641. */
  2642. if (distance != node_distance(local_node, prev_node))
  2643. node_load[node] = load;
  2644. prev_node = node;
  2645. load--;
  2646. if (order == ZONELIST_ORDER_NODE)
  2647. build_zonelists_in_node_order(pgdat, node);
  2648. else
  2649. node_order[j++] = node; /* remember order */
  2650. }
  2651. if (order == ZONELIST_ORDER_ZONE) {
  2652. /* calculate node order -- i.e., DMA last! */
  2653. build_zonelists_in_zone_order(pgdat, j);
  2654. }
  2655. build_thisnode_zonelists(pgdat);
  2656. }
  2657. /* Construct the zonelist performance cache - see further mmzone.h */
  2658. static void build_zonelist_cache(pg_data_t *pgdat)
  2659. {
  2660. struct zonelist *zonelist;
  2661. struct zonelist_cache *zlc;
  2662. struct zoneref *z;
  2663. zonelist = &pgdat->node_zonelists[0];
  2664. zonelist->zlcache_ptr = zlc = &zonelist->zlcache;
  2665. bitmap_zero(zlc->fullzones, MAX_ZONES_PER_ZONELIST);
  2666. for (z = zonelist->_zonerefs; z->zone; z++)
  2667. zlc->z_to_n[z - zonelist->_zonerefs] = zonelist_node_idx(z);
  2668. }
  2669. #ifdef CONFIG_HAVE_MEMORYLESS_NODES
  2670. /*
  2671. * Return node id of node used for "local" allocations.
  2672. * I.e., first node id of first zone in arg node's generic zonelist.
  2673. * Used for initializing percpu 'numa_mem', which is used primarily
  2674. * for kernel allocations, so use GFP_KERNEL flags to locate zonelist.
  2675. */
  2676. int local_memory_node(int node)
  2677. {
  2678. struct zone *zone;
  2679. (void)first_zones_zonelist(node_zonelist(node, GFP_KERNEL),
  2680. gfp_zone(GFP_KERNEL),
  2681. NULL,
  2682. &zone);
  2683. return zone->node;
  2684. }
  2685. #endif
  2686. #else /* CONFIG_NUMA */
  2687. static void set_zonelist_order(void)
  2688. {
  2689. current_zonelist_order = ZONELIST_ORDER_ZONE;
  2690. }
  2691. static void build_zonelists(pg_data_t *pgdat)
  2692. {
  2693. int node, local_node;
  2694. enum zone_type j;
  2695. struct zonelist *zonelist;
  2696. local_node = pgdat->node_id;
  2697. zonelist = &pgdat->node_zonelists[0];
  2698. j = build_zonelists_node(pgdat, zonelist, 0, MAX_NR_ZONES - 1);
  2699. /*
  2700. * Now we build the zonelist so that it contains the zones
  2701. * of all the other nodes.
  2702. * We don't want to pressure a particular node, so when
  2703. * building the zones for node N, we make sure that the
  2704. * zones coming right after the local ones are those from
  2705. * node N+1 (modulo N)
  2706. */
  2707. for (node = local_node + 1; node < MAX_NUMNODES; node++) {
  2708. if (!node_online(node))
  2709. continue;
  2710. j = build_zonelists_node(NODE_DATA(node), zonelist, j,
  2711. MAX_NR_ZONES - 1);
  2712. }
  2713. for (node = 0; node < local_node; node++) {
  2714. if (!node_online(node))
  2715. continue;
  2716. j = build_zonelists_node(NODE_DATA(node), zonelist, j,
  2717. MAX_NR_ZONES - 1);
  2718. }
  2719. zonelist->_zonerefs[j].zone = NULL;
  2720. zonelist->_zonerefs[j].zone_idx = 0;
  2721. }
  2722. /* non-NUMA variant of zonelist performance cache - just NULL zlcache_ptr */
  2723. static void build_zonelist_cache(pg_data_t *pgdat)
  2724. {
  2725. pgdat->node_zonelists[0].zlcache_ptr = NULL;
  2726. }
  2727. #endif /* CONFIG_NUMA */
  2728. /*
  2729. * Boot pageset table. One per cpu which is going to be used for all
  2730. * zones and all nodes. The parameters will be set in such a way
  2731. * that an item put on a list will immediately be handed over to
  2732. * the buddy list. This is safe since pageset manipulation is done
  2733. * with interrupts disabled.
  2734. *
  2735. * The boot_pagesets must be kept even after bootup is complete for
  2736. * unused processors and/or zones. They do play a role for bootstrapping
  2737. * hotplugged processors.
  2738. *
  2739. * zoneinfo_show() and maybe other functions do
  2740. * not check if the processor is online before following the pageset pointer.
  2741. * Other parts of the kernel may not check if the zone is available.
  2742. */
  2743. static void setup_pageset(struct per_cpu_pageset *p, unsigned long batch);
  2744. static DEFINE_PER_CPU(struct per_cpu_pageset, boot_pageset);
  2745. static void setup_zone_pageset(struct zone *zone);
  2746. /*
  2747. * Global mutex to protect against size modification of zonelists
  2748. * as well as to serialize pageset setup for the new populated zone.
  2749. */
  2750. DEFINE_MUTEX(zonelists_mutex);
  2751. /* return values int ....just for stop_machine() */
  2752. static __init_refok int __build_all_zonelists(void *data)
  2753. {
  2754. int nid;
  2755. int cpu;
  2756. #ifdef CONFIG_NUMA
  2757. memset(node_load, 0, sizeof(node_load));
  2758. #endif
  2759. for_each_online_node(nid) {
  2760. pg_data_t *pgdat = NODE_DATA(nid);
  2761. build_zonelists(pgdat);
  2762. build_zonelist_cache(pgdat);
  2763. }
  2764. /*
  2765. * Initialize the boot_pagesets that are going to be used
  2766. * for bootstrapping processors. The real pagesets for
  2767. * each zone will be allocated later when the per cpu
  2768. * allocator is available.
  2769. *
  2770. * boot_pagesets are used also for bootstrapping offline
  2771. * cpus if the system is already booted because the pagesets
  2772. * are needed to initialize allocators on a specific cpu too.
  2773. * F.e. the percpu allocator needs the page allocator which
  2774. * needs the percpu allocator in order to allocate its pagesets
  2775. * (a chicken-egg dilemma).
  2776. */
  2777. for_each_possible_cpu(cpu) {
  2778. setup_pageset(&per_cpu(boot_pageset, cpu), 0);
  2779. #ifdef CONFIG_HAVE_MEMORYLESS_NODES
  2780. /*
  2781. * We now know the "local memory node" for each node--
  2782. * i.e., the node of the first zone in the generic zonelist.
  2783. * Set up numa_mem percpu variable for on-line cpus. During
  2784. * boot, only the boot cpu should be on-line; we'll init the
  2785. * secondary cpus' numa_mem as they come on-line. During
  2786. * node/memory hotplug, we'll fixup all on-line cpus.
  2787. */
  2788. if (cpu_online(cpu))
  2789. set_cpu_numa_mem(cpu, local_memory_node(cpu_to_node(cpu)));
  2790. #endif
  2791. }
  2792. return 0;
  2793. }
  2794. /*
  2795. * Called with zonelists_mutex held always
  2796. * unless system_state == SYSTEM_BOOTING.
  2797. */
  2798. void __ref build_all_zonelists(void *data)
  2799. {
  2800. set_zonelist_order();
  2801. if (system_state == SYSTEM_BOOTING) {
  2802. __build_all_zonelists(NULL);
  2803. mminit_verify_zonelist();
  2804. cpuset_init_current_mems_allowed();
  2805. } else {
  2806. /* we have to stop all cpus to guarantee there is no user
  2807. of zonelist */
  2808. #ifdef CONFIG_MEMORY_HOTPLUG
  2809. if (data)
  2810. setup_zone_pageset((struct zone *)data);
  2811. #endif
  2812. stop_machine(__build_all_zonelists, NULL, NULL);
  2813. /* cpuset refresh routine should be here */
  2814. }
  2815. vm_total_pages = nr_free_pagecache_pages();
  2816. /*
  2817. * Disable grouping by mobility if the number of pages in the
  2818. * system is too low to allow the mechanism to work. It would be
  2819. * more accurate, but expensive to check per-zone. This check is
  2820. * made on memory-hotadd so a system can start with mobility
  2821. * disabled and enable it later
  2822. */
  2823. if (vm_total_pages < (pageblock_nr_pages * MIGRATE_TYPES))
  2824. page_group_by_mobility_disabled = 1;
  2825. else
  2826. page_group_by_mobility_disabled = 0;
  2827. printk("Built %i zonelists in %s order, mobility grouping %s. "
  2828. "Total pages: %ld\n",
  2829. nr_online_nodes,
  2830. zonelist_order_name[current_zonelist_order],
  2831. page_group_by_mobility_disabled ? "off" : "on",
  2832. vm_total_pages);
  2833. #ifdef CONFIG_NUMA
  2834. printk("Policy zone: %s\n", zone_names[policy_zone]);
  2835. #endif
  2836. }
  2837. /*
  2838. * Helper functions to size the waitqueue hash table.
  2839. * Essentially these want to choose hash table sizes sufficiently
  2840. * large so that collisions trying to wait on pages are rare.
  2841. * But in fact, the number of active page waitqueues on typical
  2842. * systems is ridiculously low, less than 200. So this is even
  2843. * conservative, even though it seems large.
  2844. *
  2845. * The constant PAGES_PER_WAITQUEUE specifies the ratio of pages to
  2846. * waitqueues, i.e. the size of the waitq table given the number of pages.
  2847. */
  2848. #define PAGES_PER_WAITQUEUE 256
  2849. #ifndef CONFIG_MEMORY_HOTPLUG
  2850. static inline unsigned long wait_table_hash_nr_entries(unsigned long pages)
  2851. {
  2852. unsigned long size = 1;
  2853. pages /= PAGES_PER_WAITQUEUE;
  2854. while (size < pages)
  2855. size <<= 1;
  2856. /*
  2857. * Once we have dozens or even hundreds of threads sleeping
  2858. * on IO we've got bigger problems than wait queue collision.
  2859. * Limit the size of the wait table to a reasonable size.
  2860. */
  2861. size = min(size, 4096UL);
  2862. return max(size, 4UL);
  2863. }
  2864. #else
  2865. /*
  2866. * A zone's size might be changed by hot-add, so it is not possible to determine
  2867. * a suitable size for its wait_table. So we use the maximum size now.
  2868. *
  2869. * The max wait table size = 4096 x sizeof(wait_queue_head_t). ie:
  2870. *
  2871. * i386 (preemption config) : 4096 x 16 = 64Kbyte.
  2872. * ia64, x86-64 (no preemption): 4096 x 20 = 80Kbyte.
  2873. * ia64, x86-64 (preemption) : 4096 x 24 = 96Kbyte.
  2874. *
  2875. * The maximum entries are prepared when a zone's memory is (512K + 256) pages
  2876. * or more by the traditional way. (See above). It equals:
  2877. *
  2878. * i386, x86-64, powerpc(4K page size) : = ( 2G + 1M)byte.
  2879. * ia64(16K page size) : = ( 8G + 4M)byte.
  2880. * powerpc (64K page size) : = (32G +16M)byte.
  2881. */
  2882. static inline unsigned long wait_table_hash_nr_entries(unsigned long pages)
  2883. {
  2884. return 4096UL;
  2885. }
  2886. #endif
  2887. /*
  2888. * This is an integer logarithm so that shifts can be used later
  2889. * to extract the more random high bits from the multiplicative
  2890. * hash function before the remainder is taken.
  2891. */
  2892. static inline unsigned long wait_table_bits(unsigned long size)
  2893. {
  2894. return ffz(~size);
  2895. }
  2896. #define LONG_ALIGN(x) (((x)+(sizeof(long))-1)&~((sizeof(long))-1))
  2897. /*
  2898. * Check if a pageblock contains reserved pages
  2899. */
  2900. static int pageblock_is_reserved(unsigned long start_pfn, unsigned long end_pfn)
  2901. {
  2902. unsigned long pfn;
  2903. for (pfn = start_pfn; pfn < end_pfn; pfn++) {
  2904. if (!pfn_valid_within(pfn) || PageReserved(pfn_to_page(pfn)))
  2905. return 1;
  2906. }
  2907. return 0;
  2908. }
  2909. /*
  2910. * Mark a number of pageblocks as MIGRATE_RESERVE. The number
  2911. * of blocks reserved is based on min_wmark_pages(zone). The memory within
  2912. * the reserve will tend to store contiguous free pages. Setting min_free_kbytes
  2913. * higher will lead to a bigger reserve which will get freed as contiguous
  2914. * blocks as reclaim kicks in
  2915. */
  2916. static void setup_zone_migrate_reserve(struct zone *zone)
  2917. {
  2918. unsigned long start_pfn, pfn, end_pfn, block_end_pfn;
  2919. struct page *page;
  2920. unsigned long block_migratetype;
  2921. int reserve;
  2922. /* Get the start pfn, end pfn and the number of blocks to reserve */
  2923. start_pfn = zone->zone_start_pfn;
  2924. end_pfn = start_pfn + zone->spanned_pages;
  2925. reserve = roundup(min_wmark_pages(zone), pageblock_nr_pages) >>
  2926. pageblock_order;
  2927. /*
  2928. * Reserve blocks are generally in place to help high-order atomic
  2929. * allocations that are short-lived. A min_free_kbytes value that
  2930. * would result in more than 2 reserve blocks for atomic allocations
  2931. * is assumed to be in place to help anti-fragmentation for the
  2932. * future allocation of hugepages at runtime.
  2933. */
  2934. reserve = min(2, reserve);
  2935. for (pfn = start_pfn; pfn < end_pfn; pfn += pageblock_nr_pages) {
  2936. if (!pfn_valid(pfn))
  2937. continue;
  2938. page = pfn_to_page(pfn);
  2939. /* Watch out for overlapping nodes */
  2940. if (page_to_nid(page) != zone_to_nid(zone))
  2941. continue;
  2942. /* Blocks with reserved pages will never free, skip them. */
  2943. block_end_pfn = min(pfn + pageblock_nr_pages, end_pfn);
  2944. if (pageblock_is_reserved(pfn, block_end_pfn))
  2945. continue;
  2946. block_migratetype = get_pageblock_migratetype(page);
  2947. /* If this block is reserved, account for it */
  2948. if (reserve > 0 && block_migratetype == MIGRATE_RESERVE) {
  2949. reserve--;
  2950. continue;
  2951. }
  2952. /* Suitable for reserving if this block is movable */
  2953. if (reserve > 0 && block_migratetype == MIGRATE_MOVABLE) {
  2954. set_pageblock_migratetype(page, MIGRATE_RESERVE);
  2955. move_freepages_block(zone, page, MIGRATE_RESERVE);
  2956. reserve--;
  2957. continue;
  2958. }
  2959. /*
  2960. * If the reserve is met and this is a previous reserved block,
  2961. * take it back
  2962. */
  2963. if (block_migratetype == MIGRATE_RESERVE) {
  2964. set_pageblock_migratetype(page, MIGRATE_MOVABLE);
  2965. move_freepages_block(zone, page, MIGRATE_MOVABLE);
  2966. }
  2967. }
  2968. }
  2969. /*
  2970. * Initially all pages are reserved - free ones are freed
  2971. * up by free_all_bootmem() once the early boot process is
  2972. * done. Non-atomic initialization, single-pass.
  2973. */
  2974. void __meminit memmap_init_zone(unsigned long size, int nid, unsigned long zone,
  2975. unsigned long start_pfn, enum memmap_context context)
  2976. {
  2977. struct page *page;
  2978. unsigned long end_pfn = start_pfn + size;
  2979. unsigned long pfn;
  2980. struct zone *z;
  2981. if (highest_memmap_pfn < end_pfn - 1)
  2982. highest_memmap_pfn = end_pfn - 1;
  2983. z = &NODE_DATA(nid)->node_zones[zone];
  2984. for (pfn = start_pfn; pfn < end_pfn; pfn++) {
  2985. /*
  2986. * There can be holes in boot-time mem_map[]s
  2987. * handed to this function. They do not
  2988. * exist on hotplugged memory.
  2989. */
  2990. if (context == MEMMAP_EARLY) {
  2991. if (!early_pfn_valid(pfn))
  2992. continue;
  2993. if (!early_pfn_in_nid(pfn, nid))
  2994. continue;
  2995. }
  2996. page = pfn_to_page(pfn);
  2997. set_page_links(page, zone, nid, pfn);
  2998. mminit_verify_page_links(page, zone, nid, pfn);
  2999. init_page_count(page);
  3000. reset_page_mapcount(page);
  3001. SetPageReserved(page);
  3002. /*
  3003. * Mark the block movable so that blocks are reserved for
  3004. * movable at startup. This will force kernel allocations
  3005. * to reserve their blocks rather than leaking throughout
  3006. * the address space during boot when many long-lived
  3007. * kernel allocations are made. Later some blocks near
  3008. * the start are marked MIGRATE_RESERVE by
  3009. * setup_zone_migrate_reserve()
  3010. *
  3011. * bitmap is created for zone's valid pfn range. but memmap
  3012. * can be created for invalid pages (for alignment)
  3013. * check here not to call set_pageblock_migratetype() against
  3014. * pfn out of zone.
  3015. */
  3016. if ((z->zone_start_pfn <= pfn)
  3017. && (pfn < z->zone_start_pfn + z->spanned_pages)
  3018. && !(pfn & (pageblock_nr_pages - 1)))
  3019. set_pageblock_migratetype(page, MIGRATE_MOVABLE);
  3020. INIT_LIST_HEAD(&page->lru);
  3021. #ifdef WANT_PAGE_VIRTUAL
  3022. /* The shift won't overflow because ZONE_NORMAL is below 4G. */
  3023. if (!is_highmem_idx(zone))
  3024. set_page_address(page, __va(pfn << PAGE_SHIFT));
  3025. #endif
  3026. }
  3027. }
  3028. static void __meminit zone_init_free_lists(struct zone *zone)
  3029. {
  3030. int order, t;
  3031. for_each_migratetype_order(order, t) {
  3032. INIT_LIST_HEAD(&zone->free_area[order].free_list[t]);
  3033. zone->free_area[order].nr_free = 0;
  3034. }
  3035. }
  3036. #ifndef __HAVE_ARCH_MEMMAP_INIT
  3037. #define memmap_init(size, nid, zone, start_pfn) \
  3038. memmap_init_zone((size), (nid), (zone), (start_pfn), MEMMAP_EARLY)
  3039. #endif
  3040. static int zone_batchsize(struct zone *zone)
  3041. {
  3042. #ifdef CONFIG_MMU
  3043. int batch;
  3044. /*
  3045. * The per-cpu-pages pools are set to around 1000th of the
  3046. * size of the zone. But no more than 1/2 of a meg.
  3047. *
  3048. * OK, so we don't know how big the cache is. So guess.
  3049. */
  3050. batch = zone->present_pages / 1024;
  3051. if (batch * PAGE_SIZE > 512 * 1024)
  3052. batch = (512 * 1024) / PAGE_SIZE;
  3053. batch /= 4; /* We effectively *= 4 below */
  3054. if (batch < 1)
  3055. batch = 1;
  3056. /*
  3057. * Clamp the batch to a 2^n - 1 value. Having a power
  3058. * of 2 value was found to be more likely to have
  3059. * suboptimal cache aliasing properties in some cases.
  3060. *
  3061. * For example if 2 tasks are alternately allocating
  3062. * batches of pages, one task can end up with a lot
  3063. * of pages of one half of the possible page colors
  3064. * and the other with pages of the other colors.
  3065. */
  3066. batch = rounddown_pow_of_two(batch + batch/2) - 1;
  3067. return batch;
  3068. #else
  3069. /* The deferral and batching of frees should be suppressed under NOMMU
  3070. * conditions.
  3071. *
  3072. * The problem is that NOMMU needs to be able to allocate large chunks
  3073. * of contiguous memory as there's no hardware page translation to
  3074. * assemble apparent contiguous memory from discontiguous pages.
  3075. *
  3076. * Queueing large contiguous runs of pages for batching, however,
  3077. * causes the pages to actually be freed in smaller chunks. As there
  3078. * can be a significant delay between the individual batches being
  3079. * recycled, this leads to the once large chunks of space being
  3080. * fragmented and becoming unavailable for high-order allocations.
  3081. */
  3082. return 0;
  3083. #endif
  3084. }
  3085. static void setup_pageset(struct per_cpu_pageset *p, unsigned long batch)
  3086. {
  3087. struct per_cpu_pages *pcp;
  3088. int migratetype;
  3089. memset(p, 0, sizeof(*p));
  3090. pcp = &p->pcp;
  3091. pcp->count = 0;
  3092. pcp->high = 6 * batch;
  3093. pcp->batch = max(1UL, 1 * batch);
  3094. for (migratetype = 0; migratetype < MIGRATE_PCPTYPES; migratetype++)
  3095. INIT_LIST_HEAD(&pcp->lists[migratetype]);
  3096. }
  3097. /*
  3098. * setup_pagelist_highmark() sets the high water mark for hot per_cpu_pagelist
  3099. * to the value high for the pageset p.
  3100. */
  3101. static void setup_pagelist_highmark(struct per_cpu_pageset *p,
  3102. unsigned long high)
  3103. {
  3104. struct per_cpu_pages *pcp;
  3105. pcp = &p->pcp;
  3106. pcp->high = high;
  3107. pcp->batch = max(1UL, high/4);
  3108. if ((high/4) > (PAGE_SHIFT * 8))
  3109. pcp->batch = PAGE_SHIFT * 8;
  3110. }
  3111. static void setup_zone_pageset(struct zone *zone)
  3112. {
  3113. int cpu;
  3114. zone->pageset = alloc_percpu(struct per_cpu_pageset);
  3115. for_each_possible_cpu(cpu) {
  3116. struct per_cpu_pageset *pcp = per_cpu_ptr(zone->pageset, cpu);
  3117. setup_pageset(pcp, zone_batchsize(zone));
  3118. if (percpu_pagelist_fraction)
  3119. setup_pagelist_highmark(pcp,
  3120. (zone->present_pages /
  3121. percpu_pagelist_fraction));
  3122. }
  3123. }
  3124. /*
  3125. * Allocate per cpu pagesets and initialize them.
  3126. * Before this call only boot pagesets were available.
  3127. */
  3128. void __init setup_per_cpu_pageset(void)
  3129. {
  3130. struct zone *zone;
  3131. for_each_populated_zone(zone)
  3132. setup_zone_pageset(zone);
  3133. }
  3134. static noinline __init_refok
  3135. int zone_wait_table_init(struct zone *zone, unsigned long zone_size_pages)
  3136. {
  3137. int i;
  3138. struct pglist_data *pgdat = zone->zone_pgdat;
  3139. size_t alloc_size;
  3140. /*
  3141. * The per-page waitqueue mechanism uses hashed waitqueues
  3142. * per zone.
  3143. */
  3144. zone->wait_table_hash_nr_entries =
  3145. wait_table_hash_nr_entries(zone_size_pages);
  3146. zone->wait_table_bits =
  3147. wait_table_bits(zone->wait_table_hash_nr_entries);
  3148. alloc_size = zone->wait_table_hash_nr_entries
  3149. * sizeof(wait_queue_head_t);
  3150. if (!slab_is_available()) {
  3151. zone->wait_table = (wait_queue_head_t *)
  3152. alloc_bootmem_node_nopanic(pgdat, alloc_size);
  3153. } else {
  3154. /*
  3155. * This case means that a zone whose size was 0 gets new memory
  3156. * via memory hot-add.
  3157. * But it may be the case that a new node was hot-added. In
  3158. * this case vmalloc() will not be able to use this new node's
  3159. * memory - this wait_table must be initialized to use this new
  3160. * node itself as well.
  3161. * To use this new node's memory, further consideration will be
  3162. * necessary.
  3163. */
  3164. zone->wait_table = vmalloc(alloc_size);
  3165. }
  3166. if (!zone->wait_table)
  3167. return -ENOMEM;
  3168. for(i = 0; i < zone->wait_table_hash_nr_entries; ++i)
  3169. init_waitqueue_head(zone->wait_table + i);
  3170. return 0;
  3171. }
  3172. static int __zone_pcp_update(void *data)
  3173. {
  3174. struct zone *zone = data;
  3175. int cpu;
  3176. unsigned long batch = zone_batchsize(zone), flags;
  3177. for_each_possible_cpu(cpu) {
  3178. struct per_cpu_pageset *pset;
  3179. struct per_cpu_pages *pcp;
  3180. pset = per_cpu_ptr(zone->pageset, cpu);
  3181. pcp = &pset->pcp;
  3182. local_irq_save(flags);
  3183. free_pcppages_bulk(zone, pcp->count, pcp);
  3184. setup_pageset(pset, batch);
  3185. local_irq_restore(flags);
  3186. }
  3187. return 0;
  3188. }
  3189. void zone_pcp_update(struct zone *zone)
  3190. {
  3191. stop_machine(__zone_pcp_update, zone, NULL);
  3192. }
  3193. static __meminit void zone_pcp_init(struct zone *zone)
  3194. {
  3195. /*
  3196. * per cpu subsystem is not up at this point. The following code
  3197. * relies on the ability of the linker to provide the
  3198. * offset of a (static) per cpu variable into the per cpu area.
  3199. */
  3200. zone->pageset = &boot_pageset;
  3201. if (zone->present_pages)
  3202. printk(KERN_DEBUG " %s zone: %lu pages, LIFO batch:%u\n",
  3203. zone->name, zone->present_pages,
  3204. zone_batchsize(zone));
  3205. }
  3206. __meminit int init_currently_empty_zone(struct zone *zone,
  3207. unsigned long zone_start_pfn,
  3208. unsigned long size,
  3209. enum memmap_context context)
  3210. {
  3211. struct pglist_data *pgdat = zone->zone_pgdat;
  3212. int ret;
  3213. ret = zone_wait_table_init(zone, size);
  3214. if (ret)
  3215. return ret;
  3216. pgdat->nr_zones = zone_idx(zone) + 1;
  3217. zone->zone_start_pfn = zone_start_pfn;
  3218. mminit_dprintk(MMINIT_TRACE, "memmap_init",
  3219. "Initialising map node %d zone %lu pfns %lu -> %lu\n",
  3220. pgdat->node_id,
  3221. (unsigned long)zone_idx(zone),
  3222. zone_start_pfn, (zone_start_pfn + size));
  3223. zone_init_free_lists(zone);
  3224. return 0;
  3225. }
  3226. #ifdef CONFIG_ARCH_POPULATES_NODE_MAP
  3227. /*
  3228. * Basic iterator support. Return the first range of PFNs for a node
  3229. * Note: nid == MAX_NUMNODES returns first region regardless of node
  3230. */
  3231. static int __meminit first_active_region_index_in_nid(int nid)
  3232. {
  3233. int i;
  3234. for (i = 0; i < nr_nodemap_entries; i++)
  3235. if (nid == MAX_NUMNODES || early_node_map[i].nid == nid)
  3236. return i;
  3237. return -1;
  3238. }
  3239. /*
  3240. * Basic iterator support. Return the next active range of PFNs for a node
  3241. * Note: nid == MAX_NUMNODES returns next region regardless of node
  3242. */
  3243. static int __meminit next_active_region_index_in_nid(int index, int nid)
  3244. {
  3245. for (index = index + 1; index < nr_nodemap_entries; index++)
  3246. if (nid == MAX_NUMNODES || early_node_map[index].nid == nid)
  3247. return index;
  3248. return -1;
  3249. }
  3250. #ifndef CONFIG_HAVE_ARCH_EARLY_PFN_TO_NID
  3251. /*
  3252. * Required by SPARSEMEM. Given a PFN, return what node the PFN is on.
  3253. * Architectures may implement their own version but if add_active_range()
  3254. * was used and there are no special requirements, this is a convenient
  3255. * alternative
  3256. */
  3257. int __meminit __early_pfn_to_nid(unsigned long pfn)
  3258. {
  3259. int i;
  3260. for (i = 0; i < nr_nodemap_entries; i++) {
  3261. unsigned long start_pfn = early_node_map[i].start_pfn;
  3262. unsigned long end_pfn = early_node_map[i].end_pfn;
  3263. if (start_pfn <= pfn && pfn < end_pfn)
  3264. return early_node_map[i].nid;
  3265. }
  3266. /* This is a memory hole */
  3267. return -1;
  3268. }
  3269. #endif /* CONFIG_HAVE_ARCH_EARLY_PFN_TO_NID */
  3270. int __meminit early_pfn_to_nid(unsigned long pfn)
  3271. {
  3272. int nid;
  3273. nid = __early_pfn_to_nid(pfn);
  3274. if (nid >= 0)
  3275. return nid;
  3276. /* just returns 0 */
  3277. return 0;
  3278. }
  3279. #ifdef CONFIG_NODES_SPAN_OTHER_NODES
  3280. bool __meminit early_pfn_in_nid(unsigned long pfn, int node)
  3281. {
  3282. int nid;
  3283. nid = __early_pfn_to_nid(pfn);
  3284. if (nid >= 0 && nid != node)
  3285. return false;
  3286. return true;
  3287. }
  3288. #endif
  3289. /* Basic iterator support to walk early_node_map[] */
  3290. #define for_each_active_range_index_in_nid(i, nid) \
  3291. for (i = first_active_region_index_in_nid(nid); i != -1; \
  3292. i = next_active_region_index_in_nid(i, nid))
  3293. /**
  3294. * free_bootmem_with_active_regions - Call free_bootmem_node for each active range
  3295. * @nid: The node to free memory on. If MAX_NUMNODES, all nodes are freed.
  3296. * @max_low_pfn: The highest PFN that will be passed to free_bootmem_node
  3297. *
  3298. * If an architecture guarantees that all ranges registered with
  3299. * add_active_ranges() contain no holes and may be freed, this
  3300. * this function may be used instead of calling free_bootmem() manually.
  3301. */
  3302. void __init free_bootmem_with_active_regions(int nid,
  3303. unsigned long max_low_pfn)
  3304. {
  3305. int i;
  3306. for_each_active_range_index_in_nid(i, nid) {
  3307. unsigned long size_pages = 0;
  3308. unsigned long end_pfn = early_node_map[i].end_pfn;
  3309. if (early_node_map[i].start_pfn >= max_low_pfn)
  3310. continue;
  3311. if (end_pfn > max_low_pfn)
  3312. end_pfn = max_low_pfn;
  3313. size_pages = end_pfn - early_node_map[i].start_pfn;
  3314. free_bootmem_node(NODE_DATA(early_node_map[i].nid),
  3315. PFN_PHYS(early_node_map[i].start_pfn),
  3316. size_pages << PAGE_SHIFT);
  3317. }
  3318. }
  3319. #ifdef CONFIG_HAVE_MEMBLOCK
  3320. /*
  3321. * Basic iterator support. Return the last range of PFNs for a node
  3322. * Note: nid == MAX_NUMNODES returns last region regardless of node
  3323. */
  3324. static int __meminit last_active_region_index_in_nid(int nid)
  3325. {
  3326. int i;
  3327. for (i = nr_nodemap_entries - 1; i >= 0; i--)
  3328. if (nid == MAX_NUMNODES || early_node_map[i].nid == nid)
  3329. return i;
  3330. return -1;
  3331. }
  3332. /*
  3333. * Basic iterator support. Return the previous active range of PFNs for a node
  3334. * Note: nid == MAX_NUMNODES returns next region regardless of node
  3335. */
  3336. static int __meminit previous_active_region_index_in_nid(int index, int nid)
  3337. {
  3338. for (index = index - 1; index >= 0; index--)
  3339. if (nid == MAX_NUMNODES || early_node_map[index].nid == nid)
  3340. return index;
  3341. return -1;
  3342. }
  3343. #define for_each_active_range_index_in_nid_reverse(i, nid) \
  3344. for (i = last_active_region_index_in_nid(nid); i != -1; \
  3345. i = previous_active_region_index_in_nid(i, nid))
  3346. u64 __init find_memory_core_early(int nid, u64 size, u64 align,
  3347. u64 goal, u64 limit)
  3348. {
  3349. int i;
  3350. /* Need to go over early_node_map to find out good range for node */
  3351. for_each_active_range_index_in_nid_reverse(i, nid) {
  3352. u64 addr;
  3353. u64 ei_start, ei_last;
  3354. u64 final_start, final_end;
  3355. ei_last = early_node_map[i].end_pfn;
  3356. ei_last <<= PAGE_SHIFT;
  3357. ei_start = early_node_map[i].start_pfn;
  3358. ei_start <<= PAGE_SHIFT;
  3359. final_start = max(ei_start, goal);
  3360. final_end = min(ei_last, limit);
  3361. if (final_start >= final_end)
  3362. continue;
  3363. addr = memblock_find_in_range(final_start, final_end, size, align);
  3364. if (addr == MEMBLOCK_ERROR)
  3365. continue;
  3366. return addr;
  3367. }
  3368. return MEMBLOCK_ERROR;
  3369. }
  3370. #endif
  3371. int __init add_from_early_node_map(struct range *range, int az,
  3372. int nr_range, int nid)
  3373. {
  3374. int i;
  3375. u64 start, end;
  3376. /* need to go over early_node_map to find out good range for node */
  3377. for_each_active_range_index_in_nid(i, nid) {
  3378. start = early_node_map[i].start_pfn;
  3379. end = early_node_map[i].end_pfn;
  3380. nr_range = add_range(range, az, nr_range, start, end);
  3381. }
  3382. return nr_range;
  3383. }
  3384. void __init work_with_active_regions(int nid, work_fn_t work_fn, void *data)
  3385. {
  3386. int i;
  3387. int ret;
  3388. for_each_active_range_index_in_nid(i, nid) {
  3389. ret = work_fn(early_node_map[i].start_pfn,
  3390. early_node_map[i].end_pfn, data);
  3391. if (ret)
  3392. break;
  3393. }
  3394. }
  3395. /**
  3396. * sparse_memory_present_with_active_regions - Call memory_present for each active range
  3397. * @nid: The node to call memory_present for. If MAX_NUMNODES, all nodes will be used.
  3398. *
  3399. * If an architecture guarantees that all ranges registered with
  3400. * add_active_ranges() contain no holes and may be freed, this
  3401. * function may be used instead of calling memory_present() manually.
  3402. */
  3403. void __init sparse_memory_present_with_active_regions(int nid)
  3404. {
  3405. int i;
  3406. for_each_active_range_index_in_nid(i, nid)
  3407. memory_present(early_node_map[i].nid,
  3408. early_node_map[i].start_pfn,
  3409. early_node_map[i].end_pfn);
  3410. }
  3411. /**
  3412. * get_pfn_range_for_nid - Return the start and end page frames for a node
  3413. * @nid: The nid to return the range for. If MAX_NUMNODES, the min and max PFN are returned.
  3414. * @start_pfn: Passed by reference. On return, it will have the node start_pfn.
  3415. * @end_pfn: Passed by reference. On return, it will have the node end_pfn.
  3416. *
  3417. * It returns the start and end page frame of a node based on information
  3418. * provided by an arch calling add_active_range(). If called for a node
  3419. * with no available memory, a warning is printed and the start and end
  3420. * PFNs will be 0.
  3421. */
  3422. void __meminit get_pfn_range_for_nid(unsigned int nid,
  3423. unsigned long *start_pfn, unsigned long *end_pfn)
  3424. {
  3425. int i;
  3426. *start_pfn = -1UL;
  3427. *end_pfn = 0;
  3428. for_each_active_range_index_in_nid(i, nid) {
  3429. *start_pfn = min(*start_pfn, early_node_map[i].start_pfn);
  3430. *end_pfn = max(*end_pfn, early_node_map[i].end_pfn);
  3431. }
  3432. if (*start_pfn == -1UL)
  3433. *start_pfn = 0;
  3434. }
  3435. /*
  3436. * This finds a zone that can be used for ZONE_MOVABLE pages. The
  3437. * assumption is made that zones within a node are ordered in monotonic
  3438. * increasing memory addresses so that the "highest" populated zone is used
  3439. */
  3440. static void __init find_usable_zone_for_movable(void)
  3441. {
  3442. int zone_index;
  3443. for (zone_index = MAX_NR_ZONES - 1; zone_index >= 0; zone_index--) {
  3444. if (zone_index == ZONE_MOVABLE)
  3445. continue;
  3446. if (arch_zone_highest_possible_pfn[zone_index] >
  3447. arch_zone_lowest_possible_pfn[zone_index])
  3448. break;
  3449. }
  3450. VM_BUG_ON(zone_index == -1);
  3451. movable_zone = zone_index;
  3452. }
  3453. /*
  3454. * The zone ranges provided by the architecture do not include ZONE_MOVABLE
  3455. * because it is sized independent of architecture. Unlike the other zones,
  3456. * the starting point for ZONE_MOVABLE is not fixed. It may be different
  3457. * in each node depending on the size of each node and how evenly kernelcore
  3458. * is distributed. This helper function adjusts the zone ranges
  3459. * provided by the architecture for a given node by using the end of the
  3460. * highest usable zone for ZONE_MOVABLE. This preserves the assumption that
  3461. * zones within a node are in order of monotonic increases memory addresses
  3462. */
  3463. static void __meminit adjust_zone_range_for_zone_movable(int nid,
  3464. unsigned long zone_type,
  3465. unsigned long node_start_pfn,
  3466. unsigned long node_end_pfn,
  3467. unsigned long *zone_start_pfn,
  3468. unsigned long *zone_end_pfn)
  3469. {
  3470. /* Only adjust if ZONE_MOVABLE is on this node */
  3471. if (zone_movable_pfn[nid]) {
  3472. /* Size ZONE_MOVABLE */
  3473. if (zone_type == ZONE_MOVABLE) {
  3474. *zone_start_pfn = zone_movable_pfn[nid];
  3475. *zone_end_pfn = min(node_end_pfn,
  3476. arch_zone_highest_possible_pfn[movable_zone]);
  3477. /* Adjust for ZONE_MOVABLE starting within this range */
  3478. } else if (*zone_start_pfn < zone_movable_pfn[nid] &&
  3479. *zone_end_pfn > zone_movable_pfn[nid]) {
  3480. *zone_end_pfn = zone_movable_pfn[nid];
  3481. /* Check if this whole range is within ZONE_MOVABLE */
  3482. } else if (*zone_start_pfn >= zone_movable_pfn[nid])
  3483. *zone_start_pfn = *zone_end_pfn;
  3484. }
  3485. }
  3486. /*
  3487. * Return the number of pages a zone spans in a node, including holes
  3488. * present_pages = zone_spanned_pages_in_node() - zone_absent_pages_in_node()
  3489. */
  3490. static unsigned long __meminit zone_spanned_pages_in_node(int nid,
  3491. unsigned long zone_type,
  3492. unsigned long *ignored)
  3493. {
  3494. unsigned long node_start_pfn, node_end_pfn;
  3495. unsigned long zone_start_pfn, zone_end_pfn;
  3496. /* Get the start and end of the node and zone */
  3497. get_pfn_range_for_nid(nid, &node_start_pfn, &node_end_pfn);
  3498. zone_start_pfn = arch_zone_lowest_possible_pfn[zone_type];
  3499. zone_end_pfn = arch_zone_highest_possible_pfn[zone_type];
  3500. adjust_zone_range_for_zone_movable(nid, zone_type,
  3501. node_start_pfn, node_end_pfn,
  3502. &zone_start_pfn, &zone_end_pfn);
  3503. /* Check that this node has pages within the zone's required range */
  3504. if (zone_end_pfn < node_start_pfn || zone_start_pfn > node_end_pfn)
  3505. return 0;
  3506. /* Move the zone boundaries inside the node if necessary */
  3507. zone_end_pfn = min(zone_end_pfn, node_end_pfn);
  3508. zone_start_pfn = max(zone_start_pfn, node_start_pfn);
  3509. /* Return the spanned pages */
  3510. return zone_end_pfn - zone_start_pfn;
  3511. }
  3512. /*
  3513. * Return the number of holes in a range on a node. If nid is MAX_NUMNODES,
  3514. * then all holes in the requested range will be accounted for.
  3515. */
  3516. unsigned long __meminit __absent_pages_in_range(int nid,
  3517. unsigned long range_start_pfn,
  3518. unsigned long range_end_pfn)
  3519. {
  3520. int i = 0;
  3521. unsigned long prev_end_pfn = 0, hole_pages = 0;
  3522. unsigned long start_pfn;
  3523. /* Find the end_pfn of the first active range of pfns in the node */
  3524. i = first_active_region_index_in_nid(nid);
  3525. if (i == -1)
  3526. return 0;
  3527. prev_end_pfn = min(early_node_map[i].start_pfn, range_end_pfn);
  3528. /* Account for ranges before physical memory on this node */
  3529. if (early_node_map[i].start_pfn > range_start_pfn)
  3530. hole_pages = prev_end_pfn - range_start_pfn;
  3531. /* Find all holes for the zone within the node */
  3532. for (; i != -1; i = next_active_region_index_in_nid(i, nid)) {
  3533. /* No need to continue if prev_end_pfn is outside the zone */
  3534. if (prev_end_pfn >= range_end_pfn)
  3535. break;
  3536. /* Make sure the end of the zone is not within the hole */
  3537. start_pfn = min(early_node_map[i].start_pfn, range_end_pfn);
  3538. prev_end_pfn = max(prev_end_pfn, range_start_pfn);
  3539. /* Update the hole size cound and move on */
  3540. if (start_pfn > range_start_pfn) {
  3541. BUG_ON(prev_end_pfn > start_pfn);
  3542. hole_pages += start_pfn - prev_end_pfn;
  3543. }
  3544. prev_end_pfn = early_node_map[i].end_pfn;
  3545. }
  3546. /* Account for ranges past physical memory on this node */
  3547. if (range_end_pfn > prev_end_pfn)
  3548. hole_pages += range_end_pfn -
  3549. max(range_start_pfn, prev_end_pfn);
  3550. return hole_pages;
  3551. }
  3552. /**
  3553. * absent_pages_in_range - Return number of page frames in holes within a range
  3554. * @start_pfn: The start PFN to start searching for holes
  3555. * @end_pfn: The end PFN to stop searching for holes
  3556. *
  3557. * It returns the number of pages frames in memory holes within a range.
  3558. */
  3559. unsigned long __init absent_pages_in_range(unsigned long start_pfn,
  3560. unsigned long end_pfn)
  3561. {
  3562. return __absent_pages_in_range(MAX_NUMNODES, start_pfn, end_pfn);
  3563. }
  3564. /* Return the number of page frames in holes in a zone on a node */
  3565. static unsigned long __meminit zone_absent_pages_in_node(int nid,
  3566. unsigned long zone_type,
  3567. unsigned long *ignored)
  3568. {
  3569. unsigned long node_start_pfn, node_end_pfn;
  3570. unsigned long zone_start_pfn, zone_end_pfn;
  3571. get_pfn_range_for_nid(nid, &node_start_pfn, &node_end_pfn);
  3572. zone_start_pfn = max(arch_zone_lowest_possible_pfn[zone_type],
  3573. node_start_pfn);
  3574. zone_end_pfn = min(arch_zone_highest_possible_pfn[zone_type],
  3575. node_end_pfn);
  3576. adjust_zone_range_for_zone_movable(nid, zone_type,
  3577. node_start_pfn, node_end_pfn,
  3578. &zone_start_pfn, &zone_end_pfn);
  3579. return __absent_pages_in_range(nid, zone_start_pfn, zone_end_pfn);
  3580. }
  3581. #else
  3582. static inline unsigned long __meminit zone_spanned_pages_in_node(int nid,
  3583. unsigned long zone_type,
  3584. unsigned long *zones_size)
  3585. {
  3586. return zones_size[zone_type];
  3587. }
  3588. static inline unsigned long __meminit zone_absent_pages_in_node(int nid,
  3589. unsigned long zone_type,
  3590. unsigned long *zholes_size)
  3591. {
  3592. if (!zholes_size)
  3593. return 0;
  3594. return zholes_size[zone_type];
  3595. }
  3596. #endif
  3597. static void __meminit calculate_node_totalpages(struct pglist_data *pgdat,
  3598. unsigned long *zones_size, unsigned long *zholes_size)
  3599. {
  3600. unsigned long realtotalpages, totalpages = 0;
  3601. enum zone_type i;
  3602. for (i = 0; i < MAX_NR_ZONES; i++)
  3603. totalpages += zone_spanned_pages_in_node(pgdat->node_id, i,
  3604. zones_size);
  3605. pgdat->node_spanned_pages = totalpages;
  3606. realtotalpages = totalpages;
  3607. for (i = 0; i < MAX_NR_ZONES; i++)
  3608. realtotalpages -=
  3609. zone_absent_pages_in_node(pgdat->node_id, i,
  3610. zholes_size);
  3611. pgdat->node_present_pages = realtotalpages;
  3612. printk(KERN_DEBUG "On node %d totalpages: %lu\n", pgdat->node_id,
  3613. realtotalpages);
  3614. }
  3615. #ifndef CONFIG_SPARSEMEM
  3616. /*
  3617. * Calculate the size of the zone->blockflags rounded to an unsigned long
  3618. * Start by making sure zonesize is a multiple of pageblock_order by rounding
  3619. * up. Then use 1 NR_PAGEBLOCK_BITS worth of bits per pageblock, finally
  3620. * round what is now in bits to nearest long in bits, then return it in
  3621. * bytes.
  3622. */
  3623. static unsigned long __init usemap_size(unsigned long zonesize)
  3624. {
  3625. unsigned long usemapsize;
  3626. usemapsize = roundup(zonesize, pageblock_nr_pages);
  3627. usemapsize = usemapsize >> pageblock_order;
  3628. usemapsize *= NR_PAGEBLOCK_BITS;
  3629. usemapsize = roundup(usemapsize, 8 * sizeof(unsigned long));
  3630. return usemapsize / 8;
  3631. }
  3632. static void __init setup_usemap(struct pglist_data *pgdat,
  3633. struct zone *zone, unsigned long zonesize)
  3634. {
  3635. unsigned long usemapsize = usemap_size(zonesize);
  3636. zone->pageblock_flags = NULL;
  3637. if (usemapsize)
  3638. zone->pageblock_flags = alloc_bootmem_node_nopanic(pgdat,
  3639. usemapsize);
  3640. }
  3641. #else
  3642. static inline void setup_usemap(struct pglist_data *pgdat,
  3643. struct zone *zone, unsigned long zonesize) {}
  3644. #endif /* CONFIG_SPARSEMEM */
  3645. #ifdef CONFIG_HUGETLB_PAGE_SIZE_VARIABLE
  3646. /* Return a sensible default order for the pageblock size. */
  3647. static inline int pageblock_default_order(void)
  3648. {
  3649. if (HPAGE_SHIFT > PAGE_SHIFT)
  3650. return HUGETLB_PAGE_ORDER;
  3651. return MAX_ORDER-1;
  3652. }
  3653. /* Initialise the number of pages represented by NR_PAGEBLOCK_BITS */
  3654. static inline void __init set_pageblock_order(unsigned int order)
  3655. {
  3656. /* Check that pageblock_nr_pages has not already been setup */
  3657. if (pageblock_order)
  3658. return;
  3659. /*
  3660. * Assume the largest contiguous order of interest is a huge page.
  3661. * This value may be variable depending on boot parameters on IA64
  3662. */
  3663. pageblock_order = order;
  3664. }
  3665. #else /* CONFIG_HUGETLB_PAGE_SIZE_VARIABLE */
  3666. /*
  3667. * When CONFIG_HUGETLB_PAGE_SIZE_VARIABLE is not set, set_pageblock_order()
  3668. * and pageblock_default_order() are unused as pageblock_order is set
  3669. * at compile-time. See include/linux/pageblock-flags.h for the values of
  3670. * pageblock_order based on the kernel config
  3671. */
  3672. static inline int pageblock_default_order(unsigned int order)
  3673. {
  3674. return MAX_ORDER-1;
  3675. }
  3676. #define set_pageblock_order(x) do {} while (0)
  3677. #endif /* CONFIG_HUGETLB_PAGE_SIZE_VARIABLE */
  3678. /*
  3679. * Set up the zone data structures:
  3680. * - mark all pages reserved
  3681. * - mark all memory queues empty
  3682. * - clear the memory bitmaps
  3683. */
  3684. static void __paginginit free_area_init_core(struct pglist_data *pgdat,
  3685. unsigned long *zones_size, unsigned long *zholes_size)
  3686. {
  3687. enum zone_type j;
  3688. int nid = pgdat->node_id;
  3689. unsigned long zone_start_pfn = pgdat->node_start_pfn;
  3690. int ret;
  3691. pgdat_resize_init(pgdat);
  3692. pgdat->nr_zones = 0;
  3693. init_waitqueue_head(&pgdat->kswapd_wait);
  3694. pgdat->kswapd_max_order = 0;
  3695. pgdat_page_cgroup_init(pgdat);
  3696. for (j = 0; j < MAX_NR_ZONES; j++) {
  3697. struct zone *zone = pgdat->node_zones + j;
  3698. unsigned long size, realsize, memmap_pages;
  3699. enum lru_list l;
  3700. size = zone_spanned_pages_in_node(nid, j, zones_size);
  3701. realsize = size - zone_absent_pages_in_node(nid, j,
  3702. zholes_size);
  3703. /*
  3704. * Adjust realsize so that it accounts for how much memory
  3705. * is used by this zone for memmap. This affects the watermark
  3706. * and per-cpu initialisations
  3707. */
  3708. memmap_pages =
  3709. PAGE_ALIGN(size * sizeof(struct page)) >> PAGE_SHIFT;
  3710. if (realsize >= memmap_pages) {
  3711. realsize -= memmap_pages;
  3712. if (memmap_pages)
  3713. printk(KERN_DEBUG
  3714. " %s zone: %lu pages used for memmap\n",
  3715. zone_names[j], memmap_pages);
  3716. } else
  3717. printk(KERN_WARNING
  3718. " %s zone: %lu pages exceeds realsize %lu\n",
  3719. zone_names[j], memmap_pages, realsize);
  3720. /* Account for reserved pages */
  3721. if (j == 0 && realsize > dma_reserve) {
  3722. realsize -= dma_reserve;
  3723. printk(KERN_DEBUG " %s zone: %lu pages reserved\n",
  3724. zone_names[0], dma_reserve);
  3725. }
  3726. if (!is_highmem_idx(j))
  3727. nr_kernel_pages += realsize;
  3728. nr_all_pages += realsize;
  3729. zone->spanned_pages = size;
  3730. zone->present_pages = realsize;
  3731. #ifdef CONFIG_NUMA
  3732. zone->node = nid;
  3733. zone->min_unmapped_pages = (realsize*sysctl_min_unmapped_ratio)
  3734. / 100;
  3735. zone->min_slab_pages = (realsize * sysctl_min_slab_ratio) / 100;
  3736. #endif
  3737. zone->name = zone_names[j];
  3738. spin_lock_init(&zone->lock);
  3739. spin_lock_init(&zone->lru_lock);
  3740. zone_seqlock_init(zone);
  3741. zone->zone_pgdat = pgdat;
  3742. zone_pcp_init(zone);
  3743. for_each_lru(l)
  3744. INIT_LIST_HEAD(&zone->lru[l].list);
  3745. zone->reclaim_stat.recent_rotated[0] = 0;
  3746. zone->reclaim_stat.recent_rotated[1] = 0;
  3747. zone->reclaim_stat.recent_scanned[0] = 0;
  3748. zone->reclaim_stat.recent_scanned[1] = 0;
  3749. zap_zone_vm_stats(zone);
  3750. zone->flags = 0;
  3751. if (!size)
  3752. continue;
  3753. set_pageblock_order(pageblock_default_order());
  3754. setup_usemap(pgdat, zone, size);
  3755. ret = init_currently_empty_zone(zone, zone_start_pfn,
  3756. size, MEMMAP_EARLY);
  3757. BUG_ON(ret);
  3758. memmap_init(size, nid, j, zone_start_pfn);
  3759. zone_start_pfn += size;
  3760. }
  3761. }
  3762. static void __init_refok alloc_node_mem_map(struct pglist_data *pgdat)
  3763. {
  3764. /* Skip empty nodes */
  3765. if (!pgdat->node_spanned_pages)
  3766. return;
  3767. #ifdef CONFIG_FLAT_NODE_MEM_MAP
  3768. /* ia64 gets its own node_mem_map, before this, without bootmem */
  3769. if (!pgdat->node_mem_map) {
  3770. unsigned long size, start, end;
  3771. struct page *map;
  3772. /*
  3773. * The zone's endpoints aren't required to be MAX_ORDER
  3774. * aligned but the node_mem_map endpoints must be in order
  3775. * for the buddy allocator to function correctly.
  3776. */
  3777. start = pgdat->node_start_pfn & ~(MAX_ORDER_NR_PAGES - 1);
  3778. end = pgdat->node_start_pfn + pgdat->node_spanned_pages;
  3779. end = ALIGN(end, MAX_ORDER_NR_PAGES);
  3780. size = (end - start) * sizeof(struct page);
  3781. map = alloc_remap(pgdat->node_id, size);
  3782. if (!map)
  3783. map = alloc_bootmem_node_nopanic(pgdat, size);
  3784. pgdat->node_mem_map = map + (pgdat->node_start_pfn - start);
  3785. }
  3786. #ifndef CONFIG_NEED_MULTIPLE_NODES
  3787. /*
  3788. * With no DISCONTIG, the global mem_map is just set as node 0's
  3789. */
  3790. if (pgdat == NODE_DATA(0)) {
  3791. mem_map = NODE_DATA(0)->node_mem_map;
  3792. #ifdef CONFIG_ARCH_POPULATES_NODE_MAP
  3793. if (page_to_pfn(mem_map) != pgdat->node_start_pfn)
  3794. mem_map -= (pgdat->node_start_pfn - ARCH_PFN_OFFSET);
  3795. #endif /* CONFIG_ARCH_POPULATES_NODE_MAP */
  3796. }
  3797. #endif
  3798. #endif /* CONFIG_FLAT_NODE_MEM_MAP */
  3799. }
  3800. void __paginginit free_area_init_node(int nid, unsigned long *zones_size,
  3801. unsigned long node_start_pfn, unsigned long *zholes_size)
  3802. {
  3803. pg_data_t *pgdat = NODE_DATA(nid);
  3804. pgdat->node_id = nid;
  3805. pgdat->node_start_pfn = node_start_pfn;
  3806. calculate_node_totalpages(pgdat, zones_size, zholes_size);
  3807. alloc_node_mem_map(pgdat);
  3808. #ifdef CONFIG_FLAT_NODE_MEM_MAP
  3809. printk(KERN_DEBUG "free_area_init_node: node %d, pgdat %08lx, node_mem_map %08lx\n",
  3810. nid, (unsigned long)pgdat,
  3811. (unsigned long)pgdat->node_mem_map);
  3812. #endif
  3813. free_area_init_core(pgdat, zones_size, zholes_size);
  3814. }
  3815. #ifdef CONFIG_ARCH_POPULATES_NODE_MAP
  3816. #if MAX_NUMNODES > 1
  3817. /*
  3818. * Figure out the number of possible node ids.
  3819. */
  3820. static void __init setup_nr_node_ids(void)
  3821. {
  3822. unsigned int node;
  3823. unsigned int highest = 0;
  3824. for_each_node_mask(node, node_possible_map)
  3825. highest = node;
  3826. nr_node_ids = highest + 1;
  3827. }
  3828. #else
  3829. static inline void setup_nr_node_ids(void)
  3830. {
  3831. }
  3832. #endif
  3833. /**
  3834. * add_active_range - Register a range of PFNs backed by physical memory
  3835. * @nid: The node ID the range resides on
  3836. * @start_pfn: The start PFN of the available physical memory
  3837. * @end_pfn: The end PFN of the available physical memory
  3838. *
  3839. * These ranges are stored in an early_node_map[] and later used by
  3840. * free_area_init_nodes() to calculate zone sizes and holes. If the
  3841. * range spans a memory hole, it is up to the architecture to ensure
  3842. * the memory is not freed by the bootmem allocator. If possible
  3843. * the range being registered will be merged with existing ranges.
  3844. */
  3845. void __init add_active_range(unsigned int nid, unsigned long start_pfn,
  3846. unsigned long end_pfn)
  3847. {
  3848. int i;
  3849. mminit_dprintk(MMINIT_TRACE, "memory_register",
  3850. "Entering add_active_range(%d, %#lx, %#lx) "
  3851. "%d entries of %d used\n",
  3852. nid, start_pfn, end_pfn,
  3853. nr_nodemap_entries, MAX_ACTIVE_REGIONS);
  3854. mminit_validate_memmodel_limits(&start_pfn, &end_pfn);
  3855. /* Merge with existing active regions if possible */
  3856. for (i = 0; i < nr_nodemap_entries; i++) {
  3857. if (early_node_map[i].nid != nid)
  3858. continue;
  3859. /* Skip if an existing region covers this new one */
  3860. if (start_pfn >= early_node_map[i].start_pfn &&
  3861. end_pfn <= early_node_map[i].end_pfn)
  3862. return;
  3863. /* Merge forward if suitable */
  3864. if (start_pfn <= early_node_map[i].end_pfn &&
  3865. end_pfn > early_node_map[i].end_pfn) {
  3866. early_node_map[i].end_pfn = end_pfn;
  3867. return;
  3868. }
  3869. /* Merge backward if suitable */
  3870. if (start_pfn < early_node_map[i].start_pfn &&
  3871. end_pfn >= early_node_map[i].start_pfn) {
  3872. early_node_map[i].start_pfn = start_pfn;
  3873. return;
  3874. }
  3875. }
  3876. /* Check that early_node_map is large enough */
  3877. if (i >= MAX_ACTIVE_REGIONS) {
  3878. printk(KERN_CRIT "More than %d memory regions, truncating\n",
  3879. MAX_ACTIVE_REGIONS);
  3880. return;
  3881. }
  3882. early_node_map[i].nid = nid;
  3883. early_node_map[i].start_pfn = start_pfn;
  3884. early_node_map[i].end_pfn = end_pfn;
  3885. nr_nodemap_entries = i + 1;
  3886. }
  3887. /**
  3888. * remove_active_range - Shrink an existing registered range of PFNs
  3889. * @nid: The node id the range is on that should be shrunk
  3890. * @start_pfn: The new PFN of the range
  3891. * @end_pfn: The new PFN of the range
  3892. *
  3893. * i386 with NUMA use alloc_remap() to store a node_mem_map on a local node.
  3894. * The map is kept near the end physical page range that has already been
  3895. * registered. This function allows an arch to shrink an existing registered
  3896. * range.
  3897. */
  3898. void __init remove_active_range(unsigned int nid, unsigned long start_pfn,
  3899. unsigned long end_pfn)
  3900. {
  3901. int i, j;
  3902. int removed = 0;
  3903. printk(KERN_DEBUG "remove_active_range (%d, %lu, %lu)\n",
  3904. nid, start_pfn, end_pfn);
  3905. /* Find the old active region end and shrink */
  3906. for_each_active_range_index_in_nid(i, nid) {
  3907. if (early_node_map[i].start_pfn >= start_pfn &&
  3908. early_node_map[i].end_pfn <= end_pfn) {
  3909. /* clear it */
  3910. early_node_map[i].start_pfn = 0;
  3911. early_node_map[i].end_pfn = 0;
  3912. removed = 1;
  3913. continue;
  3914. }
  3915. if (early_node_map[i].start_pfn < start_pfn &&
  3916. early_node_map[i].end_pfn > start_pfn) {
  3917. unsigned long temp_end_pfn = early_node_map[i].end_pfn;
  3918. early_node_map[i].end_pfn = start_pfn;
  3919. if (temp_end_pfn > end_pfn)
  3920. add_active_range(nid, end_pfn, temp_end_pfn);
  3921. continue;
  3922. }
  3923. if (early_node_map[i].start_pfn >= start_pfn &&
  3924. early_node_map[i].end_pfn > end_pfn &&
  3925. early_node_map[i].start_pfn < end_pfn) {
  3926. early_node_map[i].start_pfn = end_pfn;
  3927. continue;
  3928. }
  3929. }
  3930. if (!removed)
  3931. return;
  3932. /* remove the blank ones */
  3933. for (i = nr_nodemap_entries - 1; i > 0; i--) {
  3934. if (early_node_map[i].nid != nid)
  3935. continue;
  3936. if (early_node_map[i].end_pfn)
  3937. continue;
  3938. /* we found it, get rid of it */
  3939. for (j = i; j < nr_nodemap_entries - 1; j++)
  3940. memcpy(&early_node_map[j], &early_node_map[j+1],
  3941. sizeof(early_node_map[j]));
  3942. j = nr_nodemap_entries - 1;
  3943. memset(&early_node_map[j], 0, sizeof(early_node_map[j]));
  3944. nr_nodemap_entries--;
  3945. }
  3946. }
  3947. /**
  3948. * remove_all_active_ranges - Remove all currently registered regions
  3949. *
  3950. * During discovery, it may be found that a table like SRAT is invalid
  3951. * and an alternative discovery method must be used. This function removes
  3952. * all currently registered regions.
  3953. */
  3954. void __init remove_all_active_ranges(void)
  3955. {
  3956. memset(early_node_map, 0, sizeof(early_node_map));
  3957. nr_nodemap_entries = 0;
  3958. }
  3959. /* Compare two active node_active_regions */
  3960. static int __init cmp_node_active_region(const void *a, const void *b)
  3961. {
  3962. struct node_active_region *arange = (struct node_active_region *)a;
  3963. struct node_active_region *brange = (struct node_active_region *)b;
  3964. /* Done this way to avoid overflows */
  3965. if (arange->start_pfn > brange->start_pfn)
  3966. return 1;
  3967. if (arange->start_pfn < brange->start_pfn)
  3968. return -1;
  3969. return 0;
  3970. }
  3971. /* sort the node_map by start_pfn */
  3972. void __init sort_node_map(void)
  3973. {
  3974. sort(early_node_map, (size_t)nr_nodemap_entries,
  3975. sizeof(struct node_active_region),
  3976. cmp_node_active_region, NULL);
  3977. }
  3978. /**
  3979. * node_map_pfn_alignment - determine the maximum internode alignment
  3980. *
  3981. * This function should be called after node map is populated and sorted.
  3982. * It calculates the maximum power of two alignment which can distinguish
  3983. * all the nodes.
  3984. *
  3985. * For example, if all nodes are 1GiB and aligned to 1GiB, the return value
  3986. * would indicate 1GiB alignment with (1 << (30 - PAGE_SHIFT)). If the
  3987. * nodes are shifted by 256MiB, 256MiB. Note that if only the last node is
  3988. * shifted, 1GiB is enough and this function will indicate so.
  3989. *
  3990. * This is used to test whether pfn -> nid mapping of the chosen memory
  3991. * model has fine enough granularity to avoid incorrect mapping for the
  3992. * populated node map.
  3993. *
  3994. * Returns the determined alignment in pfn's. 0 if there is no alignment
  3995. * requirement (single node).
  3996. */
  3997. unsigned long __init node_map_pfn_alignment(void)
  3998. {
  3999. unsigned long accl_mask = 0, last_end = 0;
  4000. int last_nid = -1;
  4001. int i;
  4002. for_each_active_range_index_in_nid(i, MAX_NUMNODES) {
  4003. int nid = early_node_map[i].nid;
  4004. unsigned long start = early_node_map[i].start_pfn;
  4005. unsigned long end = early_node_map[i].end_pfn;
  4006. unsigned long mask;
  4007. if (!start || last_nid < 0 || last_nid == nid) {
  4008. last_nid = nid;
  4009. last_end = end;
  4010. continue;
  4011. }
  4012. /*
  4013. * Start with a mask granular enough to pin-point to the
  4014. * start pfn and tick off bits one-by-one until it becomes
  4015. * too coarse to separate the current node from the last.
  4016. */
  4017. mask = ~((1 << __ffs(start)) - 1);
  4018. while (mask && last_end <= (start & (mask << 1)))
  4019. mask <<= 1;
  4020. /* accumulate all internode masks */
  4021. accl_mask |= mask;
  4022. }
  4023. /* convert mask to number of pages */
  4024. return ~accl_mask + 1;
  4025. }
  4026. /* Find the lowest pfn for a node */
  4027. static unsigned long __init find_min_pfn_for_node(int nid)
  4028. {
  4029. int i;
  4030. unsigned long min_pfn = ULONG_MAX;
  4031. /* Assuming a sorted map, the first range found has the starting pfn */
  4032. for_each_active_range_index_in_nid(i, nid)
  4033. min_pfn = min(min_pfn, early_node_map[i].start_pfn);
  4034. if (min_pfn == ULONG_MAX) {
  4035. printk(KERN_WARNING
  4036. "Could not find start_pfn for node %d\n", nid);
  4037. return 0;
  4038. }
  4039. return min_pfn;
  4040. }
  4041. /**
  4042. * find_min_pfn_with_active_regions - Find the minimum PFN registered
  4043. *
  4044. * It returns the minimum PFN based on information provided via
  4045. * add_active_range().
  4046. */
  4047. unsigned long __init find_min_pfn_with_active_regions(void)
  4048. {
  4049. return find_min_pfn_for_node(MAX_NUMNODES);
  4050. }
  4051. /*
  4052. * early_calculate_totalpages()
  4053. * Sum pages in active regions for movable zone.
  4054. * Populate N_HIGH_MEMORY for calculating usable_nodes.
  4055. */
  4056. static unsigned long __init early_calculate_totalpages(void)
  4057. {
  4058. int i;
  4059. unsigned long totalpages = 0;
  4060. for (i = 0; i < nr_nodemap_entries; i++) {
  4061. unsigned long pages = early_node_map[i].end_pfn -
  4062. early_node_map[i].start_pfn;
  4063. totalpages += pages;
  4064. if (pages)
  4065. node_set_state(early_node_map[i].nid, N_HIGH_MEMORY);
  4066. }
  4067. return totalpages;
  4068. }
  4069. /*
  4070. * Find the PFN the Movable zone begins in each node. Kernel memory
  4071. * is spread evenly between nodes as long as the nodes have enough
  4072. * memory. When they don't, some nodes will have more kernelcore than
  4073. * others
  4074. */
  4075. static void __init find_zone_movable_pfns_for_nodes(unsigned long *movable_pfn)
  4076. {
  4077. int i, nid;
  4078. unsigned long usable_startpfn;
  4079. unsigned long kernelcore_node, kernelcore_remaining;
  4080. /* save the state before borrow the nodemask */
  4081. nodemask_t saved_node_state = node_states[N_HIGH_MEMORY];
  4082. unsigned long totalpages = early_calculate_totalpages();
  4083. int usable_nodes = nodes_weight(node_states[N_HIGH_MEMORY]);
  4084. /*
  4085. * If movablecore was specified, calculate what size of
  4086. * kernelcore that corresponds so that memory usable for
  4087. * any allocation type is evenly spread. If both kernelcore
  4088. * and movablecore are specified, then the value of kernelcore
  4089. * will be used for required_kernelcore if it's greater than
  4090. * what movablecore would have allowed.
  4091. */
  4092. if (required_movablecore) {
  4093. unsigned long corepages;
  4094. /*
  4095. * Round-up so that ZONE_MOVABLE is at least as large as what
  4096. * was requested by the user
  4097. */
  4098. required_movablecore =
  4099. roundup(required_movablecore, MAX_ORDER_NR_PAGES);
  4100. corepages = totalpages - required_movablecore;
  4101. required_kernelcore = max(required_kernelcore, corepages);
  4102. }
  4103. /* If kernelcore was not specified, there is no ZONE_MOVABLE */
  4104. if (!required_kernelcore)
  4105. goto out;
  4106. /* usable_startpfn is the lowest possible pfn ZONE_MOVABLE can be at */
  4107. find_usable_zone_for_movable();
  4108. usable_startpfn = arch_zone_lowest_possible_pfn[movable_zone];
  4109. restart:
  4110. /* Spread kernelcore memory as evenly as possible throughout nodes */
  4111. kernelcore_node = required_kernelcore / usable_nodes;
  4112. for_each_node_state(nid, N_HIGH_MEMORY) {
  4113. /*
  4114. * Recalculate kernelcore_node if the division per node
  4115. * now exceeds what is necessary to satisfy the requested
  4116. * amount of memory for the kernel
  4117. */
  4118. if (required_kernelcore < kernelcore_node)
  4119. kernelcore_node = required_kernelcore / usable_nodes;
  4120. /*
  4121. * As the map is walked, we track how much memory is usable
  4122. * by the kernel using kernelcore_remaining. When it is
  4123. * 0, the rest of the node is usable by ZONE_MOVABLE
  4124. */
  4125. kernelcore_remaining = kernelcore_node;
  4126. /* Go through each range of PFNs within this node */
  4127. for_each_active_range_index_in_nid(i, nid) {
  4128. unsigned long start_pfn, end_pfn;
  4129. unsigned long size_pages;
  4130. start_pfn = max(early_node_map[i].start_pfn,
  4131. zone_movable_pfn[nid]);
  4132. end_pfn = early_node_map[i].end_pfn;
  4133. if (start_pfn >= end_pfn)
  4134. continue;
  4135. /* Account for what is only usable for kernelcore */
  4136. if (start_pfn < usable_startpfn) {
  4137. unsigned long kernel_pages;
  4138. kernel_pages = min(end_pfn, usable_startpfn)
  4139. - start_pfn;
  4140. kernelcore_remaining -= min(kernel_pages,
  4141. kernelcore_remaining);
  4142. required_kernelcore -= min(kernel_pages,
  4143. required_kernelcore);
  4144. /* Continue if range is now fully accounted */
  4145. if (end_pfn <= usable_startpfn) {
  4146. /*
  4147. * Push zone_movable_pfn to the end so
  4148. * that if we have to rebalance
  4149. * kernelcore across nodes, we will
  4150. * not double account here
  4151. */
  4152. zone_movable_pfn[nid] = end_pfn;
  4153. continue;
  4154. }
  4155. start_pfn = usable_startpfn;
  4156. }
  4157. /*
  4158. * The usable PFN range for ZONE_MOVABLE is from
  4159. * start_pfn->end_pfn. Calculate size_pages as the
  4160. * number of pages used as kernelcore
  4161. */
  4162. size_pages = end_pfn - start_pfn;
  4163. if (size_pages > kernelcore_remaining)
  4164. size_pages = kernelcore_remaining;
  4165. zone_movable_pfn[nid] = start_pfn + size_pages;
  4166. /*
  4167. * Some kernelcore has been met, update counts and
  4168. * break if the kernelcore for this node has been
  4169. * satisified
  4170. */
  4171. required_kernelcore -= min(required_kernelcore,
  4172. size_pages);
  4173. kernelcore_remaining -= size_pages;
  4174. if (!kernelcore_remaining)
  4175. break;
  4176. }
  4177. }
  4178. /*
  4179. * If there is still required_kernelcore, we do another pass with one
  4180. * less node in the count. This will push zone_movable_pfn[nid] further
  4181. * along on the nodes that still have memory until kernelcore is
  4182. * satisified
  4183. */
  4184. usable_nodes--;
  4185. if (usable_nodes && required_kernelcore > usable_nodes)
  4186. goto restart;
  4187. /* Align start of ZONE_MOVABLE on all nids to MAX_ORDER_NR_PAGES */
  4188. for (nid = 0; nid < MAX_NUMNODES; nid++)
  4189. zone_movable_pfn[nid] =
  4190. roundup(zone_movable_pfn[nid], MAX_ORDER_NR_PAGES);
  4191. out:
  4192. /* restore the node_state */
  4193. node_states[N_HIGH_MEMORY] = saved_node_state;
  4194. }
  4195. /* Any regular memory on that node ? */
  4196. static void check_for_regular_memory(pg_data_t *pgdat)
  4197. {
  4198. #ifdef CONFIG_HIGHMEM
  4199. enum zone_type zone_type;
  4200. for (zone_type = 0; zone_type <= ZONE_NORMAL; zone_type++) {
  4201. struct zone *zone = &pgdat->node_zones[zone_type];
  4202. if (zone->present_pages)
  4203. node_set_state(zone_to_nid(zone), N_NORMAL_MEMORY);
  4204. }
  4205. #endif
  4206. }
  4207. /**
  4208. * free_area_init_nodes - Initialise all pg_data_t and zone data
  4209. * @max_zone_pfn: an array of max PFNs for each zone
  4210. *
  4211. * This will call free_area_init_node() for each active node in the system.
  4212. * Using the page ranges provided by add_active_range(), the size of each
  4213. * zone in each node and their holes is calculated. If the maximum PFN
  4214. * between two adjacent zones match, it is assumed that the zone is empty.
  4215. * For example, if arch_max_dma_pfn == arch_max_dma32_pfn, it is assumed
  4216. * that arch_max_dma32_pfn has no pages. It is also assumed that a zone
  4217. * starts where the previous one ended. For example, ZONE_DMA32 starts
  4218. * at arch_max_dma_pfn.
  4219. */
  4220. void __init free_area_init_nodes(unsigned long *max_zone_pfn)
  4221. {
  4222. unsigned long nid;
  4223. int i;
  4224. /* Sort early_node_map as initialisation assumes it is sorted */
  4225. sort_node_map();
  4226. /* Record where the zone boundaries are */
  4227. memset(arch_zone_lowest_possible_pfn, 0,
  4228. sizeof(arch_zone_lowest_possible_pfn));
  4229. memset(arch_zone_highest_possible_pfn, 0,
  4230. sizeof(arch_zone_highest_possible_pfn));
  4231. arch_zone_lowest_possible_pfn[0] = find_min_pfn_with_active_regions();
  4232. arch_zone_highest_possible_pfn[0] = max_zone_pfn[0];
  4233. for (i = 1; i < MAX_NR_ZONES; i++) {
  4234. if (i == ZONE_MOVABLE)
  4235. continue;
  4236. arch_zone_lowest_possible_pfn[i] =
  4237. arch_zone_highest_possible_pfn[i-1];
  4238. arch_zone_highest_possible_pfn[i] =
  4239. max(max_zone_pfn[i], arch_zone_lowest_possible_pfn[i]);
  4240. }
  4241. arch_zone_lowest_possible_pfn[ZONE_MOVABLE] = 0;
  4242. arch_zone_highest_possible_pfn[ZONE_MOVABLE] = 0;
  4243. /* Find the PFNs that ZONE_MOVABLE begins at in each node */
  4244. memset(zone_movable_pfn, 0, sizeof(zone_movable_pfn));
  4245. find_zone_movable_pfns_for_nodes(zone_movable_pfn);
  4246. /* Print out the zone ranges */
  4247. printk("Zone PFN ranges:\n");
  4248. for (i = 0; i < MAX_NR_ZONES; i++) {
  4249. if (i == ZONE_MOVABLE)
  4250. continue;
  4251. printk(" %-8s ", zone_names[i]);
  4252. if (arch_zone_lowest_possible_pfn[i] ==
  4253. arch_zone_highest_possible_pfn[i])
  4254. printk("empty\n");
  4255. else
  4256. printk("%0#10lx -> %0#10lx\n",
  4257. arch_zone_lowest_possible_pfn[i],
  4258. arch_zone_highest_possible_pfn[i]);
  4259. }
  4260. /* Print out the PFNs ZONE_MOVABLE begins at in each node */
  4261. printk("Movable zone start PFN for each node\n");
  4262. for (i = 0; i < MAX_NUMNODES; i++) {
  4263. if (zone_movable_pfn[i])
  4264. printk(" Node %d: %lu\n", i, zone_movable_pfn[i]);
  4265. }
  4266. /* Print out the early_node_map[] */
  4267. printk("early_node_map[%d] active PFN ranges\n", nr_nodemap_entries);
  4268. for (i = 0; i < nr_nodemap_entries; i++)
  4269. printk(" %3d: %0#10lx -> %0#10lx\n", early_node_map[i].nid,
  4270. early_node_map[i].start_pfn,
  4271. early_node_map[i].end_pfn);
  4272. /* Initialise every node */
  4273. mminit_verify_pageflags_layout();
  4274. setup_nr_node_ids();
  4275. for_each_online_node(nid) {
  4276. pg_data_t *pgdat = NODE_DATA(nid);
  4277. free_area_init_node(nid, NULL,
  4278. find_min_pfn_for_node(nid), NULL);
  4279. /* Any memory on that node */
  4280. if (pgdat->node_present_pages)
  4281. node_set_state(nid, N_HIGH_MEMORY);
  4282. check_for_regular_memory(pgdat);
  4283. }
  4284. }
  4285. static int __init cmdline_parse_core(char *p, unsigned long *core)
  4286. {
  4287. unsigned long long coremem;
  4288. if (!p)
  4289. return -EINVAL;
  4290. coremem = memparse(p, &p);
  4291. *core = coremem >> PAGE_SHIFT;
  4292. /* Paranoid check that UL is enough for the coremem value */
  4293. WARN_ON((coremem >> PAGE_SHIFT) > ULONG_MAX);
  4294. return 0;
  4295. }
  4296. /*
  4297. * kernelcore=size sets the amount of memory for use for allocations that
  4298. * cannot be reclaimed or migrated.
  4299. */
  4300. static int __init cmdline_parse_kernelcore(char *p)
  4301. {
  4302. return cmdline_parse_core(p, &required_kernelcore);
  4303. }
  4304. /*
  4305. * movablecore=size sets the amount of memory for use for allocations that
  4306. * can be reclaimed or migrated.
  4307. */
  4308. static int __init cmdline_parse_movablecore(char *p)
  4309. {
  4310. return cmdline_parse_core(p, &required_movablecore);
  4311. }
  4312. early_param("kernelcore", cmdline_parse_kernelcore);
  4313. early_param("movablecore", cmdline_parse_movablecore);
  4314. #endif /* CONFIG_ARCH_POPULATES_NODE_MAP */
  4315. /**
  4316. * set_dma_reserve - set the specified number of pages reserved in the first zone
  4317. * @new_dma_reserve: The number of pages to mark reserved
  4318. *
  4319. * The per-cpu batchsize and zone watermarks are determined by present_pages.
  4320. * In the DMA zone, a significant percentage may be consumed by kernel image
  4321. * and other unfreeable allocations which can skew the watermarks badly. This
  4322. * function may optionally be used to account for unfreeable pages in the
  4323. * first zone (e.g., ZONE_DMA). The effect will be lower watermarks and
  4324. * smaller per-cpu batchsize.
  4325. */
  4326. void __init set_dma_reserve(unsigned long new_dma_reserve)
  4327. {
  4328. dma_reserve = new_dma_reserve;
  4329. }
  4330. void __init free_area_init(unsigned long *zones_size)
  4331. {
  4332. free_area_init_node(0, zones_size,
  4333. __pa(PAGE_OFFSET) >> PAGE_SHIFT, NULL);
  4334. }
  4335. static int page_alloc_cpu_notify(struct notifier_block *self,
  4336. unsigned long action, void *hcpu)
  4337. {
  4338. int cpu = (unsigned long)hcpu;
  4339. if (action == CPU_DEAD || action == CPU_DEAD_FROZEN) {
  4340. drain_pages(cpu);
  4341. /*
  4342. * Spill the event counters of the dead processor
  4343. * into the current processors event counters.
  4344. * This artificially elevates the count of the current
  4345. * processor.
  4346. */
  4347. vm_events_fold_cpu(cpu);
  4348. /*
  4349. * Zero the differential counters of the dead processor
  4350. * so that the vm statistics are consistent.
  4351. *
  4352. * This is only okay since the processor is dead and cannot
  4353. * race with what we are doing.
  4354. */
  4355. refresh_cpu_vm_stats(cpu);
  4356. }
  4357. return NOTIFY_OK;
  4358. }
  4359. void __init page_alloc_init(void)
  4360. {
  4361. hotcpu_notifier(page_alloc_cpu_notify, 0);
  4362. }
  4363. /*
  4364. * calculate_totalreserve_pages - called when sysctl_lower_zone_reserve_ratio
  4365. * or min_free_kbytes changes.
  4366. */
  4367. static void calculate_totalreserve_pages(void)
  4368. {
  4369. struct pglist_data *pgdat;
  4370. unsigned long reserve_pages = 0;
  4371. enum zone_type i, j;
  4372. for_each_online_pgdat(pgdat) {
  4373. for (i = 0; i < MAX_NR_ZONES; i++) {
  4374. struct zone *zone = pgdat->node_zones + i;
  4375. unsigned long max = 0;
  4376. /* Find valid and maximum lowmem_reserve in the zone */
  4377. for (j = i; j < MAX_NR_ZONES; j++) {
  4378. if (zone->lowmem_reserve[j] > max)
  4379. max = zone->lowmem_reserve[j];
  4380. }
  4381. /* we treat the high watermark as reserved pages. */
  4382. max += high_wmark_pages(zone);
  4383. if (max > zone->present_pages)
  4384. max = zone->present_pages;
  4385. reserve_pages += max;
  4386. }
  4387. }
  4388. totalreserve_pages = reserve_pages;
  4389. }
  4390. /*
  4391. * setup_per_zone_lowmem_reserve - called whenever
  4392. * sysctl_lower_zone_reserve_ratio changes. Ensures that each zone
  4393. * has a correct pages reserved value, so an adequate number of
  4394. * pages are left in the zone after a successful __alloc_pages().
  4395. */
  4396. static void setup_per_zone_lowmem_reserve(void)
  4397. {
  4398. struct pglist_data *pgdat;
  4399. enum zone_type j, idx;
  4400. for_each_online_pgdat(pgdat) {
  4401. for (j = 0; j < MAX_NR_ZONES; j++) {
  4402. struct zone *zone = pgdat->node_zones + j;
  4403. unsigned long present_pages = zone->present_pages;
  4404. zone->lowmem_reserve[j] = 0;
  4405. idx = j;
  4406. while (idx) {
  4407. struct zone *lower_zone;
  4408. idx--;
  4409. if (sysctl_lowmem_reserve_ratio[idx] < 1)
  4410. sysctl_lowmem_reserve_ratio[idx] = 1;
  4411. lower_zone = pgdat->node_zones + idx;
  4412. lower_zone->lowmem_reserve[j] = present_pages /
  4413. sysctl_lowmem_reserve_ratio[idx];
  4414. present_pages += lower_zone->present_pages;
  4415. }
  4416. }
  4417. }
  4418. /* update totalreserve_pages */
  4419. calculate_totalreserve_pages();
  4420. }
  4421. /**
  4422. * setup_per_zone_wmarks - called when min_free_kbytes changes
  4423. * or when memory is hot-{added|removed}
  4424. *
  4425. * Ensures that the watermark[min,low,high] values for each zone are set
  4426. * correctly with respect to min_free_kbytes.
  4427. */
  4428. void setup_per_zone_wmarks(void)
  4429. {
  4430. unsigned long pages_min = min_free_kbytes >> (PAGE_SHIFT - 10);
  4431. unsigned long lowmem_pages = 0;
  4432. struct zone *zone;
  4433. unsigned long flags;
  4434. /* Calculate total number of !ZONE_HIGHMEM pages */
  4435. for_each_zone(zone) {
  4436. if (!is_highmem(zone))
  4437. lowmem_pages += zone->present_pages;
  4438. }
  4439. for_each_zone(zone) {
  4440. u64 tmp;
  4441. spin_lock_irqsave(&zone->lock, flags);
  4442. tmp = (u64)pages_min * zone->present_pages;
  4443. do_div(tmp, lowmem_pages);
  4444. if (is_highmem(zone)) {
  4445. /*
  4446. * __GFP_HIGH and PF_MEMALLOC allocations usually don't
  4447. * need highmem pages, so cap pages_min to a small
  4448. * value here.
  4449. *
  4450. * The WMARK_HIGH-WMARK_LOW and (WMARK_LOW-WMARK_MIN)
  4451. * deltas controls asynch page reclaim, and so should
  4452. * not be capped for highmem.
  4453. */
  4454. int min_pages;
  4455. min_pages = zone->present_pages / 1024;
  4456. if (min_pages < SWAP_CLUSTER_MAX)
  4457. min_pages = SWAP_CLUSTER_MAX;
  4458. if (min_pages > 128)
  4459. min_pages = 128;
  4460. zone->watermark[WMARK_MIN] = min_pages;
  4461. } else {
  4462. /*
  4463. * If it's a lowmem zone, reserve a number of pages
  4464. * proportionate to the zone's size.
  4465. */
  4466. zone->watermark[WMARK_MIN] = tmp;
  4467. }
  4468. zone->watermark[WMARK_LOW] = min_wmark_pages(zone) + (tmp >> 2);
  4469. zone->watermark[WMARK_HIGH] = min_wmark_pages(zone) + (tmp >> 1);
  4470. setup_zone_migrate_reserve(zone);
  4471. spin_unlock_irqrestore(&zone->lock, flags);
  4472. }
  4473. /* update totalreserve_pages */
  4474. calculate_totalreserve_pages();
  4475. }
  4476. /*
  4477. * The inactive anon list should be small enough that the VM never has to
  4478. * do too much work, but large enough that each inactive page has a chance
  4479. * to be referenced again before it is swapped out.
  4480. *
  4481. * The inactive_anon ratio is the target ratio of ACTIVE_ANON to
  4482. * INACTIVE_ANON pages on this zone's LRU, maintained by the
  4483. * pageout code. A zone->inactive_ratio of 3 means 3:1 or 25% of
  4484. * the anonymous pages are kept on the inactive list.
  4485. *
  4486. * total target max
  4487. * memory ratio inactive anon
  4488. * -------------------------------------
  4489. * 10MB 1 5MB
  4490. * 100MB 1 50MB
  4491. * 1GB 3 250MB
  4492. * 10GB 10 0.9GB
  4493. * 100GB 31 3GB
  4494. * 1TB 101 10GB
  4495. * 10TB 320 32GB
  4496. */
  4497. static void __meminit calculate_zone_inactive_ratio(struct zone *zone)
  4498. {
  4499. unsigned int gb, ratio;
  4500. /* Zone size in gigabytes */
  4501. gb = zone->present_pages >> (30 - PAGE_SHIFT);
  4502. if (gb)
  4503. ratio = int_sqrt(10 * gb);
  4504. else
  4505. ratio = 1;
  4506. zone->inactive_ratio = ratio;
  4507. }
  4508. static void __meminit setup_per_zone_inactive_ratio(void)
  4509. {
  4510. struct zone *zone;
  4511. for_each_zone(zone)
  4512. calculate_zone_inactive_ratio(zone);
  4513. }
  4514. /*
  4515. * Initialise min_free_kbytes.
  4516. *
  4517. * For small machines we want it small (128k min). For large machines
  4518. * we want it large (64MB max). But it is not linear, because network
  4519. * bandwidth does not increase linearly with machine size. We use
  4520. *
  4521. * min_free_kbytes = 4 * sqrt(lowmem_kbytes), for better accuracy:
  4522. * min_free_kbytes = sqrt(lowmem_kbytes * 16)
  4523. *
  4524. * which yields
  4525. *
  4526. * 16MB: 512k
  4527. * 32MB: 724k
  4528. * 64MB: 1024k
  4529. * 128MB: 1448k
  4530. * 256MB: 2048k
  4531. * 512MB: 2896k
  4532. * 1024MB: 4096k
  4533. * 2048MB: 5792k
  4534. * 4096MB: 8192k
  4535. * 8192MB: 11584k
  4536. * 16384MB: 16384k
  4537. */
  4538. int __meminit init_per_zone_wmark_min(void)
  4539. {
  4540. unsigned long lowmem_kbytes;
  4541. lowmem_kbytes = nr_free_buffer_pages() * (PAGE_SIZE >> 10);
  4542. min_free_kbytes = int_sqrt(lowmem_kbytes * 16);
  4543. if (min_free_kbytes < 128)
  4544. min_free_kbytes = 128;
  4545. if (min_free_kbytes > 65536)
  4546. min_free_kbytes = 65536;
  4547. setup_per_zone_wmarks();
  4548. refresh_zone_stat_thresholds();
  4549. setup_per_zone_lowmem_reserve();
  4550. setup_per_zone_inactive_ratio();
  4551. return 0;
  4552. }
  4553. module_init(init_per_zone_wmark_min)
  4554. /*
  4555. * min_free_kbytes_sysctl_handler - just a wrapper around proc_dointvec() so
  4556. * that we can call two helper functions whenever min_free_kbytes
  4557. * changes.
  4558. */
  4559. int min_free_kbytes_sysctl_handler(ctl_table *table, int write,
  4560. void __user *buffer, size_t *length, loff_t *ppos)
  4561. {
  4562. proc_dointvec(table, write, buffer, length, ppos);
  4563. if (write)
  4564. setup_per_zone_wmarks();
  4565. return 0;
  4566. }
  4567. #ifdef CONFIG_NUMA
  4568. int sysctl_min_unmapped_ratio_sysctl_handler(ctl_table *table, int write,
  4569. void __user *buffer, size_t *length, loff_t *ppos)
  4570. {
  4571. struct zone *zone;
  4572. int rc;
  4573. rc = proc_dointvec_minmax(table, write, buffer, length, ppos);
  4574. if (rc)
  4575. return rc;
  4576. for_each_zone(zone)
  4577. zone->min_unmapped_pages = (zone->present_pages *
  4578. sysctl_min_unmapped_ratio) / 100;
  4579. return 0;
  4580. }
  4581. int sysctl_min_slab_ratio_sysctl_handler(ctl_table *table, int write,
  4582. void __user *buffer, size_t *length, loff_t *ppos)
  4583. {
  4584. struct zone *zone;
  4585. int rc;
  4586. rc = proc_dointvec_minmax(table, write, buffer, length, ppos);
  4587. if (rc)
  4588. return rc;
  4589. for_each_zone(zone)
  4590. zone->min_slab_pages = (zone->present_pages *
  4591. sysctl_min_slab_ratio) / 100;
  4592. return 0;
  4593. }
  4594. #endif
  4595. /*
  4596. * lowmem_reserve_ratio_sysctl_handler - just a wrapper around
  4597. * proc_dointvec() so that we can call setup_per_zone_lowmem_reserve()
  4598. * whenever sysctl_lowmem_reserve_ratio changes.
  4599. *
  4600. * The reserve ratio obviously has absolutely no relation with the
  4601. * minimum watermarks. The lowmem reserve ratio can only make sense
  4602. * if in function of the boot time zone sizes.
  4603. */
  4604. int lowmem_reserve_ratio_sysctl_handler(ctl_table *table, int write,
  4605. void __user *buffer, size_t *length, loff_t *ppos)
  4606. {
  4607. proc_dointvec_minmax(table, write, buffer, length, ppos);
  4608. setup_per_zone_lowmem_reserve();
  4609. return 0;
  4610. }
  4611. /*
  4612. * percpu_pagelist_fraction - changes the pcp->high for each zone on each
  4613. * cpu. It is the fraction of total pages in each zone that a hot per cpu pagelist
  4614. * can have before it gets flushed back to buddy allocator.
  4615. */
  4616. int percpu_pagelist_fraction_sysctl_handler(ctl_table *table, int write,
  4617. void __user *buffer, size_t *length, loff_t *ppos)
  4618. {
  4619. struct zone *zone;
  4620. unsigned int cpu;
  4621. int ret;
  4622. ret = proc_dointvec_minmax(table, write, buffer, length, ppos);
  4623. if (!write || (ret == -EINVAL))
  4624. return ret;
  4625. for_each_populated_zone(zone) {
  4626. for_each_possible_cpu(cpu) {
  4627. unsigned long high;
  4628. high = zone->present_pages / percpu_pagelist_fraction;
  4629. setup_pagelist_highmark(
  4630. per_cpu_ptr(zone->pageset, cpu), high);
  4631. }
  4632. }
  4633. return 0;
  4634. }
  4635. int hashdist = HASHDIST_DEFAULT;
  4636. #ifdef CONFIG_NUMA
  4637. static int __init set_hashdist(char *str)
  4638. {
  4639. if (!str)
  4640. return 0;
  4641. hashdist = simple_strtoul(str, &str, 0);
  4642. return 1;
  4643. }
  4644. __setup("hashdist=", set_hashdist);
  4645. #endif
  4646. /*
  4647. * allocate a large system hash table from bootmem
  4648. * - it is assumed that the hash table must contain an exact power-of-2
  4649. * quantity of entries
  4650. * - limit is the number of hash buckets, not the total allocation size
  4651. */
  4652. void *__init alloc_large_system_hash(const char *tablename,
  4653. unsigned long bucketsize,
  4654. unsigned long numentries,
  4655. int scale,
  4656. int flags,
  4657. unsigned int *_hash_shift,
  4658. unsigned int *_hash_mask,
  4659. unsigned long limit)
  4660. {
  4661. unsigned long long max = limit;
  4662. unsigned long log2qty, size;
  4663. void *table = NULL;
  4664. /* allow the kernel cmdline to have a say */
  4665. if (!numentries) {
  4666. /* round applicable memory size up to nearest megabyte */
  4667. numentries = nr_kernel_pages;
  4668. numentries += (1UL << (20 - PAGE_SHIFT)) - 1;
  4669. numentries >>= 20 - PAGE_SHIFT;
  4670. numentries <<= 20 - PAGE_SHIFT;
  4671. /* limit to 1 bucket per 2^scale bytes of low memory */
  4672. if (scale > PAGE_SHIFT)
  4673. numentries >>= (scale - PAGE_SHIFT);
  4674. else
  4675. numentries <<= (PAGE_SHIFT - scale);
  4676. /* Make sure we've got at least a 0-order allocation.. */
  4677. if (unlikely(flags & HASH_SMALL)) {
  4678. /* Makes no sense without HASH_EARLY */
  4679. WARN_ON(!(flags & HASH_EARLY));
  4680. if (!(numentries >> *_hash_shift)) {
  4681. numentries = 1UL << *_hash_shift;
  4682. BUG_ON(!numentries);
  4683. }
  4684. } else if (unlikely((numentries * bucketsize) < PAGE_SIZE))
  4685. numentries = PAGE_SIZE / bucketsize;
  4686. }
  4687. numentries = roundup_pow_of_two(numentries);
  4688. /* limit allocation size to 1/16 total memory by default */
  4689. if (max == 0) {
  4690. max = ((unsigned long long)nr_all_pages << PAGE_SHIFT) >> 4;
  4691. do_div(max, bucketsize);
  4692. }
  4693. if (numentries > max)
  4694. numentries = max;
  4695. log2qty = ilog2(numentries);
  4696. do {
  4697. size = bucketsize << log2qty;
  4698. if (flags & HASH_EARLY)
  4699. table = alloc_bootmem_nopanic(size);
  4700. else if (hashdist)
  4701. table = __vmalloc(size, GFP_ATOMIC, PAGE_KERNEL);
  4702. else {
  4703. /*
  4704. * If bucketsize is not a power-of-two, we may free
  4705. * some pages at the end of hash table which
  4706. * alloc_pages_exact() automatically does
  4707. */
  4708. if (get_order(size) < MAX_ORDER) {
  4709. table = alloc_pages_exact(size, GFP_ATOMIC);
  4710. kmemleak_alloc(table, size, 1, GFP_ATOMIC);
  4711. }
  4712. }
  4713. } while (!table && size > PAGE_SIZE && --log2qty);
  4714. if (!table)
  4715. panic("Failed to allocate %s hash table\n", tablename);
  4716. printk(KERN_INFO "%s hash table entries: %ld (order: %d, %lu bytes)\n",
  4717. tablename,
  4718. (1UL << log2qty),
  4719. ilog2(size) - PAGE_SHIFT,
  4720. size);
  4721. if (_hash_shift)
  4722. *_hash_shift = log2qty;
  4723. if (_hash_mask)
  4724. *_hash_mask = (1 << log2qty) - 1;
  4725. return table;
  4726. }
  4727. /* Return a pointer to the bitmap storing bits affecting a block of pages */
  4728. static inline unsigned long *get_pageblock_bitmap(struct zone *zone,
  4729. unsigned long pfn)
  4730. {
  4731. #ifdef CONFIG_SPARSEMEM
  4732. return __pfn_to_section(pfn)->pageblock_flags;
  4733. #else
  4734. return zone->pageblock_flags;
  4735. #endif /* CONFIG_SPARSEMEM */
  4736. }
  4737. static inline int pfn_to_bitidx(struct zone *zone, unsigned long pfn)
  4738. {
  4739. #ifdef CONFIG_SPARSEMEM
  4740. pfn &= (PAGES_PER_SECTION-1);
  4741. return (pfn >> pageblock_order) * NR_PAGEBLOCK_BITS;
  4742. #else
  4743. pfn = pfn - zone->zone_start_pfn;
  4744. return (pfn >> pageblock_order) * NR_PAGEBLOCK_BITS;
  4745. #endif /* CONFIG_SPARSEMEM */
  4746. }
  4747. /**
  4748. * get_pageblock_flags_group - Return the requested group of flags for the pageblock_nr_pages block of pages
  4749. * @page: The page within the block of interest
  4750. * @start_bitidx: The first bit of interest to retrieve
  4751. * @end_bitidx: The last bit of interest
  4752. * returns pageblock_bits flags
  4753. */
  4754. unsigned long get_pageblock_flags_group(struct page *page,
  4755. int start_bitidx, int end_bitidx)
  4756. {
  4757. struct zone *zone;
  4758. unsigned long *bitmap;
  4759. unsigned long pfn, bitidx;
  4760. unsigned long flags = 0;
  4761. unsigned long value = 1;
  4762. zone = page_zone(page);
  4763. pfn = page_to_pfn(page);
  4764. bitmap = get_pageblock_bitmap(zone, pfn);
  4765. bitidx = pfn_to_bitidx(zone, pfn);
  4766. for (; start_bitidx <= end_bitidx; start_bitidx++, value <<= 1)
  4767. if (test_bit(bitidx + start_bitidx, bitmap))
  4768. flags |= value;
  4769. return flags;
  4770. }
  4771. /**
  4772. * set_pageblock_flags_group - Set the requested group of flags for a pageblock_nr_pages block of pages
  4773. * @page: The page within the block of interest
  4774. * @start_bitidx: The first bit of interest
  4775. * @end_bitidx: The last bit of interest
  4776. * @flags: The flags to set
  4777. */
  4778. void set_pageblock_flags_group(struct page *page, unsigned long flags,
  4779. int start_bitidx, int end_bitidx)
  4780. {
  4781. struct zone *zone;
  4782. unsigned long *bitmap;
  4783. unsigned long pfn, bitidx;
  4784. unsigned long value = 1;
  4785. zone = page_zone(page);
  4786. pfn = page_to_pfn(page);
  4787. bitmap = get_pageblock_bitmap(zone, pfn);
  4788. bitidx = pfn_to_bitidx(zone, pfn);
  4789. VM_BUG_ON(pfn < zone->zone_start_pfn);
  4790. VM_BUG_ON(pfn >= zone->zone_start_pfn + zone->spanned_pages);
  4791. for (; start_bitidx <= end_bitidx; start_bitidx++, value <<= 1)
  4792. if (flags & value)
  4793. __set_bit(bitidx + start_bitidx, bitmap);
  4794. else
  4795. __clear_bit(bitidx + start_bitidx, bitmap);
  4796. }
  4797. /*
  4798. * This is designed as sub function...plz see page_isolation.c also.
  4799. * set/clear page block's type to be ISOLATE.
  4800. * page allocater never alloc memory from ISOLATE block.
  4801. */
  4802. static int
  4803. __count_immobile_pages(struct zone *zone, struct page *page, int count)
  4804. {
  4805. unsigned long pfn, iter, found;
  4806. /*
  4807. * For avoiding noise data, lru_add_drain_all() should be called
  4808. * If ZONE_MOVABLE, the zone never contains immobile pages
  4809. */
  4810. if (zone_idx(zone) == ZONE_MOVABLE)
  4811. return true;
  4812. if (get_pageblock_migratetype(page) == MIGRATE_MOVABLE)
  4813. return true;
  4814. pfn = page_to_pfn(page);
  4815. for (found = 0, iter = 0; iter < pageblock_nr_pages; iter++) {
  4816. unsigned long check = pfn + iter;
  4817. if (!pfn_valid_within(check))
  4818. continue;
  4819. page = pfn_to_page(check);
  4820. if (!page_count(page)) {
  4821. if (PageBuddy(page))
  4822. iter += (1 << page_order(page)) - 1;
  4823. continue;
  4824. }
  4825. if (!PageLRU(page))
  4826. found++;
  4827. /*
  4828. * If there are RECLAIMABLE pages, we need to check it.
  4829. * But now, memory offline itself doesn't call shrink_slab()
  4830. * and it still to be fixed.
  4831. */
  4832. /*
  4833. * If the page is not RAM, page_count()should be 0.
  4834. * we don't need more check. This is an _used_ not-movable page.
  4835. *
  4836. * The problematic thing here is PG_reserved pages. PG_reserved
  4837. * is set to both of a memory hole page and a _used_ kernel
  4838. * page at boot.
  4839. */
  4840. if (found > count)
  4841. return false;
  4842. }
  4843. return true;
  4844. }
  4845. bool is_pageblock_removable_nolock(struct page *page)
  4846. {
  4847. struct zone *zone = page_zone(page);
  4848. return __count_immobile_pages(zone, page, 0);
  4849. }
  4850. int set_migratetype_isolate(struct page *page)
  4851. {
  4852. struct zone *zone;
  4853. unsigned long flags, pfn;
  4854. struct memory_isolate_notify arg;
  4855. int notifier_ret;
  4856. int ret = -EBUSY;
  4857. zone = page_zone(page);
  4858. spin_lock_irqsave(&zone->lock, flags);
  4859. pfn = page_to_pfn(page);
  4860. arg.start_pfn = pfn;
  4861. arg.nr_pages = pageblock_nr_pages;
  4862. arg.pages_found = 0;
  4863. /*
  4864. * It may be possible to isolate a pageblock even if the
  4865. * migratetype is not MIGRATE_MOVABLE. The memory isolation
  4866. * notifier chain is used by balloon drivers to return the
  4867. * number of pages in a range that are held by the balloon
  4868. * driver to shrink memory. If all the pages are accounted for
  4869. * by balloons, are free, or on the LRU, isolation can continue.
  4870. * Later, for example, when memory hotplug notifier runs, these
  4871. * pages reported as "can be isolated" should be isolated(freed)
  4872. * by the balloon driver through the memory notifier chain.
  4873. */
  4874. notifier_ret = memory_isolate_notify(MEM_ISOLATE_COUNT, &arg);
  4875. notifier_ret = notifier_to_errno(notifier_ret);
  4876. if (notifier_ret)
  4877. goto out;
  4878. /*
  4879. * FIXME: Now, memory hotplug doesn't call shrink_slab() by itself.
  4880. * We just check MOVABLE pages.
  4881. */
  4882. if (__count_immobile_pages(zone, page, arg.pages_found))
  4883. ret = 0;
  4884. /*
  4885. * immobile means "not-on-lru" paes. If immobile is larger than
  4886. * removable-by-driver pages reported by notifier, we'll fail.
  4887. */
  4888. out:
  4889. if (!ret) {
  4890. set_pageblock_migratetype(page, MIGRATE_ISOLATE);
  4891. move_freepages_block(zone, page, MIGRATE_ISOLATE);
  4892. }
  4893. spin_unlock_irqrestore(&zone->lock, flags);
  4894. if (!ret)
  4895. drain_all_pages();
  4896. return ret;
  4897. }
  4898. void unset_migratetype_isolate(struct page *page)
  4899. {
  4900. struct zone *zone;
  4901. unsigned long flags;
  4902. zone = page_zone(page);
  4903. spin_lock_irqsave(&zone->lock, flags);
  4904. if (get_pageblock_migratetype(page) != MIGRATE_ISOLATE)
  4905. goto out;
  4906. set_pageblock_migratetype(page, MIGRATE_MOVABLE);
  4907. move_freepages_block(zone, page, MIGRATE_MOVABLE);
  4908. out:
  4909. spin_unlock_irqrestore(&zone->lock, flags);
  4910. }
  4911. #ifdef CONFIG_MEMORY_HOTREMOVE
  4912. /*
  4913. * All pages in the range must be isolated before calling this.
  4914. */
  4915. void
  4916. __offline_isolated_pages(unsigned long start_pfn, unsigned long end_pfn)
  4917. {
  4918. struct page *page;
  4919. struct zone *zone;
  4920. int order, i;
  4921. unsigned long pfn;
  4922. unsigned long flags;
  4923. /* find the first valid pfn */
  4924. for (pfn = start_pfn; pfn < end_pfn; pfn++)
  4925. if (pfn_valid(pfn))
  4926. break;
  4927. if (pfn == end_pfn)
  4928. return;
  4929. zone = page_zone(pfn_to_page(pfn));
  4930. spin_lock_irqsave(&zone->lock, flags);
  4931. pfn = start_pfn;
  4932. while (pfn < end_pfn) {
  4933. if (!pfn_valid(pfn)) {
  4934. pfn++;
  4935. continue;
  4936. }
  4937. page = pfn_to_page(pfn);
  4938. BUG_ON(page_count(page));
  4939. BUG_ON(!PageBuddy(page));
  4940. order = page_order(page);
  4941. #ifdef CONFIG_DEBUG_VM
  4942. printk(KERN_INFO "remove from free list %lx %d %lx\n",
  4943. pfn, 1 << order, end_pfn);
  4944. #endif
  4945. list_del(&page->lru);
  4946. rmv_page_order(page);
  4947. zone->free_area[order].nr_free--;
  4948. __mod_zone_page_state(zone, NR_FREE_PAGES,
  4949. - (1UL << order));
  4950. for (i = 0; i < (1 << order); i++)
  4951. SetPageReserved((page+i));
  4952. pfn += (1 << order);
  4953. }
  4954. spin_unlock_irqrestore(&zone->lock, flags);
  4955. }
  4956. #endif
  4957. #ifdef CONFIG_MEMORY_FAILURE
  4958. bool is_free_buddy_page(struct page *page)
  4959. {
  4960. struct zone *zone = page_zone(page);
  4961. unsigned long pfn = page_to_pfn(page);
  4962. unsigned long flags;
  4963. int order;
  4964. spin_lock_irqsave(&zone->lock, flags);
  4965. for (order = 0; order < MAX_ORDER; order++) {
  4966. struct page *page_head = page - (pfn & ((1 << order) - 1));
  4967. if (PageBuddy(page_head) && page_order(page_head) >= order)
  4968. break;
  4969. }
  4970. spin_unlock_irqrestore(&zone->lock, flags);
  4971. return order < MAX_ORDER;
  4972. }
  4973. #endif
  4974. static struct trace_print_flags pageflag_names[] = {
  4975. {1UL << PG_locked, "locked" },
  4976. {1UL << PG_error, "error" },
  4977. {1UL << PG_referenced, "referenced" },
  4978. {1UL << PG_uptodate, "uptodate" },
  4979. {1UL << PG_dirty, "dirty" },
  4980. {1UL << PG_lru, "lru" },
  4981. {1UL << PG_active, "active" },
  4982. {1UL << PG_slab, "slab" },
  4983. {1UL << PG_owner_priv_1, "owner_priv_1" },
  4984. {1UL << PG_arch_1, "arch_1" },
  4985. {1UL << PG_reserved, "reserved" },
  4986. {1UL << PG_private, "private" },
  4987. {1UL << PG_private_2, "private_2" },
  4988. {1UL << PG_writeback, "writeback" },
  4989. #ifdef CONFIG_PAGEFLAGS_EXTENDED
  4990. {1UL << PG_head, "head" },
  4991. {1UL << PG_tail, "tail" },
  4992. #else
  4993. {1UL << PG_compound, "compound" },
  4994. #endif
  4995. {1UL << PG_swapcache, "swapcache" },
  4996. {1UL << PG_mappedtodisk, "mappedtodisk" },
  4997. {1UL << PG_reclaim, "reclaim" },
  4998. {1UL << PG_swapbacked, "swapbacked" },
  4999. {1UL << PG_unevictable, "unevictable" },
  5000. #ifdef CONFIG_MMU
  5001. {1UL << PG_mlocked, "mlocked" },
  5002. #endif
  5003. #ifdef CONFIG_ARCH_USES_PG_UNCACHED
  5004. {1UL << PG_uncached, "uncached" },
  5005. #endif
  5006. #ifdef CONFIG_MEMORY_FAILURE
  5007. {1UL << PG_hwpoison, "hwpoison" },
  5008. #endif
  5009. {-1UL, NULL },
  5010. };
  5011. static void dump_page_flags(unsigned long flags)
  5012. {
  5013. const char *delim = "";
  5014. unsigned long mask;
  5015. int i;
  5016. printk(KERN_ALERT "page flags: %#lx(", flags);
  5017. /* remove zone id */
  5018. flags &= (1UL << NR_PAGEFLAGS) - 1;
  5019. for (i = 0; pageflag_names[i].name && flags; i++) {
  5020. mask = pageflag_names[i].mask;
  5021. if ((flags & mask) != mask)
  5022. continue;
  5023. flags &= ~mask;
  5024. printk("%s%s", delim, pageflag_names[i].name);
  5025. delim = "|";
  5026. }
  5027. /* check for left over flags */
  5028. if (flags)
  5029. printk("%s%#lx", delim, flags);
  5030. printk(")\n");
  5031. }
  5032. void dump_page(struct page *page)
  5033. {
  5034. printk(KERN_ALERT
  5035. "page:%p count:%d mapcount:%d mapping:%p index:%#lx\n",
  5036. page, atomic_read(&page->_count), page_mapcount(page),
  5037. page->mapping, page->index);
  5038. dump_page_flags(page->flags);
  5039. mem_cgroup_print_bad_page(page);
  5040. }