Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1760138AbcKDHw4 (ORCPT ); Fri, 4 Nov 2016 03:52:56 -0400 Received: from mailout2.samsung.com ([203.254.224.25]:49362 "EHLO mailout2.samsung.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1753956AbcKDHwz (ORCPT ); Fri, 4 Nov 2016 03:52:55 -0400 X-AuditID: cbfee61a-f79916d0000062de-35-581c3e54c565 From: Jacek Anaszewski To: linux-leds@vger.kernel.org Cc: linux-kernel@vger.kernel.org, Jacek Anaszewski , Hans de Goede , Sakari Ailus , Pavel Machek , Andrew Lunn Subject: [PATCH] leds: Add mutex protection in brightness_show() Date: Fri, 04 Nov 2016 08:52:42 +0100 Message-id: <1478245962-15706-1-git-send-email-j.anaszewski@samsung.com> X-Mailer: git-send-email 1.9.1 X-Brightmail-Tracker: H4sIAAAAAAAAA+NgFrrJLMWRmVeSWpSXmKPExsVy+t9jAd0QO5kIgwerTSzO3z3EbPHm+HQm i96rzxktLu+aw2ax9c06Rou7p46yWXza8o3Jgd1j3slAj507PjN5vN93lc2jb8sqRo8Vq7+z e3zeJBfAFuVmk5GamJJapJCal5yfkpmXbqsUGuKma6GkkJeYm2qrFKHrGxKkpFCWmFMK5BkZ oAEH5wD3YCV9uwS3jHnnJ7MULOKqONBxmK2B8SpHFyMnh4SAicTy6zdYIWwxiQv31rN1MXJx CAksZZTYf+csI4Tzk1Hi29MOdpAqNgFDiZ8vXjN1MXJwiAjISew8UwlSwyzwgFHi2vqVYDXC Ag4Sn15/BbNZBFQleh7fYAOxeQU8JPZt/sgGsU1O4uSxyawTGLkXMDKsYpRILUguKE5KzzXM Sy3XK07MLS7NS9dLzs/dxAgO1mdSOxgP7nI/xCjAwajEw7uDQSZCiDWxrLgy9xCjBAezkggv uxVQiDclsbIqtSg/vqg0J7X4EKMp0AETmaVEk/OBkZRXEm9oYm5ibmxgYW5paWKkJM7bOPtZ uJBAemJJanZqakFqEUwfEwenVAPjEmZr71+Fj49MjpqWnyF/do/K1iXunm92u4q6rxKItD/C s+T2+9yqL6dmvviQNP9Of31SusF5Rb8TX8O/T2m/faY3gNll9bbafated0ZUvfyff1lUYj3H 47lKMR8TP3xqflTmdM2PS98+hPfMHq29E5imnvnKcH6riOmE9KKzys5thxwW/HyVosRSnJFo qMVcVJwIAMaE3XhsAgAA X-MTR: 20000000000000000@CPGS Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Length: 1277 Lines: 35 Initially the claim about no need for lock in brightness_show() was valid as the function was just returning unchanged LED brightness. After the addition of led_update_brightness() this is no longer true, as the function can change the brightness if a LED class driver implements brightness_get op. It can lead to races between led_update_brightness() and led_set_brightness(), resulting in overwriting new brightness with the old one before the former is written to the device. Signed-off-by: Jacek Anaszewski Cc: Hans de Goede Cc: Sakari Ailus Cc: Pavel Machek Cc: Andrew Lunn --- drivers/leds/led-class.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/drivers/leds/led-class.c b/drivers/leds/led-class.c index 731e4eb..0c2307b 100644 --- a/drivers/leds/led-class.c +++ b/drivers/leds/led-class.c @@ -30,8 +30,9 @@ static ssize_t brightness_show(struct device *dev, { struct led_classdev *led_cdev = dev_get_drvdata(dev); - /* no lock needed for this */ + mutex_lock(&led_cdev->led_access); led_update_brightness(led_cdev); + mutex_unlock(&led_cdev->led_access); return sprintf(buf, "%u\n", led_cdev->brightness); } -- 1.9.1