Skip to contentSkip to conversation

drm/crc: Handle opening and closing crc better

PATCH In review Maarten Lankhorst 4 participants 6 messages Jun 21, 2017 – Jul 10, 2017

Subsystems (2)
lore
Share & tools
lore

Maarten Lankhorst

author maintainer
#0 lore raw

PATCH 1/1 drm/crc: Handle opening and closing crc better

When I was doing a grep . -r /sys/kernel/debug/dri/0 I noticed a WARN appearing when I aborted the grep with ^C.

After investigating I've also noticed that the error handling was lacking and there are race conditions involving multiple calls to open/close simultaneously.

Fix this by setting the opened flag first and using crc->entries to decide when crc can be collected.

Also call unset crc source before cleaning up, this way there is no race with a future open().

---
 drivers/gpu/drm/drm_debugfs_crc.c | 46 ++++++++++++++++++++++++++-------------
 1 file changed, 31 insertions(+), 15 deletions(-)
drivers/gpu/drm/drm_debugfs_crc.c +31 -15
index 1722d8f21449..d0ea4627a093 100644
@@ -136,21 +136,38 @@ static int crtc_crc_data_count(struct drm_crtc_crc *crc)
 	return CIRC_CNT(crc->head, crc->tail, DRM_CRC_ENTRIES_NR);
 }
 
+static void crtc_crc_cleanup(struct drm_crtc_crc *crc)
+{
+	kfree(crc->entries);
+	crc->entries = NULL;
+	crc->head = 0;
+	crc->tail = 0;
+	crc->values_cnt = 0;
+	crc->opened = false;
+}
+
 static int crtc_crc_open(struct inode *inode, struct file *filep)
 {
 	struct drm_crtc *crtc = inode->i_private;
 	struct drm_crtc_crc *crc = &crtc->crc;
 	struct drm_crtc_crc_entry *entries = NULL;
 	size_t values_cnt;
-	int ret;
+	int ret = 0;
 
-	if (crc->opened)
-		return -EBUSY;
+	spin_lock_irq(&crc->lock);
+	if (!crc->opened)
+		crc->opened = true;
+	else
+		ret = -EBUSY;
+	spin_unlock_irq(&crc->lock);
 
-	ret = crtc->funcs->set_crc_source(crtc, crc->source, &values_cnt);
 	if (ret)
 		return ret;
 
+	ret = crtc->funcs->set_crc_source(crtc, crc->source, &values_cnt);
+	if (ret)
+		goto err;
+
 	if (WARN_ON(values_cnt > DRM_MAX_CRC_NR)) {
 		ret = -EINVAL;
 		goto err_disable;
@@ -170,7 +187,6 @@ static int crtc_crc_open(struct inode *inode, struct file *filep)
 	spin_lock_irq(&crc->lock);
 	crc->entries = entries;
 	crc->values_cnt = values_cnt;
-	crc->opened = true;
 
 	/*
 	 * Only return once we got a first frame, so userspace doesn't have to
@@ -182,12 +198,17 @@ static int crtc_crc_open(struct inode *inode, struct file *filep)
 						crc->lock);
 	spin_unlock_irq(&crc->lock);
 
-	WARN_ON(ret);
+	if (ret)
+		goto err_disable;
 
 	return 0;
 
 err_disable:
 	crtc->funcs->set_crc_source(crtc, NULL, &values_cnt);
+err:
+	spin_lock_irq(&crc->lock);
+	crtc_crc_cleanup(crc);
+	spin_unlock_irq(&crc->lock);
 	return ret;
 }
 
@@ -197,17 +218,12 @@ static int crtc_crc_release(struct inode *inode, struct file *filep)
 	struct drm_crtc_crc *crc = &crtc->crc;
 	size_t values_cnt;
 
+	crtc->funcs->set_crc_source(crtc, NULL, &values_cnt);
+
 	spin_lock_irq(&crc->lock);
-	kfree(crc->entries);
-	crc->entries = NULL;
-	crc->head = 0;
-	crc->tail = 0;
-	crc->values_cnt = 0;
-	crc->opened = false;
+	crtc_crc_cleanup(crc);
 	spin_unlock_irq(&crc->lock);
 
-	crtc->funcs->set_crc_source(crtc, NULL, &values_cnt);
-
 	return 0;
 }
 
@@ -334,7 +350,7 @@ int drm_crtc_add_crc_entry(struct drm_crtc *crtc, bool has_frame,
 	spin_lock(&crc->lock);
 
 	/* Caller may not have noticed yet that userspace has stopped reading */
-	if (!crc->opened) {
+	if (!crc->entries) {
 		spin_unlock(&crc->lock);
 		return -EINVAL;
 	}
Signature · 6 lines
2.11.0

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

✓ Fi.CI.BAT: success for drm/crc: Handle opening and closing crc better

== Series Details ==

== Series Details ==

Series: drm/crc: Handle opening and closing crc better
URL : https://patchwork.freedesktop.org/series/26140/
State : success

== Summary ==

Series 26140v1 drm/crc: Handle opening and closing crc better
https://patchwork.freedesktop.org/api/1.0/series/26140/revisions/1/mbox/

Test gem_exec_flush:

        Subgroup basic-batch-kernel-default-uc:
                pass       -> FAIL       (fi-snb-2600) fdo#100007
Test prime_busy:
        Subgroup basic-wait-after-default:
                dmesg-warn -> PASS       (fi-skl-6700hq) fdo#101515 +3

fdo#100007 https://bugs.freedesktop.org/show_bug.cgi?id=100007
fdo#101515 https://bugs.freedesktop.org/show_bug.cgi?id=101515

fi-bdw-5557u     total:278  pass:267  dwarn:0   dfail:0   fail:0   skip:11  time:450s
fi-bdw-gvtdvm    total:278  pass:256  dwarn:8   dfail:0   fail:0   skip:14  time:427s
fi-bsw-n3050     total:278  pass:241  dwarn:1   dfail:0   fail:0   skip:36  time:530s
fi-bxt-j4205     total:278  pass:259  dwarn:0   dfail:0   fail:0   skip:19  time:506s
fi-byt-j1900     total:278  pass:252  dwarn:2   dfail:0   fail:0   skip:24  time:492s
fi-byt-n2820     total:278  pass:248  dwarn:2   dfail:0   fail:0   skip:28  time:480s
fi-glk-2a        total:278  pass:259  dwarn:0   dfail:0   fail:0   skip:19  time:583s
fi-hsw-4770      total:278  pass:262  dwarn:0   dfail:0   fail:0   skip:16  time:440s
fi-hsw-4770r     total:278  pass:262  dwarn:0   dfail:0   fail:0   skip:16  time:409s
fi-ilk-650       total:278  pass:228  dwarn:0   dfail:0   fail:0   skip:50  time:416s
fi-ivb-3520m     total:278  pass:260  dwarn:0   dfail:0   fail:0   skip:18  time:494s
fi-ivb-3770      total:278  pass:260  dwarn:0   dfail:0   fail:0   skip:18  time:467s
fi-kbl-7500u     total:278  pass:260  dwarn:0   dfail:0   fail:0   skip:18  time:466s
fi-kbl-7560u     total:278  pass:267  dwarn:1   dfail:0   fail:0   skip:10  time:577s
fi-kbl-r         total:278  pass:259  dwarn:1   dfail:0   fail:0   skip:18  time:579s
fi-skl-6260u     total:278  pass:268  dwarn:0   dfail:0   fail:0   skip:10  time:459s
fi-skl-6700hq    total:278  pass:220  dwarn:3   dfail:0   fail:30  skip:24  time:338s
fi-skl-6700k     total:278  pass:256  dwarn:4   dfail:0   fail:0   skip:18  time:472s
fi-skl-6770hq    total:278  pass:268  dwarn:0   dfail:0   fail:0   skip:10  time:480s
fi-skl-gvtdvm    total:278  pass:265  dwarn:0   dfail:0   fail:0   skip:13  time:437s
fi-snb-2520m     total:278  pass:250  dwarn:0   dfail:0   fail:0   skip:28  time:531s
fi-snb-2600      total:278  pass:248  dwarn:0   dfail:0   fail:1   skip:29  time:407s

186f6503dc0750d739db5d9301dc9a507eacc6ef drm-tip: 2017y-06m-21d-10h-08m-42s UTC integration manifest 4cb07ba drm/crc: Handle opening and closing crc better

== Logs ==

For more details see: https://intel-gfx-ci.01.org/CI/Patchwork_5012/
_______________________________________________
Intel-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/intel-gfx

Reviewed-by on v1 1/1
#2 lore raw
In reply to Maarten Lankhorst

Looks good to me:

I guess you have tested this with IGT? In any case, I think it would be good to mention how a patch has been tested in the changelog. That can be very useful to others if things go wrong at some point.

Thanks,

Tomeu

Quoted 125 lines · Maarten Lankhorst

When I was doing a grep . -r /sys/kernel/debug/dri/0 I noticed a WARN appearing when I aborted the grep with ^C.

After investigating I've also noticed that the error handling was lacking and there are race conditions involving multiple calls to open/close simultaneously.

Fix this by setting the opened flag first and using crc->entries to decide when crc can be collected.

Also call unset crc source before cleaning up, this way there is no race with a future open().

---
 drivers/gpu/drm/drm_debugfs_crc.c | 46 ++++++++++++++++++++++++++-------------
 1 file changed, 31 insertions(+), 15 deletions(-)
drivers/gpu/drm/drm_debugfs_crc.c +31 -16
index 1722d8f21449..d0ea4627a093 100644
@@ -136,21 +136,38 @@ static int crtc_crc_data_count(struct drm_crtc_crc *crc)
        return CIRC_CNT(crc->head, crc->tail, DRM_CRC_ENTRIES_NR);
 }
 
+static void crtc_crc_cleanup(struct drm_crtc_crc *crc)
+{
+       kfree(crc->entries);
+       crc->entries = NULL;
+       crc->head = 0;
+       crc->tail = 0;
+       crc->values_cnt = 0;
+       crc->opened = false;
+}
+
 static int crtc_crc_open(struct inode *inode, struct file *filep)
 {
        struct drm_crtc *crtc = inode->i_private;
        struct drm_crtc_crc *crc = &crtc->crc;
        struct drm_crtc_crc_entry *entries = NULL;
        size_t values_cnt;
-       int ret;
+       int ret = 0;
 
-       if (crc->opened)
-               return -EBUSY;
+       spin_lock_irq(&crc->lock);
+       if (!crc->opened)
+               crc->opened = true;
+       else
+               ret = -EBUSY;
+       spin_unlock_irq(&crc->lock);
 
-       ret = crtc->funcs->set_crc_source(crtc, crc->source, &values_cnt);
        if (ret)
                return ret;
 
+       ret = crtc->funcs->set_crc_source(crtc, crc->source, &values_cnt);
+       if (ret)
+               goto err;
+
        if (WARN_ON(values_cnt > DRM_MAX_CRC_NR)) {
                ret = -EINVAL;
                goto err_disable;
@@ -170,7 +187,6 @@ static int crtc_crc_open(struct inode *inode, struct file *filep)
        spin_lock_irq(&crc->lock);
        crc->entries = entries;
        crc->values_cnt = values_cnt;
-       crc->opened = true;
 
        /*
         * Only return once we got a first frame, so userspace doesn't have to
@@ -182,12 +198,17 @@ static int crtc_crc_open(struct inode *inode, struct file *filep)
                                                crc->lock);
        spin_unlock_irq(&crc->lock);
 
-       WARN_ON(ret);
+       if (ret)
+               goto err_disable;
 
        return 0;
 
 err_disable:
        crtc->funcs->set_crc_source(crtc, NULL, &values_cnt);
+err:
+       spin_lock_irq(&crc->lock);
+       crtc_crc_cleanup(crc);
+       spin_unlock_irq(&crc->lock);
        return ret;
 }
 
@@ -197,17 +218,12 @@ static int crtc_crc_release(struct inode *inode, struct file *filep)
        struct drm_crtc_crc *crc = &crtc->crc;
        size_t values_cnt;
 
+       crtc->funcs->set_crc_source(crtc, NULL, &values_cnt);
+
        spin_lock_irq(&crc->lock);
-       kfree(crc->entries);
-       crc->entries = NULL;
-       crc->head = 0;
-       crc->tail = 0;
-       crc->values_cnt = 0;
-       crc->opened = false;
+       crtc_crc_cleanup(crc);
        spin_unlock_irq(&crc->lock);
 
-       crtc->funcs->set_crc_source(crtc, NULL, &values_cnt);
-
        return 0;
 }
 
@@ -334,7 +350,7 @@ int drm_crtc_add_crc_entry(struct drm_crtc *crtc, bool has_frame,
        spin_lock(&crc->lock);
 
        /* Caller may not have noticed yet that userspace has stopped reading */
-       if (!crc->opened) {
+       if (!crc->entries) {
                spin_unlock(&crc->lock);
                return -EINVAL;
        }
--

2.11.0

Quoted 4 lines

_______________________________________________
Intel-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/intel-gfx

_______________________________________________
Intel-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/intel-gfx

author maintainer
#3 lore raw

PATCH 1/1 drm/crc: Only open CRC on atomic drivers when the CRTC is active.

Op 06-07-17 om 13:09 schreef Tomeu Vizoso:

Quoted 7 lines

Looks good to me:

I guess you have tested this with IGT? In any case, I think it would be good to mention how a patch has been tested in the changelog. That can be very useful to others if things go wrong at some point.

Testcase: debugfs_test.read_all_entries

But I hit it by doing a recursive grep, which I guess is the same thing here. :)

One further improvement I wanted to do was reject opening the CRC with -EIO when the crtc is not active, that way the above test will not hang. Does the below patch also look good to you?

----8<-----
Commit e8fa5671183c ("drm: crc: Wait for a frame before returning from open()") adds a wait for CRC frame, but with the CRTC off this will never be generated. For atomic drivers we know if a CRTC is active through crtc_state->active, so when inactive reject the open with -EIO.

Fixes e8fa5671183c ("drm: crc: Wait for a frame before returning from open()")

Testcase: debugfs_test.read_all_entries
---

drivers/gpu/drm/drm_debugfs_crc.c +13 -0
index d0ea4627a093..f9e26dda56d6 100644
@@ -154,6 +154,19 @@ static int crtc_crc_open(struct inode *inode, struct file *filep)
 	size_t values_cnt;
 	int ret = 0;
 
+	if (drm_drv_uses_atomic_modeset(crtc->dev)) {
+		ret = drm_modeset_lock_interruptible(&crtc->mutex, NULL);
+		if (ret)
+			return ret;
+
+		if (!crtc->state->active)
+			ret = -EIO;
+		drm_modeset_unlock(&crtc->mutex);
+
+		if (ret)
+			return ret;
+	}
+
 	spin_lock_irq(&crc->lock);
 	if (!crc->opened)
 		crc->opened = true;

_______________________________________________
Intel-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/intel-gfx

Reviewed-by on v1 1/1
#4 lore raw

Re: [Intel-gfx] [PATCH] drm/crc: Only open CRC on atomic drivers when the CRTC is active.

Quoted 23 lines · Maarten Lankhorst

Op 06-07-17 om 13:09 schreef Tomeu Vizoso:

Quoted 7 lines

Looks good to me:

I guess you have tested this with IGT? In any case, I think it would be good to mention how a patch has been tested in the changelog. That can be very useful to others if things go wrong at some point.

Testcase: debugfs_test.read_all_entries

But I hit it by doing a recursive grep, which I guess is the same thing here. :)

One further improvement I wanted to do was reject opening the CRC with -EIO when the crtc is not active, that way the above test will not hang. Does the below patch also look good to you?

----8<-----
Commit e8fa5671183c ("drm: crc: Wait for a frame before returning from open()") adds a wait for CRC frame, but with the CRTC off this will never be generated. For atomic drivers we know if a CRTC is active through crtc_state->active, so when inactive reject the open with -EIO.

Quoted 3 lines
Fixes e8fa5671183c ("drm: crc: Wait for a frame before returning from open()")

Testcase: debugfs_test.read_all_entries

At least for the semantics I think this makes sense. Opening the CRC file when the crtc is off is undefined.

But pls get Tomeu's ack too.

Thanks, Daniel

Quoted 30 lines

---

drivers/gpu/drm/drm_debugfs_crc.c +13 -0
index d0ea4627a093..f9e26dda56d6 100644
@@ -154,6 +154,19 @@ static int crtc_crc_open(struct inode *inode, struct file *filep)
 	size_t values_cnt;
 	int ret = 0;
 
+	if (drm_drv_uses_atomic_modeset(crtc->dev)) {
+		ret = drm_modeset_lock_interruptible(&crtc->mutex, NULL);
+		if (ret)
+			return ret;
+
+		if (!crtc->state->active)
+			ret = -EIO;
+		drm_modeset_unlock(&crtc->mutex);
+
+		if (ret)
+			return ret;
+	}
+
 	spin_lock_irq(&crc->lock);
 	if (!crc->opened)
 		crc->opened = true;

_______________________________________________
Intel-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/intel-gfx

Signature · 7 lines
Daniel Vetter
Software Engineer, Intel Corporation
http://blog.ffwll.ch
_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel
author maintainer
#5 lore raw

Re: [PATCH] drm/crc: Only open CRC on atomic drivers when the CRTC is active.

Op 07-07-17 om 13:01 schreef Daniel Vetter:

Quoted 27 lines · Maarten Lankhorst
Quoted 23 lines

Op 06-07-17 om 13:09 schreef Tomeu Vizoso:

Quoted 7 lines

Looks good to me:

I guess you have tested this with IGT? In any case, I think it would be good to mention how a patch has been tested in the changelog. That can be very useful to others if things go wrong at some point.

Testcase: debugfs_test.read_all_entries

But I hit it by doing a recursive grep, which I guess is the same thing here. :)

One further improvement I wanted to do was reject opening the CRC with -EIO when the crtc is not active, that way the above test will not hang. Does the below patch also look good to you?

----8<-----
Commit e8fa5671183c ("drm: crc: Wait for a frame before returning from open()") adds a wait for CRC frame, but with the CRTC off this will never be generated. For atomic drivers we know if a CRTC is active through crtc_state->active, so when inactive reject the open with -EIO.

Quoted 3 lines
Fixes e8fa5671183c ("drm: crc: Wait for a frame before returning from open()")

Testcase: debugfs_test.read_all_entries

Quoted 6 lines

At least for the semantics I think this makes sense. Opening the CRC file when the crtc is off is undefined.

But pls get Tomeu's ack too.

Tomeu, can you ack? :)

I did some testing on IGT with both patches applied on all tests with CRC in their name, no problems with opening CRC as far as I can see, and all tests except kms_ccs and kms_mmap_write_crc succeed. The former needs render compression, the latter fails on a crc failure, so it can't have been caused by this patch.

_______________________________________________
Intel-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/intel-gfx