2014-10-29 05:35:08

by Gioh Kim

[permalink] [raw]
Subject: [PATCHv2 0/3] staging: ion: enable pool shrinking in page unit

Hello,

Current pool shrinking is not page unit, block unit.
But shrinker returns the pool size in page unit,
so it is confused.

And there is no way to control pool size and shrink pool directly.

I have 3 patches like followings.

1. Patch 1/3: make pool be shrinked by page unit
This patch shrinks pool in page unit.

2. Patch 2/3: limit pool size
This patch specifies pool size limit via debugfs.
The default value of limit is 0.
cat /sys/kernel/debug/ion/heaps/system_limit returns 0.
If you want to create 4 pools and limit each pool by 10MB,
you can write 40MB(=41943040) at system_limit debugfs file
like following:
echo 41943040 > /sys/kernel/debug/ion/heaps/system_limit

2. Patch 3/3: enable debugfs to shrink page directly
This patch enables debugfs to specify shrink amount.
For instance, this shrinks all pages in every pool.
echo 0 > /sys/kernel/debug/ion/heaps/system_shrink
And this shrinks 300-pages from entire pool.
echo 30 > /sys/kernel/debug/ion/heaps/system_shrink
It try to shrink high-order pool first because high-order pages is necessary
more than low-order when the system has low memory.

This patchset is based on linux-next-20141023.


Gioh Kim (3):
staging: ion: shrink page-pool by page unit
staging: ion: limit pool size
staging: ion: debugfs to shrink pool

drivers/staging/android/ion/ion.c | 62 ++++++++++++++++---------
drivers/staging/android/ion/ion_page_pool.c | 32 ++++++++-----
drivers/staging/android/ion/ion_system_heap.c | 20 ++++++--
3 files changed, 75 insertions(+), 39 deletions(-)

--
1.7.9.5


2014-10-29 05:35:12

by Gioh Kim

[permalink] [raw]
Subject: [PATCHv2 1/3] staging: ion: shrink page-pool by page unit

This patch shrink page-pool by page unit.

Shrinker usually get the pool size with the pool-scanner
and pass the size to the pool-counter to shrink entire pool.
But the pool-scanner is working in block unit.
and pool-counter page unit.
So it is confused.

Change-Id: If25c693c09f6ebd14c87809feddb72f9058e8308
Signed-off-by: Gioh Kim <[email protected]>
---
drivers/staging/android/ion/ion_page_pool.c | 5 +++--
drivers/staging/android/ion/ion_system_heap.c | 18 +++++++++++++++---
2 files changed, 18 insertions(+), 5 deletions(-)

