Skip to content

media: apple: isp: keep the driver state until the last file is closed - #268

Merged
RyanTheTide merged 1 commit into
aurora-wipfrom
j700/isp-v4l2-lifetime
Oct 11, 2026
Merged

RyanTheTide merged 1 commit into
aurora-wipfrom
j700/isp-v4l2-lifetime

Conversation

@RyanTheTide

Copy link
Copy Markdown
Contributor

media: apple: isp: keep the driver state until the last file is closed

struct apple_isp embeds the video device, its vb2 queue and lock, the control handler and the media device. It is allocated with devm_kzalloc() and so freed as soon as the driver is unbound. A process can still have /dev/videoN open at unbind: a camera application, a portal or PipeWire, or one blocked in VIDIOC_DQEVENT. That process keeps a reference to the video device. When it later closes the file, vb2_fop_release() takes the queue lock and v4l2_fh_release() walks the file-handle lists, all in freed memory. A control event subscription reaches the freed control handler too. Depending on what reuses the memory, the close oopses, hangs on the stale mutex, or corrupts memory.

This change follows the V4L2 refcounting convention:

  • The state is allocated with kzalloc_obj() instead of devres.
  • A v4l2_device release callback frees the control handler, cleans up the media device and frees the state.
  • Remove unregisters the device nodes as before and drops the driver's own v4l2_device reference last. The video device holds the other reference while it is registered or open, so the state lives until the last file is closed.
  • Until the video device is registered nothing else can reach the state, so a failed probe still frees it directly. The release callback is set only once registration has succeeded.

Hardware teardown at unbind (firmware stop, IRQ, power domains, DMA mappings, workqueue) is unchanged and still happens in remove. After remove, the file operations only reach the queue lock, the file-handle lists and the controls, which now stay valid.

Tests

  • J700 config build: 0 warnings. W=1 on drivers/media/platform/apple/isp/: no warnings. checkpatch --strict: clean apart from the Assisted-by format note. A KASAN (generic) build of the same tree is provided for the hardware check.

Written for this tree; tested on a MacBook Neo (J700), booted with slab_debug=FZPU:

  • Streaming is unchanged: 150 frames of 1080p NV12 at 30 fps, then 20 opens and closes with no new
    kernel messages.
  • With an idle file held open, unbind returns 0 in 32 ms. The file stays open, and the last close
    returns at once.
  • Unbinding while a stream runs and a control event waiter is subscribed returns in 64 ms. The stream
    ends with an error from VIDIOC_DQBUF, and the event waiter exits.
  • The rebind after each case fails cleanly in stage 1 (-110).
  • No poison, redzone, use-after-free, oops or WARN reports on either boot. After a normal reboot the
    camera streams at 30 fps.
  • MacBook Air (M2) and MacBook Pro (M1 Pro): the camera opens, streams and reopens.

🤖 Generated with Claude Code

The driver state, which embeds the video device, its queue and lock, the
control handler and the media device, is allocated with devm_kzalloc()
and freed as soon as the driver is unbound. A process that still has the
video device open at unbind keeps a reference to the video device, and
when it closes the file, vb2_fop_release() takes the queue lock and
v4l2_fh_release() walks the file handle lists, all in freed memory. A
control event subscription also reaches the freed control handler.

Tie the state to the v4l2_device reference count as V4L2 drivers do:
allocate it with kzalloc(), free it, the control handler and the media
device from the v4l2_device release callback, and drop the driver's own
reference at the end of remove. The video device holds the other one
while it is registered or open, so the state lives until the last file
is closed. Before the video device is registered nothing else can reach
the state, so a failed probe still frees it directly.

Assisted-by: LLM
Signed-off-by: Ryan Murray <[email protected]>
@RyanTheTide
RyanTheTide merged commit 3b2bd35 into aurora-wip Oct 11, 2026
@DjDeveloperr
DjDeveloperr deleted the j700/isp-v4l2-lifetime branch October 12, 2026 19:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant