Skip to contentSkip to conversation

bonding: restructure and simplify bond_for_each_slave_next()

PATCH Superseded Ding Tianhong 2 participants 3 messages Aug 30, 2013 – Sep 4, 2013

Subsystems (3)

v1 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

PATCH 5/6 bonding: restructure and simplify bond_for_each_slave_next()

remove the wordy int and add bond_for_each_slave_next_rcu() for future use.

Cc Nikolay Aleksandrov <[email protected]>
---
 drivers/net/bonding/bond_alb.c  |  3 +--
 drivers/net/bonding/bond_main.c |  6 ++----
 drivers/net/bonding/bonding.h   | 19 +++++++++++++++----
 3 files changed, 18 insertions(+), 10 deletions(-)
drivers/net/bonding/bond_alb.c +1 -2
index 3a5db7b..d266c56 100644
@@ -383,7 +383,6 @@ static struct slave *rlb_next_rx_slave(struct bonding *bond)
 {
 	struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
 	struct slave *rx_slave, *slave, *start_at;
-	int i = 0;
 
 	if (bond_info->next_rx_slave)
 		start_at = bond_info->next_rx_slave;
@@ -392,7 +391,7 @@ static struct slave *rlb_next_rx_slave(struct bonding *bond)
 
 	rx_slave = NULL;
 
-	bond_for_each_slave_from(bond, slave, i, start_at) {
+	bond_for_each_slave_from(bond, slave, start_at) {
 		if (SLAVE_IS_OK(slave)) {
 			if (!rx_slave) {
 				rx_slave = slave;
drivers/net/bonding/bond_main.c +2 -4
index 4264a76..8c9902a 100644
@@ -904,7 +904,6 @@ static struct slave *bond_find_best_slave(struct bonding *bond)
 	struct slave *new_active, *old_active;
 	struct slave *bestslave = NULL;
 	int mintime = bond->params.updelay;
-	int i;
 
 	new_active = bond->curr_active_slave;
 
@@ -923,7 +922,7 @@ static struct slave *bond_find_best_slave(struct bonding *bond)
 	/* remember where to stop iterating over the slaves */
 	old_active = new_active;
 
-	bond_for_each_slave_from(bond, new_active, i, old_active) {
+	bond_for_each_slave_from(bond, new_active, old_active) {
 		if (new_active->link == BOND_LINK_UP) {
 			return new_active;
 		} else if (new_active->link == BOND_LINK_BACK &&
@@ -2891,7 +2890,6 @@ do_failover:
 static void bond_ab_arp_probe(struct bonding *bond)
 {
 	struct slave *slave, *next_slave;
-	int i;
 
 	read_lock(&bond->curr_slave_lock);
 
@@ -2923,7 +2921,7 @@ static void bond_ab_arp_probe(struct bonding *bond)
 
 	/* search for next candidate */
 	next_slave = bond_next_slave(bond, bond->current_arp_slave);
-	bond_for_each_slave_from(bond, slave, i, next_slave) {
+	bond_for_each_slave_from(bond, slave, next_slave) {
 		if (IS_UP(slave->dev)) {
 			slave->link = BOND_LINK_BACK;
 			bond_set_slave_active_flags(slave);
drivers/net/bonding/bonding.h +15 -4
index 9898493..a3ab47f 100644
@@ -119,14 +119,25 @@
  * bond_for_each_slave_from - iterate the slaves list from a starting point
  * @bond:	the bond holding this list.
  * @pos:	current slave.
- * @cnt:	counter for max number of moves
  * @start:	starting point.
  *
  * Caller must hold bond->lock
  */
-#define bond_for_each_slave_from(bond, pos, cnt, start) \
-	for (cnt = 0, pos = start; pos && cnt < (bond)->slave_cnt; \
-	     cnt++, pos = bond_next_slave(bond, pos))
+#define bond_for_each_slave_from(bond, pos, start) \
+	for (int cnt = 0, pos = start; pos && cnt < (bond)->slave_cnt; \
+	    cnt++, pos = bond_next_slave(bond, pos))
+
+/**
+ * bond_for_each_slave_from_rcu - iterate the slaves list from a starting point
+ * @bond:	the bond holding this list.
+ * @pos:	current slave.
+ * @start:	starting point.
+ *
+ * Caller must hold rcu_read_lock
+ */
+#define bond_for_each_slave_from_rcu(bond, pos, start) \
+	for (int cnt = 0, pos = start; pos && cnt < (bond)->slave_cnt; \
+	    cnt++, pos = bond_next_slave_rcu(bond, pos))
 
 /**
  * bond_for_each_slave - iterate over all slaves
Signature · 1 line
1.8.2.1
#1 lore raw
Quoted 79 lines · Ding Tianhong

remove the wordy int and add bond_for_each_slave_next_rcu() for future use.

Cc Nikolay Aleksandrov <[email protected]>
---
 drivers/net/bonding/bond_alb.c  |  3 +--
 drivers/net/bonding/bond_main.c |  6 ++----
 drivers/net/bonding/bonding.h   | 19 +++++++++++++++----
 3 files changed, 18 insertions(+), 10 deletions(-)
drivers/net/bonding/bond_alb.c +1 -2
index 3a5db7b..d266c56 100644
@@ -383,7 +383,6 @@ static struct slave *rlb_next_rx_slave(struct bonding *bond)
 {
 	struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
 	struct slave *rx_slave, *slave, *start_at;
-	int i = 0;
 
 	if (bond_info->next_rx_slave)
 		start_at = bond_info->next_rx_slave;
@@ -392,7 +391,7 @@ static struct slave *rlb_next_rx_slave(struct bonding *bond)
 
 	rx_slave = NULL;
 
-	bond_for_each_slave_from(bond, slave, i, start_at) {
+	bond_for_each_slave_from(bond, slave, start_at) {
 		if (SLAVE_IS_OK(slave)) {
 			if (!rx_slave) {
 				rx_slave = slave;
drivers/net/bonding/bond_main.c +2 -4
index 4264a76..8c9902a 100644
@@ -904,7 +904,6 @@ static struct slave *bond_find_best_slave(struct bonding *bond)
 	struct slave *new_active, *old_active;
 	struct slave *bestslave = NULL;
 	int mintime = bond->params.updelay;
-	int i;
 
 	new_active = bond->curr_active_slave;
 
@@ -923,7 +922,7 @@ static struct slave *bond_find_best_slave(struct bonding *bond)
 	/* remember where to stop iterating over the slaves */
 	old_active = new_active;
 
-	bond_for_each_slave_from(bond, new_active, i, old_active) {
+	bond_for_each_slave_from(bond, new_active, old_active) {
 		if (new_active->link == BOND_LINK_UP) {
 			return new_active;
 		} else if (new_active->link == BOND_LINK_BACK &&
@@ -2891,7 +2890,6 @@ do_failover:
 static void bond_ab_arp_probe(struct bonding *bond)
 {
 	struct slave *slave, *next_slave;
-	int i;
 
 	read_lock(&bond->curr_slave_lock);
 
@@ -2923,7 +2921,7 @@ static void bond_ab_arp_probe(struct bonding *bond)
 
 	/* search for next candidate */
 	next_slave = bond_next_slave(bond, bond->current_arp_slave);
-	bond_for_each_slave_from(bond, slave, i, next_slave) {
+	bond_for_each_slave_from(bond, slave, next_slave) {
 		if (IS_UP(slave->dev)) {
 			slave->link = BOND_LINK_BACK;
 			bond_set_slave_active_flags(slave);
drivers/net/bonding/bonding.h +0 -1
index 9898493..a3ab47f 100644
@@ -119,14 +119,25 @@
  * bond_for_each_slave_from - iterate the slaves list from a starting point
  * @bond:	the bond holding this list.
  * @pos:	current slave.
- * @cnt:	counter for max number of moves
  * @start:	starting point.

drivers/net/bonding/bonding.h line 129

Quoted 10 lines
drivers/net/bonding/bonding.h +4 -3
  *
  * Caller must hold bond->lock
  */
-#define bond_for_each_slave_from(bond, pos, cnt, start) \
-	for (cnt = 0, pos = start; pos && cnt < (bond)->slave_cnt; \
-	     cnt++, pos = bond_next_slave(bond, pos))
+#define bond_for_each_slave_from(bond, pos, start) \
+	for (int cnt = 0, pos = start; pos && cnt < (bond)->slave_cnt; \
+	    cnt++, pos = bond_next_slave(bond, pos))
+

Please read below the argument against using the nested cnt definition.

drivers/net/bonding/bonding.h line 141

Quoted 12 lines
drivers/net/bonding/bonding.h +11 -0
+/**
+ * bond_for_each_slave_from_rcu - iterate the slaves list from a starting point
+ * @bond:	the bond holding this list.
+ * @pos:	current slave.
+ * @start:	starting point.
+ *
+ * Caller must hold rcu_read_lock
+ */
+#define bond_for_each_slave_from_rcu(bond, pos, start) \
+	for (int cnt = 0, pos = start; pos && cnt < (bond)->slave_cnt; \
+	    cnt++, pos = bond_next_slave_rcu(bond, pos))
 

I don't think you can rely on slave_cnt in RCU, you may go overboard and pass twice over the same slave if a slave gets removed, or the opposite. Also this definition of cnt is troublesome because the name is quite common, I don't know if it's accepted, but it if it is at least change the name to something like __cnt or anything that is less likely to be defined.
The cnt argument goes for bond_for_each_slave_from as well.

Quoted 3 lines
drivers/net/bonding/bonding.h +0 -0
 /**
  * bond_for_each_slave - iterate over all slaves
#2 lore raw
Quoted 102 lines · Nikolay Aleksandrov
Quoted 79 lines · Ding Tianhong

remove the wordy int and add bond_for_each_slave_next_rcu() for future use.

Cc Nikolay Aleksandrov <[email protected]>
---
 drivers/net/bonding/bond_alb.c  |  3 +--
 drivers/net/bonding/bond_main.c |  6 ++----
 drivers/net/bonding/bonding.h   | 19 +++++++++++++++----
 3 files changed, 18 insertions(+), 10 deletions(-)
drivers/net/bonding/bond_alb.c +1 -2
index 3a5db7b..d266c56 100644
@@ -383,7 +383,6 @@ static struct slave *rlb_next_rx_slave(struct bonding *bond)
 {
 	struct alb_bond_info *bond_info = &(BOND_ALB_INFO(bond));
 	struct slave *rx_slave, *slave, *start_at;
-	int i = 0;
 
 	if (bond_info->next_rx_slave)
 		start_at = bond_info->next_rx_slave;
@@ -392,7 +391,7 @@ static struct slave *rlb_next_rx_slave(struct bonding *bond)
 
 	rx_slave = NULL;
 
-	bond_for_each_slave_from(bond, slave, i, start_at) {
+	bond_for_each_slave_from(bond, slave, start_at) {
 		if (SLAVE_IS_OK(slave)) {
 			if (!rx_slave) {
 				rx_slave = slave;
drivers/net/bonding/bond_main.c +2 -4
index 4264a76..8c9902a 100644
@@ -904,7 +904,6 @@ static struct slave *bond_find_best_slave(struct bonding *bond)
 	struct slave *new_active, *old_active;
 	struct slave *bestslave = NULL;
 	int mintime = bond->params.updelay;
-	int i;
 
 	new_active = bond->curr_active_slave;
 
@@ -923,7 +922,7 @@ static struct slave *bond_find_best_slave(struct bonding *bond)
 	/* remember where to stop iterating over the slaves */
 	old_active = new_active;
 
-	bond_for_each_slave_from(bond, new_active, i, old_active) {
+	bond_for_each_slave_from(bond, new_active, old_active) {
 		if (new_active->link == BOND_LINK_UP) {
 			return new_active;
 		} else if (new_active->link == BOND_LINK_BACK &&
@@ -2891,7 +2890,6 @@ do_failover:
 static void bond_ab_arp_probe(struct bonding *bond)
 {
 	struct slave *slave, *next_slave;
-	int i;
 
 	read_lock(&bond->curr_slave_lock);
 
@@ -2923,7 +2921,7 @@ static void bond_ab_arp_probe(struct bonding *bond)
 
 	/* search for next candidate */
 	next_slave = bond_next_slave(bond, bond->current_arp_slave);
-	bond_for_each_slave_from(bond, slave, i, next_slave) {
+	bond_for_each_slave_from(bond, slave, next_slave) {
 		if (IS_UP(slave->dev)) {
 			slave->link = BOND_LINK_BACK;
 			bond_set_slave_active_flags(slave);
drivers/net/bonding/bonding.h +0 -1
index 9898493..a3ab47f 100644
@@ -119,14 +119,25 @@
  * bond_for_each_slave_from - iterate the slaves list from a starting point
  * @bond:	the bond holding this list.
  * @pos:	current slave.
- * @cnt:	counter for max number of moves
  * @start:	starting point.

drivers/net/bonding/bonding.h line 129

Quoted 10 lines
drivers/net/bonding/bonding.h +4 -3
  *
  * Caller must hold bond->lock
  */
-#define bond_for_each_slave_from(bond, pos, cnt, start) \
-	for (cnt = 0, pos = start; pos && cnt < (bond)->slave_cnt; \
-	     cnt++, pos = bond_next_slave(bond, pos))
+#define bond_for_each_slave_from(bond, pos, start) \
+	for (int cnt = 0, pos = start; pos && cnt < (bond)->slave_cnt; \
+	    cnt++, pos = bond_next_slave(bond, pos))
+

Please read below the argument against using the nested cnt definition.

Quoted 10 lines
drivers/net/bonding/bonding.h +10 -0
+/**
+ * bond_for_each_slave_from_rcu - iterate the slaves list from a starting point
+ * @bond:	the bond holding this list.
+ * @pos:	current slave.
+ * @start:	starting point.
+ *
+ * Caller must hold rcu_read_lock
+ */
+#define bond_for_each_slave_from_rcu(bond, pos, start) \
+	for (int cnt = 0, pos = start; pos && cnt < (bond)->slave_cnt; \
Quoted 9 lines

drivers/net/bonding/bonding.h line 141

Quoted 2 lines
drivers/net/bonding/bonding.h +1 -0
+	    cnt++, pos = bond_next_slave_rcu(bond, pos))
 

I don't think you can rely on slave_cnt in RCU, you may go overboard and pass twice over the same slave if a slave gets removed, or the opposite. Also this definition of cnt is troublesome because the name is quite common, I don't know if it's accepted, but it if it is at least change the name to something like __cnt or anything that is less likely to be defined.
The cnt argument goes for bond_for_each_slave_from as well.

yes, more details need to think about, I modify the function to this:

#define bond_for_each_slave_from(bond, pos, start) \
        for (pos = start; (pos == bond_next_slave(bond, start) ? \
                    &pos->list != &bond->slave_list : \
                    &pos->list != &start->list); \
                    pos = bond_next_slave(bond, pos))

I think it is fine here, or I miss something, please tell me, thanks. :)

Quoted 11 lines

drivers/net/bonding/bonding.h line 143

Quoted 3 lines
drivers/net/bonding/bonding.h +0 -0
 /**
  * bond_for_each_slave - iterate over all slaves

--
To unsubscribe from this list: send the line "unsubscribe netdev" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

.