All of lore.kernel.org
 help / color / mirror / Atom feed
From: Guenter Roeck <linux@roeck-us.net>
To: Konstantin Aladyshev <aladyshev22@gmail.com>
Cc: Kun Yi <kunyi@google.com>, Jean Delvare <jdelvare@suse.com>,
	linux-hwmon@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: Re: [PATCH v2] hwmon: (sbtsi) Don't read sensor more than once if it doesn't respond
Date: Tue, 6 Apr 2021 13:09:00 -0700	[thread overview]
Message-ID: <40d02688-d40c-fcdd-b8eb-580a1e44b244@roeck-us.net> (raw)
In-Reply-To: <CACSj6VUCgxCQeA9EF4Oz+pKY+TdF9Gp=DV1V=-TcVE4ixtg45Q@mail.gmail.com>

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 <linux@roeck-us.net <mailto:linux@roeck-us.net>> 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 <aladyshev22@gmail.com <mailto:aladyshev22@gmail.com>>
>     > ---
>     > 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;
>     >
> 


  parent reply	other threads:[~2021-04-06 20:09 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-04-01 21:45 [PATCH] hwmon: (sbtsi) Don't read sensor more than once if it doesn't respond Konstantin Aladyshev
2021-04-05 21:41 ` Guenter Roeck
2021-04-06 18:16   ` [PATCH v2] " Konstantin Aladyshev
2021-04-06 18:42     ` Guenter Roeck
     [not found]       ` <CACSj6VUCgxCQeA9EF4Oz+pKY+TdF9Gp=DV1V=-TcVE4ixtg45Q@mail.gmail.com>
2021-04-06 20:09         ` Guenter Roeck [this message]
2021-04-06 21:09           ` Konstantin Aladyshev
2021-04-06 21:34             ` Guenter Roeck
2021-04-06 22:25               ` Konstantin Aladyshev
2021-04-06 23:28                 ` Guenter Roeck

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=40d02688-d40c-fcdd-b8eb-580a1e44b244@roeck-us.net \
    --to=linux@roeck-us.net \
    --cc=aladyshev22@gmail.com \
    --cc=jdelvare@suse.com \
    --cc=kunyi@google.com \
    --cc=linux-hwmon@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.