From: Marek Vasut Date: Tue, 21 Jul 2020 14:21:14 +0000 (+0200) Subject: etnaviv: Add lock around pending_ctx X-Git-Url: https://git.libre-soc.org/?a=commitdiff_plain;h=60975ebe58ddb05beebbdd3850dc9d866aee5420;p=mesa.git 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: e5cc66dfad0 ("etnaviv: Rework locking") Signed-off-by: Marek Vasut Cc: Reviewed-by: Christian Gmeiner Part-of: --- diff --git a/src/gallium/drivers/etnaviv/etnaviv_context.c b/src/gallium/drivers/etnaviv/etnaviv_context.c index 4c204fc9d0c..ffe2f32ecff 100644 --- a/src/gallium/drivers/etnaviv/etnaviv_context.c +++ b/src/gallium/drivers/etnaviv/etnaviv_context.c @@ -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); } diff --git a/src/gallium/drivers/etnaviv/etnaviv_resource.c b/src/gallium/drivers/etnaviv/etnaviv_resource.c index 1f763c2b683..e2ef97f286f 100644 --- a/src/gallium/drivers/etnaviv/etnaviv_resource.c +++ b/src/gallium/drivers/etnaviv/etnaviv_resource.c @@ -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); } diff --git a/src/gallium/drivers/etnaviv/etnaviv_resource.h b/src/gallium/drivers/etnaviv/etnaviv_resource.h index 25d263db1ec..cb83e891d34 100644 --- a/src/gallium/drivers/etnaviv/etnaviv_resource.h +++ b/src/gallium/drivers/etnaviv/etnaviv_resource.h @@ -96,6 +96,7 @@ struct etna_resource { enum etna_resource_status status; + mtx_t lock; /* Lock to protect pending_ctx */ struct set *pending_ctx; }; diff --git a/src/gallium/drivers/etnaviv/etnaviv_transfer.c b/src/gallium/drivers/etnaviv/etnaviv_transfer.c index 0d0324ec0cb..27f3ebe585b 100644 --- a/src/gallium/drivers/etnaviv/etnaviv_transfer.c +++ b/src/gallium/drivers/etnaviv/etnaviv_transfer.c @@ -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);