Skip to contentSkip to conversation

sound: Don't assume i2c device probing always succeeds

PATCH In review Jean Delvare 3 participants 10 messages Sep 30, 2009 – Oct 5, 2009

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

PATCH 1/1 sound: Don't assume i2c device probing always succeeds

If i2c device probing fails, then there is no driver to dereference after calling i2c_new_device(). Stop assuming that probing will always succeed, to avoid NULL pointer dereferences. We have an easier access to the driver anyway.

---
The code is similar to the one in therm_adt746x, for which Tim reported a real-world oops, so it should be fixed ASAP.

 sound/aoa/codecs/onyx.c |    4 +++-
 sound/aoa/codecs/tas.c  |    4 +++-
 sound/ppc/keywest.c     |    4 +++-
 3 files changed, 9 insertions(+), 3 deletions(-)
sound/aoa/codecs/onyx.c +3 -1
@@ -996,6 +996,8 @@ static void onyx_exit_codec(struct aoa_c
 	onyx->codec.soundbus_dev->detach_codec(onyx->codec.soundbus_dev, onyx);
 }
 
+static struct i2c_driver onyx_driver;
+
 static int onyx_create(struct i2c_adapter *adapter,
 		       struct device_node *node,
 		       int addr)
@@ -1027,7 +1029,7 @@ static int onyx_create(struct i2c_adapte
 	 * Let i2c-core delete that device on driver removal.
 	 * This is safe because i2c-core holds the core_lock mutex for us.
 	 */
-	list_add_tail(&client->detected, &client->driver->clients);
+	list_add_tail(&client->detected, &onyx_driver.clients);
 	return 0;
 }
 
sound/aoa/codecs/tas.c +3 -1
@@ -882,6 +882,8 @@ static void tas_exit_codec(struct aoa_co
 }
 
 
+static struct i2c_driver tas_driver;
+
 static int tas_create(struct i2c_adapter *adapter,
 		       struct device_node *node,
 		       int addr)
@@ -902,7 +904,7 @@ static int tas_create(struct i2c_adapter
 	 * Let i2c-core delete that device on driver removal.
 	 * This is safe because i2c-core holds the core_lock mutex for us.
 	 */
-	list_add_tail(&client->detected, &client->driver->clients);
+	list_add_tail(&client->detected, &tas_driver.clients);
 	return 0;
 }
 
sound/ppc/keywest.c +3 -1
@@ -40,6 +40,8 @@ static int keywest_probe(struct i2c_clie
 	return 0;
 }
 
+struct i2c_driver keywest_driver;
+
 /*
  * This is kind of a hack, best would be to turn powermac to fixed i2c
  * bus numbers and declare the sound device as part of platform
@@ -65,7 +67,7 @@ static int keywest_attach_adapter(struct
 	 * This is safe because i2c-core holds the core_lock mutex for us.
 	 */
 	list_add_tail(&keywest_ctx->client->detected,
-		      &keywest_ctx->client->driver->clients);
+		      &keywest_driver.clients);
 	return 0;
 }
 
Signature · 1 line
Jean Delvare
#1 lore raw

At Wed, 30 Sep 2009 15:25:42 +0200,

Quoted 6 lines · Jean Delvare

If i2c device probing fails, then there is no driver to dereference after calling i2c_new_device(). Stop assuming that probing will always succeed, to avoid NULL pointer dereferences. We have an easier access to the driver anyway.

Quoted 6 lines

---
The code is similar to the one in therm_adt746x, for which Tim reported a real-world oops, so it should be fixed ASAP.

Jean, thanks for the patch.

I'm just wondering whether the additional NULL check of client->driver would be enough? If yes, sound/aoa/onyx.c has it, at least, and we can add the similar checks to the rest, too.

Takashi

Quoted 71 lines
 sound/aoa/codecs/onyx.c |    4 +++-
 sound/aoa/codecs/tas.c  |    4 +++-
 sound/ppc/keywest.c     |    4 +++-
 3 files changed, 9 insertions(+), 3 deletions(-)
sound/aoa/codecs/onyx.c +3 -1
@@ -996,6 +996,8 @@ static void onyx_exit_codec(struct aoa_c
 	onyx->codec.soundbus_dev->detach_codec(onyx->codec.soundbus_dev, onyx);
 }
 
