[PATCH v3 06/11] zram: allocate zsmalloc object under slot lock
From: Sergey Senozhatsky
Date: Wed Nov 11 2026 - 01:56:24 EST
zram_write_page() and write_incompressible_page() allocate and
write zsmalloc object first and take slot lock only to free the
old slot data and set the new handle. A subsequent patch makes
zsmalloc store object location directly in zram table entry, so
we need to free the slot before we allocate a new object for it.
Free the slot and allocate the object under slot lock.
recompress_slot() already allocates objects under it, and
GFP_NOIO reclaim cannot re-enter zram write path.
mem_limit is checked before slot_free() (previous patch), so hitting
the limit keeps the old slot data. zs_malloc() can still fail after
slot_free(), e.g. when we write from reclaim (PF_MEMALLOC), which is
swap-out, so the old slot data (if any) is stale anyway, or when the
writer is an OOM victim that has no reserves left. The slot is left
empty then, matching standard block write-failure semantics.
Signed-off-by: Sergey Senozhatsky <senozhatsky@xxxxxxxxxxxx>
---
drivers/block/zram/zram_drv.c | 13 ++++++++-----
1 file changed, 8 insertions(+), 5 deletions(-)
diff --git a/drivers/block/zram/zram_drv.c b/drivers/block/zram/zram_drv.c
index 74e02cf4f53d..dbcbc267344e 100644
--- a/drivers/block/zram/zram_drv.c
+++ b/drivers/block/zram/zram_drv.c
@@ -2261,11 +2261,15 @@ static int write_incompressible_page(struct zram *zram, struct page *page,
struct zs_handle *handle;
void *src;
+ slot_lock(zram, index);
+ slot_free(zram, index);
handle = zs_malloc(zram->mem_pool, PAGE_SIZE,
GFP_NOIO | __GFP_NOWARN |
__GFP_HIGHMEM | __GFP_MOVABLE, page_to_nid(page));
- if (IS_ERR(handle))
+ if (IS_ERR(handle)) {
+ slot_unlock(zram, index);
return PTR_ERR(handle);
+ }
update_used_max(zram);
@@ -2273,8 +2277,6 @@ static int write_incompressible_page(struct zram *zram, struct page *page,
zs_obj_write(zram->mem_pool, handle, src, PAGE_SIZE);
kunmap_local(src);
- slot_lock(zram, index);
- slot_free(zram, index);
set_slot_flag(zram, index, ZRAM_HUGE);
zram->table[index].handle = handle;
set_slot_size(zram, index, PAGE_SIZE);
@@ -2326,11 +2328,14 @@ static int zram_write_page(struct zram *zram, struct page *page,
return write_incompressible_page(zram, page, index);
}
+ slot_lock(zram, index);
+ slot_free(zram, index);
handle = zs_malloc(zram->mem_pool, comp_len,
GFP_NOIO | __GFP_NOWARN |
__GFP_HIGHMEM | __GFP_MOVABLE, page_to_nid(page));
if (IS_ERR(handle)) {
zcomp_cstrm_put(zstrm);
+ slot_unlock(zram, index);
return PTR_ERR(handle);
}
@@ -2339,8 +2344,6 @@ static int zram_write_page(struct zram *zram, struct page *page,
zs_obj_write(zram->mem_pool, handle, zstrm->buffer, comp_len);
zcomp_cstrm_put(zstrm);
- slot_lock(zram, index);
- slot_free(zram, index);
zram->table[index].handle = handle;
set_slot_size(zram, index, comp_len);
mark_slot_accessed(zram, index);
--
2.56.0.542.g3ef89b7020-goog