From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) (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 58BF8381704 for ; Wed, 14 Jan 2026 09:19:50 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.133.124 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1768382391; cv=none; b=GJJzD/DXFnPqA3zJCyC9PJViLLhF1yv3rAMoH2XZcnU+he8AS6PAD0QalpfwRkR1WbcPsZfgtXtuYY+VKIiMt/LGr72PewvOewvPwsMwa/NgkdEmBIzxC4VZceB26ma5Sw7PFGCEMMzNKdU5b+r7sfLXACt7G68A8e4q13XRYfc= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1768382391; c=relaxed/simple; bh=UBKbvliwZc+3q8cMdhvTS4Z97tCdMusVzHgk1fkr744=; h=From:To:Cc:Subject:Date:Message-ID:MIME-Version:Content-Type; b=IcOfsd+ihy8LJb+Q5TYyTjNwfBV+RzrvshhLpQDe7aBu9ShpxyK6ARXSuQ/bd9rrV/JacxEjco8DWdB/4c0pcoMBicv1YWHizgIt0LayeRkMn2RFl5GmF3DcDfunFq+4vzECEw4d9MVwRFNiBPr1T1wR4OP4dqAZpiifVL3Cgow= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=eh3GaKWc; dkim=pass (2048-bit key) header.d=redhat.com header.i=@redhat.com header.b=O/N5PftL; arc=none smtp.client-ip=170.10.133.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="eh3GaKWc"; dkim=pass (2048-bit key) header.d=redhat.com header.i=@redhat.com header.b="O/N5PftL" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1768382389; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=G9SVdNqjRAtTrnCSKMEAUtKcyXA7FAEWs6RSUFgfDUM=; b=eh3GaKWcXAuUbnsbcaX8Rkuw5jV+wG3V3o7qYtRgQqHzenePjmRBAt1MVmEw5VUqKsZv1y yDNvGV5WXlj4119MV0JmIf7dOqEz1z77mKcPDpnn34ymLLCxld/brYm4usCjt75HXussLG P/Je2UMBf/DtaMRtAt32kcH6ymC2VUU= Received: from mail-lf1-f71.google.com (mail-lf1-f71.google.com [209.85.167.71]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-614-QAZ-YpqtPQGfnncsTsACUA-1; Wed, 14 Jan 2026 04:19:48 -0500 X-MC-Unique: QAZ-YpqtPQGfnncsTsACUA-1 X-Mimecast-MFC-AGG-ID: QAZ-YpqtPQGfnncsTsACUA_1768382386 Received: by mail-lf1-f71.google.com with SMTP id 2adb3069b0e04-59b6f3890e4so6678774e87.3 for ; Wed, 14 Jan 2026 01:19:47 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=google; t=1768382386; x=1768987186; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=G9SVdNqjRAtTrnCSKMEAUtKcyXA7FAEWs6RSUFgfDUM=; b=O/N5PftLurcIRd1W99RQVCqh7MKgXQt4bhhl/BoQTF9/3hZp/Hr+UMQKTAlWM8boli 6rSVml2f5Ucmp2T35KfB7T4+dY020CvDLe0BgsUZQj8pDF4UV/YT9lom5P6B10u1Q3G/ yGyn7fQi5SHMbKUGEa8rbKMKvtcMyTjY6kBgX2rTATtqzdML98UViFXmyjrjXtu0AYi4 IyZzcDpql79XfD5SsJtrxjQdul/khVjfFZ1k6uCNSKZPMSOOgPJajDpjt2EbyPhYaP83 7lLlmsGte2xf+m1/Zqa9FOEfKUN2wNSqj/pbDz30SYZoj6WA7t0yx3U2El8Iw0iv0ExF Vmzg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1768382386; x=1768987186; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-gg:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=G9SVdNqjRAtTrnCSKMEAUtKcyXA7FAEWs6RSUFgfDUM=; b=BdBPOkk6n7L9sdEQL5x4y7qXIqmZ5Thstbb3tcTpLwprPk3+D2Cc2rVufXlAV4tsF6 SitT2vlFCR1EKAuDP201P3myKo23eGdmoP9HxP8Lkm3IwQq14FVubhEaHe6N0sUVbljT RjblNTxpfuvbReKG3LRgI3pm7mujkz4JyGD+asgddvoKuQ8smNi9ysZ5CqyiY2M6QeNi LxCmlyHBFj4/ItJtR2mWCifS1/R6Blwpnm2vqS/jn8ObsJxB4LCx/tagzwXQgb67eftW eM6C2m2RbNuseaXzOzJxM20LQnXHXZAtXMgVy95ouHCIp/3mhfH1e0F30ZlAMMBrZ74v d2rA== X-Gm-Message-State: AOJu0YxrCUu1ht+Pbx6zkeDwj++s1ikEfICYUnKF1of8AzaFH+SevRGT 96RHwsMpeSuA0pxGP3vz3e88hpC9URHFZ1WUiLsbLD3kbbAaEvW0BYTZDR4+kOC0O31GMNM+NtK lOsaIe04280SDz56hnqOjfhrJiSpqbMrBZmR1h+6Co89CdyHAxsjCkgQVeoJZAh9m X-Gm-Gg: AY/fxX5boFjVIN+XA6lRUaJ2b+AwFPEs8JG8+2C6uubmeTXWra/CtYZ6cJ8g1lZy0OH +us4vR05sa/v4W4fjly6crg/W3vJhE01vdwMsdP4ee9nzUdsFMGgWlLp6yYYMTvOumtulTxNr0+ 8TMJ1NadGSbCR3TBKmMkSC0p2W8jgiZjE2mQN6GCuUa0j7q404SYWfrBzei8qizuT9nMeSXX0us hQ9X1OS8fOXzc1tjMhHaxexasBVyMVu9Xf3PYrW72SmaNresxseMC50k0KDYUE9x9Lt2+QRem1U fqlmnzUgXEC7il9mXXhRo8SR40tHD12sABxU/y5YUMZXR5DqXeI8BXeV56qke/LSNs4sYYiUCRu 27WLwJP6Pa3/y/NZTw8iLSW4i X-Received: by 2002:a05:6512:3194:b0:59b:7c23:c637 with SMTP id 2adb3069b0e04-59ba0f6b15fmr618334e87.22.1768382386331; Wed, 14 Jan 2026 01:19:46 -0800 (PST) X-Received: by 2002:a05:6512:3194:b0:59b:7c23:c637 with SMTP id 2adb3069b0e04-59ba0f6b15fmr618318e87.22.1768382385864; Wed, 14 Jan 2026 01:19:45 -0800 (PST) Received: from fedora (85-23-51-1.bb.dnainternet.fi. [85.23.51.1]) by smtp.gmail.com with ESMTPSA id 2adb3069b0e04-59ba1045be2sm513898e87.81.2026.01.14.01.19.45 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 14 Jan 2026 01:19:45 -0800 (PST) From: mpenttil@redhat.com To: linux-mm@kvack.org Cc: linux-kernel@vger.kernel.org, =?UTF-8?q?Mika=20Penttil=C3=A4?= , David Hildenbrand , Jason Gunthorpe , Leon Romanovsky , Alistair Popple , Balbir Singh , Zi Yan , Matthew Brost Subject: [PATCH 0/3] Migrate on fault for device pages Date: Wed, 14 Jan 2026 11:19:20 +0200 Message-ID: <20260114091923.3950465-1-mpenttil@redhat.com> X-Mailer: git-send-email 2.50.0 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit From: Mika Penttilä Currently, the way device page faulting and migration works is not optimal, if you want to do both fault handling and migration at once. Being able to migrate not present pages (or pages mapped with incorrect permissions, eg. COW) to the GPU requires doing either of the following sequences: 1. hmm_range_fault() - fault in non-present pages with correct permissions, etc. 2. migrate_vma_*() - migrate the pages Or: 1. migrate_vma_*() - migrate present pages 2. If non-present pages detected by migrate_vma_*(): a) call hmm_range_fault() to fault pages in b) call migrate_vma_*() again to migrate now present pages The problem with the first sequence is that you always have to do two page walks even when most of the time the pages are present or zero page mappings so the common case takes a performance hit. The second sequence is better for the common case, but far worse if pages aren't present because now you have to walk the page tables three times (once to find the page is not present, once so hmm_range_fault() can find a non-present page to fault in and once again to setup the migration). It is also tricky to code correctly. One page table walk could costs over 1000 cpu cycles on X86-64, which is a significant hit. We should be able to walk the page table once, faulting pages in as required and replacing them with migration entries if requested. Add a new flag to HMM APIs, HMM_PFN_REQ_MIGRATE, which tells to prepare for migration also during fault handling. Also, for the migrate_vma_setup() call paths, a flags, MIGRATE_VMA_FAULT, is added to tell to add fault handling to migrate. Tested in X86-64 VM with HMM test device, passing the selftests. Tested also rebased on the "Remove device private pages from physical address space" series: https://lore.kernel.org/linux-mm/20260107091823.68974-1-jniethe@nvidia.com/ plus a small patch to adjust with no problems. Changes from RFC: - rebase on 6.19-rc5 - adjust for the device THP - changes from feedback Revisions: - RFC https://lore.kernel.org/linux-mm/20250814072045.3637192-1-mpenttil@redhat.com/ Cc: David Hildenbrand Cc: Jason Gunthorpe Cc: Leon Romanovsky Cc: Alistair Popple Cc: Balbir Singh Cc: Zi Yan Cc: Matthew Brost Suggested-by: Alistair Popple Signed-off-by: Mika Penttilä Mika Penttilä (3): mm: unified hmm fault and migrate device pagewalk paths mm: add new testcase for the migrate on fault case mm:/migrate_device.c: remove migrate_vma_collect_*() functions include/linux/hmm.h | 17 +- include/linux/migrate.h | 6 +- lib/test_hmm.c | 100 +++- lib/test_hmm_uapi.h | 19 +- mm/hmm.c | 657 +++++++++++++++++++++++-- mm/migrate_device.c | 589 +++------------------- tools/testing/selftests/mm/hmm-tests.c | 54 ++ 7 files changed, 869 insertions(+), 573 deletions(-) -- 2.50.0