Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

zswap: don't param_set_charp while holding spinlock

Change the zpool/compressor param callback function to release the
zswap_pools_lock spinlock before calling param_set_charp, since that
function may sleep when it calls kmalloc with GFP_KERNEL.

While this problem has existed for a while, I wasn't able to trigger it
using a tight loop changing either/both the zpool and compressor params; I
think it's very unlikely to be an issue on the stable kernels, especially
since most zswap users will change the compressor and/or zpool from sysfs
only one time each boot - or zero times, if they add the params to the
kernel boot.

Fixes: c99b42c3529e ("zswap: use charp for zswap param strings")
Link: http://lkml.kernel.org/r/20170126155821.4545-1-ddstreet@ieee.org
Signed-off-by: Dan Streetman <dan.streetman@canonical.com>
Reported-by: Sergey Senozhatsky <sergey.senozhatsky.work@gmail.com>
Cc: Michal Hocko <mhocko@kernel.org>
Cc: Minchan Kim <minchan@kernel.org>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>

authored by

Dan Streetman and committed by
Linus Torvalds
fd5bb66c bae21db8

+13 -7
+13 -7
mm/zswap.c
··· 704 704 pool = zswap_pool_find_get(type, compressor); 705 705 if (pool) { 706 706 zswap_pool_debug("using existing", pool); 707 + WARN_ON(pool == zswap_pool_current()); 707 708 list_del_rcu(&pool->list); 708 - } else { 709 - spin_unlock(&zswap_pools_lock); 710 - pool = zswap_pool_create(type, compressor); 711 - spin_lock(&zswap_pools_lock); 712 709 } 710 + 711 + spin_unlock(&zswap_pools_lock); 712 + 713 + if (!pool) 714 + pool = zswap_pool_create(type, compressor); 713 715 714 716 if (pool) 715 717 ret = param_set_charp(s, kp); 716 718 else 717 719 ret = -EINVAL; 720 + 721 + spin_lock(&zswap_pools_lock); 718 722 719 723 if (!ret) { 720 724 put_pool = zswap_pool_current(); ··· 731 727 */ 732 728 list_add_tail_rcu(&pool->list, &zswap_pools); 733 729 put_pool = pool; 734 - } else if (!zswap_has_pool) { 730 + } 731 + 732 + spin_unlock(&zswap_pools_lock); 733 + 734 + if (!zswap_has_pool && !pool) { 735 735 /* if initial pool creation failed, and this pool creation also 736 736 * failed, maybe both compressor and zpool params were bad. 737 737 * Allow changing this param, so pool creation will succeed ··· 745 737 */ 746 738 ret = param_set_charp(s, kp); 747 739 } 748 - 749 - spin_unlock(&zswap_pools_lock); 750 740 751 741 /* drop the ref from either the old current pool, 752 742 * or the new pool we failed to add