Skip to content

Commit 218bf10

Browse files
Hans Verkuilmchehab
authored andcommitted
media: v4l2-subdev: handle module refcounting here
The module ownership refcounting was done in media_entity_get/put, but that was very confusing and it did not work either in case an application had a v4l-subdevX device open and the module was unbound. When the v4l-subdevX device was closed the media_entity_put was never called and the module refcount was left one too high, making it impossible to unload it. Since v4l2-subdev.c was the only place where media_entity_get/put was called, just move the functionality to v4l2-subdev.c and drop those confusing entity functions. Store the module in subdev_fh so module_put no longer depends on the media_entity struct. Signed-off-by: Hans Verkuil <hverkuil-cisco@xs4all.nl> Reviewed-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com> Signed-off-by: Mauro Carvalho Chehab <mchehab+samsung@kernel.org>
1 parent 0e43734 commit 218bf10

4 files changed

Lines changed: 11 additions & 65 deletions

File tree

‎drivers/media/media-entity.c‎

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
*/
1818

1919
#include <linux/bitmap.h>
20-
#include <linux/module.h>
2120
#include <linux/property.h>
2221
#include <linux/slab.h>
2322
#include <media/media-entity.h>
@@ -587,33 +586,6 @@ void media_pipeline_stop(struct media_entity *entity)
587586
}
588587
EXPORT_SYMBOL_GPL(media_pipeline_stop);
589588

590-
/* -----------------------------------------------------------------------------
591-
* Module use count
592-
*/
593-
594-
struct media_entity *media_entity_get(struct media_entity *entity)
595-
{
596-
if (entity == NULL)
597-
return NULL;
598-
599-
if (entity->graph_obj.mdev->dev &&
600-
!try_module_get(entity->graph_obj.mdev->dev->driver->owner))
601-
return NULL;
602-
603-
return entity;
604-
}
605-
EXPORT_SYMBOL_GPL(media_entity_get);
606-
607-
void media_entity_put(struct media_entity *entity)
608-
{
609-
if (entity == NULL)
610-
return;
611-
612-
if (entity->graph_obj.mdev->dev)
613-
module_put(entity->graph_obj.mdev->dev->driver->owner);
614-
}
615-
EXPORT_SYMBOL_GPL(media_entity_put);
616-
617589
/* -----------------------------------------------------------------------------
618590
* Links management
619591
*/

‎drivers/media/v4l2-core/v4l2-subdev.c‎

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
#include <linux/ioctl.h>
2020
#include <linux/mm.h>
21+
#include <linux/module.h>
2122
#include <linux/slab.h>
2223
#include <linux/types.h>
2324
#include <linux/videodev2.h>
@@ -54,9 +55,6 @@ static int subdev_open(struct file *file)
5455
struct video_device *vdev = video_devdata(file);
5556
struct v4l2_subdev *sd = vdev_to_v4l2_subdev(vdev);
5657
struct v4l2_subdev_fh *subdev_fh;
57-
#if defined(CONFIG_MEDIA_CONTROLLER)
58-
struct media_entity *entity = NULL;
59-
#endif
6058
int ret;
6159

6260
subdev_fh = kzalloc(sizeof(*subdev_fh), GFP_KERNEL);
@@ -73,12 +71,15 @@ static int subdev_open(struct file *file)
7371
v4l2_fh_add(&subdev_fh->vfh);
7472
file->private_data = &subdev_fh->vfh;
7573
#if defined(CONFIG_MEDIA_CONTROLLER)
76-
if (sd->v4l2_dev->mdev) {
77-
entity = media_entity_get(&sd->entity);
78-
if (!entity) {
74+
if (sd->v4l2_dev->mdev && sd->entity.graph_obj.mdev->dev) {
75+
struct module *owner;
76+
77+
owner = sd->entity.graph_obj.mdev->dev->driver->owner;
78+
if (!try_module_get(owner)) {
7979
ret = -EBUSY;
8080
goto err;
8181
}
82+
subdev_fh->owner = owner;
8283
}
8384
#endif
8485

@@ -91,9 +92,7 @@ static int subdev_open(struct file *file)
9192
return 0;
9293

9394
err:
94-
#if defined(CONFIG_MEDIA_CONTROLLER)
95-
media_entity_put(entity);
96-
#endif
95+
module_put(subdev_fh->owner);
9796
v4l2_fh_del(&subdev_fh->vfh);
9897
v4l2_fh_exit(&subdev_fh->vfh);
9998
subdev_fh_free(subdev_fh);
@@ -111,10 +110,7 @@ static int subdev_close(struct file *file)
111110

112111
if (sd->internal_ops && sd->internal_ops->close)
113112
sd->internal_ops->close(sd, subdev_fh);
114-
#if defined(CONFIG_MEDIA_CONTROLLER)
115-
if (sd->v4l2_dev->mdev)
116-
media_entity_put(&sd->entity);
117-
#endif
113+
module_put(subdev_fh->owner);
118114
v4l2_fh_del(vfh);
119115
v4l2_fh_exit(vfh);
120116
subdev_fh_free(subdev_fh);

‎include/media/media-entity.h‎

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -864,19 +864,6 @@ struct media_link *media_entity_find_link(struct media_pad *source,
864864
*/
865865
struct media_pad *media_entity_remote_pad(const struct media_pad *pad);
866866

867-
/**
868-
* media_entity_get - Get a reference to the parent module
869-
*
870-
* @entity: The entity
871-
*
872-
* Get a reference to the parent media device module.
873-
*
874-
* The function will return immediately if @entity is %NULL.
875-
*
876-
* Return: returns a pointer to the entity on success or %NULL on failure.
877-
*/
878-
struct media_entity *media_entity_get(struct media_entity *entity);
879-
880867
/**
881868
* media_entity_get_fwnode_pad - Get pad number from fwnode
882869
*
@@ -916,17 +903,6 @@ __must_check int media_graph_walk_init(
916903
*/
917904
void media_graph_walk_cleanup(struct media_graph *graph);
918905

919-
/**
920-
* media_entity_put - Release the reference to the parent module
921-
*
922-
* @entity: The entity
923-
*
924-
* Release the reference count acquired by media_entity_get().
925-
*
926-
* The function will return immediately if @entity is %NULL.
927-
*/
928-
void media_entity_put(struct media_entity *entity);
929-
930906
/**
931907
* media_graph_walk_start - Start walking the media graph at a
932908
* given entity

‎include/media/v4l2-subdev.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -910,9 +910,11 @@ struct v4l2_subdev {
910910
*
911911
* @vfh: pointer to &struct v4l2_fh
912912
* @pad: pointer to &struct v4l2_subdev_pad_config
913+
* @owner: module pointer to the owner of this file handle
913914
*/
914915
struct v4l2_subdev_fh {
915916
struct v4l2_fh vfh;
917+
struct module *owner;
916918
#if defined(CONFIG_VIDEO_V4L2_SUBDEV_API)
917919
struct v4l2_subdev_pad_config *pad;
918920
#endif

0 commit comments

Comments
 (0)