From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=170.10.133.124 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796100995; b=oKbMg/rMTtcRbmBlPS26t9J0NfYIYd2YqNQhX5kqLpGZKfh349MRsVo9bYfop+sGmoVy Ust0yKhZUmAjvS761o+fZgvnSC2ENp3HVud2lmsunOjv3VHQ8MFN05DmHwODOgMhqdHLm EfBJrt5n+aSWMWDcPQo0Pwtm2l6/VXCv8CLv5iipaaMX/+nhV4qv4mtqf2908vchHIhrP I2LjeYMvr/Pj1C4i5ArGPfZXL0poURXI3Sbx0ZkJlL2p4zGoM9O7f5ZdtJNXK4vyqg5Yr uJjY3y2+4GD0ErGcrD2sSFJh1ETX+IXSfV5DRpouZXSoMiyaisUICZZ6opoRp3OA+OQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796100995; 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=0ebKXXhUSYDbaVOf7o+b7IN1o6tK35/PnJeR1VDU1AI=; b=Ie/eY/N0HNLRLjx9HhULYGTPztOkUsoAIYhYczSeQidGxokXdZ1kum6fTwUc4Rgthf+/ KktFSr/Kt7ZezEGoiED6BthtFYVMEBzBLwP+lwA3jHiRGrBZDrYWKaWydN5J27OKg+Nqf 3AvzTGLd2KvlImFuEiKFOvQKK2C3TAhtrIw5jgEviDIXp+2mwazEU8mP/z4I319kiKOuw cEjy5H1bw4Bb24h2NKzayXl/2vxQYJj07dpSCeSnRXKEqiCFdEGAIAqtu23cjpXptGYXs jm87usGPgKhoYvR48PXlmmyB3D6+Tj8Xgz51gHYKuDWpHbgpMtHVRPeM1/W3ih+JVNg== 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=Hj4XNcXN; dkim=pass header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=google header.b=FWse7JEt; arc=none smtp.remote-ip=170.10.133.124 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id B8C65606A32F for ; Tue, 1 Dec 2026 04:56:34 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org B8C65606A32F 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=Hj4XNcXN; dkim=pass (2048-bit key) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=google header.b=FWse7JEt DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1796100994; 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=Hj4XNcXNRus45sB/vszx495aSV+RSJ9AlsM5Oke8wqMFzE50Ct1JmJ8y0nrdPDyKi9cYOD YkdWN+G2NNTgfH7/ijwb/9FGnN2E3q0axqsySirFMwBlwDYbVqullTDuXPqMMiy0Mxsje7 CYcmvSoX+azfOC+I+pagZL62eC2keOk= 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-378-SyeaudP0NnS7fcg70GtKww-1; Tue, 1 Dec 2026 04:56:31 +0000 X-MC-Unique: SyeaudP0NnS7fcg70GtKww-1 X-Mimecast-MFC-AGG-ID: SyeaudP0NnS7fcg70GtKww_1796100990 Received: by mail-lf1-f69.google.com with SMTP id 2adb3069b0e04-5bfc3d1fadeso4357634e87.3 for ; Mon, 30 Nov 2026 20:56:31 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=google; t=1796100990; x=1796705790; 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=0ebKXXhUSYDbaVOf7o+b7IN1o6tK35/PnJeR1VDU1AI=; b=FWse7JEtGNNeIdAjmF5jQoxFEf4L1gEuEIJAUqnW0gtWZcjjwn1BJ3njymY4F1X1jg ++u7+PnBEWxaSOLXVlAAby5zaRtWJfNRnW/hAH9bOQrSf5p6A2IgCtFcNZVrIrvDCSw+ E9IT5LB+pWQTqQSSYKUAXurpBrJUPl9a2DRGOp3BWcMKBBCwMfi/D5K8Yy8/NWYgO0dH 9pkhihbCjW0XPjTBJdcD3/bOOeqmWE1bDQFlzZZMqBK+mTikdHisi4i70MpRgMOg7jAh RfzLIOKsarx8dw2hQFmeFd4UJiT0mpQjNIEGQWhOmKBpeL+0ZgxCZEqwl5lVfYaHiZw4 kNFQ== 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=U8bt8f7NCCX1CjbKJHS+a2Bv+hN9rud63COedKNVLO/kCipE+9URMvU9dbeNIDyZhF Hohlxr89e+kMl2oCk8eIzj1P6yhKceSjM8a/MEluGusfYquZfy0lUH6nETzKk6P1rFuh zjl1XdKcrGUE5U1WFb4CTCxk0yODIwFsk9YRRjnRzHC7gP/QOFnoSDW/fwk9ATHn1rRu TigNEPnsxMwzCv5wZyP/sJgbDfOh6KKlRbqW4TogKl0o/Gm0MZ0TlUsV0S0AP9b620sn 6kN7L+MpbNqOWpCrmnJMy52rl7NRAgwMqWaEvi5jI1McRQFTkdEa/IqdI8q625y05AMS VWoQ== X-Forwarded-Encrypted: i=1; AL+hErELVxN1M0oqLhkXawHK8kWhGiY2LgvEoCROA5XACHU8kFCRA53m2SNrs28jEKAinNmhcYuoR8rJx5W/usY=@vger.kernel.org X-Gm-Message-State: APuU4Z/nSywB5d1ING6Jc7hfy3nbsAq0snAcc/t3J2lO0w1L+6B482f3 yrGqQx/RwToQMSlgI+BvlWLJ1B52YEnA4u10VVK6zAEQjJeNzrNQ+2lTk5uGBMCkB6pzDTOlMRJ ZvePHNVtvzOnntuSl8I/ikIswAwdB7IzsUVX30j9HResvhE9uf1jsQNvRywOqvres X-Gm-Gg: AZFdTZf6orFNH5XcEwCaaXfjThCPXu6FKp9gBjw3wB0uAFnKIt4hK/9vv/eTx5hp1Wu NYDktuOArrC2JBwdSls6UZ1Lo20DvPUeNvaIMJafM2q+7tITiEs6kI52zAh/TgPg1DCBfcFtr3o vKNyGIGblhwWrJ1igfeqpi5Z4GaRIHVt8Je3DukOguhqXkHgPgIBn66G0Oruyh+2W8PLDEv7w9T y0W2M6TPdJQO6+DPlJ/WW634gYGf6yCcvTkaNZB1piacEnrw0SVKACslBK6vCq2Pd9ixyDOkhwY Rt5rMookhscJHip0JsBHCVDbA8PJWrmEzONeFiQzQEueqYa5EYHnr/mOymxBZX588ZkwmBDXDXY hx7CmYwGvNIiNW9jR00kJdc8LUKlkZEcs3Wt7N1QTIeCIkBaFG6hZUuG7o1hiS+U7ki9uEQ== X-Received: by 2002:a05:6512:239f:b0:5c1:5e33:6994 with SMTP id 2adb3069b0e04-5c1bc7ad610mr11134479e87.12.1796100990135; 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-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ä 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