Linux bluetooth development
 help / color / mirror / Atom feed
* [PATCH BlueZ v1 1/2] shared/hci: Avoid redundant BPF filter updates on duplicate events
@ 2026-06-08 20:50 Luiz Augusto von Dentz
  2026-06-08 20:50 ` [PATCH BlueZ v1 2/2] shared/hci: Debounce SO_ATTACH_FILTER with timeout_add(0) Luiz Augusto von Dentz
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: Luiz Augusto von Dentz @ 2026-06-08 20:50 UTC (permalink / raw)
  To: linux-bluetooth

From: Luiz Augusto von Dentz <luiz.von.dentz@intel.com>

Skip updating the BPF socket filter in bt_hci_register and
bt_hci_register_subevent when the event/subevent is already
registered, since it is already part of the filter.

Similarly, skip updating the filter in bt_hci_unregister and
bt_hci_unregister_subevent when other handlers for the same
event/subevent still remain in the queue.

This avoids unnecessary setsockopt(SO_ATTACH_FILTER) calls when
multiple handlers are registered for the same event code.
---
 src/shared/hci.c | 37 +++++++++++++++++++++++++++++++++----
 1 file changed, 33 insertions(+), 4 deletions(-)

diff --git a/src/shared/hci.c b/src/shared/hci.c
index 40326fc810e6..1aba7db1d995 100644
--- a/src/shared/hci.c
+++ b/src/shared/hci.c
@@ -765,15 +765,28 @@ static void update_evt_filter(struct bt_hci *hci)
 	free(filters);
 }
 
+static bool match_evt_event(const void *a, const void *b)
+{
+	const struct evt *evt = a;
+	uint8_t event = PTR_TO_UINT(b);
+
+	return evt->event == event;
+}
+
 unsigned int bt_hci_register(struct bt_hci *hci, uint8_t event,
 				bt_hci_callback_func_t callback,
 				void *user_data, bt_hci_destroy_func_t destroy)
 {
 	struct evt *evt;
+	bool update_filter;
 
 	if (!hci)
 		return 0;
 
+	/* Check if event already has a handler registered */
+	update_filter = !queue_find(hci->evt_list, match_evt_event,
+							UINT_TO_PTR(event));
+
 	evt = new0(struct evt, 1);
 	evt->event = event;
 
@@ -791,7 +804,8 @@ unsigned int bt_hci_register(struct bt_hci *hci, uint8_t event,
 		return 0;
 	}
 
-	update_evt_filter(hci);
+	if (update_filter)
+		update_evt_filter(hci);
 
 	return evt->id;
 }
@@ -849,6 +863,7 @@ static bool match_evt_id(const void *a, const void *b)
 bool bt_hci_unregister(struct bt_hci *hci, unsigned int id)
 {
 	struct evt *evt;
+	uint8_t event;
 
 	if (!hci || !id)
 		return false;
@@ -857,9 +872,12 @@ bool bt_hci_unregister(struct bt_hci *hci, unsigned int id)
 	if (!evt)
 		return false;
 
+	event = evt->event;
 	evt_free(evt);
 
-	update_evt_filter(hci);
+	/* Only update filter if no other handler for this event remains */
+	if (!queue_find(hci->evt_list, match_evt_event, UINT_TO_PTR(event)))
+		update_evt_filter(hci);
 
 	return true;
 }
@@ -871,10 +889,15 @@ unsigned int bt_hci_register_subevent(struct bt_hci *hci,
 				void *user_data, bt_hci_destroy_func_t destroy)
 {
 	struct evt *evt;
+	bool update_filter;
 
 	if (!hci)
 		return 0;
 
+	/* Check if subevent already has a handler registered */
+	update_filter = !queue_find(hci->subevt_list, match_evt_event,
+						UINT_TO_PTR(subevent));
+
 	evt = new0(struct evt, 1);
 	evt->event = subevent;
 
@@ -892,7 +915,8 @@ unsigned int bt_hci_register_subevent(struct bt_hci *hci,
 		return 0;
 	}
 
-	update_evt_filter(hci);
+	if (update_filter)
+		update_evt_filter(hci);
 
 	return evt->id;
 }
@@ -900,6 +924,7 @@ unsigned int bt_hci_register_subevent(struct bt_hci *hci,
 bool bt_hci_unregister_subevent(struct bt_hci *hci, unsigned int id)
 {
 	struct evt *evt;
+	uint8_t event;
 
 	if (!hci || !id)
 		return false;
@@ -909,9 +934,13 @@ bool bt_hci_unregister_subevent(struct bt_hci *hci, unsigned int id)
 	if (!evt)
 		return false;
 
+	event = evt->event;
 	evt_free(evt);
 
-	update_evt_filter(hci);
+	/* Only update filter if no other handler for this subevent remains */
+	if (!queue_find(hci->subevt_list, match_evt_event,
+							UINT_TO_PTR(event)))
+		update_evt_filter(hci);
 
 	return true;
 }
-- 
2.54.0


^ permalink raw reply related	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2026-06-09 15:20 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-06-08 20:50 [PATCH BlueZ v1 1/2] shared/hci: Avoid redundant BPF filter updates on duplicate events Luiz Augusto von Dentz
2026-06-08 20:50 ` [PATCH BlueZ v1 2/2] shared/hci: Debounce SO_ATTACH_FILTER with timeout_add(0) Luiz Augusto von Dentz
2026-06-08 22:15 ` [BlueZ,v1,1/2] shared/hci: Avoid redundant BPF filter updates on duplicate events bluez.test.bot
2026-06-09 15:20 ` [PATCH BlueZ v1 1/2] " patchwork-bot+bluetooth

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox