Skip to contentSkip to conversation

xf86drm.c: Use integer logarithm.

PATCH Applied Paul Gofman 4 participants 6 messages Oct 26, 2020 – Oct 28, 2020

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 xf86drm.c: Use integer logarithm.

log() is affected by FP control word and can provide inaccurate result.

Fixes Killer Instinct under Wine not being able to find AMD vulkan device.

---
With the rounding mode the application sets (unsigned int)log2(4) is 1. The log2_int() implemetation is copied from radeon/radeon_surface.c.

 xf86drm.c | 18 +++++++++++++++++-
 1 file changed, 17 insertions(+), 1 deletion(-)
xf86drm.c +17 -1
index 50a6f092..dbb7c14b 100644
@@ -124,6 +124,22 @@ static drmServerInfoPtr drm_server_info;
 static bool drmNodeIsDRM(int maj, int min);
 static char *drmGetMinorNameForFD(int fd, int type);
 
+static unsigned log2_int(unsigned x)
+{
+    unsigned l;
+
+    if (x < 2) {
+        return 0;
+    }
+    for (l = 2; ; l++) {
+        if ((unsigned)(1 << l) > x) {
+            return l - 1;
+        }
+    }
+    return 0;
+}
+
+
 drm_public void drmSetServerInfo(drmServerInfoPtr info)
 {
     drm_server_info = info;
@@ -4001,7 +4017,7 @@ static void drmFoldDuplicatedDevices(drmDevicePtr local_devices[], int count)
         for (j = i + 1; j < count; j++) {
             if (drmDevicesEqual(local_devices[i], local_devices[j])) {
                 local_devices[i]->available_nodes |= local_devices[j]->available_nodes;
-                node_type = log2(local_devices[j]->available_nodes);
+                node_type = log2_int(local_devices[j]->available_nodes);
                 memcpy(local_devices[i]->nodes[node_type],
                        local_devices[j]->nodes[node_type], drmGetMaxNodeName());
                 drmFreeDevice(&local_devices[j]);
Signature · 6 lines
2.26.2

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

I've applied this to libdrm master.

Thanks,
Dave.

Quoted 53 lines · Paul Gofman

log() is affected by FP control word and can provide inaccurate result.

Fixes Killer Instinct under Wine not being able to find AMD vulkan device.

---
With the rounding mode the application sets (unsigned int)log2(4) is 1. The log2_int() implemetation is copied from radeon/radeon_surface.c.

 xf86drm.c | 18 +++++++++++++++++-
 1 file changed, 17 insertions(+), 1 deletion(-)
xf86drm.c +17 -2
index 50a6f092..dbb7c14b 100644
@@ -124,6 +124,22 @@ static drmServerInfoPtr drm_server_info;
 static bool drmNodeIsDRM(int maj, int min);
 static char *drmGetMinorNameForFD(int fd, int type);
 
+static unsigned log2_int(unsigned x)
+{
+    unsigned l;
+
+    if (x < 2) {
+        return 0;
+    }
+    for (l = 2; ; l++) {
+        if ((unsigned)(1 << l) > x) {
+            return l - 1;
+        }
+    }
+    return 0;
+}
+
+
 drm_public void drmSetServerInfo(drmServerInfoPtr info)
 {
     drm_server_info = info;
@@ -4001,7 +4017,7 @@ static void drmFoldDuplicatedDevices(drmDevicePtr local_devices[], int count)
         for (j = i + 1; j < count; j++) {
             if (drmDevicesEqual(local_devices[i], local_devices[j])) {
                 local_devices[i]->available_nodes |= local_devices[j]->available_nodes;
-                node_type = log2(local_devices[j]->available_nodes);
+                node_type = log2_int(local_devices[j]->available_nodes);
                 memcpy(local_devices[i]->nodes[node_type],
                        local_devices[j]->nodes[node_type], drmGetMaxNodeName());
                 drmFreeDevice(&local_devices[j]);
--

2.26.2

Quoted 4 lines

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

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

#2 lore raw
In reply to Paul Gofman
Quoted 20 lines · On Mon, 26 Oct 2020 16:11:20 +0300 Paul Gofman <[email protected]> wrote:

log() is affected by FP control word and can provide inaccurate result.

Fixes Killer Instinct under Wine not being able to find AMD vulkan device.

---
With the rounding mode the application sets (unsigned int)log2(4) is 1. The log2_int() implemetation is copied from radeon/radeon_surface.c.

 xf86drm.c | 18 +++++++++++++++++-
 1 file changed, 17 insertions(+), 1 deletion(-)
xf86drm.c +0 -0
index 50a6f092..dbb7c14b 100644
@@ -124,6 +124,22 @@ static drmServerInfoPtr drm_server_info;
 static bool drmNodeIsDRM(int maj, int min);
 static char *drmGetMinorNameForFD(int fd, int type);

xf86drm.c static drmServerInfoPtr drm_server_info; · line 135

Quoted 10 lines
xf86drm.c +9 -0
 
+static unsigned log2_int(unsigned x)
+{
+    unsigned l;
+
+    if (x < 2) {
+        return 0;
+    }
+    for (l = 2; ; l++) {
+        if ((unsigned)(1 << l) > x) {

Hi,

wouldn't this loop fail to end when x >= 0x80000000?

Sure, such value probably cannot occur where this is currently used, but it seems like a landmine for the next developer to step on.

Thanks,
pq

Quoted 11 lines
xf86drm.c +7 -0
+            return l - 1;
+        }
+    }
+    return 0;
+}
+
+
 drm_public void drmSetServerInfo(drmServerInfoPtr info)
 {
     drm_server_info = info;

xf86drm.c static void drmFoldDuplicatedDevices(drmDevicePtr local_devices[], int count) · line 4023

Quoted 8 lines
xf86drm.c +1 -1
@@ -4001,7 +4017,7 @@ static void drmFoldDuplicatedDevices(drmDevicePtr local_devices[], int count)
         for (j = i + 1; j < count; j++) {
             if (drmDevicesEqual(local_devices[i], local_devices[j])) {
                 local_devices[i]->available_nodes |= local_devices[j]->available_nodes;
-                node_type = log2(local_devices[j]->available_nodes);
+                node_type = log2_int(local_devices[j]->available_nodes);
                 memcpy(local_devices[i]->nodes[node_type],
                        local_devices[j]->nodes[node_type], drmGetMaxNodeName());
                 drmFreeDevice(&local_devices[j]);

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

#3 lore raw
Quoted 13 lines · Pekka Paalanen

xf86drm.c static drmServerInfoPtr drm_server_info; · line 135

Quoted 11 lines
xf86drm.c +9 -0
+static unsigned log2_int(unsigned x)
+{
+    unsigned l;
+
+    if (x < 2) {
+        return 0;
+    }
+    for (l = 2; ; l++) {
+        if ((unsigned)(1 << l) > x) {

Hi,

Quoted 5 lines

wouldn't this loop fail to end when x >= 0x80000000?

Sure, such value probably cannot occur where this is currently used, but it seems like a landmine for the next developer to step on.

Indeed, thanks. I've sent the patches for consideration which avoid function duplication and potentially infinite loop.
_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel

#4 lore raw
Quoted 14 lines · Paul Gofman
Quoted 13 lines · Pekka Paalanen

xf86drm.c static drmServerInfoPtr drm_server_info; · line 135

Quoted 11 lines
xf86drm.c +9 -0
+static unsigned log2_int(unsigned x)
+{
+    unsigned l;
+
+    if (x < 2) {
+        return 0;
+    }
+    for (l = 2; ; l++) {
+        if ((unsigned)(1 << l) > x) {

Hi,

Quoted 7 lines
Quoted 5 lines

wouldn't this loop fail to end when x >= 0x80000000?

Sure, such value probably cannot occur where this is currently used, but it seems like a landmine for the next developer to step on.

Indeed, thanks. I've sent the patches for consideration which avoid function duplication and potentially infinite loop.

libdrm uses GitLab merge requests now: https://gitlab.freedesktop.org/mesa/drm/-/merge_requests

Signature · 6 lines
Earthling Michel Dänzer               |               https://redhat.com
Libre software enthusiast             |             Mesa and X developer
_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel
#5 lore raw
Quoted 16 lines · Michel Dänzer
Quoted 15 lines · Paul Gofman
Quoted 14 lines · Pekka Paalanen

xf86drm.c static drmServerInfoPtr drm_server_info; · line 135

Quoted 10 lines

  +static unsigned log2_int(unsigned x)

xf86drm.c +8 -0
+{
+    unsigned l;
+
+    if (x < 2) {
+        return 0;
+    }
+    for (l = 2; ; l++) {
+        if ((unsigned)(1 << l) > x) {

Hi,

wouldn't this loop fail to end when x >= 0x80000000?

Quoted 10 lines
Quoted 5 lines
Quoted 3 lines

Sure, such value probably cannot occur where this is currently used, but it seems like a landmine for the next developer to step on.

Indeed, thanks. I've sent the patches for consideration which avoid function duplication and potentially infinite loop.

libdrm uses GitLab merge requests now:
https://gitlab.freedesktop.org/mesa/drm/-/merge_requests

I see, thanks. I was following the instructions in CONTRIBUTING.rst.

Do you think I should proceed with merge request for these patches or those already sent could be considered this way?

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