Patrick Georgi merged this change.

View Change

Approvals: build bot (Jenkins): Verified Hung-Te Lin: Looks good to me, approved
device_tree: Switch allocations to xzalloc()

The FIT code is already using xzalloc() everywhere, and that's the only
real consumer of device tree code right now. Chances are if you're
trying to unflatten an FDT and it doesn't fit into the heap you're
pretty much screwed anyway, so all the OOM handling feels a bit
unnecessary (and some functions will just silently fail because they
don't have a return value, which is bad). Let's just switch this all to
die on failed allocations.

Change-Id: I738f24d550a776653b2becd3d4f7d4d2cb3cc048
Signed-off-by: Julius Werner <jwerner@chromium.org>
Reviewed-on: https://review.coreboot.org/c/coreboot/+/32861
Reviewed-by: Hung-Te Lin <hungte@chromium.org>
Tested-by: build bot (Jenkins) <no-reply@coreboot.org>
---
M src/lib/device_tree.c
M src/lib/fit.c
2 files changed, 11 insertions(+), 46 deletions(-)

diff --git a/src/lib/device_tree.c b/src/lib/device_tree.c
index 8dbc510..bb40eee 100644
--- a/src/lib/device_tree.c
+++ b/src/lib/device_tree.c
@@ -16,6 +16,7 @@
*/

#include <assert.h>
+#include <commonlib/stdlib.h>
#include <console/console.h>
#include <device_tree.h>
#include <endian.h>
@@ -153,24 +154,6 @@
/*
* Functions to turn a flattened tree into an unflattened one.
*/
-static struct device_tree_node *alloc_node(void)
-{
- struct device_tree_node *buf = malloc(sizeof(struct device_tree_node));
- if (!buf)
- return NULL;
- memset(buf, 0, sizeof(*buf));
- return buf;
-}
-
-static struct device_tree_property *alloc_prop(void)
-{
- struct device_tree_property *buf =
- malloc(sizeof(struct device_tree_property));
- if (!buf)
- return NULL;
- memset(buf, 0, sizeof(*buf));
- return buf;
-}

static int fdt_unflatten_node(const void *blob, uint32_t start_offset,
struct device_tree_node **new_node)
@@ -185,18 +168,14 @@
return 0;
offset += size;

- struct device_tree_node *node = alloc_node();
+ struct device_tree_node *node = xzalloc(sizeof(*node));
*new_node = node;
- if (!node)
- return 0;
node->name = name;

struct fdt_property fprop;
last = &node->properties;
while ((size = fdt_next_property(blob, offset, &fprop))) {
- struct device_tree_property *prop = alloc_prop();
- if (!prop)
- return 0;
+ struct device_tree_property *prop = xzalloc(sizeof(*prop));
prop->prop = fprop;

list_insert_after(&prop->list_node, last);
@@ -227,10 +206,7 @@
if (!size)
return 0;

- struct device_tree_reserve_map_entry *entry = malloc(sizeof(*entry));
- if (!entry)
- return 0;
- memset(entry, 0, sizeof(*entry));
+ struct device_tree_reserve_map_entry *entry = xzalloc(sizeof(*entry));
*new = entry;
entry->start = start;
entry->size = size;
@@ -240,11 +216,8 @@

struct device_tree *fdt_unflatten(const void *blob)
{
- struct device_tree *tree = malloc(sizeof(*tree));
+ struct device_tree *tree = xzalloc(sizeof(*tree));
const struct fdt_header *header = (const struct fdt_header *)blob;
- if (!tree)
- return NULL;
- memset(tree, 0, sizeof(*tree));
tree->header = header;

uint32_t struct_offset = be32toh(header->structure_offset);
@@ -534,7 +507,7 @@
if (!create)
return NULL;

- found = alloc_node();
+ found = malloc(sizeof(*found));
if (!found)
return NULL;
found->name = strdup(*path);
@@ -804,9 +777,7 @@
}
}

- prop = alloc_prop();
- if (!prop)
- return;
+ prop = xzalloc(sizeof(*prop));
list_insert_after(&prop->list_node, &node->properties);
prop->prop.name = name;
prop->prop.data = data;
@@ -878,9 +849,7 @@
*/
void dt_add_u32_prop(struct device_tree_node *node, const char *name, u32 val)
{
- u32 *val_ptr = malloc(sizeof(val));
- if (!val_ptr)
- return;
+ u32 *val_ptr = xmalloc(sizeof(val));
*val_ptr = htobe32(val);
dt_add_bin_prop(node, name, val_ptr, sizeof(*val_ptr));
}
@@ -894,9 +863,7 @@
*/
void dt_add_u64_prop(struct device_tree_node *node, const char *name, u64 val)
{
- u64 *val_ptr = malloc(sizeof(val));
- if (!val_ptr)
- return;
+ u64 *val_ptr = xmalloc(sizeof(val));
*val_ptr = htobe64(val);
dt_add_bin_prop(node, name, val_ptr, sizeof(*val_ptr));
}
@@ -916,9 +883,7 @@
{
int i;
size_t length = (addr_cells + size_cells) * sizeof(u32) * count;
- u8 *data = malloc(length);
- if (!data)
- return;
+ u8 *data = xmalloc(length);
u8 *cur = data;

for (i = 0; i < count; i++) {
diff --git a/src/lib/fit.c b/src/lib/fit.c
index 3aad806..d15641d 100644
--- a/src/lib/fit.c
+++ b/src/lib/fit.c
@@ -27,7 +27,7 @@
#include <fit.h>
#include <boardid.h>
#include <commonlib/cbfs_serialized.h>
-#include <commonlib/include/commonlib/stdlib.h>
+#include <commonlib/stdlib.h>

static struct list_node image_nodes;
static struct list_node config_nodes;

To view, visit change 32861. To unsubscribe, or for help writing mail filters, visit settings.

Gerrit-Project: coreboot
Gerrit-Branch: master
Gerrit-Change-Id: I738f24d550a776653b2becd3d4f7d4d2cb3cc048
Gerrit-Change-Number: 32861
Gerrit-PatchSet: 6
Gerrit-Owner: Julius Werner <jwerner@chromium.org>
Gerrit-Reviewer: Hung-Te Lin <hungte@chromium.org>
Gerrit-Reviewer: Julius Werner <jwerner@chromium.org>
Gerrit-Reviewer: Patrick Georgi <pgeorgi@google.com>
Gerrit-Reviewer: Patrick Rudolph <patrick.rudolph@9elements.com>
Gerrit-Reviewer: build bot (Jenkins) <no-reply@coreboot.org>
Gerrit-CC: Paul Menzel <paulepanter@users.sourceforge.net>
Gerrit-MessageType: merged