Skip to content

Commit

Permalink
networking: introduce and use skb_put_data()
Browse files Browse the repository at this point in the history
A common pattern with skb_put() is to just want to memcpy()
some data into the new space, introduce skb_put_data() for
this.

An spatch similar to the one for skb_put_zero() converts many
of the places using it:

    @@
    identifier p, p2;
    expression len, skb, data;
    type t, t2;
    @@
    (
    -p = skb_put(skb, len);
    +p = skb_put_data(skb, data, len);
    |
    -p = (t)skb_put(skb, len);
    +p = skb_put_data(skb, data, len);
    )
    (
    p2 = (t2)p;
    -memcpy(p2, data, len);
    |
    -memcpy(p, data, len);
    )

    @@
    type t, t2;
    identifier p, p2;
    expression skb, data;
    @@
    t *p;
    ...
    (
    -p = skb_put(skb, sizeof(t));
    +p = skb_put_data(skb, data, sizeof(t));
    |
    -p = (t *)skb_put(skb, sizeof(t));
    +p = skb_put_data(skb, data, sizeof(t));
    )
    (
    p2 = (t2)p;
    -memcpy(p2, data, sizeof(*p));
    |
    -memcpy(p, data, sizeof(*p));
    )

    @@
    expression skb, len, data;
    @@
    -memcpy(skb_put(skb, len), data, len);
    +skb_put_data(skb, data, len);

(again, manually post-processed to retain some comments)

Reviewed-by: Stephen Hemminger <[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 b080db5 commit 59ae1d1
Show file tree
Hide file tree
Showing 252 changed files with 622 additions and 741 deletions.
2 changes: 1 addition & 1 deletion drivers/atm/fore200e.c
Original file line number Diff line number Diff line change
Expand Up @@ -1104,7 +1104,7 @@ fore200e_push_rpd(struct fore200e* fore200e, struct atm_vcc* vcc, struct rpd* rp
/* Make device DMA transfer visible to CPU. */
fore200e->bus->dma_sync_for_cpu(fore200e, buffer->data.dma_addr, rpd->rsd[ i ].length, DMA_FROM_DEVICE);

memcpy(skb_put(skb, rpd->rsd[ i ].length), buffer->data.align_addr, rpd->rsd[ i ].length);
skb_put_data(skb, buffer->data.align_addr, rpd->rsd[i].length);

/* Now let the device get at it again. */
fore200e->bus->dma_sync_for_device(fore200e, buffer->data.dma_addr, rpd->rsd[ i ].length, DMA_FROM_DEVICE);
Expand Down
2 changes: 1 addition & 1 deletion drivers/atm/he.c
Original file line number Diff line number Diff line change
Expand Up @@ -1735,7 +1735,7 @@ he_service_rbrq(struct he_dev *he_dev, int group)
__net_timestamp(skb);

list_for_each_entry(heb, &he_vcc->buffers, entry)
memcpy(skb_put(skb, heb->len), &heb->data, heb->len);
skb_put_data(skb, &heb->data, heb->len);

switch (vcc->qos.aal) {
case ATM_AAL0:
Expand Down
11 changes: 4 additions & 7 deletions drivers/atm/idt77252.c
Original file line number Diff line number Diff line change
Expand Up @@ -1090,8 +1090,7 @@ dequeue_rx(struct idt77252_dev *card, struct rsq_entry *rsqe)

*((u32 *) sb->data) = aal0;
skb_put(sb, sizeof(u32));
memcpy(skb_put(sb, ATM_CELL_PAYLOAD),
cell, ATM_CELL_PAYLOAD);
skb_put_data(sb, cell, ATM_CELL_PAYLOAD);

ATM_SKB(sb)->vcc = vcc;
__net_timestamp(sb);
Expand Down Expand Up @@ -1159,8 +1158,7 @@ dequeue_rx(struct idt77252_dev *card, struct rsq_entry *rsqe)
return;
}
skb_queue_walk(&rpp->queue, sb)
memcpy(skb_put(skb, sb->len),
sb->data, sb->len);
skb_put_data(skb, sb->data, sb->len);

recycle_rx_pool_skb(card, rpp);

Expand Down Expand Up @@ -1322,8 +1320,7 @@ idt77252_rx_raw(struct idt77252_dev *card)

*((u32 *) sb->data) = header;
skb_put(sb, sizeof(u32));
memcpy(skb_put(sb, ATM_CELL_PAYLOAD), &(queue->data[16]),
ATM_CELL_PAYLOAD);
skb_put_data(sb, &(queue->data[16]), ATM_CELL_PAYLOAD);

ATM_SKB(sb)->vcc = vcc;
__net_timestamp(sb);
Expand Down Expand Up @@ -2014,7 +2011,7 @@ idt77252_send_oam(struct atm_vcc *vcc, void *cell, int flags)
}
atomic_add(skb->truesize, &sk_atm(vcc)->sk_wmem_alloc);

memcpy(skb_put(skb, 52), cell, 52);
skb_put_data(skb, cell, 52);

return idt77252_send_skb(vcc, skb, 1);
}
Expand Down
2 changes: 1 addition & 1 deletion drivers/atm/solos-pci.c
Original file line number Diff line number Diff line change
Expand Up @@ -493,7 +493,7 @@ static int send_command(struct solos_card *card, int dev, const char *buf, size_
header->vci = cpu_to_le16(0);
header->type = cpu_to_le16(PKT_COMMAND);

memcpy(skb_put(skb, size), buf, size);
skb_put_data(skb, buf, size);

fpga_queue(card, dev, skb, NULL);

Expand Down
6 changes: 3 additions & 3 deletions drivers/bluetooth/bfusb.c
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,7 @@ static inline int bfusb_recv_block(struct bfusb_data *data, int hdr, unsigned ch
}

if (len > 0)
memcpy(skb_put(data->reassembly, len), buf, len);
skb_put_data(data->reassembly, buf, len);

if (hdr & 0x08) {
hci_recv_frame(data->hdev, data->reassembly);
Expand Down Expand Up @@ -505,7 +505,7 @@ static int bfusb_send_frame(struct hci_dev *hdev, struct sk_buff *skb)
buf[1] = 0x00;
buf[2] = (size == BFUSB_MAX_BLOCK_SIZE) ? 0 : size;

memcpy(skb_put(nskb, 3), buf, 3);
skb_put_data(nskb, buf, 3);
skb_copy_from_linear_data_offset(skb, sent, skb_put(nskb, size), size);

sent += size;
Expand All @@ -516,7 +516,7 @@ static int bfusb_send_frame(struct hci_dev *hdev, struct sk_buff *skb)
if ((nskb->len % data->bulk_pkt_size) == 0) {
buf[0] = 0xdd;
buf[1] = 0x00;
memcpy(skb_put(nskb, 2), buf, 2);
skb_put_data(nskb, buf, 2);
}

read_lock(&data->lock);
Expand Down
2 changes: 1 addition & 1 deletion drivers/bluetooth/bluecard_cs.c
Original file line number Diff line number Diff line change
Expand Up @@ -597,7 +597,7 @@ static int bluecard_hci_set_baud_rate(struct hci_dev *hdev, int baud)
break;
}

memcpy(skb_put(skb, sizeof(cmd)), cmd, sizeof(cmd));
skb_put_data(skb, cmd, sizeof(cmd));

skb_queue_tail(&(info->txq), skb);

Expand Down
2 changes: 1 addition & 1 deletion drivers/bluetooth/btmrvl_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ static int btmrvl_send_sync_cmd(struct btmrvl_private *priv, u16 opcode,
hdr->plen = len;

if (len)
memcpy(skb_put(skb, len), param, len);
skb_put_data(skb, param, len);

hci_skb_pkt_type(skb) = MRVL_VENDOR_PKT;

Expand Down
2 changes: 1 addition & 1 deletion drivers/bluetooth/btqcomsmd.c
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ static int btqcomsmd_recv(struct hci_dev *hdev, unsigned int type,
}

hci_skb_pkt_type(skb) = type;
memcpy(skb_put(skb, count), data, count);
skb_put_data(skb, data, count);

return hci_recv_frame(hdev, skb);
}
Expand Down
12 changes: 6 additions & 6 deletions drivers/bluetooth/btusb.c
Original file line number Diff line number Diff line change
Expand Up @@ -478,7 +478,7 @@ static int btusb_recv_intr(struct btusb_data *data, void *buffer, int count)
}

len = min_t(uint, hci_skb_expect(skb), count);
memcpy(skb_put(skb, len), buffer, len);
skb_put_data(skb, buffer, len);

count -= len;
buffer += len;
Expand Down Expand Up @@ -533,7 +533,7 @@ static int btusb_recv_bulk(struct btusb_data *data, void *buffer, int count)
}

len = min_t(uint, hci_skb_expect(skb), count);
memcpy(skb_put(skb, len), buffer, len);
skb_put_data(skb, buffer, len);

count -= len;
buffer += len;
Expand Down Expand Up @@ -590,7 +590,7 @@ static int btusb_recv_isoc(struct btusb_data *data, void *buffer, int count)
}

len = min_t(uint, hci_skb_expect(skb), count);
memcpy(skb_put(skb, len), buffer, len);
skb_put_data(skb, buffer, len);

count -= len;
buffer += len;
Expand Down Expand Up @@ -934,8 +934,8 @@ static void btusb_diag_complete(struct urb *urb)

skb = bt_skb_alloc(urb->actual_length, GFP_ATOMIC);
if (skb) {
memcpy(skb_put(skb, urb->actual_length),
urb->transfer_buffer, urb->actual_length);
skb_put_data(skb, urb->transfer_buffer,
urb->actual_length);
hci_recv_diag(hdev, skb);
}
} else if (urb->status == -ENOENT) {
Expand Down Expand Up @@ -2395,7 +2395,7 @@ static int marvell_config_oob_wake(struct hci_dev *hdev)
return -ENOMEM;
}

memcpy(skb_put(skb, sizeof(cmd)), cmd, sizeof(cmd));
skb_put_data(skb, cmd, sizeof(cmd));
hci_skb_pkt_type(skb) = HCI_COMMAND_PKT;

ret = btusb_send_frame(hdev, skb);
Expand Down
16 changes: 8 additions & 8 deletions drivers/bluetooth/hci_bcsp.c
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ static void bcsp_slip_msgdelim(struct sk_buff *skb)
{
const char pkt_delim = 0xc0;

memcpy(skb_put(skb, 1), &pkt_delim, 1);
skb_put_data(skb, &pkt_delim, 1);
}

static void bcsp_slip_one_byte(struct sk_buff *skb, u8 c)
Expand All @@ -135,13 +135,13 @@ static void bcsp_slip_one_byte(struct sk_buff *skb, u8 c)

switch (c) {
case 0xc0:
memcpy(skb_put(skb, 2), &esc_c0, 2);
skb_put_data(skb, &esc_c0, 2);
break;
case 0xdb:
memcpy(skb_put(skb, 2), &esc_db, 2);
skb_put_data(skb, &esc_db, 2);
break;
default:
memcpy(skb_put(skb, 1), &c, 1);
skb_put_data(skb, &c, 1);
}
}

Expand Down Expand Up @@ -423,7 +423,7 @@ static void bcsp_handle_le_pkt(struct hci_uart *hu)
BT_DBG("Found a LE conf pkt");
if (!nskb)
return;
memcpy(skb_put(nskb, 4), conf_rsp_pkt, 4);
skb_put_data(nskb, conf_rsp_pkt, 4);
hci_skb_pkt_type(nskb) = BCSP_LE_PKT;

skb_queue_head(&bcsp->unrel, nskb);
Expand All @@ -447,7 +447,7 @@ static inline void bcsp_unslip_one_byte(struct bcsp_struct *bcsp, unsigned char
bcsp->rx_esc_state = BCSP_ESCSTATE_ESC;
break;
default:
memcpy(skb_put(bcsp->rx_skb, 1), &byte, 1);
skb_put_data(bcsp->rx_skb, &byte, 1);
if ((bcsp->rx_skb->data[0] & 0x40) != 0 &&
bcsp->rx_state != BCSP_W4_CRC)
bcsp_crc_update(&bcsp->message_crc, byte);
Expand All @@ -458,7 +458,7 @@ static inline void bcsp_unslip_one_byte(struct bcsp_struct *bcsp, unsigned char
case BCSP_ESCSTATE_ESC:
switch (byte) {
case 0xdc:
memcpy(skb_put(bcsp->rx_skb, 1), &c0, 1);
skb_put_data(bcsp->rx_skb, &c0, 1);
if ((bcsp->rx_skb->data[0] & 0x40) != 0 &&
bcsp->rx_state != BCSP_W4_CRC)
bcsp_crc_update(&bcsp->message_crc, 0xc0);
Expand All @@ -467,7 +467,7 @@ static inline void bcsp_unslip_one_byte(struct bcsp_struct *bcsp, unsigned char
break;

case 0xdd:
memcpy(skb_put(bcsp->rx_skb, 1), &db, 1);
skb_put_data(bcsp->rx_skb, &db, 1);
if ((bcsp->rx_skb->data[0] & 0x40) != 0 &&
bcsp->rx_state != BCSP_W4_CRC)
bcsp_crc_update(&bcsp->message_crc, 0xdb);
Expand Down
2 changes: 1 addition & 1 deletion drivers/bluetooth/hci_h4.c
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ struct sk_buff *h4_recv_buf(struct hci_dev *hdev, struct sk_buff *skb,
}

len = min_t(uint, hci_skb_expect(skb) - skb->len, count);
memcpy(skb_put(skb, len), buffer, len);
skb_put_data(skb, buffer, len);

count -= len;
buffer += len;
Expand Down
12 changes: 6 additions & 6 deletions drivers/bluetooth/hci_h5.c
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ static void h5_link_control(struct hci_uart *hu, const void *data, size_t len)

hci_skb_pkt_type(nskb) = HCI_3WIRE_LINK_PKT;

memcpy(skb_put(nskb, len), data, len);
skb_put_data(nskb, data, len);

skb_queue_tail(&h5->unrel, nskb);
}
Expand Down Expand Up @@ -487,7 +487,7 @@ static void h5_unslip_one_byte(struct h5 *h5, unsigned char c)
}
}

memcpy(skb_put(h5->rx_skb, 1), byte, 1);
skb_put_data(h5->rx_skb, byte, 1);
h5->rx_pending--;

BT_DBG("unsliped 0x%02hhx, rx_pending %zu", *byte, h5->rx_pending);
Expand Down Expand Up @@ -579,7 +579,7 @@ static void h5_slip_delim(struct sk_buff *skb)
{
const char delim = SLIP_DELIMITER;

memcpy(skb_put(skb, 1), &delim, 1);
skb_put_data(skb, &delim, 1);
}

static void h5_slip_one_byte(struct sk_buff *skb, u8 c)
Expand All @@ -589,13 +589,13 @@ static void h5_slip_one_byte(struct sk_buff *skb, u8 c)

switch (c) {
case SLIP_DELIMITER:
memcpy(skb_put(skb, 2), &esc_delim, 2);
skb_put_data(skb, &esc_delim, 2);
break;
case SLIP_ESC:
memcpy(skb_put(skb, 2), &esc_esc, 2);
skb_put_data(skb, &esc_esc, 2);
break;
default:
memcpy(skb_put(skb, 1), &c, 1);
skb_put_data(skb, &c, 1);
}
}

Expand Down
7 changes: 3 additions & 4 deletions drivers/bluetooth/hci_intel.c
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ static int intel_lpm_suspend(struct hci_uart *hu)
return -ENOMEM;
}

memcpy(skb_put(skb, sizeof(suspend)), suspend, sizeof(suspend));
skb_put_data(skb, suspend, sizeof(suspend));
hci_skb_pkt_type(skb) = HCI_LPM_PKT;

set_bit(STATE_LPM_TRANSACTION, &intel->flags);
Expand Down Expand Up @@ -270,8 +270,7 @@ static int intel_lpm_host_wake(struct hci_uart *hu)
return -ENOMEM;
}

memcpy(skb_put(skb, sizeof(lpm_resume_ack)), lpm_resume_ack,
sizeof(lpm_resume_ack));
skb_put_data(skb, lpm_resume_ack, sizeof(lpm_resume_ack));
hci_skb_pkt_type(skb) = HCI_LPM_PKT;

/* LPM flow is a priority, enqueue packet at list head */
Expand Down Expand Up @@ -522,7 +521,7 @@ static int intel_set_baudrate(struct hci_uart *hu, unsigned int speed)
return -ENOMEM;
}

memcpy(skb_put(skb, sizeof(speed_cmd)), speed_cmd, sizeof(speed_cmd));
skb_put_data(skb, speed_cmd, sizeof(speed_cmd));
hci_skb_pkt_type(skb) = HCI_COMMAND_PKT;

hci_uart_set_flow_control(hu, true);
Expand Down
2 changes: 1 addition & 1 deletion drivers/bluetooth/hci_ll.c
Original file line number Diff line number Diff line change
Expand Up @@ -413,7 +413,7 @@ static int ll_recv(struct hci_uart *hu, const void *data, int count)
while (count) {
if (ll->rx_count) {
len = min_t(unsigned int, ll->rx_count, count);
memcpy(skb_put(ll->rx_skb, len), ptr, len);
skb_put_data(ll->rx_skb, ptr, len);
ll->rx_count -= len; count -= len; ptr += len;

if (ll->rx_count)
Expand Down
2 changes: 1 addition & 1 deletion drivers/bluetooth/hci_mrvl.c
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,7 @@ static int mrvl_load_firmware(struct hci_dev *hdev, const char *name)
}
bt_cb(skb)->pkt_type = MRVL_RAW_DATA;

memcpy(skb_put(skb, mrvl->tx_len), fw_ptr, mrvl->tx_len);
skb_put_data(skb, fw_ptr, mrvl->tx_len);
fw_ptr += mrvl->tx_len;

set_bit(STATE_FW_REQ_PENDING, &mrvl->flags);
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 @@ -869,7 +869,7 @@ static int qca_set_baudrate(struct hci_dev *hdev, uint8_t baudrate)
}

/* Assign commands to change baudrate and packet type. */
memcpy(skb_put(skb, sizeof(cmd)), cmd, sizeof(cmd));
skb_put_data(skb, cmd, sizeof(cmd));
hci_skb_pkt_type(skb) = HCI_COMMAND_PKT;

skb_queue_tail(&qca->txq, skb);
Expand Down
2 changes: 1 addition & 1 deletion drivers/char/pcmcia/synclink_cs.c
Original file line number Diff line number Diff line change
Expand Up @@ -4235,7 +4235,7 @@ static void hdlcdev_rx(MGSLPC_INFO *info, char *buf, int size)
return;
}

memcpy(skb_put(skb, size), buf, size);
skb_put_data(skb, buf, size);

skb->protocol = hdlc_type_trans(skb, dev);

Expand Down
2 changes: 1 addition & 1 deletion drivers/firewire/net.c
Original file line number Diff line number Diff line change
Expand Up @@ -600,7 +600,7 @@ static int fwnet_incoming_packet(struct fwnet_device *dev, __be32 *buf, int len,
return -ENOMEM;
}
skb_reserve(skb, LL_RESERVED_SPACE(net));
memcpy(skb_put(skb, len), buf, len);
skb_put_data(skb, buf, len);

return fwnet_finish_incoming_packet(net, skb, source_node_id,
is_broadcast, ether_type);
Expand Down
2 changes: 1 addition & 1 deletion drivers/isdn/capi/capi.c
Original file line number Diff line number Diff line change
Expand Up @@ -1058,7 +1058,7 @@ static int capinc_tty_write(struct tty_struct *tty,
}

skb_reserve(skb, CAPI_DATA_B3_REQ_LEN);
memcpy(skb_put(skb, count), buf, count);
skb_put_data(skb, buf, count);

__skb_queue_tail(&mp->outqueue, skb);
mp->outbytes += skb->len;
Expand Down
2 changes: 1 addition & 1 deletion drivers/isdn/capi/capidrv.c
Original file line number Diff line number Diff line change
Expand Up @@ -516,7 +516,7 @@ static void send_message(capidrv_contr *card, _cmsg *cmsg)
printk(KERN_ERR "capidrv::send_message: can't allocate mem\n");
return;
}
memcpy(skb_put(skb, len), cmsg->buf, len);
skb_put_data(skb, cmsg->buf, len);
if (capi20_put_message(&global.ap, skb) != CAPI_NOERROR)
kfree_skb(skb);
}
Expand Down
6 changes: 3 additions & 3 deletions drivers/isdn/hardware/avm/b1.c
Original file line number Diff line number Diff line change
Expand Up @@ -529,8 +529,8 @@ irqreturn_t b1_interrupt(int interrupt, void *devptr)
printk(KERN_ERR "%s: incoming packet dropped\n",
card->name);
} else {
memcpy(skb_put(skb, MsgLen), card->msgbuf, MsgLen);
memcpy(skb_put(skb, DataB3Len), card->databuf, DataB3Len);
skb_put_data(skb, card->msgbuf, MsgLen);
skb_put_data(skb, card->databuf, DataB3Len);
capi_ctr_handle_message(ctrl, ApplId, skb);
}
break;
Expand All @@ -544,7 +544,7 @@ irqreturn_t b1_interrupt(int interrupt, void *devptr)
card->name);
spin_unlock_irqrestore(&card->lock, flags);
} else {
memcpy(skb_put(skb, MsgLen), card->msgbuf, MsgLen);
skb_put_data(skb, card->msgbuf, MsgLen);
if (CAPIMSG_CMD(skb->data) == CAPI_DATA_B3_CONF)
capilib_data_b3_conf(&cinfo->ncci_head, ApplId,
CAPIMSG_NCCI(skb->data),
Expand Down
Loading

0 comments on commit 59ae1d1

Please sign in to comment.