public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: Markus Pargmann <mpa@pengutronix.de>
To: "Pranay Kr. Srivastava" <pranjas@gmail.com>
Cc: nbd-general@lists.sourceforge.net, linux-kernel@vger.kernel.org,
	gregkh@linuxfoundation.org
Subject: Re: [PATCH v4 01/18] nbd: Fix might_sleep warning on xmit timeout
Date: Thu, 12 May 2016 11:40:45 +0200	[thread overview]
Message-ID: <2919757.NIRCIcnYi2@adelgunde> (raw)
In-Reply-To: <1462954726-11825-2-git-send-email-pranjas@gmail.com>

[-- Attachment #1: Type: text/plain, Size: 6168 bytes --]

Hi,

On Wednesday 11 May 2016 11:18:29 Pranay Kr. Srivastava wrote:
> This patch fixes the warning generated when a timeout occurs
> on the request and socket is closed from a non-sleep context
> by
> 
> 1. Moving the socket closing on a timeout to nbd_thread_send
> 
> 2. Make sock lock to be a mutex instead of a spin lock, since
>    nbd_xmit_timeout doesn't need to hold it anymore.

This patch seems quite big and complicated. Isn't the main issue that a
socket shutdown is called from within a spinlock?

When the issue got reported by Mikulas Patocka I created a patch but
forgot to send it, sorry. It is a bit simpler by simpling moving the
socket shutdown out of the spinlock. I will send it as reply. Please
have a look.

Thanks,

Markus

> 
> Signed-off-by: Pranay Kr. Srivastava <pranjas@gmail.com>
> ---
>  drivers/block/nbd.c | 65 ++++++++++++++++++++++++++++++++---------------------
>  1 file changed, 39 insertions(+), 26 deletions(-)
> 
> diff --git a/drivers/block/nbd.c b/drivers/block/nbd.c
> index 31e73a7..c79bcd7 100644
> --- a/drivers/block/nbd.c
> +++ b/drivers/block/nbd.c
> @@ -57,12 +57,12 @@ struct nbd_device {
>  	int blksize;
>  	loff_t bytesize;
>  	int xmit_timeout;
> -	bool timedout;
> +	atomic_t timedout;
>  	bool disconnect; /* a disconnect has been requested by user */
>  
>  	struct timer_list timeout_timer;
>  	/* protects initialization and shutdown of the socket */
> -	spinlock_t sock_lock;
> +	struct mutex sock_lock;
>  	struct task_struct *task_recv;
>  	struct task_struct *task_send;
>  
> @@ -172,10 +172,9 @@ static void nbd_end_request(struct nbd_device *nbd, struct request *req)
>   */
>  static void sock_shutdown(struct nbd_device *nbd)
>  {
> -	spin_lock_irq(&nbd->sock_lock);
> -
> +	mutex_lock(&nbd->sock_lock);
>  	if (!nbd->sock) {
> -		spin_unlock_irq(&nbd->sock_lock);
> +		mutex_unlock(&nbd->sock_lock);
>  		return;
>  	}
>  
> @@ -183,27 +182,19 @@ static void sock_shutdown(struct nbd_device *nbd)
>  	kernel_sock_shutdown(nbd->sock, SHUT_RDWR);
>  	sockfd_put(nbd->sock);
>  	nbd->sock = NULL;
> -	spin_unlock_irq(&nbd->sock_lock);
> -
> +	mutex_unlock(&nbd->sock_lock);
>  	del_timer(&nbd->timeout_timer);
>  }
>  
>  static void nbd_xmit_timeout(unsigned long arg)
>  {
>  	struct nbd_device *nbd = (struct nbd_device *)arg;
> -	unsigned long flags;
>  
>  	if (list_empty(&nbd->queue_head))
>  		return;
>  
> -	spin_lock_irqsave(&nbd->sock_lock, flags);
> -
> -	nbd->timedout = true;
> -
> -	if (nbd->sock)
> -		kernel_sock_shutdown(nbd->sock, SHUT_RDWR);
> -
> -	spin_unlock_irqrestore(&nbd->sock_lock, flags);
> +	atomic_inc(&nbd->timedout);
> +	wake_up(&nbd->waiting_wq);
>  
>  	dev_err(nbd_to_dev(nbd), "Connection timed out, shutting down connection\n");
>  }
> @@ -579,7 +570,27 @@ static int nbd_thread_send(void *data)
>  		/* wait for something to do */
>  		wait_event_interruptible(nbd->waiting_wq,
>  					 kthread_should_stop() ||
> -					 !list_empty(&nbd->waiting_queue));
> +					 !list_empty(&nbd->waiting_queue) ||
> +					 atomic_read(&nbd->timedout));
> +
> +		if (atomic_read(&nbd->timedout)) {
> +			mutex_lock(&nbd->sock_lock);
> +			if (nbd->sock) {
> +				struct request sreq;
> +
> +				blk_rq_init(NULL, &sreq);
> +				sreq.cmd_type = REQ_TYPE_DRV_PRIV;
> +				mutex_lock(&nbd->tx_lock);
> +				nbd->disconnect = true;
> +				nbd_send_req(nbd, &sreq);
> +				mutex_unlock(&nbd->tx_lock);
> +				dev_err(disk_to_dev(nbd->disk),
> +					"Device Timeout occured.Shutting down"
> +					" socket.");
> +			}
> +			mutex_unlock(&nbd->sock_lock);
> +			sock_shutdown(nbd);
> +		}
>  
>  		/* extract request */
>  		if (list_empty(&nbd->waiting_queue))
> @@ -592,7 +603,11 @@ static int nbd_thread_send(void *data)
>  		spin_unlock_irq(&nbd->queue_lock);
>  
>  		/* handle request */
> -		nbd_handle_req(nbd, req);
> +		if (atomic_read(&nbd->timedout)) {
> +			req->errors++;
> +			nbd_end_request(nbd, req);
> +		} else
> +			nbd_handle_req(nbd, req);
>  	}
>  
>  	nbd->task_send = NULL;
> @@ -647,7 +662,7 @@ static int nbd_set_socket(struct nbd_device *nbd, struct socket *sock)
>  {
>  	int ret = 0;
>  
> -	spin_lock_irq(&nbd->sock_lock);
> +	mutex_lock(&nbd->sock_lock);
>  
>  	if (nbd->sock) {
>  		ret = -EBUSY;
> @@ -657,7 +672,7 @@ static int nbd_set_socket(struct nbd_device *nbd, struct socket *sock)
>  	nbd->sock = sock;
>  
>  out:
> -	spin_unlock_irq(&nbd->sock_lock);
> +	mutex_unlock(&nbd->sock_lock);
>  
>  	return ret;
>  }
> @@ -666,7 +681,7 @@ out:
>  static void nbd_reset(struct nbd_device *nbd)
>  {
>  	nbd->disconnect = false;
> -	nbd->timedout = false;
> +	atomic_set(&nbd->timedout, 0);
>  	nbd->blksize = 1024;
>  	nbd->bytesize = 0;
>  	set_capacity(nbd->disk, 0);
> @@ -803,17 +818,15 @@ static int __nbd_ioctl(struct block_device *bdev, struct nbd_device *nbd,
>  		error = nbd_thread_recv(nbd, bdev);
>  		nbd_dev_dbg_close(nbd);
>  		kthread_stop(thread);
> -
> -		mutex_lock(&nbd->tx_lock);
> -
>  		sock_shutdown(nbd);
> +		mutex_lock(&nbd->tx_lock);
>  		nbd_clear_que(nbd);
>  		kill_bdev(bdev);
>  		nbd_bdev_reset(bdev);
>  
>  		if (nbd->disconnect) /* user requested, ignore socket errors */
>  			error = 0;
> -		if (nbd->timedout)
> +		if (atomic_read(&nbd->timedout))
>  			error = -ETIMEDOUT;
>  
>  		nbd_reset(nbd);
> @@ -1075,7 +1088,7 @@ static int __init nbd_init(void)
>  		nbd_dev[i].magic = NBD_MAGIC;
>  		INIT_LIST_HEAD(&nbd_dev[i].waiting_queue);
>  		spin_lock_init(&nbd_dev[i].queue_lock);
> -		spin_lock_init(&nbd_dev[i].sock_lock);
> +		mutex_init(&nbd_dev[i].sock_lock);
>  		INIT_LIST_HEAD(&nbd_dev[i].queue_head);
>  		mutex_init(&nbd_dev[i].tx_lock);
>  		init_timer(&nbd_dev[i].timeout_timer);
> 

-- 
Pengutronix e.K.                           |                             |
Industrial Linux Solutions                 | http://www.pengutronix.de/  |
Peiner Str. 6-8, 31137 Hildesheim, Germany | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-5555 |

[-- Attachment #2: This is a digitally signed message part. --]
[-- Type: application/pgp-signature, Size: 819 bytes --]

  reply	other threads:[~2016-05-12  9:40 UTC|newest]

Thread overview: 38+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-05-11  8:18 [PATCH v4 00/18] nbd: fixes for might_sleep warning, checkpatch warning and device wait Pranay Kr. Srivastava
2016-05-11  8:18 ` [PATCH v4 01/18] nbd: Fix might_sleep warning on xmit timeout Pranay Kr. Srivastava
2016-05-12  9:40   ` Markus Pargmann [this message]
2016-05-12  9:43     ` [PATCH] nbd: Move socket shutdown out of spinlock Markus Pargmann
2016-05-12 11:12       ` Pranay Srivastava
2016-05-12 12:43         ` Markus Pargmann
2016-05-12 15:08           ` Pranay Srivastava
2016-05-19  6:22   ` [PATCH v4 01/18] nbd: Fix might_sleep warning on xmit timeout Markus Pargmann
2016-05-19 20:35     ` Pranay Srivastava
2016-05-20  8:22       ` Markus Pargmann
2016-05-23 10:32         ` Pranay Srivastava
2016-05-25 17:15           ` Pranay Srivastava
2016-05-11  8:18 ` [PATCH v4 02/18] nbd: fix checkpatch trailing space warning Pranay Kr. Srivastava
2016-05-11  8:33   ` Greg KH
     [not found]     ` <CA+aCy1GKfNd+VXi6f9Nrz63wx4tOafPp4j8_vScPF+T=+YR41Q@mail.gmail.com>
2016-05-11  9:38       ` Fwd: " Pranay Srivastava
2016-05-11 13:46         ` [Nbd] " Eric Blake
2016-05-12  9:25           ` Markus Pargmann
2016-06-03 16:50           ` Pavel Machek
2016-05-11  8:18 ` [PATCH v4 03/18] nbd: fix checkpatch warning use linux/uaccess.h Pranay Kr. Srivastava
2016-05-11  8:18 ` [PATCH v4 04/18] nbd : fix checkpatch pointer declaration warning Pranay Kr. Srivastava
2016-05-11  8:18 ` [PATCH v4 05/18] nbd: fix checkpatch warning no newline after decleration Pranay Kr. Srivastava
2016-05-11  8:18 ` [PATCH v4 06/18] " Pranay Kr. Srivastava
2016-05-11  8:18 ` [PATCH v4 07/18] nbd: fix checkpatch split string warning Pranay Kr. Srivastava
2016-05-12  8:39   ` Markus Pargmann
2016-05-11  8:18 ` [PATCH v4 08/18] nbd : fix checkpatch line over 80 char warning Pranay Kr. Srivastava
2016-05-11  8:18 ` [PATCH v4 09/18] nbd: fix checkpatch trailing whitespace warning Pranay Kr. Srivastava
2016-05-11  8:18 ` [PATCH v4 10/18] " Pranay Kr. Srivastava
2016-05-11  8:18 ` [PATCH v4 11/18] nbd : fix checkpatch structure declaration braces on next line warning Pranay Kr. Srivastava
2016-05-11  8:18 ` [PATCH v4 12/18] nbd : fix checkpatch trailing whitespace warning Pranay Kr. Srivastava
2016-05-11  8:18 ` [PATCH v4 13/18] nbd : fix checkpatch printk warning Pranay Kr. Srivastava
2016-05-11  8:18 ` [PATCH v4 14/18] nbd: fix checkpatch no extra line after decleration warning Pranay Kr. Srivastava
2016-05-11  8:18 ` [PATCH v4 15/18] nbd: fix checkpatch printk warning to pr_info Pranay Kr. Srivastava
2016-05-11  8:18 ` [PATCH v4 16/18] nbd: fix checkpatch no new line after decleration warning Pranay Kr. Srivastava
2016-05-11  8:18 ` [PATCH v4 17/18] nbd: fix checkpatch printk warning to pr_info Pranay Kr. Srivastava
2016-05-11  8:18 ` [PATCH v4 18/18] make nbd device wait for its users in case of timeout Pranay Kr. Srivastava
2016-05-12  9:19   ` Markus Pargmann
2016-05-12 15:19     ` Pranay Srivastava
2016-05-19  6:36       ` Markus Pargmann

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=2919757.NIRCIcnYi2@adelgunde \
    --to=mpa@pengutronix.de \
    --cc=gregkh@linuxfoundation.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=nbd-general@lists.sourceforge.net \
    --cc=pranjas@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox