Skip to content

Commit deedf1f

Browse files
thierryredingdavem330
authored andcommitted
r8169: Avoid pointer aliasing
Read MAC address 32-bit at a time and manually extract the individual bytes. This avoids pointer aliasing and gives the compiler a better chance of optimizing the operation. Suggested-by: Andrew Lunn <andrew@lunn.ch> Signed-off-by: Thierry Reding <treding@nvidia.com> Reviewed-by: Andrew Lunn <andrew@lunn.ch> Signed-off-by: David S. Miller <davem@davemloft.net>
1 parent 04c7788 commit deedf1f

File tree

1 file changed

+13
-3
lines changed
  • drivers/net/ethernet/realtek

1 file changed

+13
-3
lines changed

drivers/net/ethernet/realtek/r8169.c

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7113,12 +7113,21 @@ static int rtl_alloc_irq(struct rtl8169_private *tp)
71137113
static void rtl_read_mac_address(struct rtl8169_private *tp,
71147114
u8 mac_addr[ETH_ALEN])
71157115
{
7116+
u32 value;
7117+
71167118
/* Get MAC address */
71177119
switch (tp->mac_version) {
71187120
case RTL_GIGA_MAC_VER_35 ... RTL_GIGA_MAC_VER_38:
71197121
case RTL_GIGA_MAC_VER_40 ... RTL_GIGA_MAC_VER_51:
7120-
*(u32 *)&mac_addr[0] = rtl_eri_read(tp, 0xe0, ERIAR_EXGMAC);
7121-
*(u16 *)&mac_addr[4] = rtl_eri_read(tp, 0xe4, ERIAR_EXGMAC);
7122+
value = rtl_eri_read(tp, 0xe0, ERIAR_EXGMAC);
7123+
mac_addr[0] = (value >> 0) & 0xff;
7124+
mac_addr[1] = (value >> 8) & 0xff;
7125+
mac_addr[2] = (value >> 16) & 0xff;
7126+
mac_addr[3] = (value >> 24) & 0xff;
7127+
7128+
value = rtl_eri_read(tp, 0xe4, ERIAR_EXGMAC);
7129+
mac_addr[4] = (value >> 0) & 0xff;
7130+
mac_addr[5] = (value >> 8) & 0xff;
71227131
break;
71237132
default:
71247133
break;
@@ -7316,7 +7325,8 @@ static int rtl_get_ether_clk(struct rtl8169_private *tp)
73167325
static int rtl_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
73177326
{
73187327
const struct rtl_cfg_info *cfg = rtl_cfg_infos + ent->driver_data;
7319-
u8 mac_addr[ETH_ALEN] __aligned(4) = {};
7328+
/* align to u16 for is_valid_ether_addr() */
7329+
u8 mac_addr[ETH_ALEN] __aligned(2) = {};
73207330
struct rtl8169_private *tp;
73217331
struct net_device *dev;
73227332
int chipset, region, i;

0 commit comments

Comments
 (0)