[Beignet] [PATCH 1/2] runtime/driver: refine error handlings.

Zhigang Gong zhigang.gong at intel.com
Thu Jul 10 19:18:10 PDT 2014


We should always check whether a dri_bo_map success or fail.

Signed-off-by: Zhigang Gong <zhigang.gong at intel.com>
---
 src/cl_command_queue_gen7.c   | 52 +++++++++++++++++++++++++++++-------------
 src/cl_driver.h               | 12 +++++-----
 src/intel/intel_batchbuffer.c | 12 ++++++----
 src/intel/intel_batchbuffer.h |  2 +-
 src/intel/intel_gpgpu.c       | 53 ++++++++++++++++++++++++++++++++-----------
 5 files changed, 91 insertions(+), 40 deletions(-)

diff --git a/src/cl_command_queue_gen7.c b/src/cl_command_queue_gen7.c
index 5b80d74..58ecc5a 100644
--- a/src/cl_command_queue_gen7.c
+++ b/src/cl_command_queue_gen7.c
@@ -96,7 +96,7 @@ error:
   return err;
 }
 
-static void
+static int
 cl_upload_constant_buffer(cl_command_queue queue, cl_kernel ker)
 {
   /* calculate constant buffer size
@@ -125,11 +125,15 @@ cl_upload_constant_buffer(cl_command_queue queue, cl_kernel ker)
     }
   }
   if(raw_size == 0)
-     return;
+     return 0;
 
   cl_buffer bo = cl_gpgpu_alloc_constant_buffer(gpgpu, aligned_size);
+  if (bo == NULL)
+    return -1;
   cl_buffer_map(bo, 1);
   char * cst_addr = cl_buffer_get_virtual(bo);
+  if (cst_addr == NULL)
+    return -1;
 
   /* upload the global constant data */
   if (global_const_size > 0) {
@@ -162,6 +166,7 @@ cl_upload_constant_buffer(cl_command_queue queue, cl_kernel ker)
     }
   }
   cl_buffer_unmap(bo);
+  return 0;
 }
 
 /* Will return the total amount of slm used */
@@ -254,19 +259,24 @@ cl_bind_stack(cl_gpgpu gpgpu, cl_kernel ker)
   cl_gpgpu_set_stack(gpgpu, offset, stack_sz, cl_gpgpu_get_cache_ctrl());
 }
 
-static void
+static int
 cl_bind_printf(cl_gpgpu gpgpu, cl_kernel ker, void* printf_info, int printf_num, size_t global_sz) {
   int32_t value = GBE_CURBE_PRINTF_INDEX_POINTER;
   int32_t offset = interp_kernel_get_curbe_offset(ker->opaque, value, 0);
   size_t buf_size = global_sz * sizeof(int) * printf_num;
-  if (offset > 0)
-    cl_gpgpu_set_printf_buffer(gpgpu, 0, buf_size, offset);
+  if (offset > 0) {
+    if (cl_gpgpu_set_printf_buffer(gpgpu, 0, buf_size, offset) != 0)
+      return -1;
+  }
 
   value = GBE_CURBE_PRINTF_BUF_POINTER;
   offset = interp_kernel_get_curbe_offset(ker->opaque, value, 0);
   buf_size = interp_get_printf_sizeof_size(printf_info) * global_sz;
-  if (offset > 0)
-    cl_gpgpu_set_printf_buffer(gpgpu, 1, buf_size, offset);
+  if (offset > 0) {
+    if (cl_gpgpu_set_printf_buffer(gpgpu, 1, buf_size, offset) != 0)
+      return -1;
+  }
+  return 0;
 }
 
 LOCAL cl_int
