Skip to contentSkip to conversation

mm: Add cache coloring mechanism

PATCH RESEND In review Łukasz Daniluk 3 participants 9 messages Aug 23, 2017 – Aug 25, 2017

Subsystems (7)
lore
Share & tools
lore
#0 lore raw

cover mm: Add cache coloring mechanism

Patches resend with Linux Kernel Mailing List added correctly this time.

This patch series adds cache coloring mechanism that works along buddy allocator. The solution is opt-in, disabled by default minimally interferes with default allocation paths due to added if statements.

Why would such patches be needed? Big caches with low associativity (direct mapped caches, 2-way associative) will benefit from the solution the most - it allows for near constant performance through the lifetime of a system, despite the allocations and deallocations happening and reordering buddy lists.

On KNL system, the STREAM benchmark with problem size resulting in its internal arrays being of 16GB size will yield bandwidth performance of 336GB/s after fresh boot. With cache coloring patches applied and enabled, this performance stays near constant (most 1.5% drop observed), despite running benchmark multiple times with varying sizes over course of days. Without these patches however, the bandwidth when using such allocations drops to 117GB/s - over 65% of irrecoverable performance penalty. Workloads that exceed set cache size suffer from decreased randomization of allocations with cache coloring enabled, but effect of cache usage disappears roughly at the same allocation size.

Solution is divided into three patches. First patch is a preparatory one that provides interface for retrieving (information about) free lists contained by particular free_area structure. Second one (parallel structure keeping separate list_heads for each cache color in a given context) shows general solution overview and is working as it is. However, it has serious performance implications with bigger caches due to linear search for next color to be used during allocations. Third patch (sorting list_heads using RB trees) aims to improve solution's performance by replacing linear search for next color with searching in RB tree. While improving computational performance, it imposes increased memory cost of the solution.

A?ukasz Daniluk (3):
mm: move free_list selection to dedicated functions
mm: Add page colored allocation path
mm: Add helper rbtree to search for next cache color

 Documentation/admin-guide/kernel-parameters.txt |   8 +
 include/linux/mmzone.h                          |  12 +-
 mm/compaction.c                                 |   4 +-
 mm/page_alloc.c                                 | 381 ++++++++++++++++++++++--
 mm/vmstat.c                                     |  10 +-
 5 files changed, 383 insertions(+), 32 deletions(-)
Signature · 7 lines
2.13.3

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected].  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
#1 lore raw

PATCH RESEND 1/3 mm: move free_list selection to dedicated functions

Currently free_list selection from particular free_area was done on principle that there is only one free_list per order per migratetype. This patch is preparation for page coloring solution utilising multiple free_lists.

---
 include/linux/mmzone.h |  4 ++++
 mm/compaction.c        |  4 ++--
 mm/page_alloc.c        | 61 +++++++++++++++++++++++++++++++++++++-------------
 mm/vmstat.c            | 10 +++------
 4 files changed, 55 insertions(+), 24 deletions(-)
include/linux/mmzone.h +4 -0
index fc14b8b3f6ce..04128890a684 100644
@@ -910,6 +910,10 @@ extern struct pglist_data contig_page_data;
 
 #endif /* !CONFIG_NEED_MULTIPLE_NODES */
 
+extern bool area_empty(struct free_area *area, int order, int migratetype);
+extern unsigned long area_free_count(struct free_area *area, int order,
+					int migratetype);
+
 extern struct pglist_data *first_online_pgdat(void);
 extern struct pglist_data *next_online_pgdat(struct pglist_data *pgdat);
 extern struct zone *next_zone(struct zone *zone);
mm/compaction.c +2 -2
index fb548e4c7bd4..8f6274c0a04b 100644
@@ -1334,13 +1334,13 @@ static enum compact_result __compact_finished(struct zone *zone,
 		bool can_steal;
 
 		/* Job done if page is free of the right migratetype */
-		if (!list_empty(&area->free_list[migratetype]))
+		if (!area_empty(area, order, migratetype))
 			return COMPACT_SUCCESS;
 
 #ifdef CONFIG_CMA
 		/* MIGRATE_MOVABLE can fallback on MIGRATE_CMA */
 		if (migratetype == MIGRATE_MOVABLE &&
-			!list_empty(&area->free_list[MIGRATE_CMA]))
+			!area_empty(area, order, MIGRATE_CMA))
 			return COMPACT_SUCCESS;
 #endif
 		/*
mm/page_alloc.c +46 -15

142 lines not on this page · raw message

mm/vmstat.c +3 -7
index 9a4441bbeef2..1ff86656cb2e 100644
@@ -1183,14 +1183,10 @@ static void pagetypeinfo_showfree_print(struct seq_file *m,
 					zone->name,
 					migratetype_names[mtype]);
 		for (order = 0; order < MAX_ORDER; ++order) {
-			unsigned long freecount = 0;
-			struct free_area *area;
-			struct list_head *curr;
+			struct free_area *area = &(zone->free_area[order]);
+			unsigned long freecount =
+				area_free_count(area, order, mtype);
 
-			area = &(zone->free_area[order]);
-
-			list_for_each(curr, &area->free_list[mtype])
-				freecount++;
 			seq_printf(m, "%6lu ", freecount);
 		}
 		seq_putc(m, '\n');
Signature · 7 lines
2.13.3

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected].  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
#2 lore raw

PATCH RESEND 2/3 mm: Add page colored allocation path

In reply to Łukasz Daniluk

Create alternative path when requesting free_list from buddy system that takes recent allocations or physical page offset into account.

This solution carries a cost of additional, early memory allocations done by kernel that is dependent on cache size and minimum order to be colored.

---
 Documentation/admin-guide/kernel-parameters.txt |   8 +
 include/linux/mmzone.h                          |   5 +-
 mm/page_alloc.c                                 | 253 +++++++++++++++++++++---
 3 files changed, 243 insertions(+), 23 deletions(-)
Documentation/admin-guide/kernel-parameters.txt +8 -0
index 372cc66bba23..4648f1cc6665 100644
@@ -455,6 +455,14 @@
 			possible to determine what the correct size should be.
 			This option provides an override for these situations.
 
+	cache_color_size=
+			[KNL] Set cache size for purposes of cache coloring
+			mechanism in buddy allocator.
+
+	cache_color_min_order=
+			[KNL] Set minimal order for which page coloring
+			mechanism will be enabled in buddy allocator.
+
 	ca_keys=	[KEYS] This parameter identifies a specific key(s) on
 			the system trusted keyring to be used for certificate
 			trust validation.
include/linux/mmzone.h +4 -1
index 04128890a684..d10a5421b18b 100644
@@ -25,7 +25,8 @@
 #else
 #define MAX_ORDER CONFIG_FORCE_MAX_ZONEORDER
 #endif
-#define MAX_ORDER_NR_PAGES (1 << (MAX_ORDER - 1))
+#define ORDER_NR_PAGES(order) (1 << (order))
+#define MAX_ORDER_NR_PAGES ORDER_NR_PAGES(MAX_ORDER - 1)
 
 /*
  * PAGE_ALLOC_COSTLY_ORDER is the order at which allocations are deemed
@@ -94,6 +95,8 @@ extern int page_group_by_mobility_disabled;
 
 struct free_area {
 	struct list_head	free_list[MIGRATE_TYPES];
+	struct list_head	*colored_free_list[MIGRATE_TYPES];
+	unsigned long		next_color[MIGRATE_TYPES];
 	unsigned long		nr_free;
 };
 
mm/page_alloc.c +231 -22

346 lines not on this page · raw message

Signature · 7 lines
2.13.3

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected].  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
#4 lore raw

PATCH RESEND 3/3 mm: Add helper rbtree to search for next cache color

In reply to Łukasz Daniluk

Before this patch search for next available cache color was done linearly. This kind of search is problematic if the contents are sparse or there are no contents at all.

This patch aims to fix this problem by arranging the free_lists that take part in cache aware allocations in the RB tree structure.

In order for the solution to work properly, space for free_lists and RB tree nodes has to be allocated. Required space is 5 pointers per color (struct rb_node, struct list_head).

Cost of the solution with RB tree helpers can be estimated as:
5 * MIGRATE_TYPES * cache_colors(order), per zone per node, for each order that we wish to be affected. For example 16GB cache with only order 10 enabled requires 160KB per zone type, migratetype, node; whereas enabling this feature for order 9 will bump this number up to 480KB per zone type, migratetype.

---
 include/linux/mmzone.h |   5 +-
 mm/page_alloc.c        | 155 ++++++++++++++++++++++++++++++++++++++++---------
 2 files changed, 130 insertions(+), 30 deletions(-)
include/linux/mmzone.h +4 -1
index d10a5421b18b..cda726854078 100644
@@ -6,6 +6,7 @@
 
 #include <linux/spinlock.h>
 #include <linux/list.h>
+#include <linux/rbtree.h>
 #include <linux/wait.h>
 #include <linux/bitops.h>
 #include <linux/cache.h>
@@ -93,9 +94,11 @@ extern int page_group_by_mobility_disabled;
 	get_pfnblock_flags_mask(page, page_to_pfn(page),		\
 			PB_migrate_end, MIGRATETYPE_MASK)
 
+struct cache_color;
 struct free_area {
 	struct list_head	free_list[MIGRATE_TYPES];
-	struct list_head	*colored_free_list[MIGRATE_TYPES];
+	struct rb_root		cache_colored_free_lists[MIGRATE_TYPES];
+	struct cache_color	*cache_colors[MIGRATE_TYPES];
 	unsigned long		next_color[MIGRATE_TYPES];
 	unsigned long		nr_free;
 };
mm/page_alloc.c +126 -29

230 lines not on this page · raw message

Signature · 7 lines
2.13.3

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected].  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
#3 lore raw

Re: [RESEND PATCH 2/3] mm: Add page colored allocation path

In reply to Łukasz Daniluk

Review

Quoted 7 lines · A?ukasz Daniluk
(quoted diff) +7 -0
+	cache_color_size=
+			[KNL] Set cache size for purposes of cache coloring
+			mechanism in buddy allocator.
+
+	cache_color_min_order=
+			[KNL] Set minimal order for which page coloring
+			mechanism will be enabled in buddy allocator.

I guess I should send along the code I've been playing with. I have this broken out into a bunch of helper patches, but I'll just attach the combined patch.

This also uses an rbtree, but it puts 'struct page' itself in the rbtree. Further, it reuses the zone->free_area list_heads' storage for the rbtree head. This means no additional space overhead and you can also enable it at runtime without boot options. You can also have it enabled for any order(s) you want.

The rbtree(s) you've grafted on will not need to be walked or rebalanced as much as the ones in my version, so that's a plus for your version.

The trick with either of these is trying to make sure the cost of all the new branches very low.

include/linux/mm_types.h +12 -0
index 45cdb27..b7dd66d 100644
@@ -104,6 +104,16 @@ struct page {
 		};
 	};
 
+	union {
+		/*
+		 * This is fugly, but it works for now.
+		 *
+		 * An rb_node needs three pointers.  Take
+		 * the space from page->lru (2) and
+		 * page->private (1).
+		 */
+		struct rb_node rb;
+	struct {
 	/*
 	 * Third double word block
 	 *
@@ -185,7 +195,9 @@ struct page {
 #endif
 		struct kmem_cache *slab_cache;	/* SL[AU]B: Pointer to slab */
 	};
+	};
 
+	};
 #ifdef CONFIG_MEMCG
 	struct mem_cgroup *mem_cgroup;
 #endif
include/linux/mmzone.h +15 -1
index ef6a13b..c70cb02 100644
@@ -17,6 +17,7 @@
 #include <linux/pageblock-flags.h>
 #include <linux/page-flags-layout.h>
 #include <linux/atomic.h>
+#include <linux/rbtree.h>
 #include <asm/page.h>
 
 /* Free memory management - zoned buddy allocator.  */
@@ -92,8 +93,18 @@ static inline bool is_migrate_movable(int mt)
 	get_pfnblock_flags_mask(page, page_to_pfn(page),		\
 			PB_migrate_end, MIGRATETYPE_MASK)
 
+union free_area_list {
+	/* Used for orders where !tree_free_order(): */
+	struct list_head list;
+	/* Used for orders where tree_free_order()==true: */
+	struct {
+		struct rb_root rb_root;
+		unsigned long  rb_color;
+	};
+};
+
 struct free_area {
-	struct list_head	free_list[MIGRATE_TYPES];
+	union free_area_list	free_pages[MIGRATE_TYPES];
 	unsigned long		nr_free;
 };
 
@@ -828,6 +839,9 @@ static inline bool populated_zone(struct zone *zone)
 
 extern int movable_zone;
 
+extern unsigned long free_page_count(struct zone *zone, int order, int mt);
+extern bool free_area_empty(int order, struct free_area *area, int mt);
+
 #ifdef CONFIG_HIGHMEM
 static inline int zone_movable_is_highmem(void)
 {
kernel/crash_core.c +2 -2
index fcbd568..fd1bd46 100644
@@ -408,14 +408,14 @@ static int __init crash_save_vmcoreinfo_init(void)
 	VMCOREINFO_OFFSET(zone, free_area);
 	VMCOREINFO_OFFSET(zone, vm_stat);
 	VMCOREINFO_OFFSET(zone, spanned_pages);
-	VMCOREINFO_OFFSET(free_area, free_list);
+	VMCOREINFO_OFFSET(free_area, free_pages);
 	VMCOREINFO_OFFSET(list_head, next);
 	VMCOREINFO_OFFSET(list_head, prev);
 	VMCOREINFO_OFFSET(vmap_area, va_start);
 	VMCOREINFO_OFFSET(vmap_area, list);
 	VMCOREINFO_LENGTH(zone.free_area, MAX_ORDER);
 	log_buf_vmcoreinfo_setup();
-	VMCOREINFO_LENGTH(free_area.free_list, MIGRATE_TYPES);
+	VMCOREINFO_LENGTH(free_area.free_pages, MIGRATE_TYPES);
 	VMCOREINFO_NUMBER(NR_FREE_PAGES);
 	VMCOREINFO_NUMBER(PG_lru);
 	VMCOREINFO_NUMBER(PG_private);
kernel/sysctl.c +14 -0
index 4dfba1a..c48229a 100644
@@ -113,6 +113,10 @@
 #ifndef CONFIG_MMU
 extern int sysctl_nr_trim_pages;
 #endif
+extern int sysctl_tree_free_order;
+extern int tree_free_order_sysctl_handler(struct ctl_table *table, int write,
+		void __user *buffer, size_t *length, loff_t *ppos);
+
 
 /* Constants used for minimum and  maximum */
 #ifdef CONFIG_LOCKUP_DETECTOR
@@ -134,6 +138,7 @@
 #ifdef CONFIG_PERF_EVENTS
 static int six_hundred_forty_kb = 640 * 1024;
 #endif
+static int max_order_minus_one = MAX_ORDER-1;
 
 /* this is needed for the proc_doulongvec_minmax of vm_dirty_bytes */
 static unsigned long dirty_bytes_min = 2 * PAGE_SIZE;
@@ -1387,6 +1392,15 @@ static int sysrq_sysctl_handler(struct ctl_table *table, int write,
 		.extra1		= &one,
 		.extra2		= &four,
 	},
+	{
+		.procname	= "tree_free_order",
+		.data		= &sysctl_tree_free_order,
+		.maxlen		= sizeof(int),
+		.mode		= 0644,
+		.proc_handler	= tree_free_order_sysctl_handler,
+		.extra1		= &neg_one,
+		.extra2		= &max_order_minus_one,
+	},
 #ifdef CONFIG_COMPACTION
 	{
 		.procname	= "compact_memory",
mm/compaction.c +2 -2
index 613c59e..73c0e71 100644
@@ -1335,13 +1335,13 @@ static enum compact_result __compact_finished(struct zone *zone,
 		bool can_steal;
 
 		/* Job done if page is free of the right migratetype */
-		if (!list_empty(&area->free_list[migratetype]))
+		if (!free_area_empty(order, area, migratetype))
 			return COMPACT_SUCCESS;
 
 #ifdef CONFIG_CMA
 		/* MIGRATE_MOVABLE can fallback on MIGRATE_CMA */
 		if (migratetype == MIGRATE_MOVABLE &&
-			!list_empty(&area->free_list[MIGRATE_CMA]))
+			!free_area_empty(order, area, MIGRATE_CMA))
 			return COMPACT_SUCCESS;
 #endif
 		/*
mm/internal.h +1 -1
index 0e4f558..716dd90 100644
@@ -227,7 +227,7 @@ int find_suitable_fallback(struct free_area *area, unsigned int order,
 static inline unsigned int page_order(struct page *page)
 {
 	/* PageBuddy() must be checked by the caller */
-	return page_private(page);
+	return page->index;
 }
 
 /*
mm/page_alloc.c +480 -30

642 lines not on this page · raw message

mm/vmstat.c +2 -9
index 76f7367..18f23c1 100644
@@ -1180,15 +1180,8 @@ static void pagetypeinfo_showfree_print(struct seq_file *m,
 					zone->name,
 					migratetype_names[mtype]);
 		for (order = 0; order < MAX_ORDER; ++order) {
-			unsigned long freecount = 0;
-			struct free_area *area;
-			struct list_head *curr;
-
-			area = &(zone->free_area[order]);
-
-			list_for_each(curr, &area->free_list[mtype])
-				freecount++;
-			seq_printf(m, "%6lu ", freecount);
+			seq_printf(m, "%6lu ",
+					free_page_count(zone, order, mtype));
 		}
 		seq_putc(m, '\n');
 	}
#5 lore raw
In reply to Łukasz Daniluk
Quoted 11 lines · A?ukasz Daniluk

Patches resend with Linux Kernel Mailing List added correctly this time.

This patch series adds cache coloring mechanism that works along buddy allocator. The solution is opt-in, disabled by default minimally interferes with default allocation paths due to added if statements.

Why would such patches be needed? Big caches with low associativity (direct mapped caches, 2-way associative) will benefit from the solution the most - it allows for near constant performance through the lifetime of a system, despite the allocations and deallocations happening and reordering buddy lists.

So the obvious question, what about THPs? Their size should be enough to contain all the colors with current caches, no? Even on KNL I didn't find more than "32x 1 MB 16-way L2 caches". This is in addition to the improved TLB performance, which you want to get as well for such workloads?

Quoted 10 lines

On KNL system, the STREAM benchmark with problem size resulting in its internal arrays being of 16GB size will yield bandwidth performance of 336GB/s after fresh boot. With cache coloring patches applied and enabled, this performance stays near constant (most 1.5% drop observed), despite running benchmark multiple times with varying sizes over course of days. Without these patches however, the bandwidth when using such allocations drops to 117GB/s - over 65% of irrecoverable performance penalty. Workloads that exceed set cache size suffer from decreased randomization of allocations with cache coloring enabled, but effect of cache usage disappears roughly at the same allocation size.

So was the test with THP's enabled or disabled? And what was the cache configuration and the values of cache_color_size and cache_color_min_order parameters?

I'm also confused about the "cache_color_min_order=" parameter. If this wants to benefit non-THP userspace, then you would need to set it to 0, right? Or does this mean that indeed you expect THP to not contain all the colors, so you'd set it to the THP order (9)?

Quoted 18 lines

Solution is divided into three patches. First patch is a preparatory one that provides interface for retrieving (information about) free lists contained by particular free_area structure. Second one (parallel structure keeping separate list_heads for each cache color in a given context) shows general solution overview and is working as it is. However, it has serious performance implications with bigger caches due to linear search for next color to be used during allocations. Third patch (sorting list_heads using RB trees) aims to improve solution's performance by replacing linear search for next color with searching in RB tree. While improving computational performance, it imposes increased memory cost of the solution.

A?ukasz Daniluk (3):
mm: move free_list selection to dedicated functions
mm: Add page colored allocation path
mm: Add helper rbtree to search for next cache color

Quoted 7 lines
 Documentation/admin-guide/kernel-parameters.txt |   8 +
 include/linux/mmzone.h                          |  12 +-
 mm/compaction.c                                 |   4 +-
 mm/page_alloc.c                                 | 381 ++++++++++++++++++++++--
 mm/vmstat.c                                     |  10 +-
 5 files changed, 383 insertions(+), 32 deletions(-)

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

#6 lore raw
Quoted 4 lines · Vlastimil Babka

So the obvious question, what about THPs? Their size should be enough to contain all the colors with current caches, no? Even on KNL I didn't find more than "32x 1 MB 16-way L2 caches". This is in addition to the improved TLB performance, which you want to get as well for such workloads?

The cache in this case is "MCDRAM" which is 16GB in size. It can be used as normal RAM or a cache. This patch deals with when "MCDRAM" is in its cache mode.

It's described in the "Memory Modes" slide here:

Quoted 1 line

http://www.nersc.gov/users/computational-systems/cori/configuration/knl-processor-modes/

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

#7 lore raw
Quoted 9 lines · Dave Hansen
Quoted 4 lines · Vlastimil Babka

So the obvious question, what about THPs? Their size should be enough to contain all the colors with current caches, no? Even on KNL I didn't find more than "32x 1 MB 16-way L2 caches". This is in addition to the improved TLB performance, which you want to get as well for such workloads?

The cache in this case is "MCDRAM" which is 16GB in size. It can be used as normal RAM or a cache. This patch deals with when "MCDRAM" is in its cache mode.

Hm, 16GB direct mapped, that means 8k colors for 2MB THP's. Is that really practical? Wouldn't such workload use 1GB hugetlbfs pages? Then it's still 16 colors to manage, but could be done purely in userspace since they should not move in physical memory and userspace can control where to map each phase in the virtual layout.

Quoted 4 lines

It's described in the "Memory Modes" slide here:

Quoted 1 line

http://www.nersc.gov/users/computational-systems/cori/configuration/knl-processor-modes/

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

#8 lore raw
Quoted 6 lines · Vlastimil Babka
Quoted 5 lines · Dave Hansen
Quoted 4 lines · Vlastimil Babka

So the obvious question, what about THPs? Their size should be enough to contain all the colors with current caches, no? Even on KNL I didn't find more than "32x 1 MB 16-way L2 caches". This is in addition to the improved TLB performance, which you want to get as well for such workloads?

Quoted 8 lines
Quoted 3 lines

The cache in this case is "MCDRAM" which is 16GB in size. It can be used as normal RAM or a cache. This patch deals with when "MCDRAM" is in its cache mode.

Hm, 16GB direct mapped, that means 8k colors for 2MB THP's. Is that really practical? Wouldn't such workload use 1GB hugetlbfs pages? Then it's still 16 colors to manage, but could be done purely in userspace since they should not move in physical memory and userspace can control where to map each phase in the virtual layout.

There are lots of options for applications that are written with specific knowledge of MCDRAM. The easiest option from the kernel's perspective is to just turn the caching mode off and treat MCDRAM as normal RAM (it shows up in a separate NUMA node in that case).

But, one of the reasons for the cache mode in the first place was to support applications that don't have specific knowledge of MCDRAM. Or, even old binaries that were compiled long ago.

In other words, I don't think this is something we can easily punt to userspace.