Skip to contentSkip to conversation

remoteproc: core: Move cdev add before device add

PATCH Applied Siddharth Gupta 4 participants 6 messages Apr 20, 2021 – Jun 23, 2021 Remote processor (remoteproc)

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 remoteproc: core: Move cdev add before device add

When cdev_add is called after device_add has been called there is no way for the userspace to know about the addition of a cdev as cdev_add itself doesn't trigger a uevent notification, or for the kernel to know about the change to devt. This results in two problems:
- mknod is never called for the cdev and hence no cdev appears on devtmpfs.
- sysfs links to the new cdev are not established.

Based on how cdev_device_add[1] is written, it appears that the correct way to use these APIs is to call cdev_add before device_add is called. Since the cdev is an optional feature for remoteproc we cannot directly use the existing API. Hence moving rproc_char_device_add() before device_add() in rproc_add().

[1]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/fs/char_dev.c#n537

---
 drivers/remoteproc/remoteproc_core.c | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)
drivers/remoteproc/remoteproc_core.c +5 -5
index 626a6b90f..562355a 100644
@@ -2316,6 +2316,11 @@ int rproc_add(struct rproc *rproc)
 	struct device *dev = &rproc->dev;
 	int ret;
 
+	/* add char device for this remoteproc */
+	ret = rproc_char_device_add(rproc);
+	if (ret < 0)
+		return ret;
+
 	ret = device_add(dev);
 	if (ret < 0)
 		return ret;
@@ -2329,11 +2334,6 @@ int rproc_add(struct rproc *rproc)
 	/* create debugfs entries */
 	rproc_create_debug_dir(rproc);
 
-	/* add char device for this remoteproc */
-	ret = rproc_char_device_add(rproc);
-	if (ret < 0)
-		return ret;
-
 	/* if rproc is marked always-on, request it to boot */
 	if (rproc->auto_boot) {
 		ret = rproc_trigger_auto_boot(rproc);
Signature · 2 lines
Qualcomm Innovation Center, Inc. is a member of the Code Aurora Forum,
a Linux Foundation Collaborative Project
maintainer
#1 lore raw

Hi Siddharth,

Quoted 9 lines · Siddharth Gupta

When cdev_add is called after device_add has been called there is no way for the userspace to know about the addition of a cdev as cdev_add itself doesn't trigger a uevent notification, or for the kernel to know about the change to devt. This results in two problems:
- mknod is never called for the cdev and hence no cdev appears on devtmpfs.
- sysfs links to the new cdev are not established.

Based on how cdev_device_add[1] is written, it appears that the correct

Please don't add this kind of reference to the change log as it will become invalid with time.

Quoted 3 lines

way to use these APIs is to call cdev_add before device_add is called. Since the cdev is an optional feature for remoteproc we cannot directly use the existing API.

Please explain why the existing API can't be used directly.

Quoted 15 lines

Hence moving rproc_char_device_add() before
device_add() in rproc_add().

[1]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/fs/char_dev.c#n537

---
 drivers/remoteproc/remoteproc_core.c | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)
drivers/remoteproc/remoteproc_core.c +0 -0
index 626a6b90f..562355a 100644

drivers/remoteproc/remoteproc_core.c int rproc_add(struct rproc *rproc) · line 2323

Quoted 8 lines
drivers/remoteproc/remoteproc_core.c +5 -0
@@ -2316,6 +2316,11 @@ int rproc_add(struct rproc *rproc)
 	struct device *dev = &rproc->dev;
 	int ret;
 
+	/* add char device for this remoteproc */
+	ret = rproc_char_device_add(rproc);
+	if (ret < 0)
+		return ret;
+

I have tested this change and it works. So how did it work before?

drivers/remoteproc/remoteproc_core.c int rproc_add(struct rproc *rproc) · line 2336

Quoted 12 lines
drivers/remoteproc/remoteproc_core.c +0 -5
 	ret = device_add(dev);
 	if (ret < 0)
 		return ret;
@@ -2329,11 +2334,6 @@ int rproc_add(struct rproc *rproc)
 	/* create debugfs entries */
 	rproc_create_debug_dir(rproc);
 
-	/* add char device for this remoteproc */
-	ret = rproc_char_device_add(rproc);
-	if (ret < 0)
-		return ret;
-

While reviewing this patch I had another look at rproc_add() and noticed it doesn't clean up after itself in case of failure. If any of the conditions aren't met the function returns but rproc_delete_debug_dir(), rproc_char_device_remove() and device_del() aren't called. Please fix that as part of your next revision.

Thanks,
Mathieu

Quoted 7 lines
drivers/remoteproc/remoteproc_core.c +0 -0
 	/* if rproc is marked always-on, request it to boot */
 	if (rproc->auto_boot) {
 		ret = rproc_trigger_auto_boot(rproc);
Signature · 2 lines
Qualcomm Innovation Center, Inc. is a member of the Code Aurora Forum,
a Linux Foundation Collaborative Project
#2 lore raw
Quoted 11 lines · Mathieu Poirier

Hi Siddharth,

Quoted 8 lines · Siddharth Gupta

When cdev_add is called after device_add has been called there is no way for the userspace to know about the addition of a cdev as cdev_add itself doesn't trigger a uevent notification, or for the kernel to know about the change to devt. This results in two problems:
- mknod is never called for the cdev and hence no cdev appears on devtmpfs.
- sysfs links to the new cdev are not established.

Quoted 3 lines
Quoted 1 line

Based on how cdev_device_add[1] is written, it appears that the correct

Please don't add this kind of reference to the change log as it will become invalid with time.

Okay sure, I will remove it.

Quoted 5 lines
Quoted 4 lines

way to use these APIs is to call cdev_add before device_add is called. Since the cdev is an optional feature for remoteproc we cannot directly use the existing API.

Please explain why the existing API can't be used directly.

Not sure what you mean here Mathieu? The reason why we can't use it is because cdev is an optional feature. We would either have to move device_add inside rproc_char_dev_add or the other way around and make cdev a regular feature. Since device_add can't be called on the same device struct twice[1], we have to do things this way. Also this way we don't have to rely on the userspace to call mknod as it will be called[2] as a part of the device_add call in devtmpfs_create_node.

Now that I think about it, is the above what you want me to put in the commit text? :)

[1]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/drivers/base/core.c#n3105
[2]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/drivers/base/devtmpfs.c#n215

Quoted 19 lines
Quoted 19 lines

Hence moving rproc_char_device_add() before
device_add() in rproc_add().

[1]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/fs/char_dev.c#n537

---
  drivers/remoteproc/remoteproc_core.c | 10 +++++-----
  1 file changed, 5 insertions(+), 5 deletions(-)
drivers/remoteproc/remoteproc_core.c +0 -0
index 626a6b90f..562355a 100644
@@ -2316,6 +2316,11 @@ int rproc_add(struct rproc *rproc)
  	struct device *dev = &rproc->dev;
  	int ret;
 
Quoted 6 lines

drivers/remoteproc/remoteproc_core.c int rproc_add(struct rproc *rproc) · line 2323

Quoted 5 lines
drivers/remoteproc/remoteproc_core.c +5 -0
+	/* add char device for this remoteproc */
+	ret = rproc_char_device_add(rproc);
+	if (ret < 0)
+		return ret;
+

I have tested this change and it works. So how did it work before?

It is a sporadic issue due to a race between the userspace uevent handler and cdev_add. If the uevent for the device is received/processed after cdev_add the cdev is created.

If "add" is written to the uevent file or mknod is manually called for devt the cdev works as expected, just that the sysfs links won't be created.

Quoted 8 lines
Quoted 8 lines
drivers/remoteproc/remoteproc_core.c +0 -0
  	ret = device_add(dev);
  	if (ret < 0)
  		return ret;
@@ -2329,11 +2334,6 @@ int rproc_add(struct rproc *rproc)
  	/* create debugfs entries */
  	rproc_create_debug_dir(rproc);
 
Quoted 10 lines

drivers/remoteproc/remoteproc_core.c int rproc_add(struct rproc *rproc) · line 2336

Quoted 5 lines
drivers/remoteproc/remoteproc_core.c +0 -5
-	/* add char device for this remoteproc */
-	ret = rproc_char_device_add(rproc);
-	if (ret < 0)
-		return ret;
-

While reviewing this patch I had another look at rproc_add() and noticed it doesn't clean up after itself in case of failure. If any of the conditions aren't met the function returns but rproc_delete_debug_dir(), rproc_char_device_remove() and device_del() aren't called. Please fix that as part of your next revision.

Sure. I'll make those changes.

Thanks,
Sid

Quoted 12 lines

Thanks,
Mathieu

Quoted 7 lines
drivers/remoteproc/remoteproc_core.c +0 -0
  	/* if rproc is marked always-on, request it to boot */
  	if (rproc->auto_boot) {
  		ret = rproc_trigger_auto_boot(rproc);
Signature · 2 lines
Qualcomm Innovation Center, Inc. is a member of the Code Aurora Forum,
a Linux Foundation Collaborative Project
#3 lore raw
Quoted 30 lines · Siddharth Gupta
Quoted 11 lines · Mathieu Poirier

Hi Siddharth,

Quoted 8 lines · Siddharth Gupta

When cdev_add is called after device_add has been called there is no way for the userspace to know about the addition of a cdev as cdev_add itself doesn't trigger a uevent notification, or for the kernel to know about the change to devt. This results in two problems:
- mknod is never called for the cdev and hence no cdev appears on devtmpfs.
- sysfs links to the new cdev are not established.

Quoted 3 lines
Quoted 1 line

Based on how cdev_device_add[1] is written, it appears that the correct

Please don't add this kind of reference to the change log as it will become invalid with time.

Okay sure, I will remove it.

Quoted 5 lines
Quoted 4 lines

way to use these APIs is to call cdev_add before device_add is called. Since the cdev is an optional feature for remoteproc we cannot directly use the existing API.

Please explain why the existing API can't be used directly.

Not sure what you mean here Mathieu? The reason why we can't use it is because cdev is an optional feature. We would either have to move device_add inside rproc_char_dev_add or the other way around and make cdev a regular feature. Since device_add can't be called on the same device struct twice[1], we have to do things this way. Also this way we don't have to rely on the userspace to call mknod as it will be called[2] as a part of the device_add call in devtmpfs_create_node.

Quoted 3 lines

Now that I think about it, is the above what you want me to put in the commit text? :)

Your patch is correct, we need to cdev_add() and in particular assign dev->devt before calling device_add(). Given how the code is split in core and cdev there's no way to use cdev_device_add(), but I don't think anyone is suggesting that - except for your commit message.

So while everything you mention in your commit message seems correct, you should be able to make it more to the point by distilling it down to something like:

The cdev needs to be added and devt assigned before device_add() is called in order for the relevant sysfs and devtmpfs entries to be created and the uevent to be properly populated.

Quoted 26 lines

[1]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/drivers/base/core.c#n3105
[2]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/drivers/base/devtmpfs.c#n215

Quoted 11 lines
Quoted 11 lines

Hence moving rproc_char_device_add() before
device_add() in rproc_add().

[1]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/fs/char_dev.c#n537

---
  drivers/remoteproc/remoteproc_core.c | 10 +++++-----
  1 file changed, 5 insertions(+), 5 deletions(-)
Quoted 13 lines

drivers/remoteproc/remoteproc_core.c int rproc_add(struct rproc *rproc) · line 2322

Quoted 12 lines
drivers/remoteproc/remoteproc_core.c +5 -0
index 626a6b90f..562355a 100644
@@ -2316,6 +2316,11 @@ int rproc_add(struct rproc *rproc)
  	struct device *dev = &rproc->dev;
  	int ret;
