diff mbox

[17/22] USB: udc: atmel_usba_udc: no need to check return value of debugfs_create functions

Message ID 20180529153107.12791-17-gregkh@linuxfoundation.org (mailing list archive)
State New, archived
Headers show

Commit Message

Greg KH May 29, 2018, 3:31 p.m. UTC
When calling debugfs functions, there is no need to ever check the
return value.  The function can work or not, but the code logic should
never do something different based on this.

There is also no need to keep the file dentries around at all, so remove
those variables from the device structure.

Cc: Nicolas Ferre <nicolas.ferre@microchip.com>
Cc: Felipe Balbi <balbi@kernel.org>
Cc: Alexandre Belloni <alexandre.belloni@bootlin.com>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
---
 drivers/usb/gadget/udc/atmel_usba_udc.c | 71 ++++---------------------
 drivers/usb/gadget/udc/atmel_usba_udc.h |  4 --
 2 files changed, 11 insertions(+), 64 deletions(-)

Comments

Alexandre Belloni May 30, 2018, 5:31 p.m. UTC | #1
Hi,

On 29/05/2018 17:31:02+0200, Greg Kroah-Hartman wrote:
> When calling debugfs functions, there is no need to ever check the
> return value.  The function can work or not, but the code logic should
> never do something different based on this.
> 
> There is also no need to keep the file dentries around at all, so remove
> those variables from the device structure.
> 
> Cc: Nicolas Ferre <nicolas.ferre@microchip.com>
> Cc: Felipe Balbi <balbi@kernel.org>
> Cc: Alexandre Belloni <alexandre.belloni@bootlin.com>
> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
> ---
>  drivers/usb/gadget/udc/atmel_usba_udc.c | 71 ++++---------------------
>  drivers/usb/gadget/udc/atmel_usba_udc.h |  4 --
>  2 files changed, 11 insertions(+), 64 deletions(-)
> 
> diff --git a/drivers/usb/gadget/udc/atmel_usba_udc.c b/drivers/usb/gadget/udc/atmel_usba_udc.c
> index 2f586f2bda7e..a4d99bf50f2f 100644
> --- a/drivers/usb/gadget/udc/atmel_usba_udc.c
> +++ b/drivers/usb/gadget/udc/atmel_usba_udc.c
> @@ -206,94 +206,45 @@ static void usba_ep_init_debugfs(struct usba_udc *udc,
>  	struct dentry *ep_root;
>  
>  	ep_root = debugfs_create_dir(ep->ep.name, udc->debugfs_root);
> -	if (!ep_root)
> -		goto err_root;
>  	ep->debugfs_dir = ep_root;
>  
> -	ep->debugfs_queue = debugfs_create_file("queue", 0400, ep_root,
> -						ep, &queue_dbg_fops);
> -	if (!ep->debugfs_queue)
> -		goto err_queue;
> -
> -	if (ep->can_dma) {
> -		ep->debugfs_dma_status
> -			= debugfs_create_u32("dma_status", 0400, ep_root,
> -					&ep->last_dma_status);
> -		if (!ep->debugfs_dma_status)
> -			goto err_dma_status;
> -	}
> -	if (ep_is_control(ep)) {
> -		ep->debugfs_state
> -			= debugfs_create_u32("state", 0400, ep_root,
> -					&ep->state);
> -		if (!ep->debugfs_state)
> -			goto err_state;
> -	}
> -
> -	return;
> -
> -err_state:
> +	debugfs_create_file("queue", 0400, ep_root, ep, &queue_dbg_fops);

What happens here if debugfs_create_dir returned NULL? I guess the file
will be placed at the root of the debugfs filesystem which is not great.

Should we stop caring about that and assume that if debugfs_create_dir,
the following debugfs_create_* calls will fail?
Greg KH May 30, 2018, 6:06 p.m. UTC | #2
On Wed, May 30, 2018 at 07:31:13PM +0200, Alexandre Belloni wrote:
> Hi,
> 
> On 29/05/2018 17:31:02+0200, Greg Kroah-Hartman wrote:
> > When calling debugfs functions, there is no need to ever check the
> > return value.  The function can work or not, but the code logic should
> > never do something different based on this.
> > 
> > There is also no need to keep the file dentries around at all, so remove
> > those variables from the device structure.
> > 
> > Cc: Nicolas Ferre <nicolas.ferre@microchip.com>
> > Cc: Felipe Balbi <balbi@kernel.org>
> > Cc: Alexandre Belloni <alexandre.belloni@bootlin.com>
> > Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
> > ---
> >  drivers/usb/gadget/udc/atmel_usba_udc.c | 71 ++++---------------------
> >  drivers/usb/gadget/udc/atmel_usba_udc.h |  4 --
> >  2 files changed, 11 insertions(+), 64 deletions(-)
> > 
> > diff --git a/drivers/usb/gadget/udc/atmel_usba_udc.c b/drivers/usb/gadget/udc/atmel_usba_udc.c
> > index 2f586f2bda7e..a4d99bf50f2f 100644
> > --- a/drivers/usb/gadget/udc/atmel_usba_udc.c
> > +++ b/drivers/usb/gadget/udc/atmel_usba_udc.c
> > @@ -206,94 +206,45 @@ static void usba_ep_init_debugfs(struct usba_udc *udc,
> >  	struct dentry *ep_root;
> >  
> >  	ep_root = debugfs_create_dir(ep->ep.name, udc->debugfs_root);
> > -	if (!ep_root)
> > -		goto err_root;
> >  	ep->debugfs_dir = ep_root;
> >  
> > -	ep->debugfs_queue = debugfs_create_file("queue", 0400, ep_root,
> > -						ep, &queue_dbg_fops);
> > -	if (!ep->debugfs_queue)
> > -		goto err_queue;
> > -
> > -	if (ep->can_dma) {
> > -		ep->debugfs_dma_status
> > -			= debugfs_create_u32("dma_status", 0400, ep_root,
> > -					&ep->last_dma_status);
> > -		if (!ep->debugfs_dma_status)
> > -			goto err_dma_status;
> > -	}
> > -	if (ep_is_control(ep)) {
> > -		ep->debugfs_state
> > -			= debugfs_create_u32("state", 0400, ep_root,
> > -					&ep->state);
> > -		if (!ep->debugfs_state)
> > -			goto err_state;
> > -	}
> > -
> > -	return;
> > -
> > -err_state:
> > +	debugfs_create_file("queue", 0400, ep_root, ep, &queue_dbg_fops);
> 
> What happens here if debugfs_create_dir returned NULL? I guess the file
> will be placed at the root of the debugfs filesystem which is not great.

It doesn't really matter :)

The only way it can return NULL is if something really bad happens to
debugfs, and if that occurs, no one cares.  The function can only return
NULL if you pass a bad dentry into it, or if the directory is being
removed at that exact point in time.  Either of which are bigger issues
and then who cares what happens in debugfs.

No normal code should ever change its functionality based on if debugfs
is working or not.

> Should we stop caring about that and assume that if debugfs_create_dir,
> the following debugfs_create_* calls will fail?

Yes.  You should not care :)

thanks,

greg k-h
--
To unsubscribe from this list: send the line "unsubscribe linux-usb" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Alexandre Belloni May 30, 2018, 6:20 p.m. UTC | #3
On 29/05/2018 17:31:02+0200, Greg Kroah-Hartman wrote:
> When calling debugfs functions, there is no need to ever check the
> return value.  The function can work or not, but the code logic should
> never do something different based on this.
> 
> There is also no need to keep the file dentries around at all, so remove
> those variables from the device structure.
> 
> Cc: Nicolas Ferre <nicolas.ferre@microchip.com>
> Cc: Felipe Balbi <balbi@kernel.org>
> Cc: Alexandre Belloni <alexandre.belloni@bootlin.com>
> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>

Reviewed-by: Alexandre Belloni <alexandre.belloni@bootlin.com>

> ---
>  drivers/usb/gadget/udc/atmel_usba_udc.c | 71 ++++---------------------
>  drivers/usb/gadget/udc/atmel_usba_udc.h |  4 --
>  2 files changed, 11 insertions(+), 64 deletions(-)
> 
> diff --git a/drivers/usb/gadget/udc/atmel_usba_udc.c b/drivers/usb/gadget/udc/atmel_usba_udc.c
> index 2f586f2bda7e..a4d99bf50f2f 100644
> --- a/drivers/usb/gadget/udc/atmel_usba_udc.c
> +++ b/drivers/usb/gadget/udc/atmel_usba_udc.c
> @@ -206,94 +206,45 @@ static void usba_ep_init_debugfs(struct usba_udc *udc,
>  	struct dentry *ep_root;
>  
>  	ep_root = debugfs_create_dir(ep->ep.name, udc->debugfs_root);
> -	if (!ep_root)
> -		goto err_root;
>  	ep->debugfs_dir = ep_root;
>  
> -	ep->debugfs_queue = debugfs_create_file("queue", 0400, ep_root,
> -						ep, &queue_dbg_fops);
> -	if (!ep->debugfs_queue)
> -		goto err_queue;
> -
> -	if (ep->can_dma) {
> -		ep->debugfs_dma_status
> -			= debugfs_create_u32("dma_status", 0400, ep_root,
> -					&ep->last_dma_status);
> -		if (!ep->debugfs_dma_status)
> -			goto err_dma_status;
> -	}
> -	if (ep_is_control(ep)) {
> -		ep->debugfs_state
> -			= debugfs_create_u32("state", 0400, ep_root,
> -					&ep->state);
> -		if (!ep->debugfs_state)
> -			goto err_state;
> -	}
> -
> -	return;
> -
> -err_state:
> +	debugfs_create_file("queue", 0400, ep_root, ep, &queue_dbg_fops);
>  	if (ep->can_dma)
> -		debugfs_remove(ep->debugfs_dma_status);
> -err_dma_status:
> -	debugfs_remove(ep->debugfs_queue);
> -err_queue:
> -	debugfs_remove(ep_root);
> -err_root:
> -	dev_err(&ep->udc->pdev->dev,
> -		"failed to create debugfs directory for %s\n", ep->ep.name);
> +		debugfs_create_u32("dma_status", 0400, ep_root,
> +				   &ep->last_dma_status);
> +	if (ep_is_control(ep))
> +		debugfs_create_u32("state", 0400, ep_root, &ep->state);
>  }
>  
>  static void usba_ep_cleanup_debugfs(struct usba_ep *ep)
>  {
> -	debugfs_remove(ep->debugfs_queue);
> -	debugfs_remove(ep->debugfs_dma_status);
> -	debugfs_remove(ep->debugfs_state);
> -	debugfs_remove(ep->debugfs_dir);
> -	ep->debugfs_dma_status = NULL;
> -	ep->debugfs_dir = NULL;
> +	debugfs_remove_recursive(ep->debugfs_dir);
>  }
>  
>  static void usba_init_debugfs(struct usba_udc *udc)
>  {
> -	struct dentry *root, *regs;
> +	struct dentry *root;
>  	struct resource *regs_resource;
>  
>  	root = debugfs_create_dir(udc->gadget.name, NULL);
> -	if (IS_ERR(root) || !root)
> -		goto err_root;
>  	udc->debugfs_root = root;
>  
>  	regs_resource = platform_get_resource(udc->pdev, IORESOURCE_MEM,
>  				CTRL_IOMEM_ID);
>  
>  	if (regs_resource) {
> -		regs = debugfs_create_file_size("regs", 0400, root, udc,
> -						&regs_dbg_fops,
> -						resource_size(regs_resource));
> -		if (!regs)
> -			goto err_regs;
> -		udc->debugfs_regs = regs;
> +		debugfs_create_file_size("regs", 0400, root, udc,
> +					 &regs_dbg_fops,
> +					 resource_size(regs_resource));
>  	}
>  
>  	usba_ep_init_debugfs(udc, to_usba_ep(udc->gadget.ep0));
> -
> -	return;
> -
> -err_regs:
> -	debugfs_remove(root);
> -err_root:
> -	udc->debugfs_root = NULL;
> -	dev_err(&udc->pdev->dev, "debugfs is not available\n");
>  }
>  
>  static void usba_cleanup_debugfs(struct usba_udc *udc)
>  {
>  	usba_ep_cleanup_debugfs(to_usba_ep(udc->gadget.ep0));
> -	debugfs_remove(udc->debugfs_regs);
> -	debugfs_remove(udc->debugfs_root);
> -	udc->debugfs_regs = NULL;
> -	udc->debugfs_root = NULL;
> +	debugfs_remove_recursive(udc->debugfs_root);
>  }
>  #else
>  static inline void usba_ep_init_debugfs(struct usba_udc *udc,
> diff --git a/drivers/usb/gadget/udc/atmel_usba_udc.h b/drivers/usb/gadget/udc/atmel_usba_udc.h
> index d7eb7cf4fd5c..030bf797cd25 100644
> --- a/drivers/usb/gadget/udc/atmel_usba_udc.h
> +++ b/drivers/usb/gadget/udc/atmel_usba_udc.h
> @@ -287,9 +287,6 @@ struct usba_ep {
>  #ifdef CONFIG_USB_GADGET_DEBUG_FS
>  	u32					last_dma_status;
>  	struct dentry				*debugfs_dir;
> -	struct dentry				*debugfs_queue;
> -	struct dentry				*debugfs_dma_status;
> -	struct dentry				*debugfs_state;
>  #endif
>  };
>  
> @@ -344,7 +341,6 @@ struct usba_udc {
>  
>  #ifdef CONFIG_USB_GADGET_DEBUG_FS
>  	struct dentry *debugfs_root;
> -	struct dentry *debugfs_regs;
>  #endif
>  
>  	struct regmap *pmc;
> -- 
> 2.17.0
>
Greg KH May 31, 2018, 10:50 a.m. UTC | #4
On Wed, May 30, 2018 at 08:20:50PM +0200, Alexandre Belloni wrote:
> On 29/05/2018 17:31:02+0200, Greg Kroah-Hartman wrote:
> > When calling debugfs functions, there is no need to ever check the
> > return value.  The function can work or not, but the code logic should
> > never do something different based on this.
> > 
> > There is also no need to keep the file dentries around at all, so remove
> > those variables from the device structure.
> > 
> > Cc: Nicolas Ferre <nicolas.ferre@microchip.com>
> > Cc: Felipe Balbi <balbi@kernel.org>
> > Cc: Alexandre Belloni <alexandre.belloni@bootlin.com>
> > Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
> 
> Reviewed-by: Alexandre Belloni <alexandre.belloni@bootlin.com>

Thanks for reviewing this.

greg k-h
--
To unsubscribe from this list: send the line "unsubscribe linux-usb" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
diff mbox

Patch

diff --git a/drivers/usb/gadget/udc/atmel_usba_udc.c b/drivers/usb/gadget/udc/atmel_usba_udc.c
index 2f586f2bda7e..a4d99bf50f2f 100644
--- a/drivers/usb/gadget/udc/atmel_usba_udc.c
+++ b/drivers/usb/gadget/udc/atmel_usba_udc.c
@@ -206,94 +206,45 @@  static void usba_ep_init_debugfs(struct usba_udc *udc,
 	struct dentry *ep_root;
 
 	ep_root = debugfs_create_dir(ep->ep.name, udc->debugfs_root);
-	if (!ep_root)
-		goto err_root;
 	ep->debugfs_dir = ep_root;
 
-	ep->debugfs_queue = debugfs_create_file("queue", 0400, ep_root,
-						ep, &queue_dbg_fops);
-	if (!ep->debugfs_queue)
-		goto err_queue;
-
-	if (ep->can_dma) {
-		ep->debugfs_dma_status
-			= debugfs_create_u32("dma_status", 0400, ep_root,
-					&ep->last_dma_status);
-		if (!ep->debugfs_dma_status)
-			goto err_dma_status;
-	}
-	if (ep_is_control(ep)) {
-		ep->debugfs_state
-			= debugfs_create_u32("state", 0400, ep_root,
-					&ep->state);
-		if (!ep->debugfs_state)
-			goto err_state;
-	}
-
-	return;
-
-err_state:
+	debugfs_create_file("queue", 0400, ep_root, ep, &queue_dbg_fops);
 	if (ep->can_dma)
-		debugfs_remove(ep->debugfs_dma_status);
-err_dma_status:
-	debugfs_remove(ep->debugfs_queue);
-err_queue:
-	debugfs_remove(ep_root);
-err_root:
-	dev_err(&ep->udc->pdev->dev,
-		"failed to create debugfs directory for %s\n", ep->ep.name);
+		debugfs_create_u32("dma_status", 0400, ep_root,
+				   &ep->last_dma_status);
+	if (ep_is_control(ep))
+		debugfs_create_u32("state", 0400, ep_root, &ep->state);
 }
 
 static void usba_ep_cleanup_debugfs(struct usba_ep *ep)
 {
-	debugfs_remove(ep->debugfs_queue);
-	debugfs_remove(ep->debugfs_dma_status);
-	debugfs_remove(ep->debugfs_state);
-	debugfs_remove(ep->debugfs_dir);
-	ep->debugfs_dma_status = NULL;
-	ep->debugfs_dir = NULL;
+	debugfs_remove_recursive(ep->debugfs_dir);
 }
 
 static void usba_init_debugfs(struct usba_udc *udc)
 {
-	struct dentry *root, *regs;
+	struct dentry *root;
 	struct resource *regs_resource;
 
 	root = debugfs_create_dir(udc->gadget.name, NULL);
-	if (IS_ERR(root) || !root)
-		goto err_root;
 	udc->debugfs_root = root;
 
 	regs_resource = platform_get_resource(udc->pdev, IORESOURCE_MEM,
 				CTRL_IOMEM_ID);
 
 	if (regs_resource) {
-		regs = debugfs_create_file_size("regs", 0400, root, udc,
-						&regs_dbg_fops,
-						resource_size(regs_resource));
-		if (!regs)
-			goto err_regs;
-		udc->debugfs_regs = regs;
+		debugfs_create_file_size("regs", 0400, root, udc,
+					 &regs_dbg_fops,
+					 resource_size(regs_resource));
 	}
 
 	usba_ep_init_debugfs(udc, to_usba_ep(udc->gadget.ep0));
-
-	return;
-
-err_regs:
-	debugfs_remove(root);
-err_root:
-	udc->debugfs_root = NULL;
-	dev_err(&udc->pdev->dev, "debugfs is not available\n");
 }
 
 static void usba_cleanup_debugfs(struct usba_udc *udc)
 {
 	usba_ep_cleanup_debugfs(to_usba_ep(udc->gadget.ep0));
-	debugfs_remove(udc->debugfs_regs);
-	debugfs_remove(udc->debugfs_root);
-	udc->debugfs_regs = NULL;
-	udc->debugfs_root = NULL;
+	debugfs_remove_recursive(udc->debugfs_root);
 }
 #else
 static inline void usba_ep_init_debugfs(struct usba_udc *udc,
diff --git a/drivers/usb/gadget/udc/atmel_usba_udc.h b/drivers/usb/gadget/udc/atmel_usba_udc.h
index d7eb7cf4fd5c..030bf797cd25 100644
--- a/drivers/usb/gadget/udc/atmel_usba_udc.h
+++ b/drivers/usb/gadget/udc/atmel_usba_udc.h
@@ -287,9 +287,6 @@  struct usba_ep {
 #ifdef CONFIG_USB_GADGET_DEBUG_FS
 	u32					last_dma_status;
 	struct dentry				*debugfs_dir;
-	struct dentry				*debugfs_queue;
-	struct dentry				*debugfs_dma_status;
-	struct dentry				*debugfs_state;
 #endif
 };
 
@@ -344,7 +341,6 @@  struct usba_udc {
 
 #ifdef CONFIG_USB_GADGET_DEBUG_FS
 	struct dentry *debugfs_root;
-	struct dentry *debugfs_regs;
 #endif
 
 	struct regmap *pmc;