Skip to contentSkip to conversation

media: entity: skip non-data link when removing reverse links

PATCH In review Yunke Cao 2 participants 4 messages Apr 12, 2022 – Apr 14, 2022

Subsystems (2)
lore
Share & tools
lore

Yunke Cao

author
#0 lore raw

PATCH 1/1 media: entity: skip non-data link when removing reverse links

The original implementation removes reverse links for any input link and assumes the presense of sink/source.
It fails when the link is a not a data link. media_entity_remove_links when there's an ancillary link can also fail.

We only need to remove reverse links for a data link.

---
 drivers/media/mc/mc-entity.c | 34 +++++++++++++++++++---------------
 1 file changed, 19 insertions(+), 15 deletions(-)
drivers/media/mc/mc-entity.c +19 -15
index 1ff60d411ea9..11f5207f73aa 100644
@@ -597,26 +597,30 @@ static void __media_entity_remove_link(struct media_entity *entity,
 	struct media_link *rlink, *tmp;
 	struct media_entity *remote;
 
-	if (link->source->entity == entity)
-		remote = link->sink->entity;
-	else
-		remote = link->source->entity;
+	/* Remove the reverse links for a data link. */
+	if ((link->flags & MEDIA_LNK_FL_LINK_TYPE) == MEDIA_LNK_FL_DATA_LINK) {
+		if (link->source->entity == entity)
+			remote = link->sink->entity;
+		else
+			remote = link->source->entity;
 
-	list_for_each_entry_safe(rlink, tmp, &remote->links, list) {
-		if (rlink != link->reverse)
-			continue;
+		list_for_each_entry_safe(rlink, tmp, &remote->links, list) {
+			if (rlink != link->reverse)
+				continue;
 
-		if (link->source->entity == entity)
-			remote->num_backlinks--;
+			if (link->source->entity == entity)
+				remote->num_backlinks--;
 
-		/* Remove the remote link */
-		list_del(&rlink->list);
-		media_gobj_destroy(&rlink->graph_obj);
-		kfree(rlink);
+			/* Remove the remote link */
+			list_del(&rlink->list);
+			media_gobj_destroy(&rlink->graph_obj);
+			kfree(rlink);
 
-		if (--remote->num_links == 0)
-			break;
+			if (--remote->num_links == 0)
+				break;
+		}
 	}
+
 	list_del(&link->list);
 	media_gobj_destroy(&link->graph_obj);
 	kfree(link);
Signature · 1 line
2.35.1.1178.g4f1659d476-goog
maintainer
#1 lore raw

Hi Yunke,

Thanks for the patch.

Quoted 4 lines · Yunke Cao

The original implementation removes reverse links for any input link and assumes the presense of sink/source.
It fails when the link is a not a data link. media_entity_remove_links when there's an ancillary link can also fail.

The function's return type is void. Are there other adverse effects from this? Looking at the function, it would seem like that the reverse link simply isn't found in this case, and so not removed.

Quoted 2 lines

We only need to remove reverse links for a data link.

Ideally this would not be based on the link flags as it's not a very robust way to test whather a backlink needs to be removed.

Quoted 56 lines
---
 drivers/media/mc/mc-entity.c | 34 +++++++++++++++++++---------------
 1 file changed, 19 insertions(+), 15 deletions(-)
drivers/media/mc/mc-entity.c +19 -15
index 1ff60d411ea9..11f5207f73aa 100644
@@ -597,26 +597,30 @@ static void __media_entity_remove_link(struct media_entity *entity,
 	struct media_link *rlink, *tmp;
 	struct media_entity *remote;
 
-	if (link->source->entity == entity)
-		remote = link->sink->entity;
-	else
-		remote = link->source->entity;
+	/* Remove the reverse links for a data link. */
+	if ((link->flags & MEDIA_LNK_FL_LINK_TYPE) == MEDIA_LNK_FL_DATA_LINK) {
+		if (link->source->entity == entity)
+			remote = link->sink->entity;
+		else
+			remote = link->source->entity;
 
-	list_for_each_entry_safe(rlink, tmp, &remote->links, list) {
-		if (rlink != link->reverse)
-			continue;
+		list_for_each_entry_safe(rlink, tmp, &remote->links, list) {
+			if (rlink != link->reverse)
+				continue;
 
-		if (link->source->entity == entity)
-			remote->num_backlinks--;
+			if (link->source->entity == entity)
+				remote->num_backlinks--;
 
-		/* Remove the remote link */
-		list_del(&rlink->list);
-		media_gobj_destroy(&rlink->graph_obj);
-		kfree(rlink);
+			/* Remove the remote link */
+			list_del(&rlink->list);
+			media_gobj_destroy(&rlink->graph_obj);
+			kfree(rlink);
 
-		if (--remote->num_links == 0)
-			break;
+			if (--remote->num_links == 0)
+				break;
+		}
 	}
+
 	list_del(&link->list);
 	media_gobj_destroy(&link->graph_obj);
 	kfree(link);
Signature · 3 lines
Kind regards,

Sakari Ailus
author
#2 lore raw

Hi Sakari!

Thanks for the review. See my reply inline.

Quoted 6 lines · Sakari Ailus

Hi Yunke,

Thanks for the patch.

On Tue, Apr 12, 2022 at 03:23:13PM +0900, Yunke Cao wrote:

Quoted 9 lines
Quoted 4 lines

The original implementation removes reverse links for any input link and assumes the presense of sink/source.
It fails when the link is a not a data link. media_entity_remove_links when there's an ancillary link can also fail.

The function's return type is void. Are there other adverse effects from this? Looking at the function, it would seem like that the reverse link simply isn't found in this case, and so not removed.

The function dereferences without any check link->source and link->sink ("link->source->entity == entity" etc.), which is in union. Ancillary links populate gobj0/gobj1 instead of source/sink. Calling this function on ancillary links can cause crashes.

Quoted 6 lines
Quoted 2 lines

We only need to remove reverse links for a data link.

Ideally this would not be based on the link flags as it's not a very robust way to test whather a backlink needs to be removed.

I was mainly trying to make sure link->source and link->sink are populated by checking the link type.
Currently, only data links need to run this part of the code to remove reverse links so I feel this is
the easiest way. Let me know if there's any better alternative.

Quoted 60 lines
Quoted 46 lines
---
 drivers/media/mc/mc-entity.c | 34 +++++++++++++++++++---------------
 1 file changed, 19 insertions(+), 15 deletions(-)
drivers/media/mc/mc-entity.c +15 -13
index 1ff60d411ea9..11f5207f73aa 100644
@@ -597,26 +597,30 @@ static void __media_entity_remove_link(struct media_entity *entity,
      struct media_link *rlink, *tmp;
      struct media_entity *remote;
 
-     if (link->source->entity == entity)
-             remote = link->sink->entity;
-     else
-             remote = link->source->entity;
+     /* Remove the reverse links for a data link. */
+     if ((link->flags & MEDIA_LNK_FL_LINK_TYPE) == MEDIA_LNK_FL_DATA_LINK) {
+             if (link->source->entity == entity)
+                     remote = link->sink->entity;
+             else
+                     remote = link->source->entity;
 
-     list_for_each_entry_safe(rlink, tmp, &remote->links, list) {
-             if (rlink != link->reverse)
-                     continue;
+             list_for_each_entry_safe(rlink, tmp, &remote->links, list) {
+                     if (rlink != link->reverse)
+                             continue;
 
-             if (link->source->entity == entity)
-                     remote->num_backlinks--;
+                     if (link->source->entity == entity)
+                             remote->num_backlinks--;
 
-             /* Remove the remote link */
-             list_del(&rlink->list);
-             media_gobj_destroy(&rlink->graph_obj);
-             kfree(rlink);
+                     /* Remove the remote link */
+                     list_del(&rlink->list);
+                     media_gobj_destroy(&rlink->graph_obj);
+                     kfree(rlink);
 

drivers/media/mc/mc-entity.c static void __media_entity_remove_link(struct media_entity *entity, · line 626

Quoted 10 lines
drivers/media/mc/mc-entity.c +4 -2
-             if (--remote->num_links == 0)
-                     break;
+                     if (--remote->num_links == 0)
+                             break;
+             }
      }
+
      list_del(&link->list);
      media_gobj_destroy(&link->graph_obj);
      kfree(link);

--
Kind regards,

Quoted 1 line

Sakari Ailus

Thanks!
Yunke

maintainer
#3 lore raw

Hi Yunke,

Quoted 13 lines · Yunke Cao
Quoted 11 lines · Sakari Ailus

Hi Yunke,

Thanks for the patch.

Quoted 4 lines · Yunke Cao

The original implementation removes reverse links for any input link and assumes the presense of sink/source.
It fails when the link is a not a data link. media_entity_remove_links when there's an ancillary link can also fail.

Quoted 8 lines
Quoted 4 lines

The function's return type is void. Are there other adverse effects from this? Looking at the function, it would seem like that the reverse link simply isn't found in this case, and so not removed.

The function dereferences without any check link->source and link->sink ("link->source->entity == entity" etc.), which is in union. Ancillary links populate gobj0/gobj1 instead of source/sink. Calling this function on ancillary links can cause crashes.

Indeed. The struct definition isn't too pretty either. And this only works with interface links as they're not included in the links list...

Quoted 11 lines
Quoted 6 lines
Quoted 2 lines

We only need to remove reverse links for a data link.

Ideally this would not be based on the link flags as it's not a very robust way to test whather a backlink needs to be removed.

I was mainly trying to make sure link->source and link->sink are populated by checking the link type.
Currently, only data links need to run this part of the code to remove reverse links so I feel this is
the easiest way. Let me know if there's any better alternative.

I think we should get this in now. The code seems to be in a dire need for cleanup still.

Signature · 3 lines
Regards,

Sakari Ailus