page_alloc.c 89 KB

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