How to register external contiguous memory buffer from v4l2 device

Goran Pantar <[email protected]> Wed, 19 Jun 2024 05:29:22 +0000
Newsgroups dev.linux.lists.spdk
Message-ID <[email protected]>
Hello,

I have v4l2 capture device that provides camera video buffers. I want to st=
ore the content of this buffers to the NVMe PCIe disk.
Video buffer memory is allocated in contiguous DMA memory by the v4l2 Kerne=
l driver. Physical address of buffer start is aligned to 16MB. Size of vide=
o buffers is around 15.8MB.

When buffer is mmap-ed to userspace this results in 4KB aligned  virtual ad=
dress.

Original code for write to disk uses memcpy which I would like to avoid:
   ...
    int mret =3D spdk_mem_register(buff.virt,  buff_size); // THIS fails
    if (spdk_vtophys(buff.virt, &buff_size) =3D=3D SPDK_VTOPHYS_ERROR)
    {
       write_buff =3D spdk_malloc(buff_size, 0x1000, NULL, SPDK_ENV_SOCKET_=
ID_ANY, SPDK_MALLOC_DMA);
       memcpy(write_buff, buff.virt, buff_size);
       spdk_free_dma_buff =3D 1;
    }
   ...
   spdk_nvme_ns_cmd_write(...)
   ....

I have tried using spdk_mem_register function to register this buffer but i=
t fails.
Which function/API  do you recommend for registering external memory in thi=
s case?

I'm on x86 system with unified memory. Video capture is PCIe DMA device con=
trolled by v4l2 driver. NVMe disks use vfio-pci driver.
        # /usr/scripts/setup.sh status
        Type     BDF             Vendor Device NUMA    Driver           Dev=
ice     Block devices
        NVMe     0000:05:00.0    1344   51c3   0       vfio-pci         -  =
        -



I also have a following workaround:
-       I setup a 1GB huge page. Memory for video buffers is allocated usin=
g spdk_alloc/spdk_free from the 1GB hugetlbfs.
-       I'm using V4L2_MEMORY_USERPTR to provide the buffers to the v4l2 de=
vice. Video DMA is using this buffers to write and the SPDK for reading wit=
hout copying.

Thank you,
Goran Pantar


----- Disclaimer -----
This e-mail message and its attachments may contain privileged and/or confi=
dential information. Please do not read the message if You are not its desi=
gnated recipient. If You have received this message by mistake, please info=
rm its sender and destroy the original message and its attachments without =
reading or storing of any kind. Any unauthorized use, distribution, reprodu=
ction or publication of this message is forbidden.

----- Pravne napomene -----
Ova elektronicka poruka i njeni prilozi mogu sadrzavati povlastene informac=
ije i/ili povjerljive informacije. Molimo Vas da poruku ne citate ako niste=
 njen naznaceni primatelj. Ako ste ovu poruku primili greskom, molimo Vas d=
a o tome obavijestite posiljatelja i da izvornu poruku i njene privitke uni=
stite bez citanja ili bilo kakvog pohranjivanja. Svaka neovlastena upotreba=
, distribucija, reprodukcija ili priopcavanje ove poruke zabranjena je.