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