vpelletier / python-functionfs

Pythonic API for linux's functionfs
GNU General Public License v3.0
40 stars 13 forks source link

[Question] Is mmap really necessary for IN endpoint buffers? #18

Closed dogtopus closed 3 years ago

dogtopus commented 3 years ago

In python-libaio docs, there's the note:

For example, when submitting AIO blocks to an USB gadget endpoint file, the block should be aligned to page boundaries because some USB Device Controllers do not have the ability to read/write partial pages.

In python, this means mmap should be used to allocate such buffer instead of just any bytearray.

And I'm pretty sure there is at least one mention about EndpointINFile.submit() being "zero-copying", which implies that the kernel directly ingests the buffer provided by the submit() call (via libaio) and push it to the UDC driver.

Since there are little information about this, here's the question: Is what I said actually true? Do I need to use mmap to allocate buffers for use with EndpointINFile.submit() (and the return value of onComplete()) to get best compatibility?

vpelletier commented 3 years ago

Is what I said actually true?

Yes, this is consistent with my understanding.

Do I need to use mmap to allocate buffers for use with EndpointINFile.submit() (and the return value of onComplete()) to get best compatibility?

Yes, for lack of (...at least to my knowledge) any other way in python to get memory-aligned buffers (page-aligned in the case of mmap, and I expect that no UDC would have alignment constraints stricter than a page). This is the other edge of the zero-copy sword: nothing in the code path will more any byte from the handed-down buffer, so they have to be in the correct spot from the beginning. So obscure constraints originating in the silicon become apparent to userland.

Maybe not all UDC have this constraint, and maybe the alignment can be enforced before handing the buffer over to the UDC, but then performance is worse than if no alignment fix was needed.

dogtopus commented 3 years ago

Thanks for clarifying this.

Maybe not all UDC have this constraint, and maybe the alignment can be enforced before handing the buffer over to the UDC, but then performance is worse than if no alignment fix was needed.

The test devices I use (PC with dummy_hcd and Pinephone which is powered by Allwinner A64) don't require aligned buffer. However that's just 1 real device so idk about the big picture.