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=1795947215; b=oJ3U+VfxjpZZQdAdV9V3lRxwehxYWb4ih3pwH/vKzEgzyB/8RyZQbkbFjZGvVGM6PVoR L3w3+d4I4uczwyydOO5HyMbVPGf8tF3kXEViuufpFnjM9xAiX+gTFI263HvKsgBQPydh1 1DRkBaouPQzrgJiSZbGWwnrqUQ/rGAf1jtkriCSSJSR1lgzPSyWFCAKfQQBf81qF/Kqpx 7kavFkt2KFD8sKb+mP09XLzbF5NVPML0HlnHUHhGnf46npEeJUD4UJEKsj7+E02tBN48P /Peo4fUmQ22SfW57kxbFJn9KGc3UIiCIz6OlPkgz9QsCQEX5P6F54JLSFyyop3JirWA== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795947215; 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=p5E431AtlHn+vFE412xheepKIsPi2RijR77gURMwRP4=; b=HaqVpuhBeTTM6BdfFrIecImDwsLblX4yzA3k5Ot45HxIg9XpYIlid1oE4vrTU15wX/yw nYsBZWdhi/k6GII5N0467EhODE3n95bgaQCp2YS2MZ0UgxoLnW+RcRVheQcng1ZOWD6m0 beXnSPqT+3t+4cdqbZKJY9ETtq+dEMlm95J5Kpjtd8lbiT2ExctcIvLAOqb/aAzz/aG1r 9qzoYZn17vaceyWp4csTEWrK4NpgPesJCuuf3lXeaw9GJYBH95GtFSnwDKljr/tWtyPFZ p9gu5WeFlP4zz92uqYXxre/fro9SLq0q6tiD1c5RPLmPIYGhDHCC0TttWqLN5mLgA1w== 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=BnKa+Fm1; 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 47E50606A329; Sun, 29 Nov 2026 10:13:34 +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=BnKa+Fm1 Received: by smtp.kernel.org (Postfix) with ESMTPSA id 107A31F00923; Sun, 29 Nov 2026 10:13:16 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1795947214; bh=p5E431AtlHn+vFE412xheepKIsPi2RijR77gURMwRP4=; h=From:Date:Subject:References:In-Reply-To:To:Cc; b=BnKa+Fm1hFMVVyObt/wy2GLHOxU2fEPz2SWZqK4JZEjkfqbpZLZa3PY+yHDyH3c6G 8s4Rjm0mqACOrQCT8r6OgiHpLxN1n8txZ2ekGa21ZvL7Wc9Ya6uK7L8fdGHQreBDOo G6dP5dhNoVpph/+CZQK3q4StSkMNpBd+A1fKtZeDbb3/wqQLKpraT9svYxNvNHsnIY 8+WEl/xzK6xtbRz5WNssbFpkKzJ1HdScV/wwmvh9VeBAc6ClHTOH6oVKEBpGFFeH6n R2ESx23QecXEPucSW5oeJkI4Oscj012j7NV8vtfmp5jprA5YqQlktxVwk1RqGyiUxT i8myQcq5xwokg== From: "David Hildenbrand (Arm)" Date: Sun, 29 Nov 2026 11:06:52 +0100 Subject: [PATCH 21/32] mm/migrate_device: mark folios leaving device memory dirty 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-21-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)" , stable@vger.kernel.org, Chris Mason X-Mailer: b4 0.16.0 X-Developer-Signature: v=1; a=openpgp-sha256; l=2940; i=david@kernel.org; h=from:subject:message-id; bh=HBo7ew8w4XhI1KugT5VqkFSZoOCOEI40BIVz6OYkzUM=; b=owGbwMvMwCF2/OVpCdfQ3ZWMp9WSGLK5f+qmzDXpbdY9+dlz0i22Bb+uz9ixXvSdbK/Lq/OsL Up8WenyHaUsDGIcDLJiiixBwfcNXr5cV1vtxscMM4eVCWQIAxenAEyE24ThD18qN+fuEOmY0/O0 fjEH6/ZX1zapnbquuef4nb2HzGQaWRj+aejEbfnoVng2VNz6gvqWzyKbnpt45yrLxMVVfOKcH/O aAQA= X-Developer-Key: i=david@kernel.org; a=openpgp; fpr=1BD9CAAD735C4C3A460DFCCA4DDE10F700FF835A Device-private page table entries are weird: they are non-present page table entries, and the device can write to the memory without the CPU noticing or the CPU page tables reflecting it. So there is no dirty bit on the PTE we could sync to the folio. And usually, nobody cares about the dirty bit for these folios, as these folios are not on the LRU. Device-coherent memory has the same problem: it is mapped through present PTEs, but the device writes through its own page tables and does not set the PTE dirty bit. There is one problem, though: when we migrate such a clean folio back to system RAM, the new folio will be clean, and will be mapped through clean PTEs. Consequently, reclaim can discard the data if the folio is lazyfree: (1) MADV_FREE on an ordinary anonymous folio makes the folio lazyfree and clean. (2) We migrate the folio to device memory. The new (device) folio is lazyfree and clean. (3) The device writes to the folio. (4) We migrate the folio back to system RAM. The new folio is still lazyfree, and clean just like the PTE that now maps it. (5) Reclaim discards the folio, and the data the device wrote is lost. Similarly, after fork(), a child that shares such a clean folio can call MADV_FREE, which does not check a clean folio for other mappings. So let's fix it by simply marking folios dirty when migrating them out of device memory. This has low urgency, as it requires MADV_FREE on memory that is migrated to device memory and written by the device. Fixes: a5430dda8a3a ("mm/migrate: support un-addressable ZONE_DEVICE page in migration") Fixes: 8315ada7f095 ("mm/migrate: allow migrate_vma() to alloc new page on empty entry") Fixes: f25cbb7a95a2 ("mm: add zone device coherent type memory support") Fixes: a30b48bf1b24 ("mm/migrate_device: implement THP migration of zone device pages") Cc: stable@vger.kernel.org Reported-by: Chris Mason Signed-off-by: David Hildenbrand (Arm) --- mm/migrate_device.c | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/mm/migrate_device.c b/mm/migrate_device.c index 4d1b2067b60aa..c6473f2e5388b 100644 --- a/mm/migrate_device.c +++ b/mm/migrate_device.c @@ -1293,10 +1293,18 @@ static void __migrate_device_pages(unsigned long *src_pfns, mapping = folio_mapping(folio); r = folio_migrate_mapping(mapping, newfolio, folio, extra_cnt); - if (r) + if (r) { src_pfns[i+j] &= ~MIGRATE_PFN_MIGRATE; - else - folio_migrate_flags(newfolio, folio); + continue; + } + folio_migrate_flags(newfolio, folio); + /* + * A device can write to device memory without setting + * any dirty bit, so just consider folios that leave + * device memory dirty. + */ + if (folio_is_zone_device(folio)) + folio_mark_dirty(newfolio); } next: i += nr; -- 2.56.1