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 68A7E606A31A for ; Tue, 1 Dec 2026 04:57:10 +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=XD3fm0uf Received: by kanga.kvack.org (Postfix) id A090B6B05C5; Mon, 30 Nov 2026 23:56:39 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 91DCF6B05C4; Mon, 30 Nov 2026 23:56:39 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 7BE896B05C5; Mon, 30 Nov 2026 23:56:39 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by kanga.kvack.org (Postfix) with ESMTP id EA2746B05C3 for ; Mon, 30 Nov 2026 23:56:38 -0500 (EST) Received: from smtpin17.hostedemail.com (lb01a-stub [10.200.18.249]) by unirelay10.hostedemail.com (Postfix) with ESMTP id 3F7B7C0D8C for ; Tue, 1 Dec 2026 04:56:38 +0000 (UTC) X-FDA: 85494203676.17.4F4CB03 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by imf18.hostedemail.com (Postfix) with ESMTP id ED2281C0009 for ; Tue, 1 Dec 2026 04:56:35 +0000 (UTC) Authentication-Results: imf18.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=XD3fm0uf; dmarc=pass (policy=quarantine) header.from=redhat.com; spf=pass (imf18.hostedemail.com: domain of mpenttil@redhat.com designates 170.10.133.124 as permitted sender) smtp.mailfrom=mpenttil@redhat.com ARC-Seal: i=1; a=rsa-sha256; d=hostedemail.com; s=arc-20220608; cv=none; t=1796100996; b=KAiguT23W+pEdr7chE+NEIwiBzpNGKHLQemq69mMVz5XKgc64pWJtmtg3jf6ofIbeU8OKr b1xT1eS0GzeJz3/cwOsvraBsE0MejZ2KBRbkRnrAdrZrzC9/awi3ak3TX2VQRBzsbEHVgL l1nYNSUn73cPTMbjmyjZNxUzj9z1Fk4= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1796100996; 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=0ebKXXhUSYDbaVOf7o+b7IN1o6tK35/PnJeR1VDU1AI=; b=um4R2qVmnVaiiuYxvYvoCOUI3lsfTVvaMfer4p2TM0IVcmHcfoaf3PcQ8J1sn7iDH7usHk HJohg8SOT2Mzh7HzsQZhE9bACKQCW804Vq3/KOGDb32poreGImqZKwvmQR9WwrQj9e1vuE tSQP+3ITiJFwzEYAFrAi2uRPzW0sdc4= ARC-Authentication-Results: i=1; imf18.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=XD3fm0uf; dmarc=pass (policy=quarantine) header.from=redhat.com; spf=pass (imf18.hostedemail.com: domain of mpenttil@redhat.com designates 170.10.133.124 as permitted sender) smtp.mailfrom=mpenttil@redhat.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1796100995; 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=0ebKXXhUSYDbaVOf7o+b7IN1o6tK35/PnJeR1VDU1AI=; b=XD3fm0uf0CuVrJUEx8UyIQxmOaumlUU6NF6GVBH1owfxaA7IXb2/FgUqxtbakqmDe0cjA0 j8BGLvojj/yv+4CsmQ5fdjAnmi8zmdO8wJ/aopGH/VT6ZUfWcUHVKX43kLbCw37y+yKRyI UGJEsbp02QCYGiGxEWLQWHwV6ULRPcE= 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-164-TeWr1dvjMqGrFmvKafudYQ-1; Tue, 1 Dec 2026 04:56:32 +0000 X-MC-Unique: TeWr1dvjMqGrFmvKafudYQ-1 X-Mimecast-MFC-AGG-ID: TeWr1dvjMqGrFmvKafudYQ_1796100990 Received: by mail-lf1-f70.google.com with SMTP id 2adb3069b0e04-5c00ca36371so3439550e87.0 for ; Mon, 30 Nov 2026 20:56:32 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796100990; x=1796705790; 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=0ebKXXhUSYDbaVOf7o+b7IN1o6tK35/PnJeR1VDU1AI=; b=AnOt3Cc0s6QClCt1cVGYUVLqGKaWkicOvi97sgXJ2nPOEjTEthuHsQ6fnSJ4SeLQHv gOLBCPcrnRY4f+96bEiSAdfoM/KxSjzjdb5Kk8s0msnw5Uvs5PRutFDDIfIp6ZkHRg7O Dow6c+VLQ1DLFxU+KM1GtZ6VhmiFXtUJXdr5PHn6PJJBwkg4enFfaRmigCKtZQlY0M1z y36psYLBvnaBSnGH7wlUPnvMI6CM/StwZ4eZEHiR5+/dKwNAs4xztzgKQf6kiQ/bRTIX LqY21HkCbW90ZClqFBGrk12NJZ4s6tBUf7XBHscYX9LoClFrd77SrRVf3DOGeY9KwAr9 Fy/g== X-Gm-Message-State: APuU4Z8ixosgnTtjRqbZ2yZbrGqZXCHgQaJXvzsdClgjYuoBXD0AEqlm qgz7GMqXsLvm6FDjjqq58o0asqtq5TVUdl+tHEFElS5r0do1XnuA/EXgxPpFz93UfzzS5nujppP tW8RsogM72hKMuw8SXYcb3/eRt4+LmYQTNdpgV2xAGQdNI4Qnr0dAD6EsvwhU+nI4RKUSmqC9bL Y6BjRNTxXQLRfOublZoSPobygZoG6pzaB0kPb5OA== X-Gm-Gg: AZFdTZd+Rv7zTp2QikVnFZ3lUvmv9ja1jjUdZT3I9vJ09Q1kn0344NIPUvsfVNxlk3Y Bb2IvKzP6Y7KhkGSutDhb2PGqAicwgjUzKqDlBYpOFw23IIdfOtDKCj7Tuy8ZZB6MPCq7U2WS6R FSuWBNTdXgvBQdgusDJSy9M0gckqua3/m3fjTsvXS4ytsOiovaI/r95i9u9Va5W6/mzSoLjKjoq 0wPY4w42AkPOj4lyFJCsrzulV2rQ+9ZuuQmwT2UnDRGjbjT1Am5c+P1XvyLm9GCh7RNpnx56I0L Qih5SGBNeIAku8q4jjrjocPadmvSlsKbihBgfUalU1tKxM6W62MTtTddWOdGuDbdNbNtWFlYaNQ KmLAeCesR4Wzq9RCFP/TUfQMbAuJ1KVKRBZ9Jm1j7SF35mq3L0E+d06vVPiTNNAd7YgEi7w== X-Received: by 2002:a05:6512:239f:b0:5c1:5e33:6994 with SMTP id 2adb3069b0e04-5c1bc7ad610mr11134491e87.12.1796100990195; Mon, 30 Nov 2026 20:56:30 -0800 (PST) X-Received: by 2002:a05:6512:239f:b0:5c1:5e33:6994 with SMTP id 2adb3069b0e04-5c1bc7ad610mr11134461e87.12.1796100989579; Mon, 30 Nov 2026 20:56:29 -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.27 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 30 Nov 2026 20:56:27 -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 13/14] drm/nouveau/dmem: commit migrate_to_ram() only after the copy is confirmed Date: Tue, 1 Dec 2026 06:55:30 +0200 Message-ID: <20261201045531.3622535-14-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: sxZil66EL3atwSVzuXLRMUkZjPljXroU-k9XvYpGRSE_1796100990 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Stat-Signature: qsety3xtqw3ij69rjwj5of1ok6q5zfp5 X-Rspamd-Server: rspam03 X-Rspam-User: 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-Rspamd-Queue-Id: ED2281C0009 X-HE-Tag: 1796100995-135917 X-HE-Meta: U2FsdGVkX18iK5cTonvdUjwZtTlYaQyabGY1PRrFMZtYx/WWjPkVHCLZdsiViRr89Esa7oY2RV0IEnj5VPQ1NisNLGJl6CnoTqkuSvH3YkTn6SqgSsmMsnp8im7yWAHUaVvr7/5bKXbsvHgycEmP2ckxVVhbH8jngARa91sK5mcMUd83LF4EkZeBB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RqfQ/ilXufia4n24B5CSX2thX8Ju5wjIsjcWZF3HvjG2ai68F/CHMsqriUin2p/3CgJiTXbcAWHmnLugwHp9ke4KU0ZImOpzrbjNVXdC+z/s0i0w2IpYkltOdJgLr5o6eYHjXM/gY1KVodeY+ljLuaC26cYZK/KJw8M6zsFSBQh7jJAWRxde1LAYl2fRTXilZPlCmfGtifBrTSJk1tyRsZM54dVrMc63Mh8Wx5ykkaZ7olbDT/V+2oqbMJ19WU24VkFE1xoVoaPqy24qCYUQzJCUvntfiuJ/yQcGmtJheWGF0cv73U28OgJ6mbfysOphhAhrb0cGw3vfh4EjBmW7fFB8D0OwoEmh44c1aXkKpNZgimFsOihEOl3Nw== 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ä nouveau_dmem_migrate_to_ram() ignores the nouveau_fence_new() result, commits the migration with migrate_vma_pages() before the copy has completed, and then uses the lazy, interrupt driven fence wait, whose completion interrupt can be missed. A failed fence allocation leaves the fence uninitialized, and a copy that doesn't complete in time leaves the process with a page that wasn't fully copied. Check the fence, poll it a jiffy at a time with a deadline (a tighter interval was found to regress to the timeout on hardware), and commit the migration only once the copy is confirmed: a fence signaled with an error, as a channel kill does, doesn't count. Otherwise abort it: finalize restores the device private pte with the data intact, and the fault returns SIGBUS (VM_FAULT_OOM if the fence couldn't be allocated). Letting the CPU fault again instead would, with a wedged copy engine, queue another copy and leak another page on every retry. The destination page and its DMA mapping may still be in use by the copy engine, so they are leaked rather than freed. That is rare, as the copy didn't complete within the deadline. The early returns after allocating the src/dst arrays now free them too. A GPU access to the page while the copy runs faults, and the GPU fault worker waits for this migration before it can replay that fault. A copy was seen on hardware not to progress until the faulting channel was replayed, so this can hold up the copy. The deadline covers that case as well. Fixes: c32287471077 ("gpu/drm/nouveau: enable THP support for GPU memory migration") Fixes: bfe69ef94aee ("nouveau: simplify nouveau_dmem_migrate_to_ram") Signed-off-by: Mika Penttilä --- drivers/gpu/drm/nouveau/nouveau_dmem.c | 109 +++++++++++++++++++++++-- 1 file changed, 100 insertions(+), 9 deletions(-) diff --git a/drivers/gpu/drm/nouveau/nouveau_dmem.c b/drivers/gpu/drm/nouveau/nouveau_dmem.c index 6824f5401d00..36c44d3a3c8f 100644 --- a/drivers/gpu/drm/nouveau/nouveau_dmem.c +++ b/drivers/gpu/drm/nouveau/nouveau_dmem.c @@ -40,6 +40,7 @@ #include #include #include +#include /* * FIXME: this is ugly right now we are using TTM to allocate vram and we pin @@ -213,6 +214,70 @@ static void nouveau_dmem_fence_done(struct nouveau_fence **fence) } } +/* + * Poll the copy fence instead of the lazy (interrupt-driven) wait since + * completion IRQ is randomly missed, hitting the timeout. Poll gently, a + * jiffy at a time: a tighter interval (usleep_range()) polls the fence far + * more often and was found on hardware to regress to the timeout. A CPU + * fault on device memory thus takes at least a jiffy. The deadline is + * well beyond any copy time and only catches a copy that won't complete. + * + * A fence signaled with an error, as a channel kill does to its pending + * fences, doesn't confirm the copy. + * + * Returns 0 on completion, -EBUSY on timeout, -EIO if the fence signaled + * with an error. Consumes the fence reference. + */ +static int nouveau_dmem_fence_poll(struct nouveau_fence **fence) +{ + unsigned long deadline = jiffies + 30 * HZ; + int ret = 0; + + while (!nouveau_fence_done(*fence)) { + if (time_after(jiffies, deadline)) { + ret = -EBUSY; + break; + } + schedule_timeout_uninterruptible(msecs_to_jiffies(1)); + } + if (!ret && dma_fence_get_status(&(*fence)->base) < 0) + ret = -EIO; + nouveau_fence_unref(fence); + + return ret; +} + +/* + * The copy could not be confirmed (no fence, timeout, or a fence signaled with + * an error): don't migrate, so finalize restores the original ptes and the + * source stays valid. The copy engine may still be writing the destination, + * and there is no telling when it stops, so keep the destination folios: take + * them out of dst[], so that migrate_vma_pages() doesn't migrate those entries + * and finalize doesn't free them, and keep their allocation reference. The + * caller skips the DMA unmap. Only the destination is written by the copy; + * the source is restored and stays in use. + * + * This is rare: the copy didn't complete within the deadline, because the + * engine is wedged or held up. Leaking is the safe choice then. The leaked + * destination stays allocated with its DMA mapping; a VRAM destination keeps + * its chunk in use, so device teardown can't complete afterwards. + */ +static void nouveau_dmem_migrate_abort(struct nouveau_drm *drm, + struct migrate_vma *args) +{ + unsigned long i; + + for (i = 0; i < args->npages; i++) { + struct page *dpage = migrate_pfn_to_page(args->dst[i]); + + if (dpage) { + folio_unlock(page_folio(dpage)); + args->dst[i] = 0; + } + } + NV_ERROR(drm, "svm: copy not confirmed, migration aborted, pages leaked\n"); +} + static int nouveau_dmem_copy_folio(struct nouveau_drm *drm, struct folio *sfolio, struct folio *dfolio, struct nouveau_dmem_dma_info *dma_info) @@ -244,11 +309,11 @@ static int nouveau_dmem_copy_folio(struct nouveau_drm *drm, static vm_fault_t nouveau_dmem_migrate_to_ram(struct vm_fault *vmf) { struct nouveau_drm *drm = page_to_drm(vmf->page); - struct nouveau_fence *fence; + struct nouveau_fence *fence = NULL; struct nouveau_svmm *svmm; struct page *dpage; vm_fault_t ret = 0; - int err; + int err, fret = 0; struct migrate_vma args = { .vma = vmf->vma, .pgmap_owner = drm->dev, @@ -293,10 +358,12 @@ static vm_fault_t nouveau_dmem_migrate_to_ram(struct vm_fault *vmf) * than just one page on CPU fault. When such fault happens it is very * likely that more surrounding page will CPU fault too. */ - if (migrate_vma_setup(&args) < 0) - return VM_FAULT_SIGBUS; + if (migrate_vma_setup(&args) < 0) { + ret = VM_FAULT_SIGBUS; + goto err; + } if (!args.cpages) - return 0; + goto done; if (order) dpage = folio_page(vma_alloc_folio(GFP_HIGHUSER | __GFP_ZERO, @@ -324,11 +391,35 @@ static vm_fault_t nouveau_dmem_migrate_to_ram(struct vm_fault *vmf) goto done; } - nouveau_dmem_fence_new(drm, &fence); + /* + * Commit the migration only once the copy is confirmed. Without a + * fence the copy can't be confirmed: treat it as failed. + */ + fret = nouveau_dmem_fence_new(drm, &fence); + /* + * A GPU access to this page while the copy runs faults, and the GPU + * fault worker waits for this migration before it can replay that + * fault. A copy was seen on hardware not to progress until the + * faulting channel was replayed, so this can hold up the copy. The + * deadline then aborts the migration instead of committing an + * unconfirmed copy. + */ + if (!fret) + fret = nouveau_dmem_fence_poll(&fence); + if (fret) { + nouveau_dmem_migrate_abort(drm, &args); + /* + * finalize restores the device private pte and the data is + * intact, but don't let the CPU fault again: with the copy + * engine wedged, every refault would queue another copy and + * leak another destination page. + */ + ret = fret == -ENOMEM ? VM_FAULT_OOM : VM_FAULT_SIGBUS; + } else { + dma_unmap_page(drm->dev->dev, dma_info.dma_addr, dma_info.size, + DMA_BIDIRECTIONAL); + } migrate_vma_pages(&args); - nouveau_dmem_fence_done(&fence); - dma_unmap_page(drm->dev->dev, dma_info.dma_addr, dma_info.size, - DMA_BIDIRECTIONAL); done: migrate_vma_finalize(&args); err: -- 2.55.0