[4/4] bus/pci/bsd: Fix device existence check
Checks
Commit Message
Use open(2) instead of access(2) to check for the existence of the target
device. This avoids a possible race condition where the the device file is
removed after a successful call to access(2) but before open(2).
This also fixes any potential bugs associated with passing open(2)-style
flags into access(2). i.e. access(2) does not formally support the O_RDWR
flag.
Signed-off-by: Jake Freeland <jfree@FreeBSD.org>
---
drivers/bus/pci/bsd/pci.c | 19 ++++++++++++++-----
1 file changed, 14 insertions(+), 5 deletions(-)
Comments
On 5/6/2025 7:40 PM, Jake Freeland wrote:
> Use open(2) instead of access(2) to check for the existence of the target
> device. This avoids a possible race condition where the the device file is
> removed after a successful call to access(2) but before open(2).
>
> This also fixes any potential bugs associated with passing open(2)-style
> flags into access(2). i.e. access(2) does not formally support the O_RDWR
> flag.
>
> Signed-off-by: Jake Freeland <jfree@FreeBSD.org>
> ---
It's odd that static analysis didn't flag this, as that's the kind of
thing it would normally complain about.
Acked-by: Anatoly Burakov <anatoly.burakov@intel.com>
@@ -106,20 +106,29 @@ pci_uio_alloc_resource(struct rte_pci_device *dev,
{
char devname[PATH_MAX]; /* contains the /dev/uioX */
struct rte_pci_addr *loc;
+ int fd;
loc = &dev->addr;
snprintf(devname, sizeof(devname), "/dev/uio@pci:%u:%u:%u",
dev->addr.bus, dev->addr.devid, dev->addr.function);
- if (access(devname, O_RDWR) < 0) {
- PCI_LOG(WARNING, " "PCI_PRI_FMT" not managed by UIO driver, skipping",
- loc->domain, loc->bus, loc->devid, loc->function);
- return 1;
+ fd = open(devname, O_RDWR);
+ if (fd < 0) {
+ if (errno == ENOENT) {
+ PCI_LOG(WARNING, PCI_PRI_FMT" not managed by UIO "
+ "driver, skipping", loc->domain,
+ loc->bus, loc->devid, loc->function);
+ return 1;
+ }
+ PCI_LOG(ERR, "Failed to open device file for " PCI_PRI_FMT
+ " (%s)", loc->domain, loc->bus, loc->devid,
+ loc->function, devname);
+ return -1;
}
/* save fd if in primary process */
- if (rte_intr_fd_set(dev->intr_handle, open(devname, O_RDWR))) {
+ if (rte_intr_fd_set(dev->intr_handle, fd)) {
PCI_LOG(WARNING, "Failed to save fd");
goto error;
}