All of lore.kernel.org
 help / color / mirror / Atom feed
From: "C.Lee Taylor" <clintonlee.taylor@gmail.com>
To: linux-bluetooth@vger.kernel.org
Cc: "C.Lee Taylor" <clinton.lee.taylor@gmail.com>
Subject: [PATCH BlueZ] device: Don't rewrite unchanged cache on removal
Date: Fri,  4 Dec 2026 11:49:26 +0200	[thread overview]
Message-ID: <20261204094926.24428-1-clinton.lee.taylor@gmail.com> (raw)

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


             reply	other threads:[~2026-12-04  9:49 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-04  9:49 C.Lee Taylor [this message]
2026-12-04 17:17 ` [BlueZ] device: Don't rewrite unchanged cache on removal bluez.test.bot
2026-12-05  6:29 ` [PATCH BlueZ v2] " C.Lee Taylor
2026-12-05 14:42   ` [BlueZ,v2] " 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=20261204094926.24428-1-clinton.lee.taylor@gmail.com \
    --to=clintonlee.taylor@gmail.com \
    --cc=clinton.lee.taylor@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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.