page_alloc.c 70 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845
  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/config.h>
  17. #include <linux/stddef.h>
  18. #include <linux/mm.h>
  19. #include <linux/swap.h>
  20. #include <linux/interrupt.h>
  21. #include <linux/pagemap.h>
  22. #include <linux/bootmem.h>
  23. #include <linux/compiler.h>
  24. #include <linux/kernel.h>
  25. #include <linux/module.h>
  26. #include <linux/suspend.h>
  27. #include <linux/pagevec.h>
  28. #include <linux/blkdev.h>
  29. #include <linux/slab.h>
  30. #include <linux/notifier.h>
  31. #include <linux/topology.h>
  32. #include <linux/sysctl.h>
  33. #include <linux/cpu.h>
  34. #include <linux/cpuset.h>
  35. #include <linux/memory_hotplug.h>
  36. #include <linux/nodemask.h>
  37. #include <linux/vmalloc.h>
  38. #include <linux/mempolicy.h>
  39. #include <asm/tlbflush.h>
  40. #include <asm/div64.h>
  41. #include "internal.h"
  42. /*
  43. * MCD - HACK: Find somewhere to initialize this EARLY, or make this
  44. * initializer cleaner
  45. */
  46. nodemask_t node_online_map __read_mostly = { { [0] = 1UL } };
  47. EXPORT_SYMBOL(node_online_map);
  48. nodemask_t node_possible_map __read_mostly = NODE_MASK_ALL;
  49. EXPORT_SYMBOL(node_possible_map);
  50. unsigned long totalram_pages __read_mostly;
  51. unsigned long totalhigh_pages __read_mostly;
  52. unsigned long totalreserve_pages __read_mostly;
  53. long nr_swap_pages;
  54. int percpu_pagelist_fraction;
  55. static void __free_pages_ok(struct page *page, unsigned int order);
  56. /*
  57. * results with 256, 32 in the lowmem_reserve sysctl:
  58. * 1G machine -> (16M dma, 800M-16M normal, 1G-800M high)
  59. * 1G machine -> (16M dma, 784M normal, 224M high)
  60. * NORMAL allocation will leave 784M/256 of ram reserved in the ZONE_DMA
  61. * HIGHMEM allocation will leave 224M/32 of ram reserved in ZONE_NORMAL
  62. * HIGHMEM allocation will (224M+784M)/256 of ram reserved in ZONE_DMA
  63. *
  64. * TBD: should special case ZONE_DMA32 machines here - in those we normally
  65. * don't need any ZONE_NORMAL reservation
  66. */
  67. int sysctl_lowmem_reserve_ratio[MAX_NR_ZONES-1] = { 256, 256, 32 };
  68. EXPORT_SYMBOL(totalram_pages);
  69. /*
  70. * Used by page_zone() to look up the address of the struct zone whose
  71. * id is encoded in the upper bits of page->flags
  72. */
  73. struct zone *zone_table[1 << ZONETABLE_SHIFT] __read_mostly;
  74. EXPORT_SYMBOL(zone_table);
  75. static char *zone_names[MAX_NR_ZONES] = { "DMA", "DMA32", "Normal", "HighMem" };
  76. int min_free_kbytes = 1024;
  77. unsigned long __initdata nr_kernel_pages;
  78. unsigned long __initdata nr_all_pages;
  79. #ifdef CONFIG_DEBUG_VM
  80. static int page_outside_zone_boundaries(struct zone *zone, struct page *page)
  81. {
  82. int ret = 0;
  83. unsigned seq;
  84. unsigned long pfn = page_to_pfn(page);
  85. do {
  86. seq = zone_span_seqbegin(zone);
  87. if (pfn >= zone->zone_start_pfn + zone->spanned_pages)
  88. ret = 1;
  89. else if (pfn < zone->zone_start_pfn)
  90. ret = 1;
  91. } while (zone_span_seqretry(zone, seq));
  92. return ret;
  93. }
  94. static int page_is_consistent(struct zone *zone, struct page *page)
  95. {
  96. #ifdef CONFIG_HOLES_IN_ZONE
  97. if (!pfn_valid(page_to_pfn(page)))
  98. return 0;
  99. #endif
  100. if (zone != page_zone(page))
  101. return 0;
  102. return 1;
  103. }
  104. /*
  105. * Temporary debugging check for pages not lying within a given zone.
  106. */
  107. static int bad_range(struct zone *zone, struct page *page)
  108. {
  109. if (page_outside_zone_boundaries(zone, page))
  110. return 1;
  111. if (!page_is_consistent(zone, page))
  112. return 1;
  113. return 0;
  114. }
  115. #else
  116. static inline int bad_range(struct zone *zone, struct page *page)
  117. {
  118. return 0;
  119. }
  120. #endif
  121. static void bad_page(struct page *page)
  122. {
  123. printk(KERN_EMERG "Bad page state in process '%s'\n"
  124. KERN_EMERG "page:%p flags:0x%0*lx mapping:%p mapcount:%d count:%d\n"
  125. KERN_EMERG "Trying to fix it up, but a reboot is needed\n"
  126. KERN_EMERG "Backtrace:\n",
  127. current->comm, page, (int)(2*sizeof(unsigned long)),
  128. (unsigned long)page->flags, page->mapping,
  129. page_mapcount(page), page_count(page));
  130. dump_stack();
  131. page->flags &= ~(1 << PG_lru |
  132. 1 << PG_private |
  133. 1 << PG_locked |
  134. 1 << PG_active |
  135. 1 << PG_dirty |
  136. 1 << PG_reclaim |
  137. 1 << PG_slab |
  138. 1 << PG_swapcache |
  139. 1 << PG_writeback |
  140. 1 << PG_buddy );
  141. set_page_count(page, 0);
  142. reset_page_mapcount(page);
  143. page->mapping = NULL;
  144. add_taint(TAINT_BAD_PAGE);
  145. }
  146. /*
  147. * Higher-order pages are called "compound pages". They are structured thusly:
  148. *
  149. * The first PAGE_SIZE page is called the "head page".
  150. *
  151. * The remaining PAGE_SIZE pages are called "tail pages".
  152. *
  153. * All pages have PG_compound set. All pages have their ->private pointing at
  154. * the head page (even the head page has this).
  155. *
  156. * The first tail page's ->lru.next holds the address of the compound page's
  157. * put_page() function. Its ->lru.prev holds the order of allocation.
  158. * This usage means that zero-order pages may not be compound.
  159. */
  160. static void free_compound_page(struct page *page)
  161. {
  162. __free_pages_ok(page, (unsigned long)page[1].lru.prev);
  163. }
  164. static void prep_compound_page(struct page *page, unsigned long order)
  165. {
  166. int i;
  167. int nr_pages = 1 << order;
  168. page[1].lru.next = (void *)free_compound_page; /* set dtor */
  169. page[1].lru.prev = (void *)order;
  170. for (i = 0; i < nr_pages; i++) {
  171. struct page *p = page + i;
  172. __SetPageCompound(p);
  173. set_page_private(p, (unsigned long)page);
  174. }
  175. }
  176. static void destroy_compound_page(struct page *page, unsigned long order)
  177. {
  178. int i;
  179. int nr_pages = 1 << order;
  180. if (unlikely((unsigned long)page[1].lru.prev != order))
  181. bad_page(page);
  182. for (i = 0; i < nr_pages; i++) {
  183. struct page *p = page + i;
  184. if (unlikely(!PageCompound(p) |
  185. (page_private(p) != (unsigned long)page)))
  186. bad_page(page);
  187. __ClearPageCompound(p);
  188. }
  189. }
  190. static inline void prep_zero_page(struct page *page, int order, gfp_t gfp_flags)
  191. {
  192. int i;
  193. BUG_ON((gfp_flags & (__GFP_WAIT | __GFP_HIGHMEM)) == __GFP_HIGHMEM);
  194. /*
  195. * clear_highpage() will use KM_USER0, so it's a bug to use __GFP_ZERO
  196. * and __GFP_HIGHMEM from hard or soft interrupt context.
  197. */
  198. BUG_ON((gfp_flags & __GFP_HIGHMEM) && in_interrupt());
  199. for (i = 0; i < (1 << order); i++)
  200. clear_highpage(page + i);
  201. }
  202. /*
  203. * function for dealing with page's order in buddy system.
  204. * zone->lock is already acquired when we use these.
  205. * So, we don't need atomic page->flags operations here.
  206. */
  207. static inline unsigned long page_order(struct page *page)
  208. {
  209. return page_private(page);
  210. }
  211. static inline void set_page_order(struct page *page, int order)
  212. {
  213. set_page_private(page, order);
  214. __SetPageBuddy(page);
  215. }
  216. static inline void rmv_page_order(struct page *page)
  217. {
  218. __ClearPageBuddy(page);
  219. set_page_private(page, 0);
  220. }
  221. /*
  222. * Locate the struct page for both the matching buddy in our
  223. * pair (buddy1) and the combined O(n+1) page they form (page).
  224. *
  225. * 1) Any buddy B1 will have an order O twin B2 which satisfies
  226. * the following equation:
  227. * B2 = B1 ^ (1 << O)
  228. * For example, if the starting buddy (buddy2) is #8 its order
  229. * 1 buddy is #10:
  230. * B2 = 8 ^ (1 << 1) = 8 ^ 2 = 10
  231. *
  232. * 2) Any buddy B will have an order O+1 parent P which
  233. * satisfies the following equation:
  234. * P = B & ~(1 << O)
  235. *
  236. * Assumption: *_mem_map is contigious at least up to MAX_ORDER
  237. */
  238. static inline struct page *
  239. __page_find_buddy(struct page *page, unsigned long page_idx, unsigned int order)
  240. {
  241. unsigned long buddy_idx = page_idx ^ (1 << order);
  242. return page + (buddy_idx - page_idx);
  243. }
  244. static inline unsigned long
  245. __find_combined_index(unsigned long page_idx, unsigned int order)
  246. {
  247. return (page_idx & ~(1 << order));
  248. }
  249. /*
  250. * This function checks whether a page is free && is the buddy
  251. * we can do coalesce a page and its buddy if
  252. * (a) the buddy is not in a hole &&
  253. * (b) the buddy is in the buddy system &&
  254. * (c) a page and its buddy have the same order.
  255. *
  256. * For recording whether a page is in the buddy system, we use PG_buddy.
  257. * Setting, clearing, and testing PG_buddy is serialized by zone->lock.
  258. *
  259. * For recording page's order, we use page_private(page).
  260. */
  261. static inline int page_is_buddy(struct page *page, int order)
  262. {
  263. #ifdef CONFIG_HOLES_IN_ZONE
  264. if (!pfn_valid(page_to_pfn(page)))
  265. return 0;
  266. #endif
  267. if (PageBuddy(page) && page_order(page) == order) {
  268. BUG_ON(page_count(page) != 0);
  269. return 1;
  270. }
  271. return 0;
  272. }
  273. /*
  274. * Freeing function for a buddy system allocator.
  275. *
  276. * The concept of a buddy system is to maintain direct-mapped table
  277. * (containing bit values) for memory blocks of various "orders".
  278. * The bottom level table contains the map for the smallest allocatable
  279. * units of memory (here, pages), and each level above it describes
  280. * pairs of units from the levels below, hence, "buddies".
  281. * At a high level, all that happens here is marking the table entry
  282. * at the bottom level available, and propagating the changes upward
  283. * as necessary, plus some accounting needed to play nicely with other
  284. * parts of the VM system.
  285. * At each level, we keep a list of pages, which are heads of continuous
  286. * free pages of length of (1 << order) and marked with PG_buddy. Page's
  287. * order is recorded in page_private(page) field.
  288. * So when we are allocating or freeing one, we can derive the state of the
  289. * other. That is, if we allocate a small block, and both were
  290. * free, the remainder of the region must be split into blocks.
  291. * If a block is freed, and its buddy is also free, then this
  292. * triggers coalescing into a block of larger size.
  293. *
  294. * -- wli
  295. */
  296. static inline void __free_one_page(struct page *page,
  297. struct zone *zone, unsigned int order)
  298. {
  299. unsigned long page_idx;
  300. int order_size = 1 << order;
  301. if (unlikely(PageCompound(page)))
  302. destroy_compound_page(page, order);
  303. page_idx = page_to_pfn(page) & ((1 << MAX_ORDER) - 1);
  304. BUG_ON(page_idx & (order_size - 1));
  305. BUG_ON(bad_range(zone, page));
  306. zone->free_pages += order_size;
  307. while (order < MAX_ORDER-1) {
  308. unsigned long combined_idx;
  309. struct free_area *area;
  310. struct page *buddy;
  311. buddy = __page_find_buddy(page, page_idx, order);
  312. if (!page_is_buddy(buddy, order))
  313. break; /* Move the buddy up one level. */
  314. list_del(&buddy->lru);
  315. area = zone->free_area + order;
  316. area->nr_free--;
  317. rmv_page_order(buddy);
  318. combined_idx = __find_combined_index(page_idx, order);
  319. page = page + (combined_idx - page_idx);
  320. page_idx = combined_idx;
  321. order++;
  322. }
  323. set_page_order(page, order);
  324. list_add(&page->lru, &zone->free_area[order].free_list);
  325. zone->free_area[order].nr_free++;
  326. }
  327. static inline int free_pages_check(struct page *page)
  328. {
  329. if (unlikely(page_mapcount(page) |
  330. (page->mapping != NULL) |
  331. (page_count(page) != 0) |
  332. (page->flags & (
  333. 1 << PG_lru |
  334. 1 << PG_private |
  335. 1 << PG_locked |
  336. 1 << PG_active |
  337. 1 << PG_reclaim |
  338. 1 << PG_slab |
  339. 1 << PG_swapcache |
  340. 1 << PG_writeback |
  341. 1 << PG_reserved |
  342. 1 << PG_buddy ))))
  343. bad_page(page);
  344. if (PageDirty(page))
  345. __ClearPageDirty(page);
  346. /*
  347. * For now, we report if PG_reserved was found set, but do not
  348. * clear it, and do not free the page. But we shall soon need
  349. * to do more, for when the ZERO_PAGE count wraps negative.
  350. */
  351. return PageReserved(page);
  352. }
  353. /*
  354. * Frees a list of pages.
  355. * Assumes all pages on list are in same zone, and of same order.
  356. * count is the number of pages to free.
  357. *
  358. * If the zone was previously in an "all pages pinned" state then look to
  359. * see if this freeing clears that state.
  360. *
  361. * And clear the zone's pages_scanned counter, to hold off the "all pages are
  362. * pinned" detection logic.
  363. */
  364. static void free_pages_bulk(struct zone *zone, int count,
  365. struct list_head *list, int order)
  366. {
  367. spin_lock(&zone->lock);
  368. zone->all_unreclaimable = 0;
  369. zone->pages_scanned = 0;
  370. while (count--) {
  371. struct page *page;
  372. BUG_ON(list_empty(list));
  373. page = list_entry(list->prev, struct page, lru);
  374. /* have to delete it as __free_one_page list manipulates */
  375. list_del(&page->lru);
  376. __free_one_page(page, zone, order);
  377. }
  378. spin_unlock(&zone->lock);
  379. }
  380. static void free_one_page(struct zone *zone, struct page *page, int order)
  381. {
  382. LIST_HEAD(list);
  383. list_add(&page->lru, &list);
  384. free_pages_bulk(zone, 1, &list, order);
  385. }
  386. static void __free_pages_ok(struct page *page, unsigned int order)
  387. {
  388. unsigned long flags;
  389. int i;
  390. int reserved = 0;
  391. arch_free_page(page, order);
  392. if (!PageHighMem(page))
  393. mutex_debug_check_no_locks_freed(page_address(page),
  394. PAGE_SIZE<<order);
  395. for (i = 0 ; i < (1 << order) ; ++i)
  396. reserved += free_pages_check(page + i);
  397. if (reserved)
  398. return;
  399. kernel_map_pages(page, 1 << order, 0);
  400. local_irq_save(flags);
  401. __mod_page_state(pgfree, 1 << order);
  402. free_one_page(page_zone(page), page, order);
  403. local_irq_restore(flags);
  404. }
  405. /*
  406. * permit the bootmem allocator to evade page validation on high-order frees
  407. */
  408. void fastcall __init __free_pages_bootmem(struct page *page, unsigned int order)
  409. {
  410. if (order == 0) {
  411. __ClearPageReserved(page);
  412. set_page_count(page, 0);
  413. set_page_refcounted(page);
  414. __free_page(page);
  415. } else {
  416. int loop;
  417. prefetchw(page);
  418. for (loop = 0; loop < BITS_PER_LONG; loop++) {
  419. struct page *p = &page[loop];
  420. if (loop + 1 < BITS_PER_LONG)
  421. prefetchw(p + 1);
  422. __ClearPageReserved(p);
  423. set_page_count(p, 0);
  424. }
  425. set_page_refcounted(page);
  426. __free_pages(page, order);
  427. }
  428. }
  429. /*
  430. * The order of subdivision here is critical for the IO subsystem.
  431. * Please do not alter this order without good reasons and regression
  432. * testing. Specifically, as large blocks of memory are subdivided,
  433. * the order in which smaller blocks are delivered depends on the order
  434. * they're subdivided in this function. This is the primary factor
  435. * influencing the order in which pages are delivered to the IO
  436. * subsystem according to empirical testing, and this is also justified
  437. * by considering the behavior of a buddy system containing a single
  438. * large block of memory acted on by a series of small allocations.
  439. * This behavior is a critical factor in sglist merging's success.
  440. *
  441. * -- wli
  442. */
  443. static inline void expand(struct zone *zone, struct page *page,
  444. int low, int high, struct free_area *area)
  445. {
  446. unsigned long size = 1 << high;
  447. while (high > low) {
  448. area--;
  449. high--;
  450. size >>= 1;
  451. BUG_ON(bad_range(zone, &page[size]));
  452. list_add(&page[size].lru, &area->free_list);
  453. area->nr_free++;
  454. set_page_order(&page[size], high);
  455. }
  456. }
  457. /*
  458. * This page is about to be returned from the page allocator
  459. */
  460. static int prep_new_page(struct page *page, int order, gfp_t gfp_flags)
  461. {
  462. if (unlikely(page_mapcount(page) |
  463. (page->mapping != NULL) |
  464. (page_count(page) != 0) |
  465. (page->flags & (
  466. 1 << PG_lru |
  467. 1 << PG_private |
  468. 1 << PG_locked |
  469. 1 << PG_active |
  470. 1 << PG_dirty |
  471. 1 << PG_reclaim |
  472. 1 << PG_slab |
  473. 1 << PG_swapcache |
  474. 1 << PG_writeback |
  475. 1 << PG_reserved |
  476. 1 << PG_buddy ))))
  477. bad_page(page);
  478. /*
  479. * For now, we report if PG_reserved was found set, but do not
  480. * clear it, and do not allocate the page: as a safety net.
  481. */
  482. if (PageReserved(page))
  483. return 1;
  484. page->flags &= ~(1 << PG_uptodate | 1 << PG_error |
  485. 1 << PG_referenced | 1 << PG_arch_1 |
  486. 1 << PG_checked | 1 << PG_mappedtodisk);
  487. set_page_private(page, 0);
  488. set_page_refcounted(page);
  489. kernel_map_pages(page, 1 << order, 1);
  490. if (gfp_flags & __GFP_ZERO)
  491. prep_zero_page(page, order, gfp_flags);
  492. if (order && (gfp_flags & __GFP_COMP))
  493. prep_compound_page(page, order);
  494. return 0;
  495. }
  496. /*
  497. * Do the hard work of removing an element from the buddy allocator.
  498. * Call me with the zone->lock already held.
  499. */
  500. static struct page *__rmqueue(struct zone *zone, unsigned int order)
  501. {
  502. struct free_area * area;
  503. unsigned int current_order;
  504. struct page *page;
  505. for (current_order = order; current_order < MAX_ORDER; ++current_order) {
  506. area = zone->free_area + current_order;
  507. if (list_empty(&area->free_list))
  508. continue;
  509. page = list_entry(area->free_list.next, struct page, lru);
  510. list_del(&page->lru);
  511. rmv_page_order(page);
  512. area->nr_free--;
  513. zone->free_pages -= 1UL << order;
  514. expand(zone, page, order, current_order, area);
  515. return page;
  516. }
  517. return NULL;
  518. }
  519. /*
  520. * Obtain a specified number of elements from the buddy allocator, all under
  521. * a single hold of the lock, for efficiency. Add them to the supplied list.
  522. * Returns the number of new pages which were placed at *list.
  523. */
  524. static int rmqueue_bulk(struct zone *zone, unsigned int order,
  525. unsigned long count, struct list_head *list)
  526. {
  527. int i;
  528. spin_lock(&zone->lock);
  529. for (i = 0; i < count; ++i) {
  530. struct page *page = __rmqueue(zone, order);
  531. if (unlikely(page == NULL))
  532. break;
  533. list_add_tail(&page->lru, list);
  534. }
  535. spin_unlock(&zone->lock);
  536. return i;
  537. }
  538. #ifdef CONFIG_NUMA
  539. /*
  540. * Called from the slab reaper to drain pagesets on a particular node that
  541. * belong to the currently executing processor.
  542. * Note that this function must be called with the thread pinned to
  543. * a single processor.
  544. */
  545. void drain_node_pages(int nodeid)
  546. {
  547. int i, z;
  548. unsigned long flags;
  549. for (z = 0; z < MAX_NR_ZONES; z++) {
  550. struct zone *zone = NODE_DATA(nodeid)->node_zones + z;
  551. struct per_cpu_pageset *pset;
  552. pset = zone_pcp(zone, smp_processor_id());
  553. for (i = 0; i < ARRAY_SIZE(pset->pcp); i++) {
  554. struct per_cpu_pages *pcp;
  555. pcp = &pset->pcp[i];
  556. if (pcp->count) {
  557. local_irq_save(flags);
  558. free_pages_bulk(zone, pcp->count, &pcp->list, 0);
  559. pcp->count = 0;
  560. local_irq_restore(flags);
  561. }
  562. }
  563. }
  564. }
  565. #endif
  566. #if defined(CONFIG_PM) || defined(CONFIG_HOTPLUG_CPU)
  567. static void __drain_pages(unsigned int cpu)
  568. {
  569. unsigned long flags;
  570. struct zone *zone;
  571. int i;
  572. for_each_zone(zone) {
  573. struct per_cpu_pageset *pset;
  574. pset = zone_pcp(zone, cpu);
  575. for (i = 0; i < ARRAY_SIZE(pset->pcp); i++) {
  576. struct per_cpu_pages *pcp;
  577. pcp = &pset->pcp[i];
  578. local_irq_save(flags);
  579. free_pages_bulk(zone, pcp->count, &pcp->list, 0);
  580. pcp->count = 0;
  581. local_irq_restore(flags);
  582. }
  583. }
  584. }
  585. #endif /* CONFIG_PM || CONFIG_HOTPLUG_CPU */
  586. #ifdef CONFIG_PM
  587. void mark_free_pages(struct zone *zone)
  588. {
  589. unsigned long zone_pfn, flags;
  590. int order;
  591. struct list_head *curr;
  592. if (!zone->spanned_pages)
  593. return;
  594. spin_lock_irqsave(&zone->lock, flags);
  595. for (zone_pfn = 0; zone_pfn < zone->spanned_pages; ++zone_pfn)
  596. ClearPageNosaveFree(pfn_to_page(zone_pfn + zone->zone_start_pfn));
  597. for (order = MAX_ORDER - 1; order >= 0; --order)
  598. list_for_each(curr, &zone->free_area[order].free_list) {
  599. unsigned long start_pfn, i;
  600. start_pfn = page_to_pfn(list_entry(curr, struct page, lru));
  601. for (i=0; i < (1<<order); i++)
  602. SetPageNosaveFree(pfn_to_page(start_pfn+i));
  603. }
  604. spin_unlock_irqrestore(&zone->lock, flags);
  605. }
  606. /*
  607. * Spill all of this CPU's per-cpu pages back into the buddy allocator.
  608. */
  609. void drain_local_pages(void)
  610. {
  611. unsigned long flags;
  612. local_irq_save(flags);
  613. __drain_pages(smp_processor_id());
  614. local_irq_restore(flags);
  615. }
  616. #endif /* CONFIG_PM */
  617. static void zone_statistics(struct zonelist *zonelist, struct zone *z, int cpu)
  618. {
  619. #ifdef CONFIG_NUMA
  620. pg_data_t *pg = z->zone_pgdat;
  621. pg_data_t *orig = zonelist->zones[0]->zone_pgdat;
  622. struct per_cpu_pageset *p;
  623. p = zone_pcp(z, cpu);
  624. if (pg == orig) {
  625. p->numa_hit++;
  626. } else {
  627. p->numa_miss++;
  628. zone_pcp(zonelist->zones[0], cpu)->numa_foreign++;
  629. }
  630. if (pg == NODE_DATA(numa_node_id()))
  631. p->local_node++;
  632. else
  633. p->other_node++;
  634. #endif
  635. }
  636. /*
  637. * Free a 0-order page
  638. */
  639. static void fastcall free_hot_cold_page(struct page *page, int cold)
  640. {
  641. struct zone *zone = page_zone(page);
  642. struct per_cpu_pages *pcp;
  643. unsigned long flags;
  644. arch_free_page(page, 0);
  645. if (PageAnon(page))
  646. page->mapping = NULL;
  647. if (free_pages_check(page))
  648. return;
  649. kernel_map_pages(page, 1, 0);
  650. pcp = &zone_pcp(zone, get_cpu())->pcp[cold];
  651. local_irq_save(flags);
  652. __inc_page_state(pgfree);
  653. list_add(&page->lru, &pcp->list);
  654. pcp->count++;
  655. if (pcp->count >= pcp->high) {
  656. free_pages_bulk(zone, pcp->batch, &pcp->list, 0);
  657. pcp->count -= pcp->batch;
  658. }
  659. local_irq_restore(flags);
  660. put_cpu();
  661. }
  662. void fastcall free_hot_page(struct page *page)
  663. {
  664. free_hot_cold_page(page, 0);
  665. }
  666. void fastcall free_cold_page(struct page *page)
  667. {
  668. free_hot_cold_page(page, 1);
  669. }
  670. /*
  671. * split_page takes a non-compound higher-order page, and splits it into
  672. * n (1<<order) sub-pages: page[0..n]
  673. * Each sub-page must be freed individually.
  674. *
  675. * Note: this is probably too low level an operation for use in drivers.
  676. * Please consult with lkml before using this in your driver.
  677. */
  678. void split_page(struct page *page, unsigned int order)
  679. {
  680. int i;
  681. BUG_ON(PageCompound(page));
  682. BUG_ON(!page_count(page));
  683. for (i = 1; i < (1 << order); i++)
  684. set_page_refcounted(page + i);
  685. }
  686. /*
  687. * Really, prep_compound_page() should be called from __rmqueue_bulk(). But
  688. * we cheat by calling it from here, in the order > 0 path. Saves a branch
  689. * or two.
  690. */
  691. static struct page *buffered_rmqueue(struct zonelist *zonelist,
  692. struct zone *zone, int order, gfp_t gfp_flags)
  693. {
  694. unsigned long flags;
  695. struct page *page;
  696. int cold = !!(gfp_flags & __GFP_COLD);
  697. int cpu;
  698. again:
  699. cpu = get_cpu();
  700. if (likely(order == 0)) {
  701. struct per_cpu_pages *pcp;
  702. pcp = &zone_pcp(zone, cpu)->pcp[cold];
  703. local_irq_save(flags);
  704. if (!pcp->count) {
  705. pcp->count += rmqueue_bulk(zone, 0,
  706. pcp->batch, &pcp->list);
  707. if (unlikely(!pcp->count))
  708. goto failed;
  709. }
  710. page = list_entry(pcp->list.next, struct page, lru);
  711. list_del(&page->lru);
  712. pcp->count--;
  713. } else {
  714. spin_lock_irqsave(&zone->lock, flags);
  715. page = __rmqueue(zone, order);
  716. spin_unlock(&zone->lock);
  717. if (!page)
  718. goto failed;
  719. }
  720. __mod_page_state_zone(zone, pgalloc, 1 << order);
  721. zone_statistics(zonelist, zone, cpu);
  722. local_irq_restore(flags);
  723. put_cpu();
  724. BUG_ON(bad_range(zone, page));
  725. if (prep_new_page(page, order, gfp_flags))
  726. goto again;
  727. return page;
  728. failed:
  729. local_irq_restore(flags);
  730. put_cpu();
  731. return NULL;
  732. }
  733. #define ALLOC_NO_WATERMARKS 0x01 /* don't check watermarks at all */
  734. #define ALLOC_WMARK_MIN 0x02 /* use pages_min watermark */
  735. #define ALLOC_WMARK_LOW 0x04 /* use pages_low watermark */
  736. #define ALLOC_WMARK_HIGH 0x08 /* use pages_high watermark */
  737. #define ALLOC_HARDER 0x10 /* try to alloc harder */
  738. #define ALLOC_HIGH 0x20 /* __GFP_HIGH set */
  739. #define ALLOC_CPUSET 0x40 /* check for correct cpuset */
  740. /*
  741. * Return 1 if free pages are above 'mark'. This takes into account the order
  742. * of the allocation.
  743. */
  744. int zone_watermark_ok(struct zone *z, int order, unsigned long mark,
  745. int classzone_idx, int alloc_flags)
  746. {
  747. /* free_pages my go negative - that's OK */
  748. long min = mark, free_pages = z->free_pages - (1 << order) + 1;
  749. int o;
  750. if (alloc_flags & ALLOC_HIGH)
  751. min -= min / 2;
  752. if (alloc_flags & ALLOC_HARDER)
  753. min -= min / 4;
  754. if (free_pages <= min + z->lowmem_reserve[classzone_idx])
  755. return 0;
  756. for (o = 0; o < order; o++) {
  757. /* At the next order, this order's pages become unavailable */
  758. free_pages -= z->free_area[o].nr_free << o;
  759. /* Require fewer higher order pages to be free */
  760. min >>= 1;
  761. if (free_pages <= min)
  762. return 0;
  763. }
  764. return 1;
  765. }
  766. /*
  767. * get_page_from_freeliest goes through the zonelist trying to allocate
  768. * a page.
  769. */
  770. static struct page *
  771. get_page_from_freelist(gfp_t gfp_mask, unsigned int order,
  772. struct zonelist *zonelist, int alloc_flags)
  773. {
  774. struct zone **z = zonelist->zones;
  775. struct page *page = NULL;
  776. int classzone_idx = zone_idx(*z);
  777. /*
  778. * Go through the zonelist once, looking for a zone with enough free.
  779. * See also cpuset_zone_allowed() comment in kernel/cpuset.c.
  780. */
  781. do {
  782. if ((alloc_flags & ALLOC_CPUSET) &&
  783. !cpuset_zone_allowed(*z, gfp_mask))
  784. continue;
  785. if (!(alloc_flags & ALLOC_NO_WATERMARKS)) {
  786. unsigned long mark;
  787. if (alloc_flags & ALLOC_WMARK_MIN)
  788. mark = (*z)->pages_min;
  789. else if (alloc_flags & ALLOC_WMARK_LOW)
  790. mark = (*z)->pages_low;
  791. else
  792. mark = (*z)->pages_high;
  793. if (!zone_watermark_ok(*z, order, mark,
  794. classzone_idx, alloc_flags))
  795. if (!zone_reclaim_mode ||
  796. !zone_reclaim(*z, gfp_mask, order))
  797. continue;
  798. }
  799. page = buffered_rmqueue(zonelist, *z, order, gfp_mask);
  800. if (page) {
  801. break;
  802. }
  803. } while (*(++z) != NULL);
  804. return page;
  805. }
  806. /*
  807. * This is the 'heart' of the zoned buddy allocator.
  808. */
  809. struct page * fastcall
  810. __alloc_pages(gfp_t gfp_mask, unsigned int order,
  811. struct zonelist *zonelist)
  812. {
  813. const gfp_t wait = gfp_mask & __GFP_WAIT;
  814. struct zone **z;
  815. struct page *page;
  816. struct reclaim_state reclaim_state;
  817. struct task_struct *p = current;
  818. int do_retry;
  819. int alloc_flags;
  820. int did_some_progress;
  821. might_sleep_if(wait);
  822. restart:
  823. z = zonelist->zones; /* the list of zones suitable for gfp_mask */
  824. if (unlikely(*z == NULL)) {
  825. /* Should this ever happen?? */
  826. return NULL;
  827. }
  828. page = get_page_from_freelist(gfp_mask|__GFP_HARDWALL, order,
  829. zonelist, ALLOC_WMARK_LOW|ALLOC_CPUSET);
  830. if (page)
  831. goto got_pg;
  832. do {
  833. if (cpuset_zone_allowed(*z, gfp_mask|__GFP_HARDWALL))
  834. wakeup_kswapd(*z, order);
  835. } while (*(++z));
  836. /*
  837. * OK, we're below the kswapd watermark and have kicked background
  838. * reclaim. Now things get more complex, so set up alloc_flags according
  839. * to how we want to proceed.
  840. *
  841. * The caller may dip into page reserves a bit more if the caller
  842. * cannot run direct reclaim, or if the caller has realtime scheduling
  843. * policy or is asking for __GFP_HIGH memory. GFP_ATOMIC requests will
  844. * set both ALLOC_HARDER (!wait) and ALLOC_HIGH (__GFP_HIGH).
  845. */
  846. alloc_flags = ALLOC_WMARK_MIN;
  847. if ((unlikely(rt_task(p)) && !in_interrupt()) || !wait)
  848. alloc_flags |= ALLOC_HARDER;
  849. if (gfp_mask & __GFP_HIGH)
  850. alloc_flags |= ALLOC_HIGH;
  851. if (wait)
  852. alloc_flags |= ALLOC_CPUSET;
  853. /*
  854. * Go through the zonelist again. Let __GFP_HIGH and allocations
  855. * coming from realtime tasks go deeper into reserves.
  856. *
  857. * This is the last chance, in general, before the goto nopage.
  858. * Ignore cpuset if GFP_ATOMIC (!wait) rather than fail alloc.
  859. * See also cpuset_zone_allowed() comment in kernel/cpuset.c.
  860. */
  861. page = get_page_from_freelist(gfp_mask, order, zonelist, alloc_flags);
  862. if (page)
  863. goto got_pg;
  864. /* This allocation should allow future memory freeing. */
  865. if (((p->flags & PF_MEMALLOC) || unlikely(test_thread_flag(TIF_MEMDIE)))
  866. && !in_interrupt()) {
  867. if (!(gfp_mask & __GFP_NOMEMALLOC)) {
  868. nofail_alloc:
  869. /* go through the zonelist yet again, ignoring mins */
  870. page = get_page_from_freelist(gfp_mask, order,
  871. zonelist, ALLOC_NO_WATERMARKS);
  872. if (page)
  873. goto got_pg;
  874. if (gfp_mask & __GFP_NOFAIL) {
  875. blk_congestion_wait(WRITE, HZ/50);
  876. goto nofail_alloc;
  877. }
  878. }
  879. goto nopage;
  880. }
  881. /* Atomic allocations - we can't balance anything */
  882. if (!wait)
  883. goto nopage;
  884. rebalance:
  885. cond_resched();
  886. /* We now go into synchronous reclaim */
  887. cpuset_memory_pressure_bump();
  888. p->flags |= PF_MEMALLOC;
  889. reclaim_state.reclaimed_slab = 0;
  890. p->reclaim_state = &reclaim_state;
  891. did_some_progress = try_to_free_pages(zonelist->zones, gfp_mask);
  892. p->reclaim_state = NULL;
  893. p->flags &= ~PF_MEMALLOC;
  894. cond_resched();
  895. if (likely(did_some_progress)) {
  896. page = get_page_from_freelist(gfp_mask, order,
  897. zonelist, alloc_flags);
  898. if (page)
  899. goto got_pg;
  900. } else if ((gfp_mask & __GFP_FS) && !(gfp_mask & __GFP_NORETRY)) {
  901. /*
  902. * Go through the zonelist yet one more time, keep
  903. * very high watermark here, this is only to catch
  904. * a parallel oom killing, we must fail if we're still
  905. * under heavy pressure.
  906. */
  907. page = get_page_from_freelist(gfp_mask|__GFP_HARDWALL, order,
  908. zonelist, ALLOC_WMARK_HIGH|ALLOC_CPUSET);
  909. if (page)
  910. goto got_pg;
  911. out_of_memory(zonelist, gfp_mask, order);
  912. goto restart;
  913. }
  914. /*
  915. * Don't let big-order allocations loop unless the caller explicitly
  916. * requests that. Wait for some write requests to complete then retry.
  917. *
  918. * In this implementation, __GFP_REPEAT means __GFP_NOFAIL for order
  919. * <= 3, but that may not be true in other implementations.
  920. */
  921. do_retry = 0;
  922. if (!(gfp_mask & __GFP_NORETRY)) {
  923. if ((order <= 3) || (gfp_mask & __GFP_REPEAT))
  924. do_retry = 1;
  925. if (gfp_mask & __GFP_NOFAIL)
  926. do_retry = 1;
  927. }
  928. if (do_retry) {
  929. blk_congestion_wait(WRITE, HZ/50);
  930. goto rebalance;
  931. }
  932. nopage:
  933. if (!(gfp_mask & __GFP_NOWARN) && printk_ratelimit()) {
  934. printk(KERN_WARNING "%s: page allocation failure."
  935. " order:%d, mode:0x%x\n",
  936. p->comm, order, gfp_mask);
  937. dump_stack();
  938. show_mem();
  939. }
  940. got_pg:
  941. return page;
  942. }
  943. EXPORT_SYMBOL(__alloc_pages);
  944. /*
  945. * Common helper functions.
  946. */
  947. fastcall unsigned long __get_free_pages(gfp_t gfp_mask, unsigned int order)
  948. {
  949. struct page * page;
  950. page = alloc_pages(gfp_mask, order);
  951. if (!page)
  952. return 0;
  953. return (unsigned long) page_address(page);
  954. }
  955. EXPORT_SYMBOL(__get_free_pages);
  956. fastcall unsigned long get_zeroed_page(gfp_t gfp_mask)
  957. {
  958. struct page * page;
  959. /*
  960. * get_zeroed_page() returns a 32-bit address, which cannot represent
  961. * a highmem page
  962. */
  963. BUG_ON((gfp_mask & __GFP_HIGHMEM) != 0);
  964. page = alloc_pages(gfp_mask | __GFP_ZERO, 0);
  965. if (page)
  966. return (unsigned long) page_address(page);
  967. return 0;
  968. }
  969. EXPORT_SYMBOL(get_zeroed_page);
  970. void __pagevec_free(struct pagevec *pvec)
  971. {
  972. int i = pagevec_count(pvec);
  973. while (--i >= 0)
  974. free_hot_cold_page(pvec->pages[i], pvec->cold);
  975. }
  976. fastcall void __free_pages(struct page *page, unsigned int order)
  977. {
  978. if (put_page_testzero(page)) {
  979. if (order == 0)
  980. free_hot_page(page);
  981. else
  982. __free_pages_ok(page, order);
  983. }
  984. }
  985. EXPORT_SYMBOL(__free_pages);
  986. fastcall void free_pages(unsigned long addr, unsigned int order)
  987. {
  988. if (addr != 0) {
  989. BUG_ON(!virt_addr_valid((void *)addr));
  990. __free_pages(virt_to_page((void *)addr), order);
  991. }
  992. }
  993. EXPORT_SYMBOL(free_pages);
  994. /*
  995. * Total amount of free (allocatable) RAM:
  996. */
  997. unsigned int nr_free_pages(void)
  998. {
  999. unsigned int sum = 0;
  1000. struct zone *zone;
  1001. for_each_zone(zone)
  1002. sum += zone->free_pages;
  1003. return sum;
  1004. }
  1005. EXPORT_SYMBOL(nr_free_pages);
  1006. #ifdef CONFIG_NUMA
  1007. unsigned int nr_free_pages_pgdat(pg_data_t *pgdat)
  1008. {
  1009. unsigned int i, sum = 0;
  1010. for (i = 0; i < MAX_NR_ZONES; i++)
  1011. sum += pgdat->node_zones[i].free_pages;
  1012. return sum;
  1013. }
  1014. #endif
  1015. static unsigned int nr_free_zone_pages(int offset)
  1016. {
  1017. /* Just pick one node, since fallback list is circular */
  1018. pg_data_t *pgdat = NODE_DATA(numa_node_id());
  1019. unsigned int sum = 0;
  1020. struct zonelist *zonelist = pgdat->node_zonelists + offset;
  1021. struct zone **zonep = zonelist->zones;
  1022. struct zone *zone;
  1023. for (zone = *zonep++; zone; zone = *zonep++) {
  1024. unsigned long size = zone->present_pages;
  1025. unsigned long high = zone->pages_high;
  1026. if (size > high)
  1027. sum += size - high;
  1028. }
  1029. return sum;
  1030. }
  1031. /*
  1032. * Amount of free RAM allocatable within ZONE_DMA and ZONE_NORMAL
  1033. */
  1034. unsigned int nr_free_buffer_pages(void)
  1035. {
  1036. return nr_free_zone_pages(gfp_zone(GFP_USER));
  1037. }
  1038. /*
  1039. * Amount of free RAM allocatable within all zones
  1040. */
  1041. unsigned int nr_free_pagecache_pages(void)
  1042. {
  1043. return nr_free_zone_pages(gfp_zone(GFP_HIGHUSER));
  1044. }
  1045. #ifdef CONFIG_HIGHMEM
  1046. unsigned int nr_free_highpages (void)
  1047. {
  1048. pg_data_t *pgdat;
  1049. unsigned int pages = 0;
  1050. for_each_online_pgdat(pgdat)
  1051. pages += pgdat->node_zones[ZONE_HIGHMEM].free_pages;
  1052. return pages;
  1053. }
  1054. #endif
  1055. #ifdef CONFIG_NUMA
  1056. static void show_node(struct zone *zone)
  1057. {
  1058. printk("Node %d ", zone->zone_pgdat->node_id);
  1059. }
  1060. #else
  1061. #define show_node(zone) do { } while (0)
  1062. #endif
  1063. /*
  1064. * Accumulate the page_state information across all CPUs.
  1065. * The result is unavoidably approximate - it can change
  1066. * during and after execution of this function.
  1067. */
  1068. static DEFINE_PER_CPU(struct page_state, page_states) = {0};
  1069. atomic_t nr_pagecache = ATOMIC_INIT(0);
  1070. EXPORT_SYMBOL(nr_pagecache);
  1071. #ifdef CONFIG_SMP
  1072. DEFINE_PER_CPU(long, nr_pagecache_local) = 0;
  1073. #endif
  1074. static void __get_page_state(struct page_state *ret, int nr, cpumask_t *cpumask)
  1075. {
  1076. unsigned cpu;
  1077. memset(ret, 0, nr * sizeof(unsigned long));
  1078. cpus_and(*cpumask, *cpumask, cpu_online_map);
  1079. for_each_cpu_mask(cpu, *cpumask) {
  1080. unsigned long *in;
  1081. unsigned long *out;
  1082. unsigned off;
  1083. unsigned next_cpu;
  1084. in = (unsigned long *)&per_cpu(page_states, cpu);
  1085. next_cpu = next_cpu(cpu, *cpumask);
  1086. if (likely(next_cpu < NR_CPUS))
  1087. prefetch(&per_cpu(page_states, next_cpu));
  1088. out = (unsigned long *)ret;
  1089. for (off = 0; off < nr; off++)
  1090. *out++ += *in++;
  1091. }
  1092. }
  1093. void get_page_state_node(struct page_state *ret, int node)
  1094. {
  1095. int nr;
  1096. cpumask_t mask = node_to_cpumask(node);
  1097. nr = offsetof(struct page_state, GET_PAGE_STATE_LAST);
  1098. nr /= sizeof(unsigned long);
  1099. __get_page_state(ret, nr+1, &mask);
  1100. }
  1101. void get_page_state(struct page_state *ret)
  1102. {
  1103. int nr;
  1104. cpumask_t mask = CPU_MASK_ALL;
  1105. nr = offsetof(struct page_state, GET_PAGE_STATE_LAST);
  1106. nr /= sizeof(unsigned long);
  1107. __get_page_state(ret, nr + 1, &mask);
  1108. }
  1109. void get_full_page_state(struct page_state *ret)
  1110. {
  1111. cpumask_t mask = CPU_MASK_ALL;
  1112. __get_page_state(ret, sizeof(*ret) / sizeof(unsigned long), &mask);
  1113. }
  1114. unsigned long read_page_state_offset(unsigned long offset)
  1115. {
  1116. unsigned long ret = 0;
  1117. int cpu;
  1118. for_each_online_cpu(cpu) {
  1119. unsigned long in;
  1120. in = (unsigned long)&per_cpu(page_states, cpu) + offset;
  1121. ret += *((unsigned long *)in);
  1122. }
  1123. return ret;
  1124. }
  1125. void __mod_page_state_offset(unsigned long offset, unsigned long delta)
  1126. {
  1127. void *ptr;
  1128. ptr = &__get_cpu_var(page_states);
  1129. *(unsigned long *)(ptr + offset) += delta;
  1130. }
  1131. EXPORT_SYMBOL(__mod_page_state_offset);
  1132. void mod_page_state_offset(unsigned long offset, unsigned long delta)
  1133. {
  1134. unsigned long flags;
  1135. void *ptr;
  1136. local_irq_save(flags);
  1137. ptr = &__get_cpu_var(page_states);
  1138. *(unsigned long *)(ptr + offset) += delta;
  1139. local_irq_restore(flags);
  1140. }
  1141. EXPORT_SYMBOL(mod_page_state_offset);
  1142. void __get_zone_counts(unsigned long *active, unsigned long *inactive,
  1143. unsigned long *free, struct pglist_data *pgdat)
  1144. {
  1145. struct zone *zones = pgdat->node_zones;
  1146. int i;
  1147. *active = 0;
  1148. *inactive = 0;
  1149. *free = 0;
  1150. for (i = 0; i < MAX_NR_ZONES; i++) {
  1151. *active += zones[i].nr_active;
  1152. *inactive += zones[i].nr_inactive;
  1153. *free += zones[i].free_pages;
  1154. }
  1155. }
  1156. void get_zone_counts(unsigned long *active,
  1157. unsigned long *inactive, unsigned long *free)
  1158. {
  1159. struct pglist_data *pgdat;
  1160. *active = 0;
  1161. *inactive = 0;
  1162. *free = 0;
  1163. for_each_online_pgdat(pgdat) {
  1164. unsigned long l, m, n;
  1165. __get_zone_counts(&l, &m, &n, pgdat);
  1166. *active += l;
  1167. *inactive += m;
  1168. *free += n;
  1169. }
  1170. }
  1171. void si_meminfo(struct sysinfo *val)
  1172. {
  1173. val->totalram = totalram_pages;
  1174. val->sharedram = 0;
  1175. val->freeram = nr_free_pages();
  1176. val->bufferram = nr_blockdev_pages();
  1177. #ifdef CONFIG_HIGHMEM
  1178. val->totalhigh = totalhigh_pages;
  1179. val->freehigh = nr_free_highpages();
  1180. #else
  1181. val->totalhigh = 0;
  1182. val->freehigh = 0;
  1183. #endif
  1184. val->mem_unit = PAGE_SIZE;
  1185. }
  1186. EXPORT_SYMBOL(si_meminfo);
  1187. #ifdef CONFIG_NUMA
  1188. void si_meminfo_node(struct sysinfo *val, int nid)
  1189. {
  1190. pg_data_t *pgdat = NODE_DATA(nid);
  1191. val->totalram = pgdat->node_present_pages;
  1192. val->freeram = nr_free_pages_pgdat(pgdat);
  1193. val->totalhigh = pgdat->node_zones[ZONE_HIGHMEM].present_pages;
  1194. val->freehigh = pgdat->node_zones[ZONE_HIGHMEM].free_pages;
  1195. val->mem_unit = PAGE_SIZE;
  1196. }
  1197. #endif
  1198. #define K(x) ((x) << (PAGE_SHIFT-10))
  1199. /*
  1200. * Show free area list (used inside shift_scroll-lock stuff)
  1201. * We also calculate the percentage fragmentation. We do this by counting the
  1202. * memory on each free list with the exception of the first item on the list.
  1203. */
  1204. void show_free_areas(void)
  1205. {
  1206. struct page_state ps;
  1207. int cpu, temperature;
  1208. unsigned long active;
  1209. unsigned long inactive;
  1210. unsigned long free;
  1211. struct zone *zone;
  1212. for_each_zone(zone) {
  1213. show_node(zone);
  1214. printk("%s per-cpu:", zone->name);
  1215. if (!populated_zone(zone)) {
  1216. printk(" empty\n");
  1217. continue;
  1218. } else
  1219. printk("\n");
  1220. for_each_online_cpu(cpu) {
  1221. struct per_cpu_pageset *pageset;
  1222. pageset = zone_pcp(zone, cpu);
  1223. for (temperature = 0; temperature < 2; temperature++)
  1224. printk("cpu %d %s: high %d, batch %d used:%d\n",
  1225. cpu,
  1226. temperature ? "cold" : "hot",
  1227. pageset->pcp[temperature].high,
  1228. pageset->pcp[temperature].batch,
  1229. pageset->pcp[temperature].count);
  1230. }
  1231. }
  1232. get_page_state(&ps);
  1233. get_zone_counts(&active, &inactive, &free);
  1234. printk("Free pages: %11ukB (%ukB HighMem)\n",
  1235. K(nr_free_pages()),
  1236. K(nr_free_highpages()));
  1237. printk("Active:%lu inactive:%lu dirty:%lu writeback:%lu "
  1238. "unstable:%lu free:%u slab:%lu mapped:%lu pagetables:%lu\n",
  1239. active,
  1240. inactive,
  1241. ps.nr_dirty,
  1242. ps.nr_writeback,
  1243. ps.nr_unstable,
  1244. nr_free_pages(),
  1245. ps.nr_slab,
  1246. ps.nr_mapped,
  1247. ps.nr_page_table_pages);
  1248. for_each_zone(zone) {
  1249. int i;
  1250. show_node(zone);
  1251. printk("%s"
  1252. " free:%lukB"
  1253. " min:%lukB"
  1254. " low:%lukB"
  1255. " high:%lukB"
  1256. " active:%lukB"
  1257. " inactive:%lukB"
  1258. " present:%lukB"
  1259. " pages_scanned:%lu"
  1260. " all_unreclaimable? %s"
  1261. "\n",
  1262. zone->name,
  1263. K(zone->free_pages),
  1264. K(zone->pages_min),
  1265. K(zone->pages_low),
  1266. K(zone->pages_high),
  1267. K(zone->nr_active),
  1268. K(zone->nr_inactive),
  1269. K(zone->present_pages),
  1270. zone->pages_scanned,
  1271. (zone->all_unreclaimable ? "yes" : "no")
  1272. );
  1273. printk("lowmem_reserve[]:");
  1274. for (i = 0; i < MAX_NR_ZONES; i++)
  1275. printk(" %lu", zone->lowmem_reserve[i]);
  1276. printk("\n");
  1277. }
  1278. for_each_zone(zone) {
  1279. unsigned long nr, flags, order, total = 0;
  1280. show_node(zone);
  1281. printk("%s: ", zone->name);
  1282. if (!populated_zone(zone)) {
  1283. printk("empty\n");
  1284. continue;
  1285. }
  1286. spin_lock_irqsave(&zone->lock, flags);
  1287. for (order = 0; order < MAX_ORDER; order++) {
  1288. nr = zone->free_area[order].nr_free;
  1289. total += nr << order;
  1290. printk("%lu*%lukB ", nr, K(1UL) << order);
  1291. }
  1292. spin_unlock_irqrestore(&zone->lock, flags);
  1293. printk("= %lukB\n", K(total));
  1294. }
  1295. show_swap_cache_info();
  1296. }
  1297. /*
  1298. * Builds allocation fallback zone lists.
  1299. *
  1300. * Add all populated zones of a node to the zonelist.
  1301. */
  1302. static int __init build_zonelists_node(pg_data_t *pgdat,
  1303. struct zonelist *zonelist, int nr_zones, int zone_type)
  1304. {
  1305. struct zone *zone;
  1306. BUG_ON(zone_type > ZONE_HIGHMEM);
  1307. do {
  1308. zone = pgdat->node_zones + zone_type;
  1309. if (populated_zone(zone)) {
  1310. #ifndef CONFIG_HIGHMEM
  1311. BUG_ON(zone_type > ZONE_NORMAL);
  1312. #endif
  1313. zonelist->zones[nr_zones++] = zone;
  1314. check_highest_zone(zone_type);
  1315. }
  1316. zone_type--;
  1317. } while (zone_type >= 0);
  1318. return nr_zones;
  1319. }
  1320. static inline int highest_zone(int zone_bits)
  1321. {
  1322. int res = ZONE_NORMAL;
  1323. if (zone_bits & (__force int)__GFP_HIGHMEM)
  1324. res = ZONE_HIGHMEM;
  1325. if (zone_bits & (__force int)__GFP_DMA32)
  1326. res = ZONE_DMA32;
  1327. if (zone_bits & (__force int)__GFP_DMA)
  1328. res = ZONE_DMA;
  1329. return res;
  1330. }
  1331. #ifdef CONFIG_NUMA
  1332. #define MAX_NODE_LOAD (num_online_nodes())
  1333. static int __initdata node_load[MAX_NUMNODES];
  1334. /**
  1335. * find_next_best_node - find the next node that should appear in a given node's fallback list
  1336. * @node: node whose fallback list we're appending
  1337. * @used_node_mask: nodemask_t of already used nodes
  1338. *
  1339. * We use a number of factors to determine which is the next node that should
  1340. * appear on a given node's fallback list. The node should not have appeared
  1341. * already in @node's fallback list, and it should be the next closest node
  1342. * according to the distance array (which contains arbitrary distance values
  1343. * from each node to each node in the system), and should also prefer nodes
  1344. * with no CPUs, since presumably they'll have very little allocation pressure
  1345. * on them otherwise.
  1346. * It returns -1 if no node is found.
  1347. */
  1348. static int __init find_next_best_node(int node, nodemask_t *used_node_mask)
  1349. {
  1350. int n, val;
  1351. int min_val = INT_MAX;
  1352. int best_node = -1;
  1353. /* Use the local node if we haven't already */
  1354. if (!node_isset(node, *used_node_mask)) {
  1355. node_set(node, *used_node_mask);
  1356. return node;
  1357. }
  1358. for_each_online_node(n) {
  1359. cpumask_t tmp;
  1360. /* Don't want a node to appear more than once */
  1361. if (node_isset(n, *used_node_mask))
  1362. continue;
  1363. /* Use the distance array to find the distance */
  1364. val = node_distance(node, n);
  1365. /* Penalize nodes under us ("prefer the next node") */
  1366. val += (n < node);
  1367. /* Give preference to headless and unused nodes */
  1368. tmp = node_to_cpumask(n);
  1369. if (!cpus_empty(tmp))
  1370. val += PENALTY_FOR_NODE_WITH_CPUS;
  1371. /* Slight preference for less loaded node */
  1372. val *= (MAX_NODE_LOAD*MAX_NUMNODES);
  1373. val += node_load[n];
  1374. if (val < min_val) {
  1375. min_val = val;
  1376. best_node = n;
  1377. }
  1378. }
  1379. if (best_node >= 0)
  1380. node_set(best_node, *used_node_mask);
  1381. return best_node;
  1382. }
  1383. static void __init build_zonelists(pg_data_t *pgdat)
  1384. {
  1385. int i, j, k, node, local_node;
  1386. int prev_node, load;
  1387. struct zonelist *zonelist;
  1388. nodemask_t used_mask;
  1389. /* initialize zonelists */
  1390. for (i = 0; i < GFP_ZONETYPES; i++) {
  1391. zonelist = pgdat->node_zonelists + i;
  1392. zonelist->zones[0] = NULL;
  1393. }
  1394. /* NUMA-aware ordering of nodes */
  1395. local_node = pgdat->node_id;
  1396. load = num_online_nodes();
  1397. prev_node = local_node;
  1398. nodes_clear(used_mask);
  1399. while ((node = find_next_best_node(local_node, &used_mask)) >= 0) {
  1400. int distance = node_distance(local_node, node);
  1401. /*
  1402. * If another node is sufficiently far away then it is better
  1403. * to reclaim pages in a zone before going off node.
  1404. */
  1405. if (distance > RECLAIM_DISTANCE)
  1406. zone_reclaim_mode = 1;
  1407. /*
  1408. * We don't want to pressure a particular node.
  1409. * So adding penalty to the first node in same
  1410. * distance group to make it round-robin.
  1411. */
  1412. if (distance != node_distance(local_node, prev_node))
  1413. node_load[node] += load;
  1414. prev_node = node;
  1415. load--;
  1416. for (i = 0; i < GFP_ZONETYPES; i++) {
  1417. zonelist = pgdat->node_zonelists + i;
  1418. for (j = 0; zonelist->zones[j] != NULL; j++);
  1419. k = highest_zone(i);
  1420. j = build_zonelists_node(NODE_DATA(node), zonelist, j, k);
  1421. zonelist->zones[j] = NULL;
  1422. }
  1423. }
  1424. }
  1425. #else /* CONFIG_NUMA */
  1426. static void __init build_zonelists(pg_data_t *pgdat)
  1427. {
  1428. int i, j, k, node, local_node;
  1429. local_node = pgdat->node_id;
  1430. for (i = 0; i < GFP_ZONETYPES; i++) {
  1431. struct zonelist *zonelist;
  1432. zonelist = pgdat->node_zonelists + i;
  1433. j = 0;
  1434. k = highest_zone(i);
  1435. j = build_zonelists_node(pgdat, zonelist, j, k);
  1436. /*
  1437. * Now we build the zonelist so that it contains the zones
  1438. * of all the other nodes.
  1439. * We don't want to pressure a particular node, so when
  1440. * building the zones for node N, we make sure that the
  1441. * zones coming right after the local ones are those from
  1442. * node N+1 (modulo N)
  1443. */
  1444. for (node = local_node + 1; node < MAX_NUMNODES; node++) {
  1445. if (!node_online(node))
  1446. continue;
  1447. j = build_zonelists_node(NODE_DATA(node), zonelist, j, k);
  1448. }
  1449. for (node = 0; node < local_node; node++) {
  1450. if (!node_online(node))
  1451. continue;
  1452. j = build_zonelists_node(NODE_DATA(node), zonelist, j, k);
  1453. }
  1454. zonelist->zones[j] = NULL;
  1455. }
  1456. }
  1457. #endif /* CONFIG_NUMA */
  1458. void __init build_all_zonelists(void)
  1459. {
  1460. int i;
  1461. for_each_online_node(i)
  1462. build_zonelists(NODE_DATA(i));
  1463. printk("Built %i zonelists\n", num_online_nodes());
  1464. cpuset_init_current_mems_allowed();
  1465. }
  1466. /*
  1467. * Helper functions to size the waitqueue hash table.
  1468. * Essentially these want to choose hash table sizes sufficiently
  1469. * large so that collisions trying to wait on pages are rare.
  1470. * But in fact, the number of active page waitqueues on typical
  1471. * systems is ridiculously low, less than 200. So this is even
  1472. * conservative, even though it seems large.
  1473. *
  1474. * The constant PAGES_PER_WAITQUEUE specifies the ratio of pages to
  1475. * waitqueues, i.e. the size of the waitq table given the number of pages.
  1476. */
  1477. #define PAGES_PER_WAITQUEUE 256
  1478. static inline unsigned long wait_table_size(unsigned long pages)
  1479. {
  1480. unsigned long size = 1;
  1481. pages /= PAGES_PER_WAITQUEUE;
  1482. while (size < pages)
  1483. size <<= 1;
  1484. /*
  1485. * Once we have dozens or even hundreds of threads sleeping
  1486. * on IO we've got bigger problems than wait queue collision.
  1487. * Limit the size of the wait table to a reasonable size.
  1488. */
  1489. size = min(size, 4096UL);
  1490. return max(size, 4UL);
  1491. }
  1492. /*
  1493. * This is an integer logarithm so that shifts can be used later
  1494. * to extract the more random high bits from the multiplicative
  1495. * hash function before the remainder is taken.
  1496. */
  1497. static inline unsigned long wait_table_bits(unsigned long size)
  1498. {
  1499. return ffz(~size);
  1500. }
  1501. #define LONG_ALIGN(x) (((x)+(sizeof(long))-1)&~((sizeof(long))-1))
  1502. static void __init calculate_zone_totalpages(struct pglist_data *pgdat,
  1503. unsigned long *zones_size, unsigned long *zholes_size)
  1504. {
  1505. unsigned long realtotalpages, totalpages = 0;
  1506. int i;
  1507. for (i = 0; i < MAX_NR_ZONES; i++)
  1508. totalpages += zones_size[i];
  1509. pgdat->node_spanned_pages = totalpages;
  1510. realtotalpages = totalpages;
  1511. if (zholes_size)
  1512. for (i = 0; i < MAX_NR_ZONES; i++)
  1513. realtotalpages -= zholes_size[i];
  1514. pgdat->node_present_pages = realtotalpages;
  1515. printk(KERN_DEBUG "On node %d totalpages: %lu\n", pgdat->node_id, realtotalpages);
  1516. }
  1517. /*
  1518. * Initially all pages are reserved - free ones are freed
  1519. * up by free_all_bootmem() once the early boot process is
  1520. * done. Non-atomic initialization, single-pass.
  1521. */
  1522. void __meminit memmap_init_zone(unsigned long size, int nid, unsigned long zone,
  1523. unsigned long start_pfn)
  1524. {
  1525. struct page *page;
  1526. unsigned long end_pfn = start_pfn + size;
  1527. unsigned long pfn;
  1528. for (pfn = start_pfn; pfn < end_pfn; pfn++) {
  1529. if (!early_pfn_valid(pfn))
  1530. continue;
  1531. page = pfn_to_page(pfn);
  1532. set_page_links(page, zone, nid, pfn);
  1533. init_page_count(page);
  1534. reset_page_mapcount(page);
  1535. SetPageReserved(page);
  1536. INIT_LIST_HEAD(&page->lru);
  1537. #ifdef WANT_PAGE_VIRTUAL
  1538. /* The shift won't overflow because ZONE_NORMAL is below 4G. */
  1539. if (!is_highmem_idx(zone))
  1540. set_page_address(page, __va(pfn << PAGE_SHIFT));
  1541. #endif
  1542. }
  1543. }
  1544. void zone_init_free_lists(struct pglist_data *pgdat, struct zone *zone,
  1545. unsigned long size)
  1546. {
  1547. int order;
  1548. for (order = 0; order < MAX_ORDER ; order++) {
  1549. INIT_LIST_HEAD(&zone->free_area[order].free_list);
  1550. zone->free_area[order].nr_free = 0;
  1551. }
  1552. }
  1553. #define ZONETABLE_INDEX(x, zone_nr) ((x << ZONES_SHIFT) | zone_nr)
  1554. void zonetable_add(struct zone *zone, int nid, int zid, unsigned long pfn,
  1555. unsigned long size)
  1556. {
  1557. unsigned long snum = pfn_to_section_nr(pfn);
  1558. unsigned long end = pfn_to_section_nr(pfn + size);
  1559. if (FLAGS_HAS_NODE)
  1560. zone_table[ZONETABLE_INDEX(nid, zid)] = zone;
  1561. else
  1562. for (; snum <= end; snum++)
  1563. zone_table[ZONETABLE_INDEX(snum, zid)] = zone;
  1564. }
  1565. #ifndef __HAVE_ARCH_MEMMAP_INIT
  1566. #define memmap_init(size, nid, zone, start_pfn) \
  1567. memmap_init_zone((size), (nid), (zone), (start_pfn))
  1568. #endif
  1569. static int __cpuinit zone_batchsize(struct zone *zone)
  1570. {
  1571. int batch;
  1572. /*
  1573. * The per-cpu-pages pools are set to around 1000th of the
  1574. * size of the zone. But no more than 1/2 of a meg.
  1575. *
  1576. * OK, so we don't know how big the cache is. So guess.
  1577. */
  1578. batch = zone->present_pages / 1024;
  1579. if (batch * PAGE_SIZE > 512 * 1024)
  1580. batch = (512 * 1024) / PAGE_SIZE;
  1581. batch /= 4; /* We effectively *= 4 below */
  1582. if (batch < 1)
  1583. batch = 1;
  1584. /*
  1585. * Clamp the batch to a 2^n - 1 value. Having a power
  1586. * of 2 value was found to be more likely to have
  1587. * suboptimal cache aliasing properties in some cases.
  1588. *
  1589. * For example if 2 tasks are alternately allocating
  1590. * batches of pages, one task can end up with a lot
  1591. * of pages of one half of the possible page colors
  1592. * and the other with pages of the other colors.
  1593. */
  1594. batch = (1 << (fls(batch + batch/2)-1)) - 1;
  1595. return batch;
  1596. }
  1597. inline void setup_pageset(struct per_cpu_pageset *p, unsigned long batch)
  1598. {
  1599. struct per_cpu_pages *pcp;
  1600. memset(p, 0, sizeof(*p));
  1601. pcp = &p->pcp[0]; /* hot */
  1602. pcp->count = 0;
  1603. pcp->high = 6 * batch;
  1604. pcp->batch = max(1UL, 1 * batch);
  1605. INIT_LIST_HEAD(&pcp->list);
  1606. pcp = &p->pcp[1]; /* cold*/
  1607. pcp->count = 0;
  1608. pcp->high = 2 * batch;
  1609. pcp->batch = max(1UL, batch/2);
  1610. INIT_LIST_HEAD(&pcp->list);
  1611. }
  1612. /*
  1613. * setup_pagelist_highmark() sets the high water mark for hot per_cpu_pagelist
  1614. * to the value high for the pageset p.
  1615. */
  1616. static void setup_pagelist_highmark(struct per_cpu_pageset *p,
  1617. unsigned long high)
  1618. {
  1619. struct per_cpu_pages *pcp;
  1620. pcp = &p->pcp[0]; /* hot list */
  1621. pcp->high = high;
  1622. pcp->batch = max(1UL, high/4);
  1623. if ((high/4) > (PAGE_SHIFT * 8))
  1624. pcp->batch = PAGE_SHIFT * 8;
  1625. }
  1626. #ifdef CONFIG_NUMA
  1627. /*
  1628. * Boot pageset table. One per cpu which is going to be used for all
  1629. * zones and all nodes. The parameters will be set in such a way
  1630. * that an item put on a list will immediately be handed over to
  1631. * the buddy list. This is safe since pageset manipulation is done
  1632. * with interrupts disabled.
  1633. *
  1634. * Some NUMA counter updates may also be caught by the boot pagesets.
  1635. *
  1636. * The boot_pagesets must be kept even after bootup is complete for
  1637. * unused processors and/or zones. They do play a role for bootstrapping
  1638. * hotplugged processors.
  1639. *
  1640. * zoneinfo_show() and maybe other functions do
  1641. * not check if the processor is online before following the pageset pointer.
  1642. * Other parts of the kernel may not check if the zone is available.
  1643. */
  1644. static struct per_cpu_pageset boot_pageset[NR_CPUS];
  1645. /*
  1646. * Dynamically allocate memory for the
  1647. * per cpu pageset array in struct zone.
  1648. */
  1649. static int __cpuinit process_zones(int cpu)
  1650. {
  1651. struct zone *zone, *dzone;
  1652. for_each_zone(zone) {
  1653. zone_pcp(zone, cpu) = kmalloc_node(sizeof(struct per_cpu_pageset),
  1654. GFP_KERNEL, cpu_to_node(cpu));
  1655. if (!zone_pcp(zone, cpu))
  1656. goto bad;
  1657. setup_pageset(zone_pcp(zone, cpu), zone_batchsize(zone));
  1658. if (percpu_pagelist_fraction)
  1659. setup_pagelist_highmark(zone_pcp(zone, cpu),
  1660. (zone->present_pages / percpu_pagelist_fraction));
  1661. }
  1662. return 0;
  1663. bad:
  1664. for_each_zone(dzone) {
  1665. if (dzone == zone)
  1666. break;
  1667. kfree(zone_pcp(dzone, cpu));
  1668. zone_pcp(dzone, cpu) = NULL;
  1669. }
  1670. return -ENOMEM;
  1671. }
  1672. static inline void free_zone_pagesets(int cpu)
  1673. {
  1674. struct zone *zone;
  1675. for_each_zone(zone) {
  1676. struct per_cpu_pageset *pset = zone_pcp(zone, cpu);
  1677. zone_pcp(zone, cpu) = NULL;
  1678. kfree(pset);
  1679. }
  1680. }
  1681. static int pageset_cpuup_callback(struct notifier_block *nfb,
  1682. unsigned long action,
  1683. void *hcpu)
  1684. {
  1685. int cpu = (long)hcpu;
  1686. int ret = NOTIFY_OK;
  1687. switch (action) {
  1688. case CPU_UP_PREPARE:
  1689. if (process_zones(cpu))
  1690. ret = NOTIFY_BAD;
  1691. break;
  1692. case CPU_UP_CANCELED:
  1693. case CPU_DEAD:
  1694. free_zone_pagesets(cpu);
  1695. break;
  1696. default:
  1697. break;
  1698. }
  1699. return ret;
  1700. }
  1701. static struct notifier_block pageset_notifier =
  1702. { &pageset_cpuup_callback, NULL, 0 };
  1703. void __init setup_per_cpu_pageset(void)
  1704. {
  1705. int err;
  1706. /* Initialize per_cpu_pageset for cpu 0.
  1707. * A cpuup callback will do this for every cpu
  1708. * as it comes online
  1709. */
  1710. err = process_zones(smp_processor_id());
  1711. BUG_ON(err);
  1712. register_cpu_notifier(&pageset_notifier);
  1713. }
  1714. #endif
  1715. static __meminit
  1716. void zone_wait_table_init(struct zone *zone, unsigned long zone_size_pages)
  1717. {
  1718. int i;
  1719. struct pglist_data *pgdat = zone->zone_pgdat;
  1720. /*
  1721. * The per-page waitqueue mechanism uses hashed waitqueues
  1722. * per zone.
  1723. */
  1724. zone->wait_table_size = wait_table_size(zone_size_pages);
  1725. zone->wait_table_bits = wait_table_bits(zone->wait_table_size);
  1726. zone->wait_table = (wait_queue_head_t *)
  1727. alloc_bootmem_node(pgdat, zone->wait_table_size
  1728. * sizeof(wait_queue_head_t));
  1729. for(i = 0; i < zone->wait_table_size; ++i)
  1730. init_waitqueue_head(zone->wait_table + i);
  1731. }
  1732. static __meminit void zone_pcp_init(struct zone *zone)
  1733. {
  1734. int cpu;
  1735. unsigned long batch = zone_batchsize(zone);
  1736. for (cpu = 0; cpu < NR_CPUS; cpu++) {
  1737. #ifdef CONFIG_NUMA
  1738. /* Early boot. Slab allocator not functional yet */
  1739. zone_pcp(zone, cpu) = &boot_pageset[cpu];
  1740. setup_pageset(&boot_pageset[cpu],0);
  1741. #else
  1742. setup_pageset(zone_pcp(zone,cpu), batch);
  1743. #endif
  1744. }
  1745. if (zone->present_pages)
  1746. printk(KERN_DEBUG " %s zone: %lu pages, LIFO batch:%lu\n",
  1747. zone->name, zone->present_pages, batch);
  1748. }
  1749. static __meminit void init_currently_empty_zone(struct zone *zone,
  1750. unsigned long zone_start_pfn, unsigned long size)
  1751. {
  1752. struct pglist_data *pgdat = zone->zone_pgdat;
  1753. zone_wait_table_init(zone, size);
  1754. pgdat->nr_zones = zone_idx(zone) + 1;
  1755. zone->zone_start_pfn = zone_start_pfn;
  1756. memmap_init(size, pgdat->node_id, zone_idx(zone), zone_start_pfn);
  1757. zone_init_free_lists(pgdat, zone, zone->spanned_pages);
  1758. }
  1759. /*
  1760. * Set up the zone data structures:
  1761. * - mark all pages reserved
  1762. * - mark all memory queues empty
  1763. * - clear the memory bitmaps
  1764. */
  1765. static void __init free_area_init_core(struct pglist_data *pgdat,
  1766. unsigned long *zones_size, unsigned long *zholes_size)
  1767. {
  1768. unsigned long j;
  1769. int nid = pgdat->node_id;
  1770. unsigned long zone_start_pfn = pgdat->node_start_pfn;
  1771. pgdat_resize_init(pgdat);
  1772. pgdat->nr_zones = 0;
  1773. init_waitqueue_head(&pgdat->kswapd_wait);
  1774. pgdat->kswapd_max_order = 0;
  1775. for (j = 0; j < MAX_NR_ZONES; j++) {
  1776. struct zone *zone = pgdat->node_zones + j;
  1777. unsigned long size, realsize;
  1778. realsize = size = zones_size[j];
  1779. if (zholes_size)
  1780. realsize -= zholes_size[j];
  1781. if (j < ZONE_HIGHMEM)
  1782. nr_kernel_pages += realsize;
  1783. nr_all_pages += realsize;
  1784. zone->spanned_pages = size;
  1785. zone->present_pages = realsize;
  1786. zone->name = zone_names[j];
  1787. spin_lock_init(&zone->lock);
  1788. spin_lock_init(&zone->lru_lock);
  1789. zone_seqlock_init(zone);
  1790. zone->zone_pgdat = pgdat;
  1791. zone->free_pages = 0;
  1792. zone->temp_priority = zone->prev_priority = DEF_PRIORITY;
  1793. zone_pcp_init(zone);
  1794. INIT_LIST_HEAD(&zone->active_list);
  1795. INIT_LIST_HEAD(&zone->inactive_list);
  1796. zone->nr_scan_active = 0;
  1797. zone->nr_scan_inactive = 0;
  1798. zone->nr_active = 0;
  1799. zone->nr_inactive = 0;
  1800. atomic_set(&zone->reclaim_in_progress, 0);
  1801. if (!size)
  1802. continue;
  1803. zonetable_add(zone, nid, j, zone_start_pfn, size);
  1804. init_currently_empty_zone(zone, zone_start_pfn, size);
  1805. zone_start_pfn += size;
  1806. }
  1807. }
  1808. static void __init alloc_node_mem_map(struct pglist_data *pgdat)
  1809. {
  1810. /* Skip empty nodes */
  1811. if (!pgdat->node_spanned_pages)
  1812. return;
  1813. #ifdef CONFIG_FLAT_NODE_MEM_MAP
  1814. /* ia64 gets its own node_mem_map, before this, without bootmem */
  1815. if (!pgdat->node_mem_map) {
  1816. unsigned long size, start, end;
  1817. struct page *map;
  1818. /*
  1819. * The zone's endpoints aren't required to be MAX_ORDER
  1820. * aligned but the node_mem_map endpoints must be in order
  1821. * for the buddy allocator to function correctly.
  1822. */
  1823. start = pgdat->node_start_pfn & ~(MAX_ORDER_NR_PAGES - 1);
  1824. end = pgdat->node_start_pfn + pgdat->node_spanned_pages;
  1825. end = ALIGN(end, MAX_ORDER_NR_PAGES);
  1826. size = (end - start) * sizeof(struct page);
  1827. map = alloc_remap(pgdat->node_id, size);
  1828. if (!map)
  1829. map = alloc_bootmem_node(pgdat, size);
  1830. pgdat->node_mem_map = map + (pgdat->node_start_pfn - start);
  1831. }
  1832. #ifdef CONFIG_FLATMEM
  1833. /*
  1834. * With no DISCONTIG, the global mem_map is just set as node 0's
  1835. */
  1836. if (pgdat == NODE_DATA(0))
  1837. mem_map = NODE_DATA(0)->node_mem_map;
  1838. #endif
  1839. #endif /* CONFIG_FLAT_NODE_MEM_MAP */
  1840. }
  1841. void __init free_area_init_node(int nid, struct pglist_data *pgdat,
  1842. unsigned long *zones_size, unsigned long node_start_pfn,
  1843. unsigned long *zholes_size)
  1844. {
  1845. pgdat->node_id = nid;
  1846. pgdat->node_start_pfn = node_start_pfn;
  1847. calculate_zone_totalpages(pgdat, zones_size, zholes_size);
  1848. alloc_node_mem_map(pgdat);
  1849. free_area_init_core(pgdat, zones_size, zholes_size);
  1850. }
  1851. #ifndef CONFIG_NEED_MULTIPLE_NODES
  1852. static bootmem_data_t contig_bootmem_data;
  1853. struct pglist_data contig_page_data = { .bdata = &contig_bootmem_data };
  1854. EXPORT_SYMBOL(contig_page_data);
  1855. #endif
  1856. void __init free_area_init(unsigned long *zones_size)
  1857. {
  1858. free_area_init_node(0, NODE_DATA(0), zones_size,
  1859. __pa(PAGE_OFFSET) >> PAGE_SHIFT, NULL);
  1860. }
  1861. #ifdef CONFIG_PROC_FS
  1862. #include <linux/seq_file.h>
  1863. static void *frag_start(struct seq_file *m, loff_t *pos)
  1864. {
  1865. pg_data_t *pgdat;
  1866. loff_t node = *pos;
  1867. for (pgdat = first_online_pgdat();
  1868. pgdat && node;
  1869. pgdat = next_online_pgdat(pgdat))
  1870. --node;
  1871. return pgdat;
  1872. }
  1873. static void *frag_next(struct seq_file *m, void *arg, loff_t *pos)
  1874. {
  1875. pg_data_t *pgdat = (pg_data_t *)arg;
  1876. (*pos)++;
  1877. return next_online_pgdat(pgdat);
  1878. }
  1879. static void frag_stop(struct seq_file *m, void *arg)
  1880. {
  1881. }
  1882. /*
  1883. * This walks the free areas for each zone.
  1884. */
  1885. static int frag_show(struct seq_file *m, void *arg)
  1886. {
  1887. pg_data_t *pgdat = (pg_data_t *)arg;
  1888. struct zone *zone;
  1889. struct zone *node_zones = pgdat->node_zones;
  1890. unsigned long flags;
  1891. int order;
  1892. for (zone = node_zones; zone - node_zones < MAX_NR_ZONES; ++zone) {
  1893. if (!populated_zone(zone))
  1894. continue;
  1895. spin_lock_irqsave(&zone->lock, flags);
  1896. seq_printf(m, "Node %d, zone %8s ", pgdat->node_id, zone->name);
  1897. for (order = 0; order < MAX_ORDER; ++order)
  1898. seq_printf(m, "%6lu ", zone->free_area[order].nr_free);
  1899. spin_unlock_irqrestore(&zone->lock, flags);
  1900. seq_putc(m, '\n');
  1901. }
  1902. return 0;
  1903. }
  1904. struct seq_operations fragmentation_op = {
  1905. .start = frag_start,
  1906. .next = frag_next,
  1907. .stop = frag_stop,
  1908. .show = frag_show,
  1909. };
  1910. /*
  1911. * Output information about zones in @pgdat.
  1912. */
  1913. static int zoneinfo_show(struct seq_file *m, void *arg)
  1914. {
  1915. pg_data_t *pgdat = arg;
  1916. struct zone *zone;
  1917. struct zone *node_zones = pgdat->node_zones;
  1918. unsigned long flags;
  1919. for (zone = node_zones; zone - node_zones < MAX_NR_ZONES; zone++) {
  1920. int i;
  1921. if (!populated_zone(zone))
  1922. continue;
  1923. spin_lock_irqsave(&zone->lock, flags);
  1924. seq_printf(m, "Node %d, zone %8s", pgdat->node_id, zone->name);
  1925. seq_printf(m,
  1926. "\n pages free %lu"
  1927. "\n min %lu"
  1928. "\n low %lu"
  1929. "\n high %lu"
  1930. "\n active %lu"
  1931. "\n inactive %lu"
  1932. "\n scanned %lu (a: %lu i: %lu)"
  1933. "\n spanned %lu"
  1934. "\n present %lu",
  1935. zone->free_pages,
  1936. zone->pages_min,
  1937. zone->pages_low,
  1938. zone->pages_high,
  1939. zone->nr_active,
  1940. zone->nr_inactive,
  1941. zone->pages_scanned,
  1942. zone->nr_scan_active, zone->nr_scan_inactive,
  1943. zone->spanned_pages,
  1944. zone->present_pages);
  1945. seq_printf(m,
  1946. "\n protection: (%lu",
  1947. zone->lowmem_reserve[0]);
  1948. for (i = 1; i < ARRAY_SIZE(zone->lowmem_reserve); i++)
  1949. seq_printf(m, ", %lu", zone->lowmem_reserve[i]);
  1950. seq_printf(m,
  1951. ")"
  1952. "\n pagesets");
  1953. for_each_online_cpu(i) {
  1954. struct per_cpu_pageset *pageset;
  1955. int j;
  1956. pageset = zone_pcp(zone, i);
  1957. for (j = 0; j < ARRAY_SIZE(pageset->pcp); j++) {
  1958. if (pageset->pcp[j].count)
  1959. break;
  1960. }
  1961. if (j == ARRAY_SIZE(pageset->pcp))
  1962. continue;
  1963. for (j = 0; j < ARRAY_SIZE(pageset->pcp); j++) {
  1964. seq_printf(m,
  1965. "\n cpu: %i pcp: %i"
  1966. "\n count: %i"
  1967. "\n high: %i"
  1968. "\n batch: %i",
  1969. i, j,
  1970. pageset->pcp[j].count,
  1971. pageset->pcp[j].high,
  1972. pageset->pcp[j].batch);
  1973. }
  1974. #ifdef CONFIG_NUMA
  1975. seq_printf(m,
  1976. "\n numa_hit: %lu"
  1977. "\n numa_miss: %lu"
  1978. "\n numa_foreign: %lu"
  1979. "\n interleave_hit: %lu"
  1980. "\n local_node: %lu"
  1981. "\n other_node: %lu",
  1982. pageset->numa_hit,
  1983. pageset->numa_miss,
  1984. pageset->numa_foreign,
  1985. pageset->interleave_hit,
  1986. pageset->local_node,
  1987. pageset->other_node);
  1988. #endif
  1989. }
  1990. seq_printf(m,
  1991. "\n all_unreclaimable: %u"
  1992. "\n prev_priority: %i"
  1993. "\n temp_priority: %i"
  1994. "\n start_pfn: %lu",
  1995. zone->all_unreclaimable,
  1996. zone->prev_priority,
  1997. zone->temp_priority,
  1998. zone->zone_start_pfn);
  1999. spin_unlock_irqrestore(&zone->lock, flags);
  2000. seq_putc(m, '\n');
  2001. }
  2002. return 0;
  2003. }
  2004. struct seq_operations zoneinfo_op = {
  2005. .start = frag_start, /* iterate over all zones. The same as in
  2006. * fragmentation. */
  2007. .next = frag_next,
  2008. .stop = frag_stop,
  2009. .show = zoneinfo_show,
  2010. };
  2011. static char *vmstat_text[] = {
  2012. "nr_dirty",
  2013. "nr_writeback",
  2014. "nr_unstable",
  2015. "nr_page_table_pages",
  2016. "nr_mapped",
  2017. "nr_slab",
  2018. "pgpgin",
  2019. "pgpgout",
  2020. "pswpin",
  2021. "pswpout",
  2022. "pgalloc_high",
  2023. "pgalloc_normal",
  2024. "pgalloc_dma32",
  2025. "pgalloc_dma",
  2026. "pgfree",
  2027. "pgactivate",
  2028. "pgdeactivate",
  2029. "pgfault",
  2030. "pgmajfault",
  2031. "pgrefill_high",
  2032. "pgrefill_normal",
  2033. "pgrefill_dma32",
  2034. "pgrefill_dma",
  2035. "pgsteal_high",
  2036. "pgsteal_normal",
  2037. "pgsteal_dma32",
  2038. "pgsteal_dma",
  2039. "pgscan_kswapd_high",
  2040. "pgscan_kswapd_normal",
  2041. "pgscan_kswapd_dma32",
  2042. "pgscan_kswapd_dma",
  2043. "pgscan_direct_high",
  2044. "pgscan_direct_normal",
  2045. "pgscan_direct_dma32",
  2046. "pgscan_direct_dma",
  2047. "pginodesteal",
  2048. "slabs_scanned",
  2049. "kswapd_steal",
  2050. "kswapd_inodesteal",
  2051. "pageoutrun",
  2052. "allocstall",
  2053. "pgrotated",
  2054. "nr_bounce",
  2055. };
  2056. static void *vmstat_start(struct seq_file *m, loff_t *pos)
  2057. {
  2058. struct page_state *ps;
  2059. if (*pos >= ARRAY_SIZE(vmstat_text))
  2060. return NULL;
  2061. ps = kmalloc(sizeof(*ps), GFP_KERNEL);
  2062. m->private = ps;
  2063. if (!ps)
  2064. return ERR_PTR(-ENOMEM);
  2065. get_full_page_state(ps);
  2066. ps->pgpgin /= 2; /* sectors -> kbytes */
  2067. ps->pgpgout /= 2;
  2068. return (unsigned long *)ps + *pos;
  2069. }
  2070. static void *vmstat_next(struct seq_file *m, void *arg, loff_t *pos)
  2071. {
  2072. (*pos)++;
  2073. if (*pos >= ARRAY_SIZE(vmstat_text))
  2074. return NULL;
  2075. return (unsigned long *)m->private + *pos;
  2076. }
  2077. static int vmstat_show(struct seq_file *m, void *arg)
  2078. {
  2079. unsigned long *l = arg;
  2080. unsigned long off = l - (unsigned long *)m->private;
  2081. seq_printf(m, "%s %lu\n", vmstat_text[off], *l);
  2082. return 0;
  2083. }
  2084. static void vmstat_stop(struct seq_file *m, void *arg)
  2085. {
  2086. kfree(m->private);
  2087. m->private = NULL;
  2088. }
  2089. struct seq_operations vmstat_op = {
  2090. .start = vmstat_start,
  2091. .next = vmstat_next,
  2092. .stop = vmstat_stop,
  2093. .show = vmstat_show,
  2094. };
  2095. #endif /* CONFIG_PROC_FS */
  2096. #ifdef CONFIG_HOTPLUG_CPU
  2097. static int page_alloc_cpu_notify(struct notifier_block *self,
  2098. unsigned long action, void *hcpu)
  2099. {
  2100. int cpu = (unsigned long)hcpu;
  2101. long *count;
  2102. unsigned long *src, *dest;
  2103. if (action == CPU_DEAD) {
  2104. int i;
  2105. /* Drain local pagecache count. */
  2106. count = &per_cpu(nr_pagecache_local, cpu);
  2107. atomic_add(*count, &nr_pagecache);
  2108. *count = 0;
  2109. local_irq_disable();
  2110. __drain_pages(cpu);
  2111. /* Add dead cpu's page_states to our own. */
  2112. dest = (unsigned long *)&__get_cpu_var(page_states);
  2113. src = (unsigned long *)&per_cpu(page_states, cpu);
  2114. for (i = 0; i < sizeof(struct page_state)/sizeof(unsigned long);
  2115. i++) {
  2116. dest[i] += src[i];
  2117. src[i] = 0;
  2118. }
  2119. local_irq_enable();
  2120. }
  2121. return NOTIFY_OK;
  2122. }
  2123. #endif /* CONFIG_HOTPLUG_CPU */
  2124. void __init page_alloc_init(void)
  2125. {
  2126. hotcpu_notifier(page_alloc_cpu_notify, 0);
  2127. }
  2128. /*
  2129. * calculate_totalreserve_pages - called when sysctl_lower_zone_reserve_ratio
  2130. * or min_free_kbytes changes.
  2131. */
  2132. static void calculate_totalreserve_pages(void)
  2133. {
  2134. struct pglist_data *pgdat;
  2135. unsigned long reserve_pages = 0;
  2136. int i, j;
  2137. for_each_online_pgdat(pgdat) {
  2138. for (i = 0; i < MAX_NR_ZONES; i++) {
  2139. struct zone *zone = pgdat->node_zones + i;
  2140. unsigned long max = 0;
  2141. /* Find valid and maximum lowmem_reserve in the zone */
  2142. for (j = i; j < MAX_NR_ZONES; j++) {
  2143. if (zone->lowmem_reserve[j] > max)
  2144. max = zone->lowmem_reserve[j];
  2145. }
  2146. /* we treat pages_high as reserved pages. */
  2147. max += zone->pages_high;
  2148. if (max > zone->present_pages)
  2149. max = zone->present_pages;
  2150. reserve_pages += max;
  2151. }
  2152. }
  2153. totalreserve_pages = reserve_pages;
  2154. }
  2155. /*
  2156. * setup_per_zone_lowmem_reserve - called whenever
  2157. * sysctl_lower_zone_reserve_ratio changes. Ensures that each zone
  2158. * has a correct pages reserved value, so an adequate number of
  2159. * pages are left in the zone after a successful __alloc_pages().
  2160. */
  2161. static void setup_per_zone_lowmem_reserve(void)
  2162. {
  2163. struct pglist_data *pgdat;
  2164. int j, idx;
  2165. for_each_online_pgdat(pgdat) {
  2166. for (j = 0; j < MAX_NR_ZONES; j++) {
  2167. struct zone *zone = pgdat->node_zones + j;
  2168. unsigned long present_pages = zone->present_pages;
  2169. zone->lowmem_reserve[j] = 0;
  2170. for (idx = j-1; idx >= 0; idx--) {
  2171. struct zone *lower_zone;
  2172. if (sysctl_lowmem_reserve_ratio[idx] < 1)
  2173. sysctl_lowmem_reserve_ratio[idx] = 1;
  2174. lower_zone = pgdat->node_zones + idx;
  2175. lower_zone->lowmem_reserve[j] = present_pages /
  2176. sysctl_lowmem_reserve_ratio[idx];
  2177. present_pages += lower_zone->present_pages;
  2178. }
  2179. }
  2180. }
  2181. /* update totalreserve_pages */
  2182. calculate_totalreserve_pages();
  2183. }
  2184. /*
  2185. * setup_per_zone_pages_min - called when min_free_kbytes changes. Ensures
  2186. * that the pages_{min,low,high} values for each zone are set correctly
  2187. * with respect to min_free_kbytes.
  2188. */
  2189. void setup_per_zone_pages_min(void)
  2190. {
  2191. unsigned long pages_min = min_free_kbytes >> (PAGE_SHIFT - 10);
  2192. unsigned long lowmem_pages = 0;
  2193. struct zone *zone;
  2194. unsigned long flags;
  2195. /* Calculate total number of !ZONE_HIGHMEM pages */
  2196. for_each_zone(zone) {
  2197. if (!is_highmem(zone))
  2198. lowmem_pages += zone->present_pages;
  2199. }
  2200. for_each_zone(zone) {
  2201. u64 tmp;
  2202. spin_lock_irqsave(&zone->lru_lock, flags);
  2203. tmp = (u64)pages_min * zone->present_pages;
  2204. do_div(tmp, lowmem_pages);
  2205. if (is_highmem(zone)) {
  2206. /*
  2207. * __GFP_HIGH and PF_MEMALLOC allocations usually don't
  2208. * need highmem pages, so cap pages_min to a small
  2209. * value here.
  2210. *
  2211. * The (pages_high-pages_low) and (pages_low-pages_min)
  2212. * deltas controls asynch page reclaim, and so should
  2213. * not be capped for highmem.
  2214. */
  2215. int min_pages;
  2216. min_pages = zone->present_pages / 1024;
  2217. if (min_pages < SWAP_CLUSTER_MAX)
  2218. min_pages = SWAP_CLUSTER_MAX;
  2219. if (min_pages > 128)
  2220. min_pages = 128;
  2221. zone->pages_min = min_pages;
  2222. } else {
  2223. /*
  2224. * If it's a lowmem zone, reserve a number of pages
  2225. * proportionate to the zone's size.
  2226. */
  2227. zone->pages_min = tmp;
  2228. }
  2229. zone->pages_low = zone->pages_min + (tmp >> 2);
  2230. zone->pages_high = zone->pages_min + (tmp >> 1);
  2231. spin_unlock_irqrestore(&zone->lru_lock, flags);
  2232. }
  2233. /* update totalreserve_pages */
  2234. calculate_totalreserve_pages();
  2235. }
  2236. /*
  2237. * Initialise min_free_kbytes.
  2238. *
  2239. * For small machines we want it small (128k min). For large machines
  2240. * we want it large (64MB max). But it is not linear, because network
  2241. * bandwidth does not increase linearly with machine size. We use
  2242. *
  2243. * min_free_kbytes = 4 * sqrt(lowmem_kbytes), for better accuracy:
  2244. * min_free_kbytes = sqrt(lowmem_kbytes * 16)
  2245. *
  2246. * which yields
  2247. *
  2248. * 16MB: 512k
  2249. * 32MB: 724k
  2250. * 64MB: 1024k
  2251. * 128MB: 1448k
  2252. * 256MB: 2048k
  2253. * 512MB: 2896k
  2254. * 1024MB: 4096k
  2255. * 2048MB: 5792k
  2256. * 4096MB: 8192k
  2257. * 8192MB: 11584k
  2258. * 16384MB: 16384k
  2259. */
  2260. static int __init init_per_zone_pages_min(void)
  2261. {
  2262. unsigned long lowmem_kbytes;
  2263. lowmem_kbytes = nr_free_buffer_pages() * (PAGE_SIZE >> 10);
  2264. min_free_kbytes = int_sqrt(lowmem_kbytes * 16);
  2265. if (min_free_kbytes < 128)
  2266. min_free_kbytes = 128;
  2267. if (min_free_kbytes > 65536)
  2268. min_free_kbytes = 65536;
  2269. setup_per_zone_pages_min();
  2270. setup_per_zone_lowmem_reserve();
  2271. return 0;
  2272. }
  2273. module_init(init_per_zone_pages_min)
  2274. /*
  2275. * min_free_kbytes_sysctl_handler - just a wrapper around proc_dointvec() so
  2276. * that we can call two helper functions whenever min_free_kbytes
  2277. * changes.
  2278. */
  2279. int min_free_kbytes_sysctl_handler(ctl_table *table, int write,
  2280. struct file *file, void __user *buffer, size_t *length, loff_t *ppos)
  2281. {
  2282. proc_dointvec(table, write, file, buffer, length, ppos);
  2283. setup_per_zone_pages_min();
  2284. return 0;
  2285. }
  2286. /*
  2287. * lowmem_reserve_ratio_sysctl_handler - just a wrapper around
  2288. * proc_dointvec() so that we can call setup_per_zone_lowmem_reserve()
  2289. * whenever sysctl_lowmem_reserve_ratio changes.
  2290. *
  2291. * The reserve ratio obviously has absolutely no relation with the
  2292. * pages_min watermarks. The lowmem reserve ratio can only make sense
  2293. * if in function of the boot time zone sizes.
  2294. */
  2295. int lowmem_reserve_ratio_sysctl_handler(ctl_table *table, int write,
  2296. struct file *file, void __user *buffer, size_t *length, loff_t *ppos)
  2297. {
  2298. proc_dointvec_minmax(table, write, file, buffer, length, ppos);
  2299. setup_per_zone_lowmem_reserve();
  2300. return 0;
  2301. }
  2302. /*
  2303. * percpu_pagelist_fraction - changes the pcp->high for each zone on each
  2304. * cpu. It is the fraction of total pages in each zone that a hot per cpu pagelist
  2305. * can have before it gets flushed back to buddy allocator.
  2306. */
  2307. int percpu_pagelist_fraction_sysctl_handler(ctl_table *table, int write,
  2308. struct file *file, void __user *buffer, size_t *length, loff_t *ppos)
  2309. {
  2310. struct zone *zone;
  2311. unsigned int cpu;
  2312. int ret;
  2313. ret = proc_dointvec_minmax(table, write, file, buffer, length, ppos);
  2314. if (!write || (ret == -EINVAL))
  2315. return ret;
  2316. for_each_zone(zone) {
  2317. for_each_online_cpu(cpu) {
  2318. unsigned long high;
  2319. high = zone->present_pages / percpu_pagelist_fraction;
  2320. setup_pagelist_highmark(zone_pcp(zone, cpu), high);
  2321. }
  2322. }
  2323. return 0;
  2324. }
  2325. __initdata int hashdist = HASHDIST_DEFAULT;
  2326. #ifdef CONFIG_NUMA
  2327. static int __init set_hashdist(char *str)
  2328. {
  2329. if (!str)
  2330. return 0;
  2331. hashdist = simple_strtoul(str, &str, 0);
  2332. return 1;
  2333. }
  2334. __setup("hashdist=", set_hashdist);
  2335. #endif
  2336. /*
  2337. * allocate a large system hash table from bootmem
  2338. * - it is assumed that the hash table must contain an exact power-of-2
  2339. * quantity of entries
  2340. * - limit is the number of hash buckets, not the total allocation size
  2341. */
  2342. void *__init alloc_large_system_hash(const char *tablename,
  2343. unsigned long bucketsize,
  2344. unsigned long numentries,
  2345. int scale,
  2346. int flags,
  2347. unsigned int *_hash_shift,
  2348. unsigned int *_hash_mask,
  2349. unsigned long limit)
  2350. {
  2351. unsigned long long max = limit;
  2352. unsigned long log2qty, size;
  2353. void *table = NULL;
  2354. /* allow the kernel cmdline to have a say */
  2355. if (!numentries) {
  2356. /* round applicable memory size up to nearest megabyte */
  2357. numentries = (flags & HASH_HIGHMEM) ? nr_all_pages : nr_kernel_pages;
  2358. numentries += (1UL << (20 - PAGE_SHIFT)) - 1;
  2359. numentries >>= 20 - PAGE_SHIFT;
  2360. numentries <<= 20 - PAGE_SHIFT;
  2361. /* limit to 1 bucket per 2^scale bytes of low memory */
  2362. if (scale > PAGE_SHIFT)
  2363. numentries >>= (scale - PAGE_SHIFT);
  2364. else
  2365. numentries <<= (PAGE_SHIFT - scale);
  2366. }
  2367. numentries = roundup_pow_of_two(numentries);
  2368. /* limit allocation size to 1/16 total memory by default */
  2369. if (max == 0) {
  2370. max = ((unsigned long long)nr_all_pages << PAGE_SHIFT) >> 4;
  2371. do_div(max, bucketsize);
  2372. }
  2373. if (numentries > max)
  2374. numentries = max;
  2375. log2qty = long_log2(numentries);
  2376. do {
  2377. size = bucketsize << log2qty;
  2378. if (flags & HASH_EARLY)
  2379. table = alloc_bootmem(size);
  2380. else if (hashdist)
  2381. table = __vmalloc(size, GFP_ATOMIC, PAGE_KERNEL);
  2382. else {
  2383. unsigned long order;
  2384. for (order = 0; ((1UL << order) << PAGE_SHIFT) < size; order++)
  2385. ;
  2386. table = (void*) __get_free_pages(GFP_ATOMIC, order);
  2387. }
  2388. } while (!table && size > PAGE_SIZE && --log2qty);
  2389. if (!table)
  2390. panic("Failed to allocate %s hash table\n", tablename);
  2391. printk("%s hash table entries: %d (order: %d, %lu bytes)\n",
  2392. tablename,
  2393. (1U << log2qty),
  2394. long_log2(size) - PAGE_SHIFT,
  2395. size);
  2396. if (_hash_shift)
  2397. *_hash_shift = log2qty;
  2398. if (_hash_mask)
  2399. *_hash_mask = (1 << log2qty) - 1;
  2400. return table;
  2401. }
  2402. #ifdef CONFIG_OUT_OF_LINE_PFN_TO_PAGE
  2403. /*
  2404. * pfn <-> page translation. out-of-line version.
  2405. * (see asm-generic/memory_model.h)
  2406. */
  2407. #if defined(CONFIG_FLATMEM)
  2408. struct page *pfn_to_page(unsigned long pfn)
  2409. {
  2410. return mem_map + (pfn - ARCH_PFN_OFFSET);
  2411. }
  2412. unsigned long page_to_pfn(struct page *page)
  2413. {
  2414. return (page - mem_map) + ARCH_PFN_OFFSET;
  2415. }
  2416. #elif defined(CONFIG_DISCONTIGMEM)
  2417. struct page *pfn_to_page(unsigned long pfn)
  2418. {
  2419. int nid = arch_pfn_to_nid(pfn);
  2420. return NODE_DATA(nid)->node_mem_map + arch_local_page_offset(pfn,nid);
  2421. }
  2422. unsigned long page_to_pfn(struct page *page)
  2423. {
  2424. struct pglist_data *pgdat = NODE_DATA(page_to_nid(page));
  2425. return (page - pgdat->node_mem_map) + pgdat->node_start_pfn;
  2426. }
  2427. #elif defined(CONFIG_SPARSEMEM)
  2428. struct page *pfn_to_page(unsigned long pfn)
  2429. {
  2430. return __section_mem_map_addr(__pfn_to_section(pfn)) + pfn;
  2431. }
  2432. unsigned long page_to_pfn(struct page *page)
  2433. {
  2434. long section_id = page_to_section(page);
  2435. return page - __section_mem_map_addr(__nr_to_section(section_id));
  2436. }
  2437. #endif /* CONFIG_FLATMEM/DISCONTIGMME/SPARSEMEM */
  2438. EXPORT_SYMBOL(pfn_to_page);
  2439. EXPORT_SYMBOL(page_to_pfn);
  2440. #endif /* CONFIG_OUT_OF_LINE_PFN_TO_PAGE */