diff --git a/drivers/staging/android/ion/ion_page_pool.c b/drivers/staging/android/ion/ion_page_pool.c
index 5864f3d..165152f 100644
--- a/drivers/staging/android/ion/ion_page_pool.c
+++ b/drivers/staging/android/ion/ion_page_pool.c
@@ -116,7 +116,7 @@ static int ion_page_pool_total(struct ion_page_pool *pool, bool high)
int ion_page_pool_shrink(struct ion_page_pool *pool, gfp_t gfp_mask,
int nr_to_scan)
{
- int freed;
+ int freed = 0;
bool high;

if (current_is_kswapd())
@@ -127,7 +127,7 @@ int ion_page_pool_shrink(struct ion_page_pool *pool, gfp_t gfp_mask,
if (nr_to_scan == 0)
return ion_page_pool_total(pool, high);

- for (freed = 0; freed < nr_to_scan; freed++) {
+ while (freed <= nr_to_scan) {
struct page *page;

mutex_lock(&pool->mutex);
@@ -141,6 +141,7 @@ int ion_page_pool_shrink(struct ion_page_pool *pool, gfp_t gfp_mask,
}
mutex_unlock(&pool->mutex);
ion_page_pool_free_pages(pool, page);
+ freed += (1 << pool->order);
}

return freed;
diff --git a/drivers/staging/android/ion/ion_system_heap.c b/drivers/staging/android/ion/ion_system_heap.c
index da2a63c..0ba8aaf 100644
--- a/drivers/staging/android/ion/ion_system_heap.c
+++ b/drivers/staging/android/ion/ion_system_heap.c
@@ -29,7 +29,7 @@
static gfp_t high_order_gfp_flags = (GFP_HIGHUSER | __GFP_ZERO | __GFP_NOWARN |
__GFP_NORETRY) & ~__GFP_WAIT;
static gfp_t low_order_gfp_flags = (GFP_HIGHUSER | __GFP_ZERO | __GFP_NOWARN);
-static const unsigned int orders[] = {8, 4, 0};
+static const unsigned int orders[] = {8, 6, 4, 0};
static const int num_orders = ARRAY_SIZE(orders);
static int order_to_index(unsigned int order)
{
@@ -212,14 +212,26 @@ static int ion_system_heap_shrink(struct ion_heap *heap, gfp_t gfp_mask,
{
struct ion_system_heap *sys_heap;
int nr_total = 0;
- int i;
+ int i, nr_freed;
+ int only_scan = 0;

sys_heap = container_of(heap, struct ion_system_heap, heap);

+ if (!nr_to_scan)
+ only_scan = 1;
+
for (i = 0; i < num_orders; i++) {
struct ion_page_pool *pool = sys_heap->pools[i];

- nr_total += ion_page_pool_shrink(pool, gfp_mask, nr_to_scan);
+ nr_freed = ion_page_pool_shrink(pool, gfp_mask, nr_to_scan);
+ nr_total += nr_freed;
+
+ if (!only_scan) {
+ nr_to_scan -= nr_freed;
+ /* shrink completed */
+ if (nr_to_scan <= 0)
+ break;
+ }
}

return nr_total;
--
1.7.9.5

2014-10-29 05:35:11

by Gioh Kim

[permalink] [raw]
Subject: [PATCHv2 3/3] staging: ion: debugfs to shrink pool

This patch enables debugfs files /sys/kernel/debug/ion/heaps/system_shrink
,which was commented out, to shrink pool or get pool size
Reading the file returns pool size and writing occurs to shrink pool.

Signed-off-by: Gioh Kim <[email protected]>
---
drivers/staging/android/ion/ion.c | 31 +++++++--------------------
drivers/staging/android/ion/ion_page_pool.c | 5 ++++-
2 files changed, 12 insertions(+), 24 deletions(-)

diff --git a/drivers/staging/android/ion/ion.c b/drivers/staging/android/ion/ion.c
index 036e29b..4879076 100644
--- a/drivers/staging/android/ion/ion.c
+++ b/drivers/staging/android/ion/ion.c
@@ -1463,43 +1463,29 @@ static const struct file_operations debug_heap_fops = {
.release = single_release,
};

-#ifdef DEBUG_HEAP_SHRINKER
static int debug_shrink_set(void *data, u64 val)
{
struct ion_heap *heap = data;
- struct shrink_control sc;
int objs;

- sc.gfp_mask = -1;
- sc.nr_to_scan = 0;
-
- if (!val)
- return 0;
-
- objs = heap->shrinker.shrink(&heap->shrinker, &sc);
- sc.nr_to_scan = objs;
+ if (val)
+ objs = val;
+ else
+ objs = heap->ops->shrink(heap, __GFP_HIGHMEM, 0);

- heap->shrinker.shrink(&heap->shrinker, &sc);
+ (void)heap->ops->shrink(heap, __GFP_HIGHMEM, objs);
return 0;
}

static int debug_shrink_get(void *data, u64 *val)
{
struct ion_heap *heap = data;
- struct shrink_control sc;
- int objs;
-
- sc.gfp_mask = -1;
- sc.nr_to_scan = 0;
-
- objs = heap->shrinker.shrink(&heap->shrinker, &sc);
- *val = objs;
+ *val = heap->ops->shrink(heap, __GFP_HIGHMEM, 0);
return 0;
}

DEFINE_SIMPLE_ATTRIBUTE(debug_shrink_fops, debug_shrink_get,
debug_shrink_set, "%llu\n");
-#endif

extern int pool_limit;
extern const int num_orders;
@@ -1553,8 +1539,7 @@ void ion_device_add_heap(struct ion_device *dev, struct ion_heap *heap)
path, heap->name);
}

-#ifdef DEBUG_HEAP_SHRINKER
- if (heap->shrinker.shrink) {
+ if (heap->ops->shrink) {
char debug_name[64];

snprintf(debug_name, 64, "%s_shrink", heap->name);
@@ -1581,7 +1566,7 @@ void ion_device_add_heap(struct ion_device *dev, struct ion_heap *heap)
path, debug_name);
}
}
-#endif
+
up_write(&dev->lock);
}

diff --git a/drivers/staging/android/ion/ion_page_pool.c b/drivers/staging/android/ion/ion_page_pool.c
index 15d1ac8..c473c2a 100644
--- a/drivers/staging/android/ion/ion_page_pool.c
+++ b/drivers/staging/android/ion/ion_page_pool.c
@@ -25,6 +25,9 @@
#include <linux/kconfig.h>
#include "ion_priv.h"

+/* limit number of pages each pool can have */
+int pool_limit;
+
static void *ion_page_pool_alloc_pages(struct ion_page_pool *pool)
{
struct page *page = alloc_pages(pool->gfp_mask, pool->order);
@@ -54,7 +57,7 @@ static int ion_page_pool_total(struct ion_page_pool *pool, bool high)

static int ion_page_pool_add(struct ion_page_pool *pool, struct page *page)
{
- if (CONFIG_ION_POOL_LIMIT && ion_page_pool_total(pool, 1) > POOL_LIMIT)
+ if (pool_limit && ion_page_pool_total(pool, 1) > pool_limit)
return 1;

mutex_lock(&pool->mutex);
--
1.7.9.5

2014-10-29 05:35:09

by Gioh Kim

[permalink] [raw]
Subject: [PATCHv2 2/3] staging: ion: limit pool size

This patch limits pool size by page unit.
And enable a debugfs file to set limit.

Change-Id: Idaef4daa69084e8ec0844ecefc6738afeab79ccb
Signed-off-by: Gioh Kim <[email protected]>
---
drivers/staging/android/ion/ion.c | 31 +++++++++++++++++++++++++
drivers/staging/android/ion/ion_page_pool.c | 24 +++++++++++--------
drivers/staging/android/ion/ion_system_heap.c | 2 +-
3 files changed, 46 insertions(+), 11 deletions(-)

diff --git a/drivers/staging/android/ion/ion.c b/drivers/staging/android/ion/ion.c
index 290d4d2..036e29b 100644
--- a/drivers/staging/android/ion/ion.c
+++ b/drivers/staging/android/ion/ion.c
@@ -1501,6 +1501,25 @@ DEFINE_SIMPLE_ATTRIBUTE(debug_shrink_fops, debug_shrink_get,
debug_shrink_set, "%llu\n");
#endif

+extern int pool_limit;
+extern const int num_orders;
+
+static int debug_limit_set(void *data, u64 val)
+{
+ /* val is kb unit, pool_limit is for page limit of individual pool */
+ pool_limit = (int)(val / PAGE_SIZE) / num_orders;
+ return 0;
+}
+
+static int debug_limit_get(void *data, u64 *val)
+{
+ *val = pool_limit * PAGE_SIZE * num_orders;
+ return 0;
+}
+
+DEFINE_SIMPLE_ATTRIBUTE(debug_limit_fops, debug_limit_get,
+ debug_limit_set, "%llu\n");
+
void ion_device_add_heap(struct ion_device *dev, struct ion_heap *heap)
{
struct dentry *debug_file;
@@ -1549,6 +1568,18 @@ void ion_device_add_heap(struct ion_device *dev, struct ion_heap *heap)
pr_err("Failed to create heap shrinker debugfs at %s/%s\n",
path, debug_name);
}
+
+ snprintf(debug_name, 64, "%s_limit", heap->name);
+ debug_file = debugfs_create_file(
+ debug_name, 0644, dev->heaps_debug_root, heap,
+ &debug_limit_fops);
+ if (!debug_file) {
+ char buf[256], *path;
+
+ path = dentry_path(dev->heaps_debug_root, buf, 256);
+ pr_err("Failed to create heap shrinker debugfs at %s/%s\n",
+ path, debug_name);
+ }
}
#endif
up_write(&dev->lock);
diff --git a/drivers/staging/android/ion/ion_page_pool.c b/drivers/staging/android/ion/ion_page_pool.c
index 165152f..15d1ac8 100644
--- a/drivers/staging/android/ion/ion_page_pool.c
+++ b/drivers/staging/android/ion/ion_page_pool.c
@@ -22,6 +22,7 @@
#include <linux/module.h>
#include <linux/slab.h>
#include <linux/swap.h>
+#include <linux/kconfig.h>
#include "ion_priv.h"

static void *ion_page_pool_alloc_pages(struct ion_page_pool *pool)
@@ -41,8 +42,21 @@ static void ion_page_pool_free_pages(struct ion_page_pool *pool,
__free_pages(page, pool->order);
}

+static int ion_page_pool_total(struct ion_page_pool *pool, bool high)
+{
+ int count = pool->low_count;
+
+ if (high)
+ count += pool->high_count;
+
+ return count << pool->order;
+}
+
static int ion_page_pool_add(struct ion_page_pool *pool, struct page *page)
{
+ if (CONFIG_ION_POOL_LIMIT && ion_page_pool_total(pool, 1) > POOL_LIMIT)
+ return 1;
+
mutex_lock(&pool->mutex);
if (PageHighMem(page)) {
list_add_tail(&page->lru, &pool->high_items);
@@ -103,16 +117,6 @@ void ion_page_pool_free(struct ion_page_pool *pool, struct page *page)
ion_page_pool_free_pages(pool, page);
}

-static int ion_page_pool_total(struct ion_page_pool *pool, bool high)
-{
- int count = pool->low_count;
-
- if (high)
- count += pool->high_count;
-
- return count << pool->order;
-}
-
int ion_page_pool_shrink(struct ion_page_pool *pool, gfp_t gfp_mask,
int nr_to_scan)
{
diff --git a/drivers/staging/android/ion/ion_system_heap.c b/drivers/staging/android/ion/ion_system_heap.c
index 0ba8aaf..d6c0acd 100644
--- a/drivers/staging/android/ion/ion_system_heap.c
+++ b/drivers/staging/android/ion/ion_system_heap.c
@@ -30,7 +30,7 @@ static gfp_t high_order_gfp_flags = (GFP_HIGHUSER | __GFP_ZERO | __GFP_NOWARN |
__GFP_NORETRY) & ~__GFP_WAIT;
static gfp_t low_order_gfp_flags = (GFP_HIGHUSER | __GFP_ZERO | __GFP_NOWARN);
static const unsigned int orders[] = {4, 3, 2, 0};
-static const int num_orders = ARRAY_SIZE(orders);
+const int num_orders = ARRAY_SIZE(orders);
static int order_to_index(unsigned int order)
{
int i;
--
1.7.9.5

2014-10-29 06:45:17

by Gioh Kim

[permalink] [raw]
Subject: Re: [PATCHv2 1/3] staging: ion: shrink page-pool by page unit



> diff --git a/drivers/staging/android/ion/ion_system_heap.c b/drivers/staging/android/ion/ion_system_heap.c
> index da2a63c..0ba8aaf 100644
> --- a/drivers/staging/android/ion/ion_system_heap.c
> +++ b/drivers/staging/android/ion/ion_system_heap.c
> @@ -29,7 +29,7 @@
> static gfp_t high_order_gfp_flags = (GFP_HIGHUSER | __GFP_ZERO | __GFP_NOWARN |
> __GFP_NORETRY) & ~__GFP_WAIT;
> static gfp_t low_order_gfp_flags = (GFP_HIGHUSER | __GFP_ZERO | __GFP_NOWARN);
> -static const unsigned int orders[] = {8, 4, 0};
> +static const unsigned int orders[] = {8, 6, 4, 0};

Please ignore this. I'll put it in separate patch.
I'm sorry.

2014-10-29 08:40:56

by Gioh Kim

[permalink] [raw]
Subject: Re: [PATCHv2 0/3] staging: ion: enable pool shrinking in page unit



2014-10-29 ???? 2:35?? Gioh Kim ??(??) ?? ??:
> Hello,
>
> Current pool shrinking is not page unit, block unit.
> But shrinker returns the pool size in page unit,
> so it is confused.
>
> And there is no way to control pool size and shrink pool directly.
>
> I have 3 patches like followings.
>
> 1. Patch 1/3: make pool be shrinked by page unit
> This patch shrinks pool in page unit.
>
> 2. Patch 2/3: limit pool size
> This patch specifies pool size limit via debugfs.
> The default value of limit is 0.
> cat /sys/kernel/debug/ion/heaps/system_limit returns 0.
> If you want to create 4 pools and limit each pool by 10MB,
> you can write 40MB(=41943040) at system_limit debugfs file
> like following:
> echo 41943040 > /sys/kernel/debug/ion/heaps/system_limit
>
> 2. Patch 3/3: enable debugfs to shrink page directly
> This patch enables debugfs to specify shrink amount.
> For instance, this shrinks all pages in every pool.
> echo 0 > /sys/kernel/debug/ion/heaps/system_shrink
> And this shrinks 300-pages from entire pool.
> echo 30 > /sys/kernel/debug/ion/heaps/system_shrink
> It try to shrink high-order pool first because high-order pages is necessary
> more than low-order when the system has low memory.
>
> This patchset is based on linux-next-20141023.
>
>
> Gioh Kim (3):
> staging: ion: shrink page-pool by page unit
> staging: ion: limit pool size
> staging: ion: debugfs to shrink pool
>
> drivers/staging/android/ion/ion.c | 62 ++++++++++++++++---------
> drivers/staging/android/ion/ion_page_pool.c | 32 ++++++++-----
> drivers/staging/android/ion/ion_system_heap.c | 20 ++++++--
> 3 files changed, 75 insertions(+), 39 deletions(-)
>

Following is my test result.
I set the orders as 4,3,2,0 for test.

# mount -t debugfs none /sys/kernel/debug

...... activate driver that calls ion-alloc ......

# cat /sys/kernel/debug/ion/heaps/system ===================> no limit
client pid size
----------------------------------------------------
----------------------------------------------------
orphaned allocations (info is from last known client):
----------------------------------------------------
total orphaned 0
total 0
deferred free 0
----------------------------------------------------
0 order 4 highmem pages in pool = 0 total
176 order 4 lowmem pages in pool = 11534336 total
0 order 3 highmem pages in pool = 0 total
0 order 3 lowmem pages in pool = 0 total
0 order 2 highmem pages in pool = 0 total
704 order 2 lowmem pages in pool = 11534336 total
0 order 0 highmem pages in pool = 0 total
2816 order 0 lowmem pages in pool = 11534336 total
# cat /sys/kernel/debug/ion/heaps/system_limit
0
# echo 41943040 > /sys/kernel/debug/ion/heaps/system_limit
# cat /sys/kernel/debug/ion/heaps/system_limit
41943040

...... activate driver that calls ion-alloc ......

# cat /sys/kernel/debug/ion/heaps/system ====================> 10MB limit
client pid size
----------------------------------------------------
----------------------------------------------------
orphaned allocations (info is from last known client):
----------------------------------------------------
total orphaned 0
total 0
deferred free 0
----------------------------------------------------
0 order 4 highmem pages in pool = 0 total
161 order 4 lowmem pages in pool = 10551296 total
0 order 3 highmem pages in pool = 0 total
0 order 3 lowmem pages in pool = 0 total
0 order 2 highmem pages in pool = 0 total
641 order 2 lowmem pages in pool = 10502144 total
0 order 0 highmem pages in pool = 0 total
2561 order 0 lowmem pages in pool = 10489856 total
# cat /sys/kernel/debug/ion/heaps/system_shrink ===============> count total pages
7701
# echo 0 > /sys/kernel/debug/ion/heaps/system_shrink =========> shrink all pages
# cat /sys/kernel/debug/ion/heaps/system
client pid size
----------------------------------------------------
----------------------------------------------------
orphaned allocations (info is from last known client):
----------------------------------------------------
total orphaned 0
total 0
deferred free 0
----------------------------------------------------
0 order 4 highmem pages in pool = 0 total
0 order 4 lowmem pages in pool = 0 total
0 order 3 highmem pages in pool = 0 total
0 order 3 lowmem pages in pool = 0 total
0 order 2 highmem pages in pool = 0 total
0 order 2 lowmem pages in pool = 0 total
0 order 0 highmem pages in pool = 0 total
0 order 0 lowmem pages in pool = 0 total
# cat /sys/kernel/debug/ion/heaps/system_shrink
0