Skip to content

TC358743 - NULL pointer dereference on cable plugging #4128

Closed

Description

@mdevaev

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

  1. mdevaev commented on Feb 7, 2021

    @mdevaev
    ContributorAuthor

    @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.

  2. 6by9 commented on Feb 7, 2021

    @6by9
    Contributor

    __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.

  3. mdevaev commented on Feb 7, 2021

    @mdevaev
    ContributorAuthor

    Thanks. In this case, I will try to reproduce the NULL pointer dereference.

  4. mdevaev commented on Feb 7, 2021

    @mdevaev
    ContributorAuthor

    Catched NULL pointer dereference:

    [  113.910609] Unable to handle kernel NULL pointer dereference at virtual address 00000122
    [  113.918751] pgd = c2ff1852
    [  113.921470] [00000122] *pgd=80000000004003, *pmd=00000000
    [  113.926905] Internal error: Oops: a06 [#1] SMP ARM
    
    Entering kdb (current=0xc3fada00, pid 140) on processor 0 Oops: (null)
    due to oops @ 0xbf2e06e0
    CPU: 0 PID: 140 Comm: kworker/0:3 Tainted: G         C        5.10.13-2-ARCH #1
    Hardware name: BCM2711
    Workqueue: events dbs_work_handler
    PC is at unicam_isr+0x2bc/0x360 [bcm2835_unicam]
    LR is at 0x122
    pc : [<bf2e06e0>]    lr : [<00000122>]    psr: 60000193
    sp : c3fa1d10  ip : c25f5e80  fp : eece3c10
    r10: eece3000  r9 : 000004fc  r8 : 00000100
    r7 : 00000608  r6 : 00000000  r5 : eece34fc  r4 : eece3000
    r3 : c25f6280  r2 : 00000122  r1 : 00000000  r0 : c25f6000
    Flags: nZCv  IRQs off  FIQs on  Mode SVC_32  ISA ARM  Segment user
    Control: 30c5383d  Table: 02947280  DAC: fffffffd
    CPU: 0 PID: 140 Comm: kworker/0:3 Tainted: G         C        5.10.13-2-ARCH #1
    Hardware name: BCM2711
    Workqueue: events dbs_work_handler
    [<c020f0a0>] (unwind_backtrace) from [<c020ae7c>] (show_stack+0x10/0x14)
    [<c020ae7c>] (show_stack) from [<c0dd60dc>] (dump_stack+0xa4/0xc4)
    [<c0dd60dc>] (dump_stack) from [<c02ffd50>] (kdb_main_loop+0x4bc/0x9a0)
    [<c02ffd50>] (kdb_main_loop) from [<c0302e04>] (kdb_stub+0x258/0x458)
    [<c0302e04>] (kdb_stub) from [<c02f7f90>] (kgdb_cpu_enter+0x3e0/0x6fc)
    

    ... Crash, reboot

    And also this, similar as discribed in the first report but with another trace:

    [   76.559135] ------------[ cut here ]------------
    [   76.563844] WARNING: CPU: 3 PID: 541 at drivers/media/common/videobuf2/videobuf2-core.c:997 unicam_return_buffers+0x84/0xd0 [bcm2835_unicam]
    [   76.576556] Modules linked in: usb_f_mass_storage usb_f_hid spidev btsdio bluetooth snd_soc_spdif_rx snd_soc_simple_card ecdh_generic snd_soc_simple_card_utils ecc brcmfmac i2c_mux_pinctrl brcmutil i2c_mux raspberrypi_hwmon bcm2835_unicam cfg80211 snd_soc_bcm2835_i2s snd_soc_core bcm2835_v4l2(C) snd_compress i2c_bcm2835 bcm2835_codec(C) snd_pcm_dmaengine spi_bcm2835 bcm2835_isp(C) bcm2835_mmal_vchiq(C) v4l2_mem2mem videobuf2_dma_contig videobuf2_vmalloc videobuf2_memops videobuf2_v4l2 iproc_rng200 videobuf2_common rng_core rpivid_mem uio_pdrv_genirq nvmem_rmem 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
    [   76.645367] CPU: 3 PID: 541 Comm: stream Tainted: G         C        5.10.13-2-ARCH #1
    [   76.653302] Hardware name: BCM2711
    [   76.656722] [<c020f0a0>] (unwind_backtrace) from [<c020ae7c>] (show_stack+0x10/0x14)
    [   76.664490] [<c020ae7c>] (show_stack) from [<c0dd60dc>] (dump_stack+0xa4/0xc4)
    [   76.671734] [<c0dd60dc>] (dump_stack) from [<c022ae0c>] (__warn+0xec/0x148)
    [   76.678716] [<c022ae0c>] (__warn) from [<c0dce37c>] (warn_slowpath_fmt+0x7c/0xac)
    [   76.686848] [<c0dce37c>] (warn_slowpath_fmt) from [<bf30c72c>] (unicam_return_buffers+0x84/0xd0 [bcm2835_unicam])
    [   76.698427] [<bf30c72c>] (unicam_return_buffers [bcm2835_unicam]) from [<bf214a48>] (__vb2_queue_cancel+0x28/0x24c [videobuf2_common])
    [   76.711830] [<bf214a48>] (__vb2_queue_cancel [videobuf2_common]) from [<bf215548>] (vb2_core_streamoff+0x18/0xac [videobuf2_common])
    [   76.725079] [<bf215548>] (vb2_core_streamoff [videobuf2_common]) from [<bf1d5474>] (__video_do_ioctl+0x238/0x490 [videodev])
    [   76.737669] [<bf1d5474>] (__video_do_ioctl [videodev]) from [<bf1d6354>] (video_usercopy+0x170/0x5ac [videodev])
    [   76.749188] [<bf1d6354>] (video_usercopy [videodev]) from [<c043ca78>] (sys_ioctl+0x34c/0x904)
    [   76.759122] [<c043ca78>] (sys_ioctl) from [<c0200040>] (ret_fast_syscall+0x0/0x4c)
    [   76.768026] Exception stack(0xc5599fa8 to 0xc5599ff0)
    [   76.773747] 9fa0:                   b16f9474 00000004 0000000c 40045613 b16f8960 009ad660
    [   76.783251] 9fc0: b16f9474 00000004 00000000 00000036 40045613 0048e06c b16f8960 0000000c
    [   76.792767] 9fe0: 0048de18 b16f893c 00439c08 b6bcd9fc
    [   76.798490] ---[ end trace 599a6883e85eaf43 ]---
    

    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 test

  5. mdevaev commented on Feb 8, 2021

    @mdevaev
    ContributorAuthor

    @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.

  6. mdevaev commented on Feb 13, 2021

    @mdevaev
    ContributorAuthor

    Another catch on zero. Crash and warning:

    Feb 13 08:07:26 pikvm kernel: ------------[ cut here ]------------
    Feb 13 08:07:26 pikvm kernel: WARNING: CPU: 0 PID: 128 at drivers/media/common/videobuf2/videobuf2-core.c:997 unicam_isr+0xdc/0x36c [bcm2835_unicam]
    Feb 13 08:07:26 pikvm kernel: Modules linked in: usb_f_mass_storage usb_f_hid brcmfmac brcmutil cfg80211 raspberrypi_hwmon i2c_mux_pinctrl i2c_mux bcm2835_unicam bcm2835_codec(C) bcm2835_v4l2(C) bcm2835_isp(>
    Feb 13 08:07:26 pikvm kernel: CPU: 0 PID: 128 Comm: kworker/u2:2 Tainted: G         C        5.10.14-2-ARCH #1
    Feb 13 08:07:26 pikvm kernel: Hardware name: BCM2835
    Feb 13 08:07:26 pikvm kernel: Workqueue:  0x0 (brcmf_wq/mmc1:0001:1)
    Feb 13 08:07:26 pikvm kernel: [<8010ef8c>] (unwind_backtrace) from [<8010ae04>] (show_stack+0x10/0x14)
    Feb 13 08:07:26 pikvm kernel: [<8010ae04>] (show_stack) from [<80d03c88>] (dump_stack+0xd0/0xf8)
    Feb 13 08:07:26 pikvm kernel: [<80d03c88>] (dump_stack) from [<8012bd1c>] (__warn+0xd4/0x140)
    Feb 13 08:07:26 pikvm kernel: [<8012bd1c>] (__warn) from [<80cfb694>] (warn_slowpath_fmt+0x74/0xa8)
    Feb 13 08:07:26 pikvm kernel: [<80cfb694>] (warn_slowpath_fmt) from [<7f2bae4c>] (unicam_isr+0xdc/0x36c [bcm2835_unicam])
    Feb 13 08:07:26 pikvm kernel: [<7f2bae4c>] (unicam_isr [bcm2835_unicam]) from [<8018eb18>] (__handle_irq_event_percpu+0x48/0x1ec)
    Feb 13 08:07:26 pikvm kernel: [<8018eb18>] (__handle_irq_event_percpu) from [<8018eda8>] (handle_irq_event+0x64/0xd8)
    Feb 13 08:07:26 pikvm kernel: [<8018eda8>] (handle_irq_event) from [<8019334c>] (handle_level_irq+0xb0/0x1a0)
    Feb 13 08:07:26 pikvm kernel: [<8019334c>] (handle_level_irq) from [<8018e430>] (__handle_domain_irq+0x74/0xd4)
    Feb 13 08:07:26 pikvm kernel: [<8018e430>] (__handle_domain_irq) from [<8089e644>] (bcm2835_handle_irq+0x2c/0x34)
    Feb 13 08:07:26 pikvm kernel: [<8089e644>] (bcm2835_handle_irq) from [<80100af8>] (__irq_svc+0x58/0x74)
    Feb 13 08:07:26 pikvm kernel: Exception stack(0x84367e68 to 0x84367eb0)
    Feb 13 08:07:26 pikvm kernel: 7e60:                   8306cb00 40000700 0000fde3 00000000 81266b80 00000000
    Feb 13 08:07:26 pikvm kernel: 7e80: 8306cb00 9b6d2b80 1a46c000 84220f00 00000000 84367ef4 846e8040 84367eb8
    Feb 13 08:07:26 pikvm kernel: 7ea0: 80d0b0d8 80157940 60000013 ffffffff
    Feb 13 08:07:26 pikvm kernel: [<80100af8>] (__irq_svc) from [<80157940>] (finish_task_switch+0x7c/0x218)
    Feb 13 08:07:26 pikvm kernel: [<80157940>] (finish_task_switch) from [<80d0b0d8>] (__schedule+0x2e4/0xa54)
    Feb 13 08:07:26 pikvm kernel: [<80d0b0d8>] (__schedule) from [<80d0b8a8>] (schedule+0x60/0xdc)
    Feb 13 08:07:26 pikvm kernel: [<80d0b8a8>] (schedule) from [<80147e74>] (worker_thread+0xcc/0x594)
    Feb 13 08:07:26 pikvm kernel: [<80147e74>] (worker_thread) from [<8014dca8>] (kthread+0x130/0x144)
    Feb 13 08:07:26 pikvm kernel: [<8014dca8>] (kthread) from [<80100138>] (ret_from_fork+0x14/0x3c)
    Feb 13 08:07:26 pikvm kernel: Exception stack(0x84367fb0 to 0x84367ff8)
    Feb 13 08:07:26 pikvm kernel: 7fa0:                                     00000000 00000000 00000000 00000000
    Feb 13 08:07:26 pikvm kernel: 7fc0: 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000
    Feb 13 08:07:26 pikvm kernel: 7fe0: 00000000 00000000 00000000 00000000 00000013 00000000
    Feb 13 08:07:26 pikvm kernel: ---[ end trace 1e400d33e83b8123 ]---
    ...
    Feb 13 08:08:06 pikvm kernel: ------------[ cut here ]------------
    Feb 13 08:08:06 pikvm kernel: WARNING: CPU: 0 PID: 459 at drivers/media/common/videobuf2/videobuf2-core.c:1945 __vb2_queue_cancel+0x1cc/0x250 [videobuf2_common]
    Feb 13 08:08:06 pikvm kernel: Modules linked in: usb_f_mass_storage usb_f_hid brcmfmac brcmutil cfg80211 raspberrypi_hwmon i2c_mux_pinctrl i2c_mux bcm2835_unicam bcm2835_codec(C) bcm2835_v4l2(C) bcm2835_isp(>
    Feb 13 08:08:06 pikvm kernel: CPU: 0 PID: 459 Comm: stream Tainted: G        WC        5.10.14-2-ARCH #1
    Feb 13 08:08:06 pikvm kernel: Hardware name: BCM2835
    Feb 13 08:08:06 pikvm kernel: [<8010ef8c>] (unwind_backtrace) from [<8010ae04>] (show_stack+0x10/0x14)
    Feb 13 08:08:06 pikvm kernel: [<8010ae04>] (show_stack) from [<80d03c88>] (dump_stack+0xd0/0xf8)
    Feb 13 08:08:06 pikvm kernel: [<80d03c88>] (dump_stack) from [<8012bd1c>] (__warn+0xd4/0x140)
    Feb 13 08:08:06 pikvm kernel: [<8012bd1c>] (__warn) from [<80cfb694>] (warn_slowpath_fmt+0x74/0xa8)
    Feb 13 08:08:06 pikvm kernel: [<80cfb694>] (warn_slowpath_fmt) from [<7f259a94>] (__vb2_queue_cancel+0x1cc/0x250 [videobuf2_common])
    Feb 13 08:08:06 pikvm kernel: [<7f259a94>] (__vb2_queue_cancel [videobuf2_common]) from [<7f25a3f4>] (vb2_core_streamoff+0x18/0xac [videobuf2_common])
    Feb 13 08:08:06 pikvm kernel: [<7f25a3f4>] (vb2_core_streamoff [videobuf2_common]) from [<7f1a0570>] (__video_do_ioctl+0x228/0x474 [videodev])
    Feb 13 08:08:06 pikvm kernel: [<7f1a0570>] (__video_do_ioctl [videodev]) from [<7f1a14b8>] (video_usercopy+0x148/0x598 [videodev])
    Feb 13 08:08:06 pikvm kernel: [<7f1a14b8>] (video_usercopy [videodev]) from [<8033aa68>] (sys_ioctl+0x32c/0x92c)
    Feb 13 08:08:06 pikvm kernel: [<8033aa68>] (sys_ioctl) from [<80100060>] (ret_fast_syscall+0x0/0x4c)
    Feb 13 08:08:06 pikvm kernel: Exception stack(0x833c7fa8 to 0x833c7ff0)
    Feb 13 08:08:06 pikvm kernel: 7fa0:                   72600b80 00000004 00000009 40045613 737f8960 02087798
    Feb 13 08:08:06 pikvm kernel: 7fc0: 72600b80 00000004 00000000 00000036 40045613 004df06c 737f8960 00000009
    Feb 13 08:08:06 pikvm kernel: 7fe0: 004dee18 737f893c 00489bf8 76c9a92c
    Feb 13 08:08:06 pikvm kernel: ---[ end trace 1e400d33e83b8124 ]---
    Feb 13 08:08:06 pikvm kernel: videobuf2_common: driver bug: stop_streaming operation is leaving buf cbe27ef1 in active state
    
  7. mdevaev commented on Feb 15, 2021

    @mdevaev
    ContributorAuthor

    @6by9 Is there anything I can do to help solve this bug? I get pecked at the head because of the NULL dereference.

  8. mdevaev commented on Mar 2, 2021

    @mdevaev
    ContributorAuthor

    Sup?

  9. 6by9 commented on Mar 2, 2021

    @6by9
    Contributor

    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?

  10. mdevaev commented on Mar 2, 2021

    @mdevaev
    ContributorAuthor

    @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.

  11. naushir commented on Mar 3, 2021

    @naushir
    Contributor

    The only way I can see this problem occurring is if unicam_isr were to be preempted by unicam_stop_streaming but can that really happen?? If so, here is a possible (and entirely untested) fix:

    diff --git a/drivers/media/platform/bcm2835/bcm2835-unicam.c b/drivers/media/platform/bcm2835/bcm2835-unicam.c
    index a8b3f5433f04..5496f93aa43c 100644
    --- a/drivers/media/platform/bcm2835/bcm2835-unicam.c
    +++ b/drivers/media/platform/bcm2835/bcm2835-unicam.c
    @@ -831,10 +831,12 @@ static irqreturn_t unicam_isr(int irq, void *dev)
                            if (!unicam->node[i].streaming)
                                    continue;
     
    +                       spin_lock(&unicam->node[i].dma_queue_lock);
                            if (unicam->node[i].cur_frm)
                                    unicam_process_buffer_complete(&unicam->node[i],
                                                                   sequence);
                            unicam->node[i].cur_frm = unicam->node[i].next_frm;
    +                       spin_unlock(&unicam->node[i].dma_queue_lock);
                    }
                    unicam->sequence++;
            }
    @@ -849,6 +851,7 @@ static irqreturn_t unicam_isr(int irq, void *dev)
                            if (!unicam->node[i].streaming)
                                    continue;
     
    +                       spin_lock(&unicam->node[i].dma_queue_lock);
                            if (unicam->node[i].cur_frm)
                                    unicam->node[i].cur_frm->vb.vb2_buf.timestamp =
                                                                    ts;
    @@ -858,6 +861,7 @@ static irqreturn_t unicam_isr(int irq, void *dev)
                             * from the queue.
                             */
                            unicam_schedule_dummy_buffer(&unicam->node[i]);
    +                       spin_unlock(&unicam->node[i].dma_queue_lock);
                    }
     
                    unicam_queue_event_sof(unicam);
    
  12. mdevaev commented on Mar 3, 2021

    @mdevaev
    ContributorAuthor

    @naushir Well, yes, I have reproduced it stably, as you can see :) Can your patch fix NULL dereference, do you think?

  13. naushir commented on Mar 3, 2021

    @naushir
    Contributor

    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.

  14. mdevaev commented on Mar 3, 2021

    @mdevaev
    ContributorAuthor

    @naushir A potential fix is much better than no fix :) Thank you! I'll take care of it immediately.

  15. mdevaev commented on Mar 3, 2021

    @mdevaev
    ContributorAuthor

    @naushir Got this:

    [  151.372449] ------------[ cut here ]------------
    [  151.377155] WARNING: CPU: 0 PID: 652 at drivers/media/common/videobuf2/videobuf2-core.c:997 unicam_return_buffers+0x58/0xd0 [bcm2835_unicam]
    [  151.389868] Modules linked in: usb_f_mass_storage usb_f_hid spidev btsdio snd_soc_spdif_rx bluetooth ecdh_generic ecc snd_soc_simple_card brcmfmac
    snd_soc_simple_card_utils brcmutil i2c_mux_pinctrl i2c_mux bcm2835_unicam snd_soc_bcm2835_i2s cfg80211 raspberrypi_hwmon snd_soc_core i2c_bcm2835 snd_
    compress spi_bcm2835 snd_pcm_dmaengine bcm2835_codec(C) bcm2835_v4l2(C) bcm2835_isp(C) bcm2835_mmal_vchiq(C) v4l2_mem2mem videobuf2_dma_contig videobu
    f2_vmalloc videobuf2_memops iproc_rng200 videobuf2_v4l2 rng_core videobuf2_common rpivid_mem nvmem_rmem uio_pdrv_genirq uio sch_fq_codel i2c_dev snd_b
    cm2835(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_q
    uirks ip_tables x_tables ipv6
    [  151.458693] CPU: 0 PID: 652 Comm: stream Tainted: G         C        5.10.17-2-ARCH #1
    [  151.466677] Hardware name: BCM2711
    [  151.470132] [<c020f10c>] (unwind_backtrace) from [<c020aed0>] (show_stack+0x10/0x14)
    [  151.477954] [<c020aed0>] (show_stack) from [<c0de7864>] (dump_stack+0xa4/0xc4)
    [  151.485250] [<c0de7864>] (dump_stack) from [<c022ae9c>] (__warn+0xec/0x148)
    [  151.492278] [<c022ae9c>] (__warn) from [<c0ddfb00>] (warn_slowpath_fmt+0x7c/0xac)
    [  151.501226] [<c0ddfb00>] (warn_slowpath_fmt) from [<bf54e700>] (unicam_return_buffers+0x58/0xd0 [bcm2835_unicam])
    [  151.514436] [<bf54e700>] (unicam_return_buffers [bcm2835_unicam]) from [<bf20fa48>] (__vb2_queue_cancel+0x28/0x24c [videobuf2_common])
    [  151.529507] [<bf20fa48>] (__vb2_queue_cancel [videobuf2_common]) from [<bf210548>] (vb2_core_streamoff+0x18/0xac [videobuf2_common])
    [  151.544455] [<bf210548>] (vb2_core_streamoff [videobuf2_common]) from [<bf1d5474>] (__video_do_ioctl+0x238/0x490 [videodev])
    [  151.558797] [<bf1d5474>] (__video_do_ioctl [videodev]) from [<bf1d6354>] (video_usercopy+0x170/0x5ac [videodev])
    [  151.572027] [<bf1d6354>] (video_usercopy [videodev]) from [<c043df50>] (sys_ioctl+0x34c/0x904)
    [  151.583625] [<c043df50>] (sys_ioctl) from [<c0200040>] (ret_fast_syscall+0x0/0x4c)
    [  151.594146] Exception stack(0xc530ffa8 to 0xc530fff0)
    [  151.600672] ffa0:                   b0505000 00000004 0000000c 40045613 b16f8960 01d67648
    [  151.611754] ffc0: b0505000 00000004 00000000 00000036 40045613 0048f06c b16f8960 0000000c
    [  151.622822] ffe0: 0048ee1c b16f893c 00439c08 b6ba59fc
    [  151.629321] ---[ end trace 90a2d269f970e9d2 ]---
    [  151.635469] ------------[ cut here ]------------
    [  151.641586] WARNING: CPU: 0 PID: 652 at drivers/media/common/videobuf2/videobuf2-core.c:1945 __vb2_queue_cancel+0x1d0/0x24c [videobuf2_common]
    [  151.656335] Modules linked in: usb_f_mass_storage usb_f_hid spidev btsdio snd_soc_spdif_rx bluetooth ecdh_generic ecc snd_soc_simple_card brcmfmac snd_soc_simple_card_utils brcmutil i2c_mux_pinctrl i2c_mux bcm2835_unicam snd_soc_bcm2835_i2s cfg80211 raspberrypi_hwmon snd_soc_core i2c_bcm2835 snd_compress spi_bcm2835 snd_pcm_dmaengine bcm2835_codec(C) bcm2835_v4l2(C) bcm2835_isp(C) bcm2835_mmal_vchiq(C) v4l2_mem2mem videobuf2_dma_contig videobuf2_vmalloc videobuf2_memops iproc_rng200 videobuf2_v4l2 rng_core videobuf2_common 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
    [  151.731803] CPU: 0 PID: 652 Comm: stream Tainted: G        WC        5.10.17-2-ARCH #1
    [  151.741047] Hardware name: BCM2711
    [  151.745110] [<c020f10c>] (unwind_backtrace) from [<c020aed0>] (show_stack+0x10/0x14)
    [  151.754156] [<c020aed0>] (show_stack) from [<c0de7864>] (dump_stack+0xa4/0xc4)
    [  151.762679] [<c0de7864>] (dump_stack) from [<c022ae9c>] (__warn+0xec/0x148)
    [  151.770303] [<c022ae9c>] (__warn) from [<c0ddfb00>] (warn_slowpath_fmt+0x7c/0xac)
    [  151.779094] [<c0ddfb00>] (warn_slowpath_fmt) from [<bf20fbf0>] (__vb2_queue_cancel+0x1d0/0x24c [videobuf2_common])
    [  151.790760] [<bf20fbf0>] (__vb2_queue_cancel [videobuf2_common]) from [<bf210548>] (vb2_core_streamoff+0x18/0xac [videobuf2_common])
    [  151.803997] [<bf210548>] (vb2_core_streamoff [videobuf2_common]) from [<bf1d5474>] (__video_do_ioctl+0x238/0x490 [videodev])
    [  151.816558] [<bf1d5474>] (__video_do_ioctl [videodev]) from [<bf1d6354>] (video_usercopy+0x170/0x5ac [videodev])
    [  151.828079] [<bf1d6354>] (video_usercopy [videodev]) from [<c043df50>] (sys_ioctl+0x34c/0x904)
    [  151.838018] [<c043df50>] (sys_ioctl) from [<c0200040>] (ret_fast_syscall+0x0/0x4c)
    [  151.846950] Exception stack(0xc530ffa8 to 0xc530fff0)
    [  151.852662] ffa0:                   b0505000 00000004 0000000c 40045613 b16f8960 01d67648
    [  151.862185] ffc0: b0505000 00000004 00000000 00000036 40045613 0048f06c b16f8960 0000000c
    [  151.871715] ffe0: 0048ee1c b16f893c 00439c08 b6ba59fc
    [  151.877495] ---[ end trace 90a2d269f970e9d3 ]---
    [  151.882820] videobuf2_common: driver bug: stop_streaming operation is leaving buf dca779ef in active state
    

    And NULL dereference after ~30 retries:

    [  372.258026] 8<--- cut here ---
    [  372.261116] Unable to handle kernel NULL pointer dereference at virtual address 00000122
    [  372.269256] pgd = a13d13c3
    [  372.271977] [00000122] *pgd=80000000004003, *pmd=00000000
    [  372.277418] Internal error: Oops: a06 [#1] SMP ARM
    
    Entering kdb (current=0xc14082c0, pid 0) on processor 0 Oops: (null)
    due to oops @ 0xbf2c90c4
    CPU: 0 PID: 0 Comm: swapper/0 Tainted: G         C        5.10.17-2-ARCH #1
    Hardware name: BCM2711
    PC is at unicam_isr+0x30c/0x3b0 [bcm2835_unicam]
    LR is at 0x122
    pc : [<bf2c90c4>]    lr : [<00000122>]    psr: 60080193
    sp : c1401e70  ip : c2964680  fp : bf2cab6c
    r10: c43be000  r9 : 000004fc  r8 : 00000100
    r7 : 00000608  r6 : 00000000  r5 : c43be4fc  r4 : c43be000
    r3 : c2969a80  r2 : 00000122  r1 : 00000000  r0 : c2969800
    Flags: nZCv  IRQs off  FIQs on  Mode SVC_32  ISA ARM  Segment user
    Control: 30c5383d  Table: 029ae1c0  DAC: fffffffd
    CPU: 0 PID: 0 Comm: swapper/0 Tainted: G         C        5.10.17-2-ARCH #1
    Hardware name: BCM2711
    [<c020f10c>] (unwind_backtrace) from [<c020aed0>] (show_stack+0x10/0x14)
    [<c020aed0>] (show_stack) from [<c0de7864>] (dump_stack+0xa4/0xc4)
    [<c0de7864>] (dump_stack) from [<c03010a8>] (kdb_main_loop+0x4bc/0x9a0)
    [<c03010a8>] (kdb_main_loop) from [<c030415c>] (kdb_stub+0x258/0x458)
    [<c030415c>] (kdb_stub) from [<c02f92e8>] (kgdb_cpu_enter+0x3e0/0x6fc)
    [<c02f92e8>] (kgdb_cpu_enter) from [<c02f9ca8>] (kgdb_handle_exception+0xe0/0x13c)
    more>
    (reboot)
    
  16. 33 remaining items

  17. 6by9 commented on Mar 5, 2021

    @6by9
    Contributor

    The ISR is taking FAR to long to run

    [  707.302030] unicam fe801000.csi: ISR: ISTA: 0x5, STA: 0xC003, sequence 2, lines done 0       <<< FS + LC + PI0
    [  707.316801] unicam fe801000.csi: ISR: FE: cur c53c9000, next c53c8c00
    [  707.342066] unicam fe801000.csi: ISR: End
    

    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.

  18. naushir commented on Mar 5, 2021

    @naushir
    Contributor

    Based on the original firwmare driver FE detection is sometimes dubious, so CMP0/PI0 is set up to also look for FE (VC = 0 and DT/short packet ID = 1 (FE))

    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.

  19. naushir commented on Mar 5, 2021

    @naushir
    Contributor

    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.

    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.

  20. 6by9 commented on Mar 5, 2021

    @6by9
    Contributor

    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_lisr with 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).

  21. naushir commented on Mar 5, 2021

    @naushir
    Contributor

    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

  22. mdevaev commented on Mar 5, 2021

    @mdevaev
    ContributorAuthor

    Building...

  23. mdevaev commented on Mar 5, 2021

    @mdevaev
    ContributorAuthor

    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):

    [  582.917872] unicam fe801000.csi: ISR: ISTA: 0x5, STA: 0xC003, sequence 84, lines done 0
    [  582.922439] unicam fe801000.csi: ISR: FE: cur 00000000, next 00000000
    [  582.931550] unicam fe801000.csi: Scheduling dummy buffer for node 0
    [  582.945364] unicam fe801000.csi: ISR: End
    [  582.945371] unicam fe801000.csi: ISR: ISTA: 0x4, STA: 0x4001, sequence 85, lines done 0
    [  582.949932] unicam fe801000.csi: schedule_next_buffer: next buf c52fec00, vb c52fec00
    [  582.959057] unicam fe801000.csi: ISR: End
    [  582.968014] unicam fe801000.csi: ISR: ISTA: 0x5, STA: 0xC003, sequence 85, lines done 0
    [  582.972592] unicam fe801000.csi: ISR: FE: cur 00000000, next c52fec00
    [  582.981739] unicam fe801000.csi: Scheduling dummy buffer for node 0
    [  582.981848] i2c-bcm2835 fe205000.i2c: i2c transfer timed out
    [  582.988810] unicam fe801000.csi: ISR: End
    [  582.988820] unicam fe801000.csi: ISR: ISTA: 0x4, STA: 0x4001, sequence 86, lines done 0
    [  582.995720] tc358743 10-000f: i2c_rd: reading register 0x14 from 0xf failed
    [  583.001917] unicam fe801000.csi: schedule_next_buffer: next buf c52fe400, vb c52fe400
    [  583.001920] unicam fe801000.csi: ISR: End
    

    Warning and null dereference not yet reproduced

  24. JakubVanek commented on Mar 5, 2021

    @JakubVanek
    Contributor

    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.

  25. mdevaev commented on Mar 5, 2021

    @mdevaev
    ContributorAuthor

    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 :)

  26. naushir commented on Mar 5, 2021

    @naushir
    Contributor

    Good news! I'll prepare a clean commit for a PR soon.

  27. mdevaev commented on Mar 5, 2021

    @mdevaev
    ContributorAuthor

    @naushir If it's not difficult, plz attach the new patch, I will test the final version.

  28. naushir commented on Mar 5, 2021

    @naushir
    Contributor

    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.

  29. mdevaev commented on Mar 5, 2021

    @mdevaev
    ContributorAuthor

    It's okay, I'll wait. Solving this problem is a top priority for me.

  30. mdevaev commented on Mar 7, 2021

    @mdevaev
    ContributorAuthor

    Given the presence of the fix, I think that this issue can be closed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions