Fix context leak with internal kernels

Submitted by Patrick Beaulieu on June 15, 2017, 11:15 p.m.

Details

Message ID E5B07653C9BD46428B85B1827F84D243283C6DF9@mbx027-w1-ca-4.exch027.domain.local
State New
Headers show
Series "Fix context leak with internal kernels" ( rev: 1 ) in Beignet

Not browsing as part of any series.

Commit Message

Patrick Beaulieu June 15, 2017, 11:15 p.m.
Account for internal program ctx references in cl_context_delete

Signed-off-by: Patrick Beaulieu <patrick.beaulieu@avigilon.com>
---
 src/cl_context.c | 19 ++++++++++++++++++-
 1 file changed, 18 insertions(+), 1 deletion(-)

Patch hide | download patch | download mbox

diff --git a/src/cl_context.c b/src/cl_context.c
index 1ba23024..89362365 100644
--- a/src/cl_context.c
+++ b/src/cl_context.c
@@ -358,10 +358,25 @@  cl_context_delete(cl_context ctx)
   if (UNLIKELY(ctx == NULL))
     return;
 
+  int internal_ctx_refs = 1;
+  // determine how many ctx refs are held by internal_prgs and built_in_prgs
+  for (i = CL_INTERNAL_KERNEL_MIN; i < CL_INTERNAL_KERNEL_MAX; i++) {
+    if (ctx->internal_kernels[i] && ctx->internal_prgs[i])
+      ++internal_ctx_refs;
+  }
+  
+  if (ctx->built_in_prgs)
+     ++internal_ctx_refs;
+
   /* We are not done yet */
-  if (CL_OBJECT_DEC_REF(ctx) > 1)
+  if (CL_OBJECT_DEC_REF(ctx) > internal_ctx_refs)
     return;
 
+  // create a temporary extra ref here so cl_program_delete doesn't
+  // attempt a recursive full cl_context_delete when cleaning up
+  // our internal programs
+  CL_OBJECT_INC_REF(ctx);
+
   /* delete the internal programs. */
   for (i = CL_INTERNAL_KERNEL_MIN; i < CL_INTERNAL_KERNEL_MAX; i++) {
     if (ctx->internal_kernels[i]) {
@@ -382,6 +397,8 @@  cl_context_delete(cl_context ctx)
   cl_program_delete(ctx->built_in_prgs);
   ctx->built_in_prgs = NULL;
 
+  CL_OBJECT_DEC_REF(ctx);
+
   cl_free(ctx->prop_user);
   cl_free(ctx->devices);
   cl_driver_delete(ctx->drv);

Comments

Rebase to master and add internal_ctx_refs when ctx->image_queue is not NULL.
The patch LGTM, pushed, thanks.

> -----Original Message-----

> From: Beignet [mailto:beignet-bounces@lists.freedesktop.org] On Behalf Of

> Patrick Beaulieu

> Sent: Friday, June 16, 2017 7:15

> To: beignet@lists.freedesktop.org

> Subject: [Beignet] [PATCH] Fix context leak with internal kernels

> 

> Account for internal program ctx references in cl_context_delete

> 

> Signed-off-by: Patrick Beaulieu <patrick.beaulieu@avigilon.com>

> ---

>  src/cl_context.c | 19 ++++++++++++++++++-

>  1 file changed, 18 insertions(+), 1 deletion(-)

> 

> diff --git a/src/cl_context.c b/src/cl_context.c index 1ba23024..89362365

> 100644

> --- a/src/cl_context.c

> +++ b/src/cl_context.c

> @@ -358,10 +358,25 @@ cl_context_delete(cl_context ctx)

>    if (UNLIKELY(ctx == NULL))

>      return;

> 

> +  int internal_ctx_refs = 1;

> +  // determine how many ctx refs are held by internal_prgs and

> + built_in_prgs  for (i = CL_INTERNAL_KERNEL_MIN; i <

> CL_INTERNAL_KERNEL_MAX; i++) {

> +    if (ctx->internal_kernels[i] && ctx->internal_prgs[i])

> +      ++internal_ctx_refs;

> +  }

> +

> +  if (ctx->built_in_prgs)

> +     ++internal_ctx_refs;

> +

>    /* We are not done yet */

> -  if (CL_OBJECT_DEC_REF(ctx) > 1)

> +  if (CL_OBJECT_DEC_REF(ctx) > internal_ctx_refs)

>      return;

> 

> +  // create a temporary extra ref here so cl_program_delete doesn't  //

> + attempt a recursive full cl_context_delete when cleaning up  // our

> + internal programs  CL_OBJECT_INC_REF(ctx);

> +

>    /* delete the internal programs. */

>    for (i = CL_INTERNAL_KERNEL_MIN; i < CL_INTERNAL_KERNEL_MAX; i++) {

>      if (ctx->internal_kernels[i]) {

> @@ -382,6 +397,8 @@ cl_context_delete(cl_context ctx)

>    cl_program_delete(ctx->built_in_prgs);

>    ctx->built_in_prgs = NULL;

> 

> +  CL_OBJECT_DEC_REF(ctx);

> +

>    cl_free(ctx->prop_user);

>    cl_free(ctx->devices);

>    cl_driver_delete(ctx->drv);

> --

> 2.11.0

> 

> _______________________________________________

> Beignet mailing list

> Beignet@lists.freedesktop.org

> https://lists.freedesktop.org/mailman/listinfo/beignet