All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH 0/3] memstick/ms_block:
@ 2022-06-25 12:55 Christophe JAILLET
  2022-06-25 12:55 ` [PATCH 1/3] memstick/ms_block: Fix some incorrect memory allocation Christophe JAILLET
                   ` (2 more replies)
  0 siblings, 3 replies; 7+ messages in thread
From: Christophe JAILLET @ 2022-06-25 12:55 UTC (permalink / raw)
  Cc: linux-kernel, kernel-janitors, Christophe JAILLET

This serie is related to bitmap usage in memstick/ms_block.

Patch 1 use the dedicated bitmap API to make sure that a number of long is
allocated, instead of a number of bytes. 

Patch 2 fix a memory leak. This is speculative.

Pacth 3 is just a clean up to make more use of the bitmap API mostly for
consistancy reason.

Christophe JAILLET (3):
  memstick/ms_block: Fix some incorrect memory allocation
  memstick/ms_block: Fix a memory leak
  memstick/ms_block: Use the bitmap API when applicable

 drivers/memstick/core/ms_block.c | 15 ++++++++-------
 1 file changed, 8 insertions(+), 7 deletions(-)

-- 
2.34.1


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

* [PATCH 1/3] memstick/ms_block: Fix some incorrect memory allocation
  2022-06-25 12:55 [PATCH 0/3] memstick/ms_block: Christophe JAILLET
@ 2022-06-25 12:55 ` Christophe JAILLET
  2022-07-12 11:08   ` Ulf Hansson
  2022-06-25 12:55 ` [PATCH 2/3] memstick/ms_block: Fix a memory leak Christophe JAILLET
  2022-06-25 12:56 ` [PATCH 3/3] memstick/ms_block: Use the bitmap API when applicable Christophe JAILLET
  2 siblings, 1 reply; 7+ messages in thread
From: Christophe JAILLET @ 2022-06-25 12:55 UTC (permalink / raw)
  To: Maxim Levitsky, Alex Dubov, Ulf Hansson, Andrew Morton
  Cc: linux-kernel, kernel-janitors, Christophe JAILLET, linux-mmc

Some functions of the bitmap API take advantage of the fact that a bitmap
is an array of long.

So, to make sure this assertion is correct, allocate bitmaps with
bitmap_zalloc() instead of kzalloc()+hand-computed number of bytes.

While at it, also use bitmap_free() instead of kfree() to keep the
semantic.

Fixes: 0ab30494bc4f ("memstick: add support for legacy memorysticks")
Signed-off-by: Christophe JAILLET <christophe.jaillet@wanadoo.fr>
---
I guess that it is not an issue in RL because memory allocation is
certainly "rounding" to keep memory alignment.
---
 drivers/memstick/core/ms_block.c | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)

diff --git a/drivers/memstick/core/ms_block.c b/drivers/memstick/core/ms_block.c
index 3993bdd4b519..f8f151163667 100644
--- a/drivers/memstick/core/ms_block.c
+++ b/drivers/memstick/core/ms_block.c
@@ -1341,17 +1341,17 @@ static int msb_ftl_initialize(struct msb_data *msb)
 	msb->zone_count = msb->block_count / MS_BLOCKS_IN_ZONE;
 	msb->logical_block_count = msb->zone_count * 496 - 2;
 
-	msb->used_blocks_bitmap = kzalloc(msb->block_count / 8, GFP_KERNEL);
-	msb->erased_blocks_bitmap = kzalloc(msb->block_count / 8, GFP_KERNEL);
+	msb->used_blocks_bitmap = bitmap_zalloc(msb->block_count, GFP_KERNEL);
+	msb->erased_blocks_bitmap = bitmap_zalloc(msb->block_count, GFP_KERNEL);
 	msb->lba_to_pba_table =
 		kmalloc_array(msb->logical_block_count, sizeof(u16),
 			      GFP_KERNEL);
 
 	if (!msb->used_blocks_bitmap || !msb->lba_to_pba_table ||
 						!msb->erased_blocks_bitmap) {
-		kfree(msb->used_blocks_bitmap);
+		bitmap_free(msb->used_blocks_bitmap);
+		bitmap_free(msb->erased_blocks_bitmap);
 		kfree(msb->lba_to_pba_table);
-		kfree(msb->erased_blocks_bitmap);
 		return -ENOMEM;
 	}
 
