[Git][ghc/ghc][wip/tsan-ghc-8.10] 79 commits: Backport: Fix for #18955 to GHC 8.10 #18955

Ben Gamari gitlab at gitlab.haskell.org
Tue Dec 1 17:49:00 UTC 2020



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


Commits:
65ed2fdc by Roland Senn at 2020-11-30T14:26:58+01:00
Backport: Fix for #18955 to GHC 8.10 #18955

Since MR !554 (#15454) GHCi automatically enabled the flag `-fobject-code` on
any module using the UnboxedTuples or UnboxedSum extensions.

MR !1553 (#16876) allowed to inhibit the automatic compiling to object-code
of these modules by setting the `fbyte-code` flag. However, it assigned 2
different semantics to this flag and introduced the regression described in
issue #18955.

This MR fixes this regression by unsetting the internal flag
`Opt_ByteCodeIfUnboxed` before it's copied to DynFlags local to the module.
In GHC 9.0.1 the issue is solved by introducing a new flag
`-f(no-)object-code-if-unboxed`.

- - - - -
3e418e85 by Ben Gamari at 2020-12-01T12:48:52-05:00
SMP.h: Add C11-style atomic operations

- - - - -
e11cdee8 by Ben Gamari at 2020-12-01T12:48:53-05:00
rts: Infrastructure for testing with ThreadSanitizer

- - - - -
0420a914 by Ben Gamari at 2020-12-01T12:48:53-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.

- - - - -
3fc4f18c by Ben Gamari at 2020-12-01T12:48:53-05:00
rts/Capability: Intialize interrupt field

Previously this was left uninitialized.

Also clarify some comments.

- - - - -
0db05bae by Ben Gamari at 2020-12-01T12:48:53-05:00
rts/Task: Make comments proper Notes

- - - - -
92beeb48 by Ben Gamari at 2020-12-01T12:48:53-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.

- - - - -
ca5adbeb by Ben Gamari at 2020-12-01T12:48:53-05:00
rts/OSThreads: Fix data race

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

- - - - -
b76a4cb3 by Ben Gamari at 2020-12-01T12:48:53-05:00
rts/ClosureMaros: Use relaxed atomics

- - - - -
42603f7b by Ben Gamari at 2020-12-01T12:48:53-05:00
testsuite: Fix thread leak in hs_try_putmvar00[13]

- - - - -
31cc96ac by Ben Gamari at 2020-12-01T12:48:53-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.

- - - - -
f5474698 by Ben Gamari at 2020-12-01T12:48:53-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.

- - - - -
e56161d7 by Ben Gamari at 2020-12-01T12:48:53-05:00
rts: Annotate benign race in waitForCapability

- - - - -
67a18092 by Ben Gamari at 2020-12-01T12:48:53-05:00
rts: Clarify locking behavior of releaseCapability_

- - - - -
eed7218e by Ben Gamari at 2020-12-01T12:48:53-05:00
rts: Add assertions for task ownership of capabilities

- - - - -
b4ea5341 by Ben Gamari at 2020-12-01T12:48:53-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.

- - - - -
2220752f by Ben Gamari at 2020-12-01T12:48:53-05:00
rts: Mitigate races in capability interruption logic

- - - - -
444e6476 by Ben Gamari at 2020-12-01T12:48:53-05:00
rts/Capability: Use relaxed operations for last_free_capability

- - - - -
3c626d75 by Ben Gamari at 2020-12-01T12:48:53-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.

- - - - -
efa866e6 by Ben Gamari at 2020-12-01T12:48:53-05:00
rts/Schedule: Use relaxed operations for sched_state

- - - - -
a12ce9ad by Ben Gamari at 2020-12-01T12:48:53-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.

- - - - -
eaf108c8 by Ben Gamari at 2020-12-01T12:48:53-05:00
rts: Eliminate data races on pending_sync

- - - - -
2398fea7 by Ben Gamari at 2020-12-01T12:48:53-05:00
rts/Schedule: Eliminate data races on recent_activity

We cannot safely use relaxed atomics here.

- - - - -
7f2bacf5 by Ben Gamari at 2020-12-01T12:48:53-05:00
rts: Avoid data races in message handling

- - - - -
cd94f7bf by Ben Gamari at 2020-12-01T12:48:53-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.

- - - - -
2f5e1853 by Ben Gamari at 2020-12-01T12:48:53-05:00
rts/ThreadPaused: Avoid data races

- - - - -
9600e424 by Ben Gamari at 2020-12-01T12:48:53-05:00
rts/Schedule: Eliminate data races in run queue management

- - - - -
7e84329b by Ben Gamari at 2020-12-01T12:48:53-05:00
rts: Eliminate shutdown data race on task counters

- - - - -
061611c9 by Ben Gamari at 2020-12-01T12:48:53-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.

- - - - -
99c5e53a by Ben Gamari at 2020-12-01T12:48:53-05:00
rts/Messages: Annotate benign race

- - - - -
88644055 by Ben Gamari at 2020-12-01T12:48:53-05:00
rts/RaiseAsync: Synchronize what_next read

- - - - -
20a89e98 by Ben Gamari at 2020-12-01T12:48:53-05:00
rts/Task: Move debugTrace to avoid data race

Specifically, we need to hold all_tasks_mutex to read taskCount.

- - - - -
aebe5c4d by Ben Gamari at 2020-12-01T12:48:53-05:00
Disable flawed assertion

- - - - -
914196db by Ben Gamari at 2020-12-01T12:48:53-05:00
Document schedulePushWork race

- - - - -
68e93f4d by Ben Gamari at 2020-12-01T12:48:53-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.

- - - - -
0ad7e4fe by Ben Gamari at 2020-12-01T12:48:53-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.

- - - - -
f4fa413d by Ben Gamari at 2020-12-01T12:48:53-05:00
gitlab-ci: Add nightly-x86_64-linux-deb9-tsan job

- - - - -
aa2dc3a6 by GHC GitLab CI at 2020-12-01T12:48:53-05:00
testsuite: Mark setnumcapabilities001 as broken with TSAN

Due to #18808.

- - - - -
d29d1d59 by GHC GitLab CI at 2020-12-01T12:48:53-05:00
testsuite: Skip divbyzero and derefnull under TSAN

ThreadSanitizer changes the output of these tests.

- - - - -
db69bfe9 by Ben Gamari at 2020-12-01T12:48:54-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.

- - - - -
7dbf4140 by Ben Gamari at 2020-12-01T12:48:54-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.

- - - - -
80019b4d by Ben Gamari at 2020-12-01T12:48:54-05:00
testsuite: Mark T9872[abc] as high_memory_usage

These all have a maximum residency of over 2 GB.

- - - - -
cbc7e06d by Ben Gamari at 2020-12-01T12:48:54-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.

- - - - -
fcd68815 by Ben Gamari at 2020-12-01T12:48:54-05:00
TSANUtils: Ensure that C11 atomics are supported

- - - - -
f966c105 by Ben Gamari at 2020-12-01T12:48:54-05:00
testsuite: Mark T3807 as broken with TSAN

Due to #18883.

- - - - -
38a0ede0 by Ben Gamari at 2020-12-01T12:48:54-05:00
testsuite: Mark T13702 as broken with TSAN due to #18884

- - - - -
571a13e8 by Ben Gamari at 2020-12-01T12:48:54-05:00
rts/BlockAlloc: Use relaxed operations

- - - - -
2ecc5693 by Ben Gamari at 2020-12-01T12:48:54-05:00
rts: Rework handling of mutlist scavenging statistics

- - - - -
78a5bdff by Ben Gamari at 2020-12-01T12:48:54-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.

- - - - -
a191f6d5 by Ben Gamari at 2020-12-01T12:48:54-05:00
rts/Storage: Use atomics

- - - - -
7d4d0751 by Ben Gamari at 2020-12-01T12:48:54-05:00
rts/Updates: Use proper atomic operations

- - - - -
afdc79e8 by Ben Gamari at 2020-12-01T12:48:54-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.

- - - - -
2e3a9399 by Ben Gamari at 2020-12-01T12:48:54-05:00
rts/GC: Use atomics

- - - - -
c6855d59 by Ben Gamari at 2020-12-01T12:48:54-05:00
rts: Use RELEASE ordering in unlockClosure

- - - - -
84cf7ed6 by Ben Gamari at 2020-12-01T12:48:54-05:00
rts/Storage: Accept races on heap size counters

- - - - -
f9480f50 by Ben Gamari at 2020-12-01T12:48:54-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.

- - - - -
0ea9a980 by GHC GitLab CI at 2020-12-01T12:48:54-05:00
rts: Fix race in GC CPU time accounting

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

- - - - -
e97ed920 by Ben Gamari at 2020-12-01T12:48:54-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`).

- - - - -
e0bf6738 by Ben Gamari at 2020-12-01T12:48:54-05:00
rts: Use relaxed ordering on spinlock counters

- - - - -
48054cf2 by Ben Gamari at 2020-12-01T12:48:54-05:00
rts: Annotate hopefully "benign" races in freeGroup

- - - - -
dddd97ec by Ben Gamari at 2020-12-01T12:48:54-05:00
Strengthen ordering in releaseGCThreads

- - - - -
bfe39b7f by Ben Gamari at 2020-12-01T12:48:54-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.

- - - - -
f9ed0fd2 by Ben Gamari at 2020-12-01T12:48:54-05:00
rts: Use relaxed atomics for whitehole spin stats

- - - - -
211af4a1 by Ben Gamari at 2020-12-01T12:48:54-05:00
rts: Avoid lock order inversion during fork

Fixes #17275.

- - - - -
7131d341 by GHC GitLab CI at 2020-12-01T12:48:54-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.

- - - - -
20178cde by Ben Gamari at 2020-12-01T12:48:54-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.

- - - - -
8894b402 by Ben Gamari at 2020-12-01T12:48:54-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.

- - - - -
81a9029b by Ben Gamari at 2020-12-01T12:48:54-05:00
Mitigate data races in event manager startup/shutdown

- - - - -
9801d049 by Ben Gamari at 2020-12-01T12:48:54-05:00
Suppress data race due to close

This suppresses the other side of a race during shutdown.

- - - - -
9b5bd302 by Ben Gamari at 2020-12-01T12:48:54-05:00
rts: Accept benign races in Proftimer

- - - - -
25b9c7f9 by Ben Gamari at 2020-12-01T12:48:54-05:00
rts: Pause timer while changing capability count

This avoids #17289.

- - - - -
a433c908 by Ben Gamari at 2020-12-01T12:48:54-05:00
Fix #17289

- - - - -
2161aed8 by Ben Gamari at 2020-12-01T12:48:54-05:00
suppress #17289 (ticker) race

- - - - -
c93f11fe by Ben Gamari at 2020-12-01T12:48:54-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.

- - - - -
1ada18d2 by Ben Gamari at 2020-12-01T12:48:54-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.

- - - - -
d315985a by Ben Gamari at 2020-12-01T12:48:54-05:00
rts/Stats: Hide a few unused unnecessarily global functions

- - - - -
953de20f by Ben Gamari at 2020-12-01T12:48:55-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.

- - - - -
9ad6e1f1 by Ben Gamari at 2020-12-01T12:48:55-05:00
rts: Tear down stats_mutex after exitHeapProfiling

Since the latter wants to call getRTSStats.

- - - - -
0f28389c by Ben Gamari at 2020-12-01T12:48:55-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
- compiler/main/DynFlags.hs
- compiler/main/GhcMake.hs
- ghc/GHCi/UI.hs
- 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


The diff was not included because it is too large.


View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/compare/383bb46f9de2e5943ac7d5bc5722af9d36ede017...0f28389c783a54a74c472f176c250ddf7a9c4bf4

-- 
View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/compare/383bb46f9de2e5943ac7d5bc5722af9d36ede017...0f28389c783a54a74c472f176c250ddf7a9c4bf4
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/20201201/000d538e/attachment-0001.html>


More information about the ghc-commits mailing list