From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 456F2C19F2A for ; Mon, 8 Aug 2022 01:58:55 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S243730AbiHHB6v (ORCPT ); Sun, 7 Aug 2022 21:58:51 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52130 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S243814AbiHHB4F (ORCPT ); Sun, 7 Aug 2022 21:56:05 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 840F4F5B7; Sun, 7 Aug 2022 18:39:38 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id 1F8E560DF3; Mon, 8 Aug 2022 01:39:38 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id A05D7C433D6; Mon, 8 Aug 2022 01:39:36 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1659922777; bh=M1AyWmAGk+oh66zy1o1YHkgCt5uJYmz4Xr4V5wi3H8k=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=S1EVOo4d0gpi5dXf9hdXwESMyFNXgtWzOI2KakTIC/D8D2fY5mUsyt2iD9PcbhhjQ 5o5WVv8t/EeVKQj/740dPEtHfpwxh58ScECst7B89hovwdgj9gXho8PvNUo1R+NmAH 4meo5uVzi8SVkuUqEIDRwkr0XqVqsIpxWk3x+9lh2W4fUYQbSjJv2fqGYou8Go65Dn hR+NhIZj6sMi338Z/lnwUZ/ohj1BX/hIxFYdAJxwwVVE/lyG4RWQI8eZNTsp1Xfl9t gCKHyZVyhk1uQ2fOv0k1/YMwEq9/i6f30eq2uGjHwjS/yyuLU/YpMeRAEUYcmFSYNu BS8nN4YWZe4eg== From: Sasha Levin To: linux-kernel@vger.kernel.org, stable@vger.kernel.org Cc: =?UTF-8?q?Uwe=20Kleine-K=C3=B6nig?= , Guenter Roeck , Sasha Levin , jdelvare@suse.com, linux-hwmon@vger.kernel.org Subject: [PATCH AUTOSEL 4.19 14/16] hwmon: (sht15) Fix wrong assumptions in device remove callback Date: Sun, 7 Aug 2022 21:39:11 -0400 Message-Id: <20220808013914.316709-14-sashal@kernel.org> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20220808013914.316709-1-sashal@kernel.org> References: <20220808013914.316709-1-sashal@kernel.org> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 X-stable: review X-Patchwork-Hint: Ignore Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Uwe Kleine-König [ Upstream commit 7d4edccc9bbfe1dcdff641343f7b0c6763fbe774 ] Taking a lock at the beginning of .remove() doesn't prevent new readers. With the existing approach it can happen, that a read occurs just when the lock was taken blocking the reader until the lock is released at the end of the remove callback which then accessed *data that is already freed then. To actually fix this problem the hwmon core needs some adaption. Until this is implemented take the optimistic approach of assuming that all readers are gone after hwmon_device_unregister() and sysfs_remove_group() as most other drivers do. (And once the core implements that, taking the lock would deadlock.) So drop the lock, move the reset to after device unregistration to keep the device in a workable state until it's deregistered. Also add a error message in case the reset fails and return 0 anyhow. (Returning an error code, doesn't stop the platform device unregistration and only results in a little helpful error message before the devm cleanup handlers are called.) Signed-off-by: Uwe Kleine-König Link: https://lore.kernel.org/r/20220725194344.150098-1-u.kleine-koenig@pengutronix.de Signed-off-by: Guenter Roeck Signed-off-by: Sasha Levin --- drivers/hwmon/sht15.c | 17 ++++++----------- 1 file changed, 6 insertions(+), 11 deletions(-) diff --git a/drivers/hwmon/sht15.c b/drivers/hwmon/sht15.c index 2be77752cd56..0a4578aae85b 100644 --- a/drivers/hwmon/sht15.c +++ b/drivers/hwmon/sht15.c @@ -1029,25 +1029,20 @@ static int sht15_probe(struct platform_device *pdev) static int sht15_remove(struct platform_device *pdev) { struct sht15_data *data = platform_get_drvdata(pdev); + int ret; - /* - * Make sure any reads from the device are done and - * prevent new ones beginning - */ - mutex_lock(&data->read_lock); - if (sht15_soft_reset(data)) { - mutex_unlock(&data->read_lock); - return -EFAULT; - } hwmon_device_unregister(data->hwmon_dev); sysfs_remove_group(&pdev->dev.kobj, &sht15_attr_group); + + ret = sht15_soft_reset(data); + if (ret) + dev_err(&pdev->dev, "Failed to reset device (%pe)\n", ERR_PTR(ret)); + if (!IS_ERR(data->reg)) { regulator_unregister_notifier(data->reg, &data->nb); regulator_disable(data->reg); } - mutex_unlock(&data->read_lock); - return 0; } -- 2.35.1