page_alloc.c 64 KB

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