Skip to contentSkip to conversation

Bluetooth: Optimize bt_cb(skb) usage

PATCH In review Johan Hedberg 2 participants 5 messages Nov 5, 2015

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

cover Bluetooth: Optimize bt_cb(skb) usage

Hi,

This set of patches does changes to the structs behind bt_cb(skb) to minimize their sizes. This is important since skb->cb has a quite small fixed size.

Johan

----------------------------------------------------------------
Johan Hedberg (3):
Bluetooth: Remove redundant setting to zero of bt_cb
Bluetooth: Compress the size of struct hci_ctrl
Bluetooth: bt_skb_cb: Merge force_active and incoming into flags

 include/net/bluetooth/bluetooth.h | 25 ++++++++++++++-----------
 include/net/bluetooth/hci_core.h  |  2 +-
 net/bluetooth/hci_conn.c          |  4 ++--
 net/bluetooth/hci_core.c          | 29 ++++++++++++++++-------------
 net/bluetooth/hci_request.c       | 10 +++++++---
 net/bluetooth/hci_sock.c          | 12 ++++++------
 net/bluetooth/l2cap_core.c        |  7 +++++--
 7 files changed, 51 insertions(+), 38 deletions(-)
#1 lore raw

PATCH 1/3 Bluetooth: Remove redundant setting to zero of bt_cb

From: Johan Hedberg <[email protected]>

The socket allocation functions will always memset skb->cb to zero so there's no need to make other initializations needing the same thing.

---
 include/net/bluetooth/bluetooth.h | 8 ++------
 1 file changed, 2 insertions(+), 6 deletions(-)
include/net/bluetooth/bluetooth.h +2 -6
index 663e0ef1eaa0..a85e6d3d75ef 100644
@@ -325,10 +325,8 @@ static inline struct sk_buff *bt_skb_alloc(unsigned int len, gfp_t how)
 	struct sk_buff *skb;
 
 	skb = alloc_skb(len + BT_SKB_RESERVE, how);
-	if (skb) {
+	if (skb)
 		skb_reserve(skb, BT_SKB_RESERVE);
-		bt_cb(skb)->incoming  = 0;
-	}
 	return skb;
 }
 
@@ -338,10 +336,8 @@ static inline struct sk_buff *bt_skb_send_alloc(struct sock *sk,
 	struct sk_buff *skb;
 
 	skb = sock_alloc_send_skb(sk, len + BT_SKB_RESERVE, nb, err);
-	if (skb) {
+	if (skb)
 		skb_reserve(skb, BT_SKB_RESERVE);
-		bt_cb(skb)->incoming  = 0;
-	}
 
 	if (!skb && *err)
 		return NULL;
Signature · 1 line
2.5.0
#2 lore raw

PATCH 2/3 Bluetooth: Compress the size of struct hci_ctrl

In reply to Johan Hedberg

From: Johan Hedberg <[email protected]>

We can reduce the size of the hci_ctrl struct by converting 'bool req_start' to 'u8 req_flags' and making the two function pointers a union (since only one is ever set at a time).

---
 include/net/bluetooth/bluetooth.h | 11 ++++++++---
 net/bluetooth/hci_core.c          | 14 +++++++-------
 net/bluetooth/hci_request.c       | 10 +++++++---
 net/bluetooth/hci_sock.c          |  2 +-
 4 files changed, 23 insertions(+), 14 deletions(-)
include/net/bluetooth/bluetooth.h +8 -3
index a85e6d3d75ef..8d38f411009c 100644
@@ -296,12 +296,17 @@ typedef void (*hci_req_complete_t)(struct hci_dev *hdev, u8 status, u16 opcode);
 typedef void (*hci_req_complete_skb_t)(struct hci_dev *hdev, u8 status,
 				       u16 opcode, struct sk_buff *skb);
 
+#define HCI_REQ_START	BIT(0)
+#define HCI_REQ_SKB	BIT(1)
+
 struct hci_ctrl {
 	__u16 opcode;
-	bool req_start;
+	u8 req_flags;
 	u8 req_event;
-	hci_req_complete_t req_complete;
-	hci_req_complete_skb_t req_complete_skb;
+	union {
+		hci_req_complete_t req_complete;
+		hci_req_complete_skb_t req_complete_skb;
+	};
 };
 
 struct bt_skb_cb {
net/bluetooth/hci_core.c +7 -7
index aa4df13a8139..c270feaac8a1 100644
@@ -3690,7 +3690,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)->hci.req_start = true;
+	bt_cb(skb)->hci.req_flags |= HCI_REQ_START;
 
 	skb_queue_tail(&hdev->cmd_q, skb);
 	queue_work(hdev->workqueue, &hdev->cmd_work);
@@ -4387,7 +4387,7 @@ static bool hci_req_is_complete(struct hci_dev *hdev)
 	if (!skb)
 		return true;
 
-	return bt_cb(skb)->hci.req_start;
+	return (bt_cb(skb)->hci.req_flags & HCI_REQ_START);
 }
 
 static void hci_resend_last(struct hci_dev *hdev)
@@ -4447,20 +4447,20 @@ void hci_req_cmd_complete(struct hci_dev *hdev, u16 opcode, u8 status,
 	 * callback would be found in hdev->sent_cmd instead of the
 	 * command queue (hdev->cmd_q).
 	 */
-	if (bt_cb(hdev->sent_cmd)->hci.req_complete) {
-		*req_complete = bt_cb(hdev->sent_cmd)->hci.req_complete;
+	if (bt_cb(hdev->sent_cmd)->hci.req_flags & HCI_REQ_SKB) {
+		*req_complete_skb = bt_cb(hdev->sent_cmd)->hci.req_complete_skb;
 		return;
 	}
 
-	if (bt_cb(hdev->sent_cmd)->hci.req_complete_skb) {
-		*req_complete_skb = bt_cb(hdev->sent_cmd)->hci.req_complete_skb;
+	if (bt_cb(hdev->sent_cmd)->hci.req_complete) {
+		*req_complete = bt_cb(hdev->sent_cmd)->hci.req_complete;
 		return;
 	}
 
 	/* 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)->hci.req_start) {
+		if (bt_cb(skb)->hci.req_flags & HCI_REQ_START) {
 			__skb_queue_head(&hdev->cmd_q, skb);
 			break;
 		}
net/bluetooth/hci_request.c +7 -3
index bdb170995966..5ba27c30e8f2 100644
@@ -56,8 +56,12 @@ static int req_run(struct hci_request *req, hci_req_complete_t complete,
 		return -ENODATA;
 
 	skb = skb_peek_tail(&req->cmd_q);
-	bt_cb(skb)->hci.req_complete = complete;
-	bt_cb(skb)->hci.req_complete_skb = complete_skb;
+	if (complete) {
+		bt_cb(skb)->hci.req_complete = complete;
+	} else if (complete_skb) {
+		bt_cb(skb)->hci.req_complete_skb = complete_skb;
+		bt_cb(skb)->hci.req_flags |= HCI_REQ_SKB;
+	}
 
 	spin_lock_irqsave(&hdev->cmd_q.lock, flags);
 	skb_queue_splice_tail(&req->cmd_q, &hdev->cmd_q);
@@ -128,7 +132,7 @@ void hci_req_add_ev(struct hci_request *req, u16 opcode, u32 plen,
 	}
 
 	if (skb_queue_empty(&req->cmd_q))
-		bt_cb(skb)->hci.req_start = true;
+		bt_cb(skb)->hci.req_flags |= HCI_REQ_START;
 
 	bt_cb(skb)->hci.req_event = event;
 
net/bluetooth/hci_sock.c +1 -1
index 235ad0fa3571..19b23013c4f6 100644
@@ -1249,7 +1249,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)->hci.req_start = true;
+			bt_cb(skb)->hci.req_flags |= HCI_REQ_START;
 
 			skb_queue_tail(&hdev->cmd_q, skb);
 			queue_work(hdev->workqueue, &hdev->cmd_work);
Signature · 1 line
2.5.0
#3 lore raw

PATCH 3/3 Bluetooth: bt_skb_cb: Merge force_active and incoming into flags

In reply to Johan Hedberg

From: Johan Hedberg <[email protected]>

These are both boolean values in practice so we can save some space in the bt_skb_cb struct by merging them both into a single flags field.

---
 include/net/bluetooth/bluetooth.h |  6 ++++--
 include/net/bluetooth/hci_core.h  |  2 +-
 net/bluetooth/hci_conn.c          |  4 ++--
 net/bluetooth/hci_core.c          | 15 +++++++++------
 net/bluetooth/hci_sock.c          | 10 +++++-----
 net/bluetooth/l2cap_core.c        |  7 +++++--
 6 files changed, 26 insertions(+), 18 deletions(-)
include/net/bluetooth/bluetooth.h +4 -2
index 8d38f411009c..d8b3d5ef2f82 100644
@@ -309,11 +309,13 @@ struct hci_ctrl {
 	};
 };
 
+#define BT_INCOMING		BIT(0)
+#define BT_FORCE_ACTIVE		BIT(1)
+
 struct bt_skb_cb {
 	__u8 pkt_type;
-	__u8 force_active;
+	__u8 flags;
 	__u16 expect;
-	__u8 incoming:1;
 	union {
 		struct l2cap_ctrl l2cap;
 		struct hci_ctrl hci;
include/net/bluetooth/hci_core.h +1 -1
index 1878d0a96333..9c461d43260f 100644
@@ -889,7 +889,7 @@ int hci_conn_security(struct hci_conn *conn, __u8 sec_level, __u8 auth_type,
 		      bool initiator);
 int hci_conn_switch_role(struct hci_conn *conn, __u8 role);
 
-void hci_conn_enter_active_mode(struct hci_conn *conn, __u8 force_active);
+void hci_conn_enter_active_mode(struct hci_conn *conn, bool force_active);
 
 void hci_le_conn_failed(struct hci_conn *conn, u8 status);
 
net/bluetooth/hci_conn.c +2 -2
index 85b82f7adbd2..f24d876b64d5 100644
@@ -1126,7 +1126,7 @@ struct hci_conn *hci_connect_sco(struct hci_dev *hdev, int type, bdaddr_t *dst,
 	if (acl->state == BT_CONNECTED &&
 	    (sco->state == BT_OPEN || sco->state == BT_CLOSED)) {
 		set_bit(HCI_CONN_POWER_SAVE, &acl->flags);
-		hci_conn_enter_active_mode(acl, BT_POWER_FORCE_ACTIVE_ON);
+		hci_conn_enter_active_mode(acl, true);
 
 		if (test_bit(HCI_CONN_MODE_CHANGE_PEND, &acl->flags)) {
 			/* defer SCO setup until mode change completed */
@@ -1323,7 +1323,7 @@ int hci_conn_switch_role(struct hci_conn *conn, __u8 role)
 EXPORT_SYMBOL(hci_conn_switch_role);
 
 /* Enter active mode */
-void hci_conn_enter_active_mode(struct hci_conn *conn, __u8 force_active)
+void hci_conn_enter_active_mode(struct hci_conn *conn, bool force_active)
 {
 	struct hci_dev *hdev = conn->hdev;
 
net/bluetooth/hci_core.c +9 -6
index c270feaac8a1..b20dbefb4a0e 100644
@@ -3586,7 +3586,7 @@ int hci_recv_frame(struct hci_dev *hdev, struct sk_buff *skb)
 	}
 
 	/* Incoming skb */
-	bt_cb(skb)->incoming = 1;
+	bt_cb(skb)->flags |= BT_INCOMING;
 
 	/* Time stamp */
 	__net_timestamp(skb);
@@ -4088,6 +4088,8 @@ static void hci_sched_acl_pkt(struct hci_dev *hdev)
 	       (chan = hci_chan_sent(hdev, ACL_LINK, &quote))) {
 		u32 priority = (skb_peek(&chan->data_q))->priority;
 		while (quote-- && (skb = skb_peek(&chan->data_q))) {
+			bool force_active;
+
 			BT_DBG("chan %p skb %p len %d priority %u", chan, skb,
 			       skb->len, skb->priority);
 
@@ -4097,8 +4099,8 @@ static void hci_sched_acl_pkt(struct hci_dev *hdev)
 
 			skb = skb_dequeue(&chan->data_q);
 
-			hci_conn_enter_active_mode(chan->conn,
-						   bt_cb(skb)->force_active);
+			force_active = (bt_cb(skb)->flags & BT_FORCE_ACTIVE);
+			hci_conn_enter_active_mode(chan->conn, force_active);
 
 			hci_send_frame(hdev, skb);
 			hdev->acl_last_tx = jiffies;
@@ -4134,6 +4136,7 @@ static void hci_sched_acl_blk(struct hci_dev *hdev)
 	       (chan = hci_chan_sent(hdev, type, &quote))) {
 		u32 priority = (skb_peek(&chan->data_q))->priority;
 		while (quote > 0 && (skb = skb_peek(&chan->data_q))) {
+			bool force_active;
 			int blocks;
 
 			BT_DBG("chan %p skb %p len %d priority %u", chan, skb,
@@ -4149,8 +4152,8 @@ static void hci_sched_acl_blk(struct hci_dev *hdev)
 			if (blocks > hdev->block_cnt)
 				return;
 
-			hci_conn_enter_active_mode(chan->conn,
-						   bt_cb(skb)->force_active);
+			force_active = (bt_cb(skb)->flags & BT_FORCE_ACTIVE);
+			hci_conn_enter_active_mode(chan->conn, force_active);
 
 			hci_send_frame(hdev, skb);
 			hdev->acl_last_tx = jiffies;
@@ -4335,7 +4338,7 @@ static void hci_acldata_packet(struct hci_dev *hdev, struct sk_buff *skb)
 	hci_dev_unlock(hdev);
 
 	if (conn) {
-		hci_conn_enter_active_mode(conn, BT_POWER_FORCE_ACTIVE_OFF);
+		hci_conn_enter_active_mode(conn, false);
 
 		/* Send to upper protocol */
 		l2cap_recv_acldata(conn, skb, flags);
net/bluetooth/hci_sock.c +5 -5
index 19b23013c4f6..41555a04919e 100644
@@ -178,7 +178,7 @@ void hci_send_to_sock(struct hci_dev *hdev, struct sk_buff *skb)
 			if (is_filtered_packet(sk, skb))
 				continue;
 		} else if (hci_pi(sk)->channel == HCI_CHANNEL_USER) {
-			if (!bt_cb(skb)->incoming)
+			if (!(bt_cb(skb)->flags & BT_INCOMING))
 				continue;
 			if (hci_skb_pkt_type(skb) != HCI_EVENT_PKT &&
 			    hci_skb_pkt_type(skb) != HCI_ACLDATA_PKT &&
@@ -270,13 +270,13 @@ void hci_send_to_monitor(struct hci_dev *hdev, struct sk_buff *skb)
 		opcode = cpu_to_le16(HCI_MON_EVENT_PKT);
 		break;
 	case HCI_ACLDATA_PKT:
-		if (bt_cb(skb)->incoming)
+		if (bt_cb(skb)->flags & BT_INCOMING)
 			opcode = cpu_to_le16(HCI_MON_ACL_RX_PKT);
 		else
 			opcode = cpu_to_le16(HCI_MON_ACL_TX_PKT);
 		break;
 	case HCI_SCODATA_PKT:
-		if (bt_cb(skb)->incoming)
+		if (bt_cb(skb)->flags & BT_INCOMING)
 			opcode = cpu_to_le16(HCI_MON_SCO_RX_PKT);
 		else
 			opcode = cpu_to_le16(HCI_MON_SCO_TX_PKT);
@@ -444,7 +444,7 @@ static void hci_si_event(struct hci_dev *hdev, int type, int dlen, void *data)
 	ev->type = type;
 	memcpy(ev->data, data, dlen);
 
-	bt_cb(skb)->incoming = 1;
+	bt_cb(skb)->flags |= BT_INCOMING;
 	__net_timestamp(skb);
 
 	hci_skb_pkt_type(skb) = HCI_EVENT_PKT;
@@ -960,7 +960,7 @@ static void hci_sock_cmsg(struct sock *sk, struct msghdr *msg,
 	__u32 mask = hci_pi(sk)->cmsg_mask;
 
 	if (mask & HCI_CMSG_DIR) {
-		int incoming = bt_cb(skb)->incoming;
+		int incoming = !!(bt_cb(skb)->flags & BT_INCOMING);
 		put_cmsg(msg, SOL_HCI, HCI_CMSG_DIR, sizeof(incoming),
 			 &incoming);
 	}
net/bluetooth/l2cap_core.c +5 -2
index 7c65ee200c29..c8be10b3ef5a 100644
@@ -857,7 +857,7 @@ static void l2cap_send_cmd(struct l2cap_conn *conn, u8 ident, u8 code, u16 len,
 	else
 		flags = ACL_START;
 
-	bt_cb(skb)->force_active = BT_POWER_FORCE_ACTIVE_ON;
+	bt_cb(skb)->flags |= BT_FORCE_ACTIVE;
 	skb->priority = HCI_PRIO_MAX;
 
 	hci_send_acl(conn->hchan, skb, flags);
@@ -897,7 +897,10 @@ static void l2cap_do_send(struct l2cap_chan *chan, struct sk_buff *skb)
 	else
 		flags = ACL_START;
 
-	bt_cb(skb)->force_active = test_bit(FLAG_FORCE_ACTIVE, &chan->flags);
+	if (test_bit(FLAG_FORCE_ACTIVE, &chan->flags))
+		bt_cb(skb)->flags |= BT_FORCE_ACTIVE;
+	else
+		bt_cb(skb)->flags &= ~BT_FORCE_ACTIVE;
 	hci_send_acl(chan->conn->hchan, skb, flags);
 }
 
Signature · 1 line
2.5.0
maintainer
#4 lore raw
In reply to Johan Hedberg

Hi Johan,

Quoted 12 lines

This set of patches does changes to the structs behind bt_cb(skb) to minimize their sizes. This is important since skb->cb has a quite small fixed size.

Johan

----------------------------------------------------------------
Johan Hedberg (3):
Bluetooth: Remove redundant setting to zero of bt_cb
Bluetooth: Compress the size of struct hci_ctrl
Bluetooth: bt_skb_cb: Merge force_active and incoming into flags

Quoted 8 lines
include/net/bluetooth/bluetooth.h | 25 ++++++++++++++-----------
include/net/bluetooth/hci_core.h  |  2 +-
net/bluetooth/hci_conn.c          |  4 ++--
net/bluetooth/hci_core.c          | 29 ++++++++++++++++-------------
net/bluetooth/hci_request.c       | 10 +++++++---
net/bluetooth/hci_sock.c          | 12 ++++++------
net/bluetooth/l2cap_core.c        |  7 +++++--
7 files changed, 51 insertions(+), 38 deletions(-)

patch 1 and 2 have been applied to bluetooth-next tree.

I am not so sure about patch 3 yet. I think that needs a deeper look into it.

Regards

Marcel