Skip to contentSkip to conversation

drm: fix call_kern.cocci warnings v3

PATCH In review Chunming Zhou 6 participants 8 messages Oct 25, 2018

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

PATCH 1/1 drm: fix call_kern.cocci warnings v3

drivers/gpu/drm/drm_syncobj.c:202:4-14: ERROR: function drm_syncobj_find_signal_pt_for_point called on line 390 inside lock on line 389 but uses GFP_KERNEL

Find functions that refer to GFP_KERNEL but are called with locks held.

Generated by: scripts/coccinelle/locks/call_kern.cocci

v2:
syncobj->timeline still needs protect.

v3:
use a global signaled fence instead of re-allocation.

Cc Maarten Lankhorst <[email protected]>
Cc Christian König <[email protected]>
---
 drivers/gpu/drm/drm_drv.c     |  2 ++
 drivers/gpu/drm/drm_syncobj.c | 52 +++++++++++++++++++++--------------
 include/drm/drm_syncobj.h     |  1 +
 3 files changed, 34 insertions(+), 21 deletions(-)
drivers/gpu/drm/drm_drv.c +2 -0
index 36e8e9cbec52..0a6f1023d6c3 100644
@@ -37,6 +37,7 @@
 #include <drm/drm_client.h>
 #include <drm/drm_drv.h>
 #include <drm/drmP.h>
+#include <drm/drm_syncobj.h>
 
 #include "drm_crtc_internal.h"
 #include "drm_legacy.h"
@@ -1003,6 +1004,7 @@ static int __init drm_core_init(void)
 	if (ret < 0)
 		goto error;
 
+	drm_syncobj_stub_fence_init();
 	drm_core_init_complete = true;
 
 	DRM_DEBUG("Initialized\n");
drivers/gpu/drm/drm_syncobj.c +31 -21

90 lines not on this page · raw message

include/drm/drm_syncobj.h +1 -0
index 29244cbcd05e..63cfd1540241 100644
@@ -148,4 +148,5 @@ int drm_syncobj_get_fd(struct drm_syncobj *syncobj, int *p_fd);
 int drm_syncobj_search_fence(struct drm_syncobj *syncobj, u64 point, u64 flags,
 			     struct dma_fence **fence);
 
+void drm_syncobj_stub_fence_init(void);
 #endif
Signature · 6 lines
2.17.1

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

✗ Fi.CI.CHECKPATCH: warning for drm: fix call_kern.cocci warnings (rev3)

== Series Details ==

== Series Details ==

Series: drm: fix call_kern.cocci warnings (rev3)
URL : https://patchwork.freedesktop.org/series/51498/
State : warning

== Summary ==

$ dim checkpatch origin/drm-tip
2fa016495d37 drm: fix call_kern.cocci warnings v3
-:58: WARNING:AVOID_BUG: Avoid crashing the kernel - try using WARN_ON & recovery code rather than BUG() or BUG_ON()
#58: FILE: drivers/gpu/drm/drm_syncobj.c:87:
+	BUG();

-:60: CHECK:LINE_SPACING: Please use a blank line after function/struct/union/enum declarations #60: FILE: drivers/gpu/drm/drm_syncobj.c:89:
+}
+static const struct dma_fence_ops global_stub_fence_ops = {

total: 0 errors, 1 warnings, 1 checks, 109 lines checked

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

#2 lore raw
In reply to Chunming Zhou

Op 25-10-18 om 12:21 schreef Chunming Zhou:

Quoted 47 lines

drivers/gpu/drm/drm_syncobj.c:202:4-14: ERROR: function drm_syncobj_find_signal_pt_for_point called on line 390 inside lock on line 389 but uses GFP_KERNEL

Find functions that refer to GFP_KERNEL but are called with locks held.

Generated by: scripts/coccinelle/locks/call_kern.cocci

v2:
syncobj->timeline still needs protect.

v3:
use a global signaled fence instead of re-allocation.

Cc Maarten Lankhorst <[email protected]>
Cc Christian König <[email protected]>
---
 drivers/gpu/drm/drm_drv.c     |  2 ++
 drivers/gpu/drm/drm_syncobj.c | 52 +++++++++++++++++++++--------------
 include/drm/drm_syncobj.h     |  1 +
 3 files changed, 34 insertions(+), 21 deletions(-)
drivers/gpu/drm/drm_drv.c +2 -0
index 36e8e9cbec52..0a6f1023d6c3 100644
@@ -37,6 +37,7 @@
 #include <drm/drm_client.h>
 #include <drm/drm_drv.h>
 #include <drm/drmP.h>
+#include <drm/drm_syncobj.h>
 
 #include "drm_crtc_internal.h"
 #include "drm_legacy.h"
@@ -1003,6 +1004,7 @@ static int __init drm_core_init(void)
 	if (ret < 0)
 		goto error;
 
+	drm_syncobj_stub_fence_init();
 	drm_core_init_complete = true;
 
 	DRM_DEBUG("Initialized\n");
drivers/gpu/drm/drm_syncobj.c +0 -0
index b7eaa603f368..6b3f5a06e4d3 100644

drivers/gpu/drm/drm_syncobj.c struct drm_syncobj_signal_pt { · line 88

Quoted 9 lines
drivers/gpu/drm/drm_syncobj.c +6 -0
@@ -80,6 +80,27 @@ struct drm_syncobj_signal_pt {
 	struct list_head list;
 };
 
+static struct drm_syncobj_stub_fence stub_signaled_fence;
+static void global_stub_fence_release(struct dma_fence *fence)
+{
+	/* it is impossible to come here */
+	BUG();
+}

WARN_ON_ONCE(1)? No need to halt the machine.

Quoted 90 lines
drivers/gpu/drm/drm_syncobj.c +25 -21
+static const struct dma_fence_ops global_stub_fence_ops = {
+	.get_driver_name = drm_syncobj_stub_fence_get_name,
+	.get_timeline_name = drm_syncobj_stub_fence_get_name,
+	.release = global_stub_fence_release,
+};
+
+void drm_syncobj_stub_fence_init(void)
+{
+	spin_lock_init(&stub_signaled_fence.lock);
+	dma_fence_init(&stub_signaled_fence.base,
+		       &global_stub_fence_ops,
+		       &stub_signaled_fence.lock,
+		       0, 0);
+	dma_fence_signal(&stub_signaled_fence.base);
+}
 /**
  * drm_syncobj_find - lookup and reference a sync object.
  * @file_private: drm file private pointer
@@ -111,24 +132,14 @@ static struct dma_fence
 				      uint64_t point)
 {
 	struct drm_syncobj_signal_pt *signal_pt;
+	struct dma_fence *f = NULL;
 
+	spin_lock(&syncobj->pt_lock);
 	if ((syncobj->type == DRM_SYNCOBJ_TYPE_TIMELINE) &&
 	    (point <= syncobj->timeline)) {
-		struct drm_syncobj_stub_fence *fence =
-			kzalloc(sizeof(struct drm_syncobj_stub_fence),
-				GFP_KERNEL);
-
-		if (!fence)
-			return NULL;
-		spin_lock_init(&fence->lock);
-		dma_fence_init(&fence->base,
-			       &drm_syncobj_stub_fence_ops,
-			       &fence->lock,
-			       syncobj->timeline_context,
-			       point);
-
-		dma_fence_signal(&fence->base);
-		return &fence->base;
+		dma_fence_get(&stub_signaled_fence.base);
+		spin_unlock(&syncobj->pt_lock);
+		return &stub_signaled_fence.base;
 	}
 
 	list_for_each_entry(signal_pt, &syncobj->signal_pt_list, list) {
@@ -137,9 +148,12 @@ static struct dma_fence
 		if ((syncobj->type == DRM_SYNCOBJ_TYPE_BINARY) &&
 		    (point != signal_pt->value))
 			continue;
-		return dma_fence_get(&signal_pt->fence_array->base);
+		f = dma_fence_get(&signal_pt->fence_array->base);
+		break;
 	}
-	return NULL;
+	spin_unlock(&syncobj->pt_lock);
+
+	return f;
 }
 
 static void drm_syncobj_add_callback_locked(struct drm_syncobj *syncobj,
@@ -166,9 +180,7 @@ static void drm_syncobj_fence_get_or_add_callback(struct drm_syncobj *syncobj,
 	}
 
 	mutex_lock(&syncobj->cb_mutex);
-	spin_lock(&syncobj->pt_lock);
 	*fence = drm_syncobj_find_signal_pt_for_point(syncobj, pt_value);
-	spin_unlock(&syncobj->pt_lock);
 	if (!*fence)
 		drm_syncobj_add_callback_locked(syncobj, cb, func);
 	mutex_unlock(&syncobj->cb_mutex);
@@ -379,11 +391,9 @@ drm_syncobj_point_get(struct drm_syncobj *syncobj, u64 point, u64 flags,
 		if (ret < 0)
 			return ret;
 	}
-	spin_lock(&syncobj->pt_lock);
 	*fence = drm_syncobj_find_signal_pt_for_point(syncobj, point);
 	if (!*fence)
 		ret = -EINVAL;
-	spin_unlock(&syncobj->pt_lock);
 	return ret;
 }
 
include/drm/drm_syncobj.h +0 -0
index 29244cbcd05e..63cfd1540241 100644

include/drm/drm_syncobj.h int drm_syncobj_get_fd(struct drm_syncobj *syncobj, int *p_fd); · line 152

Quoted 5 lines
include/drm/drm_syncobj.h +1 -0
@@ -148,4 +148,5 @@ int drm_syncobj_get_fd(struct drm_syncobj *syncobj, int *p_fd);
 int drm_syncobj_search_fence(struct drm_syncobj *syncobj, u64 point, u64 flags,
 			     struct dma_fence **fence);
 
+void drm_syncobj_stub_fence_init(void);
 #endif

Move it to drm_internal.h ? This is not for driver consumption. :)

i would split up the changes at this point:

1. Replace memory allocation with a static fence, not necessarily changing any locking, add a fixes tag to this commit.

2. Move locking.

3. Fix drm_syncobj_fence_get_or_add_callback to return an error.

That way it should be nicely bisectable.

~Maarten

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

#3 lore raw

Am 25.10.18 um 12:36 schrieb Maarten Lankhorst:

Quoted 156 lines

Op 25-10-18 om 12:21 schreef Chunming Zhou:

Quoted 47 lines

drivers/gpu/drm/drm_syncobj.c:202:4-14: ERROR: function drm_syncobj_find_signal_pt_for_point called on line 390 inside lock on line 389 but uses GFP_KERNEL

Find functions that refer to GFP_KERNEL but are called with locks held.

Generated by: scripts/coccinelle/locks/call_kern.cocci

v2:
syncobj->timeline still needs protect.

v3:
use a global signaled fence instead of re-allocation.

Cc Maarten Lankhorst <[email protected]>
Cc Christian König <[email protected]>
---
  drivers/gpu/drm/drm_drv.c     |  2 ++
  drivers/gpu/drm/drm_syncobj.c | 52 +++++++++++++++++++++--------------
  include/drm/drm_syncobj.h     |  1 +
  3 files changed, 34 insertions(+), 21 deletions(-)
drivers/gpu/drm/drm_drv.c +2 -0
index 36e8e9cbec52..0a6f1023d6c3 100644
@@ -37,6 +37,7 @@
  #include <drm/drm_client.h>
  #include <drm/drm_drv.h>
  #include <drm/drmP.h>
+#include <drm/drm_syncobj.h>
 
  #include "drm_crtc_internal.h"
  #include "drm_legacy.h"
@@ -1003,6 +1004,7 @@ static int __init drm_core_init(void)
  	if (ret < 0)
  		goto error;
 
+	drm_syncobj_stub_fence_init();
  	drm_core_init_complete = true;
 
  	DRM_DEBUG("Initialized\n");
drivers/gpu/drm/drm_syncobj.c +0 -0
index b7eaa603f368..6b3f5a06e4d3 100644

drivers/gpu/drm/drm_syncobj.c struct drm_syncobj_signal_pt { · line 88

Quoted 9 lines
drivers/gpu/drm/drm_syncobj.c +6 -0
@@ -80,6 +80,27 @@ struct drm_syncobj_signal_pt {
  	struct list_head list;
  };
 
+static struct drm_syncobj_stub_fence stub_signaled_fence;
+static void global_stub_fence_release(struct dma_fence *fence)
+{
+	/* it is impossible to come here */
+	BUG();
+}

WARN_ON_ONCE(1)? No need to halt the machine.

Quoted 90 lines
drivers/gpu/drm/drm_syncobj.c +25 -21
+static const struct dma_fence_ops global_stub_fence_ops = {
+	.get_driver_name = drm_syncobj_stub_fence_get_name,
+	.get_timeline_name = drm_syncobj_stub_fence_get_name,
+	.release = global_stub_fence_release,
+};
+
+void drm_syncobj_stub_fence_init(void)
+{
+	spin_lock_init(&stub_signaled_fence.lock);
+	dma_fence_init(&stub_signaled_fence.base,
+		       &global_stub_fence_ops,
+		       &stub_signaled_fence.lock,
+		       0, 0);
+	dma_fence_signal(&stub_signaled_fence.base);
+}
  /**
   * drm_syncobj_find - lookup and reference a sync object.
   * @file_private: drm file private pointer
@@ -111,24 +132,14 @@ static struct dma_fence
  				      uint64_t point)
  {
  	struct drm_syncobj_signal_pt *signal_pt;
+	struct dma_fence *f = NULL;
 
+	spin_lock(&syncobj->pt_lock);
  	if ((syncobj->type == DRM_SYNCOBJ_TYPE_TIMELINE) &&
  	    (point <= syncobj->timeline)) {
-		struct drm_syncobj_stub_fence *fence =
-			kzalloc(sizeof(struct drm_syncobj_stub_fence),
-				GFP_KERNEL);
-
-		if (!fence)
-			return NULL;
-		spin_lock_init(&fence->lock);
-		dma_fence_init(&fence->base,
-			       &drm_syncobj_stub_fence_ops,
-			       &fence->lock,
-			       syncobj->timeline_context,
-			       point);
-
-		dma_fence_signal(&fence->base);
-		return &fence->base;
+		dma_fence_get(&stub_signaled_fence.base);
+		spin_unlock(&syncobj->pt_lock);
+		return &stub_signaled_fence.base;
  	}
 
  	list_for_each_entry(signal_pt, &syncobj->signal_pt_list, list) {
@@ -137,9 +148,12 @@ static struct dma_fence
  		if ((syncobj->type == DRM_SYNCOBJ_TYPE_BINARY) &&
  		    (point != signal_pt->value))
  			continue;
-		return dma_fence_get(&signal_pt->fence_array->base);
+		f = dma_fence_get(&signal_pt->fence_array->base);
+		break;
  	}
-	return NULL;
+	spin_unlock(&syncobj->pt_lock);
+
+	return f;
  }
 
  static void drm_syncobj_add_callback_locked(struct drm_syncobj *syncobj,
@@ -166,9 +180,7 @@ static void drm_syncobj_fence_get_or_add_callback(struct drm_syncobj *syncobj,
  	}
 
  	mutex_lock(&syncobj->cb_mutex);
-	spin_lock(&syncobj->pt_lock);
  	*fence = drm_syncobj_find_signal_pt_for_point(syncobj, pt_value);
-	spin_unlock(&syncobj->pt_lock);
  	if (!*fence)
  		drm_syncobj_add_callback_locked(syncobj, cb, func);
  	mutex_unlock(&syncobj->cb_mutex);
@@ -379,11 +391,9 @@ drm_syncobj_point_get(struct drm_syncobj *syncobj, u64 point, u64 flags,
  		if (ret < 0)
  			return ret;
  	}
-	spin_lock(&syncobj->pt_lock);
  	*fence = drm_syncobj_find_signal_pt_for_point(syncobj, point);
  	if (!*fence)
  		ret = -EINVAL;
-	spin_unlock(&syncobj->pt_lock);
  	return ret;
  }
 
include/drm/drm_syncobj.h +0 -0
index 29244cbcd05e..63cfd1540241 100644

include/drm/drm_syncobj.h int drm_syncobj_get_fd(struct drm_syncobj *syncobj, int *p_fd); · line 152

Quoted 5 lines
include/drm/drm_syncobj.h +1 -0
@@ -148,4 +148,5 @@ int drm_syncobj_get_fd(struct drm_syncobj *syncobj, int *p_fd);
  int drm_syncobj_search_fence(struct drm_syncobj *syncobj, u64 point, u64 flags,
  			     struct dma_fence **fence);
 
+void drm_syncobj_stub_fence_init(void);
  #endif

Move it to drm_internal.h ? This is not for driver consumption. :)

Quoted 7 lines

i would split up the changes at this point:

1. Replace memory allocation with a static fence, not necessarily changing any locking, add a fixes tag to this commit.

2. Move locking.

3. Fix drm_syncobj_fence_get_or_add_callback to return an error.

Actually we don't need to move the locking or change into returning an error code any more.

Without any allocation we don't have anything that could fail.

Christian.

Quoted 5 lines

That way it should be nicely bisectable.

~Maarten

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

#4 lore raw
In reply to Maarten Lankhorst
Quoted 156 lines · Maarten Lankhorst

Op 25-10-18 om 12:21 schreef Chunming Zhou:

Quoted 47 lines

drivers/gpu/drm/drm_syncobj.c:202:4-14: ERROR: function drm_syncobj_find_signal_pt_for_point called on line 390 inside lock on line 389 but uses GFP_KERNEL

Find functions that refer to GFP_KERNEL but are called with locks held.

Generated by: scripts/coccinelle/locks/call_kern.cocci

v2:
syncobj->timeline still needs protect.

v3:
use a global signaled fence instead of re-allocation.

Cc Maarten Lankhorst <[email protected]>
Cc Christian König <[email protected]>
---
  drivers/gpu/drm/drm_drv.c     |  2 ++
  drivers/gpu/drm/drm_syncobj.c | 52 +++++++++++++++++++++--------------
  include/drm/drm_syncobj.h     |  1 +
  3 files changed, 34 insertions(+), 21 deletions(-)
drivers/gpu/drm/drm_drv.c +2 -0
index 36e8e9cbec52..0a6f1023d6c3 100644
@@ -37,6 +37,7 @@
  #include <drm/drm_client.h>
  #include <drm/drm_drv.h>
  #include <drm/drmP.h>
+#include <drm/drm_syncobj.h>
 
  #include "drm_crtc_internal.h"
  #include "drm_legacy.h"
@@ -1003,6 +1004,7 @@ static int __init drm_core_init(void)
  	if (ret < 0)
  		goto error;
 
+	drm_syncobj_stub_fence_init();
  	drm_core_init_complete = true;
 
  	DRM_DEBUG("Initialized\n");
drivers/gpu/drm/drm_syncobj.c +0 -0
index b7eaa603f368..6b3f5a06e4d3 100644

drivers/gpu/drm/drm_syncobj.c struct drm_syncobj_signal_pt { · line 88

Quoted 9 lines
drivers/gpu/drm/drm_syncobj.c +6 -0
@@ -80,6 +80,27 @@ struct drm_syncobj_signal_pt {
  	struct list_head list;
  };
 
+static struct drm_syncobj_stub_fence stub_signaled_fence;
+static void global_stub_fence_release(struct dma_fence *fence)
+{
+	/* it is impossible to come here */
+	BUG();
+}

WARN_ON_ONCE(1)? No need to halt the machine.

Quoted 90 lines
drivers/gpu/drm/drm_syncobj.c +25 -21
+static const struct dma_fence_ops global_stub_fence_ops = {
+	.get_driver_name = drm_syncobj_stub_fence_get_name,
+	.get_timeline_name = drm_syncobj_stub_fence_get_name,
+	.release = global_stub_fence_release,
+};
+
+void drm_syncobj_stub_fence_init(void)
+{
+	spin_lock_init(&stub_signaled_fence.lock);
+	dma_fence_init(&stub_signaled_fence.base,
+		       &global_stub_fence_ops,
+		       &stub_signaled_fence.lock,
+		       0, 0);
+	dma_fence_signal(&stub_signaled_fence.base);
+}
  /**
   * drm_syncobj_find - lookup and reference a sync object.
   * @file_private: drm file private pointer
@@ -111,24 +132,14 @@ static struct dma_fence
  				      uint64_t point)
  {
  	struct drm_syncobj_signal_pt *signal_pt;
+	struct dma_fence *f = NULL;
 
+	spin_lock(&syncobj->pt_lock);
  	if ((syncobj->type == DRM_SYNCOBJ_TYPE_TIMELINE) &&
  	    (point <= syncobj->timeline)) {
-		struct drm_syncobj_stub_fence *fence =
-			kzalloc(sizeof(struct drm_syncobj_stub_fence),
-				GFP_KERNEL);
-
-		if (!fence)
-			return NULL;
-		spin_lock_init(&fence->lock);
-		dma_fence_init(&fence->base,
-			       &drm_syncobj_stub_fence_ops,
-			       &fence->lock,
-			       syncobj->timeline_context,
-			       point);
-
-		dma_fence_signal(&fence->base);
-		return &fence->base;
+		dma_fence_get(&stub_signaled_fence.base);
+		spin_unlock(&syncobj->pt_lock);
+		return &stub_signaled_fence.base;
  	}
 
  	list_for_each_entry(signal_pt, &syncobj->signal_pt_list, list) {
@@ -137,9 +148,12 @@ static struct dma_fence
  		if ((syncobj->type == DRM_SYNCOBJ_TYPE_BINARY) &&
  		    (point != signal_pt->value))
  			continue;
-		return dma_fence_get(&signal_pt->fence_array->base);
+		f = dma_fence_get(&signal_pt->fence_array->base);
+		break;
  	}
-	return NULL;
+	spin_unlock(&syncobj->pt_lock);
+
+	return f;
  }
 
  static void drm_syncobj_add_callback_locked(struct drm_syncobj *syncobj,
@@ -166,9 +180,7 @@ static void drm_syncobj_fence_get_or_add_callback(struct drm_syncobj *syncobj,
  	}
 
  	mutex_lock(&syncobj->cb_mutex);
-	spin_lock(&syncobj->pt_lock);
  	*fence = drm_syncobj_find_signal_pt_for_point(syncobj, pt_value);
-	spin_unlock(&syncobj->pt_lock);
  	if (!*fence)
  		drm_syncobj_add_callback_locked(syncobj, cb, func);
  	mutex_unlock(&syncobj->cb_mutex);
@@ -379,11 +391,9 @@ drm_syncobj_point_get(struct drm_syncobj *syncobj, u64 point, u64 flags,
  		if (ret < 0)
  			return ret;
  	}
-	spin_lock(&syncobj->pt_lock);
  	*fence = drm_syncobj_find_signal_pt_for_point(syncobj, point);
  	if (!*fence)
  		ret = -EINVAL;
-	spin_unlock(&syncobj->pt_lock);
  	return ret;
  }
 
include/drm/drm_syncobj.h +0 -0
index 29244cbcd05e..63cfd1540241 100644

include/drm/drm_syncobj.h int drm_syncobj_get_fd(struct drm_syncobj *syncobj, int *p_fd); · line 152

Quoted 5 lines
include/drm/drm_syncobj.h +1 -0
@@ -148,4 +148,5 @@ int drm_syncobj_get_fd(struct drm_syncobj *syncobj, int *p_fd);
  int drm_syncobj_search_fence(struct drm_syncobj *syncobj, u64 point, u64 flags,
  			     struct dma_fence **fence);
 
+void drm_syncobj_stub_fence_init(void);
  #endif

Move it to drm_internal.h ? This is not for driver consumption. :)

Quoted 5 lines

i would split up the changes at this point:

1. Replace memory allocation with a static fence, not necessarily changing any locking, add a fixes tag to this commit.

2. Move locking.

No necessary, why change it?

Quoted 2 lines

3. Fix drm_syncobj_fence_get_or_add_callback to return an error.

why does it still need a return value? Chris and I are trying to avoid that.

Thanks,
David

Quoted 5 lines

That way it should be nicely bisectable.

~Maarten

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

#5 lore raw

Op 25-10-18 om 14:01 schreef Chunming Zhou:

Quoted 159 lines · Maarten Lankhorst
Quoted 157 lines

Op 25-10-18 om 12:21 schreef Chunming Zhou:

Quoted 47 lines

drivers/gpu/drm/drm_syncobj.c:202:4-14: ERROR: function drm_syncobj_find_signal_pt_for_point called on line 390 inside lock on line 389 but uses GFP_KERNEL

Find functions that refer to GFP_KERNEL but are called with locks held.

Generated by: scripts/coccinelle/locks/call_kern.cocci

v2:
syncobj->timeline still needs protect.

v3:
use a global signaled fence instead of re-allocation.

Cc Maarten Lankhorst <[email protected]>
Cc Christian König <[email protected]>
---
  drivers/gpu/drm/drm_drv.c     |  2 ++
  drivers/gpu/drm/drm_syncobj.c | 52 +++++++++++++++++++++--------------
  include/drm/drm_syncobj.h     |  1 +
  3 files changed, 34 insertions(+), 21 deletions(-)
drivers/gpu/drm/drm_drv.c +2 -0
index 36e8e9cbec52..0a6f1023d6c3 100644
@@ -37,6 +37,7 @@
  #include <drm/drm_client.h>
  #include <drm/drm_drv.h>
  #include <drm/drmP.h>
+#include <drm/drm_syncobj.h>
 
  #include "drm_crtc_internal.h"
  #include "drm_legacy.h"
@@ -1003,6 +1004,7 @@ static int __init drm_core_init(void)
  	if (ret < 0)
  		goto error;
 
+	drm_syncobj_stub_fence_init();
  	drm_core_init_complete = true;
 
  	DRM_DEBUG("Initialized\n");
drivers/gpu/drm/drm_syncobj.c +0 -0
index b7eaa603f368..6b3f5a06e4d3 100644

drivers/gpu/drm/drm_syncobj.c struct drm_syncobj_signal_pt { · line 88

Quoted 9 lines
drivers/gpu/drm/drm_syncobj.c +6 -0
@@ -80,6 +80,27 @@ struct drm_syncobj_signal_pt {
  	struct list_head list;
  };
 
+static struct drm_syncobj_stub_fence stub_signaled_fence;
+static void global_stub_fence_release(struct dma_fence *fence)
+{
+	/* it is impossible to come here */
+	BUG();
+}

WARN_ON_ONCE(1)? No need to halt the machine.

Quoted 90 lines
drivers/gpu/drm/drm_syncobj.c +25 -21
+static const struct dma_fence_ops global_stub_fence_ops = {
+	.get_driver_name = drm_syncobj_stub_fence_get_name,
+	.get_timeline_name = drm_syncobj_stub_fence_get_name,
+	.release = global_stub_fence_release,
+};
+
+void drm_syncobj_stub_fence_init(void)
+{
+	spin_lock_init(&stub_signaled_fence.lock);
+	dma_fence_init(&stub_signaled_fence.base,
+		       &global_stub_fence_ops,
+		       &stub_signaled_fence.lock,
+		       0, 0);
+	dma_fence_signal(&stub_signaled_fence.base);
+}
  /**
   * drm_syncobj_find - lookup and reference a sync object.
   * @file_private: drm file private pointer
@@ -111,24 +132,14 @@ static struct dma_fence
  				      uint64_t point)
  {
  	struct drm_syncobj_signal_pt *signal_pt;
+	struct dma_fence *f = NULL;
 
+	spin_lock(&syncobj->pt_lock);
  	if ((syncobj->type == DRM_SYNCOBJ_TYPE_TIMELINE) &&
  	    (point <= syncobj->timeline)) {
-		struct drm_syncobj_stub_fence *fence =
-			kzalloc(sizeof(struct drm_syncobj_stub_fence),
-				GFP_KERNEL);
-
-		if (!fence)
-			return NULL;
-		spin_lock_init(&fence->lock);
-		dma_fence_init(&fence->base,
-			       &drm_syncobj_stub_fence_ops,
-			       &fence->lock,
-			       syncobj->timeline_context,
-			       point);
-
-		dma_fence_signal(&fence->base);
-		return &fence->base;
+		dma_fence_get(&stub_signaled_fence.base);
+		spin_unlock(&syncobj->pt_lock);
+		return &stub_signaled_fence.base;
  	}
 
  	list_for_each_entry(signal_pt, &syncobj->signal_pt_list, list) {
@@ -137,9 +148,12 @@ static struct dma_fence
  		if ((syncobj->type == DRM_SYNCOBJ_TYPE_BINARY) &&
  		    (point != signal_pt->value))
  			continue;
-		return dma_fence_get(&signal_pt->fence_array->base);
+		f = dma_fence_get(&signal_pt->fence_array->base);
+		break;
  	}
-	return NULL;
+	spin_unlock(&syncobj->pt_lock);
+
+	return f;
  }
 
  static void drm_syncobj_add_callback_locked(struct drm_syncobj *syncobj,
@@ -166,9 +180,7 @@ static void drm_syncobj_fence_get_or_add_callback(struct drm_syncobj *syncobj,
  	}
 
  	mutex_lock(&syncobj->cb_mutex);
-	spin_lock(&syncobj->pt_lock);
  	*fence = drm_syncobj_find_signal_pt_for_point(syncobj, pt_value);
-	spin_unlock(&syncobj->pt_lock);
  	if (!*fence)
  		drm_syncobj_add_callback_locked(syncobj, cb, func);
  	mutex_unlock(&syncobj->cb_mutex);
@@ -379,11 +391,9 @@ drm_syncobj_point_get(struct drm_syncobj *syncobj, u64 point, u64 flags,
  		if (ret < 0)
  			return ret;
  	}
-	spin_lock(&syncobj->pt_lock);
  	*fence = drm_syncobj_find_signal_pt_for_point(syncobj, point);
  	if (!*fence)
  		ret = -EINVAL;
-	spin_unlock(&syncobj->pt_lock);
  	return ret;
  }
 
include/drm/drm_syncobj.h +0 -0
index 29244cbcd05e..63cfd1540241 100644

include/drm/drm_syncobj.h int drm_syncobj_get_fd(struct drm_syncobj *syncobj, int *p_fd); · line 152

Quoted 5 lines
include/drm/drm_syncobj.h +1 -0
@@ -148,4 +148,5 @@ int drm_syncobj_get_fd(struct drm_syncobj *syncobj, int *p_fd);
  int drm_syncobj_search_fence(struct drm_syncobj *syncobj, u64 point, u64 flags,
  			     struct dma_fence **fence);
 
+void drm_syncobj_stub_fence_init(void);
  #endif

Move it to drm_internal.h ? This is not for driver consumption. :) i would split up the changes at this point:

Quoted 7 lines
Quoted 3 lines

1. Replace memory allocation with a static fence, not necessarily changing any locking, add a fixes tag to this commit.

2. Move locking.

No necessary, why change it?

Quoted 1 line

3. Fix drm_syncobj_fence_get_or_add_callback to return an error.

why does it still need a return value? Chris and I are trying to avoid that.

I just need a clarification on why it's ok to ignore to return fence not found. Could be a comment in the code as well. :)
_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel

#6 lore raw

✓ Fi.CI.BAT: success for drm: fix call_kern.cocci warnings (rev3)

In reply to Chunming Zhou
== Series Details ==

== Series Details ==

Series: drm: fix call_kern.cocci warnings (rev3)
URL : https://patchwork.freedesktop.org/series/51498/
State : success

== Summary ==

= CI Bug Log - changes from CI_DRM_5033 -> Patchwork_10576 =

== Summary - WARNING ==

Minor unknown changes coming with Patchwork_10576 need to be verified manually.

If you think the reported changes have nothing to do with the changes introduced in Patchwork_10576, please notify your bug team to allow them to document this new failure mode, which will reduce false positives in CI.

External URL: https://patchwork.freedesktop.org/api/1.0/series/51498/revisions/3/mbox/

== Possible new issues ==

Here are the unknown changes that may have been introduced in Patchwork_10576:

=== IGT changes ===

    ==== Warnings ====

    igt@pm_rpm@module-reload:
      fi-hsw-4770r:       SKIP -> PASS

== Known issues ==

Here are the changes found in Patchwork_10576 that come from known issues:

=== IGT changes ===

    ==== Issues hit ====

    igt@drv_module_reload@basic-reload:
      fi-glk-j4005:       PASS -> DMESG-WARN (fdo#106725, fdo#106248)

    igt@gem_exec_suspend@basic-s4-devices:
      fi-blb-e6850:       PASS -> INCOMPLETE (fdo#107718)

    igt@kms_flip@basic-flip-vs-dpms:
      fi-glk-j4005:       PASS -> DMESG-WARN (fdo#106000)

    igt@kms_flip@basic-flip-vs-wf_vblank:
      fi-glk-j4005:       PASS -> FAIL (fdo#100368)

    igt@kms_flip@basic-plain-flip:
      fi-glk-j4005:       PASS -> DMESG-WARN (fdo#106097)

    igt@kms_frontbuffer_tracking@basic:
      fi-byt-clapper:     PASS -> FAIL (fdo#103167)

    igt@kms_pipe_crc_basic@nonblocking-crc-pipe-b-frame-sequence:
      fi-byt-clapper:     PASS -> FAIL (fdo#103191, fdo#107362)

    igt@pm_rpm@module-reload:
      fi-glk-j4005:       PASS -> DMESG-WARN (fdo#107726, fdo#106097)

    
    ==== Possible fixes ====

    igt@drv_module_reload@basic-reload-inject:
      fi-hsw-4770r:       DMESG-WARN (fdo#107924, fdo#107425) -> PASS

    igt@drv_selftest@live_hangcheck:
      fi-kbl-7560u:       INCOMPLETE (fdo#108044) -> PASS

    igt@kms_flip@basic-flip-vs-dpms:
      fi-skl-6700hq:      DMESG-WARN (fdo#105998) -> PASS

fdo#100368 https://bugs.freedesktop.org/show_bug.cgi?id=100368
fdo#103167 https://bugs.freedesktop.org/show_bug.cgi?id=103167
fdo#103191 https://bugs.freedesktop.org/show_bug.cgi?id=103191
fdo#105998 https://bugs.freedesktop.org/show_bug.cgi?id=105998
fdo#106000 https://bugs.freedesktop.org/show_bug.cgi?id=106000
fdo#106097 https://bugs.freedesktop.org/show_bug.cgi?id=106097
fdo#106248 https://bugs.freedesktop.org/show_bug.cgi?id=106248
fdo#106725 https://bugs.freedesktop.org/show_bug.cgi?id=106725
fdo#107362 https://bugs.freedesktop.org/show_bug.cgi?id=107362
fdo#107425 https://bugs.freedesktop.org/show_bug.cgi?id=107425
fdo#107718 https://bugs.freedesktop.org/show_bug.cgi?id=107718
fdo#107726 https://bugs.freedesktop.org/show_bug.cgi?id=107726
fdo#107924 https://bugs.freedesktop.org/show_bug.cgi?id=107924
fdo#108044 https://bugs.freedesktop.org/show_bug.cgi?id=108044

== Participating hosts (47 -> 42) ==

Missing (5): fi-ilk-m540 fi-byt-squawks fi-bsw-cyan fi-ctg-p8600 fi-icl-u

== Build changes ==

* Linux: CI_DRM_5033 -> Patchwork_10576

CI_DRM_5033: f935e4c7634781e6ffef10bb8a1c93225ac42d90 @ git://anongit.freedesktop.org/gfx-ci/linux
IGT_4691: d445be01f5edc7e7a324444c73e221c9ed75602e @ git://anongit.freedesktop.org/xorg/app/intel-gpu-tools
Patchwork_10576: 2fa016495d372e97e2673442bdef79e88183b3e9 @ git://anongit.freedesktop.org/gfx-ci/linux

== Linux commits ==

2fa016495d37 drm: fix call_kern.cocci warnings v3

== Logs ==

For more details see: https://intel-gfx-ci.01.org/tree/drm-tip/Patchwork_10576/issues.html
_______________________________________________
Intel-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/intel-gfx

#7 lore raw
In reply to Chunming Zhou

Give this a nice summary,

drm/syncobj: Avoid kmalloc(GFP_KERNEL) under spinlock

Quoting Chunming Zhou (2018-10-25 11:21:05)

Quoted 4 lines

drivers/gpu/drm/drm_syncobj.c:202:4-14: ERROR: function drm_syncobj_find_signal_pt_for_point called on line 390 inside lock on line 389 but uses GFP_KERNEL

Find functions that refer to GFP_KERNEL but are called with locks held.

Quoted 8 lines

Generated by: scripts/coccinelle/locks/call_kern.cocci

v2:
syncobj->timeline still needs protect.

v3:
use a global signaled fence instead of re-allocation.

Good practice, would be to add Testcase: (and as penance for the bug, write it ;)

Quoted 35 lines
Cc Maarten Lankhorst <[email protected]>
Cc Christian König <[email protected]>
---
 drivers/gpu/drm/drm_drv.c     |  2 ++
 drivers/gpu/drm/drm_syncobj.c | 52 +++++++++++++++++++++--------------
 include/drm/drm_syncobj.h     |  1 +
 3 files changed, 34 insertions(+), 21 deletions(-)
drivers/gpu/drm/drm_drv.c +2 -0
index 36e8e9cbec52..0a6f1023d6c3 100644
@@ -37,6 +37,7 @@
 #include <drm/drm_client.h>
 #include <drm/drm_drv.h>
 #include <drm/drmP.h>
+#include <drm/drm_syncobj.h>
 
 #include "drm_crtc_internal.h"
 #include "drm_legacy.h"
@@ -1003,6 +1004,7 @@ static int __init drm_core_init(void)
        if (ret < 0)
                goto error;
 
+       drm_syncobj_stub_fence_init();
        drm_core_init_complete = true;
 
        DRM_DEBUG("Initialized\n");
drivers/gpu/drm/drm_syncobj.c +0 -0
index b7eaa603f368..6b3f5a06e4d3 100644

drivers/gpu/drm/drm_syncobj.c struct drm_syncobj_signal_pt { · line 87

Quoted 8 lines
drivers/gpu/drm/drm_syncobj.c +5 -0
@@ -80,6 +80,27 @@ struct drm_syncobj_signal_pt {
        struct list_head list;
 };
 
+static struct drm_syncobj_stub_fence stub_signaled_fence;
+static void global_stub_fence_release(struct dma_fence *fence)
+{
+       /* it is impossible to come here */
+       BUG();

