page_alloc.c 64 KB

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