Skip to contentSkip to conversation

hw/s390/css: avoid taking address members in packed structs

PATCH Halil Pasic 6 participants 10 messages Apr 1, 2019 – Apr 12, 2019 5 messages not in the archive

lore
Share & tools
lore

A message that is not in the archive (sent off-list or not yet received): [email protected]

A message that is not in the archive (sent off-list or not yet received): [email protected]

Halil Pasic

author Reviewed-by
#2 lore raw
Quoted 22 lines · On Fri, 29 Mar 2019 11:11:02 +0000 Daniel P. Berrangé <[email protected]>…

The GCC 9 compiler complains about many places in s390 code that take the address of members of the 'struct SCHIB' which is marked packed:

hw/s390x/css.c: In function ‘sch_handle_clear_func’:
hw/s390x/css.c:698:15: warning: taking address of packed member of ‘struct SCHIB’ may result in an unaligned pointer val\
ue [-Waddress-of-packed-member]

  698 |     PMCW *p = &sch->curr_status.pmcw;
      |               ^~~~~~~~~~~~~~~~~~~~~~
hw/s390x/css.c:699:15: warning: taking address of packed member of ‘struct SCHIB’ may result in an unaligned pointer val\
ue [-Waddress-of-packed-member]
  699 |     SCSW *s = &sch->curr_status.scsw;
      |               ^~~~~~~~~~~~~~~~~~~~~~

...snip many more...

Almost all of these are just done for convenience to avoid typing out long variable/field names when referencing struct members. We can get most of this convenience by taking the address of the 'struct SCHIB' instead, avoiding triggering the compiler warnings.

Quoted 5 lines

In a couple of places we copy via a local variable which is a technique already applied elsewhere in s390 code for this problem.

Thanks!

A message that is not in the archive (sent off-list or not yet received): [email protected]

author Reviewed-by
#4 lore raw

Re: [Qemu-devel] [PATCH 11/14] hw/vfio/ccw: avoid taking address members in packed structs

Quoted 22 lines · On Fri, 29 Mar 2019 11:11:01 +0000 Daniel P. Berrangé <[email protected]>…

The GCC 9 compiler complains about many places in s390 code that take the address of members of the 'struct SCHIB' which is marked packed:

hw/vfio/ccw.c: In function ‘vfio_ccw_io_notifier_handler’:
hw/vfio/ccw.c:133:15: warning: taking address of packed member of ‘struct SCHIB’ may result in an unaligned pointer value \
[-Waddress-of-packed-member]

  133 |     SCSW *s = &sch->curr_status.scsw;
      |               ^~~~~~~~~~~~~~~~~~~~~~
hw/vfio/ccw.c:134:15: warning: taking address of packed member of ‘struct SCHIB’ may result in an unaligned pointer value \
[-Waddress-of-packed-member]
  134 |     PMCW *p = &sch->curr_status.pmcw;
      |               ^~~~~~~~~~~~~~~~~~~~~~

...snip many more...

Almost all of these are just done for convenience to avoid typing out long variable/field names when referencing struct members. We can get most of this convenience by taking the address of the 'struct SCHIB' instead, avoiding triggering the compiler warnings.

Quoted 5 lines

In a couple of places we copy via a local variable which is a technique already applied elsewhere in s390 code for this problem.

BTW the reason for SCHIB being packed is the padding that would emerge at the end of the struct (sizeof(SCHIB) == 52 and non-packed sizeof(SCHIB) == 54). So getting rid of packed seems to be viable as long as we write guest memory with the correct size (52 bytes).

Regards,
Halil

Reviewed-by
#5 lore raw

Re: [Qemu-devel] [PATCH 11/14] hw/vfio/ccw: avoid taking address members in packed structs

In reply to a message not in the archive
Quoted 103 lines · Daniel P. Berrangé

The GCC 9 compiler complains about many places in s390 code that take the address of members of the 'struct SCHIB' which is marked packed:

hw/vfio/ccw.c: In function ‘vfio_ccw_io_notifier_handler’:
hw/vfio/ccw.c:133:15: warning: taking address of packed member of ‘struct SCHIB’ may result in an unaligned pointer value \
[-Waddress-of-packed-member]

   133 |     SCSW *s = &sch->curr_status.scsw;
       |               ^~~~~~~~~~~~~~~~~~~~~~
hw/vfio/ccw.c:134:15: warning: taking address of packed member of ‘struct SCHIB’ may result in an unaligned pointer value \
[-Waddress-of-packed-member]
   134 |     PMCW *p = &sch->curr_status.pmcw;
       |               ^~~~~~~~~~~~~~~~~~~~~~

...snip many more...

Almost all of these are just done for convenience to avoid typing out long variable/field names when referencing struct members. We can get most of this convenience by taking the address of the 'struct SCHIB' instead, avoiding triggering the compiler warnings.

In a couple of places we copy via a local variable which is a technique already applied elsewhere in s390 code for this problem.

---
  hw/vfio/ccw.c | 42 ++++++++++++++++++++++--------------------
  1 file changed, 22 insertions(+), 20 deletions(-)
hw/vfio/ccw.c +19 -18
index 9246729a75..c44d13cc50 100644
@@ -130,8 +130,8 @@ static void vfio_ccw_io_notifier_handler(void *opaque)
      S390CCWDevice *cdev = S390_CCW_DEVICE(vcdev);
      CcwDevice *ccw_dev = CCW_DEVICE(cdev);
      SubchDev *sch = ccw_dev->sch;
-    SCSW *s = &sch->curr_status.scsw;
-    PMCW *p = &sch->curr_status.pmcw;
+    SCHIB *schib = &sch->curr_status;
+    SCSW s;
      IRB irb;
      int size;
 
@@ -145,33 +145,33 @@ static void vfio_ccw_io_notifier_handler(void *opaque)
          switch (errno) {
          case ENODEV:
              /* Generate a deferred cc 3 condition. */
-            s->flags |= SCSW_FLAGS_MASK_CC;
-            s->ctrl &= ~SCSW_CTRL_MASK_STCTL;
-            s->ctrl |= (SCSW_STCTL_ALERT | SCSW_STCTL_STATUS_PEND);
+            schib->scsw.flags |= SCSW_FLAGS_MASK_CC;
+            schib->scsw.ctrl &= ~SCSW_CTRL_MASK_STCTL;
+            schib->scsw.ctrl |= (SCSW_STCTL_ALERT | SCSW_STCTL_STATUS_PEND);
              goto read_err;
          case EFAULT:
              /* Memory problem, generate channel data check. */
-            s->ctrl &= ~SCSW_ACTL_START_PEND;
-            s->cstat = SCSW_CSTAT_DATA_CHECK;
-            s->ctrl &= ~SCSW_CTRL_MASK_STCTL;
-            s->ctrl |= SCSW_STCTL_PRIMARY | SCSW_STCTL_SECONDARY |
+            schib->scsw.ctrl &= ~SCSW_ACTL_START_PEND;
+            schib->scsw.cstat = SCSW_CSTAT_DATA_CHECK;
+            schib->scsw.ctrl &= ~SCSW_CTRL_MASK_STCTL;
+            schib->scsw.ctrl |= SCSW_STCTL_PRIMARY | SCSW_STCTL_SECONDARY |
                         SCSW_STCTL_ALERT | SCSW_STCTL_STATUS_PEND;
              goto read_err;
          default:
              /* Error, generate channel program check. */
-            s->ctrl &= ~SCSW_ACTL_START_PEND;
-            s->cstat = SCSW_CSTAT_PROG_CHECK;
-            s->ctrl &= ~SCSW_CTRL_MASK_STCTL;
-            s->ctrl |= SCSW_STCTL_PRIMARY | SCSW_STCTL_SECONDARY |
+            schib->scsw.ctrl &= ~SCSW_ACTL_START_PEND;
+            schib->scsw.cstat = SCSW_CSTAT_PROG_CHECK;
+            schib->scsw.ctrl &= ~SCSW_CTRL_MASK_STCTL;
+            schib->scsw.ctrl |= SCSW_STCTL_PRIMARY | SCSW_STCTL_SECONDARY |
                         SCSW_STCTL_ALERT | SCSW_STCTL_STATUS_PEND;
              goto read_err;
          }
      } else if (size != vcdev->io_region_size) {
          /* Information transfer error, generate channel-control check. */
-        s->ctrl &= ~SCSW_ACTL_START_PEND;
-        s->cstat = SCSW_CSTAT_CHN_CTRL_CHK;
-        s->ctrl &= ~SCSW_CTRL_MASK_STCTL;
-        s->ctrl |= SCSW_STCTL_PRIMARY | SCSW_STCTL_SECONDARY |
+        schib->scsw.ctrl &= ~SCSW_ACTL_START_PEND;
+        schib->scsw.cstat = SCSW_CSTAT_CHN_CTRL_CHK;
+        schib->scsw.ctrl &= ~SCSW_CTRL_MASK_STCTL;
+        schib->scsw.ctrl |= SCSW_STCTL_PRIMARY | SCSW_STCTL_SECONDARY |
                     SCSW_STCTL_ALERT | SCSW_STCTL_STATUS_PEND;
          goto read_err;
      }
@@ -179,11 +179,13 @@ static void vfio_ccw_io_notifier_handler(void *opaque)
      memcpy(&irb, region->irb_area, sizeof(IRB));
 
      /* Update control block via irb. */
-    copy_scsw_to_guest(s, &irb.scsw);
+    s = schib->scsw;
+    copy_scsw_to_guest(&s, &irb.scsw);

hw/vfio/ccw.c static void vfio_ccw_io_notifier_handler(void *opaque) · line 191

Quoted 10 lines
hw/vfio/ccw.c +3 -2
+    schib->scsw = s;
 
      /* If a uint check is pending, copy sense data. */
-    if ((s->dstat & SCSW_DSTAT_UNIT_CHECK) &&
-        (p->chars & PMCW_CHARS_MASK_CSENSE)) {
+    if ((schib->scsw.dstat & SCSW_DSTAT_UNIT_CHECK) &&
+        (schib->pmcw.chars & PMCW_CHARS_MASK_CSENSE)) {
          memcpy(sch->sense_data, irb.ecw, sizeof(irb.ecw));
      }
 
#6 lore raw

Re: [Qemu-devel] [PATCH 11/14] hw/vfio/ccw: avoid taking address members in packed structs

In reply to a message not in the archive
Quoted 22 lines · On Fri, 29 Mar 2019 11:11:01 +0000 Daniel P. Berrangé <[email protected]>…

The GCC 9 compiler complains about many places in s390 code that take the address of members of the 'struct SCHIB' which is marked packed:

hw/vfio/ccw.c: In function ‘vfio_ccw_io_notifier_handler’:
hw/vfio/ccw.c:133:15: warning: taking address of packed member of ‘struct SCHIB’ may result in an unaligned pointer value \
[-Waddress-of-packed-member]

  133 |     SCSW *s = &sch->curr_status.scsw;
      |               ^~~~~~~~~~~~~~~~~~~~~~
hw/vfio/ccw.c:134:15: warning: taking address of packed member of ‘struct SCHIB’ may result in an unaligned pointer value \
[-Waddress-of-packed-member]
  134 |     PMCW *p = &sch->curr_status.pmcw;
      |               ^~~~~~~~~~~~~~~~~~~~~~

...snip many more...

Almost all of these are just done for convenience to avoid typing out long variable/field names when referencing struct members. We can get most of this convenience by taking the address of the 'struct SCHIB' instead, avoiding triggering the compiler warnings.

Quoted 8 lines

In a couple of places we copy via a local variable which is a technique already applied elsewhere in s390 code for this problem.

---
 hw/vfio/ccw.c | 42 ++++++++++++++++++++++--------------------
 1 file changed, 22 insertions(+), 20 deletions(-)

I'm currently in the process of queuing this and the other three s390x fixes, but I'm inclined to do so for 4.1 (it feels a bit late in the cycle for 4.0.)

Other opinions?

#7 lore raw

Re: [Qemu-devel] [PATCH 11/14] hw/vfio/ccw: avoid taking address members in packed structs

Quoted 34 lines · Cornelia Huck
Quoted 22 lines · On Fri, 29 Mar 2019 11:11:01 +0000 Daniel P. Berrangé <[email protected]>…

The GCC 9 compiler complains about many places in s390 code that take the address of members of the 'struct SCHIB' which is marked packed:

hw/vfio/ccw.c: In function ‘vfio_ccw_io_notifier_handler’:
hw/vfio/ccw.c:133:15: warning: taking address of packed member of ‘struct SCHIB’ may result in an unaligned pointer value \
[-Waddress-of-packed-member]

  133 |     SCSW *s = &sch->curr_status.scsw;
      |               ^~~~~~~~~~~~~~~~~~~~~~
hw/vfio/ccw.c:134:15: warning: taking address of packed member of ‘struct SCHIB’ may result in an unaligned pointer value \
[-Waddress-of-packed-member]
  134 |     PMCW *p = &sch->curr_status.pmcw;
      |               ^~~~~~~~~~~~~~~~~~~~~~

...snip many more...

Almost all of these are just done for convenience to avoid typing out long variable/field names when referencing struct members. We can get most of this convenience by taking the address of the 'struct SCHIB' instead, avoiding triggering the compiler warnings.

Quoted 8 lines

In a couple of places we copy via a local variable which is a technique already applied elsewhere in s390 code for this problem.

---
 hw/vfio/ccw.c | 42 ++++++++++++++++++++++--------------------
 1 file changed, 22 insertions(+), 20 deletions(-)
Quoted 5 lines

I'm currently in the process of queuing this and the other three s390x fixes, but I'm inclined to do so for 4.1 (it feels a bit late in the cycle for 4.0.)

Other opinions?

IMHO it would be nice to get rid of the compiler warnings for the release. Multiple people reviewed the patches, so I think it should still be fine to include them.

Thomas

#8 lore raw

Re: [Qemu-devel] [PATCH 11/14] hw/vfio/ccw: avoid taking address members in packed structs

Quoted 39 lines · On Tue, 2 Apr 2019 18:05:15 +0200 Thomas Huth <[email protected]> wrote:
Quoted 38 lines · Cornelia Huck
Quoted 22 lines · On Fri, 29 Mar 2019 11:11:01 +0000 Daniel P. Berrangé <[email protected]>…

The GCC 9 compiler complains about many places in s390 code that take the address of members of the 'struct SCHIB' which is marked packed:

hw/vfio/ccw.c: In function ‘vfio_ccw_io_notifier_handler’:
hw/vfio/ccw.c:133:15: warning: taking address of packed member of ‘struct SCHIB’ may result in an unaligned pointer value \
[-Waddress-of-packed-member]

  133 |     SCSW *s = &sch->curr_status.scsw;
      |               ^~~~~~~~~~~~~~~~~~~~~~
hw/vfio/ccw.c:134:15: warning: taking address of packed member of ‘struct SCHIB’ may result in an unaligned pointer value \
[-Waddress-of-packed-member]
  134 |     PMCW *p = &sch->curr_status.pmcw;
      |               ^~~~~~~~~~~~~~~~~~~~~~

...snip many more...

Almost all of these are just done for convenience to avoid typing out long variable/field names when referencing struct members. We can get most of this convenience by taking the address of the 'struct SCHIB' instead, avoiding triggering the compiler warnings.

Quoted 8 lines

In a couple of places we copy via a local variable which is a technique already applied elsewhere in s390 code for this problem.

---
 hw/vfio/ccw.c | 42 ++++++++++++++++++++++--------------------
 1 file changed, 22 insertions(+), 20 deletions(-)  

I'm currently in the process of queuing this and the other three s390x fixes, but I'm inclined to do so for 4.1 (it feels a bit late in the cycle for 4.0.)

Quoted 5 lines
Quoted 1 line

Other opinions?

IMHO it would be nice to get rid of the compiler warnings for the release. Multiple people reviewed the patches, so I think it should still be fine to include them.

Still need to run my smoke tests, but I can send a pull req tomorrow for -rc3.

#9 lore raw

Re: [Qemu-devel] [PATCH 11/14] hw/vfio/ccw: avoid taking address members in packed structs

In reply to Cornelia Huck
Quoted 34 lines · Cornelia Huck
Quoted 22 lines · On Fri, 29 Mar 2019 11:11:01 +0000 Daniel P. Berrangé <[email protected]>…

The GCC 9 compiler complains about many places in s390 code that take the address of members of the 'struct SCHIB' which is marked packed:

hw/vfio/ccw.c: In function ‘vfio_ccw_io_notifier_handler’:
hw/vfio/ccw.c:133:15: warning: taking address of packed member of ‘struct SCHIB’ may result in an unaligned pointer value \
[-Waddress-of-packed-member]

  133 |     SCSW *s = &sch->curr_status.scsw;
      |               ^~~~~~~~~~~~~~~~~~~~~~
hw/vfio/ccw.c:134:15: warning: taking address of packed member of ‘struct SCHIB’ may result in an unaligned pointer value \
[-Waddress-of-packed-member]
  134 |     PMCW *p = &sch->curr_status.pmcw;
      |               ^~~~~~~~~~~~~~~~~~~~~~

...snip many more...

Almost all of these are just done for convenience to avoid typing out long variable/field names when referencing struct members. We can get most of this convenience by taking the address of the 'struct SCHIB' instead, avoiding triggering the compiler warnings.

Quoted 8 lines

In a couple of places we copy via a local variable which is a technique already applied elsewhere in s390 code for this problem.

---
 hw/vfio/ccw.c | 42 ++++++++++++++++++++++--------------------
 1 file changed, 22 insertions(+), 20 deletions(-)
Quoted 5 lines

I'm currently in the process of queuing this and the other three s390x fixes, but I'm inclined to do so for 4.1 (it feels a bit late in the cycle for 4.0.)

Other opinions?

It would be nice to be warning free for 4.0, but I agree that it feels kind of late to be making these changes. They're not fixing real world bugs, and even if you queue the s390 bits we're unlikely to get all the others merged, especially the usb-mtp one is a nasty mess. So we'll not be 100% warning free.

Regards,
Daniel

Signature · 3 lines
|: 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 :|
#10 lore raw

Re: [Qemu-devel] [PATCH 11/14] hw/vfio/ccw: avoid taking address members in packed structs

Quoted 39 lines · On Tue, 2 Apr 2019 17:11:29 +0100 Daniel P. Berrangé <[email protected]>…
Quoted 38 lines · Cornelia Huck
Quoted 22 lines · On Fri, 29 Mar 2019 11:11:01 +0000 Daniel P. Berrangé <[email protected]>…

The GCC 9 compiler complains about many places in s390 code that take the address of members of the 'struct SCHIB' which is marked packed:

hw/vfio/ccw.c: In function ‘vfio_ccw_io_notifier_handler’:
hw/vfio/ccw.c:133:15: warning: taking address of packed member of ‘struct SCHIB’ may result in an unaligned pointer value \
[-Waddress-of-packed-member]

  133 |     SCSW *s = &sch->curr_status.scsw;
      |               ^~~~~~~~~~~~~~~~~~~~~~
hw/vfio/ccw.c:134:15: warning: taking address of packed member of ‘struct SCHIB’ may result in an unaligned pointer value \
[-Waddress-of-packed-member]
  134 |     PMCW *p = &sch->curr_status.pmcw;
      |               ^~~~~~~~~~~~~~~~~~~~~~

...snip many more...

Almost all of these are just done for convenience to avoid typing out long variable/field names when referencing struct members. We can get most of this convenience by taking the address of the 'struct SCHIB' instead, avoiding triggering the compiler warnings.

Quoted 8 lines

In a couple of places we copy via a local variable which is a technique already applied elsewhere in s390 code for this problem.

---
 hw/vfio/ccw.c | 42 ++++++++++++++++++++++--------------------
 1 file changed, 22 insertions(+), 20 deletions(-)  

I'm currently in the process of queuing this and the other three s390x fixes, but I'm inclined to do so for 4.1 (it feels a bit late in the cycle for 4.0.)

Quoted 7 lines
Quoted 1 line

Other opinions?

It would be nice to be warning free for 4.0, but I agree that it feels kind of late to be making these changes. They're not fixing real world bugs, and even if you queue the s390 bits we're unlikely to get all the others merged, especially the usb-mtp one is a nasty mess. So we'll not be 100% warning free.

Yeah, but OTOH, the s390x changes are straightforward and have been reviewed by several people.

So I changed my mind and queued them to s390-fixes :)

A message that is not in the archive (sent off-list or not yet received): [email protected]

Reviewed-by
#12 lore raw

Re: [Qemu-devel] [PATCH 13/14] hw/s390x/ipl: avoid taking address of fields in packed struct

Quoted 57 lines · Daniel P. Berrangé

Compiling with GCC 9 complains

hw/s390x/ipl.c: In function ‘s390_ipl_set_boot_menu’:
hw/s390x/ipl.c:256:25: warning: taking address of packed member of ‘struct QemuIplParameters’ may result in an unaligned pointer value [-Waddress-of-packed-member]

   256 |     uint32_t *timeout = &ipl->qipl.boot_menu_timeout;
       |                         ^~~~~~~~~~~~~~~~~~~~~~~~~~~~

This local variable is only present to save a little bit of typing when setting the field later. Get rid of this to avoid the warning about unaligned accesses.

---
  hw/s390x/ipl.c | 12 +++++-------
  1 file changed, 5 insertions(+), 7 deletions(-)
hw/s390x/ipl.c +3 -5
index 896888bf8f..51b272e190 100644
@@ -252,8 +252,6 @@ static void s390_ipl_set_boot_menu(S390IPLState *ipl)
  {
      QemuOptsList *plist = qemu_find_opts("boot-opts");
      QemuOpts *opts = QTAILQ_FIRST(&plist->head);
-    uint8_t *flags = &ipl->qipl.qipl_flags;
-    uint32_t *timeout = &ipl->qipl.boot_menu_timeout;
      const char *tmp;
      unsigned long splash_time = 0;
 
@@ -269,7 +267,7 @@ static void s390_ipl_set_boot_menu(S390IPLState *ipl)
      case S390_IPL_TYPE_CCW:
          /* In the absence of -boot menu, use zipl parameters */
          if (!qemu_opt_get(opts, "menu")) {
-            *flags |= QIPL_FLAG_BM_OPTS_ZIPL;
+            ipl->qipl.qipl_flags |= QIPL_FLAG_BM_OPTS_ZIPL;
              return;
          }
          break;
@@ -286,23 +284,23 @@ static void s390_ipl_set_boot_menu(S390IPLState *ipl)
          return;
      }
 
