From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 40308606A320 for ; Tue, 1 Dec 2026 04:56:13 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=E+fjQFVG Received: by kanga.kvack.org (Postfix) id 1078E6B05B5; Mon, 30 Nov 2026 23:56:08 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 02A366B05B6; Mon, 30 Nov 2026 23:56:07 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id DB0AD6B05B7; Mon, 30 Nov 2026 23:56:07 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id 6D0236B05B5 for ; Mon, 30 Nov 2026 23:56:07 -0500 (EST) Received: from smtpin09.hostedemail.com (lb01a-stub [10.200.18.249]) by unirelay01.hostedemail.com (Postfix) with ESMTP id 79B6F1C42CB for ; Tue, 1 Dec 2026 04:56:06 +0000 (UTC) X-FDA: 85494202332.09.547A737 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by imf11.hostedemail.com (Postfix) with ESMTP id 152D340008 for ; Tue, 1 Dec 2026 04:56:03 +0000 (UTC) Authentication-Results: imf11.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=E+fjQFVG; spf=pass (imf11.hostedemail.com: domain of mpenttil@redhat.com designates 170.10.133.124 as permitted sender) smtp.mailfrom=mpenttil@redhat.com; dmarc=pass (policy=quarantine) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; d=hostedemail.com; s=arc-20220608; cv=none; t=1796100964; b=JPrKagm5IZ7S4FYdQ9rgatU/GMKe3aR0DSLE/cMzNhtw4K7/Pc4bM93VGT7r9zOncQaevZ oqmnQFPPHYuj3Jdb9aOvF+ipHpKT/gi0Jadx5kGbJ+1niUhms4Z/o4Ld74KgoVuaCYehiJ J33mhhaklMWAkqYJF2WxRdlL9y9HOkE= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1796100964; h=from:from:sender: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:dkim-signature; bh=RYnj4fg9rzSyGm+Exf6K+DcGYgqI0u1oUfHsn0OgwYs=; b=F94SBtIvyOn/GawOjS/I58MOYx8LsG3AGQQ7WnyZdUfk3DdPE6AUjZefs4Kn66oL94nzpc u7+WFrnXBLeC0fBJ9ughlnXoTjLwPMzmbhCvIP0qrlmnC7qRtk8elEOgmGaOD6dMaR8E60 Sh2lem7cSoIzf2qt+SiiiW1iFvEXb68= ARC-Authentication-Results: i=1; imf11.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=E+fjQFVG; spf=pass (imf11.hostedemail.com: domain of mpenttil@redhat.com designates 170.10.133.124 as permitted sender) smtp.mailfrom=mpenttil@redhat.com; dmarc=pass (policy=quarantine) header.from=redhat.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1796100964; 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=RYnj4fg9rzSyGm+Exf6K+DcGYgqI0u1oUfHsn0OgwYs=; b=E+fjQFVGyrJT3I2wComE5Z4EomAxEiBkg68UzJnPVZKP5wVGvnNhyI/gSuTJ7037UuEokc Igr7N0hE6sFxIp4O72sdE8PYxbHYD92kYEbZSeTw77KFpflzwU61q1qz6QEQOwF8Io3HAI tzgaSRiBykrVQNu84iRz15IF1rVlza8= X-MC-Rep: binary re single Received: from mail-lf1-f70.google.com (mail-lf1-f70.google.com [209.85.167.70]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-80-6I_-bd2LO36sTCCcxygpsQ-1; Tue, 1 Dec 2026 04:56:01 +0000 X-MC-Unique: 6I_-bd2LO36sTCCcxygpsQ-1 X-Mimecast-MFC-AGG-ID: 6I_-bd2LO36sTCCcxygpsQ_1796100960 Received: by mail-lf1-f70.google.com with SMTP id 2adb3069b0e04-5c35382e6b4so850419e87.2 for ; Mon, 30 Nov 2026 20:56:00 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796100959; x=1796705759; 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=RYnj4fg9rzSyGm+Exf6K+DcGYgqI0u1oUfHsn0OgwYs=; b=01N9Qm28sjFcpcsdeKxJbMWRyWY4A17LWdJR7GHpZ3Hu5D/kWRr+nG6phrYYmdj9tz 7As/Y2aNpFhQUSUpZvE/A11heSmWeVF05lGibNl8+wDkepL7Q9W5hWxqflOOjAoyFEmM hPLQC7rjvGgx0NNw1T3ByhEGCKKIPJ30eY3CUc7VWzXaSsrDHB6ZBqS/A1Glh1g6Yiy7 DAOKnnwXDsqSBAdDtPf85aC7a5ZMPOqKI/un9DSsBH8gtdMlzk5YcL7/TM4VXWPxuGcl PmLTAd83epyQ19NxCrstfa4f9OihPtnL1nnSBRTcKEQoT5HM57gSEALthX1Ar2wHbbgd pP1Q== X-Gm-Message-State: APuU4Z+0wy8GosF8KySXrC7B8opC5oeMDUxt1bfxjdrcDtUaCOcOGX8l i9+dLMJzazEGQ1IjgP+9muqmUXGoGMvRF5HgN4ZQtRLDtyyzBgreUke3qIJlbZV5/lCwndhSUgy 9MN5nvZyH/EYzpa3+388WPh7FiDs1SLB1joARd2LPmh78dC0O8jbnsNzyf/3MNEMnCGhyC0KqiB 41yEtmFyMxoJHxPnMCWgYpaQIXNsCnOmVXIUgvZA== X-Gm-Gg: AZFdTZfcWfX4Upll5wkcpbS2A59bkFIInPZ5dyNGKB8PoRY49w2k1HNLoRsmiZEaRki P4dWprLf7mjsHcBnFiemWika9z0M/KLhDoJOrzaTIG1LXgeXY9oxjKbyhH4dCnUa7MSuLR67wqJ FshNBWqx4pY7kO2xGqan8TPfbCEmFLTt5rvd3MPCL9QrzBgLbhwq6SJgFm8FzcHB+wYV6ZG7SFF OK3ANSoCVpeup2reSMFDI23ZuXfejH9puSr4dIeGNlaQkb+TE4zm9RoufIJtpbX7P9ipJdon1MU k7b8Tu/us0yRvy3Q6Imwwh44YYoOxYSoNfBHuHGAkmw4l/8n57hm57nXqB/7KJiDIE1XzCBJAqd qA6Mqtmh1Z1bkwf8cfE9bOFEVvl6ngGyEMGLUz08UItndOl0PFJ2Pd08wLr/4+DYkAeLhvA== X-Received: by 2002:a05:6512:2583:b0:5c3:35fb:5679 with SMTP id 2adb3069b0e04-5c335fb5c97mr3006584e87.37.1796100959478; Mon, 30 Nov 2026 20:55:59 -0800 (PST) X-Received: by 2002:a05:6512:2583:b0:5c3:35fb:5679 with SMTP id 2adb3069b0e04-5c335fb5c97mr3006553e87.37.1796100958837; Mon, 30 Nov 2026 20:55:58 -0800 (PST) Received: from fedora (89-27-86-246.bb.dnainternet.fi. [89.27.86.246]) by smtp.gmail.com with ESMTPSA id 2adb3069b0e04-5c1bd0563casm8668638e87.67.2026.11.30.20.55.56 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 30 Nov 2026 20:55:57 -0800 (PST) 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 , Michal Hocko , Lyude Paul , Danilo Krummrich , Jonathan Corbet , Shuah Khan , Madhavan Srinivasan Subject: [PATCH v18 03/14] mm/hmm: preparations for HMM to participate in migration Date: Tue, 1 Dec 2026 06:55:20 +0200 Message-ID: <20261201045531.3622535-4-mpenttil@redhat.com> X-Mailer: git-send-email 2.55.0 In-Reply-To: <20261201045531.3622535-1-mpenttil@redhat.com> References: <20261201045531.3622535-1-mpenttil@redhat.com> X-Archived-By: MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-MFC-PROC-ID: PtfLbnL97MCL6vfUZrNpQNo7zxBLR9wjL5fheaTNbBM_1796100960 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Rspamd-Server: rspam08 X-Rspamd-Queue-Id: 152D340008 X-Rspam-User: X-Stat-Signature: 65kqchouchtgswh17ppo15ghaxtyreaa X-Spam-Status: No, score=-18.8 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_PASS, SPF_PASS,USER_AGENT_GIT autolearn=ham autolearn_force=no version=4.0.1 X-HE-Tag: 1796100963-117418 X-HE-Meta: U2FsdGVkX1+cc06vssL7+KYaEK3jqNoJuocVu6/1FXj18oglRRgcUKtDCanhYVZQJXihIZdZU3hhHGzwNCOBrH6DAiv22Ih42X18hH8WmRU2b+DYzxiySlwZXsWvOfUMad33IuupSZoa6cm/yi9/EHeKCRCH1Djep2BeFSphQbq/RfDw4qn6zH7uDA4IN49uReSP9SuJuHSMRzsGUkimRUlO4Xs1w4didnjyInY0CpmFuRWz2PMV/yVaAg6FHu47sWzVrYbaqDfMnWpHer70ndkD5qOclJse4R69MyTjZWceasYeIeMaMM8jDGKZa0CT/EgsPwfroR2YFnev50ELeslrT3h1SDHlww74MOFmxo2QL1lC7f7wpbwNEswN/Yif5htIbSYSdXbvbP8u53q2MiXr9UxBcNZ7aj45Xq1Jdhw6FNuil0fD+yUMuxXJVG8lYfs1b9K64rjrrUm30nmh083FB5keeTwFp8YhsOk5JVtyCRlbUEmmPWKRbENQPDll0RArHWXa5M4ZGb356ipcD3EovOg4eRYDtrApl9rAftHv87GIqJRRgMUb4ubMBipWEOs19MWHCNsZgpOdHg9Ockgqq/7wLIU2/Nzn/EsVrqgHjP3hDN6o7aQf1xzNI8MY0ObVGJ6vUoBx4t+9qaAYYHQxUAvJW/hkwcTqAbd4dBAlQNlaBwHEF4KjpUCW+0B2j4035FlJRuvXTLnGnfvqdgkXhdTfNhsohuCwGiZAeLuu593mL2x1j0ht+l3pcpDokRLS5E77o28yL2d6v8PtxUJyKzQLjnqPA27ot3K31IWdB6R/G0mcL27aFEaPvS+zCvzPHHnYJhmpCtPT9VTjvZZ2aKhUkDlR8EygXo23M29efd4DHEoALJDVqagzh+PkCXQ8H4mZUOUOhduWbT6kGmyrF0mi+aMV794UDD5LetCDH8hcT7V7/rCRm6UTfN64txqF2JYeiVuiwuyDejd LlizMo6/ FrsMd2/I4TbLqUd3t4POOlGHU+d262EdRuCrXdZybIIq2VzrrDMCeLYMKxJHZui+GlnthffCRokkmqjEmmEfriM9KRsRck6E04b2Jtmq1lf5D6dMNCFazqAWZEF9xKE3hgA2YVDY0PMOOR11EUiaG4YcGN24bPLtyHW6VXKedwyePHFSYVUXt5HJO5q+9cJtmj/TP0lfPiF1l2Hfvf/4vQ/WXXFwbUTI00f0e9Kmva0yLxFWhbiOs7MS5H8nCBLgaQWu4UIwbZ8s+W6KWvmd9GxIymVF/VMThvAlkrkNpWVDJIoAOH/AZgVYewBo1PcgxsxmDjIq0ZvnKOj3nV6YqXDBX3FyMVeP+THPgVSYtjB0ZTBa8NcF/JzgUkr8MrlcpsIB9sZGc4sXGXBwbb67Iyl4p14QTNrjZGoaBqpyytE/qETpXVlCYFztTQusxs5dvYAi+LOuWHoYTMiEK4acBkh0AFnwtok4NJ+uT0DD3pOiRWRY0dcJF3xba4iwnvzs1gT8Arl4mDZbCM87mXmYNks6WlWc6N1BDt1kZEiTVThEAJ1NHGnW2mapapWoyXoXFlfe45XHPeYM7PIhsBY3QttzjKs5GJgZ2uyk4BYHmC+eU/Y77MSkuS9kDLXxdbLpPfdSoZixC54M1n1oEtHEzK4BZVmfIqJSvUJSZPbvOucp5HM1mW7h+xzfVQOVOYjMYSpFf Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: X-Spam-Checker-Version: SpamAssassin 4.0.1 (2024-03-26) on aws-us-west-2-korg-listserv-alma10-1 From: Mika Penttilä For migration to happen after hmm_range_fault(), the vma has to be retrieved. Luckily, pagewalk already resolves that for us, so just have to save it for further use. For migration, mmap_lock has to be held 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, and don't wait for another migration's entries while migrating. A migrating walk re-walks while holding collected folios and may have no notifier to end it (migrate_vma_setup()), so a fatal signal pending after a fault ends it with -EINTR. The vma restrictions match migrate_vma_setup(), and shadow stack vmas are refused as well, as in the pending "mm/migrate_device: refuse to migrate shadow stacks". When migrating, requests come only from range->default_flags (pfn_flags_mask must be 0), so the input range->hmm_pfns carry nothing and are cleared at the start of hmm_range_fault(). Collected entries (HMM_PFN_VALID | HMM_PFN_MIGRATE) can then be told apart reliably during the walk and on rollback. This also means that HMM_PFN_INOUT_FLAGS such as HMM_PFN_DMA_MAPPED are not preserved across a migrating call, which is intended since the pages are being replaced. Also, prepare for differences of how mm_struct * is retrieved in fault and migrate_vma paths. Migration is still stubbed off here: hmm_select_migrate() returns MIGRATE_VMA_SELECT_NONE until a later patch enables it, so none of this takes effect yet. The enum migrate_vma_direction becomes migrate_vma_info, as it also carries the fault flags added later, and gets MIGRATE_VMA_SELECT_NONE. The small helpers hmm_select_migrate(), hmm_fill_migrate_vma() and hmm_range_fault_mm() live in migrate.h next to struct migrate_vma, which they use, with stubs for !CONFIG_MIGRATION. 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/hmm.h | 7 +- include/linux/migrate.h | 66 ++++++++++++- lib/test_hmm.c | 2 +- mm/hmm.c | 208 +++++++++++++++++++++++++++++++++++----- 4 files changed, 257 insertions(+), 26 deletions(-) diff --git a/include/linux/hmm.h b/include/linux/hmm.h index 4f56f3419cb4..e4cd255da1a2 100644 --- a/include/linux/hmm.h +++ b/include/linux/hmm.h @@ -40,11 +40,14 @@ struct migrate_vma; * On input: * 0 - Return the current state of the page, do not fault it. * HMM_PFN_REQ_FAULT - The output must have HMM_PFN_VALID or hmm_range_fault() - * will fail + * will fail, except when migrating, which is best + * effort (see HMM_PFN_REQ_MIGRATE) * HMM_PFN_REQ_WRITE - The output must have HMM_PFN_WRITE or hmm_range_fault() * will fail. Must be combined with HMM_PFN_REQ_FAULT. * HMM_PFN_REQ_MIGRATE - For default_flags only, request to migrate the range, - * according to hmm_range.migrate.flags + * according to hmm_range.migrate.flags. Collection, + * and faulting with it, is best effort, see + * hmm_range_fault(). */ enum hmm_pfn_flags { /* Output fields and flags */ diff --git a/include/linux/migrate.h b/include/linux/migrate.h index b778f8bba5a3..9513d1697232 100644 --- a/include/linux/migrate.h +++ b/include/linux/migrate.h @@ -5,6 +5,7 @@ #include #include #include +#include #include #include @@ -108,6 +109,35 @@ 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, + /* No migration, so no compound migration either */ + 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_migrate_pgmap_owner(struct hmm_range *range) +{ + return NULL; +} + +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 @@ -152,7 +182,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, @@ -194,6 +225,39 @@ struct migrate_vma { struct page *fault_page; }; +/* + * Helpers for the migration path of hmm_range_fault() in mm/hmm.c. They live + * here next to struct migrate_vma, which they use, and the !CONFIG_MIGRATION + * stubs above let mm/hmm.c build without #ifdefs. + */ +/* Migration collection in hmm_range_fault() is enabled later in the series */ +static inline enum migrate_vma_info hmm_select_migrate(struct hmm_range *range) +{ + return MIGRATE_VMA_SELECT_NONE; +} + +static inline void *hmm_migrate_pgmap_owner(struct hmm_range *range) +{ + return range->migrate->pgmap_owner; +} + +#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 a09a766b15b0..63226caea5da 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 9871d3c3cd07..35bbae71e19c 100644 --- a/mm/hmm.c +++ b/mm/hmm.c @@ -21,6 +21,7 @@ #include #include #include +#include #include #include #include @@ -32,11 +33,18 @@ #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; + /* End of the range armed for migration, set when the vma is captured */ + unsigned long end; + unsigned long last; + /* fault and lock drop related fields */ + bool *mmlocked; + unsigned long fault_end; + unsigned int required_fault; }; /* @@ -95,7 +103,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; } @@ -176,7 +184,7 @@ static int hmm_vma_walk_hole(unsigned long addr, unsigned long end, required_fault = hmm_range_need_fault(hmm_vma_walk, hmm_pfns, npages, 0); if (!walk->vma) { - if (required_fault) + if (required_fault || hmm_select_migrate(range)) return -EFAULT; return hmm_pfns_fill(addr, end, range, HMM_PFN_ERROR); } @@ -296,6 +304,13 @@ static int hmm_vma_handle_pte(struct mm_walk *walk, unsigned long addr, goto fault; if (softleaf_is_migration(entry)) { + /* + * Someone else's migration. When migrating, don't wait + * for it: we may hold collected folios it waits for. + * Leave the entry uncollected (best effort). + */ + if (hmm_select_migrate(range)) + goto out; pte_unmap(ptep); hmm_vma_walk->last = addr; migration_entry_wait(walk->mm, pmdp, addr); @@ -400,6 +415,79 @@ 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; + struct vm_area_struct *vma = walk->vma; + + if (!hmm_select_migrate(range)) + return 0; + + if (!hmm_vma_walk->vma) { + /* + * Only vmas migrate_vma_setup() handles: mm backed, not + * hugetlb or DAX. Shadow stacks are refused too, so that no + * device gets a writable copy of one. + */ + if (!vma_is_mm_backed(vma) || vma_is_hugetlb(vma) || + vma_is_dax(vma) || + vma_test_single_mask(vma, VMA_SHADOW_STACK)) + return -EINVAL; + /* + * migrate_vma covers the whole range and indexes src[] from + * range->start, so a single vma must cover the whole range. + */ + if (vma->vm_start > range->start || + vma->vm_end < range->end) + return -ERANGE; + hmm_vma_walk->vma = vma; + hmm_vma_walk->end = end; + } + + /* + * The invalidation is closed before faulting, see + * hmm_range_fault_locked(), so (re)open it for each walk. Only over + * what is still to be walked: an earlier walk handled the range below + * start, and its entries are not changed again. + */ + if (!hmm_vma_walk->mmu_armed) { + mmu_notifier_range_init_owner(&hmm_vma_walk->mmu_range, MMU_NOTIFY_MIGRATE, 0, + walk->vma->vm_mm, start, + hmm_vma_walk->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_migrate_disarm(struct hmm_vma_walk *hmm_vma_walk) +{ + if (hmm_vma_walk->mmu_armed) { + mmu_notifier_invalidate_range_end(&hmm_vma_walk->mmu_range); + hmm_vma_walk->mmu_armed = false; + } +} + +static void hmm_vma_post_range_fault(struct hmm_vma_walk *hmm_vma_walk) +{ + struct hmm_range *range = hmm_vma_walk->range; + + /* + * The migrate_vma path has the following initialized. For the fault + * path fill them in, also when the walk failed before capturing the + * vma, so migrate_hmm_range_setup() always sees the range. + */ + if (hmm_select_migrate(range) && range->notifier) + hmm_fill_migrate_vma(range, + hmm_vma_walk->vma, + range->start, range->end); + hmm_vma_migrate_disarm(hmm_vma_walk); +} + static int hmm_vma_walk_pmd(pmd_t *pmdp, unsigned long start, unsigned long end, @@ -420,7 +508,9 @@ static int hmm_vma_walk_pmd(pmd_t *pmdp, return hmm_vma_walk_hole(start, end, -1, walk); if (thp_migration_supported() && pmd_is_migration_entry(pmd)) { - if (hmm_range_need_fault(hmm_vma_walk, hmm_pfns, npages, 0)) { + /* Someone else's migration: when migrating, don't wait (see pte level) */ + if (!hmm_select_migrate(range) && + hmm_range_need_fault(hmm_vma_walk, hmm_pfns, npages, 0)) { hmm_vma_walk->last = addr; pmd_migration_entry_wait(walk->mm, pmdp); return -EBUSY; @@ -594,10 +684,13 @@ 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; + /* Migration without faulting moves pages regardless of VM_READ */ + bool migrate_only = hmm_select_migrate(range) && + !(range->default_flags & HMM_PFN_REQ_FAULT); if (!(vma->vm_flags & (VM_IO | VM_PFNMAP)) && - vma->vm_flags & VM_READ) - return 0; + (vma->vm_flags & VM_READ || migrate_only)) + return hmm_vma_capture_migrate_range(start, end, walk); /* * vma ranges that don't have struct page backing them or map I/O @@ -607,10 +700,11 @@ static int hmm_vma_walk_test(unsigned long start, unsigned long end, * allow write access either. HMM does not support architectures that * allow write without read. * - * If a fault is requested for an unsupported range then it is a hard - * failure. + * If a fault or migration is requested for an unsupported range then + * it is a hard failure. */ - if (hmm_range_need_fault(hmm_vma_walk, + if (hmm_select_migrate(range) || + hmm_range_need_fault(hmm_vma_walk, range->hmm_pfns + ((start - range->start) >> PAGE_SHIFT), (end - start) >> PAGE_SHIFT, 0)) @@ -646,12 +740,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 +764,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 +788,54 @@ 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; int ret; + /* Checked once here, so the migration helpers can rely on it */ + if ((range->default_flags & HMM_PFN_REQ_MIGRATE) && + WARN_ON_ONCE(!range->migrate)) + return -EINVAL; + mm = hmm_range_fault_mm(range); + + /* Migration is incompatible with mmap lock drop */ + if (locked && hmm_select_migrate(range)) + return -EINVAL; + + /* + * Migration takes its requests only from default_flags, so the input + * pfns carry nothing. Clear them so that collected entries can be + * told apart reliably during the walk and on rollback. + * A migrating call doesn't resume a previous one: the caller must + * finish the previous attempt with migrate_vma_pages() and + * migrate_vma_finalize() before calling again. + */ + if (hmm_select_migrate(range)) { + WARN_ON_ONCE(range->pfn_flags_mask); + WARN_ON_ONCE(range->dev_private_owner != + hmm_migrate_pgmap_owner(range)); + memset(range->hmm_pfns, 0, + ((range->end - range->start) >> PAGE_SHIFT) * + sizeof(*range->hmm_pfns)); + } + 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); /* @@ -718,12 +847,38 @@ static int hmm_range_fault_locked(struct hmm_range *range, bool *locked) * now-present entries. */ if (ret == HMM_FAULT_PENDING) { + /* + * Don't hold the migrate invalidation across a + * possibly sleeping fault. Already collected entries + * stay locked and referenced, and the next walk + * reopens the invalidation. Entries below last are not + * revisited; if their migration ptes vanish meanwhile, + * the caller's migrate_vma_finalize() still releases + * them, as after migrate_vma_setup(). Whatever removed + * them also invalidated the range, so the caller's + * mmu_interval_read_retry() keeps it from mapping the + * unused destination on the device. + */ + hmm_vma_migrate_disarm(&hmm_vma_walk); ret = hmm_do_fault(mm, &hmm_vma_walk); if (ret == HMM_FAULT_UNLOCKED) { if (fatal_signal_pending(current)) return -EINTR; return -EBUSY; } + /* + * A fault can return without making the entry present + * (e.g. a migrate_to_ram() that didn't migrate), so the + * next walk faults it again. A migrating walk keeps + * collected folios locked across these re-walks, and + * migrate_vma_setup() has no notifier to end them: let + * a fatal signal end it. + */ + if (ret == -EBUSY && hmm_select_migrate(range) && + fatal_signal_pending(current)) { + ret = -EINTR; + break; + } } /* * When -EBUSY is returned the loop restarts with @@ -732,6 +887,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; } @@ -750,6 +907,13 @@ static int hmm_range_fault_locked(struct hmm_range *range, bool *locked) * the invalidation to finish. * -EFAULT: A page was requested to be valid and could not be made valid * ie it has no backing VMA or it is illegal to access + * -ERANGE: When migrating, the range is not covered by a single VMA + * (a range starting in a hole gets -EFAULT instead). + * -EINTR: When migrating, a fatal signal is pending after a fault. + * + * When migrating (HMM_PFN_REQ_MIGRATE), collection and faulting are best + * effort: the call can succeed with entries left without HMM_PFN_VALID, even + * with HMM_PFN_REQ_FAULT, so callers check each entry. * * This is similar to get_user_pages(), except that it can read the page tables * without mutating them (ie causing faults). -- 2.55.0