[PATCH] virtio-gpu: use BIT() macro for scanout bitmask operations

Bin Guo posted 1 patch 1 week, 1 day ago
Patches applied successfully (tree, apply log)
git fetch https://github.com/patchew-project/qemu tags/patchew/20260717015252.25675-1-guobin@linux.alibaba.com
Maintainers: "Michael S. Tsirkin" <mst@redhat.com>, "Alex Bennée" <alex.bennee@linaro.org>, Akihiko Odaki <odaki@rsg.ci.i.u-tokyo.ac.jp>, Dmitry Osipenko <dmitry.osipenko@collabora.com>
hw/display/virtio-gpu.c | 13 +++++++------
1 file changed, 7 insertions(+), 6 deletions(-)
[PATCH] virtio-gpu: use BIT() macro for scanout bitmask operations
Posted by Bin Guo 1 week, 1 day ago
The scanout_bitmask field is uint32_t, but all six bit operations used
the signed literal `1 << shift`.  When the shift count reaches 31 this
is undefined behavior (signed integer overflow).  Replace every
occurrence with the BIT() macro to perform an unsigned shift, which is
well-defined for all bit positions 0-31 and consistent with QEMU
bit-operation conventions.

No functional change; the result is already stored in a uint32_t, so
the generated code is identical on most platforms.

Reviewed-by: Marc-André Lureau <marcandre.lureau@redhat.com>
Signed-off-by: Bin Guo <guobin@linux.alibaba.com>
---
 hw/display/virtio-gpu.c | 13 +++++++------
 1 file changed, 7 insertions(+), 6 deletions(-)

diff --git a/hw/display/virtio-gpu.c b/hw/display/virtio-gpu.c
index c20efe4fb9..cd07b70a05 100644
--- a/hw/display/virtio-gpu.c
+++ b/hw/display/virtio-gpu.c
@@ -12,6 +12,7 @@
  */
 
 #include "qemu/osdep.h"
+#include "qemu/bitops.h"
 #include "qemu/units.h"
 #include "qemu/iov.h"
 #include "system/cpus.h"
@@ -393,7 +394,7 @@ void virtio_gpu_disable_scanout(VirtIOGPU *g, int scanout_id)
 
     res = virtio_gpu_find_resource(g, scanout->resource_id);
     if (res) {
-        res->scanout_bitmask &= ~(1 << scanout_id);
+        res->scanout_bitmask &= ~BIT(scanout_id);
     }
 
     qemu_console_set_surface(scanout->con, NULL);
@@ -411,7 +412,7 @@ static void virtio_gpu_resource_destroy(VirtIOGPU *g,
 
     if (res->scanout_bitmask) {
         for (i = 0; i < g->parent_obj.conf.max_outputs; i++) {
-            if (res->scanout_bitmask & (1 << i)) {
+            if (res->scanout_bitmask & BIT(i)) {
                 virtio_gpu_disable_scanout(g, i);
             }
         }
@@ -577,7 +578,7 @@ static void virtio_gpu_resource_flush(VirtIOGPU *g,
     for (i = 0; i < g->parent_obj.conf.max_outputs; i++) {
         QemuRect rect;
 
-        if (!(res->scanout_bitmask & (1 << i))) {
+        if (!(res->scanout_bitmask & BIT(i))) {
             continue;
         }
         scanout = &g->parent_obj.scanout[i];
@@ -611,10 +612,10 @@ void virtio_gpu_update_scanout(VirtIOGPU *g,
     scanout = &g->parent_obj.scanout[scanout_id];
     ores = virtio_gpu_find_resource(g, scanout->resource_id);
     if (ores) {
-        ores->scanout_bitmask &= ~(1 << scanout_id);
+        ores->scanout_bitmask &= ~BIT(scanout_id);
     }
 
-    res->scanout_bitmask |= (1 << scanout_id);
+    res->scanout_bitmask |= BIT(scanout_id);
     scanout->resource_id = res->resource_id;
     scanout->x = r->x;
     scanout->y = r->y;
@@ -1496,7 +1497,7 @@ static int virtio_gpu_post_load(void *opaque, int version_id)
         if (scanout->cursor.resource_id) {
             update_cursor(g, &scanout->cursor);
         }
-        res->scanout_bitmask |= (1 << i);
+        res->scanout_bitmask |= BIT(i);
     }
 
     return 0;
-- 
2.50.1 (Apple Git-155)


Re: [PATCH] virtio-gpu: use BIT() macro for scanout bitmask operations
Posted by Akihiko Odaki 1 week, 1 day ago
On 2026/07/17 10:52, Bin Guo wrote:
> The scanout_bitmask field is uint32_t, but all six bit operations used
> the signed literal `1 << shift`.  When the shift count reaches 31 this
> is undefined behavior (signed integer overflow).  Replace every
> occurrence with the BIT() macro to perform an unsigned shift, which is
> well-defined for all bit positions 0-31 and consistent with QEMU
> bit-operation conventions.
> 
> No functional change; the result is already stored in a uint32_t, so
> the generated code is identical on most platforms.
> 
> Reviewed-by: Marc-André Lureau <marcandre.lureau@redhat.com>
> Signed-off-by: Bin Guo <guobin@linux.alibaba.com>

Reviewed-by: Akihiko Odaki <odaki@rsg.ci.i.u-tokyo.ac.jp>

Regards,
Akihiko Odaki