From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=220.197.31.3 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796352332; b=Gl3vC8C4MG7qdhi/GgmWzBqxQRfCa9azrJJ5CPBxs3QDyIMBy3qvdYXaLSyp9iaByTnU JXlJNQei2bFbaV2rb9785oYpqMKAaa9c7+GnO76by6rncSrBdNYVf1x9TJYyvxCfz2OgU 7p38Pl8ek/7GoRNhmy3bk1ksrtvs+w6AXCFSRllOL+fGwr9+94C1WTlo/QSINOBwqW4BY UItgyD/I9F2jZxnT04B8EQlhqEXKw4Ca51jD9C5ZA22ibzDqrghSAJsb9wZEnvXD/Uffw S+QudDQVGuFe3VpRovCqE3VCjsXsElhQPDyiZy6Fvw5htdn1WNHpRiGWgiBZIMq3Y2g== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796352332; h=DMARC-Filter:DKIM-Signature:Received:From:To:Cc:Subject:Date: Message-ID:X-Mailer:MIME-Version:Content-Transfer-Encoding: X-CM-TRANSID:X-Coremail-Antispam:X-CM-SenderInfo; bh=r+NJaMoApsomknJMZRMJgGupbWOfZvFOPCg3MXkqeRQ=; b=ZvsLYFJTD4UR3s+qZgw/aAB6ySGNmGp2L/Rem0HR7Kn+j34LoPB5+SmLFGuT9QnMfb0y gTKvr6KtQC1TpjO2brOvrOBVaXcYhVgke5iakbq/GP9bySjxiXeAr7do2zU5iKPCS1Wo3 JfqmT/e8qkzL8az0NYXdNJ+pCzQpCQxiHNkcgNCEIUwp/0+81fM31PuZSI8RcBpYiWbvs 3OEnWK+R7xMV6gKtLA8UAsA36WkV8R5RFPX7Cz8ogfsLu9LFh2/MpWfWN1+WReq8E+Tub 9BG1s3rC2xrn5+XnSG7OB+o+zOt9Jghdam5vDM2AiM8iEBYmEzHWQv84CAQ6cwl7OHQ== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=163.com; spf=pass smtp.mailfrom=163.com; dkim=pass header.d=163.com header.i=@163.com header.a=rsa-sha256 header.s=s110527 header.b=CeISsUqS; arc=none smtp.remote-ip=220.197.31.3 Received: from m16.mail.163.com (m16.mail.163.com [220.197.31.3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 8D6AB606A322; Fri, 4 Dec 2026 02:45:29 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 8D6AB606A322 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=163.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=163.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=163.com header.i=@163.com header.a=rsa-sha256 header.s=s110527 header.b=CeISsUqS DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=163.com; s=s110527; h=From:To:Subject:Date:Message-ID:MIME-Version; bh=r+ NJaMoApsomknJMZRMJgGupbWOfZvFOPCg3MXkqeRQ=; b=CeISsUqScUO+4s26E/ 76MVQJ/SsPeQxsAktyyvggtQneH0O5RrRAOXbC421iFdzWtpzcASjz6jhgRuZYUs zfT2r4InUUbQLYKHFcHT0RMYQ6bTEUHy/M6TiSTPqQSH98dhIGxjGUQzo6ua/jcp Z2cy3kN83cq5rgiBM7vqKSOUE= Received: from localhost.localdomain (unknown []) by gzsmtp4 (Coremail) with SMTP id PygvCgD3n6YhKRJrk9k_Jw--.2717S2; Fri, 04 Dec 2026 10:44:50 +0800 (CST) From: albin_yang@163.com To: akpm@linux-foundation.org, chrisl@kernel.org, kasong@tencent.com Cc: nphamcs@gmail.com, baoquan.he@linux.dev, baohua@kernel.org, youngjun.park@lge.com, linux-mm@kvack.org, linux-kernel@vger.kernel.org, albin_yang@163.com, albinwyang@tencent.com, stable@vger.kernel.org Subject: [PATCH v2] mm/swap: skip devices without SWP_PAGE_DISCARD in swap_sync_discard() Date: Fri, 4 Dec 2026 10:44:46 +0800 Message-ID: <20261204024446.3612318-1-albin_yang@163.com> X-Mailer: git-send-email 2.43.7 X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-CM-TRANSID:PygvCgD3n6YhKRJrk9k_Jw--.2717S2 X-Coremail-Antispam: 1Uf129KBjvJXoWxXF4fAryDCFy5Zw47XryxKrg_yoW5Zw4fpr n0gasYka15WrySkw4Uur4xWryru3ykCF45JF4UGw4avw1xGr13KrWvk3W2qryUKrs3GFW5 Wa1vv3sFyF4jkaDanT9S1TB71UUUUU7qnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDUYxBIdaVFxhVjvjDU0xZFpf9x0pRdwI3UUUUU= X-CM-SenderInfo: pdoex0xb1d0wi6rwjhhfrp/xtbC7wOxVGsSKSNpVwAA3E From: Wei Yang swap_sync_discard() walks swap_active_head and, for every device, drops and re-takes the global swap_lock and does a get/put_swap_device() round trip. A device without SWP_PAGE_DISCARD can never make this function return true, because swap_do_scheduled_discard() is only reached under that flag. For such devices all of that work is pure overhead. Skip them without dropping swap_lock. A skipped device cannot affect ret or any global state, so folio_alloc_swap() behaves identically. Holding the lock keeps 'next' on the list (plist_del() takes the same lock), so the plist_node_empty() re-check below is unnecessary here; visited devices still unlock/relock and re-validate as before. SWP_PAGE_DISCARD is only set or cleared at swapon/swapoff time. We backported the upstream swap sync-discard changes into our 6.6-based kernel. LTP/stress-ng swap stress testing then drove swap 100% full and triggered a hung_task panic: the vmcore showed 17 of 256 CPUs spinning on swap_lock here, with 25 active devices and none carrying SWP_PAGE_DISCARD, while the mmap_read_lock holder starved on block tags. With swap full, each failed folio_alloc_swap() on an order-0, non-swapcache folio walks all N devices, costing 1+N+N+1 global swap_lock operations plus N get/put round trips. The walk is over swap_active_head, so an empty avail list no longer lets it exit early. The same code is present in current mainline. Measured on v7.4-rc4, strictly paired on the same machine (26 x 256MB swap devices, 16 concurrent workers, 512MB MADV_PAGEOUT chunks, swap 100% full), unpatched vs patched: folio_alloc_swap avg 32.745 us -> 1.505 us (21.8x) allocations in 30s 916,734 -> 6,216,609 (6.8x) per-page 30.2 us -> 4.0 us (7.6x) spinlock slowpath/alloc 27.1 -> 1.87 (14.5x lower) The per-allocation cost used to grow linearly with the device count and is now independent of it. Semantics are unchanged: swap stayed 100% full in both runs, so allocation outcomes were identical, and swap_do_scheduled_discard() had 0 hits. A separate test with 25 no-flag devices plus one enabled with --discard=pages confirmed that flag-bearing devices still take the original path: swap_do_scheduled_discard() was called once per folio_alloc_swap() call, with no hung tasks or regression. Fixes: f9e82f99b377 ("mm/swapfile: fix list iteration when next node is removed during discard") Cc: stable@vger.kernel.org Signed-off-by: Wei Yang --- Changes in v2: - Point Fixes at f9e82f99b377 (Kairui Song) - Add Cc: stable (Kairui Song) v1: https://lore.kernel.org/r/20261128134810.3203203-1-albin_yang@163.com mm/swapfile.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/mm/swapfile.c b/mm/swapfile.c index 226230dda0aa..e553bd2c20a7 100644 --- a/mm/swapfile.c +++ b/mm/swapfile.c @@ -1509,6 +1509,8 @@ static bool swap_sync_discard(void) spin_lock(&swap_lock); start_over: plist_for_each_entry_safe(si, next, &swap_active_head, list) { + if (!(si->flags & SWP_PAGE_DISCARD)) + continue; spin_unlock(&swap_lock); if (get_swap_device_info(si)) { if (si->flags & SWP_PAGE_DISCARD) -- 2.43.7