+static struct i2c_driver onyx_driver;
+
 static int onyx_create(struct i2c_adapter *adapter,
 		       struct device_node *node,
 		       int addr)
@@ -1027,7 +1029,7 @@ static int onyx_create(struct i2c_adapte
 	 * Let i2c-core delete that device on driver removal.
 	 * This is safe because i2c-core holds the core_lock mutex for us.
 	 */
-	list_add_tail(&client->detected, &client->driver->clients);
+	list_add_tail(&client->detected, &onyx_driver.clients);
 	return 0;
 }
 
sound/aoa/codecs/tas.c +3 -1
@@ -882,6 +882,8 @@ static void tas_exit_codec(struct aoa_co
 }
 
 
+static struct i2c_driver tas_driver;
+
 static int tas_create(struct i2c_adapter *adapter,
 		       struct device_node *node,
 		       int addr)
@@ -902,7 +904,7 @@ static int tas_create(struct i2c_adapter
 	 * Let i2c-core delete that device on driver removal.
 	 * This is safe because i2c-core holds the core_lock mutex for us.
 	 */
-	list_add_tail(&client->detected, &client->driver->clients);
+	list_add_tail(&client->detected, &tas_driver.clients);
 	return 0;
 }
 
sound/ppc/keywest.c +3 -1
@@ -40,6 +40,8 @@ static int keywest_probe(struct i2c_clie
 	return 0;
 }
 
+struct i2c_driver keywest_driver;
+
 /*
  * This is kind of a hack, best would be to turn powermac to fixed i2c
  * bus numbers and declare the sound device as part of platform
@@ -65,7 +67,7 @@ static int keywest_attach_adapter(struct
 	 * This is safe because i2c-core holds the core_lock mutex for us.
 	 */
 	list_add_tail(&keywest_ctx->client->detected,
-		      &keywest_ctx->client->driver->clients);
+		      &keywest_driver.clients);
 	return 0;
 }
 
Signature · 2 lines
-- 
Jean Delvare
#2 lore raw

Hi Takashi,

Thanks for the swift reply.

Quoted 15 lines · Takashi Iwai

At Wed, 30 Sep 2009 15:25:42 +0200,

Quoted 6 lines · Jean Delvare

If i2c device probing fails, then there is no driver to dereference after calling i2c_new_device(). Stop assuming that probing will always succeed, to avoid NULL pointer dereferences. We have an easier access to the driver anyway.

Quoted 6 lines

---
The code is similar to the one in therm_adt746x, for which Tim reported a real-world oops, so it should be fixed ASAP.

Quoted 5 lines

Jean, thanks for the patch.

I'm just wondering whether the additional NULL check of client->driver would be enough? If yes, sound/aoa/onyx.c has it, at least, and we can add the similar checks to the rest, too.

The NULL check of client->driver, if followed by a call to i2c_unregister_device(), would indeed be enough. But unlike the onyx driver which we know we sometimes load erroneously, the other drivers should never fail. I am reluctant to add code to handle error cases which are not supposed to happen, which is why I prefer my proposed
fix: it does not add code.

That being said, I will be happy with any solution that fixes the problem, so if you prefer client->driver NULL checks, I can send a patch doing this.

Thanks,

Signature · 1 line
Jean Delvare
maintainer
#3 lore raw
Quoted 4 lines · Jean Delvare

The NULL check of client->driver, if followed by a call to i2c_unregister_device(), would indeed be enough. But unlike the onyx driver which we know we sometimes load erroneously, the other drivers should never fail.

All of these drivers can be loaded manually and then fail though, or am I misunderstanding something?

johannes

_______________________________________________
Alsa-devel mailing list
[email protected]
http://mailman.alsa-project.org/mailman/listinfo/alsa-devel

#4 lore raw
Quoted 9 lines · Johannes Berg
Quoted 4 lines · Jean Delvare

The NULL check of client->driver, if followed by a call to i2c_unregister_device(), would indeed be enough. But unlike the onyx driver which we know we sometimes load erroneously, the other drivers should never fail.

All of these drivers can be loaded manually and then fail though, or am I misunderstanding something?

