[Git][ghc/ghc][ghc-9.0] 78 commits: SMP.h: Add C11-style atomic operations

Ben Gamari gitlab at gitlab.haskell.org
Wed Nov 25 02:26:12 UTC 2020



Ben Gamari pushed to branch ghc-9.0 at Glasgow Haskell Compiler / GHC


Commits:
08d75467 by Ben Gamari at 2020-11-24T12:03:00-05:00
SMP.h: Add C11-style atomic operations

- - - - -
9f6d3341 by Ben Gamari at 2020-11-24T12:03:00-05:00
rts: Infrastructure for testing with ThreadSanitizer

- - - - -
8b5e7dc7 by Ben Gamari at 2020-11-24T12:03:01-05:00
rts/CNF: Initialize all bdescrs in group

It seems wise and cheap to ensure that the whole bdescr of all blocks of
a compact group is valid, even if most cases only look at the flags
field.

- - - - -
67c0f410 by Ben Gamari at 2020-11-24T12:03:01-05:00
rts/Capability: Intialize interrupt field

Previously this was left uninitialized.

Also clarify some comments.

- - - - -
829a72cd by Ben Gamari at 2020-11-24T12:03:01-05:00
rts/Task: Make comments proper Notes

- - - - -
c19ee6d5 by Ben Gamari at 2020-11-24T12:03:01-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.

- - - - -
5ed8139a by Ben Gamari at 2020-11-24T12:03:01-05:00
rts/OSThreads: Fix data race

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

- - - - -
3c35c588 by Ben Gamari at 2020-11-24T12:03:01-05:00
rts/ClosureMaros: Use relaxed atomics

- - - - -
83e759a7 by Ben Gamari at 2020-11-24T12:03:01-05:00
configure: Bump minimum-supported gcc version to 4.7

Since the __atomic_* builtins are not supported until gcc 4.7. Given
that this version was released in 2012 I think this is acceptable.

- - - - -
7d846a79 by Ben Gamari at 2020-11-24T12:03:01-05:00
testsuite: Fix thread leak in hs_try_putmvar00[13]

- - - - -
37886925 by Ben Gamari at 2020-11-24T12:03:01-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.

- - - - -
5541b8ea by Ben Gamari at 2020-11-24T12:03:01-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.

- - - - -
12c8702a by Ben Gamari at 2020-11-24T12:03:01-05:00
rts: Annotate benign race in waitForCapability

- - - - -
3eb46f2e by Ben Gamari at 2020-11-24T12:03:01-05:00
rts: Clarify locking behavior of releaseCapability_

- - - - -
e052a812 by Ben Gamari at 2020-11-24T12:03:01-05:00
rts: Add assertions for task ownership of capabilities

- - - - -
2b1da3d8 by Ben Gamari at 2020-11-24T12:03:01-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.

- - - - -
053d3c5b by Ben Gamari at 2020-11-24T12:03:02-05:00
rts: Mitigate races in capability interruption logic

- - - - -
7ebad34c by Ben Gamari at 2020-11-24T12:03:02-05:00
rts/Capability: Use relaxed operations for last_free_capability

- - - - -
67716ed3 by Ben Gamari at 2020-11-24T12:03:02-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.

- - - - -
5615aac8 by Ben Gamari at 2020-11-24T12:03:02-05:00
rts/Schedule: Use relaxed operations for sched_state

- - - - -
677988d5 by Ben Gamari at 2020-11-24T12:03:02-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.

- - - - -
7ce38423 by Ben Gamari at 2020-11-24T12:03:02-05:00
rts: Eliminate data races on pending_sync

- - - - -
05f59c23 by Ben Gamari at 2020-11-24T12:03:02-05:00
rts/Schedule: Eliminate data races on recent_activity

We cannot safely use relaxed atomics here.

- - - - -
6417288c by Ben Gamari at 2020-11-24T12:03:02-05:00
rts: Avoid data races in message handling

- - - - -
dba1771b by Ben Gamari at 2020-11-24T12:03:02-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.

- - - - -
1727bc57 by Ben Gamari at 2020-11-24T12:03:02-05:00
rts/ThreadPaused: Avoid data races

- - - - -
3e36d9ee by Ben Gamari at 2020-11-24T12:03:02-05:00
rts/Schedule: Eliminate data races in run queue management

- - - - -
7b856fcd by Ben Gamari at 2020-11-24T12:03:03-05:00
rts: Eliminate shutdown data race on task counters

- - - - -
04a19bfc by Ben Gamari at 2020-11-24T12:03:03-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.

- - - - -
1f5dded6 by Ben Gamari at 2020-11-24T12:03:03-05:00
rts/Messages: Annotate benign race

- - - - -
852eb2cc by Ben Gamari at 2020-11-24T12:03:03-05:00
rts/RaiseAsync: Synchronize what_next read

- - - - -
d9b6eb31 by Ben Gamari at 2020-11-24T12:03:03-05:00
rts/Task: Move debugTrace to avoid data race

Specifically, we need to hold all_tasks_mutex to read taskCount.

- - - - -
1bcc9cd0 by Ben Gamari at 2020-11-24T12:03:03-05:00
Disable flawed assertion

- - - - -
2e76a631 by Ben Gamari at 2020-11-24T12:03:03-05:00
Document schedulePushWork race

- - - - -
718a46db by Ben Gamari at 2020-11-24T12:03:03-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.

- - - - -
588a950b by Ben Gamari at 2020-11-24T12:03:03-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.

- - - - -
853ef5e1 by Ben Gamari at 2020-11-24T12:03:03-05:00
gitlab-ci: Add nightly-x86_64-linux-deb9-tsan job

- - - - -
5bcda8ba by GHC GitLab CI at 2020-11-24T12:03:03-05:00
testsuite: Mark setnumcapabilities001 as broken with TSAN

Due to #18808.

- - - - -
e78d90e3 by GHC GitLab CI at 2020-11-24T12:03:03-05:00
testsuite: Skip divbyzero and derefnull under TSAN

ThreadSanitizer changes the output of these tests.

- - - - -
a99f05ef by Ben Gamari at 2020-11-24T12:03:03-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.

- - - - -
02d2e42b by Ben Gamari at 2020-11-24T12:03:04-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.

- - - - -
fba38edf by Ben Gamari at 2020-11-24T12:03:04-05:00
testsuite: Mark T9872[abc] as high_memory_usage

These all have a maximum residency of over 2 GB.

- - - - -
0215ff52 by Ben Gamari at 2020-11-24T12:03:04-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.

- - - - -
e191eb78 by Ben Gamari at 2020-11-24T12:03:04-05:00
TSANUtils: Ensure that C11 atomics are supported

- - - - -
a31bccca by Ben Gamari at 2020-11-24T12:03:04-05:00
testsuite: Mark T3807 as broken with TSAN

Due to #18883.

- - - - -
7aba9e54 by Ben Gamari at 2020-11-24T12:03:04-05:00
testsuite: Mark T13702 as broken with TSAN due to #18884

- - - - -
23a30a3b by Ben Gamari at 2020-11-24T12:03:04-05:00
rts/BlockAlloc: Use relaxed operations

- - - - -
4139b672 by Ben Gamari at 2020-11-24T12:03:04-05:00
rts: Rework handling of mutlist scavenging statistics

- - - - -
33b7b375 by Ben Gamari at 2020-11-24T12:03:04-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.

- - - - -
725dfd75 by Ben Gamari at 2020-11-24T12:03:04-05:00
rts/Storage: Use atomics

- - - - -
240bb1b4 by Ben Gamari at 2020-11-24T12:03:04-05:00
rts/Updates: Use proper atomic operations

- - - - -
b7b0f3ae by Ben Gamari at 2020-11-24T12:03:04-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.

- - - - -
20787589 by Ben Gamari at 2020-11-24T12:03:04-05:00
rts/GC: Use atomics

- - - - -
b15db127 by Ben Gamari at 2020-11-24T12:03:04-05:00
rts: Use RELEASE ordering in unlockClosure

- - - - -
49c8049e by Ben Gamari at 2020-11-24T12:03:04-05:00
rts/Storage: Accept races on heap size counters

- - - - -
89864d46 by Ben Gamari at 2020-11-24T12:03:04-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.

- - - - -
31669bd3 by GHC GitLab CI at 2020-11-24T12:03:04-05:00
rts: Fix race in GC CPU time accounting

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

- - - - -
7e968942 by Ben Gamari at 2020-11-24T12:03:05-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`).

- - - - -
acef7fd3 by Ben Gamari at 2020-11-24T12:03:05-05:00
rts: Use relaxed ordering on spinlock counters

- - - - -
53920304 by Ben Gamari at 2020-11-24T12:03:05-05:00
rts: Annotate hopefully "benign" races in freeGroup

- - - - -
0ca2beeb by Ben Gamari at 2020-11-24T12:03:05-05:00
Strengthen ordering in releaseGCThreads

- - - - -
7d3d0f13 by Ben Gamari at 2020-11-24T12:03:05-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.

- - - - -
c29b1a83 by Ben Gamari at 2020-11-24T12:03:05-05:00
rts: Use relaxed atomics for whitehole spin stats

- - - - -
55b252f2 by Ben Gamari at 2020-11-24T12:03:05-05:00
rts: Avoid lock order inversion during fork

Fixes #17275.

- - - - -
d9e82a56 by GHC GitLab CI at 2020-11-24T12:03:05-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.

- - - - -
d9ed5a62 by Ben Gamari at 2020-11-24T12:03:05-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.

- - - - -
219f6496 by Ben Gamari at 2020-11-24T12:03:05-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.

- - - - -
cf99c5e5 by Ben Gamari at 2020-11-24T12:03:05-05:00
Mitigate data races in event manager startup/shutdown

- - - - -
d7655654 by Ben Gamari at 2020-11-24T12:03:05-05:00
Suppress data race due to close

This suppresses the other side of a race during shutdown.

- - - - -
389c92df by Ben Gamari at 2020-11-24T12:03:05-05:00
rts: Accept benign races in Proftimer

- - - - -
b4ced846 by Ben Gamari at 2020-11-24T12:03:05-05:00
rts: Pause timer while changing capability count

This avoids #17289.

- - - - -
9116b39a by Ben Gamari at 2020-11-24T12:03:05-05:00
Fix #17289

- - - - -
75b8c066 by Ben Gamari at 2020-11-24T12:03:05-05:00
suppress #17289 (ticker) race

- - - - -
dcea5aef by Ben Gamari at 2020-11-24T12:03:05-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.

- - - - -
16d3ea21 by Ben Gamari at 2020-11-24T12:03:06-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.

- - - - -
da69341b by Ben Gamari at 2020-11-24T12:03:06-05:00
rts/Stats: Hide a few unused unnecessarily global functions

- - - - -
f65a5c9e by Ben Gamari at 2020-11-24T12:03:06-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.

- - - - -
543ad4f3 by Ben Gamari at 2020-11-24T12:03:06-05:00
rts: Tear down stats_mutex after exitHeapProfiling

Since the latter wants to call getRTSStats.

- - - - -


30 changed files:

- .gitlab-ci.yml
- configure.ac
- hadrian/hadrian.cabal
- hadrian/src/Flavour.hs
- hadrian/src/Settings.hs
- + hadrian/src/Settings/Flavours/ThreadSanitizer.hs
- includes/Rts.h
- 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


The diff was not included because it is too large.


View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/compare/d4483f7b81dc7bd3d246fc22728a1ec86570e6e8...543ad4f3f8320c17be0029078a1cbe832f5b6f56

-- 
View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/compare/d4483f7b81dc7bd3d246fc22728a1ec86570e6e8...543ad4f3f8320c17be0029078a1cbe832f5b6f56
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/20201124/04b36230/attachment-0001.html>


More information about the ghc-commits mailing list