page_alloc.c 65 KB

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