diff mbox

[v4,42/47] ivshmem: use strtosz()

Message ID 1443094669-4144-43-git-send-email-marcandre.lureau@redhat.com
State New
Headers show

Commit Message

Marc-André Lureau Sept. 24, 2015, 11:37 a.m. UTC
From: Marc-André Lureau <marcandre.lureau@redhat.com>

Use the common qemu utility function to parse the memory size.

Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
---
 hw/misc/ivshmem.c | 36 +++++-------------------------------
 1 file changed, 5 insertions(+), 31 deletions(-)

Comments

Marc-André Lureau Sept. 24, 2015, 12:13 p.m. UTC | #1
On Thu, Sep 24, 2015 at 1:37 PM,  <marcandre.lureau@redhat.com> wrote:
> From: Marc-André Lureau <marcandre.lureau@redhat.com>
>
> Use the common qemu utility function to parse the memory size.
>
> Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
> ---
>  hw/misc/ivshmem.c | 36 +++++-------------------------------
>  1 file changed, 5 insertions(+), 31 deletions(-)
>
> diff --git a/hw/misc/ivshmem.c b/hw/misc/ivshmem.c
> index 273db36..0ee61d5 100644
> --- a/hw/misc/ivshmem.c
> +++ b/hw/misc/ivshmem.c
> @@ -646,33 +646,6 @@ static void ivshmem_reset(DeviceState *d)
>      ivshmem_use_msix(s);
>  }
>
> -static uint64_t ivshmem_get_size(IVShmemState * s, Error **errp) {
> -
> -    uint64_t value;
> -    char *ptr;
> -
> -    value = strtoull(s->sizearg, &ptr, 10);
> -    switch (*ptr) {
> -        case 0: case 'M': case 'm':
> -            value <<= 20;
> -            break;
> -        case 'G': case 'g':
> -            value <<= 30;
> -            break;
> -        default:
> -            error_setg(errp, "invalid ram size: %s", s->sizearg);
> -            return 0;
> -    }
> -
> -    /* BARs must be a power of 2 */
> -    if (!is_power_of_2(value)) {
> -        error_setg(errp, "size must be power of 2");
> -        return 0;
> -    }
> -
> -    return value;
> -}
> -
>  static int ivshmem_setup_msi(IVShmemState * s)
>  {
>      if (msix_init_exclusive_bar(PCI_DEVICE(s), s->vectors, 1)) {
> @@ -700,16 +673,17 @@ static void pci_ivshmem_realize(PCIDevice *dev, Error **errp)
>      uint8_t *pci_conf;
>      uint8_t attr = PCI_BASE_ADDRESS_SPACE_MEMORY |
>          PCI_BASE_ADDRESS_MEM_PREFETCH;
> -    Error *local_err = NULL;
>
>      if (s->sizearg == NULL) {
>          s->ivshmem_size = 4 << 20; /* 4 MB default */
>      } else {
> -        s->ivshmem_size = ivshmem_get_size(s, &local_err);
> -        if (local_err) {
> -            error_propagate(errp, local_err);
> +        char *end;
> +        int64_t size = strtosz(s->sizearg, &end);
> +        if (size < 0 || *end != '\0') {
> +            error_setg(errp, "Invalid size %s", s->sizearg);
>              return;
>          }
> +        s->ivshmem_size = size;
>      }
>
>      fifo8_create(&s->incoming_fifo, sizeof(long));
> --
> 2.4.3
>


scrap this one, it doesn't check the power of 2, and I wonder if
accepting values lower than megabyte is acceptable
Marc-André Lureau Sept. 24, 2015, 12:33 p.m. UTC | #2
Hi

On Thu, Sep 24, 2015 at 2:13 PM, Marc-André Lureau
<marcandre.lureau@gmail.com> wrote:
> scrap this one, it doesn't check the power of 2, and I wonder if
> accepting values lower than megabyte is acceptable

So the ivshmem server allows kilobytes shm, and with this patch, when
giving non-pow2 size to ivshmem device, you get the following error:
ERROR: PCI region size must be pow2 type=0xc, size=0xc00

This patch might be worth considering as is.
Claudio Fontana Sept. 29, 2015, 2:34 p.m. UTC | #3
On 24.09.2015 13:37, marcandre.lureau@redhat.com wrote:
> From: Marc-André Lureau <marcandre.lureau@redhat.com>
> 
> Use the common qemu utility function to parse the memory size.
> 
> Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
> ---
>  hw/misc/ivshmem.c | 36 +++++-------------------------------
>  1 file changed, 5 insertions(+), 31 deletions(-)
> 
> diff --git a/hw/misc/ivshmem.c b/hw/misc/ivshmem.c
> index 273db36..0ee61d5 100644
> --- a/hw/misc/ivshmem.c
> +++ b/hw/misc/ivshmem.c
> @@ -646,33 +646,6 @@ static void ivshmem_reset(DeviceState *d)
>      ivshmem_use_msix(s);
>  }
>  
> -static uint64_t ivshmem_get_size(IVShmemState * s, Error **errp) {
> -
> -    uint64_t value;
> -    char *ptr;
> -
> -    value = strtoull(s->sizearg, &ptr, 10);
> -    switch (*ptr) {
> -        case 0: case 'M': case 'm':
> -            value <<= 20;
> -            break;
> -        case 'G': case 'g':
> -            value <<= 30;
> -            break;
> -        default:
> -            error_setg(errp, "invalid ram size: %s", s->sizearg);
> -            return 0;
> -    }
> -
> -    /* BARs must be a power of 2 */
> -    if (!is_power_of_2(value)) {
> -        error_setg(errp, "size must be power of 2");
> -        return 0;
> -    }
> -
> -    return value;
> -}
> -
>  static int ivshmem_setup_msi(IVShmemState * s)
>  {
>      if (msix_init_exclusive_bar(PCI_DEVICE(s), s->vectors, 1)) {
> @@ -700,16 +673,17 @@ static void pci_ivshmem_realize(PCIDevice *dev, Error **errp)
>      uint8_t *pci_conf;
>      uint8_t attr = PCI_BASE_ADDRESS_SPACE_MEMORY |
>          PCI_BASE_ADDRESS_MEM_PREFETCH;
> -    Error *local_err = NULL;
>  
>      if (s->sizearg == NULL) {
>          s->ivshmem_size = 4 << 20; /* 4 MB default */
>      } else {
> -        s->ivshmem_size = ivshmem_get_size(s, &local_err);
> -        if (local_err) {
> -            error_propagate(errp, local_err);
> +        char *end;
> +        int64_t size = strtosz(s->sizearg, &end);
> +        if (size < 0 || *end != '\0') {

If there is additional validation that you need to do (in light of your comments to this patch),
it could be done here instead of failing later.

> +            error_setg(errp, "Invalid size %s", s->sizearg);
>              return;
>          }
> +        s->ivshmem_size = size;
>      }
>  
>      fifo8_create(&s->incoming_fifo, sizeof(long));
>
Marc-Andre Lureau Sept. 29, 2015, 2:51 p.m. UTC | #4
----- Original Message -----
> On 24.09.2015 13:37, marcandre.lureau@redhat.com wrote:
> > From: Marc-André Lureau <marcandre.lureau@redhat.com>
> > 
> > Use the common qemu utility function to parse the memory size.
> > 
> > Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
> > ---
> >  hw/misc/ivshmem.c | 36 +++++-------------------------------
> >  1 file changed, 5 insertions(+), 31 deletions(-)
> > 
> > diff --git a/hw/misc/ivshmem.c b/hw/misc/ivshmem.c
> > index 273db36..0ee61d5 100644
> > --- a/hw/misc/ivshmem.c
> > +++ b/hw/misc/ivshmem.c
> > @@ -646,33 +646,6 @@ static void ivshmem_reset(DeviceState *d)
> >      ivshmem_use_msix(s);
> >  }
> >  
> > -static uint64_t ivshmem_get_size(IVShmemState * s, Error **errp) {
> > -
> > -    uint64_t value;
> > -    char *ptr;
> > -
> > -    value = strtoull(s->sizearg, &ptr, 10);
> > -    switch (*ptr) {
> > -        case 0: case 'M': case 'm':
> > -            value <<= 20;
> > -            break;
> > -        case 'G': case 'g':
> > -            value <<= 30;
> > -            break;
> > -        default:
> > -            error_setg(errp, "invalid ram size: %s", s->sizearg);
> > -            return 0;
> > -    }
> > -
> > -    /* BARs must be a power of 2 */
> > -    if (!is_power_of_2(value)) {
> > -        error_setg(errp, "size must be power of 2");
> > -        return 0;
> > -    }
> > -
> > -    return value;
> > -}
> > -
> >  static int ivshmem_setup_msi(IVShmemState * s)
> >  {
> >      if (msix_init_exclusive_bar(PCI_DEVICE(s), s->vectors, 1)) {
> > @@ -700,16 +673,17 @@ static void pci_ivshmem_realize(PCIDevice *dev, Error
> > **errp)
> >      uint8_t *pci_conf;
> >      uint8_t attr = PCI_BASE_ADDRESS_SPACE_MEMORY |
> >          PCI_BASE_ADDRESS_MEM_PREFETCH;
> > -    Error *local_err = NULL;
> >  
> >      if (s->sizearg == NULL) {
> >          s->ivshmem_size = 4 << 20; /* 4 MB default */
> >      } else {
> > -        s->ivshmem_size = ivshmem_get_size(s, &local_err);
> > -        if (local_err) {
> > -            error_propagate(errp, local_err);
> > +        char *end;
> > +        int64_t size = strtosz(s->sizearg, &end);
> > +        if (size < 0 || *end != '\0') {
> 
> If there is additional validation that you need to do (in light of your
> comments to this patch),
> it could be done here instead of failing later.

It will duplicate the check done by pci bar code, but I agree it could be done here too. I'll add it.

> 
> > +            error_setg(errp, "Invalid size %s", s->sizearg);
> >              return;
> >          }
> > +        s->ivshmem_size = size;
> >      }
> >  
> >      fifo8_create(&s->incoming_fifo, sizeof(long));
> > 
> 
> 
>
Claudio Fontana Sept. 30, 2015, 8:39 a.m. UTC | #5
On 24.09.2015 13:37, marcandre.lureau@redhat.com wrote:
> From: Marc-André Lureau <marcandre.lureau@redhat.com>
> 
> Use the common qemu utility function to parse the memory size.
> 
> Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
> ---
>  hw/misc/ivshmem.c | 36 +++++-------------------------------
>  1 file changed, 5 insertions(+), 31 deletions(-)
> 
> diff --git a/hw/misc/ivshmem.c b/hw/misc/ivshmem.c
> index 273db36..0ee61d5 100644
> --- a/hw/misc/ivshmem.c
> +++ b/hw/misc/ivshmem.c
> @@ -646,33 +646,6 @@ static void ivshmem_reset(DeviceState *d)
>      ivshmem_use_msix(s);
>  }
>  
> -static uint64_t ivshmem_get_size(IVShmemState * s, Error **errp) {
> -
> -    uint64_t value;
> -    char *ptr;
> -
> -    value = strtoull(s->sizearg, &ptr, 10);
> -    switch (*ptr) {
> -        case 0: case 'M': case 'm':
> -            value <<= 20;
> -            break;
> -        case 'G': case 'g':
> -            value <<= 30;
> -            break;
> -        default:
> -            error_setg(errp, "invalid ram size: %s", s->sizearg);
> -            return 0;
> -    }
> -
> -    /* BARs must be a power of 2 */
> -    if (!is_power_of_2(value)) {
> -        error_setg(errp, "size must be power of 2");
> -        return 0;
> -    }
> -
> -    return value;
> -}
> -
>  static int ivshmem_setup_msi(IVShmemState * s)
>  {
>      if (msix_init_exclusive_bar(PCI_DEVICE(s), s->vectors, 1)) {
> @@ -700,16 +673,17 @@ static void pci_ivshmem_realize(PCIDevice *dev, Error **errp)
>      uint8_t *pci_conf;
>      uint8_t attr = PCI_BASE_ADDRESS_SPACE_MEMORY |
>          PCI_BASE_ADDRESS_MEM_PREFETCH;
> -    Error *local_err = NULL;
>  
>      if (s->sizearg == NULL) {
>          s->ivshmem_size = 4 << 20; /* 4 MB default */
>      } else {
> -        s->ivshmem_size = ivshmem_get_size(s, &local_err);
> -        if (local_err) {
> -            error_propagate(errp, local_err);
> +        char *end;
> +        int64_t size = strtosz(s->sizearg, &end);

hmm the function name is now qemu_strtosz, changed by yourself.
Are you on latest master?

> +        if (size < 0 || *end != '\0') {
> +            error_setg(errp, "Invalid size %s", s->sizearg);
>              return;
>          }
> +        s->ivshmem_size = size;
>      }
>  
>      fifo8_create(&s->incoming_fifo, sizeof(long));
>
diff mbox

Patch

diff --git a/hw/misc/ivshmem.c b/hw/misc/ivshmem.c
index 273db36..0ee61d5 100644
--- a/hw/misc/ivshmem.c
+++ b/hw/misc/ivshmem.c
@@ -646,33 +646,6 @@  static void ivshmem_reset(DeviceState *d)
     ivshmem_use_msix(s);
 }
 
-static uint64_t ivshmem_get_size(IVShmemState * s, Error **errp) {
-
-    uint64_t value;
-    char *ptr;
-
-    value = strtoull(s->sizearg, &ptr, 10);
-    switch (*ptr) {
-        case 0: case 'M': case 'm':
-            value <<= 20;
-            break;
-        case 'G': case 'g':
-            value <<= 30;
-            break;
-        default:
-            error_setg(errp, "invalid ram size: %s", s->sizearg);
-            return 0;
-    }
-
-    /* BARs must be a power of 2 */
-    if (!is_power_of_2(value)) {
-        error_setg(errp, "size must be power of 2");
-        return 0;
-    }
-
-    return value;
-}
-
 static int ivshmem_setup_msi(IVShmemState * s)
 {
     if (msix_init_exclusive_bar(PCI_DEVICE(s), s->vectors, 1)) {
@@ -700,16 +673,17 @@  static void pci_ivshmem_realize(PCIDevice *dev, Error **errp)
     uint8_t *pci_conf;
     uint8_t attr = PCI_BASE_ADDRESS_SPACE_MEMORY |
         PCI_BASE_ADDRESS_MEM_PREFETCH;
-    Error *local_err = NULL;
 
     if (s->sizearg == NULL) {
         s->ivshmem_size = 4 << 20; /* 4 MB default */
     } else {
-        s->ivshmem_size = ivshmem_get_size(s, &local_err);
-        if (local_err) {
-            error_propagate(errp, local_err);
+        char *end;
+        int64_t size = strtosz(s->sizearg, &end);
+        if (size < 0 || *end != '\0') {
+            error_setg(errp, "Invalid size %s", s->sizearg);
             return;
         }
+        s->ivshmem_size = size;
     }
 
     fifo8_create(&s->incoming_fifo, sizeof(long));