Repository navigation
ipc4: logging: make the mtrace hook safe in any context #11332
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
kv2019i
merged 1 commit into
thesofproject:main
from
serhiy-katsyuba-intel:mtrace_log_hook_spinlock
Oct 23, 2026
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, can you @serhiy-katsyuba-intel see how this is handled in other backends (in Zephyr), and whether this is safe for immediate mode logging? IOW, are we really, really sure this is called in ISR only in panic?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry, used AI to answer. Hope that's OK :) The answer is lengthy, but it seems a pity to cut it.
In SOF's deferred logging mode, the hook only runs in ISR context during a panic. In immediate mode it also runs from ordinary ISRs. That case is still safe: the log data is written either way, and only the BUFFER_STATUS notification for those bytes is postponed. That postponement already happens today for logs written on cores 1–3. Before this PR, immediate mode with ISR logging hit the "mutexes cannot be used inside ISRs" assertion, so the PR makes that case strictly better. Zephyr backends normally handle this differently, through the .panic() callback; details below.
When is the hook called in ISR context?
The hook is invoked from the mtrace backend's
char_out(), i.e. whereverlog_process()/msg_process()runs:app/prj.conf)LOG_PANIC()z_log_msg_post_finalize())CONFIG_LOG_MODE_IMMEDIATE)z_log_msg_commit()→msg_process())In SOF builds,
LOG_PANIC()is reached only on the fatal path:z_fatal_error()runscoredump()first, and the coredump logging backend callsLOG_PANIC(), thenk_sys_fatal_error_handler()calls it again. (ztest also calls it at the end of a run, from thread context.) So in deferred mode, yes, the hook runs in ISR context only in panic.No in-tree SOF config combines immediate mode with mtrace (ACP boards don't use mtrace; the PTL/WCL sim configs set
LOG_BACKEND_ADSP_MTRACE=n), but it is a common local debug setting, so it is worth covering.Is it safe in immediate mode?
Yes. With the
k_is_in_isr()early return, a log from a regular ISR still goes to the slot:mtrace_out()copies the data and advancesdsp_ptrbefore the hook is called. Only theBUFFER_STATUSaccounting and notification are skipped. The host readsdsp_ptrfrom the slot, so those bytes become visible with the next notification: the next core-0 thread log, the aging work, orEXCEPTION_CAUGHT. This is exactly what already happens for logs from secondary cores (the existing TODO inmtrace_log_hook_unlocked()). The only cost is notification latency for long ISR-only log bursts.Before this PR, the same case (immediate mode + log from ISR) hit the
"mutexes cannot be used inside ISRs"assertion, so this is strictly an improvement.Notifying from a non-panic ISR would also mostly work: spinlocks and
k_work_schedule_for_queue()are ISR-safe. The problem is that the hook has no way to tell a panic from a regular ISR. In panic, the backend forcesspace_left = 0, but that value also means a full buffer.How other Zephyr backends handle this
None of them use
k_is_in_isr(); they all rely on the.panic()callback:uartin_panic, switches from asyncuart_tx()touart_poll_out()rttpanic_mode; sync mode (immediate or panic) busy-waits instead ofk_msleep()adsp(winstream), SOFprobelog_backend_std_panic(), lock-free synchronous outputnet,ws,blepanic_mode→process()returns early (the stack can't run anymore)fsThe mtrace backend's
.panic()does the opposite: it setsmtrace_panic_modeand keeps calling the hook withspace_leftforced to 0 to get an immediate notification. It assumes the hook is panic-safe but doesn't tell the hook it is in panic. On Xtensa every fatal path enters through the exception vector, which incrementsnested, sok_is_in_isr()is a reliable signal for that.Possible follow-up
A cleaner long-term fix would be in Zephyr's
log_backend_adsp_mtrace.c: either pass a panic flag to the hook, or skip the hook in panic mode. SOF doesn't need the backend's panic-time notification, because the host takes the final write pointer onEXCEPTION_CAUGHT(ipc4_mtrace_fw_crashed()). The hook could then check panic explicitly instead of usingk_is_in_isr()as a proxy.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks @serhiy-katsyuba-intel ! I think agent replies are perfectly ok, especially if you have done a human sanity check on the output (it's often just spot on, but sometimes completely off, so a sanity check before posting is appreciated so we don't waste precious non-agent cycles many times).