Skip to contentSkip to conversation

md: properly lock and unlock in rdev_attr_store()

PATCH In review Lukas Bulwahn 3 participants 4 messages Apr 28, 2019 – May 6, 2019 MD RAID

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 md: properly lock and unlock in rdev_attr_store()

rdev_attr_store() should lock and unlock mddev->reconfig_mutex in a balanced way with mddev_lock() and mddev_unlock().

But when rdev->mddev is NULL, rdev_attr_store() would try to unlock without locking before. Resolve this locking issue..

This locking issue was detected with Clang Thread Safety Analyser:

drivers/md/md.c:3393:3: warning: releasing mutex 'mddev->reconfig_mutex' that was not held [-Wthread-safety-analysis]

                mddev_unlock(mddev);
                ^

This warning was reported after annotating mutex functions and mddev_lock() and mddev_unlock().

Fixes 27c529bb8e90 ("md: lock access to rdev attributes properly")

---
Arnd, Neil, here a proposal to fix lock and unlocking asymmetry.

I quite sure that if mddev is NULL, it should just return.

I am still puzzled if the return value from mddev_lock() should be really return by rdev_attr_store() when it is not 0. But that was the behaviour before, so I will keep it that way.

 drivers/md/md.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
drivers/md/md.c +3 -1
index 05ffffb8b769..a9735d8f1e70 100644
@@ -3384,7 +3384,9 @@ rdev_attr_store(struct kobject *kobj, struct attribute *attr,
 		return -EIO;
 	if (!capable(CAP_SYS_ADMIN))
 		return -EACCES;
-	rv = mddev ? mddev_lock(mddev): -EBUSY;
+	if (!mddev)
+		return -EBUSY;
+	rv = mddev_lock(mddev);
 	if (!rv) {
 		if (rdev->mddev == NULL)
 			rv = -EBUSY;
Signature · 1 line
2.17.1
#1 lore raw
Quoted 16 lines · Lukas Bulwahn

rdev_attr_store() should lock and unlock mddev->reconfig_mutex in a balanced way with mddev_lock() and mddev_unlock().

But when rdev->mddev is NULL, rdev_attr_store() would try to unlock without locking before. Resolve this locking issue..

This locking issue was detected with Clang Thread Safety Analyser:

drivers/md/md.c:3393:3: warning: releasing mutex 'mddev->reconfig_mutex' that was not held [-Wthread-safety-analysis]

                mddev_unlock(mddev);
                ^

This warning was reported after annotating mutex functions and mddev_lock() and mddev_unlock().

Quoted 7 lines
Fixes 27c529bb8e90 ("md: lock access to rdev attributes properly")

---
Arnd, Neil, here a proposal to fix lock and unlocking asymmetry.

I quite sure that if mddev is NULL, it should just return.

I think current code will just return -EBUSY when mddev is NULL, right?

Song

Quoted 26 lines

I am still puzzled if the return value from mddev_lock() should be really return by rdev_attr_store() when it is not 0. But that was the behaviour before, so I will keep it that way.

 drivers/md/md.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
drivers/md/md.c +3 -2
index 05ffffb8b769..a9735d8f1e70 100644
@@ -3384,7 +3384,9 @@ rdev_attr_store(struct kobject *kobj, struct attribute *attr,
                return -EIO;
        if (!capable(CAP_SYS_ADMIN))
                return -EACCES;
-       rv = mddev ? mddev_lock(mddev): -EBUSY;
+       if (!mddev)
+               return -EBUSY;
+       rv = mddev_lock(mddev);
        if (!rv) {
                if (rdev->mddev == NULL)
                        rv = -EBUSY;
--

2.17.1

#2 lore raw
In reply to Lukas Bulwahn
Quoted 2 lines · Lukas Bulwahn

rdev_attr_store() should lock and unlock mddev->reconfig_mutex in a balanced way with mddev_lock() and mddev_unlock().

It does.

Quoted 3 lines

But when rdev->mddev is NULL, rdev_attr_store() would try to unlock without locking before. Resolve this locking issue..

This is incorrect.

Quoted 2 lines

This locking issue was detected with Clang Thread Safety Analyser:

Either the Clang Thread Safety Analyser is broken, or you used it incorrectly.

Quoted 8 lines

drivers/md/md.c:3393:3: warning: releasing mutex 'mddev->reconfig_mutex' that was not held [-Wthread-safety-analysis]

                mddev_unlock(mddev);
                ^

This warning was reported after annotating mutex functions and mddev_lock() and mddev_unlock().

Quoted 7 lines
Fixes 27c529bb8e90 ("md: lock access to rdev attributes properly")

---
Arnd, Neil, here a proposal to fix lock and unlocking asymmetry.

I quite sure that if mddev is NULL, it should just return.

If mddev is NULL, the code does return (with -EBUSY). All you've done is change things so it returns from a different part of the code. You haven't changed the behaviour at all.

Quoted 4 lines

I am still puzzled if the return value from mddev_lock() should be really return by rdev_attr_store() when it is not 0. But that was the behaviour before, so I will keep it that way.

Certainly it should. mddev_lock() either returns 0 to indicate success or -EINTR if it received a signal.
If it was interrupted by a signal, then rdev_attr_store() should return -EINTR as well.

As Arnd tried to explain, the only possible problem here is that the C compiler is allowed to assume that rdev->mddev never changes value, so in
rv = mddev ? mddev_lock(mddev) : =EBUSY

it could load rdev->mddev, test if it is NULL, then load it again and pass that value to mddev_lock() - the new value might be NULL which would cause problems.

This could be fixed by changing

	struct mddev *mddev = rdev->mddev;
to
	struct mddev *mddev = READ_ONCE(rdev->mddev);

That is the only change that might be useful here.

NeilBrown

Quoted 21 lines
 drivers/md/md.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
drivers/md/md.c +3 -1
index 05ffffb8b769..a9735d8f1e70 100644
@@ -3384,7 +3384,9 @@ rdev_attr_store(struct kobject *kobj, struct attribute *attr,
 		return -EIO;
 	if (!capable(CAP_SYS_ADMIN))
 		return -EACCES;
-	rv = mddev ? mddev_lock(mddev): -EBUSY;
+	if (!mddev)
+		return -EBUSY;
+	rv = mddev_lock(mddev);
 	if (!rv) {
 		if (rdev->mddev == NULL)
 			rv = -EBUSY;
Signature · 1 line
2.17.1
#3 lore raw
Quoted 11 lines · NeilBrown
Quoted 2 lines · Lukas Bulwahn

rdev_attr_store() should lock and unlock mddev->reconfig_mutex in a balanced way with mddev_lock() and mddev_unlock().

It does.

Quoted 3 lines

But when rdev->mddev is NULL, rdev_attr_store() would try to unlock without locking before. Resolve this locking issue..

Quoted 8 lines

This is incorrect.

Quoted 2 lines

This locking issue was detected with Clang Thread Safety Analyser:

Either the Clang Thread Safety Analyser is broken, or you used it incorrectly.

Please ignore this patch.

Clang Thread Safety Analyser cannot handle the original code, but can handle my semantically equivalent code. I did not get that at first, and thought I fixed an issue, but I did not.

Sorry for the noise.

Lukas

Quoted 72 lines
Quoted 8 lines

drivers/md/md.c:3393:3: warning: releasing mutex 'mddev->reconfig_mutex' that was not held [-Wthread-safety-analysis]

                mddev_unlock(mddev);
                ^

This warning was reported after annotating mutex functions and mddev_lock() and mddev_unlock().

Quoted 7 lines
Fixes 27c529bb8e90 ("md: lock access to rdev attributes properly")

---
Arnd, Neil, here a proposal to fix lock and unlocking asymmetry.

I quite sure that if mddev is NULL, it should just return.

If mddev is NULL, the code does return (with -EBUSY). All you've done is change things so it returns from a different part of the code. You haven't changed the behaviour at all.

Quoted 4 lines

I am still puzzled if the return value from mddev_lock() should be really return by rdev_attr_store() when it is not 0. But that was the behaviour before, so I will keep it that way.

Certainly it should. mddev_lock() either returns 0 to indicate success or -EINTR if it received a signal.
If it was interrupted by a signal, then rdev_attr_store() should return -EINTR as well.

As Arnd tried to explain, the only possible problem here is that the C compiler is allowed to assume that rdev->mddev never changes value, so in
rv = mddev ? mddev_lock(mddev) : =EBUSY

it could load rdev->mddev, test if it is NULL, then load it again and pass that value to mddev_lock() - the new value might be NULL which would cause problems.

This could be fixed by changing

	struct mddev *mddev = rdev->mddev;
to
	struct mddev *mddev = READ_ONCE(rdev->mddev);

That is the only change that might be useful here.

NeilBrown

Quoted 21 lines
 drivers/md/md.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
drivers/md/md.c +3 -1
index 05ffffb8b769..a9735d8f1e70 100644
@@ -3384,7 +3384,9 @@ rdev_attr_store(struct kobject *kobj, struct attribute *attr,
 		return -EIO;
 	if (!capable(CAP_SYS_ADMIN))
 		return -EACCES;
-	rv = mddev ? mddev_lock(mddev): -EBUSY;
+	if (!mddev)
+		return -EBUSY;
+	rv = mddev_lock(mddev);
 	if (!rv) {
 		if (rdev->mddev == NULL)
 			rv = -EBUSY;
Signature · 1 line
2.17.1