diff mbox series

[linux,dev-5.1] hwmon (occ): Fix division by zero issue

Message ID 1562574721-125584-1-git-send-email-mine260309@gmail.com
State Accepted, archived
Headers show
Series [linux,dev-5.1] hwmon (occ): Fix division by zero issue | expand

Commit Message

Lei YU July 8, 2019, 8:32 a.m. UTC
The code in occ_get_powr_avg() invokes div64_u64() without checking the
divisor. In case the divisor is zero, kernel gets an "Division by zero
in kernel" error.

Check the divisor and make it return 0 if the divisor is 0.

Signed-off-by: Lei YU <mine260309@gmail.com>
---
 drivers/hwmon/occ/common.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)

Comments

Eddie James July 8, 2019, 3:18 p.m. UTC | #1
On 7/8/19 3:32 AM, Lei YU wrote:
> The code in occ_get_powr_avg() invokes div64_u64() without checking the
> divisor. In case the divisor is zero, kernel gets an "Division by zero
> in kernel" error.
>
> Check the divisor and make it return 0 if the divisor is 0.


Thanks!

Reviewed-by: Eddie James <eajames@linux.ibm.com>


>
> Signed-off-by: Lei YU <mine260309@gmail.com>
> ---
>   drivers/hwmon/occ/common.c | 6 ++++--
>   1 file changed, 4 insertions(+), 2 deletions(-)
>
> diff --git a/drivers/hwmon/occ/common.c b/drivers/hwmon/occ/common.c
> index e9d7167..15c5d43 100644
> --- a/drivers/hwmon/occ/common.c
> +++ b/drivers/hwmon/occ/common.c
> @@ -406,8 +406,10 @@ static ssize_t occ_show_power_1(struct device *dev,
>
>   static u64 occ_get_powr_avg(u64 *accum, u32 *samples)
>   {
> -	return div64_u64(get_unaligned_be64(accum) * 1000000ULL,
> -			 get_unaligned_be32(samples));
> +	u64 divisor = get_unaligned_be32(samples);
> +
> +	return (divisor == 0) ? 0 :
> +		div64_u64(get_unaligned_be64(accum) * 1000000ULL, divisor);
>   }
>
>   static ssize_t occ_show_power_2(struct device *dev,
Joel Stanley July 11, 2019, 1:51 a.m. UTC | #2
On Mon, 8 Jul 2019 at 15:19, Eddie James <eajames@linux.vnet.ibm.com> wrote:
>
>
> On 7/8/19 3:32 AM, Lei YU wrote:
> > The code in occ_get_powr_avg() invokes div64_u64() without checking the
> > divisor. In case the divisor is zero, kernel gets an "Division by zero
> > in kernel" error.
> >
> > Check the divisor and make it return 0 if the divisor is 0.
>
>
> Thanks!
>
> Reviewed-by: Eddie James <eajames@linux.ibm.com>

Merged into dev-5.2.

Lei, can you please submit this to upstream? Please add Eddie's r-b to
the commit message.

Cheers,

Joel

>
>
> >
> > Signed-off-by: Lei YU <mine260309@gmail.com>
> > ---
> >   drivers/hwmon/occ/common.c | 6 ++++--
> >   1 file changed, 4 insertions(+), 2 deletions(-)
> >
> > diff --git a/drivers/hwmon/occ/common.c b/drivers/hwmon/occ/common.c
> > index e9d7167..15c5d43 100644
> > --- a/drivers/hwmon/occ/common.c
> > +++ b/drivers/hwmon/occ/common.c
> > @@ -406,8 +406,10 @@ static ssize_t occ_show_power_1(struct device *dev,
> >
> >   static u64 occ_get_powr_avg(u64 *accum, u32 *samples)
> >   {
> > -     return div64_u64(get_unaligned_be64(accum) * 1000000ULL,
> > -                      get_unaligned_be32(samples));
> > +     u64 divisor = get_unaligned_be32(samples);
> > +
> > +     return (divisor == 0) ? 0 :
> > +             div64_u64(get_unaligned_be64(accum) * 1000000ULL, divisor);
> >   }
> >
> >   static ssize_t occ_show_power_2(struct device *dev,
>
Lei YU July 11, 2019, 2:02 a.m. UTC | #3
Sure, will do.

On Thu, Jul 11, 2019 at 9:51 AM Joel Stanley <joel@jms.id.au> wrote:
>
> On Mon, 8 Jul 2019 at 15:19, Eddie James <eajames@linux.vnet.ibm.com> wrote:
> >
> >
> > On 7/8/19 3:32 AM, Lei YU wrote:
> > > The code in occ_get_powr_avg() invokes div64_u64() without checking the
> > > divisor. In case the divisor is zero, kernel gets an "Division by zero
> > > in kernel" error.
> > >
> > > Check the divisor and make it return 0 if the divisor is 0.
> >
> >
> > Thanks!
> >
> > Reviewed-by: Eddie James <eajames@linux.ibm.com>
>
> Merged into dev-5.2.
>
> Lei, can you please submit this to upstream? Please add Eddie's r-b to
> the commit message.
>
> Cheers,
>
> Joel
>
> >
> >
> > >
> > > Signed-off-by: Lei YU <mine260309@gmail.com>
> > > ---
> > >   drivers/hwmon/occ/common.c | 6 ++++--
> > >   1 file changed, 4 insertions(+), 2 deletions(-)
> > >
> > > diff --git a/drivers/hwmon/occ/common.c b/drivers/hwmon/occ/common.c
> > > index e9d7167..15c5d43 100644
> > > --- a/drivers/hwmon/occ/common.c
> > > +++ b/drivers/hwmon/occ/common.c
> > > @@ -406,8 +406,10 @@ static ssize_t occ_show_power_1(struct device *dev,
> > >
> > >   static u64 occ_get_powr_avg(u64 *accum, u32 *samples)
> > >   {
> > > -     return div64_u64(get_unaligned_be64(accum) * 1000000ULL,
> > > -                      get_unaligned_be32(samples));
> > > +     u64 divisor = get_unaligned_be32(samples);
> > > +
> > > +     return (divisor == 0) ? 0 :
> > > +             div64_u64(get_unaligned_be64(accum) * 1000000ULL, divisor);
> > >   }
> > >
> > >   static ssize_t occ_show_power_2(struct device *dev,
> >
diff mbox series

Patch

diff --git a/drivers/hwmon/occ/common.c b/drivers/hwmon/occ/common.c
index e9d7167..15c5d43 100644
--- a/drivers/hwmon/occ/common.c
+++ b/drivers/hwmon/occ/common.c
@@ -406,8 +406,10 @@  static ssize_t occ_show_power_1(struct device *dev,
 
 static u64 occ_get_powr_avg(u64 *accum, u32 *samples)
 {
-	return div64_u64(get_unaligned_be64(accum) * 1000000ULL,
-			 get_unaligned_be32(samples));
+	u64 divisor = get_unaligned_be32(samples);
+
+	return (divisor == 0) ? 0 :
+		div64_u64(get_unaligned_be64(accum) * 1000000ULL, divisor);
 }
 
 static ssize_t occ_show_power_2(struct device *dev,