mm/zswap.c: fix two bugs in zswap_writeback_entry()
authorMiaohe Lin <linmiaohe@huawei.com>
Thu, 1 Jul 2021 01:52:55 +0000 (18:52 -0700)
committerLinus Torvalds <torvalds@linux-foundation.org>
Thu, 1 Jul 2021 03:47:31 +0000 (20:47 -0700)
In the ZSWAP_SWAPCACHE_FAIL and ZSWAP_SWAPCACHE_EXIST case, we forgot to
call zpool_unmap_handle() when zpool can't sleep. And we might sleep in
zswap_get_swap_cache_page() while zpool can't sleep. To fix all of these,
zpool_unmap_handle() should be done before zswap_get_swap_cache_page()
when zpool can't sleep.

Link: https://lkml.kernel.org/r/20210522092242.3233191-4-linmiaohe@huawei.com
Fixes: fc6697a89f56 ("mm/zswap: add the flag can_sleep_mapped")
Signed-off-by: Miaohe Lin <linmiaohe@huawei.com>
Cc: Colin Ian King <colin.king@canonical.com>
Cc: Dan Streetman <ddstreet@ieee.org>
Cc: Nathan Chancellor <nathan@kernel.org>
Cc: Sebastian Andrzej Siewior <bigeasy@linutronix.de>
Cc: Seth Jennings <sjenning@redhat.com>
Cc: Tian Tao <tiantao6@hisilicon.com>
Cc: Vitaly Wool <vitaly.wool@konsulko.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
mm/zswap.c

index c27ce9f..7944e3e 100644 (file)
@@ -967,6 +967,13 @@ static int zswap_writeback_entry(struct zpool *pool, unsigned long handle)
        spin_unlock(&tree->lock);
        BUG_ON(offset != entry->offset);
 
+       src = (u8 *)zhdr + sizeof(struct zswap_header);
+       if (!zpool_can_sleep_mapped(pool)) {
+               memcpy(tmp, src, entry->length);
+               src = tmp;
+               zpool_unmap_handle(pool, handle);
+       }
+
        /* try to allocate swap cache page */
        switch (zswap_get_swap_cache_page(swpentry, &page)) {
        case ZSWAP_SWAPCACHE_FAIL: /* no memory or invalidate happened */
@@ -982,17 +989,7 @@ static int zswap_writeback_entry(struct zpool *pool, unsigned long handle)
        case ZSWAP_SWAPCACHE_NEW: /* page is locked */
                /* decompress */
                acomp_ctx = raw_cpu_ptr(entry->pool->acomp_ctx);
-
                dlen = PAGE_SIZE;
-               src = (u8 *)zhdr + sizeof(struct zswap_header);
-
-               if (!zpool_can_sleep_mapped(pool)) {
-
-                       memcpy(tmp, src, entry->length);
-                       src = tmp;
-
-                       zpool_unmap_handle(pool, handle);
-               }
 
                mutex_lock(acomp_ctx->mutex);
                sg_init_one(&input, src, entry->length);