From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-alma10-1.taild15c8.ts.net [100.103.45.18]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id D0F0544238C; Fri, 7 Aug 2026 15:05:35 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=100.103.45.18 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1786115137; cv=none; b=k9tufFN33uvsMPCD2G+lfxgah7CBJn9WW3y5KpzQqYwxUcGxuFAl55uB+xdd2L3dSYVvBXttX6zU57c+JO080rZm2y60iVDCxI/LY+YADkP2IRLJ/7RLa7m6TeLQbHJutgvymS7+I1QMWn38/uLWspIQpeAldo26CklU9cN6zQM= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1786115137; c=relaxed/simple; bh=EIhW9D8wL76oYug07aeu9XgkKN0jJCVneZkq4IL3wzg=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=ePMNq+SMp0kAMArG8loQhr0jASHBBS+gMfcLJzka/aARgUuVtlrFggHR6AkY8UMt2Ohy6BN2nrWQXC6BSkL8QWYQGAzKVSQ5OmloAtCvIw51GT20EWLN/l/DhLT5KQL78RnWfxn57WjYza7EPNc7fRmFaCB9SXBLlLH4jRJaluI= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linuxfoundation.org header.i=@linuxfoundation.org header.b=OVUROxfK; arc=none smtp.client-ip=100.103.45.18 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linuxfoundation.org header.i=@linuxfoundation.org header.b="OVUROxfK" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 30F1D1F000E9; Fri, 7 Aug 2026 15:05:35 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linuxfoundation.org; s=korg; t=1786115135; bh=Jx1VAd5oo+lLj653xamLuUcWpjOnqP6GJluGDXbd/t0=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=OVUROxfK/KJdxRlcQXPZvROweSPpcvfxb44A/CLdiFonvKmG1ue6JafMejlWLp3ka gQ78HJdiFbzBOpqkdIXw56cOI1jnzlMt0kK0Fn0vSe34X2DxV3WCoF0nQnx4ngsIc4 4tz/uv2XddJEePPr6+UAtr39crsH5mOuZnNblmBQ= From: Greg Kroah-Hartman To: stable@vger.kernel.org Cc: Greg Kroah-Hartman , patches@lists.linux.dev, Kefeng Wang , Balbir Singh , Zi Yan , Alistair Popple , Byungchul Park , David Hildenbrand , Gregory Price , "Huang, Ying" , Joshua Hahn , Matthew Brost , Rakie Kim , Andrew Morton Subject: [PATCH 6.18 169/396] mm: migrate_device: fix pte_pfn/pte_dirty called on non-present PTE Date: Fri, 7 Aug 2026 16:35:29 +0200 Message-ID: <20260807143427.946521376@linuxfoundation.org> X-Mailer: git-send-email 2.55.0 In-Reply-To: <20260807143424.272339768@linuxfoundation.org> References: <20260807143424.272339768@linuxfoundation.org> User-Agent: quilt/0.69 X-stable: review X-Patchwork-Hint: ignore Precedence: bulk X-Mailing-List: patches@lists.linux.dev List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 6.18-stable review patch. If anyone has any objections, please let me know. ------------------ From: Kefeng Wang commit 63867c82d0c0c2d182016a32b1cc0103116b0ea5 upstream. pte_pfn() and pte_dirty() have undefined behaviour when called on a non-present PTE. In migrate_vma_collect_pmd(), these functions may be invoked on non-present entries (e.g., device-private entries), leading to potential crashes from pte_pfn() or incorrect dirty folio accounting from pte_dirty(). Fix both by guarding with pte_present() checks. Link: https://lore.kernel.org/20260708003955.4024340-1-wangkefeng.wang@huawei.com Link: https://lore.kernel.org/20260706111958.3649651-1-wangkefeng.wang@huawei.com Fixes: fd35ca3d12cc ("mm/migrate_device.c: copy pte dirty bit to page") Fixes: 6c287605fd56 ("mm: remember exclusively mapped anonymous pages with PG_anon_exclusive") Signed-off-by: Kefeng Wang Reviewed-by: Balbir Singh Acked-by: Zi Yan Cc: Alistair Popple Cc: Byungchul Park Cc: David Hildenbrand Cc: Gregory Price Cc: "Huang, Ying" Cc: Joshua Hahn Cc: Matthew Brost Cc: Rakie Kim Cc: Ying Huang Cc: Signed-off-by: Andrew Morton Signed-off-by: Greg Kroah-Hartman --- mm/migrate_device.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) --- a/mm/migrate_device.c +++ b/mm/migrate_device.c @@ -209,7 +209,8 @@ again: bool anon_exclusive; pte_t swp_pte; - flush_cache_page(vma, addr, pte_pfn(pte)); + if (pte_present(pte)) + flush_cache_page(vma, addr, pte_pfn(pte)); anon_exclusive = folio_test_anon(folio) && PageAnonExclusive(page); if (anon_exclusive) { @@ -230,7 +231,7 @@ again: migrate->cpages++; /* Set the dirty flag on the folio now the pte is gone. */ - if (pte_dirty(pte)) + if (pte_present(pte) && pte_dirty(pte)) folio_mark_dirty(folio); /* Setup special migration page table entry */