Skip to content

Commit 1510dd5

Browse files
pielerictorvalds
authored andcommitted
lis3lv02d: avoid divide by zero due to unchecked
After an "unexpected" reboot, I found this Oops in my logs: divide error: 0000 [raspberrypi#1] PREEMPT SMP=20 CPU 0=20 Modules linked in: lis3lv02d hp_wmi input_polldev [...] Pid: 390, comm: modprobe Tainted: G C 2.6.39-rc7-wl+=20 RIP: 0010:[<ffffffffa014b427>] [<ffffffffa014b427>] lis3lv02d_poweron+0x4e/0x94 [lis3lv02d] RSP: 0018:ffff8801d6407cf8 EFLAGS: 00010246 RAX: 0000000000000bb8 RBX: ffffffffa014e000 RCX: 0000000000000000 RDX: 0000000000000000 RSI: ffffea00066e4708 RDI: ffff8801df002700 RBP: ffff8801d6407d18 R08: ffffea00066c5a30 R09: ffffffff812498c9 R10: ffff8801d7bfcea0 R11: ffff8801d7bfce10 R12: 0000000000000bb8 R13: 00000000ffffffda R14: ffffffffa0154120 R15: ffffffffa0154030 =46S: 00007fc0705db700(0000) GS:ffff8801dfa00000(0000) knlGS:0 CS: 0010 DS: 0000 ES: 0000 CR0: 000000008005003b CR2: 00007f33549174f0 CR3: 00000001d65c9000 CR4: 00000000000406f0 Process modprobe (pid: 390, threadinfo ffff8801d6406000, task ffff8801d6b40= 000) Stack: ffffffffa0154120 62ffffffa0154030 ffffffffa014e000 00000000ffffffea ffff8801d6407d58 ffffffffa014bcc1 0000000000000000 0000000000000048 ffff8801d8bae800 00000000ffffffea 00000000ffffffda ffffffffa0154120 Call Trace: [<ffffffffa014bcc1>] lis3lv02d_init_device+0x1ce/0x496 [lis3lv02d] [<ffffffffa01522ff>] lis3lv02d_add+0x10f/0x17c [hp_accel] [<ffffffff81233e11>] acpi_device_probe+0x49/0x117 [...] Code: 3a 75 06 80 4d ef 50 eb 04 80 4d ef 40 0f b6 55 ef be 21 00 00 00 48 89 df ff 53 18 44 8b 63 6c e8 3e fc ff ff 89 c1 44 89 e0 99 <f7> f9 89 c7 e8 93 82 ef e0 48 83 7b 30 00 74 2d 45 31 e4 80 7b=20 RIP [<ffffffffa014b427>] lis3lv02d_poweron+0x4e/0x94 [lis3lv02d] RSP <ffff8801d6407cf8> >From my POV, it looks like the hardware is not working as expected and returns a bogus data rate. The driver doesn't check the result and directly uses it as some sort of divisor in some places: msleep(lis3->pwron_delay / lis3lv02d_get_odr()); Under this circumstances, this could very well cause the "divide by zero" exception from above. For now, I fixed it the easiest and most obvious way: Check if the result is sane and if it isn't use a sane default instead. I went for "100" in the latter case, simply because /sys/devices/platform/lis3lv02d/rate returns it on a successful boot. Signed-off-by: Christian Lamparter <chunkeey@googlemail.com> Signed-off-by: Éric Piel <eric.piel@tremplin-utc.net> Cc: Matthew Garrett <mjg@redhat.com> Cc: Witold Pilat <witold.pilat@gmail.com> Cc: Lyall Pearce <lyall.pearce@hp.com> Cc: Malte Starostik <m-starostik@versanet.de> Cc: Ilkka Koskinen <ilkka.koskinen@nokia.com> Cc: Thadeu Lima de Souza Cascardo <cascardo@holoscopio.com> Cc: Christian Lamparter <chunkeey@googlemail.com> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent 6580704 commit 1510dd5

3 files changed

Lines changed: 32 additions & 9 deletions

File tree

‎drivers/misc/lis3lv02d/lis3lv02d.c‎

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,18 @@ static int lis3lv02d_get_odr(void)
206206
return lis3_dev.odrs[(ctrl >> shift)];
207207
}
208208

209+
static int lis3lv02d_get_pwron_wait(struct lis3lv02d *lis3)
210+
{
211+
int div = lis3lv02d_get_odr();
212+
213+
if (WARN_ONCE(div == 0, "device returned spurious data"))
214+
return -ENXIO;
215+
216+
/* LIS3 power on delay is quite long */
217+
msleep(lis3->pwron_delay / div);
218+
return 0;
219+
}
220+
209221
static int lis3lv02d_set_odr(int rate)
210222
{
211223
u8 ctrl;
@@ -266,7 +278,9 @@ static int lis3lv02d_selftest(struct lis3lv02d *lis3, s16 results[3])
266278

267279
lis3->read(lis3, ctlreg, &reg);
268280
lis3->write(lis3, ctlreg, (reg | selftest));
269-
msleep(lis3->pwron_delay / lis3lv02d_get_odr());
281+
ret = lis3lv02d_get_pwron_wait(lis3);
282+
if (ret)
283+
goto fail;
270284

271285
/* Read directly to avoid axis remap */
272286
x = lis3->read_data(lis3, OUTX);
@@ -275,7 +289,9 @@ static int lis3lv02d_selftest(struct lis3lv02d *lis3, s16 results[3])
275289

276290
/* back to normal settings */
277291
lis3->write(lis3, ctlreg, reg);
278-
msleep(lis3->pwron_delay / lis3lv02d_get_odr());
292+
ret = lis3lv02d_get_pwron_wait(lis3);
293+
if (ret)
294+
goto fail;
279295

280296
results[0] = x - lis3->read_data(lis3, OUTX);
281297
results[1] = y - lis3->read_data(lis3, OUTY);
@@ -363,8 +379,9 @@ void lis3lv02d_poweroff(struct lis3lv02d *lis3)
363379
}
364380
EXPORT_SYMBOL_GPL(lis3lv02d_poweroff);
365381

366-
void lis3lv02d_poweron(struct lis3lv02d *lis3)
382+
int lis3lv02d_poweron(struct lis3lv02d *lis3)
367383
{
384+
int err;
368385
u8 reg;
369386

370387
lis3->init(lis3);
@@ -384,11 +401,14 @@ void lis3lv02d_poweron(struct lis3lv02d *lis3)
384401
lis3->write(lis3, CTRL_REG2, reg);
385402
}
386403

387-
/* LIS3 power on delay is quite long */
388-
msleep(lis3->pwron_delay / lis3lv02d_get_odr());
404+
err = lis3lv02d_get_pwron_wait(lis3);
405+
if (err)
406+
return err;
389407

390408
if (lis3->reg_ctrl)
391409
lis3_context_restore(lis3);
410+
411+
return 0;
392412
}
393413
EXPORT_SYMBOL_GPL(lis3lv02d_poweron);
394414

@@ -928,7 +948,11 @@ int lis3lv02d_init_device(struct lis3lv02d *dev)
928948
atomic_set(&dev->wake_thread, 0);
929949

930950
lis3lv02d_add_fs(dev);
931-
lis3lv02d_poweron(dev);
951+
err = lis3lv02d_poweron(dev);
952+
if (err) {
953+
lis3lv02d_remove_fs(dev);
954+
return err;
955+
}
932956

933957
if (dev->pm_dev) {
934958
pm_runtime_set_active(dev->pm_dev);

‎drivers/misc/lis3lv02d/lis3lv02d.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,7 @@ int lis3lv02d_init_device(struct lis3lv02d *lis3);
285285
int lis3lv02d_joystick_enable(void);
286286
void lis3lv02d_joystick_disable(void);
287287
void lis3lv02d_poweroff(struct lis3lv02d *lis3);
288-
void lis3lv02d_poweron(struct lis3lv02d *lis3);
288+
int lis3lv02d_poweron(struct lis3lv02d *lis3);
289289
int lis3lv02d_remove_fs(struct lis3lv02d *lis3);
290290

291291
extern struct lis3lv02d lis3_dev;

‎drivers/platform/x86/hp_accel.c‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -354,8 +354,7 @@ static int lis3lv02d_suspend(struct acpi_device *device, pm_message_t state)
354354

355355
static int lis3lv02d_resume(struct acpi_device *device)
356356
{
357-
lis3lv02d_poweron(&lis3_dev);
358-
return 0;
357+
return lis3lv02d_poweron(&lis3_dev);
359358
}
360359
#else
361360
#define lis3lv02d_suspend NULL

0 commit comments

Comments
 (0)