Skip to content

Commit ffedb45

Browse files
James BottomleyJames Bottomley
authored andcommitted
[SCSI] fix scsi process problems and clean up the target reap issues
In order to use the new execute_in_process_context() API, you have to provide it with the work storage, which I do in SCSI in scsi_device and scsi_target, but which also means that we can no longer queue up the target reaps, so instead I moved the target to a state model which allows target_alloc to detect if we've received a dying target and wait for it to be gone. Hopefully, this should also solve the target namespace race. Signed-off-by: James Bottomley <James.Bottomley@SteelEye.com>
1 parent 1fa44ec commit ffedb45

5 files changed

Lines changed: 46 additions & 77 deletions

File tree

‎drivers/scsi/scsi_lib.c‎

Lines changed: 0 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -2257,61 +2257,3 @@ scsi_target_unblock(struct device *dev)
22572257
device_for_each_child(dev, NULL, target_unblock);
22582258
}
22592259
EXPORT_SYMBOL_GPL(scsi_target_unblock);
2260-
2261-
2262-
struct work_queue_work {
2263-
struct work_struct work;
2264-
void (*fn)(void *);
2265-
void *data;
2266-
};
2267-
2268-
static void execute_in_process_context_work(void *data)
2269-
{
2270-
void (*fn)(void *data);
2271-
struct work_queue_work *wqw = data;
2272-
2273-
fn = wqw->fn;
2274-
data = wqw->data;
2275-
2276-
kfree(wqw);
2277-
2278-
fn(data);
2279-
}
2280-
2281-
/**
2282-
* scsi_execute_in_process_context - reliably execute the routine with user context
2283-
* @fn: the function to execute
2284-
* @data: data to pass to the function
2285-
*
2286-
* Executes the function immediately if process context is available,
2287-
* otherwise schedules the function for delayed execution.
2288-
*
2289-
* Returns: 0 - function was executed
2290-
* 1 - function was scheduled for execution
2291-
* <0 - error
2292-
*/
2293-
int scsi_execute_in_process_context(void (*fn)(void *data), void *data)
2294-
{
2295-
struct work_queue_work *wqw;
2296-
2297-
if (!in_interrupt()) {
2298-
fn(data);
2299-
return 0;
2300-
}
2301-
2302-
wqw = kmalloc(sizeof(struct work_queue_work), GFP_ATOMIC);
2303-
2304-
if (unlikely(!wqw)) {
2305-
printk(KERN_ERR "Failed to allocate memory\n");
2306-
WARN_ON(1);
2307-
return -ENOMEM;
2308-
}
2309-
2310-
INIT_WORK(&wqw->work, execute_in_process_context_work, wqw);
2311-
wqw->fn = fn;
2312-
wqw->data = data;
2313-
schedule_work(&wqw->work);
2314-
2315-
return 1;
2316-
}
2317-
EXPORT_SYMBOL_GPL(scsi_execute_in_process_context);

‎drivers/scsi/scsi_scan.c‎

Lines changed: 33 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -349,6 +349,8 @@ static struct scsi_target *scsi_alloc_target(struct device *parent,
349349
starget->channel = channel;
350350
INIT_LIST_HEAD(&starget->siblings);
351351
INIT_LIST_HEAD(&starget->devices);
352+
starget->state = STARGET_RUNNING;
353+
retry:
352354
spin_lock_irqsave(shost->host_lock, flags);
353355

354356
found_target = __scsi_find_target(parent, channel, id);
@@ -381,8 +383,15 @@ static struct scsi_target *scsi_alloc_target(struct device *parent,
381383
found_target->reap_ref++;
382384
spin_unlock_irqrestore(shost->host_lock, flags);
383385
put_device(parent);
384-
kfree(starget);
385-
return found_target;
386+
if (found_target->state != STARGET_DEL) {
387+
kfree(starget);
388+
return found_target;
389+
}
390+
/* Unfortunately, we found a dying target; need to
391+
* wait until it's dead before we can get a new one */
392+
put_device(&found_target->dev);
393+
flush_scheduled_work();
394+
goto retry;
386395
}
387396

388397
static void scsi_target_reap_usercontext(void *data)
@@ -391,21 +400,13 @@ static void scsi_target_reap_usercontext(void *data)
391400
struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
392401
unsigned long flags;
393402

403+
transport_remove_device(&starget->dev);
404+
device_del(&starget->dev);
405+
transport_destroy_device(&starget->dev);
394406
spin_lock_irqsave(shost->host_lock, flags);
395-
396-
if (--starget->reap_ref == 0 && list_empty(&starget->devices)) {
397-
list_del_init(&starget->siblings);
398-
spin_unlock_irqrestore(shost->host_lock, flags);
399-
transport_remove_device(&starget->dev);
400-
device_del(&starget->dev);
401-
transport_destroy_device(&starget->dev);
402-
put_device(&starget->dev);
403-
return;
404-
405-
}
407+
list_del_init(&starget->siblings);
406408
spin_unlock_irqrestore(shost->host_lock, flags);
407-
408-
return;
409+
put_device(&starget->dev);
409410
}
410411

