Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 93B95C433EF for ; Tue, 7 Jun 2022 10:37:09 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S240537AbiFGKhI (ORCPT ); Tue, 7 Jun 2022 06:37:08 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41346 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236574AbiFGKhF (ORCPT ); Tue, 7 Jun 2022 06:37:05 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [IPv6:2604:1380:4641:c500::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 804AFEC3C8 for ; Tue, 7 Jun 2022 03:37:04 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id 28224614F4 for ; Tue, 7 Jun 2022 10:37:04 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 913C3C385A5; Tue, 7 Jun 2022 10:37:02 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1654598223; bh=y6Lwj9r5Vh8YDtEUV48nsmOM+Zw9mDLiGs1H2FyFf5g=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=FLbRk8w+P8//Oca7WjNGQ0iZI4tpCOEA4DI704KgESW4PnToLJjBN5Mq6KIvwXB1M LlG6sK49arSxZYqSSA9+EZTo5QpL7xUCI16/scacWxlb4mnumS5mAgTaCibI2i3X2X q80MyldkBByEaH6F4mY6n3kcruVaKaozNwe/x7zMc3+q/0RqLczNNnmZN0PDwEWztu 3pbJzD2FHx8WnBCxItbExcunQ2zn6NXKM+vYF/lHoI2hlSWpcCVA3VzTQDDznoSUXb x/6xjq6QjpLZ6IRwlAh2ws017Yy9s3HmESWNr+ID2hndR7BngrMezoZH6/4U9x0xyT qsjCSxkQ6hScA== Date: Tue, 7 Jun 2022 11:36:58 +0100 From: Will Deacon To: Andre Przywara Cc: Julien Thierry , Alexandru Elisei , kvm@vger.kernel.org Subject: Re: [PATCH kvmtool 2/4] virtio/mmio: access header members normally Message-ID: <20220607103658.GA32508@willie-the-truck> References: <20220601165138.3135246-1-andre.przywara@arm.com> <20220601165138.3135246-3-andre.przywara@arm.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20220601165138.3135246-3-andre.przywara@arm.com> User-Agent: Mutt/1.10.1 (2018-07-13) Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org On Wed, Jun 01, 2022 at 05:51:36PM +0100, Andre Przywara wrote: > The handlers for accessing the virtio-mmio header tried to be very > clever, by modelling the internal data structure to look exactly like > the protocol header, so that address offsets can "reused". > > This requires using a packed structure, which creates other problems, > and seems to be totally unnecessary in this case. > > Replace the offset-based access hacks to the structure with proper > compiler visible accesses, to avoid unaligned accesses and make the code > more robust. > > This fixes UBSAN complaints about unaligned accesses. > > Signed-off-by: Andre Przywara > --- > include/kvm/virtio-mmio.h | 2 +- > virtio/mmio.c | 19 +++++++++++++++---- > 2 files changed, 16 insertions(+), 5 deletions(-) > > diff --git a/include/kvm/virtio-mmio.h b/include/kvm/virtio-mmio.h > index 13dcccb6..aa4cab3c 100644 > --- a/include/kvm/virtio-mmio.h > +++ b/include/kvm/virtio-mmio.h > @@ -39,7 +39,7 @@ struct virtio_mmio_hdr { > u32 interrupt_ack; > u32 reserved_5[2]; > u32 status; > -} __attribute__((packed)); > +}; Does this mean that the previous patch is no longer required? > > struct virtio_mmio { > u32 addr; > diff --git a/virtio/mmio.c b/virtio/mmio.c > index 3782d55a..c9ad8ee7 100644 > --- a/virtio/mmio.c > +++ b/virtio/mmio.c > @@ -135,12 +135,22 @@ static void virtio_mmio_config_in(struct kvm_cpu *vcpu, > > switch (addr) { > case VIRTIO_MMIO_MAGIC_VALUE: > + memcpy(data, &vmmio->hdr.magic, sizeof(vmmio->hdr.magic)); Hmm, this is a semantic change as we used to treat the magic as a u32 by passing it to ioport__write32(), which would in turn do the swab for big-endian machines. I don't think we should be using raw memcpy() here. Will