page_alloc.c 63 KB

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