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