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 44B4F606A31E for ; Wed, 25 Nov 2026 13:49:40 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key, unprotected) header.d=intel.com header.i=@intel.com header.a=rsa-sha256 header.s=Intel header.b=H6MQMsvO Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id 9AE0410FC69; Wed, 25 Nov 2026 13:49:34 +0000 (UTC) Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.14]) by gabe.freedesktop.org (Postfix) with ESMTPS id 45C5210FC91 for ; Wed, 25 Nov 2026 13:49:28 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1795614569; x=1827150569; h=message-id:date:mime-version:subject:to:cc:references: from:in-reply-to:content-transfer-encoding; bh=ez+954RV2Jp+dA01ixbjxaoRUMeG7KvpLfAxeZrJtgk=; b=H6MQMsvOI6B0SPWr47X8kbhoLu/05Qr84w+mNqEpIPPrh7HilbczFGFK sF++4x5b9MEEVlfarCo8J7oXNQ09sN+oo/GGTPW0rL8Ge9kJOoBtzLIk1 7O56jqK1bkeF5TbMrCmYjnLuy8OQEKfy+9nYA6BtS1HxoYwKEJR7BRTsi KKuaXThl7yaArS8TarYGxpFaKzc8f+dJXCqAjuiIqBIfD49Xr5TwW+b3x jmurp4BapqFjOsrXo6I2GuO8naJZxR1lVnyySKgYChvpLOFtTRg/+muOT WSoVBwAuFpupCVcTYJuSrvOK6dUvpavvvKbgYo0XVdRzyg7Fm3ox2Fd07 A==; X-CSE-ConnectionGUID: q/0b6BePRQ2U9S9qifoVDA== X-CSE-MsgGUID: tkbBc+umTM64ahDM3Vmqsw== X-IronPort-AV: E=McAfee;i="6800,10657,11977"; a="4398036" X-IronPort-AV: E=Sophos;i="6.27,233,1787036400"; d="scan'208";a="4398036" Received: from orviesa005.jf.intel.com ([10.64.159.145]) by orvoesa106.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 25 Nov 2026 05:49:28 -0800 X-CSE-ConnectionGUID: HidqXBCnRFO+7NsbLRqSjg== X-CSE-MsgGUID: jV1mu358QbqY550dWtIUVw== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.27,233,1787036400"; d="scan'208";a="11769782" Received: from unknown (HELO [10.94.255.201]) ([10.94.255.201]) by smtpauth.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 25 Nov 2026 05:49:26 -0800 Message-ID: Date: Wed, 25 Nov 2026 14:49:22 +0100 X-Archived-By: MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH v2] accel/ivpu: Allocate a scatterlist per userptr dma-buf mapping To: =?UTF-8?Q?Christian_K=C3=B6nig?= , dri-devel@lists.freedesktop.org Cc: apopple@nvidia.com, Andrzej Kacprowski , Oded Gabbay , Jeff Hugo , dawid.osuchowski@linux.intel.com, sumit.semwal@linaro.org References: <20261123102644.627963-1-karol.wachowski@linux.intel.com> Content-Language: en-US From: Karol Wachowski In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit X-BeenThere: dri-devel@lists.freedesktop.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Direct Rendering Infrastructure - Development List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "dri-devel" X-Spam-Status: No, score=-15.3 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE, SPF_PASS,USER_AGENT_SANE_1 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 On 25-Nov-26 14:42, Christian König wrote: > On 11/23/26 11:26, Karol Wachowski wrote: >> The userptr dma-buf DMA maps the same sg_table on every map_dma_buf(), >> so a second mapping would overwrite the DMA addresses of the first one. >> It only works today because ivpu is the only importer. >> >> Build a new sg_table from the pinned pages on each map instead. >> >> Fixes: 57557964b582 ("accel/ivpu: Add support for userptr buffer objects") >> Signed-off-by: Karol Wachowski > > Stuffing an userptr into an DMA-buf is broken bejong repair in the first place and we just removed that from amdxdna as well, see patch "accel/amdxdna: Drop dma-buf wrapping for ubuf". > > The only thing which saves you here is that you don't expose the resulting DMA-buf anywhere else, but I strongly suggest to remove the DMA-buf wrapping here as well. > > Regards, > Christian. Hi Christian, Agree, I plan to rework userptr BOs to drop the dma-buf layer, likely along the lines of what amdxdna did. I can't give a timeline yet. Our resources are limited right now and I want to get the design right, but it's on my list. Regards, Karol > >> --- >> >> Notes: >> v2: >> - Use the correct upstream sha in the Fixes: tag (Sashiko). >> - v1: https://lore.kernel.org/r/20261123085935.623564-1-karol.wachowski@linux.intel.com >> >> Alistair, your DMA-buf mapping types series [1] noted this as a >> pre-existing issue in "[PATCH v3 15/24] accel/ivpu: Convert the userptr >> exporter to the SGT mapping type" [2]. With this patch applied first, >> ivpu becomes an exporter that allocates a table per map, so its >> conversion reduces to the same pattern as "[PATCH v3 07/24] dma-buf: >> Convert exporters allocating a table per map to the SGT mapping type" >> (e.g. amdxdna_ubuf), without the shared header copy. >> >> [1] https://lore.kernel.org/r/20261120-dma_mapping_type_v4-v3-0-c0183a409bdf@nvidia.com >> [2] https://lore.kernel.org/r/20261120-dma_mapping_type_v4-v3-15-c0183a409bdf@nvidia.com >> >> drivers/accel/ivpu/ivpu_gem_userptr.c | 65 ++++++++++++++++----------- >> 1 file changed, 38 insertions(+), 27 deletions(-) >> >> diff --git a/drivers/accel/ivpu/ivpu_gem_userptr.c b/drivers/accel/ivpu/ivpu_gem_userptr.c >> index 7cbf3a4cdc73..cfb2894b572d 100644 >> --- a/drivers/accel/ivpu/ivpu_gem_userptr.c >> +++ b/drivers/accel/ivpu/ivpu_gem_userptr.c >> @@ -19,18 +19,39 @@ >> #include "ivpu_drv.h" >> #include "ivpu_gem.h" >> >> +struct ivpu_userptr { >> + struct page **pages; >> + unsigned long nr_pages; >> +}; >> + >> static struct sg_table * >> ivpu_gem_userptr_dmabuf_map(struct dma_buf_attachment *attachment, >> enum dma_data_direction direction) >> { >> - struct sg_table *sgt = attachment->dmabuf->priv; >> + struct ivpu_userptr *userptr = attachment->dmabuf->priv; >> + struct sg_table *sgt; >> int ret; >> >> + sgt = kmalloc_obj(*sgt); >> + if (!sgt) >> + return ERR_PTR(-ENOMEM); >> + >> + ret = sg_alloc_table_from_pages(sgt, userptr->pages, userptr->nr_pages, 0, >> + attachment->dmabuf->size, GFP_KERNEL); >> + if (ret) >> + goto free_sgt; >> + >> ret = dma_map_sgtable(attachment->dev, sgt, direction, DMA_ATTR_SKIP_CPU_SYNC); >> if (ret) >> - return ERR_PTR(ret); >> + goto free_table; >> >> return sgt; >> + >> +free_table: >> + sg_free_table(sgt); >> +free_sgt: >> + kfree(sgt); >> + return ERR_PTR(ret); >> } >> >> static void ivpu_gem_userptr_dmabuf_unmap(struct dma_buf_attachment *attachment, >> @@ -38,21 +59,17 @@ static void ivpu_gem_userptr_dmabuf_unmap(struct dma_buf_attachment *attachment, >> enum dma_data_direction direction) >> { >> dma_unmap_sgtable(attachment->dev, sgt, direction, DMA_ATTR_SKIP_CPU_SYNC); >> + sg_free_table(sgt); >> + kfree(sgt); >> } >> >> static void ivpu_gem_userptr_dmabuf_release(struct dma_buf *dma_buf) >> { >> - struct sg_table *sgt = dma_buf->priv; >> - struct sg_page_iter page_iter; >> - struct page *page; >> - >> - for_each_sgtable_page(sgt, &page_iter, 0) { >> - page = sg_page_iter_page(&page_iter); >> - unpin_user_page(page); >> - } >> + struct ivpu_userptr *userptr = dma_buf->priv; >> >> - sg_free_table(sgt); >> - kfree(sgt); >> + unpin_user_pages(userptr->pages, userptr->nr_pages); >> + kvfree(userptr->pages); >> + kfree(userptr); >> } >> >> static const struct dma_buf_ops ivpu_gem_userptr_dmabuf_ops = { >> @@ -67,7 +84,7 @@ ivpu_create_userptr_dmabuf(struct ivpu_device *vdev, void __user *user_ptr, >> { >> struct dma_buf_export_info exp_info = {}; >> struct dma_buf *dma_buf; >> - struct sg_table *sgt; >> + struct ivpu_userptr *userptr; >> struct page **pages; >> unsigned long nr_pages = size >> PAGE_SHIFT; >> unsigned int gup_flags = FOLL_LONGTERM; >> @@ -94,39 +111,33 @@ ivpu_create_userptr_dmabuf(struct ivpu_device *vdev, void __user *user_ptr, >> goto unpin_pages; >> } >> >> - sgt = kmalloc_obj(*sgt); >> - if (!sgt) { >> + userptr = kmalloc_obj(*userptr); >> + if (!userptr) { >> ret = -ENOMEM; >> goto unpin_pages; >> } >> >> - ret = sg_alloc_table_from_pages(sgt, pages, nr_pages, 0, size, GFP_KERNEL); >> - if (ret) { >> - ivpu_dbg(vdev, IOCTL, "Failed to create sg table: %d\n", ret); >> - goto free_sgt; >> - } >> + userptr->pages = pages; >> + userptr->nr_pages = nr_pages; >> >> exp_info.exp_name = "ivpu_userptr_dmabuf"; >> exp_info.owner = THIS_MODULE; >> exp_info.ops = &ivpu_gem_userptr_dmabuf_ops; >> exp_info.size = size; >> exp_info.flags = O_RDWR | O_CLOEXEC; >> - exp_info.priv = sgt; >> + exp_info.priv = userptr; >> >> dma_buf = dma_buf_export(&exp_info); >> if (IS_ERR(dma_buf)) { >> ret = PTR_ERR(dma_buf); >> ivpu_dbg(vdev, IOCTL, "Failed to export userptr dma-buf: %d\n", ret); >> - goto free_sg_table; >> + goto free_userptr; >> } >> >> - kvfree(pages); >> return dma_buf; >> >> -free_sg_table: >> - sg_free_table(sgt); >> -free_sgt: >> - kfree(sgt); >> +free_userptr: >> + kfree(userptr); >> unpin_pages: >> for (i = 0; i < pinned; i++) >> unpin_user_page(pages[i]); >> >> base-commit: 5ffdc8f8361387a1b11553e5c95cd589c67a51f2 >