Skip to content

Conversation

w0xlt
Copy link
Contributor

@w0xlt w0xlt commented Jan 25, 2022

Related to #19303, this PR gets rid of the RecursiveMutex cs_totalBytesSent and also adds AssertLockNotHeld macros combined with LOCKS_EXCLUDED thread safety annotations to avoid recursive locking.

@w0xlt w0xlt marked this pull request as draft January 25, 2022 22:32
@DrahtBot DrahtBot added the P2P label Jan 25, 2022
@w0xlt w0xlt force-pushed the cs_totalBytesSent_mutex branch from 756e882 to 6cb0fab Compare January 26, 2022 03:25
@w0xlt w0xlt marked this pull request as ready for review January 26, 2022 06:06
@w0xlt w0xlt force-pushed the cs_totalBytesSent_mutex branch from 45e2f9b to 378facf Compare February 10, 2022 15:57
@w0xlt w0xlt marked this pull request as draft February 10, 2022 16:12
@w0xlt w0xlt force-pushed the cs_totalBytesSent_mutex branch from 378facf to 6a3eeda Compare February 10, 2022 17:50
@w0xlt w0xlt marked this pull request as ready for review February 10, 2022 18:40
@w0xlt
Copy link
Contributor Author

w0xlt commented Feb 10, 2022

Addressed @hebasto suggestion.

All functions changed in the second commit use EXCLUSIVE_LOCKS_REQUIRED(!m_total_bytes_sent_mutex) instead of LOCKS_EXCLUDED(m_total_bytes_sent_mutex).

The only exception is void RecordBytesSent(uint64_t bytes) which displayed the error below when using EXCLUSIVE_LOCKS_REQUIRED.

Not sure about the reason for this error.

net.cpp:1655:29: error: calling function 'RecordBytesSent' requires negative capability '!m_total_bytes_sent_mutex' [-Werror,-Wthread-safety-analysis]
            if (bytes_sent) RecordBytesSent(bytes_sent);
                            ^
net.cpp:3077:21: error: calling function 'RecordBytesSent' requires negative capability '!m_total_bytes_sent_mutex' [-Werror,-Wthread-safety-analysis]
    if (nBytesSent) RecordBytesSent(nBytesSent);

@hebasto
Copy link
Member

hebasto commented Feb 10, 2022

@w0xlt

Not sure about the reason for this error.

Maybe:

--- a/src/net.h
+++ b/src/net.h
@@ -829,7 +829,8 @@ public:
 
     bool ForNode(NodeId id, std::function<bool(CNode* pnode)> func);
 
-    void PushMessage(CNode* pnode, CSerializedNetMsg&& msg);
+    void PushMessage(CNode* pnode, CSerializedNetMsg&& msg)
+        EXCLUSIVE_LOCKS_REQUIRED(!m_total_bytes_sent_mutex);
 
     using NodeFn = std::function<void(CNode*)>;
     void ForEachNode(const NodeFn& func)
@@ -1024,7 +1025,8 @@ private:
     /**
      * Check connected and listening sockets for IO readiness and process them accordingly.
      */
-    void SocketHandler();
+    void SocketHandler()
+        EXCLUSIVE_LOCKS_REQUIRED(!m_total_bytes_sent_mutex);
 
     /**
      * Do the read/write for connected sockets that are ready for IO.
@@ -1037,7 +1039,8 @@ private:
     void SocketHandlerConnected(const std::vector<CNode*>& nodes,
                                 const std::set<SOCKET>& recv_set,
                                 const std::set<SOCKET>& send_set,
-                                const std::set<SOCKET>& error_set);
+                                const std::set<SOCKET>& error_set)
+        EXCLUSIVE_LOCKS_REQUIRED(!m_total_bytes_sent_mutex);
 
     /**
      * Accept incoming connections, one from each read-ready listening socket.
@@ -1045,7 +1048,8 @@ private:
      */
     void SocketHandlerListening(const std::set<SOCKET>& recv_set);
 
-    void ThreadSocketHandler();
+    void ThreadSocketHandler()
+        EXCLUSIVE_LOCKS_REQUIRED(!m_total_bytes_sent_mutex);
     void ThreadDNSAddressSeed();
 
     uint64_t CalculateKeyedNetGroup(const CAddress& ad) const;
@@ -1074,7 +1078,8 @@ private:
 
     // Network stats
     void RecordBytesRecv(uint64_t bytes);
-    void RecordBytesSent(uint64_t bytes) LOCKS_EXCLUDED(m_total_bytes_sent_mutex);
+    void RecordBytesSent(uint64_t bytes)
+        EXCLUSIVE_LOCKS_REQUIRED(!m_total_bytes_sent_mutex);
 
     /**
      * Return vector of current BLOCK_RELAY peers.

?

@w0xlt w0xlt force-pushed the cs_totalBytesSent_mutex branch from 6a3eeda to ebf1f2c Compare February 10, 2022 21:51
@w0xlt
Copy link
Contributor Author

w0xlt commented Feb 10, 2022

@hebasto Thanks. This worked.

Very interesting. So, using EXCLUSIVE_LOCKS_REQUIRED(!mutex) requires that all functions that call the annotated function also have the same annotation. It is a cascading effect.

@hebasto
Copy link
Member

hebasto commented Feb 11, 2022

@hebasto Thanks. This worked.

Very interesting. So, using EXCLUSIVE_LOCKS_REQUIRED(!mutex) requires that all functions that call the annotated function also have the same annotation. It is a cascading effect.

That is why negative capabilities provide a stronger safety guarantee.


I don't see any reason of splitting changes between second and third commits. Maybe squash them?

@w0xlt w0xlt force-pushed the cs_totalBytesSent_mutex branch from ebf1f2c to 0b6c5da Compare February 11, 2022 13:23
@w0xlt
Copy link
Contributor Author

w0xlt commented Feb 11, 2022

Maybe squash them?

Sure. Done.

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 0b6c5da

@DrahtBot
Copy link
Contributor

DrahtBot commented Feb 13, 2022

The following sections might be updated with supplementary metadata relevant to reviewers and maintainers.

Conflicts

Reviewers, this pull request conflicts with the following ones:

  • #24931 (Strengthen thread safety assertions by ajtowns)
  • #24356 (refactor: replace CConnman::SocketEvents() with mockable Sock::WaitMany() by vasild)

If you consider this pull request important, please also help to review the conflicting pull requests. Ideally, start with the one that should be merged first.

@w0xlt w0xlt force-pushed the cs_totalBytesSent_mutex branch from 0b6c5da to f04ffdd Compare February 13, 2022 19:50
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.

re-ACK f04ffdd

@DrahtBot DrahtBot mentioned this pull request Feb 14, 2022
12 tasks
@w0xlt w0xlt force-pushed the cs_totalBytesSent_mutex branch from f04ffdd to eef2c7b Compare February 14, 2022 17:20
@w0xlt
Copy link
Contributor Author

w0xlt commented Feb 14, 2022

d370938 addresses @vasild suggestions.

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 eef2c7b

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.

re-ACK eef2c7b, only suggested changes since my previous review.

UPDATE: ACK retracted. See #24157 (comment)

@w0xlt
Copy link
Contributor Author

w0xlt commented Apr 12, 2022

Is any further action needed in this PR?

Copy link
Contributor

@ajtowns ajtowns left a comment

Choose a reason for hiding this comment

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

Think there's a missing annotation, but otherwise looks good.

@jonatack
Copy link
Member

ACK modulo the missing annotation

Verified the methods updated here have both an annotation in the declaration and a corresponding assertion in the definition:

GetMaxOutboundTarget
GetMaxOutboundTimeLeftInCycle
GetMaxOutboundTimeLeftInCycle_
GetOutboundTargetBytesLeft
GetTotalBytesSent
OutboundTargetReached
PushMessage
RecordBytesSent
SocketHandler
SocketHandlerConnected
ThreadSocketHandler

-BEGIN VERIFY SCRIPT-
sed -i 's/cs_totalBytesSent/m_total_bytes_sent_mutex/g' -- $(git grep --files-with-matches 'cs_totalBytesSent')
-END VERIFY SCRIPT-
@w0xlt w0xlt force-pushed the cs_totalBytesSent_mutex branch from eef2c7b to eff7918 Compare April 18, 2022 16:23
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 eff7918

Compiles with clang 15.

w0xlt and others added 2 commits April 22, 2022 05:40
@w0xlt w0xlt force-pushed the cs_totalBytesSent_mutex branch from eff7918 to 709af67 Compare April 22, 2022 08:41
@w0xlt
Copy link
Contributor Author

w0xlt commented Apr 22, 2022

Added the suggestion #24157 (comment)

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 709af67

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 709af67, tested on Ubuntu 22.04.

@fanquake fanquake requested a review from ajtowns April 22, 2022 09:47
@jonatack
Copy link
Member

ACK 709af67 per git range-diff 7a4ac71 eff7918 709af67, rebase to master, clang 15 debug build, and build with -Wthread-safety-negative

@laanwj laanwj merged commit a19f641 into bitcoin:master Apr 26, 2022
sidhujag pushed a commit to syscoin/syscoin that referenced this pull request Apr 26, 2022
@w0xlt w0xlt deleted the cs_totalBytesSent_mutex branch April 27, 2022 21:24
@bitcoin bitcoin locked and limited conversation to collaborators Apr 27, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants