Received: by 2002:a05:6a10:22f:0:0:0:0 with SMTP id 15csp885163pxk; Thu, 17 Sep 2020 20:18:11 -0700 (PDT) X-Google-Smtp-Source: ABdhPJx54n2Dndp/jCLGNzx6nN9cAg1uW/oMjUGt+U8bINrbCU+vk3vAEC9Wiaghe6Bm9PY7fhFP X-Received: by 2002:a17:906:f8d1:: with SMTP id lh17mr32906141ejb.409.1600399091590; Thu, 17 Sep 2020 20:18:11 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1600399091; cv=none; d=google.com; s=arc-20160816; b=X+jVOTzb/j9arCV04t+Nv4u51OyK40FGTfRbKSx7t81uXKn3Ydm/XQK3AWRjWsD9M+ on3uYm0+Hq7jupXrTG5K+6b8n0wH77gTCvQOakDxnjHTfSgZwxFiPjs/GBnsWPwrlf7p CEvBIqE+zCiANwEbeqA2rp6Pj5oMMy6f/ouneEHtk/OwedBDb7wManCQqOYyZhj5NepR F5hMG7QP8WXng/aYuh0EoJ74mRVhOfVE1x9jstdH6zD4/Jg1uX29JQZCBEfEY5Lxxoqw nBsiIqNVIQ3s1KOKVWJYOjmghNUXt7h7dv3dVTRk3setg7AlMqH8bPXd+5n6CD1rVmgG 2+sw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=so7Oq8UUHx8BVs3QQ7AKrJ6XJzKJIno0tBS2vU92sxk=; b=TeQWmv6fYLqTr7vinAoMQimpNz0h0i1etFv+mMXI/kS1nIUrtbZHBOYUfzyNmDtesR j8W6o8bpKzQC5cEbxZA7ueBzEDnGDc14EYrBy1CcV5/MIclXIIzmatkVmVNGyv/ppDab j5bjJ2G2aSz+1bcZfEcyY39m6ffHPEGEwm1MN5Q6PJyrmU+ke4kRjWbxnzqcyF07tt4h QSerxtTkAi9Rl5wX5GhDZdiQCpJksKn32Ii17Wwtlf83ora6fxK+WKRnLAaphsN3tchF NBrATifnxSeu7fygg2iV9OYB324Px5E6fW/LY6X8pZY/GX5OvLpuTZw+jkjJzYSMhcxp Z8Xw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=KBZtwxDm; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id o22si1315132eje.193.2020.09.17.20.17.48; Thu, 17 Sep 2020 20:18:11 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=KBZtwxDm; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726628AbgIRDPi (ORCPT + 99 others); Thu, 17 Sep 2020 23:15:38 -0400 Received: from mail.kernel.org ([198.145.29.99]:45444 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726201AbgIRCBT (ORCPT ); Thu, 17 Sep 2020 22:01:19 -0400 Received: from sasha-vm.mshome.net (c-73-47-72-35.hsd1.nh.comcast.net [73.47.72.35]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 223C921707; Fri, 18 Sep 2020 02:01:18 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1600394479; bh=ZWXCHAIhOxLvBohGGN9gIS4oL85Z+wVf83WCgrQzrfI=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=KBZtwxDmCyiu3Dwwu7j6hBUw13b/Ao8djJ/WCUjbedcbZDdwGhXwMvH/6K4MLG4iV QjFoD0+PDKCee6Mt2c8kmvpWNfGwWiFGszINg60V0APtJIB88/EHyalk8/VSPEmYx/ Idy+pxojTMifM2y5ZQmE+c1OtTnqPPlcJaGVCSSI= From: Sasha Levin To: linux-kernel@vger.kernel.org, stable@vger.kernel.org Cc: zhengbin , Hulk Robot , Sakari Ailus , Mauro Carvalho Chehab , Sasha Levin , linux-media@vger.kernel.org Subject: [PATCH AUTOSEL 5.4 006/330] media: mc-device.c: fix memleak in media_device_register_entity Date: Thu, 17 Sep 2020 21:55:46 -0400 Message-Id: <20200918020110.2063155-6-sashal@kernel.org> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20200918020110.2063155-1-sashal@kernel.org> References: <20200918020110.2063155-1-sashal@kernel.org> MIME-Version: 1.0 X-stable: review X-Patchwork-Hint: Ignore Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: zhengbin [ Upstream commit 713f871b30a66dc4daff4d17b760c9916aaaf2e1 ] In media_device_register_entity, if media_graph_walk_init fails, need to free the previously memory. Reported-by: Hulk Robot Signed-off-by: zhengbin Signed-off-by: Sakari Ailus Signed-off-by: Mauro Carvalho Chehab Signed-off-by: Sasha Levin --- drivers/media/mc/mc-device.c | 65 ++++++++++++++++++------------------ 1 file changed, 33 insertions(+), 32 deletions(-) diff --git a/drivers/media/mc/mc-device.c b/drivers/media/mc/mc-device.c index e19df5165e78c..da80883511352 100644 --- a/drivers/media/mc/mc-device.c +++ b/drivers/media/mc/mc-device.c @@ -575,6 +575,38 @@ static void media_device_release(struct media_devnode *devnode) dev_dbg(devnode->parent, "Media device released\n"); } +static void __media_device_unregister_entity(struct media_entity *entity) +{ + struct media_device *mdev = entity->graph_obj.mdev; + struct media_link *link, *tmp; + struct media_interface *intf; + unsigned int i; + + ida_free(&mdev->entity_internal_idx, entity->internal_idx); + + /* Remove all interface links pointing to this entity */ + list_for_each_entry(intf, &mdev->interfaces, graph_obj.list) { + list_for_each_entry_safe(link, tmp, &intf->links, list) { + if (link->entity == entity) + __media_remove_intf_link(link); + } + } + + /* Remove all data links that belong to this entity */ + __media_entity_remove_links(entity); + + /* Remove all pads that belong to this entity */ + for (i = 0; i < entity->num_pads; i++) + media_gobj_destroy(&entity->pads[i].graph_obj); + + /* Remove the entity */ + media_gobj_destroy(&entity->graph_obj); + + /* invoke entity_notify callbacks to handle entity removal?? */ + + entity->graph_obj.mdev = NULL; +} + /** * media_device_register_entity - Register an entity with a media device * @mdev: The media device @@ -632,6 +664,7 @@ int __must_check media_device_register_entity(struct media_device *mdev, */ ret = media_graph_walk_init(&new, mdev); if (ret) { + __media_device_unregister_entity(entity); mutex_unlock(&mdev->graph_mutex); return ret; } @@ -644,38 +677,6 @@ int __must_check media_device_register_entity(struct media_device *mdev, } EXPORT_SYMBOL_GPL(media_device_register_entity); -static void __media_device_unregister_entity(struct media_entity *entity) -{ - struct media_device *mdev = entity->graph_obj.mdev; - struct media_link *link, *tmp; - struct media_interface *intf; - unsigned int i; - - ida_free(&mdev->entity_internal_idx, entity->internal_idx); - - /* Remove all interface links pointing to this entity */ - list_for_each_entry(intf, &mdev->interfaces, graph_obj.list) { - list_for_each_entry_safe(link, tmp, &intf->links, list) { - if (link->entity == entity) - __media_remove_intf_link(link); - } - } - - /* Remove all data links that belong to this entity */ - __media_entity_remove_links(entity); - - /* Remove all pads that belong to this entity */ - for (i = 0; i < entity->num_pads; i++) - media_gobj_destroy(&entity->pads[i].graph_obj); - - /* Remove the entity */ - media_gobj_destroy(&entity->graph_obj); - - /* invoke entity_notify callbacks to handle entity removal?? */ - - entity->graph_obj.mdev = NULL; -} - void media_device_unregister_entity(struct media_entity *entity) { struct media_device *mdev = entity->graph_obj.mdev; -- 2.25.1