From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 1E44ACD1288 for ; Wed, 27 Mar 2024 12:23:47 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id AEFC56B009B; Wed, 27 Mar 2024 08:23:46 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id A9FEB6B009C; Wed, 27 Mar 2024 08:23:46 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 941056B009D; Wed, 27 Mar 2024 08:23:46 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id 74C076B009B for ; Wed, 27 Mar 2024 08:23:46 -0400 (EDT) Received: from smtpin14.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id 46CC41C0B89 for ; Wed, 27 Mar 2024 12:23:46 +0000 (UTC) X-FDA: 81942735252.14.0DEEF72 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by imf29.hostedemail.com (Postfix) with ESMTP id BB087120017 for ; Wed, 27 Mar 2024 12:23:44 +0000 (UTC) Authentication-Results: imf29.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b="LeYyS/zE"; spf=pass (imf29.hostedemail.com: domain of sashal@kernel.org designates 139.178.84.217 as permitted sender) smtp.mailfrom=sashal@kernel.org; dmarc=pass (policy=none) header.from=kernel.org ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1711542224; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:references:dkim-signature; bh=EP+zebvjEDFjZW0fjfrxYPgiAkABtQwc8XHGpx7aUdM=; b=BD8YR7WbG4zMA5AZR6nkcYNuWwZsimjzyLfd3bCB7oHz6AMFtH7N17IVqz5BxKCd/k6e9a RmGpkDKD4Wcx7IQctpvFSxTDSKXkR1JC0BsTQ3v7t/iAquzrO6Gv1YRfLgGFoluay8eq/w 18Z5QD/T7d5Qtyh3FhOukdXo41eCvGA= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1711542224; a=rsa-sha256; cv=none; b=QYzbjWAyeV1nF5CawA3FLU7u0NwbCA/DZpC9NKr2K9Fl+CB/JmtdCzORhKO0XskE01RoHx qpSf37HBkTtXNKJERytruLegTyfemiQVZKBgNwZMnoe8xrpv4ERayJnlgjNM+QLTmhlBji SJj5OiQRui051qXE/sQQrHMXwdau49g= ARC-Authentication-Results: i=1; imf29.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b="LeYyS/zE"; spf=pass (imf29.hostedemail.com: domain of sashal@kernel.org designates 139.178.84.217 as permitted sender) smtp.mailfrom=sashal@kernel.org; dmarc=pass (policy=none) header.from=kernel.org Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by dfw.source.kernel.org (Postfix) with ESMTP id 18794614E2; Wed, 27 Mar 2024 12:23:44 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id F3FB5C433F1; Wed, 27 Mar 2024 12:23:42 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1711542223; bh=xm8T28oIDHMvWTt9NSn1dlKeEuIotCmc790ci7Yspc8=; h=From:To:Cc:Subject:Date:From; b=LeYyS/zEK2UIH9T1zR5TcMXEhhcqZdxS9Itq6y7s66ux9uCI3/sxpK2TTmcYat6nP 9bobyi/48zKXfpxzfFRRBcNuETqsCuIzgeZG1dJGAdrZcPPtnsi1a0aTWEFZhs5gbW T+iIjUWk33s7UHTzF0RIcvFk7VKVpyrBW5d7o2px2PisZCVd6PM/vQvMb66WtV0u+P Okjzw8jrCwmzPDzCX/gLCFjPAy9Gtow07pzZiwniVQi0bsSvFeDkBxfdxtAxNazKeo L68c/tNUGE/l1rFF5y5VKQWv+sMUlY3RaJiIwD3UN4n9zPpK5UqxKtmojHt67fzOne slGoDzHF8WYug== From: Sasha Levin To: stable@vger.kernel.org, ryan.roberts@arm.com Cc: David Hildenbrand , "Huang, Ying" , Andrew Morton , linux-mm@kvack.org, linux-kernel@vger.kernel.org Subject: FAILED: Patch "mm: swap: fix race between free_swap_and_cache() and swapoff()" failed to apply to 5.4-stable tree Date: Wed, 27 Mar 2024 08:23:41 -0400 Message-ID: <20240327122342.2838763-1-sashal@kernel.org> X-Mailer: git-send-email 2.43.0 MIME-Version: 1.0 X-Patchwork-Hint: ignore X-stable: review Content-Transfer-Encoding: 8bit X-Rspamd-Queue-Id: BB087120017 X-Rspam-User: X-Rspamd-Server: rspam11 X-Stat-Signature: 9ernaim8xkkidw33s3ybyh7kg9uik3dy X-HE-Tag: 1711542224-213933 X-HE-Meta: U2FsdGVkX1/jUU9E9mVl/FnZo8V6o4e4u5VICWuGrDLWXdkUcpdDUXORB9992mjG/PAjlHfUe3zg1png9VsqSw4dWYdD86DKmucck7OzWfgVh6pXmEoYnUto6WO0zt/11egtCxk/zZ6Ilvw6AD1AVdt3Im2qeylQDa3ZCdQs3lkfu6IsWotHSaIuiuP9jGP4iXJB+1E1ajtUr7fcUCyCZupzUjgTerYmAKDt9hbInh6eH+5N80ckj2pvNYxsfC2uK3BKoGyWkb5tdU+Q2wKf/S7z33wvwRI7ASNf4fOURImc5QPRr4TB9sKI3sXHPQGI18hds1gT8B+pPyuo5aoevZL6C40f9legvKxDw2sfbKG6r/kNDHsdhOrUIMx68zx1syRL0gUdrq+7YO22AbN6PHbD1NYlOc9O2iLPcPY5jdVx9zJ1s9ZjgmpIOZzrUHmSp2oLeRDPrRq0aRb8jKBRQYH9p903kpRzZ0RuGWNl0Y8wQJlSSYh0266GmvUFBBgDa38GXvwVA06W9vOx233EOEkEDq76EhuPupTsQreLNbsdzSmAzNyGr1u4J5iUmLMDlMYLz0/MIk2HM6E/Yr8Ht6sHM3nqH7W5xdNPH/AmpDF4nzuexj5T8weQIwoljXgEY6Nd/zEnnuwmFKErJjRQ6hJ8M5KNHHbTc6lfFsmnLIIJjP2HAtfoufNS9URFE5HEAM4yZqqPQfsYrHlvdDKn0H4oZ9qGew7bh6O+aSfgSntTYCSTyUdASena0YYToB7eOVAknq9bZetLOI33xqnNOj3ujQuWFglE4xyDvc/MuBs7hKQGgvWGd/hti7ZLUgTHWdEJ3Wj+HJ8aCqTVsekS/iVw/agnH71QC267suhuVB/kGubJHbl8khi3E4x8iQqRZnxEf9GRppyiX7UhFrR2G9WEjBbcQKtr7PSG6/EpLNhZjgLV6VPGWc2FGbR+nixWssh1ctCuqvw5jUDQHGh qpKh4Zqi Luutz X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: The patch below does not apply to the 5.4-stable tree. If someone wants it applied there, or to any other stable or longterm tree, then please email the backport, including the original git commit id to . Thanks, Sasha ------------------ original commit in Linus's tree ------------------ >From 82b1c07a0af603e3c47b906c8e991dc96f01688e Mon Sep 17 00:00:00 2001 From: Ryan Roberts Date: Wed, 6 Mar 2024 14:03:56 +0000 Subject: [PATCH] mm: swap: fix race between free_swap_and_cache() and swapoff() There was previously a theoretical window where swapoff() could run and teardown a swap_info_struct while a call to free_swap_and_cache() was running in another thread. This could cause, amongst other bad possibilities, swap_page_trans_huge_swapped() (called by free_swap_and_cache()) to access the freed memory for swap_map. This is a theoretical problem and I haven't been able to provoke it from a test case. But there has been agreement based on code review that this is possible (see link below). Fix it by using get_swap_device()/put_swap_device(), which will stall swapoff(). There was an extra check in _swap_info_get() to confirm that the swap entry was not free. This isn't present in get_swap_device() because it doesn't make sense in general due to the race between getting the reference and swapoff. So I've added an equivalent check directly in free_swap_and_cache(). Details of how to provoke one possible issue (thanks to David Hildenbrand for deriving this): --8<----- __swap_entry_free() might be the last user and result in "count == SWAP_HAS_CACHE". swapoff->try_to_unuse() will stop as soon as soon as si->inuse_pages==0. So the question is: could someone reclaim the folio and turn si->inuse_pages==0, before we completed swap_page_trans_huge_swapped(). Imagine the following: 2 MiB folio in the swapcache. Only 2 subpages are still references by swap entries. Process 1 still references subpage 0 via swap entry. Process 2 still references subpage 1 via swap entry. Process 1 quits. Calls free_swap_and_cache(). -> count == SWAP_HAS_CACHE [then, preempted in the hypervisor etc.] Process 2 quits. Calls free_swap_and_cache(). -> count == SWAP_HAS_CACHE Process 2 goes ahead, passes swap_page_trans_huge_swapped(), and calls __try_to_reclaim_swap(). __try_to_reclaim_swap()->folio_free_swap()->delete_from_swap_cache()-> put_swap_folio()->free_swap_slot()->swapcache_free_entries()-> swap_entry_free()->swap_range_free()-> ... WRITE_ONCE(si->inuse_pages, si->inuse_pages - nr_entries); What stops swapoff to succeed after process 2 reclaimed the swap cache but before process1 finished its call to swap_page_trans_huge_swapped()? --8<----- Link: https://lkml.kernel.org/r/20240306140356.3974886-1-ryan.roberts@arm.com Fixes: 7c00bafee87c ("mm/swap: free swap slots in batch") Closes: https://lore.kernel.org/linux-mm/65a66eb9-41f8-4790-8db2-0c70ea15979f@redhat.com/ Signed-off-by: Ryan Roberts Cc: David Hildenbrand Cc: "Huang, Ying" Cc: Signed-off-by: Andrew Morton --- mm/swapfile.c | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/mm/swapfile.c b/mm/swapfile.c index 2b3a2d85e350b..1155a63041192 100644 --- a/mm/swapfile.c +++ b/mm/swapfile.c @@ -1232,6 +1232,11 @@ static unsigned char __swap_entry_free_locked(struct swap_info_struct *p, * with get_swap_device() and put_swap_device(), unless the swap * functions call get/put_swap_device() by themselves. * + * Note that when only holding the PTL, swapoff might succeed immediately + * after freeing a swap entry. Therefore, immediately after + * __swap_entry_free(), the swap info might become stale and should not + * be touched without a prior get_swap_device(). + * * Check whether swap entry is valid in the swap device. If so, * return pointer to swap_info_struct, and keep the swap entry valid * via preventing the swap device from being swapoff, until @@ -1609,13 +1614,19 @@ int free_swap_and_cache(swp_entry_t entry) if (non_swap_entry(entry)) return 1; - p = _swap_info_get(entry); + p = get_swap_device(entry); if (p) { + if (WARN_ON(data_race(!p->swap_map[swp_offset(entry)]))) { + put_swap_device(p); + return 0; + } + count = __swap_entry_free(p, entry); if (count == SWAP_HAS_CACHE && !swap_page_trans_huge_swapped(p, entry)) __try_to_reclaim_swap(p, swp_offset(entry), TTRS_UNMAPPED | TTRS_FULL); + put_swap_device(p); } return p != NULL; } -- 2.43.0