Skip to contentSkip to conversation

mm: add zblock allocator

PATCH In review Vitaly Wool 7 participants 20 messages Apr 12, 2025 – Jul 20, 2025

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

PATCH v4 1/1 mm: add zblock allocator

zblock is a special purpose allocator for storing compressed pages. It stores integer number of same size objects per its block. These blocks consist of several physical pages (2**n, i. e. 1/2/4/8).

With zblock, it is possible to densely arrange objects of various sizes resulting in low internal fragmentation. Also this allocator tries to fill incomplete blocks instead of adding new ones, in many cases providing a compression ratio comparable to zmalloc's.

zblock is also in most cases superior to zsmalloc with regard to average performance and worst execution times, thus allowing for better response time and real-time characteristics of the whole system.

High memory and page migration are currently not supported by zblock.

---
v3: https://patchwork.kernel.org/project/linux-mm/patch/[email protected]/
Changes since v3:
- rebased and tested against the latest -mm tree
- the block descriptors table was updated for better compression ratio
- fixed the bug with wrong SLOT_BITS value
- slot search moved to find_and_claim_block()

Test results (zstd compressor, 8 core Ryzen 9 VM, make bzImage):
- zblock:

    real	6m52.621s
    user	33m41.771s
    sys 	6m28.825s
    Zswap:            162328 kB
    Zswapped:         754468 kB
    zswpin 93851
    zswpout 542481
    zswpwb 935
- zsmalloc:
    real	7m4.355s
    user	34m37.538s
    sys 	6m22.086s
    zswpin 101243
    zswpout 448217
    zswpwb 640
    Zswap:            175704 kB
    Zswapped:         778692 kB
 Documentation/mm/zblock.rst |  24 ++
 MAINTAINERS                 |   7 +
 mm/Kconfig                  |  12 +
 mm/Makefile                 |   1 +
 mm/zblock.c                 | 443 ++++++++++++++++++++++++++++++++++++
 mm/zblock.h                 | 176 ++++++++++++++
 6 files changed, 663 insertions(+)
 create mode 100644 Documentation/mm/zblock.rst
 create mode 100644 mm/zblock.c
 create mode 100644 mm/zblock.h
