mail archive of the barebox mailing list
 help / color / mirror / Atom feed
* [PATCH 1/2] partitions: efi: use calloc instead of kzalloc
@ 2024-10-22  8:33 Ahmad Fatoum
  2024-10-22  8:33 ` [PATCH 2/2] partition: dos: use xmalloc for unchecked allocations Ahmad Fatoum
  2024-10-22  9:22 ` [PATCH 1/2] partitions: efi: use calloc instead of kzalloc Sascha Hauer
  0 siblings, 2 replies; 3+ messages in thread
From: Ahmad Fatoum @ 2024-10-22  8:33 UTC (permalink / raw)
  To: barebox; +Cc: Ahmad Fatoum

kzalloc() uses at least DMA_ALIGNMENT as alignment, which is more than
we need for the data structures that are used to hold the partition info.

Use the normal allocator to be more efficient with memory.

Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de>
---
 common/partitions/efi.c | 28 ++++++++++++++--------------
 1 file changed, 14 insertions(+), 14 deletions(-)

diff --git a/common/partitions/efi.c b/common/partitions/efi.c
index 829360da6e1f..07944109ac89 100644
--- a/common/partitions/efi.c
+++ b/common/partitions/efi.c
@@ -92,7 +92,7 @@ static gpt_entry *alloc_read_gpt_entries(struct block_device *blk,
 	if (!count)
 		return NULL;
 
-	pte = kzalloc(count, GFP_KERNEL);
+	pte = calloc(count, 1);
 	if (!pte)
 		return NULL;
 
@@ -100,7 +100,7 @@ static gpt_entry *alloc_read_gpt_entries(struct block_device *blk,
 	size = count / GPT_BLOCK_SIZE;
 	ret = block_read(blk, pte, from, size);
 	if (ret) {
-		kfree(pte);
+		free(pte);
 		pte=NULL;
 		return NULL;
 	}
@@ -129,13 +129,13 @@ static gpt_header *alloc_read_gpt_header(struct block_device *blk,
 	unsigned ssz = bdev_logical_block_size(blk);
 	int ret;
 
-	gpt = kzalloc(ssz, GFP_KERNEL);
+	gpt = calloc(ssz, 1);
 	if (!gpt)
 		return NULL;
 
 	ret = block_read(blk, gpt, lba, 1);
 	if (ret) {
-		kfree(gpt);
+		free(gpt);
 		gpt=NULL;
 		return NULL;
 	}
@@ -227,10 +227,10 @@ static int is_gpt_valid(struct block_device *blk, u64 lba,
 	return 1;
 
  fail_ptes:
-	kfree(*ptes);
+	free(*ptes);
 	*ptes = NULL;
  fail:
-	kfree(*gpt);
+	free(*gpt);
 	*gpt = NULL;
 	return 0;
 }
@@ -406,8 +406,8 @@ static int find_valid_gpt(void *buf, struct block_device *blk, gpt_header **gpt,
 	if (good_pgpt) {
 		*gpt  = pgpt;
 		*ptes = pptes;
-		kfree(agpt);
-		kfree(aptes);
+		free(agpt);
+		free(aptes);
 		if (!good_agpt)
 			dev_warn(blk->dev, "Alternate GPT is invalid, using primary GPT.\n");
 		return 1;
@@ -415,17 +415,17 @@ static int find_valid_gpt(void *buf, struct block_device *blk, gpt_header **gpt,
 	else if (good_agpt) {
 		*gpt  = agpt;
 		*ptes = aptes;
-		kfree(pgpt);
-		kfree(pptes);
+		free(pgpt);
+		free(pptes);
 		dev_warn(blk->dev, "Primary GPT is invalid, using alternate GPT.\n");
 		return 1;
 	}
 
  fail:
-	kfree(pgpt);
-	kfree(agpt);
-	kfree(pptes);
-	kfree(aptes);
+	free(pgpt);
+	free(agpt);
+	free(pptes);
+	free(aptes);
 	*gpt = NULL;
 	*ptes = NULL;
 	return 0;
-- 
2.39.5




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

* [PATCH 2/2] partition: dos: use xmalloc for unchecked allocations
  2024-10-22  8:33 [PATCH 1/2] partitions: efi: use calloc instead of kzalloc Ahmad Fatoum
@ 2024-10-22  8:33 ` Ahmad Fatoum
  2024-10-22  9:22 ` [PATCH 1/2] partitions: efi: use calloc instead of kzalloc Sascha Hauer
  1 sibling, 0 replies; 3+ messages in thread
From: Ahmad Fatoum @ 2024-10-22  8:33 UTC (permalink / raw)
  To: barebox; +Cc: Ahmad Fatoum

Instead of unconditional dereferencing of pointers that may be NULL, do a
controlled panic in the case we are out of memory by using xmalloc().

Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de>
---
 common/partitions/dos.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/common/partitions/dos.c b/common/partitions/dos.c
index 8e4edd885bfa..511d58cf0489 100644
--- a/common/partitions/dos.c
+++ b/common/partitions/dos.c
@@ -53,7 +53,7 @@ static inline int is_extended_partition(struct partition *p)
 
 static void *read_mbr(struct block_device *blk)
 {
-	void *buf = malloc(SECTOR_SIZE);
+	void *buf = xmalloc(SECTOR_SIZE);
 	int ret;
 
 	ret = block_read(blk, buf, 0, 1);
@@ -124,7 +124,7 @@ static int dos_get_disk_signature(struct param_d *p, void *_priv)
 static void dos_extended_partition(struct block_device *blk, struct dos_partition_desc *dpd,
 		struct partition *partition, uint32_t signature)
 {
-	uint8_t *buf = malloc(SECTOR_SIZE);
+	uint8_t *buf = xmalloc(SECTOR_SIZE);
 	uint32_t ebr_sector = partition->first_sec;
 	struct partition_entry *table = (struct partition_entry *)&buf[0x1be];
 	unsigned partno = 4;
-- 
2.39.5




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

* Re: [PATCH 1/2] partitions: efi: use calloc instead of kzalloc
  2024-10-22  8:33 [PATCH 1/2] partitions: efi: use calloc instead of kzalloc Ahmad Fatoum
  2024-10-22  8:33 ` [PATCH 2/2] partition: dos: use xmalloc for unchecked allocations Ahmad Fatoum
@ 2024-10-22  9:22 ` Sascha Hauer
  1 sibling, 0 replies; 3+ messages in thread
From: Sascha Hauer @ 2024-10-22  9:22 UTC (permalink / raw)
  To: barebox, Ahmad Fatoum


On Tue, 22 Oct 2024 10:33:46 +0200, Ahmad Fatoum wrote:
> kzalloc() uses at least DMA_ALIGNMENT as alignment, which is more than
> we need for the data structures that are used to hold the partition info.
> 
> Use the normal allocator to be more efficient with memory.
> 
> 

Applied, thanks!

[1/2] partitions: efi: use calloc instead of kzalloc
      https://git.pengutronix.de/cgit/barebox/commit/?id=7faca080705f (link may not be stable)
[2/2] partition: dos: use xmalloc for unchecked allocations
      https://git.pengutronix.de/cgit/barebox/commit/?id=c528475db3ef (link may not be stable)

Best regards,
-- 
Sascha Hauer <s.hauer@pengutronix.de>




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

end of thread, other threads:[~2024-10-22  9:39 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-10-22  8:33 [PATCH 1/2] partitions: efi: use calloc instead of kzalloc Ahmad Fatoum
2024-10-22  8:33 ` [PATCH 2/2] partition: dos: use xmalloc for unchecked allocations Ahmad Fatoum
2024-10-22  9:22 ` [PATCH 1/2] partitions: efi: use calloc instead of kzalloc Sascha Hauer

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox