From patchwork Thu May 9 12:29:32 2013 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Sylwester Nawrocki/Kernel \\(PLT\\) /SRPOL/Staff Engineer/Samsung Electronics" X-Patchwork-Id: 2543921 Return-Path: X-Original-To: patchwork-linux-media@patchwork.kernel.org Delivered-To: patchwork-process-083081@patchwork1.kernel.org Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by patchwork1.kernel.org (Postfix) with ESMTP id 89D783FC5A for ; Thu, 9 May 2013 12:31:31 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752881Ab3EIMb3 (ORCPT ); Thu, 9 May 2013 08:31:29 -0400 Received: from mailout4.samsung.com ([203.254.224.34]:46545 "EHLO mailout4.samsung.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752557Ab3EIMb2 (ORCPT ); Thu, 9 May 2013 08:31:28 -0400 Received: from epcpsbgm1.samsung.com (epcpsbgm1 [203.254.230.26]) by mailout4.samsung.com (Oracle Communications Messaging Server 7u4-24.01(7.0.4.24.0) 64bit (built Nov 17 2011)) with ESMTP id <0MMJ00IH86S0DD90@mailout4.samsung.com> for linux-media@vger.kernel.org; Thu, 09 May 2013 21:31:27 +0900 (KST) X-AuditID: cbfee61a-b7fd56d0000022fd-29-518b971e1be5 Received: from epmmp2 ( [203.254.227.17]) by epcpsbgm1.samsung.com (EPCPMTA) with SMTP id A9.C7.08957.E179B815; Thu, 09 May 2013 21:31:26 +0900 (KST) Received: from amdc1344.digital.local ([106.116.147.32]) by mmp2.samsung.com (Oracle Communications Messaging Server 7u4-24.01 (7.0.4.24.0) 64bit (built Nov 17 2011)) with ESMTPA id <0MMJ00F6C6PDOG61@mmp2.samsung.com>; Thu, 09 May 2013 21:31:26 +0900 (KST) From: Sylwester Nawrocki To: linux-media@vger.kernel.org Cc: laurent.pinchart@ideasonboard.com, sakari.ailus@iki.fi, kyungmin.park@samsung.com, sw0312.kim@samsung.com, a.hajda@samsung.com, hj210.choi@samsung.com, Sylwester Nawrocki Subject: [RFC PATCH 1/2] media: Add function removing all media entity links Date: Thu, 09 May 2013 14:29:32 +0200 Message-id: <1368102573-16183-2-git-send-email-s.nawrocki@samsung.com> X-Mailer: git-send-email 1.7.9.5 In-reply-to: <1368102573-16183-1-git-send-email-s.nawrocki@samsung.com> References: <1368102573-16183-1-git-send-email-s.nawrocki@samsung.com> X-Brightmail-Tracker: H4sIAAAAAAAAA+NgFprCLMWRmVeSWpSXmKPExsVy+t9jQV256d2BBj3XbSxurTvHavF44zVm i7NNb9gtOicuYbfo2bCV1eLwm3ZWizP7V7JZzJj8ks2Bw2N2x0xWj8NfF7J49G1ZxejxeZNc AEsUl01Kak5mWWqRvl0CV8bVZYfYCqZLV6z+uZa1gfGJWBcjJ4eEgInExCkTmCBsMYkL99az dTFycQgJTGeUWPl0PpTTwSRxu+UEK0gVm4ChRO/RPkYQW0RAXuJJ7w2wImaB04wSB7Y8AysS FvCR+LHlETOIzSKgKtG5eD8LiM0r4CZxf9ccoHUcQOsUJOZMsgEJcwq4S0y/eA5sphBQSdvj 5cwTGHkXMDKsYhRNLUguKE5KzzXUK07MLS7NS9dLzs/dxAgOq2dSOxhXNlgcYhTgYFTi4c1I 7AoUYk0sK67MPcQowcGsJMK7uaM7UIg3JbGyKrUoP76oNCe1+BCjNAeLkjjvgVbrQCGB9MSS 1OzU1ILUIpgsEwenVAOj0LYPmud2BjNVN3MuYA9d86twlX3bw8pvy2Ys+Hp+846er4enmj59 tXudv+CjDPkJp67NfdXn5xyT/Xb9hws/lbKO9kyoNU6bzyfM9unWstY/noV8PYqtnNzJ6x/+ DMpq/+zX/V7ZUeKs0qHzN2eIrHj+lHeDyTZ/+13RXTG5k5Nj28Uq3a8bKbEUZyQaajEXFScC AMTSi1onAgAA Sender: linux-media-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-media@vger.kernel.org This function allows to remove all media entity's links to other entities, leaving no references to a media entity's links array at its remote entities. Currently when a driver of some entity is removed it will free its media entities links[] array, leaving dangling pointers at other entities that are part of same media graph. This is troublesome when drivers of a media device entities are in separate kernel modules, removing only some modules will leave others in incorrect state. This function is intended to be used when an entity is being unregistered from a media device. With an assumption that media links should be created only after they are registered to a media device and with the graph mutex held. Signed-off-by: Sylwester Nawrocki Reviewed-by: Andrzej Hajda Signed-off-by: Kyungmin Park --- drivers/media/media-entity.c | 51 ++++++++++++++++++++++++++++++++++++++++++ include/media/media-entity.h | 3 +++ 2 files changed, 54 insertions(+) diff --git a/drivers/media/media-entity.c b/drivers/media/media-entity.c index e1cd132..7c2b51c 100644 --- a/drivers/media/media-entity.c +++ b/drivers/media/media-entity.c @@ -429,6 +429,57 @@ media_entity_create_link(struct media_entity *source, u16 source_pad, } EXPORT_SYMBOL_GPL(media_entity_create_link); +void __media_entity_remove_links(struct media_entity *entity) +{ + int i, r; + + for (i = 0; i < entity->num_links; i++) { + struct media_link *link = &entity->links[i]; + struct media_entity *remote; + int num_links; + + if (link->source->entity == entity) + remote = link->sink->entity; + else + remote = link->source->entity; + + num_links = remote->num_links; + + for (r = 0; r < num_links; r++) { + struct media_link *rlink = &remote->links[r]; + + if (rlink != link->reverse) + continue; + + if (link->source->entity == entity) + remote->num_backlinks--; + + remote->num_links--; + + if (remote->num_links < 1) + break; + + /* Insert last entry in place of the dropped link. */ + remote->links[r--] = remote->links[remote->num_links]; + } + } + + entity->num_links = 0; + entity->num_backlinks = 0; +} +EXPORT_SYMBOL_GPL(__media_entity_remove_links); + +void media_entity_remove_links(struct media_entity *entity) +{ + if (WARN_ON_ONCE(entity->parent == NULL)) + return; + + mutex_lock(&entity->parent->graph_mutex); + __media_entity_remove_links(entity); + mutex_lock(&entity->parent->graph_mutex); +} +EXPORT_SYMBOL_GPL(media_entity_remove_links); + static int __media_entity_setup_link_notify(struct media_link *link, u32 flags) { int ret; diff --git a/include/media/media-entity.h b/include/media/media-entity.h index 0c16f51..0d941d2 100644 --- a/include/media/media-entity.h +++ b/include/media/media-entity.h @@ -128,6 +128,9 @@ void media_entity_cleanup(struct media_entity *entity); int media_entity_create_link(struct media_entity *source, u16 source_pad, struct media_entity *sink, u16 sink_pad, u32 flags); +void __media_entity_remove_links(struct media_entity *entity); +void media_entity_remove_links(struct media_entity *entity); + int __media_entity_setup_link(struct media_link *link, u32 flags); int media_entity_setup_link(struct media_link *link, u32 flags); struct media_link *media_entity_find_link(struct media_pad *source,