Linux bluetooth development
 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 v2] device: Don't rewrite unchanged cache on removal
Date: Sat,  5 Dec 2026 08:29:26 +0200	[thread overview]
Message-ID: <20261205062926.65953-1-clinton.lee.taylor@gmail.com> (raw)
In-Reply-To: <20261204094926.24428-1-clinton.lee.taylor@gmail.com>

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/.../cache/<peer>.WFDQZ3>) = 0 <0.677583>

Only rewrite the file when a group was actually removed. An empty file
is still unlinked as before.
---
v2: Shorten the strace line in the commit message to fit checkpatch's
    75-column limit; no code changes.

 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


  parent reply	other threads:[~2026-12-05  6:29 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
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
2026-12-05  6:29 ` C.Lee Taylor [this message]
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=20261205062926.65953-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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox