Skip to content

Commit f262f5d

Browse files
WeiFang-NXPkuba-moo
authored andcommitted
net: enetc: fix race condition in VF MAC address configuration
Sashiko reported a potential race condition between the VF message handler and administrative VF MAC configuration from the host [1]. The VF message handler (enetc_msg_pf_set_vf_primary_mac_addr) runs asynchronously in a workqueue context and accesses vf_state->flags without any locking. Concurrently, the host can administratively change the VF MAC address via enetc_pf_set_vf_mac(), which executes under RTNL lock and modifies both vf_state->flags and hardware registers. This creates two race windows: 1) TOCTOU race on vf_state->flags: The check of ENETC_VF_FLAG_PF_SET_MAC and subsequent MAC programming are not atomic, allowing the flag state to change between check and use. 2) Torn MAC address writes: Hardware MAC programming requires multiple non-atomic register writes (__raw_writel for lower 32 bits and __raw_writew for upper 16 bits). Concurrent updates from VF mailbox and PF admin paths can interleave these operations, resulting in a corrupted MAC address being programmed into the hardware. Fix by introducing a per-VF mutex to serialize access to vf_state and hardware MAC register updates. Both enetc_pf_set_vf_mac() and enetc_msg_pf_set_vf_primary_mac_addr() now acquire this lock before accessing vf_state->flags or programming the MAC address, ensuring atomic read-modify-write sequences and preventing register write interleaving. Link: https://sashiko.dev/#/patchset/20260511080805.2052495-1-wei.fang%40nxp.com #1 Fixes: beb74ac ("enetc: Add vf to pf messaging support") Signed-off-by: Wei Fang <wei.fang@nxp.com> Reviewed-by: Harshitha Ramamurthy <hramamurthy@google.com> Link: https://patch.msgid.link/20260520064421.91569-6-wei.fang@nxp.com Signed-off-by: Jakub Kicinski <kuba@kernel.org>
1 parent c666fa6 commit f262f5d

2 files changed

Lines changed: 11 additions & 0 deletions

File tree

drivers/net/ethernet/freescale/enetc/enetc_pf.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,8 +252,12 @@ static int enetc_pf_set_vf_mac(struct net_device *ndev, int vf, u8 *mac)
252252
return -EADDRNOTAVAIL;
253253

254254
vf_state = &pf->vf_state[vf];
255+
256+
mutex_lock(&vf_state->lock);
255257
vf_state->flags |= ENETC_VF_FLAG_PF_SET_MAC;
256258
enetc_pf_set_primary_mac_addr(&priv->si->hw, vf + 1, mac);
259+
mutex_unlock(&vf_state->lock);
260+
257261
return 0;
258262
}
259263

@@ -496,14 +500,17 @@ static u16 enetc_msg_pf_set_vf_primary_mac_addr(struct enetc_pf *pf,
496500
return ENETC_MSG_CMD_STATUS_FAIL;
497501
}
498502

503+
mutex_lock(&vf_state->lock);
499504
if (vf_state->flags & ENETC_VF_FLAG_PF_SET_MAC) {
505+
mutex_unlock(&vf_state->lock);
500506
dev_err_ratelimited(dev,
501507
"VF%d attempted to override PF set MAC\n",
502508
vf_id);
503509
return ENETC_MSG_CMD_STATUS_FAIL;
504510
}
505511

506512
enetc_pf_set_primary_mac_addr(&pf->si->hw, vf_id + 1, addr);
513+
mutex_unlock(&vf_state->lock);
507514

508515
return ENETC_MSG_CMD_STATUS_OK;
509516
}
@@ -989,6 +996,9 @@ static int enetc_pf_probe(struct pci_dev *pdev,
989996
err = -ENOMEM;
990997
goto err_alloc_vf_state;
991998
}
999+
1000+
for (int i = 0; i < pf->total_vfs; i++)
1001+
mutex_init(&pf->vf_state[i].lock);
9921002
}
9931003

9941004
err = enetc_setup_mac_addresses(node, pf);

drivers/net/ethernet/freescale/enetc/enetc_pf.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ enum enetc_vf_flags {
1414
};
1515

1616
struct enetc_vf_state {
17+
struct mutex lock; /* Prevent concurrent access */
1718
enum enetc_vf_flags flags;
1819
};
1920

0 commit comments

Comments
 (0)