From patchwork Thu Nov 10 15:24:58 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Guenter Roeck X-Patchwork-Id: 18164 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:6687:0:0:0:0:0 with SMTP id l7csp211968wru; Thu, 10 Nov 2022 07:28:48 -0800 (PST) X-Google-Smtp-Source: AMsMyM6d5H5zTQHZ4S3QEEH5+o6YZIumYsiMLnVZpiGWjenr2oePLYxuyCgURtor+gmhq7i4ZmcD X-Received: by 2002:a17:906:f4e:b0:7a2:335e:90e2 with SMTP id h14-20020a1709060f4e00b007a2335e90e2mr2927078ejj.712.1668094128494; Thu, 10 Nov 2022 07:28:48 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1668094128; cv=none; d=google.com; s=arc-20160816; b=Z/ftgqIzVHzSpHfgFy82XiN2ZDE6OtwYmpFmYkVH7GYz7dzx/qEVFrVA8AxC9W6VcQ qxaWsYgG5OhxI4T3tyQ+GkwuNGxuBqn1O7/FZWdSFsJbis1NlxBVF+3DfOpXW7HLS26V 1XVlHjuIbs4JUWaWpBwHbdVrxsPp2RYFDHTx/3/HgqqrisPyU9la0oi3P3AwBKD0F/TK UiNqpm1PZKLttesVeTxS9S2qAISYzWPDXRWoqAZhxB9WAjE+Io3ft+RT4c9EK+BjaEnK 1ePrSUzaIesiDftBwaykkU1Ep+JuSTebjTu5XC9I+UqKSUq4YGiC12NiilUBvcpiFOkm VeTA== 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=wERUdQdjWjh/WLWGcq6kXq7Y3fhhIRGrVPA3P4TVF/k=; b=jwIRjWxUqxU3ZwQXeCrDsLd98V7m/CATGR2VDMG39O5tTmydH0DSzBQ8h0aiv5ocHb IyowDnODCtbedm1NBWZVG5F26zzS8bF+AryULTHNRMMrImJYORfYAnhWikT0qWKjdii5 LbglHp4T2LIQnXgKg8yrDi6SakwlXrKxJAbgUm28wrC/YO5x5avoYUQheIdnvXtjeada vBopSBBc9t9xatNC1AlCxvo2qkAIotGwbktoTmuZm6DvBavjOT22UNFcEfDsZEfo07Ui uuiwE6Ynsc4EOd+earFKLkStfYAe1EXQ3VW7voTFSOjFm9qtIP7kvIdp/ycjRUKB6rVg 8PvQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=XlcvyvGd; 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 hh18-20020a170906a95200b007ae4717bf18si14818938ejb.156.2022.11.10.07.28.23; Thu, 10 Nov 2022 07:28:48 -0800 (PST) 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=XlcvyvGd; 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 S230426AbiKJPZf (ORCPT + 99 others); Thu, 10 Nov 2022 10:25:35 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40600 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231464AbiKJPZV (ORCPT ); Thu, 10 Nov 2022 10:25:21 -0500 Received: from mail-oi1-x232.google.com (mail-oi1-x232.google.com [IPv6:2607:f8b0:4864:20::232]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 096883E086; Thu, 10 Nov 2022 07:25:16 -0800 (PST) Received: by mail-oi1-x232.google.com with SMTP id n205so2162506oib.1; Thu, 10 Nov 2022 07:25:16 -0800 (PST) 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=wERUdQdjWjh/WLWGcq6kXq7Y3fhhIRGrVPA3P4TVF/k=; b=XlcvyvGdDoL7j/ld25ik8DutzpkV/ZhQe5SsKtayc7k0cH7McLdnpH7Rz7pb4os8oD niAH3q9Vj29F3ImpOFH0sGgKWEE6/eIMu/okSj7HeY/3uoELTaanIlljr4Y5Eb/ct+29 yxxawZ7k9JCQvLD/X5LwkUsnrCt2xe0upzKAanbY3wUSqUyjcGc5VrMG80MiakGCxuro 8KVizuzoKSFN60Zns+Hp72DXGX0hS92YVnuhy6geIC/VupZQrDEZzW5a5k4ZnqoNaQs3 Q7hRrMtHLE3bz23jU24DnuURApm+NpuOA39YAji3BEj8bOsNR4SMWToYqk9fo4dKldjF Q2Uw== 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=wERUdQdjWjh/WLWGcq6kXq7Y3fhhIRGrVPA3P4TVF/k=; b=dqVwZfHa/TptIOLp20WGPc1/aRpAJ17eb8ro9KE1cLNpk9B0HR8u0VMfLflj7wkO4X W5RDmV+J+vw75cELf4FqaPxU7vB4BElsxyW/ypzxOqbmw51T4x1VKVeLti0pODCGFit2 ixkiPdNL/l2GjWKDSuaL8TTaLSx7OEsB0a9Ronn+/GWFtHSAqQ986Eo/nTXAizgOK5di bym9RUsDRvJGfM4sL3R5/FPzuvFUkd736xTAkm77Sz6thSXEIUm4rdeC5IHGnaphkoPP s4r4CkKHxVijVv8LBgcimqSUO6jWLqYcoVVqommLutSlvN4TruZHl0kAf6YxAn8xzJ9z hA8g== X-Gm-Message-State: ACrzQf3MqNMNUitJaovDRcnsKQa81HgETDfsAJSdzxr8v68CDxcryPLG SPAqfZZQ6Yw31W26CYdYskE= X-Received: by 2002:a05:6808:658:b0:343:4df6:c5d with SMTP id z24-20020a056808065800b003434df60c5dmr1574790oih.279.1668093915356; Thu, 10 Nov 2022 07:25:15 -0800 (PST) Received: from server.roeck-us.net ([2600:1700:e321:62f0:329c:23ff:fee3:9d7c]) by smtp.gmail.com with ESMTPSA id s16-20020a4adb90000000b0047f72b6988fsm5221123oou.45.2022.11.10.07.25.14 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 10 Nov 2022 07:25:14 -0800 (PST) Sender: Guenter Roeck From: Guenter Roeck To: "Rafael J . Wysocki" , Daniel Lezcano Cc: Amit Kucheria , Zhang Rui , linux-pm@vger.kernel.org, linux-kernel@vger.kernel.org, Guenter Roeck Subject: [PATCH v2 7/9] thermal/core: Protect sysfs accesses to thermal operations with thermal zone mutex Date: Thu, 10 Nov 2022 07:24:58 -0800 Message-Id: <20221110152500.3032655-8-linux@roeck-us.net> X-Mailer: git-send-email 2.36.2 In-Reply-To: <20221110152500.3032655-1-linux@roeck-us.net> References: <20221110152500.3032655-1-linux@roeck-us.net> MIME-Version: 1.0 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: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1749123468574778556?= X-GMAIL-MSGID: =?utf-8?q?1749123468574778556?= Protect access to thermal operations against thermal zone removal by acquiring the thermal zone device mutex. After acquiring the mutex, check if the thermal zone device is registered and abort the operation if not. With this change, we can call __thermal_zone_device_update() instead of thermal_zone_device_update() from trip_point_temp_store() and from emul_temp_store(). Similar, we can call __thermal_zone_set_trips() instead of thermal_zone_set_trips() from trip_point_hyst_store(). Signed-off-by: Guenter Roeck --- v2: Simplify error handling to avoid additional goto. Do not use ternary operator in modified code. Remove now unnecessary { } in emul_temp_store(). drivers/thermal/thermal_core.c | 4 +- drivers/thermal/thermal_core.h | 2 + drivers/thermal/thermal_sysfs.c | 79 +++++++++++++++++++++++++++------ 3 files changed, 69 insertions(+), 16 deletions(-) diff --git a/drivers/thermal/thermal_core.c b/drivers/thermal/thermal_core.c index 9facd9c5b70f..b8e3b262b2bd 100644 --- a/drivers/thermal/thermal_core.c +++ b/drivers/thermal/thermal_core.c @@ -403,8 +403,8 @@ static void thermal_zone_device_init(struct thermal_zone_device *tz) pos->initialized = false; } -static void __thermal_zone_device_update(struct thermal_zone_device *tz, - enum thermal_notify_event event) +void __thermal_zone_device_update(struct thermal_zone_device *tz, + enum thermal_notify_event event) { int count; diff --git a/drivers/thermal/thermal_core.h b/drivers/thermal/thermal_core.h index 1571917bd3c8..7b51b9a22e7e 100644 --- a/drivers/thermal/thermal_core.h +++ b/drivers/thermal/thermal_core.h @@ -109,6 +109,8 @@ int thermal_register_governor(struct thermal_governor *); void thermal_unregister_governor(struct thermal_governor *); int thermal_zone_device_set_policy(struct thermal_zone_device *, char *); int thermal_build_list_of_policies(char *buf); +void __thermal_zone_device_update(struct thermal_zone_device *tz, + enum thermal_notify_event event); /* Helpers */ void thermal_zone_set_trips(struct thermal_zone_device *tz); diff --git a/drivers/thermal/thermal_sysfs.c b/drivers/thermal/thermal_sysfs.c index ec495c7dff03..e2e94294fbb2 100644 --- a/drivers/thermal/thermal_sysfs.c +++ b/drivers/thermal/thermal_sysfs.c @@ -92,7 +92,14 @@ trip_point_type_show(struct device *dev, struct device_attribute *attr, if (sscanf(attr->attr.name, "trip_point_%d_type", &trip) != 1) return -EINVAL; - result = tz->ops->get_trip_type(tz, trip, &type); + mutex_lock(&tz->lock); + + if (device_is_registered(dev)) + result = tz->ops->get_trip_type(tz, trip, &type); + else + result = -ENODEV; + + mutex_unlock(&tz->lock); if (result) return result; @@ -128,10 +135,17 @@ trip_point_temp_store(struct device *dev, struct device_attribute *attr, if (kstrtoint(buf, 10, &temperature)) return -EINVAL; + mutex_lock(&tz->lock); + + if (!device_is_registered(dev)) { + ret = -ENODEV; + goto unlock; + } + if (tz->ops->set_trip_temp) { ret = tz->ops->set_trip_temp(tz, trip, temperature); if (ret) - return ret; + goto unlock; } if (tz->trips) @@ -140,16 +154,22 @@ trip_point_temp_store(struct device *dev, struct device_attribute *attr, if (tz->ops->get_trip_hyst) { ret = tz->ops->get_trip_hyst(tz, trip, &hyst); if (ret) - return ret; + goto unlock; } ret = tz->ops->get_trip_type(tz, trip, &type); if (ret) - return ret; + goto unlock; thermal_notify_tz_trip_change(tz->id, trip, type, temperature, hyst); - thermal_zone_device_update(tz, THERMAL_EVENT_UNSPECIFIED); + __thermal_zone_device_update(tz, THERMAL_EVENT_UNSPECIFIED); + +unlock: + mutex_unlock(&tz->lock); + + if (ret) + return ret; return count; } @@ -168,7 +188,14 @@ trip_point_temp_show(struct device *dev, struct device_attribute *attr, if (sscanf(attr->attr.name, "trip_point_%d_temp", &trip) != 1) return -EINVAL; - ret = tz->ops->get_trip_temp(tz, trip, &temperature); + mutex_lock(&tz->lock); + + if (device_is_registered(dev)) + ret = tz->ops->get_trip_temp(tz, trip, &temperature); + else + ret = -ENODEV; + + mutex_unlock(&tz->lock); if (ret) return ret; @@ -193,6 +220,13 @@ trip_point_hyst_store(struct device *dev, struct device_attribute *attr, if (kstrtoint(buf, 10, &temperature)) return -EINVAL; + mutex_lock(&tz->lock); + + if (!device_is_registered(dev)) { + ret = -ENODEV; + goto unlock; + } + /* * We are not doing any check on the 'temperature' value * here. The driver implementing 'set_trip_hyst' has to @@ -201,7 +235,10 @@ trip_point_hyst_store(struct device *dev, struct device_attribute *attr, ret = tz->ops->set_trip_hyst(tz, trip, temperature); if (!ret) - thermal_zone_set_trips(tz); + __thermal_zone_set_trips(tz); + +unlock: + mutex_unlock(&tz->lock); return ret ? ret : count; } @@ -220,7 +257,14 @@ trip_point_hyst_show(struct device *dev, struct device_attribute *attr, if (sscanf(attr->attr.name, "trip_point_%d_hyst", &trip) != 1) return -EINVAL; - ret = tz->ops->get_trip_hyst(tz, trip, &temperature); + mutex_lock(&tz->lock); + + if (device_is_registered(dev)) + ret = tz->ops->get_trip_hyst(tz, trip, &temperature); + else + ret = -ENODEV; + + mutex_unlock(&tz->lock); return ret ? ret : sprintf(buf, "%d\n", temperature); } @@ -269,16 +313,23 @@ emul_temp_store(struct device *dev, struct device_attribute *attr, if (kstrtoint(buf, 10, &temperature)) return -EINVAL; - if (!tz->ops->set_emul_temp) { - mutex_lock(&tz->lock); + mutex_lock(&tz->lock); + + if (!device_is_registered(dev)) { + ret = -ENODEV; + goto unlock; + } + + if (!tz->ops->set_emul_temp) tz->emul_temperature = temperature; - mutex_unlock(&tz->lock); - } else { + else ret = tz->ops->set_emul_temp(tz, temperature); - } if (!ret) - thermal_zone_device_update(tz, THERMAL_EVENT_UNSPECIFIED); + __thermal_zone_device_update(tz, THERMAL_EVENT_UNSPECIFIED); + +unlock: + mutex_unlock(&tz->lock); return ret ? ret : count; }