Skip to content

Add lock to mod resources tracking#10960

Open
jsarha wants to merge 3 commits into
thesofproject:mainfrom
jsarha:add_lock_to_mod_resources_tracking
Open

Add lock to mod resources tracking#10960
jsarha wants to merge 3 commits into
thesofproject:mainfrom
jsarha:add_lock_to_mod_resources_tracking

Conversation

@jsarha

@jsarha jsarha commented Jun 25, 2026

Copy link
Copy Markdown
Contributor

This PR is a result from the discussion in this PR: #10862

Jyri Sarha added 3 commits June 25, 2026 22:14
Convert mod_data_blob_handler_new() to a Zephyr syscall following
the same pattern as mod_alloc_ext(), mod_free(), and mod_fast_get().

This allows modules running in user mode to call the function through
the syscall interface. The z_vrfy_ handler validates the processing_module
pointer and its heap region before dispatching to the implementation.

Signed-off-by: Jyri Sarha <jyri.sarha@linux.intel.com>
Add a k_spinlock to struct module_resources and take it around every objpool
access in z_impl_mod_alloc_ext(), z_impl_mod_data_blob_handler_new(),
z_impl_mod_fast_get(), z_impl_mod_free(), and mod_free_all().

This prevents concurrent access to the resource tracking pool when a
module's IPC thread and processing thread operate in parallel (e.g.
set_large_config arriving while the module is running).

Signed-off-by: Jyri Sarha <jyri.sarha@linux.intel.com>
Remove the MEM_API_CHECK_THREAD() macro, CONFIG_MODULE_MEMORY_API_DEBUG
Kconfig option, and the rsrc_mngr field from struct module_resources.

This single-thread-owner check is now redundant: the previous commit
added a spinlock that properly serializes all accesses to the resource
pool, making the debug-only thread identity warning unnecessary.

Signed-off-by: Jyri Sarha <jyri.sarha@linux.intel.com>

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR adds locking around module resource tracking in the module adapter to allow safe resource bookkeeping when allocations/frees can occur from multiple threads (e.g., IPC thread vs DP thread). It also removes the now-obsolete thread-check Kconfig option and adjusts the blob handler creation API to follow the Zephyr syscall pattern in full Zephyr application builds.

Changes:

  • Add a k_spinlock to struct module_resources and use it to protect tracked resource allocation/free paths.
  • Convert mod_data_blob_handler_new() to a __syscall API for full Zephyr application builds (with z_impl_/z_vrfy_ and mrsh glue).
  • Remove MODULE_MEMORY_API_DEBUG Kconfig and the associated thread-check logic.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.

File Description
src/include/sof/audio/module_adapter/module/generic.h Adds spinlock to module resource struct; updates blob handler API to syscall vs z_impl_ mapping depending on build.
src/audio/module_adapter/module/generic.c Initializes and applies the new spinlock around most resource tracking operations; adds syscall verification/marshalling for blob handler creation.
src/audio/module_adapter/Kconfig Removes the legacy thread-safety debug-check option.

Comment on lines 179 to 183
struct module_resources *res = &mod->priv.resources;
struct module_resource *container;

MEM_API_CHECK_THREAD(res);

container = container_get(mod);
if (!container)
k_spin_unlock(&res->lock, key);

/* Make sure resource lists and accounting are reset */
mod_resource_init(mod);

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd protect this too

struct module_resources *res = &mod->priv.resources;

/* Init memory list */
k_spinlock_init(&res->lock);

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why a spinlock? Spinlocks disable interrupts. I'd really prefer a mutex if possible

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, the memory operations should be fast, but sure I can easily make it mutex.


MEM_API_CHECK_THREAD(res);

key = k_spin_lock(&res->lock);

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

so, I suppose we should add locking everywhere where we currently call MEM_API_CHECK_THREAD(), so I've also missed mod_balloc_align(), right?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, Copilot pointed that out too, I am already doing that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants