[RFC PATCH] bq27000 battery mutex protection

Lars-Peter Clausen lars at metafoo.de
Mon Oct 12 20:25:20 CEST 2009


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Michael Trimarchi wrote:

>Protect the data using a mutex. Fix a race that can happen when
>the user read from the sysfs and the worker execute in the middle.
>
>Signed-off-by: Michael Trimarchi <michael at panicking.kicks-ass.org>
>
>diff --git a/drivers/power/bq27000_battery.c
b/drivers/power/bq27000_battery.c
>index 01168ea..ddee537 100644
>--- a/drivers/power/bq27000_battery.c
>+++ b/drivers/power/bq27000_battery.c
>@@ -134,6 +134,7 @@ struct bq27000_device_info {
>     struct bq27000_bat_regs regs;
> };
>
>+static DEFINE_MUTEX(battery_mutex);
> static unsigned int cache_time = 5000;
> module_param(cache_time, uint, 0644);
> MODULE_PARM_DESC(cache_time, "cache time in milliseconds");
>
> ...
>
> static void bq27000_battery_work(struct work_struct *work)
> @@ -353,6 +383,8 @@ static void bq27000_battery_work(struct work_struct
*work)
>     struct bq27000_device_info *di =
>         container_of(work, struct bq27000_device_info, work.work);
>
>+    mutex_lock(&battery_mutex);
>+
>     if ((di->pdata->hdq_initialized)()) {
>         struct bq27000_bat_regs regs;
>
>@@ -375,6 +407,8 @@ static void bq27000_battery_work(struct work_struct
*work)
>
>     if (!schedule_delayed_work(&di->work, cache_time))
>         dev_err(di->dev, "battery service reschedule failed\n");
>+
>+    mutex_unlock(&battery_mutex);
> }
 
You only need to protect the assignment to di->regs. There is no need
to hold the look for the whole function call.

- -Lars
 static enum power_supply_property bq27000_battery_props[] = {
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iEYEARECAAYFAkrTdJAACgkQBX4mSR26RiPqVgCfVH8tV1HDb04wdj+ufJFuk+1c
7HAAnil8I/P+K+3pmh7gJfmeZwqw5AIe
=qCTo
-----END PGP SIGNATURE-----




More information about the openmoko-kernel mailing list