RPI CM5 + Picamera2: switch_config results in Failed to queue buffer 0: Invalid argument ERROR RPISTREAM rpi_stream.cpp:276 Failed to queue buffer for CFE Image

Hello,

I’m running balenaOS with kernel 6.12.61-v8-16k on a Raspberry Pi 5 with an IMX477 camera. I need to switch between picamera2 configurations at runtime (to change sensor mode/resolution). On RPi OS 6.6.74+rpt-rpi-v8 this works with a ~0.3s delay. On balenaOS it requires ~8 seconds, and anything shorter produces:

ERROR V4L2 v4l2_videodevice.cpp:1825 /dev/video12[23:cap]: Failed to queue buffer 0: Invalid argument

After investigation, the root cause appears to be a kernel driver issue. Running dmesg immediately after a config switch shows:

WARNING: CPU: 2 PID: 88153 at drivers/media/v4l2-core/v4l2-subdev.c:457 call_s_stream+0xe8/0x110 [videodev]
__vb2_queue_cancel+0x38/0x2e0 [videobuf2_common]
vb2_core_streamoff+0x2c/0xe0 [videobuf2_common]

This indicates vb2_queue_cancel is calling s_stream(0) on the IMX477 subdev while buffers are still in flight, leaving the sensor in an unknown state and forcing a full cold I2C reinit (~8s) on every restart.

Comparing the rp1_cfe driver between the two systems confirms they are built from different source:

  • balenaOS 6.12.61-v8-16k: srcversion: 585B4BF4EC144D534980F63

  • RPi OS 6.6.74+rpt-rpi-v8: srcversion: 0D2B276CED3198F6A45B339

The RPi OS kernel is built from the RPi foundation’s rpi-6.6.y branch which contains Pi-specific fixes for rp1_cfe teardown that were never upstreamed to mainline. BalenaOS’s 6.12.61 appears to be built from vanilla mainline and is missing these patches.

Could balenaOS switch to building from github.com/raspberrypi/linux rpi-6.12.y branch (which the RPi foundation actively maintains with these fixes) rather than vanilla mainline 6.12.y?

Hi @antoineschmider

Great question, and it sounds like you’ve done a thorough debug.

I’ve flagged this with the OS engineers, and we’ll get back to you!

Hi,

This is the process we use to update the kernel balena-raspberrypi/layers/meta-balena-raspberrypi/recipes-kernel/linux/linux-raspberrypi_6.12.inc at master · balena-os/balena-raspberrypi · GitHub

Maybe we just need to update the SRCREV from the rpi-6.12.y branch?

Can you point to the commit in the rpi-6.12.y branch that fixes the issues you mentioned?

Hello,

Thanks for the fast reply.

I think the fix I am looking for is in

598462fb0279b3615c40430ce9f3616e0a9d44b8
media/platform/raspberypi/rp1_cfe: Candidate fix for #5821

AuthorDate: Mon May 19 16:16:20 2025 +0100
CommitDate: Tue Jun 3 12:34:46 2025 +0100

The commit message says:

To avoid lost frame start in a subsequent session, avoid setting the number of lanes back to 1 or putting CSI-2 Host into reset.

That sounds very close to the issue I’m seeing when switching Picamera2 configurations at runtime: the next camera session appears to start from a bad/unknown CFE/CSI state unless I wait several seconds.