Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit b02f8bed authored by NeilBrown's avatar NeilBrown Committed by Greg Kroah-Hartman
Browse files

W1: split master mutex to avoid deadlocks.



The 'mutex' in struct w1_master is use for two very different
purposes.

Firstly it protects various data structures such as the list of all
slaves.

Secondly it protects the w1 buss against concurrent accesses.

This can lead to deadlocks when the ->probe code called while adding a
slave needs to talk on the bus, as is the case for power_supply
devices.
ds2780 and ds2781 drivers contain a work around to track which
process hold the lock simply to avoid this deadlock.  bq27000 doesn't
have that work around and so deadlocks.

There are other possible deadlocks involving sysfs.
When removing a device the sysfs s_active lock is held, so the lock
that protects the slave list must take precedence over s_active.
However when access power_supply attributes via sysfs, the s_active
lock must take precedence over the lock that protects accesses to
the bus.

So to avoid deadlocks between w1 slaves and sysfs, these must be
two separate locks.  Making them separate means that the work around
in ds2780 and ds2781 can be removed.

So this patch:
 - adds a new mutex: "bus_mutex" which serialises access to the bus.
 - takes in mutex in w1_search and ds1wm_search while they access
   the bus for searching.  The mutex is dropped before calling the
   callback which adds the slave.
 - changes all slaves to use bus_mutex instead of mutex to
   protect access to the bus
 - removes w1_ds2790_io_nolock and w1_ds2781_io_nolock, and the
   related code from drivers/power/ds278[01]_battery.c which
   calls them.

Signed-off-by: default avatarNeilBrown <neilb@suse.de>
Acked-by: default avatarEvgeniy Polyakov <zbr@ioremap.net>
Signed-off-by: default avatarGreg Kroah-Hartman <gregkh@linuxfoundation.org>
parent b7e938d0
Loading
Loading
Loading
Loading
+1 −10
Original line number Diff line number Diff line
@@ -39,7 +39,6 @@ struct ds2780_device_info {
	struct device *dev;
	struct power_supply bat;
	struct device *w1_dev;
	struct task_struct *mutex_holder;
};

enum current_types {
@@ -64,9 +63,6 @@ static inline struct power_supply *to_power_supply(struct device *dev)
static inline int ds2780_battery_io(struct ds2780_device_info *dev_info,
	char *buf, int addr, size_t count, int io)
{
	if (dev_info->mutex_holder == current)
		return w1_ds2780_io_nolock(dev_info->w1_dev, buf, addr, count, io);
	else
	return w1_ds2780_io(dev_info->w1_dev, buf, addr, count, io);
}

@@ -779,7 +775,6 @@ static int __devinit ds2780_battery_probe(struct platform_device *pdev)
	dev_info->bat.properties	= ds2780_battery_props;
	dev_info->bat.num_properties	= ARRAY_SIZE(ds2780_battery_props);
	dev_info->bat.get_property	= ds2780_battery_get_property;
	dev_info->mutex_holder		= current;

	ret = power_supply_register(&pdev->dev, &dev_info->bat);
	if (ret) {
@@ -809,8 +804,6 @@ static int __devinit ds2780_battery_probe(struct platform_device *pdev)
		goto fail_remove_bin_file;
	}

	dev_info->mutex_holder = NULL;

	return 0;

fail_remove_bin_file:
@@ -830,8 +823,6 @@ static int __devexit ds2780_battery_remove(struct platform_device *pdev)
{
	struct ds2780_device_info *dev_info = platform_get_drvdata(pdev);

	dev_info->mutex_holder = current;

	/* remove attributes */
	sysfs_remove_group(&dev_info->bat.dev->kobj, &ds2780_attr_group);

+1 −11
Original line number Diff line number Diff line
@@ -37,7 +37,6 @@ struct ds2781_device_info {
	struct device *dev;
	struct power_supply bat;
	struct device *w1_dev;
	struct task_struct *mutex_holder;
};

enum current_types {
@@ -62,10 +61,6 @@ static inline struct power_supply *to_power_supply(struct device *dev)
static inline int ds2781_battery_io(struct ds2781_device_info *dev_info,
	char *buf, int addr, size_t count, int io)
{
	if (dev_info->mutex_holder == current)
		return w1_ds2781_io_nolock(dev_info->w1_dev, buf, addr,
				count, io);
	else
	return w1_ds2781_io(dev_info->w1_dev, buf, addr, count, io);
}

@@ -775,7 +770,6 @@ static int __devinit ds2781_battery_probe(struct platform_device *pdev)
	dev_info->bat.properties	= ds2781_battery_props;
	dev_info->bat.num_properties	= ARRAY_SIZE(ds2781_battery_props);
	dev_info->bat.get_property	= ds2781_battery_get_property;
	dev_info->mutex_holder		= current;

	ret = power_supply_register(&pdev->dev, &dev_info->bat);
	if (ret) {
@@ -805,8 +799,6 @@ static int __devinit ds2781_battery_probe(struct platform_device *pdev)
		goto fail_remove_bin_file;
	}

	dev_info->mutex_holder = NULL;

	return 0;

fail_remove_bin_file:
@@ -826,8 +818,6 @@ static int __devexit ds2781_battery_remove(struct platform_device *pdev)
{
	struct ds2781_device_info *dev_info = platform_get_drvdata(pdev);

	dev_info->mutex_holder = current;

	/* remove attributes */
	sysfs_remove_group(&dev_info->bat.dev->kobj, &ds2781_attr_group);

+4 −0
Original line number Diff line number Diff line
@@ -334,7 +334,9 @@ static void ds1wm_search(void *data, struct w1_master *master_dev,
			return;
		}

		mutex_lock(&master_dev->bus_mutex);
		if (ds1wm_reset(ds1wm_data)) {
			mutex_unlock(&master_dev->bus_mutex);
			dev_dbg(&ds1wm_data->pdev->dev,
				"pass: %d reset error (or no slaves)\n", pass);
			break;
@@ -387,6 +389,7 @@ static void ds1wm_search(void *data, struct w1_master *master_dev,

		}
		if (ds1wm_data->read_error) {
			mutex_unlock(&master_dev->bus_mutex);
			dev_err(&ds1wm_data->pdev->dev,
				"pass: %d read error, retrying\n", pass);
			break;
@@ -400,6 +403,7 @@ static void ds1wm_search(void *data, struct w1_master *master_dev,
		dev_dbg(&ds1wm_data->pdev->dev,
			"pass: %d resetting bus\n", pass);
		ds1wm_reset(ds1wm_data);
		mutex_unlock(&master_dev->bus_mutex);
		if ((r_prime & ((u64)1 << 63)) && (d & ((u64)1 << 63))) {
			dev_err(&ds1wm_data->pdev->dev,
				"pass: %d bus error, retrying\n", pass);
+2 −2
Original line number Diff line number Diff line
@@ -31,10 +31,10 @@ static int w1_bq27000_read(struct device *dev, unsigned int reg)
	u8 val;
	struct w1_slave *sl = container_of(dev->parent, struct w1_slave, dev);

	mutex_lock(&sl->master->mutex);
	mutex_lock(&sl->master->bus_mutex);
	w1_write_8(sl->master, HDQ_CMD_READ | reg);
	val = w1_read_8(sl->master);
	mutex_unlock(&sl->master->mutex);
	mutex_unlock(&sl->master->bus_mutex);

	return val;
}
+12 −12
Original line number Diff line number Diff line
@@ -52,11 +52,11 @@ static int _read_reg(struct w1_slave *sl, u8 address, unsigned char* buf)
	if (!buf)
		return -EINVAL;

	mutex_lock(&sl->master->mutex);
	mutex_lock(&sl->master->bus_mutex);
	dev_dbg(&sl->dev, "mutex locked");

	if (w1_reset_select_slave(sl)) {
		mutex_unlock(&sl->master->mutex);
		mutex_unlock(&sl->master->bus_mutex);
		return -EIO;
	}

@@ -66,7 +66,7 @@ static int _read_reg(struct w1_slave *sl, u8 address, unsigned char* buf)
	w1_write_block(sl->master, wrbuf, 3);
	*buf = w1_read_8(sl->master);

	mutex_unlock(&sl->master->mutex);
	mutex_unlock(&sl->master->bus_mutex);
	dev_dbg(&sl->dev, "mutex unlocked");
	return 1;
}
@@ -165,7 +165,7 @@ static ssize_t w1_f29_write_output(
		return -EFAULT;

	dev_dbg(&sl->dev, "locking mutex for write_output");
	mutex_lock(&sl->master->mutex);
	mutex_lock(&sl->master->bus_mutex);
	dev_dbg(&sl->dev, "mutex locked");

	if (w1_reset_select_slave(sl))
@@ -200,14 +200,14 @@ static ssize_t w1_f29_write_output(
		/* read the result of the READ_PIO_REGS command */
		if (w1_read_8(sl->master) == *buf) {
			/* success! */
			mutex_unlock(&sl->master->mutex);
			mutex_unlock(&sl->master->bus_mutex);
			dev_dbg(&sl->dev,
				"mutex unlocked, retries:%d", retries);
			return 1;
		}
	}
error:
	mutex_unlock(&sl->master->mutex);
	mutex_unlock(&sl->master->bus_mutex);
	dev_dbg(&sl->dev, "mutex unlocked in error, retries:%d", retries);

	return -EIO;
@@ -228,7 +228,7 @@ static ssize_t w1_f29_write_activity(
	if (count != 1 || off != 0)
		return -EFAULT;

	mutex_lock(&sl->master->mutex);
	mutex_lock(&sl->master->bus_mutex);

	if (w1_reset_select_slave(sl))
		goto error;
@@ -236,7 +236,7 @@ static ssize_t w1_f29_write_activity(
	while (retries--) {
		w1_write_8(sl->master, W1_F29_FUNC_RESET_ACTIVITY_LATCHES);
		if (w1_read_8(sl->master) == W1_F29_SUCCESS_CONFIRM_BYTE) {
			mutex_unlock(&sl->master->mutex);
			mutex_unlock(&sl->master->bus_mutex);
			return 1;
		}
		if (w1_reset_resume_command(sl->master))
@@ -244,7 +244,7 @@ static ssize_t w1_f29_write_activity(
	}

error:
	mutex_unlock(&sl->master->mutex);
	mutex_unlock(&sl->master->bus_mutex);
	return -EIO;
}

@@ -263,7 +263,7 @@ static ssize_t w1_f29_write_status_control(
	if (count != 1 || off != 0)
		return -EFAULT;

	mutex_lock(&sl->master->mutex);
	mutex_lock(&sl->master->bus_mutex);

	if (w1_reset_select_slave(sl))
		goto error;
@@ -285,12 +285,12 @@ static ssize_t w1_f29_write_status_control(
		w1_write_block(sl->master, w1_buf, 3);
		if (w1_read_8(sl->master) == *buf) {
			/* success! */
			mutex_unlock(&sl->master->mutex);
			mutex_unlock(&sl->master->bus_mutex);
			return 1;
		}
	}
error:
	mutex_unlock(&sl->master->mutex);
	mutex_unlock(&sl->master->bus_mutex);

	return -EIO;
}
Loading