Skip to contentSkip to conversation

Allow ZRAM to use any zpool-compatible backend

PATCH NAK Vitaly Wool 2 participants 6 messages Dec 19, 2019 – Dec 20, 2019

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

v2 cover Allow ZRAM to use any zpool-compatible backend

The coming patchset is a new take on the old issue: ZRAM can currently be used only with zsmalloc even though this may not be the optimal combination for some configurations. The previous (unsuccessful) attempt dates back to 2015 [1] and is notable for the heated discussions it has caused.

This patchset addresses the increasing demand to deploy ZRAM in systems where zsmalloc is not a perfect match or is not applicable at all. An example of a system of the first type is an embedded system using ZRAM block device as a swap where quick application launch is critical for good user experience since z3fold is substantially faster on read than zsmalloc [2].

A system of the second type would, for instance, be the one with hardware on-the-fly RAM compression/decompression where the saved RAM space could be used for ZRAM but would require a special allocator.

The preliminary results for this work have been delivered at Linux Plumbers this year [3]. The talk at LPC ended in a consensus to continue the work and pursue the goal of decoupling ZRAM from zsmalloc.

The current patchset has been stress tested on arm64 and x86_64 devices, including the Dell laptop I'm writing this message on now, not to mention several QEmu confugirations.

The first version of this patchset can be found at [4]. Changelog since V1:
* better formatting
* allocator backend is now configurable on a per-ZRAM device basis
* allocator backend is runtime configurable via sysfs

[1] https://lkml.org/lkml/2015/9/14/356
[2] https://lkml.org/lkml/2019/10/21/743
[3] https://linuxplumbersconf.org/event/4/contributions/551/
[4] https://lkml.org/lkml/2019/10/10/1046

#1 lore raw

PATCH v2 1/3 zpool: add compaction api

This patch adds the following functions to the zpool API:
- zpool_compact()
- zpool_get_num_compacted()
- zpool_huge_class_size()

The first one triggers compaction for the underlying allocator, the second retrieves the number of pages migrated due to compaction for the whole time of this pool's existence and the third one returns the huge class size.

This API extension is done to align zpool API with zsmalloc API.

---
 include/linux/zpool.h | 14 +++++++++++++-
 mm/zpool.c            | 36 ++++++++++++++++++++++++++++++++++++
 2 files changed, 49 insertions(+), 1 deletion(-)
