From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796228331; b=GrM071nnQpjetd1qX88p2OlzJv+8QpaRZ+o8COHVDSXPfdlBcX1A1dY+75PupUFKwq44 PAwX2ry9SYOz+g5m7YTDJHItt0TrilAiCcx+J6fk+U9mHJIrIhu5wK+G8MIsh+jcP5YAX BxXBPNjYEhIJ4/3aVWSmMkPBVXGssxSNaehns6NZdiADXjy/U2J8JPo2cIArdKO+F56qv os47jXZnoV11BPvHZCThF5nk0tR5AhwzmaXcRI7cxncfCS3C9O9psmIeSCsWAnKIwE2Z1 +YccODSd7oGwzLSU2Q/TvOAaf9xCry4xZEkHek6Koh680fmcp9X3SnytxOHJtokJe8w== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796228331; h=Received:DKIM-Signature:Date:From:To:Cc:Subject:Message-ID: References:MIME-Version:Content-Type:Content-Disposition: Content-Transfer-Encoding:In-Reply-To; bh=opdZ5IbsMxdhebtSs2YmZoxUvaFI/8xRI/U7w8gXI/w=; b=3dIYIJu9754PN+8fJmbfpZ+aQbvkfJ8jw+mb+d7ESVBQTFkClbx40L/GekA9wCzRGRot ca7wsxbgk1z+Ty3BhZIwN4wczURIvLZmcb3Px9H3bswyumpPO6ONuPEXOf0A/n622Hjl0 PB9wLMp/IsupM34xZC1JRyqYKgVOMI3CgIJPNS9V5b0JX6qTI9JD63r5z9tACNVZWKE0E twayFfslw2YISunVQQSM9TSU69sMi8z9Ipz9v9vRBCwun8x9Obko+A2OGm0X9zjGUpm/G z0UJbj4Lpv1pM/vC4w3g5JHgu+akZG8grx2bdB1/jcox44Ya2Crzpd4T2enohJoTSwg== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=J1znfGtF; arc=none smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id CA4F1606A31A; Wed, 2 Dec 2026 16:18:51 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=J1znfGtF Received: by smtp.kernel.org (Postfix) with ESMTPSA id 6FFF91F00896; Wed, 2 Dec 2026 16:18:50 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796228331; bh=opdZ5IbsMxdhebtSs2YmZoxUvaFI/8xRI/U7w8gXI/w=; h=Date:From:To:Cc:Subject:References:In-Reply-To; b=J1znfGtF71gtb0o0qFfXuco90uvsOfsMF+cq+jR2/oZpBkLuABi1BUmZ6EX4gqlQG NF5eukCaNEOkuIJXdFaUm/CyhLod8SN5l1MFSzk58RC7hnSpV/OrMRabKDwbz0WKuk WeBkr7qiVtsmmCGJr8WTazn05WPrqyLA1m3fXZ9CRiu0fyDRf3A/8myrSA5z/G66TP iWIJJrnjbKEZyd4GBTkwf9llYEo2n8edo686r3Q8Z6PviUKDOkS5XUaZfsNWfQVM2d NCRE1qq9BF0AZN+zFinvo1A9xi3kD36qSMx1NNdvp2Nzp67A/8mx6JHsHpTvaC/QeF 4zQfWdxcXBQkw== Date: Wed, 2 Dec 2026 16:18:47 +0000 From: Lee Jones To: =?utf-8?B?Tmd1eeG7hW4gQ8O0bmc=?= Cc: Pavel Machek , Luca Weiss , linux-leds@vger.kernel.org, linux-kernel@vger.kernel.org Subject: Re: [PATCH] leds: sgm3140: Put the child node reference on success Message-ID: <20261202161847.GI43081@kernel.org> References: <20261125134013.2692510-1-congnt264@gmail.com> <179568529013.32537.1835646355373713109@kernel.org> X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: On Thu, 26 Nov 2026, Nguyễn Công wrote: > On Thu, Nov 26, 2026 at 7:41 PM Lee Jones wrote: > > > > On Wed, 25 Nov 2026, Cong Nguyen wrote: > > > > > sgm3140_probe() gets the LED child node with device_get_next_child_node(), > > > which returns it with its reference count raised. The error path puts the > > > reference, but a successful probe returns before reaching that code, so the > > > reference is never dropped. The v4l2 flash sub-device takes its own > > > reference on the node, and the LED class device only stores the pointer, > > > > As the commit message notes, devm_led_classdev_flash_register_ext() > > stores 'init_data->fwnode' in 'led_cdev->dev->fwnode' without taking a > > reference on it and led_classdev_unregister() subsequently calls > > fwnode_handle_put(led_cdev->dev->fwnode) when the device is removed. > > Hi Lee, > I could not find that call. led_classdev_unregister() only stops > triggers and blinking, turns the LED off and calls device_unregister(), > and device_create_release() just does kfree(). The only > fwnode_handle_put() in led-class.c is in fwnode_led_get(), for the node > it looked up itself. > > > > > So 'led_cdev->dev->fwnode' takes ownership of the caller's 'child_node' > > reference on successful registration. Dropping 'child_node' here on the > > success path causes a reference underflow / use-after-free while the LED > > device is registered and a double-put on unbind. > > > > In fact, there is already a bug on the error path if v4l2_flash_init() > > fails after devm_led_classdev_flash_register_ext() has succeeded: 'goto > > err' drops 'child_node' once, and then devres teardown calls > > devm_led_classdev_release() -> led_classdev_unregister() which calls > > fwnode_handle_put() a second time! > > v4l2_flash_init() takes its own reference on the node and puts it on > its error path, so the one 'goto err' drops is the probe's. Other > drivers drop their reference right after registering, for example > leds-lt3593: > > init_data.fwnode = child; > ret = devm_led_classdev_register_ext(dev, &led_data->cdev, &init_data); > fwnode_handle_put(child); > > Is there a path I am missing? You're right, led_classdev_unregister() doesn't call fwnode_handle_put() directly. Sorry about that. However, the underlying lifetime issue remains. led_classdev_register_ext() assigns: led_cdev->dev->fwnode = init_data->fwnode; led_cdev->dev->of_node = to_of_node(init_data->fwnode); Without calling fwnode_handle_get(). Those pointers remain live on 'led_cdev->dev' for the entire lifetime of the LED device (and are dereferenced by triggers, sysfs, fwnode_led_get() consumers, etc.). If the caller drops its reference as soon as probe() returns, 'led_cdev->dev->fwnode' is left holding an unreferenced pointer (in this driver v4l2_flash_init() happens to take its own reference, but that is dropped in v4l2_flash_release() before devm unregisters the LED device, and when CONFIG_V4L2_FLASH_LED_CLASS is disabled v4l2_flash_init() is a no-op stub that takes no reference at all). That said, since LED core currently neither takes nor releases a reference on 'init_data->fwnode', virtually every other LED driver drops its 'child' reference after registration just as you pointed out with leds-lt3593. Fixing 'led-class' to properly take and release a reference in led_classdev_register_ext() / led_classdev_unregister() would be needed if we wanted 'led_cdev->dev' to own one, and until then the patch does match the existing practice across the subsystem. -- Lee Jones