linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH 1/2] cxl: Use the bitmap API to allocate bitmaps
@ 2022-07-11 19:14 Christophe JAILLET
  2022-07-11 19:14 ` [PATCH 2/2] cxl: Fix a memory leak in an error handling path Christophe JAILLET
  2022-07-12 16:42 ` [PATCH 1/2] cxl: Use the bitmap API to allocate bitmaps Andrew Donnellan
  0 siblings, 2 replies; 4+ messages in thread
From: Christophe JAILLET @ 2022-07-11 19:14 UTC (permalink / raw)
  To: Frederic Barrat, Andrew Donnellan, Arnd Bergmann, Greg Kroah-Hartman
  Cc: linux-kernel, kernel-janitors, Christophe JAILLET, linuxppc-dev

Use bitmap_zalloc()/bitmap_free() instead of hand-writing them.

It is less verbose and it improves the semantic.

Signed-off-by: Christophe JAILLET <christophe.jaillet@wanadoo.fr>
---
 drivers/misc/cxl/context.c | 2 +-
 drivers/misc/cxl/guest.c   | 2 +-
 drivers/misc/cxl/irq.c     | 3 +--
 drivers/misc/cxl/of.c      | 5 ++---
 4 files changed, 5 insertions(+), 7 deletions(-)

diff --git a/drivers/misc/cxl/context.c b/drivers/misc/cxl/context.c
index e627b4056623..acaa44809c58 100644
--- a/drivers/misc/cxl/context.c
+++ b/drivers/misc/cxl/context.c
@@ -331,7 +331,7 @@ static void reclaim_ctx(struct rcu_head *rcu)
 		__free_page(ctx->ff_page);
 	ctx->sstp = NULL;
 
-	kfree(ctx->irq_bitmap);
+	bitmap_free(ctx->irq_bitmap);
 
 	/* Drop ref to the afu device taken during cxl_context_init */
 	cxl_afu_put(ctx->afu);
diff --git a/drivers/misc/cxl/guest.c b/drivers/misc/cxl/guest.c
index 3321c014913c..375f692ae9d6 100644
--- a/drivers/misc/cxl/guest.c
+++ b/drivers/misc/cxl/guest.c
@@ -1053,7 +1053,7 @@ static void free_adapter(struct cxl *adapter)
 		if (adapter->guest->irq_avail) {
 			for (i = 0; i < adapter->guest->irq_nranges; i++) {
 				cur = &adapter->guest->irq_avail[i];
-				kfree(cur->bitmap);
+				bitmap_free(cur->bitmap);
 			}
 			kfree(adapter->guest->irq_avail);
 		}
diff --git a/drivers/misc/cxl/irq.c b/drivers/misc/cxl/irq.c
index 5f0e2dcebb34..0ce91d99aead 100644
--- a/drivers/misc/cxl/irq.c
+++ b/drivers/misc/cxl/irq.c
@@ -319,8 +319,7 @@ int afu_allocate_irqs(struct cxl_context *ctx, u32 count)
 	}
 
 	ctx->irq_count = count;
-	ctx->irq_bitmap = kcalloc(BITS_TO_LONGS(count),
-				  sizeof(*ctx->irq_bitmap), GFP_KERNEL);
+	ctx->irq_bitmap = bitmap_zalloc(count, GFP_KERNEL);
 	if (!ctx->irq_bitmap)
 		goto out;
 
diff --git a/drivers/misc/cxl/of.c b/drivers/misc/cxl/of.c
index 1cfecba42d01..25ce725035e7 100644
--- a/drivers/misc/cxl/of.c
+++ b/drivers/misc/cxl/of.c
@@ -308,8 +308,7 @@ static int read_adapter_irq_config(struct cxl *adapter, struct device_node *np)
 		cur = &adapter->guest->irq_avail[i];
 		cur->offset = be32_to_cpu(ranges[i * 2]);
 		cur->range  = be32_to_cpu(ranges[i * 2 + 1]);
-		cur->bitmap = kcalloc(BITS_TO_LONGS(cur->range),
-				sizeof(*cur->bitmap), GFP_KERNEL);
+		cur->bitmap = bitmap_zalloc(cur->range, GFP_KERNEL);
 		if (cur->bitmap == NULL)
 			goto err;
 		if (cur->offset < adapter->guest->irq_base_offset)
@@ -326,7 +325,7 @@ static int read_adapter_irq_config(struct cxl *adapter, struct device_node *np)
 err:
 	for (i--; i >= 0; i--) {
 		cur = &adapter->guest->irq_avail[i];
-		kfree(cur->bitmap);
+		bitmap_free(cur->bitmap);
 	}
 	kfree(adapter->guest->irq_avail);
 	adapter->guest->irq_avail = NULL;
-- 
2.34.1


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* [PATCH 2/2] cxl: Fix a memory leak in an error handling path
  2022-07-11 19:14 [PATCH 1/2] cxl: Use the bitmap API to allocate bitmaps Christophe JAILLET
@ 2022-07-11 19:14 ` Christophe JAILLET
  2022-07-12 16:46   ` Andrew Donnellan
  2022-07-12 16:42 ` [PATCH 1/2] cxl: Use the bitmap API to allocate bitmaps Andrew Donnellan
  1 sibling, 1 reply; 4+ messages in thread
From: Christophe JAILLET @ 2022-07-11 19:14 UTC (permalink / raw)
  To: Frederic Barrat, Andrew Donnellan, Arnd Bergmann, Greg Kroah-Hartman
  Cc: linux-kernel, kernel-janitors, Christophe JAILLET, linuxppc-dev

A bitmap_zalloc() must be balanced by a corresponding bitmap_free() in the
error handling path of afu_allocate_irqs().

Signed-off-by: Christophe JAILLET <christophe.jaillet@wanadoo.fr>
---
The error handling path should be done in the reversed order but it should
work as-is.
---
 drivers/misc/cxl/irq.c | 1 +
 1 file changed, 1 insertion(+)

diff --git a/drivers/misc/cxl/irq.c b/drivers/misc/cxl/irq.c
index 0ce91d99aead..b730e022a48e 100644
--- a/drivers/misc/cxl/irq.c
+++ b/drivers/misc/cxl/irq.c
@@ -349,6 +349,7 @@ int afu_allocate_irqs(struct cxl_context *ctx, u32 count)
 
 out:
 	cxl_ops->release_irq_ranges(&ctx->irqs, ctx->afu->adapter);
+	bitmap_free(ctx->irq_bitmap);
 	afu_irq_name_free(ctx);
 	return -ENOMEM;
 }
-- 
2.34.1


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH 1/2] cxl: Use the bitmap API to allocate bitmaps
  2022-07-11 19:14 [PATCH 1/2] cxl: Use the bitmap API to allocate bitmaps Christophe JAILLET
  2022-07-11 19:14 ` [PATCH 2/2] cxl: Fix a memory leak in an error handling path Christophe JAILLET
@ 2022-07-12 16:42 ` Andrew Donnellan
  1 sibling, 0 replies; 4+ messages in thread
From: Andrew Donnellan @ 2022-07-12 16:42 UTC (permalink / raw)
  To: Christophe JAILLET, Frederic Barrat, Arnd Bergmann, Greg Kroah-Hartman
  Cc: linux-kernel, kernel-janitors, linuxppc-dev

On Mon, 2022-07-11 at 21:14 +0200, Christophe JAILLET wrote:
> Use bitmap_zalloc()/bitmap_free() instead of hand-writing them.
> 
> It is less verbose and it improves the semantic.
> 
> Signed-off-by: Christophe JAILLET <christophe.jaillet@wanadoo.fr>

Thanks!

Acked-by: Andrew Donnellan <ajd@linux.ibm.com>

> ---
>  drivers/misc/cxl/context.c | 2 +-
>  drivers/misc/cxl/guest.c   | 2 +-
>  drivers/misc/cxl/irq.c     | 3 +--
>  drivers/misc/cxl/of.c      | 5 ++---
>  4 files changed, 5 insertions(+), 7 deletions(-)
> 
> diff --git a/drivers/misc/cxl/context.c b/drivers/misc/cxl/context.c
> index e627b4056623..acaa44809c58 100644
> --- a/drivers/misc/cxl/context.c
> +++ b/drivers/misc/cxl/context.c
> @@ -331,7 +331,7 @@ static void reclaim_ctx(struct rcu_head *rcu)
>                 __free_page(ctx->ff_page);
>         ctx->sstp = NULL;
>  
> -       kfree(ctx->irq_bitmap);
> +       bitmap_free(ctx->irq_bitmap);
>  
>         /* Drop ref to the afu device taken during cxl_context_init
> */
>         cxl_afu_put(ctx->afu);
> diff --git a/drivers/misc/cxl/guest.c b/drivers/misc/cxl/guest.c
> index 3321c014913c..375f692ae9d6 100644
> --- a/drivers/misc/cxl/guest.c
> +++ b/drivers/misc/cxl/guest.c
> @@ -1053,7 +1053,7 @@ static void free_adapter(struct cxl *adapter)
>                 if (adapter->guest->irq_avail) {
>                         for (i = 0; i < adapter->guest->irq_nranges;
> i++) {
>                                 cur = &adapter->guest->irq_avail[i];
> -                               kfree(cur->bitmap);
> +                               bitmap_free(cur->bitmap);
>                         }
>                         kfree(adapter->guest->irq_avail);
>                 }
> diff --git a/drivers/misc/cxl/irq.c b/drivers/misc/cxl/irq.c
> index 5f0e2dcebb34..0ce91d99aead 100644
> --- a/drivers/misc/cxl/irq.c
> +++ b/drivers/misc/cxl/irq.c
> @@ -319,8 +319,7 @@ int afu_allocate_irqs(struct cxl_context *ctx,
> u32 count)
>         }
>  
>         ctx->irq_count = count;
> -       ctx->irq_bitmap = kcalloc(BITS_TO_LONGS(count),
> -                                 sizeof(*ctx->irq_bitmap),
> GFP_KERNEL);
> +       ctx->irq_bitmap = bitmap_zalloc(count, GFP_KERNEL);
>         if (!ctx->irq_bitmap)
>                 goto out;
>  
> diff --git a/drivers/misc/cxl/of.c b/drivers/misc/cxl/of.c
> index 1cfecba42d01..25ce725035e7 100644
> --- a/drivers/misc/cxl/of.c
> +++ b/drivers/misc/cxl/of.c
> @@ -308,8 +308,7 @@ static int read_adapter_irq_config(struct cxl
> *adapter, struct device_node *np)
>                 cur = &adapter->guest->irq_avail[i];
>                 cur->offset = be32_to_cpu(ranges[i * 2]);
>                 cur->range  = be32_to_cpu(ranges[i * 2 + 1]);
> -               cur->bitmap = kcalloc(BITS_TO_LONGS(cur->range),
> -                               sizeof(*cur->bitmap), GFP_KERNEL);
> +               cur->bitmap = bitmap_zalloc(cur->range, GFP_KERNEL);
>                 if (cur->bitmap == NULL)
>                         goto err;
>                 if (cur->offset < adapter->guest->irq_base_offset)
> @@ -326,7 +325,7 @@ static int read_adapter_irq_config(struct cxl
> *adapter, struct device_node *np)
>  err:
>         for (i--; i >= 0; i--) {
>                 cur = &adapter->guest->irq_avail[i];
> -               kfree(cur->bitmap);
> +               bitmap_free(cur->bitmap);
>         }
>         kfree(adapter->guest->irq_avail);
>         adapter->guest->irq_avail = NULL;



^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH 2/2] cxl: Fix a memory leak in an error handling path
  2022-07-11 19:14 ` [PATCH 2/2] cxl: Fix a memory leak in an error handling path Christophe JAILLET
@ 2022-07-12 16:46   ` Andrew Donnellan
  0 siblings, 0 replies; 4+ messages in thread
From: Andrew Donnellan @ 2022-07-12 16:46 UTC (permalink / raw)
  To: Christophe JAILLET, Frederic Barrat, Arnd Bergmann, Greg Kroah-Hartman
  Cc: linux-kernel, kernel-janitors, linuxppc-dev

On Mon, 2022-07-11 at 21:14 +0200, Christophe JAILLET wrote:
> A bitmap_zalloc() must be balanced by a corresponding bitmap_free()
> in the
> error handling path of afu_allocate_irqs().
> 
> Signed-off-by: Christophe JAILLET <christophe.jaillet@wanadoo.fr>

Thanks for catching this.

Acked-by: Andrew Donnellan <ajd@linux.ibm.com>

> ---
> The error handling path should be done in the reversed order but it
> should
> work as-is.
> ---
>  drivers/misc/cxl/irq.c | 1 +
>  1 file changed, 1 insertion(+)
> 
> diff --git a/drivers/misc/cxl/irq.c b/drivers/misc/cxl/irq.c
> index 0ce91d99aead..b730e022a48e 100644
> --- a/drivers/misc/cxl/irq.c
> +++ b/drivers/misc/cxl/irq.c
> @@ -349,6 +349,7 @@ int afu_allocate_irqs(struct cxl_context *ctx,
> u32 count)
>  
>  out:
>         cxl_ops->release_irq_ranges(&ctx->irqs, ctx->afu->adapter);
> +       bitmap_free(ctx->irq_bitmap);
>         afu_irq_name_free(ctx);
>         return -ENOMEM;
>  }



^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2022-07-12 16:46 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-07-11 19:14 [PATCH 1/2] cxl: Use the bitmap API to allocate bitmaps Christophe JAILLET
2022-07-11 19:14 ` [PATCH 2/2] cxl: Fix a memory leak in an error handling path Christophe JAILLET
2022-07-12 16:46   ` Andrew Donnellan
2022-07-12 16:42 ` [PATCH 1/2] cxl: Use the bitmap API to allocate bitmaps Andrew Donnellan

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).