All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] media: ipu3: don't use recursion at the Kernel
@ 2021-12-02 11:44 Mauro Carvalho Chehab
  2021-12-02 12:31 ` Daniel Scally
  2021-12-02 12:50 ` Sakari Ailus
  0 siblings, 2 replies; 3+ messages in thread
From: Mauro Carvalho Chehab @ 2021-12-02 11:44 UTC (permalink / raw)
  To: Sakari Ailus
  Cc: linuxarm, mauro.chehab, Mauro Carvalho Chehab, Bingbu Cao,
	Dan Scally, Mauro Carvalho Chehab, Tianshu Qiu, Yong Zhi,
	linux-kernel, linux-media

The Kernel stack is too small. Doing recursions there is a very
bad idea, as, if something gets wrong, it could lead to data
corruption. So, re-implement cio2_check_fwnode_graph() to avoid
recursion.

Compile-tested only.

Signed-off-by: Mauro Carvalho Chehab <mchehab+huawei@kernel.org>
---
 drivers/media/pci/intel/ipu3/ipu3-cio2-main.c | 17 ++++++++---------
 1 file changed, 8 insertions(+), 9 deletions(-)

diff --git a/drivers/media/pci/intel/ipu3/ipu3-cio2-main.c b/drivers/media/pci/intel/ipu3/ipu3-cio2-main.c
index 356ea966cf8d..8e4f250a8b56 100644
--- a/drivers/media/pci/intel/ipu3/ipu3-cio2-main.c
+++ b/drivers/media/pci/intel/ipu3/ipu3-cio2-main.c
@@ -1691,16 +1691,15 @@ static int cio2_check_fwnode_graph(struct fwnode_handle *fwnode)
 {
 	struct fwnode_handle *endpoint;
 
-	if (IS_ERR_OR_NULL(fwnode))
-		return -EINVAL;
-
-	endpoint = fwnode_graph_get_next_endpoint(fwnode, NULL);
-	if (endpoint) {
-		fwnode_handle_put(endpoint);
-		return 0;
+	while (!IS_ERR_OR_NULL(fwnode)) {
+		endpoint = fwnode_graph_get_next_endpoint(fwnode, NULL);
+		if (endpoint) {
+			fwnode_handle_put(endpoint);
+			return 0;
+		}
+		fwnode = fwnode->secondary;
 	}
-
-	return cio2_check_fwnode_graph(fwnode->secondary);
+	return -EINVAL;
 }
 
 /**************** PCI interface ****************/
-- 
2.33.1


^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH] media: ipu3: don't use recursion at the Kernel
  2021-12-02 11:44 [PATCH] media: ipu3: don't use recursion at the Kernel Mauro Carvalho Chehab
@ 2021-12-02 12:31 ` Daniel Scally
  2021-12-02 12:50 ` Sakari Ailus
  1 sibling, 0 replies; 3+ messages in thread
From: Daniel Scally @ 2021-12-02 12:31 UTC (permalink / raw)
  To: Mauro Carvalho Chehab, Sakari Ailus
  Cc: linuxarm, mauro.chehab, Bingbu Cao, Mauro Carvalho Chehab,
	Tianshu Qiu, Yong Zhi, linux-kernel, linux-media

Hi Mauro

On 02/12/2021 11:44, Mauro Carvalho Chehab wrote:
> The Kernel stack is too small. Doing recursions there is a very
> bad idea, as, if something gets wrong, it could lead to data
> corruption.


TIL - I'll bear that in mind in the future, thanks.

> So, re-implement cio2_check_fwnode_graph() to avoid
> recursion.
>
> Compile-tested only.
>
> Signed-off-by: Mauro Carvalho Chehab <mchehab+huawei@kernel.org>


FWIW:

Reviewed-by: Daniel Scally <djrscally@gmail.com>

Tested-by: Daniel Scally <djrscally@gmail.com>

> ---
>  drivers/media/pci/intel/ipu3/ipu3-cio2-main.c | 17 ++++++++---------
>  1 file changed, 8 insertions(+), 9 deletions(-)
>
> diff --git a/drivers/media/pci/intel/ipu3/ipu3-cio2-main.c b/drivers/media/pci/intel/ipu3/ipu3-cio2-main.c
> index 356ea966cf8d..8e4f250a8b56 100644
> --- a/drivers/media/pci/intel/ipu3/ipu3-cio2-main.c
> +++ b/drivers/media/pci/intel/ipu3/ipu3-cio2-main.c
> @@ -1691,16 +1691,15 @@ static int cio2_check_fwnode_graph(struct fwnode_handle *fwnode)
>  {
>  	struct fwnode_handle *endpoint;
>  
> -	if (IS_ERR_OR_NULL(fwnode))
> -		return -EINVAL;
> -
> -	endpoint = fwnode_graph_get_next_endpoint(fwnode, NULL);
> -	if (endpoint) {
> -		fwnode_handle_put(endpoint);
> -		return 0;
> +	while (!IS_ERR_OR_NULL(fwnode)) {
> +		endpoint = fwnode_graph_get_next_endpoint(fwnode, NULL);
> +		if (endpoint) {
> +			fwnode_handle_put(endpoint);
> +			return 0;
> +		}
> +		fwnode = fwnode->secondary;
>  	}
> -
> -	return cio2_check_fwnode_graph(fwnode->secondary);
> +	return -EINVAL;
>  }
>  
>  /**************** PCI interface ****************/

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] media: ipu3: don't use recursion at the Kernel
  2021-12-02 11:44 [PATCH] media: ipu3: don't use recursion at the Kernel Mauro Carvalho Chehab
  2021-12-02 12:31 ` Daniel Scally
@ 2021-12-02 12:50 ` Sakari Ailus
  1 sibling, 0 replies; 3+ messages in thread
From: Sakari Ailus @ 2021-12-02 12:50 UTC (permalink / raw)
  To: Mauro Carvalho Chehab
  Cc: linuxarm, mauro.chehab, Bingbu Cao, Dan Scally,
	Mauro Carvalho Chehab, Tianshu Qiu, Yong Zhi, linux-kernel,
	linux-media

Hi Mauro,

On Thu, Dec 02, 2021 at 12:44:26PM +0100, Mauro Carvalho Chehab wrote:
> The Kernel stack is too small. Doing recursions there is a very
> bad idea, as, if something gets wrong, it could lead to data
> corruption. So, re-implement cio2_check_fwnode_graph() to avoid
> recursion.

Any decent compiler should be able to optimise out tail recursion.

But is checking the secondary pointer even needed these days?
fwnode_graph_get_next_endpoint() does that already, it's not something
drivers should be required to do.

-- 
Kind regards,

Sakari Ailus

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2021-12-02 12:51 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-12-02 11:44 [PATCH] media: ipu3: don't use recursion at the Kernel Mauro Carvalho Chehab
2021-12-02 12:31 ` Daniel Scally
2021-12-02 12:50 ` Sakari Ailus

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.