page_alloc.c 64 KB

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