page_alloc.c 64 KB

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