Re: [PATCH v2] drivers/hwmon/adm9240: fix data race in adm9240_fan_read

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Thu, Sep 22, 2022 at 4:50 PM Guenter Roeck <linux@xxxxxxxxxxxx> wrote:
>
> On 9/22/22 16:43, Li Zhong wrote:
> > In
> > adm9240_read()
> >    adm9240_fan_read()
> >      adm9240_write_fan_div(),
> >
> > it assumes that the caller of adm9240_write_fan_div() must hold
> > data->update_lock. Otherwise, it may cause data races when data is
> > updated by other threads.
> >
> > Signed-off-by: Li Zhong <floridsleeves@xxxxxxxxx>
> > ---
> > v2: add mutex_unlock() in error handling
> >
> >   drivers/hwmon/adm9240.c | 10 ++++++++--
> >   1 file changed, 8 insertions(+), 2 deletions(-)
> >
> > diff --git a/drivers/hwmon/adm9240.c b/drivers/hwmon/adm9240.c
> > index 483cd757abd3..d58999e186dc 100644
> > --- a/drivers/hwmon/adm9240.c
> > +++ b/drivers/hwmon/adm9240.c
> > @@ -501,16 +501,22 @@ static int adm9240_fan_read(struct device *dev, u32 attr, int channel, long *val
> >
> >       switch (attr) {
> >       case hwmon_fan_input:
> > +             mutex_lock(&data->update_lock);
> >               err = regmap_read(data->regmap, ADM9240_REG_FAN(channel), &regval);
> > -             if (err < 0)
> > +             if (err < 0) {
> > +                     mutex_unlock(&data->update_lock);
> >                       return err;
> > +             }
> >               if (regval == 255 && data->fan_div[channel] < 3) {
> >                       /* adjust fan clock divider on overflow */
> >                       err = adm9240_write_fan_div(data, channel,
> >                                                   ++data->fan_div[channel]);
> > -                     if (err)
> > +                     if (err) {
> > +                             mutex_unlock(&data->update_lock);
> >                               return err;
> > +                     }
> >               }
> > +             mutex_unlock(&data->update_lock);
> >               *val = FAN_FROM_REG(regval, BIT(data->fan_div[channel]));
>
> Unfortunately, this is still racy. All accesses to data->fan_div[channel]
> need to be included in the lock.
>
> Thanks,
> Guenter
>
> >               break;
> >       case hwmon_fan_div:
>

Updated in v3 patch. Sorry for the carelessness. Thanks for your suggestions.



[Index of Archives]     [LM Sensors]     [Linux Sound]     [ALSA Users]     [ALSA Devel]     [Linux Audio Users]     [Linux Media]     [Kernel]     [Gimp]     [Yosemite News]     [Linux Media]

  Powered by Linux