I don't think so. At least tas and keywest have checks before they attempt to instantiate an i2c client, which I think are reliable. The onyx case is different because apparently some machines have it but are difficult to detect:

	/* if that didn't work, try desperate mode for older
	 * machines that have stuff missing from the device tree */

And then we have to attempt to instantiate i2c devices at a not completely known address, and that may fail. I think this is the reason why onyx has this extra client->driver NULL check and the other two drivers do not.

I would really love if someone with good knowledge of the device tree on mac would convert all these hacks to proper i2c device declarations. All the infrastructure is available already, but I don't know enough about open firmware and mac the device tree to do it myself.

Signature · 1 line
Jean Delvare
#5 lore raw
In reply to Jean Delvare

At Wed, 30 Sep 2009 17:00:06 +0200,

Quoted 34 lines · Jean Delvare

Hi Takashi,

Thanks for the swift reply.

Quoted 15 lines · Takashi Iwai

At Wed, 30 Sep 2009 15:25:42 +0200,

Quoted 6 lines · Jean Delvare

If i2c device probing fails, then there is no driver to dereference after calling i2c_new_device(). Stop assuming that probing will always succeed, to avoid NULL pointer dereferences. We have an easier access to the driver anyway.

Quoted 6 lines

---
The code is similar to the one in therm_adt746x, for which Tim reported a real-world oops, so it should be fixed ASAP.

Quoted 5 lines

Jean, thanks for the patch.

I'm just wondering whether the additional NULL check of client->driver would be enough? If yes, sound/aoa/onyx.c has it, at least, and we can add the similar checks to the rest, too.

The NULL check of client->driver, if followed by a call to i2c_unregister_device(), would indeed be enough. But unlike the onyx driver which we know we sometimes load erroneously, the other drivers should never fail. I am reluctant to add code to handle error cases which are not supposed to happen, which is why I prefer my proposed
fix: it does not add code.

Quoted 3 lines

That being said, I will be happy with any solution that fixes the problem, so if you prefer client->driver NULL checks, I can send a patch doing this.

Yes, indeed I prefer NULL check because the user can know the error at the right place. I share your concern about the code addition, though :)

I already made a patch below, but it's totally untested. It'd be helpful if someone can do review and build-test it.

thanks,

Takashi

---

sound/aoa/codecs/tas.c +4 -0
index f0ebc97..0f810c8 100644
@@ -897,6 +897,10 @@ static int tas_create(struct i2c_adapter *adapter,
 	client = i2c_new_device(adapter, &info);
 	if (!client)
 		return -ENODEV;
+	if (!client->driver) {
+		i2c_unregister_device(client);
+		return -ENODEV;
+	}
 
 	/*
 	 * Let i2c-core delete that device on driver removal.
sound/ppc/keywest.c +7 -0
index 835fa19..473c5a6 100644
@@ -59,6 +59,13 @@ static int keywest_attach_adapter(struct i2c_adapter *adapter)
 	strlcpy(info.type, "keywest", I2C_NAME_SIZE);
 	info.addr = keywest_ctx->addr;
 	keywest_ctx->client = i2c_new_device(adapter, &info);
+	if (!keywest_ctx->client)
+		return -ENODEV;
+	if (!keywest_ctx->client->driver) {
+		i2c_unregister_device(keywest_ctx->client);
+		keywest_ctx->client = NULL;
+		return -ENODEV;
+	}
 	
 	/*
 	 * Let i2c-core delete that device on driver removal.
#6 lore raw
Quoted 36 lines · Takashi Iwai

Yes, indeed I prefer NULL check because the user can know the error at the right place. I share your concern about the code addition, though :)

I already made a patch below, but it's totally untested. It'd be helpful if someone can do review and build-test it.

thanks,

Takashi

---

sound/aoa/codecs/tas.c +4 -0
index f0ebc97..0f810c8 100644
@@ -897,6 +897,10 @@ static int tas_create(struct i2c_adapter *adapter,
 	client = i2c_new_device(adapter, &info);
 	if (!client)
 		return -ENODEV;
+	if (!client->driver) {
+		i2c_unregister_device(client);
+		return -ENODEV;
+	}
 
 	/*
 	 * Let i2c-core delete that device on driver removal.
sound/ppc/keywest.c +0 -0
index 835fa19..473c5a6 100644
@@ -59,6 +59,13 @@ static int keywest_attach_adapter(struct i2c_adapter *adapter)
 	strlcpy(info.type, "keywest", I2C_NAME_SIZE);
 	info.addr = keywest_ctx->addr;
 	keywest_ctx->client = i2c_new_device(adapter, &info);

sound/ppc/keywest.c static int keywest_attach_adapter(struct i2c_adapter *adapter) · line 71

Quoted 10 lines
sound/ppc/keywest.c +7 -0
+	if (!keywest_ctx->client)
+		return -ENODEV;
+	if (!keywest_ctx->client->driver) {
+		i2c_unregister_device(keywest_ctx->client);
+		keywest_ctx->client = NULL;
+		return -ENODEV;
+	}
 
 	/*
 	 * Let i2c-core delete that device on driver removal.

This looks good to me. Please add the following comment before the client->driver check in both drivers:

/*
* We know the driver is already loaded, so the device should be
* already bound. If not it means binding failed, and then there
* is no point in keeping the device instantiated. */

Otherwise it's a little difficult to understand why the check is there.

Signature · 1 line
Jean Delvare
#7 lore raw

At Wed, 30 Sep 2009 18:55:05 +0200,

Quoted 52 lines · Jean Delvare
Quoted 36 lines · Takashi Iwai

Yes, indeed I prefer NULL check because the user can know the error at the right place. I share your concern about the code addition, though :)

I already made a patch below, but it's totally untested. It'd be helpful if someone can do review and build-test it.

thanks,

Takashi

---

sound/aoa/codecs/tas.c +4 -0
index f0ebc97..0f810c8 100644
@@ -897,6 +897,10 @@ static int tas_create(struct i2c_adapter *adapter,
 	client = i2c_new_device(adapter, &info);
 	if (!client)
 		return -ENODEV;
+	if (!client->driver) {
+		i2c_unregister_device(client);
+		return -ENODEV;
+	}
 
 	/*
 	 * Let i2c-core delete that device on driver removal.
sound/ppc/keywest.c +0 -0
index 835fa19..473c5a6 100644
@@ -59,6 +59,13 @@ static int keywest_attach_adapter(struct i2c_adapter *adapter)
 	strlcpy(info.type, "keywest", I2C_NAME_SIZE);
 	info.addr = keywest_ctx->addr;
 	keywest_ctx->client = i2c_new_device(adapter, &info);

sound/ppc/keywest.c static int keywest_attach_adapter(struct i2c_adapter *adapter) · line 71

Quoted 10 lines
sound/ppc/keywest.c +7 -0
+	if (!keywest_ctx->client)
+		return -ENODEV;
+	if (!keywest_ctx->client->driver) {
+		i2c_unregister_device(keywest_ctx->client);
+		keywest_ctx->client = NULL;
+		return -ENODEV;
+	}
 
 	/*
 	 * Let i2c-core delete that device on driver removal.

This looks good to me. Please add the following comment before the client->driver check in both drivers:

Quoted 7 lines

/*
* We know the driver is already loaded, so the device should be
* already bound. If not it means binding failed, and then there
* is no point in keeping the device instantiated. */

Otherwise it's a little difficult to understand why the check is there.

Fair enough. I applied the patch with the comment now. Thanks!

Takashi

#8 lore raw

Hi Takashi,

Quoted 60 lines · Takashi Iwai

At Wed, 30 Sep 2009 18:55:05 +0200,

Quoted 58 lines · Jean Delvare
Quoted 36 lines · Takashi Iwai

Yes, indeed I prefer NULL check because the user can know the error at the right place. I share your concern about the code addition, though :)

I already made a patch below, but it's totally untested. It'd be helpful if someone can do review and build-test it.

thanks,

Takashi

---

sound/aoa/codecs/tas.c +4 -0
index f0ebc97..0f810c8 100644
@@ -897,6 +897,10 @@ static int tas_create(struct i2c_adapter *adapter,
 	client = i2c_new_device(adapter, &info);
 	if (!client)
 		return -ENODEV;
+	if (!client->driver) {
+		i2c_unregister_device(client);
+		return -ENODEV;
+	}
 
 	/*
 	 * Let i2c-core delete that device on driver removal.
sound/ppc/keywest.c +0 -0
index 835fa19..473c5a6 100644
@@ -59,6 +59,13 @@ static int keywest_attach_adapter(struct i2c_adapter *adapter)
 	strlcpy(info.type, "keywest", I2C_NAME_SIZE);
 	info.addr = keywest_ctx->addr;
 	keywest_ctx->client = i2c_new_device(adapter, &info);

sound/ppc/keywest.c static int keywest_attach_adapter(struct i2c_adapter *adapter) · line 71

Quoted 10 lines
sound/ppc/keywest.c +7 -0
+	if (!keywest_ctx->client)
+		return -ENODEV;
+	if (!keywest_ctx->client->driver) {
+		i2c_unregister_device(keywest_ctx->client);
+		keywest_ctx->client = NULL;
+		return -ENODEV;
+	}
 
 	/*
 	 * Let i2c-core delete that device on driver removal.

This looks good to me. Please add the following comment before the client->driver check in both drivers:

/*
* We know the driver is already loaded, so the device should be
* already bound. If not it means binding failed, and then there
* is no point in keeping the device instantiated. */

Quoted 4 lines
Quoted 1 line

Otherwise it's a little difficult to understand why the check is there.

Fair enough. I applied the patch with the comment now. Thanks!

I see this is upstream now. While the keywest fix was essentially theoretical, the tas one addresses a crash which really could happen, so I think it would be worth sending to stable for 2.6.31. What do you think? Will you take care, or do you want me to?

Thanks,

Signature · 1 line
Jean Delvare
#9 lore raw

At Sun, 4 Oct 2009 11:35:21 +0200,

Quoted 66 lines · Jean Delvare

Hi Takashi,

Quoted 62 lines · Takashi Iwai

At Wed, 30 Sep 2009 18:55:05 +0200,

Quoted 59 lines · Jean Delvare
Quoted 36 lines · Takashi Iwai

Yes, indeed I prefer NULL check because the user can know the error at the right place. I share your concern about the code addition, though :)

I already made a patch below, but it's totally untested. It'd be helpful if someone can do review and build-test it.

thanks,

Takashi

---

sound/aoa/codecs/tas.c +4 -0
index f0ebc97..0f810c8 100644
@@ -897,6 +897,10 @@ static int tas_create(struct i2c_adapter *adapter,
 	client = i2c_new_device(adapter, &info);
 	if (!client)
 		return -ENODEV;
+	if (!client->driver) {
+		i2c_unregister_device(client);
+		return -ENODEV;
+	}
 
 	/*
 	 * Let i2c-core delete that device on driver removal.
sound/ppc/keywest.c +0 -0
index 835fa19..473c5a6 100644
@@ -59,6 +59,13 @@ static int keywest_attach_adapter(struct i2c_adapter *adapter)
 	strlcpy(info.type, "keywest", I2C_NAME_SIZE);
 	info.addr = keywest_ctx->addr;
 	keywest_ctx->client = i2c_new_device(adapter, &info);

sound/ppc/keywest.c static int keywest_attach_adapter(struct i2c_adapter *adapter) · line 71

Quoted 10 lines
sound/ppc/keywest.c +7 -0
+	if (!keywest_ctx->client)
+		return -ENODEV;
+	if (!keywest_ctx->client->driver) {
+		i2c_unregister_device(keywest_ctx->client);
+		keywest_ctx->client = NULL;
+		return -ENODEV;
+	}
 
 	/*
 	 * Let i2c-core delete that device on driver removal.

This looks good to me. Please add the following comment before the client->driver check in both drivers:

/*
* We know the driver is already loaded, so the device should be
* already bound. If not it means binding failed, and then there
* is no point in keeping the device instantiated. */

Otherwise it's a little difficult to understand why the check is there.

Quoted 7 lines
Quoted 2 lines

Fair enough. I applied the patch with the comment now. Thanks!

I see this is upstream now. While the keywest fix was essentially theoretical, the tas one addresses a crash which really could happen, so I think it would be worth sending to stable for 2.6.31. What do you think? Will you take care, or do you want me to?

Agreed, it's safer to send the patch to stable tree. I'm going to submit it.

thanks,

Takashi