411412
/**
@@ -419,7 +420,23 @@ static void scsi_target_reap_usercontext(void *data)
419420
*/
420421
void scsi_target_reap(struct scsi_target *starget)
421422
{
422-
scsi_execute_in_process_context(scsi_target_reap_usercontext, starget);
423+
struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
424+
unsigned long flags;
425+
426+
spin_lock_irqsave(shost->host_lock, flags);
427+
428+
if (--starget->reap_ref == 0 && list_empty(&starget->devices)) {
429+
BUG_ON(starget->state == STARGET_DEL);
430+
starget->state = STARGET_DEL;
431+
spin_unlock_irqrestore(shost->host_lock, flags);
432+
execute_in_process_context(scsi_target_reap_usercontext,
433+
starget, &starget->ew);
434+
return;
435+
436+
}
437+
spin_unlock_irqrestore(shost->host_lock, flags);
438+
439+
return;
423440
}
424441

425442
/**

‎drivers/scsi/scsi_sysfs.c‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,9 @@ static void scsi_device_dev_release_usercontext(void *data)
256256

257257
static void scsi_device_dev_release(struct device *dev)
258258
{
259-
scsi_execute_in_process_context(scsi_device_dev_release_usercontext, dev);
259+
struct scsi_device *sdp = to_scsi_device(dev);
260+
execute_in_process_context(scsi_device_dev_release_usercontext, dev,
261+
&sdp->ew);
260262
}
261263

262264
static struct class sdev_class = {

‎include/scsi/scsi.h‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -433,6 +433,4 @@ struct scsi_lun {
433433
/* Used to obtain the PCI location of a device */
434434
#define SCSI_IOCTL_GET_PCI 0x5387
435435

436-
int scsi_execute_in_process_context(void (*fn)(void *data), void *data);
437-
438436
#endif /* _SCSI_SCSI_H */

‎include/scsi/scsi_device.h‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include <linux/device.h>
55
#include <linux/list.h>
66
#include <linux/spinlock.h>
7+
#include <linux/workqueue.h>
78
#include <asm/atomic.h>
89

910
struct request_queue;
@@ -137,6 +138,8 @@ struct scsi_device {
137138
struct device sdev_gendev;
138139
struct class_device sdev_classdev;
139140

141+
struct execute_work ew; /* used to get process context on put */
142+
140143
enum scsi_device_state sdev_state;
141144
unsigned long sdev_data[0];
142145
} __attribute__((aligned(sizeof(unsigned long))));
@@ -153,6 +156,11 @@ struct scsi_device {
153156
#define scmd_printk(prefix, scmd, fmt, a...) \
154157
dev_printk(prefix, &(scmd)->device->sdev_gendev, fmt, ##a)
155158

159+
enum scsi_target_state {
160+
STARGET_RUNNING = 1,
161+
STARGET_DEL,
162+
};
163+
156164
/*
157165
* scsi_target: representation of a scsi target, for now, this is only
158166
* used for single_lun devices. If no one has active IO to the target,
@@ -172,6 +180,8 @@ struct scsi_target {
172180
/* means no lun present */
173181

174182
char scsi_level;
183+
struct execute_work ew;
184+
enum scsi_target_state state;
175185
void *hostdata; /* available to low-level driver */
176186
unsigned long starget_data[0]; /* for the transport */
177187
/* starget_data must be the last element!!!! */

0 commit comments

Comments
 (0)