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