From: "Cédric Le Goater" <clg@redhat.com>
To: Akihiko Odaki <odaki@rsg.ci.i.u-tokyo.ac.jp>,
Markus Armbruster <armbru@redhat.com>
Cc: "Kasireddy, Vivek" <vivek.kasireddy@intel.com>,
"qemu-devel@nongnu.org" <qemu-devel@nongnu.org>,
Alex Williamson <alex@shazbot.org>
Subject: Re: [PATCH v13 6/9] vfio/device: Add support for creating dmabuf from multiple ranges
Date: Wed, 14 Oct 2026 18:29:36 +0200 [thread overview]
Message-ID: <8fabff75-f7d7-40a0-bd8d-acef46b76530@redhat.com> (raw)
In-Reply-To: <3f38296f-012f-40af-807d-5979e4759015@rsg.ci.i.u-tokyo.ac.jp>
On 10/14/26 15:07, Akihiko Odaki wrote:
>>
>
> 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?
>
> Both virtio_gpu_create_udmabuf() and vfio_device_create_dmabuf_fd() face this challenge. Evaluating the available design options against your advice yields the following:
>
> 1. Use custom error code enum.
>
> Defining an enum is not as convenient as negative errno, but it is KISS; it encodes specific error conditions we care about:
>
> enum {
> VFIO_DMABUF_CREATE_ERR_INVALID_IOV = -1,
> VFIO_DMABUF_CREATE_ERR_UNSPEC = -2,
> };
>
> static int vfio_device_create_dmabuf_fd(..., Error **errp)
> {
> ...
>
> if (!vfio_device_lookup(iov, &vbasedev, &first_rb, errp)) {
> return VFIO_DMABUF_CREATE_ERR_INVALID_IOV;
> }
>
> ...
>
> ret = vfio_device_get_feature(vbasedev, feature);
> if (ret < 0) {
> error_setg_errno(errp, -ret,
> "Could not create dmabuf fd via VFIO device");
> return VFIO_DMABUF_CREATE_ERR_UNSPEC;
> }
> return ret;
> }
Works, it's heavy.
This function has a single caller, and the enum values break the
-errno convention that the rest of the fallback chain uses
(virtio_gpu_create_udmabuf returns -errno). The caller would
handle two different return-value conventions in the same fallback
sequence.
A VFIO_DMABUF_CREATE_ERR_* enum defined in a VFIO header that
hw/display/virtio-gpu-dmabuf.c must include and switch on.
That's adding a VFIO-specific error vocabulary to the display
subsystem's control flow. It introduces a layering violation.
What is important is to acknowledge that how the implementation
arrives at "not applicable" (device lookup failed, region lookup
failed) is irrelevant to the caller.
A VFIO-specific error enum in the return type inverts thatrelationship;it makes the caller encode knowledge of the implementation's internal
failure modes. That's the opposite of what a library API should do.
> 2. Remap -errno.
>
> This is what Cédric suggested [1].
>
> The negative errno values returned by internal callees do not precisely represent the error conditions we need to handle, making negative errno a "bad fit" here. However, the main issue isn't negative errno itself, but rather that we must handle negative errno values originating from different function calls. We can remap the values to resolve this:
>
> static int vfio_device_create_dmabuf_fd(..., Error **errp)
> {
> ...
>
> if (!vfio_device_lookup(iov, &vbasedev, &first_rb, errp)) {
> return -EINVAL;
> }
>
> ...
>
> ret = vfio_device_get_feature(vbasedev, feature);
> if (ret < 0) {
> error_setg_errno(errp, -ret,
> "Could not create dmabuf fd via VFIO device");
> /*
> * Remap -EINVAL: caller uses it to mean
> * "addresses incompatible"
> */
> return (ret == -EINVAL) ? -EIO : ret;
> }
> return ret;
> }
>
> The downside is that it is not KISS; the condition to remap errno encodes more possible errors, but they do not matter to the current caller (and it coalesces error codes from vfio_device_get_feature(), though that is also harmless for the caller).
The KISS argument is a bit thin here. you acknowledge yourself that neither
concern matters to the caller.
> 3. Another better idea?
If the downside is to encode knowledge about what vfio_device_get_feature()
might return (that the caller should not even know about), and future changes
to that function's error codes could silently break the contract, let's use
-ENODEV as a compromise.
int vfio_device_create_dmabuf_fd(struct iovec *iov, unsigned int iov_cnt,
Error **errp)
{
...
if (!vfio_device_lookup(iov, &vbasedev, &first_rb, errp)) {
return -ENODEV; <--------
}
...
for (i = 0; i < iov_cnt; i++) {
if (!vfio_region_lookup(iov[i].iov_base, ®ion,
first_rb, &offset, errp)) {
return -ENODEV; <----------
}
...
}
...
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; /* pass through kernel errno unchanged */
}
return ret;
}
Caller becomes:
res->dmabuf_fd = vfio_device_create_dmabuf_fd(res->iov,
res->iov_cnt,
&local_err);
if (res->dmabuf_fd == -ENODEV) {
error_free_or_abort(&local_err);
qemu_log_mask(LOG_GUEST_ERROR,
"Cannot create dmabuf: incompatible memory\n");
return false;
}
Thanks,
C.
next prev parent reply other threads:[~2026-10-14 16:31 UTC|newest]
Thread overview: 36+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-10-07 20:09 [PATCH v13 0/9] vfio: Create dmabuf from multiple VFIO ranges and use it in virtio-gpu Vivek Kasireddy
2026-10-07 20:09 ` [PATCH v13 1/9] virtio-gpu: Find hva for Guest's DMA addr associated with a ram device Vivek Kasireddy
2026-10-08 9:02 ` Akihiko Odaki
2026-10-09 4:32 ` Kasireddy, Vivek
2026-10-09 9:26 ` Akihiko Odaki
2026-10-12 6:10 ` Kasireddy, Vivek
2026-10-07 20:09 ` [PATCH v13 2/9] virtio-gpu: Rename udmabuf files and helpers to dmabuf Vivek Kasireddy
2026-10-07 20:09 ` [PATCH v13 3/9] virtio-gpu-dmabuf: Remove rcu_read_lock/unlock from virtio_gpu_create_udmabuf() Vivek Kasireddy
2026-10-07 20:09 ` [PATCH v13 4/9] virtio-gpu-dmabuf: Use g_autofree for the list pointer Vivek Kasireddy
2026-10-07 20:09 ` [PATCH v13 5/9] vfio/region: Add a helper to get VFIO region from memory region Vivek Kasireddy
2026-10-07 20:09 ` [PATCH v13 6/9] vfio/device: Add support for creating dmabuf from multiple ranges Vivek Kasireddy
2026-10-08 9:08 ` Akihiko Odaki
2026-10-09 4:24 ` Kasireddy, Vivek
2026-10-09 8:13 ` Cédric Le Goater
2026-10-12 6:27 ` Kasireddy, Vivek
2026-10-12 14:01 ` Akihiko Odaki
2026-10-13 4:06 ` Kasireddy, Vivek
2026-10-13 16:20 ` Cédric Le Goater
2026-10-14 2:17 ` Akihiko Odaki
2026-10-14 7:41 ` Cédric Le Goater
2026-10-14 16:40 ` Cédric Le Goater
2026-10-14 7:03 ` Markus Armbruster
2026-10-14 13:07 ` Akihiko Odaki
2026-10-14 16:29 ` Cédric Le Goater [this message]
2026-10-13 16:40 ` Cédric Le Goater
2026-10-07 20:09 ` [PATCH v13 7/9] vfio/device: Add a helper to mmap a dmabuf Vivek Kasireddy
2026-10-09 8:13 ` Cédric Le Goater
2026-10-07 20:09 ` [PATCH v13 8/9] virtio-gpu-dmabuf: Improve error handling with 'Error **' and err enum Vivek Kasireddy
2026-10-09 8:17 ` Cédric Le Goater
2026-10-07 20:09 ` [PATCH v13 9/9] virtio-gpu-dmabuf: Create dmabuf for blobs associated with VFIO devices Vivek Kasireddy
2026-10-08 9:11 ` Akihiko Odaki
2026-10-09 4:26 ` Kasireddy, Vivek
2026-10-09 9:51 ` Akihiko Odaki
2026-10-12 6:14 ` Kasireddy, Vivek
2026-10-12 14:00 ` Akihiko Odaki
2026-10-13 3:57 ` Kasireddy, Vivek
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=8fabff75-f7d7-40a0-bd8d-acef46b76530@redhat.com \
--to=clg@redhat.com \
--cc=alex@shazbot.org \
--cc=armbru@redhat.com \
--cc=odaki@rsg.ci.i.u-tokyo.ac.jp \
--cc=qemu-devel@nongnu.org \
--cc=vivek.kasireddy@intel.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.