Skip to content

Commit ce4be9e

Browse files
sergey-senozhatskyakpm00
authored andcommitted
zram: fix slot write race condition
Parallel concurrent writes to the same zram index result in leaked zsmalloc handles. Schematically we can have something like this: CPU0 CPU1 zram_slot_lock() zs_free(handle) zram_slot_lock() zram_slot_lock() zs_free(handle) zram_slot_lock() compress compress handle = zs_malloc() handle = zs_malloc() zram_slot_lock zram_set_handle(handle) zram_slot_lock zram_slot_lock zram_set_handle(handle) zram_slot_lock Either CPU0 or CPU1 zsmalloc handle will leak because zs_free() is done too early. In fact, we need to reset zram entry right before we set its new handle, all under the same slot lock scope. Link: https://lkml.kernel.org/r/[email protected] Fixes: 7126803 ("zram: free slot memory early during write") Signed-off-by: Sergey Senozhatsky <[email protected]> Reported-by: Changhui Zhong <[email protected]> Closes: https://lore.kernel.org/all/CAGVVp+UtpGoW5WEdEU7uVTtsSCjPN=ksN6EcvyypAtFDOUf30A@mail.gmail.com/ Tested-by: Changhui Zhong <[email protected]> Cc: Jens Axboe <[email protected]> Cc: Minchan Kim <[email protected]> Cc: <[email protected]> Signed-off-by: Andrew Morton <[email protected]>
1 parent 025e87f commit ce4be9e

File tree

1 file changed

+3
-5
lines changed

1 file changed

+3
-5
lines changed

drivers/block/zram/zram_drv.c

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1795,6 +1795,7 @@ static int write_same_filled_page(struct zram *zram, unsigned long fill,
17951795
u32 index)
17961796
{
17971797
zram_slot_lock(zram, index);
1798+
zram_free_page(zram, index);
17981799
zram_set_flag(zram, index, ZRAM_SAME);
17991800
zram_set_handle(zram, index, fill);
18001801
zram_slot_unlock(zram, index);
@@ -1832,6 +1833,7 @@ static int write_incompressible_page(struct zram *zram, struct page *page,
18321833
kunmap_local(src);
18331834

18341835
zram_slot_lock(zram, index);
1836+
zram_free_page(zram, index);
18351837
zram_set_flag(zram, index, ZRAM_HUGE);
18361838
zram_set_handle(zram, index, handle);
18371839
zram_set_obj_size(zram, index, PAGE_SIZE);
@@ -1855,11 +1857,6 @@ static int zram_write_page(struct zram *zram, struct page *page, u32 index)
18551857
unsigned long element;
18561858
bool same_filled;
18571859

1858-
/* First, free memory allocated to this slot (if any) */
1859-
zram_slot_lock(zram, index);
1860-
zram_free_page(zram, index);
1861-
zram_slot_unlock(zram, index);
1862-
18631860
mem = kmap_local_page(page);
18641861
same_filled = page_same_filled(mem, &element);
18651862
kunmap_local(mem);
@@ -1901,6 +1898,7 @@ static int zram_write_page(struct zram *zram, struct page *page, u32 index)
19011898
zcomp_stream_put(zstrm);
19021899

19031900
zram_slot_lock(zram, index);
1901+
zram_free_page(zram, index);
19041902
zram_set_handle(zram, index, handle);
19051903
zram_set_obj_size(zram, index, comp_len);
19061904
zram_slot_unlock(zram, index);

0 commit comments

Comments
 (0)