Skip to content

Commit

Permalink
networking: add and use skb_put_u8()
Browse files Browse the repository at this point in the history
Joe and Bjørn suggested that it'd be nicer to not have the
cast in the fairly common case of doing
	*(u8 *)skb_put(skb, 1) = c;

Add skb_put_u8() for this case, and use it across the code,
using the following spatch:

    @@
    expression SKB, C, S;
    typedef u8;
    identifier fn = {skb_put};
    fresh identifier fn2 = fn ## "_u8";
    @@
    - *(u8 *)fn(SKB, S) = C;
    + fn2(SKB, C);

Note that due to the "S", the spatch isn't perfect, it should
have checked that S is 1, but there's also places that use a
sizeof expression like sizeof(var) or sizeof(u8) etc. Turns
out that nobody ever did something like
	*(u8 *)skb_put(skb, 2) = c;

which would be wrong anyway since the second byte wouldn't be
initialized.

Suggested-by: Joe Perches <[email protected]>
Suggested-by: Bjørn Mork <[email protected]>
Signed-off-by: Johannes Berg <[email protected]>
Signed-off-by: David S. Miller <[email protected]>
  • Loading branch information
jmberg-intel authored and davem330 committed Jun 16, 2017
1 parent d58ff35 commit 634fef6
Show file tree
Hide file tree
Showing 39 changed files with 106 additions and 100 deletions.
2 changes: 1 addition & 1 deletion drivers/bluetooth/bluecard_cs.c
Original file line number Diff line number Diff line change
Expand Up @@ -448,7 +448,7 @@ static void bluecard_receive(struct bluecard_info *info,

} else {

*(u8 *)skb_put(info->rx_skb, 1) = buf[i];
skb_put_u8(info->rx_skb, buf[i]);
info->rx_count--;

if (info->rx_count == 0) {
Expand Down
2 changes: 1 addition & 1 deletion drivers/bluetooth/bt3c_cs.c
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ static void bt3c_receive(struct bt3c_info *info)

__u8 x = inb(iobase + DATA_L);

*(u8 *)skb_put(info->rx_skb, 1) = x;
skb_put_u8(info->rx_skb, x);
inb(iobase + DATA_H);
info->rx_count--;

Expand Down
2 changes: 1 addition & 1 deletion drivers/bluetooth/btuart_cs.c
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ static void btuart_receive(struct btuart_info *info)

} else {

*(u8 *)skb_put(info->rx_skb, 1) = inb(iobase + UART_RX);
skb_put_u8(info->rx_skb, inb(iobase + UART_RX));
info->rx_count--;

if (info->rx_count == 0) {
Expand Down
6 changes: 3 additions & 3 deletions drivers/bluetooth/btusb.c
Original file line number Diff line number Diff line change
Expand Up @@ -1844,7 +1844,7 @@ static int inject_cmd_complete(struct hci_dev *hdev, __u16 opcode)
evt->ncmd = 0x01;
evt->opcode = cpu_to_le16(opcode);

*(u8 *)skb_put(skb, 1) = 0x00;
skb_put_u8(skb, 0x00);

hci_skb_pkt_type(skb) = HCI_EVENT_PKT;

Expand Down Expand Up @@ -2767,8 +2767,8 @@ static struct urb *alloc_diag_urb(struct hci_dev *hdev, bool enable)
return ERR_PTR(-ENOMEM);
}

*(u8 *)skb_put(skb, 1) = 0xf0;
*(u8 *)skb_put(skb, 1) = enable;
skb_put_u8(skb, 0xf0);
skb_put_u8(skb, enable);

pipe = usb_sndbulkpipe(data->udev, data->diag_tx_ep->bEndpointAddress);

Expand Down
4 changes: 2 additions & 2 deletions drivers/bluetooth/dtl1_cs.c
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ static void dtl1_receive(struct dtl1_info *info)
}
}

*(u8 *)skb_put(info->rx_skb, 1) = inb(iobase + UART_RX);
skb_put_u8(info->rx_skb, inb(iobase + UART_RX));
nsh = (struct nsh *)info->rx_skb->data;

