page_alloc.c 107 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937
  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/bootmem.h>
  22. #include <linux/compiler.h>
  23. #include <linux/kernel.h>
  24. #include <linux/module.h>
  25. #include <linux/suspend.h>
  26. #include <linux/pagevec.h>
  27. #include <linux/blkdev.h>
  28. #include <linux/slab.h>
  29. #include <linux/notifier.h>
  30. #include <linux/topology.h>
  31. #include <linux/sysctl.h>
  32. #include <linux/cpu.h>
  33. #include <linux/cpuset.h>
  34. #include <linux/memory_hotplug.h>
  35. #include <linux/nodemask.h>
  36. #include <linux/vmalloc.h>
  37. #include <linux/mempolicy.h>
  38. #include <linux/stop_machine.h>
  39. #include <linux/sort.h>
  40. #include <linux/pfn.h>
  41. #include <linux/backing-dev.h>
  42. #include <linux/fault-inject.h>
  43. #include <asm/tlbflush.h>
  44. #include <asm/div64.h>
  45. #include "internal.h"
  46. /*
  47. * MCD - HACK: Find somewhere to initialize this EARLY, or make this
  48. * initializer cleaner
  49. */
  50. nodemask_t node_online_map __read_mostly = { { [0] = 1UL } };
  51. EXPORT_SYMBOL(node_online_map);
  52. nodemask_t node_possible_map __read_mostly = NODE_MASK_ALL;
  53. EXPORT_SYMBOL(node_possible_map);
  54. unsigned long totalram_pages __read_mostly;
  55. unsigned long totalreserve_pages __read_mostly;
  56. long nr_swap_pages;
  57. int percpu_pagelist_fraction;
  58. static void __free_pages_ok(struct page *page, unsigned int order);
  59. /*
  60. * results with 256, 32 in the lowmem_reserve sysctl:
  61. * 1G machine -> (16M dma, 800M-16M normal, 1G-800M high)
  62. * 1G machine -> (16M dma, 784M normal, 224M high)
  63. * NORMAL allocation will leave 784M/256 of ram reserved in the ZONE_DMA
  64. * HIGHMEM allocation will leave 224M/32 of ram reserved in ZONE_NORMAL
  65. * HIGHMEM allocation will (224M+784M)/256 of ram reserved in ZONE_DMA
  66. *
  67. * TBD: should special case ZONE_DMA32 machines here - in those we normally
  68. * don't need any ZONE_NORMAL reservation
  69. */
  70. int sysctl_lowmem_reserve_ratio[MAX_NR_ZONES-1] = {
  71. #ifdef CONFIG_ZONE_DMA
  72. 256,
  73. #endif
  74. #ifdef CONFIG_ZONE_DMA32
  75. 256,
  76. #endif
  77. #ifdef CONFIG_HIGHMEM
  78. 32,
  79. #endif
  80. 32,
  81. };
  82. EXPORT_SYMBOL(totalram_pages);
  83. static char * const zone_names[MAX_NR_ZONES] = {
  84. #ifdef CONFIG_ZONE_DMA
  85. "DMA",
  86. #endif
  87. #ifdef CONFIG_ZONE_DMA32
  88. "DMA32",
  89. #endif
  90. "Normal",
  91. #ifdef CONFIG_HIGHMEM
  92. "HighMem",
  93. #endif
  94. "Movable",
  95. };
  96. int min_free_kbytes = 1024;
  97. unsigned long __meminitdata nr_kernel_pages;
  98. unsigned long __meminitdata nr_all_pages;
  99. static unsigned long __meminitdata dma_reserve;
  100. #ifdef CONFIG_ARCH_POPULATES_NODE_MAP
  101. /*
  102. * MAX_ACTIVE_REGIONS determines the maxmimum number of distinct
  103. * ranges of memory (RAM) that may be registered with add_active_range().
  104. * Ranges passed to add_active_range() will be merged if possible
  105. * so the number of times add_active_range() can be called is
  106. * related to the number of nodes and the number of holes
  107. */
  108. #ifdef CONFIG_MAX_ACTIVE_REGIONS
  109. /* Allow an architecture to set MAX_ACTIVE_REGIONS to save memory */
  110. #define MAX_ACTIVE_REGIONS CONFIG_MAX_ACTIVE_REGIONS
  111. #else
  112. #if MAX_NUMNODES >= 32
  113. /* If there can be many nodes, allow up to 50 holes per node */
  114. #define MAX_ACTIVE_REGIONS (MAX_NUMNODES*50)
  115. #else
  116. /* By default, allow up to 256 distinct regions */
  117. #define MAX_ACTIVE_REGIONS 256
  118. #endif
  119. #endif
  120. static struct node_active_region __meminitdata early_node_map[MAX_ACTIVE_REGIONS];
  121. static int __meminitdata nr_nodemap_entries;
  122. static unsigned long __meminitdata arch_zone_lowest_possible_pfn[MAX_NR_ZONES];
  123. static unsigned long __meminitdata arch_zone_highest_possible_pfn[MAX_NR_ZONES];
  124. #ifdef CONFIG_MEMORY_HOTPLUG_RESERVE
  125. static unsigned long __meminitdata node_boundary_start_pfn[MAX_NUMNODES];
  126. static unsigned long __meminitdata node_boundary_end_pfn[MAX_NUMNODES];
  127. #endif /* CONFIG_MEMORY_HOTPLUG_RESERVE */
  128. unsigned long __initdata required_kernelcore;
  129. unsigned long __initdata required_movablecore;
  130. unsigned long __meminitdata zone_movable_pfn[MAX_NUMNODES];
  131. /* movable_zone is the "real" zone pages in ZONE_MOVABLE are taken from */
  132. int movable_zone;
  133. EXPORT_SYMBOL(movable_zone);
  134. #endif /* CONFIG_ARCH_POPULATES_NODE_MAP */
  135. #if MAX_NUMNODES > 1
  136. int nr_node_ids __read_mostly = MAX_NUMNODES;
  137. EXPORT_SYMBOL(nr_node_ids);
  138. #endif
  139. #ifdef CONFIG_DEBUG_VM
  140. static int page_outside_zone_boundaries(struct zone *zone, struct page *page)
  141. {
  142. int ret = 0;
  143. unsigned seq;
  144. unsigned long pfn = page_to_pfn(page);
  145. do {
  146. seq = zone_span_seqbegin(zone);
  147. if (pfn >= zone->zone_start_pfn + zone->spanned_pages)
  148. ret = 1;
  149. else if (pfn < zone->zone_start_pfn)
  150. ret = 1;
  151. } while (zone_span_seqretry(zone, seq));
  152. return ret;
  153. }
  154. static int page_is_consistent(struct zone *zone, struct page *page)
  155. {
  156. if (!pfn_valid_within(page_to_pfn(page)))
  157. return 0;
  158. if (zone != page_zone(page))
  159. return 0;
  160. return 1;
  161. }
  162. /*
  163. * Temporary debugging check for pages not lying within a given zone.
  164. */
  165. static int bad_range(struct zone *zone, struct page *page)
  166. {
  167. if (page_outside_zone_boundaries(zone, page))
  168. return 1;
  169. if (!page_is_consistent(zone, page))
  170. return 1;
  171. return 0;
  172. }
  173. #else
  174. static inline int bad_range(struct zone *zone, struct page *page)
  175. {
  176. return 0;
  177. }
  178. #endif
  179. static void bad_page(struct page *page)
  180. {
  181. printk(KERN_EMERG "Bad page state in process '%s'\n"
  182. KERN_EMERG "page:%p flags:0x%0*lx mapping:%p mapcount:%d count:%d\n"
  183. KERN_EMERG "Trying to fix it up, but a reboot is needed\n"
  184. KERN_EMERG "Backtrace:\n",
  185. current->comm, page, (int)(2*sizeof(unsigned long)),
  186. (unsigned long)page->flags, page->mapping,
  187. page_mapcount(page), page_count(page));
  188. dump_stack();
  189. page->flags &= ~(1 << PG_lru |
  190. 1 << PG_private |
  191. 1 << PG_locked |
  192. 1 << PG_active |
  193. 1 << PG_dirty |
  194. 1 << PG_reclaim |
  195. 1 << PG_slab |
  196. 1 << PG_swapcache |
  197. 1 << PG_writeback |
  198. 1 << PG_buddy );
  199. set_page_count(page, 0);
  200. reset_page_mapcount(page);
  201. page->mapping = NULL;
  202. add_taint(TAINT_BAD_PAGE);
  203. }
  204. /*
  205. * Higher-order pages are called "compound pages". They are structured thusly:
  206. *
  207. * The first PAGE_SIZE page is called the "head page".
  208. *
  209. * The remaining PAGE_SIZE pages are called "tail pages".
  210. *
  211. * All pages have PG_compound set. All pages have their ->private pointing at
  212. * the head page (even the head page has this).
  213. *
  214. * The first tail page's ->lru.next holds the address of the compound page's
  215. * put_page() function. Its ->lru.prev holds the order of allocation.
  216. * This usage means that zero-order pages may not be compound.
  217. */
  218. static void free_compound_page(struct page *page)
  219. {
  220. __free_pages_ok(page, compound_order(page));
  221. }
  222. static void prep_compound_page(struct page *page, unsigned long order)
  223. {
  224. int i;
  225. int nr_pages = 1 << order;
  226. set_compound_page_dtor(page, free_compound_page);
  227. set_compound_order(page, order);
  228. __SetPageHead(page);
  229. for (i = 1; i < nr_pages; i++) {
  230. struct page *p = page + i;
  231. __SetPageTail(p);
  232. p->first_page = page;
  233. }
  234. }
  235. static void destroy_compound_page(struct page *page, unsigned long order)
  236. {
  237. int i;
  238. int nr_pages = 1 << order;
  239. if (unlikely(compound_order(page) != order))
  240. bad_page(page);
  241. if (unlikely(!PageHead(page)))
  242. bad_page(page);
  243. __ClearPageHead(page);
  244. for (i = 1; i < nr_pages; i++) {
  245. struct page *p = page + i;
  246. if (unlikely(!PageTail(p) |
  247. (p->first_page != page)))
  248. bad_page(page);
  249. __ClearPageTail(p);
  250. }
  251. }
  252. static inline void prep_zero_page(struct page *page, int order, gfp_t gfp_flags)
  253. {
  254. int i;
  255. VM_BUG_ON((gfp_flags & (__GFP_WAIT | __GFP_HIGHMEM)) == __GFP_HIGHMEM);
  256. /*
  257. * clear_highpage() will use KM_USER0, so it's a bug to use __GFP_ZERO
  258. * and __GFP_HIGHMEM from hard or soft interrupt context.
  259. */
  260. VM_BUG_ON((gfp_flags & __GFP_HIGHMEM) && in_interrupt());
  261. for (i = 0; i < (1 << order); i++)
  262. clear_highpage(page + i);
  263. }
  264. /*
  265. * function for dealing with page's order in buddy system.
  266. * zone->lock is already acquired when we use these.
  267. * So, we don't need atomic page->flags operations here.
  268. */
  269. static inline unsigned long page_order(struct page *page)
  270. {
  271. return page_private(page);
  272. }
  273. static inline void set_page_order(struct page *page, int order)
  274. {
  275. set_page_private(page, order);
  276. __SetPageBuddy(page);
  277. }
  278. static inline void rmv_page_order(struct page *page)
  279. {
  280. __ClearPageBuddy(page);
  281. set_page_private(page, 0);
  282. }
  283. /*
  284. * Locate the struct page for both the matching buddy in our
  285. * pair (buddy1) and the combined O(n+1) page they form (page).
  286. *
  287. * 1) Any buddy B1 will have an order O twin B2 which satisfies
  288. * the following equation:
  289. * B2 = B1 ^ (1 << O)
  290. * For example, if the starting buddy (buddy2) is #8 its order
  291. * 1 buddy is #10:
  292. * B2 = 8 ^ (1 << 1) = 8 ^ 2 = 10
  293. *
  294. * 2) Any buddy B will have an order O+1 parent P which
  295. * satisfies the following equation:
  296. * P = B & ~(1 << O)
  297. *
  298. * Assumption: *_mem_map is contiguous at least up to MAX_ORDER
  299. */
  300. static inline struct page *
  301. __page_find_buddy(struct page *page, unsigned long page_idx, unsigned int order)
  302. {
  303. unsigned long buddy_idx = page_idx ^ (1 << order);
  304. return page + (buddy_idx - page_idx);
  305. }
  306. static inline unsigned long
  307. __find_combined_index(unsigned long page_idx, unsigned int order)
  308. {
  309. return (page_idx & ~(1 << order));
  310. }
  311. /*
  312. * This function checks whether a page is free && is the buddy
  313. * we can do coalesce a page and its buddy if
  314. * (a) the buddy is not in a hole &&
  315. * (b) the buddy is in the buddy system &&
  316. * (c) a page and its buddy have the same order &&
  317. * (d) a page and its buddy are in the same zone.
  318. *
  319. * For recording whether a page is in the buddy system, we use PG_buddy.
  320. * Setting, clearing, and testing PG_buddy is serialized by zone->lock.
  321. *
  322. * For recording page's order, we use page_private(page).
  323. */
  324. static inline int page_is_buddy(struct page *page, struct page *buddy,
  325. int order)
  326. {
  327. if (!pfn_valid_within(page_to_pfn(buddy)))
  328. return 0;
  329. if (page_zone_id(page) != page_zone_id(buddy))
  330. return 0;
  331. if (PageBuddy(buddy) && page_order(buddy) == order) {
  332. BUG_ON(page_count(buddy) != 0);
  333. return 1;
  334. }
  335. return 0;
  336. }
  337. /*
  338. * Freeing function for a buddy system allocator.
  339. *
  340. * The concept of a buddy system is to maintain direct-mapped table
  341. * (containing bit values) for memory blocks of various "orders".
  342. * The bottom level table contains the map for the smallest allocatable
  343. * units of memory (here, pages), and each level above it describes
  344. * pairs of units from the levels below, hence, "buddies".
  345. * At a high level, all that happens here is marking the table entry
  346. * at the bottom level available, and propagating the changes upward
  347. * as necessary, plus some accounting needed to play nicely with other
  348. * parts of the VM system.
  349. * At each level, we keep a list of pages, which are heads of continuous
  350. * free pages of length of (1 << order) and marked with PG_buddy. Page's
  351. * order is recorded in page_private(page) field.
  352. * So when we are allocating or freeing one, we can derive the state of the
  353. * other. That is, if we allocate a small block, and both were
  354. * free, the remainder of the region must be split into blocks.
  355. * If a block is freed, and its buddy is also free, then this
  356. * triggers coalescing into a block of larger size.
  357. *
  358. * -- wli
  359. */
  360. static inline void __free_one_page(struct page *page,
  361. struct zone *zone, unsigned int order)
  362. {
  363. unsigned long page_idx;
  364. int order_size = 1 << order;
  365. if (unlikely(PageCompound(page)))
  366. destroy_compound_page(page, order);
  367. page_idx = page_to_pfn(page) & ((1 << MAX_ORDER) - 1);
  368. VM_BUG_ON(page_idx & (order_size - 1));
  369. VM_BUG_ON(bad_range(zone, page));
  370. __mod_zone_page_state(zone, NR_FREE_PAGES, order_size);
  371. while (order < MAX_ORDER-1) {
  372. unsigned long combined_idx;
  373. struct free_area *area;
  374. struct page *buddy;
  375. buddy = __page_find_buddy(page, page_idx, order);
  376. if (!page_is_buddy(page, buddy, order))
  377. break; /* Move the buddy up one level. */
  378. list_del(&buddy->lru);
  379. area = zone->free_area + order;
  380. area->nr_free--;
  381. rmv_page_order(buddy);
  382. combined_idx = __find_combined_index(page_idx, order);
  383. page = page + (combined_idx - page_idx);
  384. page_idx = combined_idx;
  385. order++;
  386. }
  387. set_page_order(page, order);
  388. list_add(&page->lru, &zone->free_area[order].free_list);
  389. zone->free_area[order].nr_free++;
  390. }
  391. static inline int free_pages_check(struct page *page)
  392. {
  393. if (unlikely(page_mapcount(page) |
  394. (page->mapping != NULL) |
  395. (page_count(page) != 0) |
  396. (page->flags & (
  397. 1 << PG_lru |
  398. 1 << PG_private |
  399. 1 << PG_locked |
  400. 1 << PG_active |
  401. 1 << PG_slab |
  402. 1 << PG_swapcache |
  403. 1 << PG_writeback |
  404. 1 << PG_reserved |
  405. 1 << PG_buddy ))))
  406. bad_page(page);
  407. if (PageDirty(page))
  408. __ClearPageDirty(page);
  409. /*
  410. * For now, we report if PG_reserved was found set, but do not
  411. * clear it, and do not free the page. But we shall soon need
  412. * to do more, for when the ZERO_PAGE count wraps negative.
  413. */
  414. return PageReserved(page);
  415. }
  416. /*
  417. * Frees a list of pages.
  418. * Assumes all pages on list are in same zone, and of same order.
  419. * count is the number of pages to free.
  420. *
  421. * If the zone was previously in an "all pages pinned" state then look to
  422. * see if this freeing clears that state.
  423. *
  424. * And clear the zone's pages_scanned counter, to hold off the "all pages are
  425. * pinned" detection logic.
  426. */
  427. static void free_pages_bulk(struct zone *zone, int count,
  428. struct list_head *list, int order)
  429. {
  430. spin_lock(&zone->lock);
  431. zone->all_unreclaimable = 0;
  432. zone->pages_scanned = 0;
  433. while (count--) {
  434. struct page *page;
  435. VM_BUG_ON(list_empty(list));
  436. page = list_entry(list->prev, struct page, lru);
  437. /* have to delete it as __free_one_page list manipulates */
  438. list_del(&page->lru);
  439. __free_one_page(page, zone, order);
  440. }
  441. spin_unlock(&zone->lock);
  442. }
  443. static void free_one_page(struct zone *zone, struct page *page, int order)
  444. {
  445. spin_lock(&zone->lock);
  446. zone->all_unreclaimable = 0;
  447. zone->pages_scanned = 0;
  448. __free_one_page(page, zone, order);
  449. spin_unlock(&zone->lock);
  450. }
  451. static void __free_pages_ok(struct page *page, unsigned int order)
  452. {
  453. unsigned long flags;
  454. int i;
  455. int reserved = 0;
  456. for (i = 0 ; i < (1 << order) ; ++i)
  457. reserved += free_pages_check(page + i);
  458. if (reserved)
  459. return;
  460. if (!PageHighMem(page))
  461. debug_check_no_locks_freed(page_address(page),PAGE_SIZE<<order);
  462. arch_free_page(page, order);
  463. kernel_map_pages(page, 1 << order, 0);
  464. local_irq_save(flags);
  465. __count_vm_events(PGFREE, 1 << order);
  466. free_one_page(page_zone(page), page, order);
  467. local_irq_restore(flags);
  468. }
  469. /*
  470. * permit the bootmem allocator to evade page validation on high-order frees
  471. */
  472. void fastcall __init __free_pages_bootmem(struct page *page, unsigned int order)
  473. {
  474. if (order == 0) {
  475. __ClearPageReserved(page);
  476. set_page_count(page, 0);
  477. set_page_refcounted(page);
  478. __free_page(page);
  479. } else {
  480. int loop;
  481. prefetchw(page);
  482. for (loop = 0; loop < BITS_PER_LONG; loop++) {
  483. struct page *p = &page[loop];
  484. if (loop + 1 < BITS_PER_LONG)
  485. prefetchw(p + 1);
  486. __ClearPageReserved(p);
  487. set_page_count(p, 0);
  488. }
  489. set_page_refcounted(page);
  490. __free_pages(page, order);
  491. }
  492. }
  493. /*
  494. * The order of subdivision here is critical for the IO subsystem.
  495. * Please do not alter this order without good reasons and regression
  496. * testing. Specifically, as large blocks of memory are subdivided,
  497. * the order in which smaller blocks are delivered depends on the order
  498. * they're subdivided in this function. This is the primary factor
  499. * influencing the order in which pages are delivered to the IO
  500. * subsystem according to empirical testing, and this is also justified
  501. * by considering the behavior of a buddy system containing a single
  502. * large block of memory acted on by a series of small allocations.
  503. * This behavior is a critical factor in sglist merging's success.
  504. *
  505. * -- wli
  506. */
  507. static inline void expand(struct zone *zone, struct page *page,
  508. int low, int high, struct free_area *area)
  509. {
  510. unsigned long size = 1 << high;
  511. while (high > low) {
  512. area--;
  513. high--;
  514. size >>= 1;
  515. VM_BUG_ON(bad_range(zone, &page[size]));
  516. list_add(&page[size].lru, &area->free_list);
  517. area->nr_free++;
  518. set_page_order(&page[size], high);
  519. }
  520. }
  521. /*
  522. * This page is about to be returned from the page allocator
  523. */
  524. static int prep_new_page(struct page *page, int order, gfp_t gfp_flags)
  525. {
  526. if (unlikely(page_mapcount(page) |
  527. (page->mapping != NULL) |
  528. (page_count(page) != 0) |
  529. (page->flags & (
  530. 1 << PG_lru |
  531. 1 << PG_private |
  532. 1 << PG_locked |
  533. 1 << PG_active |
  534. 1 << PG_dirty |
  535. 1 << PG_slab |
  536. 1 << PG_swapcache |
  537. 1 << PG_writeback |
  538. 1 << PG_reserved |
  539. 1 << PG_buddy ))))
  540. bad_page(page);
  541. /*
  542. * For now, we report if PG_reserved was found set, but do not
  543. * clear it, and do not allocate the page: as a safety net.
  544. */
  545. if (PageReserved(page))
  546. return 1;
  547. page->flags &= ~(1 << PG_uptodate | 1 << PG_error | 1 << PG_readahead |
  548. 1 << PG_referenced | 1 << PG_arch_1 |
  549. 1 << PG_owner_priv_1 | 1 << PG_mappedtodisk);
  550. set_page_private(page, 0);
  551. set_page_refcounted(page);
  552. arch_alloc_page(page, order);
  553. kernel_map_pages(page, 1 << order, 1);
  554. if (gfp_flags & __GFP_ZERO)
  555. prep_zero_page(page, order, gfp_flags);
  556. if (order && (gfp_flags & __GFP_COMP))
  557. prep_compound_page(page, order);
  558. return 0;
  559. }
  560. /*
  561. * Do the hard work of removing an element from the buddy allocator.
  562. * Call me with the zone->lock already held.
  563. */
  564. static struct page *__rmqueue(struct zone *zone, unsigned int order)
  565. {
  566. struct free_area * area;
  567. unsigned int current_order;
  568. struct page *page;
  569. for (current_order = order; current_order < MAX_ORDER; ++current_order) {
  570. area = zone->free_area + current_order;
  571. if (list_empty(&area->free_list))
  572. continue;
  573. page = list_entry(area->free_list.next, struct page, lru);
  574. list_del(&page->lru);
  575. rmv_page_order(page);
  576. area->nr_free--;
  577. __mod_zone_page_state(zone, NR_FREE_PAGES, - (1UL << order));
  578. expand(zone, page, order, current_order, area);
  579. return page;
  580. }
  581. return NULL;
  582. }
  583. /*
  584. * Obtain a specified number of elements from the buddy allocator, all under
  585. * a single hold of the lock, for efficiency. Add them to the supplied list.
  586. * Returns the number of new pages which were placed at *list.
  587. */
  588. static int rmqueue_bulk(struct zone *zone, unsigned int order,
  589. unsigned long count, struct list_head *list)
  590. {
  591. int i;
  592. spin_lock(&zone->lock);
  593. for (i = 0; i < count; ++i) {
  594. struct page *page = __rmqueue(zone, order);
  595. if (unlikely(page == NULL))
  596. break;
  597. list_add_tail(&page->lru, list);
  598. }
  599. spin_unlock(&zone->lock);
  600. return i;
  601. }
  602. #ifdef CONFIG_NUMA
  603. /*
  604. * Called from the vmstat counter updater to drain pagesets of this
  605. * currently executing processor on remote nodes after they have
  606. * expired.
  607. *
  608. * Note that this function must be called with the thread pinned to
  609. * a single processor.
  610. */
  611. void drain_zone_pages(struct zone *zone, struct per_cpu_pages *pcp)
  612. {
  613. unsigned long flags;
  614. int to_drain;
  615. local_irq_save(flags);
  616. if (pcp->count >= pcp->batch)
  617. to_drain = pcp->batch;
  618. else
  619. to_drain = pcp->count;
  620. free_pages_bulk(zone, to_drain, &pcp->list, 0);
  621. pcp->count -= to_drain;
  622. local_irq_restore(flags);
  623. }
  624. #endif
  625. static void __drain_pages(unsigned int cpu)
  626. {
  627. unsigned long flags;
  628. struct zone *zone;
  629. int i;
  630. for_each_zone(zone) {
  631. struct per_cpu_pageset *pset;
  632. if (!populated_zone(zone))
  633. continue;
  634. pset = zone_pcp(zone, cpu);
  635. for (i = 0; i < ARRAY_SIZE(pset->pcp); i++) {
  636. struct per_cpu_pages *pcp;
  637. pcp = &pset->pcp[i];
  638. local_irq_save(flags);
  639. free_pages_bulk(zone, pcp->count, &pcp->list, 0);
  640. pcp->count = 0;
  641. local_irq_restore(flags);
  642. }
  643. }
  644. }
  645. #ifdef CONFIG_HIBERNATION
  646. void mark_free_pages(struct zone *zone)
  647. {
  648. unsigned long pfn, max_zone_pfn;
  649. unsigned long flags;
  650. int order;
  651. struct list_head *curr;
  652. if (!zone->spanned_pages)
  653. return;
  654. spin_lock_irqsave(&zone->lock, flags);
  655. max_zone_pfn = zone->zone_start_pfn + zone->spanned_pages;
  656. for (pfn = zone->zone_start_pfn; pfn < max_zone_pfn; pfn++)
  657. if (pfn_valid(pfn)) {
  658. struct page *page = pfn_to_page(pfn);
  659. if (!swsusp_page_is_forbidden(page))
  660. swsusp_unset_page_free(page);
  661. }
  662. for (order = MAX_ORDER - 1; order >= 0; --order)
  663. list_for_each(curr, &zone->free_area[order].free_list) {
  664. unsigned long i;
  665. pfn = page_to_pfn(list_entry(curr, struct page, lru));
  666. for (i = 0; i < (1UL << order); i++)
  667. swsusp_set_page_free(pfn_to_page(pfn + i));
  668. }
  669. spin_unlock_irqrestore(&zone->lock, flags);
  670. }
  671. /*
  672. * Spill all of this CPU's per-cpu pages back into the buddy allocator.
  673. */
  674. void drain_local_pages(void)
  675. {
  676. unsigned long flags;
  677. local_irq_save(flags);
  678. __drain_pages(smp_processor_id());
  679. local_irq_restore(flags);
  680. }
  681. #endif /* CONFIG_HIBERNATION */
  682. /*
  683. * Free a 0-order page
  684. */
  685. static void fastcall free_hot_cold_page(struct page *page, int cold)
  686. {
  687. struct zone *zone = page_zone(page);
  688. struct per_cpu_pages *pcp;
  689. unsigned long flags;
  690. if (PageAnon(page))
  691. page->mapping = NULL;
  692. if (free_pages_check(page))
  693. return;
  694. if (!PageHighMem(page))
  695. debug_check_no_locks_freed(page_address(page), PAGE_SIZE);
  696. arch_free_page(page, 0);
  697. kernel_map_pages(page, 1, 0);
  698. pcp = &zone_pcp(zone, get_cpu())->pcp[cold];
  699. local_irq_save(flags);
  700. __count_vm_event(PGFREE);
  701. list_add(&page->lru, &pcp->list);
  702. pcp->count++;
  703. if (pcp->count >= pcp->high) {
  704. free_pages_bulk(zone, pcp->batch, &pcp->list, 0);
  705. pcp->count -= pcp->batch;
  706. }
  707. local_irq_restore(flags);
  708. put_cpu();
  709. }
  710. void fastcall free_hot_page(struct page *page)
  711. {
  712. free_hot_cold_page(page, 0);
  713. }
  714. void fastcall free_cold_page(struct page *page)
  715. {
  716. free_hot_cold_page(page, 1);
  717. }
  718. /*
  719. * split_page takes a non-compound higher-order page, and splits it into
  720. * n (1<<order) sub-pages: page[0..n]
  721. * Each sub-page must be freed individually.
  722. *
  723. * Note: this is probably too low level an operation for use in drivers.
  724. * Please consult with lkml before using this in your driver.
  725. */
  726. void split_page(struct page *page, unsigned int order)
  727. {
  728. int i;
  729. VM_BUG_ON(PageCompound(page));
  730. VM_BUG_ON(!page_count(page));
  731. for (i = 1; i < (1 << order); i++)
  732. set_page_refcounted(page + i);
  733. }
  734. /*
  735. * Really, prep_compound_page() should be called from __rmqueue_bulk(). But
  736. * we cheat by calling it from here, in the order > 0 path. Saves a branch
  737. * or two.
  738. */
  739. static struct page *buffered_rmqueue(struct zonelist *zonelist,
  740. struct zone *zone, int order, gfp_t gfp_flags)
  741. {
  742. unsigned long flags;
  743. struct page *page;
  744. int cold = !!(gfp_flags & __GFP_COLD);
  745. int cpu;
  746. again:
  747. cpu = get_cpu();
  748. if (likely(order == 0)) {
  749. struct per_cpu_pages *pcp;
  750. pcp = &zone_pcp(zone, cpu)->pcp[cold];
  751. local_irq_save(flags);
  752. if (!pcp->count) {
  753. pcp->count = rmqueue_bulk(zone, 0,
  754. pcp->batch, &pcp->list);
  755. if (unlikely(!pcp->count))
  756. goto failed;
  757. }
  758. page = list_entry(pcp->list.next, struct page, lru);
  759. list_del(&page->lru);
  760. pcp->count--;
  761. } else {
  762. spin_lock_irqsave(&zone->lock, flags);
  763. page = __rmqueue(zone, order);
  764. spin_unlock(&zone->lock);
  765. if (!page)
  766. goto failed;
  767. }
  768. __count_zone_vm_events(PGALLOC, zone, 1 << order);
  769. zone_statistics(zonelist, zone);
  770. local_irq_restore(flags);
  771. put_cpu();
  772. VM_BUG_ON(bad_range(zone, page));
  773. if (prep_new_page(page, order, gfp_flags))
  774. goto again;
  775. return page;
  776. failed:
  777. local_irq_restore(flags);
  778. put_cpu();
  779. return NULL;
  780. }
  781. #define ALLOC_NO_WATERMARKS 0x01 /* don't check watermarks at all */
  782. #define ALLOC_WMARK_MIN 0x02 /* use pages_min watermark */
  783. #define ALLOC_WMARK_LOW 0x04 /* use pages_low watermark */
  784. #define ALLOC_WMARK_HIGH 0x08 /* use pages_high watermark */
  785. #define ALLOC_HARDER 0x10 /* try to alloc harder */
  786. #define ALLOC_HIGH 0x20 /* __GFP_HIGH set */
  787. #define ALLOC_CPUSET 0x40 /* check for correct cpuset */
  788. #ifdef CONFIG_FAIL_PAGE_ALLOC
  789. static struct fail_page_alloc_attr {
  790. struct fault_attr attr;
  791. u32 ignore_gfp_highmem;
  792. u32 ignore_gfp_wait;
  793. u32 min_order;
  794. #ifdef CONFIG_FAULT_INJECTION_DEBUG_FS
  795. struct dentry *ignore_gfp_highmem_file;
  796. struct dentry *ignore_gfp_wait_file;
  797. struct dentry *min_order_file;
  798. #endif /* CONFIG_FAULT_INJECTION_DEBUG_FS */
  799. } fail_page_alloc = {
  800. .attr = FAULT_ATTR_INITIALIZER,
  801. .ignore_gfp_wait = 1,
  802. .ignore_gfp_highmem = 1,
  803. .min_order = 1,
  804. };
  805. static int __init setup_fail_page_alloc(char *str)
  806. {
  807. return setup_fault_attr(&fail_page_alloc.attr, str);
  808. }
  809. __setup("fail_page_alloc=", setup_fail_page_alloc);
  810. static int should_fail_alloc_page(gfp_t gfp_mask, unsigned int order)
  811. {
  812. if (order < fail_page_alloc.min_order)
  813. return 0;
  814. if (gfp_mask & __GFP_NOFAIL)
  815. return 0;
  816. if (fail_page_alloc.ignore_gfp_highmem && (gfp_mask & __GFP_HIGHMEM))
  817. return 0;
  818. if (fail_page_alloc.ignore_gfp_wait && (gfp_mask & __GFP_WAIT))
  819. return 0;
  820. return should_fail(&fail_page_alloc.attr, 1 << order);
  821. }
  822. #ifdef CONFIG_FAULT_INJECTION_DEBUG_FS
  823. static int __init fail_page_alloc_debugfs(void)
  824. {
  825. mode_t mode = S_IFREG | S_IRUSR | S_IWUSR;
  826. struct dentry *dir;
  827. int err;
  828. err = init_fault_attr_dentries(&fail_page_alloc.attr,
  829. "fail_page_alloc");
  830. if (err)
  831. return err;
  832. dir = fail_page_alloc.attr.dentries.dir;
  833. fail_page_alloc.ignore_gfp_wait_file =
  834. debugfs_create_bool("ignore-gfp-wait", mode, dir,
  835. &fail_page_alloc.ignore_gfp_wait);
  836. fail_page_alloc.ignore_gfp_highmem_file =
  837. debugfs_create_bool("ignore-gfp-highmem", mode, dir,
  838. &fail_page_alloc.ignore_gfp_highmem);
  839. fail_page_alloc.min_order_file =
  840. debugfs_create_u32("min-order", mode, dir,
  841. &fail_page_alloc.min_order);
  842. if (!fail_page_alloc.ignore_gfp_wait_file ||
  843. !fail_page_alloc.ignore_gfp_highmem_file ||
  844. !fail_page_alloc.min_order_file) {
  845. err = -ENOMEM;
  846. debugfs_remove(fail_page_alloc.ignore_gfp_wait_file);
  847. debugfs_remove(fail_page_alloc.ignore_gfp_highmem_file);
  848. debugfs_remove(fail_page_alloc.min_order_file);
  849. cleanup_fault_attr_dentries(&fail_page_alloc.attr);
  850. }
  851. return err;
  852. }
  853. late_initcall(fail_page_alloc_debugfs);
  854. #endif /* CONFIG_FAULT_INJECTION_DEBUG_FS */
  855. #else /* CONFIG_FAIL_PAGE_ALLOC */
  856. static inline int should_fail_alloc_page(gfp_t gfp_mask, unsigned int order)
  857. {
  858. return 0;
  859. }
  860. #endif /* CONFIG_FAIL_PAGE_ALLOC */
  861. /*
  862. * Return 1 if free pages are above 'mark'. This takes into account the order
  863. * of the allocation.
  864. */
  865. int zone_watermark_ok(struct zone *z, int order, unsigned long mark,
  866. int classzone_idx, int alloc_flags)
  867. {
  868. /* free_pages my go negative - that's OK */
  869. long min = mark;
  870. long free_pages = zone_page_state(z, NR_FREE_PAGES) - (1 << order) + 1;
  871. int o;
  872. if (alloc_flags & ALLOC_HIGH)
  873. min -= min / 2;
  874. if (alloc_flags & ALLOC_HARDER)
  875. min -= min / 4;
  876. if (free_pages <= min + z->lowmem_reserve[classzone_idx])
  877. return 0;
  878. for (o = 0; o < order; o++) {
  879. /* At the next order, this order's pages become unavailable */
  880. free_pages -= z->free_area[o].nr_free << o;
  881. /* Require fewer higher order pages to be free */
  882. min >>= 1;
  883. if (free_pages <= min)
  884. return 0;
  885. }
  886. return 1;
  887. }
  888. #ifdef CONFIG_NUMA
  889. /*
  890. * zlc_setup - Setup for "zonelist cache". Uses cached zone data to
  891. * skip over zones that are not allowed by the cpuset, or that have
  892. * been recently (in last second) found to be nearly full. See further
  893. * comments in mmzone.h. Reduces cache footprint of zonelist scans
  894. * that have to skip over alot of full or unallowed zones.
  895. *
  896. * If the zonelist cache is present in the passed in zonelist, then
  897. * returns a pointer to the allowed node mask (either the current
  898. * tasks mems_allowed, or node_online_map.)
  899. *
  900. * If the zonelist cache is not available for this zonelist, does
  901. * nothing and returns NULL.
  902. *
  903. * If the fullzones BITMAP in the zonelist cache is stale (more than
  904. * a second since last zap'd) then we zap it out (clear its bits.)
  905. *
  906. * We hold off even calling zlc_setup, until after we've checked the
  907. * first zone in the zonelist, on the theory that most allocations will
  908. * be satisfied from that first zone, so best to examine that zone as
  909. * quickly as we can.
  910. */
  911. static nodemask_t *zlc_setup(struct zonelist *zonelist, int alloc_flags)
  912. {
  913. struct zonelist_cache *zlc; /* cached zonelist speedup info */
  914. nodemask_t *allowednodes; /* zonelist_cache approximation */
  915. zlc = zonelist->zlcache_ptr;
  916. if (!zlc)
  917. return NULL;
  918. if (jiffies - zlc->last_full_zap > 1 * HZ) {
  919. bitmap_zero(zlc->fullzones, MAX_ZONES_PER_ZONELIST);
  920. zlc->last_full_zap = jiffies;
  921. }
  922. allowednodes = !in_interrupt() && (alloc_flags & ALLOC_CPUSET) ?
  923. &cpuset_current_mems_allowed :
  924. &node_online_map;
  925. return allowednodes;
  926. }
  927. /*
  928. * Given 'z' scanning a zonelist, run a couple of quick checks to see
  929. * if it is worth looking at further for free memory:
  930. * 1) Check that the zone isn't thought to be full (doesn't have its
  931. * bit set in the zonelist_cache fullzones BITMAP).
  932. * 2) Check that the zones node (obtained from the zonelist_cache
  933. * z_to_n[] mapping) is allowed in the passed in allowednodes mask.
  934. * Return true (non-zero) if zone is worth looking at further, or
  935. * else return false (zero) if it is not.
  936. *
  937. * This check -ignores- the distinction between various watermarks,
  938. * such as GFP_HIGH, GFP_ATOMIC, PF_MEMALLOC, ... If a zone is
  939. * found to be full for any variation of these watermarks, it will
  940. * be considered full for up to one second by all requests, unless
  941. * we are so low on memory on all allowed nodes that we are forced
  942. * into the second scan of the zonelist.
  943. *
  944. * In the second scan we ignore this zonelist cache and exactly
  945. * apply the watermarks to all zones, even it is slower to do so.
  946. * We are low on memory in the second scan, and should leave no stone
  947. * unturned looking for a free page.
  948. */
  949. static int zlc_zone_worth_trying(struct zonelist *zonelist, struct zone **z,
  950. nodemask_t *allowednodes)
  951. {
  952. struct zonelist_cache *zlc; /* cached zonelist speedup info */
  953. int i; /* index of *z in zonelist zones */
  954. int n; /* node that zone *z is on */
  955. zlc = zonelist->zlcache_ptr;
  956. if (!zlc)
  957. return 1;
  958. i = z - zonelist->zones;
  959. n = zlc->z_to_n[i];
  960. /* This zone is worth trying if it is allowed but not full */
  961. return node_isset(n, *allowednodes) && !test_bit(i, zlc->fullzones);
  962. }
  963. /*
  964. * Given 'z' scanning a zonelist, set the corresponding bit in
  965. * zlc->fullzones, so that subsequent attempts to allocate a page
  966. * from that zone don't waste time re-examining it.
  967. */
  968. static void zlc_mark_zone_full(struct zonelist *zonelist, struct zone **z)
  969. {
  970. struct zonelist_cache *zlc; /* cached zonelist speedup info */
  971. int i; /* index of *z in zonelist zones */
  972. zlc = zonelist->zlcache_ptr;
  973. if (!zlc)
  974. return;
  975. i = z - zonelist->zones;
  976. set_bit(i, zlc->fullzones);
  977. }
  978. #else /* CONFIG_NUMA */
  979. static nodemask_t *zlc_setup(struct zonelist *zonelist, int alloc_flags)
  980. {
  981. return NULL;
  982. }
  983. static int zlc_zone_worth_trying(struct zonelist *zonelist, struct zone **z,
  984. nodemask_t *allowednodes)
  985. {
  986. return 1;
  987. }
  988. static void zlc_mark_zone_full(struct zonelist *zonelist, struct zone **z)
  989. {
  990. }
  991. #endif /* CONFIG_NUMA */
  992. /*
  993. * get_page_from_freelist goes through the zonelist trying to allocate
  994. * a page.
  995. */
  996. static struct page *
  997. get_page_from_freelist(gfp_t gfp_mask, unsigned int order,
  998. struct zonelist *zonelist, int alloc_flags)
  999. {
  1000. struct zone **z;
  1001. struct page *page = NULL;
  1002. int classzone_idx = zone_idx(zonelist->zones[0]);
  1003. struct zone *zone;
  1004. nodemask_t *allowednodes = NULL;/* zonelist_cache approximation */
  1005. int zlc_active = 0; /* set if using zonelist_cache */
  1006. int did_zlc_setup = 0; /* just call zlc_setup() one time */
  1007. enum zone_type highest_zoneidx = -1; /* Gets set for policy zonelists */
  1008. zonelist_scan:
  1009. /*
  1010. * Scan zonelist, looking for a zone with enough free.
  1011. * See also cpuset_zone_allowed() comment in kernel/cpuset.c.
  1012. */
  1013. z = zonelist->zones;
  1014. do {
  1015. /*
  1016. * In NUMA, this could be a policy zonelist which contains
  1017. * zones that may not be allowed by the current gfp_mask.
  1018. * Check the zone is allowed by the current flags
  1019. */
  1020. if (unlikely(alloc_should_filter_zonelist(zonelist))) {
  1021. if (highest_zoneidx == -1)
  1022. highest_zoneidx = gfp_zone(gfp_mask);
  1023. if (zone_idx(*z) > highest_zoneidx)
  1024. continue;
  1025. }
  1026. if (NUMA_BUILD && zlc_active &&
  1027. !zlc_zone_worth_trying(zonelist, z, allowednodes))
  1028. continue;
  1029. zone = *z;
  1030. if (unlikely(NUMA_BUILD && (gfp_mask & __GFP_THISNODE) &&
  1031. zone->zone_pgdat != zonelist->zones[0]->zone_pgdat))
  1032. break;
  1033. if ((alloc_flags & ALLOC_CPUSET) &&
  1034. !cpuset_zone_allowed_softwall(zone, gfp_mask))
  1035. goto try_next_zone;
  1036. if (!(alloc_flags & ALLOC_NO_WATERMARKS)) {
  1037. unsigned long mark;
  1038. if (alloc_flags & ALLOC_WMARK_MIN)
  1039. mark = zone->pages_min;
  1040. else if (alloc_flags & ALLOC_WMARK_LOW)
  1041. mark = zone->pages_low;
  1042. else
  1043. mark = zone->pages_high;
  1044. if (!zone_watermark_ok(zone, order, mark,
  1045. classzone_idx, alloc_flags)) {
  1046. if (!zone_reclaim_mode ||
  1047. !zone_reclaim(zone, gfp_mask, order))
  1048. goto this_zone_full;
  1049. }
  1050. }
  1051. page = buffered_rmqueue(zonelist, zone, order, gfp_mask);
  1052. if (page)
  1053. break;
  1054. this_zone_full:
  1055. if (NUMA_BUILD)
  1056. zlc_mark_zone_full(zonelist, z);
  1057. try_next_zone:
  1058. if (NUMA_BUILD && !did_zlc_setup) {
  1059. /* we do zlc_setup after the first zone is tried */
  1060. allowednodes = zlc_setup(zonelist, alloc_flags);
  1061. zlc_active = 1;
  1062. did_zlc_setup = 1;
  1063. }
  1064. } while (*(++z) != NULL);
  1065. if (unlikely(NUMA_BUILD && page == NULL && zlc_active)) {
  1066. /* Disable zlc cache for second zonelist scan */
  1067. zlc_active = 0;
  1068. goto zonelist_scan;
  1069. }
  1070. return page;
  1071. }
  1072. /*
  1073. * This is the 'heart' of the zoned buddy allocator.
  1074. */
  1075. struct page * fastcall
  1076. __alloc_pages(gfp_t gfp_mask, unsigned int order,
  1077. struct zonelist *zonelist)
  1078. {
  1079. const gfp_t wait = gfp_mask & __GFP_WAIT;
  1080. struct zone **z;
  1081. struct page *page;
  1082. struct reclaim_state reclaim_state;
  1083. struct task_struct *p = current;
  1084. int do_retry;
  1085. int alloc_flags;
  1086. int did_some_progress;
  1087. might_sleep_if(wait);
  1088. if (should_fail_alloc_page(gfp_mask, order))
  1089. return NULL;
  1090. restart:
  1091. z = zonelist->zones; /* the list of zones suitable for gfp_mask */
  1092. if (unlikely(*z == NULL)) {
  1093. /* Should this ever happen?? */
  1094. return NULL;
  1095. }
  1096. page = get_page_from_freelist(gfp_mask|__GFP_HARDWALL, order,
  1097. zonelist, ALLOC_WMARK_LOW|ALLOC_CPUSET);
  1098. if (page)
  1099. goto got_pg;
  1100. /*
  1101. * GFP_THISNODE (meaning __GFP_THISNODE, __GFP_NORETRY and
  1102. * __GFP_NOWARN set) should not cause reclaim since the subsystem
  1103. * (f.e. slab) using GFP_THISNODE may choose to trigger reclaim
  1104. * using a larger set of nodes after it has established that the
  1105. * allowed per node queues are empty and that nodes are
  1106. * over allocated.
  1107. */
  1108. if (NUMA_BUILD && (gfp_mask & GFP_THISNODE) == GFP_THISNODE)
  1109. goto nopage;
  1110. for (z = zonelist->zones; *z; z++)
  1111. wakeup_kswapd(*z, order);
  1112. /*
  1113. * OK, we're below the kswapd watermark and have kicked background
  1114. * reclaim. Now things get more complex, so set up alloc_flags according
  1115. * to how we want to proceed.
  1116. *
  1117. * The caller may dip into page reserves a bit more if the caller
  1118. * cannot run direct reclaim, or if the caller has realtime scheduling
  1119. * policy or is asking for __GFP_HIGH memory. GFP_ATOMIC requests will
  1120. * set both ALLOC_HARDER (!wait) and ALLOC_HIGH (__GFP_HIGH).
  1121. */
  1122. alloc_flags = ALLOC_WMARK_MIN;
  1123. if ((unlikely(rt_task(p)) && !in_interrupt()) || !wait)
  1124. alloc_flags |= ALLOC_HARDER;
  1125. if (gfp_mask & __GFP_HIGH)
  1126. alloc_flags |= ALLOC_HIGH;
  1127. if (wait)
  1128. alloc_flags |= ALLOC_CPUSET;
  1129. /*
  1130. * Go through the zonelist again. Let __GFP_HIGH and allocations
  1131. * coming from realtime tasks go deeper into reserves.
  1132. *
  1133. * This is the last chance, in general, before the goto nopage.
  1134. * Ignore cpuset if GFP_ATOMIC (!wait) rather than fail alloc.
  1135. * See also cpuset_zone_allowed() comment in kernel/cpuset.c.
  1136. */
  1137. page = get_page_from_freelist(gfp_mask, order, zonelist, alloc_flags);
  1138. if (page)
  1139. goto got_pg;
  1140. /* This allocation should allow future memory freeing. */
  1141. rebalance:
  1142. if (((p->flags & PF_MEMALLOC) || unlikely(test_thread_flag(TIF_MEMDIE)))
  1143. && !in_interrupt()) {
  1144. if (!(gfp_mask & __GFP_NOMEMALLOC)) {
  1145. nofail_alloc:
  1146. /* go through the zonelist yet again, ignoring mins */
  1147. page = get_page_from_freelist(gfp_mask, order,
  1148. zonelist, ALLOC_NO_WATERMARKS);
  1149. if (page)
  1150. goto got_pg;
  1151. if (gfp_mask & __GFP_NOFAIL) {
  1152. congestion_wait(WRITE, HZ/50);
  1153. goto nofail_alloc;
  1154. }
  1155. }
  1156. goto nopage;
  1157. }
  1158. /* Atomic allocations - we can't balance anything */
  1159. if (!wait)
  1160. goto nopage;
  1161. cond_resched();
  1162. /* We now go into synchronous reclaim */
  1163. cpuset_memory_pressure_bump();
  1164. p->flags |= PF_MEMALLOC;
  1165. reclaim_state.reclaimed_slab = 0;
  1166. p->reclaim_state = &reclaim_state;
  1167. did_some_progress = try_to_free_pages(zonelist->zones, order, gfp_mask);
  1168. p->reclaim_state = NULL;
  1169. p->flags &= ~PF_MEMALLOC;
  1170. cond_resched();
  1171. if (likely(did_some_progress)) {
  1172. page = get_page_from_freelist(gfp_mask, order,
  1173. zonelist, alloc_flags);
  1174. if (page)
  1175. goto got_pg;
  1176. } else if ((gfp_mask & __GFP_FS) && !(gfp_mask & __GFP_NORETRY)) {
  1177. /*
  1178. * Go through the zonelist yet one more time, keep
  1179. * very high watermark here, this is only to catch
  1180. * a parallel oom killing, we must fail if we're still
  1181. * under heavy pressure.
  1182. */
  1183. page = get_page_from_freelist(gfp_mask|__GFP_HARDWALL, order,
  1184. zonelist, ALLOC_WMARK_HIGH|ALLOC_CPUSET);
  1185. if (page)
  1186. goto got_pg;
  1187. /* The OOM killer will not help higher order allocs so fail */
  1188. if (order > PAGE_ALLOC_COSTLY_ORDER)
  1189. goto nopage;
  1190. out_of_memory(zonelist, gfp_mask, order);
  1191. goto restart;
  1192. }
  1193. /*
  1194. * Don't let big-order allocations loop unless the caller explicitly
  1195. * requests that. Wait for some write requests to complete then retry.
  1196. *
  1197. * In this implementation, __GFP_REPEAT means __GFP_NOFAIL for order
  1198. * <= 3, but that may not be true in other implementations.
  1199. */
  1200. do_retry = 0;
  1201. if (!(gfp_mask & __GFP_NORETRY)) {
  1202. if ((order <= PAGE_ALLOC_COSTLY_ORDER) ||
  1203. (gfp_mask & __GFP_REPEAT))
  1204. do_retry = 1;
  1205. if (gfp_mask & __GFP_NOFAIL)
  1206. do_retry = 1;
  1207. }
  1208. if (do_retry) {
  1209. congestion_wait(WRITE, HZ/50);
  1210. goto rebalance;
  1211. }
  1212. nopage:
  1213. if (!(gfp_mask & __GFP_NOWARN) && printk_ratelimit()) {
  1214. printk(KERN_WARNING "%s: page allocation failure."
  1215. " order:%d, mode:0x%x\n",
  1216. p->comm, order, gfp_mask);
  1217. dump_stack();
  1218. show_mem();
  1219. }
  1220. got_pg:
  1221. return page;
  1222. }
  1223. EXPORT_SYMBOL(__alloc_pages);
  1224. /*
  1225. * Common helper functions.
  1226. */
  1227. fastcall unsigned long __get_free_pages(gfp_t gfp_mask, unsigned int order)
  1228. {
  1229. struct page * page;
  1230. page = alloc_pages(gfp_mask, order);
  1231. if (!page)
  1232. return 0;
  1233. return (unsigned long) page_address(page);
  1234. }
  1235. EXPORT_SYMBOL(__get_free_pages);
  1236. fastcall unsigned long get_zeroed_page(gfp_t gfp_mask)
  1237. {
  1238. struct page * page;
  1239. /*
  1240. * get_zeroed_page() returns a 32-bit address, which cannot represent
  1241. * a highmem page
  1242. */
  1243. VM_BUG_ON((gfp_mask & __GFP_HIGHMEM) != 0);
  1244. page = alloc_pages(gfp_mask | __GFP_ZERO, 0);
  1245. if (page)
  1246. return (unsigned long) page_address(page);
  1247. return 0;
  1248. }
  1249. EXPORT_SYMBOL(get_zeroed_page);
  1250. void __pagevec_free(struct pagevec *pvec)
  1251. {
  1252. int i = pagevec_count(pvec);
  1253. while (--i >= 0)
  1254. free_hot_cold_page(pvec->pages[i], pvec->cold);
  1255. }
  1256. fastcall void __free_pages(struct page *page, unsigned int order)
  1257. {
  1258. if (put_page_testzero(page)) {
  1259. if (order == 0)
  1260. free_hot_page(page);
  1261. else
  1262. __free_pages_ok(page, order);
  1263. }
  1264. }
  1265. EXPORT_SYMBOL(__free_pages);
  1266. fastcall void free_pages(unsigned long addr, unsigned int order)
  1267. {
  1268. if (addr != 0) {
  1269. VM_BUG_ON(!virt_addr_valid((void *)addr));
  1270. __free_pages(virt_to_page((void *)addr), order);
  1271. }
  1272. }
  1273. EXPORT_SYMBOL(free_pages);
  1274. static unsigned int nr_free_zone_pages(int offset)
  1275. {
  1276. /* Just pick one node, since fallback list is circular */
  1277. pg_data_t *pgdat = NODE_DATA(numa_node_id());
  1278. unsigned int sum = 0;
  1279. struct zonelist *zonelist = pgdat->node_zonelists + offset;
  1280. struct zone **zonep = zonelist->zones;
  1281. struct zone *zone;
  1282. for (zone = *zonep++; zone; zone = *zonep++) {
  1283. unsigned long size = zone->present_pages;
  1284. unsigned long high = zone->pages_high;
  1285. if (size > high)
  1286. sum += size - high;
  1287. }
  1288. return sum;
  1289. }
  1290. /*
  1291. * Amount of free RAM allocatable within ZONE_DMA and ZONE_NORMAL
  1292. */
  1293. unsigned int nr_free_buffer_pages(void)
  1294. {
  1295. return nr_free_zone_pages(gfp_zone(GFP_USER));
  1296. }
  1297. EXPORT_SYMBOL_GPL(nr_free_buffer_pages);
  1298. /*
  1299. * Amount of free RAM allocatable within all zones
  1300. */
  1301. unsigned int nr_free_pagecache_pages(void)
  1302. {
  1303. return nr_free_zone_pages(gfp_zone(GFP_HIGHUSER_MOVABLE));
  1304. }
  1305. static inline void show_node(struct zone *zone)
  1306. {
  1307. if (NUMA_BUILD)
  1308. printk("Node %d ", zone_to_nid(zone));
  1309. }
  1310. void si_meminfo(struct sysinfo *val)
  1311. {
  1312. val->totalram = totalram_pages;
  1313. val->sharedram = 0;
  1314. val->freeram = global_page_state(NR_FREE_PAGES);
  1315. val->bufferram = nr_blockdev_pages();
  1316. val->totalhigh = totalhigh_pages;
  1317. val->freehigh = nr_free_highpages();
  1318. val->mem_unit = PAGE_SIZE;
  1319. }
  1320. EXPORT_SYMBOL(si_meminfo);
  1321. #ifdef CONFIG_NUMA
  1322. void si_meminfo_node(struct sysinfo *val, int nid)
  1323. {
  1324. pg_data_t *pgdat = NODE_DATA(nid);
  1325. val->totalram = pgdat->node_present_pages;
  1326. val->freeram = node_page_state(nid, NR_FREE_PAGES);
  1327. #ifdef CONFIG_HIGHMEM
  1328. val->totalhigh = pgdat->node_zones[ZONE_HIGHMEM].present_pages;
  1329. val->freehigh = zone_page_state(&pgdat->node_zones[ZONE_HIGHMEM],
  1330. NR_FREE_PAGES);
  1331. #else
  1332. val->totalhigh = 0;
  1333. val->freehigh = 0;
  1334. #endif
  1335. val->mem_unit = PAGE_SIZE;
  1336. }
  1337. #endif
  1338. #define K(x) ((x) << (PAGE_SHIFT-10))
  1339. /*
  1340. * Show free area list (used inside shift_scroll-lock stuff)
  1341. * We also calculate the percentage fragmentation. We do this by counting the
  1342. * memory on each free list with the exception of the first item on the list.
  1343. */
  1344. void show_free_areas(void)
  1345. {
  1346. int cpu;
  1347. struct zone *zone;
  1348. for_each_zone(zone) {
  1349. if (!populated_zone(zone))
  1350. continue;
  1351. show_node(zone);
  1352. printk("%s per-cpu:\n", zone->name);
  1353. for_each_online_cpu(cpu) {
  1354. struct per_cpu_pageset *pageset;
  1355. pageset = zone_pcp(zone, cpu);
  1356. printk("CPU %4d: Hot: hi:%5d, btch:%4d usd:%4d "
  1357. "Cold: hi:%5d, btch:%4d usd:%4d\n",
  1358. cpu, pageset->pcp[0].high,
  1359. pageset->pcp[0].batch, pageset->pcp[0].count,
  1360. pageset->pcp[1].high, pageset->pcp[1].batch,
  1361. pageset->pcp[1].count);
  1362. }
  1363. }
  1364. printk("Active:%lu inactive:%lu dirty:%lu writeback:%lu unstable:%lu\n"
  1365. " free:%lu slab:%lu mapped:%lu pagetables:%lu bounce:%lu\n",
  1366. global_page_state(NR_ACTIVE),
  1367. global_page_state(NR_INACTIVE),
  1368. global_page_state(NR_FILE_DIRTY),
  1369. global_page_state(NR_WRITEBACK),
  1370. global_page_state(NR_UNSTABLE_NFS),
  1371. global_page_state(NR_FREE_PAGES),
  1372. global_page_state(NR_SLAB_RECLAIMABLE) +
  1373. global_page_state(NR_SLAB_UNRECLAIMABLE),
  1374. global_page_state(NR_FILE_MAPPED),
  1375. global_page_state(NR_PAGETABLE),
  1376. global_page_state(NR_BOUNCE));
  1377. for_each_zone(zone) {
  1378. int i;
  1379. if (!populated_zone(zone))
  1380. continue;
  1381. show_node(zone);
  1382. printk("%s"
  1383. " free:%lukB"
  1384. " min:%lukB"
  1385. " low:%lukB"
  1386. " high:%lukB"
  1387. " active:%lukB"
  1388. " inactive:%lukB"
  1389. " present:%lukB"
  1390. " pages_scanned:%lu"
  1391. " all_unreclaimable? %s"
  1392. "\n",
  1393. zone->name,
  1394. K(zone_page_state(zone, NR_FREE_PAGES)),
  1395. K(zone->pages_min),
  1396. K(zone->pages_low),
  1397. K(zone->pages_high),
  1398. K(zone_page_state(zone, NR_ACTIVE)),
  1399. K(zone_page_state(zone, NR_INACTIVE)),
  1400. K(zone->present_pages),
  1401. zone->pages_scanned,
  1402. (zone->all_unreclaimable ? "yes" : "no")
  1403. );
  1404. printk("lowmem_reserve[]:");
  1405. for (i = 0; i < MAX_NR_ZONES; i++)
  1406. printk(" %lu", zone->lowmem_reserve[i]);
  1407. printk("\n");
  1408. }
  1409. for_each_zone(zone) {
  1410. unsigned long nr[MAX_ORDER], flags, order, total = 0;
  1411. if (!populated_zone(zone))
  1412. continue;
  1413. show_node(zone);
  1414. printk("%s: ", zone->name);
  1415. spin_lock_irqsave(&zone->lock, flags);
  1416. for (order = 0; order < MAX_ORDER; order++) {
  1417. nr[order] = zone->free_area[order].nr_free;
  1418. total += nr[order] << order;
  1419. }
  1420. spin_unlock_irqrestore(&zone->lock, flags);
  1421. for (order = 0; order < MAX_ORDER; order++)
  1422. printk("%lu*%lukB ", nr[order], K(1UL) << order);
  1423. printk("= %lukB\n", K(total));
  1424. }
  1425. show_swap_cache_info();
  1426. }
  1427. /*
  1428. * Builds allocation fallback zone lists.
  1429. *
  1430. * Add all populated zones of a node to the zonelist.
  1431. */
  1432. static int build_zonelists_node(pg_data_t *pgdat, struct zonelist *zonelist,
  1433. int nr_zones, enum zone_type zone_type)
  1434. {
  1435. struct zone *zone;
  1436. BUG_ON(zone_type >= MAX_NR_ZONES);
  1437. zone_type++;
  1438. do {
  1439. zone_type--;
  1440. zone = pgdat->node_zones + zone_type;
  1441. if (populated_zone(zone)) {
  1442. zonelist->zones[nr_zones++] = zone;
  1443. check_highest_zone(zone_type);
  1444. }
  1445. } while (zone_type);
  1446. return nr_zones;
  1447. }
  1448. /*
  1449. * zonelist_order:
  1450. * 0 = automatic detection of better ordering.
  1451. * 1 = order by ([node] distance, -zonetype)
  1452. * 2 = order by (-zonetype, [node] distance)
  1453. *
  1454. * If not NUMA, ZONELIST_ORDER_ZONE and ZONELIST_ORDER_NODE will create
  1455. * the same zonelist. So only NUMA can configure this param.
  1456. */
  1457. #define ZONELIST_ORDER_DEFAULT 0
  1458. #define ZONELIST_ORDER_NODE 1
  1459. #define ZONELIST_ORDER_ZONE 2
  1460. /* zonelist order in the kernel.
  1461. * set_zonelist_order() will set this to NODE or ZONE.
  1462. */
  1463. static int current_zonelist_order = ZONELIST_ORDER_DEFAULT;
  1464. static char zonelist_order_name[3][8] = {"Default", "Node", "Zone"};
  1465. #ifdef CONFIG_NUMA
  1466. /* The value user specified ....changed by config */
  1467. static int user_zonelist_order = ZONELIST_ORDER_DEFAULT;
  1468. /* string for sysctl */
  1469. #define NUMA_ZONELIST_ORDER_LEN 16
  1470. char numa_zonelist_order[16] = "default";
  1471. /*
  1472. * interface for configure zonelist ordering.
  1473. * command line option "numa_zonelist_order"
  1474. * = "[dD]efault - default, automatic configuration.
  1475. * = "[nN]ode - order by node locality, then by zone within node
  1476. * = "[zZ]one - order by zone, then by locality within zone
  1477. */
  1478. static int __parse_numa_zonelist_order(char *s)
  1479. {
  1480. if (*s == 'd' || *s == 'D') {
  1481. user_zonelist_order = ZONELIST_ORDER_DEFAULT;
  1482. } else if (*s == 'n' || *s == 'N') {
  1483. user_zonelist_order = ZONELIST_ORDER_NODE;
  1484. } else if (*s == 'z' || *s == 'Z') {
  1485. user_zonelist_order = ZONELIST_ORDER_ZONE;
  1486. } else {
  1487. printk(KERN_WARNING
  1488. "Ignoring invalid numa_zonelist_order value: "
  1489. "%s\n", s);
  1490. return -EINVAL;
  1491. }
  1492. return 0;
  1493. }
  1494. static __init int setup_numa_zonelist_order(char *s)
  1495. {
  1496. if (s)
  1497. return __parse_numa_zonelist_order(s);
  1498. return 0;
  1499. }
  1500. early_param("numa_zonelist_order", setup_numa_zonelist_order);
  1501. /*
  1502. * sysctl handler for numa_zonelist_order
  1503. */
  1504. int numa_zonelist_order_handler(ctl_table *table, int write,
  1505. struct file *file, void __user *buffer, size_t *length,
  1506. loff_t *ppos)
  1507. {
  1508. char saved_string[NUMA_ZONELIST_ORDER_LEN];
  1509. int ret;
  1510. if (write)
  1511. strncpy(saved_string, (char*)table->data,
  1512. NUMA_ZONELIST_ORDER_LEN);
  1513. ret = proc_dostring(table, write, file, buffer, length, ppos);
  1514. if (ret)
  1515. return ret;
  1516. if (write) {
  1517. int oldval = user_zonelist_order;
  1518. if (__parse_numa_zonelist_order((char*)table->data)) {
  1519. /*
  1520. * bogus value. restore saved string
  1521. */
  1522. strncpy((char*)table->data, saved_string,
  1523. NUMA_ZONELIST_ORDER_LEN);
  1524. user_zonelist_order = oldval;
  1525. } else if (oldval != user_zonelist_order)
  1526. build_all_zonelists();
  1527. }
  1528. return 0;
  1529. }
  1530. #define MAX_NODE_LOAD (num_online_nodes())
  1531. static int node_load[MAX_NUMNODES];
  1532. /**
  1533. * find_next_best_node - find the next node that should appear in a given node's fallback list
  1534. * @node: node whose fallback list we're appending
  1535. * @used_node_mask: nodemask_t of already used nodes
  1536. *
  1537. * We use a number of factors to determine which is the next node that should
  1538. * appear on a given node's fallback list. The node should not have appeared
  1539. * already in @node's fallback list, and it should be the next closest node
  1540. * according to the distance array (which contains arbitrary distance values
  1541. * from each node to each node in the system), and should also prefer nodes
  1542. * with no CPUs, since presumably they'll have very little allocation pressure
  1543. * on them otherwise.
  1544. * It returns -1 if no node is found.
  1545. */
  1546. static int find_next_best_node(int node, nodemask_t *used_node_mask)
  1547. {
  1548. int n, val;
  1549. int min_val = INT_MAX;
  1550. int best_node = -1;
  1551. /* Use the local node if we haven't already */
  1552. if (!node_isset(node, *used_node_mask)) {
  1553. node_set(node, *used_node_mask);
  1554. return node;
  1555. }
  1556. for_each_online_node(n) {
  1557. cpumask_t tmp;
  1558. /* Don't want a node to appear more than once */
  1559. if (node_isset(n, *used_node_mask))
  1560. continue;
  1561. /* Use the distance array to find the distance */
  1562. val = node_distance(node, n);
  1563. /* Penalize nodes under us ("prefer the next node") */
  1564. val += (n < node);
  1565. /* Give preference to headless and unused nodes */
  1566. tmp = node_to_cpumask(n);
  1567. if (!cpus_empty(tmp))
  1568. val += PENALTY_FOR_NODE_WITH_CPUS;
  1569. /* Slight preference for less loaded node */
  1570. val *= (MAX_NODE_LOAD*MAX_NUMNODES);
  1571. val += node_load[n];
  1572. if (val < min_val) {
  1573. min_val = val;
  1574. best_node = n;
  1575. }
  1576. }
  1577. if (best_node >= 0)
  1578. node_set(best_node, *used_node_mask);
  1579. return best_node;
  1580. }
  1581. /*
  1582. * Build zonelists ordered by node and zones within node.
  1583. * This results in maximum locality--normal zone overflows into local
  1584. * DMA zone, if any--but risks exhausting DMA zone.
  1585. */
  1586. static void build_zonelists_in_node_order(pg_data_t *pgdat, int node)
  1587. {
  1588. enum zone_type i;
  1589. int j;
  1590. struct zonelist *zonelist;
  1591. for (i = 0; i < MAX_NR_ZONES; i++) {
  1592. zonelist = pgdat->node_zonelists + i;
  1593. for (j = 0; zonelist->zones[j] != NULL; j++)
  1594. ;
  1595. j = build_zonelists_node(NODE_DATA(node), zonelist, j, i);
  1596. zonelist->zones[j] = NULL;
  1597. }
  1598. }
  1599. /*
  1600. * Build zonelists ordered by zone and nodes within zones.
  1601. * This results in conserving DMA zone[s] until all Normal memory is
  1602. * exhausted, but results in overflowing to remote node while memory
  1603. * may still exist in local DMA zone.
  1604. */
  1605. static int node_order[MAX_NUMNODES];
  1606. static void build_zonelists_in_zone_order(pg_data_t *pgdat, int nr_nodes)
  1607. {
  1608. enum zone_type i;
  1609. int pos, j, node;
  1610. int zone_type; /* needs to be signed */
  1611. struct zone *z;
  1612. struct zonelist *zonelist;
  1613. for (i = 0; i < MAX_NR_ZONES; i++) {
  1614. zonelist = pgdat->node_zonelists + i;
  1615. pos = 0;
  1616. for (zone_type = i; zone_type >= 0; zone_type--) {
  1617. for (j = 0; j < nr_nodes; j++) {
  1618. node = node_order[j];
  1619. z = &NODE_DATA(node)->node_zones[zone_type];
  1620. if (populated_zone(z)) {
  1621. zonelist->zones[pos++] = z;
  1622. check_highest_zone(zone_type);
  1623. }
  1624. }
  1625. }
  1626. zonelist->zones[pos] = NULL;
  1627. }
  1628. }
  1629. static int default_zonelist_order(void)
  1630. {
  1631. int nid, zone_type;
  1632. unsigned long low_kmem_size,total_size;
  1633. struct zone *z;
  1634. int average_size;
  1635. /*
  1636. * ZONE_DMA and ZONE_DMA32 can be very small area in the sytem.
  1637. * If they are really small and used heavily, the system can fall
  1638. * into OOM very easily.
  1639. * This function detect ZONE_DMA/DMA32 size and confgigures zone order.
  1640. */
  1641. /* Is there ZONE_NORMAL ? (ex. ppc has only DMA zone..) */
  1642. low_kmem_size = 0;
  1643. total_size = 0;
  1644. for_each_online_node(nid) {
  1645. for (zone_type = 0; zone_type < MAX_NR_ZONES; zone_type++) {
  1646. z = &NODE_DATA(nid)->node_zones[zone_type];
  1647. if (populated_zone(z)) {
  1648. if (zone_type < ZONE_NORMAL)
  1649. low_kmem_size += z->present_pages;
  1650. total_size += z->present_pages;
  1651. }
  1652. }
  1653. }
  1654. if (!low_kmem_size || /* there are no DMA area. */
  1655. low_kmem_size > total_size/2) /* DMA/DMA32 is big. */
  1656. return ZONELIST_ORDER_NODE;
  1657. /*
  1658. * look into each node's config.
  1659. * If there is a node whose DMA/DMA32 memory is very big area on
  1660. * local memory, NODE_ORDER may be suitable.
  1661. */
  1662. average_size = total_size / (num_online_nodes() + 1);
  1663. for_each_online_node(nid) {
  1664. low_kmem_size = 0;
  1665. total_size = 0;
  1666. for (zone_type = 0; zone_type < MAX_NR_ZONES; zone_type++) {
  1667. z = &NODE_DATA(nid)->node_zones[zone_type];
  1668. if (populated_zone(z)) {
  1669. if (zone_type < ZONE_NORMAL)
  1670. low_kmem_size += z->present_pages;
  1671. total_size += z->present_pages;
  1672. }
  1673. }
  1674. if (low_kmem_size &&
  1675. total_size > average_size && /* ignore small node */
  1676. low_kmem_size > total_size * 70/100)
  1677. return ZONELIST_ORDER_NODE;
  1678. }
  1679. return ZONELIST_ORDER_ZONE;
  1680. }
  1681. static void set_zonelist_order(void)
  1682. {
  1683. if (user_zonelist_order == ZONELIST_ORDER_DEFAULT)
  1684. current_zonelist_order = default_zonelist_order();
  1685. else
  1686. current_zonelist_order = user_zonelist_order;
  1687. }
  1688. static void build_zonelists(pg_data_t *pgdat)
  1689. {
  1690. int j, node, load;
  1691. enum zone_type i;
  1692. nodemask_t used_mask;
  1693. int local_node, prev_node;
  1694. struct zonelist *zonelist;
  1695. int order = current_zonelist_order;
  1696. /* initialize zonelists */
  1697. for (i = 0; i < MAX_NR_ZONES; i++) {
  1698. zonelist = pgdat->node_zonelists + i;
  1699. zonelist->zones[0] = NULL;
  1700. }
  1701. /* NUMA-aware ordering of nodes */
  1702. local_node = pgdat->node_id;
  1703. load = num_online_nodes();
  1704. prev_node = local_node;
  1705. nodes_clear(used_mask);
  1706. memset(node_load, 0, sizeof(node_load));
  1707. memset(node_order, 0, sizeof(node_order));
  1708. j = 0;
  1709. while ((node = find_next_best_node(local_node, &used_mask)) >= 0) {
  1710. int distance = node_distance(local_node, node);
  1711. /*
  1712. * If another node is sufficiently far away then it is better
  1713. * to reclaim pages in a zone before going off node.
  1714. */
  1715. if (distance > RECLAIM_DISTANCE)
  1716. zone_reclaim_mode = 1;
  1717. /*
  1718. * We don't want to pressure a particular node.
  1719. * So adding penalty to the first node in same
  1720. * distance group to make it round-robin.
  1721. */
  1722. if (distance != node_distance(local_node, prev_node))
  1723. node_load[node] = load;
  1724. prev_node = node;
  1725. load--;
  1726. if (order == ZONELIST_ORDER_NODE)
  1727. build_zonelists_in_node_order(pgdat, node);
  1728. else
  1729. node_order[j++] = node; /* remember order */
  1730. }
  1731. if (order == ZONELIST_ORDER_ZONE) {
  1732. /* calculate node order -- i.e., DMA last! */
  1733. build_zonelists_in_zone_order(pgdat, j);
  1734. }
  1735. }
  1736. /* Construct the zonelist performance cache - see further mmzone.h */
  1737. static void build_zonelist_cache(pg_data_t *pgdat)
  1738. {
  1739. int i;
  1740. for (i = 0; i < MAX_NR_ZONES; i++) {
  1741. struct zonelist *zonelist;
  1742. struct zonelist_cache *zlc;
  1743. struct zone **z;
  1744. zonelist = pgdat->node_zonelists + i;
  1745. zonelist->zlcache_ptr = zlc = &zonelist->zlcache;
  1746. bitmap_zero(zlc->fullzones, MAX_ZONES_PER_ZONELIST);
  1747. for (z = zonelist->zones; *z; z++)
  1748. zlc->z_to_n[z - zonelist->zones] = zone_to_nid(*z);
  1749. }
  1750. }
  1751. #else /* CONFIG_NUMA */
  1752. static void set_zonelist_order(void)
  1753. {
  1754. current_zonelist_order = ZONELIST_ORDER_ZONE;
  1755. }
  1756. static void build_zonelists(pg_data_t *pgdat)
  1757. {
  1758. int node, local_node;
  1759. enum zone_type i,j;
  1760. local_node = pgdat->node_id;
  1761. for (i = 0; i < MAX_NR_ZONES; i++) {
  1762. struct zonelist *zonelist;
  1763. zonelist = pgdat->node_zonelists + i;
  1764. j = build_zonelists_node(pgdat, zonelist, 0, i);
  1765. /*
  1766. * Now we build the zonelist so that it contains the zones
  1767. * of all the other nodes.
  1768. * We don't want to pressure a particular node, so when
  1769. * building the zones for node N, we make sure that the
  1770. * zones coming right after the local ones are those from
  1771. * node N+1 (modulo N)
  1772. */
  1773. for (node = local_node + 1; node < MAX_NUMNODES; node++) {
  1774. if (!node_online(node))
  1775. continue;
  1776. j = build_zonelists_node(NODE_DATA(node), zonelist, j, i);
  1777. }
  1778. for (node = 0; node < local_node; node++) {
  1779. if (!node_online(node))
  1780. continue;
  1781. j = build_zonelists_node(NODE_DATA(node), zonelist, j, i);
  1782. }
  1783. zonelist->zones[j] = NULL;
  1784. }
  1785. }
  1786. /* non-NUMA variant of zonelist performance cache - just NULL zlcache_ptr */
  1787. static void build_zonelist_cache(pg_data_t *pgdat)
  1788. {
  1789. int i;
  1790. for (i = 0; i < MAX_NR_ZONES; i++)
  1791. pgdat->node_zonelists[i].zlcache_ptr = NULL;
  1792. }
  1793. #endif /* CONFIG_NUMA */
  1794. /* return values int ....just for stop_machine_run() */
  1795. static int __build_all_zonelists(void *dummy)
  1796. {
  1797. int nid;
  1798. for_each_online_node(nid) {
  1799. build_zonelists(NODE_DATA(nid));
  1800. build_zonelist_cache(NODE_DATA(nid));
  1801. }
  1802. return 0;
  1803. }
  1804. void build_all_zonelists(void)
  1805. {
  1806. set_zonelist_order();
  1807. if (system_state == SYSTEM_BOOTING) {
  1808. __build_all_zonelists(NULL);
  1809. cpuset_init_current_mems_allowed();
  1810. } else {
  1811. /* we have to stop all cpus to guaranntee there is no user
  1812. of zonelist */
  1813. stop_machine_run(__build_all_zonelists, NULL, NR_CPUS);
  1814. /* cpuset refresh routine should be here */
  1815. }
  1816. vm_total_pages = nr_free_pagecache_pages();
  1817. printk("Built %i zonelists in %s order. Total pages: %ld\n",
  1818. num_online_nodes(),
  1819. zonelist_order_name[current_zonelist_order],
  1820. vm_total_pages);
  1821. #ifdef CONFIG_NUMA
  1822. printk("Policy zone: %s\n", zone_names[policy_zone]);
  1823. #endif
  1824. }
  1825. /*
  1826. * Helper functions to size the waitqueue hash table.
  1827. * Essentially these want to choose hash table sizes sufficiently
  1828. * large so that collisions trying to wait on pages are rare.
  1829. * But in fact, the number of active page waitqueues on typical
  1830. * systems is ridiculously low, less than 200. So this is even
  1831. * conservative, even though it seems large.
  1832. *
  1833. * The constant PAGES_PER_WAITQUEUE specifies the ratio of pages to
  1834. * waitqueues, i.e. the size of the waitq table given the number of pages.
  1835. */
  1836. #define PAGES_PER_WAITQUEUE 256
  1837. #ifndef CONFIG_MEMORY_HOTPLUG
  1838. static inline unsigned long wait_table_hash_nr_entries(unsigned long pages)
  1839. {
  1840. unsigned long size = 1;
  1841. pages /= PAGES_PER_WAITQUEUE;
  1842. while (size < pages)
  1843. size <<= 1;
  1844. /*
  1845. * Once we have dozens or even hundreds of threads sleeping
  1846. * on IO we've got bigger problems than wait queue collision.
  1847. * Limit the size of the wait table to a reasonable size.
  1848. */
  1849. size = min(size, 4096UL);
  1850. return max(size, 4UL);
  1851. }
  1852. #else
  1853. /*
  1854. * A zone's size might be changed by hot-add, so it is not possible to determine
  1855. * a suitable size for its wait_table. So we use the maximum size now.
  1856. *
  1857. * The max wait table size = 4096 x sizeof(wait_queue_head_t). ie:
  1858. *
  1859. * i386 (preemption config) : 4096 x 16 = 64Kbyte.
  1860. * ia64, x86-64 (no preemption): 4096 x 20 = 80Kbyte.
  1861. * ia64, x86-64 (preemption) : 4096 x 24 = 96Kbyte.
  1862. *
  1863. * The maximum entries are prepared when a zone's memory is (512K + 256) pages
  1864. * or more by the traditional way. (See above). It equals:
  1865. *
  1866. * i386, x86-64, powerpc(4K page size) : = ( 2G + 1M)byte.
  1867. * ia64(16K page size) : = ( 8G + 4M)byte.
  1868. * powerpc (64K page size) : = (32G +16M)byte.
  1869. */
  1870. static inline unsigned long wait_table_hash_nr_entries(unsigned long pages)
  1871. {
  1872. return 4096UL;
  1873. }
  1874. #endif
  1875. /*
  1876. * This is an integer logarithm so that shifts can be used later
  1877. * to extract the more random high bits from the multiplicative
  1878. * hash function before the remainder is taken.
  1879. */
  1880. static inline unsigned long wait_table_bits(unsigned long size)
  1881. {
  1882. return ffz(~size);
  1883. }
  1884. #define LONG_ALIGN(x) (((x)+(sizeof(long))-1)&~((sizeof(long))-1))
  1885. /*
  1886. * Initially all pages are reserved - free ones are freed
  1887. * up by free_all_bootmem() once the early boot process is
  1888. * done. Non-atomic initialization, single-pass.
  1889. */
  1890. void __meminit memmap_init_zone(unsigned long size, int nid, unsigned long zone,
  1891. unsigned long start_pfn, enum memmap_context context)
  1892. {
  1893. struct page *page;
  1894. unsigned long end_pfn = start_pfn + size;
  1895. unsigned long pfn;
  1896. for (pfn = start_pfn; pfn < end_pfn; pfn++) {
  1897. /*
  1898. * There can be holes in boot-time mem_map[]s
  1899. * handed to this function. They do not
  1900. * exist on hotplugged memory.
  1901. */
  1902. if (context == MEMMAP_EARLY) {
  1903. if (!early_pfn_valid(pfn))
  1904. continue;
  1905. if (!early_pfn_in_nid(pfn, nid))
  1906. continue;
  1907. }
  1908. page = pfn_to_page(pfn);
  1909. set_page_links(page, zone, nid, pfn);
  1910. init_page_count(page);
  1911. reset_page_mapcount(page);
  1912. SetPageReserved(page);
  1913. INIT_LIST_HEAD(&page->lru);
  1914. #ifdef WANT_PAGE_VIRTUAL
  1915. /* The shift won't overflow because ZONE_NORMAL is below 4G. */
  1916. if (!is_highmem_idx(zone))
  1917. set_page_address(page, __va(pfn << PAGE_SHIFT));
  1918. #endif
  1919. }
  1920. }
  1921. static void __meminit zone_init_free_lists(struct pglist_data *pgdat,
  1922. struct zone *zone, unsigned long size)
  1923. {
  1924. int order;
  1925. for (order = 0; order < MAX_ORDER ; order++) {
  1926. INIT_LIST_HEAD(&zone->free_area[order].free_list);
  1927. zone->free_area[order].nr_free = 0;
  1928. }
  1929. }
  1930. #ifndef __HAVE_ARCH_MEMMAP_INIT
  1931. #define memmap_init(size, nid, zone, start_pfn) \
  1932. memmap_init_zone((size), (nid), (zone), (start_pfn), MEMMAP_EARLY)
  1933. #endif
  1934. static int __devinit zone_batchsize(struct zone *zone)
  1935. {
  1936. int batch;
  1937. /*
  1938. * The per-cpu-pages pools are set to around 1000th of the
  1939. * size of the zone. But no more than 1/2 of a meg.
  1940. *
  1941. * OK, so we don't know how big the cache is. So guess.
  1942. */
  1943. batch = zone->present_pages / 1024;
  1944. if (batch * PAGE_SIZE > 512 * 1024)
  1945. batch = (512 * 1024) / PAGE_SIZE;
  1946. batch /= 4; /* We effectively *= 4 below */
  1947. if (batch < 1)
  1948. batch = 1;
  1949. /*
  1950. * Clamp the batch to a 2^n - 1 value. Having a power
  1951. * of 2 value was found to be more likely to have
  1952. * suboptimal cache aliasing properties in some cases.
  1953. *
  1954. * For example if 2 tasks are alternately allocating
  1955. * batches of pages, one task can end up with a lot
  1956. * of pages of one half of the possible page colors
  1957. * and the other with pages of the other colors.
  1958. */
  1959. batch = (1 << (fls(batch + batch/2)-1)) - 1;
  1960. return batch;
  1961. }
  1962. inline void setup_pageset(struct per_cpu_pageset *p, unsigned long batch)
  1963. {
  1964. struct per_cpu_pages *pcp;
  1965. memset(p, 0, sizeof(*p));
  1966. pcp = &p->pcp[0]; /* hot */
  1967. pcp->count = 0;
  1968. pcp->high = 6 * batch;
  1969. pcp->batch = max(1UL, 1 * batch);
  1970. INIT_LIST_HEAD(&pcp->list);
  1971. pcp = &p->pcp[1]; /* cold*/
  1972. pcp->count = 0;
  1973. pcp->high = 2 * batch;
  1974. pcp->batch = max(1UL, batch/2);
  1975. INIT_LIST_HEAD(&pcp->list);
  1976. }
  1977. /*
  1978. * setup_pagelist_highmark() sets the high water mark for hot per_cpu_pagelist
  1979. * to the value high for the pageset p.
  1980. */
  1981. static void setup_pagelist_highmark(struct per_cpu_pageset *p,
  1982. unsigned long high)
  1983. {
  1984. struct per_cpu_pages *pcp;
  1985. pcp = &p->pcp[0]; /* hot list */
  1986. pcp->high = high;
  1987. pcp->batch = max(1UL, high/4);
  1988. if ((high/4) > (PAGE_SHIFT * 8))
  1989. pcp->batch = PAGE_SHIFT * 8;
  1990. }
  1991. #ifdef CONFIG_NUMA
  1992. /*
  1993. * Boot pageset table. One per cpu which is going to be used for all
  1994. * zones and all nodes. The parameters will be set in such a way
  1995. * that an item put on a list will immediately be handed over to
  1996. * the buddy list. This is safe since pageset manipulation is done
  1997. * with interrupts disabled.
  1998. *
  1999. * Some NUMA counter updates may also be caught by the boot pagesets.
  2000. *
  2001. * The boot_pagesets must be kept even after bootup is complete for
  2002. * unused processors and/or zones. They do play a role for bootstrapping
  2003. * hotplugged processors.
  2004. *
  2005. * zoneinfo_show() and maybe other functions do
  2006. * not check if the processor is online before following the pageset pointer.
  2007. * Other parts of the kernel may not check if the zone is available.
  2008. */
  2009. static struct per_cpu_pageset boot_pageset[NR_CPUS];
  2010. /*
  2011. * Dynamically allocate memory for the
  2012. * per cpu pageset array in struct zone.
  2013. */
  2014. static int __cpuinit process_zones(int cpu)
  2015. {
  2016. struct zone *zone, *dzone;
  2017. for_each_zone(zone) {
  2018. if (!populated_zone(zone))
  2019. continue;
  2020. zone_pcp(zone, cpu) = kmalloc_node(sizeof(struct per_cpu_pageset),
  2021. GFP_KERNEL, cpu_to_node(cpu));
  2022. if (!zone_pcp(zone, cpu))
  2023. goto bad;
  2024. setup_pageset(zone_pcp(zone, cpu), zone_batchsize(zone));
  2025. if (percpu_pagelist_fraction)
  2026. setup_pagelist_highmark(zone_pcp(zone, cpu),
  2027. (zone->present_pages / percpu_pagelist_fraction));
  2028. }
  2029. return 0;
  2030. bad:
  2031. for_each_zone(dzone) {
  2032. if (!populated_zone(dzone))
  2033. continue;
  2034. if (dzone == zone)
  2035. break;
  2036. kfree(zone_pcp(dzone, cpu));
  2037. zone_pcp(dzone, cpu) = NULL;
  2038. }
  2039. return -ENOMEM;
  2040. }
  2041. static inline void free_zone_pagesets(int cpu)
  2042. {
  2043. struct zone *zone;
  2044. for_each_zone(zone) {
  2045. struct per_cpu_pageset *pset = zone_pcp(zone, cpu);
  2046. /* Free per_cpu_pageset if it is slab allocated */
  2047. if (pset != &boot_pageset[cpu])
  2048. kfree(pset);
  2049. zone_pcp(zone, cpu) = NULL;
  2050. }
  2051. }
  2052. static int __cpuinit pageset_cpuup_callback(struct notifier_block *nfb,
  2053. unsigned long action,
  2054. void *hcpu)
  2055. {
  2056. int cpu = (long)hcpu;
  2057. int ret = NOTIFY_OK;
  2058. switch (action) {
  2059. case CPU_UP_PREPARE:
  2060. case CPU_UP_PREPARE_FROZEN:
  2061. if (process_zones(cpu))
  2062. ret = NOTIFY_BAD;
  2063. break;
  2064. case CPU_UP_CANCELED:
  2065. case CPU_UP_CANCELED_FROZEN:
  2066. case CPU_DEAD:
  2067. case CPU_DEAD_FROZEN:
  2068. free_zone_pagesets(cpu);
  2069. break;
  2070. default:
  2071. break;
  2072. }
  2073. return ret;
  2074. }
  2075. static struct notifier_block __cpuinitdata pageset_notifier =
  2076. { &pageset_cpuup_callback, NULL, 0 };
  2077. void __init setup_per_cpu_pageset(void)
  2078. {
  2079. int err;
  2080. /* Initialize per_cpu_pageset for cpu 0.
  2081. * A cpuup callback will do this for every cpu
  2082. * as it comes online
  2083. */
  2084. err = process_zones(smp_processor_id());
  2085. BUG_ON(err);
  2086. register_cpu_notifier(&pageset_notifier);
  2087. }
  2088. #endif
  2089. static noinline __init_refok
  2090. int zone_wait_table_init(struct zone *zone, unsigned long zone_size_pages)
  2091. {
  2092. int i;
  2093. struct pglist_data *pgdat = zone->zone_pgdat;
  2094. size_t alloc_size;
  2095. /*
  2096. * The per-page waitqueue mechanism uses hashed waitqueues
  2097. * per zone.
  2098. */
  2099. zone->wait_table_hash_nr_entries =
  2100. wait_table_hash_nr_entries(zone_size_pages);
  2101. zone->wait_table_bits =
  2102. wait_table_bits(zone->wait_table_hash_nr_entries);
  2103. alloc_size = zone->wait_table_hash_nr_entries
  2104. * sizeof(wait_queue_head_t);
  2105. if (system_state == SYSTEM_BOOTING) {
  2106. zone->wait_table = (wait_queue_head_t *)
  2107. alloc_bootmem_node(pgdat, alloc_size);
  2108. } else {
  2109. /*
  2110. * This case means that a zone whose size was 0 gets new memory
  2111. * via memory hot-add.
  2112. * But it may be the case that a new node was hot-added. In
  2113. * this case vmalloc() will not be able to use this new node's
  2114. * memory - this wait_table must be initialized to use this new
  2115. * node itself as well.
  2116. * To use this new node's memory, further consideration will be
  2117. * necessary.
  2118. */
  2119. zone->wait_table = (wait_queue_head_t *)vmalloc(alloc_size);
  2120. }
  2121. if (!zone->wait_table)
  2122. return -ENOMEM;
  2123. for(i = 0; i < zone->wait_table_hash_nr_entries; ++i)
  2124. init_waitqueue_head(zone->wait_table + i);
  2125. return 0;
  2126. }
  2127. static __meminit void zone_pcp_init(struct zone *zone)
  2128. {
  2129. int cpu;
  2130. unsigned long batch = zone_batchsize(zone);
  2131. for (cpu = 0; cpu < NR_CPUS; cpu++) {
  2132. #ifdef CONFIG_NUMA
  2133. /* Early boot. Slab allocator not functional yet */
  2134. zone_pcp(zone, cpu) = &boot_pageset[cpu];
  2135. setup_pageset(&boot_pageset[cpu],0);
  2136. #else
  2137. setup_pageset(zone_pcp(zone,cpu), batch);
  2138. #endif
  2139. }
  2140. if (zone->present_pages)
  2141. printk(KERN_DEBUG " %s zone: %lu pages, LIFO batch:%lu\n",
  2142. zone->name, zone->present_pages, batch);
  2143. }
  2144. __meminit int init_currently_empty_zone(struct zone *zone,
  2145. unsigned long zone_start_pfn,
  2146. unsigned long size,
  2147. enum memmap_context context)
  2148. {
  2149. struct pglist_data *pgdat = zone->zone_pgdat;
  2150. int ret;
  2151. ret = zone_wait_table_init(zone, size);
  2152. if (ret)
  2153. return ret;
  2154. pgdat->nr_zones = zone_idx(zone) + 1;
  2155. zone->zone_start_pfn = zone_start_pfn;
  2156. memmap_init(size, pgdat->node_id, zone_idx(zone), zone_start_pfn);
  2157. zone_init_free_lists(pgdat, zone, zone->spanned_pages);
  2158. return 0;
  2159. }
  2160. #ifdef CONFIG_ARCH_POPULATES_NODE_MAP
  2161. /*
  2162. * Basic iterator support. Return the first range of PFNs for a node
  2163. * Note: nid == MAX_NUMNODES returns first region regardless of node
  2164. */
  2165. static int __meminit first_active_region_index_in_nid(int nid)
  2166. {
  2167. int i;
  2168. for (i = 0; i < nr_nodemap_entries; i++)
  2169. if (nid == MAX_NUMNODES || early_node_map[i].nid == nid)
  2170. return i;
  2171. return -1;
  2172. }
  2173. /*
  2174. * Basic iterator support. Return the next active range of PFNs for a node
  2175. * Note: nid == MAX_NUMNODES returns next region regardles of node
  2176. */
  2177. static int __meminit next_active_region_index_in_nid(int index, int nid)
  2178. {
  2179. for (index = index + 1; index < nr_nodemap_entries; index++)
  2180. if (nid == MAX_NUMNODES || early_node_map[index].nid == nid)
  2181. return index;
  2182. return -1;
  2183. }
  2184. #ifndef CONFIG_HAVE_ARCH_EARLY_PFN_TO_NID
  2185. /*
  2186. * Required by SPARSEMEM. Given a PFN, return what node the PFN is on.
  2187. * Architectures may implement their own version but if add_active_range()
  2188. * was used and there are no special requirements, this is a convenient
  2189. * alternative
  2190. */
  2191. int __meminit early_pfn_to_nid(unsigned long pfn)
  2192. {
  2193. int i;
  2194. for (i = 0; i < nr_nodemap_entries; i++) {
  2195. unsigned long start_pfn = early_node_map[i].start_pfn;
  2196. unsigned long end_pfn = early_node_map[i].end_pfn;
  2197. if (start_pfn <= pfn && pfn < end_pfn)
  2198. return early_node_map[i].nid;
  2199. }
  2200. return 0;
  2201. }
  2202. #endif /* CONFIG_HAVE_ARCH_EARLY_PFN_TO_NID */
  2203. /* Basic iterator support to walk early_node_map[] */
  2204. #define for_each_active_range_index_in_nid(i, nid) \
  2205. for (i = first_active_region_index_in_nid(nid); i != -1; \
  2206. i = next_active_region_index_in_nid(i, nid))
  2207. /**
  2208. * free_bootmem_with_active_regions - Call free_bootmem_node for each active range
  2209. * @nid: The node to free memory on. If MAX_NUMNODES, all nodes are freed.
  2210. * @max_low_pfn: The highest PFN that will be passed to free_bootmem_node
  2211. *
  2212. * If an architecture guarantees that all ranges registered with
  2213. * add_active_ranges() contain no holes and may be freed, this
  2214. * this function may be used instead of calling free_bootmem() manually.
  2215. */
  2216. void __init free_bootmem_with_active_regions(int nid,
  2217. unsigned long max_low_pfn)
  2218. {
  2219. int i;
  2220. for_each_active_range_index_in_nid(i, nid) {
  2221. unsigned long size_pages = 0;
  2222. unsigned long end_pfn = early_node_map[i].end_pfn;
  2223. if (early_node_map[i].start_pfn >= max_low_pfn)
  2224. continue;
  2225. if (end_pfn > max_low_pfn)
  2226. end_pfn = max_low_pfn;
  2227. size_pages = end_pfn - early_node_map[i].start_pfn;
  2228. free_bootmem_node(NODE_DATA(early_node_map[i].nid),
  2229. PFN_PHYS(early_node_map[i].start_pfn),
  2230. size_pages << PAGE_SHIFT);
  2231. }
  2232. }
  2233. /**
  2234. * sparse_memory_present_with_active_regions - Call memory_present for each active range
  2235. * @nid: The node to call memory_present for. If MAX_NUMNODES, all nodes will be used.
  2236. *
  2237. * If an architecture guarantees that all ranges registered with
  2238. * add_active_ranges() contain no holes and may be freed, this
  2239. * function may be used instead of calling memory_present() manually.
  2240. */
  2241. void __init sparse_memory_present_with_active_regions(int nid)
  2242. {
  2243. int i;
  2244. for_each_active_range_index_in_nid(i, nid)
  2245. memory_present(early_node_map[i].nid,
  2246. early_node_map[i].start_pfn,
  2247. early_node_map[i].end_pfn);
  2248. }
  2249. /**
  2250. * push_node_boundaries - Push node boundaries to at least the requested boundary
  2251. * @nid: The nid of the node to push the boundary for
  2252. * @start_pfn: The start pfn of the node
  2253. * @end_pfn: The end pfn of the node
  2254. *
  2255. * In reserve-based hot-add, mem_map is allocated that is unused until hotadd
  2256. * time. Specifically, on x86_64, SRAT will report ranges that can potentially
  2257. * be hotplugged even though no physical memory exists. This function allows
  2258. * an arch to push out the node boundaries so mem_map is allocated that can
  2259. * be used later.
  2260. */
  2261. #ifdef CONFIG_MEMORY_HOTPLUG_RESERVE
  2262. void __init push_node_boundaries(unsigned int nid,
  2263. unsigned long start_pfn, unsigned long end_pfn)
  2264. {
  2265. printk(KERN_DEBUG "Entering push_node_boundaries(%u, %lu, %lu)\n",
  2266. nid, start_pfn, end_pfn);
  2267. /* Initialise the boundary for this node if necessary */
  2268. if (node_boundary_end_pfn[nid] == 0)
  2269. node_boundary_start_pfn[nid] = -1UL;
  2270. /* Update the boundaries */
  2271. if (node_boundary_start_pfn[nid] > start_pfn)
  2272. node_boundary_start_pfn[nid] = start_pfn;
  2273. if (node_boundary_end_pfn[nid] < end_pfn)
  2274. node_boundary_end_pfn[nid] = end_pfn;
  2275. }
  2276. /* If necessary, push the node boundary out for reserve hotadd */
  2277. static void __meminit account_node_boundary(unsigned int nid,
  2278. unsigned long *start_pfn, unsigned long *end_pfn)
  2279. {
  2280. printk(KERN_DEBUG "Entering account_node_boundary(%u, %lu, %lu)\n",
  2281. nid, *start_pfn, *end_pfn);
  2282. /* Return if boundary information has not been provided */
  2283. if (node_boundary_end_pfn[nid] == 0)
  2284. return;
  2285. /* Check the boundaries and update if necessary */
  2286. if (node_boundary_start_pfn[nid] < *start_pfn)
  2287. *start_pfn = node_boundary_start_pfn[nid];
  2288. if (node_boundary_end_pfn[nid] > *end_pfn)
  2289. *end_pfn = node_boundary_end_pfn[nid];
  2290. }
  2291. #else
  2292. void __init push_node_boundaries(unsigned int nid,
  2293. unsigned long start_pfn, unsigned long end_pfn) {}
  2294. static void __meminit account_node_boundary(unsigned int nid,
  2295. unsigned long *start_pfn, unsigned long *end_pfn) {}
  2296. #endif
  2297. /**
  2298. * get_pfn_range_for_nid - Return the start and end page frames for a node
  2299. * @nid: The nid to return the range for. If MAX_NUMNODES, the min and max PFN are returned.
  2300. * @start_pfn: Passed by reference. On return, it will have the node start_pfn.
  2301. * @end_pfn: Passed by reference. On return, it will have the node end_pfn.
  2302. *
  2303. * It returns the start and end page frame of a node based on information
  2304. * provided by an arch calling add_active_range(). If called for a node
  2305. * with no available memory, a warning is printed and the start and end
  2306. * PFNs will be 0.
  2307. */
  2308. void __meminit get_pfn_range_for_nid(unsigned int nid,
  2309. unsigned long *start_pfn, unsigned long *end_pfn)
  2310. {
  2311. int i;
  2312. *start_pfn = -1UL;
  2313. *end_pfn = 0;
  2314. for_each_active_range_index_in_nid(i, nid) {
  2315. *start_pfn = min(*start_pfn, early_node_map[i].start_pfn);
  2316. *end_pfn = max(*end_pfn, early_node_map[i].end_pfn);
  2317. }
  2318. if (*start_pfn == -1UL) {
  2319. printk(KERN_WARNING "Node %u active with no memory\n", nid);
  2320. *start_pfn = 0;
  2321. }
  2322. /* Push the node boundaries out if requested */
  2323. account_node_boundary(nid, start_pfn, end_pfn);
  2324. }
  2325. /*
  2326. * This finds a zone that can be used for ZONE_MOVABLE pages. The
  2327. * assumption is made that zones within a node are ordered in monotonic
  2328. * increasing memory addresses so that the "highest" populated zone is used
  2329. */
  2330. void __init find_usable_zone_for_movable(void)
  2331. {
  2332. int zone_index;
  2333. for (zone_index = MAX_NR_ZONES - 1; zone_index >= 0; zone_index--) {
  2334. if (zone_index == ZONE_MOVABLE)
  2335. continue;
  2336. if (arch_zone_highest_possible_pfn[zone_index] >
  2337. arch_zone_lowest_possible_pfn[zone_index])
  2338. break;
  2339. }
  2340. VM_BUG_ON(zone_index == -1);
  2341. movable_zone = zone_index;
  2342. }
  2343. /*
  2344. * The zone ranges provided by the architecture do not include ZONE_MOVABLE
  2345. * because it is sized independant of architecture. Unlike the other zones,
  2346. * the starting point for ZONE_MOVABLE is not fixed. It may be different
  2347. * in each node depending on the size of each node and how evenly kernelcore
  2348. * is distributed. This helper function adjusts the zone ranges
  2349. * provided by the architecture for a given node by using the end of the
  2350. * highest usable zone for ZONE_MOVABLE. This preserves the assumption that
  2351. * zones within a node are in order of monotonic increases memory addresses
  2352. */
  2353. void __meminit adjust_zone_range_for_zone_movable(int nid,
  2354. unsigned long zone_type,
  2355. unsigned long node_start_pfn,
  2356. unsigned long node_end_pfn,
  2357. unsigned long *zone_start_pfn,
  2358. unsigned long *zone_end_pfn)
  2359. {
  2360. /* Only adjust if ZONE_MOVABLE is on this node */
  2361. if (zone_movable_pfn[nid]) {
  2362. /* Size ZONE_MOVABLE */
  2363. if (zone_type == ZONE_MOVABLE) {
  2364. *zone_start_pfn = zone_movable_pfn[nid];
  2365. *zone_end_pfn = min(node_end_pfn,
  2366. arch_zone_highest_possible_pfn[movable_zone]);
  2367. /* Adjust for ZONE_MOVABLE starting within this range */
  2368. } else if (*zone_start_pfn < zone_movable_pfn[nid] &&
  2369. *zone_end_pfn > zone_movable_pfn[nid]) {
  2370. *zone_end_pfn = zone_movable_pfn[nid];
  2371. /* Check if this whole range is within ZONE_MOVABLE */
  2372. } else if (*zone_start_pfn >= zone_movable_pfn[nid])
  2373. *zone_start_pfn = *zone_end_pfn;
  2374. }
  2375. }
  2376. /*
  2377. * Return the number of pages a zone spans in a node, including holes
  2378. * present_pages = zone_spanned_pages_in_node() - zone_absent_pages_in_node()
  2379. */
  2380. static unsigned long __meminit zone_spanned_pages_in_node(int nid,
  2381. unsigned long zone_type,
  2382. unsigned long *ignored)
  2383. {
  2384. unsigned long node_start_pfn, node_end_pfn;
  2385. unsigned long zone_start_pfn, zone_end_pfn;
  2386. /* Get the start and end of the node and zone */
  2387. get_pfn_range_for_nid(nid, &node_start_pfn, &node_end_pfn);
  2388. zone_start_pfn = arch_zone_lowest_possible_pfn[zone_type];
  2389. zone_end_pfn = arch_zone_highest_possible_pfn[zone_type];
  2390. adjust_zone_range_for_zone_movable(nid, zone_type,
  2391. node_start_pfn, node_end_pfn,
  2392. &zone_start_pfn, &zone_end_pfn);
  2393. /* Check that this node has pages within the zone's required range */
  2394. if (zone_end_pfn < node_start_pfn || zone_start_pfn > node_end_pfn)
  2395. return 0;
  2396. /* Move the zone boundaries inside the node if necessary */
  2397. zone_end_pfn = min(zone_end_pfn, node_end_pfn);
  2398. zone_start_pfn = max(zone_start_pfn, node_start_pfn);
  2399. /* Return the spanned pages */
  2400. return zone_end_pfn - zone_start_pfn;
  2401. }
  2402. /*
  2403. * Return the number of holes in a range on a node. If nid is MAX_NUMNODES,
  2404. * then all holes in the requested range will be accounted for.
  2405. */
  2406. unsigned long __meminit __absent_pages_in_range(int nid,
  2407. unsigned long range_start_pfn,
  2408. unsigned long range_end_pfn)
  2409. {
  2410. int i = 0;
  2411. unsigned long prev_end_pfn = 0, hole_pages = 0;
  2412. unsigned long start_pfn;
  2413. /* Find the end_pfn of the first active range of pfns in the node */
  2414. i = first_active_region_index_in_nid(nid);
  2415. if (i == -1)
  2416. return 0;
  2417. prev_end_pfn = min(early_node_map[i].start_pfn, range_end_pfn);
  2418. /* Account for ranges before physical memory on this node */
  2419. if (early_node_map[i].start_pfn > range_start_pfn)
  2420. hole_pages = prev_end_pfn - range_start_pfn;
  2421. /* Find all holes for the zone within the node */
  2422. for (; i != -1; i = next_active_region_index_in_nid(i, nid)) {
  2423. /* No need to continue if prev_end_pfn is outside the zone */
  2424. if (prev_end_pfn >= range_end_pfn)
  2425. break;
  2426. /* Make sure the end of the zone is not within the hole */
  2427. start_pfn = min(early_node_map[i].start_pfn, range_end_pfn);
  2428. prev_end_pfn = max(prev_end_pfn, range_start_pfn);
  2429. /* Update the hole size cound and move on */
  2430. if (start_pfn > range_start_pfn) {
  2431. BUG_ON(prev_end_pfn > start_pfn);
  2432. hole_pages += start_pfn - prev_end_pfn;
  2433. }
  2434. prev_end_pfn = early_node_map[i].end_pfn;
  2435. }
  2436. /* Account for ranges past physical memory on this node */
  2437. if (range_end_pfn > prev_end_pfn)
  2438. hole_pages += range_end_pfn -
  2439. max(range_start_pfn, prev_end_pfn);
  2440. return hole_pages;
  2441. }
  2442. /**
  2443. * absent_pages_in_range - Return number of page frames in holes within a range
  2444. * @start_pfn: The start PFN to start searching for holes
  2445. * @end_pfn: The end PFN to stop searching for holes
  2446. *
  2447. * It returns the number of pages frames in memory holes within a range.
  2448. */
  2449. unsigned long __init absent_pages_in_range(unsigned long start_pfn,
  2450. unsigned long end_pfn)
  2451. {
  2452. return __absent_pages_in_range(MAX_NUMNODES, start_pfn, end_pfn);
  2453. }
  2454. /* Return the number of page frames in holes in a zone on a node */
  2455. static unsigned long __meminit zone_absent_pages_in_node(int nid,
  2456. unsigned long zone_type,
  2457. unsigned long *ignored)
  2458. {
  2459. unsigned long node_start_pfn, node_end_pfn;
  2460. unsigned long zone_start_pfn, zone_end_pfn;
  2461. get_pfn_range_for_nid(nid, &node_start_pfn, &node_end_pfn);
  2462. zone_start_pfn = max(arch_zone_lowest_possible_pfn[zone_type],
  2463. node_start_pfn);
  2464. zone_end_pfn = min(arch_zone_highest_possible_pfn[zone_type],
  2465. node_end_pfn);
  2466. adjust_zone_range_for_zone_movable(nid, zone_type,
  2467. node_start_pfn, node_end_pfn,
  2468. &zone_start_pfn, &zone_end_pfn);
  2469. return __absent_pages_in_range(nid, zone_start_pfn, zone_end_pfn);
  2470. }
  2471. #else
  2472. static inline unsigned long __meminit zone_spanned_pages_in_node(int nid,
  2473. unsigned long zone_type,
  2474. unsigned long *zones_size)
  2475. {
  2476. return zones_size[zone_type];
  2477. }
  2478. static inline unsigned long __meminit zone_absent_pages_in_node(int nid,
  2479. unsigned long zone_type,
  2480. unsigned long *zholes_size)
  2481. {
  2482. if (!zholes_size)
  2483. return 0;
  2484. return zholes_size[zone_type];
  2485. }
  2486. #endif
  2487. static void __meminit calculate_node_totalpages(struct pglist_data *pgdat,
  2488. unsigned long *zones_size, unsigned long *zholes_size)
  2489. {
  2490. unsigned long realtotalpages, totalpages = 0;
  2491. enum zone_type i;
  2492. for (i = 0; i < MAX_NR_ZONES; i++)
  2493. totalpages += zone_spanned_pages_in_node(pgdat->node_id, i,
  2494. zones_size);
  2495. pgdat->node_spanned_pages = totalpages;
  2496. realtotalpages = totalpages;
  2497. for (i = 0; i < MAX_NR_ZONES; i++)
  2498. realtotalpages -=
  2499. zone_absent_pages_in_node(pgdat->node_id, i,
  2500. zholes_size);
  2501. pgdat->node_present_pages = realtotalpages;
  2502. printk(KERN_DEBUG "On node %d totalpages: %lu\n", pgdat->node_id,
  2503. realtotalpages);
  2504. }
  2505. /*
  2506. * Set up the zone data structures:
  2507. * - mark all pages reserved
  2508. * - mark all memory queues empty
  2509. * - clear the memory bitmaps
  2510. */
  2511. static void __meminit free_area_init_core(struct pglist_data *pgdat,
  2512. unsigned long *zones_size, unsigned long *zholes_size)
  2513. {
  2514. enum zone_type j;
  2515. int nid = pgdat->node_id;
  2516. unsigned long zone_start_pfn = pgdat->node_start_pfn;
  2517. int ret;
  2518. pgdat_resize_init(pgdat);
  2519. pgdat->nr_zones = 0;
  2520. init_waitqueue_head(&pgdat->kswapd_wait);
  2521. pgdat->kswapd_max_order = 0;
  2522. for (j = 0; j < MAX_NR_ZONES; j++) {
  2523. struct zone *zone = pgdat->node_zones + j;
  2524. unsigned long size, realsize, memmap_pages;
  2525. size = zone_spanned_pages_in_node(nid, j, zones_size);
  2526. realsize = size - zone_absent_pages_in_node(nid, j,
  2527. zholes_size);
  2528. /*
  2529. * Adjust realsize so that it accounts for how much memory
  2530. * is used by this zone for memmap. This affects the watermark
  2531. * and per-cpu initialisations
  2532. */
  2533. memmap_pages = (size * sizeof(struct page)) >> PAGE_SHIFT;
  2534. if (realsize >= memmap_pages) {
  2535. realsize -= memmap_pages;
  2536. printk(KERN_DEBUG
  2537. " %s zone: %lu pages used for memmap\n",
  2538. zone_names[j], memmap_pages);
  2539. } else
  2540. printk(KERN_WARNING
  2541. " %s zone: %lu pages exceeds realsize %lu\n",
  2542. zone_names[j], memmap_pages, realsize);
  2543. /* Account for reserved pages */
  2544. if (j == 0 && realsize > dma_reserve) {
  2545. realsize -= dma_reserve;
  2546. printk(KERN_DEBUG " %s zone: %lu pages reserved\n",
  2547. zone_names[0], dma_reserve);
  2548. }
  2549. if (!is_highmem_idx(j))
  2550. nr_kernel_pages += realsize;
  2551. nr_all_pages += realsize;
  2552. zone->spanned_pages = size;
  2553. zone->present_pages = realsize;
  2554. #ifdef CONFIG_NUMA
  2555. zone->node = nid;
  2556. zone->min_unmapped_pages = (realsize*sysctl_min_unmapped_ratio)
  2557. / 100;
  2558. zone->min_slab_pages = (realsize * sysctl_min_slab_ratio) / 100;
  2559. #endif
  2560. zone->name = zone_names[j];
  2561. spin_lock_init(&zone->lock);
  2562. spin_lock_init(&zone->lru_lock);
  2563. zone_seqlock_init(zone);
  2564. zone->zone_pgdat = pgdat;
  2565. zone->prev_priority = DEF_PRIORITY;
  2566. zone_pcp_init(zone);
  2567. INIT_LIST_HEAD(&zone->active_list);
  2568. INIT_LIST_HEAD(&zone->inactive_list);
  2569. zone->nr_scan_active = 0;
  2570. zone->nr_scan_inactive = 0;
  2571. zap_zone_vm_stats(zone);
  2572. atomic_set(&zone->reclaim_in_progress, 0);
  2573. if (!size)
  2574. continue;
  2575. ret = init_currently_empty_zone(zone, zone_start_pfn,
  2576. size, MEMMAP_EARLY);
  2577. BUG_ON(ret);
  2578. zone_start_pfn += size;
  2579. }
  2580. }
  2581. static void __init_refok alloc_node_mem_map(struct pglist_data *pgdat)
  2582. {
  2583. /* Skip empty nodes */
  2584. if (!pgdat->node_spanned_pages)
  2585. return;
  2586. #ifdef CONFIG_FLAT_NODE_MEM_MAP
  2587. /* ia64 gets its own node_mem_map, before this, without bootmem */
  2588. if (!pgdat->node_mem_map) {
  2589. unsigned long size, start, end;
  2590. struct page *map;
  2591. /*
  2592. * The zone's endpoints aren't required to be MAX_ORDER
  2593. * aligned but the node_mem_map endpoints must be in order
  2594. * for the buddy allocator to function correctly.
  2595. */
  2596. start = pgdat->node_start_pfn & ~(MAX_ORDER_NR_PAGES - 1);
  2597. end = pgdat->node_start_pfn + pgdat->node_spanned_pages;
  2598. end = ALIGN(end, MAX_ORDER_NR_PAGES);
  2599. size = (end - start) * sizeof(struct page);
  2600. map = alloc_remap(pgdat->node_id, size);
  2601. if (!map)
  2602. map = alloc_bootmem_node(pgdat, size);
  2603. pgdat->node_mem_map = map + (pgdat->node_start_pfn - start);
  2604. }
  2605. #ifndef CONFIG_NEED_MULTIPLE_NODES
  2606. /*
  2607. * With no DISCONTIG, the global mem_map is just set as node 0's
  2608. */
  2609. if (pgdat == NODE_DATA(0)) {
  2610. mem_map = NODE_DATA(0)->node_mem_map;
  2611. #ifdef CONFIG_ARCH_POPULATES_NODE_MAP
  2612. if (page_to_pfn(mem_map) != pgdat->node_start_pfn)
  2613. mem_map -= pgdat->node_start_pfn;
  2614. #endif /* CONFIG_ARCH_POPULATES_NODE_MAP */
  2615. }
  2616. #endif
  2617. #endif /* CONFIG_FLAT_NODE_MEM_MAP */
  2618. }
  2619. void __meminit free_area_init_node(int nid, struct pglist_data *pgdat,
  2620. unsigned long *zones_size, unsigned long node_start_pfn,
  2621. unsigned long *zholes_size)
  2622. {
  2623. pgdat->node_id = nid;
  2624. pgdat->node_start_pfn = node_start_pfn;
  2625. calculate_node_totalpages(pgdat, zones_size, zholes_size);
  2626. alloc_node_mem_map(pgdat);
  2627. free_area_init_core(pgdat, zones_size, zholes_size);
  2628. }
  2629. #ifdef CONFIG_ARCH_POPULATES_NODE_MAP
  2630. #if MAX_NUMNODES > 1
  2631. /*
  2632. * Figure out the number of possible node ids.
  2633. */
  2634. static void __init setup_nr_node_ids(void)
  2635. {
  2636. unsigned int node;
  2637. unsigned int highest = 0;
  2638. for_each_node_mask(node, node_possible_map)
  2639. highest = node;
  2640. nr_node_ids = highest + 1;
  2641. }
  2642. #else
  2643. static inline void setup_nr_node_ids(void)
  2644. {
  2645. }
  2646. #endif
  2647. /**
  2648. * add_active_range - Register a range of PFNs backed by physical memory
  2649. * @nid: The node ID the range resides on
  2650. * @start_pfn: The start PFN of the available physical memory
  2651. * @end_pfn: The end PFN of the available physical memory
  2652. *
  2653. * These ranges are stored in an early_node_map[] and later used by
  2654. * free_area_init_nodes() to calculate zone sizes and holes. If the
  2655. * range spans a memory hole, it is up to the architecture to ensure
  2656. * the memory is not freed by the bootmem allocator. If possible
  2657. * the range being registered will be merged with existing ranges.
  2658. */
  2659. void __init add_active_range(unsigned int nid, unsigned long start_pfn,
  2660. unsigned long end_pfn)
  2661. {
  2662. int i;
  2663. printk(KERN_DEBUG "Entering add_active_range(%d, %lu, %lu) "
  2664. "%d entries of %d used\n",
  2665. nid, start_pfn, end_pfn,
  2666. nr_nodemap_entries, MAX_ACTIVE_REGIONS);
  2667. /* Merge with existing active regions if possible */
  2668. for (i = 0; i < nr_nodemap_entries; i++) {
  2669. if (early_node_map[i].nid != nid)
  2670. continue;
  2671. /* Skip if an existing region covers this new one */
  2672. if (start_pfn >= early_node_map[i].start_pfn &&
  2673. end_pfn <= early_node_map[i].end_pfn)
  2674. return;
  2675. /* Merge forward if suitable */
  2676. if (start_pfn <= early_node_map[i].end_pfn &&
  2677. end_pfn > early_node_map[i].end_pfn) {
  2678. early_node_map[i].end_pfn = end_pfn;
  2679. return;
  2680. }
  2681. /* Merge backward if suitable */
  2682. if (start_pfn < early_node_map[i].end_pfn &&
  2683. end_pfn >= early_node_map[i].start_pfn) {
  2684. early_node_map[i].start_pfn = start_pfn;
  2685. return;
  2686. }
  2687. }
  2688. /* Check that early_node_map is large enough */
  2689. if (i >= MAX_ACTIVE_REGIONS) {
  2690. printk(KERN_CRIT "More than %d memory regions, truncating\n",
  2691. MAX_ACTIVE_REGIONS);
  2692. return;
  2693. }
  2694. early_node_map[i].nid = nid;
  2695. early_node_map[i].start_pfn = start_pfn;
  2696. early_node_map[i].end_pfn = end_pfn;
  2697. nr_nodemap_entries = i + 1;
  2698. }
  2699. /**
  2700. * shrink_active_range - Shrink an existing registered range of PFNs
  2701. * @nid: The node id the range is on that should be shrunk
  2702. * @old_end_pfn: The old end PFN of the range
  2703. * @new_end_pfn: The new PFN of the range
  2704. *
  2705. * i386 with NUMA use alloc_remap() to store a node_mem_map on a local node.
  2706. * The map is kept at the end physical page range that has already been
  2707. * registered with add_active_range(). This function allows an arch to shrink
  2708. * an existing registered range.
  2709. */
  2710. void __init shrink_active_range(unsigned int nid, unsigned long old_end_pfn,
  2711. unsigned long new_end_pfn)
  2712. {
  2713. int i;
  2714. /* Find the old active region end and shrink */
  2715. for_each_active_range_index_in_nid(i, nid)
  2716. if (early_node_map[i].end_pfn == old_end_pfn) {
  2717. early_node_map[i].end_pfn = new_end_pfn;
  2718. break;
  2719. }
  2720. }
  2721. /**
  2722. * remove_all_active_ranges - Remove all currently registered regions
  2723. *
  2724. * During discovery, it may be found that a table like SRAT is invalid
  2725. * and an alternative discovery method must be used. This function removes
  2726. * all currently registered regions.
  2727. */
  2728. void __init remove_all_active_ranges(void)
  2729. {
  2730. memset(early_node_map, 0, sizeof(early_node_map));
  2731. nr_nodemap_entries = 0;
  2732. #ifdef CONFIG_MEMORY_HOTPLUG_RESERVE
  2733. memset(node_boundary_start_pfn, 0, sizeof(node_boundary_start_pfn));
  2734. memset(node_boundary_end_pfn, 0, sizeof(node_boundary_end_pfn));
  2735. #endif /* CONFIG_MEMORY_HOTPLUG_RESERVE */
  2736. }
  2737. /* Compare two active node_active_regions */
  2738. static int __init cmp_node_active_region(const void *a, const void *b)
  2739. {
  2740. struct node_active_region *arange = (struct node_active_region *)a;
  2741. struct node_active_region *brange = (struct node_active_region *)b;
  2742. /* Done this way to avoid overflows */
  2743. if (arange->start_pfn > brange->start_pfn)
  2744. return 1;
  2745. if (arange->start_pfn < brange->start_pfn)
  2746. return -1;
  2747. return 0;
  2748. }
  2749. /* sort the node_map by start_pfn */
  2750. static void __init sort_node_map(void)
  2751. {
  2752. sort(early_node_map, (size_t)nr_nodemap_entries,
  2753. sizeof(struct node_active_region),
  2754. cmp_node_active_region, NULL);
  2755. }
  2756. /* Find the lowest pfn for a node */
  2757. unsigned long __init find_min_pfn_for_node(unsigned long nid)
  2758. {
  2759. int i;
  2760. unsigned long min_pfn = ULONG_MAX;
  2761. /* Assuming a sorted map, the first range found has the starting pfn */
  2762. for_each_active_range_index_in_nid(i, nid)
  2763. min_pfn = min(min_pfn, early_node_map[i].start_pfn);
  2764. if (min_pfn == ULONG_MAX) {
  2765. printk(KERN_WARNING
  2766. "Could not find start_pfn for node %lu\n", nid);
  2767. return 0;
  2768. }
  2769. return min_pfn;
  2770. }
  2771. /**
  2772. * find_min_pfn_with_active_regions - Find the minimum PFN registered
  2773. *
  2774. * It returns the minimum PFN based on information provided via
  2775. * add_active_range().
  2776. */
  2777. unsigned long __init find_min_pfn_with_active_regions(void)
  2778. {
  2779. return find_min_pfn_for_node(MAX_NUMNODES);
  2780. }
  2781. /**
  2782. * find_max_pfn_with_active_regions - Find the maximum PFN registered
  2783. *
  2784. * It returns the maximum PFN based on information provided via
  2785. * add_active_range().
  2786. */
  2787. unsigned long __init find_max_pfn_with_active_regions(void)
  2788. {
  2789. int i;
  2790. unsigned long max_pfn = 0;
  2791. for (i = 0; i < nr_nodemap_entries; i++)
  2792. max_pfn = max(max_pfn, early_node_map[i].end_pfn);
  2793. return max_pfn;
  2794. }
  2795. unsigned long __init early_calculate_totalpages(void)
  2796. {
  2797. int i;
  2798. unsigned long totalpages = 0;
  2799. for (i = 0; i < nr_nodemap_entries; i++)
  2800. totalpages += early_node_map[i].end_pfn -
  2801. early_node_map[i].start_pfn;
  2802. return totalpages;
  2803. }
  2804. /*
  2805. * Find the PFN the Movable zone begins in each node. Kernel memory
  2806. * is spread evenly between nodes as long as the nodes have enough
  2807. * memory. When they don't, some nodes will have more kernelcore than
  2808. * others
  2809. */
  2810. void __init find_zone_movable_pfns_for_nodes(unsigned long *movable_pfn)
  2811. {
  2812. int i, nid;
  2813. unsigned long usable_startpfn;
  2814. unsigned long kernelcore_node, kernelcore_remaining;
  2815. int usable_nodes = num_online_nodes();
  2816. /*
  2817. * If movablecore was specified, calculate what size of
  2818. * kernelcore that corresponds so that memory usable for
  2819. * any allocation type is evenly spread. If both kernelcore
  2820. * and movablecore are specified, then the value of kernelcore
  2821. * will be used for required_kernelcore if it's greater than
  2822. * what movablecore would have allowed.
  2823. */
  2824. if (required_movablecore) {
  2825. unsigned long totalpages = early_calculate_totalpages();
  2826. unsigned long corepages;
  2827. /*
  2828. * Round-up so that ZONE_MOVABLE is at least as large as what
  2829. * was requested by the user
  2830. */
  2831. required_movablecore =
  2832. roundup(required_movablecore, MAX_ORDER_NR_PAGES);
  2833. corepages = totalpages - required_movablecore;
  2834. required_kernelcore = max(required_kernelcore, corepages);
  2835. }
  2836. /* If kernelcore was not specified, there is no ZONE_MOVABLE */
  2837. if (!required_kernelcore)
  2838. return;
  2839. /* usable_startpfn is the lowest possible pfn ZONE_MOVABLE can be at */
  2840. find_usable_zone_for_movable();
  2841. usable_startpfn = arch_zone_lowest_possible_pfn[movable_zone];
  2842. restart:
  2843. /* Spread kernelcore memory as evenly as possible throughout nodes */
  2844. kernelcore_node = required_kernelcore / usable_nodes;
  2845. for_each_online_node(nid) {
  2846. /*
  2847. * Recalculate kernelcore_node if the division per node
  2848. * now exceeds what is necessary to satisfy the requested
  2849. * amount of memory for the kernel
  2850. */
  2851. if (required_kernelcore < kernelcore_node)
  2852. kernelcore_node = required_kernelcore / usable_nodes;
  2853. /*
  2854. * As the map is walked, we track how much memory is usable
  2855. * by the kernel using kernelcore_remaining. When it is
  2856. * 0, the rest of the node is usable by ZONE_MOVABLE
  2857. */
  2858. kernelcore_remaining = kernelcore_node;
  2859. /* Go through each range of PFNs within this node */
  2860. for_each_active_range_index_in_nid(i, nid) {
  2861. unsigned long start_pfn, end_pfn;
  2862. unsigned long size_pages;
  2863. start_pfn = max(early_node_map[i].start_pfn,
  2864. zone_movable_pfn[nid]);
  2865. end_pfn = early_node_map[i].end_pfn;
  2866. if (start_pfn >= end_pfn)
  2867. continue;
  2868. /* Account for what is only usable for kernelcore */
  2869. if (start_pfn < usable_startpfn) {
  2870. unsigned long kernel_pages;
  2871. kernel_pages = min(end_pfn, usable_startpfn)
  2872. - start_pfn;
  2873. kernelcore_remaining -= min(kernel_pages,
  2874. kernelcore_remaining);
  2875. required_kernelcore -= min(kernel_pages,
  2876. required_kernelcore);
  2877. /* Continue if range is now fully accounted */
  2878. if (end_pfn <= usable_startpfn) {
  2879. /*
  2880. * Push zone_movable_pfn to the end so
  2881. * that if we have to rebalance
  2882. * kernelcore across nodes, we will
  2883. * not double account here
  2884. */
  2885. zone_movable_pfn[nid] = end_pfn;
  2886. continue;
  2887. }
  2888. start_pfn = usable_startpfn;
  2889. }
  2890. /*
  2891. * The usable PFN range for ZONE_MOVABLE is from
  2892. * start_pfn->end_pfn. Calculate size_pages as the
  2893. * number of pages used as kernelcore
  2894. */
  2895. size_pages = end_pfn - start_pfn;
  2896. if (size_pages > kernelcore_remaining)
  2897. size_pages = kernelcore_remaining;
  2898. zone_movable_pfn[nid] = start_pfn + size_pages;
  2899. /*
  2900. * Some kernelcore has been met, update counts and
  2901. * break if the kernelcore for this node has been
  2902. * satisified
  2903. */
  2904. required_kernelcore -= min(required_kernelcore,
  2905. size_pages);
  2906. kernelcore_remaining -= size_pages;
  2907. if (!kernelcore_remaining)
  2908. break;
  2909. }
  2910. }
  2911. /*
  2912. * If there is still required_kernelcore, we do another pass with one
  2913. * less node in the count. This will push zone_movable_pfn[nid] further
  2914. * along on the nodes that still have memory until kernelcore is
  2915. * satisified
  2916. */
  2917. usable_nodes--;
  2918. if (usable_nodes && required_kernelcore > usable_nodes)
  2919. goto restart;
  2920. /* Align start of ZONE_MOVABLE on all nids to MAX_ORDER_NR_PAGES */
  2921. for (nid = 0; nid < MAX_NUMNODES; nid++)
  2922. zone_movable_pfn[nid] =
  2923. roundup(zone_movable_pfn[nid], MAX_ORDER_NR_PAGES);
  2924. }
  2925. /**
  2926. * free_area_init_nodes - Initialise all pg_data_t and zone data
  2927. * @max_zone_pfn: an array of max PFNs for each zone
  2928. *
  2929. * This will call free_area_init_node() for each active node in the system.
  2930. * Using the page ranges provided by add_active_range(), the size of each
  2931. * zone in each node and their holes is calculated. If the maximum PFN
  2932. * between two adjacent zones match, it is assumed that the zone is empty.
  2933. * For example, if arch_max_dma_pfn == arch_max_dma32_pfn, it is assumed
  2934. * that arch_max_dma32_pfn has no pages. It is also assumed that a zone
  2935. * starts where the previous one ended. For example, ZONE_DMA32 starts
  2936. * at arch_max_dma_pfn.
  2937. */
  2938. void __init free_area_init_nodes(unsigned long *max_zone_pfn)
  2939. {
  2940. unsigned long nid;
  2941. enum zone_type i;
  2942. /* Sort early_node_map as initialisation assumes it is sorted */
  2943. sort_node_map();
  2944. /* Record where the zone boundaries are */
  2945. memset(arch_zone_lowest_possible_pfn, 0,
  2946. sizeof(arch_zone_lowest_possible_pfn));
  2947. memset(arch_zone_highest_possible_pfn, 0,
  2948. sizeof(arch_zone_highest_possible_pfn));
  2949. arch_zone_lowest_possible_pfn[0] = find_min_pfn_with_active_regions();
  2950. arch_zone_highest_possible_pfn[0] = max_zone_pfn[0];
  2951. for (i = 1; i < MAX_NR_ZONES; i++) {
  2952. if (i == ZONE_MOVABLE)
  2953. continue;
  2954. arch_zone_lowest_possible_pfn[i] =
  2955. arch_zone_highest_possible_pfn[i-1];
  2956. arch_zone_highest_possible_pfn[i] =
  2957. max(max_zone_pfn[i], arch_zone_lowest_possible_pfn[i]);
  2958. }
  2959. arch_zone_lowest_possible_pfn[ZONE_MOVABLE] = 0;
  2960. arch_zone_highest_possible_pfn[ZONE_MOVABLE] = 0;
  2961. /* Find the PFNs that ZONE_MOVABLE begins at in each node */
  2962. memset(zone_movable_pfn, 0, sizeof(zone_movable_pfn));
  2963. find_zone_movable_pfns_for_nodes(zone_movable_pfn);
  2964. /* Print out the zone ranges */
  2965. printk("Zone PFN ranges:\n");
  2966. for (i = 0; i < MAX_NR_ZONES; i++) {
  2967. if (i == ZONE_MOVABLE)
  2968. continue;
  2969. printk(" %-8s %8lu -> %8lu\n",
  2970. zone_names[i],
  2971. arch_zone_lowest_possible_pfn[i],
  2972. arch_zone_highest_possible_pfn[i]);
  2973. }
  2974. /* Print out the PFNs ZONE_MOVABLE begins at in each node */
  2975. printk("Movable zone start PFN for each node\n");
  2976. for (i = 0; i < MAX_NUMNODES; i++) {
  2977. if (zone_movable_pfn[i])
  2978. printk(" Node %d: %lu\n", i, zone_movable_pfn[i]);
  2979. }
  2980. /* Print out the early_node_map[] */
  2981. printk("early_node_map[%d] active PFN ranges\n", nr_nodemap_entries);
  2982. for (i = 0; i < nr_nodemap_entries; i++)
  2983. printk(" %3d: %8lu -> %8lu\n", early_node_map[i].nid,
  2984. early_node_map[i].start_pfn,
  2985. early_node_map[i].end_pfn);
  2986. /* Initialise every node */
  2987. setup_nr_node_ids();
  2988. for_each_online_node(nid) {
  2989. pg_data_t *pgdat = NODE_DATA(nid);
  2990. free_area_init_node(nid, pgdat, NULL,
  2991. find_min_pfn_for_node(nid), NULL);
  2992. }
  2993. }
  2994. static int __init cmdline_parse_core(char *p, unsigned long *core)
  2995. {
  2996. unsigned long long coremem;
  2997. if (!p)
  2998. return -EINVAL;
  2999. coremem = memparse(p, &p);
  3000. *core = coremem >> PAGE_SHIFT;
  3001. /* Paranoid check that UL is enough for the coremem value */
  3002. WARN_ON((coremem >> PAGE_SHIFT) > ULONG_MAX);
  3003. return 0;
  3004. }
  3005. /*
  3006. * kernelcore=size sets the amount of memory for use for allocations that
  3007. * cannot be reclaimed or migrated.
  3008. */
  3009. static int __init cmdline_parse_kernelcore(char *p)
  3010. {
  3011. return cmdline_parse_core(p, &required_kernelcore);
  3012. }
  3013. /*
  3014. * movablecore=size sets the amount of memory for use for allocations that
  3015. * can be reclaimed or migrated.
  3016. */
  3017. static int __init cmdline_parse_movablecore(char *p)
  3018. {
  3019. return cmdline_parse_core(p, &required_movablecore);
  3020. }
  3021. early_param("kernelcore", cmdline_parse_kernelcore);
  3022. early_param("movablecore", cmdline_parse_movablecore);
  3023. #endif /* CONFIG_ARCH_POPULATES_NODE_MAP */
  3024. /**
  3025. * set_dma_reserve - set the specified number of pages reserved in the first zone
  3026. * @new_dma_reserve: The number of pages to mark reserved
  3027. *
  3028. * The per-cpu batchsize and zone watermarks are determined by present_pages.
  3029. * In the DMA zone, a significant percentage may be consumed by kernel image
  3030. * and other unfreeable allocations which can skew the watermarks badly. This
  3031. * function may optionally be used to account for unfreeable pages in the
  3032. * first zone (e.g., ZONE_DMA). The effect will be lower watermarks and
  3033. * smaller per-cpu batchsize.
  3034. */
  3035. void __init set_dma_reserve(unsigned long new_dma_reserve)
  3036. {
  3037. dma_reserve = new_dma_reserve;
  3038. }
  3039. #ifndef CONFIG_NEED_MULTIPLE_NODES
  3040. static bootmem_data_t contig_bootmem_data;
  3041. struct pglist_data contig_page_data = { .bdata = &contig_bootmem_data };
  3042. EXPORT_SYMBOL(contig_page_data);
  3043. #endif
  3044. void __init free_area_init(unsigned long *zones_size)
  3045. {
  3046. free_area_init_node(0, NODE_DATA(0), zones_size,
  3047. __pa(PAGE_OFFSET) >> PAGE_SHIFT, NULL);
  3048. }
  3049. static int page_alloc_cpu_notify(struct notifier_block *self,
  3050. unsigned long action, void *hcpu)
  3051. {
  3052. int cpu = (unsigned long)hcpu;
  3053. if (action == CPU_DEAD || action == CPU_DEAD_FROZEN) {
  3054. local_irq_disable();
  3055. __drain_pages(cpu);
  3056. vm_events_fold_cpu(cpu);
  3057. local_irq_enable();
  3058. refresh_cpu_vm_stats(cpu);
  3059. }
  3060. return NOTIFY_OK;
  3061. }
  3062. void __init page_alloc_init(void)
  3063. {
  3064. hotcpu_notifier(page_alloc_cpu_notify, 0);
  3065. }
  3066. /*
  3067. * calculate_totalreserve_pages - called when sysctl_lower_zone_reserve_ratio
  3068. * or min_free_kbytes changes.
  3069. */
  3070. static void calculate_totalreserve_pages(void)
  3071. {
  3072. struct pglist_data *pgdat;
  3073. unsigned long reserve_pages = 0;
  3074. enum zone_type i, j;
  3075. for_each_online_pgdat(pgdat) {
  3076. for (i = 0; i < MAX_NR_ZONES; i++) {
  3077. struct zone *zone = pgdat->node_zones + i;
  3078. unsigned long max = 0;
  3079. /* Find valid and maximum lowmem_reserve in the zone */
  3080. for (j = i; j < MAX_NR_ZONES; j++) {
  3081. if (zone->lowmem_reserve[j] > max)
  3082. max = zone->lowmem_reserve[j];
  3083. }
  3084. /* we treat pages_high as reserved pages. */
  3085. max += zone->pages_high;
  3086. if (max > zone->present_pages)
  3087. max = zone->present_pages;
  3088. reserve_pages += max;
  3089. }
  3090. }
  3091. totalreserve_pages = reserve_pages;
  3092. }
  3093. /*
  3094. * setup_per_zone_lowmem_reserve - called whenever
  3095. * sysctl_lower_zone_reserve_ratio changes. Ensures that each zone
  3096. * has a correct pages reserved value, so an adequate number of
  3097. * pages are left in the zone after a successful __alloc_pages().
  3098. */
  3099. static void setup_per_zone_lowmem_reserve(void)
  3100. {
  3101. struct pglist_data *pgdat;
  3102. enum zone_type j, idx;
  3103. for_each_online_pgdat(pgdat) {
  3104. for (j = 0; j < MAX_NR_ZONES; j++) {
  3105. struct zone *zone = pgdat->node_zones + j;
  3106. unsigned long present_pages = zone->present_pages;
  3107. zone->lowmem_reserve[j] = 0;
  3108. idx = j;
  3109. while (idx) {
  3110. struct zone *lower_zone;
  3111. idx--;
  3112. if (sysctl_lowmem_reserve_ratio[idx] < 1)
  3113. sysctl_lowmem_reserve_ratio[idx] = 1;
  3114. lower_zone = pgdat->node_zones + idx;
  3115. lower_zone->lowmem_reserve[j] = present_pages /
  3116. sysctl_lowmem_reserve_ratio[idx];
  3117. present_pages += lower_zone->present_pages;
  3118. }
  3119. }
  3120. }
  3121. /* update totalreserve_pages */
  3122. calculate_totalreserve_pages();
  3123. }
  3124. /**
  3125. * setup_per_zone_pages_min - called when min_free_kbytes changes.
  3126. *
  3127. * Ensures that the pages_{min,low,high} values for each zone are set correctly
  3128. * with respect to min_free_kbytes.
  3129. */
  3130. void setup_per_zone_pages_min(void)
  3131. {
  3132. unsigned long pages_min = min_free_kbytes >> (PAGE_SHIFT - 10);
  3133. unsigned long lowmem_pages = 0;
  3134. struct zone *zone;
  3135. unsigned long flags;
  3136. /* Calculate total number of !ZONE_HIGHMEM pages */
  3137. for_each_zone(zone) {
  3138. if (!is_highmem(zone))
  3139. lowmem_pages += zone->present_pages;
  3140. }
  3141. for_each_zone(zone) {
  3142. u64 tmp;
  3143. spin_lock_irqsave(&zone->lru_lock, flags);
  3144. tmp = (u64)pages_min * zone->present_pages;
  3145. do_div(tmp, lowmem_pages);
  3146. if (is_highmem(zone)) {
  3147. /*
  3148. * __GFP_HIGH and PF_MEMALLOC allocations usually don't
  3149. * need highmem pages, so cap pages_min to a small
  3150. * value here.
  3151. *
  3152. * The (pages_high-pages_low) and (pages_low-pages_min)
  3153. * deltas controls asynch page reclaim, and so should
  3154. * not be capped for highmem.
  3155. */
  3156. int min_pages;
  3157. min_pages = zone->present_pages / 1024;
  3158. if (min_pages < SWAP_CLUSTER_MAX)
  3159. min_pages = SWAP_CLUSTER_MAX;
  3160. if (min_pages > 128)
  3161. min_pages = 128;
  3162. zone->pages_min = min_pages;
  3163. } else {
  3164. /*
  3165. * If it's a lowmem zone, reserve a number of pages
  3166. * proportionate to the zone's size.
  3167. */
  3168. zone->pages_min = tmp;
  3169. }
  3170. zone->pages_low = zone->pages_min + (tmp >> 2);
  3171. zone->pages_high = zone->pages_min + (tmp >> 1);
  3172. spin_unlock_irqrestore(&zone->lru_lock, flags);
  3173. }
  3174. /* update totalreserve_pages */
  3175. calculate_totalreserve_pages();
  3176. }
  3177. /*
  3178. * Initialise min_free_kbytes.
  3179. *
  3180. * For small machines we want it small (128k min). For large machines
  3181. * we want it large (64MB max). But it is not linear, because network
  3182. * bandwidth does not increase linearly with machine size. We use
  3183. *
  3184. * min_free_kbytes = 4 * sqrt(lowmem_kbytes), for better accuracy:
  3185. * min_free_kbytes = sqrt(lowmem_kbytes * 16)
  3186. *
  3187. * which yields
  3188. *
  3189. * 16MB: 512k
  3190. * 32MB: 724k
  3191. * 64MB: 1024k
  3192. * 128MB: 1448k
  3193. * 256MB: 2048k
  3194. * 512MB: 2896k
  3195. * 1024MB: 4096k
  3196. * 2048MB: 5792k
  3197. * 4096MB: 8192k
  3198. * 8192MB: 11584k
  3199. * 16384MB: 16384k
  3200. */
  3201. static int __init init_per_zone_pages_min(void)
  3202. {
  3203. unsigned long lowmem_kbytes;
  3204. lowmem_kbytes = nr_free_buffer_pages() * (PAGE_SIZE >> 10);
  3205. min_free_kbytes = int_sqrt(lowmem_kbytes * 16);
  3206. if (min_free_kbytes < 128)
  3207. min_free_kbytes = 128;
  3208. if (min_free_kbytes > 65536)
  3209. min_free_kbytes = 65536;
  3210. setup_per_zone_pages_min();
  3211. setup_per_zone_lowmem_reserve();
  3212. return 0;
  3213. }
  3214. module_init(init_per_zone_pages_min)
  3215. /*
  3216. * min_free_kbytes_sysctl_handler - just a wrapper around proc_dointvec() so
  3217. * that we can call two helper functions whenever min_free_kbytes
  3218. * changes.
  3219. */
  3220. int min_free_kbytes_sysctl_handler(ctl_table *table, int write,
  3221. struct file *file, void __user *buffer, size_t *length, loff_t *ppos)
  3222. {
  3223. proc_dointvec(table, write, file, buffer, length, ppos);
  3224. if (write)
  3225. setup_per_zone_pages_min();
  3226. return 0;
  3227. }
  3228. #ifdef CONFIG_NUMA
  3229. int sysctl_min_unmapped_ratio_sysctl_handler(ctl_table *table, int write,
  3230. struct file *file, void __user *buffer, size_t *length, loff_t *ppos)
  3231. {
  3232. struct zone *zone;
  3233. int rc;
  3234. rc = proc_dointvec_minmax(table, write, file, buffer, length, ppos);
  3235. if (rc)
  3236. return rc;
  3237. for_each_zone(zone)
  3238. zone->min_unmapped_pages = (zone->present_pages *
  3239. sysctl_min_unmapped_ratio) / 100;
  3240. return 0;
  3241. }
  3242. int sysctl_min_slab_ratio_sysctl_handler(ctl_table *table, int write,
  3243. struct file *file, void __user *buffer, size_t *length, loff_t *ppos)
  3244. {
  3245. struct zone *zone;
  3246. int rc;
  3247. rc = proc_dointvec_minmax(table, write, file, buffer, length, ppos);
  3248. if (rc)
  3249. return rc;
  3250. for_each_zone(zone)
  3251. zone->min_slab_pages = (zone->present_pages *
  3252. sysctl_min_slab_ratio) / 100;
  3253. return 0;
  3254. }
  3255. #endif
  3256. /*
  3257. * lowmem_reserve_ratio_sysctl_handler - just a wrapper around
  3258. * proc_dointvec() so that we can call setup_per_zone_lowmem_reserve()
  3259. * whenever sysctl_lowmem_reserve_ratio changes.
  3260. *
  3261. * The reserve ratio obviously has absolutely no relation with the
  3262. * pages_min watermarks. The lowmem reserve ratio can only make sense
  3263. * if in function of the boot time zone sizes.
  3264. */
  3265. int lowmem_reserve_ratio_sysctl_handler(ctl_table *table, int write,
  3266. struct file *file, void __user *buffer, size_t *length, loff_t *ppos)
  3267. {
  3268. proc_dointvec_minmax(table, write, file, buffer, length, ppos);
  3269. setup_per_zone_lowmem_reserve();
  3270. return 0;
  3271. }
  3272. /*
  3273. * percpu_pagelist_fraction - changes the pcp->high for each zone on each
  3274. * cpu. It is the fraction of total pages in each zone that a hot per cpu pagelist
  3275. * can have before it gets flushed back to buddy allocator.
  3276. */
  3277. int percpu_pagelist_fraction_sysctl_handler(ctl_table *table, int write,
  3278. struct file *file, void __user *buffer, size_t *length, loff_t *ppos)
  3279. {
  3280. struct zone *zone;
  3281. unsigned int cpu;
  3282. int ret;
  3283. ret = proc_dointvec_minmax(table, write, file, buffer, length, ppos);
  3284. if (!write || (ret == -EINVAL))
  3285. return ret;
  3286. for_each_zone(zone) {
  3287. for_each_online_cpu(cpu) {
  3288. unsigned long high;
  3289. high = zone->present_pages / percpu_pagelist_fraction;
  3290. setup_pagelist_highmark(zone_pcp(zone, cpu), high);
  3291. }
  3292. }
  3293. return 0;
  3294. }
  3295. int hashdist = HASHDIST_DEFAULT;
  3296. #ifdef CONFIG_NUMA
  3297. static int __init set_hashdist(char *str)
  3298. {
  3299. if (!str)
  3300. return 0;
  3301. hashdist = simple_strtoul(str, &str, 0);
  3302. return 1;
  3303. }
  3304. __setup("hashdist=", set_hashdist);
  3305. #endif
  3306. /*
  3307. * allocate a large system hash table from bootmem
  3308. * - it is assumed that the hash table must contain an exact power-of-2
  3309. * quantity of entries
  3310. * - limit is the number of hash buckets, not the total allocation size
  3311. */
  3312. void *__init alloc_large_system_hash(const char *tablename,
  3313. unsigned long bucketsize,
  3314. unsigned long numentries,
  3315. int scale,
  3316. int flags,
  3317. unsigned int *_hash_shift,
  3318. unsigned int *_hash_mask,
  3319. unsigned long limit)
  3320. {
  3321. unsigned long long max = limit;
  3322. unsigned long log2qty, size;
  3323. void *table = NULL;
  3324. /* allow the kernel cmdline to have a say */
  3325. if (!numentries) {
  3326. /* round applicable memory size up to nearest megabyte */
  3327. numentries = nr_kernel_pages;
  3328. numentries += (1UL << (20 - PAGE_SHIFT)) - 1;
  3329. numentries >>= 20 - PAGE_SHIFT;
  3330. numentries <<= 20 - PAGE_SHIFT;
  3331. /* limit to 1 bucket per 2^scale bytes of low memory */
  3332. if (scale > PAGE_SHIFT)
  3333. numentries >>= (scale - PAGE_SHIFT);
  3334. else
  3335. numentries <<= (PAGE_SHIFT - scale);
  3336. /* Make sure we've got at least a 0-order allocation.. */
  3337. if (unlikely((numentries * bucketsize) < PAGE_SIZE))
  3338. numentries = PAGE_SIZE / bucketsize;
  3339. }
  3340. numentries = roundup_pow_of_two(numentries);
  3341. /* limit allocation size to 1/16 total memory by default */
  3342. if (max == 0) {
  3343. max = ((unsigned long long)nr_all_pages << PAGE_SHIFT) >> 4;
  3344. do_div(max, bucketsize);
  3345. }
  3346. if (numentries > max)
  3347. numentries = max;
  3348. log2qty = ilog2(numentries);
  3349. do {
  3350. size = bucketsize << log2qty;
  3351. if (flags & HASH_EARLY)
  3352. table = alloc_bootmem(size);
  3353. else if (hashdist)
  3354. table = __vmalloc(size, GFP_ATOMIC, PAGE_KERNEL);
  3355. else {
  3356. unsigned long order;
  3357. for (order = 0; ((1UL << order) << PAGE_SHIFT) < size; order++)
  3358. ;
  3359. table = (void*) __get_free_pages(GFP_ATOMIC, order);
  3360. /*
  3361. * If bucketsize is not a power-of-two, we may free
  3362. * some pages at the end of hash table.
  3363. */
  3364. if (table) {
  3365. unsigned long alloc_end = (unsigned long)table +
  3366. (PAGE_SIZE << order);
  3367. unsigned long used = (unsigned long)table +
  3368. PAGE_ALIGN(size);
  3369. split_page(virt_to_page(table), order);
  3370. while (used < alloc_end) {
  3371. free_page(used);
  3372. used += PAGE_SIZE;
  3373. }
  3374. }
  3375. }
  3376. } while (!table && size > PAGE_SIZE && --log2qty);
  3377. if (!table)
  3378. panic("Failed to allocate %s hash table\n", tablename);
  3379. printk(KERN_INFO "%s hash table entries: %d (order: %d, %lu bytes)\n",
  3380. tablename,
  3381. (1U << log2qty),
  3382. ilog2(size) - PAGE_SHIFT,
  3383. size);
  3384. if (_hash_shift)
  3385. *_hash_shift = log2qty;
  3386. if (_hash_mask)
  3387. *_hash_mask = (1 << log2qty) - 1;
  3388. return table;
  3389. }
  3390. #ifdef CONFIG_OUT_OF_LINE_PFN_TO_PAGE
  3391. struct page *pfn_to_page(unsigned long pfn)
  3392. {
  3393. return __pfn_to_page(pfn);
  3394. }
  3395. unsigned long page_to_pfn(struct page *page)
  3396. {
  3397. return __page_to_pfn(page);
  3398. }
  3399. EXPORT_SYMBOL(pfn_to_page);
  3400. EXPORT_SYMBOL(page_to_pfn);
  3401. #endif /* CONFIG_OUT_OF_LINE_PFN_TO_PAGE */