Commit 53c165e0 authored by James Bottomley's avatar James Bottomley Committed by James Bottomley

[SCSI] correct attribute_container list usage

One of the changes in the attribute_container code in the scsi-misc tree
was to add a lock to protect the list of devices per container.  This,
unfortunately, leads to potential scheduling while atomic problems if
there's a sleep in the function called by a trigger.

The correct solution is to use the kernel klist infrastructure instead
which allows lockless traversal of a list.
Signed-off-by: default avatarJames Bottomley <James.Bottomley@SteelEye.com>
parent 51490c89
...@@ -22,7 +22,7 @@ ...@@ -22,7 +22,7 @@
/* This is a private structure used to tie the classdev and the /* This is a private structure used to tie the classdev and the
* container .. it should never be visible outside this file */ * container .. it should never be visible outside this file */
struct internal_container { struct internal_container {
struct list_head node; struct klist_node node;
struct attribute_container *cont; struct attribute_container *cont;
struct class_device classdev; struct class_device classdev;
}; };
...@@ -57,8 +57,7 @@ int ...@@ -57,8 +57,7 @@ int
attribute_container_register(struct attribute_container *cont) attribute_container_register(struct attribute_container *cont)
{ {
INIT_LIST_HEAD(&cont->node); INIT_LIST_HEAD(&cont->node);
INIT_LIST_HEAD(&cont->containers); klist_init(&cont->containers);
spin_lock_init(&cont->containers_lock);
down(&attribute_container_mutex); down(&attribute_container_mutex);
list_add_tail(&cont->node, &attribute_container_list); list_add_tail(&cont->node, &attribute_container_list);
...@@ -78,13 +77,13 @@ attribute_container_unregister(struct attribute_container *cont) ...@@ -78,13 +77,13 @@ attribute_container_unregister(struct attribute_container *cont)
{ {
int retval = -EBUSY; int retval = -EBUSY;
down(&attribute_container_mutex); down(&attribute_container_mutex);
spin_lock(&cont->containers_lock); spin_lock(&cont->containers.k_lock);
if (!list_empty(&cont->containers)) if (!list_empty(&cont->containers.k_list))
goto out; goto out;
retval = 0; retval = 0;
list_del(&cont->node); list_del(&cont->node);
out: out:
spin_unlock(&cont->containers_lock); spin_unlock(&cont->containers.k_lock);
up(&attribute_container_mutex); up(&attribute_container_mutex);
return retval; return retval;
...@@ -143,7 +142,6 @@ attribute_container_add_device(struct device *dev, ...@@ -143,7 +142,6 @@ attribute_container_add_device(struct device *dev,
continue; continue;
} }
memset(ic, 0, sizeof(struct internal_container)); memset(ic, 0, sizeof(struct internal_container));
INIT_LIST_HEAD(&ic->node);
ic->cont = cont; ic->cont = cont;
class_device_initialize(&ic->classdev); class_device_initialize(&ic->classdev);
ic->classdev.dev = get_device(dev); ic->classdev.dev = get_device(dev);
...@@ -154,13 +152,22 @@ attribute_container_add_device(struct device *dev, ...@@ -154,13 +152,22 @@ attribute_container_add_device(struct device *dev,
fn(cont, dev, &ic->classdev); fn(cont, dev, &ic->classdev);
else else
attribute_container_add_class_device(&ic->classdev); attribute_container_add_class_device(&ic->classdev);
spin_lock(&cont->containers_lock); klist_add_tail(&ic->node, &cont->containers);
list_add_tail(&ic->node, &cont->containers);
spin_unlock(&cont->containers_lock);
} }
up(&attribute_container_mutex); up(&attribute_container_mutex);
} }
/* FIXME: can't break out of this unless klist_iter_exit is also
* called before doing the break
*/
#define klist_for_each_entry(pos, head, member, iter) \
for (klist_iter_init(head, iter); (pos = ({ \
struct klist_node *n = klist_next(iter); \
n ? ({ klist_iter_exit(iter) ; NULL; }) : \
container_of(n, typeof(*pos), member);\
}) ) != NULL; )
/** /**
* attribute_container_remove_device - make device eligible for removal. * attribute_container_remove_device - make device eligible for removal.
* *
...@@ -187,18 +194,19 @@ attribute_container_remove_device(struct device *dev, ...@@ -187,18 +194,19 @@ attribute_container_remove_device(struct device *dev,
down(&attribute_container_mutex); down(&attribute_container_mutex);
list_for_each_entry(cont, &attribute_container_list, node) { list_for_each_entry(cont, &attribute_container_list, node) {
struct internal_container *ic, *tmp; struct internal_container *ic;
struct klist_iter iter;
if (attribute_container_no_classdevs(cont)) if (attribute_container_no_classdevs(cont))
continue; continue;
if (!cont->match(cont, dev)) if (!cont->match(cont, dev))
continue; continue;
spin_lock(&cont->containers_lock);
list_for_each_entry_safe(ic, tmp, &cont->containers, node) { klist_for_each_entry(ic, &cont->containers, node, &iter) {
if (dev != ic->classdev.dev) if (dev != ic->classdev.dev)
continue; continue;
list_del(&ic->node); klist_remove(&ic->node);
if (fn) if (fn)
fn(cont, dev, &ic->classdev); fn(cont, dev, &ic->classdev);
else { else {
...@@ -206,7 +214,6 @@ attribute_container_remove_device(struct device *dev, ...@@ -206,7 +214,6 @@ attribute_container_remove_device(struct device *dev,
class_device_unregister(&ic->classdev); class_device_unregister(&ic->classdev);
} }
} }
spin_unlock(&cont->containers_lock);
} }
up(&attribute_container_mutex); up(&attribute_container_mutex);
} }
...@@ -232,7 +239,8 @@ attribute_container_device_trigger(struct device *dev, ...@@ -232,7 +239,8 @@ attribute_container_device_trigger(struct device *dev,
down(&attribute_container_mutex); down(&attribute_container_mutex);
list_for_each_entry(cont, &attribute_container_list, node) { list_for_each_entry(cont, &attribute_container_list, node) {
struct internal_container *ic, *tmp; struct internal_container *ic;
struct klist_iter iter;
if (!cont->match(cont, dev)) if (!cont->match(cont, dev))
continue; continue;
...@@ -242,12 +250,10 @@ attribute_container_device_trigger(struct device *dev, ...@@ -242,12 +250,10 @@ attribute_container_device_trigger(struct device *dev,
continue; continue;
} }
spin_lock(&cont->containers_lock); klist_for_each_entry(ic, &cont->containers, node, &iter) {
list_for_each_entry_safe(ic, tmp, &cont->containers, node) {
if (dev == ic->classdev.dev) if (dev == ic->classdev.dev)
fn(cont, dev, &ic->classdev); fn(cont, dev, &ic->classdev);
} }
spin_unlock(&cont->containers_lock);
} }
up(&attribute_container_mutex); up(&attribute_container_mutex);
} }
...@@ -397,15 +403,16 @@ attribute_container_find_class_device(struct attribute_container *cont, ...@@ -397,15 +403,16 @@ attribute_container_find_class_device(struct attribute_container *cont,
{ {
struct class_device *cdev = NULL; struct class_device *cdev = NULL;
struct internal_container *ic; struct internal_container *ic;
struct klist_iter iter;
spin_lock(&cont->containers_lock); klist_for_each_entry(ic, &cont->containers, node, &iter) {
list_for_each_entry(ic, &cont->containers, node) {
if (ic->classdev.dev == dev) { if (ic->classdev.dev == dev) {
cdev = &ic->classdev; cdev = &ic->classdev;
/* FIXME: must exit iterator then break */
klist_iter_exit(&iter);
break; break;
} }
} }
spin_unlock(&cont->containers_lock);
return cdev; return cdev;
} }
......
...@@ -11,12 +11,12 @@ ...@@ -11,12 +11,12 @@
#include <linux/device.h> #include <linux/device.h>
#include <linux/list.h> #include <linux/list.h>
#include <linux/klist.h>
#include <linux/spinlock.h> #include <linux/spinlock.h>
struct attribute_container { struct attribute_container {
struct list_head node; struct list_head node;
struct list_head containers; struct klist containers;
spinlock_t containers_lock;
struct class *class; struct class *class;
struct class_device_attribute **attrs; struct class_device_attribute **attrs;
int (*match)(struct attribute_container *, struct device *); int (*match)(struct attribute_container *, struct device *);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment