Skip to content

Commit fb9840c

Browse files
pkitszelanguy11
authored andcommitted
ice: split ice_aq_wait_for_event() func into two
Mitigate race between registering on wait list and receiving AQ Response from FW. ice_aq_prep_for_event() should be called before sending AQ command, ice_aq_wait_for_event() should be called after sending AQ command, to wait for AQ Response. Please note, that this was found by reading the code, an actual race has not yet materialized. Reviewed-by: Jacob Keller <[email protected]> Signed-off-by: Przemek Kitszel <[email protected]> Reviewed-by: Simon Horman <[email protected]> Tested-by: Pucha Himasekhar Reddy <[email protected]> (A Contingent worker at Intel) Reviewed-by: Leon Romanovsky <[email protected]> Signed-off-by: Tony Nguyen <[email protected]>
1 parent b214b98 commit fb9840c

File tree

3 files changed

+57
-28
lines changed

3 files changed

+57
-28
lines changed

drivers/net/ethernet/intel/ice/ice.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -919,6 +919,7 @@ void ice_fdir_replay_fltrs(struct ice_pf *pf);
919919
int ice_fdir_create_dflt_rules(struct ice_pf *pf);
920920

921921
enum ice_aq_task_state {
922+
ICE_AQ_TASK_NOT_PREPARED,
922923
ICE_AQ_TASK_WAITING,
923924
ICE_AQ_TASK_COMPLETE,
924925
ICE_AQ_TASK_CANCELED,
@@ -931,8 +932,10 @@ struct ice_aq_task {
931932
u16 opcode;
932933
};
933934

935+
void ice_aq_prep_for_event(struct ice_pf *pf, struct ice_aq_task *task,
936+
u16 opcode);
934937
int ice_aq_wait_for_event(struct ice_pf *pf, struct ice_aq_task *task,
935-
u16 opcode, unsigned long timeout);
938+
unsigned long timeout);
936939
int ice_open(struct net_device *netdev);
937940
int ice_open_internal(struct net_device *netdev);
938941
int ice_stop(struct net_device *netdev);

drivers/net/ethernet/intel/ice/ice_fw_update.c

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,8 @@ ice_write_one_nvm_block(struct ice_pf *pf, u16 module, u32 offset,
302302
dev_dbg(dev, "Writing block of %u bytes for module 0x%02x at offset %u\n",
303303
block_size, module, offset);
304304

305+
ice_aq_prep_for_event(pf, &task, ice_aqc_opc_nvm_write);
306+
305307
err = ice_aq_update_nvm(hw, module, offset, block_size, block,
306308
last_cmd, 0, NULL);
307309
if (err) {
@@ -318,7 +320,7 @@ ice_write_one_nvm_block(struct ice_pf *pf, u16 module, u32 offset,
318320
* is conservative and is intended to prevent failure to update when
319321
* firmware is slow to respond.
320322
*/
321-
err = ice_aq_wait_for_event(pf, &task, ice_aqc_opc_nvm_write, 15 * HZ);
323+
err = ice_aq_wait_for_event(pf, &task, 15 * HZ);
322324
if (err) {
323325
dev_err(dev, "Timed out while trying to flash module 0x%02x with block of size %u at offset %u, err %d\n",
324326
module, block_size, offset, err);
@@ -491,6 +493,8 @@ ice_erase_nvm_module(struct ice_pf *pf, u16 module, const char *component,
491493

492494
devlink_flash_update_timeout_notify(devlink, "Erasing", component, ICE_FW_ERASE_TIMEOUT);
493495

496+
ice_aq_prep_for_event(pf, &task, ice_aqc_opc_nvm_erase);
497+
494498
err = ice_aq_erase_nvm(hw, module, NULL);
495499
if (err) {
496500
dev_err(dev, "Failed to erase %s (module 0x%02x), err %d aq_err %s\n",
@@ -501,7 +505,7 @@ ice_erase_nvm_module(struct ice_pf *pf, u16 module, const char *component,
501505
goto out_notify_devlink;
502506
}
503507

504-
err = ice_aq_wait_for_event(pf, &task, ice_aqc_opc_nvm_erase, ICE_FW_ERASE_TIMEOUT * HZ);
508+
err = ice_aq_wait_for_event(pf, &task, ICE_FW_ERASE_TIMEOUT * HZ);
505509
if (err) {
506510
dev_err(dev, "Timed out waiting for firmware to respond with erase completion for %s (module 0x%02x), err %d\n",
507511
component, module, err);
@@ -566,6 +570,8 @@ ice_switch_flash_banks(struct ice_pf *pf, u8 activate_flags,
566570
u8 response_flags;
567571
int err;
568572

573+
ice_aq_prep_for_event(pf, &task, ice_aqc_opc_nvm_write_activate);
574+
569575
err = ice_nvm_write_activate(hw, activate_flags, &response_flags);
570576
if (err) {
571577
dev_err(dev, "Failed to switch active flash banks, err %d aq_err %s\n",
@@ -590,8 +596,7 @@ ice_switch_flash_banks(struct ice_pf *pf, u8 activate_flags,
590596
}
591597
}
592598

593-
err = ice_aq_wait_for_event(pf, &task, ice_aqc_opc_nvm_write_activate,
594-
30 * HZ);
599+
err = ice_aq_wait_for_event(pf, &task, 30 * HZ);
595600
if (err) {
596601
dev_err(dev, "Timed out waiting for firmware to switch active flash banks, err %d\n",
597602
err);

drivers/net/ethernet/intel/ice/ice_main.c

Lines changed: 44 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1251,43 +1251,62 @@ ice_handle_link_event(struct ice_pf *pf, struct ice_rq_event_info *event)
12511251
}
12521252

12531253
/**
1254-
* ice_aq_wait_for_event - Wait for an AdminQ event from firmware
1254+
* ice_aq_prep_for_event - Prepare to wait for an AdminQ event from firmware
12551255
* @pf: pointer to the PF private structure
1256-
* @task: ptr to task structure
1256+
* @task: intermediate helper storage and identifier for waiting
12571257
* @opcode: the opcode to wait for
1258-
* @timeout: how long to wait, in jiffies
12591258
*
1260-
* Waits for a specific AdminQ completion event on the ARQ for a given PF. The
1261-
* current thread will be put to sleep until the specified event occurs or
1262-
* until the given timeout is reached.
1259+
* Prepares to wait for a specific AdminQ completion event on the ARQ for
1260+
* a given PF. Actual wait would be done by a call to ice_aq_wait_for_event().
12631261
*
1264-
* To obtain only the descriptor contents, pass an event without an allocated
1265-
* msg_buf. If the complete data buffer is desired, allocate the
1266-
* event->msg_buf with enough space ahead of time.
1262+
* Calls are separated to allow caller registering for event before sending
1263+
* the command, which mitigates a race between registering and FW responding.
12671264
*
1268-
* Returns: zero on success, or a negative error code on failure.
1265+
* To obtain only the descriptor contents, pass an task->event with null
1266+
* msg_buf. If the complete data buffer is desired, allocate the
1267+
* task->event.msg_buf with enough space ahead of time.
12691268
*/
1270-
int ice_aq_wait_for_event(struct ice_pf *pf, struct ice_aq_task *task,
1271-
u16 opcode, unsigned long timeout)
1269+
void ice_aq_prep_for_event(struct ice_pf *pf, struct ice_aq_task *task,
1270+
u16 opcode)
12721271
{
1273-
struct device *dev = ice_pf_to_dev(pf);
1274-
unsigned long start;
1275-
long ret;
1276-
int err;
1277-
12781272
INIT_HLIST_NODE(&task->entry);
12791273
task->opcode = opcode;
12801274
task->state = ICE_AQ_TASK_WAITING;
12811275

12821276
spin_lock_bh(&pf->aq_wait_lock);
12831277
hlist_add_head(&task->entry, &pf->aq_wait_list);
12841278
spin_unlock_bh(&pf->aq_wait_lock);
1279+
}
12851280

1286-
start = jiffies;
1281+
/**
1282+
* ice_aq_wait_for_event - Wait for an AdminQ event from firmware
1283+
* @pf: pointer to the PF private structure
1284+
* @task: ptr prepared by ice_aq_prep_for_event()
1285+
* @timeout: how long to wait, in jiffies
1286+
*
1287+
* Waits for a specific AdminQ completion event on the ARQ for a given PF. The
1288+
* current thread will be put to sleep until the specified event occurs or
1289+
* until the given timeout is reached.
1290+
*
1291+
* Returns: zero on success, or a negative error code on failure.
1292+
*/
1293+
int ice_aq_wait_for_event(struct ice_pf *pf, struct ice_aq_task *task,
1294+
unsigned long timeout)
1295+
{
1296+
enum ice_aq_task_state *state = &task->state;
1297+
struct device *dev = ice_pf_to_dev(pf);
1298+
unsigned long start = jiffies;
1299+
long ret;
1300+
int err;
12871301

1288-
ret = wait_event_interruptible_timeout(pf->aq_wait_queue, task->state,
1302+
ret = wait_event_interruptible_timeout(pf->aq_wait_queue,
1303+
*state != ICE_AQ_TASK_WAITING,
12891304
timeout);
1290-
switch (task->state) {
1305+
switch (*state) {
1306+
case ICE_AQ_TASK_NOT_PREPARED:
1307+
WARN(1, "call to %s without ice_aq_prep_for_event()", __func__);
1308+
err = -EINVAL;
1309+
break;
12911310
case ICE_AQ_TASK_WAITING:
12921311
err = ret < 0 ? ret : -ETIMEDOUT;
12931312
break;
@@ -1298,15 +1317,15 @@ int ice_aq_wait_for_event(struct ice_pf *pf, struct ice_aq_task *task,
12981317
err = ret < 0 ? ret : 0;
12991318
break;
13001319
default:
1301-
WARN(1, "Unexpected AdminQ wait task state %u", task->state);
1320+
WARN(1, "Unexpected AdminQ wait task state %u", *state);
13021321
err = -EINVAL;
13031322
break;
13041323
}
13051324

13061325
dev_dbg(dev, "Waited %u msecs (max %u msecs) for firmware response to op 0x%04x\n",
13071326
jiffies_to_msecs(jiffies - start),
13081327
jiffies_to_msecs(timeout),
1309-
opcode);
1328+
task->opcode);
13101329

13111330
spin_lock_bh(&pf->aq_wait_lock);
13121331
hlist_del(&task->entry);
@@ -1342,7 +1361,9 @@ static void ice_aq_check_events(struct ice_pf *pf, u16 opcode,
13421361

13431362
spin_lock_bh(&pf->aq_wait_lock);
13441363
hlist_for_each_entry(task, &pf->aq_wait_list, entry) {
1345-
if (task->state || task->opcode != opcode)
1364+
if (task->state != ICE_AQ_TASK_WAITING)
1365+
continue;
1366+
if (task->opcode != opcode)
13461367
continue;
13471368

13481369
task_ev = &task->event;

0 commit comments

Comments
 (0)