Skip to contentSkip to conversation

drbd: fix a race of drbd_free_peer_req

PATCH Superseded Rui Xu 2 participants 3 messages Apr 19, 2022 – Apr 26, 2022 DRBD driver

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Rui Xu

author
#0 lore raw

PATCH 1/1 drbd: fix a race of drbd_free_peer_req

Commit e061feb8 and 9a11ebeb5 introduce a bug to race with drbd_free_peer_req() in got_peer_ack.

drbd_free_page_chain in drbd_finish_peer_reqs and got_peer_ack may happen concurrently, the sequence is as follows:

drbd_finish_peer_reqs got_peer_ack
drbd_free_page_chain drbd_free_peer_req
spin_lock()
page_chain_add
spin_unlock() spin_lock()
page_chain_add spin_unlock()
We can see that page_chain_add will be called twice which will lead to crash.

Revert those two commits to ensure that drbd_free_page_chain will only called once for one peer_req.

---
 drbd/drbd_receiver.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
drbd/drbd_receiver.c +3 -3
index f077f2b..8cac89b 100644
@@ -653,10 +653,12 @@ static int drbd_finish_peer_reqs(struct drbd_connection *connection)
 
 		++n;
 		/* list_del not necessary, next/prev members not touched */
-		/* The callback may free peer_req. */
 		err2 = peer_req->w.cb(&peer_req->w, !!err);
 		if (!err)
 			err = err2;
+	
+		if (list_empty(&peer_req->recv_order)) {
+			drbd_free_peer_req(peer_req);
 	}
 	if (atomic_sub_and_test(n, &connection->done_ee_cnt))
 		wake_up(&connection->ee_wait);
@@ -2293,7 +2295,6 @@ static int e_end_resync_block(struct drbd_work *w, int unused)
 	}
 	dec_unacked(peer_device);
 
-	drbd_free_peer_req(peer_req);
 	return err;
 }
 
@@ -2562,7 +2563,6 @@ static int e_end_block(struct drbd_work *w, int cancel)
 
 	drbd_may_finish_epoch(peer_device->connection, peer_req->epoch, EV_PUT + (cancel ? EV_CLEANUP : 0));
 
-	drbd_free_page_chain(&peer_device->connection->transport, &peer_req->page_chain, 0);
 	return err;
 }
 
Signature · 1 line
1.8.3.1
#1 lore raw

Hi Xu,

I think there is a potential race here, but I am not convinced that this is a general solution. The peer request could still be freed by got_peer_ack() between checking "list_empty(&peer_req->recv_order)" and freeing it in drbd_finish_peer_reqs(). Also, this solution keeps the page chain for peer requests for an unnecessarily long time, which is not ideal in memory constrained situations.

The underlying race, as far as I understand it, is that got_peer_ack() can be called while still processing the request in drbd_finish_peer_reqs(). This is only relevant for peer writes and not resync, so only the e_end_block() path is of interest. got_peer_ack() will only be called after we have sent the corresponding barrier ack for the peer request.

On the basis of this reasoning, I think a simple solution is to swap drbd_may_finish_epoch() and drbd_free_page_chain() in e_end_block(). Please try this and send it as a patch if it solves your problem.

By the way, this patch doesn't compile due to a mismatched brace.

Best regards,
Joel

author
#2 lore raw

Hi Joel,

It looks good to me and i have sent a new patch for it.

Thanks,
Xu

From: Joel Colledge <[email protected]>
Date: 2022-04-20 18:22:32
To: Rui Xu <[email protected]>
Cc: Philipp Reisner <[email protected]>,[email protected],[email protected]
Subject: Re: [PATCH] drbd: fix a race of drbd_free_peer_req>Hi Xu,

Quoted 23 lines

I think there is a potential race here, but I am not convinced that this is a general solution. The peer request could still be freed by got_peer_ack() between checking "list_empty(&peer_req->recv_order)" and freeing it in drbd_finish_peer_reqs(). Also, this solution keeps the page chain for peer requests for an unnecessarily long time, which is not ideal in memory constrained situations.

The underlying race, as far as I understand it, is that got_peer_ack() can be called while still processing the request in drbd_finish_peer_reqs(). This is only relevant for peer writes and not resync, so only the e_end_block() path is of interest. got_peer_ack() will only be called after we have sent the corresponding barrier ack for the peer request.

On the basis of this reasoning, I think a simple solution is to swap drbd_may_finish_epoch() and drbd_free_page_chain() in e_end_block(). Please try this and send it as a patch if it solves your problem.

By the way, this patch doesn't compile due to a mismatched brace.

Best regards,
Joel