Skip to content

Commit 4f2b30f

Browse files
alexaringteigland
authored andcommitted
fs: dlm: fix race in nodeid2con
This patch fixes a race in nodeid2con in cases that we parallel running a lookup and both will create a connection structure for the same nodeid. It's a rare case to create a new connection structure to keep reader lockless we just do a lookup inside the protection area again and drop previous work if this race happens. Fixes: a47666e ("fs: dlm: make connection hash lockless") Signed-off-by: Alexander Aring <aahringo@redhat.com> Signed-off-by: David Teigland <teigland@redhat.com>
1 parent 4798cbb commit 4f2b30f

1 file changed

Lines changed: 15 additions & 1 deletion

File tree

‎fs/dlm/lowcomms.c‎

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ static struct connection *__find_con(int nodeid)
175175
*/
176176
static struct connection *nodeid2con(int nodeid, gfp_t alloc)
177177
{
178-
struct connection *con = NULL;
178+
struct connection *con, *tmp;
179179
int r;
180180

181181
con = __find_con(nodeid);
@@ -213,6 +213,20 @@ static struct connection *nodeid2con(int nodeid, gfp_t alloc)
213213
r = nodeid_hash(nodeid);
214214

215215
spin_lock(&connections_lock);
216+
/* Because multiple workqueues/threads calls this function it can
217+
* race on multiple cpu's. Instead of locking hot path __find_con()
218+
* we just check in rare cases of recently added nodes again
219+
* under protection of connections_lock. If this is the case we
220+
* abort our connection creation and return the existing connection.
221+
*/
222+
tmp = __find_con(nodeid);
223+
if (tmp) {
224+
spin_unlock(&connections_lock);
225+
kfree(con->rx_buf);
226+
kfree(con);
227+
return tmp;
228+
}
229+
216230
hlist_add_head_rcu(&con->list, &connection_hash[r]);
217231
spin_unlock(&connections_lock);
218232

0 commit comments

Comments
 (0)