Skip to contentSkip to conversation

introduce gcma

RFC Superseded SeongJae Park 2 participants 9 messages Nov 11, 2014 – Nov 12, 2014

Subsystems (3)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

cover introduce gcma

Abstract
========

Current cma(contiguous memory allocator) could not guarantee success and fast latency of contiguous allocation.
This coverletter explains about the problem in detail and suggest new contiguous memory allocator, gcma(guaranteed contiguous allocator).

CMA: Contiguous Memory Allocator
================================

Basic idea of cma
-----------------

Basic idea of cma is as follows. It focuses on memory efficiency while keeping contiguous allocation could be done without serious penalty.

- Reserves large contiguous memory area during boot and let the area could be used by contiguous allocation.
- Because system memory could be inefficient if the reserved memory is not fully utilized by contiguous allocation, let the area could be allocated for 2nd-class clients
- If pages being allocated for 2nd-class clients are necessary for contiguous allocation(doubtless 1st class client), migrates or discard the page and use them for contiguous allocation.

In cma, _2nd-class client_ is movable page. The reserved area could be allocated for movable pages and the movable pages be migrated or discarded if contiguous allocation needs them.

Problem of cma
--------------

This cma mechanism imposes following weaknesses.

1. Allocation failure
CMA could fail to allocate contiguous memory due to following reasons. 1-1. Direct pinning
Any kernel thread could pin any movable pages for a long time. If a movable page which needs to be migrated for a contiguous memory allocation is already pinned by someone, migration could not be completed. In consequence, contiguous allocation could be fail if the page is not be unpinned longtime. 1-2. Indirect pin
If a movable page have dependency with an object, the object would increase reference count of the movable page to assert it is safe to use the page. If a movable page which is needs to be migrated for a contiguous memory allocation is in the case, the page could not be free to be used by contiguous allocation. In consequence, contiguous allocation could be failed.

2. High cost
Contiguous memory allocation of CMA could be expensive by following reasons. 2-1. Function overhead
Most of all, migration itself is not so simple. It should manipulate rmap and copy content of the pages into another pages. It could require relatively long time.
After migration, migrated pages be inserted in head of LRU page list again though it was not be used, just migrated. In that case, the pages on LRU list is not ordered in LRU degree. In consequence, system performance could be degraded because working set pages could be swapped-out by the abnormal LRU list.
2-2. Writeback cost
If the page which needs to be discarded for contiguous memory allocation was dirty, it should be written-back to mapped file. Latency of write-back is usually not predictably high because it depends on not only memory management, but also block layer, file system and block device h/w characteristic.

In short, cma doesn't guarantee success and fast latency of contiguous memory allocation. And, the core cause is the fact that cma chosen 2nd-class client(movable pages) were not nice(hard to migrate / discard) enough.

The problem was discussed in detail from [1] and [2].

GCMA: Guaranteed contiguous memory allocator
============================================

Goal of gcma is to solve those two weaknesses of cma discussed above. In other words, gcma aims to guarantee success and fast latency of contiguous memory allocation.

Basic idea
----------

Basic idea of gcma is as same as cma's. It reserves large contiguous memory area during boot and use it for contiguous memory allocator while let it be allocated for 2nd-class clients to keep memory efficiency. If the pages allocated for 2nd-class clients necessary for contiguous allocation(doubtless 1st-class client), discard or migrate them.

Difference with cma is choice and operation of 2nd-class client. In gcma, 2nd-class client should allocate pages from the reserved area only if the allocated pages mets following conditions.

1. Out of Kernel
If a page is out of kernel scope, the page could be handled by the 2nd-class client only and no others could see, touch or hold it. Those pages could be discarded anytime. In consequence, contiguous allocation could not be fail if 2nd-class client cooperates well.
2. Quickly discardable or migratable
The pages being used by 2nd-class client should be Quickly discardable of migratable. If so, the contiguous allocation could guarantee fast latency.

With above conditions, we picked 2 candidates for gcma 2nd-class clients. Frontswap and cleancache are them.

Frontswap backend
-----------------

1. Out of Kernel
Pages inside frontswap backend is swapped-out pages, which are out of kernel.

2. Quickly discardable or migratable
Pages inside frontswap backend could be discarded using following policies. 2.1. Write-back
After the pages written-back containing data to backed real swap device, the page could be free without any interference.
In this policy, latency of write-back operation could be bounded to swap device write speed.
2.2. Write-through
Frontswap could be run with write-through mode. In this case, any pages in frontswap backend could be free immediately because the data is already in swap device.
This policy could show very fast speed but could make whole system slow due to frequent write-through. In flash storage based system, it could cause the storage system failure unless it do wear-leveling on swap device. 2.3. Put-back
When pages inside frontswap backend need to be discarded, gcma could allocates pages from system memroy(not reserved memory) and copy content of discarding pages into newly allocated page. After that, put those newly-allocated, data copied pages inside swap cache to let them in frontswap backend again. After that, the discarding pages are free.
Because it do only memory-operation, speed would not be too slow. We call the operation as _put-back_.

Cleancache
----------

1. Out of Kernel
Pages inside clean cache is clean pages evicted from page cache, which means out of kernel.

2. Quickly discardable or migratable
Because pages inside clean cache is clean, it could be free immediately without any additional operation.

Current RFC implementation
==========================

Though we suggested 2 candidates and various policy for fast discarding, current RFC implements gcma using only frontswap / write-through policy naively because this is a prototype of prototype for various opinions of reviewers.

At the moment, current naive implementation is as follows:
1) Reserves large amount of memory during boot.
2) Allow the memory to write-through mode frontswap and contiguous memory allocation.
3) Drain pages being used for the frontswap if contiguous memroy allocation needs.

As discussed above, this implementation could introduces clear trade-off:
1) System performance could be degraded due to write-through mode
2) Flash storage using system should worry about wear-leveling

Configuring swap device using zram could be helpful to alleviate those problems though the trade-off still exist.

Basic concept, implementation, and performance evaluation result were presented in detail at [2].

Disclaimer
==========

Because cma and gcma has clear merits and demerits, gcma aims to be coexists with cma rather than alternates it. Users could operate cma and gcma on a system concurrently and could use them as they need.

Performance Evaluation
======================

Machine Setting
---------------

CuBox i4 Pro
- ARM v7, 4 * 1 GHz cores
- 800 MiB DDR3 RAM (Originally 2 GiB equipped.)
- Class 10 SanDisk 16 GiB microSD card

Evaluation Variants
-------------------

- Baseline: Linux v3.17, 128 MiB swap
- cma: Baseline + 256 MiB CMA area
- gcma: Baseline + 256 MiB GCMA area
- gcma.zram: GCMA + 128 MiB zram swap device

Workloads
---------

- Background workload: make defconfig && time make -j 16 with Linux v3.12.6
- Foreground workload: Request 1-32000 contiguous page allocation 32 times

Evaluation Result
-----------------

[ Latency (u-seconds) ]
Results below shows gcma's latency is significantly lower than cma's. Note that cma max latency reaches more than 4 seconds easily.

		cma			gcma			gcma.zram

nr_pages min max avg min max avg min max avg
1 383 53397 15737 13 43 13 13 34 13
512 578 3909212 135736 384 588 411 384 3326385 104419
1024 3074 4277142 386083 763 15580 1433 766 42521 3548
2048 3862 3334665 246806 1564 41844 3158 1536 11930 2379
4096 2502 3813997 266966 3122 10491 3608 3081 13155 3793
8192 12244 4196931 656029 6152 10682 6903 6154 37543 8406
16384 5447 4071272 853303 12544 50947 15647 12499 16819 13522
32000 18505 4293604 1102669 25427 62671 29331 25354 65421 28721

[ Background workload performance ]
Background workload(kernel build) result measured to evaluate system performance degradation cma / gcma affects.
original means background workload result on CMA configuration without foreground(contiguous allocation) workload.

cma and gcma degraded system performance due to page migration / write-through and affected kernel build workload performance while gcma with zram swap device shows alleviated performance degradation.

user system elapsed cpu
original 1702.98 169.41 08:32.13 365
cma 1723.13 187.21 09:25.46 337
gcma 1720.95 174.23 09:27.91 333
gcma.zram 1736.61 171.6 08:50.72 359

[ Evaluation result summary ]
With performance evaluation results above, we can say,
1. latency of gcma is significantly lower then cma's.
2. gcma degrade system performance though zram swap device configuration can abbreviate the effect a little.

NOTE: Appreciates any feedback to this simple idea and implementation though this RFC is not yet matured and ugly a lot.

[1] https://lkml.org/lkml/2013/10/30/16
[2] http://sched.co/1qZcBAO

Really appreciate Minchan who suggested main idea and have helped a lot during development with code fix/review.

SeongJae Park (6):
gcma: introduce contiguous memory allocator
gcma: utilize reserved memory as swap cache
gcma: evict frontswap pages in LRU order when memory is full
gcma: discard swap cache pages to meet successful GCMA allocation
gcma: export statistical data on debugfs
gcma: integrate gcma under cma interface

 include/linux/cma.h  |   4 +
 include/linux/gcma.h |  46 +++
 mm/Kconfig           |  15 +
 mm/Makefile          |   2 +
 mm/cma.c             | 110 +++++--
 mm/gcma.c            | 799 +++++++++++++++++++++++++++++++++++++++++++++++++++
 6 files changed, 953 insertions(+), 23 deletions(-)
 create mode 100644 include/linux/gcma.h
 create mode 100644 mm/gcma.c
Signature · 7 lines
1.9.1

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

RFC 1/6 gcma: introduce contiguous memory allocator

This patch introduces a simple contiguous memory allocator. It's simple bitmap allocator to manage a contiguos memory.

---
 include/linux/gcma.h |  26 ++++++++
 mm/gcma.c            | 173 +++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 199 insertions(+)
 create mode 100644 include/linux/gcma.h
 create mode 100644 mm/gcma.c
include/linux/gcma.h new file +26 -0
new file mode 100644
index 0000000..3016968
@@ -0,0 +1,26 @@
+/*
+ * gcma.h - Guaranteed Contiguous Memory Allocator
+ *
+ * GCMA aims for contiguous memory allocation with success and fast
+ * latency guarantee.
+ * It reserves large amount of memory and let it be allocated to the
+ * contiguous memory request.
+ *
+ * Copyright (C) 2014  LG Electronics Inc.,
+ * Copyright (C) 2014  Minchan Kim <[email protected]>
+ * Copyright (C) 2014  SeongJae Park <[email protected]>
+ */
+
+#ifndef _LINUX_GCMA_H
+#define _LINUX_GCMA_H
+
+struct gcma;
+
+int gcma_init(unsigned long start_pfn, unsigned long size,
+	      struct gcma **res_gcma);
+int gcma_alloc_contig(struct gcma *gcma,
+		      unsigned long start_pfn, unsigned long size);
+void gcma_free_contig(struct gcma *gcma,
+		      unsigned long start_pfn, unsigned long size);
+
+#endif /* _LINUX_GCMA_H */
mm/gcma.c new file +173 -0

173 lines not on this page · raw message

Signature · 7 lines
1.9.1

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

RFC 2/6 gcma: utilize reserved memory as swap cache

In reply to SeongJae Park

GCMA reserves an amount of memory during boot and the memory space should be always available for guest of that area. However, the guest doesn't need it everytime so this patch makes the reserved memory as swap cache via write-through frontswap for memory efficiency.

If the guest declares to need it sometime, we can discard all of swap cache because every data should be on swap disk by write-through frontswap. It makes allocation latency for the guest really small.

The drawback of the approach is that it could degrade system performance due to earlier swapout by reserving if the user makes GCMA area big(e.g., 1/3 of the system memory) and swap-cache hit ratio is low. It's a trade-off for getting guaranteed low latency contiguous memory allocation.

---
 include/linux/gcma.h |   2 +-
 mm/gcma.c            | 330 ++++++++++++++++++++++++++++++++++++++++++++++++++-
 2 files changed, 330 insertions(+), 2 deletions(-)
include/linux/gcma.h +1 -1
index 3016968..d733a9b 100644
@@ -4,7 +4,7 @@
  * GCMA aims for contiguous memory allocation with success and fast
  * latency guarantee.
  * It reserves large amount of memory and let it be allocated to the
- * contiguous memory request.
+ * contiguous memory request and utilize them as swap cache.
  *
  * Copyright (C) 2014  LG Electronics Inc.,
  * Copyright (C) 2014  Minchan Kim <[email protected]>
mm/gcma.c +329 -1

360 lines not on this page · raw message

Signature · 7 lines
1.9.1

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

RFC 3/6 gcma: evict frontswap pages in LRU order when memory is full

In reply to SeongJae Park

GCMA uses free pages of the reserved space as swap cache so sometime we ends up shortage of free space as time goes by and we should drain some pages of swap cache for keeping new swapout pages in cache. For it, GCMA manages swap cache in LRU order so we can keep active pages in memory if possible. It could make swap-cache hit ratio high rather than random evicting.

---
 mm/gcma.c | 93 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++----
 1 file changed, 88 insertions(+), 5 deletions(-)
mm/gcma.c +88 -5
index ddfc0d8..d459116 100644
@@ -19,6 +19,9 @@
 #include <linux/highmem.h>
 #include <linux/gcma.h>
 
