From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.128.50 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796377775; b=yix46C+TIv0T6iHEmqjGqQXOnRIPjy4el6hy2e2DiRsk/9PTNDjSuwuICvwBSSj8yjQc 7dMUQugY/0t/wlKZsebj8UwDk/9XpGPEjBm8Sve3J6Av7ymobHagTQfG5oKUsRv79M994 pzjTwbYVxo44QTj00szrRZq63UPS3n74JFE2dlrume7SFujWWbpY4kSAOj/swqWD9lU8u 5IqhoI4SdNNEgZO2Kai7LIH35LhXmi7B3bItVx+zSWzwpypmEJTdNIu4xY4eka6JlxwqX R748UYm/VMMZJEBobZ/wG93Jh3wuP+jdeFokYUVnl3XvzFdqiHegC0D2bGW29hdMp2Q== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796377775; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Gm-Message-State:X-Gm-Gg:X-Received:Received:From: X-Google-Original-From:To:Cc:Subject:Date:Message-ID:X-Mailer: MIME-Version:Content-Transfer-Encoding; bh=rhcSOnpod4/n7CheMivCNiN2L4nczXxsMgzVHDWoiyQ=; b=QtUvDIxfXPgZ2amBtercqfq38I1UgVEfphBWhK6bR+ELQWsVvEn8UI2QpDZ8rEkqlSFs geDx++8MoNp9dyUde6bDEUs7GKPDTlXJBeYD17qTV+p9pgwNhR7QGMPsDIKCFonCcUdMc /FNdVOvFz+4FQegrLK2SC9i9xXD9PfjAVnVvNBm2eOOW1nz5ZN6zrEjL8/H+k5iTQo9c0 quT1FzRwHcsKw5u3ozhEAYufohYgsb7JJnY67sG9qkhK3q5nGyAk63BDMA7AA3JbFAt8i eiQW5ZLpcm8WsqhgsfL36xXW8qFJOQiyl6Ox8qEwg7xaoWCN0LW9JWoGZ3WWhf+HRGg== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=CxQ563b5; arc=none smtp.remote-ip=209.85.128.50 Received: from mail-wm1-f50.google.com (mail-wm1-f50.google.com [209.85.128.50]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id D298B606A332 for ; Fri, 4 Dec 2026 09:49:34 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org D298B606A332 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=CxQ563b5 Received: by mail-wm1-f50.google.com with SMTP id 5b1f17b1804b1-4bb5b8ef642so6558875e9.1 for ; Fri, 04 Dec 2026 01:49:34 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1796377773; x=1796982573; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to:content-type; bh=rhcSOnpod4/n7CheMivCNiN2L4nczXxsMgzVHDWoiyQ=; b=CxQ563b5VoQCRFfT/4IJQkOt+53+S4FZfhR6mgRAaZrTSoTsMCf6DDrBsejJmmvZiz TbLwaaC2IVMyqY+6OzYHWymGVP0x91IiBRnBLg3cE57lo99tu5fSknWFBpPIhGIEFQTw ZN2yvZOWj+N9O522zEn6T83YW5+xbjOkmXpbwe6N9+xc4jfpgqAax3nR+61oLkc7aIVf XVtruguOpkqYg8W9Fb3OLEBK0nfxE8kFD590lrkK8BO4ra5j9oKaR0htjH9D/xlkwGJM W4tkshYWV//64HDsO0mjxyIPmCNNwrlZOe1UkybRlunxJETylAjPtxIiaRnrn52gICYo L8uw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796377773; x=1796982573; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-gg:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to:content-type; bh=rhcSOnpod4/n7CheMivCNiN2L4nczXxsMgzVHDWoiyQ=; b=sNHNV9wijWQf+O9RqMwZrmh/pElDj1xfA5egCZF2g/o9XEnA0NC+yU+ft+8cckhM3c HGnKzek3q49iB3WgTaN5x6cLFkS52hVNX0mJyt8o9GTwsWQ/0rv4uuV4lqv1iz6Ld0KT +dkkRAxPrA6XELvla9/Xhw+rc3WKCcqtAOADMgDNzarQ2NlXkA/XdcgeHPzFD1PXUUTn xUf7pezTzHm809FKe21rd3QPbbJaU/hru+YuI5fFJCh9/fpuQMvO0UzlPVTz6b2wWXYW 1N3E/6rgUIk8eLONsFbKtwwNKSCzqR2WCcpn4Cpo0elD6zU1zdzxZtKH4XbdQ+ftp8sa olmQ== X-Gm-Message-State: APuU4Z/+86Ri7ZMgivcUU3n0lkZlj7z3dQt4tKQJsLsW1N5TPfyAIIIA Bs3gDPaynK1NSDRgrtUOA+e/PYRzYZhH0SpitG0iTgJym9n8wa+jACaYsuOKZA== X-Gm-Gg: AZFdTZencUQpnNJRcpPFss6U2/LQwpDRd0huAx3CzJh6QrdgKWhuAFsDWAc0N8BawNc gfNGaQn4OssEDuKA88gwxq1v2r0c1SO34vzC1d2BvmpwKwgJZlPmdnS6EPKijvw0RxSRKxp36Z0 2+2eEC+bx95+68P8gf7qIlbMmSwPR9Evnq/WhCyQ4VkqCgCLKBEp5HsLlK8GlfzjZp1h3bYJTDq mqOmo7d6noqz/U2Jhl2kJ37SosfGfnyZ6niOKcAaHfK6wHBOa7nqJglsKrjJvyaYUkgOFXoMJJQ l5B841FtyT+nhC7k0DnrcQ30xecNAEY3ZOs5aulbr4xDsLn1VbPo4qgAoSbOQf9/1fIW8BM2IGe ZSeqbRoxgSm0aiOOl/Gx1XcjJXn4ZekAPXT+l5RPMgHXGQjkLglzFb8XbYFj049JFBefeR5KXg+ zojs08kf2RW+JYTzngfmKXT7fyIIgSete92TTqUUf8G9oTzGI6xNeRN88fiwUE5J26yjIAJE4II AbohHRemd9BsjvAq4sQBlzgnFAQeORBZsrHFzGuDaDNRr+x8eRndYn0BnfPSm6n+lG2vb1eix6R 7njAxGtrdSM2LA8KeULaXJJhJtWtyFwB0/OoKi0jhat1danlK6cNRnyAzIeSbi9QGRitV5JLuVb ESqNl3vDHx347lKAY15IvFmoWNn6OtRfi4DqmJDZmaxQ3/Eliqhpo9leEoERVZPHSAIuW5bWb2N YNBVwcpOYVojd+S5EBgp3nTdeG6rlZXez1AseSdHtXJft/lTSK7y+UFA== X-Received: by 2002:a05:600c:8b85:b0:4bb:e377:77c1 with SMTP id 5b1f17b1804b1-4bbeb6ec5d1mr24962785e9.1.1796377773173; Fri, 04 Dec 2026 01:49:33 -0800 (PST) Received: from localhost.localdomain (vc-gp-n-105-242-225-124.umts.vodacom.co.za. [105.242.225.124]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-4bbe00890f4sm147040355e9.8.2026.12.04.01.49.31 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Fri, 04 Dec 2026 01:49:32 -0800 (PST) From: "C.Lee Taylor" X-Google-Original-From: "C.Lee Taylor" To: linux-bluetooth@vger.kernel.org Cc: "C.Lee Taylor" Subject: [PATCH BlueZ] device: Don't rewrite unchanged cache on removal Date: Fri, 4 Dec 2026 11:49:26 +0200 Message-ID: <20261204094926.24428-1-clinton.lee.taylor@gmail.com> X-Mailer: git-send-email 2.56.1 X-Mailing-List: linux-bluetooth@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit When a temporary device is removed (remove_temporary_devices() once discovery has stopped), device_remove_stored() loads cache/, 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/cache/.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