From patchwork Mon Apr 8 20:12:03 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Marc-Andr=C3=A9_Lureau?= X-Patchwork-Id: 1081466 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (mailfrom) smtp.mailfrom=nongnu.org (client-ip=209.51.188.17; helo=lists.gnu.org; envelope-from=qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org; receiver=) Authentication-Results: ozlabs.org; dmarc=fail (p=none dis=none) header.from=redhat.com Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 44dMFd0fsxz9s4Y for ; Tue, 9 Apr 2019 06:18:59 +1000 (AEST) Received: from localhost ([127.0.0.1]:58444 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1hDajI-0003Sh-1M for incoming@patchwork.ozlabs.org; Mon, 08 Apr 2019 16:18:56 -0400 Received: from eggs.gnu.org ([209.51.188.92]:53667) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1hDaiv-0003Sa-0Q for qemu-devel@nongnu.org; Mon, 08 Apr 2019 16:18:34 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1hDait-0006Tj-1m for qemu-devel@nongnu.org; Mon, 08 Apr 2019 16:18:32 -0400 Received: from mx1.redhat.com ([209.132.183.28]:54684) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1hDair-0006Qo-0z for qemu-devel@nongnu.org; Mon, 08 Apr 2019 16:18:29 -0400 Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id C4404308FBB4 for ; Mon, 8 Apr 2019 20:12:09 +0000 (UTC) Received: from localhost (unknown [10.36.112.10]) by smtp.corp.redhat.com (Postfix) with ESMTP id E0A1460472; Mon, 8 Apr 2019 20:12:04 +0000 (UTC) From: =?utf-8?q?Marc-Andr=C3=A9_Lureau?= To: qemu-devel@nongnu.org Date: Mon, 8 Apr 2019 22:12:03 +0200 Message-Id: <20190408201203.28924-1-marcandre.lureau@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.11 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.43]); Mon, 08 Apr 2019 20:12:09 +0000 (UTC) X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 209.132.183.28 Subject: [Qemu-devel] [PATCH] qxl: fix -Waddress-of-packed-member X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: =?utf-8?q?Marc-Andr=C3=A9_Lureau?= , kraxel@redhat.com Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: "Qemu-devel" The GCC9 compiler complains about QXL code that takes the address of members of the 'struct QXLReleaseRing' which is marked packed: CC hw/display/qxl.o /home/elmarco/src/qemu/hw/display/qxl.c: In function ‘init_qxl_ram’: /home/elmarco/src/qemu/hw/display/qxl.c:50:19: warning: taking address of packed member of ‘struct QXLReleaseRing_ring_el’ may result in an unaligned pointer value [-Waddress-of-packed-member] 50 | ret = &(r)->items[prod].el; \ | ^~~~~~~~~~~~~~~~~~~~ /home/elmarco/src/qemu/hw/display/qxl.c:429:5: note: in expansion of macro ‘SPICE_RING_PROD_ITEM’ 429 | SPICE_RING_PROD_ITEM(d, &d->ram->release_ring, item); | ^~~~~~~~~~~~~~~~~~~~ /home/elmarco/src/qemu/hw/display/qxl.c: In function ‘qxl_push_free_res’: /home/elmarco/src/qemu/hw/display/qxl.c:50:19: warning: taking address of packed member of ‘struct QXLReleaseRing_ring_el’ may result in an unaligned pointer value [-Waddress-of-packed-member] 50 | ret = &(r)->items[prod].el; \ | ^~~~~~~~~~~~~~~~~~~~ /home/elmarco/src/qemu/hw/display/qxl.c:762:5: note: in expansion of macro ‘SPICE_RING_PROD_ITEM’ 762 | SPICE_RING_PROD_ITEM(d, ring, item); | ^~~~~~~~~~~~~~~~~~~~ /home/elmarco/src/qemu/hw/display/qxl.c: In function ‘interface_release_resource’: /home/elmarco/src/qemu/hw/display/qxl.c:50:19: warning: taking address of packed member of ‘struct QXLReleaseRing_ring_el’ may result in an unaligned pointer value [-Waddress-of-packed-member] 50 | ret = &(r)->items[prod].el; \ | ^~~~~~~~~~~~~~~~~~~~ /home/elmarco/src/qemu/hw/display/qxl.c:795:5: note: in expansion of macro ‘SPICE_RING_PROD_ITEM’ 795 | SPICE_RING_PROD_ITEM(qxl, ring, item); | ^~~~~~~~~~~~~~~~~~~~ Replace pointer usage by direct structure/array access instead. Signed-off-by: Marc-André Lureau Tested-by: Philippe Mathieu-Daudé --- hw/display/qxl.c | 83 +++++++++++++++++++++++++++++------------------- 1 file changed, 50 insertions(+), 33 deletions(-) diff --git a/hw/display/qxl.c b/hw/display/qxl.c index c8ce5781e0..12d83dd6f1 100644 --- a/hw/display/qxl.c +++ b/hw/display/qxl.c @@ -39,29 +39,49 @@ * 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; \ - } \ +#define SPICE_RING_GET_CHECK(qxl, r, field) ({ \ + field = (r)->field & SPICE_RING_INDEX_MASK(r); \ + bool mismatch = field >= ARRAY_SIZE((r)->items); \ + if (mismatch) { \ + qxl_set_guest_bug(qxl, "SPICE_RING_GET %s indices mismatch " \ + "%u >= %zu", stringify(field), field, \ + ARRAY_SIZE((r)->items)); \ + } \ + !mismatch; \ +}) + +static inline uint64_t +qxl_release_ring_get_prod(PCIQXLDevice *qxl) +{ + struct QXLReleaseRing *ring = &qxl->ram->release_ring; + uint32_t prod; + bool ok = SPICE_RING_GET_CHECK(qxl, ring, prod); + assert(ok); + + return ring->items[prod].el; +} + +static inline bool +qxl_release_ring_set_prod(PCIQXLDevice *qxl, uint64_t val) +{ + struct QXLReleaseRing *ring = &qxl->ram->release_ring; + uint32_t prod; + bool ok = SPICE_RING_GET_CHECK(qxl, ring, prod); + if (ok) { + ring->items[prod].el = val; } + return ok; +} #undef SPICE_RING_CONS_ITEM -#define SPICE_RING_CONS_ITEM(qxl, r, ret) { \ - uint32_t cons = (r)->cons & SPICE_RING_INDEX_MASK(r); \ - if (cons >= ARRAY_SIZE((r)->items)) { \ - qxl_set_guest_bug(qxl, "SPICE_RING_CONS_ITEM indices mismatch " \ - "%u >= %zu", cons, ARRAY_SIZE((r)->items)); \ - ret = NULL; \ - } else { \ - ret = &(r)->items[cons].el; \ - } \ - } +#define SPICE_RING_CONS_ITEM(qxl, r, ret) { \ + uint32_t cons; \ + if (!SPICE_RING_GET_CHECK(qxl, r, cons)) { \ + ret = NULL; \ + } else { \ + ret = &(r)->items[cons].el; \ + } \ +} #undef ALIGN #define ALIGN(a, b) (((a) + ((b) - 1)) & ~((b) - 1)) @@ -414,7 +434,6 @@ static void init_qxl_rom(PCIQXLDevice *d) static void init_qxl_ram(PCIQXLDevice *d) { uint8_t *buf; - uint64_t *item; buf = d->vga.vram_ptr; d->ram = (QXLRam *)(buf + le32_to_cpu(d->shadow_rom.ram_header_offset)); @@ -426,9 +445,9 @@ 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; + if (!qxl_release_ring_set_prod(d, 0)) { + g_assert_not_reached(); + } qxl_ring_set_dirty(d); } @@ -732,7 +751,6 @@ 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; int notify; #define QXL_FREE_BUNCH_SIZE 32 @@ -759,11 +777,9 @@ 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) { + if (!qxl_release_ring_set_prod(d, 0)) { return; } - *item = 0; d->num_free_res = 0; d->last_release = NULL; qxl_ring_set_dirty(d); @@ -775,7 +791,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 +809,16 @@ 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) { + + if (!SPICE_RING_GET_CHECK(qxl, ring, prod)) { return; } - if (*item == 0) { + if (qxl_release_ring_get_prod(qxl) == 0) { /* stick head into the ring */ id = ext.info->id; ext.info->next = 0; qxl_ram_set_dirty(qxl, &ext.info->next); - *item = id; + qxl_release_ring_set_prod(qxl, id); qxl_ring_set_dirty(qxl); } else { /* append item to the list */