page_alloc.c 64 KB

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