Skip to contentSkip to conversation

Add iterator for an entity's data links

PATCH In review Daniel Scally 3 participants 5 messages Jul 7, 2022 – Jul 11, 2022

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

v2 cover Add iterator for an entity's data links

There are a bunch of places in the kernel where code iterates over an entity's links to perform some action. Those almost invariably have the implicit assumption that those links are data links, which might not be true following the introduction of ancillary links. Add a dedicated iterator that skips any non data links for use instead, which will allow that assumption to hold true.

Daniel Scally (2):
media: entity: Add iterator for entity data links
media: entity: Use dedicated data link iterator

 drivers/media/mc/mc-entity.c | 22 +++++++++++++++++++---
 include/media/media-entity.h | 29 +++++++++++++++++++++++++++++
 2 files changed, 48 insertions(+), 3 deletions(-)
Signature · 1 line
2.25.1
#1 lore raw

PATCH v2 1/2 media: entity: Add iterator for entity data links

Iterating over the links for an entity is a somewhat common need through the media subsystem, but generally the assumption is that they will all be data links. To meet that assumption add a new macro that iterates through an entity's links and skips non-data links.

---
Changes since v1 (suggested by Jacopo and Laurent):

- Simplified __media_entity_next_link()
- Added the link_type param to __media_entity_next_link()
- Moved __media_entity_next_link() to mc-entity.c rather than media-entity.h

 drivers/media/mc/mc-entity.c | 16 ++++++++++++++++
 include/media/media-entity.h | 29 +++++++++++++++++++++++++++++
 2 files changed, 45 insertions(+)
drivers/media/mc/mc-entity.c +16 -0
index 11f5207f73aa..a247d5679930 100644
@@ -9,6 +9,7 @@
  */
 
 #include <linux/bitmap.h>
+#include <linux/list.h>
 #include <linux/property.h>
 #include <linux/slab.h>
 #include <media/media-entity.h>
@@ -1051,3 +1052,18 @@ struct media_link *media_create_ancillary_link(struct media_entity *primary,
 	return link;
 }
 EXPORT_SYMBOL_GPL(media_create_ancillary_link);
+
+struct media_link *__media_entity_next_link(struct media_entity *entity,
+					    struct media_link *link,
+					    unsigned long link_type)
+{
+	link = link ? list_next_entry(link, list)
+		    : list_first_entry(&entity->links, typeof(*link), list);
+
+	list_for_each_entry_from(link, &entity->links, list)
+		if ((link->flags & MEDIA_LNK_FL_LINK_TYPE) == link_type)
+			return link;
+
+	return NULL;
+}
+EXPORT_SYMBOL_GPL(__media_entity_next_link);
include/media/media-entity.h +29 -0
index a9a1c0ec5d1c..903c9368c50d 100644
@@ -1140,4 +1140,33 @@ struct media_link *
 media_create_ancillary_link(struct media_entity *primary,
 			    struct media_entity *ancillary);
 
+/**
+ * __media_entity_next_link() - iterate through a &media_entity's links,
+ *
+ * @entity:	pointer to the &media_entity
+ * @link:	pointer to a &media_link to hold the iterated values
+ * @link_type:	one of the MEDIA_LNK_FL_LINK_TYPE flags
+ *
+ * Return the next link against an entity matching a specific link type. This
+ * allows iteration through an entity's links whilst guaranteeing all of the
+ * returned links are of the given type.
+ */
+struct media_link *__media_entity_next_link(struct media_entity *entity,
+					    struct media_link *link,
+					    unsigned long link_type);
+
+/**
+ * for_each_media_entity_data_link() - Iterate through an entity's data links
+ *
+ * @entity:	pointer to the &media_entity
+ * @link:	pointer to a &media_link to hold the iterated values
+ *
+ * Iterate over a &media_entity's data links
+ */
+#define for_each_media_entity_data_link(entity, link)			\
+	for (link = __media_entity_next_link(entity, NULL,		\
+					     MEDIA_LNK_FL_DATA_LINK);	\
+	     link;							\
+	     link = __media_entity_next_link(entity, link,		\
+					     MEDIA_LNK_FL_DATA_LINK))
 #endif
Signature · 1 line
2.25.1
maintainer Reviewed-by on v2 1/2
#2 lore raw

Re: [PATCH v2 1/2] media: entity: Add iterator for entity data links

Hi Daniel,

Thank you for the patch.

Quoted 54 lines · Daniel Scally

Iterating over the links for an entity is a somewhat common need through the media subsystem, but generally the assumption is that they will all be data links. To meet that assumption add a new macro that iterates through an entity's links and skips non-data links.

---
Changes since v1 (suggested by Jacopo and Laurent):

- Simplified __media_entity_next_link()
- Added the link_type param to __media_entity_next_link()
- Moved __media_entity_next_link() to mc-entity.c rather than media-entity.h

 drivers/media/mc/mc-entity.c | 16 ++++++++++++++++
 include/media/media-entity.h | 29 +++++++++++++++++++++++++++++
 2 files changed, 45 insertions(+)
drivers/media/mc/mc-entity.c +16 -0
index 11f5207f73aa..a247d5679930 100644
@@ -9,6 +9,7 @@
  */
 
 #include <linux/bitmap.h>
+#include <linux/list.h>
 #include <linux/property.h>
 #include <linux/slab.h>
 #include <media/media-entity.h>
@@ -1051,3 +1052,18 @@ struct media_link *media_create_ancillary_link(struct media_entity *primary,
 	return link;
 }
 EXPORT_SYMBOL_GPL(media_create_ancillary_link);
+
+struct media_link *__media_entity_next_link(struct media_entity *entity,
+					    struct media_link *link,
+					    unsigned long link_type)
+{
+	link = link ? list_next_entry(link, list)
+		    : list_first_entry(&entity->links, typeof(*link), list);
+
+	list_for_each_entry_from(link, &entity->links, list)
+		if ((link->flags & MEDIA_LNK_FL_LINK_TYPE) == link_type)
+			return link;
+
+	return NULL;
+}
+EXPORT_SYMBOL_GPL(__media_entity_next_link);
include/media/media-entity.h +0 -0
index a9a1c0ec5d1c..903c9368c50d 100644

include/media/media-entity.h struct media_link * · line 1144

Quoted 5 lines
include/media/media-entity.h +2 -0
@@ -1140,4 +1140,33 @@ struct media_link *
 media_create_ancillary_link(struct media_entity *primary,
 			    struct media_entity *ancillary);
 
+/**
+ * __media_entity_next_link() - iterate through a &media_entity's links,

s/iterate/Iterate/
s/,$//

Quoted 17 lines
include/media/media-entity.h +17 -0
+ *
+ * @entity:	pointer to the &media_entity
+ * @link:	pointer to a &media_link to hold the iterated values
+ * @link_type:	one of the MEDIA_LNK_FL_LINK_TYPE flags
+ *
+ * Return the next link against an entity matching a specific link type. This
+ * allows iteration through an entity's links whilst guaranteeing all of the
+ * returned links are of the given type.
+ */
+struct media_link *__media_entity_next_link(struct media_entity *entity,
+					    struct media_link *link,
+					    unsigned long link_type);
+
+/**
+ * for_each_media_entity_data_link() - Iterate through an entity's data links
+ *
+ * @entity:	pointer to the &media_entity

include/media/media-entity.h struct media_link * · line 1171

Quoted 10 lines
include/media/media-entity.h +10 -0
+ * @link:	pointer to a &media_link to hold the iterated values
+ *
+ * Iterate over a &media_entity's data links
+ */
+#define for_each_media_entity_data_link(entity, link)			\
+	for (link = __media_entity_next_link(entity, NULL,		\
+					     MEDIA_LNK_FL_DATA_LINK);	\
+	     link;							\
+	     link = __media_entity_next_link(entity, link,		\
+					     MEDIA_LNK_FL_DATA_LINK))

Missing one blank line here.

Quoted 1 line
include/media/media-entity.h +0 -0
 #endif
Signature · 3 lines
Regards,

Laurent Pinchart
maintainer
#3 lore raw

Re: [PATCH v2 1/2] media: entity: Add iterator for entity data links

Hi Laurent, Daniel,

Quoted 98 lines · Laurent Pinchart

Hi Daniel,

Thank you for the patch.

Quoted 54 lines · Daniel Scally

Iterating over the links for an entity is a somewhat common need through the media subsystem, but generally the assumption is that they will all be data links. To meet that assumption add a new macro that iterates through an entity's links and skips non-data links.

---
Changes since v1 (suggested by Jacopo and Laurent):

- Simplified __media_entity_next_link()
- Added the link_type param to __media_entity_next_link()
- Moved __media_entity_next_link() to mc-entity.c rather than media-entity.h

 drivers/media/mc/mc-entity.c | 16 ++++++++++++++++
 include/media/media-entity.h | 29 +++++++++++++++++++++++++++++
 2 files changed, 45 insertions(+)
drivers/media/mc/mc-entity.c +16 -0
index 11f5207f73aa..a247d5679930 100644
@@ -9,6 +9,7 @@
  */
 
 #include <linux/bitmap.h>
