From patchwork Wed Feb 1 21:45:10 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sakari Ailus X-Patchwork-Id: 13125013 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id B29FAC05027 for ; Wed, 1 Feb 2023 21:45:46 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229662AbjBAVpq (ORCPT ); Wed, 1 Feb 2023 16:45:46 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60868 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229510AbjBAVpm (ORCPT ); Wed, 1 Feb 2023 16:45:42 -0500 Received: from mga03.intel.com (mga03.intel.com [134.134.136.65]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id BA65065346 for ; Wed, 1 Feb 2023 13:45:41 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1675287941; x=1706823941; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=g+bFFGgu3eqZtS1KL3A/+x8X+w49RNraDxINzuRWRw8=; b=Z5vpVQi7qwiYtQYorsuUroMc5ZlC85wtg4Gyz6hCEH9GnCycn888T5vx lBUBO7RdhFD3n2OWhAqpwvWzIUW7i+rpMCCEyiOyp3T9WdpKlzDaykXf2 uDDn9KJ7o9I69QMmN7PMWPJPTadelitoUzHEUnwizpwBbsVLBXt8sLHt/ EF4aTugVciu4VQIAmsK0PVKys/oCpV1tM4e1WC40M/jtWxyWSbfkTVj+k j01UQz1MoLOvaihY///D7S+2lqmaNdLQqNbiRgzhi1n26QzLlVf1loWgf 80Jnb8Man4g9ALqqe9s+/bag2UMZoA0myK43R3QBgQP+CjWESOMRYJ9lE w==; X-IronPort-AV: E=McAfee;i="6500,9779,10608"; a="330415600" X-IronPort-AV: E=Sophos;i="5.97,265,1669104000"; d="scan'208";a="330415600" Received: from orsmga008.jf.intel.com ([10.7.209.65]) by orsmga103.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 01 Feb 2023 13:45:41 -0800 X-IronPort-AV: E=McAfee;i="6500,9779,10608"; a="695527290" X-IronPort-AV: E=Sophos;i="5.97,265,1669104000"; d="scan'208";a="695527290" Received: from turnipsi.fi.intel.com (HELO kekkonen.fi.intel.com) ([10.237.72.44]) by orsmga008-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 01 Feb 2023 13:45:40 -0800 Received: from svinhufvud.ger.corp.intel.com (localhost [IPv6:::1]) by kekkonen.fi.intel.com (Postfix) with ESMTP id 1069712276E; Wed, 1 Feb 2023 23:45:36 +0200 (EET) From: Sakari Ailus To: linux-media@vger.kernel.org Cc: laurent.pinchart@ideasonboard.com, hverkuil@xs4all.nl Subject: [PATCH 01/26] Revert "[media] media: fix media devnode ioctl/syscall and unregister race" Date: Wed, 1 Feb 2023 23:45:10 +0200 Message-Id: <20230201214535.347075-2-sakari.ailus@linux.intel.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20230201214535.347075-1-sakari.ailus@linux.intel.com> References: <20230201214535.347075-1-sakari.ailus@linux.intel.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-media@vger.kernel.org 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 --- 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 25020d58eb06..013d54e1a55a 100644 --- a/drivers/media/mc/mc-device.c +++ b/drivers/media/mc/mc-device.c @@ -745,7 +745,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; } @@ -802,9 +801,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); @@ -829,10 +825,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 680fbb3a9340..0ab33214d243 100644 --- a/drivers/media/mc/mc-devnode.c +++ b/drivers/media/mc/mc-devnode.c @@ -267,7 +267,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)) @@ -275,12 +275,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 Wed Feb 1 21:45:11 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sakari Ailus X-Patchwork-Id: 13125014 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id B01BAC636D4 for ; Wed, 1 Feb 2023 21:45:47 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229666AbjBAVpq (ORCPT ); Wed, 1 Feb 2023 16:45:46 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60884 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229548AbjBAVpo (ORCPT ); Wed, 1 Feb 2023 16:45:44 -0500 Received: from mga03.intel.com (mga03.intel.com [134.134.136.65]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 3268E66ECC for ; Wed, 1 Feb 2023 13:45:43 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1675287943; x=1706823943; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=/JCObSNJwyqezd0azm+ENLtFvcxhLnXT5MNmwkW4oqA=; b=a9wNCx7HVKTaCZU3EJ9UHF/JyAlVqk1kK3cX5VZNRfbW3wx+EquNhG5Q lLY/86UFmaV5Evu6Tr/H7qaxQr8kGpT2JVWfilka8z2GmbhJLetytbP7N e356PnG8eR4o/6jkolhTZrVw6HokRyHtInAMESAwwKM08C7EjBBjRIfQu ilJmH6AqN5VsiV3GhTZi8h99Iu5EY6L6SFX3JQQu4+AHJ6CT9Koql5LOO HCkCWpaEKeTx36gnqjGJsKhGBJ99aWohsH87XccBtxGiSulEYkusjMNFb 9FJUKrFUNeP2POn4AvSZZEvMkboRV0YhZSysKfa0JMfmtAKS3rHpGntiu g==; X-IronPort-AV: E=McAfee;i="6500,9779,10608"; a="330415607" X-IronPort-AV: E=Sophos;i="5.97,265,1669104000"; d="scan'208";a="330415607" Received: from orsmga008.jf.intel.com ([10.7.209.65]) by orsmga103.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 01 Feb 2023 13:45:41 -0800 X-IronPort-AV: E=McAfee;i="6500,9779,10608"; a="695527292" X-IronPort-AV: E=Sophos;i="5.97,265,1669104000"; d="scan'208";a="695527292" Received: from turnipsi.fi.intel.com (HELO kekkonen.fi.intel.com) ([10.237.72.44]) by orsmga008-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 01 Feb 2023 13:45:40 -0800 Received: from svinhufvud.ger.corp.intel.com (localhost [IPv6:::1]) by kekkonen.fi.intel.com (Postfix) with ESMTP id C1D51122774; Wed, 1 Feb 2023 23:45:37 +0200 (EET) From: Sakari Ailus To: linux-media@vger.kernel.org Cc: laurent.pinchart@ideasonboard.com, hverkuil@xs4all.nl Subject: [PATCH 02/26] Revert "media: utilize new cdev_device_add helper function" Date: Wed, 1 Feb 2023 23:45:11 +0200 Message-Id: <20230201214535.347075-3-sakari.ailus@linux.intel.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20230201214535.347075-1-sakari.ailus@linux.intel.com> References: <20230201214535.347075-1-sakari.ailus@linux.intel.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-media@vger.kernel.org This reverts commit 857313e51006ff51524579bcd8808b70f9a80812. This patch is temporarily reverted for internal rework. Signed-off-by: Sakari Ailus --- 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 af358e901b5f..0eb54aceff13 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); @@ -193,7 +200,8 @@ static void cec_devnode_unregister(struct cec_adapter *adap) __cec_s_log_addrs(adap, NULL, false); 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 0ab33214d243..740573552e5d 100644 --- a/drivers/media/mc/mc-devnode.c +++ b/drivers/media/mc/mc-devnode.c @@ -240,23 +240,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); @@ -276,9 +286,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 Wed Feb 1 21:45:12 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sakari Ailus X-Patchwork-Id: 13125017 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 42E83C636D4 for ; Wed, 1 Feb 2023 21:45:53 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229471AbjBAVpw (ORCPT ); Wed, 1 Feb 2023 16:45:52 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60898 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229838AbjBAVpr (ORCPT ); Wed, 1 Feb 2023 16:45:47 -0500 Received: from mga03.intel.com (mga03.intel.com [134.134.136.65]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 29C1965346 for ; Wed, 1 Feb 2023 13:45:45 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1675287945; x=1706823945; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=2XOxk44DL1NJEhniZ2VaMl9pEjAT0XcPLoBwJ0UWIM0=; b=KIl0YnLRIdYcrr+RG6J0w5Nuu72KItWAtEtkE92+wXForV6yCqvdAmh/ sNS6a/hGH6EqPrAnJDclXeUJDtYvW7LdkOhAYzKbuazQEUd+Lpw/mvPPc JF9Sf+uYHD88/e5gPxxhUMq2Po34m9i3/Xlo4dLqUXKhGK2sw345uAO1A khZhXwFbRHoUkOqqZGqxsec9CwF+Qna4XrF5cmPlASQSMxBd6oNmkuPoC dsyvy4IjrzS1mi/SzgfVc+ZgT4SLXf1bQWJ6z4GGo8DBgg17Yoo+nUzNA rooQ5Gvl9mvgolGJpgGvRwcK7a7uLlODvv1F1EhPucSaLZmaPL7jSFvOA g==; X-IronPort-AV: E=McAfee;i="6500,9779,10608"; a="330415614" X-IronPort-AV: E=Sophos;i="5.97,265,1669104000"; d="scan'208";a="330415614" Received: from orsmga008.jf.intel.com ([10.7.209.65]) by orsmga103.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 01 Feb 2023 13:45:43 -0800 X-IronPort-AV: E=McAfee;i="6500,9779,10608"; a="695527293" X-IronPort-AV: E=Sophos;i="5.97,265,1669104000"; d="scan'208";a="695527293" Received: from turnipsi.fi.intel.com (HELO kekkonen.fi.intel.com) ([10.237.72.44]) by orsmga008-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 01 Feb 2023 13:45:41 -0800 Received: from svinhufvud.ger.corp.intel.com (localhost [IPv6:::1]) by kekkonen.fi.intel.com (Postfix) with ESMTP id ACA8C12279A; Wed, 1 Feb 2023 23:45:38 +0200 (EET) From: Sakari Ailus To: linux-media@vger.kernel.org Cc: laurent.pinchart@ideasonboard.com, hverkuil@xs4all.nl Subject: [PATCH 03/26] Revert "[media] media: fix use-after-free in cdev_put() when app exits after driver unbind" Date: Wed, 1 Feb 2023 23:45:12 +0200 Message-Id: <20230201214535.347075-4-sakari.ailus@linux.intel.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20230201214535.347075-1-sakari.ailus@linux.intel.com> References: <20230201214535.347075-1-sakari.ailus@linux.intel.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-media@vger.kernel.org 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. Signed-off-by: Sakari Ailus --- drivers/media/mc/mc-device.c | 6 ++--- drivers/media/mc/mc-devnode.c | 48 ++++++++++++++--------------------- 2 files changed, 21 insertions(+), 33 deletions(-) diff --git a/drivers/media/mc/mc-device.c b/drivers/media/mc/mc-device.c index 013d54e1a55a..b6640e2c8a4c 100644 --- a/drivers/media/mc/mc-device.c +++ b/drivers/media/mc/mc-device.c @@ -736,16 +736,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; } @@ -829,8 +829,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 740573552e5d..1e1792c3ae3f 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 struct bus_type media_bus_type = { @@ -189,8 +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); - - pr_debug("%s: Media Release\n", __func__); return 0; } @@ -221,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; } @@ -231,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. */ @@ -265,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; } @@ -285,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 Wed Feb 1 21:45:13 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sakari Ailus X-Patchwork-Id: 13125016 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id BF4F8C636D7 for ; Wed, 1 Feb 2023 21:45:51 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230208AbjBAVpu (ORCPT ); Wed, 1 Feb 2023 16:45:50 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60900 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229774AbjBAVpr (ORCPT ); Wed, 1 Feb 2023 16:45:47 -0500 Received: from mga03.intel.com (mga03.intel.com [134.134.136.65]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 600FE6601A for ; Wed, 1 Feb 2023 13:45:45 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1675287945; x=1706823945; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=xloCx4/hBtlKlBrilJK3QgZzZS9Y+UBQ1Ab1wxqk2Pc=; b=GdHnB2Grxu+ZXSLbvi+ZUpKK5b9xvyW7p5FG3ptcSNNUpEOtOvnOSMmi KbGm3Kolwm1F0/btF1Bti9COePwSK96LbcaHXaUbOShzlgv3o/OeBc65p Nhwv5N7GYxvC0erQyB0kCg+990D3q145rhLUNxktbAi1lT6xyQct2fRxC 6F/zIlWisL12gpwxoLrlxQguGI1l9s80Qx4mQJ50EPh4hs6aL7JMcS+br TOxcfxjGwWzld0LpgR9cQKGjX1SvTzVXh43xpq5z+XiroeahFcyjPJUC6 e3/opnHTvzLcJbX335TJS4uMSeg7zVawvin5bVeaAYKdMGIifEtxlZPYc A==; X-IronPort-AV: E=McAfee;i="6500,9779,10608"; a="330415619" X-IronPort-AV: E=Sophos;i="5.97,265,1669104000"; d="scan'208";a="330415619" Received: from orsmga008.jf.intel.com ([10.7.209.65]) by orsmga103.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 01 Feb 2023 13:45:44 -0800 X-IronPort-AV: E=McAfee;i="6500,9779,10608"; a="695527294" X-IronPort-AV: E=Sophos;i="5.97,265,1669104000"; d="scan'208";a="695527294" Received: from turnipsi.fi.intel.com (HELO kekkonen.fi.intel.com) ([10.237.72.44]) by orsmga008-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 01 Feb 2023 13:45:42 -0800 Received: from svinhufvud.ger.corp.intel.com (localhost [IPv6:::1]) by kekkonen.fi.intel.com (Postfix) with ESMTP id E7B691227A0; Wed, 1 Feb 2023 23:45:39 +0200 (EET) From: Sakari Ailus To: linux-media@vger.kernel.org Cc: laurent.pinchart@ideasonboard.com, hverkuil@xs4all.nl Subject: [PATCH 04/26] media: utilize new cdev_device_add helper function Date: Wed, 1 Feb 2023 23:45:13 +0200 Message-Id: <20230201214535.347075-5-sakari.ailus@linux.intel.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20230201214535.347075-1-sakari.ailus@linux.intel.com> References: <20230201214535.347075-1-sakari.ailus@linux.intel.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-media@vger.kernel.org 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 Acked-by: Hans Verkuil Signed-off-by: Greg Kroah-Hartman --- 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 0eb54aceff13..57023d020973 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); @@ -200,8 +193,7 @@ static void cec_devnode_unregister(struct cec_adapter *adap) __cec_s_log_addrs(adap, NULL, false); 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 1e1792c3ae3f..fabcd646679b 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 Wed Feb 1 21:45:14 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sakari Ailus X-Patchwork-Id: 13125018 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id A1480C63797 for ; Wed, 1 Feb 2023 21:45:54 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229838AbjBAVpx (ORCPT ); Wed, 1 Feb 2023 16:45:53 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60910 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230017AbjBAVpt (ORCPT ); Wed, 1 Feb 2023 16:45:49 -0500 Received: from mga03.intel.com (mga03.intel.com [134.134.136.65]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id AC7A166ED7 for ; Wed, 1 Feb 2023 13:45:46 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1675287946; x=1706823946; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=0G6KgED8Kb4CBMHP/HHyLyyV3uHCnoxQ7MBu3GWLKNc=; b=mlRYtpGPuvlIwrJW3oO/3/vzWfLF2IfWw4kIY4K2Nsa61yBN5mP4d4nz Eu/1SeC4prlQ/2YFPYFXz0KL3sk4Et47Tw0qB6YbeQzakzRixmvwJosxg zgt7R5z2CdweyPdECUBzgQ5ANu70RTCcU5+BJW/qeSyXspXToGrFJrdqT 6iK/YYoFUDpgmCQWX8BKAhkXwK4VzeppXxG7bPnKXiMQolCyjhjdadH3K Oi+fAq7duTRYLMEV5seeJtT+d1KUJU5vvt7UYI9hVUoqKJ0Vt3/meh5vB TdicmnVT+DuawLVdlrV9nP1ymvMyBdL9QlD3eYgbQ6gvaNdXhXaA5d2mb A==; X-IronPort-AV: E=McAfee;i="6500,9779,10608"; a="330415622" X-IronPort-AV: E=Sophos;i="5.97,265,1669104000"; d="scan'208";a="330415622" Received: from orsmga008.jf.intel.com ([10.7.209.65]) by orsmga103.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 01 Feb 2023 13:45:45 -0800 X-IronPort-AV: E=McAfee;i="6500,9779,10608"; a="695527296" X-IronPort-AV: E=Sophos;i="5.97,265,1669104000"; d="scan'208";a="695527296" Received: from turnipsi.fi.intel.com (HELO kekkonen.fi.intel.com) ([10.237.72.44]) by orsmga008-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 01 Feb 2023 13:45:43 -0800 Received: from svinhufvud.ger.corp.intel.com (localhost [IPv6:::1]) by kekkonen.fi.intel.com (Postfix) with ESMTP id 8A5CC1227A1; Wed, 1 Feb 2023 23:45:40 +0200 (EET) From: Sakari Ailus To: linux-media@vger.kernel.org Cc: laurent.pinchart@ideasonboard.com, hverkuil@xs4all.nl Subject: [PATCH 05/26] Revert "media: uvcvideo: Refactor teardown of uvc on USB disconnect" Date: Wed, 1 Feb 2023 23:45:14 +0200 Message-Id: <20230201214535.347075-6-sakari.ailus@linux.intel.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20230201214535.347075-1-sakari.ailus@linux.intel.com> References: <20230201214535.347075-1-sakari.ailus@linux.intel.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-media@vger.kernel.org This reverts commit 10e1fdb95809ed21406f53b5b4f064673a1b9ceb. Temporarily revert this patch to revert a dependent patch. The patch is re-applied later, rebased on the revert. Signed-off-by: Sakari Ailus --- drivers/media/usb/uvc/uvc_driver.c | 13 ++++--------- drivers/media/usb/uvc/uvc_status.c | 12 ++++-------- drivers/media/usb/uvc/uvcvideo.h | 1 - 3 files changed, 8 insertions(+), 18 deletions(-) diff --git a/drivers/media/usb/uvc/uvc_driver.c b/drivers/media/usb/uvc/uvc_driver.c index 11f3d716b5bf..d414b2221dae 100644 --- a/drivers/media/usb/uvc/uvc_driver.c +++ b/drivers/media/usb/uvc/uvc_driver.c @@ -1834,7 +1834,11 @@ static void uvc_delete(struct kref *kref) usb_put_intf(dev->intf); usb_put_dev(dev->udev); + if (dev->vdev.dev) + v4l2_device_unregister(&dev->vdev); #ifdef CONFIG_MEDIA_CONTROLLER + if (media_devnode_is_registered(dev->mdev.devnode)) + media_device_unregister(&dev->mdev); media_device_cleanup(&dev->mdev); #endif @@ -1891,15 +1895,6 @@ static void uvc_unregister_video(struct uvc_device *dev) uvc_debugfs_cleanup_stream(stream); } - - uvc_status_unregister(dev); - - if (dev->vdev.dev) - v4l2_device_unregister(&dev->vdev); -#ifdef CONFIG_MEDIA_CONTROLLER - if (media_devnode_is_registered(dev->mdev.devnode)) - media_device_unregister(&dev->mdev); -#endif } int uvc_register_video_device(struct uvc_device *dev, diff --git a/drivers/media/usb/uvc/uvc_status.c b/drivers/media/usb/uvc/uvc_status.c index a78a88c710e2..015be0886801 100644 --- a/drivers/media/usb/uvc/uvc_status.c +++ b/drivers/media/usb/uvc/uvc_status.c @@ -73,7 +73,7 @@ static int uvc_input_init(struct uvc_device *dev) return ret; } -static void uvc_input_unregister(struct uvc_device *dev) +static void uvc_input_cleanup(struct uvc_device *dev) { if (dev->input) input_unregister_device(dev->input); @@ -90,7 +90,7 @@ static void uvc_input_report_key(struct uvc_device *dev, unsigned int code, #else #define uvc_input_init(dev) -#define uvc_input_unregister(dev) +#define uvc_input_cleanup(dev) #define uvc_input_report_key(dev, code, value) #endif /* CONFIG_USB_VIDEO_CLASS_INPUT_EVDEV */ @@ -290,16 +290,12 @@ int uvc_status_init(struct uvc_device *dev) return 0; } -void uvc_status_unregister(struct uvc_device *dev) -{ - usb_kill_urb(dev->int_urb); - uvc_input_unregister(dev); -} - void uvc_status_cleanup(struct uvc_device *dev) { + usb_kill_urb(dev->int_urb); usb_free_urb(dev->int_urb); kfree(dev->status); + uvc_input_cleanup(dev); } int uvc_status_start(struct uvc_device *dev, gfp_t flags) diff --git a/drivers/media/usb/uvc/uvcvideo.h b/drivers/media/usb/uvc/uvcvideo.h index 9a596c8d894a..80de6f8395c8 100644 --- a/drivers/media/usb/uvc/uvcvideo.h +++ b/drivers/media/usb/uvc/uvcvideo.h @@ -742,7 +742,6 @@ int uvc_register_video_device(struct uvc_device *dev, /* Status */ int uvc_status_init(struct uvc_device *dev); -void uvc_status_unregister(struct uvc_device *dev); void uvc_status_cleanup(struct uvc_device *dev); int uvc_status_start(struct uvc_device *dev, gfp_t flags); void uvc_status_stop(struct uvc_device *dev); From patchwork Wed Feb 1 21:45:15 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sakari Ailus X-Patchwork-Id: 13125019 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id C01D0C636D7 for ; Wed, 1 Feb 2023 21:45:55 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230444AbjBAVpy (ORCPT ); Wed, 1 Feb 2023 16:45:54 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60922 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230369AbjBAVpv (ORCPT ); Wed, 1 Feb 2023 16:45:51 -0500 Received: from mga03.intel.com (mga03.intel.com [134.134.136.65]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8B8CA66FAE for ; Wed, 1 Feb 2023 13:45:46 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1675287947; x=1706823947; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=qRPSCOKHkvRc/COhkncP3OkmK5KQZ1gTt6vbocLSYkk=; b=GoFEZKXY9aUvDlP9o/7BR+Em7w2KTa/ED1C/KlXLgtzEME/jCByJdFSq GjT50+bEzWbOoCR4/GDXQSCmH4tn2BPIcbcExOAn/nUJtoATNP7DoekVT 7l/ElkJ96Ce/7TpY3HqfFxZcug5bRAO8d3+h5mVGcW7LKjkLwmHysnUnY TNlP9vkIUtIchYlZye3B8uLvO6qX9nvyKtAiVK8gmF/cmCod3or0upHPz BpSc81JFBbfXfKq1KW/FKHM0Acs5Gz+0mvkVjd+DzydnOhlrBlRnJsL2/ SndwkE0LanGP4M2Ne9pvJMFNF1bCWPJdh7tyHdho/unyYbFsgmT6ziOeu g==; X-IronPort-AV: E=McAfee;i="6500,9779,10608"; a="330415625" X-IronPort-AV: E=Sophos;i="5.97,265,1669104000"; d="scan'208";a="330415625" Received: from orsmga008.jf.intel.com ([10.7.209.65]) by orsmga103.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 01 Feb 2023 13:45:45 -0800 X-IronPort-AV: E=McAfee;i="6500,9779,10608"; a="695527298" X-IronPort-AV: E=Sophos;i="5.97,265,1669104000"; d="scan'208";a="695527298" Received: from turnipsi.fi.intel.com (HELO kekkonen.fi.intel.com) ([10.237.72.44]) by orsmga008-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 01 Feb 2023 13:45:44 -0800 Received: from svinhufvud.ger.corp.intel.com (localhost [IPv6:::1]) by kekkonen.fi.intel.com (Postfix) with ESMTP id 2D72D1227F2; Wed, 1 Feb 2023 23:45:41 +0200 (EET) From: Sakari Ailus To: linux-media@vger.kernel.org Cc: laurent.pinchart@ideasonboard.com, hverkuil@xs4all.nl Subject: [PATCH 06/26] Revert "[media] media-device: dynamically allocate struct media_devnode" Date: Wed, 1 Feb 2023 23:45:15 +0200 Message-Id: <20230201214535.347075-7-sakari.ailus@linux.intel.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20230201214535.347075-1-sakari.ailus@linux.intel.com> References: <20230201214535.347075-1-sakari.ailus@linux.intel.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-media@vger.kernel.org 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. Signed-off-by: Sakari Ailus --- 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 +------ 7 files changed, 25 insertions(+), 54 deletions(-) diff --git a/drivers/media/mc/mc-device.c b/drivers/media/mc/mc-device.c index b6640e2c8a4c..5c2e65717c19 100644 --- a/drivers/media/mc/mc-device.c +++ b/drivers/media/mc/mc-device.c @@ -439,7 +439,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; @@ -523,7 +523,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) { @@ -559,8 +559,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); } @@ -718,34 +717,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; } @@ -796,7 +784,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; } @@ -823,13 +811,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 fabcd646679b..ce93ab9be676 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 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 877e85a451cb..0876b267568d 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 d414b2221dae..e13b9e012e05 100644 --- a/drivers/media/usb/uvc/uvc_driver.c +++ b/drivers/media/usb/uvc/uvc_driver.c @@ -1837,7 +1837,7 @@ static void uvc_delete(struct kref *kref) 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); media_device_cleanup(&dev->mdev); #endif diff --git a/drivers/staging/media/sunxi/cedrus/cedrus.c b/drivers/staging/media/sunxi/cedrus/cedrus.c index a43d5ff66716..41d3c84becfe 100644 --- a/drivers/staging/media/sunxi/cedrus/cedrus.c +++ b/drivers/staging/media/sunxi/cedrus/cedrus.c @@ -547,7 +547,7 @@ static int cedrus_remove(struct platform_device *pdev) { struct cedrus_dev *dev = platform_get_drvdata(pdev); - 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 86716ee7cc6c..a33820075aa4 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); } From patchwork Wed Feb 1 21:45:16 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sakari Ailus X-Patchwork-Id: 13125020 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 8E820C636D4 for ; Wed, 1 Feb 2023 21:45:57 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230462AbjBAVpz (ORCPT ); Wed, 1 Feb 2023 16:45:55 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60922 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230432AbjBAVpv (ORCPT ); Wed, 1 Feb 2023 16:45:51 -0500 Received: from mga03.intel.com (mga03.intel.com [134.134.136.65]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id AA7B4677B1 for ; Wed, 1 Feb 2023 13:45:48 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1675287949; x=1706823949; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=QWK7YEarN+YlvW1WGbMgZtp1wGCUvLK3q1yiKO1PcpU=; b=eFpG3ftz47Wn1qhn9YPSj9jESDDyHWLkU/zv3ze148OE/LQikj2VcFMs 81PieAVwUxKiFpQDdPOx2vXVn2uWiQux+nxbvX8dUtIHsuXhkanm880Mv RnDRsRwK9EjGYfXgyhX0dwRPjwpUqY0ziZvmVg3/dd7ogiWFyibePqt2U 73+FEc5C1U8Ad/SXBpmuYcQ3LobewtsFD4Xri1Jrb08fnLa3zQn5QvNFS uzaQtnyqg5Oz42SrsEDIkvfhOG+/Pk8EPVsDvRUr5EjyrjlEX2KpgjijE iHtWDm2CzBjQpoDMey89Mdw1bPeKeFrsm9FQNcd7cSLmxcH743VuyoHbQ w==; X-IronPort-AV: E=McAfee;i="6500,9779,10608"; a="330415629" X-IronPort-AV: E=Sophos;i="5.97,265,1669104000"; d="scan'208";a="330415629" Received: from orsmga008.jf.intel.com ([10.7.209.65]) by orsmga103.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 01 Feb 2023 13:45:46 -0800 X-IronPort-AV: E=McAfee;i="6500,9779,10608"; a="695527301" X-IronPort-AV: E=Sophos;i="5.97,265,1669104000"; d="scan'208";a="695527301" Received: from turnipsi.fi.intel.com (HELO kekkonen.fi.intel.com) ([10.237.72.44]) by orsmga008-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 01 Feb 2023 13:45:45 -0800 Received: from svinhufvud.ger.corp.intel.com (localhost [IPv6:::1]) by kekkonen.fi.intel.com (Postfix) with ESMTP id 5964E120339; Wed, 1 Feb 2023 23:45:42 +0200 (EET) From: Sakari Ailus To: linux-media@vger.kernel.org Cc: laurent.pinchart@ideasonboard.com, hverkuil@xs4all.nl Subject: [PATCH 07/26] media: uvcvideo: Refactor teardown of uvc on USB disconnect Date: Wed, 1 Feb 2023 23:45:16 +0200 Message-Id: <20230201214535.347075-8-sakari.ailus@linux.intel.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20230201214535.347075-1-sakari.ailus@linux.intel.com> References: <20230201214535.347075-1-sakari.ailus@linux.intel.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-media@vger.kernel.org From: Daniel Axtens Currently, disconnecting a USB webcam while it is in use prints out a number of warnings, such as: WARNING: CPU: 2 PID: 3118 at /build/linux-ezBi1T/linux-4.8.0/fs/sysfs/group.c:237 sysfs_remove_group+0x8b/0x90 sysfs group ffffffffa7cd0780 not found for kobject 'event13' This has been noticed before. [0] This is because of the order in which things are torn down. If there are no streams active during a USB disconnect: - uvc_disconnect() is invoked via device_del() through the bus notifier mechanism. - this calls uvc_unregister_video(). - uvc_unregister_video() unregisters the video device for each stream, - because there are no streams open, it calls uvc_delete() - uvc_delete() calls uvc_status_cleanup(), which cleans up the status input device. - uvc_delete() calls media_device_unregister(), which cleans up the media device - uvc_delete(), uvc_unregister_video() and uvc_disconnect() all return, and we end up back in device_del(). - device_del() then cleans up the sysfs folder for the camera with dpm_sysfs_remove(). Because uvc_status_cleanup() and media_device_unregister() have already been called, this all works nicely. If, on the other hand, there *are* streams active during a USB disconnect: - uvc_disconnect() is invoked - this calls uvc_unregister_video() - uvc_unregister_video() unregisters the video device for each stream, - uvc_unregister_video() and uvc_disconnect() return, and we end up back in device_del(). - device_del() then cleans up the sysfs folder for the camera with dpm_sysfs_remove(). Because the status input device and the media device are children of the USB device, this also deletes their sysfs folders. - Sometime later, the final stream is closed, invoking uvc_release(). - uvc_release() calls uvc_delete() - uvc_delete() calls uvc_status_cleanup(), which cleans up the status input device. Because the sysfs directory has already been removed, this causes a WARNing. - uvc_delete() calls media_device_unregister(), which cleans up the media device. Because the sysfs directory has already been removed, this causes another WARNing. To fix this, we need to make sure the devices are always unregistered before the end of uvc_disconnect(). To this, move the unregistration into the disconnect path: - split uvc_status_cleanup() into two parts, one on disconnect that unregisters and one on delete that frees. - move v4l2_device_unregister() and media_device_unregister() into the disconnect path. [0]: https://lkml.org/lkml/2016/12/8/657 [Renamed uvc_input_cleanup() to uvc_input_unregister()] Signed-off-by: Daniel Axtens Acked-by: Greg Kroah-Hartman Signed-off-by: Laurent Pinchart Signed-off-by: Mauro Carvalho Chehab [Sakari Ailus: Rebase on patch Revert "[media] media-device: dynamically allocate struct media_devnode"] Signed-off-by: Sakari Ailus --- drivers/media/usb/uvc/uvc_driver.c | 13 +++++++++---- drivers/media/usb/uvc/uvc_status.c | 12 ++++++++---- drivers/media/usb/uvc/uvcvideo.h | 1 + 3 files changed, 18 insertions(+), 8 deletions(-) diff --git a/drivers/media/usb/uvc/uvc_driver.c b/drivers/media/usb/uvc/uvc_driver.c index e13b9e012e05..5beefbb25fcc 100644 --- a/drivers/media/usb/uvc/uvc_driver.c +++ b/drivers/media/usb/uvc/uvc_driver.c @@ -1834,11 +1834,7 @@ static void uvc_delete(struct kref *kref) usb_put_intf(dev->intf); usb_put_dev(dev->udev); - if (dev->vdev.dev) - v4l2_device_unregister(&dev->vdev); #ifdef CONFIG_MEDIA_CONTROLLER - if (media_devnode_is_registered(&dev->mdev.devnode)) - media_device_unregister(&dev->mdev); media_device_cleanup(&dev->mdev); #endif @@ -1895,6 +1891,15 @@ static void uvc_unregister_video(struct uvc_device *dev) uvc_debugfs_cleanup_stream(stream); } + + uvc_status_unregister(dev); + + if (dev->vdev.dev) + v4l2_device_unregister(&dev->vdev); +#ifdef CONFIG_MEDIA_CONTROLLER + if (media_devnode_is_registered(&dev->mdev.devnode)) + media_device_unregister(&dev->mdev); +#endif } int uvc_register_video_device(struct uvc_device *dev, diff --git a/drivers/media/usb/uvc/uvc_status.c b/drivers/media/usb/uvc/uvc_status.c index 015be0886801..a78a88c710e2 100644 --- a/drivers/media/usb/uvc/uvc_status.c +++ b/drivers/media/usb/uvc/uvc_status.c @@ -73,7 +73,7 @@ static int uvc_input_init(struct uvc_device *dev) return ret; } -static void uvc_input_cleanup(struct uvc_device *dev) +static void uvc_input_unregister(struct uvc_device *dev) { if (dev->input) input_unregister_device(dev->input); @@ -90,7 +90,7 @@ static void uvc_input_report_key(struct uvc_device *dev, unsigned int code, #else #define uvc_input_init(dev) -#define uvc_input_cleanup(dev) +#define uvc_input_unregister(dev) #define uvc_input_report_key(dev, code, value) #endif /* CONFIG_USB_VIDEO_CLASS_INPUT_EVDEV */ @@ -290,12 +290,16 @@ int uvc_status_init(struct uvc_device *dev) return 0; } -void uvc_status_cleanup(struct uvc_device *dev) +void uvc_status_unregister(struct uvc_device *dev) { usb_kill_urb(dev->int_urb); + uvc_input_unregister(dev); +} + +void uvc_status_cleanup(struct uvc_device *dev) +{ usb_free_urb(dev->int_urb); kfree(dev->status); - uvc_input_cleanup(dev); } int uvc_status_start(struct uvc_device *dev, gfp_t flags) diff --git a/drivers/media/usb/uvc/uvcvideo.h b/drivers/media/usb/uvc/uvcvideo.h index 80de6f8395c8..9a596c8d894a 100644 --- a/drivers/media/usb/uvc/uvcvideo.h +++ b/drivers/media/usb/uvc/uvcvideo.h @@ -742,6 +742,7 @@ int uvc_register_video_device(struct uvc_device *dev, /* Status */ int uvc_status_init(struct uvc_device *dev); +void uvc_status_unregister(struct uvc_device *dev); void uvc_status_cleanup(struct uvc_device *dev); int uvc_status_start(struct uvc_device *dev, gfp_t flags); void uvc_status_stop(struct uvc_device *dev); From patchwork Wed Feb 1 21:45:17 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sakari Ailus X-Patchwork-Id: 13125022 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 55E97C636D7 for ; Wed, 1 Feb 2023 21:45:59 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231129AbjBAVp5 (ORCPT ); Wed, 1 Feb 2023 16:45:57 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60972 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230372AbjBAVpy (ORCPT ); Wed, 1 Feb 2023 16:45:54 -0500 Received: from mga03.intel.com (mga03.intel.com [134.134.136.65]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 9FC1966EEB for ; Wed, 1 Feb 2023 13:45:50 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1675287951; x=1706823951; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=gRnTqFdSnV/PtsbR89EbHPCsvx9fLVJgjrTdVHvFkGQ=; b=TVcy/+/vWPxLt9MSUNep+Gv3vDFyy7WK+ye60Oozojsap7/mUvoMVRdh kkOhW+h8EHPJby8IzHXzT67qDVB49Ss2cCuJ/u3KBrq/Nb7hy1QOVgSCy aA6rYDepQ/rP+ZwK5A9skYztJFXL8qdlaYOBtqi71ZIILl1q3VBmGzdXv WBhhwxL4QT3MYez962bJ2clA9iMdZ9LR0bPAKjhQk85idDefBMwEcWHU7 JyltbehTj/qphvht5wY/iGXiee65uW8Gm9jpDa0y4Pq6Bl5becPctoZJe NrLMcYSoQZCtU5N7wwuhDKxismwv1/rKlKTnvyGIs+NwlAB5d186gFG3a g==; X-IronPort-AV: E=McAfee;i="6500,9779,10608"; a="330415632" X-IronPort-AV: E=Sophos;i="5.97,265,1669104000"; d="scan'208";a="330415632" Received: from orsmga008.jf.intel.com ([10.7.209.65]) by orsmga103.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 01 Feb 2023 13:45:46 -0800 X-IronPort-AV: E=McAfee;i="6500,9779,10608"; a="695527303" X-IronPort-AV: E=Sophos;i="5.97,265,1669104000"; d="scan'208";a="695527303" Received: from turnipsi.fi.intel.com (HELO kekkonen.fi.intel.com) ([10.237.72.44]) by orsmga008-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 01 Feb 2023 13:45:45 -0800 Received: from svinhufvud.ger.corp.intel.com (localhost [IPv6:::1]) by kekkonen.fi.intel.com (Postfix) with ESMTP id 1C37212276E; Wed, 1 Feb 2023 23:45:43 +0200 (EET) From: Sakari Ailus To: linux-media@vger.kernel.org Cc: laurent.pinchart@ideasonboard.com, hverkuil@xs4all.nl Subject: [PATCH 08/26] media device: Drop nop release callback Date: Wed, 1 Feb 2023 23:45:17 +0200 Message-Id: <20230201214535.347075-9-sakari.ailus@linux.intel.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20230201214535.347075-1-sakari.ailus@linux.intel.com> References: <20230201214535.347075-1-sakari.ailus@linux.intel.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-media@vger.kernel.org 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 --- 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 5c2e65717c19..64cc6921a8bc 100644 --- a/drivers/media/mc/mc-device.c +++ b/drivers/media/mc/mc-device.c @@ -570,11 +570,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; @@ -722,7 +717,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 Wed Feb 1 21:45:18 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sakari Ailus X-Patchwork-Id: 13125021 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id CE46EC05027 for ; Wed, 1 Feb 2023 21:45:59 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230432AbjBAVp7 (ORCPT ); Wed, 1 Feb 2023 16:45:59 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60990 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230456AbjBAVpy (ORCPT ); Wed, 1 Feb 2023 16:45:54 -0500 Received: from mga03.intel.com (mga03.intel.com [134.134.136.65]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id EF9AA68125 for ; Wed, 1 Feb 2023 13:45:51 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1675287951; x=1706823951; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=2xDNdTCMUoHegNKMckUQwqDHHiguGCguDjb+JObCnlM=; b=A6RrZIaG1jWK6VCVTdHYdOweDFZXiUnR3GDjCla/ieW/olGvr58Oexat DVkkSwbRlGXw3ldBk7VSzynjLC8x3/MMnd/h2LJtGsgUpAx+woHqiwY62 D5CvgmdAny8URVeMNUb5rJDScClDfJT13AkACPnd6zi71+JN705Evc2i3 R0KBrQ6392N9+a/5JWwIlTpg8/bcpJ+jnMKlNnGI5c5+W5p4RxuQeq572 +qXJGyaPdw6PeCy5ommMrWtITkCGMdQ6GQ/Y+CePMy4UMQivmgX11/YzL Dtx80uVIkxwZgs4/Gu9Wu7lXfuyQMA0hicq72IeLqki5Qq//H67itefDD w==; X-IronPort-AV: E=McAfee;i="6500,9779,10608"; a="330415636" X-IronPort-AV: E=Sophos;i="5.97,265,1669104000"; d="scan'208";a="330415636" Received: from orsmga008.jf.intel.com ([10.7.209.65]) by orsmga103.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 01 Feb 2023 13:45:47 -0800 X-IronPort-AV: E=McAfee;i="6500,9779,10608"; a="695527305" X-IronPort-AV: E=Sophos;i="5.97,265,1669104000"; d="scan'208";a="695527305" Received: from turnipsi.fi.intel.com (HELO kekkonen.fi.intel.com) ([10.237.72.44]) by orsmga008-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 01 Feb 2023 13:45:46 -0800 Received: from svinhufvud.ger.corp.intel.com (localhost [IPv6:::1]) by kekkonen.fi.intel.com (Postfix) with ESMTP id AA41F122774; Wed, 1 Feb 2023 23:45:43 +0200 (EET) From: Sakari Ailus To: linux-media@vger.kernel.org Cc: laurent.pinchart@ideasonboard.com, hverkuil@xs4all.nl Subject: [PATCH 09/26] media: Do not call cdev_device_del() if cdev_device_add() fails Date: Wed, 1 Feb 2023 23:45:18 +0200 Message-Id: <20230201214535.347075-10-sakari.ailus@linux.intel.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20230201214535.347075-1-sakari.ailus@linux.intel.com> References: <20230201214535.347075-1-sakari.ailus@linux.intel.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-media@vger.kernel.org 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 --- drivers/media/mc/mc-devnode.c | 1 - 1 file changed, 1 deletion(-) diff --git a/drivers/media/mc/mc-devnode.c b/drivers/media/mc/mc-devnode.c index ce93ab9be676..7e22938dfd81 100644 --- a/drivers/media/mc/mc-devnode.c +++ b/drivers/media/mc/mc-devnode.c @@ -254,7 +254,6 @@ 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); From patchwork Wed Feb 1 21:45:19 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sakari Ailus X-Patchwork-Id: 13125023 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 67DACC636D4 for ; Wed, 1 Feb 2023 21:46:00 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230487AbjBAVp7 (ORCPT ); Wed, 1 Feb 2023 16:45:59 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:32796 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230523AbjBAVp4 (ORCPT ); Wed, 1 Feb 2023 16:45:56 -0500 Received: from mga03.intel.com (mga03.intel.com [134.134.136.65]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 3C9DB6812D for ; Wed, 1 Feb 2023 13:45:52 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1675287953; x=1706823953; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=AiymkVisz2X6EMCSt4cggDqcXeMWHyq5B/W47nd2Gfk=; b=gUszZNifB69ueuK/PrL93cLymT/8gryZy+cpKIU4km76mYfBqU1EiBkX zkUIGwdgu9BcuiIDkzEVYto6ysqnWnb3Bx3Cbue2mpPyLn9e93HfY2o02 f4AYSyCQp0SxKwsR/oXQxDU2U0GLv4DmT1iRtgT/S8qSHsjtiXBSfOrUL rMcZCyCuWNNFqGGP2tLEaf6yUIQD7onGpkTrLc+8QYQIGyNG6MAQ1vI1B IcyIs7VVnRlKp35Xtk7E6K4hPOtakfMcwJ0Qiq5eufYBNq1Rz2+4eOzhl 7OMpsxn/bk2zuhsv4QYgpVBtEgGTblX+yR7fy10vEFKjYjfg+W2qO97J9 Q==; X-IronPort-AV: E=McAfee;i="6500,9779,10608"; a="330415638" X-IronPort-AV: E=Sophos;i="5.97,265,1669104000"; d="scan'208";a="330415638" Received: from orsmga008.jf.intel.com ([10.7.209.65]) by orsmga103.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 01 Feb 2023 13:45:48 -0800 X-IronPort-AV: E=McAfee;i="6500,9779,10608"; a="695527306" X-IronPort-AV: E=Sophos;i="5.97,265,1669104000"; d="scan'208";a="695527306" Received: from turnipsi.fi.intel.com (HELO kekkonen.fi.intel.com) ([10.237.72.44]) by orsmga008-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 01 Feb 2023 13:45:47 -0800 Received: from svinhufvud.ger.corp.intel.com (localhost [IPv6:::1]) by kekkonen.fi.intel.com (Postfix) with ESMTP id 48FB212279A; Wed, 1 Feb 2023 23:45:44 +0200 (EET) From: Sakari Ailus To: linux-media@vger.kernel.org Cc: laurent.pinchart@ideasonboard.com, hverkuil@xs4all.nl Subject: [PATCH 10/26] media-device: Delete character device early Date: Wed, 1 Feb 2023 23:45:19 +0200 Message-Id: <20230201214535.347075-11-sakari.ailus@linux.intel.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20230201214535.347075-1-sakari.ailus@linux.intel.com> References: <20230201214535.347075-1-sakari.ailus@linux.intel.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-media@vger.kernel.org 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 --- 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 7e22938dfd81..8bc7450ac144 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); @@ -270,6 +267,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 Wed Feb 1 21:45:20 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sakari Ailus X-Patchwork-Id: 13125024 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id A1B83C05027 for ; Wed, 1 Feb 2023 21:46:03 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230522AbjBAVqC (ORCPT ); Wed, 1 Feb 2023 16:46:02 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:32842 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231178AbjBAVqA (ORCPT ); Wed, 1 Feb 2023 16:46:00 -0500 Received: from mga03.intel.com (mga03.intel.com [134.134.136.65]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 31A93677A3 for ; Wed, 1 Feb 2023 13:45:54 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1675287955; x=1706823955; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=I/aBcjDEp1fqulu9MJq2LAagydGpio2YlPMS/J1I0V8=; b=RyeDlE3KgHJyHf2yO9MR1yQ82a+8IPfzl9obvNCkMSqGgU2YQAHPP/YN vYQN+dDwB7tZdNAfcN/h/jY3PUToFWv81+7KMT4NOTAqWEP/yGRGDqDKD zSZ4uj/XZ4zDT/1Xx8FVg3ZORWQNT/to2W9cEf6S8hBWBKU6Tt5aIO5ai csyRkBV6mdiJe3gZwYgOrXCXci6dfVSrmCrB9fE40AQe6LyaTNAiITzme KK1ZZn8CHuRUx7fviSaIaaJgT0Ly6p9fejhyhSMepnFSMPZLfR6mHsOpu tlvzvOMgHWO7TRnoguHow+bZfcHCAUSm6eJr4SKPz3R6uOpKdSqDTjxpX g==; X-IronPort-AV: E=McAfee;i="6500,9779,10608"; a="330415641" X-IronPort-AV: E=Sophos;i="5.97,265,1669104000"; d="scan'208";a="330415641" Received: from orsmga008.jf.intel.com ([10.7.209.65]) by orsmga103.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 01 Feb 2023 13:45:49 -0800 X-IronPort-AV: E=McAfee;i="6500,9779,10608"; a="695527307" X-IronPort-AV: E=Sophos;i="5.97,265,1669104000"; d="scan'208";a="695527307" Received: from turnipsi.fi.intel.com (HELO kekkonen.fi.intel.com) ([10.237.72.44]) by orsmga008-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 01 Feb 2023 13:45:47 -0800 Received: from svinhufvud.ger.corp.intel.com (localhost [IPv6:::1]) by kekkonen.fi.intel.com (Postfix) with ESMTP id 28DEF1227A0; Wed, 1 Feb 2023 23:45:44 +0200 (EET) From: Sakari Ailus To: linux-media@vger.kernel.org Cc: laurent.pinchart@ideasonboard.com, hverkuil@xs4all.nl Subject: [PATCH 11/26] media: Split initialising and adding media devnode Date: Wed, 1 Feb 2023 23:45:20 +0200 Message-Id: <20230201214535.347075-12-sakari.ailus@linux.intel.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20230201214535.347075-1-sakari.ailus@linux.intel.com> References: <20230201214535.347075-1-sakari.ailus@linux.intel.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-media@vger.kernel.org As 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 distribute the effects of these changes yet. Add media_device_get() and media_device_put() first. Signed-off-by: Sakari Ailus --- drivers/media/mc/mc-device.c | 18 +++++++++++++----- drivers/media/mc/mc-devnode.c | 17 ++++++++++------- include/media/media-devnode.h | 19 ++++++++++++++----- 3 files changed, 37 insertions(+), 17 deletions(-) diff --git a/drivers/media/mc/mc-device.c b/drivers/media/mc/mc-device.c index 64cc6921a8bc..d63807e85f1c 100644 --- a/drivers/media/mc/mc-device.c +++ b/drivers/media/mc/mc-device.c @@ -721,19 +721,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 out_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 out_unregister; dev_dbg(mdev->dev, "Media device registered\n"); return 0; + +out_unregister: + media_devnode_unregister(&mdev->devnode); +out_put: + put_device(&mdev->devnode.dev); + + return ret; } EXPORT_SYMBOL_GPL(__media_device_register); @@ -808,6 +815,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 8bc7450ac144..7b17419050fb 100644 --- a/drivers/media/mc/mc-devnode.c +++ b/drivers/media/mc/mc-devnode.c @@ -204,6 +204,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) { @@ -235,7 +240,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); @@ -267,14 +271,13 @@ 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); } /* * Initialise media for linux */ -static int __init media_devnode_init(void) +static int __init media_devnode_module_init(void) { int ret; @@ -296,14 +299,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 1117d1dfd6bf..6d46c658be21 100644 --- a/include/media/media-devnode.h +++ b/include/media/media-devnode.h @@ -90,6 +90,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 * @@ -100,11 +111,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 Wed Feb 1 21:45:21 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sakari Ailus X-Patchwork-Id: 13125026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 45F76C636D7 for ; Wed, 1 Feb 2023 21:46:05 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231269AbjBAVqE (ORCPT ); Wed, 1 Feb 2023 16:46:04 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:32860 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231245AbjBAVqA (ORCPT ); Wed, 1 Feb 2023 16:46:00 -0500 Received: from mga03.intel.com (mga03.intel.com [134.134.136.65]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1B30768118 for ; Wed, 1 Feb 2023 13:45:55 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1675287956; x=1706823956; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=uJ5f+OiDI6FGaFs7qgS9ti8o3hAoDzq0qKHqtwano2E=; b=MqChoVC3W7HAni1i7/6UoUzsN9Q5/zweAEoIvhbr2QHkfMQaP7IJTiWE VRhzdln3ZaXP7oUWlGCq4JJjhSnlYv6lZRp/bK17C3kpdYyqnpGYO+Mx8 0Tt0d/2VrWmzusISyLTBzKLbb0dF7eOUSwFJZYxhYxdhnevGD141HwA2a 10aZ+F6vB7a59hzQ6LVP9RfKBKzd2NFrXJL0Pm+Bnp8HNwfHePgx0fIxe T4Ac1Jh6Us0l9nZxoPic+/+OcgtDEiS6zTxBSbGhep23JcPxAuzC4jePP +fCk9zK6NTuGWox8bkANQ8rlSDFRRT4lDCUrp5FqmTYrXWZvLFa4trlrz Q==; X-IronPort-AV: E=McAfee;i="6500,9779,10608"; a="330415645" X-IronPort-AV: E=Sophos;i="5.97,265,1669104000"; d="scan'208";a="330415645" Received: from orsmga008.jf.intel.com ([10.7.209.65]) by orsmga103.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 01 Feb 2023 13:45:49 -0800 X-IronPort-AV: E=McAfee;i="6500,9779,10608"; a="695527309" X-IronPort-AV: E=Sophos;i="5.97,265,1669104000"; d="scan'208";a="695527309" Received: from turnipsi.fi.intel.com (HELO kekkonen.fi.intel.com) ([10.237.72.44]) by orsmga008-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 01 Feb 2023 13:45:48 -0800 Received: from svinhufvud.ger.corp.intel.com (localhost [IPv6:::1]) by kekkonen.fi.intel.com (Postfix) with ESMTP id F16AF1227A1; Wed, 1 Feb 2023 23:45:45 +0200 (EET) From: Sakari Ailus To: linux-media@vger.kernel.org Cc: laurent.pinchart@ideasonboard.com, hverkuil@xs4all.nl Subject: [PATCH 12/26] media: Shuffle functions around Date: Wed, 1 Feb 2023 23:45:21 +0200 Message-Id: <20230201214535.347075-13-sakari.ailus@linux.intel.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20230201214535.347075-1-sakari.ailus@linux.intel.com> References: <20230201214535.347075-1-sakari.ailus@linux.intel.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-media@vger.kernel.org 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 --- drivers/media/mc/mc-device.c | 56 ++++++++++++++++++------------------ 1 file changed, 28 insertions(+), 28 deletions(-) diff --git a/drivers/media/mc/mc-device.c b/drivers/media/mc/mc-device.c index d63807e85f1c..c90f79454988 100644 --- a/drivers/media/mc/mc-device.c +++ b/drivers/media/mc/mc-device.c @@ -677,6 +677,34 @@ void media_device_unregister_entity(struct media_entity *entity) } EXPORT_SYMBOL_GPL(media_device_unregister_entity); +int __must_check 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); + return 0; +} +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); @@ -744,34 +772,6 @@ int __must_check __media_device_register(struct media_device *mdev, } EXPORT_SYMBOL_GPL(__media_device_register); -int __must_check 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); - return 0; -} -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 Wed Feb 1 21:45:22 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sakari Ailus X-Patchwork-Id: 13125025 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 7C76EC636D4 for ; Wed, 1 Feb 2023 21:46:04 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229616AbjBAVqD (ORCPT ); Wed, 1 Feb 2023 16:46:03 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:32848 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231185AbjBAVqA (ORCPT ); Wed, 1 Feb 2023 16:46:00 -0500 Received: from mga03.intel.com (mga03.intel.com [134.134.136.65]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1BA1769517 for ; Wed, 1 Feb 2023 13:45:55 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1675287956; x=1706823956; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=VKjoCYwpt5HtdCIBTFH+klUo3ng/sgbc8tHhHqplIrg=; b=OlxV46sAr9u/xy1zNjWTSVxbZOqYkUgFuCR2S6Dw6uAqitOttGm4DOuV d86OskWhT1kSwW3/58A2YgSV2w7R61D5hqw5+PcK+AR2916uvugOSOke6 P3bS59YqTq/6KYaFOreLkxIT7ITq3V2ymaC/0LQputg8CHmAH3dO5aZaI Ir+9Fs41SbJO0KrKrOdn6730WHO4KRJR8BN7rGvPqGtLXyIc1opngYdR1 8P8NQv6guz+XcSoC5pFRfifdxY5IZdAxTFa4E83cdBdhnd9+wFnxgC1RD 66SXQewpB2THAQbuFhdvaN3M6A95lL89xH95YvsqAs5rJPQ69YCapEWTh g==; X-IronPort-AV: E=McAfee;i="6500,9779,10608"; a="330415653" X-IronPort-AV: E=Sophos;i="5.97,265,1669104000"; d="scan'208";a="330415653" Received: from orsmga008.jf.intel.com ([10.7.209.65]) by orsmga103.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 01 Feb 2023 13:45:50 -0800 X-IronPort-AV: E=McAfee;i="6500,9779,10608"; a="695527310" X-IronPort-AV: E=Sophos;i="5.97,265,1669104000"; d="scan'208";a="695527310" Received: from turnipsi.fi.intel.com (HELO kekkonen.fi.intel.com) ([10.237.72.44]) by orsmga008-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 01 Feb 2023 13:45:49 -0800 Received: from svinhufvud.ger.corp.intel.com (localhost [IPv6:::1]) by kekkonen.fi.intel.com (Postfix) with ESMTP id 9465C120339; Wed, 1 Feb 2023 23:45:46 +0200 (EET) From: Sakari Ailus To: linux-media@vger.kernel.org Cc: laurent.pinchart@ideasonboard.com, hverkuil@xs4all.nl Subject: [PATCH 13/26] media device: Initialise media devnode in media_device_init() Date: Wed, 1 Feb 2023 23:45:22 +0200 Message-Id: <20230201214535.347075-14-sakari.ailus@linux.intel.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20230201214535.347075-1-sakari.ailus@linux.intel.com> References: <20230201214535.347075-1-sakari.ailus@linux.intel.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-media@vger.kernel.org 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 --- drivers/media/mc/mc-device.c | 21 +++++++-------------- 1 file changed, 7 insertions(+), 14 deletions(-) diff --git a/drivers/media/mc/mc-device.c b/drivers/media/mc/mc-device.c index c90f79454988..f4d880fcd977 100644 --- a/drivers/media/mc/mc-device.c +++ b/drivers/media/mc/mc-device.c @@ -716,8 +716,8 @@ 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), @@ -734,6 +734,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); @@ -749,26 +750,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 out_put; + return ret; ret = device_create_file(&mdev->devnode.dev, &dev_attr_model); - if (ret < 0) - goto out_unregister; + if (ret < 0) { + media_devnode_unregister(&mdev->devnode); + return ret; + } dev_dbg(mdev->dev, "Media device registered\n"); return 0; - -out_unregister: - media_devnode_unregister(&mdev->devnode); -out_put: - put_device(&mdev->devnode.dev); - - return ret; } EXPORT_SYMBOL_GPL(__media_device_register); @@ -815,7 +809,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 Wed Feb 1 21:45:23 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sakari Ailus X-Patchwork-Id: 13125028 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 52462C6379F for ; Wed, 1 Feb 2023 21:46:06 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231302AbjBAVqF (ORCPT ); Wed, 1 Feb 2023 16:46:05 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:32936 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231252AbjBAVqD (ORCPT ); Wed, 1 Feb 2023 16:46:03 -0500 Received: from mga03.intel.com (mga03.intel.com [134.134.136.65]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 3039F66ED2 for ; Wed, 1 Feb 2023 13:45:59 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1675287961; x=1706823961; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=MIr121G0j+ekzmFalhInu7JIL7t66lP7ul6BsVHZVq8=; b=j2DpWnwb9rsKCDDMe0ml5/qng+JBklVYeodBpKbfJI9kTso9ck2anALH aJF8G1Yqc9YT+tqRgIrX/HleoiN39faEh30whmJbCXfqH06NB2oZl1Vo8 J3t0D1lBEQgYAinSxaIx6EVVYj7R1RcULixRpxQ3lKbxqPJNxs5A021MI VLo2fsbc2AZwl+SckK3twHsi6HD/utg/7Z7cvKHn/xgQCFxst+GCU/iD/ +oMfcwhnHwpjMZ7uWZMs+Ezlje9aO4/UFW57u51bZ2mPIHwpNkEg1Sqz+ oUor4fOlwzuhy4F4t5q/x9M+mpmmikiL1xREl+AVhs7fRHutlGnu481+P w==; X-IronPort-AV: E=McAfee;i="6500,9779,10608"; a="330415656" X-IronPort-AV: E=Sophos;i="5.97,265,1669104000"; d="scan'208";a="330415656" Received: from orsmga008.jf.intel.com ([10.7.209.65]) by orsmga103.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 01 Feb 2023 13:45:51 -0800 X-IronPort-AV: E=McAfee;i="6500,9779,10608"; a="695527312" X-IronPort-AV: E=Sophos;i="5.97,265,1669104000"; d="scan'208";a="695527312" Received: from turnipsi.fi.intel.com (HELO kekkonen.fi.intel.com) ([10.237.72.44]) by orsmga008-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 01 Feb 2023 13:45:50 -0800 Received: from svinhufvud.ger.corp.intel.com (localhost [IPv6:::1]) by kekkonen.fi.intel.com (Postfix) with ESMTP id 4431512276E; Wed, 1 Feb 2023 23:45:47 +0200 (EET) From: Sakari Ailus To: linux-media@vger.kernel.org Cc: laurent.pinchart@ideasonboard.com, hverkuil@xs4all.nl Subject: [PATCH 14/26] media device: Refcount the media device Date: Wed, 1 Feb 2023 23:45:23 +0200 Message-Id: <20230201214535.347075-15-sakari.ailus@linux.intel.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20230201214535.347075-1-sakari.ailus@linux.intel.com> References: <20230201214535.347075-1-sakari.ailus@linux.intel.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-media@vger.kernel.org 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 --- drivers/media/mc/mc-device.c | 43 +++++++++++++++++++++++++++++++----- include/media/media-device.h | 28 +++++++++++++++++++++++ 2 files changed, 65 insertions(+), 6 deletions(-) diff --git a/drivers/media/mc/mc-device.c b/drivers/media/mc/mc-device.c index f4d880fcd977..c13cbdfdbaab 100644 --- a/drivers/media/mc/mc-device.c +++ b/drivers/media/mc/mc-device.c @@ -705,6 +705,30 @@ 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); + + __media_device_release(mdev); + + if (mdev->ops && mdev->ops->release) + mdev->ops->release(mdev); + else + dev_warn(mdev->dev, + "calling media_device_release but no release callback set!\n"); +} + void media_device_init(struct media_device *mdev) { INIT_LIST_HEAD(&mdev->entities); @@ -717,6 +741,17 @@ void media_device_init(struct media_device *mdev) mutex_init(&mdev->graph_mutex); ida_init(&mdev->entity_internal_idx); atomic_set(&mdev->request_id, 0); + + /* + * Set the release callback to the media device if we have one + * set. Otherwise, the caller is responsible for calling + * media_device_cleanup() in order to release resources + * related to the media device, i.e. the media device is not + * refcounted. This is deprecated. + */ + if (mdev->ops && mdev->ops->release) + mdev->devnode.release = media_device_release; + media_devnode_init(&mdev->devnode); if (!*mdev->bus_info) @@ -729,12 +764,8 @@ 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); + __media_device_release(mdev); + media_device_put(mdev); } EXPORT_SYMBOL_GPL(media_device_cleanup); diff --git a/include/media/media-device.h b/include/media/media-device.h index a33820075aa4..7e8bca6756ba 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,30 @@ struct usb_device; */ void media_device_init(struct media_device *mdev); +/** + * media_device_get() - Get a reference to a media device + * + * @mdev: media device + */ +#define media_device_get(mdev) \ + do { \ + dev_dbg((mdev)->dev, "%s: get media device %s\n", \ + __func__, (mdev)->bus_info); \ + get_device(&(mdev)->devnode.dev); \ + } while (0) + +/** + * media_device_put() - Put a reference to a media device + * + * @mdev: media device + */ +#define media_device_put(mdev) \ + do { \ + dev_dbg((mdev)->dev, "%s: put media device %s\n", \ + __func__, (mdev)->bus_info); \ + put_device(&(mdev)->devnode.dev); \ + } while (0) + /** * media_device_cleanup() - Cleanups a media device element * @@ -432,6 +458,8 @@ void __media_device_usb_init(struct media_device *mdev, const char *driver_name); #else +#define media_device_get(mdev) do { } while (0) +#define media_device_put(mdev) do { } while (0) static inline int media_device_register(struct media_device *mdev) { return 0; From patchwork Wed Feb 1 21:45:24 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sakari Ailus X-Patchwork-Id: 13125029 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 89A05C05027 for ; Wed, 1 Feb 2023 21:46:07 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231252AbjBAVqG (ORCPT ); Wed, 1 Feb 2023 16:46:06 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:32938 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230246AbjBAVqE (ORCPT ); Wed, 1 Feb 2023 16:46:04 -0500 Received: from mga03.intel.com (mga03.intel.com [134.134.136.65]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 31D3168128 for ; Wed, 1 Feb 2023 13:46:00 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1675287961; x=1706823961; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=ZUurEOy8ElwcaUTXxPIydMqSnjPXWrdqw8HIZK8djbU=; b=e2vizJpuw6lbNgzPyfVYJDQLe8SRAQGpGLXW3maI8+fqng3qwdLRVrFf 2/VmfAFQTZqYVAgS1Ko0Zi3FOATgSc+QLZ8yX5cx0FKJbLz31xWbh+ggF j/QU9GB2garVecNQehFTFU0p5CcZtlUxGf2wjTS2zNoJVAVjb5mSQEUOW j32vftcicipjdEJFfEw+w8Ae0Lv3HsFuGqOTevVPnmK7ZhFBcqkABFfmv y/CHy3Wt9DeCbdjVDLP3o2U7kbu1K0+eyHFeXQZ/FW6aLhqZ9fHAUVHLw C3Q64wY9jtt48JhZYitx4qQGZrwAmvmhS/R7lP/7vlqdu7UEXFGWK0dmC Q==; X-IronPort-AV: E=McAfee;i="6500,9779,10608"; a="330415661" X-IronPort-AV: E=Sophos;i="5.97,265,1669104000"; d="scan'208";a="330415661" Received: from orsmga008.jf.intel.com ([10.7.209.65]) by orsmga103.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 01 Feb 2023 13:45:52 -0800 X-IronPort-AV: E=McAfee;i="6500,9779,10608"; a="695527314" X-IronPort-AV: E=Sophos;i="5.97,265,1669104000"; d="scan'208";a="695527314" Received: from turnipsi.fi.intel.com (HELO kekkonen.fi.intel.com) ([10.237.72.44]) by orsmga008-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 01 Feb 2023 13:45:50 -0800 Received: from svinhufvud.ger.corp.intel.com (localhost [IPv6:::1]) by kekkonen.fi.intel.com (Postfix) with ESMTP id 09979122774; Wed, 1 Feb 2023 23:45:47 +0200 (EET) From: Sakari Ailus To: linux-media@vger.kernel.org Cc: laurent.pinchart@ideasonboard.com, hverkuil@xs4all.nl Subject: [PATCH 15/26] v4l: Acquire a reference to the media device for every video device Date: Wed, 1 Feb 2023 23:45:24 +0200 Message-Id: <20230201214535.347075-16-sakari.ailus@linux.intel.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20230201214535.347075-1-sakari.ailus@linux.intel.com> References: <20230201214535.347075-1-sakari.ailus@linux.intel.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-media@vger.kernel.org The video device depends on the existence of its media device --- if there is one. Acquire a reference to it. Signed-off-by: Sakari Ailus --- drivers/media/v4l2-core/v4l2-dev.c | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/drivers/media/v4l2-core/v4l2-dev.c b/drivers/media/v4l2-core/v4l2-dev.c index 397d553177fa..6386948eefaa 100644 --- a/drivers/media/v4l2-core/v4l2-dev.c +++ b/drivers/media/v4l2-core/v4l2-dev.c @@ -176,6 +176,9 @@ static void v4l2_device_release(struct device *cd) { struct video_device *vdev = to_video_device(cd); struct v4l2_device *v4l2_dev = vdev->v4l2_dev; +#ifdef CONFIG_MEDIA_CONTROLLER + struct media_device *mdev = v4l2_dev->mdev; +#endif mutex_lock(&videodev_lock); if (WARN_ON(video_devices[vdev->minor] != vdev)) { @@ -198,8 +201,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) @@ -225,6 +228,11 @@ static void v4l2_device_release(struct device *cd) /* Decrease v4l2_device refcount */ if (v4l2_dev) v4l2_device_put(v4l2_dev); + +#ifdef CONFIG_MEDIA_CONTROLLER + if (mdev) + media_device_put(mdev); +#endif } static struct class video_class = { @@ -872,6 +880,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 Wed Feb 1 21:45:25 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sakari Ailus X-Patchwork-Id: 13125027 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id AF248C63797 for ; Wed, 1 Feb 2023 21:46:05 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231178AbjBAVqF (ORCPT ); Wed, 1 Feb 2023 16:46:05 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:32920 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230508AbjBAVqC (ORCPT ); Wed, 1 Feb 2023 16:46:02 -0500 Received: from mga03.intel.com (mga03.intel.com [134.134.136.65]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 325BF6812D for ; Wed, 1 Feb 2023 13:46:00 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1675287961; x=1706823961; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=weabiT4yTA0ziBp/dh6uchP6cSbrI8QEYQw1qYaQKpw=; b=GTUZ60dqZ6gspXSmMFPhFslvxR4Ey+BHIZ3hhRtnAYZg6wlOW0t/0qrg i6K7clREW6uogb71rab2YvWfcN00p/oNhkEVE90DYf0TvYC/khZqTp4au V7fSmyghrFF49z8Vi/WMATsURH6w4C5bDa2ch+n9gM0l7xGvPmLTMAB3t yBbWeI8DARsuTHvMdWgBBOgQoyKLeGWITriTMNb+03qttviGvg11LHMCY +q++GHnoYjfwlDITV415TUbGDewsg19F4KpdNRB34hVGC7G6AfSqjqf4L blb3m5oyUvComosNGuFa+LDFv7dSyWxW6E9ZCA2LzGIOCcWCglaFpAbsI A==; X-IronPort-AV: E=McAfee;i="6500,9779,10608"; a="330415664" X-IronPort-AV: E=Sophos;i="5.97,265,1669104000"; d="scan'208";a="330415664" Received: from orsmga008.jf.intel.com ([10.7.209.65]) by orsmga103.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 01 Feb 2023 13:45:52 -0800 X-IronPort-AV: E=McAfee;i="6500,9779,10608"; a="695527316" X-IronPort-AV: E=Sophos;i="5.97,265,1669104000"; d="scan'208";a="695527316" Received: from turnipsi.fi.intel.com (HELO kekkonen.fi.intel.com) ([10.237.72.44]) by orsmga008-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 01 Feb 2023 13:45:51 -0800 Received: from svinhufvud.ger.corp.intel.com (localhost [IPv6:::1]) by kekkonen.fi.intel.com (Postfix) with ESMTP id BAA4912279A; Wed, 1 Feb 2023 23:45:48 +0200 (EET) From: Sakari Ailus To: linux-media@vger.kernel.org Cc: laurent.pinchart@ideasonboard.com, hverkuil@xs4all.nl Subject: [PATCH 16/26] media-device: Postpone graph object removal until free Date: Wed, 1 Feb 2023 23:45:25 +0200 Message-Id: <20230201214535.347075-17-sakari.ailus@linux.intel.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20230201214535.347075-1-sakari.ailus@linux.intel.com> References: <20230201214535.347075-1-sakari.ailus@linux.intel.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-media@vger.kernel.org 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 c13cbdfdbaab..00c44752b4a1 100644 --- a/drivers/media/mc/mc-device.c +++ b/drivers/media/mc/mc-device.c @@ -707,8 +707,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); @@ -799,42 +824,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 Wed Feb 1 21:45:26 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sakari Ailus X-Patchwork-Id: 13125030 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 16FDDC64EC3 for ; Wed, 1 Feb 2023 21:46:08 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231364AbjBAVqH (ORCPT ); Wed, 1 Feb 2023 16:46:07 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:32938 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230508AbjBAVqF (ORCPT ); Wed, 1 Feb 2023 16:46:05 -0500 Received: from mga03.intel.com (mga03.intel.com [134.134.136.65]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id CD44168AC5 for ; Wed, 1 Feb 2023 13:46:03 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1675287963; x=1706823963; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=0vjvXljbF9nkb2tybijGMYSFUDEMczyzv12TKKFVfdY=; b=GVxMb9EWPBQyasUhpbgbAes5cKR5DKVLY1Y3WNezbV/P36y6ONp4f7z0 +WOaxa4yZJUynGISyCzpvye7SwGM04rZ6RpblKIuajaaPlHTnC4jfSoth JgtrKu1PAKEcCCf7iRnSU2XhsJopIQDirc0HpFcYAQ3Qfs1HhQRoBfCOI JR0MXDbNw5x2CbMjaA8ab9gXyj0CEsnHT97EOrEgJbzHrNm7fazctJWrN ddnt1Tthud4MC2ODiZwS4O0XoWyZY8pHeDWFGm3PUXiFdOIBgA7SB4ZGL wpUEnZLTmoSP2KEEJLpJsDzO78G7zDXQ5EdKTuToAMtlFo65VVtxWJBGI w==; X-IronPort-AV: E=McAfee;i="6500,9779,10608"; a="330415668" X-IronPort-AV: E=Sophos;i="5.97,265,1669104000"; d="scan'208";a="330415668" Received: from orsmga008.jf.intel.com ([10.7.209.65]) by orsmga103.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 01 Feb 2023 13:45:53 -0800 X-IronPort-AV: E=McAfee;i="6500,9779,10608"; a="695527317" X-IronPort-AV: E=Sophos;i="5.97,265,1669104000"; d="scan'208";a="695527317" Received: from turnipsi.fi.intel.com (HELO kekkonen.fi.intel.com) ([10.237.72.44]) by orsmga008-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 01 Feb 2023 13:45:52 -0800 Received: from svinhufvud.ger.corp.intel.com (localhost [IPv6:::1]) by kekkonen.fi.intel.com (Postfix) with ESMTP id 68F401227A0; Wed, 1 Feb 2023 23:45:49 +0200 (EET) From: Sakari Ailus To: linux-media@vger.kernel.org Cc: laurent.pinchart@ideasonboard.com, hverkuil@xs4all.nl Subject: [PATCH 17/26] omap3isp: Release the isp device struct by media device callback Date: Wed, 1 Feb 2023 23:45:26 +0200 Message-Id: <20230201214535.347075-18-sakari.ailus@linux.intel.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20230201214535.347075-1-sakari.ailus@linux.intel.com> References: <20230201214535.347075-1-sakari.ailus@linux.intel.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-media@vger.kernel.org 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 --- drivers/media/platform/ti/omap3isp/isp.c | 25 ++++++++++++++++++------ 1 file changed, 19 insertions(+), 6 deletions(-) diff --git a/drivers/media/platform/ti/omap3isp/isp.c b/drivers/media/platform/ti/omap3isp/isp.c index e7327e38482d..9665f1eb345e 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_notifier_cleanup(&isp->notifier); + + kfree(isp); +} + static int isp_attach_iommu(struct isp_device *isp) { #ifdef CONFIG_ARM_DMA_USE_IOMMU @@ -2003,17 +2018,15 @@ static int isp_remove(struct platform_device *pdev) v4l2_async_nf_unregister(&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); - v4l2_async_nf_cleanup(&isp->notifier); - - kfree(isp); + /* May release isp immediately */ + media_device_put(&isp->media_dev); return 0; } From patchwork Wed Feb 1 21:45:27 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sakari Ailus X-Patchwork-Id: 13125032 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id A8ADEC636D7 for ; Wed, 1 Feb 2023 21:46:09 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231237AbjBAVqI (ORCPT ); Wed, 1 Feb 2023 16:46:08 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:32936 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230246AbjBAVqG (ORCPT ); Wed, 1 Feb 2023 16:46:06 -0500 Received: from mga03.intel.com (mga03.intel.com [134.134.136.65]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8E6EA6812D for ; Wed, 1 Feb 2023 13:46:03 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1675287964; x=1706823964; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=iOLvgbUxkhp7ltXZAraG0q1tCmQNTIu8Zaqv8LJXzKw=; b=J80EibEp9XHWYFOvB9J+QLTW+v0UTsIQuAx4a0U123ZQ6Cbt+bOwFshd eKjc4OJmluhCG46UJB2CJuEkJ4SoSAUUedcBELIQFYGg5QshHdTCB5Jq/ 2LCTd3H0Pzx12eQ27lBnzefJgmRF73JOdnRs2+795SaQ+xMnRRM5pM074 I8ukkDzQFNG7l7jaWnMJBnACViKBX4ZtQgJETyFBq8NxRkQiJEcKuaAnZ zBJLSKRRmXb4V2kM6UX//mpbZpNdxBY4s6SgR0iRznrB6AWFLZkcx5Qsg 9dvHN/V14pvPdF3Mfmz4sw9FPdaHh3exxO1SEr+PwVQj5684aUqLoYlyU Q==; X-IronPort-AV: E=McAfee;i="6500,9779,10608"; a="330415671" X-IronPort-AV: E=Sophos;i="5.97,265,1669104000"; d="scan'208";a="330415671" Received: from orsmga008.jf.intel.com ([10.7.209.65]) by orsmga103.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 01 Feb 2023 13:45:54 -0800 X-IronPort-AV: E=McAfee;i="6500,9779,10608"; a="695527320" X-IronPort-AV: E=Sophos;i="5.97,265,1669104000"; d="scan'208";a="695527320" Received: from turnipsi.fi.intel.com (HELO kekkonen.fi.intel.com) ([10.237.72.44]) by orsmga008-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 01 Feb 2023 13:45:52 -0800 Received: from svinhufvud.ger.corp.intel.com (localhost [IPv6:::1]) by kekkonen.fi.intel.com (Postfix) with ESMTP id 0B9781227A1; Wed, 1 Feb 2023 23:45:49 +0200 (EET) From: Sakari Ailus To: linux-media@vger.kernel.org Cc: laurent.pinchart@ideasonboard.com, hverkuil@xs4all.nl Subject: [PATCH 18/26] omap3isp: Don't use devm_request_irq() Date: Wed, 1 Feb 2023 23:45:27 +0200 Message-Id: <20230201214535.347075-19-sakari.ailus@linux.intel.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20230201214535.347075-1-sakari.ailus@linux.intel.com> References: <20230201214535.347075-1-sakari.ailus@linux.intel.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-media@vger.kernel.org Use request_irq() instead of devm_request_irq(), as a handler set using devm_request_irq() may still be called once the driver's remove() callback has been called. Signed-off-by: Sakari Ailus --- drivers/media/platform/ti/omap3isp/isp.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/drivers/media/platform/ti/omap3isp/isp.c b/drivers/media/platform/ti/omap3isp/isp.c index 9665f1eb345e..904a2c2141f9 100644 --- a/drivers/media/platform/ti/omap3isp/isp.c +++ b/drivers/media/platform/ti/omap3isp/isp.c @@ -2024,6 +2024,7 @@ static int isp_remove(struct platform_device *pdev) __omap3isp_get(isp, false); isp_detach_iommu(isp); __omap3isp_put(isp, false); + free_irq(isp->irq_num, isp); /* May release isp immediately */ media_device_put(&isp->media_dev); @@ -2419,8 +2420,7 @@ static int isp_probe(struct platform_device *pdev) } isp->irq_num = ret; - if (devm_request_irq(isp->dev, isp->irq_num, isp_isr, IRQF_SHARED, - "OMAP3 ISP", isp)) { + if (request_irq(isp->irq_num, isp_isr, IRQF_SHARED, "OMAP3 ISP", isp)) { dev_err(isp->dev, "Unable to request IRQ\n"); ret = -EINVAL; goto error_iommu; @@ -2429,7 +2429,7 @@ static int isp_probe(struct platform_device *pdev) /* Entities */ ret = isp_initialize_modules(isp); if (ret < 0) - goto error_iommu; + goto error_irq; ret = isp_register_entities(isp); if (ret < 0) @@ -2454,6 +2454,8 @@ static int isp_probe(struct platform_device *pdev) isp_unregister_entities(isp); error_modules: isp_cleanup_modules(isp); +error_irq: + free_irq(isp->irq_num, isp); error_iommu: isp_detach_iommu(isp); error_isp: From patchwork Wed Feb 1 21:45:28 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sakari Ailus X-Patchwork-Id: 13125031 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id CD255C636D4 for ; Wed, 1 Feb 2023 21:46:08 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231305AbjBAVqI (ORCPT ); Wed, 1 Feb 2023 16:46:08 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:32934 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231224AbjBAVqG (ORCPT ); Wed, 1 Feb 2023 16:46:06 -0500 Received: from mga03.intel.com (mga03.intel.com [134.134.136.65]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8E62568126 for ; Wed, 1 Feb 2023 13:46:03 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1675287964; x=1706823964; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=C7ojklwdsw9pyjTRpLRjwb/ybwVkxHASBuJm5fS+J7Y=; b=iuL6okoDCnhBNNhpqne4n5tQMfYZLSFL8Vxn9JDmsuRyyJh2Mw3ItSl7 VkfWsaqVRBeKS6gKmw6eCkjxZ4XAAdUCaViXMbR3W+kd09PbUsW0iaaIz 5lfCmwuJXlTaM6bkgITmLHwqZ8fMcZQuauPBFc6GbV4LFImSMKO9Nt48D Oi5S8oFeqhkunQmQTTMJs8FU6Q9axe7iA23hJjRDXPWGNDyg9aHazAL+P WwMczKXax1HbDnCZIWbTicK5kbQPb1cbfJ12UuNoUeD4Zgmr6COe2pCjC Wyqhdxp8qhe6uimw9xbhpIokod18PV2jt/Ym7rathpEf9dmloW7RwM81q w==; X-IronPort-AV: E=McAfee;i="6500,9779,10608"; a="330415675" X-IronPort-AV: E=Sophos;i="5.97,265,1669104000"; d="scan'208";a="330415675" Received: from orsmga008.jf.intel.com ([10.7.209.65]) by orsmga103.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 01 Feb 2023 13:45:54 -0800 X-IronPort-AV: E=McAfee;i="6500,9779,10608"; a="695527321" X-IronPort-AV: E=Sophos;i="5.97,265,1669104000"; d="scan'208";a="695527321" Received: from turnipsi.fi.intel.com (HELO kekkonen.fi.intel.com) ([10.237.72.44]) by orsmga008-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 01 Feb 2023 13:45:53 -0800 Received: from svinhufvud.ger.corp.intel.com (localhost [IPv6:::1]) by kekkonen.fi.intel.com (Postfix) with ESMTP id D14A2120339; Wed, 1 Feb 2023 23:45:50 +0200 (EET) From: Sakari Ailus To: linux-media@vger.kernel.org Cc: laurent.pinchart@ideasonboard.com, hverkuil@xs4all.nl Subject: [PATCH 19/26] media: Add nop implementations of media_device_{init,cleanup} Date: Wed, 1 Feb 2023 23:45:28 +0200 Message-Id: <20230201214535.347075-20-sakari.ailus@linux.intel.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20230201214535.347075-1-sakari.ailus@linux.intel.com> References: <20230201214535.347075-1-sakari.ailus@linux.intel.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-media@vger.kernel.org To support compliation with Media controller disabled, drivers were required to conditionally call media_device_init and media_device_cleanup. Add nop implementations of both so drivers don't need to care (or at least care less). Signed-off-by: Sakari Ailus --- include/media/media-device.h | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/include/media/media-device.h b/include/media/media-device.h index 7e8bca6756ba..780440bbb39d 100644 --- a/include/media/media-device.h +++ b/include/media/media-device.h @@ -460,6 +460,11 @@ void __media_device_usb_init(struct media_device *mdev, #else #define media_device_get(mdev) do { } while (0) #define media_device_put(mdev) do { } while (0) + +static inline void media_device_init(struct media_device *mdev) +{ +} + static inline int media_device_register(struct media_device *mdev) { return 0; @@ -467,6 +472,11 @@ static inline int media_device_register(struct media_device *mdev) static inline void media_device_unregister(struct media_device *mdev) { } + +static inline void media_device_cleanup(struct media_device *mdev) +{ +} + static inline int media_device_register_entity(struct media_device *mdev, struct media_entity *entity) { From patchwork Wed Feb 1 21:45:29 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sakari Ailus X-Patchwork-Id: 13125035 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 0BC2AC636D7 for ; Wed, 1 Feb 2023 21:46:12 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231371AbjBAVqL (ORCPT ); Wed, 1 Feb 2023 16:46:11 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33076 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231376AbjBAVqJ (ORCPT ); Wed, 1 Feb 2023 16:46:09 -0500 Received: from mga03.intel.com (mga03.intel.com [134.134.136.65]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 418A36952D for ; Wed, 1 Feb 2023 13:46:05 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1675287966; x=1706823966; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=/gm4sb/4TkW1CYbLuQm1axWyl1O/DR1Djwowv5ZsKq8=; b=YFl55f35tDN2UwARIQFpB1I7jNQVgKe05/hokZ0gzS+gbr8kAb8Zl9x2 58RjqekRnTi5YcINtOf1CtYqcphgJT/Q/XTzvwzLCtX+ryn98QZQY3fxF t+SIIT+eB5Dknukyzcr2CeLGVj/nZipXVYvZ6qusZhjXti2URJ6Wzrff6 Svj4T2nqmFDSRXzPd9vLE/hsrPARhz9xj7aM45/+B9u27h5/WSqJsQ4rs Gph+RSaaOKDizveZPaTulg3qNCXxm5G6jEwmek8X2N0ZtENb/K168dDlp xtYY9o+ncFecmLHBg8M79SNi0GFqOR635tT3pt+pYI0RZi21F0JVEo/Aq Q==; X-IronPort-AV: E=McAfee;i="6500,9779,10608"; a="330415679" X-IronPort-AV: E=Sophos;i="5.97,265,1669104000"; d="scan'208";a="330415679" Received: from orsmga008.jf.intel.com ([10.7.209.65]) by orsmga103.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 01 Feb 2023 13:45:55 -0800 X-IronPort-AV: E=McAfee;i="6500,9779,10608"; a="695527323" X-IronPort-AV: E=Sophos;i="5.97,265,1669104000"; d="scan'208";a="695527323" Received: from turnipsi.fi.intel.com (HELO kekkonen.fi.intel.com) ([10.237.72.44]) by orsmga008-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 01 Feb 2023 13:45:54 -0800 Received: from svinhufvud.ger.corp.intel.com (localhost [IPv6:::1]) by kekkonen.fi.intel.com (Postfix) with ESMTP id 7008212276E; Wed, 1 Feb 2023 23:45:51 +0200 (EET) From: Sakari Ailus To: linux-media@vger.kernel.org Cc: laurent.pinchart@ideasonboard.com, hverkuil@xs4all.nl Subject: [PATCH 20/26] media: ipu3-cio2: Call v4l2_device_unregister() earlier Date: Wed, 1 Feb 2023 23:45:29 +0200 Message-Id: <20230201214535.347075-21-sakari.ailus@linux.intel.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20230201214535.347075-1-sakari.ailus@linux.intel.com> References: <20230201214535.347075-1-sakari.ailus@linux.intel.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-media@vger.kernel.org v4l2_device_unregister() unregisters V4L2 sub-device nodes among other things. Call it before releasing memory and other resources. Signed-off-by: Sakari Ailus --- drivers/media/pci/intel/ipu3/ipu3-cio2-main.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/drivers/media/pci/intel/ipu3/ipu3-cio2-main.c b/drivers/media/pci/intel/ipu3/ipu3-cio2-main.c index 3b76a9d0383a..d1bfcfba112f 100644 --- a/drivers/media/pci/intel/ipu3/ipu3-cio2-main.c +++ b/drivers/media/pci/intel/ipu3/ipu3-cio2-main.c @@ -1836,11 +1836,11 @@ static void cio2_pci_remove(struct pci_dev *pci_dev) struct cio2_device *cio2 = pci_get_drvdata(pci_dev); media_device_unregister(&cio2->media_dev); + v4l2_device_unregister(&cio2->v4l2_dev); v4l2_async_nf_unregister(&cio2->notifier); v4l2_async_nf_cleanup(&cio2->notifier); cio2_queues_exit(cio2); cio2_fbpt_exit_dummy(cio2); - v4l2_device_unregister(&cio2->v4l2_dev); media_device_cleanup(&cio2->media_dev); mutex_destroy(&cio2->lock); From patchwork Wed Feb 1 21:45:30 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sakari Ailus X-Patchwork-Id: 13125034 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 58E47C636D4 for ; Wed, 1 Feb 2023 21:46:11 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230246AbjBAVqK (ORCPT ); Wed, 1 Feb 2023 16:46:10 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33042 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230131AbjBAVqJ (ORCPT ); Wed, 1 Feb 2023 16:46:09 -0500 Received: from mga03.intel.com (mga03.intel.com [134.134.136.65]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id CB7C366FAE for ; Wed, 1 Feb 2023 13:46:06 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1675287966; x=1706823966; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=2tB1U4gSG7i6iUdvuFpxNjdylTQOIc7zBUgl8qwpxoQ=; b=UMKjoEffaxgcBqPWL1JUJHZL2Zmy9MIgR0qGBnAwo4LwkV5Bh/URuwat 8A1GxyII2iDF/dX6upjIFAlo2Wa7PZuW54Rq/3r+INaMOJtJueZtnx2Qp v1UFw7n8S1kJUQTnsNh3AoxpWajQOHEMqPIatKpItukVLh1cwOQ2+6Xap xyCD/rA+901o8LwfuityvCu9ZAcqZEfJPVdjJp+nhFnKmX1Xi6mf4Nv9u UDzleAGYwapMy9lQviEimZkDVbt63H0RZuumUsBJJHOZeyq813Iowzl5n w9FJR+byoGiz6YwmihNXmXOKfooAqVfj43HZixLENcYskrgeaP1Z0aKAZ A==; X-IronPort-AV: E=McAfee;i="6500,9779,10608"; a="330415683" X-IronPort-AV: E=Sophos;i="5.97,265,1669104000"; d="scan'208";a="330415683" Received: from orsmga008.jf.intel.com ([10.7.209.65]) by orsmga103.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 01 Feb 2023 13:45:55 -0800 X-IronPort-AV: E=McAfee;i="6500,9779,10608"; a="695527325" X-IronPort-AV: E=Sophos;i="5.97,265,1669104000"; d="scan'208";a="695527325" Received: from turnipsi.fi.intel.com (HELO kekkonen.fi.intel.com) ([10.237.72.44]) by orsmga008-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 01 Feb 2023 13:45:54 -0800 Received: from svinhufvud.ger.corp.intel.com (localhost [IPv6:::1]) by kekkonen.fi.intel.com (Postfix) with ESMTP id 0D7AA1227F2; Wed, 1 Feb 2023 23:45:51 +0200 (EET) From: Sakari Ailus To: linux-media@vger.kernel.org Cc: laurent.pinchart@ideasonboard.com, hverkuil@xs4all.nl Subject: [PATCH 21/26] media: ipu3-cio2: Don't use devm_request_irq() Date: Wed, 1 Feb 2023 23:45:30 +0200 Message-Id: <20230201214535.347075-22-sakari.ailus@linux.intel.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20230201214535.347075-1-sakari.ailus@linux.intel.com> References: <20230201214535.347075-1-sakari.ailus@linux.intel.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-media@vger.kernel.org Use request_irq() instead of devm_request_irq(), as a handler set using devm_request_irq() may still be called once the driver's remove() callback has been called. Also register the IRQ earlier on. Signed-off-by: Sakari Ailus --- drivers/media/pci/intel/ipu3/ipu3-cio2-main.c | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/drivers/media/pci/intel/ipu3/ipu3-cio2-main.c b/drivers/media/pci/intel/ipu3/ipu3-cio2-main.c index d1bfcfba112f..9fdfb2a794db 100644 --- a/drivers/media/pci/intel/ipu3/ipu3-cio2-main.c +++ b/drivers/media/pci/intel/ipu3/ipu3-cio2-main.c @@ -1773,6 +1773,12 @@ static int cio2_pci_probe(struct pci_dev *pci_dev, if (r) return r; + r = request_irq(pci_dev->irq, cio2_irq, IRQF_SHARED, CIO2_NAME, cio2); + if (r) { + dev_err(dev, "failed to request IRQ (%d)\n", r); + goto fail_mutex_destroy; + } + mutex_init(&cio2->lock); cio2->media_dev.dev = dev; @@ -1783,7 +1789,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_free_irq; cio2->v4l2_dev.mdev = &cio2->media_dev; r = v4l2_device_register(dev, &cio2->v4l2_dev); @@ -1803,13 +1809,6 @@ static int cio2_pci_probe(struct pci_dev *pci_dev, if (r) goto fail_clean_notifier; - r = devm_request_irq(dev, pci_dev->irq, cio2_irq, IRQF_SHARED, - CIO2_NAME, cio2); - if (r) { - dev_err(dev, "failed to request IRQ (%d)\n", r); - goto fail_clean_notifier; - } - pm_runtime_put_noidle(dev); pm_runtime_allow(dev); @@ -1824,6 +1823,8 @@ static int cio2_pci_probe(struct pci_dev *pci_dev, fail_media_device_unregister: media_device_unregister(&cio2->media_dev); media_device_cleanup(&cio2->media_dev); +fail_free_irq: + free_irq(pci_dev->irq, cio2); fail_mutex_destroy: mutex_destroy(&cio2->lock); cio2_fbpt_exit_dummy(cio2); @@ -1837,6 +1838,7 @@ static void cio2_pci_remove(struct pci_dev *pci_dev) media_device_unregister(&cio2->media_dev); v4l2_device_unregister(&cio2->v4l2_dev); + free_irq(pci_dev->irq, cio2); v4l2_async_nf_unregister(&cio2->notifier); v4l2_async_nf_cleanup(&cio2->notifier); cio2_queues_exit(cio2); From patchwork Wed Feb 1 21:45:31 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sakari Ailus X-Patchwork-Id: 13125033 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id D1430C05027 for ; Wed, 1 Feb 2023 21:46:10 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231428AbjBAVqK (ORCPT ); Wed, 1 Feb 2023 16:46:10 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33044 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231371AbjBAVqJ (ORCPT ); Wed, 1 Feb 2023 16:46:09 -0500 Received: from mga03.intel.com (mga03.intel.com [134.134.136.65]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id CBA0A68AC5 for ; Wed, 1 Feb 2023 13:46:06 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1675287966; x=1706823966; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=V1h83OUcJIWIRBLzf2YZvY4c2hnZblcP9eeYXGsmjtk=; b=cczpY/eAbcWIGQV2lqhKSNIOlCKk25XdVx319JufaOQUzTEEhgO4LaMC xdirsWbkQX6vZhpNOxU91mOJFEiYmJzo4j5WIqU354MS6M4K7YQkbPv7l BloJueMGAVG0SDX1knjq5m1h5XVQPSttiJDBSjXvk3ma9t+ryiKUs4Hko oNyFdYndhOsTdHnLAulgY0Iz04mJg5pyS98B4YbAs/iOCFjLaZWERpumU GfgyIkZj+Z9Gvh3hvkr8PHjkySlJUt+/VfWCHfdrvTqtI1JiB/yIN/ISK 3EWqQQBZqfY44VcSlvd3NhcYy1TtiSdCfl+UgDeOkUsa0Enqx9OJ2/SLO w==; X-IronPort-AV: E=McAfee;i="6500,9779,10608"; a="330415685" X-IronPort-AV: E=Sophos;i="5.97,265,1669104000"; d="scan'208";a="330415685" Received: from orsmga008.jf.intel.com ([10.7.209.65]) by orsmga103.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 01 Feb 2023 13:45:56 -0800 X-IronPort-AV: E=McAfee;i="6500,9779,10608"; a="695527327" X-IronPort-AV: E=Sophos;i="5.97,265,1669104000"; d="scan'208";a="695527327" Received: from turnipsi.fi.intel.com (HELO kekkonen.fi.intel.com) ([10.237.72.44]) by orsmga008-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 01 Feb 2023 13:45:55 -0800 Received: from svinhufvud.ger.corp.intel.com (localhost [IPv6:::1]) by kekkonen.fi.intel.com (Postfix) with ESMTP id A4A94122774; Wed, 1 Feb 2023 23:45:52 +0200 (EET) From: Sakari Ailus To: linux-media@vger.kernel.org Cc: laurent.pinchart@ideasonboard.com, hverkuil@xs4all.nl Subject: [PATCH 22/26] media: ipu3-cio2: Release the cio2 device context by media device callback Date: Wed, 1 Feb 2023 23:45:31 +0200 Message-Id: <20230201214535.347075-23-sakari.ailus@linux.intel.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20230201214535.347075-1-sakari.ailus@linux.intel.com> References: <20230201214535.347075-1-sakari.ailus@linux.intel.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-media@vger.kernel.org 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 --- drivers/media/pci/intel/ipu3/ipu3-cio2-main.c | 55 ++++++++++++++----- 1 file changed, 41 insertions(+), 14 deletions(-) diff --git a/drivers/media/pci/intel/ipu3/ipu3-cio2-main.c b/drivers/media/pci/intel/ipu3/ipu3-cio2-main.c index 9fdfb2a794db..78bef16b2152 100644 --- a/drivers/media/pci/intel/ipu3/ipu3-cio2-main.c +++ b/drivers/media/pci/intel/ipu3/ipu3-cio2-main.c @@ -236,9 +236,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 ****************/ @@ -1652,13 +1658,13 @@ 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); + 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) @@ -1704,6 +1710,23 @@ static int cio2_check_fwnode_graph(struct fwnode_handle *fwnode) return cio2_check_fwnode_graph(fwnode->secondary); } +static void cio2_media_release(struct media_device *mdev) +{ + struct cio2_device *cio2 = + container_of(mdev, struct cio2_device, media_dev); + + v4l2_async_nf_cleanup(&cio2->notifier); + cio2_queues_exit(cio2); + cio2_fbpt_exit_dummy(cio2); + mutex_destroy(&cio2->lock); + + kfree(cio2); +} + +static struct media_device_ops cio2_mdev_ops = { + .release = cio2_media_release, +}; + /**************** PCI interface ****************/ static int cio2_pci_probe(struct pci_dev *pci_dev, @@ -1731,7 +1754,7 @@ static int cio2_pci_probe(struct pci_dev *pci_dev, 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; @@ -1782,6 +1805,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; @@ -1816,15 +1840,18 @@ 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_free_irq: free_irq(pci_dev->irq, cio2); + media_device_put(&cio2->media_dev); + return r; + fail_mutex_destroy: mutex_destroy(&cio2->lock); cio2_fbpt_exit_dummy(cio2); @@ -1835,19 +1862,19 @@ static int cio2_pci_probe(struct pci_dev *pci_dev, 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); + for (i = 0; i < CIO2_QUEUES; i++) + vb2_video_unregister_device(&cio2->queue[i].vdev); v4l2_device_unregister(&cio2->v4l2_dev); free_irq(pci_dev->irq, cio2); v4l2_async_nf_unregister(&cio2->notifier); - v4l2_async_nf_cleanup(&cio2->notifier); - cio2_queues_exit(cio2); - cio2_fbpt_exit_dummy(cio2); - 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 Wed Feb 1 21:45:32 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sakari Ailus X-Patchwork-Id: 13125036 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id C410EC636D4 for ; Wed, 1 Feb 2023 21:46:15 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229935AbjBAVqO (ORCPT ); Wed, 1 Feb 2023 16:46:14 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33134 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231376AbjBAVqN (ORCPT ); Wed, 1 Feb 2023 16:46:13 -0500 Received: from mga03.intel.com (mga03.intel.com [134.134.136.65]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 2017A69B29 for ; Wed, 1 Feb 2023 13:46:09 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1675287969; x=1706823969; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=+DLksq+WPU0YWpSHQZj+ffwywQs/DE2vR7tbCri9UXc=; b=mEbfRQI+KwZnNxzR+XR7RxIxJgEcRKgrTgJw+dXo06Jbmcgy94wuF1rV lT9xe1FVRmSjHS3cAfN9yhr5lIGFTqkB1XgqH0Xodbiz62PawE2i74yFt PJuPoJhK2BCUf0hmSiXLrRWPQwkBjZdJHschckJcCgnkJ4GZqO6qXmUb4 y3e4LRQ5+n1/w+WHG7EcdkyV8+mrIb/CrNGp7JQZgfS8lhwihWIKO2+gv 2OpbJBurrTNzvpSuIR5d0ULgRTwMjpDNgxOT998XwcYKhqi48vQ/pCxKv Qw5DnZzn8ObW8GFv06BY1TearOKvLSx1jqmEYJXXPPN4/kJ987yhuh8hk A==; X-IronPort-AV: E=McAfee;i="6500,9779,10608"; a="330415692" X-IronPort-AV: E=Sophos;i="5.97,265,1669104000"; d="scan'208";a="330415692" Received: from orsmga008.jf.intel.com ([10.7.209.65]) by orsmga103.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 01 Feb 2023 13:45:57 -0800 X-IronPort-AV: E=McAfee;i="6500,9779,10608"; a="695527329" X-IronPort-AV: E=Sophos;i="5.97,265,1669104000"; d="scan'208";a="695527329" Received: from turnipsi.fi.intel.com (HELO kekkonen.fi.intel.com) ([10.237.72.44]) by orsmga008-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 01 Feb 2023 13:45:56 -0800 Received: from svinhufvud.ger.corp.intel.com (localhost [IPv6:::1]) by kekkonen.fi.intel.com (Postfix) with ESMTP id 9D3BF12279A; Wed, 1 Feb 2023 23:45:53 +0200 (EET) From: Sakari Ailus To: linux-media@vger.kernel.org Cc: laurent.pinchart@ideasonboard.com, hverkuil@xs4all.nl Subject: [PATCH 23/26] media: Add per-file-handle data support Date: Wed, 1 Feb 2023 23:45:32 +0200 Message-Id: <20230201214535.347075-24-sakari.ailus@linux.intel.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20230201214535.347075-1-sakari.ailus@linux.intel.com> References: <20230201214535.347075-1-sakari.ailus@linux.intel.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-media@vger.kernel.org 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 --- 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 00c44752b4a1..0419a7deccc3 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 7b17419050fb..cb07b3a87bfb 100644 --- a/drivers/media/mc/mc-devnode.c +++ b/drivers/media/mc/mc-devnode.c @@ -140,6 +140,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 @@ -160,17 +161,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; } @@ -179,8 +178,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 780440bbb39d..e363b4f3b01d 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 6d46c658be21..f22b3835702e 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 * @media_dev: pointer to struct &media_device @@ -137,7 +151,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 Wed Feb 1 21:45:33 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sakari Ailus X-Patchwork-Id: 13125037 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id F3CB9C05027 for ; Wed, 1 Feb 2023 21:46:17 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231485AbjBAVqP (ORCPT ); Wed, 1 Feb 2023 16:46:15 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33136 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230361AbjBAVqN (ORCPT ); Wed, 1 Feb 2023 16:46:13 -0500 Received: from mga03.intel.com (mga03.intel.com [134.134.136.65]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 2025369B2A for ; Wed, 1 Feb 2023 13:46:09 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1675287969; x=1706823969; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=CodEgkuIHBS9QKN/y7XdadxqvsBvU6LWKHvmFNJ4lFY=; b=kNP3aOzh0d/bVTTbWs6pU685e++1qTj7e+6dTMZH9GI6Ity/Qz+cpptx hp4uuBNspMkrtWQa0/kRkF5taledARghYKnIglfhZHF1HX0VGQ6DIjZGU yclmzBe9/m7W394YRMuG7g+fhf9EnMH7A7VFnwv7wpjWljgmFYVplzF7t gXTa2FMGFdaCJl6PpXmpG4E1df7e1U/pb6+C3Aq7afzWjz57hdulwnH3O pv8hXTscFgCZoNsU2uhsWs3r7z3tLK9gjshkGYMA40vCMlB+EywOWKkIV Tt+Ms+YQRaoYZicjDRP52SvksqwBs4vsd2h3yUev9FmzyOhk1aTtwDMcO Q==; X-IronPort-AV: E=McAfee;i="6500,9779,10608"; a="330415697" X-IronPort-AV: E=Sophos;i="5.97,265,1669104000"; d="scan'208";a="330415697" Received: from orsmga008.jf.intel.com ([10.7.209.65]) by orsmga103.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 01 Feb 2023 13:45:58 -0800 X-IronPort-AV: E=McAfee;i="6500,9779,10608"; a="695527332" X-IronPort-AV: E=Sophos;i="5.97,265,1669104000"; d="scan'208";a="695527332" Received: from turnipsi.fi.intel.com (HELO kekkonen.fi.intel.com) ([10.237.72.44]) by orsmga008-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 01 Feb 2023 13:45:57 -0800 Received: from svinhufvud.ger.corp.intel.com (localhost [IPv6:::1]) by kekkonen.fi.intel.com (Postfix) with ESMTP id 7C6141227A0; Wed, 1 Feb 2023 23:45:54 +0200 (EET) From: Sakari Ailus To: linux-media@vger.kernel.org Cc: laurent.pinchart@ideasonboard.com, hverkuil@xs4all.nl Subject: [PATCH 24/26] media: Maintain a list of open file handles in a media device Date: Wed, 1 Feb 2023 23:45:33 +0200 Message-Id: <20230201214535.347075-25-sakari.ailus@linux.intel.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20230201214535.347075-1-sakari.ailus@linux.intel.com> References: <20230201214535.347075-1-sakari.ailus@linux.intel.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-media@vger.kernel.org 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 | 23 ++++++++++++++++++++++- drivers/media/mc/mc-devnode.c | 2 +- include/media/media-devnode.h | 4 +++- 3 files changed, 26 insertions(+), 3 deletions(-) diff --git a/drivers/media/mc/mc-device.c b/drivers/media/mc/mc-device.c index 0419a7deccc3..3a1db5fdbba7 100644 --- a/drivers/media/mc/mc-device.c +++ b/drivers/media/mc/mc-device.c @@ -45,9 +45,11 @@ 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; + unsigned long flags; fh = kzalloc(sizeof(*fh), GFP_KERNEL); if (!fh) @@ -55,12 +57,23 @@ static int media_device_open(struct file *filp) filp->private_data = &fh->fh; + spin_lock_irqsave(&mdev->fh_list_lock, flags); + list_add(&fh->mdev_list, &mdev->fh_list); + spin_unlock_irqrestore(&mdev->fh_list_lock, flags); + 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); + unsigned long flags; + + spin_lock_irqsave(&mdev->fh_list_lock, flags); + list_del(&fh->mdev_list); + spin_unlock_irqrestore(&mdev->fh_list_lock, flags); kfree(fh); @@ -773,11 +786,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); /* * Set the release callback to the media device if we have one @@ -836,6 +851,8 @@ EXPORT_SYMBOL_GPL(__media_device_register); void media_device_unregister(struct media_device *mdev) { + unsigned long flags; + if (mdev == NULL) return; @@ -846,6 +863,10 @@ void media_device_unregister(struct media_device *mdev) } mutex_unlock(&mdev->graph_mutex); + spin_lock_irqsave(&mdev->fh_list_lock, flags); + list_del_init(&mdev->fh_list); + spin_unlock_irqrestore(&mdev->fh_list_lock, flags); + 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 cb07b3a87bfb..760314dd22e1 100644 --- a/drivers/media/mc/mc-devnode.c +++ b/drivers/media/mc/mc-devnode.c @@ -161,7 +161,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 f22b3835702e..d21c13829072 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 Wed Feb 1 21:45:34 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sakari Ailus X-Patchwork-Id: 13125038 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 55135C636D7 for ; Wed, 1 Feb 2023 21:46:20 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231509AbjBAVqT (ORCPT ); Wed, 1 Feb 2023 16:46:19 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33186 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231376AbjBAVqP (ORCPT ); Wed, 1 Feb 2023 16:46:15 -0500 Received: from mga03.intel.com (mga03.intel.com [134.134.136.65]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id ACB6D6951B for ; Wed, 1 Feb 2023 13:46:09 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1675287969; x=1706823969; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=0G5EzL3hGZG7GuSZqgsovIfSzRNc32QP7+mtegUJga4=; b=Ie7iFFx006PkgKfqurdon1f2dJqb9EC1dkUTWe4o3Pau+kGx82l7GNBu D+TvSXq56NwYEKPKnSd2qZXKz16B0BZ/sxh5rpzpDBp6tphBZDe+jK8Os 5UN2AWi91OriNBANzNkWgvEeRohVmYiSDFPg8tu5XeLWSUDSWYGvpP2mb 0Rs+5aY5jQR39uGPwidL9BrBo3Wwv8nqZSt6rHWCAv9H7jUYWLBgL81be Kcqj34sjzBqWnN6csDlZ2DBShpxlHcFch4MiMS6u1TmOMCSFPtu2rAB45 6rU8XAUoLjwKLDIdggjjfUItE1Wf5rPYK54RlyYRkgfA/pJpb7g2s5RGK w==; X-IronPort-AV: E=McAfee;i="6500,9779,10608"; a="330415702" X-IronPort-AV: E=Sophos;i="5.97,265,1669104000"; d="scan'208";a="330415702" Received: from orsmga008.jf.intel.com ([10.7.209.65]) by orsmga103.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 01 Feb 2023 13:45:59 -0800 X-IronPort-AV: E=McAfee;i="6500,9779,10608"; a="695527335" X-IronPort-AV: E=Sophos;i="5.97,265,1669104000"; d="scan'208";a="695527335" Received: from turnipsi.fi.intel.com (HELO kekkonen.fi.intel.com) ([10.237.72.44]) by orsmga008-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 01 Feb 2023 13:45:58 -0800 Received: from svinhufvud.ger.corp.intel.com (localhost [IPv6:::1]) by kekkonen.fi.intel.com (Postfix) with ESMTP id 3FC58120339; Wed, 1 Feb 2023 23:45:55 +0200 (EET) From: Sakari Ailus To: linux-media@vger.kernel.org Cc: laurent.pinchart@ideasonboard.com, hverkuil@xs4all.nl Subject: [PATCH 25/26] media: Implement best effort media device removal safety sans refcounting Date: Wed, 1 Feb 2023 23:45:34 +0200 Message-Id: <20230201214535.347075-26-sakari.ailus@linux.intel.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20230201214535.347075-1-sakari.ailus@linux.intel.com> References: <20230201214535.347075-1-sakari.ailus@linux.intel.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-media@vger.kernel.org Add a new helper data structure media_devnode_compat_ref, which is used to prevent user space from calling IOCTLs or other system calls to the media device that has been already unregistered. 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 | 60 ++++++++++++++++++++++++++++++----- drivers/media/mc/mc-devnode.c | 21 ++++++++---- include/media/media-devnode.h | 29 +++++++++++++++++ 3 files changed, 96 insertions(+), 14 deletions(-) diff --git a/drivers/media/mc/mc-device.c b/drivers/media/mc/mc-device.c index 3a1db5fdbba7..22fdaa6370ea 100644 --- a/drivers/media/mc/mc-device.c +++ b/drivers/media/mc/mc-device.c @@ -45,18 +45,34 @@ static inline void __user *media_get_uptr(__u64 arg) return (void __user *)(uintptr_t)arg; } +static void compat_ref_release(struct kref *kref) +{ + struct media_devnode_compat_ref *ref = + container_of_const(kref, struct media_devnode_compat_ref, kref); + + kfree(ref); +} + 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; unsigned long flags; + if (devnode->ref && (!atomic_read(&devnode->ref->registered) || + !kref_get_unless_zero(&devnode->ref->kref))) + return -ENXIO; + fh = kzalloc(sizeof(*fh), GFP_KERNEL); - if (!fh) + if (!fh) { + if (devnode->ref) + kref_put(&devnode->ref->kref, compat_ref_release); return -ENOMEM; + } - filp->private_data = &fh->fh; + fh->fh.ref = devnode->ref; + filp->private_data = &fh->fh; spin_lock_irqsave(&mdev->fh_list_lock, flags); list_add(&fh->mdev_list, &mdev->fh_list); spin_unlock_irqrestore(&mdev->fh_list_lock, flags); @@ -71,9 +87,14 @@ static int media_device_close(struct file *filp) struct media_device_fh *fh = media_device_fh(filp); unsigned long flags; - spin_lock_irqsave(&mdev->fh_list_lock, flags); - list_del(&fh->mdev_list); - spin_unlock_irqrestore(&mdev->fh_list_lock, flags); + if (!fh->fh.ref || atomic_read(&fh->fh.ref->registered)) { + spin_lock_irqsave(&mdev->fh_list_lock, flags); + list_del(&fh->mdev_list); + spin_unlock_irqrestore(&mdev->fh_list_lock, flags); + } + + if (fh->fh.ref) + kref_put(&fh->fh.ref->kref, compat_ref_release); kfree(fh); @@ -816,6 +837,8 @@ EXPORT_SYMBOL_GPL(media_device_init); void media_device_cleanup(struct media_device *mdev) { + if (mdev->devnode.ref) + kref_put(&mdev->devnode.ref->kref, compat_ref_release); __media_device_release(mdev); media_device_put(mdev); } @@ -824,6 +847,7 @@ 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; /* Register the device node. */ @@ -833,19 +857,39 @@ 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; + if (!mdev->ops || !mdev->ops->release) { + ref = kzalloc(sizeof(*mdev->devnode.ref), GFP_KERNEL); + if (!ref) + return -ENOMEM; + + kref_init(&ref->kref); + atomic_set(&ref->registered, 1); + mdev->devnode.ref = ref; + } + ret = media_devnode_register(&mdev->devnode, owner); if (ret < 0) - return ret; + goto err_release_ref; ret = device_create_file(&mdev->devnode.dev, &dev_attr_model); if (ret < 0) { - media_devnode_unregister(&mdev->devnode); - return ret; + goto err_devnode_unregister; } dev_dbg(mdev->dev, "Media device registered\n"); return 0; + +err_devnode_unregister: + media_devnode_unregister(&mdev->devnode); + +err_release_ref: + if (ref) { + kref_put(&ref->kref, compat_ref_release); + mdev->devnode.ref = NULL; + } + + return ret; } EXPORT_SYMBOL_GPL(__media_device_register); diff --git a/drivers/media/mc/mc-devnode.c b/drivers/media/mc/mc-devnode.c index 760314dd22e1..f2cb3617df02 100644 --- a/drivers/media/mc/mc-devnode.c +++ b/drivers/media/mc/mc-devnode.c @@ -65,6 +65,14 @@ static 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) { @@ -72,7 +80,7 @@ static ssize_t media_read(struct file *filp, char __user *buf, if (!devnode->fops->read) return -EINVAL; - if (!media_devnode_is_registered(devnode)) + if (!media_devnode_is_registered_compat(filp->private_data)) return -EIO; return devnode->fops->read(filp, buf, sz, off); } @@ -84,7 +92,7 @@ static ssize_t media_write(struct file *filp, const char __user *buf, if (!devnode->fops->write) return -EINVAL; - if (!media_devnode_is_registered(devnode)) + if (!media_devnode_is_registered_compat(filp->private_data)) return -EIO; return devnode->fops->write(filp, buf, sz, off); } @@ -94,7 +102,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; @@ -106,12 +114,10 @@ __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)) + if (!media_devnode_is_registered_compat(filp->private_data)) return -EIO; return ioctl_func(filp, cmd, arg); @@ -265,6 +271,9 @@ 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); diff --git a/include/media/media-devnode.h b/include/media/media-devnode.h index d21c13829072..9ea55c53e5cb 100644 --- a/include/media/media-devnode.h +++ b/include/media/media-devnode.h @@ -20,6 +20,7 @@ #include #include #include +#include struct media_devnode; @@ -55,9 +56,31 @@ struct media_file_operations { int (*release) (struct file *); }; +/** + * struct media_devnode_compat_ref - Workaround for drivers not managing media + * device lifetime + * + * 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. + * + * @kref: kref for the memory of this struct + * @registered: is this device registered? + */ +struct media_devnode_compat_ref { + struct kref kref; + 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,6 +90,7 @@ struct media_file_operations { */ struct media_devnode_fh { struct media_devnode *devnode; + struct media_devnode_compat_ref *ref; }; /** @@ -80,6 +104,8 @@ struct media_devnode_fh { * @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 system call safety in device + * removal * * This structure represents a media-related device node. * @@ -101,6 +127,9 @@ struct media_devnode { /* callbacks */ void (*release)(struct media_devnode *devnode); + + /* compat reference */ + struct media_devnode_compat_ref *ref; }; /* dev to media_devnode */ From patchwork Wed Feb 1 21:45:35 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sakari Ailus X-Patchwork-Id: 13125039 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 93CDFC05027 for ; Wed, 1 Feb 2023 21:46:21 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229816AbjBAVqU (ORCPT ); Wed, 1 Feb 2023 16:46:20 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33188 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231479AbjBAVqR (ORCPT ); Wed, 1 Feb 2023 16:46:17 -0500 Received: from mga03.intel.com (mga03.intel.com [134.134.136.65]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 2D46E68AC5 for ; Wed, 1 Feb 2023 13:46:13 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1675287974; x=1706823974; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=Lqsi/rnDTFrzfU792UxAlQWXSue9htTmfWVjdK5fprM=; b=UOAvqopeRDq/HxwyfNcaoCMdev04SP3ISSq+b17Ggr8+ydQDILU01cql ypjRy6vt2D2EKxr6ZOM+hPoLUgibNRQods5HOPdkohCifqRuloi6mHsuz 2ouU0WiV5BMh7nuFQJ7Az3XozPv3CJPWns3JNcV+ka101vV8HoG/DqFqX SVjDBudXgSZ/EorSCOqntMSIupLU829ATah3geqLzhcMyDNUjcyXwSt4n TMkikOY3b09qC5dw2RPrYkHm0aNYImhAtwODOsXXGHYog+ucdXav8HTNm AxWc/lqVOWBRs5eeCiFHW6RDpauxpu8dSAY2NaXuk9GVa+wxsSOLGtGwn g==; X-IronPort-AV: E=McAfee;i="6500,9779,10608"; a="330415709" X-IronPort-AV: E=Sophos;i="5.97,265,1669104000"; d="scan'208";a="330415709" Received: from orsmga008.jf.intel.com ([10.7.209.65]) by orsmga103.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 01 Feb 2023 13:45:59 -0800 X-IronPort-AV: E=McAfee;i="6500,9779,10608"; a="695527337" X-IronPort-AV: E=Sophos;i="5.97,265,1669104000"; d="scan'208";a="695527337" Received: from turnipsi.fi.intel.com (HELO kekkonen.fi.intel.com) ([10.237.72.44]) by orsmga008-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 01 Feb 2023 13:45:58 -0800 Received: from svinhufvud.ger.corp.intel.com (localhost [IPv6:::1]) by kekkonen.fi.intel.com (Postfix) with ESMTP id 07B7A12276E; Wed, 1 Feb 2023 23:45:55 +0200 (EET) From: Sakari Ailus To: linux-media@vger.kernel.org Cc: laurent.pinchart@ideasonboard.com, hverkuil@xs4all.nl Subject: [PATCH 26/26] media: Document how Media device resources are released Date: Wed, 1 Feb 2023 23:45:35 +0200 Message-Id: <20230201214535.347075-27-sakari.ailus@linux.intel.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20230201214535.347075-1-sakari.ailus@linux.intel.com> References: <20230201214535.347075-1-sakari.ailus@linux.intel.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-media@vger.kernel.org Document that after unregistering, Media device memory resources are released by the release() callback rather than by calling media_device_cleanup(). Signed-off-by: Sakari Ailus --- Documentation/driver-api/media/mc-core.rst | 12 ++++++++++-- include/media/media-device.h | 6 ++++-- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/Documentation/driver-api/media/mc-core.rst b/Documentation/driver-api/media/mc-core.rst index 400b8ca29367..b5bebef40e56 100644 --- a/Documentation/driver-api/media/mc-core.rst +++ b/Documentation/driver-api/media/mc-core.rst @@ -46,8 +46,16 @@ 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 an 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 diff --git a/include/media/media-device.h b/include/media/media-device.h index e363b4f3b01d..ca9a4d7272c0 100644 --- a/include/media/media-device.h +++ b/include/media/media-device.h @@ -257,8 +257,10 @@ void media_device_init(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);