page_alloc.c 70 KB

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