Documentation/mm/zblock.rst new file +24 -0
new file mode 100644
index 000000000000..9751434d0b76
@@ -0,0 +1,24 @@
+.. SPDX-License-Identifier: GPL-2.0
+
+======
+zblock
+======
+
+zblock is a special purpose allocator for storing compressed pages.
+It stores integer number of compressed objects per its block. These
+blocks consist of several physical pages (2**n, i. e. 1/2/4/8).
+
+With zblock, it is possible to densely arrange objects of various sizes
+resulting in low internal fragmentation. Also this allocator tries to
+fill incomplete blocks instead of adding new ones,  in many cases
+providing a compression ratio substantially higher than z3fold and zbud
+(though lower than zmalloc's).
+
+zblock does not require MMU to operate and also is superior to zsmalloc
+with regard to average performance and worst execution times, thus
+allowing for better response time and real-time characteristics of the
+whole system.
+
+E. g. on a series of stress-ng tests run on a Raspberry Pi 5, we get
+5-10% higher value for bogo ops/s in zblock/zsmalloc comparison.
+
MAINTAINERS +7 -0
index 96b827049501..46465c986005 100644
@@ -26640,6 +26640,13 @@ F:	Documentation/networking/device_drivers/hamradio/z8530drv.rst
 F:	drivers/net/hamradio/*scc.c
 F:	drivers/net/hamradio/z8530.h
 
+ZBLOCK COMPRESSED SLAB MEMORY ALLOCATOR
+M:	Vitaly Wool <[email protected]>
+L:	[email protected]
+S:	Maintained
+F:	Documentation/mm/zblock.rst
+F:	mm/zblock.[ch]
+
 ZD1211RW WIRELESS DRIVER
 L:	[email protected]
 S:	Orphan
mm/Kconfig +12 -0
index e113f713b493..5aa1479151ec 100644
@@ -152,6 +152,18 @@ config ZSWAP_ZPOOL_DEFAULT
        default "zsmalloc" if ZSWAP_ZPOOL_DEFAULT_ZSMALLOC
        default ""
 
+config ZBLOCK
+	tristate "Fast compression allocator with high density"
+	depends on ZPOOL
+	help
+	  A special purpose allocator for storing compressed pages.
+	  It stores integer number of same size compressed objects per
+	  its block. These blocks consist of several physical pages
+	  (2**n, i. e. 1/2/4/8).
+
+	  With zblock, it is possible to densely arrange objects of
+	  various sizes resulting in low internal fragmentation.
+
 config ZSMALLOC
 	tristate
 	prompt "N:1 compression allocator (zsmalloc)" if (ZSWAP || ZRAM)
mm/Makefile +1 -0
index e7f6bbf8ae5f..9d7e5b5bb694 100644
@@ -116,6 +116,7 @@ obj-$(CONFIG_DEBUG_VM_PGTABLE) += debug_vm_pgtable.o
 obj-$(CONFIG_PAGE_OWNER) += page_owner.o
 obj-$(CONFIG_MEMORY_ISOLATION) += page_isolation.o
 obj-$(CONFIG_ZPOOL)	+= zpool.o
+obj-$(CONFIG_ZBLOCK)	+= zblock.o
 obj-$(CONFIG_ZSMALLOC)	+= zsmalloc.o
 obj-$(CONFIG_GENERIC_EARLY_IOREMAP) += early_ioremap.o
 obj-$(CONFIG_CMA)	+= cma.o
mm/zblock.c new file +443 -0

443 lines not on this page · raw message

mm/zblock.h new file +176 -0

176 lines not on this page · raw message

Signature · 1 line
2.49.0
Tested-by on v4 1/1
#1 lore raw

2025-04-12 19:42 skrev Vitaly Wool:

Quoted 9 lines

zblock is a special purpose allocator for storing compressed pages. It stores integer number of same size objects per its block. These blocks consist of several physical pages (2**n, i. e. 1/2/4/8).

With zblock, it is possible to densely arrange objects of various sizes resulting in low internal fragmentation. Also this allocator tries to fill incomplete blocks instead of adding new ones, in many cases providing a compression ratio comparable to zmalloc's.

Quoted 8 lines

zblock is also in most cases superior to zsmalloc with regard to average performance and worst execution times, thus allowing for better response time and real-time characteristics of the whole system.

High memory and page migration are currently not supported by zblock.

Quoted 782 lines

---
v3: https://patchwork.kernel.org/project/linux-mm/patch/[email protected]/
Changes since v3:
- rebased and tested against the latest -mm tree
- the block descriptors table was updated for better compression ratio
- fixed the bug with wrong SLOT_BITS value
- slot search moved to find_and_claim_block()

Test results (zstd compressor, 8 core Ryzen 9 VM, make bzImage):
- zblock:

    real	6m52.621s
    user	33m41.771s
    sys 	6m28.825s
    Zswap:            162328 kB
    Zswapped:         754468 kB
    zswpin 93851
    zswpout 542481
    zswpwb 935
- zsmalloc:
    real	7m4.355s
    user	34m37.538s
    sys 	6m22.086s
    zswpin 101243
    zswpout 448217
    zswpwb 640
    Zswap:            175704 kB
    Zswapped:         778692 kB
 Documentation/mm/zblock.rst |  24 ++
 MAINTAINERS                 |   7 +
 mm/Kconfig                  |  12 +
 mm/Makefile                 |   1 +
 mm/zblock.c                 | 443 ++++++++++++++++++++++++++++++++++++
 mm/zblock.h                 | 176 ++++++++++++++
 6 files changed, 663 insertions(+)
 create mode 100644 Documentation/mm/zblock.rst
 create mode 100644 mm/zblock.c
 create mode 100644 mm/zblock.h
Documentation/mm/zblock.rst new file +11 -0
new file mode 100644
index 000000000000..9751434d0b76
@@ -0,0 +1,24 @@
+.. SPDX-License-Identifier: GPL-2.0
+
+======
+zblock
+======
+
+zblock is a special purpose allocator for storing compressed pages.
+It stores integer number of compressed objects per its block. These
+blocks consist of several physical pages (2**n, i. e. 1/2/4/8).
+
+With zblock, it is possible to densely arrange objects of various 

sizes

Documentation/mm/zblock.rst new file +3 -0
+resulting in low internal fragmentation. Also this allocator tries to
+fill incomplete blocks instead of adding new ones,  in many cases
+providing a compression ratio substantially higher than z3fold and 

zbud

Documentation/mm/zblock.rst new file +3 -0
+(though lower than zmalloc's).
+
+zblock does not require MMU to operate and also is superior to 

zsmalloc

Documentation/mm/zblock.rst new file +7 -0
+with regard to average performance and worst execution times, thus
+allowing for better response time and real-time characteristics of the
+whole system.
+
+E. g. on a series of stress-ng tests run on a Raspberry Pi 5, we get
+5-10% higher value for bogo ops/s in zblock/zsmalloc comparison.
+
MAINTAINERS +0 -0
index 96b827049501..46465c986005 100644
@@ -26640,6 +26640,13 @@ 

F: Documentation/networking/device_drivers/hamradio/z8530drv.rst
F: drivers/net/hamradio/*scc.c
F: drivers/net/hamradio/z8530.h

MAINTAINERS +7 -0
+ZBLOCK COMPRESSED SLAB MEMORY ALLOCATOR
+M:	Vitaly Wool <[email protected]>
+L:	[email protected]
+S:	Maintained
+F:	Documentation/mm/zblock.rst
+F:	mm/zblock.[ch]
+
 ZD1211RW WIRELESS DRIVER
 L:	[email protected]
 S:	Orphan
mm/Kconfig +12 -0
index e113f713b493..5aa1479151ec 100644
@@ -152,6 +152,18 @@ config ZSWAP_ZPOOL_DEFAULT
        default "zsmalloc" if ZSWAP_ZPOOL_DEFAULT_ZSMALLOC
        default ""
 
+config ZBLOCK
+	tristate "Fast compression allocator with high density"
+	depends on ZPOOL
+	help
+	  A special purpose allocator for storing compressed pages.
+	  It stores integer number of same size compressed objects per
+	  its block. These blocks consist of several physical pages
+	  (2**n, i. e. 1/2/4/8).
+
+	  With zblock, it is possible to densely arrange objects of
+	  various sizes resulting in low internal fragmentation.
+
 config ZSMALLOC
 	tristate
 	prompt "N:1 compression allocator (zsmalloc)" if (ZSWAP || ZRAM)
mm/Makefile +0 -0
index e7f6bbf8ae5f..9d7e5b5bb694 100644
@@ -116,6 +116,7 @@ obj-$(CONFIG_DEBUG_VM_PGTABLE) += 

debug_vm_pgtable.o
obj-$(CONFIG_PAGE_OWNER) += page_owner.o obj-$(CONFIG_MEMORY_ISOLATION) += page_isolation.o
obj-$(CONFIG_ZPOOL) += zpool.o

mm/Makefile +1 -0
+obj-$(CONFIG_ZBLOCK)	+= zblock.o
 obj-$(CONFIG_ZSMALLOC)	+= zsmalloc.o
 obj-$(CONFIG_GENERIC_EARLY_IOREMAP) += early_ioremap.o
 obj-$(CONFIG_CMA)	+= cma.o
mm/zblock.c new file +12 -0
new file mode 100644
index 000000000000..ecc7aeb611af
@@ -0,0 +1,443 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * zblock.c
+ *
+ * Author: Vitaly Wool <[email protected]>
+ * Based on the work from Ananda Badmaev <[email protected]>
+ * Copyright (C) 2022-2025, Konsulko AB.
+ *
+ * Zblock is a small object allocator with the intention to serve as a
+ * zpool backend. It operates on page blocks which consist of number
+ * of physical pages being a power of 2 and store integer number of
+ * compressed pages per block which results in determinism and 

simplicity.
+ *
+ * zblock doesn't export any API and is meant to be used via zpool API.
+ */

mm/zblock.c new file +17 -0
+
+#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
+
+#include <linux/atomic.h>
+#include <linux/list.h>
+#include <linux/mm.h>
+#include <linux/module.h>
+#include <linux/preempt.h>
+#include <linux/slab.h>
+#include <linux/spinlock.h>
+#include <linux/zpool.h>
+#include "zblock.h"
+
+static struct rb_root block_desc_tree = RB_ROOT;
+
+/* Encode handle of a particular slot in the pool using metadata */
+static inline unsigned long metadata_to_handle(struct zblock_block 

*block,

mm/zblock.c new file +6 -0
+				unsigned int block_type, unsigned int slot)
+{
+	return (unsigned long)(block) | (block_type << SLOT_BITS) | slot;
+}
+
+/* Return block, block type and slot in the pool corresponding to 

handle */
+static inline struct zblock_block *handle_to_metadata(unsigned long handle,

mm/zblock.c new file +12 -0
+				unsigned int *block_type, unsigned int *slot)
+{
+	*block_type = (handle & (PAGE_SIZE - 1)) >> SLOT_BITS;
+	*slot = handle & SLOT_MASK;
+	return (struct zblock_block *)(handle & PAGE_MASK);
+}
+
+/*
+ * Find a block with at least one free slot and claim it.
+ * We make sure that the first block, if exists, will always work.
+ */
+static inline struct zblock_block *find_and_claim_block(struct 

block_list *b,

mm/zblock.c new file +71 -0
+		int block_type, unsigned long *handle)
+{
+	struct list_head *l = &b->active_list;
+	unsigned int slot;
+
+	if (!list_empty(l)) {
+		struct zblock_block *z = list_first_entry(l, typeof(*z), link);
+
+		if (--z->free_slots == 0)
+			list_move(&z->link, &b->full_list);
+		/*
+		 * There is a slot in the block and we just made sure it would
+		 * remain.
+		 * Find that slot and set the busy bit.
+		 */
+		for (slot = find_first_zero_bit(z->slot_info,
+					block_desc[block_type].slots_per_block);
+		     slot < block_desc[block_type].slots_per_block;
+		     slot = find_next_zero_bit(z->slot_info,
+					block_desc[block_type].slots_per_block,
+					slot)) {
+			if (!test_and_set_bit(slot, z->slot_info))
+				break;
+			barrier();
+		}
+
+		WARN_ON(slot >= block_desc[block_type].slots_per_block);
+		*handle = metadata_to_handle(z, block_type, slot);
+		return z;
+	}
+	return NULL;
+}
+
+/*
+ * allocate new block and add it to corresponding block list
+ */
+static struct zblock_block *alloc_block(struct zblock_pool *pool,
+					int block_type, gfp_t gfp,
+					unsigned long *handle)
+{
+	struct zblock_block *block;
+	struct block_list *block_list;
+
+	block = (void *)__get_free_pages(gfp, block_desc[block_type].order);
+	if (!block)
+		return NULL;
+
+	block_list = &pool->block_lists[block_type];
+
+	/* init block data  */
+	block->free_slots = block_desc[block_type].slots_per_block - 1;
+	memset(&block->slot_info, 0, sizeof(block->slot_info));
+	set_bit(0, block->slot_info);
+	*handle = metadata_to_handle(block, block_type, 0);
+
+	spin_lock(&block_list->lock);
+	list_add(&block->link, &block_list->active_list);
+	block_list->block_count++;
+	spin_unlock(&block_list->lock);
+	return block;
+}
+
+/*****************
+ * API Functions
+ *****************/
+/**
+ * zblock_create_pool() - create a new zblock pool
+ * @gfp:	gfp flags when allocating the zblock pool structure
+ * @ops:	user-defined operations for the zblock pool
+ *
+ * Return: pointer to the new zblock pool or NULL if the metadata 

allocation
+ * failed.
+ */

mm/zblock.c new file +40 -0
+static struct zblock_pool *zblock_create_pool(gfp_t gfp)
+{
+	struct zblock_pool *pool;
+	struct block_list *block_list;
+	int i;
+
+	pool = kmalloc(sizeof(struct zblock_pool), gfp);
+	if (!pool)
+		return NULL;
+
+	/* init each block list */
+	for (i = 0; i < ARRAY_SIZE(block_desc); i++) {
+		block_list = &pool->block_lists[i];
+		spin_lock_init(&block_list->lock);
+		INIT_LIST_HEAD(&block_list->full_list);
+		INIT_LIST_HEAD(&block_list->active_list);
+		block_list->block_count = 0;
+	}
+	return pool;
+}
+
+/**
+ * zblock_destroy_pool() - destroys an existing zblock pool
+ * @pool:	the zblock pool to be destroyed
+ *
+ */
+static void zblock_destroy_pool(struct zblock_pool *pool)
+{
+	kfree(pool);
+}
+
+
+/**
+ * zblock_alloc() - allocates a slot of appropriate size
+ * @pool:	zblock pool from which to allocate
+ * @size:	size in bytes of the desired allocation
+ * @gfp:	gfp flags used if the pool needs to grow
+ * @handle:	handle of the new allocation
+ *
+ * Return: 0 if success and handle is set, otherwise -EINVAL if the 

size or
+ * gfp arguments are invalid or -ENOMEM if the pool was unable to allocate
+ * a new slot.
+ */
+static int zblock_alloc(struct zblock_pool *pool, size_t size, gfp_t gfp,

mm/zblock.c new file +46 -0
+			unsigned long *handle)
+{
+	int block_type = -1;
+	struct zblock_block *block;
+	struct block_list *block_list;
+
+	if (!size)
+		return -EINVAL;
+
+	if (size > PAGE_SIZE)
+		return -ENOSPC;
+
+	/* find basic block type with suitable slot size */
+	if (size < block_desc[0].slot_size)
+		block_type = 0;
+	else {
+		struct block_desc_node *block_node;
+		struct rb_node *node = block_desc_tree.rb_node;
+
+		while (node) {
+			block_node = container_of(node, typeof(*block_node), node);
+			if (size < block_node->this_slot_size)
+				node = node->rb_left;
+			else if (size >= block_node->next_slot_size)
+				node = node->rb_right;
+			else {
+				block_type = block_node->block_idx + 1;
+				break;
+			}
+		}
+	}
+	if (WARN_ON(block_type < 0))
+		return -EINVAL;
+	if (block_type >= ARRAY_SIZE(block_desc))
+		return -ENOSPC;
+
+	block_list = &pool->block_lists[block_type];
+
+	spin_lock(&block_list->lock);
+	block = find_and_claim_block(block_list, block_type, handle);
+	spin_unlock(&block_list->lock);
+	if (block)
+		return 0;
+
+	/* not found block with free slots try to allocate new empty block */
+	block = alloc_block(pool, block_type, gfp & ~(__GFP_MOVABLE | 

__GFP_HIGHMEM), handle);

mm/zblock.c new file +5 -0
+	return block ? 0 : -ENOMEM;
+}
+
+/**
+ * zblock_free() - frees the allocation associated with the given 

handle
+ * @pool: pool in which the allocation resided
+ * @handle: handle associated with the allocation returned by
zblock_alloc()
+ *
+ */
+static void zblock_free(struct zblock_pool *pool, unsigned long handle)

mm/zblock.c new file +31 -0
+{
+	unsigned int slot, block_type;
+	struct zblock_block *block;
+	struct block_list *block_list;
+
+	block = handle_to_metadata(handle, &block_type, &slot);
+	block_list = &pool->block_lists[block_type];
+
+	spin_lock(&block_list->lock);
+	/* if all slots in block are empty delete whole block */
+	if (++block->free_slots == block_desc[block_type].slots_per_block) {
+		block_list->block_count--;
+		list_del(&block->link);
+		spin_unlock(&block_list->lock);
+		free_pages((unsigned long)block, block_desc[block_type].order);
+		return;
+	} else if (block->free_slots == 1)
+		list_move_tail(&block->link, &block_list->active_list);
+	clear_bit(slot, block->slot_info);
+	spin_unlock(&block_list->lock);
+}
+
+/**
+ * zblock_map() - maps the allocation associated with the given handle
+ * @pool:	pool in which the allocation resides
+ * @handle:	handle associated with the allocation to be mapped
+ *
+ *
+ * Returns: a pointer to the mapped allocation
+ */
+static void *zblock_map(struct zblock_pool *pool, unsigned long 

handle)

mm/zblock.c new file +14 -0
+{
+	unsigned int block_type, slot;
+	struct zblock_block *block;
+	unsigned long offs;
+	void *p;
+
+	block = handle_to_metadata(handle, &block_type, &slot);
+	offs = ZBLOCK_HEADER_SIZE + slot * block_desc[block_type].slot_size;
+	p = (void *)block + offs;
+	return p;
+}
+
+/**
+ * zblock_unmap() - unmaps the allocation associated with the given 

handle
+ * @pool: pool in which the allocation resides
+ * @handle: handle associated with the allocation to be unmapped
+ */
+static void zblock_unmap(struct zblock_pool *pool, unsigned long handle)

mm/zblock.c new file +11 -0
+{
+}
+
+/**
+ * zblock_write() - write to the memory area defined by handle
+ * @pool:	pool in which the allocation resides
+ * @handle:	handle associated with the allocation
+ * @handle_mem: pointer to source memory block
+ * @mem_len:	length of the memory block to write
+ */
+static void zblock_write(struct zblock_pool *pool, unsigned long 

handle,

mm/zblock.c new file +27 -0
+			 void *handle_mem, size_t mem_len)
+{
+	unsigned int block_type, slot;
+	struct zblock_block *block;
+	unsigned long offs;
+	void *p;
+
+	block = handle_to_metadata(handle, &block_type, &slot);
+	offs = ZBLOCK_HEADER_SIZE + slot * block_desc[block_type].slot_size;
+	p = (void *)block + offs;
+	memcpy(p, handle_mem, mem_len);
+}
+
+/**
+ * zblock_get_total_pages() - gets the zblock pool size in pages
+ * @pool:	pool being queried
+ *
+ * Returns: size in bytes of the given pool.
+ */
+static u64 zblock_get_total_pages(struct zblock_pool *pool)
+{
+	u64 total_size;
+	int i;
+
+	total_size = 0;
+	for (i = 0; i < ARRAY_SIZE(block_desc); i++)
+		total_size += pool->block_lists[i].block_count << 

block_desc[i].order;

mm/zblock.c new file +132 -0
+
+	return total_size;
+}
+
+/*****************
+ * zpool
+ ****************/
+
+static void *zblock_zpool_create(const char *name, gfp_t gfp)
+{
+	return zblock_create_pool(gfp);
+}
+
+static void zblock_zpool_destroy(void *pool)
+{
+	zblock_destroy_pool(pool);
+}
+
+static int zblock_zpool_malloc(void *pool, size_t size, gfp_t gfp,
+			unsigned long *handle)
+{
+	return zblock_alloc(pool, size, gfp, handle);
+}
+
+static void zblock_zpool_free(void *pool, unsigned long handle)
+{
+	zblock_free(pool, handle);
+}
+
+static void *zblock_zpool_read_begin(void *pool, unsigned long handle,
+				void *local_copy)
+{
+	return zblock_map(pool, handle);
+}
+
+static void zblock_zpool_obj_write(void *pool, unsigned long handle,
+				void *handle_mem, size_t mem_len)
+{
+	zblock_write(pool, handle, handle_mem, mem_len);
+}
+
+static void zblock_zpool_read_end(void *pool, unsigned long handle,
+				void *handle_mem)
+{
+	zblock_unmap(pool, handle);
+}
+
+static u64 zblock_zpool_total_pages(void *pool)
+{
+	return zblock_get_total_pages(pool);
+}
+
+static struct zpool_driver zblock_zpool_driver = {
+	.type =			"zblock",
+	.owner =		THIS_MODULE,
+	.create =		zblock_zpool_create,
+	.destroy =		zblock_zpool_destroy,
+	.malloc =		zblock_zpool_malloc,
+	.free =			zblock_zpool_free,
+	.obj_read_begin =	zblock_zpool_read_begin,
+	.obj_read_end =		zblock_zpool_read_end,
+	.obj_write =		zblock_zpool_obj_write,
+	.total_pages =		zblock_zpool_total_pages,
+};
+
+MODULE_ALIAS("zpool-zblock");
+
+static void delete_rbtree(void)
+{
+	while (!RB_EMPTY_ROOT(&block_desc_tree))
+		rb_erase(block_desc_tree.rb_node, &block_desc_tree);
+}
+
+static int __init create_rbtree(void)
+{
+	int i;
+
+	for (i = 0; i < ARRAY_SIZE(block_desc); i++) {
+		struct block_desc_node *block_node = kmalloc(sizeof(*block_node),
+							GFP_KERNEL);
+		struct rb_node **new = &block_desc_tree.rb_node, *parent = NULL;
+
+		if (!block_node) {
+			delete_rbtree();
+			return -ENOMEM;
+		}
+		if (i > 0 && block_desc[i].slot_size <= block_desc[i-1].slot_size) {
+			pr_err("%s: block descriptors not in ascending order\n",
+				__func__);
+			delete_rbtree();
+			return -EINVAL;
+		}
+		block_node->this_slot_size = block_desc[i].slot_size;
+		block_node->block_idx = i;
+		if (i == ARRAY_SIZE(block_desc) - 1)
+			block_node->next_slot_size = PAGE_SIZE;
+		else
+			block_node->next_slot_size = block_desc[i+1].slot_size;
+		while (*new) {
+			parent = *new;
+			/* the array is sorted so we will always go to the right */
+			new = &((*new)->rb_right);
+		}
+		rb_link_node(&block_node->node, parent, new);
+		rb_insert_color(&block_node->node, &block_desc_tree);
+	}
+	return 0;
+}
+
+static int __init init_zblock(void)
+{
+	int ret = create_rbtree();
+
+	if (ret)
+		return ret;
+
+	zpool_register_driver(&zblock_zpool_driver);
+	return 0;
+}
+
+static void __exit exit_zblock(void)
+{
+	zpool_unregister_driver(&zblock_zpool_driver);
+	delete_rbtree();
+}
+
+module_init(init_zblock);
+module_exit(exit_zblock);
+
+MODULE_LICENSE("GPL");
+MODULE_AUTHOR("Vitaly Wool <[email protected]>");
+MODULE_DESCRIPTION("Block allocator for compressed pages");
mm/zblock.h new file +30 -0
new file mode 100644
index 000000000000..fd72961c077a
@@ -0,0 +1,176 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/*
+ * Author: Vitaly Wool <[email protected]>
+ * Copyright (C) 2025, Konsulko AB.
+ */
+#ifndef __ZBLOCK_H__
+#define __ZBLOCK_H__
+
+#include <linux/mm.h>
+#include <linux/rbtree.h>
+#include <linux/types.h>
+
+#define SLOT_FREE 0
+#define BIT_SLOT_OCCUPIED 0
+#define BIT_SLOT_MAPPED 1
+
+#if PAGE_SIZE == 0x1000
+/* max 128 slots per block, max table size 32 */
+#define SLOT_BITS 7
+#elif PAGE_SIZE == 0x4000
+/* max 256 slots per block, max table size 64 */
+#define SLOT_BITS 8
+#else
+#error Unsupported PAGE_SIZE
+#endif
+
+#define MAX_SLOTS (1 << SLOT_BITS)
+#define SLOT_MASK ((0x1UL << SLOT_BITS) - 1)
+
+#define ZBLOCK_HEADER_SIZE	round_up(sizeof(struct zblock_block), 

sizeof(long))
+#define BLOCK_DATA_SIZE(order) ((PAGE_SIZE << order) - ZBLOCK_HEADER_SIZE)
+#define SLOT_SIZE(nslots, order) (round_down((BLOCK_DATA_SIZE(order) / nslots), sizeof(long)))

