Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

util: add file locking for PCIe access. #54

Merged
merged 1 commit into from
Oct 4, 2024
Merged

util: add file locking for PCIe access. #54

merged 1 commit into from
Oct 4, 2024

Conversation

ericonr
Copy link
Member

@ericonr ericonr commented Sep 23, 2024

This way, only one application using the library can access a given device at a time.

We use a file descriptor pointing to BAR0 (via the resource0 file) because it's the first one to be opened (i.e. we fail early), and because it makes the most sense semantically: the issue with sharing a device between processes is that they will need to access different page indexes, which are controlled by BAR0.

We chose to use Open File Description (OFD) locking because it has more predictable behavior [1] and is POSIX, unlike the flock(2) API.

No locking is added for the serial mode because it's not used in production, and it's believed that the kernel will already report the device as busy when another process has already opened it.

[1] https://lwn.net/Articles/586904/

@ericonr
Copy link
Member Author

ericonr commented Sep 23, 2024

@ericonr
Copy link
Member Author

ericonr commented Sep 24, 2024

I have been informed that the POSIX text doesn't specify if the reference is to the file itself or to the OFD. So for now I might hold off on merging this (or find some reference for the Linux behavior instead)

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
This way, only one application using the library can access a given
device at a time.

We use the file descriptor pointing to BAR0 (via the resource0 file)
because it's the first one to be opened (i.e. we fail early), and
because it makes the most sense semantically: the issue with sharing a
device between processes is that they will need to access different page
indexes, which are controlled by BAR0.

We chose to use Open File Description (OFD) locking because it has more
predictable behavior [1] and is POSIX.1-2024 [2], unlike the flock(2)
API. Furthermore, we rely on mmap keeping a reference to the file
descriptor, therefore holding the lock while the mapping is active [3]
[4]:

    The mmap() function shall add an extra reference to the file
    associated with the file descriptor fildes which is not removed by a
    subsequent close() on that file descriptor. This reference shall be
    removed when there are no more mappings to the file.

No locking is added for the serial mode because the kernel will report
the device as busy when another process has already opened it.

[1] https://lwn.net/Articles/586904/
[2] https://pubs.opengroup.org/onlinepubs/9799919799/functions/fcntl.html
[3] https://pubs.opengroup.org/onlinepubs/9799919799/functions/mmap.html
[4] https://unix.stackexchange.com/q/784020/437538
@ericonr ericonr merged commit b70d15a into master Oct 4, 2024
25 checks passed
@ericonr ericonr deleted the locking branch October 4, 2024 16:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants