Skip to contentSkip to conversation

media: staging/intel-ipu3: Finalize subdev initialization to allcoate active state

PATCH In review Maximilian Luz 3 participants 4 messages Sep 7, 2022

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 media: staging/intel-ipu3: Finalize subdev initialization to allcoate active state

Commit f69952a4dc1e ("media: subdev: add active state to struct v4l2_subdev") introduced the active_state member to struct v4l2_subdev. This state needs to be allocated via v4l2_subdev_init_finalize(). The intel-ipu3 driver unfortunately does not do that, due to which, active_state is NULL and we run into an oops (NULL pointer dereference) when that state is accessed.

In particular, this happens subdev in IOCTLs as commit 3cc7a4bbc381 ("media: subdev: pass also the active state to subdevs from ioctls") passes that state on to the subdev IOCTLs. An example scenario where this happens is running libcamera's qcam or cam on a device with IPU3, for example the Microsoft Surface Book 2. In this case, the oops is reproducibly in v4l2_subdev_get_try_crop(), called via imgu_subdev_set_selection().

To fix this, allocate the active_state member via
v4l2_subdev_init_finalize().

Fixes 3cc7a4bbc381 ("media: subdev: pass also the active state to subdevs from ioctls")
---
 drivers/staging/media/ipu3/ipu3-v4l2.c | 12 +++++++++++-
 1 file changed, 11 insertions(+), 1 deletion(-)
drivers/staging/media/ipu3/ipu3-v4l2.c +11 -1
index d1c539cefba8..84ab98ba9a2e 100644
@@ -1093,10 +1093,18 @@ static int imgu_v4l2_subdev_register(struct imgu_device *imgu,
 			"failed to create subdev v4l2 ctrl with err %d", r);
 		goto fail_subdev;
 	}
+
+	r = v4l2_subdev_init_finalize(&imgu_sd->subdev);
+	if (r) {
+		dev_err(&imgu->pci_dev->dev,
+			"failed to initialize subdev (%d)\n", r);
+		goto fail_subdev;
+	}
+
 	r = v4l2_device_register_subdev(&imgu->v4l2_dev, &imgu_sd->subdev);
 	if (r) {
 		dev_err(&imgu->pci_dev->dev,
-			"failed initialize subdev (%d)\n", r);
+			"failed to register subdev (%d)\n", r);
 		goto fail_subdev;
 	}
 
@@ -1104,6 +1112,7 @@ static int imgu_v4l2_subdev_register(struct imgu_device *imgu,
 	return 0;
 
 fail_subdev:
+	v4l2_subdev_cleanup(&imgu_sd->subdev);
 	v4l2_ctrl_handler_free(imgu_sd->subdev.ctrl_handler);
 	media_entity_cleanup(&imgu_sd->subdev.entity);
 
@@ -1275,6 +1284,7 @@ static void imgu_v4l2_subdev_cleanup(struct imgu_device *imgu, unsigned int i)
 	struct imgu_media_pipe *imgu_pipe = &imgu->imgu_pipe[i];
 
 	v4l2_device_unregister_subdev(&imgu_pipe->imgu_sd.subdev);
+	v4l2_subdev_cleanup(&imgu_pipe->imgu_sd.subdev);
 	v4l2_ctrl_handler_free(imgu_pipe->imgu_sd.subdev.ctrl_handler);
 	media_entity_cleanup(&imgu_pipe->imgu_sd.subdev.entity);
 }
Signature · 1 line
2.37.3
#1 lore raw

Hi Maximilian,

Quoted 4 lines · Maximilian Luz

Commit f69952a4dc1e ("media: subdev: add active state to struct v4l2_subdev") introduced the active_state member to struct v4l2_subdev. This state needs to be allocated via v4l2_subdev_init_finalize(). The intel-ipu3 driver unfortunately does not do that, due to which,

That is fine, a driver only needs to allocate the active state if it uses the active state.

Quoted 11 lines

active_state is NULL and we run into an oops (NULL pointer dereference) when that state is accessed.

In particular, this happens subdev in IOCTLs as commit 3cc7a4bbc381 ("media: subdev: pass also the active state to subdevs from ioctls") passes that state on to the subdev IOCTLs. An example scenario where this happens is running libcamera's qcam or cam on a device with IPU3, for example the Microsoft Surface Book 2. In this case, the oops is reproducibly in v4l2_subdev_get_try_crop(), called via imgu_subdev_set_selection().

Quoted 2 lines

To fix this, allocate the active_state member via
v4l2_subdev_init_finalize().

This is not a correct fix. Sakari has sent (and maybe pushed?) this:

https://lore.kernel.org/all/[email protected]/

Tomi

Quoted 47 lines
Fixes 3cc7a4bbc381 ("media: subdev: pass also the active state to subdevs from ioctls")
---
  drivers/staging/media/ipu3/ipu3-v4l2.c | 12 +++++++++++-
  1 file changed, 11 insertions(+), 1 deletion(-)
drivers/staging/media/ipu3/ipu3-v4l2.c +11 -1
index d1c539cefba8..84ab98ba9a2e 100644
@@ -1093,10 +1093,18 @@ static int imgu_v4l2_subdev_register(struct imgu_device *imgu,
  			"failed to create subdev v4l2 ctrl with err %d", r);
  		goto fail_subdev;
  	}
+
+	r = v4l2_subdev_init_finalize(&imgu_sd->subdev);
+	if (r) {
+		dev_err(&imgu->pci_dev->dev,
+			"failed to initialize subdev (%d)\n", r);
+		goto fail_subdev;
+	}
+
  	r = v4l2_device_register_subdev(&imgu->v4l2_dev, &imgu_sd->subdev);
  	if (r) {
  		dev_err(&imgu->pci_dev->dev,
-			"failed initialize subdev (%d)\n", r);
+			"failed to register subdev (%d)\n", r);
  		goto fail_subdev;
  	}
 
@@ -1104,6 +1112,7 @@ static int imgu_v4l2_subdev_register(struct imgu_device *imgu,
  	return 0;
 
  fail_subdev:
+	v4l2_subdev_cleanup(&imgu_sd->subdev);
  	v4l2_ctrl_handler_free(imgu_sd->subdev.ctrl_handler);
  	media_entity_cleanup(&imgu_sd->subdev.entity);
 
@@ -1275,6 +1284,7 @@ static void imgu_v4l2_subdev_cleanup(struct imgu_device *imgu, unsigned int i)
  	struct imgu_media_pipe *imgu_pipe = &imgu->imgu_pipe[i];
 
  	v4l2_device_unregister_subdev(&imgu_pipe->imgu_sd.subdev);
+	v4l2_subdev_cleanup(&imgu_pipe->imgu_sd.subdev);
  	v4l2_ctrl_handler_free(imgu_pipe->imgu_sd.subdev.ctrl_handler);
  	media_entity_cleanup(&imgu_pipe->imgu_sd.subdev.entity);
  }
#2 lore raw
Quoted 22 lines · Tomi Valkeinen

Hi Maximilian,

Quoted 4 lines · Maximilian Luz

Commit f69952a4dc1e ("media: subdev: add active state to struct v4l2_subdev") introduced the active_state member to struct v4l2_subdev. This state needs to be allocated via v4l2_subdev_init_finalize(). The intel-ipu3 driver unfortunately does not do that, due to which,

That is fine, a driver only needs to allocate the active state if it uses the active state.

Quoted 11 lines

active_state is NULL and we run into an oops (NULL pointer dereference) when that state is accessed.

In particular, this happens subdev in IOCTLs as commit 3cc7a4bbc381 ("media: subdev: pass also the active state to subdevs from ioctls") passes that state on to the subdev IOCTLs. An example scenario where this happens is running libcamera's qcam or cam on a device with IPU3, for example the Microsoft Surface Book 2. In this case, the oops is reproducibly in v4l2_subdev_get_try_crop(), called via imgu_subdev_set_selection().

Quoted 6 lines
Quoted 2 lines

To fix this, allocate the active_state member via
v4l2_subdev_init_finalize().

This is not a correct fix. Sakari has sent (and maybe pushed?) this:

