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 kanga.kvack.org (kanga.kvack.org [205.233.56.17]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id D5495EB5979 for ; Wed, 11 Feb 2026 08:13:42 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 40B8B6B008A; Wed, 11 Feb 2026 03:13:42 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 3998C6B008C; Wed, 11 Feb 2026 03:13:42 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 263BC6B0092; Wed, 11 Feb 2026 03:13:42 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id 0CBB76B008A for ; Wed, 11 Feb 2026 03:13:42 -0500 (EST) Received: from smtpin15.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id A6CD21A02BE for ; Wed, 11 Feb 2026 08:13:41 +0000 (UTC) X-FDA: 84431461842.15.F51353A Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by imf19.hostedemail.com (Postfix) with ESMTP id 76D201A0007 for ; Wed, 11 Feb 2026 08:13:39 +0000 (UTC) Authentication-Results: imf19.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=iXU+3h5+; spf=pass (imf19.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-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1770797619; 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=dKoH29cV1ZIkOnI06Bks6a0Ek8/pBZTR7+EAH7YjfdI=; b=Gyo+1udGNCCCItPKbI6d7uCf+2bMIZHKALOc2K6tgRDDNeAQXRcUJ9+bsWimvrf3e5R4IN dT6xArXqCvK575osNKQwjBLpQ0vF+agaNypvVO1c+Z+hM6WDp8/4XJcqbbqnQbFFhPOG/C xw4CuzLMyBnxQKjgAIUKOfkLpxrqlns= ARC-Authentication-Results: i=1; imf19.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=iXU+3h5+; spf=pass (imf19.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; s=arc-20220608; d=hostedemail.com; t=1770797619; a=rsa-sha256; cv=none; b=dLdXNKEMZZMvrpIWG5cp2U40os2FmsoICrSJdXIQ4ckRM9Ei7763M9NbmTPPZj1Z0oWIJr 3dS7sx2ITqJxQVRSErOW6BMDZuX39b25kgMxwzWbincwDjUFJ3BVY+uKy9xwQvTX2+EgfQ eWehoU0bkltDf1Ov25pv/mCmXPX1uw8= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1770797618; 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=dKoH29cV1ZIkOnI06Bks6a0Ek8/pBZTR7+EAH7YjfdI=; b=iXU+3h5+ok3Tz+oc/02Q1NO93xy2bnE6liIoLJpqyaJ0vOIwgGhm56yZR48P2rBQjmqy8x Ly+mvmddrLlaXpq3yaDEL6HC6AQJ+Ta945U+mrWk4VdUj0FMR+8Whz+QjgkKt/gUZF9mv6 Pa+h1Rvac0x7SdeZooKvow+by+HO8Ww= 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-542-y5Uq4JNPNRyr5mvXAHseVQ-1; Wed, 11 Feb 2026 03:13:37 -0500 X-MC-Unique: y5Uq4JNPNRyr5mvXAHseVQ-1 X-Mimecast-MFC-AGG-ID: y5Uq4JNPNRyr5mvXAHseVQ_1770797616 Received: by mail-lf1-f69.google.com with SMTP id 2adb3069b0e04-59e11fb980fso4087105e87.2 for ; Wed, 11 Feb 2026 00:13:37 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1770797615; x=1771402415; h=content-transfer-encoding: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; bh=dKoH29cV1ZIkOnI06Bks6a0Ek8/pBZTR7+EAH7YjfdI=; b=w7UEF8wafbadPs9FWmscUHDa4m6FpWgWO0BoZqrt+qaKEsraH2MDbrh7HNYBNDPol7 mElZfcsxEq/WCbRaoniHiADebhyG1d8xuCu5DURwQJDPdszqaOvXtaXC5EOLREVw2ZnV 1xax/ll4ymYkyBCk7r2qITlFhF5RDEo3pqxZzOs0HhYdE/6PGNzbrbtWh5JLsKnZ3bn8 HQQhqHHVNWk2Ot3qCTNsULJrq+d30BVg59hpbPgOtz31tPTUVPWUBTsKIiS1bdBR8eiT AOiBnnsLIo5AkANkBWVAJYjNVNJf8slKPMabJOP7vK6F8SjCXGnq3gm7xbFUUPBL7peT lt3w== X-Gm-Message-State: AOJu0Yz5rjQ5NaW2cT5tyR0ouqnwwvOgXDimRm4QVbph/daF+gVSuM4k hyo1acUMqF+CIKgXx5S1sIjmYpDgFG7UW7+JKoH8Ku3ocwX0pDO0nDlTRBNvGpt223sv4H0FEQm CzukHXRxFsgDWk0u7//XK3wHbhxx8lBf28LnjI5Ay9TQRjJvBgYYuK5x66DQwh4HEFjZgLwFlNw mbigMaSJISw574oQyxxICt1JXrRvqQGm8+aZM= X-Gm-Gg: AZuq6aIZ3ounuqDdUCq4FGmc9hSd/bZlFVvRA/4tfjQ/1ClZQKO/XGBPuhzjN2ZC/ye RfcSZDYRihiYsZ/PogUCxVhu1GgHMNpyC1pNgkZ21LNZEhJqGexhXSZekr4Yrv3nFnZOexKE3La Ja22JIMIu9aDyb7jHiRPf+mUbq6S6JORlNjcibX7VJMaIP4L00KCF/eJ3YUleLebbhAyNEA8s1I 1taJWAVzjKcUJt/3BBn7Stwbzr6Ziqp+I/5Wjc4hh99Qt5o8QJr+6rbA51ZQCI9QUskL4wgnDJV VzoWZjmFOOtVetxRAkgJ+z2nY0c5ynvR2ZAsdCWchj9aMU97uws1n8CM80L4cpCOg/jRt8ogUr3 E+FRzEVXc6DT18VazaxlgRbWWWAtVWpl3I3xL X-Received: by 2002:a05:6512:1150:b0:59e:56fb:7e53 with SMTP id 2adb3069b0e04-59e5e1fa5bamr400703e87.4.1770797615410; Wed, 11 Feb 2026 00:13:35 -0800 (PST) X-Received: by 2002:a05:6512:1150:b0:59e:56fb:7e53 with SMTP id 2adb3069b0e04-59e5e1fa5bamr400686e87.4.1770797614797; Wed, 11 Feb 2026 00:13:34 -0800 (PST) Received: from fedora (85-23-51-1.bb.dnainternet.fi. [85.23.51.1]) by smtp.gmail.com with ESMTPSA id 2adb3069b0e04-59e5f568737sm188515e87.21.2026.02.11.00.13.34 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 11 Feb 2026 00:13:34 -0800 (PST) From: mpenttil@redhat.com To: linux-mm@kvack.org Cc: 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 Subject: [PATCH v5 3/6] mm/hmm: do the plumbing for HMM to participate in migration Date: Wed, 11 Feb 2026 10:12:58 +0200 Message-ID: <20260211081301.2940672-4-mpenttil@redhat.com> X-Mailer: git-send-email 2.50.0 In-Reply-To: <20260211081301.2940672-1-mpenttil@redhat.com> References: <20260211081301.2940672-1-mpenttil@redhat.com> MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-MFC-PROC-ID: 2yQvWK4qJwPVXQ5BaEbD3h44UJrghHT2vPe-Db0lIDM_1770797616 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Rspam-User: X-Rspamd-Queue-Id: 76D201A0007 X-Rspamd-Server: rspam07 X-Stat-Signature: fpqez9pjusup6hpgms6ehzhibq4gg6m4 X-HE-Tag: 1770797619-761331 X-HE-Meta: U2FsdGVkX1/r1mrIMUrAbiLBJpzykvxq+cz8uhWlagVhUzdMODaHt3StkgDiuKrubA8UaTFw9li/I2JfBVpisr6n7zi0bQtPtX2FdZXSvCkzC6c4NpEL/dzZUstZszaD28xZ154oYyq2/fRoEBQb5vDb8gNRmrdlWj63S+pGz/p4FOkFUliav54R2gIkTwhlUhuOhgepCWZiEyqIl50wo4XaLzl41XCxuTxnNaWnv7pp1XKHDvl9JD/bCnTSHhBMVnSHC+kHxWeDUCSJKawVGoIYmC0kk1q5OM8tzp/qzFFLgUP2wpCJEOIblgcWWLYCq2YRv64hlDU1VPGaC3p+0Rlb7eEyiOiJist40E5lWzFPqyAWTznqUNjMDRF+harmoo32Js7FBUP/IH+AiHHmsU9gc5IkaSzh2s9ZL9SW96o/v629j2r0FvaIlxnwchNuUW7CbtZ87UsCsJ/Hu6N33LoV7RlwTXOcV9dnMiLp44+jUmFipKkMXlI4EMHhcgYQ2uAL9pZtYPwbvr/6bGR1mYzuR6kPajfKRQ0HUAy76PyF2EXtpbvyNHSm+O9UCLxuXpXDx15QdNaLxupPoU5rXDmRJ0VGutzFhW5dkE/ut/1MiToVxbM4GxmMhleR+sPDJG8YAH5ISO+mXzPWcKV/gtMsaEDFUCLaMEz+IH7j5VHrHcrtrjRNRALNFCeOWcMznpnDGtNxURl618yr5KWTEfP5APOZwFJh7vrOakCNQhKJwmJdEB7L62mKSgmOZfH1RA1Gonjz/Z8J+RlmLgj6rMEjGhec6Q7nouNTHYwDyCKIlh4/JBk6MipjIf1XzK7MOAPHMJQJqzMjjW4wnmGMjG4aNABGflyDw5/ClVreeDe/J3WJ0jhFRGvy/Ru7nN+5KFujZPOQaQLWo9c+lYWZTCLGEklxcR5ZFfGpmQWnF/hhoxT8QJuHLZ7QCfNVxIm0JiMOA6iRxXS8TstuAX8 FMRNE5BB 47PMuOkB0PInGZwIE33Kv3VflqutTO4mGfvnORURBjlOPRXEvERcg7pT4zHl/6Fo6ijImhcJmA0o6VbnMQGgiVK6xL0x3xmNCSMwN/2xa1TCv4/V5uGGUfx63BKunLhFAcm32vx1d+07GcpZRsCi4OY1RQ45b50Hl0iw6K6QmCrsd6raqoaMsUf7GpS/SCndedv/tMGttUOUwmxnRD9Q20/Kd9ul8Hfo+Mu1m7DyU6PXa05vP029UXwORR3G96a6kRPi0PrxJ7e+0o6DLGW/wvsxeNyB3dba9KmmQQeyHf/cwyUpboawpx2Ho1WHROWozF9t8XRbFvSRFRo+tZUoM37tJf9kn1WHD8aunHv/W16MGgxxXUINt4j1OLfakoseArCBhr2x4pTM1C0RxL+HUVATKmv+3V6k4tRJDi0MrgpD6JmhO5x5VY1ExSpQLcAUJW0ru X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: From: Mika Penttilä Do the preparations in hmm_range_fault() and pagewalk callbacks to do the "collecting" part of migration, needed for migration on fault. These steps include locking for pmd/pte if migrating, capturing the vma for further migrate actions, and calling the still dummy hmm_vma_handle_migrate_prepare_pmd() and hmm_vma_handle_migrate_prepare() functions in the pagewalk. 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 | 18 +- lib/test_hmm.c | 2 +- mm/hmm.c | 419 +++++++++++++++++++++++++++++++++++----- 3 files changed, 386 insertions(+), 53 deletions(-) diff --git a/include/linux/migrate.h b/include/linux/migrate.h index 8e6c28efd4f8..818272b2a7b5 100644 --- a/include/linux/migrate.h +++ b/include/linux/migrate.h @@ -98,6 +98,16 @@ static inline int set_movable_ops(const struct movable_operations *ops, enum pag return -ENOSYS; } +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; +} + #endif /* CONFIG_MIGRATION */ #ifdef CONFIG_NUMA_BALANCING @@ -141,7 +151,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, @@ -183,6 +193,12 @@ 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; +} + 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 455a6862ae50..94f1f4cff8b1 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 21ff99379836..22ca89b0a89e 100644 --- a/mm/hmm.c +++ b/mm/hmm.c @@ -20,6 +20,7 @@ #include #include #include +#include #include #include #include @@ -27,14 +28,44 @@ #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; + /* + * For migration we need pte/pmd + * locked for the handle_* and + * prepare_* regions. While faulting + * we have to drop the locks and + * start again. + * ptelocked and pmdlocked + * hold the state and tells if need + * to drop locks before faulting. + * ptl is the lock held for pte or pmd. + * + */ + bool ptelocked; + bool pmdlocked; + spinlock_t *ptl; }; +#define HMM_ASSERT_PTE_LOCKED(hmm_vma_walk, locked) \ + WARN_ON_ONCE(hmm_vma_walk->ptelocked != locked) + +#define HMM_ASSERT_PMD_LOCKED(hmm_vma_walk, locked) \ + WARN_ON_ONCE(hmm_vma_walk->pmdlocked != locked) + +#define HMM_ASSERT_UNLOCKED(hmm_vma_walk) \ + WARN_ON_ONCE(hmm_vma_walk->ptelocked || \ + hmm_vma_walk->pmdlocked) + enum { HMM_NEED_FAULT = 1 << 0, HMM_NEED_WRITE_FAULT = 1 << 1, @@ -42,14 +73,37 @@ enum { }; static int hmm_pfns_fill(unsigned long addr, unsigned long end, - struct hmm_range *range, unsigned long cpu_flags) + struct hmm_vma_walk *hmm_vma_walk, unsigned long cpu_flags) { + struct hmm_range *range = hmm_vma_walk->range; unsigned long i = (addr - range->start) >> PAGE_SHIFT; + enum migrate_vma_info minfo; + bool migrate = false; + + minfo = hmm_select_migrate(range); + if (cpu_flags != HMM_PFN_ERROR) { + if (minfo && (vma_is_anonymous(hmm_vma_walk->vma))) { + cpu_flags |= (HMM_PFN_VALID | HMM_PFN_MIGRATE); + migrate = true; + } + } + + if (migrate && thp_migration_supported() && + (minfo & MIGRATE_VMA_SELECT_COMPOUND) && + IS_ALIGNED(addr, HPAGE_PMD_SIZE) && + IS_ALIGNED(end, HPAGE_PMD_SIZE)) { + range->hmm_pfns[i] &= HMM_PFN_INOUT_FLAGS; + range->hmm_pfns[i] |= cpu_flags | HMM_PFN_COMPOUND; + addr += PAGE_SIZE; + i++; + cpu_flags = 0; + } for (; addr < end; addr += PAGE_SIZE, i++) { range->hmm_pfns[i] &= HMM_PFN_INOUT_FLAGS; range->hmm_pfns[i] |= cpu_flags; } + return 0; } @@ -72,6 +126,7 @@ static int hmm_vma_fault(unsigned long addr, unsigned long end, unsigned int fault_flags = FAULT_FLAG_REMOTE; WARN_ON_ONCE(!required_fault); + HMM_ASSERT_UNLOCKED(hmm_vma_walk); hmm_vma_walk->last = addr; if (required_fault & HMM_NEED_WRITE_FAULT) { @@ -165,11 +220,11 @@ static int hmm_vma_walk_hole(unsigned long addr, unsigned long end, if (!walk->vma) { if (required_fault) return -EFAULT; - return hmm_pfns_fill(addr, end, range, HMM_PFN_ERROR); + return hmm_pfns_fill(addr, end, hmm_vma_walk, HMM_PFN_ERROR); } if (required_fault) return hmm_vma_fault(addr, end, required_fault, walk); - return hmm_pfns_fill(addr, end, range, 0); + return hmm_pfns_fill(addr, end, hmm_vma_walk, 0); } static inline unsigned long hmm_pfn_flags_order(unsigned long order) @@ -202,8 +257,13 @@ static int hmm_vma_handle_pmd(struct mm_walk *walk, unsigned long addr, cpu_flags = pmd_to_hmm_pfn_flags(range, pmd); required_fault = hmm_range_need_fault(hmm_vma_walk, hmm_pfns, npages, cpu_flags); - if (required_fault) + if (required_fault) { + if (hmm_vma_walk->pmdlocked) { + spin_unlock(hmm_vma_walk->ptl); + hmm_vma_walk->pmdlocked = false; + } return hmm_vma_fault(addr, end, required_fault, walk); + } pfn = pmd_pfn(pmd) + ((addr & ~PMD_MASK) >> PAGE_SHIFT); for (i = 0; addr < end; addr += PAGE_SIZE, i++, pfn++) { @@ -283,14 +343,23 @@ static int hmm_vma_handle_pte(struct mm_walk *walk, unsigned long addr, goto fault; if (softleaf_is_migration(entry)) { - pte_unmap(ptep); - hmm_vma_walk->last = addr; - migration_entry_wait(walk->mm, pmdp, addr); - return -EBUSY; + if (!hmm_select_migrate(range)) { + HMM_ASSERT_UNLOCKED(hmm_vma_walk); + hmm_vma_walk->last = addr; + migration_entry_wait(walk->mm, pmdp, addr); + return -EBUSY; + } else + goto out; } /* Report error for everything else */ - pte_unmap(ptep); + + if (hmm_vma_walk->ptelocked) { + pte_unmap_unlock(ptep, hmm_vma_walk->ptl); + hmm_vma_walk->ptelocked = false; + } else + pte_unmap(ptep); + return -EFAULT; } @@ -307,7 +376,12 @@ static int hmm_vma_handle_pte(struct mm_walk *walk, unsigned long addr, if (!vm_normal_page(walk->vma, addr, pte) && !is_zero_pfn(pte_pfn(pte))) { if (hmm_pte_need_fault(hmm_vma_walk, pfn_req_flags, 0)) { - pte_unmap(ptep); + if (hmm_vma_walk->ptelocked) { + pte_unmap_unlock(ptep, hmm_vma_walk->ptl); + hmm_vma_walk->ptelocked = false; + } else + pte_unmap(ptep); + return -EFAULT; } new_pfn_flags = HMM_PFN_ERROR; @@ -320,7 +394,11 @@ static int hmm_vma_handle_pte(struct mm_walk *walk, unsigned long addr, return 0; fault: - pte_unmap(ptep); + if (hmm_vma_walk->ptelocked) { + pte_unmap_unlock(ptep, hmm_vma_walk->ptl); + hmm_vma_walk->ptelocked = false; + } else + pte_unmap(ptep); /* Fault any virtual address we were asked to fault */ return hmm_vma_fault(addr, end, required_fault, walk); } @@ -364,13 +442,18 @@ static int hmm_vma_handle_absent_pmd(struct mm_walk *walk, unsigned long start, required_fault = hmm_range_need_fault(hmm_vma_walk, hmm_pfns, npages, 0); if (required_fault) { - if (softleaf_is_device_private(entry)) + if (softleaf_is_device_private(entry)) { + if (hmm_vma_walk->pmdlocked) { + spin_unlock(hmm_vma_walk->ptl); + hmm_vma_walk->pmdlocked = false; + } return hmm_vma_fault(addr, end, required_fault, walk); + } else return -EFAULT; } - return hmm_pfns_fill(start, end, range, HMM_PFN_ERROR); + return hmm_pfns_fill(start, end, hmm_vma_walk, HMM_PFN_ERROR); } #else static int hmm_vma_handle_absent_pmd(struct mm_walk *walk, unsigned long start, @@ -378,15 +461,100 @@ static int hmm_vma_handle_absent_pmd(struct mm_walk *walk, unsigned long start, pmd_t pmd) { struct hmm_vma_walk *hmm_vma_walk = walk->private; - struct hmm_range *range = hmm_vma_walk->range; unsigned long npages = (end - start) >> PAGE_SHIFT; if (hmm_range_need_fault(hmm_vma_walk, hmm_pfns, npages, 0)) return -EFAULT; - return hmm_pfns_fill(start, end, range, HMM_PFN_ERROR); + return hmm_pfns_fill(start, end, hmm_vma_walk, HMM_PFN_ERROR); } #endif /* CONFIG_ARCH_ENABLE_THP_MIGRATION */ +#ifdef CONFIG_DEVICE_MIGRATION +static int hmm_vma_handle_migrate_prepare_pmd(const struct mm_walk *walk, + pmd_t *pmdp, + unsigned long start, + unsigned long end, + unsigned long *hmm_pfn) +{ + // TODO: implement migration entry insertion + return 0; +} + +static int hmm_vma_handle_migrate_prepare(const struct mm_walk *walk, + pmd_t *pmdp, + pte_t *pte, + unsigned long addr, + unsigned long *hmm_pfn) +{ + // TODO: implement migration entry insertion + return 0; +} + +static int hmm_vma_walk_split(pmd_t *pmdp, + unsigned long addr, + struct mm_walk *walk) +{ + // TODO : implement split + return 0; +} + +#else +static int hmm_vma_handle_migrate_prepare_pmd(const struct mm_walk *walk, + pmd_t *pmdp, + unsigned long start, + unsigned long end, + unsigned long *hmm_pfn) +{ + return 0; +} + +static int hmm_vma_handle_migrate_prepare(const struct mm_walk *walk, + pmd_t *pmdp, + pte_t *pte, + unsigned long addr, + unsigned long *hmm_pfn) +{ + return 0; +} + +static int hmm_vma_walk_split(pmd_t *pmdp, + unsigned long addr, + struct mm_walk *walk) +{ + return 0; +} +#endif + +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 int hmm_vma_walk_pmd(pmd_t *pmdp, unsigned long start, unsigned long end, @@ -397,43 +565,127 @@ static int hmm_vma_walk_pmd(pmd_t *pmdp, unsigned long *hmm_pfns = &range->hmm_pfns[(start - range->start) >> PAGE_SHIFT]; unsigned long npages = (end - start) >> PAGE_SHIFT; + struct mm_struct *mm = walk->vma->vm_mm; unsigned long addr = start; + enum migrate_vma_info minfo; + unsigned long i; pte_t *ptep; pmd_t pmd; + int r = 0; + + minfo = hmm_select_migrate(range); again: - pmd = pmdp_get_lockless(pmdp); - if (pmd_none(pmd)) - return hmm_vma_walk_hole(start, end, -1, walk); + hmm_vma_walk->ptelocked = false; + hmm_vma_walk->pmdlocked = false; + + if (minfo) { + hmm_vma_walk->ptl = pmd_lock(mm, pmdp); + hmm_vma_walk->pmdlocked = true; + pmd = pmdp_get(pmdp); + } else + pmd = pmdp_get_lockless(pmdp); + + if (pmd_none(pmd)) { + r = hmm_vma_walk_hole(start, end, -1, walk); + + if (hmm_vma_walk->pmdlocked) { + spin_unlock(hmm_vma_walk->ptl); + hmm_vma_walk->pmdlocked = false; + } + return r; + } if (thp_migration_supported() && pmd_is_migration_entry(pmd)) { - if (hmm_range_need_fault(hmm_vma_walk, hmm_pfns, npages, 0)) { + if (!minfo) { + if (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; + } + } + for (i = 0; addr < end; addr += PAGE_SIZE, i++) + hmm_pfns[i] &= HMM_PFN_INOUT_FLAGS; + + if (hmm_vma_walk->pmdlocked) { + spin_unlock(hmm_vma_walk->ptl); + hmm_vma_walk->pmdlocked = false; + } + + return 0; + } + + if (pmd_trans_huge(pmd) || !pmd_present(pmd)) { + + if (!pmd_present(pmd)) { + r = hmm_vma_handle_absent_pmd(walk, start, end, hmm_pfns, + pmd); + // If not migrating we are done + if (r || !minfo) { + if (hmm_vma_walk->pmdlocked) { + spin_unlock(hmm_vma_walk->ptl); + hmm_vma_walk->pmdlocked = false; + } + return r; + } + } + + if (pmd_trans_huge(pmd)) { + + /* + * No need to take pmd_lock here if not migrating, + * even if some other thread is splitting the huge + * pmd we will get that event through mmu_notifier callback. + * + * So just read pmd value and check again it's a transparent + * huge or device mapping one and compute corresponding pfn + * values. + */ + + if (!minfo) { + pmd = pmdp_get_lockless(pmdp); + if (!pmd_trans_huge(pmd)) + goto again; + } + + r = hmm_vma_handle_pmd(walk, addr, end, hmm_pfns, pmd); + + // If not migrating we are done + if (r || !minfo) { + if (hmm_vma_walk->pmdlocked) { + spin_unlock(hmm_vma_walk->ptl); + hmm_vma_walk->pmdlocked = false; + } + return r; + } + } + + r = hmm_vma_handle_migrate_prepare_pmd(walk, pmdp, start, end, hmm_pfns); + + if (hmm_vma_walk->pmdlocked) { + spin_unlock(hmm_vma_walk->ptl); + hmm_vma_walk->pmdlocked = false; + } + + if (r == -ENOENT) { + r = hmm_vma_walk_split(pmdp, addr, walk); + if (r) { + /* Split not successful, skip */ + return hmm_pfns_fill(start, end, hmm_vma_walk, HMM_PFN_ERROR); + } + + /* Split successful or "again", reloop */ hmm_vma_walk->last = addr; - pmd_migration_entry_wait(walk->mm, pmdp); return -EBUSY; } - return hmm_pfns_fill(start, end, range, 0); - } - if (!pmd_present(pmd)) - return hmm_vma_handle_absent_pmd(walk, start, end, hmm_pfns, - pmd); + return r; - if (pmd_trans_huge(pmd)) { - /* - * No need to take pmd_lock here, even if some other thread - * is splitting the huge pmd we will get that event through - * mmu_notifier callback. - * - * So just read pmd value and check again it's a transparent - * huge or device mapping one and compute corresponding pfn - * values. - */ - pmd = pmdp_get_lockless(pmdp); - if (!pmd_trans_huge(pmd)) - goto again; + } - return hmm_vma_handle_pmd(walk, addr, end, hmm_pfns, pmd); + if (hmm_vma_walk->pmdlocked) { + spin_unlock(hmm_vma_walk->ptl); + hmm_vma_walk->pmdlocked = false; } /* @@ -445,22 +697,43 @@ static int hmm_vma_walk_pmd(pmd_t *pmdp, if (pmd_bad(pmd)) { if (hmm_range_need_fault(hmm_vma_walk, hmm_pfns, npages, 0)) return -EFAULT; - return hmm_pfns_fill(start, end, range, HMM_PFN_ERROR); + return hmm_pfns_fill(start, end, hmm_vma_walk, HMM_PFN_ERROR); } - ptep = pte_offset_map(pmdp, addr); + if (minfo) { + ptep = pte_offset_map_lock(mm, pmdp, addr, &hmm_vma_walk->ptl); + if (ptep) + hmm_vma_walk->ptelocked = true; + } else + ptep = pte_offset_map(pmdp, addr); if (!ptep) goto again; + for (; addr < end; addr += PAGE_SIZE, ptep++, hmm_pfns++) { - int r; r = hmm_vma_handle_pte(walk, addr, end, pmdp, ptep, hmm_pfns); if (r) { - /* hmm_vma_handle_pte() did pte_unmap() */ + /* hmm_vma_handle_pte() did pte_unmap() / pte_unmap_unlock */ return r; } + + r = hmm_vma_handle_migrate_prepare(walk, pmdp, ptep, addr, hmm_pfns); + if (r == -EAGAIN) { + HMM_ASSERT_UNLOCKED(hmm_vma_walk); + goto again; + } + if (r) { + hmm_pfns_fill(addr, end, hmm_vma_walk, HMM_PFN_ERROR); + break; + } } - pte_unmap(ptep - 1); + + if (hmm_vma_walk->ptelocked) { + pte_unmap_unlock(ptep - 1, hmm_vma_walk->ptl); + hmm_vma_walk->ptelocked = false; + } else + pte_unmap(ptep - 1); + return 0; } @@ -594,6 +867,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) @@ -616,7 +894,7 @@ static int hmm_vma_walk_test(unsigned long start, unsigned long end, (end - start) >> PAGE_SHIFT, 0)) return -EFAULT; - hmm_pfns_fill(start, end, range, HMM_PFN_ERROR); + hmm_pfns_fill(start, end, hmm_vma_walk, HMM_PFN_ERROR); /* Skip this vma and continue processing the next vma. */ return 1; @@ -646,9 +924,17 @@ static const struct mm_walk_ops hmm_walk_ops = { * 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: The range crosses multiple VMAs, or space for hmm_pfns array + * is too low. * * This is similar to get_user_pages(), except that it can read the page tables * without mutating them (ie causing faults). + * + * If want to do migrate after faulting, call hmm_range_fault() with + * HMM_PFN_REQ_MIGRATE and initialize range.migrate field. + * After hmm_range_fault() call migrate_hmm_range_setup() instead of + * migrate_vma_setup() and after that follow normal migrate calls path. + * */ int hmm_range_fault(struct hmm_range *range) { @@ -656,16 +942,34 @@ int hmm_range_fault(struct hmm_range *range) .range = range, .last = range->start, }; - struct mm_struct *mm = range->notifier->mm; + struct mm_struct *mm; + bool is_fault_path; int ret; + /* + * + * 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). + * + */ +#ifdef CONFIG_DEVICE_MIGRATION + is_fault_path = !!range->notifier; + mm = is_fault_path ? range->notifier->mm : range->migrate->vma->vm_mm; +#else + is_fault_path = true; + mm = range->notifier->mm; +#endif 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 (is_fault_path && 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); /* @@ -675,6 +979,19 @@ int hmm_range_fault(struct hmm_range *range) * output, and all >= are still at their input values. */ } while (ret == -EBUSY); + +#ifdef CONFIG_DEVICE_MIGRATION + if (hmm_select_migrate(range) && range->migrate && + hmm_vma_walk.mmu_range.owner) { + // The migrate_vma path has the following initialized + if (is_fault_path) { + range->migrate->vma = hmm_vma_walk.vma; + range->migrate->start = range->start; + range->migrate->end = hmm_vma_walk.end; + } + mmu_notifier_invalidate_range_end(&hmm_vma_walk.mmu_range); + } +#endif return ret; } EXPORT_SYMBOL(hmm_range_fault); -- 2.50.0