From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=170.10.133.124 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1792562733; b=I6HsoSQGGQyfooXtFrvQWV4x5xaPjfL3y3090+v95uygSp7/5aBnkGBxLneAPPt4UtOT duGlSP8Nmfk/aOXrumVRQnBgL7mWLu6k4hQVfZecKMDhf2LXcYJoS3MeD10JA9Qw4hRut mjTxXogYULUzbBz1FEItDhBQo7Of7aZHvPCMBfRpfh4WN5Lr4c2AD9BCr5QfLU+zg0Vzs 7Ixil7meTIIiH9Fp2RKym+zRic2IbiZzgI/pwCfJley4WQOBWUhUpjJLOejW8Qu7VkdHJ BDSNp5PS00iWFXi7I/vgOYJFdGN8I/ewxLBM8E/4mV2HRbXPDc3e1CTL/unWwRT63gw== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1792562733; h=DMARC-Filter:DKIM-Signature:Received:X-MC-Unique: X-Mimecast-MFC-AGG-ID:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Forwarded-Encrypted:X-Gm-Message-State:X-Gm-Gg:X-Received: X-Received:Received:From:To:Cc:Subject:Date:Message-ID:X-Mailer: In-Reply-To:References:MIME-Version:Content-Type: Content-Transfer-Encoding; bh=SfJxNxkIc73uzTBEX6ZvvDehbex1Gl1LW4EuriUAtGk=; b=iMZ9FwyAvL448/oP2cPPZA+DFMAqmNtH4swCclVk3LOC9Cqm9cqlYz0jAixG2oZ1MUXe bS/uAlMCsDuv+JAKifkXTxFkxLr93dBKoeCmGZMizwzEGDyNSBYZDLqamTI6rhE//Nc0s bwzPi5sdrdbsf1ErN+ryu0J4Q/OkO7hNJUV3iZuNgmwJO/5d4xy0MiGf9X9GihQJibBgR 1X97tLK/SWwHWJvIkfVnXD0vizpn749J/ctWzQpKAKCaPpfhd6UqAN3GzKQ8HtJT+mZxe CCvxKnPt3LDsK+/+Upf7EULtHVleZleV03MgsAI6NSU5z9wpLSJu6n4cG4rp2KieY8g== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=UwSQFpV0; dkim=pass header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=google header.b=ED5oqnRH; arc=none smtp.remote-ip=170.10.133.124 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 520E360A2624 for ; Wed, 21 Oct 2026 06:05:32 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 520E360A2624 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, unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=UwSQFpV0; dkim=pass (2048-bit key, unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=google header.b=ED5oqnRH DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1792562731; 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=SfJxNxkIc73uzTBEX6ZvvDehbex1Gl1LW4EuriUAtGk=; b=UwSQFpV0frp9VoCdRgm4j4xeRL7sAFhdncNOVORp7Tt1Ospa3iDuN1RSKYFNvMZjyq117U U8hSWEWz03/b5TwI8Z2369gf39uam1krx+OpVtQOmRtKlMK50U2WQW41foApKppaHrLkfF 5wFl1bj2dyzxZ0cvhhXN3lwtZ8+94eY= Received: from mail-wr1-f72.google.com (mail-wr1-f72.google.com [209.85.221.72]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-578-kKmptY14OtKEKqv90WF8hw-1; Wed, 21 Oct 2026 06:05:29 +0000 X-MC-Unique: kKmptY14OtKEKqv90WF8hw-1 X-Mimecast-MFC-AGG-ID: kKmptY14OtKEKqv90WF8hw_1792562728 Received: by mail-wr1-f72.google.com with SMTP id ffacd0b85a97d-48b0a199b20so293385f8f.1 for ; Tue, 20 Oct 2026 23:05:29 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=google; t=1792562728; x=1793167528; 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=SfJxNxkIc73uzTBEX6ZvvDehbex1Gl1LW4EuriUAtGk=; b=ED5oqnRHz9TbQtKhW5IICTsiSycLxO4d3TRh+FkeKemfRgviEcFD1xz3FG348JkCv4 M/4v940WvmqwAd+yFJiYdcDtrSVZ2EyoYGTqI2hHvnrwzQ9GEjes431pd00YqDKdcSOG yKaGdzPkYJ18F53lmetitDb9wtQ7hwFDO4Ct5NeyhO9iIA6nQkMI9hAKIOnOJx+fmyQx aHpFzNWOy3WwRjETyFe+xnaDDuVHvtQ9iZapbk08lueZLrnI5KwZiQjjwh5VrvuuARco 26hiFXfGTu+gM6UXBV89zBP14Pbb9HHBK7Rl5mkMQTQ8WjH2WoyLHcgNl594flnLylhw DUCw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1792562728; x=1793167528; 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=SfJxNxkIc73uzTBEX6ZvvDehbex1Gl1LW4EuriUAtGk=; b=sUyBHkZu3y/GsjJIZIcri263pBg9p+FYylEYAuxRyOncyNf5oeAmLwargWARXWKzAo DO+YfElT+LXiMjHxnze70b9A3GZlssw0yGfoaPri/XrCO/5nwkLBcGBrg7BmEswUy97A VQQp7mvXJmVG1F+v8/qo09qB6XL+CIAcSyTa19IMX17VMie59VZ/abxpLjrC7/OQIHKj 5SL5zandcstD3mm7ptNk1THodA8H81KuxN73Oh6iNuWFWZlTopllhy2GePDOwpcLwsK+ KRPwSssUT6joXYpX/W/PiuDnss3YZwXs+TGlenpTnMexSL7CsTYBLtGOiW31uW0WM3kH imHw== X-Forwarded-Encrypted: i=1; AKwUvBwt4oaLRFeHut3Xmfrz8r7R4KmHZSzwFRC1t4IuLTQYoqbxtH8dT7B+DKfelr6Ouxx9ZvN/irk2DJOZfkE=@vger.kernel.org X-Gm-Message-State: AFq9FYLChGrevQiU9mml/5AR7ANnXnYnnO1vxGP82X3OtitwNSqGUh42 OzKBvv3vM432IMntTw4qS5ius3omqHuC+LUvhg6q43fl/EGL44dmle16IMG+I4BekEYWwYsXSei I14Eki8YPh8DQ1bJbq5PDZ1zb2mdbBt+XvSbpwFJBRcEHyAtoyvWnLzsaJmoOi2ci X-Gm-Gg: AYBFou2iIKAkrMnqXx5NhsDiSISEQhRWEMfP8+p1XhTErvoMj9T4zcjUc3DW0Kmst9F rW/zo28cjsoRdC+8a0AaqOJbRi6eH5Mxxlz/zqSiPgkRXX7bE6l6gPs1V/GNrFtTDfDleW6iLf3 XOl/8ATrlbJtDuZn9dkK7SNYNaWkjKuPOXFEWHOMiGMhG6sNpo+ZOfo2r3DetkdMsIv2J7J9pZs Zrr19Z4lPH3EHsd6PHQyk8Dt4UVt2omttsUZ2tetg3eFaqB6FFU/mju3a3iFxtnPfq4uuMoAMan h4epiWqLRchKWS6T+XexwkRP2PrgvnkDreETLF/W22BfBUmfiSANJoZ3HDLMQ3ecMHKXVCjVoSj XP8FAwu3GiXyi+aPkP0kN6lZSH/gErb1jXgOZ X-Received: by 2002:a05:6000:4a07:b0:48d:f14a:9183 with SMTP id ffacd0b85a97d-48e264a1d38mr1725890f8f.15.1792562728177; Tue, 20 Oct 2026 23:05:28 -0700 (PDT) X-Received: by 2002:a05:6000:4a07:b0:48d:f14a:9183 with SMTP id ffacd0b85a97d-48e264a1d38mr1725844f8f.15.1792562727746; Tue, 20 Oct 2026 23:05:27 -0700 (PDT) Received: from fedora.redhat.corp (89-27-86-246.bb.dnainternet.fi. [89.27.86.246]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-48e254f02f1sm3413741f8f.3.2026.10.20.23.05.25 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 20 Oct 2026 23:05:26 -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 , Lyude Paul , Danilo Krummrich Subject: [PATCH v16 03/12] mm/hmm: preparations for HMM to participate in migration Date: Wed, 21 Oct 2026 09:04:57 +0300 Message-ID: <20261021060506.2377545-4-mpenttil@redhat.com> X-Mailer: git-send-email 2.55.0 In-Reply-To: <20261021060506.2377545-1-mpenttil@redhat.com> References: <20261021060506.2377545-1-mpenttil@redhat.com> X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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. In case lock-drop behavior is requested, return an error while migrating. Also, rename some struct hmm_vma_walk fields to solve namespace collisions, and we are later introducing more xxlocked fields so be unambiguous. 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 | 50 +++++++++++++++++- lib/test_hmm.c | 2 +- mm/hmm.c | 110 ++++++++++++++++++++++++++++++++++------ 3 files changed, 145 insertions(+), 17 deletions(-) diff --git a/include/linux/migrate.h b/include/linux/migrate.h index 280ec9a866c7..dd7dd7ec6995 100644 --- a/include/linux/migrate.h +++ b/include/linux/migrate.h @@ -5,6 +5,7 @@ #include #include #include +#include #include #include @@ -108,6 +109,29 @@ 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) +{ +} + +#ifdef CONFIG_MMU_NOTIFIER +static inline struct mm_struct *hmm_range_fault_mm(struct hmm_range *range) +{ + return range->notifier->mm; +} +#endif #endif /* CONFIG_MIGRATION */ #ifdef CONFIG_NUMA_BALANCING @@ -151,7 +175,8 @@ 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_NONE = 0, MIGRATE_VMA_SELECT_SYSTEM = 1 << 0, MIGRATE_VMA_SELECT_DEVICE_PRIVATE = 1 << 1, MIGRATE_VMA_SELECT_DEVICE_COHERENT = 1 << 2, @@ -193,6 +218,29 @@ 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; +} + +#ifdef CONFIG_MMU_NOTIFIER +static inline struct mm_struct *hmm_range_fault_mm(struct hmm_range *range) +{ + return range->notifier ? range->notifier->mm : range->migrate->vma->vm_mm; +} +#endif + +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 6911daa9f854..cd88e8177d0a 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 2f1e98c6b644..4e9542aedc25 100644 --- a/mm/hmm.c +++ b/mm/hmm.c @@ -21,6 +21,7 @@ #include #include #include +#include #include #include #include @@ -28,15 +29,23 @@ #include #include #include +#include #include "internal.h" struct hmm_vma_walk { - struct hmm_range *range; - bool *locked; - unsigned long last; - unsigned long end; - unsigned int required_fault; + struct mmu_notifier_range mmu_range; + /* Set once mmu_range is armed with mmu_notifier_invalidate_range_start() */ + bool mmu_armed; + struct vm_area_struct *vma; + struct hmm_range *range; + unsigned long start; + unsigned long end; + unsigned long last; + /* fault and lock drop related fields */ + bool *mmlocked; + unsigned long fault_end; + unsigned int required_fault; }; /* @@ -95,7 +104,7 @@ static int hmm_record_fault(unsigned long addr, unsigned long end, WARN_ON_ONCE(!required_fault); hmm_vma_walk->last = addr; - hmm_vma_walk->end = end; + hmm_vma_walk->fault_end = end; hmm_vma_walk->required_fault = required_fault; return HMM_FAULT_PENDING; } @@ -400,6 +409,58 @@ static int hmm_vma_handle_absent_pmd(struct mm_walk *walk, unsigned long start, } #endif /* CONFIG_ARCH_HAS_PMD_SOFTLEAVES */ +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; + + if (end - start > range->end - range->start) + return -ERANGE; + + if (!hmm_vma_walk->mmu_armed) { + hmm_vma_walk->start = start; + hmm_vma_walk->end = end; + 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); + hmm_vma_walk->mmu_armed = true; + } + + 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_armed) { + /* + * 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, @@ -594,6 +655,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) @@ -646,12 +712,12 @@ static int hmm_do_fault(struct mm_struct *mm, struct hmm_vma_walk *hmm_vma_walk) { unsigned long addr = hmm_vma_walk->last; - unsigned long end = hmm_vma_walk->end; + unsigned long end = hmm_vma_walk->fault_end; unsigned int required_fault = hmm_vma_walk->required_fault; unsigned int fault_flags = FAULT_FLAG_REMOTE; struct vm_area_struct *vma; - if (hmm_vma_walk->locked) + if (hmm_vma_walk->mmlocked) fault_flags |= FAULT_FLAG_ALLOW_RETRY | FAULT_FLAG_KILLABLE; vma = vma_lookup(mm, addr); @@ -670,8 +736,8 @@ static int hmm_do_fault(struct mm_struct *mm, ret = handle_mm_fault(vma, addr, fault_flags, NULL); if (ret & (VM_FAULT_COMPLETED | VM_FAULT_RETRY)) { - if (hmm_vma_walk->locked) /* needed by sparse */ - *hmm_vma_walk->locked = false; + if (hmm_vma_walk->mmlocked) /* needed by sparse */ + *hmm_vma_walk->mmlocked = false; else WARN_ON_ONCE(1); /* broken fault handler */ return HMM_FAULT_UNLOCKED; @@ -694,19 +760,31 @@ static int hmm_range_fault_locked(struct hmm_range *range, bool *locked) { struct hmm_vma_walk hmm_vma_walk = { .range = range, - .locked = locked, + .mmlocked = locked, .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; + /* Migration is incompatible with mmap lock drop */ + if (locked && hmm_select_migrate(range)) + return -EINVAL; + 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); /* @@ -732,6 +810,8 @@ static int hmm_range_fault_locked(struct hmm_range *range, bool *locked) * output, and all >= are still at their input values. */ } while (ret == -EBUSY); + + hmm_vma_post_range_fault(&hmm_vma_walk); return ret; } -- 2.55.0