Received: from lists1p.gnu.org (lists1p.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-ECDSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 9C7B4606A322 for ; Sat, 5 Dec 2026 11:50:20 +0000 (UTC) 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=CwU1foGT; dkim=pass (2048-bit key, unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=google header.b=G5mUn9jQ Received: from localhost ([::1] helo=lists1p.gnu.org) by lists1p.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1xZoGt-0007JA-KR; Sat, 05 Dec 2026 06:49:43 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists1p.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1xZoGr-0007Iq-8R for qemu-devel@nongnu.org; Sat, 05 Dec 2026 06:49:41 -0500 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1xZoGo-0004uC-QU for qemu-devel@nongnu.org; Sat, 05 Dec 2026 06:49:40 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1796471378; 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:autocrypt:autocrypt; bh=HwyiUYLL6UIz1X7G4AACLBRUtUqiOrr9DpS+/4WBGPU=; b=CwU1foGTIJBaUMUFlzwCRGicdNn8rzTKV9nR7ImUJdwLyYdwuyPpkBgJNbHyGGIT2pZUEb XZNVo6NWOnMr3XIUunZbDtA1WsB/rtrqnVHui8RJ3XguHB2mw11gAEmqc29VhI2Bwlls9l TbaIsQtbqemAZU7XOoZ7itRT2f/1c44= Received: from mail-wr1-f69.google.com (mail-wr1-f69.google.com [209.85.221.69]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-608-MtRjEjWFOEq3L9EE8sNZDA-1; Sat, 5 Dec 2026 11:49:35 +0000 X-MC-Unique: MtRjEjWFOEq3L9EE8sNZDA-1 X-Mimecast-MFC-AGG-ID: MtRjEjWFOEq3L9EE8sNZDA_1796471374 Received: by mail-wr1-f69.google.com with SMTP id ffacd0b85a97d-4903fc8537aso1185686f8f.0 for ; Sat, 05 Dec 2026 03:49:34 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=google; t=1796471374; x=1797076174; darn=nongnu.org; h=content-transfer-encoding:content-type:in-reply-to:autocrypt :content-language:from:references:cc:to:subject:user-agent :mime-version:date:message-id:from:to:cc:subject:date:message-id :reply-to:content-type; bh=HwyiUYLL6UIz1X7G4AACLBRUtUqiOrr9DpS+/4WBGPU=; b=G5mUn9jQUD9jVRDBe1FW+aRm8tnqFb7Cxd00FPcPJWLI3vXRErJ8hB7bI+arShrMGA 3NO4KeCNW0wOZSvPK/tvvf8nTtFHSdY5MQ2vZWFhxKnTPU4s4lCLEcSC1FCpx3Wy8Gvk DSPNeTEQufeSCDZ+rgfYWiJYrpsEQdc5g9TYoz2OzaVxbO9oeBPff9Ac/QHBb3qxbSvy roJC99fhLnUGC50JXn2h32iEbNUqmHWbtoRucjDKBDW8I/2ugrkVCfwKaaTZmtfdioul JWnVhisD8Kv1yKJq+Stz3fFlUoTUtDwJC24HpUgdEnTGBsEOPTT++R+Uz2zu56D+szUD OYAQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796471374; x=1797076174; h=content-transfer-encoding:content-type:in-reply-to:autocrypt :content-language:from:references:cc:to:subject:user-agent :mime-version:date:message-id:x-gm-gg:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to:content-type; bh=HwyiUYLL6UIz1X7G4AACLBRUtUqiOrr9DpS+/4WBGPU=; b=jd3vQHov0Gu0GiOXz4fvSBN6s8rTxn13D+GlKCrMkPwgfdisVcC6CJSI/nhdbhHrBj axMsnwMuvJcmJFB3Qxh9r4yG4xwu1yMIjOITxigTwowZIx6BEIAZ50zLnlC7RqXJ0mrC 2GpMN56lARzz+SxJeOpeblWQPfadteImRICQc19ey4XxGkqSEYV45se44AEg1dI3UxYG 5N0iwMZaEFnXgIhYTQbRNo/V8CS2GnxFBNn5Kqq7AexA7FKPbEq+VGAzVvRt3EqOM1Nq 2m5/AhX3EwutOVPwrU+WWgvPh3of/OKTJKRL9lsQysetlRT8jf5eFns8pM0ovk2PfdSg Qrmg== X-Gm-Message-State: AB4q+uGSkm7kEk6ukmCOJEyW+tmI5aNIVt+rCiIxiruvsj9UtMKqjx4B H1DFH5RLcCSL9y6n2LMIQGM5E4JMWsNNERzjWICpsANNPwK/dY3n3QmU8ICKVUKJA0FgJcb9Je+ 2X/ehxKZCPC1azQOAo0KaIlAjkkTxSyr9Etffksp9beZ8wYLRdrdft7mI X-Gm-Gg: AZFdTZeRVaYoHuNOifeBoCoyx7w5ee4ukc7hj2+qighZKQri1ZMuxm5zoWh8VY5f1av cYgefZzCDgBApBlr33JuNYMCO4rMuPWUfbe6GNR2YVwY8BAJSsLFJKkvIc/LM1ogjTSlHaSo8h8 /3lT4Wi4uHuHSm3vFjhCD3Qjao48kM1nQvDUC1k4HdjPXGaS4kN5bX0MCajiH4H1cJLOA0TFKqY iPtMySybxMfQlBRLQV/ZlDHGkK6z5rjX1269Dxd/DjF38HU5V8qtd6dJBNG/ezpNyOK0o+0HTaU ElZW6p/1w889iSW25exVNvZYdvhpI6TkyBWMGpriQrBTXZdt58hTP0ygf/rHpN7su/chXQ/8+HN t1MOdLd2Afvb9RIH0ZKlyc1xbFh54VhpE7KUOf+y+nNjg3/mET61SKKfeDsoHdRm0s1XBjVWmYv DKvYBc57lrawiP X-Received: by 2002:a05:6000:2286:b0:4a6:4862:28f5 with SMTP id ffacd0b85a97d-4d1e2827e68mr8664688f8f.3.1796471373652; Sat, 05 Dec 2026 03:49:33 -0800 (PST) X-Received: by 2002:a05:6000:2286:b0:4a6:4862:28f5 with SMTP id ffacd0b85a97d-4d1e2827e68mr8664656f8f.3.1796471373134; Sat, 05 Dec 2026 03:49:33 -0800 (PST) Received: from ?IPV6:2a01:e0a:1292:d530:adab:2c08:97de:c619? ([2a01:e0a:1292:d530:adab:2c08:97de:c619]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-4d174fe6fc7sm12070654f8f.57.2026.12.05.03.49.31 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Sat, 05 Dec 2026 03:49:31 -0800 (PST) Message-ID: <50142ba2-3f04-439a-8af5-712751222b9e@redhat.com> Date: Sat, 5 Dec 2026 12:49:30 +0100 X-Archived-By: MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH v13 6/9] vfio/device: Add support for creating dmabuf from multiple ranges To: "Kasireddy, Vivek" , Akihiko Odaki , Markus Armbruster Cc: "qemu-devel@nongnu.org" , Alex Williamson References: <20261007201222.648687-1-vivek.kasireddy@intel.com> <20261007201222.648687-7-vivek.kasireddy@intel.com> <306e36da-31eb-4e54-8b47-afb5ae55a6c9@redhat.com> <98667ef2-8931-497a-bb76-04097a2aa43b@rsg.ci.i.u-tokyo.ac.jp> <87h5ioepb9.fsf@pond.sub.org> <3f38296f-012f-40af-807d-5979e4759015@rsg.ci.i.u-tokyo.ac.jp> <8fabff75-f7d7-40a0-bd8d-acef46b76530@redhat.com> From: =?UTF-8?Q?C=C3=A9dric_Le_Goater?= Content-Language: en-US, fr Autocrypt: addr=clg@redhat.com; keydata= xsFNBFu8o3UBEADP+oJVJaWm5v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EU2sIDQCHuv1QgzmKwhE58d475KdVX/3Vt5I9kTXpvEpfW18TjlFkdHGESM/Jx IqVsqvhAJkalABEBAAHCwV8EGAECAAkFAlu8o3UCGwwACgkQUaNDx8/77KEhwg//WqVopd5k 8hQb9VVdk6RQOCTfo6wHhEqgjbXQGlaxKHoXywEQBi8eULbeMQf5l4+tHJWBxswQ93IHBQjK yKyNr4FXseUI5O20XVNYDJZUrhA4yn0e/Af0IX25d94HXQ5sMTWr1qlSK6Zu79lbH3R57w9j hQm9emQEp785ui3A5U2Lqp6nWYWXz0eUZ0Tad2zC71Gg9VazU9MXyWn749s0nXbVLcLS0yop s302Gf3ZmtgfXTX/W+M25hiVRRKCH88yr6it+OMJBUndQVAA/fE9hYom6t/zqA248j0QAV/p LHH3hSirE1mv+7jpQnhMvatrwUpeXrOiEw1nHzWCqOJUZ4SY+HmGFW0YirWV2mYKoaGO2YBU wYF7O9TI3GEEgRMBIRT98fHa0NPwtlTktVISl73LpgVscdW8yg9Gc82oe8FzU1uHjU8b10lU XOMHpqDDEV9//r4ZhkKZ9C4O+YZcTFu+mvAY3GlqivBNkmYsHYSlFsbxc37E1HpTEaSWsGfA HQoPn9qrDJgsgcbBVc1gkUT6hnxShKPp4PlsZVMNjvPAnr5TEBgHkk54HQRhhwcYv1T2QumQ izDiU6iOrUzBThaMhZO3i927SG2DwWDVzZltKrCMD1aMPvb3NU8FOYRhNmIFR3fcalYr+9gD uVKe8BVz4atMOoktmt0GWTOC8P4= In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit Received-SPF: pass client-ip=170.10.133.124; envelope-from=clg@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -28 X-Spam_score: -2.9 X-Spam_bar: -- X-Spam_report: (-2.9 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.142, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_LOW=-0.7, RCVD_IN_MSPIKE_H3=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_PASS=-0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: qemu development List-Archive: List-Post: List-Help: List-Subscribe: , Sender: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org X-Spam-Status: No, score=-17.6 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_PATCH,MAILING_LIST_MULTI,MENTIONS_GIT_HOSTING,SPF_HELO_PASS, 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 12/5/26 00:06, Kasireddy, Vivek wrote: > Hi Akihiko, > >>> Thank you for the guidance. Rephrasing your points: >>> >>> - Negative errno is often convenient, but an alternative set of error >>>   codes are used when it's a bad fit. >>> >>> - KISS: encode specific error conditions to be distinguished instead >> encoding all. >>> >>> This brings us to a common architectural intersection: How should a >> function handle and propagate errors when its callees return >> overlapping -errno values that require different treatment by the caller? > Not sure whether you got a chance to look into the latest response from > Cedric (on 10/14) but I am still wondering how to break through this gridlock. > Please let us know your thoughts and a provide a path forward for fixing > this issue. One extra issue, adding complexity, is deciding whether a buffer can be exported through the VFIO backend and creating the dmabuf. These are two distinct questions. I introduced a new helper for it : vfio_device_iov_resolve(). vfio_device_create_dmabuf_fd() and vfio_device_mmap_dmabuf() can now take a VFIODevice, which is consistent with the interface. Reworked routine virtio_gpu_init_dmabuf() with it and added some aborts. Please look at this tree and its last commit : https://github.com/legoater/qemu/tree/virtio-gpu-dmabuf The VFIO code is below. Thanks, C. diff --git a/include/hw/vfio/vfio-device.h b/include/hw/vfio/vfio-device.h index 8472420d3fac..4a13585e36b6 100644 --- a/include/hw/vfio/vfio-device.h +++ b/include/hw/vfio/vfio-device.h @@ -323,8 +323,58 @@ bool vfio_device_has_region_cap(VFIODevice *vbasedev, int region, uint16_t cap_t int vfio_device_get_irq_info(VFIODevice *vbasedev, int index, struct vfio_irq_info *info); + #endif +/** + * Check if the addresses stored in the iovec array are mappings + * of regions of a VFIO device. + * + * @iov: array of iovec entries associated with a buffer + * @iov_cnt: number of entries in the iovec array + * @errp: pointer to Error*, to store an error if it happens. + * + * Returns the VFIODevice owning the buffer if so, NULL + * otherwise. + */ +VFIODevice *vfio_device_iov_resolve(struct iovec *iov, + unsigned int iov_cnt, Error **errp); + +/** + * Create a dmabuf fd for a buffer checked with + * vfio_device_iov_resolve(), translating each iovec entry into a + * VFIO region offset for the VFIO_DEVICE_FEATURE_DMA_BUF + * feature. Aborts QEMU if the buffer was not checked first. + * + * @vbasedev: #VFIODevice associated with the buffer + * @iov: array of iovec entries associated with a buffer + * @iov_cnt: number of entries in the iovec array + * @errp: pointer to Error*, to store an error if it happens. + * + * Returns the dmabuf fd (>= 0) on success, or a negative errno: + * - -ENOTSUP: VFIO not compiled in + * - other negative errno from the kernel VFIO ioctl + */ +int vfio_device_create_dmabuf_fd(VFIODevice *vbasedev, struct iovec *iov, + unsigned int iov_cnt, Error **errp); + +/** + * Map the dmabuf of a buffer checked with + * vfio_device_iov_resolve() into a single contiguous mapping, + * one segment per iovec entry, at consecutive offsets. + * + * @vbasedev: #VFIODevice associated with the buffer + * @iov: array of iovec entries associated with the dmabuf + * @iov_cnt: number of entries in the iovec array + * @errp: pointer to Error*, to store an error if it happens. + * + * Returns the mapped pointer on success and NULL on error. The + * caller must munmap() it with the total size of the buffer. + */ +void *vfio_device_mmap_dmabuf(VFIODevice *vbasedev, struct iovec *iov, + unsigned int iov_cnt, Error **errp); + + /* Returns 0 on success, or a negative errno. */ bool vfio_device_get_name(VFIODevice *vbasedev, Error **errp); void vfio_device_free_name(VFIODevice *vbasedev); diff --git a/hw/vfio/device.c b/hw/vfio/device.c index 4f119596336f..60914bdcadbe 100644 --- a/hw/vfio/device.c +++ b/hw/vfio/device.c @@ -667,3 +669,189 @@ static VFIODeviceIOOps vfio_device_io_ops_ioctl = { .region_read = vfio_device_io_region_read, .region_write = vfio_device_io_region_write, }; + +/* + * This helper looks up the VFIODevice corresponding to the given iov. It + * can be useful to determine if a buffer (represented by the iov) belongs + * to a VFIO device or not. This is mainly invoked when external components + * such as virtio-gpu request creation of dmabuf fds for buffers that may + * belong to a VFIO device. + */ +static bool vfio_device_lookup(struct iovec *iov, VFIODevice **vbasedevp, + RAMBlock **first_rbp, Error **errp) +{ + RAMBlock *first_rb; + VFIORegion *region; + ram_addr_t offset; + + first_rb = qemu_ram_block_from_host(iov[0].iov_base, false, &offset); + if (!first_rb) { + error_setg(errp, "Could not find first ramblock"); + return false; + } + + region = vfio_get_region_from_mr(first_rb->mr); + if (!region) { + error_setg(errp, "Memory does not belong to a VFIO device"); + return false; + } + + *first_rbp = first_rb; + *vbasedevp = region->vbasedev; + return true; +} + +/* + * This helper looks up the VFIORegion corresponding to the given address. + * It also verifies that the RAMBlock associated with the address is the + * same as the first_rb passed in. This is to ensure that all addresses + * in the iov belong to the same region. + */ +static bool vfio_region_lookup(void *addr, VFIODevice *vbasedev, + VFIORegion **regionp, RAMBlock *first_rb, + ram_addr_t *offsetp, Error **errp) +{ + VFIORegion *region; + RAMBlock *rb; + + rb = qemu_ram_block_from_host(addr, false, offsetp); + if (!rb || rb != first_rb) { + error_setg(errp, "Dmabuf segments must belong to the same region"); + return false; + } + + region = vfio_get_region_from_mr(rb->mr); + if (region && region->vbasedev == vbasedev) { + *regionp = region; + return true; + } + error_setg(errp, "Could not find valid region for dmabuf segment"); + return false; +} + +VFIODevice *vfio_device_iov_resolve(struct iovec *iov, unsigned int iov_cnt, + Error **errp) +{ + VFIODevice *vbasedev; + RAMBlock *first_rb; + int i; + + if (!vfio_device_lookup(iov, &vbasedev, &first_rb, errp)) { + return NULL; + } + + for (i = 0; i < iov_cnt; i++) { + VFIORegion *region; + ram_addr_t offset; + + if (!vfio_region_lookup(iov[i].iov_base, vbasedev, ®ion, + first_rb, &offset, errp)) { + return NULL; + } + } + + return vbasedev; +} + +int vfio_device_create_dmabuf_fd(VFIODevice *vbasedev, struct iovec *iov, + unsigned int iov_cnt, Error **errp) +{ + g_autofree struct vfio_device_feature *feature = NULL; + struct vfio_device_feature_dma_buf *dma_buf; + RAMBlock *first_rb; + VFIORegion *region; + ram_addr_t offset; + size_t argsz; + int i, ret; + + /* + * The buffer is expected to have been resolved with by the caller + * vfio_device_iov_resolve(). The lookups below cannot fail, so + * use &error_abort. + */ + first_rb = qemu_ram_block_from_host(iov[0].iov_base, false, &offset); + g_assert(first_rb); + + argsz = sizeof(*feature) + sizeof (*dma_buf) + + sizeof(struct vfio_region_dma_range) * iov_cnt; + feature = g_malloc0(argsz); + *feature = (struct vfio_device_feature) { + .argsz = argsz, + .flags = VFIO_DEVICE_FEATURE_GET | VFIO_DEVICE_FEATURE_DMA_BUF, + }; + dma_buf = (struct vfio_device_feature_dma_buf *)feature->data; + + for (i = 0; i < iov_cnt; i++) { + vfio_region_lookup(iov[i].iov_base, vbasedev, ®ion, + first_rb, &offset, &error_abort); + + dma_buf->region_index = region->nr; + dma_buf->dma_ranges[i].offset = offset; + dma_buf->dma_ranges[i].length = iov[i].iov_len; + } + + dma_buf->nr_ranges = iov_cnt; + dma_buf->open_flags = O_RDONLY | O_CLOEXEC; + + ret = vfio_device_get_feature(vbasedev, feature); + if (ret < 0) { + error_setg_errno(errp, -ret, + "Could not create dmabuf fd via VFIO device"); + return ret; + } + return ret; +} + +void *vfio_device_mmap_dmabuf(VFIODevice *vbasedev, struct iovec *iov, + unsigned int iov_cnt, Error **errp) +{ + size_t total_size = iov_size(iov, iov_cnt); + struct vfio_region_info *info = NULL; + ram_addr_t offset, len = 0; + VFIORegion *region; + RAMBlock *first_rb; + void *map, *submap; + int i; + + /* + * The buffer was resolved by vfio_device_iov_resolve(), see + * vfio_device_create_dmabuf_fd(). + */ + first_rb = qemu_ram_block_from_host(iov[0].iov_base, false, &offset); + g_assert(first_rb); + + /* + * We first reserve a contiguous chunk of address space for the entire + * dmabuf, then replace it with smaller mappings that correspond to the + * individual segments of the dmabuf. + */ + map = mmap(NULL, total_size, PROT_NONE, MAP_SHARED | MAP_ANONYMOUS, -1, 0); + if (map == MAP_FAILED) { + error_setg_errno(errp, errno, "Could not reserve placeholder mapping"); + return NULL; + } + + for (i = 0; i < iov_cnt; i++) { + vfio_region_lookup(iov[i].iov_base, vbasedev, ®ion, + first_rb, &offset, &error_abort); + + if (vfio_device_get_region_info(vbasedev, region->nr, &info)) { + error_setg(errp, "Cannot find region info for dmabuf segment"); + goto err; + } + + submap = mmap(map + len, iov[i].iov_len, PROT_READ, + MAP_SHARED | MAP_FIXED, vbasedev->fd, + info->offset + offset); + if (submap == MAP_FAILED) { + error_setg_errno(errp, errno, "Could not mmap dmabuf segment"); + goto err; + } + len += iov[i].iov_len; + } + return map; +err: + munmap(map, total_size); + error_prepend(errp, "VFIO dmabuf mmap failed: "); + return NULL; +}