From patchwork Fri Mar 21 08:00:27 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Huang, Honglei1" X-Patchwork-Id: 14024954 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from gabe.freedesktop.org (gabe.freedesktop.org [131.252.210.177]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id B738EC35FF3 for ; Fri, 21 Mar 2025 08:01:26 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id 21ACA10E664; Fri, 21 Mar 2025 08:01:26 +0000 (UTC) Authentication-Results: gabe.freedesktop.org; dkim=pass (1024-bit key; unprotected) header.d=amd.com header.i=@amd.com header.b="tXs77HH/"; dkim-atps=neutral Received: from NAM11-BN8-obe.outbound.protection.outlook.com (mail-bn8nam11on2080.outbound.protection.outlook.com [40.107.236.80]) by gabe.freedesktop.org (Postfix) with ESMTPS id 0762110E6FA for ; Fri, 21 Mar 2025 08:01:25 +0000 (UTC) ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=ywgVXxrVh9L2UDbkqLBLUYby4Heia6GGxkFYumGB7UzzFZghUgmcxc1sV7cPvoGWTOGsrsf3tQ9mDRJwYicQCcO83zKRzgNPD36vGM4KI6GT2ZTxrlMk3ETWZDr4Xv46JgT3rYXDq+g3jNdBO3DHBUSIjXyLGxu2Lhn+Kfw5wfuCG/trZlBZ30A6wGIAyzd9a8N2BS4cadyinFmmzgbcPbi06swGPWCjkBoB8eooZoWQbjcACB7SBfg+rcst+lj/PRZoCmS31THyiZJpEvMx2bW0YbYPT5UGYeWn4wjfZu/FRb1p55TAPTE7YcE+XvujkCzG13DIxVinZ9OJLuqQEg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector10001; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=86uHILBfB9VPjbFbb7fwYrDkXHq4q3fZex7aErIP4Y4=; b=G+X2K0TcBulTLhg9biUSre93gq1PCr8JzGKWsyONcmWlNDL4Nrz/zlif787YHlfbZLZD7UBUkM/sVd7aij+Ll/eD9uhLq4rdkYbZvcj+VtCiw6CrLUoTZOvzi8jlSx6Ti7ERchLtYQ43d5q0cDpQbzBMMc4f09mlhQIpLU6vodYHCqPfDSUm7IfbcaRuFxBDXY5IrIMr67atSkUMNyiFIwINmjj6N9VjIBbBnESICMuRvw2mh0904h1+1L+mhLcTXqr4A/tevzNbuJ3Ig1WfXqyWEUS3l+ZAmZh1kcw4Qmitz4alHxJz4R4Vm1CSCGvY5Tfw7VLbuV9dgdrAb55Ajg== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 165.204.84.17) smtp.rcpttodomain=redhat.com smtp.mailfrom=amd.com; dmarc=pass (p=quarantine sp=quarantine pct=100) action=none header.from=amd.com; dkim=none (message not signed); arc=none (0) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amd.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=86uHILBfB9VPjbFbb7fwYrDkXHq4q3fZex7aErIP4Y4=; b=tXs77HH/R+ZcX7T0uDIohLCX7W0WY5ypAF+A3renWtfxp2Eb/7/vqX0xIrHZiZ+rCYVbV8sNTVp67UnfxMy+WDu3yv/OgHfG07Ijad04btFLKt2JYhGwbvEG7iQIGwnMLyaAQ/+Z43qJtTP5FLe85Xyq914OHk2DRKQvS2Fu6ro= Received: from DS7PR05CA0046.namprd05.prod.outlook.com (2603:10b6:8:2f::14) by PH8PR12MB6723.namprd12.prod.outlook.com (2603:10b6:510:1ce::12) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.8534.33; Fri, 21 Mar 2025 08:01:17 +0000 Received: from DS3PEPF000099E1.namprd04.prod.outlook.com (2603:10b6:8:2f:cafe::dc) by DS7PR05CA0046.outlook.office365.com (2603:10b6:8:2f::14) with Microsoft SMTP Server (version=TLS1_3, cipher=TLS_AES_256_GCM_SHA384) id 15.20.8534.35 via Frontend Transport; Fri, 21 Mar 2025 08:01:17 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 165.204.84.17) smtp.mailfrom=amd.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=amd.com; Received-SPF: Pass (protection.outlook.com: domain of amd.com designates 165.204.84.17 as permitted sender) receiver=protection.outlook.com; client-ip=165.204.84.17; helo=SATLEXMB04.amd.com; pr=C Received: from SATLEXMB04.amd.com (165.204.84.17) by DS3PEPF000099E1.mail.protection.outlook.com (10.167.17.196) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.8534.20 via Frontend Transport; Fri, 21 Mar 2025 08:01:17 +0000 Received: from jenkins-honglei.amd.com (10.180.168.240) by SATLEXMB04.amd.com (10.181.40.145) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.39; Fri, 21 Mar 2025 03:01:13 -0500 From: Honglei Huang To: David Airlie , Gerd Hoffmann , Gurchetan Singh , Chia-I Wu , Maarten Lankhorst , Maxime Ripard , Thomas Zimmermann , Simona Vetter , Rob Clark , Huang Rui CC: , , , Demi Marie Obenour , Dmitry Osipenko , Honglei Huang Subject: [PATCH v2 5/7] drm/virtio: implement userptr: add userptr obj Date: Fri, 21 Mar 2025 16:00:27 +0800 Message-ID: <20250321080029.1715078-6-honglei1.huang@amd.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20250321080029.1715078-1-honglei1.huang@amd.com> References: <20250321080029.1715078-1-honglei1.huang@amd.com> MIME-Version: 1.0 X-Originating-IP: [10.180.168.240] X-ClientProxiedBy: SATLEXMB03.amd.com (10.181.40.144) To SATLEXMB04.amd.com (10.181.40.145) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: DS3PEPF000099E1:EE_|PH8PR12MB6723:EE_ X-MS-Office365-Filtering-Correlation-Id: 4d62fc6e-9057-484f-98c4-08dd684e8f43 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; ARA:13230040|376014|36860700013|82310400026|1800799024|7416014|921020; X-Microsoft-Antispam-Message-Info: HEdyhGr+1xXRa9L60GmZkwfVvEK8CtbmaWitjoP9x6QyF2pCi7lq4pQbVPLBb/hbrstlxAkMAxik02nIzmklk9f1nGt5P90MPAtY67EIkGuM6KQFDYsNFaJIIiKyIC7YzllT+1hTiWpzI8P7suFiep8BmastBiMefMWDb1VKi5E9EzMfXkpDgvKBF7bW1nPDGB88cCkx1zQL6IuM661Mds9yEkUmWGeET6VsWqG9CHjccSCr31SVXd75Qu5Xh5e4V5YYP5WHNyNY0AEsEJTT3fHvfDlChqKbzR1bsY6w80kpthta0fMAxPLOgUmgmiW7Wgi5T8/daxva8SWZ+ez5sL50uQAslxFVTkaM+A5L/e+Y1aRuRz+IikQtoYv60ynBSeSvI0TDxyYnhk/egOy0A7xiDOKokujsn95poHsEg3IRd1X/PuIZ1c0nk3gZh9pyZfHF2XxA5dVCq+CZX0/Ya2E330iIhJTdFKlUU7dUd6lZk492MvjA2Le/zSOyuiZIRIq8H9HN3eaYWBhHmOljAT92WsA7tYvg97IHZ74sO3bfSz/uJNepeCPbV/J85Lue5SDUuFRTnLitCu2B+I8VSJmK6QCM9uNlS8MSH8rDBUIMfmLkZ+LTDzLSC5YS9E7jh1dgp0U2MA4vI7uQIHhdDEryoaE+IRNEk/ieBnuRb2z7JmN1TPGiBKxjCIhGo7COufuhW8xryUXfkuMBNw1w4x62iY6WqFcQZlCgkvprJCHPFkPpXD7Lp3WPNjtyGI+FShQAqEanUEt3Er98eUVJYqs6gfj8sjzylfVKXvRb6dmZ9cUUdY99T7vgXSG+nK8ZmPi3VVTq3Q8siF2a7mlu/4YK9xpCILjkwwKUMBugxRUvIpZDBfOLZhAEAE9SNNG7DkY9FYx48XGQyis9U2CA+K8HYMUIAbgKTQcI9+1SBFnoG61a4wLhkqZFBVgel9OgNDGTuEPUyO4mXlBYSZq7+q546Fa7eKQpxs5J7ciEl0MHCW20tCCEEN2ogF2KCnJ1Mwhvydgu5IV5PoItW9mA9K52NLOG1oBWjPBHR6eF704kWDX3FEFZmTMxqtw92jWZxKADHuNtkyyVYwDSBqbccWc+eI8/Oz6WRfTnGe5tP8UM0cx/BoDjk0ZiPbc8Qe0MBF18YKqyOXAypOZ0Nv7fy0tCn/SVLRszu/6RQr/OsvZTB8WsjM4m4voRVy3qn3AihHgGepuwd6bxgTbcayxrgn3rWZWgrg76ZqXXJ9Y8W1UhEUf3tn6+E0LWStzK3hk6cQgJIb5/blNMlD+h73xMtySUdNFOCgtBKPlwcCF40MmMSuftTADNUvfGdKUxTet2hkNLnx3EnOOHZ92sGUvrAOv7uHK17KBSz0tVPU9IBUytdB9ffTPg+Fv2RFaP5FRZDQOCERDYtSluTZeQHNSy6OfnLLPuvGsExo7RLd2wNxImHB1vGOSOes8ut1mJOJV3zWrnfMUeRUadBJfletCDdsieTiEF4XrMTi/SsUPC+q6fS6N2ZQAzAevGT4XS7KXI X-Forefront-Antispam-Report: CIP:165.204.84.17; CTRY:US; LANG:en; SCL:1; SRV:; IPV:CAL; SFV:NSPM; H:SATLEXMB04.amd.com; PTR:InfoDomainNonexistent; CAT:NONE; SFS:(13230040)(376014)(36860700013)(82310400026)(1800799024)(7416014)(921020); DIR:OUT; SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 21 Mar 2025 08:01:17.6006 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 4d62fc6e-9057-484f-98c4-08dd684e8f43 X-MS-Exchange-CrossTenant-Id: 3dd8961f-e488-4e60-8e11-a82d994e183d X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=3dd8961f-e488-4e60-8e11-a82d994e183d; Ip=[165.204.84.17]; Helo=[SATLEXMB04.amd.com] X-MS-Exchange-CrossTenant-AuthSource: DS3PEPF000099E1.namprd04.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: PH8PR12MB6723 X-BeenThere: dri-devel@lists.freedesktop.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Direct Rendering Infrastructure - Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: dri-devel-bounces@lists.freedesktop.org Sender: "dri-devel" From: Honglei Huang Add implement for virtio gpu userptr. Current solution is pinning all the user space memory. The UMD needs manage all the userptrs. Signed-off-by: Honglei Huang --- drivers/gpu/drm/virtio/Makefile | 3 +- drivers/gpu/drm/virtio/virtgpu_drv.h | 33 ++++ drivers/gpu/drm/virtio/virtgpu_object.c | 5 + drivers/gpu/drm/virtio/virtgpu_userptr.c | 230 +++++++++++++++++++++++ 4 files changed, 270 insertions(+), 1 deletion(-) create mode 100644 drivers/gpu/drm/virtio/virtgpu_userptr.c diff --git a/drivers/gpu/drm/virtio/Makefile b/drivers/gpu/drm/virtio/Makefile index d2e1788a8227..fe7332a621aa 100644 --- a/drivers/gpu/drm/virtio/Makefile +++ b/drivers/gpu/drm/virtio/Makefile @@ -6,6 +6,7 @@ virtio-gpu-y := virtgpu_drv.o virtgpu_kms.o virtgpu_gem.o virtgpu_vram.o \ virtgpu_display.o virtgpu_vq.o \ virtgpu_fence.o virtgpu_object.o virtgpu_debugfs.o virtgpu_plane.o \ - virtgpu_ioctl.o virtgpu_prime.o virtgpu_trace_points.o virtgpu_submit.o + virtgpu_ioctl.o virtgpu_prime.o virtgpu_trace_points.o virtgpu_submit.o \ + virtgpu_userptr.o obj-$(CONFIG_DRM_VIRTIO_GPU) += virtio-gpu.o diff --git a/drivers/gpu/drm/virtio/virtgpu_drv.h b/drivers/gpu/drm/virtio/virtgpu_drv.h index 7bdcbaa20ef1..f3dcbd241f5a 100644 --- a/drivers/gpu/drm/virtio/virtgpu_drv.h +++ b/drivers/gpu/drm/virtio/virtgpu_drv.h @@ -85,6 +85,7 @@ struct virtio_gpu_object_params { uint32_t blob_mem; uint32_t blob_flags; uint64_t blob_id; + uint64_t userptr; }; struct virtio_gpu_object { @@ -112,12 +113,38 @@ struct virtio_gpu_object_vram { struct drm_mm_node vram_node; }; +struct virtio_gpu_object_userptr; + +struct virtio_gpu_object_userptr_ops { + int (*get_pages)(struct virtio_gpu_object_userptr *userptr); + void (*put_pages)(struct virtio_gpu_object_userptr *userptr); + void (*release)(struct virtio_gpu_object_userptr *userptr); +}; +struct virtio_gpu_object_userptr { + struct virtio_gpu_object base; + const struct virtio_gpu_object_userptr_ops *ops; + struct mutex lock; + + uint64_t start; + uint32_t npages; + uint32_t bo_handle; + uint32_t flags; + + struct virtio_gpu_device *vgdev; + struct drm_file *file; + struct page **pages; + struct sg_table *sgt; +}; + #define to_virtio_gpu_shmem(virtio_gpu_object) \ container_of((virtio_gpu_object), struct virtio_gpu_object_shmem, base) #define to_virtio_gpu_vram(virtio_gpu_object) \ container_of((virtio_gpu_object), struct virtio_gpu_object_vram, base) +#define to_virtio_gpu_userptr(virtio_gpu_object) \ + container_of((virtio_gpu_object), struct virtio_gpu_object_userptr, base) + struct virtio_gpu_object_array { struct ww_acquire_ctx ticket; struct list_head next; @@ -489,4 +516,10 @@ void virtio_gpu_vram_unmap_dma_buf(struct device *dev, int virtio_gpu_execbuffer_ioctl(struct drm_device *dev, void *data, struct drm_file *file); +/* virtgpu_userptr.c */ +int virtio_gpu_userptr_create(struct virtio_gpu_device *vgdev, + struct drm_file *file, + struct virtio_gpu_object_params *params, + struct virtio_gpu_object **bo_ptr); +bool virtio_gpu_is_userptr(struct virtio_gpu_object *bo); #endif diff --git a/drivers/gpu/drm/virtio/virtgpu_object.c b/drivers/gpu/drm/virtio/virtgpu_object.c index c7e74cf13022..31659b0a028d 100644 --- a/drivers/gpu/drm/virtio/virtgpu_object.c +++ b/drivers/gpu/drm/virtio/virtgpu_object.c @@ -80,6 +80,11 @@ void virtio_gpu_cleanup_object(struct virtio_gpu_object *bo) drm_gem_free_mmap_offset(&vram->base.base.base); drm_gem_object_release(&vram->base.base.base); kfree(vram); + } else if (virtio_gpu_is_userptr(bo)) { + struct virtio_gpu_object_userptr *userptr = to_virtio_gpu_userptr(bo); + + drm_gem_object_release(&userptr->base.base.base); + kfree(userptr); } } diff --git a/drivers/gpu/drm/virtio/virtgpu_userptr.c b/drivers/gpu/drm/virtio/virtgpu_userptr.c new file mode 100644 index 000000000000..b4a08811d345 --- /dev/null +++ b/drivers/gpu/drm/virtio/virtgpu_userptr.c @@ -0,0 +1,230 @@ +// SPDX-License-Identifier: GPL-2.0 +#include +#include +#include +#include + +#include "virtgpu_drv.h" +#include "drm/drm_gem.h" + +static struct sg_table * +virtio_gpu_userptr_get_sg_table(struct drm_gem_object *obj); + +static void virtio_gpu_userptr_free(struct drm_gem_object *obj) +{ + struct virtio_gpu_object *bo = gem_to_virtio_gpu_obj(obj); + struct virtio_gpu_device *vgdev = obj->dev->dev_private; + struct virtio_gpu_object_userptr *userptr = to_virtio_gpu_userptr(bo); + + if (bo->created) { + userptr->ops->release(userptr); + + virtio_gpu_cmd_unref_resource(vgdev, bo); + virtio_gpu_notify(vgdev); + } +} + +static void virtio_gpu_userptr_object_close(struct drm_gem_object *obj, + struct drm_file *file) +{ + virtio_gpu_gem_object_close(obj, file); +} + +static const struct drm_gem_object_funcs virtio_gpu_userptr_funcs = { + .open = virtio_gpu_gem_object_open, + .close = virtio_gpu_userptr_object_close, + .free = virtio_gpu_userptr_free, + .get_sg_table = virtio_gpu_userptr_get_sg_table, +}; + +bool virtio_gpu_is_userptr(struct virtio_gpu_object *bo) +{ + return bo->base.base.funcs == &virtio_gpu_userptr_funcs; +} + +static int +virtio_gpu_userptr_get_pages(struct virtio_gpu_object_userptr *userptr) +{ + unsigned int flag = FOLL_LONGTERM; + unsigned int num_pages, pinned = 0; + int ret = 0; + + if (userptr->pages) + return 0; + + userptr->pages = kvmalloc_array(userptr->npages, sizeof(struct page *), + GFP_KERNEL); + if (!userptr->pages) + return -ENOMEM; + + if (!(userptr->flags & VIRTGPU_BLOB_FLAG_USERPTR_RDONLY)) + flag |= FOLL_WRITE; + + do { + num_pages = userptr->npages - pinned; + + ret = pin_user_pages_fast(userptr->start + pinned * PAGE_SIZE, + num_pages, flag, + userptr->pages + pinned); + + if (ret < 0) { + if (pinned) + unpin_user_pages(userptr->pages, pinned); + kvfree(userptr->pages); + return ret; + } + + pinned += ret; + + } while (pinned < userptr->npages); + + return 0; +} + +static void +virtio_gpu_userptr_put_pages(struct virtio_gpu_object_userptr *userptr) +{ + if (userptr->pages) { + unpin_user_pages(userptr->pages, userptr->npages); + kvfree(userptr->pages); + userptr->pages = NULL; + } + + if (userptr->sgt) { + sg_free_table(userptr->sgt); + kfree(userptr->sgt); + userptr->sgt = NULL; + } +} + +static void +virtio_gpu_userptr_release(struct virtio_gpu_object_userptr *userptr) +{ + mutex_lock(&userptr->lock); + userptr->ops->put_pages(userptr); + mutex_unlock(&userptr->lock); +} + +static struct sg_table * +virtio_gpu_userptr_get_sg_table(struct drm_gem_object *obj) +{ + struct virtio_gpu_object *bo = gem_to_virtio_gpu_obj(obj); + struct virtio_gpu_object_userptr *userptr = to_virtio_gpu_userptr(bo); + + mutex_lock(&userptr->lock); + if (!userptr->pages) { + if (userptr->ops->get_pages(userptr)) { + mutex_unlock(&userptr->lock); + return ERR_PTR(-ENOMEM); + } + } + + if (!userptr->sgt) + userptr->sgt = drm_prime_pages_to_sg(NULL, userptr->pages, + userptr->npages); + mutex_unlock(&userptr->lock); + + return userptr->sgt; +} + +static int +virtio_gpu_userptr_init(struct drm_device *dev, struct drm_file *file, + struct virtio_gpu_object_userptr *userptr, + struct virtio_gpu_object_params *params, + const struct virtio_gpu_object_userptr_ops *ops) +{ + uint32_t page_offset; + uint64_t aligned_size; + uint64_t aligned_addr; + int ret; + struct drm_gem_object *obj; + + page_offset = params->userptr & (PAGE_SIZE - 1UL); + aligned_addr = params->userptr - page_offset; + aligned_size = roundup(page_offset + params->size, PAGE_SIZE); + + userptr->start = aligned_addr; + userptr->npages = aligned_size >> PAGE_SHIFT; + userptr->flags = params->blob_flags; + + mutex_init(&userptr->lock); + userptr->vgdev = dev->dev_private; + userptr->file = file; + userptr->ops = ops; + + obj = &userptr->base.base.base; + obj->funcs = &virtio_gpu_userptr_funcs; + + drm_gem_private_object_init(dev, obj, aligned_size); + + ret = virtio_gpu_resource_id_get(userptr->vgdev, + &userptr->base.hw_res_handle); + + return ret; +} + +static const struct virtio_gpu_object_userptr_ops virtio_gpu_userptr_ops = { + .get_pages = virtio_gpu_userptr_get_pages, + .put_pages = virtio_gpu_userptr_put_pages, + .release = virtio_gpu_userptr_release, +}; + +int virtio_gpu_userptr_create(struct virtio_gpu_device *vgdev, + struct drm_file *file, + struct virtio_gpu_object_params *params, + struct virtio_gpu_object **bo_ptr) +{ + struct virtio_gpu_object_userptr *userptr; + int ret, si; + struct sg_table *sgt; + struct scatterlist *sg; + struct virtio_gpu_mem_entry *ents; + + if (!params->size) + return -EINVAL; + + if (!access_ok((char __user *)(unsigned long)params->userptr, + params->size)) + return -EFAULT; + + userptr = kzalloc(sizeof(*userptr), GFP_KERNEL); + if (!userptr) + return -ENOMEM; + + ret = virtio_gpu_userptr_init(vgdev->ddev, file, userptr, params, + &virtio_gpu_userptr_ops); + + if (ret) + goto failed_free; + + sgt = virtio_gpu_userptr_get_sg_table(&userptr->base.base.base); + + if (IS_ERR(sgt)) { + ret = PTR_ERR(sgt); + goto failed_free; + } + + ents = kvmalloc_array(sgt->nents, sizeof(struct virtio_gpu_mem_entry), + GFP_KERNEL); + + if (!ents) { + ret = -ENOMEM; + goto failed_free; + } + + for_each_sgtable_sg(sgt, sg, si) { + (ents)[si].addr = cpu_to_le64(sg_phys(sg)); + (ents)[si].length = cpu_to_le32(sg->length); + (ents)[si].padding = 0; + } + + virtio_gpu_cmd_resource_create_blob(vgdev, &userptr->base, params, ents, + sgt->nents); + + *bo_ptr = &userptr->base; + return 0; + +failed_free: + kfree(userptr); + return ret; +}