+	/* add char device for this remoteproc */
+	ret = rproc_char_device_add(rproc);
+	if (ret < 0)
+		return ret;
+

I have tested this change and it works. So how did it work before?

Quoted 6 lines

It is a sporadic issue due to a race between the userspace uevent handler and cdev_add. If the uevent for the device is received/processed after cdev_add the cdev is created.

If "add" is written to the uevent file or mknod is manually called for devt the cdev works as expected, just that the sysfs links won't be created.

So it works for e.g. systemd based systems (most of the time), while in a system based on devtmpfs the dev node would never show up.

Regards,
Bjorn

Quoted 33 lines
Quoted 17 lines

drivers/remoteproc/remoteproc_core.c int rproc_add(struct rproc *rproc) · line 2335

Quoted 12 lines
drivers/remoteproc/remoteproc_core.c +0 -5
  	ret = device_add(dev);
  	if (ret < 0)
  		return ret;
@@ -2329,11 +2334,6 @@ int rproc_add(struct rproc *rproc)
  	/* create debugfs entries */
  	rproc_create_debug_dir(rproc);
-	/* add char device for this remoteproc */
-	ret = rproc_char_device_add(rproc);
-	if (ret < 0)
-		return ret;
-

While reviewing this patch I had another look at rproc_add() and noticed it doesn't clean up after itself in case of failure. If any of the conditions aren't met the function returns but rproc_delete_debug_dir(), rproc_char_device_remove() and device_del() aren't called. Please fix that as part of your next revision.

Sure. I'll make those changes.

Thanks,
Sid

Quoted 12 lines

Thanks,
Mathieu

Quoted 7 lines
drivers/remoteproc/remoteproc_core.c +0 -1
  	/* if rproc is marked always-on, request it to boot */
  	if (rproc->auto_boot) {
  		ret = rproc_trigger_auto_boot(rproc);
-- 

Qualcomm Innovation Center, Inc. is a member of the Code Aurora Forum, a Linux Foundation Collaborative Project

#4 lore raw
Quoted 38 lines · Bjorn Andersson
Quoted 28 lines · Siddharth Gupta
Quoted 11 lines · Mathieu Poirier

Hi Siddharth,

Quoted 8 lines · Siddharth Gupta

When cdev_add is called after device_add has been called there is no way for the userspace to know about the addition of a cdev as cdev_add itself doesn't trigger a uevent notification, or for the kernel to know about the change to devt. This results in two problems:
- mknod is never called for the cdev and hence no cdev appears on devtmpfs.
- sysfs links to the new cdev are not established.

Quoted 3 lines
Quoted 1 line

Based on how cdev_device_add[1] is written, it appears that the correct

Please don't add this kind of reference to the change log as it will become invalid with time.

Okay sure, I will remove it.

Quoted 4 lines
Quoted 3 lines

way to use these APIs is to call cdev_add before device_add is called. Since the cdev is an optional feature for remoteproc we cannot directly use the existing API.

Please explain why the existing API can't be used directly.

Not sure what you mean here Mathieu? The reason why we can't use it is because cdev is an optional feature. We would either have to move device_add inside rproc_char_dev_add or the other way around and make cdev a regular feature. Since device_add can't be called on the same device struct twice[1], we have to do things this way. Also this way we don't have to rely on the userspace to call mknod as it will be called[2] as a part of the device_add call in devtmpfs_create_node.

Quoted 3 lines

Now that I think about it, is the above what you want me to put in the commit text? :)

Your patch is correct, we need to cdev_add() and in particular assign dev->devt before calling device_add(). Given how the code is split in core and cdev there's no way to use cdev_device_add(), but I don't think anyone is suggesting that - except for your commit message.

Quoted 7 lines

So while everything you mention in your commit message seems correct, you should be able to make it more to the point by distilling it down to something like:

The cdev needs to be added and devt assigned before device_add() is called in order for the relevant sysfs and devtmpfs entries to be created and the uevent to be properly populated.

Okay, I will make the appropriate changes. Thanks!

Quoted 25 lines
Quoted 25 lines

[1]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/drivers/base/core.c#n3105
[2]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/drivers/base/devtmpfs.c#n215

Quoted 23 lines
Quoted 15 lines

Hence moving rproc_char_device_add() before
device_add() in rproc_add().

[1]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/fs/char_dev.c#n537

---
   drivers/remoteproc/remoteproc_core.c | 10 +++++-----
   1 file changed, 5 insertions(+), 5 deletions(-)
drivers/remoteproc/remoteproc_core.c +0 -0
index 626a6b90f..562355a 100644

drivers/remoteproc/remoteproc_core.c int rproc_add(struct rproc *rproc) · line 2322

Quoted 7 lines
drivers/remoteproc/remoteproc_core.c +5 -0
@@ -2316,6 +2316,11 @@ int rproc_add(struct rproc *rproc)
   	struct device *dev = &rproc->dev;
   	int ret;
+	/* add char device for this remoteproc */
+	ret = rproc_char_device_add(rproc);
+	if (ret < 0)
+		return ret;
+

I have tested this change and it works. So how did it work before?

Quoted 8 lines
Quoted 6 lines

It is a sporadic issue due to a race between the userspace uevent handler and cdev_add. If the uevent for the device is received/processed after cdev_add the cdev is created.

If "add" is written to the uevent file or mknod is manually called for devt the cdev works as expected, just that the sysfs links won't be created.

So it works for e.g. systemd based systems (most of the time), while in a system based on devtmpfs the dev node would never show up.

Yes exactly! We were just lucky till now :)

Thanks,
Sid

Quoted 34 lines

Regards,
Bjorn

Quoted 31 lines
Quoted 8 lines
Quoted 8 lines
drivers/remoteproc/remoteproc_core.c +0 -2
   	ret = device_add(dev);
   	if (ret < 0)
   		return ret;
@@ -2329,11 +2334,6 @@ int rproc_add(struct rproc *rproc)
   	/* create debugfs entries */
   	rproc_create_debug_dir(rproc);
-	/* add char device for this remoteproc */
-	ret = rproc_char_device_add(rproc);
Quoted 8 lines

drivers/remoteproc/remoteproc_core.c int rproc_add(struct rproc *rproc) · line 2336

Quoted 3 lines
drivers/remoteproc/remoteproc_core.c +0 -3
-	if (ret < 0)
-		return ret;
-

While reviewing this patch I had another look at rproc_add() and noticed it doesn't clean up after itself in case of failure. If any of the conditions aren't met the function returns but rproc_delete_debug_dir(), rproc_char_device_remove() and device_del() aren't called. Please fix that as part of your next revision.

Sure. I'll make those changes.

Thanks,
Sid

Quoted 11 lines

Thanks,
Mathieu

Quoted 7 lines
drivers/remoteproc/remoteproc_core.c +0 -1
   	/* if rproc is marked always-on, request it to boot */
   	if (rproc->auto_boot) {
   		ret = rproc_trigger_auto_boot(rproc);
-- 

Qualcomm Innovation Center, Inc. is a member of the Code Aurora Forum, a Linux Foundation Collaborative Project

#5 lore raw
In reply to Siddharth Gupta
This patch was applied to andersson/remoteproc.git (refs/heads/for-next):

Hello:

This patch was applied to andersson/remoteproc.git (refs/heads/for-next):

Quoted 9 lines · On Tue, 20 Apr 2021 12:26:45 -0700 you wrote:

When cdev_add is called after device_add has been called there is no way for the userspace to know about the addition of a cdev as cdev_add itself doesn't trigger a uevent notification, or for the kernel to know about the change to devt. This results in two problems:
- mknod is never called for the cdev and hence no cdev appears on devtmpfs.
- sysfs links to the new cdev are not established.

[...]

Here is the summary with links:
- remoteproc: core: Move cdev add before device add

    https://git.kernel.org/andersson/remoteproc/c/c6659ee893e2

You are awesome, thank you!
--
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html