+/* XXX: What's the ideal? */
+#define NR_EVICT_BATCH	32
+
 struct gcma {
 	spinlock_t lock;
 	unsigned long *bitmap;
@@ -49,9 +52,13 @@ struct frontswap_tree {
 	spinlock_t lock;
 };
 
+static LIST_HEAD(slru_list);	/* LRU list of swap cache */
+static spinlock_t slru_lock;	/* protect slru_list */
 static struct frontswap_tree *gcma_swap_trees[MAX_SWAPFILES];
 static struct kmem_cache *swap_slot_entry_cache;
 
+static unsigned long evict_frontswap_pages(unsigned long nr_pages);
+
 static struct frontswap_tree *swap_tree(struct page *page)
 {
 	return (struct frontswap_tree *)page->mapping;
@@ -209,6 +216,7 @@ static struct page *frontswap_alloc_page(struct gcma **res_gcma)
 	struct page *page;
 	struct gcma *gcma;
 
+retry:
 	spin_lock(&ginfo.lock);
 	gcma = list_first_entry(&ginfo.head, struct gcma, list);
 	list_move_tail(&gcma->list, &ginfo.head);
@@ -216,13 +224,18 @@ static struct page *frontswap_alloc_page(struct gcma **res_gcma)
 	list_for_each_entry(gcma, &ginfo.head, list) {
 		page = gcma_alloc_page(gcma);
 		if (page) {
-			*res_gcma = gcma;
-			goto out;
+			spin_unlock(&ginfo.lock);
+			goto got;
 		}
 	}
-
-out:
 	spin_unlock(&ginfo.lock);
+
+	/* Failed to alloc a page from entire gcma. Evict adequate LRU
+	 * frontswap slots and try allocation again */
+	if (evict_frontswap_pages(NR_EVICT_BATCH))
+		goto retry;
+
+got:
 	*res_gcma = gcma;
 	return page;
 }
@@ -240,7 +253,7 @@ static void swap_slot_entry_get(struct swap_slot_entry *entry)
 }
 
 /*
- * Caller should hold frontswap tree spinlock.
+ * Caller should hold frontswap tree spinlock and slru_lock.
  * Remove from the tree and free it, if nobody reference the entry.
  */
 static void swap_slot_entry_put(struct frontswap_tree *tree,
@@ -251,11 +264,67 @@ static void swap_slot_entry_put(struct frontswap_tree *tree,
 	BUG_ON(refcount < 0);
 
 	if (refcount == 0) {
+		struct page *page = entry->page;
+
 		frontswap_rb_erase(&tree->rbroot, entry);
+		list_del(&page->lru);
+
 		frontswap_free_entry(entry);
 	}
 }
 
+/*
+ * evict_frontswap_pages - evict @nr_pages LRU frontswap backed pages
+ *
+ * @nr_pages	number of LRU pages to be evicted
+ *
+ * Returns number of successfully evicted pages
+ */
+static unsigned long evict_frontswap_pages(unsigned long nr_pages)
+{
+	struct frontswap_tree *tree;
+	struct swap_slot_entry *entry;
+	struct page *page, *n;
+	unsigned long evicted = 0;
+	LIST_HEAD(free_pages);
+
+	spin_lock(&slru_lock);
+	list_for_each_entry_safe_reverse(page, n, &slru_list, lru) {
+		entry = swap_slot(page);
+
+		/*
+		 * the entry could be free by other thread in the while.
+		 * check whether the situation occurred and avoid others to
+		 * free it by compare reference count and increase it
+		 * atomically.
+		 */
+		if (!atomic_inc_not_zero(&entry->refcount))
+			continue;
+
+		list_move(&page->lru, &free_pages);
+		if (++evicted >= nr_pages)
+			break;
+	}
+	spin_unlock(&slru_lock);
+
+	list_for_each_entry_safe(page, n, &free_pages, lru) {
+		tree = swap_tree(page);
+		entry = swap_slot(page);
+
+		spin_lock(&tree->lock);
+		spin_lock(&slru_lock);
+		/* drop refcount increased by above loop */
+		swap_slot_entry_put(tree, entry);
+		/* free entry if the entry is still in tree */
+		if (frontswap_rb_search(&tree->rbroot, entry->offset))
+			swap_slot_entry_put(tree, entry);
+		spin_unlock(&slru_lock);
+		spin_unlock(&tree->lock);
+	}
+
+	return evicted;
+}
+
 /* Caller should hold frontswap tree spinlock */
 static struct swap_slot_entry *frontswap_find_get(struct frontswap_tree *tree,
 						pgoff_t offset)
@@ -339,9 +408,15 @@ int gcma_frontswap_store(unsigned type, pgoff_t offset,
 		ret = frontswap_rb_insert(&tree->rbroot, entry, &dupentry);
 		if (ret == -EEXIST) {
 			frontswap_rb_erase(&tree->rbroot, dupentry);
+			spin_lock(&slru_lock);
 			swap_slot_entry_put(tree, dupentry);
+			spin_unlock(&slru_lock);
 		}
 	} while (ret == -EEXIST);
+
+	spin_lock(&slru_lock);
+	list_add(&gcma_page->lru, &slru_list);
+	spin_unlock(&slru_lock);
 	spin_unlock(&tree->lock);
 
 	return ret;
@@ -378,7 +453,10 @@ int gcma_frontswap_load(unsigned type, pgoff_t offset,
 	kunmap_atomic(dst);
 
 	spin_lock(&tree->lock);
+	spin_lock(&slru_lock);
+	list_move(&gcma_page->lru, &slru_list);
 	swap_slot_entry_put(tree, entry);
+	spin_unlock(&slru_lock);
 	spin_unlock(&tree->lock);
 
 	return 0;
@@ -396,7 +474,9 @@ void gcma_frontswap_invalidate_page(unsigned type, pgoff_t offset)
 		return;
 	}
 
+	spin_lock(&slru_lock);
 	swap_slot_entry_put(tree, entry);
+	spin_unlock(&slru_lock);
 	spin_unlock(&tree->lock);
 }
 
@@ -411,7 +491,9 @@ void gcma_frontswap_invalidate_area(unsigned type)
 	spin_lock(&tree->lock);
 	rbtree_postorder_for_each_entry_safe(entry, n, &tree->rbroot, rbnode) {
 		frontswap_rb_erase(&tree->rbroot, entry);
+		spin_lock(&slru_lock);
 		swap_slot_entry_put(tree, entry);
+		spin_unlock(&slru_lock);
 	}
 	tree->rbroot = RB_ROOT;
 	spin_unlock(&tree->lock);
