All of lore.kernel.org
 help / color / mirror / Atom feed
* [Qemu-devel] [PATCH] virtio-gpu: fix bug in host memory calculation.
@ 2017-11-08 18:05 Tao Wu
  2017-11-09 10:34 ` Marc-André Lureau
  0 siblings, 1 reply; 5+ messages in thread
From: Tao Wu @ 2017-11-08 18:05 UTC (permalink / raw)
  To: qemu-devel, kraxel; +Cc: marcandre.lureau, airlied, liqiang6-s, Tao Wu

The old code treats bits as bytes when calculating host memory usage.
Change it to be consistent with allocation logic in pixman library.

Signed-off-by: Tao Wu <lepton@google.com>
---
 hw/display/virtio-gpu.c | 13 +++++++++++--
 1 file changed, 11 insertions(+), 2 deletions(-)

diff --git a/hw/display/virtio-gpu.c b/hw/display/virtio-gpu.c
index 43bbe09ea0..428786f291 100644
--- a/hw/display/virtio-gpu.c
+++ b/hw/display/virtio-gpu.c
@@ -322,6 +322,15 @@ static pixman_format_code_t get_pixman_format(uint32_t virtio_gpu_format)
     }
 }
 
+static uint32_t calc_image_hostmem(pixman_format_code_t pformat,
+                                   uint32_t width, uint32_t height)
+{
+    /* copied from pixman/pixman-bits-image.c, skip integer overflow check. */
+    int bpp = PIXMAN_FORMAT_BPP(pformat);
+    int stride = ((width * bpp + 0x1f) >> 5) * sizeof(uint32_t);
+    return height * stride;
+}
+
 static void virtio_gpu_resource_create_2d(VirtIOGPU *g,
                                           struct virtio_gpu_ctrl_command *cmd)
 {
@@ -366,7 +375,7 @@ static void virtio_gpu_resource_create_2d(VirtIOGPU *g,
         return;
     }
 
-    res->hostmem = PIXMAN_FORMAT_BPP(pformat) * c2d.width * c2d.height;
+    res->hostmem = calc_image_hostmem(pformat, c2d.width, c2d.height);
     if (res->hostmem + g->hostmem < g->conf.max_hostmem) {
         res->image = pixman_image_create_bits(pformat,
                                               c2d.width,
@@ -1087,7 +1096,7 @@ static int virtio_gpu_load(QEMUFile *f, void *opaque, size_t size,
             return -EINVAL;
         }
 
-        res->hostmem = PIXMAN_FORMAT_BPP(pformat) * res->width * res->height;
+        res->hostmem = calc_image_hostmem(pformat, res->width, res->height);
 
         res->addrs = g_new(uint64_t, res->iov_cnt);
         res->iov = g_new(struct iovec, res->iov_cnt);
-- 
2.15.0.403.gc27cc4dac6-goog

^ permalink raw reply related	[flat|nested] 5+ messages in thread

* Re: [Qemu-devel] [PATCH] virtio-gpu: fix bug in host memory calculation.
  2017-11-08 18:05 [Qemu-devel] [PATCH] virtio-gpu: fix bug in host memory calculation Tao Wu
@ 2017-11-09 10:34 ` Marc-André Lureau
  2017-11-09 18:17   ` Tao Wu
  2017-11-09 18:20   ` Tao Wu(吴涛@Eng)
  0 siblings, 2 replies; 5+ messages in thread
From: Marc-André Lureau @ 2017-11-09 10:34 UTC (permalink / raw)
  To: Tao Wu; +Cc: qemu-devel, kraxel, airlied, liqiang6-s

Hi

----- Original Message -----
> The old code treats bits as bytes when calculating host memory usage.
> Change it to be consistent with allocation logic in pixman library.
> 

Good catch

