From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1795947139; b=fMd1zCvkOfF0oHV5OISYb9kLdGzEzFYw4n62gJnGF0dBR1SV44G7vxQyvSDqEZT7CXuS 4iq3lDfsoKCnrvgcDSaYIr2jbZ6u2COopip5ojqWAI6nStYu8zHYJAMJdkrWgrxQGcFks P7C3CjQcYXh+FcmU9TwgI9IyPoojp22dtwnbL3A1WoCLlT28ZJWDMabpBySuMWPSoSySI QGgSJepzFKzalSVbUkFzjvXYNFWUPUteYuHQ6YJHps7vgzoGo5AIfQkUqwEOImvo1303a ZtcnsGU6mgAZRGKKmRcIQaPsRg1NdqSmvCZhle5Lq73YhvzqxHEg6cnwAxIV8XmeRbg== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795947139; h=Received:DKIM-Signature:From:Date:Subject:MIME-Version:Content-Type: Content-Transfer-Encoding:Message-Id:References:In-Reply-To:To:Cc: X-Mailer:X-Developer-Signature:X-Developer-Key; bh=onmcO1nQTjcazgBFRETnpIKx2lkY7XUZF0YchcSTSTc=; b=O+zIHC/ouqbL1HkhBGB/hXmbM+19Gs5oezYNoqVVQlD/83EczOmIs3UW3P62YaY3Sidp N98WirfgvWIOBU85e5FJLn8QzoG74Cjap6nsLNsoRk7fHcJXcuM9WG9F1SJOPoAcMr6bD EiU2sBXUiR3ne+e3n6Cpm4pTBv46vFeQ7TDW4hguaj9xSNVXVh1Yj9x5OfCRCIC/VDL3m 0j3EUCokOlqz6JllCZWxO6yZIgxZoZFPxI+lPClVbtvnb3rGWExpVSHeWehoTJ5TxjW9f BYgQlXpQ2HaCELaFE36l85nyuzAwukZcIqE9Nvbe7ENVh7uZDKKP+soCuOo3eTK20Wg== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=Vwtx+0aL; arc=none smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id BE01B606A323; Sun, 29 Nov 2026 10:12:19 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=Vwtx+0aL Received: by smtp.kernel.org (Postfix) with ESMTPSA id 48B381F000C1; Sun, 29 Nov 2026 10:12:01 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1795947139; bh=onmcO1nQTjcazgBFRETnpIKx2lkY7XUZF0YchcSTSTc=; h=From:Date:Subject:References:In-Reply-To:To:Cc; b=Vwtx+0aLXsRdjaPXPz1RiasgY3HyTdVsTkRSnF9qcTQLIQJZfyF/hl+gSFlbgrSzo 77fSDqtwv6JOr58MRKd+GkB9K763XFLQBjCnKubWp2Qn1x+jQ1u5lAZkUbPbBvAIb/ wGIMQSaE1QOZxsc4w6wsObt3DPc9SZxWQ79okGpkrjr3FYz+PhtIuryZdYfzAC827O UfBdl/tZ8JIMaO5Fblsv1U2F0qH2GFH1/1vYUX/kL1it+mnloPwBOEuzIpJsq1gvPz LeJ4eAAFKtVp1XkiqwORDs/tIHSAZl1F3y4MZpNZ7cQR8UYowlYfF/Lfa628KAqnUt FMAcm79rYxxHA== From: "David Hildenbrand (Arm)" Date: Sun, 29 Nov 2026 11:06:48 +0100 Subject: [PATCH 17/32] mm/memory: drop the swap device ref before do_wp_page() 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-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit Message-Id: <20261129-fixes_core_3-v1-17-f66094116801@kernel.org> References: <20261129-fixes_core_3-v1-0-f66094116801@kernel.org> In-Reply-To: <20261129-fixes_core_3-v1-0-f66094116801@kernel.org> To: Hugh Dickins , Baolin Wang , Andrew Morton , Mike Rapoport , Peter Xu , Michal Hocko , Kiryl Shutsemau , Manfred Spraul , Davidlohr Bueso , Cyrill Gorcunov , Zi Yan , "Liam R. Howlett" , Nico Pache , Ryan Roberts , Dev Jain , Barry Song , Lance Yang , Usama Arif , Lorenzo Stoakes , Rik van Riel , Vlastimil Babka , Harry Yoo , Jann Horn , Yang Shi , Zach O'Keefe , Song Liu , David Stevens , Kefeng Wang , Alex Williamson , Suren Baghdasaryan , Dave Jiang , Matthew Wilcox , Miaohe Lin , David Rientjes , Shakeel Butt , Jan Kara , Mel Gorman , Jason Gunthorpe , John Hubbard , Gerd Hoffmann , Vivek Kasireddy , Dave Airlie , Matthew Brost , Joshua Hahn , Byungchul Park , Gregory Price , Ying Huang , Alistair Popple , Hagen Paul Pfeifer , James Bottomley , Felix Kuehling , Alex Sierra , Balbir Singh , Kairui Song , Qi Zheng , Axel Rasmussen , Yuanchu Xie , Wei Xu , Baoquan He , Oscar Salvador , Muchun Song , Dan Williams , Rakie Kim , Hao Li , Christoph Lameter , Roman Gushchin , Sebastian Andrzej Siewior , Clark Williams , Steven Rostedt , Alexei Starovoitov , Xu Xin , Chengming Zhou , Shivank Garg Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-cxl@vger.kernel.org, linux-rt-devel@lists.linux.dev, SJ Park , "David Hildenbrand (Arm)" , Chris Mason , stable@vger.kernel.org X-Mailer: b4 0.16.0 X-Developer-Signature: v=1; a=openpgp-sha256; l=2058; i=david@kernel.org; h=from:subject:message-id; bh=MTLGBXanUSVC3PzrdOQiV4ZpcxuUxV4/HQOR484hz6U=; b=owGbwMvMwCF2/OVpCdfQ3ZWMp9WSGLK5f+oW9W03lHv+x/msTtR3Vq0P53N/rDt+b/LBWlfNm 32/Qip1O0pZGMQ4GGTFFFmCgu8bvHy5rrbajY8ZZg4rE8gQBi5OAZjITS2G/56NeU1zFIwlZ6cx RR/qWzcljC92vkmnW4+pSvK9Q1snpTIyzKhn+6qq9Fzx4cXttnwvy64LJjfujniZP4FHQCJ75t3 HzAA= X-Developer-Key: i=david@kernel.org; a=openpgp; fpr=1BD9CAAD735C4C3A460DFCCA4DDE10F700FF835A From: Chris Mason swapoff kills si->users and waits for the last put_swap_device() with wait_for_completion(). do_swap_page() currently holds its reference to the swap device until it returns to the caller. If it ends up calling into do_wp_page() and triggers uffd-wp, we will sleep in handle_userfault() until the user space handler has processed the event and woken up the thread: do_swap_page() swapoff ============== ======= get_swap_device() set_ptes() folio_unlock() do_wp_page() handle_userfault() schedule() try_to_unuse() returns 0 percpu_ref_kill(&p->users) wait_for_completion(&p->comp) put_swap_device() Consequently, swapoff hangs until the user space handler resolves the fault. Let's fix it by dropping the swap device reference before calling do_wp_page(). This has medium urgency, as an unprivileged process can make swapoff hang for as long as it likes, using a UFFD_USER_MODE_ONLY userfaultfd. Fixes: 2799e77529c2 ("swap: fix do_swap_page() race with swapoff") Cc: stable@vger.kernel.org Signed-off-by: Chris Mason [ david: minor code cleanups, adjust comment and description ] Signed-off-by: David Hildenbrand (Arm) --- mm/memory.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/mm/memory.c b/mm/memory.c index 1bc450cc359ca..7b0aeb102eff7 100644 --- a/mm/memory.c +++ b/mm/memory.c @@ -5390,10 +5390,12 @@ vm_fault_t do_swap_page(struct vm_fault *vmf) * drop the marker for a non-exclusive page. */ if ((vmf->flags & FAULT_FLAG_WRITE) && !pte_write(pte) && !rwp_restore) { + /* do_wp_page() may wait for uffd, clean up first. */ + put_swap_device(si); ret |= do_wp_page(vmf); if (ret & VM_FAULT_ERROR) ret &= VM_FAULT_ERROR; - goto out; + return ret; } /* No need to invalidate - it was non-present before */ -- 2.56.1