From patchwork Tue Mar 12 10:33:57 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sakari Ailus X-Patchwork-Id: 13589795 Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 8C4A835F18 for ; Tue, 12 Mar 2024 10:34:29 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=192.198.163.17 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1710239671; cv=none; b=gm+J0IYoeQ/SqxuZEKWMGa8TJ1MNrJEvOZoj+jynzuTeiLnG51SHQdyblGrAWoN/haagqD8cvEc1Y+5Iqt85eb3//Dt6+hrvCdkpmzRe0jXIIOgmVo/HhBjdqYRkvxgaqnhbUud4yW8wb8+Oeu94BDqMmrvj5cEGO575aZHMkUc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1710239671; c=relaxed/simple; bh=wt5TpGLUjA2yafUJ8E8ZxwHU8bMfsj7s2Wy4iVnixqM=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=Q6kAyCu0tutRam/nuFa9Q1pdWiQ5RBCMPPT0w8KRYTocApyRh6wCVIIxwi7tAFbH8WegCGH2S96MFDxXWR248zK687KAUjNJTXdr2pvtjuGrN3joZTFW7/VDE7UQ2Y7HrOmljyEQiP0d3dFA3tO2ol+i0Fe8e1cD7pGf6W3hUCQ= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.intel.com; spf=none smtp.mailfrom=linux.intel.com; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b=hX3pizxb; arc=none smtp.client-ip=192.198.163.17 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.intel.com Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=linux.intel.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="hX3pizxb" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1710239670; x=1741775670; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=wt5TpGLUjA2yafUJ8E8ZxwHU8bMfsj7s2Wy4iVnixqM=; b=hX3pizxbznMvvqGYcVGj09J24H6/6V1C07sXz78sHTIZi2+P3tE61+1O h+AUp1IyQ0gcF+vlkXqmLedfk/4HAh9ix2K+Loqp5lxYPu8FXxfFLMOR1 8OEqbq0KVdtCGh1pu4rhHb5XIEYoRzV4EfTprxgpLvM2UxjfR0VY+hZyT Z3E2KtEUANlqSqI8A5jerpnHihnmm++M3f8kGFofizQuLK485aDkq2KmS 2jzg4AlklZn3RD87KQtrlOB2CXOiTZBfcsSveWfNwieUdTSpHoUJ1A9g2 UoXRndGUFHEqfWpBNqkaJd97HNZp4RAJeyaafUe0Ifhrey18La/xEck0H w==; X-IronPort-AV: E=McAfee;i="6600,9927,11010"; a="4794255" X-IronPort-AV: E=Sophos;i="6.07,119,1708416000"; d="scan'208";a="4794255" Received: from orviesa005.jf.intel.com ([10.64.159.145]) by fmvoesa111.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 12 Mar 2024 03:34:28 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.07,119,1708416000"; d="scan'208";a="16194055" Received: from turnipsi.fi.intel.com (HELO kekkonen.fi.intel.com) ([10.237.72.44]) by orviesa005-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 12 Mar 2024 03:34:27 -0700 Received: from svinhufvud.ger.corp.intel.com (localhost [IPv6:::1]) by kekkonen.fi.intel.com (Postfix) with ESMTP id 0592B11F81D; Tue, 12 Mar 2024 12:34:23 +0200 (EET) From: Sakari Ailus To: linux-media@vger.kernel.org Cc: laurent.pinchart@ideasonboard.com, hverkuil@xs4all.nl Subject: [PATCH v3 01/26] Revert "[media] media: fix media devnode ioctl/syscall and unregister race" Date: Tue, 12 Mar 2024 12:33:57 +0200 Message-Id: <20240312103422.216484-2-sakari.ailus@linux.intel.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20240312103422.216484-1-sakari.ailus@linux.intel.com> References: <20240312103422.216484-1-sakari.ailus@linux.intel.com> Precedence: bulk X-Mailing-List: linux-media@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 This reverts commit 6f0dd24a084a ("[media] media: fix media devnode ioctl/syscall and unregister race"). The commit was part of an original patchset to avoid crashes when an unregistering device is in use. Signed-off-by: Sakari Ailus Acked-by: Hans Verkuil --- drivers/media/mc/mc-device.c | 15 +++++++-------- drivers/media/mc/mc-devnode.c | 8 +------- include/media/media-devnode.h | 16 ++-------------- 3 files changed, 10 insertions(+), 29 deletions(-) diff --git a/drivers/media/mc/mc-device.c b/drivers/media/mc/mc-device.c index c0dd4ae57227..6c569ecd4b3d 100644 --- a/drivers/media/mc/mc-device.c +++ b/drivers/media/mc/mc-device.c @@ -741,7 +741,6 @@ int __must_check __media_device_register(struct media_device *mdev, if (ret < 0) { /* devnode free is handled in media_devnode_*() */ mdev->devnode = NULL; - media_devnode_unregister_prepare(devnode); media_devnode_unregister(devnode); return ret; } @@ -797,9 +796,6 @@ void media_device_unregister(struct media_device *mdev) return; } - /* Clear the devnode register bit to avoid races with media dev open */ - media_devnode_unregister_prepare(mdev->devnode); - /* Remove all entities from the media device */ list_for_each_entry_safe(entity, next, &mdev->entities, graph_obj.list) __media_device_unregister_entity(entity); @@ -824,10 +820,13 @@ void media_device_unregister(struct media_device *mdev) dev_dbg(mdev->dev, "Media device unregistered\n"); - device_remove_file(&mdev->devnode->dev, &dev_attr_model); - media_devnode_unregister(mdev->devnode); - /* devnode free is handled in media_devnode_*() */ - mdev->devnode = NULL; + /* Check if mdev devnode was registered */ + if (media_devnode_is_registered(mdev->devnode)) { + device_remove_file(&mdev->devnode->dev, &dev_attr_model); + media_devnode_unregister(mdev->devnode); + /* devnode free is handled in media_devnode_*() */ + mdev->devnode = NULL; + } } EXPORT_SYMBOL_GPL(media_device_unregister); diff --git a/drivers/media/mc/mc-devnode.c b/drivers/media/mc/mc-devnode.c index 7f67825c8757..302328f51417 100644 --- a/drivers/media/mc/mc-devnode.c +++ b/drivers/media/mc/mc-devnode.c @@ -266,7 +266,7 @@ int __must_check media_devnode_register(struct media_device *mdev, return ret; } -void media_devnode_unregister_prepare(struct media_devnode *devnode) +void media_devnode_unregister(struct media_devnode *devnode) { /* Check if devnode was ever registered at all */ if (!media_devnode_is_registered(devnode)) @@ -274,12 +274,6 @@ void media_devnode_unregister_prepare(struct media_devnode *devnode) mutex_lock(&media_devnode_lock); clear_bit(MEDIA_FLAG_REGISTERED, &devnode->flags); - mutex_unlock(&media_devnode_lock); -} - -void media_devnode_unregister(struct media_devnode *devnode) -{ - mutex_lock(&media_devnode_lock); /* Delete the cdev on this minor as well */ cdev_device_del(&devnode->cdev, &devnode->dev); devnode->media_dev = NULL; diff --git a/include/media/media-devnode.h b/include/media/media-devnode.h index d27c1c646c28..46f0d3ae44d1 100644 --- a/include/media/media-devnode.h +++ b/include/media/media-devnode.h @@ -115,19 +115,6 @@ int __must_check media_devnode_register(struct media_device *mdev, struct media_devnode *devnode, struct module *owner); -/** - * media_devnode_unregister_prepare - clear the media device node register bit - * @devnode: the device node to prepare for unregister - * - * This clears the passed device register bit. Future open calls will be met - * with errors. Should be called before media_devnode_unregister() to avoid - * races with unregister and device file open calls. - * - * This function can safely be called if the device node has never been - * registered or has already been unregistered. - */ -void media_devnode_unregister_prepare(struct media_devnode *devnode); - /** * media_devnode_unregister - unregister a media device node * @devnode: the device node to unregister @@ -135,7 +122,8 @@ void media_devnode_unregister_prepare(struct media_devnode *devnode); * This unregisters the passed device. Future open calls will be met with * errors. * - * Should be called after media_devnode_unregister_prepare() + * This function can safely be called if the device node has never been + * registered or has already been unregistered. */ void media_devnode_unregister(struct media_devnode *devnode); From patchwork Tue Mar 12 10:33:58 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sakari Ailus X-Patchwork-Id: 13589796 Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 416434176D for ; Tue, 12 Mar 2024 10:34:30 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=192.198.163.17 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1710239671; cv=none; b=oiUqD23kAmCTSq/EJlkA4hT96dqKlPB9xBtC4n+IkuVS3J3oKtoHv8y5XcVxV+Qx7FIh1XsMe1ffBIzuV3bfQFNpVHN+Xj6IbbYfXIGidnC+c6tSjDrxBla+PzpLSRZ1VL/7Hrc8tVyHpjeQ0ihfo1xH0SmYjKNuua18oMbnbus= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1710239671; c=relaxed/simple; bh=hGMHWbk+42dOTjRmgjMTR3uqtbd8CE5teAWCzPI0P7M=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=hNrR/iGi3aYfv4VeAMvuo5PYr7RMjDr+YbVLypf0JCAj06/6hCFaPJVPrKfirfV0ZlWB/IVxM+QohebIMzmkr7WeDIScrWVD+KpmFNvZiTzBO8ikTiERfU8Sro02ZQtCUmgLWaSlN0gZOcaRAwMPdzmoukQHkeJC5w3b3SrnfqY= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.intel.com; spf=none smtp.mailfrom=linux.intel.com; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b=h4d4LaBq; arc=none smtp.client-ip=192.198.163.17 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.intel.com Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=linux.intel.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="h4d4LaBq" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1710239670; x=1741775670; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=hGMHWbk+42dOTjRmgjMTR3uqtbd8CE5teAWCzPI0P7M=; b=h4d4LaBqfRMTYbqRJL7C2Jhv6MMJyHjRViVgp3AJJbH0Ze39jJx0/S92 WJEAE4hf8rSecVA0szw32d2w+2Kl1SPfT+w7zyqw+RLJWMewSPUrQipz/ oxt6OwN0z6PCo+pmq/XK/W/UxeVytOtqpkMI7rRJCarI50+VfQ9HC/wk8 HjgeL+8C5SIw/Ih1pn7RwDWgoHUwf4U2q9cnRJH4HlSssZpvEhYkZheKF ppretG1KFDK+YFJAs0ecXBJeu0xS7ANDF8VYLcyHed67vpumj9EL7pK6K xfQC4jU9g8gch0zSRu+SIMXD/ZSwdhk4SzUvswm5BB/hocVUQ9XycVHyg A==; X-IronPort-AV: E=McAfee;i="6600,9927,11010"; a="4794258" X-IronPort-AV: E=Sophos;i="6.07,119,1708416000"; d="scan'208";a="4794258" Received: from orviesa005.jf.intel.com ([10.64.159.145]) by fmvoesa111.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 12 Mar 2024 03:34:29 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.07,119,1708416000"; d="scan'208";a="16194058" Received: from turnipsi.fi.intel.com (HELO kekkonen.fi.intel.com) ([10.237.72.44]) by orviesa005-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 12 Mar 2024 03:34:28 -0700 Received: from svinhufvud.ger.corp.intel.com (localhost [IPv6:::1]) by kekkonen.fi.intel.com (Postfix) with ESMTP id EAB7011FB11; Tue, 12 Mar 2024 12:34:24 +0200 (EET) From: Sakari Ailus To: linux-media@vger.kernel.org Cc: laurent.pinchart@ideasonboard.com, hverkuil@xs4all.nl Subject: [PATCH v3 02/26] Revert "media: utilize new cdev_device_add helper function" Date: Tue, 12 Mar 2024 12:33:58 +0200 Message-Id: <20240312103422.216484-3-sakari.ailus@linux.intel.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20240312103422.216484-1-sakari.ailus@linux.intel.com> References: <20240312103422.216484-1-sakari.ailus@linux.intel.com> Precedence: bulk X-Mailing-List: linux-media@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 This reverts commit 857313e51006ff51524579bcd8808b70f9a80812. This patch is temporarily reverted for internal rework. Signed-off-by: Sakari Ailus Acked-by: Hans Verkuil --- drivers/media/cec/core/cec-core.c | 16 ++++++++++++---- drivers/media/mc/mc-devnode.c | 21 ++++++++++++++++----- 2 files changed, 28 insertions(+), 9 deletions(-) diff --git a/drivers/media/cec/core/cec-core.c b/drivers/media/cec/core/cec-core.c index 5a54db839e5d..3e0c0dcff358 100644 --- a/drivers/media/cec/core/cec-core.c +++ b/drivers/media/cec/core/cec-core.c @@ -137,19 +137,26 @@ static int __must_check cec_devnode_register(struct cec_devnode *devnode, /* Part 2: Initialize and register the character device */ cdev_init(&devnode->cdev, &cec_devnode_fops); + devnode->cdev.kobj.parent = &devnode->dev.kobj; devnode->cdev.owner = owner; kobject_set_name(&devnode->cdev.kobj, "cec%d", devnode->minor); devnode->registered = true; - ret = cdev_device_add(&devnode->cdev, &devnode->dev); - if (ret) { + ret = cdev_add(&devnode->cdev, devnode->dev.devt, 1); + if (ret < 0) { + pr_err("%s: cdev_add failed\n", __func__); devnode->registered = false; - pr_err("%s: cdev_device_add failed\n", __func__); goto clr_bit; } + ret = device_add(&devnode->dev); + if (ret) + goto cdev_del; + return 0; +cdev_del: + cdev_del(&devnode->cdev); clr_bit: mutex_lock(&cec_devnode_lock); clear_bit(devnode->minor, cec_devnode_nums); @@ -195,7 +202,8 @@ static void cec_devnode_unregister(struct cec_adapter *adap) cec_adap_enable(adap); mutex_unlock(&adap->lock); - cdev_device_del(&devnode->cdev, &devnode->dev); + device_del(&devnode->dev); + cdev_del(&devnode->cdev); put_device(&devnode->dev); } diff --git a/drivers/media/mc/mc-devnode.c b/drivers/media/mc/mc-devnode.c index 302328f51417..7827e9519382 100644 --- a/drivers/media/mc/mc-devnode.c +++ b/drivers/media/mc/mc-devnode.c @@ -239,23 +239,33 @@ int __must_check media_devnode_register(struct media_device *mdev, dev_set_name(&devnode->dev, "media%d", devnode->minor); device_initialize(&devnode->dev); - /* Part 2: Initialize the character device */ + /* Part 2: Initialize and register the character device */ cdev_init(&devnode->cdev, &media_devnode_fops); devnode->cdev.owner = owner; + devnode->cdev.kobj.parent = &devnode->dev.kobj; kobject_set_name(&devnode->cdev.kobj, "media%d", devnode->minor); - /* Part 3: Add the media and char device */ - ret = cdev_device_add(&devnode->cdev, &devnode->dev); + ret = cdev_add(&devnode->cdev, MKDEV(MAJOR(media_dev_t), + devnode->minor), 1); if (ret < 0) { - pr_err("%s: cdev_device_add failed\n", __func__); + pr_err("%s: cdev_add failed\n", __func__); goto cdev_add_error; } + /* Part 3: Add the media device */ + ret = device_add(&devnode->dev); + if (ret < 0) { + pr_err("%s: device_add failed\n", __func__); + goto device_add_error; + } + /* Part 4: Activate this minor. The char device can now be used. */ set_bit(MEDIA_FLAG_REGISTERED, &devnode->flags); return 0; +device_add_error: + cdev_del(&devnode->cdev); cdev_add_error: mutex_lock(&media_devnode_lock); clear_bit(devnode->minor, media_devnode_nums); @@ -275,9 +285,10 @@ void media_devnode_unregister(struct media_devnode *devnode) mutex_lock(&media_devnode_lock); clear_bit(MEDIA_FLAG_REGISTERED, &devnode->flags); /* Delete the cdev on this minor as well */ - cdev_device_del(&devnode->cdev, &devnode->dev); + cdev_del(&devnode->cdev); devnode->media_dev = NULL; mutex_unlock(&media_devnode_lock); + device_del(&devnode->dev); put_device(&devnode->dev); } From patchwork Tue Mar 12 10:33:59 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sakari Ailus X-Patchwork-Id: 13589797 Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id CD8B754FA1 for ; Tue, 12 Mar 2024 10:34:30 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=192.198.163.17 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1710239672; cv=none; b=Q3wPaUzVOYVeILq8KIfdVTKYiq5il7uZ2XU5A8HnnsOGytLJ7jfiDptWGwlKU7V7pr4HrtjSf3qcBO8Bw/QxW0++GaV/Q7Ug3rdxwz/vBnFGbZzBy/xSsQPFnEh+iZDPUCL5tKCD3O1DaS754wB3YR0SvWtDQNXz8lNDND1Y/ro= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1710239672; c=relaxed/simple; bh=o/F1a3xbdz1UyMohzLghpOgV2cZ3kudDlGMs5iGC+IA=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=N2FF4FXuYW5+G9L8BaQTdT0rV0uEM9Pxg40nMO11bwsG/VI7UPZQrdOC1Kt1zjR02uKXl0owUi/iHXcHdgPfzYctZRfDOFEul+JZ94ZV23wDVYwcQswTVSx2ydyv3FP+TpsJnoFwPgjmUDmLOXatWpiFaN0QPt/qiBFPHER8dr8= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.intel.com; spf=none smtp.mailfrom=linux.intel.com; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b=diV7p3y+; arc=none smtp.client-ip=192.198.163.17 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.intel.com Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=linux.intel.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="diV7p3y+" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1710239671; x=1741775671; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=o/F1a3xbdz1UyMohzLghpOgV2cZ3kudDlGMs5iGC+IA=; b=diV7p3y+pxDp7WPqiPJOXOwDYz1428P2fc1rYiwt8Kn4hMYf4+a00rU0 CX8xbYEvKIZ6C2MV9xxdOUeB1f8wKYUgqNJmvITV6LXOz5+A8TRQMKDFI 7QkfgPQbwf2u7entaZ5qgZM5WbrXoS/f9uqncWHrSjpbMILiz7qBvHAcU QGjNUxdxuVsFpxRKkcI8EJA+fEN8aL4TgEav3w2E4Dl/ztBnKrDlwdIFK SbPQtrU7VlrJ1/t/V/zU+IaaUe64Z9x1qJPmdvha/O8r9fAb3nGsDnlg9 Cpv5fq8ZhXJ2ImdinK4zwrwW8eUsQKnCYM0grvqQLZsSyx7EVYWI+wGce A==; X-IronPort-AV: E=McAfee;i="6600,9927,11010"; a="4794264" X-IronPort-AV: E=Sophos;i="6.07,119,1708416000"; d="scan'208";a="4794264" Received: from orviesa005.jf.intel.com ([10.64.159.145]) by fmvoesa111.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 12 Mar 2024 03:34:30 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.07,119,1708416000"; d="scan'208";a="16194060" Received: from turnipsi.fi.intel.com (HELO kekkonen.fi.intel.com) ([10.237.72.44]) by orviesa005-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 12 Mar 2024 03:34:29 -0700 Received: from svinhufvud.ger.corp.intel.com (localhost [IPv6:::1]) by kekkonen.fi.intel.com (Postfix) with ESMTP id D208E11FB89; Tue, 12 Mar 2024 12:34:25 +0200 (EET) From: Sakari Ailus To: linux-media@vger.kernel.org Cc: laurent.pinchart@ideasonboard.com, hverkuil@xs4all.nl Subject: [PATCH v3 03/26] Revert "[media] media: fix use-after-free in cdev_put() when app exits after driver unbind" Date: Tue, 12 Mar 2024 12:33:59 +0200 Message-Id: <20240312103422.216484-4-sakari.ailus@linux.intel.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20240312103422.216484-1-sakari.ailus@linux.intel.com> References: <20240312103422.216484-1-sakari.ailus@linux.intel.com> Precedence: bulk X-Mailing-List: linux-media@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 This reverts commit 5b28dde51d0c ("[media] media: fix use-after-free in cdev_put() when app exits after driver unbind"). The commit was part of an original patchset to avoid crashes when an unregistering device is in use. This revert is performed to roll back to a state which is more suitable for the objective: making media device refcountable. Signed-off-by: Sakari Ailus Acked-by: Hans Verkuil --- drivers/media/mc/mc-device.c | 6 ++--- drivers/media/mc/mc-devnode.c | 47 ++++++++++++++--------------------- 2 files changed, 21 insertions(+), 32 deletions(-) diff --git a/drivers/media/mc/mc-device.c b/drivers/media/mc/mc-device.c index 6c569ecd4b3d..4772a7f55112 100644 --- a/drivers/media/mc/mc-device.c +++ b/drivers/media/mc/mc-device.c @@ -732,16 +732,16 @@ int __must_check __media_device_register(struct media_device *mdev, ret = media_devnode_register(mdev, devnode, owner); if (ret < 0) { - /* devnode free is handled in media_devnode_*() */ mdev->devnode = NULL; + kfree(devnode); return ret; } ret = device_create_file(&devnode->dev, &dev_attr_model); if (ret < 0) { - /* devnode free is handled in media_devnode_*() */ mdev->devnode = NULL; media_devnode_unregister(devnode); + kfree(devnode); return ret; } @@ -824,8 +824,6 @@ void media_device_unregister(struct media_device *mdev) if (media_devnode_is_registered(mdev->devnode)) { device_remove_file(&mdev->devnode->dev, &dev_attr_model); media_devnode_unregister(mdev->devnode); - /* devnode free is handled in media_devnode_*() */ - mdev->devnode = NULL; } } EXPORT_SYMBOL_GPL(media_device_unregister); diff --git a/drivers/media/mc/mc-devnode.c b/drivers/media/mc/mc-devnode.c index 7827e9519382..e6ee1382f27d 100644 --- a/drivers/media/mc/mc-devnode.c +++ b/drivers/media/mc/mc-devnode.c @@ -51,8 +51,13 @@ static void media_devnode_release(struct device *cd) struct media_devnode *devnode = to_media_devnode(cd); mutex_lock(&media_devnode_lock); + + /* Delete the cdev on this minor as well */ + cdev_del(&devnode->cdev); + /* Mark device node number as free */ clear_bit(devnode->minor, media_devnode_nums); + mutex_unlock(&media_devnode_lock); /* Release media_devnode and perform other cleanups as needed. */ @@ -60,7 +65,6 @@ static void media_devnode_release(struct device *cd) devnode->release(devnode); kfree(devnode); - pr_debug("%s: Media Devnode Deallocated\n", __func__); } static const struct bus_type media_bus_type = { @@ -189,7 +193,6 @@ static int media_release(struct inode *inode, struct file *filp) /* decrease the refcount unconditionally since the release() return value is ignored. */ put_device(&devnode->dev); - return 0; } @@ -220,7 +223,6 @@ int __must_check media_devnode_register(struct media_device *mdev, if (minor == MEDIA_NUM_DEVICES) { mutex_unlock(&media_devnode_lock); pr_err("could not get a free minor\n"); - kfree(devnode); return -ENFILE; } @@ -230,33 +232,29 @@ int __must_check media_devnode_register(struct media_device *mdev, devnode->minor = minor; devnode->media_dev = mdev; - /* Part 1: Initialize dev now to use dev.kobj for cdev.kobj.parent */ - devnode->dev.bus = &media_bus_type; - devnode->dev.devt = MKDEV(MAJOR(media_dev_t), devnode->minor); - devnode->dev.release = media_devnode_release; - if (devnode->parent) - devnode->dev.parent = devnode->parent; - dev_set_name(&devnode->dev, "media%d", devnode->minor); - device_initialize(&devnode->dev); - /* Part 2: Initialize and register the character device */ cdev_init(&devnode->cdev, &media_devnode_fops); devnode->cdev.owner = owner; - devnode->cdev.kobj.parent = &devnode->dev.kobj; kobject_set_name(&devnode->cdev.kobj, "media%d", devnode->minor); ret = cdev_add(&devnode->cdev, MKDEV(MAJOR(media_dev_t), devnode->minor), 1); if (ret < 0) { pr_err("%s: cdev_add failed\n", __func__); - goto cdev_add_error; + goto error; } - /* Part 3: Add the media device */ - ret = device_add(&devnode->dev); + /* Part 3: Register the media device */ + devnode->dev.bus = &media_bus_type; + devnode->dev.devt = MKDEV(MAJOR(media_dev_t), devnode->minor); + devnode->dev.release = media_devnode_release; + if (devnode->parent) + devnode->dev.parent = devnode->parent; + dev_set_name(&devnode->dev, "media%d", devnode->minor); + ret = device_register(&devnode->dev); if (ret < 0) { - pr_err("%s: device_add failed\n", __func__); - goto device_add_error; + pr_err("%s: device_register failed\n", __func__); + goto error; } /* Part 4: Activate this minor. The char device can now be used. */ @@ -264,15 +262,12 @@ int __must_check media_devnode_register(struct media_device *mdev, return 0; -device_add_error: - cdev_del(&devnode->cdev); -cdev_add_error: +error: mutex_lock(&media_devnode_lock); + cdev_del(&devnode->cdev); clear_bit(devnode->minor, media_devnode_nums); - devnode->media_dev = NULL; mutex_unlock(&media_devnode_lock); - put_device(&devnode->dev); return ret; } @@ -284,13 +279,9 @@ void media_devnode_unregister(struct media_devnode *devnode) mutex_lock(&media_devnode_lock); clear_bit(MEDIA_FLAG_REGISTERED, &devnode->flags); - /* Delete the cdev on this minor as well */ - cdev_del(&devnode->cdev); - devnode->media_dev = NULL; mutex_unlock(&media_devnode_lock); - device_del(&devnode->dev); - put_device(&devnode->dev); + device_unregister(&devnode->dev); } /* From patchwork Tue Mar 12 10:34:00 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sakari Ailus X-Patchwork-Id: 13589798 Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 6F5F95730A for ; Tue, 12 Mar 2024 10:34:31 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=192.198.163.17 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1710239673; cv=none; b=EoT+h5gGQ1ZC3xP8e87706eMQlvvyaAi3WJ5prSRnd24Lv9ZDe/OMWLuFOEMjxOSkqVwmzUGBIyPqfx2PW30GozoyA6JWg5eNuy9+RaVOa2p9e324NORHaZpax0S/lDCyJKmbfGqOR3VYU+7c1a2NFM7oSxPdGEq8cwTO4rRhsE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1710239673; c=relaxed/simple; bh=huJz8vtYoKQIr7RibVp7+Lj+b9OTmQZbPyMn3Sm7kFc=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=FtP55XCZadD7qIK5lpHiPeHoclBeXUj7hbOgiwTXsoOI6GrI9v694q7zmPFiniOXgOHM8oF/pKdrdgKwsLI1tTM2cZKkr9ZLZnhmZqMgfietHhU3iMwIeC9Dvn69+2A2pnqqQgbbrl2TnMtIj67AaG74gie0n3TbtNCE/kbN1AA= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.intel.com; spf=none smtp.mailfrom=linux.intel.com; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b=hJh19zML; arc=none smtp.client-ip=192.198.163.17 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.intel.com Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=linux.intel.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="hJh19zML" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1710239672; x=1741775672; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=huJz8vtYoKQIr7RibVp7+Lj+b9OTmQZbPyMn3Sm7kFc=; b=hJh19zMLWSSYxXySTNyU7CLVrEHmuOPxvU1UTLAwE3y79gvnq0absw22 WKb8wgDL9YdTdvLvW9GOTbu5yoaISi6R9flOF9sS8CFBjSpqf/DYRJ4sE BLjev8/1u6kTaVTVYm3raVdwJS3q4D6nwpRzEAUwe+mKrTYX1yQByQJ2O Yra1OwtJrYPz8gXNI8RPqysFuhL7SgKWKEUaLHnNJCQUDxNUW3A8JKUYc 7S9JVQMcvdD0taTVS00pqlCFKmpqLjWZPdJDmuc0w7tWUqttN7exwJ+sB Ub54dI6SE3dFFDzdEhKXpA4x6NdiSqkzOFXXNKHdl/6Ax2qXyiHgw4x5C Q==; X-IronPort-AV: E=McAfee;i="6600,9927,11010"; a="4794268" X-IronPort-AV: E=Sophos;i="6.07,119,1708416000"; d="scan'208";a="4794268" Received: from orviesa005.jf.intel.com ([10.64.159.145]) by fmvoesa111.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 12 Mar 2024 03:34:30 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.07,119,1708416000"; d="scan'208";a="16194062" Received: from turnipsi.fi.intel.com (HELO kekkonen.fi.intel.com) ([10.237.72.44]) by orviesa005-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 12 Mar 2024 03:34:29 -0700 Received: from svinhufvud.ger.corp.intel.com (localhost [IPv6:::1]) by kekkonen.fi.intel.com (Postfix) with ESMTP id 6A2091201F7; Tue, 12 Mar 2024 12:34:26 +0200 (EET) From: Sakari Ailus To: linux-media@vger.kernel.org Cc: laurent.pinchart@ideasonboard.com, hverkuil@xs4all.nl Subject: [PATCH v3 04/26] media: mc: utilize new cdev_device_add helper function Date: Tue, 12 Mar 2024 12:34:00 +0200 Message-Id: <20240312103422.216484-5-sakari.ailus@linux.intel.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20240312103422.216484-1-sakari.ailus@linux.intel.com> References: <20240312103422.216484-1-sakari.ailus@linux.intel.com> Precedence: bulk X-Mailing-List: linux-media@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 From: Logan Gunthorpe Replace the open coded registration of the cdev and dev with the new device_add_cdev() helper. The helper replaces a common pattern by taking the proper reference against the parent device and adding both the cdev and the device. Signed-off-by: Logan Gunthorpe --- drivers/media/cec/core/cec-core.c | 16 ++++------------ drivers/media/mc/mc-devnode.c | 23 +++++++++-------------- 2 files changed, 13 insertions(+), 26 deletions(-) diff --git a/drivers/media/cec/core/cec-core.c b/drivers/media/cec/core/cec-core.c index 3e0c0dcff358..acd2fc979045 100644 --- a/drivers/media/cec/core/cec-core.c +++ b/drivers/media/cec/core/cec-core.c @@ -137,26 +137,19 @@ static int __must_check cec_devnode_register(struct cec_devnode *devnode, /* Part 2: Initialize and register the character device */ cdev_init(&devnode->cdev, &cec_devnode_fops); - devnode->cdev.kobj.parent = &devnode->dev.kobj; devnode->cdev.owner = owner; kobject_set_name(&devnode->cdev.kobj, "cec%d", devnode->minor); devnode->registered = true; - ret = cdev_add(&devnode->cdev, devnode->dev.devt, 1); - if (ret < 0) { - pr_err("%s: cdev_add failed\n", __func__); + ret = cdev_device_add(&devnode->cdev, &devnode->dev); + if (ret) { + pr_err("%s: cdev_device_add failed\n", __func__); devnode->registered = false; goto clr_bit; } - ret = device_add(&devnode->dev); - if (ret) - goto cdev_del; - return 0; -cdev_del: - cdev_del(&devnode->cdev); clr_bit: mutex_lock(&cec_devnode_lock); clear_bit(devnode->minor, cec_devnode_nums); @@ -202,8 +195,7 @@ static void cec_devnode_unregister(struct cec_adapter *adap) cec_adap_enable(adap); mutex_unlock(&adap->lock); - device_del(&devnode->dev); - cdev_del(&devnode->cdev); + cdev_device_del(&devnode->cdev, &devnode->dev); put_device(&devnode->dev); } diff --git a/drivers/media/mc/mc-devnode.c b/drivers/media/mc/mc-devnode.c index e6ee1382f27d..60dafd3d48ba 100644 --- a/drivers/media/mc/mc-devnode.c +++ b/drivers/media/mc/mc-devnode.c @@ -232,29 +232,24 @@ int __must_check media_devnode_register(struct media_device *mdev, devnode->minor = minor; devnode->media_dev = mdev; - /* Part 2: Initialize and register the character device */ + /* Part 2: Initialize the media and character devices */ cdev_init(&devnode->cdev, &media_devnode_fops); devnode->cdev.owner = owner; kobject_set_name(&devnode->cdev.kobj, "media%d", devnode->minor); - ret = cdev_add(&devnode->cdev, MKDEV(MAJOR(media_dev_t), - devnode->minor), 1); - if (ret < 0) { - pr_err("%s: cdev_add failed\n", __func__); - goto error; - } - - /* Part 3: Register the media device */ devnode->dev.bus = &media_bus_type; devnode->dev.devt = MKDEV(MAJOR(media_dev_t), devnode->minor); devnode->dev.release = media_devnode_release; if (devnode->parent) devnode->dev.parent = devnode->parent; dev_set_name(&devnode->dev, "media%d", devnode->minor); - ret = device_register(&devnode->dev); + device_initialize(&devnode->dev); + + /* Part 3: Add the media and character devices */ + ret = cdev_device_add(&devnode->cdev, &devnode->dev); if (ret < 0) { - pr_err("%s: device_register failed\n", __func__); - goto error; + pr_err("%s: cdev_device_add failed\n", __func__); + goto cdev_add_error; } /* Part 4: Activate this minor. The char device can now be used. */ @@ -262,9 +257,9 @@ int __must_check media_devnode_register(struct media_device *mdev, return 0; -error: +cdev_add_error: mutex_lock(&media_devnode_lock); - cdev_del(&devnode->cdev); + cdev_device_del(&devnode->cdev, &devnode->dev); clear_bit(devnode->minor, media_devnode_nums); mutex_unlock(&media_devnode_lock); From patchwork Tue Mar 12 10:34:01 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sakari Ailus X-Patchwork-Id: 13589799 Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id E00FC58AC8 for ; Tue, 12 Mar 2024 10:34:31 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=192.198.163.17 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1710239673; cv=none; b=PAWEmWlVMuTFQDfLlYgKDJAZ9pM1ws/4HlRJtoDSwwIwRMycOLaCbTOkHFbdDzt/Hgv/FZ1Bvhi5MW0uG5Urq6P/dHXPwm/Umbi/c/VADX4zLP2IRux6gTYLDCmDAJ8thJ3vB/yxgLPVLTt4HQ024n2FQxEPdo0F3JhzhyIwT98= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1710239673; c=relaxed/simple; bh=NfyHwqteI3KUApRl+uZSf6G0mbjzscxxSse4risGnh0=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=QjTSMFfgJWPPofS7OmE+5KreGPVGTmBtYhwmBMJCEbeh2/ffKMzR6zeX3v9QTUKFTxpFeOqWUSMGjNPfeuyfaR/5Suoshxs4dkVqUhRYlXWVQ3/eFb3v2gZdvtHRZCOXxbRBSrEl76ISdoIsDv2WD0RAcqLT+KdnOMyr/3aWxYw= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.intel.com; spf=none smtp.mailfrom=linux.intel.com; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b=Ygw7pKxn; arc=none smtp.client-ip=192.198.163.17 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.intel.com Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=linux.intel.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="Ygw7pKxn" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1710239672; x=1741775672; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=NfyHwqteI3KUApRl+uZSf6G0mbjzscxxSse4risGnh0=; b=Ygw7pKxnM7/Z/x1wTShGBeZFgnyE3tGFKJuzWB/9WApYZSdM1p0HZR9i nyQ/N9Sr2/k0PysUUKvxG7Bow4Jx/vMWt06+6ozpeMJTK9TqjTz0B5v9S SMbN2FPg0lgK2piLHbCVcXbUKg1Y9W9XmxlJ3/YL6ytVwBcGcqYnZdRd9 89JrSUO+dgcfQ96lE0OetrKG7OFRQMA2aEI6bjgIvtwdC+KvMivPLthiS AK7N4PE2wzcBOBrP38drEp241uZ1BepS2EO00u0mbq+lyCwPuEv4Nb5Fp Z7tdAX5qv0TJAHJ+of2ENYtXGAnIUHRrK61rObe7saydtDMsSnhq/ywvk g==; X-IronPort-AV: E=McAfee;i="6600,9927,11010"; a="4794273" X-IronPort-AV: E=Sophos;i="6.07,119,1708416000"; d="scan'208";a="4794273" Received: from orviesa005.jf.intel.com ([10.64.159.145]) by fmvoesa111.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 12 Mar 2024 03:34:31 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.07,119,1708416000"; d="scan'208";a="16194064" Received: from turnipsi.fi.intel.com (HELO kekkonen.fi.intel.com) ([10.237.72.44]) by orviesa005-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 12 Mar 2024 03:34:30 -0700 Received: from svinhufvud.ger.corp.intel.com (localhost [IPv6:::1]) by kekkonen.fi.intel.com (Postfix) with ESMTP id 02300120619; Tue, 12 Mar 2024 12:34:26 +0200 (EET) From: Sakari Ailus To: linux-media@vger.kernel.org Cc: laurent.pinchart@ideasonboard.com, hverkuil@xs4all.nl Subject: [PATCH v3 05/26] Revert "[media] media-device: dynamically allocate struct media_devnode" Date: Tue, 12 Mar 2024 12:34:01 +0200 Message-Id: <20240312103422.216484-6-sakari.ailus@linux.intel.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20240312103422.216484-1-sakari.ailus@linux.intel.com> References: <20240312103422.216484-1-sakari.ailus@linux.intel.com> Precedence: bulk X-Mailing-List: linux-media@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 This reverts commit a087ce704b80 ("[media] media-device: dynamically allocate struct media_devnode"). The commit was part of an original patchset to avoid crashes when an unregistering device is in use. This revert is performed to roll back to a state which is more suitable for the objective: making media device refcountable. Signed-off-by: Sakari Ailus Acked-by: Hans Verkuil --- drivers/media/mc/mc-device.c | 44 +++++++-------------- drivers/media/mc/mc-devnode.c | 7 +--- drivers/media/usb/au0828/au0828-core.c | 4 +- drivers/media/usb/uvc/uvc_driver.c | 2 +- drivers/staging/media/sunxi/cedrus/cedrus.c | 2 +- include/media/media-device.h | 5 ++- include/media/media-devnode.h | 15 +------ sound/usb/media.c | 8 ++-- 8 files changed, 29 insertions(+), 58 deletions(-) diff --git a/drivers/media/mc/mc-device.c b/drivers/media/mc/mc-device.c index 4772a7f55112..d4553a3705f5 100644 --- a/drivers/media/mc/mc-device.c +++ b/drivers/media/mc/mc-device.c @@ -435,7 +435,7 @@ static long media_device_ioctl(struct file *filp, unsigned int cmd, unsigned long __arg) { struct media_devnode *devnode = media_devnode_data(filp); - struct media_device *dev = devnode->media_dev; + struct media_device *dev = to_media_device(devnode); const struct media_ioctl_info *info; void __user *arg = (void __user *)__arg; char __karg[256], *karg = __karg; @@ -519,7 +519,7 @@ static long media_device_compat_ioctl(struct file *filp, unsigned int cmd, unsigned long arg) { struct media_devnode *devnode = media_devnode_data(filp); - struct media_device *dev = devnode->media_dev; + struct media_device *dev = to_media_device(devnode); long ret; switch (cmd) { @@ -555,8 +555,7 @@ static const struct media_file_operations media_device_fops = { static ssize_t model_show(struct device *cd, struct device_attribute *attr, char *buf) { - struct media_devnode *devnode = to_media_devnode(cd); - struct media_device *mdev = devnode->media_dev; + struct media_device *mdev = to_media_device(to_media_devnode(cd)); return sprintf(buf, "%.*s\n", (int)sizeof(mdev->model), mdev->model); } @@ -714,34 +713,23 @@ EXPORT_SYMBOL_GPL(media_device_cleanup); int __must_check __media_device_register(struct media_device *mdev, struct module *owner) { - struct media_devnode *devnode; int ret; - devnode = kzalloc(sizeof(*devnode), GFP_KERNEL); - if (!devnode) - return -ENOMEM; - /* Register the device node. */ - mdev->devnode = devnode; - devnode->fops = &media_device_fops; - devnode->parent = mdev->dev; - devnode->release = media_device_release; + mdev->devnode.fops = &media_device_fops; + mdev->devnode.parent = mdev->dev; + mdev->devnode.release = media_device_release; /* Set version 0 to indicate user-space that the graph is static */ mdev->topology_version = 0; - ret = media_devnode_register(mdev, devnode, owner); - if (ret < 0) { - mdev->devnode = NULL; - kfree(devnode); + ret = media_devnode_register(&mdev->devnode, owner); + if (ret < 0) return ret; - } - ret = device_create_file(&devnode->dev, &dev_attr_model); + ret = device_create_file(&mdev->devnode.dev, &dev_attr_model); if (ret < 0) { - mdev->devnode = NULL; - media_devnode_unregister(devnode); - kfree(devnode); + media_devnode_unregister(&mdev->devnode); return ret; } @@ -791,7 +779,7 @@ void media_device_unregister(struct media_device *mdev) mutex_lock(&mdev->graph_mutex); /* Check if mdev was ever registered at all */ - if (!media_devnode_is_registered(mdev->devnode)) { + if (!media_devnode_is_registered(&mdev->devnode)) { mutex_unlock(&mdev->graph_mutex); return; } @@ -818,13 +806,9 @@ void media_device_unregister(struct media_device *mdev) mutex_unlock(&mdev->graph_mutex); - dev_dbg(mdev->dev, "Media device unregistered\n"); - - /* Check if mdev devnode was registered */ - if (media_devnode_is_registered(mdev->devnode)) { - device_remove_file(&mdev->devnode->dev, &dev_attr_model); - media_devnode_unregister(mdev->devnode); - } + device_remove_file(&mdev->devnode.dev, &dev_attr_model); + dev_dbg(mdev->dev, "Media device unregistering\n"); + media_devnode_unregister(&mdev->devnode); } EXPORT_SYMBOL_GPL(media_device_unregister); diff --git a/drivers/media/mc/mc-devnode.c b/drivers/media/mc/mc-devnode.c index 60dafd3d48ba..186f585545c2 100644 --- a/drivers/media/mc/mc-devnode.c +++ b/drivers/media/mc/mc-devnode.c @@ -32,7 +32,6 @@ #include #include -#include #define MEDIA_NUM_DEVICES 256 #define MEDIA_NAME "media" @@ -63,8 +62,6 @@ static void media_devnode_release(struct device *cd) /* Release media_devnode and perform other cleanups as needed. */ if (devnode->release) devnode->release(devnode); - - kfree(devnode); } static const struct bus_type media_bus_type = { @@ -210,8 +207,7 @@ static const struct file_operations media_devnode_fops = { .llseek = no_llseek, }; -int __must_check media_devnode_register(struct media_device *mdev, - struct media_devnode *devnode, +int __must_check media_devnode_register(struct media_devnode *devnode, struct module *owner) { int minor; @@ -230,7 +226,6 @@ int __must_check media_devnode_register(struct media_device *mdev, mutex_unlock(&media_devnode_lock); devnode->minor = minor; - devnode->media_dev = mdev; /* Part 2: Initialize the media and character devices */ cdev_init(&devnode->cdev, &media_devnode_fops); diff --git a/drivers/media/usb/au0828/au0828-core.c b/drivers/media/usb/au0828/au0828-core.c index 1e246b47766d..4101e12cda5a 100644 --- a/drivers/media/usb/au0828/au0828-core.c +++ b/drivers/media/usb/au0828/au0828-core.c @@ -128,7 +128,7 @@ static void au0828_unregister_media_device(struct au0828_dev *dev) struct media_device *mdev = dev->media_dev; struct media_entity_notify *notify, *nextp; - if (!mdev || !media_devnode_is_registered(mdev->devnode)) + if (!mdev || !media_devnode_is_registered(&mdev->devnode)) return; /* Remove au0828 entity_notify callbacks */ @@ -566,7 +566,7 @@ static int au0828_media_device_register(struct au0828_dev *dev, if (!dev->media_dev) return 0; - if (!media_devnode_is_registered(dev->media_dev->devnode)) { + if (!media_devnode_is_registered(&dev->media_dev->devnode)) { /* register media device */ ret = media_device_register(dev->media_dev); diff --git a/drivers/media/usb/uvc/uvc_driver.c b/drivers/media/usb/uvc/uvc_driver.c index bbd90123a4e7..7add92f45329 100644 --- a/drivers/media/usb/uvc/uvc_driver.c +++ b/drivers/media/usb/uvc/uvc_driver.c @@ -1922,7 +1922,7 @@ static void uvc_unregister_video(struct uvc_device *dev) if (dev->vdev.dev) v4l2_device_unregister(&dev->vdev); #ifdef CONFIG_MEDIA_CONTROLLER - if (media_devnode_is_registered(dev->mdev.devnode)) + if (media_devnode_is_registered(&dev->mdev.devnode)) media_device_unregister(&dev->mdev); #endif } diff --git a/drivers/staging/media/sunxi/cedrus/cedrus.c b/drivers/staging/media/sunxi/cedrus/cedrus.c index f52df6836045..f8f678835a4c 100644 --- a/drivers/staging/media/sunxi/cedrus/cedrus.c +++ b/drivers/staging/media/sunxi/cedrus/cedrus.c @@ -548,7 +548,7 @@ static void cedrus_remove(struct platform_device *pdev) struct cedrus_dev *dev = platform_get_drvdata(pdev); cancel_delayed_work_sync(&dev->watchdog_work); - if (media_devnode_is_registered(dev->mdev.devnode)) { + if (media_devnode_is_registered(&dev->mdev.devnode)) { media_device_unregister(&dev->mdev); v4l2_m2m_unregister_media_controller(dev->m2m_dev); media_device_cleanup(&dev->mdev); diff --git a/include/media/media-device.h b/include/media/media-device.h index 53d2a16a70b0..c791f3d5ad77 100644 --- a/include/media/media-device.h +++ b/include/media/media-device.h @@ -145,7 +145,7 @@ struct media_device_ops { struct media_device { /* dev->driver_data points to this struct. */ struct device *dev; - struct media_devnode *devnode; + struct media_devnode devnode; char model[32]; char driver_name[32]; @@ -191,6 +191,9 @@ struct usb_device; #define MEDIA_DEV_NOTIFY_PRE_LINK_CH 0 #define MEDIA_DEV_NOTIFY_POST_LINK_CH 1 +/* media_devnode to media_device */ +#define to_media_device(node) container_of(node, struct media_device, devnode) + /** * media_device_init() - Initializes a media device element * diff --git a/include/media/media-devnode.h b/include/media/media-devnode.h index 46f0d3ae44d1..1117d1dfd6bf 100644 --- a/include/media/media-devnode.h +++ b/include/media/media-devnode.h @@ -21,8 +21,6 @@ #include #include -struct media_device; - /* * Flag to mark the media_devnode struct as registered. Drivers must not touch * this flag directly, it will be set and cleared by media_devnode_register and @@ -73,8 +71,6 @@ struct media_file_operations { * before registering the node. */ struct media_devnode { - struct media_device *media_dev; - /* device ops */ const struct media_file_operations *fops; @@ -97,8 +93,7 @@ struct media_devnode { /** * media_devnode_register - register a media device node * - * @mdev: struct media_device we want to register a device node - * @devnode: media device node structure we want to register + * @devnode: struct media_devnode we want to register a device node * @owner: should be filled with %THIS_MODULE * * The registration code assigns minor numbers and registers the new device node @@ -111,8 +106,7 @@ struct media_devnode { * the media_devnode structure is *not* called, so the caller is responsible for * freeing any data. */ -int __must_check media_devnode_register(struct media_device *mdev, - struct media_devnode *devnode, +int __must_check media_devnode_register(struct media_devnode *devnode, struct module *owner); /** @@ -142,14 +136,9 @@ static inline struct media_devnode *media_devnode_data(struct file *filp) * false otherwise. * * @devnode: pointer to struct &media_devnode. - * - * Note: If mdev is NULL, it also returns false. */ static inline int media_devnode_is_registered(struct media_devnode *devnode) { - if (!devnode) - return false; - return test_bit(MEDIA_FLAG_REGISTERED, &devnode->flags); } diff --git a/sound/usb/media.c b/sound/usb/media.c index d48db6f3ae65..f97e937378ee 100644 --- a/sound/usb/media.c +++ b/sound/usb/media.c @@ -122,7 +122,7 @@ void snd_media_stream_delete(struct snd_usb_substream *subs) struct media_device *mdev; mdev = mctl->media_dev; - if (mdev && media_devnode_is_registered(mdev->devnode)) { + if (mdev && media_devnode_is_registered(&mdev->devnode)) { media_devnode_remove(mctl->intf_devnode); media_device_unregister_entity(&mctl->media_entity); media_entity_cleanup(&mctl->media_entity); @@ -239,14 +239,14 @@ static void snd_media_mixer_delete(struct snd_usb_audio *chip) if (!mixer->media_mixer_ctl) continue; - if (media_devnode_is_registered(mdev->devnode)) { + if (media_devnode_is_registered(&mdev->devnode)) { media_device_unregister_entity(&mctl->media_entity); media_entity_cleanup(&mctl->media_entity); } kfree(mctl); mixer->media_mixer_ctl = NULL; } - if (media_devnode_is_registered(mdev->devnode)) + if (media_devnode_is_registered(&mdev->devnode)) media_devnode_remove(chip->ctl_intf_media_devnode); chip->ctl_intf_media_devnode = NULL; } @@ -284,7 +284,7 @@ int snd_media_device_create(struct snd_usb_audio *chip, "Couldn't create media mixer entities. Error: %d\n", ret); - if (!media_devnode_is_registered(mdev->devnode)) { + if (!media_devnode_is_registered(&mdev->devnode)) { /* don't register if snd_media_mixer_init() failed */ if (ret) goto create_fail; From patchwork Tue Mar 12 10:34:02 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sakari Ailus X-Patchwork-Id: 13589800 Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id B1CC75915C for ; Tue, 12 Mar 2024 10:34:32 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=192.198.163.17 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1710239674; cv=none; b=FGhbi7l6ygWvM5D8kamFOQpDzzwS1Mi/rPXc7ZAoSxZcCQmziQfn7MO0ttu4Py5sPcZsAI/BNjZhFJjZsWmc17+evLx3Vkwi+ibJjfqpZHHRI9YeK6+1SHPMCvYqS4V331AZu3dm/Hn8bhz2lXGpM0QebJhEpLvzwQyCQbXAbgk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1710239674; c=relaxed/simple; bh=BCV4Q0jxmi4NLkGyKEPc+e1Rh62tXpkYRWDOjDRVNUE=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=Ww96nNZfgkwoDzyp4uMVbDzNmuTJ0/DHx2d9zpFdVW3ezK/lhs0RlMQB9Iwy1KeHTFhP2l23/xaVdRdYB9woU/50UhhUruox/IOGQwE3uk7CUL+Avd1lGl4fWvumiAywfIoIR0ufXJH8JbWz1LV0hkoZ+uQGNafMrFsggmAg308= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.intel.com; spf=none smtp.mailfrom=linux.intel.com; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b=aWvLPy8v; arc=none smtp.client-ip=192.198.163.17 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.intel.com Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=linux.intel.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="aWvLPy8v" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1710239673; x=1741775673; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=BCV4Q0jxmi4NLkGyKEPc+e1Rh62tXpkYRWDOjDRVNUE=; b=aWvLPy8vqB+HLnoEeJ/kiE1qULmGm7dVRkeU3AiytlL92n+ZWUlFUvyJ fQkzdN4CSjJg3lHgHIl6zzmfFXi4POMRkMjLPPsi4v2AQo2FCroetDJsL JDeOerLJAMrPBi6vrVg8fVXyRMqHbbjFS5Wozi+BSKoifqGdv/y+f13oA UbEotyIpSNnRjm+axwREb/qny7gWavDu5NB/ctyonvBD4o5yriSoX6M7z YbDKOXhrz94QlKOMC1CaZ4/xDIf8Ok9UXnob/sip8uRLOtIpZ28SiUCM5 uIwKIX9bjtVXak3hPUmhhkj/YRp3dP+rO+q9Rj1PkXVI2SWn631/sB4zz A==; X-IronPort-AV: E=McAfee;i="6600,9927,11010"; a="4794277" X-IronPort-AV: E=Sophos;i="6.07,119,1708416000"; d="scan'208";a="4794277" Received: from orviesa005.jf.intel.com ([10.64.159.145]) by fmvoesa111.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 12 Mar 2024 03:34:31 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.07,119,1708416000"; d="scan'208";a="16194066" Received: from turnipsi.fi.intel.com (HELO kekkonen.fi.intel.com) ([10.237.72.44]) by orviesa005-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 12 Mar 2024 03:34:30 -0700 Received: from svinhufvud.ger.corp.intel.com (localhost [IPv6:::1]) by kekkonen.fi.intel.com (Postfix) with ESMTP id B77F711F819; Tue, 12 Mar 2024 12:34:27 +0200 (EET) From: Sakari Ailus To: linux-media@vger.kernel.org Cc: laurent.pinchart@ideasonboard.com, hverkuil@xs4all.nl Subject: [PATCH v3 06/26] media: mc: Drop nop release callback Date: Tue, 12 Mar 2024 12:34:02 +0200 Message-Id: <20240312103422.216484-7-sakari.ailus@linux.intel.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20240312103422.216484-1-sakari.ailus@linux.intel.com> References: <20240312103422.216484-1-sakari.ailus@linux.intel.com> Precedence: bulk X-Mailing-List: linux-media@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 The release callback is only used to print a debug message. Drop it. (It will be re-introduced later in a different form.) Signed-off-by: Sakari Ailus Acked-by: Hans Verkuil Reviewed-by: Laurent Pinchart --- drivers/media/mc/mc-device.c | 6 ------ 1 file changed, 6 deletions(-) diff --git a/drivers/media/mc/mc-device.c b/drivers/media/mc/mc-device.c index d4553a3705f5..c0ea08a8fc31 100644 --- a/drivers/media/mc/mc-device.c +++ b/drivers/media/mc/mc-device.c @@ -566,11 +566,6 @@ static DEVICE_ATTR_RO(model); * Registration/unregistration */ -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; @@ -718,7 +713,6 @@ int __must_check __media_device_register(struct media_device *mdev, /* Register the device node. */ mdev->devnode.fops = &media_device_fops; mdev->devnode.parent = mdev->dev; - mdev->devnode.release = media_device_release; /* Set version 0 to indicate user-space that the graph is static */ mdev->topology_version = 0; From patchwork Tue Mar 12 10:34:03 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sakari Ailus X-Patchwork-Id: 13589801 Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 1F29D69D0B for ; Tue, 12 Mar 2024 10:34:33 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=192.198.163.17 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1710239674; cv=none; b=nNQtTWKNdElmuaVZ2gSyDGev1IJiWyk+PHbUuUB4FlZu9Zzkei6qm6CUuWszgYLd7GU66t3MkuxmK/8EhnR9LS8WObJyL53zMwYS08InX1edqDw3zCqJvAIP6pb+6PakOjCu4HBA2IR0y7bOK7gvt1JZ1CIxWuIZmPVnx5YGVKI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1710239674; c=relaxed/simple; bh=HyOG+obzA3lxQt6iGtMe16dffGHTQG1lkMpum4Twabs=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=hpjHT5OJTB/gPjbGAVTClNEYBPMerOgx7gG0VFeQKrUPBp1mVZY6xmbtNoXecZLBqel9YsX8SYIfvUgfSZON3RkWZWaSfxKYvW+rYPOW7IVIk6h9kMAD8MN/008L9UUb2mGuDpw1BQ/NnLpacsmjR5PyN7oVXf0cTnprzpaRK04= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.intel.com; spf=none smtp.mailfrom=linux.intel.com; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b=kXHUK4HS; arc=none smtp.client-ip=192.198.163.17 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.intel.com Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=linux.intel.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="kXHUK4HS" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1710239673; x=1741775673; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=HyOG+obzA3lxQt6iGtMe16dffGHTQG1lkMpum4Twabs=; b=kXHUK4HSWZedief6YzXcnFavQ5324//oYGID61tzGx3SiUiDE5XqJckR Cska+c4n0Ae8BlhhDVJ7ibxH2pSfOFCAPFwH2JVCsMqgGz0zbW//t/KQN kzPMIMhRfUmdv3NAh2J+7Nw8NEprO8P0aXfmNyXl+XMnsRmgi+egxMYRz Ww8wKnWGcnUIgchVwnsemKyqccVqjkEABnXE/zLy91bxj5EScBJ8w/Khc 88TQSBTF+gWMkZ6pcc3VJHGanD81EbLZghNtebfmk96MyKsf7cCvoykIg qS59ynCmGMyvnZiAtid/Lw9wP6uuMCHnlb71ZZehnw2wVwFpQ2X3c7TwH A==; X-IronPort-AV: E=McAfee;i="6600,9927,11010"; a="4794281" X-IronPort-AV: E=Sophos;i="6.07,119,1708416000"; d="scan'208";a="4794281" Received: from orviesa005.jf.intel.com ([10.64.159.145]) by fmvoesa111.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 12 Mar 2024 03:34:32 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.07,119,1708416000"; d="scan'208";a="16194067" Received: from turnipsi.fi.intel.com (HELO kekkonen.fi.intel.com) ([10.237.72.44]) by orviesa005-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 12 Mar 2024 03:34:31 -0700 Received: from svinhufvud.ger.corp.intel.com (localhost [IPv6:::1]) by kekkonen.fi.intel.com (Postfix) with ESMTP id 6D52011F81D; Tue, 12 Mar 2024 12:34:28 +0200 (EET) From: Sakari Ailus To: linux-media@vger.kernel.org Cc: laurent.pinchart@ideasonboard.com, hverkuil@xs4all.nl Subject: [PATCH v3 07/26] media: mc: Drop media_dev description from struct media_devnode Date: Tue, 12 Mar 2024 12:34:03 +0200 Message-Id: <20240312103422.216484-8-sakari.ailus@linux.intel.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20240312103422.216484-1-sakari.ailus@linux.intel.com> References: <20240312103422.216484-1-sakari.ailus@linux.intel.com> Precedence: bulk X-Mailing-List: linux-media@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Signed-off-by: Sakari Ailus --- include/media/media-devnode.h | 1 - 1 file changed, 1 deletion(-) diff --git a/include/media/media-devnode.h b/include/media/media-devnode.h index 1117d1dfd6bf..024dfb98a6fd 100644 --- a/include/media/media-devnode.h +++ b/include/media/media-devnode.h @@ -55,7 +55,6 @@ struct media_file_operations { /** * struct media_devnode - Media device node - * @media_dev: pointer to struct &media_device * @fops: pointer to struct &media_file_operations with media device ops * @dev: pointer to struct &device containing the media controller device * @cdev: struct cdev pointer character device From patchwork Tue Mar 12 10:34:04 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sakari Ailus X-Patchwork-Id: 13589802 Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id E13DD7867A for ; Tue, 12 Mar 2024 10:34:33 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=192.198.163.17 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1710239675; cv=none; b=F2QuygS9+A+V12+lX/BfG0Ilm9aW8foOlYgN4a0LX+0riRoXSEcNzhS+0SP2helrXABZ1EsdSBr3RvvIaoXyh9+Wwxal1f3VvmAPMXu/hgfjh6idosypOxx8UANbN4mFab2GgQ3xeznAeoWxsIpqKeQAyTVb5S1d0tnk5dHZWhQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1710239675; c=relaxed/simple; bh=bbLg7O1l6ZI5zph8yNSYjKyaUdrjlP/AihfL7XBciVc=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=JYzvndpVoKITjTZpwmr5VHbRgcgAeZX4r2RCBb+HBcPGi6kysJm2CB27AltXi9KgyVV+Oo0uQdw/C/L/1+dZ/I8Yl/S9GFaGnoDag6lvJks3IYzzgSCfKuRs45F1gpsxNRrKBWDdkpmSMy84tZVuqCxGDQjurwoK3cNqx7IpW60= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.intel.com; spf=none smtp.mailfrom=linux.intel.com; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b=Sq49bCG4; arc=none smtp.client-ip=192.198.163.17 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.intel.com Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=linux.intel.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="Sq49bCG4" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1710239674; x=1741775674; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=bbLg7O1l6ZI5zph8yNSYjKyaUdrjlP/AihfL7XBciVc=; b=Sq49bCG4WV8wLeN0Et4VKAuNPjruQtvjFkTVmvUrtNRK+48NHcAbvGeE 6lmqwqVIoQcwoTVhPebAuKu40vEXfaoI4UQPgRDCSth+pGOYTuydwFqeo ORY0mpN/ufL1Sj44YRDc371HxJOv9BR44F5oZ/3g5RsmqN8fRdWVAetcF BTCYEe749Fa7VBEPJsxQvy7fsQrL+mTXIN8qS6LV6nQ5Fa4J3ckz3b/0e x0Ewc376BHXmfe40ikczLNRzXY9/9H/VR3p+6FSxet16L65oPnZST8jbq CBTFN7V3Zu31eAOYqwuo0jcB9onsicDsQ6myW53YIm2ZWxzzXMpF/nnY5 w==; X-IronPort-AV: E=McAfee;i="6600,9927,11010"; a="4794285" X-IronPort-AV: E=Sophos;i="6.07,119,1708416000"; d="scan'208";a="4794285" Received: from orviesa005.jf.intel.com ([10.64.159.145]) by fmvoesa111.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 12 Mar 2024 03:34:32 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.07,119,1708416000"; d="scan'208";a="16194068" Received: from turnipsi.fi.intel.com (HELO kekkonen.fi.intel.com) ([10.237.72.44]) by orviesa005-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 12 Mar 2024 03:34:32 -0700 Received: from svinhufvud.ger.corp.intel.com (localhost [IPv6:::1]) by kekkonen.fi.intel.com (Postfix) with ESMTP id E5F0311FB11; Tue, 12 Mar 2024 12:34:28 +0200 (EET) From: Sakari Ailus To: linux-media@vger.kernel.org Cc: laurent.pinchart@ideasonboard.com, hverkuil@xs4all.nl Subject: [PATCH v3 08/26] media: mc: Do not call cdev_device_del() if cdev_device_add() fails Date: Tue, 12 Mar 2024 12:34:04 +0200 Message-Id: <20240312103422.216484-9-sakari.ailus@linux.intel.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20240312103422.216484-1-sakari.ailus@linux.intel.com> References: <20240312103422.216484-1-sakari.ailus@linux.intel.com> Precedence: bulk X-Mailing-List: linux-media@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 cdev_device_del() is the right function to remove a device when cdev_device_add() succeeds. If it does not, however, put_device() needs to be used instead. Fix this. Signed-off-by: Sakari Ailus Acked-by: Hans Verkuil --- drivers/media/mc/mc-devnode.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/drivers/media/mc/mc-devnode.c b/drivers/media/mc/mc-devnode.c index 186f585545c2..5696ccf01d81 100644 --- a/drivers/media/mc/mc-devnode.c +++ b/drivers/media/mc/mc-devnode.c @@ -254,9 +254,9 @@ int __must_check media_devnode_register(struct media_devnode *devnode, cdev_add_error: mutex_lock(&media_devnode_lock); - cdev_device_del(&devnode->cdev, &devnode->dev); clear_bit(devnode->minor, media_devnode_nums); mutex_unlock(&media_devnode_lock); + put_device(&devnode->dev); return ret; } From patchwork Tue Mar 12 10:34:05 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sakari Ailus X-Patchwork-Id: 13589803 Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 74A1078695 for ; Tue, 12 Mar 2024 10:34:34 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=192.198.163.17 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1710239676; cv=none; b=pCH1R+/II/PRSPvDhMDxPZEwEBjkH9CJ4amnrHFhCN/bB+Y/48eJoOw45SO62ZZ6wba+t0/SPhudTafhOtUOjYeiyo0Ms4SxoXBiutruO2dpZomJahgPYkPp5RveEqz/so2wUqKJW6y7oHA/Ho+TdtezZ8ib6t3eubftyho2S5Y= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1710239676; c=relaxed/simple; bh=pumpeucLrbSPE9Y0gR7PCLnIjblk0+6TPE2td5baNZk=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=MHAiNZ3yuUoFFwpQJdI0/LCX3q2lgD4eegLK4WXfJexN3ALRl68QlnpdE0Y0FsEN2UKyuE2ORxbVVgRHMnKK8khRhiKr6CxiuubbiDmAxoUG5zoxyFSBW6fpO8vu4DiwTKGuNXLc73IycTCmu6kBMVPmuFinQuEsXqQcpeGuD+4= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.intel.com; spf=none smtp.mailfrom=linux.intel.com; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b=BRF3D4DN; arc=none smtp.client-ip=192.198.163.17 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.intel.com Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=linux.intel.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="BRF3D4DN" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1710239675; x=1741775675; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=pumpeucLrbSPE9Y0gR7PCLnIjblk0+6TPE2td5baNZk=; b=BRF3D4DNRcEkTq6BJsyRrzQGXCwPIuB6cUW4xuPh6xUPqGWM+3/8qtaM wjSUPxz41YWKwzXTn92so7R+uv7kAxXkv7WOTRLgLpjMCecP4vCY/tZYJ vkKU1cZpWIMbvxhUjIFYvBdQA5GLfwOe9j0/7uySzI0yL8gpseMyozaUG 7nGlapdNFGRpvAy+gHPSajPdsZFS7GVKZp1sH/AHcJDoy94BRCH47Hvxj reAQ8QTiuRCZzqueGiyqQf1SxuIy5Pea+FSHMPcOJ4yYGeXHN+9mZario xTD0Evk0SfTpXRCfKteNNEK0nRIRuvWaTBymO680p99f0S8aIxvGeEonF Q==; X-IronPort-AV: E=McAfee;i="6600,9927,11010"; a="4794291" X-IronPort-AV: E=Sophos;i="6.07,119,1708416000"; d="scan'208";a="4794291" Received: from orviesa005.jf.intel.com ([10.64.159.145]) by fmvoesa111.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 12 Mar 2024 03:34:33 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.07,119,1708416000"; d="scan'208";a="16194079" Received: from turnipsi.fi.intel.com (HELO kekkonen.fi.intel.com) ([10.237.72.44]) by orviesa005-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 12 Mar 2024 03:34:32 -0700 Received: from svinhufvud.ger.corp.intel.com (localhost [IPv6:::1]) by kekkonen.fi.intel.com (Postfix) with ESMTP id 6AEE91206F9; Tue, 12 Mar 2024 12:34:29 +0200 (EET) From: Sakari Ailus To: linux-media@vger.kernel.org Cc: laurent.pinchart@ideasonboard.com, hverkuil@xs4all.nl Subject: [PATCH v3 09/26] media: mc: Delete character device early Date: Tue, 12 Mar 2024 12:34:05 +0200 Message-Id: <20240312103422.216484-10-sakari.ailus@linux.intel.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20240312103422.216484-1-sakari.ailus@linux.intel.com> References: <20240312103422.216484-1-sakari.ailus@linux.intel.com> Precedence: bulk X-Mailing-List: linux-media@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 The parent of the character device related to the media devnode is the media devnode. Thus the character device needs to be released before the media devnode's release function. Move it to unregistering of the media devnode, which mirrors adding the character device in conjunction with registering the media devnode. Signed-off-by: Sakari Ailus Acked-by: Hans Verkuil Reviewed-by: Laurent Pinchart --- drivers/media/mc/mc-devnode.c | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/drivers/media/mc/mc-devnode.c b/drivers/media/mc/mc-devnode.c index 5696ccf01d81..dbf546853ca9 100644 --- a/drivers/media/mc/mc-devnode.c +++ b/drivers/media/mc/mc-devnode.c @@ -51,9 +51,6 @@ static void media_devnode_release(struct device *cd) mutex_lock(&media_devnode_lock); - /* Delete the cdev on this minor as well */ - cdev_del(&devnode->cdev); - /* Mark device node number as free */ clear_bit(devnode->minor, media_devnode_nums); @@ -271,6 +268,7 @@ void media_devnode_unregister(struct media_devnode *devnode) clear_bit(MEDIA_FLAG_REGISTERED, &devnode->flags); mutex_unlock(&media_devnode_lock); + cdev_del(&devnode->cdev); device_unregister(&devnode->dev); } From patchwork Tue Mar 12 10:34:06 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sakari Ailus X-Patchwork-Id: 13589804 Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 9EB1578B46 for ; Tue, 12 Mar 2024 10:34:34 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=192.198.163.17 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1710239676; cv=none; b=XxAzbUsSzP2UhldqJunCq/UK48GjAohAb9pcU3viVGtsvI1iJFZvIuCOAkjFibxMZ5oSsGCo68fYuHBR7Y4SeYFmrm4qmUr4R90vHLIj0Gtq/jz7g6885jLI4Xn3KPFc5JPnF1tlmSdOVTS16OWPMG0I/4DzJhYwd6eiJA5oYEo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1710239676; c=relaxed/simple; bh=m4PnEBFUNTnx84fAG11/MiRlWNZSJbIncWd/WAbUDoE=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=knTWbAc+0Dz30nH1pgAq0Gjsv+C+X8BqxqFVCuLIX6MTR+9yaAHxp/t5/NtDKDw1fbtN0PDmt+UwFLxTrV93Nr/0p+u/p9xPf2mpv+bgy+SlZlphPu2R7Hpm6gjzEIWueFN1boIBlFTVAQdEj2s0jfcD+Z2Np/QwtlccvqiBw0I= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.intel.com; spf=none smtp.mailfrom=linux.intel.com; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b=F2826sjN; arc=none smtp.client-ip=192.198.163.17 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.intel.com Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=linux.intel.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="F2826sjN" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1710239675; x=1741775675; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=m4PnEBFUNTnx84fAG11/MiRlWNZSJbIncWd/WAbUDoE=; b=F2826sjNPV9tkt1scilbMwc2joNnePg5CTzLL/YL90FHer2/fBHJHRlF IaxHel/PKPUssXIxf09aJnW4D+bY0Vb510xjIt79kMrv42aLItZQtQHF7 7wDZeb+v1vOXwuSPqa/ueZ99ZvVTuO6tubzkvIU7bvqxMKYuJVC0iNA2v H7c4H8OXgv08ASo6aGkoEIjZ1LJTs8ZOHdbLklhckpFR2agyH40oj95Z/ euTJ741UWgMsWatEOyCvVfXGmXsnO27VNCaKa1XxD0JI4D/7QGWbdr60y jTAhZsCc+GPgRSp2DfPrr3tF/8RmXB/qfxA/sLnDz+1WM1mECYNfjbU7z A==; X-IronPort-AV: E=McAfee;i="6600,9927,11010"; a="4794294" X-IronPort-AV: E=Sophos;i="6.07,119,1708416000"; d="scan'208";a="4794294" Received: from orviesa005.jf.intel.com ([10.64.159.145]) by fmvoesa111.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 12 Mar 2024 03:34:34 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.07,119,1708416000"; d="scan'208";a="16194085" Received: from turnipsi.fi.intel.com (HELO kekkonen.fi.intel.com) ([10.237.72.44]) by orviesa005-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 12 Mar 2024 03:34:33 -0700 Received: from svinhufvud.ger.corp.intel.com (localhost [IPv6:::1]) by kekkonen.fi.intel.com (Postfix) with ESMTP id 5C72511FB89; Tue, 12 Mar 2024 12:34:30 +0200 (EET) From: Sakari Ailus To: linux-media@vger.kernel.org Cc: laurent.pinchart@ideasonboard.com, hverkuil@xs4all.nl Subject: [PATCH v3 10/26] media: mc: Clear minor number reservation at unregistration time Date: Tue, 12 Mar 2024 12:34:06 +0200 Message-Id: <20240312103422.216484-11-sakari.ailus@linux.intel.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20240312103422.216484-1-sakari.ailus@linux.intel.com> References: <20240312103422.216484-1-sakari.ailus@linux.intel.com> Precedence: bulk X-Mailing-List: linux-media@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Clear the media device's minor number reservation at unregister time as there's no need to keep it reserved for longer. This makes it possible to reserve the same minor right after unregistration. Signed-off-by: Sakari Ailus --- drivers/media/mc/mc-devnode.c | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/drivers/media/mc/mc-devnode.c b/drivers/media/mc/mc-devnode.c index dbf546853ca9..2538371a90af 100644 --- a/drivers/media/mc/mc-devnode.c +++ b/drivers/media/mc/mc-devnode.c @@ -49,13 +49,6 @@ static void media_devnode_release(struct device *cd) { struct media_devnode *devnode = to_media_devnode(cd); - mutex_lock(&media_devnode_lock); - - /* Mark device node number as free */ - clear_bit(devnode->minor, media_devnode_nums); - - mutex_unlock(&media_devnode_lock); - /* Release media_devnode and perform other cleanups as needed. */ if (devnode->release) devnode->release(devnode); @@ -270,6 +263,10 @@ void media_devnode_unregister(struct media_devnode *devnode) cdev_del(&devnode->cdev); device_unregister(&devnode->dev); + + mutex_lock(&media_devnode_lock); + clear_bit(devnode->minor, media_devnode_nums); + mutex_unlock(&media_devnode_lock); } /* From patchwork Tue Mar 12 10:34:07 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sakari Ailus X-Patchwork-Id: 13589805 Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 6A22941A85 for ; Tue, 12 Mar 2024 10:34:35 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=192.198.163.17 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1710239677; cv=none; b=WRCEeKCvCPJCseHJc615fWSeRYta9reHcwFjxmyjQVwi5wmtoL+j5FJXl2zR99dYyiOkn8xF2udqpeG4SFXZA1ncDZP2PpjWnl7/Kosj7quPX2uua5bfr6ViOFvgVNZmyOfXy8vWxi8O/Po6xBf8oYwTIxm8hs6Wir666xaJAT0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1710239677; c=relaxed/simple; bh=NSa1+7HWptkDSUdoc8ftVBAjeDD2R4oqEQa5IQgXMmg=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=s+E2K/4NTeofz7PtLJJGsB8u0KlGqHrPS70jgpS9xWlA+7kcKPIeZs5wDIY1BFYyWWHVArAh09H8kktsEExCv0O8AvyvOuIGRiLTIsjLmlk0nZhd/dkDPQxZVMmNyZ8+5Pa39Cr9UO0IfMYy9DBwluxCuxx8Cn/gqQkIA+aWj8k= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.intel.com; spf=none smtp.mailfrom=linux.intel.com; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b=UYpV0Elb; arc=none smtp.client-ip=192.198.163.17 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.intel.com Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=linux.intel.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="UYpV0Elb" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1710239676; x=1741775676; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=NSa1+7HWptkDSUdoc8ftVBAjeDD2R4oqEQa5IQgXMmg=; b=UYpV0Elbl6hPGIUL3afKBQiCbr4BkAT8hzvO6gPGdgkpjxzXvINfrOc6 XnHsSkNVl9zAHC+Jda4tDZ9dyrJoF4Nu5nElvu1Fnegfrxml5onzQrrA4 AawgGwYVeQ3RCXekcrEKtG26/psCRlCnbsYtHZmq7Sy8gQ/ZtFfHpCIag FZkuuRB7iy8T3/McPZm0hGVvvsuXPB3UNHp/xKaSHHOJGTm3MbxD5isKc l58MQHmO2WII1my2/tTyvEGDKxCWppKpuRBgpdaAiO3cJKuvsggp88OC0 zWlQxujxnBNMU64YBFfVxWOUrTYRpWIoTdobR4yINOEO0kCjIBGi4y/kc w==; X-IronPort-AV: E=McAfee;i="6600,9927,11010"; a="4794298" X-IronPort-AV: E=Sophos;i="6.07,119,1708416000"; d="scan'208";a="4794298" Received: from orviesa005.jf.intel.com ([10.64.159.145]) by fmvoesa111.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 12 Mar 2024 03:34:35 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.07,119,1708416000"; d="scan'208";a="16194089" Received: from turnipsi.fi.intel.com (HELO kekkonen.fi.intel.com) ([10.237.72.44]) by orviesa005-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 12 Mar 2024 03:34:34 -0700 Received: from svinhufvud.ger.corp.intel.com (localhost [IPv6:::1]) by kekkonen.fi.intel.com (Postfix) with ESMTP id 0A1101201F7; Tue, 12 Mar 2024 12:34:30 +0200 (EET) From: Sakari Ailus To: linux-media@vger.kernel.org Cc: laurent.pinchart@ideasonboard.com, hverkuil@xs4all.nl Subject: [PATCH v3 11/26] media: mc: Split initialising and adding media devnode Date: Tue, 12 Mar 2024 12:34:07 +0200 Message-Id: <20240312103422.216484-12-sakari.ailus@linux.intel.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20240312103422.216484-1-sakari.ailus@linux.intel.com> References: <20240312103422.216484-1-sakari.ailus@linux.intel.com> Precedence: bulk X-Mailing-List: linux-media@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Registering a device node of an entity belonging to a media device will require a reference to the struct device. Taking that reference is only possible once the device has been initialised, which took place only when it was registered. Split this in two, and initialise the device when the media device is allocated. Don't propagate the effects of these changes to drivers yet, we want to expose media_device refcounting with media_device_get() and media_device_put() functions first. Signed-off-by: Sakari Ailus Acked-by: Hans Verkuil --- drivers/media/mc/mc-device.c | 18 +++++++++++++----- drivers/media/mc/mc-devnode.c | 18 ++++++++++-------- include/media/media-devnode.h | 19 ++++++++++++++----- 3 files changed, 37 insertions(+), 18 deletions(-) diff --git a/drivers/media/mc/mc-device.c b/drivers/media/mc/mc-device.c index c0ea08a8fc31..dd4d589a6701 100644 --- a/drivers/media/mc/mc-device.c +++ b/drivers/media/mc/mc-device.c @@ -717,19 +717,26 @@ int __must_check __media_device_register(struct media_device *mdev, /* Set version 0 to indicate user-space that the graph is static */ mdev->topology_version = 0; + media_devnode_init(&mdev->devnode); + ret = media_devnode_register(&mdev->devnode, owner); if (ret < 0) - return ret; + goto err_put; ret = device_create_file(&mdev->devnode.dev, &dev_attr_model); - if (ret < 0) { - media_devnode_unregister(&mdev->devnode); - return ret; - } + if (ret < 0) + goto err_unregister; dev_dbg(mdev->dev, "Media device registered\n"); return 0; + +err_unregister: + media_devnode_unregister(&mdev->devnode); +err_put: + put_device(&mdev->devnode.dev); + + return ret; } EXPORT_SYMBOL_GPL(__media_device_register); @@ -803,6 +810,7 @@ void media_device_unregister(struct media_device *mdev) device_remove_file(&mdev->devnode.dev, &dev_attr_model); dev_dbg(mdev->dev, "Media device unregistering\n"); media_devnode_unregister(&mdev->devnode); + put_device(&mdev->devnode.dev); } EXPORT_SYMBOL_GPL(media_device_unregister); diff --git a/drivers/media/mc/mc-devnode.c b/drivers/media/mc/mc-devnode.c index 2538371a90af..64aadc51352b 100644 --- a/drivers/media/mc/mc-devnode.c +++ b/drivers/media/mc/mc-devnode.c @@ -197,6 +197,11 @@ static const struct file_operations media_devnode_fops = { .llseek = no_llseek, }; +void media_devnode_init(struct media_devnode *devnode) +{ + device_initialize(&devnode->dev); +} + int __must_check media_devnode_register(struct media_devnode *devnode, struct module *owner) { @@ -228,7 +233,6 @@ int __must_check media_devnode_register(struct media_devnode *devnode, if (devnode->parent) devnode->dev.parent = devnode->parent; dev_set_name(&devnode->dev, "media%d", devnode->minor); - device_initialize(&devnode->dev); /* Part 3: Add the media and character devices */ ret = cdev_device_add(&devnode->cdev, &devnode->dev); @@ -246,7 +250,6 @@ int __must_check media_devnode_register(struct media_devnode *devnode, mutex_lock(&media_devnode_lock); clear_bit(devnode->minor, media_devnode_nums); mutex_unlock(&media_devnode_lock); - put_device(&devnode->dev); return ret; } @@ -261,8 +264,7 @@ void media_devnode_unregister(struct media_devnode *devnode) clear_bit(MEDIA_FLAG_REGISTERED, &devnode->flags); mutex_unlock(&media_devnode_lock); - cdev_del(&devnode->cdev); - device_unregister(&devnode->dev); + cdev_device_del(&devnode->cdev, &devnode->dev); mutex_lock(&media_devnode_lock); clear_bit(devnode->minor, media_devnode_nums); @@ -272,7 +274,7 @@ void media_devnode_unregister(struct media_devnode *devnode) /* * Initialise media for linux */ -static int __init media_devnode_init(void) +static int __init media_devnode_module_init(void) { int ret; @@ -294,14 +296,14 @@ static int __init media_devnode_init(void) return 0; } -static void __exit media_devnode_exit(void) +static void __exit media_devnode_module_exit(void) { bus_unregister(&media_bus_type); unregister_chrdev_region(media_dev_t, MEDIA_NUM_DEVICES); } -subsys_initcall(media_devnode_init); -module_exit(media_devnode_exit) +subsys_initcall(media_devnode_module_init); +module_exit(media_devnode_module_exit) MODULE_AUTHOR("Laurent Pinchart "); MODULE_DESCRIPTION("Device node registration for media drivers"); diff --git a/include/media/media-devnode.h b/include/media/media-devnode.h index 024dfb98a6fd..113c317e6a0e 100644 --- a/include/media/media-devnode.h +++ b/include/media/media-devnode.h @@ -89,6 +89,17 @@ struct media_devnode { /* dev to media_devnode */ #define to_media_devnode(cd) container_of(cd, struct media_devnode, dev) +/** + * media_devnode_init - initialise a media devnode + * + * @devnode: struct media_devnode we want to initialise + * + * Initialise a media devnode. Note that after initialising the media + * devnode is refcounted. Releasing references to it may be done using + * put_device(). + */ +void media_devnode_init(struct media_devnode *devnode); + /** * media_devnode_register - register a media device node * @@ -99,11 +110,9 @@ struct media_devnode { * with the kernel. An error is returned if no free minor number can be found, * or if the registration of the device node fails. * - * Zero is returned on success. - * - * Note that if the media_devnode_register call fails, the release() callback of - * the media_devnode structure is *not* called, so the caller is responsible for - * freeing any data. + * Zero is returned on success. Note that in case + * media_devnode_register() fails, the caller is responsible for + * releasing the reference to the device using put_device(). */ int __must_check media_devnode_register(struct media_devnode *devnode, struct module *owner); From patchwork Tue Mar 12 10:34:08 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sakari Ailus X-Patchwork-Id: 13589806 Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 363EE79B66 for ; Tue, 12 Mar 2024 10:34:36 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=192.198.163.17 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1710239677; cv=none; b=DwKnLVMrqszhIyvXV5+Ha7mzykZpz1iKdewKnYFAva4xJ464xsAn2eGg3k0bbAgGp9yavmUsRty4e0Bmo1gzZeDbcLZUqvcsS3EqCUVg76z8dKuDIJuITv6JBHs+uCPsxP7CtYg0N/BlSN+rTjotCNIjzkfEf4GIQGZ5iFfVRNs= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1710239677; c=relaxed/simple; bh=tM6zZKGkriDI1MbccPv6yeW0LtSVx/9DN3hcT1x3INw=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=c5gyLFX38ZB0i+4Ig6ugm+eZVvMdJmXLoY6l9uOMQx61MW8vupx9IcsKdPSO6eamROeqNAMuq+rBZYuoN4gkRJcp/IGkkO1r0JxqADgeWjqOOlS+oA3sFEfGrJJfri+w0HTgUS7LvzZThXvIUBuTSu4L8086zEpuQXnkme7svHE= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.intel.com; spf=none smtp.mailfrom=linux.intel.com; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b=dwkFRzDx; arc=none smtp.client-ip=192.198.163.17 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.intel.com Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=linux.intel.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="dwkFRzDx" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1710239676; x=1741775676; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=tM6zZKGkriDI1MbccPv6yeW0LtSVx/9DN3hcT1x3INw=; b=dwkFRzDxk9feiE15n57IJg8szCQiK5ay9VZPW6xLa7h38arjQ/iAhuQO JopTk9bIYZd8OlxRWNdwNFaZDkhbG9Mru0XMDBMxg5o/oBo+xg/4hJ27J 0Zl7t/ucV5lyfv8XJWa5P1+2IlBEZPxvbXfgw/oheEiTVjuMxACmIn6aU wXaEhwAfzIO5LqqYhVCCK3cgvIYNB2FyHcC8WtNf0K8f9PxTGDVt651bJ QGRPdPAgUmbjAjLE/u87tJyfc/qNZqvlYWMHIXsBh3NsVkKe6g4gUN4i7 0Fi77gFnoic9g8UU7GREQioDtkTHHEcKRImO169Ep1Fe5kZ6yQtRBHQzp A==; X-IronPort-AV: E=McAfee;i="6600,9927,11010"; a="4794301" X-IronPort-AV: E=Sophos;i="6.07,119,1708416000"; d="scan'208";a="4794301" Received: from orviesa005.jf.intel.com ([10.64.159.145]) by fmvoesa111.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 12 Mar 2024 03:34:35 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.07,119,1708416000"; d="scan'208";a="16194090" Received: from turnipsi.fi.intel.com (HELO kekkonen.fi.intel.com) ([10.237.72.44]) by orviesa005-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 12 Mar 2024 03:34:35 -0700 Received: from svinhufvud.ger.corp.intel.com (localhost [IPv6:::1]) by kekkonen.fi.intel.com (Postfix) with ESMTP id B4B9C11F819; Tue, 12 Mar 2024 12:34:31 +0200 (EET) From: Sakari Ailus To: linux-media@vger.kernel.org Cc: laurent.pinchart@ideasonboard.com, hverkuil@xs4all.nl Subject: [PATCH v3 12/26] media: mc: Shuffle functions around Date: Tue, 12 Mar 2024 12:34:08 +0200 Message-Id: <20240312103422.216484-13-sakari.ailus@linux.intel.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20240312103422.216484-1-sakari.ailus@linux.intel.com> References: <20240312103422.216484-1-sakari.ailus@linux.intel.com> Precedence: bulk X-Mailing-List: linux-media@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 As the call paths of the functions in question will change, move them around in anticipation of that. No other changes. Signed-off-by: Sakari Ailus Acked-by: Hans Verkuil Reviewed-by: Laurent Pinchart --- drivers/media/mc/mc-device.c | 54 ++++++++++++++++++------------------ 1 file changed, 27 insertions(+), 27 deletions(-) diff --git a/drivers/media/mc/mc-device.c b/drivers/media/mc/mc-device.c index dd4d589a6701..f1f3addf7932 100644 --- a/drivers/media/mc/mc-device.c +++ b/drivers/media/mc/mc-device.c @@ -673,6 +673,33 @@ void media_device_unregister_entity(struct media_entity *entity) } EXPORT_SYMBOL_GPL(media_device_unregister_entity); +void media_device_register_entity_notify(struct media_device *mdev, + struct media_entity_notify *nptr) +{ + mutex_lock(&mdev->graph_mutex); + list_add_tail(&nptr->list, &mdev->entity_notify); + mutex_unlock(&mdev->graph_mutex); +} +EXPORT_SYMBOL_GPL(media_device_register_entity_notify); + +/* + * Note: Should be called with mdev->lock held. + */ +static void __media_device_unregister_entity_notify(struct media_device *mdev, + struct media_entity_notify *nptr) +{ + list_del(&nptr->list); +} + +void media_device_unregister_entity_notify(struct media_device *mdev, + struct media_entity_notify *nptr) +{ + mutex_lock(&mdev->graph_mutex); + __media_device_unregister_entity_notify(mdev, nptr); + mutex_unlock(&mdev->graph_mutex); +} +EXPORT_SYMBOL_GPL(media_device_unregister_entity_notify); + void media_device_init(struct media_device *mdev) { INIT_LIST_HEAD(&mdev->entities); @@ -740,33 +767,6 @@ int __must_check __media_device_register(struct media_device *mdev, } EXPORT_SYMBOL_GPL(__media_device_register); -void media_device_register_entity_notify(struct media_device *mdev, - struct media_entity_notify *nptr) -{ - mutex_lock(&mdev->graph_mutex); - list_add_tail(&nptr->list, &mdev->entity_notify); - mutex_unlock(&mdev->graph_mutex); -} -EXPORT_SYMBOL_GPL(media_device_register_entity_notify); - -/* - * Note: Should be called with mdev->lock held. - */ -static void __media_device_unregister_entity_notify(struct media_device *mdev, - struct media_entity_notify *nptr) -{ - list_del(&nptr->list); -} - -void media_device_unregister_entity_notify(struct media_device *mdev, - struct media_entity_notify *nptr) -{ - mutex_lock(&mdev->graph_mutex); - __media_device_unregister_entity_notify(mdev, nptr); - mutex_unlock(&mdev->graph_mutex); -} -EXPORT_SYMBOL_GPL(media_device_unregister_entity_notify); - void media_device_unregister(struct media_device *mdev) { struct media_entity *entity; From patchwork Tue Mar 12 10:34:09 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sakari Ailus X-Patchwork-Id: 13589807 Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 8C72079B76 for ; Tue, 12 Mar 2024 10:34:36 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=192.198.163.17 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1710239678; cv=none; b=osaIjTXXTcmp86KvkHIbK/WEaCewqPciGzAbP4nzOi2NNFdJDD6lsZhzG5xZdEdxeh8Qsgo26YZp9ZmL8Gqyj9S7gmm5Se4lNkfI9Idnyk4+Pi/MA3Yy4ZaAGBil4jFX/uSkpNLV7UMWJx6oySyjxJj3Yu1z+HuTs1d4TVVHthU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1710239678; c=relaxed/simple; bh=II6lz7idmJWX19uPmLqcUf7Hfulwjv5ymsaQBoCu7Zc=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=UCqCKTJvuk3VyrThfYsP6r/zwdOALdcS23n9Ja9YUZG4y87YAFbhrtFagAR7PEQM34FmpUGBZoQ2EMLlgumYYhQOIMaBws6+RuqhT7bKQ5NCaYPHlVEQCkW7UFCt9LxBvJ5s8CAZ/f/JMjgf+ZAG8Dt11rfRunlCMLCJuQhIrM4= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.intel.com; spf=none smtp.mailfrom=linux.intel.com; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b=n3fIxNxg; arc=none smtp.client-ip=192.198.163.17 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.intel.com Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=linux.intel.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="n3fIxNxg" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1710239677; x=1741775677; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=II6lz7idmJWX19uPmLqcUf7Hfulwjv5ymsaQBoCu7Zc=; b=n3fIxNxgLqFxE7jmMoIpd8r/wJKnGWAEmi6jzoqRiMpfFh0KCgBOig/G LKR01qW73gmXwWOnF2tPI0QxWKizGsl2TYJLwG8aLkGf6YP9pibiUSo2F BOXMCNWgX/eWwpL5SdnZKMXkOx4Dw0lYwYG/T9SWVESrK+R6ZsVOzzFxM mRJX+HwebMEFInM9wowyI9XqnJ8AR5gYwPAV9H2Qu5p0EVBbtv/s4RJDF NzNHqUN6yynyDd3YCv1AbtA3TSZIz0OYm0wM0L+1e4Xq/5BLIJifPs/gY dbDsuoYRdOwQaWYFnWswFI1zQrvYNme8juDpnPn8DP8wib75cX45seCRY w==; X-IronPort-AV: E=McAfee;i="6600,9927,11010"; a="4794305" X-IronPort-AV: E=Sophos;i="6.07,119,1708416000"; d="scan'208";a="4794305" Received: from orviesa005.jf.intel.com ([10.64.159.145]) by fmvoesa111.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 12 Mar 2024 03:34:36 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.07,119,1708416000"; d="scan'208";a="16194092" Received: from turnipsi.fi.intel.com (HELO kekkonen.fi.intel.com) ([10.237.72.44]) by orviesa005-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 12 Mar 2024 03:34:35 -0700 Received: from svinhufvud.ger.corp.intel.com (localhost [IPv6:::1]) by kekkonen.fi.intel.com (Postfix) with ESMTP id 773D811F81D; Tue, 12 Mar 2024 12:34:32 +0200 (EET) From: Sakari Ailus To: linux-media@vger.kernel.org Cc: laurent.pinchart@ideasonboard.com, hverkuil@xs4all.nl Subject: [PATCH v3 13/26] media: mc: Initialise media devnode in media_device_init() Date: Tue, 12 Mar 2024 12:34:09 +0200 Message-Id: <20240312103422.216484-14-sakari.ailus@linux.intel.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20240312103422.216484-1-sakari.ailus@linux.intel.com> References: <20240312103422.216484-1-sakari.ailus@linux.intel.com> Precedence: bulk X-Mailing-List: linux-media@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Call media_devnode_init() from media_device_init(). This has the effect of creating a struct device for the media_devnode before it is registered, making it possible to obtain a reference to it for e.g. video devices. Signed-off-by: Sakari Ailus Acked-by: Hans Verkuil Reviewed-by: Laurent Pinchart --- drivers/media/mc/mc-device.c | 22 ++++++++-------------- 1 file changed, 8 insertions(+), 14 deletions(-) diff --git a/drivers/media/mc/mc-device.c b/drivers/media/mc/mc-device.c index f1f3addf7932..f1d89d940fe1 100644 --- a/drivers/media/mc/mc-device.c +++ b/drivers/media/mc/mc-device.c @@ -711,9 +711,10 @@ void media_device_init(struct media_device *mdev) mutex_init(&mdev->req_queue_mutex); mutex_init(&mdev->graph_mutex); ida_init(&mdev->entity_internal_idx); - atomic_set(&mdev->request_id, 0); + media_devnode_init(&mdev->devnode); + if (!*mdev->bus_info) media_set_bus_info(mdev->bus_info, sizeof(mdev->bus_info), mdev->dev); @@ -729,6 +730,7 @@ void media_device_cleanup(struct media_device *mdev) media_graph_walk_cleanup(&mdev->pm_count_walk); mutex_destroy(&mdev->graph_mutex); mutex_destroy(&mdev->req_queue_mutex); + put_device(&mdev->devnode.dev); } EXPORT_SYMBOL_GPL(media_device_cleanup); @@ -744,26 +746,19 @@ int __must_check __media_device_register(struct media_device *mdev, /* Set version 0 to indicate user-space that the graph is static */ mdev->topology_version = 0; - media_devnode_init(&mdev->devnode); - ret = media_devnode_register(&mdev->devnode, owner); if (ret < 0) - goto err_put; + return ret; ret = device_create_file(&mdev->devnode.dev, &dev_attr_model); - if (ret < 0) - goto err_unregister; + if (ret < 0) { + media_devnode_unregister(&mdev->devnode); + return ret; + } dev_dbg(mdev->dev, "Media device registered\n"); return 0; - -err_unregister: - media_devnode_unregister(&mdev->devnode); -err_put: - put_device(&mdev->devnode.dev); - - return ret; } EXPORT_SYMBOL_GPL(__media_device_register); @@ -810,7 +805,6 @@ void media_device_unregister(struct media_device *mdev) device_remove_file(&mdev->devnode.dev, &dev_attr_model); dev_dbg(mdev->dev, "Media device unregistering\n"); media_devnode_unregister(&mdev->devnode); - put_device(&mdev->devnode.dev); } EXPORT_SYMBOL_GPL(media_device_unregister); From patchwork Tue Mar 12 10:34:10 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sakari Ailus X-Patchwork-Id: 13589810 Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 23BB779B75 for ; Tue, 12 Mar 2024 10:34:37 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=192.198.163.17 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1710239680; cv=none; b=q6egXGhvKAzsUJkJbLbZ8opdxeTkmqCn2kuRS4LFfSF8K7wR2pgWl5qr6l5Ek6W+LsXa7YDNO5qQ+TygvrzrK73P4SpPZt38hXnNmZde61rg1mw/agd9WaEQkDPskerOEXz2wREFDn0XzggpiYC7LmL1xeEjIXVxU0GXnGRh1yQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1710239680; c=relaxed/simple; bh=gHkn3+4ra63kv16zVeXU2Mj2qs4V5THT9bHKSfu2byc=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=r2Avqk6nbN0wDW+qoBiVRQxIYzaHR3peyw8PV7Vd7qTPXhabGmvbaqQH3KzstEaJJhamw8oHVLeFZKTFzc7IyRmx9IpV+iZLKl1Mcb8q1QNhs+k16d7v4QutAndawsx3Sv/ASK3vJTdbLTXGkvCfPtbMHouQIgMbOpQTQAVkpyQ= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.intel.com; spf=none smtp.mailfrom=linux.intel.com; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b=f58rlD6B; arc=none smtp.client-ip=192.198.163.17 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.intel.com Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=linux.intel.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="f58rlD6B" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1710239677; x=1741775677; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=gHkn3+4ra63kv16zVeXU2Mj2qs4V5THT9bHKSfu2byc=; b=f58rlD6B219iJwq9l+OLQGhil3v700ONkz3ngU5WgiSLi2K/GKZoDgii p7PZrmwg5GUA+3ekbeXQulpxtTS961tr9n80oCbLRDpRfCvOsoTXHI840 1VpDudv5TFeKnDP63gYmRQSM2karNliawColu5R88Mgs0NVuqtzd4qioQ YMeSCoLrNYcNTpuVPmzcmQuCtwjbyLeJFnemROfugFz0qp35eo8sLHqjb wPPzFha0H3pB4SUmiYX9TUZZ2t3IzhodDdcWLSkH0mux1MB7kaEBdERzc 77aXagpaYajSvAlN4uZPbpEW4VqC1ufTBH3gV/IJ6g00KEpGrh6HbBENP g==; X-IronPort-AV: E=McAfee;i="6600,9927,11010"; a="4794309" X-IronPort-AV: E=Sophos;i="6.07,119,1708416000"; d="scan'208";a="4794309" Received: from orviesa005.jf.intel.com ([10.64.159.145]) by fmvoesa111.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 12 Mar 2024 03:34:37 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.07,119,1708416000"; d="scan'208";a="16194094" Received: from turnipsi.fi.intel.com (HELO kekkonen.fi.intel.com) ([10.237.72.44]) by orviesa005-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 12 Mar 2024 03:34:36 -0700 Received: from svinhufvud.ger.corp.intel.com (localhost [IPv6:::1]) by kekkonen.fi.intel.com (Postfix) with ESMTP id 0EA6A11FB11; Tue, 12 Mar 2024 12:34:32 +0200 (EET) From: Sakari Ailus To: linux-media@vger.kernel.org Cc: laurent.pinchart@ideasonboard.com, hverkuil@xs4all.nl Subject: [PATCH v3 14/26] media: mc: Refcount the media device Date: Tue, 12 Mar 2024 12:34:10 +0200 Message-Id: <20240312103422.216484-15-sakari.ailus@linux.intel.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20240312103422.216484-1-sakari.ailus@linux.intel.com> References: <20240312103422.216484-1-sakari.ailus@linux.intel.com> Precedence: bulk X-Mailing-List: linux-media@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 As the struct media_device embeds struct media_devnode, the lifetime of that object must be that same than that of the media_device. References are obtained by media_device_get() and released by media_device_put(). In order to use refcounting, the driver must set the release callback before calling media_device_init() on the media device. Signed-off-by: Sakari Ailus Acked-by: Hans Verkuil --- drivers/media/mc/mc-device.c | 35 +++++++++++++++++++++++++++++------ drivers/media/mc/mc-devnode.c | 2 +- include/media/media-device.h | 35 +++++++++++++++++++++++++++++++++++ 3 files changed, 65 insertions(+), 7 deletions(-) diff --git a/drivers/media/mc/mc-device.c b/drivers/media/mc/mc-device.c index f1d89d940fe1..bbc233e726d2 100644 --- a/drivers/media/mc/mc-device.c +++ b/drivers/media/mc/mc-device.c @@ -700,6 +700,31 @@ void media_device_unregister_entity_notify(struct media_device *mdev, } EXPORT_SYMBOL_GPL(media_device_unregister_entity_notify); +static void __media_device_release(struct media_device *mdev) +{ + dev_dbg(mdev->dev, "Media device released\n"); + + ida_destroy(&mdev->entity_internal_idx); + mdev->entity_internal_idx_max = 0; + media_graph_walk_cleanup(&mdev->pm_count_walk); + mutex_destroy(&mdev->graph_mutex); + mutex_destroy(&mdev->req_queue_mutex); +} + +static void media_device_release(struct media_devnode *devnode) +{ + struct media_device *mdev = to_media_device(devnode); + + if (mdev->ops && mdev->ops->release) { + /* + * If release op isn't set, __media_device_release() is called + * via media_device_cleanup(). + */ + __media_device_release(mdev); + mdev->ops->release(mdev); + } +} + void media_device_init(struct media_device *mdev) { INIT_LIST_HEAD(&mdev->entities); @@ -713,6 +738,7 @@ void media_device_init(struct media_device *mdev) ida_init(&mdev->entity_internal_idx); atomic_set(&mdev->request_id, 0); + mdev->devnode.release = media_device_release; media_devnode_init(&mdev->devnode); if (!*mdev->bus_info) @@ -725,12 +751,9 @@ EXPORT_SYMBOL_GPL(media_device_init); void media_device_cleanup(struct media_device *mdev) { - ida_destroy(&mdev->entity_internal_idx); - mdev->entity_internal_idx_max = 0; - media_graph_walk_cleanup(&mdev->pm_count_walk); - mutex_destroy(&mdev->graph_mutex); - mutex_destroy(&mdev->req_queue_mutex); - put_device(&mdev->devnode.dev); + WARN_ON(mdev->ops && mdev->ops->release); + __media_device_release(mdev); + media_device_put(mdev); } EXPORT_SYMBOL_GPL(media_device_cleanup); diff --git a/drivers/media/mc/mc-devnode.c b/drivers/media/mc/mc-devnode.c index 64aadc51352b..6fb3a202eb26 100644 --- a/drivers/media/mc/mc-devnode.c +++ b/drivers/media/mc/mc-devnode.c @@ -200,6 +200,7 @@ static const struct file_operations media_devnode_fops = { void media_devnode_init(struct media_devnode *devnode) { device_initialize(&devnode->dev); + devnode->dev.release = media_devnode_release; } int __must_check media_devnode_register(struct media_devnode *devnode, @@ -229,7 +230,6 @@ int __must_check media_devnode_register(struct media_devnode *devnode, devnode->dev.bus = &media_bus_type; devnode->dev.devt = MKDEV(MAJOR(media_dev_t), devnode->minor); - devnode->dev.release = media_devnode_release; if (devnode->parent) devnode->dev.parent = devnode->parent; dev_set_name(&devnode->dev, "media%d", devnode->minor); diff --git a/include/media/media-device.h b/include/media/media-device.h index c791f3d5ad77..f1afbfc4dca2 100644 --- a/include/media/media-device.h +++ b/include/media/media-device.h @@ -62,6 +62,7 @@ struct media_entity_notify { * request (and thus the buffer) must be available to the driver. * And once a buffer is queued, then the driver can complete * or delete objects from the request before req_queue exits. + * @release: Release the resources of the media device. */ struct media_device_ops { int (*link_notify)(struct media_link *link, u32 flags, @@ -70,6 +71,7 @@ struct media_device_ops { void (*req_free)(struct media_request *req); int (*req_validate)(struct media_request *req); void (*req_queue)(struct media_request *req); + void (*release)(struct media_device *mdev); }; /** @@ -219,6 +221,32 @@ struct usb_device; */ void media_device_init(struct media_device *mdev); +/** + * media_device_get() - atomically increment the reference count for the media + * device + * + * Returns: the media device + * + * @mdev: media device + */ +static inline struct media_device *media_device_get(struct media_device *mdev) +{ + get_device(&mdev->devnode.dev); + + return mdev; +} + +/** + * media_device_put() - atomically decrement the reference count for the media + * device + * + * @mdev: media device + */ +static inline void media_device_put(struct media_device *mdev) +{ + put_device(&mdev->devnode.dev); +} + /** * media_device_cleanup() - Cleanups a media device element * @@ -435,6 +463,13 @@ void __media_device_usb_init(struct media_device *mdev, static inline void media_device_init(struct media_device *mdev) { } +static inline struct media_device *media_device_get(struct media_device *mdev) +{ + return NULL; +} +static inline void media_device_put(struct media_device *mdev) +{ +} static inline int media_device_register(struct media_device *mdev) { return 0; From patchwork Tue Mar 12 10:34:11 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sakari Ailus X-Patchwork-Id: 13589808 Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id D7F4779B85 for ; Tue, 12 Mar 2024 10:34:37 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=192.198.163.17 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1710239679; cv=none; b=ICimNKvCTPBk01G5pEP9DGqjNRMSBzt/wAdAzJL7DJaZJ4J5hkWTmR4Nh6vhWIKXss/98SYMVBlcC1LoEY4tAf9DgPVGeH31vEDZZtBENXCC/v1wWmV0Ze0PN48lm4NaQj5BpIuse377iwrDOVfaasx7epdYvLpm457dVVNht2Y= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1710239679; c=relaxed/simple; bh=tZamopol//gjvxe0Jk3bDem0g/gPKnP+1lObBx5EELg=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=IbASJPFYuD/LPZiAF9oviKv5xI4tcaUbmcGekT2qFip1+BM8ZswqOJFsLaZXe9vgmxmNP5CA5lbqdhf5LqutA9zmqNDf7umTlMEPURzQPNihdb9HiHYo4g7s9cCH+chChdMnlzw1AYU3Ts3y7AwxyAgDLalzHOODK/wWruZ3+BA= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.intel.com; spf=none smtp.mailfrom=linux.intel.com; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b=UYuUANrW; arc=none smtp.client-ip=192.198.163.17 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.intel.com Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=linux.intel.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="UYuUANrW" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1710239678; x=1741775678; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=tZamopol//gjvxe0Jk3bDem0g/gPKnP+1lObBx5EELg=; b=UYuUANrWSmWUxAUTkbEDdA6D8a2g40TSLcw207Ap42P9wsS6PBWm3/MH t0Pqa2neQpk5na90ocFgQLd8GzFvplwaU2ZjH1eS30Y18Kk87tkX+bCuJ DwvR9t1Fthyv6fTNhFgPDSyIRNYLx2+0SxrQivnXQY1d6nDrhfWuAt0RO ZVZl/6r+QtLh52DQ2Hg7YWzhGyPmIBi9LmGQEHOlLv1VW1UZsd9QjlBod U1mOP54lLAkyCJEN/t39xIr4v+apidZ80DuBFTiD15o72LEymA/RWZ2aH 3yHm4tcddL92lAHquntxidgUMx+QnuG70Y58ntzKvzqXeGvnDEU2nKJNG A==; X-IronPort-AV: E=McAfee;i="6600,9927,11010"; a="4794314" X-IronPort-AV: E=Sophos;i="6.07,119,1708416000"; d="scan'208";a="4794314" Received: from orviesa005.jf.intel.com ([10.64.159.145]) by fmvoesa111.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 12 Mar 2024 03:34:38 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.07,119,1708416000"; d="scan'208";a="16194095" Received: from turnipsi.fi.intel.com (HELO kekkonen.fi.intel.com) ([10.237.72.44]) by orviesa005-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 12 Mar 2024 03:34:36 -0700 Received: from svinhufvud.ger.corp.intel.com (localhost [IPv6:::1]) by kekkonen.fi.intel.com (Postfix) with ESMTP id B79B6120619; Tue, 12 Mar 2024 12:34:33 +0200 (EET) From: Sakari Ailus To: linux-media@vger.kernel.org Cc: laurent.pinchart@ideasonboard.com, hverkuil@xs4all.nl Subject: [PATCH v3 15/26] media: v4l: Acquire a reference to the media device for every video device Date: Tue, 12 Mar 2024 12:34:11 +0200 Message-Id: <20240312103422.216484-16-sakari.ailus@linux.intel.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20240312103422.216484-1-sakari.ailus@linux.intel.com> References: <20240312103422.216484-1-sakari.ailus@linux.intel.com> Precedence: bulk X-Mailing-List: linux-media@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 The video device depends on the existence of its media device --- if there is one. Acquire a reference to it. Note that when the media device release callback is used, then the V4L2 device release callback is ignored and a warning is issued if both are set. Signed-off-by: Sakari Ailus --- drivers/media/v4l2-core/v4l2-dev.c | 50 +++++++++++++++++++----------- 1 file changed, 32 insertions(+), 18 deletions(-) diff --git a/drivers/media/v4l2-core/v4l2-dev.c b/drivers/media/v4l2-core/v4l2-dev.c index d13954bd31fd..ba0bfcbe7136 100644 --- a/drivers/media/v4l2-core/v4l2-dev.c +++ b/drivers/media/v4l2-core/v4l2-dev.c @@ -176,6 +176,11 @@ static void v4l2_device_release(struct device *cd) { struct video_device *vdev = to_video_device(cd); struct v4l2_device *v4l2_dev = vdev->v4l2_dev; + bool v4l2_dev_call_release = v4l2_dev->release; +#ifdef CONFIG_MEDIA_CONTROLLER + struct media_device *mdev = v4l2_dev->mdev; + bool mdev_has_release = mdev && mdev->ops && mdev->ops->release; +#endif mutex_lock(&videodev_lock); if (WARN_ON(video_devices[vdev->minor] != vdev)) { @@ -198,8 +203,8 @@ static void v4l2_device_release(struct device *cd) mutex_unlock(&videodev_lock); -#if defined(CONFIG_MEDIA_CONTROLLER) - if (v4l2_dev->mdev && vdev->vfl_dir != VFL_DIR_M2M) { +#ifdef CONFIG_MEDIA_CONTROLLER + if (mdev && vdev->vfl_dir != VFL_DIR_M2M) { /* Remove interfaces and interface links */ media_devnode_remove(vdev->intf_devnode); if (vdev->entity.function != MEDIA_ENT_F_UNKNOWN) @@ -207,23 +212,28 @@ static void v4l2_device_release(struct device *cd) } #endif - /* Do not call v4l2_device_put if there is no release callback set. - * Drivers that have no v4l2_device release callback might free the - * v4l2_dev instance in the video_device release callback below, so we - * must perform this check here. - * - * TODO: In the long run all drivers that use v4l2_device should use the - * v4l2_device release callback. This check will then be unnecessary. - */ - if (v4l2_dev->release == NULL) - v4l2_dev = NULL; - /* Release video_device and perform other cleanups as needed. */ vdev->release(vdev); - /* Decrease v4l2_device refcount */ - if (v4l2_dev) +#ifdef CONFIG_MEDIA_CONTROLLER + if (mdev) + media_device_put(mdev); + + /* + * Generally both struct media_device and struct v4l2_device are + * embedded in the same driver's context struct so having a release + * callback in both is a bug. + */ + if (WARN_ON(v4l2_dev_call_release && mdev_has_release)) + v4l2_dev_call_release = false; +#endif + + /* + * Decrease v4l2_device refcount, but only if the media device doesn't + * have a release callback. + */ + if (v4l2_dev_call_release) v4l2_device_put(v4l2_dev); } @@ -792,11 +802,14 @@ static int video_register_media_controller(struct video_device *vdev) u32 intf_type; int ret; - /* Memory-to-memory devices are more complex and use - * their own function to register its mc entities. + /* + * Memory-to-memory devices are more complex and use their own function + * to register its mc entities. */ - if (!vdev->v4l2_dev->mdev || vdev->vfl_dir == VFL_DIR_M2M) + if (!vdev->v4l2_dev->mdev || vdev->vfl_dir == VFL_DIR_M2M) { + media_device_get(vdev->v4l2_dev->mdev); return 0; + } vdev->entity.obj_type = MEDIA_ENTITY_TYPE_VIDEO_DEVICE; vdev->entity.function = MEDIA_ENT_F_UNKNOWN; @@ -875,6 +888,7 @@ static int video_register_media_controller(struct video_device *vdev) /* FIXME: how to create the other interface links? */ + media_device_get(vdev->v4l2_dev->mdev); #endif return 0; } From patchwork Tue Mar 12 10:34:12 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sakari Ailus X-Patchwork-Id: 13589809 Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 2CEDB79B8C for ; Tue, 12 Mar 2024 10:34:38 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=192.198.163.17 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1710239679; cv=none; b=aJPcc0wjVGbXKKXFy1nhCnUQpiGULarqKzlwRXGyDFq7SeJs7Zt1tc7LLKK5BOggnEr76KiGlH45gn3QmLTwmOeQhO35f36bad47Asq60xq9ZoX1Qyd6wAssqrJ+1hurm+hcYHULdECNooTXcZ1SwfaQgAEhw268tXOVaZ6tVXo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1710239679; c=relaxed/simple; bh=b8A0zAtDgHgdySNbkiiN2Py2hl+u3/NaWACsnHEcZGk=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=I7k7HLKrBI5JDMSmTPfoKwVmd7Ed48mc0LdP1m9T38Gv5eR2xA+qKltoNBfWTrAXzD2yKZPL+OXKoMtMmRwvCJZNl02m5lJR4S8iBzygYitwZ+OCR7fDgrXfjNqCT5eWtq14lyLyFqFbRbDa8qseZMickM9BZeg6EkyHTdnIwOc= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.intel.com; spf=none smtp.mailfrom=linux.intel.com; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b=C4BBzjYn; arc=none smtp.client-ip=192.198.163.17 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.intel.com Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=linux.intel.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="C4BBzjYn" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1710239678; x=1741775678; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=b8A0zAtDgHgdySNbkiiN2Py2hl+u3/NaWACsnHEcZGk=; b=C4BBzjYn/UQiZTj9M7rdE6ao0UysAwnOfGYt5ac8Vo31EB8C1qmF8hSt Jd9zkdGo8DMYvp+J4uSa3SiapEsjciGbNbtXEfclDthKjKUbx7WlnOw2a OiD/ZApg0uup5H1kROY+aTqcA/MNP5cIraaJJRFzMTVymd5NJyQ8dtwu7 9NDyKy/BeBMQleSv0TnzalPco9tbWEtYPT0wfgkjmRR86DQDQ6uRJeq9w c3m3pFZp0XIrGAsQPv/syfAW1fAi9jnmtfwyFM+XKsbPEa9pta2q9e/n+ aISiwWJwo3QW9O8oI5000Pi3fJ2i1tVD9y0O1X/REUuKWiZZSSpBqBCwM A==; X-IronPort-AV: E=McAfee;i="6600,9927,11010"; a="4794318" X-IronPort-AV: E=Sophos;i="6.07,119,1708416000"; d="scan'208";a="4794318" Received: from orviesa005.jf.intel.com ([10.64.159.145]) by fmvoesa111.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 12 Mar 2024 03:34:38 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.07,119,1708416000"; d="scan'208";a="16194096" Received: from turnipsi.fi.intel.com (HELO kekkonen.fi.intel.com) ([10.237.72.44]) by orviesa005-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 12 Mar 2024 03:34:37 -0700 Received: from svinhufvud.ger.corp.intel.com (localhost [IPv6:::1]) by kekkonen.fi.intel.com (Postfix) with ESMTP id 4069111FB89; Tue, 12 Mar 2024 12:34:34 +0200 (EET) From: Sakari Ailus To: linux-media@vger.kernel.org Cc: laurent.pinchart@ideasonboard.com, hverkuil@xs4all.nl Subject: [PATCH v3 16/26] media: mc: Postpone graph object removal until free Date: Tue, 12 Mar 2024 12:34:12 +0200 Message-Id: <20240312103422.216484-17-sakari.ailus@linux.intel.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20240312103422.216484-1-sakari.ailus@linux.intel.com> References: <20240312103422.216484-1-sakari.ailus@linux.intel.com> Precedence: bulk X-Mailing-List: linux-media@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 The media device itself will be unregistered based on it being unbound and driver's remove callback being called. The graph objects themselves may still be in use; rely on the media device release callback to release them. Signed-off-by: Sakari Ailus Acked-by: Hans Verkuil --- drivers/media/mc/mc-device.c | 53 +++++++++++++++++------------------- 1 file changed, 25 insertions(+), 28 deletions(-) diff --git a/drivers/media/mc/mc-device.c b/drivers/media/mc/mc-device.c index bbc233e726d2..10426c2796b6 100644 --- a/drivers/media/mc/mc-device.c +++ b/drivers/media/mc/mc-device.c @@ -702,8 +702,33 @@ EXPORT_SYMBOL_GPL(media_device_unregister_entity_notify); static void __media_device_release(struct media_device *mdev) { + struct media_entity *entity; + struct media_entity *next; + struct media_interface *intf, *tmp_intf; + struct media_entity_notify *notify, *nextp; + dev_dbg(mdev->dev, "Media device released\n"); + /* Remove all entities from the media device */ + list_for_each_entry_safe(entity, next, &mdev->entities, graph_obj.list) + __media_device_unregister_entity(entity); + + /* Remove all entity_notify callbacks from the media device */ + list_for_each_entry_safe(notify, nextp, &mdev->entity_notify, list) + __media_device_unregister_entity_notify(mdev, notify); + + /* Remove all interfaces from the media device */ + list_for_each_entry_safe(intf, tmp_intf, &mdev->interfaces, + graph_obj.list) { + /* + * Unlink the interface, but don't free it here; the + * module which created it is responsible for freeing + * it + */ + __media_remove_intf_links(intf); + media_gobj_destroy(&intf->graph_obj); + } + ida_destroy(&mdev->entity_internal_idx); mdev->entity_internal_idx_max = 0; media_graph_walk_cleanup(&mdev->pm_count_walk); @@ -787,42 +812,14 @@ EXPORT_SYMBOL_GPL(__media_device_register); void media_device_unregister(struct media_device *mdev) { - struct media_entity *entity; - struct media_entity *next; - struct media_interface *intf, *tmp_intf; - struct media_entity_notify *notify, *nextp; - if (mdev == NULL) return; mutex_lock(&mdev->graph_mutex); - - /* Check if mdev was ever registered at all */ if (!media_devnode_is_registered(&mdev->devnode)) { mutex_unlock(&mdev->graph_mutex); return; } - - /* Remove all entities from the media device */ - list_for_each_entry_safe(entity, next, &mdev->entities, graph_obj.list) - __media_device_unregister_entity(entity); - - /* Remove all entity_notify callbacks from the media device */ - list_for_each_entry_safe(notify, nextp, &mdev->entity_notify, list) - __media_device_unregister_entity_notify(mdev, notify); - - /* Remove all interfaces from the media device */ - list_for_each_entry_safe(intf, tmp_intf, &mdev->interfaces, - graph_obj.list) { - /* - * Unlink the interface, but don't free it here; the - * module which created it is responsible for freeing - * it - */ - __media_remove_intf_links(intf); - media_gobj_destroy(&intf->graph_obj); - } - mutex_unlock(&mdev->graph_mutex); device_remove_file(&mdev->devnode.dev, &dev_attr_model); From patchwork Tue Mar 12 10:34:13 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sakari Ailus X-Patchwork-Id: 13589811 Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 8060979B76 for ; Tue, 12 Mar 2024 10:34:39 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=192.198.163.17 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1710239681; cv=none; b=jHz7Wzd1V/SK0CqBW85SO3UTT0aVoof9mN1fQKD7fkHQDp5VjV+zGkv/au3F8aoD9bwy59ql0wDf9eNitVZP5EJ1tmso7Xljka6lrCkgQXcCdzrxx80pbcH2IhRDqKhKiky7pUSo/iWwJkWz21ebG8cdHQxuu+5Z2/v6gLCUJtw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1710239681; c=relaxed/simple; bh=bNPcGQWQiPdDPUXvLZG+GLaS8C7SWTIVIHJbLUbtkHo=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=LdlRte56yXMJ3ZtxWJ9omFAgihO9bRxfwLXC33U0NOJt39pnN20YzHX8JfIgCs/nQ7+IEUjgoqUV6ZhAehQ9uA6QUfx58GdPdrog0BlWJ7OlthWIToFDJQrDOKlu43UqxqTTvL05M8d6lUPufFTlCndj5QZQ5bItE7cDl9Njuoc= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.intel.com; spf=none smtp.mailfrom=linux.intel.com; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b=jTKV9n+n; arc=none smtp.client-ip=192.198.163.17 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.intel.com Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=linux.intel.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="jTKV9n+n" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1710239680; x=1741775680; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=bNPcGQWQiPdDPUXvLZG+GLaS8C7SWTIVIHJbLUbtkHo=; b=jTKV9n+nr5OGGBH6reTmPhljfYl9K1eWGJUSY15XNfQ3V9Pdnc5ozUmm 9pCo754aKDWYs+5cm0htRdtbFezUTK+MdOdw3lxCV0TU6r+wVvcJA53Cr sAP4VRNzPKhQNKatgZUUSFZwahmQHtSeH3M2ZitqV6xuMbL87IzwE0cKn minIAosG5RBtBn7MQFuKqf2eJ/OPUIk9EMNfhRLHQpLgsaR9uj1Nnn6pP NGnyw5xnPEkHBzM4Eglof5s/ITVuPo0wJ/UN+R/z9VmV4trf/ygXxQkoM qPFxpI0ISM9ctGpdUyklwJN4C9fehps6pBcOZneP2pzhNi/UqYNoW3hrA g==; X-IronPort-AV: E=McAfee;i="6600,9927,11010"; a="4794322" X-IronPort-AV: E=Sophos;i="6.07,119,1708416000"; d="scan'208";a="4794322" Received: from orviesa005.jf.intel.com ([10.64.159.145]) by fmvoesa111.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 12 Mar 2024 03:34:39 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.07,119,1708416000"; d="scan'208";a="16194099" Received: from turnipsi.fi.intel.com (HELO kekkonen.fi.intel.com) ([10.237.72.44]) by orviesa005-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 12 Mar 2024 03:34:37 -0700 Received: from svinhufvud.ger.corp.intel.com (localhost [IPv6:::1]) by kekkonen.fi.intel.com (Postfix) with ESMTP id B9F111201F7; Tue, 12 Mar 2024 12:34:34 +0200 (EET) From: Sakari Ailus To: linux-media@vger.kernel.org Cc: laurent.pinchart@ideasonboard.com, hverkuil@xs4all.nl Subject: [PATCH v3 17/26] media: omap3isp: Release the isp device struct by media device callback Date: Tue, 12 Mar 2024 12:34:13 +0200 Message-Id: <20240312103422.216484-18-sakari.ailus@linux.intel.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20240312103422.216484-1-sakari.ailus@linux.intel.com> References: <20240312103422.216484-1-sakari.ailus@linux.intel.com> Precedence: bulk X-Mailing-List: linux-media@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Use the media device release callback to release the isp device's data structure. This approach has the benefit of not releasing memory which may still be accessed through open file handles whilst the isp driver is being unbound. Signed-off-by: Sakari Ailus Acked-by: Hans Verkuil --- drivers/media/platform/ti/omap3isp/isp.c | 24 +++++++++++++++++++----- 1 file changed, 19 insertions(+), 5 deletions(-) diff --git a/drivers/media/platform/ti/omap3isp/isp.c b/drivers/media/platform/ti/omap3isp/isp.c index 1cda23244c7b..ef6a781d6da1 100644 --- a/drivers/media/platform/ti/omap3isp/isp.c +++ b/drivers/media/platform/ti/omap3isp/isp.c @@ -649,8 +649,11 @@ static irqreturn_t isp_isr(int irq, void *_isp) return IRQ_HANDLED; } +static void isp_release(struct media_device *mdev); + static const struct media_device_ops isp_media_ops = { .link_notify = v4l2_pipeline_link_notify, + .release = isp_release, }; /* ----------------------------------------------------------------------------- @@ -1607,7 +1610,6 @@ static void isp_unregister_entities(struct isp_device *isp) omap3isp_stat_unregister_entities(&isp->isp_hist); v4l2_device_unregister(&isp->v4l2_dev); - media_device_cleanup(&isp->media_dev); } static int isp_link_entity( @@ -1955,6 +1957,19 @@ static void isp_detach_iommu(struct isp_device *isp) #endif } +static void isp_release(struct media_device *mdev) +{ + struct isp_device *isp = + container_of(mdev, struct isp_device, media_dev); + + isp_cleanup_modules(isp); + + media_entity_enum_cleanup(&isp->crashed); + v4l2_async_nf_cleanup(&isp->notifier); + + kfree(isp); +} + static int isp_attach_iommu(struct isp_device *isp) { #ifdef CONFIG_ARM_DMA_USE_IOMMU @@ -2004,16 +2019,15 @@ static void isp_remove(struct platform_device *pdev) v4l2_async_nf_unregister(&isp->notifier); v4l2_async_nf_cleanup(&isp->notifier); isp_unregister_entities(isp); - isp_cleanup_modules(isp); + isp_xclk_cleanup(isp); __omap3isp_get(isp, false); isp_detach_iommu(isp); __omap3isp_put(isp, false); - media_entity_enum_cleanup(&isp->crashed); - - kfree(isp); + /* May release isp immediately */ + media_device_put(&isp->media_dev); } enum isp_of_phy { From patchwork Tue Mar 12 10:34:14 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sakari Ailus X-Patchwork-Id: 13589812 Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id CCDCD79B98 for ; Tue, 12 Mar 2024 10:34:39 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=192.198.163.17 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1710239681; cv=none; b=iA2INnlgQ94qBJwJYfj37kk1PS2KUbpLb2ONM+mpd0GO4KXPRGXUtB9ZH28dVRQi2klPA4fIOUDi3kNKXu1czODGC8J3d3HmZjQ1LFQk8OZ3D2vbu2737TUQAAz+y+7XFuXyyFqMUCgfZIgODy60Tw27kWptbIgsVF2ba02ievw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1710239681; c=relaxed/simple; bh=mjtyqLV3PD/0dNFTGCwDwuf08P3lvfWUEm6jcsOWoZs=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=oaZEbnpIYK8+Rpsut3N/+RYl9DaV31+hI/uRIiezRmDMKNwkR4KFbULo8TTQa1Jo+aS9dY652q2yOcVklXfEL8EO+G2b0oOlD1lJ3ntvssuQJ55VMV22GlU2YRjHqRztVq34jSOY+9SNdUSnwzzAaCdV8uwSIyvYzZY5X+enZ1o= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.intel.com; spf=none smtp.mailfrom=linux.intel.com; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b=bdHhElFI; arc=none smtp.client-ip=192.198.163.17 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.intel.com Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=linux.intel.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="bdHhElFI" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1710239680; x=1741775680; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=mjtyqLV3PD/0dNFTGCwDwuf08P3lvfWUEm6jcsOWoZs=; b=bdHhElFIOJ3DmD2nsFeZg/kfek0oRTmI8c6KQEBFGBHWa4vzkqN3k2Sl CWhTSPFCExJ4Ux02f+BJkV8kGYx+dQ8lgDVnH9W4MfrdxABogkzW4XnSK ewxNjwzawJ9+muhEEf8pyX1NVLbTn9Z3uJ1OYmvsz7I/J2lMGKS38T9bp i1HjyPCgoAFn/VocAldJ2/ZIY49mHnwYrB4OjBkOxC5VD4OSPW/CtT/Zm gu1bUYHUYYsKPN/9Lcl1RNLrIiafGKyaQ7ryNVELqBmXocZS6vUxGFvja gVPyh7vQL6hNDF3mRN+XiiPX9phYR2dGVI9SgNQ257/hQHD6XjxSl2v9T A==; X-IronPort-AV: E=McAfee;i="6600,9927,11010"; a="4794325" X-IronPort-AV: E=Sophos;i="6.07,119,1708416000"; d="scan'208";a="4794325" Received: from orviesa005.jf.intel.com ([10.64.159.145]) by fmvoesa111.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 12 Mar 2024 03:34:39 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.07,119,1708416000"; d="scan'208";a="16194101" Received: from turnipsi.fi.intel.com (HELO kekkonen.fi.intel.com) ([10.237.72.44]) by orviesa005-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 12 Mar 2024 03:34:38 -0700 Received: from svinhufvud.ger.corp.intel.com (localhost [IPv6:::1]) by kekkonen.fi.intel.com (Postfix) with ESMTP id 52AC21206F9; Tue, 12 Mar 2024 12:34:35 +0200 (EET) From: Sakari Ailus To: linux-media@vger.kernel.org Cc: laurent.pinchart@ideasonboard.com, hverkuil@xs4all.nl Subject: [PATCH v3 18/26] media: ipu3-cio2: Release the cio2 device context by media device callback Date: Tue, 12 Mar 2024 12:34:14 +0200 Message-Id: <20240312103422.216484-19-sakari.ailus@linux.intel.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20240312103422.216484-1-sakari.ailus@linux.intel.com> References: <20240312103422.216484-1-sakari.ailus@linux.intel.com> Precedence: bulk X-Mailing-List: linux-media@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Use the media device release callback to release the cio2 device's data structure. This approach has the benefit of not releasing memory which may still be accessed through open file handles whilst the ipu3-cio2 driver is being unbound. Signed-off-by: Sakari Ailus Acked-by: Hans Verkuil --- drivers/media/pci/intel/ipu3/ipu3-cio2.c | 72 ++++++++++++++++-------- 1 file changed, 47 insertions(+), 25 deletions(-) diff --git a/drivers/media/pci/intel/ipu3/ipu3-cio2.c b/drivers/media/pci/intel/ipu3/ipu3-cio2.c index 00090e7f5f9d..87eda435e37d 100644 --- a/drivers/media/pci/intel/ipu3/ipu3-cio2.c +++ b/drivers/media/pci/intel/ipu3/ipu3-cio2.c @@ -239,9 +239,15 @@ static int cio2_fbpt_init(struct cio2_device *cio2, struct cio2_queue *q) return 0; } -static void cio2_fbpt_exit(struct cio2_queue *q, struct device *dev) +static int cio2_fbpt_exit(struct cio2_queue *q, struct device *dev) { + if (!q->fbpt) + return -ENOENT; + dma_free_coherent(dev, CIO2_FBPT_SIZE, q->fbpt, q->fbpt_bus_addr); + q->fbpt = NULL; + + return 0; } /**************** CSI2 hardware setup ****************/ @@ -1631,13 +1637,16 @@ static int cio2_queue_init(struct cio2_device *cio2, struct cio2_queue *q) static void cio2_queue_exit(struct cio2_device *cio2, struct cio2_queue *q) { - vb2_video_unregister_device(&q->vdev); media_entity_cleanup(&q->vdev.entity); - v4l2_device_unregister_subdev(&q->subdev); media_entity_cleanup(&q->subdev.entity); - cio2_fbpt_exit(q, &cio2->pci_dev->dev); - mutex_destroy(&q->subdev_lock); - mutex_destroy(&q->lock); + /* + * Note that not all mutexes may have been initialised, destroy only + * those that have. + */ + if (!cio2_fbpt_exit(q, &cio2->pci_dev->dev)) { + mutex_destroy(&q->subdev_lock); + mutex_destroy(&q->lock); + } } static int cio2_queues_init(struct cio2_device *cio2) @@ -1647,16 +1656,10 @@ static int cio2_queues_init(struct cio2_device *cio2) for (i = 0; i < CIO2_QUEUES; i++) { r = cio2_queue_init(cio2, &cio2->queue[i]); if (r) - break; + return r; } - if (i == CIO2_QUEUES) - return 0; - - for (i--; i >= 0; i--) - cio2_queue_exit(cio2, &cio2->queue[i]); - - return r; + return 0; } static void cio2_queues_exit(struct cio2_device *cio2) @@ -1667,6 +1670,22 @@ static void cio2_queues_exit(struct cio2_device *cio2) cio2_queue_exit(cio2, &cio2->queue[i]); } +static void cio2_media_release(struct media_device *mdev) +{ + struct cio2_device *cio2 = + container_of(mdev, struct cio2_device, media_dev); + + cio2_queues_exit(cio2); + cio2_fbpt_exit_dummy(cio2); + mutex_destroy(&cio2->lock); + + kfree(cio2); +} + +static const struct media_device_ops cio2_mdev_ops = { + .release = cio2_media_release, +}; + /**************** PCI interface ****************/ static int cio2_pci_probe(struct pci_dev *pci_dev, @@ -1685,7 +1704,7 @@ static int cio2_pci_probe(struct pci_dev *pci_dev, if (r) return r; - cio2 = devm_kzalloc(dev, sizeof(*cio2), GFP_KERNEL); + cio2 = kzalloc(sizeof(*cio2), GFP_KERNEL); if (!cio2) return -ENOMEM; cio2->pci_dev = pci_dev; @@ -1730,6 +1749,7 @@ static int cio2_pci_probe(struct pci_dev *pci_dev, mutex_init(&cio2->lock); cio2->media_dev.dev = dev; + cio2->media_dev.ops = &cio2_mdev_ops; strscpy(cio2->media_dev.model, CIO2_DEVICE_NAME, sizeof(cio2->media_dev.model)); cio2->media_dev.hw_revision = 0; @@ -1737,7 +1757,7 @@ static int cio2_pci_probe(struct pci_dev *pci_dev, media_device_init(&cio2->media_dev); r = media_device_register(&cio2->media_dev); if (r < 0) - goto fail_mutex_destroy; + goto fail_media_device_put; cio2->v4l2_dev.mdev = &cio2->media_dev; r = v4l2_device_register(dev, &cio2->v4l2_dev); @@ -1772,34 +1792,36 @@ static int cio2_pci_probe(struct pci_dev *pci_dev, fail_clean_notifier: v4l2_async_nf_unregister(&cio2->notifier); v4l2_async_nf_cleanup(&cio2->notifier); - cio2_queues_exit(cio2); + fail_v4l2_device_unregister: v4l2_device_unregister(&cio2->v4l2_dev); + fail_media_device_unregister: media_device_unregister(&cio2->media_dev); - media_device_cleanup(&cio2->media_dev); -fail_mutex_destroy: - mutex_destroy(&cio2->lock); - cio2_fbpt_exit_dummy(cio2); +fail_media_device_put: + media_device_put(&cio2->media_dev); return r; } static void cio2_pci_remove(struct pci_dev *pci_dev) { struct cio2_device *cio2 = pci_get_drvdata(pci_dev); + unsigned int i; media_device_unregister(&cio2->media_dev); v4l2_async_nf_unregister(&cio2->notifier); v4l2_async_nf_cleanup(&cio2->notifier); - cio2_queues_exit(cio2); - cio2_fbpt_exit_dummy(cio2); + for (i = 0; i < CIO2_QUEUES; i++) { + vb2_video_unregister_device(&cio2->queue[i].vdev); + v4l2_device_unregister_subdev(&cio2->queue[i].subdev); + } v4l2_device_unregister(&cio2->v4l2_dev); - media_device_cleanup(&cio2->media_dev); - mutex_destroy(&cio2->lock); pm_runtime_forbid(&pci_dev->dev); pm_runtime_get_noresume(&pci_dev->dev); + + media_device_put(&cio2->media_dev); } static int __maybe_unused cio2_runtime_suspend(struct device *dev) From patchwork Tue Mar 12 10:34:15 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sakari Ailus X-Patchwork-Id: 13589813 Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 60ECF79DA2 for ; Tue, 12 Mar 2024 10:34:40 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=192.198.163.17 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1710239681; cv=none; b=gMcKdQwatPzZ6j+2NPdgANDdtDgr5FOiFd+0jJpXD0cl2HgTMRDeqR1SVRTzMhic+Iium1Awa5vT1pLaxy0uBYycyoO2ww1O4nIwH2S9xne4HhhBL7JrxPs1HW2+LeifA6UnIozusRuBBCBO0kdj7LC2och3PPSDotplNnxtQVo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1710239681; c=relaxed/simple; bh=+rG7vchsA0u3MasKBPPJoNX+GZxQlI7TKYCl1XWjqSk=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=nAmKhmRtX2/S5p+ZWX2WbbSfAdrKc+NG72yD5SFvIZxtZVtFx/CKnpAYkclBOTGqOEYEegZJwqvoVMW02OvjIQJneICzGvYz8+6uN8k0uw06Vc06P4So7sX7zoGyld0hRmbZyJrVo9u/bDDbXiJrf3EhKkIiJ6get0hu9GofSdM= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.intel.com; spf=none smtp.mailfrom=linux.intel.com; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b=hktqlkrz; arc=none smtp.client-ip=192.198.163.17 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.intel.com Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=linux.intel.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="hktqlkrz" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1710239681; x=1741775681; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=+rG7vchsA0u3MasKBPPJoNX+GZxQlI7TKYCl1XWjqSk=; b=hktqlkrzhOCRSBfnNpIeulWdcYQ0xFiMcqPQAGZpEphqvbGAzBfBqJ7O +8UtiV83iiFW1LSuzIHo0ZgjXp6yijjDekLEoOgjDANvgXz2MR3uVMU4c kkwpjxrEkhYU9ZpnM5FM29jksu8XBHCYXKZwvs+KHrzpA+GM8s7Lm1JZ+ l9JVA9rKLIdIG55AfRJdNQQMJ4yWy46JUROztlcdPP/wHNGJPWlh//8Rf xREF2UQeRb/jZ8RY3Hhxmv/PrXV216SEhDF0QGBcDjyv4PsiujTBWRwb2 z4jiOi4NTuIWd2YJfiWTjb64T4ynNWn/vDhQFKwpeK4bvIsbXgikM+kkw A==; X-IronPort-AV: E=McAfee;i="6600,9927,11010"; a="4794328" X-IronPort-AV: E=Sophos;i="6.07,119,1708416000"; d="scan'208";a="4794328" Received: from orviesa005.jf.intel.com ([10.64.159.145]) by fmvoesa111.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 12 Mar 2024 03:34:40 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.07,119,1708416000"; d="scan'208";a="16194104" Received: from turnipsi.fi.intel.com (HELO kekkonen.fi.intel.com) ([10.237.72.44]) by orviesa005-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 12 Mar 2024 03:34:39 -0700 Received: from svinhufvud.ger.corp.intel.com (localhost [IPv6:::1]) by kekkonen.fi.intel.com (Postfix) with ESMTP id E0C3F11F819; Tue, 12 Mar 2024 12:34:35 +0200 (EET) From: Sakari Ailus To: linux-media@vger.kernel.org Cc: laurent.pinchart@ideasonboard.com, hverkuil@xs4all.nl Subject: [PATCH v3 19/26] media: vimc: Release resources on media device release Date: Tue, 12 Mar 2024 12:34:15 +0200 Message-Id: <20240312103422.216484-20-sakari.ailus@linux.intel.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20240312103422.216484-1-sakari.ailus@linux.intel.com> References: <20240312103422.216484-1-sakari.ailus@linux.intel.com> Precedence: bulk X-Mailing-List: linux-media@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Release all the resources when the media device is released, moving away from the struct v4l2_device used for that purpose. This is done to exemplify the use of the media device's release callback. Switch to container_of_const(), too, while we're changing the code anyway. Signed-off-by: Sakari Ailus --- drivers/media/test-drivers/vimc/vimc-core.c | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/drivers/media/test-drivers/vimc/vimc-core.c b/drivers/media/test-drivers/vimc/vimc-core.c index af127476e920..3e59f8c256c7 100644 --- a/drivers/media/test-drivers/vimc/vimc-core.c +++ b/drivers/media/test-drivers/vimc/vimc-core.c @@ -264,13 +264,12 @@ static int vimc_add_subdevs(struct vimc_device *vimc) return 0; } -static void vimc_v4l2_dev_release(struct v4l2_device *v4l2_dev) +static void vimc_mdev_release(struct media_device *mdev) { struct vimc_device *vimc = - container_of(v4l2_dev, struct vimc_device, v4l2_dev); + container_of_const(mdev, struct vimc_device, mdev); vimc_release_subdevs(vimc); - media_device_cleanup(&vimc->mdev); kfree(vimc->ent_devs); kfree(vimc); } @@ -336,6 +335,10 @@ static int vimc_register_devices(struct vimc_device *vimc) return ret; } +static const struct media_device_ops vimc_mdev_ops = { + .release = vimc_mdev_release, +}; + static int vimc_probe(struct platform_device *pdev) { const struct font_desc *font = find_font("VGA8x16"); @@ -369,12 +372,12 @@ static int vimc_probe(struct platform_device *pdev) snprintf(vimc->mdev.bus_info, sizeof(vimc->mdev.bus_info), "platform:%s", VIMC_PDEV_NAME); vimc->mdev.dev = &pdev->dev; + vimc->mdev.ops = &vimc_mdev_ops; media_device_init(&vimc->mdev); ret = vimc_register_devices(vimc); if (ret) { - media_device_cleanup(&vimc->mdev); - kfree(vimc); + media_device_put(&vimc->mdev); return ret; } /* @@ -382,7 +385,6 @@ static int vimc_probe(struct platform_device *pdev) * if the registration fails, we release directly from probe */ - vimc->v4l2_dev.release = vimc_v4l2_dev_release; platform_set_drvdata(pdev, vimc); return 0; } @@ -397,6 +399,7 @@ static void vimc_remove(struct platform_device *pdev) media_device_unregister(&vimc->mdev); v4l2_device_unregister(&vimc->v4l2_dev); v4l2_device_put(&vimc->v4l2_dev); + media_device_put(&vimc->mdev); } static void vimc_dev_release(struct device *dev) From patchwork Tue Mar 12 10:34:16 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sakari Ailus X-Patchwork-Id: 13589814 Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 50BB979DAF for ; Tue, 12 Mar 2024 10:34:41 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=192.198.163.17 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1710239682; cv=none; b=h7EcDNtzWSJFJVBLDlF1KdpCCxBqjiCWsbbKW+G7HwUt2Do9vIhZGMAp9pzlOpuKpeV0Remu7Zxivafxdtp6CNL+AK0dbXo76JM64ZVR2HYMspds8X+v9pj8Z5LMeL5VpHbe0PHjW47IvIELsBsZV/MZXQCBAuL1f0ua7Q6qi2M= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1710239682; c=relaxed/simple; bh=WBJkQRl6HUUCoBTVYVQygl04ebZa2GrobqlbR0Xm2nk=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=I680jp3c+q+fOOfv7Sls65qZnUaivpjosg1pnF4FMTPGz2Ld9/G272KezsCZIsybYlRwu8Cp7Xp+R3AFMBzybgzb0PqnBOQBfqAEitklvmWVfFiLtOyuV/TSaFYkjYG39GruBquj4FJr1rEj1gEg7Fci2CX5ciw6zD0tPPKs4wg= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.intel.com; spf=none smtp.mailfrom=linux.intel.com; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b=gWR5O6YU; arc=none smtp.client-ip=192.198.163.17 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.intel.com Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=linux.intel.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="gWR5O6YU" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1710239682; x=1741775682; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=WBJkQRl6HUUCoBTVYVQygl04ebZa2GrobqlbR0Xm2nk=; b=gWR5O6YUNHtZb32VF7jrOMO51Fe/nCr6AGsfRjujK+HLE24mOh6NaSXF oP/AuHCCY+mCedw3eCNrlofVTsPwuY3BqiRq+HHirhBd6U4zRPUgcSvph voF9pi/M8dOtsleFPAEj5HnKBAmq0LWp1aXgdz1FrrVl/Dx8q2ooSGzMC xACIT5MnBVKatQCJik+dBI9yByebEiZrICeQ2VHU0ZLDMBfH3c8RUl6am xJkVefa12ATVP+KdmKHRuxenck5WiCA9hDjnF3iBrL1FW4KZosCVMB2rT M2LmnLsknXs8kq/luP7qsUq5YQIIKVkxfB6i72tSZql39CXW8i4AbFyS9 A==; X-IronPort-AV: E=McAfee;i="6600,9927,11010"; a="4794331" X-IronPort-AV: E=Sophos;i="6.07,119,1708416000"; d="scan'208";a="4794331" Received: from orviesa005.jf.intel.com ([10.64.159.145]) by fmvoesa111.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 12 Mar 2024 03:34:40 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.07,119,1708416000"; d="scan'208";a="16194105" Received: from turnipsi.fi.intel.com (HELO kekkonen.fi.intel.com) ([10.237.72.44]) by orviesa005-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 12 Mar 2024 03:34:39 -0700 Received: from svinhufvud.ger.corp.intel.com (localhost [IPv6:::1]) by kekkonen.fi.intel.com (Postfix) with ESMTP id 6724211F81D; Tue, 12 Mar 2024 12:34:36 +0200 (EET) From: Sakari Ailus To: linux-media@vger.kernel.org Cc: laurent.pinchart@ideasonboard.com, hverkuil@xs4all.nl Subject: [PATCH v3 20/26] media: Documentation: Document how Media device resources are released Date: Tue, 12 Mar 2024 12:34:16 +0200 Message-Id: <20240312103422.216484-21-sakari.ailus@linux.intel.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20240312103422.216484-1-sakari.ailus@linux.intel.com> References: <20240312103422.216484-1-sakari.ailus@linux.intel.com> Precedence: bulk X-Mailing-List: linux-media@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Document that after unregistering, Media device memory resources are released by the release() callback rather than by calling media_device_cleanup(). Also add that driver memory resources should be bound to the Media device, not V4L2 device. Signed-off-by: Sakari Ailus Acked-by: Hans Verkuil --- Documentation/driver-api/media/mc-core.rst | 18 ++++++++++++++++-- include/media/media-device.h | 6 ++++-- 2 files changed, 20 insertions(+), 4 deletions(-) diff --git a/Documentation/driver-api/media/mc-core.rst b/Documentation/driver-api/media/mc-core.rst index 2456950ce8ff..f9108f14d1ed 100644 --- a/Documentation/driver-api/media/mc-core.rst +++ b/Documentation/driver-api/media/mc-core.rst @@ -46,13 +46,27 @@ Drivers initialise media device instances by calling :c:func:`media_device_init()`. After initialising a media device instance, it is registered by calling :c:func:`__media_device_register()` via the macro ``media_device_register()`` and unregistered by calling -:c:func:`media_device_unregister()`. An initialised media device must be -eventually cleaned up by calling :c:func:`media_device_cleanup()`. +:c:func:`media_device_unregister()`. The resources of a newly unregistered media +device will be released by the ``release()`` callback of :c:type:`media_device` +ops, which will be called when the last user of the media device has released it +calling :c:func:`media_device_put()`. + +The ``release()`` callback is the way all the resources of the media device are +released once :c:func:`media_device_init()` has been called. This is also +relevant during device driver's probe function as the ``release()`` callback +will also have to be able to safely release the resources related to a partially +initialised media device. Note that it is not allowed to unregister a media device instance that was not previously registered, or clean up a media device instance that was not previously initialised. +Media device and driver's per-device context +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +Drivers should use the struct media_device_ops ``release()`` callback to release +their own resources and not e.g. that of the struct v4l2_device. + Entities ^^^^^^^^ diff --git a/include/media/media-device.h b/include/media/media-device.h index f1afbfc4dca2..fe4625f3f62b 100644 --- a/include/media/media-device.h +++ b/include/media/media-device.h @@ -252,8 +252,10 @@ static inline void media_device_put(struct media_device *mdev) * * @mdev: pointer to struct &media_device * - * This function that will destroy the graph_mutex that is - * initialized in media_device_init(). + * This function that will destroy the graph_mutex that is initialized in + * media_device_init(). Note that *only* drivers that do not manage releasing + * the memory of th media device itself call this function. This function is + * thus effectively DEPRECATED. */ void media_device_cleanup(struct media_device *mdev); From patchwork Tue Mar 12 10:34:17 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sakari Ailus X-Patchwork-Id: 13589815 Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 8106379DB3 for ; Tue, 12 Mar 2024 10:34:41 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=192.198.163.17 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1710239683; cv=none; b=pqCAJFAiAMmAPoENVqoH/nO1C+3N6S3njdVSmliEvDn7ecU+8mPVKe1GpG4+P4NLqzHVD+tiNyFdD0ZD74PorpkUi8sdq9giuVPn5rVIecVUj03gU7pbXs4ig2cfVtJX38ORgpDCk2AuSOrWKS+AHLhyWm7CMy3Z78ojO5F7quE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1710239683; c=relaxed/simple; bh=FS0nhNcBPCInVIG4RYXDiiNQbo0LCaPWxZJEEPTZ4oQ=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=YO2GfrsUsi6kHGfqRUgWp8OaQn7O0xMvmlgxgHaqQ3sU15JFti9UsmccXzY21WQXhv7PTJ6W5+Pbwi580yYqE2igceABFotoh8m4iD+UNvZpDd3V4QoJ8MojpF1NzkdA+uFnNXJHa5ZC73acOOktX2Pjoo/9QmXdLHW/XwjaHow= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.intel.com; spf=none smtp.mailfrom=linux.intel.com; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b=fhDz16+m; arc=none smtp.client-ip=192.198.163.17 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.intel.com Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=linux.intel.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="fhDz16+m" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1710239682; x=1741775682; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=FS0nhNcBPCInVIG4RYXDiiNQbo0LCaPWxZJEEPTZ4oQ=; b=fhDz16+m7zht/uuM37xXjrF0oqqsOjWVUds1FZHttMIElL8UxTR2OPAT U4ia8SBAoKT1N2bwjtc6hIQjkY0NLFqvvTRjpJ1apfOeDtCnrl06g+/lM KTk6s8EY5k9TykhJKXd4UVqWeOPGdNK+KsUcRIIUr+7d6SG6vIWQKk2Q9 ZhMws0nbC01de7qUCEecCTA5IrnhyH59STUH/zQ783Tx8/bUn3u6U83F9 9NknON3wH6QX9/97ajGjZdFOAR1ogLEzxtiNqK00TPW1zoccvmvFfTm6S 5naN/GXzJOmVNgnHDckQpFa35e5soKKhTbrYV8cvcbj+0rQaLPeh5evh3 g==; X-IronPort-AV: E=McAfee;i="6600,9927,11010"; a="4794334" X-IronPort-AV: E=Sophos;i="6.07,119,1708416000"; d="scan'208";a="4794334" Received: from orviesa005.jf.intel.com ([10.64.159.145]) by fmvoesa111.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 12 Mar 2024 03:34:41 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.07,119,1708416000"; d="scan'208";a="16194108" Received: from turnipsi.fi.intel.com (HELO kekkonen.fi.intel.com) ([10.237.72.44]) by orviesa005-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 12 Mar 2024 03:34:40 -0700 Received: from svinhufvud.ger.corp.intel.com (localhost [IPv6:::1]) by kekkonen.fi.intel.com (Postfix) with ESMTP id F2D6A11FB11; Tue, 12 Mar 2024 12:34:36 +0200 (EET) From: Sakari Ailus To: linux-media@vger.kernel.org Cc: laurent.pinchart@ideasonboard.com, hverkuil@xs4all.nl Subject: [PATCH v3 21/26] media: mc: Add per-file-handle data support Date: Tue, 12 Mar 2024 12:34:17 +0200 Message-Id: <20240312103422.216484-22-sakari.ailus@linux.intel.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20240312103422.216484-1-sakari.ailus@linux.intel.com> References: <20240312103422.216484-1-sakari.ailus@linux.intel.com> Precedence: bulk X-Mailing-List: linux-media@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 From: Laurent Pinchart The media devnode core associates devnodes with files by storing the devnode pointer in the file structure private_data field. In order to allow tracking of per-file-handle data introduce a new media devnode file handle structure that stores the devnode pointer, and store a pointer to that structure in the file private_data field. Users of the media devnode code (the only existing user being media_device) are responsible for managing their own subclass of the media_devnode_fh structure. Signed-off-by: Laurent Pinchart Prepare struct media_device_fh to be used for maintaining file handle list to avoid shuffling things here and there right after. Signed-off-by: Sakari Ailus Acked-by: Hans Verkuil --- drivers/media/mc/mc-device.c | 14 +++++++++++++- drivers/media/mc/mc-devnode.c | 20 +++++++++----------- include/media/media-device.h | 7 +++++++ include/media/media-devnode.h | 18 +++++++++++++++++- include/media/media-fh.h | 32 ++++++++++++++++++++++++++++++++ 5 files changed, 78 insertions(+), 13 deletions(-) create mode 100644 include/media/media-fh.h diff --git a/drivers/media/mc/mc-device.c b/drivers/media/mc/mc-device.c index 10426c2796b6..67a39cb63f89 100644 --- a/drivers/media/mc/mc-device.c +++ b/drivers/media/mc/mc-device.c @@ -22,6 +22,7 @@ #include #include #include +#include #include #ifdef CONFIG_MEDIA_CONTROLLER @@ -35,7 +36,6 @@ #define MEDIA_ENT_SUBTYPE_MASK 0x0000ffff #define MEDIA_ENT_T_DEVNODE_UNKNOWN (MEDIA_ENT_F_OLD_BASE | \ MEDIA_ENT_SUBTYPE_MASK) - /* ----------------------------------------------------------------------------- * Userspace API */ @@ -47,11 +47,23 @@ static inline void __user *media_get_uptr(__u64 arg) static int media_device_open(struct file *filp) { + struct media_device_fh *fh; + + fh = kzalloc(sizeof(*fh), GFP_KERNEL); + if (!fh) + return -ENOMEM; + + filp->private_data = &fh->fh; + return 0; } static int media_device_close(struct file *filp) { + struct media_device_fh *fh = media_device_fh(filp); + + kfree(fh); + return 0; } diff --git a/drivers/media/mc/mc-devnode.c b/drivers/media/mc/mc-devnode.c index 6fb3a202eb26..4fdb1572ff9b 100644 --- a/drivers/media/mc/mc-devnode.c +++ b/drivers/media/mc/mc-devnode.c @@ -133,6 +133,7 @@ static long media_compat_ioctl(struct file *filp, unsigned int cmd, static int media_open(struct inode *inode, struct file *filp) { struct media_devnode *devnode; + struct media_devnode_fh *fh; int ret; /* Check if the media device is available. This needs to be done with @@ -153,17 +154,15 @@ static int media_open(struct inode *inode, struct file *filp) get_device(&devnode->dev); mutex_unlock(&media_devnode_lock); - filp->private_data = devnode; - - if (devnode->fops->open) { - ret = devnode->fops->open(filp); - if (ret) { - put_device(&devnode->dev); - filp->private_data = NULL; - return ret; - } + ret = devnode->fops->open(filp); + if (ret) { + put_device(&devnode->dev); + return ret; } + fh = filp->private_data; + fh->devnode = devnode; + return 0; } @@ -172,8 +171,7 @@ static int media_release(struct inode *inode, struct file *filp) { struct media_devnode *devnode = media_devnode_data(filp); - if (devnode->fops->release) - devnode->fops->release(filp); + devnode->fops->release(filp); filp->private_data = NULL; diff --git a/include/media/media-device.h b/include/media/media-device.h index fe4625f3f62b..f9f7c37e7d57 100644 --- a/include/media/media-device.h +++ b/include/media/media-device.h @@ -110,6 +110,10 @@ struct media_device_ops { * other operations that stop or start streaming. * @request_id: Used to generate unique request IDs * + * @fh_list: List of file handles in the media device + * (struct media_device_fh.mdev_list). + * @fh_list_lock: Serialise access to fh_list list. + * * This structure represents an abstract high-level media device. It allows easy * access to entities and provides basic media device-level support. The * structure can be allocated directly or embedded in a larger structure. @@ -182,6 +186,9 @@ struct media_device { struct mutex req_queue_mutex; atomic_t request_id; + + struct list_head fh_list; + spinlock_t fh_list_lock; }; /* We don't need to include usb.h here */ diff --git a/include/media/media-devnode.h b/include/media/media-devnode.h index 113c317e6a0e..e4e8552598eb 100644 --- a/include/media/media-devnode.h +++ b/include/media/media-devnode.h @@ -53,6 +53,20 @@ struct media_file_operations { int (*release) (struct file *); }; +/** + * struct media_devnode_fh - Media device node file handle + * @devnode: pointer to the media device node + * + * This structure serves as a base for per-file-handle data storage. Media + * device node users embed media_devnode_fh in their custom file handle data + * structures and store the media_devnode_fh in the file private_data in order + * to let the media device node core locate the media_devnode corresponding to a + * file handle. + */ +struct media_devnode_fh { + struct media_devnode *devnode; +}; + /** * struct media_devnode - Media device node * @fops: pointer to struct &media_file_operations with media device ops @@ -136,7 +150,9 @@ void media_devnode_unregister(struct media_devnode *devnode); */ static inline struct media_devnode *media_devnode_data(struct file *filp) { - return filp->private_data; + struct media_devnode_fh *fh = filp->private_data; + + return fh->devnode; } /** diff --git a/include/media/media-fh.h b/include/media/media-fh.h new file mode 100644 index 000000000000..6f00744b81d6 --- /dev/null +++ b/include/media/media-fh.h @@ -0,0 +1,32 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +/* + * Media device file handle + * + * Copyright (C) 2019--2023 Intel Corporation + */ + +#ifndef MEDIA_FH_H +#define MEDIA_FH_H + +#include +#include + +#include + +/** + * struct media_device_fh - File handle specific information on MC + * + * @fh: The media device file handle + * @mdev_list: This file handle in media device's list of file handles + */ +struct media_device_fh { + struct media_devnode_fh fh; + struct list_head mdev_list; +}; + +static inline struct media_device_fh *media_device_fh(struct file *filp) +{ + return container_of(filp->private_data, struct media_device_fh, fh); +} + +#endif /* MEDIA_FH_H */ From patchwork Tue Mar 12 10:34:18 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sakari Ailus X-Patchwork-Id: 13589816 Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id EC4A879DBF for ; Tue, 12 Mar 2024 10:34:41 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=192.198.163.17 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1710239683; cv=none; b=ii76U1pqUIRQCqMIew+5yrmd3tg/n8IctLg4ao7ytc2U7kdwfHmkzOdeFCX1Vgftb6M0U0mn4P+3v5phsUcgivICb5g34hYh/GRWxpmuHT1pVCHNIL7U69ztK9PY54wNF4xZy2SOQKkBHxcMeFvbFPvk+WZtXyGXm9iiEvWMII8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1710239683; c=relaxed/simple; bh=b52QlhxKu3wE7KEdG0WWrG7Jv7ikslIfdOhydp/Ww+0=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=nDB2/lZ/Zqu0PONqPVy4htXS7+aKY99tdWcx5vhQWb7ypz9G6MbA9BOdrYDSL1ZrbGj2xX/gxSqVGVxC2UCjXLwazc6O4DFqmfltnvc/OzHoLZvq8bjlnryHu9/CpNHNsnhSpEQXvBB91xQFWq5UDhjTVrKoP6pim3ODJFI36wA= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.intel.com; spf=none smtp.mailfrom=linux.intel.com; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b=EW0f4VQa; arc=none smtp.client-ip=192.198.163.17 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.intel.com Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=linux.intel.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="EW0f4VQa" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1710239682; x=1741775682; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=b52QlhxKu3wE7KEdG0WWrG7Jv7ikslIfdOhydp/Ww+0=; b=EW0f4VQaN3vb/VI1AWfnMPwPZWjf+C9YkMVkRQshY0hbTSY9os6dohLS KZfnzUParUphY+k2cbs1mJwirAQRkay8eBMxtpgGn4qYEmcBiXLmUTE43 ooEaWJQBxu1tyzYZRX91eup68h2sBZo3EWg4UYb52dCgLqB4YFZ3AwnKc IFyZ0utAuX7CkTinQICnPDXpEubvWAz+2AS43xMhSpnLs0dj11U8x8E80 kPIW091LYYr5U/93XQR5R+Aux6gjdwIUqMyixgZDerYlCPH6tBtOBn88G aEM0UXFX8rjPIyugOcurDIoTR3JDDwq/H8FJRV2ogNbYW0cR2/XKb1azh Q==; X-IronPort-AV: E=McAfee;i="6600,9927,11010"; a="4794337" X-IronPort-AV: E=Sophos;i="6.07,119,1708416000"; d="scan'208";a="4794337" Received: from orviesa005.jf.intel.com ([10.64.159.145]) by fmvoesa111.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 12 Mar 2024 03:34:41 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.07,119,1708416000"; d="scan'208";a="16194113" Received: from turnipsi.fi.intel.com (HELO kekkonen.fi.intel.com) ([10.237.72.44]) by orviesa005-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 12 Mar 2024 03:34:40 -0700 Received: from svinhufvud.ger.corp.intel.com (localhost [IPv6:::1]) by kekkonen.fi.intel.com (Postfix) with ESMTP id C49B4120619; Tue, 12 Mar 2024 12:34:37 +0200 (EET) From: Sakari Ailus To: linux-media@vger.kernel.org Cc: laurent.pinchart@ideasonboard.com, hverkuil@xs4all.nl Subject: [PATCH v3 22/26] media: mc: Maintain a list of open file handles in a media device Date: Tue, 12 Mar 2024 12:34:18 +0200 Message-Id: <20240312103422.216484-23-sakari.ailus@linux.intel.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20240312103422.216484-1-sakari.ailus@linux.intel.com> References: <20240312103422.216484-1-sakari.ailus@linux.intel.com> Precedence: bulk X-Mailing-List: linux-media@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 The list of file handles is needed to deliver media events as well as for other purposes in the future. Signed-off-by: Sakari Ailus --- drivers/media/mc/mc-device.c | 19 ++++++++++++++++++- drivers/media/mc/mc-devnode.c | 2 +- include/media/media-devnode.h | 4 +++- 3 files changed, 22 insertions(+), 3 deletions(-) diff --git a/drivers/media/mc/mc-device.c b/drivers/media/mc/mc-device.c index 67a39cb63f89..2c7e3ab33e3a 100644 --- a/drivers/media/mc/mc-device.c +++ b/drivers/media/mc/mc-device.c @@ -45,8 +45,9 @@ static inline void __user *media_get_uptr(__u64 arg) return (void __user *)(uintptr_t)arg; } -static int media_device_open(struct file *filp) +static int media_device_open(struct media_devnode *devnode, struct file *filp) { + struct media_device *mdev = to_media_device(devnode); struct media_device_fh *fh; fh = kzalloc(sizeof(*fh), GFP_KERNEL); @@ -55,13 +56,23 @@ static int media_device_open(struct file *filp) filp->private_data = &fh->fh; + spin_lock_irq(&mdev->fh_list_lock); + list_add(&fh->mdev_list, &mdev->fh_list); + spin_unlock_irq(&mdev->fh_list_lock); + return 0; } static int media_device_close(struct file *filp) { + struct media_devnode *devnode = media_devnode_data(filp); + struct media_device *mdev = to_media_device(devnode); struct media_device_fh *fh = media_device_fh(filp); + spin_lock_irq(&mdev->fh_list_lock); + list_del(&fh->mdev_list); + spin_unlock_irq(&mdev->fh_list_lock); + kfree(fh); return 0; @@ -769,11 +780,13 @@ void media_device_init(struct media_device *mdev) INIT_LIST_HEAD(&mdev->pads); INIT_LIST_HEAD(&mdev->links); INIT_LIST_HEAD(&mdev->entity_notify); + INIT_LIST_HEAD(&mdev->fh_list); mutex_init(&mdev->req_queue_mutex); mutex_init(&mdev->graph_mutex); ida_init(&mdev->entity_internal_idx); atomic_set(&mdev->request_id, 0); + spin_lock_init(&mdev->fh_list_lock); mdev->devnode.release = media_device_release; media_devnode_init(&mdev->devnode); @@ -834,6 +847,10 @@ void media_device_unregister(struct media_device *mdev) } mutex_unlock(&mdev->graph_mutex); + spin_lock_irq(&mdev->fh_list_lock); + list_del_init(&mdev->fh_list); + spin_unlock_irq(&mdev->fh_list_lock); + device_remove_file(&mdev->devnode.dev, &dev_attr_model); dev_dbg(mdev->dev, "Media device unregistering\n"); media_devnode_unregister(&mdev->devnode); diff --git a/drivers/media/mc/mc-devnode.c b/drivers/media/mc/mc-devnode.c index 4fdb1572ff9b..4c23551012d4 100644 --- a/drivers/media/mc/mc-devnode.c +++ b/drivers/media/mc/mc-devnode.c @@ -154,7 +154,7 @@ static int media_open(struct inode *inode, struct file *filp) get_device(&devnode->dev); mutex_unlock(&media_devnode_lock); - ret = devnode->fops->open(filp); + ret = devnode->fops->open(devnode, filp); if (ret) { put_device(&devnode->dev); return ret; diff --git a/include/media/media-devnode.h b/include/media/media-devnode.h index e4e8552598eb..898fa67ca090 100644 --- a/include/media/media-devnode.h +++ b/include/media/media-devnode.h @@ -21,6 +21,8 @@ #include #include +struct media_devnode; + /* * Flag to mark the media_devnode struct as registered. Drivers must not touch * this flag directly, it will be set and cleared by media_devnode_register and @@ -49,7 +51,7 @@ struct media_file_operations { __poll_t (*poll) (struct file *, struct poll_table_struct *); long (*ioctl) (struct file *, unsigned int, unsigned long); long (*compat_ioctl) (struct file *, unsigned int, unsigned long); - int (*open) (struct file *); + int (*open) (struct media_devnode *, struct file *); int (*release) (struct file *); }; From patchwork Tue Mar 12 10:34:19 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sakari Ailus X-Patchwork-Id: 13589818 Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id E573459147 for ; Tue, 12 Mar 2024 10:34:42 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=192.198.163.17 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1710239684; cv=none; b=ceHCC8s+NwHA6gwu+TgTeKng+ceOkOI1+7GlidRjzOHWZhZRQGjkU7E6mvbQps1OV/n3sC42KG3PA/p1W4PwGs7ffBPrA1toBU5c5QngpXOeeh9VeeUYjByu80fGbmNHUv1+1wutDdgesAQ0bFPn8cQQhiMt/Iii/syqgxw6lJU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1710239684; c=relaxed/simple; bh=zxqJjgBe50k6Fg5l/L0Kv0413agcxpDaOBJVJI245ZU=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=Vxz8MEESxZL7F+owr7luK3Om/VeEtDJ2TZQHZz/nMd4Md75W1ce6lSwzRn93jfR8d4UCUr++zPIePPi+qKkAfWAcavKG8XOY52A4Fa1mAqOmD/W3M0xmL+RuEcYk9iORe4iPFrF1IsrTwRKxr2lpjJzXx7/P4nb2zj1f6Btej0I= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.intel.com; spf=none smtp.mailfrom=linux.intel.com; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b=MBPrgoNH; arc=none smtp.client-ip=192.198.163.17 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.intel.com Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=linux.intel.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="MBPrgoNH" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1710239683; x=1741775683; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=zxqJjgBe50k6Fg5l/L0Kv0413agcxpDaOBJVJI245ZU=; b=MBPrgoNHBQ9k51P/OWudkKJoSnBG/DLYiFRZlsURyqm/iCfTRCVmphPM +GDcIysuTBtmdhH3a1MYZQ51cfMvzkVjYsiga/E+CsiiYXFHv/cK4+SiB mB8GLCKcnMaTXXhySpJWhxXSPwTvsPlRP2XAWu81qN8NJGtFfgQi9OOKB VuzrCx+lFcMgZo/igcjOZeKMqBUj4d7NiQLu+yZJWhHHk3aI7GKRDRCwW qxXiyvntNllu19I1RBGALby36eg9IbIrJI5GJvsV9vWu4kea2GqqAehLp OdiXWGU/fS+1Mx4CUEOY6rGu2mS3s3v9D7dVLq8mFRTnasOLbABYkXsbA w==; X-IronPort-AV: E=McAfee;i="6600,9927,11010"; a="4794340" X-IronPort-AV: E=Sophos;i="6.07,119,1708416000"; d="scan'208";a="4794340" Received: from orviesa005.jf.intel.com ([10.64.159.145]) by fmvoesa111.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 12 Mar 2024 03:34:43 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.07,119,1708416000"; d="scan'208";a="16194116" Received: from turnipsi.fi.intel.com (HELO kekkonen.fi.intel.com) ([10.237.72.44]) by orviesa005-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 12 Mar 2024 03:34:41 -0700 Received: from svinhufvud.ger.corp.intel.com (localhost [IPv6:::1]) by kekkonen.fi.intel.com (Postfix) with ESMTP id 5DFD911FB89; Tue, 12 Mar 2024 12:34:38 +0200 (EET) From: Sakari Ailus To: linux-media@vger.kernel.org Cc: laurent.pinchart@ideasonboard.com, hverkuil@xs4all.nl Subject: [PATCH v3 23/26] media: mc: Implement best effort media device removal safety sans refcount Date: Tue, 12 Mar 2024 12:34:19 +0200 Message-Id: <20240312103422.216484-24-sakari.ailus@linux.intel.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20240312103422.216484-1-sakari.ailus@linux.intel.com> References: <20240312103422.216484-1-sakari.ailus@linux.intel.com> Precedence: bulk X-Mailing-List: linux-media@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Add a new helper data structures media_devnode_compat_ref and media_devnode_cdev. The latter is used to prevent user space from calling IOCTLs or other system calls to the media device that has been already unregistered and the former to help with obtaining the container struct (either media_devnode_compat_ref or media_devnode) in media_open(). The media device's memory may of course still be released during the call but there is only so much that can be done to this without the driver managing the lifetime of the resources it needs somehow. This patch should be reverted once all drivers have been converted to manage their resources' lifetime. Signed-off-by: Sakari Ailus --- drivers/media/mc/mc-device.c | 49 +++++++++--- drivers/media/mc/mc-devnode.c | 118 ++++++++++++++++++++++------- drivers/media/v4l2-core/v4l2-dev.c | 26 +++++-- include/media/media-device.h | 8 +- include/media/media-devnode.h | 65 ++++++++++++++-- 5 files changed, 210 insertions(+), 56 deletions(-) diff --git a/drivers/media/mc/mc-device.c b/drivers/media/mc/mc-device.c index 2c7e3ab33e3a..dcd40fdab29d 100644 --- a/drivers/media/mc/mc-device.c +++ b/drivers/media/mc/mc-device.c @@ -54,6 +54,8 @@ static int media_device_open(struct media_devnode *devnode, struct file *filp) if (!fh) return -ENOMEM; + fh->fh.ref = devnode->ref; + filp->private_data = &fh->fh; spin_lock_irq(&mdev->fh_list_lock); @@ -65,13 +67,16 @@ static int media_device_open(struct media_devnode *devnode, struct file *filp) static int media_device_close(struct file *filp) { - struct media_devnode *devnode = media_devnode_data(filp); - struct media_device *mdev = to_media_device(devnode); struct media_device_fh *fh = media_device_fh(filp); - spin_lock_irq(&mdev->fh_list_lock); - list_del(&fh->mdev_list); - spin_unlock_irq(&mdev->fh_list_lock); + if (!fh->fh.ref || atomic_read(&fh->fh.ref->registered)) { + struct media_devnode *devnode = media_devnode_data(filp); + struct media_device *mdev = to_media_device(devnode); + + spin_lock_irq(&mdev->fh_list_lock); + list_del(&fh->mdev_list); + spin_unlock_irq(&mdev->fh_list_lock); + } kfree(fh); @@ -810,28 +815,45 @@ EXPORT_SYMBOL_GPL(media_device_cleanup); int __must_check __media_device_register(struct media_device *mdev, struct module *owner) { + struct media_devnode_compat_ref *ref = NULL; int ret; + if (!mdev->ops || !mdev->ops->release) { + ref = kzalloc(sizeof(*mdev->devnode.ref), GFP_KERNEL); + if (!ref) + return -ENOMEM; + } + /* Register the device node. */ mdev->devnode.fops = &media_device_fops; mdev->devnode.parent = mdev->dev; + mdev->devnode.ref = ref; /* Set version 0 to indicate user-space that the graph is static */ mdev->topology_version = 0; ret = media_devnode_register(&mdev->devnode, owner); if (ret < 0) - return ret; + goto out_put_ref; + + ret = device_create_file(media_devnode_dev(&mdev->devnode), + &dev_attr_model); + if (ret < 0) + goto out_devnode_unregister; - ret = device_create_file(&mdev->devnode.dev, &dev_attr_model); - if (ret < 0) { - media_devnode_unregister(&mdev->devnode); - return ret; - } dev_dbg(mdev->dev, "Media device registered\n"); return 0; + +out_devnode_unregister: + media_devnode_unregister(&mdev->devnode); + +out_put_ref: + if (ref) + put_device(&ref->dev); + + return ret; } EXPORT_SYMBOL_GPL(__media_device_register); @@ -851,9 +873,12 @@ void media_device_unregister(struct media_device *mdev) list_del_init(&mdev->fh_list); spin_unlock_irq(&mdev->fh_list_lock); - device_remove_file(&mdev->devnode.dev, &dev_attr_model); + device_remove_file(media_devnode_dev(&mdev->devnode), &dev_attr_model); dev_dbg(mdev->dev, "Media device unregistering\n"); media_devnode_unregister(&mdev->devnode); + + if (mdev->devnode.ref) + put_device(&mdev->devnode.ref->dev); } EXPORT_SYMBOL_GPL(media_device_unregister); diff --git a/drivers/media/mc/mc-devnode.c b/drivers/media/mc/mc-devnode.c index 4c23551012d4..e2a30b21e6c8 100644 --- a/drivers/media/mc/mc-devnode.c +++ b/drivers/media/mc/mc-devnode.c @@ -49,24 +49,52 @@ static void media_devnode_release(struct device *cd) { struct media_devnode *devnode = to_media_devnode(cd); + /* If the devnode has a ref, it is simply released by the user. */ + if (devnode->ref) + return; + /* Release media_devnode and perform other cleanups as needed. */ if (devnode->release) devnode->release(devnode); } +static void media_devnode_ref_release(struct device *cd) +{ + struct media_devnode_compat_ref *ref = + container_of_const(cd, struct media_devnode_compat_ref, dev); + + kfree(ref); +} + +struct media_devnode *to_media_devnode(struct device *dev) +{ + if (dev->release == media_devnode_release) + return container_of(dev, struct media_devnode, dev); + + return container_of(dev, struct media_devnode_compat_ref, dev)->devnode; +} + static const struct bus_type media_bus_type = { .name = MEDIA_NAME, }; +static bool media_devnode_is_registered_compat(struct media_devnode_fh *fh) +{ + if (fh->ref) + return atomic_read(&fh->ref->registered); + + return media_devnode_is_registered(fh->devnode); +} + static ssize_t media_read(struct file *filp, char __user *buf, size_t sz, loff_t *off) { struct media_devnode *devnode = media_devnode_data(filp); + if (!media_devnode_is_registered_compat(filp->private_data)) + return -EIO; if (!devnode->fops->read) return -EINVAL; - if (!media_devnode_is_registered(devnode)) - return -EIO; return devnode->fops->read(filp, buf, sz, off); } @@ -75,10 +103,10 @@ static ssize_t media_write(struct file *filp, const char __user *buf, { struct media_devnode *devnode = media_devnode_data(filp); + if (!media_devnode_is_registered_compat(filp->private_data)) + return -EIO; if (!devnode->fops->write) return -EINVAL; - if (!media_devnode_is_registered(devnode)) - return -EIO; return devnode->fops->write(filp, buf, sz, off); } @@ -87,7 +115,7 @@ static __poll_t media_poll(struct file *filp, { struct media_devnode *devnode = media_devnode_data(filp); - if (!media_devnode_is_registered(devnode)) + if (!media_devnode_is_registered_compat(filp->private_data)) return EPOLLERR | EPOLLHUP; if (!devnode->fops->poll) return DEFAULT_POLLMASK; @@ -99,14 +127,9 @@ __media_ioctl(struct file *filp, unsigned int cmd, unsigned long arg, long (*ioctl_func)(struct file *filp, unsigned int cmd, unsigned long arg)) { - struct media_devnode *devnode = media_devnode_data(filp); - if (!ioctl_func) return -ENOTTY; - if (!media_devnode_is_registered(devnode)) - return -EIO; - return ioctl_func(filp, cmd, arg); } @@ -114,6 +137,9 @@ static long media_ioctl(struct file *filp, unsigned int cmd, unsigned long arg) { struct media_devnode *devnode = media_devnode_data(filp); + if (!media_devnode_is_registered_compat(filp->private_data)) + return -EIO; + return __media_ioctl(filp, cmd, arg, devnode->fops->ioctl); } @@ -124,6 +150,9 @@ static long media_compat_ioctl(struct file *filp, unsigned int cmd, { struct media_devnode *devnode = media_devnode_data(filp); + if (!media_devnode_is_registered_compat(filp->private_data)) + return -EIO; + return __media_ioctl(filp, cmd, arg, devnode->fops->compat_ioctl); } @@ -132,6 +161,7 @@ static long media_compat_ioctl(struct file *filp, unsigned int cmd, /* Override for the open function */ static int media_open(struct inode *inode, struct file *filp) { + struct media_devnode_cdev *mcdev; struct media_devnode *devnode; struct media_devnode_fh *fh; int ret; @@ -143,7 +173,12 @@ static int media_open(struct inode *inode, struct file *filp) * a crash. */ mutex_lock(&media_devnode_lock); - devnode = container_of(inode->i_cdev, struct media_devnode, cdev); + mcdev = container_of(inode->i_cdev, struct media_devnode_cdev, cdev); + if (mcdev->is_compat_ref) + devnode = container_of(mcdev, struct media_devnode_compat_ref, + mcdev)->devnode; + else + devnode = container_of(mcdev, struct media_devnode, mcdev); /* return ENXIO if the media device has been removed already or if it is not registered anymore. */ if (!media_devnode_is_registered(devnode)) { @@ -151,12 +186,12 @@ static int media_open(struct inode *inode, struct file *filp) return -ENXIO; } /* and increase the device refcount */ - get_device(&devnode->dev); + get_device(media_devnode_dev(devnode)); mutex_unlock(&media_devnode_lock); ret = devnode->fops->open(devnode, filp); if (ret) { - put_device(&devnode->dev); + put_device(media_devnode_dev(devnode)); return ret; } @@ -169,15 +204,21 @@ static int media_open(struct inode *inode, struct file *filp) /* Override for the release function */ static int media_release(struct inode *inode, struct file *filp) { - struct media_devnode *devnode = media_devnode_data(filp); - - devnode->fops->release(filp); + struct media_devnode_fh *fh = filp->private_data; + struct device *dev; + + if (!fh->ref) { + dev = &fh->devnode->dev; + fh->devnode->fops->release(filp); + } else { + dev = &fh->ref->dev; + fh->ref->release(filp); + } filp->private_data = NULL; - /* decrease the refcount unconditionally since the release() - return value is ignored. */ - put_device(&devnode->dev); + put_device(dev); + return 0; } @@ -204,6 +245,9 @@ void media_devnode_init(struct media_devnode *devnode) int __must_check media_devnode_register(struct media_devnode *devnode, struct module *owner) { + struct media_devnode_compat_ref *ref = devnode->ref; + struct cdev *cdev; + struct device *dev; int minor; int ret; @@ -222,18 +266,30 @@ int __must_check media_devnode_register(struct media_devnode *devnode, devnode->minor = minor; /* Part 2: Initialize the media and character devices */ - cdev_init(&devnode->cdev, &media_devnode_fops); - devnode->cdev.owner = owner; - kobject_set_name(&devnode->cdev.kobj, "media%d", devnode->minor); - - devnode->dev.bus = &media_bus_type; - devnode->dev.devt = MKDEV(MAJOR(media_dev_t), devnode->minor); + cdev = ref ? &ref->mcdev.cdev : &devnode->mcdev.cdev; + cdev_init(cdev, &media_devnode_fops); + cdev->owner = owner; + kobject_set_name(&cdev->kobj, "media%d", devnode->minor); + + if (!ref) { + dev = &devnode->dev; + } else { + ref->mcdev.is_compat_ref = true; + device_initialize(&ref->dev); + atomic_set(&ref->registered, 1); + ref->devnode = devnode; + ref->release = devnode->fops->release; + dev = &ref->dev; + dev->release = media_devnode_ref_release; + } + dev->bus = &media_bus_type; + dev->devt = MKDEV(MAJOR(media_dev_t), devnode->minor); if (devnode->parent) - devnode->dev.parent = devnode->parent; - dev_set_name(&devnode->dev, "media%d", devnode->minor); + dev->parent = devnode->parent; + dev_set_name(dev, "media%d", devnode->minor); /* Part 3: Add the media and character devices */ - ret = cdev_device_add(&devnode->cdev, &devnode->dev); + ret = cdev_device_add(cdev, dev); if (ret < 0) { pr_err("%s: cdev_device_add failed\n", __func__); goto cdev_add_error; @@ -258,11 +314,15 @@ void media_devnode_unregister(struct media_devnode *devnode) if (!media_devnode_is_registered(devnode)) return; + if (devnode->ref) + atomic_set(&devnode->ref->registered, 0); + mutex_lock(&media_devnode_lock); clear_bit(MEDIA_FLAG_REGISTERED, &devnode->flags); mutex_unlock(&media_devnode_lock); - cdev_device_del(&devnode->cdev, &devnode->dev); + cdev_device_del(devnode->ref ? &devnode->ref->mcdev.cdev : + &devnode->mcdev.cdev, media_devnode_dev(devnode)); mutex_lock(&media_devnode_lock); clear_bit(devnode->minor, media_devnode_nums); diff --git a/drivers/media/v4l2-core/v4l2-dev.c b/drivers/media/v4l2-core/v4l2-dev.c index ba0bfcbe7136..0476f96da62c 100644 --- a/drivers/media/v4l2-core/v4l2-dev.c +++ b/drivers/media/v4l2-core/v4l2-dev.c @@ -179,7 +179,7 @@ static void v4l2_device_release(struct device *cd) bool v4l2_dev_call_release = v4l2_dev->release; #ifdef CONFIG_MEDIA_CONTROLLER struct media_device *mdev = v4l2_dev->mdev; - bool mdev_has_release = mdev && mdev->ops && mdev->ops->release; + bool mdev_has_ref = mdev && mdev->devnode.ref; #endif mutex_lock(&videodev_lock); @@ -212,12 +212,24 @@ static void v4l2_device_release(struct device *cd) } #endif - /* Release video_device and perform other - cleanups as needed. */ + /* + * Put struct media_devnode_compat_ref here as indicated by + * mdev_has_ref. mdev may be released by vdev->release() below. + */ +#ifdef CONFIG_MEDIA_CONTROLLER + if (mdev && mdev_has_ref) + media_device_put(mdev); +#endif + + /* Release video_device and perform other cleanups as needed. */ vdev->release(vdev); #ifdef CONFIG_MEDIA_CONTROLLER - if (mdev) + /* + * Put a reference to struct media_device acquired in + * video_register_media_controller(). + */ + if (mdev && !mdev_has_ref) media_device_put(mdev); /* @@ -225,13 +237,15 @@ static void v4l2_device_release(struct device *cd) * embedded in the same driver's context struct so having a release * callback in both is a bug. */ - if (WARN_ON(v4l2_dev_call_release && mdev_has_release)) + if (WARN_ON(v4l2_dev_call_release && !mdev_has_ref)) v4l2_dev_call_release = false; #endif /* * Decrease v4l2_device refcount, but only if the media device doesn't - * have a release callback. + * have a release callback. Otherwise one could expect accessing + * released memory --- driver's context struct refcounted already via + * struct media_device. */ if (v4l2_dev_call_release) v4l2_device_put(v4l2_dev); diff --git a/include/media/media-device.h b/include/media/media-device.h index f9f7c37e7d57..30f9b78d1ce7 100644 --- a/include/media/media-device.h +++ b/include/media/media-device.h @@ -259,10 +259,10 @@ static inline void media_device_put(struct media_device *mdev) * * @mdev: pointer to struct &media_device * - * This function that will destroy the graph_mutex that is initialized in - * media_device_init(). Note that *only* drivers that do not manage releasing - * the memory of th media device itself call this function. This function is - * thus effectively DEPRECATED. + * This function will destroy the graph_mutex that is initialized in + * media_device_init(). Note that only drivers that do not have a proper release + * callback of the struct media_device call this function. This function is thus + * effectively DEPRECATED. */ void media_device_cleanup(struct media_device *mdev); diff --git a/include/media/media-devnode.h b/include/media/media-devnode.h index 898fa67ca090..5dee1acbc3f7 100644 --- a/include/media/media-devnode.h +++ b/include/media/media-devnode.h @@ -41,8 +41,7 @@ struct media_devnode; * @compat_ioctl: pointer to the function that will handle 32 bits userspace * calls to the ioctl() syscall on a Kernel compiled with 64 bits. * @open: pointer to the function that implements open() syscall - * @release: pointer to the function that will release the resources allocated - * by the @open function. + * @release: pointer to the function that implements release() syscall */ struct media_file_operations { struct module *owner; @@ -55,9 +54,54 @@ struct media_file_operations { int (*release) (struct file *); }; +/** + * struct media_devnode_cdev - Workaround for drivers not managing media device + * lifetime - character device + * + * Store the characeter device and whether this is a compatibility reference or + * not. struct media_devnode_cdev is embedded in either struct + * media_devnode_compat_ref or struct media_devnode. + * + * @cdev: the chracter device + * @is_compat_ref: Is this a compatibility reference or not? + */ +struct media_devnode_cdev { + struct cdev cdev; + bool is_compat_ref; +}; + +/** + * struct media_devnode_compat_ref - Workaround for drivers not managing media + * device lifetime - reference + * + * The purpose if this struct is to support drivers that do not manage the + * lifetime of their respective media devices to avoid the worst effects of + * this, namely an IOCTL call on an open file handle to a device that has been + * unbound causing a kernel oops systematically. This is not a fix. The proper, + * reliable way to handle this is to manage the resources used by the + * driver. This struct and its use can be removed once all drivers have been + * converted. + * + * @dev: struct device that remains in place as long as any reference remains + * @mcdev: the related character device + * @devnode: a pointer back to the devnode + * @release: pointer to the function that will release the resources + * allocated by the @open function. + * @registered: is this device registered? + */ +struct media_devnode_compat_ref { + struct device dev; + struct media_devnode_cdev mcdev; + struct media_devnode *devnode; + int (*release)(struct file *); + atomic_t registered; +}; + /** * struct media_devnode_fh - Media device node file handle * @devnode: pointer to the media device node + * @ref: media device compat ref, if the driver does not manage media + * device lifetime * * This structure serves as a base for per-file-handle data storage. Media * device node users embed media_devnode_fh in their custom file handle data @@ -67,18 +111,21 @@ struct media_file_operations { */ struct media_devnode_fh { struct media_devnode *devnode; + struct media_devnode_compat_ref *ref; }; /** * struct media_devnode - Media device node * @fops: pointer to struct &media_file_operations with media device ops * @dev: pointer to struct &device containing the media controller device - * @cdev: struct cdev pointer character device + * @mcdev: related to the character device * @parent: parent device * @minor: device node minor number * @flags: flags, combination of the ``MEDIA_FLAG_*`` constants * @release: release callback called at the end of ``media_devnode_release()`` * routine at media-device.c. + * @ref: reference for providing best effort memory safety in device + * removal * * This structure represents a media-related device node. * @@ -91,7 +138,7 @@ struct media_devnode { /* sysfs */ struct device dev; /* media device */ - struct cdev cdev; /* character device */ + struct media_devnode_cdev mcdev; /* character device + compat status */ struct device *parent; /* device parent */ /* device info */ @@ -100,10 +147,18 @@ struct media_devnode { /* callbacks */ void (*release)(struct media_devnode *devnode); + + /* compat reference */ + struct media_devnode_compat_ref *ref; }; +static inline struct device *media_devnode_dev(struct media_devnode *devnode) +{ + return devnode->ref ? &devnode->ref->dev : &devnode->dev; +} + /* dev to media_devnode */ -#define to_media_devnode(cd) container_of(cd, struct media_devnode, dev) +struct media_devnode *to_media_devnode(struct device *dev); /** * media_devnode_init - initialise a media devnode From patchwork Tue Mar 12 10:34:20 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sakari Ailus X-Patchwork-Id: 13589817 Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 3500C79DCD for ; Tue, 12 Mar 2024 10:34:43 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=192.198.163.17 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1710239684; cv=none; b=IScTCdG/FLlkYsBFWXGnSGyDPiUL7R8q14BS7eXUVvABaFYuwGegfZzXk9T9ALaUQxvKO4ZSslbZHyaDnUN6QCeWPs1awTAm4cy9k0xe1xNH2J5WAkw4kRktc//xDtBwq7em5lC36Fny+rNilBv6BGysivrZat1bJhiF9/RyBIA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1710239684; c=relaxed/simple; bh=55b8zBsGXYerQP0lfnydR27J6d7lXdm2jtthzLGldBI=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=qoyzsSfJidehEKPfo3CTTonIc/5qDSgEpzcxisDVjaIwVXebNLFp+oHCJrZ8LptmNYRDghaJYRetGi8ka4GuSfDKBSI2SkasT5mBPEqWUBga+G1/XNPgYRX9STPMmJIXE416w0hvxYb+Yfemqp3FM9TUqTTO69xvi5PFrAoTE+o= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.intel.com; spf=none smtp.mailfrom=linux.intel.com; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b=VkbUeAzm; arc=none smtp.client-ip=192.198.163.17 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.intel.com Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=linux.intel.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="VkbUeAzm" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1710239683; x=1741775683; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=55b8zBsGXYerQP0lfnydR27J6d7lXdm2jtthzLGldBI=; b=VkbUeAzmLJFyLLcWhvYXKYQukJFQKniCCX1TNQkgi34d6s9Auib1hiSl OGchl+2dj3S3FFdC+jkGKio3bJYER+RZpcnWlNG/9mGITIlIMVwlLQt91 BwynNyJQ+MkmQoyoBFH0Kt8Ihtwgs8/f8LFeWJ9EeWvnt2ugh8WEJAuN3 DXq5WG/xpr0U3nUECv3UkDdApwlqIXaI2AQabphqVqs1p1MfXQFJsL2Fa 2Metgz5uXZ6AvBrH9HZMT04gb8KNTI0aNhDB6Y0PH27kVN3eiaOJAL4QL gl85PXAA8uVwaeYlxWcQSfCjIUWGjnR9GzJ907sQCJ4EQ52b+bNdLQ9Be w==; X-IronPort-AV: E=McAfee;i="6600,9927,11010"; a="4794343" X-IronPort-AV: E=Sophos;i="6.07,119,1708416000"; d="scan'208";a="4794343" Received: from orviesa005.jf.intel.com ([10.64.159.145]) by fmvoesa111.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 12 Mar 2024 03:34:43 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.07,119,1708416000"; d="scan'208";a="16194117" Received: from turnipsi.fi.intel.com (HELO kekkonen.fi.intel.com) ([10.237.72.44]) by orviesa005-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 12 Mar 2024 03:34:42 -0700 Received: from svinhufvud.ger.corp.intel.com (localhost [IPv6:::1]) by kekkonen.fi.intel.com (Postfix) with ESMTP id 3B4881201F7; Tue, 12 Mar 2024 12:34:39 +0200 (EET) From: Sakari Ailus To: linux-media@vger.kernel.org Cc: laurent.pinchart@ideasonboard.com, hverkuil@xs4all.nl Subject: [PATCH v3 24/26] media: mc: Warn about drivers not releasing media device safely Date: Tue, 12 Mar 2024 12:34:20 +0200 Message-Id: <20240312103422.216484-25-sakari.ailus@linux.intel.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20240312103422.216484-1-sakari.ailus@linux.intel.com> References: <20240312103422.216484-1-sakari.ailus@linux.intel.com> Precedence: bulk X-Mailing-List: linux-media@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 The media device and associated resources may be released only when its memory is no longer used. Warn about drivers not doing this, but instead releasing the resources at driver unbind time. Signed-off-by: Sakari Ailus --- drivers/media/mc/mc-device.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/drivers/media/mc/mc-device.c b/drivers/media/mc/mc-device.c index dcd40fdab29d..15411682466f 100644 --- a/drivers/media/mc/mc-device.c +++ b/drivers/media/mc/mc-device.c @@ -822,6 +822,9 @@ int __must_check __media_device_register(struct media_device *mdev, ref = kzalloc(sizeof(*mdev->devnode.ref), GFP_KERNEL); if (!ref) return -ENOMEM; + + dev_warn(mdev->dev, + "Set mdev release op to safely release resources!\n"); } /* Register the device node. */ From patchwork Tue Mar 12 10:34:21 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sakari Ailus X-Patchwork-Id: 13589819 Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 8D22A79DDD for ; Tue, 12 Mar 2024 10:34:43 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=192.198.163.17 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1710239685; cv=none; b=HOz+nGi52Wgd4pWbZOrUaRJsN/CIaxrCGaJ///UWr4eF0Cg3TlCkP8KCgPL8exZVE9ddOYBfu6vnyVrPXlLGnq0IehhKiRYgppVcOETNP+TQKtti8OVq6tqK2GGO4d+QDze/YipBgLKiYasVqFdyzVAldUeo5zFk0kqZxJ8tYpY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1710239685; c=relaxed/simple; bh=GnXZ+uykzjm2yeKEIqJ8UlgN7XyR0uSitA/90So7sNo=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=k482UfG4ROZx2Qm73Qp2f+MKZnrLITPeiOC1GPugeFA/+plf1GqVyoiveC3Dz2c1FzQwDER5aNOCLkaupYL3u5wj4BG8sw2kByGfTDJws7D/z2xjQ7dSm+TQKFcT/yAt20bCxHzqwmCz/lGoHDErUn8vULIv8Fn3GOs7Xc21j5E= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.intel.com; spf=none smtp.mailfrom=linux.intel.com; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b=F8RYpnuZ; arc=none smtp.client-ip=192.198.163.17 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.intel.com Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=linux.intel.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="F8RYpnuZ" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1710239684; x=1741775684; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=GnXZ+uykzjm2yeKEIqJ8UlgN7XyR0uSitA/90So7sNo=; b=F8RYpnuZqBgb7/2AY7cQodIrBEY3i8Kjl4knWBmJ9OfpdBBr0oaNPYN2 bZ90vhXTitVytaHajSoCl6j+JQPnN3QWSJGrpbE/ltZ/x/WXakt2I0SrY PBMmpjHYR6BKP3o1rtUkIAh9ZrQNRPYb5noxcf8ZydbB+fBaADhEwwdCS KUUWXbNtkfHRMJFisyQQnvVBFgsHz8BH8APUlm3ba/0DAbMwbEpa8AOK8 RvmQhm6Ca69Hg9DTVr9LQW68rkt2eDqO5ocBar/avJUf55iZ1ATS16GLw x1q5VCeaykvlgaGvozIV2X2y+FhCqmMQakemPB+GycCJIDC82IxuAE8AX A==; X-IronPort-AV: E=McAfee;i="6600,9927,11010"; a="4794346" X-IronPort-AV: E=Sophos;i="6.07,119,1708416000"; d="scan'208";a="4794346" Received: from orviesa005.jf.intel.com ([10.64.159.145]) by fmvoesa111.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 12 Mar 2024 03:34:43 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.07,119,1708416000"; d="scan'208";a="16194119" Received: from turnipsi.fi.intel.com (HELO kekkonen.fi.intel.com) ([10.237.72.44]) by orviesa005-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 12 Mar 2024 03:34:43 -0700 Received: from svinhufvud.ger.corp.intel.com (localhost [IPv6:::1]) by kekkonen.fi.intel.com (Postfix) with ESMTP id D0A8A11F819; Tue, 12 Mar 2024 12:34:39 +0200 (EET) From: Sakari Ailus To: linux-media@vger.kernel.org Cc: laurent.pinchart@ideasonboard.com, hverkuil@xs4all.nl Subject: [PATCH v3 25/26] media: mc: Enforce one-time registration Date: Tue, 12 Mar 2024 12:34:21 +0200 Message-Id: <20240312103422.216484-26-sakari.ailus@linux.intel.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20240312103422.216484-1-sakari.ailus@linux.intel.com> References: <20240312103422.216484-1-sakari.ailus@linux.intel.com> Precedence: bulk X-Mailing-List: linux-media@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 A media devnode may be registered only once. Enforce this by setting the minor to -1 in init. Registration initialises the character device and sets up the device name. These should take place only once during the lifetime of the media device. Signed-off-by: Sakari Ailus --- drivers/media/mc/mc-devnode.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/drivers/media/mc/mc-devnode.c b/drivers/media/mc/mc-devnode.c index e2a30b21e6c8..8742bd7a3521 100644 --- a/drivers/media/mc/mc-devnode.c +++ b/drivers/media/mc/mc-devnode.c @@ -240,6 +240,7 @@ void media_devnode_init(struct media_devnode *devnode) { device_initialize(&devnode->dev); devnode->dev.release = media_devnode_release; + devnode->minor = -1; } int __must_check media_devnode_register(struct media_devnode *devnode, @@ -251,6 +252,9 @@ int __must_check media_devnode_register(struct media_devnode *devnode, int minor; int ret; + if (devnode->minor != -1) + return -EINVAL; + /* Part 1: Find a free minor number */ mutex_lock(&media_devnode_lock); minor = find_first_zero_bit(media_devnode_nums, MEDIA_NUM_DEVICES); From patchwork Tue Mar 12 10:34:22 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sakari Ailus X-Patchwork-Id: 13589820 Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id BC3A57A127 for ; Tue, 12 Mar 2024 10:34:44 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=192.198.163.17 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1710239686; cv=none; b=UVbSfyKsSTUM5oXCZLrWJWP2xS9SHvuPAZCm+C6Z6KeowS9vH9BbmSlAYRnV9Wf2uugVqtPTFluog0NIOaUkMr/MJ4otvBZDKC5UZ3LPTnmfkUvFosuAZGZE08t3hXBJMdm15TvIgtrvZXXPJfmCc56sNEczbgnjjiksm+0l4+4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1710239686; c=relaxed/simple; bh=1xlSTuEdeFyONt5K7qt6F4QtCG63Eauf7PTfPM9vWqk=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=Y9N+89iF8fUdCHYCTRBJxzWbG/5K4v23BvH9UC+d9Q07qwPwoX1BA4SOal5dZA7I4c61QpV1a7oTXQH9tz+Qp5HFsfg34BkNI4/SRkHfhTKZjTHMGbdA8G/PvzoRmk1Rmlq4h1U6KCNFG5jzms5XPJPZc6N4HVXcn2ATXFNYbRg= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.intel.com; spf=none smtp.mailfrom=linux.intel.com; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b=lILlt1sN; arc=none smtp.client-ip=192.198.163.17 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.intel.com Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=linux.intel.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="lILlt1sN" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1710239685; x=1741775685; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=1xlSTuEdeFyONt5K7qt6F4QtCG63Eauf7PTfPM9vWqk=; b=lILlt1sNxDA606q6ZjB/Vtvm8xu3bRDRMy4twNI/gcwckhMoO+ByRPyW vK4DMdNXFdugKitTs6lO+CRewL0KuJ2Ls4xg6OPsTOPwHYXSH4K/f9qb0 +422m+TVZfs5015LK39LXmHHnb0R1URw3ikwRiW+ZN7hFI+M1xwxLsIXr 3nD3UdHWBdIVcTzDOfi9ePUD1fT3vg36Hdl5ATp4o/C8atJRtlBgGjHHD MI2ldDqxinv0gPH/33mCX6e6Io7QyUhNFtrsoUjd7RQSJOa7vooi2/3f/ k6ASCNCKF7nS+h1fOA3JLOFWTF8Z4IRc99Wu2d7l/I5jqqiv4V3/jJv10 A==; X-IronPort-AV: E=McAfee;i="6600,9927,11010"; a="4794349" X-IronPort-AV: E=Sophos;i="6.07,119,1708416000"; d="scan'208";a="4794349" Received: from orviesa005.jf.intel.com ([10.64.159.145]) by fmvoesa111.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 12 Mar 2024 03:34:44 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.07,119,1708416000"; d="scan'208";a="16194124" Received: from turnipsi.fi.intel.com (HELO kekkonen.fi.intel.com) ([10.237.72.44]) by orviesa005-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 12 Mar 2024 03:34:43 -0700 Received: from svinhufvud.ger.corp.intel.com (localhost [IPv6:::1]) by kekkonen.fi.intel.com (Postfix) with ESMTP id 6884A11F81D; Tue, 12 Mar 2024 12:34:40 +0200 (EET) From: Sakari Ailus To: linux-media@vger.kernel.org Cc: laurent.pinchart@ideasonboard.com, hverkuil@xs4all.nl Subject: [PATCH v3 26/26] media: Documentation: Document media device memory safety helper Date: Tue, 12 Mar 2024 12:34:22 +0200 Message-Id: <20240312103422.216484-27-sakari.ailus@linux.intel.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20240312103422.216484-1-sakari.ailus@linux.intel.com> References: <20240312103422.216484-1-sakari.ailus@linux.intel.com> Precedence: bulk X-Mailing-List: linux-media@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Document how the best effort memory safety helper for accessing media device works, and that drivers should be converted to refcount the media device. Signed-off-by: Sakari Ailus --- drivers/media/mc/mc-devnode.c | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/drivers/media/mc/mc-devnode.c b/drivers/media/mc/mc-devnode.c index 8742bd7a3521..e5ae187246d6 100644 --- a/drivers/media/mc/mc-devnode.c +++ b/drivers/media/mc/mc-devnode.c @@ -243,6 +243,32 @@ void media_devnode_init(struct media_devnode *devnode) devnode->minor = -1; } +/* + * Best effort media device lifetime management for old drivers + * + * Drivers that do not manage the lifetime of the media device are provided with + * a best effort lifetime management support. This means that as the driver does + * not release the media device once all users are gone but when the device is + * unbound, there are bound to be (brief) moments when released memory may get + * accessed. All drivers should be converted to release their memory at a safe + * time, i.e. provide a release callback in struct media_file_operations to do + * so. This is especially important for drivers for devices that are + * unpluggable, e.g. USB devices. + * + * A second struct device is used to manage the lifetime of a helper object, + * struct media_devnode_compat_ref. For a media device, one is initialised in + * media_devnode_register and put in media_devnode_unregister. This object is + * also used as the device of the media character device so file handles to the + * media device have a reference to this object. When the media device is + * released, any file handle retains a reference to this helper that also + * contains the media device's registration status. If a media device is + * released and a user space process attempts to access the file handle, an + * error is returned. + * + * The struct device in struct media_devnode is put at media_device_cleanup and + * uses an empty release callback, reflecting the expectation the driver will + * release the memory of the media device at unbind time. + */ int __must_check media_devnode_register(struct media_devnode *devnode, struct module *owner) {