Switch zswap to object read/write APIs
Subsystems (8)
cover Switch zswap to object read/write APIs
This patch series updates zswap to use the new object read/write APIs defined by zsmalloc in [1], and remove the old object mapping APIs and the related code from zpool and zsmalloc.
This depends on the zsmalloc/zram series introducing the APIs [1] and the series removing zbud and z3fold [2].
[1]https://lore.kernel.org/lkml/[email protected]/ [2]https://lore.kernel.org/lkml/[email protected]/
Yosry Ahmed (5):
mm: zpool: Add interfaces for object read/write APIs
mm: zswap: Use object read/write APIs instead of object mapping APIs
mm: zpool: Remove object mapping APIs
mm: zsmalloc: Remove object mapping APIs and per-CPU map areas
mm: zpool: Remove zpool_malloc_support_movable()
include/linux/cpuhotplug.h | 1 - include/linux/zpool.h | 42 ++---- include/linux/zsmalloc.h | 21 --- mm/zpool.c | 93 +++++-------- mm/zsmalloc.c | 263 +++---------------------------------- mm/zswap.c | 37 ++---- 6 files changed, 75 insertions(+), 382 deletions(-)
Signature · 1 line
2.48.1.711.g2feabab25a-goog
PATCH 1/5 mm: zpool: Add interfaces for object read/write APIs
Zsmalloc introduce new APIs to read/write objects besides mapping them. Add the necessary zpool interfaces.
--- include/linux/zpool.h | 17 +++++++++++++++ mm/zpool.c | 48 +++++++++++++++++++++++++++++++++++++++++++ mm/zsmalloc.c | 21 +++++++++++++++++++ 3 files changed, 86 insertions(+)
include/linux/zpool.h +17 -0
@@ -52,6 +52,16 @@ void *zpool_map_handle(struct zpool *pool, unsigned long handle,void zpool_unmap_handle (struct zpool *pool,unsigned long handle); + +void *zpool_obj_read_begin (struct zpool *zpool,unsigned long handle, +void *local_copy); + +void zpool_obj_read_end (struct zpool *zpool,unsigned long handle, +void *handle_mem); + +void zpool_obj_write (struct zpool *zpool,unsigned long handle, +void *handle_mem,size_t mem_len); +u64 zpool_get_total_pages (struct zpool *pool);
@@ -90,6 +100,13 @@ struct zpool_driver { enum zpool_mapmode mm);void (*unmap)(void *pool,unsigned long handle); +void *(*obj_read_begin)(void *pool,unsigned long handle, +void *local_copy); +void (*obj_read_end)(void *pool,unsigned long handle, +void *handle_mem); +void (*obj_write)(void *pool,unsigned long handle, +void *handle_mem,size_t mem_len); +u64 (*total_pages)(void *pool); };
mm/zpool.c +48 -0
54 lines not on this page · raw message
mm/zsmalloc.c +21 -0
@@ -507,6 +507,24 @@ static void zs_zpool_unmap(void *pool, unsigned long handle)zs_unmap_object (pool, handle); } +static void *zs_zpool_obj_read_begin (void *pool,unsigned long handle, +void *local_copy) +{ +return zs_obj_read_begin (pool, handle, local_copy); +} + +static void zs_zpool_obj_read_end (void *pool,unsigned long handle, +void *handle_mem) +{ +zs_obj_read_end (pool, handle, handle_mem); +} + +static void zs_zpool_obj_write (void *pool,unsigned long handle, +void *handle_mem,size_t mem_len) +{ +zs_obj_write (pool, handle, handle_mem, mem_len); +} +static u64 zs_zpool_total_pages (void *pool) {return zs_get_total_pages (pool);
@@ -522,6 +540,9 @@ static struct zpool_driver zs_zpool_driver = { .free = zs_zpool_free, .map = zs_zpool_map, .unmap = zs_zpool_unmap, + .obj_read_begin = zs_zpool_obj_read_begin, + .obj_read_end = zs_zpool_obj_read_end, + .obj_write = zs_zpool_obj_write, .total_pages = zs_zpool_total_pages, };
Signature · 1 line
2.48.1.711.g2feabab25a-goog
Re: [PATCH mm-unstable 1/5] mm: zpool: Add interfaces for object read/write APIs
Quoted 4 lines · Yosry Ahmed
Zsmalloc introduce new APIs to read/write objects besides mapping them. Add the necessary zpool interfaces.
Signed-off-by Yosry Ahmed <[email protected]>
Re: [PATCH mm-unstable 1/5] mm: zpool: Add interfaces for object read/write APIs
In reply to Yosry AhmedQuoted 4 lines · Yosry Ahmed
Zsmalloc introduce new APIs to read/write objects besides mapping them. Add the necessary zpool interfaces.
Signed-off-by Yosry Ahmed <[email protected]>
Re: [PATCH mm-unstable 1/5] mm: zpool: Add interfaces for object read/write APIs
In reply to Yosry AhmedQuoted 5 lines · Yosry Ahmed
Zsmalloc introduce new APIs to read/write objects besides mapping them. Add the necessary zpool interfaces.
Signed-off-by Yosry Ahmed <[email protected]>
PATCH 2/5 mm: zswap: Use object read/write APIs instead of object mapping APIs
In reply to Yosry AhmedUse the new object read/write APIs instead of mapping APIs.
On compress side, zpool_obj_write() is more concise and provides exactly what zswap needs to write the compressed object to the zpool, instead of map->copy->unmap.
On the decompress side, zpool_obj_read_begin() is sleepable, which allows avoiding the memcpy() for zsmalloc and slightly simplifying the code by:
- Avoiding checking if the zpool driver is sleepable, reducing special cases and shrinking the huge comment.
- Having a single zpool_obj_read_end() call rather than multiple conditional zpool_unmap_handle() calls.
The !virt_addr_valid() case can be removed in the future if the crypto API supports kmap addresses or by using kmap_to_page(), completely eliminating the memcpy() path in zswap_decompress(). This a step toward that. In that spirit, opportunistically make the comment more specific about the kmap case instead of generic non-linear addresses. This is the only case that needs to be handled in practice, and the generic comment makes it seem like a bigger problem that it actually is.
---
Herbert, I think we can completely get rid of the memcpy() in zswap_decompress() if we can pass a highmem address to sg and crypto. I believe your new virtual address API may be used here for this?
--- mm/zswap.c | 33 +++++++++++++-------------------- 1 file changed, 13 insertions(+), 20 deletions(-)
mm/zswap.c +13 -20
@@ -930,7 +930,6 @@ static bool zswap_compress(struct page *page, struct zswap_entry *entry,unsigned int dlen =PAGE_SIZE ;unsigned long handle;struct zpool *zpool; -char *buf;gfp_t gfp;u8 *dst;
@@ -972,10 +971,7 @@ static bool zswap_compress(struct page *page, struct zswap_entry *entry,if (alloc_ret) goto unlock; - buf =zpool_map_handle (zpool, handle,ZPOOL_MM_WO ); -memcpy (buf, dst, dlen); -zpool_unmap_handle (zpool, handle); - +zpool_obj_write (zpool, handle, dst, dlen); entry->handle = handle; entry->length = dlen;
@@ -996,24 +992,22 @@ static void zswap_decompress(struct zswap_entry *entry, struct folio *folio)struct zpool *zpool = entry->pool->zpool;struct scatterlist input, output;struct crypto_acomp_ctx *acomp_ctx; -u8 *src; +u8 *src, *obj; acomp_ctx =acomp_ctx_get_cpu_lock (entry->pool); - src =zpool_map_handle (zpool, entry->handle,ZPOOL_MM_RO ); + obj =zpool_obj_read_begin (zpool, entry->handle, acomp_ctx->buffer); +/* -* If zpool_map_handle is atomic, we cannot reliably utilize its mapped buffer -* to do crypto_acomp_decompress() which might sleep. In such cases, we must -* resort to copying the buffer to a temporary one. -* Meanwhile, zpool_map_handle() might return a non-linearly mapped buffer, -* such as a kmap address of high memory or even ever a vmap address. -* However, sg_init_one is only equipped to handle linearly mapped low memory. -* In such cases, we also must copy the buffer to a temporary and lowmem one. +* zpool_obj_read_begin() might return a kmap address of highmem when +* acomp_ctx->buffer is not used. However, sg_init_one() does not +* handle highmem addresses, so copy the object to acomp_ctx->buffer. */ -if ((acomp_ctx->is_sleepable && !zpool_can_sleep_mapped (zpool)) || - !virt_addr_valid (src)) { -memcpy (acomp_ctx->buffer, src, entry->length); +if (virt_addr_valid (obj)) { + src = obj; + }else { +WARN_ON_ONCE (obj == acomp_ctx->buffer); +memcpy (acomp_ctx->buffer, obj, entry->length); src = acomp_ctx->buffer; -zpool_unmap_handle (zpool, entry->handle); }sg_init_one (&input, src, entry->length);
@@ -1023,8 +1017,7 @@ static void zswap_decompress(struct zswap_entry *entry, struct folio *folio)BUG_ON (crypto_wait_req (crypto_acomp_decompress (acomp_ctx->req), &acomp_ctx->wait));BUG_ON (acomp_ctx->req->dlen !=PAGE_SIZE ); -if (src != acomp_ctx->buffer) -zpool_unmap_handle (zpool, entry->handle); +zpool_obj_read_end (zpool, entry->handle, obj);acomp_ctx_put_unlock (acomp_ctx); }
Signature · 1 line
2.48.1.711.g2feabab25a-goog
Re: [PATCH mm-unstable 2/5] mm: zswap: Use object read/write APIs instead of object mapping APIs
Quoted 22 lines · Yosry Ahmed
Use the new object read/write APIs instead of mapping APIs.
On compress side,
zpool_obj_write()is more concise and provides exactly what zswap needs to write the compressed object to the zpool, instead ofmap->copy->unmap.On the decompress side,
zpool_obj_read_begin()is sleepable, which allows avoiding thememcpy()for zsmalloc and slightly simplifying the code by:
- Avoiding checking if the zpool driver is sleepable, reducing special cases and shrinking the huge comment.
- Having a singlezpool_obj_read_end()call rather than multiple conditionalzpool_unmap_handle()calls.The !virt_addr_valid() case can be removed in the future if the crypto API supports kmap addresses or by using
kmap_to_page(), completely eliminating thememcpy()path inzswap_decompress(). This a step toward that. In that spirit, opportunistically make the comment more specific about the kmap case instead of generic non-linear addresses. This is the only case that needs to be handled in practice, and the generic comment makes it seem like a bigger problem that it actually is.
Quoted 1 line
Signed-off-by Yosry Ahmed <[email protected]>
Re: [PATCH mm-unstable 2/5] mm: zswap: Use object read/write APIs instead of object mapping APIs
In reply to Yosry AhmedQuoted 23 lines · Yosry Ahmed
Use the new object read/write APIs instead of mapping APIs.
On compress side,
zpool_obj_write()is more concise and provides exactly what zswap needs to write the compressed object to the zpool, instead ofmap->copy->unmap.On the decompress side,
zpool_obj_read_begin()is sleepable, which allows avoiding thememcpy()for zsmalloc and slightly simplifying the code by:
- Avoiding checking if the zpool driver is sleepable, reducing special cases and shrinking the huge comment.
- Having a singlezpool_obj_read_end()call rather than multiple conditionalzpool_unmap_handle()calls.The !virt_addr_valid() case can be removed in the future if the crypto API supports kmap addresses or by using
kmap_to_page(), completely eliminating thememcpy()path inzswap_decompress(). This a step toward that. In that spirit, opportunistically make the comment more specific about the kmap case instead of generic non-linear addresses. This is the only case that needs to be handled in practice, and the generic comment makes it seem like a bigger problem that it actually is.
Quoted 1 line
Signed-off-by Yosry Ahmed <[email protected]>
PATCH 3/5 mm: zpool: Remove object mapping APIs
In reply to Yosry Ahmedzpool_map_handle(), zpool_unmap_handle(), and zpool_can_sleep_mapped() are no longer used. Remove them with the underlying driver callbacks.
--- include/linux/zpool.h | 30 --------------------- mm/zpool.c | 61 ------------------------------------------- mm/zsmalloc.c | 27 ------------------- 3 files changed, 118 deletions(-)
include/linux/zpool.h +0 -30
@@ -13,25 +13,6 @@struct zpool ; -/* -* Control how a handle is mapped. It will be ignored if the -* implementation does not support it. Its use is optional. -* Note that this does not refer to memory protection, it -* refers to how the memory will be copied in/out if copying -* is necessary during mapping; read-write is the safest as -* it copies the existing memory in on map, and copies the -* changed memory back out on unmap. Write-only does not copy -* in the memory and should only be used for initialization. -* If in doubt, use ZPOOL_MM_DEFAULT which is read-write. -*/ -enum zpool_mapmode { -ZPOOL_MM_RW ,/* normal read-write mapping */ -ZPOOL_MM_RO ,/* read-only (no copy-out at unmap time) */ -ZPOOL_MM_WO ,/* write-only (no copy-in at map time) */ - -ZPOOL_MM_DEFAULT =ZPOOL_MM_RW -}; -bool zpool_has_pool (char *type);struct zpool *zpool_create_pool (const char *type,const char *name,gfp_t gfp);
@@ -47,12 +28,6 @@ int zpool_malloc(struct zpool *pool, size_t size, gfp_t gfp,void zpool_free (struct zpool *pool,unsigned long handle); -void *zpool_map_handle (struct zpool *pool,unsigned long handle, -enum zpool_mapmode mm); - -void zpool_unmap_handle (struct zpool *pool,unsigned long handle); - -void *zpool_obj_read_begin (struct zpool *zpool,unsigned long handle,void *local_copy);
@@ -95,11 +70,6 @@ struct zpool_driver { unsigned long *handle);void (*free)(void *pool,unsigned long handle); -bool sleep_mapped; -void *(*map)(void *pool,unsigned long handle, -enum zpool_mapmode mm); -void (*unmap)(void *pool,unsigned long handle); -void *(*obj_read_begin)(void *pool,unsigned long handle,void *local_copy); void (*obj_read_end)(void *pool, unsigned long handle,
mm/zpool.c +0 -61
72 lines not on this page · raw message
mm/zsmalloc.c +0 -27
@@ -482,31 +482,6 @@ static void zs_zpool_free(void *pool, unsigned long handle)zs_free (pool, handle); } -static void *zs_zpool_map (void *pool,unsigned long handle, -enum zpool_mapmode mm) -{ -enum zs_mapmode zs_mm; - -switch (mm) { -case ZPOOL_MM_RO : - zs_mm =ZS_MM_RO ; -break ; -case ZPOOL_MM_WO : - zs_mm =ZS_MM_WO ; -break ; -case ZPOOL_MM_RW : -default : - zs_mm =ZS_MM_RW ; -break ; - } - -return zs_map_object (pool, handle, zs_mm); -} -static void zs_zpool_unmap (void *pool,unsigned long handle) -{ -zs_unmap_object (pool, handle); -} -static void *zs_zpool_obj_read_begin (void *pool,unsigned long handle,void *local_copy) {
@@ -538,8 +513,6 @@ static struct zpool_driver zs_zpool_driver = { .malloc_support_movable = true, .malloc = zs_zpool_malloc, .free = zs_zpool_free, - .map = zs_zpool_map, - .unmap = zs_zpool_unmap, .obj_read_begin = zs_zpool_obj_read_begin, .obj_read_end = zs_zpool_obj_read_end, .obj_write = zs_zpool_obj_write,
Signature · 1 line
2.48.1.711.g2feabab25a-goog
Re: [PATCH mm-unstable 3/5] mm: zpool: Remove object mapping APIs
Quoted 4 lines · Yosry Ahmed
zpool_map_handle(),zpool_unmap_handle(), andzpool_can_sleep_mapped()are no longer used. Remove them with the underlying driver callbacks.Signed-off-by Yosry Ahmed <[email protected]>
Re: [PATCH mm-unstable 3/5] mm: zpool: Remove object mapping APIs
In reply to Yosry AhmedQuoted 4 lines · Yosry Ahmed
zpool_map_handle(),zpool_unmap_handle(), andzpool_can_sleep_mapped()are no longer used. Remove them with the underlying driver callbacks.Signed-off-by Yosry Ahmed <[email protected]>
Re: [PATCH mm-unstable 3/5] mm: zpool: Remove object mapping APIs
In reply to Yosry AhmedQuoted 10 lines · Yosry Ahmed
zpool_map_handle(),zpool_unmap_handle(), andzpool_can_sleep_mapped()are no longer used. Remove them with the underlying driver callbacks.Signed-off-by Yosry Ahmed <[email protected]>--- include/linux/zpool.h | 30 --------------------- mm/zpool.c | 61 ------------------------------------------- mm/zsmalloc.c | 27 ------------------- 3 files changed, 118 deletions(-)
Me see deletions, me likey. I've never liked the object mapping API anyway.
Re: [PATCH mm-unstable 3/5] mm: zpool: Remove object mapping APIs
In reply to Yosry AhmedQuoted 9 lines · Yosry Ahmed
zpool_map_handle(),zpool_unmap_handle(), andzpool_can_sleep_mapped()are no longer used. Remove them with the underlying driver callbacks.Signed-off-by Yosry Ahmed <[email protected]>--- include/linux/zpool.h | 30 --------------------- mm/zpool.c | 61 ------------------------------------------- mm/zsmalloc.c | 27 ------------------- 3 files changed, 118 deletions(-)
This patch breaks zbud and z3fold because they haven't been converted to the new interface.
Cheers,
Signature · 3 lines
Email: Herbert Xu <[email protected]> Home Page: http://gondor.apana.org.au/~herbert/ PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt
Re: [PATCH mm-unstable 3/5] mm: zpool: Remove object mapping APIs
Quoted 3 lines · Herbert Xu
This patch breaks zbud and z3fold because they haven't been converted to the new interface.
I've rebased my zswap SG patch on top of your series. I've removed all the mapping code from zpool/zsmalloc and pushed it out to zram instead.
This patch depends on a new memcpy_sglist function which I've just posted a patch for:
https://patchwork.kernel.org/project/linux-crypto/patch/[email protected]/
From a77ee529b831e7e606ed2a5b723b74ce234a3915 Mon Sep 17 00:00:00 2001
From: Herbert Xu <[email protected]>
Date: Thu, 6 Mar 2025 12:13:58 +0800
Subject: [PATCH] mm: zswap: Give non-linear objects to Crypto API
Instead of copying non-linear objects into a buffer, use the scatterlist to give them directly to the Crypto API.
--- drivers/block/zram/zram_drv.c | 58 ++++++++++++---- include/linux/zpool.h | 26 +++---- include/linux/zsmalloc.h | 11 ++- mm/z3fold.c | 17 +++-- mm/zbud.c | 45 +++++++------ mm/zpool.c | 54 ++++++--------- mm/zsmalloc.c | 123 +++++++--------------------------- mm/zswap.c | 35 +++------- 8 files changed, 148 insertions(+), 221 deletions(-)
drivers/block/zram/zram_drv.c +44 -14
@@ -15,6 +15,7 @@#define KMSG_COMPONENT "zram"#define pr_fmt (fmt) KMSG_COMPONENT ": " fmt +#include <crypto/scatterwalk.h> #include <linux/module.h> #include <linux/kernel.h> #include <linux/bio.h>
@@ -23,6 +24,7 @@#include <linux/buffer_head.h> #include <linux/device.h> #include <linux/highmem.h> +#include <linux/scatterlist.h> #include <linux/slab.h> #include <linux/backing-dev.h> #include <linux/string.h>
@@ -1559,21 +1561,23 @@ static int read_same_filled_page(struct zram *zram, struct page *page,static int read_incompressible_page (struct zram *zram,struct page *page,u32 index) { +struct scatterlist output[1 ]; +struct scatterlist input[2 ];unsigned long handle; -void *src, *dst; +sg_init_table (output,1 ); +sg_set_page (output, page,PAGE_SIZE ,0 ); handle =zram_get_handle (zram, index); - src =zs_obj_read_begin (zram->mem_pool, handle,NULL ); - dst =kmap_local_page (page); -copy_page (dst, src); -kunmap_local (dst); -zs_obj_read_end (zram->mem_pool, handle, src); +zs_pin_object (zram->mem_pool, handle, input); +memcpy_sglist (output, input,PAGE_SIZE ); +zs_unpin_object (zram->mem_pool, handle);return 0 ; }static int read_compressed_page (struct zram *zram,struct page *page,u32 index) { +struct scatterlist input[2 ];struct zcomp_strm *zstrm;unsigned long handle;unsigned int size;
@@ -1585,11 +1589,22 @@ static int read_compressed_page(struct zram *zram, struct page *page, u32 index) prio =zram_get_priority (zram, index); zstrm =zcomp_stream_get (zram->comps[prio]); - src =zs_obj_read_begin (zram->mem_pool, handle, zstrm->local_copy); +zs_pin_object (zram->mem_pool, handle, input); +if (sg_is_last (input)) { +unsigned int offset = input[0 ].offset; + + src =kmap_local_page (sg_page (input) + (offset >>PAGE_SHIFT )); + src +=offset_in_page (offset); + }else { +memcpy_from_sglist (zstrm->local_copy, input,0 , size); + src = zstrm->local_copy; + } dst =kmap_local_page (page); ret =zcomp_decompress (zram->comps[prio], zstrm, src, size, dst);kunmap_local (dst); -zs_obj_read_end (zram->mem_pool, handle, src); +if (sg_is_last (input)) +kunmap_local (src); +zs_unpin_object (zram->mem_pool, handle);zcomp_stream_put (zstrm);return ret;
@@ -1684,8 +1699,9 @@ static int write_same_filled_page(struct zram *zram, unsigned long fill,static int write_incompressible_page (struct zram *zram,struct page *page,u32 index) { +struct scatterlist output[2 ]; +struct scatterlist input[1 ];unsigned long handle; -void *src;/* * This function is called from preemptible context so we don't need
@@ -1703,9 +1719,11 @@ static int write_incompressible_page(struct zram *zram, struct page *page,return -ENOMEM ; } - src =kmap_local_page (page); -zs_obj_write (zram->mem_pool, handle, src,PAGE_SIZE ); -kunmap_local (src); +sg_init_table (input,1 ); +sg_set_page (input, page,PAGE_SIZE ,0 ); +zs_pin_object (zram->mem_pool, handle, output); +memcpy_sglist (output, input,PAGE_SIZE ); +zs_unpin_object (zram->mem_pool, handle);zram_slot_lock (zram, index);zram_set_flag (zram, index,ZRAM_HUGE );
@@ -1723,6 +1741,8 @@ static int write_incompressible_page(struct zram *zram, struct page *page,static int zram_write_page (struct zram *zram,struct page *page,u32 index) { +struct scatterlist output[2 ]; +struct scatterlist input[1 ];int ret =0 ;unsigned long handle;unsigned int comp_len;
@@ -1773,7 +1793,11 @@ static int zram_write_page(struct zram *zram, struct page *page, u32 index)return -ENOMEM ; } -zs_obj_write (zram->mem_pool, handle, zstrm->buffer, comp_len); +sg_init_table (input,1 ); +sg_set_buf (input, zstrm->buffer, comp_len); +zs_pin_object (zram->mem_pool, handle, output); +memcpy_sglist (output, input, comp_len); +zs_unpin_object (zram->mem_pool, handle);zcomp_stream_put (zstrm);zram_slot_lock (zram, index);
@@ -1872,6 +1896,8 @@ static int recompress_slot(struct zram *zram, u32 index, struct page *page, u64 *num_recomp_pages, u32 threshold, u32 prio, u32 prio_max) { + struct scatterlist output[2]; +struct scatterlist input[1 ];struct zcomp_strm *zstrm =NULL ;unsigned long handle_old;unsigned long handle_new;
@@ -1990,7 +2016,11 @@ static int recompress_slot(struct zram *zram, u32 index, struct page *page,return PTR_ERR ((void *)handle_new); } -zs_obj_write (zram->mem_pool, handle_new, zstrm->buffer, comp_len_new); +sg_init_table (input,1 ); +sg_set_buf (input, zstrm->buffer, comp_len_new); +zs_pin_object (zram->mem_pool, handle_new, output); +memcpy_sglist (output, input, comp_len_new); +zs_unpin_object (zram->mem_pool, handle_new);zcomp_stream_put (zstrm);zram_free_page (zram, index);
include/linux/zpool.h +8 -18
@@ -12,6 +12,7 @@#ifndef _ZPOOL_H_#define _ZPOOL_H_ +struct scatterlist ;struct zpool ;bool zpool_has_pool (char *type);
@@ -27,14 +28,10 @@ int zpool_malloc(struct zpool *pool, size_t size, gfp_t gfp,void zpool_free (struct zpool *pool,unsigned long handle); -void *zpool_obj_read_begin (struct zpool *zpool,unsigned long handle, -void *local_copy); +void zpool_pin_handle (struct zpool *pool,unsigned long handle, +struct scatterlist *sg); -void zpool_obj_read_end (struct zpool *zpool,unsigned long handle, -void *handle_mem); - -void zpool_obj_write (struct zpool *zpool,unsigned long handle, -void *handle_mem,size_t mem_len); +void zpool_unpin_handle (struct zpool *pool,unsigned long handle);u64 zpool_get_total_pages (struct zpool *pool);
@@ -47,9 +44,8 @@ u64 zpool_get_total_pages(struct zpool *pool);* @destroy: destroy a pool. * @malloc: allocate mem from a pool. * @free: free mem from a pool. -* @sleep_mapped: whether zpool driver can sleep during map. -* @map: map a handle. -* @unmap: unmap a handle. +* @pin: pin a handle and write it into a two-entry SG list. +* @unpin: unpin a handle. * @total_size: get total size of a pool. * * This is created by a zpool implementation and registered
@@ -68,12 +64,8 @@ struct zpool_driver { unsigned long *handle);void (*free)(void *pool,unsigned long handle); -void *(*obj_read_begin)(void *pool,unsigned long handle, -void *local_copy); -void (*obj_read_end)(void *pool,unsigned long handle, -void *handle_mem); -void (*obj_write)(void *pool,unsigned long handle, -void *handle_mem,size_t mem_len); +void (*pin)(void *pool,unsigned long handle,struct scatterlist *sg); +void (*unpin)(void *pool,unsigned long handle);u64 (*total_pages)(void *pool); };
@@ -82,6 +74,4 @@ void zpool_register_driver(struct zpool_driver *driver);int zpool_unregister_driver (struct zpool_driver *driver); -bool zpool_can_sleep_mapped (struct zpool *pool); -#endif
include/linux/zsmalloc.h +5 -6
@@ -16,6 +16,8 @@#include <linux/types.h> +struct scatterlist ; +struct zs_pool_stats {/* How many pages were migrated (freed) */ atomic_long_t pages_compacted;
@@ -38,11 +40,8 @@ unsigned int zs_lookup_class_index(struct zs_pool *pool, unsigned int size);void zs_pool_stats (struct zs_pool *pool,struct zs_pool_stats *stats); -void *zs_obj_read_begin (struct zs_pool *pool,unsigned long handle, -void *local_copy); -void zs_obj_read_end (struct zs_pool *pool,unsigned long handle, -void *handle_mem); -void zs_obj_write (struct zs_pool *pool,unsigned long handle, -void *handle_mem,size_t mem_len); +void zs_pin_object (struct zs_pool *pool,unsigned long handle, +struct scatterlist *sg); +void zs_unpin_object (struct zs_pool *pool,unsigned long handle);#endif
mm/z3fold.c +10 -7
@@ -36,6 +36,7 @@#include <linux/percpu.h> #include <linux/preempt.h> #include <linux/workqueue.h> +#include <linux/scatterlist.h> #include <linux/slab.h> #include <linux/spinlock.h> #include <linux/zpool.h>
@@ -1392,12 +1393,15 @@ static void z3fold_zpool_free(void *pool, unsigned long handle)z3fold_free (pool, handle); } -static void *z3fold_zpool_map (void *pool,unsigned long handle, -enum zpool_mapmode mm) +static void z3fold_zpool_pin (void *pool,unsigned long handle, +struct scatterlist sg[2 ]) { -return z3fold_map (pool, handle); +void *buf =z3fold_map (pool, handle); + +sg_init_one (sg, buf,PAGE_SIZE -offset_in_page (buf)); } -static void z3fold_zpool_unmap (void *pool,unsigned long handle) + +static void z3fold_zpool_unpin (void *pool,unsigned long handle) {z3fold_unmap (pool, handle); }
@@ -1409,14 +1413,13 @@ static u64 z3fold_zpool_total_pages(void *pool)static struct zpool_driver z3fold_zpool_driver = { .type ="z3fold" , - .sleep_mapped = true, .owner =THIS_MODULE , .create = z3fold_zpool_create, .destroy = z3fold_zpool_destroy, .malloc = z3fold_zpool_malloc, .free = z3fold_zpool_free, - .map = z3fold_zpool_map, - .unmap = z3fold_zpool_unmap, + .pin = z3fold_zpool_pin, + .unpin = z3fold_zpool_unpin, .total_pages = z3fold_zpool_total_pages, };
mm/zbud.c +23 -22
95 lines not on this page · raw message
mm/zpool.c +21 -33
78 lines not on this page · raw message
mm/zsmalloc.c +26 -97
198 lines not on this page · raw message
mm/zswap.c +11 -24
@@ -13,6 +13,8 @@#define pr_fmt (fmt) KBUILD_MODNAME ": " fmt +#include <crypto/acompress.h> +#include <crypto/scatterwalk.h> #include <linux/module.h> #include <linux/cpu.h> #include <linux/highmem.h>
@@ -26,7 +28,6 @@#include <linux/mempolicy.h> #include <linux/mempool.h> #include <linux/zpool.h> -#include <crypto/acompress.h> #include <linux/zswap.h> #include <linux/mm_types.h> #include <linux/page-flags.h>
@@ -147,7 +148,6 @@ struct crypto_acomp_ctx {struct crypto_wait wait;u8 *buffer;struct mutex mutex; -bool is_sleepable; };/*
@@ -865,7 +865,6 @@ static int zswap_cpu_comp_prepare(unsigned int cpu, struct hlist_node *node) acomp_ctx->buffer = buffer; acomp_ctx->acomp = acomp; - acomp_ctx->is_sleepable =acomp_is_async (acomp); acomp_ctx->req = req;mutex_unlock (&acomp_ctx->mutex);return 0 ;
@@ -928,6 +927,7 @@ static bool zswap_compress(struct page *page, struct zswap_entry *entry,struct scatterlist input, output;int comp_ret =0 , alloc_ret =0 ;unsigned int dlen =PAGE_SIZE ; +struct scatterlist sg[2 ];unsigned long handle;struct zpool *zpool;gfp_t gfp;
@@ -969,7 +969,9 @@ static bool zswap_compress(struct page *page, struct zswap_entry *entry,if (alloc_ret) goto unlock; -zpool_obj_write (zpool, handle, dst, dlen); +zpool_pin_handle (zpool, handle, sg); +memcpy_to_sglist (sg,0 , dst, dlen); +zpool_unpin_handle (zpool, handle); entry->handle = handle; entry->length = dlen;
@@ -988,34 +990,19 @@ static bool zswap_compress(struct page *page, struct zswap_entry *entry,static void zswap_decompress (struct zswap_entry *entry,struct folio *folio) {struct zpool *zpool = entry->pool->zpool; -struct scatterlist input, output;struct crypto_acomp_ctx *acomp_ctx; -u8 *src, *obj; +struct scatterlist input[2 ]; +struct scatterlist output; acomp_ctx =acomp_ctx_get_cpu_lock (entry->pool); - obj =zpool_obj_read_begin (zpool, entry->handle, acomp_ctx->buffer); - -/* -* zpool_obj_read_begin() might return a kmap address of highmem when -* acomp_ctx->buffer is not used. However, sg_init_one() does not -* handle highmem addresses, so copy the object to acomp_ctx->buffer. -*/ -if (virt_addr_valid (obj)) { - src = obj; - }else { -WARN_ON_ONCE (obj == acomp_ctx->buffer); -memcpy (acomp_ctx->buffer, obj, entry->length); - src = acomp_ctx->buffer; - } - -sg_init_one (&input, src, entry->length); +zpool_pin_handle (zpool, entry->handle, input);sg_init_table (&output,1 );sg_set_folio (&output, folio,PAGE_SIZE ,0 ); -acomp_request_set_params (acomp_ctx->req, &input, &output, entry->length,PAGE_SIZE ); +acomp_request_set_params (acomp_ctx->req, input, &output, entry->length,PAGE_SIZE );BUG_ON (crypto_wait_req (crypto_acomp_decompress (acomp_ctx->req), &acomp_ctx->wait));BUG_ON (acomp_ctx->req->dlen !=PAGE_SIZE ); -zpool_obj_read_end (zpool, entry->handle, obj); +zpool_unpin_handle (zpool, entry->handle);acomp_ctx_put_unlock (acomp_ctx); }
Signature · 7 lines
2.39.5 Cheers, -- Email: Herbert Xu <[email protected]> Home Page: http://gondor.apana.org.au/~herbert/ PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt
Re: [PATCH mm-unstable 3/5] mm: zpool: Remove object mapping APIs
Quoted 19 lines · Herbert Xu
Quoted 3 lines · Herbert Xu
This patch breaks zbud and z3fold because they haven't been converted to the new interface.
I've rebased my zswap SG patch on top of your series. I've removed all the mapping code from zpool/zsmalloc and pushed it out to zram instead.
This patch depends on a new
memcpy_sglistfunction which I've just posted a patch for:https://patchwork.kernel.org/project/linux-crypto/patch/[email protected]/
From
a77ee529b831e7e606ed2a5b723b74ce234a3915Mon Sep 17 00:00:00 2001
From: Herbert Xu <[email protected]>
Date: Thu, 6 Mar 2025 12:13:58 +0800
Subject: [PATCH] mm: zswap: Give non-linear objects to Crypto API
Quoted 4 lines
Instead of copying non-linear objects into a buffer, use the scatterlist to give them directly to the Crypto API.
Signed-off-by Herbert Xu <[email protected]>
The zswap and zsmalloc look good and the code is simpler. I am fine with this approach if Sergey is fine with it, although I wonder if we should update Sergey's patches in mm-unstable do this directly. Currently we are switching from mapping APIs to read/write APIs, and then quickly to the pinning APIs. The history will be confusing.
Sergey, do you prefer if we keep things as-is, or if you update your series to incorporate Herbert's changes for zsmalloc/zram, then I can update my series to incorporate the changes in zswap?
We can also combine the series into a single updated one with zsmalloc/zram/zswap changes.
Let me know what you prefer.
Re: [PATCH mm-unstable 3/5] mm: zpool: Remove object mapping APIs
Quoted 7 lines · Yosry Ahmed
The zswap and zsmalloc look good and the code is simpler. I am fine with this approach if Sergey is fine with it, although I wonder if we should update Sergey's patches in mm-unstable do this directly. Currently we are switching from mapping APIs to read/write APIs, and then quickly to the pinning APIs. The history will be confusing.
Quoted 8 lines
Sergey, do you prefer if we keep things as-is, or if you update your series to incorporate Herbert's changes for zsmalloc/zram, then I can update my series to incorporate the changes in zswap?
We can also combine the series into a single updated one with zsmalloc/zram/zswap changes.
Let me know what you prefer.
This patch is only illustrating what zswap would look like once we move to an SG-based interface. So I'm not actually submitting it for inclusion at this time.
Sergey has volunteed to add parameter support to acomp. Let's wait for that before making these changes to zsmalloc/zswap.
Thanks,
Signature · 3 lines
Email: Herbert Xu <[email protected]> Home Page: http://gondor.apana.org.au/~herbert/ PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt
Sergey Senozhatsky
maintainerRe: [PATCH mm-unstable 3/5] mm: zpool: Remove object mapping APIs
Quoted 12 lines · Herbert Xu
Quoted 11 lines · Yosry Ahmed
The zswap and zsmalloc look good and the code is simpler. I am fine with this approach if Sergey is fine with it, although I wonder if we should update Sergey's patches in mm-unstable do this directly. Currently we are switching from mapping APIs to read/write APIs, and then quickly to the pinning APIs. The history will be confusing.
Sergey, do you prefer if we keep things as-is, or if you update your series to incorporate Herbert's changes for zsmalloc/zram, then I can update my series to incorporate the changes in zswap?
Quoted 8 lines
Quoted 4 lines
We can also combine the series into a single updated one with zsmalloc/zram/zswap changes.
Let me know what you prefer.
This patch is only illustrating what zswap would look like once we move to an SG-based interface. So I'm not actually submitting it for inclusion at this time.
Ah, OK, that's good to know, I was also a bit puzzled. Perhaps next time we can label such patches as "POC" or something.
Quoted 2 lines
Sergey has volunteed to add parameter support to acomp. Let's wait for that before making these changes to zsmalloc/zswap.
Yup, I'll keep you posted (once I have something.)
Johannes Weiner
maintainerRe: [PATCH mm-unstable 3/5] mm: zpool: Remove object mapping APIs
In reply to Herbert XuQuoted 11 lines · Herbert Xu
Quoted 9 lines · Yosry Ahmed
zpool_map_handle(),zpool_unmap_handle(), andzpool_can_sleep_mapped()are no longer used. Remove them with the underlying driver callbacks.Signed-off-by Yosry Ahmed <[email protected]>--- include/linux/zpool.h | 30 --------------------- mm/zpool.c | 61 ------------------------------------------- mm/zsmalloc.c | 27 ------------------- 3 files changed, 118 deletions(-)
Quoted 2 lines
This patch breaks zbud and z3fold because they haven't been converted to the new interface.
They're both scheduled for removal and already gone in the mm tree.
PATCH 4/5 mm: zsmalloc: Remove object mapping APIs and per-CPU map areas
In reply to Yosry Ahmedzs_map_object() and zs_unmap_object() are no longer used, remove them. Since these are the only users of per-CPU mapping_areas, remove them and the associated CPU hotplug callbacks too.
--- include/linux/cpuhotplug.h | 1 - include/linux/zsmalloc.h | 21 ---- mm/zsmalloc.c | 226 +------------------------------------ 3 files changed, 1 insertion(+), 247 deletions(-)
include/linux/cpuhotplug.h +0 -1
@@ -116,7 +116,6 @@ enum cpuhp_state {CPUHP_NET_IUCV_PREPARE ,CPUHP_ARM_BL_PREPARE ,CPUHP_TRACE_RB_PREPARE , -CPUHP_MM_ZS_PREPARE ,CPUHP_MM_ZSWP_POOL_PREPARE ,CPUHP_KVM_PPC_BOOK3S_PREPARE , CPUHP_ZCOMP_PREPARE,
include/linux/zsmalloc.h +0 -21
@@ -16,23 +16,6 @@#include <linux/types.h> -/* -* zsmalloc mapping modes -* -* NOTE: These only make a difference when a mapped object spans pages. -*/ -enum zs_mapmode { -ZS_MM_RW ,/* normal read-write mapping */ -ZS_MM_RO ,/* read-only (no copy-out at unmap time) */ -ZS_MM_WO /* write-only (no copy-in at map time) */ -/* -* NOTE: ZS_MM_WO should only be used for initializing new -* (uninitialized) allocations. Partial writes to already -* initialized allocations should use ZS_MM_RW to preserve the -* existing data. -*/ -}; -struct zs_pool_stats {/* How many pages were migrated (freed) */ atomic_long_t pages_compacted;
@@ -48,10 +31,6 @@ void zs_free(struct zs_pool *pool, unsigned long obj);size_t zs_huge_class_size (struct zs_pool *pool); -void *zs_map_object (struct zs_pool *pool,unsigned long handle, -enum zs_mapmode mm); -void zs_unmap_object (struct zs_pool *pool,unsigned long handle); -unsigned long zs_get_total_pages (struct zs_pool *pool);unsigned long zs_compact (struct zs_pool *pool);
mm/zsmalloc.c +1 -225
273 lines not on this page · raw message
Signature · 1 line
2.48.1.711.g2feabab25a-goog
Re: [PATCH mm-unstable 4/5] mm: zsmalloc: Remove object mapping APIs and per-CPU map areas
Quoted 5 lines · Yosry Ahmed
zs_map_object()andzs_unmap_object()are no longer used, remove them. Since these are the only users of per-CPUmapping_areas, remove them and the associated CPU hotplug callbacks too.Signed-off-by Yosry Ahmed <[email protected]>
Re: [PATCH mm-unstable 4/5] mm: zsmalloc: Remove object mapping APIs and per-CPU map areas
In reply to Yosry AhmedQuoted 5 lines · Yosry Ahmed
zs_map_object()andzs_unmap_object()are no longer used, remove them. Since these are the only users of per-CPUmapping_areas, remove them and the associated CPU hotplug callbacks too.Signed-off-by Yosry Ahmed <[email protected]>
Re: [PATCH mm-unstable 4/5] mm: zsmalloc: Remove object mapping APIs and per-CPU map areas
In reply to Yosry AhmedQuoted 6 lines · Yosry Ahmed
zs_map_object()andzs_unmap_object()are no longer used, remove them. Since these are the only users of per-CPUmapping_areas, remove them and the associated CPU hotplug callbacks too.Signed-off-by Yosry Ahmed <[email protected]>
Re: [PATCH mm-unstable 4/5] mm: zsmalloc: Remove object mapping APIs and per-CPU map areas
In reply to Yosry AhmedQuoted 10 lines · Yosry Ahmed
zs_map_object()andzs_unmap_object()are no longer used, remove them. Since these are the only users of per-CPUmapping_areas, remove them and the associated CPU hotplug callbacks too.Signed-off-by Yosry Ahmed <[email protected]>--- include/linux/cpuhotplug.h | 1 - include/linux/zsmalloc.h | 21 ---- mm/zsmalloc.c | 226 +------------------------------------ 3 files changed, 1 insertion(+), 247 deletions(-)
I missed updating the docs. Andrew, could you please squash the following diff in? I intentionally did not state the name of the new APIs to avoid needing to update the docs with similar changes in the future:
Documentation/mm/zsmalloc.rst +2 -3
@@ -27,9 +27,8 @@ Instead, it returns an opaque handle (unsigned long) which encodes actual location of the allocated object. The reason for this indirection is that zsmalloc does not keep zspages permanently mapped since that would cause issues on 32-bit systems where the VA region for kernel space mappings -is very small. So, before using the allocating memory, the object has to -be mapped using zs_map_object() to get a usable pointer and subsequently -unmapped using zs_unmap_object(). +is very small. So, using the allocated memory should be done through the +proper handle-based APIs. stat ====
PATCH 5/5 mm: zpool: Remove zpool_malloc_support_movable()
In reply to Yosry Ahmedzpool_malloc_support_movable() always returns true for zsmalloc, the only remaining zpool driver. Remove it and set the gfp flags in zswap_compress() accordingly. Opportunistically use GFP_NOWAIT instead of __GFP_NOWARN | __GFP_KSWAPD_RECLAIM for conciseness as they are equivalent.
--- include/linux/zpool.h | 3 --- mm/zpool.c | 16 ---------------- mm/zsmalloc.c | 1 - mm/zswap.c | 4 +--- 4 files changed, 1 insertion(+), 23 deletions(-)
include/linux/zpool.h +0 -3
@@ -21,8 +21,6 @@ const char *zpool_get_type(struct zpool *pool);void zpool_destroy_pool (struct zpool *pool); -bool zpool_malloc_support_movable (struct zpool *pool); -int zpool_malloc (struct zpool *pool,size_t size,gfp_t gfp,unsigned long *handle);
@@ -65,7 +63,6 @@ struct zpool_driver {void *(*create)(const char *name,gfp_t gfp);void (*destroy)(void *pool); -bool malloc_support_movable;int (*malloc)(void *pool,size_t size,gfp_t gfp,unsigned long *handle);void (*free)(void *pool,unsigned long handle);
mm/zpool.c +0 -16
@@ -220,22 +220,6 @@ const char *zpool_get_type(struct zpool *zpool)return zpool->driver->type; } -/** -* zpool_malloc_support_movable() - Check if the zpool supports -* allocating movable memory -* @zpool: The zpool to check -* -* This returns if the zpool supports allocating movable memory. -* -* Implementations must guarantee this to be thread-safe. -* -* Returns: true if the zpool supports allocating movable memory, false if not -*/ -bool zpool_malloc_support_movable (struct zpool *zpool) -{ -return zpool->driver->malloc_support_movable; -} -/** * zpool_malloc() - Allocate memory * @zpool: The zpool to allocate from.
mm/zsmalloc.c +0 -1
@@ -503,7 +503,6 @@ static struct zpool_driver zs_zpool_driver = { .owner = THIS_MODULE, .create = zs_zpool_create, .destroy = zs_zpool_destroy, - .malloc_support_movable = true, .malloc = zs_zpool_malloc, .free = zs_zpool_free, .obj_read_begin = zs_zpool_obj_read_begin,
mm/zswap.c +1 -3
@@ -964,9 +964,7 @@ static bool zswap_compress(struct page *page, struct zswap_entry *entry, goto unlock; zpool = pool->zpool; - gfp = __GFP_NORETRY | __GFP_NOWARN | __GFP_KSWAPD_RECLAIM; -if (zpool_malloc_support_movable (zpool)) - gfp |= __GFP_HIGHMEM | __GFP_MOVABLE; + gfp =GFP_NOWAIT | __GFP_NORETRY | __GFP_HIGHMEM | __GFP_MOVABLE; alloc_ret =zpool_malloc (zpool, dlen, gfp, &handle);if (alloc_ret) goto unlock;
Signature · 1 line
2.48.1.711.g2feabab25a-goog
Re: [PATCH mm-unstable 5/5] mm: zpool: Remove zpool_malloc_support_movable()
Quoted 7 lines · Yosry Ahmed
zpool_malloc_support_movable()always returns true for zsmalloc, the only remaining zpool driver. Remove it and set the gfp flags inzswap_compress()accordingly. Opportunistically useGFP_NOWAITinstead of__GFP_NOWARN|__GFP_KSWAPD_RECLAIMfor conciseness as they are equivalent.Signed-off-by Yosry Ahmed <[email protected]>
Re: [PATCH mm-unstable 5/5] mm: zpool: Remove zpool_malloc_support_movable()
In reply to Yosry AhmedQuoted 7 lines · Yosry Ahmed
zpool_malloc_support_movable()always returns true for zsmalloc, the only remaining zpool driver. Remove it and set the gfp flags inzswap_compress()accordingly. Opportunistically useGFP_NOWAITinstead of__GFP_NOWARN|__GFP_KSWAPD_RECLAIMfor conciseness as they are equivalent.Signed-off-by Yosry Ahmed <[email protected]>
Re: [PATCH mm-unstable 5/5] mm: zpool: Remove zpool_malloc_support_movable()
In reply to Yosry AhmedQuoted 8 lines · Yosry Ahmed
zpool_malloc_support_movable()always returns true for zsmalloc, the only remaining zpool driver. Remove it and set the gfp flags inzswap_compress()accordingly. Opportunistically useGFP_NOWAITinstead of__GFP_NOWARN|__GFP_KSWAPD_RECLAIMfor conciseness as they are equivalent.Signed-off-by Yosry Ahmed <[email protected]>
Sergey Senozhatsky
maintainerQuoted 3 lines · Yosry Ahmed
This patch series updates zswap to use the new object read/write APIs defined by zsmalloc in [1], and remove the old object mapping APIs and the related code from zpool and zsmalloc.
Thank you for working on this!