@@ -140,7 +140,7 @@ describe_obj(struct seq_file *m, struct drm_i915_gem_object *obj)
obj->madv == I915_MADV_DONTNEED ? " purgeable" : "");
if (obj->base.name)
seq_printf(m, " (name: %d)", obj->base.name);
- list_for_each_entry(vma, &obj->vma_list, obj_link) {
+ i915_gem_object_for_each_vma(vma, obj) {
if (vma->pin_count > 0)
pin_count++;
}
@@ -51,6 +51,7 @@
#include <linux/backlight.h>
#include <linux/hash.h>
#include <linux/intel-iommu.h>
+#include <linux/list_check.h>
#include <linux/kref.h>
#include <linux/pm_qos.h>
#include "intel_guc.h"
@@ -3565,4 +3566,7 @@ int remap_io_mapping(struct vm_area_struct *vma,
unsigned long addr, unsigned long pfn, unsigned long size,
struct io_mapping *iomap);
+#define i915_gem_object_for_each_vma(vma, obj) \
+ list_for_each_entry_check(vma, &(obj)->vma_list, obj_link, &(obj)->base.dev->struct_mutex)
+
#endif
@@ -30,6 +30,7 @@
#include <linux/types.h>
#include <linux/idr.h>
#include <linux/fb.h>
+#include <linux/list_check.h>
#include <linux/hdmi.h>
#include <linux/media-bus-format.h>
#include <uapi/drm/drm_mode.h>
@@ -2541,7 +2542,7 @@ static inline struct drm_property *drm_property_find(struct drm_device *dev,
#define drm_for_each_crtc(crtc, dev) \
list_for_each_entry(crtc, &(dev)->mode_config.crtc_list, head)
-static inline void
+static inline int
assert_drm_connector_list_read_locked(struct drm_mode_config *mode_config)
{
/*
@@ -2553,23 +2554,21 @@ assert_drm_connector_list_read_locked(struct drm_mode_config *mode_config)
*/
WARN_ON(!mutex_is_locked(&mode_config->mutex) &&
!drm_modeset_is_locked(&mode_config->connection_mutex));
+
+ return 1;
}
#define drm_for_each_connector(connector, dev) \
- for (assert_drm_connector_list_read_locked(&(dev)->mode_config), \
- connector = list_first_entry(&(dev)->mode_config.connector_list, \
- struct drm_connector, head); \
- &connector->head != (&(dev)->mode_config.connector_list); \
- connector = list_next_entry(connector, head))
+ __list_for_each_entry_check(connector, \
+ &(dev)->mode_config.connector_list, \
+ head, \
+ assert_drm_connector_list_read_locked(&(dev)->mode_config))
#define drm_for_each_encoder(encoder, dev) \
list_for_each_entry(encoder, &(dev)->mode_config.encoder_list, head)
#define drm_for_each_fb(fb, dev) \
- for (WARN_ON(!mutex_is_locked(&(dev)->mode_config.fb_lock)), \
- fb = list_first_entry(&(dev)->mode_config.fb_list, \
- struct drm_framebuffer, head); \
- &fb->head != (&(dev)->mode_config.fb_list); \
- fb = list_next_entry(fb, head))
+ list_for_each_entry_check(fb, &(dev)->mode_config.fb_list, head, \
+ &(dev)->mode_config.fb_lock)
#endif /* __DRM_CRTC_H__ */
new file mode 100644
@@ -0,0 +1,62 @@
+#ifndef _LINUX_LIST_CHECK_H
+#define _LINUX_LIST_CHECK_H
+
+#include <linux/bug.h>
+#include <linux/lockdep.h>
+#include <linux/mutex.h>
+#include <linux/spinlock.h>
+
+/**
+ * __list_for_each_entry_check - iterate over list of given type
+ * @pos: the type * to use as a loop cursor.
+ * @head: the head for your list.
+ * @member: the name of the list_head within the struct.
+ * @expr: the expression to call before iterating
+ *
+ * Before iterating over the list, see list_for_each_entry(), evaluate @expr.
+ */
+#define __list_for_each_entry_check(pos, head, member, expr) \
+ for (expr, pos = list_first_entry(head, typeof(*pos), member); \
+ &pos->member != (head); \
+ pos = list_next_entry(pos, member))
+
+static __always_inline void __list_check_mutex(struct mutex *lock)
+{
+ lockdep_assert_held(lock);
+}
+
+static __always_inline void __list_check_spin(struct spinlock *lock)
+{
+ assert_spin_locked(lock);
+}
+
+static __always_inline void __list_check_bug(void *_lock)
+{
+ BUILD_BUG_ON("unknown lock type");
+}
+
+#define __list_check(lock) \
+ ({ __builtin_types_compatible_p(typeof(*lock), struct mutex) ? \
+ __list_check_mutex((struct mutex *)lock) : \
+ __builtin_types_compatible_p(typeof(*lock), spinlock_t) ? \
+ __list_check_spin((spinlock_t *)lock) : \
+ __list_check_bug(lock); \
+ 1; })
+
+/**
+ * list_for_each_entry_check - iterate over list of given type
+ * @pos: the type * to use as a loop cursor.
+ * @head: the head for your list.
+ * @member: the name of the list_head within the struct.
+ * @lock: the lock guarding the list
+ *
+ * Before iterating over the list, see list_for_each_entry(), first
+ * verify that the @lock is held, either using lockdep_assert_held() for
+ * mutexes or assert_spin_locked() for spinlocks. The purpose of this macro
+ * is two-fold: one to document the locks guarding the list, and secondly
+ * to verify that invariant is held at runtime.
+ */
+#define list_for_each_entry_check(pos, head, member, lock) \
+ __list_for_each_entry_check(pos, head, member, __list_check(lock))
+
+#endif