Skip to contentSkip to conversation

staging: wlan-ng: replace memcpy with ether_addr_copy

PATCH In review Ioana Ciornei 2 participants 4 messages Oct 2, 2015 – Oct 6, 2015 Staging

lore
Share & tools
lore
#0 lore raw

PATCH v4 1/1 staging: wlan-ng: replace memcpy with ether_addr_copy

Replace memcpy with ether_addr_copy since the output of pahole shows that the addresses are aligned.

struct p80211_hdr_a3 {

        __le16                     fc;                   /*     0     2 */
        u16                        dur;                  /*     2     2 */
        u8                         a1[6];                /*     4     6 */
        u8                         a2[6];                /*    10     6 */
        u8                         a3[6];                /*    16     6 */
        u16                        seq;                  /*    22     2 */
        /* size: 24, cachelines: 1, members: 6 */
        /* last cacheline: 24 bytes */
};

struct net_device {

        char                       name[16];             /*     0    16 */
        struct hlist_node          name_hlist;           /*    16    16 */
     ...............

/* --- cacheline 12 boundary (768 bytes) --- */

        long unsigned int          last_rx;              /*   768     8 */
        unsigned char *            dev_addr;             /*   776     8 */
        struct netdev_rx_queue *   _rx;                  /*   784     8 */
    .................
};

struct wlandevice {
struct wlandevice * next; /* 0 8 */
.................
u8 bssid[6]; /* 128 6 */
.................
};

struct net_device {
char name[16]; /* 0 16 */
.................
unsigned char * dev_addr; /* 776 8 */
................
};

struct wlan_ethhdr {

        u8                         daddr[6];             /*     0     6 */
        u8                         saddr[6];             /*     6     6 */
        u16                        type;                 /*    12     2 */
        /* size: 14, cachelines: 1, members: 3 */
        /* last cacheline: 14 bytes */
};

struct sockaddr {

        sa_family_t                sa_family;            /*     0     2 */
        char                       sa_data[14];          /*     2    14 */
        /* size: 16, cachelines: 1, members: 2 */
        /* last cacheline: 16 bytes */
};

struct p80211pstr6 {

        u8                         len;                  /*     0     1 */
        u8                         data[6];              /*     1     6 */
        /* size: 7, cachelines: 1, members: 2 */
        /* last cacheline: 7 bytes */
};

Coccinelle semantic patch:

@@
expression E1,E2;
@@

- memcpy(E1, E2, 6)
+ ether_addr_copy(E1, E2)

@@
expression E1,E2;
@@

- memcpy(E1, E2, ETH_ALEN)
+ ether_addr_copy(E1, E2)

---
Changes in v2:
- revised the cocci patch to be a more constrainting one
- applied cocci patch on the entire wlan-ng folder
- updated the patch description with further pahole output

Changes in v3:
- make an explicit cast from u8 (*)[6] to const u8 * to avoid compile warning

Changes in v4:
- fix compile and checkpatch warnings introduced in v3

 drivers/staging/wlan-ng/p80211conv.c   | 20 ++++++++++----------
 drivers/staging/wlan-ng/p80211netdev.c |  2 +-
 2 files changed, 11 insertions(+), 11 deletions(-)
