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