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=1796389012; b=SyUZ2IKt8GiQmsrYuZEBOSyEtFjeISdYV+iOxJ78HUNryUiGkTAIstvL/ofg9G6isFv5 nVoW1KOrqB/zTGiz7qlG1mw24S2avgHVA5b/JbtWdd8gP+jqBc6dZn+Hx0+aNYMxWNL/H PavYAkMe8jrOwQIhMZmIwjS7+i8T6iWo3EtEIElmPMOLKNR69VSGplhcQyyxIei+WQl4n cbRWl4aRIpDIvxNB++F/p/O6r6jo1WnQOoCuuLHqQws9qU52n5ykwA3GitJBR1KQBHwgm FFEa2WcRNhYXmiwk0uXwMJ4EdcQoAnPVOXJKriDXhZLrveTUYk0t7GMCv34W4Hb7xGQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796389012; 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=baq2rQ/G0eT0pOIhKQAAFbt34FQJoAWWD98TIUl+TIE=; b=PXZdhacNH0Z1341GhXi9Altt57Zw8OnPeoHfHAMLK/GQ991bUjV9uzh0YUiN+SVIb7mA LQ7HzRZVYZpp/uH9+un6qMLsRUc39ndjXrfCYlAN9FnbcqE5PB7DilxTtPgW3PSmhY2zQ DLPEA/GuXXSgyeC1CsmCSQenQsYn/stPyAHluCa72Hq50RdAlbb9kCJJTJpqMbkVfr+Xh w76OFw4JMlml0CsmWWCXIV+1O3NiMgkvNnV3NXtzFtn3mgnyZwj/00PrwzuJBx1Yi7NVf dkcUQCXE9HSB9g5Q4P341SsSRAN9FpZPstKbiGz0luS1mLxvteu7LIsRzTQSZG0puLw== 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=hU5EGU+9; 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 02A23606A32F; Fri, 4 Dec 2026 12:56:51 +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=hU5EGU+9 Received: by smtp.kernel.org (Postfix) with ESMTPSA id 4F7561F00936; Fri, 4 Dec 2026 12:56:34 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796389011; bh=baq2rQ/G0eT0pOIhKQAAFbt34FQJoAWWD98TIUl+TIE=; h=From:Date:Subject:References:In-Reply-To:To:Cc; b=hU5EGU+9DRNIN6+EnFblxUEj6PNJAJz7TOaiwjYZa3q8ZCcD+nE8rf0f5FctdbxPM KA3344J3hWqaGK2OfIwS/RUOAs3CfhQ97Tvth6URvNaEXCPgJFPLjxcw/d/OMR04ap aOPFd8iCvlWZD+Qt+y8E+HKOwbpZV8JLsavVKtR9+uIClOzxLf7nml8AT1FHiXBmfh z9cQn7nxHyo+vbWl61aUQ6jWte6y2IxXKKLgN6FELuLO80NJFpP+blHbZkTLQ4lAqB Apbbm6ARmZ61JUIuk9g9TajyJl8UI6RCnjgC4CW6uWqdXW8k8xq0HUjpPsSdkMVMZI jyVivdiddboVA== From: "David Hildenbrand (Arm)" Date: Fri, 04 Dec 2026 13:50:12 +0100 Subject: [PATCH v2 21/31] mm/migrate_device: mark folios leaving device memory dirty X-Mailing-List: stable@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: <20261204-fixes_core_3-v2-21-9aad65727632@kernel.org> References: <20261204-fixes_core_3-v2-0-9aad65727632@kernel.org> In-Reply-To: <20261204-fixes_core_3-v2-0-9aad65727632@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=3147; i=david@kernel.org; h=from:subject:message-id; bh=kG6gze6D1OmauBT8uzPHQFnEt85BOsgsZD4wASrYa4c=; b=owGbwMvMwCF2/OVpCdfQ3ZWMp9WSGLKFtjPMCLKMTFcLO/Ah0bAps332s5o/3WmHVpgUmked2 8hx3duuo5SFQYyDQVZMkSUo+L7By5fraqvd+Jhh5rAygQxh4OIUgInsz2dkWD0h+teuhptK3t9W 2h+yTjh0iiHuf72+ms7Ls/tWOu1NtWP4nzp/ysQtc6/OOCJ4yOz81fk1Ua/m69mc/pl7yqNI+Oc OJkYA 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 Tested-by: Gregory Price (Meta) Reviewed-by: Gregory Price (Meta) Reviewed-by: Alistair Popple Reviewed-by: Balbir Singh 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 578a4419bb083..964549222ed7c 100644 --- a/mm/migrate_device.c +++ b/mm/migrate_device.c @@ -1289,10 +1289,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