etnaviv: Add lock around pending_ctx

The content of rsc->pending_ctx could be changed from multiple contexts
and thus from multiple threads. The per-context lock is not sufficient
to protect this list. Add per-resource lock to protect this list.

Fixes: e5cc66dfad ("etnaviv: Rework locking")
Signed-off-by: Marek Vasut <marex@denx.de>
Cc: <mesa-stable@lists.freedesktop.org>
Reviewed-by: Christian Gmeiner <christian.gmeiner@gmail.com>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/6454>
This commit is contained in:
Marek Vasut
2020-07-21 16:21:14 +02:00
committed by Marge Bot
parent da660c90bf
commit 60975ebe58
4 changed files with 53 additions and 3 deletions

View File

@@ -93,6 +93,7 @@ etna_context_destroy(struct pipe_context *pctx)
struct etna_context *ctx = etna_context(pctx);
mtx_lock(&ctx->lock);
if (ctx->used_resources_read) {
/*
@@ -103,7 +104,9 @@ etna_context_destroy(struct pipe_context *pctx)
set_foreach(ctx->used_resources_read, entry) {
struct etna_resource *rsc = (struct etna_resource *)entry->key;
mtx_lock(&rsc->lock);
_mesa_set_remove_key(rsc->pending_ctx, ctx);
mtx_unlock(&rsc->lock);
}
_mesa_set_destroy(ctx->used_resources_read, NULL);
@@ -118,7 +121,9 @@ etna_context_destroy(struct pipe_context *pctx)
set_foreach(ctx->used_resources_write, entry) {
struct etna_resource *rsc = (struct etna_resource *)entry->key;
mtx_lock(&rsc->lock);
_mesa_set_remove_key(rsc->pending_ctx, ctx);
mtx_unlock(&rsc->lock);
}
_mesa_set_destroy(ctx->used_resources_write, NULL);
@@ -484,12 +489,16 @@ etna_flush(struct pipe_context *pctx, struct pipe_fence_handle **fence,
struct etna_resource *rsc = (struct etna_resource *)entry->key;
struct pipe_resource *referenced = &rsc->base;
mtx_lock(&rsc->lock);
_mesa_set_remove_key(rsc->pending_ctx, ctx);
/* if resource has no pending ctx's reset its status */
if (_mesa_set_next_entry(rsc->pending_ctx, NULL) == NULL)
rsc->status &= ~ETNA_PENDING_READ;
mtx_unlock(&rsc->lock);
pipe_resource_reference(&referenced, NULL);
}
_mesa_set_clear(ctx->used_resources_read, NULL);
@@ -498,11 +507,13 @@ etna_flush(struct pipe_context *pctx, struct pipe_fence_handle **fence,
struct etna_resource *rsc = (struct etna_resource *)entry->key;
struct pipe_resource *referenced = &rsc->base;
mtx_lock(&rsc->lock);
_mesa_set_remove_key(rsc->pending_ctx, ctx);
/* if resource has no pending ctx's reset its status */
if (_mesa_set_next_entry(rsc->pending_ctx, NULL) == NULL)
rsc->status &= ~ETNA_PENDING_WRITE;
mtx_unlock(&rsc->lock);
pipe_resource_reference(&referenced, NULL);
}

View File