@@ -1946,7 +1946,7 @@ static DEFINE_MUTEX(msb_disk_lock); /* protects against races in open/release */
 static void msb_data_clear(struct msb_data *msb)
 {
 	kfree(msb->boot_page);
-	kfree(msb->used_blocks_bitmap);
+	bitmap_free(msb->used_blocks_bitmap);
 	kfree(msb->lba_to_pba_table);
 	kfree(msb->cache);
 	msb->card = NULL;
-- 
2.34.1


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

* [PATCH 2/3] memstick/ms_block: Fix a memory leak
  2022-06-25 12:55 [PATCH 0/3] memstick/ms_block: Christophe JAILLET
  2022-06-25 12:55 ` [PATCH 1/3] memstick/ms_block: Fix some incorrect memory allocation Christophe JAILLET
@ 2022-06-25 12:55 ` Christophe JAILLET
  2022-07-12 11:08   ` Ulf Hansson
  2022-06-25 12:56 ` [PATCH 3/3] memstick/ms_block: Use the bitmap API when applicable Christophe JAILLET
  2 siblings, 1 reply; 7+ messages in thread
From: Christophe JAILLET @ 2022-06-25 12:55 UTC (permalink / raw)
  To: Maxim Levitsky, Alex Dubov, Ulf Hansson, Andrew Morton
  Cc: linux-kernel, kernel-janitors, Christophe JAILLET, linux-mmc

'erased_blocks_bitmap' is never freed. As it is allocated at the same time
as 'used_blocks_bitmap', it is likely that it should be freed also at the
same time.

Add the corresponding bitmap_free() in msb_data_clear().

Fixes: 0ab30494bc4f ("memstick: add support for legacy memorysticks")
Signed-off-by: Christophe JAILLET <christophe.jaillet@wanadoo.fr>
---
This patch is speculative.
---
 drivers/memstick/core/ms_block.c | 1 +
 1 file changed, 1 insertion(+)

diff --git a/drivers/memstick/core/ms_block.c b/drivers/memstick/core/ms_block.c
index f8f151163667..f8fdf88fb240 100644
--- a/drivers/memstick/core/ms_block.c
+++ b/drivers/memstick/core/ms_block.c
@@ -1947,6 +1947,7 @@ static void msb_data_clear(struct msb_data *msb)
 {
 	kfree(msb->boot_page);
 	bitmap_free(msb->used_blocks_bitmap);
+	bitmap_free(msb->erased_blocks_bitmap);
 	kfree(msb->lba_to_pba_table);
 	kfree(msb->cache);
 	msb->card = NULL;
-- 
2.34.1


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

* [PATCH 3/3] memstick/ms_block: Use the bitmap API when applicable
  2022-06-25 12:55 [PATCH 0/3] memstick/ms_block: Christophe JAILLET
  2022-06-25 12:55 ` [PATCH 1/3] memstick/ms_block: Fix some incorrect memory allocation Christophe JAILLET
  2022-06-25 12:55 ` [PATCH 2/3] memstick/ms_block: Fix a memory leak Christophe JAILLET
@ 2022-06-25 12:56 ` Christophe JAILLET
  2022-07-12 11:08   ` Ulf Hansson
  2 siblings, 1 reply; 7+ messages in thread
From: Christophe JAILLET @ 2022-06-25 12:56 UTC (permalink / raw)
  To: Maxim Levitsky, Alex Dubov, Ulf Hansson
  Cc: linux-kernel, kernel-janitors, Christophe JAILLET, linux-mmc

Use bitmap_equal() instead of hand writing it. It improves semantic and
avoids some explicit computation to convert a number of bits to a number of
bytes.

Signed-off-by: Christophe JAILLET <christophe.jaillet@wanadoo.fr>
---
 drivers/memstick/core/ms_block.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/drivers/memstick/core/ms_block.c b/drivers/memstick/core/ms_block.c
index f8fdf88fb240..c05edfc1c841 100644
--- a/drivers/memstick/core/ms_block.c
+++ b/drivers/memstick/core/ms_block.c
@@ -2245,8 +2245,8 @@ static int msb_resume(struct memstick_dev *card)
 		goto out;
 
 	if (msb->block_count != new_msb->block_count ||
-		memcmp(msb->used_blocks_bitmap, new_msb->used_blocks_bitmap,
-							msb->block_count / 8))
+	    !bitmap_equal(msb->used_blocks_bitmap, new_msb->used_blocks_bitmap,
+							msb->block_count))
 		goto out;
 
 	card_dead = false;
-- 
2.34.1


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

* Re: [PATCH 1/3] memstick/ms_block: Fix some incorrect memory allocation
  2022-06-25 12:55 ` [PATCH 1/3] memstick/ms_block: Fix some incorrect memory allocation Christophe JAILLET
@ 2022-07-12 11:08   ` Ulf Hansson
  0 siblings, 0 replies; 7+ messages in thread
From: Ulf Hansson @ 2022-07-12 11:08 UTC (permalink / raw)
  To: Christophe JAILLET
  Cc: Maxim Levitsky, Alex Dubov, Andrew Morton, linux-kernel,
	kernel-janitors, linux-mmc

On Sat, 25 Jun 2022 at 14:55, Christophe JAILLET
<christophe.jaillet@wanadoo.fr> wrote:
>
> Some functions of the bitmap API take advantage of the fact that a bitmap
> is an array of long.
>
> So, to make sure this assertion is correct, allocate bitmaps with
> bitmap_zalloc() instead of kzalloc()+hand-computed number of bytes.
>
> While at it, also use bitmap_free() instead of kfree() to keep the
> semantic.
>
> Fixes: 0ab30494bc4f ("memstick: add support for legacy memorysticks")
> Signed-off-by: Christophe JAILLET <christophe.jaillet@wanadoo.fr>

Applied for next, thanks!

Kind regards
Uffe


> ---
> I guess that it is not an issue in RL because memory allocation is
> certainly "rounding" to keep memory alignment.
> ---
>  drivers/memstick/core/ms_block.c | 10 +++++-----
>  1 file changed, 5 insertions(+), 5 deletions(-)
>
> diff --git a/drivers/memstick/core/ms_block.c b/drivers/memstick/core/ms_block.c
> index 3993bdd4b519..f8f151163667 100644
> --- a/drivers/memstick/core/ms_block.c
> +++ b/drivers/memstick/core/ms_block.c
> @@ -1341,17 +1341,17 @@ static int msb_ftl_initialize(struct msb_data *msb)
>         msb->zone_count = msb->block_count / MS_BLOCKS_IN_ZONE;
>         msb->logical_block_count = msb->zone_count * 496 - 2;
>
> -       msb->used_blocks_bitmap = kzalloc(msb->block_count / 8, GFP_KERNEL);
> -       msb->erased_blocks_bitmap = kzalloc(msb->block_count / 8, GFP_KERNEL);
> +       msb->used_blocks_bitmap = bitmap_zalloc(msb->block_count, GFP_KERNEL);
> +       msb->erased_blocks_bitmap = bitmap_zalloc(msb->block_count, GFP_KERNEL);
>         msb->lba_to_pba_table =
>                 kmalloc_array(msb->logical_block_count, sizeof(u16),
>                               GFP_KERNEL);
>
>         if (!msb->used_blocks_bitmap || !msb->lba_to_pba_table ||
>                                                 !msb->erased_blocks_bitmap) {
> -               kfree(msb->used_blocks_bitmap);
> +               bitmap_free(msb->used_blocks_bitmap);
> +               bitmap_free(msb->erased_blocks_bitmap);
>                 kfree(msb->lba_to_pba_table);
> -               kfree(msb->erased_blocks_bitmap);
>                 return -ENOMEM;
>         }
>
> @@ -1946,7 +1946,7 @@ static DEFINE_MUTEX(msb_disk_lock); /* protects against races in open/release */
>  static void msb_data_clear(struct msb_data *msb)
>  {
>         kfree(msb->boot_page);
> -       kfree(msb->used_blocks_bitmap);
> +       bitmap_free(msb->used_blocks_bitmap);
>         kfree(msb->lba_to_pba_table);
>         kfree(msb->cache);
>         msb->card = NULL;
> --
> 2.34.1
>

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

* Re: [PATCH 2/3] memstick/ms_block: Fix a memory leak
  2022-06-25 12:55 ` [PATCH 2/3] memstick/ms_block: Fix a memory leak Christophe JAILLET
@ 2022-07-12 11:08   ` Ulf Hansson
  0 siblings, 0 replies; 7+ messages in thread
From: Ulf Hansson @ 2022-07-12 11:08 UTC (permalink / raw)
  To: Christophe JAILLET
  Cc: Maxim Levitsky, Alex Dubov, Andrew Morton, linux-kernel,
	kernel-janitors, linux-mmc

On Sat, 25 Jun 2022 at 14:55, Christophe JAILLET
<christophe.jaillet@wanadoo.fr> wrote:
>
> 'erased_blocks_bitmap' is never freed. As it is allocated at the same time
> as 'used_blocks_bitmap', it is likely that it should be freed also at the
> same time.
>
> Add the corresponding bitmap_free() in msb_data_clear().
>
> Fixes: 0ab30494bc4f ("memstick: add support for legacy memorysticks")
> Signed-off-by: Christophe JAILLET <christophe.jaillet@wanadoo.fr>

Applied for next, thanks!

Kind regards
Uffe


> ---
> This patch is speculative.
> ---
>  drivers/memstick/core/ms_block.c | 1 +
>  1 file changed, 1 insertion(+)
>
> diff --git a/drivers/memstick/core/ms_block.c b/drivers/memstick/core/ms_block.c
> index f8f151163667..f8fdf88fb240 100644
> --- a/drivers/memstick/core/ms_block.c
> +++ b/drivers/memstick/core/ms_block.c
> @@ -1947,6 +1947,7 @@ static void msb_data_clear(struct msb_data *msb)
>  {
>         kfree(msb->boot_page);
>         bitmap_free(msb->used_blocks_bitmap);
> +       bitmap_free(msb->erased_blocks_bitmap);
>         kfree(msb->lba_to_pba_table);
>         kfree(msb->cache);
>         msb->card = NULL;
> --
> 2.34.1
>

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

* Re: [PATCH 3/3] memstick/ms_block: Use the bitmap API when applicable
  2022-06-25 12:56 ` [PATCH 3/3] memstick/ms_block: Use the bitmap API when applicable Christophe JAILLET
@ 2022-07-12 11:08   ` Ulf Hansson
  0 siblings, 0 replies; 7+ messages in thread
From: Ulf Hansson @ 2022-07-12 11:08 UTC (permalink / raw)
  To: Christophe JAILLET
  Cc: Maxim Levitsky, Alex Dubov, linux-kernel, kernel-janitors, linux-mmc

On Sat, 25 Jun 2022 at 14:56, Christophe JAILLET
<christophe.jaillet@wanadoo.fr> wrote:
>
> Use bitmap_equal() instead of hand writing it. It improves semantic and
> avoids some explicit computation to convert a number of bits to a number of
> bytes.
>
> Signed-off-by: Christophe JAILLET <christophe.jaillet@wanadoo.fr>

Applied for next, thanks!

Kind regards
Uffe


> ---
>  drivers/memstick/core/ms_block.c | 4 ++--
>  1 file changed, 2 insertions(+), 2 deletions(-)
>
> diff --git a/drivers/memstick/core/ms_block.c b/drivers/memstick/core/ms_block.c
> index f8fdf88fb240..c05edfc1c841 100644
> --- a/drivers/memstick/core/ms_block.c
> +++ b/drivers/memstick/core/ms_block.c
> @@ -2245,8 +2245,8 @@ static int msb_resume(struct memstick_dev *card)
>                 goto out;
>
>         if (msb->block_count != new_msb->block_count ||
> -               memcmp(msb->used_blocks_bitmap, new_msb->used_blocks_bitmap,
> -                                                       msb->block_count / 8))
> +           !bitmap_equal(msb->used_blocks_bitmap, new_msb->used_blocks_bitmap,
> +                                                       msb->block_count))
>                 goto out;
>
>         card_dead = false;
> --
> 2.34.1
>

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

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

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-06-25 12:55 [PATCH 0/3] memstick/ms_block: Christophe JAILLET
2022-06-25 12:55 ` [PATCH 1/3] memstick/ms_block: Fix some incorrect memory allocation Christophe JAILLET
2022-07-12 11:08   ` Ulf Hansson
2022-06-25 12:55 ` [PATCH 2/3] memstick/ms_block: Fix a memory leak Christophe JAILLET
2022-07-12 11:08   ` Ulf Hansson
2022-06-25 12:56 ` [PATCH 3/3] memstick/ms_block: Use the bitmap API when applicable Christophe JAILLET
2022-07-12 11:08   ` Ulf Hansson

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.