page_alloc.c 157 KB

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