Repository navigation
Description
Describe the bug
If you connect the cable while the ustreamer is running, the driver may crash. In rarer cases, with the same reproducing method - NULL pointer dereference. Perhaps the nature of both of these bugs is the same, so this is one issue. The bug is not related to image encoding, just getting a RAW stream is enough.
[ 228.324204] ------------[ cut here ]------------
[ 228.328944] WARNING: CPU: 3 PID: 612 at drivers/media/common/videobuf2/videobuf2-core.c:1945 __vb2_queue_cancel+0x1d0/0x24c [videobuf2_common]
[ 228.341870] Modules linked in: usb_f_mass_storage usb_f_hid spidev btsdio bluetooth ecdh_generic ecc brcmfmac snd_soc_spdif_rx brcmutil snd_soc_simple_card snd_soc_simple_card_utils i2c_mux_pinctrl i2c_mux cfg80211 snd_soc_bcm2835_i2s raspberrypi_hwmon bcm2835_unicam snd_soc_core snd_compress snd_pcm_dmaengine i2c_bcm2835 spi_bcm2835 bcm2835_codec(C) bcm2835_isp(C) bcm2835_v4l2(C) v4l2_mem2mem bcm2835_mmal_vchiq(C) videobuf2_dma_contig videobuf2_vmalloc videobuf2_memops videobuf2_v4l2 iproc_rng200 videobuf2_common rng_core rpivid_mem nvmem_rmem uio_pdrv_genirq uio sch_fq_codel i2c_dev snd_bcm2835(C) snd_pcm snd_timer snd tc358743 v4l2_dv_timings v4l2_fwnode videodev mc cec libcomposite dwc2 udc_core roles drm fuse drm_panel_orientation_quirks ip_tables x_tables ipv6
[ 228.410770] CPU: 3 PID: 612 Comm: stream Tainted: G C 5.10.13-2-ARCH #1
[ 228.418762] Hardware name: BCM2711
[ 228.422221] [<c020f0a0>] (unwind_backtrace) from [<c020ae7c>] (show_stack+0x10/0x14)
[ 228.430044] [<c020ae7c>] (show_stack) from [<c0dd60dc>] (dump_stack+0xa4/0xc4)
[ 228.437342] [<c0dd60dc>] (dump_stack) from [<c022ae0c>] (__warn+0xec/0x148)
[ 228.444372] [<c022ae0c>] (__warn) from [<c0dce37c>] (warn_slowpath_fmt+0x7c/0xac)
[ 228.453323] [<c0dce37c>] (warn_slowpath_fmt) from [<bf278bf0>] (__vb2_queue_cancel+0x1d0/0x24c [videobuf2_common])
[ 228.466613] [<bf278bf0>] (__vb2_queue_cancel [videobuf2_common]) from [<bf279548>] (vb2_core_streamoff+0x18/0xac [videobuf2_common])
[ 228.481555] [<bf279548>] (vb2_core_streamoff [videobuf2_common]) from [<bf1d5474>] (__video_do_ioctl+0x238/0x490 [videodev])
[ 228.495854] [<bf1d5474>] (__video_do_ioctl [videodev]) from [<bf1d6354>] (video_usercopy+0x170/0x5ac [videodev])
[ 228.509073] [<bf1d6354>] (video_usercopy [videodev]) from [<c043ca78>] (sys_ioctl+0x34c/0x904)
[ 228.520652] [<c043ca78>] (sys_ioctl) from [<c0200040>] (ret_fast_syscall+0x0/0x4c)
[ 228.531168] Exception stack(0xc55e5fa8 to 0xc55e5ff0)
[ 228.537691] 5fa0: b68de46c 00000004 00000008 40045613 b68dd968 01147638
[ 228.548766] 5fc0: b68de46c 00000004 00000000 00000036 40045613 0054c35c b68dd968 00000008
[ 228.559827] 5fe0: 0054c11c b68dd944 004f99bc b6c209fc
[ 228.566357] ---[ end trace e70ee86b0b0a292c ]---
[ 228.572403] videobuf2_common: driver bug: stop_streaming operation is leaving buf fed1a223 in active state
To reproduce
At the moment, it can't be reproduced on Raspbian (only on Arch), as it has not yet built the 5.10.13 kernel and cannot be installed with rpi-update. However the approximate method looks like this:
- Install ustreamer
- Load some EDID to the tc358743 device.
- Run
ustreamer --dv-timings --device=/dev/video0 --persistent --format=uyvy --encoder=noop - Plug HDMI
- Stop ustreamer
- Unplug HDMI
- Start ustreamer
- Plug HDMI
- ...
- Error
The error may occur after the second or twentieth iteration, it is non-periodic, but it occurs when the cable is connected. In rarer cases, some kind of NULL pointer dereference may occur, but I have not yet managed to catch it in the log, I only saw it briefly in the UART console three times, after which the Pi rebooted. In even rarer cases, something strange happens like double free or 'munmap_chunk(): invalid pointer'. I suppose it's all one problem.
On older 5.4 kernels, I didn't see this. Interestingly, if you now try to reproduce the problem on Raspbian with the kernel 5.10.11, the first connection of the cable will cause segfault in ustreamer, this was also not observed before.
If you need any more information, I'm ready to help as usual.
System
- Raspberry Pi 4 Model B Rev 1.1
- Arch Linux ARM
- firmware:
version 99d9a48302e4553cff3688692bb7e9ac760a03fa (clean) (release) (start) / Jan 27 2021 22:19:57 Linux pikvm 5.10.13-2-ARCH #1 SMP Fri Feb 5 15:47:26 UTC 2021 armv7l GNU/Linux
Logs
Just dmesg: dmesg1.txt
Another crash (UART log):
screenlog1.txt
Activity
mdevaev commentedon Feb 7, 2021
@6by9 In ancient times, ambassadors with bad news could be killed, but I hope you will spare me in this time :) I brought another nasty bug related to TC358743.
6by9 commentedon Feb 7, 2021
__vb2_queue_cancel is a warning, not a crash. See the comment at https://elixir.bootlin.com/linux/latest/source/drivers/media/common/videobuf2/videobuf2-core.c#L1939 just before it.
I will look at it again as we should return all queued buffers, but it won't crash the system.
mdevaev commentedon Feb 7, 2021
Thanks. In this case, I will try to reproduce the NULL pointer dereference.
mdevaev commentedon Feb 7, 2021
Catched NULL pointer dereference:
... Crash, reboot
And also this, similar as discribed in the first report but with another trace:
I use a slightly different command: here, jpeg encoding is enabled for omx and h264 encoding for mmal using zerocopy from the video device. But it seems to me that this is not related, because I added oops and stacktrace as above (not warnings) and without these options.
ustreamer --dv-timings --device /dev/video0 --persistent --format=uyvy --encoder=omx --h264-sink testmdevaev commentedon Feb 8, 2021
@6by9 I continued testing attempts and realized that NULL dereference (described in the previous comment) happens stably for about 10-20 attempts to connect the cable. Next, the kernel runs kdbg, but I can't do anything there since the kernel is rebooted after that (probably Arch settings). The command causing the crash is also in the previous comment.
mdevaev commentedon Feb 13, 2021
Another catch on zero. Crash and warning:
mdevaev commentedon Feb 15, 2021
@6by9 Is there anything I can do to help solve this bug? I get pecked at the head because of the NULL dereference.
mdevaev commentedon Mar 2, 2021
Sup?
6by9 commentedon Mar 2, 2021
Sorry, still a way down the priority list.
@naushir It looks like there is a path where the unicam driver can return a buffer that isn't queued to it. That's what the videobuf2-core.c:997 warning is about.
TC358743 has no metadata pad, so it should only be the image pad active. It's as if there is a race between unicam_isr and unicam_stop_streaming.
Is it just that we've forgotten to clear UNICAM_ICTL, so the interrupts are still active?
mdevaev commentedon Mar 2, 2021
@6by9 In this situation, I'm more concerned about the NULL dereference rather than the warning that occurs. Apparently you're right and it's a race, given the diverse range of errors. Most of all, I would like to get rid of crash. Warning is a much lesser evil.
naushir commentedon Mar 3, 2021
The only way I can see this problem occurring is if
unicam_isrwere to be preempted byunicam_stop_streamingbut can that really happen?? If so, here is a possible (and entirely untested) fix:mdevaev commentedon Mar 3, 2021
@naushir Well, yes, I have reproduced it stably, as you can see :) Can your patch fix NULL dereference, do you think?
naushir commentedon Mar 3, 2021
The patch above should fix a potential NULL deference, but it may or may not be the one that you have seen triggered, it's difficult to tell. Please do give it a try and let me know.
mdevaev commentedon Mar 3, 2021
@naushir A potential fix is much better than no fix :) Thank you! I'll take care of it immediately.
mdevaev commentedon Mar 3, 2021
@naushir Got this:
And NULL dereference after ~30 retries:
33 remaining items
6by9 commentedon Mar 5, 2021
The ISR is taking FAR to long to run
That looks to be 40ms, which explains why FS + LC + FE/PI0 all occur at the same time. FS+FE/PI0 simultaneously is a condition we can't handle well to determine whether the buffer is actually swapped or not.
naushir commentedon Mar 5, 2021
Do you have any experience with this device? It never seems to produce FE interrupts, and often we get only one interrupt per frame with FS + LC + PI0 set together. This is not good as buffer swaps must happen on LC interrupts but only without FE.
naushir commentedon Mar 5, 2021
Good catch, I missed that. This is likely due to my patch that adds a spinlock wait in that bit of code.
I think the problem starts on the first interrupt where we get FS + PI0. Since LC has not had a chance to swap buffers, cur_buf and next_buf are the same values. We could not do the FE handling if we have a FS + FE simultaneously, but I'm not sure that is the right answer.
6by9 commentedon Mar 5, 2021
It is Unicam that sometimes fails to trigger on FE, not the CSI2 source failing to produce it.
You'll find the same thing in
unicam_lisrwith STA & CAM_PI0 -> UNICAM_STAT_FE, and ISTA & CAM_FEI -> UNICAM_STAT_FE.If you want one of these then I have about 6 here. I need to head past the office and can detour slightly to your place.
(They're generally the Chinese boards from Ebay that come up under a search for "TC358743 Pi", but they work the same as the Auvidea ones for video).
naushir commentedon Mar 5, 2021
I think I have a grasp on the buffer warning now. Hopefully the attached patch should get rid of it. However, I am not convinced that this would also get rid of the NULL deference exception.
@mdevaev can you give this a try whenever you are able to please? I've removed the extra spinlocks so that interrupts should also behave better with this patch.
EDIT: Noticed a typo in the last patch, please reapply this updated one.
0001-Unicam-testing.patch1.zip
mdevaev commentedon Mar 5, 2021
Building...
mdevaev commentedon Mar 5, 2021
I had to set debug level to 3 to be able to use ctrl+c.
Also found some interesting thing about i2c (maybe not related):
Warning and null dereference not yet reproduced
JakubVanek commentedon Mar 5, 2021
Neither can I reproduce the warning (neither from videobuf2 nor from list corruption) nor the NULL dereference.
Sample dmesg logs with ustreamer using JPEG and H264 encoding: jpeg.log, h264.log.
I think I saw a similar I2C warning earlier, but I cannot get it to appear now.
mdevaev commentedon Mar 5, 2021
It seems the problem is fixed. At least I didn't manage to reproduce it for an hour and a half. I suggest to clean the patch and apply it. It doesn't seem to get any worse :)
naushir commentedon Mar 5, 2021
Good news! I'll prepare a clean commit for a PR soon.
mdevaev commentedon Mar 5, 2021
@naushir If it's not difficult, plz attach the new patch, I will test the final version.
naushir commentedon Mar 5, 2021
No problem. I will cc you on the PR. Once you confirm it working we will submit it.
However I may not be able to get it ready until later on in the day.
mdevaev commentedon Mar 5, 2021
It's okay, I'll wait. Solving this problem is a top priority for me.
mdevaev commentedon Mar 7, 2021
Given the presence of the fix, I think that this issue can be closed.