stand/fdt: Scale blob size better as overlays apply

Currently, our overlay blob will grow to include the size of the complete
overlay blob we're applying. This doesn't scale very well with a lot of
overlays- they tend to include a lot of overhead, and they will generally
only add a fraction of their total size to the blob they're being applied
to.

To combat this, pack the blob as we apply new overlays and keep track of how
many overlays we've applied. Only ubldr has any fixups to be applied after
overlays, so we only need to re-pad the blob in ubldr. Presumably the
allocation won't fail since we just did a lot worse in trying to apply
overlays and succeeded.

I have no intention of removing the padding in make_dtb.sh. There might be
an argument to be had over whether it should be configurable, since ubldr
*is* the only loader that actually has fixups to be applied and we can do
this at runtime, but I'm not too concerned about this.

This diff has been sitting in Phabricator for a year and a half, but I've
decided to flush it as it does make sure that we're scaling the blob
appropriately and leave room at the end for fixups in case of some freak
circumstance where applying overlays leaves us with a blob of insufficient
size.

Reviewed by:	gonzo (a long time ago)
MFC after:	1 week
Differential Revision:	https://reviews.freebsd.org/D14133
This commit is contained in:
Kyle Evans 2020-01-09 04:34:42 +00:00
parent 39eae263cd
commit cc03366097
3 changed files with 39 additions and 9 deletions

View File

@ -427,7 +427,10 @@ fdt_check_overlay_compatible(void *base_fdt, void *overlay_fdt)
return (1); return (1);
} }
void /*
* Returns the number of overlays successfully applied
*/
int
fdt_apply_overlays() fdt_apply_overlays()
{ {
struct preloaded_file *fp; struct preloaded_file *fp;
@ -436,13 +439,13 @@ fdt_apply_overlays()
void *current_fdtp; void *current_fdtp;
void *next_fdtp; void *next_fdtp;
void *overlay; void *overlay;
int rv; int overlays_applied, rv;
if ((fdtp == NULL) || (fdtp_size == 0)) if ((fdtp == NULL) || (fdtp_size == 0))
return; return (0);
if (fdt_overlays_applied) if (fdt_overlays_applied)
return; return (0);
max_overlay_size = 0; max_overlay_size = 0;
for (fp = file_findfile(NULL, "dtbo"); fp != NULL; fp = fp->f_next) { for (fp = file_findfile(NULL, "dtbo"); fp != NULL; fp = fp->f_next) {
@ -452,15 +455,16 @@ fdt_apply_overlays()
/* Nothing to apply */ /* Nothing to apply */
if (max_overlay_size == 0) if (max_overlay_size == 0)
return; return (0);
overlay = malloc(max_overlay_size); overlay = malloc(max_overlay_size);
if (overlay == NULL) { if (overlay == NULL) {
printf("failed to allocate memory for DTB blob with overlays\n"); printf("failed to allocate memory for DTB blob with overlays\n");
return; return (0);
} }
current_fdtp = fdtp; current_fdtp = fdtp;
current_fdtp_size = fdtp_size; current_fdtp_size = fdtp_size;
overlays_applied = 0;
for (fp = file_findfile(NULL, "dtbo"); fp != NULL; fp = fp->f_next) { for (fp = file_findfile(NULL, "dtbo"); fp != NULL; fp = fp->f_next) {
COPYOUT(fp->f_addr, overlay, fp->f_size); COPYOUT(fp->f_addr, overlay, fp->f_size);
/* Check compatible first to avoid unnecessary allocation */ /* Check compatible first to avoid unnecessary allocation */
@ -493,7 +497,9 @@ fdt_apply_overlays()
if (current_fdtp != fdtp) if (current_fdtp != fdtp)
free(current_fdtp); free(current_fdtp);
current_fdtp = next_fdtp; current_fdtp = next_fdtp;
current_fdtp_size = next_fdtp_size; fdt_pack(current_fdtp);
current_fdtp_size = fdt_totalsize(current_fdtp);
overlays_applied++;
} else { } else {
/* /*
* Assume here that the base we tried to apply on is * Assume here that the base we tried to apply on is
@ -513,6 +519,26 @@ fdt_apply_overlays()
} }
free(overlay); free(overlay);
fdt_overlays_applied = 1; fdt_overlays_applied = 1;
return (overlays_applied);
}
int
fdt_pad_dtb(size_t padding)
{
void *padded_fdtp;
size_t padded_fdtp_size;
padded_fdtp_size = fdtp_size + padding;
padded_fdtp = malloc(padded_fdtp_size);
if (padded_fdtp == NULL)
return (1);
if (fdt_open_into(fdtp, padded_fdtp, padded_fdtp_size) != 0) {
free(padded_fdtp);
return (1);
}
fdtp = padded_fdtp;
fdtp_size = padded_fdtp_size;
return (0);
} }
int int

View File

@ -43,7 +43,8 @@ void fdt_fixup_cpubusfreqs(unsigned long, unsigned long);
void fdt_fixup_ethernet(const char *, char *, int); void fdt_fixup_ethernet(const char *, char *, int);
void fdt_fixup_memory(struct fdt_mem_region *, size_t); void fdt_fixup_memory(struct fdt_mem_region *, size_t);
void fdt_fixup_stdout(const char *); void fdt_fixup_stdout(const char *);
void fdt_apply_overlays(void); int fdt_apply_overlays(void);
int fdt_pad_dtb(size_t);
int fdt_load_dtb_addr(struct fdt_header *); int fdt_load_dtb_addr(struct fdt_header *);
int fdt_load_dtb_file(const char *); int fdt_load_dtb_file(const char *);
void fdt_load_dtb_overlays(const char *); void fdt_load_dtb_overlays(const char *);

View File

@ -63,6 +63,8 @@ fdt_platform_load_from_ubenv(const char *var)
return (1); return (1);
} }
#define FDT_DTB_PADSZ 1024
int int
fdt_platform_load_dtb(void) fdt_platform_load_dtb(void)
{ {
@ -127,7 +129,8 @@ fdt_platform_fixups(void)
ethstr = NULL; ethstr = NULL;
/* Apply overlays before anything else */ /* Apply overlays before anything else */
fdt_apply_overlays(); if (fdt_apply_overlays() > 0)
fdt_pad_dtb(FDT_DTB_PADSZ);
/* Acquire sys_info */ /* Acquire sys_info */
si = ub_get_sys_info(); si = ub_get_sys_info();