v3dv: add a refcount mechanism to BOs
authorIago Toral Quiroga <itoral@igalia.com>
Tue, 4 Jan 2022 11:56:42 +0000 (12:56 +0100)
committerMarge Bot <emma+marge@anholt.net>
Wed, 5 Jan 2022 12:22:45 +0000 (12:22 +0000)
commit44fa8304d45e775050a42f2fc85c02ad5ddb63bd
treeda17881f4d60126c09bd706ed1f35c45b5f65718
parent946bd90a097e8bf4f060f7a18d04f1df1c23275f
v3dv: add a refcount mechanism to BOs

Until now we have lived without a refcount mechanism in the driver
because in Vulkan the user is responsible for handling the life
span of memory allocations for all Vulkan objects, however,
imported BOs are tricky because the kernel doesn't refcount
so user-space needs to make sure that:

1. When importing a BO into the same device used to create it
   (self-importing) it does not double free the same BO.
2. Frees imported BOs that were not allocated through the same
   device.

Our initial implementation always freed BOs when requested,
so we handled 2) correctly but not 1) and we would double-free
self-imported BOs. We tried to fix that in commit d809d9f3
but that broke 2) and we started to leak BOs for some imports.

This fixes the problem for good by adding refcounts to BOs
so that self-imported BOs have a refcnt > 1 and are only freed
when all references are freed.

Closes: https://gitlab.freedesktop.org/mesa/mesa/-/issues/5769
Tested-by: Roman Stratiienko <r.stratiienko@gmail.com>
Reviewed-by: Juan A. Suarez <jasuarez@igalia.com>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/14392>
src/broadcom/vulkan/v3dv_bo.c
src/broadcom/vulkan/v3dv_bo.h
src/broadcom/vulkan/v3dv_device.c
src/broadcom/vulkan/v3dv_private.h