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 6904A33ADB3 for ; Tue, 4 Aug 2026 04:27:24 +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=1785817646; cv=none; b=g204i8Qh/TDETn8UZfCoL6eWeHwoQ+v/Y8rA4PpB0KGud36yv+uW26skQHORqDNERWomAALXdOK787P0nqFU0MJo4wtI/6n5m6TpAE00sWaTL0S9Vhmx6Uvcn8ugwwtaez0ykES8/nc5Q75jmC2pN3t7RR2BKIqj9tlWDnGs+Bo= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1785817646; c=relaxed/simple; bh=d14mUJPIWhmWzAEhhRR5qBtIt+cpauPTZgkwIHlJNKw=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=dVt3uhJf5jUIMsBStbDzKWdkIESRpQez8mq3sIYjWC7LiucR9w6WlBQpey7++4rIPPsdAYR3XWdkwJz8tZFLHroC+F7/fcoqvwygI0XolJw/Uhh5ItiLVOGSTcZ6yxOLko9wPlmv/B+JEq6tcE/5I25XeVw1KegeACb3MqDE91I= 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=XA4oCeQu; dkim=pass (2048-bit key) header.d=redhat.com header.i=@redhat.com header.b=agJRxFMr; 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="XA4oCeQu"; dkim=pass (2048-bit key) header.d=redhat.com header.i=@redhat.com header.b="agJRxFMr" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1785817643; 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: in-reply-to:in-reply-to:references:references; bh=kzj2oXiMIPJc8DDmgyLO85pt6xHacr5zfN3Pc28UGUc=; b=XA4oCeQuBAJUV1zdBJCq6Tclzu0hk3CkSuMilqiTY2sMZ5NYEh5ujTMtHyZYhmGvVKtwM3 zoUutJEMKjrVuggkPkoHPNwQVN1KIFOdfBx2AJTUWlHEpQvkshftJ0ME7m8R8OJHiZylTR wepq6Yb1rUihbcR+cPYbjzCWx1IQfmo= Received: from mail-lf1-f69.google.com (mail-lf1-f69.google.com [209.85.167.69]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-103-G2qEPA72NvWBvPwjYBAejw-1; Tue, 04 Aug 2026 00:27:21 -0400 X-MC-Unique: G2qEPA72NvWBvPwjYBAejw-1 X-Mimecast-MFC-AGG-ID: G2qEPA72NvWBvPwjYBAejw_1785817640 Received: by mail-lf1-f69.google.com with SMTP id 2adb3069b0e04-5b14d51dae1so1537101e87.3 for ; Mon, 03 Aug 2026 21:27:21 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=google; t=1785817640; x=1786422440; darn=vger.kernel.org; h=content-transfer-encoding:content-type:mime-version:references :in-reply-to:message-id:date:subject:cc:to:from:from:to:cc:subject :date:message-id:reply-to:content-type; bh=kzj2oXiMIPJc8DDmgyLO85pt6xHacr5zfN3Pc28UGUc=; b=agJRxFMrwlyGfgOBXPrTehQHvzBroM/XcR9H//qOAS0Kb7SJU/sZ1sCZZDlaV2YzuI F0FsIyNiMZ6UC3uF4fI/hg0Y5UDvxQ0dOmIEB78SEu6NqkMzGo3TmAb78qyjE5NqB7nN txvtVZpzxvHwm93zo00jmZvcWkKEkRobaiv1hb8XzcbXKJXPrZiIL8xYJlIP2kE2DGGK InIohrpyT8kfEBRcrPJRHxqN0ydz48/DaQ4prJYdgv00lGky2nHQ86dweGNH2mgYZpEp +pU7rmRc0Kf1O1uCuWOKE3tDLdmv2nsr91D1n2770a+gixui1s+Z14uZPIrbBFofDB60 DYIg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1785817640; x=1786422440; h=content-transfer-encoding:content-type:mime-version:references :in-reply-to: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=kzj2oXiMIPJc8DDmgyLO85pt6xHacr5zfN3Pc28UGUc=; b=keCpQC3wNW8MwsPMxq66kd+rNtzGAd9/7dmU0cRq4qu+oCRtP/01TcRUIyN0FIo+Dk NhCYxJnm5egcOWxd4dsuZKWGBEZAFCMoqd85uhSXgFfxzaKiUBdMIXjrTmvoTPfiY/Tc sa0uwXkX4Fmw+m6hdGVzkzMg8qIe/wV+1sK/TYoXhHB43ujzDvsfdBXh8VJk1hE+G4uY uvE6QG7cyXmKbzel0moAz+cwyB/kEtNqHWS7xchgihcUa8AJCr6KXuIiHsOZfdLLaVbK aml/7IzOknZdGFr/We2dKj9P72zQfJNxArWcOWfnKGLsYw8yqpHb3T+4PBhthsTbDKoZ Rd6w== X-Forwarded-Encrypted: i=1; AHgh+Rouoc6Ehish8EJZMovgytnu6+3GiB9lKxOt4ipAIpDseocc3Wgd8Zf+WIw0u/Cvr4iN1wcYwgiUgapgUYc=@vger.kernel.org X-Gm-Message-State: AOJu0YyL0QWWy6uqcxGRKtRNldvWxv7/9+GrwxmUvsqe01DPbur1z1IY Y2gakOBeUnH3p7uiJtib5gVZoEyryYp+ctaEYEfydYpxscZ2rQl9o/tVv/0AVfG1scNTUQA/lIk N8pbJjabNdtYGauDelUkLCXZtbJ+cUesOYqJeNGUKGLhkda7Nch6fgQo4CWrnvh29 X-Gm-Gg: AR+sD13aZMR2LUDwnyC3wRp07xeydq575uMdsQ4kwj2v/FS8bhSh/Y2sHifwnTmc0vf SEHffK6yg2H1b/sKJc6F9eh9kNB3VGWN7Tbap3dAAkmgqGZkvsCh0R7x5gAWpHnjj8jo3juCHVj xTrVAW2ITLjKIzDfJnc49V7+UOFhidjYQrmY61MWcZV8YkTzvO2UthQRzP4B90Ru5DSMe5WcAWO Xrs5c3qmm3BWsGiG2sTh6FX5dHPhaSmtlaDTO5TZfOpB17N/dQmKg5v1ga2rFkNaHoHeKdt+3aj cVDfW0B4dDvcjwwuIzAxRzud9V9n741onbgOYNw3sPEwJ9/+PbAJda22I2UYw+uZSPDqNTH3t2Q wvqB17XYoApvb/tKN X-Received: by 2002:ac2:532a:0:b0:5b1:56c7:5753 with SMTP id 2adb3069b0e04-5b2e4f8064emr1809690e87.47.1785817640027; Mon, 03 Aug 2026 21:27:20 -0700 (PDT) X-Received: by 2002:ac2:532a:0:b0:5b1:56c7:5753 with SMTP id 2adb3069b0e04-5b2e4f8064emr1809678e87.47.1785817639563; Mon, 03 Aug 2026 21:27:19 -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.27.15 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 03 Aug 2026 21:27:15 -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 06/11] mm/hmm: migrate collection in HMM pagewalk - pte level Date: Tue, 4 Aug 2026 07:26:26 +0300 Message-ID: <20260804042631.2175585-7-mpenttil@redhat.com> X-Mailer: git-send-email 2.55.0 In-Reply-To: <20260804042631.2175585-1-mpenttil@redhat.com> References: <20260804042631.2175585-1-mpenttil@redhat.com> 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ä Implement the needed hmm_vma_handle_migrate_prepare() function which is mostly carried over from migrate_device.c's migrate_vma_collect_pmd() function. Also implement the migrate_vma_split_folio(), for splitting pte mapped large folios. It is also mostly from migrate_device.c, with care taken to reference folio before relasing page table lock. With HMM pagewalk based migration, the idea is that hmm_vma_handle_*() are responsible for faulting, and the pfn collecting part. hmm_vma_handle_migrate_prepare*() do the migration decisions (with HMM_PFN_MIGRATE), possibly split folios, and insert migration ptes/pmds. HMM pagewalk based migration is enabled in later commit, for now now hmm_select_migrate() returns 0. 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ä --- mm/hmm.c | 262 ++++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 261 insertions(+), 1 deletion(-) diff --git a/mm/hmm.c b/mm/hmm.c index 3621d1df51d3..2d902b8bcd58 100644 --- a/mm/hmm.c +++ b/mm/hmm.c @@ -486,6 +486,63 @@ static int hmm_vma_handle_absent_pmd(struct mm_walk *walk, unsigned long start, #endif /* CONFIG_ARCH_ENABLE_THP_MIGRATION */ #ifdef CONFIG_DEVICE_MIGRATION +/** + * migrate_vma_split_folio() - Helper function to split a THP folio + * @folio: the folio to split + * @fault_page: struct page associated with the fault if any + * @hmm_vma_walk: walk in progress + * @ptep: pte_t * for unmap and unlock ptl + * + * Returns 0 on success + */ +static int migrate_vma_split_folio(struct folio *folio, + struct page *fault_page, + struct hmm_vma_walk *hmm_vma_walk, + pte_t *ptep) +{ + int ret; + struct folio *fault_folio = fault_page ? page_folio(fault_page) : NULL; + struct folio *new_fault_folio = NULL; + + if (folio != fault_folio) + folio_get(folio); + + pte_unmap_unlock(ptep, hmm_vma_walk->ptl); + hmm_vma_walk->ptelocked = false; + + if (folio != fault_folio) + folio_lock(folio); + + ret = split_folio(folio); + if (ret) { + if (folio != fault_folio) { + folio_unlock(folio); + folio_put(folio); + } + return ret; + } + + new_fault_folio = fault_page ? page_folio(fault_page) : NULL; + + /* + * Ensure the lock is held on the correct + * folio after the split + */ + if (!new_fault_folio) { + folio_unlock(folio); + folio_put(folio); + } else if (folio != new_fault_folio) { + if (new_fault_folio != fault_folio) { + folio_get(new_fault_folio); + folio_lock(new_fault_folio); + } + folio_unlock(folio); + folio_put(folio); + } + + return 0; +} + static int hmm_vma_handle_migrate_prepare_pmd(const struct mm_walk *walk, pmd_t *pmdp, unsigned long start, @@ -496,6 +553,11 @@ static int hmm_vma_handle_migrate_prepare_pmd(const struct mm_walk *walk, return 0; } +/* + * Install migration entries if migration requested, either from fault + * or migrate paths. + * + */ static int hmm_vma_handle_migrate_prepare(const struct mm_walk *walk, pmd_t *pmdp, pte_t *ptep, @@ -503,8 +565,206 @@ static int hmm_vma_handle_migrate_prepare(const struct mm_walk *walk, unsigned long *hmm_pfn, bool *unmapped) { - // TODO: implement migration entry insertion + struct hmm_vma_walk *hmm_vma_walk = walk->private; + struct hmm_range *range = hmm_vma_walk->range; + struct migrate_vma *migrate = range->migrate; + struct mm_struct *mm = walk->vma->vm_mm; + struct folio *fault_folio = NULL; + enum migrate_vma_info minfo; + struct dev_pagemap *pgmap; + bool anon_exclusive; + struct folio *folio; + unsigned long pfn; + struct page *page; + softleaf_t entry; + pte_t pte, swp_pte; + bool writable = false; + + // Do we want to migrate at all? + minfo = hmm_select_migrate(range); + if (!minfo) + return 0; + + WARN_ON_ONCE(!migrate); + HMM_ASSERT_PTE_LOCKED(hmm_vma_walk, true); + + fault_folio = migrate->fault_page ? + page_folio(migrate->fault_page) : NULL; + + pte = ptep_get(ptep); + + if (pte_none(pte)) { + if (vma_is_anonymous(walk->vma)) { + *hmm_pfn &= HMM_PFN_INOUT_FLAGS; + *hmm_pfn |= HMM_PFN_MIGRATE; + goto out; + } + } + + if (!(hmm_pfn[0] & HMM_PFN_VALID)) + goto out; + + if (!pte_present(pte)) { + /* + * Only care about unaddressable device page special + * page table entry. Other special swap entries are not + * migratable, and we ignore regular swapped page. + */ + entry = softleaf_from_pte(pte); + if (!softleaf_is_device_private(entry)) + goto out; + + if (!(minfo & MIGRATE_VMA_SELECT_DEVICE_PRIVATE)) + goto out; + + page = softleaf_to_page(entry); + folio = page_folio(page); + if (folio->pgmap->owner != migrate->pgmap_owner) + goto out; + + if (folio_test_large(folio)) { + int ret; + + ret = migrate_vma_split_folio(folio, + migrate->fault_page, + hmm_vma_walk, + ptep); + if (ret) + goto out_error; + return -EAGAIN; + } + + pfn = page_to_pfn(page); + if (softleaf_is_device_private_write(entry)) + writable = true; + } else { + pfn = pte_pfn(pte); + if (is_zero_pfn(pfn) && + (minfo & MIGRATE_VMA_SELECT_SYSTEM)) { + *hmm_pfn = HMM_PFN_MIGRATE; + goto out; + } + page = vm_normal_page(walk->vma, addr, pte); + if (page && !is_zone_device_page(page) && + !(minfo & MIGRATE_VMA_SELECT_SYSTEM)) { + goto out; + } else if (page && is_device_coherent_page(page)) { + pgmap = page_pgmap(page); + + if (!(minfo & + MIGRATE_VMA_SELECT_DEVICE_COHERENT) || + pgmap->owner != migrate->pgmap_owner) + goto out; + } + + folio = page ? page_folio(page) : NULL; + if (folio && folio_test_large(folio)) { + int ret; + + ret = migrate_vma_split_folio(folio, + migrate->fault_page, + hmm_vma_walk, + ptep); + if (ret) + goto out_error; + return -EAGAIN; + } + + writable = pte_write(pte); + } + + if (!page || !page->mapping) + goto out; + + /* + * By getting a reference on the folio we pin it and that blocks + * any kind of migration. Side effect is that it "freezes" the + * pte. + * + * We drop this reference after isolating the folio from the lru + * for non device folio (device folio are not on the lru and thus + * can't be dropped from it). + */ + folio = page_folio(page); + folio_get(folio); + + /* + * We rely on folio_trylock() to avoid deadlock between + * concurrent migrations where each is waiting on the others + * folio lock. If we can't immediately lock the folio we fail this + * migration as it is only best effort anyway. + * + * If we can lock the folio it's safe to set up a migration entry + * now. In the common case where the folio is mapped once in a + * single process setting up the migration entry now is an + * optimisation to avoid walking the rmap later with + * try_to_migrate(). + */ + + if (fault_folio == folio || folio_trylock(folio)) { + anon_exclusive = folio_test_anon(folio) && + PageAnonExclusive(page); + + if (pte_present(pte)) + flush_cache_page(walk->vma, addr, pfn); + + if (anon_exclusive) { + pte = ptep_clear_flush(walk->vma, addr, ptep); + + if (folio_try_share_anon_rmap_pte(folio, page)) { + set_pte_at(mm, addr, ptep, pte); + folio_unlock(folio); + folio_put(folio); + goto out; + } + } else { + pte = ptep_get_and_clear(mm, addr, ptep); + } + + if (pte_present(pte) && pte_dirty(pte)) + folio_mark_dirty(folio); + + /* Setup special migration page table entry */ + if (writable) + entry = make_writable_migration_entry(pfn); + else if (anon_exclusive) + entry = make_readable_exclusive_migration_entry(pfn); + else + entry = make_readable_migration_entry(pfn); + + if (pte_present(pte)) { + if (pte_young(pte)) + entry = make_migration_entry_young(entry); + if (pte_dirty(pte)) + entry = make_migration_entry_dirty(entry); + } + + swp_pte = swp_entry_to_pte(entry); + if (pte_present(pte)) { + if (pte_soft_dirty(pte)) + swp_pte = pte_swp_mksoft_dirty(swp_pte); + if (pte_uffd_wp(pte)) + swp_pte = pte_swp_mkuffd_wp(swp_pte); + } else { + if (pte_swp_soft_dirty(pte)) + swp_pte = pte_swp_mksoft_dirty(swp_pte); + if (pte_swp_uffd_wp(pte)) + swp_pte = pte_swp_mkuffd_wp(swp_pte); + } + + set_pte_at(mm, addr, ptep, swp_pte); + folio_remove_rmap_pte(folio, page, walk->vma); + folio_put(folio); + *hmm_pfn |= HMM_PFN_MIGRATE; + if (pte_present(pte)) + *unmapped = true; + } else { + folio_put(folio); + } +out: return 0; +out_error: + return -EFAULT; } static int hmm_vma_walk_split(pmd_t *pmdp, -- 2.55.0