public inbox for linux-scsi@vger.kernel.org
 help / color / mirror / Atom feed
* Re: BUG in: Driver core: convert block from raw kobjects to core devices (fwd)
@ 2007-10-29 15:18 Alan Stern
  2007-10-29 15:35 ` James Bottomley
  0 siblings, 1 reply; 28+ messages in thread
From: Alan Stern @ 2007-10-29 15:18 UTC (permalink / raw)
  To: James Bottomley
  Cc: Kay Sievers, Hannes Reinecke, Greg KH, SCSI development list

[-- Attachment #1: Type: TEXT/PLAIN, Size: 3133 bytes --]

James:

With regard to the discussion below, is there any objection to the 
attached patch?  It moves the call to scsi_release_queue() from 
scsi_device_dev_release_usercontext() to __scsi_remove_device().  In 
fact, it might turn out that with this change, the extra _usercontext 
routine isn't needed at all.

As far as I can see, the only reason for not adopting this patch would 
be if a scsi_device's queue structure was needed even after the 
scsi_device was unregistered.  Does this ever happen?  If it does, it 
would be indicative of a nasty reference-loop problem (scsi_device 
needs reference to request_queue, request_queue holds reference to 
gendisk, gendisk holds reference to scsi_device).

Alan Stern


---------- Forwarded message ----------
Date: Tue, 23 Oct 2007 13:27:49 +0200
From: Kay Sievers <kay.sievers@vrfy.org>
To: Alan Stern <stern@rowland.harvard.edu>
Cc: Greg KH <greg@kroah.com>,
     Kernel development list <linux-kernel@vger.kernel.org>,
     Hannes Reinecke <hare@suse.de>
Subject: Re: BUG in: Driver core: convert block from raw kobjects to core
    devices

On Tue, 2007-10-23 at 00:14 -0400, Alan Stern wrote:
> On Tue, 23 Oct 2007, Kay Sievers wrote:
> 
> > There must be something going wrong with the block patch in conjunction
> > with the crazy SCSI release logic.
> 
> I don't know -- there's nothing obviously wrong with the block patch 
> except the extra put_device.  But you're right that the SCSI logic is 
> crazy.  The scsi_device is the parent of the gendisk, which is the 
> parent of the request_queue.  But the scsi_device holds a reference to 
> the request_queue, which is dropped in the scsi_device's release 
> routine!

That's the thing. We see a circular reference when we use the SCSI LUN
as the parent.
The sysfs relationship is: scsi_device -> genhd -> queue, while the
queue holds a ref to to the blockdev (parent), the blockdev to the
scsi_device (parent) and the scsi_devices to the queue (SCSI). All
waiting for their release functions to be called, to release the other
refs.

The scsi_device needs to drop the queue reference while the device is
removed, not when it's data is released. Hannes came up with the
attached patch, which seems to work fine here.

> That doesn't make much sense to me, and it is complicated by 
> the fact that deleting a kobject doesn't drop the kobject's reference 
> to its parent -- only releasing the kobject does.

Right, that makes things very complicated.

> In fact, for my development work I normally use a patch which makes 
> kobjects behave better: They do drop the reference to their parent when 
> they are deleted.  This actually is nothing more than a reversion of a 
> patch added several years ago to try and cover up some of the 
> weaknesses of the SCSI stack!  Now that the SCSI stack is in better 
> shape, maybe my patch should be included in the mainstream kernel.  The 
> patch is below; see what you think.

Sounds good to me, to disconnect a dead object from its parent when it's
deleted. We only need to protect for "use after free" but not lock the
parent, I guess. We should give it a try.

Thanks,
Kay

[-- Attachment #2: Type: TEXT/X-PATCH, Size: 1258 bytes --]

diff --git a/drivers/scsi/scsi_sysfs.c b/drivers/scsi/scsi_sysfs.c


index daed37d..577bbf3 100644


--- a/drivers/scsi/scsi_sysfs.c


+++ b/drivers/scsi/scsi_sysfs.c


@@ -280,15 +280,6 @@ static void scsi_device_dev_release_usercontext(struct work_struct *work)


 	list_del(&sdev->starved_entry);


 	spin_unlock_irqrestore(sdev->host->host_lock, flags);


 


-	if (sdev->request_queue) {


-		sdev->request_queue->queuedata = NULL;


-		/* user context needed to free queue */


-		scsi_free_queue(sdev->request_queue);


-		/* temporary expedient, try to catch use of queue lock


-		 * after free of sdev */


-		sdev->request_queue = NULL;


-	}


-


 	scsi_target_reap(scsi_target(sdev));


 


 	kfree(sdev->inquiry);


@@ -799,6 +790,16 @@ void __scsi_remove_device(struct scsi_device *sdev)


 	if (sdev->host->hostt->slave_destroy)


 		sdev->host->hostt->slave_destroy(sdev);


 	transport_destroy_device(dev);


+


+	if (sdev->request_queue) {


+		sdev->request_queue->queuedata = NULL;


+		/* user context needed to free queue */


+		scsi_free_queue(sdev->request_queue);


+		/* temporary expedient, try to catch use of queue lock


+		 * after free of sdev */


+		sdev->request_queue = NULL;


+	}


+


 	put_device(dev);


 }


 



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

end of thread, other threads:[~2007-10-31 18:38 UTC | newest]

Thread overview: 28+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2007-10-29 15:18 BUG in: Driver core: convert block from raw kobjects to core devices (fwd) Alan Stern
2007-10-29 15:35 ` James Bottomley
2007-10-29 16:38   ` Alan Stern
2007-10-29 16:45     ` James Bottomley
2007-10-29 17:04       ` Alan Stern
2007-10-29 18:47   ` Alan Stern
2007-10-29 19:13     ` Kay Sievers
2007-10-31  4:25       ` Greg KH
2007-10-31 10:46         ` Kay Sievers
2007-10-31 14:32           ` Greg KH
2007-10-31 15:15             ` James Bottomley
2007-10-31 15:40               ` Kay Sievers
2007-10-31 15:47                 ` James Bottomley
2007-10-31 16:04                   ` Alan Stern
2007-10-31 16:13                     ` James Bottomley
2007-10-31 16:24                       ` Kay Sievers
2007-10-31 16:31                         ` James Bottomley
2007-10-31 16:42                           ` Kay Sievers
2007-10-31 16:46                             ` James Bottomley
2007-10-31 17:32                               ` Kay Sievers
2007-10-31 18:36                               ` Alan Stern
2007-10-31 16:44                           ` Alan Stern
2007-10-31 17:07                             ` James Bottomley
2007-10-31 18:38                               ` Alan Stern
2007-10-31 15:58               ` Alan Stern
2007-10-31 16:11                 ` James Bottomley
2007-10-31  4:24     ` Greg KH
2007-10-31 15:51       ` Alan Stern

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox