qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: "Daniel P. Berrangé" <berrange@redhat.com>
To: Thomas Huth <thuth@redhat.com>
Cc: "Tomáš Golembiovský" <tgolembi@redhat.com>,
	qemu-devel@nongnu.org, "Michael Roth" <mdroth@linux.vnet.ibm.com>
Subject: Re: [PATCH for-5.2 2/3] qga/commands-posix: Rework build_guest_fsinfo_for_real_device() function
Date: Tue, 21 Jul 2020 09:56:00 +0100	[thread overview]
Message-ID: <20200721085600.GB843362@redhat.com> (raw)
In-Reply-To: <20200720110133.4366-3-thuth@redhat.com>

On Mon, Jul 20, 2020 at 01:01:32PM +0200, Thomas Huth wrote:
> We are going to support non-PCI devices soon. For this we need to split
> the generic GuestDiskAddress and GuestDiskAddressList memory allocation
> and chaining into a separate function first.
> 
> Signed-off-by: Thomas Huth <thuth@redhat.com>
> ---
>  qga/commands-posix.c | 65 ++++++++++++++++++++++++++++----------------
>  1 file changed, 41 insertions(+), 24 deletions(-)
> 
> diff --git a/qga/commands-posix.c b/qga/commands-posix.c
> index 1a62a3a70d..cddbaf5c69 100644
> --- a/qga/commands-posix.c
> +++ b/qga/commands-posix.c
> @@ -861,28 +861,30 @@ static int build_hosts(char const *syspath, char const *host, bool ata,
>      return i;
>  }
>  
> -/* Store disk device info specified by @sysfs into @fs */
> -static void build_guest_fsinfo_for_real_device(char const *syspath,
> -                                               GuestFilesystemInfo *fs,
> -                                               Error **errp)
> +/*
> + * Store disk device info for devices on the PCI bus.
> + * Returns true if information has been stored, or false for failure.
> + */
> +static bool build_guest_fsinfo_for_pci_dev(char const *syspath,
> +                                           GuestDiskAddress *disk,
> +                                           GuestPCIAddress *pciaddr,
> +                                           Error **errp)
>  {
>      unsigned int pci[4], host, hosts[8], tgt[3];
>      int i, nhosts = 0, pcilen;
> -    GuestDiskAddress *disk;
> -    GuestPCIAddress *pciaddr;
> -    GuestDiskAddressList *list = NULL;
>      bool has_ata = false, has_host = false, has_tgt = false;
>      char *p, *q, *driver = NULL;
>  #ifdef CONFIG_LIBUDEV
>      struct udev *udev = NULL;
>      struct udev_device *udevice = NULL;
>  #endif
> +    bool ret = false;
>  
>      p = strstr(syspath, "/devices/pci");
>      if (!p || sscanf(p + 12, "%*x:%*x/%x:%x:%x.%x%n",
>                       pci, pci + 1, pci + 2, pci + 3, &pcilen) < 4) {
>          g_debug("only pci device is supported: sysfs path '%s'", syspath);
> -        return;
> +        return false;
>      }
>  
>      p += 12 + pcilen;
> @@ -903,7 +905,7 @@ static void build_guest_fsinfo_for_real_device(char const *syspath,
>          }
>  
>          g_debug("unsupported driver or sysfs path '%s'", syspath);
> -        return;
> +        return false;
>      }
>  
>      p = strstr(syspath, "/target");
> @@ -929,18 +931,11 @@ static void build_guest_fsinfo_for_real_device(char const *syspath,
>          }
>      }
>  
> -    pciaddr = g_malloc0(sizeof(*pciaddr));
>      pciaddr->domain = pci[0];
>      pciaddr->bus = pci[1];
>      pciaddr->slot = pci[2];
>      pciaddr->function = pci[3];
>  
> -    disk = g_malloc0(sizeof(*disk));
> -    disk->pci_controller = pciaddr;
> -
> -    list = g_malloc0(sizeof(*list));
> -    list->value = disk;
> -
>  #ifdef CONFIG_LIBUDEV
>      udev = udev_new();
>      udevice = udev_device_new_from_syspath(udev, syspath);
> @@ -1018,21 +1013,43 @@ static void build_guest_fsinfo_for_real_device(char const *syspath,
>          goto cleanup;
>      }
>  
> -    list->next = fs->disk;
> -    fs->disk = list;
> -    goto out;
> +    ret = true;
>  
>  cleanup:
> -    if (list) {
> -        qapi_free_GuestDiskAddressList(list);
> -    }
> -out:
>      g_free(driver);
>  #ifdef CONFIG_LIBUDEV
>      udev_unref(udev);
>      udev_device_unref(udevice);
>  #endif
> -    return;
> +    return ret;
> +}
> +
> +/* Store disk device info specified by @sysfs into @fs */
> +static void build_guest_fsinfo_for_real_device(char const *syspath,
> +                                               GuestFilesystemInfo *fs,
> +                                               Error **errp)
> +{
> +    GuestDiskAddress *disk;
> +    GuestPCIAddress *pciaddr;
> +    GuestDiskAddressList *list = NULL;
> +    bool has_pci;
> +
> +    pciaddr = g_malloc(sizeof(*pciaddr));

g_new0 instead of g_malloc and thus kill the sizeof.

> +    memset(pciaddr, -1, sizeof(*pciaddr));  /* -1 means field is invalid */

Each field in GuestPCIAddress is an "int64_t", but memset works on bytes.

So you're not setting the fields to "-1" here, you're setting
each octet in the "int64_t" to -1.

> +
> +    disk = g_malloc0(sizeof(*disk));
> +    disk->pci_controller = pciaddr;
> +
> +    list = g_malloc0(sizeof(*list));
> +    list->value = disk;

g_new0 for these too.

(yes, I realize these were all pre-existing bugs)

> +
> +    has_pci = build_guest_fsinfo_for_pci_dev(syspath, disk, pciaddr, errp);
> +    if (has_pci) {
> +        list->next = fs->disk;
> +        fs->disk = list;
> +    } else {
> +        qapi_free_GuestDiskAddressList(list);
> +    }
>  }


Regards,
Daniel
-- 
|: https://berrange.com      -o-    https://www.flickr.com/photos/dberrange :|
|: https://libvirt.org         -o-            https://fstop138.berrange.com :|
|: https://entangle-photo.org    -o-    https://www.instagram.com/dberrange :|



  reply	other threads:[~2020-07-21  8:56 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-07-20 11:01 [PATCH for-5.2 0/3] Allow guest-get-fsinfo also for non-PCI devices Thomas Huth
2020-07-20 11:01 ` [PATCH for-5.2 1/3] qga/qapi-schema: Document -1 for invalid PCI address fields Thomas Huth
2020-07-21  8:51   ` Daniel P. Berrangé
2020-07-20 11:01 ` [PATCH for-5.2 2/3] qga/commands-posix: Rework build_guest_fsinfo_for_real_device() function Thomas Huth
2020-07-21  8:56   ` Daniel P. Berrangé [this message]
2020-07-20 11:01 ` [PATCH for-5.2 3/3] qga/commands-posix: Move the udev code from the pci to the generic function Thomas Huth
2020-07-21  8:58   ` Daniel P. Berrangé

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=20200721085600.GB843362@redhat.com \
    --to=berrange@redhat.com \
    --cc=mdroth@linux.vnet.ibm.com \
    --cc=qemu-devel@nongnu.org \
    --cc=tgolembi@redhat.com \
    --cc=thuth@redhat.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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).