From nobody Thu Apr 9 17:19:55 2026 Received: from out-189.mta1.migadu.com (out-189.mta1.migadu.com [95.215.58.189]) (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 B152133F392 for ; Fri, 6 Mar 2026 11:51:10 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=95.215.58.189 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1772797872; cv=none; b=LWIKl4Kt17QsXPB1ugvTo7bG7zWoJEgsJNwVoPjWn8sz2tg0c57CH//l4QzKAhumKRVKbOpfUdIbdr4Br0v04Q3ZLWsy0yvXicPpWNuxHJYsn8QTtlOkXQ+qRwm8U1OBM8VBTR2tf4t9IB9UXQEV/fHM4fpNHfGcGrfd8n5BUpg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1772797872; c=relaxed/simple; bh=J2IvZFkJs/jAnH1ff1YlObO2es+kOzOgbyxmdTcGZ/A=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=ugr6pgVAYNs7jMvOB3QJtcx9K8M+fqNu4FeSgwsv5LboTXx+h7BvKclrxvzush9SGzOwSG1YubiUZfwg9Exm18c+4w0AwFBFQWcYR9ZRQN2mxXWZKER3vFCnANDkrOlanIE7Qi1uXMgp9DTdUJAEbLvrQDZO9BM70VRDh3qAKNE= 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; arc=none smtp.client-ip=95.215.58.189 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. From: Hui Zhu To: Andrew Morton , Chris Li , Kairui Song , Kemeng Shi , Nhat Pham , Baoquan He , Barry Song , linux-mm@kvack.org, linux-kernel@vger.kernel.org Cc: Hui Zhu Subject: [PATCH 1/2] mm/swap: fix missing locks in swap_reclaim_work() Date: Fri, 6 Mar 2026 19:50:36 +0800 Message-ID: <02f5912caa6c427705bf8da43497801caf3b102f.1772797581.git.zhuhui@kylinos.cn> In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Hui Zhu swap_cluster_alloc_table() assumes that the caller holds the following locks: ci->lock percpu_swap_cluster.lock si->global_cluster_lock (required for non-SWP_SOLIDSTATE devices) There are five call paths leading to swap_cluster_alloc_table(): swap_alloc_hibernation_slot->cluster_alloc_swap_entry ->alloc_swap_scan_list->isolate_lock_cluster->swap_cluster_alloc_table swap_alloc_slow->cluster_alloc_swap_entry->alloc_swap_scan_list ->isolate_lock_cluster->swap_cluster_alloc_table swap_alloc_hibernation_slot->cluster_alloc_swap_entry ->swap_reclaim_full_clusters->isolate_lock_cluster ->swap_cluster_alloc_table swap_alloc_slow->cluster_alloc_swap_entry->swap_reclaim_full_clusters ->isolate_lock_cluster->swap_cluster_alloc_table swap_reclaim_work->swap_reclaim_full_clusters->isolate_lock_cluster ->swap_cluster_alloc_table Other paths correctly acquire the necessary locks before calling swap_cluster_alloc_table(). But the swap_reclaim_work() path fails to acquire percpu_swap_cluster.lock and, for non-SWP_SOLIDSTATE devices, si->global_cluster_lock. This patch fixes the issue by ensuring swap_reclaim_work() properly acquires the required locks before proceeding with the swap cluster allocation. Signed-off-by: Hui Zhu --- mm/swapfile.c | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/mm/swapfile.c b/mm/swapfile.c index 94af29d1de88..2e8717f84ba3 100644 --- a/mm/swapfile.c +++ b/mm/swapfile.c @@ -1031,7 +1031,15 @@ static void swap_reclaim_work(struct work_struct *wo= rk) =20 si =3D container_of(work, struct swap_info_struct, reclaim_work); =20 + local_lock(&percpu_swap_cluster.lock); + if (!(si->flags & SWP_SOLIDSTATE)) + spin_lock(&si->global_cluster_lock); + swap_reclaim_full_clusters(si, true); + + if (!(si->flags & SWP_SOLIDSTATE)) + spin_unlock(&si->global_cluster_lock); + local_unlock(&percpu_swap_cluster.lock); } =20 /* --=20 2.43.0 From nobody Thu Apr 9 17:19:55 2026 Received: from out-177.mta1.migadu.com (out-177.mta1.migadu.com [95.215.58.177]) (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 123773612D8 for ; Fri, 6 Mar 2026 11:51:12 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=95.215.58.177 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1772797874; cv=none; b=UBkDnVioiS/U94GhF/uGhcKUAW9is19RTmnEuoAQXYH9ul58AzayJ84azfvj5ewmvSgEagi39VgtVSq+TDLLQPk6thhmo0J+ySg9505zEooO9KLhpLuJIjjoJEfps3FZFLje+jYh038RCAkf+dJT0vU8ZAzffcfMEcG5fqeeu00= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1772797874; c=relaxed/simple; bh=E+gBT57VFq/KWzWdhPgLb48LIyp73ZpjTyQfWd2tAIU=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=fEFz4Ya1ugWGeUKb02E+ERoIm+VXjlgVTSydkdESLCwwYAHIBWUIXWDaizYgfENWE1E4i1pSXPmCqmf50DJaPh29OqhQ+cq9rSLyjM8ZfccSH/k2p13XPO8soTguH+iaGAyzki4IkqrYMbE0spu/z4WnzqZTWWbMCwZh6YGi4LU= 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; arc=none smtp.client-ip=95.215.58.177 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. From: Hui Zhu To: Andrew Morton , Chris Li , Kairui Song , Kemeng Shi , Nhat Pham , Baoquan He , Barry Song , linux-mm@kvack.org, linux-kernel@vger.kernel.org Cc: Hui Zhu Subject: [PATCH 2/2] mm/swap: add lockdep for si->global_cluster_lock in swap_cluster_alloc_table() Date: Fri, 6 Mar 2026 19:50:37 +0800 Message-ID: In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Hui Zhu Add a lockdep_assert_held(&si->global_cluster_lock) in swap_cluster_alloc_table() for non-SWP_SOLIDSTATE devices. The function already requires the caller to hold both ci->lock and percpu_swap_cluster.lock. And it also necessitates si->global_cluster_lock when the device is not SWP_SOLIDSTATE. Adding this assertion ensures locking consistency and helps catch potential synchronization issues during development. Signed-off-by: Hui Zhu --- mm/swapfile.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/mm/swapfile.c b/mm/swapfile.c index 2e8717f84ba3..1400a1585033 100644 --- a/mm/swapfile.c +++ b/mm/swapfile.c @@ -477,6 +477,8 @@ swap_cluster_alloc_table(struct swap_info_struct *si, * Swap allocator uses percpu clusters and holds the local lock. */ lockdep_assert_held(&ci->lock); + if (!(si->flags & SWP_SOLIDSTATE)) + lockdep_assert_held(&si->global_cluster_lock); lockdep_assert_held(&this_cpu_ptr(&percpu_swap_cluster)->lock); =20 /* The cluster must be free and was just isolated from the free list. */ --=20 2.43.0