Skip to contentSkip to conversation

device_shutdown can loop if the driver frees itself

PATCH New Michael Richardson 1 participant 1 message Jan 5, 2006

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 device_shutdown can loop if the driver frees itself

This patch changes device_shutdown() to use a newly introduce safe reverse list traversal. We experienced loops on system reboot if we had removed and re-inserted our device from the device list.

We noticed this problem on PPC405. Our PCI IDE device comes and goes a lot.

Our hypothesis was that there was a loop caused by the driver->shutdown freeing memory. It is possible that we do something wrong as well, but being unable to reboot is kind of nasty.

---

 drivers/base/power/shutdown.c |    4 ++--
 include/linux/list.h          |   13 +++++++++++++
 2 files changed, 15 insertions(+), 2 deletions(-)

applies-to: 96a0b5842e24d6736c698cdbd820a3ad9d8d9f10 49291875d63ec697510fc4cc9a1766b7400ee2d3

drivers/base/power/shutdown.c +2 -2
index f50a08b..3a417c8 100644
@@ -35,10 +35,10 @@ extern int sysdev_shutdown(void);
  */
 void device_shutdown(void)
 {
-	struct device * dev;
+	struct device * dev, *devn;
 
 	down_write(&devices_subsys.rwsem);
-	list_for_each_entry_reverse(dev, &devices_subsys.kset.list,
+	list_for_each_entry_safe_reverse(dev, devn, &devices_subsys.kset.list,
 				kobj.entry) {
 		if (dev->driver && dev->driver->shutdown) {
 			dev_dbg(dev, "shutdown\n");
include/linux/list.h +13 -0
index 8e33882..b31ec32 100644
@@ -436,6 +436,19 @@ static inline void list_splice_init(stru
 	     pos = n, n = list_entry(n->member.next, typeof(*n), member))
 
 /**
+ * list_for_each_entry_safe_reverse - iterate backwards over list of given type safe against removal of list entry
+ * @pos:	the type * to use as a loop counter.
+ * @n:          another type * to use as temporary storage
+ * @head:	the head for your list.
+ * @member:	the name of the list_struct within the struct.
+ */
+#define list_for_each_entry_safe_reverse(pos, n, head, member)		\
+	for (pos = list_entry((head)->prev, typeof(*pos), member),	\
+		     n = list_entry(pos->member.next, typeof(*pos), member); \
+	     &pos->member != (head); 	\
+	     pos = n, n = list_entry(n->member.prev, typeof(*n), member))
+
+/**
  * list_for_each_rcu	-	iterate over an rcu-protected list
  * @pos:	the &struct list_head to use as a loop counter.
  * @head:	the head for your list.

---
0.99.9.GIT