> Signed-off-by: Tao Wu <lepton@google.com>
> ---
>  hw/display/virtio-gpu.c | 13 +++++++++++--
>  1 file changed, 11 insertions(+), 2 deletions(-)
> 
> diff --git a/hw/display/virtio-gpu.c b/hw/display/virtio-gpu.c
> index 43bbe09ea0..428786f291 100644
> --- a/hw/display/virtio-gpu.c
> +++ b/hw/display/virtio-gpu.c
> @@ -322,6 +322,15 @@ static pixman_format_code_t get_pixman_format(uint32_t
> virtio_gpu_format)
>      }
>  }
>  
> +static uint32_t calc_image_hostmem(pixman_format_code_t pformat,
> +                                   uint32_t width, uint32_t height)
> +{
> +    /* copied from pixman/pixman-bits-image.c, skip integer overflow check.
> */

So we rely on pixman create_bits() to fail if overflow happened? perhaps it's worth a comment.
 
> +    int bpp = PIXMAN_FORMAT_BPP(pformat);
> +    int stride = ((width * bpp + 0x1f) >> 5) * sizeof(uint32_t);
> +    return height * stride;
> +}
> +
>  static void virtio_gpu_resource_create_2d(VirtIOGPU *g,
>                                            struct virtio_gpu_ctrl_command
>                                            *cmd)
>  {
> @@ -366,7 +375,7 @@ static void virtio_gpu_resource_create_2d(VirtIOGPU *g,
>          return;
>      }
>  
> -    res->hostmem = PIXMAN_FORMAT_BPP(pformat) * c2d.width * c2d.height;
> +    res->hostmem = calc_image_hostmem(pformat, c2d.width, c2d.height);
>      if (res->hostmem + g->hostmem < g->conf.max_hostmem) {
>          res->image = pixman_image_create_bits(pformat,
>                                                c2d.width,
> @@ -1087,7 +1096,7 @@ static int virtio_gpu_load(QEMUFile *f, void *opaque,
> size_t size,
>              return -EINVAL;
>          }
>  
> -        res->hostmem = PIXMAN_FORMAT_BPP(pformat) * res->width *
> res->height;
> +        res->hostmem = calc_image_hostmem(pformat, res->width, res->height);
>  
>          res->addrs = g_new(uint64_t, res->iov_cnt);
>          res->iov = g_new(struct iovec, res->iov_cnt);
> --
> 2.15.0.403.gc27cc4dac6-goog
> 

looks good otherwise,

Reviewed-by: Marc-André Lureau <marcandre.lureau@redhat.com>

> 

^ permalink raw reply	[flat|nested] 5+ messages in thread

* [Qemu-devel] [PATCH] virtio-gpu: fix bug in host memory calculation.
  2017-11-09 10:34 ` Marc-André Lureau
@ 2017-11-09 18:17   ` Tao Wu
  2017-11-09 18:19     ` Marc-André Lureau
  2017-11-09 18:20   ` Tao Wu(吴涛@Eng)
  1 sibling, 1 reply; 5+ messages in thread
From: Tao Wu @ 2017-11-09 18:17 UTC (permalink / raw)
  To: qemu-devel, kraxel; +Cc: marcandre.lureau, airlied, Tao Wu

The old code treats bits as bytes when calculating host memory usage.
Change it to be consistent with allocation logic in pixman library.

Signed-off-by: Tao Wu <lepton@google.com>
---
 hw/display/virtio-gpu.c | 16 ++++++++++++++--
 1 file changed, 14 insertions(+), 2 deletions(-)

diff --git a/hw/display/virtio-gpu.c b/hw/display/virtio-gpu.c
index 43bbe09ea0..274e365713 100644
--- a/hw/display/virtio-gpu.c
+++ b/hw/display/virtio-gpu.c
@@ -322,6 +322,18 @@ static pixman_format_code_t get_pixman_format(uint32_t virtio_gpu_format)
     }
 }
 
+static uint32_t calc_image_hostmem(pixman_format_code_t pformat,
+                                   uint32_t width, uint32_t height)
+{
+    /* Copied from pixman/pixman-bits-image.c, skip integer overflow check.
+     * pixman_image_create_bits will fail in case it overflow.
+     */
+
+    int bpp = PIXMAN_FORMAT_BPP(pformat);
+    int stride = ((width * bpp + 0x1f) >> 5) * sizeof(uint32_t);
+    return height * stride;
+}
+
 static void virtio_gpu_resource_create_2d(VirtIOGPU *g,
                                           struct virtio_gpu_ctrl_command *cmd)
 {
@@ -366,7 +378,7 @@ static void virtio_gpu_resource_create_2d(VirtIOGPU *g,
         return;
     }
 
-    res->hostmem = PIXMAN_FORMAT_BPP(pformat) * c2d.width * c2d.height;
+    res->hostmem = calc_image_hostmem(pformat, c2d.width, c2d.height);
     if (res->hostmem + g->hostmem < g->conf.max_hostmem) {
         res->image = pixman_image_create_bits(pformat,
                                               c2d.width,
@@ -1087,7 +1099,7 @@ static int virtio_gpu_load(QEMUFile *f, void *opaque, size_t size,
             return -EINVAL;
         }
 
-        res->hostmem = PIXMAN_FORMAT_BPP(pformat) * res->width * res->height;
+        res->hostmem = calc_image_hostmem(pformat, res->width, res->height);
 
         res->addrs = g_new(uint64_t, res->iov_cnt);
         res->iov = g_new(struct iovec, res->iov_cnt);
-- 
2.15.0.448.gf294e3d99a-goog

^ permalink raw reply related	[flat|nested] 5+ messages in thread

* Re: [Qemu-devel] [PATCH] virtio-gpu: fix bug in host memory calculation.
  2017-11-09 18:17   ` Tao Wu
@ 2017-11-09 18:19     ` Marc-André Lureau
  0 siblings, 0 replies; 5+ messages in thread
From: Marc-André Lureau @ 2017-11-09 18:19 UTC (permalink / raw)
  To: Tao Wu; +Cc: QEMU, Gerd Hoffmann, David Airlie

On Thu, Nov 9, 2017 at 7:17 PM, Tao Wu via Qemu-devel
<qemu-devel@nongnu.org> wrote:
> The old code treats bits as bytes when calculating host memory usage.
> Change it to be consistent with allocation logic in pixman library.
>
> Signed-off-by: Tao Wu <lepton@google.com>

Reviewed-by: Marc-André Lureau <marcandre.lureau@redhat.com>
> ---
>  hw/display/virtio-gpu.c | 16 ++++++++++++++--
>  1 file changed, 14 insertions(+), 2 deletions(-)
>
> diff --git a/hw/display/virtio-gpu.c b/hw/display/virtio-gpu.c
> index 43bbe09ea0..274e365713 100644
> --- a/hw/display/virtio-gpu.c
> +++ b/hw/display/virtio-gpu.c
> @@ -322,6 +322,18 @@ static pixman_format_code_t get_pixman_format(uint32_t virtio_gpu_format)
>      }
>  }
>
> +static uint32_t calc_image_hostmem(pixman_format_code_t pformat,
> +                                   uint32_t width, uint32_t height)
> +{
> +    /* Copied from pixman/pixman-bits-image.c, skip integer overflow check.
> +     * pixman_image_create_bits will fail in case it overflow.
> +     */
> +
> +    int bpp = PIXMAN_FORMAT_BPP(pformat);
> +    int stride = ((width * bpp + 0x1f) >> 5) * sizeof(uint32_t);
> +    return height * stride;
> +}
> +
>  static void virtio_gpu_resource_create_2d(VirtIOGPU *g,
>                                            struct virtio_gpu_ctrl_command *cmd)
>  {
> @@ -366,7 +378,7 @@ static void virtio_gpu_resource_create_2d(VirtIOGPU *g,
>          return;
>      }
>
> -    res->hostmem = PIXMAN_FORMAT_BPP(pformat) * c2d.width * c2d.height;
> +    res->hostmem = calc_image_hostmem(pformat, c2d.width, c2d.height);
>      if (res->hostmem + g->hostmem < g->conf.max_hostmem) {
>          res->image = pixman_image_create_bits(pformat,
>                                                c2d.width,
> @@ -1087,7 +1099,7 @@ static int virtio_gpu_load(QEMUFile *f, void *opaque, size_t size,
>              return -EINVAL;
>          }
>
> -        res->hostmem = PIXMAN_FORMAT_BPP(pformat) * res->width * res->height;
> +        res->hostmem = calc_image_hostmem(pformat, res->width, res->height);
>
>          res->addrs = g_new(uint64_t, res->iov_cnt);
>          res->iov = g_new(struct iovec, res->iov_cnt);
> --
> 2.15.0.448.gf294e3d99a-goog
>
>



-- 
Marc-André Lureau

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [Qemu-devel] [PATCH] virtio-gpu: fix bug in host memory calculation.
  2017-11-09 10:34 ` Marc-André Lureau
  2017-11-09 18:17   ` Tao Wu
@ 2017-11-09 18:20   ` Tao Wu(吴涛@Eng)
  1 sibling, 0 replies; 5+ messages in thread
From: Tao Wu(吴涛@Eng) @ 2017-11-09 18:20 UTC (permalink / raw)
  To: Marc-André Lureau; +Cc: qemu-devel, kraxel, airlied, liqiang6-s

Thanks. Sent out a new version add comments to say that we rely on
pixman create_bits to fail.

On Thu, Nov 9, 2017 at 2:34 AM, Marc-André Lureau
<marcandre.lureau@redhat.com> wrote:
> Hi
>
> ----- Original Message -----
>> The old code treats bits as bytes when calculating host memory usage.
>> Change it to be consistent with allocation logic in pixman library.
>>
>
> Good catch
>
>> Signed-off-by: Tao Wu <lepton@google.com>
>> ---
>>  hw/display/virtio-gpu.c | 13 +++++++++++--
>>  1 file changed, 11 insertions(+), 2 deletions(-)
>>
>> diff --git a/hw/display/virtio-gpu.c b/hw/display/virtio-gpu.c
>> index 43bbe09ea0..428786f291 100644
>> --- a/hw/display/virtio-gpu.c
>> +++ b/hw/display/virtio-gpu.c
>> @@ -322,6 +322,15 @@ static pixman_format_code_t get_pixman_format(uint32_t
>> virtio_gpu_format)
>>      }
>>  }
>>
>> +static uint32_t calc_image_hostmem(pixman_format_code_t pformat,
>> +                                   uint32_t width, uint32_t height)
>> +{
>> +    /* copied from pixman/pixman-bits-image.c, skip integer overflow check.
>> */
>
> So we rely on pixman create_bits() to fail if overflow happened? perhaps it's worth a comment.
>
>> +    int bpp = PIXMAN_FORMAT_BPP(pformat);
>> +    int stride = ((width * bpp + 0x1f) >> 5) * sizeof(uint32_t);
>> +    return height * stride;
>> +}
>> +
>>  static void virtio_gpu_resource_create_2d(VirtIOGPU *g,
>>                                            struct virtio_gpu_ctrl_command
>>                                            *cmd)
>>  {
>> @@ -366,7 +375,7 @@ static void virtio_gpu_resource_create_2d(VirtIOGPU *g,
>>          return;
>>      }
>>
>> -    res->hostmem = PIXMAN_FORMAT_BPP(pformat) * c2d.width * c2d.height;
>> +    res->hostmem = calc_image_hostmem(pformat, c2d.width, c2d.height);
>>      if (res->hostmem + g->hostmem < g->conf.max_hostmem) {
>>          res->image = pixman_image_create_bits(pformat,
>>                                                c2d.width,
>> @@ -1087,7 +1096,7 @@ static int virtio_gpu_load(QEMUFile *f, void *opaque,
>> size_t size,
>>              return -EINVAL;
>>          }
>>
>> -        res->hostmem = PIXMAN_FORMAT_BPP(pformat) * res->width *
>> res->height;
>> +        res->hostmem = calc_image_hostmem(pformat, res->width, res->height);
>>
>>          res->addrs = g_new(uint64_t, res->iov_cnt);
>>          res->iov = g_new(struct iovec, res->iov_cnt);
>> --
>> 2.15.0.403.gc27cc4dac6-goog
>>
>
> looks good otherwise,
>
> Reviewed-by: Marc-André Lureau <marcandre.lureau@redhat.com>
>
>>

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2017-11-09 18:20 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-11-08 18:05 [Qemu-devel] [PATCH] virtio-gpu: fix bug in host memory calculation Tao Wu
2017-11-09 10:34 ` Marc-André Lureau
2017-11-09 18:17   ` Tao Wu
2017-11-09 18:19     ` Marc-André Lureau
2017-11-09 18:20   ` Tao Wu(吴涛@Eng)

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.