page_alloc.c 63 KB

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