From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mta1.migadu.com (out-172.mta1.migadu.com [95.215.58.172]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 0F41C3FD137 for ; Mon, 24 Aug 2026 09:50:27 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=95.215.58.172 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1787565032; cv=none; b=BaLpWS+tTbT/rhfemLSrLRmAJEpRPUtr8DNvfNmm65q7laICGwmc2zugaLmiam1gTfT4WDlGJN34o/UKQB2ZgKrw7ntAFpSJeIjQ/0AdOrehaonHKWaLbxA2mMh9oBGr9g67l4cm9jmTGKHKuUXWM+F3nSBTWzqewLGEfJWzqf0= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1787565032; c=relaxed/simple; bh=X9ufpZjmt4MTtglWZo2Xlzd7jESQtJorX9XRJ/2a3JE=; h=From:To:Cc:Subject:Date:Message-Id:MIME-Version; b=fp5vu0YhPyCiI0/vioxin5waY6tNcbXtPTFC2Nj5q9vtkARP8FQsxv0Dzd6OV37Yb9gQEKlkIPlZct4MjBIW6kdkqgJ9VGrEl1hEiGsIiVEwqbDRGVCT8mqb/JYLUpxhbVHKWnNpduuRWVuP59qB8DuhuN4SwkWvkRqulM1hcIw= 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=cHxQvXr/; arc=none smtp.client-ip=95.215.58.172 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="cHxQvXr/" X-Envelope-To: linux-kernel@vger.kernel.org DKIM-Signature: a=rsa-sha256; bh=X9ufpZjmt4MTtglWZo2Xlzd7jESQtJorX9XRJ/2a3JE=; c=simple/simple; d=linux.dev; h=from:to:subject:date:message-id:mime-version:content-type; s=key1; t=1787565025; v=1; x=1788169825; b=cHxQvXr/m7dDgUj1iUfWadKl8lsgOinpRGrVII/lxOswxHoX/pnYUwqiJDPpUbpW1HZtm93h gt5kcViKvLZoZEvwXvafO2Wc4tFP8ANMk2FC4qRUITY07NYYdfradzUD13TbxlA9JfdZqAd4AO0 7EtibDLMUOk9EVtsqBUNld+I= X-Envelope-To: linux-kernel@vger.kernel.org Received: from localhost.localdomain (39.156.73.11) by smtp.migadu.com with ESMTPS id 0f92cbfa78e388f7; Mon, 24 Aug 2026 09:50:24 +0000 X-Mizu-Trace-ID: 0f92cbfa78e388f7 X-Migadu-Flow: FLOW_OUT From: Ye Liu To: Andrew Morton , Uladzislau Rezki Cc: Ye Liu , linux-mm@kvack.org, linux-kernel@vger.kernel.org Subject: [PATCH] mm/vmalloc: fix vmap_purge_lock livelock under memory pressure Date: Mon, 24 Aug 2026 17:50:19 +0800 Message-Id: <20260824095020.1225189-1-ye.liu@linux.dev> X-Mailer: git-send-email 2.25.1 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit From: Ye Liu The vmap_purge_lock mutex can be held for an extended period by __purge_vmap_area_lazy() which calls flush_work() to wait for purge_vmap_node workers while holding the lock. Under memory pressure, those workers may themselves be blocked in direct reclaim trying to acquire the same lock via the vmap_node_shrink_scan() shrinker callback, creating a circular dependency that deadlocks the entire system. Two changes: 1. vmap_node_shrink_scan(): replace blocking guard(mutex) with mutex_trylock(). This is a shrinker that only decays the vmap pool and returns SHRINK_STOP without freeing memory; skipping a decay cycle when the lock is contended is harmless and prevents tasks from piling up on the mutex in the direct reclaim path. 2. __purge_vmap_area_lazy(): purge all vmap nodes inline instead of scheduling purge_vmap_node via workqueue and calling flush_work() while holding vmap_purge_lock. The workqueue dispatch + flush pattern under a mutex is the deadlock trigger: if no free worker threads are available (all blocked on the same lock), flush_work() never returns and the lock is held indefinitely. Fixes: 7679ba6b36db ("mm: vmalloc: add a shrinker to drain vmap pools") Signed-off-by: Ye Liu --- mm/vmalloc.c | 51 +++++++++++++++++++++------------------------------ 1 file changed, 21 insertions(+), 30 deletions(-) diff --git a/mm/vmalloc.c b/mm/vmalloc.c index bea9f76ed7e7..41443708d22d 100644 --- a/mm/vmalloc.c +++ b/mm/vmalloc.c @@ -2358,7 +2358,6 @@ static bool __purge_vmap_area_lazy(unsigned long start, unsigned long end, bool full_pool_decay) { unsigned long nr_purged_areas = 0; - unsigned int nr_purge_helpers; static cpumask_t purge_nodes; unsigned int nr_purge_nodes; struct vmap_node *vn; @@ -2397,36 +2396,17 @@ static bool __purge_vmap_area_lazy(unsigned long start, unsigned long end, if (nr_purge_nodes > 0) { flush_tlb_kernel_range(start, end); - /* One extra worker is per a lazy_max_pages() full set minus one. */ - nr_purge_helpers = atomic_long_read(&vmap_lazy_nr) / lazy_max_pages(); - nr_purge_helpers = clamp(nr_purge_helpers, 1U, nr_purge_nodes) - 1; - - for_each_cpu(i, &purge_nodes) { - vn = &vmap_nodes[i]; - - if (nr_purge_helpers > 0) { - INIT_WORK(&vn->purge_work, purge_vmap_node); - - if (cpumask_test_cpu(i, cpu_online_mask)) - schedule_work_on(i, &vn->purge_work); - else - schedule_work(&vn->purge_work); - - nr_purge_helpers--; - } else { - vn->purge_work.func = NULL; - purge_vmap_node(&vn->purge_work); - nr_purged_areas += vn->nr_purged; - } - } - + /* + * Purge all nodes inline. Do not schedule_work() and + * flush_work() here: flush_work() while holding + * vmap_purge_lock can deadlock if the worker pool is + * starved (e.g. all workers blocked on this same lock + * in the direct reclaim path via vmap_node_shrink_scan). + */ for_each_cpu(i, &purge_nodes) { vn = &vmap_nodes[i]; - - if (vn->purge_work.func) { - flush_work(&vn->purge_work); - nr_purged_areas += vn->nr_purged; - } + purge_vmap_node(&vn->purge_work); + nr_purged_areas += vn->nr_purged; } } @@ -5519,10 +5499,21 @@ vmap_node_shrink_scan(struct shrinker *shrink, struct shrink_control *sc) { struct vmap_node *vn; - guard(mutex)(&vmap_purge_lock); + /* + * This shrinker is invoked from direct reclaim path where memory + * pressure is already high. Blocking on vmap_purge_lock here can + * cause a pile-up of tasks all waiting for the same mutex while + * the lock holder may itself be blocked in flush_work() waiting for + * a worker that is stuck in the same reclaim path. Use trylock to + * avoid this; skipping a pool decay cycle is harmless. + */ + if (!mutex_trylock(&vmap_purge_lock)) + return SHRINK_STOP; + for_each_vmap_node(vn) decay_va_pool_node(vn, true); + mutex_unlock(&vmap_purge_lock); return SHRINK_STOP; } -- 2.25.1