Skip to contentSkip to conversation

Bluetooth: Refactor HCI request & L2CAP bt_skb_cb members to use union

PATCH In review Johan Hedberg 2 participants 2 messages Mar 29, 2015

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Bluetooth: Refactor HCI request & L2CAP bt_skb_cb members to use union

From: Johan Hedberg <[email protected]>

The HCI request and L2CAP related members of the bt_skb_cb struct will never be in use at the same time. Since space available for the skb cb is limited this patch splits the members to the existing l2cap_ctrl and a new req_ctrl struct, both behind the same union.

This paves the way to extend the req_ctrl with another callback option without hitting the limits of the skb cb size.

---
 include/net/bluetooth/bluetooth.h | 20 ++++++++++------
 net/bluetooth/hci_core.c          | 12 +++++-----
 net/bluetooth/hci_event.c         |  4 ++--
 net/bluetooth/hci_request.c       |  6 ++---
 net/bluetooth/hci_sock.c          |  2 +-
 net/bluetooth/l2cap_core.c        | 48 +++++++++++++++++++--------------------
 net/bluetooth/l2cap_sock.c        |  6 ++---
 net/bluetooth/smp.c               |  2 +-
 8 files changed, 53 insertions(+), 47 deletions(-)
include/net/bluetooth/bluetooth.h +13 -7
index 33a5e00025aa..8b2840d8e974 100644
@@ -269,25 +269,31 @@ struct l2cap_ctrl {
 	__u16	reqseq;
 	__u16	txseq;
 	__u8	retries;
+	__le16  psm;
+	struct l2cap_chan *chan;
+	bdaddr_t bdaddr;
 };
 
 struct hci_dev;
 
 typedef void (*hci_req_complete_t)(struct hci_dev *hdev, u8 status, u16 opcode);
 
+struct req_ctrl {
+	__u8 start:1;
+	u8 event;
+	hci_req_complete_t complete;
+};
+
 struct bt_skb_cb {
 	__u8 pkt_type;
 	__u8 force_active;
 	__u16 opcode;
 	__u16 expect;
 	__u8 incoming:1;
-	__u8 req_start:1;
-	u8 req_event;
-	hci_req_complete_t req_complete;
-	struct l2cap_chan *chan;
-	struct l2cap_ctrl control;
-	bdaddr_t bdaddr;
-	__le16 psm;
+	union {
+		struct l2cap_ctrl l2cap;
+		struct req_ctrl req;
+	};
 };
 #define bt_cb(skb) ((struct bt_skb_cb *)((skb)->cb))
 
net/bluetooth/hci_core.c +6 -6
index e6bfeb7b4415..9eaa26754b2d 100644
@@ -3585,7 +3585,7 @@ int hci_send_cmd(struct hci_dev *hdev, __u16 opcode, __u32 plen,
 	/* Stand-alone HCI commands must be flagged as
 	 * single-command requests.
 	 */
-	bt_cb(skb)->req_start = 1;
+	bt_cb(skb)->req.start = 1;
 
 	skb_queue_tail(&hdev->cmd_q, skb);
 	queue_work(hdev->workqueue, &hdev->cmd_work);
@@ -4263,7 +4263,7 @@ static bool hci_req_is_complete(struct hci_dev *hdev)
 	if (!skb)
 		return true;
 
-	return bt_cb(skb)->req_start;
+	return bt_cb(skb)->req.start;
 }
 
 static void hci_resend_last(struct hci_dev *hdev)
