vkr: wrap vkr_ring_layout offset+size as memory_region struct

Tracking offset+size together for each ring control variable makes extra
upcoming layout validation simpler.

Signed-off-by: Ryan Neph <ryanneph@google.com>
Reviewed-by: Chia-I Wu <olvaffe@gmail.com>
macos/master
Ryan Neph 3 years ago
parent b70ac3b2dd
commit fe251d992d
  1. 10
      src/venus/vkr_ring.c
  2. 19
      src/venus/vkr_ring.h
  3. 30
      src/venus/vkr_transport.c

@ -68,7 +68,7 @@ vkr_ring_create(const struct vkr_ring_layout *layout,
return NULL;
#define ring_attach_shared(member) \
ring->shared.member = (void *)((uint8_t *)shared + layout->member##_offset)
ring->shared.member = (void *)((uint8_t *)shared + layout->member.offset)
ring_attach_shared(head);
ring_attach_shared(tail);
ring_attach_shared(status);
@ -76,10 +76,10 @@ vkr_ring_create(const struct vkr_ring_layout *layout,
ring_attach_shared(extra);
#undef ring_attach_shared
assert(layout->buffer_size && util_is_power_of_two(layout->buffer_size));
ring->buffer_size = layout->buffer_size;
ring->buffer_mask = layout->buffer_size - 1;
ring->extra_size = layout->extra_size;
assert(layout->buffer.size && util_is_power_of_two(layout->buffer.size));
ring->buffer_size = layout->buffer.size;
ring->buffer_mask = layout->buffer.size - 1;
ring->extra_size = layout->extra.size;
/* we will manage head and status, and we expect them to be 0 initially */
if (*ring->shared.head || *ring->shared.status) {

@ -10,17 +10,18 @@
struct virgl_context;
struct memory_region {
size_t offset;
size_t size;
};
/* the layout of a ring in a virgl_resource */
struct vkr_ring_layout {
size_t head_offset;
size_t tail_offset;
size_t status_offset;
size_t buffer_offset;
size_t buffer_size;
size_t extra_offset;
size_t extra_size;
struct memory_region head;
struct memory_region tail;
struct memory_region status;
struct memory_region buffer;
struct memory_region extra;
};
static_assert(ATOMIC_INT_LOCK_FREE == 2 && sizeof(atomic_uint) == 4,

@ -155,19 +155,19 @@ lookup_ring(struct vkr_context *ctx, uint64_t ring_id)
static bool
validate_ring_layout(const struct vkr_ring_layout *layout, size_t buf_size)
{
if (layout->head_offset > buf_size || layout->tail_offset > buf_size ||
layout->status_offset > buf_size || layout->buffer_offset > buf_size ||
layout->extra_offset > buf_size)
if (layout->head.offset > buf_size || layout->tail.offset > buf_size ||
layout->status.offset > buf_size || layout->buffer.offset > buf_size ||
layout->extra.offset > buf_size)
return false;
if (sizeof(uint32_t) > buf_size - layout->head_offset ||
sizeof(uint32_t) > buf_size - layout->tail_offset ||
sizeof(uint32_t) > buf_size - layout->status_offset ||
layout->buffer_size > buf_size - layout->buffer_offset ||
layout->extra_size > buf_size - layout->extra_offset)
if (sizeof(uint32_t) > buf_size - layout->head.offset ||
sizeof(uint32_t) > buf_size - layout->tail.offset ||
sizeof(uint32_t) > buf_size - layout->status.offset ||
layout->buffer.size > buf_size - layout->buffer.offset ||
layout->extra.size > buf_size - layout->extra.offset)
return false;
if (!layout->buffer_size || !util_is_power_of_two(layout->buffer_size))
if (!layout->buffer.size || !util_is_power_of_two(layout->buffer.size))
return false;
return true;
@ -207,13 +207,11 @@ vkr_dispatch_vkCreateRingMESA(struct vn_dispatch_context *dispatch,
size = info->size;
const struct vkr_ring_layout layout = {
.head_offset = info->headOffset,
.tail_offset = info->tailOffset,
.status_offset = info->statusOffset,
.buffer_offset = info->bufferOffset,
.buffer_size = info->bufferSize,
.extra_offset = info->extraOffset,
.extra_size = info->extraSize,
.head = {info->headOffset, sizeof(uint32_t)},
.tail = {info->tailOffset, sizeof(uint32_t)},
.status = {info->statusOffset, sizeof(uint32_t)},
.buffer = {info->bufferOffset, info->bufferSize},
.extra = {info->extraOffset, info->extraSize},
};
if (!validate_ring_layout(&layout, size)) {

Loading…
Cancel
Save