[Git][ghc/ghc][wip/tsan-ghc-8.10] 73 commits: rts/SpinLock: Move to proper atomics

Ben Gamari gitlab at gitlab.haskell.org
Fri Nov 27 18:31:15 UTC 2020



Ben Gamari pushed to branch wip/tsan-ghc-8.10 at Glasgow Haskell Compiler / GHC


Commits:
f8da754c by Ben Gamari at 2020-11-27T13:28:30-05:00
rts/SpinLock: Move to proper atomics

This is fairly straightforward; we just needed to use relaxed operations
for the PROF_SPIN counters and a release store instead of a write
barrier.

- - - - -
2f6d3907 by Ben Gamari at 2020-11-27T13:28:34-05:00
rts/OSThreads: Fix data race

Previously we would race on the cached processor count. Avoiding this is
straightforward; just use relaxed operations.

- - - - -
cca9c1cb by Ben Gamari at 2020-11-27T13:28:34-05:00
rts/ClosureMaros: Use relaxed atomics

- - - - -
7dc087c5 by Ben Gamari at 2020-11-27T13:28:34-05:00
testsuite: Fix thread leak in hs_try_putmvar00[13]

- - - - -
11791ecf by Ben Gamari at 2020-11-27T13:28:34-05:00
rts: Introduce SET_HDR_RELEASE

Also ensure that we also store the info table pointer last to ensure
that the synchronization covers all stores.

- - - - -
f9936c89 by Ben Gamari at 2020-11-27T13:28:34-05:00
rts: Factor out logic to identify a good capability for running a task

Not only does this make the control flow a bit clearer but it also
allows us to add a TSAN suppression on this logic, which requires
(harmless) data races.

- - - - -
2526bae7 by Ben Gamari at 2020-11-27T13:28:34-05:00
rts: Annotate benign race in waitForCapability

- - - - -
fc42a34c by Ben Gamari at 2020-11-27T13:28:34-05:00
rts: Clarify locking behavior of releaseCapability_

- - - - -
f569c301 by Ben Gamari at 2020-11-27T13:28:34-05:00
rts: Add assertions for task ownership of capabilities

- - - - -
b3262547 by Ben Gamari at 2020-11-27T13:28:34-05:00
rts: Use relaxed atomics on n_returning_tasks

This mitigates the warning of a benign race on n_returning_tasks in
shouldYieldCapability.

See #17261.

- - - - -
9b546265 by Ben Gamari at 2020-11-27T13:28:34-05:00
rts: Mitigate races in capability interruption logic

- - - - -
8dbaa27c by Ben Gamari at 2020-11-27T13:28:34-05:00
rts/Capability: Use relaxed operations for last_free_capability

- - - - -
bd5ec309 by Ben Gamari at 2020-11-27T13:28:34-05:00
rts: Use relaxed operations for cap->running_task (TODO)

This shouldn't be necessary since only the owning thread of the capability
should be touching this.

- - - - -
41e854b7 by Ben Gamari at 2020-11-27T13:28:34-05:00
rts/Schedule: Use relaxed operations for sched_state

- - - - -
f54d381b by Ben Gamari at 2020-11-27T13:28:34-05:00
rts: Accept data race in work-stealing implementation

This race is okay since the task is owned by the capability pushing it.
By Note [Ownership of Task] this means that the capability is free to
write to `task->cap` without taking `task->lock`.

Fixes #17276.

- - - - -
5a29a9ac by Ben Gamari at 2020-11-27T13:28:34-05:00
rts: Eliminate data races on pending_sync

- - - - -
3b56d934 by Ben Gamari at 2020-11-27T13:28:34-05:00
rts/Schedule: Eliminate data races on recent_activity

We cannot safely use relaxed atomics here.

- - - - -
5eec2560 by Ben Gamari at 2020-11-27T13:28:34-05:00
rts: Avoid data races in message handling

- - - - -
014482a8 by Ben Gamari at 2020-11-27T13:28:34-05:00
rts/Messages: Drop incredibly fishy write barrier

executeMessage previously had a write barrier at the beginning of its
loop apparently in an attempt to synchronize with another thread's
writes to the Message. I would guess that the author had intended to use
a load barrier here given that there are no globally-visible writes done
in executeMessage.

I've removed the redundant barrier since the necessary load barrier is
now provided by the ACQUIRE_LOAD.

- - - - -
5ad949c8 by Ben Gamari at 2020-11-27T13:28:34-05:00
rts/ThreadPaused: Avoid data races

- - - - -
3a61a624 by Ben Gamari at 2020-11-27T13:28:34-05:00
rts/Schedule: Eliminate data races in run queue management

- - - - -
6b0c4a4d by Ben Gamari at 2020-11-27T13:28:34-05:00
rts: Eliminate shutdown data race on task counters

- - - - -
d035205e by Ben Gamari at 2020-11-27T13:28:34-05:00
rts/Threads: Avoid data races (TODO)

Replace barriers with appropriate ordering. Drop redundant barrier in
tryWakeupThread (the RELEASE barrier will be provided by sendMessage's
mutex release).

We use relaxed operations on why_blocked and the stack although it's not
clear to me why this is necessary.

- - - - -
47b3aa0f by Ben Gamari at 2020-11-27T13:28:34-05:00
rts/Messages: Annotate benign race

- - - - -
8dff50c6 by Ben Gamari at 2020-11-27T13:28:34-05:00
rts/RaiseAsync: Synchronize what_next read

- - - - -
c28b7cc2 by Ben Gamari at 2020-11-27T13:28:34-05:00
rts/Task: Move debugTrace to avoid data race

Specifically, we need to hold all_tasks_mutex to read taskCount.

- - - - -
041c2e85 by Ben Gamari at 2020-11-27T13:28:34-05:00
Disable flawed assertion

- - - - -
c12ef8b2 by Ben Gamari at 2020-11-27T13:28:35-05:00
Document schedulePushWork race

- - - - -
82ddaf61 by Ben Gamari at 2020-11-27T13:28:35-05:00
Capabiliity: Properly fix data race on n_returning_tasks

There is a real data race but can be made safe by using proper atomic
(but relaxed) accesses.

- - - - -
46866cfd by Ben Gamari at 2020-11-27T13:28:35-05:00
rts: Make write of to_cap->inbox atomic

This is necessary since emptyInbox may read from to_cap->inbox without
taking cap->lock.

- - - - -
3b827993 by Ben Gamari at 2020-11-27T13:28:35-05:00
gitlab-ci: Add nightly-x86_64-linux-deb9-tsan job

- - - - -
43913d14 by GHC GitLab CI at 2020-11-27T13:28:35-05:00
testsuite: Mark setnumcapabilities001 as broken with TSAN

Due to #18808.

- - - - -
a3cff132 by GHC GitLab CI at 2020-11-27T13:28:35-05:00
testsuite: Skip divbyzero and derefnull under TSAN

ThreadSanitizer changes the output of these tests.

- - - - -
49b08d4b by Ben Gamari at 2020-11-27T13:28:35-05:00
testsuite: Skip high memory usage tests with TSAN

ThreadSanitizer significantly increases the memory footprint of tests,
so much so that it can send machines into OOM.

- - - - -
e0019742 by Ben Gamari at 2020-11-27T13:28:35-05:00
testsuite: Mark hie002 as high_memory_usage

This test has a peak residency of 1GByte; this is large enough to
classify as "high" in my book.

- - - - -
09882529 by Ben Gamari at 2020-11-27T13:28:35-05:00
testsuite: Mark T9872[abc] as high_memory_usage

These all have a maximum residency of over 2 GB.

- - - - -
7f9413ae by Ben Gamari at 2020-11-27T13:28:35-05:00
gitlab-ci: Disable documentation in TSAN build

Haddock chews through enough memory to cause the CI builders to OOM and
there's frankly no reason to build documentation in this job anyways.

- - - - -
e7560554 by Ben Gamari at 2020-11-27T13:28:35-05:00
TSANUtils: Ensure that C11 atomics are supported

- - - - -
55500716 by Ben Gamari at 2020-11-27T13:28:35-05:00
testsuite: Mark T3807 as broken with TSAN

Due to #18883.

- - - - -
4197478e by Ben Gamari at 2020-11-27T13:28:35-05:00
testsuite: Mark T13702 as broken with TSAN due to #18884

- - - - -
e6029389 by Ben Gamari at 2020-11-27T13:28:35-05:00
rts/BlockAlloc: Use relaxed operations

- - - - -
0772dac6 by Ben Gamari at 2020-11-27T13:28:35-05:00
rts: Rework handling of mutlist scavenging statistics

- - - - -
d623201e by Ben Gamari at 2020-11-27T13:28:35-05:00
rts: Avoid data races in StablePtr implementation

This fixes two potentially problematic data races in the StablePtr
implementation:

 * We would fail to RELEASE the stable pointer table when enlarging it,
   causing other cores to potentially see uninitialized memory.

 * We would fail to ACQUIRE when dereferencing a stable pointer.

- - - - -
62dc2674 by Ben Gamari at 2020-11-27T13:28:35-05:00
rts/Storage: Use atomics

- - - - -
948cfaf2 by Ben Gamari at 2020-11-27T13:28:35-05:00
rts/Updates: Use proper atomic operations

- - - - -
c84d1546 by Ben Gamari at 2020-11-27T13:28:35-05:00
rts/Weak: Eliminate data races

By taking all_tasks_mutex in stat_exit. Also better-document the fact
that the task statistics are protected by all_tasks_mutex.

- - - - -
18f1cd4f by Ben Gamari at 2020-11-27T13:28:35-05:00
rts/GC: Use atomics

- - - - -
8d181e61 by Ben Gamari at 2020-11-27T13:28:35-05:00
rts: Use RELEASE ordering in unlockClosure

- - - - -
f6e6ae9a by Ben Gamari at 2020-11-27T13:28:35-05:00
rts/Storage: Accept races on heap size counters

- - - - -
7672301d by Ben Gamari at 2020-11-27T13:28:35-05:00
rts: Join to concurrent mark thread during shutdown

Previously we would take all capabilities but fail to join on the thread
itself, potentially resulting in a leaked thread.

- - - - -
81731699 by GHC GitLab CI at 2020-11-27T13:28:35-05:00
rts: Fix race in GC CPU time accounting

Ensure that the GC leader synchronizes with workers before calling
stat_endGC.

- - - - -
2222390f by Ben Gamari at 2020-11-27T13:30:25-05:00
rts/SpinLock: Separate out slow path

Not only is this in general a good idea, but it turns out that GCC
unrolls the retry loop, resulting is massive code bloat in critical
parts of the RTS (e.g. `evacuate`).

- - - - -
840cd2d3 by Ben Gamari at 2020-11-27T13:31:02-05:00
rts: Use relaxed ordering on spinlock counters

- - - - -
aade2cdd by Ben Gamari at 2020-11-27T13:31:04-05:00
rts: Annotate hopefully "benign" races in freeGroup

- - - - -
574181f4 by Ben Gamari at 2020-11-27T13:31:04-05:00
Strengthen ordering in releaseGCThreads

- - - - -
c5fcb0f2 by Ben Gamari at 2020-11-27T13:31:04-05:00
rts/WSDeque: Rewrite with proper atomics

After a few attempts at shoring up the previous implementation, I ended
up turning to the literature and now use the proven implementation,

> N.M. LĂȘ, A. Pop, A.Cohen, and F.Z. Nardelli. "Correct and Efficient
> Work-Stealing for Weak Memory Models". PPoPP'13, February 2013,
> ACM 978-1-4503-1922/13/02.

Note only is this approach formally proven correct under C11 semantics
but it is also proved to be a bit faster in practice.

- - - - -
4e57a865 by Ben Gamari at 2020-11-27T13:31:04-05:00
rts: Use relaxed atomics for whitehole spin stats

- - - - -
5e9a359e by Ben Gamari at 2020-11-27T13:31:04-05:00
rts: Avoid lock order inversion during fork

Fixes #17275.

- - - - -
f7607554 by GHC GitLab CI at 2020-11-27T13:31:04-05:00
rts: Use proper relaxe operations in getCurrentThreadCPUTime

Here we are doing lazy initialization; it's okay if we do the check more
than once, hence relaxed operation is fine.

- - - - -
a6fdae36 by Ben Gamari at 2020-11-27T13:31:04-05:00
rts/STM: Use atomics

This fixes a potentially harmful race where we failed to synchronize
before looking at a TVar's current_value.

Also did a bit of refactoring to avoid abstract over management of
max_commits.

