Commit Graph

65364 Commits

Author SHA1 Message Date
Vladimir Serbinenko
d1e42115d0 i2c_hid: Don't crash on bogus i2c devices
Chromebook elemi has no touchscreen but coreboot still declares one in ACPI
tables. Fail gracefully rather than hard crash

Change-Id: I3bad098e7c7887e86669143c0ccec5b46b1428d4
Reviewed-on: https://review.haiku-os.org/c/haiku/+/6659
Reviewed-by: Jérôme Duval <jerome.duval@gmail.com>
2023-06-27 14:24:46 +00:00
Jérôme Duval
3801ec71a3 emuxki: use user_memcpy to read/write the user buffers.
* map registers kernel only.
* alloc buffer user only.
* may help with #18475 and #18467

Change-Id: I3f739b98e0ec2d07e83c15a614b8b1d1722025e2
Reviewed-on: https://review.haiku-os.org/c/haiku/+/6657
Reviewed-by: Adrien Destugues <pulkomandy@pulkomandy.tk>
Tested-by: Commit checker robot <no-reply+buildbot@haiku-os.org>
2023-06-26 10:05:47 +00:00
Vladimir Serbinenko
1ece7e4531 PS2: Fix PS2 probing on Google Chromebook
Currently internal keyboard is not detected. Turns out
that reset sequence doesn't respond in expected way. Comparing with
Linux implementation, it doesn't check the return value of reset command but
compensates it by checking get_device_id and set_leds. Try reset test if it
doesn't work check ID

Change-Id: I5506f69a7083ab8ea6c936c8016fd59311a16368
Reviewed-on: https://review.haiku-os.org/c/haiku/+/6610
Reviewed-by: Adrien Destugues <pulkomandy@pulkomandy.tk>
Tested-by: Commit checker robot <no-reply+buildbot@haiku-os.org>
2023-06-26 06:45:30 +00:00
David Karoly
b9cca28d48 boot: implement skip-init when detecting UART from FDT
Change-Id: I012c6238b083197c8e268185904aa0ecf6641ba3
Reviewed-on: https://review.haiku-os.org/c/haiku/+/6638
Reviewed-by: Adrien Destugues <pulkomandy@pulkomandy.tk>
Tested-by: Commit checker robot <no-reply+buildbot@haiku-os.org>
2023-06-25 09:24:16 +00:00
Humdinger
55403e4b96 ShowImage: avoid trigger screen saver in full screen
ShowImage hides the mouse pointer when in full screen. This change
additionally avoids triggering the screen saver in that case, using
the same method as MediaPlayer, i.e. setting the current mouse
coordinates.

Change-Id: I151be209f02a2b8d3033766f2b7469655a5c8b61
Reviewed-on: https://review.haiku-os.org/c/haiku/+/6639
Tested-by: Commit checker robot <no-reply+buildbot@haiku-os.org>
Reviewed-by: waddlesplash <waddlesplash@gmail.com>
2023-06-25 09:23:57 +00:00
Niels Sascha Reedijk
68578fe6a8 device_manager: remove unused #include <set>
GCC 13 is more strict about what C++ features are available in a freestanding
(= build without standard library) build. The container types are not
available.

In this case the device manager does not use the std::set container.

Change-Id: Iaa2d71b40c9247fac8ccf2a0033c9fc288550dc7
Reviewed-on: https://review.haiku-os.org/c/haiku/+/6642
Reviewed-by: waddlesplash <waddlesplash@gmail.com>
2023-06-25 09:10:49 +00:00
Niels Sascha Reedijk
4958c5d7b6 HaikuBook: fix some errors and warnings during documentation generation
Change-Id: Ib6b876ff83aeba0d2346990ab2e9869cbf519284
Reviewed-on: https://review.haiku-os.org/c/haiku/+/6641
Tested-by: Commit checker robot <no-reply+buildbot@haiku-os.org>
Reviewed-by: Adrien Destugues <pulkomandy@pulkomandy.tk>
2023-06-24 22:00:27 +00:00
Autocomitter
8872deb057 Update translations from Pootle 2023-06-24 08:14:34 +00:00
Freaxed
ae3724532e AboutWindow: fixed invalid string pointer on application version
Change-Id: I6caf78fffddedca322464dcb4350ba03bb0b0207
Reviewed-on: https://review.haiku-os.org/c/haiku/+/6637
Tested-by: Commit checker robot <no-reply+buildbot@haiku-os.org>
Reviewed-by: Adrien Destugues <pulkomandy@pulkomandy.tk>
2023-06-23 02:14:32 +00:00
Alexander von Gluck IV
2bd91dc18b HaikuPortsCross: Correct misaligned bootstrap package versions
Change-Id: Ia836711b471318283a2b1ecb7c5a2e10963e8b7f
2023-06-22 10:03:00 -05:00
Alexander von Gluck IV
a165c670fd 3rdparty: Fix revision check
* Add a few nice to haves

Change-Id: I53e1fd7d067357af5ad625ebf86de3ee68903cbe
2023-06-22 10:02:29 -05:00
Alexander von Gluck IV
445d4fd926 3rdparty: Add validateBootstrapRepo tool
* Fills in a detection gap where HaikuPortsCross might reference
  packages that no longer exist in haikuports.cross
* A handy pre-bootstrap check

Change-Id: Id397ecda731a7b7d8fc3d407a4791724494611d7
2023-06-22 09:49:42 -05:00
David Karoly
b4c0589f3a kernel/arm: remove unneeded call to get_commpage_image
Change-Id: Id978ebea32ea45500ed1f30f24eaeb0017487133
Reviewed-on: https://review.haiku-os.org/c/haiku/+/6614
Reviewed-by: waddlesplash <waddlesplash@gmail.com>
Tested-by: Commit checker robot <no-reply+buildbot@haiku-os.org>
2023-06-21 19:37:10 +00:00
David Karoly
426dc0c7e4 kernel/arm: remove unneeded call to arch_thread_set_current_thread
Change-Id: I1026c733033458ed02fab9e9e61f041441e56d1e
Reviewed-on: https://review.haiku-os.org/c/haiku/+/6615
Reviewed-by: waddlesplash <waddlesplash@gmail.com>
Tested-by: Commit checker robot <no-reply+buildbot@haiku-os.org>
2023-06-21 09:17:17 +00:00
David Karoly
c629d42947 kernel/arm: remove unused barrier functions
Change-Id: I45c863c4ac027cc3ec5bb1922c22c58433603875
Reviewed-on: https://review.haiku-os.org/c/haiku/+/6613
Tested-by: Commit checker robot <no-reply+buildbot@haiku-os.org>
Reviewed-by: waddlesplash <waddlesplash@gmail.com>
2023-06-21 09:17:02 +00:00
Augustin Cavalier
46a5ba80a3 emuxki: Allocate memory for the kernel only.
May fix #18467. Untested, I don't have this hardware.
2023-06-20 16:42:02 -04:00
David Karoly
ddc88ea85a docs: remove TODO item for ARM Accessed and Modifed page flags
Change-Id: I497c054e4a58a39804e2a1ef7a5d30eb8cc73130
Reviewed-on: https://review.haiku-os.org/c/haiku/+/6611
Reviewed-by: Adrien Destugues <pulkomandy@pulkomandy.tk>
Tested-by: Commit checker robot <no-reply+buildbot@haiku-os.org>
2023-06-19 20:49:48 +00:00
Augustin Cavalier
513f86c7de user_mutex_defs: Add note that the flags area is shared with timeout flags.
No functional change.
2023-06-19 16:40:33 -04:00
Augustin Cavalier
70e8eacb35 kernel: Implement realloc_etc and make use of it. 2023-06-19 16:33:22 -04:00
Augustin Cavalier
f96456d863 kernel/vfs: Fix missing lock in fs_mount().
At least a read lock of the sVnodeLock must be held when calling
lookup_vnode, but we held none at all. This rectifies that problem.

