Received: by 2002:a05:7208:9594:b0:7e:5202:c8b4 with SMTP id gs20csp2613693rbb; Wed, 28 Feb 2024 07:19:14 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCUOOnqG/KmhNagwxnD4WFdpmvqA4O2mFMrYSQ2U8vdJKOCZTcBJ+wf4l+TgvLnM3KCBc1sTs8RcGa/bKtnuB9AQCXIzyx9leJscjlJktQ== X-Google-Smtp-Source: AGHT+IGWl6tv9lktuqBOceD9FGlIosyPzhSglkc4p8Z+3rEYQxe8jfrVFqRMsyMbhB3t8Gv6bLw9 X-Received: by 2002:a05:6808:1391:b0:3c1:9b87:dd90 with SMTP id c17-20020a056808139100b003c19b87dd90mr6121997oiw.34.1709133554755; Wed, 28 Feb 2024 07:19:14 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1709133554; cv=pass; d=google.com; s=arc-20160816; b=AGje85i0zAS9xg0fbJIN/aw5oeIY9Sh0SDx4bWcZn4kc/U49tYcJR82VR1Gp5JKE4X 3TuuWSjnRRoBKkBmnnr0MyaKNZeDpGie5FKbPYb/RXXze0JeHhrI6j5c+nMoQ5agIxO1 pj2iuVUQXpwmv2q6wJiVONd3SL25uO/0fw6KyUK+rk8ZeX3MN1WiiCD9wPjZEKGF5cXf 8mUHk0di5gR1PgykbcpMY3W5q06GA1QCfBg0d5m21NotyCfPlbKIU+hYK9Vy4Mcgq+J0 93VWmCh3QW3U88XL9ik+H7yAIgE4d8CxjHaNAF4ap/jTuDl85kX/6Q2qmVBW0K0i/5rB E/fA== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from:dkim-signature; bh=dQ/GDqUz6wZpxgKFsr9ka4SjiFzrDvg9/BbQeXQM+7g=; fh=ASxnO2IJH+Un7Jag7/fChUjZevm4sKtHwF56k3GLEsE=; b=WG5iG+vaxyxrsjzojC/PUBP0sII+8ICsJBE7O6c65dwMMvD35+tUID6mPFkTQNNLkV d3BDuhIgK3BYxh6P2o6InlS0fbJlYmOwixNbHfm7Lsk/eIVXqgqL4Bq5Skxlkduujs3M 375bmKISTrD4uwyhjpQB438p5ZvM1JPJpgnoGEj9I8yMQmWIN5rhxcSlwcw3zkPic5Xm SJFfUA0Z/8spHHKabIJ9rs9QqscZr9COAM7vgWHfAg7F7zWnVOxhFhUELPmErySgWVH6 ycON4Vs64Jngmjin1j2kBZkJzUlOXIxxa8TmOT5UjJQkWHO3+zEjGGapZ4I5lzHrx4Ph D9JA==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@linux.dev header.s=key1 header.b=H9isTi+Z; arc=pass (i=1 spf=pass spfdomain=linux.dev dkim=pass dkdomain=linux.dev dmarc=pass fromdomain=linux.dev); spf=pass (google.com: domain of linux-kernel+bounces-85251-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-85251-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linux.dev Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [2604:1380:45d1:ec00::1]) by mx.google.com with ESMTPS id z9-20020ac875c9000000b0042e5ac6459asi9482498qtq.73.2024.02.28.07.19.14 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 28 Feb 2024 07:19:14 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-85251-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) client-ip=2604:1380:45d1:ec00::1; Authentication-Results: mx.google.com; dkim=pass header.i=@linux.dev header.s=key1 header.b=H9isTi+Z; arc=pass (i=1 spf=pass spfdomain=linux.dev dkim=pass dkdomain=linux.dev dmarc=pass fromdomain=linux.dev); spf=pass (google.com: domain of linux-kernel+bounces-85251-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-85251-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linux.dev Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ny.mirrors.kernel.org (Postfix) with ESMTPS id 22A561C25AF0 for ; Wed, 28 Feb 2024 15:19:08 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id F332615CD43; Wed, 28 Feb 2024 15:18:51 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b="H9isTi+Z" Received: from out-176.mta0.migadu.com (out-176.mta0.migadu.com [91.218.175.176]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id D13741C10 for ; Wed, 28 Feb 2024 15:18:48 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=91.218.175.176 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709133530; cv=none; b=bahPYlGlYus9kfgWR7IT5ZD4tcnpbfrZUt0u62AeUkPGQLUtxYgUtqSRyJtkFBy7iDzEvt/DPK20R1lGM8VlHl7uYcM6FpRv6OKiH5B/UwRtzFOtHLW+9Ro9JNxgJ8vsKEwvnmGmqx8CHlv1ZOu55iLdq3Bc3MV+S1uQLqG4hzo= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709133530; c=relaxed/simple; bh=stUe9QZ/uCOkF302zZNjNXzNH7QMbzuu9f7aaU4HsLk=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=glgaeE/L6pXMFG4Q4uxUgNVsfoSx5QkmU2mnANTupRM5bKkUNhY7hvtex/obP2/Zx5+B8CCHxDrbJNvQQ4iGj41fnTDg7/Bb3slByCiQW8LGQJu+GAYsQhVeH6TFB87/jZnDDenVTa592ej563ltMs/5kgCIFllV9cyo6POXKA0= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev; spf=pass smtp.mailfrom=linux.dev; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b=H9isTi+Z; arc=none smtp.client-ip=91.218.175.176 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.dev X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1709133526; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=dQ/GDqUz6wZpxgKFsr9ka4SjiFzrDvg9/BbQeXQM+7g=; b=H9isTi+ZhtCmS+Oppv/gIrbAUAkhjlICKB+k/lnApdMQ3QJJpeqyg2taL+x2pYwXu2nrAu 8GaheRZYXPFBCrw3hgG2vfHe4PO/QhyZgg6cUtRLuLObphL7HPw75W0F7HGgL4giMnMfMq yAoP2fDyrijAgjA0MJuybdimHhVNphI= From: Chengming Zhou To: akpm@linux-foundation.org Cc: hannes@cmpxchg.org, yosryahmed@google.com, nphamcs@gmail.com, linux-mm@kvack.org, linux-kernel@vger.kernel.org, Chengming Zhou Subject: [PATCH mm-unstable hotfix] mm/zswap: fix zswap_pools_lock usages after changing to percpu_ref Date: Wed, 28 Feb 2024 15:18:32 +0000 Message-Id: <20240228151832.2431993-1-chengming.zhou@linux.dev> In-Reply-To: <20240210-zswap-global-lru-v3-2-200495333595@bytedance.com> References: <20240210-zswap-global-lru-v3-2-200495333595@bytedance.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Migadu-Flow: FLOW_OUT Now the release of zswap pool is controlled by percpu_ref, its release callback (__zswap_pool_empty()) will be called when percpu_ref hit 0. But this release callback may potentially be called from RCU callback context by percpu_ref_kill(), which maybe in the interrupt context. So we need to use spin_lock_irqsave() and spin_unlock_irqrestore() in the release callback: __zswap_pool_empty(). In other task context places, spin_lock_irq() and spin_unlock_irq() are enough to avoid potential deadlock. This problem is introduced by the commit f3da427e82c4 ("mm/zswap: change zswap_pool kref to percpu_ref"), which is in mm-unstable branch now. It can be reproduced by testing kernel build in tmpfs with zswap and CONFIG_LOCKDEP enabled, meanwhile changing the zswap compressor setting dynamically. Signed-off-by: Chengming Zhou --- mm/zswap.c | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/mm/zswap.c b/mm/zswap.c index 011e068eb355..894bd184f78e 100644 --- a/mm/zswap.c +++ b/mm/zswap.c @@ -456,10 +456,11 @@ static struct zswap_pool *zswap_pool_current(void); static void __zswap_pool_empty(struct percpu_ref *ref) { struct zswap_pool *pool; + unsigned long flags; pool = container_of(ref, typeof(*pool), ref); - spin_lock(&zswap_pools_lock); + spin_lock_irqsave(&zswap_pools_lock, flags); WARN_ON(pool == zswap_pool_current()); @@ -468,7 +469,7 @@ static void __zswap_pool_empty(struct percpu_ref *ref) INIT_WORK(&pool->release_work, __zswap_pool_release); schedule_work(&pool->release_work); - spin_unlock(&zswap_pools_lock); + spin_unlock_irqrestore(&zswap_pools_lock, flags); } static int __must_check zswap_pool_get(struct zswap_pool *pool) @@ -598,7 +599,7 @@ static int __zswap_param_set(const char *val, const struct kernel_param *kp, return -EINVAL; } - spin_lock(&zswap_pools_lock); + spin_lock_irq(&zswap_pools_lock); pool = zswap_pool_find_get(type, compressor); if (pool) { @@ -607,7 +608,7 @@ static int __zswap_param_set(const char *val, const struct kernel_param *kp, list_del_rcu(&pool->list); } - spin_unlock(&zswap_pools_lock); + spin_unlock_irq(&zswap_pools_lock); if (!pool) pool = zswap_pool_create(type, compressor); @@ -628,7 +629,7 @@ static int __zswap_param_set(const char *val, const struct kernel_param *kp, else ret = -EINVAL; - spin_lock(&zswap_pools_lock); + spin_lock_irq(&zswap_pools_lock); if (!ret) { put_pool = zswap_pool_current(); @@ -643,7 +644,7 @@ static int __zswap_param_set(const char *val, const struct kernel_param *kp, put_pool = pool; } - spin_unlock(&zswap_pools_lock); + spin_unlock_irq(&zswap_pools_lock); if (!zswap_has_pool && !pool) { /* if initial pool creation failed, and this pool creation also -- 2.40.1