iio: health: max30100: remove mlock usage
PATCH 1/1 iio: health: max30100: remove mlock usage
Use local lock instead of indio_dev's mlock.
The mlock was being used to protect local driver state thus using the local lock is a better option here.
--- drivers/iio/health/max30100.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/iio/health/max30100.c +2 -2
@@ -388,7 +388,7 @@ static int max30100_read_raw(struct iio_dev *indio_dev,* Temperature reading can only be acquired while engine * is running */ -mutex_lock (&indio_dev->mlock); +mutex_lock (&data->lock);if (!iio_buffer_enabled (indio_dev)) ret = -EAGAIN ;
@@ -399,7 +399,7 @@ static int max30100_read_raw(struct iio_dev *indio_dev, } -mutex_unlock (&indio_dev->mlock); +mutex_unlock (&data->lock);break ;case IIO_CHAN_INFO_SCALE : *val =1 ;/* 0.0625 */
Signature · 1 line
2.23.0.385.gbc12974a89
Jonathan Cameron
maintainerQuoted 5 lines · On Tue, 10 Mar 2020 00:01:28 +0530 Rohit Sarkar <[email protected]>…
Use local lock instead of indio_dev's mlock.
The mlock was being used to protect local driver state thus using the local lock is a better option here.Signed-off-by Rohit Sarkar <[email protected]>
Matt. Definitely need your input on this.
Quoted 9 lines
--- drivers/iio/health/max30100.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)drivers/iio/health/max30100.c +0
-0
drivers/iio/health/max30100.c static int max30100_read_raw(struct iio_dev *indio_dev, · line 391
Quoted 5 lines
drivers/iio/health/max30100.c +1
-1@@ -388,7 +388,7 @@ static int max30100_read_raw(struct iio_dev *indio_dev,* Temperature reading can only be acquired while engine * is running */ -mutex_lock (&indio_dev->mlock); +mutex_lock (&data->lock);
Hmm.. It's another complex one. What is actually being protected here is the buffer state, but not to take it exclusively like claim_direct does.
Here we need the inverse, we want to ensure we are 'not' in the direct mode because this hardware requires the buffer to be running to read the temperature.
That is the sort of interface that is going to get userspace very confused.
Matt, normally what I'd suggest here is that the temperature read should:
1) Claim direct mode, if it fails then do the dance you have here (with more comments to explain why you are taking an internal lock)
2) Start up capture as if we were in buffered mode
3) Grab that temp
4) stop capture to return to non buffered mode.
5) Release direct mode.
I guess we decided it wasn't worth the hassle.
So Rohit. This one probably needs a comment rather than any change. We 'could' add a 'hold_buffered_mode' function that takes the mlock, verifies we are in buffered mode and continues to hold the lock until the 'release_buffered_mode'. However, I'm not sure any other drivers do this particular dance, so clear commenting in the driver
might be enough. Should we ever change how mlock is used in the
core, we'd have to fix this driver up as well.
Hmm. This is really hammering home that perhaps all the remaining mlock cases are 'hard'.
Thanks,
Jonathan
Quoted 12 lines
drivers/iio/health/max30100.c +1
-1if (!iio_buffer_enabled (indio_dev)) ret = -EAGAIN ;@@ -399,7 +399,7 @@ static int max30100_read_raw(struct iio_dev *indio_dev, } -mutex_unlock (&indio_dev->mlock); +mutex_unlock (&data->lock);break ;case IIO_CHAN_INFO_SCALE : *val =1 ;/* 0.0625 */
Quoted 15 lines · Jonathan Cameron
Quoted 5 lines · On Tue, 10 Mar 2020 00:01:28 +0530 Rohit Sarkar <[email protected]>…
Use local lock instead of indio_dev's mlock.
The mlock was being used to protect local driver state thus using the local lock is a better option here.Signed-off-by Rohit Sarkar <[email protected]>Matt. Definitely need your input on this.
Quoted 4 lines
--- drivers/iio/health/max30100.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
Quoted 13 lines
drivers/iio/health/max30100.c static int max30100_read_raw(struct iio_dev *indio_dev, · line 391
Quoted 10 lines
drivers/iio/health/max30100.c +1
-1@@ -388,7 +388,7 @@ static int max30100_read_raw(struct iio_dev *indio_dev,* Temperature reading can only be acquired while engine * is running */ -mutex_lock (&indio_dev->mlock); +mutex_lock (&data->lock);Hmm.. It's another complex one. What is actually being protected here is the buffer state, but not to take it exclusively like
claim_directdoes.
So just to check if I understand correctly, let's say we did not use any lock in this case. If an execution thread reached the iio_buffer_enabled() check and found the buffer to be enabled, but the buffer got disabled simultaneously, the temperature readings that we get will be corrupted. Does this make sense?
Quoted 6 lines
Here we need the inverse, we want to ensure we are 'not' in the direct mode because this hardware requires the buffer to be running to read the temperature.
That is the sort of interface that is going to get userspace very confused.
Agreed
Quoted 9 lines
Matt, normally what I'd suggest here is that the temperature read should:
1) Claim direct mode, if it fails then do the dance you have here (with more comments to explain why you are taking an internal lock)
2) Start up capture as if we were in buffered mode
3) Grab that temp
4) stop capture to return to non buffered mode.
5) Release direct mode.
Quoted 3 lines
I guess we decided it wasn't worth the hassle.
So Rohit. This one probably needs a comment rather than any change.
The code already mentions that the "Temperature can only be acquired while engine is running.", should I add something like "mlock is acquired to protect the buffer state..." to the same comment.
Quoted 6 lines
We 'could' add a '
hold_buffered_mode' function that takes the mlock, verifies we are in buffered mode and continues to hold the lock until the 'release_buffered_mode'. However, I'm not sure any other drivers do this particular dance, so clear commenting in the driver
might be enough. Should we ever change how mlock is used in the
core, we'd have to fix this driver up as well.
Understood.
Quoted 2 lines
Hmm. This is really hammering home that perhaps all the remaining mlock cases are 'hard'.
A nice sideffect of me investigating these is that I am getting some good insight into how iio works. I will see if I can investigate a couple more cases
Quoted 5 lines
Thanks,
Jonathan
Quoted 1 line
Thanks,
Rohit
Quoted 12 lines
Quoted 11 lines
drivers/iio/health/max30100.c +1
-1if (!iio_buffer_enabled (indio_dev)) ret = -EAGAIN ;@@ -399,7 +399,7 @@ static int max30100_read_raw(struct iio_dev *indio_dev, } -mutex_unlock (&indio_dev->mlock); +mutex_unlock (&data->lock);break ;case IIO_CHAN_INFO_SCALE : *val =1 ;/* 0.0625 */
Jonathan Cameron
maintainerQuoted 30 lines · On Sun, 15 Mar 2020 16:27:24 +0530 Rohit Sarkar <[email protected]>…
Quoted 15 lines · Jonathan Cameron
Quoted 5 lines · On Tue, 10 Mar 2020 00:01:28 +0530 Rohit Sarkar <[email protected]>…
Use local lock instead of indio_dev's mlock.
The mlock was being used to protect local driver state thus using the local lock is a better option here.Signed-off-by Rohit Sarkar <[email protected]>Matt. Definitely need your input on this.
Quoted 4 lines
--- drivers/iio/health/max30100.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)Quoted 13 lines
drivers/iio/health/max30100.c static int max30100_read_raw(struct iio_dev *indio_dev, · line 391
Quoted 10 lines
drivers/iio/health/max30100.c +1
-1@@ -388,7 +388,7 @@ static int max30100_read_raw(struct iio_dev *indio_dev,* Temperature reading can only be acquired while engine * is running */ -mutex_lock (&indio_dev->mlock); +mutex_lock (&data->lock);Hmm.. It's another complex one. What is actually being protected here is the buffer state, but not to take it exclusively like
claim_directdoes.
Quoted 5 lines
So just to check if I understand correctly, let's say we did not use any lock in this case. If an execution thread reached the
iio_buffer_enabled()check and found the buffer to be enabled, but the buffer got disabled simultaneously, the temperature readings that we get will be corrupted. Does this make sense?
Yes, any time after that check the buffer could be disabled and we'd potentially get a garbage result.
Quoted 18 lines
Quoted 7 lines
Here we need the inverse, we want to ensure we are 'not' in the direct mode because this hardware requires the buffer to be running to read the temperature.
That is the sort of interface that is going to get userspace very confused.
Agreed
Quoted 9 lines
Matt, normally what I'd suggest here is that the temperature read should:
1) Claim direct mode, if it fails then do the dance you have here (with more comments to explain why you are taking an internal lock)
2) Start up capture as if we were in buffered mode
3) Grab that temp
4) stop capture to return to non buffered mode.
5) Release direct mode.
Quoted 6 lines
Quoted 3 lines
I guess we decided it wasn't worth the hassle.
So Rohit. This one probably needs a comment rather than any change.
The code already mentions that the "Temperature can only be acquired while engine is running.", should I add something like "mlock is acquired to protect the buffer state..." to the same comment.
I'd wait for Matt to come back with whether he'd like to enable the more complex solution to allow the buffer to be enabled on demand.
Quoted 7 lines
Quoted 7 lines
We 'could' add a '
hold_buffered_mode' function that takes the mlock, verifies we are in buffered mode and continues to hold the lock until the 'release_buffered_mode'. However, I'm not sure any other drivers do this particular dance, so clear commenting in the driver
might be enough. Should we ever change how mlock is used in the
core, we'd have to fix this driver up as well.
Quoted 7 lines
Understood.
Quoted 2 lines
Hmm. This is really hammering home that perhaps all the remaining mlock cases are 'hard'.
A nice sideffect of me investigating these is that I am getting some good insight into how iio works. I will see if I can investigate a couple more cases
Cool.
Jonathan
Quoted 19 lines
Quoted 5 lines
Thanks,
Jonathan
Quoted 1 line
Thanks,
RohitQuoted 12 lines
Quoted 11 lines
drivers/iio/health/max30100.c +1
-1if (!iio_buffer_enabled (indio_dev)) ret = -EAGAIN ;@@ -399,7 +399,7 @@ static int max30100_read_raw(struct iio_dev *indio_dev, } -mutex_unlock (&indio_dev->mlock); +mutex_unlock (&data->lock);break ;case IIO_CHAN_INFO_SCALE : *val =1 ;/* 0.0625 */
Quoted 56 lines · Jonathan Cameron
Quoted 5 lines · On Tue, 10 Mar 2020 00:01:28 +0530 Rohit Sarkar <[email protected]>…
Use local lock instead of indio_dev's mlock.
The mlock was being used to protect local driver state thus using the local lock is a better option here.Signed-off-by Rohit Sarkar <[email protected]>Matt. Definitely need your input on this.
Quoted 9 lines
--- drivers/iio/health/max30100.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)drivers/iio/health/max30100.c +0
-0drivers/iio/health/max30100.c static int max30100_read_raw(struct iio_dev *indio_dev, · line 391
Quoted 5 lines
drivers/iio/health/max30100.c +1
-1@@ -388,7 +388,7 @@ static int max30100_read_raw(struct iio_dev *indio_dev,* Temperature reading can only be acquired while engine * is running */ -mutex_lock (&indio_dev->mlock); +mutex_lock (&data->lock);Hmm.. It's another complex one. What is actually being protected here is the buffer state, but not to take it exclusively like
claim_directdoes.Here we need the inverse, we want to ensure we are 'not' in the direct mode because this hardware requires the buffer to be running to read the temperature.
That is the sort of interface that is going to get userspace very confused.
Matt, normally what I'd suggest here is that the temperature read should:
1) Claim direct mode, if it fails then do the dance you have here (with more comments to explain why you are taking an internal lock)
2) Start up capture as if we were in buffered mode
3) Grab that temp
4) stop capture to return to non buffered mode.
5) Release direct mode.I guess we decided it wasn't worth the hassle.
So Rohit. This one probably needs a comment rather than any change. We 'could' add a '
hold_buffered_mode' function that takes the mlock, verifies we are in buffered mode and continues to hold the lock until the 'release_buffered_mode'. However, I'm not sure any other drivers do this particular dance, so clear commenting in the driver
might be enough. Should we ever change how mlock is used in the
core, we'd have to fix this driver up as well.
Quoted 2 lines
Hmm. This is really hammering home that perhaps all the remaining mlock cases are 'hard'.
Heh really had to look this over what I was doing since it has been almost half a decade now :).
Think locking that mutex was only to prevent another read during the temp reading, and not really
not sure how effective that is actually. Especially since the I2C subsystem should handle those reads
in a queue like fashion.
- Matt
Quoted 18 lines
Thanks,
Jonathan
Quoted 12 lines
drivers/iio/health/max30100.c +1
-1if (!iio_buffer_enabled (indio_dev)) ret = -EAGAIN ;@@ -399,7 +399,7 @@ static int max30100_read_raw(struct iio_dev *indio_dev, } -mutex_unlock (&indio_dev->mlock); +mutex_unlock (&data->lock);break ;case IIO_CHAN_INFO_SCALE : *val =1 ;/* 0.0625 */
Quoted 64 lines · Matt Ranostay
Quoted 56 lines · Jonathan Cameron
Quoted 5 lines · On Tue, 10 Mar 2020 00:01:28 +0530 Rohit Sarkar <[email protected]>…
Use local lock instead of indio_dev's mlock.
The mlock was being used to protect local driver state thus using the local lock is a better option here.Signed-off-by Rohit Sarkar <[email protected]>Matt. Definitely need your input on this.
Quoted 9 lines
--- drivers/iio/health/max30100.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)drivers/iio/health/max30100.c +0
-0drivers/iio/health/max30100.c static int max30100_read_raw(struct iio_dev *indio_dev, · line 391
Quoted 5 lines
drivers/iio/health/max30100.c +1
-1@@ -388,7 +388,7 @@ static int max30100_read_raw(struct iio_dev *indio_dev,* Temperature reading can only be acquired while engine * is running */ -mutex_lock (&indio_dev->mlock); +mutex_lock (&data->lock);Hmm.. It's another complex one. What is actually being protected here is the buffer state, but not to take it exclusively like
claim_directdoes.Here we need the inverse, we want to ensure we are 'not' in the direct mode because this hardware requires the buffer to be running to read the temperature.
That is the sort of interface that is going to get userspace very confused.
Matt, normally what I'd suggest here is that the temperature read should:
1) Claim direct mode, if it fails then do the dance you have here (with more comments to explain why you are taking an internal lock)
2) Start up capture as if we were in buffered mode
3) Grab that temp
4) stop capture to return to non buffered mode.
5) Release direct mode.I guess we decided it wasn't worth the hassle.
So Rohit. This one probably needs a comment rather than any change. We 'could' add a '
hold_buffered_mode' function that takes the mlock, verifies we are in buffered mode and continues to hold the lock until the 'release_buffered_mode'. However, I'm not sure any other drivers do this particular dance, so clear commenting in the driver
might be enough. Should we ever change how mlock is used in the
core, we'd have to fix this driver up as well.Quoted 2 lines
Hmm. This is really hammering home that perhaps all the remaining mlock cases are 'hard'.
Heh really had to look this over what I was doing since it has been almost half a decade now :).
Quoted 8 lines
Think locking that mutex was only to prevent another read during the temp reading, and not really
not sure how effective that is actually. Especially since the I2C subsystem should handle those reads
in a queue like fashion.- Matt
So to be clear I think we can just remove the lock period since the odds of this actually being requested (or disabled) at the exact time so very remote. Along with the worse case being a failed read.
- Matt
Quoted 18 lines
Quoted 18 lines
Thanks,
Jonathan
Quoted 12 lines
drivers/iio/health/max30100.c +1
-1if (!iio_buffer_enabled (indio_dev)) ret = -EAGAIN ;@@ -399,7 +399,7 @@ static int max30100_read_raw(struct iio_dev *indio_dev, } -mutex_unlock (&indio_dev->mlock); +mutex_unlock (&data->lock);break ;case IIO_CHAN_INFO_SCALE : *val =1 ;/* 0.0625 */
Quoted 72 lines · On Mon, 16 Mar 2020 01:29:28 -0700 Matt Ranostay <[email protected]>…
Quoted 70 lines · Matt Ranostay
Quoted 56 lines · Jonathan Cameron
Quoted 5 lines · On Tue, 10 Mar 2020 00:01:28 +0530 Rohit Sarkar <[email protected]>…
Use local lock instead of indio_dev's mlock.
The mlock was being used to protect local driver state thus using the local lock is a better option here.Signed-off-by Rohit Sarkar <[email protected]>Matt. Definitely need your input on this.
Quoted 9 lines
--- drivers/iio/health/max30100.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)drivers/iio/health/max30100.c +0
-0drivers/iio/health/max30100.c static int max30100_read_raw(struct iio_dev *indio_dev, · line 391
Quoted 5 lines
drivers/iio/health/max30100.c +1
-1@@ -388,7 +388,7 @@ static int max30100_read_raw(struct iio_dev *indio_dev,* Temperature reading can only be acquired while engine * is running */ -mutex_lock (&indio_dev->mlock); +mutex_lock (&data->lock);Hmm.. It's another complex one. What is actually being protected here is the buffer state, but not to take it exclusively like
claim_directdoes.Here we need the inverse, we want to ensure we are 'not' in the direct mode because this hardware requires the buffer to be running to read the temperature.
That is the sort of interface that is going to get userspace very confused.
Matt, normally what I'd suggest here is that the temperature read should:
1) Claim direct mode, if it fails then do the dance you have here (with more comments to explain why you are taking an internal lock)
2) Start up capture as if we were in buffered mode
3) Grab that temp
4) stop capture to return to non buffered mode.
5) Release direct mode.I guess we decided it wasn't worth the hassle.
So Rohit. This one probably needs a comment rather than any change. We 'could' add a '
hold_buffered_mode' function that takes the mlock, verifies we are in buffered mode and continues to hold the lock until the 'release_buffered_mode'. However, I'm not sure any other drivers do this particular dance, so clear commenting in the driver
might be enough. Should we ever change how mlock is used in the
core, we'd have to fix this driver up as well.Quoted 2 lines
Hmm. This is really hammering home that perhaps all the remaining mlock cases are 'hard'.
Heh really had to look this over what I was doing since it has been almost half a decade now :).
Think locking that mutex was only to prevent another read during the temp reading, and not really
not sure how effective that is actually. Especially since the I2C subsystem should handle those reads
in a queue like fashion.
Quoted 6 lines
Quoted 2 lines
- Matt
So to be clear I think we can just remove the lock period since the odds of this actually being requested (or disabled) at the exact time so very remote. Along with the worse case being a failed read.
I disagree. What that lock prevents is disabling buffered mode between
the check on whether it is enabled and the read. That's a clear race
so we should keep the lock.
Jonathan
Quoted 21 lines
- Matt
Quoted 18 lines
Quoted 18 lines
Thanks,
Jonathan
Quoted 12 lines
drivers/iio/health/max30100.c +1
-1if (!iio_buffer_enabled (indio_dev)) ret = -EAGAIN ;@@ -399,7 +399,7 @@ static int max30100_read_raw(struct iio_dev *indio_dev, } -mutex_unlock (&indio_dev->mlock); +mutex_unlock (&data->lock);break ;case IIO_CHAN_INFO_SCALE : *val =1 ;/* 0.0625 */