- - - - -
798a81d5 by Ben Gamari at 2020-11-27T13:31:04-05:00
rts/stm: Strengthen orderings to SEQ_CST instead of volatile

Previously the `current_value`, `first_watch_queue_entry`, and
`num_updates` fields of `StgTVar` were marked as `volatile` in an
attempt to provide strong ordering. Of course, this isn't sufficient.

We now use proper atomic operations. In most of these cases I strengthen
the ordering all the way to SEQ_CST although it's possible that some
could be weakened with some thought.

- - - - -
8a4c40c4 by Ben Gamari at 2020-11-27T13:31:04-05:00
Mitigate data races in event manager startup/shutdown

- - - - -
1f636c10 by Ben Gamari at 2020-11-27T13:31:04-05:00
Suppress data race due to close

This suppresses the other side of a race during shutdown.

- - - - -
8f0d6db9 by Ben Gamari at 2020-11-27T13:31:04-05:00
rts: Accept benign races in Proftimer

- - - - -
52704812 by Ben Gamari at 2020-11-27T13:31:04-05:00
rts: Pause timer while changing capability count

This avoids #17289.

- - - - -
e09376e0 by Ben Gamari at 2020-11-27T13:31:04-05:00
Fix #17289

- - - - -
d7761be1 by Ben Gamari at 2020-11-27T13:31:04-05:00
suppress #17289 (ticker) race

- - - - -
dab8b674 by Ben Gamari at 2020-11-27T13:31:04-05:00
rts: Fix timer initialization

Previously `initScheduler` would attempt to pause the ticker and in so
doing acquire the ticker mutex. However, initTicker, which is
responsible for initializing said mutex, hadn't been called
yet.

- - - - -
11fa84bf by Ben Gamari at 2020-11-27T13:31:04-05:00
rts: Fix races in Pthread timer backend shudown

We can generally be pretty relaxed in the barriers here since the timer
thread is a loop.

- - - - -
dfb50537 by Ben Gamari at 2020-11-27T13:31:04-05:00
rts/Stats: Hide a few unused unnecessarily global functions

- - - - -
b9541309 by Ben Gamari at 2020-11-27T13:31:04-05:00
rts/Stats: Protect with mutex

While on face value this seems a bit heavy, I think it's far better than
enforcing ordering on every access.

- - - - -
5a46207b by Ben Gamari at 2020-11-27T13:31:04-05:00
rts: Tear down stats_mutex after exitHeapProfiling

Since the latter wants to call getRTSStats.

- - - - -
a3152aa0 by Ben Gamari at 2020-11-27T13:31:04-05:00
rts/Stats: Reintroduce mut_user_time

Fix the previous backport; this function was dead code in master but is
still needed due to ProfHeap.c in ghc-8.10.

- - - - -


30 changed files:

- .gitlab-ci.yml
- hadrian/src/Flavour.hs
- includes/rts/OSThreads.h
- includes/rts/SpinLock.h
- includes/rts/StablePtr.h
- includes/rts/TSANUtils.h
- includes/rts/storage/ClosureMacros.h
- includes/rts/storage/Closures.h
- includes/rts/storage/GC.h
- includes/stg/SMP.h
- libraries/base/GHC/Event/Control.hs
- rts/.tsan-suppressions
- rts/Capability.c
- rts/Capability.h
- rts/Messages.c
- rts/Proftimer.c
- rts/RaiseAsync.c
- rts/RtsStartup.c
- rts/SMPClosureOps.h
- rts/STM.c
- rts/Schedule.c
- rts/Schedule.h
- rts/Sparks.c
- + rts/SpinLock.c
- rts/StablePtr.c
- rts/Stats.c
- rts/Stats.h
- rts/Task.c
- rts/ThreadPaused.c
- rts/Threads.c


The diff was not included because it is too large.


View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/compare/51d95b564fa7319719c1c810df4faeb983f281bb...a3152aa057644dac7b8df4c30c3034d3ab180748

-- 
View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/compare/51d95b564fa7319719c1c810df4faeb983f281bb...a3152aa057644dac7b8df4c30c3034d3ab180748
You're receiving this email because of your account on gitlab.haskell.org.


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/ghc-commits/attachments/20201127/087b1305/attachment-0001.html>


More information about the ghc-commits mailing list