Skip to contentSkip to conversation

drm/virtio: fix resource id creation race

PATCH Applied John Bates 4 participants 4 messages Feb 21, 2020 – Feb 24, 2020

Subsystems (3)
lore
Share & tools
lore

John Bates

author
#0 lore raw

PATCH v2 1/1 drm/virtio: fix resource id creation race

The previous code was not thread safe and caused undefined behavior from spurious duplicate resource IDs. In this patch, an atomic_t is used instead. We no longer see any duplicate IDs in tests with this change.

Fixes 16065fcdd19d ("drm/virtio: do NOT reuse resource ids")
---
 drivers/gpu/drm/virtio/virtgpu_object.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/gpu/drm/virtio/virtgpu_object.c +2 -2
index 017a9e0fc3bb..890121a45625 100644
@@ -42,8 +42,8 @@ static int virtio_gpu_resource_id_get(struct virtio_gpu_device *vgdev,
 		 * "f91a9dd35715 Fix unlinking resources from hash
 		 * table." (Feb 2019) fixes the bug.
 		 */
-		static int handle;
-		handle++;
+		static atomic_t seqno = ATOMIC_INIT(0);
+		int handle = atomic_inc_return(&seqno);
 		*resid = handle + 1;
 	} else {
 		int handle = ida_alloc(&vgdev->resource_ida, GFP_KERNEL);
Signature · 6 lines
2.25.0.265.gbab2e86ba0-goog

_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel
reviewer Reviewed-by on v2 1/1
#1 lore raw
Quoted 8 lines · John Bates

The previous code was not thread safe and caused undefined behavior from spurious duplicate resource IDs. In this patch, an atomic_t is used instead. We no longer see any duplicate IDs in tests with this change.

Fixes 16065fcdd19d ("drm/virtio: do NOT reuse resource ids")
Quoted 9 lines
---
 drivers/gpu/drm/virtio/virtgpu_object.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/gpu/drm/virtio/virtgpu_object.c +0 -0
index 017a9e0fc3bb..890121a45625 100644

drivers/gpu/drm/virtio/virtgpu_object.c static int virtio_gpu_resource_id_get(struct virtio_gpu_device *vgdev, · line 47

Quoted 8 lines
drivers/gpu/drm/virtio/virtgpu_object.c +2 -2
@@ -42,8 +42,8 @@ static int virtio_gpu_resource_id_get(struct virtio_gpu_device *vgdev,
                 * "f91a9dd35715 Fix unlinking resources from hash
                 * table." (Feb 2019) fixes the bug.
                 */
-               static int handle;
-               handle++;
+               static atomic_t seqno = ATOMIC_INIT(0);
+               int handle = atomic_inc_return(&seqno);
                *resid = handle + 1;

resid 1 is (was) discriminated :D

Quoted 9 lines
drivers/gpu/drm/virtio/virtgpu_object.c +0 -1
        } else {
                int handle = ida_alloc(&vgdev->resource_ida, GFP_KERNEL);
--

2.25.0.265.gbab2e86ba0-goog

_______________________________________________
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
Quoted 29 lines · Chia-I Wu
Quoted 8 lines · John Bates

The previous code was not thread safe and caused undefined behavior from spurious duplicate resource IDs. In this patch, an atomic_t is used instead. We no longer see any duplicate IDs in tests with this change.

Fixes 16065fcdd19d ("drm/virtio: do NOT reuse resource ids")

Review

Quoted 5 lines
---
 drivers/gpu/drm/virtio/virtgpu_object.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
(quoted diff) +0 -0

b/drivers/gpu/drm/virtio/virtgpu_object.c

drivers/gpu/drm/virtio/virtgpu_object.c static int virtio_gpu_resource_id_get(struct

Quoted 4 lines

index 017a9e0fc3bb..890121a45625 100644

drivers/gpu/drm/virtio/virtgpu_object.c +0 -0
@@ -42,8 +42,8 @@ static int virtio_gpu_resource_id_get(struct

virtio_gpu_device *vgdev,

drivers/gpu/drm/virtio/virtgpu_object.c static int virtio_gpu_resource_id_get(struct virtio_gpu_device *vgdev, · line 47

Quoted 8 lines
drivers/gpu/drm/virtio/virtgpu_object.c +2 -2
                 * "f91a9dd35715 Fix unlinking resources from hash
                 * table." (Feb 2019) fixes the bug.
                 */
-               static int handle;
-               handle++;
+               static atomic_t seqno = ATOMIC_INIT(0);
+               int handle = atomic_inc_return(&seqno);
                *resid = handle + 1;
Quoted 2 lines

resid 1 is (was) discriminated :D

Yup, noticed that too :)

Quoted 11 lines
Quoted 10 lines
drivers/gpu/drm/virtio/virtgpu_object.c +0 -1
        } else {
                int handle = ida_alloc(&vgdev->resource_ida, GFP_KERNEL);
--

2.25.0.265.gbab2e86ba0-goog

_______________________________________________
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

maintainer Applied
#3 lore raw
In reply to John Bates
Quoted 7 lines · John Bates

The previous code was not thread safe and caused undefined behavior from spurious duplicate resource IDs. In this patch, an atomic_t is used instead. We no longer see any duplicate IDs in tests with this change.

Fixes 16065fcdd19d ("drm/virtio: do NOT reuse resource ids")

Pushed to drm-misc-fixes.

thanks,
Gerd

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