Skip to contentSkip to conversation

drm/amdgpu: Fix blocking in RCU critical section(v2)

PATCH In review Alex Xie 5 participants 5 messages Jul 20, 2017 – Jul 26, 2017 3 messages not in the archive

Subsystems (2)
lore
Share & tools
lore

Alex Xie

author
#0 lore raw

PATCH 1/1 drm/amdgpu: Fix blocking in RCU critical section(v2)

In RCU read-side critical sections, blocking or sleeping is prohibited.

v2: Unlock RCU for the code path where result==NULL. (David Zhou)
Update subject

Tested-by and reported by: Dave Airlie <[email protected]>

[  141.965723] =============================
[  141.965724] WARNING: suspicious RCU usage
[  141.965726] 4.12.0-rc7 #221 Not tainted
[  141.965727] -----------------------------
[  141.965728] /home/airlied/devel/kernel/linux-2.6/include/linux/rcupdate.h:531
Illegal context switch in RCU read-side critical section!
[  141.965730]
               other info that might help us debug this:
[  141.965731]
               rcu_scheduler_active = 2, debug_locks = 0
[  141.965732] 1 lock held by amdgpu_cs:0/1332:
[  141.965733]  #0:  (rcu_read_lock){......}, at: [<ffffffffa01a0d07>]
amdgpu_bo_list_get+0x0/0x109 [amdgpu]
[  141.965774]
               stack backtrace:
[  141.965776] CPU: 6 PID: 1332 Comm: amdgpu_cs:0 Not tainted 4.12.0-rc7 #221
[  141.965777] Hardware name: To be filled by O.E.M. To be filled by
O.E.M./M5A97 R2.0, BIOS 2603 06/26/2015
[  141.965778] Call Trace:
[  141.965782]  dump_stack+0x68/0x92
[  141.965785]  lockdep_rcu_suspicious+0xf7/0x100
[  141.965788]  ___might_sleep+0x56/0x1fc
[  141.965790]  __might_sleep+0x68/0x6f
[  141.965793]  __mutex_lock+0x4e/0x7b5
[  141.965817]  ? amdgpu_bo_list_get+0xa4/0x109 [amdgpu]
[  141.965820]  ? lock_acquire+0x125/0x1b9
[  141.965844]  ? amdgpu_bo_list_set+0x464/0x464 [amdgpu]
[  141.965846]  mutex_lock_nested+0x16/0x18
[  141.965848]  ? mutex_lock_nested+0x16/0x18
[  141.965872]  amdgpu_bo_list_get+0xa4/0x109 [amdgpu]
[  141.965895]  amdgpu_cs_ioctl+0x4a0/0x17dd [amdgpu]
[  141.965898]  ? radix_tree_node_alloc.constprop.11+0x77/0xab
[  141.965916]  drm_ioctl+0x264/0x393 [drm]
[  141.965939]  ? amdgpu_cs_find_mapping+0x83/0x83 [amdgpu]
[  141.965942]  ? trace_hardirqs_on_caller+0x16a/0x186
---
 drivers/gpu/drm/amd/amdgpu/amdgpu_bo_list.c | 12 +++++++++---
 1 file changed, 9 insertions(+), 3 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_bo_list.c +9 -3
index dc87962..e5d61fe 100644
@@ -198,12 +198,18 @@ amdgpu_bo_list_get(struct amdgpu_fpriv *fpriv, int id)
 	result = idr_find(&fpriv->bo_list_handles, id);
 
 	if (result) {
-		if (kref_get_unless_zero(&result->refcount))
+		if (kref_get_unless_zero(&result->refcount)) {
+			rcu_read_unlock();
 			mutex_lock(&result->lock);
-		else
+		}
+		else {
+			rcu_read_unlock();
 			result = NULL;
+		}
+	}
+	else {
+		rcu_read_unlock();
 	}
-	rcu_read_unlock();
 
 	return result;
 }
Signature · 6 lines
2.7.4

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

A message that is not in the archive (sent off-list or not yet received): 1500518816-12399-1-git-send-email-AlexBin.Xie-5C7GfCeVMHo@public.gmane.org

#2 lore raw

one small nit pick, with that fix, Reviewed-by: Chunming Zhou <[email protected]>

Quoted 54 lines · Alex Xie

In RCU read-side critical sections, blocking or sleeping is prohibited.

v2: Unlock RCU for the code path where result==NULL. (David Zhou)
Update subject

Tested-by and reported by: Dave Airlie <[email protected]>

[  141.965723] =============================
[  141.965724] WARNING: suspicious RCU usage
[  141.965726] 4.12.0-rc7 #221 Not tainted
[  141.965727] -----------------------------
[  141.965728] /home/airlied/devel/kernel/linux-2.6/include/linux/rcupdate.h:531
Illegal context switch in RCU read-side critical section!
[  141.965730]
                other info that might help us debug this:
[  141.965731]
                rcu_scheduler_active = 2, debug_locks = 0
[  141.965732] 1 lock held by amdgpu_cs:0/1332:
[  141.965733]  #0:  (rcu_read_lock){......}, at: [<ffffffffa01a0d07>]
amdgpu_bo_list_get+0x0/0x109 [amdgpu]
[  141.965774]
                stack backtrace:
[  141.965776] CPU: 6 PID: 1332 Comm: amdgpu_cs:0 Not tainted 4.12.0-rc7 #221
[  141.965777] Hardware name: To be filled by O.E.M. To be filled by
O.E.M./M5A97 R2.0, BIOS 2603 06/26/2015
[  141.965778] Call Trace:
[  141.965782]  dump_stack+0x68/0x92
[  141.965785]  lockdep_rcu_suspicious+0xf7/0x100
[  141.965788]  ___might_sleep+0x56/0x1fc
[  141.965790]  __might_sleep+0x68/0x6f
[  141.965793]  __mutex_lock+0x4e/0x7b5
[  141.965817]  ? amdgpu_bo_list_get+0xa4/0x109 [amdgpu]
[  141.965820]  ? lock_acquire+0x125/0x1b9
[  141.965844]  ? amdgpu_bo_list_set+0x464/0x464 [amdgpu]
[  141.965846]  mutex_lock_nested+0x16/0x18
[  141.965848]  ? mutex_lock_nested+0x16/0x18
[  141.965872]  amdgpu_bo_list_get+0xa4/0x109 [amdgpu]
[  141.965895]  amdgpu_cs_ioctl+0x4a0/0x17dd [amdgpu]
[  141.965898]  ? radix_tree_node_alloc.constprop.11+0x77/0xab
[  141.965916]  drm_ioctl+0x264/0x393 [drm]
[  141.965939]  ? amdgpu_cs_find_mapping+0x83/0x83 [amdgpu]
[  141.965942]  ? trace_hardirqs_on_caller+0x16a/0x186
---
  drivers/gpu/drm/amd/amdgpu/amdgpu_bo_list.c | 12 +++++++++---
  1 file changed, 9 insertions(+), 3 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_bo_list.c +0 -0
index dc87962..e5d61fe 100644

drivers/gpu/drm/amd/amdgpu/amdgpu_bo_list.c amdgpu_bo_list_get(struct amdgpu_fpriv *fpriv, int id) · line 205

Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_bo_list.c +4 -2
@@ -198,12 +198,18 @@ amdgpu_bo_list_get(struct amdgpu_fpriv *fpriv, int id)
  	result = idr_find(&fpriv->bo_list_handles, id);
 
  	if (result) {
-		if (kref_get_unless_zero(&result->refcount))
+		if (kref_get_unless_zero(&result->refcount)) {
+			rcu_read_unlock();
  			mutex_lock(&result->lock);
-		else
+		}
+		else {

don't need another line, just like } else {

drivers/gpu/drm/amd/amdgpu/amdgpu_bo_list.c amdgpu_bo_list_get(struct amdgpu_fpriv *fpriv, int id) · line 210

Quoted 5 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_bo_list.c +4 -0
+			rcu_read_unlock();
  			result = NULL;
+		}
+	}
+	else {

don't need another line, just like } else {

drivers/gpu/drm/amd/amdgpu/amdgpu_bo_list.c amdgpu_bo_list_get(struct amdgpu_fpriv *fpriv, int id) · line 215

Quoted 6 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_bo_list.c +1 -1
+		rcu_read_unlock();
  	}
-	rcu_read_unlock();
 
  	return result;
  }

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

A message that is not in the archive (sent off-list or not yet received): [email protected]

#4 lore raw

Yeah, with the coding style fixed Reviewed-by: Christian König <[email protected]> as well.

Christian.

Am 20.07.2017 um 05:01 schrieb zhoucm1:

Quoted 80 lines

one small nit pick, with that fix, Reviewed-by: Chunming Zhou <[email protected]>

Quoted 54 lines · Alex Xie

In RCU read-side critical sections, blocking or sleeping is prohibited.

v2: Unlock RCU for the code path where result==NULL. (David Zhou)
Update subject

Tested-by and reported by: Dave Airlie <[email protected]>

[  141.965723] =============================
[  141.965724] WARNING: suspicious RCU usage
[  141.965726] 4.12.0-rc7 #221 Not tainted
[  141.965727] -----------------------------
[  141.965728] /home/airlied/devel/kernel/linux-2.6/include/linux/rcupdate.h:531
Illegal context switch in RCU read-side critical section!
[  141.965730]
                other info that might help us debug this:
[  141.965731]
                rcu_scheduler_active = 2, debug_locks = 0
[  141.965732] 1 lock held by amdgpu_cs:0/1332:
[  141.965733]  #0:  (rcu_read_lock){......}, at: [<ffffffffa01a0d07>]
amdgpu_bo_list_get+0x0/0x109 [amdgpu]
[  141.965774]
                stack backtrace:
[  141.965776] CPU: 6 PID: 1332 Comm: amdgpu_cs:0 Not tainted 4.12.0-rc7 #221
[  141.965777] Hardware name: To be filled by O.E.M. To be filled by
O.E.M./M5A97 R2.0, BIOS 2603 06/26/2015
[  141.965778] Call Trace:
[  141.965782]  dump_stack+0x68/0x92
[  141.965785]  lockdep_rcu_suspicious+0xf7/0x100
[  141.965788]  ___might_sleep+0x56/0x1fc
[  141.965790]  __might_sleep+0x68/0x6f
[  141.965793]  __mutex_lock+0x4e/0x7b5
[  141.965817]  ? amdgpu_bo_list_get+0xa4/0x109 [amdgpu]
[  141.965820]  ? lock_acquire+0x125/0x1b9
[  141.965844]  ? amdgpu_bo_list_set+0x464/0x464 [amdgpu]
[  141.965846]  mutex_lock_nested+0x16/0x18
[  141.965848]  ? mutex_lock_nested+0x16/0x18
[  141.965872]  amdgpu_bo_list_get+0xa4/0x109 [amdgpu]
[  141.965895]  amdgpu_cs_ioctl+0x4a0/0x17dd [amdgpu]
[  141.965898]  ? radix_tree_node_alloc.constprop.11+0x77/0xab
[  141.965916]  drm_ioctl+0x264/0x393 [drm]
[  141.965939]  ? amdgpu_cs_find_mapping+0x83/0x83 [amdgpu]
[  141.965942]  ? trace_hardirqs_on_caller+0x16a/0x186
---
  drivers/gpu/drm/amd/amdgpu/amdgpu_bo_list.c | 12 +++++++++---
  1 file changed, 9 insertions(+), 3 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_bo_list.c +0 -0
index dc87962..e5d61fe 100644

drivers/gpu/drm/amd/amdgpu/amdgpu_bo_list.c amdgpu_bo_list_get(struct amdgpu_fpriv *fpriv, · line 205

Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_bo_list.c +4 -2
@@ -198,12 +198,18 @@ amdgpu_bo_list_get(struct amdgpu_fpriv *fpriv, 
 int id)
      result = idr_find(&fpriv->bo_list_handles, id);
        if (result) {
-        if (kref_get_unless_zero(&result->refcount))
+        if (kref_get_unless_zero(&result->refcount)) {
+            rcu_read_unlock();
              mutex_lock(&result->lock);
-        else
+        }
+        else {

don't need another line, just like } else {

drivers/gpu/drm/amd/amdgpu/amdgpu_bo_list.c amdgpu_bo_list_get(struct amdgpu_fpriv *fpriv, int id) · line 210

Quoted 5 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_bo_list.c +4 -0
+            rcu_read_unlock();
              result = NULL;
+        }
+    }
+    else {

don't need another line, just like } else {

drivers/gpu/drm/amd/amdgpu/amdgpu_bo_list.c amdgpu_bo_list_get(struct amdgpu_fpriv *fpriv, int id) · line 215

Quoted 5 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_bo_list.c +1 -1
+        rcu_read_unlock();
      }
-    rcu_read_unlock();
        return result;
  }
Quoted 4 lines

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

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

#5 lore raw
In reply to a message not in the archive

Hi Alex,

Quoted 13 lines · Alex Xie
drivers/gpu/drm/amd/amdgpu/amdgpu_bo_list.c +4 -2
@@ -198,12 +198,18 @@ amdgpu_bo_list_get(struct amdgpu_fpriv *fpriv, int id)
        result = idr_find(&fpriv->bo_list_handles, id);
 
        if (result) {
-               if (kref_get_unless_zero(&result->refcount))
+               if (kref_get_unless_zero(&result->refcount)) {
+                       rcu_read_unlock();
                        mutex_lock(&result->lock);
-               else
+               }
+               else {

drivers/gpu/drm/amd/amdgpu/amdgpu_bo_list.c amdgpu_bo_list_get(struct amdgpu_fpriv *fpriv, int id) · line 214

Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_bo_list.c +5 -1
+                       rcu_read_unlock();
                        result = NULL;
+               }
+       }
+       else {
+               rcu_read_unlock();
        }
-       rcu_read_unlock();
 
        return result;

A drive-by suggestion - feel free to ignore.

The "return early" approach seems great IMHO. The code will be shorter, indentation - less, no {}/else to track plus overall it seems clearer.
Namely:

   result = idr_find(&fpriv->bo_list_handles, id);

   if (!result || !kref_get_unless_zero(&result->refcount)) {
         rcu_read_unlock();
         return NULL;
   }

rcu_read_unlock();
mutex_lock(&result->lock);
return result;
}

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

A message that is not in the archive (sent off-list or not yet received): CACvgo53BAk8t-VxxgftA1Un2MUxSW=61=SgNfQEPBm+c9wejtQ-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org

#7 lore raw

Hi Emil,

Sorry for late reply. I was so busy last week and this week.

Your logic looks correct, smarter and shorter. I feel relatively more difficult to understand the logic, with two "!" and one "||" in the same if statement.

Thanks for the advice always.

Quoted 45 lines · Emil Velikov

Hi Alex,

Quoted 13 lines · Alex Xie
drivers/gpu/drm/amd/amdgpu/amdgpu_bo_list.c +4 -2
@@ -198,12 +198,18 @@ amdgpu_bo_list_get(struct amdgpu_fpriv *fpriv, int id)
         result = idr_find(&fpriv->bo_list_handles, id);
 
         if (result) {
-               if (kref_get_unless_zero(&result->refcount))
+               if (kref_get_unless_zero(&result->refcount)) {
+                       rcu_read_unlock();
                         mutex_lock(&result->lock);
-               else
+               }
+               else {

drivers/gpu/drm/amd/amdgpu/amdgpu_bo_list.c amdgpu_bo_list_get(struct amdgpu_fpriv *fpriv, int id) · line 214

Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_bo_list.c +5 -1
+                       rcu_read_unlock();
                         result = NULL;
+               }
+       }
+       else {
+               rcu_read_unlock();
         }
-       rcu_read_unlock();
 
         return result;

A drive-by suggestion - feel free to ignore.

The "return early" approach seems great IMHO. The code will be shorter, indentation - less, no {}/else to track plus overall it seems clearer.
Namely:

    result = idr_find(&fpriv->bo_list_handles, id);

    if (!result || !kref_get_unless_zero(&result->refcount)) {
          rcu_read_unlock();
          return NULL;
    }

    rcu_read_unlock();
    mutex_lock(&result->lock);
    return result;
}
Quoted 2 lines

HTH
Emil

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