Skip to content

Commit 9c3760e

Browse files
Yu Zhaotorvalds
authored andcommitted
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. [[email protected]: The zpool->" prefix is a result of copy & paste] Link: http://lkml.kernel.org/r/[email protected] Link: http://lkml.kernel.org/r/[email protected] Signed-off-by: Yu Zhao <[email protected]> Acked-by: Dan Streetman <[email protected]> Reviewed-by: Sergey Senozhatsky <[email protected]> Cc: Seth Jennings <[email protected]> Cc: Minchan Kim <[email protected]> Signed-off-by: Andrew Morton <[email protected]> Signed-off-by: Linus Torvalds <[email protected]>
1 parent a7ab400 commit 9c3760e

File tree

4 files changed

+35
-19
lines changed

4 files changed

+35
-19
lines changed

include/linux/zpool.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,4 +108,6 @@ void zpool_register_driver(struct zpool_driver *driver);
108108

109109
int zpool_unregister_driver(struct zpool_driver *driver);
110110

111+
bool zpool_evictable(struct zpool *pool);
112+
111113
#endif

mm/zpool.c

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ struct zpool {
2121
struct zpool_driver *driver;
2222
void *pool;
2323
const struct zpool_ops *ops;
24+
bool evictable;
2425

2526
struct list_head list;
2627
};
@@ -142,7 +143,7 @@ EXPORT_SYMBOL(zpool_has_pool);
142143
*
143144
* This creates a new zpool of the specified type. The gfp flags will be
144145
* 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.
146147
*
147148
* Implementations must guarantee this to be thread-safe.
148149
*
@@ -180,6 +181,7 @@ struct zpool *zpool_create_pool(const char *type, const char *name, gfp_t gfp,
180181
zpool->driver = driver;
181182
zpool->pool = driver->create(name, gfp, ops, zpool);
182183
zpool->ops = ops;
184+
zpool->evictable = driver->shrink && ops && ops->evict;
183185

184186
if (!zpool->pool) {
185187
pr_err("couldn't create %s pool\n", type);
@@ -296,7 +298,8 @@ void zpool_free(struct zpool *zpool, unsigned long handle)
296298
int zpool_shrink(struct zpool *zpool, unsigned int pages,
297299
unsigned int *reclaimed)
298300
{
299-
return zpool->driver->shrink(zpool->pool, pages, reclaimed);
301+
return zpool->driver->shrink ?
302+
zpool->driver->shrink(zpool->pool, pages, reclaimed) : -EINVAL;
300303
}
301304

302305
/**
@@ -355,6 +358,24 @@ u64 zpool_get_total_size(struct zpool *zpool)
355358
return zpool->driver->total_size(zpool->pool);
356359
}
357360

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+
*/
374+
bool zpool_evictable(struct zpool *zpool)
375+
{
376+
return zpool->evictable;
377+
}
378+
358379
MODULE_LICENSE("GPL");
359380
MODULE_AUTHOR("Dan Streetman <[email protected]>");
360381
MODULE_DESCRIPTION("Common API for compressed memory storage");

mm/zsmalloc.c

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -404,12 +404,6 @@ static void zs_zpool_free(void *pool, unsigned long handle)
404404
zs_free(pool, handle);
405405
}
406406

407-
static int zs_zpool_shrink(void *pool, unsigned int pages,
408-
unsigned int *reclaimed)
409-
{
410-
return -EINVAL;
411-
}
412-
413407
static void *zs_zpool_map(void *pool, unsigned long handle,
414408
enum zpool_mapmode mm)
415409
{
@@ -447,7 +441,6 @@ static struct zpool_driver zs_zpool_driver = {
447441
.destroy = zs_zpool_destroy,
448442
.malloc = zs_zpool_malloc,
449443
.free = zs_zpool_free,
450-
.shrink = zs_zpool_shrink,
451444
.map = zs_zpool_map,
452445
.unmap = zs_zpool_unmap,
453446
.total_size = zs_zpool_total_size,

mm/zswap.c

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1001,11 +1001,11 @@ static int zswap_frontswap_store(unsigned type, pgoff_t offset,
10011001
struct zswap_entry *entry, *dupentry;
10021002
struct crypto_comp *tfm;
10031003
int ret;
1004-
unsigned int dlen = PAGE_SIZE, len;
1004+
unsigned int hlen, dlen = PAGE_SIZE;
10051005
unsigned long handle, value;
10061006
char *buf;
10071007
u8 *src, *dst;
1008-
struct zswap_header *zhdr;
1008+
struct zswap_header zhdr = { .swpentry = swp_entry(type, offset) };
10091009

10101010
if (!zswap_enabled || !tree) {
10111011
ret = -ENODEV;
@@ -1063,8 +1063,8 @@ static int zswap_frontswap_store(unsigned type, pgoff_t offset,
10631063
}
10641064

10651065
/* store */
1066-
len = dlen + sizeof(struct zswap_header);
1067-
ret = zpool_malloc(entry->pool->zpool, len,
1066+
hlen = zpool_evictable(entry->pool->zpool) ? sizeof(zhdr) : 0;
1067+
ret = zpool_malloc(entry->pool->zpool, hlen + dlen,
10681068
__GFP_NORETRY | __GFP_NOWARN | __GFP_KSWAPD_RECLAIM,
10691069
&handle);
10701070
if (ret == -ENOSPC) {
@@ -1075,10 +1075,9 @@ static int zswap_frontswap_store(unsigned type, pgoff_t offset,
10751075
zswap_reject_alloc_fail++;
10761076
goto put_dstmem;
10771077
}
1078-
zhdr = zpool_map_handle(entry->pool->zpool, handle, ZPOOL_MM_RW);
1079-
zhdr->swpentry = swp_entry(type, offset);
1080-
buf = (u8 *)(zhdr + 1);
1081-
memcpy(buf, dst, dlen);
1078+
buf = zpool_map_handle(entry->pool->zpool, handle, ZPOOL_MM_RW);
1079+
memcpy(buf, &zhdr, hlen);
1080+
memcpy(buf + hlen, dst, dlen);
10821081
zpool_unmap_handle(entry->pool->zpool, handle);
10831082
put_cpu_var(zswap_dstmem);
10841083

@@ -1149,8 +1148,9 @@ static int zswap_frontswap_load(unsigned type, pgoff_t offset,
11491148

11501149
/* decompress */
11511150
dlen = PAGE_SIZE;
1152-
src = (u8 *)zpool_map_handle(entry->pool->zpool, entry->handle,
1153-
ZPOOL_MM_RO) + sizeof(struct zswap_header);
1151+
src = zpool_map_handle(entry->pool->zpool, entry->handle, ZPOOL_MM_RO);
1152+
if (zpool_evictable(entry->pool->zpool))
1153+
src += sizeof(struct zswap_header);
11541154
dst = kmap_atomic(page);
11551155
tfm = *get_cpu_ptr(entry->pool->tfm);
11561156
ret = crypto_comp_decompress(tfm, src, entry->length, dst, &dlen);

0 commit comments

Comments
 (0)