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