From patchwork Thu Jan 19 19:29:21 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: srinivas pandruvada X-Patchwork-Id: 45949 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:eb09:0:0:0:0:0 with SMTP id s9csp517423wrn; Thu, 19 Jan 2023 11:59:40 -0800 (PST) X-Google-Smtp-Source: AMrXdXvJ2TsT51s+dqJKabN3RSkOW6aKJ2iAnMVTKLUfsxM1/vIqd9Ly3QawfsiwpUenFuC+/Kq/ X-Received: by 2002:a05:6a20:c6c9:b0:b8:8a19:d6ad with SMTP id gw9-20020a056a20c6c900b000b88a19d6admr11282067pzb.24.1674158380511; Thu, 19 Jan 2023 11:59:40 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1674158380; cv=none; d=google.com; s=arc-20160816; b=yFTPqtfRv5Dsx46Jc3aUSF1d0t6Dh0GyKQo15Tacc2EEfxtNINd7slAUDi1w2Hpwrt /Ti9+XV2HUwPN2VO+Aib9488QmR2RBEJo2VxJxUwdVhpl1bzLJ944/7RswTDP6Cf0D4j 1rUNQ7vmFBoo9g4SaQaHLhSvohC7y3mT6dhez6P/xw0F8JAUcGWC7ti0MYZBrwcsg+GB y5g61hY9ldwmtGCqXQSUAeT9ZQ1N9f/PFSYenzeOoxj7EX61yW16Oup7pRJndE93lavm UYfUTUYyOdiLgl2wnsLBnIvr0psQPY8GKUyI00RqBWwQ1pnk9B5Qytk2/uA1nFzlmfuU Bdgw== 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 :message-id:date:subject:cc:to:from:dkim-signature; bh=RFtirhX7N/+SvSgrFSMhlY9nbvqt0N2WXP/Het7YS8s=; b=COqgX9PwcxKEVcR2IhyrleAsq2q4oM95DSs33PpvGvAFeZRWIKC5/8wWG/PFLA/peS 8b1u/NBBIB9a/7EIn/PaXXyv81/pnHrwdX0pENebMlHmZb5VyxVh7svnDzTmoolutdkV r/JJgU2AsnTP4vZoa7xa/XiKkpmouv1I+RcQfDhL1DL51G29Vam86IQhqVZwTb618GV9 lFOv2a2Kv+fLKOq7BmNtRi7zYQKpj1YipjazwxD5UpYEOa1rQZ8eDo1EpkjGk1MeYffZ 2bZgZOG6+wPCHGgM00YLwFrVhwBv0KDQwtxoOGk3gCxYKmYMfuvEhnoI4ZDaIYforIRQ +1IA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=P7nVsk6j; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id 131-20020a630089000000b0047865b303a5si1912831pga.762.2023.01.19.11.59.28; Thu, 19 Jan 2023 11:59:40 -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=@intel.com header.s=Intel header.b=P7nVsk6j; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230070AbjAST3p (ORCPT + 99 others); Thu, 19 Jan 2023 14:29:45 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44066 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229811AbjAST3c (ORCPT ); Thu, 19 Jan 2023 14:29:32 -0500 Received: from mga17.intel.com (mga17.intel.com [192.55.52.151]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6B70D7495B; Thu, 19 Jan 2023 11:29:31 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1674156571; x=1705692571; h=from:to:cc:subject:date:message-id:mime-version: content-transfer-encoding; bh=XiEoqcw1GCxHBI/TeAEMlbF5M4j7VYAXm9Oo4Dv0gzE=; b=P7nVsk6j6W4SB3IZxdDJaCdA+qo3hm37BrFtmauh3KXmtF1LFHAu/eUS rMpckN+VevNck46q7wxqOpSG7TUQgRPChmVt0tDpm0HzKCZLl5jZCFcOT s8N97z6W253tbsnbUE/QyBZOKb37IGGOimhVQzjyFH9WAiJiOjiuZhNwU wHj7I7z2Kirogk1vaWaW1d+CuyzMr1vO1YMLSBKam56g5CiBvfleIuP3H yyTjKiK4AlFPxvFXKPlyuzLHS9r3QQriK4EaS/x5Q0c3drRBM+gzjqy3z jVDFFE8BrN1RChrvm1xJjS9AGKpwMR0/LIf3sQIw0J1BOLf34F9xrO2qB w==; X-IronPort-AV: E=McAfee;i="6500,9779,10595"; a="305763093" X-IronPort-AV: E=Sophos;i="5.97,229,1669104000"; d="scan'208";a="305763093" Received: from fmsmga006.fm.intel.com ([10.253.24.20]) by fmsmga107.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 19 Jan 2023 11:29:28 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6500,9779,10595"; a="905669312" X-IronPort-AV: E=Sophos;i="5.97,229,1669104000"; d="scan'208";a="905669312" Received: from spandruv-desk.jf.intel.com ([10.54.75.8]) by fmsmga006.fm.intel.com with ESMTP; 19 Jan 2023 11:29:27 -0800 From: Srinivas Pandruvada To: rafael@kernel.org, rui.zhang@intel.com, daniel.lezcano@linaro.org Cc: linux-pm@vger.kernel.org, linux-kernel@vger.kernel.org, Srinivas Pandruvada , stable@vger.kernel.org Subject: [PATCH] thermal: int340x: Protect trip temperature from dynamic update Date: Thu, 19 Jan 2023 11:29:21 -0800 Message-Id: <20230119192921.3215965-1-srinivas.pandruvada@linux.intel.com> X-Mailer: git-send-email 2.31.1 MIME-Version: 1.0 X-Spam-Status: No, score=-4.3 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_EF,RCVD_IN_DNSWL_MED,SPF_HELO_NONE, SPF_NONE autolearn=ham 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?1755482298008345935?= X-GMAIL-MSGID: =?utf-8?q?1755482298008345935?= Trip temperatures are read using ACPI methods and stored in the memory during zone initializtion and when the firmware sends a notification for change. This trip temperature is returned when the thermal core calls via callback get_trip_temp(). But it is possible that while updating the memory copy of the trips when the firmware sends a notification for change, thermal core is reading the trip temperature via the callback get_trip_temp(). This may return invalid trip temperature. To address this add a mutex to protect the invalid temperature reads in the callback get_trip_temp() and int340x_thermal_read_trips(). Signed-off-by: Srinivas Pandruvada Cc: stable@vger.kernel.org # 5.0+ --- .../intel/int340x_thermal/int340x_thermal_zone.c | 16 +++++++++++++++- .../intel/int340x_thermal/int340x_thermal_zone.h | 1 + 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/drivers/thermal/intel/int340x_thermal/int340x_thermal_zone.c b/drivers/thermal/intel/int340x_thermal/int340x_thermal_zone.c index 62c0aa5d0783..fd9080640e03 100644 --- a/drivers/thermal/intel/int340x_thermal/int340x_thermal_zone.c +++ b/drivers/thermal/intel/int340x_thermal/int340x_thermal_zone.c @@ -49,6 +49,8 @@ static int int340x_thermal_get_trip_temp(struct thermal_zone_device *zone, if (d->override_ops && d->override_ops->get_trip_temp) return d->override_ops->get_trip_temp(zone, trip, temp); + mutex_lock(&d->trip_mutex); + if (trip < d->aux_trip_nr) *temp = d->aux_trips[trip]; else if (trip == d->crt_trip_id) @@ -65,10 +67,14 @@ static int int340x_thermal_get_trip_temp(struct thermal_zone_device *zone, break; } } - if (i == INT340X_THERMAL_MAX_ACT_TRIP_COUNT) + if (i == INT340X_THERMAL_MAX_ACT_TRIP_COUNT) { + mutex_unlock(&d->trip_mutex); return -EINVAL; + } } + mutex_unlock(&d->trip_mutex); + return 0; } @@ -180,6 +186,8 @@ int int340x_thermal_read_trips(struct int34x_thermal_zone *int34x_zone) int trip_cnt = int34x_zone->aux_trip_nr; int i; + mutex_lock(&int34x_zone->trip_mutex); + int34x_zone->crt_trip_id = -1; if (!int340x_thermal_get_trip_config(int34x_zone->adev->handle, "_CRT", &int34x_zone->crt_temp)) @@ -207,6 +215,8 @@ int int340x_thermal_read_trips(struct int34x_thermal_zone *int34x_zone) int34x_zone->act_trips[i].valid = true; } + mutex_unlock(&int34x_zone->trip_mutex); + return trip_cnt; } EXPORT_SYMBOL_GPL(int340x_thermal_read_trips); @@ -230,6 +240,8 @@ struct int34x_thermal_zone *int340x_thermal_zone_add(struct acpi_device *adev, if (!int34x_thermal_zone) return ERR_PTR(-ENOMEM); + mutex_init(&int34x_thermal_zone->trip_mutex); + int34x_thermal_zone->adev = adev; int34x_thermal_zone->override_ops = override_ops; @@ -281,6 +293,7 @@ struct int34x_thermal_zone *int340x_thermal_zone_add(struct acpi_device *adev, acpi_lpat_free_conversion_table(int34x_thermal_zone->lpat_table); kfree(int34x_thermal_zone->aux_trips); err_trip_alloc: + mutex_destroy(&int34x_thermal_zone->trip_mutex); kfree(int34x_thermal_zone); return ERR_PTR(ret); } @@ -292,6 +305,7 @@ void int340x_thermal_zone_remove(struct int34x_thermal_zone thermal_zone_device_unregister(int34x_thermal_zone->zone); acpi_lpat_free_conversion_table(int34x_thermal_zone->lpat_table); kfree(int34x_thermal_zone->aux_trips); + mutex_destroy(&int34x_thermal_zone->trip_mutex); kfree(int34x_thermal_zone); } EXPORT_SYMBOL_GPL(int340x_thermal_zone_remove); diff --git a/drivers/thermal/intel/int340x_thermal/int340x_thermal_zone.h b/drivers/thermal/intel/int340x_thermal/int340x_thermal_zone.h index 3b4971df1b33..8f9872afd0d3 100644 --- a/drivers/thermal/intel/int340x_thermal/int340x_thermal_zone.h +++ b/drivers/thermal/intel/int340x_thermal/int340x_thermal_zone.h @@ -32,6 +32,7 @@ struct int34x_thermal_zone { struct thermal_zone_device_ops *override_ops; void *priv_data; struct acpi_lpat_conversion_table *lpat_table; + struct mutex trip_mutex; }; struct int34x_thermal_zone *int340x_thermal_zone_add(struct acpi_device *,