[PATCH 4/9] boot: imagemap: Preserve mappings when an extend fails
Daniel Golle
daniel at makrotopia.org
Fri Nov 13 16:11:17 PST 2026
Extending a region releases its reservation before allocating and
reading the larger range. A failure leaves the old mapping pointing at
unreserved memory, which cleanup may free again after another user
has allocated it.
Keep the original reservation until the read succeeds, reserving the
added space or a new block first. Update the mapping after success.
Fixes: 9b41c7dbee96 ("boot: add imagemap on-demand loading from storage")
Signed-off-by: Daniel Golle <daniel at makrotopia.org>
---
boot/imagemap.c | 143 +++++++++++++++++++++++++++++-------------------
1 file changed, 87 insertions(+), 56 deletions(-)
diff --git a/boot/imagemap.c b/boot/imagemap.c
index d46c1430fb7..0e3fbd0934a 100644
--- a/boot/imagemap.c
+++ b/boot/imagemap.c
@@ -208,6 +208,82 @@ static int imagemap_read_exact(struct spl_load_info *info, loff_t off,
return 0;
}
+/**
+ * imagemap_extend() - Grow a region this device allocated
+ *
+ * The region keeps its reservation and its record until the larger one
+ * is reserved and read, so a failure leaves the existing mapping valid.
+ *
+ * @priv: The imagemap uclass data
+ * @r: Region to grow, with lmb_reserved set
+ * @img_offset: Byte offset within the source image
+ * @size: Number of bytes wanted from @img_offset
+ * Return: pointer to the data at @img_offset, or ERR_PTR on failure
+ */
+static void *imagemap_extend(struct imagemap_priv *priv,
+ struct imagemap_region *r, loff_t img_offset,
+ ulong size)
+{
+ phys_size_t old_alloc = ALIGN(r->size, ARCH_DMA_MINALIGN);
+ ulong bl_len = spl_get_bl_len(&priv->info);
+ ulong overhead = img_offset & (bl_len - 1);
+ ulong read_size = ALIGN(size + overhead, bl_len);
+ phys_addr_t old_addr = map_to_sysmem(r->ram);
+ phys_size_t alloc_size, tail_size;
+ phys_addr_t addr, tail;
+ void *base;
+ int ret;
+
+ alloc_size = ALIGN(read_size, ARCH_DMA_MINALIGN);
+ tail_size = alloc_size - old_alloc;
+ tail = old_addr + old_alloc;
+
+ ret = 0;
+ if (tail_size)
+ ret = lmb_alloc_mem(LMB_MEM_ALLOC_ADDR, 0, &tail, tail_size,
+ LMB_NONE);
+ if (!ret) {
+ base = map_sysmem(old_addr, alloc_size);
+ ret = spl_load_region(&priv->info, r->img_offset + r->size,
+ read_size - r->size,
+ (char *)base + r->size);
+ if (ret < 0) {
+ if (tail_size)
+ lmb_free(tail, tail_size, LMB_NONE);
+ goto err_read;
+ }
+ r->size = read_size;
+
+ return base + overhead;
+ }
+
+ if (lmb_alloc_mem(LMB_MEM_ALLOC_ANY, ARCH_DMA_MINALIGN, &addr,
+ alloc_size, LMB_NONE)) {
+ log_err("imagemap: LMB alloc failed (0x%lx bytes)\n",
+ (ulong)alloc_size);
+ return ERR_PTR(-ENOMEM);
+ }
+ base = map_sysmem(addr, alloc_size);
+
+ ret = spl_load_region(&priv->info, img_offset, size, base);
+ if (ret < 0) {
+ lmb_free(addr, alloc_size, LMB_NONE);
+ goto err_read;
+ }
+
+ lmb_free(old_addr, old_alloc, LMB_NONE);
+ r->size = read_size;
+ r->ram = base;
+
+ return base + overhead;
+
+err_read:
+ log_err("imagemap: read failed at offset 0x%llx (size 0x%lx): %d\n",
+ (unsigned long long)img_offset, size, ret);
+
+ return ERR_PTR(ret);
+}
+
void *imagemap_map(struct udevice *dev, loff_t img_offset, ulong size)
{
struct imagemap_priv *priv = dev_get_uclass_priv(dev);
@@ -215,10 +291,8 @@ void *imagemap_map(struct udevice *dev, loff_t img_offset, ulong size)
ulong overhead = img_offset & (bl_len - 1);
loff_t base_off = img_offset - overhead;
ulong read_size = ALIGN(size + overhead, bl_len);
- phys_addr_t old_addr;
phys_addr_t addr;
phys_size_t alloc_size;
- ulong old_size;
void *base;
int ret;
struct imagemap_region *r;
@@ -231,62 +305,19 @@ void *imagemap_map(struct udevice *dev, loff_t img_offset, ulong size)
alloc_size = ALIGN(read_size, ARCH_DMA_MINALIGN);
- /*
- * Extend a block-aligned region at the same base offset to the
- * larger size, re-reading the full range from storage.
- */
alist_for_each(r, &priv->regions) {
- if (r->img_offset == base_off && r->size < read_size) {
- /*
- * Only a region this device allocated may be resized;
- * one recorded by imagemap_map_to() lives at an address
- * the caller chose and owns.
- */
- if (!r->lmb_reserved)
- break;
-
- old_size = r->size;
- old_addr = map_to_sysmem(r->ram);
- addr = old_addr;
-
- lmb_free(addr, ALIGN(old_size, ARCH_DMA_MINALIGN),
- LMB_NONE);
+ if (r->img_offset != base_off || r->size >= read_size)
+ continue;
- /* Try to re-reserve at the same address with new size */
- if (lmb_alloc_mem(LMB_MEM_ALLOC_ADDR, 0, &addr,
- alloc_size, LMB_NONE)) {
- /* In-place extend failed, allocate elsewhere */
- if (lmb_alloc_mem(LMB_MEM_ALLOC_ANY,
- ARCH_DMA_MINALIGN,
- &addr, alloc_size,
- LMB_NONE)) {
- log_err("imagemap: LMB alloc failed (0x%lx bytes)\n",
- (ulong)alloc_size);
- return ERR_PTR(-ENOMEM);
- }
- }
- base = map_sysmem(addr, alloc_size);
-
- if (addr == old_addr)
- ret = spl_load_region(&priv->info,
- base_off + old_size,
- read_size - old_size,
- (char *)base + old_size);
- else
- ret = spl_load_region(&priv->info, img_offset,
- size, base);
- if (ret < 0) {
- log_err("imagemap: read failed at offset 0x%llx (size 0x%lx): %d\n",
- (unsigned long long)img_offset, size, ret);
- lmb_free(addr, alloc_size, LMB_NONE);
- return ERR_PTR(ret);
- }
- r->size = read_size;
- r->ram = base;
- r->lmb_reserved = true;
-
- return base + overhead;
- }
+ /*
+ * Only a region this device allocated may be resized;
+ * one recorded by imagemap_map_to() lives at an address
+ * the caller chose and owns.
+ */
+ if (!r->lmb_reserved)
+ break;
+
+ return imagemap_extend(priv, r, img_offset, size);
}
/* New region - allocate from LMB */
--
2.56.0
More information about the openwrt-devel
mailing list