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

@ -10,17 +10,18 @@
struct virgl_context; struct virgl_context;
struct memory_region {
size_t offset;
size_t size;
};
/* the layout of a ring in a virgl_resource */ /* the layout of a ring in a virgl_resource */
struct vkr_ring_layout { struct vkr_ring_layout {
size_t head_offset; struct memory_region head;
size_t tail_offset; struct memory_region tail;
size_t status_offset; struct memory_region status;
struct memory_region buffer;
size_t buffer_offset; struct memory_region extra;
size_t buffer_size;
size_t extra_offset;
size_t extra_size;
}; };
static_assert(ATOMIC_INT_LOCK_FREE == 2 && sizeof(atomic_uint) == 4, 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 static bool
validate_ring_layout(const struct vkr_ring_layout *layout, size_t buf_size) validate_ring_layout(const struct vkr_ring_layout *layout, size_t buf_size)
{ {
if (layout->head_offset > buf_size || layout->tail_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->status.offset > buf_size || layout->buffer.offset > buf_size ||
layout->extra_offset > buf_size) layout->extra.offset > buf_size)
return false; return false;
if (sizeof(uint32_t) > buf_size - layout->head_offset || if (sizeof(uint32_t) > buf_size - layout->head.offset ||
sizeof(uint32_t) > buf_size - layout->tail_offset || sizeof(uint32_t) > buf_size - layout->tail.offset ||
sizeof(uint32_t) > buf_size - layout->status_offset || sizeof(uint32_t) > buf_size - layout->status.offset ||
layout->buffer_size > buf_size - layout->buffer_offset || layout->buffer.size > buf_size - layout->buffer.offset ||
layout->extra_size > buf_size - layout->extra_offset) layout->extra.size > buf_size - layout->extra.offset)
return false; 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 false;
return true; return true;
@ -207,13 +207,11 @@ vkr_dispatch_vkCreateRingMESA(struct vn_dispatch_context *dispatch,
size = info->size; size = info->size;
const struct vkr_ring_layout layout = { const struct vkr_ring_layout layout = {
.head_offset = info->headOffset, .head = {info->headOffset, sizeof(uint32_t)},
.tail_offset = info->tailOffset, .tail = {info->tailOffset, sizeof(uint32_t)},
.status_offset = info->statusOffset, .status = {info->statusOffset, sizeof(uint32_t)},
.buffer_offset = info->bufferOffset, .buffer = {info->bufferOffset, info->bufferSize},
.buffer_size = info->bufferSize, .extra = {info->extraOffset, info->extraSize},
.extra_offset = info->extraOffset,
.extra_size = info->extraSize,
}; };
if (!validate_ring_layout(&layout, size)) { if (!validate_ring_layout(&layout, size)) {

Loading…
Cancel
Save