This bug appears to have been around for many years, but no-one
noticed since ASSERT_READ_LOCKED_RW_LOCK only works with more
debug options turned on than the kernel is built with. I discovered
this while working on a new version of those additional options.
2023-06-19 14:27:48 -04:00
Augustin Cavalier
5e8058566c kernel/locks: De-duplicate two inlined methods.
The functions declared in locks.h were and are exactly identical
to these inline blocks of code. So, rather than duplicate them,
just invoke them directly. The compiler will probably inline them
anyway.
2023-06-19 13:23:22 -04:00
Trung Nguyen
79572316c4 kernel/vm: check if page is in area
Checks if a `vm_page` is part of a `VMArea` before doing work with
it, as pages in a `VMCache` that an area is a part of might not
belong to that area.

This fixes a bug for copy-on-write areas when an application is
`fork`ing.

Change-Id: Ic5683c67865b41bf3708bb7ea4104502ddf31a19
Reviewed-on: https://review.haiku-os.org/c/haiku/+/6496
Tested-by: Commit checker robot <no-reply+buildbot@haiku-os.org>
Reviewed-by: Jérôme Duval <jerome.duval@gmail.com>
Reviewed-by: Axel Dörfler <axeld@pinc-software.de>
2023-06-19 15:59:11 +00:00
Trung Nguyen
bdcc293fa8 kernel/vm: handle page protections in cut_area
- Resize the `page_protections` array in `cut_area` and also shift
the bits if necessary.
- Set the correct protection array as well as the real page
protections for the second area produced by `cut_area`.

Change-Id: I62293480487e869420ebe5a3bc729cec2a14c687
Reviewed-on: https://review.haiku-os.org/c/haiku/+/6395
Reviewed-by: Jérôme Duval <jerome.duval@gmail.com>
Tested-by: Commit checker robot <no-reply+buildbot@haiku-os.org>
2023-06-19 15:59:11 +00:00
Augustin Cavalier
c5a0df2490 user_mutex: Add "user_mutex" KDL command to dump user-mutex information.
It takes one parameter to specify a thread that is blocked on such
a user mutex.

Change-Id: I513ce130137a327cbaf305d2945e6cfe3c09879e
Reviewed-on: https://review.haiku-os.org/c/haiku/+/6606
Tested-by: Commit checker robot <no-reply+buildbot@haiku-os.org>
Reviewed-by: waddlesplash <waddlesplash@gmail.com>
2023-06-19 14:56:10 +00:00
Augustin Cavalier
fb688aa144 user_mutex: Use unwired virtual addresses when possible.
This requires the use of fault handlers in the atomics.

GLTeapot now runs in the range of 590-610 FPS on my VM. However, it still
isn't using anywhere near 100% CPU usage. Some of that may be waiting for
app_server to respond to draw requests, but a lot of it still isn't.

Change-Id: I7be87d10cb1b00f07b055d9094b77837b49c5055
Reviewed-on: https://review.haiku-os.org/c/haiku/+/6603
Tested-by: Commit checker robot <no-reply+buildbot@haiku-os.org>
Reviewed-by: waddlesplash <waddlesplash@gmail.com>
2023-06-19 14:56:10 +00:00
Augustin Cavalier
93d7d1c52c user_mutex: Per-team contexts.
This requires the introduction of the flag B_USER_MUTEX_SHARED, and then
actually using the SHARED flags in pthread structures to determine when
it should be passed through.

This commit still uses wired memory even for per-team contexts.
That will change in the next commit.

GLTeapot FPS seems about the same.

Change-Id: I749a00dcea1531e113a65299b6d6610f57511fcc
Reviewed-on: https://review.haiku-os.org/c/haiku/+/6602
Reviewed-by: waddlesplash <waddlesplash@gmail.com>
Tested-by: Commit checker robot <no-reply+buildbot@haiku-os.org>
2023-06-19 14:56:10 +00:00
Augustin Cavalier
0ab9f280ec user_mutex: Granularize locking.
Now the table is locked with a rw_lock, and individual entries have
each their own rw_locks also.

This improves the "contention" benchmark I have here (2 locks, 6 threads,
acquire & release one lock 50,000 times per thread) mentioned in the last
commit down to under 4s consistently, around 3.45s with the system idle
and around 3.9s when moving windows around. Before this commit, it was
around 6.7s in the best case and 7.0s in the worst, and before that,
it couldn't break 3.8s in the best case.

This does make GLTeapot just slightly worse: it is now down to 310-330
(with occasional dips to 300-310) from 320-340. But the following commits
will improve that substantially.

Change-Id: Ie029a2510746f876f4d4c74d7e878fdadf3cf590
Reviewed-on: https://review.haiku-os.org/c/haiku/+/6601
Reviewed-by: waddlesplash <waddlesplash@gmail.com>
Tested-by: Commit checker robot <no-reply+buildbot@haiku-os.org>
2023-06-19 14:56:10 +00:00
Augustin Cavalier
13491fd259 kernel/user_mutex: Refactor around ConditionVariable features.
* Get rid of the multiple entries/condition-variables system.
   Instead, allocate one structure per variable and do not add/remove
   it from the hash until all waiters are gone.

 * Get rid of "locked". All wait wakeups of B_OK mean "locked". All
   nonzero values mean "not locked." This mirrors what the kernel mutex
   implementation does (however, that also tracks the owning thread,
   for assertion's sake.)

 * Remove "lastWaiter" logic (for now.) As we no longer hold a lock
   after wakeup, we cannot reliably check and act on it outside the
   "wait" function. This means that interrupted or timed-out waits
   will cause a potentially unnecessary syscall on next unblock,
   but that will be resolved in the next commit.

Due to the single global lock, user mutex acquisition is an extremely
"noisy" process that can take shorter or longer depending on what is
going on elsewhere on the system, so performance is hard to measure.

With one benchmark that acquires mutexes as fast as possible with
lots of contention, most runs came in as being around the same amount
of time both before and after this change (around 4.25s real). Moving
Terminal's window around while running the test caused runtime to go
up to around 6.7s before this change, and about 7.0s after.

GLTeapot seems to go from 350-380 FPS before this change and 320-340
after. It still spends the vast majority of its time waiting for address
space and cache locks, however.

It is expected that the next commits will build on this change to
improve performance beyond even the "before" numbers above.

Change-Id: I6581a6f7cb0ca0513ea639f8499a1c0c8596c026
Reviewed-on: https://review.haiku-os.org/c/haiku/+/6490
Reviewed-by: waddlesplash <waddlesplash@gmail.com>
Reviewed-by: Adrien Destugues <pulkomandy@pulkomandy.tk>
Tested-by: Commit checker robot <no-reply+buildbot@haiku-os.org>
2023-06-19 14:56:10 +00:00
Zardshard
16edf24a3c Icon-O-Matic: Remove unused dependency
Change-Id: I6b511d5d8ef7de22759f6a25484754473657a11e
Reviewed-on: https://review.haiku-os.org/c/haiku/+/6609
Reviewed-by: Adrien Destugues <pulkomandy@pulkomandy.tk>
2023-06-19 14:50:03 +00:00
Zardshard
098eaec630 Icon-O-Matic: Add reference images
Fixes #2748

Also fixes a comment misstating a function's name and code style.

Change-Id: I609a1f1e100ded647818e70b428cedc48cf29036
Reviewed-on: https://review.haiku-os.org/c/haiku/+/6604
Tested-by: Commit checker robot <no-reply+buildbot@haiku-os.org>
Reviewed-by: Adrien Destugues <pulkomandy@pulkomandy.tk>
2023-06-18 11:25:54 +00:00
David Karoly
b799d160f2 kernel/arm/paging: implement Modified Flag
* Introduce SWDBM flag similarly to the arm64 port
* Reuse TEX[2] for SWDBM flag which should be availble
  to be used by the operating system if TEX remap
  is enabled.
* Introduce SetAndClearPageTableEntryFlags for updating
  accessed and modified flags atomically
* Startup sequence is handled similarly to accessed flag, i.e.
  set Modified flag in initially mapped pages in bootloader and early map.
* Once the kernel initialization has progressed enough,
  pages are mapped as read-only and modified flag handling is done
  in the page fault handler.

Change-Id: I8f761e2c6325d1b91481abd569d5e8befded0761
Reviewed-on: https://review.haiku-os.org/c/haiku/+/6518
Tested-by: Commit checker robot <no-reply+buildbot@haiku-os.org>
Reviewed-by: Adrien Destugues <pulkomandy@pulkomandy.tk>
2023-06-18 11:18:24 +00:00
Alexander von Gluck IV
73c451087f drivers/wlan: Enable on riscv64
Change-Id: I20c755a05b43f9166558a2b9f2d3da8961b52d0b
2023-06-17 09:41:38 -05:00
Autocomitter
5146c41132 Update translations from Pootle 2023-06-17 08:17:02 +00:00
Alexander von Gluck IV
06898ee467 build/profile: Add nightly-mmc for riscv64 (and arm later)
Change-Id: I6aeddabedb8cf3e1505519f4393e4d0f2af5ea7f
2023-06-16 14:45:25 -05:00
X512
4a6b1e401c repo/riscv64: Bump HaikuPorts build-packages for riscv64
Change-Id: Idba2bc58c46deaa182ad27011b5a99e3231afe56
2023-06-16 14:44:48 -05:00
Alexander von Gluck IV
5676ddf7ca repo/riscv64: Bump HaikuPorts build-packages for riscv64
Change-Id: Ib230422bc514831983cd8b4d2794c37f46d62343
2023-06-16 13:10:53 -05:00
Alexander von Gluck IV
710016817c tests/qemu: Fix network on riscv64 test vm
* While virtio was attaching, network connectivity
  wasn't functional.
* Use usb-net for now to get things working.
* Move to a user network with dhcp for simplicity
* Forward port 5555 on the host to 22 on the guest
  for debugging / testing

Change-Id: Ieac095d7272c3132c39e4eaa0ccc461a32972dd2
2023-06-16 11:29:59 -05:00
David Karoly
b6e15e8bf5 boot/arm: enable TEX remap
Configure PRRR and NMRR as follows:
- memory type 0 is Strongly-Ordered
- memory type 1 is Shareable Device
- memory type 2 is Normal, Inner/Outer Write-Through
- memory type 3 is Normal, Inner/Outer Write-Back, no Write-Allocate

This way no change is needed in B and C bits so we can keep the
existing MemoryTypeToPageTableEntryFlags() implementation.

Change-Id: Icb4b18b0082774fdbef28576cee8624fae610538
Reviewed-on: https://review.haiku-os.org/c/haiku/+/6607
Reviewed-by: Adrien Destugues <pulkomandy@pulkomandy.tk>
Tested-by: Commit checker robot <no-reply+buildbot@haiku-os.org>
2023-06-15 10:43:14 +00:00
Augustin Cavalier
6554d7448d kernel: Print the first 4 chars of cvar names in KDL "threads" command.
Condition variables are now a pretty common way the kernel blocks threads.
That means the "threads" command was getting difficult to navigate, since
at any given time, a lot of threads could be blocked on "cvar".

Now we try (carefully, because it could fault!) to fetch the first 4
characters of the "type" name and display then. This suffices to
distinguish the most common object block types in the list at a glance
(e.g. "cvar:port" for port reads, the most common.)

Change-Id: I94f4b59fd78b7ebdce913944551a5e98f0ca2e33
Reviewed-on: https://review.haiku-os.org/c/haiku/+/6605
Reviewed-by: waddlesplash <waddlesplash@gmail.com>
2023-06-13 19:41:07 +00:00
KENZ
f0e9ed4488 Debugger: just skip .debug_frame/.eh_frame section contains a 0-length CIE
Some executables (or shared objects) may have .debug_frame or
.eh_frame section which contains the CIE(/FDE) length is 0.
The DWARF spec doesn't describe this case explicitly, but doesn't
prohibit it.
LSB says to treat this a terminator of the CIE.

https://refspecs.linuxbase.org/LSB_5.0.0/LSB-Core-generic/LSB-Core-generic/ehframechpt.html

Previous code failed to load the entire debug info of the
executable.

New code just skip these section (don't read anymore) after the
Debugger (kit) encounter a 0-length CIE.

Fixes #18438.

Change-Id: I382d0ec409d40570b5bccd384d38fa3c29ae2e7f
Reviewed-on: https://review.haiku-os.org/c/haiku/+/6538
Reviewed-by: Rene Gollent <rene@gollent.com>
Tested-by: Commit checker robot <no-reply+buildbot@haiku-os.org>
2023-06-13 12:22:56 +00:00
Augustin Cavalier
496e411397 pthread_barrier: Ensure the barrier is "idle" during destruction.
If the serial thread tries to destroy barriers immediately, not all threads
may have exited yet. This takes care of that case.
2023-06-12 23:55:08 -04:00
Augustin Cavalier
f79c7ae83e pthread_cond: Use test_and_set in cond_wait.
This is necessary in the case where only one thread is
being woken up at a time.
2023-06-12 23:53:39 -04:00
Augustin Cavalier
ca458a2b55 user_mutex: Adjust semantics of B_USER_MUTEX_UNBLOCK_ALL.
No longer is it required that the mutex be unlocked.
This was the case before the recent refactor, though it
wasn't noted anywhere. Now it is the case once more.

Should fix #18445.
2023-06-12 23:53:02 -04:00
Augustin Cavalier
901b48c2e8 user_mutex: Fix potential race in switch_lock.
We need to set the "to" mutex as locked+waiting before performing
the unlock of the first mutex, otherwise something in userland could
unset the "locked" flag but never call the kernel because "waiting"
had not yet been set.

In practice, the one consumer of this API (pthread_cond) could not,
at present, wind up in that situation, as far as I can tell, so this
race was entirely theoretical.
2023-06-12 22:51:45 -04:00
Jérôme Duval
9c4bd82612 ifconfig: check the family when different from AF_UNSPEC
fix #18443

Change-Id: I05df404311f7e2048b83d5c4ebfdf8e1b0fc64c0
Reviewed-on: https://review.haiku-os.org/c/haiku/+/6597
Tested-by: Commit checker robot <no-reply+buildbot@haiku-os.org>
Reviewed-by: Adrien Destugues <pulkomandy@pulkomandy.tk>
2023-06-12 14:07:07 +00:00
X512
edd8e471a6 xhci: fix root hub endpoint companion descriptor size
Fixes "SuperSpeed device without an endpoint companion descriptor!" error on riscv64.

Change-Id: Ie4d02997f17b68ea89bb3f3c8ab1ead99a2fc07a
Reviewed-on: https://review.haiku-os.org/c/haiku/+/6596
Tested-by: Commit checker robot <no-reply+buildbot@haiku-os.org>
Reviewed-by: Adrien Destugues <pulkomandy@pulkomandy.tk>
2023-06-11 21:55:51 +00:00
PulkoMandy
37e1b12911 framebuffer: report display EDID data
This allows to see the display in Screen preferences, and know its DPI
and physical size (as much as EDID data can be trusted). This
information could be used to compute the default font size, for example,
so it's important that all drivers provide it whenever possible.

Change-Id: Ic3d04e53cf5fcb24e22d35661d2b364a257947da
Reviewed-on: https://review.haiku-os.org/c/haiku/+/6576
Tested-by: Commit checker robot <no-reply+buildbot@haiku-os.org>
Reviewed-by: Adrien Destugues <pulkomandy@pulkomandy.tk>
2023-06-11 11:23:01 +00:00
Trung Nguyen
29f8da76d7 strace: Add mutex type
Added a dedicated mutex type handler that can print mutex status
flags while still showing the mutex address.

Change-Id: Ie028e5c0a336063a4c03a4f9adf955ffa6911837
Reviewed-on: https://review.haiku-os.org/c/haiku/+/6557
Reviewed-by: waddlesplash <waddlesplash@gmail.com>
2023-06-10 00:34:37 +00:00
Trung Nguyen
c80a875a74 libroot: fix pthread_[g/s]etschedparam
Make `pthread_getschedparam` and `pthread_setschedparam` handle
scheduling policies more consistently with `sched_get_priority_min`
and `sched_get_priority_max`.

Threads running in real-time priority will appear to be under the
`SCHED_RR` policy, while normal threads will appaer to be
`SCHED_OTHER`.

This prevents POSIX code using `sched_get_priority_min` with the
calling thread's current policy returned by `pthread_getschedparam`
to adjust its priority from unwantedly promote into real-time code
and affect overall system performance.

Change-Id: I9664257dc1b98db579e55218ce352cb762524b0c
Reviewed-on: https://review.haiku-os.org/c/haiku/+/6556
Reviewed-by: Adrien Destugues <pulkomandy@pulkomandy.tk>
Tested-by: Commit checker robot <no-reply+buildbot@haiku-os.org>
2023-06-09 17:07:44 +00:00
Augustin Cavalier
6f3f29c7dd user_mutex: Refactor locking and unblocking mechanism.
Suppose the following scenario:

1. Thread A holds a mutex.

2. Thread B goes to acquire the mutex, winds up in kernel waiting.

3. Thread A unlocks; first unsets the LOCKED flag.
   As WAITING is set, it calls the kernel; but instead of processing
   this immediately, the thread is suspended for any reason (locks,
   reschedule, etc.)

4. Thread B hits a timeout, or a signal. It then unblocks in the kernel,
   which causes the WAITING flag to be unset.

5. Thread C goes to acquire the lock. It sets the LOCKED flag.
   It sees the WAITING flag is not set, so it returns at once,
   having successfully acquired the lock.

6. Thread A, suspended back in step 3, resumes.

Now we encounter the problem. Under the previous code, the following
would occur.

7. Thread A sees that no threads are waiting. It thus unsets the LOCKED
   flag, and returns from the kernel. Now we have a mutex theoretically
   held by thread C but which (illegally) has no LOCKED flag set!

8. Some other thread tries to acquire the lock, and succeeds, for LOCKED
   is not set. We now have one lock owned by two separate threads.
   That's very bad!

The solution, in this commit, is to (1) switch from using "atomic_or"
to lock mutexes, to using "atomic_test_and_set", and (2) mandate that
_kern_unblock_mutex must be invoked with the mutex already unlocked.

Trying to solve the problem with (2) but without (1) produces other
complications and would overall be more complicated. For instance,
all existing userland code expected that it would set LOCKED, but then
check LOCKED|WAITING. If _kern_mutex_unlock does not unset LOCKED,
then whichever thread sets LOCKED when it was previously unset is
now the mutex's undisputed owner, and if it fails to notice this,
would deadlock.

That could have been solved with extra checks at all lock points, but
then that would mean locks would not be acquired "fairly": it would
be possible for any thread to race with an unlocking thread, and
acquire the lock before the kernel had a chance to wake anyone up.

Given how fast atomics can be, and how slow invoking the kernel is
comparatively, that would probably make our mutexes extremely "unfair."
This would not violate the POSIX specification, but it does seem like
a dangerous choice to make in implementing these APIs.

Linux's "futex" API, which our API bears some similarities to, requires
at least one atomic test-and-set for an uncontended acquisition,
and multiple atomics more for even the simplest case of contended
acquisition. If it works for them, it should work for us, too.

Fixes #18436.

Change-Id: Ib8c28acf04ce03234fe738e41aa0969ca1917540
Reviewed-on: https://review.haiku-os.org/c/haiku/+/6537
Tested-by: Commit checker robot <no-reply+buildbot@haiku-os.org>
Reviewed-by: Adrien Destugues <pulkomandy@pulkomandy.tk>
Reviewed-by: waddlesplash <waddlesplash@gmail.com>
2023-06-08 16:49:05 +00:00