Module: Mesa
Branch: master
Commit: c7725ad4aa21e3b63fb4a4ca1a2af7a0c87ca55e
URL:    
http://cgit.freedesktop.org/mesa/mesa/commit/?id=c7725ad4aa21e3b63fb4a4ca1a2af7a0c87ca55e

Author: Pierre-Eric Pelloux-Prayer <[email protected]>
Date:   Fri Feb  5 15:31:19 2021 +0100

gallium/u_upload_mgr: lower risk of hitting an assert

The assert(size < INT32_MAX / 2) can be triggered by large uploads.

Since we know that the caller of u_upload_alloc_buffer will consume
min_size bytes, we can init buffer_private_refcount to a much smaller
value.

Reviewed-by: Marek Olšák <[email protected]>
Fixes: ccf9ef36285 ("gallium/u_upload_mgr: eliminate all atomics for the upload 
buffer")
Closes: https://gitlab.freedesktop.org/mesa/mesa/-/issues/4235
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/8880>

---

 src/gallium/auxiliary/util/u_upload_mgr.c | 11 ++++++++---
 1 file changed, 8 insertions(+), 3 deletions(-)

diff --git a/src/gallium/auxiliary/util/u_upload_mgr.c 
b/src/gallium/auxiliary/util/u_upload_mgr.c
index 69622ada5e6..95f434b36be 100644
--- a/src/gallium/auxiliary/util/u_upload_mgr.c
+++ b/src/gallium/auxiliary/util/u_upload_mgr.c
@@ -227,10 +227,14 @@ u_upload_alloc_buffer(struct u_upload_mgr *upload, 
unsigned min_size)
     * before the buffer is unreferenced.
     *
     * This technique can increase CPU performance by 10%.
+    *
+    * The caller of u_upload_alloc_buffer will consume min_size bytes,
+    * so init the buffer_private_refcount to 1 + size - min_size, instead
+    * of size to avoid overflowing reference.count when size is huge.
     */
-   assert(size < INT32_MAX / 2); /* prevent overflows of reference.count */
-   p_atomic_add(&upload->buffer->reference.count, size);
-   upload->buffer_private_refcount = size;
+   upload->buffer_private_refcount = 1 + (size - min_size);
+   assert(upload->buffer_private_refcount < INT32_MAX / 2);
+   p_atomic_add(&upload->buffer->reference.count, 
upload->buffer_private_refcount);
 
    /* Map the new buffer. */
    upload->map = pipe_buffer_map_range(upload->pipe, upload->buffer,
@@ -304,6 +308,7 @@ u_upload_alloc(struct u_upload_mgr *upload,
    if (*outbuf != upload->buffer) {
       pipe_resource_reference(outbuf, NULL);
       *outbuf = upload->buffer;
+      assert (upload->buffer_private_refcount > 0);
       upload->buffer_private_refcount--;
    }
 

_______________________________________________
mesa-commit mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/mesa-commit

Reply via email to