@@ -479,6 +561,7 @@ static int __init init_gcma(void)
 {
 	pr_info("loading gcma\n");
 
+	spin_lock_init(&slru_lock);
 	swap_slot_entry_cache = KMEM_CACHE(swap_slot_entry, 0);
 	if (swap_slot_entry_cache == NULL)
 		return -ENOMEM;
Signature · 7 lines
1.9.1

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

RFC 4/6 gcma: discard swap cache pages to meet successful GCMA allocation

In reply to SeongJae Park

GCMA's goal is to allocate contiguous memory successfully anytime as well as efficient usage of reserved memory space.

For memory efficiency, we allowed using reserved space as swap cache so we should be able to drain those swap cache pages when GCMA user want to get contiguos memory successfully, anytime.

We just discard swap caches pages if needed.
It's okay because we have used write-through mode of frontswap so all of data should be on disk already.

---
 mm/gcma.c | 192 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++----
 1 file changed, 181 insertions(+), 11 deletions(-)
mm/gcma.c +181 -11
index d459116..9c07128 100644
@@ -80,6 +80,50 @@ static void set_swap_slot(struct page *page, struct swap_slot_entry *slot)
 }
 
 /*
+ * Flags for status of a page in gcma
+ *
+ * GF_SWAP_LRU
+ * The page is being used for frontswap and hang on frontswap LRU list.
+ * It can be drained for contiguous memory allocation anytime.
+ * Protected by slru_lock.
+ *
+ * GF_RECLAIMING
+ * The page is being draining for contiguous memory allocation.
+ * Frontswap guests should not use it.
+ * Protected by slru_lock.
+ *
+ * GF_ISOLATED
+ * The page is isolated for contiguous memory allocation.
+ * GCMA guests can use the page safely while frontswap guests should not.
+ * Protected by gcma->lock.
+ */
+enum gpage_flags {
+	GF_SWAP_LRU = 0x1,
+	GF_RECLAIMING = 0x2,
+	GF_ISOLATED = 0x4,
+};
+
+static int gpage_flag(struct page *page, int flag)
+{
+	return page->private & flag;
+}
+
+static void set_gpage_flag(struct page *page, int flag)
+{
+	page->private |= flag;
+}
+
+static void clear_gpage_flag(struct page *page, int flag)
+{
+	page->private &= ~flag;
+}
+
+static void clear_gpage_flagall(struct page *page)
+{
+	page->private = 0;
+}
+
+/*
  * gcma_init - initializes a contiguous memory area
  *
  * @start_pfn	start pfn of contiguous memory area
@@ -137,11 +181,13 @@ static struct page *gcma_alloc_page(struct gcma *gcma)
 	bitmap_set(bitmap, bit, 1);
 	page = pfn_to_page(gcma->base_pfn + bit);
 	spin_unlock(&gcma->lock);
+	clear_gpage_flagall(page);
 
 out:
 	return page;
 }
 
+/* Caller should hold slru_lock */
 static void gcma_free_page(struct gcma *gcma, struct page *page)
 {
 	unsigned long pfn, offset;
@@ -151,7 +197,18 @@ static void gcma_free_page(struct gcma *gcma, struct page *page)
 	spin_lock(&gcma->lock);
 	offset = pfn - gcma->base_pfn;
 
-	bitmap_clear(gcma->bitmap, offset, 1);
+	if (likely(!gpage_flag(page, GF_RECLAIMING))) {
+		bitmap_clear(gcma->bitmap, offset, 1);
+	} else {
+		/*
+		 * The page should be safe to be used for a thread which
+		 * reclaimed the page.
+		 * To prevent further allocation from other thread,
+		 * set bitmap and mark the page as isolated.
+		 */
+		bitmap_set(gcma->bitmap, offset, 1);
+		set_gpage_flag(page, GF_ISOLATED);
+	}
 	spin_unlock(&gcma->lock);
 }
 
@@ -301,6 +358,7 @@ static unsigned long evict_frontswap_pages(unsigned long nr_pages)
 		if (!atomic_inc_not_zero(&entry->refcount))
 			continue;
 
+		clear_gpage_flag(page, GF_SWAP_LRU);
 		list_move(&page->lru, &free_pages);
 		if (++evicted >= nr_pages)
 			break;
