All of lore.kernel.org
 help / color / mirror / Atom feed
From: Bjorn Andersson <bjorn.andersson@linaro.org>
To: Rishabh Bhatnagar <rishabhb@codeaurora.org>
Cc: linux-remoteproc-owner@vger.kernel.org,
	linux-kernel@vger.kernel.org, mathieu.poirier@linaro.org,
	psodagud@codeaurora.org, tsoni@codeaurora.org,
	sidgup@codeaurora.org
Subject: Re: [PATCH 1/2] remoteproc: Add userspace char device driver
Date: Tue, 24 Mar 2020 21:10:23 -0700	[thread overview]
Message-ID: <20200325041023.GE522435@yoga> (raw)
In-Reply-To: <1584747377-14824-2-git-send-email-rishabhb@codeaurora.org>

On Fri 20 Mar 16:36 PDT 2020, Rishabh Bhatnagar wrote:

> 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.
> 
> Signed-off-by: Rishabh Bhatnagar <rishabhb@codeaurora.org>

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

> ---
>  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
> 
> diff --git a/drivers/remoteproc/Makefile b/drivers/remoteproc/Makefile
> index e30a1b1..facb3fa 100644
> --- a/drivers/remoteproc/Makefile
> +++ b/drivers/remoteproc/Makefile
> @@ -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
> diff --git a/drivers/remoteproc/remoteproc_internal.h b/drivers/remoteproc/remoteproc_internal.h
> index 493ef92..bafaa12 100644
> --- a/drivers/remoteproc/remoteproc_internal.h
> +++ b/drivers/remoteproc/remoteproc_internal.h
> @@ -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.

> +
>  static inline
>  int rproc_fw_sanity_check(struct rproc *rproc, const struct firmware *fw)
>  {
> diff --git a/drivers/remoteproc/remoteproc_userspace.c b/drivers/remoteproc/remoteproc_userspace.c
> new file mode 100644
> index 0000000..e3017e7
> --- /dev/null
> +++ b/drivers/remoteproc/remoteproc_userspace.c
> @@ -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>
> +
> +#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).

> +
> +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;
> +	}
> +	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.

> +	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".

> +
> +	return retval;
> +}
> +
> +static int rproc_close(struct inode *inode, struct file *file)

s/close/release/

> +{
> +	struct rproc *rproc;
> +
> +	rproc = rproc_get_by_dev_no(iminor(inode));
> +	if (!rproc)
> +		return -EINVAL;
> +
> +	rproc_shutdown(rproc);
> +	rproc_put(rproc);
> +
> +	return 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.

> +	static int major, minor;

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

> +	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

> +		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;
> +}
> diff --git a/include/linux/remoteproc.h b/include/linux/remoteproc.h
> index 16ad666..c4ca796 100644
> --- a/include/linux/remoteproc.h
> +++ b/include/linux/remoteproc.h
> @@ -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;
>  };
>  
>  /**
> -- 
> The Qualcomm Innovation Center, Inc. is a member of the Code Aurora Forum,
> a Linux Foundation Collaborative Project

  reply	other threads:[~2020-03-25  4:10 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-03-20 23:36 [PATCH 0/2] Add character device interface to remoteproc Rishabh Bhatnagar
2020-03-20 23:36 ` [PATCH 1/2] remoteproc: Add userspace char device driver Rishabh Bhatnagar
2020-03-25  4:10   ` Bjorn Andersson [this message]
2020-03-20 23:36 ` [PATCH 2/2] remoteproc: core: Register the character device interface Rishabh Bhatnagar
2020-03-25  4:22   ` Bjorn Andersson
  -- strict thread matches above, loose matches on Subject: below --
2020-03-26 16:50 [PATCH 0/2] Add character device interface to remoteproc Rishabh Bhatnagar
2020-03-26 16:50 ` [PATCH 1/2] remoteproc: Add userspace char device driver Rishabh Bhatnagar
2020-03-26 17:37   ` Clément Leger
2020-03-28  0:09     ` rishabhb
2020-03-30 10:42       ` Clément Leger
2020-03-27  4:00   ` kbuild test robot
2020-03-27  4:00     ` kbuild test robot
2020-03-30 22:12   ` Mathieu Poirier
2020-03-30 22:45     ` Bjorn Andersson
2020-03-31 16:47       ` Mathieu Poirier
2020-03-31 17:38         ` rishabhb
2020-03-31 17:55         ` Bjorn Andersson

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20200325041023.GE522435@yoga \
    --to=bjorn.andersson@linaro.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-remoteproc-owner@vger.kernel.org \
    --cc=mathieu.poirier@linaro.org \
    --cc=psodagud@codeaurora.org \
    --cc=rishabhb@codeaurora.org \
    --cc=sidgup@codeaurora.org \
    --cc=tsoni@codeaurora.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.