Skip to content

Commit

Permalink
virtio-mem: Don't rely on implicit compiler padding for requests
Browse files Browse the repository at this point in the history
The compiler will add padding after the last member, make that explicit.
The size of a request is always 24 bytes. The size of a response always
10 bytes. Add compile-time checks.

Cc: "Michael S. Tsirkin" <mst@redhat.com>
Cc: Pankaj Gupta <pankaj.gupta.linux@gmail.com>
Cc: teawater <teawaterz@linux.alibaba.com>
Signed-off-by: David Hildenbrand <david@redhat.com>
Link: https://lore.kernel.org/r/20200515101402.16597-1-david@redhat.com
Signed-off-by: Michael S. Tsirkin <mst@redhat.com>
  • Loading branch information
David Hildenbrand authored and Michael S. Tsirkin committed Jun 4, 2020
1 parent 72f9525 commit fce8afd
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 0 deletions.
3 changes: 3 additions & 0 deletions drivers/virtio/virtio_mem.c
Original file line number Diff line number Diff line change
Expand Up @@ -1770,6 +1770,9 @@ static int virtio_mem_probe(struct virtio_device *vdev)
struct virtio_mem *vm;
int rc = -EINVAL;

BUILD_BUG_ON(sizeof(struct virtio_mem_req) != 24);
BUILD_BUG_ON(sizeof(struct virtio_mem_resp) != 10);

vdev->priv = vm = kzalloc(sizeof(*vm), GFP_KERNEL);
if (!vm)
return -ENOMEM;
Expand Down
3 changes: 3 additions & 0 deletions include/uapi/linux/virtio_mem.h
Original file line number Diff line number Diff line change
Expand Up @@ -103,16 +103,19 @@
struct virtio_mem_req_plug {
__virtio64 addr;
__virtio16 nb_blocks;
__virtio16 padding[3];
};

struct virtio_mem_req_unplug {
__virtio64 addr;
__virtio16 nb_blocks;
__virtio16 padding[3];
};

struct virtio_mem_req_state {
__virtio64 addr;
__virtio16 nb_blocks;
__virtio16 padding[3];
};

struct virtio_mem_req {
Expand Down

0 comments on commit fce8afd

Please sign in to comment.