@@ -377,7 +435,9 @@ int gcma_frontswap_store(unsigned type, pgoff_t offset,
 
 	entry = kmem_cache_alloc(swap_slot_entry_cache, GFP_NOIO);
 	if (!entry) {
+		spin_lock(&slru_lock);
 		gcma_free_page(gcma, gcma_page);
+		spin_unlock(&slru_lock);
 		return -ENOMEM;
 	}
 
@@ -415,6 +475,7 @@ int gcma_frontswap_store(unsigned type, pgoff_t offset,
 	} while (ret == -EEXIST);
 
 	spin_lock(&slru_lock);
+	set_gpage_flag(gcma_page, GF_SWAP_LRU);
 	list_add(&gcma_page->lru, &slru_list);
 	spin_unlock(&slru_lock);
 	spin_unlock(&tree->lock);
@@ -454,7 +515,8 @@ int gcma_frontswap_load(unsigned type, pgoff_t offset,
 
 	spin_lock(&tree->lock);
 	spin_lock(&slru_lock);
-	list_move(&gcma_page->lru, &slru_list);
+	if (likely(gpage_flag(gcma_page, GF_SWAP_LRU)))
+		list_move(&gcma_page->lru, &slru_list);
 	swap_slot_entry_put(tree, entry);
 	spin_unlock(&slru_lock);
 	spin_unlock(&tree->lock);
@@ -511,6 +573,43 @@ static struct frontswap_ops gcma_frontswap_ops = {
 };
 
 /*
+ * Return 0 if [start_pfn, end_pfn] is isolated.
+ * Otherwise, return first unisolated pfn from the start_pfn.
+ */
+static unsigned long isolate_interrupted(struct gcma *gcma,
+		unsigned long start_pfn, unsigned long end_pfn)
+{
+	unsigned long offset;
+	unsigned long *bitmap;
+	unsigned long pfn, ret = 0;
+	struct page *page;
+
+	spin_lock(&gcma->lock);
+
+	for (pfn = start_pfn; pfn < end_pfn; pfn++) {
+		int set;
+
+		offset = pfn - gcma->base_pfn;
+		bitmap = gcma->bitmap + offset / BITS_PER_LONG;
+
+		set = test_bit(pfn % BITS_PER_LONG, bitmap);
+		if (!set) {
+			ret = pfn;
+			break;
+		}
+
+		page = pfn_to_page(pfn);
+		if (!gpage_flag(page, GF_ISOLATED)) {
+			ret = pfn;
+			break;
+		}
+
+	}
+	spin_unlock(&gcma->lock);
+	return ret;
+}
+
+/*
  * gcma_alloc_contig - allocates contiguous pages
  *
  * @start_pfn	start pfn of requiring contiguous memory area
@@ -521,21 +620,92 @@ static struct frontswap_ops gcma_frontswap_ops = {
 int gcma_alloc_contig(struct gcma *gcma, unsigned long start_pfn,
 			unsigned long size)
 {
+	LIST_HEAD(free_pages);
+	struct page *page, *n;
+	struct swap_slot_entry *entry;
 	unsigned long offset;
+	unsigned long *bitmap;
+	struct frontswap_tree *tree;
+	unsigned long pfn;
+	unsigned long orig_start = start_pfn;
 
-	spin_lock(&gcma->lock);
-	offset = start_pfn - gcma->base_pfn;
+retry:
+	for (pfn = start_pfn; pfn < start_pfn + size; pfn++) {
+		spin_lock(&gcma->lock);
+
+		offset = pfn - gcma->base_pfn;
+		bitmap = gcma->bitmap + offset / BITS_PER_LONG;
+		page = pfn_to_page(pfn);
+
+		if (!test_bit(offset % BITS_PER_LONG, bitmap)) {
+			/* set a bit for prevent allocation for frontswap */
+			bitmap_set(gcma->bitmap, offset, 1);
+			set_gpage_flag(page, GF_ISOLATED);
+			spin_unlock(&gcma->lock);
+			continue;
+		}
+
+		/* Someone is using the page so it's complicated :( */
+		spin_unlock(&gcma->lock);
+		spin_lock(&slru_lock);
+		/*
+		 * If the page is in LRU, we can get swap_slot_entry from
+		 * the page with no problem.
+		 */
+		if (gpage_flag(page, GF_SWAP_LRU)) {
+			BUG_ON(gpage_flag(page, GF_RECLAIMING));
+
+			entry = swap_slot(page);
+			if (atomic_inc_not_zero(&entry->refcount)) {
+				clear_gpage_flag(page, GF_SWAP_LRU);
+				set_gpage_flag(page, GF_RECLAIMING);
+				list_move(&page->lru, &free_pages);
+				spin_unlock(&slru_lock);
+				continue;
+			}
+		}
 
-	if (bitmap_find_next_zero_area(gcma->bitmap, gcma->size, offset,
-				size, 0) != 0) {
+		/*
+		 * Someone is allocating the page but it's not yet in LRU
+		 * in case of frontswap_store or it was deleted from LRU
+		 * but not yet from gcma's bitmap in case of
+		 * frontswap_invalidate. Anycase, the race is small so retry
+		 * after a while will see success. Below isolate_interrupted
+		 * handles it.
+		 */
+		spin_lock(&gcma->lock);
+		if (!test_bit(offset % BITS_PER_LONG, bitmap)) {
+			bitmap_set(gcma->bitmap, offset, 1);
+			set_gpage_flag(page, GF_ISOLATED);
+		} else {
+			set_gpage_flag(page, GF_RECLAIMING);
+		}
 		spin_unlock(&gcma->lock);
-		pr_warn("already allocated region required: %lu, %lu",
-				start_pfn, size);
-		return -EINVAL;
+		spin_unlock(&slru_lock);
 	}
 
-	bitmap_set(gcma->bitmap, offset, size);
-	spin_unlock(&gcma->lock);
+	/*
+	 * Since we increased refcount of the page above, we can access
+	 * swap_slot_entry with safe
+	 */
+	list_for_each_entry_safe(page, n, &free_pages, lru) {
+		tree = swap_tree(page);
+		entry = swap_slot(page);
+
+		spin_lock(&tree->lock);
+		spin_lock(&slru_lock);
+		/* drop refcount increased by above loop */
+		swap_slot_entry_put(tree, entry);
+		/* free entry if the entry is still in tree */
+		if (frontswap_rb_search(&tree->rbroot, entry->offset))
+			swap_slot_entry_put(tree, entry);
+		spin_unlock(&slru_lock);
+		spin_unlock(&tree->lock);
+	}
+
+	start_pfn = isolate_interrupted(gcma, orig_start, orig_start + size);
+	if (start_pfn)
+		goto retry;
 
 	return 0;
 }
Signature · 7 lines
1.9.1

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

RFC 5/6 gcma: export statistical data on debugfs

In reply to SeongJae Park

Export saved / loaded / evicted / reclaimed pages from gcma's frontswap backend on debugfs to let users know how gcma is working internally.

---
 mm/gcma.c | 46 ++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 46 insertions(+)
mm/gcma.c +46 -0
index 9c07128..65395ec 100644
@@ -57,6 +57,12 @@ static spinlock_t slru_lock;	/* protect slru_list */
 static struct frontswap_tree *gcma_swap_trees[MAX_SWAPFILES];
 static struct kmem_cache *swap_slot_entry_cache;
 
+/* For statistics */
+static atomic_t gcma_stored_pages = ATOMIC_INIT(0);
+static atomic_t gcma_loaded_pages = ATOMIC_INIT(0);
+static atomic_t gcma_evicted_pages = ATOMIC_INIT(0);
+static atomic_t gcma_reclaimed_pages = ATOMIC_INIT(0);
+
 static unsigned long evict_frontswap_pages(unsigned long nr_pages);
 
 static struct frontswap_tree *swap_tree(struct page *page)
@@ -380,6 +386,7 @@ static unsigned long evict_frontswap_pages(unsigned long nr_pages)
 		spin_unlock(&tree->lock);
 	}
 
+	atomic_add(evicted, &gcma_evicted_pages);
 	return evicted;
 }
 
