Skip to contentSkip to conversation

xf86drm: adjust device node path for minor base

PATCH Superseded Jonathan Gray 2 participants 12 messages Dec 10, 2016 – Dec 24, 2016

lore
Share & tools
lore
#0 lore raw

PATCH 1/3 xf86drm: adjust device node path for minor base

When constructing a path to a device node the minor number retrieved from fstat needs to have the offset of the node type subtracted from it. Control and render node types have the same major as the primary node but each has their own block of minor types at fixed offsets.

---
 xf86drm.c | 16 ++++++++++++----
 1 file changed, 12 insertions(+), 4 deletions(-)
xf86drm.c +12 -4
index 2e8c956..6705605 100644
@@ -2838,7 +2838,7 @@ out_close_dir:
     char buf[PATH_MAX + 1];
     const char *dev_name;
     unsigned int maj, min;
-    int n;
+    int n, base;
 
     if (fstat(fd, &sbuf))
         return NULL;
@@ -2863,7 +2863,11 @@ out_close_dir:
         return NULL;
     };
 
-    n = snprintf(buf, sizeof(buf), dev_name, DRM_DIR_NAME, min);
+    base = drmGetMinorBase(type);
+    if (base < 0 || min < base)
+        return NULL;
+
+    n = snprintf(buf, sizeof(buf), dev_name, DRM_DIR_NAME, min - base);
     if (n == -1 || n >= sizeof(buf))
         return NULL;
 
@@ -3262,7 +3266,7 @@ int drmGetDevice2(int fd, uint32_t flags, drmDevicePtr *device)
     char             node[PATH_MAX + 1];
     const char      *dev_name;
     int              node_type, subsystem_type;
-    int              maj, min, n, ret;
+    int              maj, min, n, ret, base;
 
     if (fd == -1 || device == NULL)
         return -EINVAL;
@@ -3294,7 +3298,11 @@ int drmGetDevice2(int fd, uint32_t flags, drmDevicePtr *device)
         return -EINVAL;
     };
 
-    n = snprintf(node, PATH_MAX, dev_name, DRM_DIR_NAME, min);
+    base = drmGetMinorBase(node_type);
+    if (base < 0 || min < base)
+        return -EINVAL;
+
+    n = snprintf(node, PATH_MAX, dev_name, DRM_DIR_NAME, min - base);
     if (n == -1 || n >= PATH_MAX)
       return -errno;
     if (stat(node, &sbuf))
Signature · 6 lines
2.10.2

_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel
#1 lore raw

PATCH 2/3 xf86drm: add a non-sysfs version of drmGetDeviceNameFromFd2

Implement a generic drmGetDeviceNameFromFd2() to use on non-linux systems without sysfs.

---
 xf86drm.c | 44 ++++++++++++++++++++++++++++++++++++++++++--
 1 file changed, 42 insertions(+), 2 deletions(-)
xf86drm.c +42 -2
index 6705605..afce8ec 100644
@@ -3627,7 +3627,47 @@ char *drmGetDeviceNameFromFd2(int fd)
     fclose(f);
     return device_name;
 #else
-#warning "Missing implementation of drmGetDeviceNameFromFd2"
-    return NULL;
+    struct stat      sbuf;
+    char             node[PATH_MAX + 1];
+    const char      *dev_name;
+    int              node_type;
+    int              maj, min, n, base;
+
+    if (fstat(fd, &sbuf))
+        return NULL;
+
+    maj = major(sbuf.st_rdev);
+    min = minor(sbuf.st_rdev);
+
+    if (maj != DRM_MAJOR || !S_ISCHR(sbuf.st_mode))
+        return NULL;
+
+    node_type = drmGetMinorType(min);
+    if (node_type == -1)
+        return NULL;
+
+    switch (node_type) {
+    case DRM_NODE_PRIMARY:
+        dev_name = DRM_DEV_NAME;
+        break;
+    case DRM_NODE_CONTROL:
+        dev_name = DRM_CONTROL_DEV_NAME;
+        break;
+    case DRM_NODE_RENDER:
+        dev_name = DRM_RENDER_DEV_NAME;
+        break;
+    default:
+        return NULL;
+    };
+
+    base = drmGetMinorBase(node_type);
+    if (base < 0 || min < base)
+        return NULL;
+
+    n = snprintf(node, PATH_MAX, dev_name, DRM_DIR_NAME, min - base);
+    if (n == -1 || n >= PATH_MAX)
+      return NULL;
+
+    return strdup(node);
 #endif
 }
Signature · 6 lines
2.10.2

_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel
#2 lore raw

PATCH 3/3 xf86drm: don't fatal on per device error in drmGetDevice[s]2

In reply to Jonathan Gray

When iterating over all the device nodes if drmProcessPciDevice() returned an error for any node the function would return an error, ignoring any valid nodes.

The result of this on OpenBSD where drmProcessPciDevice() results in device nodes being opened to issue ioctls to get pci data was that data obtained from /dev/drm0 would be ignored if /dev/drm1 could not be opened.

---
 xf86drm.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
xf86drm.c +2 -2
index afce8ec..2dc679a 100644
@@ -3383,7 +3383,7 @@ int drmGetDevice2(int fd, uint32_t flags, drmDevicePtr *device)
         case DRM_BUS_PCI:
             ret = drmProcessPciDevice(&d, node, node_type, maj, min, true, flags);
             if (ret)
-                goto free_devices;
+                continue;
 
             break;
         default:
@@ -3514,7 +3514,7 @@ int drmGetDevices2(uint32_t flags, drmDevicePtr devices[], int max_devices)
             ret = drmProcessPciDevice(&device, node, node_type,
                                       maj, min, devices != NULL, flags);
             if (ret)
-                goto free_devices;
+                continue;
 
             break;
         default:
Signature · 6 lines
2.10.2

_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel
#3 lore raw

Re: [PATCH libdrm 3/3] xf86drm: don't fatal on per device error in drmGetDevice[s]2

Quoted 9 lines · Jonathan Gray

When iterating over all the device nodes if drmProcessPciDevice() returned an error for any node the function would return an error, ignoring any valid nodes.

The result of this on OpenBSD where drmProcessPciDevice() results in device nodes being opened to issue ioctls to get pci data was that data obtained from /dev/drm0 would be ignored if /dev/drm1 could not be opened.

Out of curiosity - did you actually hit such an issue or you've spotted it by observation ?
I'm not too sure which route is the "correct" one here, but I believe you got it spot on.

I'll push this alongside v2 of 1/3 and 2/3.

Thanks !
Emil
_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel

#4 lore raw

Re: [PATCH libdrm 3/3] xf86drm: don't fatal on per device error in drmGetDevice[s]2

Quoted 15 lines · Emil Velikov
Quoted 9 lines · Jonathan Gray

When iterating over all the device nodes if drmProcessPciDevice() returned an error for any node the function would return an error, ignoring any valid nodes.

The result of this on OpenBSD where drmProcessPciDevice() results in device nodes being opened to issue ioctls to get pci data was that data obtained from /dev/drm0 would be ignored if /dev/drm1 could not be opened.

Out of curiosity - did you actually hit such an issue or you've spotted it by observation ?
I'm not too sure which route is the "correct" one here, but I believe you got it spot on.

Quoted 4 lines

I'll push this alongside v2 of 1/3 and 2/3.

Thanks !
Emil

Currently by default on login we change ownership of /dev/drm0 via fbtab when a user logs in on a glass console but no other /dev/drm nodes.

I saw this before the OpenBSD specific path was added and was reminded of it when testing one of the functions that isn't used by Mesa yet.
_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel

#5 lore raw
In reply to Jonathan Gray
Quoted 24 lines · Jonathan Gray

When constructing a path to a device node the minor number retrieved from fstat needs to have the offset of the node type subtracted from it. Control and render node types have the same major as the primary node but each has their own block of minor types at fixed offsets.

---
 xf86drm.c | 16 ++++++++++++----
 1 file changed, 12 insertions(+), 4 deletions(-)
xf86drm.c +1 -1
index 2e8c956..6705605 100644
@@ -2838,7 +2838,7 @@ out_close_dir:
     char buf[PATH_MAX + 1];
     const char *dev_name;
     unsigned int maj, min;
-    int n;
+    int n, base;
 
     if (fstat(fd, &sbuf))
         return NULL;

xf86drm.c out_close_dir: · line 2867

Quoted 6 lines
xf86drm.c +2 -1
@@ -2863,7 +2863,11 @@ out_close_dir:
         return NULL;
     };
 
-    n = snprintf(buf, sizeof(buf), dev_name, DRM_DIR_NAME, min);
+    base = drmGetMinorBase(type);
+    if (base < 0 || min < base)

min < base seems bogus, since it will never be true, right ? If so can we drop it please.

Same goes below and in 2/3.
Emil
_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel

#6 lore raw
Quoted 28 lines · Emil Velikov
Quoted 27 lines · Jonathan Gray

When constructing a path to a device node the minor number retrieved from fstat needs to have the offset of the node type subtracted from it. Control and render node types have the same major as the primary node but each has their own block of minor types at fixed offsets.

---
 xf86drm.c | 16 ++++++++++++----
 1 file changed, 12 insertions(+), 4 deletions(-)
