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 X-Spam-Level: X-Spam-Status: No, score=-15.3 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,NICE_REPLY_A,SPF_HELO_NONE,SPF_PASS,USER_AGENT_SANE_1 autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id C85F0C433B4 for ; Tue, 6 Apr 2021 20:09:06 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 9C86C613CC for ; Tue, 6 Apr 2021 20:09:06 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235318AbhDFUJN (ORCPT ); Tue, 6 Apr 2021 16:09:13 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33358 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1347162AbhDFUJM (ORCPT ); Tue, 6 Apr 2021 16:09:12 -0400 Received: from mail-oo1-xc32.google.com (mail-oo1-xc32.google.com [IPv6:2607:f8b0:4864:20::c32]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id ED42EC06174A; Tue, 6 Apr 2021 13:09:03 -0700 (PDT) Received: by mail-oo1-xc32.google.com with SMTP id h3-20020a4ae8c30000b02901b68b39e2d3so3981787ooe.9; Tue, 06 Apr 2021 13:09:03 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=sender:subject:to:cc:references:from:autocrypt:message-id:date :user-agent:mime-version:in-reply-to:content-language :content-transfer-encoding; bh=AX6W2V3H2wUm9gL8P5cg3gNxKe6TSNhG0ajD+9l3ZZ0=; b=kgX0tnjpuFgD8e+TwJm4BBNXMe9Z8STyhsZfewsWFnuw96dhOKDL/QwQnOzx5Hgkgk GZ86yxoDjaWR2kkHj394MJvOYKA69TUKOSKJdFSV//kUruWWCUhbyU2JeNPH/wAlJALs Z7l0AY3offqYt5jyNvAedgOHNUyQblJ/vl6ILDiBnQW2rYtbpx1bOgmXv8gJLqpeP6IK SQb5spqmPkcp3gao82G6nfOGFWnvzCQztkiNKS/8FAJgQYocV3DD7ZeKUUpSVzVLU5T/ PPANRcnm+mY16tMfGMNDI8nnuX5zMfs34IOtys0eVVffrc1mW3pJ7+OkJ4YQN2oXWtcP bqHQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:sender:subject:to:cc:references:from:autocrypt :message-id:date:user-agent:mime-version:in-reply-to :content-language:content-transfer-encoding; bh=AX6W2V3H2wUm9gL8P5cg3gNxKe6TSNhG0ajD+9l3ZZ0=; b=dkx+LWWyqYuTT8Yv2nQHI2u41L6g7n2qABbXBBk8P62J/x+4xlb1vZIbDeWZAzXU9J rFE0lIagUeDOKVeTsIgdULoAdWwcRW7HMNIo1jfR0xqpixxeoN+55i8JaKf5EOEGn6AC 6EIMGhUQrMFw3fFdpbEnsgWNAzSzu0Dpeusoca2zFD2WcfpEtjlF3onHsrYXqV3yrABI ZifFxNcSrnJJOQw70r53lyAJX2U3q3JyKfi834K8It+jt5iH1KHyY9iUCEku/yf8klUE kwdPOcTcWyxGZ45/+fN3CBzhp7CQOdjCySKjg83ge6pfx73Iw96hflpL+euaZScTPZVT mKqg== X-Gm-Message-State: AOAM533FMhz3MOJWFWuDv5UPVNpqatR0lADYvEAJynrtghJdpQKvfC1k qoPVJxIgXoJF5/9/QjPTRyNX6S8jYF8= X-Google-Smtp-Source: ABdhPJzR5yesO3MDLeLvOA0ilU3GAWgcqbBQD//DCSXd2yhJl9IOpMuxZaqqMjGVATFcmJb1xi2I3A== X-Received: by 2002:a4a:9843:: with SMTP id z3mr27804519ooi.51.1617739742891; Tue, 06 Apr 2021 13:09:02 -0700 (PDT) Received: from server.roeck-us.net ([2600:1700:e321:62f0:329c:23ff:fee3:9d7c]) by smtp.gmail.com with ESMTPSA id v195sm3996112oia.38.2021.04.06.13.09.01 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Tue, 06 Apr 2021 13:09:02 -0700 (PDT) Sender: Guenter Roeck Subject: Re: [PATCH v2] hwmon: (sbtsi) Don't read sensor more than once if it doesn't respond To: Konstantin Aladyshev Cc: Kun Yi , Jean Delvare , linux-hwmon@vger.kernel.org, linux-kernel@vger.kernel.org References: <20210406181624.713-1-aladyshev22@gmail.com> <0ba1d271-78cd-c30a-e662-97ab8ba6451a@roeck-us.net> From: Guenter Roeck Autocrypt: addr=linux@roeck-us.net; keydata= xsFNBE6H1WcBEACu6jIcw5kZ5dGeJ7E7B2uweQR/4FGxH10/H1O1+ApmcQ9i87XdZQiB9cpN RYHA7RCEK2dh6dDccykQk3bC90xXMPg+O3R+C/SkwcnUak1UZaeK/SwQbq/t0tkMzYDRxfJ7 nyFiKxUehbNF3r9qlJgPqONwX5vJy4/GvDHdddSCxV41P/ejsZ8PykxyJs98UWhF54tGRWFl 7i1xvaDB9lN5WTLRKSO7wICuLiSz5WZHXMkyF4d+/O5ll7yz/o/JxK5vO/sduYDIlFTvBZDh gzaEtNf5tQjsjG4io8E0Yq0ViobLkS2RTNZT8ICq/Jmvl0SpbHRvYwa2DhNsK0YjHFQBB0FX IdhdUEzNefcNcYvqigJpdICoP2e4yJSyflHFO4dr0OrdnGLe1Zi/8Xo/2+M1dSSEt196rXaC kwu2KgIgmkRBb3cp2vIBBIIowU8W3qC1+w+RdMUrZxKGWJ3juwcgveJlzMpMZNyM1jobSXZ0 VHGMNJ3MwXlrEFPXaYJgibcg6brM6wGfX/LBvc/haWw4yO24lT5eitm4UBdIy9pKkKmH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tHULdlzg8VjDnFZ3lfz5PWEOeIMk7Rj trjv82EZtrhLuLjHRCaG50OOm0hwPSk1J64R8O3HjSLdertmw7eyAYOo4RuWJguYMg5DRnBk WkRwrSuCn7UG+qVWZeKEsFKFOkynOs3pVbcbq1pxbhk3TRWCGRU5JolI4ohy/7JV1TVbjiDI HP/aVnm6NC8of26P40Pg8EdAhajZnHHjA7FrJXsy3cyIGqvg9os4rNkUWmrCfLLsZDHD8FnU mDW4+i+XlNFUPUYMrIKi9joBhu18ssf5i5Q= Message-ID: <40d02688-d40c-fcdd-b8eb-580a1e44b244@roeck-us.net> Date: Tue, 6 Apr 2021 13:09:00 -0700 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Thunderbird/68.10.0 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8 Content-Language: en-US Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 4/6/21 12:20 PM, Konstantin Aladyshev wrote: > Thanks for the comment. > Yes, you are correct, this patch adds an extra 'i2c_smbus_read_byte_data' call for the temp_max/temp_min reads. > I guess I did that intentionally because I just wanted to keep the restructured code concise. After all I thought, 'temp_input' generally is read more often than 'temp_max/temp_min'. > As I understand now, it seems like it is not acceptable. Therefore could you point me in the right direction about what I should do? > Should I just stick with the original driver version and simply add two more i2c call checks for the first operations for min/max? > Correct, it is not acceptable. A normal use case for hwmon devices is to use the "sensors" command which _will_ read all attributes. i2c reads are expensive, and unnecessary read operations should be avoided. There are several ways to solve the problem. Checking return values after each read is the simple option. There are other possibilities, such as reading the limits and the read order only once during probe, but I don't know enough about the hardware to suggest a more sophisticated solution. For example, I don't know what "CPU is off" means. Offline ? Not present ? If it means "not present", or if the status is permanent, the condition should be handled in the is_visible function (or the driver should not be instantiated in the first place). Otherwise, the code should possibly return -ENODATA instead of -ETIMEDOUT on error. But, again, I can not really suggest a better solution since I don't know enough (nothing, actually) about the hardware (and the public part of the SBTSI specification doesn't say anything about expected behavior for offline CPUs or CPU cores). What I did find, though, is that the driver does not implement temperature offset support, and it that doesn't support reporting alerts. I'd have assumed this to be more important than optimizing error handling, but that is just my personal opinion. Thanks, Guenter > Best regards, > Konstantin Aladyshev > > > On Tue, Apr 6, 2021 at 9:42 PM Guenter Roeck > wrote: > > On 4/6/21 11:16 AM, Konstantin Aladyshev wrote: > > SBTSI sensors don't work when the CPU is off. > > In this case every 'i2c_smbus_read_byte_data' function would fail > > by a timeout. > > Currently temp1_max/temp1_min file reads can cause two such timeouts > > for every read. > > Restructure code so there will be no more than one timeout for every > > read operation. > > > > Signed-off-by: Konstantin Aladyshev > > > --- > > Changes in v2: > >   - Fix typo in a commit message > >   - Don't swap temp_int/temp_dec checks at the end of the 'sbtsi_read' function > > > > This doesn't explain the reason for the extra read operation for > limits. Preventing a second read in error cases is not an argument > for adding an extra read for non-error cases. > > Guenter > > >  drivers/hwmon/sbtsi_temp.c | 55 +++++++++++++++++++------------------- > >  1 file changed, 27 insertions(+), 28 deletions(-) > > > > diff --git a/drivers/hwmon/sbtsi_temp.c b/drivers/hwmon/sbtsi_temp.c > > index e35357c48b8e..4ae48635bb31 100644 > > --- a/drivers/hwmon/sbtsi_temp.c > > +++ b/drivers/hwmon/sbtsi_temp.c > > @@ -74,48 +74,47 @@ static int sbtsi_read(struct device *dev, enum hwmon_sensor_types type, > >                     u32 attr, int channel, long *val) > >  { > >       struct sbtsi_data *data = dev_get_drvdata(dev); > > +     u8 temp_int_reg, temp_dec_reg; > >       s32 temp_int, temp_dec; > >       int err; > >  > >       switch (attr) { > >       case hwmon_temp_input: > > -             /* > > -              * ReadOrder bit specifies the reading order of integer and > > -              * decimal part of CPU temp for atomic reads. If bit == 0, > > -              * reading integer part triggers latching of the decimal part, > > -              * so integer part should be read first. If bit == 1, read > > -              * order should be reversed. > > -              */ > > -             err = i2c_smbus_read_byte_data(data->client, SBTSI_REG_CONFIG); > > -             if (err < 0) > > -                     return err; > > - > > -             mutex_lock(&data->lock); > > -             if (err & BIT(SBTSI_CONFIG_READ_ORDER_SHIFT)) { > > -                     temp_dec = i2c_smbus_read_byte_data(data->client, SBTSI_REG_TEMP_DEC); > > -                     temp_int = i2c_smbus_read_byte_data(data->client, SBTSI_REG_TEMP_INT); > > -             } else { > > -                     temp_int = i2c_smbus_read_byte_data(data->client, SBTSI_REG_TEMP_INT); > > -                     temp_dec = i2c_smbus_read_byte_data(data->client, SBTSI_REG_TEMP_DEC); > > -             } > > -             mutex_unlock(&data->lock); > > +             temp_int_reg = SBTSI_REG_TEMP_INT; > > +             temp_dec_reg = SBTSI_REG_TEMP_DEC; > >               break; > >       case hwmon_temp_max: > > -             mutex_lock(&data->lock); > > -             temp_int = i2c_smbus_read_byte_data(data->client, SBTSI_REG_TEMP_HIGH_INT); > > -             temp_dec = i2c_smbus_read_byte_data(data->client, SBTSI_REG_TEMP_HIGH_DEC); > > -             mutex_unlock(&data->lock); > > +             temp_int_reg = SBTSI_REG_TEMP_HIGH_INT; > > +             temp_dec_reg = SBTSI_REG_TEMP_HIGH_DEC; > >               break; > >       case hwmon_temp_min: > > -             mutex_lock(&data->lock); > > -             temp_int = i2c_smbus_read_byte_data(data->client, SBTSI_REG_TEMP_LOW_INT); > > -             temp_dec = i2c_smbus_read_byte_data(data->client, SBTSI_REG_TEMP_LOW_DEC); > > -             mutex_unlock(&data->lock); > > +             temp_int_reg = SBTSI_REG_TEMP_LOW_INT; > > +             temp_dec_reg = SBTSI_REG_TEMP_LOW_DEC; > >               break; > >       default: > >               return -EINVAL; > >       } > >  > > +     /* > > +      * ReadOrder bit specifies the reading order of integer and > > +      * decimal part of CPU temp for atomic reads. If bit == 0, > > +      * reading integer part triggers latching of the decimal part, > > +      * so integer part should be read first. If bit == 1, read > > +      * order should be reversed. > > +      */ > > +     err = i2c_smbus_read_byte_data(data->client, SBTSI_REG_CONFIG); > > +     if (err < 0) > > +             return err; > > + > > +     mutex_lock(&data->lock); > > +     if (err & BIT(SBTSI_CONFIG_READ_ORDER_SHIFT)) { > > +             temp_dec = i2c_smbus_read_byte_data(data->client, temp_dec_reg); > > +             temp_int = i2c_smbus_read_byte_data(data->client, temp_int_reg); > > +     } else { > > +             temp_int = i2c_smbus_read_byte_data(data->client, temp_int_reg); > > +             temp_dec = i2c_smbus_read_byte_data(data->client, temp_dec_reg); > > +     } > > +     mutex_unlock(&data->lock); > >  > >       if (temp_int < 0) > >               return temp_int; > > >