Skip to contentSkip to conversation

drm/virtio: fix virtio-gpu resource id creation race

PATCH Superseded John Bates 5 participants 5 messages Feb 20, 2020

Subsystems (3)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#2 lore raw
In reply to John Bates
Quoted 16 lines · John Bates
Quoted 12 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.

---

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

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

Quoted 11 lines
drivers/gpu/drm/virtio/virtgpu_object.c +1 -2
index 017a9e0fc3bb..b11c1fce1770 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;

nit: ATOMIC_INIT(0)?

Quoted 7 lines
Quoted 7 lines
drivers/gpu/drm/virtio/virtgpu_object.c +1 -1
+               int handle = atomic_inc_return(&seqno);
                *resid = handle + 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

#1 lore raw
In reply to John Bates
Quoted 28 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.

---

 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..b11c1fce1770 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;
+               int handle = atomic_inc_return(&seqno);
                *resid = handle + 1;
        } else {
                int handle = ida_alloc(&vgdev->resource_ida, GFP_KERNEL);
Quoted 4 lines

--
2.25.0.265.gbab2e86ba0-goog

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

John Bates

author
#0 lore raw

PATCH 1/1 drm/virtio: fix virtio-gpu 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.

---

 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..b11c1fce1770 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;
+		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
#3 lore raw

Hi John,

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.

Adding a fixes tag like below makes it easier to track. Especially for Greg and team who are working on stable kernels.

Fixes 3e93bc2a58aa ("drm/virtio: make resource id workaround runtime

switchable.")

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

reviewer
#4 lore raw
Quoted 10 lines · Emil Velikov

Hi John,

Quoted 6 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.

Quoted 6 lines
Quoted 1 line

Adding a fixes tag like below makes it easier to track. Especially for Greg and team who are working on stable kernels.

Fixes 3e93bc2a58aa ("drm/virtio: make resource id workaround runtime

switchable.")

FWIW, the fixes tag should refer to this commit instead

commit 16065fcdd19ddb9e093192914ac863884f308766

Author: Gerd Hoffmann <[email protected]>
Date:   Fri Feb 8 15:04:09 2019 +0100

drm/virtio: do NOT reuse resource ids

Quoted 7 lines

HTH
Emil
_______________________________________________
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