Repository navigation
media: apple: isp: keep the driver state until the last file is closed - #268
Merged
Merged
Conversation
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]>
4 of 6 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
media: apple: isp: keep the driver state until the last file is closed
struct apple_ispembeds the video device, its vb2 queue and lock, the control handler and the media device. It is allocated withdevm_kzalloc()and so freed as soon as the driver is unbound. A process can still have/dev/videoNopen at unbind: a camera application, a portal or PipeWire, or one blocked inVIDIOC_DQEVENT. That process keeps a reference to the video device. When it later closes the file,vb2_fop_release()takes the queue lock andv4l2_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:
kzalloc_obj()instead of devres.v4l2_devicerelease callback frees the control handler, cleans up the media device and frees the state.v4l2_devicereference last. The video device holds the other reference while it is registered or open, so the state lives until the last file is closed.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
drivers/media/platform/apple/isp/: no warnings. checkpatch--strict: clean apart from theAssisted-byformat 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:kernel messages.
returns at once.
ends with an error from
VIDIOC_DQBUF, and the event waiter exits.camera streams at 30 fps.
🤖 Generated with Claude Code