Skip to contentSkip to conversation

memstick/ms_block:

PATCH In review Christophe JAILLET 2 participants 7 messages Jun 25, 2022 – Jul 12, 2022

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

cover memstick/ms_block:

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(-)
Signature · 1 line
2.34.1
#1 lore raw

PATCH 1/3 memstick/ms_block: Fix some incorrect memory allocation

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")

---
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(-)
drivers/memstick/core/ms_block.c +5 -5
index 3993bdd4b519..f8f151163667 100644
@@ -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;
Signature · 1 line
2.34.1
#2 lore raw

Re: [PATCH 1/3] memstick/ms_block: Fix some incorrect memory allocation

Quoted 4 lines · Christophe JAILLET

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

Quoted 8 lines

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")

Applied for next, thanks!

Kind regards
Uffe

Quoted 45 lines

---
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(-)
drivers/memstick/core/ms_block.c +5 -6
index 3993bdd4b519..f8f151163667 100644
@@ -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

#3 lore raw

PATCH 2/3 memstick/ms_block: Fix a memory leak

In reply to Christophe JAILLET

'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")

---
This patch is speculative.

---
 drivers/memstick/core/ms_block.c | 1 +
 1 file changed, 1 insertion(+)
drivers/memstick/core/ms_block.c +1 -0
index f8f151163667..f8fdf88fb240 100644
@@ -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;
Signature · 1 line
2.34.1
#4 lore raw

Re: [PATCH 2/3] memstick/ms_block: Fix a memory leak

Quoted 9 lines · Christophe JAILLET

'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")

Applied for next, thanks!

Kind regards
Uffe

Quoted 21 lines

---
This patch is speculative.

---
 drivers/memstick/core/ms_block.c | 1 +
 1 file changed, 1 insertion(+)
drivers/memstick/core/ms_block.c +1 -1
index f8f151163667..f8fdf88fb240 100644
@@ -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

#5 lore raw

PATCH 3/3 memstick/ms_block: Use the bitmap API when applicable

In reply to Christophe JAILLET

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.

---
 drivers/memstick/core/ms_block.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/memstick/core/ms_block.c +2 -2
index f8fdf88fb240..c05edfc1c841 100644
@@ -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;
Signature · 1 line
2.34.1
#6 lore raw

Re: [PATCH 3/3] memstick/ms_block: Use the bitmap API when applicable

Quoted 6 lines · Christophe JAILLET

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.

Applied for next, thanks!

Kind regards
Uffe

Quoted 22 lines
---
 drivers/memstick/core/ms_block.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/memstick/core/ms_block.c +2 -3
index f8fdf88fb240..c05edfc1c841 100644
@@ -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