All of lore.kernel.org
 help / color / mirror / Atom feed
From: Markus Armbruster <armbru@redhat.com>
To: "Cédric Le Goater" <clg@redhat.com>
Cc: "Kasireddy, Vivek" <vivek.kasireddy@intel.com>,
	 Akihiko Odaki <odaki@rsg.ci.i.u-tokyo.ac.jp>,
	 "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 09:03:54 +0200	[thread overview]
Message-ID: <87h5ioepb9.fsf@pond.sub.org> (raw)
In-Reply-To: <c18f2fde-5dae-45c5-a61a-8cade3ae5286@redhat.com> ("Cédric Le Goater"'s message of "Tue, 13 Oct 2026 18:20:34 +0200")

Cédric Le Goater <clg@redhat.com> writes:

> On 10/13/26 06:06, Kasireddy, Vivek wrote:
>> Hi Akihiko,
>> 
>>> Subject: Re: [PATCH v13 6/9] vfio/device: Add support for creating
>>> dmabuf from multiple ranges
>>>
>>>>>
>>>>>> In order to create a dmabuf associated with a buffer that spans
>>>>>> multiple ranges, we first need to identify the VFIO region and
>>>>>> index the buffer (represented by iovec) belongs to and then
>>>>>> translate its addresses to offsets within that region.
>>>>>>
>>>>>> The qemu_ram_block_from_host() API gives us both the region and
>>>>>> the offset info we need to populate the dma ranges so that we can
>>>>>> invoke the VFIO_DEVICE_FEATURE_DMA_BUF feature to create the dmabuf.
>>>>>>
>>>>>> Also, instead of iterating over all QOM devices to find the
>>>>>> VFIODevice associated with a memory region, introduce a helper
>>>>>> to just use the vfio_device_list to lookup the VFIODevice.
>>>>>> And, introduce another helper to lookup the VFIO region given an
>>>>>> address.
>>>>>>
>>>>>> Lastly, introduce an enum to return the type of error encountered
>>>>>> while creating the dmabuf fd.
>>>>>>
>>>>>> Cc: Alex Williamson <alex@shazbot.org>
>>>>>> Cc: Cédric Le Goater <clg@redhat.com>
>>>>>> Signed-off-by: Vivek Kasireddy <vivek.kasireddy@intel.com>
>>>>>> ---
>>>>>>     include/hw/vfio/vfio-device.h |  22 +++++++
>>>>>>     hw/vfio/device.c              | 109 ++++++++++++++++++++++++++++++++++
>>>>>>     hw/vfio/dmabuf-stubs.c        |  17 ++++++
>>>>>>     hw/vfio/meson.build           |   1 +
>>>>>>     4 files changed, 149 insertions(+)
>>>>>>     create mode 100644 hw/vfio/dmabuf-stubs.c
>>>>>>
>>>>>> diff --git a/include/hw/vfio/vfio-device.h b/include/hw/vfio/vfio-device.h
>>>>>> index 8472420d3f..c05430be8a 100644
>>>>>> --- a/include/hw/vfio/vfio-device.h
>>>>>> +++ b/include/hw/vfio/vfio-device.h
>>>>>> @@ -41,6 +41,13 @@ enum {
>>>>>>         VFIO_DEVICE_TYPE_AP = 3,
>>>>>>     };
>>>>>>
>>>>>> +enum {
>>>>>> +    /* The Guest passed addresses stored in IOV are invalid */
>>>>>> +    VFIO_DMABUF_CREATE_ERR_INVALID_IOV = -1,
>>>>>> +    /* Guest or Host may be at fault for this type of error */
>>>>>> +    VFIO_DMABUF_CREATE_ERR_UNSPEC = -2,
>>>>>> +};
>>>>>
>>>>> Nack.
>>>>>
>>>>> As said a few times already : it's a VFIO-specific enum leaking
>>>>> into vfio-device.h to serve virtio-gpu's fallback logic. The
>>>>> cleanest alternative is to use standard -errno returns and
>>>>> make the fd an output parameter.
>>>> It looks like Cedric remains unconvinced about the value or usefulness
>>>> of this error enum. Therefore, to make progress, I intend to adopt his
>>>> suggestion to use standard -errno returns for the next version, considering
>>>> that this series has been stuck for a year.
>>>>
>>>> AFAICS, dropping this error enum means that we would not be able to
>>>> clearly identify and log (or attribute) Guest vs Host errors in virtio-gpu,
>>>> particularly the INVALID_IOV case. I am not sure if this can be considered
>>>> a significant limitation. However, if there is a reasonable middle ground
>>>> solution or an alternative that you can think of that does not involve
>>>> using VFIO error enums in virtio-gpu, then I would like to pursue that
>>>> instead.
>>>
>>> Cédric's comment is a reiteration of previous points, but his concrete
>>> code suggestion is new and brings to light some challenges with errno
>>> propagation.
>>>
>>> First, errno does not precisely match the required semantics. We need
>>> to
>>> distinguish lookup failures from the other errors. However, with your
>>> suggestion, lookup failures become -EINVAL and
>>> vfio_device_get_feature()
>>> also returns the same value when the backend lacks the operation,
>>> making
>>> it impossible to distinguish them.

This matters only when something actually needs to distinguish them.

If -EINVAL on lookup failure bothers you, consider -ENOENT.

>>> Propagating errno also requires extra care to preserve it when
>>> necessary. For patch "[PATCH v13 8/9] virtio-gpu-dmabuf: Improve error
>>> handling with 'Error **' and err enum", you suggested to add the
>>> "saved_errno" variable to save errno before error_setg_errno(). That is
>>> redundant because error_setg_errno() preserves errno. Realizing that
>>> requires careful attention.
>>>
>>> While we could spend effort fixing these issues, we can simply avoid the
>>> churn by not using errno.

Two separate styles:

1. Libc commonly sets @errno and returns a single error value.  Care may
   indeed be necessary to preserve errno when passing errors up the
   stack in this style.

2. We commonly use negative errno codes as error values.  Preserving
   @errno is a non-issue then.

>>> In any case, I agree keeping this stuck is not ideal while I don't think
>>> I can provide any further insights. So I'm inviting Markus Armbruster to
>> Looks like you forgot to cc Markus. I have added him now.
>>
>>> this discussion. This is an area better handled by him, who maintains
>>> the error reporting facility, including include/qapi/error.h, which
>>> extensively documents codebase-wide error-handling convention.
>>>
>>> Markus, could you advise on the appropriate convention for returning
>>> this kind of classification alongside Error **? Your perspective
>>> across the codebase may help us settle on a consistent approach
>>> beyond the individual subsystems involved here. Perhaps you may
>>> improve
>>> the documentation according to a design decision made here.

error.h indeed recommends use of negative integers as error values, but
doesn't recommend any particular encoding.  Perhaps it should mention
the common practice of using negative errno codes.

>> Markus, here is some additional context from Akihiko as to why there is
>> a need to introduce an error enum in VFIO:
>> https://lore.kernel.org/qemu-devel/09f02fe2-63cb-4283-899e-4cffc5f60cf1@rsg.ci.i.u-tokyo.ac.jp/
>> 
>>>
>>> If anyone else has a clean middle-ground solution in the meantime, I
>>> would greatly appreciate your input as well.
>> I am not sure if it can be considered clean or elegant but one middle-ground
>> solution I can think of is to have (and use) the error enum just in virtio-gpu
>> only but associate EINVAL (return value of vfio_device_create_dmabuf_fd())
>> as the equivalent of INVALID_IOV. That is, the relevant code now becomes:
>>          res->dmabuf_fd = virtio_gpu_create_udmabuf(res, &local_err);
>>          if (res->dmabuf_fd == VIRTIO_GPU_DMABUF_CREATE_ERR_INVALID_IOV) {
>>              error_free_or_abort(&local_err);
>>              res->dmabuf_fd = vfio_device_create_dmabuf_fd(res->iov,
>>                                                            res->iov_cnt,
>>                                                            &local_err);
>>              if (res->dmabuf_fd == -EINVAL) {
>>                  error_free_or_abort(&local_err);
>>                  qemu_log_mask(LOG_GUEST_ERROR,
>>                                "Cannot create dmabuf: incompatible memory\n");
>>                  return false;
>>              }
>> I think this is reasonable IMO, but is this proposal acceptable to you?
>
> The return value drives control flow.
>
> AFAICT, in virtio_gpu_init_dmabuf(), the only distinction that seems
> to matter for control flow is : "should I try the next backend ?" and
> that's EINVAL, which means "these addresses are incompatible with this
> particular backend."
>
> The guest-vs-host attribution for logging/debugging is in the errp
> messages :
>
>  - "IOV memory address incompatible with udmabuf" : guest gave addresses udmabuf can't handle
>  - "No VFIO device found to create dmabuf from"   : guest addresses don't map to a VFIO device
>  - "UDMABUF_CREATE_LIST: ioctl failed" + errno    :  host/kernel issue
>
> The Error* object is designed to encode a human-readable attribution
> in a return value. It carries the diagnostic detail and the return
> code carries the machine action.

Yes, this is the overwhelmingly common way to do this[*].

When all errors are handled the same, a single return value signifying
failure suffices.  When callers need to handle different values
differently, which is rare, we need several error values.  Negative
errno is often convenient then.  Sometimes, it's a bad fit, so we come
up with some other set of error codes.

Regardless, error values need to be clearly documented.

To decide what error values to use, you should start with how errors are
handled.  Design for that.  In a library, you may not know.  But when
you do, resist the temptation to encode all possible errors perfectly.
KISS.  You can always make it more complicated when new callers require
it.

Does this help?  If not, try asking pointed questions.


[...]


[*] There's also the ErrorClass carried by Error objects.  It was meant
to carry the machine action, but it turned out to be a costly failure.
Use of error classes other than ERROR_CLASS_GENERIC_ERROR is strongly
discouraged.



  parent reply	other threads:[~2026-10-14  7:04 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 [this message]
2026-10-14 13:07               ` Akihiko Odaki
2026-10-14 16:29                 ` Cédric Le Goater
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=87h5ioepb9.fsf@pond.sub.org \
    --to=armbru@redhat.com \
    --cc=alex@shazbot.org \
    --cc=clg@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.