Skip to content

Commit 9dffac0

Browse files
teburdcarlescufi
authored andcommitted
llext: flush logging before unloading extensions
Extensions could have used logging, when log processing is deferred, the logging thread can run after the extension has been unloaded and thereby access invalid memory addresses. Make sure to flush all logs before unloading extensions. Signed-off-by: Tom Burdick <[email protected]> Signed-off-by: Guennadi Liakhovetski <[email protected]>
1 parent c09b3bc commit 9dffac0

File tree

1 file changed

+51
-0
lines changed

1 file changed

+51
-0
lines changed

subsys/llext/llext.c

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,12 +180,63 @@ int llext_load(struct llext_loader *ldr, const char *name, struct llext **ext,
180180
return ret;
181181
}
182182

183+
#include <zephyr/logging/log_ctrl.h>
184+
185+
static void llext_log_flush(void)
186+
{
187+
#ifdef CONFIG_LOG_MODE_DEFERRED
188+
extern struct k_thread logging_thread;
189+
int cur_prio = k_thread_priority_get(k_current_get());
190+
int log_prio = k_thread_priority_get(&logging_thread);
191+
int target_prio;
192+
bool adjust_cur, adjust_log;
193+
194+
/*
195+
* Our goal is to raise the logger thread priority above current, but if
196+
* current has the highest possble priority, both need to be adjusted,
197+
* particularly if the logger thread has the lowest possible priority
198+
*/
199+
if (log_prio < cur_prio) {
200+
adjust_cur = false;
201+
adjust_log = false;
202+
target_prio = 0;
203+
} else if (cur_prio == K_HIGHEST_THREAD_PRIO) {
204+
adjust_cur = true;
205+
adjust_log = true;
206+
target_prio = cur_prio;
207+
k_thread_priority_set(k_current_get(), cur_prio + 1);
208+
} else {
209+
adjust_cur = false;
210+
adjust_log = true;
211+
target_prio = cur_prio - 1;
212+
}
213+
214+
/* adjust logging thread priority if needed */
215+
if (adjust_log) {
216+
k_thread_priority_set(&logging_thread, target_prio);
217+
}
218+
219+
log_thread_trigger();
220+
k_yield();
221+
222+
if (adjust_log) {
223+
k_thread_priority_set(&logging_thread, log_prio);
224+
}
225+
if (adjust_cur) {
226+
k_thread_priority_set(&logging_thread, cur_prio);
227+
}
228+
#endif
229+
}
230+
183231
int llext_unload(struct llext **ext)
184232
{
185233
__ASSERT(*ext, "Expected non-null extension");
186234
struct llext *tmp = *ext;
187235

188236
k_mutex_lock(&llext_lock, K_FOREVER);
237+
238+
llext_log_flush();
239+
189240
__ASSERT(tmp->use_count, "A valid LLEXT cannot have a zero use-count!");
190241

191242
if (tmp->use_count-- != 1) {

0 commit comments

Comments
 (0)