Skip to contentSkip to conversation

part_efi: fix protective mbr struct allocation

PATCH New Hector Palacios 3 participants 5 messages Feb 13, 2014 – Feb 25, 2014

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 part_efi: fix protective mbr struct allocation

The calloc() call was allocating space for the sizeof the struct pointer rather than for the struct contents.
Besides, since this buffer is passed to mmc for writing and some platforms may use cache, the legacy_mbr struct should be cache-aligned.

---

Notes:
Changes since V1:
- Cache-align declaration of p_mbr pointer
- Use *p_mbr in sizeof() to match kernel CodingStyle

 disk/part_efi.c | 8 +++-----
 1 file changed, 3 insertions(+), 5 deletions(-)
disk/part_efi.c +3 -5
index 5dfaf490c89a..42936e04fb67 100644
@@ -229,10 +229,10 @@ int test_part_efi(block_dev_desc_t * dev_desc)
  */
 static int set_protective_mbr(block_dev_desc_t *dev_desc)
 {
-	legacy_mbr *p_mbr;
-
 	/* Setup the Protective MBR */
-	p_mbr = calloc(1, sizeof(p_mbr));
+	ALLOC_CACHE_ALIGN_BUFFER(legacy_mbr, p_mbr, 1);
+	memset(p_mbr, 0, sizeof(*p_mbr));
+
 	if (p_mbr == NULL) {
 		printf("%s: calloc failed!\n", __func__);
 		return -1;
@@ -247,11 +247,9 @@ static int set_protective_mbr(block_dev_desc_t *dev_desc)
 	if (dev_desc->block_write(dev_desc->dev, 0, 1, p_mbr) != 1) {
 		printf("** Can't write to device %d **\n",
 			dev_desc->dev);
-		free(p_mbr);
 		return -1;
 	}
 
-	free(p_mbr);
 	return 0;
 }
 
#1 lore raw

PATCH v2 1/1 part_efi: fix protective mbr struct allocation

Hi Hector,

Quoted 5 lines

The calloc() call was allocating space for the sizeof the struct pointer rather than for the struct contents.
Besides, since this buffer is passed to mmc for writing and some platforms may use cache, the legacy_mbr struct should be cache-aligned.

Thanks for preparing v2.
Everything seems OK.

Test HW: Exynos4210 - Trats.

Quoted 42 lines

---

Notes:
Changes since V1:
- Cache-align declaration of p_mbr pointer
- Use *p_mbr in sizeof() to match kernel CodingStyle

 disk/part_efi.c | 8 +++-----
 1 file changed, 3 insertions(+), 5 deletions(-)
disk/part_efi.c +3 -5
index 5dfaf490c89a..42936e04fb67 100644
@@ -229,10 +229,10 @@ int test_part_efi(block_dev_desc_t * dev_desc)
  */
 static int set_protective_mbr(block_dev_desc_t *dev_desc)
 {
-	legacy_mbr *p_mbr;
-
 	/* Setup the Protective MBR */
-	p_mbr = calloc(1, sizeof(p_mbr));
+	ALLOC_CACHE_ALIGN_BUFFER(legacy_mbr, p_mbr, 1);
+	memset(p_mbr, 0, sizeof(*p_mbr));
+
 	if (p_mbr == NULL) {
 		printf("%s: calloc failed!\n", __func__);
 		return -1;
@@ -247,11 +247,9 @@ static int set_protective_mbr(block_dev_desc_t
 *dev_desc) if (dev_desc->block_write(dev_desc->dev, 0, 1, p_mbr) !=
 1) { printf("** Can't write to device %d **\n",
 			dev_desc->dev);
-		free(p_mbr);
 		return -1;
 	}
 
-	free(p_mbr);
 	return 0;
 }
 
Signature · 5 lines
Best regards,

Lukasz Majewski

Samsung R&D Institute Poland (SRPOL) | Linux Platform Group
#2 lore raw

PATCH v2 1/1 part_efi: fix protective mbr struct allocation

In reply to Hector Palacios

Hi Tom,

Quoted 5 lines

The calloc() call was allocating space for the sizeof the struct pointer rather than for the struct contents.
Besides, since this buffer is passed to mmc for writing and some platforms may use cache, the legacy_mbr struct should be cache-aligned.

Is there any problem with this patch?

Quoted 42 lines

---

Notes:
Changes since V1:
- Cache-align declaration of p_mbr pointer
- Use *p_mbr in sizeof() to match kernel CodingStyle

 disk/part_efi.c | 8 +++-----
 1 file changed, 3 insertions(+), 5 deletions(-)
disk/part_efi.c +3 -5
index 5dfaf490c89a..42936e04fb67 100644
@@ -229,10 +229,10 @@ int test_part_efi(block_dev_desc_t * dev_desc)
  */
 static int set_protective_mbr(block_dev_desc_t *dev_desc)
 {
-	legacy_mbr *p_mbr;
-
 	/* Setup the Protective MBR */
-	p_mbr = calloc(1, sizeof(p_mbr));
+	ALLOC_CACHE_ALIGN_BUFFER(legacy_mbr, p_mbr, 1);
+	memset(p_mbr, 0, sizeof(*p_mbr));
+
 	if (p_mbr == NULL) {
 		printf("%s: calloc failed!\n", __func__);
 		return -1;
@@ -247,11 +247,9 @@ static int set_protective_mbr(block_dev_desc_t
 *dev_desc) if (dev_desc->block_write(dev_desc->dev, 0, 1, p_mbr) !=
 1) { printf("** Can't write to device %d **\n",
 			dev_desc->dev);
-		free(p_mbr);
 		return -1;
 	}
 
-	free(p_mbr);
 	return 0;
 }
 
Signature · 5 lines
Best regards,

Lukasz Majewski

Samsung R&D Institute Poland (SRPOL) | Linux Platform Group
#3 lore raw

PATCH v2 1/1 part_efi: fix protective mbr struct allocation

Quoted 9 lines · Lukasz Majewski

Hi Tom,

Quoted 5 lines

The calloc() call was allocating space for the sizeof the struct pointer rather than for the struct contents.
Besides, since this buffer is passed to mmc for writing and some platforms may use cache, the legacy_mbr struct should be cache-aligned.

Is there any problem with this patch?

Just started re-reviewing this one today in fact, good timing.

Signature · 8 lines
Tom
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 836 bytes
Desc: Digital signature
URL: <http://lists.denx.de/pipermail/u-boot/attachments/20140224/1644ca72/attachment.pgp>
#4 lore raw

PATCH v2 1/1 part_efi: fix protective mbr struct allocation

In reply to Hector Palacios
Quoted 7 lines · Hector Palacios

The calloc() call was allocating space for the sizeof the struct pointer rather than for the struct contents.
Besides, since this buffer is passed to mmc for writing and some platforms may use cache, the legacy_mbr struct should be cache-aligned.

Applied to u-boot/master, thanks!

Signature · 8 lines
Tom
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 836 bytes
Desc: Digital signature
URL: <http://lists.denx.de/pipermail/u-boot/attachments/20140225/19435031/attachment.pgp>