Linux bluetooth development
 help / color / mirror / Atom feed
* [PATCH BlueZ] device: Don't rewrite unchanged cache on removal
@ 2026-12-04  9:49 C.Lee Taylor
  2026-12-04 17:17 ` [BlueZ] " bluez.test.bot
  0 siblings, 1 reply; 2+ messages in thread
From: C.Lee Taylor @ 2026-12-04  9:49 UTC (permalink / raw)
  To: linux-bluetooth; +Cc: C.Lee Taylor

When a temporary device is removed (remove_temporary_devices() once
discovery has stopped), device_remove_stored() loads cache/<peer>,
removes the ServiceRecords, Attributes and Endpoints groups and writes
the file back with g_file_set_contents(), even when none of those groups
existed. That is the normal case for a device that was only seen while
discovering: its cache file holds just the cached name. Each write is a
temporary file, fsync(), a rename and a directory fsync() on the main
loop, once per expiring device.

On slow storage, such as a microSD card, one fsync() can take most of a
second, and nothing else runs on the main loop meanwhile. On a Raspberry
Pi 4, HID over GATT input reports from a connected gamepad were held
back for up to 0.95 s and then delivered to uhid in one burst. strace -T
of bluetoothd during one such stall, removing an unrelated nearby device
that was never connected:

  fsync(28</var/lib/bluetooth/<adapter>/cache/<peer>.WFDQZ3>) = 0 <0.677583>

Only rewrite the file when a group was actually removed. An empty file
is still unlinked as before.
---
Notes (not part of the commit):

Tested with current master (71f224fd6) on a Raspberry Pi 4 (arm64,
Raspberry Pi OS trixie, kernel 6.18), with a script that scans for 15 s,
waits for the temporary devices to expire, records which cache files
were rewritten with unchanged content (new inode, same bytes) and pings
bluetoothd over D-Bus every ~50 ms. Writes to cache/ were slowed to
100 ms with dm-delay to stand in for a slow card:

                     expired w/ cache   rewritten   cache fsyncs   worst ping
  master                      2             2             4          692 ms
  master + this patch         5             0             0           43 ms

With the real microSD card (no dm-delay), in a HoG input test sending
~165 reports per session: 3/20 sessions had input held back with the
stock 5.82 package, 0/20 with this change applied to 5.88 (and 0/20
with cache/ on tmpfs, no patch). The script is at
https://github.com/LeeNX/ESP32-BLE-Gamepad-HIL/blob/main/tester/bluez-cache-fsync-repro.sh

Other cache/ writes still happen on the main loop (the cached name when
a device is found, GATT attributes after discovery); this only drops
the write that changes nothing. Skipping the cache store for temporary
devices altogether would also work, if that is preferred.

I found and tested this with the help of an AI coding assistant; I have
reviewed the change and the results myself.

 src/device.c | 17 +++++++++++------
 1 file changed, 11 insertions(+), 6 deletions(-)

diff --git a/src/device.c b/src/device.c
index 720b4af69..27b7d6709 100644
--- a/src/device.c
+++ b/src/device.c
@@ -5590,6 +5590,7 @@ static void device_remove_stored(struct btd_device *device)
 	GError *gerr = NULL;
 	char *data;
 	gsize length = 0;
+	gboolean changed = FALSE;
 
 	if (device->bredr_state.bonded)
 		device_remove_bonding(device, BDADDR_BREDR);
@@ -5620,20 +5621,24 @@ static void device_remove_stored(struct btd_device *device)
 		g_key_file_free(key_file);
 		return;
 	}
-	g_key_file_remove_group(key_file, "ServiceRecords", NULL);
-	g_key_file_remove_group(key_file, "Attributes", NULL);
-	g_key_file_remove_group(key_file, "Endpoints", NULL);
+	changed |= g_key_file_remove_group(key_file, "ServiceRecords", NULL);
+	changed |= g_key_file_remove_group(key_file, "Attributes", NULL);
+	changed |= g_key_file_remove_group(key_file, "Endpoints", NULL);
 
 	data = g_key_file_to_data(key_file, &length, NULL);
-	if (length > 0) {
+	if (length == 0) {
+		unlink(filename);
+	} else if (changed) {
+		/* Only rewrite the file if a group was removed: a device that
+		 * was only seen while discovering has nothing to drop, and
+		 * g_file_set_contents() fsyncs on the main loop.
+		 */
 		create_file(filename, 0600);
 		if (!g_file_set_contents(filename, data, length, &gerr)) {
 			error("Unable set contents for %s: (%s)", filename,
 								gerr->message);
 			g_error_free(gerr);
 		}
-	} else {
-		unlink(filename);
 	}
 
 	g_free(data);
-- 
2.56.1


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

end of thread, other threads:[~2026-12-04 17:17 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-04  9:49 [PATCH BlueZ] device: Don't rewrite unchanged cache on removal C.Lee Taylor
2026-12-04 17:17 ` [BlueZ] " bluez.test.bot

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