mm/zblock.h new file +18 -0
+
+/**
+ * struct zblock_block - block metadata
+ * Block consists of several (1/2/4/8) pages and contains fixed
+ * integer number of slots for allocating compressed pages.
+ *
+ * free_slots:	number of free slots in the block
+ * slot_info:	contains data about free/occupied slots
+ */
+struct zblock_block {
+	struct list_head link;
+	DECLARE_BITMAP(slot_info, 1 << SLOT_BITS);
+	u32 free_slots;
+};
+
+/**
+ * struct block_desc - general metadata for block lists
+ * Each block list stores only blocks of corresponding type which 

means
+ * that all blocks in it have the same number and size of slots.
+ * All slots are aligned to size of long.
+ *
+ * slot_size: size of slot for this list
+ * slots_per_block: number of slots per block for this list
+ * order: order for __get_free_pages
+ */

mm/zblock.h new file +110 -0
+struct block_desc {
+	unsigned int slot_size;
+	unsigned short slots_per_block;
+	unsigned short order;
+};
+
+struct block_desc_node {
+	struct rb_node node;
+	unsigned int this_slot_size;
+	unsigned int next_slot_size;
+	unsigned int block_idx;
+};
+
+static const struct block_desc block_desc[] = {
+#if PAGE_SIZE == 0x1000
+	{ SLOT_SIZE(63, 0), 63, 0 },
+	{ SLOT_SIZE(32, 0), 32, 0 },
+	{ SLOT_SIZE(21, 0), 21, 0 },
+	{ SLOT_SIZE(15, 0), 15, 0 },
+	{ SLOT_SIZE(12, 0), 12, 0 },
+	{ SLOT_SIZE(10, 0), 10, 0 },
+	{ SLOT_SIZE(9, 0), 9, 0 },
+	{ SLOT_SIZE(8, 0), 8, 0 },
+	{ SLOT_SIZE(29, 2), 29, 2 },
+	{ SLOT_SIZE(13, 1), 13, 1 },
+	{ SLOT_SIZE(6, 0), 6, 0 },
+	{ SLOT_SIZE(11, 1), 11, 1 },
+	{ SLOT_SIZE(5, 0), 5, 0 },
+	{ SLOT_SIZE(9, 1), 9, 1 },
+	{ SLOT_SIZE(8, 1), 8, 1 },
+	{ SLOT_SIZE(29, 3), 29, 3 },
+	{ SLOT_SIZE(13, 2), 13, 2 },
+	{ SLOT_SIZE(12, 2), 12, 2 },
+	{ SLOT_SIZE(11, 2), 11, 2 },
+	{ SLOT_SIZE(10, 2), 10, 2 },
+	{ SLOT_SIZE(9, 2), 9, 2 },
+	{ SLOT_SIZE(17, 3), 17, 3 },
+	{ SLOT_SIZE(8, 2), 8, 2 },
+	{ SLOT_SIZE(15, 3), 15, 3 },
+	{ SLOT_SIZE(14, 3), 14, 3 },
+	{ SLOT_SIZE(13, 3), 13, 3 },
+	{ SLOT_SIZE(6, 2), 6, 2 },
+	{ SLOT_SIZE(11, 3), 11, 3 },
+	{ SLOT_SIZE(10, 3), 10, 3 },
+	{ SLOT_SIZE(9, 3), 9, 3 },
+	{ SLOT_SIZE(4, 2), 4, 2 },
+#elif PAGE_SIZE == 0x4000
+	{ SLOT_SIZE(255, 0), 255, 0 },
+	{ SLOT_SIZE(185, 0), 185, 0 },
+	{ SLOT_SIZE(145, 0), 145, 0 },
+	{ SLOT_SIZE(113, 0), 113, 0 },
+	{ SLOT_SIZE(92, 0), 92, 0 },
+	{ SLOT_SIZE(75, 0), 75, 0 },
+	{ SLOT_SIZE(60, 0), 60, 0 },
+	{ SLOT_SIZE(51, 0), 51, 0 },
+	{ SLOT_SIZE(43, 0), 43, 0 },
+	{ SLOT_SIZE(37, 0), 37, 0 },
+	{ SLOT_SIZE(32, 0), 32, 0 },
+	{ SLOT_SIZE(27, 0), 27, 0 },
+	{ SLOT_SIZE(23, 0), 23, 0 },
+	{ SLOT_SIZE(19, 0), 19, 0 },
+	{ SLOT_SIZE(17, 0), 17, 0 },
+	{ SLOT_SIZE(15, 0), 15, 0 },
+	{ SLOT_SIZE(13, 0), 13, 0 },
+	{ SLOT_SIZE(11, 0), 11, 0 },
+	{ SLOT_SIZE(10, 0), 10, 0 },
+	{ SLOT_SIZE(9, 0), 9, 0 },
+	{ SLOT_SIZE(8, 0), 8, 0 },
+	{ SLOT_SIZE(15, 1), 15, 1 },
+	{ SLOT_SIZE(14, 1), 14, 1 },
+	{ SLOT_SIZE(13, 1), 13, 1 },
+	{ SLOT_SIZE(12, 1), 12, 1 },
+	{ SLOT_SIZE(11, 1), 11, 1 },
+	{ SLOT_SIZE(10, 1), 10, 1 },
+	{ SLOT_SIZE(9, 1), 9, 1 },
+	{ SLOT_SIZE(8, 1), 8, 1 },
+	{ SLOT_SIZE(15, 2), 15, 2 },
+	{ SLOT_SIZE(14, 2), 14, 2 },
+	{ SLOT_SIZE(13, 2), 13, 2 },
+	{ SLOT_SIZE(12, 2), 12, 2 },
+	{ SLOT_SIZE(11, 2), 11, 2 },
+	{ SLOT_SIZE(10, 2), 10, 2 },
+	{ SLOT_SIZE(9, 2), 9, 2 },
+	{ SLOT_SIZE(8, 2), 8, 2 },
+	{ SLOT_SIZE(7, 2), 7, 2 },
+	{ SLOT_SIZE(6, 2), 6, 2 },
+	{ SLOT_SIZE(5, 2), 5, 2 },
+#endif /* PAGE_SIZE */
+};
+
+/**
+ * struct block_list - stores metadata of particular list
+ * lock:		protects the list of blocks
+ * active_list:		linked list of active (non-full) blocks
+ * full_list:		linked list of full blocks
+ * block_count:		total number of blocks in the list
+ */
+struct block_list {
+	spinlock_t lock;
+	struct list_head active_list;
+	struct list_head full_list;
+	unsigned long block_count;
+};
+
+/**
+ * struct zblock_pool - stores metadata for each zblock pool
+ * @block_lists:	array of block lists
+ * @zpool:		zpool driver
+ *
+ * This structure is allocated at pool creation time and maintains 

metadata
+ * for a particular zblock pool.
+ */

mm/zblock.h new file +7 -0
+struct zblock_pool {
+	struct block_list block_lists[ARRAY_SIZE(block_desc)];
+	struct zpool *zpool;
+};
+
+
+#endif
#2 lore raw
In reply to Vitaly Wool
Quoted 4 lines · Vitaly Wool

zblock is a special purpose allocator for storing compressed pages. It stores integer number of same size objects per its block. These blocks consist of several physical pages (2**n, i. e. 1/2/4/8).

Quoted 8 lines

With zblock, it is possible to densely arrange objects of various sizes resulting in low internal fragmentation. Also this allocator tries to fill incomplete blocks instead of adding new ones, in many cases providing a compression ratio comparable to zmalloc's.