+#include <linux/list.h>
 #include <linux/property.h>
 #include <linux/slab.h>
 #include <media/media-entity.h>
@@ -1051,3 +1052,18 @@ struct media_link *media_create_ancillary_link(struct media_entity *primary,
 	return link;
 }
 EXPORT_SYMBOL_GPL(media_create_ancillary_link);
+
+struct media_link *__media_entity_next_link(struct media_entity *entity,
+					    struct media_link *link,
+					    unsigned long link_type)
+{
+	link = link ? list_next_entry(link, list)
+		    : list_first_entry(&entity->links, typeof(*link), list);
+
+	list_for_each_entry_from(link, &entity->links, list)
+		if ((link->flags & MEDIA_LNK_FL_LINK_TYPE) == link_type)
+			return link;
+
+	return NULL;
+}
+EXPORT_SYMBOL_GPL(__media_entity_next_link);
include/media/media-entity.h +0 -0
index a9a1c0ec5d1c..903c9368c50d 100644

include/media/media-entity.h struct media_link * · line 1144

Quoted 5 lines
include/media/media-entity.h +2 -0
@@ -1140,4 +1140,33 @@ struct media_link *
 media_create_ancillary_link(struct media_entity *primary,
 			    struct media_entity *ancillary);
 
+/**
+ * __media_entity_next_link() - iterate through a &media_entity's links,

s/iterate/Iterate/
s/,$//

Quoted 17 lines
include/media/media-entity.h +17 -0
+ *
+ * @entity:	pointer to the &media_entity
+ * @link:	pointer to a &media_link to hold the iterated values
+ * @link_type:	one of the MEDIA_LNK_FL_LINK_TYPE flags
+ *
+ * Return the next link against an entity matching a specific link type. This
+ * allows iteration through an entity's links whilst guaranteeing all of the
+ * returned links are of the given type.
+ */
+struct media_link *__media_entity_next_link(struct media_entity *entity,
+					    struct media_link *link,
+					    unsigned long link_type);
+
+/**
+ * for_each_media_entity_data_link() - Iterate through an entity's data links
+ *
+ * @entity:	pointer to the &media_entity

include/media/media-entity.h struct media_link * · line 1171

Quoted 10 lines
include/media/media-entity.h +10 -0
+ * @link:	pointer to a &media_link to hold the iterated values
+ *
+ * Iterate over a &media_entity's data links
+ */
+#define for_each_media_entity_data_link(entity, link)			\
+	for (link = __media_entity_next_link(entity, NULL,		\
+					     MEDIA_LNK_FL_DATA_LINK);	\
+	     link;							\
+	     link = __media_entity_next_link(entity, link,		\
+					     MEDIA_LNK_FL_DATA_LINK))

Missing one blank line here.

Quoted 1 line

Added with these addressed.

Signature · 1 line
Sakari Ailus
#4 lore raw

PATCH v2 2/2 media: entity: Use dedicated data link iterator

In reply to Daniel Scally

Where iteration over links for an entity is clearly assuming that all of those links are data links, use the new iterator to guarantee that that assumption is met.

---
 drivers/media/mc/mc-entity.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
drivers/media/mc/mc-entity.c +3 -3
index a247d5679930..cebb905260d3 100644
@@ -450,7 +450,7 @@ __must_check int __media_pipeline_start(struct media_entity *entity,
 		bitmap_zero(active, entity->num_pads);
 		bitmap_fill(has_no_links, entity->num_pads);
 
-		list_for_each_entry(link, &entity->links, list) {
+		for_each_media_entity_data_link(entity, link) {
 			struct media_pad *pad = link->sink->entity == entity
 						? link->sink : link->source;
 
@@ -889,7 +889,7 @@ media_entity_find_link(struct media_pad *source, struct media_pad *sink)
 {
 	struct media_link *link;
 
-	list_for_each_entry(link, &source->entity->links, list) {
+	for_each_media_entity_data_link(source->entity, link) {
 		if (link->source->entity == source->entity &&
 		    link->source->index == source->index &&
 		    link->sink->entity == sink->entity &&
@@ -905,7 +905,7 @@ struct media_pad *media_entity_remote_pad(const struct media_pad *pad)
 {
 	struct media_link *link;
 
-	list_for_each_entry(link, &pad->entity->links, list) {
+	for_each_media_entity_data_link(pad->entity, link) {
 		if (!(link->flags & MEDIA_LNK_FL_ENABLED))
 			continue;
 
Signature · 1 line
2.25.1