summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorYu Zhao <yuzhao@google.com>2018-01-31 19:19:59 -0500
committerLinus Torvalds <torvalds@linux-foundation.org>2018-01-31 20:18:39 -0500
commit9c3760eb80880f3e02546e0a2ef479e1454986b3 (patch)
treee0f90aca53a602676bf50160ad1efbe0ac66a352
parenta7ab400d6fe73d0119fdc234e9982a6f80faea9f (diff)
zswap: only save zswap header when necessary
We waste sizeof(swp_entry_t) for zswap header when using zsmalloc as zpool driver because zsmalloc doesn't support eviction. Add zpool_evictable() to detect if zpool is potentially evictable, and use it in zswap to avoid waste memory for zswap header. [yuzhao@google.com: The zpool->" prefix is a result of copy & paste] Link: http://lkml.kernel.org/r/20180110225626.110330-1-yuzhao@google.com Link: http://lkml.kernel.org/r/20180110224741.83751-1-yuzhao@google.com Signed-off-by: Yu Zhao <yuzhao@google.com> Acked-by: Dan Streetman <ddstreet@ieee.org> Reviewed-by: Sergey Senozhatsky <sergey.senozhatsky@gmail.com> Cc: Seth Jennings <sjenning@redhat.com> Cc: Minchan Kim <minchan@kernel.org> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
-rw-r--r--include/linux/zpool.h2
-rw-r--r--mm/zpool.c25
-rw-r--r--mm/zsmalloc.c7
-rw-r--r--mm/zswap.c20
4 files changed, 35 insertions, 19 deletions
diff --git a/include/linux/zpool.h b/include/linux/zpool.h
index 004ba807df96..7238865e75b0 100644
--- a/include/linux/zpool.h
+++ b/include/linux/zpool.h
@@ -108,4 +108,6 @@ void zpool_register_driver(struct zpool_driver *driver);
108 108
109int zpool_unregister_driver(struct zpool_driver *driver); 109int zpool_unregister_driver(struct zpool_driver *driver);
110 110
111bool zpool_evictable(struct zpool *pool);
112
111#endif 113#endif
diff --git a/mm/zpool.c b/mm/zpool.c
index fd3ff719c32c..e1e7aa6d1d06 100644
--- a/mm/zpool.c
+++ b/mm/zpool.c
@@ -21,6 +21,7 @@ struct zpool {
21 struct zpool_driver *driver; 21 struct zpool_driver *driver;
22 void *pool; 22 void *pool;
23 const struct zpool_ops *ops; 23 const struct zpool_ops *ops;
24 bool evictable;
24 25
25 struct list_head list; 26 struct list_head list;
26}; 27};
@@ -142,7 +143,7 @@ EXPORT_SYMBOL(zpool_has_pool);
142 * 143 *
143 * This creates a new zpool of the specified type. The gfp flags will be 144 * This creates a new zpool of the specified type. The gfp flags will be
144 * used when allocating memory, if the implementation supports it. If the 145 * used when allocating memory, if the implementation supports it. If the
145 * ops param is NULL, then the created zpool will not be shrinkable. 146 * ops param is NULL, then the created zpool will not be evictable.
146 * 147 *
147 * Implementations must guarantee this to be thread-safe. 148 * Implementations must guarantee this to be thread-safe.
148 * 149 *
@@ -180,6 +181,7 @@ struct zpool *zpool_create_pool(const char *type, const char *name, gfp_t gfp,
180 zpool->driver = driver; 181 zpool->driver = driver;
181 zpool->pool = driver->create(name, gfp, ops, zpool); 182 zpool->pool = driver->create(name, gfp, ops, zpool);
182 zpool->ops = ops; 183 zpool->ops = ops;
184 zpool->evictable = driver->shrink && ops && ops->evict;
183 185
184 if (!zpool->pool) { 186 if (!zpool->pool) {
185 pr_err("couldn't create %s pool\n", type); 187 pr_err("couldn't create %s pool\n", type);
@@ -296,7 +298,8 @@ void zpool_free(struct zpool *zpool, unsigned long handle)
296int zpool_shrink(struct zpool *zpool, unsigned int pages, 298int zpool_shrink(struct zpool *zpool, unsigned int pages,
297 unsigned int *reclaimed) 299 unsigned int *reclaimed)
298{ 300{
299 return zpool->driver->shrink(zpool->pool, pages, reclaimed); 301 return zpool->driver->shrink ?
302 zpool->driver->shrink(zpool->pool, pages, reclaimed) : -EINVAL;
300} 303}
301 304
302/** 305/**
@@ -355,6 +358,24 @@ u64 zpool_get_total_size(struct zpool *zpool)
355 return zpool->driver->total_size(zpool->pool); 358 return zpool->driver->total_size(zpool->pool);
356} 359}
357 360
361/**
362 * zpool_evictable() - Test if zpool is potentially evictable
363 * @pool The zpool to test
364 *
365 * Zpool is only potentially evictable when it's created with struct
366 * zpool_ops.evict and its driver implements struct zpool_driver.shrink.
367 *
368 * However, it doesn't necessarily mean driver will use zpool_ops.evict
369 * in its implementation of zpool_driver.shrink. It could do internal
370 * defragmentation instead.
371 *
372 * Returns: true if potentially evictable; false otherwise.
373 */
374bool zpool_evictable(struct zpool *zpool)
375{
376 return zpool->evictable;
377}
378
358MODULE_LICENSE("GPL"); 379MODULE_LICENSE("GPL");
359MODULE_AUTHOR("Dan Streetman <ddstreet@ieee.org>"); 380MODULE_AUTHOR("Dan Streetman <ddstreet@ieee.org>");
360MODULE_DESCRIPTION("Common API for compressed memory storage"); 381MODULE_DESCRIPTION("Common API for compressed memory storage");
diff --git a/mm/zsmalloc.c b/mm/zsmalloc.c
index e136a8e72c48..f797d8b0d820 100644
--- a/mm/zsmalloc.c
+++ b/mm/zsmalloc.c
@@ -404,12 +404,6 @@ static void zs_zpool_free(void *pool, unsigned long handle)
404 zs_free(pool, handle); 404 zs_free(pool, handle);
405} 405}
406 406
407static int zs_zpool_shrink(void *pool, unsigned int pages,
408 unsigned int *reclaimed)
409{
410 return -EINVAL;
411}
412
413static void *zs_zpool_map(void *pool, unsigned long handle, 407static void *zs_zpool_map(void *pool, unsigned long handle,
414 enum zpool_mapmode mm) 408 enum zpool_mapmode mm)
415{ 409{
@@ -447,7 +441,6 @@ static struct zpool_driver zs_zpool_driver = {
447 .destroy = zs_zpool_destroy, 441 .destroy = zs_zpool_destroy,
448 .malloc = zs_zpool_malloc, 442 .malloc = zs_zpool_malloc,
449 .free = zs_zpool_free, 443 .free = zs_zpool_free,
450 .shrink = zs_zpool_shrink,
451 .map = zs_zpool_map, 444 .map = zs_zpool_map,
452 .unmap = zs_zpool_unmap, 445 .unmap = zs_zpool_unmap,
453 .total_size = zs_zpool_total_size, 446 .total_size = zs_zpool_total_size,
diff --git a/mm/zswap.c b/mm/zswap.c
index 1133b4ceb72e..c004aa4fd3f4 100644
--- a/mm/zswap.c
+++ b/mm/zswap.c
@@ -1001,11 +1001,11 @@ static int zswap_frontswap_store(unsigned type, pgoff_t offset,
1001 struct zswap_entry *entry, *dupentry; 1001 struct zswap_entry *entry, *dupentry;
1002 struct crypto_comp *tfm; 1002 struct crypto_comp *tfm;
1003 int ret; 1003 int ret;
1004 unsigned int dlen = PAGE_SIZE, len; 1004 unsigned int hlen, dlen = PAGE_SIZE;
1005 unsigned long handle, value; 1005 unsigned long handle, value;
1006 char *buf; 1006 char *buf;
1007 u8 *src, *dst; 1007 u8 *src, *dst;
1008 struct zswap_header *zhdr; 1008 struct zswap_header zhdr = { .swpentry = swp_entry(type, offset) };
1009 1009
1010 if (!zswap_enabled || !tree) { 1010 if (!zswap_enabled || !tree) {
1011 ret = -ENODEV; 1011 ret = -ENODEV;
@@ -1063,8 +1063,8 @@ static int zswap_frontswap_store(unsigned type, pgoff_t offset,
1063 } 1063 }
1064 1064
1065 /* store */ 1065 /* store */
1066 len = dlen + sizeof(struct zswap_header); 1066 hlen = zpool_evictable(entry->pool->zpool) ? sizeof(zhdr) : 0;
1067 ret = zpool_malloc(entry->pool->zpool, len, 1067 ret = zpool_malloc(entry->pool->zpool, hlen + dlen,
1068 __GFP_NORETRY | __GFP_NOWARN | __GFP_KSWAPD_RECLAIM, 1068 __GFP_NORETRY | __GFP_NOWARN | __GFP_KSWAPD_RECLAIM,
1069 &handle); 1069 &handle);
1070 if (ret == -ENOSPC) { 1070 if (ret == -ENOSPC) {
@@ -1075,10 +1075,9 @@ static int zswap_frontswap_store(unsigned type, pgoff_t offset,
1075 zswap_reject_alloc_fail++; 1075 zswap_reject_alloc_fail++;
1076 goto put_dstmem; 1076 goto put_dstmem;
1077 } 1077 }
1078 zhdr = zpool_map_handle(entry->pool->zpool, handle, ZPOOL_MM_RW); 1078 buf = zpool_map_handle(entry->pool->zpool, handle, ZPOOL_MM_RW);
1079 zhdr->swpentry = swp_entry(type, offset); 1079 memcpy(buf, &zhdr, hlen);
1080 buf = (u8 *)(zhdr + 1); 1080 memcpy(buf + hlen, dst, dlen);
1081 memcpy(buf, dst, dlen);
1082 zpool_unmap_handle(entry->pool->zpool, handle); 1081 zpool_unmap_handle(entry->pool->zpool, handle);
1083 put_cpu_var(zswap_dstmem); 1082 put_cpu_var(zswap_dstmem);
1084 1083
@@ -1149,8 +1148,9 @@ static int zswap_frontswap_load(unsigned type, pgoff_t offset,
1149 1148
1150 /* decompress */ 1149 /* decompress */
1151 dlen = PAGE_SIZE; 1150 dlen = PAGE_SIZE;
1152 src = (u8 *)zpool_map_handle(entry->pool->zpool, entry->handle, 1151 src = zpool_map_handle(entry->pool->zpool, entry->handle, ZPOOL_MM_RO);
1153 ZPOOL_MM_RO) + sizeof(struct zswap_header); 1152 if (zpool_evictable(entry->pool->zpool))
1153 src += sizeof(struct zswap_header);
1154 dst = kmap_atomic(page); 1154 dst = kmap_atomic(page);
1155 tfm = *get_cpu_ptr(entry->pool->tfm); 1155 tfm = *get_cpu_ptr(entry->pool->tfm);
1156 ret = crypto_comp_decompress(tfm, src, entry->length, dst, &dlen); 1156 ret = crypto_comp_decompress(tfm, src, entry->length, dst, &dlen);