Skip to contentSkip to conversation

Input: Avoid kernel panic during device unregistration

PATCH In review Aniroop Mathur 2 participants 5 messages Dec 28, 2014 – Dec 29, 2014

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Input: Avoid kernel panic during device unregistration

This patch adds null check before actually unregistering the input device to avoid null pointer exception which leads to kernel panic.

So now, input device drivers won't have to worry about or add null case condition before calling input_unregister_device() in shutdown and remove functions.

---
 drivers/input/input.c | 3 +++
 1 file changed, 3 insertions(+)
drivers/input/input.c +3 -0
index 01fe49e..575219d 100644
@@ -2170,6 +2170,9 @@ EXPORT_SYMBOL(input_register_device);
  */
 void input_unregister_device(struct input_dev *dev)
 {
+	if (!dev)
+		return;
+
 	if (dev->devres_managed) {
 		WARN_ON(devres_destroy(dev->dev.parent,
 					devm_input_device_unregister,
Signature · 1 line
1.9.1
maintainer
#1 lore raw

Hi Aniroop,

Quoted 6 lines · Aniroop Mathur

This patch adds null check before actually unregistering the input device to avoid null pointer exception which leads to kernel panic.

So now, input device drivers won't have to worry about or add null case condition before calling input_unregister_device() in shutdown and remove functions.

input_unregister_device() should be called only if input_register_device() succeeded, which it would not with input device being NULL.

Unlike input_free_device() which does handle NULL argument, similar to many other "free" APIs I do not believe that input_unregister_device should be handling such cases.

Quoted 23 lines
---
 drivers/input/input.c | 3 +++
 1 file changed, 3 insertions(+)
drivers/input/input.c +3 -0
index 01fe49e..575219d 100644
@@ -2170,6 +2170,9 @@ EXPORT_SYMBOL(input_register_device);
  */
 void input_unregister_device(struct input_dev *dev)
 {
+	if (!dev)
+		return;
+
 	if (dev->devres_managed) {
 		WARN_ON(devres_destroy(dev->dev.parent,
 					devm_input_device_unregister,
Signature · 1 line
1.9.1

Thanks.

Signature · 1 line
Dmitry
#2 lore raw

Hello Mr. Torokhov,

Quoted 10 lines · Dmitry Torokhov

Hi Aniroop,

Quoted 6 lines · Aniroop Mathur

This patch adds null check before actually unregistering the input device to avoid null pointer exception which leads to kernel panic.

So now, input device drivers won't have to worry about or add null case condition before calling input_unregister_device() in shutdown and remove functions.

Quoted 8 lines

input_unregister_device() should be called only if input_register_device() succeeded, which it would not with input device being NULL.

Unlike input_free_device() which does handle NULL argument, similar to many other "free" APIs I do not believe that input_unregister_device should be handling such cases.

Right !!
Actually, quite recently I worked on one input device hub driver in which many devices are registered in probe and in shutdown and remove functions, they are unregistered.

probe() {
...
...
accel_dev = input_register_device();
gyro_dev = input_register_device();
mag_dev = input_register_device();
prox_dev = input_register_device();
light_dev = input_register_device();
baro_dev = input_register_device();
more ...
...
}

shutdown() {
...
...
if (accel_dev)
input_unregister_device(accel_dev);
if (gyro_dev)
input_unregister_device(gyro_dev);
if (mag_dev)
input_unregister_device(mag_dev);
if (prox_dev )
input_unregister_device(prox_dev);
if (light_dev)
input_unregister_device(light_dev);
if (baro_dev)
input_unregister_device(baro_dev);
more ...
...
}

In probe, few registrations may fail and so it is freed in probe itself. And in driver shutdown function, we need to unregister or free devices registered in probe.
So adding null check before every input_device_unregister() looks not quite good.
Similar thing for remove function in driver.
The best solution I thought is to add null check in input subsystem unregister function itself.
Umm... Is there any better way possible ?

Regards,
Aniroop

Quoted 28 lines
Quoted 21 lines
---
 drivers/input/input.c | 3 +++
 1 file changed, 3 insertions(+)
drivers/input/input.c +3 -1
index 01fe49e..575219d 100644
@@ -2170,6 +2170,9 @@ EXPORT_SYMBOL(input_register_device);
  */
 void input_unregister_device(struct input_dev *dev)
 {
+     if (!dev)
+             return;
+
      if (dev->devres_managed) {
              WARN_ON(devres_destroy(dev->dev.parent,
                                      devm_input_device_unregister,
--
Quoted 2 lines

1.9.1

Thanks.

--
Dmitry

maintainer
#3 lore raw

Hi Aniroop,

Quoted 60 lines · Aniroop Mathur

Hello Mr. Torokhov,

Quoted 10 lines · Dmitry Torokhov

Hi Aniroop,

Quoted 6 lines · Aniroop Mathur

This patch adds null check before actually unregistering the input device to avoid null pointer exception which leads to kernel panic.

So now, input device drivers won't have to worry about or add null case condition before calling input_unregister_device() in shutdown and remove functions.

Quoted 8 lines

input_unregister_device() should be called only if input_register_device() succeeded, which it would not with input device being NULL.

Unlike input_free_device() which does handle NULL argument, similar to many other "free" APIs I do not believe that input_unregister_device should be handling such cases.

Right !!
Actually, quite recently I worked on one input device hub driver in which many devices are registered in probe and in shutdown and remove functions, they are unregistered.

probe() {
...
...
accel_dev = input_register_device();
gyro_dev = input_register_device();
mag_dev = input_register_device();
prox_dev = input_register_device();
light_dev = input_register_device();
baro_dev = input_register_device();
more ...
...
}

shutdown() {
...
...
if (accel_dev)
input_unregister_device(accel_dev);
if (gyro_dev)
input_unregister_device(gyro_dev);
if (mag_dev)
input_unregister_device(mag_dev);
if (prox_dev )
input_unregister_device(prox_dev);
if (light_dev)
input_unregister_device(light_dev);
if (baro_dev)
input_unregister_device(baro_dev);
more ...
...
}

Quoted 1 line

In probe, few registrations may fail and so it is freed in probe itself.

Why would they fail? Is it because the hardware is not there or other errors?

Quoted 8 lines

And in driver shutdown function, we need to unregister or free devices registered in probe.
So adding null check before every input_device_unregister() looks not quite good.
Similar thing for remove function in driver.
The best solution I thought is to add null check in input subsystem unregister function itself.
Umm... Is there any better way possible ?

I would look into using devm_* infrastructure instead and simply not allocate input devices for any sub-devices of your "hub" that are not present and simply aborting the probe() for other errors. Then you would not need pretty much any code in your remove() method.

If not devm_ then you can consider creating array of struct input_dev * and iterating it in error paths and remove() instead of long open-coded sequence of unregistering. Then a single NULL check won't be seen as such an issue.

Thanks.

Signature · 1 line
Dmitry
#4 lore raw

Hello Mr. Torokhov,

Quoted 63 lines · Dmitry Torokhov

Hi Aniroop,

Quoted 60 lines · Aniroop Mathur

Hello Mr. Torokhov,

Quoted 10 lines · Dmitry Torokhov

Hi Aniroop,

Quoted 6 lines · Aniroop Mathur

This patch adds null check before actually unregistering the input device to avoid null pointer exception which leads to kernel panic.

So now, input device drivers won't have to worry about or add null case condition before calling input_unregister_device() in shutdown and remove functions.

Quoted 8 lines

input_unregister_device() should be called only if input_register_device() succeeded, which it would not with input device being NULL.

Unlike input_free_device() which does handle NULL argument, similar to many other "free" APIs I do not believe that input_unregister_device should be handling such cases.

Right !!
Actually, quite recently I worked on one input device hub driver in which many devices are registered in probe and in shutdown and remove functions, they are unregistered.

probe() {
...
...
accel_dev = input_register_device();
gyro_dev = input_register_device();
mag_dev = input_register_device();
prox_dev = input_register_device();
light_dev = input_register_device();
baro_dev = input_register_device();
more ...
...
}

shutdown() {
...
...
if (accel_dev)
input_unregister_device(accel_dev);
if (gyro_dev)
input_unregister_device(gyro_dev);
if (mag_dev)
input_unregister_device(mag_dev);
if (prox_dev )
input_unregister_device(prox_dev);
if (light_dev)
input_unregister_device(light_dev);
if (baro_dev)
input_unregister_device(baro_dev);
more ...
...
}

Quoted 5 lines
Quoted 1 line

In probe, few registrations may fail and so it is freed in probe itself.

Why would they fail? Is it because the hardware is not there or other errors?

They never fail as such.
But still handling error chances for very rare cases like memory not avaliable, etc.

Quoted 14 lines
Quoted 8 lines

And in driver shutdown function, we need to unregister or free devices registered in probe.
So adding null check before every input_device_unregister() looks not quite good.
Similar thing for remove function in driver.
The best solution I thought is to add null check in input subsystem unregister function itself.
Umm... Is there any better way possible ?

I would look into using devm_* infrastructure instead and simply not allocate input devices for any sub-devices of your "hub" that are not present and simply aborting the probe() for other errors. Then you would not need pretty much any code in your remove() method.

Quoted 5 lines

If not devm_ then you can consider creating array of struct input_dev * and iterating it in error paths and remove() instead of long open-coded sequence of unregistering. Then a single NULL check won't be seen as such an issue.

Seems better approach. I'll try to follow the same.

Thanks,
Aniroop

Quoted 4 lines

Thanks.

--
Dmitry