aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/hwmon/sch56xx-common.c
diff options
context:
space:
mode:
authorHans de Goede <hdegoede@redhat.com>2012-05-22 05:40:27 -0400
committerWim Van Sebroeck <wim@iguana.be>2012-05-30 01:55:46 -0400
commit54e2dc9341aca23d5241699e3b74c8dce609fa2d (patch)
tree0a7ae888e38c2233b957bc0476d81bee7cc3e148 /drivers/hwmon/sch56xx-common.c
parent85a2e40cb5053574cd3b1f33c00194309ce3704c (diff)
watchdog: sch56xx-common: Add proper ref-counting of watchdog data
This fixes referencing free-ed memory in the corner case where /dev/watchdog is open when the platform driver gets unbound from the platform device. Signed-off-by: Hans de Goede <hdegoede@redhat.com> Signed-off-by: Wim Van Sebroeck <wim@iguana.be>
Diffstat (limited to 'drivers/hwmon/sch56xx-common.c')
-rw-r--r--drivers/hwmon/sch56xx-common.c30
1 files changed, 29 insertions, 1 deletions
diff --git a/drivers/hwmon/sch56xx-common.c b/drivers/hwmon/sch56xx-common.c
index 35846cbf1c9c..839087caa360 100644
--- a/drivers/hwmon/sch56xx-common.c
+++ b/drivers/hwmon/sch56xx-common.c
@@ -67,6 +67,7 @@ MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default="
67struct sch56xx_watchdog_data { 67struct sch56xx_watchdog_data {
68 u16 addr; 68 u16 addr;
69 struct mutex *io_lock; 69 struct mutex *io_lock;
70 struct kref kref;
70 struct watchdog_info wdinfo; 71 struct watchdog_info wdinfo;
71 struct watchdog_device wddev; 72 struct watchdog_device wddev;
72 u8 watchdog_preset; 73 u8 watchdog_preset;
@@ -257,6 +258,15 @@ EXPORT_SYMBOL(sch56xx_read_virtual_reg12);
257 * Watchdog routines 258 * Watchdog routines
258 */ 259 */
259 260
261/* Release our data struct when we're unregistered *and*
262 all references to our watchdog device are released */
263static void watchdog_release_resources(struct kref *r)
264{
265 struct sch56xx_watchdog_data *data =
266 container_of(r, struct sch56xx_watchdog_data, kref);
267 kfree(data);
268}
269
260static int watchdog_set_timeout(struct watchdog_device *wddev, 270static int watchdog_set_timeout(struct watchdog_device *wddev,
261 unsigned int timeout) 271 unsigned int timeout)
262{ 272{
@@ -385,12 +395,28 @@ static int watchdog_stop(struct watchdog_device *wddev)
385 return 0; 395 return 0;
386} 396}
387 397
398static void watchdog_ref(struct watchdog_device *wddev)
399{
400 struct sch56xx_watchdog_data *data = watchdog_get_drvdata(wddev);
401
402 kref_get(&data->kref);
403}
404
405static void watchdog_unref(struct watchdog_device *wddev)
406{
407 struct sch56xx_watchdog_data *data = watchdog_get_drvdata(wddev);
408
409 kref_put(&data->kref, watchdog_release_resources);
410}
411
388static const struct watchdog_ops watchdog_ops = { 412static const struct watchdog_ops watchdog_ops = {
389 .owner = THIS_MODULE, 413 .owner = THIS_MODULE,
390 .start = watchdog_start, 414 .start = watchdog_start,
391 .stop = watchdog_stop, 415 .stop = watchdog_stop,
392 .ping = watchdog_trigger, 416 .ping = watchdog_trigger,
393 .set_timeout = watchdog_set_timeout, 417 .set_timeout = watchdog_set_timeout,
418 .ref = watchdog_ref,
419 .unref = watchdog_unref,
394}; 420};
395 421
396struct sch56xx_watchdog_data *sch56xx_watchdog_register(struct device *parent, 422struct sch56xx_watchdog_data *sch56xx_watchdog_register(struct device *parent,
@@ -422,6 +448,7 @@ struct sch56xx_watchdog_data *sch56xx_watchdog_register(struct device *parent,
422 448
423 data->addr = addr; 449 data->addr = addr;
424 data->io_lock = io_lock; 450 data->io_lock = io_lock;
451 kref_init(&data->kref);
425 452
426 strlcpy(data->wdinfo.identity, "sch56xx watchdog", 453 strlcpy(data->wdinfo.identity, "sch56xx watchdog",
427 sizeof(data->wdinfo.identity)); 454 sizeof(data->wdinfo.identity));
@@ -467,7 +494,8 @@ EXPORT_SYMBOL(sch56xx_watchdog_register);
467void sch56xx_watchdog_unregister(struct sch56xx_watchdog_data *data) 494void sch56xx_watchdog_unregister(struct sch56xx_watchdog_data *data)
468{ 495{
469 watchdog_unregister_device(&data->wddev); 496 watchdog_unregister_device(&data->wddev);
470 kfree(data); 497 kref_put(&data->kref, watchdog_release_resources);
498 /* Don't touch data after this it may have been free-ed! */
471} 499}
472EXPORT_SYMBOL(sch56xx_watchdog_unregister); 500EXPORT_SYMBOL(sch56xx_watchdog_unregister);
473 501