mbox series

[v5,0/4] user space client interface driver

Message ID 1596696063-17802-1-git-send-email-hemantk@codeaurora.org
Headers show
Series user space client interface driver | expand

Message

Hemant Kumar Aug. 6, 2020, 6:40 a.m. UTC
V5:
- Removed mhi_uci_drv structure.
- Used idr instead of creating global list of uci devices.
- Used kref instead of local ref counting for uci device and
  open count.
- Removed unlikely macro.

V4:
- Fix locking to protect proper struct members.
- Updated documentation describing uci client driver use cases.
- Fixed uci ref counting in mhi_uci_open for error case.
- Addressed style related review comments.

V3: Added documentation for MHI UCI driver.

V2: Added mutex lock to prevent multiple readers to access same
mhi buffer which can result into use after free.

Hemant Kumar (4):
  bus: mhi: core: Add helper API to return number of free TREs
  bus: mhi: core: Move MHI_MAX_MTU to external header file
  docs: Add documentation for userspace client interface
  bus: mhi: clients: Add userspace client interface driver

 Documentation/mhi/index.rst      |   1 +
 Documentation/mhi/uci.rst        |  39 +++
 drivers/bus/mhi/Kconfig          |   6 +
 drivers/bus/mhi/Makefile         |   1 +
 drivers/bus/mhi/clients/Kconfig  |  15 +
 drivers/bus/mhi/clients/Makefile |   3 +
 drivers/bus/mhi/clients/uci.c    | 662 +++++++++++++++++++++++++++++++++++++++
 drivers/bus/mhi/core/internal.h  |   1 -
 drivers/bus/mhi/core/main.c      |  12 +
 include/linux/mhi.h              |  12 +
 10 files changed, 751 insertions(+), 1 deletion(-)
 create mode 100644 Documentation/mhi/uci.rst
 create mode 100644 drivers/bus/mhi/clients/Kconfig
 create mode 100644 drivers/bus/mhi/clients/Makefile
 create mode 100644 drivers/bus/mhi/clients/uci.c

Comments

Hemant Kumar Sept. 2, 2020, 8:31 p.m. UTC | #1
Hi Jeff and Mani,
On 8/5/20 11:40 PM, Hemant Kumar wrote:
> V5:
> - Removed mhi_uci_drv structure.
> - Used idr instead of creating global list of uci devices.
> - Used kref instead of local ref counting for uci device and
>    open count.
> - Removed unlikely macro.
> 
> V4:
> - Fix locking to protect proper struct members.
> - Updated documentation describing uci client driver use cases.
> - Fixed uci ref counting in mhi_uci_open for error case.
> - Addressed style related review comments.
> 
> V3: Added documentation for MHI UCI driver.
> 
> V2: Added mutex lock to prevent multiple readers to access same
> mhi buffer which can result into use after free.
> 
> Hemant Kumar (4):
>    bus: mhi: core: Add helper API to return number of free TREs
>    bus: mhi: core: Move MHI_MAX_MTU to external header file
>    docs: Add documentation for userspace client interface
>    bus: mhi: clients: Add userspace client interface driver
> 
>   Documentation/mhi/index.rst      |   1 +
>   Documentation/mhi/uci.rst        |  39 +++
>   drivers/bus/mhi/Kconfig          |   6 +
>   drivers/bus/mhi/Makefile         |   1 +
>   drivers/bus/mhi/clients/Kconfig  |  15 +
>   drivers/bus/mhi/clients/Makefile |   3 +
>   drivers/bus/mhi/clients/uci.c    | 662 +++++++++++++++++++++++++++++++++++++++
>   drivers/bus/mhi/core/internal.h  |   1 -
>   drivers/bus/mhi/core/main.c      |  12 +
>   include/linux/mhi.h              |  12 +
>   10 files changed, 751 insertions(+), 1 deletion(-)
>   create mode 100644 Documentation/mhi/uci.rst
>   create mode 100644 drivers/bus/mhi/clients/Kconfig
>   create mode 100644 drivers/bus/mhi/clients/Makefile
>   create mode 100644 drivers/bus/mhi/clients/uci.c
> 

Could you please review my changes on V5? I tried addressing Greg's 
comment and would need help getting it reviewed from other contributors 
to MHI driver as well.

Thanks,
Hemant
Greg KH Sept. 7, 2020, 9:37 a.m. UTC | #2
On Wed, Aug 05, 2020 at 11:41:03PM -0700, Hemant Kumar wrote:
> This MHI client driver allows userspace clients to transfer
> raw data between MHI device and host using standard file operations.
> Device file node is created with format
> 
> /dev/mhi_<controller_name>_<mhi_device_name>
> 
> Currently it supports LOOPBACK channel.
> 
> Signed-off-by: Hemant Kumar <hemantk@codeaurora.org>
> ---
>  drivers/bus/mhi/Kconfig          |   6 +
>  drivers/bus/mhi/Makefile         |   1 +
>  drivers/bus/mhi/clients/Kconfig  |  15 +

Why a whole new subdirectory for just one file?

Please keep it in the same directory until you get too many different
files.

>  drivers/bus/mhi/clients/Makefile |   3 +
>  drivers/bus/mhi/clients/uci.c    | 662 +++++++++++++++++++++++++++++++++++++++
>  5 files changed, 687 insertions(+)
>  create mode 100644 drivers/bus/mhi/clients/Kconfig
>  create mode 100644 drivers/bus/mhi/clients/Makefile
>  create mode 100644 drivers/bus/mhi/clients/uci.c
> 
> diff --git a/drivers/bus/mhi/Kconfig b/drivers/bus/mhi/Kconfig
> index 6a217ff..927c392 100644
> --- a/drivers/bus/mhi/Kconfig
> +++ b/drivers/bus/mhi/Kconfig
> @@ -20,3 +20,9 @@ config MHI_BUS_DEBUG
>  	 Enable debugfs support for use with the MHI transport. Allows
>  	 reading and/or modifying some values within the MHI controller
>  	 for debug and test purposes.
> +
> +if MHI_BUS
> +
> +source "drivers/bus/mhi/clients/Kconfig"
> +
> +endif
> diff --git a/drivers/bus/mhi/Makefile b/drivers/bus/mhi/Makefile
> index 19e6443..48f6028 100644
> --- a/drivers/bus/mhi/Makefile
> +++ b/drivers/bus/mhi/Makefile
> @@ -1,2 +1,3 @@
>  # core layer
>  obj-y += core/
> +obj-y += clients/
> diff --git a/drivers/bus/mhi/clients/Kconfig b/drivers/bus/mhi/clients/Kconfig
> new file mode 100644
> index 0000000..37aaf51
> --- /dev/null
> +++ b/drivers/bus/mhi/clients/Kconfig
> @@ -0,0 +1,15 @@
> +# SPDX-License-Identifier: GPL-2.0-only
> +
> +menu "MHI clients support"
> +
> +config MHI_UCI
> +       tristate "MHI UCI"
> +       depends on MHI_BUS
> +       help
> +	 MHI based userspace client interface driver is used for transferring
> +	 raw data between host and device using standard file operations from
> +	 userspace. Open, read, write, and close operations are supported
> +	 by this driver. Please check mhi_uci_match_table for all supported
> +	 channels that are exposed to userspace.
> +

Module name information?


> +endmenu
> diff --git a/drivers/bus/mhi/clients/Makefile b/drivers/bus/mhi/clients/Makefile
> new file mode 100644
> index 0000000..cd34282
> --- /dev/null
> +++ b/drivers/bus/mhi/clients/Makefile
> @@ -0,0 +1,3 @@
> +# SPDX-License-Identifier: GPL-2.0-only
> +
> +obj-$(CONFIG_MHI_UCI) += uci.o

Why not mhi_ as a prefix?  This is a very generic module name, right?

> diff --git a/drivers/bus/mhi/clients/uci.c b/drivers/bus/mhi/clients/uci.c
> new file mode 100644
> index 0000000..a25d5d0
> --- /dev/null
> +++ b/drivers/bus/mhi/clients/uci.c
> @@ -0,0 +1,662 @@
> +// SPDX-License-Identifier: GPL-2.0-only
> +/* Copyright (c) 2018-2020, The Linux Foundation. All rights reserved.*/
> +
> +#include <linux/kernel.h>
> +#include <linux/mhi.h>
> +#include <linux/mod_devicetable.h>
> +#include <linux/module.h>
> +#include <linux/poll.h>
> +
> +#define DEVICE_NAME "mhi"
> +#define MHI_UCI_DRIVER_NAME "mhi_uci"
> +#define MAX_UCI_MINORS (128)
> +
> +static DEFINE_IDR(uci_idr);
> +static DEFINE_MUTEX(uci_idr_mutex);
> +static struct class *uci_dev_class;
> +static int uci_dev_major = -1;

Why init this to -1?

> +
> +/**
> + * struct uci_chan - MHI channel for a uci device
> + * @wq: wait queue for reader/writer
> + * @lock: spin lock
> + * @pending: list of rx buffers userspace is waiting to read
> + * @cur_buf: current buffer userspace is reading
> + * @rx_size: size of the current rx buffer userspace is reading
> + */
> +struct uci_chan {
> +	wait_queue_head_t wq;
> +
> +	/* protects pending and cur_buf members */
> +	spinlock_t lock;
> +
> +	struct list_head pending;
> +	struct uci_buf *cur_buf;
> +	size_t rx_size;
> +};
> +
> +/**
> + * struct uci_buf - uci buffer
> + * @data: data buffer
> + * @len: length of data buffer
> + * @node: list node of the uci buffer
> + */
> +struct uci_buf {
> +	void *data;
> +	size_t len;
> +	struct list_head node;
> +};
> +
> +/**
> + * struct uci_dev - MHI uci device
> + * @minor: uci device node minor number
> + * @mhi_dev: associated mhi device object
> + * @chan: MHI channel name
> + * @lock: mutex lock
> + * @ul_chan: uplink uci channel object
> + * @dl_chan: downlink uci channel object
> + * @mtu: max tx buffer length
> + * @actual_mtu: maximum size of incoming buffer
> + * @ref_count: uci_dev reference count
> + * @open_count: device node open count
> + * @enabled: uci device probed
> + */
> +struct uci_dev {
> +	unsigned int minor;
> +	struct mhi_device *mhi_dev;
> +	const char *chan;
> +
> +	/* protects uci_dev struct members */
> +	struct mutex lock;
> +
> +	struct uci_chan ul_chan;
> +	struct uci_chan dl_chan;
> +	size_t mtu;
> +	size_t actual_mtu;
> +	struct kref ref_count;
> +	struct kref open_count;

I'm stopping right here.  A structure can only have ONE reference count
to control its lifespan.  You have 2 here, which guarantees that either
you are using a kref incorrectly, or your code is totally confused and
will break easily.

Please fix this as this is not how to do this.

Also, why does anyone need to care about the number of times that open()
is called?  The vfs layer should handle all of that for you, right?

No other misc driver has to deal with this multi-layered reference
counting, so perhaps you are making this a lot more complex than it
really has to be?

thanks,

greg k-h
Hemant Kumar Sept. 11, 2020, 6:28 a.m. UTC | #3
Hi Greg,

On 9/7/20 2:37 AM, Greg KH wrote:
> On Wed, Aug 05, 2020 at 11:41:03PM -0700, Hemant Kumar wrote:
>> This MHI client driver allows userspace clients to transfer
>> raw data between MHI device and host using standard file operations.
>> Device file node is created with format
>>
>> /dev/mhi_<controller_name>_<mhi_device_name>
>>
>> Currently it supports LOOPBACK channel.
>>
>> Signed-off-by: Hemant Kumar <hemantk@codeaurora.org>
>> ---
>>   drivers/bus/mhi/Kconfig          |   6 +
>>   drivers/bus/mhi/Makefile         |   1 +
>>   drivers/bus/mhi/clients/Kconfig  |  15 +
> 
> Why a whole new subdirectory for just one file?
> 
> Please keep it in the same directory until you get too many different
> files.
Currently MHI core driver files are under bus/mhi/core. Since MHI 
clients sit on top of core layer of MHI driver, i can move uci.c 
directly under /bus/mhi/.

> 
>>   drivers/bus/mhi/clients/Makefile |   3 +
>>   drivers/bus/mhi/clients/uci.c    | 662 +++++++++++++++++++++++++++++++++++++++
>>   5 files changed, 687 insertions(+)
>>   create mode 100644 drivers/bus/mhi/clients/Kconfig
>>   create mode 100644 drivers/bus/mhi/clients/Makefile
>>   create mode 100644 drivers/bus/mhi/clients/uci.c
>>
>> diff --git a/drivers/bus/mhi/Kconfig b/drivers/bus/mhi/Kconfig
>> index 6a217ff..927c392 100644
>> --- a/drivers/bus/mhi/Kconfig
>> +++ b/drivers/bus/mhi/Kconfig
>> @@ -20,3 +20,9 @@ config MHI_BUS_DEBUG
>>   	 Enable debugfs support for use with the MHI transport. Allows
>>   	 reading and/or modifying some values within the MHI controller
>>   	 for debug and test purposes.
>> +
>> +if MHI_BUS
>> +
>> +source "drivers/bus/mhi/clients/Kconfig"
>> +
>> +endif
>> diff --git a/drivers/bus/mhi/Makefile b/drivers/bus/mhi/Makefile
>> index 19e6443..48f6028 100644
>> --- a/drivers/bus/mhi/Makefile
>> +++ b/drivers/bus/mhi/Makefile
>> @@ -1,2 +1,3 @@
>>   # core layer
>>   obj-y += core/
>> +obj-y += clients/
>> diff --git a/drivers/bus/mhi/clients/Kconfig b/drivers/bus/mhi/clients/Kconfig
>> new file mode 100644
>> index 0000000..37aaf51
>> --- /dev/null
>> +++ b/drivers/bus/mhi/clients/Kconfig
>> @@ -0,0 +1,15 @@
>> +# SPDX-License-Identifier: GPL-2.0-only
>> +
>> +menu "MHI clients support"
>> +
>> +config MHI_UCI
>> +       tristate "MHI UCI"
>> +       depends on MHI_BUS
>> +       help
>> +	 MHI based userspace client interface driver is used for transferring
>> +	 raw data between host and device using standard file operations from
>> +	 userspace. Open, read, write, and close operations are supported
>> +	 by this driver. Please check mhi_uci_match_table for all supported
>> +	 channels that are exposed to userspace.
>> +
> 
> Module name information?
Thanks for pointing it out, will add in next patch set.
> 
> 
>> +endmenu
>> diff --git a/drivers/bus/mhi/clients/Makefile b/drivers/bus/mhi/clients/Makefile
>> new file mode 100644
>> index 0000000..cd34282
>> --- /dev/null
>> +++ b/drivers/bus/mhi/clients/Makefile
>> @@ -0,0 +1,3 @@
>> +# SPDX-License-Identifier: GPL-2.0-only
>> +
>> +obj-$(CONFIG_MHI_UCI) += uci.o
> 
> Why not mhi_ as a prefix?  This is a very generic module name, right?
how about adding
mhi_uci-y		:= uci.o
obj-$(CONFIG_MHI_UCI)	+= mhi_uci.o

> 
>> diff --git a/drivers/bus/mhi/clients/uci.c b/drivers/bus/mhi/clients/uci.c
>> new file mode 100644
>> index 0000000..a25d5d0
>> --- /dev/null
>> +++ b/drivers/bus/mhi/clients/uci.c
>> @@ -0,0 +1,662 @@
>> +// SPDX-License-Identifier: GPL-2.0-only
>> +/* Copyright (c) 2018-2020, The Linux Foundation. All rights reserved.*/
>> +
>> +#include <linux/kernel.h>
>> +#include <linux/mhi.h>
>> +#include <linux/mod_devicetable.h>
>> +#include <linux/module.h>
>> +#include <linux/poll.h>
>> +
>> +#define DEVICE_NAME "mhi"
>> +#define MHI_UCI_DRIVER_NAME "mhi_uci"
>> +#define MAX_UCI_MINORS (128)
>> +
>> +static DEFINE_IDR(uci_idr);
>> +static DEFINE_MUTEX(uci_idr_mutex);
>> +static struct class *uci_dev_class;
>> +static int uci_dev_major = -1;
> 
> Why init this to -1?
Idea was to initialize with invalid value, i can leave it zero initialized.
> 
>> +
>> +/**
>> + * struct uci_chan - MHI channel for a uci device
>> + * @wq: wait queue for reader/writer
>> + * @lock: spin lock
>> + * @pending: list of rx buffers userspace is waiting to read
>> + * @cur_buf: current buffer userspace is reading
>> + * @rx_size: size of the current rx buffer userspace is reading
>> + */
>> +struct uci_chan {
>> +	wait_queue_head_t wq;
>> +
>> +	/* protects pending and cur_buf members */
>> +	spinlock_t lock;
>> +
>> +	struct list_head pending;
>> +	struct uci_buf *cur_buf;
>> +	size_t rx_size;
>> +};
>> +
>> +/**
>> + * struct uci_buf - uci buffer
>> + * @data: data buffer
>> + * @len: length of data buffer
>> + * @node: list node of the uci buffer
>> + */
>> +struct uci_buf {
>> +	void *data;
>> +	size_t len;
>> +	struct list_head node;
>> +};
>> +
>> +/**
>> + * struct uci_dev - MHI uci device
>> + * @minor: uci device node minor number
>> + * @mhi_dev: associated mhi device object
>> + * @chan: MHI channel name
>> + * @lock: mutex lock
>> + * @ul_chan: uplink uci channel object
>> + * @dl_chan: downlink uci channel object
>> + * @mtu: max tx buffer length
>> + * @actual_mtu: maximum size of incoming buffer
>> + * @ref_count: uci_dev reference count
>> + * @open_count: device node open count
>> + * @enabled: uci device probed
>> + */
>> +struct uci_dev {
>> +	unsigned int minor;
>> +	struct mhi_device *mhi_dev;
>> +	const char *chan;
>> +
>> +	/* protects uci_dev struct members */
>> +	struct mutex lock;
>> +
>> +	struct uci_chan ul_chan;
>> +	struct uci_chan dl_chan;
>> +	size_t mtu;
>> +	size_t actual_mtu;
>> +	struct kref ref_count;
>> +	struct kref open_count;
> 
> I'm stopping right here.  A structure can only have ONE reference count
> to control its lifespan.  You have 2 here, which guarantees that either
> you are using a kref incorrectly, or your code is totally confused and
> will break easily.
> 
> Please fix this as this is not how to do this.
> 
> Also, why does anyone need to care about the number of times that open()
> is called?  The vfs layer should handle all of that for you, right?
Reason for using open_count was to allow start MHI channel only when 
first open() was called and stop the MHI channel when last release() is 
called. Since uci driver just need to handle one open() from user space
other calls to open can simply return -EBUSY. i will get rid of 
open_count and does not let multiple threads to open same file node.
> 
> No other misc driver has to deal with this multi-layered reference
> counting, so perhaps you are making this a lot more complex than it
> really has to be?
> 
> thanks,
> 
> greg k-h
>
Greg KH Sept. 11, 2020, 7:47 a.m. UTC | #4
On Fri, Sep 11, 2020 at 06:28:02AM +0000, Hemant Kumar wrote:
> > > +struct uci_dev {
> > > +	unsigned int minor;
> > > +	struct mhi_device *mhi_dev;
> > > +	const char *chan;
> > > +
> > > +	/* protects uci_dev struct members */
> > > +	struct mutex lock;
> > > +
> > > +	struct uci_chan ul_chan;
> > > +	struct uci_chan dl_chan;
> > > +	size_t mtu;
> > > +	size_t actual_mtu;
> > > +	struct kref ref_count;
> > > +	struct kref open_count;
> > 
> > I'm stopping right here.  A structure can only have ONE reference count
> > to control its lifespan.  You have 2 here, which guarantees that either
> > you are using a kref incorrectly, or your code is totally confused and
> > will break easily.
> > 
> > Please fix this as this is not how to do this.
> > 
> > Also, why does anyone need to care about the number of times that open()
> > is called?  The vfs layer should handle all of that for you, right?
> Reason for using open_count was to allow start MHI channel only when first
> open() was called and stop the MHI channel when last release() is called.
> Since uci driver just need to handle one open() from user space
> other calls to open can simply return -EBUSY. i will get rid of open_count
> and does not let multiple threads to open same file node.

You will fail in trying to attempt only one open on your device node,
sorry.  You can properly trigger off of the first/last things, but
having two different reference counts is NOT how to do this, those are
to control the lifetime of a structure/object.

greg k-h