page_alloc.c 151 KB

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