From a2d83c68a3ea6f2017fa9eabda72dfbafae0fc37 Mon Sep 17 00:00:00 2001 From: Raul E Rangel Date: Thu, 22 Jul 2021 11:16:19 -0600 Subject: [PATCH] lib/thread,lib/hardwaremain: Lazy initialize threads By lazy initializing the threads, if a stage doesn't use them, they will be garbage collected. BUG=b:179699789 TEST=Boot guybrush to the OS and verify threads worked Suggested-by: Julius Werner Signed-off-by: Raul E Rangel Change-Id: I7208ffb5dcda63d916bc6cfdea28d92a62435da6 Reviewed-on: https://review.coreboot.org/c/coreboot/+/56532 Reviewed-by: Furquan Shaikh Tested-by: build bot (Jenkins) --- src/include/thread.h | 3 --- src/lib/hardwaremain.c | 1 - src/lib/thread.c | 11 ++++++++++- 3 files changed, 10 insertions(+), 5 deletions(-) diff --git a/src/include/thread.h b/src/include/thread.h index ce250b8520..3c06cc5311 100644 --- a/src/include/thread.h +++ b/src/include/thread.h @@ -51,8 +51,6 @@ struct thread { struct thread_handle *handle; }; -void threads_initialize(void); - /* Return 0 on successful yield, < 0 when thread did not yield. */ int thread_yield(void); @@ -88,7 +86,6 @@ asmlinkage void switch_to_thread(uintptr_t new_stack, uintptr_t *saved_stack); void arch_prepare_thread(struct thread *t, asmlinkage void (*thread_entry)(void *), void *arg); #else -static inline void threads_initialize(void) {} static inline int thread_yield(void) { return -1; diff --git a/src/lib/hardwaremain.c b/src/lib/hardwaremain.c index a239f8f238..a6ab66347e 100644 --- a/src/lib/hardwaremain.c +++ b/src/lib/hardwaremain.c @@ -456,7 +456,6 @@ void main(void) /* Handoff sleep type from romstage. */ acpi_is_wakeup_s3(); - threads_initialize(); /* Initialise GNVS early. */ if (CONFIG(ACPI_SOC_NVS)) diff --git a/src/lib/thread.c b/src/lib/thread.c index 413e5b4bec..2ff09a50d5 100644 --- a/src/lib/thread.c +++ b/src/lib/thread.c @@ -257,13 +257,16 @@ static void *thread_alloc_space(struct thread *t, size_t bytes) return (void *)t->stack_current; } -void threads_initialize(void) +static void threads_initialize(void) { int i; struct thread *t; u8 *stack_top; struct cpu_info *ci; + if (initialized) + return; + /* `cpu_info()` requires the stacks to be STACK_SIZE aligned */ assert(IS_ALIGNED((uintptr_t)thread_stacks, CONFIG_STACK_SIZE)); @@ -295,6 +298,9 @@ int thread_run(struct thread_handle *handle, enum cb_err (*func)(void *), void * struct thread *current; struct thread *t; + /* Lazy initialization */ + threads_initialize(); + current = current_thread(); if (!thread_can_yield(current)) { @@ -327,6 +333,9 @@ int thread_run_until(struct thread_handle *handle, enum cb_err (*func)(void *), if (!ENV_RAMSTAGE) dead_code(); + /* Lazy initialization */ + threads_initialize(); + current = current_thread(); if (!thread_can_yield(current)) {