Mesa (staging/21.0): mesa/st: Allocate the gl_context with 16-byte alignment.

GitLab Mirror gitlab-mirror at kemper.freedesktop.org
Mon Feb 1 17:12:29 UTC 2021


Module: Mesa
Branch: staging/21.0
Commit: 7bd9982ba126a4fe3579e5a64a0ef5e157c32bed
URL:    http://cgit.freedesktop.org/mesa/mesa/commit/?id=7bd9982ba126a4fe3579e5a64a0ef5e157c32bed

Author: Eric Anholt <eric at anholt.net>
Date:   Fri Jan 29 09:59:34 2021 -0800

mesa/st: Allocate the gl_context with 16-byte alignment.

The _ModelProjectMatrix matrix embedded inside has members inside of it
marked as 16-byte aligned, and so the context also has to be 16-byte
aligned or access to those members would be invalid.  I believe the
compiler used this to use better 16-byte-aligned load/stores to other
members of the context, breaking when the context's alignment was only 8
(as normal mallocs guarantee).

Fixes: 3175b63a0dfa ("mesa: don't allocate matrices with malloc")
Tested-by: Rob Clark <robdclark at chromium.org>
Reviewed-by: Rob Clark <robdclark at chromium.org>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/8783>
(cherry picked from commit 55e853d823e860dd232a8be0378bd0519e35b6bd)

---

 .pick_status.json                   | 2 +-
 src/mesa/state_tracker/st_context.c | 8 +++++---
 2 files changed, 6 insertions(+), 4 deletions(-)

diff --git a/.pick_status.json b/.pick_status.json
index aaf8a9de722..5a70009a789 100644
--- a/.pick_status.json
+++ b/.pick_status.json
@@ -517,7 +517,7 @@
         "description": "mesa/st: Allocate the gl_context with 16-byte alignment.",
         "nominated": true,
         "nomination_type": 1,
-        "resolution": 0,
+        "resolution": 1,
         "master_sha": null,
         "because_sha": "3175b63a0dfa290430f9f7eb651387788933a02b"
     },
diff --git a/src/mesa/state_tracker/st_context.c b/src/mesa/state_tracker/st_context.c
index a035d792ce0..95263ea19ad 100644
--- a/src/mesa/state_tracker/st_context.c
+++ b/src/mesa/state_tracker/st_context.c
@@ -1020,12 +1020,14 @@ st_create_context(gl_api api, struct pipe_context *pipe,
    if (pipe->set_context_param)
       funcs.PinDriverToL3Cache = st_pin_driver_to_l3_cache;
 
-   ctx = calloc(1, sizeof(struct gl_context));
+   /* gl_context must be 16-byte aligned due to the alignment on GLmatrix. */
+   ctx = align_malloc(sizeof(struct gl_context), 16);
    if (!ctx)
       return NULL;
+   memset(ctx, 0, sizeof(*ctx));
 
    if (!_mesa_initialize_context(ctx, api, visual, shareCtx, &funcs)) {
-      free(ctx);
+      align_free(ctx);
       return NULL;
    }
 
@@ -1158,7 +1160,7 @@ st_destroy_context(struct st_context *st)
 
    _mesa_destroy_debug_output(ctx);
 
-   free(ctx);
+   align_free(ctx);
 
    if (save_ctx == ctx) {
       /* unbind the context we just deleted */



More information about the mesa-commit mailing list