Skip to content

Support dynamic allocation of sys_{mutex,sem} #36228

@stephanosio

Description

@stephanosio

Is your enhancement proposal related to a problem? Please describe.
The sys_mutex and sys_sem mechanisms, unlike the kernel equivalent (k_mutex and k_sem), do not support run-time dynamic allocation, and must be statically allocated at the build time to work properly.

These mechanisms exist to provide abstraction for the synchronisation primitives such that their performance impact can be minimised (i.e. it internally uses k_mutex and k_sem when CONFIG_USERSPACE=n, and atomic integer and k_futex when CONFIG_USERSPACE=y).

Being unable to dynamically allocate these locks imposes severe restrictions for their practical use and forces applications to use k_mutex and k_sem instead: for instance, any OOP class-like dynamically-instantiable primitives containing synchronisation objects (e.g. dynamically instantiated MQTT client containing a mutex) will have to use k_mutex and k_sem with k_object_alloc instead of using sys_mutex and sys_sem.

This leads to severe performance impact and defeats the purpose of having the sys_mutex and sys_sem abstractions in the first place.

Describe the solution you'd like

Background information
In theory, both sys_mutex and sys_sem should be using k_futex; but, due to the limitations of the current Zephyr kernel implementation, only sys_sem does.

Prior to implementing one of the following solutions, the sys_mutex should be updated to use k_futex first (corresponds to the issue #15138). The following solutions assume that sys_mutex has been updated to use k_futex.

Solution 1 (ideal)

  • Rework the k_futex mechanism such that struct z_futex_data resides in user-accessible memory location (arbitrary futex pointers #25497).
  • Embed struct z_futex_data into struct sys_mutex and struct sys_sem.

Solution 2 (if we don't make progress on #25497)

  • Keep kernel-side struct z_futex_data (i.e. assuming arbitrary futex pointers #25497 is not implemented).
  • Make struct z_futex_data kernel object dynamically allocable.
  • Add a kernel API to link a dynamically allocated kernel object (i.e. z_futex_data) to a dynamically allocated user mode object (i.e. k_futex).

DISCLAIMER: The "solutions" suggested above are meant serve as only general guideline for resolving this issue. Some very in-depth details may not be fully correct.

Additional context
See #36201 (comment) for the analysis on the status quo.

Metadata

Metadata

Labels

EnhancementChanges/Updates/Additions to existing featuresarea: Kernel

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions