page_alloc.c 55 KB

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