From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from out-174.mta0.migadu.com (out-174.mta0.migadu.com [91.218.175.174]) (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 06B9030214B for ; Mon, 27 Jul 2026 14:06:39 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=91.218.175.174 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1785161201; cv=none; b=SdW4xhXzHV7z2EFu76L0xCa2HsW6IOYvRbeiE2E/WDgi6UYhxk1XE+vKDQ67sEuxNMRdpWf4SMpp3NFvCSPiPtAr42fJ1PdYdrL/Nrwliv9JJasjAR4RMhfbaxr/FSmUpIrGD/L0iqEev4FP0rjQ/979NCk6iYgEUhvM3Dd+m48= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1785161201; c=relaxed/simple; bh=48dw+tMQ45ub2O70fILyZ5jpNjuekuEBp+MP77HQtjI=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type:Content-type; b=KtIU3rlwKbN18QqQBpshjmLAectBGvE6FBFDDrM792pTB3qYa/L293m2bG6gnVF97e7mk+jHlUZAj9Z58GVs1hTMlKb4xq8xAZgs/ZU49JRB7j6IIB5mofrdKaYQBvRgeScLWKKjmD/JzwQPZZ72fQ8w4ap3GESitrj+k9ww308= 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=Ol4EjBnM; arc=none smtp.client-ip=91.218.175.174 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="Ol4EjBnM" 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=1785161197; 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=4yGYb4251xen6xq86IV6E6mB7obBsI6jDmsaNtiaKQw=; b=Ol4EjBnMG3jAVqaTNBrQNftF0n45q6z3fLWxqc8Mp8aara4iUzUCTVxMThAlxS640iHEHw LP1XunNzgeje1Er9j984M+KsfbsdxP1b0NUYKLD0QOxDCzu+1dV73d5Hl3AjLjxIQvdlJ8 X42wigqlOFa6/7nrIdgszuh1/qKrRHw= From: Baoquan He To: linux-mm@kvack.org Cc: akpm@linux-foundation.org, 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 10/11] mm, swap: defer xswap shrink to workqueue to avoid lock recursion Date: Mon, 27 Jul 2026 22:05:01 +0800 Message-ID: <20260727140503.1060918-9-baoquan.he@linux.dev> In-Reply-To: <20260727140503.1060918-1-baoquan.he@linux.dev> References: <20260727135029.1059441-1-baoquan.he@linux.dev> <20260727140503.1060918-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 | 18 ++++++++++++------ 2 files changed, 13 insertions(+), 6 deletions(-) diff --git a/include/linux/swap.h b/include/linux/swap.h index ebcd5eaa7924..ebae01fbd100 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 */ #endif struct list_head free_clusters; /* free clusters list */ struct list_head full_clusters; /* full clusters list */ diff --git a/mm/swapfile.c b/mm/swapfile.c index af62981506d3..b41ff2c594fc 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); @@ -4030,6 +4028,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. @@ -4137,6 +4142,7 @@ 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; + INIT_WORK(&si->xswap_shrink_work, xswap_shrink_work_fn); xswap_debugfs_add(si); return 0; -- 2.54.0