From patchwork Thu Nov 10 15:24:55 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Guenter Roeck X-Patchwork-Id: 18184 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:6687:0:0:0:0:0 with SMTP id l7csp213060wru; Thu, 10 Nov 2022 07:30:47 -0800 (PST) X-Google-Smtp-Source: AMsMyM6Dq1Y+BSCnLlzX3Gmrt89o9vgWMd/btv/AMp+PrW9LCk22qtPp7WdX0oF4fW1PfvYMEPVi X-Received: by 2002:a17:906:858a:b0:7ad:d164:bc45 with SMTP id v10-20020a170906858a00b007add164bc45mr52667135ejx.113.1668094247537; Thu, 10 Nov 2022 07:30:47 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1668094247; cv=none; d=google.com; s=arc-20160816; b=wLp3lFXsVNf9V4owA5cLs/r5WHIKBvwZwpLxWhZ+ndGbuRf+/grUcm17cqbkTISh6w kLCGRnn2assyuTe4I1FcPAPGydrKwwKz5BV0J+lS8oKpp24UN1M++tgbPsoHgAYcTUGw GJYPhfURvs79oURt7cOWWbxfWtaezy5jJWqsowcTW2AY3q7qDve+eB+bJ83yfpDY8w6z hdAAII6iL8bqyxQX1CDYnxdTdu6blAh3EX/9KZ2XkmJD/DfRjQ6y8WuTO0bgEQ5/z1pw tYbIaRUgdwWOgXbyALbsETseN7HnPa8xsRO0qxQPKaYyV12PLKGyxEsSZNuzKqaC3ocQ vULg== 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=nPRDmNkrm1vn+V+1JEnLY9+b9B506P/EqjPsjoC9grA=; b=jZQ2rm2luwbjWHTmYa6XCaGIRbSgxlJYrK8guL1i4QRcxPE6931NaJQ83FopN4e5E1 Vghv7cU5WMTRS9hOhHeKjwny2NnMOL/qLBpwL48LHHsg+nHQFprz+pG2c7UwUr1Lt4ju F/zDkzv4PTBsAPzubqIf4UnxJPuea1wUhxlGoQ4pY6iqRly0aHkUicZBFqK2aXK3tyEJ fNAKfZSYNMxcdRSbBKFW4U5f4toXNxtFiw5scZsiUdvzI5BHVPYvfEq9sOM49fH7dwE4 h8G9ncKxnXFyVhgs8+2CKemgXTwH7KnagfkC9lZBfukCThA37io7PwUWgShKeVAXO/1V Gmrg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=KKlnEPm2; 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 qm7-20020a170907674700b0078dcdb1c9dfsi16620132ejc.553.2022.11.10.07.30.22; Thu, 10 Nov 2022 07:30:47 -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=KKlnEPm2; 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 S230343AbiKJPZV (ORCPT + 99 others); Thu, 10 Nov 2022 10:25:21 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40306 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229746AbiKJPZL (ORCPT ); Thu, 10 Nov 2022 10:25:11 -0500 Received: from mail-oa1-x36.google.com (mail-oa1-x36.google.com [IPv6:2001:4860:4864:20::36]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id F07CF2CCB2; Thu, 10 Nov 2022 07:25:10 -0800 (PST) Received: by mail-oa1-x36.google.com with SMTP id 586e51a60fabf-13b23e29e36so2484168fac.8; Thu, 10 Nov 2022 07:25:10 -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=nPRDmNkrm1vn+V+1JEnLY9+b9B506P/EqjPsjoC9grA=; b=KKlnEPm2PbaBOKwE1hreqVjPeEksFU9/M3Q+oOqwhTimgksD4vs0scvu1vOcPsSSaV GrWRxwkb/nKRMPhjMl8YdlQaVZsPe5c6AAcfBZwi6Yg38A/qDMtYNQ0lRUZ3s0nuE/Sm lcx0UdiW4tJJNh9YhZns0/iSu2DNr59c5VWVXF+Gr1IElswx/TLXKJ/gdrcBqMVFKFDE ejDizxQHa9DY2P3LI8ywPLjOp6Q9TSVG7mY0pMohSFQbZJ/sHm9kIY0K9K7N0Gh8rNM9 YdcVmvQPen05DhLcjrR0fRadV0Q7rbzm5vBYlTO2E7plgaEJA4nilboEN35MD9E2ViPL tK+w== 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=nPRDmNkrm1vn+V+1JEnLY9+b9B506P/EqjPsjoC9grA=; b=1YlPk021bUnG0CwecgOs8RF50zHWOF3YQhwVEESpd0B2RLPMI2mr6W3VCw/Sh2K1xG 5yUx5eRRdtkp3FOfBzdO6J6cKcmeJlSEAE5A+ktv2k4zdARqtxvqczUij2ziWM1Mqyy0 M7CyLFu8EnFv2KSrClv/yAcfAs4cFfq2A9t3UMARQq0jyXb1PQilPol85gIrZA2H9pta G/Dh49EavRmln+oSQ/WjCJjtc1VGTDV/pifR2ipOZ5I2fb7DC5M3IQpEGc+/WQvgBcs9 djWoVu00XahjTDwRbjbpCpLViQorkAlkeJZlJdim4P4H9OwUivIp/bOAeeontsAVaBff MqAw== X-Gm-Message-State: ACrzQf2uIAlpQTnWlEJy6K8cUkazrsvzO0US8N7Rnro7SS4HW4ezTlz5 P9gqdBLE0fSdXd3n/XChD5k= X-Received: by 2002:a05:6870:525:b0:130:9e35:137a with SMTP id j37-20020a056870052500b001309e35137amr37067612oao.88.1668093910340; Thu, 10 Nov 2022 07:25:10 -0800 (PST) Received: from server.roeck-us.net ([2600:1700:e321:62f0:329c:23ff:fee3:9d7c]) by smtp.gmail.com with ESMTPSA id r7-20020a056830418700b006618586b850sm6582808otu.46.2022.11.10.07.25.09 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 10 Nov 2022 07:25:09 -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 4/9] thermal/core: Move parameter validation from __thermal_zone_get_temp to thermal_zone_get_temp Date: Thu, 10 Nov 2022 07:24:55 -0800 Message-Id: <20221110152500.3032655-5-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?1749123594015545558?= X-GMAIL-MSGID: =?utf-8?q?1749123594015545558?= All callers of __thermal_zone_get_temp() already validated the thermal zone parameters. Move validation to thermal_zone_get_temp() where it is actually needed. Also add kernel documentation for __thermal_zone_get_temp(), listing the requirement that the function must be called with validated parameters and with thermal device mutex held. Signed-off-by: Guenter Roeck --- v2: Add note about added kernel documentation to description . Use IS_ERR_OR_NULL instead of manually coding it. drivers/thermal/thermal_helpers.c | 26 +++++++++++++++++++++++--- 1 file changed, 23 insertions(+), 3 deletions(-) diff --git a/drivers/thermal/thermal_helpers.c b/drivers/thermal/thermal_helpers.c index fca0b23570f9..321f8020082d 100644 --- a/drivers/thermal/thermal_helpers.c +++ b/drivers/thermal/thermal_helpers.c @@ -64,6 +64,20 @@ get_thermal_instance(struct thermal_zone_device *tz, } EXPORT_SYMBOL(get_thermal_instance); +/** + * __thermal_zone_get_temp() - returns the temperature of a thermal zone + * @tz: a valid pointer to a struct thermal_zone_device + * @temp: a valid pointer to where to store the resulting temperature. + * + * When a valid thermal zone reference is passed, it will fetch its + * temperature and fill @temp. + * + * Both tz and tz->ops must be valid pointers when calling this function, + * and the tz->ops->get_temp callback must be provided. + * The function must be called under tz->lock. + * + * Return: On success returns 0, an error code otherwise + */ int __thermal_zone_get_temp(struct thermal_zone_device *tz, int *temp) { int ret = -EINVAL; @@ -73,9 +87,6 @@ int __thermal_zone_get_temp(struct thermal_zone_device *tz, int *temp) lockdep_assert_held(&tz->lock); - if (!tz || IS_ERR(tz) || !tz->ops->get_temp) - return -EINVAL; - ret = tz->ops->get_temp(tz, temp); if (IS_ENABLED(CONFIG_THERMAL_EMULATION) && tz->emul_temperature) { @@ -114,13 +125,22 @@ int thermal_zone_get_temp(struct thermal_zone_device *tz, int *temp) { int ret; + if (IS_ERR_OR_NULL(tz)) + return -EINVAL; + mutex_lock(&tz->lock); + if (!tz->ops->get_temp) { + ret = -EINVAL; + goto unlock; + } + if (device_is_registered(&tz->device)) ret = __thermal_zone_get_temp(tz, temp); else ret = -ENODEV; +unlock: mutex_unlock(&tz->lock); return ret;