[6/9] thermal/core: Protect hwmon accesses to thermal operations with thermal zone mutex
Message ID | 20221017130910.2307118-7-linux@roeck-us.net |
---|---|
State | New |
Headers |
Return-Path: <linux-kernel-owner@vger.kernel.org> Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:4ac7:0:0:0:0:0 with SMTP id y7csp1439367wrs; Mon, 17 Oct 2022 06:12:00 -0700 (PDT) X-Google-Smtp-Source: AMsMyM48DjEdx1AT43zheCbja6/t5MIwcrciGmohNA7vYZX6/P54fLjyvRCIMjvPB0Vtz5xYTrgc X-Received: by 2002:a63:fd41:0:b0:438:7670:589a with SMTP id m1-20020a63fd41000000b004387670589amr10622229pgj.148.1666012320242; Mon, 17 Oct 2022 06:12:00 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1666012320; cv=none; d=google.com; s=arc-20160816; b=JJup7WxoBHi4XbL5Z8t4hj0JHRg8EXi1ij2JTadw8dmWSzZyBmsVpGzXw+8Knj8me9 Nl+v7ZswuXvoo4E2/olacvukRomzcpx8iFAco+Qf03kIQkw8kGG6sYw4SnFlVX+3dw1U orcn4YrPhRfEcBvX7QiW+kWDA7AehhOvpnZ/4CQaNDDFUPo8CX7snlhyJQ/PVsN/orF8 b76Fgh5+p79S5khhBGVjiJO4ZO0AGcQdvtnQ44Id7jqfoOmbBQUKgk2yx3Ityc5sGx6w EwyjOomiESeE32aJxPLifBaZTJAnYBR01tmKUc/7OUITsqlvHm3IJFoLn8QgIbVCqDGk KOmA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from:sender :dkim-signature; bh=XxBO71YTPZQhAtFbi9nnTkRa58Zyh2hdgkHI+eeVMDA=; b=Nbu8qfwLyBjbRkTrCPABTS/nMYPjRFCYblf2ZSNU5O7yC67SGLHnLQwHUROuNTKjHc zZIY3QqMhWg+9HrpzyCRWUAR02m1IzgJ2YGId1fuZsaoVYoQZwoD2NhBHHt7+DSEwhgc NWvE+c2ow3Qrurt8VGAqHxZb+iWf+OfoIzkHzVGqLkGM08Fc/LiEhhtRVCWgQrHmOk86 INPTeaTonY0E4qbOpiMKE/K1XEI8PFbh5Drv16Hnp6T3C8AK1f/bgVh8mNUwP1wm82NT GYRwELhtB3dWTNUlq31hMSYWsrsVoLFUSDZMG74ujd359EzyUeSH9B3p4AwJfTu+wW9m snrA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=Nv1MpuRk; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id k8-20020a6568c8000000b0044e74d0e841si12203977pgt.201.2022.10.17.06.11.17; Mon, 17 Oct 2022 06:12:00 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=Nv1MpuRk; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230418AbiJQNJq (ORCPT <rfc822;ouuuleilei@gmail.com> + 99 others); Mon, 17 Oct 2022 09:09:46 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45142 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229993AbiJQNJ1 (ORCPT <rfc822;linux-kernel@vger.kernel.org>); Mon, 17 Oct 2022 09:09:27 -0400 Received: from mail-oi1-x22c.google.com (mail-oi1-x22c.google.com [IPv6:2607:f8b0:4864:20::22c]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E03A0D9F; Mon, 17 Oct 2022 06:09:22 -0700 (PDT) Received: by mail-oi1-x22c.google.com with SMTP id g10so12002030oif.10; Mon, 17 Oct 2022 06:09:22 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:sender:from:to:cc:subject:date :message-id:reply-to; bh=XxBO71YTPZQhAtFbi9nnTkRa58Zyh2hdgkHI+eeVMDA=; b=Nv1MpuRk8XWnN8W4oZzjLoBkpJ5NX7m02MZydTw5nLViCz9tGpw2b2AYNoMVJliJbn XEdocACuUheqF4g6XSJitDgrNFN3NV6KvWMp5Wf0VILlzmSoVgZfJK0QzizXNKE/VXYi EbYMuS3RGIYfhtYlMc2ffrBBPfcfM//IwKMhYAZtEVh6BToqsouD/BzaRdVHACxoqiYu Cs6CbFRCepQFdWkUOB4CsjjBuKmpaJjpLROH4FSlfth9mkmheVXgsInb89wiwfpvpsFD ZDUH+2RAsgxkvfXtM+HlGxQLkLx00R5hUNF5reGf1GNXNE6EHA3CQSixUHiHimbrKVXF sUkw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:sender:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=XxBO71YTPZQhAtFbi9nnTkRa58Zyh2hdgkHI+eeVMDA=; b=TrBsOt9pe5XWnkvmLK5FbSnbRvTubIqdE8nikDhbDOJiDZr/Mh/XFUTN7aBDwuj1zy 2o/B1QBvqCtjupL6xLcRP+9DvHVJsaazQkRZjCRRaTLpP1fP5GXdtZ+2KBsqlW1q6VYh YA0+7iQvVfmqky92a6a67lqhuuJzY3h92+7oH0r96NeCg1cMcep5+7P3cC64+50ZeJ2o 1IzmLPV9bIGJZqi9wGDx/ii60GKMBKcxwiRqYkOOSV44VYvpoGOTvRXaCrwoLB4BFKnk lU5hmr8IrSiWOkIwYcVnwaXm8CwHm7PlghIF4Ez/RDazFvNBlr3F/2A0F3KXujOddMs3 CqHA== X-Gm-Message-State: ACrzQf0csF0PUlrl3/LF4z5hqmRqvgV6ofY7xaxsziLroaKfCUDIXr/x UwS6GObMgT4i+YbWZsDwGXAEVai8lJw= X-Received: by 2002:a05:6808:16ac:b0:353:b7d7:5fef with SMTP id bb44-20020a05680816ac00b00353b7d75fefmr4952009oib.293.1666012162337; Mon, 17 Oct 2022 06:09:22 -0700 (PDT) Received: from server.roeck-us.net ([2600:1700:e321:62f0:329c:23ff:fee3:9d7c]) by smtp.gmail.com with ESMTPSA id d10-20020a4aaa8a000000b0047644a9b49asm4207865oon.19.2022.10.17.06.09.21 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 17 Oct 2022 06:09:22 -0700 (PDT) Sender: Guenter Roeck <groeck7@gmail.com> From: Guenter Roeck <linux@roeck-us.net> To: "Rafael J . Wysocki" <rafael@kernel.org> Cc: Daniel Lezcano <daniel.lezcano@linaro.org>, Amit Kucheria <amitk@kernel.org>, Zhang Rui <rui.zhang@intel.com>, linux-pm@vger.kernel.org, linux-kernel@vger.kernel.org, Guenter Roeck <linux@roeck-us.net> Subject: [PATCH 6/9] thermal/core: Protect hwmon accesses to thermal operations with thermal zone mutex Date: Mon, 17 Oct 2022 06:09:07 -0700 Message-Id: <20221017130910.2307118-7-linux@roeck-us.net> X-Mailer: git-send-email 2.36.2 In-Reply-To: <20221017130910.2307118-1-linux@roeck-us.net> References: <20221017130910.2307118-1-linux@roeck-us.net> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-1.2 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_EF,FREEMAIL_ENVFROM_END_DIGIT, FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM,HEADER_FROM_DIFFERENT_DOMAINS, RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS autolearn=no autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: <linux-kernel.vger.kernel.org> X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1746940534456662883?= X-GMAIL-MSGID: =?utf-8?q?1746940534456662883?= |
Series |
thermal/core: Protect thermal device operations against removal
|
|
Commit Message
Guenter Roeck
Oct. 17, 2022, 1:09 p.m. UTC
In preparation to protecting access to thermal operations against thermal
zone device removal, protect hwmon accesses to thermal zone operations
with the thermal zone mutex. After acquiring the mutex, ensure that the
thermal zone device is registered before proceeding.
Signed-off-by: Guenter Roeck <linux@roeck-us.net>
---
drivers/thermal/thermal_hwmon.c | 14 +++++++++++---
1 file changed, 11 insertions(+), 3 deletions(-)
Comments
On Mon, Oct 17, 2022 at 3:09 PM Guenter Roeck <linux@roeck-us.net> wrote: > > In preparation to protecting access to thermal operations against thermal > zone device removal, protect hwmon accesses to thermal zone operations > with the thermal zone mutex. After acquiring the mutex, ensure that the > thermal zone device is registered before proceeding. > > Signed-off-by: Guenter Roeck <linux@roeck-us.net> > --- > drivers/thermal/thermal_hwmon.c | 14 +++++++++++--- > 1 file changed, 11 insertions(+), 3 deletions(-) > > diff --git a/drivers/thermal/thermal_hwmon.c b/drivers/thermal/thermal_hwmon.c > index f53f4ceb6a5d..33bfbaed4236 100644 > --- a/drivers/thermal/thermal_hwmon.c > +++ b/drivers/thermal/thermal_hwmon.c > @@ -77,11 +77,19 @@ temp_crit_show(struct device *dev, struct device_attribute *attr, char *buf) > int temperature; > int ret; > > + mutex_lock(&tz->lock); > + > + if (!device_is_registered(&tz->device)) { > + ret = -ENODEV; > + goto unlock; > + } > + > ret = tz->ops->get_crit_temp(tz, &temperature); Again, I would do it this way: if (device_is_registered(&tz->device)) ret = tz->ops->get_crit_temp(tz, &temperature); else ret = -ENODEV; And I wouldn't change the code below (the ternary operator is out of fashion in particular). > - if (ret) > - return ret; > > - return sprintf(buf, "%d\n", temperature); > +unlock: > + mutex_unlock(&tz->lock); > + > + return ret ? ret : sprintf(buf, "%d\n", temperature); > } > > > --
On Wed, Nov 09, 2022 at 08:19:13PM +0100, Rafael J. Wysocki wrote: > On Mon, Oct 17, 2022 at 3:09 PM Guenter Roeck <linux@roeck-us.net> wrote: [ ... ] > > And I wouldn't change the code below (the ternary operator is out of > fashion in particular). > I tried to introduce some consistency; the ternary operator is used in some of the existing thermal code. Guess I went the wrong direction. Never mind; I don't have a strong opinion either way. I updated the series patches to no longer use ternary operators in updated code, but I left existing code alone (changing that should not be part of this patch set anyway). Thanks, Guenter
On Thu, Nov 10, 2022 at 3:21 PM Guenter Roeck <linux@roeck-us.net> wrote: > > On Wed, Nov 09, 2022 at 08:19:13PM +0100, Rafael J. Wysocki wrote: > > On Mon, Oct 17, 2022 at 3:09 PM Guenter Roeck <linux@roeck-us.net> wrote: > [ ... ] > > > > And I wouldn't change the code below (the ternary operator is out of > > fashion in particular). > > > > I tried to introduce some consistency; the ternary operator is used > in some of the existing thermal code. Guess I went the wrong direction. > Never mind; I don't have a strong opinion either way. > I updated the series patches to no longer use ternary operators in > updated code, but I left existing code alone (changing that should not > be part of this patch set anyway). Thanks, that's what I would have done too.
diff --git a/drivers/thermal/thermal_hwmon.c b/drivers/thermal/thermal_hwmon.c index f53f4ceb6a5d..33bfbaed4236 100644 --- a/drivers/thermal/thermal_hwmon.c +++ b/drivers/thermal/thermal_hwmon.c @@ -77,11 +77,19 @@ temp_crit_show(struct device *dev, struct device_attribute *attr, char *buf) int temperature; int ret; + mutex_lock(&tz->lock); + + if (!device_is_registered(&tz->device)) { + ret = -ENODEV; + goto unlock; + } + ret = tz->ops->get_crit_temp(tz, &temperature); - if (ret) - return ret; - return sprintf(buf, "%d\n", temperature); +unlock: + mutex_unlock(&tz->lock); + + return ret ? ret : sprintf(buf, "%d\n", temperature); }