page_alloc.c 64 KB

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