Memory alignment issue. mutex has to ablligned?

I have a class that wraps the Zephyr implementation of a mutex. For some reason, when the code runs, I get this error:

ASSERTION FAIL [mutex->lock_count > 0U] @ WEST_TOPDIR/zephyr/kernel/mutex.c:239

If I add __align(8) to the wrapper class, the problem is fixed and the code runs perfectly.
Does Zephyr's mutex implementation have to be aligned?

Thanks in advance, 
Shlomo

Parents
  • Hi Shlomo,

    I do not see an immediate reason why you would ge tth eassert on line 249 in mutex.c due to alignment issues. I suspect that the root cause is something else, perhaps a memory corruption issue of some sort caused the mutex to be overwritten so that the lock count was set to 0 when it should not? Do you see more in the log? And can  you share more details about your project where you reproduce this?

    Br,

    Einar

  • yep, I have figured it out, the mutex was inlocking a mutex in a class which was deleted by another thread.
    Still, all of this saga led me to realize that there are two ways to create a mutex in zephyr, with:

    K_MUTEX_DEFINE(my_mutex); 

    or with 

    struct k_mutex my_mutex;
    
    k_mutex_init(&my_mutex);

    but the macro opens up to:

    __aligned(__alignof(struct k_mutex)) struct k_mutex my_mutex
    __in_section(_k_mutex, static, _CONCAT(my_mutex,_))
    __used __noasan
    = {
    .wait_q = Z_WAIT_Q_INIT(&my_mutex.wait_q),
    .owner = NULL,
    .lock_count = 0,
    .owner_orig_prio= K_LOWEST_APPLICATION_THREAD_PRIO,
    };

    as you can see in this case the mutex is aligned.
    why in one case it has to be aligned and in the other it doesn't?

     

Reply
  • yep, I have figured it out, the mutex was inlocking a mutex in a class which was deleted by another thread.
    Still, all of this saga led me to realize that there are two ways to create a mutex in zephyr, with:

    K_MUTEX_DEFINE(my_mutex); 

    or with 

    struct k_mutex my_mutex;
    
    k_mutex_init(&my_mutex);

    but the macro opens up to:

    __aligned(__alignof(struct k_mutex)) struct k_mutex my_mutex
    __in_section(_k_mutex, static, _CONCAT(my_mutex,_))
    __used __noasan
    = {
    .wait_q = Z_WAIT_Q_INIT(&my_mutex.wait_q),
    .owner = NULL,
    .lock_count = 0,
    .owner_orig_prio= K_LOWEST_APPLICATION_THREAD_PRIO,
    };

    as you can see in this case the mutex is aligned.
    why in one case it has to be aligned and in the other it doesn't?

     

Children
No Data
Related