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.129.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 24CBD33291C for ; Mon, 26 Jan 2026 11:19:58 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.129.124 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1769426401; cv=none; b=igw7eb/P+7XefUju+mj79M4r7qT+nrLPwFEVpE/7lkc5dmJ0MvjyeFsHVyVc/5n/2KMdjVhO2bWrgj5sLo2HUemxSi5KClZVC8l46bM/+KsolQ/adlqV0o12Tree2jMUF1DQFB6+D24xrztJrOZsT/HyWlBAhwqdV8agvOevZMk= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1769426401; c=relaxed/simple; bh=jtSIpxacixQdD27hTdmMyP8JJeOzlUfgjqikX9+B6h4=; h=From:To:Cc:Subject:Date:Message-ID:MIME-Version:Content-Type; b=VlI2SuQxWTL+yketFIxTv6WQjW1bbAliJkTlVdTiTJodOfNpzYjia8xswv5pMSvXB7sLTbYshC7suTeP8IQPGpC7GJNw9kNqr7vi292w8KFMlx5oLJiuXXpwb4WPIMc6I0J/3VPDeC85FyFMdlscC52djhDAZbWZlg0yKV3jTCA= 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=N/BIJ2kD; dkim=pass (2048-bit key) header.d=redhat.com header.i=@redhat.com header.b=rgB3+4tY; arc=none smtp.client-ip=170.10.129.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="N/BIJ2kD"; dkim=pass (2048-bit key) header.d=redhat.com header.i=@redhat.com header.b="rgB3+4tY" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1769426397; 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=yM69Fp+p/qvQDMim1NNKj1peSV7uhPcp8MJ7UDoVv8g=; b=N/BIJ2kDMngGYtrUOUh1NxyLN9VoO4CgMawLX5CNdvNedtmzoNKigX/QB1tsi84UonC0fu jNz7Z6HsgLQuM2T231YqbcrGxDJEJ55whMFqZYmhXgyyn3zH24rorH2NmDJON7YrrJ3SuT QvGYHKeCTJTt69jN68plO0Zkrxj2Dh8= 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-397-we2LZq3WNx6PTPouEb3jRA-1; Mon, 26 Jan 2026 06:19:55 -0500 X-MC-Unique: we2LZq3WNx6PTPouEb3jRA-1 X-Mimecast-MFC-AGG-ID: we2LZq3WNx6PTPouEb3jRA_1769426394 Received: by mail-lf1-f71.google.com with SMTP id 2adb3069b0e04-59b70088327so2609103e87.1 for ; Mon, 26 Jan 2026 03:19:55 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=google; t=1769426394; x=1770031194; 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=yM69Fp+p/qvQDMim1NNKj1peSV7uhPcp8MJ7UDoVv8g=; b=rgB3+4tYA4l5cQzE/QPS22hyIgnCMFGGwTftare0lw1ITh3+rkF07s1jfcc2OOOeCo 740Mqe0e9GRvhKRIaQjR6i3SAITFGznC2zeY74eDKAGNGE0DWtEtOx1PuQ/nwGMfwYbW x+1VgZdJdz6DZEVwutYVzGHsTYcMp9oGIX0ufanRlvaHZ8UOzF4LcidHEwBRCKjlaQ9U 4P8CH2/HsjbtEgBn0oTOFTgbOZQD8vBIo5rF/wiceQtDjWRmDr1Q6fH0/Tx5ecKtoZ9N S45dXJnTBaj30AWGsdpa+N9NT1jpxkRsuwtfNrjl46efZ1NctsshLcqFO1toTQZd1BTt Y/NA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1769426394; x=1770031194; 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=yM69Fp+p/qvQDMim1NNKj1peSV7uhPcp8MJ7UDoVv8g=; b=Z7ORwd9tYOHX2ddcWDzcwvDOvyg3rwkhaYJzOwyjQBlzqQHgxuEv1dYZzZeacOGHZM e3fLsM8Vue6sMQUXGuQGYp48tPHmwM7GZ2mL6xNPqg+BRC5hCJWNWlHnf2FZN0Sa3gLI u4572BLRFFbToYh8IJxkN3OvTD1jztqSimxyhQt/NPJu+QkX7L3fl11i8Qtyy4KXTAsd QZmFwsDetwmllbod+C6peIMIC0SUj0i7qHYB+j1gv2BDS2ISiBdkAZuAi8MA/QhaOc+J bgbuXJ5OQq+BxjcFSOo0u+xfF/d//hzWeVrF9wNVuRilUa1Y8K5cSpF37vV5S3cLzIRJ XCNw== X-Gm-Message-State: AOJu0Yz/OlaP6JG9Mp4YOmJhEVVPJK1Bgmth8/zeBreMHnD6iiD2Yhm1 CVVz5evC4QgJZN+OOmxRuV/5o9uZ2uZXA7ek32LJgiR5HYKfgyzMFhUK9ur1Y18A5URj4Ry60SL Ggd98Lr6qmj5ivi62MwYu1BuEz5KOP0U2lAGzyOX3S/5o2copbQP3L/GFRbIXOPWJkLmnu07A X-Gm-Gg: AZuq6aJQmOlxATBZUmxPoi/i+VcdW48mlAYse0c3E5COmxHOFu9mIksMG4Lar1bcpxL 0DFXMQqqKIb7fYWP6j37IjrKag2HjRNamNJVkqabcSYsdVqZdJcwLBPrQIEZRSJePrP/oC4qa5Z yuIpdWdp5wCP+OwOg1KBnecr+7RKl42D5qlgxaSO+WqquFaUXe0goxkxAMUVvHg1DokHs8Ohrbs 9s93j6P3v8ZZ780b3iXi0OLOZ6WtaBjkTqy2OvkgQlePiiYDkxjJDxWnvXzUhUu4ynEWNiJG4CL R/APQhv5EDwEjSW1gti+whlU98CZ1nsQnBoEXAX4uRNVX/unIKqC+ARDBdvKp7UWGKrmG2puNCJ cFrPOKNoEP9dVH9fNSX6cO06M X-Received: by 2002:a05:6512:3995:b0:59d:e93e:a8ae with SMTP id 2adb3069b0e04-59df3a3a34emr1437979e87.47.1769426393919; Mon, 26 Jan 2026 03:19:53 -0800 (PST) X-Received: by 2002:a05:6512:3995:b0:59d:e93e:a8ae with SMTP id 2adb3069b0e04-59df3a3a34emr1437961e87.47.1769426393424; Mon, 26 Jan 2026 03:19:53 -0800 (PST) Received: from fedora (85-23-51-1.bb.dnainternet.fi. [85.23.51.1]) by smtp.gmail.com with ESMTPSA id 2adb3069b0e04-59de48e5f76sm2572221e87.23.2026.01.26.03.19.52 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 26 Jan 2026 03:19:53 -0800 (PST) From: mpenttil@redhat.com To: linux-mm@kvack.org Cc: linux-kernel@vger.kernel.org, =?UTF-8?q?Mika=20Penttil=C3=A4?= Subject: [PATCH v3 0/3] Migrate on fault for device pages Date: Mon, 26 Jan 2026 13:19:36 +0200 Message-ID: <20260126111939.1332983-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/20260123062309.23090-1-jniethe@nvidia.com/ plus a small patch to adjust with no problems. 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/ 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/Kconfig | 1 + mm/hmm.c | 801 +++++++++++++++++++++++-- mm/migrate_device.c | 594 +++--------------- tools/testing/selftests/mm/hmm-tests.c | 54 ++ 8 files changed, 1032 insertions(+), 583 deletions(-) base-commit: 63804fed149a6750ffd28610c5c1c98cce6bd377 -- 2.50.0