Skip to contentSkip to conversation

iio:dac:ad5064 mlock cleanup - move to a local lock.

PATCH In review [email protected] 2 participants 5 messages Feb 20, 2019 – Mar 16, 2019

Subsystems (2)
lore
Share & tools
lore

[email protected]

author maintainer
#0 lore raw

PATCH 1/1 iio:dac:ad5064 mlock cleanup - move to a local lock.

From: Jonathan Cameron <[email protected]>

indio_dev->mlock is intended to protect state transitions in the core. It's scope is tightly defined. For device specific uses such as those made here, we should define a local lock allowing the scope of the lock to be defined near to what it is protecting.

These mlock changes can be non obvious, but given we don't do anything other than direct for DACs, these ones are easy to do.

If anyone wants to help with this particular effort it would be most welcome!

---
 drivers/iio/dac/ad5064.c | 15 +++++++++------
 1 file changed, 9 insertions(+), 6 deletions(-)
drivers/iio/dac/ad5064.c +9 -6
index 2f98cb2a3b96..6c3ba143839b 100644
@@ -112,6 +112,8 @@ struct ad5064_state {
 	bool				use_internal_vref;
 
 	ad5064_write_func		write;
+	/* Lock used to maintain consistency between cached and dev state */
+	struct mutex lock;
 
 	/*
 	 * DMA (thus cache coherency maintenance) requires the
@@ -248,11 +250,11 @@ static int ad5064_set_powerdown_mode(struct iio_dev *indio_dev,
 	struct ad5064_state *st = iio_priv(indio_dev);
 	int ret;
 
-	mutex_lock(&indio_dev->mlock);
+	mutex_lock(&st->lock);
 	st->pwr_down_mode[chan->channel] = mode + 1;
 
 	ret = ad5064_sync_powerdown_mode(st, chan);
-	mutex_unlock(&indio_dev->mlock);
+	mutex_unlock(&st->lock);
 
 	return ret;
 }
@@ -291,11 +293,11 @@ static ssize_t ad5064_write_dac_powerdown(struct iio_dev *indio_dev,
 	if (ret)
 		return ret;
 
-	mutex_lock(&indio_dev->mlock);
+	mutex_lock(&st->lock);
 	st->pwr_down[chan->channel] = pwr_down;
 
 	ret = ad5064_sync_powerdown_mode(st, chan);
-	mutex_unlock(&indio_dev->mlock);
+	mutex_unlock(&st->lock);
 	return ret ? ret : len;
 }
 
@@ -349,12 +351,12 @@ static int ad5064_write_raw(struct iio_dev *indio_dev,
 		if (val >= (1 << chan->scan_type.realbits) || val < 0)
 			return -EINVAL;
 
-		mutex_lock(&indio_dev->mlock);
+		mutex_lock(&st->lock);
 		ret = ad5064_write(st, AD5064_CMD_WRITE_INPUT_N_UPDATE_N,
 				chan->address, val, chan->scan_type.shift);
 		if (ret == 0)
 			st->dac_cache[chan->channel] = val;
-		mutex_unlock(&indio_dev->mlock);
+		mutex_unlock(&st->lock);
 		break;
 	default:
 		ret = -EINVAL;
@@ -856,6 +858,7 @@ static int ad5064_probe(struct device *dev, enum ad5064_type type,
 		return  -ENOMEM;
 
 	st = iio_priv(indio_dev);
+	mutex_init(&st->lock);
 	dev_set_drvdata(dev, indio_dev);
 
 	st->chip_info = &ad5064_chip_info_tbl[type];
Signature · 1 line
2.20.1
#1 lore raw
Quoted 1 line · On Wed, Feb 20, 2019 at 8:24 PM <[email protected]> wrote:

Hey,

One comment inline.

Quoted 76 lines

From: Jonathan Cameron <[email protected]>

indio_dev->mlock is intended to protect state transitions in the core. It's scope is tightly defined. For device specific uses such as those made here, we should define a local lock allowing the scope of the lock to be defined near to what it is protecting.

These mlock changes can be non obvious, but given we don't do anything other than direct for DACs, these ones are easy to do.

If anyone wants to help with this particular effort it would be most welcome!

---
 drivers/iio/dac/ad5064.c | 15 +++++++++------
 1 file changed, 9 insertions(+), 6 deletions(-)
drivers/iio/dac/ad5064.c +8 -6
index 2f98cb2a3b96..6c3ba143839b 100644
@@ -112,6 +112,8 @@ struct ad5064_state {
        bool                            use_internal_vref;
 
        ad5064_write_func               write;
+       /* Lock used to maintain consistency between cached and dev state */
+       struct mutex lock;
 
        /*
         * DMA (thus cache coherency maintenance) requires the
@@ -248,11 +250,11 @@ static int ad5064_set_powerdown_mode(struct iio_dev *indio_dev,
        struct ad5064_state *st = iio_priv(indio_dev);
        int ret;
 
-       mutex_lock(&indio_dev->mlock);
+       mutex_lock(&st->lock);
        st->pwr_down_mode[chan->channel] = mode + 1;
 
        ret = ad5064_sync_powerdown_mode(st, chan);
-       mutex_unlock(&indio_dev->mlock);
+       mutex_unlock(&st->lock);
 
        return ret;
 }
@@ -291,11 +293,11 @@ static ssize_t ad5064_write_dac_powerdown(struct iio_dev *indio_dev,
        if (ret)
                return ret;
 
-       mutex_lock(&indio_dev->mlock);
+       mutex_lock(&st->lock);
        st->pwr_down[chan->channel] = pwr_down;
 
        ret = ad5064_sync_powerdown_mode(st, chan);
-       mutex_unlock(&indio_dev->mlock);
+       mutex_unlock(&st->lock);
        return ret ? ret : len;
 }
 
@@ -349,12 +351,12 @@ static int ad5064_write_raw(struct iio_dev *indio_dev,
                if (val >= (1 << chan->scan_type.realbits) || val < 0)
                        return -EINVAL;
 
-               mutex_lock(&indio_dev->mlock);
+               mutex_lock(&st->lock);
                ret = ad5064_write(st, AD5064_CMD_WRITE_INPUT_N_UPDATE_N,
                                chan->address, val, chan->scan_type.shift);
                if (ret == 0)
                        st->dac_cache[chan->channel] = val;
-               mutex_unlock(&indio_dev->mlock);
+               mutex_unlock(&st->lock);
                break;
        default:
                ret = -EINVAL;

drivers/iio/dac/ad5064.c static int ad5064_probe(struct device *dev, enum ad5064_type type, · line 861

Quoted 4 lines
drivers/iio/dac/ad5064.c +1 -0
@@ -856,6 +858,7 @@ static int ad5064_probe(struct device *dev, enum ad5064_type type,
                return  -ENOMEM;
 
        st = iio_priv(indio_dev);
+       mutex_init(&st->lock);

An equivalent mutex_destroy() call would be a good idea in ad5064_remove()

Quoted 6 lines
drivers/iio/dac/ad5064.c +0 -1
        dev_set_drvdata(dev, indio_dev);
 
        st->chip_info = &ad5064_chip_info_tbl[type];
--

2.20.1

author maintainer
#2 lore raw
Quoted 85 lines · On Fri, 22 Feb 2019 13:58:13 +0200 Alexandru Ardelean <[email protected]>…
Quoted 1 line · On Wed, Feb 20, 2019 at 8:24 PM <[email protected]> wrote:

Hey,

One comment inline.

Quoted 78 lines

From: Jonathan Cameron <[email protected]>

indio_dev->mlock is intended to protect state transitions in the core. It's scope is tightly defined. For device specific uses such as those made here, we should define a local lock allowing the scope of the lock to be defined near to what it is protecting.

These mlock changes can be non obvious, but given we don't do anything other than direct for DACs, these ones are easy to do.

If anyone wants to help with this particular effort it would be most welcome!

---
 drivers/iio/dac/ad5064.c | 15 +++++++++------
 1 file changed, 9 insertions(+), 6 deletions(-)
drivers/iio/dac/ad5064.c +8 -6
index 2f98cb2a3b96..6c3ba143839b 100644
@@ -112,6 +112,8 @@ struct ad5064_state {
        bool                            use_internal_vref;
 
        ad5064_write_func               write;
+       /* Lock used to maintain consistency between cached and dev state */
+       struct mutex lock;
 
        /*
         * DMA (thus cache coherency maintenance) requires the
@@ -248,11 +250,11 @@ static int ad5064_set_powerdown_mode(struct iio_dev *indio_dev,
        struct ad5064_state *st = iio_priv(indio_dev);
        int ret;
 
-       mutex_lock(&indio_dev->mlock);
+       mutex_lock(&st->lock);
        st->pwr_down_mode[chan->channel] = mode + 1;
 
        ret = ad5064_sync_powerdown_mode(st, chan);
-       mutex_unlock(&indio_dev->mlock);
+       mutex_unlock(&st->lock);
 
        return ret;
 }
@@ -291,11 +293,11 @@ static ssize_t ad5064_write_dac_powerdown(struct iio_dev *indio_dev,
        if (ret)
                return ret;
 
-       mutex_lock(&indio_dev->mlock);
+       mutex_lock(&st->lock);
        st->pwr_down[chan->channel] = pwr_down;
 
        ret = ad5064_sync_powerdown_mode(st, chan);
-       mutex_unlock(&indio_dev->mlock);
+       mutex_unlock(&st->lock);
        return ret ? ret : len;
 }
 
@@ -349,12 +351,12 @@ static int ad5064_write_raw(struct iio_dev *indio_dev,
                if (val >= (1 << chan->scan_type.realbits) || val < 0)
                        return -EINVAL;
 
-               mutex_lock(&indio_dev->mlock);
+               mutex_lock(&st->lock);
                ret = ad5064_write(st, AD5064_CMD_WRITE_INPUT_N_UPDATE_N,
                                chan->address, val, chan->scan_type.shift);
                if (ret == 0)
                        st->dac_cache[chan->channel] = val;
-               mutex_unlock(&indio_dev->mlock);
+               mutex_unlock(&st->lock);
                break;
        default:
                ret = -EINVAL;
@@ -856,6 +858,7 @@ static int ad5064_probe(struct device *dev, enum ad5064_type type,
                return  -ENOMEM;
 
Quoted 5 lines

drivers/iio/dac/ad5064.c static int ad5064_probe(struct device *dev, enum ad5064_type type, · line 861

Quoted 2 lines
drivers/iio/dac/ad5064.c +1 -0
        st = iio_priv(indio_dev);
+       mutex_init(&st->lock);  

An equivalent mutex_destroy() call would be a good idea in ad5064_remove()

Ah, this one..

mutex_destroy is only useful in finding use after free if mutex debugging is turned on. In my view it is of dubious benefit if it's getting called only in remove, on the basis we should be pretty sure the driver won't take the lock if we hit that path.

The downside is that it complicates the remove path because there is no devm version of mutex_init to automate the cleanup.

I'll look at whether it makes sense here (i.e. if there is too much cost to adding it).

Quoted 6 lines
Quoted 6 lines
drivers/iio/dac/ad5064.c +0 -1
        dev_set_drvdata(dev, indio_dev);
 
        st->chip_info = &ad5064_chip_info_tbl[type];
--

2.20.1

#3 lore raw
Quoted 101 lines · Jonathan Cameron
Quoted 85 lines · On Fri, 22 Feb 2019 13:58:13 +0200 Alexandru Ardelean <[email protected]>…
Quoted 1 line · On Wed, Feb 20, 2019 at 8:24 PM <[email protected]> wrote:

Hey,

One comment inline.

Quoted 78 lines

From: Jonathan Cameron <[email protected]>

indio_dev->mlock is intended to protect state transitions in the core. It's scope is tightly defined. For device specific uses such as those made here, we should define a local lock allowing the scope of the lock to be defined near to what it is protecting.

These mlock changes can be non obvious, but given we don't do anything other than direct for DACs, these ones are easy to do.

If anyone wants to help with this particular effort it would be most welcome!

---
 drivers/iio/dac/ad5064.c | 15 +++++++++------
 1 file changed, 9 insertions(+), 6 deletions(-)
drivers/iio/dac/ad5064.c +8 -6
index 2f98cb2a3b96..6c3ba143839b 100644
@@ -112,6 +112,8 @@ struct ad5064_state {
        bool                            use_internal_vref;
 
        ad5064_write_func               write;
+       /* Lock used to maintain consistency between cached and dev state */
+       struct mutex lock;
 
        /*
         * DMA (thus cache coherency maintenance) requires the
@@ -248,11 +250,11 @@ static int ad5064_set_powerdown_mode(struct iio_dev *indio_dev,
        struct ad5064_state *st = iio_priv(indio_dev);
        int ret;
 
-       mutex_lock(&indio_dev->mlock);
+       mutex_lock(&st->lock);
        st->pwr_down_mode[chan->channel] = mode + 1;
 
        ret = ad5064_sync_powerdown_mode(st, chan);
-       mutex_unlock(&indio_dev->mlock);
+       mutex_unlock(&st->lock);
 
        return ret;
 }
@@ -291,11 +293,11 @@ static ssize_t ad5064_write_dac_powerdown(struct iio_dev *indio_dev,
        if (ret)
                return ret;
 
-       mutex_lock(&indio_dev->mlock);
+       mutex_lock(&st->lock);
        st->pwr_down[chan->channel] = pwr_down;
 
        ret = ad5064_sync_powerdown_mode(st, chan);
-       mutex_unlock(&indio_dev->mlock);
+       mutex_unlock(&st->lock);
        return ret ? ret : len;
 }
 
@@ -349,12 +351,12 @@ static int ad5064_write_raw(struct iio_dev *indio_dev,
                if (val >= (1 << chan->scan_type.realbits) || val < 0)
                        return -EINVAL;
 
-               mutex_lock(&indio_dev->mlock);
+               mutex_lock(&st->lock);
                ret = ad5064_write(st, AD5064_CMD_WRITE_INPUT_N_UPDATE_N,
                                chan->address, val, chan->scan_type.shift);
                if (ret == 0)
                        st->dac_cache[chan->channel] = val;
-               mutex_unlock(&indio_dev->mlock);
+               mutex_unlock(&st->lock);
                break;
        default:
                ret = -EINVAL;
@@ -856,6 +858,7 @@ static int ad5064_probe(struct device *dev, enum ad5064_type type,
                return  -ENOMEM;
 
Quoted 5 lines

drivers/iio/dac/ad5064.c static int ad5064_probe(struct device *dev, enum ad5064_type type, · line 861

Quoted 2 lines
drivers/iio/dac/ad5064.c +1 -0
        st = iio_priv(indio_dev);
+       mutex_init(&st->lock);

An equivalent mutex_destroy() call would be a good idea in ad5064_remove()

Ah, this one..

mutex_destroy is only useful in finding use after free if mutex debugging is turned on. In my view it is of dubious benefit if it's getting called only in remove, on the basis we should be pretty sure the driver won't take the lock if we hit that path.

Quoted 5 lines

The downside is that it complicates the remove path because there is no devm version of mutex_init to automate the cleanup.

I'll look at whether it makes sense here (i.e. if there is too much cost to adding it).

Hmm, ok.
From my side it's fine whether to leave it or add it.

Quoted 8 lines
Quoted 7 lines
Quoted 7 lines
drivers/iio/dac/ad5064.c +0 -1
        dev_set_drvdata(dev, indio_dev);
 
        st->chip_info = &ad5064_chip_info_tbl[type];
--

2.20.1

author maintainer
#4 lore raw
Quoted 102 lines · On Sun, 3 Mar 2019 16:26:45 +0200 Alexandru Ardelean <[email protected]>…
Quoted 101 lines · Jonathan Cameron
Quoted 85 lines · On Fri, 22 Feb 2019 13:58:13 +0200 Alexandru Ardelean <[email protected]>…
Quoted 1 line · On Wed, Feb 20, 2019 at 8:24 PM <[email protected]> wrote:

Hey,

One comment inline.

Quoted 78 lines

From: Jonathan Cameron <[email protected]>

indio_dev->mlock is intended to protect state transitions in the core. It's scope is tightly defined. For device specific uses such as those made here, we should define a local lock allowing the scope of the lock to be defined near to what it is protecting.

These mlock changes can be non obvious, but given we don't do anything other than direct for DACs, these ones are easy to do.

If anyone wants to help with this particular effort it would be most welcome!

---
 drivers/iio/dac/ad5064.c | 15 +++++++++------
 1 file changed, 9 insertions(+), 6 deletions(-)
drivers/iio/dac/ad5064.c +8 -6
index 2f98cb2a3b96..6c3ba143839b 100644
@@ -112,6 +112,8 @@ struct ad5064_state {
        bool                            use_internal_vref;
 
        ad5064_write_func               write;
+       /* Lock used to maintain consistency between cached and dev state */
+       struct mutex lock;
 
        /*
         * DMA (thus cache coherency maintenance) requires the
@@ -248,11 +250,11 @@ static int ad5064_set_powerdown_mode(struct iio_dev *indio_dev,
        struct ad5064_state *st = iio_priv(indio_dev);
        int ret;
 
-       mutex_lock(&indio_dev->mlock);
+       mutex_lock(&st->lock);
        st->pwr_down_mode[chan->channel] = mode + 1;
 
        ret = ad5064_sync_powerdown_mode(st, chan);
-       mutex_unlock(&indio_dev->mlock);
+       mutex_unlock(&st->lock);
 
        return ret;
 }
@@ -291,11 +293,11 @@ static ssize_t ad5064_write_dac_powerdown(struct iio_dev *indio_dev,
        if (ret)
                return ret;
 
-       mutex_lock(&indio_dev->mlock);
+       mutex_lock(&st->lock);
        st->pwr_down[chan->channel] = pwr_down;
 
        ret = ad5064_sync_powerdown_mode(st, chan);
-       mutex_unlock(&indio_dev->mlock);
+       mutex_unlock(&st->lock);
        return ret ? ret : len;
 }
 
@@ -349,12 +351,12 @@ static int ad5064_write_raw(struct iio_dev *indio_dev,
                if (val >= (1 << chan->scan_type.realbits) || val < 0)
                        return -EINVAL;
 
-               mutex_lock(&indio_dev->mlock);
+               mutex_lock(&st->lock);
                ret = ad5064_write(st, AD5064_CMD_WRITE_INPUT_N_UPDATE_N,
                                chan->address, val, chan->scan_type.shift);
                if (ret == 0)
                        st->dac_cache[chan->channel] = val;
-               mutex_unlock(&indio_dev->mlock);
+               mutex_unlock(&st->lock);
                break;
        default:
                ret = -EINVAL;
@@ -856,6 +858,7 @@ static int ad5064_probe(struct device *dev, enum ad5064_type type,
                return  -ENOMEM;
 
Quoted 5 lines

drivers/iio/dac/ad5064.c static int ad5064_probe(struct device *dev, enum ad5064_type type, · line 861

Quoted 2 lines
drivers/iio/dac/ad5064.c +1 -0
        st = iio_priv(indio_dev);
+       mutex_init(&st->lock);  

An equivalent mutex_destroy() call would be a good idea in ad5064_remove()

Ah, this one..

mutex_destroy is only useful in finding use after free if mutex debugging is turned on. In my view it is of dubious benefit if it's getting called only in remove, on the basis we should be pretty sure the driver won't take the lock if we hit that path.

Quoted 8 lines
Quoted 5 lines

The downside is that it complicates the remove path because there is no devm version of mutex_init to automate the cleanup.

I'll look at whether it makes sense here (i.e. if there is too much cost to adding it).

Hmm, ok.
From my side it's fine whether to leave it or add it.

Applied before I forget about it.

Thanks,

Jonathan

Quoted 9 lines
Quoted 9 lines
Quoted 8 lines
Quoted 8 lines
drivers/iio/dac/ad5064.c +0 -1
        dev_set_drvdata(dev, indio_dev);
 
        st->chip_info = &ad5064_chip_info_tbl[type];
--

2.20.1