aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorYuval Mintz <Yuval.Mintz@qlogic.com>2015-03-23 04:56:14 -0400
committerDavid S. Miller <davem@davemloft.net>2015-03-23 22:38:24 -0400
commitdff173de84958a677ce0d24b1da3cdc3a32b4238 (patch)
tree0b69f6fbf728fb82dbf311c9b8bfc0ddaa2601b3
parent7ef70aabe073380b150ee520a2605ab997a8e9d4 (diff)
bnx2x: Fix statistics locking scheme
Statistics' state-machine in bnx2x driver must be synced with various driver flows, but its current locking scheme manages to be wasteful [using 2 locks + additional local variable] and prone to race-conditions at the same time, as the state-machine and 'action' are being accessed under different locks. In addition, current 'safe exec' isn't in fact safe, since the only guarantee it gives is that DMA transactions are over, but ramrods might still be running. This patch cleans up said logic, leaving us with a single lock for the entire flow and removing the possible races. Changes from v2: - Switched into mutex locking from semaphore locking. - Release locks on error flows. Changes from v1: Failure to acquire lock fails flow instead of printing a warning and allowing access to the critical section. Signed-off-by: Yuval Mintz <Yuval.Mintz@qlogic.com> Signed-off-by: Ariel Elior <Ariel.Elior@qlogic.com> Signed-off-by: David S. Miller <davem@davemloft.net>
-rw-r--r--drivers/net/ethernet/broadcom/bnx2x/bnx2x.h4
-rw-r--r--drivers/net/ethernet/broadcom/bnx2x/bnx2x_main.c7
-rw-r--r--drivers/net/ethernet/broadcom/bnx2x/bnx2x_sriov.c4
-rw-r--r--drivers/net/ethernet/broadcom/bnx2x/bnx2x_stats.c162
-rw-r--r--drivers/net/ethernet/broadcom/bnx2x/bnx2x_stats.h6
5 files changed, 83 insertions, 100 deletions
diff --git a/drivers/net/ethernet/broadcom/bnx2x/bnx2x.h b/drivers/net/ethernet/broadcom/bnx2x/bnx2x.h
index 756053c028be..4085c4b31047 100644
--- a/drivers/net/ethernet/broadcom/bnx2x/bnx2x.h
+++ b/drivers/net/ethernet/broadcom/bnx2x/bnx2x.h
@@ -1811,7 +1811,7 @@ struct bnx2x {
1811 int stats_state; 1811 int stats_state;
1812 1812
1813 /* used for synchronization of concurrent threads statistics handling */ 1813 /* used for synchronization of concurrent threads statistics handling */
1814 spinlock_t stats_lock; 1814 struct mutex stats_lock;
1815 1815
1816 /* used by dmae command loader */ 1816 /* used by dmae command loader */
1817 struct dmae_command stats_dmae; 1817 struct dmae_command stats_dmae;
@@ -1935,8 +1935,6 @@ struct bnx2x {
1935 1935
1936 int fp_array_size; 1936 int fp_array_size;
1937 u32 dump_preset_idx; 1937 u32 dump_preset_idx;
1938 bool stats_started;
1939 struct semaphore stats_sema;
1940 1938
1941 u8 phys_port_id[ETH_ALEN]; 1939 u8 phys_port_id[ETH_ALEN];
1942 1940
diff --git a/drivers/net/ethernet/broadcom/bnx2x/bnx2x_main.c b/drivers/net/ethernet/broadcom/bnx2x/bnx2x_main.c
index 996e215fc324..ae571a199f2f 100644
--- a/drivers/net/ethernet/broadcom/bnx2x/bnx2x_main.c
+++ b/drivers/net/ethernet/broadcom/bnx2x/bnx2x_main.c
@@ -12037,9 +12037,8 @@ static int bnx2x_init_bp(struct bnx2x *bp)
12037 mutex_init(&bp->port.phy_mutex); 12037 mutex_init(&bp->port.phy_mutex);
12038 mutex_init(&bp->fw_mb_mutex); 12038 mutex_init(&bp->fw_mb_mutex);
12039 mutex_init(&bp->drv_info_mutex); 12039 mutex_init(&bp->drv_info_mutex);
12040 mutex_init(&bp->stats_lock);
12040 bp->drv_info_mng_owner = false; 12041 bp->drv_info_mng_owner = false;
12041 spin_lock_init(&bp->stats_lock);
12042 sema_init(&bp->stats_sema, 1);
12043 12042
12044 INIT_DELAYED_WORK(&bp->sp_task, bnx2x_sp_task); 12043 INIT_DELAYED_WORK(&bp->sp_task, bnx2x_sp_task);
12045 INIT_DELAYED_WORK(&bp->sp_rtnl_task, bnx2x_sp_rtnl_task); 12044 INIT_DELAYED_WORK(&bp->sp_rtnl_task, bnx2x_sp_rtnl_task);
@@ -13668,9 +13667,9 @@ static int bnx2x_eeh_nic_unload(struct bnx2x *bp)
13668 cancel_delayed_work_sync(&bp->sp_task); 13667 cancel_delayed_work_sync(&bp->sp_task);
13669 cancel_delayed_work_sync(&bp->period_task); 13668 cancel_delayed_work_sync(&bp->period_task);
13670 13669
13671 spin_lock_bh(&bp->stats_lock); 13670 mutex_lock(&bp->stats_lock);
13672 bp->stats_state = STATS_STATE_DISABLED; 13671 bp->stats_state = STATS_STATE_DISABLED;
13673 spin_unlock_bh(&bp->stats_lock); 13672 mutex_unlock(&bp->stats_lock);
13674 13673
13675 bnx2x_save_statistics(bp); 13674 bnx2x_save_statistics(bp);
13676 13675
diff --git a/drivers/net/ethernet/broadcom/bnx2x/bnx2x_sriov.c b/drivers/net/ethernet/broadcom/bnx2x/bnx2x_sriov.c
index e5aca2de1871..cfe3c7695455 100644
--- a/drivers/net/ethernet/broadcom/bnx2x/bnx2x_sriov.c
+++ b/drivers/net/ethernet/broadcom/bnx2x/bnx2x_sriov.c
@@ -2238,7 +2238,9 @@ int bnx2x_vf_close(struct bnx2x *bp, struct bnx2x_virtf *vf)
2238 2238
2239 cookie.vf = vf; 2239 cookie.vf = vf;
2240 cookie.state = VF_ACQUIRED; 2240 cookie.state = VF_ACQUIRED;
2241 bnx2x_stats_safe_exec(bp, bnx2x_set_vf_state, &cookie); 2241 rc = bnx2x_stats_safe_exec(bp, bnx2x_set_vf_state, &cookie);
2242 if (rc)
2243 goto op_err;
2242 } 2244 }
2243 2245
2244 DP(BNX2X_MSG_IOV, "set state to acquired\n"); 2246 DP(BNX2X_MSG_IOV, "set state to acquired\n");
diff --git a/drivers/net/ethernet/broadcom/bnx2x/bnx2x_stats.c b/drivers/net/ethernet/broadcom/bnx2x/bnx2x_stats.c
index d1608297c773..800ab44a07ce 100644
--- a/drivers/net/ethernet/broadcom/bnx2x/bnx2x_stats.c
+++ b/drivers/net/ethernet/broadcom/bnx2x/bnx2x_stats.c
@@ -123,36 +123,28 @@ static void bnx2x_dp_stats(struct bnx2x *bp)
123 */ 123 */
124static void bnx2x_storm_stats_post(struct bnx2x *bp) 124static void bnx2x_storm_stats_post(struct bnx2x *bp)
125{ 125{
126 if (!bp->stats_pending) { 126 int rc;
127 int rc;
128 127
129 spin_lock_bh(&bp->stats_lock); 128 if (bp->stats_pending)
130 129 return;
131 if (bp->stats_pending) {
132 spin_unlock_bh(&bp->stats_lock);
133 return;
134 }
135
136 bp->fw_stats_req->hdr.drv_stats_counter =
137 cpu_to_le16(bp->stats_counter++);
138 130
139 DP(BNX2X_MSG_STATS, "Sending statistics ramrod %d\n", 131 bp->fw_stats_req->hdr.drv_stats_counter =
140 le16_to_cpu(bp->fw_stats_req->hdr.drv_stats_counter)); 132 cpu_to_le16(bp->stats_counter++);
141 133
142 /* adjust the ramrod to include VF queues statistics */ 134 DP(BNX2X_MSG_STATS, "Sending statistics ramrod %d\n",
143 bnx2x_iov_adjust_stats_req(bp); 135 le16_to_cpu(bp->fw_stats_req->hdr.drv_stats_counter));
144 bnx2x_dp_stats(bp);
145 136
146 /* send FW stats ramrod */ 137 /* adjust the ramrod to include VF queues statistics */
147 rc = bnx2x_sp_post(bp, RAMROD_CMD_ID_COMMON_STAT_QUERY, 0, 138 bnx2x_iov_adjust_stats_req(bp);
148 U64_HI(bp->fw_stats_req_mapping), 139 bnx2x_dp_stats(bp);
149 U64_LO(bp->fw_stats_req_mapping),
150 NONE_CONNECTION_TYPE);
151 if (rc == 0)
152 bp->stats_pending = 1;
153 140
154 spin_unlock_bh(&bp->stats_lock); 141 /* send FW stats ramrod */
155 } 142 rc = bnx2x_sp_post(bp, RAMROD_CMD_ID_COMMON_STAT_QUERY, 0,
143 U64_HI(bp->fw_stats_req_mapping),
144 U64_LO(bp->fw_stats_req_mapping),
145 NONE_CONNECTION_TYPE);
146 if (rc == 0)
147 bp->stats_pending = 1;
156} 148}
157 149
158static void bnx2x_hw_stats_post(struct bnx2x *bp) 150static void bnx2x_hw_stats_post(struct bnx2x *bp)
@@ -221,7 +213,7 @@ static void bnx2x_stats_comp(struct bnx2x *bp)
221 */ 213 */
222 214
223/* should be called under stats_sema */ 215/* should be called under stats_sema */
224static void __bnx2x_stats_pmf_update(struct bnx2x *bp) 216static void bnx2x_stats_pmf_update(struct bnx2x *bp)
225{ 217{
226 struct dmae_command *dmae; 218 struct dmae_command *dmae;
227 u32 opcode; 219 u32 opcode;
@@ -519,7 +511,7 @@ static void bnx2x_func_stats_init(struct bnx2x *bp)
519} 511}
520 512
521/* should be called under stats_sema */ 513/* should be called under stats_sema */
522static void __bnx2x_stats_start(struct bnx2x *bp) 514static void bnx2x_stats_start(struct bnx2x *bp)
523{ 515{
524 if (IS_PF(bp)) { 516 if (IS_PF(bp)) {
525 if (bp->port.pmf) 517 if (bp->port.pmf)
@@ -531,34 +523,13 @@ static void __bnx2x_stats_start(struct bnx2x *bp)
531 bnx2x_hw_stats_post(bp); 523 bnx2x_hw_stats_post(bp);
532 bnx2x_storm_stats_post(bp); 524 bnx2x_storm_stats_post(bp);
533 } 525 }
534
535 bp->stats_started = true;
536}
537
538static void bnx2x_stats_start(struct bnx2x *bp)
539{
540 if (down_timeout(&bp->stats_sema, HZ/10))
541 BNX2X_ERR("Unable to acquire stats lock\n");
542 __bnx2x_stats_start(bp);
543 up(&bp->stats_sema);
544} 526}
545 527
546static void bnx2x_stats_pmf_start(struct bnx2x *bp) 528static void bnx2x_stats_pmf_start(struct bnx2x *bp)
547{ 529{
548 if (down_timeout(&bp->stats_sema, HZ/10))
549 BNX2X_ERR("Unable to acquire stats lock\n");
550 bnx2x_stats_comp(bp); 530 bnx2x_stats_comp(bp);
551 __bnx2x_stats_pmf_update(bp); 531 bnx2x_stats_pmf_update(bp);
552 __bnx2x_stats_start(bp); 532 bnx2x_stats_start(bp);
553 up(&bp->stats_sema);
554}
555
556static void bnx2x_stats_pmf_update(struct bnx2x *bp)
557{
558 if (down_timeout(&bp->stats_sema, HZ/10))
559 BNX2X_ERR("Unable to acquire stats lock\n");
560 __bnx2x_stats_pmf_update(bp);
561 up(&bp->stats_sema);
562} 533}
563 534
564static void bnx2x_stats_restart(struct bnx2x *bp) 535static void bnx2x_stats_restart(struct bnx2x *bp)
@@ -568,11 +539,9 @@ static void bnx2x_stats_restart(struct bnx2x *bp)
568 */ 539 */
569 if (IS_VF(bp)) 540 if (IS_VF(bp))
570 return; 541 return;
571 if (down_timeout(&bp->stats_sema, HZ/10)) 542
572 BNX2X_ERR("Unable to acquire stats lock\n");
573 bnx2x_stats_comp(bp); 543 bnx2x_stats_comp(bp);
574 __bnx2x_stats_start(bp); 544 bnx2x_stats_start(bp);
575 up(&bp->stats_sema);
576} 545}
577 546
578static void bnx2x_bmac_stats_update(struct bnx2x *bp) 547static void bnx2x_bmac_stats_update(struct bnx2x *bp)
@@ -1246,18 +1215,12 @@ static void bnx2x_stats_update(struct bnx2x *bp)
1246{ 1215{
1247 u32 *stats_comp = bnx2x_sp(bp, stats_comp); 1216 u32 *stats_comp = bnx2x_sp(bp, stats_comp);
1248 1217
1249 /* we run update from timer context, so give up 1218 if (bnx2x_edebug_stats_stopped(bp))
1250 * if somebody is in the middle of transition
1251 */
1252 if (down_trylock(&bp->stats_sema))
1253 return; 1219 return;
1254 1220
1255 if (bnx2x_edebug_stats_stopped(bp) || !bp->stats_started)
1256 goto out;
1257
1258 if (IS_PF(bp)) { 1221 if (IS_PF(bp)) {
1259 if (*stats_comp != DMAE_COMP_VAL) 1222 if (*stats_comp != DMAE_COMP_VAL)
1260 goto out; 1223 return;
1261 1224
1262 if (bp->port.pmf) 1225 if (bp->port.pmf)
1263 bnx2x_hw_stats_update(bp); 1226 bnx2x_hw_stats_update(bp);
@@ -1267,7 +1230,7 @@ static void bnx2x_stats_update(struct bnx2x *bp)
1267 BNX2X_ERR("storm stats were not updated for 3 times\n"); 1230 BNX2X_ERR("storm stats were not updated for 3 times\n");
1268 bnx2x_panic(); 1231 bnx2x_panic();
1269 } 1232 }
1270 goto out; 1233 return;
1271 } 1234 }
1272 } else { 1235 } else {
1273 /* vf doesn't collect HW statistics, and doesn't get completions 1236 /* vf doesn't collect HW statistics, and doesn't get completions
@@ -1281,7 +1244,7 @@ static void bnx2x_stats_update(struct bnx2x *bp)
1281 1244
1282 /* vf is done */ 1245 /* vf is done */
1283 if (IS_VF(bp)) 1246 if (IS_VF(bp))
1284 goto out; 1247 return;
1285 1248
1286 if (netif_msg_timer(bp)) { 1249 if (netif_msg_timer(bp)) {
1287 struct bnx2x_eth_stats *estats = &bp->eth_stats; 1250 struct bnx2x_eth_stats *estats = &bp->eth_stats;
@@ -1292,9 +1255,6 @@ static void bnx2x_stats_update(struct bnx2x *bp)
1292 1255
1293 bnx2x_hw_stats_post(bp); 1256 bnx2x_hw_stats_post(bp);
1294 bnx2x_storm_stats_post(bp); 1257 bnx2x_storm_stats_post(bp);
1295
1296out:
1297 up(&bp->stats_sema);
1298} 1258}
1299 1259
1300static void bnx2x_port_stats_stop(struct bnx2x *bp) 1260static void bnx2x_port_stats_stop(struct bnx2x *bp)
@@ -1358,12 +1318,7 @@ static void bnx2x_port_stats_stop(struct bnx2x *bp)
1358 1318
1359static void bnx2x_stats_stop(struct bnx2x *bp) 1319static void bnx2x_stats_stop(struct bnx2x *bp)
1360{ 1320{
1361 int update = 0; 1321 bool update = false;
1362
1363 if (down_timeout(&bp->stats_sema, HZ/10))
1364 BNX2X_ERR("Unable to acquire stats lock\n");
1365
1366 bp->stats_started = false;
1367 1322
1368 bnx2x_stats_comp(bp); 1323 bnx2x_stats_comp(bp);
1369 1324
@@ -1381,8 +1336,6 @@ static void bnx2x_stats_stop(struct bnx2x *bp)
1381 bnx2x_hw_stats_post(bp); 1336 bnx2x_hw_stats_post(bp);
1382 bnx2x_stats_comp(bp); 1337 bnx2x_stats_comp(bp);
1383 } 1338 }
1384
1385 up(&bp->stats_sema);
1386} 1339}
1387 1340
1388static void bnx2x_stats_do_nothing(struct bnx2x *bp) 1341static void bnx2x_stats_do_nothing(struct bnx2x *bp)
@@ -1410,18 +1363,28 @@ static const struct {
1410 1363
1411void bnx2x_stats_handle(struct bnx2x *bp, enum bnx2x_stats_event event) 1364void bnx2x_stats_handle(struct bnx2x *bp, enum bnx2x_stats_event event)
1412{ 1365{
1413 enum bnx2x_stats_state state; 1366 enum bnx2x_stats_state state = bp->stats_state;
1414 void (*action)(struct bnx2x *bp); 1367
1415 if (unlikely(bp->panic)) 1368 if (unlikely(bp->panic))
1416 return; 1369 return;
1417 1370
1418 spin_lock_bh(&bp->stats_lock); 1371 /* Statistics update run from timer context, and we don't want to stop
1419 state = bp->stats_state; 1372 * that context in case someone is in the middle of a transition.
1373 * For other events, wait a bit until lock is taken.
1374 */
1375 if (!mutex_trylock(&bp->stats_lock)) {
1376 if (event == STATS_EVENT_UPDATE)
1377 return;
1378
1379 DP(BNX2X_MSG_STATS,
1380 "Unlikely stats' lock contention [event %d]\n", event);
1381 mutex_lock(&bp->stats_lock);
1382 }
1383
1384 bnx2x_stats_stm[state][event].action(bp);
1420 bp->stats_state = bnx2x_stats_stm[state][event].next_state; 1385 bp->stats_state = bnx2x_stats_stm[state][event].next_state;
1421 action = bnx2x_stats_stm[state][event].action;
1422 spin_unlock_bh(&bp->stats_lock);
1423 1386
1424 action(bp); 1387 mutex_unlock(&bp->stats_lock);
1425 1388
1426 if ((event != STATS_EVENT_UPDATE) || netif_msg_timer(bp)) 1389 if ((event != STATS_EVENT_UPDATE) || netif_msg_timer(bp))
1427 DP(BNX2X_MSG_STATS, "state %d -> event %d -> state %d\n", 1390 DP(BNX2X_MSG_STATS, "state %d -> event %d -> state %d\n",
@@ -1998,13 +1961,34 @@ void bnx2x_afex_collect_stats(struct bnx2x *bp, void *void_afex_stats,
1998 } 1961 }
1999} 1962}
2000 1963
2001void bnx2x_stats_safe_exec(struct bnx2x *bp, 1964int bnx2x_stats_safe_exec(struct bnx2x *bp,
2002 void (func_to_exec)(void *cookie), 1965 void (func_to_exec)(void *cookie),
2003 void *cookie){ 1966 void *cookie)
2004 if (down_timeout(&bp->stats_sema, HZ/10)) 1967{
2005 BNX2X_ERR("Unable to acquire stats lock\n"); 1968 int cnt = 10, rc = 0;
1969
1970 /* Wait for statistics to end [while blocking further requests],
1971 * then run supplied function 'safely'.
1972 */
1973 mutex_lock(&bp->stats_lock);
1974
2006 bnx2x_stats_comp(bp); 1975 bnx2x_stats_comp(bp);
1976 while (bp->stats_pending && cnt--)
1977 if (bnx2x_storm_stats_update(bp))
1978 usleep_range(1000, 2000);
1979 if (bp->stats_pending) {
1980 BNX2X_ERR("Failed to wait for stats pending to clear [possibly FW is stuck]\n");
1981 rc = -EBUSY;
1982 goto out;
1983 }
1984
2007 func_to_exec(cookie); 1985 func_to_exec(cookie);
2008 __bnx2x_stats_start(bp); 1986
2009 up(&bp->stats_sema); 1987out:
1988 /* No need to restart statistics - if they're enabled, the timer
1989 * will restart the statistics.
1990 */
1991 mutex_unlock(&bp->stats_lock);
1992
1993 return rc;
2010} 1994}
diff --git a/drivers/net/ethernet/broadcom/bnx2x/bnx2x_stats.h b/drivers/net/ethernet/broadcom/bnx2x/bnx2x_stats.h
index 2beceaefdeea..965539a9dabe 100644
--- a/drivers/net/ethernet/broadcom/bnx2x/bnx2x_stats.h
+++ b/drivers/net/ethernet/broadcom/bnx2x/bnx2x_stats.h
@@ -539,9 +539,9 @@ struct bnx2x;
539void bnx2x_memset_stats(struct bnx2x *bp); 539void bnx2x_memset_stats(struct bnx2x *bp);
540void bnx2x_stats_init(struct bnx2x *bp); 540void bnx2x_stats_init(struct bnx2x *bp);
541void bnx2x_stats_handle(struct bnx2x *bp, enum bnx2x_stats_event event); 541void bnx2x_stats_handle(struct bnx2x *bp, enum bnx2x_stats_event event);
542void bnx2x_stats_safe_exec(struct bnx2x *bp, 542int bnx2x_stats_safe_exec(struct bnx2x *bp,
543 void (func_to_exec)(void *cookie), 543 void (func_to_exec)(void *cookie),
544 void *cookie); 544 void *cookie);
545 545
546/** 546/**
547 * bnx2x_save_statistics - save statistics when unloading. 547 * bnx2x_save_statistics - save statistics when unloading.