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 EBE383C9EC3 for ; Mon, 30 Mar 2026 11:56:39 +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=1774871801; cv=none; b=K7ElAcoKIsUmp7vxFRfK/2nxym0SBbamGum38bjrBK3mFfMH/1T4+9X036tDwHPlWmpvtEUnFgMb6Iey3NKFMuTA07byCN5HjYbh2i7KiLt19v/QXBjijDosi3oCV7bA+JdP9W/V03FQ07VI5c9NNUhhUDgv0JZX/yu2F8YYf98= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1774871801; c=relaxed/simple; bh=M4HyWWLEyG/PaSMhJhmqBoznLUBhaqUM5I7Xo6kWQlY=; h=From:To:Cc:Subject:Date:Message-ID:MIME-Version:Content-Type; b=npKLLXIjed86Hkgq0gm8AvIiLo9vsfqI9ZciJPgFbcjmIbQYXhRnq2pmri5KV9kV5PZxR/JU8DTCk+5ejg/1v40fDyozvm/LJHOQ4WSuD6Se02r6/kv4qQXpGAtw7emW0uP+VQTef8Mr4Iu3kfClzMfjpoYIt0JqdYlxPmlQtiM= 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=PgUtMJVZ; dkim=pass (2048-bit key) header.d=redhat.com header.i=@redhat.com header.b=ocLgnX3p; 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="PgUtMJVZ"; dkim=pass (2048-bit key) header.d=redhat.com header.i=@redhat.com header.b="ocLgnX3p" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1774871799; 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=99+Ss0uYTsIYKLWmw7UltxGOhAAHJGZHHKb35Rp1Csc=; b=PgUtMJVZEzxFiIviXekUml4usQu7PjFp1UcUsH85tT8rtn/3sjf2POAMaquCog8lxvjx3C Am6AdIVk81DLbwMByIF5bBlEUBgqfbyUiDorb4YWWd1hpuWBjYG7Dqf+u0FifDmXTAHwo4 8zGG1u9t0flq4bkw4ohdwVJxjopNPyw= Received: from mail-lj1-f197.google.com (mail-lj1-f197.google.com [209.85.208.197]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-130-uancf5hgPPSf4YogqH-VMQ-1; Mon, 30 Mar 2026 07:56:35 -0400 X-MC-Unique: uancf5hgPPSf4YogqH-VMQ-1 X-Mimecast-MFC-AGG-ID: uancf5hgPPSf4YogqH-VMQ_1774871794 Received: by mail-lj1-f197.google.com with SMTP id 38308e7fff4ca-38a326b8fa0so28859481fa.0 for ; Mon, 30 Mar 2026 04:56:35 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=google; t=1774871794; x=1775476594; 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=99+Ss0uYTsIYKLWmw7UltxGOhAAHJGZHHKb35Rp1Csc=; b=ocLgnX3pZokxPTY4+bisIpIP2qdLBa3OHt2DrRqEqNhiaoDMdfxsyY5SJkA/2vHaUT O9dLNgNf3WsWAEBDUDDVAtfkOs5eQuZtJv3KEU0KN3okkqF1xI9rRUuD6CuTZ9cS4JT4 UxzXf2HSpNdB+upAwhbL7g2HPxrd6vXNNJW7dVM6PgWp8Lyjp3pcGMKypXUzs5O5QmCa jm1NPMJX/kBZLjt/DfW7GWb9haMK5/yM7memJcA+fAXELDiZb+JDFMQy4MK0/JCE9dbP 29C0u3O3fTshaMLIC6TpyCEYSTfxoPBzsMhmu0D4M3MP7XvUb3xgj0ftVHFDhSxK3Joa 74dg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1774871794; x=1775476594; 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=99+Ss0uYTsIYKLWmw7UltxGOhAAHJGZHHKb35Rp1Csc=; b=efcKZ8tMzDQN76f++knS2U8hpOzGG3bJbFa/W7zEbCVZNZ910iAUuJa9HK9ls5Lhyi 0g5KRAvEY8DnW/qyVaxo2I16x8YGm0a1xj7gtCXqOuHqkq75z8j0gdjP0KQYtquCBB13 S7dxTPDVecrKgnnDKNpEmuT12I8qvruSaTGHyKjgiIZwTD45xTC8anWM8wc6jqovf+zk Fjllja1IHlhZtFhtSda0ZVAALRl3xRxPznqm5hO0Ocbh1DxB9/+FSI+7+cR4g9PHm52H 5XtXWetiPxRCUDVvgJ/1N91YM6ZVR6u9DyenDFtnEXyiBr28Ilq7m7LH22KQPDmL5632 qWcQ== X-Gm-Message-State: AOJu0Ywt1NfZl2VNCWVYeE2bITQQ0/YG9NB/HyWZZA+bEYA20DcL67BP w6B0HcwzV9qnjO3dF58vQHK1lXvgANF5BuYw+2SIUem1PO79UDHIsjYlSpv8/stxmI0SIrTRGpd UpJPbYt9wTpy+ClwDusrbBGPg6I8A319HxTCGKXHOg9UgjkfhFlEXTHqYEvhl7sZe X-Gm-Gg: ATEYQzxjv5FEwYkJozHuKdMeUo1CxZNGG5KRlVdYR7ZdOyL+okOiEA2FfoRw614K1Cv gpgcZVifdme/kVkebVeWkTpvSVZqIkmFIjPd81RIfriaXNMFF9xHXGO16a4X3ppWTN+mzZmW9hk VXiemeSZnhqZMcXFZZv36p4GaYjZzwFD6z2Gfarofc6X3Teuq9DbF9kH6hBjj2wLd+LMH8yDWrq Lsvbdty6qalCNzNLyxwItBqeqvfwDWmx3FlBMj1kEHuA1RDLOR8V+zxQGiFp2NtdRgKdD1btnkg Dv7heZ+qpk2fvzV9sH4epUudHmauRlyzpL4DaFZ/upagc+HFjMwmIEQsV9VXpPVg8EsISxkShMV SwdNgi/juFL2djzeSilJKTKOfNbajCv2Mlj0N X-Received: by 2002:a2e:3617:0:b0:387:b72:816a with SMTP id 38308e7fff4ca-38c6537b330mr39500381fa.3.1774871793901; Mon, 30 Mar 2026 04:56:33 -0700 (PDT) X-Received: by 2002:a2e:3617:0:b0:387:b72:816a with SMTP id 38308e7fff4ca-38c6537b330mr39500271fa.3.1774871793268; Mon, 30 Mar 2026 04:56:33 -0700 (PDT) Received: from fedora (85-23-51-1.bb.dnainternet.fi. [85.23.51.1]) by smtp.gmail.com with ESMTPSA id 38308e7fff4ca-38c838daa4bsm16330071fa.32.2026.03.30.04.56.28 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 30 Mar 2026 04:56:30 -0700 (PDT) From: mpenttil@redhat.com To: linux-mm@kvack.org Cc: linux-kernel@vger.kernel.org, =?UTF-8?q?Mika=20Penttil=C3=A4?= Subject: [RESEND PATCH v7 0/6] Migrate on fault for device pages Date: Mon, 30 Mar 2026 14:56:05 +0300 Message-ID: <20260330115611.347988-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ä Resend due to merge glitch.. 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 flag, MIGRATE_VMA_FAULT, is added to tell to add fault handling to migrate. One extra benefit of migrating with hmm_range_fault() path is the migrate_vma.vma gets populated, so no need to retrieve that separataly. Tested in X86-64 VM with HMM test device, passing the selftests. For performance, the migrate throughput tests from the selftests show similar numbers (within error margin) as unmodified kernel. Tested also rebased on the "Remove device private pages from physical address space" series: https://lore.kernel.org/linux-mm/20260130111050.53670-1-jniethe@nvidia.com/ plus a small patch to adjust with no problems. Changes v6-v7 - rebase on 7.0.0-rc6 - added documentation and comments - denote to be migrated zero page as HMM_PFN_MIGRATE alone - got rid of HMM_PFN_INOUT_FLAGS movement in patch 2 - picked up Acked-By from David for patch 1 Changes v5-v6 - rebase on 7.0.0-rc4 - use range based TLB flushing while unmapping ptes - gate migration behind HMM_PFN_REQ_MIGRATE for fault and migrate paths - always infer migration flags from migrate->flags only Changes v4-v5 - rebase on 6.19 - fixed David's email address - fixed link issue without CONFIG_TRANSPARENT_HUGEPAGE - refactored into smaller commits - added more comments to code Changes v3-v4: - rebase on 6.19-rc8 - fixed issues found by kernel test robot with random configs - fixed typos Changes v2-v3: - rebase on 6.19-rc7 - fixed issues found by kernel test robot - fixed smatch issues reported by Dan Carpenter - fixes to lock handling (pmd/pte) on errors - added assertions for pmd/pte lock states - other issues discovered by Matthew, thanks! Changes v1-v2: - rebase on 6.19-rc6 - fixed issues found by kernel test robot - fixed locking (pmd/ptl) to cover handle_ and prepare_ regions parts if migrating - other issues discovered by Matthew, thanks! Changes RFC-v1: - 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/ - v1: https://lore.kernel.org/all/20260114091923.3950465-1-mpenttil@redhat.com/ - v2: https://lore.kernel.org/all/20260119112502.645059-1-mpenttil@redhat.com/ - v3: https://lore.kernel.org/all/20260126111939.1332983-2-mpenttil@redhat.com/ - v4: https://lore.kernel.org/all/20260202112622.2104213-1-mpenttil@redhat.com/ - v5: https://lore.kernel.org/linux-mm/20260211081301.2940672-1-mpenttil@redhat.com/ - v6: https://lore.kernel.org/linux-mm/20260316062407.3354636-1-mpenttil@redhat.com/ Mika Penttilä (6): mm:/Kconfig changes for migrate on fault for device pages mm: Add helper to convert HMM pfn to migrate pfn mm/hmm: do the plumbing for HMM to participate in migration mm: setup device page migration in HMM pagewalk mm: add new testcase for the migrate on fault case mm:/migrate_device.c: remove migrate_vma_collect_*() include/linux/hmm.h | 18 +- include/linux/migrate.h | 26 +- lib/test_hmm.c | 101 ++- lib/test_hmm_uapi.h | 19 +- mm/Kconfig | 2 + mm/hmm.c | 821 +++++++++++++++++++++++-- mm/migrate_device.c | 591 +++--------------- tools/testing/selftests/mm/hmm-tests.c | 54 ++ 8 files changed, 1054 insertions(+), 578 deletions(-) base-commit: 7aaa8047eafd0bd628065b15757d9b48c5f9c07d -- 2.50.0