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 05A55331EB3 for ; Tue, 4 Aug 2026 04:26:48 +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=1785817610; cv=none; b=ky0x55iz5ixI6k52/fTFilb+73mCCaJqnZwjMNEh2u5vnBi2qH3A/be9BTK5Adiby2C1g0FcyMBFl22PcScFPnoOOrFlYgwYwYEnH7lRAZoLBjm5c/sqk/ZO3Gc8K/mUdNB6T2CVLaQe14UZrudiFLoJ3wtxR8ZAB+QIy0p6MM8= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1785817610; c=relaxed/simple; bh=O9qmHSwCmAuJvGOgTvtDdVN8kqlu2UkCfsnumc3QIHs=; h=From:To:Cc:Subject:Date:Message-ID:MIME-Version:Content-Type; b=CnGzFhxzoxS8miz+FCeETvTWjyw3/Mgw/R5SYVnISwW0LkhQI+l5YQp3yayhLQMifeAakSx9fQVhce1zYA+GZoEj1hzhcT3oWDzjfgSme69XjawRZRZLluqAvriKqtiK99pY6+VO9mID8T+wvtj4Bo+/lthNrs/WR08WN4+VlcA= 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=Zj+5iLQp; dkim=pass (2048-bit key) header.d=redhat.com header.i=@redhat.com header.b=DUKKp3Dj; 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="Zj+5iLQp"; dkim=pass (2048-bit key) header.d=redhat.com header.i=@redhat.com header.b="DUKKp3Dj" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1785817607; 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=NA4GkWrTTacC5SlKbIYSTdyMjm/Le+vBS1p2iRoL4e4=; b=Zj+5iLQppR9mL6EqfWIPzlIi4Z4MjmLD384pCx24Ij9wq0dmE9iMnaRSMvUpZZbURZIQW/ 63ikhGa/FGp7kWNVR7OUUqNTGHeOn96OkqMtMxHGOJ7LAfrLg8SuYPH0UdKXNdJarQmn6m kNHOcB78HYnHmDyVyk/3K0h66M/zXJc= 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-492-KuACTlVyM923_7f5HOmz7g-1; Tue, 04 Aug 2026 00:26:46 -0400 X-MC-Unique: KuACTlVyM923_7f5HOmz7g-1 X-Mimecast-MFC-AGG-ID: KuACTlVyM923_7f5HOmz7g_1785817605 Received: by mail-lf1-f72.google.com with SMTP id 2adb3069b0e04-5b2d7c7ec78so1197261e87.2 for ; Mon, 03 Aug 2026 21:26:46 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=google; t=1785817605; x=1786422405; darn=vger.kernel.org; h=content-transfer-encoding:content-type:mime-version:message-id:date :subject:cc:to:from:from:to:cc:subject:date:message-id:reply-to :content-type; bh=NA4GkWrTTacC5SlKbIYSTdyMjm/Le+vBS1p2iRoL4e4=; b=DUKKp3DjVnKXLoDBvCy4qoIHli3QUp79ccsWeJRH9zO3+h9ygdUrvV1w8xtS+o8Ret y4owR5HnUnOdV/QHYIdVqJw8moSrZIM0PDhstHH7DhD4LHEiE7kEDQMN8O5klBNqDmmL gWPk0xZZ8z5AbCITrmiq6hoo5MAHWuJ1+o+fY3SgKWhfUtiJcHtcd2WrTjaLvRK4jeID 5zNVkie2dqnIfOxv/mtmW4sTjYL40N7LKwLo7CB4V3+RVWsJQUdKk75QhUVqmBuRed7t AH6biNiF8+aWWatAJ7gOL3K2bS9q4lFFh8tqj57OetpPwGuqOBnHcbInkGXkpbWFfQ4M Bp0Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1785817605; x=1786422405; h=content-transfer-encoding:content-type: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:content-type; bh=NA4GkWrTTacC5SlKbIYSTdyMjm/Le+vBS1p2iRoL4e4=; b=cfZvVgFZy1DQI1Kcx1l0BbT9ktwZSvoElWJJkVlQ1dqBfbHvlir4rJIMk/kNmrbCMp YMz5xCYHR6M55YLamZRnHWWk//d4QTochW/90K+0A41GFQ2o6+70cTkmjCpVb5mH/otV E7xAaoRShpqjBd2qrIggFE11Rj99pE9MmBHWE6VfWdh6soF7lRMyXdkGiTT4gBgP4sgE MlAjyvyGUdc0btftOkMZJLHrnB0YhFc75450IOO7f0xfeyaaw7Yzw8H8eoTjg64tL8mb zKyuUZ1jns2sNQ0HTfLk5On0EhxP41ckApkXqpY+BmgvN6vCm5dOLvqEyxgIo2AibZMt KkIA== X-Forwarded-Encrypted: i=1; AHgh+Rqi66dX69Wmt9X+UyQSCfJgbxtnOcIo5sTRe3evGe12Rpg3yyCpUGP0+ml1rpfwL0u68BK+ly2t7Tha1Hw=@vger.kernel.org X-Gm-Message-State: AOJu0YxQTvoEdJXoglROfwAU0lowEU3JK2NzNTnHulfWvh0LfLRaChAa o0tJs1zTIjkEjTH+YG9rwxOJ5n7GUnd3lF5BTyrxfz6YYZE1Pt9VWlSHt753Mc0RBrPSnhUzAa/ BEoYJX4xqnj2fEyOvRCZHacbDR7fDhM7F4+yAG0LB+Isj575Hu3iJSF3RnuWqWx/u X-Gm-Gg: AR+sD13TqGz5R8E0QfJqnGcOCk/JIk4xJNlX/Nx5UO1/D3lPlFeeQ1sWCfbmNq1Um8l yfj0jQVANDORhEwrRbVTRNK+Wiwa0FhrInE883sfjfHLjVHozCgnyJsVl0uoUsTV/67FB9t1eSh FCuyTGkWTH3ooBq+uq/RM8Ly1koh+Viksv1HL3RwXZn+sJ+I+WyIWabeuyR7rW1I0piRZUvAAEM k1YefHX3zjkvdvuo704iq+c30z4O62hOtYSxYFYrr765cdM24o8KoNA3dIipypuRTNx5/lNWbH+ RQODuqSQMuu5Q/AJui784lbkLmEi5jcgUkmPqbpmR3rvXzTtZHEeRHOaD36q6pyiPLPXWaYd+0R aIH4n/+zug7J8ZNP6 X-Received: by 2002:a05:6512:31d4:b0:5b2:ebd0:8d10 with SMTP id 2adb3069b0e04-5b2ebd08e4bmr1109584e87.42.1785817604853; Mon, 03 Aug 2026 21:26:44 -0700 (PDT) X-Received: by 2002:a05:6512:31d4:b0:5b2:ebd0:8d10 with SMTP id 2adb3069b0e04-5b2ebd08e4bmr1109576e87.42.1785817604342; Mon, 03 Aug 2026 21:26:44 -0700 (PDT) Received: from fedora (85-23-51-1.bb.dnainternet.fi. [85.23.51.1]) by smtp.gmail.com with ESMTPSA id 2adb3069b0e04-5b2e23c46c4sm2311351e87.17.2026.08.03.21.26.41 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 03 Aug 2026 21:26:42 -0700 (PDT) From: mpenttil@redhat.com To: linux-mm@kvack.org Cc: dri-devel@lists.freedesktop.org, intel-xe@lists.freedesktop.org, 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 , Andrew Morton , Lorenzo Stoakes , "Liam R. Howlett" , Vlastimil Babka , Mike Rapoport , Suren Baghdasaryan , Michal Hocko Subject: [PATCH v13 00/11] migrate on fault for device pages Date: Tue, 4 Aug 2026 07:26:20 +0300 Message-ID: <20260804042631.2175585-1-mpenttil@redhat.com> X-Mailer: git-send-email 2.55.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 since v12: - fixed Lorenzo's mail address - made hmm_pfns_fill() check for PMD size region for hole migration - added lazy mmu mode for migrate - check pte_present() before flush_cache_page() an folio_mark_dirty() - allowed the normal migration path to specify write access (fault) - split and refactored the series more. Also reduced ifdeffery in hmm.c with static inline functions - handled rollback for cases where the page table has been cleared and/or replaced with something else like PMD leaf 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/ - v7: https://lore.kernel.org/linux-mm/20260330115611.347988-1-mpenttil@redhat.com/ - v8: https://lore.kernel.org/linux-mm/20260414041226.1539439-1-mpenttil@redhat.com/ - v9: https://lore.kernel.org/linux-mm/20260505051658.2219537-1-mpenttil@redhat.com/ - v10: https://lore.kernel.org/linux-mm/20260505184421.2324798-1-mpenttil@redhat.com/ - v11: https://lore.kernel.org/linux-mm/20260525050830.100254-1-mpenttil@redhat.com/ - v12: https://lore.kernel.org/linux-mm/20260525084524.139868-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 Cc: Andrew Morton Cc: Lorenzo Stoakes Cc: "Liam R. Howlett" Cc: Vlastimil Babka Cc: Mike Rapoport Cc: Suren Baghdasaryan Cc: Michal Hocko Mika Penttilä (11): mm/Kconfig: changes for migrate on fault for device pages mm: add helper to convert HMM pfn to migrate pfn mm/hmm: preparations for HMM to participate in migration mm/hmm: do the plumbing for HMM to participate in migration mm/hmm: implement folio split for migrate needs in HMM pagewalk mm/hmm: migrate collection in HMM pagewalk - pte level mm/hmm: migrate collection in HMM pagewalk - pmd level mm/hmm: add lazy MMU mode support for migration in HMM pagewalk mm/hmm: implement rollback for device page migration in HMM pagewalk mm: enable device page migration from HMM pagewalk lib/test_hmm: add a new testcase for the migrate on fault include/linux/hmm.h | 41 +- include/linux/migrate.h | 54 +- lib/test_hmm.c | 132 +++- lib/test_hmm_uapi.h | 19 +- mm/Kconfig | 2 + mm/hmm.c | 950 +++++++++++++++++++++++-- mm/migrate_device.c | 588 +++------------ tools/testing/selftests/mm/hmm-tests.c | 54 ++ 8 files changed, 1260 insertions(+), 580 deletions(-) drm-tip base-commit: c2d24e2eda55d74743f0d99a98805bf0b596beef -- 2.55.0