nxp-mcuxpresso / rpmsg-lite

RPMsg implementation for small MCUs
BSD 3-Clause "New" or "Revised" License
235 stars 74 forks source link

use env_rmb() is for what? #29

Closed Hans5278 closed 1 year ago

Hans5278 commented 1 year ago

I have a question about the use of _env_rmb()_ function in _rpmsglite/virtio/virtqueue.c _virtqueue_getbuffer. It seems that both _usedidx and uep are dependent on the following code, so doesn't the program itself reorder? _envrmb() is used here to prevent reordering of which code?

MichalPrincNXP commented 1 year ago

Hello @Hans5278 , env_rmb() is a function that inserts read memory barrier and is mostly implemented as dsb instruction, ensuring consequent data (in shared memory) read correctness (all previous memory accesses are finished). This is not related to code reordering.

Hans5278 commented 1 year ago

Why need _envrmb() ? If the operation of read uep is not finished, what is the value of uep->id? I mean the read operation of _envrmb() is based on the finish of uep read operation. Could you help me understand this question?

MichalPrincNXP commented 1 year ago

It deals with data synchronization barrier, i.e. we need to be sure we read correct uep->id value from the shared memory. env_rmb() prevents incorrect value read while the opposite core is just writing a new value into that shared memory address. It simply waits until all previous memory transactions are done before reading that memory.

Hans5278 commented 1 year ago

Linux virtio https://github.com/torvalds/linux/blob/master/drivers/virtio/virtio_ring.c in line 816-823 is wrtten differently than you did here, but both of you do the same thing. It makes me confused.