Linux bluetooth development
 help / color / mirror / Atom feed
From: Luiz Augusto von Dentz <luiz.dentz@gmail.com>
To: linux-bluetooth@vger.kernel.org
Subject: [PATCH BlueZ v1 2/2] shared/hci: Debounce SO_ATTACH_FILTER with timeout_add(0)
Date: Mon,  8 Jun 2026 16:50:09 -0400	[thread overview]
Message-ID: <20260608205009.97585-2-luiz.dentz@gmail.com> (raw)
In-Reply-To: <20260608205009.97585-1-luiz.dentz@gmail.com>

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

Coalesce multiple BPF filter updates into a single SO_ATTACH_FILTER
setsockopt call by deferring the update to the next event loop
iteration using timeout_add(0, ...).

When bt_hci_register_event or bt_hci_register_subevent is called
multiple times in succession (e.g. from bt_rap_attach_hci), each call
previously triggered a full filter rebuild and setsockopt. Now,
schedule_evt_filter() simply marks a pending update which fires once
in filter_timeout() after all synchronous registrations complete.
---
 src/shared/hci.c | 34 ++++++++++++++++++++++++++++++----
 1 file changed, 30 insertions(+), 4 deletions(-)

diff --git a/src/shared/hci.c b/src/shared/hci.c
index 1aba7db1d995..e8586ab5014c 100644
--- a/src/shared/hci.c
+++ b/src/shared/hci.c
@@ -31,6 +31,7 @@
 #include "src/shared/io.h"
 #include "src/shared/util.h"
 #include "src/shared/queue.h"
+#include "src/shared/timeout.h"
 #include "src/shared/hci.h"
 
 
@@ -42,6 +43,7 @@ struct bt_hci {
 	uint8_t num_cmds;
 	unsigned int next_cmd_id;
 	unsigned int next_evt_id;
+	unsigned int filter_id;
 	struct queue *cmd_queue;
 	struct queue *rsp_queue;
 	struct queue *evt_list;
@@ -485,6 +487,9 @@ void bt_hci_unref(struct bt_hci *hci)
 	if (__sync_sub_and_fetch(&hci->ref_count, 1))
 		return;
 
+	if (hci->filter_id)
+		timeout_remove(hci->filter_id);
+
 	queue_destroy(hci->evt_list, evt_free);
 	queue_destroy(hci->subevt_list, evt_free);
 	queue_destroy(hci->cmd_queue, cmd_free);
@@ -765,6 +770,27 @@ static void update_evt_filter(struct bt_hci *hci)
 	free(filters);
 }
 
+static bool filter_timeout(void *user_data)
+{
+	struct bt_hci *hci = user_data;
+
+	hci->filter_id = 0;
+	update_evt_filter(hci);
+
+	return false;
+}
+
+static void schedule_evt_filter(struct bt_hci *hci)
+{
+	/* Coalesce multiple filter updates into a single SO_ATTACH_FILTER call
+	 * by deferring the update to the next event loop iteration.
+	 */
+	if (hci->filter_id)
+		return;
+
+	hci->filter_id = timeout_add(0, filter_timeout, hci, NULL);
+}
+
 static bool match_evt_event(const void *a, const void *b)
 {
 	const struct evt *evt = a;
@@ -805,7 +831,7 @@ unsigned int bt_hci_register(struct bt_hci *hci, uint8_t event,
 	}
 
 	if (update_filter)
-		update_evt_filter(hci);
+		schedule_evt_filter(hci);
 
 	return evt->id;
 }
@@ -877,7 +903,7 @@ bool bt_hci_unregister(struct bt_hci *hci, unsigned int id)
 
 	/* 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);
+		schedule_evt_filter(hci);
 
 	return true;
 }
@@ -916,7 +942,7 @@ unsigned int bt_hci_register_subevent(struct bt_hci *hci,
 	}
 
 	if (update_filter)
-		update_evt_filter(hci);
+		schedule_evt_filter(hci);
 
 	return evt->id;
 }
@@ -940,7 +966,7 @@ bool bt_hci_unregister_subevent(struct bt_hci *hci, unsigned int id)
 	/* 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);
+		schedule_evt_filter(hci);
 
 	return true;
 }
-- 
2.54.0


  reply	other threads:[~2026-06-08 20:50 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
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 ` Luiz Augusto von Dentz [this message]
2026-06-08 22:15 ` [BlueZ,v1,1/2] " bluez.test.bot

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20260608205009.97585-2-luiz.dentz@gmail.com \
    --to=luiz.dentz@gmail.com \
    --cc=linux-bluetooth@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox