Modifying kernel boot options (cmdline.txt) to increase CMA memory allocation for large pi-camera image capture

I am currently setting up a raspberry pi 4 with a pi-camera module 3 using the imx-708 dtoverlay and boot options to specify gpu_memory.

I am using libcamera to make use of the sensor and I have noticed that when trying to take default res pictures (4K I believe), I run into a dmaHeap allocation issue where the sensor fails to allocate capture buffers for my picture size. Here are both the app logs and the dimes logs showing a failure to allocate enough CMA.

App Logs

Mode selection for 4608:2592:12:P
    SRGGB10_CSI2P,1536x864/0 - Score: 10600
    SRGGB10_CSI2P,2304x1296/0 - Score: 8200
    SRGGB10_CSI2P,4608x2592/0 - Score: 1000
[0:14:31.665819084] [130]  INFO Camera camera.cpp:1183 configuring streams: (0) 4608x2592-YUV420 (1) 4608x2592-SBGGR10_CSI2P
[0:14:31.673482076] [136]  INFO RPI vc4.cpp:621 Sensor: platform/soc/fe205000.i2c/i2c-22/i2c-10/10-001a imx708 - Selected sensor format: 4608x2592-SBGGR10_1X10 - Selected unicam format: 4608x2592-pBAA
dmaHeap allocation failure for rpicam-apps0
ERROR: *** failed to allocate capture buffers for stream ***

Kernel Logs

[    0.000000] OF: reserved mem: initialized node linux,cma, compatible id shared-dma-pool
[    0.000000] Memory: 7726576K/8060928K available (10688K kernel code, 1716K rwdata, 4240K rodata, 28864K init, 999K bss, 268816K reserved, 65536K cma-reserved)
[    8.242366] vc_sm_cma: module is from the staging directory, the quality is unknown, you have been warned.
[    8.246661] bcm2835_vc_sm_cma_probe: Videocore shared memory driver
[  279.176348] cma: cma_alloc: linux,cma: alloc failed, req-size: 4374 pages, ret: -12

From the dmesg output, we can see that lib camera is attempting to allocate 4374*4kb=17496 (~18Mb) of contiguous cma memory. It surprised me that this failed at first because as you can see from earlier logs, I have 64MB of CMA memory allocated at kernel boot, meaning there should be enough CMA to allocate 18MB for a quick capture. I suspect that this allocation fails because while there is enough free CMA total, there may not be a single contiguous 18mb memory block available.

After browsing online, I tried increasing the amount of allocated CMA memory to 512MB by adding the cma-512 device tree overlay which should work on raspberry pi
Here is what my config.txt looks like:

gpu_mem_1024=256
gpu_mem_256=64
gpu_mem_512=128
start_x=1
camera_auto_detect=1
disable_fw_kms_setup=0
dtoverlay=imx708,cma-512
avoid_warnings=1
disable_splash=0
dtparam=i2c_arm=on
dtparam=spi=on
dtparam=audio=on
enable_uart=0
gpu_mem=128

Unfortunately that did not work as well. It seems others suggest that adding more cma memory is primarily doable through adding the cma=512MB option to

Request: edit cmdline.txt to add cma=512mb option

I currently see on some previous balena forums thread responses by team members that modifying cmdline.txt directly is not supported nor is it being considered for support. Is there any workaround to allow allocating more CMA memory

Hi, did you get to the bottom of the cma-512 overlay not working? That would be the recommended approach to do this.

Even though balenaCloud does not support kernel command line customization, you can still modify the /mnt/boot/cmdline.txt file on individual devices if you want to test.

1 Like

Hi Alex,

Thanks for replying so quickly, I did not realize that I could still manually modify cmdline.txt without the balena supervisor reverting the change. I’m not quite sure why the overlay is not working. I’m working on figuring out. For now this is good enough for testing