Skip to contentSkip to conversation

RFC: v2 set/get scratch page contents

PATCH New [email protected] 1 participant 4 messages Dec 8, 2017

lore
Share & tools
lore
#0 lore raw

cover RFC: v2 set/get scratch page contents

From: Kevin Rogovin <[email protected]>

This patch series for Mesa defines and uses a new ioctl interface to get and set the contents of the scratch page for a PPGTT. The purpose of checking that the scratch page is not changed is to help detect out-of-bound buffer object writes by the GPU.

v2:
- Correctly change to using GEM_CONTEXT_GET/SETPARAM and thus make scratch page get/set as per-context
- place checking of scratch page in same location if Mesa/i965 as syncing

Kevin Rogovin (3):
drm-uapi:i915 define set/get (per-context) scratch page ioctl
interface
i965: define gen_debug for checking (per-context) scratch page
i965: implement (per-context) scratch page checking

 include/drm-uapi/i915_drm.h                   |  9 ++++++++
 src/intel/common/gen_debug.c                  |  1 +
 src/intel/common/gen_debug.h                  |  1 +
 src/mesa/drivers/dri/i965/brw_context.c       | 30 +++++++++++++++++++++++++++
 src/mesa/drivers/dri/i965/brw_context.h       | 10 +++++++++
 src/mesa/drivers/dri/i965/intel_batchbuffer.c | 15 +++++++++++++-
 6 files changed, 65 insertions(+), 1 deletion(-)
Signature · 6 lines
2.15.0

_______________________________________________
Intel-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/intel-gfx
#1 lore raw

PATCH 1/3 drm-uapi:i915 define set/get (per-context) scratch page ioctl interface

From: Kevin Rogovin <[email protected]>

---
 include/drm-uapi/i915_drm.h | 9 +++++++++
 1 file changed, 9 insertions(+)
include/drm-uapi/i915_drm.h +9 -0
index 7f28eea403..e75eea058b 100644
@@ -1412,6 +1412,15 @@ struct drm_i915_gem_context_param {
 #define   I915_CONTEXT_MAX_USER_PRIORITY	1023 /* inclusive */
 #define   I915_CONTEXT_DEFAULT_PRIORITY		0
 #define   I915_CONTEXT_MIN_USER_PRIORITY	-1023 /* inclusive */
+/* Notes for set/get of I915_CONTEXT_PARAM_SCRATCH_PAGE_CONTENTS:
+ *  1) for the duration of set/get, the caller must guarantee
+ *     that nothing can read/write the scratch page
+ *  2) on set and get, ioctl will write to drm_i915_gem_context_param::set
+ *     the size of the scratch page
+ *  3) on get, first drm_i915_gem_context_param::size of the scratch
+ *     page contents will be written to.
+ */
+#define I915_CONTEXT_PARAM_SCRATCH_PAGE_CONTENTS 0x7
 	__u64 value;
 };
 
Signature · 6 lines
2.15.0

_______________________________________________
Intel-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/intel-gfx
#2 lore raw

PATCH 2/3 i965: define gen_debug for checking (per-context) scratch page

In reply to [email protected]

From: Kevin Rogovin <[email protected]>

---
 src/intel/common/gen_debug.c | 1 +
 src/intel/common/gen_debug.h | 1 +
 2 files changed, 2 insertions(+)
src/intel/common/gen_debug.c +1 -0
index a978f2f581..b8bb8b1395 100644
@@ -85,6 +85,7 @@ static const struct debug_control debug_control[] = {
    { "nohiz",       DEBUG_NO_HIZ },
    { "color",       DEBUG_COLOR },
    { "reemit",      DEBUG_REEMIT },
+   { "check_scratch", DEBUG_CHECK_SCRATCH_PAGE },
    { NULL,    0 }
 };
 
src/intel/common/gen_debug.h +1 -0
index da5b5a569d..c1532d951e 100644
@@ -83,6 +83,7 @@ extern uint64_t INTEL_DEBUG;
 #define DEBUG_NO_HIZ              (1ull << 39)
 #define DEBUG_COLOR               (1ull << 40)
 #define DEBUG_REEMIT              (1ull << 41)
+#define DEBUG_CHECK_SCRATCH_PAGE  (1ull << 42)
 
 #ifdef HAVE_ANDROID_PLATFORM
 #define LOG_TAG "INTEL-MESA"
Signature · 6 lines
2.15.0

_______________________________________________
Intel-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/intel-gfx
#3 lore raw

PATCH 3/3 i965: implement (per-context) scratch page checking

In reply to [email protected]

From: Kevin Rogovin <[email protected]>

---
 src/mesa/drivers/dri/i965/brw_context.c       | 30 +++++++++++++++++++++++++++
 src/mesa/drivers/dri/i965/brw_context.h       | 10 +++++++++
 src/mesa/drivers/dri/i965/intel_batchbuffer.c | 15 +++++++++++++-
 3 files changed, 54 insertions(+), 1 deletion(-)
src/mesa/drivers/dri/i965/brw_context.c +30 -0
index 126c187f62..8e1afdc859 100644
@@ -844,6 +844,7 @@ brwCreateContext(gl_api api,
 {
    struct gl_context *shareCtx = (struct gl_context *) sharedContextPrivate;
    struct intel_screen *screen = driContextPriv->driScreenPriv->driverPrivate;
+   __DRIscreen *dri_screen = screen->driScrnPriv;
    const struct gen_device_info *devinfo = &screen->devinfo;
    struct dd_function_table functions;
 
@@ -1078,6 +1079,30 @@ brwCreateContext(gl_api api,
 
    brw_disk_cache_init(brw);
 
+   brw->scratch.size = 0;
+   if (brw->hw_ctx && (INTEL_DEBUG & DEBUG_CHECK_SCRATCH_PAGE)) {
+      struct drm_i915_gem_context_param p;
+      int ret;
+
+      p.ctx_id = brw->hw_ctx;
+      p.size = 0;
+      p.param = I915_CONTEXT_PARAM_SCRATCH_PAGE_CONTENTS;
+      p.value = 0;
+      ret = drmIoctl(dri_screen->fd, DRM_IOCTL_I915_GEM_CONTEXT_GETPARAM, &p);
+      if (ret == 0 && p.size > 0) {
+         brw->scratch.size = p.size;
+         brw->scratch.values = calloc(brw->scratch.size, 1);
+         brw->scratch.tmp = calloc(brw->scratch.size, 1);
+         for (uint64_t i = 0; i < brw->scratch.size; ++i) {
+            brw->scratch.values[i] = rand() & 0xFF;
+         }
+         p.value = (__u64) (uintptr_t) brw->scratch.values;
+         ret = drmIoctl(dri_screen->fd, DRM_IOCTL_I915_GEM_CONTEXT_SETPARAM, &p);
+         assert(ret == 0);
+         assert(p.size == brw->scratch.size);
+      }
+   }
+
    return true;
 }
 
@@ -1140,6 +1165,11 @@ intelDestroyContext(__DRIcontext * driContextPriv)
 
    driDestroyOptionCache(&brw->optionCache);
 
+   if (brw->scratch.size > 0) {
+      free(brw->scratch.values);
+      free(brw->scratch.tmp);
+   }
+
    /* free the Mesa context */
    _mesa_free_context_data(&brw->ctx);
 
src/mesa/drivers/dri/i965/brw_context.h +10 -0
index 0f0aad8534..e73eab9160 100644
@@ -1263,6 +1263,16 @@ struct brw_context
     */
    bool draw_aux_buffer_disabled[MAX_DRAW_BUFFERS];
 
+   /* Checking the scratch page to detect out-of-bounds writes
+    * by the GPU; a zero value on the scratch size indicates
+    * that scratch page checking is not enabled.
+    */
+   struct {
+      uint8_t *values;
+      uint8_t *tmp;
+      uint64_t size;
+   } scratch;
+
    __DRIcontext *driContext;
    struct intel_screen *screen;
 };
src/mesa/drivers/dri/i965/intel_batchbuffer.c +14 -1
index 91a6506a89..2e4e7747d0 100644
@@ -936,9 +936,22 @@ _intel_batchbuffer_flush_fence(struct brw_context *brw,
 
    ret = submit_batch(brw, in_fence_fd, out_fence_fd);
 
-   if (unlikely(INTEL_DEBUG & DEBUG_SYNC)) {
+   if (unlikely((INTEL_DEBUG & DEBUG_SYNC) || brw->scratch.size > 0)) {
       fprintf(stderr, "waiting for idle\n");
       brw_bo_wait_rendering(brw->batch.batch.bo);
+      if (brw->scratch.size > 0) {
+         struct drm_i915_gem_context_param p;
+         int ret;
+
+         p.ctx_id = brw->hw_ctx;
+         p.size = brw->scratch.size;
+         p.param = I915_CONTEXT_PARAM_SCRATCH_PAGE_CONTENTS;
+         p.value = (__u64) (uintptr_t) brw->scratch.tmp;
+         ret = drmIoctl(brw->screen->driScrnPriv->fd, DRM_IOCTL_I915_GEM_CONTEXT_GETPARAM, &p);
+         assert(ret == 0);
+         assert(p.size == brw->scratch.size);
+         assert(memcmp(brw->scratch.tmp, brw->scratch.values, brw->scratch.size) == 0);
+      }
    }
 
    /* Start a new batch buffer. */
Signature · 6 lines
2.15.0

_______________________________________________
Intel-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/intel-gfx