From patchwork Thu Dec 10 10:51:38 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mauro Carvalho Chehab X-Patchwork-Id: 7818211 Return-Path: X-Original-To: patchwork-linux-media@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork2.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.136]) by patchwork2.web.kernel.org (Postfix) with ESMTP id 6A7F2BEEE1 for ; Thu, 10 Dec 2015 10:51:56 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id 7E803205AA for ; Thu, 10 Dec 2015 10:51:55 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 84DA820592 for ; Thu, 10 Dec 2015 10:51:54 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751639AbbLJKvw (ORCPT ); Thu, 10 Dec 2015 05:51:52 -0500 Received: from bombadil.infradead.org ([198.137.202.9]:44295 "EHLO bombadil.infradead.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751054AbbLJKvw (ORCPT ); Thu, 10 Dec 2015 05:51:52 -0500 Received: from 201.47.149.17.dynamic.adsl.gvt.net.br ([201.47.149.17] helo=smtp.w2.samsung.com) by bombadil.infradead.org with esmtpsa (Exim 4.80.1 #2 (Red Hat Linux)) id 1a6ypD-0004Je-TE; Thu, 10 Dec 2015 10:51:52 +0000 Received: from mchehab by smtp.w2.samsung.com with local (Exim 4.86) (envelope-from ) id 1a6yp4-0003XI-5V; Thu, 10 Dec 2015 08:51:42 -0200 From: Mauro Carvalho Chehab Cc: Mauro Carvalho Chehab , Linux Media Mailing List , Mauro Carvalho Chehab Subject: [PATCH] [media] media-entity: protect object creation/removal using spin lock Date: Thu, 10 Dec 2015 08:51:38 -0200 Message-Id: <72d33f06a1e18b0448a9eb94cb58557b46cf928d.1449744671.git.mchehab@osg.samsung.com> X-Mailer: git-send-email 2.5.0 To: unlisted-recipients:; (no To-header on input) Sender: linux-media-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-media@vger.kernel.org X-Spam-Status: No, score=-6.9 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_HI, T_RP_MATCHES_RCVD, UNPARSEABLE_RELAY autolearn=unavailable version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP Some parts of the media controller are using mutexes while others are using spin locks in order to protect creation and removal of elements in the graph. That's wrong! Also, the V4L2 core can remove graph elements on non-interactive context: BUG: sleeping function called from invalid context at include/linux/sched.h:2776 Fix it by always using spin locks for graph element addition/removal, just like entity creation/removal is protected at media-device.c Signed-off-by: Mauro Carvalho Chehab --- PS.: I'm adding this patch just after "media-device: remove interfaces and interface links". It prevents some troubles with DEBUG_KMEMLEAK and KASAN, with makes them to hang the Kernel during physical device removal (or device driver removal). Without this, we get this error: kasan: GPF could be caused by NULL-ptr deref or user memory accessgeneral protection fault: 0000 [#1] SMP KASAN drivers/media/media-entity.c | 16 ++++++++-------- include/media/media-device.h | 2 +- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/drivers/media/media-entity.c b/drivers/media/media-entity.c index ee0f81364960..b1390d843909 100644 --- a/drivers/media/media-entity.c +++ b/drivers/media/media-entity.c @@ -700,9 +700,9 @@ void media_entity_remove_links(struct media_entity *entity) if (entity->graph_obj.mdev == NULL) return; - mutex_lock(&entity->graph_obj.mdev->graph_mutex); + spin_lock(&entity->graph_obj.mdev->lock); __media_entity_remove_links(entity); - mutex_unlock(&entity->graph_obj.mdev->graph_mutex); + spin_unlock(&entity->graph_obj.mdev->lock); } EXPORT_SYMBOL_GPL(media_entity_remove_links); @@ -792,9 +792,9 @@ int media_entity_setup_link(struct media_link *link, u32 flags) { int ret; - mutex_lock(&link->source->entity->graph_obj.mdev->graph_mutex); + spin_lock(&link->source->entity->graph_obj.mdev->lock); ret = __media_entity_setup_link(link, flags); - mutex_unlock(&link->source->entity->graph_obj.mdev->graph_mutex); + spin_unlock(&link->source->entity->graph_obj.mdev->lock); return ret; } @@ -932,9 +932,9 @@ EXPORT_SYMBOL_GPL(__media_remove_intf_link); void media_remove_intf_link(struct media_link *link) { - mutex_lock(&link->graph_obj.mdev->graph_mutex); + spin_lock(&link->graph_obj.mdev->lock); __media_remove_intf_link(link); - mutex_unlock(&link->graph_obj.mdev->graph_mutex); + spin_unlock(&link->graph_obj.mdev->lock); } EXPORT_SYMBOL_GPL(media_remove_intf_link); @@ -954,8 +954,8 @@ void media_remove_intf_links(struct media_interface *intf) if (intf->graph_obj.mdev == NULL) return; - mutex_lock(&intf->graph_obj.mdev->graph_mutex); + spin_lock(&intf->graph_obj.mdev->lock); __media_remove_intf_links(intf); - mutex_unlock(&intf->graph_obj.mdev->graph_mutex); + spin_unlock(&intf->graph_obj.mdev->lock); } EXPORT_SYMBOL_GPL(media_remove_intf_links); diff --git a/include/media/media-device.h b/include/media/media-device.h index 1b12774a9ab4..87ff299e1265 100644 --- a/include/media/media-device.h +++ b/include/media/media-device.h @@ -88,7 +88,7 @@ struct media_device { struct list_head pads; struct list_head links; - /* Protects the entities list */ + /* Protects the graph objects creation/removal */ spinlock_t lock; /* Serializes graph operations. */ struct mutex graph_mutex;