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 BC5F2311949 for ; Mon, 19 Jan 2026 11:27:54 +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=1768822076; cv=none; b=hr1HIm0QKpa1+p5YjeM5FQescwLCmg4r8ZnaXt2V3lB4DhtGWPPkc5CVQw+keaQbOPRHyJdlF6qAw0YTcHV4/o71qHGMfr3GC+auhCuGA64wUKWyDbkOcb9v7pYBVdEdRmIM+gTDYsZ7yGStP1h4tT5Qmgntt8i60SQB7nyW55k= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1768822076; c=relaxed/simple; bh=3CQRTrMnx0MbggRTnvrBOoWw12vR0FFwn8lQsXD9HbE=; h=From:To:Cc:Subject:Date:Message-ID:MIME-Version:Content-Type; b=Aqrt3D5Caj0bM+yC1kR8ujtWW1RW5lenGKPxwUoW6m42JpOvMyQvztRXBr/6qFAsMHpK/dIRMwr4M51iqD2zcwmJMHzEmL3Tg5sNNCF3wfBFHwo78VfiCCwr/gx18/aCGQVdhbJRP/yQjyXyNh9GLh21VUCkc7Hr7qvqSCKD75Y= 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=OBXFRGCc; dkim=pass (2048-bit key) header.d=redhat.com header.i=@redhat.com header.b=cK3jh9uC; 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="OBXFRGCc"; dkim=pass (2048-bit key) header.d=redhat.com header.i=@redhat.com header.b="cK3jh9uC" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1768822073; 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=pP6KPkEm66wfX7plJqqUtylJcknJM7pwcp7nxWzHOCA=; b=OBXFRGCcXY3qnD5b8dV9rTFMR6C3DDkm+a2FCZelJG/C6ljp7cCVyGTXsdrH84mqhmpq94 5DHffbP0U3xElFpTjhNCi97lA64WlXQLNN0tZAKY6mDKdAYv/Mgr6esxFHbCun47Gf+yj5 Tx6xIQBDMNhUxHd9rQc2vGKsLxGpHgY= Received: from mail-lj1-f198.google.com (mail-lj1-f198.google.com [209.85.208.198]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-108-WbVvMjz8OsW5ENjffdxqDg-1; Mon, 19 Jan 2026 06:27:52 -0500 X-MC-Unique: WbVvMjz8OsW5ENjffdxqDg-1 X-Mimecast-MFC-AGG-ID: WbVvMjz8OsW5ENjffdxqDg_1768822071 Received: by mail-lj1-f198.google.com with SMTP id 38308e7fff4ca-38310a79f92so29668051fa.3 for ; Mon, 19 Jan 2026 03:27:52 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=google; t=1768822071; x=1769426871; 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=pP6KPkEm66wfX7plJqqUtylJcknJM7pwcp7nxWzHOCA=; b=cK3jh9uCKJ/g0erpfbgEhGq7+tKu1xl9d1yrBJ1RWN9Rsw8xBDN205QU8a77JLXcig lfOOGntOiJEg3ObkpNaiHEqMLAH3jXNiulGh/IGuOXNbcO7lMjH5fP2KQotcuvbtSO6C Epxxq0alDkb44fF1UjEVIMenboqUD3vFjojX56Ewe74cyNR8uCUNKQjM6SkRoly/rWHZ d3gW3DQSMxdnK7NcraJgv/WCalYyajCJ8ciqGN9kAdEoPsuVUDKtd9PwW0jlBZEJAT1k /gE5kEnEKDl3dwWZt8JQXbsi7KIQdiGi//f4vzYz5Hq7a4t7fil/GGKSnn1vYYHS5OnH GbFw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1768822071; x=1769426871; 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=pP6KPkEm66wfX7plJqqUtylJcknJM7pwcp7nxWzHOCA=; b=hGsYoovGkzmYE/QaqHE9tosvgOhLZceGI7Bl0eRoLSbaKs7s4+CWBDP8LVQ7Sxo3w2 wiXNkGRtQqtYwMHtfvGFYd6opOAU/X4uW49KUVbtNjJAcraFRLN0OHk8TZnqaD/lXvWl qkWiDe7QlwkCYUyHcOK76HrdrpUosSYKycFto4PsYyON+IvTq5YXh1P0/wk5AMpNVwph uYVq2Zi7AxLnaEEPHvWx3Waowh6ZCLPifVdvZOTsUpphrlpXbJ/xiyDtKxFZCTwyl9Lk Wdcq2vuUdqzKk35vaCVTjYDFMcxNeB8ivtF3HyODoDxwMFyfb1bPRVHmCYA9uXxET05i 2uxA== X-Gm-Message-State: AOJu0Yz/f1sJqSkFGKtdaGNXwHQCBX5HyI0Ozu70Tu5RdTQHT7R63C1b F15AsTo89oHQ/2pkTz16KA6tIFOQdiISNAggGyFT5xNWoMwcPg4L4LDbRW8UmpEOKkWD4bu/+2O nxINq5InF5tUAhvPcnIB2U1mIQku89SZvtV/KcVoZ+RITZXPk6Zx40G2NxeOi9aEY X-Gm-Gg: AY/fxX6veHm4l2YTM027ipmsdLTd6LDdBBO3RQRqgyEHimOfDPCannevxd++SQAFtAJ JPwC5JWV43XbL5ygkjim78BwyBqMY2Z18pbBBCj5ceqCuyyHcJjHg3sGbfMIXmT4KDDUtFy0Y9i JX9utidqVSPag8bPytEIvEJwvRYiXOpvL0441PMRoUJmX+jqGnMS/1Lcd/Lx5m//3Brs3ki2hvn CKzJCRrk2yUY2KYBs46BdFxHFwefkZ2vQF3EXkSodOxykoRymJX2KJVTNsITSs5tMGoqamAK6LH vETmByUCBevCMsG1t65ZSkZgW7cjiXNMMW0ho0/6/0FImwOTeYLXibEzwA/Sr9lb4xTIaxR/bgv jCFse8XBWwWlOsR/q1K4K1L0K X-Received: by 2002:ac2:4f14:0:b0:59b:6f38:9083 with SMTP id 2adb3069b0e04-59baeebad8amr3861487e87.3.1768822070724; Mon, 19 Jan 2026 03:27:50 -0800 (PST) X-Received: by 2002:ac2:4f14:0:b0:59b:6f38:9083 with SMTP id 2adb3069b0e04-59baeebad8amr3861472e87.3.1768822070247; Mon, 19 Jan 2026 03:27:50 -0800 (PST) Received: from fedora (85-23-51-1.bb.dnainternet.fi. [85.23.51.1]) by smtp.gmail.com with ESMTPSA id 38308e7fff4ca-38384f8a1efsm31226411fa.39.2026.01.19.03.27.49 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 19 Jan 2026 03:27:49 -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 v2 0/3] Migrate on fault for device pages Date: Mon, 19 Jan 2026 13:24:59 +0200 Message-ID: <20260119112502.645059-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. 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 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/ 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 | 19 +- include/linux/migrate.h | 27 +- lib/test_hmm.c | 100 +++- lib/test_hmm_uapi.h | 19 +- mm/hmm.c | 770 +++++++++++++++++++++++-- mm/migrate_device.c | 594 +++---------------- tools/testing/selftests/mm/hmm-tests.c | 54 ++ 7 files changed, 1002 insertions(+), 581 deletions(-) base-commit: 24d479d26b25bce5faea3ddd9fa8f3a6c3129ea7 -- 2.50.0