@@ -322,13 +332,15 @@ cl_command_queue_ND_range_gen7(cl_command_queue queue,
 
   /* Setup the kernel */
   if (queue->props & CL_QUEUE_PROFILING_ENABLE)
-    cl_gpgpu_state_init(gpgpu, ctx->device->max_compute_unit * ctx->device->max_thread_per_unit, cst_sz / 32, 1);
+    err = cl_gpgpu_state_init(gpgpu, ctx->device->max_compute_unit * ctx->device->max_thread_per_unit, cst_sz / 32, 1);
   else
-    cl_gpgpu_state_init(gpgpu, ctx->device->max_compute_unit * ctx->device->max_thread_per_unit, cst_sz / 32, 0);
-
+    err = cl_gpgpu_state_init(gpgpu, ctx->device->max_compute_unit * ctx->device->max_thread_per_unit, cst_sz / 32, 0);
+  if (err != 0)
+    goto error;
   printf_num = interp_get_printf_num(printf_info);
   if (printf_num) {
-    cl_bind_printf(gpgpu, ker, printf_info, printf_num, global_size);
+    if (cl_bind_printf(gpgpu, ker, printf_info, printf_num, global_size) != 0)
+      goto error;
   }
 
   /* Bind user buffers */
@@ -338,12 +350,14 @@ cl_command_queue_ND_range_gen7(cl_command_queue queue,
   /* Bind all samplers */
   cl_gpgpu_bind_sampler(gpgpu, ker->samplers, ker->sampler_sz);
 
-  cl_gpgpu_set_scratch(gpgpu, scratch_sz);
+  if (cl_gpgpu_set_scratch(gpgpu, scratch_sz) != 0)
+    goto error;
 
   /* Bind a stack if needed */
   cl_bind_stack(gpgpu, ker);
 
-  cl_upload_constant_buffer(queue, ker);
+  if (cl_upload_constant_buffer(queue, ker) != 0)
+    goto error;
 
   cl_gpgpu_states_setup(gpgpu, &kernel);
 
@@ -355,12 +369,14 @@ cl_command_queue_ND_range_gen7(cl_command_queue queue,
         memcpy(final_curbe + cst_sz * i, ker->curbe, cst_sz);
     }
     TRY (cl_set_varying_payload, ker, final_curbe, local_wk_sz, simd_sz, cst_sz, thread_n);
-    cl_gpgpu_upload_curbes(gpgpu, final_curbe, thread_n*cst_sz);
+    if (cl_gpgpu_upload_curbes(gpgpu, final_curbe, thread_n*cst_sz) != 0)
+      goto error;
   }
 
   /* Start a new batch buffer */
   batch_sz = cl_kernel_compute_batch_sz(ker);
-  cl_gpgpu_batch_reset(gpgpu, batch_sz);
+  if (cl_gpgpu_batch_reset(gpgpu, batch_sz) != 0)
+    goto error;
   cl_set_thread_batch_buf(queue, cl_gpgpu_ref_batch_buf(gpgpu));
   cl_gpgpu_batch_start(gpgpu);
 
@@ -369,7 +385,11 @@ cl_command_queue_ND_range_gen7(cl_command_queue queue,
 
   /* Close the batch buffer and submit it */
   cl_gpgpu_batch_end(gpgpu, 0);
+  return CL_SUCCESS;
+
 error:
-  return err;
+  fprintf(stderr, "error occured. \n");
+  exit(-1);
+  return CL_OUT_OF_RESOURCES;
 }
 
diff --git a/src/cl_driver.h b/src/cl_driver.h
index 3d1d8d8..374813a 100644
--- a/src/cl_driver.h
+++ b/src/cl_driver.h
@@ -143,11 +143,11 @@ typedef void (cl_gpgpu_set_stack_cb)(cl_gpgpu, uint32_t offset, uint32_t size, u
 extern cl_gpgpu_set_stack_cb *cl_gpgpu_set_stack;
 
 /* Setup scratch */
-typedef void (cl_gpgpu_set_scratch_cb)(cl_gpgpu, uint32_t per_thread_size);
+typedef int (cl_gpgpu_set_scratch_cb)(cl_gpgpu, uint32_t per_thread_size);
 extern cl_gpgpu_set_scratch_cb *cl_gpgpu_set_scratch;
 
 /* Configure internal state */
-typedef void (cl_gpgpu_state_init_cb)(cl_gpgpu, uint32_t max_threads, uint32_t size_cs_entry, int profiling);
+typedef int (cl_gpgpu_state_init_cb)(cl_gpgpu, uint32_t max_threads, uint32_t size_cs_entry, int profiling);
 extern cl_gpgpu_state_init_cb *cl_gpgpu_state_init;
 
 /* Set the buffer object where to report performance counters */
@@ -155,7 +155,7 @@ typedef void (cl_gpgpu_set_perf_counters_cb)(cl_gpgpu, cl_buffer perf);
 extern cl_gpgpu_set_perf_counters_cb *cl_gpgpu_set_perf_counters;
 
 /* Fills current curbe buffer with data */
-typedef void (cl_gpgpu_upload_curbes_cb)(cl_gpgpu, const void* data, uint32_t size);
+typedef int (cl_gpgpu_upload_curbes_cb)(cl_gpgpu, const void* data, uint32_t size);
 extern cl_gpgpu_upload_curbes_cb *cl_gpgpu_upload_curbes;
 
 typedef cl_buffer (cl_gpgpu_alloc_constant_buffer_cb)(cl_gpgpu, uint32_t size);
@@ -174,7 +174,7 @@ typedef void (cl_gpgpu_set_sampler_cb)(cl_gpgpu, uint32_t index, uint32_t non_no
 extern cl_gpgpu_set_sampler_cb *cl_gpgpu_set_sampler;
 
 /* Allocate the batch buffer and return the BO used for the batch buffer */
-typedef void (cl_gpgpu_batch_reset_cb)(cl_gpgpu, size_t sz);
+typedef int (cl_gpgpu_batch_reset_cb)(cl_gpgpu, size_t sz);
 extern cl_gpgpu_batch_reset_cb *cl_gpgpu_batch_reset;
 
 /* Atomic begin, pipeline select, urb, pipeline state and constant buffer */
@@ -226,7 +226,7 @@ typedef void (cl_gpgpu_unref_batch_buf_cb)(void*);
 extern cl_gpgpu_unref_batch_buf_cb *cl_gpgpu_unref_batch_buf;
 
 /* Set the printf buffer */
-typedef void (cl_gpgpu_set_printf_buffer_cb)(cl_gpgpu, uint32_t, uint32_t, uint32_t);
+typedef int (cl_gpgpu_set_printf_buffer_cb)(cl_gpgpu, uint32_t, uint32_t, uint32_t);
 extern cl_gpgpu_set_printf_buffer_cb *cl_gpgpu_set_printf_buffer;
 
 /* get the printf buffer offset in the apeture*/
@@ -246,7 +246,7 @@ typedef unsigned long (cl_gpgpu_release_printf_buffer_cb)(cl_gpgpu, uint32_t);
 extern cl_gpgpu_release_printf_buffer_cb *cl_gpgpu_release_printf_buffer;
 
 /* Set the last printfset pointer */
-typedef void (cl_gpgpu_set_printf_info_cb)(cl_gpgpu, void *, size_t*);
+typedef int (cl_gpgpu_set_printf_info_cb)(cl_gpgpu, void *, size_t*);
 extern cl_gpgpu_set_printf_info_cb *cl_gpgpu_set_printf_info;
 
 /* Get the last printfset pointer */
diff --git a/src/intel/intel_batchbuffer.c b/src/intel/intel_batchbuffer.c
index 7767db3..d3da3cc 100644
--- a/src/intel/intel_batchbuffer.c
+++ b/src/intel/intel_batchbuffer.c
@@ -53,7 +53,7 @@
 #include <string.h>
 #include <assert.h>
 
-LOCAL void
+LOCAL int
 intel_batchbuffer_reset(intel_batchbuffer_t *batch, size_t sz)
 {
   if (batch->buffer != NULL) {
@@ -66,15 +66,19 @@ intel_batchbuffer_reset(intel_batchbuffer_t *batch, size_t sz)
                                "batch buffer",
                                sz,
                                64);
-  assert(batch->buffer);
-
-  dri_bo_map(batch->buffer, 1);
+  if (!batch->buffer || (dri_bo_map(batch->buffer, 1) != 0)) {
+    if (batch->buffer)
+      dri_bo_unreference(batch->buffer);
+    batch->buffer = NULL;
+    return -1;
+  }
   batch->map = (uint8_t*) batch->buffer->virtual;
   batch->size = sz;
   batch->ptr = batch->map;
   batch->atomic = 0;
   batch->last_bo = batch->buffer;
   batch->enable_slm = 0;
+  return 0;
 }
 
 LOCAL void
diff --git a/src/intel/intel_batchbuffer.h b/src/intel/intel_batchbuffer.h
index 0c3bc13..4c28a7c 100644
--- a/src/intel/intel_batchbuffer.h
+++ b/src/intel/intel_batchbuffer.h
@@ -100,7 +100,7 @@ extern void intel_batchbuffer_emit_mi_flush(intel_batchbuffer_t*);
 extern void intel_batchbuffer_init(intel_batchbuffer_t*, struct intel_driver*);
 extern void intel_batchbuffer_terminate(intel_batchbuffer_t*);
 extern void intel_batchbuffer_flush(intel_batchbuffer_t*);
-extern void intel_batchbuffer_reset(intel_batchbuffer_t*, size_t sz);
+extern int intel_batchbuffer_reset(intel_batchbuffer_t*, size_t sz);
 
 static INLINE uint32_t
 intel_batchbuffer_space(const intel_batchbuffer_t *batch)
diff --git a/src/intel/intel_gpgpu.c b/src/intel/intel_gpgpu.c
index d00bc83..174baba 100644
--- a/src/intel/intel_gpgpu.c
+++ b/src/intel/intel_gpgpu.c
@@ -28,6 +28,7 @@
 #include <sys/stat.h>
 #include <fcntl.h>
 #include <stddef.h>
+#include <errno.h>
 
 #include "intel/intel_gpgpu.h"
 #include "intel/intel_defines.h"
@@ -553,10 +554,10 @@ intel_gpgpu_batch_end(intel_gpgpu_t *gpgpu, int32_t flush_mode)
   intel_batchbuffer_end_atomic(gpgpu->batch);
 }
 
-static void
+static int
 intel_gpgpu_batch_reset(intel_gpgpu_t *gpgpu, size_t sz)
 {
-  intel_batchbuffer_reset(gpgpu->batch, sz);
+  return intel_batchbuffer_reset(gpgpu->batch, sz);
 }
 /* check we do not get a 0 starting address for binded buf */
 static void
@@ -584,7 +585,7 @@ intel_gpgpu_flush(intel_gpgpu_t *gpgpu)
   intel_gpgpu_check_binded_buf_address(gpgpu);
 }
 
-static void
+static int
 intel_gpgpu_state_init(intel_gpgpu_t *gpgpu,
                        uint32_t max_threads,
                        uint32_t size_cs_entry,
@@ -616,8 +617,9 @@ intel_gpgpu_state_init(intel_gpgpu_t *gpgpu,
   gpgpu->time_stamp_b.bo = NULL;
   if (profiling) {
     bo = dri_bo_alloc(gpgpu->drv->bufmgr, "timestamp query", 4096, 4096);
-    assert(bo);
     gpgpu->time_stamp_b.bo = bo;
+    if (!bo)
+      fprintf(stderr, "Could not allocate buffer for profiling.\n");
   }
 
   /* stack */
@@ -629,6 +631,7 @@ intel_gpgpu_state_init(intel_gpgpu_t *gpgpu,
   uint32_t size_aux = 0;
   if(gpgpu->aux_buf.bo)
     dri_bo_unreference(gpgpu->aux_buf.bo);
+  gpgpu->aux_buf.bo = NULL;
 
   //surface heap must be 4096 bytes aligned because state base address use 20bit for the address
   size_aux = ALIGN(size_aux, 4096);
@@ -656,10 +659,18 @@ intel_gpgpu_state_init(intel_gpgpu_t *gpgpu,
   size_aux += GEN_MAX_SAMPLERS * sizeof(gen7_sampler_border_color_t);
 
   bo = dri_bo_alloc(gpgpu->drv->bufmgr, "AUX_BUFFER", size_aux, 0);
-  assert(bo);
-  dri_bo_map(bo, 1);
+  if (!bo || dri_bo_map(bo, 1) != 0) {
+    fprintf(stderr, "%s:%d: %s.\n", __FILE__, __LINE__, strerror(errno));
+    if (bo)
+      dri_bo_unreference(bo);
+    if (profiling && gpgpu->time_stamp_b.bo)
+      dri_bo_unreference(gpgpu->time_stamp_b.bo);
+    gpgpu->time_stamp_b.bo = NULL;
+    return -1;
+  }
   memset(bo->virtual, 0, size_aux);
   gpgpu->aux_buf.bo = bo;
+  return 0;
 }
 
 static void
@@ -698,7 +709,8 @@ intel_gpgpu_alloc_constant_buffer(intel_gpgpu_t *gpgpu, uint32_t size)
   if(gpgpu->constant_b.bo)
     dri_bo_unreference(gpgpu->constant_b.bo);
   gpgpu->constant_b.bo = drm_intel_bo_alloc(gpgpu->drv->bufmgr, "CONSTANT_BUFFER", s, 64);
-  assert(gpgpu->constant_b.bo);
+  if (gpgpu->constant_b.bo == NULL)
+    return NULL;
   ss2->ss1.base_addr = gpgpu->constant_b.bo->offset;
   dri_bo_emit_reloc(gpgpu->aux_buf.bo,
                       I915_GEM_DOMAIN_RENDER,
@@ -882,7 +894,7 @@ intel_gpgpu_bind_buf(intel_gpgpu_t *gpgpu, drm_intel_bo *buf, uint32_t offset,
   gpgpu->binded_n++;
 }
 
-static void
+static int
 intel_gpgpu_set_scratch(intel_gpgpu_t * gpgpu, uint32_t per_thread_size)
 {
   drm_intel_bufmgr *bufmgr = gpgpu->drv->bufmgr;
@@ -899,8 +911,12 @@ intel_gpgpu_set_scratch(intel_gpgpu_t * gpgpu, uint32_t per_thread_size)
     old = NULL;
   }
 
-  if(!old)
+  if(!old && total) {
     gpgpu->scratch_b.bo = drm_intel_bo_alloc(bufmgr, "SCRATCH_BO", total, 4096);
+    if (gpgpu->scratch_b.bo == NULL)
+      return -1;
+  }
+  return 0;
 }
 static void
 intel_gpgpu_set_stack(intel_gpgpu_t *gpgpu, uint32_t offset, uint32_t size, uint32_t cchint)
@@ -966,7 +982,7 @@ intel_gpgpu_build_idrt(intel_gpgpu_t *gpgpu, cl_gpgpu_kernel *kernel)
                     gpgpu->aux_buf.bo);
 }
 
-static void
+static int
 intel_gpgpu_upload_curbes(intel_gpgpu_t *gpgpu, const void* data, uint32_t size)
 {
   unsigned char *curbe = NULL;
@@ -974,7 +990,10 @@ intel_gpgpu_upload_curbes(intel_gpgpu_t *gpgpu, const void* data, uint32_t size)
   uint32_t i, j;
 
   /* Upload the data first */
-  dri_bo_map(gpgpu->aux_buf.bo, 1);
+  if (dri_bo_map(gpgpu->aux_buf.bo, 1) != 0) {
+    fprintf(stderr, "%s:%d: %s.\n", __FILE__, __LINE__, strerror(errno));
+    return -1;
+  }
   assert(gpgpu->aux_buf.bo->virtual);
   curbe = (unsigned char *) (gpgpu->aux_buf.bo->virtual + gpgpu->aux_offset.curbe_offset);
   memcpy(curbe, data, size);
@@ -991,6 +1010,7 @@ intel_gpgpu_upload_curbes(intel_gpgpu_t *gpgpu, const void* data, uint32_t size)
                               I915_GEM_DOMAIN_RENDER);
     }
   dri_bo_unmap(gpgpu->aux_buf.bo);
+  return 0;
 }
 
 static void
@@ -1294,7 +1314,7 @@ intel_gpgpu_event_get_exec_timestamp(intel_gpgpu_t* gpgpu, intel_event_t *event,
   drm_intel_gem_bo_unmap_gtt(event->ts_buf);
 }
 
-static void
+static int
 intel_gpgpu_set_printf_buf(intel_gpgpu_t *gpgpu, uint32_t i, uint32_t size, uint32_t offset)
 {
   drm_intel_bo *bo = NULL;
@@ -1311,11 +1331,18 @@ intel_gpgpu_set_printf_buf(intel_gpgpu_t *gpgpu, uint32_t i, uint32_t size, uint
   } else
     assert(0);
 
-  drm_intel_bo_map(bo, 1);
+  if (!bo || (drm_intel_bo_map(bo, 1) != 0)) {
+    if (gpgpu->printf_b.bo)
+      drm_intel_bo_unreference(gpgpu->printf_b.bo);
+    gpgpu->printf_b.bo = NULL;
+    fprintf(stderr, "%s:%d: %s.\n", __FILE__, __LINE__, strerror(errno));
+    return -1;
+  }
   memset(bo->virtual, 0, size);
   drm_intel_bo_unmap(bo);
 
   intel_gpgpu_bind_buf(gpgpu, bo, offset, 0, 0);
+  return 0;
 }
 
 static void*
-- 
1.8.3.2



More information about the Beignet mailing list