zblock is also in most cases superior to zsmalloc with regard to average performance and worst execution times, thus allowing for better response time and real-time characteristics of the whole system.

Is there a reason not to use this allocation scheme in zsmalloc then?

I'm curious what others think, but I'm still not convinced a second allocator makes sense. It's maintenance overhead, a permanent struggle to match feature parity, and it fragments development and testing base.

Not long ago several slab allocators were removed for those reasons. Likewise, we just deleted zbud and z3fold because they didn't get any attention and bitrotted, but not before years of inflicting pain through the zpool interface, users accidentally making very suboptimal choices, reporting the same bugs over and over again etc.

If you discovered a better allocation scheme, that's excellent. But I don't see why it warrants forking the entire allocator.

I also don't buy the fragmentation argument. Even if you are better at packing during allocation time (although, citation needed), the workload can unmap swap pages such that they leave partial blocks behind indefinitely if you don't have block compaction.

Then there is the migration support, which you said is planned, but which would presumably require the same indirection between handle and the backing pages that zsmalloc has. How much will this eat into the performance advantage?

I'd much rather you'd focus on making zsmalloc better. Improve the packing scheme, make expensive features optional/configurable etc. That would be easier on developers and users alike.

author
#3 lore raw
Quoted 5 lines · Johannes Weiner
Quoted 5 lines

zblock is also in most cases superior to zsmalloc with regard to average performance and worst execution times, thus allowing for better response time and real-time characteristics of the whole system.

Is there a reason not to use this allocation scheme in zsmalloc then?

Introducing such a scheme in zsmalloc is theoretically possible but it appears to be more complicated than implementing it from scratch, which is exactly what was done.

Quoted 9 lines

I'm curious what others think, but I'm still not convinced a second allocator makes sense. It's maintenance overhead, a permanent struggle to match feature parity, and it fragments development and testing base.

Not long ago several slab allocators were removed for those reasons. Likewise, we just deleted zbud and z3fold because they didn't get any attention and bitrotted, but not before years of inflicting pain through the zpool interface, users accidentally making very suboptimal choices, reporting the same bugs over and over again etc.

I'm not sure what pain you are talking about. There were reasons why z3fold and zbud existed. z3fold and zbud were the ones that supported page reclaim, zsmalloc wasn't quite usable with zswap until recently. When we did z3fold it was outperforming zsmalloc.

With that said, I didn't object to removing z3fold because I did understand that it made no sense to keep it at that point.

Quoted 4 lines

I also don't buy the fragmentation argument. Even if you are better at packing during allocation time (although, citation needed), the workload can unmap swap pages such that they leave partial blocks behind indefinitely if you don't have block compaction.

We published Zswap/Zswapped values for zblock/zsmalloc after stress loads and those were on par, basically.

Quoted 4 lines

Then there is the migration support, which you said is planned, but which would presumably require the same indirection between handle and the backing pages that zsmalloc has. How much will this eat into the performance advantage?

I don't think that will be necessary. We're working on supporting GFP_MOVABLE and minimising high order allocations

Quoted 3 lines

I'd much rather you'd focus on making zsmalloc better. Improve the packing scheme, make expensive features optional/configurable etc. That would be easier on developers and users alike.

zblock's source code is almost 5x smaller in size than zsmalloc's and yet zblock works better in many cases with just a few bottlenecks. Why would you mind that we'd focus on making zblock better instead and possibly retire zsmalloc when that mission is accomplished, just like we retired z3fold a while ago?

Best regards,

Vitaly

#4 lore raw
Quoted 7 lines · Vitaly
Quoted 3 lines · Johannes Weiner
Quoted 3 lines

zblock is also in most cases superior to zsmalloc with regard to average performance and worst execution times, thus allowing for better response time and real-time characteristics of the whole system.

Quoted 5 lines
Quoted 1 line
Quoted 1 line

Is there a reason not to use this allocation scheme in zsmalloc then?

Introducing such a scheme in zsmalloc is theoretically possible but it appears to be more complicated than implementing it from scratch, which is exactly what was done.

Sure, but having two options adds sizable complexity as well.

Quoted 11 lines
Quoted 9 lines

I'm curious what others think, but I'm still not convinced a second allocator makes sense. It's maintenance overhead, a permanent struggle to match feature parity, and it fragments development and testing base.

Not long ago several slab allocators were removed for those reasons. Likewise, we just deleted zbud and z3fold because they didn't get any attention and bitrotted, but not before years of inflicting pain through the zpool interface, users accidentally making very suboptimal choices, reporting the same bugs over and over again etc.

I'm not sure what pain you are talking about.

I list them in the paragraph you're replying to, and I've previously outlined the burden on developers, maintainers, and admins to support multiple implementations of the same functionality.

There is a real cost to doing this that you seem to dismiss.

Quoted 4 lines

There were reasons why z3fold and zbud existed. z3fold and zbud were the ones that supported page reclaim, zsmalloc wasn't quite usable with zswap until recently. When we did z3fold it was outperforming zsmalloc.

We see a higher compression ratio than 3 on a wide variety of workloads, so I'm a little doubtful z3fold ever outperformed zsmalloc in general-purpose environments.

When Meta started using zswap, certainly zsmalloc was the only real contender. zbud's storage density was almost not worth the cost of compression. z3fold was also not dense enough, and we ran into stability issues and crashes. The page reclaim/writeback implementation was not very useful either - take a look at the history of changes from Nhat and Domenico. These weren't just issues specific to our usecases, but much more blatant "how was this ever supposed to work?" problems.

There is nothing wrong with the evolution from zbud to more sophisticated allocators. But there is a pretty mature and feature-rich allocator now, and that sets a floor on what new allocators need to support to be considered general-purpose.

Quoted 2 lines

With that said, I didn't object to removing z3fold because I did understand that it made no sense to keep it at that point.

But you're proposing to do the same thing again, when multiple people just got done phasing out and cleaning up your previous experiments.

Quoted 7 lines
Quoted 4 lines

I also don't buy the fragmentation argument. Even if you are better at packing during allocation time (although, citation needed), the workload can unmap swap pages such that they leave partial blocks behind indefinitely if you don't have block compaction.

We published Zswap/Zswapped values for zblock/zsmalloc after stress loads and those were on par, basically.

Those are accounted in zswap, so unfortunately don't capture backend fragmentation. You'd need to implement some of the debugging features and memory counters that zsmalloc has in order to compare them.

Quoted 5 lines
Quoted 4 lines

Then there is the migration support, which you said is planned, but which would presumably require the same indirection between handle and the backing pages that zsmalloc has. How much will this eat into the performance advantage?

Quoted 8 lines

I don't think that will be necessary. We're working on supporting GFP_MOVABLE and minimising high order allocations

Quoted 3 lines

I'd much rather you'd focus on making zsmalloc better. Improve the packing scheme, make expensive features optional/configurable etc. That would be easier on developers and users alike.

zblock's source code is almost 5x smaller in size than zsmalloc's

It's an apple-to-oranges comparison.

zsmalloc has memory pressure handling and a rich debugging infrastructure that was added over time based on what people thought necessary and useful from production experience.

Implement the same functionality in zblock and we can compare lines and performance.

Quoted 4 lines

and yet zblock works better in many cases with just a few bottlenecks. Why would you mind that we'd focus on making zblock better instead and possibly retire zsmalloc when that mission is accomplished, just like we retired z3fold a while ago?

You're proposing a significant, open-ended maintenance burden for everybody else. I'm just asking for some justification stronger than "the small subset of the backend allocator that we implemented is slightly faster in a limited number of benchmarks."

The fact that zstd - a very commonly used compressor - immediately surfaced bugs that made it *much slower* is not reassuring.

The freelist issues that came up suggest strongly that you haven't looked too closely at zsmalloc and actually tried to find out why it does things the way it does. Which in turn suggests to me that this is not going to be the only cornercase lesson that zblock will go through for things that have been addressed in zsmalloc a long time ago.

All new research is expected to address prior work in its space. A new allocator should at least come with some analysis of where exactly the current allocator is flawed and why fixing that would likely amount to an entire rewrite anyway. Or be a more realistic drop-in replacement for the existing allocator already.

Making incremental improvements is the default. Forking needs a very good reason.

#5 lore raw
Quoted 30 lines · Johannes Weiner
Quoted 7 lines · Vitaly
Quoted 3 lines · Johannes Weiner
Quoted 3 lines

zblock is also in most cases superior to zsmalloc with regard to average performance and worst execution times, thus allowing for better response time and real-time characteristics of the whole system.

Quoted 5 lines
Quoted 1 line
Quoted 1 line

Is there a reason not to use this allocation scheme in zsmalloc then?

Introducing such a scheme in zsmalloc is theoretically possible but it appears to be more complicated than implementing it from scratch, which is exactly what was done.

Sure, but having two options adds sizable complexity as well.

Quoted 10 lines
Quoted 9 lines

I'm curious what others think, but I'm still not convinced a second allocator makes sense. It's maintenance overhead, a permanent struggle to match feature parity, and it fragments development and testing base.

Not long ago several slab allocators were removed for those reasons. Likewise, we just deleted zbud and z3fold because they didn't get any attention and bitrotted, but not before years of inflicting pain through the zpool interface, users accidentally making very suboptimal choices, reporting the same bugs over and over again etc.

Quoted 7 lines
Quoted 1 line

I'm not sure what pain you are talking about.

I list them in the paragraph you're replying to, and I've previously outlined the burden on developers, maintainers, and admins to support multiple implementations of the same functionality.

There is a real cost to doing this that you seem to dismiss.

I don’t dismiss the cost of maintenance, I just don’t buy this “years of pain” point because it’s very subjective. There are many people who still think that using Linux is a pain. OTOH back in 2016-2017 mobile devices using z3fold had better response metrics than those using zsmalloc.

Quoted 9 lines
Quoted 5 lines

There were reasons why z3fold and zbud existed. z3fold and zbud were the ones that supported page reclaim, zsmalloc wasn't quite usable with zswap until recently. When we did z3fold it was outperforming zsmalloc.

We see a higher compression ratio than 3 on a wide variety of workloads, so I'm a little doubtful z3fold ever outperformed zsmalloc in general-purpose environments.

z3fold never outperformed zsmalloc in terms of allocation density but before zsmalloc got a *working* compaction implementation it was on par.
Besides, compression ratios higher than 3 had been pretty rare before zstd was introduced, and if you ever tried zstd on msm8009 which was the SoC for many mobile devices back then you’d have probably understood how pointless it was back then for most of the embedded world.

And at that time z3fold was indeed faster than zsmalloc on multi-core systems, and especially on big.LITTLE.

Quoted 15 lines

When Meta started using zswap, certainly zsmalloc was the only real contender. zbud's storage density was almost not worth the cost of compression. z3fold was also not dense enough, and we ran into stability issues and crashes. The page reclaim/writeback implementation was not very useful either - take a look at the history of changes from Nhat and Domenico. These weren't just issues specific to our usecases, but much more blatant "how was this ever supposed to work?" problems.

There is nothing wrong with the evolution from zbud to more sophisticated allocators. But there is a pretty mature and feature-rich allocator now, and that sets a floor on what new allocators need to support to be considered general-purpose.

Quoted 5 lines
Quoted 2 lines

With that said, I didn't object to removing z3fold because I did understand that it made no sense to keep it at that point.

But you're proposing to do the same thing again, when multiple people just got done phasing out and cleaning up your previous experiments.

Oh well, the conversation is taking a twist. Do you mean that any new submission is “the same thing again”? Or what exactly do you mean here?

Quoted 6 lines
Quoted 6 lines
Quoted 5 lines

I also don't buy the fragmentation argument. Even if you are better at packing during allocation time (although, citation needed), the workload can unmap swap pages such that they leave partial blocks behind indefinitely if you don't have block compaction.

Quoted 6 lines
Quoted 2 lines

We published Zswap/Zswapped values for zblock/zsmalloc after stress loads and those were on par, basically.

Those are accounted in zswap, so unfortunately don't capture backend fragmentation. You'd need to implement some of the debugging features and memory counters that zsmalloc has in order to compare them.

zblock reports total_pages in an honest way, i. e. the amount of pages it allocated, no matter how full or empty these are, and the numbers clearly show there’s no substantial internal fragmentation for any real life workloads we could come up with. You can of course allocate a bunch of slots and then free every second one and not do anything else at all and then the fragmentation will be large, but this is not how zswap operates.

FWIW it is possible to make zsmalloc constantly alternate between compaction and fragmentation but this will not mimic either zram or zswap operation too.

Quoted 14 lines
Quoted 5 lines
Quoted 4 lines

Then there is the migration support, which you said is planned, but which would presumably require the same indirection between handle and the backing pages that zsmalloc has. How much will this eat into the performance advantage?

Quoted 8 lines

I don't think that will be necessary. We're working on supporting GFP_MOVABLE and minimising high order allocations

Quoted 3 lines

I'd much rather you'd focus on making zsmalloc better. Improve the packing scheme, make expensive features optional/configurable etc. That would be easier on developers and users alike.

zblock's source code is almost 5x smaller in size than zsmalloc's

Quoted 8 lines

It's an apple-to-oranges comparison.

zsmalloc has memory pressure handling and a rich debugging infrastructure that was added over time based on what people thought necessary and useful from production experience.

Implement the same functionality in zblock and we can compare lines and performance.

You assume that e.g. memory pressure handing implemented in zsmalloc is necessary for zblock, which I don’t think is the case. Debug facilitating additions are good but I _really_ _doubt_ these will bloat zblock code by 5x.

Quoted 10 lines
Quoted 5 lines

and yet zblock works better in many cases with just a few bottlenecks. Why would you mind that we'd focus on making zblock better instead and possibly retire zsmalloc when that mission is accomplished, just like we retired z3fold a while ago?

You're proposing a significant, open-ended maintenance burden for everybody else. I'm just asking for some justification stronger than "the small subset of the backend allocator that we implemented is slightly faster in a limited number of benchmarks."

Obviously we will be maintaining and extending the code, we have already discussed that.

Quoted 3 lines

The fact that zstd - a very commonly used compressor - immediately surfaced bugs that made it *much slower* is not reassuring.

I would expect that someone stating that first would first ask what the problem actually was. :)

~Vitaly

#6 lore raw
In reply to Johannes Weiner
Quoted 20 lines · Johannes Weiner
Quoted 4 lines · Vitaly Wool

zblock is a special purpose allocator for storing compressed pages. It stores integer number of same size objects per its block. These blocks consist of several physical pages (2**n, i. e. 1/2/4/8).

Quoted 8 lines

With zblock, it is possible to densely arrange objects of various sizes resulting in low internal fragmentation. Also this allocator tries to fill incomplete blocks instead of adding new ones, in many cases providing a compression ratio comparable to zmalloc's.

zblock is also in most cases superior to zsmalloc with regard to average performance and worst execution times, thus allowing for better response time and real-time characteristics of the whole system.

Is there a reason not to use this allocation scheme in zsmalloc then?

I'm curious what others think, but I'm still not convinced a second allocator makes sense. It's maintenance overhead, a permanent struggle to match feature parity, and it fragments development and testing base.

Quoted 8 lines

Not long ago several slab allocators were removed for those reasons. Likewise, we just deleted zbud and z3fold because they didn't get any attention and bitrotted, but not before years of inflicting pain through the zpool interface, users accidentally making very suboptimal choices, reporting the same bugs over and over again etc.

If you discovered a better allocation scheme, that's excellent. But I don't see why it warrants forking the entire allocator.

Just curious, I see a review on v4 happening on something that was nacked by two people in v2 [1].

Do these nack's still apply or were something clarified and they no longer apply?

[1] https://lore.kernel.org/linux-mm/CAKEwX=Ma9phmURz5nyJm0MQrWmXGFLFBPwr8-Cx=zbc473rx9A@mail.gmail.com/

Signature · 3 lines
Cheers,

David / dhildenb
#7 lore raw
Quoted 30 lines · David Hildenbrand
Quoted 13 lines · Johannes Weiner
Quoted 4 lines · Vitaly Wool

zblock is a special purpose allocator for storing compressed pages. It stores integer number of same size objects per its block. These blocks consist of several physical pages (2**n, i. e. 1/2/4/8).

Quoted 8 lines

With zblock, it is possible to densely arrange objects of various sizes resulting in low internal fragmentation. Also this allocator tries to fill incomplete blocks instead of adding new ones, in many cases providing a compression ratio comparable to zmalloc's.

zblock is also in most cases superior to zsmalloc with regard to average performance and worst execution times, thus allowing for better response time and real-time characteristics of the whole system.

Quoted 11 lines

Is there a reason not to use this allocation scheme in zsmalloc then? I'm curious what others think, but I'm still not convinced a second allocator makes sense. It's maintenance overhead, a permanent struggle to match feature parity, and it fragments development and testing base. Not long ago several slab allocators were removed for those reasons. Likewise, we just deleted zbud and z3fold because they didn't get any attention and bitrotted, but not before years of inflicting pain through the zpool interface, users accidentally making very suboptimal choices, reporting the same bugs over and over again etc. If you discovered a better allocation scheme, that's excellent. But I don't see why it warrants forking the entire allocator.

Quoted 3 lines

Just curious, I see a review on v4 happening on something that was nacked by two people in v2 [1].

Do these nack's still apply or were something clarified and they no longer apply?

The reasons for both NAKs are no longer valid (since v3).

~Vitaly

Quoted 9 lines

[1] https://lore.kernel.org/linux-mm/CAKEwX=Ma9phmURz5nyJm0MQrWmXGFLFBPwr8-Cx=zbc473rx9A@mail.gmail.com/

Signature · 3 lines
Cheers,

David / dhildenb
#8 lore raw
Quoted 30 lines · Vitaly Wool
Quoted 30 lines · David Hildenbrand
Quoted 24 lines · Johannes Weiner
Quoted 4 lines · Vitaly Wool

zblock is a special purpose allocator for storing compressed pages. It stores integer number of same size objects per its block. These blocks consist of several physical pages (2**n, i. e. 1/2/4/8).

Quoted 8 lines

With zblock, it is possible to densely arrange objects of various sizes resulting in low internal fragmentation. Also this allocator tries to fill incomplete blocks instead of adding new ones, in many cases providing a compression ratio comparable to zmalloc's.

zblock is also in most cases superior to zsmalloc with regard to average performance and worst execution times, thus allowing for better response time and real-time characteristics of the whole system.

Is there a reason not to use this allocation scheme in zsmalloc then? I'm curious what others think, but I'm still not convinced a second allocator makes sense. It's maintenance overhead, a permanent struggle to match feature parity, and it fragments development and testing base. Not long ago several slab allocators were removed for those reasons. Likewise, we just deleted zbud and z3fold because they didn't get any attention and bitrotted, but not before years of inflicting pain through the zpool interface, users accidentally making very suboptimal choices, reporting the same bugs over and over again etc. If you discovered a better allocation scheme, that's excellent. But I don't see why it warrants forking the entire allocator.

Quoted 5 lines
Quoted 3 lines

Just curious, I see a review on v4 happening on something that was nacked by two people in v2 [1].

Do these nack's still apply or were something clarified and they no longer apply?

The reasons for both NAKs are no longer valid (since v3).

Well, there I read [2]

"This is a general NAK from me on any new allocators"

So now I am confused. But maybe that's a different NAK :)

Anyhow, I was just stumbling over this after skimming v2 when it was under review, and I did not find a clue about that in the version change info under --- (e.g., NAK addressed because of Y).

Have to leave now for the long weekend, Happy Easter / Happy Holidays!

[2] https://lore.kernel.org/linux-mm/[email protected]/

Signature · 3 lines
Cheers,

David / dhildenb
#9 lore raw
In reply to Vitaly Wool
Quoted 9 lines · Vitaly Wool

zblock is a special purpose allocator for storing compressed pages. It stores integer number of same size objects per its block. These blocks consist of several physical pages (2**n, i. e. 1/2/4/8).

With zblock, it is possible to densely arrange objects of various sizes resulting in low internal fragmentation. Also this allocator tries to fill incomplete blocks instead of adding new ones, in many cases providing a compression ratio comparable to zmalloc's.

Quoted 8 lines

zblock is also in most cases superior to zsmalloc with regard to average performance and worst execution times, thus allowing for better response time and real-time characteristics of the whole system.

High memory and page migration are currently not supported by zblock.

Please CC me when sending zswap-related patches, not sure how you're picking the CC list. Also, CC'ing the zsmalloc maintainers here for the discussion of adding a new allocator vs improving zsmalloc.

I didn't look too closely but I generally agree that we should improve zsmalloc where possible rather than add a new allocator. We are trying not to repeat the zbud/z3fold or slub/slob stories here. Zsmalloc is getting a lot of mileage from both zswap and zram, and is more-or-less battle-tested. Let's work toward building upon that instead of starting over.

#10 lore raw
Quoted 6 lines · Yosry Ahmed

I didn't look too closely but I generally agree that we should improve zsmalloc where possible rather than add a new allocator. We are trying not to repeat the zbud/z3fold or slub/slob stories here. Zsmalloc is getting a lot of mileage from both zswap and zram, and is more-or-less battle-tested. Let's work toward building upon that instead of starting over.

The thing here is, zblock is using a very different approach to small object allocation. The idea is: we have an array of descriptors which correspond to multi-page blocks divided in chunks of equal size (block_size[i]). For each object of size x we find the descriptor n such as:

	block_size[n-1] < n < block_size[n]

and then we store that object in an empty slot in one of the blocks. Thus, the density is high, the search is fast (rbtree based) and there are no objects spanning over 2 pages, so no extra memcpy involved.

And with the latest zblock, we see that it has a clear advantage in performance over zsmalloc, retaining roughly the same allocation density for 4K pages and scoring better on 16K pages. E. g. on a kernel compilation:

* zsmalloc/zstd/make -j32 bzImage

	real	8m0.594s
	user	39m37.783s
	sys	8m24.262s
	Zswap:            200600 kB <-- after build completion
	Zswapped:         854072 kB <-- after build completion
	zswpin 309774
	zswpout 1538332

* zblock/zstd/make -j32 bzImage

	real	7m35.546s
	user	38m03.475s
	sys	7m47.407s
	Zswap:            250940 kB <-- after build completion
	Zswapped:         870660 kB <-- after build completion
	zswpin 248606
	zswpout 1277319

So what we see here is that zblock is definitely faster and at least not worse with regard to allocation density under heavy load. It has slightly worse _idle_ allocation density but since it will quickly catch up under load it is not really important. What is important is that its characteristics don't deteriorate over time. Overall, zblock is simple and efficient and there is /raison d'etre/ for it.

Now, it is indeed possible to partially rework zsmalloc using zblock's algorithm but this will be a rather substantial change, equal or bigger in effort to implementing the approach described above from scratch (and this is what we did), and with such drastic changes most of the testing that has been done with zsmalloc would be invalidated, and we'll be out in the wild anyway. So even though I see your point, I don't think it applies in this particular case.

~Vitaly

#11 lore raw
Quoted 8 lines · Vitaly Wool
Quoted 6 lines · Yosry Ahmed

I didn't look too closely but I generally agree that we should improve zsmalloc where possible rather than add a new allocator. We are trying not to repeat the zbud/z3fold or slub/slob stories here. Zsmalloc is getting a lot of mileage from both zswap and zram, and is more-or-less battle-tested. Let's work toward building upon that instead of starting over.

Quoted 8 lines

The thing here is, zblock is using a very different approach to small object allocation. The idea is: we have an array of descriptors which correspond to multi-page blocks divided in chunks of equal size (block_size[i]). For each object of size x we find the descriptor n such as:

	block_size[n-1] < n < block_size[n]

and then we store that object in an empty slot in one of the blocks. Thus, the density is high, the search is fast (rbtree based) and there are no objects spanning over 2 pages, so no extra memcpy involved.

The block sizes seem to be similar in principle to class sizes in zsmalloc. It seems to me that there are two apparent differentiating properties to zblock:

- Block lookup uses an rbtree, so it's faster than zsmalloc's list iteration. On the other hand, zsmalloc divides each class into fullness groups and tries to pack almost full groups first. Not sure if zblock's approach is strictly better.

- Zblock uses higher order allocations vs. zsmalloc always using order-0 allocations. I think this may be the main advantage and I remember asking if zsmalloc can support this. Always using order-0 pages is more reliable but may not always be the best choice.

On the other hand, zblock is lacking in other regards. For example:
- The lack of compaction means that certain workloads will see a lot of fragmentation. It purely depends on the access patterns. We could end up with a lot of blocks each containing a single object and there is no way to recover AFAICT.

- Zblock will fail if a high order allocation cannot be satisfied, which is more likely to happen under memory pressure, and it's usually when zblock is needed in the first place.

- There's probably more, I didn't check too closely, and I am hoping that Minchan and Sergey will chime in here.

Quoted 23 lines

And with the latest zblock, we see that it has a clear advantage in performance over zsmalloc, retaining roughly the same allocation density for 4K pages and scoring better on 16K pages. E. g. on a kernel compilation:

* zsmalloc/zstd/make -j32 bzImage

	real	8m0.594s
	user	39m37.783s
	sys	8m24.262s
	Zswap:            200600 kB <-- after build completion
	Zswapped:         854072 kB <-- after build completion
	zswpin 309774
	zswpout 1538332

* zblock/zstd/make -j32 bzImage

	real	7m35.546s
	user	38m03.475s
	sys	7m47.407s
	Zswap:            250940 kB <-- after build completion
	Zswapped:         870660 kB <-- after build completion
	zswpin 248606
	zswpout 1277319
Quoted 6 lines

So what we see here is that zblock is definitely faster and at least not worse with regard to allocation density under heavy load. It has slightly worse _idle_ allocation density but since it will quickly catch up under load it is not really important. What is important is that its characteristics don't deteriorate over time. Overall, zblock is simple and efficient and there is /raison d'etre/ for it.

Zblock is performing better for this specific workload, but as I mentioned earlier there are other aspects that zblock is missing. Zsmalloc has seen a very large range of workloads of different types, and we cannot just dismiss this.

Quoted 8 lines

Now, it is indeed possible to partially rework zsmalloc using zblock's algorithm but this will be a rather substantial change, equal or bigger in effort to implementing the approach described above from scratch (and this is what we did), and with such drastic changes most of the testing that has been done with zsmalloc would be invalidated, and we'll be out in the wild anyway. So even though I see your point, I don't think it applies in this particular case.

Well, we should start by breaking down the differences and finding out why zblock is performing better, as I mentioned above. If it's the faster lookups or higher order allocations, we can work to support that in zsmalloc. Similarly, if zsmalloc has unnecessary complexity it'd be great to get rid of it rather than starting over.

Also, we don't have to do it all at once and invalidate the testing that zsmalloc has seen. These can be incremental changes that get spread over multiple releases, getting incremental exposure in the process.

Quoted 2 lines

~Vitaly

#12 lore raw

Hi Yosry,

Quoted 18 lines · Yosry Ahmed
Quoted 8 lines · Vitaly Wool
Quoted 6 lines · Yosry Ahmed

I didn't look too closely but I generally agree that we should improve zsmalloc where possible rather than add a new allocator. We are trying not to repeat the zbud/z3fold or slub/slob stories here. Zsmalloc is getting a lot of mileage from both zswap and zram, and is more-or-less battle-tested. Let's work toward building upon that instead of starting over.

Quoted 8 lines

The thing here is, zblock is using a very different approach to small object allocation. The idea is: we have an array of descriptors which correspond to multi-page blocks divided in chunks of equal size (block_size[i]). For each object of size x we find the descriptor n such as:

	block_size[n-1] < n < block_size[n]

and then we store that object in an empty slot in one of the blocks. Thus, the density is high, the search is fast (rbtree based) and there are no objects spanning over 2 pages, so no extra memcpy involved.

Quoted 8 lines

The block sizes seem to be similar in principle to class sizes in zsmalloc. It seems to me that there are two apparent differentiating properties to zblock:

- Block lookup uses an rbtree, so it's faster than zsmalloc's list iteration. On the other hand, zsmalloc divides each class into fullness groups and tries to pack almost full groups first. Not sure if zblock's approach is strictly better.

If we free a slot in a fully packed block we put it on top of the list. zswap's normal operation pattern is that there will be more free slots in that block so it's roughly the same.

Quoted 4 lines

- Zblock uses higher order allocations vs. zsmalloc always using order-0 allocations. I think this may be the main advantage and I remember asking if zsmalloc can support this. Always using order-0 pages is more reliable but may not always be the best choice.

There's a patch we'll be posting soon with "opportunistic" high order allocations (i. e. if try_alloc_pages fails, allocate order-0 pages instead). This will leverage the benefits of higher order allocations without putting too much stress on the system.

Quoted 5 lines

On the other hand, zblock is lacking in other regards. For example:
- The lack of compaction means that certain workloads will see a lot of fragmentation. It purely depends on the access patterns. We could end up with a lot of blocks each containing a single object and there is no way to recover AFAICT.

We have been giving many variants of stress load on the memory subsystem and the worst compression ratio *after* the stress load was 2.8x using zstd as the compressor (and about 4x under load). With zsmalloc under the same conditions the ratio was 3.6x after and 4x under load.

With more normal (but still stressing) usage patterns the numbers *after* the stress load were around 3.8x and 4.1x, respectively.

Bottom line, ending up with a lot of blocks each containing a single object is not a real life scenario. With that said, we have a quite simple solution in the making that will get zblock on par with zsmalloc even in the cases described above.

Quoted 3 lines

- Zblock will fail if a high order allocation cannot be satisfied, which is more likely to happen under memory pressure, and it's usually when zblock is needed in the first place.

See above, this issue will be addressed in the patch coming in a really short while.

Quoted 33 lines

- There's probably more, I didn't check too closely, and I am hoping that Minchan and Sergey will chime in here.

Quoted 23 lines

And with the latest zblock, we see that it has a clear advantage in performance over zsmalloc, retaining roughly the same allocation density for 4K pages and scoring better on 16K pages. E. g. on a kernel compilation:

* zsmalloc/zstd/make -j32 bzImage

	real	8m0.594s
	user	39m37.783s
	sys	8m24.262s
	Zswap:            200600 kB <-- after build completion
	Zswapped:         854072 kB <-- after build completion
	zswpin 309774
	zswpout 1538332

* zblock/zstd/make -j32 bzImage

	real	7m35.546s
	user	38m03.475s
	sys	7m47.407s
	Zswap:            250940 kB <-- after build completion
	Zswapped:         870660 kB <-- after build completion
	zswpin 248606
	zswpout 1277319
Quoted 6 lines

So what we see here is that zblock is definitely faster and at least not worse with regard to allocation density under heavy load. It has slightly worse _idle_ allocation density but since it will quickly catch up under load it is not really important. What is important is that its characteristics don't deteriorate over time. Overall, zblock is simple and efficient and there is /raison d'etre/ for it.

Quoted 4 lines

Zblock is performing better for this specific workload, but as I mentioned earlier there are other aspects that zblock is missing. Zsmalloc has seen a very large range of workloads of different types, and we cannot just dismiss this.

We've been running many different work loads with both allocators but posting all the results in the patch description will go well beyond the purpose of a patch submission. If there are some workloads you are interested in in particular, please let me know, odds are high we have some results for those too.

Quoted 15 lines
Quoted 7 lines

Now, it is indeed possible to partially rework zsmalloc using zblock's algorithm but this will be a rather substantial change, equal or bigger in effort to implementing the approach described above from scratch (and this is what we did), and with such drastic changes most of the testing that has been done with zsmalloc would be invalidated, and we'll be out in the wild anyway. So even though I see your point, I don't think it applies in this particular case.

Well, we should start by breaking down the differences and finding out why zblock is performing better, as I mentioned above. If it's the faster lookups or higher order allocations, we can work to support that in zsmalloc. Similarly, if zsmalloc has unnecessary complexity it'd be great to get rid of it rather than starting over.

Quoted 3 lines

Also, we don't have to do it all at once and invalidate the testing that zsmalloc has seen. These can be incremental changes that get spread over multiple releases, getting incremental exposure in the process.

I believe we are a lot closer now to having a zblock without the initial drawbacks you have pointed out than a faster zsmalloc, retaining the code simplicity of the former.

~Vitaly

#13 lore raw

On (25/05/01 14:41), Vitaly Wool wrote:
[..]

Quoted 2 lines

Bottom line, ending up with a lot of blocks each containing a single object is not a real life scenario.

Why not? What if the data patterns do not favor compressible objects? E.g. what if the distribution of compressed objects looks like this (a real zsmalloc stats):

size-class objs-allocated
---------------------------
...

1904         3315
1920         3468
1936         3515
2048         25816
2144         22363
2160         3230
2176         3075
2192         2990
2224         5665
2272         8118
2304         5040
2336         4529
2384         6132
2400         1768
2448         4825
2512         5135
2560         2944
2592         1562
2624         1512
2720         3813
2832         3315
2864         820

...

Notice tenth of thousands of 2048-2144 bytes objects. zsmalloc size classes are fundamentally independent of each other and, hence, of the compressed objects distribution: the lack of objects, or even "the absence of" for that matter, in size-class 256 does not change a thing for size-class 2048. This is a very important property.

#14 lore raw
In reply to Vitaly Wool
Quoted 25 lines · Vitaly Wool

Hi Yosry,

Quoted 22 lines · Yosry Ahmed
Quoted 8 lines · Vitaly Wool
Quoted 6 lines · Yosry Ahmed

I didn't look too closely but I generally agree that we should improve zsmalloc where possible rather than add a new allocator. We are trying not to repeat the zbud/z3fold or slub/slob stories here. Zsmalloc is getting a lot of mileage from both zswap and zram, and is more-or-less battle-tested. Let's work toward building upon that instead of starting over.

Quoted 8 lines

The thing here is, zblock is using a very different approach to small object allocation. The idea is: we have an array of descriptors which correspond to multi-page blocks divided in chunks of equal size (block_size[i]). For each object of size x we find the descriptor n such as:

	block_size[n-1] < n < block_size[n]

and then we store that object in an empty slot in one of the blocks. Thus, the density is high, the search is fast (rbtree based) and there are no objects spanning over 2 pages, so no extra memcpy involved.

The block sizes seem to be similar in principle to class sizes in zsmalloc. It seems to me that there are two apparent differentiating properties to zblock:

Quoted 8 lines
Quoted 4 lines

- Block lookup uses an rbtree, so it's faster than zsmalloc's list iteration. On the other hand, zsmalloc divides each class into fullness groups and tries to pack almost full groups first. Not sure if zblock's approach is strictly better.

If we free a slot in a fully packed block we put it on top of the list. zswap's normal operation pattern is that there will be more free slots in that block so it's roughly the same.

How so? IIUC the order in which slots are freed depends on the LRU (for writeback) and swapins (for loads). Why do we expect that slots from the same block will be freed in close succession?

