page_alloc.c 62 KB

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