@@ -4323,14 +4323,14 @@ void hci_req_cmd_complete(struct hci_dev *hdev, u16 opcode, u8 status)
 	 * command queue (hdev->cmd_q).
 	 */
 	if (hdev->sent_cmd) {
-		req_complete = bt_cb(hdev->sent_cmd)->req_complete;
+		req_complete = bt_cb(hdev->sent_cmd)->req.complete;
 
 		if (req_complete) {
 			/* We must set the complete callback to NULL to
 			 * avoid calling the callback more than once if
 			 * this function gets called again.
 			 */
-			bt_cb(hdev->sent_cmd)->req_complete = NULL;
+			bt_cb(hdev->sent_cmd)->req.complete = NULL;
 
 			goto call_complete;
 		}
@@ -4339,12 +4339,12 @@ void hci_req_cmd_complete(struct hci_dev *hdev, u16 opcode, u8 status)
 	/* Remove all pending commands belonging to this request */
 	spin_lock_irqsave(&hdev->cmd_q.lock, flags);
 	while ((skb = __skb_dequeue(&hdev->cmd_q))) {
-		if (bt_cb(skb)->req_start) {
+		if (bt_cb(skb)->req.start) {
 			__skb_queue_head(&hdev->cmd_q, skb);
 			break;
 		}
 
-		req_complete = bt_cb(skb)->req_complete;
+		req_complete = bt_cb(skb)->req.complete;
 		kfree_skb(skb);
 	}
 	spin_unlock_irqrestore(&hdev->cmd_q.lock, flags);
net/bluetooth/hci_event.c +2 -2
index 20f34b861426..7c0f992602f5 100644
@@ -3125,7 +3125,7 @@ static void hci_cmd_status_evt(struct hci_dev *hdev, struct sk_buff *skb)
 		atomic_set(&hdev->cmd_cnt, 1);
 
 	if (ev->status ||
-	    (hdev->sent_cmd && !bt_cb(hdev->sent_cmd)->req_event))
+	    (hdev->sent_cmd && !bt_cb(hdev->sent_cmd)->req.event))
 		hci_req_cmd_complete(hdev, opcode, ev->status);
 
 	if (atomic_read(&hdev->cmd_cnt) && !skb_queue_empty(&hdev->cmd_q))
@@ -5049,7 +5049,7 @@ void hci_event_packet(struct hci_dev *hdev, struct sk_buff *skb)
 
 	skb_pull(skb, HCI_EVENT_HDR_SIZE);
 
-	if (hdev->sent_cmd && bt_cb(hdev->sent_cmd)->req_event == event) {
+	if (hdev->sent_cmd && bt_cb(hdev->sent_cmd)->req.event == event) {
 		struct hci_command_hdr *cmd_hdr = (void *) hdev->sent_cmd->data;
 		u16 opcode = __le16_to_cpu(cmd_hdr->opcode);
 
net/bluetooth/hci_request.c +3 -3
index 55e096d20a0f..8a0b7e319523 100644
@@ -55,7 +55,7 @@ int hci_req_run(struct hci_request *req, hci_req_complete_t complete)
 		return -ENODATA;
 
 	skb = skb_peek_tail(&req->cmd_q);
-	bt_cb(skb)->req_complete = complete;
+	bt_cb(skb)->req.complete = complete;
 
 	spin_lock_irqsave(&hdev->cmd_q.lock, flags);
 	skb_queue_splice_tail(&req->cmd_q, &hdev->cmd_q);
@@ -116,9 +116,9 @@ void hci_req_add_ev(struct hci_request *req, u16 opcode, u32 plen,
 	}
 
 	if (skb_queue_empty(&req->cmd_q))
-		bt_cb(skb)->req_start = 1;
+		bt_cb(skb)->req.start = 1;
 
-	bt_cb(skb)->req_event = event;
+	bt_cb(skb)->req.event = event;
 
 	skb_queue_tail(&req->cmd_q, skb);
 }
net/bluetooth/hci_sock.c +1 -1
index 85a44a7dc150..2c0abef1aa83 100644
@@ -1164,7 +1164,7 @@ static int hci_sock_sendmsg(struct socket *sock, struct msghdr *msg,
 			/* Stand-alone HCI commands must be flagged as
 			 * single-command requests.
 			 */
-			bt_cb(skb)->req_start = 1;
+			bt_cb(skb)->req.start = 1;
 
 			skb_queue_tail(&hdev->cmd_q, skb);
 			queue_work(hdev->workqueue, &hdev->cmd_work);
net/bluetooth/l2cap_core.c +24 -24

149 lines not on this page · raw message

net/bluetooth/l2cap_sock.c +3 -3
index 9070720eedc8..a7278f05eafb 100644
@@ -1330,7 +1330,7 @@ static struct sk_buff *l2cap_sock_alloc_skb_cb(struct l2cap_chan *chan,
 
 	skb->priority = sk->sk_priority;
 
-	bt_cb(skb)->chan = chan;
+	bt_cb(skb)->l2cap.chan = chan;
 
 	return skb;
 }
@@ -1444,8 +1444,8 @@ static void l2cap_skb_msg_name(struct sk_buff *skb, void *msg_name,
 
 	memset(la, 0, sizeof(struct sockaddr_l2));
 	la->l2_family = AF_BLUETOOTH;
-	la->l2_psm = bt_cb(skb)->psm;
-	bacpy(&la->l2_bdaddr, &bt_cb(skb)->bdaddr);
+	la->l2_psm = bt_cb(skb)->l2cap.psm;
+	bacpy(&la->l2_bdaddr, &bt_cb(skb)->l2cap.bdaddr);
 
 	*msg_namelen = sizeof(struct sockaddr_l2);
 }
net/bluetooth/smp.c +1 -1
index 1ec3f66b5a74..1910c5806974 100644
@@ -3017,7 +3017,7 @@ static struct sk_buff *smp_alloc_skb_cb(struct l2cap_chan *chan,
 		return ERR_PTR(-ENOMEM);
 
 	skb->priority = HCI_PRIO_MAX;
-	bt_cb(skb)->chan = chan;
+	bt_cb(skb)->l2cap.chan = chan;
 
 	return skb;
 }
Signature · 1 line
2.1.0
maintainer
#1 lore raw

Hi Johan,

Quoted 25 lines

The HCI request and L2CAP related members of the bt_skb_cb struct will never be in use at the same time. Since space available for the skb cb is limited this patch splits the members to the existing l2cap_ctrl and a new req_ctrl struct, both behind the same union.

This paves the way to extend the req_ctrl with another callback option without hitting the limits of the skb cb size.

---

include/net/bluetooth/bluetooth.h | 20 ++++++++++------
net/bluetooth/hci_core.c          | 12 +++++-----
net/bluetooth/hci_event.c         |  4 ++--
net/bluetooth/hci_request.c       |  6 ++---
net/bluetooth/hci_sock.c          |  2 +-
net/bluetooth/l2cap_core.c        | 48 +++++++++++++++++++--------------------
net/bluetooth/l2cap_sock.c        |  6 ++---
net/bluetooth/smp.c               |  2 +-
8 files changed, 53 insertions(+), 47 deletions(-)
include/net/bluetooth/bluetooth.h +0 -0
index 33a5e00025aa..8b2840d8e974 100644

include/net/bluetooth/bluetooth.h struct l2cap_ctrl { · line 275

Quoted 7 lines
include/net/bluetooth/bluetooth.h +3 -0
@@ -269,25 +269,31 @@ struct l2cap_ctrl {
 	__u16	reqseq;
 	__u16	txseq;
 	__u8	retries;
+	__le16  psm;
+	struct l2cap_chan *chan;
+	bdaddr_t bdaddr;
 };

it might make sense to have bdaddr_t before the data pointer. At least bdaddr_t is fixed size on each architecture would be most likely easier to pack and align.

include/net/bluetooth/bluetooth.h struct l2cap_ctrl { · line 282

Quoted 6 lines
include/net/bluetooth/bluetooth.h +2 -0
 struct hci_dev;
 
 typedef void (*hci_req_complete_t)(struct hci_dev *hdev, u8 status, u16 opcode);
 
+struct req_ctrl {
+	__u8 start:1;

Since this is of no benefit here, I wonder if we should just start using bool here.

Quoted 12 lines
include/net/bluetooth/bluetooth.h +4 -2
+	u8 event;
+	hci_req_complete_t complete;
+};
+
 struct bt_skb_cb {
 	__u8 pkt_type;
 	__u8 force_active;
 	__u16 opcode;
 	__u16 expect;
 	__u8 incoming:1;
-	__u8 req_start:1;
-	u8 req_event;

include/net/bluetooth/bluetooth.h struct l2cap_ctrl { · line 297

Quoted 10 lines
include/net/bluetooth/bluetooth.h +4 -5
-	hci_req_complete_t req_complete;
-	struct l2cap_chan *chan;
-	struct l2cap_ctrl control;
-	bdaddr_t bdaddr;
-	__le16 psm;
+	union {
+		struct l2cap_ctrl l2cap;
+		struct req_ctrl req;
+	};
 };

I would have done this in multiple patches. Fist group the L2CAP specific ones into l2cap_ctrl. I mean add psm, bdaddr and chan into l2cap_ctrl struct. And then rename control to l2cap. That would confine the changes into the L2CAP layer things.

Then second, create the req_ctrl and use a req_ctrl req entry in the struct.

And thirdly move l2cap and req into a union. Actually third might not work out and breaks compilation in 64-bit if incoming and req_start are no longer close to each other so that bits are really moved into a single char.

The rest seems fine.

Regards

Marcel