page_alloc.c 62 KB

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