page_alloc.c 67 KB

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