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 B5E88C54E67 for ; Wed, 27 Mar 2024 12:27:41 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 4B2326B009E; Wed, 27 Mar 2024 08:27:41 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 4622F6B009F; Wed, 27 Mar 2024 08:27:41 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 32A036B00A0; Wed, 27 Mar 2024 08:27:41 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id 131616B009E for ; Wed, 27 Mar 2024 08:27:41 -0400 (EDT) Received: from smtpin26.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id D3808A1243 for ; Wed, 27 Mar 2024 12:27:40 +0000 (UTC) X-FDA: 81942745080.26.A0D183E Received: from sin.source.kernel.org (sin.source.kernel.org [145.40.73.55]) by imf10.hostedemail.com (Postfix) with ESMTP id AEFF9C0002 for ; Wed, 27 Mar 2024 12:27:38 +0000 (UTC) Authentication-Results: imf10.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=HGcCWvIo; spf=pass (imf10.hostedemail.com: domain of sashal@kernel.org designates 145.40.73.55 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=1711542459; 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=/35YK6W8o25x5W05QSnmbfniMk8ubyPHBWKo8zesyzs=; b=7cmKnoW/jF//KB6VLhJ+753hrHb412bk/svVyb35G/UJ3GWmyi7Mlmg9Io+0JrIc3FcfLW lWpY5KXzaXjuLNZfUPdu6U/fTfEYaLUYNOKYOBtb4hIi8/uB6lP42gXUfYemPdADpcJpvV q/au7Bci+6BcN8SzG2lNqrPemV1zlh8= ARC-Authentication-Results: i=1; imf10.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=HGcCWvIo; spf=pass (imf10.hostedemail.com: domain of sashal@kernel.org designates 145.40.73.55 as permitted sender) smtp.mailfrom=sashal@kernel.org; dmarc=pass (policy=none) header.from=kernel.org ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1711542459; a=rsa-sha256; cv=none; b=H4/zB13k2CiBB7JkxrDUfb+GdfoR1mFWKB3MvAxPOJSTRauAhdbMKBovob/gCfAPn2RN6k lhDidopPmXKB4l6BAMSBvLCUF0vmXCM8sg6HSuwbua2FEtjB2J0TOxlRfFvtjrcF6FkOcn 8jEF4EAFPFfvf8rDcYvpRnHV+Sy7WPk= Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by sin.source.kernel.org (Postfix) with ESMTP id 1AE85CE25AF; Wed, 27 Mar 2024 12:27:34 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 84744C433F1; Wed, 27 Mar 2024 12:27:32 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1711542453; bh=GTR/FjozN56wOnP8zjD8VcJ2o2Xshht3KHG7+7Y3jXg=; h=From:To:Cc:Subject:Date:From; b=HGcCWvIoD5zrY494qT576iQfzO+xFXtKNk0hl5opNJYVIzrdQ27vgxVgKWu19Bh1Z XRsQm0wN5hxWo98/McNObfnNCbo48tTMUoPW7TK673MsRsj3AEGPPSVgS6JOt3tzw1 rwUCMKJmHsaCi1K5iTBmQ+OpV5nQKbzD0hZc5Qu0HhcG/r8VHJ7x7v4UBOpg+UG1qb 5a6qSbUcLb6IhLr0iB1+V4CgDoV43Xzxkb/JHS/v6g2fOyW0e0KXJPUPJt4KJb70Zf KgTeSckmlzxA58drxE558T+v2PAkufSu4gndb+DskZ+U0yqf4SyxDApzTaKvC7DwFt tN3udUodpiBDw== 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 4.19-stable tree Date: Wed, 27 Mar 2024 08:27:31 -0400 Message-ID: <20240327122731.2841985-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: AEFF9C0002 X-Rspam-User: X-Stat-Signature: i51kff3wer9s9zppp5oeomcjce8uhurp X-Rspamd-Server: rspam01 X-HE-Tag: 1711542458-955136 X-HE-Meta: U2FsdGVkX1+U3arSqaQdooLJxVvANekqjgbehdYb5gYZGgH3IgSF4mqgSasWz1QAU9FhmDmUp6NFTb8KVyR6ojg2/ZODTm9FDkk1K3HvnZni6em81bcnHEdSypsIgkWC1CH9RRlXxwtVXMXJG/6CX7I+SsJEWzjxTnHc8aCmRDKdkbc0DrcvbFN7JfgwucgjIg1pRFGEMMBl7UqD1ILdFJPUv6nrRa2FcmdSOwW/d6LONJAQzkt23qG8aqmzZD+fzf+JDCcIXoJ5VlDTFKl/w3XiSXRxG1seltOTH17Em8zblLMW0Vy2nnkZnU6MDAObI+J31qwnQEhphn1wM4tucDqWgBzJdsxv9LXTPYxZGpMyLTetZeT/4ee1phR5AADYKtdw4Nf22tPmzXFAoOfZdptnaenughTyO/fHrY/RSsU17Inyt3UzyKonywOpxV64xx7GcVSqLXh2wBj8hlMzS4P6wxj1XwtvEXIOOUZSnwNspeZLwZo4kQh8CZ9CoPirkYjr6iGL0yxokztlipEZo9gvnE4Mp8a5UybSl3azqdk/O+SOkdHeG97ZeZbsCiN2PWOaYMNwO4a1C+MR9ExGCyeWYEbbDnz0qeCvYObjJ+Q4ImLkqn/NfdKBIB+fV2Kr9LFEgeKdbBLsfmjZvnBKZbLfqvrk8vO/bkXsAlCI+eIMfuVGqJhrk9iucuRY1PWxM7qZROlrbvxaRPUiA87l2ZkwkXor61XflqbbTMFoYaqByEsptA7Qwa2IbpllS6I6W0x7XhZubGnAxmvBE6WzR2wGFxV5DXrGlZGm8puONmmO9x68nVve2kCErs7uFhHeZjQmiAQHYtywkmcoNZ80kJcI/j7mleXIAhNieO4PSKFOC58mP0ZgYzVL9QeIMmdy/N0mgJX2jmU/JAlaXJR/6Eejj3p0qiuRpdkLSAG4iyS/34LmE3GhVmBccNBd98UyGLJQ5PJhm+sy+lgYaLl BJeTu+Ib Yf5GV 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 4.19-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