https://lore.kernel.org/all/[email protected]/

This being said, it would be nice to convert drivers to use the active state, but that's not related to fixing this issue.

Quoted 48 lines
Quoted 47 lines
Fixes 3cc7a4bbc381 ("media: subdev: pass also the active state to subdevs from ioctls")
---
  drivers/staging/media/ipu3/ipu3-v4l2.c | 12 +++++++++++-
  1 file changed, 11 insertions(+), 1 deletion(-)
drivers/staging/media/ipu3/ipu3-v4l2.c +11 -1
index d1c539cefba8..84ab98ba9a2e 100644
@@ -1093,10 +1093,18 @@ static int imgu_v4l2_subdev_register(struct imgu_device *imgu,
  			"failed to create subdev v4l2 ctrl with err %d", r);
  		goto fail_subdev;
  	}
+
+	r = v4l2_subdev_init_finalize(&imgu_sd->subdev);
+	if (r) {
+		dev_err(&imgu->pci_dev->dev,
+			"failed to initialize subdev (%d)\n", r);
+		goto fail_subdev;
+	}
+
  	r = v4l2_device_register_subdev(&imgu->v4l2_dev, &imgu_sd->subdev);
  	if (r) {
  		dev_err(&imgu->pci_dev->dev,
-			"failed initialize subdev (%d)\n", r);
+			"failed to register subdev (%d)\n", r);
  		goto fail_subdev;
  	}
 
@@ -1104,6 +1112,7 @@ static int imgu_v4l2_subdev_register(struct imgu_device *imgu,
  	return 0;
 
  fail_subdev:
+	v4l2_subdev_cleanup(&imgu_sd->subdev);
  	v4l2_ctrl_handler_free(imgu_sd->subdev.ctrl_handler);
  	media_entity_cleanup(&imgu_sd->subdev.entity);
 
@@ -1275,6 +1284,7 @@ static void imgu_v4l2_subdev_cleanup(struct imgu_device *imgu, unsigned int i)
  	struct imgu_media_pipe *imgu_pipe = &imgu->imgu_pipe[i];
 
  	v4l2_device_unregister_subdev(&imgu_pipe->imgu_sd.subdev);
+	v4l2_subdev_cleanup(&imgu_pipe->imgu_sd.subdev);
  	v4l2_ctrl_handler_free(imgu_pipe->imgu_sd.subdev.ctrl_handler);
  	media_entity_cleanup(&imgu_pipe->imgu_sd.subdev.entity);
  }
Signature · 3 lines
Regards,

Laurent Pinchart
#3 lore raw
In reply to Tomi Valkeinen

Hi,

Quoted 22 lines · Tomi Valkeinen

Hi Maximilian,

Quoted 4 lines · Maximilian Luz

Commit f69952a4dc1e ("media: subdev: add active state to struct v4l2_subdev") introduced the active_state member to struct v4l2_subdev. This state needs to be allocated via v4l2_subdev_init_finalize(). The intel-ipu3 driver unfortunately does not do that, due to which,

That is fine, a driver only needs to allocate the active state if it uses the active state.

Quoted 11 lines

active_state is NULL and we run into an oops (NULL pointer dereference) when that state is accessed.

In particular, this happens subdev in IOCTLs as commit 3cc7a4bbc381 ("media: subdev: pass also the active state to subdevs from ioctls") passes that state on to the subdev IOCTLs. An example scenario where this happens is running libcamera's qcam or cam on a device with IPU3, for example the Microsoft Surface Book 2. In this case, the oops is reproducibly in v4l2_subdev_get_try_crop(), called via imgu_subdev_set_selection().

Quoted 6 lines
Quoted 2 lines

To fix this, allocate the active_state member via
v4l2_subdev_init_finalize().

This is not a correct fix. Sakari has sent (and maybe pushed?) this:

https://lore.kernel.org/all/[email protected]/

Thanks! Unfortunately that doesn't fix the issue completely: That patch addresses imgu_subdev_get_selection() but imgu_subdev_set_selection() still runs into the oops.

I assume a similar fix to the one you linked is needed? I'll give that a try.

Regards,
Max