page_alloc.c 68 KB

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