From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from gabe.freedesktop.org (gabe.freedesktop.org [131.252.210.177]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 3C648C55ABC for ; Tue, 4 Aug 2026 04:27:22 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id D1D8510E854; Tue, 4 Aug 2026 04:27:21 +0000 (UTC) Authentication-Results: gabe.freedesktop.org; dkim=pass (1024-bit key; unprotected) header.d=redhat.com header.i=@redhat.com header.b="F7L4B4yW"; dkim-atps=neutral Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by gabe.freedesktop.org (Postfix) with ESMTPS id C5BE210E83D for ; Tue, 4 Aug 2026 04:27:09 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1785817629; 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=Bp/HOa9o+BFxZP9Tl5+LymilN/hYIUdOgGYp2AmAGQw=; b=F7L4B4yWsziYjdGlZIkOo0oLk243WQAOoDlkIaW2oXK4mGqQT3FvNoDuZ9M2dVQieINufK BHyngE0DbaXTSSFYchrrX4hNA/TozG4R5zRumWjcmcQvNV2dp8CrwrqvKmq46c72sSvmwx PAGExBnn6ldtA5sve50tTeF/wNkWI1g= 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-619-n0rZjyX7PuuOmVbO4aY2Zg-1; Tue, 04 Aug 2026 00:27:06 -0400 X-MC-Unique: n0rZjyX7PuuOmVbO4aY2Zg-1 X-Mimecast-MFC-AGG-ID: n0rZjyX7PuuOmVbO4aY2Zg_1785817624 Received: by mail-lf1-f72.google.com with SMTP id 2adb3069b0e04-5aec6240ddbso2424964e87.3 for ; Mon, 03 Aug 2026 21:27:05 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1785817624; x=1786422424; 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=Bp/HOa9o+BFxZP9Tl5+LymilN/hYIUdOgGYp2AmAGQw=; b=ek5VHV86ShaALA0OY1tL669+OCliq82SdDxYv6VfTH/VeWDukmWTIXnCBNmyrt2gO4 U06N/ITq3EF83eYSeDwRx/EnFjZj1NYnH3GwkoTYvCL/HV5QyQnu/jFDZMO5wUgmRqYx 5yaKsoe7wCEYjY8rHAkoFTGmBPslcw4oBpkSw8DbP6NkmOH3rEyOPgXvxOoqmKG8vtNJ SDGxbDaM2dqbc5Q1E2P4hIyrAgtMHRTarwp2C5Tg3PgHI1YD7bIXvD349SfI+dOn7lpl vnQjExfKMwf5jDc+fTUB5ZPHLRl+Qz+kHSV80bKIHIhyi/an0U6OP0ssH2/db2oEHuEj fAMw== X-Forwarded-Encrypted: i=1; AHgh+Rpcb3trSsWjXsGs8S/J14lrmju3tfdEWiaM3Bd+o1QtLDLQQqXgBZm1heRMitweeMUzA8txEkjqhQ==@lists.freedesktop.org X-Gm-Message-State: AOJu0YyARJZmMlKYNtr/1Sm3x6DNcwUDmMGAX3gpm5Y7hMSUiFcxgTHZ l/aJYVRpnICOQ6Ro0+ps7cY4jeUOIbaw2AFAtKgrC6+ZthoKPmSwLHOEN33eaw8SBHXQ79mKYJ0 r049DcuVLQYE3ociSSuyOQVS2f7fQ0XAbCsUKlldhY4NyXCWT3BrG+Hn/800VKbGbo9w= X-Gm-Gg: AR+sD12pjH9OdsTWgSyNjng1r38c9ZKFnMMjTp6WpRaeXdRgINQ8q14JBNwl38Hu4R4 n6MRTBc1qVYoxGjt7XXGERZa5ePF1p1XpuLm0nMO/ktOg2RZLsr5rqMCGMcxcYUKBN9KuXx8a9g rI9AoQ791GBiqsDhmtjGElG5eZD9cqgFpOAAJtNBLwhiqnsBuerlxm5ilQkM/fY0qPk2s/vHdTm diu3w7SUNMYi3P7wIB708L1QtkFb3DeqdoA+Keu9hS9qy+PzEtMGpW6cGxN+sxjSr6yFJeNZLSt Dz+zKqiWKO8qsj/JZd710ln6rgcVUEG8JnuLgIjA0QWuxvv9oVWMJ9+oyo0QY7QZtMPRPWFPAAN tJADxFsJfV4zv7UDy X-Received: by 2002:a05:6512:63cb:b0:5b2:b85d:4802 with SMTP id 2adb3069b0e04-5b2e4f19bb8mr1748240e87.4.1785817624362; Mon, 03 Aug 2026 21:27:04 -0700 (PDT) X-Received: by 2002:a05:6512:63cb:b0:5b2:b85d:4802 with SMTP id 2adb3069b0e04-5b2e4f19bb8mr1748235e87.4.1785817623922; Mon, 03 Aug 2026 21:27:03 -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.59 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 03 Aug 2026 21:27:00 -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 03/11] mm/hmm: preparations for HMM to participate in migration Date: Tue, 4 Aug 2026 07:26:23 +0300 Message-ID: <20260804042631.2175585-4-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> MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-MFC-PROC-ID: eIh7TDmafT7_2jqmEsK9GFbl5uI82PxKcJY9G4Qfwjw_1785817624 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-BeenThere: intel-xe@lists.freedesktop.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Intel Xe graphics driver List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: intel-xe-bounces@lists.freedesktop.org Sender: "Intel-xe" From: Mika Penttilä For migration to happen after hmm_range_fault(), the vma has to retrieved. Luckily, pagewalk already resolves that for us, so just have to save it for further use. For migration, mmap_lock has to be hold for the whole operation, so vma stays stable. Take care also of firing the mmu_notifier when migrating. Also, prepare for differencies of how mm_struct * is retrieved in fault and migrate_vma paths. 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ä --- include/linux/migrate.h | 45 ++++++++++++++++++++- lib/test_hmm.c | 2 +- mm/hmm.c | 86 ++++++++++++++++++++++++++++++++++++++--- 3 files changed, 125 insertions(+), 8 deletions(-) diff --git a/include/linux/migrate.h b/include/linux/migrate.h index 425ab5242da0..7380a89756ee 100644 --- a/include/linux/migrate.h +++ b/include/linux/migrate.h @@ -106,6 +106,28 @@ static inline void softleaf_entry_wait_on_locked(softleaf_t entry, spinlock_t *p spin_unlock(ptl); } +enum migrate_vma_info { + MIGRATE_VMA_SELECT_NONE = 0, + MIGRATE_VMA_SELECT_COMPOUND = MIGRATE_VMA_SELECT_NONE, +}; + +static inline enum migrate_vma_info hmm_select_migrate(struct hmm_range *range) +{ + return MIGRATE_VMA_SELECT_NONE; +} + +static inline void hmm_fill_migrate_vma(struct hmm_range *range, + struct vm_area_struct *vma, + unsigned long start, + unsigned long end) +{ +} + +static inline struct mm_struct *hmm_range_fault_mm(struct hmm_range *range) +{ + return range->notifier->mm; +} + #endif /* CONFIG_MIGRATION */ #ifdef CONFIG_NUMA_BALANCING @@ -149,7 +171,7 @@ static inline unsigned long migrate_pfn(unsigned long pfn) return (pfn << MIGRATE_PFN_SHIFT) | MIGRATE_PFN_VALID; } -enum migrate_vma_direction { +enum migrate_vma_info { MIGRATE_VMA_SELECT_SYSTEM = 1 << 0, MIGRATE_VMA_SELECT_DEVICE_PRIVATE = 1 << 1, MIGRATE_VMA_SELECT_DEVICE_COHERENT = 1 << 2, @@ -191,6 +213,27 @@ struct migrate_vma { struct page *fault_page; }; +// TODO: enable migration +static inline enum migrate_vma_info hmm_select_migrate(struct hmm_range *range) +{ + return 0; +} + +static inline struct mm_struct *hmm_range_fault_mm(struct hmm_range *range) +{ + return range->notifier ? range->notifier->mm : range->migrate->vma->vm_mm; +} + +static inline void hmm_fill_migrate_vma(struct hmm_range *range, + struct vm_area_struct *vma, + unsigned long start, + unsigned long end) +{ + range->migrate->vma = vma; + range->migrate->start = start; + range->migrate->end = end; +} + int migrate_vma_setup(struct migrate_vma *args); void migrate_vma_pages(struct migrate_vma *migrate); void migrate_vma_finalize(struct migrate_vma *migrate); diff --git a/lib/test_hmm.c b/lib/test_hmm.c index c4adbf98fac7..79cb7487d211 100644 --- a/lib/test_hmm.c +++ b/lib/test_hmm.c @@ -145,7 +145,7 @@ static bool dmirror_is_private_zone(struct dmirror_device *mdevice) HMM_DMIRROR_MEMORY_DEVICE_PRIVATE); } -static enum migrate_vma_direction +static enum migrate_vma_info dmirror_select_device(struct dmirror *dmirror) { return (dmirror->mdevice->zone_device_type == diff --git a/mm/hmm.c b/mm/hmm.c index c72c9ddfdb2f..637c00188b3e 100644 --- a/mm/hmm.c +++ b/mm/hmm.c @@ -20,6 +20,7 @@ #include #include #include +#include #include #include #include @@ -27,12 +28,17 @@ #include #include #include +#include #include "internal.h" struct hmm_vma_walk { - struct hmm_range *range; - unsigned long last; + struct mmu_notifier_range mmu_range; + struct vm_area_struct *vma; + struct hmm_range *range; + unsigned long start; + unsigned long end; + unsigned long last; }; enum { @@ -393,6 +399,57 @@ static int hmm_vma_handle_absent_pmd(struct mm_walk *walk, unsigned long start, } #endif /* CONFIG_ARCH_ENABLE_THP_MIGRATION */ +static int hmm_vma_capture_migrate_range(unsigned long start, + unsigned long end, + struct mm_walk *walk) +{ + struct hmm_vma_walk *hmm_vma_walk = walk->private; + struct hmm_range *range = hmm_vma_walk->range; + + if (!hmm_select_migrate(range)) + return 0; + + if (hmm_vma_walk->vma && (hmm_vma_walk->vma != walk->vma)) + return -ERANGE; + + hmm_vma_walk->vma = walk->vma; + hmm_vma_walk->start = start; + hmm_vma_walk->end = end; + + if (end - start > range->end - range->start) + return -ERANGE; + + if (!hmm_vma_walk->mmu_range.owner) { + mmu_notifier_range_init_owner(&hmm_vma_walk->mmu_range, MMU_NOTIFY_MIGRATE, 0, + walk->vma->vm_mm, start, end, + range->dev_private_owner); + mmu_notifier_invalidate_range_start(&hmm_vma_walk->mmu_range); + } + + return 0; +} + +static void hmm_vma_post_range_fault(struct hmm_vma_walk *hmm_vma_walk) +{ + + struct hmm_range *range = hmm_vma_walk->range; + + if (hmm_select_migrate(range) && + hmm_vma_walk->mmu_range.owner) { + /* + * The migrate_vma path has the following initialized, + * so take care of fault path below. + */ + if (range->notifier) { + hmm_fill_migrate_vma(range, + hmm_vma_walk->vma, + hmm_vma_walk->start, + hmm_vma_walk->end); + } + mmu_notifier_invalidate_range_end(&hmm_vma_walk->mmu_range); + } +} + static int hmm_vma_walk_pmd(pmd_t *pmdp, unsigned long start, unsigned long end, @@ -600,6 +657,11 @@ static int hmm_vma_walk_test(unsigned long start, unsigned long end, struct hmm_vma_walk *hmm_vma_walk = walk->private; struct hmm_range *range = hmm_vma_walk->range; struct vm_area_struct *vma = walk->vma; + int r; + + r = hmm_vma_capture_migrate_range(start, end, walk); + if (r) + return r; if (!(vma->vm_flags & (VM_IO | VM_PFNMAP)) && vma->vm_flags & VM_READ) @@ -662,16 +724,25 @@ int hmm_range_fault(struct hmm_range *range) .range = range, .last = range->start, }; - struct mm_struct *mm = range->notifier->mm; + /* + * Could be serving a device fault or come from migrate + * entry point. For the former we have not resolved the vma + * yet, and the latter we don't have a notifier (but have a vma). + * + */ + struct mm_struct *mm = hmm_range_fault_mm(range); int ret; mmap_assert_locked(mm); do { /* If range is no longer valid force retry. */ - if (mmu_interval_check_retry(range->notifier, - range->notifier_seq)) - return -EBUSY; + if (range->notifier && mmu_interval_check_retry(range->notifier, + range->notifier_seq)) { + ret = -EBUSY; + break; + } + ret = walk_page_range(mm, hmm_vma_walk.last, range->end, &hmm_walk_ops, &hmm_vma_walk); /* @@ -681,6 +752,9 @@ int hmm_range_fault(struct hmm_range *range) * output, and all >= are still at their input values. */ } while (ret == -EBUSY); + + hmm_vma_post_range_fault(&hmm_vma_walk); + return ret; } EXPORT_SYMBOL(hmm_range_fault); -- 2.55.0