Quoted 22 lines
Quoted 5 lines

- Zblock uses higher order allocations vs. zsmalloc always using order-0 allocations. I think this may be the main advantage and I remember asking if zsmalloc can support this. Always using order-0 pages is more reliable but may not always be the best choice.

There's a patch we'll be posting soon with "opportunistic" high order allocations (i. e. if try_alloc_pages fails, allocate order-0 pages instead). This will leverage the benefits of higher order allocations without putting too much stress on the system.

Quoted 5 lines

On the other hand, zblock is lacking in other regards. For example:
- The lack of compaction means that certain workloads will see a lot of fragmentation. It purely depends on the access patterns. We could end up with a lot of blocks each containing a single object and there is no way to recover AFAICT.

We have been giving many variants of stress load on the memory subsystem and the worst compression ratio *after* the stress load was 2.8x using zstd as the compressor (and about 4x under load). With zsmalloc under the same conditions the ratio was 3.6x after and 4x under load.

Quoted 7 lines

With more normal (but still stressing) usage patterns the numbers *after* the stress load were around 3.8x and 4.1x, respectively.

Bottom line, ending up with a lot of blocks each containing a single object is not a real life scenario. With that said, we have a quite simple solution in the making that will get zblock on par with zsmalloc even in the cases described above.

Could you share a high-level description of how this issue will be addressed in zblock? I am trying to understand why/how zblock can handle this better/simpler than zsmalloc.

Quoted 46 lines
Quoted 4 lines

- Zblock will fail if a high order allocation cannot be satisfied, which is more likely to happen under memory pressure, and it's usually when zblock is needed in the first place.

See above, this issue will be addressed in the patch coming in a really short while.

Quoted 33 lines

- There's probably more, I didn't check too closely, and I am hoping that Minchan and Sergey will chime in here.

Quoted 23 lines

And with the latest zblock, we see that it has a clear advantage in performance over zsmalloc, retaining roughly the same allocation density for 4K pages and scoring better on 16K pages. E. g. on a kernel compilation:

* zsmalloc/zstd/make -j32 bzImage

	real	8m0.594s
	user	39m37.783s
	sys	8m24.262s
	Zswap:            200600 kB <-- after build completion
	Zswapped:         854072 kB <-- after build completion
	zswpin 309774
	zswpout 1538332

* zblock/zstd/make -j32 bzImage

	real	7m35.546s
	user	38m03.475s
	sys	7m47.407s
	Zswap:            250940 kB <-- after build completion
	Zswapped:         870660 kB <-- after build completion
	zswpin 248606
	zswpout 1277319
Quoted 6 lines

So what we see here is that zblock is definitely faster and at least not worse with regard to allocation density under heavy load. It has slightly worse _idle_ allocation density but since it will quickly catch up under load it is not really important. What is important is that its characteristics don't deteriorate over time. Overall, zblock is simple and efficient and there is /raison d'etre/ for it.

Quoted 4 lines

Zblock is performing better for this specific workload, but as I mentioned earlier there are other aspects that zblock is missing. Zsmalloc has seen a very large range of workloads of different types, and we cannot just dismiss this.

Quoted 5 lines

We've been running many different work loads with both allocators but posting all the results in the patch description will go well beyond the purpose of a patch submission. If there are some workloads you are interested in in particular, please let me know, odds are high we have some results for those too.

That's good to know. I don't have specific workloads in mind, was just stating the fact that zsmalloc has been tested with a variety of workloads in production environments.

Quoted 16 lines
Quoted 16 lines
Quoted 8 lines

Now, it is indeed possible to partially rework zsmalloc using zblock's algorithm but this will be a rather substantial change, equal or bigger in effort to implementing the approach described above from scratch (and this is what we did), and with such drastic changes most of the testing that has been done with zsmalloc would be invalidated, and we'll be out in the wild anyway. So even though I see your point, I don't think it applies in this particular case.

Well, we should start by breaking down the differences and finding out why zblock is performing better, as I mentioned above. If it's the faster lookups or higher order allocations, we can work to support that in zsmalloc. Similarly, if zsmalloc has unnecessary complexity it'd be great to get rid of it rather than starting over.

Quoted 7 lines
Quoted 3 lines

Also, we don't have to do it all at once and invalidate the testing that zsmalloc has seen. These can be incremental changes that get spread over multiple releases, getting incremental exposure in the process.

I believe we are a lot closer now to having a zblock without the initial drawbacks you have pointed out than a faster zsmalloc, retaining the code simplicity of the former.

This does not answer my question tho. I am trying to understand what makes zblock faster than zsmalloc.

If it's the (optionally opportunistic) higher order allocations, we can probably do the same in zsmalloc and avoid memcpys as well. I guess we can try to allocate zspages as a high-order contiguous allocation first, and fallback to the current "chain" structure on failure.

If it's the lack of overhead from compaction (e.g. less locking), then the question should be why does zsmalloc need compaction and zblock allegedly doesn't?

I am not arguing that you're seeing better results from zblock, what I am saying is let's pinpoint what makes zblock better first, then decide whether the same can be applied to zsmalloc or if it's really better to create a new allocator instead. Right now we don't have full information.

WDYT?

#15 lore raw
Quoted 157 lines · Yosry Ahmed
Quoted 25 lines · Vitaly Wool

Hi Yosry,

Quoted 22 lines · Yosry Ahmed
Quoted 8 lines · Vitaly Wool
Quoted 6 lines · Yosry Ahmed

I didn't look too closely but I generally agree that we should improve zsmalloc where possible rather than add a new allocator. We are trying not to repeat the zbud/z3fold or slub/slob stories here. Zsmalloc is getting a lot of mileage from both zswap and zram, and is more-or-less battle-tested. Let's work toward building upon that instead of starting over.

Quoted 8 lines

The thing here is, zblock is using a very different approach to small object allocation. The idea is: we have an array of descriptors which correspond to multi-page blocks divided in chunks of equal size (block_size[i]). For each object of size x we find the descriptor n such as:
block_size[n-1] < n < block_size[n]
and then we store that object in an empty slot in one of the blocks. Thus, the density is high, the search is fast (rbtree based) and there are no objects spanning over 2 pages, so no extra memcpy involved.

The block sizes seem to be similar in principle to class sizes in zsmalloc. It seems to me that there are two apparent differentiating properties to zblock:

Quoted 8 lines
Quoted 4 lines

- Block lookup uses an rbtree, so it's faster than zsmalloc's list iteration. On the other hand, zsmalloc divides each class into fullness groups and tries to pack almost full groups first. Not sure if zblock's approach is strictly better.

If we free a slot in a fully packed block we put it on top of the list. zswap's normal operation pattern is that there will be more free slots in that block so it's roughly the same.

How so? IIUC the order in which slots are freed depends on the LRU (for writeback) and swapins (for loads). Why do we expect that slots from the same block will be freed in close succession?

Quoted 22 lines
Quoted 5 lines

- Zblock uses higher order allocations vs. zsmalloc always using order-0 allocations. I think this may be the main advantage and I remember asking if zsmalloc can support this. Always using order-0 pages is more reliable but may not always be the best choice.

There's a patch we'll be posting soon with "opportunistic" high order allocations (i. e. if try_alloc_pages fails, allocate order-0 pages instead). This will leverage the benefits of higher order allocations without putting too much stress on the system.

Quoted 5 lines

On the other hand, zblock is lacking in other regards. For example:
- The lack of compaction means that certain workloads will see a lot of fragmentation. It purely depends on the access patterns. We could end up with a lot of blocks each containing a single object and there is no way to recover AFAICT.

We have been giving many variants of stress load on the memory subsystem and the worst compression ratio *after* the stress load was 2.8x using zstd as the compressor (and about 4x under load). With zsmalloc under the same conditions the ratio was 3.6x after and 4x under load.

Quoted 7 lines

With more normal (but still stressing) usage patterns the numbers *after* the stress load were around 3.8x and 4.1x, respectively.

Bottom line, ending up with a lot of blocks each containing a single object is not a real life scenario. With that said, we have a quite simple solution in the making that will get zblock on par with zsmalloc even in the cases described above.

Could you share a high-level description of how this issue will be addressed in zblock? I am trying to understand why/how zblock can handle this better/simpler than zsmalloc.

Quoted 46 lines
Quoted 4 lines

- Zblock will fail if a high order allocation cannot be satisfied, which is more likely to happen under memory pressure, and it's usually when zblock is needed in the first place.

See above, this issue will be addressed in the patch coming in a really short while.

Quoted 33 lines

- There's probably more, I didn't check too closely, and I am hoping that Minchan and Sergey will chime in here.

Quoted 23 lines

And with the latest zblock, we see that it has a clear advantage in performance over zsmalloc, retaining roughly the same allocation density for 4K pages and scoring better on 16K pages. E. g. on a kernel compilation:

* zsmalloc/zstd/make -j32 bzImage
real 8m0.594s
user 39m37.783s
sys 8m24.262s

Zswap:            200600 kB <-- after build completion
Zswapped:         854072 kB <-- after build completion

zswpin 309774
zswpout 1538332

* zblock/zstd/make -j32 bzImage
real 7m35.546s
user 38m03.475s
sys 7m47.407s

Zswap:            250940 kB <-- after build completion
Zswapped:         870660 kB <-- after build completion

zswpin 248606
zswpout 1277319

Quoted 6 lines

So what we see here is that zblock is definitely faster and at least not worse with regard to allocation density under heavy load. It has slightly worse _idle_ allocation density but since it will quickly catch up under load it is not really important. What is important is that its characteristics don't deteriorate over time. Overall, zblock is simple and efficient and there is /raison d'etre/ for it.

Quoted 4 lines

Zblock is performing better for this specific workload, but as I mentioned earlier there are other aspects that zblock is missing. Zsmalloc has seen a very large range of workloads of different types, and we cannot just dismiss this.

Quoted 5 lines

We've been running many different work loads with both allocators but posting all the results in the patch description will go well beyond the purpose of a patch submission. If there are some workloads you are interested in in particular, please let me know, odds are high we have some results for those too.

That's good to know. I don't have specific workloads in mind, was just stating the fact that zsmalloc has been tested with a variety of workloads in production environments.

Quoted 16 lines
Quoted 16 lines
Quoted 8 lines

Now, it is indeed possible to partially rework zsmalloc using zblock's algorithm but this will be a rather substantial change, equal or bigger in effort to implementing the approach described above from scratch (and this is what we did), and with such drastic changes most of the testing that has been done with zsmalloc would be invalidated, and we'll be out in the wild anyway. So even though I see your point, I don't think it applies in this particular case.

Well, we should start by breaking down the differences and finding out why zblock is performing better, as I mentioned above. If it's the faster lookups or higher order allocations, we can work to support that in zsmalloc. Similarly, if zsmalloc has unnecessary complexity it'd be great to get rid of it rather than starting over.

Quoted 7 lines
Quoted 3 lines

Also, we don't have to do it all at once and invalidate the testing that zsmalloc has seen. These can be incremental changes that get spread over multiple releases, getting incremental exposure in the process.

I believe we are a lot closer now to having a zblock without the initial drawbacks you have pointed out than a faster zsmalloc, retaining the code simplicity of the former.

Quoted 7 lines

This does not answer my question tho. I am trying to understand what makes zblock faster than zsmalloc.

If it's the (optionally opportunistic) higher order allocations, we can probably do the same in zsmalloc and avoid memcpys as well. I guess we can try to allocate zspages as a high-order contiguous allocation first, and fallback to the current "chain" structure on failure.

We got rid of high order allocations and see better results now. vmalloc works well for our purpose.

Quoted 7 lines

I am not arguing that you're seeing better results from zblock, what I am saying is let's pinpoint what makes zblock better first, then decide whether the same can be applied to zsmalloc or if it's really better to create a new allocator instead. Right now we don't have full information.

