[PATCH 1/2] media: vsp1: vsp1_dl: Add usage counter

Jacopo Mondi posted 2 patches 8 months, 2 weeks ago
There is a newer version of this series
[PATCH 1/2] media: vsp1: vsp1_dl: Add usage counter
Posted by Jacopo Mondi 8 months, 2 weeks ago
In order to detect invalid usage pattern such as double list_put()
calls, add a usage counter to each display list. Increment it whenever
a list is get() and decrement it when the list is put(). Warn if the
usage counter goes below 0.

Signed-off-by: Jacopo Mondi <jacopo.mondi+renesas@ideasonboard.com>
---
 drivers/media/platform/renesas/vsp1/vsp1_dl.c | 7 +++++++
 1 file changed, 7 insertions(+)

diff --git a/drivers/media/platform/renesas/vsp1/vsp1_dl.c b/drivers/media/platform/renesas/vsp1/vsp1_dl.c
index bb8228b19824..8a3c0274a163 100644
--- a/drivers/media/platform/renesas/vsp1/vsp1_dl.c
+++ b/drivers/media/platform/renesas/vsp1/vsp1_dl.c
@@ -179,6 +179,7 @@ struct vsp1_dl_cmd_pool {
  * @has_chain: if true, indicates that there's a partition chain
  * @chain: entry in the display list partition chain
  * @flags: display list flags, a combination of VSP1_DL_FRAME_END_*
+ * @usage: usage counter to detect double list free
  */
 struct vsp1_dl_list {
 	struct list_head list;
@@ -198,6 +199,7 @@ struct vsp1_dl_list {
 	struct list_head chain;
 
 	unsigned int flags;
+	int usage;
 };
 
 /**
@@ -617,6 +619,7 @@ struct vsp1_dl_list *vsp1_dl_list_get(struct vsp1_dl_manager *dlm)
 		 * display list can assert list_empty() if it is not in a chain.
 		 */
 		INIT_LIST_HEAD(&dl->chain);
+		dl->usage++;
 	}
 
 	spin_unlock_irqrestore(&dlm->lock, flags);
@@ -657,6 +660,10 @@ static void __vsp1_dl_list_put(struct vsp1_dl_list *dl)
 	 */
 	dl->body0->num_entries = 0;
 
+	/* decrement usage count to detect invalid usage pattern. */
+	if (WARN_ON_ONCE(--dl->usage < 0))
+		dl->usage = 0;
+
 	list_add_tail(&dl->list, &dl->dlm->free);
 }
 

-- 
2.49.0
Re: [PATCH 1/2] media: vsp1: vsp1_dl: Add usage counter
Posted by Laurent Pinchart 8 months ago
Hi Jacopo,

Thank you for the patch.

On Thu, May 29, 2025 at 06:36:30PM +0200, Jacopo Mondi wrote:
> In order to detect invalid usage pattern such as double list_put()
> calls, add a usage counter to each display list. Increment it whenever
> a list is get() and decrement it when the list is put(). Warn if the
> usage counter goes below 0.
> 
> Signed-off-by: Jacopo Mondi <jacopo.mondi+renesas@ideasonboard.com>
> ---
>  drivers/media/platform/renesas/vsp1/vsp1_dl.c | 7 +++++++
>  1 file changed, 7 insertions(+)
> 
> diff --git a/drivers/media/platform/renesas/vsp1/vsp1_dl.c b/drivers/media/platform/renesas/vsp1/vsp1_dl.c
> index bb8228b19824..8a3c0274a163 100644
> --- a/drivers/media/platform/renesas/vsp1/vsp1_dl.c
> +++ b/drivers/media/platform/renesas/vsp1/vsp1_dl.c
> @@ -179,6 +179,7 @@ struct vsp1_dl_cmd_pool {
>   * @has_chain: if true, indicates that there's a partition chain
>   * @chain: entry in the display list partition chain
>   * @flags: display list flags, a combination of VSP1_DL_FRAME_END_*
> + * @usage: usage counter to detect double list free
>   */
>  struct vsp1_dl_list {
>  	struct list_head list;
> @@ -198,6 +199,7 @@ struct vsp1_dl_list {
>  	struct list_head chain;
>  
>  	unsigned int flags;
> +	int usage;

If you have no objection, I'd like to include "count" in the name. This
could be usage_count, ref_count or refcount.

>  };
>  
>  /**
> @@ -617,6 +619,7 @@ struct vsp1_dl_list *vsp1_dl_list_get(struct vsp1_dl_manager *dlm)
>  		 * display list can assert list_empty() if it is not in a chain.
>  		 */
>  		INIT_LIST_HEAD(&dl->chain);
> +		dl->usage++;

The usage count will never go above 1, as there's not real reference
counting for display lists. Better than including "count" in the field
name, we could replace

	int usage;

with

	bool allocated;

(right after has_chain to avoid increasing the structure size).

>  	}
>  
>  	spin_unlock_irqrestore(&dlm->lock, flags);
> @@ -657,6 +660,10 @@ static void __vsp1_dl_list_put(struct vsp1_dl_list *dl)
>  	 */
>  	dl->body0->num_entries = 0;
>  
> +	/* decrement usage count to detect invalid usage pattern. */

s/decrement/Decrement/

> +	if (WARN_ON_ONCE(--dl->usage < 0))
> +		dl->usage = 0;
> +
>  	list_add_tail(&dl->list, &dl->dlm->free);
>  }
>  

-- 
Regards,

Laurent Pinchart