From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=170.10.129.124 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796100998; b=4rZbuvCjV8HYkrp+n3+nbP/5ENxHX81zNJ9d3Zufj9Z1WMX+Scd5XQnDblZ4LIZuq1WE 6V+Riod8Q9Oei+v/xJVp1ctMFf45DzvXDmiKys9JhgDE37BvWC6ckaSFcEMIzQIcuXXkn 2tKOGR0/kEaremlEiDI0xuouiM6wtBqXp+ATuKYBtLgx4Rh9LiHAw0WMM+Kb3PynFr1Si K8OGPDbQ0EFpllMWcwXxzxGIVcGfgDeYEuNdXsxks3wgTTvFPAfAF/Q8OCYqhRfgQOJ3b Rl2JhORRYCcSUUk+HAR79FNQbh41hVQHYLNS4hG6Fhrmw/tskbcLuufe4VnpAVn0xVw== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796100998; 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=Bi+O8o47ZmVxRWYOGsNYx9ZCEckwoMdVLfBs0oC5T6U=; b=K/ajk8BgtMWIuBJkxgiFz5sUk2OyaU/AD+oHayGdsT+eWAvSgT4KYnhoolvqz5NohODd kiegemvwjzPD5IpyjX12f4CAcqQqEDgzO+K5M7Z2l2Z02+hdBFd2P4esNRgx3FnhBL2gN 93sfOj9vKJ0XA7wV8ipI0Kni9SzcdKDP8FZbiSZdjvPznnBNPKWZ4qaPGFN3L5DHqEwi4 7VHd5D8ROULScCdUfnLTcqIkBNqkoexIejXCOljQ3luDKvtc9bRmB2J2MiLHeJjUdSDRz zCHdyuoU5D8vOpjg1OXC31GtO6GNcKZez8APkNUCtYYk/GhHHJ8mqO/rymoivuRxFgg== 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=PPn8wK1n; dkim=pass header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=google header.b="WKbc/23m"; arc=none smtp.remote-ip=170.10.129.124 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.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 979BC606A327 for ; Tue, 1 Dec 2026 04:56:37 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 979BC606A327 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.a=rsa-sha256 header.s=mimecast20190719 header.b=PPn8wK1n; dkim=pass (2048-bit key) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=google header.b=WKbc/23m DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1796100997; 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=Bi+O8o47ZmVxRWYOGsNYx9ZCEckwoMdVLfBs0oC5T6U=; b=PPn8wK1ny+cqwAPiQy38rHAy7CEt5kdE5w8Bro+WgnMxm9ieGmTPob8Y656ld9O4k7I0om 6HA3cmgCz4pammr4Qwl2dIXlXSpz2oUI8ub9RzhKcGU92PDMJ6CCHOdCr2xbi88OFCv0hk nk7d/ERgHDsJiiwo6G4hy9N6OOLbEtA= 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-145-W8MKfkAOOjWT8lHseLmKsQ-1; Tue, 1 Dec 2026 04:56:34 +0000 X-MC-Unique: W8MKfkAOOjWT8lHseLmKsQ-1 X-Mimecast-MFC-AGG-ID: W8MKfkAOOjWT8lHseLmKsQ_1796100993 Received: by mail-lf1-f72.google.com with SMTP id 2adb3069b0e04-5bfc3d1fadeso4357648e87.3 for ; Mon, 30 Nov 2026 20:56:34 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=google; t=1796100993; x=1796705793; 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=Bi+O8o47ZmVxRWYOGsNYx9ZCEckwoMdVLfBs0oC5T6U=; b=WKbc/23mQLdTE80TEFsIXeMZGgMCPBT9vVI2/3uQIQlCk0X9nwz76px4MW0IC3umSr aHQnIKqkvRRcJGBF4f5swl+1uH+7ldUsuYEUD6o2f23KGfT6bBh1+hyqRuVHpOWOz2iw NaDi1/WjYY7Unw2emIrGRu8dud7Y85s0q4OqHdxh9ky7KVlokZmnUdB82QIpoI0uI3Ve TMR65ow9icvlD0EgN1CUm3CLKiwtaF+B5O7YC+PleNQvCr+pPJcyDdiq5iCgeFz4c0GP CZWQXWIFXuISGuxKQzK5WY4hTembJHzZsQEUNAFnbgEs9cIuSnS3SE8ntvpXHdKy2Sfk Nq3A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796100993; x=1796705793; 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=Bi+O8o47ZmVxRWYOGsNYx9ZCEckwoMdVLfBs0oC5T6U=; b=rj5hS2em8Q1IUkqX60jdDb5iLfqlEkrt9fZD2l4WAbyJqyZtcYMcVDYsQUp1tYspvL 5uB+mrbTqurFiyep2NNAMBwEhKN4Ag5GKEpg6VY5FvGJJ+UK783lzYetWx7ffd5MuM5o QQ4PKR5KTJwyPsa1yVwmcCSFsLu4HkqNiZAr2o876EpChzX5DoyGP4lU1cROIU8AD33c dsSwE0UZUbt/K8yM858Xa9xlpjuEZsSwufBFPGCDH71MzW3UYVTOmdGFwTBqWU1avxDk 9Ds4WLS2dJkXPwXC5TJmtJTudj0MgScfmM1H32KL2UqLVqWlq/nREHqgaXQY/+KtKbUx 2SOQ== X-Forwarded-Encrypted: i=1; AL+hErE9Hc0ztMChP5NM344WHM1T5eyV/cF67uI/e6GeOs4EEQ17MFpZcykFJkWu8KFF5IJbiDTJtZ3xzH99HIA=@vger.kernel.org X-Gm-Message-State: APuU4Z/CNiPv/fGFYqTmlTr+rXQU2svmCIQovVN7UToNytlXaFnv6+PW vgVSquHcukphKB0NCqbjTrsZ9BNAvGQTTTgYRsHJTqgxqVrRTo9O1QxUnJESzQiD2FEJfLwRPch tiu7/CWXPuWc5MriyuGN46zAlcyezFv3PkKuoqP9PUC4nkVXoULdQRDgDtaDfPbv2 X-Gm-Gg: AZFdTZcib5XOqA12neB4aMVi+DKuOoks9/i8PKMavNRuzo/JxP3zSdC/EvlLjReADVT KDkQiv/zzwBMsw9uxu5AoO+oThpV9Hw9JEcT59zIT9g9XEBbQ79ANBSj6eSHlMHmSIsaxQrX1ve /6Lbyyg0aY4nxVLUjeuuqjpe1iB7+oO8L2070C3NQFLWjVlF6ddjygbhzaZiOP0PZPHqO+zUklN Lt6gQQzWapxYyeH3wsg/9orrxTxes7cYVveCjBZo9oX8I3ozT8XLBVpbaz9xLCZzebIJ2+m3xmO RsAyCvlcBXF7UkvUpP/mwrx1xzz+4v/G1k22tWzAGRIDHqXChU6EF6dbpdVVgQYITROaBPfkdUM 2JkgIHT4B+kI/Mx+S6nQ4R3UWemL/Rr648EDTF56j36pwhJPVYUTovDd13ZVleural20Pnw== X-Received: by 2002:a05:6512:1320:b0:5c1:57d2:c1bd with SMTP id 2adb3069b0e04-5c1bc7ca4b1mr10413683e87.25.1796100992359; Mon, 30 Nov 2026 20:56:32 -0800 (PST) X-Received: by 2002:a05:6512:1320:b0:5c1:57d2:c1bd with SMTP id 2adb3069b0e04-5c1bc7ca4b1mr10413655e87.25.1796100991718; Mon, 30 Nov 2026 20:56:31 -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.56.30 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 30 Nov 2026 20:56:31 -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 14/14] drm/nouveau/svm: add support for migrate on fault Date: Tue, 1 Dec 2026 06:55:31 +0200 Message-ID: <20261201045531.3622535-15-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-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ä Add a direct user for the new HMM_PFN_REQ_MIGRATE flag and implement migrate on fault for nouveau driver. It is gated by the svm_migrate_on_fault module parameter, default off. When enabled, the policy is fully lazy: pages are migrated to VRAM on GPU fault, and SVM_BIND no longer migrates the bound range up front. Added nouveau_dmem_migrate_fault() that faults a range in and does migrate collecting in a single hmm_range_fault() walk. It uses the copy channel lock, the fence poll and the abort handling added by the previous two patches. The migrate on fault path faults a single page and doesn't use MIGRATE_VMA_SELECT_COMPOUND, so a THP covering it is split first and stays split if the migration fails. Faulting in whole THPs is left for later. As an optimization, a migration policy - deciding which faulting ranges are worth migrating to VRAM and which are better left mirrored in system memory - would be useful. This patch establishes the migrate-on-fault mechanism as a basis for later refinement. A GPU write fault on a read-only device private page owned by nouveau is mapped read-only and refaults, because hmm_range_fault() reports the caller's own device private pages without faulting them. This is pre-existing (nouveau_range_fault() behaves the same) and needs a fix in hmm_range_fault(). Signed-off-by: Mika Penttilä --- drivers/gpu/drm/nouveau/nouveau_dmem.c | 281 ++++++++++++++++++++++++- drivers/gpu/drm/nouveau/nouveau_dmem.h | 12 ++ drivers/gpu/drm/nouveau/nouveau_svm.c | 164 ++++++++++----- drivers/gpu/drm/nouveau/nouveau_svm.h | 5 + 4 files changed, 410 insertions(+), 52 deletions(-) diff --git a/drivers/gpu/drm/nouveau/nouveau_dmem.c b/drivers/gpu/drm/nouveau/nouveau_dmem.c index 36c44d3a3c8f..ecb8397951c1 100644 --- a/drivers/gpu/drm/nouveau/nouveau_dmem.c +++ b/drivers/gpu/drm/nouveau/nouveau_dmem.c @@ -41,6 +41,7 @@ #include #include #include +#include /* * FIXME: this is ugly right now we are using TTM to allocate vram and we pin @@ -79,7 +80,7 @@ struct nouveau_dmem_migrate { struct nouveau_channel *chan; /* * Serializes the dmem users of chan (CPU faults in migrate_to_ram(), - * bulk migration, eviction): the copy and clear + * migrate on fault, bulk migration, eviction): the copy and clear * commands they write to its push buffer and the fences they emit. * Held only around those writes, never across memory allocation * (fences are allocated before taking it). @@ -946,11 +947,12 @@ static unsigned long nouveau_dmem_migrate_copy_one(struct nouveau_drm *drm, return 0; } -static void nouveau_dmem_migrate_chunk(struct nouveau_drm *drm, +static unsigned long +nouveau_dmem_migrate_alloc_and_copy(struct nouveau_drm *drm, struct nouveau_svmm *svmm, struct migrate_vma *args, - struct nouveau_dmem_dma_info *dma_info, u64 *pfns) + struct nouveau_dmem_dma_info *dma_info, u64 *pfns, + unsigned long *nr_dma_out, unsigned long *order_out) { - struct nouveau_fence *fence; unsigned long addr = args->start, nr_dma = 0, i; unsigned long order = 0; @@ -972,6 +974,21 @@ static void nouveau_dmem_migrate_chunk(struct nouveau_drm *drm, addr += (1 << order) * PAGE_SIZE; } + *nr_dma_out = nr_dma; + *order_out = order; + return i; +} + +static void nouveau_dmem_migrate_chunk(struct nouveau_drm *drm, + struct nouveau_svmm *svmm, struct migrate_vma *args, + struct nouveau_dmem_dma_info *dma_info, u64 *pfns) +{ + struct nouveau_fence *fence; + unsigned long nr_dma, order, i; + + i = nouveau_dmem_migrate_alloc_and_copy(drm, svmm, args, dma_info, pfns, + &nr_dma, &order); + nouveau_dmem_fence_new(drm, &fence); migrate_vma_pages(args); nouveau_dmem_fence_done(&fence); @@ -984,6 +1001,262 @@ static void nouveau_dmem_migrate_chunk(struct nouveau_drm *drm, migrate_vma_finalize(args); } +/* + * Encode an hmm_range_fault() pfn for @start into the nouveau PFNMAP format, + * shared by nouveau_hmm_convert_pfn() and the in-place map in + * nouveau_dmem_migrate_fault(). + */ +u64 nouveau_dmem_hmm_pfn_to_map(unsigned long hmm_pfn, unsigned long start, + unsigned long *map_addr, unsigned int *page_shift) +{ + unsigned long order; + struct page *page; + u64 pfn; + + *map_addr = start; + *page_shift = PAGE_SHIFT; + + if (!(hmm_pfn & HMM_PFN_VALID)) + return NVIF_VMM_PFNMAP_V0_NONE; + + page = hmm_pfn_to_page(hmm_pfn); + + order = hmm_pfn_to_map_order(hmm_pfn); + if (order) { + *page_shift = order + PAGE_SHIFT; + *map_addr = start & ~((1UL << *page_shift) - 1); + page -= (start - *map_addr) >> PAGE_SHIFT; + } + + if (is_device_private_page(page)) + pfn = nouveau_dmem_page_addr(page) | + NVIF_VMM_PFNMAP_V0_V | NVIF_VMM_PFNMAP_V0_VRAM; + else + pfn = page_to_phys(page) | + NVIF_VMM_PFNMAP_V0_V | NVIF_VMM_PFNMAP_V0_HOST; + if (hmm_pfn & HMM_PFN_WRITE) + pfn |= NVIF_VMM_PFNMAP_V0_W; + + return pfn; +} + +/* + * Fault a range in and migrate it to VRAM. + * Returns 0 on success, or an error (so the caller + * falls back to nouveau_range_fault()) if nothing could be mapped. + */ +int +nouveau_dmem_migrate_fault(struct nouveau_drm *drm, struct nouveau_svmm *svmm, + struct mmu_interval_notifier *notifier, + unsigned long start, unsigned long end, + unsigned long hmm_flags, + u64 *out_pfn0, unsigned long *out_addr, + unsigned int *out_shift) +{ + unsigned long timeout = + jiffies + msecs_to_jiffies(HMM_RANGE_DEFAULT_TIMEOUT); + unsigned long npages = (end - start) >> PAGE_SHIFT; + struct mm_struct *mm = notifier->mm; + struct nouveau_dmem_dma_info *dma_info; + struct nouveau_fence *fence = NULL; + struct migrate_vma migrate = { + .start = start, + .end = end, + .pgmap_owner = drm->dev, + /* + * No MIGRATE_VMA_SELECT_COMPOUND: the fault range is a single + * page, so a THP covering it is split before migrating, and + * stays split if the migration then fails. Faulting in whole + * THPs is left for later. + */ + .flags = MIGRATE_VMA_SELECT_SYSTEM, + }; + struct hmm_range range = { + .notifier = notifier, + .start = start, + .end = end, + .dev_private_owner = drm->dev, + .migrate = &migrate, + .default_flags = hmm_flags | HMM_PFN_REQ_MIGRATE, + }; + unsigned long nr_dma, order, n, i; + unsigned long hmm_pfn0 = 0; + bool migrated; + u64 *pfns; + int ret, fret = 0; + + if (!drm->dmem) + return -ENODEV; + + migrate.src = kcalloc(npages, sizeof(*migrate.src), GFP_KERNEL); + if (!migrate.src) + return -ENOMEM; + migrate.dst = kcalloc(npages, sizeof(*migrate.dst), GFP_KERNEL); + if (!migrate.dst) { + ret = -ENOMEM; + goto out_free_src; + } + dma_info = kmalloc_objs(*dma_info, npages); + if (!dma_info) { + ret = -ENOMEM; + goto out_free_dst; + } + pfns = nouveau_pfns_alloc(npages); + if (!pfns) { + ret = -ENOMEM; + goto out_free_dma; + } + + /* hmm_range_fault() shares the source pfn array with migrate_vma. */ + range.hmm_pfns = migrate.src; + + do { + mmap_read_lock(mm); + range.notifier_seq = mmu_interval_read_begin(notifier); + + ret = hmm_range_fault(&range); + /* + * Snapshot the pfn before migrate_hmm_range_setup() overwrites the + * shared array; used to map in place if nothing migrates. + */ + if (!ret) + hmm_pfn0 = range.hmm_pfns[0]; + migrate_hmm_range_setup(&range); + if (ret) { + migrate_vma_pages(&migrate); + migrate_vma_finalize(&migrate); + goto unlock; + } + + n = nouveau_dmem_migrate_alloc_and_copy(drm, svmm, &migrate, + dma_info, pfns, + &nr_dma, &order); + + /* Only a migrated page has an in-flight CE copy to unblock below. */ + for (i = 0, migrated = false; i < npages; i++) { + if (migrate.dst[i]) { + migrated = true; + break; + } + } + + /* + * Track a copy fence only when a copy was enqueued; waiting on an + * empty fence would block until the fault's CTXSW_TIMEOUT. + */ + if (migrated) + fret = nouveau_dmem_fence_new(drm, &fence); + + /* + * The fault-in path must replay the faulting channel before waiting on + * the copy fence: with the fault still unreplayed the copy does not + * make progress. GPU has no mapping so it might refault, until it + * gets the migrated or mirrored mapping; it is mapped only once the + * copy is confirmed. In practice such a refault doesn't hold up the + * copy; as a safety net, if the copy doesn't complete by the + * deadline, the migration is aborted and the fault is mapped in place. + */ + if (drm->svm && migrated) + nouveau_svm_fault_replay(drm->svm); + + /* Without a fence (fret set) the copy can't be confirmed. */ + if (migrated && !fret) + fret = nouveau_dmem_fence_poll(&fence); + + /* Commit the migration only once the copy is confirmed. */ + if (fret) + nouveau_dmem_migrate_abort(drm, &migrate); + migrate_vma_pages(&migrate); + + /* + * Don't map destination pages that migrate_vma_pages() didn't + * migrate: finalize frees them and restores the source. + */ + for (i = 0; i < npages; i++) + if (!(migrate.src[i] & MIGRATE_PFN_MIGRATE)) + pfns[i] = NVIF_VMM_PFNMAP_V0_NONE; + + mutex_lock(&svmm->mutex); + if (fret) { + /* Copy did not complete; don't map a half-copied page. */ + ret = -EIO; + } else if (mmu_interval_read_retry(notifier, range.notifier_seq)) { + ret = -EBUSY; + } else { + unsigned long map_addr = migrate.start; + unsigned int page_shift = PAGE_SHIFT + order; + + /* + * If nothing migrated might still be able to mirror. + * In that case use the snapshot hmm_pfn. + * npages == 1 always today. + * + * A read-only device private page of ours is reported + * without HMM_PFN_WRITE even for a write fault, because + * hmm_range_fault() doesn't fault the caller's own device + * private pages. It is then mapped read-only and the GPU + * faults again, as with nouveau_range_fault(). That is + * pre-existing and needs a fix in hmm_range_fault(). + */ + if (npages == 1 && !(pfns[0] & NVIF_VMM_PFNMAP_V0_V)) + pfns[0] = nouveau_dmem_hmm_pfn_to_map(hmm_pfn0, + start, &map_addr, &page_shift); + + /* + * Nothing to map: leave the GPU mapping alone, the + * caller falls back to nouveau_range_fault(). + */ + if (pfns[0] & NVIF_VMM_PFNMAP_V0_V) { + /* On failure the caller falls back, then cancels */ + ret = __nouveau_pfns_map_locked(svmm, map_addr, + pfns, n, + page_shift); + if (!ret) { + *out_pfn0 = pfns[0]; + *out_addr = map_addr; + *out_shift = page_shift; + } + } + } + /* + * The migration may be committed without being mapped, and our + * own MIGRATE invalidation didn't clear the GPU page table: drop + * a mapping an earlier fault left to a source page that finalize + * may free. + */ + if (ret && !fret) + nouveau_svmm_invalidate(svmm, migrate.start, migrate.end); + mutex_unlock(&svmm->mutex); + + if (fret) + NV_ERROR(drm, + "svm: migrate copy did not complete (%d) for %lx-%lx\n", + fret, start, end); + + /* After an aborted copy the engine may still use the mappings */ + while (!fret && nr_dma--) { + dma_unmap_page(drm->dev->dev, dma_info[nr_dma].dma_addr, + dma_info[nr_dma].size, DMA_BIDIRECTIONAL); + } + migrate_vma_finalize(&migrate); +unlock: + mmap_read_unlock(mm); + } while (ret == -EBUSY && !time_after(jiffies, timeout)); + + /* Nothing migrated or mappable in place, fallback to nouveau_range_fault(). */ + if (!ret && !(pfns[0] & NVIF_VMM_PFNMAP_V0_V)) + ret = -ENOENT; + + nouveau_pfns_free(pfns); +out_free_dma: + kfree(dma_info); +out_free_dst: + kfree(migrate.dst); +out_free_src: + kfree(migrate.src); + return ret; +} + int nouveau_dmem_migrate_vma(struct nouveau_drm *drm, struct nouveau_svmm *svmm, diff --git a/drivers/gpu/drm/nouveau/nouveau_dmem.h b/drivers/gpu/drm/nouveau/nouveau_dmem.h index 64da5d3635c8..91e8910cadbf 100644 --- a/drivers/gpu/drm/nouveau/nouveau_dmem.h +++ b/drivers/gpu/drm/nouveau/nouveau_dmem.h @@ -27,6 +27,7 @@ struct drm_file; struct nouveau_drm; struct nouveau_svmm; struct hmm_range; +struct mmu_interval_notifier; #if IS_ENABLED(CONFIG_DRM_NOUVEAU_SVM) void nouveau_dmem_init(struct nouveau_drm *); @@ -39,7 +40,18 @@ int nouveau_dmem_migrate_vma(struct nouveau_drm *drm, struct vm_area_struct *vma, unsigned long start, unsigned long end); +int nouveau_dmem_migrate_fault(struct nouveau_drm *drm, + struct nouveau_svmm *svmm, + struct mmu_interval_notifier *notifier, + unsigned long start, + unsigned long end, + unsigned long hmm_flags, + u64 *out_pfn0, + unsigned long *out_addr, + unsigned int *out_shift); unsigned long nouveau_dmem_page_addr(struct page *page); +u64 nouveau_dmem_hmm_pfn_to_map(unsigned long hmm_pfn, unsigned long start, + unsigned long *map_addr, unsigned int *page_shift); #else /* IS_ENABLED(CONFIG_DRM_NOUVEAU_SVM) */ static inline void nouveau_dmem_init(struct nouveau_drm *drm) {} diff --git a/drivers/gpu/drm/nouveau/nouveau_svm.c b/drivers/gpu/drm/nouveau/nouveau_svm.c index c519c28164fa..a67395207032 100644 --- a/drivers/gpu/drm/nouveau/nouveau_svm.c +++ b/drivers/gpu/drm/nouveau/nouveau_svm.c @@ -37,6 +37,7 @@ #include #include #include +#include struct nouveau_svm { struct nouveau_drm *drm; @@ -78,6 +79,17 @@ struct nouveau_svm { #define SVM_DBG(s,f,a...) NV_DEBUG((s)->drm, "svm: "f"\n", ##a) #define SVM_ERR(s,f,a...) NV_WARN((s)->drm, "svm: "f"\n", ##a) +/* + * When set, migrate the faulting range to VRAM on GPU fault (default off). + * Like the other nouveau parameters, it is documented by MODULE_PARM_DESC + * only, and it matters only with the experimental SVM support. + */ +static bool nouveau_svm_migrate_on_fault; +module_param_named(svm_migrate_on_fault, nouveau_svm_migrate_on_fault, bool, + 0644); +MODULE_PARM_DESC(svm_migrate_on_fault, + "migrate system memory to VRAM on GPU fault (default off)"); + struct nouveau_pfnmap_args { struct nvif_ioctl_v0_hdr i; struct nvif_ioctl_mthd_v0_hdr m; @@ -182,9 +194,15 @@ nouveau_svmm_bind(struct drm_device *dev, void *data, addr = max(addr, vma->vm_start); next = min(vma->vm_end, end); - /* This is a best effort so we ignore errors */ - nouveau_dmem_migrate_vma(cli->drm, cli->svm.svmm, vma, addr, - next); + /* + * Best effort. With svm_migrate_on_fault the policy is lazy: + * pages migrate to VRAM on GPU fault, not up front here. Pages + * the GPU already maps in system memory don't fault again, so + * they stay there; that is part of the opt-in policy. + */ + if (!nouveau_svm_migrate_on_fault) + nouveau_dmem_migrate_vma(cli->drm, cli->svm.svmm, vma, + addr, next); addr = next; } @@ -390,7 +408,7 @@ nouveau_svmm_init(struct drm_device *dev, void *data, } /* Issue fault replay for GPU to retry accesses that faulted previously. */ -static void +void nouveau_svm_fault_replay(struct nouveau_svm *svm) { SVM_DBG(svm, "replay"); @@ -514,6 +532,8 @@ nouveau_svm_fault_cache(struct nouveau_svm *svm, struct svm_notifier { struct mmu_interval_notifier notifier; struct nouveau_svmm *svmm; + /* Task running this fault's migrate-on-fault walk, or NULL */ + struct task_struct *migrating; }; static bool nouveau_svm_range_invalidate(struct mmu_interval_notifier *mni, @@ -527,6 +547,17 @@ static bool nouveau_svm_range_invalidate(struct mmu_interval_notifier *mni, range->owner == sn->svmm->vmm->cli->drm->dev) return true; + /* + * Ignore the invalidation of this fault's own migrate-on-fault walk, + * which runs in the walking task; otherwise its MMU_NOTIFY_MIGRATE + * bumps the seq and read_retry() spins to timeout. Other migrations + * on the device (CPU faults, bind) must still invalidate. + */ + if (range->event == MMU_NOTIFY_MIGRATE && + range->owner == sn->svmm->vmm->cli->drm->dev && + READ_ONCE(sn->migrating) == current) + return true; + /* * serializes the update to mni->invalidate_seq done by caller and * prevents invalidation of the PTE from progressing while HW is being @@ -551,48 +582,25 @@ static void nouveau_hmm_convert_pfn(struct nouveau_drm *drm, struct hmm_range *range, struct nouveau_pfnmap_args *args) { - struct page *page; + unsigned long map_addr; + unsigned int page_shift; /* * The address prepared here is passed through nvif_object_ioctl() * to an eventual DMA map in something like gp100_vmm_pgt_pfn() * * This is all just encoding the internal hmm representation into a - * different nouveau internal representation. + * different nouveau internal representation; see the shared encoder + * nouveau_dmem_hmm_pfn_to_map() (also widens to the CPU folio order). */ - if (!(range->hmm_pfns[0] & HMM_PFN_VALID)) { - args->p.phys[0] = 0; - return; + args->p.phys[0] = nouveau_dmem_hmm_pfn_to_map(range->hmm_pfns[0], + args->p.addr, &map_addr, + &page_shift); + if (page_shift != PAGE_SHIFT) { + args->p.addr = map_addr; + args->p.page = page_shift; + args->p.size = 1UL << page_shift; } - - page = hmm_pfn_to_page(range->hmm_pfns[0]); - /* - * Only map compound pages to the GPU if the CPU is also mapping the - * page as a compound page. Otherwise, the PTE protections might not be - * consistent (e.g., CPU only maps part of a compound page). - * Note that the underlying page might still be larger than the - * CPU mapping (e.g., a PUD sized compound page partially mapped with - * a PMD sized page table entry). - */ - if (hmm_pfn_to_map_order(range->hmm_pfns[0])) { - unsigned long addr = args->p.addr; - - args->p.page = hmm_pfn_to_map_order(range->hmm_pfns[0]) + - PAGE_SHIFT; - args->p.size = 1UL << args->p.page; - args->p.addr &= ~(args->p.size - 1); - page -= (addr - args->p.addr) >> PAGE_SHIFT; - } - if (is_device_private_page(page)) - args->p.phys[0] = nouveau_dmem_page_addr(page) | - NVIF_VMM_PFNMAP_V0_V | - NVIF_VMM_PFNMAP_V0_VRAM; - else - args->p.phys[0] = page_to_phys(page) | - NVIF_VMM_PFNMAP_V0_V | - NVIF_VMM_PFNMAP_V0_HOST; - if (range->hmm_pfns[0] & HMM_PFN_WRITE) - args->p.phys[0] |= NVIF_VMM_PFNMAP_V0_W; } static int nouveau_atomic_range_fault(struct nouveau_svmm *svmm, @@ -728,6 +736,46 @@ static int nouveau_range_fault(struct nouveau_svmm *svmm, return ret; } +static int nouveau_range_fault_and_migrate(struct nouveau_svmm *svmm, + struct nouveau_drm *drm, + struct nouveau_pfnmap_args *args, + unsigned long hmm_flags, + struct svm_notifier *notifier) +{ + struct mm_struct *mm = svmm->notifier.mm; + unsigned long start = args->p.addr; + unsigned long end = start + args->p.size; + unsigned long map_addr; + unsigned int page_shift; + u64 pfn0; + int ret; + + ret = mmu_interval_notifier_insert(¬ifier->notifier, mm, + start, args->p.size, + &nouveau_svm_mni_ops); + if (ret) + return ret; + + WRITE_ONCE(notifier->migrating, current); + ret = nouveau_dmem_migrate_fault(drm, svmm, ¬ifier->notifier, + start, end, hmm_flags, + &pfn0, &map_addr, &page_shift); + WRITE_ONCE(notifier->migrating, NULL); + + if (!ret) { + args->p.phys[0] = pfn0; + if (page_shift != PAGE_SHIFT) { + args->p.addr = map_addr; + args->p.page = page_shift; + args->p.size = 1UL << page_shift; + } + } + + mmu_interval_notifier_remove(¬ifier->notifier); + + return ret; +} + static void nouveau_svm_fault(struct work_struct *work) { @@ -843,14 +891,26 @@ nouveau_svm_fault(struct work_struct *work) } notifier.svmm = svmm; - if (atomic) + notifier.migrating = NULL; + if (atomic) { ret = nouveau_atomic_range_fault(svmm, svm->drm, args, __struct_size(args), ¬ifier); - else + } else if (nouveau_svm_migrate_on_fault && + buffer->fault[fi]->access != FAULT_ACCESS_PREFETCH) { + /* Fault+migrate in one walk; fall back if nothing migrated. */ + ret = nouveau_range_fault_and_migrate(svmm, svm->drm, + args, hmm_flags, + ¬ifier); + if (ret) + ret = nouveau_range_fault(svmm, svm->drm, args, + __struct_size(args), + hmm_flags, ¬ifier); + } else { ret = nouveau_range_fault(svmm, svm->drm, args, __struct_size(args), hmm_flags, ¬ifier); + } mmput(mm); limit = args->p.addr + args->p.size; @@ -934,22 +994,30 @@ nouveau_pfns_free(u64 *pfns) kfree(args); } -void -nouveau_pfns_map(struct nouveau_svmm *svmm, struct mm_struct *mm, - unsigned long addr, u64 *pfns, unsigned long npages, - unsigned int page_shift) +int +__nouveau_pfns_map_locked(struct nouveau_svmm *svmm, unsigned long addr, + u64 *pfns, unsigned long npages, + unsigned int page_shift) { struct nouveau_pfnmap_args *args = nouveau_pfns_to_args(pfns); + lockdep_assert_held(&svmm->mutex); + args->p.addr = addr; args->p.size = npages << page_shift; args->p.page = page_shift; - mutex_lock(&svmm->mutex); - - nvif_object_ioctl(&svmm->vmm->vmm.object, args, - struct_size(args, p.phys, npages), NULL); + return nvif_object_ioctl(&svmm->vmm->vmm.object, args, + struct_size(args, p.phys, npages), NULL); +} +void +nouveau_pfns_map(struct nouveau_svmm *svmm, struct mm_struct *mm, + unsigned long addr, u64 *pfns, unsigned long npages, + unsigned int page_shift) +{ + mutex_lock(&svmm->mutex); + __nouveau_pfns_map_locked(svmm, addr, pfns, npages, page_shift); mutex_unlock(&svmm->mutex); } diff --git a/drivers/gpu/drm/nouveau/nouveau_svm.h b/drivers/gpu/drm/nouveau/nouveau_svm.h index 3fd78662f17e..c24b717819a5 100644 --- a/drivers/gpu/drm/nouveau/nouveau_svm.h +++ b/drivers/gpu/drm/nouveau/nouveau_svm.h @@ -5,6 +5,7 @@ struct drm_device; struct drm_file; struct nouveau_drm; +struct nouveau_svm; struct nouveau_svmm { struct mmu_notifier notifier; @@ -35,6 +36,10 @@ void nouveau_pfns_free(u64 *pfns); void nouveau_pfns_map(struct nouveau_svmm *svmm, struct mm_struct *mm, unsigned long addr, u64 *pfns, unsigned long npages, unsigned int page_shift); +int __nouveau_pfns_map_locked(struct nouveau_svmm *svmm, unsigned long addr, + u64 *pfns, unsigned long npages, + unsigned int page_shift); +void nouveau_svm_fault_replay(struct nouveau_svm *svm); #else /* IS_ENABLED(CONFIG_DRM_NOUVEAU_SVM) */ static inline void nouveau_svm_init(struct nouveau_drm *drm) {} static inline void nouveau_svm_fini(struct nouveau_drm *drm) {} -- 2.55.0