BUG() is overkill, kasan will complain for us anyway, so we can just use the default release function.

drivers/gpu/drm/drm_syncobj.c struct drm_syncobj_signal_pt { · line 95

Quoted 9 lines
drivers/gpu/drm/drm_syncobj.c +8 -0
+}
+static const struct dma_fence_ops global_stub_fence_ops = {
+       .get_driver_name = drm_syncobj_stub_fence_get_name,
+       .get_timeline_name = drm_syncobj_stub_fence_get_name,
+       .release = global_stub_fence_release,
+};
+
 
+void drm_syncobj_stub_fence_init(void)

I think we can avoid having this exposed by:

static DECLARE_SPINLOCK(signaled_fence_lock);
static dma_fence signaled_fence;

static struct dma_fence *signaled_fence_get(void)

{
	spin_lock(&signaled_fenced_lock);
	if (!signaled_fence.ops) {
		dma_fence_init(&signaled_fence,
			       &signaled_fence_ops,
			       &signaled_fence_lock,
			       0, 0);
		dma_fence_signal_locked(&signaled_fence.base);
	}
	spin_unlock(&signaled_fenced_lock);

	return dma_fence_get(&signaled_fence);
}
Quoted 20 lines
drivers/gpu/drm/drm_syncobj.c +2 -8
 /**
  * drm_syncobj_find - lookup and reference a sync object.
  * @file_private: drm file private pointer
@@ -111,24 +132,14 @@ static struct dma_fence
                                      uint64_t point)
 {
        struct drm_syncobj_signal_pt *signal_pt;
+       struct dma_fence *f = NULL;
 
+       spin_lock(&syncobj->pt_lock);
        if ((syncobj->type == DRM_SYNCOBJ_TYPE_TIMELINE) &&
            (point <= syncobj->timeline)) {
-               struct drm_syncobj_stub_fence *fence =
-                       kzalloc(sizeof(struct drm_syncobj_stub_fence),
-                               GFP_KERNEL);
-
-               if (!fence)
-                       return NULL;
-               spin_lock_init(&fence->lock);
-               dma_fence_init(&fence->base,

drivers/gpu/drm/drm_syncobj.c static struct dma_fence · line 142

Quoted 10 lines
drivers/gpu/drm/drm_syncobj.c +3 -7
-                              &drm_syncobj_stub_fence_ops,
-                              &fence->lock,
-                              syncobj->timeline_context,
-                              point);
-
-               dma_fence_signal(&fence->base);
-               return &fence->base;
+               dma_fence_get(&stub_signaled_fence.base);
+               spin_unlock(&syncobj->pt_lock);
+               return &stub_signaled_fence.base;

f = signaled_fence_get();
goto unlock;

drivers/gpu/drm/drm_syncobj.c static struct dma_fence · line 142

Quoted 12 lines
drivers/gpu/drm/drm_syncobj.c +2 -2
        }
 
        list_for_each_entry(signal_pt, &syncobj->signal_pt_list, list) {
@@ -137,9 +148,12 @@ static struct dma_fence
                if ((syncobj->type == DRM_SYNCOBJ_TYPE_BINARY) &&
                    (point != signal_pt->value))
                        continue;
-               return dma_fence_get(&signal_pt->fence_array->base);
+               f = dma_fence_get(&signal_pt->fence_array->base);
+               break;
        }
-       return NULL;

unlock:

drivers/gpu/drm/drm_syncobj.c static struct dma_fence · line 157

Quoted 4 lines
drivers/gpu/drm/drm_syncobj.c +3 -0
+       spin_unlock(&syncobj->pt_lock);
+
+       return f;
 }

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