From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from gabe.freedesktop.org (gabe.freedesktop.org [131.252.210.177]) (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 B4623606A31A for ; Tue, 1 Dec 2026 00:37:56 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=LwIrdAil Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id 54DB310E03C; Tue, 1 Dec 2026 00:37:51 +0000 (UTC) Received: from sea.source.kernel.org (sea.source.kernel.org [172.234.252.31]) by gabe.freedesktop.org (Postfix) with ESMTPS id F1BA110E03C for ; Tue, 1 Dec 2026 00:37:44 +0000 (UTC) Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) by sea.source.kernel.org (Postfix) with ESMTP id 9A74043708; Tue, 1 Dec 2026 00:37:44 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 687921F00893; Tue, 1 Dec 2026 00:37:44 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796085464; bh=3kh3HvQaMBYsxBfQoTFJzIpOVwKuyZCACTzFfmiABaE=; h=From:Subject:Reply-To:To:In-Reply-To:References:Date; b=LwIrdAilrHau9/HMY3ePNOdgQEhgnE7x2ev99J+Z5xUkT3aEEepCOqTxPruNYak7M O0RyLeTTaRSEAQVMLeWzqfCnXSbRj0zdswI9/uA8XaAVAJ8wzUI3+uLARLgyq7EDOA vKkUO0k4zENqI5H/ZffcE2d4ccKA/RNIUtD/m0hYGw/WS0GaHsN5TxA6t2nZ+f+FSJ KGU/MKP6iZZKVAIo9JlCxPpmfaRef2CLIjQ+3oMs3bUKotnu2OsFCL/8ikqEExFNh4 PBBTMGHaJoiMjtHMycbWnwlh91On92qYsdj2gbyfKbVjJcKaOd1gMNUh9X3OF8kRIH kx+09ObbHRw6g== Message-ID: From: sashiko-bot@kernel.org Subject: Re: [PATCH v3] drm/amdkfd: Check ownership of device pages for DMA mapping To: "Felix Kuehling" , amd-gfx@lists.freedesktop.org In-Reply-To: <20261201001418.26904-1-felix.kuehling@amd.com> References: <20261201001418.26904-1-felix.kuehling@amd.com> Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Date: Tue, 01 Dec 2026 00:37:43 +0000 X-BeenThere: amd-gfx@lists.freedesktop.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Discussion list for AMD gfx List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: sashiko-reviews@lists.linux.dev Sender: "amd-gfx" X-Spam-Status: No, score=-15.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_NONE, SPF_PASS autolearn=ham autolearn_force=no version=4.0.1 X-Spam-Checker-Version: SpamAssassin 4.0.1 (2024-03-26) on aws-us-west-2-korg-listserv-alma10-1 X-Archived-By: Thank you for your contribution! Sashiko AI review found 3 potential issues to consider: - [High] drm/amdkfd: same-hive device-private pages fail in svm_range_dma_map_dev() - [Medium] drm/amdkfd: dma_map_page() called on zone_device pages in svm_range_dma_map_dev() - [Low] drm/amdkfd: missing Fixes trailer for commit 1d5dbfe6c06a -- commit d01d79723c2d218e5005d3a6b9a8c0c534d177d0 Author: Felix Kuehling > drm/amdkfd: Check ownership of device pages for DMA mapping > > This issue was raised independently in two contexts: pages from DAX > devices and pages from other GPU hives or inherited from other > processed. This patch addresses both concerns together along with > Sashiko comments raised for one of the previous reviews: > https://sashiko.dev/#/patchset/20261103-kfd-svm-foreign-vram-20261103-v1-= 1-d6282053cad3@gmail.com?part=3D1 > > Before treating a page as a zone device page belonging to the SVM range, > check that it actually belongs to that range based on the > zone_device_data. Non-private pages belonging to other devices or ranges > can be treated like system memory for the purpose of DMA mapping. > > Cc: matthewkrebs8@gmail.com > Cc: jm5905938@gmail.com > Signed-off-by: Felix Kuehling [Severity: Low] This fixes the unconditional is_zone_device_page(page) ownership assumption in svm_range_dma_map_dev(). Should this include a Fixes tag for 1d5dbfe6c06a ("drm/amdkfd: classify and map mixed svm range pages in GPU")? > diff --git a/drivers/gpu/drm/amd/amdkfd/kfd_svm.c b/drivers/gpu/drm/amd/a= mdkfd/kfd_svm.c > index a571fda5c713a..ac32c77cd92cf 100644 > --- a/drivers/gpu/drm/amd/amdkfd/kfd_svm.c > +++ b/drivers/gpu/drm/amd/amdkfd/kfd_svm.c > @@ -164,6 +164,7 @@ svm_range_dma_map_dev(struct amdgpu_device *adev, str= uct svm_range *prange, > enum dma_data_direction dir =3D DMA_BIDIRECTIONAL; > dma_addr_t *addr =3D prange->dma_addr[gpuidx]; > struct device *dev =3D adev->dev; > + void *zone_device_data; > struct page *page; > int i, r; > =20 > @@ -174,13 +175,22 @@ svm_range_dma_map_dev(struct amdgpu_device *adev, s= truct svm_range *prange, > prange->dma_addr[gpuidx] =3D addr; > } > =20 > + mutex_lock(&prange->lock); > + zone_device_data =3D prange->ttm_res ? prange->svm_bo : NULL; > + mutex_unlock(&prange->lock); > + /* prange->svm_bo may change after this. If that happens, it implies > + * some kind of invalidation that would be detected by an HMM range > + * sequence number change before the page tables are updated > + */ > + > addr +=3D offset; > for (i =3D 0; i < npages; i++) { > if (svm_is_valid_dma_mapping_addr(dev, addr[i])) > dma_unmap_page(dev, addr[i], PAGE_SIZE, dir); > =20 > page =3D hmm_pfn_to_page(hmm_pfns[i]); > - if (is_zone_device_page(page)) { > + if (zone_device_data && is_zone_device_page(page) && > + page->zone_device_data =3D=3D zone_device_data) { > struct amdgpu_device *bo_adev =3D prange->svm_bo->node->adev; > =20 > addr[i] =3D (hmm_pfns[i] << PAGE_SHIFT) + > @@ -189,6 +199,16 @@ svm_range_dma_map_dev(struct amdgpu_device *adev, st= ruct svm_range *prange, > addr[i] |=3D SVM_RANGE_VRAM_DOMAIN; > pr_debug_ratelimited("vram address: 0x%llx\n", addr[i]); > continue; > + } else if (is_device_private_page(page)) { > + /* Device private memory from another driver or range. > + * This should never happen in practice but it's > + * theoretically possible after a process forks, > + * creates a new KFD context and, maps the same page > + * after the parent got a new COW page. > + */ > + pr_debug("Trying to map device private page from another device or ra= nge\n"); > + addr[i] =3D 0; > + return -EINVAL; > } [Severity: High] Can this return -EINVAL on device-private pages inherited across fork() or moved via mremap() on the same GPU or XGMI hive? svm_range_validate_and_map() computes dev_private_owner via kfd_svm_page_owner(), which returns the device-wide or hive-wide SVM_ADEV_PGMAP_OWNER(adev) even when prange->ttm_res is NULL or prange->svm_bo does not own the underlying pages. When pgmap->owner matches range->dev_private_owner, hmm_vma_handle_pte() skips hmm_pte_need_fault() a= nd returns the existing device-private PFN without faulting it back to system RAM, even on a writable COW VMA after fork(): mm/hmm.c:hmm_vma_handle_pte() { ... if (softleaf_is_device_private(entry) && page_pgmap(softleaf_to_page(entry))->owner =3D=3D range->dev_private_owner) { cpu_flags =3D HMM_PFN_VALID; if (softleaf_is_device_private_write(entry)) cpu_flags |=3D HMM_PFN_WRITE; new_pfn_flags =3D softleaf_to_pfn(entry) | cpu_flags; goto out; } ... } Because page->zone_device_data still points to the parent or source range's svm_bo, page->zone_device_data =3D=3D zone_device_data is false, so svm_range_dma_map_dev() fails with -EINVAL. When reached from svm_range_restore_work(), returning -EINVAL causes the worker to reschedule itself every 1ms indefinitely while user queues remain suspended. Passing NULL for dev_private_owner in svm_range_validate_and_map() when prange->ttm_res is NULL (or when foreign same-hive pages are detected) would let hmm_range_fault() migrate those pages back to system memory. > addr[i] =3D dma_map_page(dev, page, 0, PAGE_SIZE, dir); [Severity: Medium] Can falling through to dma_map_page() for non-private ZONE_DEVICE pages (su= ch as DAX pages or MEMORY_DEVICE_COHERENT pages from another range or hive) fa= il when CONFIG_DMA_API_DEBUG is enabled? dma_map_page() calls dma_map_page_attrs(), which rejects all ZONE_DEVICE pages under CONFIG_DMA_API_DEBUG: kernel/dma/mapping.c:dma_map_page_attrs() { ... if (IS_ENABLED(CONFIG_DMA_API_DEBUG) && WARN_ON_ONCE(is_zone_device_page(page))) return DMA_MAPPING_ERROR; ... } This triggers a WARN_ON_ONCE() and causes svm_range_dma_map_dev() to fail with -ENOMEM. Using dma_map_phys() (or pci_p2pdma_map_segment() as appropriate) avoids passing a ZONE_DEVICE struct page to dma_map_page(). --=20 Sashiko AI review =C2=B7 https://sashiko.dev/#/patchset/20261201001418.2690= 4-1-felix.kuehling@amd.com?part=3D1