@@ -321,6 +321,7 @@ etna_resource_alloc(struct pipe_screen *pscreen, unsigned layout,
memset(map, 0, size);
}
mtx_init(&rsc->lock, mtx_recursive);
rsc->pending_ctx = _mesa_set_create(NULL, _mesa_hash_pointer,
_mesa_key_pointer_equal);
if (!rsc->pending_ctx)
@@ -463,8 +464,10 @@ etna_resource_destroy(struct pipe_screen *pscreen, struct pipe_resource *prsc)
{
struct etna_resource *rsc = etna_resource(prsc);
mtx_lock(&rsc->lock);
assert(!_mesa_set_next_entry(rsc->pending_ctx, NULL));
_mesa_set_destroy(rsc->pending_ctx, NULL);
mtx_unlock(&rsc->lock);
if (rsc->bo)
etna_bo_del(rsc->bo);
@@ -483,6 +486,8 @@ etna_resource_destroy(struct pipe_screen *pscreen, struct pipe_resource *prsc)
for (unsigned i = 0; i < ETNA_NUM_LOD; i++)
FREE(rsc->levels[i].patch_offsets);
mtx_destroy(&rsc->lock);
FREE(rsc);
}
@@ -559,6 +564,7 @@ etna_resource_from_handle(struct pipe_screen *pscreen,
goto fail;
}
mtx_init(&rsc->lock, mtx_recursive);
rsc->pending_ctx = _mesa_set_create(NULL, _mesa_hash_pointer,
_mesa_key_pointer_equal);
if (!rsc->pending_ctx)
@@ -616,18 +622,41 @@ etna_resource_used(struct etna_context *ctx, struct pipe_resource *prsc,
mtx_lock(&ctx->lock);
rsc = etna_resource(prsc);
again:
mtx_lock(&rsc->lock);
set_foreach(rsc->pending_ctx, entry) {
struct etna_context *extctx = (struct etna_context *)entry->key;
struct pipe_context *pctx = &extctx->base;
bool need_flush = false;
if (mtx_trylock(&extctx->lock) != thrd_success) {
/*
* The other context could be locked in etna_flush() and
* stuck waiting for the resource lock, so release the
* resource lock here, let etna_flush() finish, and try
* again.
*/
mtx_unlock(&rsc->lock);
thrd_yield();
goto again;
}
set_foreach(extctx->used_resources_read, entry2) {
struct etna_resource *rsc2 = (struct etna_resource *)entry2->key;
if (ctx == extctx || rsc2 != rsc)
continue;
if (status & ETNA_PENDING_WRITE)
pctx->flush(pctx, NULL, 0);
if (status & ETNA_PENDING_WRITE) {
need_flush = true;
break;
}
}
if (need_flush) {
pctx->flush(pctx, NULL, 0);
mtx_unlock(&extctx->lock);
continue;
}
set_foreach(extctx->used_resources_write, entry2) {
@@ -635,8 +664,14 @@ etna_resource_used(struct etna_context *ctx, struct pipe_resource *prsc,
if (ctx == extctx || rsc2 != rsc)
continue;
pctx->flush(pctx, NULL, 0);
need_flush = true;
break;
}
if (need_flush)
pctx->flush(pctx, NULL, 0);
mtx_unlock(&extctx->lock);
}
rsc->status = status;
@@ -648,6 +683,7 @@ etna_resource_used(struct etna_context *ctx, struct pipe_resource *prsc,
_mesa_set_add(rsc->pending_ctx, ctx);
}
mtx_unlock(&rsc->lock);
mtx_unlock(&ctx->lock);
}

View File

@@ -96,6 +96,7 @@ struct etna_resource {
enum etna_resource_status status;
mtx_t lock; /* Lock to protect pending_ctx */
struct set *pending_ctx;
};

View File

@@ -391,12 +391,14 @@ etna_transfer_map(struct pipe_context *pctx, struct pipe_resource *prsc,
(!trans->rsc &&
(((usage & PIPE_TRANSFER_READ) && (rsc->status & ETNA_PENDING_WRITE)) ||
((usage & PIPE_TRANSFER_WRITE) && rsc->status)))) {
mtx_lock(&rsc->lock);
set_foreach(rsc->pending_ctx, entry) {
struct etna_context *pend_ctx = (struct etna_context *)entry->key;
struct pipe_context *pend_pctx = &pend_ctx->base;
pend_pctx->flush(pend_pctx, NULL, 0);
}
mtx_unlock(&rsc->lock);
}
mtx_unlock(&ctx->lock);