WDYT?

I would suggest that we focused on what zblock could deliver what zsmalloc doesn’t seem to be able to. E. g. it’s easy to add block descriptors at runtime basing on the statistics from the existing allocations, and it will lead to even higher compression density. Since zsmalloc only operates on 2^n sized objects, it doesn’t have that flexibility and it’s not possible to add it without making it essentially a zblock. I believe this is also why zsmalloc is losing to zblock in compression density on 16K pages already.

~Vitaly

#16 lore raw
In reply to Vitaly Wool

On (25/04/23 21:53), Vitaly Wool wrote:
[..]

Quoted 9 lines

* zsmalloc/zstd/make -j32 bzImage

	real	8m0.594s
	user	39m37.783s
	sys	8m24.262s
	Zswap:            200600 kB <-- after build completion
	Zswapped:         854072 kB <-- after build completion
	zswpin 309774
	zswpout 1538332
Quoted 8 lines

* zblock/zstd/make -j32 bzImage

	real	7m35.546s
	user	38m03.475s
	sys	7m47.407s
	Zswap:            250940 kB <-- after build completion
	Zswapped:         870660 kB <-- after build completion
	zswpin 248606
	zswpout 1277319

I'm sorry but what does this test test? That under memory pressure the kernel swaps out different pages with different compression characteristics?

author
#17 lore raw
Quoted 21 lines · Sergey Senozhatsky

On (25/04/23 21:53), Vitaly Wool wrote:
[..]

Quoted 9 lines

* zsmalloc/zstd/make -j32 bzImage
real 8m0.594s
user 39m37.783s
sys 8m24.262s
Zswap: 200600 kB <-- after build completion
Zswapped: 854072 kB <-- after build completion
zswpin 309774
zswpout 1538332

Quoted 9 lines

* zblock/zstd/make -j32 bzImage
real 7m35.546s
user 38m03.475s
sys 7m47.407s
Zswap: 250940 kB <-- after build completion
Zswapped: 870660 kB <-- after build completion
zswpin 248606
zswpout 1277319

Quoted 3 lines

I'm sorry but what does this test test? That under memory pressure the kernel swaps out different pages with different compression characteristics? This test illustrates that zblock is faster than zsmalloc. No rocket science here.

~Vitaly

maintainer
#18 lore raw
In reply to Vitaly Wool
Quoted 9 lines · On Sat, 12 Apr 2025 17:42:07 +0200 Vitaly Wool <[email protected]> wrote:

zblock is a special purpose allocator for storing compressed pages. It stores integer number of same size objects per its block. These blocks consist of several physical pages (2**n, i. e. 1/2/4/8).

With zblock, it is possible to densely arrange objects of various sizes resulting in low internal fragmentation. Also this allocator tries to fill incomplete blocks instead of adding new ones, in many cases providing a compression ratio comparable to zmalloc's.

Quoted 6 lines

zblock is also in most cases superior to zsmalloc with regard to average performance and worst execution times, thus allowing for better response time and real-time characteristics of the whole system.

High memory and page migration are currently not supported by zblock.

My x86_64 allmodconfig build failed.

MODPOST Module.symvers
ERROR: modpost: "try_alloc_pages_noprof" [mm/zblock.ko] undefined!

I don't understand why this wasn't encountered earlier.

From: Andrew Morton <[email protected]>
Subject: mm-add-zblock-allocator-fix-2
Date: Sun May 4 02:13:54 AM PDT 2025

export try_alloc_pages_noprof() to modules for CONFIG_ZBLOCK=m

Cc Vitaly Wool <[email protected]>
Cc Igor Belousov <[email protected]>
Cc Johannes Weiner <[email protected]>
Cc Nhat Pham <[email protected]>
Cc Shakeel Butt <[email protected]>
Cc Yosry Ahmed <[email protected]>
Cc David Hildenbrand <[email protected]>

---

 mm/page_alloc.c |    1 +
 1 file changed, 1 insertion(+)
mm/page_alloc.c renamed from mm/page_alloc.c~a +1 -0
@@ -7470,3 +7470,4 @@ struct page *try_alloc_pages_noprof(int
 	kmsan_alloc_page(page, order, alloc_gfp);
 	return page;
 }
+EXPORT_SYMBOL_GPL(try_alloc_pages_noprof);

_

maintainer Tested-by Igor Belousov on v4 1/1
#19 lore raw
In reply to Vitaly Wool
Quoted 9 lines · On Sat, 12 Apr 2025 17:42:07 +0200 Vitaly Wool <[email protected]> wrote:

zblock is a special purpose allocator for storing compressed pages. It stores integer number of same size objects per its block. These blocks consist of several physical pages (2**n, i. e. 1/2/4/8).

With zblock, it is possible to densely arrange objects of various sizes resulting in low internal fragmentation. Also this allocator tries to fill incomplete blocks instead of adding new ones, in many cases providing a compression ratio comparable to zmalloc's.

Quoted 5 lines

zblock is also in most cases superior to zsmalloc with regard to average performance and worst execution times, thus allowing for better response time and real-time characteristics of the whole system.

High memory and page migration are currently not supported by zblock.

Well dang, I was about to drop zblock as this version (and its nine -fix patches) have been in mm.git for three months without reported issues and review is very stuck.

But I now realize that it has been in mm.git's mm-new branch for all that time and has had no -next exposure. Sorry about that.

So after the 6.17 release I'll move the patch into mm-unstable (and hence linux-next) to get you some linux-next exposure. After some time there I expect I will drop the patch.

If you wish to persist with this I suggest you update the changelog to address all review comments which have been received thus far and resend.

Current version:

From: Vitaly Wool <[email protected]>
Subject: mm: add zblock allocator
Date: Sat, 12 Apr 2025 17:42:07 +0200

zblock is a special purpose allocator for storing compressed pages. It stores integer number of same size objects per its block. These blocks consist of several physical pages (2**n, i. e. 1/2/4/8).

With zblock, it is possible to densely arrange objects of various sizes resulting in low internal fragmentation. Also this allocator tries to fill incomplete blocks instead of adding new ones, in many cases providing a compression ratio comparable to zmalloc's.

zblock is also in most cases superior to zsmalloc with regard to average performance and worst execution times, thus allowing for better response time and real-time characteristics of the whole system.

High memory and page migration are currently not supported by zblock.

Test results (zstd compressor, 8 core Ryzen 9 VM, make bzImage):
- zblock:

    real	6m52.621s
    user	33m41.771s
    sys 	6m28.825s
    Zswap:            162328 kB
    Zswapped:         754468 kB
    zswpin 93851
    zswpout 542481
    zswpwb 935
- zsmalloc:
    real	7m4.355s
    user	34m37.538s
    sys 	6m22.086s
    zswpin 101243
    zswpout 448217
    zswpwb 640
    Zswap:            175704 kB
    Zswapped:         778692 kB
[[email protected]: fix build]
[[email protected]: export try_alloc_pages_noprof() to modules for CONFIG_ZBLOCK=m]
[[email protected]: try_alloc_pages() was renamed]
[[email protected]: fix kerneldoc for zblock_create_pool()]
[[email protected]: add debugfs]
[[email protected]: avoid failing the build]
[[email protected]: s/#warn/#warning/]
[[email protected]: use vmalloc for page allocations]
[[email protected]: make active_list rcu_list]
Cc Johannes Weiner <[email protected]>
Cc Nhat Pham <[email protected]>
Cc Shakeel Butt <[email protected]>
Cc Yosry Ahmed <[email protected]>
Cc David Hildenbrand <[email protected]>

---

 Documentation/mm/zblock.rst |   24 +
 MAINTAINERS                 |    7 
 mm/Kconfig                  |   12 
 mm/Makefile                 |    1 
 mm/page_alloc.c             |    1 
 mm/zblock.c                 |  473 ++++++++++++++++++++++++++++++++++
 mm/zblock.h                 |  205 ++++++++++++++
 7 files changed, 723 insertions(+)
Documentation/mm/zblock.rst new file +24 -0
new file mode 100644
@@ -0,0 +1,24 @@
+.. SPDX-License-Identifier: GPL-2.0
+
+======
+zblock
+======
+
+zblock is a special purpose allocator for storing compressed pages.
+It stores integer number of compressed objects per its block. These
+blocks consist of several physical pages (2**n, i. e. 1/2/4/8).
+
+With zblock, it is possible to densely arrange objects of various sizes
+resulting in low internal fragmentation. Also this allocator tries to
+fill incomplete blocks instead of adding new ones,  in many cases
+providing a compression ratio substantially higher than z3fold and zbud
+(though lower than zmalloc's).
+
+zblock does not require MMU to operate and also is superior to zsmalloc
+with regard to average performance and worst execution times, thus
+allowing for better response time and real-time characteristics of the
+whole system.
+
+E. g. on a series of stress-ng tests run on a Raspberry Pi 5, we get
+5-10% higher value for bogo ops/s in zblock/zsmalloc comparison.
+
MAINTAINERS renamed from MAINTAINERS~mm-add-zblock-allocator +7 -0
@@ -27383,6 +27383,13 @@ F:	Documentation/networking/device_drive
 F:	drivers/net/hamradio/*scc.c
 F:	drivers/net/hamradio/z8530.h
 
+ZBLOCK COMPRESSED SLAB MEMORY ALLOCATOR
+M:	Vitaly Wool <[email protected]>
+L:	[email protected]
+S:	Maintained
+F:	Documentation/mm/zblock.rst
+F:	mm/zblock.[ch]
+
 ZD1211RW WIRELESS DRIVER
 L:	[email protected]
 S:	Orphan
mm/Kconfig renamed from mm/Kconfig~mm-add-zblock-allocator +12 -0
@@ -152,6 +152,18 @@ config ZSWAP_ZPOOL_DEFAULT
        default "zsmalloc" if ZSWAP_ZPOOL_DEFAULT_ZSMALLOC
        default ""
 
+config ZBLOCK
+	tristate "Fast compression allocator with high density"
+	depends on ZPOOL
+	help
+	  A special purpose allocator for storing compressed pages.
+	  It stores integer number of same size compressed objects per
+	  its block. These blocks consist of several physical pages
+	  (2**n, i. e. 1/2/4/8).
+
+	  With zblock, it is possible to densely arrange objects of
+	  various sizes resulting in low internal fragmentation.
+
 config ZSMALLOC
 	tristate
 	prompt "N:1 compression allocator (zsmalloc)" if (ZSWAP || ZRAM)
mm/Makefile renamed from mm/Makefile~mm-add-zblock-allocator +1 -0
@@ -116,6 +116,7 @@ obj-$(CONFIG_DEBUG_VM_PGTABLE) += debug_
 obj-$(CONFIG_PAGE_OWNER) += page_owner.o
 obj-$(CONFIG_MEMORY_ISOLATION) += page_isolation.o
 obj-$(CONFIG_ZPOOL)	+= zpool.o
+obj-$(CONFIG_ZBLOCK)	+= zblock.o
 obj-$(CONFIG_ZSMALLOC)	+= zsmalloc.o
 obj-$(CONFIG_GENERIC_EARLY_IOREMAP) += early_ioremap.o
 obj-$(CONFIG_CMA)	+= cma.o
mm/page_alloc.c renamed from mm/page_alloc.c~mm-add-zblock-allocator +1 -0
@@ -7593,3 +7593,4 @@ struct page *alloc_pages_nolock_noprof(i
 	kmsan_alloc_page(page, order, alloc_gfp);
 	return page;
 }
+EXPORT_SYMBOL_GPL(alloc_pages_nolock_noprof);
mm/zblock.c new file +473 -0

473 lines not on this page · raw message

mm/zblock.h new file +205 -0

205 lines not on this page · raw message

_