Skip to content

Conversation

@maflcko
Copy link
Member

@maflcko maflcko commented Apr 4, 2021

They only make sense for mutexes that are private members. Until cs_main is a private member the negative annotations should be replaced by excluded annotations, which are optional.

@hebasto
Copy link
Member

hebasto commented Apr 4, 2021

Concept ACK (in the light of #20272 discussion).

@bitcoin bitcoin deleted a comment from szeli57 Apr 4, 2021
Copy link
Member

@hebasto hebasto left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tested faadb1b050dd37ea8d294def554f1da22029d3e6 on Ubuntu 21.10:

$ clang --version
Ubuntu clang version 12.0.0-++rc3-4ubuntu1
Target: x86_64-pc-linux-gnu
Thread model: posix
InstalledDir: /usr/bin

With this PR massive TSA warnings are gone.

Could the global namespace be explicitly used for global mutexes in the touched lines?

Copy link
Member

@hebasto hebasto left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

May I suggest to:

  • add some missed annotations:
diff --git a/src/index/base.h b/src/index/base.h
index 8559e3cb6..ac3c429a5 100644
--- a/src/index/base.h
+++ b/src/index/base.h
@@ -109,7 +109,7 @@ public:
     /// sync once and only needs to process blocks in the ValidationInterface
     /// queue. If the index is catching up from far behind, this method does
     /// not block and immediately returns false.
-    bool BlockUntilSyncedToCurrentChain() const;
+    bool BlockUntilSyncedToCurrentChain() const LOCKS_EXCLUDED(::cs_main);
 
     void Interrupt();
 
diff --git a/src/rpc/rawtransaction.cpp b/src/rpc/rawtransaction.cpp
index 7932bd291..3a650923c 100644
--- a/src/rpc/rawtransaction.cpp
+++ b/src/rpc/rawtransaction.cpp
@@ -815,7 +815,7 @@ static RPCHelpMan signrawtransactionwithkey()
     };
 }
 
-static RPCHelpMan sendrawtransaction()
+static RPCHelpMan sendrawtransaction() LOCKS_EXCLUDED(::cs_main)
 {
     return RPCHelpMan{"sendrawtransaction",
                 "\nSubmit a raw transaction (serialized, hex-encoded) to local node and network.\n"

?

@hebasto
Copy link
Member

hebasto commented Apr 5, 2021

This change reverts the @ajtowns's idea for AssertLockNotHeld but I cannot see the way to save it, unfortunately.

@ajtowns
Copy link
Contributor

ajtowns commented Apr 5, 2021

Approach ACK, +1 on doc change as well

@maflcko
Copy link
Member Author

maflcko commented Apr 5, 2021

This change reverts

Correct, but the revert is only temporary (until cs_main is removed or a private member)

@maflcko
Copy link
Member Author

maflcko commented Apr 5, 2021

Force pushed to address feedback

Copy link
Member

@hebasto hebasto left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ACK fa5eabe

Copy link
Contributor

@vasild vasild left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ACK fa5eabe

No warnings with Clang 13.

@sipa
Copy link
Member

sipa commented Apr 5, 2021

utACK fa5eabe

@fanquake fanquake requested a review from ajtowns April 6, 2021 04:16
@ajtowns
Copy link
Contributor

ajtowns commented Apr 6, 2021

ACK fa5eabe

@maflcko maflcko merged commit 9ac8f6d into bitcoin:master Apr 6, 2021
@maflcko maflcko deleted the 2104-syncNeg branch April 6, 2021 06:09
sidhujag pushed a commit to syscoin/syscoin that referenced this pull request Apr 6, 2021
…globals

fa5eabe refactor: Remove negative lock annotations from globals (MarcoFalke)

Pull request description:

  They only make sense for mutexes that are private members. Until cs_main is a private member the negative annotations should be replaced by excluded annotations, which are optional.

ACKs for top commit:
  sipa:
    utACK fa5eabe
  ajtowns:
    ACK fa5eabe
  hebasto:
    ACK fa5eabe
  vasild:
    ACK fa5eabe

Tree-SHA512: 06f8a200304f81533010efcc42d9f59b8c4d0ae355920c0a28efb6fa161a3e3e68f2dfffb0c009afd9c2501e6a293c6e5a419a64d718f1f4e79668ab2ab1fcdc
Fabcien pushed a commit to Bitcoin-ABC/bitcoin-abc that referenced this pull request Sep 23, 2021
Summary:
> They only make sense for mutexes that are private members. Until cs_main is a private member the negative annotations should be replaced by excluded annotations, which are optional.

This is a backport of [[bitcoin/bitcoin#21598 | core#21598]]

Test Plan:
With TSAN:
```
$ git checkout <previous commit hash>
$ ninja &> before.log
$ git checkout pr21598
$ ninja &> after.log
$ grep "negative capability" before.log | wc -l
33
$ grep "negative capability" after.log | wc -l
0

```

Reviewers: #bitcoin_abc, Fabien

Reviewed By: #bitcoin_abc, Fabien

Differential Revision: https://reviews.bitcoinabc.org/D10187
@vasild
Copy link
Contributor

vasild commented Jun 7, 2022

This silenced the compiler warnings at the time, so it was ok. I just wonder about the following reasoning, in the light of #24931:

Remove negative lock annotations from globals ... They only make sense for mutexes that are private members.

Why is that? For example:

Mutex g_mutex;

void f1()
{
    LOCK(g_mutex);
}

void f2()
{
    LOCK(g_mutex);
    f1();
}

This is undefined behavior that would be detected if f1() is annotated with EXCLUSIVE_LOCKS_REQUIRED(!g_mutex).

To me it looks like that negative lock annotations "make sense" regardless of the scope of the mutex, no?

@maflcko
Copy link
Member Author

maflcko commented Jun 7, 2022

This pull was done in light of #20272 (comment) . If #24931 fixes the mentioned issues, then that is fine.

@ajtowns
Copy link
Contributor

ajtowns commented Jun 13, 2022

@vasild The problem is when you call f1 -- the negative annotation needs to keep getting propogated up the call stack:

Mutex g_mutex;
void f1() EXCLUSIVE_LOCKS_REQUIRED(!g_mutex)
{
    LOCK(g_mutex);
}

class X {
private:
    Mutex m_mut;
public:
    void f1() EXCLUSIVE_LOCKS_REQUIRED(!m_mut)
    {
        LOCK(m_mut);
    }
};
X g_x;

void caller() EXCLUSIVE_LOCKS_REQUIRED(!g_mutex)
{
    f1();
    g_x.f1();
}

@vasild
Copy link
Contributor

vasild commented Jun 13, 2022

Yes, it has to be propagated, but I wouldn't call it a "problem" though because it works exactly as intended. True that it creates inconvenience with code isolation (like this code should not know about that mutex), but after all a global mutex is not isolated, so IMO it is ok and even desirable to propagate its negative annotation.

@bitcoin bitcoin locked and limited conversation to collaborators Jun 13, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants