From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from out-185.mta0.migadu.com (out-185.mta0.migadu.com [91.218.175.185]) (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 0C8E13E6DD5 for ; Wed, 5 Aug 2026 07:55:34 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=91.218.175.185 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1785916538; cv=none; b=J37qPbhDSTCKw4bk87ZcBcptwhZcmUGz6BX8MxhLZo53A112yVEm8jvy8k/TfCsIm0YhLNf1OU6FmZmMfrXf+GWeBm2UsIQEikwMZdendkGGLKcoOnVUW6TqA7R7OLkZXQsHkBQeaj4v6vlBtVxiwiI5yN4HOKoUkDhaeUeJPMU= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1785916538; c=relaxed/simple; bh=UNs7LmYi40SjWRCbHikpB3XszHaogZTJOoVSP3fg/D0=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type:Content-type; b=Qx2o6UQ5TrClLZ/O2rV7ZmauOD169n0O3QRf8a+PCVkAZriwZmeN+9/YmL2emkxOQn+Yw5vxY8h+PALSnmcGnIxFXoDBAyWKGkH82Xi8X0R6VK0992AzbiyINNI0RcR45aWwndNEjeyJt1LQX1V4WaHB8l2AF3Ro02IPx16mBY0= 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=GLgVwNRU; arc=none smtp.client-ip=91.218.175.185 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 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b="GLgVwNRU" 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=1785916532; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-type:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=EbfFddbqSLBXoL9Z6NQe5+gvAdUEfreYVifyxE81zg0=; b=GLgVwNRUwiBcSuY7U3D9LcolFHX5qp94+NHSDFNCWLyiNOSj/IDuA2OtulWLjCHMnDp0FP GBcfFr+2W4RrKIkXygjMqYPSkVmDYXphGEoyWSvWVXWVH/H8YIKDRPjIo6WiWBaP2iyTrV FpdB/Tm+iQCJEZO94Gpyn5ghqq5yloA= From: Baoquan He To: linux-mm@kvack.org Cc: chrisl@kernel.org, nphamcs@gmail.com, kasong@tencent.com, baohua@kernel.org, youngjun.park@lge.com, hannes@cmpxchg.org, yosry@kernel.org, david@kernel.org, shikemeng@huaweicloud.com, chengming.zhou@linux.dev, linux-kernel@vger.kernel.org, Baoquan He Subject: [RFC PATCH v2 10/10] mm, swap: defer xswap shrink to workqueue to avoid lock recursion Date: Wed, 5 Aug 2026 15:53:33 +0800 Message-ID: <20260805075336.3579395-11-baoquan.he@linux.dev> In-Reply-To: <20260805075336.3579395-1-baoquan.he@linux.dev> References: <20260805075336.3579395-1-baoquan.he@linux.dev> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-type: text/plain Content-Transfer-Encoding: 8bit X-Migadu-Flow: FLOW_OUT xswap_try_shrink() was called directly from __free_cluster() while holding ci->lock. The shrink path calls xswap_unmap_clusters() which unmaps vmalloc pages backing cluster_info, and on return swap_cache_del_folio() tries swap_cluster_unlock(ci) on the now- unmapped address — crashing on a not-present page. Replace direct calls with schedule_work() so shrink runs in an independent workqueue context where no cluster locks are held. Use cancel_work_sync() during swapoff to ensure no pending shrink work races with the VM area teardown. Signed-off-by: Baoquan He --- include/linux/swap.h | 1 + mm/swapfile.c | 19 +++++++++++++------ 2 files changed, 14 insertions(+), 6 deletions(-) diff --git a/include/linux/swap.h b/include/linux/swap.h index 4f4583f9a4e5..a5b323374769 100644 --- a/include/linux/swap.h +++ b/include/linux/swap.h @@ -255,6 +255,7 @@ struct swap_info_struct { unsigned long nr_clusters_mapped; /* currently mapped cluster count */ unsigned long nr_free_tail; /* contiguous free clusters at tail */ struct dentry *debugfs_entry; /* debugfs: type_max_clusters */ + struct work_struct xswap_shrink_work; /* deferred shrink trigger */ struct mutex xswap_lock; /* serialize map/unmap operations */ #endif struct list_head free_clusters; /* free clusters list */ diff --git a/mm/swapfile.c b/mm/swapfile.c index 265f2bac1a12..ca406556626e 100644 --- a/mm/swapfile.c +++ b/mm/swapfile.c @@ -127,11 +127,8 @@ static ssize_t xswap_max_clusters_write(struct file *file, spin_unlock(&swap_lock); } - /* - * Lowering the ceiling may make tail clusters eligible for - * shrinking. Trigger an immediate check. - */ - xswap_try_shrink(si); + /* Shrink trigger: lowering the ceiling may free tail clusters. */ + schedule_work(&si->xswap_shrink_work); return count; } @@ -726,7 +723,7 @@ static void __free_cluster(struct swap_info_struct *si, struct swap_cluster_info ci->order = 0; #ifdef CONFIG_XSWAP xswap_update_free_tail(si, ci - si->cluster_info); - xswap_try_shrink(si); + schedule_work(&si->xswap_shrink_work); #endif } @@ -3236,6 +3233,7 @@ static void free_swap_cluster_info(struct swap_info_struct *si) #ifdef CONFIG_XSWAP if (si->flags & SWP_XSWAP) { xswap_debugfs_del(si); + cancel_work_sync(&si->xswap_shrink_work); /* Unmap all mapped clusters and free the VM_SPARSE area */ if (si->nr_clusters_mapped > 0) xswap_unmap_clusters(si, 0, si->nr_clusters_mapped); @@ -4057,6 +4055,13 @@ static void xswap_trim_free_tail(struct swap_info_struct *si, unsigned long idx) WRITE_ONCE(si->nr_free_tail, nr_mapped - idx - 1); } +static void xswap_shrink_work_fn(struct work_struct *work) +{ + struct swap_info_struct *si = container_of(work, + struct swap_info_struct, xswap_shrink_work); + xswap_try_shrink(si); +} + /* * Try to shrink the cluster_info tail. Uses si->nr_free_tail which * is maintained incrementally during alloc/free — no scanning needed. @@ -4163,7 +4168,9 @@ static int setup_swap_clusters_info(struct swap_info_struct *si, /* All mapped clusters except cluster 0 are free at the tail */ si->nr_free_tail = si->nr_clusters_mapped - 1; + mutex_init(&si->xswap_lock); + INIT_WORK(&si->xswap_shrink_work, xswap_shrink_work_fn); xswap_debugfs_add(si); return 0; -- 2.54.0