[Beignet] [PATCH] Move the memory allocate size check to the callee.

Zhigang Gong zhigang.gong at linux.intel.com
Tue Jan 7 22:55:11 PST 2014


LGTM, pushed with slightly modification. Thanks.

On Tue, Jan 07, 2014 at 11:30:54AM +0800, Yang Rong wrote:
> Because image's alignment, the alloc size may exceed the CL_DEVICE_MAX_MEM_ALLOC_SIZE if the
> image's size is calculate from it. So move the size check from cl_mem_allocate to the callee, and
> slightly enlarge the limit size when check in allocate image.
> 
> Signed-off-by: Yang Rong <rong.r.yang at intel.com>
> ---
>  src/cl_gt_device.h |  2 +-
>  src/cl_mem.c       | 43 +++++++++++++++++++++++++++++--------------
>  2 files changed, 30 insertions(+), 15 deletions(-)
> 
> diff --git a/src/cl_gt_device.h b/src/cl_gt_device.h
> index 30692f8..110988a 100644
> --- a/src/cl_gt_device.h
> +++ b/src/cl_gt_device.h
> @@ -51,7 +51,7 @@
>  .min_data_type_align_size = sizeof(cl_long) * 16,
>  .single_fp_config = 0, /* XXX */
>  .global_mem_cache_type = CL_READ_WRITE_CACHE,
> -.global_mem_size = 256 * 1024 * 1024,
> +.global_mem_size = 128 * 1024 * 1024,
>  .max_constant_buffer_size = 512 << 10,
>  .max_constant_args = 8,
>  .error_correction_support = CL_FALSE,
> diff --git a/src/cl_mem.c b/src/cl_mem.c
> index 5c4b197..72cf92b 100644
> --- a/src/cl_mem.c
> +++ b/src/cl_mem.c
> @@ -200,23 +200,9 @@ cl_mem_allocate(enum cl_mem_type type,
>    cl_mem mem = NULL;
>    cl_int err = CL_SUCCESS;
>    size_t alignment = 64;
> -  cl_ulong max_mem_size;
>  
>    assert(ctx);
>  
> -  /* Due to alignment, the image size may exceed alloc max size, check global mem instead */
> -  if ((err = cl_get_device_info(ctx->device,
> -                                CL_DEVICE_GLOBAL_MEM_SIZE,
> -                                sizeof(max_mem_size),
> -                                &max_mem_size,
> -                                NULL)) != CL_SUCCESS) {
> -    goto error;
> -  }
> -  if (UNLIKELY(sz > max_mem_size)) {
> -    err = CL_INVALID_BUFFER_SIZE;
> -    goto error;
> -  }
> -
>    /* Allocate and inialize the structure itself */
>    if (type == CL_MEM_IMAGE_TYPE) {
>      struct _cl_mem_image *image = NULL;
> @@ -289,6 +275,7 @@ cl_mem_new_buffer(cl_context ctx,
>  
>    cl_int err = CL_SUCCESS;
>    cl_mem mem = NULL;
> +  cl_ulong max_mem_size;
>  
>    if (UNLIKELY(sz == 0)) {
>      err = CL_INVALID_BUFFER_SIZE;
> @@ -334,6 +321,19 @@ cl_mem_new_buffer(cl_context ctx,
>      goto error;
>    }
>  
> +  if ((err = cl_get_device_info(ctx->device,
> +                                CL_DEVICE_MAX_MEM_ALLOC_SIZE,
> +                                sizeof(max_mem_size),
> +                                &max_mem_size,
> +                                NULL)) != CL_SUCCESS) {
> +    goto error;
> +  }
> +
> +  if (UNLIKELY(sz > max_mem_size)) {
> +    err = CL_INVALID_BUFFER_SIZE;
> +    goto error;
> +  }
> +
>    /* Create the buffer in video memory */
>    mem = cl_mem_allocate(CL_MEM_BUFFER_TYPE, ctx, flags, sz, CL_FALSE, &err);
>    if (mem == NULL || err != CL_SUCCESS)
> @@ -520,6 +520,7 @@ _cl_mem_new_image(cl_context ctx,
>    uint32_t bpp = 0, intel_fmt = INTEL_UNSUPPORTED_FORMAT;
>    size_t sz = 0, aligned_pitch = 0, aligned_slice_pitch = 0, aligned_h;
>    cl_image_tiling_t tiling = CL_NO_TILE;
> +  cl_ulong max_mem_size;
>  
>    /* Check flags consistency */
>    if (UNLIKELY((flags & (CL_MEM_COPY_HOST_PTR | CL_MEM_USE_HOST_PTR)) && data == NULL)) {
> @@ -596,6 +597,20 @@ _cl_mem_new_image(cl_context ctx,
>    }
>  
>    sz = aligned_pitch * aligned_h * depth;
> +
> +  if ((err = cl_get_device_info(ctx->device,
> +                                CL_DEVICE_MAX_MEM_ALLOC_SIZE,
> +                                sizeof(max_mem_size),
> +                                &max_mem_size,
> +                                NULL)) != CL_SUCCESS) {
> +    goto error;
> +  }
> +  max_mem_size *= 1.1;   //enlarge alloc size limit because of alignment
> +  if (UNLIKELY(sz > max_mem_size)) {
> +    err = CL_INVALID_BUFFER_SIZE;
> +    goto error;
> +  }
> +
>    mem = cl_mem_allocate(CL_MEM_IMAGE_TYPE, ctx, flags, sz, tiling != CL_NO_TILE, &err);
>    if (mem == NULL || err != CL_SUCCESS)
>      goto error;
> -- 
> 1.8.3.2
> 
> _______________________________________________
> Beignet mailing list
> Beignet at lists.freedesktop.org
> http://lists.freedesktop.org/mailman/listinfo/beignet


More information about the Beignet mailing list