From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-wm1-f54.google.com (mail-wm1-f54.google.com [209.85.128.54]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 7BE552C3255 for ; Tue, 17 Feb 2026 20:09:05 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.54 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1771358946; cv=none; b=tIVe6r/YVLRtEwkVjpO8Isb9QsdRN2YxUj/6ZG/2cC6B1bWm55njOBOl6i7YPxM0UyObA/7j7a+x/4wN/nuOZEV8GBNW2miljHTwxr8Yv7nuv1HvZXiPJbNYXhDFh9adFw7haPcQVDh+OENVl5CxCEUPc1PNTZnWiBFIgILmUC0= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1771358946; c=relaxed/simple; bh=o0vbhrwEPUW5Jv+ySCaUz0Lh9SolmQw6jDSes2hfZz8=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=K1k8i9X50LoE4gFxibKxZF8Ny+PcqTaAWv7LP6bgadfmdS9q1rk5yQ0KgOMJgiMT2yJqSfZ8gTy50+ycQ3zOxaokq5uzpAX8xBNgW2fzf5y/OF1LtfdY10Z3kkD6WIKDoc6ChOmqk6a71h+00+3XAp6MSwt3b9YIVqHmM+G5tJU= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=JjMJE5kG; arc=none smtp.client-ip=209.85.128.54 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="JjMJE5kG" Received: by mail-wm1-f54.google.com with SMTP id 5b1f17b1804b1-483703e4b08so1505915e9.1 for ; Tue, 17 Feb 2026 12:09:05 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1771358944; x=1771963744; darn=vger.kernel.org; h=in-reply-to:content-transfer-encoding:content-disposition :mime-version:references:message-id:subject:cc:to:from:date:from:to :cc:subject:date:message-id:reply-to; bh=pbY7O8xAwkUDKQr6iB+JZ3Qx3bRNJ6Xv4CAEj2PrjMA=; b=JjMJE5kG3UKDLfyDb9aJyO+OBY1yx9ROCj4j4IRJm9ghvu+K09q3LSE+U2RjwRaQM3 0Cq3G8Dri9dxsILtbAYXT0SntZNHizvE+uBlAbZQJuP8NNDBrxSOpQ+NbV4uXTsqxVxO lEdd5pFRJ00mnnesNVzQiCpQ+MD3+DOHH8NBAMSrZITaYoTQwbYFJF922lIhMNVlnBqq w1CVIq95cJkWr+9iQGxGxUuHpW3MR8UxI7a1owq/5H4CqaTq+ffczd70W9HWRXu3Mdw6 N3fhkQDyF4CTkj8KRAioYowQ1/kEiznDtysefQBYseGNN2KCvHd2HTin3Xh9PGkZZqP0 48MA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1771358944; x=1771963744; h=in-reply-to:content-transfer-encoding:content-disposition :mime-version:references:message-id:subject:cc:to:from:date:x-gm-gg :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=pbY7O8xAwkUDKQr6iB+JZ3Qx3bRNJ6Xv4CAEj2PrjMA=; b=ulHnY6I64aY96y0hW3uz+EL6IucFya33es99GsJE+SMjyuJd3OA+dSPETY4HTHwRim nNn/e/TaHs2MXzGSpQDwi4zDnPXwe8CSuCa0uJ7HvgRqmti5cF9fa+CvHIaaRlwQMij6 At5yf6/4vZD2chpO/LVnRmTLb/By8sIu/UnNUxze3f14ESydP2hhUG032WWYPSS9285c U6+iviZ4evQlKjvtMNzhgjKwFeqnwio432d843Dg+s4u+YN4Mqa/f/EDbOFkdmgijKw/ 4CbK+vOMqUknOxn44Zhnkb3u6ohB9RGPWBvAfNrFFc00bzut0OF6KR5U9Ms/P/l4omVD 23dg== X-Forwarded-Encrypted: i=1; AJvYcCWiFAl9n3li2SfWOey3t8FXbqpPKjZO82j33j7pBYQ6tYdanrAK2qjoqGh6KsFom1ec672hbGWkGDv5OWs=@vger.kernel.org X-Gm-Message-State: AOJu0YxUekg6EhqqAB28S473/g1y/oJt/MBIwfDH+RQR87/di4wi6SfR 4WjydkwQo+M9Rb3X2OKQhplpwBqHLKQ0L3u87uyCiBy0II9tgcQ4mcxyBf1InovAAA== X-Gm-Gg: AZuq6aLLkrJQBVNQwfhesTRm9Q1HYmdxgCZTOhCDb+LeBIAsolbYFRj1cdIaXAP02aO 124WhP4tTcrl4a3Ko68dWkP8N+sihRe5qiqCVfyK/Pq+QiMj6JwJoJnrZ5WQ2hLTBtKzVteMFo7 dKiyy8NLyGIseoh4TQuyzA9+XjkURUoj2945BZ34no8D8VxM+6xf1NT/HqRuTgeaRZB5pUNUgN2 4mY/LRin2UjSfsxPTYTVBkVdkDoOr5bHTwIe+7G/PaITLUByiNS+0Dlz7jUrGDHjmTCWvBaKgxQ DSagqvkK6Hgs1CInrEFOhAK8IlgkK1kyiB+FaTMeWDdUmo1azWEuSuJrxyVVMJD8I1LcLkP5Mtc +h3V6cM6/oi86Ox+YiQe7KM8QD9v1JmprNFhbZHe71wsQYGn+hdf/q0j+xIdeQazt60aZiZpD+S L4JnNuQ0nX5V9GOlBnEc+bLuOixR3Nl8j7Urh7AQBUeur0XGk3ztXjdA== X-Received: by 2002:a05:600c:3e05:b0:480:4a90:1afd with SMTP id 5b1f17b1804b1-4838dbae89emr77350015e9.0.1771358943406; Tue, 17 Feb 2026 12:09:03 -0800 (PST) Received: from google.com ([2a00:79e0:288a:8:8d29:f905:4a47:1dbf]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-4836aa0847asm587397615e9.3.2026.02.17.12.09.02 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 17 Feb 2026 12:09:02 -0800 (PST) Date: Tue, 17 Feb 2026 21:08:57 +0100 From: =?utf-8?Q?G=C3=BCnther?= Noack To: Benjamin Tissoires Cc: Jiri Kosina , linux-input@vger.kernel.org, linux-kernel@vger.kernel.org Subject: Re: [PATCH 0/3] HID: Fix some memory leaks in drivers/hid Message-ID: References: <20260217160125.1097578-1-gnoack@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: Hello! On Tue, Feb 17, 2026 at 07:36:46PM +0100, Benjamin Tissoires wrote: > On Feb 17 2026, Günther Noack wrote: > > These patches fix a few memory leaks in HID report descriptor fixups. > > > > FWIW, a good ad-hoc way to look for usages of allocation functions in > > these is: > > > > awk '/static.*report_fixup.*/,/^}/ { print FILENAME, $0 }' drivers/hid/hid-*.c \ > > | grep -E '(malloc|kzalloc|kcalloc|kmemdup)' > > > > The devm_* variants are safe in this context, because they tie the > > allocated memory to the lifetime of the driver. > > No. Look at hid_close_report() in drivers/hid/hid-core.c. > > HID still hasn't fully migrated to devm, so as a rule of thumb, if you > change a kzalloc into a devm_kzalloc, you are getting into troubles > unless you fix the all the kfree path. OK, I have not verified where the devm-allocated objects get freed up. If devm_*() is not possible here, then the drivers hid-asus.c and hid-gembird.c have two additional memory leaks, because they do that. $ awk '/static.*report_fixup.*/,/^}/ { print FILENAME, $0 }' drivers/hid/hid-*.c | grep -E 'alloc' drivers/hid/hid-asus.c new_rdesc = devm_kzalloc(&hdev->dev, new_size, GFP_KERNEL); drivers/hid/hid-gembird.c new_rdesc = devm_kzalloc(&hdev->dev, new_size, GFP_KERNEL); (That is without my threee patches) > > For transparency, I generated these commits with Gemini-CLI, > > starting with this prompt: > > > > We are working in the Linux kernel. In the HID drivers in > > `drivers/hid/hid-*.c`, the `report_fixup` driver hook is a function > > that gets a byte buffer (with size) as input and that may modify that > > byte buffer, and optionally return a pointer to a new byte buffer and > > update the size. The returned value is *not* memory-managed by the > > caller though and will not be freed subsequently. When the > > If the memory is *not* managed, why would gemini converts kzalloc into > devm variants without changing the kfree paths???? I'm not sure I understand the question, it's not clear to me what you mean by the "kfree paths". I have seen usages of devm in other HID drivers and I was under the impression that devm_* allocations would work in the HID subsystem to allocate objects which are then freed automatically at a later point when the device gets removed. Is that inaccurate? > > `report_fixup` implementation allocates a new buffer and returns that, > > that will not get freed by the caller. > > This is wrong. See hid_close_report(): if the new rdesc (after fixup) > differs from the one initially set, there is an explicit call to > kfree(). > > -> there is no memleak AFAICT, and your prompt is wrong. See my discussion in [1]. The pointer returned by report_fixup() is immediately discarded in the position marked with (4). This is still in the hid_open_report() function where the leak happens. Let me know whether this makes sense. I'm happy to be corrected, but so far, I still have the feeling that my reasoning is sound. —Günther [1] https://lore.kernel.org/all/aZTEnPEHcWEkoTJR@google.com/