Skip to contentSkip to conversation

fix sd open/remove race

PATCH Alan Stern 2 participants 9 messages Apr 9, 2004 1 message not in the archive SCSI

lore
Share & tools
lore

Alan Stern

author
#1 lore raw
In reply to a message not in the archive
Quoted 3 lines · Patrick Mansfield

James posted this sd.c patch to fix the open/remove race:

http://marc.theaimsgroup.com/?l=linux-scsi&m=108130838613268&w=2

The patch is almost but not quite correct. It acquires the new semaphore unnecessarily during scsi_disk_put, it fails to acquire it when erasing the gendisk private_data, and it needs to erase the private_data during sd_remove (not scsi_disk_release). It also contains a syntax error.

This patch, applied on top of James's, should fix things up. It compiles okay, but I haven't tried running it.

Alan Stern

drivers/scsi/sd.c renamed from drivers/scsi/sd.c.orig +6 -7
@@ -162,7 +162,7 @@
 
 /* reverse mapping dev -> (sd_nr, part) not currently needed */
 
-#define to_scsi_disk(obj) container_of(obj,struct scsi_disk,kobj);
+#define to_scsi_disk(obj) container_of(obj,struct scsi_disk,kobj)
 
 static inline struct scsi_disk *scsi_disk(struct gendisk *disk)
 {
@@ -186,9 +186,7 @@
 
 static void scsi_disk_put(struct scsi_disk *sdkp)
 {
-	down(&sd_ref_sem);
 	kobject_put(&sdkp->kobj);
-	up(&sd_ref_sem);
 }
 
 /**
@@ -1442,6 +1440,10 @@
 {
 	struct scsi_disk *sdkp = dev_get_drvdata(dev);
 
+	down(&sd_ref_sem);
+	sdkp->disk->private_data = NULL;
+	up(&sd_ref_sem);
+
 	del_gendisk(sdkp->disk);
 	sd_shutdown(dev);
 	kobject_put(&sdkp->kobj);
@@ -1457,15 +1459,12 @@
 {
 	struct scsi_disk *sdkp = to_scsi_disk(kobj);
 	struct scsi_device *sdev = sdkp->device;
-	struct gendisk *disk = sdkp->disk;
 	
 	spin_lock(&sd_index_lock);
 	clear_bit(sdkp->index, sd_index_bits);
 	spin_unlock(&sd_index_lock);
 
-	disk->private_data = NULL;
-
-	put_disk(disk);
+	put_disk(sdkp->disk);
 
 	kfree(sdkp);
 
#2 lore raw
Quoted 4 lines · Alan Stern

The patch is almost but not quite correct. It acquires the new semaphore unnecessarily during scsi_disk_put, it fails to acquire it when erasing the gendisk private_data, and it needs to erase the private_data during sd_remove (not scsi_disk_release). It also contains a syntax error.

Heh, the additional semicolon I'll remove.

However, the rest of your patch is wrong.

The reason is that what you're trying to synchronise is the naming lookup (the gendisk open). You have to allow currently open devices to continue to operate. NULL'ing out the disk->private_data before del_gendisk would mean it was null for sd_release, so every attempt to close the device after removal would oops. That's why the NULL must be done in the reference release routine, not in the disconnection indication routine. The race you're trying to prevent is the 0->1 reference after disconnection.

The original patch, By the way, did hold the sd_ref_sem around the NULL of disk->private_data because the scsi_disk_release is triggered from kobject_put on last put (and thus is under the semaphore).

James

author
#3 lore raw
Quoted 1 line · James Bottomley

Heh, the additional semicolon I'll remove.

I'm glad somethings in life aren't controversial! :-)

Quoted 10 lines

However, the rest of your patch is wrong.

The reason is that what you're trying to synchronise is the naming lookup (the gendisk open). You have to allow currently open devices to continue to operate. NULL'ing out the disk->private_data before del_gendisk would mean it was null for sd_release, so every attempt to close the device after removal would oops. That's why the NULL must be done in the reference release routine, not in the disconnection indication routine. The race you're trying to prevent is the 0->1 reference after disconnection.

You're right. However, you still want calls to sd_open to start failing as soon as the disconnection notice is received. Since the disk-unavailable state can't be indicated by clearing disk->private_data, it has to be indicated in some other way. How to do it is up to you, but whatever way you choose, the indicator should be cleared in sd_remove (and that change must be synchronized by acquiring the new semaphore).

Quoted 3 lines

The original patch, By the way, did hold the sd_ref_sem around the NULL of disk->private_data because the scsi_disk_release is triggered from kobject_put on last put (and thus is under the semaphore).

Ah, yes. However there's no reason to hold the semaphore _every_ time you do a scsi_disk_put. And once you use something else as an indicator, there won't be any need to clear disk->private_data.

Alan Stern

#4 lore raw
Quoted 6 lines · Alan Stern

You're right. However, you still want calls to sd_open to start failing as soon as the disconnection notice is received. Since the disk-unavailable state can't be indicated by clearing disk->private_data, it has to be indicated in some other way. How to do it is up to you, but whatever way you choose, the indicator should be cleared in sd_remove (and that change must be synchronized by acquiring the new semaphore).

That's what the original bug2400 thread was all about. I think opening with an already degraded device (one that will EIO to everything) is just as good as refusing the open. That's why the only race is the reference race. It's better to refuse the open, but the effects of either are the same.

Quoted 3 lines

Ah, yes. However there's no reason to hold the semaphore _every_ time you do a scsi_disk_put. And once you use something else as an indicator, there won't be any need to clear disk->private_data.

Actually, there is a (very obscure) three thread SMP race that this prevents. Thread 1 -> sd_remove; Thread 2 -> sd_close (for last close) and Thread 3 -> sd_open

Thread 2 might end up doing the last put and if it were not protected it would still race with sd_open.

James

author
#5 lore raw
Quoted 8 lines · James Bottomley
Quoted 6 lines · Alan Stern

You're right. However, you still want calls to sd_open to start failing as soon as the disconnection notice is received. Since the disk-unavailable state can't be indicated by clearing disk->private_data, it has to be indicated in some other way. How to do it is up to you, but whatever way you choose, the indicator should be cleared in sd_remove (and that change must be synchronized by acquiring the new semaphore).

Quoted 5 lines

That's what the original bug2400 thread was all about. I think opening with an already degraded device (one that will EIO to everything) is just as good as refusing the open. That's why the only race is the reference race. It's better to refuse the open, but the effects of either are the same.

Okay, yes, that will work either way.

Quoted 10 lines
Quoted 3 lines

Ah, yes. However there's no reason to hold the semaphore _every_ time you do a scsi_disk_put. And once you use something else as an indicator, there won't be any need to clear disk->private_data.

Actually, there is a (very obscure) three thread SMP race that this prevents. Thread 1 -> sd_remove; Thread 2 -> sd_close (for last close) and Thread 3 -> sd_open

Thread 2 might end up doing the last put and if it were not protected it would still race with sd_open.

Hmm. That wouldn't matter if you cleared the indicator in sd_remove. Then the race would only be sd_remove against sd_open. Also, you do have to admit, your way of doing it is more obscure. You should at least add comments explaining this 3-way race and the fact that the semaphore is already acquired during scsi_disk_release.

Alan Stern

#6 lore raw
Quoted 5 lines · Alan Stern

Hmm. That wouldn't matter if you cleared the indicator in sd_remove. Then the race would only be sd_remove against sd_open. Also, you do have to admit, your way of doing it is more obscure. You should at least add comments explaining this 3-way race and the fact that the semaphore is already acquired during scsi_disk_release.

But, as I pointed out before, you can't clear the indicator in sd_remove because, fundamentally, that's not where the race is. The fundamental race is the get/put one making sure no-one can get a reference to a freed object.

sd_remove is just the disconnection indication. It triggers a put of the object, causing garbage collection if it's the last put, but that's all it does. To mediate the race, the synchronisation has to be around object acquisition/deallocation.

I can add comments, but the placement of the semaphores is natural to mediate the object acquisition/deallocation race. The three thread illustration was just the first thing that came into my head for explaining what problems could be caused by not doing natural semaphore placement.

James

author
#7 lore raw
Quoted 9 lines · James Bottomley

But, as I pointed out before, you can't clear the indicator in sd_remove because, fundamentally, that's not where the race is. The fundamental race is the get/put one making sure no-one can get a reference to a freed object.

sd_remove is just the disconnection indication. It triggers a put of the object, causing garbage collection if it's the last put, but that's all it does. To mediate the race, the synchronisation has to be around object acquisition/deallocation.

I understand you now. It's really a matter of what one is trying to protect by the synchronization:

You want to prevent anyone from acquiring a reference to the object after it has been deallocated. Hence you use the semaphore around the get and the put.

I want to prevent anyone from acquiring a reference to the object after the driver has decided that no more references should be given out, i.e., after sd_remove has been called. Hence I use the semaphore around the get and in the remove.

Both approaches are valid.

There's a somewhat similar problem that arises in the relation between the SCSI core and the low-level drivers. Here the data structure in question is the host template. (Yes, LLDs don't normally deallocate their host templates, but if they are built as modules they can be unloaded from memory, which has the same effect.) After calling scsi_remove_host() the LLD has no way to know when the SCSI core is finished using the template.

Mike Anderson has suggested that scsi_remove_host could replace the hostt pointer with a pointer to a dummy template that will fail every operation. There may still be difficulties involving the procfs interface, though.

Alan Stern

#8 lore raw
Quoted 4 lines · Alan Stern

I want to prevent anyone from acquiring a reference to the object after the driver has decided that no more references should be given out, i.e., after sd_remove has been called. Hence I use the semaphore around the get and in the remove.

Well, but I think you're going to spend a lot of effort trying to do this. I'm not convinced it's worth it.

Think of the CD ROM unplug that started all this. Supposing the user opens the CD, waits a minute, disconnects it then tries to use the open fd. Both of our schemes are forced to keep the object around giving EIO until the user gets bored and closes it.

since we have to support the open degraded object anyway, why worry about mediating open/disconnect races the only result of which would be to refuse the open if it occurs within the correct window with the disconnect?

Quoted 10 lines

There's a somewhat similar problem that arises in the relation between the SCSI core and the low-level drivers. Here the data structure in question is the host template. (Yes, LLDs don't normally deallocate their host templates, but if they are built as modules they can be unloaded from memory, which has the same effect.) After calling scsi_remove_host() the LLD has no way to know when the SCSI core is finished using the template.

Mike Anderson has suggested that scsi_remove_host could replace the hostt pointer with a pointer to a dummy template that will fail every operation. There may still be difficulties involving the procfs interface, though.

Yes, well, the host needs a proper lifecycle state model to begin with. I'm afraid I've been concentrating on getting the device sorted out first.

James

author
#9 lore raw
Quoted 8 lines · James Bottomley
Quoted 4 lines · Alan Stern

I want to prevent anyone from acquiring a reference to the object after the driver has decided that no more references should be given out, i.e., after sd_remove has been called. Hence I use the semaphore around the get and in the remove.

Well, but I think you're going to spend a lot of effort trying to do this. I'm not convinced it's worth it.

If it was possible to set disk->private_data to NULL within sd_remove, I'd argue that it's no more effort than what you've done. As it is though, I'm forced to agree.

Alan Stern