diff mbox

ndctl: fix libdaxctl memory leak

Message ID 152337938996.14344.7696749937613340141.stgit@djiang5-desk3.ch.intel.com (mailing list archive)
State New, archived
Headers show

Commit Message

Dave Jiang April 10, 2018, 4:56 p.m. UTC
When daxctl_unref is releasing the context, we should make sure that the
regions and devices are also being released.

Signed-off-by: Dave Jiang <dave.jiang@intel.com>
---
 daxctl/lib/libdaxctl.c |    7 +++++++
 1 file changed, 7 insertions(+)

Comments

Dan Williams April 10, 2018, 5:01 p.m. UTC | #1
On Tue, Apr 10, 2018 at 9:56 AM, Dave Jiang <dave.jiang@intel.com> wrote:
> When daxctl_unref is releasing the context, we should make sure that the
> regions and devices are also being released.
>
> Signed-off-by: Dave Jiang <dave.jiang@intel.com>
> ---
>  daxctl/lib/libdaxctl.c |    7 +++++++
>  1 file changed, 7 insertions(+)
>
> diff --git a/daxctl/lib/libdaxctl.c b/daxctl/lib/libdaxctl.c
> index 9e503201..0552f6d7 100644
> --- a/daxctl/lib/libdaxctl.c
> +++ b/daxctl/lib/libdaxctl.c
> @@ -119,11 +119,18 @@ DAXCTL_EXPORT struct daxctl_ctx *daxctl_ref(struct daxctl_ctx *ctx)
>   */
>  DAXCTL_EXPORT void daxctl_unref(struct daxctl_ctx *ctx)
>  {
> +       struct daxctl_region *region;
> +
>         if (ctx == NULL)
>                 return;
>         ctx->refcount--;
>         if (ctx->refcount > 0)
>                 return;
> +
> +       while ((region = list_top(&ctx->regions, struct daxctl_region, list)) !=
> +                       NULL)
> +               daxctl_region_unref(region);
> +

Sorry, should have mentioned this before. Why not use
list_for_each_entry_safe() to iterate and delete regions like all the
other free routines?
Dave Jiang April 10, 2018, 5:05 p.m. UTC | #2
On 04/10/2018 10:01 AM, Dan Williams wrote:
> On Tue, Apr 10, 2018 at 9:56 AM, Dave Jiang <dave.jiang@intel.com> wrote:
>> When daxctl_unref is releasing the context, we should make sure that the
>> regions and devices are also being released.
>>
>> Signed-off-by: Dave Jiang <dave.jiang@intel.com>
>> ---
>>  daxctl/lib/libdaxctl.c |    7 +++++++
>>  1 file changed, 7 insertions(+)
>>
>> diff --git a/daxctl/lib/libdaxctl.c b/daxctl/lib/libdaxctl.c
>> index 9e503201..0552f6d7 100644
>> --- a/daxctl/lib/libdaxctl.c
>> +++ b/daxctl/lib/libdaxctl.c
>> @@ -119,11 +119,18 @@ DAXCTL_EXPORT struct daxctl_ctx *daxctl_ref(struct daxctl_ctx *ctx)
>>   */
>>  DAXCTL_EXPORT void daxctl_unref(struct daxctl_ctx *ctx)
>>  {
>> +       struct daxctl_region *region;
>> +
>>         if (ctx == NULL)
>>                 return;
>>         ctx->refcount--;
>>         if (ctx->refcount > 0)
>>                 return;
>> +
>> +       while ((region = list_top(&ctx->regions, struct daxctl_region, list)) !=
>> +                       NULL)
>> +               daxctl_region_unref(region);
>> +
> 
> Sorry, should have mentioned this before. Why not use
> list_for_each_entry_safe() to iterate and delete regions like all the
> other free routines?
> 

Somehow I missed that even though I was looking for it. Will fix.
diff mbox

Patch

diff --git a/daxctl/lib/libdaxctl.c b/daxctl/lib/libdaxctl.c
index 9e503201..0552f6d7 100644
--- a/daxctl/lib/libdaxctl.c
+++ b/daxctl/lib/libdaxctl.c
@@ -119,11 +119,18 @@  DAXCTL_EXPORT struct daxctl_ctx *daxctl_ref(struct daxctl_ctx *ctx)
  */
 DAXCTL_EXPORT void daxctl_unref(struct daxctl_ctx *ctx)
 {
+	struct daxctl_region *region;
+
 	if (ctx == NULL)
 		return;
 	ctx->refcount--;
 	if (ctx->refcount > 0)
 		return;
+
+	while ((region = list_top(&ctx->regions, struct daxctl_region, list)) !=
+			NULL)
+		daxctl_region_unref(region);
+
 	info(ctx, "context %p released\n", ctx);
 	free(ctx);
 }