[PATCH 5/8] mm/zsmalloc: avoid calculate max objects of zspage twice

From: Ganesh Mahendran
Date: Fri Jul 01 2016 - 02:42:20 EST


Currently, if a class can not be merged, the max objects of zspage
in that class may be calculated twice.

This patch calculate max objects of zspage at the begin, and pass
the value to can_merge() to decide whether the class can be merged.

Signed-off-by: Ganesh Mahendran <opensource.ganesh@xxxxxxxxx>
---
mm/zsmalloc.c | 21 ++++++++++-----------
1 file changed, 10 insertions(+), 11 deletions(-)

diff --git a/mm/zsmalloc.c b/mm/zsmalloc.c
index 50283b1..2690914 100644
--- a/mm/zsmalloc.c
+++ b/mm/zsmalloc.c
@@ -1362,16 +1362,14 @@ static void init_zs_size_classes(void)
zs_size_classes = nr;
}

-static bool can_merge(struct size_class *prev, int size, int pages_per_zspage)
+static bool can_merge(struct size_class *prev, int pages_per_zspage,
+ int objs_per_zspage)
{
- if (prev->pages_per_zspage != pages_per_zspage)
- return false;
-
- if (prev->objs_per_zspage
- != get_maxobj_per_zspage(size, pages_per_zspage))
- return false;
+ if (prev->pages_per_zspage == pages_per_zspage &&
+ prev->objs_per_zspage == objs_per_zspage)
+ return true;

- return true;
+ return false;
}

static bool zspage_full(struct size_class *class, struct zspage *zspage)
@@ -2460,6 +2458,7 @@ struct zs_pool *zs_create_pool(const char *name)
for (i = zs_size_classes - 1; i >= 0; i--) {
int size;
int pages_per_zspage;
+ int objs_per_zspage;
struct size_class *class;
int fullness = 0;

@@ -2467,6 +2466,7 @@ struct zs_pool *zs_create_pool(const char *name)
if (size > ZS_MAX_ALLOC_SIZE)
size = ZS_MAX_ALLOC_SIZE;
pages_per_zspage = get_pages_per_zspage(size);
+ objs_per_zspage = get_maxobj_per_zspage(size, pages_per_zspage);

/*
* size_class is used for normal zsmalloc operation such
@@ -2478,7 +2478,7 @@ struct zs_pool *zs_create_pool(const char *name)
* previous size_class if possible.
*/
if (prev_class) {
- if (can_merge(prev_class, size, pages_per_zspage)) {
+ if (can_merge(prev_class, pages_per_zspage, objs_per_zspage)) {
pool->size_class[i] = prev_class;
continue;
}
@@ -2491,8 +2491,7 @@ struct zs_pool *zs_create_pool(const char *name)
class->size = size;
class->index = i;
class->pages_per_zspage = pages_per_zspage;
- class->objs_per_zspage = get_maxobj_per_zspage(class->size,
- class->pages_per_zspage);
+ class->objs_per_zspage = objs_per_zspage;
spin_lock_init(&class->lock);
pool->size_class[i] = class;
for (fullness = ZS_EMPTY; fullness < NR_ZS_FULLNESS;
--
1.9.1