-    *flags |= QIPL_FLAG_BM_OPTS_CMD;
+    ipl->qipl.qipl_flags |= QIPL_FLAG_BM_OPTS_CMD;
 
      tmp = qemu_opt_get(opts, "splash-time");
 
      if (tmp && qemu_strtoul(tmp, NULL, 10, &splash_time)) {
          error_report("splash-time is invalid, forcing it to 0");
-        *timeout = 0;
+        ipl->qipl.boot_menu_timeout = 0;
          return;
      }
 
      if (splash_time > 0xffffffff) {
          error_report("splash-time is too large, forcing it to max value");

hw/s390x/ipl.c static void s390_ipl_set_boot_menu(S390IPLState *ipl) · line 306

Quoted 10 lines
hw/s390x/ipl.c +2 -2
-        *timeout = 0xffffffff;
+        ipl->qipl.boot_menu_timeout = 0xffffffff;
          return;
      }
 
-    *timeout = cpu_to_be32(splash_time);
+    ipl->qipl.boot_menu_timeout = cpu_to_be32(splash_time);
  }
 
  static CcwDevice *s390_get_ccw_device(DeviceState *dev_st)

A message that is not in the archive (sent off-list or not yet received): [email protected]

#14 lore raw

Re: [Qemu-devel] [PATCH 08/14] qxl: avoid unaligned pointer reads/writes

Hi

Quoted 6 lines · Daniel P. Berrangé

The SPICE_RING_PROD_ITEM() macro is initializing a local 'uint64_t *' variable to point to the 'el' field inside the QXLReleaseRing struct. This uint64_t field is not guaranteed aligned as the struct is packed.

Quoted 8 lines

Code should not take the address of fields within a packed struct. Changing the SPICE_RING_PROD_ITEM() macro to avoid taking the address of the field is impractical. It is clearer to just remove the macro and inline its functionality in the three call sites that need it.

I didn't notice your patch, I sent a different one which didn't inline as much code:

https://patchew.org/QEMU/[email protected]/

Quoted 124 lines
---
 hw/display/qxl.c | 55 +++++++++++++++++++++---------------------------
 1 file changed, 24 insertions(+), 31 deletions(-)
hw/display/qxl.c +24 -32
index c8ce5781e0..5c38e6e906 100644
@@ -33,24 +33,6 @@
 
 #include "qxl.h"
 
-/*
- * NOTE: SPICE_RING_PROD_ITEM accesses memory on the pci bar and as
- * such can be changed by the guest, so to avoid a guest trigerrable
- * abort we just qxl_set_guest_bug and set the return to NULL. Still
- * it may happen as a result of emulator bug as well.
- */
-#undef SPICE_RING_PROD_ITEM
-#define SPICE_RING_PROD_ITEM(qxl, r, ret) {                             \
-        uint32_t prod = (r)->prod & SPICE_RING_INDEX_MASK(r);           \
-        if (prod >= ARRAY_SIZE((r)->items)) {                           \
-            qxl_set_guest_bug(qxl, "SPICE_RING_PROD_ITEM indices mismatch " \
-                          "%u >= %zu", prod, ARRAY_SIZE((r)->items));   \
-            ret = NULL;                                                 \
-        } else {                                                        \
-            ret = &(r)->items[prod].el;                                 \
-        }                                                               \
-    }
-
 #undef SPICE_RING_CONS_ITEM
 #define SPICE_RING_CONS_ITEM(qxl, r, ret) {                             \
         uint32_t cons = (r)->cons & SPICE_RING_INDEX_MASK(r);           \
@@ -414,7 +396,8 @@ static void init_qxl_rom(PCIQXLDevice *d)
 static void init_qxl_ram(PCIQXLDevice *d)
 {
     uint8_t *buf;
-    uint64_t *item;
+    uint32_t prod;
+    QXLReleaseRing *ring;
 
     buf = d->vga.vram_ptr;
     d->ram = (QXLRam *)(buf + le32_to_cpu(d->shadow_rom.ram_header_offset));
@@ -426,9 +409,12 @@ static void init_qxl_ram(PCIQXLDevice *d)
     SPICE_RING_INIT(&d->ram->cmd_ring);
     SPICE_RING_INIT(&d->ram->cursor_ring);
     SPICE_RING_INIT(&d->ram->release_ring);
-    SPICE_RING_PROD_ITEM(d, &d->ram->release_ring, item);
-    assert(item);
-    *item = 0;
+
+    ring = &d->ram->release_ring;
+    prod = ring->prod & SPICE_RING_INDEX_MASK(ring);
+    assert(prod < ARRAY_SIZE(ring->items));
+    ring->items[prod].el = 0;
+
     qxl_ring_set_dirty(d);
 }
 
@@ -732,7 +718,7 @@ static int interface_req_cmd_notification(QXLInstance *sin)
 static inline void qxl_push_free_res(PCIQXLDevice *d, int flush)
 {
     QXLReleaseRing *ring = &d->ram->release_ring;
-    uint64_t *item;
+    uint32_t prod;
     int notify;
 
 #define QXL_FREE_BUNCH_SIZE 32
@@ -759,11 +745,15 @@ static inline void qxl_push_free_res(PCIQXLDevice *d, int flush)
     if (notify) {
         qxl_send_events(d, QXL_INTERRUPT_DISPLAY);
     }
-    SPICE_RING_PROD_ITEM(d, ring, item);
-    if (!item) {
+
+    ring = &d->ram->release_ring;
+    prod = ring->prod & SPICE_RING_INDEX_MASK(ring);
+    if (prod >= ARRAY_SIZE(ring->items)) {
+        qxl_set_guest_bug(d, "SPICE_RING_PROD_ITEM indices mismatch "
+                          "%u >= %zu", prod, ARRAY_SIZE(ring->items));
         return;
     }
-    *item = 0;
+    ring->items[prod].el = 0;
     d->num_free_res = 0;
     d->last_release = NULL;
     qxl_ring_set_dirty(d);
@@ -775,7 +765,8 @@ static void interface_release_resource(QXLInstance *sin,
 {
     PCIQXLDevice *qxl = container_of(sin, PCIQXLDevice, ssd.qxl);
     QXLReleaseRing *ring;
-    uint64_t *item, id;
+    uint32_t prod;
+    uint64_t id;
 
     if (ext.group_id == MEMSLOT_GROUP_HOST) {
         /* host group -> vga mode update request */
@@ -792,16 +783,18 @@ static void interface_release_resource(QXLInstance *sin,
      * pci bar 0, $command.release_info
      */
     ring = &qxl->ram->release_ring;
-    SPICE_RING_PROD_ITEM(qxl, ring, item);
-    if (!item) {
+    prod = ring->prod & SPICE_RING_INDEX_MASK(ring);
+    if (prod >= ARRAY_SIZE(ring->items)) {
+        qxl_set_guest_bug(qxl, "SPICE_RING_PROD_ITEM indices mismatch "
+                          "%u >= %zu", prod, ARRAY_SIZE(ring->items));
         return;
     }
-    if (*item == 0) {
+    if (ring->items[prod].el == 0) {
         /* stick head into the ring */
         id = ext.info->id;
         ext.info->next = 0;
         qxl_ram_set_dirty(qxl, &ext.info->next);
-        *item = id;
+        ring->items[prod].el = id;
         qxl_ring_set_dirty(qxl);
     } else {
         /* append item to the list */
--

2.20.1

Signature · 1 line
Marc-André Lureau