From: Markus Armbruster <armbru@redhat.com>
To: Eduardo Habkost <ehabkost@redhat.com>
Cc: Mao Zhongyi <maozy.fnst@cn.fujitsu.com>,
mst@redhat.com, jasowang@redhat.com, qemu-devel@nongnu.org,
marcel@redhat.com, alex.williamson@redhat.com, dmitry@daynix.com,
pbonzini@redhat.com, rth@twiddle.net
Subject: Re: [Qemu-devel] [PATCH v3 5/7] pci: Make errp the last parameter of pci_add_capability()
Date: Tue, 06 Jun 2017 18:24:26 +0200 [thread overview]
Message-ID: <8760g9jd8l.fsf@dusky.pond.sub.org> (raw)
In-Reply-To: <20170606145250.GG5016@thinpad.lan.raisama.net> (Eduardo Habkost's message of "Tue, 6 Jun 2017 11:52:50 -0300")
Eduardo Habkost <ehabkost@redhat.com> writes:
> On Tue, Jun 06, 2017 at 07:26:30PM +0800, Mao Zhongyi wrote:
>> Add Error argument for pci_add_capability() to leverage the errp
>> to pass info on errors. This way is helpful for its callers to
>> make a better error handling when moving to 'realize'.
>>
>> Cc: pbonzini@redhat.com
>> Cc: rth@twiddle.net
>> Cc: ehabkost@redhat.com
>> Cc: mst@redhat.com
>> CC: dmitry@daynix.com
>> Cc: jasowang@redhat.com
>> Cc: marcel@redhat.com
>> Cc: alex.williamson@redhat.com
>> Cc: armbru@redhat.com
>> Signed-off-by: Mao Zhongyi <maozy.fnst@cn.fujitsu.com>
>> ---
>> hw/i386/amd_iommu.c | 24 +++++++++++++++++-------
>> hw/net/e1000e.c | 7 ++++++-
>> hw/net/eepro100.c | 20 +++++++++++++++-----
>> hw/pci-bridge/i82801b11.c | 1 +
>> hw/pci/pci.c | 10 ++++------
>> hw/pci/pci_bridge.c | 7 ++++++-
>> hw/pci/pcie.c | 10 ++++++++--
>> hw/pci/shpc.c | 5 ++++-
>> hw/pci/slotid_cap.c | 7 ++++++-
>> hw/vfio/pci.c | 3 ++-
>> hw/virtio/virtio-pci.c | 19 ++++++++++++++-----
>> include/hw/pci/pci.h | 3 ++-
>> 12 files changed, 85 insertions(+), 31 deletions(-)
>
>
> There are multiple places below that checks for errors like this:
>
> function(...);
> if (function succeeded) {
> /* non-error code path here */
> foo = bar;
> }
>
> Sometimes it even includes another branch for the error path:
>
> function(...);
> if (function succeeded) {
> /* non-error code path here */
> foo = bar;
> } else {
> /* error path here */
> return ret;
> }
>
> I suggest doing this instead, for readability:
>
> function(...)
> if (function failed) {
> return ...; /* or: "goto out" */
> }
>
> /* non-error code path here */
> foo = bar;
Yes, please.
>> diff --git a/hw/i386/amd_iommu.c b/hw/i386/amd_iommu.c
>> index 7b6d4ea..d93ffc2 100644
>> --- a/hw/i386/amd_iommu.c
>> +++ b/hw/i386/amd_iommu.c
>> @@ -1158,13 +1158,23 @@ static void amdvi_realize(DeviceState *dev, Error **err)
>> x86_iommu->type = TYPE_AMD;
>> qdev_set_parent_bus(DEVICE(&s->pci), &bus->qbus);
>> object_property_set_bool(OBJECT(&s->pci), true, "realized", err);
>> - s->capab_offset = pci_add_capability(&s->pci.dev, AMDVI_CAPAB_ID_SEC, 0,
>> - AMDVI_CAPAB_SIZE);
>> - assert(s->capab_offset > 0);
>> - ret = pci_add_capability(&s->pci.dev, PCI_CAP_ID_MSI, 0, AMDVI_CAPAB_REG_SIZE);
>> - assert(ret > 0);
>> - ret = pci_add_capability(&s->pci.dev, PCI_CAP_ID_HT, 0, AMDVI_CAPAB_REG_SIZE);
>> - assert(ret > 0);
>> + ret = pci_add_capability(&s->pci.dev, AMDVI_CAPAB_ID_SEC, 0,
>> + AMDVI_CAPAB_SIZE, err);
>> + if (ret < 0) {
>> + return;
>
> Maybe adding a local_err variable is preferred instead of
> checking for (pos < 0), but I'm not sure it's necessary.
>
> Markus, what's the recommendation on those cases? Should we use
> the negative return value to avoid adding an extra local_err
> variable, or should we add local_err anyway to match the existing
> style elsewhere?
Opinions and practice vary on this one.
I prefer checking the return value because it lets me avoid the
error_propagate() boiler-plate more often.
Having both an Error parameter and an error return value poses the
question whether the two agree.
You can assert they do, but it's distracting. We generally don't.
When there's no success value to transmit, you avoid the problem by
making the function return void. We used to favor that, but it has
turned out not to be a success, because it leads to cumbersome code.
For what it's worth, GLib wants you to transmit success / failure in the
return value, too:
https://developer.gnome.org/glib/unstable/glib-Error-Reporting.html#gerror-rules
[...]
next prev parent reply other threads:[~2017-06-06 16:24 UTC|newest]
Thread overview: 15+ messages / expand[flat|nested] mbox.gz Atom feed top
2017-06-06 11:26 [Qemu-devel] [PATCH v3 0/7] Convert to realize and cleanup Mao Zhongyi
2017-06-06 11:26 ` [Qemu-devel] [PATCH v3 1/7] pci: Clean up error checking in pci_add_capability() Mao Zhongyi
2017-06-06 11:26 ` [Qemu-devel] [PATCH v3 2/7] pci: Add comment for pci_add_capability2() Mao Zhongyi
2017-06-06 11:26 ` [Qemu-devel] [PATCH v3 3/7] pci: Fix the return value checking Mao Zhongyi
2017-06-06 11:26 ` [Qemu-devel] [PATCH v3 4/7] net/eepro100: Fix code style Mao Zhongyi
2017-06-06 15:31 ` Michael S. Tsirkin
2017-06-07 2:43 ` Mao Zhongyi
2017-06-06 11:26 ` [Qemu-devel] [PATCH v3 5/7] pci: Make errp the last parameter of pci_add_capability() Mao Zhongyi
2017-06-06 14:52 ` Eduardo Habkost
2017-06-06 16:24 ` Markus Armbruster [this message]
2017-06-07 5:33 ` Mao Zhongyi
2017-06-07 7:05 ` Markus Armbruster
2017-06-07 9:33 ` Mao Zhongyi
2017-06-06 11:26 ` [Qemu-devel] [PATCH v3 6/7] pci: Convert to realize Mao Zhongyi
2017-06-06 11:26 ` [Qemu-devel] [PATCH v3 7/7] pci: Convert shpc_init() to Error Mao Zhongyi
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=8760g9jd8l.fsf@dusky.pond.sub.org \
--to=armbru@redhat.com \
--cc=alex.williamson@redhat.com \
--cc=dmitry@daynix.com \
--cc=ehabkost@redhat.com \
--cc=jasowang@redhat.com \
--cc=maozy.fnst@cn.fujitsu.com \
--cc=marcel@redhat.com \
--cc=mst@redhat.com \
--cc=pbonzini@redhat.com \
--cc=qemu-devel@nongnu.org \
--cc=rth@twiddle.net \
/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.