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=1796100993; b=Ivf6g0efcpJERp4TQuNlAVtpjfQtlF7umssf9BGlKMzLb+pv3ZwNfplixMywZsY+N1cA dYmHIV7NKFBhR/Pj/u2R661Bdr0cdTIUBMoLn8kxQvI/xgwqebvZ2rm8VOyjUcUtpB+N8 w3KNXpH1r6/5vfwEPBNwUrih4M2O5iCL/u+LQ118y7YDo7JLU36/rKb4m0qgjfyaPYYDD CekaDgHFiqzrQ2M2gviGvvdowcz7ZeRD1fSzOJXBCU8ohOPL4EMeyVTCCsZEp0Eo/muCZ Ar4lodM7Bs1Yw36d2IuFymU6Hg2D4YI4j52z2COlOUFfjO6b9WKinvVCCKPGZrggxcg== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796100993; 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=NJ2/Cu5GCvUZgPHrvRiDUgRSF2HPXptkSiqEP7Q/uN8=; b=wZos2OgG7/wMxzNvi8werbQcP8qsBLYFnH+X8JWTRvYru2bkYgvCjuNknVqLxb+uiWPU JcDWbaBUDFVkEd7ElQGLd/J1EIjQjQ8Nde22pLasKRLSwzwxv8igJrKSNGeTxyk1YXTb2 IIVB4naFAeltrO3rAMQfqDMMOfnT6E4LAe/nVzCY/pci+aH428gpbxnCkYfLwY204/Q4E GwUMoyze/mNurIXQ1jKpOKRo/UlG9Sv1xQwqLTs2eepDyIvqy/kMke+JZjrgNB9a5Hg7o GOrXZTxEkgXqS3yJCJjGKprt422wXno91HdfGS1MVT4IMGvq8qE179UdUdMWNyLB5qw== 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=IhXR0xCb; dkim=pass header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=google header.b=YxsxHlfg; 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 03915606A32C for ; Tue, 1 Dec 2026 04:56:32 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 03915606A32C 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=IhXR0xCb; dkim=pass (2048-bit key) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=google header.b=YxsxHlfg DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1796100993; 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=NJ2/Cu5GCvUZgPHrvRiDUgRSF2HPXptkSiqEP7Q/uN8=; b=IhXR0xCbA8ypGFkElW9ltDjheoHX3B1b5aiYpbjRTAuaTk33mPtJADEBiOBuTrbkrLkLQz qFVtuoSQNFCINGJtcAfYyNbvhguxOKOYR7xzGBh7CEV0MszhyoufRJeYPWgKywzGJMWXM2 IBjhOcHjO0IrAn1VR2IgWrKOSR/m+vg= 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-543-o4sMZgazMYG8lImPg1cRfg-1; Tue, 1 Dec 2026 04:56:28 +0000 X-MC-Unique: o4sMZgazMYG8lImPg1cRfg-1 X-Mimecast-MFC-AGG-ID: o4sMZgazMYG8lImPg1cRfg_1796100986 Received: by mail-lf1-f70.google.com with SMTP id 2adb3069b0e04-5c157b6cb9fso734428e87.0 for ; Mon, 30 Nov 2026 20:56:27 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=google; t=1796100986; x=1796705786; 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=NJ2/Cu5GCvUZgPHrvRiDUgRSF2HPXptkSiqEP7Q/uN8=; b=YxsxHlfgUmxcYfVuT8GGxuggqqNPFvIUAwiRrXN5gTKo5qK18o94cQOAH04SMFEO57 nks0a/EbzOE9isanKZaQQ7Qc2M1y3q/2LEbMHPUMkYoxa0ukA5q0fec2RYL1IAZ/7ACf C5wsXEY5PBxMh1BJPZ38a2uWTuY2ZAh3KrUtqR04QryR4pFCpF34pkKzJlu1kkiuXT0c TnTsO6YQx/UczxqtXR0xorc/k0ir1rWWA5T8wCbp5c/Ssnov5fELIULTaxmAjzLwPSEm 4JkfnkbdbtS1qmk2FdycC+C97Nijv8rIcVT65i/cs0AW4yM4OxsyugwEL21NAUYgXsUq jZHQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796100986; x=1796705786; 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=NJ2/Cu5GCvUZgPHrvRiDUgRSF2HPXptkSiqEP7Q/uN8=; b=qIM6tbNpYVxdfvYhzRsROlTFvO3YgOgDLO8S3pmgcsCHayF7Csaa5SjftXbWWwT1NG tFkKIiWE8V1U07ASshbdNZeq5drbF1Ixp7IRS/SapcKAfRRs8A7afMoI+QejJ9jvBlQ0 JBjAswabChh93YUAbQnNPLl5i7UOmERjQg0i6c9ltVeYo28POVdtW8igRLPMfGYcOHvu XE5IpwISOJbFXXWpFU9kccwpo9yzeKN9QzEj+wjF+WVGyiHuPGPXk7J1vme1OFAlEL6n kug5KOXduOGX+fzVpMGCdRi6DCReQ7Y4LSIN7q0Thukpbs+NEfH+qAm6NLRToEIgRv5U HX+A== X-Forwarded-Encrypted: i=1; AL+hErExkmeJIsrHgbL7tMQciOXKoFQTQusPmGlCaYrAvxhdHOu95+x1FQ+nyPOSElfvCdgPDb66EjkI3yoBlAA=@vger.kernel.org X-Gm-Message-State: APuU4Z9dt4kcl9y5sCumkTiCsh+fWLrW507mC4Dh/Og6Nv1QoD/kbdCG g4h2n2vuWbsGRFMMXir03WV5piMYTOFReK1Oj88QA3ZwEXNQcGKDrPIDwcBqcyVSHwIoPRdorDr cbAHQ9ORjYN0bZZC1+dZ1PYfX0ahJTZaxKEHW5S6zUU3X33/1N1p5HE0R06TcTuUf X-Gm-Gg: AZFdTZcp2BfpuUzjgaB5CxCvZxWLxRtaQiazg3BauDhkGC5bYaPQYVq/H7ac4az2/4q z7H+6Losw0R8FjJWwrXx4Bm6HrXGHo1nSByd/pejWLWiySHg0YCleUBQDJieBXJ4QmsE1ndMI/P fs6/lkpxHUs9TMke6vx/X/cwCXDRlRB9f60V8MFT77R7XFhXngfGizfhiiqsyJfmGSKt2onHlnA YjWSA7Hadj5nKacgYP/om2fPmssfitFkYRMe3D1MJ06tRviMAxBhLHnTQcFu5ldZKJy2iIl0cCh yP5InOGLbP6JXST1JVczG/6lM3IHcqiv20YBptV4t1pHHnFYoKm5mSuRuYkOMWfOtUxmHKfjnz3 hNsCnh7MALS4J93WVoIaaI0Y/eh+AF7QK6wXYBuhtRgJNQFx6PFmoR8tgRUaZqRGV/o4v/w== X-Received: by 2002:a05:6512:401b:b0:5c3:384d:e2c4 with SMTP id 2adb3069b0e04-5c3384de3cfmr4734351e87.61.1796100986374; Mon, 30 Nov 2026 20:56:26 -0800 (PST) X-Received: by 2002:a05:6512:401b:b0:5c3:384d:e2c4 with SMTP id 2adb3069b0e04-5c3384de3cfmr4734349e87.61.1796100985869; Mon, 30 Nov 2026 20:56:25 -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.25 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 30 Nov 2026 20:56:25 -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 12/14] drm/nouveau/dmem: serialize the copy channel users Date: Tue, 1 Dec 2026 06:55:29 +0200 Message-ID: <20261201045531.3622535-13-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ä The dmem users of the migration copy channel (CPU faults in migrate_to_ram(), bulk migration and chunk eviction) write copy and clear commands to its push buffer and emit fences without any serialization, so concurrent users can interleave their pushes. Add drm->dmem->migrate.lock and the wrappers nouveau_dmem_copy(), nouveau_dmem_clear() and nouveau_dmem_fence_new(), which take it only around the pushes and the fence emit. It is never held across memory allocation: migrate_to_ram() copies under svmm->mutex, and reclaim can take svmm->mutex through the mmu notifier, so the fence is allocated before taking the lock. The channel is shared with TTM, which serializes its own use with cli->mutex. dmem and TTM stay unserialized against each other, as before: dmem can't take cli->mutex, since TTM allocates memory under it. Fixes: 5be73b690875 ("drm/nouveau/dmem: device memory helpers for SVM") Signed-off-by: Mika Penttilä --- drivers/gpu/drm/nouveau/nouveau_dmem.c | 81 ++++++++++++++++++++++---- 1 file changed, 71 insertions(+), 10 deletions(-) diff --git a/drivers/gpu/drm/nouveau/nouveau_dmem.c b/drivers/gpu/drm/nouveau/nouveau_dmem.c index eacac8055834..6824f5401d00 100644 --- a/drivers/gpu/drm/nouveau/nouveau_dmem.c +++ b/drivers/gpu/drm/nouveau/nouveau_dmem.c @@ -76,6 +76,22 @@ struct nouveau_dmem_migrate { nouveau_migrate_copy_t copy_func; nouveau_clear_page_t clear_func; struct nouveau_channel *chan; + /* + * Serializes the dmem users of chan (CPU faults in migrate_to_ram(), + * 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). + * + * chan is shared with TTM (drm->ttm.chan), which serializes its own + * use with cli->mutex instead. dmem and TTM are therefore not + * serialized against each other, as before this lock was added. + * dmem can't take cli->mutex: dmem copies run under svmm->mutex, + * while TTM may allocate memory under cli->mutex, and an allocation + * can enter reclaim, whose mmu notifier invalidation takes + * svmm->mutex. + */ + struct mutex lock; }; struct nouveau_dmem { @@ -93,6 +109,51 @@ struct nouveau_dmem_dma_info { size_t size; }; +static int nouveau_dmem_copy(struct nouveau_drm *drm, u64 npages, + enum nouveau_aper dst_aper, u64 dst_addr, + enum nouveau_aper src_aper, u64 src_addr) +{ + int ret; + + mutex_lock(&drm->dmem->migrate.lock); + ret = drm->dmem->migrate.copy_func(drm, npages, dst_aper, dst_addr, + src_aper, src_addr); + mutex_unlock(&drm->dmem->migrate.lock); + return ret; +} + +static int nouveau_dmem_clear(struct nouveau_drm *drm, u32 length, + enum nouveau_aper dst_aper, u64 dst_addr) +{ + int ret; + + mutex_lock(&drm->dmem->migrate.lock); + ret = drm->dmem->migrate.clear_func(drm, length, dst_aper, dst_addr); + mutex_unlock(&drm->dmem->migrate.lock); + return ret; +} + +static int nouveau_dmem_fence_new(struct nouveau_drm *drm, + struct nouveau_fence **fence) +{ + int ret; + + /* + * nouveau_fence_new() without the allocation under migrate.lock: + * the fence is allocated first, the lock only covers the emit. + */ + ret = nouveau_fence_create(fence, drm->dmem->migrate.chan); + if (ret) + return ret; + + mutex_lock(&drm->dmem->migrate.lock); + ret = nouveau_fence_emit(*fence); + mutex_unlock(&drm->dmem->migrate.lock); + if (ret) + nouveau_fence_unref(fence); + return ret; +} + static struct nouveau_dmem_chunk *nouveau_page_to_chunk(struct page *page) { return container_of(page_pgmap(page), struct nouveau_dmem_chunk, @@ -168,10 +229,10 @@ static int nouveau_dmem_copy_folio(struct nouveau_drm *drm, if (dma_mapping_error(dev, dma_info->dma_addr)) return -EIO; - if (drm->dmem->migrate.copy_func(drm, folio_nr_pages(sfolio), - NOUVEAU_APER_HOST, dma_info->dma_addr, - NOUVEAU_APER_VRAM, - nouveau_dmem_page_addr(spage))) { + if (nouveau_dmem_copy(drm, folio_nr_pages(sfolio), + NOUVEAU_APER_HOST, dma_info->dma_addr, + NOUVEAU_APER_VRAM, + nouveau_dmem_page_addr(spage))) { dma_unmap_page(dev, dma_info->dma_addr, page_size(dpage), DMA_BIDIRECTIONAL); return -EIO; @@ -183,7 +244,6 @@ 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_dmem *dmem = drm->dmem; struct nouveau_fence *fence; struct nouveau_svmm *svmm; struct page *dpage; @@ -264,7 +324,7 @@ static vm_fault_t nouveau_dmem_migrate_to_ram(struct vm_fault *vmf) goto done; } - nouveau_fence_new(&fence, dmem->migrate.chan); + nouveau_dmem_fence_new(drm, &fence); migrate_vma_pages(&args); nouveau_dmem_fence_done(&fence); dma_unmap_page(drm->dev->dev, dma_info.dma_addr, dma_info.size, @@ -528,7 +588,7 @@ nouveau_dmem_evict_chunk(struct nouveau_dmem_chunk *chunk) } } - nouveau_fence_new(&fence, chunk->drm->dmem->migrate.chan); + nouveau_dmem_fence_new(chunk->drm, &fence); migrate_device_pages(src_pfns, dst_pfns, npages); nouveau_dmem_fence_done(&fence); migrate_device_finalize(src_pfns, dst_pfns, npages); @@ -729,6 +789,7 @@ nouveau_dmem_init(struct nouveau_drm *drm) INIT_LIST_HEAD(&drm->dmem->chunks); mutex_init(&drm->dmem->mutex); spin_lock_init(&drm->dmem->lock); + mutex_init(&drm->dmem->migrate.lock); /* Initialize migration dma helpers before registering memory */ ret = nouveau_dmem_migrate_init(drm); @@ -764,13 +825,13 @@ static unsigned long nouveau_dmem_migrate_copy_one(struct nouveau_drm *drm, dma_info->size = page_size(spage); if (dma_mapping_error(dev, dma_info->dma_addr)) goto out_free_page; - if (drm->dmem->migrate.copy_func(drm, folio_nr_pages(page_folio(spage)), + if (nouveau_dmem_copy(drm, folio_nr_pages(page_folio(spage)), NOUVEAU_APER_VRAM, paddr, NOUVEAU_APER_HOST, dma_info->dma_addr)) goto out_dma_unmap; } else { dma_info->dma_addr = DMA_MAPPING_ERROR; - if (drm->dmem->migrate.clear_func(drm, page_size(dpage), + if (nouveau_dmem_clear(drm, page_size(dpage), NOUVEAU_APER_VRAM, paddr)) goto out_free_page; } @@ -820,7 +881,7 @@ static void nouveau_dmem_migrate_chunk(struct nouveau_drm *drm, addr += (1 << order) * PAGE_SIZE; } - nouveau_fence_new(&fence, drm->dmem->migrate.chan); + nouveau_dmem_fence_new(drm, &fence); migrate_vma_pages(args); nouveau_dmem_fence_done(&fence); nouveau_pfns_map(svmm, args->vma->vm_mm, args->start, pfns, i, order); -- 2.55.0