public inbox for linux-acpi@vger.kernel.org
 help / color / mirror / Atom feed
From: Dmitry Torokhov <dmitry.torokhov@gmail.com>
To: Len Brown <len.brown@intel.com>, Rui Zhang <rui.zhang@intel.com>
Cc: linux-acpi@vger.kernel.org
Subject: [patch 3/8] ACPI: video - remove unsafe uses of list_for_each_safe()
Date: Mon, 05 Nov 2007 11:43:31 -0500	[thread overview]
Message-ID: <20071105165011.953107289@anvil.corenet.prv> (raw)
In-Reply-To: 20071105164328.982283020@anvil.corenet.prv

[-- Attachment #1: acpi-video-dont-use-list-safe.patch --]
[-- Type: text/plain, Size: 4479 bytes --]

ACPI: video - remove unsafe uses of list_for_each_safe()

list_for_each_safe() only protects list from list alterations
performed by the same thread. One still needs to implement
proper locking when list is being accessed from several threads.

Signed-off-by: Dmitry Torokhov <dtor@mail.ru>
---
 drivers/acpi/video.c |   71 ++++++++++++++++++++++++---------------------------
 1 file changed, 34 insertions(+), 37 deletions(-)

Index: work/drivers/acpi/video.c
===================================================================
--- work.orig/drivers/acpi/video.c
+++ work/drivers/acpi/video.c
@@ -1462,12 +1462,14 @@ acpi_video_bus_get_one_device(struct acp
 
 static void acpi_video_device_rebind(struct acpi_video_bus *video)
 {
-	struct list_head *node, *next;
-	list_for_each_safe(node, next, &video->video_device_list) {
-		struct acpi_video_device *dev =
-		    container_of(node, struct acpi_video_device, entry);
+	struct acpi_video_device *dev;
+
+	down(&video->sem);
+
+	list_for_each_entry(dev, &video->video_device_list, entry)
 		acpi_video_device_bind(video, dev);
-	}
+
+	up(&video->sem);
 }
 
 /*
@@ -1592,30 +1594,33 @@ static int acpi_video_device_enumerate(s
 
 static int acpi_video_switch_output(struct acpi_video_bus *video, int event)
 {
-	struct list_head *node, *next;
+	struct list_head *node;
 	struct acpi_video_device *dev = NULL;
 	struct acpi_video_device *dev_next = NULL;
 	struct acpi_video_device *dev_prev = NULL;
 	unsigned long state;
 	int status = 0;
 
+	down(&video->sem);
 
-	list_for_each_safe(node, next, &video->video_device_list) {
+	list_for_each(node, &video->video_device_list) {
 		dev = container_of(node, struct acpi_video_device, entry);
 		status = acpi_video_device_get_state(dev, &state);
 		if (state & 0x2) {
-			dev_next =
-			    container_of(node->next, struct acpi_video_device,
-					 entry);
-			dev_prev =
-			    container_of(node->prev, struct acpi_video_device,
-					 entry);
+			dev_next = container_of(node->next,
+					struct acpi_video_device, entry);
+			dev_prev = container_of(node->prev,
+					struct acpi_video_device, entry);
 			goto out;
 		}
 	}
+
 	dev_next = container_of(node->next, struct acpi_video_device, entry);
 	dev_prev = container_of(node->prev, struct acpi_video_device, entry);
-      out:
+
+ out:
+	up(&video->sem);
+
 	switch (event) {
 	case ACPI_VIDEO_NOTIFY_CYCLE:
 	case ACPI_VIDEO_NOTIFY_NEXT_OUTPUT:
@@ -1691,24 +1696,17 @@ acpi_video_bus_get_devices(struct acpi_v
 			   struct acpi_device *device)
 {
 	int status = 0;
-	struct list_head *node, *next;
-
+	struct acpi_device *dev;
 
 	acpi_video_device_enumerate(video);
 
-	list_for_each_safe(node, next, &device->children) {
-		struct acpi_device *dev =
-		    list_entry(node, struct acpi_device, node);
-
-		if (!dev)
-			continue;
+	list_for_each_entry(dev, &device->children, node) {
 
 		status = acpi_video_bus_get_one_device(dev, video);
 		if (ACPI_FAILURE(status)) {
 			ACPI_EXCEPTION((AE_INFO, status, "Cant attach device"));
 			continue;
 		}
-
 	}
 	return status;
 }
@@ -1724,9 +1722,6 @@ static int acpi_video_bus_put_one_device
 
 	video = device->video;
 
-	down(&video->sem);
-	list_del(&device->entry);
-	up(&video->sem);
 	acpi_video_device_remove_fs(device->dev);
 
 	status = acpi_remove_notify_handler(device->dev->handle,
@@ -1734,32 +1729,34 @@ static int acpi_video_bus_put_one_device
 					    acpi_video_device_notify);
 	backlight_device_unregister(device->backlight);
 	video_output_unregister(device->output_dev);
+
 	return 0;
 }
 
 static int acpi_video_bus_put_devices(struct acpi_video_bus *video)
 {
 	int status;
-	struct list_head *node, *next;
+	struct acpi_video_device *dev, *next;
 
+	down(&video->sem);
 
-	list_for_each_safe(node, next, &video->video_device_list) {
-		struct acpi_video_device *data =
-		    list_entry(node, struct acpi_video_device, entry);
-		if (!data)
-			continue;
+	list_for_each_entry_safe(dev, next, &video->video_device_list, entry) {
 
-		status = acpi_video_bus_put_one_device(data);
+		status = acpi_video_bus_put_one_device(dev);
 		if (ACPI_FAILURE(status))
 			printk(KERN_WARNING PREFIX
 			       "hhuuhhuu bug in acpi video driver.\n");
 
-		if (data->brightness)
-			kfree(data->brightness->levels);
-		kfree(data->brightness);
-		kfree(data);
+		if (dev->brightness) {
+			kfree(dev->brightness->levels);
+			kfree(dev->brightness);
+		}
+		list_del(&dev->entry);
+		kfree(dev);
 	}
 
+	up(&video->sem);
+
 	return 0;
 }
 

-- 
Dmitry


  parent reply	other threads:[~2007-11-05 16:52 UTC|newest]

Thread overview: 30+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2007-11-05 16:43 [patch 0/8] ACPI Video various cleanups & fixes Dmitry Torokhov
2007-11-05 16:43 ` [patch 1/8] ACPI: video - fit input device into sysfs tree Dmitry Torokhov
2007-11-14  7:17   ` Zhang Rui
2007-11-14 16:49     ` Len Brown
2007-11-05 16:43 ` [patch 2/8] ACPI: video - add missing input_free_device() Dmitry Torokhov
2007-11-14  7:17   ` Zhang Rui
2007-11-14 17:00   ` Len Brown
2007-11-05 16:43 ` Dmitry Torokhov [this message]
2007-11-14  7:17   ` [patch 3/8] ACPI: video - remove unsafe uses of list_for_each_safe() Zhang Rui
2007-11-14 17:46   ` Len Brown
2007-11-05 16:43 ` [patch 4/8] ACPI: video - convert semaphore to a mutex Dmitry Torokhov
2007-11-14  7:17   ` Zhang Rui
2007-11-14 17:18     ` Len Brown
2007-11-05 16:43 ` [patch 5/8] ACPI: video - simplify handling of attached devices Dmitry Torokhov
2007-11-14  7:17   ` Zhang Rui
2007-11-14 17:34     ` Len Brown
2007-11-05 16:43 ` [patch 6/8] ACPI: video - properly handle errors when registering proc elements Dmitry Torokhov
2007-11-14  7:18   ` Zhang Rui
2007-11-14 13:36     ` Henrique de Moraes Holschuh
2007-11-14 15:06       ` Dmitry Torokhov
2007-11-14 17:22         ` Len Brown
2007-11-14 17:38     ` Len Brown
2007-11-05 16:43 ` [patch 7/8] ACPI: video - more cleanups Dmitry Torokhov
2007-11-14  7:18   ` Zhang Rui
2007-11-14 17:42     ` Len Brown
2007-11-14 17:47       ` Len Brown
2007-11-05 16:43 ` [patch 8/8] ACPI: video - fix permissions on some proc entries Dmitry Torokhov
2007-11-14  7:18   ` Zhang Rui
2007-11-14  7:17 ` [patch 0/8] ACPI Video various cleanups & fixes Zhang Rui
2007-11-14 15:04   ` Dmitry Torokhov

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20071105165011.953107289@anvil.corenet.prv \
    --to=dmitry.torokhov@gmail.com \
    --cc=len.brown@intel.com \
    --cc=linux-acpi@vger.kernel.org \
    --cc=rui.zhang@intel.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox