Skip to contentSkip to conversation

drm: Copy drm_wait_vblank request and copy_to_user before return.

PATCH In review Mark Yacoub 3 participants 7 messages Aug 11, 2021 – Aug 17, 2021

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 drm: Copy drm_wait_vblank request and copy_to_user before return.

From: Mark Yacoub <[email protected]>

[Why]
Userspace should get back a copy of the request that's been modified even when drm_wait_vblank_ioctl returns a failure.

Rationale:
drm_wait_vblank_ioctl modifies the request and expects the user to read back. When the type is RELATIVE, it modifies it to ABSOLUTE and updates the sequence to become current_vblank_count + sequence (which was relative), not it becomes absolute.
drmWaitVBlank (in libdrm), expects this to be the case as it modifies the request to be Absolute as it expects the sequence to would have been updated.

The change is in compat_drm_wait_vblank, which is called by drm_compat_ioctl. This change of copying the data back regardless of the return number makes it en par with drm_ioctl, which always copies the data before returning.

[How]
Copy the drm_wait_vblank request.
Return from the function after everything has been copied to user.

Fixes: IGT:kms_flip::modeset-vs-vblank-race-interruptible Tested on ChromeOS Trogdor(msm)

---
 drivers/gpu/drm/drm_ioc32.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)
drivers/gpu/drm/drm_ioc32.c +5 -3
index d29907955ff79..275b860df8fbe 100644
@@ -855,17 +855,19 @@ static int compat_drm_wait_vblank(struct file *file, unsigned int cmd,
 	req.request.sequence = req32.request.sequence;
 	req.request.signal = req32.request.signal;
 	err = drm_ioctl_kernel(file, drm_wait_vblank_ioctl, &req, DRM_UNLOCKED);
-	if (err)
-		return err;
 
 	req32.reply.type = req.reply.type;
 	req32.reply.sequence = req.reply.sequence;
 	req32.reply.tval_sec = req.reply.tval_sec;
 	req32.reply.tval_usec = req.reply.tval_usec;
+	/* drm_wait_vblank_ioctl modifies Request, update their values here as well. */
+	req32.request.type = req.request.type;
+	req32.request.sequence = req.request.sequence;
+	req32.request.signal = req.request.signal;
 	if (copy_to_user(argp, &req32, sizeof(req32)))
 		return -EFAULT;
 
-	return 0;
+	return err;
 }
 
 #if defined(CONFIG_X86)
Signature · 1 line
2.33.0.rc1.237.g0d66db33f3-goog
#1 lore raw
Quoted 41 lines · Mark Yacoub

From: Mark Yacoub <[email protected]>

[Why]
Userspace should get back a copy of the request that's been modified even when drm_wait_vblank_ioctl returns a failure.

Rationale:
drm_wait_vblank_ioctl modifies the request and expects the user to read back. When the type is RELATIVE, it modifies it to ABSOLUTE and updates the sequence to become current_vblank_count + sequence (which was relative), not it becomes absolute.
drmWaitVBlank (in libdrm), expects this to be the case as it modifies the request to be Absolute as it expects the sequence to would have been updated.

The change is in compat_drm_wait_vblank, which is called by drm_compat_ioctl. This change of copying the data back regardless of the return number makes it en par with drm_ioctl, which always copies the data before returning.

[How]
Copy the drm_wait_vblank request.
Return from the function after everything has been copied to user.

Fixes: IGT:kms_flip::modeset-vs-vblank-race-interruptible Tested on ChromeOS Trogdor(msm)

---
 drivers/gpu/drm/drm_ioc32.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)
drivers/gpu/drm/drm_ioc32.c +0 -1
index d29907955ff79..275b860df8fbe 100644
@@ -855,17 +855,19 @@ static int compat_drm_wait_vblank(struct file *file, unsigned int cmd,
 	req.request.sequence = req32.request.sequence;
 	req.request.signal = req32.request.signal;
 	err = drm_ioctl_kernel(file, drm_wait_vblank_ioctl, &req, DRM_UNLOCKED);
-	if (err)

drivers/gpu/drm/drm_ioc32.c static int compat_drm_wait_vblank(struct file *file, unsigned int cmd, · line 866

Quoted 10 lines
drivers/gpu/drm/drm_ioc32.c +4 -1
-		return err;
 
 	req32.reply.type = req.reply.type;
 	req32.reply.sequence = req.reply.sequence;
 	req32.reply.tval_sec = req.reply.tval_sec;
 	req32.reply.tval_usec = req.reply.tval_usec;
+	/* drm_wait_vblank_ioctl modifies Request, update their values here as well. */
+	req32.request.type = req.request.type;
+	req32.request.sequence = req.request.sequence;
+	req32.request.signal = req.request.signal;

The added assignments are superfluous, since req32.reply and req32.request are members of the same union.

drivers/gpu/drm/drm_ioc32.c static int compat_drm_wait_vblank(struct file *file, unsigned int cmd, · line 871

Quoted 6 lines
drivers/gpu/drm/drm_ioc32.c +1 -1
 	if (copy_to_user(argp, &req32, sizeof(req32)))
 		return -EFAULT;
 
-	return 0;
+	return err;
 }

The other changes look correct.

Signature · 2 lines
Earthling Michel Dänzer               |               https://redhat.com
Libre software enthusiast             |             Mesa and X developer
#2 lore raw
Quoted 45 lines · Michel Dänzer
Quoted 43 lines · Mark Yacoub

From: Mark Yacoub <[email protected]>

[Why]
Userspace should get back a copy of the request that's been modified even when drm_wait_vblank_ioctl returns a failure.

Rationale:
drm_wait_vblank_ioctl modifies the request and expects the user to read back. When the type is RELATIVE, it modifies it to ABSOLUTE and updates the sequence to become current_vblank_count + sequence (which was relative), not it becomes absolute.
drmWaitVBlank (in libdrm), expects this to be the case as it modifies the request to be Absolute as it expects the sequence to would have been updated.

The change is in compat_drm_wait_vblank, which is called by drm_compat_ioctl. This change of copying the data back regardless of the return number makes it en par with drm_ioctl, which always copies the data before returning.

[How]
Copy the drm_wait_vblank request.
Return from the function after everything has been copied to user.

Fixes: IGT:kms_flip::modeset-vs-vblank-race-interruptible Tested on ChromeOS Trogdor(msm)

---
 drivers/gpu/drm/drm_ioc32.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)
drivers/gpu/drm/drm_ioc32.c +0 -2
index d29907955ff79..275b860df8fbe 100644
@@ -855,17 +855,19 @@ static int compat_drm_wait_vblank(struct file *file, unsigned int cmd,
      req.request.sequence = req32.request.sequence;
      req.request.signal = req32.request.signal;
      err = drm_ioctl_kernel(file, drm_wait_vblank_ioctl, &req, DRM_UNLOCKED);
-     if (err)
-             return err;
 
Quoted 10 lines

drivers/gpu/drm/drm_ioc32.c static int compat_drm_wait_vblank(struct file *file, unsigned int cmd, · line 866

Quoted 8 lines
drivers/gpu/drm/drm_ioc32.c +4 -0
      req32.reply.type = req.reply.type;
      req32.reply.sequence = req.reply.sequence;
      req32.reply.tval_sec = req.reply.tval_sec;
      req32.reply.tval_usec = req.reply.tval_usec;
+     /* drm_wait_vblank_ioctl modifies Request, update their values here as well. */
+     req32.request.type = req.request.type;
+     req32.request.sequence = req.request.sequence;
+     req32.request.signal = req.request.signal;

The added assignments are superfluous, since req32.reply and req32.request are members of the same union.

Noted.

Quoted 10 lines

drivers/gpu/drm/drm_ioc32.c static int compat_drm_wait_vblank(struct file *file, unsigned int cmd, · line 871

Quoted 8 lines
drivers/gpu/drm/drm_ioc32.c +1 -1
      if (copy_to_user(argp, &req32, sizeof(req32)))
              return -EFAULT;
 
-     return 0;
+     return err;
 }

The other changes look correct.

Thanks! updated v2:
https://patchwork.freedesktop.org/patch/449768/?series=93605&rev=2

Quoted 5 lines

--
Earthling Michel Dänzer | https://redhat.com
Libre software enthusiast | Mesa and X developer

#3 lore raw

PATCH v2 1/1 drm: Copy drm_wait_vblank to user before returning

In reply to Mark Yacoub

From: Mark Yacoub <[email protected]>

[Why]
Userspace should get back a copy of drm_wait_vblank that's been modified even when drm_wait_vblank_ioctl returns a failure.

Rationale:
drm_wait_vblank_ioctl modifies the request and expects the user to read it back. When the type is RELATIVE, it modifies it to ABSOLUTE and updates the sequence to become current_vblank_count + sequence (which was RELATIVE), but now it became ABSOLUTE.
drmWaitVBlank (in libdrm) expects this to be the case as it modifies the request to be Absolute so it expects the sequence to would have been updated.

The change is in compat_drm_wait_vblank, which is called by drm_compat_ioctl. This change of copying the data back regardless of the return number makes it en par with drm_ioctl, which always copies the data before returning.

[How]
Return from the function after everything has been copied to user.

Fixes: IGT:kms_flip::modeset-vs-vblank-race-interruptible Tested on ChromeOS Trogdor(msm)

Change-Id I98da279a5f1329c66a9d1e06b88d40b247b51313
---
 drivers/gpu/drm/drm_ioc32.c | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)
drivers/gpu/drm/drm_ioc32.c +1 -3
index d29907955ff79..5d82891c32223 100644
@@ -855,8 +855,6 @@ static int compat_drm_wait_vblank(struct file *file, unsigned int cmd,
 	req.request.sequence = req32.request.sequence;
 	req.request.signal = req32.request.signal;
 	err = drm_ioctl_kernel(file, drm_wait_vblank_ioctl, &req, DRM_UNLOCKED);
-	if (err)
-		return err;
 
 	req32.reply.type = req.reply.type;
 	req32.reply.sequence = req.reply.sequence;
@@ -865,7 +863,7 @@ static int compat_drm_wait_vblank(struct file *file, unsigned int cmd,
 	if (copy_to_user(argp, &req32, sizeof(req32)))
 		return -EFAULT;
 
-	return 0;
+	return err;
 }
 
 #if defined(CONFIG_X86)
Signature · 1 line
2.33.0.rc1.237.g0d66db33f3-goog
Reviewed-by on v2 1/1
#4 lore raw

Re: [PATCH v2] drm: Copy drm_wait_vblank to user before returning

Quoted 20 lines · Mark Yacoub

From: Mark Yacoub <[email protected]>

[Why]
Userspace should get back a copy of drm_wait_vblank that's been modified even when drm_wait_vblank_ioctl returns a failure.

Rationale:
drm_wait_vblank_ioctl modifies the request and expects the user to read it back. When the type is RELATIVE, it modifies it to ABSOLUTE and updates the sequence to become current_vblank_count + sequence (which was RELATIVE), but now it became ABSOLUTE.
drmWaitVBlank (in libdrm) expects this to be the case as it modifies the request to be Absolute so it expects the sequence to would have been updated.

The change is in compat_drm_wait_vblank, which is called by drm_compat_ioctl. This change of copying the data back regardless of the return number makes it en par with drm_ioctl, which always copies the data before returning.

Quoted 8 lines

[How]
Return from the function after everything has been copied to user.

Fixes: IGT:kms_flip::modeset-vs-vblank-race-interruptible Tested on ChromeOS Trogdor(msm)

Change-Id I98da279a5f1329c66a9d1e06b88d40b247b51313

With the Gerrit Change-Id removed,

Signature · 2 lines
Earthling Michel Dänzer               |               https://redhat.com
Libre software enthusiast             |             Mesa and X developer
#5 lore raw

Re: [PATCH v2] drm: Copy drm_wait_vblank to user before returning

Thanks for your review Michel!
@MAINTAINER, could you please strip the Change-Id when applying. Thanks!

Quoted 39 lines · Michel Dänzer
Quoted 20 lines · Mark Yacoub

From: Mark Yacoub <[email protected]>

[Why]
Userspace should get back a copy of drm_wait_vblank that's been modified even when drm_wait_vblank_ioctl returns a failure.

Rationale:
drm_wait_vblank_ioctl modifies the request and expects the user to read it back. When the type is RELATIVE, it modifies it to ABSOLUTE and updates the sequence to become current_vblank_count + sequence (which was RELATIVE), but now it became ABSOLUTE.
drmWaitVBlank (in libdrm) expects this to be the case as it modifies the request to be Absolute so it expects the sequence to would have been updated.

The change is in compat_drm_wait_vblank, which is called by drm_compat_ioctl. This change of copying the data back regardless of the return number makes it en par with drm_ioctl, which always copies the data before returning.

Quoted 8 lines

[How]
Return from the function after everything has been copied to user.

Fixes: IGT:kms_flip::modeset-vs-vblank-race-interruptible Tested on ChromeOS Trogdor(msm)

Change-Id I98da279a5f1329c66a9d1e06b88d40b247b51313

With the Gerrit Change-Id removed,

--
Earthling Michel Dänzer | https://redhat.com
Libre software enthusiast | Mesa and X developer

Applied
#6 lore raw

Re: [PATCH v2] drm: Copy drm_wait_vblank to user before returning

Quoted 4 lines · Mark Yacoub

Thanks for your review Michel!
@MAINTAINER, could you please strip the Change-Id when applying. Thanks!

Applied to drm-misc-fixes with the Change-Id removed.

Thank you for your patch!

Sean

Quoted 41 lines · Michel Dänzer
Quoted 39 lines · Mark Yacoub
Quoted 20 lines

From: Mark Yacoub <[email protected]>

[Why]
Userspace should get back a copy of drm_wait_vblank that's been modified even when drm_wait_vblank_ioctl returns a failure.

Rationale:
drm_wait_vblank_ioctl modifies the request and expects the user to read it back. When the type is RELATIVE, it modifies it to ABSOLUTE and updates the sequence to become current_vblank_count + sequence (which was RELATIVE), but now it became ABSOLUTE.
drmWaitVBlank (in libdrm) expects this to be the case as it modifies the request to be Absolute so it expects the sequence to would have been updated.

The change is in compat_drm_wait_vblank, which is called by drm_compat_ioctl. This change of copying the data back regardless of the return number makes it en par with drm_ioctl, which always copies the data before returning.

Quoted 8 lines

[How]
Return from the function after everything has been copied to user.

Fixes: IGT:kms_flip::modeset-vs-vblank-race-interruptible Tested on ChromeOS Trogdor(msm)

Change-Id I98da279a5f1329c66a9d1e06b88d40b247b51313

With the Gerrit Change-Id removed,

--
Earthling Michel Dänzer | https://redhat.com
Libre software enthusiast | Mesa and X developer