Skip to contentSkip to conversation

unifying the vma offset managers

PATCH In review Dave Airlie 3 participants 6 messages Dec 19, 2012

Subsystems (13)
lore
Share & tools
lore

Dave Airlie

author maintainer
#0 lore raw

So a passing comment on irc from Daniel made me look at this, and cleaning up some surrounding things. This unifies the GEM/TTM vma offset managers around a single one based on the TTM one.

There is also a patch to cleanup the GEM code after this, and one to clean up some bits of TTM also.

I've tested it on an intel and a radeon machine and it appears to at least boot.

Dave.

author maintainer
#1 lore raw

PATCH 1/3 drm: create unified vma offset manager

From: Dave Airlie <[email protected]>

So we have to offset manager implementations for dealing with VMA offsets. GEM had one using the hash table, TTM had one using an rbtree,

I'd rather we just had one to rule them all, since ttm is using the rbtree variant to allow sub mappings, to keep ABI we need to use that one.

This also adds a bunch of inline helpers to avoid gem/ttm poking around inside the vma_offset objects. TTM needs a reset helper to remove a vma_offset when it copies an object to another one for buffer moves. The helpers also let drivers avoid the map_list.hash_key << PAGE_SHIFT nonsense.

TODO: check interactions with ttm vm_lock.

I've tested booted this on Intel Ironlake and AMD REDWOOD.

---
 drivers/gpu/drm/Makefile                |   2 +-
 drivers/gpu/drm/drm_gem.c               |  54 +++---------
 drivers/gpu/drm/drm_gem_cma_helper.c    |   4 +-
 drivers/gpu/drm/drm_vma_offset_man.c    | 149 ++++++++++++++++++++++++++++++++
 drivers/gpu/drm/exynos/exynos_drm_gem.c |   6 +-
 drivers/gpu/drm/gma500/gem.c            |   6 +-
 drivers/gpu/drm/i915/i915_gem.c         |   8 +-
 drivers/gpu/drm/ttm/ttm_bo.c            |  74 +++-------------
 drivers/gpu/drm/ttm/ttm_bo_util.c       |   2 +-
 drivers/gpu/drm/ttm/ttm_bo_vm.c         |  86 ++++++++----------
 drivers/gpu/drm/udl/udl_gem.c           |   6 +-
 include/drm/drmP.h                      |   8 +-
 include/drm/drm_vma_offset_man.h        |  58 +++++++++++++
 include/drm/ttm/ttm_bo_api.h            |  10 +--
 include/drm/ttm/ttm_bo_driver.h         |   3 +-
 15 files changed, 291 insertions(+), 185 deletions(-)
 create mode 100644 drivers/gpu/drm/drm_vma_offset_man.c
 create mode 100644 include/drm/drm_vma_offset_man.h
drivers/gpu/drm/Makefile +1 -1
index 6f58c81..20a1eeb 100644
@@ -12,7 +12,7 @@ drm-y       :=	drm_auth.o drm_buffer.o drm_bufs.o drm_cache.o \
 		drm_platform.o drm_sysfs.o drm_hashtab.o drm_mm.o \
 		drm_crtc.o drm_modes.o drm_edid.o \
 		drm_info.o drm_debugfs.o drm_encoder_slave.o \
-		drm_trace_points.o drm_global.o drm_prime.o
+		drm_trace_points.o drm_global.o drm_prime.o drm_vma_offset_man.o
 
 drm-$(CONFIG_COMPAT) += drm_ioc32.o
 drm-$(CONFIG_DRM_GEM_CMA_HELPER) += drm_gem_cma_helper.o
drivers/gpu/drm/drm_gem.c +14 -40

101 lines not on this page · raw message

drivers/gpu/drm/drm_gem_cma_helper.c +2 -2
index 1aa8fee..43b485f 100644
@@ -29,7 +29,7 @@
 
 static unsigned int get_gem_mmap_offset(struct drm_gem_object *obj)
 {
-	return (unsigned int)obj->map_list.hash.key << PAGE_SHIFT;
+	return (unsigned int)drm_vma_node_offset_addr(&obj->map_list.vma_offset);
 }
 
 static void drm_gem_cma_buf_destroy(struct drm_device *drm,
@@ -140,7 +140,7 @@ void drm_gem_cma_free_object(struct drm_gem_object *gem_obj)
 {
 	struct drm_gem_cma_object *cma_obj;
 
-	if (gem_obj->map_list.map)
+	if (drm_vma_node_is_allocated(&obj->map_list.vma_offset))
 		drm_gem_free_mmap_offset(gem_obj);
 
 	drm_gem_object_release(gem_obj);
drivers/gpu/drm/drm_vma_offset_man.c new file +149 -0

149 lines not on this page · raw message

drivers/gpu/drm/exynos/exynos_drm_gem.c +3 -3
index d48183e..5389bfb 100644
@@ -168,7 +168,7 @@ out:
 	exynos_drm_fini_buf(obj->dev, buf);
 	exynos_gem_obj->buffer = NULL;
 
-	if (obj->map_list.map)
+	if (drm_vma_node_is_allocated(&obj->map_list.vma_offset))
 		drm_gem_free_mmap_offset(obj);
 
 	/* release file pointer to gem object. */
@@ -704,13 +704,13 @@ int exynos_drm_gem_dumb_map_offset(struct drm_file *file_priv,
 		goto unlock;
 	}
 
-	if (!obj->map_list.map) {
+	if (!drm_vma_node_is_allocated(&obj->map_list.vma_offset)) {
 		ret = drm_gem_create_mmap_offset(obj);
 		if (ret)
 			goto out;
 	}
 
-	*offset = (u64)obj->map_list.hash.key << PAGE_SHIFT;
+	*offset = drm_vma_node_offset_addr(&obj->map_list.vma_offset);
 	DRM_DEBUG_KMS("offset = 0x%lx\n", (unsigned long)*offset);
 
 out:
drivers/gpu/drm/gma500/gem.c +3 -3
index eefd6cc..1c49fbc 100644
@@ -38,7 +38,7 @@ void psb_gem_free_object(struct drm_gem_object *obj)
 	struct gtt_range *gtt = container_of(obj, struct gtt_range, gem);
 
 	/* Remove the list map if one is present */
-	if (obj->map_list.map)
+	if (drm_vma_node_is_allocated(&obj->map_list.vma_offset))
 		drm_gem_free_mmap_offset(obj);
 	drm_gem_object_release(obj);
 
@@ -81,13 +81,13 @@ int psb_gem_dumb_map_gtt(struct drm_file *file, struct drm_device *dev,
 	/* What validation is needed here ? */
 
 	/* Make it mmapable */
-	if (!obj->map_list.map) {
+	if (!drm_vma_node_is_allocated(&obj->map_list.vma_offset)) {
 		ret = drm_gem_create_mmap_offset(obj);
 		if (ret)
 			goto out;
 	}
 	/* GEM should really work out the hash offsets for us */
-	*offset = (u64)obj->map_list.hash.key << PAGE_SHIFT;
+	*offset = drm_vma_node_offset_addr(&obj->map_list.vma_offset);
 out:
 	drm_gem_object_unreference(obj);
 unlock:
drivers/gpu/drm/i915/i915_gem.c +4 -4
index c1f6919..75138cb 100644
@@ -1426,7 +1426,7 @@ i915_gem_release_mmap(struct drm_i915_gem_object *obj)
 
 	if (obj->base.dev->dev_mapping)
 		unmap_mapping_range(obj->base.dev->dev_mapping,
-				    (loff_t)obj->base.map_list.hash.key<<PAGE_SHIFT,
+				    (loff_t)drm_vma_node_offset_addr(&obj->base.map_list.vma_offset),
 				    obj->base.size, 1);
 
 	obj->fault_mappable = false;
@@ -1514,7 +1514,7 @@ static int i915_gem_object_create_mmap_offset(struct drm_i915_gem_object *obj)
 	struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
 	int ret;
 
-	if (obj->base.map_list.map)
+	if (drm_vma_node_is_allocated(&obj->base.map_list.vma_offset))
 		return 0;
 
 	ret = drm_gem_create_mmap_offset(&obj->base);
@@ -1539,7 +1539,7 @@ static int i915_gem_object_create_mmap_offset(struct drm_i915_gem_object *obj)
 
 static void i915_gem_object_free_mmap_offset(struct drm_i915_gem_object *obj)
 {
-	if (!obj->base.map_list.map)
+	if (!drm_vma_node_is_allocated(&obj->base.map_list.vma_offset))
 		return;
 
 	drm_gem_free_mmap_offset(&obj->base);
@@ -1580,7 +1580,7 @@ i915_gem_mmap_gtt(struct drm_file *file,
 	if (ret)
 		goto out;
 
-	*offset = (u64)obj->base.map_list.hash.key << PAGE_SHIFT;
+	*offset = drm_vma_node_offset_addr(&obj->base.map_list.vma_offset);
 
 out:
 	drm_gem_object_unreference(&obj->base);
drivers/gpu/drm/ttm/ttm_bo.c +10 -64

108 lines not on this page · raw message

drivers/gpu/drm/ttm/ttm_bo_util.c +1 -1
index 9e9c5d2..52d9750 100644
@@ -438,7 +438,7 @@ static int ttm_buffer_object_transfer(struct ttm_buffer_object *bo,
 	INIT_LIST_HEAD(&fbo->lru);
 	INIT_LIST_HEAD(&fbo->swap);
 	INIT_LIST_HEAD(&fbo->io_reserve_lru);
-	fbo->vm_node = NULL;
+	drm_vma_node_reset(&fbo->vma_offset);
 	atomic_set(&fbo->cpu_writers, 0);
 
 	fbo->sync_obj = driver->sync_obj_ref(bo->sync_obj);
drivers/gpu/drm/ttm/ttm_bo_vm.c +35 -51

124 lines not on this page · raw message

drivers/gpu/drm/udl/udl_gem.c +3 -3
index afd212c..2cc79d2 100644
@@ -223,7 +223,7 @@ void udl_gem_free_object(struct drm_gem_object *gem_obj)
 	if (obj->pages)
 		udl_gem_put_pages(obj);
 
-	if (gem_obj->map_list.map)
+	if (drm_vma_node_is_allocated(&gem_obj->map_list.vma_offset))
 		drm_gem_free_mmap_offset(gem_obj);
 }
 
@@ -247,13 +247,13 @@ int udl_gem_mmap(struct drm_file *file, struct drm_device *dev,
 	ret = udl_gem_get_pages(gobj, GFP_KERNEL);
 	if (ret)
 		goto out;
-	if (!gobj->base.map_list.map) {
+	if (!drm_vma_node_is_allocated(&gobj->base.map_list.vma_offset)) {
 		ret = drm_gem_create_mmap_offset(obj);
 		if (ret)
 			goto out;
 	}
 
-	*offset = (u64)gobj->base.map_list.hash.key << PAGE_SHIFT;
+	*offset = drm_vma_node_offset_addr(&obj->map_list.vma_offset);
 
 out:
 	drm_gem_object_unreference(&gobj->base);
include/drm/drmP.h +5 -3
index fad21c9..963e00e 100644
@@ -88,6 +88,8 @@ struct drm_device;
 #include <drm/drm_os_linux.h>
 #include <drm/drm_hashtab.h>
 #include <drm/drm_mm.h>
+#include <drm/drm_vma_offset_man.h>
+
 
 #define DRM_UT_CORE 		0x01
 #define DRM_UT_DRIVER		0x02
@@ -578,7 +580,7 @@ struct drm_map_list {
 	struct drm_local_map *map;	/**< mapping */
 	uint64_t user_token;
 	struct drm_master *master;
-	struct drm_mm_node *file_offset_node;	/**< fake offset */
+	struct drm_vma_offset_node vma_offset;
 };
 
 /**
@@ -613,8 +615,7 @@ struct drm_ati_pcigart_info {
  * GEM specific mm private for tracking GEM objects
  */
 struct drm_gem_mm {
-	struct drm_mm offset_manager;	/**< Offset mgmt for buffer objects */
-	struct drm_open_hash offset_hash; /**< User token hash table for maps */
+	struct drm_vma_offset_manager vma_manager;
 };
 
 /**
@@ -1198,6 +1199,7 @@ struct drm_device {
 	int switch_power_state;
 
 	atomic_t unplugged; /* device has been unplugged or gone away */
+
 };
 
 #define DRM_SWITCH_POWER_ON 0
include/drm/drm_vma_offset_man.h new file +58 -0

58 lines not on this page · raw message

include/drm/ttm/ttm_bo_api.h +2 -8
index 3cb5d84..fd2020f 100644
@@ -32,12 +32,12 @@
 #define _TTM_BO_API_H_
 
 #include <drm/drm_hashtab.h>
+#include <drm/drm_vma_offset_man.h>
 #include <linux/kref.h>
 #include <linux/list.h>
 #include <linux/wait.h>
 #include <linux/mutex.h>
 #include <linux/mm.h>
-#include <linux/rbtree.h>
 #include <linux/bitmap.h>
 
 struct ttm_bo_device;
@@ -254,13 +254,7 @@ struct ttm_buffer_object {
 	void *sync_obj;
 	unsigned long priv_flags;
 
-	/**
-	 * Members protected by the bdev::vm_lock
-	 */
-
-	struct rb_node vm_rb;
-	struct drm_mm_node *vm_node;
-
+	struct drm_vma_offset_node vma_offset;
 
 	/**
 	 * Special members that are protected by the reserve lock
include/drm/ttm/ttm_bo_driver.h +1 -2
index e3a43a4..77afa32 100644
@@ -541,8 +541,7 @@ struct ttm_bo_device {
 	/*
 	 * Protected by the vm lock.
 	 */
-	struct rb_root addr_space_rb;
-	struct drm_mm addr_space_mm;
+	struct drm_vma_offset_manager offset_manager;
 
 	/*
 	 * Protected by the global:lru lock.
Signature · 1 line
1.8.0.2
author maintainer
#4 lore raw

PATCH 2/3 drm/gem: drop maplist from gem objects.

In reply to Dave Airlie

From: Dave Airlie <[email protected]>

We currently don't need map_lists to store this information, with the new encapsulation, just move the vma_offset object into the gem object.

In the future I'd guess we need per-filp vma offsets so this might make it a bit cleaner to start from.

---
 drivers/gpu/drm/drm_gem.c               | 44 ++++-----------------------------
 drivers/gpu/drm/drm_gem_cma_helper.c    |  4 +--
 drivers/gpu/drm/exynos/exynos_drm_gem.c |  6 ++---
 drivers/gpu/drm/gma500/gem.c            |  6 ++---
 drivers/gpu/drm/i915/i915_gem.c         |  8 +++---
 drivers/gpu/drm/udl/udl_gem.c           |  6 ++---
 include/drm/drmP.h                      |  3 +--
 7 files changed, 21 insertions(+), 56 deletions(-)
drivers/gpu/drm/drm_gem.c +5 -39
index 551352f..bb5ac23 100644
@@ -304,11 +304,8 @@ drm_gem_free_mmap_offset(struct drm_gem_object *obj)
 {
 	struct drm_device *dev = obj->dev;
 	struct drm_gem_mm *mm = dev->mm_private;
-	struct drm_map_list *list = &obj->map_list;
 
-	drm_vma_offset_destroy(&mm->vma_manager, &list->vma_offset);
-	kfree(list->map);
-	list->map = NULL;
+	drm_vma_offset_destroy(&mm->vma_manager, &obj->vma_offset);
 }
 EXPORT_SYMBOL(drm_gem_free_mmap_offset);
 
@@ -328,32 +325,10 @@ drm_gem_create_mmap_offset(struct drm_gem_object *obj)
 {
 	struct drm_device *dev = obj->dev;
 	struct drm_gem_mm *mm = dev->mm_private;
-	struct drm_map_list *list;
-	struct drm_local_map *map;
 	int ret;
 
-	/* Set the object up for mmap'ing */
-	list = &obj->map_list;
-	list->map = kzalloc(sizeof(struct drm_map_list), GFP_KERNEL);
-	if (!list->map)
-		return -ENOMEM;
-
-	map = list->map;
-	map->type = _DRM_GEM;
-	map->size = obj->size;
-	map->handle = obj;
-
-	ret = drm_vma_offset_setup(&mm->vma_manager, &list->vma_offset,
+	ret = drm_vma_offset_setup(&mm->vma_manager, &obj->vma_offset,
 				   obj->size / PAGE_SIZE);
-	if (ret)
-		goto out_free_list;
-
-	return 0;
-
-out_free_list:
-	kfree(list->map);
-	list->map = NULL;
-
 	return ret;
 }
 EXPORT_SYMBOL(drm_gem_create_mmap_offset);
@@ -642,10 +617,8 @@ int drm_gem_mmap(struct file *filp, struct vm_area_struct *vma)
 	struct drm_file *priv = filp->private_data;
 	struct drm_device *dev = priv->minor->dev;
 	struct drm_gem_mm *mm = dev->mm_private;
-	struct drm_local_map *map = NULL;
 	struct drm_gem_object *obj;
 	struct drm_vma_offset_node *offset_node;
-	struct drm_map_list *list;
 	int ret = 0;
 
 	if (drm_device_is_unplugged(dev))
@@ -660,21 +633,14 @@ int drm_gem_mmap(struct file *filp, struct vm_area_struct *vma)
 		return drm_mmap(filp, vma);
 	}
 
-	list = container_of(offset_node, struct drm_map_list, vma_offset);
-	map = list->map;
-	if (!map ||
-	    ((map->flags & _DRM_RESTRICTED) && !capable(CAP_SYS_ADMIN))) {
-		ret =  -EPERM;
-		goto out_unlock;
-	}
+	obj = container_of(offset_node, struct drm_gem_object, vma_offset);
 
 	/* Check for valid size. */
-	if (map->size < vma->vm_end - vma->vm_start) {
+	if (obj->size < vma->vm_end - vma->vm_start) {
 		ret = -EINVAL;
 		goto out_unlock;
 	}
 
-	obj = map->handle;
 	if (!obj->dev->driver->gem_vm_ops) {
 		ret = -EINVAL;
 		goto out_unlock;
@@ -682,7 +648,7 @@ int drm_gem_mmap(struct file *filp, struct vm_area_struct *vma)
 
 	vma->vm_flags |= VM_IO | VM_PFNMAP | VM_DONTEXPAND | VM_DONTDUMP;
 	vma->vm_ops = obj->dev->driver->gem_vm_ops;
-	vma->vm_private_data = map->handle;
+	vma->vm_private_data = (void *)obj;
 	vma->vm_page_prot =  pgprot_writecombine(vm_get_page_prot(vma->vm_flags));
 
 	/* Take a ref for this mapping of the object, so that the fault
drivers/gpu/drm/drm_gem_cma_helper.c +2 -2
index 43b485f..a567a6e 100644
@@ -29,7 +29,7 @@
 
 static unsigned int get_gem_mmap_offset(struct drm_gem_object *obj)
 {
-	return (unsigned int)drm_vma_node_offset_addr(&obj->map_list.vma_offset);
+	return (unsigned int)drm_vma_node_offset_addr(&obj->vma_offset);
 }
 
 static void drm_gem_cma_buf_destroy(struct drm_device *drm,
@@ -140,7 +140,7 @@ void drm_gem_cma_free_object(struct drm_gem_object *gem_obj)
 {
 	struct drm_gem_cma_object *cma_obj;
 
-	if (drm_vma_node_is_allocated(&obj->map_list.vma_offset))
+	if (drm_vma_node_is_allocated(&obj->vma_offset))
 		drm_gem_free_mmap_offset(gem_obj);
 
 	drm_gem_object_release(gem_obj);
drivers/gpu/drm/exynos/exynos_drm_gem.c +3 -3
index 5389bfb..8e64997 100644
@@ -168,7 +168,7 @@ out:
 	exynos_drm_fini_buf(obj->dev, buf);
 	exynos_gem_obj->buffer = NULL;
 
-	if (drm_vma_node_is_allocated(&obj->map_list.vma_offset))
+	if (drm_vma_node_is_allocated(&obj->vma_offset))
 		drm_gem_free_mmap_offset(obj);
 
 	/* release file pointer to gem object. */
@@ -704,13 +704,13 @@ int exynos_drm_gem_dumb_map_offset(struct drm_file *file_priv,
 		goto unlock;
 	}
 
-	if (!drm_vma_node_is_allocated(&obj->map_list.vma_offset)) {
+	if (!drm_vma_node_is_allocated(&obj->vma_offset)) {
 		ret = drm_gem_create_mmap_offset(obj);
 		if (ret)
 			goto out;
 	}
 
-	*offset = drm_vma_node_offset_addr(&obj->map_list.vma_offset);
+	*offset = drm_vma_node_offset_addr(&obj->vma_offset);
 	DRM_DEBUG_KMS("offset = 0x%lx\n", (unsigned long)*offset);
 
 out:
drivers/gpu/drm/gma500/gem.c +3 -3
index 1c49fbc..c60252c 100644
@@ -38,7 +38,7 @@ void psb_gem_free_object(struct drm_gem_object *obj)
 	struct gtt_range *gtt = container_of(obj, struct gtt_range, gem);
 
 	/* Remove the list map if one is present */
-	if (drm_vma_node_is_allocated(&obj->map_list.vma_offset))
+	if (drm_vma_node_is_allocated(&obj->vma_offset))
 		drm_gem_free_mmap_offset(obj);
 	drm_gem_object_release(obj);
 
@@ -81,13 +81,13 @@ int psb_gem_dumb_map_gtt(struct drm_file *file, struct drm_device *dev,
 	/* What validation is needed here ? */
 
 	/* Make it mmapable */
-	if (!drm_vma_node_is_allocated(&obj->map_list.vma_offset)) {
+	if (!drm_vma_node_is_allocated(&obj->vma_offset)) {
 		ret = drm_gem_create_mmap_offset(obj);
 		if (ret)
 			goto out;
 	}
 	/* GEM should really work out the hash offsets for us */
-	*offset = drm_vma_node_offset_addr(&obj->map_list.vma_offset);
+	*offset = drm_vma_node_offset_addr(&obj->vma_offset);
 out:
 	drm_gem_object_unreference(obj);
 unlock:
drivers/gpu/drm/i915/i915_gem.c +4 -4
index 75138cb..16f1b2c 100644
@@ -1426,7 +1426,7 @@ i915_gem_release_mmap(struct drm_i915_gem_object *obj)
 
 	if (obj->base.dev->dev_mapping)
 		unmap_mapping_range(obj->base.dev->dev_mapping,
-				    (loff_t)drm_vma_node_offset_addr(&obj->base.map_list.vma_offset),
+				    (loff_t)drm_vma_node_offset_addr(&obj->base.vma_offset),
 				    obj->base.size, 1);
 
 	obj->fault_mappable = false;
@@ -1514,7 +1514,7 @@ static int i915_gem_object_create_mmap_offset(struct drm_i915_gem_object *obj)
 	struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
 	int ret;
 
-	if (drm_vma_node_is_allocated(&obj->base.map_list.vma_offset))
+	if (drm_vma_node_is_allocated(&obj->base.vma_offset))
 		return 0;
 
 	ret = drm_gem_create_mmap_offset(&obj->base);
@@ -1539,7 +1539,7 @@ static int i915_gem_object_create_mmap_offset(struct drm_i915_gem_object *obj)
 
 static void i915_gem_object_free_mmap_offset(struct drm_i915_gem_object *obj)
 {
-	if (!drm_vma_node_is_allocated(&obj->base.map_list.vma_offset))
+	if (!drm_vma_node_is_allocated(&obj->base.vma_offset))
 		return;
 
 	drm_gem_free_mmap_offset(&obj->base);
@@ -1580,7 +1580,7 @@ i915_gem_mmap_gtt(struct drm_file *file,
 	if (ret)
 		goto out;
 
-	*offset = drm_vma_node_offset_addr(&obj->base.map_list.vma_offset);
+	*offset = drm_vma_node_offset_addr(&obj->base.vma_offset);
 
 out:
 	drm_gem_object_unreference(&obj->base);
drivers/gpu/drm/udl/udl_gem.c +3 -3
index 2cc79d2..7b5fb1f 100644
@@ -223,7 +223,7 @@ void udl_gem_free_object(struct drm_gem_object *gem_obj)
 	if (obj->pages)
 		udl_gem_put_pages(obj);
 
-	if (drm_vma_node_is_allocated(&gem_obj->map_list.vma_offset))
+	if (drm_vma_node_is_allocated(&gem_obj->vma_offset))
 		drm_gem_free_mmap_offset(gem_obj);
 }
 
@@ -247,13 +247,13 @@ int udl_gem_mmap(struct drm_file *file, struct drm_device *dev,
 	ret = udl_gem_get_pages(gobj, GFP_KERNEL);
 	if (ret)
 		goto out;
-	if (!drm_vma_node_is_allocated(&gobj->base.map_list.vma_offset)) {
+	if (!drm_vma_node_is_allocated(&gobj->base.vma_offset)) {
 		ret = drm_gem_create_mmap_offset(obj);
 		if (ret)
 			goto out;
 	}
 
-	*offset = drm_vma_node_offset_addr(&obj->map_list.vma_offset);
+	*offset = drm_vma_node_offset_addr(&obj->vma_offset);
 
 out:
 	drm_gem_object_unreference(&gobj->base);
include/drm/drmP.h +1 -2
index 963e00e..f7186e8 100644
@@ -580,7 +580,6 @@ struct drm_map_list {
 	struct drm_local_map *map;	/**< mapping */
 	uint64_t user_token;
 	struct drm_master *master;
-	struct drm_vma_offset_node vma_offset;
 };
 
 /**
@@ -636,7 +635,7 @@ struct drm_gem_object {
 	struct file *filp;
 
 	/* Mapping info for this object */
-	struct drm_map_list map_list;
+	struct drm_vma_offset_node vma_offset;
 
 	/**
 	 * Size of the object, in bytes.  Immutable over the object's
Signature · 1 line
1.8.0.2
author maintainer
#5 lore raw

PATCH 3/3 drm/ttm: drop addr_space_offset, use accessor

In reply to Dave Airlie

From: Dave Airlie <[email protected]>

This uses the drm vm accessor to access the address space offset rather than storing it separately.

When I boot tested this, it threw up a problem in the virtual unmapping code where we unmap a mapping range from 0 unnecessairly, so this patch also checks we have a mapping before calling the unmap_mapping_range.

---
 drivers/gpu/drm/ast/ast_main.c            |  2 +-
 drivers/gpu/drm/cirrus/cirrus_main.c      |  2 +-
 drivers/gpu/drm/mgag200/mgag200_main.c    |  2 +-
 drivers/gpu/drm/nouveau/nouveau_display.c |  2 +-
 drivers/gpu/drm/nouveau/nouveau_gem.c     |  2 +-
 drivers/gpu/drm/radeon/radeon_object.h    |  2 +-
 drivers/gpu/drm/ttm/ttm_bo.c              | 13 ++++++-------
 drivers/gpu/drm/vmwgfx/vmwgfx_resource.c  |  4 ++--
 include/drm/ttm/ttm_bo_api.h              |  1 -
 9 files changed, 14 insertions(+), 16 deletions(-)
drivers/gpu/drm/ast/ast_main.c +1 -1
index f668e6c..bb7e9d5 100644
@@ -495,7 +495,7 @@ void ast_gem_free_object(struct drm_gem_object *obj)
 
 static inline u64 ast_bo_mmap_offset(struct ast_bo *bo)
 {
-	return bo->bo.addr_space_offset;
+	return drm_vma_node_offset_addr(&bo->bo.vma_offset);
 }
 int
 ast_dumb_mmap_offset(struct drm_file *file,
drivers/gpu/drm/cirrus/cirrus_main.c +1 -1
index 6a9b12e..1b21135 100644
@@ -302,7 +302,7 @@ void cirrus_gem_free_object(struct drm_gem_object *obj)
 
 static inline u64 cirrus_bo_mmap_offset(struct cirrus_bo *bo)
 {
-	return bo->bo.addr_space_offset;
+	return drm_vma_node_offset_addr(&bo->bo.vma_offset);
 }
 
 int
drivers/gpu/drm/mgag200/mgag200_main.c +1 -1
index 70dd3c5..f139008 100644
@@ -357,7 +357,7 @@ void mgag200_gem_free_object(struct drm_gem_object *obj)
 
 static inline u64 mgag200_bo_mmap_offset(struct mgag200_bo *bo)
 {
-	return bo->bo.addr_space_offset;
+	return drm_vma_node_offset_addr(&bo->bo.vma_offset);
 }
 
 int
drivers/gpu/drm/nouveau/nouveau_display.c +1 -1
index e4188f2..e5c127a 100644
@@ -778,7 +778,7 @@ nouveau_display_dumb_map_offset(struct drm_file *file_priv,
 	gem = drm_gem_object_lookup(dev, file_priv, handle);
 	if (gem) {
 		struct nouveau_bo *bo = gem->driver_private;
-		*poffset = bo->bo.addr_space_offset;
+		*poffset = drm_vma_node_offset_addr(&bo->bo.vma_offset);
 		drm_gem_object_unreference_unlocked(gem);
 		return 0;
 	}
drivers/gpu/drm/nouveau/nouveau_gem.c +1 -1
index 8bf695c..6be9249 100644
@@ -194,7 +194,7 @@ nouveau_gem_info(struct drm_file *file_priv, struct drm_gem_object *gem,
 	}
 
 	rep->size = nvbo->bo.mem.num_pages << PAGE_SHIFT;
-	rep->map_handle = nvbo->bo.addr_space_offset;
+	rep->map_handle = drm_vma_node_offset_addr(&nvbo->bo.vma_offset);
 	rep->tile_mode = nvbo->tile_mode;
 	rep->tile_flags = nvbo->tile_flags;
 	return 0;
drivers/gpu/drm/radeon/radeon_object.h +1 -1
index 5fc86b0..a661db5 100644
@@ -104,7 +104,7 @@ static inline unsigned radeon_bo_gpu_page_alignment(struct radeon_bo *bo)
  */
 static inline u64 radeon_bo_mmap_offset(struct radeon_bo *bo)
 {
-	return bo->tbo.addr_space_offset;
+	return drm_vma_node_offset_addr(&bo->tbo.vma_offset);
 }
 
 extern int radeon_bo_wait(struct radeon_bo *bo, u32 *mem_type,
drivers/gpu/drm/ttm/ttm_bo.c +6 -7
index 7f3596c..3f42621 100644
@@ -1596,12 +1596,13 @@ bool ttm_mem_reg_is_pci(struct ttm_bo_device *bdev, struct ttm_mem_reg *mem)
 void ttm_bo_unmap_virtual_locked(struct ttm_buffer_object *bo)
 {
 	struct ttm_bo_device *bdev = bo->bdev;
-	loff_t offset = (loff_t) bo->addr_space_offset;
-	loff_t holelen = ((loff_t) bo->mem.num_pages) << PAGE_SHIFT;
 
-	if (!bdev->dev_mapping)
-		return;
-	unmap_mapping_range(bdev->dev_mapping, offset, holelen, 1);
+	if (drm_vma_node_is_allocated(&bo->vma_offset) && bdev->dev_mapping) {
+		loff_t offset = (loff_t) drm_vma_node_offset_addr(&bo->vma_offset);
+		loff_t holelen = ((loff_t) bo->mem.num_pages) << PAGE_SHIFT;
+
+		unmap_mapping_range(bdev->dev_mapping, offset, holelen, 1);
+	}
 	ttm_mem_io_free_vm(bo);
 }
 
@@ -1638,8 +1639,6 @@ static int ttm_bo_setup_vm(struct ttm_buffer_object *bo)
 				   &bo->vma_offset,
 				   bo->mem.num_pages);
 
-	if (ret == 0)
-		bo->addr_space_offset = drm_vma_node_offset_addr(&bo->vma_offset);
 	return ret;
 }
 
drivers/gpu/drm/vmwgfx/vmwgfx_resource.c +2 -2
index e01a17b..b2dcfbe 100644
@@ -501,7 +501,7 @@ int vmw_dmabuf_alloc_ioctl(struct drm_device *dev, void *data,
 		goto out_no_dmabuf;
 
 	rep->handle = handle;
-	rep->map_handle = dma_buf->base.addr_space_offset;
+	rep->map_handle = drm_vma_node_offset_addr(&dma_buf->base.vma_offset);
 	rep->cur_gmr_id = handle;
 	rep->cur_gmr_offset = 0;
 
@@ -835,7 +835,7 @@ int vmw_dumb_map_offset(struct drm_file *file_priv,
 	if (ret != 0)
 		return -EINVAL;
 
-	*offset = out_buf->base.addr_space_offset;
+	*offset = drm_vma_node_offset_addr(&out_buf->base.vma_offset);
 	vmw_dmabuf_unreference(&out_buf);
 	return 0;
 }
include/drm/ttm/ttm_bo_api.h +0 -1
index fd2020f..5c88c8f 100644
@@ -200,7 +200,6 @@ struct ttm_buffer_object {
 	enum ttm_bo_type type;
 	void (*destroy) (struct ttm_buffer_object *);
 	unsigned long num_pages;
-	uint64_t addr_space_offset;
 	size_t acc_size;
 
 	/**
Signature · 1 line
1.8.0.2
#2 lore raw

Re: [PATCH 1/3] drm: create unified vma offset manager

In reply to Dave Airlie
Quoted 5 lines · Dave Airlie

From: Dave Airlie <[email protected]>

So we have to offset manager implementations for dealing with VMA offsets. GEM had one using the hash table, TTM had one using an rbtree,

Quoted 7 lines

I'd rather we just had one to rule them all, since ttm is using the rbtree variant to allow sub mappings, to keep ABI we need to use that one.

This also adds a bunch of inline helpers to avoid gem/ttm poking around inside the vma_offset objects. TTM needs a reset helper to remove a vma_offset when it copies an object to another one for buffer moves. The helpers also let drivers avoid the map_list.hash_key << PAGE_SHIFT nonsense.

Any clue as to the performance difference between the two implementations? What does it add to the cost of a pagefault?

Hmm, don't have an i-g-t handy for scalability testing of the fault handlers.

Quoted 7 lines
drivers/gpu/drm/drm_vma_offset_man.c new file +7 -0
+int drm_vma_offset_setup(struct drm_vma_offset_manager *man,
+			 struct drm_vma_offset_node *node,
+			 unsigned long num_pages)
+{
+	int ret;
+
+retry_pre_get:

drivers/gpu/drm/drm_vma_offset_man.c line 104

Quoted 10 lines
drivers/gpu/drm/drm_vma_offset_man.c new file +10 -0
+	ret = drm_mm_pre_get(&man->addr_space_mm);
+	if (unlikely(ret != 0))
+		return ret;
+
+	write_lock(&man->vm_lock);
+	node->vm_node = drm_mm_search_free(&man->addr_space_mm,
+					   num_pages, 0, 0);
+
+	if (unlikely(node->vm_node == NULL)) {
+		ret = -ENOMEM;

ret = -ENOSPC;

Depended upon by the higher layers for determining when to purge their caches; i-g-t/gem_mmap_offset_exhaustion

drivers/gpu/drm/drm_vma_offset_man.c line 110

Quoted 6 lines
drivers/gpu/drm/drm_vma_offset_man.c new file +6 -0
+		goto out_unlock;
+	}
+
+	node->vm_node = drm_mm_get_block_atomic(node->vm_node,
+						num_pages, 0);
+

I'd feel happier if this tried to only take from the preallocated pool rather than actually try a GFP_ATOMIC allocation.

Quoted 16 lines
drivers/gpu/drm/drm_vma_offset_man.c new file +16 -0
+	if (unlikely(node->vm_node == NULL)) {
+		write_unlock(&man->vm_lock);
+		goto retry_pre_get;
+	}
+
+	node->num_pages = num_pages;
+	drm_vma_offset_insert_rb(man, node);
+	write_unlock(&man->vm_lock);
+
+	return 0;
+out_unlock:
+	write_unlock(&man->vm_lock);
+	return ret;	
+	
+}
+EXPORT_SYMBOL(drm_vma_offset_setup);
Signature · 1 line
Chris Wilson, Intel Open Source Technology Centre
#3 lore raw

Re: [PATCH 1/3] drm: create unified vma offset manager

Quoted 39 lines · Chris Wilson
Quoted 5 lines · Dave Airlie

From: Dave Airlie <[email protected]>

So we have to offset manager implementations for dealing with VMA offsets. GEM had one using the hash table, TTM had one using an rbtree,

Quoted 7 lines

I'd rather we just had one to rule them all, since ttm is using the rbtree variant to allow sub mappings, to keep ABI we need to use that one.

This also adds a bunch of inline helpers to avoid gem/ttm poking around inside the vma_offset objects. TTM needs a reset helper to remove a vma_offset when it copies an object to another one for buffer moves. The helpers also let drivers avoid the map_list.hash_key << PAGE_SHIFT nonsense.

Any clue as to the performance difference between the two implementations? What does it add to the cost of a pagefault?

Hmm, don't have an i-g-t handy for scalability testing of the fault handlers.

Quoted 7 lines
drivers/gpu/drm/drm_vma_offset_man.c new file +7 -0
+int drm_vma_offset_setup(struct drm_vma_offset_manager *man,
+			 struct drm_vma_offset_node *node,
+			 unsigned long num_pages)
+{
+	int ret;
+
+retry_pre_get:

drivers/gpu/drm/drm_vma_offset_man.c line 104

Quoted 10 lines
drivers/gpu/drm/drm_vma_offset_man.c new file +10 -0
+	ret = drm_mm_pre_get(&man->addr_space_mm);
+	if (unlikely(ret != 0))
+		return ret;
+
+	write_lock(&man->vm_lock);
+	node->vm_node = drm_mm_search_free(&man->addr_space_mm,
+					   num_pages, 0, 0);
+
+	if (unlikely(node->vm_node == NULL)) {
+		ret = -ENOMEM;

ret = -ENOSPC;

Quoted 2 lines

Depended upon by the higher layers for determining when to purge their caches; i-g-t/gem_mmap_offset_exhaustion

The larger topic is that drm_mm is only 32bit on 32bit and we routinely exhaust that after a few weeks of uptime. Or better: We _did_ exhaust that, until we've added tons of checks in both kernel&libdrm to reap cached objects if it doesn't work. Hence it's paramount for our code to get a -ENOSPC to engage in mmap offset reaping.

Quoted 9 lines

drivers/gpu/drm/drm_vma_offset_man.c line 110

Quoted 6 lines
drivers/gpu/drm/drm_vma_offset_man.c new file +6 -0
+		goto out_unlock;
+	}
+
+	node->vm_node = drm_mm_get_block_atomic(node->vm_node,
+						num_pages, 0);
+

I'd feel happier if this tried to only take from the preallocated pool rather than actually try a GFP_ATOMIC allocation.

Seconded. Imo drm_mm_pre_get should just die - it artificially limits prealloc to 4 nodes, which means you'll fall over if 5 threads enter this. And with the drm_mm rework of about a year ago it's no longer required, you can simply embedded the struct drm_mm_node whereever you want, and mm won't allocate anything any more on top of that. Or preallocate the drm_mm_node in the code, outside of the locks. Inserting the node happens then with drm_mm_insert* functions, which combine the search_free + get_blcok (since really, there's nothing interesting you can do with the hole space you get from search_free). See e.g.

http://cgit.freedesktop.org/~danvet/drm-intel/commit/?id=dc9dd7a20fde95aa81a8307cde79c2dff9f83f3d

for the conversion. My efforts of yonder to convert everyon have stalled a bit in the ttm code, but now that i915 is converted and (hopefully) the mmap offset stuff, I'll pick this up again. Would allow us to kill quite a bit of cruft from the drm_mm interfaces.

Cheers, Daniel

Signature · 3 lines
Daniel Vetter
Software Engineer, Intel Corporation
+41 (0) 79 365 57 48 - http://blog.ffwll.ch