nutanix / libvfio-user

framework for emulating devices in userspace
BSD 3-Clause "New" or "Revised" License
162 stars 51 forks source link

remove need for mutex in client sample #749

Closed w-henderson closed 1 year ago

w-henderson commented 1 year ago

For writes to BAR1 (which come from the fake guest thread), we now send the command with the no_reply flag set so we don't interfere with the main thread. This means we don't require a mutex.

jlevon commented 1 year ago

I'm missing the context as to why we want to make this change

w-henderson commented 1 year ago

@jlevon

Context: https://github.com/nutanix/libvfio-user/pull/747#discussion_r1262517464

In v2 we make more use of the mutex as it's no longer access_region that's being called while the fake guest is running, it's also all the migration commands. In my implementation I just moved the mutex in access_region to be a global mutex to ensure that no commands can be sent to the server while still waiting for a response. However, if the fake guest just doesn't ask for responses (which it doesn't need to, as it's only writing to BAR1 and, being a sample, the writes shouldn't ever fail), we don't need a mutex at all as all responses are for the main thread.

jlevon commented 1 year ago

If previously we could access the socket from both threads simultaneously, why is it not still possible? I get that we'll no longer need to get replies in the fake guest thread, but can't we still race at the tran_sock layer? What prevents both threads from using the fd at the same time?

w-henderson commented 1 year ago

You're right, I was working under the assumption that sendmsg is thread-safe, which it is not - should I just close this PR then?