All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] nbd: clear DISCONNECT_REQUESTED flag once disconnection occurs.
@ 2018-05-29 23:09 kvigor
  2018-05-30 15:41 ` Jens Axboe
  0 siblings, 1 reply; 5+ messages in thread
From: kvigor @ 2018-05-29 23:09 UTC (permalink / raw)
  To: linux-block; +Cc: josef, axboe, Kevin Vigor

From: Kevin Vigor <kvigor@fb.com>

When a userspace client requests a NBD device be disconnected, the
DISCONNECT_REQUESTED flag is set. While this flag is set, the driver
will not inform userspace when a connection is closed.

Unfortunately the flag was never cleared, so once a disconnect was
requested the driver would thereafter never tell userspace about a
closed connection. Thus when connections failed due to timeout, no
attempt to reconnect was made and eventually the device would fail.

Fix by clearing the DISCONNECT_REQUESTED flag (and setting the
DISCONNECTED flag) once all connections are closed.

Additionally wake all tasks waiting in wait_for_reconnect() when a
connection is established instead of only waking one and letting the
rest timeout.

Signed-off-by: Kevin Vigor <kvigor@fb.com>
---
 drivers/block/nbd.c | 23 ++++++++++++++++-------
 1 file changed, 16 insertions(+), 7 deletions(-)

diff --git a/drivers/block/nbd.c b/drivers/block/nbd.c
index afbc202..11956d4 100644
--- a/drivers/block/nbd.c
+++ b/drivers/block/nbd.c
@@ -213,7 +213,15 @@ static void nbd_mark_nsock_dead(struct nbd_device *nbd, struct nbd_sock *nsock,
 	}
 	if (!nsock->dead) {
 		kernel_sock_shutdown(nsock->sock, SHUT_RDWR);
-		atomic_dec(&nbd->config->live_connections);
+		if (atomic_dec_return(&nbd->config->live_connections) == 0) {
+			if (test_and_clear_bit(NBD_DISCONNECT_REQUESTED,
+					       &nbd->config->runtime_flags)) {
+				set_bit(NBD_DISCONNECTED,
+					&nbd->config->runtime_flags);
+				dev_info(nbd_to_dev(nbd),
+					"Disconnected due to user request.\n");
+			}
+		}
 	}
 	nsock->dead = true;
 	nsock->pending = NULL;
@@ -292,7 +300,9 @@ static enum blk_eh_timer_return nbd_xmit_timeout(struct request *req,
 
 	if (config->num_connections > 1) {
 		dev_err_ratelimited(nbd_to_dev(nbd),
-				    "Connection timed out, retrying\n");
+				    "Connection timed out, retrying (%d/%d alive)\n",
+				    atomic_read(&config->live_connections),
+				    config->num_connections);
 		/*
 		 * Hooray we have more connections, requeue this IO, the submit
 		 * path will put it on a real connection.
@@ -714,10 +724,9 @@ static int wait_for_reconnect(struct nbd_device *nbd)
 		return 0;
 	if (test_bit(NBD_DISCONNECTED, &config->runtime_flags))
 		return 0;
-	wait_event_timeout(config->conn_wait,
-			   atomic_read(&config->live_connections),
-			   config->dead_conn_timeout);
-	return atomic_read(&config->live_connections);
+	return wait_event_timeout(config->conn_wait,
+				  atomic_read(&config->live_connections) > 0,
+				  config->dead_conn_timeout) > 0;
 }
 
 static int nbd_handle_cmd(struct nbd_cmd *cmd, int index)
@@ -937,7 +946,7 @@ static int nbd_reconnect_socket(struct nbd_device *nbd, unsigned long arg)
 		queue_work(recv_workqueue, &args->work);
 
 		atomic_inc(&config->live_connections);
-		wake_up(&config->conn_wait);
+		wake_up_all(&config->conn_wait);
 		return 0;
 	}
 	sockfd_put(sock);
-- 
2.7.4

^ permalink raw reply related	[flat|nested] 5+ messages in thread

* Re: [PATCH] nbd: clear DISCONNECT_REQUESTED flag once disconnection occurs.
  2018-05-29 23:09 [PATCH] nbd: clear DISCONNECT_REQUESTED flag once disconnection occurs kvigor
@ 2018-05-30 15:41 ` Jens Axboe
  0 siblings, 0 replies; 5+ messages in thread
From: Jens Axboe @ 2018-05-30 15:41 UTC (permalink / raw)
  To: kvigor, linux-block; +Cc: josef, Kevin Vigor

On 5/29/18 5:09 PM, kvigor@gmail.com wrote:
> @@ -937,7 +946,7 @@ static int nbd_reconnect_socket(struct nbd_device *nbd, unsigned long arg)
>  		queue_work(recv_workqueue, &args->work);
>  
>  		atomic_inc(&config->live_connections);
> -		wake_up(&config->conn_wait);
> +		wake_up_all(&config->conn_wait);
>  		return 0;
>  	}
>  	sockfd_put(sock);

Unless you're using exclusive waits, and you are not, then wake_up() is
equivalent to wake_up_all().

-- 
Jens Axboe

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] nbd: clear DISCONNECT_REQUESTED flag once disconnection occurs.
  2018-05-30 16:45 kvigor
  2018-05-30 17:28 ` Josef Bacik
@ 2018-05-30 17:31 ` Jens Axboe
  1 sibling, 0 replies; 5+ messages in thread
From: Jens Axboe @ 2018-05-30 17:31 UTC (permalink / raw)
  To: kvigor, linux-block; +Cc: josef, Kevin Vigor

On 5/30/18 10:45 AM, kvigor@gmail.com wrote:
> From: Kevin Vigor <kvigor@fb.com>
> 
> When a userspace client requests a NBD device be disconnected, the
> DISCONNECT_REQUESTED flag is set. While this flag is set, the driver
> will not inform userspace when a connection is closed.
> 
> Unfortunately the flag was never cleared, so once a disconnect was
> requested the driver would thereafter never tell userspace about a
> closed connection. Thus when connections failed due to timeout, no
> attempt to reconnect was made and eventually the device would fail.
> 
> Fix by clearing the DISCONNECT_REQUESTED flag (and setting the
> DISCONNECTED flag) once all connections are closed.

Applied, thanks Kevin.

-- 
Jens Axboe

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] nbd: clear DISCONNECT_REQUESTED flag once disconnection occurs.
  2018-05-30 16:45 kvigor
@ 2018-05-30 17:28 ` Josef Bacik
  2018-05-30 17:31 ` Jens Axboe
  1 sibling, 0 replies; 5+ messages in thread
From: Josef Bacik @ 2018-05-30 17:28 UTC (permalink / raw)
  To: kvigor; +Cc: linux-block, josef, axboe, Kevin Vigor

On Wed, May 30, 2018 at 10:45:11AM -0600, kvigor@gmail.com wrote:
> From: Kevin Vigor <kvigor@fb.com>
> 
> When a userspace client requests a NBD device be disconnected, the
> DISCONNECT_REQUESTED flag is set. While this flag is set, the driver
> will not inform userspace when a connection is closed.
> 
> Unfortunately the flag was never cleared, so once a disconnect was
> requested the driver would thereafter never tell userspace about a
> closed connection. Thus when connections failed due to timeout, no
> attempt to reconnect was made and eventually the device would fail.
> 
> Fix by clearing the DISCONNECT_REQUESTED flag (and setting the
> DISCONNECTED flag) once all connections are closed.
> 
> Changes relative to v1 (https://marc.info/?l=linux-block&m=152763540418902):
> 
> * remove pointless wake_up() -> wake_up_all() change.
> 

Usually you want to put the changelog after the --- bit below so we can all see
it but it doesn't end up in the git changelog.

Reviewed-by: Josef Bacik <josef@toxicpanda.com>

Thanks,

Josef

^ permalink raw reply	[flat|nested] 5+ messages in thread

* [PATCH] nbd: clear DISCONNECT_REQUESTED flag once disconnection occurs.
@ 2018-05-30 16:45 kvigor
  2018-05-30 17:28 ` Josef Bacik
  2018-05-30 17:31 ` Jens Axboe
  0 siblings, 2 replies; 5+ messages in thread
From: kvigor @ 2018-05-30 16:45 UTC (permalink / raw)
  To: linux-block; +Cc: josef, axboe, Kevin Vigor

From: Kevin Vigor <kvigor@fb.com>

When a userspace client requests a NBD device be disconnected, the
DISCONNECT_REQUESTED flag is set. While this flag is set, the driver
will not inform userspace when a connection is closed.

Unfortunately the flag was never cleared, so once a disconnect was
requested the driver would thereafter never tell userspace about a
closed connection. Thus when connections failed due to timeout, no
attempt to reconnect was made and eventually the device would fail.

Fix by clearing the DISCONNECT_REQUESTED flag (and setting the
DISCONNECTED flag) once all connections are closed.

Changes relative to v1 (https://marc.info/?l=linux-block&m=152763540418902):

* remove pointless wake_up() -> wake_up_all() change.

Signed-off-by: Kevin Vigor <kvigor@fb.com>
---
 drivers/block/nbd.c | 21 +++++++++++++++------
 1 file changed, 15 insertions(+), 6 deletions(-)

diff --git a/drivers/block/nbd.c b/drivers/block/nbd.c
index afbc202..1cd041b 100644
--- a/drivers/block/nbd.c
+++ b/drivers/block/nbd.c
@@ -213,7 +213,15 @@ static void nbd_mark_nsock_dead(struct nbd_device *nbd, struct nbd_sock *nsock,
 	}
 	if (!nsock->dead) {
 		kernel_sock_shutdown(nsock->sock, SHUT_RDWR);
-		atomic_dec(&nbd->config->live_connections);
+		if (atomic_dec_return(&nbd->config->live_connections) == 0) {
+			if (test_and_clear_bit(NBD_DISCONNECT_REQUESTED,
+					       &nbd->config->runtime_flags)) {
+				set_bit(NBD_DISCONNECTED,
+					&nbd->config->runtime_flags);
+				dev_info(nbd_to_dev(nbd),
+					"Disconnected due to user request.\n");
+			}
+		}
 	}
 	nsock->dead = true;
 	nsock->pending = NULL;
@@ -292,7 +300,9 @@ static enum blk_eh_timer_return nbd_xmit_timeout(struct request *req,
 
 	if (config->num_connections > 1) {
 		dev_err_ratelimited(nbd_to_dev(nbd),
-				    "Connection timed out, retrying\n");
+				    "Connection timed out, retrying (%d/%d alive)\n",
+				    atomic_read(&config->live_connections),
+				    config->num_connections);
 		/*
 		 * Hooray we have more connections, requeue this IO, the submit
 		 * path will put it on a real connection.
@@ -714,10 +724,9 @@ static int wait_for_reconnect(struct nbd_device *nbd)
 		return 0;
 	if (test_bit(NBD_DISCONNECTED, &config->runtime_flags))
 		return 0;
-	wait_event_timeout(config->conn_wait,
-			   atomic_read(&config->live_connections),
-			   config->dead_conn_timeout);
-	return atomic_read(&config->live_connections);
+	return wait_event_timeout(config->conn_wait,
+				  atomic_read(&config->live_connections) > 0,
+				  config->dead_conn_timeout) > 0;
 }
 
 static int nbd_handle_cmd(struct nbd_cmd *cmd, int index)
-- 
2.7.4

^ permalink raw reply related	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2018-05-30 17:31 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2018-05-29 23:09 [PATCH] nbd: clear DISCONNECT_REQUESTED flag once disconnection occurs kvigor
2018-05-30 15:41 ` Jens Axboe
2018-05-30 16:45 kvigor
2018-05-30 17:28 ` Josef Bacik
2018-05-30 17:31 ` Jens Axboe

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.