@@ -480,6 +487,7 @@ int gcma_frontswap_store(unsigned type, pgoff_t offset,
 	spin_unlock(&slru_lock);
 	spin_unlock(&tree->lock);
 
+	atomic_inc(&gcma_stored_pages);
 	return ret;
 }
 
@@ -521,6 +529,7 @@ int gcma_frontswap_load(unsigned type, pgoff_t offset,
 	spin_unlock(&slru_lock);
 	spin_unlock(&tree->lock);
 
+	atomic_inc(&gcma_loaded_pages);
 	return 0;
 }
 
@@ -659,6 +668,7 @@ retry:
 			if (atomic_inc_not_zero(&entry->refcount)) {
 				clear_gpage_flag(page, GF_SWAP_LRU);
 				set_gpage_flag(page, GF_RECLAIMING);
+				atomic_inc(&gcma_reclaimed_pages);
 				list_move(&page->lru, &free_pages);
 				spin_unlock(&slru_lock);
 				continue;
@@ -679,6 +689,7 @@ retry:
 			set_gpage_flag(page, GF_ISOLATED);
 		} else {
 			set_gpage_flag(page, GF_RECLAIMING);
+			atomic_inc(&gcma_reclaimed_pages);
 		}
 		spin_unlock(&gcma->lock);
 		spin_unlock(&slru_lock);
@@ -727,6 +738,40 @@ void gcma_free_contig(struct gcma *gcma,
 	spin_unlock(&gcma->lock);
 }
 
+#ifdef CONFIG_DEBUG_FS
+#include <linux/debugfs.h>
+
+static struct dentry *gcma_debugfs_root;
+
+static int __init gcma_debugfs_init(void)
+{
+	if (!debugfs_initialized())
+		return -ENODEV;
+
+	gcma_debugfs_root = debugfs_create_dir("gcma", NULL);
+	if (!gcma_debugfs_root)
+		return -ENOMEM;
+
+	debugfs_create_atomic_t("stored_pages", S_IRUGO,
+			gcma_debugfs_root, &gcma_stored_pages);
+	debugfs_create_atomic_t("loaded_pages", S_IRUGO,
+			gcma_debugfs_root, &gcma_loaded_pages);
+	debugfs_create_atomic_t("evicted_pages", S_IRUGO,
+			gcma_debugfs_root, &gcma_evicted_pages);
+	debugfs_create_atomic_t("reclaimed_pages", S_IRUGO,
+			gcma_debugfs_root, &gcma_reclaimed_pages);
+
+	pr_info("gcma debufs init\n");
+	return 0;
+}
+#else
+static int __init gcma_debugfs_init(void)
+{
+	return 0;
+}
+#endif
+
+
 static int __init init_gcma(void)
 {
 	pr_info("loading gcma\n");
@@ -743,6 +788,7 @@ static int __init init_gcma(void)
 	frontswap_writethrough(true);
 	frontswap_register_ops(&gcma_frontswap_ops);
 
+	gcma_debugfs_init();
 	return 0;
 }
 
Signature · 7 lines
1.9.1

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

RFC 6/6 gcma: integrate gcma under cma interface

In reply to SeongJae Park

Currently, cma reserves large contiguous memory area during early boot and let the area could be used by others for movable pages only. Then, if the movable pages arenecessary for contiguous memory allocation, cma migrates and/or discards them out.

This mechanism have two weakness.
1) Because any one in kernel can pin any movable pages, contiguous memory allocation could be fail due to migration failure.
2) Because of migration / reclaim overhead, the latency could be extremely high.
In short, cma doesn't guarantee success and fast latency of contiguous memory allocation. The problem was discussed in detail from [1] and [2].

gcma, which introduced by above patches, guarantees success and fast latency of contiguous memory allocation. gcma concept and implementation, performance evaluation was presented in detail from [2].

This patch let cma clients to be able to use gcma easily using friendly cma interface by integrating gcma under cma interface.

After this patch, clients can decalre a contiguous memory area to be managed in gcma way instead of cma way internally by using gcma_declare_contiguous() function call. After declaration, clients can use the area using familiar cma interface while it works in gcma way.

For example, you can use following code snippet to make two contiguous
regions: one region will work as cma and the other will work as gcma.

```
struct cma *cma, *gcma;

cma_declare_contiguous(base, size, limit, 0, 0, fixed, &cma);
gcma_declare_contiguous(gcma_base, size, gcma_limit, 0, 0, fixed, &gcma);

cma_alloc(cma, 1024, 0);	/* alloc in cma way */
cma_alloc(gcma, 1024, 0);	/* alloc in gcma way */
```

[1] https://lkml.org/lkml/2013/10/30/16
[2] http://sched.co/1qZcBAO

---
 include/linux/cma.h  |   4 ++
 include/linux/gcma.h |  21 ++++++++++
 mm/Kconfig           |  15 +++++++
 mm/Makefile          |   2 +
 mm/cma.c             | 110 ++++++++++++++++++++++++++++++++++++++++-----------
 5 files changed, 129 insertions(+), 23 deletions(-)
include/linux/cma.h +4 -0
index 371b930..f81d0dd 100644
@@ -22,6 +22,10 @@ extern int __init cma_declare_contiguous(phys_addr_t size,
 			phys_addr_t base, phys_addr_t limit,
 			phys_addr_t alignment, unsigned int order_per_bit,
 			bool fixed, struct cma **res_cma);
+extern int __init gcma_declare_contiguous(phys_addr_t size,
+			phys_addr_t base, phys_addr_t limit,
+			phys_addr_t alignment, unsigned int order_per_bit,
+			bool fixed, struct cma **res_cma);
 extern struct page *cma_alloc(struct cma *cma, int count, unsigned int align);
 extern bool cma_release(struct cma *cma, struct page *pages, int count);
 #endif
include/linux/gcma.h +21 -0
index d733a9b..dedbd0f 100644
@@ -16,6 +16,25 @@
 
 struct gcma;
 
+#ifndef CONFIG_GCMA
+
+inline int gcma_init(unsigned long start_pfn, unsigned long size,
+		     struct gcma **res_gcma)
+{
+	return 0;
+}
+
+inline int gcma_alloc_contig(struct gcma *gcma,
+			     unsigned long start, unsigned long end)
+{
+	return 0;
+}
+
+void gcma_free_contig(struct gcma *gcma,
+		      unsigned long pfn, unsigned long nr_pages) { }
+
+#else
+
 int gcma_init(unsigned long start_pfn, unsigned long size,
 	      struct gcma **res_gcma);
 int gcma_alloc_contig(struct gcma *gcma,
@@ -23,4 +42,6 @@ int gcma_alloc_contig(struct gcma *gcma,
 void gcma_free_contig(struct gcma *gcma,
 		      unsigned long start_pfn, unsigned long size);
 
+#endif
+
 #endif /* _LINUX_GCMA_H */
mm/Kconfig +15 -0
index 886db21..1b232e3 100644
@@ -519,6 +519,21 @@ config CMA_AREAS
 
 	  If unsure, leave the default value "7".
 
+config GCMA
+	bool "Guaranteed Contiguous Memory Allocator (EXPERIMENTAL)"
+	default n
+	select FRONTSWAP
+	select CMA
+	help
+	  A contiguous memory allocator which guarantees success and
+	  predictable latency for allocation request.
+	  It carves out large amount of memory and let them be allocated
+	  to the contiguous memory request while it can be used as backend
+	  for frontswap.
+
+	  This is marked experimental because it is a new feature that
+	  interacts heavily with memory reclaim.
+
 config MEM_SOFT_DIRTY
 	bool "Track memory changes"
 	depends on CHECKPOINT_RESTORE && HAVE_ARCH_SOFT_DIRTY && PROC_FS
mm/Makefile +2 -0
index 632ae77..ecff2c7 100644
@@ -33,6 +33,7 @@ obj-$(CONFIG_HAVE_MEMBLOCK) += memblock.o
 obj-$(CONFIG_SWAP)	+= page_io.o swap_state.o swapfile.o
 obj-$(CONFIG_FRONTSWAP)	+= frontswap.o
 obj-$(CONFIG_ZSWAP)	+= zswap.o
+obj-$(CONFIG_GCMA)	+= gcma.o
 obj-$(CONFIG_HAS_DMA)	+= dmapool.o
 obj-$(CONFIG_HUGETLBFS)	+= hugetlb.o
 obj-$(CONFIG_NUMA) 	+= mempolicy.o
@@ -64,3 +65,4 @@ obj-$(CONFIG_ZBUD)	+= zbud.o
 obj-$(CONFIG_ZSMALLOC)	+= zsmalloc.o
 obj-$(CONFIG_GENERIC_EARLY_IOREMAP) += early_ioremap.o
 obj-$(CONFIG_CMA)	+= cma.o
+obj-$(CONFIG_GCMA)	+= gcma.o
mm/cma.c +87 -23

177 lines not on this page · raw message

Signature · 7 lines
1.9.1

--
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
In reply to SeongJae Park
Quoted 3 lines · SeongJae Park

Difference with cma is choice and operation of 2nd-class client. In gcma, 2nd-class client should allocate pages from the reserved area only if the allocated pages mets following conditions.

How about making CMA configurable in some fashion to be able to specify the type of 2nd class clients? Clean page-cache pages can also be rather easily evicted (see zone-reclaim). You could migrate them out when they are dirtied so that you do not have the high writeback latency from the CMA reserved area if it needs to be evicted later.

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

Hi Christoph,

Quoted 11 lines · Christoph Lameter
Quoted 3 lines · SeongJae Park

Difference with cma is choice and operation of 2nd-class client. In gcma, 2nd-class client should allocate pages from the reserved area only if the allocated pages mets following conditions.

How about making CMA configurable in some fashion to be able to specify the type of 2nd class clients? Clean page-cache pages can also be rather easily evicted (see zone-reclaim). You could migrate them out when they are dirtied so that you do not have the high writeback latency from the CMA reserved area if it needs to be evicted later.

Nice point.

Currently, gcma is integrated inside cma and user could decide a specific contiguous memory area to work in cma way(movable pages as 2nd class) or in gcma way(out-of-kernel, easy-to-discard pages as 2nd class). It is implemented in 6th change of this RFC, "gcma: integrate gcma under cma interface".

In short, the 2nd-clients of cma is already configurable between movable pages and frontswap backend with this RFC.

And yes, cleancache will be great 2nd class client.
As described within coverletter, our 2nd class client candidates are frontswap and _cleancache_. But, because the gcma is still in unmatured sate yet, current RFC(this patchset) use only frontswap. In future, it will be configurable.

Apologize I forgot to describe about future plan.

Thanks,
SeongJae Park

Quoted 2 lines

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