Skip to contentSkip to conversation

ch: add refcounting

PATCH In review Hannes Reinecke 2 participants 4 messages Apr 8, 2014 – Apr 25, 2014 SCSI

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 ch: add refcounting

The 'scsi_changer' structure needs refcounting, as the device might be removed while the fd is still open.

---
 drivers/scsi/ch.c | 16 ++++++++++++++--
 1 file changed, 14 insertions(+), 2 deletions(-)
drivers/scsi/ch.c +14 -2
index 2a32374..adb5426 100644
@@ -102,6 +102,7 @@ do {									\
 static struct class * ch_sysfs_class;
 
 typedef struct {
+	struct kref         ref;
 	struct list_head    list;
 	int                 minor;
 	char                name[8];
@@ -565,13 +566,23 @@ static int ch_gstatus(scsi_changer *ch, int type, unsigned char __user *dest)
 
 /* ------------------------------------------------------------------------ */
 
+static void ch_destroy(struct kref *ref)
+{
+	scsi_changer *ch = container_of(ref, scsi_changer, ref);
+
+	kfree(ch->dt);
+	kfree(ch);
+}
+
 static int
 ch_release(struct inode *inode, struct file *file)
 {
 	scsi_changer *ch = file->private_data;
 
 	scsi_device_put(ch->device);
+	ch->device = NULL;
 	file->private_data = NULL;
+	kref_put(&ch->ref, ch_destroy);
 	return 0;
 }
 
@@ -590,6 +601,7 @@ ch_open(struct inode *inode, struct file *file)
 		mutex_unlock(&ch_mutex);
 		return -ENXIO;
 	}
+	kref_get(&ch->ref);
 	spin_unlock(&ch_index_lock);
 
 	file->private_data = ch;
@@ -931,6 +943,7 @@ static int ch_probe(struct device *dev)
 	}
 
 	mutex_init(&ch->lock);
+	kref_init(&ch->ref);
 	ch->device = sd;
 	ch_readconfig(ch);
 	if (init)
@@ -956,8 +969,7 @@ static int ch_remove(struct device *dev)
 	spin_unlock(&ch_index_lock);
 
 	device_destroy(ch_sysfs_class, MKDEV(SCSI_CHANGER_MAJOR,ch->minor));
-	kfree(ch->dt);
-	kfree(ch);
+	kref_put(&ch->ref, ch_destroy);
 	return 0;
 }
 
Signature · 1 line
1.7.12.4
#1 lore raw

drivers/scsi/ch.c static int ch_gstatus(scsi_changer *ch, int type, unsigned char __user *dest) · line 585

Quoted 9 lines

static int
ch_release(struct inode *inode, struct file *file) {

drivers/scsi/ch.c +2 -0
 	scsi_changer *ch = file->private_data;
 
 	scsi_device_put(ch->device);
+	ch->device = NULL;
 	file->private_data = NULL;
+	kref_put(&ch->ref, ch_destroy);

Any reason you need to put the scsi_device here already? Defering this would give you much eaiser life time rules, and no need to deal with a NULL ch->device ever.

#2 lore raw
Quoted 5 lines · Christoph Hellwig
Quoted 5 lines

static int
ch_release(struct inode *inode, struct file *file) {
scsi_changer *ch = file->private_data;

Quoted 9 lines

drivers/scsi/ch.c static int ch_gstatus(scsi_changer *ch, int type, unsigned char __user *dest) · line 585

Quoted 4 lines
drivers/scsi/ch.c +2 -0
 	scsi_device_put(ch->device);
+	ch->device = NULL;
 	file->private_data = NULL;
+	kref_put(&ch->ref, ch_destroy);

Any reason you need to put the scsi_device here already? Defering this would give you much eaiser life time rules, and no need to deal with a NULL ch->device ever.

Sure. But this would require a far more in-depth analysis of the lifetime of the ch object, and most likely a far more intrusive patch. You're welcome to do so :-)

This patch is just a minimal fix; I didn't dare to change too much of the internals.

Cheers,

Hannes

Signature · 8 lines
Dr. Hannes Reinecke		      zSeries & Storage
[email protected]			      +49 911 74053 688
SUSE LINUX Products GmbH, Maxfeldstr. 5, 90409 Nürnberg
GF: J. Hawn, J. Guild, F. Imendörffer, HRB 16746 (AG Nürnberg)
--
To unsubscribe from this list: send the line "unsubscribe linux-scsi" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Reviewed-by
#3 lore raw
Quoted 15 lines · Hannes Reinecke
Quoted 5 lines · Christoph Hellwig
Quoted 5 lines

static int
ch_release(struct inode *inode, struct file *file) {
scsi_changer *ch = file->private_data;

Quoted 9 lines

drivers/scsi/ch.c static int ch_gstatus(scsi_changer *ch, int type, unsigned char __user *dest) · line 585

Quoted 4 lines
drivers/scsi/ch.c +2 -0
 	scsi_device_put(ch->device);
+	ch->device = NULL;
 	file->private_data = NULL;
+	kref_put(&ch->ref, ch_destroy);

Any reason you need to put the scsi_device here already? Defering this would give you much eaiser life time rules, and no need to deal with a NULL ch->device ever.

Quoted 6 lines

Sure. But this would require a far more in-depth analysis of the lifetime of the ch object, and most likely a far more intrusive patch. You're welcome to do so :-)

This patch is just a minimal fix; I didn't dare to change too much of the internals.

Oh well, let's go with your simpler version for now.