Skip to contentSkip to conversation

mm: Add cache coloring mechanism

PATCH Superseded Łukasz Daniluk 1 participant 4 messages Aug 22, 2017

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

cover mm: Add cache coloring mechanism

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 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 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>
#3 lore raw

PATCH 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>