-
Notifications
You must be signed in to change notification settings - Fork 8.2k
libc: newlib: make sbrk() thread-safe #17552
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
45b8864 to
1739341
Compare
|
CI failure is broken in master branch, I'm opening a bug |
lib/libc/newlib/libc-hooks.c
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe a spinlock (which becomes an irq_lock on uniprocessor systems) would be a better fit here? This is a fast operation with bounded runtime. There's no real benefit to trying to block a process on it and significant extra code involved in sucking in the mutex implementation.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This code has to run in user mode, so a spinlock is not appropriate.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Futex then? :)
Just seems like the (comparatively very heavyweight, especially when done via syscalls) blocking mechanism is exactly what we don't want when synchronizing a tiny bit of deterministic code in the guts of the allocator.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I thought sys_mutex was the priority inheriting thing (which we don't use much from elsewhere in the kernel, will suck in non-trivial extra code for all newlib targets, and which is known to have SMP bugs)?
You win.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I thought sys_mutex was the priority inheriting thing
It is. It is backed by a k_mutex on the kernel side. The design of sys mutex is to have the same semantics as FUTEX_LOCK_PI / FUTEX_UNLOCK_PI
which is known to have SMP bugs
Which bugs are these?? We should label them high priority and fix them...
will suck in non-trivial extra code for all newlib targets
What do you want here?
This code in its current state is broken.
Should I use a sys_sem instead?
|
Discussed some more offline, going to use a one-count sys_sem instead of a sys_mutex. Need to wait until the sys_sem patch is merged #16523 |
1739341 to
523e884
Compare
|
Found the following issues, please fix and resubmit: checkpatch issues |
|
Changed to a sys_sem instead of a mutex. |
This can't be done at toplevel. |
523e884 to
67633e0
Compare
67633e0 to
23c9dbd
Compare
We need a SYS_SEM_DEFINE() that works just like K_SEM_DEFINE(). Signed-off-by: Andrew Boie <[email protected]>
Concurrent use of this function could lead to corruption. Use a sys_sem to synchronize access. Signed-off-by: Andrew Boie <[email protected]>
23c9dbd to
9a5490e
Compare
|
@andrewboie may I ask why you haven't used the Perhaps a separate issue but closely related to this as malloc_lock / unlock require a reentrancy struct. The documentation within newlib's reent.h instructs us to either provide reentrant versions of all syscalls and manage reentrancy structures ourself, or to keep a |
I didn't know they existed.
@kaidoho Zephyr's newlib integration predates my involvement with the project -- I'm not the original author. I fixed the concurrency issue with sbrk() in a way I knew how, but if there's a better way to do some things please file a GH issue (or send patches) |
|
Ok, will file an GH issue. Thx for your response @andrewboie |
Concurrent use of this function could lead to corruption.
Add a semaphore to synchronize this.
Signed-off-by: Andrew Boie [email protected]