page_alloc.c 139 KB

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