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 B5CB234FF58 for ; Wed, 11 Feb 2026 08:13:25 +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=1770797607; cv=none; b=bo5LEKqdCJyU8wMlpWiXf93+sJclxyS9UMCT4ns4F2YkvQCGeT4k5Ku7CTm4ztEM1lbAIZRDcq3urPSCHI9Og1zxfxTqC6pE9MBLcadnBk9fstFGEO44J+MhSAlzev2qwmbdUJzVWiNZSJ0jO6BJXcXWe+3sXjwnrIA4QyThSZM= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1770797607; c=relaxed/simple; bh=ZKS0qL+nx6+vahJOa4BMwXeCf8bTAbHge3BbQx2n8AE=; h=From:To:Cc:Subject:Date:Message-ID:MIME-Version:Content-Type; b=kKxi7xpxCGIkBn7Hw/mwzdIAtwfU5GFbIe3oVPN65aUtOVysddgY2kcZwe0nJK64inAiYH0v6YFDSgGL4t2jBtNagwYHsqIP1JBfgqAbBgFe3UNSQNpf2atwy5NQZCl4P5sL9b1/kqrbKaeSChC9xIMHgVatskTL3PvKW5M+5zw= 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=PlZ9rZMb; dkim=pass (2048-bit key) header.d=redhat.com header.i=@redhat.com header.b=eL9bcRB1; 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="PlZ9rZMb"; dkim=pass (2048-bit key) header.d=redhat.com header.i=@redhat.com header.b="eL9bcRB1" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1770797604; 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=nZ49YzVRHq95c7sl6l6/qp5imwtWxWIALbbPA1ykmsQ=; b=PlZ9rZMbduNF5NGInMcngQs9yxe1CGNQuFmLgrPL7+pvwD5cwrhRxyr9tLM9sJILz5GrHm CoFQoVXQJWcHnooT00BzRhKu0iTqVn42gAP98+Z9pj/a51ASkdGD24RKLCxQT4dolR4N7N P9pZVZUsu2KduUltAMyt9p/1WexMO5g= Received: from mail-lf1-f72.google.com (mail-lf1-f72.google.com [209.85.167.72]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-510-63jOw-qWM0uSRmSKn_JVww-1; Wed, 11 Feb 2026 03:13:23 -0500 X-MC-Unique: 63jOw-qWM0uSRmSKn_JVww-1 X-Mimecast-MFC-AGG-ID: 63jOw-qWM0uSRmSKn_JVww_1770797602 Received: by mail-lf1-f72.google.com with SMTP id 2adb3069b0e04-59e14ac0ed7so2530090e87.0 for ; Wed, 11 Feb 2026 00:13:22 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=google; t=1770797601; x=1771402401; 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=nZ49YzVRHq95c7sl6l6/qp5imwtWxWIALbbPA1ykmsQ=; b=eL9bcRB1Y5UDftFxK1sw+9dUbvIecub53RogSi1aATNo3dUJmfwsBSHmRk00+zYKon 6jAdkTiUdJy/vMb2Zn/jFJkvjLb3/5gSxLNTxqW4HMNkVUqYJHo/jKXW78WxK00RVJKY dm40Z4DF4EgWb6SDSO+QR1wVox1H8mGkdkpIDyQ4s+Sc+A369gqW0fOfEKggHvq07jRX e2XBkT+fhfIKF6OkDDxhAzUV2EH6+HZ6TV5ZJ/r6dUzlX1GyqFd9HTMn85mIvnSgXVJI gZmbFZWuHQtSmPmrnhW1b+m5WiFI/94mRTPI71cE7m1aHFKIqgROwVfYfsckysndO8XB WRZA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1770797602; x=1771402402; 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=nZ49YzVRHq95c7sl6l6/qp5imwtWxWIALbbPA1ykmsQ=; b=oBKF4f7PPPcpWCPsBPWitJDxDsZaKxgP0xoAeDXHv5MiMtBycJ77JocDaUTgCq+eHH jKOiBsCjjHf1Uwgzu+X1GTZ1Fuix9KFohQKnsI5gnUKK3+JnvaaD4QfdnBjRZbOo1T+L tOZ94SjumqJkZ+qRU5Ys9tMkHndMVs1Y0LwxAXrYpPpePqbLQUqg0X7loFMukx7HFORW EI9UeGIVyxQXFy66ZqrLn1vF03FjUJvQAelMJDWZDlJhMEl1lA5P5lSiHg9UzCI1rsg4 bp1mnrvMltWRds/n+ZR4QSRprRWGc9HFhQzG8qw2cg3DvgH5FhLPCPxTg9dEP4zOpCzE gg/g== X-Gm-Message-State: AOJu0YztyCIjIBjzZAo+5kWXJFuwFdq1Oo9R4I9WtbRpSxWXXHkZ+ode RX9p6ZToGMtx3bymXZJqePY2aFrJcxBtei87ZJvZp5vamqauHCTWz8lc10IZA6Y78fVWcCk+gSa ZRnOIka6L3He4FIs49Gn48IwwWiMNhaWiy5FPXTC4aGXfh93aXLneAthRn15sqLYg X-Gm-Gg: AZuq6aJ6J7w7ZLHgHwO2YEQQgwzhcOMF7IgXlwYEfmF1qZxaIoUR+AUR8dzIIgZfOvA 9s/dSfSr0k9PYXbhC9fSpY3iZ2o3LxB8/N9YcEkFKPUBFq9A2ssyfzoZLu+q560Zj1qn2oVvmTm JybkSFW6EsMNsudNFCxIuZUxCR7eQtT/UyHUHRuii2cW3sxDwndPclPQfD2qhuXyYNS9JUAmE/V rLW4ItJyNLXvZfMSB8IxbRshnx5NaG0Zh5qOu1Uk7SGddfyLO0Xg003Dfox95CeSjJxa2+dZkcO 6nEoNnbOMRcEMF9IPCEAdaLU58jLzd6aqxgeXzNQSd7GljpIc1n0n8K1AXDm57v0mAJe954xJYU 9loYdeB5sg9Ybhrt5e+0H8TE5+HFLy3dUswJm X-Received: by 2002:a05:6512:681:b0:59b:79e7:c8e4 with SMTP id 2adb3069b0e04-59e5c3f80c1mr631346e87.49.1770797601501; Wed, 11 Feb 2026 00:13:21 -0800 (PST) X-Received: by 2002:a05:6512:681:b0:59b:79e7:c8e4 with SMTP id 2adb3069b0e04-59e5c3f80c1mr631332e87.49.1770797601029; Wed, 11 Feb 2026 00:13:21 -0800 (PST) Received: from fedora (85-23-51-1.bb.dnainternet.fi. [85.23.51.1]) by smtp.gmail.com with ESMTPSA id 2adb3069b0e04-59e5f568737sm188515e87.21.2026.02.11.00.13.20 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 11 Feb 2026 00:13:20 -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 v5 0/6] Migrate on fault for device pages Date: Wed, 11 Feb 2026 10:12:55 +0200 Message-ID: <20260211081301.2940672-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 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/ Cc: David Hildenbrand Cc: Jason Gunthorpe Cc: Leon Romanovsky Cc: Alistair Popple Cc: Balbir Singh Cc: Zi Yan Cc: Matthew Brost 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: implement device page migration for 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 | 27 +- lib/test_hmm.c | 100 ++- lib/test_hmm_uapi.h | 19 +- mm/Kconfig | 2 + mm/hmm.c | 814 +++++++++++++++++++++++-- mm/migrate_device.c | 602 +++--------------- tools/testing/selftests/mm/hmm-tests.c | 54 ++ 8 files changed, 1053 insertions(+), 583 deletions(-) base-commit: 05f7e89ab9731565d8a62e3b5d1ec206485eeb0b -- 2.50.0