Skip to content
This repository was archived by the owner on Nov 5, 2024. It is now read-only.

Commit 54cbafb

Browse files
committed
Synchronize access to soc_tomahawk_reconfigure_ports() function
This fixes a data race when user code reconfigures the same port from two threads at the same time.
1 parent 8cd5d01 commit 54cbafb

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

src/bcmtm/flexport/main/flexport_top.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@
9494

9595
#include <bsl/bsl.h>
9696
#include <sal/sal_sleep.h>
97+
#include <sal/sal_mutex.h>
9798

9899
#include <bcmtm/bcmtm_types.h>
99100

src/bcmtm/flexport/tomahawk/src/tomahawk_flexport_top.c

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,6 @@ int (*flexport_phases[MAX_FLEX_PHASES]) (int unit, soc_port_schedule_state_t
151151
NULL /* 24 */
152152
};
153153

154-
155154
/*! @fn void *soc_tomahawk_port_lane_info_alloc(void)
156155
* @brief Allocates memory pointed to by soc_port_lane_info_t. Need this
157156
* special function because SystemVerilog-C DPI limitations in
@@ -248,6 +247,20 @@ soc_tomahawk_reconfigure_ports (
248247
)
249248
{
250249
int i, j, rv;
250+
static sal_mutex_t mtx = NULL;
251+
252+
// Only runs once
253+
// TODO: make init thread-safe
254+
if (!mtx) {
255+
mtx = sal_mutex_create("Tomahawk port reconfiguration mutex");
256+
}
257+
258+
if (sal_mutex_take(mtx, 1000 * 1000 /* us */)) {
259+
LOG_ERROR(BSL_LS_SOC_PORT,
260+
(BSL_META_U(unit, "Port reconfiguration mutex could not be"
261+
" taken within timeout. Deadlock?\n")));
262+
return SOC_E_FAIL;
263+
}
251264

252265
if (LOG_CHECK(BSL_LS_SOC_PORT)) {
253266
LOG_DEBUG(BSL_LS_SOC_PORT,
@@ -303,13 +316,16 @@ soc_tomahawk_reconfigure_ports (
303316
rv = flexport_phases[i](unit, port_schedule_state);
304317
if (rv != SOC_E_NONE) {
305318
sal_free(port_schedule_state->cookie);
319+
port_schedule_state->cookie = NULL;
306320
LOG_DEBUG(BSL_LS_SOC_PORT, (BSL_META_U(unit,
307321
"Error encountered. Cookie space deallocated.\n")));
322+
sal_mutex_give(mtx);
308323
return rv;
309324
}
310325
}
311326
}
312327

328+
sal_mutex_give(mtx);
313329
return SOC_E_NONE;
314330
}
315331

0 commit comments

Comments
 (0)