Add function zs_shrink() to implement zs_pool shrinking. This allows the pool owner to reduce the size of the zs_pool by one zspage, which contains one or more struct pages. Once the zs_pool is shrunk, the freed pages are available for system use. This is used by zswap to limit its total system memory usage to a user-defined amount, while attempting to keep the most recently stored pages compressed in memory, and the oldest (or older) pages are evicted from the zsmalloc zs_pool and written out to swap disk. Signed-off-by: Dan Streetman <ddstreet@xxxxxxxx> Cc: Minchan Kim <minchan@xxxxxxxxxx> --- include/linux/zsmalloc.h | 1 + mm/zsmalloc.c | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+) diff --git a/include/linux/zsmalloc.h b/include/linux/zsmalloc.h index 2c341d4..07fe84d 100644 --- a/include/linux/zsmalloc.h +++ b/include/linux/zsmalloc.h @@ -45,6 +45,7 @@ void zs_destroy_pool(struct zs_pool *pool); unsigned long zs_malloc(struct zs_pool *pool, size_t size); void zs_free(struct zs_pool *pool, unsigned long obj); +int zs_shrink(struct zs_pool *pool); void *zs_map_object(struct zs_pool *pool, unsigned long handle, enum zs_mapmode mm); diff --git a/mm/zsmalloc.c b/mm/zsmalloc.c index 60fd23e..f769c21 100644 --- a/mm/zsmalloc.c +++ b/mm/zsmalloc.c @@ -1296,6 +1296,41 @@ void zs_free(struct zs_pool *pool, unsigned long obj) EXPORT_SYMBOL_GPL(zs_free); /** + * zs_shrink - Shrink the pool + * @pool: pool to shrink + * + * The pool will be shrunk by one zspage, which is some + * number of pages in size. On success, the number of freed + * pages is returned. On failure, the error is returned. + */ +int zs_shrink(struct zs_pool *pool) +{ + struct size_class *class; + enum fullness_group fullness; + struct page *page; + int class_idx, ret; + + if (!pool->ops || !pool->ops->evict) + return -EINVAL; + + /* if a page is found, the class is locked */ + page = find_lru_zspage(pool); + if (!page) + return -ENOENT; + + get_zspage_mapping(page, &class_idx, &fullness); + class = &pool->size_class[class_idx]; + + /* reclaim_zspage unlocks the class lock */ + ret = reclaim_zspage(pool, page); + if (ret) + return ret; + + return class->pages_per_zspage; +} +EXPORT_SYMBOL_GPL(zs_shrink); + +/** * zs_map_object - get address of allocated object from handle. * @pool: pool from which the object was allocated * @handle: handle returned from zs_malloc -- 1.8.3.1 -- To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to majordomo@xxxxxxxxx. For more info on Linux MM, see: http://www.linux-mm.org/ . Don't email: <a href=mailto:"dont@xxxxxxxxx"> email@xxxxxxxxx </a>