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