Skip to contentSkip to conversation

Add character device interface to remoteproc

PATCH In review Rishabh Bhatnagar 2 participants 5 messages Mar 20, 2020 – Mar 25, 2020 Remote processor (remoteproc)

lore
Share & tools
lore
#0 lore raw

cover Add character device interface to remoteproc

This patch series adds a character device interface to remoteproc framework. This interface can be used by userspace clients in order to boot and shutdown the remote processor.
Currently there is only a sysfs interface which the userspace clients can use. If a usersapce application crashes after booting the remote processor through the sysfs interface the remote processor does not get any indication about the crash. It might still assume that the application is running.
For example modem uses remotefs service to data from disk/flash memory. If the remotefs service crashes, modem still keeps on requesting data which might lead to crash on modem. Even if the service is restarted the file handles modem requested previously would become stale. Adding a character device interface makes the remote processor tightly coupled with the user space application. A crash of the application leads to a close on the file descriptors therefore shutting down the remoteproc.

Rishabh Bhatnagar (2):
remoteproc: Add userspace char device driver
remoteproc: core: Register the character device interface

 drivers/remoteproc/Makefile               |   1 +
 drivers/remoteproc/remoteproc_core.c      |   8 +-
 drivers/remoteproc/remoteproc_internal.h  |   3 +
 drivers/remoteproc/remoteproc_userspace.c | 126 ++++++++++++++++++++++++++++++
 include/linux/remoteproc.h                |   2 +
 5 files changed, 139 insertions(+), 1 deletion(-)
 create mode 100644 drivers/remoteproc/remoteproc_userspace.c
Signature · 2 lines
The Qualcomm Innovation Center, Inc. is a member of the Code Aurora Forum,
a Linux Foundation Collaborative Project
#1 lore raw

PATCH 1/2 remoteproc: Add userspace char device driver

Add the driver for creating the character device interface for userspace applications. The character device interface can be used in order to boot up and shutdown the remote processor. This might be helpful for remote processors that are booted by userspace applications and need to shutdown when the application crahes/shutsdown.

---
 drivers/remoteproc/Makefile               |   1 +
 drivers/remoteproc/remoteproc_internal.h  |   3 +
 drivers/remoteproc/remoteproc_userspace.c | 126 ++++++++++++++++++++++++++++++
 include/linux/remoteproc.h                |   2 +
 4 files changed, 132 insertions(+)
 create mode 100644 drivers/remoteproc/remoteproc_userspace.c
drivers/remoteproc/Makefile +1 -0
index e30a1b1..facb3fa 100644
@@ -7,6 +7,7 @@ obj-$(CONFIG_REMOTEPROC)		+= remoteproc.o
 remoteproc-y				:= remoteproc_core.o
 remoteproc-y				+= remoteproc_debugfs.o
 remoteproc-y				+= remoteproc_sysfs.o
+remoteproc-y				+= remoteproc_userspace.o
 remoteproc-y				+= remoteproc_virtio.o
 remoteproc-y				+= remoteproc_elf_loader.o
 obj-$(CONFIG_IMX_REMOTEPROC)		+= imx_rproc.o
drivers/remoteproc/remoteproc_internal.h +3 -0
index 493ef92..bafaa12 100644
@@ -63,6 +63,9 @@ struct resource_table *rproc_elf_find_loaded_rsc_table(struct rproc *rproc,
 struct rproc_mem_entry *
 rproc_find_carveout_by_name(struct rproc *rproc, const char *name, ...);
 
+/* from remoteproc_userspace.c */
+extern int rproc_char_device_add(struct rproc *rproc);
+
 static inline
 int rproc_fw_sanity_check(struct rproc *rproc, const struct firmware *fw)
 {
drivers/remoteproc/remoteproc_userspace.c new file +126 -0

126 lines not on this page · raw message

include/linux/remoteproc.h +2 -0
index 16ad666..c4ca796 100644
@@ -37,6 +37,7 @@
 
 #include <linux/types.h>
 #include <linux/mutex.h>
+#include <linux/cdev.h>
 #include <linux/virtio.h>
 #include <linux/completion.h>
 #include <linux/idr.h>
@@ -514,6 +515,7 @@ struct rproc {
 	bool auto_boot;
 	struct list_head dump_segments;
 	int nb_vdev;
+	struct cdev char_dev;
 };
 
 /**
Signature · 2 lines
The Qualcomm Innovation Center, Inc. is a member of the Code Aurora Forum,
a Linux Foundation Collaborative Project
#2 lore raw

Re: [PATCH 1/2] remoteproc: Add userspace char device driver

Quoted 8 lines · Rishabh Bhatnagar

Add the driver for creating the character device interface for userspace applications. The character device interface can be used in order to boot up and shutdown the remote processor. This might be helpful for remote processors that are booted by userspace applications and need to shutdown when the application crahes/shutsdown.

Please use linux-remoteproc@ instead of linux-remoteproc-owner@, to ensure you're reaching the whole community.

Quoted 25 lines
---
 drivers/remoteproc/Makefile               |   1 +
 drivers/remoteproc/remoteproc_internal.h  |   3 +
 drivers/remoteproc/remoteproc_userspace.c | 126 ++++++++++++++++++++++++++++++
 include/linux/remoteproc.h                |   2 +
 4 files changed, 132 insertions(+)
 create mode 100644 drivers/remoteproc/remoteproc_userspace.c
drivers/remoteproc/Makefile +1 -0
index e30a1b1..facb3fa 100644
@@ -7,6 +7,7 @@ obj-$(CONFIG_REMOTEPROC)		+= remoteproc.o
 remoteproc-y				:= remoteproc_core.o
 remoteproc-y				+= remoteproc_debugfs.o
 remoteproc-y				+= remoteproc_sysfs.o
+remoteproc-y				+= remoteproc_userspace.o
 remoteproc-y				+= remoteproc_virtio.o
 remoteproc-y				+= remoteproc_elf_loader.o
 obj-$(CONFIG_IMX_REMOTEPROC)		+= imx_rproc.o
drivers/remoteproc/remoteproc_internal.h +0 -0
index 493ef92..bafaa12 100644

drivers/remoteproc/remoteproc_internal.h struct resource_table *rproc_elf_find_loaded_rsc_table(struct rproc *rproc, · line 67

Quoted 5 lines
drivers/remoteproc/remoteproc_internal.h +2 -0
@@ -63,6 +63,9 @@ struct resource_table *rproc_elf_find_loaded_rsc_table(struct rproc *rproc,
 struct rproc_mem_entry *
 rproc_find_carveout_by_name(struct rproc *rproc, const char *name, ...);
 
+/* from remoteproc_userspace.c */
+extern int rproc_char_device_add(struct rproc *rproc);

Please omit "external" from this.

Quoted 22 lines
drivers/remoteproc/remoteproc_internal.h +1 -0
+
 static inline
 int rproc_fw_sanity_check(struct rproc *rproc, const struct firmware *fw)
 {
drivers/remoteproc/remoteproc_userspace.c new file +12 -0
new file mode 100644
index 0000000..e3017e7
@@ -0,0 +1,126 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * Character device interface driver for Remoteproc framework.
+ *
+ * Copyright (c) 2020, The Linux Foundation. All rights reserved.
+ */
+
+#include <linux/module.h>
+#include <linux/fs.h>
+#include <linux/cdev.h>
+#include <linux/mutex.h>
+#include <linux/remoteproc.h>

drivers/remoteproc/remoteproc_userspace.c line 22

Quoted 10 lines
drivers/remoteproc/remoteproc_userspace.c new file +10 -0
+
+#include "remoteproc_internal.h"
+
+static LIST_HEAD(rproc_chrdev_list);
+
+struct rproc_char_dev {
+	struct list_head node;
+	dev_t dev_no;
+	struct rproc *rproc;
+};

Per below suggestions you don't need this struct (or list).

Quoted 12 lines
drivers/remoteproc/remoteproc_userspace.c new file +12 -0
+
+static DEFINE_MUTEX(rproc_chrdev_lock);
+
+static struct rproc *rproc_get_by_dev_no(int minor)
+{
+	struct rproc_char_dev *r;
+
+	mutex_lock(&rproc_chrdev_lock);
+	list_for_each_entry(r, &rproc_chrdev_list, node) {
+		if (MINOR(r->dev_no) == minor)
+			break;
+	}

drivers/remoteproc/remoteproc_userspace.c line 44

Quoted 10 lines
drivers/remoteproc/remoteproc_userspace.c new file +10 -0
+	mutex_unlock(&rproc_chrdev_lock);
+
+	return r->rproc;
+}
+
+static int rproc_open(struct inode *inode, struct file *file)
+{
+	struct rproc *rproc;
+	int retval;
+

rproc can be found with:
container_of(inode->i_cdev, struct rproc, char_dev);

so you don't need rproc_get_by_dev_no() and hence not rproc_chrdev_list.

drivers/remoteproc/remoteproc_userspace.c line 55

Quoted 11 lines
drivers/remoteproc/remoteproc_userspace.c new file +11 -0
+	rproc = rproc_get_by_dev_no(iminor(inode));
+	if (!rproc)
+		return -EINVAL;
+
+	if (!try_module_get(rproc->dev.parent->driver->owner)) {
+		dev_err(&rproc->dev, "can't get owner\n");
+		return -EINVAL;
+	}
+
+	get_device(&rproc->dev);
+	retval = rproc_boot(rproc);

return rproc_boot(); and drop "retval".

drivers/remoteproc/remoteproc_userspace.c line 60

Quoted 5 lines
drivers/remoteproc/remoteproc_userspace.c new file +5 -0
+
+	return retval;
+}
+
+static int rproc_close(struct inode *inode, struct file *file)

s/close/release/

Quoted 11 lines
drivers/remoteproc/remoteproc_userspace.c new file +11 -0
+{
+	struct rproc *rproc;
+
+	rproc = rproc_get_by_dev_no(iminor(inode));
+	if (!rproc)
+		return -EINVAL;
+
+	rproc_shutdown(rproc);
+	rproc_put(rproc);
+
+	return 0;

drivers/remoteproc/remoteproc_userspace.c line 81

Quoted 10 lines
drivers/remoteproc/remoteproc_userspace.c new file +10 -0
+}
+
+static const struct file_operations rproc_fops = {
+	.open = rproc_open,
+	.release = rproc_close,
+};
+
+int rproc_char_device_add(struct rproc *rproc)
+{
+	int ret = 0;

No need to initialize ret, its first use is an assignment.

drivers/remoteproc/remoteproc_userspace.c line 82

Quoted 1 line
drivers/remoteproc/remoteproc_userspace.c new file +1 -0
+	static int major, minor;

Move these to the top of the file, and use an ida for allocating the minor.

drivers/remoteproc/remoteproc_userspace.c line 88

Quoted 6 lines
drivers/remoteproc/remoteproc_userspace.c new file +6 -0
+	dev_t dev_no;
+	struct rproc_char_dev *chrdev;
+
+	mutex_lock(&rproc_chrdev_lock);
+	if (!major) {
+		ret = alloc_chrdev_region(&dev_no, 0, 4, "subsys");

Please do this during remoteproc_init()

Regards,
Bjorn

Quoted 61 lines
drivers/remoteproc/remoteproc_userspace.c new file +38 -0
+		if (ret < 0) {
+			pr_err("Failed to alloc subsys_dev region, err %d\n",
+									ret);
+			goto fail;
+		}
+		major = MAJOR(dev_no);
+		minor = MINOR(dev_no);
+	} else
+		dev_no = MKDEV(major, minor);
+
+	cdev_init(&rproc->char_dev, &rproc_fops);
+	rproc->char_dev.owner = THIS_MODULE;
+	ret = cdev_add(&rproc->char_dev, dev_no, 1);
+	if (ret < 0)
+		goto fail_unregister_cdev_region;
+
+	rproc->dev.devt = dev_no;
+
+	chrdev = kzalloc(sizeof(struct rproc_char_dev), GFP_KERNEL);
+	if (!chrdev) {
+		ret = -ENOMEM;
+		goto fail_unregister_cdev_region;
+	}
+
+	chrdev->rproc = rproc;
+	chrdev->dev_no = dev_no;
+	list_add(&chrdev->node, &rproc_chrdev_list);
+	++minor;
+	mutex_unlock(&rproc_chrdev_lock);
+
+	return 0;
+
+fail_unregister_cdev_region:
+	unregister_chrdev_region(dev_no, 1);
+fail:
+	mutex_unlock(&rproc_chrdev_lock);
+	return ret;
+}
include/linux/remoteproc.h +2 -0
index 16ad666..c4ca796 100644
@@ -37,6 +37,7 @@
 
 #include <linux/types.h>
 #include <linux/mutex.h>
+#include <linux/cdev.h>
 #include <linux/virtio.h>
 #include <linux/completion.h>
 #include <linux/idr.h>
@@ -514,6 +515,7 @@ struct rproc {
 	bool auto_boot;
 	struct list_head dump_segments;
 	int nb_vdev;
+	struct cdev char_dev;
 };
 
 /**
Signature · 2 lines
The Qualcomm Innovation Center, Inc. is a member of the Code Aurora Forum,
a Linux Foundation Collaborative Project
#3 lore raw

PATCH 2/2 remoteproc: core: Register the character device interface

In reply to Rishabh Bhatnagar

Add the character device during rproc_add. This would create a character device node at /dev/subsys_<rproc_name>. Userspace applications can interact with the remote processor using this interface rather than using sysfs node. To distinguish between different remote processor nodes the device name has been changed to include the rproc name appended to "subsys_" string.

---
 drivers/remoteproc/remoteproc_core.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)
drivers/remoteproc/remoteproc_core.c +7 -1
index 097f33e..48a3932 100644
@@ -1907,6 +1907,12 @@ int rproc_add(struct rproc *rproc)
 	struct device *dev = &rproc->dev;
 	int ret;
 
+	ret = rproc_char_device_add(rproc);
+	if (ret) {
+		pr_err("error while adding character device\n");
+		return ret;
+	}
+
 	ret = device_add(dev);
 	if (ret < 0)
 		return ret;
@@ -2044,7 +2050,7 @@ struct rproc *rproc_alloc(struct device *dev, const char *name,
 		return NULL;
 	}
 
-	dev_set_name(&rproc->dev, "remoteproc%d", rproc->index);
+	dev_set_name(&rproc->dev, "subsys_%s", rproc->name);
 
 	atomic_set(&rproc->power, 0);
 
Signature · 2 lines
The Qualcomm Innovation Center, Inc. is a member of the Code Aurora Forum,
a Linux Foundation Collaborative Project
#4 lore raw

Re: [PATCH 2/2] remoteproc: core: Register the character device interface

Quoted 30 lines · Rishabh Bhatnagar

Add the character device during rproc_add. This would create a character device node at /dev/subsys_<rproc_name>. Userspace applications can interact with the remote processor using this interface rather than using sysfs node. To distinguish between different remote processor nodes the device name has been changed to include the rproc name appended to "subsys_" string.

---
 drivers/remoteproc/remoteproc_core.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)
drivers/remoteproc/remoteproc_core.c +6 -0
index 097f33e..48a3932 100644
@@ -1907,6 +1907,12 @@ int rproc_add(struct rproc *rproc)
 	struct device *dev = &rproc->dev;
 	int ret;
 
+	ret = rproc_char_device_add(rproc);
+	if (ret) {
+		pr_err("error while adding character device\n");
+		return ret;
+	}
+
 	ret = device_add(dev);
 	if (ret < 0)
 		return ret;

drivers/remoteproc/remoteproc_core.c struct rproc *rproc_alloc(struct device *dev, const char *name, · line 2053

Quoted 5 lines
drivers/remoteproc/remoteproc_core.c +1 -1
@@ -2044,7 +2050,7 @@ struct rproc *rproc_alloc(struct device *dev, const char *name,
 		return NULL;
 	}
 
-	dev_set_name(&rproc->dev, "remoteproc%d", rproc->index);
+	dev_set_name(&rproc->dev, "subsys_%s", rproc->name);

Afaict there's no guarantee that rproc->name is unique and I wonder if this will break any userspace expectations. Please repost this to remoteproc@ so we can get further input on this.

And if we can do this, I would like it to be "rproc-%s".

Regards,
Bjorn

Quoted 6 lines
drivers/remoteproc/remoteproc_core.c +0 -1
 	atomic_set(&rproc->power, 0);
 
-- 

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