All of lore.kernel.org
 help / color / mirror / Atom feed
From: Tejun Heo <tj@kernel.org>
To: Maxim Levitsky <maximlevitsky@gmail.com>
Cc: linux-kernel <linux-kernel@vger.kernel.org>,
	Jens Axboe <jens.axboe@oracle.com>
Subject: Re: [block subsystem] Need help to prevent races on unexpected device removal
Date: Wed, 20 Jan 2010 11:53:49 +0900	[thread overview]
Message-ID: <4B56703D.8000303@kernel.org> (raw)
In-Reply-To: <1262546027.4555.17.camel@maxim-laptop>

(cc'ing Jens) Hello,

Sorry about the late reply.  I tagged this while I was watching block
related mails a couple of weeks ago but forgot about this.

On 01/04/2010 04:13 AM, Maxim Levitsky wrote:
> During development of hotplug support for mtd translation layer I seems
> to be unable to figure a way to prevent following race:
> 
> First of all, a block device is registered. I attach a private structure
> to that device to save all internal information.

I suppose you're talking about struct gendisk and using
gendisk->private_data for the private data, right?

> Then out of the blue (when user pulls off the card) I receive a request
> to remove the device.
> 
> In the function that handles such removal, I do:
> 
> del_gendisk(...
> blk_start_queue
> 
> stop thread that processes the requests
> 
> blk_cleanup_queue(old->rq);
> 
> 
> The problem is that I don't know where/when to free the private
> structure.
> 
> I though about adding a field to the structure, with name 'invalid', so
> that release will not attempt to go futher, but free the structure, but
> what happens if release is never called?
> In other words this will work as long as there is a user of the block
> device.
> 
> I thought then that I can detect that condition and free the structure
> in the removal function itself, but then I get a race with ->open
> running in same time, and mutex will not prevent it, I will have to
> release it somwhen, and then ->open will access a freed structure....

On hotunplug, the driver should mark the device dead so that all
further operations coming from existing open fail and then put the
base reference.  On the final put which may happen either as part of
device destruction or release, the private data structure can be
destroyed while holding a mutex.  Open can be protected by grabbing
the mutex before dereferencing the private_data.

Thanks.

-- 
tejun

      parent reply	other threads:[~2010-01-20  2:47 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2010-01-03 19:13 [block subsystem] Need help to prevent races on unexpected device removal Maxim Levitsky
2010-01-04 19:56 ` Maxim Levitsky
2010-01-20  2:53 ` Tejun Heo [this message]

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=4B56703D.8000303@kernel.org \
    --to=tj@kernel.org \
    --cc=jens.axboe@oracle.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=maximlevitsky@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 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.