drivers/staging/wlan-ng/p80211conv.c +10 -10
index 1b02cdf..87d323a 100644
@@ -178,21 +178,21 @@ int skb_ether_to_p80211(wlandevice_t *wlandev, u32 ethconv,
 
 	switch (wlandev->macmode) {
 	case WLAN_MACMODE_IBSS_STA:
-		memcpy(p80211_hdr->a3.a1, &e_hdr.daddr, ETH_ALEN);
-		memcpy(p80211_hdr->a3.a2, wlandev->netdev->dev_addr, ETH_ALEN);
-		memcpy(p80211_hdr->a3.a3, wlandev->bssid, ETH_ALEN);
+		ether_addr_copy(p80211_hdr->a3.a1, (const u8 *)&e_hdr.daddr);
+		ether_addr_copy(p80211_hdr->a3.a2, wlandev->netdev->dev_addr);
+		ether_addr_copy(p80211_hdr->a3.a3, wlandev->bssid);
 		break;
 	case WLAN_MACMODE_ESS_STA:
 		fc |= cpu_to_le16(WLAN_SET_FC_TODS(1));
-		memcpy(p80211_hdr->a3.a1, wlandev->bssid, ETH_ALEN);
-		memcpy(p80211_hdr->a3.a2, wlandev->netdev->dev_addr, ETH_ALEN);
-		memcpy(p80211_hdr->a3.a3, &e_hdr.daddr, ETH_ALEN);
+		ether_addr_copy(p80211_hdr->a3.a1, wlandev->bssid);
+		ether_addr_copy(p80211_hdr->a3.a2, wlandev->netdev->dev_addr);
+		ether_addr_copy(p80211_hdr->a3.a3, (const u8 *)&e_hdr.daddr);
 		break;
 	case WLAN_MACMODE_ESS_AP:
 		fc |= cpu_to_le16(WLAN_SET_FC_FROMDS(1));
-		memcpy(p80211_hdr->a3.a1, &e_hdr.daddr, ETH_ALEN);
-		memcpy(p80211_hdr->a3.a2, wlandev->bssid, ETH_ALEN);
-		memcpy(p80211_hdr->a3.a3, &e_hdr.saddr, ETH_ALEN);
+		ether_addr_copy(p80211_hdr->a3.a1, (const u8 *)&e_hdr.daddr);
+		ether_addr_copy(p80211_hdr->a3.a2, wlandev->bssid);
+		ether_addr_copy(p80211_hdr->a3.a3, (const u8 *)&e_hdr.saddr);
 		break;
 	default:
 		netdev_err(wlandev->netdev,
@@ -243,7 +243,7 @@ static void orinoco_spy_gather(wlandevice_t *wlandev, char *mac,
 	for (i = 0; i < wlandev->spy_number; i++) {
 
 		if (!memcmp(wlandev->spy_address[i], mac, ETH_ALEN)) {
-			memcpy(wlandev->spy_address[i], mac, ETH_ALEN);
+			ether_addr_copy(wlandev->spy_address[i], mac);
 			wlandev->spy_stat[i].level = rxmeta->signal;
 			wlandev->spy_stat[i].noise = rxmeta->noise;
 			wlandev->spy_stat[i].qual =
drivers/staging/wlan-ng/p80211netdev.c +1 -1
index a9c1e0b..14e3418 100644
@@ -644,7 +644,7 @@ static int p80211knetdev_set_mac_address(netdevice_t *dev, void *addr)
 	macaddr->status = P80211ENUM_msgitem_status_data_ok;
 	macaddr->len = sizeof(macaddr->data);
 	macaddr->data.len = ETH_ALEN;
-	memcpy(&macaddr->data.data, new_addr->sa_data, ETH_ALEN);
+	ether_addr_copy((u8 *)&macaddr->data.data, new_addr->sa_data);
 
 	/* Set up the resultcode argument */
 	resultcode->did = DIDmsg_dot11req_mibset_resultcode;
Signature · 1 line
2.1.4
maintainer
#1 lore raw
Quoted 106 lines · Ioana Ciornei

Replace memcpy with ether_addr_copy since the output of pahole shows that the addresses are aligned.

struct p80211_hdr_a3 {

        __le16                     fc;                   /*     0     2 */
        u16                        dur;                  /*     2     2 */
        u8                         a1[6];                /*     4     6 */
        u8                         a2[6];                /*    10     6 */
        u8                         a3[6];                /*    16     6 */
        u16                        seq;                  /*    22     2 */
        /* size: 24, cachelines: 1, members: 6 */
        /* last cacheline: 24 bytes */
};

struct net_device {

        char                       name[16];             /*     0    16 */
        struct hlist_node          name_hlist;           /*    16    16 */
     ...............

/* --- cacheline 12 boundary (768 bytes) --- */

        long unsigned int          last_rx;              /*   768     8 */
        unsigned char *            dev_addr;             /*   776     8 */
        struct netdev_rx_queue *   _rx;                  /*   784     8 */
    .................
};

struct wlandevice {
struct wlandevice * next; /* 0 8 */
.................
u8 bssid[6]; /* 128 6 */
.................
};

struct net_device {
char name[16]; /* 0 16 */
.................
unsigned char * dev_addr; /* 776 8 */
................
};

struct wlan_ethhdr {

        u8                         daddr[6];             /*     0     6 */
        u8                         saddr[6];             /*     6     6 */
        u16                        type;                 /*    12     2 */
        /* size: 14, cachelines: 1, members: 3 */
        /* last cacheline: 14 bytes */
};

struct sockaddr {

        sa_family_t                sa_family;            /*     0     2 */
        char                       sa_data[14];          /*     2    14 */
        /* size: 16, cachelines: 1, members: 2 */
        /* last cacheline: 16 bytes */
};

struct p80211pstr6 {

        u8                         len;                  /*     0     1 */
        u8                         data[6];              /*     1     6 */
        /* size: 7, cachelines: 1, members: 2 */
        /* last cacheline: 7 bytes */
};

Coccinelle semantic patch:

@@
expression E1,E2;
@@

- memcpy(E1, E2, 6)
+ ether_addr_copy(E1, E2)

@@
expression E1,E2;
@@

- memcpy(E1, E2, ETH_ALEN)
+ ether_addr_copy(E1, E2)

---
Changes in v2:
- revised the cocci patch to be a more constrainting one
- applied cocci patch on the entire wlan-ng folder
- updated the patch description with further pahole output

Changes in v3:
- make an explicit cast from u8 (*)[6] to const u8 * to avoid compile warning

Changes in v4:
- fix compile and checkpatch warnings introduced in v3

 drivers/staging/wlan-ng/p80211conv.c   | 20 ++++++++++----------
 drivers/staging/wlan-ng/p80211netdev.c |  2 +-
 2 files changed, 11 insertions(+), 11 deletions(-)
drivers/staging/wlan-ng/p80211conv.c +0 -0
index 1b02cdf..87d323a 100644

drivers/staging/wlan-ng/p80211conv.c int skb_ether_to_p80211(wlandevice_t *wlandev, u32 ethconv, · line 181

Quoted 7 lines
drivers/staging/wlan-ng/p80211conv.c +1 -3
@@ -178,21 +178,21 @@ int skb_ether_to_p80211(wlandevice_t *wlandev, u32 ethconv,
 
 	switch (wlandev->macmode) {
 	case WLAN_MACMODE_IBSS_STA:
-		memcpy(p80211_hdr->a3.a1, &e_hdr.daddr, ETH_ALEN);
-		memcpy(p80211_hdr->a3.a2, wlandev->netdev->dev_addr, ETH_ALEN);
-		memcpy(p80211_hdr->a3.a3, wlandev->bssid, ETH_ALEN);
+		ether_addr_copy(p80211_hdr->a3.a1, (const u8 *)&e_hdr.daddr);

When you have to add a cast like this, it's a good idea to try to figure out _why_ that cast is needed.

I think you should just make the struct wlan_ethhdr daddr field be const and then you don't need to make any casts when you convert to ether_addr_copy().

So can you turn this into a two patch series? The first one change the structure variable type, and the second one this ether_addr_copy() change?

thanks,

greg k-h

#2 lore raw
Quoted 6 lines · Greg KH
Quoted 5 lines · Ioana Ciornei

Replace memcpy with ether_addr_copy since the output of pahole shows that the addresses are aligned.

struct p80211_hdr_a3 {
__le16 fc; /* 0 2

*/

Quoted 1 line
Quoted 1 line

u16 dur; /* 2 2

*/

Quoted 1 line
Quoted 1 line

u8 a1[6]; /* 4 6

*/

Quoted 1 line
Quoted 1 line

u8 a2[6]; /* 10 6

*/

Quoted 1 line
Quoted 1 line

u8 a3[6]; /* 16 6

*/

Quoted 1 line
Quoted 1 line

u16 seq; /* 22 2

*/

Quoted 7 lines
Quoted 7 lines
        /* size: 24, cachelines: 1, members: 6 */
        /* last cacheline: 24 bytes */
};

struct net_device {
char name[16]; /* 0 16

*/

Quoted 1 line
Quoted 1 line

struct hlist_node name_hlist; /* 16 16

*/

Quoted 5 lines
Quoted 5 lines
     ...............

/* --- cacheline 12 boundary (768 bytes) --- */
long unsigned int last_rx; /* 768 8

*/

Quoted 1 line
Quoted 1 line

unsigned char * dev_addr; /* 776 8

*/

Quoted 1 line
Quoted 1 line

struct netdev_rx_queue * _rx; /* 784 8

*/

Quoted 6 lines
Quoted 6 lines
    .................
};

struct wlandevice {
struct wlandevice * next; /* 0 8

*/

Quoted 2 lines
Quoted 2 lines

.................
u8 bssid[6]; /* 128 6

*/

Quoted 5 lines
Quoted 5 lines
    .................
};

struct net_device {
char name[16]; /* 0 16

*/

Quoted 2 lines
Quoted 2 lines

.................
unsigned char * dev_addr; /* 776 8

*/

Quoted 5 lines
Quoted 5 lines
    ................
};

struct wlan_ethhdr {
u8 daddr[6]; /* 0 6

*/

Quoted 1 line
Quoted 1 line

u8 saddr[6]; /* 6 6

*/

Quoted 1 line
Quoted 1 line

u16 type; /* 12 2

*/

Quoted 7 lines
Quoted 7 lines
        /* size: 14, cachelines: 1, members: 3 */
        /* last cacheline: 14 bytes */
};

struct sockaddr {
sa_family_t sa_family; /* 0 2

*/

Quoted 1 line
Quoted 1 line

char sa_data[14]; /* 2 14

*/

Quoted 7 lines
Quoted 7 lines
        /* size: 16, cachelines: 1, members: 2 */
        /* last cacheline: 16 bytes */
};

struct p80211pstr6 {
u8 len; /* 0 1

*/

Quoted 1 line
Quoted 1 line

u8 data[6]; /* 1 6

*/

Quoted 28 lines
Quoted 28 lines
        /* size: 7, cachelines: 1, members: 2 */
        /* last cacheline: 7 bytes */
};

Coccinelle semantic patch:

@@
expression E1,E2;
@@

- memcpy(E1, E2, 6)
+ ether_addr_copy(E1, E2)

@@
expression E1,E2;
@@

- memcpy(E1, E2, ETH_ALEN)
+ ether_addr_copy(E1, E2)

---
Changes in v2:
- revised the cocci patch to be a more constrainting one
- applied cocci patch on the entire wlan-ng folder
- updated the patch description with further pahole output

Quoted 2 lines
Quoted 2 lines

Changes in v3:
- make an explicit cast from u8 (*)[6] to const u8 * to avoid

compile warning

Quoted 9 lines

Review

Quoted 9 lines

Changes in v4:
- fix compile and checkpatch warnings introduced in v3

 drivers/staging/wlan-ng/p80211conv.c   | 20 ++++++++++----------
 drivers/staging/wlan-ng/p80211netdev.c |  2 +-
 2 files changed, 11 insertions(+), 11 deletions(-)
(quoted diff) +0 -0

b/drivers/staging/wlan-ng/p80211conv.c

Quoted 4 lines

drivers/staging/wlan-ng/p80211conv.c int skb_ether_to_p80211(wlandevice_t *wlandev, u32

Quoted 4 lines

index 1b02cdf..87d323a 100644

drivers/staging/wlan-ng/p80211conv.c +0 -0
@@ -178,21 +178,21 @@ int skb_ether_to_p80211(wlandevice_t *wlandev, u32

ethconv,

Quoted 5 lines

drivers/staging/wlan-ng/p80211conv.c int skb_ether_to_p80211(wlandevice_t *wlandev, u32 ethconv,

Quoted 5 lines
drivers/staging/wlan-ng/p80211conv.c +0 -2
      switch (wlandev->macmode) {
      case WLAN_MACMODE_IBSS_STA:
-             memcpy(p80211_hdr->a3.a1, &e_hdr.daddr, ETH_ALEN);
-             memcpy(p80211_hdr->a3.a2, wlandev->netdev->dev_addr,

ETH_ALEN);

Quoted 2 lines

drivers/staging/wlan-ng/p80211conv.c int skb_ether_to_p80211(wlandevice_t *wlandev, u32 · line 180

Quoted 2 lines
drivers/staging/wlan-ng/p80211conv.c +1 -1
-             memcpy(p80211_hdr->a3.a3, wlandev->bssid, ETH_ALEN);
+             ether_addr_copy(p80211_hdr->a3.a1, (const u8

*)&e_hdr.daddr);

Quoted 4 lines

When you have to add a cast like this, it's a good idea to try to figure out _why_ that cast is needed.

Quoted 8 lines

I think you should just make the struct wlan_ethhdr daddr field be const and then you don't need to make any casts when you convert to ether_addr_copy().

So can you turn this into a two patch series? The first one change the structure variable type, and the second one this ether_addr_copy() change?

Changing the type of daddr and saddr fields from wlan_ethhdr to const is not enough in this situation.
The type should be changed to const u8* from u8 (*) [6] but this introduces other compile time warnings like 'discards ‘const’ qualifier from pointer target type'.
Also, if I make the change, every time a variable of wlan_ethhdr is used memory should be allocated for daddr and saddr fields.
In this circumstance, should I continue with the patchset?

Thanks,
Ioana

maintainer
#3 lore raw
Quoted 134 lines · Ioana Ciornei
Quoted 92 lines · Greg KH
Quoted 91 lines · Ioana Ciornei

Replace memcpy with ether_addr_copy since the output of pahole shows that the addresses are aligned.

struct p80211_hdr_a3 {
        __le16                     fc;                   /*     0     2 */ u16                        dur;                  /*     2     2 */ u8                         a1[6];                /*     4     6 */ u8                         a2[6];                /*    10     6 */ u8                         a3[6];                /*    16     6 */ u16                        seq;                  /*    22     2 */

        /* size: 24, cachelines: 1, members: 6 */
        /* last cacheline: 24 bytes */
};

struct net_device {
        char                       name[16];             /*     0    16 */ struct hlist_node          name_hlist;           /*    16    16 */

     ...............

 /* --- cacheline 12 boundary (768 bytes) --- */
        long unsigned int          last_rx;              /*   768     8 */ unsigned char *            dev_addr;             /*   776     8 */ struct netdev_rx_queue *   _rx;                  /*   784     8 */

    .................
};

struct wlandevice {
        struct wlandevice *        next;                 /*     0     8 */
    .................
        u8                         bssid[6];             /*   128     6 */
    .................
};

struct net_device {
        char                       name[16];             /*     0    16 */
    .................
        unsigned char *            dev_addr;             /*   776     8 */
    ................
};

struct wlan_ethhdr {
        u8                         daddr[6];             /*     0     6 */ u8                         saddr[6];             /*     6     6 */ u16                        type;                 /*    12     2 */

        /* size: 14, cachelines: 1, members: 3 */
        /* last cacheline: 14 bytes */
};

struct sockaddr {
        sa_family_t                sa_family;            /*     0     2 */ char                       sa_data[14];          /*     2    14 */

        /* size: 16, cachelines: 1, members: 2 */
        /* last cacheline: 16 bytes */
};

struct p80211pstr6 {
        u8                         len;                  /*     0     1 */ u8                         data[6];              /*     1     6 */

        /* size: 7, cachelines: 1, members: 2 */
        /* last cacheline: 7 bytes */
};

Coccinelle semantic patch:

@@
expression E1,E2;
@@

- memcpy(E1, E2, 6)
+ ether_addr_copy(E1, E2)

@@
expression E1,E2;
@@

- memcpy(E1, E2, ETH_ALEN)
+ ether_addr_copy(E1, E2)

---
Changes in v2:
    - revised the cocci patch to be a more constrainting one
    - applied cocci patch on the entire wlan-ng folder
    - updated the patch description with further pahole output

Quoted 2 lines
Quoted 2 lines

Changes in v3:
    - make an explicit cast from u8 (*)[6] to const u8 * to avoid compile

warning

Quoted 9 lines

drivers/staging/wlan-ng/

Quoted 9 lines

Changes in v4:
    - fix compile and checkpatch warnings introduced in v3

 drivers/staging/wlan-ng/p80211conv.c   | 20 ++++++++++---------- drivers/staging/wlan-ng/p80211netdev.c |  2 +-

 2 files changed, 11 insertions(+), 11 deletions(-)
drivers/staging/wlan-ng/ renamed from drivers/staging/wlan-ng/p80211conv.c +0 -0

p80211conv.c

Quoted 4 lines

drivers/staging/wlan-ng/p80211conv.c int skb_ether_to_p80211(wlandevice_t *wlandev, u32

Quoted 4 lines

index 1b02cdf..87d323a 100644

drivers/staging/wlan-ng/p80211conv.c +0 -0
@@ -178,21 +178,21 @@ int skb_ether_to_p80211(wlandevice_t *wlandev, u32

ethconv,

Quoted 5 lines
Quoted 5 lines
drivers/staging/wlan-ng/p80211conv.c +0 -0
       switch (wlandev->macmode) {

      case WLAN_MACMODE_IBSS_STA:
-             memcpy(p80211_hdr->a3.a1, &e_hdr.daddr, ETH_ALEN);
-             memcpy(p80211_hdr->a3.a2, wlandev->netdev->dev_addr,

ETH_ALEN);

Quoted 6 lines
Quoted 2 lines

-             memcpy(p80211_hdr->a3.a3, wlandev->bssid, ETH_ALEN);
+             ether_addr_copy(p80211_hdr->a3.a1, (const u8 *)&e_hdr.daddr);

When you have to add a cast like this, it's a good idea to try to figure out _why_ that cast is needed.

Quoted 8 lines

I think you should just make the struct wlan_ethhdr daddr field be const and then you don't need to make any casts when you convert to ether_addr_copy().

So can you turn this into a two patch series?  The first one change the structure variable type, and the second one this ether_addr_copy() change?

Quoted 5 lines

Changing the type of daddr and saddr fields from wlan_ethhdr to const is not enough in this situation.
The type should be changed to const u8* from u8 (*) [6] but this introduces other compile time warnings like 'discards ‘const’ qualifier from pointer target type'.

Then fix those up :)

Quoted 2 lines

Also, if I make the change, every time a variable of wlan_ethhdr is used memory should be allocated for daddr and saddr fields.

Hm, no, that shouldn't be needed at all.

Quoted 1 line

In this circumstance, should I continue with the patchset?

I don't really know, that's up to you.

greg k-h