Skip to contentSkip to conversation

nbd: fix locking in case of error

PATCH Pavel Machek 2 participants 2 messages Dec 18, 2008

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

This cleans up error handling some more, and prevents return with lock held from nbd_handle_req when nbd_send_req() fails. It also switches calling convention of nbd_send_req from 0 = ok, 1 = failed to 0 / -errno.

drivers/block/nbd.c +6 -5
index fcefbe4..d36787d 100644
@@ -280,7 +280,7 @@ static int nbd_send_req(struct nbd_devic
 	return 0;
 
 error_out:
-	return 1;
+	return -EIO;
 }
 
 static struct request *nbd_find_request(struct nbd_device *lo,
@@ -467,10 +467,9 @@ static void nbd_handle_req(struct nbd_de
 
 	mutex_lock(&lo->tx_lock);
 	if (unlikely(!lo->sock)) {
-		mutex_unlock(&lo->tx_lock);
 		printk(KERN_ERR "%s: Attempted send on closed socket\n",
 		       lo->disk->disk_name);
-		goto error_out;
+		goto error_out_unlock;
 	}
 
 	lo->active_req = req;
@@ -478,7 +477,7 @@ static void nbd_handle_req(struct nbd_de
 	if (nbd_send_req(lo, req) != 0) {
 		printk(KERN_ERR "%s: Request send failed\n",
 				lo->disk->disk_name);
-		goto error_out;
+		goto error_out_unlock;
 	} else {
 		spin_lock(&lo->queue_lock);
 		list_add(&req->queuelist, &lo->queue_head);
@@ -491,6 +490,8 @@ static void nbd_handle_req(struct nbd_de
 
 	return;
 
+error_out_unlock:
+	mutex_unlock(&lo->tx_lock);
 error_out:
 	req->errors++;
 	nbd_end_request(req);
@@ -531,7 +532,7 @@ static int nbd_thread(void *data)
  *   { printk( "Warning: Ignoring result!\n"); nbd_end_request( req ); }
  */
 
-static void do_nbd_request(struct request_queue * q)
+static void do_nbd_request(struct request_queue *q)
 {
 	struct request *req;
 	
Signature · 2 lines
(english) http://www.livejournal.com/~pavelmachek
(cesky, pictures) http://atrey.karlin.mff.cuni.cz/~pavel/picture/horses/blog.html
#1 lore raw
Quoted 26 lines · Pavel Machek

This cleans up error handling some more, and prevents return with lock held from nbd_handle_req when nbd_send_req() fails. It also switches

static struct request *nbd_find_request(struct nbd_device *lo,

drivers/block/nbd.c +2 -3
@@ -467,10 +467,9 @@ static void nbd_handle_req(struct nbd_de
 
 	mutex_lock(&lo->tx_lock);
 	if (unlikely(!lo->sock)) {
-		mutex_unlock(&lo->tx_lock);
 		printk(KERN_ERR "%s: Attempted send on closed socket\n",
 		       lo->disk->disk_name);
-		goto error_out;
+		goto error_out_unlock;
 	}
 
 	lo->active_req = req;
@@ -478,7 +477,7 @@ static void nbd_handle_req(struct nbd_de
 	if (nbd_send_req(lo, req) != 0) {
 		printk(KERN_ERR "%s: Request send failed\n",
 				lo->disk->disk_name);
-		goto error_out;
+		goto error_out_unlock;
 	} else {
 		spin_lock(&lo->queue_lock);
 		list_add(&req->queuelist, &lo->queue_head);

drivers/block/nbd.c static void nbd_handle_req(struct nbd_de · line 497

Quoted 8 lines
drivers/block/nbd.c +2 -0
@@ -491,6 +490,8 @@ static void nbd_handle_req(struct nbd_de
 
 	return;
 
+error_out_unlock:
+	mutex_unlock(&lo->tx_lock);
 error_out:
 	req->errors++;
 	nbd_end_request(req);

Not sure I see the problem. tx_lock is unlocked in every return path AFAICS. You introduced the tx_lock problem with the previous patch, where the second error_out was not correct.

--
Paul