Skip to content

dlt_user: Rework on dlt global mutex locks#780

Merged
minminlittleshrimp merged 1 commit intomasterfrom
dlt_devel_fix_global_mutex_locks
Nov 13, 2025
Merged

dlt_user: Rework on dlt global mutex locks#780
minminlittleshrimp merged 1 commit intomasterfrom
dlt_devel_fix_global_mutex_locks

Conversation

@minminlittleshrimp
Copy link
Collaborator

@minminlittleshrimp minminlittleshrimp commented Oct 30, 2025

  • Harden mutex handling, make global mutex and cleanup more robust
  • Fix lock-ordering, prevent lock-order inversion between components

@minminlittleshrimp
Copy link
Collaborator Author

@lti9hc kindly review

Copy link

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 pull request reworks the DLT global mutex locking mechanism to provide safe reentrant behavior and eliminate lock-order inversions. The main changes include implementing per-thread reentrancy counters using pthread keys, guarding cleanup handlers to prevent mutex underflow, and removing the problematic rwlock->mutex lock-order inversion through a compute-then-publish pattern.

Key Changes

  • Replace DLT_SEM_LOCK/DLT_SEM_FREE macros with reentrant dlt_mutex_lock()/dlt_mutex_free() functions that use per-thread counters
  • Add guarded cleanup handlers that check reentry state before unlocking to prevent mutex underflow
  • Eliminate lock-order inversion by computing trace-load settings under rwlock, then publishing pointers under DLT mutex separately

Reviewed Changes

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

File Description
src/shared/dlt_common.c Update comment to reference new mutex function name
src/lib/dlt_user.c Replace all DLT_SEM_* macros with new reentrant mutex functions and implement per-thread reentry tracking
include/dlt/dlt_user.h.in Remove old DLT_SEM_LOCK/DLT_SEM_FREE macro definitions

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@minminlittleshrimp minminlittleshrimp force-pushed the dlt_devel_fix_global_mutex_locks branch 3 times, most recently from 26f52c9 to 1938c38 Compare November 2, 2025 23:14
Copy link

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

Copilot reviewed 7 out of 7 changed files in this pull request and generated no new comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@minminlittleshrimp minminlittleshrimp changed the title dlt_user: Rework on dlt global mutex locks [Draft] dlt_user: Rework on dlt global mutex locks Nov 5, 2025
@minminlittleshrimp minminlittleshrimp force-pushed the dlt_devel_fix_global_mutex_locks branch from 1938c38 to 40e36e7 Compare November 5, 2025 21:01
Copy link

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

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


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@minminlittleshrimp minminlittleshrimp changed the title [Draft] dlt_user: Rework on dlt global mutex locks dlt_user: Rework on dlt global mutex locks Nov 6, 2025
- Harden mutex handling, make global mutex
and cleanup more robust
- Fix lock-ordering, prevent lock-order
inversion between components

Signed-off-by: LUU QUANG MINH <Minh.LuuQuang@vn.bosch.com>
@minminlittleshrimp minminlittleshrimp force-pushed the dlt_devel_fix_global_mutex_locks branch from 40e36e7 to 067afa7 Compare November 10, 2025 08:26
Copy link

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

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


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

ctx_entry->contextID);
pthread_rwlock_unlock(&trace_load_rw_lock);

dlt_mutex_lock();
Copy link
Collaborator

Choose a reason for hiding this comment

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

why do you want to use mutex lock here?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

trace_load_settings is a defined pointer included inside dlt_ll_ts_type, it is part of Dlt_user, at line 5034, an assignment of the pointer dlt_ll_ts from the struct to ctx_entry is performed, hence as the comment says, it should be protected with global mutex

}

#ifdef DLT_TRACE_LOAD_CTRL_ENABLE
pthread_rwlock_wrlock(&trace_load_rw_lock);
Copy link
Collaborator

Choose a reason for hiding this comment

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

It should not remove trace load lock
I suggest that using lock ordering
ex:
mutex_lock
trace load lock
trace load unlock
mutex unlock

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

The removal of trace load rwlock here is correct since the inside code block only protecting Dlt_user struct, no related trace load setting struct

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

rwlock shall be refactored after this merge, due to the fact that rwlock is abused (less read more write) and thus atomic shall be considered. The scope of this patch focus mainly on fixing bug of double lock, double free and mutex inversion ABBA from dlt_mutex and rwlock.

permitted in pid=46659
[17569.258894]~DLT~46659~ERROR    ~DLT sem free error: 1 - Operation not permitted in pid=46659
Send 99999 Hello
[17569.258896]~DLT~46659~ERROR    ~DLT sem lock error: 35 - Resource deadlock avoided pid=46659
[17569.258898]~DLT~46659~ERROR    ~DLT sem lock error: 35 - Resource deadlock avoided pid=46659
[17569.258902]~DLT~46659~ERROR    ~DLT sem free error: 1 - Operation not permitted in pid=46659
[17569.258906]~DLT~46659~ERROR    ~DLT sem free error: 1 - Operation not permitted in pid=46659
[17570.259482]~DLT~46659~ERROR    ~DLT sem free error: 1 - Operation not permitted in pid=46659
[17570.259500]~DLT~46659~ERROR    ~DLT sem free error: 1 - Operation not permitted in pid=46659

@minminlittleshrimp minminlittleshrimp merged commit 9c37397 into master Nov 13, 2025
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Semaphore issue "ERROR ~DLT sem free error: 1 - Operation not permitted" on QNX

3 participants