Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 32f2f538 authored by Ricardo Ribalda's avatar Ricardo Ribalda Committed by Greg Kroah-Hartman
Browse files

media: s2255: Use refcount_t instead of atomic_t for num_channels

[ Upstream commit 6cff72f6bcee89228a662435b7c47e21a391c8d0 ]

Use an API that resembles more the actual use of num_channels.

Found by cocci:
drivers/media/usb/s2255/s2255drv.c:2362:5-24: WARNING: atomic_dec_and_test variation before object free at line 2363.
drivers/media/usb/s2255/s2255drv.c:1557:5-24: WARNING: atomic_dec_and_test variation before object free at line 1558.

Link: https://lore.kernel.org/linux-media/20240429-fix-cocci-v3-11-3c4865f5a4b0@chromium.org


Signed-off-by: default avatarRicardo Ribalda <ribalda@chromium.org>
Signed-off-by: default avatarMauro Carvalho Chehab <mchehab@kernel.org>
Signed-off-by: default avatarSasha Levin <sashal@kernel.org>
parent 2401cbc0
Loading
Loading
Loading
Loading
+10 −10
Original line number Original line Diff line number Diff line
@@ -256,7 +256,7 @@ struct s2255_vc {
struct s2255_dev {
struct s2255_dev {
	struct s2255_vc         vc[MAX_CHANNELS];
	struct s2255_vc         vc[MAX_CHANNELS];
	struct v4l2_device      v4l2_dev;
	struct v4l2_device      v4l2_dev;
	atomic_t                num_channels;
	refcount_t		num_channels;
	int			frames;
	int			frames;
	struct mutex		lock;	/* channels[].vdev.lock */
	struct mutex		lock;	/* channels[].vdev.lock */
	struct mutex		cmdlock; /* protects cmdbuf */
	struct mutex		cmdlock; /* protects cmdbuf */
@@ -1574,11 +1574,11 @@ static void s2255_video_device_release(struct video_device *vdev)
		container_of(vdev, struct s2255_vc, vdev);
		container_of(vdev, struct s2255_vc, vdev);


	dprintk(dev, 4, "%s, chnls: %d\n", __func__,
	dprintk(dev, 4, "%s, chnls: %d\n", __func__,
		atomic_read(&dev->num_channels));
		refcount_read(&dev->num_channels));


	v4l2_ctrl_handler_free(&vc->hdl);
	v4l2_ctrl_handler_free(&vc->hdl);


	if (atomic_dec_and_test(&dev->num_channels))
	if (refcount_dec_and_test(&dev->num_channels))
		s2255_destroy(dev);
		s2255_destroy(dev);
	return;
	return;
}
}
@@ -1681,7 +1681,7 @@ static int s2255_probe_v4l(struct s2255_dev *dev)
				"failed to register video device!\n");
				"failed to register video device!\n");
			break;
			break;
		}
		}
		atomic_inc(&dev->num_channels);
		refcount_inc(&dev->num_channels);
		v4l2_info(&dev->v4l2_dev, "V4L2 device registered as %s\n",
		v4l2_info(&dev->v4l2_dev, "V4L2 device registered as %s\n",
			  video_device_node_name(&vc->vdev));
			  video_device_node_name(&vc->vdev));


@@ -1689,11 +1689,11 @@ static int s2255_probe_v4l(struct s2255_dev *dev)
	pr_info("Sensoray 2255 V4L driver Revision: %s\n",
	pr_info("Sensoray 2255 V4L driver Revision: %s\n",
		S2255_VERSION);
		S2255_VERSION);
	/* if no channels registered, return error and probe will fail*/
	/* if no channels registered, return error and probe will fail*/
	if (atomic_read(&dev->num_channels) == 0) {
	if (refcount_read(&dev->num_channels) == 0) {
		v4l2_device_unregister(&dev->v4l2_dev);
		v4l2_device_unregister(&dev->v4l2_dev);
		return ret;
		return ret;
	}
	}
	if (atomic_read(&dev->num_channels) != MAX_CHANNELS)
	if (refcount_read(&dev->num_channels) != MAX_CHANNELS)
		pr_warn("s2255: Not all channels available.\n");
		pr_warn("s2255: Not all channels available.\n");
	return 0;
	return 0;
}
}
@@ -2242,7 +2242,7 @@ static int s2255_probe(struct usb_interface *interface,
		goto errorFWDATA1;
		goto errorFWDATA1;
	}
	}


	atomic_set(&dev->num_channels, 0);
	refcount_set(&dev->num_channels, 0);
	dev->pid = id->idProduct;
	dev->pid = id->idProduct;
	dev->fw_data = kzalloc(sizeof(struct s2255_fw), GFP_KERNEL);
	dev->fw_data = kzalloc(sizeof(struct s2255_fw), GFP_KERNEL);
	if (!dev->fw_data)
	if (!dev->fw_data)
@@ -2362,12 +2362,12 @@ static void s2255_disconnect(struct usb_interface *interface)
{
{
	struct s2255_dev *dev = to_s2255_dev(usb_get_intfdata(interface));
	struct s2255_dev *dev = to_s2255_dev(usb_get_intfdata(interface));
	int i;
	int i;
	int channels = atomic_read(&dev->num_channels);
	int channels = refcount_read(&dev->num_channels);
	mutex_lock(&dev->lock);
	mutex_lock(&dev->lock);
	v4l2_device_disconnect(&dev->v4l2_dev);
	v4l2_device_disconnect(&dev->v4l2_dev);
	mutex_unlock(&dev->lock);
	mutex_unlock(&dev->lock);
	/*see comments in the uvc_driver.c usb disconnect function */
	/*see comments in the uvc_driver.c usb disconnect function */
	atomic_inc(&dev->num_channels);
	refcount_inc(&dev->num_channels);
	/* unregister each video device. */
	/* unregister each video device. */
	for (i = 0; i < channels; i++)
	for (i = 0; i < channels; i++)
		video_unregister_device(&dev->vc[i].vdev);
		video_unregister_device(&dev->vc[i].vdev);
@@ -2380,7 +2380,7 @@ static void s2255_disconnect(struct usb_interface *interface)
		dev->vc[i].vidstatus_ready = 1;
		dev->vc[i].vidstatus_ready = 1;
		wake_up(&dev->vc[i].wait_vidstatus);
		wake_up(&dev->vc[i].wait_vidstatus);
	}
	}
	if (atomic_dec_and_test(&dev->num_channels))
	if (refcount_dec_and_test(&dev->num_channels))
		s2255_destroy(dev);
		s2255_destroy(dev);
	dev_info(&interface->dev, "%s\n", __func__);
	dev_info(&interface->dev, "%s\n", __func__);
}
}