Skip to content

fast-get: minor fixes#10653

Open
lyakh wants to merge 4 commits intothesofproject:mainfrom
lyakh:fastget
Open

fast-get: minor fixes#10653
lyakh wants to merge 4 commits intothesofproject:mainfrom
lyakh:fastget

Conversation

@lyakh
Copy link
Copy Markdown
Collaborator

@lyakh lyakh commented Mar 25, 2026

fix a wrong heap pointer and follow-up on recent changes to remove superfluous code

@lyakh lyakh force-pushed the fastget branch 2 times, most recently from 0ef48e3 to 59b3204 Compare March 26, 2026 07:11
@lyakh lyakh requested a review from softwarecki March 26, 2026 09:29
@lgirdwood
Copy link
Copy Markdown
Member

@lyakh #10639 merged, can be non draft now ?

Comment thread zephyr/lib/fast-get.c
k_spinlock_key_t key;
void *ret;

key = k_spin_lock(&data->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.

Would it make sense to switch this from a spinlock to a mutex? Right now the spinlock covers a relatively large scope, including array iteration and memory allocation.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

@softwarecki I think so, yes, but should be a separate commit, feel free to make a PR, or I can do it later

Copy link
Copy Markdown
Collaborator

@softwarecki softwarecki left a comment

Choose a reason for hiding this comment

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

Memory domain retrieval for the proxy variant needs fixing. The proxy already stores a pointer to the memory domain in its context structure. The struct k_mem_domain *mdom; in the struct processing_module structure is only needed for APPLICATION variant.

Comment thread zephyr/lib/fast-get.c Outdated
@wjablon1
Copy link
Copy Markdown
Contributor

wjablon1 commented Apr 15, 2026

Looks like these aren't just "minor fixes". This condition is crucial:
if (size > FAST_GET_MAX_COPY_SIZE && current_is_userspace) {

Without it I observe partition leaked because the corresponding fast_put is called with mdom=NULL.

@lyakh could you please resume work on this? I already spoke to @softwarecki. He agreed to resolve potential conflicts in his LLEXT changes.

@lyakh lyakh marked this pull request as ready for review April 17, 2026 13:26
Copilot AI review requested due to automatic review settings April 17, 2026 13:26
@lyakh
Copy link
Copy Markdown
Collaborator Author

lyakh commented Apr 17, 2026

Memory domain retrieval for the proxy variant needs fixing. The proxy already stores a pointer to the memory domain in its context structure. The struct k_mem_domain *mdom; in the struct processing_module structure is only needed for APPLICATION variant.

@softwarecki not sure which code in this PR you're referring to, could you clarify please?

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

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 updates the fast-get userspace handling to fix a heap free call and simplify tracking of userspace vs kernel callers, introducing a small RTOS abstraction helper to detect whether a thread is a userspace thread.

Changes:

  • Remove per-entry memory-domain tracking from fast-get entries and update access-grant logic to key off “current thread is userspace”.
  • Fix an incorrect heap argument passed to sof_heap_free() on an error path.
  • Add thread_is_userspace() helpers to the Zephyr and POSIX RTOS wrapper headers.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.

File Description
zephyr/lib/fast-get.c Simplifies userspace access-grant logic and fixes heap free argument; updates logging and partition-existence checks.
zephyr/include/rtos/kernel.h Adds thread_is_userspace() for Zephyr builds.
posix/include/rtos/kernel.h Adds a POSIX stub thread_is_userspace() for non-userspace builds.

Comment on lines +31 to +35
struct k_thread;
static inline bool thread_is_userspace(struct k_thread *thread)
{
return false;
}
Copy link

Copilot AI Apr 17, 2026

Choose a reason for hiding this comment

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

thread_is_userspace() introduces the bool type in this header, but the file does not include <stdbool.h>, which will break compilation for any TU that includes this header. Also, the thread parameter is unused and may trigger -Wunused-parameter when the function is referenced; add (void)thread; (or an existing unused-arg macro) to silence warnings.

Copilot uses AI. Check for mistakes.
Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

fixed

Comment thread zephyr/lib/fast-get.c
Comment on lines +232 to 241
if (size > FAST_GET_MAX_COPY_SIZE && current_is_userspace) {
/* Otherwise we've allocated on thread's heap, so it already has access */
int err = fast_get_access_grant(entry->mdom, ret, size);
int err = fast_get_access_grant(k_current_get()->mem_domain_info.mem_domain,
ret, size);

if (err < 0) {
LOG_ERR("failed to grant access err=%d", err);
sof_heap_free(NULL, ret);
sof_heap_free(heap, ret);
ret = NULL;
goto out;
Copy link

Copilot AI Apr 17, 2026

Choose a reason for hiding this comment

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

On the fast_get_access_grant() failure path, entry->size and entry->sram_ptr have already been set, but the function returns with ret = NULL and leaves the entry partially initialized. This can cause subsequent fast_get() calls to hit entry->sram_ptr and then fail the mismatch check (or repeatedly reuse the same slot), effectively breaking fast-get until reboot. Delay assigning entry->{size,sram_ptr} until after all fallible steps succeed, or explicitly clear those fields before goto out on error.

Copilot uses AI. Check for mistakes.
Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

not introduced by this PR, but added a commit to fix that too

lyakh added 4 commits April 17, 2026 17:07
Checking number of memory domain partitions in thread's domain to
determine whether it's a userspace thread isn't reliable and is a
layering violation. Instead check the K_USER flag which indicates
exactly that. Also add a missing check when adding a partition for
the newly allocated entry.

Suggested-by: Adrian Warecki <adrian.warecki@intel.com>
Signed-off-by: Guennadi Liakhovetski <guennadi.liakhovetski@linux.intel.com>
Use the same heap to free the buffer as the one, used to allocate it.

Signed-off-by: Guennadi Liakhovetski <guennadi.liakhovetski@linux.intel.com>
The mdom member of struct sof_fast_get_entry is redundant, remove it.

Signed-off-by: Guennadi Liakhovetski <guennadi.liakhovetski@linux.intel.com>
When fast_get() fails it shouldn't leave partially overwritten
entries behind. Also fix the call to memcpy_s() to use the proper
destination size.

Signed-off-by: Guennadi Liakhovetski <guennadi.liakhovetski@linux.intel.com>
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.

5 participants