Skip to contentSkip to conversation

bonding: the calling of bond->slave_cnt need protection

PATCH Superseded Ding Tianhong 3 participants 6 messages Jul 20, 2013 – Jul 22, 2013

Subsystems (3)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 3/4 bonding: the calling of bond->slave_cnt need protection

The bonding_store_mode has rtnl protection, so no need to get read lock for bond->slave_cnt, but the bonding_store_fail_over_mac need to protect the bond->slave_cnt, so add read_lock().

---
 drivers/net/bonding/bond_sysfs.c | 3 +++
 1 file changed, 3 insertions(+)
drivers/net/bonding/bond_sysfs.c +3 -0
index dc36a3d..d01a189 100644
@@ -504,11 +504,14 @@ static ssize_t bonding_store_fail_over_mac(struct device *d,
 	int new_value;
 	struct bonding *bond = to_bond(d);
 
+	read_lock(&bond->lock);
 	if (bond->slave_cnt != 0) {
 		pr_err("%s: Can't alter fail_over_mac with slaves in bond.\n",
 		       bond->dev->name);
+		read_unlock(&bond->lock);
 		return -EPERM;
 	}
+	read_unlock(&bond->lock);
 
 	new_value = bond_parse_parm(buf, fail_over_mac_tbl);
 	if (new_value < 0) {
Signature · 1 line
1.8.0
#1 lore raw
Quoted 16 lines · dingtianhong

The bonding_store_mode has rtnl protection, so no need to get read lock for bond->slave_cnt, but the bonding_store_fail_over_mac need to protect the bond->slave_cnt, so add read_lock().

---
drivers/net/bonding/bond_sysfs.c | 3 +++

1 file changed, 3 insertions(+)
drivers/net/bonding/bond_sysfs.c +0 -0
index dc36a3d..d01a189 100644
@@ -504,11 +504,14 @@ static ssize_t bonding_store_fail_over_mac(struct device *d,
 	int new_value;

drivers/net/bonding/bond_sysfs.c static ssize_t bonding_store_fail_over_mac(struct device *d, · line 514

Quoted 10 lines
drivers/net/bonding/bond_sysfs.c +3 -0
 	struct bonding *bond = to_bond(d);
 
+	read_lock(&bond->lock);
 	if (bond->slave_cnt != 0) {
 		pr_err("%s: Can't alter fail_over_mac with slaves in bond.\n",
 		       bond->dev->name);
+		read_unlock(&bond->lock);
 		return -EPERM;
 	}
+	read_unlock(&bond->lock);

Maybe it's Saturday, but I really don't see *any* point in this locking.

I think you've meant that we need the rtnl protection while reading slave_cnt AND updating the .fail_over_mac, so that in between we won't add new slaves with outdated params.

Something like this (untested):

drivers/net/bonding/bond_sysfs.c +11 -4
index dc36a3d..8a5a6a3 100644
@@ -501,20 +501,25 @@ static ssize_t bonding_store_fail_over_mac(struct device *d,
 					   struct device_attribute *attr,
 					   const char *buf, size_t count)
 {
-	int new_value;
+	int new_value, ret = count;
 	struct bonding *bond = to_bond(d);
 
+	if (!rtnl_trylock())
+		return restart_syscall();
+
 	if (bond->slave_cnt != 0) {
 		pr_err("%s: Can't alter fail_over_mac with slaves in bond.\n",
 		       bond->dev->name);
-		return -EPERM;
+		ret = -EPERM;
+		goto out;
 	}
 
 	new_value = bond_parse_parm(buf, fail_over_mac_tbl);
 	if (new_value < 0) {
 		pr_err("%s: Ignoring invalid fail_over_mac value %s.\n",
 		       bond->dev->name, buf);
-		return -EINVAL;
+		ret = -EINVAL;
+		goto out;
 	}
 
 	bond->params.fail_over_mac = new_value;
@@ -522,7 +527,9 @@ static ssize_t bonding_store_fail_over_mac(struct device *d,
 		bond->dev->name, fail_over_mac_tbl[new_value].modename,
 		new_value);
 
-	return count;
+out:
+	rtnl_unlock();
+	return ret;
 }
 
 static DEVICE_ATTR(fail_over_mac, S_IRUGO | S_IWUSR,
Quoted 3 lines
drivers/net/bonding/bond_sysfs.c +0 -0
 	new_value = bond_parse_parm(buf, fail_over_mac_tbl);
 	if (new_value < 0) {
#2 lore raw
Quoted 7 lines · Veaceslav Falico
Quoted 6 lines · dingtianhong

The bonding_store_mode has rtnl protection, so no need to get read lock for bond->slave_cnt, but the bonding_store_fail_over_mac need to protect the bond->slave_cnt, so add read_lock().

<snip>

Quoted 9 lines

Maybe it's Saturday, but I really don't see *any* point in this locking.

I think you've meant that we need the rtnl protection while reading slave_cnt AND updating the .fail_over_mac, so that in between we won't add new slaves with outdated params.

Something like this (untested):

Indeed, Veaceslav's way is the correct one (I've looked at this race before), but IMO it's not worth it to protect fail_over_mac as the worst that could happen is inconsistency with the MAC addresses which isn't fatal. Anyway, I still haven't had my coffee and might be missing something :-)

Cheers,
Nik

#3 lore raw
Quoted 16 lines · Nikolay Aleksandrov
Quoted 7 lines · Veaceslav Falico
Quoted 6 lines · dingtianhong

The bonding_store_mode has rtnl protection, so no need to get read lock for bond->slave_cnt, but the bonding_store_fail_over_mac need to protect the bond->slave_cnt, so add read_lock().

<snip>

Quoted 7 lines

Maybe it's Saturday, but I really don't see *any* point in this locking.

I think you've meant that we need the rtnl protection while reading slave_cnt AND updating the .fail_over_mac, so that in between we won't add new slaves with outdated params.

Quoted 6 lines
Quoted 2 lines

Something like this (untested):

Indeed, Veaceslav's way is the correct one (I've looked at this race before), but IMO it's not worth it to protect fail_over_mac as the worst that could happen is inconsistency with the MAC addresses which isn't fatal. Anyway, I still haven't had my coffee and might be missing something :-)

Yep, agree that it's kind of minor and hard to hit in real life.

OTOH, getting the rtnl here costs us virtually nothing and might save someone from a headache :). And it also follows the logic "don't change anything slave-related without rtnl".

So I'd rather have it, as a minor improvement :).

Quoted 4 lines

Cheers,
Nik

#4 lore raw
Quoted 24 lines · Veaceslav Falico
Quoted 16 lines · Nikolay Aleksandrov
Quoted 7 lines · Veaceslav Falico
Quoted 6 lines · dingtianhong

The bonding_store_mode has rtnl protection, so no need to get read lock for bond->slave_cnt, but the bonding_store_fail_over_mac need to protect the bond->slave_cnt, so add read_lock().

<snip>

Quoted 7 lines

Maybe it's Saturday, but I really don't see *any* point in this locking.

I think you've meant that we need the rtnl protection while reading slave_cnt AND updating the .fail_over_mac, so that in between we won't add new slaves with outdated params.

Quoted 6 lines
Quoted 2 lines

Something like this (untested):

Indeed, Veaceslav's way is the correct one (I've looked at this race before), but IMO it's not worth it to protect fail_over_mac as the worst that could happen is inconsistency with the MAC addresses which isn't fatal. Anyway, I still haven't had my coffee and might be missing something :-)

Quoted 11 lines

Yep, agree that it's kind of minor and hard to hit in real life.

OTOH, getting the rtnl here costs us virtually nothing and might save someone from a headache :). And it also follows the logic "don't change anything slave-related without rtnl".

So I'd rather have it, as a minor improvement :).

Quoted 3 lines

Cheers,
Nik

yes , i think it is hard to hit the problem in real lift, just looks better. :)

Quoted 3 lines
Quoted 1 line
#5 lore raw
In reply to Veaceslav Falico
Quoted 30 lines · Veaceslav Falico
Quoted 16 lines · dingtianhong

The bonding_store_mode has rtnl protection, so no need to get read lock for bond->slave_cnt, but the bonding_store_fail_over_mac need to protect the bond->slave_cnt, so add read_lock().

---
drivers/net/bonding/bond_sysfs.c | 3 +++

1 file changed, 3 insertions(+)
drivers/net/bonding/bond_sysfs.c +0 -0
index dc36a3d..d01a189 100644
@@ -504,11 +504,14 @@ static ssize_t bonding_store_fail_over_mac(struct device *d,
    int new_value;

drivers/net/bonding/bond_sysfs.c static ssize_t bonding_store_fail_over_mac(struct device *d, · line 514

Quoted 10 lines
drivers/net/bonding/bond_sysfs.c +3 -0
    struct bonding *bond = to_bond(d);
 
+    read_lock(&bond->lock);
    if (bond->slave_cnt != 0) {
        pr_err("%s: Can't alter fail_over_mac with slaves in bond.\n",
               bond->dev->name);
+        read_unlock(&bond->lock);
        return -EPERM;
    }
+    read_unlock(&bond->lock);

Maybe it's Saturday, but I really don't see *any* point in this locking.

Quoted 3 lines

I think you've meant that we need the rtnl protection while reading slave_cnt AND updating the .fail_over_mac, so that in between we won't add new slaves with outdated params.

yes, as you said, the rtnl lock is enough here, but I think rtnl lock is bigger than a readlock, so think about the performance, i choose the readlock to protect slave_cnt.

Quoted 53 lines

Something like this (untested):

drivers/net/bonding/bond_sysfs.c +11 -4
index dc36a3d..8a5a6a3 100644
@@ -501,20 +501,25 @@ static ssize_t bonding_store_fail_over_mac(struct device *d,
                        struct device_attribute *attr,
                        const char *buf, size_t count)
 {
-    int new_value;
+    int new_value, ret = count;
     struct bonding *bond = to_bond(d);
 
+    if (!rtnl_trylock())
+        return restart_syscall();
+
     if (bond->slave_cnt != 0) {
         pr_err("%s: Can't alter fail_over_mac with slaves in bond.\n",
                bond->dev->name);
-        return -EPERM;
+        ret = -EPERM;
+        goto out;
     }
 
     new_value = bond_parse_parm(buf, fail_over_mac_tbl);
     if (new_value < 0) {
         pr_err("%s: Ignoring invalid fail_over_mac value %s.\n",
                bond->dev->name, buf);
-        return -EINVAL;
+        ret = -EINVAL;
+        goto out;
     }
 
     bond->params.fail_over_mac = new_value;
@@ -522,7 +527,9 @@ static ssize_t bonding_store_fail_over_mac(struct device *d,
         bond->dev->name, fail_over_mac_tbl[new_value].modename,
         new_value);
 
-    return count;
+out:
+    rtnl_unlock();
+    return ret;
 }
 
 static DEVICE_ATTR(fail_over_mac, S_IRUGO | S_IWUSR,

drivers/net/bonding/bond_sysfs.c static ssize_t bonding_store_fail_over_mac(struct device *d, · line 518

Quoted 3 lines
drivers/net/bonding/bond_sysfs.c +0 -0
    new_value = bond_parse_parm(buf, fail_over_mac_tbl);
    if (new_value < 0) {

.