aboutsummaryrefslogtreecommitdiffstats
path: root/mm/page_alloc.c
diff options
context:
space:
mode:
authorMel Gorman <mel@csn.ul.ie>2007-10-16 04:25:47 -0400
committerLinus Torvalds <torvalds@woody.linux-foundation.org>2007-10-16 12:42:59 -0400
commit835c134ec4dd755e5c4470af566db226d1e96742 (patch)
tree7bc659978b4fba5089fc820185a8b6f0cc010b08 /mm/page_alloc.c
parent954ffcb35f5aca428661d29b96c4eee82b3c19cd (diff)
Add a bitmap that is used to track flags affecting a block of pages
Here is the latest revision of the anti-fragmentation patches. Of particular note in this version is special treatment of high-order atomic allocations. Care is taken to group them together and avoid grouping pages of other types near them. Artifical tests imply that it works. I'm trying to get the hardware together that would allow setting up of a "real" test. If anyone already has a setup and test that can trigger the atomic-allocation problem, I'd appreciate a test of these patches and a report. The second major change is that these patches will apply cleanly with patches that implement anti-fragmentation through zones. kernbench shows effectively no performance difference varying between -0.2% and +2% on a variety of test machines. Success rates for huge page allocation are dramatically increased. For example, on a ppc64 machine, the vanilla kernel was only able to allocate 1% of memory as a hugepage and this was due to a single hugepage reserved as min_free_kbytes. With these patches applied, 17% was allocatable as superpages. With reclaim-related fixes from Andy Whitcroft, it was 40% and further reclaim-related improvements should increase this further. Changelog Since V28 o Group high-order atomic allocations together o It is no longer required to set min_free_kbytes to 10% of memory. A value of 16384 in most cases will be sufficient o Now applied with zone-based anti-fragmentation o Fix incorrect VM_BUG_ON within buffered_rmqueue() o Reorder the stack so later patches do not back out work from earlier patches o Fix bug were journal pages were being treated as movable o Bias placement of non-movable pages to lower PFNs o More agressive clustering of reclaimable pages in reactions to workloads like updatedb that flood the size of inode caches Changelog Since V27 o Renamed anti-fragmentation to Page Clustering. Anti-fragmentation was giving the mistaken impression that it was the 100% solution for high order allocations. Instead, it greatly increases the chances high-order allocations will succeed and lays the foundation for defragmentation and memory hot-remove to work properly o Redefine page groupings based on ability to migrate or reclaim instead of basing on reclaimability alone o Get rid of spurious inits o Per-cpu lists are no longer split up per-type. Instead the per-cpu list is searched for a page of the appropriate type o Added more explanation commentary o Fix up bug in pageblock code where bitmap was used before being initalised Changelog Since V26 o Fix double init of lists in setup_pageset Changelog Since V25 o Fix loop order of for_each_rclmtype_order so that order of loop matches args o gfpflags_to_rclmtype uses gfp_t instead of unsigned long o Rename get_pageblock_type() to get_page_rclmtype() o Fix alignment problem in move_freepages() o Add mechanism for assigning flags to blocks of pages instead of page->flags o On fallback, do not examine the preferred list of free pages a second time The purpose of these patches is to reduce external fragmentation by grouping pages of related types together. When pages are migrated (or reclaimed under memory pressure), large contiguous pages will be freed. This patch works by categorising allocations by their ability to migrate; Movable - The pages may be moved with the page migration mechanism. These are generally userspace pages. Reclaimable - These are allocations for some kernel caches that are reclaimable or allocations that are known to be very short-lived. Unmovable - These are pages that are allocated by the kernel that are not trivially reclaimed. For example, the memory allocated for a loaded module would be in this category. By default, allocations are considered to be of this type HighAtomic - These are high-order allocations belonging to callers that cannot sleep or perform any IO. In practice, this is restricted to jumbo frame allocation for network receive. It is assumed that the allocations are short-lived Instead of having one MAX_ORDER-sized array of free lists in struct free_area, there is one for each type of reclaimability. Once a 2^MAX_ORDER block of pages is split for a type of allocation, it is added to the free-lists for that type, in effect reserving it. Hence, over time, pages of the different types can be clustered together. When the preferred freelists are expired, the largest possible block is taken from an alternative list. Buddies that are split from that large block are placed on the preferred allocation-type freelists to mitigate fragmentation. This implementation gives best-effort for low fragmentation in all zones. Ideally, min_free_kbytes needs to be set to a value equal to 4 * (1 << (MAX_ORDER-1)) pages in most cases. This would be 16384 on x86 and x86_64 for example. Our tests show that about 60-70% of physical memory can be allocated on a desktop after a few days uptime. In benchmarks and stress tests, we are finding that 80% of memory is available as contiguous blocks at the end of the test. To compare, a standard kernel was getting < 1% of memory as large pages on a desktop and about 8-12% of memory as large pages at the end of stress tests. Following this email are 12 patches that implement thie page grouping feature. The first patch introduces a mechanism for storing flags related to a whole block of pages. Then allocations are split between movable and all other allocations. Following that are patches to deal with per-cpu pages and make the mechanism configurable. The next patch moves free pages between lists when partially allocated blocks are used for pages of another migrate type. The second last patch groups reclaimable kernel allocations such as inode caches together. The final patch related to groupings keeps high-order atomic allocations. The last two patches are more concerned with control of fragmentation. The second last patch biases placement of non-movable allocations towards the start of memory. This is with a view of supporting memory hot-remove of DIMMs with higher PFNs in the future. The biasing could be enforced a lot heavier but it would cost. The last patch agressively clusters reclaimable pages like inode caches together. The fragmentation reduction strategy needs to track if pages within a block can be moved or reclaimed so that pages are freed to the appropriate list. This patch adds a bitmap for flags affecting a whole a MAX_ORDER block of pages. In non-SPARSEMEM configurations, the bitmap is stored in the struct zone and allocated during initialisation. SPARSEMEM statically allocates the bitmap in a struct mem_section so that bitmaps do not have to be resized during memory hotadd. This wastes a small amount of memory per unused section (usually sizeof(unsigned long)) but the complexity of dynamically allocating the memory is quite high. Additional credit to Andy Whitcroft who reviewed up an earlier implementation of the mechanism an suggested how to make it a *lot* cleaner. Signed-off-by: Mel Gorman <mel@csn.ul.ie> Cc: Andy Whitcroft <apw@shadowen.org> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Diffstat (limited to 'mm/page_alloc.c')
-rw-r--r--mm/page_alloc.c111
1 files changed, 111 insertions, 0 deletions
diff --git a/mm/page_alloc.c b/mm/page_alloc.c
index e69f19e841e5..332f46d70d2d 100644
--- a/mm/page_alloc.c
+++ b/mm/page_alloc.c
@@ -2931,6 +2931,41 @@ static void __meminit calculate_node_totalpages(struct pglist_data *pgdat,
2931 realtotalpages); 2931 realtotalpages);
2932} 2932}
2933 2933
2934#ifndef CONFIG_SPARSEMEM
2935/*
2936 * Calculate the size of the zone->blockflags rounded to an unsigned long
2937 * Start by making sure zonesize is a multiple of MAX_ORDER-1 by rounding up
2938 * Then figure 1 NR_PAGEBLOCK_BITS worth of bits per MAX_ORDER-1, finally
2939 * round what is now in bits to nearest long in bits, then return it in
2940 * bytes.
2941 */
2942static unsigned long __init usemap_size(unsigned long zonesize)
2943{
2944 unsigned long usemapsize;
2945
2946 usemapsize = roundup(zonesize, MAX_ORDER_NR_PAGES);
2947 usemapsize = usemapsize >> (MAX_ORDER-1);
2948 usemapsize *= NR_PAGEBLOCK_BITS;
2949 usemapsize = roundup(usemapsize, 8 * sizeof(unsigned long));
2950
2951 return usemapsize / 8;
2952}
2953
2954static void __init setup_usemap(struct pglist_data *pgdat,
2955 struct zone *zone, unsigned long zonesize)
2956{
2957 unsigned long usemapsize = usemap_size(zonesize);
2958 zone->pageblock_flags = NULL;
2959 if (usemapsize) {
2960 zone->pageblock_flags = alloc_bootmem_node(pgdat, usemapsize);
2961 memset(zone->pageblock_flags, 0, usemapsize);
2962 }
2963}
2964#else
2965static void inline setup_usemap(struct pglist_data *pgdat,
2966 struct zone *zone, unsigned long zonesize) {}
2967#endif /* CONFIG_SPARSEMEM */
2968
2934/* 2969/*
2935 * Set up the zone data structures: 2970 * Set up the zone data structures:
2936 * - mark all pages reserved 2971 * - mark all pages reserved
@@ -3011,6 +3046,7 @@ static void __meminit free_area_init_core(struct pglist_data *pgdat,
3011 if (!size) 3046 if (!size)
3012 continue; 3047 continue;
3013 3048
3049 setup_usemap(pgdat, zone, size);
3014 ret = init_currently_empty_zone(zone, zone_start_pfn, 3050 ret = init_currently_empty_zone(zone, zone_start_pfn,
3015 size, MEMMAP_EARLY); 3051 size, MEMMAP_EARLY);
3016 BUG_ON(ret); 3052 BUG_ON(ret);
@@ -3991,4 +4027,79 @@ EXPORT_SYMBOL(pfn_to_page);
3991EXPORT_SYMBOL(page_to_pfn); 4027EXPORT_SYMBOL(page_to_pfn);
3992#endif /* CONFIG_OUT_OF_LINE_PFN_TO_PAGE */ 4028#endif /* CONFIG_OUT_OF_LINE_PFN_TO_PAGE */
3993 4029
4030/* Return a pointer to the bitmap storing bits affecting a block of pages */
4031static inline unsigned long *get_pageblock_bitmap(struct zone *zone,
4032 unsigned long pfn)
4033{
4034#ifdef CONFIG_SPARSEMEM
4035 return __pfn_to_section(pfn)->pageblock_flags;
4036#else
4037 return zone->pageblock_flags;
4038#endif /* CONFIG_SPARSEMEM */
4039}
4040
4041static inline int pfn_to_bitidx(struct zone *zone, unsigned long pfn)
4042{
4043#ifdef CONFIG_SPARSEMEM
4044 pfn &= (PAGES_PER_SECTION-1);
4045 return (pfn >> (MAX_ORDER-1)) * NR_PAGEBLOCK_BITS;
4046#else
4047 pfn = pfn - zone->zone_start_pfn;
4048 return (pfn >> (MAX_ORDER-1)) * NR_PAGEBLOCK_BITS;
4049#endif /* CONFIG_SPARSEMEM */
4050}
4051
4052/**
4053 * get_pageblock_flags_group - Return the requested group of flags for the MAX_ORDER_NR_PAGES block of pages
4054 * @page: The page within the block of interest
4055 * @start_bitidx: The first bit of interest to retrieve
4056 * @end_bitidx: The last bit of interest
4057 * returns pageblock_bits flags
4058 */
4059unsigned long get_pageblock_flags_group(struct page *page,
4060 int start_bitidx, int end_bitidx)
4061{
4062 struct zone *zone;
4063 unsigned long *bitmap;
4064 unsigned long pfn, bitidx;
4065 unsigned long flags = 0;
4066 unsigned long value = 1;
4067
4068 zone = page_zone(page);
4069 pfn = page_to_pfn(page);
4070 bitmap = get_pageblock_bitmap(zone, pfn);
4071 bitidx = pfn_to_bitidx(zone, pfn);
4072
4073 for (; start_bitidx <= end_bitidx; start_bitidx++, value <<= 1)
4074 if (test_bit(bitidx + start_bitidx, bitmap))
4075 flags |= value;
3994 4076
4077 return flags;
4078}
4079
4080/**
4081 * set_pageblock_flags_group - Set the requested group of flags for a MAX_ORDER_NR_PAGES block of pages
4082 * @page: The page within the block of interest
4083 * @start_bitidx: The first bit of interest
4084 * @end_bitidx: The last bit of interest
4085 * @flags: The flags to set
4086 */
4087void set_pageblock_flags_group(struct page *page, unsigned long flags,
4088 int start_bitidx, int end_bitidx)
4089{
4090 struct zone *zone;
4091 unsigned long *bitmap;
4092 unsigned long pfn, bitidx;
4093 unsigned long value = 1;
4094
4095 zone = page_zone(page);
4096 pfn = page_to_pfn(page);
4097 bitmap = get_pageblock_bitmap(zone, pfn);
4098 bitidx = pfn_to_bitidx(zone, pfn);
4099
4100 for (; start_bitidx <= end_bitidx; start_bitidx++, value <<= 1)
4101 if (flags & value)
4102 __set_bit(bitidx + start_bitidx, bitmap);
4103 else
4104 __clear_bit(bitidx + start_bitidx, bitmap);
4105}