xf86drm.c +1 -1
index 2e8c956..6705605 100644
@@ -2838,7 +2838,7 @@ out_close_dir:
     char buf[PATH_MAX + 1];
     const char *dev_name;
     unsigned int maj, min;
-    int n;
+    int n, base;
 
     if (fstat(fd, &sbuf))
         return NULL;
@@ -2863,7 +2863,11 @@ out_close_dir:
         return NULL;
     };
 
Quoted 8 lines

xf86drm.c out_close_dir: · line 2867

Quoted 3 lines
xf86drm.c +2 -1
-    n = snprintf(buf, sizeof(buf), dev_name, DRM_DIR_NAME, min);
+    base = drmGetMinorBase(type);
+    if (base < 0 || min < base)

min < base seems bogus, since it will never be true, right ? If so can we drop it please.

Same goes below and in 2/3.
Emil

For drmGetDevice2() and drmGetDeviceNameFromFd2() yes as it should be caught by drmGetMinorType().

For drmGetMinorNameForFD() it would be possible for a minor number to be wrong and have it not caught as the type comes from an argument not inspection of the /dev node. A sanity check of the type argument could be made based on the minor found on disk but that ends up being the same kind of test.
_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel

#7 lore raw
Quoted 38 lines · Jonathan Gray
Quoted 28 lines · Emil Velikov
Quoted 27 lines · Jonathan Gray

When constructing a path to a device node the minor number retrieved from fstat needs to have the offset of the node type subtracted from it. Control and render node types have the same major as the primary node but each has their own block of minor types at fixed offsets.

---
 xf86drm.c | 16 ++++++++++++----
 1 file changed, 12 insertions(+), 4 deletions(-)
xf86drm.c +1 -1
index 2e8c956..6705605 100644
@@ -2838,7 +2838,7 @@ out_close_dir:
     char buf[PATH_MAX + 1];
     const char *dev_name;
     unsigned int maj, min;
-    int n;
+    int n, base;
 
     if (fstat(fd, &sbuf))
         return NULL;
@@ -2863,7 +2863,11 @@ out_close_dir:
         return NULL;
     };
 
Quoted 8 lines

xf86drm.c out_close_dir: · line 2867

Quoted 3 lines
xf86drm.c +2 -1
-    n = snprintf(buf, sizeof(buf), dev_name, DRM_DIR_NAME, min);
+    base = drmGetMinorBase(type);
+    if (base < 0 || min < base)

min < base seems bogus, since it will never be true, right ? If so can we drop it please.

Same goes below and in 2/3.
Emil

Quoted 8 lines

For drmGetDevice2() and drmGetDeviceNameFromFd2() yes as it should be caught by drmGetMinorType().

For drmGetMinorNameForFD() it would be possible for a minor number to be wrong and have it not caught as the type comes from an argument not inspection of the /dev node. A sanity check of the type argument could be made based on the minor found on disk but that ends up being the same kind of test.

K-kind of ...

There is no guarantee that that card0 relate to renderD128 and vice versa. Thus strictly speaking most/all the implementations that rely on drmGetMinorBase are buggy.
IIIRC there was a discussion why those are not (and cannot) be made reliable, which inspired all the nasty sysfs parsing.

Nothing [new] should rely on drmOpen and friends, so we can ignore the drmGetMinorNameForFD corner cases.
... I hope

Thanks
Emil
_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel

#8 lore raw

PATCH v2 1/3 xf86drm: adjust device node path for minor base

In reply to Jonathan Gray

When constructing a path to a device node the minor number retrieved from fstat needs to have the offset of the node type subtracted from it. Control and render node types have the same major as the primary node but each has their own block of minor types at fixed offsets.

v2: remove min < base test as requested by Emil

---
 xf86drm.c | 16 ++++++++++++----
 1 file changed, 12 insertions(+), 4 deletions(-)
xf86drm.c +12 -4
index b5eeeb09..f6850aa2 100644
@@ -2838,7 +2838,7 @@ out_close_dir:
     char buf[PATH_MAX + 1];
     const char *dev_name;
     unsigned int maj, min;
-    int n;
+    int n, base;
 
     if (fstat(fd, &sbuf))
         return NULL;
@@ -2863,7 +2863,11 @@ out_close_dir:
         return NULL;
     };
 
-    n = snprintf(buf, sizeof(buf), dev_name, DRM_DIR_NAME, min);
+    base = drmGetMinorBase(type);
+    if (base < 0)
+        return NULL;
+
+    n = snprintf(buf, sizeof(buf), dev_name, DRM_DIR_NAME, min - base);
     if (n == -1 || n >= sizeof(buf))
         return NULL;
 
@@ -3262,7 +3266,7 @@ int drmGetDevice2(int fd, uint32_t flags, drmDevicePtr *device)
     char             node[PATH_MAX + 1];
     const char      *dev_name;
     int              node_type, subsystem_type;
-    int              maj, min, n, ret;
+    int              maj, min, n, ret, base;
 
     if (fd == -1 || device == NULL)
         return -EINVAL;
@@ -3294,7 +3298,11 @@ int drmGetDevice2(int fd, uint32_t flags, drmDevicePtr *device)
         return -EINVAL;
     };
 
-    n = snprintf(node, PATH_MAX, dev_name, DRM_DIR_NAME, min);
+    base = drmGetMinorBase(node_type);
+    if (base < 0)
+        return -EINVAL;
+
+    n = snprintf(node, PATH_MAX, dev_name, DRM_DIR_NAME, min - base);
     if (n == -1 || n >= PATH_MAX)
       return -errno;
     if (stat(node, &sbuf))
Signature · 6 lines
2.11.0

_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel
#9 lore raw

PATCH v2 2/3 xf86drm: add a non-sysfs version of drmGetDeviceNameFromFd2

Implement a generic drmGetDeviceNameFromFd2() to use on non-linux systems without sysfs.

v2: remove min < base test as requested by Emil

---
 xf86drm.c | 44 ++++++++++++++++++++++++++++++++++++++++++--
 1 file changed, 42 insertions(+), 2 deletions(-)
xf86drm.c +42 -2
index f6850aa2..f684c017 100644
@@ -3627,7 +3627,47 @@ char *drmGetDeviceNameFromFd2(int fd)
     fclose(f);
     return device_name;
 #else
-#warning "Missing implementation of drmGetDeviceNameFromFd2"
-    return NULL;
+    struct stat      sbuf;
+    char             node[PATH_MAX + 1];
+    const char      *dev_name;
+    int              node_type;
+    int              maj, min, n, base;
+
+    if (fstat(fd, &sbuf))
+        return NULL;
+
+    maj = major(sbuf.st_rdev);
+    min = minor(sbuf.st_rdev);
+
+    if (maj != DRM_MAJOR || !S_ISCHR(sbuf.st_mode))
+        return NULL;
+
+    node_type = drmGetMinorType(min);
+    if (node_type == -1)
+        return NULL;
+
+    switch (node_type) {
+    case DRM_NODE_PRIMARY:
+        dev_name = DRM_DEV_NAME;
+        break;
+    case DRM_NODE_CONTROL:
+        dev_name = DRM_CONTROL_DEV_NAME;
+        break;
+    case DRM_NODE_RENDER:
+        dev_name = DRM_RENDER_DEV_NAME;
+        break;
+    default:
+        return NULL;
+    };
+
+    base = drmGetMinorBase(node_type);
+    if (base < 0)
+        return NULL;
+
+    n = snprintf(node, PATH_MAX, dev_name, DRM_DIR_NAME, min - base);
+    if (n == -1 || n >= PATH_MAX)
+      return NULL;
+
+    return strdup(node);
 #endif
 }
Signature · 6 lines
2.11.0

_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel
#10 lore raw

PATCH v2 3/3 xf86drm: don't fatal on per device error in drmGetDevice[s]2

In reply to Jonathan Gray

When iterating over all the device nodes if drmProcessPciDevice() returned an error for any node the function would return an error, ignoring any valid nodes.

The result of this on OpenBSD where drmProcessPciDevice() results in device nodes being opened to issue ioctls to get pci data was that data obtained from /dev/drm0 would be ignored if /dev/drm1 could not be opened.

---
 xf86drm.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
xf86drm.c +2 -2
index f684c017..7d7df184 100644
@@ -3383,7 +3383,7 @@ int drmGetDevice2(int fd, uint32_t flags, drmDevicePtr *device)
         case DRM_BUS_PCI:
             ret = drmProcessPciDevice(&d, node, node_type, maj, min, true, flags);
             if (ret)
-                goto free_devices;
+                continue;
 
             break;
         default:
@@ -3514,7 +3514,7 @@ int drmGetDevices2(uint32_t flags, drmDevicePtr devices[], int max_devices)
             ret = drmProcessPciDevice(&device, node, node_type,
                                       maj, min, devices != NULL, flags);
             if (ret)
-                goto free_devices;
+                continue;
 
             break;
         default:
Signature · 6 lines
2.11.0

_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel
#11 lore raw
In reply to Jonathan Gray
Quoted 7 lines · Jonathan Gray

When constructing a path to a device node the minor number retrieved from fstat needs to have the offset of the node type subtracted from it. Control and render node types have the same major as the primary node but each has their own block of minor types at fixed offsets.

v2: remove min < base test as requested by Emil

Thanks Jonathan, I've pushed the lot to master !

-Emil
_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel