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 0665E329C7C for ; Mon, 16 Mar 2026 06:24:28 +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=1773642270; cv=none; b=AOo88SSFdVuMHClKBd52FHlOtQVNU5fu50nz/88ZM0qpcwhFUNTvqs1tfi9lpOJrDtXzhCjzJ54pZPt9MT+zFC5aSKf+wMFmPTMhLYqTreOoLpXKLLC/2Nv3snxHHKSLO5k0BSMHExuKIbuEC3sdjm/Cx8kMczneTqNEjUXb+8Q= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1773642270; c=relaxed/simple; bh=tKjTNC3D0NuA9TBvPpae7mZhZiMzwVIxJiRwrnzS9jI=; h=From:To:Cc:Subject:Date:Message-ID:MIME-Version:Content-Type; b=WgoKjR08vedcWTCKnAMj70+mx35IRSVj7jgvhVc05ZK13nKN4vQgonc6z15E3O/vokqtMOC/+qLGcD1oGGcFR+DwgT6TUA9OJW/smkzMx6Y15Tk08/LdUL16tZ1Yt2WUEfEjBaYHlsMl6ejYtzE6//yG2NkI4OIDkMdXz09ZUTc= 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=WEVIdu9u; dkim=pass (2048-bit key) header.d=redhat.com header.i=@redhat.com header.b=bU7KbT+9; 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="WEVIdu9u"; dkim=pass (2048-bit key) header.d=redhat.com header.i=@redhat.com header.b="bU7KbT+9" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1773642268; 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=hrU28509ZSz5FGAd8GCuwk8eJ9M4lgoGSuBnbmBDczk=; b=WEVIdu9uyWx9KoT/zjC0d68THluK0YfWsrO5A+xPfeN7kBdlmfue7rPhCTCPnGQhMAwZYl PUyH7gtIGF1xOXC3/pZJny7qFhfzv4vlKHxGGg+4svLwa4+DX+8G6KUCHlCMTlN4N8pm6Z JF8J1YunnTVB1k8OHk+lGSi2Q4soq8o= 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-302-Dy5d72FDPeuiWF86bF0gQQ-1; Mon, 16 Mar 2026 02:24:26 -0400 X-MC-Unique: Dy5d72FDPeuiWF86bF0gQQ-1 X-Mimecast-MFC-AGG-ID: Dy5d72FDPeuiWF86bF0gQQ_1773642264 Received: by mail-lf1-f71.google.com with SMTP id 2adb3069b0e04-5a1499d93a1so2880678e87.1 for ; Sun, 15 Mar 2026 23:24:25 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=google; t=1773642264; x=1774247064; 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=hrU28509ZSz5FGAd8GCuwk8eJ9M4lgoGSuBnbmBDczk=; b=bU7KbT+9dDIEs4sun3gW/n9IAJ/701E/kptcyHC5kfy0XCw/ETK2ayrfivMoxc6P2h 8gC5WIucQSMwoUYzA1FCXiXXvunHdJ9+ljvRkvShvbDzC8obh9KCKXIZ71v4E28LYkUk ffu2Y7ZiSHzKX0CRomlLTZN8C7VWPlrwc7pxRqOXxKgi4Dw/GiSjQUH1W2yCTqGIemAu DAw6A4PcXsAtPDhGf7eo8KSFHGLXiDOfXq9ELXQP+La6uAbW3bmzz+d4E2DmGknb38+L scHt7R4Av9gpprdWSPTsUmphnd0XWjqIIcFuSLbwjR3I31qZWAOYQURcLRwMBeKEAPnR VFRw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1773642264; x=1774247064; 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=hrU28509ZSz5FGAd8GCuwk8eJ9M4lgoGSuBnbmBDczk=; b=Ct/zNcJYsT/OxAd7dzoio1WSHw6gFgLVRQtMtU3DlD/9qExX2wpn+7lvVNO8N+1g4E p6eA9DvlksJTrVReG1KW5hnyl9tvFPrrll9kj/flmfaG4CyR9pKZ8qh1Mua4BE/lRNR4 qZZIirQ80S6MwahDBQ8OkF1AioY7tfP6lO5o5h29mav39BZWVeF2gLLzrIhlssHILCS/ 0F1YQYyggFUBWq1k8VrWiSY1nToY1Pl/uWmLTANjaEBCdgf4Rpf16ajOVd0pjoqCHHON yjQu+Liq1KDLv3bOjiKhTzSGalYEPFC2Tw3YOssUudUgeHp7yvS0QXcTZNnMzPmVFPA4 iIUA== X-Gm-Message-State: AOJu0Yx90Eq0VPuuTSd06gjr9VTmVyWoeUjLlr7IbEt0JQlPnENZlWTb xqkO04maFv3wAztRQef3HPGw/uv5fSfuRm+5L762yJm0VqYVBGisSTVIB48GY5GgAmW13sEQSC4 WS8fsVXmNUa/ePBf6MHn0G4ms7+pE4Zsc+fEHdEVskJeJFudl8BYBzV7VsTn2SuQxnYp/P/lT X-Gm-Gg: ATEYQzwJA3o+liraF1lQ/Mh0KneB+Zls8Nx/LH5tFNJpcIOY9/5YTQF09I6rcWYbjqX oAtblfnMIMOW9Cqe/Wrnb6EGkF+yubFePMXt9LsM7OpMlE9zEvbV2XuOPerIOxOaSZ6S99aosuu y0cwjt4paHDZf7DLUe7T7kLahIxIeg4q+ZSKNALoofGdd2n/UVt3CZJj9YxOgzO9tCsbpCyeQi1 zPlYF+J7QZFi/aoB9YUYelbxndCaJ6vcBPrWl539XF1I2A775MuXOlLTmuojcKxORGa6T3QgXUy nVePt9m71S1LZEQ4+fpSMJ7IM56thXC4YcvgjG4Mn0L0Y6bvZsxGkzFkv7Bo5It6qlHDMuDwJ9O gRQmNjRUccirxJZIoUBrJJFU363EDsK+yD9uS X-Received: by 2002:ac2:4c55:0:b0:5a1:53a8:fe20 with SMTP id 2adb3069b0e04-5a16254f23amr3771367e87.8.1773642263531; Sun, 15 Mar 2026 23:24:23 -0700 (PDT) X-Received: by 2002:ac2:4c55:0:b0:5a1:53a8:fe20 with SMTP id 2adb3069b0e04-5a16254f23amr3771358e87.8.1773642262993; Sun, 15 Mar 2026 23:24:22 -0700 (PDT) Received: from fedora (85-23-51-1.bb.dnainternet.fi. [85.23.51.1]) by smtp.gmail.com with ESMTPSA id 2adb3069b0e04-5a15602e692sm3263397e87.30.2026.03.15.23.24.22 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 15 Mar 2026 23:24:22 -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: [PATCH v6 0/6] Migrate on fault for device pages Date: Mon, 16 Mar 2026 08:24:01 +0200 Message-ID: <20260316062407.3354636-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 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 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/ 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 | 25 +- lib/test_hmm.c | 101 ++- lib/test_hmm_uapi.h | 19 +- mm/Kconfig | 2 + mm/hmm.c | 827 +++++++++++++++++++++++-- mm/migrate_device.c | 603 +++--------------- tools/testing/selftests/mm/hmm-tests.c | 54 ++ 8 files changed, 1064 insertions(+), 585 deletions(-) base-commit: f338e77383789c0cae23ca3d48adcc5e9e137e3c -- 2.50.0