page_alloc.c 135 KB

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