info->rx_count--;
Expand Down Expand Up @@ -414,7 +414,7 @@ static int dtl1_hci_send_frame(struct hci_dev *hdev, struct sk_buff *skb)
skb_reserve(s, NSHL);
skb_copy_from_linear_data(skb, skb_put(s, skb->len), skb->len);
if (skb->len & 0x0001)
*(u8 *)skb_put(s, 1) = 0; /* PAD */
skb_put_u8(s, 0); /* PAD */

/* Prepend skb with Nokia frame header and queue */
memcpy(skb_push(s, NSHL), &nsh, NSHL);
Expand Down
6 changes: 3 additions & 3 deletions drivers/bluetooth/hci_bcm.c
Original file line number Diff line number Diff line change
Expand Up @@ -262,9 +262,9 @@ static int bcm_set_diag(struct hci_dev *hdev, bool enable)
if (!skb)
return -ENOMEM;

*(u8 *)skb_put(skb, 1) = BCM_LM_DIAG_PKT;
*(u8 *)skb_put(skb, 1) = 0xf0;
*(u8 *)skb_put(skb, 1) = enable;
skb_put_u8(skb, BCM_LM_DIAG_PKT);
skb_put_u8(skb, 0xf0);
skb_put_u8(skb, enable);

skb_queue_tail(&bcm->txq, skb);
hci_uart_tx_wakeup(hu);
Expand Down
2 changes: 1 addition & 1 deletion drivers/bluetooth/hci_intel.c
Original file line number Diff line number Diff line change
Expand Up @@ -470,7 +470,7 @@ static int inject_cmd_complete(struct hci_dev *hdev, __u16 opcode)
evt->ncmd = 0x01;
evt->opcode = cpu_to_le16(opcode);

*(u8 *)skb_put(skb, 1) = 0x00;
skb_put_u8(skb, 0x00);

hci_skb_pkt_type(skb) = HCI_EVENT_PKT;

Expand Down
2 changes: 1 addition & 1 deletion drivers/bluetooth/hci_nokia.c
Original file line number Diff line number Diff line change
Expand Up @@ -532,7 +532,7 @@ static int nokia_enqueue(struct hci_uart *hu, struct sk_buff *skb)
err = skb_pad(skb, 1);
if (err)
return err;
*(u8 *)skb_put(skb, 1) = 0x00;
skb_put_u8(skb, 0x00);
}

skb_queue_tail(&btdev->txq, skb);
Expand Down
2 changes: 1 addition & 1 deletion drivers/bluetooth/hci_qca.c
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ static int send_hci_ibs_cmd(u8 cmd, struct hci_uart *hu)
}

/* Assign HCI_IBS type */
*(u8 *)skb_put(skb, 1) = cmd;
skb_put_u8(skb, cmd);

skb_queue_tail(&qca->txq, skb);

Expand Down
4 changes: 2 additions & 2 deletions drivers/bluetooth/hci_vhci.c
Original file line number Diff line number Diff line change
Expand Up @@ -146,8 +146,8 @@ static int __vhci_create_device(struct vhci_data *data, __u8 opcode)

hci_skb_pkt_type(skb) = HCI_VENDOR_PKT;

*(u8 *)skb_put(skb, 1) = 0xff;
*(u8 *)skb_put(skb, 1) = opcode;
skb_put_u8(skb, 0xff);
skb_put_u8(skb, opcode);
put_unaligned_le16(hdev->id, skb_put(skb, 2));
skb_queue_tail(&data->readq, skb);

Expand Down
4 changes: 2 additions & 2 deletions drivers/isdn/capi/capi.c
Original file line number Diff line number Diff line change
Expand Up @@ -1082,7 +1082,7 @@ static int capinc_tty_put_char(struct tty_struct *tty, unsigned char ch)
skb = mp->outskb;
if (skb) {
if (skb_tailroom(skb) > 0) {
*(u8 *)skb_put(skb, 1) = ch;
skb_put_u8(skb, ch);
goto unlock_out;
}
mp->outskb = NULL;
Expand All @@ -1094,7 +1094,7 @@ static int capinc_tty_put_char(struct tty_struct *tty, unsigned char ch)
skb = alloc_skb(CAPI_DATA_B3_REQ_LEN + CAPI_MAX_BLKSIZE, GFP_ATOMIC);
if (skb) {
skb_reserve(skb, CAPI_DATA_B3_REQ_LEN);
*(u8 *)skb_put(skb, 1) = ch;
skb_put_u8(skb, ch);
mp->outskb = skb;
} else {
printk(KERN_ERR "capinc_put_char: char %u lost\n", ch);
Expand Down
22 changes: 11 additions & 11 deletions drivers/isdn/gigaset/asyncdata.c
Original file line number Diff line number Diff line change
Expand Up @@ -492,33 +492,33 @@ static struct sk_buff *HDLC_Encode(struct sk_buff *skb)
hdlc_skb->mac_len = skb->mac_len;

/* Add flag sequence in front of everything.. */
*(u8 *)skb_put(hdlc_skb, 1) = PPP_FLAG;
skb_put_u8(hdlc_skb, PPP_FLAG);

/* Perform byte stuffing while copying data. */
while (skb->len--) {
if (muststuff(*skb->data)) {
*(u8 *)skb_put(hdlc_skb, 1) = PPP_ESCAPE;
*(u8 *)skb_put(hdlc_skb, 1) = (*skb->data++) ^ PPP_TRANS;
skb_put_u8(hdlc_skb, PPP_ESCAPE);
skb_put_u8(hdlc_skb, (*skb->data++) ^ PPP_TRANS);
} else
*(u8 *)skb_put(hdlc_skb, 1) = *skb->data++;
skb_put_u8(hdlc_skb, *skb->data++);
}

/* Finally add FCS (byte stuffed) and flag sequence */
c = (fcs & 0x00ff); /* least significant byte first */
if (muststuff(c)) {
*(u8 *)skb_put(hdlc_skb, 1) = PPP_ESCAPE;
skb_put_u8(hdlc_skb, PPP_ESCAPE);
c ^= PPP_TRANS;
}
*(u8 *)skb_put(hdlc_skb, 1) = c;
skb_put_u8(hdlc_skb, c);

c = ((fcs >> 8) & 0x00ff);
if (muststuff(c)) {
*(u8 *)skb_put(hdlc_skb, 1) = PPP_ESCAPE;
skb_put_u8(hdlc_skb, PPP_ESCAPE);
c ^= PPP_TRANS;
}
*(u8 *)skb_put(hdlc_skb, 1) = c;
skb_put_u8(hdlc_skb, c);

*(u8 *)skb_put(hdlc_skb, 1) = PPP_FLAG;
skb_put_u8(hdlc_skb, PPP_FLAG);

dev_kfree_skb_any(skb);
return hdlc_skb;
Expand Down Expand Up @@ -561,8 +561,8 @@ static struct sk_buff *iraw_encode(struct sk_buff *skb)
while (len--) {
c = bitrev8(*cp++);
if (c == DLE_FLAG)
*(u8 *)skb_put(iraw_skb, 1) = c;
*(u8 *)skb_put(iraw_skb, 1) = c;
skb_put_u8(iraw_skb, c);
skb_put_u8(iraw_skb, c);
}
dev_kfree_skb_any(skb);
return iraw_skb;
Expand Down
7 changes: 4 additions & 3 deletions drivers/isdn/i4l/isdn_bsdcomp.c
Original file line number Diff line number Diff line change
Expand Up @@ -602,7 +602,8 @@ static int bsd_compress(void *state, struct sk_buff *skb_in, struct sk_buff *skb
* Do not emit a completely useless byte of ones.
*/
if (bitno < 32 && skb_out && skb_tailroom(skb_out) > 0)
*(u8 *)skb_put(skb_out, 1) = (unsigned char)((accm | (0xff << (bitno - 8))) >> 24);
skb_put_u8(skb_out,
(unsigned char)((accm | (0xff << (bitno - 8))) >> 24));

/*
* Increase code size if we would have without the packet
Expand Down Expand Up @@ -698,7 +699,7 @@ static int bsd_decompress(void *state, struct sk_buff *skb_in, struct sk_buff *s
db->bytes_out += ilen;

if (skb_tailroom(skb_out) > 0)
*(u8 *)skb_put(skb_out, 1) = 0;
skb_put_u8(skb_out, 0);
else
return DECOMP_ERR_NOMEM;

Expand Down Expand Up @@ -816,7 +817,7 @@ static int bsd_decompress(void *state, struct sk_buff *skb_in, struct sk_buff *s
#endif

if (extra) /* the KwKwK case again */
*(u8 *)skb_put(skb_out, 1) = finchar;
skb_put_u8(skb_out, finchar);

/*
* If not first code in a packet, and
Expand Down
4 changes: 2 additions & 2 deletions drivers/isdn/i4l/isdn_x25iface.c
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ static int isdn_x25iface_connect_ind(struct concap_proto *cprot)

skb = dev_alloc_skb(1);
if (skb) {
*(u8 *)skb_put(skb, 1) = X25_IFACE_CONNECT;
skb_put_u8(skb, X25_IFACE_CONNECT);
skb->protocol = x25_type_trans(skb, cprot->net_dev);
netif_rx(skb);
return 0;
Expand Down Expand Up @@ -253,7 +253,7 @@ static int isdn_x25iface_disconn_ind(struct concap_proto *cprot)
*state_p = WAN_DISCONNECTED;
skb = dev_alloc_skb(1);
if (skb) {
*(u8 *)skb_put(skb, 1) = X25_IFACE_DISCONNECT;
skb_put_u8(skb, X25_IFACE_DISCONNECT);
skb->protocol = x25_type_trans(skb, cprot->net_dev);
netif_rx(skb);
return 0;
Expand Down
4 changes: 2 additions & 2 deletions drivers/net/hamradio/scc.c
Original file line number Diff line number Diff line change
Expand Up @@ -540,7 +540,7 @@ static inline void scc_rxint(struct scc_channel *scc)
}

scc->rx_buff = skb;
*(u8 *)skb_put(skb, 1) = 0; /* KISS data */
skb_put_u8(skb, 0); /* KISS data */
}

if (skb->len >= scc->stat.bufsize)
Expand All @@ -555,7 +555,7 @@ static inline void scc_rxint(struct scc_channel *scc)
return;
}

*(u8 *)skb_put(skb, 1) = Inb(scc->data);
skb_put_u8(skb, Inb(scc->data));
}


Expand Down
2 changes: 1 addition & 1 deletion drivers/net/usb/cdc_ncm.c
Original file line number Diff line number Diff line change
Expand Up @@ -1250,7 +1250,7 @@ cdc_ncm_fill_tx_frame(struct usbnet *dev, struct sk_buff *skb, __le32 sign)
skb_put_zero(skb_out, padding_count);
} else if (skb_out->len < ctx->tx_max &&
(skb_out->len % dev->maxpacket) == 0) {
*(u8 *)skb_put(skb_out, 1) = 0; /* force short packet */
skb_put_u8(skb_out, 0); /* force short packet */
}

/* set final frame length */
Expand Down
2 changes: 1 addition & 1 deletion drivers/net/usb/net1080.c
Original file line number Diff line number Diff line change
Expand Up @@ -473,7 +473,7 @@ net1080_tx_fixup(struct usbnet *dev, struct sk_buff *skb, gfp_t flags)

/* maybe pad; then trailer */
if (!((skb->len + sizeof *trailer) & 0x01))
*(u8 *)skb_put(skb, 1) = PAD_BYTE;
skb_put_u8(skb, PAD_BYTE);
trailer = skb_put(skb, sizeof *trailer);
put_unaligned(header->packet_id, &trailer->packet_id);
#if 0
Expand Down
8 changes: 4 additions & 4 deletions drivers/net/usb/zaurus.c
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,10 @@ zaurus_tx_fixup(struct usbnet *dev, struct sk_buff *skb, gfp_t flags)
fcs = crc32_le(~0, skb->data, skb->len);
fcs = ~fcs;

*(u8 *)skb_put(skb, 1) = fcs & 0xff;
*(u8 *)skb_put(skb, 1) = (fcs>> 8) & 0xff;
*(u8 *)skb_put(skb, 1) = (fcs>>16) & 0xff;
*(u8 *)skb_put(skb, 1) = (fcs>>24) & 0xff;
skb_put_u8(skb, fcs & 0xff);
skb_put_u8(skb, (fcs >> 8) & 0xff);
skb_put_u8(skb, (fcs >> 16) & 0xff);
skb_put_u8(skb, (fcs >> 24) & 0xff);
}
return skb;
}
Expand Down
2 changes: 1 addition & 1 deletion drivers/nfc/fdp/i2c.c
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ static void fdp_nci_i2c_add_len_lrc(struct sk_buff *skb)
for (i = 0; i < len + 2; i++)
lrc ^= skb->data[i];

*(u8 *)skb_put(skb, 1) = lrc;
skb_put_u8(skb, lrc);
}

static void fdp_nci_i2c_remove_len_lrc(struct sk_buff *skb)
Expand Down
4 changes: 2 additions & 2 deletions drivers/nfc/microread/i2c.c
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ static void microread_i2c_add_len_crc(struct sk_buff *skb)
for (i = 0; i < skb->len; i++)
crc = crc ^ skb->data[i];

*(u8 *)skb_put(skb, 1) = crc;
skb_put_u8(skb, crc);
}

static void microread_i2c_remove_len_crc(struct sk_buff *skb)
Expand Down Expand Up @@ -173,7 +173,7 @@ static int microread_i2c_read(struct microread_i2c_phy *phy,
goto flush;
}

*(u8 *)skb_put(*skb, 1) = len;
skb_put_u8(*skb, len);

r = i2c_master_recv(client, skb_put(*skb, len), len);
if (r != len) {
Expand Down
4 changes: 2 additions & 2 deletions drivers/nfc/microread/microread.c
Original file line number Diff line number Diff line change
Expand Up @@ -441,8 +441,8 @@ static int microread_im_transceive(struct nfc_hci_dev *hdev,

crc = crc_ccitt(0xffff, skb->data, skb->len);
crc = ~crc;
*(u8 *)skb_put(skb, 1) = crc & 0xff;
*(u8 *)skb_put(skb, 1) = crc >> 8;
skb_put_u8(skb, crc & 0xff);
skb_put_u8(skb, crc >> 8);
break;
case MICROREAD_GATE_ID_MREAD_NFC_T3:
control_bits = 0xDB;
Expand Down
4 changes: 2 additions & 2 deletions drivers/nfc/nfcmrvl/fw_dnld.c
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@ static int process_state_fw_dnld(struct nfcmrvl_private *priv,
out_skb = alloc_lc_skb(priv, 1);
if (!out_skb)
return -ENOMEM;
*(u8 *)skb_put(out_skb, 1) = 0xBF;
skb_put_u8(out_skb, 0xBF);
nci_send_frame(priv->ndev, out_skb);
priv->fw_dnld.substate = SUBSTATE_WAIT_NACK_CREDIT;
return 0;
Expand All @@ -301,7 +301,7 @@ static int process_state_fw_dnld(struct nfcmrvl_private *priv,
out_skb = alloc_lc_skb(priv, 1);
if (!out_skb)
return -ENOMEM;
*(u8 *)skb_put(out_skb, 1) = HELPER_ACK_PACKET_FORMAT;
skb_put_u8(out_skb, HELPER_ACK_PACKET_FORMAT);
nci_send_frame(priv->ndev, out_skb);
priv->fw_dnld.substate = SUBSTATE_WAIT_ACK_CREDIT;
break;
Expand Down
Loading

0 comments on commit 634fef6

Please sign in to comment.