include/linux/zpool.h +13 -1
index 51bf43076165..31f0c1360569 100644
@@ -61,8 +61,13 @@ void *zpool_map_handle(struct zpool *pool, unsigned long handle,
 
 void zpool_unmap_handle(struct zpool *pool, unsigned long handle);
 
+unsigned long zpool_compact(struct zpool *pool);
+
+unsigned long zpool_get_num_compacted(struct zpool *pool);
+
 u64 zpool_get_total_size(struct zpool *pool);
 
+size_t zpool_huge_class_size(struct zpool *zpool);
 
 /**
  * struct zpool_driver - driver implementation for zpool
@@ -75,7 +80,10 @@ u64 zpool_get_total_size(struct zpool *pool);
  * @shrink:	shrink the pool.
  * @map:	map a handle.
  * @unmap:	unmap a handle.
- * @total_size:	get total size of a pool.
+ * @compact:	try to run compaction over a pool
+ * @get_num_compacted:	get amount of compacted pages for a pool
+ * @total_size:	get total size of a pool
+ * @huge_class_size: huge class threshold for pool pages.
  *
  * This is created by a zpool implementation and registered
  * with zpool.
@@ -104,7 +112,11 @@ struct zpool_driver {
 				enum zpool_mapmode mm);
 	void (*unmap)(void *pool, unsigned long handle);
 
+	unsigned long (*compact)(void *pool);
+	unsigned long (*get_num_compacted)(void *pool);
+
 	u64 (*total_size)(void *pool);
+	size_t (*huge_class_size)(void *pool);
 };
 
 void zpool_register_driver(struct zpool_driver *driver);
mm/zpool.c +36 -0
index 863669212070..55e69213c2eb 100644
@@ -362,6 +362,30 @@ void zpool_unmap_handle(struct zpool *zpool, unsigned long handle)
 	zpool->driver->unmap(zpool->pool, handle);
 }
 
+ /**
+ * zpool_compact() - try to run compaction over zpool
+ * @pool       The zpool to compact
+ *
+ * Returns: the number of migrated pages
+ */
+unsigned long zpool_compact(struct zpool *zpool)
+{
+	return zpool->driver->compact ? zpool->driver->compact(zpool->pool) : 0;
+}
+
+
+/**
+ * zpool_get_num_compacted() - get the number of migrated/compacted pages
+ * @pool       The zpool to get compaction statistic for
+ *
+ * Returns: the total number of migrated pages for the pool
+ */
+unsigned long zpool_get_num_compacted(struct zpool *zpool)
+{
+	return zpool->driver->get_num_compacted ?
+		zpool->driver->get_num_compacted(zpool->pool) : 0;
+}
+
 /**
  * zpool_get_total_size() - The total size of the pool
  * @zpool:	The zpool to check
@@ -375,6 +399,18 @@ u64 zpool_get_total_size(struct zpool *zpool)
 	return zpool->driver->total_size(zpool->pool);
 }
 
+/**
+ * zpool_huge_class_size() - get size for the "huge" class
+ * @pool	The zpool to check
+ *
+ * Returns: size of the huge class
+ */
+size_t zpool_huge_class_size(struct zpool *zpool)
+{
+	return zpool->driver->huge_class_size ?
+		zpool->driver->huge_class_size(zpool->pool) : 0;
+}
+
 /**
  * zpool_evictable() - Test if zpool is potentially evictable
  * @zpool:	The zpool to test
Signature · 1 line
2.20.1
#2 lore raw

PATCH v2 2/3 zsmalloc: add compaction and huge class callbacks

In reply to Vitaly Wool

Add compaction callbacks for zpool compaction API extension. Add huge_class_size callback too to be fully aligned.

---
 mm/zsmalloc.c | 21 +++++++++++++++++++++
 1 file changed, 21 insertions(+)
mm/zsmalloc.c +21 -0
index 2b2b9aae8a3c..43f43272b998 100644
@@ -437,11 +437,29 @@ static void zs_zpool_unmap(void *pool, unsigned long handle)
 	zs_unmap_object(pool, handle);
 }
 
+static unsigned long zs_zpool_compact(void *pool)
+{
+	return zs_compact(pool);
+}
+
+static unsigned long zs_zpool_get_compacted(void *pool)
+{
+	struct zs_pool_stats stats;
+
+	zs_pool_stats(pool, &stats);
+	return stats.pages_compacted;
+}
+
 static u64 zs_zpool_total_size(void *pool)
 {
 	return zs_get_total_pages(pool) << PAGE_SHIFT;
 }
 
+static size_t zs_zpool_huge_class_size(void *pool)
+{
+	return zs_huge_class_size(pool);
+}
+
 static struct zpool_driver zs_zpool_driver = {
 	.type =			  "zsmalloc",
 	.owner =		  THIS_MODULE,
@@ -453,6 +471,9 @@ static struct zpool_driver zs_zpool_driver = {
 	.map =			  zs_zpool_map,
 	.unmap =		  zs_zpool_unmap,
 	.total_size =		  zs_zpool_total_size,
+	.compact =		  zs_zpool_compact,
+	.get_num_compacted =	  zs_zpool_get_compacted,
+	.huge_class_size =	  zs_zpool_huge_class_size,
 };
 
 MODULE_ALIAS("zpool-zsmalloc");
Signature · 1 line
2.20.1
#3 lore raw

PATCH v2 3/3 zram: use common zpool interface

In reply to Vitaly Wool

This patch modifies ZRAM use a common "small allocator" API (zpool). This is done to address increasing demand to deploy ZRAM in systems where zsmalloc (which is currently the only available ZRAM allocator backend) is not a perfect match or is not applicable at all. An example of a system of the first type is an embedded system using ZRAM block device as a swap where quick application launch is critical for good user experience since z3fold is substantially faster on read than zsmalloc [1]. A system of the second type is, for instance, the one with hardware on-the-fly RAM compression/decompression where the saved RAM space could be used for ZRAM but would require a special allocator. These cases wre also discussed in detail at LPC'2019 [2].

zpool-registered backend can be selected using ZRAM sysfs interface on a per-device basis. 'zsmalloc' is taken by default.

This patch is transparent with regard to the existing ZRAM functionality and does not change its default behavior.

[1] https://lkml.org/lkml/2019/10/21/743
[2] https://linuxplumbersconf.org/event/4/contributions/551/

---
 drivers/block/zram/Kconfig    |   3 +-
 drivers/block/zram/zram_drv.c | 100 ++++++++++++++++++++++++----------
 drivers/block/zram/zram_drv.h |   6 +-
 3 files changed, 76 insertions(+), 33 deletions(-)
drivers/block/zram/Kconfig +2 -1
index fe7a4b7d30cf..7248d5aa3468 100644
@@ -1,8 +1,9 @@
 # SPDX-License-Identifier: GPL-2.0
 config ZRAM
 	tristate "Compressed RAM block device support"
-	depends on BLOCK && SYSFS && ZSMALLOC && CRYPTO
+	depends on BLOCK && SYSFS && CRYPTO
 	select CRYPTO_LZO
+	select ZPOOL
 	help
 	  Creates virtual block devices called /dev/zramX (X = 0, 1, ...).
 	  Pages written to these disks are compressed and stored in memory
drivers/block/zram/zram_drv.c +70 -30

238 lines not on this page · raw message

drivers/block/zram/zram_drv.h +4 -2
index f2fd46daa760..8a649a553a7a 100644
@@ -16,7 +16,7 @@
 #define _ZRAM_DRV_H_
 
 #include <linux/rwsem.h>
-#include <linux/zsmalloc.h>
+#include <linux/zpool.h>
 #include <linux/crypto.h>
 
 #include "zcomp.h"
@@ -28,6 +28,7 @@
 #define ZRAM_SECTOR_PER_LOGICAL_BLOCK	\
 	(1 << (ZRAM_LOGICAL_BLOCK_SHIFT - SECTOR_SHIFT))
 
+#define ZRAM_MAX_ALLOCATOR_NAME	64
 
 /*
  * The lower ZRAM_FLAG_SHIFT bits of table.flags is for
@@ -91,7 +92,7 @@ struct zram_stats {
 
 struct zram {
 	struct zram_table_entry *table;
-	struct zs_pool *mem_pool;
+	struct zpool *mem_pool;
 	struct zcomp *comp;
 	struct gendisk *disk;
 	/* Prevent concurrent execution of device init */
@@ -108,6 +109,7 @@ struct zram {
 	 */
 	u64 disksize;	/* bytes */
 	char compressor[CRYPTO_MAX_ALG_NAME];
+	char allocator[ZRAM_MAX_ALLOCATOR_NAME];
 	/*
 	 * zram is claimed so open request will be failed
 	 */
Signature · 1 line
2.20.1
maintainer Nacked-by on the v2 series NAK
#4 lore raw
In reply to Vitaly Wool
Quoted 6 lines · Vitaly Wool

The coming patchset is a new take on the old issue: ZRAM can currently be used only with zsmalloc even though this may not be the optimal combination for some configurations. The previous (unsuccessful) attempt dates back to 2015 [1] and is notable for the heated discussions it has caused.

Quoted 6 lines

This patchset addresses the increasing demand to deploy ZRAM in systems where zsmalloc is not a perfect match or is not applicable at all. An example of a system of the first type is an embedded system using ZRAM block device as a swap where quick application launch is critical for good user experience since z3fold is substantially faster on read than zsmalloc [2].

Look https://lkml.org/lkml/2019/10/29/1169

z3fold read is 15% faster *only* when when compression ratio is bad below 50% since zsmalloc involves copy operation if the object is located in the boundary of discrete pages. It's never popular workload.
Even, once write is involved(write-only, mixed read-write), z3fold is always slow. Think about that swap-in could cause swap out in the field because devices are usually under memory pressure. Also, look at the memory usage. zsmalloc saves bigger memory for all of compression ratios.

You claims flexibility - some user want fast read. How do you guarantee it is always fast? It depends on compression ratio. How can admin estimate runtime data compression ratio in advance? Their workload is read-only if they use zram as swap? they are okay to lose write performance and memory efficiency? Considering that, it's niche. I don't think it's worth to add maintenance burden here for very limited usecase.

Think about why we replaced xvmalloc with zsmalloc instead of creating wrapper to keep two allocators and why people push back so hard when we introduced even zsmalloc. Why zbud was born at the cost of sacrificing memory efficiency was concern about memory fragmenation of zsmalloc so freeing memory cannot make real free memory so wanted to manage fragmentation limit(However, we introduced object migration in zsmalloc afterward so the concern was gone now).

Quoted 4 lines

A system of the second type would, for instance, be the one with hardware on-the-fly RAM compression/decompression where the saved RAM space could be used for ZRAM but would require a special allocator.

I agree. For the special compressor, we would need other allocator, even huge zram changes in future for best performance. However, I'm not sure zpool is good fit for the case. We need discussion when that kinds of requirments comes up.

#5 lore raw

On Fri, Dec 20, 2019 at 4:14 AM Minchan Kim <[email protected]> wrote:

<snip>

Quoted 9 lines

Look https://lkml.org/lkml/2019/10/29/1169

z3fold read is 15% faster *only* when when compression ratio is bad below 50% since zsmalloc involves copy operation if the object is located in the boundary of discrete pages. It's never popular workload.
Even, once write is involved(write-only, mixed read-write), z3fold is always slow. Think about that swap-in could cause swap out in the field because devices are usually under memory pressure. Also, look at the memory usage. zsmalloc saves bigger memory for all of compression ratios.

Yes I remember that. Since your measurements were done on "an x86" without providing any detail on the actual platform used, they are as good as none.

~Vitaly