All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH 1/3] drm/ttm: rework ttm_tt page limit v3
@ 2021-01-28 13:16 Christian König
  2021-01-28 13:16 ` [PATCH 2/3] drm/ttm: move memory accounting into vmwgfx v3 Christian König
                   ` (2 more replies)
  0 siblings, 3 replies; 17+ messages in thread
From: Christian König @ 2021-01-28 13:16 UTC (permalink / raw)
  To: sroland, zackr, daniel, linux-graphics-maintainer, dri-devel

TTM implements a rather extensive accounting of allocated memory.

There are two reasons for this:
1. It tries to block userspace allocating a huge number of very small
   BOs without accounting for the kmalloced memory.

2. Make sure we don't over allocate and run into an OOM situation
   during swapout while trying to handle the memory shortage.

This is only partially a good idea. First of all it is perfectly
valid for an application to use all of system memory, limiting it to
50% is not really acceptable.

What we need to take care of is that the application is held
accountable for the memory it allocated. This is what control
mechanisms like memcg and the normal Linux page accounting already do.

Making sure that we don't run into an OOM situation while trying to
cope with a memory shortage is still a good idea, but this is also
not very well implemented since it means another opportunity of
recursion from the driver back into TTM.

So start to rework all of this by implementing a shrinker callback which
allows for TT object to be swapped out if necessary.

v2: Switch from limit to shrinker callback.
v3: fix gfp mask handling, use atomic for swapable_pages, add debugfs

Signed-off-by: Christian König <christian.koenig@amd.com>
---
 drivers/gpu/drm/ttm/ttm_bo.c        |   4 +-
 drivers/gpu/drm/ttm/ttm_memory.c    |   7 +-
 drivers/gpu/drm/ttm/ttm_tt.c        | 111 ++++++++++++++++++++++++++--
 drivers/gpu/drm/vmwgfx/vmwgfx_drv.c |   2 +-
 include/drm/ttm/ttm_bo_api.h        |   2 +-
 include/drm/ttm/ttm_tt.h            |   6 +-
 6 files changed, 117 insertions(+), 15 deletions(-)

diff --git a/drivers/gpu/drm/ttm/ttm_bo.c b/drivers/gpu/drm/ttm/ttm_bo.c
index 20256797f3a6..643befc1a6f2 100644
--- a/drivers/gpu/drm/ttm/ttm_bo.c
+++ b/drivers/gpu/drm/ttm/ttm_bo.c
@@ -1219,7 +1219,7 @@ EXPORT_SYMBOL(ttm_bo_wait);
  * A buffer object shrink method that tries to swap out the first
  * buffer object on the bo_global::swap_lru list.
  */
-int ttm_bo_swapout(struct ttm_operation_ctx *ctx)
+int ttm_bo_swapout(struct ttm_operation_ctx *ctx, gfp_t gfp_flags)
 {
 	struct ttm_global *glob = &ttm_glob;
 	struct ttm_buffer_object *bo;
@@ -1302,7 +1302,7 @@ int ttm_bo_swapout(struct ttm_operation_ctx *ctx)
 	if (bo->bdev->funcs->swap_notify)
 		bo->bdev->funcs->swap_notify(bo);
 
-	ret = ttm_tt_swapout(bo->bdev, bo->ttm);
+	ret = ttm_tt_swapout(bo->bdev, bo->ttm, gfp_flags);
 out:
 
 	/**
diff --git a/drivers/gpu/drm/ttm/ttm_memory.c b/drivers/gpu/drm/ttm/ttm_memory.c
index a3bfbd9cea68..634a85c2dc4c 100644
--- a/drivers/gpu/drm/ttm/ttm_memory.c
+++ b/drivers/gpu/drm/ttm/ttm_memory.c
@@ -37,6 +37,7 @@
 #include <linux/slab.h>
 #include <linux/swap.h>
 #include <drm/ttm/ttm_pool.h>
+#include <drm/ttm/ttm_tt.h>
 
 #include "ttm_module.h"
 
@@ -276,9 +277,9 @@ static void ttm_shrink(struct ttm_mem_global *glob, bool from_wq,
 
 	while (ttm_zones_above_swap_target(glob, from_wq, extra)) {
 		spin_unlock(&glob->lock);
-		ret = ttm_bo_swapout(ctx);
+		ret = ttm_bo_swapout(ctx, GFP_KERNEL);
 		spin_lock(&glob->lock);
-		if (unlikely(ret != 0))
+		if (unlikely(ret < 0))
 			break;
 	}
 
@@ -453,6 +454,7 @@ int ttm_mem_global_init(struct ttm_mem_global *glob)
 			zone->name, (unsigned long long)zone->max_mem >> 10);
 	}
 	ttm_pool_mgr_init(glob->zone_kernel->max_mem/(2*PAGE_SIZE));
+	ttm_tt_mgr_init();
 	return 0;
 out_no_zone:
 	ttm_mem_global_release(glob);
@@ -466,6 +468,7 @@ void ttm_mem_global_release(struct ttm_mem_global *glob)
 
 	/* let the page allocator first stop the shrink work. */
 	ttm_pool_mgr_fini();
+	ttm_tt_mgr_fini();
 
 	flush_workqueue(glob->swap_queue);
 	destroy_workqueue(glob->swap_queue);
diff --git a/drivers/gpu/drm/ttm/ttm_tt.c b/drivers/gpu/drm/ttm/ttm_tt.c
index 7782d5393c7c..b67795de228d 100644
--- a/drivers/gpu/drm/ttm/ttm_tt.c
+++ b/drivers/gpu/drm/ttm/ttm_tt.c
@@ -38,6 +38,11 @@
 #include <drm/drm_cache.h>
 #include <drm/ttm/ttm_bo_driver.h>
 
+#include "ttm_module.h"
+
+static struct shrinker mm_shrinker;
+static atomic_long_t swapable_pages;
+
 /*
  * Allocates a ttm structure for the given BO.
  */
@@ -223,32 +228,41 @@ int ttm_tt_swapin(struct ttm_tt *ttm)
 	return ret;
 }
 
-int ttm_tt_swapout(struct ttm_device *bdev, struct ttm_tt *ttm)
+/**
+ * ttm_tt_swapout - swap out tt object
+ *
+ * @bdev: TTM device structure.
+ * @ttm: The struct ttm_tt.
+ * @gfp_flags: Flags to use for memory allocation.
+ *
+ * Swapout a TT object to a shmem_file, return number of pages swapped out or
+ * negative error code.
+ */
+int ttm_tt_swapout(struct ttm_device *bdev, struct ttm_tt *ttm,
+		   gfp_t gfp_flags)
 {
+	loff_t size = (loff_t)ttm->num_pages << PAGE_SHIFT;
 	struct address_space *swap_space;
 	struct file *swap_storage;
 	struct page *from_page;
 	struct page *to_page;
-	gfp_t gfp_mask;
 	int i, ret;
 
-	swap_storage = shmem_file_setup("ttm swap",
-					ttm->num_pages << PAGE_SHIFT,
-					0);
+	swap_storage = shmem_file_setup("ttm swap", size, 0);
 	if (IS_ERR(swap_storage)) {
 		pr_err("Failed allocating swap storage\n");
 		return PTR_ERR(swap_storage);
 	}
 
 	swap_space = swap_storage->f_mapping;
-	gfp_mask = mapping_gfp_mask(swap_space);
+	gfp_flags &= mapping_gfp_mask(swap_space);
 
 	for (i = 0; i < ttm->num_pages; ++i) {
 		from_page = ttm->pages[i];
 		if (unlikely(from_page == NULL))
 			continue;
 
-		to_page = shmem_read_mapping_page_gfp(swap_space, i, gfp_mask);
+		to_page = shmem_read_mapping_page_gfp(swap_space, i, gfp_flags);
 		if (IS_ERR(to_page)) {
 			ret = PTR_ERR(to_page);
 			goto out_err;
@@ -263,7 +277,7 @@ int ttm_tt_swapout(struct ttm_device *bdev, struct ttm_tt *ttm)
 	ttm->swap_storage = swap_storage;
 	ttm->page_flags |= TTM_PAGE_FLAG_SWAPPED;
 
-	return 0;
+	return ttm->num_pages;
 
 out_err:
 	fput(swap_storage);
@@ -280,6 +294,8 @@ static void ttm_tt_add_mapping(struct ttm_device *bdev, struct ttm_tt *ttm)
 
 	for (i = 0; i < ttm->num_pages; ++i)
 		ttm->pages[i]->mapping = bdev->dev_mapping;
+
+	atomic_long_add(ttm->num_pages, &swapable_pages);
 }
 
 int ttm_tt_populate(struct ttm_device *bdev,
@@ -326,6 +342,8 @@ static void ttm_tt_clear_mapping(struct ttm_tt *ttm)
 		(*page)->mapping = NULL;
 		(*page++)->index = 0;
 	}
+
+	atomic_long_sub(ttm->num_pages, &swapable_pages);
 }
 
 void ttm_tt_unpopulate(struct ttm_device *bdev,
@@ -341,3 +359,80 @@ void ttm_tt_unpopulate(struct ttm_device *bdev,
 		ttm_pool_free(&bdev->pool, ttm);
 	ttm->page_flags &= ~TTM_PAGE_FLAG_PRIV_POPULATED;
 }
+
+/* As long as pages are available make sure to release at least one */
+static unsigned long ttm_tt_shrinker_scan(struct shrinker *shrink,
+					  struct shrink_control *sc)
+{
+	struct ttm_operation_ctx ctx = {
+		.no_wait_gpu = false
+	};
+	int ret;
+
+	if (!(sc->gfp_mask & __GFP_FS))
+		return SHRINK_EMPTY;
+
+	ret = ttm_bo_swapout(&ctx, GFP_NOFS);
+	return ret < 0 ? SHRINK_EMPTY : ret;
+}
+
+/* Return the number of pages available or SHRINK_EMPTY if we have none */
+static unsigned long ttm_tt_shrinker_count(struct shrinker *shrink,
+					   struct shrink_control *sc)
+{
+	unsigned long num_pages;
+
+	if (!(sc->gfp_mask & __GFP_FS))
+		return SHRINK_EMPTY;
+
+	num_pages = atomic_long_read(&swapable_pages);
+	return num_pages ? num_pages : SHRINK_EMPTY;
+}
+
+#ifdef CONFIG_DEBUG_FS
+
+/* Test the shrinker functions and dump the result */
+static int ttm_tt_debugfs_shrink_show(struct seq_file *m, void *data)
+{
+	struct shrink_control sc = { .gfp_mask = GFP_KERNEL };
+
+	fs_reclaim_acquire(GFP_KERNEL);
+	seq_printf(m, "%lu/%lu\n", ttm_tt_shrinker_count(&mm_shrinker, &sc),
+		   ttm_tt_shrinker_scan(&mm_shrinker, &sc));
+	fs_reclaim_release(GFP_KERNEL);
+
+	return 0;
+}
+DEFINE_SHOW_ATTRIBUTE(ttm_tt_debugfs_shrink);
+
+#endif
+
+
+
+/**
+ * ttm_tt_mgr_init - register with the MM shrinker
+ *
+ * Register with the MM shrinker for swapping out BOs.
+ */
+int ttm_tt_mgr_init(void)
+{
+#ifdef CONFIG_DEBUG_FS
+	debugfs_create_file("tt_shrink", 0400, ttm_debugfs_root, NULL,
+			    &ttm_tt_debugfs_shrink_fops);
+#endif
+
+	mm_shrinker.count_objects = ttm_tt_shrinker_count;
+	mm_shrinker.scan_objects = ttm_tt_shrinker_scan;
+	mm_shrinker.seeks = 1;
+	return register_shrinker(&mm_shrinker);
+}
+
+/**
+ * ttm_tt_mgr_fini - unregister our MM shrinker
+ *
+ * Unregisters the MM shrinker.
+ */
+void ttm_tt_mgr_fini(void)
+{
+	unregister_shrinker(&mm_shrinker);
+}
diff --git a/drivers/gpu/drm/vmwgfx/vmwgfx_drv.c b/drivers/gpu/drm/vmwgfx/vmwgfx_drv.c
index b454d80c273e..710ba5169a74 100644
--- a/drivers/gpu/drm/vmwgfx/vmwgfx_drv.c
+++ b/drivers/gpu/drm/vmwgfx/vmwgfx_drv.c
@@ -1383,7 +1383,7 @@ static int vmw_pm_freeze(struct device *kdev)
 	vmw_execbuf_release_pinned_bo(dev_priv);
 	vmw_resource_evict_all(dev_priv);
 	vmw_release_device_early(dev_priv);
-	while (ttm_bo_swapout(&ctx) == 0);
+	while (ttm_bo_swapout(&ctx, GFP_KERNEL) > 0);
 	if (dev_priv->enable_fb)
 		vmw_fifo_resource_dec(dev_priv);
 	if (atomic_read(&dev_priv->num_fifo_resources) != 0) {
diff --git a/include/drm/ttm/ttm_bo_api.h b/include/drm/ttm/ttm_bo_api.h
index 62734db0b421..1297a8fb7ccb 100644
--- a/include/drm/ttm/ttm_bo_api.h
+++ b/include/drm/ttm/ttm_bo_api.h
@@ -569,7 +569,7 @@ ssize_t ttm_bo_io(struct ttm_device *bdev, struct file *filp,
 		  const char __user *wbuf, char __user *rbuf,
 		  size_t count, loff_t *f_pos, bool write);
 
-int ttm_bo_swapout(struct ttm_operation_ctx *ctx);
+int ttm_bo_swapout(struct ttm_operation_ctx *ctx, gfp_t gfp_flags);
 
 /**
  * ttm_bo_uses_embedded_gem_object - check if the given bo uses the
diff --git a/include/drm/ttm/ttm_tt.h b/include/drm/ttm/ttm_tt.h
index 0020a0588985..cce57fb49e2c 100644
--- a/include/drm/ttm/ttm_tt.h
+++ b/include/drm/ttm/ttm_tt.h
@@ -135,7 +135,8 @@ void ttm_tt_destroy_common(struct ttm_device *bdev, struct ttm_tt *ttm);
  * Swap in a previously swap out ttm_tt.
  */
 int ttm_tt_swapin(struct ttm_tt *ttm);
-int ttm_tt_swapout(struct ttm_device *bdev, struct ttm_tt *ttm);
+int ttm_tt_swapout(struct ttm_device *bdev, struct ttm_tt *ttm,
+		   gfp_t gfp_flags);
 
 /**
  * ttm_tt_populate - allocate pages for a ttm
@@ -155,6 +156,9 @@ int ttm_tt_populate(struct ttm_device *bdev, struct ttm_tt *ttm, struct ttm_oper
  */
 void ttm_tt_unpopulate(struct ttm_device *bdev, struct ttm_tt *ttm);
 
+int ttm_tt_mgr_init(void);
+void ttm_tt_mgr_fini(void);
+
 #if IS_ENABLED(CONFIG_AGP)
 #include <linux/agp_backend.h>
 
-- 
2.25.1

_______________________________________________
dri-devel mailing list
dri-devel@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/dri-devel

^ permalink raw reply related	[flat|nested] 17+ messages in thread

* [PATCH 2/3] drm/ttm: move memory accounting into vmwgfx v3
  2021-01-28 13:16 [PATCH 1/3] drm/ttm: rework ttm_tt page limit v3 Christian König
@ 2021-01-28 13:16 ` Christian König
  2021-02-02 13:04   ` Christian König
  2021-01-28 13:16 ` [PATCH 3/3] drm/ttm: drop sysfs directory Christian König
  2021-02-03 11:26 ` [PATCH 1/3] drm/ttm: rework ttm_tt page limit v3 Daniel Vetter
  2 siblings, 1 reply; 17+ messages in thread
From: Christian König @ 2021-01-28 13:16 UTC (permalink / raw)
  To: sroland, zackr, daniel, linux-graphics-maintainer, dri-devel

This is just another feature which is only used by VMWGFX, so move
it into the driver instead.

I've tried to add the accounting sysfs file to the kobject of the drm
minor, but I'm not 100% sure if this works as expected.

v2: fix typo in KFD and avoid 64bit divide
v3: fix init order in VMWGFX

Signed-off-by: Christian König <christian.koenig@amd.com>
---
 .../gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c  | 16 ++++++---
 drivers/gpu/drm/amd/amdgpu/amdgpu_object.c    |  8 ++---
 drivers/gpu/drm/drm_gem_vram_helper.c         |  6 ++--
 drivers/gpu/drm/nouveau/nouveau_bo.c          |  7 ++--
 drivers/gpu/drm/nouveau/nouveau_drv.h         |  1 -
 drivers/gpu/drm/qxl/qxl_object.c              |  4 +--
 drivers/gpu/drm/radeon/radeon_object.c        |  8 ++---
 drivers/gpu/drm/ttm/Makefile                  |  7 ++--
 drivers/gpu/drm/ttm/ttm_bo.c                  | 33 +------------------
 drivers/gpu/drm/ttm/ttm_bo_util.c             |  1 -
 drivers/gpu/drm/ttm/ttm_device.c              | 22 ++++++++++---
 drivers/gpu/drm/ttm/ttm_pool.c                | 13 +-------
 drivers/gpu/drm/vmwgfx/Makefile               |  2 +-
 drivers/gpu/drm/{ttm => vmwgfx}/ttm_memory.c  | 19 ++++-------
 .../gpu/drm/vmwgfx}/ttm_memory.h              |  5 +--
 drivers/gpu/drm/vmwgfx/ttm_object.h           |  3 +-
 drivers/gpu/drm/vmwgfx/vmwgfx_bo.c            | 22 ++++++++++---
 drivers/gpu/drm/vmwgfx/vmwgfx_drv.c           |  5 +++
 drivers/gpu/drm/vmwgfx/vmwgfx_ttm_buffer.c    | 28 +++++++++++++++-
 include/drm/ttm/ttm_bo_api.h                  | 13 ++------
 include/drm/ttm/ttm_bo_driver.h               |  1 -
 include/drm/ttm/ttm_tt.h                      |  1 +
 22 files changed, 110 insertions(+), 115 deletions(-)
 rename drivers/gpu/drm/{ttm => vmwgfx}/ttm_memory.c (97%)
 rename {include/drm/ttm => drivers/gpu/drm/vmwgfx}/ttm_memory.h (97%)

diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c b/drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c
index 0849b68e784f..e440af37dde8 100644
--- a/drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c
+++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c
@@ -118,6 +118,16 @@ void amdgpu_amdkfd_gpuvm_init_mem_limits(void)
  */
 #define ESTIMATE_PT_SIZE(mem_size) ((mem_size) >> 14)
 
+static size_t amdgpu_amdkfd_acc_size(uint64_t size)
+{
+	size >>= PAGE_SHIFT;
+	size *= sizeof(dma_addr_t) + sizeof(void *);
+
+	return __roundup_pow_of_two(sizeof(struct amdgpu_bo)) +
+		__roundup_pow_of_two(sizeof(struct ttm_tt)) +
+		PAGE_ALIGN(size);
+}
+
 static int amdgpu_amdkfd_reserve_mem_limit(struct amdgpu_device *adev,
 		uint64_t size, u32 domain, bool sg)
 {
@@ -126,8 +136,7 @@ static int amdgpu_amdkfd_reserve_mem_limit(struct amdgpu_device *adev,
 	size_t acc_size, system_mem_needed, ttm_mem_needed, vram_needed;
 	int ret = 0;
 
-	acc_size = ttm_bo_dma_acc_size(&adev->mman.bdev, size,
-				       sizeof(struct amdgpu_bo));
+	acc_size = amdgpu_amdkfd_acc_size(size);
 
 	vram_needed = 0;
 	if (domain == AMDGPU_GEM_DOMAIN_GTT) {
@@ -174,8 +183,7 @@ static void unreserve_mem_limit(struct amdgpu_device *adev,
 {
 	size_t acc_size;
 
-	acc_size = ttm_bo_dma_acc_size(&adev->mman.bdev, size,
-				       sizeof(struct amdgpu_bo));
+	acc_size = amdgpu_amdkfd_acc_size(size);
 
 	spin_lock(&kfd_mem_limit.mem_limit_lock);
 	if (domain == AMDGPU_GEM_DOMAIN_GTT) {
diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_object.c b/drivers/gpu/drm/amd/amdgpu/amdgpu_object.c
index 6cc9919b12cc..599c9a132eb6 100644
--- a/drivers/gpu/drm/amd/amdgpu/amdgpu_object.c
+++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_object.c
@@ -523,7 +523,6 @@ static int amdgpu_bo_do_create(struct amdgpu_device *adev,
 	};
 	struct amdgpu_bo *bo;
 	unsigned long page_align, size = bp->size;
-	size_t acc_size;
 	int r;
 
 	/* Note that GDS/GWS/OA allocates 1 page per byte/resource. */
@@ -546,9 +545,6 @@ static int amdgpu_bo_do_create(struct amdgpu_device *adev,
 
 	*bo_ptr = NULL;
 
-	acc_size = ttm_bo_dma_acc_size(&adev->mman.bdev, size,
-				       sizeof(struct amdgpu_bo));
-
 	bo = kzalloc(sizeof(struct amdgpu_bo), GFP_KERNEL);
 	if (bo == NULL)
 		return -ENOMEM;
@@ -577,8 +573,8 @@ static int amdgpu_bo_do_create(struct amdgpu_device *adev,
 		bo->tbo.priority = 1;
 
 	r = ttm_bo_init_reserved(&adev->mman.bdev, &bo->tbo, size, bp->type,
-				 &bo->placement, page_align, &ctx, acc_size,
-				 NULL, bp->resv, &amdgpu_bo_destroy);
+				 &bo->placement, page_align, &ctx,  NULL,
+				 bp->resv, &amdgpu_bo_destroy);
 	if (unlikely(r != 0))
 		return r;
 
diff --git a/drivers/gpu/drm/drm_gem_vram_helper.c b/drivers/gpu/drm/drm_gem_vram_helper.c
index 0b13c8507688..a0992f0b8afd 100644
--- a/drivers/gpu/drm/drm_gem_vram_helper.c
+++ b/drivers/gpu/drm/drm_gem_vram_helper.c
@@ -189,7 +189,6 @@ struct drm_gem_vram_object *drm_gem_vram_create(struct drm_device *dev,
 	struct drm_vram_mm *vmm = dev->vram_mm;
 	struct ttm_device *bdev;
 	int ret;
-	size_t acc_size;
 
 	if (WARN_ONCE(!vmm, "VRAM MM not initialized"))
 		return ERR_PTR(-EINVAL);
@@ -216,7 +215,6 @@ struct drm_gem_vram_object *drm_gem_vram_create(struct drm_device *dev,
 	}
 
 	bdev = &vmm->bdev;
-	acc_size = ttm_bo_dma_acc_size(bdev, size, sizeof(*gbo));
 
 	gbo->bo.bdev = bdev;
 	drm_gem_vram_placement(gbo, DRM_GEM_VRAM_PL_FLAG_SYSTEM);
@@ -226,8 +224,8 @@ struct drm_gem_vram_object *drm_gem_vram_create(struct drm_device *dev,
 	 * to release gbo->bo.base and kfree gbo.
 	 */
 	ret = ttm_bo_init(bdev, &gbo->bo, size, ttm_bo_type_device,
-			  &gbo->placement, pg_align, false, acc_size,
-			  NULL, NULL, ttm_buffer_object_destroy);
+			  &gbo->placement, pg_align, false, NULL, NULL,
+			  ttm_buffer_object_destroy);
 	if (ret)
 		return ERR_PTR(ret);
 
diff --git a/drivers/gpu/drm/nouveau/nouveau_bo.c b/drivers/gpu/drm/nouveau/nouveau_bo.c
index c177940d6e2c..ca2a8ae1938e 100644
--- a/drivers/gpu/drm/nouveau/nouveau_bo.c
+++ b/drivers/gpu/drm/nouveau/nouveau_bo.c
@@ -300,18 +300,15 @@ nouveau_bo_init(struct nouveau_bo *nvbo, u64 size, int align, u32 domain,
 		struct sg_table *sg, struct dma_resv *robj)
 {
 	int type = sg ? ttm_bo_type_sg : ttm_bo_type_device;
-	size_t acc_size;
 	int ret;
 
-	acc_size = ttm_bo_dma_acc_size(nvbo->bo.bdev, size, sizeof(*nvbo));
-
 	nvbo->bo.mem.num_pages = size >> PAGE_SHIFT;
 	nouveau_bo_placement_set(nvbo, domain, 0);
 	INIT_LIST_HEAD(&nvbo->io_reserve_lru);
 
 	ret = ttm_bo_init(nvbo->bo.bdev, &nvbo->bo, size, type,
-			  &nvbo->placement, align >> PAGE_SHIFT, false,
-			  acc_size, sg, robj, nouveau_bo_del_ttm);
+			  &nvbo->placement, align >> PAGE_SHIFT, false, sg,
+			  robj, nouveau_bo_del_ttm);
 	if (ret) {
 		/* ttm will call nouveau_bo_del_ttm if it fails.. */
 		return ret;
diff --git a/drivers/gpu/drm/nouveau/nouveau_drv.h b/drivers/gpu/drm/nouveau/nouveau_drv.h
index edf9d1ee9d58..a491c2c1c56e 100644
--- a/drivers/gpu/drm/nouveau/nouveau_drv.h
+++ b/drivers/gpu/drm/nouveau/nouveau_drv.h
@@ -54,7 +54,6 @@
 #include <drm/ttm/ttm_bo_api.h>
 #include <drm/ttm/ttm_bo_driver.h>
 #include <drm/ttm/ttm_placement.h>
-#include <drm/ttm/ttm_memory.h>
 
 #include <drm/drm_audio_component.h>
 
diff --git a/drivers/gpu/drm/qxl/qxl_object.c b/drivers/gpu/drm/qxl/qxl_object.c
index ceebc5881f68..705b51535492 100644
--- a/drivers/gpu/drm/qxl/qxl_object.c
+++ b/drivers/gpu/drm/qxl/qxl_object.c
@@ -138,8 +138,8 @@ int qxl_bo_create(struct qxl_device *qdev,
 	qxl_ttm_placement_from_domain(bo, domain);
 
 	r = ttm_bo_init_reserved(&qdev->mman.bdev, &bo->tbo, size, type,
-				 &bo->placement, 0, &ctx, size,
-				 NULL, NULL, &qxl_ttm_bo_destroy);
+				 &bo->placement, 0, &ctx, NULL, NULL,
+				 &qxl_ttm_bo_destroy);
 	if (unlikely(r != 0)) {
 		if (r != -ERESTARTSYS)
 			dev_err(qdev->ddev.dev,
diff --git a/drivers/gpu/drm/radeon/radeon_object.c b/drivers/gpu/drm/radeon/radeon_object.c
index 6a336284466f..804f7a427be7 100644
--- a/drivers/gpu/drm/radeon/radeon_object.c
+++ b/drivers/gpu/drm/radeon/radeon_object.c
@@ -159,7 +159,6 @@ int radeon_bo_create(struct radeon_device *rdev,
 	struct radeon_bo *bo;
 	enum ttm_bo_type type;
 	unsigned long page_align = roundup(byte_align, PAGE_SIZE) >> PAGE_SHIFT;
-	size_t acc_size;
 	int r;
 
 	size = ALIGN(size, PAGE_SIZE);
@@ -173,9 +172,6 @@ int radeon_bo_create(struct radeon_device *rdev,
 	}
 	*bo_ptr = NULL;
 
-	acc_size = ttm_bo_dma_acc_size(&rdev->mman.bdev, size,
-				       sizeof(struct radeon_bo));
-
 	bo = kzalloc(sizeof(struct radeon_bo), GFP_KERNEL);
 	if (bo == NULL)
 		return -ENOMEM;
@@ -230,8 +226,8 @@ int radeon_bo_create(struct radeon_device *rdev,
 	/* Kernel allocation are uninterruptible */
 	down_read(&rdev->pm.mclk_lock);
 	r = ttm_bo_init(&rdev->mman.bdev, &bo->tbo, size, type,
-			&bo->placement, page_align, !kernel, acc_size,
-			sg, resv, &radeon_ttm_bo_destroy);
+			&bo->placement, page_align, !kernel, sg, resv,
+			&radeon_ttm_bo_destroy);
 	up_read(&rdev->pm.mclk_lock);
 	if (unlikely(r != 0)) {
 		return r;
diff --git a/drivers/gpu/drm/ttm/Makefile b/drivers/gpu/drm/ttm/Makefile
index 8e6437eadabe..40e5e9da7953 100644
--- a/drivers/gpu/drm/ttm/Makefile
+++ b/drivers/gpu/drm/ttm/Makefile
@@ -2,10 +2,9 @@
 #
 # Makefile for the drm device driver.  This driver provides support for the
 
-ttm-y := ttm_memory.o ttm_tt.o ttm_bo.o \
-	ttm_bo_util.o ttm_bo_vm.o ttm_module.o \
-	ttm_execbuf_util.o ttm_range_manager.o \
-	ttm_resource.o ttm_pool.o ttm_device.o
+ttm-y := ttm_tt.o ttm_bo.o ttm_bo_util.o ttm_bo_vm.o ttm_module.o \
+	ttm_execbuf_util.o ttm_range_manager.o ttm_resource.o ttm_pool.o \
+	ttm_device.o
 ttm-$(CONFIG_AGP) += ttm_agp_backend.o
 
 obj-$(CONFIG_DRM_TTM) += ttm.o
diff --git a/drivers/gpu/drm/ttm/ttm_bo.c b/drivers/gpu/drm/ttm/ttm_bo.c
index 643befc1a6f2..e38102282fd5 100644
--- a/drivers/gpu/drm/ttm/ttm_bo.c
+++ b/drivers/gpu/drm/ttm/ttm_bo.c
@@ -425,7 +425,6 @@ static void ttm_bo_release(struct kref *kref)
 	struct ttm_buffer_object *bo =
 	    container_of(kref, struct ttm_buffer_object, kref);
 	struct ttm_device *bdev = bo->bdev;
-	size_t acc_size = bo->acc_size;
 	int ret;
 
 	if (!bo->deleted) {
@@ -485,7 +484,6 @@ static void ttm_bo_release(struct kref *kref)
 	if (!ttm_bo_uses_embedded_gem_object(bo))
 		dma_resv_fini(&bo->base._resv);
 	bo->destroy(bo);
-	ttm_mem_global_free(&ttm_mem_glob, acc_size);
 }
 
 void ttm_bo_put(struct ttm_buffer_object *bo)
@@ -1046,25 +1044,13 @@ int ttm_bo_init_reserved(struct ttm_device *bdev,
 			 struct ttm_placement *placement,
 			 uint32_t page_alignment,
 			 struct ttm_operation_ctx *ctx,
-			 size_t acc_size,
 			 struct sg_table *sg,
 			 struct dma_resv *resv,
 			 void (*destroy) (struct ttm_buffer_object *))
 {
-	struct ttm_mem_global *mem_glob = &ttm_mem_glob;
 	bool locked;
 	int ret = 0;
 
-	ret = ttm_mem_global_alloc(mem_glob, acc_size, ctx);
-	if (ret) {
-		pr_err("Out of kernel memory\n");
-		if (destroy)
-			(*destroy)(bo);
-		else
-			kfree(bo);
-		return -ENOMEM;
-	}
-
 	bo->destroy = destroy ? destroy : ttm_bo_default_destroy;
 
 	kref_init(&bo->kref);
@@ -1081,7 +1067,6 @@ int ttm_bo_init_reserved(struct ttm_device *bdev,
 	bo->mem.bus.addr = NULL;
 	bo->moving = NULL;
 	bo->mem.placement = 0;
-	bo->acc_size = acc_size;
 	bo->pin_count = 0;
 	bo->sg = sg;
 	if (resv) {
@@ -1142,7 +1127,6 @@ int ttm_bo_init(struct ttm_device *bdev,
 		struct ttm_placement *placement,
 		uint32_t page_alignment,
 		bool interruptible,
-		size_t acc_size,
 		struct sg_table *sg,
 		struct dma_resv *resv,
 		void (*destroy) (struct ttm_buffer_object *))
@@ -1151,8 +1135,7 @@ int ttm_bo_init(struct ttm_device *bdev,
 	int ret;
 
 	ret = ttm_bo_init_reserved(bdev, bo, size, type, placement,
-				   page_alignment, &ctx, acc_size,
-				   sg, resv, destroy);
+				   page_alignment, &ctx, sg, resv, destroy);
 	if (ret)
 		return ret;
 
@@ -1163,20 +1146,6 @@ int ttm_bo_init(struct ttm_device *bdev,
 }
 EXPORT_SYMBOL(ttm_bo_init);
 
-size_t ttm_bo_dma_acc_size(struct ttm_device *bdev,
-			   unsigned long bo_size,
-			   unsigned struct_size)
-{
-	unsigned npages = (PAGE_ALIGN(bo_size)) >> PAGE_SHIFT;
-	size_t size = 0;
-
-	size += ttm_round_pot(struct_size);
-	size += ttm_round_pot(npages * (2*sizeof(void *) + sizeof(dma_addr_t)));
-	size += ttm_round_pot(sizeof(struct ttm_tt));
-	return size;
-}
-EXPORT_SYMBOL(ttm_bo_dma_acc_size);
-
 /*
  * buffer object vm functions.
  */
diff --git a/drivers/gpu/drm/ttm/ttm_bo_util.c b/drivers/gpu/drm/ttm/ttm_bo_util.c
index db0f2661d504..031e5819fec4 100644
--- a/drivers/gpu/drm/ttm/ttm_bo_util.c
+++ b/drivers/gpu/drm/ttm/ttm_bo_util.c
@@ -309,7 +309,6 @@ static int ttm_buffer_object_transfer(struct ttm_buffer_object *bo,
 
 	kref_init(&fbo->base.kref);
 	fbo->base.destroy = &ttm_transfered_destroy;
-	fbo->base.acc_size = 0;
 	fbo->base.pin_count = 0;
 	if (bo->type != ttm_bo_type_sg)
 		fbo->base.base.resv = &fbo->base.base._resv;
diff --git a/drivers/gpu/drm/ttm/ttm_device.c b/drivers/gpu/drm/ttm/ttm_device.c
index ac0903c9e60a..6bde344e5da7 100644
--- a/drivers/gpu/drm/ttm/ttm_device.c
+++ b/drivers/gpu/drm/ttm/ttm_device.c
@@ -27,9 +27,12 @@
 
 #define pr_fmt(fmt) "[TTM DEVICE] " fmt
 
+#include <linux/mm.h>
+
 #include <drm/ttm/ttm_device.h>
-#include <drm/ttm/ttm_memory.h>
+#include <drm/ttm/ttm_tt.h>
 #include <drm/ttm/ttm_placement.h>
+#include <drm/ttm/ttm_bo_api.h>
 
 #include "ttm_module.h"
 
@@ -49,9 +52,11 @@ static void ttm_global_release(void)
 	if (--ttm_glob_use_count > 0)
 		goto out;
 
+	ttm_pool_mgr_fini();
+	ttm_tt_mgr_fini();
+
 	kobject_del(&glob->kobj);
 	kobject_put(&glob->kobj);
-	ttm_mem_global_release(&ttm_mem_glob);
 	__free_page(glob->dummy_read_page);
 	memset(glob, 0, sizeof(*glob));
 out:
@@ -61,6 +66,8 @@ static void ttm_global_release(void)
 static int ttm_global_init(void)
 {
 	struct ttm_global *glob = &ttm_glob;
+	unsigned long num_pages;
+	struct sysinfo si;
 	int ret = 0;
 	unsigned i;
 
@@ -68,9 +75,14 @@ static int ttm_global_init(void)
 	if (++ttm_glob_use_count > 1)
 		goto out;
 
-	ret = ttm_mem_global_init(&ttm_mem_glob);
-	if (ret)
-		goto out;
+	si_meminfo(&si);
+
+	/* Limit the number of pages in the pool to about 50% of the total
+	 * system memory.
+	 */
+	num_pages = ((u64)si.totalram * si.mem_unit) >> PAGE_SHIFT;
+	ttm_pool_mgr_init(num_pages * 50 / 100);
+	ttm_tt_mgr_init();
 
 	spin_lock_init(&glob->lru_lock);
 	glob->dummy_read_page = alloc_page(__GFP_ZERO | GFP_DMA32);
diff --git a/drivers/gpu/drm/ttm/ttm_pool.c b/drivers/gpu/drm/ttm/ttm_pool.c
index e0617717113f..6b0f957d63d5 100644
--- a/drivers/gpu/drm/ttm/ttm_pool.c
+++ b/drivers/gpu/drm/ttm/ttm_pool.c
@@ -404,16 +404,10 @@ int ttm_pool_alloc(struct ttm_pool *pool, struct ttm_tt *tt,
 			caching = pages + (1 << order);
 		}
 
-		r = ttm_mem_global_alloc_page(&ttm_mem_glob, p,
-					      (1 << order) * PAGE_SIZE,
-					      ctx);
-		if (r)
-			goto error_free_page;
-
 		if (dma_addr) {
 			r = ttm_pool_map(pool, order, p, &dma_addr);
 			if (r)
-				goto error_global_free;
+				goto error_free_page;
 		}
 
 		num_pages -= 1 << order;
@@ -427,9 +421,6 @@ int ttm_pool_alloc(struct ttm_pool *pool, struct ttm_tt *tt,
 
 	return 0;
 
-error_global_free:
-	ttm_mem_global_free_page(&ttm_mem_glob, p, (1 << order) * PAGE_SIZE);
-
 error_free_page:
 	ttm_pool_free_page(pool, tt->caching, order, p);
 
@@ -464,8 +455,6 @@ void ttm_pool_free(struct ttm_pool *pool, struct ttm_tt *tt)
 
 		order = ttm_pool_page_order(pool, p);
 		num_pages = 1ULL << order;
-		ttm_mem_global_free_page(&ttm_mem_glob, p,
-					 num_pages * PAGE_SIZE);
 		if (tt->dma_address)
 			ttm_pool_unmap(pool, tt->dma_address[i], num_pages);
 
diff --git a/drivers/gpu/drm/vmwgfx/Makefile b/drivers/gpu/drm/vmwgfx/Makefile
index cc4cdca7176e..8c02fa5852e7 100644
--- a/drivers/gpu/drm/vmwgfx/Makefile
+++ b/drivers/gpu/drm/vmwgfx/Makefile
@@ -9,7 +9,7 @@ vmwgfx-y := vmwgfx_execbuf.o vmwgfx_gmr.o vmwgfx_kms.o vmwgfx_drv.o \
 	    vmwgfx_cotable.o vmwgfx_so.o vmwgfx_binding.o vmwgfx_msg.o \
 	    vmwgfx_simple_resource.o vmwgfx_va.o vmwgfx_blit.o \
 	    vmwgfx_validation.o vmwgfx_page_dirty.o vmwgfx_streamoutput.o \
-	    ttm_object.o ttm_lock.o
+	    ttm_object.o ttm_lock.o ttm_memory.o
 
 vmwgfx-$(CONFIG_TRANSPARENT_HUGEPAGE) += vmwgfx_thp.o
 obj-$(CONFIG_DRM_VMWGFX) := vmwgfx.o
diff --git a/drivers/gpu/drm/ttm/ttm_memory.c b/drivers/gpu/drm/vmwgfx/ttm_memory.c
similarity index 97%
rename from drivers/gpu/drm/ttm/ttm_memory.c
rename to drivers/gpu/drm/vmwgfx/ttm_memory.c
index 634a85c2dc4c..1306d9e0f095 100644
--- a/drivers/gpu/drm/ttm/ttm_memory.c
+++ b/drivers/gpu/drm/vmwgfx/ttm_memory.c
@@ -28,7 +28,6 @@
 
 #define pr_fmt(fmt) "[TTM] " fmt
 
-#include <drm/ttm/ttm_memory.h>
 #include <linux/spinlock.h>
 #include <linux/sched.h>
 #include <linux/wait.h>
@@ -36,10 +35,11 @@
 #include <linux/module.h>
 #include <linux/slab.h>
 #include <linux/swap.h>
-#include <drm/ttm/ttm_pool.h>
-#include <drm/ttm/ttm_tt.h>
 
-#include "ttm_module.h"
+#include <drm/drm_device.h>
+#include <drm/drm_file.h>
+
+#include "ttm_memory.h"
 
 #define TTM_MEMORY_ALLOC_RETRIES 4
 
@@ -414,7 +414,7 @@ static int ttm_mem_init_dma32_zone(struct ttm_mem_global *glob,
 }
 #endif
 
-int ttm_mem_global_init(struct ttm_mem_global *glob)
+int ttm_mem_global_init(struct ttm_mem_global *glob, struct drm_device *dev)
 {
 	struct sysinfo si;
 	int ret;
@@ -425,7 +425,8 @@ int ttm_mem_global_init(struct ttm_mem_global *glob)
 	glob->swap_queue = create_singlethread_workqueue("ttm_swap");
 	INIT_WORK(&glob->work, ttm_shrink_work);
 	ret = kobject_init_and_add(
-		&glob->kobj, &ttm_mem_glob_kobj_type, ttm_get_kobj(), "memory_accounting");
+		&glob->kobj, &ttm_mem_glob_kobj_type, &dev->primary->kdev->kobj,
+		"memory_accounting");
 	if (unlikely(ret != 0)) {
 		kobject_put(&glob->kobj);
 		return ret;
@@ -453,8 +454,6 @@ int ttm_mem_global_init(struct ttm_mem_global *glob)
 		pr_info("Zone %7s: Available graphics memory: %llu KiB\n",
 			zone->name, (unsigned long long)zone->max_mem >> 10);
 	}
-	ttm_pool_mgr_init(glob->zone_kernel->max_mem/(2*PAGE_SIZE));
-	ttm_tt_mgr_init();
 	return 0;
 out_no_zone:
 	ttm_mem_global_release(glob);
@@ -466,10 +465,6 @@ void ttm_mem_global_release(struct ttm_mem_global *glob)
 	struct ttm_mem_zone *zone;
 	unsigned int i;
 
-	/* let the page allocator first stop the shrink work. */
-	ttm_pool_mgr_fini();
-	ttm_tt_mgr_fini();
-
 	flush_workqueue(glob->swap_queue);
 	destroy_workqueue(glob->swap_queue);
 	glob->swap_queue = NULL;
diff --git a/include/drm/ttm/ttm_memory.h b/drivers/gpu/drm/vmwgfx/ttm_memory.h
similarity index 97%
rename from include/drm/ttm/ttm_memory.h
rename to drivers/gpu/drm/vmwgfx/ttm_memory.h
index c1f167881e33..850ee6c867da 100644
--- a/include/drm/ttm/ttm_memory.h
+++ b/drivers/gpu/drm/vmwgfx/ttm_memory.h
@@ -35,7 +35,8 @@
 #include <linux/errno.h>
 #include <linux/kobject.h>
 #include <linux/mm.h>
-#include "ttm_bo_api.h"
+
+#include <drm/ttm/ttm_bo_api.h>
 
 /**
  * struct ttm_mem_global - Global memory accounting structure.
@@ -79,7 +80,7 @@ extern struct ttm_mem_global {
 #endif
 } ttm_mem_glob;
 
-int ttm_mem_global_init(struct ttm_mem_global *glob);
+int ttm_mem_global_init(struct ttm_mem_global *glob, struct drm_device *dev);
 void ttm_mem_global_release(struct ttm_mem_global *glob);
 int ttm_mem_global_alloc(struct ttm_mem_global *glob, uint64_t memory,
 			 struct ttm_operation_ctx *ctx);
diff --git a/drivers/gpu/drm/vmwgfx/ttm_object.h b/drivers/gpu/drm/vmwgfx/ttm_object.h
index ede26df87c93..49b064f0cb19 100644
--- a/drivers/gpu/drm/vmwgfx/ttm_object.h
+++ b/drivers/gpu/drm/vmwgfx/ttm_object.h
@@ -43,7 +43,8 @@
 #include <linux/rcupdate.h>
 
 #include <drm/drm_hashtab.h>
-#include <drm/ttm/ttm_memory.h>
+
+#include "ttm_memory.h"
 
 /**
  * enum ttm_ref_type
diff --git a/drivers/gpu/drm/vmwgfx/vmwgfx_bo.c b/drivers/gpu/drm/vmwgfx/vmwgfx_bo.c
index 6b3bfd8c678a..50e529a01677 100644
--- a/drivers/gpu/drm/vmwgfx/vmwgfx_bo.c
+++ b/drivers/gpu/drm/vmwgfx/vmwgfx_bo.c
@@ -507,11 +507,16 @@ int vmw_bo_create_kernel(struct vmw_private *dev_priv, unsigned long size,
 	acc_size = ttm_round_pot(sizeof(*bo));
 	acc_size += ttm_round_pot(npages * sizeof(void *));
 	acc_size += ttm_round_pot(sizeof(struct ttm_tt));
+
+	ret = ttm_mem_global_alloc(&ttm_mem_glob, acc_size, &ctx);
+	if (unlikely(ret))
+		goto error_free;
+
 	ret = ttm_bo_init_reserved(&dev_priv->bdev, bo, size,
 				   ttm_bo_type_device, placement, 0,
-				   &ctx, acc_size, NULL, NULL, NULL);
+				   &ctx, NULL, NULL, NULL);
 	if (unlikely(ret))
-		goto error_free;
+		goto error_account;
 
 	ttm_bo_pin(bo);
 	ttm_bo_unreserve(bo);
@@ -519,6 +524,9 @@ int vmw_bo_create_kernel(struct vmw_private *dev_priv, unsigned long size,
 
 	return 0;
 
+error_account:
+	ttm_mem_global_free(&ttm_mem_glob, acc_size);
+
 error_free:
 	kfree(bo);
 	return ret;
@@ -558,11 +566,17 @@ int vmw_bo_init(struct vmw_private *dev_priv,
 	vmw_bo->base.priority = 3;
 	vmw_bo->res_tree = RB_ROOT;
 
+	ret = ttm_mem_global_alloc(&ttm_mem_glob, acc_size, &ctx);
+	if (unlikely(ret))
+		return ret;
+
 	ret = ttm_bo_init_reserved(bdev, &vmw_bo->base, size,
 				   ttm_bo_type_device, placement,
-				   0, &ctx, acc_size, NULL, NULL, bo_free);
-	if (unlikely(ret))
+				   0, &ctx, NULL, NULL, bo_free);
+	if (unlikely(ret)) {
+		ttm_mem_global_free(&ttm_mem_glob, acc_size);
 		return ret;
+	}
 
 	if (pin)
 		ttm_bo_pin(&vmw_bo->base);
diff --git a/drivers/gpu/drm/vmwgfx/vmwgfx_drv.c b/drivers/gpu/drm/vmwgfx/vmwgfx_drv.c
index 710ba5169a74..6c0ca1011629 100644
--- a/drivers/gpu/drm/vmwgfx/vmwgfx_drv.c
+++ b/drivers/gpu/drm/vmwgfx/vmwgfx_drv.c
@@ -1268,6 +1268,7 @@ static void vmw_remove(struct pci_dev *pdev)
 {
 	struct drm_device *dev = pci_get_drvdata(pdev);
 
+	ttm_mem_global_release(&ttm_mem_glob);
 	drm_dev_unregister(dev);
 	vmw_driver_unload(dev);
 }
@@ -1518,6 +1519,10 @@ static int vmw_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
 
 	pci_set_drvdata(pdev, &vmw->drm);
 
+	ret = ttm_mem_global_init(&ttm_mem_glob, &vmw->drm);
+	if (ret)
+		return ret;
+
 	ret = vmw_driver_load(vmw, ent->device);
 	if (ret)
 		return ret;
diff --git a/drivers/gpu/drm/vmwgfx/vmwgfx_ttm_buffer.c b/drivers/gpu/drm/vmwgfx/vmwgfx_ttm_buffer.c
index d1bfa59579f1..63f10c865061 100644
--- a/drivers/gpu/drm/vmwgfx/vmwgfx_ttm_buffer.c
+++ b/drivers/gpu/drm/vmwgfx/vmwgfx_ttm_buffer.c
@@ -576,11 +576,31 @@ static void vmw_ttm_destroy(struct ttm_device *bdev, struct ttm_tt *ttm)
 static int vmw_ttm_populate(struct ttm_device *bdev,
 			    struct ttm_tt *ttm, struct ttm_operation_ctx *ctx)
 {
+	unsigned int i;
+	int ret;
+
 	/* TODO: maybe completely drop this ? */
 	if (ttm_tt_is_populated(ttm))
 		return 0;
 
-	return ttm_pool_alloc(&bdev->pool, ttm, ctx);
+	ret = ttm_pool_alloc(&bdev->pool, ttm, ctx);
+	if (ret)
+		return ret;
+
+	for (i = 0; i < ttm->num_pages; ++i) {
+		ret = ttm_mem_global_alloc_page(&ttm_mem_glob, ttm->pages[i],
+						PAGE_SIZE, ctx);
+		if (ret)
+			goto error;
+	}
+	return 0;
+
+error:
+	while (i--)
+		ttm_mem_global_free_page(&ttm_mem_glob, ttm->pages[i],
+					 PAGE_SIZE);
+	ttm_pool_free(&bdev->pool, ttm);
+	return ret;
 }
 
 static void vmw_ttm_unpopulate(struct ttm_device *bdev,
@@ -588,6 +608,7 @@ static void vmw_ttm_unpopulate(struct ttm_device *bdev,
 {
 	struct vmw_ttm_tt *vmw_tt = container_of(ttm, struct vmw_ttm_tt,
 						 dma_ttm);
+	unsigned int i;
 
 	if (vmw_tt->mob) {
 		vmw_mob_destroy(vmw_tt->mob);
@@ -595,6 +616,11 @@ static void vmw_ttm_unpopulate(struct ttm_device *bdev,
 	}
 
 	vmw_ttm_unmap_dma(vmw_tt);
+
+	for (i = 0; i < ttm->num_pages; ++i)
+		ttm_mem_global_free_page(&ttm_mem_glob, ttm->pages[i],
+					 PAGE_SIZE);
+
 	ttm_pool_free(&bdev->pool, ttm);
 }
 
diff --git a/include/drm/ttm/ttm_bo_api.h b/include/drm/ttm/ttm_bo_api.h
index 1297a8fb7ccb..4fb523dfab32 100644
--- a/include/drm/ttm/ttm_bo_api.h
+++ b/include/drm/ttm/ttm_bo_api.h
@@ -88,7 +88,6 @@ struct ttm_tt;
  * @type: The bo type.
  * @destroy: Destruction function. If NULL, kfree is used.
  * @num_pages: Actual number of pages.
- * @acc_size: Accounted size for this object.
  * @kref: Reference count of this buffer object. When this refcount reaches
  * zero, the object is destroyed or put on the delayed delete list.
  * @mem: structure describing current placement.
@@ -125,7 +124,6 @@ struct ttm_buffer_object {
 	struct ttm_device *bdev;
 	enum ttm_bo_type type;
 	void (*destroy) (struct ttm_buffer_object *);
-	size_t acc_size;
 
 	/**
 	* Members not needing protection.
@@ -357,10 +355,6 @@ void ttm_bo_unlock_delayed_workqueue(struct ttm_device *bdev, int resched);
 bool ttm_bo_eviction_valuable(struct ttm_buffer_object *bo,
 			      const struct ttm_place *place);
 
-size_t ttm_bo_dma_acc_size(struct ttm_device *bdev,
-			   unsigned long bo_size,
-			   unsigned struct_size);
-
 /**
  * ttm_bo_init_reserved
  *
@@ -371,7 +365,6 @@ size_t ttm_bo_dma_acc_size(struct ttm_device *bdev,
  * @flags: Initial placement flags.
  * @page_alignment: Data alignment in pages.
  * @ctx: TTM operation context for memory allocation.
- * @acc_size: Accounted size for this object.
  * @resv: Pointer to a dma_resv, or NULL to let ttm allocate one.
  * @destroy: Destroy function. Use NULL for kfree().
  *
@@ -402,8 +395,7 @@ int ttm_bo_init_reserved(struct ttm_device *bdev,
 			 struct ttm_placement *placement,
 			 uint32_t page_alignment,
 			 struct ttm_operation_ctx *ctx,
-			 size_t acc_size, struct sg_table *sg,
-			 struct dma_resv *resv,
+			 struct sg_table *sg, struct dma_resv *resv,
 			 void (*destroy) (struct ttm_buffer_object *));
 
 /**
@@ -421,7 +413,6 @@ int ttm_bo_init_reserved(struct ttm_device *bdev,
  * holds a pointer to a persistent shmem object. Typically, this would
  * point to the shmem object backing a GEM object if TTM is used to back a
  * GEM user interface.
- * @acc_size: Accounted size for this object.
  * @resv: Pointer to a dma_resv, or NULL to let ttm allocate one.
  * @destroy: Destroy function. Use NULL for kfree().
  *
@@ -446,7 +437,7 @@ int ttm_bo_init_reserved(struct ttm_device *bdev,
 int ttm_bo_init(struct ttm_device *bdev, struct ttm_buffer_object *bo,
 		size_t size, enum ttm_bo_type type,
 		struct ttm_placement *placement,
-		uint32_t page_alignment, bool interrubtible, size_t acc_size,
+		uint32_t page_alignment, bool interrubtible,
 		struct sg_table *sg, struct dma_resv *resv,
 		void (*destroy) (struct ttm_buffer_object *));
 
diff --git a/include/drm/ttm/ttm_bo_driver.h b/include/drm/ttm/ttm_bo_driver.h
index 1c9bf993e252..8959c0075cfd 100644
--- a/include/drm/ttm/ttm_bo_driver.h
+++ b/include/drm/ttm/ttm_bo_driver.h
@@ -40,7 +40,6 @@
 #include <drm/ttm/ttm_device.h>
 
 #include "ttm_bo_api.h"
-#include "ttm_memory.h"
 #include "ttm_placement.h"
 #include "ttm_tt.h"
 #include "ttm_pool.h"
diff --git a/include/drm/ttm/ttm_tt.h b/include/drm/ttm/ttm_tt.h
index cce57fb49e2c..069f8130241a 100644
--- a/include/drm/ttm/ttm_tt.h
+++ b/include/drm/ttm/ttm_tt.h
@@ -30,6 +30,7 @@
 #include <linux/types.h>
 #include <drm/ttm/ttm_caching.h>
 
+struct ttm_bo_device;
 struct ttm_tt;
 struct ttm_resource;
 struct ttm_buffer_object;
-- 
2.25.1

_______________________________________________
dri-devel mailing list
dri-devel@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/dri-devel

^ permalink raw reply related	[flat|nested] 17+ messages in thread

* [PATCH 3/3] drm/ttm: drop sysfs directory
  2021-01-28 13:16 [PATCH 1/3] drm/ttm: rework ttm_tt page limit v3 Christian König
  2021-01-28 13:16 ` [PATCH 2/3] drm/ttm: move memory accounting into vmwgfx v3 Christian König
@ 2021-01-28 13:16 ` Christian König
  2021-02-03 11:28   ` Daniel Vetter
  2021-02-03 11:26 ` [PATCH 1/3] drm/ttm: rework ttm_tt page limit v3 Daniel Vetter
  2 siblings, 1 reply; 17+ messages in thread
From: Christian König @ 2021-01-28 13:16 UTC (permalink / raw)
  To: sroland, zackr, daniel, linux-graphics-maintainer, dri-devel

Not used any more.

Signed-off-by: Christian König <christian.koenig@amd.com>
---
 drivers/gpu/drm/ttm/ttm_module.c | 50 --------------------------------
 drivers/gpu/drm/ttm/ttm_module.h |  2 --
 2 files changed, 52 deletions(-)

diff --git a/drivers/gpu/drm/ttm/ttm_module.c b/drivers/gpu/drm/ttm/ttm_module.c
index f6566603a60f..56b0efdba1a9 100644
--- a/drivers/gpu/drm/ttm/ttm_module.c
+++ b/drivers/gpu/drm/ttm/ttm_module.c
@@ -37,66 +37,16 @@
 
 #include "ttm_module.h"
 
-static DECLARE_WAIT_QUEUE_HEAD(exit_q);
-static atomic_t device_released;
 struct dentry *ttm_debugfs_root;
 
-static struct device_type ttm_drm_class_type = {
-	.name = "ttm",
-	/**
-	 * Add pm ops here.
-	 */
-};
-
-static void ttm_drm_class_device_release(struct device *dev)
-{
-	atomic_set(&device_released, 1);
-	wake_up_all(&exit_q);
-}
-
-static struct device ttm_drm_class_device = {
-	.type = &ttm_drm_class_type,
-	.release = &ttm_drm_class_device_release
-};
-
-struct kobject *ttm_get_kobj(void)
-{
-	struct kobject *kobj = &ttm_drm_class_device.kobj;
-	BUG_ON(kobj == NULL);
-	return kobj;
-}
-
 static int __init ttm_init(void)
 {
-	int ret;
-
-	ret = dev_set_name(&ttm_drm_class_device, "ttm");
-	if (unlikely(ret != 0))
-		return ret;
-
-	atomic_set(&device_released, 0);
-	ret = drm_class_device_register(&ttm_drm_class_device);
-	if (unlikely(ret != 0))
-		goto out_no_dev_reg;
-
 	ttm_debugfs_root = debugfs_create_dir("ttm", NULL);
 	return 0;
-out_no_dev_reg:
-	atomic_set(&device_released, 1);
-	wake_up_all(&exit_q);
-	return ret;
 }
 
 static void __exit ttm_exit(void)
 {
-	drm_class_device_unregister(&ttm_drm_class_device);
-
-	/**
-	 * Refuse to unload until the TTM device is released.
-	 * Not sure this is 100% needed.
-	 */
-
-	wait_event(exit_q, atomic_read(&device_released) == 1);
 	debugfs_remove(ttm_debugfs_root);
 }
 
diff --git a/drivers/gpu/drm/ttm/ttm_module.h b/drivers/gpu/drm/ttm/ttm_module.h
index 2f03c2fcf570..d7cac5d4b835 100644
--- a/drivers/gpu/drm/ttm/ttm_module.h
+++ b/drivers/gpu/drm/ttm/ttm_module.h
@@ -33,10 +33,8 @@
 
 #define TTM_PFX "[TTM] "
 
-struct kobject;
 struct dentry;
 
-extern struct kobject *ttm_get_kobj(void);
 extern struct dentry *ttm_debugfs_root;
 
 #endif /* _TTM_MODULE_H_ */
-- 
2.25.1

_______________________________________________
dri-devel mailing list
dri-devel@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/dri-devel

^ permalink raw reply related	[flat|nested] 17+ messages in thread

* Re: [PATCH 2/3] drm/ttm: move memory accounting into vmwgfx v3
  2021-01-28 13:16 ` [PATCH 2/3] drm/ttm: move memory accounting into vmwgfx v3 Christian König
@ 2021-02-02 13:04   ` Christian König
  2021-02-02 15:14     ` Zack Rusin
  0 siblings, 1 reply; 17+ messages in thread
From: Christian König @ 2021-02-02 13:04 UTC (permalink / raw)
  To: sroland, zackr, daniel, linux-graphics-maintainer, dri-devel

Ping?

Especially Roland and Zack do you have any objections to this?

Regards,
Christian.

Am 28.01.21 um 14:16 schrieb Christian König:
> This is just another feature which is only used by VMWGFX, so move
> it into the driver instead.
>
> I've tried to add the accounting sysfs file to the kobject of the drm
> minor, but I'm not 100% sure if this works as expected.
>
> v2: fix typo in KFD and avoid 64bit divide
> v3: fix init order in VMWGFX
>
> Signed-off-by: Christian König <christian.koenig@amd.com>
> ---
>   .../gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c  | 16 ++++++---
>   drivers/gpu/drm/amd/amdgpu/amdgpu_object.c    |  8 ++---
>   drivers/gpu/drm/drm_gem_vram_helper.c         |  6 ++--
>   drivers/gpu/drm/nouveau/nouveau_bo.c          |  7 ++--
>   drivers/gpu/drm/nouveau/nouveau_drv.h         |  1 -
>   drivers/gpu/drm/qxl/qxl_object.c              |  4 +--
>   drivers/gpu/drm/radeon/radeon_object.c        |  8 ++---
>   drivers/gpu/drm/ttm/Makefile                  |  7 ++--
>   drivers/gpu/drm/ttm/ttm_bo.c                  | 33 +------------------
>   drivers/gpu/drm/ttm/ttm_bo_util.c             |  1 -
>   drivers/gpu/drm/ttm/ttm_device.c              | 22 ++++++++++---
>   drivers/gpu/drm/ttm/ttm_pool.c                | 13 +-------
>   drivers/gpu/drm/vmwgfx/Makefile               |  2 +-
>   drivers/gpu/drm/{ttm => vmwgfx}/ttm_memory.c  | 19 ++++-------
>   .../gpu/drm/vmwgfx}/ttm_memory.h              |  5 +--
>   drivers/gpu/drm/vmwgfx/ttm_object.h           |  3 +-
>   drivers/gpu/drm/vmwgfx/vmwgfx_bo.c            | 22 ++++++++++---
>   drivers/gpu/drm/vmwgfx/vmwgfx_drv.c           |  5 +++
>   drivers/gpu/drm/vmwgfx/vmwgfx_ttm_buffer.c    | 28 +++++++++++++++-
>   include/drm/ttm/ttm_bo_api.h                  | 13 ++------
>   include/drm/ttm/ttm_bo_driver.h               |  1 -
>   include/drm/ttm/ttm_tt.h                      |  1 +
>   22 files changed, 110 insertions(+), 115 deletions(-)
>   rename drivers/gpu/drm/{ttm => vmwgfx}/ttm_memory.c (97%)
>   rename {include/drm/ttm => drivers/gpu/drm/vmwgfx}/ttm_memory.h (97%)
>
> diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c b/drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c
> index 0849b68e784f..e440af37dde8 100644
> --- a/drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c
> +++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c
> @@ -118,6 +118,16 @@ void amdgpu_amdkfd_gpuvm_init_mem_limits(void)
>    */
>   #define ESTIMATE_PT_SIZE(mem_size) ((mem_size) >> 14)
>   
> +static size_t amdgpu_amdkfd_acc_size(uint64_t size)
> +{
> +	size >>= PAGE_SHIFT;
> +	size *= sizeof(dma_addr_t) + sizeof(void *);
> +
> +	return __roundup_pow_of_two(sizeof(struct amdgpu_bo)) +
> +		__roundup_pow_of_two(sizeof(struct ttm_tt)) +
> +		PAGE_ALIGN(size);
> +}
> +
>   static int amdgpu_amdkfd_reserve_mem_limit(struct amdgpu_device *adev,
>   		uint64_t size, u32 domain, bool sg)
>   {
> @@ -126,8 +136,7 @@ static int amdgpu_amdkfd_reserve_mem_limit(struct amdgpu_device *adev,
>   	size_t acc_size, system_mem_needed, ttm_mem_needed, vram_needed;
>   	int ret = 0;
>   
> -	acc_size = ttm_bo_dma_acc_size(&adev->mman.bdev, size,
> -				       sizeof(struct amdgpu_bo));
> +	acc_size = amdgpu_amdkfd_acc_size(size);
>   
>   	vram_needed = 0;
>   	if (domain == AMDGPU_GEM_DOMAIN_GTT) {
> @@ -174,8 +183,7 @@ static void unreserve_mem_limit(struct amdgpu_device *adev,
>   {
>   	size_t acc_size;
>   
> -	acc_size = ttm_bo_dma_acc_size(&adev->mman.bdev, size,
> -				       sizeof(struct amdgpu_bo));
> +	acc_size = amdgpu_amdkfd_acc_size(size);
>   
>   	spin_lock(&kfd_mem_limit.mem_limit_lock);
>   	if (domain == AMDGPU_GEM_DOMAIN_GTT) {
> diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_object.c b/drivers/gpu/drm/amd/amdgpu/amdgpu_object.c
> index 6cc9919b12cc..599c9a132eb6 100644
> --- a/drivers/gpu/drm/amd/amdgpu/amdgpu_object.c
> +++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_object.c
> @@ -523,7 +523,6 @@ static int amdgpu_bo_do_create(struct amdgpu_device *adev,
>   	};
>   	struct amdgpu_bo *bo;
>   	unsigned long page_align, size = bp->size;
> -	size_t acc_size;
>   	int r;
>   
>   	/* Note that GDS/GWS/OA allocates 1 page per byte/resource. */
> @@ -546,9 +545,6 @@ static int amdgpu_bo_do_create(struct amdgpu_device *adev,
>   
>   	*bo_ptr = NULL;
>   
> -	acc_size = ttm_bo_dma_acc_size(&adev->mman.bdev, size,
> -				       sizeof(struct amdgpu_bo));
> -
>   	bo = kzalloc(sizeof(struct amdgpu_bo), GFP_KERNEL);
>   	if (bo == NULL)
>   		return -ENOMEM;
> @@ -577,8 +573,8 @@ static int amdgpu_bo_do_create(struct amdgpu_device *adev,
>   		bo->tbo.priority = 1;
>   
>   	r = ttm_bo_init_reserved(&adev->mman.bdev, &bo->tbo, size, bp->type,
> -				 &bo->placement, page_align, &ctx, acc_size,
> -				 NULL, bp->resv, &amdgpu_bo_destroy);
> +				 &bo->placement, page_align, &ctx,  NULL,
> +				 bp->resv, &amdgpu_bo_destroy);
>   	if (unlikely(r != 0))
>   		return r;
>   
> diff --git a/drivers/gpu/drm/drm_gem_vram_helper.c b/drivers/gpu/drm/drm_gem_vram_helper.c
> index 0b13c8507688..a0992f0b8afd 100644
> --- a/drivers/gpu/drm/drm_gem_vram_helper.c
> +++ b/drivers/gpu/drm/drm_gem_vram_helper.c
> @@ -189,7 +189,6 @@ struct drm_gem_vram_object *drm_gem_vram_create(struct drm_device *dev,
>   	struct drm_vram_mm *vmm = dev->vram_mm;
>   	struct ttm_device *bdev;
>   	int ret;
> -	size_t acc_size;
>   
>   	if (WARN_ONCE(!vmm, "VRAM MM not initialized"))
>   		return ERR_PTR(-EINVAL);
> @@ -216,7 +215,6 @@ struct drm_gem_vram_object *drm_gem_vram_create(struct drm_device *dev,
>   	}
>   
>   	bdev = &vmm->bdev;
> -	acc_size = ttm_bo_dma_acc_size(bdev, size, sizeof(*gbo));
>   
>   	gbo->bo.bdev = bdev;
>   	drm_gem_vram_placement(gbo, DRM_GEM_VRAM_PL_FLAG_SYSTEM);
> @@ -226,8 +224,8 @@ struct drm_gem_vram_object *drm_gem_vram_create(struct drm_device *dev,
>   	 * to release gbo->bo.base and kfree gbo.
>   	 */
>   	ret = ttm_bo_init(bdev, &gbo->bo, size, ttm_bo_type_device,
> -			  &gbo->placement, pg_align, false, acc_size,
> -			  NULL, NULL, ttm_buffer_object_destroy);
> +			  &gbo->placement, pg_align, false, NULL, NULL,
> +			  ttm_buffer_object_destroy);
>   	if (ret)
>   		return ERR_PTR(ret);
>   
> diff --git a/drivers/gpu/drm/nouveau/nouveau_bo.c b/drivers/gpu/drm/nouveau/nouveau_bo.c
> index c177940d6e2c..ca2a8ae1938e 100644
> --- a/drivers/gpu/drm/nouveau/nouveau_bo.c
> +++ b/drivers/gpu/drm/nouveau/nouveau_bo.c
> @@ -300,18 +300,15 @@ nouveau_bo_init(struct nouveau_bo *nvbo, u64 size, int align, u32 domain,
>   		struct sg_table *sg, struct dma_resv *robj)
>   {
>   	int type = sg ? ttm_bo_type_sg : ttm_bo_type_device;
> -	size_t acc_size;
>   	int ret;
>   
> -	acc_size = ttm_bo_dma_acc_size(nvbo->bo.bdev, size, sizeof(*nvbo));
> -
>   	nvbo->bo.mem.num_pages = size >> PAGE_SHIFT;
>   	nouveau_bo_placement_set(nvbo, domain, 0);
>   	INIT_LIST_HEAD(&nvbo->io_reserve_lru);
>   
>   	ret = ttm_bo_init(nvbo->bo.bdev, &nvbo->bo, size, type,
> -			  &nvbo->placement, align >> PAGE_SHIFT, false,
> -			  acc_size, sg, robj, nouveau_bo_del_ttm);
> +			  &nvbo->placement, align >> PAGE_SHIFT, false, sg,
> +			  robj, nouveau_bo_del_ttm);
>   	if (ret) {
>   		/* ttm will call nouveau_bo_del_ttm if it fails.. */
>   		return ret;
> diff --git a/drivers/gpu/drm/nouveau/nouveau_drv.h b/drivers/gpu/drm/nouveau/nouveau_drv.h
> index edf9d1ee9d58..a491c2c1c56e 100644
> --- a/drivers/gpu/drm/nouveau/nouveau_drv.h
> +++ b/drivers/gpu/drm/nouveau/nouveau_drv.h
> @@ -54,7 +54,6 @@
>   #include <drm/ttm/ttm_bo_api.h>
>   #include <drm/ttm/ttm_bo_driver.h>
>   #include <drm/ttm/ttm_placement.h>
> -#include <drm/ttm/ttm_memory.h>
>   
>   #include <drm/drm_audio_component.h>
>   
> diff --git a/drivers/gpu/drm/qxl/qxl_object.c b/drivers/gpu/drm/qxl/qxl_object.c
> index ceebc5881f68..705b51535492 100644
> --- a/drivers/gpu/drm/qxl/qxl_object.c
> +++ b/drivers/gpu/drm/qxl/qxl_object.c
> @@ -138,8 +138,8 @@ int qxl_bo_create(struct qxl_device *qdev,
>   	qxl_ttm_placement_from_domain(bo, domain);
>   
>   	r = ttm_bo_init_reserved(&qdev->mman.bdev, &bo->tbo, size, type,
> -				 &bo->placement, 0, &ctx, size,
> -				 NULL, NULL, &qxl_ttm_bo_destroy);
> +				 &bo->placement, 0, &ctx, NULL, NULL,
> +				 &qxl_ttm_bo_destroy);
>   	if (unlikely(r != 0)) {
>   		if (r != -ERESTARTSYS)
>   			dev_err(qdev->ddev.dev,
> diff --git a/drivers/gpu/drm/radeon/radeon_object.c b/drivers/gpu/drm/radeon/radeon_object.c
> index 6a336284466f..804f7a427be7 100644
> --- a/drivers/gpu/drm/radeon/radeon_object.c
> +++ b/drivers/gpu/drm/radeon/radeon_object.c
> @@ -159,7 +159,6 @@ int radeon_bo_create(struct radeon_device *rdev,
>   	struct radeon_bo *bo;
>   	enum ttm_bo_type type;
>   	unsigned long page_align = roundup(byte_align, PAGE_SIZE) >> PAGE_SHIFT;
> -	size_t acc_size;
>   	int r;
>   
>   	size = ALIGN(size, PAGE_SIZE);
> @@ -173,9 +172,6 @@ int radeon_bo_create(struct radeon_device *rdev,
>   	}
>   	*bo_ptr = NULL;
>   
> -	acc_size = ttm_bo_dma_acc_size(&rdev->mman.bdev, size,
> -				       sizeof(struct radeon_bo));
> -
>   	bo = kzalloc(sizeof(struct radeon_bo), GFP_KERNEL);
>   	if (bo == NULL)
>   		return -ENOMEM;
> @@ -230,8 +226,8 @@ int radeon_bo_create(struct radeon_device *rdev,
>   	/* Kernel allocation are uninterruptible */
>   	down_read(&rdev->pm.mclk_lock);
>   	r = ttm_bo_init(&rdev->mman.bdev, &bo->tbo, size, type,
> -			&bo->placement, page_align, !kernel, acc_size,
> -			sg, resv, &radeon_ttm_bo_destroy);
> +			&bo->placement, page_align, !kernel, sg, resv,
> +			&radeon_ttm_bo_destroy);
>   	up_read(&rdev->pm.mclk_lock);
>   	if (unlikely(r != 0)) {
>   		return r;
> diff --git a/drivers/gpu/drm/ttm/Makefile b/drivers/gpu/drm/ttm/Makefile
> index 8e6437eadabe..40e5e9da7953 100644
> --- a/drivers/gpu/drm/ttm/Makefile
> +++ b/drivers/gpu/drm/ttm/Makefile
> @@ -2,10 +2,9 @@
>   #
>   # Makefile for the drm device driver.  This driver provides support for the
>   
> -ttm-y := ttm_memory.o ttm_tt.o ttm_bo.o \
> -	ttm_bo_util.o ttm_bo_vm.o ttm_module.o \
> -	ttm_execbuf_util.o ttm_range_manager.o \
> -	ttm_resource.o ttm_pool.o ttm_device.o
> +ttm-y := ttm_tt.o ttm_bo.o ttm_bo_util.o ttm_bo_vm.o ttm_module.o \
> +	ttm_execbuf_util.o ttm_range_manager.o ttm_resource.o ttm_pool.o \
> +	ttm_device.o
>   ttm-$(CONFIG_AGP) += ttm_agp_backend.o
>   
>   obj-$(CONFIG_DRM_TTM) += ttm.o
> diff --git a/drivers/gpu/drm/ttm/ttm_bo.c b/drivers/gpu/drm/ttm/ttm_bo.c
> index 643befc1a6f2..e38102282fd5 100644
> --- a/drivers/gpu/drm/ttm/ttm_bo.c
> +++ b/drivers/gpu/drm/ttm/ttm_bo.c
> @@ -425,7 +425,6 @@ static void ttm_bo_release(struct kref *kref)
>   	struct ttm_buffer_object *bo =
>   	    container_of(kref, struct ttm_buffer_object, kref);
>   	struct ttm_device *bdev = bo->bdev;
> -	size_t acc_size = bo->acc_size;
>   	int ret;
>   
>   	if (!bo->deleted) {
> @@ -485,7 +484,6 @@ static void ttm_bo_release(struct kref *kref)
>   	if (!ttm_bo_uses_embedded_gem_object(bo))
>   		dma_resv_fini(&bo->base._resv);
>   	bo->destroy(bo);
> -	ttm_mem_global_free(&ttm_mem_glob, acc_size);
>   }
>   
>   void ttm_bo_put(struct ttm_buffer_object *bo)
> @@ -1046,25 +1044,13 @@ int ttm_bo_init_reserved(struct ttm_device *bdev,
>   			 struct ttm_placement *placement,
>   			 uint32_t page_alignment,
>   			 struct ttm_operation_ctx *ctx,
> -			 size_t acc_size,
>   			 struct sg_table *sg,
>   			 struct dma_resv *resv,
>   			 void (*destroy) (struct ttm_buffer_object *))
>   {
> -	struct ttm_mem_global *mem_glob = &ttm_mem_glob;
>   	bool locked;
>   	int ret = 0;
>   
> -	ret = ttm_mem_global_alloc(mem_glob, acc_size, ctx);
> -	if (ret) {
> -		pr_err("Out of kernel memory\n");
> -		if (destroy)
> -			(*destroy)(bo);
> -		else
> -			kfree(bo);
> -		return -ENOMEM;
> -	}
> -
>   	bo->destroy = destroy ? destroy : ttm_bo_default_destroy;
>   
>   	kref_init(&bo->kref);
> @@ -1081,7 +1067,6 @@ int ttm_bo_init_reserved(struct ttm_device *bdev,
>   	bo->mem.bus.addr = NULL;
>   	bo->moving = NULL;
>   	bo->mem.placement = 0;
> -	bo->acc_size = acc_size;
>   	bo->pin_count = 0;
>   	bo->sg = sg;
>   	if (resv) {
> @@ -1142,7 +1127,6 @@ int ttm_bo_init(struct ttm_device *bdev,
>   		struct ttm_placement *placement,
>   		uint32_t page_alignment,
>   		bool interruptible,
> -		size_t acc_size,
>   		struct sg_table *sg,
>   		struct dma_resv *resv,
>   		void (*destroy) (struct ttm_buffer_object *))
> @@ -1151,8 +1135,7 @@ int ttm_bo_init(struct ttm_device *bdev,
>   	int ret;
>   
>   	ret = ttm_bo_init_reserved(bdev, bo, size, type, placement,
> -				   page_alignment, &ctx, acc_size,
> -				   sg, resv, destroy);
> +				   page_alignment, &ctx, sg, resv, destroy);
>   	if (ret)
>   		return ret;
>   
> @@ -1163,20 +1146,6 @@ int ttm_bo_init(struct ttm_device *bdev,
>   }
>   EXPORT_SYMBOL(ttm_bo_init);
>   
> -size_t ttm_bo_dma_acc_size(struct ttm_device *bdev,
> -			   unsigned long bo_size,
> -			   unsigned struct_size)
> -{
> -	unsigned npages = (PAGE_ALIGN(bo_size)) >> PAGE_SHIFT;
> -	size_t size = 0;
> -
> -	size += ttm_round_pot(struct_size);
> -	size += ttm_round_pot(npages * (2*sizeof(void *) + sizeof(dma_addr_t)));
> -	size += ttm_round_pot(sizeof(struct ttm_tt));
> -	return size;
> -}
> -EXPORT_SYMBOL(ttm_bo_dma_acc_size);
> -
>   /*
>    * buffer object vm functions.
>    */
> diff --git a/drivers/gpu/drm/ttm/ttm_bo_util.c b/drivers/gpu/drm/ttm/ttm_bo_util.c
> index db0f2661d504..031e5819fec4 100644
> --- a/drivers/gpu/drm/ttm/ttm_bo_util.c
> +++ b/drivers/gpu/drm/ttm/ttm_bo_util.c
> @@ -309,7 +309,6 @@ static int ttm_buffer_object_transfer(struct ttm_buffer_object *bo,
>   
>   	kref_init(&fbo->base.kref);
>   	fbo->base.destroy = &ttm_transfered_destroy;
> -	fbo->base.acc_size = 0;
>   	fbo->base.pin_count = 0;
>   	if (bo->type != ttm_bo_type_sg)
>   		fbo->base.base.resv = &fbo->base.base._resv;
> diff --git a/drivers/gpu/drm/ttm/ttm_device.c b/drivers/gpu/drm/ttm/ttm_device.c
> index ac0903c9e60a..6bde344e5da7 100644
> --- a/drivers/gpu/drm/ttm/ttm_device.c
> +++ b/drivers/gpu/drm/ttm/ttm_device.c
> @@ -27,9 +27,12 @@
>   
>   #define pr_fmt(fmt) "[TTM DEVICE] " fmt
>   
> +#include <linux/mm.h>
> +
>   #include <drm/ttm/ttm_device.h>
> -#include <drm/ttm/ttm_memory.h>
> +#include <drm/ttm/ttm_tt.h>
>   #include <drm/ttm/ttm_placement.h>
> +#include <drm/ttm/ttm_bo_api.h>
>   
>   #include "ttm_module.h"
>   
> @@ -49,9 +52,11 @@ static void ttm_global_release(void)
>   	if (--ttm_glob_use_count > 0)
>   		goto out;
>   
> +	ttm_pool_mgr_fini();
> +	ttm_tt_mgr_fini();
> +
>   	kobject_del(&glob->kobj);
>   	kobject_put(&glob->kobj);
> -	ttm_mem_global_release(&ttm_mem_glob);
>   	__free_page(glob->dummy_read_page);
>   	memset(glob, 0, sizeof(*glob));
>   out:
> @@ -61,6 +66,8 @@ static void ttm_global_release(void)
>   static int ttm_global_init(void)
>   {
>   	struct ttm_global *glob = &ttm_glob;
> +	unsigned long num_pages;
> +	struct sysinfo si;
>   	int ret = 0;
>   	unsigned i;
>   
> @@ -68,9 +75,14 @@ static int ttm_global_init(void)
>   	if (++ttm_glob_use_count > 1)
>   		goto out;
>   
> -	ret = ttm_mem_global_init(&ttm_mem_glob);
> -	if (ret)
> -		goto out;
> +	si_meminfo(&si);
> +
> +	/* Limit the number of pages in the pool to about 50% of the total
> +	 * system memory.
> +	 */
> +	num_pages = ((u64)si.totalram * si.mem_unit) >> PAGE_SHIFT;
> +	ttm_pool_mgr_init(num_pages * 50 / 100);
> +	ttm_tt_mgr_init();
>   
>   	spin_lock_init(&glob->lru_lock);
>   	glob->dummy_read_page = alloc_page(__GFP_ZERO | GFP_DMA32);
> diff --git a/drivers/gpu/drm/ttm/ttm_pool.c b/drivers/gpu/drm/ttm/ttm_pool.c
> index e0617717113f..6b0f957d63d5 100644
> --- a/drivers/gpu/drm/ttm/ttm_pool.c
> +++ b/drivers/gpu/drm/ttm/ttm_pool.c
> @@ -404,16 +404,10 @@ int ttm_pool_alloc(struct ttm_pool *pool, struct ttm_tt *tt,
>   			caching = pages + (1 << order);
>   		}
>   
> -		r = ttm_mem_global_alloc_page(&ttm_mem_glob, p,
> -					      (1 << order) * PAGE_SIZE,
> -					      ctx);
> -		if (r)
> -			goto error_free_page;
> -
>   		if (dma_addr) {
>   			r = ttm_pool_map(pool, order, p, &dma_addr);
>   			if (r)
> -				goto error_global_free;
> +				goto error_free_page;
>   		}
>   
>   		num_pages -= 1 << order;
> @@ -427,9 +421,6 @@ int ttm_pool_alloc(struct ttm_pool *pool, struct ttm_tt *tt,
>   
>   	return 0;
>   
> -error_global_free:
> -	ttm_mem_global_free_page(&ttm_mem_glob, p, (1 << order) * PAGE_SIZE);
> -
>   error_free_page:
>   	ttm_pool_free_page(pool, tt->caching, order, p);
>   
> @@ -464,8 +455,6 @@ void ttm_pool_free(struct ttm_pool *pool, struct ttm_tt *tt)
>   
>   		order = ttm_pool_page_order(pool, p);
>   		num_pages = 1ULL << order;
> -		ttm_mem_global_free_page(&ttm_mem_glob, p,
> -					 num_pages * PAGE_SIZE);
>   		if (tt->dma_address)
>   			ttm_pool_unmap(pool, tt->dma_address[i], num_pages);
>   
> diff --git a/drivers/gpu/drm/vmwgfx/Makefile b/drivers/gpu/drm/vmwgfx/Makefile
> index cc4cdca7176e..8c02fa5852e7 100644
> --- a/drivers/gpu/drm/vmwgfx/Makefile
> +++ b/drivers/gpu/drm/vmwgfx/Makefile
> @@ -9,7 +9,7 @@ vmwgfx-y := vmwgfx_execbuf.o vmwgfx_gmr.o vmwgfx_kms.o vmwgfx_drv.o \
>   	    vmwgfx_cotable.o vmwgfx_so.o vmwgfx_binding.o vmwgfx_msg.o \
>   	    vmwgfx_simple_resource.o vmwgfx_va.o vmwgfx_blit.o \
>   	    vmwgfx_validation.o vmwgfx_page_dirty.o vmwgfx_streamoutput.o \
> -	    ttm_object.o ttm_lock.o
> +	    ttm_object.o ttm_lock.o ttm_memory.o
>   
>   vmwgfx-$(CONFIG_TRANSPARENT_HUGEPAGE) += vmwgfx_thp.o
>   obj-$(CONFIG_DRM_VMWGFX) := vmwgfx.o
> diff --git a/drivers/gpu/drm/ttm/ttm_memory.c b/drivers/gpu/drm/vmwgfx/ttm_memory.c
> similarity index 97%
> rename from drivers/gpu/drm/ttm/ttm_memory.c
> rename to drivers/gpu/drm/vmwgfx/ttm_memory.c
> index 634a85c2dc4c..1306d9e0f095 100644
> --- a/drivers/gpu/drm/ttm/ttm_memory.c
> +++ b/drivers/gpu/drm/vmwgfx/ttm_memory.c
> @@ -28,7 +28,6 @@
>   
>   #define pr_fmt(fmt) "[TTM] " fmt
>   
> -#include <drm/ttm/ttm_memory.h>
>   #include <linux/spinlock.h>
>   #include <linux/sched.h>
>   #include <linux/wait.h>
> @@ -36,10 +35,11 @@
>   #include <linux/module.h>
>   #include <linux/slab.h>
>   #include <linux/swap.h>
> -#include <drm/ttm/ttm_pool.h>
> -#include <drm/ttm/ttm_tt.h>
>   
> -#include "ttm_module.h"
> +#include <drm/drm_device.h>
> +#include <drm/drm_file.h>
> +
> +#include "ttm_memory.h"
>   
>   #define TTM_MEMORY_ALLOC_RETRIES 4
>   
> @@ -414,7 +414,7 @@ static int ttm_mem_init_dma32_zone(struct ttm_mem_global *glob,
>   }
>   #endif
>   
> -int ttm_mem_global_init(struct ttm_mem_global *glob)
> +int ttm_mem_global_init(struct ttm_mem_global *glob, struct drm_device *dev)
>   {
>   	struct sysinfo si;
>   	int ret;
> @@ -425,7 +425,8 @@ int ttm_mem_global_init(struct ttm_mem_global *glob)
>   	glob->swap_queue = create_singlethread_workqueue("ttm_swap");
>   	INIT_WORK(&glob->work, ttm_shrink_work);
>   	ret = kobject_init_and_add(
> -		&glob->kobj, &ttm_mem_glob_kobj_type, ttm_get_kobj(), "memory_accounting");
> +		&glob->kobj, &ttm_mem_glob_kobj_type, &dev->primary->kdev->kobj,
> +		"memory_accounting");
>   	if (unlikely(ret != 0)) {
>   		kobject_put(&glob->kobj);
>   		return ret;
> @@ -453,8 +454,6 @@ int ttm_mem_global_init(struct ttm_mem_global *glob)
>   		pr_info("Zone %7s: Available graphics memory: %llu KiB\n",
>   			zone->name, (unsigned long long)zone->max_mem >> 10);
>   	}
> -	ttm_pool_mgr_init(glob->zone_kernel->max_mem/(2*PAGE_SIZE));
> -	ttm_tt_mgr_init();
>   	return 0;
>   out_no_zone:
>   	ttm_mem_global_release(glob);
> @@ -466,10 +465,6 @@ void ttm_mem_global_release(struct ttm_mem_global *glob)
>   	struct ttm_mem_zone *zone;
>   	unsigned int i;
>   
> -	/* let the page allocator first stop the shrink work. */
> -	ttm_pool_mgr_fini();
> -	ttm_tt_mgr_fini();
> -
>   	flush_workqueue(glob->swap_queue);
>   	destroy_workqueue(glob->swap_queue);
>   	glob->swap_queue = NULL;
> diff --git a/include/drm/ttm/ttm_memory.h b/drivers/gpu/drm/vmwgfx/ttm_memory.h
> similarity index 97%
> rename from include/drm/ttm/ttm_memory.h
> rename to drivers/gpu/drm/vmwgfx/ttm_memory.h
> index c1f167881e33..850ee6c867da 100644
> --- a/include/drm/ttm/ttm_memory.h
> +++ b/drivers/gpu/drm/vmwgfx/ttm_memory.h
> @@ -35,7 +35,8 @@
>   #include <linux/errno.h>
>   #include <linux/kobject.h>
>   #include <linux/mm.h>
> -#include "ttm_bo_api.h"
> +
> +#include <drm/ttm/ttm_bo_api.h>
>   
>   /**
>    * struct ttm_mem_global - Global memory accounting structure.
> @@ -79,7 +80,7 @@ extern struct ttm_mem_global {
>   #endif
>   } ttm_mem_glob;
>   
> -int ttm_mem_global_init(struct ttm_mem_global *glob);
> +int ttm_mem_global_init(struct ttm_mem_global *glob, struct drm_device *dev);
>   void ttm_mem_global_release(struct ttm_mem_global *glob);
>   int ttm_mem_global_alloc(struct ttm_mem_global *glob, uint64_t memory,
>   			 struct ttm_operation_ctx *ctx);
> diff --git a/drivers/gpu/drm/vmwgfx/ttm_object.h b/drivers/gpu/drm/vmwgfx/ttm_object.h
> index ede26df87c93..49b064f0cb19 100644
> --- a/drivers/gpu/drm/vmwgfx/ttm_object.h
> +++ b/drivers/gpu/drm/vmwgfx/ttm_object.h
> @@ -43,7 +43,8 @@
>   #include <linux/rcupdate.h>
>   
>   #include <drm/drm_hashtab.h>
> -#include <drm/ttm/ttm_memory.h>
> +
> +#include "ttm_memory.h"
>   
>   /**
>    * enum ttm_ref_type
> diff --git a/drivers/gpu/drm/vmwgfx/vmwgfx_bo.c b/drivers/gpu/drm/vmwgfx/vmwgfx_bo.c
> index 6b3bfd8c678a..50e529a01677 100644
> --- a/drivers/gpu/drm/vmwgfx/vmwgfx_bo.c
> +++ b/drivers/gpu/drm/vmwgfx/vmwgfx_bo.c
> @@ -507,11 +507,16 @@ int vmw_bo_create_kernel(struct vmw_private *dev_priv, unsigned long size,
>   	acc_size = ttm_round_pot(sizeof(*bo));
>   	acc_size += ttm_round_pot(npages * sizeof(void *));
>   	acc_size += ttm_round_pot(sizeof(struct ttm_tt));
> +
> +	ret = ttm_mem_global_alloc(&ttm_mem_glob, acc_size, &ctx);
> +	if (unlikely(ret))
> +		goto error_free;
> +
>   	ret = ttm_bo_init_reserved(&dev_priv->bdev, bo, size,
>   				   ttm_bo_type_device, placement, 0,
> -				   &ctx, acc_size, NULL, NULL, NULL);
> +				   &ctx, NULL, NULL, NULL);
>   	if (unlikely(ret))
> -		goto error_free;
> +		goto error_account;
>   
>   	ttm_bo_pin(bo);
>   	ttm_bo_unreserve(bo);
> @@ -519,6 +524,9 @@ int vmw_bo_create_kernel(struct vmw_private *dev_priv, unsigned long size,
>   
>   	return 0;
>   
> +error_account:
> +	ttm_mem_global_free(&ttm_mem_glob, acc_size);
> +
>   error_free:
>   	kfree(bo);
>   	return ret;
> @@ -558,11 +566,17 @@ int vmw_bo_init(struct vmw_private *dev_priv,
>   	vmw_bo->base.priority = 3;
>   	vmw_bo->res_tree = RB_ROOT;
>   
> +	ret = ttm_mem_global_alloc(&ttm_mem_glob, acc_size, &ctx);
> +	if (unlikely(ret))
> +		return ret;
> +
>   	ret = ttm_bo_init_reserved(bdev, &vmw_bo->base, size,
>   				   ttm_bo_type_device, placement,
> -				   0, &ctx, acc_size, NULL, NULL, bo_free);
> -	if (unlikely(ret))
> +				   0, &ctx, NULL, NULL, bo_free);
> +	if (unlikely(ret)) {
> +		ttm_mem_global_free(&ttm_mem_glob, acc_size);
>   		return ret;
> +	}
>   
>   	if (pin)
>   		ttm_bo_pin(&vmw_bo->base);
> diff --git a/drivers/gpu/drm/vmwgfx/vmwgfx_drv.c b/drivers/gpu/drm/vmwgfx/vmwgfx_drv.c
> index 710ba5169a74..6c0ca1011629 100644
> --- a/drivers/gpu/drm/vmwgfx/vmwgfx_drv.c
> +++ b/drivers/gpu/drm/vmwgfx/vmwgfx_drv.c
> @@ -1268,6 +1268,7 @@ static void vmw_remove(struct pci_dev *pdev)
>   {
>   	struct drm_device *dev = pci_get_drvdata(pdev);
>   
> +	ttm_mem_global_release(&ttm_mem_glob);
>   	drm_dev_unregister(dev);
>   	vmw_driver_unload(dev);
>   }
> @@ -1518,6 +1519,10 @@ static int vmw_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
>   
>   	pci_set_drvdata(pdev, &vmw->drm);
>   
> +	ret = ttm_mem_global_init(&ttm_mem_glob, &vmw->drm);
> +	if (ret)
> +		return ret;
> +
>   	ret = vmw_driver_load(vmw, ent->device);
>   	if (ret)
>   		return ret;
> diff --git a/drivers/gpu/drm/vmwgfx/vmwgfx_ttm_buffer.c b/drivers/gpu/drm/vmwgfx/vmwgfx_ttm_buffer.c
> index d1bfa59579f1..63f10c865061 100644
> --- a/drivers/gpu/drm/vmwgfx/vmwgfx_ttm_buffer.c
> +++ b/drivers/gpu/drm/vmwgfx/vmwgfx_ttm_buffer.c
> @@ -576,11 +576,31 @@ static void vmw_ttm_destroy(struct ttm_device *bdev, struct ttm_tt *ttm)
>   static int vmw_ttm_populate(struct ttm_device *bdev,
>   			    struct ttm_tt *ttm, struct ttm_operation_ctx *ctx)
>   {
> +	unsigned int i;
> +	int ret;
> +
>   	/* TODO: maybe completely drop this ? */
>   	if (ttm_tt_is_populated(ttm))
>   		return 0;
>   
> -	return ttm_pool_alloc(&bdev->pool, ttm, ctx);
> +	ret = ttm_pool_alloc(&bdev->pool, ttm, ctx);
> +	if (ret)
> +		return ret;
> +
> +	for (i = 0; i < ttm->num_pages; ++i) {
> +		ret = ttm_mem_global_alloc_page(&ttm_mem_glob, ttm->pages[i],
> +						PAGE_SIZE, ctx);
> +		if (ret)
> +			goto error;
> +	}
> +	return 0;
> +
> +error:
> +	while (i--)
> +		ttm_mem_global_free_page(&ttm_mem_glob, ttm->pages[i],
> +					 PAGE_SIZE);
> +	ttm_pool_free(&bdev->pool, ttm);
> +	return ret;
>   }
>   
>   static void vmw_ttm_unpopulate(struct ttm_device *bdev,
> @@ -588,6 +608,7 @@ static void vmw_ttm_unpopulate(struct ttm_device *bdev,
>   {
>   	struct vmw_ttm_tt *vmw_tt = container_of(ttm, struct vmw_ttm_tt,
>   						 dma_ttm);
> +	unsigned int i;
>   
>   	if (vmw_tt->mob) {
>   		vmw_mob_destroy(vmw_tt->mob);
> @@ -595,6 +616,11 @@ static void vmw_ttm_unpopulate(struct ttm_device *bdev,
>   	}
>   
>   	vmw_ttm_unmap_dma(vmw_tt);
> +
> +	for (i = 0; i < ttm->num_pages; ++i)
> +		ttm_mem_global_free_page(&ttm_mem_glob, ttm->pages[i],
> +					 PAGE_SIZE);
> +
>   	ttm_pool_free(&bdev->pool, ttm);
>   }
>   
> diff --git a/include/drm/ttm/ttm_bo_api.h b/include/drm/ttm/ttm_bo_api.h
> index 1297a8fb7ccb..4fb523dfab32 100644
> --- a/include/drm/ttm/ttm_bo_api.h
> +++ b/include/drm/ttm/ttm_bo_api.h
> @@ -88,7 +88,6 @@ struct ttm_tt;
>    * @type: The bo type.
>    * @destroy: Destruction function. If NULL, kfree is used.
>    * @num_pages: Actual number of pages.
> - * @acc_size: Accounted size for this object.
>    * @kref: Reference count of this buffer object. When this refcount reaches
>    * zero, the object is destroyed or put on the delayed delete list.
>    * @mem: structure describing current placement.
> @@ -125,7 +124,6 @@ struct ttm_buffer_object {
>   	struct ttm_device *bdev;
>   	enum ttm_bo_type type;
>   	void (*destroy) (struct ttm_buffer_object *);
> -	size_t acc_size;
>   
>   	/**
>   	* Members not needing protection.
> @@ -357,10 +355,6 @@ void ttm_bo_unlock_delayed_workqueue(struct ttm_device *bdev, int resched);
>   bool ttm_bo_eviction_valuable(struct ttm_buffer_object *bo,
>   			      const struct ttm_place *place);
>   
> -size_t ttm_bo_dma_acc_size(struct ttm_device *bdev,
> -			   unsigned long bo_size,
> -			   unsigned struct_size);
> -
>   /**
>    * ttm_bo_init_reserved
>    *
> @@ -371,7 +365,6 @@ size_t ttm_bo_dma_acc_size(struct ttm_device *bdev,
>    * @flags: Initial placement flags.
>    * @page_alignment: Data alignment in pages.
>    * @ctx: TTM operation context for memory allocation.
> - * @acc_size: Accounted size for this object.
>    * @resv: Pointer to a dma_resv, or NULL to let ttm allocate one.
>    * @destroy: Destroy function. Use NULL for kfree().
>    *
> @@ -402,8 +395,7 @@ int ttm_bo_init_reserved(struct ttm_device *bdev,
>   			 struct ttm_placement *placement,
>   			 uint32_t page_alignment,
>   			 struct ttm_operation_ctx *ctx,
> -			 size_t acc_size, struct sg_table *sg,
> -			 struct dma_resv *resv,
> +			 struct sg_table *sg, struct dma_resv *resv,
>   			 void (*destroy) (struct ttm_buffer_object *));
>   
>   /**
> @@ -421,7 +413,6 @@ int ttm_bo_init_reserved(struct ttm_device *bdev,
>    * holds a pointer to a persistent shmem object. Typically, this would
>    * point to the shmem object backing a GEM object if TTM is used to back a
>    * GEM user interface.
> - * @acc_size: Accounted size for this object.
>    * @resv: Pointer to a dma_resv, or NULL to let ttm allocate one.
>    * @destroy: Destroy function. Use NULL for kfree().
>    *
> @@ -446,7 +437,7 @@ int ttm_bo_init_reserved(struct ttm_device *bdev,
>   int ttm_bo_init(struct ttm_device *bdev, struct ttm_buffer_object *bo,
>   		size_t size, enum ttm_bo_type type,
>   		struct ttm_placement *placement,
> -		uint32_t page_alignment, bool interrubtible, size_t acc_size,
> +		uint32_t page_alignment, bool interrubtible,
>   		struct sg_table *sg, struct dma_resv *resv,
>   		void (*destroy) (struct ttm_buffer_object *));
>   
> diff --git a/include/drm/ttm/ttm_bo_driver.h b/include/drm/ttm/ttm_bo_driver.h
> index 1c9bf993e252..8959c0075cfd 100644
> --- a/include/drm/ttm/ttm_bo_driver.h
> +++ b/include/drm/ttm/ttm_bo_driver.h
> @@ -40,7 +40,6 @@
>   #include <drm/ttm/ttm_device.h>
>   
>   #include "ttm_bo_api.h"
> -#include "ttm_memory.h"
>   #include "ttm_placement.h"
>   #include "ttm_tt.h"
>   #include "ttm_pool.h"
> diff --git a/include/drm/ttm/ttm_tt.h b/include/drm/ttm/ttm_tt.h
> index cce57fb49e2c..069f8130241a 100644
> --- a/include/drm/ttm/ttm_tt.h
> +++ b/include/drm/ttm/ttm_tt.h
> @@ -30,6 +30,7 @@
>   #include <linux/types.h>
>   #include <drm/ttm/ttm_caching.h>
>   
> +struct ttm_bo_device;
>   struct ttm_tt;
>   struct ttm_resource;
>   struct ttm_buffer_object;

_______________________________________________
dri-devel mailing list
dri-devel@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/dri-devel

^ permalink raw reply	[flat|nested] 17+ messages in thread

* Re: [PATCH 2/3] drm/ttm: move memory accounting into vmwgfx v3
  2021-02-02 13:04   ` Christian König
@ 2021-02-02 15:14     ` Zack Rusin
  2021-02-02 15:16       ` Christian König
  0 siblings, 1 reply; 17+ messages in thread
From: Zack Rusin @ 2021-02-02 15:14 UTC (permalink / raw)
  To: Christian König
  Cc: Linux-graphics-maintainer, Roland Scheidegger, dri-devel

Looks good. There’s probably not much reason to call it ttm_memory anymore as it only deals with ttm_mem_glob, we’ll likely fold it in after you submit. Thanks.

Reviewed-by: Zack Rusin <zackr@vmware.com>

z

> On Feb 2, 2021, at 08:04, Christian König <christian.koenig@amd.com> wrote:
> 
> Ping?
> 
> Especially Roland and Zack do you have any objections to this?
> 
> Regards,
> Christian.
> 
> Am 28.01.21 um 14:16 schrieb Christian König:
>> This is just another feature which is only used by VMWGFX, so move
>> it into the driver instead.
>> 
>> I've tried to add the accounting sysfs file to the kobject of the drm
>> minor, but I'm not 100% sure if this works as expected.
>> 
>> v2: fix typo in KFD and avoid 64bit divide
>> v3: fix init order in VMWGFX
>> 
>> Signed-off-by: Christian König <christian.koenig@amd.com>
>> ---
>>  .../gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c  | 16 ++++++---
>>  drivers/gpu/drm/amd/amdgpu/amdgpu_object.c    |  8 ++---
>>  drivers/gpu/drm/drm_gem_vram_helper.c         |  6 ++--
>>  drivers/gpu/drm/nouveau/nouveau_bo.c          |  7 ++--
>>  drivers/gpu/drm/nouveau/nouveau_drv.h         |  1 -
>>  drivers/gpu/drm/qxl/qxl_object.c              |  4 +--
>>  drivers/gpu/drm/radeon/radeon_object.c        |  8 ++---
>>  drivers/gpu/drm/ttm/Makefile                  |  7 ++--
>>  drivers/gpu/drm/ttm/ttm_bo.c                  | 33 +------------------
>>  drivers/gpu/drm/ttm/ttm_bo_util.c             |  1 -
>>  drivers/gpu/drm/ttm/ttm_device.c              | 22 ++++++++++---
>>  drivers/gpu/drm/ttm/ttm_pool.c                | 13 +-------
>>  drivers/gpu/drm/vmwgfx/Makefile               |  2 +-
>>  drivers/gpu/drm/{ttm => vmwgfx}/ttm_memory.c  | 19 ++++-------
>>  .../gpu/drm/vmwgfx}/ttm_memory.h              |  5 +--
>>  drivers/gpu/drm/vmwgfx/ttm_object.h           |  3 +-
>>  drivers/gpu/drm/vmwgfx/vmwgfx_bo.c            | 22 ++++++++++---
>>  drivers/gpu/drm/vmwgfx/vmwgfx_drv.c           |  5 +++
>>  drivers/gpu/drm/vmwgfx/vmwgfx_ttm_buffer.c    | 28 +++++++++++++++-
>>  include/drm/ttm/ttm_bo_api.h                  | 13 ++------
>>  include/drm/ttm/ttm_bo_driver.h               |  1 -
>>  include/drm/ttm/ttm_tt.h                      |  1 +
>>  22 files changed, 110 insertions(+), 115 deletions(-)
>>  rename drivers/gpu/drm/{ttm => vmwgfx}/ttm_memory.c (97%)
>>  rename {include/drm/ttm => drivers/gpu/drm/vmwgfx}/ttm_memory.h (97%)
>> 
>> diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c b/drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c
>> index 0849b68e784f..e440af37dde8 100644
>> --- a/drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c
>> +++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c
>> @@ -118,6 +118,16 @@ void amdgpu_amdkfd_gpuvm_init_mem_limits(void)
>>   */
>>  #define ESTIMATE_PT_SIZE(mem_size) ((mem_size) >> 14)
>>  +static size_t amdgpu_amdkfd_acc_size(uint64_t size)
>> +{
>> +	size >>= PAGE_SHIFT;
>> +	size *= sizeof(dma_addr_t) + sizeof(void *);
>> +
>> +	return __roundup_pow_of_two(sizeof(struct amdgpu_bo)) +
>> +		__roundup_pow_of_two(sizeof(struct ttm_tt)) +
>> +		PAGE_ALIGN(size);
>> +}
>> +
>>  static int amdgpu_amdkfd_reserve_mem_limit(struct amdgpu_device *adev,
>>  		uint64_t size, u32 domain, bool sg)
>>  {
>> @@ -126,8 +136,7 @@ static int amdgpu_amdkfd_reserve_mem_limit(struct amdgpu_device *adev,
>>  	size_t acc_size, system_mem_needed, ttm_mem_needed, vram_needed;
>>  	int ret = 0;
>>  -	acc_size = ttm_bo_dma_acc_size(&adev->mman.bdev, size,
>> -				       sizeof(struct amdgpu_bo));
>> +	acc_size = amdgpu_amdkfd_acc_size(size);
>>    	vram_needed = 0;
>>  	if (domain == AMDGPU_GEM_DOMAIN_GTT) {
>> @@ -174,8 +183,7 @@ static void unreserve_mem_limit(struct amdgpu_device *adev,
>>  {
>>  	size_t acc_size;
>>  -	acc_size = ttm_bo_dma_acc_size(&adev->mman.bdev, size,
>> -				       sizeof(struct amdgpu_bo));
>> +	acc_size = amdgpu_amdkfd_acc_size(size);
>>    	spin_lock(&kfd_mem_limit.mem_limit_lock);
>>  	if (domain == AMDGPU_GEM_DOMAIN_GTT) {
>> diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_object.c b/drivers/gpu/drm/amd/amdgpu/amdgpu_object.c
>> index 6cc9919b12cc..599c9a132eb6 100644
>> --- a/drivers/gpu/drm/amd/amdgpu/amdgpu_object.c
>> +++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_object.c
>> @@ -523,7 +523,6 @@ static int amdgpu_bo_do_create(struct amdgpu_device *adev,
>>  	};
>>  	struct amdgpu_bo *bo;
>>  	unsigned long page_align, size = bp->size;
>> -	size_t acc_size;
>>  	int r;
>>    	/* Note that GDS/GWS/OA allocates 1 page per byte/resource. */
>> @@ -546,9 +545,6 @@ static int amdgpu_bo_do_create(struct amdgpu_device *adev,
>>    	*bo_ptr = NULL;
>>  -	acc_size = ttm_bo_dma_acc_size(&adev->mman.bdev, size,
>> -				       sizeof(struct amdgpu_bo));
>> -
>>  	bo = kzalloc(sizeof(struct amdgpu_bo), GFP_KERNEL);
>>  	if (bo == NULL)
>>  		return -ENOMEM;
>> @@ -577,8 +573,8 @@ static int amdgpu_bo_do_create(struct amdgpu_device *adev,
>>  		bo->tbo.priority = 1;
>>    	r = ttm_bo_init_reserved(&adev->mman.bdev, &bo->tbo, size, bp->type,
>> -				 &bo->placement, page_align, &ctx, acc_size,
>> -				 NULL, bp->resv, &amdgpu_bo_destroy);
>> +				 &bo->placement, page_align, &ctx,  NULL,
>> +				 bp->resv, &amdgpu_bo_destroy);
>>  	if (unlikely(r != 0))
>>  		return r;
>>  diff --git a/drivers/gpu/drm/drm_gem_vram_helper.c b/drivers/gpu/drm/drm_gem_vram_helper.c
>> index 0b13c8507688..a0992f0b8afd 100644
>> --- a/drivers/gpu/drm/drm_gem_vram_helper.c
>> +++ b/drivers/gpu/drm/drm_gem_vram_helper.c
>> @@ -189,7 +189,6 @@ struct drm_gem_vram_object *drm_gem_vram_create(struct drm_device *dev,
>>  	struct drm_vram_mm *vmm = dev->vram_mm;
>>  	struct ttm_device *bdev;
>>  	int ret;
>> -	size_t acc_size;
>>    	if (WARN_ONCE(!vmm, "VRAM MM not initialized"))
>>  		return ERR_PTR(-EINVAL);
>> @@ -216,7 +215,6 @@ struct drm_gem_vram_object *drm_gem_vram_create(struct drm_device *dev,
>>  	}
>>    	bdev = &vmm->bdev;
>> -	acc_size = ttm_bo_dma_acc_size(bdev, size, sizeof(*gbo));
>>    	gbo->bo.bdev = bdev;
>>  	drm_gem_vram_placement(gbo, DRM_GEM_VRAM_PL_FLAG_SYSTEM);
>> @@ -226,8 +224,8 @@ struct drm_gem_vram_object *drm_gem_vram_create(struct drm_device *dev,
>>  	 * to release gbo->bo.base and kfree gbo.
>>  	 */
>>  	ret = ttm_bo_init(bdev, &gbo->bo, size, ttm_bo_type_device,
>> -			  &gbo->placement, pg_align, false, acc_size,
>> -			  NULL, NULL, ttm_buffer_object_destroy);
>> +			  &gbo->placement, pg_align, false, NULL, NULL,
>> +			  ttm_buffer_object_destroy);
>>  	if (ret)
>>  		return ERR_PTR(ret);
>>  diff --git a/drivers/gpu/drm/nouveau/nouveau_bo.c b/drivers/gpu/drm/nouveau/nouveau_bo.c
>> index c177940d6e2c..ca2a8ae1938e 100644
>> --- a/drivers/gpu/drm/nouveau/nouveau_bo.c
>> +++ b/drivers/gpu/drm/nouveau/nouveau_bo.c
>> @@ -300,18 +300,15 @@ nouveau_bo_init(struct nouveau_bo *nvbo, u64 size, int align, u32 domain,
>>  		struct sg_table *sg, struct dma_resv *robj)
>>  {
>>  	int type = sg ? ttm_bo_type_sg : ttm_bo_type_device;
>> -	size_t acc_size;
>>  	int ret;
>>  -	acc_size = ttm_bo_dma_acc_size(nvbo->bo.bdev, size, sizeof(*nvbo));
>> -
>>  	nvbo->bo.mem.num_pages = size >> PAGE_SHIFT;
>>  	nouveau_bo_placement_set(nvbo, domain, 0);
>>  	INIT_LIST_HEAD(&nvbo->io_reserve_lru);
>>    	ret = ttm_bo_init(nvbo->bo.bdev, &nvbo->bo, size, type,
>> -			  &nvbo->placement, align >> PAGE_SHIFT, false,
>> -			  acc_size, sg, robj, nouveau_bo_del_ttm);
>> +			  &nvbo->placement, align >> PAGE_SHIFT, false, sg,
>> +			  robj, nouveau_bo_del_ttm);
>>  	if (ret) {
>>  		/* ttm will call nouveau_bo_del_ttm if it fails.. */
>>  		return ret;
>> diff --git a/drivers/gpu/drm/nouveau/nouveau_drv.h b/drivers/gpu/drm/nouveau/nouveau_drv.h
>> index edf9d1ee9d58..a491c2c1c56e 100644
>> --- a/drivers/gpu/drm/nouveau/nouveau_drv.h
>> +++ b/drivers/gpu/drm/nouveau/nouveau_drv.h
>> @@ -54,7 +54,6 @@
>>  #include <drm/ttm/ttm_bo_api.h>
>>  #include <drm/ttm/ttm_bo_driver.h>
>>  #include <drm/ttm/ttm_placement.h>
>> -#include <drm/ttm/ttm_memory.h>
>>    #include <drm/drm_audio_component.h>
>>  diff --git a/drivers/gpu/drm/qxl/qxl_object.c b/drivers/gpu/drm/qxl/qxl_object.c
>> index ceebc5881f68..705b51535492 100644
>> --- a/drivers/gpu/drm/qxl/qxl_object.c
>> +++ b/drivers/gpu/drm/qxl/qxl_object.c
>> @@ -138,8 +138,8 @@ int qxl_bo_create(struct qxl_device *qdev,
>>  	qxl_ttm_placement_from_domain(bo, domain);
>>    	r = ttm_bo_init_reserved(&qdev->mman.bdev, &bo->tbo, size, type,
>> -				 &bo->placement, 0, &ctx, size,
>> -				 NULL, NULL, &qxl_ttm_bo_destroy);
>> +				 &bo->placement, 0, &ctx, NULL, NULL,
>> +				 &qxl_ttm_bo_destroy);
>>  	if (unlikely(r != 0)) {
>>  		if (r != -ERESTARTSYS)
>>  			dev_err(qdev->ddev.dev,
>> diff --git a/drivers/gpu/drm/radeon/radeon_object.c b/drivers/gpu/drm/radeon/radeon_object.c
>> index 6a336284466f..804f7a427be7 100644
>> --- a/drivers/gpu/drm/radeon/radeon_object.c
>> +++ b/drivers/gpu/drm/radeon/radeon_object.c
>> @@ -159,7 +159,6 @@ int radeon_bo_create(struct radeon_device *rdev,
>>  	struct radeon_bo *bo;
>>  	enum ttm_bo_type type;
>>  	unsigned long page_align = roundup(byte_align, PAGE_SIZE) >> PAGE_SHIFT;
>> -	size_t acc_size;
>>  	int r;
>>    	size = ALIGN(size, PAGE_SIZE);
>> @@ -173,9 +172,6 @@ int radeon_bo_create(struct radeon_device *rdev,
>>  	}
>>  	*bo_ptr = NULL;
>>  -	acc_size = ttm_bo_dma_acc_size(&rdev->mman.bdev, size,
>> -				       sizeof(struct radeon_bo));
>> -
>>  	bo = kzalloc(sizeof(struct radeon_bo), GFP_KERNEL);
>>  	if (bo == NULL)
>>  		return -ENOMEM;
>> @@ -230,8 +226,8 @@ int radeon_bo_create(struct radeon_device *rdev,
>>  	/* Kernel allocation are uninterruptible */
>>  	down_read(&rdev->pm.mclk_lock);
>>  	r = ttm_bo_init(&rdev->mman.bdev, &bo->tbo, size, type,
>> -			&bo->placement, page_align, !kernel, acc_size,
>> -			sg, resv, &radeon_ttm_bo_destroy);
>> +			&bo->placement, page_align, !kernel, sg, resv,
>> +			&radeon_ttm_bo_destroy);
>>  	up_read(&rdev->pm.mclk_lock);
>>  	if (unlikely(r != 0)) {
>>  		return r;
>> diff --git a/drivers/gpu/drm/ttm/Makefile b/drivers/gpu/drm/ttm/Makefile
>> index 8e6437eadabe..40e5e9da7953 100644
>> --- a/drivers/gpu/drm/ttm/Makefile
>> +++ b/drivers/gpu/drm/ttm/Makefile
>> @@ -2,10 +2,9 @@
>>  #
>>  # Makefile for the drm device driver.  This driver provides support for the
>>  -ttm-y := ttm_memory.o ttm_tt.o ttm_bo.o \
>> -	ttm_bo_util.o ttm_bo_vm.o ttm_module.o \
>> -	ttm_execbuf_util.o ttm_range_manager.o \
>> -	ttm_resource.o ttm_pool.o ttm_device.o
>> +ttm-y := ttm_tt.o ttm_bo.o ttm_bo_util.o ttm_bo_vm.o ttm_module.o \
>> +	ttm_execbuf_util.o ttm_range_manager.o ttm_resource.o ttm_pool.o \
>> +	ttm_device.o
>>  ttm-$(CONFIG_AGP) += ttm_agp_backend.o
>>    obj-$(CONFIG_DRM_TTM) += ttm.o
>> diff --git a/drivers/gpu/drm/ttm/ttm_bo.c b/drivers/gpu/drm/ttm/ttm_bo.c
>> index 643befc1a6f2..e38102282fd5 100644
>> --- a/drivers/gpu/drm/ttm/ttm_bo.c
>> +++ b/drivers/gpu/drm/ttm/ttm_bo.c
>> @@ -425,7 +425,6 @@ static void ttm_bo_release(struct kref *kref)
>>  	struct ttm_buffer_object *bo =
>>  	    container_of(kref, struct ttm_buffer_object, kref);
>>  	struct ttm_device *bdev = bo->bdev;
>> -	size_t acc_size = bo->acc_size;
>>  	int ret;
>>    	if (!bo->deleted) {
>> @@ -485,7 +484,6 @@ static void ttm_bo_release(struct kref *kref)
>>  	if (!ttm_bo_uses_embedded_gem_object(bo))
>>  		dma_resv_fini(&bo->base._resv);
>>  	bo->destroy(bo);
>> -	ttm_mem_global_free(&ttm_mem_glob, acc_size);
>>  }
>>    void ttm_bo_put(struct ttm_buffer_object *bo)
>> @@ -1046,25 +1044,13 @@ int ttm_bo_init_reserved(struct ttm_device *bdev,
>>  			 struct ttm_placement *placement,
>>  			 uint32_t page_alignment,
>>  			 struct ttm_operation_ctx *ctx,
>> -			 size_t acc_size,
>>  			 struct sg_table *sg,
>>  			 struct dma_resv *resv,
>>  			 void (*destroy) (struct ttm_buffer_object *))
>>  {
>> -	struct ttm_mem_global *mem_glob = &ttm_mem_glob;
>>  	bool locked;
>>  	int ret = 0;
>>  -	ret = ttm_mem_global_alloc(mem_glob, acc_size, ctx);
>> -	if (ret) {
>> -		pr_err("Out of kernel memory\n");
>> -		if (destroy)
>> -			(*destroy)(bo);
>> -		else
>> -			kfree(bo);
>> -		return -ENOMEM;
>> -	}
>> -
>>  	bo->destroy = destroy ? destroy : ttm_bo_default_destroy;
>>    	kref_init(&bo->kref);
>> @@ -1081,7 +1067,6 @@ int ttm_bo_init_reserved(struct ttm_device *bdev,
>>  	bo->mem.bus.addr = NULL;
>>  	bo->moving = NULL;
>>  	bo->mem.placement = 0;
>> -	bo->acc_size = acc_size;
>>  	bo->pin_count = 0;
>>  	bo->sg = sg;
>>  	if (resv) {
>> @@ -1142,7 +1127,6 @@ int ttm_bo_init(struct ttm_device *bdev,
>>  		struct ttm_placement *placement,
>>  		uint32_t page_alignment,
>>  		bool interruptible,
>> -		size_t acc_size,
>>  		struct sg_table *sg,
>>  		struct dma_resv *resv,
>>  		void (*destroy) (struct ttm_buffer_object *))
>> @@ -1151,8 +1135,7 @@ int ttm_bo_init(struct ttm_device *bdev,
>>  	int ret;
>>    	ret = ttm_bo_init_reserved(bdev, bo, size, type, placement,
>> -				   page_alignment, &ctx, acc_size,
>> -				   sg, resv, destroy);
>> +				   page_alignment, &ctx, sg, resv, destroy);
>>  	if (ret)
>>  		return ret;
>>  @@ -1163,20 +1146,6 @@ int ttm_bo_init(struct ttm_device *bdev,
>>  }
>>  EXPORT_SYMBOL(ttm_bo_init);
>>  -size_t ttm_bo_dma_acc_size(struct ttm_device *bdev,
>> -			   unsigned long bo_size,
>> -			   unsigned struct_size)
>> -{
>> -	unsigned npages = (PAGE_ALIGN(bo_size)) >> PAGE_SHIFT;
>> -	size_t size = 0;
>> -
>> -	size += ttm_round_pot(struct_size);
>> -	size += ttm_round_pot(npages * (2*sizeof(void *) + sizeof(dma_addr_t)));
>> -	size += ttm_round_pot(sizeof(struct ttm_tt));
>> -	return size;
>> -}
>> -EXPORT_SYMBOL(ttm_bo_dma_acc_size);
>> -
>>  /*
>>   * buffer object vm functions.
>>   */
>> diff --git a/drivers/gpu/drm/ttm/ttm_bo_util.c b/drivers/gpu/drm/ttm/ttm_bo_util.c
>> index db0f2661d504..031e5819fec4 100644
>> --- a/drivers/gpu/drm/ttm/ttm_bo_util.c
>> +++ b/drivers/gpu/drm/ttm/ttm_bo_util.c
>> @@ -309,7 +309,6 @@ static int ttm_buffer_object_transfer(struct ttm_buffer_object *bo,
>>    	kref_init(&fbo->base.kref);
>>  	fbo->base.destroy = &ttm_transfered_destroy;
>> -	fbo->base.acc_size = 0;
>>  	fbo->base.pin_count = 0;
>>  	if (bo->type != ttm_bo_type_sg)
>>  		fbo->base.base.resv = &fbo->base.base._resv;
>> diff --git a/drivers/gpu/drm/ttm/ttm_device.c b/drivers/gpu/drm/ttm/ttm_device.c
>> index ac0903c9e60a..6bde344e5da7 100644
>> --- a/drivers/gpu/drm/ttm/ttm_device.c
>> +++ b/drivers/gpu/drm/ttm/ttm_device.c
>> @@ -27,9 +27,12 @@
>>    #define pr_fmt(fmt) "[TTM DEVICE] " fmt
>>  +#include <linux/mm.h>
>> +
>>  #include <drm/ttm/ttm_device.h>
>> -#include <drm/ttm/ttm_memory.h>
>> +#include <drm/ttm/ttm_tt.h>
>>  #include <drm/ttm/ttm_placement.h>
>> +#include <drm/ttm/ttm_bo_api.h>
>>    #include "ttm_module.h"
>>  @@ -49,9 +52,11 @@ static void ttm_global_release(void)
>>  	if (--ttm_glob_use_count > 0)
>>  		goto out;
>>  +	ttm_pool_mgr_fini();
>> +	ttm_tt_mgr_fini();
>> +
>>  	kobject_del(&glob->kobj);
>>  	kobject_put(&glob->kobj);
>> -	ttm_mem_global_release(&ttm_mem_glob);
>>  	__free_page(glob->dummy_read_page);
>>  	memset(glob, 0, sizeof(*glob));
>>  out:
>> @@ -61,6 +66,8 @@ static void ttm_global_release(void)
>>  static int ttm_global_init(void)
>>  {
>>  	struct ttm_global *glob = &ttm_glob;
>> +	unsigned long num_pages;
>> +	struct sysinfo si;
>>  	int ret = 0;
>>  	unsigned i;
>>  @@ -68,9 +75,14 @@ static int ttm_global_init(void)
>>  	if (++ttm_glob_use_count > 1)
>>  		goto out;
>>  -	ret = ttm_mem_global_init(&ttm_mem_glob);
>> -	if (ret)
>> -		goto out;
>> +	si_meminfo(&si);
>> +
>> +	/* Limit the number of pages in the pool to about 50% of the total
>> +	 * system memory.
>> +	 */
>> +	num_pages = ((u64)si.totalram * si.mem_unit) >> PAGE_SHIFT;
>> +	ttm_pool_mgr_init(num_pages * 50 / 100);
>> +	ttm_tt_mgr_init();
>>    	spin_lock_init(&glob->lru_lock);
>>  	glob->dummy_read_page = alloc_page(__GFP_ZERO | GFP_DMA32);
>> diff --git a/drivers/gpu/drm/ttm/ttm_pool.c b/drivers/gpu/drm/ttm/ttm_pool.c
>> index e0617717113f..6b0f957d63d5 100644
>> --- a/drivers/gpu/drm/ttm/ttm_pool.c
>> +++ b/drivers/gpu/drm/ttm/ttm_pool.c
>> @@ -404,16 +404,10 @@ int ttm_pool_alloc(struct ttm_pool *pool, struct ttm_tt *tt,
>>  			caching = pages + (1 << order);
>>  		}
>>  -		r = ttm_mem_global_alloc_page(&ttm_mem_glob, p,
>> -					      (1 << order) * PAGE_SIZE,
>> -					      ctx);
>> -		if (r)
>> -			goto error_free_page;
>> -
>>  		if (dma_addr) {
>>  			r = ttm_pool_map(pool, order, p, &dma_addr);
>>  			if (r)
>> -				goto error_global_free;
>> +				goto error_free_page;
>>  		}
>>    		num_pages -= 1 << order;
>> @@ -427,9 +421,6 @@ int ttm_pool_alloc(struct ttm_pool *pool, struct ttm_tt *tt,
>>    	return 0;
>>  -error_global_free:
>> -	ttm_mem_global_free_page(&ttm_mem_glob, p, (1 << order) * PAGE_SIZE);
>> -
>>  error_free_page:
>>  	ttm_pool_free_page(pool, tt->caching, order, p);
>>  @@ -464,8 +455,6 @@ void ttm_pool_free(struct ttm_pool *pool, struct ttm_tt *tt)
>>    		order = ttm_pool_page_order(pool, p);
>>  		num_pages = 1ULL << order;
>> -		ttm_mem_global_free_page(&ttm_mem_glob, p,
>> -					 num_pages * PAGE_SIZE);
>>  		if (tt->dma_address)
>>  			ttm_pool_unmap(pool, tt->dma_address[i], num_pages);
>>  diff --git a/drivers/gpu/drm/vmwgfx/Makefile b/drivers/gpu/drm/vmwgfx/Makefile
>> index cc4cdca7176e..8c02fa5852e7 100644
>> --- a/drivers/gpu/drm/vmwgfx/Makefile
>> +++ b/drivers/gpu/drm/vmwgfx/Makefile
>> @@ -9,7 +9,7 @@ vmwgfx-y := vmwgfx_execbuf.o vmwgfx_gmr.o vmwgfx_kms.o vmwgfx_drv.o \
>>  	    vmwgfx_cotable.o vmwgfx_so.o vmwgfx_binding.o vmwgfx_msg.o \
>>  	    vmwgfx_simple_resource.o vmwgfx_va.o vmwgfx_blit.o \
>>  	    vmwgfx_validation.o vmwgfx_page_dirty.o vmwgfx_streamoutput.o \
>> -	    ttm_object.o ttm_lock.o
>> +	    ttm_object.o ttm_lock.o ttm_memory.o
>>    vmwgfx-$(CONFIG_TRANSPARENT_HUGEPAGE) += vmwgfx_thp.o
>>  obj-$(CONFIG_DRM_VMWGFX) := vmwgfx.o
>> diff --git a/drivers/gpu/drm/ttm/ttm_memory.c b/drivers/gpu/drm/vmwgfx/ttm_memory.c
>> similarity index 97%
>> rename from drivers/gpu/drm/ttm/ttm_memory.c
>> rename to drivers/gpu/drm/vmwgfx/ttm_memory.c
>> index 634a85c2dc4c..1306d9e0f095 100644
>> --- a/drivers/gpu/drm/ttm/ttm_memory.c
>> +++ b/drivers/gpu/drm/vmwgfx/ttm_memory.c
>> @@ -28,7 +28,6 @@
>>    #define pr_fmt(fmt) "[TTM] " fmt
>>  -#include <drm/ttm/ttm_memory.h>
>>  #include <linux/spinlock.h>
>>  #include <linux/sched.h>
>>  #include <linux/wait.h>
>> @@ -36,10 +35,11 @@
>>  #include <linux/module.h>
>>  #include <linux/slab.h>
>>  #include <linux/swap.h>
>> -#include <drm/ttm/ttm_pool.h>
>> -#include <drm/ttm/ttm_tt.h>
>>  -#include "ttm_module.h"
>> +#include <drm/drm_device.h>
>> +#include <drm/drm_file.h>
>> +
>> +#include "ttm_memory.h"
>>    #define TTM_MEMORY_ALLOC_RETRIES 4
>>  @@ -414,7 +414,7 @@ static int ttm_mem_init_dma32_zone(struct ttm_mem_global *glob,
>>  }
>>  #endif
>>  -int ttm_mem_global_init(struct ttm_mem_global *glob)
>> +int ttm_mem_global_init(struct ttm_mem_global *glob, struct drm_device *dev)
>>  {
>>  	struct sysinfo si;
>>  	int ret;
>> @@ -425,7 +425,8 @@ int ttm_mem_global_init(struct ttm_mem_global *glob)
>>  	glob->swap_queue = create_singlethread_workqueue("ttm_swap");
>>  	INIT_WORK(&glob->work, ttm_shrink_work);
>>  	ret = kobject_init_and_add(
>> -		&glob->kobj, &ttm_mem_glob_kobj_type, ttm_get_kobj(), "memory_accounting");
>> +		&glob->kobj, &ttm_mem_glob_kobj_type, &dev->primary->kdev->kobj,
>> +		"memory_accounting");
>>  	if (unlikely(ret != 0)) {
>>  		kobject_put(&glob->kobj);
>>  		return ret;
>> @@ -453,8 +454,6 @@ int ttm_mem_global_init(struct ttm_mem_global *glob)
>>  		pr_info("Zone %7s: Available graphics memory: %llu KiB\n",
>>  			zone->name, (unsigned long long)zone->max_mem >> 10);
>>  	}
>> -	ttm_pool_mgr_init(glob->zone_kernel->max_mem/(2*PAGE_SIZE));
>> -	ttm_tt_mgr_init();
>>  	return 0;
>>  out_no_zone:
>>  	ttm_mem_global_release(glob);
>> @@ -466,10 +465,6 @@ void ttm_mem_global_release(struct ttm_mem_global *glob)
>>  	struct ttm_mem_zone *zone;
>>  	unsigned int i;
>>  -	/* let the page allocator first stop the shrink work. */
>> -	ttm_pool_mgr_fini();
>> -	ttm_tt_mgr_fini();
>> -
>>  	flush_workqueue(glob->swap_queue);
>>  	destroy_workqueue(glob->swap_queue);
>>  	glob->swap_queue = NULL;
>> diff --git a/include/drm/ttm/ttm_memory.h b/drivers/gpu/drm/vmwgfx/ttm_memory.h
>> similarity index 97%
>> rename from include/drm/ttm/ttm_memory.h
>> rename to drivers/gpu/drm/vmwgfx/ttm_memory.h
>> index c1f167881e33..850ee6c867da 100644
>> --- a/include/drm/ttm/ttm_memory.h
>> +++ b/drivers/gpu/drm/vmwgfx/ttm_memory.h
>> @@ -35,7 +35,8 @@
>>  #include <linux/errno.h>
>>  #include <linux/kobject.h>
>>  #include <linux/mm.h>
>> -#include "ttm_bo_api.h"
>> +
>> +#include <drm/ttm/ttm_bo_api.h>
>>    /**
>>   * struct ttm_mem_global - Global memory accounting structure.
>> @@ -79,7 +80,7 @@ extern struct ttm_mem_global {
>>  #endif
>>  } ttm_mem_glob;
>>  -int ttm_mem_global_init(struct ttm_mem_global *glob);
>> +int ttm_mem_global_init(struct ttm_mem_global *glob, struct drm_device *dev);
>>  void ttm_mem_global_release(struct ttm_mem_global *glob);
>>  int ttm_mem_global_alloc(struct ttm_mem_global *glob, uint64_t memory,
>>  			 struct ttm_operation_ctx *ctx);
>> diff --git a/drivers/gpu/drm/vmwgfx/ttm_object.h b/drivers/gpu/drm/vmwgfx/ttm_object.h
>> index ede26df87c93..49b064f0cb19 100644
>> --- a/drivers/gpu/drm/vmwgfx/ttm_object.h
>> +++ b/drivers/gpu/drm/vmwgfx/ttm_object.h
>> @@ -43,7 +43,8 @@
>>  #include <linux/rcupdate.h>
>>    #include <drm/drm_hashtab.h>
>> -#include <drm/ttm/ttm_memory.h>
>> +
>> +#include "ttm_memory.h"
>>    /**
>>   * enum ttm_ref_type
>> diff --git a/drivers/gpu/drm/vmwgfx/vmwgfx_bo.c b/drivers/gpu/drm/vmwgfx/vmwgfx_bo.c
>> index 6b3bfd8c678a..50e529a01677 100644
>> --- a/drivers/gpu/drm/vmwgfx/vmwgfx_bo.c
>> +++ b/drivers/gpu/drm/vmwgfx/vmwgfx_bo.c
>> @@ -507,11 +507,16 @@ int vmw_bo_create_kernel(struct vmw_private *dev_priv, unsigned long size,
>>  	acc_size = ttm_round_pot(sizeof(*bo));
>>  	acc_size += ttm_round_pot(npages * sizeof(void *));
>>  	acc_size += ttm_round_pot(sizeof(struct ttm_tt));
>> +
>> +	ret = ttm_mem_global_alloc(&ttm_mem_glob, acc_size, &ctx);
>> +	if (unlikely(ret))
>> +		goto error_free;
>> +
>>  	ret = ttm_bo_init_reserved(&dev_priv->bdev, bo, size,
>>  				   ttm_bo_type_device, placement, 0,
>> -				   &ctx, acc_size, NULL, NULL, NULL);
>> +				   &ctx, NULL, NULL, NULL);
>>  	if (unlikely(ret))
>> -		goto error_free;
>> +		goto error_account;
>>    	ttm_bo_pin(bo);
>>  	ttm_bo_unreserve(bo);
>> @@ -519,6 +524,9 @@ int vmw_bo_create_kernel(struct vmw_private *dev_priv, unsigned long size,
>>    	return 0;
>>  +error_account:
>> +	ttm_mem_global_free(&ttm_mem_glob, acc_size);
>> +
>>  error_free:
>>  	kfree(bo);
>>  	return ret;
>> @@ -558,11 +566,17 @@ int vmw_bo_init(struct vmw_private *dev_priv,
>>  	vmw_bo->base.priority = 3;
>>  	vmw_bo->res_tree = RB_ROOT;
>>  +	ret = ttm_mem_global_alloc(&ttm_mem_glob, acc_size, &ctx);
>> +	if (unlikely(ret))
>> +		return ret;
>> +
>>  	ret = ttm_bo_init_reserved(bdev, &vmw_bo->base, size,
>>  				   ttm_bo_type_device, placement,
>> -				   0, &ctx, acc_size, NULL, NULL, bo_free);
>> -	if (unlikely(ret))
>> +				   0, &ctx, NULL, NULL, bo_free);
>> +	if (unlikely(ret)) {
>> +		ttm_mem_global_free(&ttm_mem_glob, acc_size);
>>  		return ret;
>> +	}
>>    	if (pin)
>>  		ttm_bo_pin(&vmw_bo->base);
>> diff --git a/drivers/gpu/drm/vmwgfx/vmwgfx_drv.c b/drivers/gpu/drm/vmwgfx/vmwgfx_drv.c
>> index 710ba5169a74..6c0ca1011629 100644
>> --- a/drivers/gpu/drm/vmwgfx/vmwgfx_drv.c
>> +++ b/drivers/gpu/drm/vmwgfx/vmwgfx_drv.c
>> @@ -1268,6 +1268,7 @@ static void vmw_remove(struct pci_dev *pdev)
>>  {
>>  	struct drm_device *dev = pci_get_drvdata(pdev);
>>  +	ttm_mem_global_release(&ttm_mem_glob);
>>  	drm_dev_unregister(dev);
>>  	vmw_driver_unload(dev);
>>  }
>> @@ -1518,6 +1519,10 @@ static int vmw_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
>>    	pci_set_drvdata(pdev, &vmw->drm);
>>  +	ret = ttm_mem_global_init(&ttm_mem_glob, &vmw->drm);
>> +	if (ret)
>> +		return ret;
>> +
>>  	ret = vmw_driver_load(vmw, ent->device);
>>  	if (ret)
>>  		return ret;
>> diff --git a/drivers/gpu/drm/vmwgfx/vmwgfx_ttm_buffer.c b/drivers/gpu/drm/vmwgfx/vmwgfx_ttm_buffer.c
>> index d1bfa59579f1..63f10c865061 100644
>> --- a/drivers/gpu/drm/vmwgfx/vmwgfx_ttm_buffer.c
>> +++ b/drivers/gpu/drm/vmwgfx/vmwgfx_ttm_buffer.c
>> @@ -576,11 +576,31 @@ static void vmw_ttm_destroy(struct ttm_device *bdev, struct ttm_tt *ttm)
>>  static int vmw_ttm_populate(struct ttm_device *bdev,
>>  			    struct ttm_tt *ttm, struct ttm_operation_ctx *ctx)
>>  {
>> +	unsigned int i;
>> +	int ret;
>> +
>>  	/* TODO: maybe completely drop this ? */
>>  	if (ttm_tt_is_populated(ttm))
>>  		return 0;
>>  -	return ttm_pool_alloc(&bdev->pool, ttm, ctx);
>> +	ret = ttm_pool_alloc(&bdev->pool, ttm, ctx);
>> +	if (ret)
>> +		return ret;
>> +
>> +	for (i = 0; i < ttm->num_pages; ++i) {
>> +		ret = ttm_mem_global_alloc_page(&ttm_mem_glob, ttm->pages[i],
>> +						PAGE_SIZE, ctx);
>> +		if (ret)
>> +			goto error;
>> +	}
>> +	return 0;
>> +
>> +error:
>> +	while (i--)
>> +		ttm_mem_global_free_page(&ttm_mem_glob, ttm->pages[i],
>> +					 PAGE_SIZE);
>> +	ttm_pool_free(&bdev->pool, ttm);
>> +	return ret;
>>  }
>>    static void vmw_ttm_unpopulate(struct ttm_device *bdev,
>> @@ -588,6 +608,7 @@ static void vmw_ttm_unpopulate(struct ttm_device *bdev,
>>  {
>>  	struct vmw_ttm_tt *vmw_tt = container_of(ttm, struct vmw_ttm_tt,
>>  						 dma_ttm);
>> +	unsigned int i;
>>    	if (vmw_tt->mob) {
>>  		vmw_mob_destroy(vmw_tt->mob);
>> @@ -595,6 +616,11 @@ static void vmw_ttm_unpopulate(struct ttm_device *bdev,
>>  	}
>>    	vmw_ttm_unmap_dma(vmw_tt);
>> +
>> +	for (i = 0; i < ttm->num_pages; ++i)
>> +		ttm_mem_global_free_page(&ttm_mem_glob, ttm->pages[i],
>> +					 PAGE_SIZE);
>> +
>>  	ttm_pool_free(&bdev->pool, ttm);
>>  }
>>  diff --git a/include/drm/ttm/ttm_bo_api.h b/include/drm/ttm/ttm_bo_api.h
>> index 1297a8fb7ccb..4fb523dfab32 100644
>> --- a/include/drm/ttm/ttm_bo_api.h
>> +++ b/include/drm/ttm/ttm_bo_api.h
>> @@ -88,7 +88,6 @@ struct ttm_tt;
>>   * @type: The bo type.
>>   * @destroy: Destruction function. If NULL, kfree is used.
>>   * @num_pages: Actual number of pages.
>> - * @acc_size: Accounted size for this object.
>>   * @kref: Reference count of this buffer object. When this refcount reaches
>>   * zero, the object is destroyed or put on the delayed delete list.
>>   * @mem: structure describing current placement.
>> @@ -125,7 +124,6 @@ struct ttm_buffer_object {
>>  	struct ttm_device *bdev;
>>  	enum ttm_bo_type type;
>>  	void (*destroy) (struct ttm_buffer_object *);
>> -	size_t acc_size;
>>    	/**
>>  	* Members not needing protection.
>> @@ -357,10 +355,6 @@ void ttm_bo_unlock_delayed_workqueue(struct ttm_device *bdev, int resched);
>>  bool ttm_bo_eviction_valuable(struct ttm_buffer_object *bo,
>>  			      const struct ttm_place *place);
>>  -size_t ttm_bo_dma_acc_size(struct ttm_device *bdev,
>> -			   unsigned long bo_size,
>> -			   unsigned struct_size);
>> -
>>  /**
>>   * ttm_bo_init_reserved
>>   *
>> @@ -371,7 +365,6 @@ size_t ttm_bo_dma_acc_size(struct ttm_device *bdev,
>>   * @flags: Initial placement flags.
>>   * @page_alignment: Data alignment in pages.
>>   * @ctx: TTM operation context for memory allocation.
>> - * @acc_size: Accounted size for this object.
>>   * @resv: Pointer to a dma_resv, or NULL to let ttm allocate one.
>>   * @destroy: Destroy function. Use NULL for kfree().
>>   *
>> @@ -402,8 +395,7 @@ int ttm_bo_init_reserved(struct ttm_device *bdev,
>>  			 struct ttm_placement *placement,
>>  			 uint32_t page_alignment,
>>  			 struct ttm_operation_ctx *ctx,
>> -			 size_t acc_size, struct sg_table *sg,
>> -			 struct dma_resv *resv,
>> +			 struct sg_table *sg, struct dma_resv *resv,
>>  			 void (*destroy) (struct ttm_buffer_object *));
>>    /**
>> @@ -421,7 +413,6 @@ int ttm_bo_init_reserved(struct ttm_device *bdev,
>>   * holds a pointer to a persistent shmem object. Typically, this would
>>   * point to the shmem object backing a GEM object if TTM is used to back a
>>   * GEM user interface.
>> - * @acc_size: Accounted size for this object.
>>   * @resv: Pointer to a dma_resv, or NULL to let ttm allocate one.
>>   * @destroy: Destroy function. Use NULL for kfree().
>>   *
>> @@ -446,7 +437,7 @@ int ttm_bo_init_reserved(struct ttm_device *bdev,
>>  int ttm_bo_init(struct ttm_device *bdev, struct ttm_buffer_object *bo,
>>  		size_t size, enum ttm_bo_type type,
>>  		struct ttm_placement *placement,
>> -		uint32_t page_alignment, bool interrubtible, size_t acc_size,
>> +		uint32_t page_alignment, bool interrubtible,
>>  		struct sg_table *sg, struct dma_resv *resv,
>>  		void (*destroy) (struct ttm_buffer_object *));
>>  diff --git a/include/drm/ttm/ttm_bo_driver.h b/include/drm/ttm/ttm_bo_driver.h
>> index 1c9bf993e252..8959c0075cfd 100644
>> --- a/include/drm/ttm/ttm_bo_driver.h
>> +++ b/include/drm/ttm/ttm_bo_driver.h
>> @@ -40,7 +40,6 @@
>>  #include <drm/ttm/ttm_device.h>
>>    #include "ttm_bo_api.h"
>> -#include "ttm_memory.h"
>>  #include "ttm_placement.h"
>>  #include "ttm_tt.h"
>>  #include "ttm_pool.h"
>> diff --git a/include/drm/ttm/ttm_tt.h b/include/drm/ttm/ttm_tt.h
>> index cce57fb49e2c..069f8130241a 100644
>> --- a/include/drm/ttm/ttm_tt.h
>> +++ b/include/drm/ttm/ttm_tt.h
>> @@ -30,6 +30,7 @@
>>  #include <linux/types.h>
>>  #include <drm/ttm/ttm_caching.h>
>>  +struct ttm_bo_device;
>>  struct ttm_tt;
>>  struct ttm_resource;
>>  struct ttm_buffer_object;
> 

_______________________________________________
dri-devel mailing list
dri-devel@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/dri-devel

^ permalink raw reply	[flat|nested] 17+ messages in thread

* Re: [PATCH 2/3] drm/ttm: move memory accounting into vmwgfx v3
  2021-02-02 15:14     ` Zack Rusin
@ 2021-02-02 15:16       ` Christian König
  2021-02-02 17:42         ` Zack Rusin
  0 siblings, 1 reply; 17+ messages in thread
From: Christian König @ 2021-02-02 15:16 UTC (permalink / raw)
  To: Zack Rusin; +Cc: Linux-graphics-maintainer, Roland Scheidegger, dri-devel

Hi Zack,

can you also give it a quick smoke test?

I'm not sure if I wired up all the sysfs magic correctly inside vmwgfx, 
but I currently don't have a setup where I can test this.

Thanks,
Christian.

Am 02.02.21 um 16:14 schrieb Zack Rusin:
> Looks good. There’s probably not much reason to call it ttm_memory anymore as it only deals with ttm_mem_glob, we’ll likely fold it in after you submit. Thanks.
>
> Reviewed-by: Zack Rusin <zackr@vmware.com>
>
> z
>
>> On Feb 2, 2021, at 08:04, Christian König <christian.koenig@amd.com> wrote:
>>
>> Ping?
>>
>> Especially Roland and Zack do you have any objections to this?
>>
>> Regards,
>> Christian.
>>
>> Am 28.01.21 um 14:16 schrieb Christian König:
>>> This is just another feature which is only used by VMWGFX, so move
>>> it into the driver instead.
>>>
>>> I've tried to add the accounting sysfs file to the kobject of the drm
>>> minor, but I'm not 100% sure if this works as expected.
>>>
>>> v2: fix typo in KFD and avoid 64bit divide
>>> v3: fix init order in VMWGFX
>>>
>>> Signed-off-by: Christian König <christian.koenig@amd.com>
>>> ---
>>>   .../gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c  | 16 ++++++---
>>>   drivers/gpu/drm/amd/amdgpu/amdgpu_object.c    |  8 ++---
>>>   drivers/gpu/drm/drm_gem_vram_helper.c         |  6 ++--
>>>   drivers/gpu/drm/nouveau/nouveau_bo.c          |  7 ++--
>>>   drivers/gpu/drm/nouveau/nouveau_drv.h         |  1 -
>>>   drivers/gpu/drm/qxl/qxl_object.c              |  4 +--
>>>   drivers/gpu/drm/radeon/radeon_object.c        |  8 ++---
>>>   drivers/gpu/drm/ttm/Makefile                  |  7 ++--
>>>   drivers/gpu/drm/ttm/ttm_bo.c                  | 33 +------------------
>>>   drivers/gpu/drm/ttm/ttm_bo_util.c             |  1 -
>>>   drivers/gpu/drm/ttm/ttm_device.c              | 22 ++++++++++---
>>>   drivers/gpu/drm/ttm/ttm_pool.c                | 13 +-------
>>>   drivers/gpu/drm/vmwgfx/Makefile               |  2 +-
>>>   drivers/gpu/drm/{ttm => vmwgfx}/ttm_memory.c  | 19 ++++-------
>>>   .../gpu/drm/vmwgfx}/ttm_memory.h              |  5 +--
>>>   drivers/gpu/drm/vmwgfx/ttm_object.h           |  3 +-
>>>   drivers/gpu/drm/vmwgfx/vmwgfx_bo.c            | 22 ++++++++++---
>>>   drivers/gpu/drm/vmwgfx/vmwgfx_drv.c           |  5 +++
>>>   drivers/gpu/drm/vmwgfx/vmwgfx_ttm_buffer.c    | 28 +++++++++++++++-
>>>   include/drm/ttm/ttm_bo_api.h                  | 13 ++------
>>>   include/drm/ttm/ttm_bo_driver.h               |  1 -
>>>   include/drm/ttm/ttm_tt.h                      |  1 +
>>>   22 files changed, 110 insertions(+), 115 deletions(-)
>>>   rename drivers/gpu/drm/{ttm => vmwgfx}/ttm_memory.c (97%)
>>>   rename {include/drm/ttm => drivers/gpu/drm/vmwgfx}/ttm_memory.h (97%)
>>>
>>> diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c b/drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c
>>> index 0849b68e784f..e440af37dde8 100644
>>> --- a/drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c
>>> +++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c
>>> @@ -118,6 +118,16 @@ void amdgpu_amdkfd_gpuvm_init_mem_limits(void)
>>>    */
>>>   #define ESTIMATE_PT_SIZE(mem_size) ((mem_size) >> 14)
>>>   +static size_t amdgpu_amdkfd_acc_size(uint64_t size)
>>> +{
>>> +	size >>= PAGE_SHIFT;
>>> +	size *= sizeof(dma_addr_t) + sizeof(void *);
>>> +
>>> +	return __roundup_pow_of_two(sizeof(struct amdgpu_bo)) +
>>> +		__roundup_pow_of_two(sizeof(struct ttm_tt)) +
>>> +		PAGE_ALIGN(size);
>>> +}
>>> +
>>>   static int amdgpu_amdkfd_reserve_mem_limit(struct amdgpu_device *adev,
>>>   		uint64_t size, u32 domain, bool sg)
>>>   {
>>> @@ -126,8 +136,7 @@ static int amdgpu_amdkfd_reserve_mem_limit(struct amdgpu_device *adev,
>>>   	size_t acc_size, system_mem_needed, ttm_mem_needed, vram_needed;
>>>   	int ret = 0;
>>>   -	acc_size = ttm_bo_dma_acc_size(&adev->mman.bdev, size,
>>> -				       sizeof(struct amdgpu_bo));
>>> +	acc_size = amdgpu_amdkfd_acc_size(size);
>>>     	vram_needed = 0;
>>>   	if (domain == AMDGPU_GEM_DOMAIN_GTT) {
>>> @@ -174,8 +183,7 @@ static void unreserve_mem_limit(struct amdgpu_device *adev,
>>>   {
>>>   	size_t acc_size;
>>>   -	acc_size = ttm_bo_dma_acc_size(&adev->mman.bdev, size,
>>> -				       sizeof(struct amdgpu_bo));
>>> +	acc_size = amdgpu_amdkfd_acc_size(size);
>>>     	spin_lock(&kfd_mem_limit.mem_limit_lock);
>>>   	if (domain == AMDGPU_GEM_DOMAIN_GTT) {
>>> diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_object.c b/drivers/gpu/drm/amd/amdgpu/amdgpu_object.c
>>> index 6cc9919b12cc..599c9a132eb6 100644
>>> --- a/drivers/gpu/drm/amd/amdgpu/amdgpu_object.c
>>> +++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_object.c
>>> @@ -523,7 +523,6 @@ static int amdgpu_bo_do_create(struct amdgpu_device *adev,
>>>   	};
>>>   	struct amdgpu_bo *bo;
>>>   	unsigned long page_align, size = bp->size;
>>> -	size_t acc_size;
>>>   	int r;
>>>     	/* Note that GDS/GWS/OA allocates 1 page per byte/resource. */
>>> @@ -546,9 +545,6 @@ static int amdgpu_bo_do_create(struct amdgpu_device *adev,
>>>     	*bo_ptr = NULL;
>>>   -	acc_size = ttm_bo_dma_acc_size(&adev->mman.bdev, size,
>>> -				       sizeof(struct amdgpu_bo));
>>> -
>>>   	bo = kzalloc(sizeof(struct amdgpu_bo), GFP_KERNEL);
>>>   	if (bo == NULL)
>>>   		return -ENOMEM;
>>> @@ -577,8 +573,8 @@ static int amdgpu_bo_do_create(struct amdgpu_device *adev,
>>>   		bo->tbo.priority = 1;
>>>     	r = ttm_bo_init_reserved(&adev->mman.bdev, &bo->tbo, size, bp->type,
>>> -				 &bo->placement, page_align, &ctx, acc_size,
>>> -				 NULL, bp->resv, &amdgpu_bo_destroy);
>>> +				 &bo->placement, page_align, &ctx,  NULL,
>>> +				 bp->resv, &amdgpu_bo_destroy);
>>>   	if (unlikely(r != 0))
>>>   		return r;
>>>   diff --git a/drivers/gpu/drm/drm_gem_vram_helper.c b/drivers/gpu/drm/drm_gem_vram_helper.c
>>> index 0b13c8507688..a0992f0b8afd 100644
>>> --- a/drivers/gpu/drm/drm_gem_vram_helper.c
>>> +++ b/drivers/gpu/drm/drm_gem_vram_helper.c
>>> @@ -189,7 +189,6 @@ struct drm_gem_vram_object *drm_gem_vram_create(struct drm_device *dev,
>>>   	struct drm_vram_mm *vmm = dev->vram_mm;
>>>   	struct ttm_device *bdev;
>>>   	int ret;
>>> -	size_t acc_size;
>>>     	if (WARN_ONCE(!vmm, "VRAM MM not initialized"))
>>>   		return ERR_PTR(-EINVAL);
>>> @@ -216,7 +215,6 @@ struct drm_gem_vram_object *drm_gem_vram_create(struct drm_device *dev,
>>>   	}
>>>     	bdev = &vmm->bdev;
>>> -	acc_size = ttm_bo_dma_acc_size(bdev, size, sizeof(*gbo));
>>>     	gbo->bo.bdev = bdev;
>>>   	drm_gem_vram_placement(gbo, DRM_GEM_VRAM_PL_FLAG_SYSTEM);
>>> @@ -226,8 +224,8 @@ struct drm_gem_vram_object *drm_gem_vram_create(struct drm_device *dev,
>>>   	 * to release gbo->bo.base and kfree gbo.
>>>   	 */
>>>   	ret = ttm_bo_init(bdev, &gbo->bo, size, ttm_bo_type_device,
>>> -			  &gbo->placement, pg_align, false, acc_size,
>>> -			  NULL, NULL, ttm_buffer_object_destroy);
>>> +			  &gbo->placement, pg_align, false, NULL, NULL,
>>> +			  ttm_buffer_object_destroy);
>>>   	if (ret)
>>>   		return ERR_PTR(ret);
>>>   diff --git a/drivers/gpu/drm/nouveau/nouveau_bo.c b/drivers/gpu/drm/nouveau/nouveau_bo.c
>>> index c177940d6e2c..ca2a8ae1938e 100644
>>> --- a/drivers/gpu/drm/nouveau/nouveau_bo.c
>>> +++ b/drivers/gpu/drm/nouveau/nouveau_bo.c
>>> @@ -300,18 +300,15 @@ nouveau_bo_init(struct nouveau_bo *nvbo, u64 size, int align, u32 domain,
>>>   		struct sg_table *sg, struct dma_resv *robj)
>>>   {
>>>   	int type = sg ? ttm_bo_type_sg : ttm_bo_type_device;
>>> -	size_t acc_size;
>>>   	int ret;
>>>   -	acc_size = ttm_bo_dma_acc_size(nvbo->bo.bdev, size, sizeof(*nvbo));
>>> -
>>>   	nvbo->bo.mem.num_pages = size >> PAGE_SHIFT;
>>>   	nouveau_bo_placement_set(nvbo, domain, 0);
>>>   	INIT_LIST_HEAD(&nvbo->io_reserve_lru);
>>>     	ret = ttm_bo_init(nvbo->bo.bdev, &nvbo->bo, size, type,
>>> -			  &nvbo->placement, align >> PAGE_SHIFT, false,
>>> -			  acc_size, sg, robj, nouveau_bo_del_ttm);
>>> +			  &nvbo->placement, align >> PAGE_SHIFT, false, sg,
>>> +			  robj, nouveau_bo_del_ttm);
>>>   	if (ret) {
>>>   		/* ttm will call nouveau_bo_del_ttm if it fails.. */
>>>   		return ret;
>>> diff --git a/drivers/gpu/drm/nouveau/nouveau_drv.h b/drivers/gpu/drm/nouveau/nouveau_drv.h
>>> index edf9d1ee9d58..a491c2c1c56e 100644
>>> --- a/drivers/gpu/drm/nouveau/nouveau_drv.h
>>> +++ b/drivers/gpu/drm/nouveau/nouveau_drv.h
>>> @@ -54,7 +54,6 @@
>>>   #include <drm/ttm/ttm_bo_api.h>
>>>   #include <drm/ttm/ttm_bo_driver.h>
>>>   #include <drm/ttm/ttm_placement.h>
>>> -#include <drm/ttm/ttm_memory.h>
>>>     #include <drm/drm_audio_component.h>
>>>   diff --git a/drivers/gpu/drm/qxl/qxl_object.c b/drivers/gpu/drm/qxl/qxl_object.c
>>> index ceebc5881f68..705b51535492 100644
>>> --- a/drivers/gpu/drm/qxl/qxl_object.c
>>> +++ b/drivers/gpu/drm/qxl/qxl_object.c
>>> @@ -138,8 +138,8 @@ int qxl_bo_create(struct qxl_device *qdev,
>>>   	qxl_ttm_placement_from_domain(bo, domain);
>>>     	r = ttm_bo_init_reserved(&qdev->mman.bdev, &bo->tbo, size, type,
>>> -				 &bo->placement, 0, &ctx, size,
>>> -				 NULL, NULL, &qxl_ttm_bo_destroy);
>>> +				 &bo->placement, 0, &ctx, NULL, NULL,
>>> +				 &qxl_ttm_bo_destroy);
>>>   	if (unlikely(r != 0)) {
>>>   		if (r != -ERESTARTSYS)
>>>   			dev_err(qdev->ddev.dev,
>>> diff --git a/drivers/gpu/drm/radeon/radeon_object.c b/drivers/gpu/drm/radeon/radeon_object.c
>>> index 6a336284466f..804f7a427be7 100644
>>> --- a/drivers/gpu/drm/radeon/radeon_object.c
>>> +++ b/drivers/gpu/drm/radeon/radeon_object.c
>>> @@ -159,7 +159,6 @@ int radeon_bo_create(struct radeon_device *rdev,
>>>   	struct radeon_bo *bo;
>>>   	enum ttm_bo_type type;
>>>   	unsigned long page_align = roundup(byte_align, PAGE_SIZE) >> PAGE_SHIFT;
>>> -	size_t acc_size;
>>>   	int r;
>>>     	size = ALIGN(size, PAGE_SIZE);
>>> @@ -173,9 +172,6 @@ int radeon_bo_create(struct radeon_device *rdev,
>>>   	}
>>>   	*bo_ptr = NULL;
>>>   -	acc_size = ttm_bo_dma_acc_size(&rdev->mman.bdev, size,
>>> -				       sizeof(struct radeon_bo));
>>> -
>>>   	bo = kzalloc(sizeof(struct radeon_bo), GFP_KERNEL);
>>>   	if (bo == NULL)
>>>   		return -ENOMEM;
>>> @@ -230,8 +226,8 @@ int radeon_bo_create(struct radeon_device *rdev,
>>>   	/* Kernel allocation are uninterruptible */
>>>   	down_read(&rdev->pm.mclk_lock);
>>>   	r = ttm_bo_init(&rdev->mman.bdev, &bo->tbo, size, type,
>>> -			&bo->placement, page_align, !kernel, acc_size,
>>> -			sg, resv, &radeon_ttm_bo_destroy);
>>> +			&bo->placement, page_align, !kernel, sg, resv,
>>> +			&radeon_ttm_bo_destroy);
>>>   	up_read(&rdev->pm.mclk_lock);
>>>   	if (unlikely(r != 0)) {
>>>   		return r;
>>> diff --git a/drivers/gpu/drm/ttm/Makefile b/drivers/gpu/drm/ttm/Makefile
>>> index 8e6437eadabe..40e5e9da7953 100644
>>> --- a/drivers/gpu/drm/ttm/Makefile
>>> +++ b/drivers/gpu/drm/ttm/Makefile
>>> @@ -2,10 +2,9 @@
>>>   #
>>>   # Makefile for the drm device driver.  This driver provides support for the
>>>   -ttm-y := ttm_memory.o ttm_tt.o ttm_bo.o \
>>> -	ttm_bo_util.o ttm_bo_vm.o ttm_module.o \
>>> -	ttm_execbuf_util.o ttm_range_manager.o \
>>> -	ttm_resource.o ttm_pool.o ttm_device.o
>>> +ttm-y := ttm_tt.o ttm_bo.o ttm_bo_util.o ttm_bo_vm.o ttm_module.o \
>>> +	ttm_execbuf_util.o ttm_range_manager.o ttm_resource.o ttm_pool.o \
>>> +	ttm_device.o
>>>   ttm-$(CONFIG_AGP) += ttm_agp_backend.o
>>>     obj-$(CONFIG_DRM_TTM) += ttm.o
>>> diff --git a/drivers/gpu/drm/ttm/ttm_bo.c b/drivers/gpu/drm/ttm/ttm_bo.c
>>> index 643befc1a6f2..e38102282fd5 100644
>>> --- a/drivers/gpu/drm/ttm/ttm_bo.c
>>> +++ b/drivers/gpu/drm/ttm/ttm_bo.c
>>> @@ -425,7 +425,6 @@ static void ttm_bo_release(struct kref *kref)
>>>   	struct ttm_buffer_object *bo =
>>>   	    container_of(kref, struct ttm_buffer_object, kref);
>>>   	struct ttm_device *bdev = bo->bdev;
>>> -	size_t acc_size = bo->acc_size;
>>>   	int ret;
>>>     	if (!bo->deleted) {
>>> @@ -485,7 +484,6 @@ static void ttm_bo_release(struct kref *kref)
>>>   	if (!ttm_bo_uses_embedded_gem_object(bo))
>>>   		dma_resv_fini(&bo->base._resv);
>>>   	bo->destroy(bo);
>>> -	ttm_mem_global_free(&ttm_mem_glob, acc_size);
>>>   }
>>>     void ttm_bo_put(struct ttm_buffer_object *bo)
>>> @@ -1046,25 +1044,13 @@ int ttm_bo_init_reserved(struct ttm_device *bdev,
>>>   			 struct ttm_placement *placement,
>>>   			 uint32_t page_alignment,
>>>   			 struct ttm_operation_ctx *ctx,
>>> -			 size_t acc_size,
>>>   			 struct sg_table *sg,
>>>   			 struct dma_resv *resv,
>>>   			 void (*destroy) (struct ttm_buffer_object *))
>>>   {
>>> -	struct ttm_mem_global *mem_glob = &ttm_mem_glob;
>>>   	bool locked;
>>>   	int ret = 0;
>>>   -	ret = ttm_mem_global_alloc(mem_glob, acc_size, ctx);
>>> -	if (ret) {
>>> -		pr_err("Out of kernel memory\n");
>>> -		if (destroy)
>>> -			(*destroy)(bo);
>>> -		else
>>> -			kfree(bo);
>>> -		return -ENOMEM;
>>> -	}
>>> -
>>>   	bo->destroy = destroy ? destroy : ttm_bo_default_destroy;
>>>     	kref_init(&bo->kref);
>>> @@ -1081,7 +1067,6 @@ int ttm_bo_init_reserved(struct ttm_device *bdev,
>>>   	bo->mem.bus.addr = NULL;
>>>   	bo->moving = NULL;
>>>   	bo->mem.placement = 0;
>>> -	bo->acc_size = acc_size;
>>>   	bo->pin_count = 0;
>>>   	bo->sg = sg;
>>>   	if (resv) {
>>> @@ -1142,7 +1127,6 @@ int ttm_bo_init(struct ttm_device *bdev,
>>>   		struct ttm_placement *placement,
>>>   		uint32_t page_alignment,
>>>   		bool interruptible,
>>> -		size_t acc_size,
>>>   		struct sg_table *sg,
>>>   		struct dma_resv *resv,
>>>   		void (*destroy) (struct ttm_buffer_object *))
>>> @@ -1151,8 +1135,7 @@ int ttm_bo_init(struct ttm_device *bdev,
>>>   	int ret;
>>>     	ret = ttm_bo_init_reserved(bdev, bo, size, type, placement,
>>> -				   page_alignment, &ctx, acc_size,
>>> -				   sg, resv, destroy);
>>> +				   page_alignment, &ctx, sg, resv, destroy);
>>>   	if (ret)
>>>   		return ret;
>>>   @@ -1163,20 +1146,6 @@ int ttm_bo_init(struct ttm_device *bdev,
>>>   }
>>>   EXPORT_SYMBOL(ttm_bo_init);
>>>   -size_t ttm_bo_dma_acc_size(struct ttm_device *bdev,
>>> -			   unsigned long bo_size,
>>> -			   unsigned struct_size)
>>> -{
>>> -	unsigned npages = (PAGE_ALIGN(bo_size)) >> PAGE_SHIFT;
>>> -	size_t size = 0;
>>> -
>>> -	size += ttm_round_pot(struct_size);
>>> -	size += ttm_round_pot(npages * (2*sizeof(void *) + sizeof(dma_addr_t)));
>>> -	size += ttm_round_pot(sizeof(struct ttm_tt));
>>> -	return size;
>>> -}
>>> -EXPORT_SYMBOL(ttm_bo_dma_acc_size);
>>> -
>>>   /*
>>>    * buffer object vm functions.
>>>    */
>>> diff --git a/drivers/gpu/drm/ttm/ttm_bo_util.c b/drivers/gpu/drm/ttm/ttm_bo_util.c
>>> index db0f2661d504..031e5819fec4 100644
>>> --- a/drivers/gpu/drm/ttm/ttm_bo_util.c
>>> +++ b/drivers/gpu/drm/ttm/ttm_bo_util.c
>>> @@ -309,7 +309,6 @@ static int ttm_buffer_object_transfer(struct ttm_buffer_object *bo,
>>>     	kref_init(&fbo->base.kref);
>>>   	fbo->base.destroy = &ttm_transfered_destroy;
>>> -	fbo->base.acc_size = 0;
>>>   	fbo->base.pin_count = 0;
>>>   	if (bo->type != ttm_bo_type_sg)
>>>   		fbo->base.base.resv = &fbo->base.base._resv;
>>> diff --git a/drivers/gpu/drm/ttm/ttm_device.c b/drivers/gpu/drm/ttm/ttm_device.c
>>> index ac0903c9e60a..6bde344e5da7 100644
>>> --- a/drivers/gpu/drm/ttm/ttm_device.c
>>> +++ b/drivers/gpu/drm/ttm/ttm_device.c
>>> @@ -27,9 +27,12 @@
>>>     #define pr_fmt(fmt) "[TTM DEVICE] " fmt
>>>   +#include <linux/mm.h>
>>> +
>>>   #include <drm/ttm/ttm_device.h>
>>> -#include <drm/ttm/ttm_memory.h>
>>> +#include <drm/ttm/ttm_tt.h>
>>>   #include <drm/ttm/ttm_placement.h>
>>> +#include <drm/ttm/ttm_bo_api.h>
>>>     #include "ttm_module.h"
>>>   @@ -49,9 +52,11 @@ static void ttm_global_release(void)
>>>   	if (--ttm_glob_use_count > 0)
>>>   		goto out;
>>>   +	ttm_pool_mgr_fini();
>>> +	ttm_tt_mgr_fini();
>>> +
>>>   	kobject_del(&glob->kobj);
>>>   	kobject_put(&glob->kobj);
>>> -	ttm_mem_global_release(&ttm_mem_glob);
>>>   	__free_page(glob->dummy_read_page);
>>>   	memset(glob, 0, sizeof(*glob));
>>>   out:
>>> @@ -61,6 +66,8 @@ static void ttm_global_release(void)
>>>   static int ttm_global_init(void)
>>>   {
>>>   	struct ttm_global *glob = &ttm_glob;
>>> +	unsigned long num_pages;
>>> +	struct sysinfo si;
>>>   	int ret = 0;
>>>   	unsigned i;
>>>   @@ -68,9 +75,14 @@ static int ttm_global_init(void)
>>>   	if (++ttm_glob_use_count > 1)
>>>   		goto out;
>>>   -	ret = ttm_mem_global_init(&ttm_mem_glob);
>>> -	if (ret)
>>> -		goto out;
>>> +	si_meminfo(&si);
>>> +
>>> +	/* Limit the number of pages in the pool to about 50% of the total
>>> +	 * system memory.
>>> +	 */
>>> +	num_pages = ((u64)si.totalram * si.mem_unit) >> PAGE_SHIFT;
>>> +	ttm_pool_mgr_init(num_pages * 50 / 100);
>>> +	ttm_tt_mgr_init();
>>>     	spin_lock_init(&glob->lru_lock);
>>>   	glob->dummy_read_page = alloc_page(__GFP_ZERO | GFP_DMA32);
>>> diff --git a/drivers/gpu/drm/ttm/ttm_pool.c b/drivers/gpu/drm/ttm/ttm_pool.c
>>> index e0617717113f..6b0f957d63d5 100644
>>> --- a/drivers/gpu/drm/ttm/ttm_pool.c
>>> +++ b/drivers/gpu/drm/ttm/ttm_pool.c
>>> @@ -404,16 +404,10 @@ int ttm_pool_alloc(struct ttm_pool *pool, struct ttm_tt *tt,
>>>   			caching = pages + (1 << order);
>>>   		}
>>>   -		r = ttm_mem_global_alloc_page(&ttm_mem_glob, p,
>>> -					      (1 << order) * PAGE_SIZE,
>>> -					      ctx);
>>> -		if (r)
>>> -			goto error_free_page;
>>> -
>>>   		if (dma_addr) {
>>>   			r = ttm_pool_map(pool, order, p, &dma_addr);
>>>   			if (r)
>>> -				goto error_global_free;
>>> +				goto error_free_page;
>>>   		}
>>>     		num_pages -= 1 << order;
>>> @@ -427,9 +421,6 @@ int ttm_pool_alloc(struct ttm_pool *pool, struct ttm_tt *tt,
>>>     	return 0;
>>>   -error_global_free:
>>> -	ttm_mem_global_free_page(&ttm_mem_glob, p, (1 << order) * PAGE_SIZE);
>>> -
>>>   error_free_page:
>>>   	ttm_pool_free_page(pool, tt->caching, order, p);
>>>   @@ -464,8 +455,6 @@ void ttm_pool_free(struct ttm_pool *pool, struct ttm_tt *tt)
>>>     		order = ttm_pool_page_order(pool, p);
>>>   		num_pages = 1ULL << order;
>>> -		ttm_mem_global_free_page(&ttm_mem_glob, p,
>>> -					 num_pages * PAGE_SIZE);
>>>   		if (tt->dma_address)
>>>   			ttm_pool_unmap(pool, tt->dma_address[i], num_pages);
>>>   diff --git a/drivers/gpu/drm/vmwgfx/Makefile b/drivers/gpu/drm/vmwgfx/Makefile
>>> index cc4cdca7176e..8c02fa5852e7 100644
>>> --- a/drivers/gpu/drm/vmwgfx/Makefile
>>> +++ b/drivers/gpu/drm/vmwgfx/Makefile
>>> @@ -9,7 +9,7 @@ vmwgfx-y := vmwgfx_execbuf.o vmwgfx_gmr.o vmwgfx_kms.o vmwgfx_drv.o \
>>>   	    vmwgfx_cotable.o vmwgfx_so.o vmwgfx_binding.o vmwgfx_msg.o \
>>>   	    vmwgfx_simple_resource.o vmwgfx_va.o vmwgfx_blit.o \
>>>   	    vmwgfx_validation.o vmwgfx_page_dirty.o vmwgfx_streamoutput.o \
>>> -	    ttm_object.o ttm_lock.o
>>> +	    ttm_object.o ttm_lock.o ttm_memory.o
>>>     vmwgfx-$(CONFIG_TRANSPARENT_HUGEPAGE) += vmwgfx_thp.o
>>>   obj-$(CONFIG_DRM_VMWGFX) := vmwgfx.o
>>> diff --git a/drivers/gpu/drm/ttm/ttm_memory.c b/drivers/gpu/drm/vmwgfx/ttm_memory.c
>>> similarity index 97%
>>> rename from drivers/gpu/drm/ttm/ttm_memory.c
>>> rename to drivers/gpu/drm/vmwgfx/ttm_memory.c
>>> index 634a85c2dc4c..1306d9e0f095 100644
>>> --- a/drivers/gpu/drm/ttm/ttm_memory.c
>>> +++ b/drivers/gpu/drm/vmwgfx/ttm_memory.c
>>> @@ -28,7 +28,6 @@
>>>     #define pr_fmt(fmt) "[TTM] " fmt
>>>   -#include <drm/ttm/ttm_memory.h>
>>>   #include <linux/spinlock.h>
>>>   #include <linux/sched.h>
>>>   #include <linux/wait.h>
>>> @@ -36,10 +35,11 @@
>>>   #include <linux/module.h>
>>>   #include <linux/slab.h>
>>>   #include <linux/swap.h>
>>> -#include <drm/ttm/ttm_pool.h>
>>> -#include <drm/ttm/ttm_tt.h>
>>>   -#include "ttm_module.h"
>>> +#include <drm/drm_device.h>
>>> +#include <drm/drm_file.h>
>>> +
>>> +#include "ttm_memory.h"
>>>     #define TTM_MEMORY_ALLOC_RETRIES 4
>>>   @@ -414,7 +414,7 @@ static int ttm_mem_init_dma32_zone(struct ttm_mem_global *glob,
>>>   }
>>>   #endif
>>>   -int ttm_mem_global_init(struct ttm_mem_global *glob)
>>> +int ttm_mem_global_init(struct ttm_mem_global *glob, struct drm_device *dev)
>>>   {
>>>   	struct sysinfo si;
>>>   	int ret;
>>> @@ -425,7 +425,8 @@ int ttm_mem_global_init(struct ttm_mem_global *glob)
>>>   	glob->swap_queue = create_singlethread_workqueue("ttm_swap");
>>>   	INIT_WORK(&glob->work, ttm_shrink_work);
>>>   	ret = kobject_init_and_add(
>>> -		&glob->kobj, &ttm_mem_glob_kobj_type, ttm_get_kobj(), "memory_accounting");
>>> +		&glob->kobj, &ttm_mem_glob_kobj_type, &dev->primary->kdev->kobj,
>>> +		"memory_accounting");
>>>   	if (unlikely(ret != 0)) {
>>>   		kobject_put(&glob->kobj);
>>>   		return ret;
>>> @@ -453,8 +454,6 @@ int ttm_mem_global_init(struct ttm_mem_global *glob)
>>>   		pr_info("Zone %7s: Available graphics memory: %llu KiB\n",
>>>   			zone->name, (unsigned long long)zone->max_mem >> 10);
>>>   	}
>>> -	ttm_pool_mgr_init(glob->zone_kernel->max_mem/(2*PAGE_SIZE));
>>> -	ttm_tt_mgr_init();
>>>   	return 0;
>>>   out_no_zone:
>>>   	ttm_mem_global_release(glob);
>>> @@ -466,10 +465,6 @@ void ttm_mem_global_release(struct ttm_mem_global *glob)
>>>   	struct ttm_mem_zone *zone;
>>>   	unsigned int i;
>>>   -	/* let the page allocator first stop the shrink work. */
>>> -	ttm_pool_mgr_fini();
>>> -	ttm_tt_mgr_fini();
>>> -
>>>   	flush_workqueue(glob->swap_queue);
>>>   	destroy_workqueue(glob->swap_queue);
>>>   	glob->swap_queue = NULL;
>>> diff --git a/include/drm/ttm/ttm_memory.h b/drivers/gpu/drm/vmwgfx/ttm_memory.h
>>> similarity index 97%
>>> rename from include/drm/ttm/ttm_memory.h
>>> rename to drivers/gpu/drm/vmwgfx/ttm_memory.h
>>> index c1f167881e33..850ee6c867da 100644
>>> --- a/include/drm/ttm/ttm_memory.h
>>> +++ b/drivers/gpu/drm/vmwgfx/ttm_memory.h
>>> @@ -35,7 +35,8 @@
>>>   #include <linux/errno.h>
>>>   #include <linux/kobject.h>
>>>   #include <linux/mm.h>
>>> -#include "ttm_bo_api.h"
>>> +
>>> +#include <drm/ttm/ttm_bo_api.h>
>>>     /**
>>>    * struct ttm_mem_global - Global memory accounting structure.
>>> @@ -79,7 +80,7 @@ extern struct ttm_mem_global {
>>>   #endif
>>>   } ttm_mem_glob;
>>>   -int ttm_mem_global_init(struct ttm_mem_global *glob);
>>> +int ttm_mem_global_init(struct ttm_mem_global *glob, struct drm_device *dev);
>>>   void ttm_mem_global_release(struct ttm_mem_global *glob);
>>>   int ttm_mem_global_alloc(struct ttm_mem_global *glob, uint64_t memory,
>>>   			 struct ttm_operation_ctx *ctx);
>>> diff --git a/drivers/gpu/drm/vmwgfx/ttm_object.h b/drivers/gpu/drm/vmwgfx/ttm_object.h
>>> index ede26df87c93..49b064f0cb19 100644
>>> --- a/drivers/gpu/drm/vmwgfx/ttm_object.h
>>> +++ b/drivers/gpu/drm/vmwgfx/ttm_object.h
>>> @@ -43,7 +43,8 @@
>>>   #include <linux/rcupdate.h>
>>>     #include <drm/drm_hashtab.h>
>>> -#include <drm/ttm/ttm_memory.h>
>>> +
>>> +#include "ttm_memory.h"
>>>     /**
>>>    * enum ttm_ref_type
>>> diff --git a/drivers/gpu/drm/vmwgfx/vmwgfx_bo.c b/drivers/gpu/drm/vmwgfx/vmwgfx_bo.c
>>> index 6b3bfd8c678a..50e529a01677 100644
>>> --- a/drivers/gpu/drm/vmwgfx/vmwgfx_bo.c
>>> +++ b/drivers/gpu/drm/vmwgfx/vmwgfx_bo.c
>>> @@ -507,11 +507,16 @@ int vmw_bo_create_kernel(struct vmw_private *dev_priv, unsigned long size,
>>>   	acc_size = ttm_round_pot(sizeof(*bo));
>>>   	acc_size += ttm_round_pot(npages * sizeof(void *));
>>>   	acc_size += ttm_round_pot(sizeof(struct ttm_tt));
>>> +
>>> +	ret = ttm_mem_global_alloc(&ttm_mem_glob, acc_size, &ctx);
>>> +	if (unlikely(ret))
>>> +		goto error_free;
>>> +
>>>   	ret = ttm_bo_init_reserved(&dev_priv->bdev, bo, size,
>>>   				   ttm_bo_type_device, placement, 0,
>>> -				   &ctx, acc_size, NULL, NULL, NULL);
>>> +				   &ctx, NULL, NULL, NULL);
>>>   	if (unlikely(ret))
>>> -		goto error_free;
>>> +		goto error_account;
>>>     	ttm_bo_pin(bo);
>>>   	ttm_bo_unreserve(bo);
>>> @@ -519,6 +524,9 @@ int vmw_bo_create_kernel(struct vmw_private *dev_priv, unsigned long size,
>>>     	return 0;
>>>   +error_account:
>>> +	ttm_mem_global_free(&ttm_mem_glob, acc_size);
>>> +
>>>   error_free:
>>>   	kfree(bo);
>>>   	return ret;
>>> @@ -558,11 +566,17 @@ int vmw_bo_init(struct vmw_private *dev_priv,
>>>   	vmw_bo->base.priority = 3;
>>>   	vmw_bo->res_tree = RB_ROOT;
>>>   +	ret = ttm_mem_global_alloc(&ttm_mem_glob, acc_size, &ctx);
>>> +	if (unlikely(ret))
>>> +		return ret;
>>> +
>>>   	ret = ttm_bo_init_reserved(bdev, &vmw_bo->base, size,
>>>   				   ttm_bo_type_device, placement,
>>> -				   0, &ctx, acc_size, NULL, NULL, bo_free);
>>> -	if (unlikely(ret))
>>> +				   0, &ctx, NULL, NULL, bo_free);
>>> +	if (unlikely(ret)) {
>>> +		ttm_mem_global_free(&ttm_mem_glob, acc_size);
>>>   		return ret;
>>> +	}
>>>     	if (pin)
>>>   		ttm_bo_pin(&vmw_bo->base);
>>> diff --git a/drivers/gpu/drm/vmwgfx/vmwgfx_drv.c b/drivers/gpu/drm/vmwgfx/vmwgfx_drv.c
>>> index 710ba5169a74..6c0ca1011629 100644
>>> --- a/drivers/gpu/drm/vmwgfx/vmwgfx_drv.c
>>> +++ b/drivers/gpu/drm/vmwgfx/vmwgfx_drv.c
>>> @@ -1268,6 +1268,7 @@ static void vmw_remove(struct pci_dev *pdev)
>>>   {
>>>   	struct drm_device *dev = pci_get_drvdata(pdev);
>>>   +	ttm_mem_global_release(&ttm_mem_glob);
>>>   	drm_dev_unregister(dev);
>>>   	vmw_driver_unload(dev);
>>>   }
>>> @@ -1518,6 +1519,10 @@ static int vmw_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
>>>     	pci_set_drvdata(pdev, &vmw->drm);
>>>   +	ret = ttm_mem_global_init(&ttm_mem_glob, &vmw->drm);
>>> +	if (ret)
>>> +		return ret;
>>> +
>>>   	ret = vmw_driver_load(vmw, ent->device);
>>>   	if (ret)
>>>   		return ret;
>>> diff --git a/drivers/gpu/drm/vmwgfx/vmwgfx_ttm_buffer.c b/drivers/gpu/drm/vmwgfx/vmwgfx_ttm_buffer.c
>>> index d1bfa59579f1..63f10c865061 100644
>>> --- a/drivers/gpu/drm/vmwgfx/vmwgfx_ttm_buffer.c
>>> +++ b/drivers/gpu/drm/vmwgfx/vmwgfx_ttm_buffer.c
>>> @@ -576,11 +576,31 @@ static void vmw_ttm_destroy(struct ttm_device *bdev, struct ttm_tt *ttm)
>>>   static int vmw_ttm_populate(struct ttm_device *bdev,
>>>   			    struct ttm_tt *ttm, struct ttm_operation_ctx *ctx)
>>>   {
>>> +	unsigned int i;
>>> +	int ret;
>>> +
>>>   	/* TODO: maybe completely drop this ? */
>>>   	if (ttm_tt_is_populated(ttm))
>>>   		return 0;
>>>   -	return ttm_pool_alloc(&bdev->pool, ttm, ctx);
>>> +	ret = ttm_pool_alloc(&bdev->pool, ttm, ctx);
>>> +	if (ret)
>>> +		return ret;
>>> +
>>> +	for (i = 0; i < ttm->num_pages; ++i) {
>>> +		ret = ttm_mem_global_alloc_page(&ttm_mem_glob, ttm->pages[i],
>>> +						PAGE_SIZE, ctx);
>>> +		if (ret)
>>> +			goto error;
>>> +	}
>>> +	return 0;
>>> +
>>> +error:
>>> +	while (i--)
>>> +		ttm_mem_global_free_page(&ttm_mem_glob, ttm->pages[i],
>>> +					 PAGE_SIZE);
>>> +	ttm_pool_free(&bdev->pool, ttm);
>>> +	return ret;
>>>   }
>>>     static void vmw_ttm_unpopulate(struct ttm_device *bdev,
>>> @@ -588,6 +608,7 @@ static void vmw_ttm_unpopulate(struct ttm_device *bdev,
>>>   {
>>>   	struct vmw_ttm_tt *vmw_tt = container_of(ttm, struct vmw_ttm_tt,
>>>   						 dma_ttm);
>>> +	unsigned int i;
>>>     	if (vmw_tt->mob) {
>>>   		vmw_mob_destroy(vmw_tt->mob);
>>> @@ -595,6 +616,11 @@ static void vmw_ttm_unpopulate(struct ttm_device *bdev,
>>>   	}
>>>     	vmw_ttm_unmap_dma(vmw_tt);
>>> +
>>> +	for (i = 0; i < ttm->num_pages; ++i)
>>> +		ttm_mem_global_free_page(&ttm_mem_glob, ttm->pages[i],
>>> +					 PAGE_SIZE);
>>> +
>>>   	ttm_pool_free(&bdev->pool, ttm);
>>>   }
>>>   diff --git a/include/drm/ttm/ttm_bo_api.h b/include/drm/ttm/ttm_bo_api.h
>>> index 1297a8fb7ccb..4fb523dfab32 100644
>>> --- a/include/drm/ttm/ttm_bo_api.h
>>> +++ b/include/drm/ttm/ttm_bo_api.h
>>> @@ -88,7 +88,6 @@ struct ttm_tt;
>>>    * @type: The bo type.
>>>    * @destroy: Destruction function. If NULL, kfree is used.
>>>    * @num_pages: Actual number of pages.
>>> - * @acc_size: Accounted size for this object.
>>>    * @kref: Reference count of this buffer object. When this refcount reaches
>>>    * zero, the object is destroyed or put on the delayed delete list.
>>>    * @mem: structure describing current placement.
>>> @@ -125,7 +124,6 @@ struct ttm_buffer_object {
>>>   	struct ttm_device *bdev;
>>>   	enum ttm_bo_type type;
>>>   	void (*destroy) (struct ttm_buffer_object *);
>>> -	size_t acc_size;
>>>     	/**
>>>   	* Members not needing protection.
>>> @@ -357,10 +355,6 @@ void ttm_bo_unlock_delayed_workqueue(struct ttm_device *bdev, int resched);
>>>   bool ttm_bo_eviction_valuable(struct ttm_buffer_object *bo,
>>>   			      const struct ttm_place *place);
>>>   -size_t ttm_bo_dma_acc_size(struct ttm_device *bdev,
>>> -			   unsigned long bo_size,
>>> -			   unsigned struct_size);
>>> -
>>>   /**
>>>    * ttm_bo_init_reserved
>>>    *
>>> @@ -371,7 +365,6 @@ size_t ttm_bo_dma_acc_size(struct ttm_device *bdev,
>>>    * @flags: Initial placement flags.
>>>    * @page_alignment: Data alignment in pages.
>>>    * @ctx: TTM operation context for memory allocation.
>>> - * @acc_size: Accounted size for this object.
>>>    * @resv: Pointer to a dma_resv, or NULL to let ttm allocate one.
>>>    * @destroy: Destroy function. Use NULL for kfree().
>>>    *
>>> @@ -402,8 +395,7 @@ int ttm_bo_init_reserved(struct ttm_device *bdev,
>>>   			 struct ttm_placement *placement,
>>>   			 uint32_t page_alignment,
>>>   			 struct ttm_operation_ctx *ctx,
>>> -			 size_t acc_size, struct sg_table *sg,
>>> -			 struct dma_resv *resv,
>>> +			 struct sg_table *sg, struct dma_resv *resv,
>>>   			 void (*destroy) (struct ttm_buffer_object *));
>>>     /**
>>> @@ -421,7 +413,6 @@ int ttm_bo_init_reserved(struct ttm_device *bdev,
>>>    * holds a pointer to a persistent shmem object. Typically, this would
>>>    * point to the shmem object backing a GEM object if TTM is used to back a
>>>    * GEM user interface.
>>> - * @acc_size: Accounted size for this object.
>>>    * @resv: Pointer to a dma_resv, or NULL to let ttm allocate one.
>>>    * @destroy: Destroy function. Use NULL for kfree().
>>>    *
>>> @@ -446,7 +437,7 @@ int ttm_bo_init_reserved(struct ttm_device *bdev,
>>>   int ttm_bo_init(struct ttm_device *bdev, struct ttm_buffer_object *bo,
>>>   		size_t size, enum ttm_bo_type type,
>>>   		struct ttm_placement *placement,
>>> -		uint32_t page_alignment, bool interrubtible, size_t acc_size,
>>> +		uint32_t page_alignment, bool interrubtible,
>>>   		struct sg_table *sg, struct dma_resv *resv,
>>>   		void (*destroy) (struct ttm_buffer_object *));
>>>   diff --git a/include/drm/ttm/ttm_bo_driver.h b/include/drm/ttm/ttm_bo_driver.h
>>> index 1c9bf993e252..8959c0075cfd 100644
>>> --- a/include/drm/ttm/ttm_bo_driver.h
>>> +++ b/include/drm/ttm/ttm_bo_driver.h
>>> @@ -40,7 +40,6 @@
>>>   #include <drm/ttm/ttm_device.h>
>>>     #include "ttm_bo_api.h"
>>> -#include "ttm_memory.h"
>>>   #include "ttm_placement.h"
>>>   #include "ttm_tt.h"
>>>   #include "ttm_pool.h"
>>> diff --git a/include/drm/ttm/ttm_tt.h b/include/drm/ttm/ttm_tt.h
>>> index cce57fb49e2c..069f8130241a 100644
>>> --- a/include/drm/ttm/ttm_tt.h
>>> +++ b/include/drm/ttm/ttm_tt.h
>>> @@ -30,6 +30,7 @@
>>>   #include <linux/types.h>
>>>   #include <drm/ttm/ttm_caching.h>
>>>   +struct ttm_bo_device;
>>>   struct ttm_tt;
>>>   struct ttm_resource;
>>>   struct ttm_buffer_object;

_______________________________________________
dri-devel mailing list
dri-devel@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/dri-devel

^ permalink raw reply	[flat|nested] 17+ messages in thread

* Re: [PATCH 2/3] drm/ttm: move memory accounting into vmwgfx v3
  2021-02-02 15:16       ` Christian König
@ 2021-02-02 17:42         ` Zack Rusin
  2021-02-03  2:45           ` [Linux-graphics-maintainer] " Zack Rusin
  0 siblings, 1 reply; 17+ messages in thread
From: Zack Rusin @ 2021-02-02 17:42 UTC (permalink / raw)
  To: Christian König
  Cc: Linux-graphics-maintainer, Roland Scheidegger, dri-devel

Ah, yes, sorry, I missed that. I just double checked and it fails with:

kobject_add_internal failed for memory_accounting (error: -2 parent: card0)

which breaks the probe and the driver won’t load. I won’t have time to look into it until tomorrow though.

z

> On Feb 2, 2021, at 10:16, Christian König <christian.koenig@amd.com> wrote:
> 
> Hi Zack,
> 
> can you also give it a quick smoke test?
> 
> I'm not sure if I wired up all the sysfs magic correctly inside vmwgfx, but I currently don't have a setup where I can test this.
> 
> Thanks,
> Christian.
> 
> Am 02.02.21 um 16:14 schrieb Zack Rusin:
>> Looks good. There’s probably not much reason to call it ttm_memory anymore as it only deals with ttm_mem_glob, we’ll likely fold it in after you submit. Thanks.
>> 
>> Reviewed-by: Zack Rusin <zackr@vmware.com>
>> 
>> z
>> 
>>> On Feb 2, 2021, at 08:04, Christian König <christian.koenig@amd.com> wrote:
>>> 
>>> Ping?
>>> 
>>> Especially Roland and Zack do you have any objections to this?
>>> 
>>> Regards,
>>> Christian.
>>> 
>>> Am 28.01.21 um 14:16 schrieb Christian König:
>>>> This is just another feature which is only used by VMWGFX, so move
>>>> it into the driver instead.
>>>> 
>>>> I've tried to add the accounting sysfs file to the kobject of the drm
>>>> minor, but I'm not 100% sure if this works as expected.
>>>> 
>>>> v2: fix typo in KFD and avoid 64bit divide
>>>> v3: fix init order in VMWGFX
>>>> 
>>>> Signed-off-by: Christian König <christian.koenig@amd.com>
>>>> ---
>>>>  .../gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c  | 16 ++++++---
>>>>  drivers/gpu/drm/amd/amdgpu/amdgpu_object.c    |  8 ++---
>>>>  drivers/gpu/drm/drm_gem_vram_helper.c         |  6 ++--
>>>>  drivers/gpu/drm/nouveau/nouveau_bo.c          |  7 ++--
>>>>  drivers/gpu/drm/nouveau/nouveau_drv.h         |  1 -
>>>>  drivers/gpu/drm/qxl/qxl_object.c              |  4 +--
>>>>  drivers/gpu/drm/radeon/radeon_object.c        |  8 ++---
>>>>  drivers/gpu/drm/ttm/Makefile                  |  7 ++--
>>>>  drivers/gpu/drm/ttm/ttm_bo.c                  | 33 +------------------
>>>>  drivers/gpu/drm/ttm/ttm_bo_util.c             |  1 -
>>>>  drivers/gpu/drm/ttm/ttm_device.c              | 22 ++++++++++---
>>>>  drivers/gpu/drm/ttm/ttm_pool.c                | 13 +-------
>>>>  drivers/gpu/drm/vmwgfx/Makefile               |  2 +-
>>>>  drivers/gpu/drm/{ttm => vmwgfx}/ttm_memory.c  | 19 ++++-------
>>>>  .../gpu/drm/vmwgfx}/ttm_memory.h              |  5 +--
>>>>  drivers/gpu/drm/vmwgfx/ttm_object.h           |  3 +-
>>>>  drivers/gpu/drm/vmwgfx/vmwgfx_bo.c            | 22 ++++++++++---
>>>>  drivers/gpu/drm/vmwgfx/vmwgfx_drv.c           |  5 +++
>>>>  drivers/gpu/drm/vmwgfx/vmwgfx_ttm_buffer.c    | 28 +++++++++++++++-
>>>>  include/drm/ttm/ttm_bo_api.h                  | 13 ++------
>>>>  include/drm/ttm/ttm_bo_driver.h               |  1 -
>>>>  include/drm/ttm/ttm_tt.h                      |  1 +
>>>>  22 files changed, 110 insertions(+), 115 deletions(-)
>>>>  rename drivers/gpu/drm/{ttm => vmwgfx}/ttm_memory.c (97%)
>>>>  rename {include/drm/ttm => drivers/gpu/drm/vmwgfx}/ttm_memory.h (97%)
>>>> 
>>>> diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c b/drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c
>>>> index 0849b68e784f..e440af37dde8 100644
>>>> --- a/drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c
>>>> +++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c
>>>> @@ -118,6 +118,16 @@ void amdgpu_amdkfd_gpuvm_init_mem_limits(void)
>>>>   */
>>>>  #define ESTIMATE_PT_SIZE(mem_size) ((mem_size) >> 14)
>>>>  +static size_t amdgpu_amdkfd_acc_size(uint64_t size)
>>>> +{
>>>> +	size >>= PAGE_SHIFT;
>>>> +	size *= sizeof(dma_addr_t) + sizeof(void *);
>>>> +
>>>> +	return __roundup_pow_of_two(sizeof(struct amdgpu_bo)) +
>>>> +		__roundup_pow_of_two(sizeof(struct ttm_tt)) +
>>>> +		PAGE_ALIGN(size);
>>>> +}
>>>> +
>>>>  static int amdgpu_amdkfd_reserve_mem_limit(struct amdgpu_device *adev,
>>>>  		uint64_t size, u32 domain, bool sg)
>>>>  {
>>>> @@ -126,8 +136,7 @@ static int amdgpu_amdkfd_reserve_mem_limit(struct amdgpu_device *adev,
>>>>  	size_t acc_size, system_mem_needed, ttm_mem_needed, vram_needed;
>>>>  	int ret = 0;
>>>>  -	acc_size = ttm_bo_dma_acc_size(&adev->mman.bdev, size,
>>>> -				       sizeof(struct amdgpu_bo));
>>>> +	acc_size = amdgpu_amdkfd_acc_size(size);
>>>>    	vram_needed = 0;
>>>>  	if (domain == AMDGPU_GEM_DOMAIN_GTT) {
>>>> @@ -174,8 +183,7 @@ static void unreserve_mem_limit(struct amdgpu_device *adev,
>>>>  {
>>>>  	size_t acc_size;
>>>>  -	acc_size = ttm_bo_dma_acc_size(&adev->mman.bdev, size,
>>>> -				       sizeof(struct amdgpu_bo));
>>>> +	acc_size = amdgpu_amdkfd_acc_size(size);
>>>>    	spin_lock(&kfd_mem_limit.mem_limit_lock);
>>>>  	if (domain == AMDGPU_GEM_DOMAIN_GTT) {
>>>> diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_object.c b/drivers/gpu/drm/amd/amdgpu/amdgpu_object.c
>>>> index 6cc9919b12cc..599c9a132eb6 100644
>>>> --- a/drivers/gpu/drm/amd/amdgpu/amdgpu_object.c
>>>> +++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_object.c
>>>> @@ -523,7 +523,6 @@ static int amdgpu_bo_do_create(struct amdgpu_device *adev,
>>>>  	};
>>>>  	struct amdgpu_bo *bo;
>>>>  	unsigned long page_align, size = bp->size;
>>>> -	size_t acc_size;
>>>>  	int r;
>>>>    	/* Note that GDS/GWS/OA allocates 1 page per byte/resource. */
>>>> @@ -546,9 +545,6 @@ static int amdgpu_bo_do_create(struct amdgpu_device *adev,
>>>>    	*bo_ptr = NULL;
>>>>  -	acc_size = ttm_bo_dma_acc_size(&adev->mman.bdev, size,
>>>> -				       sizeof(struct amdgpu_bo));
>>>> -
>>>>  	bo = kzalloc(sizeof(struct amdgpu_bo), GFP_KERNEL);
>>>>  	if (bo == NULL)
>>>>  		return -ENOMEM;
>>>> @@ -577,8 +573,8 @@ static int amdgpu_bo_do_create(struct amdgpu_device *adev,
>>>>  		bo->tbo.priority = 1;
>>>>    	r = ttm_bo_init_reserved(&adev->mman.bdev, &bo->tbo, size, bp->type,
>>>> -				 &bo->placement, page_align, &ctx, acc_size,
>>>> -				 NULL, bp->resv, &amdgpu_bo_destroy);
>>>> +				 &bo->placement, page_align, &ctx,  NULL,
>>>> +				 bp->resv, &amdgpu_bo_destroy);
>>>>  	if (unlikely(r != 0))
>>>>  		return r;
>>>>  diff --git a/drivers/gpu/drm/drm_gem_vram_helper.c b/drivers/gpu/drm/drm_gem_vram_helper.c
>>>> index 0b13c8507688..a0992f0b8afd 100644
>>>> --- a/drivers/gpu/drm/drm_gem_vram_helper.c
>>>> +++ b/drivers/gpu/drm/drm_gem_vram_helper.c
>>>> @@ -189,7 +189,6 @@ struct drm_gem_vram_object *drm_gem_vram_create(struct drm_device *dev,
>>>>  	struct drm_vram_mm *vmm = dev->vram_mm;
>>>>  	struct ttm_device *bdev;
>>>>  	int ret;
>>>> -	size_t acc_size;
>>>>    	if (WARN_ONCE(!vmm, "VRAM MM not initialized"))
>>>>  		return ERR_PTR(-EINVAL);
>>>> @@ -216,7 +215,6 @@ struct drm_gem_vram_object *drm_gem_vram_create(struct drm_device *dev,
>>>>  	}
>>>>    	bdev = &vmm->bdev;
>>>> -	acc_size = ttm_bo_dma_acc_size(bdev, size, sizeof(*gbo));
>>>>    	gbo->bo.bdev = bdev;
>>>>  	drm_gem_vram_placement(gbo, DRM_GEM_VRAM_PL_FLAG_SYSTEM);
>>>> @@ -226,8 +224,8 @@ struct drm_gem_vram_object *drm_gem_vram_create(struct drm_device *dev,
>>>>  	 * to release gbo->bo.base and kfree gbo.
>>>>  	 */
>>>>  	ret = ttm_bo_init(bdev, &gbo->bo, size, ttm_bo_type_device,
>>>> -			  &gbo->placement, pg_align, false, acc_size,
>>>> -			  NULL, NULL, ttm_buffer_object_destroy);
>>>> +			  &gbo->placement, pg_align, false, NULL, NULL,
>>>> +			  ttm_buffer_object_destroy);
>>>>  	if (ret)
>>>>  		return ERR_PTR(ret);
>>>>  diff --git a/drivers/gpu/drm/nouveau/nouveau_bo.c b/drivers/gpu/drm/nouveau/nouveau_bo.c
>>>> index c177940d6e2c..ca2a8ae1938e 100644
>>>> --- a/drivers/gpu/drm/nouveau/nouveau_bo.c
>>>> +++ b/drivers/gpu/drm/nouveau/nouveau_bo.c
>>>> @@ -300,18 +300,15 @@ nouveau_bo_init(struct nouveau_bo *nvbo, u64 size, int align, u32 domain,
>>>>  		struct sg_table *sg, struct dma_resv *robj)
>>>>  {
>>>>  	int type = sg ? ttm_bo_type_sg : ttm_bo_type_device;
>>>> -	size_t acc_size;
>>>>  	int ret;
>>>>  -	acc_size = ttm_bo_dma_acc_size(nvbo->bo.bdev, size, sizeof(*nvbo));
>>>> -
>>>>  	nvbo->bo.mem.num_pages = size >> PAGE_SHIFT;
>>>>  	nouveau_bo_placement_set(nvbo, domain, 0);
>>>>  	INIT_LIST_HEAD(&nvbo->io_reserve_lru);
>>>>    	ret = ttm_bo_init(nvbo->bo.bdev, &nvbo->bo, size, type,
>>>> -			  &nvbo->placement, align >> PAGE_SHIFT, false,
>>>> -			  acc_size, sg, robj, nouveau_bo_del_ttm);
>>>> +			  &nvbo->placement, align >> PAGE_SHIFT, false, sg,
>>>> +			  robj, nouveau_bo_del_ttm);
>>>>  	if (ret) {
>>>>  		/* ttm will call nouveau_bo_del_ttm if it fails.. */
>>>>  		return ret;
>>>> diff --git a/drivers/gpu/drm/nouveau/nouveau_drv.h b/drivers/gpu/drm/nouveau/nouveau_drv.h
>>>> index edf9d1ee9d58..a491c2c1c56e 100644
>>>> --- a/drivers/gpu/drm/nouveau/nouveau_drv.h
>>>> +++ b/drivers/gpu/drm/nouveau/nouveau_drv.h
>>>> @@ -54,7 +54,6 @@
>>>>  #include <drm/ttm/ttm_bo_api.h>
>>>>  #include <drm/ttm/ttm_bo_driver.h>
>>>>  #include <drm/ttm/ttm_placement.h>
>>>> -#include <drm/ttm/ttm_memory.h>
>>>>    #include <drm/drm_audio_component.h>
>>>>  diff --git a/drivers/gpu/drm/qxl/qxl_object.c b/drivers/gpu/drm/qxl/qxl_object.c
>>>> index ceebc5881f68..705b51535492 100644
>>>> --- a/drivers/gpu/drm/qxl/qxl_object.c
>>>> +++ b/drivers/gpu/drm/qxl/qxl_object.c
>>>> @@ -138,8 +138,8 @@ int qxl_bo_create(struct qxl_device *qdev,
>>>>  	qxl_ttm_placement_from_domain(bo, domain);
>>>>    	r = ttm_bo_init_reserved(&qdev->mman.bdev, &bo->tbo, size, type,
>>>> -				 &bo->placement, 0, &ctx, size,
>>>> -				 NULL, NULL, &qxl_ttm_bo_destroy);
>>>> +				 &bo->placement, 0, &ctx, NULL, NULL,
>>>> +				 &qxl_ttm_bo_destroy);
>>>>  	if (unlikely(r != 0)) {
>>>>  		if (r != -ERESTARTSYS)
>>>>  			dev_err(qdev->ddev.dev,
>>>> diff --git a/drivers/gpu/drm/radeon/radeon_object.c b/drivers/gpu/drm/radeon/radeon_object.c
>>>> index 6a336284466f..804f7a427be7 100644
>>>> --- a/drivers/gpu/drm/radeon/radeon_object.c
>>>> +++ b/drivers/gpu/drm/radeon/radeon_object.c
>>>> @@ -159,7 +159,6 @@ int radeon_bo_create(struct radeon_device *rdev,
>>>>  	struct radeon_bo *bo;
>>>>  	enum ttm_bo_type type;
>>>>  	unsigned long page_align = roundup(byte_align, PAGE_SIZE) >> PAGE_SHIFT;
>>>> -	size_t acc_size;
>>>>  	int r;
>>>>    	size = ALIGN(size, PAGE_SIZE);
>>>> @@ -173,9 +172,6 @@ int radeon_bo_create(struct radeon_device *rdev,
>>>>  	}
>>>>  	*bo_ptr = NULL;
>>>>  -	acc_size = ttm_bo_dma_acc_size(&rdev->mman.bdev, size,
>>>> -				       sizeof(struct radeon_bo));
>>>> -
>>>>  	bo = kzalloc(sizeof(struct radeon_bo), GFP_KERNEL);
>>>>  	if (bo == NULL)
>>>>  		return -ENOMEM;
>>>> @@ -230,8 +226,8 @@ int radeon_bo_create(struct radeon_device *rdev,
>>>>  	/* Kernel allocation are uninterruptible */
>>>>  	down_read(&rdev->pm.mclk_lock);
>>>>  	r = ttm_bo_init(&rdev->mman.bdev, &bo->tbo, size, type,
>>>> -			&bo->placement, page_align, !kernel, acc_size,
>>>> -			sg, resv, &radeon_ttm_bo_destroy);
>>>> +			&bo->placement, page_align, !kernel, sg, resv,
>>>> +			&radeon_ttm_bo_destroy);
>>>>  	up_read(&rdev->pm.mclk_lock);
>>>>  	if (unlikely(r != 0)) {
>>>>  		return r;
>>>> diff --git a/drivers/gpu/drm/ttm/Makefile b/drivers/gpu/drm/ttm/Makefile
>>>> index 8e6437eadabe..40e5e9da7953 100644
>>>> --- a/drivers/gpu/drm/ttm/Makefile
>>>> +++ b/drivers/gpu/drm/ttm/Makefile
>>>> @@ -2,10 +2,9 @@
>>>>  #
>>>>  # Makefile for the drm device driver.  This driver provides support for the
>>>>  -ttm-y := ttm_memory.o ttm_tt.o ttm_bo.o \
>>>> -	ttm_bo_util.o ttm_bo_vm.o ttm_module.o \
>>>> -	ttm_execbuf_util.o ttm_range_manager.o \
>>>> -	ttm_resource.o ttm_pool.o ttm_device.o
>>>> +ttm-y := ttm_tt.o ttm_bo.o ttm_bo_util.o ttm_bo_vm.o ttm_module.o \
>>>> +	ttm_execbuf_util.o ttm_range_manager.o ttm_resource.o ttm_pool.o \
>>>> +	ttm_device.o
>>>>  ttm-$(CONFIG_AGP) += ttm_agp_backend.o
>>>>    obj-$(CONFIG_DRM_TTM) += ttm.o
>>>> diff --git a/drivers/gpu/drm/ttm/ttm_bo.c b/drivers/gpu/drm/ttm/ttm_bo.c
>>>> index 643befc1a6f2..e38102282fd5 100644
>>>> --- a/drivers/gpu/drm/ttm/ttm_bo.c
>>>> +++ b/drivers/gpu/drm/ttm/ttm_bo.c
>>>> @@ -425,7 +425,6 @@ static void ttm_bo_release(struct kref *kref)
>>>>  	struct ttm_buffer_object *bo =
>>>>  	    container_of(kref, struct ttm_buffer_object, kref);
>>>>  	struct ttm_device *bdev = bo->bdev;
>>>> -	size_t acc_size = bo->acc_size;
>>>>  	int ret;
>>>>    	if (!bo->deleted) {
>>>> @@ -485,7 +484,6 @@ static void ttm_bo_release(struct kref *kref)
>>>>  	if (!ttm_bo_uses_embedded_gem_object(bo))
>>>>  		dma_resv_fini(&bo->base._resv);
>>>>  	bo->destroy(bo);
>>>> -	ttm_mem_global_free(&ttm_mem_glob, acc_size);
>>>>  }
>>>>    void ttm_bo_put(struct ttm_buffer_object *bo)
>>>> @@ -1046,25 +1044,13 @@ int ttm_bo_init_reserved(struct ttm_device *bdev,
>>>>  			 struct ttm_placement *placement,
>>>>  			 uint32_t page_alignment,
>>>>  			 struct ttm_operation_ctx *ctx,
>>>> -			 size_t acc_size,
>>>>  			 struct sg_table *sg,
>>>>  			 struct dma_resv *resv,
>>>>  			 void (*destroy) (struct ttm_buffer_object *))
>>>>  {
>>>> -	struct ttm_mem_global *mem_glob = &ttm_mem_glob;
>>>>  	bool locked;
>>>>  	int ret = 0;
>>>>  -	ret = ttm_mem_global_alloc(mem_glob, acc_size, ctx);
>>>> -	if (ret) {
>>>> -		pr_err("Out of kernel memory\n");
>>>> -		if (destroy)
>>>> -			(*destroy)(bo);
>>>> -		else
>>>> -			kfree(bo);
>>>> -		return -ENOMEM;
>>>> -	}
>>>> -
>>>>  	bo->destroy = destroy ? destroy : ttm_bo_default_destroy;
>>>>    	kref_init(&bo->kref);
>>>> @@ -1081,7 +1067,6 @@ int ttm_bo_init_reserved(struct ttm_device *bdev,
>>>>  	bo->mem.bus.addr = NULL;
>>>>  	bo->moving = NULL;
>>>>  	bo->mem.placement = 0;
>>>> -	bo->acc_size = acc_size;
>>>>  	bo->pin_count = 0;
>>>>  	bo->sg = sg;
>>>>  	if (resv) {
>>>> @@ -1142,7 +1127,6 @@ int ttm_bo_init(struct ttm_device *bdev,
>>>>  		struct ttm_placement *placement,
>>>>  		uint32_t page_alignment,
>>>>  		bool interruptible,
>>>> -		size_t acc_size,
>>>>  		struct sg_table *sg,
>>>>  		struct dma_resv *resv,
>>>>  		void (*destroy) (struct ttm_buffer_object *))
>>>> @@ -1151,8 +1135,7 @@ int ttm_bo_init(struct ttm_device *bdev,
>>>>  	int ret;
>>>>    	ret = ttm_bo_init_reserved(bdev, bo, size, type, placement,
>>>> -				   page_alignment, &ctx, acc_size,
>>>> -				   sg, resv, destroy);
>>>> +				   page_alignment, &ctx, sg, resv, destroy);
>>>>  	if (ret)
>>>>  		return ret;
>>>>  @@ -1163,20 +1146,6 @@ int ttm_bo_init(struct ttm_device *bdev,
>>>>  }
>>>>  EXPORT_SYMBOL(ttm_bo_init);
>>>>  -size_t ttm_bo_dma_acc_size(struct ttm_device *bdev,
>>>> -			   unsigned long bo_size,
>>>> -			   unsigned struct_size)
>>>> -{
>>>> -	unsigned npages = (PAGE_ALIGN(bo_size)) >> PAGE_SHIFT;
>>>> -	size_t size = 0;
>>>> -
>>>> -	size += ttm_round_pot(struct_size);
>>>> -	size += ttm_round_pot(npages * (2*sizeof(void *) + sizeof(dma_addr_t)));
>>>> -	size += ttm_round_pot(sizeof(struct ttm_tt));
>>>> -	return size;
>>>> -}
>>>> -EXPORT_SYMBOL(ttm_bo_dma_acc_size);
>>>> -
>>>>  /*
>>>>   * buffer object vm functions.
>>>>   */
>>>> diff --git a/drivers/gpu/drm/ttm/ttm_bo_util.c b/drivers/gpu/drm/ttm/ttm_bo_util.c
>>>> index db0f2661d504..031e5819fec4 100644
>>>> --- a/drivers/gpu/drm/ttm/ttm_bo_util.c
>>>> +++ b/drivers/gpu/drm/ttm/ttm_bo_util.c
>>>> @@ -309,7 +309,6 @@ static int ttm_buffer_object_transfer(struct ttm_buffer_object *bo,
>>>>    	kref_init(&fbo->base.kref);
>>>>  	fbo->base.destroy = &ttm_transfered_destroy;
>>>> -	fbo->base.acc_size = 0;
>>>>  	fbo->base.pin_count = 0;
>>>>  	if (bo->type != ttm_bo_type_sg)
>>>>  		fbo->base.base.resv = &fbo->base.base._resv;
>>>> diff --git a/drivers/gpu/drm/ttm/ttm_device.c b/drivers/gpu/drm/ttm/ttm_device.c
>>>> index ac0903c9e60a..6bde344e5da7 100644
>>>> --- a/drivers/gpu/drm/ttm/ttm_device.c
>>>> +++ b/drivers/gpu/drm/ttm/ttm_device.c
>>>> @@ -27,9 +27,12 @@
>>>>    #define pr_fmt(fmt) "[TTM DEVICE] " fmt
>>>>  +#include <linux/mm.h>
>>>> +
>>>>  #include <drm/ttm/ttm_device.h>
>>>> -#include <drm/ttm/ttm_memory.h>
>>>> +#include <drm/ttm/ttm_tt.h>
>>>>  #include <drm/ttm/ttm_placement.h>
>>>> +#include <drm/ttm/ttm_bo_api.h>
>>>>    #include "ttm_module.h"
>>>>  @@ -49,9 +52,11 @@ static void ttm_global_release(void)
>>>>  	if (--ttm_glob_use_count > 0)
>>>>  		goto out;
>>>>  +	ttm_pool_mgr_fini();
>>>> +	ttm_tt_mgr_fini();
>>>> +
>>>>  	kobject_del(&glob->kobj);
>>>>  	kobject_put(&glob->kobj);
>>>> -	ttm_mem_global_release(&ttm_mem_glob);
>>>>  	__free_page(glob->dummy_read_page);
>>>>  	memset(glob, 0, sizeof(*glob));
>>>>  out:
>>>> @@ -61,6 +66,8 @@ static void ttm_global_release(void)
>>>>  static int ttm_global_init(void)
>>>>  {
>>>>  	struct ttm_global *glob = &ttm_glob;
>>>> +	unsigned long num_pages;
>>>> +	struct sysinfo si;
>>>>  	int ret = 0;
>>>>  	unsigned i;
>>>>  @@ -68,9 +75,14 @@ static int ttm_global_init(void)
>>>>  	if (++ttm_glob_use_count > 1)
>>>>  		goto out;
>>>>  -	ret = ttm_mem_global_init(&ttm_mem_glob);
>>>> -	if (ret)
>>>> -		goto out;
>>>> +	si_meminfo(&si);
>>>> +
>>>> +	/* Limit the number of pages in the pool to about 50% of the total
>>>> +	 * system memory.
>>>> +	 */
>>>> +	num_pages = ((u64)si.totalram * si.mem_unit) >> PAGE_SHIFT;
>>>> +	ttm_pool_mgr_init(num_pages * 50 / 100);
>>>> +	ttm_tt_mgr_init();
>>>>    	spin_lock_init(&glob->lru_lock);
>>>>  	glob->dummy_read_page = alloc_page(__GFP_ZERO | GFP_DMA32);
>>>> diff --git a/drivers/gpu/drm/ttm/ttm_pool.c b/drivers/gpu/drm/ttm/ttm_pool.c
>>>> index e0617717113f..6b0f957d63d5 100644
>>>> --- a/drivers/gpu/drm/ttm/ttm_pool.c
>>>> +++ b/drivers/gpu/drm/ttm/ttm_pool.c
>>>> @@ -404,16 +404,10 @@ int ttm_pool_alloc(struct ttm_pool *pool, struct ttm_tt *tt,
>>>>  			caching = pages + (1 << order);
>>>>  		}
>>>>  -		r = ttm_mem_global_alloc_page(&ttm_mem_glob, p,
>>>> -					      (1 << order) * PAGE_SIZE,
>>>> -					      ctx);
>>>> -		if (r)
>>>> -			goto error_free_page;
>>>> -
>>>>  		if (dma_addr) {
>>>>  			r = ttm_pool_map(pool, order, p, &dma_addr);
>>>>  			if (r)
>>>> -				goto error_global_free;
>>>> +				goto error_free_page;
>>>>  		}
>>>>    		num_pages -= 1 << order;
>>>> @@ -427,9 +421,6 @@ int ttm_pool_alloc(struct ttm_pool *pool, struct ttm_tt *tt,
>>>>    	return 0;
>>>>  -error_global_free:
>>>> -	ttm_mem_global_free_page(&ttm_mem_glob, p, (1 << order) * PAGE_SIZE);
>>>> -
>>>>  error_free_page:
>>>>  	ttm_pool_free_page(pool, tt->caching, order, p);
>>>>  @@ -464,8 +455,6 @@ void ttm_pool_free(struct ttm_pool *pool, struct ttm_tt *tt)
>>>>    		order = ttm_pool_page_order(pool, p);
>>>>  		num_pages = 1ULL << order;
>>>> -		ttm_mem_global_free_page(&ttm_mem_glob, p,
>>>> -					 num_pages * PAGE_SIZE);
>>>>  		if (tt->dma_address)
>>>>  			ttm_pool_unmap(pool, tt->dma_address[i], num_pages);
>>>>  diff --git a/drivers/gpu/drm/vmwgfx/Makefile b/drivers/gpu/drm/vmwgfx/Makefile
>>>> index cc4cdca7176e..8c02fa5852e7 100644
>>>> --- a/drivers/gpu/drm/vmwgfx/Makefile
>>>> +++ b/drivers/gpu/drm/vmwgfx/Makefile
>>>> @@ -9,7 +9,7 @@ vmwgfx-y := vmwgfx_execbuf.o vmwgfx_gmr.o vmwgfx_kms.o vmwgfx_drv.o \
>>>>  	    vmwgfx_cotable.o vmwgfx_so.o vmwgfx_binding.o vmwgfx_msg.o \
>>>>  	    vmwgfx_simple_resource.o vmwgfx_va.o vmwgfx_blit.o \
>>>>  	    vmwgfx_validation.o vmwgfx_page_dirty.o vmwgfx_streamoutput.o \
>>>> -	    ttm_object.o ttm_lock.o
>>>> +	    ttm_object.o ttm_lock.o ttm_memory.o
>>>>    vmwgfx-$(CONFIG_TRANSPARENT_HUGEPAGE) += vmwgfx_thp.o
>>>>  obj-$(CONFIG_DRM_VMWGFX) := vmwgfx.o
>>>> diff --git a/drivers/gpu/drm/ttm/ttm_memory.c b/drivers/gpu/drm/vmwgfx/ttm_memory.c
>>>> similarity index 97%
>>>> rename from drivers/gpu/drm/ttm/ttm_memory.c
>>>> rename to drivers/gpu/drm/vmwgfx/ttm_memory.c
>>>> index 634a85c2dc4c..1306d9e0f095 100644
>>>> --- a/drivers/gpu/drm/ttm/ttm_memory.c
>>>> +++ b/drivers/gpu/drm/vmwgfx/ttm_memory.c
>>>> @@ -28,7 +28,6 @@
>>>>    #define pr_fmt(fmt) "[TTM] " fmt
>>>>  -#include <drm/ttm/ttm_memory.h>
>>>>  #include <linux/spinlock.h>
>>>>  #include <linux/sched.h>
>>>>  #include <linux/wait.h>
>>>> @@ -36,10 +35,11 @@
>>>>  #include <linux/module.h>
>>>>  #include <linux/slab.h>
>>>>  #include <linux/swap.h>
>>>> -#include <drm/ttm/ttm_pool.h>
>>>> -#include <drm/ttm/ttm_tt.h>
>>>>  -#include "ttm_module.h"
>>>> +#include <drm/drm_device.h>
>>>> +#include <drm/drm_file.h>
>>>> +
>>>> +#include "ttm_memory.h"
>>>>    #define TTM_MEMORY_ALLOC_RETRIES 4
>>>>  @@ -414,7 +414,7 @@ static int ttm_mem_init_dma32_zone(struct ttm_mem_global *glob,
>>>>  }
>>>>  #endif
>>>>  -int ttm_mem_global_init(struct ttm_mem_global *glob)
>>>> +int ttm_mem_global_init(struct ttm_mem_global *glob, struct drm_device *dev)
>>>>  {
>>>>  	struct sysinfo si;
>>>>  	int ret;
>>>> @@ -425,7 +425,8 @@ int ttm_mem_global_init(struct ttm_mem_global *glob)
>>>>  	glob->swap_queue = create_singlethread_workqueue("ttm_swap");
>>>>  	INIT_WORK(&glob->work, ttm_shrink_work);
>>>>  	ret = kobject_init_and_add(
>>>> -		&glob->kobj, &ttm_mem_glob_kobj_type, ttm_get_kobj(), "memory_accounting");
>>>> +		&glob->kobj, &ttm_mem_glob_kobj_type, &dev->primary->kdev->kobj,
>>>> +		"memory_accounting");
>>>>  	if (unlikely(ret != 0)) {
>>>>  		kobject_put(&glob->kobj);
>>>>  		return ret;
>>>> @@ -453,8 +454,6 @@ int ttm_mem_global_init(struct ttm_mem_global *glob)
>>>>  		pr_info("Zone %7s: Available graphics memory: %llu KiB\n",
>>>>  			zone->name, (unsigned long long)zone->max_mem >> 10);
>>>>  	}
>>>> -	ttm_pool_mgr_init(glob->zone_kernel->max_mem/(2*PAGE_SIZE));
>>>> -	ttm_tt_mgr_init();
>>>>  	return 0;
>>>>  out_no_zone:
>>>>  	ttm_mem_global_release(glob);
>>>> @@ -466,10 +465,6 @@ void ttm_mem_global_release(struct ttm_mem_global *glob)
>>>>  	struct ttm_mem_zone *zone;
>>>>  	unsigned int i;
>>>>  -	/* let the page allocator first stop the shrink work. */
>>>> -	ttm_pool_mgr_fini();
>>>> -	ttm_tt_mgr_fini();
>>>> -
>>>>  	flush_workqueue(glob->swap_queue);
>>>>  	destroy_workqueue(glob->swap_queue);
>>>>  	glob->swap_queue = NULL;
>>>> diff --git a/include/drm/ttm/ttm_memory.h b/drivers/gpu/drm/vmwgfx/ttm_memory.h
>>>> similarity index 97%
>>>> rename from include/drm/ttm/ttm_memory.h
>>>> rename to drivers/gpu/drm/vmwgfx/ttm_memory.h
>>>> index c1f167881e33..850ee6c867da 100644
>>>> --- a/include/drm/ttm/ttm_memory.h
>>>> +++ b/drivers/gpu/drm/vmwgfx/ttm_memory.h
>>>> @@ -35,7 +35,8 @@
>>>>  #include <linux/errno.h>
>>>>  #include <linux/kobject.h>
>>>>  #include <linux/mm.h>
>>>> -#include "ttm_bo_api.h"
>>>> +
>>>> +#include <drm/ttm/ttm_bo_api.h>
>>>>    /**
>>>>   * struct ttm_mem_global - Global memory accounting structure.
>>>> @@ -79,7 +80,7 @@ extern struct ttm_mem_global {
>>>>  #endif
>>>>  } ttm_mem_glob;
>>>>  -int ttm_mem_global_init(struct ttm_mem_global *glob);
>>>> +int ttm_mem_global_init(struct ttm_mem_global *glob, struct drm_device *dev);
>>>>  void ttm_mem_global_release(struct ttm_mem_global *glob);
>>>>  int ttm_mem_global_alloc(struct ttm_mem_global *glob, uint64_t memory,
>>>>  			 struct ttm_operation_ctx *ctx);
>>>> diff --git a/drivers/gpu/drm/vmwgfx/ttm_object.h b/drivers/gpu/drm/vmwgfx/ttm_object.h
>>>> index ede26df87c93..49b064f0cb19 100644
>>>> --- a/drivers/gpu/drm/vmwgfx/ttm_object.h
>>>> +++ b/drivers/gpu/drm/vmwgfx/ttm_object.h
>>>> @@ -43,7 +43,8 @@
>>>>  #include <linux/rcupdate.h>
>>>>    #include <drm/drm_hashtab.h>
>>>> -#include <drm/ttm/ttm_memory.h>
>>>> +
>>>> +#include "ttm_memory.h"
>>>>    /**
>>>>   * enum ttm_ref_type
>>>> diff --git a/drivers/gpu/drm/vmwgfx/vmwgfx_bo.c b/drivers/gpu/drm/vmwgfx/vmwgfx_bo.c
>>>> index 6b3bfd8c678a..50e529a01677 100644
>>>> --- a/drivers/gpu/drm/vmwgfx/vmwgfx_bo.c
>>>> +++ b/drivers/gpu/drm/vmwgfx/vmwgfx_bo.c
>>>> @@ -507,11 +507,16 @@ int vmw_bo_create_kernel(struct vmw_private *dev_priv, unsigned long size,
>>>>  	acc_size = ttm_round_pot(sizeof(*bo));
>>>>  	acc_size += ttm_round_pot(npages * sizeof(void *));
>>>>  	acc_size += ttm_round_pot(sizeof(struct ttm_tt));
>>>> +
>>>> +	ret = ttm_mem_global_alloc(&ttm_mem_glob, acc_size, &ctx);
>>>> +	if (unlikely(ret))
>>>> +		goto error_free;
>>>> +
>>>>  	ret = ttm_bo_init_reserved(&dev_priv->bdev, bo, size,
>>>>  				   ttm_bo_type_device, placement, 0,
>>>> -				   &ctx, acc_size, NULL, NULL, NULL);
>>>> +				   &ctx, NULL, NULL, NULL);
>>>>  	if (unlikely(ret))
>>>> -		goto error_free;
>>>> +		goto error_account;
>>>>    	ttm_bo_pin(bo);
>>>>  	ttm_bo_unreserve(bo);
>>>> @@ -519,6 +524,9 @@ int vmw_bo_create_kernel(struct vmw_private *dev_priv, unsigned long size,
>>>>    	return 0;
>>>>  +error_account:
>>>> +	ttm_mem_global_free(&ttm_mem_glob, acc_size);
>>>> +
>>>>  error_free:
>>>>  	kfree(bo);
>>>>  	return ret;
>>>> @@ -558,11 +566,17 @@ int vmw_bo_init(struct vmw_private *dev_priv,
>>>>  	vmw_bo->base.priority = 3;
>>>>  	vmw_bo->res_tree = RB_ROOT;
>>>>  +	ret = ttm_mem_global_alloc(&ttm_mem_glob, acc_size, &ctx);
>>>> +	if (unlikely(ret))
>>>> +		return ret;
>>>> +
>>>>  	ret = ttm_bo_init_reserved(bdev, &vmw_bo->base, size,
>>>>  				   ttm_bo_type_device, placement,
>>>> -				   0, &ctx, acc_size, NULL, NULL, bo_free);
>>>> -	if (unlikely(ret))
>>>> +				   0, &ctx, NULL, NULL, bo_free);
>>>> +	if (unlikely(ret)) {
>>>> +		ttm_mem_global_free(&ttm_mem_glob, acc_size);
>>>>  		return ret;
>>>> +	}
>>>>    	if (pin)
>>>>  		ttm_bo_pin(&vmw_bo->base);
>>>> diff --git a/drivers/gpu/drm/vmwgfx/vmwgfx_drv.c b/drivers/gpu/drm/vmwgfx/vmwgfx_drv.c
>>>> index 710ba5169a74..6c0ca1011629 100644
>>>> --- a/drivers/gpu/drm/vmwgfx/vmwgfx_drv.c
>>>> +++ b/drivers/gpu/drm/vmwgfx/vmwgfx_drv.c
>>>> @@ -1268,6 +1268,7 @@ static void vmw_remove(struct pci_dev *pdev)
>>>>  {
>>>>  	struct drm_device *dev = pci_get_drvdata(pdev);
>>>>  +	ttm_mem_global_release(&ttm_mem_glob);
>>>>  	drm_dev_unregister(dev);
>>>>  	vmw_driver_unload(dev);
>>>>  }
>>>> @@ -1518,6 +1519,10 @@ static int vmw_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
>>>>    	pci_set_drvdata(pdev, &vmw->drm);
>>>>  +	ret = ttm_mem_global_init(&ttm_mem_glob, &vmw->drm);
>>>> +	if (ret)
>>>> +		return ret;
>>>> +
>>>>  	ret = vmw_driver_load(vmw, ent->device);
>>>>  	if (ret)
>>>>  		return ret;
>>>> diff --git a/drivers/gpu/drm/vmwgfx/vmwgfx_ttm_buffer.c b/drivers/gpu/drm/vmwgfx/vmwgfx_ttm_buffer.c
>>>> index d1bfa59579f1..63f10c865061 100644
>>>> --- a/drivers/gpu/drm/vmwgfx/vmwgfx_ttm_buffer.c
>>>> +++ b/drivers/gpu/drm/vmwgfx/vmwgfx_ttm_buffer.c
>>>> @@ -576,11 +576,31 @@ static void vmw_ttm_destroy(struct ttm_device *bdev, struct ttm_tt *ttm)
>>>>  static int vmw_ttm_populate(struct ttm_device *bdev,
>>>>  			    struct ttm_tt *ttm, struct ttm_operation_ctx *ctx)
>>>>  {
>>>> +	unsigned int i;
>>>> +	int ret;
>>>> +
>>>>  	/* TODO: maybe completely drop this ? */
>>>>  	if (ttm_tt_is_populated(ttm))
>>>>  		return 0;
>>>>  -	return ttm_pool_alloc(&bdev->pool, ttm, ctx);
>>>> +	ret = ttm_pool_alloc(&bdev->pool, ttm, ctx);
>>>> +	if (ret)
>>>> +		return ret;
>>>> +
>>>> +	for (i = 0; i < ttm->num_pages; ++i) {
>>>> +		ret = ttm_mem_global_alloc_page(&ttm_mem_glob, ttm->pages[i],
>>>> +						PAGE_SIZE, ctx);
>>>> +		if (ret)
>>>> +			goto error;
>>>> +	}
>>>> +	return 0;
>>>> +
>>>> +error:
>>>> +	while (i--)
>>>> +		ttm_mem_global_free_page(&ttm_mem_glob, ttm->pages[i],
>>>> +					 PAGE_SIZE);
>>>> +	ttm_pool_free(&bdev->pool, ttm);
>>>> +	return ret;
>>>>  }
>>>>    static void vmw_ttm_unpopulate(struct ttm_device *bdev,
>>>> @@ -588,6 +608,7 @@ static void vmw_ttm_unpopulate(struct ttm_device *bdev,
>>>>  {
>>>>  	struct vmw_ttm_tt *vmw_tt = container_of(ttm, struct vmw_ttm_tt,
>>>>  						 dma_ttm);
>>>> +	unsigned int i;
>>>>    	if (vmw_tt->mob) {
>>>>  		vmw_mob_destroy(vmw_tt->mob);
>>>> @@ -595,6 +616,11 @@ static void vmw_ttm_unpopulate(struct ttm_device *bdev,
>>>>  	}
>>>>    	vmw_ttm_unmap_dma(vmw_tt);
>>>> +
>>>> +	for (i = 0; i < ttm->num_pages; ++i)
>>>> +		ttm_mem_global_free_page(&ttm_mem_glob, ttm->pages[i],
>>>> +					 PAGE_SIZE);
>>>> +
>>>>  	ttm_pool_free(&bdev->pool, ttm);
>>>>  }
>>>>  diff --git a/include/drm/ttm/ttm_bo_api.h b/include/drm/ttm/ttm_bo_api.h
>>>> index 1297a8fb7ccb..4fb523dfab32 100644
>>>> --- a/include/drm/ttm/ttm_bo_api.h
>>>> +++ b/include/drm/ttm/ttm_bo_api.h
>>>> @@ -88,7 +88,6 @@ struct ttm_tt;
>>>>   * @type: The bo type.
>>>>   * @destroy: Destruction function. If NULL, kfree is used.
>>>>   * @num_pages: Actual number of pages.
>>>> - * @acc_size: Accounted size for this object.
>>>>   * @kref: Reference count of this buffer object. When this refcount reaches
>>>>   * zero, the object is destroyed or put on the delayed delete list.
>>>>   * @mem: structure describing current placement.
>>>> @@ -125,7 +124,6 @@ struct ttm_buffer_object {
>>>>  	struct ttm_device *bdev;
>>>>  	enum ttm_bo_type type;
>>>>  	void (*destroy) (struct ttm_buffer_object *);
>>>> -	size_t acc_size;
>>>>    	/**
>>>>  	* Members not needing protection.
>>>> @@ -357,10 +355,6 @@ void ttm_bo_unlock_delayed_workqueue(struct ttm_device *bdev, int resched);
>>>>  bool ttm_bo_eviction_valuable(struct ttm_buffer_object *bo,
>>>>  			      const struct ttm_place *place);
>>>>  -size_t ttm_bo_dma_acc_size(struct ttm_device *bdev,
>>>> -			   unsigned long bo_size,
>>>> -			   unsigned struct_size);
>>>> -
>>>>  /**
>>>>   * ttm_bo_init_reserved
>>>>   *
>>>> @@ -371,7 +365,6 @@ size_t ttm_bo_dma_acc_size(struct ttm_device *bdev,
>>>>   * @flags: Initial placement flags.
>>>>   * @page_alignment: Data alignment in pages.
>>>>   * @ctx: TTM operation context for memory allocation.
>>>> - * @acc_size: Accounted size for this object.
>>>>   * @resv: Pointer to a dma_resv, or NULL to let ttm allocate one.
>>>>   * @destroy: Destroy function. Use NULL for kfree().
>>>>   *
>>>> @@ -402,8 +395,7 @@ int ttm_bo_init_reserved(struct ttm_device *bdev,
>>>>  			 struct ttm_placement *placement,
>>>>  			 uint32_t page_alignment,
>>>>  			 struct ttm_operation_ctx *ctx,
>>>> -			 size_t acc_size, struct sg_table *sg,
>>>> -			 struct dma_resv *resv,
>>>> +			 struct sg_table *sg, struct dma_resv *resv,
>>>>  			 void (*destroy) (struct ttm_buffer_object *));
>>>>    /**
>>>> @@ -421,7 +413,6 @@ int ttm_bo_init_reserved(struct ttm_device *bdev,
>>>>   * holds a pointer to a persistent shmem object. Typically, this would
>>>>   * point to the shmem object backing a GEM object if TTM is used to back a
>>>>   * GEM user interface.
>>>> - * @acc_size: Accounted size for this object.
>>>>   * @resv: Pointer to a dma_resv, or NULL to let ttm allocate one.
>>>>   * @destroy: Destroy function. Use NULL for kfree().
>>>>   *
>>>> @@ -446,7 +437,7 @@ int ttm_bo_init_reserved(struct ttm_device *bdev,
>>>>  int ttm_bo_init(struct ttm_device *bdev, struct ttm_buffer_object *bo,
>>>>  		size_t size, enum ttm_bo_type type,
>>>>  		struct ttm_placement *placement,
>>>> -		uint32_t page_alignment, bool interrubtible, size_t acc_size,
>>>> +		uint32_t page_alignment, bool interrubtible,
>>>>  		struct sg_table *sg, struct dma_resv *resv,
>>>>  		void (*destroy) (struct ttm_buffer_object *));
>>>>  diff --git a/include/drm/ttm/ttm_bo_driver.h b/include/drm/ttm/ttm_bo_driver.h
>>>> index 1c9bf993e252..8959c0075cfd 100644
>>>> --- a/include/drm/ttm/ttm_bo_driver.h
>>>> +++ b/include/drm/ttm/ttm_bo_driver.h
>>>> @@ -40,7 +40,6 @@
>>>>  #include <drm/ttm/ttm_device.h>
>>>>    #include "ttm_bo_api.h"
>>>> -#include "ttm_memory.h"
>>>>  #include "ttm_placement.h"
>>>>  #include "ttm_tt.h"
>>>>  #include "ttm_pool.h"
>>>> diff --git a/include/drm/ttm/ttm_tt.h b/include/drm/ttm/ttm_tt.h
>>>> index cce57fb49e2c..069f8130241a 100644
>>>> --- a/include/drm/ttm/ttm_tt.h
>>>> +++ b/include/drm/ttm/ttm_tt.h
>>>> @@ -30,6 +30,7 @@
>>>>  #include <linux/types.h>
>>>>  #include <drm/ttm/ttm_caching.h>
>>>>  +struct ttm_bo_device;
>>>>  struct ttm_tt;
>>>>  struct ttm_resource;
>>>>  struct ttm_buffer_object;
> 

_______________________________________________
dri-devel mailing list
dri-devel@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/dri-devel

^ permalink raw reply	[flat|nested] 17+ messages in thread

* Re: [Linux-graphics-maintainer] [PATCH 2/3] drm/ttm: move memory accounting into vmwgfx v3
  2021-02-02 17:42         ` Zack Rusin
@ 2021-02-03  2:45           ` Zack Rusin
  2021-02-03  8:20             ` Christian König
  0 siblings, 1 reply; 17+ messages in thread
From: Zack Rusin @ 2021-02-03  2:45 UTC (permalink / raw)
  To: Christian König; +Cc: Linux-graphics-maintainer, dri-devel

Just had a quick peek. The issue is that you can’t attach to the drm device (card0) because it hasn’t been registered yet (drm device registration is last in the vmw_probe in vmwgfx_drv.c via the drm_dev_register). So dev->primary->kdev->kobj that you’re using as argument to kobject_init_and_add in ttm_mem_global_init hasn’t been initialized yet. So that particular sysfs code would likely have to be refactored out of ttm_mem_global_init to another function that could be called after drm registraction. I could take this on but not until Friday or so.

z


> On Feb 2, 2021, at 12:42, Zack Rusin <zackr@vmware.com> wrote:
> 
> Ah, yes, sorry, I missed that. I just double checked and it fails with:
> 
> kobject_add_internal failed for memory_accounting (error: -2 parent: card0)
> 
> which breaks the probe and the driver won’t load. I won’t have time to look into it until tomorrow though.
> 
> z
> 
>> On Feb 2, 2021, at 10:16, Christian König <christian.koenig@amd.com> wrote:
>> 
>> Hi Zack,
>> 
>> can you also give it a quick smoke test?
>> 
>> I'm not sure if I wired up all the sysfs magic correctly inside vmwgfx, but I currently don't have a setup where I can test this.
>> 
>> Thanks,
>> Christian.
>> 
>> Am 02.02.21 um 16:14 schrieb Zack Rusin:
>>> Looks good. There’s probably not much reason to call it ttm_memory anymore as it only deals with ttm_mem_glob, we’ll likely fold it in after you submit. Thanks.
>>> 
>>> Reviewed-by: Zack Rusin <zackr@vmware.com>
>>> 
>>> z
>>> 
>>>> On Feb 2, 2021, at 08:04, Christian König <christian.koenig@amd.com> wrote:
>>>> 
>>>> Ping?
>>>> 
>>>> Especially Roland and Zack do you have any objections to this?
>>>> 
>>>> Regards,
>>>> Christian.
>>>> 
>>>> Am 28.01.21 um 14:16 schrieb Christian König:
>>>>> This is just another feature which is only used by VMWGFX, so move
>>>>> it into the driver instead.
>>>>> 
>>>>> I've tried to add the accounting sysfs file to the kobject of the drm
>>>>> minor, but I'm not 100% sure if this works as expected.
>>>>> 
>>>>> v2: fix typo in KFD and avoid 64bit divide
>>>>> v3: fix init order in VMWGFX
>>>>> 
>>>>> Signed-off-by: Christian König <christian.koenig@amd.com>
>>>>> ---
>>>>> .../gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c  | 16 ++++++---
>>>>> drivers/gpu/drm/amd/amdgpu/amdgpu_object.c    |  8 ++---
>>>>> drivers/gpu/drm/drm_gem_vram_helper.c         |  6 ++--
>>>>> drivers/gpu/drm/nouveau/nouveau_bo.c          |  7 ++--
>>>>> drivers/gpu/drm/nouveau/nouveau_drv.h         |  1 -
>>>>> drivers/gpu/drm/qxl/qxl_object.c              |  4 +--
>>>>> drivers/gpu/drm/radeon/radeon_object.c        |  8 ++---
>>>>> drivers/gpu/drm/ttm/Makefile                  |  7 ++--
>>>>> drivers/gpu/drm/ttm/ttm_bo.c                  | 33 +------------------
>>>>> drivers/gpu/drm/ttm/ttm_bo_util.c             |  1 -
>>>>> drivers/gpu/drm/ttm/ttm_device.c              | 22 ++++++++++---
>>>>> drivers/gpu/drm/ttm/ttm_pool.c                | 13 +-------
>>>>> drivers/gpu/drm/vmwgfx/Makefile               |  2 +-
>>>>> drivers/gpu/drm/{ttm => vmwgfx}/ttm_memory.c  | 19 ++++-------
>>>>> .../gpu/drm/vmwgfx}/ttm_memory.h              |  5 +--
>>>>> drivers/gpu/drm/vmwgfx/ttm_object.h           |  3 +-
>>>>> drivers/gpu/drm/vmwgfx/vmwgfx_bo.c            | 22 ++++++++++---
>>>>> drivers/gpu/drm/vmwgfx/vmwgfx_drv.c           |  5 +++
>>>>> drivers/gpu/drm/vmwgfx/vmwgfx_ttm_buffer.c    | 28 +++++++++++++++-
>>>>> include/drm/ttm/ttm_bo_api.h                  | 13 ++------
>>>>> include/drm/ttm/ttm_bo_driver.h               |  1 -
>>>>> include/drm/ttm/ttm_tt.h                      |  1 +
>>>>> 22 files changed, 110 insertions(+), 115 deletions(-)
>>>>> rename drivers/gpu/drm/{ttm => vmwgfx}/ttm_memory.c (97%)
>>>>> rename {include/drm/ttm => drivers/gpu/drm/vmwgfx}/ttm_memory.h (97%)
>>>>> 
>>>>> diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c b/drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c
>>>>> index 0849b68e784f..e440af37dde8 100644
>>>>> --- a/drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c
>>>>> +++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c
>>>>> @@ -118,6 +118,16 @@ void amdgpu_amdkfd_gpuvm_init_mem_limits(void)
>>>>>  */
>>>>> #define ESTIMATE_PT_SIZE(mem_size) ((mem_size) >> 14)
>>>>> +static size_t amdgpu_amdkfd_acc_size(uint64_t size)
>>>>> +{
>>>>> +size >>= PAGE_SHIFT;
>>>>> +size *= sizeof(dma_addr_t) + sizeof(void *);
>>>>> +
>>>>> +return __roundup_pow_of_two(sizeof(struct amdgpu_bo)) +
>>>>> +__roundup_pow_of_two(sizeof(struct ttm_tt)) +
>>>>> +PAGE_ALIGN(size);
>>>>> +}
>>>>> +
>>>>> static int amdgpu_amdkfd_reserve_mem_limit(struct amdgpu_device *adev,
>>>>> uint64_t size, u32 domain, bool sg)
>>>>> {
>>>>> @@ -126,8 +136,7 @@ static int amdgpu_amdkfd_reserve_mem_limit(struct amdgpu_device *adev,
>>>>> size_t acc_size, system_mem_needed, ttm_mem_needed, vram_needed;
>>>>> int ret = 0;
>>>>> -acc_size = ttm_bo_dma_acc_size(&adev->mman.bdev, size,
>>>>> -       sizeof(struct amdgpu_bo));
>>>>> +acc_size = amdgpu_amdkfd_acc_size(size);
>>>>>   vram_needed = 0;
>>>>> if (domain == AMDGPU_GEM_DOMAIN_GTT) {
>>>>> @@ -174,8 +183,7 @@ static void unreserve_mem_limit(struct amdgpu_device *adev,
>>>>> {
>>>>> size_t acc_size;
>>>>> -acc_size = ttm_bo_dma_acc_size(&adev->mman.bdev, size,
>>>>> -       sizeof(struct amdgpu_bo));
>>>>> +acc_size = amdgpu_amdkfd_acc_size(size);
>>>>>   spin_lock(&kfd_mem_limit.mem_limit_lock);
>>>>> if (domain == AMDGPU_GEM_DOMAIN_GTT) {
>>>>> diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_object.c b/drivers/gpu/drm/amd/amdgpu/amdgpu_object.c
>>>>> index 6cc9919b12cc..599c9a132eb6 100644
>>>>> --- a/drivers/gpu/drm/amd/amdgpu/amdgpu_object.c
>>>>> +++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_object.c
>>>>> @@ -523,7 +523,6 @@ static int amdgpu_bo_do_create(struct amdgpu_device *adev,
>>>>> };
>>>>> struct amdgpu_bo *bo;
>>>>> unsigned long page_align, size = bp->size;
>>>>> -size_t acc_size;
>>>>> int r;
>>>>>   /* Note that GDS/GWS/OA allocates 1 page per byte/resource. */
>>>>> @@ -546,9 +545,6 @@ static int amdgpu_bo_do_create(struct amdgpu_device *adev,
>>>>>   *bo_ptr = NULL;
>>>>> -acc_size = ttm_bo_dma_acc_size(&adev->mman.bdev, size,
>>>>> -       sizeof(struct amdgpu_bo));
>>>>> -
>>>>> bo = kzalloc(sizeof(struct amdgpu_bo), GFP_KERNEL);
>>>>> if (bo == NULL)
>>>>> return -ENOMEM;
>>>>> @@ -577,8 +573,8 @@ static int amdgpu_bo_do_create(struct amdgpu_device *adev,
>>>>> bo->tbo.priority = 1;
>>>>>   r = ttm_bo_init_reserved(&adev->mman.bdev, &bo->tbo, size, bp->type,
>>>>> - &bo->placement, page_align, &ctx, acc_size,
>>>>> - NULL, bp->resv, &amdgpu_bo_destroy);
>>>>> + &bo->placement, page_align, &ctx,  NULL,
>>>>> + bp->resv, &amdgpu_bo_destroy);
>>>>> if (unlikely(r != 0))
>>>>> return r;
>>>>> diff --git a/drivers/gpu/drm/drm_gem_vram_helper.c b/drivers/gpu/drm/drm_gem_vram_helper.c
>>>>> index 0b13c8507688..a0992f0b8afd 100644
>>>>> --- a/drivers/gpu/drm/drm_gem_vram_helper.c
>>>>> +++ b/drivers/gpu/drm/drm_gem_vram_helper.c
>>>>> @@ -189,7 +189,6 @@ struct drm_gem_vram_object *drm_gem_vram_create(struct drm_device *dev,
>>>>> struct drm_vram_mm *vmm = dev->vram_mm;
>>>>> struct ttm_device *bdev;
>>>>> int ret;
>>>>> -size_t acc_size;
>>>>>   if (WARN_ONCE(!vmm, "VRAM MM not initialized"))
>>>>> return ERR_PTR(-EINVAL);
>>>>> @@ -216,7 +215,6 @@ struct drm_gem_vram_object *drm_gem_vram_create(struct drm_device *dev,
>>>>> }
>>>>>   bdev = &vmm->bdev;
>>>>> -acc_size = ttm_bo_dma_acc_size(bdev, size, sizeof(*gbo));
>>>>>   gbo->bo.bdev = bdev;
>>>>> drm_gem_vram_placement(gbo, DRM_GEM_VRAM_PL_FLAG_SYSTEM);
>>>>> @@ -226,8 +224,8 @@ struct drm_gem_vram_object *drm_gem_vram_create(struct drm_device *dev,
>>>>>  * to release gbo->bo.base and kfree gbo.
>>>>>  */
>>>>> ret = ttm_bo_init(bdev, &gbo->bo, size, ttm_bo_type_device,
>>>>> -  &gbo->placement, pg_align, false, acc_size,
>>>>> -  NULL, NULL, ttm_buffer_object_destroy);
>>>>> +  &gbo->placement, pg_align, false, NULL, NULL,
>>>>> +  ttm_buffer_object_destroy);
>>>>> if (ret)
>>>>> return ERR_PTR(ret);
>>>>> diff --git a/drivers/gpu/drm/nouveau/nouveau_bo.c b/drivers/gpu/drm/nouveau/nouveau_bo.c
>>>>> index c177940d6e2c..ca2a8ae1938e 100644
>>>>> --- a/drivers/gpu/drm/nouveau/nouveau_bo.c
>>>>> +++ b/drivers/gpu/drm/nouveau/nouveau_bo.c
>>>>> @@ -300,18 +300,15 @@ nouveau_bo_init(struct nouveau_bo *nvbo, u64 size, int align, u32 domain,
>>>>> struct sg_table *sg, struct dma_resv *robj)
>>>>> {
>>>>> int type = sg ? ttm_bo_type_sg : ttm_bo_type_device;
>>>>> -size_t acc_size;
>>>>> int ret;
>>>>> -acc_size = ttm_bo_dma_acc_size(nvbo->bo.bdev, size, sizeof(*nvbo));
>>>>> -
>>>>> nvbo->bo.mem.num_pages = size >> PAGE_SHIFT;
>>>>> nouveau_bo_placement_set(nvbo, domain, 0);
>>>>> INIT_LIST_HEAD(&nvbo->io_reserve_lru);
>>>>>   ret = ttm_bo_init(nvbo->bo.bdev, &nvbo->bo, size, type,
>>>>> -  &nvbo->placement, align >> PAGE_SHIFT, false,
>>>>> -  acc_size, sg, robj, nouveau_bo_del_ttm);
>>>>> +  &nvbo->placement, align >> PAGE_SHIFT, false, sg,
>>>>> +  robj, nouveau_bo_del_ttm);
>>>>> if (ret) {
>>>>> /* ttm will call nouveau_bo_del_ttm if it fails.. */
>>>>> return ret;
>>>>> diff --git a/drivers/gpu/drm/nouveau/nouveau_drv.h b/drivers/gpu/drm/nouveau/nouveau_drv.h
>>>>> index edf9d1ee9d58..a491c2c1c56e 100644
>>>>> --- a/drivers/gpu/drm/nouveau/nouveau_drv.h
>>>>> +++ b/drivers/gpu/drm/nouveau/nouveau_drv.h
>>>>> @@ -54,7 +54,6 @@
>>>>> #include <drm/ttm/ttm_bo_api.h>
>>>>> #include <drm/ttm/ttm_bo_driver.h>
>>>>> #include <drm/ttm/ttm_placement.h>
>>>>> -#include <drm/ttm/ttm_memory.h>
>>>>>   #include <drm/drm_audio_component.h>
>>>>> diff --git a/drivers/gpu/drm/qxl/qxl_object.c b/drivers/gpu/drm/qxl/qxl_object.c
>>>>> index ceebc5881f68..705b51535492 100644
>>>>> --- a/drivers/gpu/drm/qxl/qxl_object.c
>>>>> +++ b/drivers/gpu/drm/qxl/qxl_object.c
>>>>> @@ -138,8 +138,8 @@ int qxl_bo_create(struct qxl_device *qdev,
>>>>> qxl_ttm_placement_from_domain(bo, domain);
>>>>>   r = ttm_bo_init_reserved(&qdev->mman.bdev, &bo->tbo, size, type,
>>>>> - &bo->placement, 0, &ctx, size,
>>>>> - NULL, NULL, &qxl_ttm_bo_destroy);
>>>>> + &bo->placement, 0, &ctx, NULL, NULL,
>>>>> + &qxl_ttm_bo_destroy);
>>>>> if (unlikely(r != 0)) {
>>>>> if (r != -ERESTARTSYS)
>>>>> dev_err(qdev->ddev.dev,
>>>>> diff --git a/drivers/gpu/drm/radeon/radeon_object.c b/drivers/gpu/drm/radeon/radeon_object.c
>>>>> index 6a336284466f..804f7a427be7 100644
>>>>> --- a/drivers/gpu/drm/radeon/radeon_object.c
>>>>> +++ b/drivers/gpu/drm/radeon/radeon_object.c
>>>>> @@ -159,7 +159,6 @@ int radeon_bo_create(struct radeon_device *rdev,
>>>>> struct radeon_bo *bo;
>>>>> enum ttm_bo_type type;
>>>>> unsigned long page_align = roundup(byte_align, PAGE_SIZE) >> PAGE_SHIFT;
>>>>> -size_t acc_size;
>>>>> int r;
>>>>>   size = ALIGN(size, PAGE_SIZE);
>>>>> @@ -173,9 +172,6 @@ int radeon_bo_create(struct radeon_device *rdev,
>>>>> }
>>>>> *bo_ptr = NULL;
>>>>> -acc_size = ttm_bo_dma_acc_size(&rdev->mman.bdev, size,
>>>>> -       sizeof(struct radeon_bo));
>>>>> -
>>>>> bo = kzalloc(sizeof(struct radeon_bo), GFP_KERNEL);
>>>>> if (bo == NULL)
>>>>> return -ENOMEM;
>>>>> @@ -230,8 +226,8 @@ int radeon_bo_create(struct radeon_device *rdev,
>>>>> /* Kernel allocation are uninterruptible */
>>>>> down_read(&rdev->pm.mclk_lock);
>>>>> r = ttm_bo_init(&rdev->mman.bdev, &bo->tbo, size, type,
>>>>> -&bo->placement, page_align, !kernel, acc_size,
>>>>> -sg, resv, &radeon_ttm_bo_destroy);
>>>>> +&bo->placement, page_align, !kernel, sg, resv,
>>>>> +&radeon_ttm_bo_destroy);
>>>>> up_read(&rdev->pm.mclk_lock);
>>>>> if (unlikely(r != 0)) {
>>>>> return r;
>>>>> diff --git a/drivers/gpu/drm/ttm/Makefile b/drivers/gpu/drm/ttm/Makefile
>>>>> index 8e6437eadabe..40e5e9da7953 100644
>>>>> --- a/drivers/gpu/drm/ttm/Makefile
>>>>> +++ b/drivers/gpu/drm/ttm/Makefile
>>>>> @@ -2,10 +2,9 @@
>>>>> #
>>>>> # Makefile for the drm device driver.  This driver provides support for the
>>>>> -ttm-y := ttm_memory.o ttm_tt.o ttm_bo.o \
>>>>> -ttm_bo_util.o ttm_bo_vm.o ttm_module.o \
>>>>> -ttm_execbuf_util.o ttm_range_manager.o \
>>>>> -ttm_resource.o ttm_pool.o ttm_device.o
>>>>> +ttm-y := ttm_tt.o ttm_bo.o ttm_bo_util.o ttm_bo_vm.o ttm_module.o \
>>>>> +ttm_execbuf_util.o ttm_range_manager.o ttm_resource.o ttm_pool.o \
>>>>> +ttm_device.o
>>>>> ttm-$(CONFIG_AGP) += ttm_agp_backend.o
>>>>>   obj-$(CONFIG_DRM_TTM) += ttm.o
>>>>> diff --git a/drivers/gpu/drm/ttm/ttm_bo.c b/drivers/gpu/drm/ttm/ttm_bo.c
>>>>> index 643befc1a6f2..e38102282fd5 100644
>>>>> --- a/drivers/gpu/drm/ttm/ttm_bo.c
>>>>> +++ b/drivers/gpu/drm/ttm/ttm_bo.c
>>>>> @@ -425,7 +425,6 @@ static void ttm_bo_release(struct kref *kref)
>>>>> struct ttm_buffer_object *bo =
>>>>>     container_of(kref, struct ttm_buffer_object, kref);
>>>>> struct ttm_device *bdev = bo->bdev;
>>>>> -size_t acc_size = bo->acc_size;
>>>>> int ret;
>>>>>   if (!bo->deleted) {
>>>>> @@ -485,7 +484,6 @@ static void ttm_bo_release(struct kref *kref)
>>>>> if (!ttm_bo_uses_embedded_gem_object(bo))
>>>>> dma_resv_fini(&bo->base._resv);
>>>>> bo->destroy(bo);
>>>>> -ttm_mem_global_free(&ttm_mem_glob, acc_size);
>>>>> }
>>>>>   void ttm_bo_put(struct ttm_buffer_object *bo)
>>>>> @@ -1046,25 +1044,13 @@ int ttm_bo_init_reserved(struct ttm_device *bdev,
>>>>>  struct ttm_placement *placement,
>>>>>  uint32_t page_alignment,
>>>>>  struct ttm_operation_ctx *ctx,
>>>>> - size_t acc_size,
>>>>>  struct sg_table *sg,
>>>>>  struct dma_resv *resv,
>>>>>  void (*destroy) (struct ttm_buffer_object *))
>>>>> {
>>>>> -struct ttm_mem_global *mem_glob = &ttm_mem_glob;
>>>>> bool locked;
>>>>> int ret = 0;
>>>>> -ret = ttm_mem_global_alloc(mem_glob, acc_size, ctx);
>>>>> -if (ret) {
>>>>> -pr_err("Out of kernel memory\n");
>>>>> -if (destroy)
>>>>> -(*destroy)(bo);
>>>>> -else
>>>>> -kfree(bo);
>>>>> -return -ENOMEM;
>>>>> -}
>>>>> -
>>>>> bo->destroy = destroy ? destroy : ttm_bo_default_destroy;
>>>>>   kref_init(&bo->kref);
>>>>> @@ -1081,7 +1067,6 @@ int ttm_bo_init_reserved(struct ttm_device *bdev,
>>>>> bo->mem.bus.addr = NULL;
>>>>> bo->moving = NULL;
>>>>> bo->mem.placement = 0;
>>>>> -bo->acc_size = acc_size;
>>>>> bo->pin_count = 0;
>>>>> bo->sg = sg;
>>>>> if (resv) {
>>>>> @@ -1142,7 +1127,6 @@ int ttm_bo_init(struct ttm_device *bdev,
>>>>> struct ttm_placement *placement,
>>>>> uint32_t page_alignment,
>>>>> bool interruptible,
>>>>> -size_t acc_size,
>>>>> struct sg_table *sg,
>>>>> struct dma_resv *resv,
>>>>> void (*destroy) (struct ttm_buffer_object *))
>>>>> @@ -1151,8 +1135,7 @@ int ttm_bo_init(struct ttm_device *bdev,
>>>>> int ret;
>>>>>   ret = ttm_bo_init_reserved(bdev, bo, size, type, placement,
>>>>> -   page_alignment, &ctx, acc_size,
>>>>> -   sg, resv, destroy);
>>>>> +   page_alignment, &ctx, sg, resv, destroy);
>>>>> if (ret)
>>>>> return ret;
>>>>> @@ -1163,20 +1146,6 @@ int ttm_bo_init(struct ttm_device *bdev,
>>>>> }
>>>>> EXPORT_SYMBOL(ttm_bo_init);
>>>>> -size_t ttm_bo_dma_acc_size(struct ttm_device *bdev,
>>>>> -   unsigned long bo_size,
>>>>> -   unsigned struct_size)
>>>>> -{
>>>>> -unsigned npages = (PAGE_ALIGN(bo_size)) >> PAGE_SHIFT;
>>>>> -size_t size = 0;
>>>>> -
>>>>> -size += ttm_round_pot(struct_size);
>>>>> -size += ttm_round_pot(npages * (2*sizeof(void *) + sizeof(dma_addr_t)));
>>>>> -size += ttm_round_pot(sizeof(struct ttm_tt));
>>>>> -return size;
>>>>> -}
>>>>> -EXPORT_SYMBOL(ttm_bo_dma_acc_size);
>>>>> -
>>>>> /*
>>>>>  * buffer object vm functions.
>>>>>  */
>>>>> diff --git a/drivers/gpu/drm/ttm/ttm_bo_util.c b/drivers/gpu/drm/ttm/ttm_bo_util.c
>>>>> index db0f2661d504..031e5819fec4 100644
>>>>> --- a/drivers/gpu/drm/ttm/ttm_bo_util.c
>>>>> +++ b/drivers/gpu/drm/ttm/ttm_bo_util.c
>>>>> @@ -309,7 +309,6 @@ static int ttm_buffer_object_transfer(struct ttm_buffer_object *bo,
>>>>>   kref_init(&fbo->base.kref);
>>>>> fbo->base.destroy = &ttm_transfered_destroy;
>>>>> -fbo->base.acc_size = 0;
>>>>> fbo->base.pin_count = 0;
>>>>> if (bo->type != ttm_bo_type_sg)
>>>>> fbo->base.base.resv = &fbo->base.base._resv;
>>>>> diff --git a/drivers/gpu/drm/ttm/ttm_device.c b/drivers/gpu/drm/ttm/ttm_device.c
>>>>> index ac0903c9e60a..6bde344e5da7 100644
>>>>> --- a/drivers/gpu/drm/ttm/ttm_device.c
>>>>> +++ b/drivers/gpu/drm/ttm/ttm_device.c
>>>>> @@ -27,9 +27,12 @@
>>>>>   #define pr_fmt(fmt) "[TTM DEVICE] " fmt
>>>>> +#include <linux/mm.h>
>>>>> +
>>>>> #include <drm/ttm/ttm_device.h>
>>>>> -#include <drm/ttm/ttm_memory.h>
>>>>> +#include <drm/ttm/ttm_tt.h>
>>>>> #include <drm/ttm/ttm_placement.h>
>>>>> +#include <drm/ttm/ttm_bo_api.h>
>>>>>   #include "ttm_module.h"
>>>>> @@ -49,9 +52,11 @@ static void ttm_global_release(void)
>>>>> if (--ttm_glob_use_count > 0)
>>>>> goto out;
>>>>> +ttm_pool_mgr_fini();
>>>>> +ttm_tt_mgr_fini();
>>>>> +
>>>>> kobject_del(&glob->kobj);
>>>>> kobject_put(&glob->kobj);
>>>>> -ttm_mem_global_release(&ttm_mem_glob);
>>>>> __free_page(glob->dummy_read_page);
>>>>> memset(glob, 0, sizeof(*glob));
>>>>> out:
>>>>> @@ -61,6 +66,8 @@ static void ttm_global_release(void)
>>>>> static int ttm_global_init(void)
>>>>> {
>>>>> struct ttm_global *glob = &ttm_glob;
>>>>> +unsigned long num_pages;
>>>>> +struct sysinfo si;
>>>>> int ret = 0;
>>>>> unsigned i;
>>>>> @@ -68,9 +75,14 @@ static int ttm_global_init(void)
>>>>> if (++ttm_glob_use_count > 1)
>>>>> goto out;
>>>>> -ret = ttm_mem_global_init(&ttm_mem_glob);
>>>>> -if (ret)
>>>>> -goto out;
>>>>> +si_meminfo(&si);
>>>>> +
>>>>> +/* Limit the number of pages in the pool to about 50% of the total
>>>>> + * system memory.
>>>>> + */
>>>>> +num_pages = ((u64)si.totalram * si.mem_unit) >> PAGE_SHIFT;
>>>>> +ttm_pool_mgr_init(num_pages * 50 / 100);
>>>>> +ttm_tt_mgr_init();
>>>>>   spin_lock_init(&glob->lru_lock);
>>>>> glob->dummy_read_page = alloc_page(__GFP_ZERO | GFP_DMA32);
>>>>> diff --git a/drivers/gpu/drm/ttm/ttm_pool.c b/drivers/gpu/drm/ttm/ttm_pool.c
>>>>> index e0617717113f..6b0f957d63d5 100644
>>>>> --- a/drivers/gpu/drm/ttm/ttm_pool.c
>>>>> +++ b/drivers/gpu/drm/ttm/ttm_pool.c
>>>>> @@ -404,16 +404,10 @@ int ttm_pool_alloc(struct ttm_pool *pool, struct ttm_tt *tt,
>>>>> caching = pages + (1 << order);
>>>>> }
>>>>> -r = ttm_mem_global_alloc_page(&ttm_mem_glob, p,
>>>>> -      (1 << order) * PAGE_SIZE,
>>>>> -      ctx);
>>>>> -if (r)
>>>>> -goto error_free_page;
>>>>> -
>>>>> if (dma_addr) {
>>>>> r = ttm_pool_map(pool, order, p, &dma_addr);
>>>>> if (r)
>>>>> -goto error_global_free;
>>>>> +goto error_free_page;
>>>>> }
>>>>>   num_pages -= 1 << order;
>>>>> @@ -427,9 +421,6 @@ int ttm_pool_alloc(struct ttm_pool *pool, struct ttm_tt *tt,
>>>>>   return 0;
>>>>> -error_global_free:
>>>>> -ttm_mem_global_free_page(&ttm_mem_glob, p, (1 << order) * PAGE_SIZE);
>>>>> -
>>>>> error_free_page:
>>>>> ttm_pool_free_page(pool, tt->caching, order, p);
>>>>> @@ -464,8 +455,6 @@ void ttm_pool_free(struct ttm_pool *pool, struct ttm_tt *tt)
>>>>>   order = ttm_pool_page_order(pool, p);
>>>>> num_pages = 1ULL << order;
>>>>> -ttm_mem_global_free_page(&ttm_mem_glob, p,
>>>>> - num_pages * PAGE_SIZE);
>>>>> if (tt->dma_address)
>>>>> ttm_pool_unmap(pool, tt->dma_address[i], num_pages);
>>>>> diff --git a/drivers/gpu/drm/vmwgfx/Makefile b/drivers/gpu/drm/vmwgfx/Makefile
>>>>> index cc4cdca7176e..8c02fa5852e7 100644
>>>>> --- a/drivers/gpu/drm/vmwgfx/Makefile
>>>>> +++ b/drivers/gpu/drm/vmwgfx/Makefile
>>>>> @@ -9,7 +9,7 @@ vmwgfx-y := vmwgfx_execbuf.o vmwgfx_gmr.o vmwgfx_kms.o vmwgfx_drv.o \
>>>>>     vmwgfx_cotable.o vmwgfx_so.o vmwgfx_binding.o vmwgfx_msg.o \
>>>>>     vmwgfx_simple_resource.o vmwgfx_va.o vmwgfx_blit.o \
>>>>>     vmwgfx_validation.o vmwgfx_page_dirty.o vmwgfx_streamoutput.o \
>>>>> -    ttm_object.o ttm_lock.o
>>>>> +    ttm_object.o ttm_lock.o ttm_memory.o
>>>>>   vmwgfx-$(CONFIG_TRANSPARENT_HUGEPAGE) += vmwgfx_thp.o
>>>>> obj-$(CONFIG_DRM_VMWGFX) := vmwgfx.o
>>>>> diff --git a/drivers/gpu/drm/ttm/ttm_memory.c b/drivers/gpu/drm/vmwgfx/ttm_memory.c
>>>>> similarity index 97%
>>>>> rename from drivers/gpu/drm/ttm/ttm_memory.c
>>>>> rename to drivers/gpu/drm/vmwgfx/ttm_memory.c
>>>>> index 634a85c2dc4c..1306d9e0f095 100644
>>>>> --- a/drivers/gpu/drm/ttm/ttm_memory.c
>>>>> +++ b/drivers/gpu/drm/vmwgfx/ttm_memory.c
>>>>> @@ -28,7 +28,6 @@
>>>>>   #define pr_fmt(fmt) "[TTM] " fmt
>>>>> -#include <drm/ttm/ttm_memory.h>
>>>>> #include <linux/spinlock.h>
>>>>> #include <linux/sched.h>
>>>>> #include <linux/wait.h>
>>>>> @@ -36,10 +35,11 @@
>>>>> #include <linux/module.h>
>>>>> #include <linux/slab.h>
>>>>> #include <linux/swap.h>
>>>>> -#include <drm/ttm/ttm_pool.h>
>>>>> -#include <drm/ttm/ttm_tt.h>
>>>>> -#include "ttm_module.h"
>>>>> +#include <drm/drm_device.h>
>>>>> +#include <drm/drm_file.h>
>>>>> +
>>>>> +#include "ttm_memory.h"
>>>>>   #define TTM_MEMORY_ALLOC_RETRIES 4
>>>>> @@ -414,7 +414,7 @@ static int ttm_mem_init_dma32_zone(struct ttm_mem_global *glob,
>>>>> }
>>>>> #endif
>>>>> -int ttm_mem_global_init(struct ttm_mem_global *glob)
>>>>> +int ttm_mem_global_init(struct ttm_mem_global *glob, struct drm_device *dev)
>>>>> {
>>>>> struct sysinfo si;
>>>>> int ret;
>>>>> @@ -425,7 +425,8 @@ int ttm_mem_global_init(struct ttm_mem_global *glob)
>>>>> glob->swap_queue = create_singlethread_workqueue("ttm_swap");
>>>>> INIT_WORK(&glob->work, ttm_shrink_work);
>>>>> ret = kobject_init_and_add(
>>>>> -&glob->kobj, &ttm_mem_glob_kobj_type, ttm_get_kobj(), "memory_accounting");
>>>>> +&glob->kobj, &ttm_mem_glob_kobj_type, &dev->primary->kdev->kobj,
>>>>> +"memory_accounting");
>>>>> if (unlikely(ret != 0)) {
>>>>> kobject_put(&glob->kobj);
>>>>> return ret;
>>>>> @@ -453,8 +454,6 @@ int ttm_mem_global_init(struct ttm_mem_global *glob)
>>>>> pr_info("Zone %7s: Available graphics memory: %llu KiB\n",
>>>>> zone->name, (unsigned long long)zone->max_mem >> 10);
>>>>> }
>>>>> -ttm_pool_mgr_init(glob->zone_kernel->max_mem/(2*PAGE_SIZE));
>>>>> -ttm_tt_mgr_init();
>>>>> return 0;
>>>>> out_no_zone:
>>>>> ttm_mem_global_release(glob);
>>>>> @@ -466,10 +465,6 @@ void ttm_mem_global_release(struct ttm_mem_global *glob)
>>>>> struct ttm_mem_zone *zone;
>>>>> unsigned int i;
>>>>> -/* let the page allocator first stop the shrink work. */
>>>>> -ttm_pool_mgr_fini();
>>>>> -ttm_tt_mgr_fini();
>>>>> -
>>>>> flush_workqueue(glob->swap_queue);
>>>>> destroy_workqueue(glob->swap_queue);
>>>>> glob->swap_queue = NULL;
>>>>> diff --git a/include/drm/ttm/ttm_memory.h b/drivers/gpu/drm/vmwgfx/ttm_memory.h
>>>>> similarity index 97%
>>>>> rename from include/drm/ttm/ttm_memory.h
>>>>> rename to drivers/gpu/drm/vmwgfx/ttm_memory.h
>>>>> index c1f167881e33..850ee6c867da 100644
>>>>> --- a/include/drm/ttm/ttm_memory.h
>>>>> +++ b/drivers/gpu/drm/vmwgfx/ttm_memory.h
>>>>> @@ -35,7 +35,8 @@
>>>>> #include <linux/errno.h>
>>>>> #include <linux/kobject.h>
>>>>> #include <linux/mm.h>
>>>>> -#include "ttm_bo_api.h"
>>>>> +
>>>>> +#include <drm/ttm/ttm_bo_api.h>
>>>>>   /**
>>>>>  * struct ttm_mem_global - Global memory accounting structure.
>>>>> @@ -79,7 +80,7 @@ extern struct ttm_mem_global {
>>>>> #endif
>>>>> } ttm_mem_glob;
>>>>> -int ttm_mem_global_init(struct ttm_mem_global *glob);
>>>>> +int ttm_mem_global_init(struct ttm_mem_global *glob, struct drm_device *dev);
>>>>> void ttm_mem_global_release(struct ttm_mem_global *glob);
>>>>> int ttm_mem_global_alloc(struct ttm_mem_global *glob, uint64_t memory,
>>>>>  struct ttm_operation_ctx *ctx);
>>>>> diff --git a/drivers/gpu/drm/vmwgfx/ttm_object.h b/drivers/gpu/drm/vmwgfx/ttm_object.h
>>>>> index ede26df87c93..49b064f0cb19 100644
>>>>> --- a/drivers/gpu/drm/vmwgfx/ttm_object.h
>>>>> +++ b/drivers/gpu/drm/vmwgfx/ttm_object.h
>>>>> @@ -43,7 +43,8 @@
>>>>> #include <linux/rcupdate.h>
>>>>>   #include <drm/drm_hashtab.h>
>>>>> -#include <drm/ttm/ttm_memory.h>
>>>>> +
>>>>> +#include "ttm_memory.h"
>>>>>   /**
>>>>>  * enum ttm_ref_type
>>>>> diff --git a/drivers/gpu/drm/vmwgfx/vmwgfx_bo.c b/drivers/gpu/drm/vmwgfx/vmwgfx_bo.c
>>>>> index 6b3bfd8c678a..50e529a01677 100644
>>>>> --- a/drivers/gpu/drm/vmwgfx/vmwgfx_bo.c
>>>>> +++ b/drivers/gpu/drm/vmwgfx/vmwgfx_bo.c
>>>>> @@ -507,11 +507,16 @@ int vmw_bo_create_kernel(struct vmw_private *dev_priv, unsigned long size,
>>>>> acc_size = ttm_round_pot(sizeof(*bo));
>>>>> acc_size += ttm_round_pot(npages * sizeof(void *));
>>>>> acc_size += ttm_round_pot(sizeof(struct ttm_tt));
>>>>> +
>>>>> +ret = ttm_mem_global_alloc(&ttm_mem_glob, acc_size, &ctx);
>>>>> +if (unlikely(ret))
>>>>> +goto error_free;
>>>>> +
>>>>> ret = ttm_bo_init_reserved(&dev_priv->bdev, bo, size,
>>>>>    ttm_bo_type_device, placement, 0,
>>>>> -   &ctx, acc_size, NULL, NULL, NULL);
>>>>> +   &ctx, NULL, NULL, NULL);
>>>>> if (unlikely(ret))
>>>>> -goto error_free;
>>>>> +goto error_account;
>>>>>   ttm_bo_pin(bo);
>>>>> ttm_bo_unreserve(bo);
>>>>> @@ -519,6 +524,9 @@ int vmw_bo_create_kernel(struct vmw_private *dev_priv, unsigned long size,
>>>>>   return 0;
>>>>> +error_account:
>>>>> +ttm_mem_global_free(&ttm_mem_glob, acc_size);
>>>>> +
>>>>> error_free:
>>>>> kfree(bo);
>>>>> return ret;
>>>>> @@ -558,11 +566,17 @@ int vmw_bo_init(struct vmw_private *dev_priv,
>>>>> vmw_bo->base.priority = 3;
>>>>> vmw_bo->res_tree = RB_ROOT;
>>>>> +ret = ttm_mem_global_alloc(&ttm_mem_glob, acc_size, &ctx);
>>>>> +if (unlikely(ret))
>>>>> +return ret;
>>>>> +
>>>>> ret = ttm_bo_init_reserved(bdev, &vmw_bo->base, size,
>>>>>    ttm_bo_type_device, placement,
>>>>> -   0, &ctx, acc_size, NULL, NULL, bo_free);
>>>>> -if (unlikely(ret))
>>>>> +   0, &ctx, NULL, NULL, bo_free);
>>>>> +if (unlikely(ret)) {
>>>>> +ttm_mem_global_free(&ttm_mem_glob, acc_size);
>>>>> return ret;
>>>>> +}
>>>>>   if (pin)
>>>>> ttm_bo_pin(&vmw_bo->base);
>>>>> diff --git a/drivers/gpu/drm/vmwgfx/vmwgfx_drv.c b/drivers/gpu/drm/vmwgfx/vmwgfx_drv.c
>>>>> index 710ba5169a74..6c0ca1011629 100644
>>>>> --- a/drivers/gpu/drm/vmwgfx/vmwgfx_drv.c
>>>>> +++ b/drivers/gpu/drm/vmwgfx/vmwgfx_drv.c
>>>>> @@ -1268,6 +1268,7 @@ static void vmw_remove(struct pci_dev *pdev)
>>>>> {
>>>>> struct drm_device *dev = pci_get_drvdata(pdev);
>>>>> +ttm_mem_global_release(&ttm_mem_glob);
>>>>> drm_dev_unregister(dev);
>>>>> vmw_driver_unload(dev);
>>>>> }
>>>>> @@ -1518,6 +1519,10 @@ static int vmw_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
>>>>>   pci_set_drvdata(pdev, &vmw->drm);
>>>>> +ret = ttm_mem_global_init(&ttm_mem_glob, &vmw->drm);
>>>>> +if (ret)
>>>>> +return ret;
>>>>> +
>>>>> ret = vmw_driver_load(vmw, ent->device);
>>>>> if (ret)
>>>>> return ret;
>>>>> diff --git a/drivers/gpu/drm/vmwgfx/vmwgfx_ttm_buffer.c b/drivers/gpu/drm/vmwgfx/vmwgfx_ttm_buffer.c
>>>>> index d1bfa59579f1..63f10c865061 100644
>>>>> --- a/drivers/gpu/drm/vmwgfx/vmwgfx_ttm_buffer.c
>>>>> +++ b/drivers/gpu/drm/vmwgfx/vmwgfx_ttm_buffer.c
>>>>> @@ -576,11 +576,31 @@ static void vmw_ttm_destroy(struct ttm_device *bdev, struct ttm_tt *ttm)
>>>>> static int vmw_ttm_populate(struct ttm_device *bdev,
>>>>>     struct ttm_tt *ttm, struct ttm_operation_ctx *ctx)
>>>>> {
>>>>> +unsigned int i;
>>>>> +int ret;
>>>>> +
>>>>> /* TODO: maybe completely drop this ? */
>>>>> if (ttm_tt_is_populated(ttm))
>>>>> return 0;
>>>>> -return ttm_pool_alloc(&bdev->pool, ttm, ctx);
>>>>> +ret = ttm_pool_alloc(&bdev->pool, ttm, ctx);
>>>>> +if (ret)
>>>>> +return ret;
>>>>> +
>>>>> +for (i = 0; i < ttm->num_pages; ++i) {
>>>>> +ret = ttm_mem_global_alloc_page(&ttm_mem_glob, ttm->pages[i],
>>>>> +PAGE_SIZE, ctx);
>>>>> +if (ret)
>>>>> +goto error;
>>>>> +}
>>>>> +return 0;
>>>>> +
>>>>> +error:
>>>>> +while (i--)
>>>>> +ttm_mem_global_free_page(&ttm_mem_glob, ttm->pages[i],
>>>>> + PAGE_SIZE);
>>>>> +ttm_pool_free(&bdev->pool, ttm);
>>>>> +return ret;
>>>>> }
>>>>>   static void vmw_ttm_unpopulate(struct ttm_device *bdev,
>>>>> @@ -588,6 +608,7 @@ static void vmw_ttm_unpopulate(struct ttm_device *bdev,
>>>>> {
>>>>> struct vmw_ttm_tt *vmw_tt = container_of(ttm, struct vmw_ttm_tt,
>>>>>  dma_ttm);
>>>>> +unsigned int i;
>>>>>   if (vmw_tt->mob) {
>>>>> vmw_mob_destroy(vmw_tt->mob);
>>>>> @@ -595,6 +616,11 @@ static void vmw_ttm_unpopulate(struct ttm_device *bdev,
>>>>> }
>>>>>   vmw_ttm_unmap_dma(vmw_tt);
>>>>> +
>>>>> +for (i = 0; i < ttm->num_pages; ++i)
>>>>> +ttm_mem_global_free_page(&ttm_mem_glob, ttm->pages[i],
>>>>> + PAGE_SIZE);
>>>>> +
>>>>> ttm_pool_free(&bdev->pool, ttm);
>>>>> }
>>>>> diff --git a/include/drm/ttm/ttm_bo_api.h b/include/drm/ttm/ttm_bo_api.h
>>>>> index 1297a8fb7ccb..4fb523dfab32 100644
>>>>> --- a/include/drm/ttm/ttm_bo_api.h
>>>>> +++ b/include/drm/ttm/ttm_bo_api.h
>>>>> @@ -88,7 +88,6 @@ struct ttm_tt;
>>>>>  * @type: The bo type.
>>>>>  * @destroy: Destruction function. If NULL, kfree is used.
>>>>>  * @num_pages: Actual number of pages.
>>>>> - * @acc_size: Accounted size for this object.
>>>>>  * @kref: Reference count of this buffer object. When this refcount reaches
>>>>>  * zero, the object is destroyed or put on the delayed delete list.
>>>>>  * @mem: structure describing current placement.
>>>>> @@ -125,7 +124,6 @@ struct ttm_buffer_object {
>>>>> struct ttm_device *bdev;
>>>>> enum ttm_bo_type type;
>>>>> void (*destroy) (struct ttm_buffer_object *);
>>>>> -size_t acc_size;
>>>>>   /**
>>>>> * Members not needing protection.
>>>>> @@ -357,10 +355,6 @@ void ttm_bo_unlock_delayed_workqueue(struct ttm_device *bdev, int resched);
>>>>> bool ttm_bo_eviction_valuable(struct ttm_buffer_object *bo,
>>>>>       const struct ttm_place *place);
>>>>> -size_t ttm_bo_dma_acc_size(struct ttm_device *bdev,
>>>>> -   unsigned long bo_size,
>>>>> -   unsigned struct_size);
>>>>> -
>>>>> /**
>>>>>  * ttm_bo_init_reserved
>>>>>  *
>>>>> @@ -371,7 +365,6 @@ size_t ttm_bo_dma_acc_size(struct ttm_device *bdev,
>>>>>  * @flags: Initial placement flags.
>>>>>  * @page_alignment: Data alignment in pages.
>>>>>  * @ctx: TTM operation context for memory allocation.
>>>>> - * @acc_size: Accounted size for this object.
>>>>>  * @resv: Pointer to a dma_resv, or NULL to let ttm allocate one.
>>>>>  * @destroy: Destroy function. Use NULL for kfree().
>>>>>  *
>>>>> @@ -402,8 +395,7 @@ int ttm_bo_init_reserved(struct ttm_device *bdev,
>>>>>  struct ttm_placement *placement,
>>>>>  uint32_t page_alignment,
>>>>>  struct ttm_operation_ctx *ctx,
>>>>> - size_t acc_size, struct sg_table *sg,
>>>>> - struct dma_resv *resv,
>>>>> + struct sg_table *sg, struct dma_resv *resv,
>>>>>  void (*destroy) (struct ttm_buffer_object *));
>>>>>   /**
>>>>> @@ -421,7 +413,6 @@ int ttm_bo_init_reserved(struct ttm_device *bdev,
>>>>>  * holds a pointer to a persistent shmem object. Typically, this would
>>>>>  * point to the shmem object backing a GEM object if TTM is used to back a
>>>>>  * GEM user interface.
>>>>> - * @acc_size: Accounted size for this object.
>>>>>  * @resv: Pointer to a dma_resv, or NULL to let ttm allocate one.
>>>>>  * @destroy: Destroy function. Use NULL for kfree().
>>>>>  *
>>>>> @@ -446,7 +437,7 @@ int ttm_bo_init_reserved(struct ttm_device *bdev,
>>>>> int ttm_bo_init(struct ttm_device *bdev, struct ttm_buffer_object *bo,
>>>>> size_t size, enum ttm_bo_type type,
>>>>> struct ttm_placement *placement,
>>>>> -uint32_t page_alignment, bool interrubtible, size_t acc_size,
>>>>> +uint32_t page_alignment, bool interrubtible,
>>>>> struct sg_table *sg, struct dma_resv *resv,
>>>>> void (*destroy) (struct ttm_buffer_object *));
>>>>> diff --git a/include/drm/ttm/ttm_bo_driver.h b/include/drm/ttm/ttm_bo_driver.h
>>>>> index 1c9bf993e252..8959c0075cfd 100644
>>>>> --- a/include/drm/ttm/ttm_bo_driver.h
>>>>> +++ b/include/drm/ttm/ttm_bo_driver.h
>>>>> @@ -40,7 +40,6 @@
>>>>> #include <drm/ttm/ttm_device.h>
>>>>>   #include "ttm_bo_api.h"
>>>>> -#include "ttm_memory.h"
>>>>> #include "ttm_placement.h"
>>>>> #include "ttm_tt.h"
>>>>> #include "ttm_pool.h"
>>>>> diff --git a/include/drm/ttm/ttm_tt.h b/include/drm/ttm/ttm_tt.h
>>>>> index cce57fb49e2c..069f8130241a 100644
>>>>> --- a/include/drm/ttm/ttm_tt.h
>>>>> +++ b/include/drm/ttm/ttm_tt.h
>>>>> @@ -30,6 +30,7 @@
>>>>> #include <linux/types.h>
>>>>> #include <drm/ttm/ttm_caching.h>
>>>>> +struct ttm_bo_device;
>>>>> struct ttm_tt;
>>>>> struct ttm_resource;
>>>>> struct ttm_buffer_object;
>> 
> 
> 
> 
> ----------
> 
> You're receiving this message because you're a member of the Linux-graphics-maintainer group from VMware, Inc..
> 
> Leave group:
> https://outlook.office365.com/owa/Linux-graphics-maintainer@vmware.com/groupsubscription.ashx?source=EscalatedMessage&action=leave&GuestId=69d3bf6f-5242-4be4-b863-b7949752f363
> _______________________________________________
> Sent to linux-graphics-maintainer@vmware.com

_______________________________________________
dri-devel mailing list
dri-devel@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/dri-devel

^ permalink raw reply	[flat|nested] 17+ messages in thread

* Re: [Linux-graphics-maintainer] [PATCH 2/3] drm/ttm: move memory accounting into vmwgfx v3
  2021-02-03  2:45           ` [Linux-graphics-maintainer] " Zack Rusin
@ 2021-02-03  8:20             ` Christian König
  2021-02-08 13:35               ` Christian König
  0 siblings, 1 reply; 17+ messages in thread
From: Christian König @ 2021-02-03  8:20 UTC (permalink / raw)
  To: Zack Rusin; +Cc: Linux-graphics-maintainer, dri-devel

Hi Zack,

thanks I can take over again from here on.

Quite busy today, but I think I can go over the code once more tomorrow.

Thanks for the help,
Christian.

Am 03.02.21 um 03:45 schrieb Zack Rusin:
> Just had a quick peek. The issue is that you can’t attach to the drm device (card0) because it hasn’t been registered yet (drm device registration is last in the vmw_probe in vmwgfx_drv.c via the drm_dev_register). So dev->primary->kdev->kobj that you’re using as argument to kobject_init_and_add in ttm_mem_global_init hasn’t been initialized yet. So that particular sysfs code would likely have to be refactored out of ttm_mem_global_init to another function that could be called after drm registraction. I could take this on but not until Friday or so.
>
> z
>
>
>> On Feb 2, 2021, at 12:42, Zack Rusin <zackr@vmware.com> wrote:
>>
>> Ah, yes, sorry, I missed that. I just double checked and it fails with:
>>
>> kobject_add_internal failed for memory_accounting (error: -2 parent: card0)
>>
>> which breaks the probe and the driver won’t load. I won’t have time to look into it until tomorrow though.
>>
>> z
>>
>>> On Feb 2, 2021, at 10:16, Christian König <christian.koenig@amd.com> wrote:
>>>
>>> Hi Zack,
>>>
>>> can you also give it a quick smoke test?
>>>
>>> I'm not sure if I wired up all the sysfs magic correctly inside vmwgfx, but I currently don't have a setup where I can test this.
>>>
>>> Thanks,
>>> Christian.
>>>
>>> Am 02.02.21 um 16:14 schrieb Zack Rusin:
>>>> Looks good. There’s probably not much reason to call it ttm_memory anymore as it only deals with ttm_mem_glob, we’ll likely fold it in after you submit. Thanks.
>>>>
>>>> Reviewed-by: Zack Rusin <zackr@vmware.com>
>>>>
>>>> z
>>>>
>>>>> On Feb 2, 2021, at 08:04, Christian König <christian.koenig@amd.com> wrote:
>>>>>
>>>>> Ping?
>>>>>
>>>>> Especially Roland and Zack do you have any objections to this?
>>>>>
>>>>> Regards,
>>>>> Christian.
>>>>>
>>>>> Am 28.01.21 um 14:16 schrieb Christian König:
>>>>>> This is just another feature which is only used by VMWGFX, so move
>>>>>> it into the driver instead.
>>>>>>
>>>>>> I've tried to add the accounting sysfs file to the kobject of the drm
>>>>>> minor, but I'm not 100% sure if this works as expected.
>>>>>>
>>>>>> v2: fix typo in KFD and avoid 64bit divide
>>>>>> v3: fix init order in VMWGFX
>>>>>>
>>>>>> Signed-off-by: Christian König <christian.koenig@amd.com>
>>>>>> ---
>>>>>> .../gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c  | 16 ++++++---
>>>>>> drivers/gpu/drm/amd/amdgpu/amdgpu_object.c    |  8 ++---
>>>>>> drivers/gpu/drm/drm_gem_vram_helper.c         |  6 ++--
>>>>>> drivers/gpu/drm/nouveau/nouveau_bo.c          |  7 ++--
>>>>>> drivers/gpu/drm/nouveau/nouveau_drv.h         |  1 -
>>>>>> drivers/gpu/drm/qxl/qxl_object.c              |  4 +--
>>>>>> drivers/gpu/drm/radeon/radeon_object.c        |  8 ++---
>>>>>> drivers/gpu/drm/ttm/Makefile                  |  7 ++--
>>>>>> drivers/gpu/drm/ttm/ttm_bo.c                  | 33 +------------------
>>>>>> drivers/gpu/drm/ttm/ttm_bo_util.c             |  1 -
>>>>>> drivers/gpu/drm/ttm/ttm_device.c              | 22 ++++++++++---
>>>>>> drivers/gpu/drm/ttm/ttm_pool.c                | 13 +-------
>>>>>> drivers/gpu/drm/vmwgfx/Makefile               |  2 +-
>>>>>> drivers/gpu/drm/{ttm => vmwgfx}/ttm_memory.c  | 19 ++++-------
>>>>>> .../gpu/drm/vmwgfx}/ttm_memory.h              |  5 +--
>>>>>> drivers/gpu/drm/vmwgfx/ttm_object.h           |  3 +-
>>>>>> drivers/gpu/drm/vmwgfx/vmwgfx_bo.c            | 22 ++++++++++---
>>>>>> drivers/gpu/drm/vmwgfx/vmwgfx_drv.c           |  5 +++
>>>>>> drivers/gpu/drm/vmwgfx/vmwgfx_ttm_buffer.c    | 28 +++++++++++++++-
>>>>>> include/drm/ttm/ttm_bo_api.h                  | 13 ++------
>>>>>> include/drm/ttm/ttm_bo_driver.h               |  1 -
>>>>>> include/drm/ttm/ttm_tt.h                      |  1 +
>>>>>> 22 files changed, 110 insertions(+), 115 deletions(-)
>>>>>> rename drivers/gpu/drm/{ttm => vmwgfx}/ttm_memory.c (97%)
>>>>>> rename {include/drm/ttm => drivers/gpu/drm/vmwgfx}/ttm_memory.h (97%)
>>>>>>
>>>>>> diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c b/drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c
>>>>>> index 0849b68e784f..e440af37dde8 100644
>>>>>> --- a/drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c
>>>>>> +++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c
>>>>>> @@ -118,6 +118,16 @@ void amdgpu_amdkfd_gpuvm_init_mem_limits(void)
>>>>>>   */
>>>>>> #define ESTIMATE_PT_SIZE(mem_size) ((mem_size) >> 14)
>>>>>> +static size_t amdgpu_amdkfd_acc_size(uint64_t size)
>>>>>> +{
>>>>>> +size >>= PAGE_SHIFT;
>>>>>> +size *= sizeof(dma_addr_t) + sizeof(void *);
>>>>>> +
>>>>>> +return __roundup_pow_of_two(sizeof(struct amdgpu_bo)) +
>>>>>> +__roundup_pow_of_two(sizeof(struct ttm_tt)) +
>>>>>> +PAGE_ALIGN(size);
>>>>>> +}
>>>>>> +
>>>>>> static int amdgpu_amdkfd_reserve_mem_limit(struct amdgpu_device *adev,
>>>>>> uint64_t size, u32 domain, bool sg)
>>>>>> {
>>>>>> @@ -126,8 +136,7 @@ static int amdgpu_amdkfd_reserve_mem_limit(struct amdgpu_device *adev,
>>>>>> size_t acc_size, system_mem_needed, ttm_mem_needed, vram_needed;
>>>>>> int ret = 0;
>>>>>> -acc_size = ttm_bo_dma_acc_size(&adev->mman.bdev, size,
>>>>>> -       sizeof(struct amdgpu_bo));
>>>>>> +acc_size = amdgpu_amdkfd_acc_size(size);
>>>>>>    vram_needed = 0;
>>>>>> if (domain == AMDGPU_GEM_DOMAIN_GTT) {
>>>>>> @@ -174,8 +183,7 @@ static void unreserve_mem_limit(struct amdgpu_device *adev,
>>>>>> {
>>>>>> size_t acc_size;
>>>>>> -acc_size = ttm_bo_dma_acc_size(&adev->mman.bdev, size,
>>>>>> -       sizeof(struct amdgpu_bo));
>>>>>> +acc_size = amdgpu_amdkfd_acc_size(size);
>>>>>>    spin_lock(&kfd_mem_limit.mem_limit_lock);
>>>>>> if (domain == AMDGPU_GEM_DOMAIN_GTT) {
>>>>>> diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_object.c b/drivers/gpu/drm/amd/amdgpu/amdgpu_object.c
>>>>>> index 6cc9919b12cc..599c9a132eb6 100644
>>>>>> --- a/drivers/gpu/drm/amd/amdgpu/amdgpu_object.c
>>>>>> +++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_object.c
>>>>>> @@ -523,7 +523,6 @@ static int amdgpu_bo_do_create(struct amdgpu_device *adev,
>>>>>> };
>>>>>> struct amdgpu_bo *bo;
>>>>>> unsigned long page_align, size = bp->size;
>>>>>> -size_t acc_size;
>>>>>> int r;
>>>>>>    /* Note that GDS/GWS/OA allocates 1 page per byte/resource. */
>>>>>> @@ -546,9 +545,6 @@ static int amdgpu_bo_do_create(struct amdgpu_device *adev,
>>>>>>    *bo_ptr = NULL;
>>>>>> -acc_size = ttm_bo_dma_acc_size(&adev->mman.bdev, size,
>>>>>> -       sizeof(struct amdgpu_bo));
>>>>>> -
>>>>>> bo = kzalloc(sizeof(struct amdgpu_bo), GFP_KERNEL);
>>>>>> if (bo == NULL)
>>>>>> return -ENOMEM;
>>>>>> @@ -577,8 +573,8 @@ static int amdgpu_bo_do_create(struct amdgpu_device *adev,
>>>>>> bo->tbo.priority = 1;
>>>>>>    r = ttm_bo_init_reserved(&adev->mman.bdev, &bo->tbo, size, bp->type,
>>>>>> - &bo->placement, page_align, &ctx, acc_size,
>>>>>> - NULL, bp->resv, &amdgpu_bo_destroy);
>>>>>> + &bo->placement, page_align, &ctx,  NULL,
>>>>>> + bp->resv, &amdgpu_bo_destroy);
>>>>>> if (unlikely(r != 0))
>>>>>> return r;
>>>>>> diff --git a/drivers/gpu/drm/drm_gem_vram_helper.c b/drivers/gpu/drm/drm_gem_vram_helper.c
>>>>>> index 0b13c8507688..a0992f0b8afd 100644
>>>>>> --- a/drivers/gpu/drm/drm_gem_vram_helper.c
>>>>>> +++ b/drivers/gpu/drm/drm_gem_vram_helper.c
>>>>>> @@ -189,7 +189,6 @@ struct drm_gem_vram_object *drm_gem_vram_create(struct drm_device *dev,
>>>>>> struct drm_vram_mm *vmm = dev->vram_mm;
>>>>>> struct ttm_device *bdev;
>>>>>> int ret;
>>>>>> -size_t acc_size;
>>>>>>    if (WARN_ONCE(!vmm, "VRAM MM not initialized"))
>>>>>> return ERR_PTR(-EINVAL);
>>>>>> @@ -216,7 +215,6 @@ struct drm_gem_vram_object *drm_gem_vram_create(struct drm_device *dev,
>>>>>> }
>>>>>>    bdev = &vmm->bdev;
>>>>>> -acc_size = ttm_bo_dma_acc_size(bdev, size, sizeof(*gbo));
>>>>>>    gbo->bo.bdev = bdev;
>>>>>> drm_gem_vram_placement(gbo, DRM_GEM_VRAM_PL_FLAG_SYSTEM);
>>>>>> @@ -226,8 +224,8 @@ struct drm_gem_vram_object *drm_gem_vram_create(struct drm_device *dev,
>>>>>>   * to release gbo->bo.base and kfree gbo.
>>>>>>   */
>>>>>> ret = ttm_bo_init(bdev, &gbo->bo, size, ttm_bo_type_device,
>>>>>> -  &gbo->placement, pg_align, false, acc_size,
>>>>>> -  NULL, NULL, ttm_buffer_object_destroy);
>>>>>> +  &gbo->placement, pg_align, false, NULL, NULL,
>>>>>> +  ttm_buffer_object_destroy);
>>>>>> if (ret)
>>>>>> return ERR_PTR(ret);
>>>>>> diff --git a/drivers/gpu/drm/nouveau/nouveau_bo.c b/drivers/gpu/drm/nouveau/nouveau_bo.c
>>>>>> index c177940d6e2c..ca2a8ae1938e 100644
>>>>>> --- a/drivers/gpu/drm/nouveau/nouveau_bo.c
>>>>>> +++ b/drivers/gpu/drm/nouveau/nouveau_bo.c
>>>>>> @@ -300,18 +300,15 @@ nouveau_bo_init(struct nouveau_bo *nvbo, u64 size, int align, u32 domain,
>>>>>> struct sg_table *sg, struct dma_resv *robj)
>>>>>> {
>>>>>> int type = sg ? ttm_bo_type_sg : ttm_bo_type_device;
>>>>>> -size_t acc_size;
>>>>>> int ret;
>>>>>> -acc_size = ttm_bo_dma_acc_size(nvbo->bo.bdev, size, sizeof(*nvbo));
>>>>>> -
>>>>>> nvbo->bo.mem.num_pages = size >> PAGE_SHIFT;
>>>>>> nouveau_bo_placement_set(nvbo, domain, 0);
>>>>>> INIT_LIST_HEAD(&nvbo->io_reserve_lru);
>>>>>>    ret = ttm_bo_init(nvbo->bo.bdev, &nvbo->bo, size, type,
>>>>>> -  &nvbo->placement, align >> PAGE_SHIFT, false,
>>>>>> -  acc_size, sg, robj, nouveau_bo_del_ttm);
>>>>>> +  &nvbo->placement, align >> PAGE_SHIFT, false, sg,
>>>>>> +  robj, nouveau_bo_del_ttm);
>>>>>> if (ret) {
>>>>>> /* ttm will call nouveau_bo_del_ttm if it fails.. */
>>>>>> return ret;
>>>>>> diff --git a/drivers/gpu/drm/nouveau/nouveau_drv.h b/drivers/gpu/drm/nouveau/nouveau_drv.h
>>>>>> index edf9d1ee9d58..a491c2c1c56e 100644
>>>>>> --- a/drivers/gpu/drm/nouveau/nouveau_drv.h
>>>>>> +++ b/drivers/gpu/drm/nouveau/nouveau_drv.h
>>>>>> @@ -54,7 +54,6 @@
>>>>>> #include <drm/ttm/ttm_bo_api.h>
>>>>>> #include <drm/ttm/ttm_bo_driver.h>
>>>>>> #include <drm/ttm/ttm_placement.h>
>>>>>> -#include <drm/ttm/ttm_memory.h>
>>>>>>    #include <drm/drm_audio_component.h>
>>>>>> diff --git a/drivers/gpu/drm/qxl/qxl_object.c b/drivers/gpu/drm/qxl/qxl_object.c
>>>>>> index ceebc5881f68..705b51535492 100644
>>>>>> --- a/drivers/gpu/drm/qxl/qxl_object.c
>>>>>> +++ b/drivers/gpu/drm/qxl/qxl_object.c
>>>>>> @@ -138,8 +138,8 @@ int qxl_bo_create(struct qxl_device *qdev,
>>>>>> qxl_ttm_placement_from_domain(bo, domain);
>>>>>>    r = ttm_bo_init_reserved(&qdev->mman.bdev, &bo->tbo, size, type,
>>>>>> - &bo->placement, 0, &ctx, size,
>>>>>> - NULL, NULL, &qxl_ttm_bo_destroy);
>>>>>> + &bo->placement, 0, &ctx, NULL, NULL,
>>>>>> + &qxl_ttm_bo_destroy);
>>>>>> if (unlikely(r != 0)) {
>>>>>> if (r != -ERESTARTSYS)
>>>>>> dev_err(qdev->ddev.dev,
>>>>>> diff --git a/drivers/gpu/drm/radeon/radeon_object.c b/drivers/gpu/drm/radeon/radeon_object.c
>>>>>> index 6a336284466f..804f7a427be7 100644
>>>>>> --- a/drivers/gpu/drm/radeon/radeon_object.c
>>>>>> +++ b/drivers/gpu/drm/radeon/radeon_object.c
>>>>>> @@ -159,7 +159,6 @@ int radeon_bo_create(struct radeon_device *rdev,
>>>>>> struct radeon_bo *bo;
>>>>>> enum ttm_bo_type type;
>>>>>> unsigned long page_align = roundup(byte_align, PAGE_SIZE) >> PAGE_SHIFT;
>>>>>> -size_t acc_size;
>>>>>> int r;
>>>>>>    size = ALIGN(size, PAGE_SIZE);
>>>>>> @@ -173,9 +172,6 @@ int radeon_bo_create(struct radeon_device *rdev,
>>>>>> }
>>>>>> *bo_ptr = NULL;
>>>>>> -acc_size = ttm_bo_dma_acc_size(&rdev->mman.bdev, size,
>>>>>> -       sizeof(struct radeon_bo));
>>>>>> -
>>>>>> bo = kzalloc(sizeof(struct radeon_bo), GFP_KERNEL);
>>>>>> if (bo == NULL)
>>>>>> return -ENOMEM;
>>>>>> @@ -230,8 +226,8 @@ int radeon_bo_create(struct radeon_device *rdev,
>>>>>> /* Kernel allocation are uninterruptible */
>>>>>> down_read(&rdev->pm.mclk_lock);
>>>>>> r = ttm_bo_init(&rdev->mman.bdev, &bo->tbo, size, type,
>>>>>> -&bo->placement, page_align, !kernel, acc_size,
>>>>>> -sg, resv, &radeon_ttm_bo_destroy);
>>>>>> +&bo->placement, page_align, !kernel, sg, resv,
>>>>>> +&radeon_ttm_bo_destroy);
>>>>>> up_read(&rdev->pm.mclk_lock);
>>>>>> if (unlikely(r != 0)) {
>>>>>> return r;
>>>>>> diff --git a/drivers/gpu/drm/ttm/Makefile b/drivers/gpu/drm/ttm/Makefile
>>>>>> index 8e6437eadabe..40e5e9da7953 100644
>>>>>> --- a/drivers/gpu/drm/ttm/Makefile
>>>>>> +++ b/drivers/gpu/drm/ttm/Makefile
>>>>>> @@ -2,10 +2,9 @@
>>>>>> #
>>>>>> # Makefile for the drm device driver.  This driver provides support for the
>>>>>> -ttm-y := ttm_memory.o ttm_tt.o ttm_bo.o \
>>>>>> -ttm_bo_util.o ttm_bo_vm.o ttm_module.o \
>>>>>> -ttm_execbuf_util.o ttm_range_manager.o \
>>>>>> -ttm_resource.o ttm_pool.o ttm_device.o
>>>>>> +ttm-y := ttm_tt.o ttm_bo.o ttm_bo_util.o ttm_bo_vm.o ttm_module.o \
>>>>>> +ttm_execbuf_util.o ttm_range_manager.o ttm_resource.o ttm_pool.o \
>>>>>> +ttm_device.o
>>>>>> ttm-$(CONFIG_AGP) += ttm_agp_backend.o
>>>>>>    obj-$(CONFIG_DRM_TTM) += ttm.o
>>>>>> diff --git a/drivers/gpu/drm/ttm/ttm_bo.c b/drivers/gpu/drm/ttm/ttm_bo.c
>>>>>> index 643befc1a6f2..e38102282fd5 100644
>>>>>> --- a/drivers/gpu/drm/ttm/ttm_bo.c
>>>>>> +++ b/drivers/gpu/drm/ttm/ttm_bo.c
>>>>>> @@ -425,7 +425,6 @@ static void ttm_bo_release(struct kref *kref)
>>>>>> struct ttm_buffer_object *bo =
>>>>>>      container_of(kref, struct ttm_buffer_object, kref);
>>>>>> struct ttm_device *bdev = bo->bdev;
>>>>>> -size_t acc_size = bo->acc_size;
>>>>>> int ret;
>>>>>>    if (!bo->deleted) {
>>>>>> @@ -485,7 +484,6 @@ static void ttm_bo_release(struct kref *kref)
>>>>>> if (!ttm_bo_uses_embedded_gem_object(bo))
>>>>>> dma_resv_fini(&bo->base._resv);
>>>>>> bo->destroy(bo);
>>>>>> -ttm_mem_global_free(&ttm_mem_glob, acc_size);
>>>>>> }
>>>>>>    void ttm_bo_put(struct ttm_buffer_object *bo)
>>>>>> @@ -1046,25 +1044,13 @@ int ttm_bo_init_reserved(struct ttm_device *bdev,
>>>>>>   struct ttm_placement *placement,
>>>>>>   uint32_t page_alignment,
>>>>>>   struct ttm_operation_ctx *ctx,
>>>>>> - size_t acc_size,
>>>>>>   struct sg_table *sg,
>>>>>>   struct dma_resv *resv,
>>>>>>   void (*destroy) (struct ttm_buffer_object *))
>>>>>> {
>>>>>> -struct ttm_mem_global *mem_glob = &ttm_mem_glob;
>>>>>> bool locked;
>>>>>> int ret = 0;
>>>>>> -ret = ttm_mem_global_alloc(mem_glob, acc_size, ctx);
>>>>>> -if (ret) {
>>>>>> -pr_err("Out of kernel memory\n");
>>>>>> -if (destroy)
>>>>>> -(*destroy)(bo);
>>>>>> -else
>>>>>> -kfree(bo);
>>>>>> -return -ENOMEM;
>>>>>> -}
>>>>>> -
>>>>>> bo->destroy = destroy ? destroy : ttm_bo_default_destroy;
>>>>>>    kref_init(&bo->kref);
>>>>>> @@ -1081,7 +1067,6 @@ int ttm_bo_init_reserved(struct ttm_device *bdev,
>>>>>> bo->mem.bus.addr = NULL;
>>>>>> bo->moving = NULL;
>>>>>> bo->mem.placement = 0;
>>>>>> -bo->acc_size = acc_size;
>>>>>> bo->pin_count = 0;
>>>>>> bo->sg = sg;
>>>>>> if (resv) {
>>>>>> @@ -1142,7 +1127,6 @@ int ttm_bo_init(struct ttm_device *bdev,
>>>>>> struct ttm_placement *placement,
>>>>>> uint32_t page_alignment,
>>>>>> bool interruptible,
>>>>>> -size_t acc_size,
>>>>>> struct sg_table *sg,
>>>>>> struct dma_resv *resv,
>>>>>> void (*destroy) (struct ttm_buffer_object *))
>>>>>> @@ -1151,8 +1135,7 @@ int ttm_bo_init(struct ttm_device *bdev,
>>>>>> int ret;
>>>>>>    ret = ttm_bo_init_reserved(bdev, bo, size, type, placement,
>>>>>> -   page_alignment, &ctx, acc_size,
>>>>>> -   sg, resv, destroy);
>>>>>> +   page_alignment, &ctx, sg, resv, destroy);
>>>>>> if (ret)
>>>>>> return ret;
>>>>>> @@ -1163,20 +1146,6 @@ int ttm_bo_init(struct ttm_device *bdev,
>>>>>> }
>>>>>> EXPORT_SYMBOL(ttm_bo_init);
>>>>>> -size_t ttm_bo_dma_acc_size(struct ttm_device *bdev,
>>>>>> -   unsigned long bo_size,
>>>>>> -   unsigned struct_size)
>>>>>> -{
>>>>>> -unsigned npages = (PAGE_ALIGN(bo_size)) >> PAGE_SHIFT;
>>>>>> -size_t size = 0;
>>>>>> -
>>>>>> -size += ttm_round_pot(struct_size);
>>>>>> -size += ttm_round_pot(npages * (2*sizeof(void *) + sizeof(dma_addr_t)));
>>>>>> -size += ttm_round_pot(sizeof(struct ttm_tt));
>>>>>> -return size;
>>>>>> -}
>>>>>> -EXPORT_SYMBOL(ttm_bo_dma_acc_size);
>>>>>> -
>>>>>> /*
>>>>>>   * buffer object vm functions.
>>>>>>   */
>>>>>> diff --git a/drivers/gpu/drm/ttm/ttm_bo_util.c b/drivers/gpu/drm/ttm/ttm_bo_util.c
>>>>>> index db0f2661d504..031e5819fec4 100644
>>>>>> --- a/drivers/gpu/drm/ttm/ttm_bo_util.c
>>>>>> +++ b/drivers/gpu/drm/ttm/ttm_bo_util.c
>>>>>> @@ -309,7 +309,6 @@ static int ttm_buffer_object_transfer(struct ttm_buffer_object *bo,
>>>>>>    kref_init(&fbo->base.kref);
>>>>>> fbo->base.destroy = &ttm_transfered_destroy;
>>>>>> -fbo->base.acc_size = 0;
>>>>>> fbo->base.pin_count = 0;
>>>>>> if (bo->type != ttm_bo_type_sg)
>>>>>> fbo->base.base.resv = &fbo->base.base._resv;
>>>>>> diff --git a/drivers/gpu/drm/ttm/ttm_device.c b/drivers/gpu/drm/ttm/ttm_device.c
>>>>>> index ac0903c9e60a..6bde344e5da7 100644
>>>>>> --- a/drivers/gpu/drm/ttm/ttm_device.c
>>>>>> +++ b/drivers/gpu/drm/ttm/ttm_device.c
>>>>>> @@ -27,9 +27,12 @@
>>>>>>    #define pr_fmt(fmt) "[TTM DEVICE] " fmt
>>>>>> +#include <linux/mm.h>
>>>>>> +
>>>>>> #include <drm/ttm/ttm_device.h>
>>>>>> -#include <drm/ttm/ttm_memory.h>
>>>>>> +#include <drm/ttm/ttm_tt.h>
>>>>>> #include <drm/ttm/ttm_placement.h>
>>>>>> +#include <drm/ttm/ttm_bo_api.h>
>>>>>>    #include "ttm_module.h"
>>>>>> @@ -49,9 +52,11 @@ static void ttm_global_release(void)
>>>>>> if (--ttm_glob_use_count > 0)
>>>>>> goto out;
>>>>>> +ttm_pool_mgr_fini();
>>>>>> +ttm_tt_mgr_fini();
>>>>>> +
>>>>>> kobject_del(&glob->kobj);
>>>>>> kobject_put(&glob->kobj);
>>>>>> -ttm_mem_global_release(&ttm_mem_glob);
>>>>>> __free_page(glob->dummy_read_page);
>>>>>> memset(glob, 0, sizeof(*glob));
>>>>>> out:
>>>>>> @@ -61,6 +66,8 @@ static void ttm_global_release(void)
>>>>>> static int ttm_global_init(void)
>>>>>> {
>>>>>> struct ttm_global *glob = &ttm_glob;
>>>>>> +unsigned long num_pages;
>>>>>> +struct sysinfo si;
>>>>>> int ret = 0;
>>>>>> unsigned i;
>>>>>> @@ -68,9 +75,14 @@ static int ttm_global_init(void)
>>>>>> if (++ttm_glob_use_count > 1)
>>>>>> goto out;
>>>>>> -ret = ttm_mem_global_init(&ttm_mem_glob);
>>>>>> -if (ret)
>>>>>> -goto out;
>>>>>> +si_meminfo(&si);
>>>>>> +
>>>>>> +/* Limit the number of pages in the pool to about 50% of the total
>>>>>> + * system memory.
>>>>>> + */
>>>>>> +num_pages = ((u64)si.totalram * si.mem_unit) >> PAGE_SHIFT;
>>>>>> +ttm_pool_mgr_init(num_pages * 50 / 100);
>>>>>> +ttm_tt_mgr_init();
>>>>>>    spin_lock_init(&glob->lru_lock);
>>>>>> glob->dummy_read_page = alloc_page(__GFP_ZERO | GFP_DMA32);
>>>>>> diff --git a/drivers/gpu/drm/ttm/ttm_pool.c b/drivers/gpu/drm/ttm/ttm_pool.c
>>>>>> index e0617717113f..6b0f957d63d5 100644
>>>>>> --- a/drivers/gpu/drm/ttm/ttm_pool.c
>>>>>> +++ b/drivers/gpu/drm/ttm/ttm_pool.c
>>>>>> @@ -404,16 +404,10 @@ int ttm_pool_alloc(struct ttm_pool *pool, struct ttm_tt *tt,
>>>>>> caching = pages + (1 << order);
>>>>>> }
>>>>>> -r = ttm_mem_global_alloc_page(&ttm_mem_glob, p,
>>>>>> -      (1 << order) * PAGE_SIZE,
>>>>>> -      ctx);
>>>>>> -if (r)
>>>>>> -goto error_free_page;
>>>>>> -
>>>>>> if (dma_addr) {
>>>>>> r = ttm_pool_map(pool, order, p, &dma_addr);
>>>>>> if (r)
>>>>>> -goto error_global_free;
>>>>>> +goto error_free_page;
>>>>>> }
>>>>>>    num_pages -= 1 << order;
>>>>>> @@ -427,9 +421,6 @@ int ttm_pool_alloc(struct ttm_pool *pool, struct ttm_tt *tt,
>>>>>>    return 0;
>>>>>> -error_global_free:
>>>>>> -ttm_mem_global_free_page(&ttm_mem_glob, p, (1 << order) * PAGE_SIZE);
>>>>>> -
>>>>>> error_free_page:
>>>>>> ttm_pool_free_page(pool, tt->caching, order, p);
>>>>>> @@ -464,8 +455,6 @@ void ttm_pool_free(struct ttm_pool *pool, struct ttm_tt *tt)
>>>>>>    order = ttm_pool_page_order(pool, p);
>>>>>> num_pages = 1ULL << order;
>>>>>> -ttm_mem_global_free_page(&ttm_mem_glob, p,
>>>>>> - num_pages * PAGE_SIZE);
>>>>>> if (tt->dma_address)
>>>>>> ttm_pool_unmap(pool, tt->dma_address[i], num_pages);
>>>>>> diff --git a/drivers/gpu/drm/vmwgfx/Makefile b/drivers/gpu/drm/vmwgfx/Makefile
>>>>>> index cc4cdca7176e..8c02fa5852e7 100644
>>>>>> --- a/drivers/gpu/drm/vmwgfx/Makefile
>>>>>> +++ b/drivers/gpu/drm/vmwgfx/Makefile
>>>>>> @@ -9,7 +9,7 @@ vmwgfx-y := vmwgfx_execbuf.o vmwgfx_gmr.o vmwgfx_kms.o vmwgfx_drv.o \
>>>>>>      vmwgfx_cotable.o vmwgfx_so.o vmwgfx_binding.o vmwgfx_msg.o \
>>>>>>      vmwgfx_simple_resource.o vmwgfx_va.o vmwgfx_blit.o \
>>>>>>      vmwgfx_validation.o vmwgfx_page_dirty.o vmwgfx_streamoutput.o \
>>>>>> -    ttm_object.o ttm_lock.o
>>>>>> +    ttm_object.o ttm_lock.o ttm_memory.o
>>>>>>    vmwgfx-$(CONFIG_TRANSPARENT_HUGEPAGE) += vmwgfx_thp.o
>>>>>> obj-$(CONFIG_DRM_VMWGFX) := vmwgfx.o
>>>>>> diff --git a/drivers/gpu/drm/ttm/ttm_memory.c b/drivers/gpu/drm/vmwgfx/ttm_memory.c
>>>>>> similarity index 97%
>>>>>> rename from drivers/gpu/drm/ttm/ttm_memory.c
>>>>>> rename to drivers/gpu/drm/vmwgfx/ttm_memory.c
>>>>>> index 634a85c2dc4c..1306d9e0f095 100644
>>>>>> --- a/drivers/gpu/drm/ttm/ttm_memory.c
>>>>>> +++ b/drivers/gpu/drm/vmwgfx/ttm_memory.c
>>>>>> @@ -28,7 +28,6 @@
>>>>>>    #define pr_fmt(fmt) "[TTM] " fmt
>>>>>> -#include <drm/ttm/ttm_memory.h>
>>>>>> #include <linux/spinlock.h>
>>>>>> #include <linux/sched.h>
>>>>>> #include <linux/wait.h>
>>>>>> @@ -36,10 +35,11 @@
>>>>>> #include <linux/module.h>
>>>>>> #include <linux/slab.h>
>>>>>> #include <linux/swap.h>
>>>>>> -#include <drm/ttm/ttm_pool.h>
>>>>>> -#include <drm/ttm/ttm_tt.h>
>>>>>> -#include "ttm_module.h"
>>>>>> +#include <drm/drm_device.h>
>>>>>> +#include <drm/drm_file.h>
>>>>>> +
>>>>>> +#include "ttm_memory.h"
>>>>>>    #define TTM_MEMORY_ALLOC_RETRIES 4
>>>>>> @@ -414,7 +414,7 @@ static int ttm_mem_init_dma32_zone(struct ttm_mem_global *glob,
>>>>>> }
>>>>>> #endif
>>>>>> -int ttm_mem_global_init(struct ttm_mem_global *glob)
>>>>>> +int ttm_mem_global_init(struct ttm_mem_global *glob, struct drm_device *dev)
>>>>>> {
>>>>>> struct sysinfo si;
>>>>>> int ret;
>>>>>> @@ -425,7 +425,8 @@ int ttm_mem_global_init(struct ttm_mem_global *glob)
>>>>>> glob->swap_queue = create_singlethread_workqueue("ttm_swap");
>>>>>> INIT_WORK(&glob->work, ttm_shrink_work);
>>>>>> ret = kobject_init_and_add(
>>>>>> -&glob->kobj, &ttm_mem_glob_kobj_type, ttm_get_kobj(), "memory_accounting");
>>>>>> +&glob->kobj, &ttm_mem_glob_kobj_type, &dev->primary->kdev->kobj,
>>>>>> +"memory_accounting");
>>>>>> if (unlikely(ret != 0)) {
>>>>>> kobject_put(&glob->kobj);
>>>>>> return ret;
>>>>>> @@ -453,8 +454,6 @@ int ttm_mem_global_init(struct ttm_mem_global *glob)
>>>>>> pr_info("Zone %7s: Available graphics memory: %llu KiB\n",
>>>>>> zone->name, (unsigned long long)zone->max_mem >> 10);
>>>>>> }
>>>>>> -ttm_pool_mgr_init(glob->zone_kernel->max_mem/(2*PAGE_SIZE));
>>>>>> -ttm_tt_mgr_init();
>>>>>> return 0;
>>>>>> out_no_zone:
>>>>>> ttm_mem_global_release(glob);
>>>>>> @@ -466,10 +465,6 @@ void ttm_mem_global_release(struct ttm_mem_global *glob)
>>>>>> struct ttm_mem_zone *zone;
>>>>>> unsigned int i;
>>>>>> -/* let the page allocator first stop the shrink work. */
>>>>>> -ttm_pool_mgr_fini();
>>>>>> -ttm_tt_mgr_fini();
>>>>>> -
>>>>>> flush_workqueue(glob->swap_queue);
>>>>>> destroy_workqueue(glob->swap_queue);
>>>>>> glob->swap_queue = NULL;
>>>>>> diff --git a/include/drm/ttm/ttm_memory.h b/drivers/gpu/drm/vmwgfx/ttm_memory.h
>>>>>> similarity index 97%
>>>>>> rename from include/drm/ttm/ttm_memory.h
>>>>>> rename to drivers/gpu/drm/vmwgfx/ttm_memory.h
>>>>>> index c1f167881e33..850ee6c867da 100644
>>>>>> --- a/include/drm/ttm/ttm_memory.h
>>>>>> +++ b/drivers/gpu/drm/vmwgfx/ttm_memory.h
>>>>>> @@ -35,7 +35,8 @@
>>>>>> #include <linux/errno.h>
>>>>>> #include <linux/kobject.h>
>>>>>> #include <linux/mm.h>
>>>>>> -#include "ttm_bo_api.h"
>>>>>> +
>>>>>> +#include <drm/ttm/ttm_bo_api.h>
>>>>>>    /**
>>>>>>   * struct ttm_mem_global - Global memory accounting structure.
>>>>>> @@ -79,7 +80,7 @@ extern struct ttm_mem_global {
>>>>>> #endif
>>>>>> } ttm_mem_glob;
>>>>>> -int ttm_mem_global_init(struct ttm_mem_global *glob);
>>>>>> +int ttm_mem_global_init(struct ttm_mem_global *glob, struct drm_device *dev);
>>>>>> void ttm_mem_global_release(struct ttm_mem_global *glob);
>>>>>> int ttm_mem_global_alloc(struct ttm_mem_global *glob, uint64_t memory,
>>>>>>   struct ttm_operation_ctx *ctx);
>>>>>> diff --git a/drivers/gpu/drm/vmwgfx/ttm_object.h b/drivers/gpu/drm/vmwgfx/ttm_object.h
>>>>>> index ede26df87c93..49b064f0cb19 100644
>>>>>> --- a/drivers/gpu/drm/vmwgfx/ttm_object.h
>>>>>> +++ b/drivers/gpu/drm/vmwgfx/ttm_object.h
>>>>>> @@ -43,7 +43,8 @@
>>>>>> #include <linux/rcupdate.h>
>>>>>>    #include <drm/drm_hashtab.h>
>>>>>> -#include <drm/ttm/ttm_memory.h>
>>>>>> +
>>>>>> +#include "ttm_memory.h"
>>>>>>    /**
>>>>>>   * enum ttm_ref_type
>>>>>> diff --git a/drivers/gpu/drm/vmwgfx/vmwgfx_bo.c b/drivers/gpu/drm/vmwgfx/vmwgfx_bo.c
>>>>>> index 6b3bfd8c678a..50e529a01677 100644
>>>>>> --- a/drivers/gpu/drm/vmwgfx/vmwgfx_bo.c
>>>>>> +++ b/drivers/gpu/drm/vmwgfx/vmwgfx_bo.c
>>>>>> @@ -507,11 +507,16 @@ int vmw_bo_create_kernel(struct vmw_private *dev_priv, unsigned long size,
>>>>>> acc_size = ttm_round_pot(sizeof(*bo));
>>>>>> acc_size += ttm_round_pot(npages * sizeof(void *));
>>>>>> acc_size += ttm_round_pot(sizeof(struct ttm_tt));
>>>>>> +
>>>>>> +ret = ttm_mem_global_alloc(&ttm_mem_glob, acc_size, &ctx);
>>>>>> +if (unlikely(ret))
>>>>>> +goto error_free;
>>>>>> +
>>>>>> ret = ttm_bo_init_reserved(&dev_priv->bdev, bo, size,
>>>>>>     ttm_bo_type_device, placement, 0,
>>>>>> -   &ctx, acc_size, NULL, NULL, NULL);
>>>>>> +   &ctx, NULL, NULL, NULL);
>>>>>> if (unlikely(ret))
>>>>>> -goto error_free;
>>>>>> +goto error_account;
>>>>>>    ttm_bo_pin(bo);
>>>>>> ttm_bo_unreserve(bo);
>>>>>> @@ -519,6 +524,9 @@ int vmw_bo_create_kernel(struct vmw_private *dev_priv, unsigned long size,
>>>>>>    return 0;
>>>>>> +error_account:
>>>>>> +ttm_mem_global_free(&ttm_mem_glob, acc_size);
>>>>>> +
>>>>>> error_free:
>>>>>> kfree(bo);
>>>>>> return ret;
>>>>>> @@ -558,11 +566,17 @@ int vmw_bo_init(struct vmw_private *dev_priv,
>>>>>> vmw_bo->base.priority = 3;
>>>>>> vmw_bo->res_tree = RB_ROOT;
>>>>>> +ret = ttm_mem_global_alloc(&ttm_mem_glob, acc_size, &ctx);
>>>>>> +if (unlikely(ret))
>>>>>> +return ret;
>>>>>> +
>>>>>> ret = ttm_bo_init_reserved(bdev, &vmw_bo->base, size,
>>>>>>     ttm_bo_type_device, placement,
>>>>>> -   0, &ctx, acc_size, NULL, NULL, bo_free);
>>>>>> -if (unlikely(ret))
>>>>>> +   0, &ctx, NULL, NULL, bo_free);
>>>>>> +if (unlikely(ret)) {
>>>>>> +ttm_mem_global_free(&ttm_mem_glob, acc_size);
>>>>>> return ret;
>>>>>> +}
>>>>>>    if (pin)
>>>>>> ttm_bo_pin(&vmw_bo->base);
>>>>>> diff --git a/drivers/gpu/drm/vmwgfx/vmwgfx_drv.c b/drivers/gpu/drm/vmwgfx/vmwgfx_drv.c
>>>>>> index 710ba5169a74..6c0ca1011629 100644
>>>>>> --- a/drivers/gpu/drm/vmwgfx/vmwgfx_drv.c
>>>>>> +++ b/drivers/gpu/drm/vmwgfx/vmwgfx_drv.c
>>>>>> @@ -1268,6 +1268,7 @@ static void vmw_remove(struct pci_dev *pdev)
>>>>>> {
>>>>>> struct drm_device *dev = pci_get_drvdata(pdev);
>>>>>> +ttm_mem_global_release(&ttm_mem_glob);
>>>>>> drm_dev_unregister(dev);
>>>>>> vmw_driver_unload(dev);
>>>>>> }
>>>>>> @@ -1518,6 +1519,10 @@ static int vmw_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
>>>>>>    pci_set_drvdata(pdev, &vmw->drm);
>>>>>> +ret = ttm_mem_global_init(&ttm_mem_glob, &vmw->drm);
>>>>>> +if (ret)
>>>>>> +return ret;
>>>>>> +
>>>>>> ret = vmw_driver_load(vmw, ent->device);
>>>>>> if (ret)
>>>>>> return ret;
>>>>>> diff --git a/drivers/gpu/drm/vmwgfx/vmwgfx_ttm_buffer.c b/drivers/gpu/drm/vmwgfx/vmwgfx_ttm_buffer.c
>>>>>> index d1bfa59579f1..63f10c865061 100644
>>>>>> --- a/drivers/gpu/drm/vmwgfx/vmwgfx_ttm_buffer.c
>>>>>> +++ b/drivers/gpu/drm/vmwgfx/vmwgfx_ttm_buffer.c
>>>>>> @@ -576,11 +576,31 @@ static void vmw_ttm_destroy(struct ttm_device *bdev, struct ttm_tt *ttm)
>>>>>> static int vmw_ttm_populate(struct ttm_device *bdev,
>>>>>>      struct ttm_tt *ttm, struct ttm_operation_ctx *ctx)
>>>>>> {
>>>>>> +unsigned int i;
>>>>>> +int ret;
>>>>>> +
>>>>>> /* TODO: maybe completely drop this ? */
>>>>>> if (ttm_tt_is_populated(ttm))
>>>>>> return 0;
>>>>>> -return ttm_pool_alloc(&bdev->pool, ttm, ctx);
>>>>>> +ret = ttm_pool_alloc(&bdev->pool, ttm, ctx);
>>>>>> +if (ret)
>>>>>> +return ret;
>>>>>> +
>>>>>> +for (i = 0; i < ttm->num_pages; ++i) {
>>>>>> +ret = ttm_mem_global_alloc_page(&ttm_mem_glob, ttm->pages[i],
>>>>>> +PAGE_SIZE, ctx);
>>>>>> +if (ret)
>>>>>> +goto error;
>>>>>> +}
>>>>>> +return 0;
>>>>>> +
>>>>>> +error:
>>>>>> +while (i--)
>>>>>> +ttm_mem_global_free_page(&ttm_mem_glob, ttm->pages[i],
>>>>>> + PAGE_SIZE);
>>>>>> +ttm_pool_free(&bdev->pool, ttm);
>>>>>> +return ret;
>>>>>> }
>>>>>>    static void vmw_ttm_unpopulate(struct ttm_device *bdev,
>>>>>> @@ -588,6 +608,7 @@ static void vmw_ttm_unpopulate(struct ttm_device *bdev,
>>>>>> {
>>>>>> struct vmw_ttm_tt *vmw_tt = container_of(ttm, struct vmw_ttm_tt,
>>>>>>   dma_ttm);
>>>>>> +unsigned int i;
>>>>>>    if (vmw_tt->mob) {
>>>>>> vmw_mob_destroy(vmw_tt->mob);
>>>>>> @@ -595,6 +616,11 @@ static void vmw_ttm_unpopulate(struct ttm_device *bdev,
>>>>>> }
>>>>>>    vmw_ttm_unmap_dma(vmw_tt);
>>>>>> +
>>>>>> +for (i = 0; i < ttm->num_pages; ++i)
>>>>>> +ttm_mem_global_free_page(&ttm_mem_glob, ttm->pages[i],
>>>>>> + PAGE_SIZE);
>>>>>> +
>>>>>> ttm_pool_free(&bdev->pool, ttm);
>>>>>> }
>>>>>> diff --git a/include/drm/ttm/ttm_bo_api.h b/include/drm/ttm/ttm_bo_api.h
>>>>>> index 1297a8fb7ccb..4fb523dfab32 100644
>>>>>> --- a/include/drm/ttm/ttm_bo_api.h
>>>>>> +++ b/include/drm/ttm/ttm_bo_api.h
>>>>>> @@ -88,7 +88,6 @@ struct ttm_tt;
>>>>>>   * @type: The bo type.
>>>>>>   * @destroy: Destruction function. If NULL, kfree is used.
>>>>>>   * @num_pages: Actual number of pages.
>>>>>> - * @acc_size: Accounted size for this object.
>>>>>>   * @kref: Reference count of this buffer object. When this refcount reaches
>>>>>>   * zero, the object is destroyed or put on the delayed delete list.
>>>>>>   * @mem: structure describing current placement.
>>>>>> @@ -125,7 +124,6 @@ struct ttm_buffer_object {
>>>>>> struct ttm_device *bdev;
>>>>>> enum ttm_bo_type type;
>>>>>> void (*destroy) (struct ttm_buffer_object *);
>>>>>> -size_t acc_size;
>>>>>>    /**
>>>>>> * Members not needing protection.
>>>>>> @@ -357,10 +355,6 @@ void ttm_bo_unlock_delayed_workqueue(struct ttm_device *bdev, int resched);
>>>>>> bool ttm_bo_eviction_valuable(struct ttm_buffer_object *bo,
>>>>>>        const struct ttm_place *place);
>>>>>> -size_t ttm_bo_dma_acc_size(struct ttm_device *bdev,
>>>>>> -   unsigned long bo_size,
>>>>>> -   unsigned struct_size);
>>>>>> -
>>>>>> /**
>>>>>>   * ttm_bo_init_reserved
>>>>>>   *
>>>>>> @@ -371,7 +365,6 @@ size_t ttm_bo_dma_acc_size(struct ttm_device *bdev,
>>>>>>   * @flags: Initial placement flags.
>>>>>>   * @page_alignment: Data alignment in pages.
>>>>>>   * @ctx: TTM operation context for memory allocation.
>>>>>> - * @acc_size: Accounted size for this object.
>>>>>>   * @resv: Pointer to a dma_resv, or NULL to let ttm allocate one.
>>>>>>   * @destroy: Destroy function. Use NULL for kfree().
>>>>>>   *
>>>>>> @@ -402,8 +395,7 @@ int ttm_bo_init_reserved(struct ttm_device *bdev,
>>>>>>   struct ttm_placement *placement,
>>>>>>   uint32_t page_alignment,
>>>>>>   struct ttm_operation_ctx *ctx,
>>>>>> - size_t acc_size, struct sg_table *sg,
>>>>>> - struct dma_resv *resv,
>>>>>> + struct sg_table *sg, struct dma_resv *resv,
>>>>>>   void (*destroy) (struct ttm_buffer_object *));
>>>>>>    /**
>>>>>> @@ -421,7 +413,6 @@ int ttm_bo_init_reserved(struct ttm_device *bdev,
>>>>>>   * holds a pointer to a persistent shmem object. Typically, this would
>>>>>>   * point to the shmem object backing a GEM object if TTM is used to back a
>>>>>>   * GEM user interface.
>>>>>> - * @acc_size: Accounted size for this object.
>>>>>>   * @resv: Pointer to a dma_resv, or NULL to let ttm allocate one.
>>>>>>   * @destroy: Destroy function. Use NULL for kfree().
>>>>>>   *
>>>>>> @@ -446,7 +437,7 @@ int ttm_bo_init_reserved(struct ttm_device *bdev,
>>>>>> int ttm_bo_init(struct ttm_device *bdev, struct ttm_buffer_object *bo,
>>>>>> size_t size, enum ttm_bo_type type,
>>>>>> struct ttm_placement *placement,
>>>>>> -uint32_t page_alignment, bool interrubtible, size_t acc_size,
>>>>>> +uint32_t page_alignment, bool interrubtible,
>>>>>> struct sg_table *sg, struct dma_resv *resv,
>>>>>> void (*destroy) (struct ttm_buffer_object *));
>>>>>> diff --git a/include/drm/ttm/ttm_bo_driver.h b/include/drm/ttm/ttm_bo_driver.h
>>>>>> index 1c9bf993e252..8959c0075cfd 100644
>>>>>> --- a/include/drm/ttm/ttm_bo_driver.h
>>>>>> +++ b/include/drm/ttm/ttm_bo_driver.h
>>>>>> @@ -40,7 +40,6 @@
>>>>>> #include <drm/ttm/ttm_device.h>
>>>>>>    #include "ttm_bo_api.h"
>>>>>> -#include "ttm_memory.h"
>>>>>> #include "ttm_placement.h"
>>>>>> #include "ttm_tt.h"
>>>>>> #include "ttm_pool.h"
>>>>>> diff --git a/include/drm/ttm/ttm_tt.h b/include/drm/ttm/ttm_tt.h
>>>>>> index cce57fb49e2c..069f8130241a 100644
>>>>>> --- a/include/drm/ttm/ttm_tt.h
>>>>>> +++ b/include/drm/ttm/ttm_tt.h
>>>>>> @@ -30,6 +30,7 @@
>>>>>> #include <linux/types.h>
>>>>>> #include <drm/ttm/ttm_caching.h>
>>>>>> +struct ttm_bo_device;
>>>>>> struct ttm_tt;
>>>>>> struct ttm_resource;
>>>>>> struct ttm_buffer_object;
>>
>>
>> ----------
>>
>> You're receiving this message because you're a member of the Linux-graphics-maintainer group from VMware, Inc..
>>
>> Leave group:
>> https://nam11.safelinks.protection.outlook.com/?url=https%3A%2F%2Foutlook.office365.com%2Fowa%2FLinux-graphics-maintainer%40vmware.com%2Fgroupsubscription.ashx%3Fsource%3DEscalatedMessage%26action%3Dleave%26GuestId%3D69d3bf6f-5242-4be4-b863-b7949752f363&amp;data=04%7C01%7Cchristian.koenig%40amd.com%7Cccf900e6d23648ef0b0808d8c7edb760%7C3dd8961fe4884e608e11a82d994e183d%7C0%7C0%7C637479171103384072%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C1000&amp;sdata=f8Y%2ByzBnt9Gxxmm7XZ3hpZqTdbY05og9yloArLCLIx0%3D&amp;reserved=0
>> _______________________________________________
>> Sent to linux-graphics-maintainer@vmware.com

_______________________________________________
dri-devel mailing list
dri-devel@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/dri-devel

^ permalink raw reply	[flat|nested] 17+ messages in thread

* Re: [PATCH 1/3] drm/ttm: rework ttm_tt page limit v3
  2021-01-28 13:16 [PATCH 1/3] drm/ttm: rework ttm_tt page limit v3 Christian König
  2021-01-28 13:16 ` [PATCH 2/3] drm/ttm: move memory accounting into vmwgfx v3 Christian König
  2021-01-28 13:16 ` [PATCH 3/3] drm/ttm: drop sysfs directory Christian König
@ 2021-02-03 11:26 ` Daniel Vetter
  2021-02-03 12:18   ` Christian König
  2 siblings, 1 reply; 17+ messages in thread
From: Daniel Vetter @ 2021-02-03 11:26 UTC (permalink / raw)
  To: Christian König; +Cc: linux-graphics-maintainer, dri-devel, sroland

On Thu, Jan 28, 2021 at 02:16:02PM +0100, Christian König wrote:
> TTM implements a rather extensive accounting of allocated memory.
> 
> There are two reasons for this:
> 1. It tries to block userspace allocating a huge number of very small
>    BOs without accounting for the kmalloced memory.
> 
> 2. Make sure we don't over allocate and run into an OOM situation
>    during swapout while trying to handle the memory shortage.
> 
> This is only partially a good idea. First of all it is perfectly
> valid for an application to use all of system memory, limiting it to
> 50% is not really acceptable.
> 
> What we need to take care of is that the application is held
> accountable for the memory it allocated. This is what control
> mechanisms like memcg and the normal Linux page accounting already do.
> 
> Making sure that we don't run into an OOM situation while trying to
> cope with a memory shortage is still a good idea, but this is also
> not very well implemented since it means another opportunity of
> recursion from the driver back into TTM.
> 
> So start to rework all of this by implementing a shrinker callback which
> allows for TT object to be swapped out if necessary.
> 
> v2: Switch from limit to shrinker callback.
> v3: fix gfp mask handling, use atomic for swapable_pages, add debugfs
> 
> Signed-off-by: Christian König <christian.koenig@amd.com>
> ---
>  drivers/gpu/drm/ttm/ttm_bo.c        |   4 +-
>  drivers/gpu/drm/ttm/ttm_memory.c    |   7 +-
>  drivers/gpu/drm/ttm/ttm_tt.c        | 111 ++++++++++++++++++++++++++--
>  drivers/gpu/drm/vmwgfx/vmwgfx_drv.c |   2 +-
>  include/drm/ttm/ttm_bo_api.h        |   2 +-
>  include/drm/ttm/ttm_tt.h            |   6 +-
>  6 files changed, 117 insertions(+), 15 deletions(-)
> 
> diff --git a/drivers/gpu/drm/ttm/ttm_bo.c b/drivers/gpu/drm/ttm/ttm_bo.c
> index 20256797f3a6..643befc1a6f2 100644
> --- a/drivers/gpu/drm/ttm/ttm_bo.c
> +++ b/drivers/gpu/drm/ttm/ttm_bo.c
> @@ -1219,7 +1219,7 @@ EXPORT_SYMBOL(ttm_bo_wait);
>   * A buffer object shrink method that tries to swap out the first
>   * buffer object on the bo_global::swap_lru list.
>   */
> -int ttm_bo_swapout(struct ttm_operation_ctx *ctx)
> +int ttm_bo_swapout(struct ttm_operation_ctx *ctx, gfp_t gfp_flags)
>  {
>  	struct ttm_global *glob = &ttm_glob;
>  	struct ttm_buffer_object *bo;
> @@ -1302,7 +1302,7 @@ int ttm_bo_swapout(struct ttm_operation_ctx *ctx)
>  	if (bo->bdev->funcs->swap_notify)
>  		bo->bdev->funcs->swap_notify(bo);
>  
> -	ret = ttm_tt_swapout(bo->bdev, bo->ttm);
> +	ret = ttm_tt_swapout(bo->bdev, bo->ttm, gfp_flags);
>  out:
>  
>  	/**
> diff --git a/drivers/gpu/drm/ttm/ttm_memory.c b/drivers/gpu/drm/ttm/ttm_memory.c
> index a3bfbd9cea68..634a85c2dc4c 100644
> --- a/drivers/gpu/drm/ttm/ttm_memory.c
> +++ b/drivers/gpu/drm/ttm/ttm_memory.c
> @@ -37,6 +37,7 @@
>  #include <linux/slab.h>
>  #include <linux/swap.h>
>  #include <drm/ttm/ttm_pool.h>
> +#include <drm/ttm/ttm_tt.h>
>  
>  #include "ttm_module.h"
>  
> @@ -276,9 +277,9 @@ static void ttm_shrink(struct ttm_mem_global *glob, bool from_wq,
>  
>  	while (ttm_zones_above_swap_target(glob, from_wq, extra)) {
>  		spin_unlock(&glob->lock);
> -		ret = ttm_bo_swapout(ctx);
> +		ret = ttm_bo_swapout(ctx, GFP_KERNEL);
>  		spin_lock(&glob->lock);
> -		if (unlikely(ret != 0))
> +		if (unlikely(ret < 0))
>  			break;
>  	}
>  
> @@ -453,6 +454,7 @@ int ttm_mem_global_init(struct ttm_mem_global *glob)
>  			zone->name, (unsigned long long)zone->max_mem >> 10);
>  	}
>  	ttm_pool_mgr_init(glob->zone_kernel->max_mem/(2*PAGE_SIZE));
> +	ttm_tt_mgr_init();
>  	return 0;
>  out_no_zone:
>  	ttm_mem_global_release(glob);
> @@ -466,6 +468,7 @@ void ttm_mem_global_release(struct ttm_mem_global *glob)
>  
>  	/* let the page allocator first stop the shrink work. */
>  	ttm_pool_mgr_fini();
> +	ttm_tt_mgr_fini();
>  
>  	flush_workqueue(glob->swap_queue);
>  	destroy_workqueue(glob->swap_queue);
> diff --git a/drivers/gpu/drm/ttm/ttm_tt.c b/drivers/gpu/drm/ttm/ttm_tt.c
> index 7782d5393c7c..b67795de228d 100644
> --- a/drivers/gpu/drm/ttm/ttm_tt.c
> +++ b/drivers/gpu/drm/ttm/ttm_tt.c
> @@ -38,6 +38,11 @@
>  #include <drm/drm_cache.h>
>  #include <drm/ttm/ttm_bo_driver.h>
>  
> +#include "ttm_module.h"
> +
> +static struct shrinker mm_shrinker;
> +static atomic_long_t swapable_pages;
> +
>  /*
>   * Allocates a ttm structure for the given BO.
>   */
> @@ -223,32 +228,41 @@ int ttm_tt_swapin(struct ttm_tt *ttm)
>  	return ret;
>  }
>  
> -int ttm_tt_swapout(struct ttm_device *bdev, struct ttm_tt *ttm)
> +/**
> + * ttm_tt_swapout - swap out tt object
> + *
> + * @bdev: TTM device structure.
> + * @ttm: The struct ttm_tt.
> + * @gfp_flags: Flags to use for memory allocation.
> + *
> + * Swapout a TT object to a shmem_file, return number of pages swapped out or
> + * negative error code.
> + */
> +int ttm_tt_swapout(struct ttm_device *bdev, struct ttm_tt *ttm,
> +		   gfp_t gfp_flags)
>  {
> +	loff_t size = (loff_t)ttm->num_pages << PAGE_SHIFT;
>  	struct address_space *swap_space;
>  	struct file *swap_storage;
>  	struct page *from_page;
>  	struct page *to_page;
> -	gfp_t gfp_mask;
>  	int i, ret;
>  
> -	swap_storage = shmem_file_setup("ttm swap",
> -					ttm->num_pages << PAGE_SHIFT,
> -					0);
> +	swap_storage = shmem_file_setup("ttm swap", size, 0);
>  	if (IS_ERR(swap_storage)) {
>  		pr_err("Failed allocating swap storage\n");
>  		return PTR_ERR(swap_storage);
>  	}
>  
>  	swap_space = swap_storage->f_mapping;
> -	gfp_mask = mapping_gfp_mask(swap_space);
> +	gfp_flags &= mapping_gfp_mask(swap_space);
>  
>  	for (i = 0; i < ttm->num_pages; ++i) {
>  		from_page = ttm->pages[i];
>  		if (unlikely(from_page == NULL))
>  			continue;
>  
> -		to_page = shmem_read_mapping_page_gfp(swap_space, i, gfp_mask);
> +		to_page = shmem_read_mapping_page_gfp(swap_space, i, gfp_flags);
>  		if (IS_ERR(to_page)) {
>  			ret = PTR_ERR(to_page);
>  			goto out_err;
> @@ -263,7 +277,7 @@ int ttm_tt_swapout(struct ttm_device *bdev, struct ttm_tt *ttm)
>  	ttm->swap_storage = swap_storage;
>  	ttm->page_flags |= TTM_PAGE_FLAG_SWAPPED;
>  
> -	return 0;
> +	return ttm->num_pages;
>  
>  out_err:
>  	fput(swap_storage);
> @@ -280,6 +294,8 @@ static void ttm_tt_add_mapping(struct ttm_device *bdev, struct ttm_tt *ttm)
>  
>  	for (i = 0; i < ttm->num_pages; ++i)
>  		ttm->pages[i]->mapping = bdev->dev_mapping;
> +
> +	atomic_long_add(ttm->num_pages, &swapable_pages);
>  }
>  
>  int ttm_tt_populate(struct ttm_device *bdev,
> @@ -326,6 +342,8 @@ static void ttm_tt_clear_mapping(struct ttm_tt *ttm)
>  		(*page)->mapping = NULL;
>  		(*page++)->index = 0;
>  	}
> +
> +	atomic_long_sub(ttm->num_pages, &swapable_pages);
>  }
>  
>  void ttm_tt_unpopulate(struct ttm_device *bdev,
> @@ -341,3 +359,80 @@ void ttm_tt_unpopulate(struct ttm_device *bdev,
>  		ttm_pool_free(&bdev->pool, ttm);
>  	ttm->page_flags &= ~TTM_PAGE_FLAG_PRIV_POPULATED;
>  }
> +
> +/* As long as pages are available make sure to release at least one */
> +static unsigned long ttm_tt_shrinker_scan(struct shrinker *shrink,
> +					  struct shrink_control *sc)
> +{
> +	struct ttm_operation_ctx ctx = {
> +		.no_wait_gpu = false
> +	};
> +	int ret;
> +
> +	if (!(sc->gfp_mask & __GFP_FS))
> +		return SHRINK_EMPTY;

These two checks here still look like cargo cult to me. I thought the
gfp_mask you're getting is for numa/zone-aware shrinking, which we're not
doing. __GFP_FS in the shrinker is a bug.

Maybe convert to WARN_ON to convince yourself, test, then remove? If you
ever get __GFP_FS context in a shrinker lockdep will start screaming real
fast :-)

With that addressed:

Reviewed-by: Daniel Vetter <daniel.vetter@ffwll.ch>

> +
> +	ret = ttm_bo_swapout(&ctx, GFP_NOFS);
> +	return ret < 0 ? SHRINK_EMPTY : ret;
> +}
> +
> +/* Return the number of pages available or SHRINK_EMPTY if we have none */
> +static unsigned long ttm_tt_shrinker_count(struct shrinker *shrink,
> +					   struct shrink_control *sc)
> +{
> +	unsigned long num_pages;
> +
> +	if (!(sc->gfp_mask & __GFP_FS))
> +		return SHRINK_EMPTY;
> +
> +	num_pages = atomic_long_read(&swapable_pages);
> +	return num_pages ? num_pages : SHRINK_EMPTY;
> +}
> +
> +#ifdef CONFIG_DEBUG_FS
> +
> +/* Test the shrinker functions and dump the result */
> +static int ttm_tt_debugfs_shrink_show(struct seq_file *m, void *data)
> +{
> +	struct shrink_control sc = { .gfp_mask = GFP_KERNEL };
> +
> +	fs_reclaim_acquire(GFP_KERNEL);
> +	seq_printf(m, "%lu/%lu\n", ttm_tt_shrinker_count(&mm_shrinker, &sc),
> +		   ttm_tt_shrinker_scan(&mm_shrinker, &sc));
> +	fs_reclaim_release(GFP_KERNEL);
> +
> +	return 0;
> +}
> +DEFINE_SHOW_ATTRIBUTE(ttm_tt_debugfs_shrink);
> +
> +#endif
> +
> +
> +
> +/**
> + * ttm_tt_mgr_init - register with the MM shrinker
> + *
> + * Register with the MM shrinker for swapping out BOs.
> + */
> +int ttm_tt_mgr_init(void)
> +{
> +#ifdef CONFIG_DEBUG_FS
> +	debugfs_create_file("tt_shrink", 0400, ttm_debugfs_root, NULL,
> +			    &ttm_tt_debugfs_shrink_fops);
> +#endif
> +
> +	mm_shrinker.count_objects = ttm_tt_shrinker_count;
> +	mm_shrinker.scan_objects = ttm_tt_shrinker_scan;
> +	mm_shrinker.seeks = 1;
> +	return register_shrinker(&mm_shrinker);
> +}
> +
> +/**
> + * ttm_tt_mgr_fini - unregister our MM shrinker
> + *
> + * Unregisters the MM shrinker.
> + */
> +void ttm_tt_mgr_fini(void)
> +{
> +	unregister_shrinker(&mm_shrinker);
> +}
> diff --git a/drivers/gpu/drm/vmwgfx/vmwgfx_drv.c b/drivers/gpu/drm/vmwgfx/vmwgfx_drv.c
> index b454d80c273e..710ba5169a74 100644
> --- a/drivers/gpu/drm/vmwgfx/vmwgfx_drv.c
> +++ b/drivers/gpu/drm/vmwgfx/vmwgfx_drv.c
> @@ -1383,7 +1383,7 @@ static int vmw_pm_freeze(struct device *kdev)
>  	vmw_execbuf_release_pinned_bo(dev_priv);
>  	vmw_resource_evict_all(dev_priv);
>  	vmw_release_device_early(dev_priv);
> -	while (ttm_bo_swapout(&ctx) == 0);
> +	while (ttm_bo_swapout(&ctx, GFP_KERNEL) > 0);
>  	if (dev_priv->enable_fb)
>  		vmw_fifo_resource_dec(dev_priv);
>  	if (atomic_read(&dev_priv->num_fifo_resources) != 0) {
> diff --git a/include/drm/ttm/ttm_bo_api.h b/include/drm/ttm/ttm_bo_api.h
> index 62734db0b421..1297a8fb7ccb 100644
> --- a/include/drm/ttm/ttm_bo_api.h
> +++ b/include/drm/ttm/ttm_bo_api.h
> @@ -569,7 +569,7 @@ ssize_t ttm_bo_io(struct ttm_device *bdev, struct file *filp,
>  		  const char __user *wbuf, char __user *rbuf,
>  		  size_t count, loff_t *f_pos, bool write);
>  
> -int ttm_bo_swapout(struct ttm_operation_ctx *ctx);
> +int ttm_bo_swapout(struct ttm_operation_ctx *ctx, gfp_t gfp_flags);
>  
>  /**
>   * ttm_bo_uses_embedded_gem_object - check if the given bo uses the
> diff --git a/include/drm/ttm/ttm_tt.h b/include/drm/ttm/ttm_tt.h
> index 0020a0588985..cce57fb49e2c 100644
> --- a/include/drm/ttm/ttm_tt.h
> +++ b/include/drm/ttm/ttm_tt.h
> @@ -135,7 +135,8 @@ void ttm_tt_destroy_common(struct ttm_device *bdev, struct ttm_tt *ttm);
>   * Swap in a previously swap out ttm_tt.
>   */
>  int ttm_tt_swapin(struct ttm_tt *ttm);
> -int ttm_tt_swapout(struct ttm_device *bdev, struct ttm_tt *ttm);
> +int ttm_tt_swapout(struct ttm_device *bdev, struct ttm_tt *ttm,
> +		   gfp_t gfp_flags);
>  
>  /**
>   * ttm_tt_populate - allocate pages for a ttm
> @@ -155,6 +156,9 @@ int ttm_tt_populate(struct ttm_device *bdev, struct ttm_tt *ttm, struct ttm_oper
>   */
>  void ttm_tt_unpopulate(struct ttm_device *bdev, struct ttm_tt *ttm);
>  
> +int ttm_tt_mgr_init(void);
> +void ttm_tt_mgr_fini(void);
> +
>  #if IS_ENABLED(CONFIG_AGP)
>  #include <linux/agp_backend.h>
>  
> -- 
> 2.25.1
> 

-- 
Daniel Vetter
Software Engineer, Intel Corporation
http://blog.ffwll.ch
_______________________________________________
dri-devel mailing list
dri-devel@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/dri-devel

^ permalink raw reply	[flat|nested] 17+ messages in thread

* Re: [PATCH 3/3] drm/ttm: drop sysfs directory
  2021-01-28 13:16 ` [PATCH 3/3] drm/ttm: drop sysfs directory Christian König
@ 2021-02-03 11:28   ` Daniel Vetter
  0 siblings, 0 replies; 17+ messages in thread
From: Daniel Vetter @ 2021-02-03 11:28 UTC (permalink / raw)
  To: Christian König; +Cc: linux-graphics-maintainer, dri-devel, sroland

On Thu, Jan 28, 2021 at 02:16:04PM +0100, Christian König wrote:
> Not used any more.
> 
> Signed-off-by: Christian König <christian.koenig@amd.com>

Reviewed-by: Daniel Vetter <daniel.vetter@ffwll.ch>

> ---
>  drivers/gpu/drm/ttm/ttm_module.c | 50 --------------------------------
>  drivers/gpu/drm/ttm/ttm_module.h |  2 --
>  2 files changed, 52 deletions(-)
> 
> diff --git a/drivers/gpu/drm/ttm/ttm_module.c b/drivers/gpu/drm/ttm/ttm_module.c
> index f6566603a60f..56b0efdba1a9 100644
> --- a/drivers/gpu/drm/ttm/ttm_module.c
> +++ b/drivers/gpu/drm/ttm/ttm_module.c
> @@ -37,66 +37,16 @@
>  
>  #include "ttm_module.h"
>  
> -static DECLARE_WAIT_QUEUE_HEAD(exit_q);
> -static atomic_t device_released;
>  struct dentry *ttm_debugfs_root;
>  
> -static struct device_type ttm_drm_class_type = {
> -	.name = "ttm",
> -	/**
> -	 * Add pm ops here.
> -	 */
> -};
> -
> -static void ttm_drm_class_device_release(struct device *dev)
> -{
> -	atomic_set(&device_released, 1);
> -	wake_up_all(&exit_q);
> -}
> -
> -static struct device ttm_drm_class_device = {
> -	.type = &ttm_drm_class_type,
> -	.release = &ttm_drm_class_device_release
> -};
> -
> -struct kobject *ttm_get_kobj(void)
> -{
> -	struct kobject *kobj = &ttm_drm_class_device.kobj;
> -	BUG_ON(kobj == NULL);
> -	return kobj;
> -}
> -
>  static int __init ttm_init(void)
>  {
> -	int ret;
> -
> -	ret = dev_set_name(&ttm_drm_class_device, "ttm");
> -	if (unlikely(ret != 0))
> -		return ret;
> -
> -	atomic_set(&device_released, 0);
> -	ret = drm_class_device_register(&ttm_drm_class_device);
> -	if (unlikely(ret != 0))
> -		goto out_no_dev_reg;
> -
>  	ttm_debugfs_root = debugfs_create_dir("ttm", NULL);
>  	return 0;
> -out_no_dev_reg:
> -	atomic_set(&device_released, 1);
> -	wake_up_all(&exit_q);
> -	return ret;
>  }
>  
>  static void __exit ttm_exit(void)
>  {
> -	drm_class_device_unregister(&ttm_drm_class_device);
> -
> -	/**
> -	 * Refuse to unload until the TTM device is released.
> -	 * Not sure this is 100% needed.
> -	 */
> -
> -	wait_event(exit_q, atomic_read(&device_released) == 1);
>  	debugfs_remove(ttm_debugfs_root);
>  }
>  
> diff --git a/drivers/gpu/drm/ttm/ttm_module.h b/drivers/gpu/drm/ttm/ttm_module.h
> index 2f03c2fcf570..d7cac5d4b835 100644
> --- a/drivers/gpu/drm/ttm/ttm_module.h
> +++ b/drivers/gpu/drm/ttm/ttm_module.h
> @@ -33,10 +33,8 @@
>  
>  #define TTM_PFX "[TTM] "
>  
> -struct kobject;
>  struct dentry;
>  
> -extern struct kobject *ttm_get_kobj(void);
>  extern struct dentry *ttm_debugfs_root;
>  
>  #endif /* _TTM_MODULE_H_ */
> -- 
> 2.25.1
> 

-- 
Daniel Vetter
Software Engineer, Intel Corporation
http://blog.ffwll.ch
_______________________________________________
dri-devel mailing list
dri-devel@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/dri-devel

^ permalink raw reply	[flat|nested] 17+ messages in thread

* Re: [PATCH 1/3] drm/ttm: rework ttm_tt page limit v3
  2021-02-03 11:26 ` [PATCH 1/3] drm/ttm: rework ttm_tt page limit v3 Daniel Vetter
@ 2021-02-03 12:18   ` Christian König
  0 siblings, 0 replies; 17+ messages in thread
From: Christian König @ 2021-02-03 12:18 UTC (permalink / raw)
  To: Daniel Vetter; +Cc: linux-graphics-maintainer, sroland, dri-devel

Am 03.02.21 um 12:26 schrieb Daniel Vetter:
> On Thu, Jan 28, 2021 at 02:16:02PM +0100, Christian König wrote:
>> TTM implements a rather extensive accounting of allocated memory.
>>
>> There are two reasons for this:
>> 1. It tries to block userspace allocating a huge number of very small
>>     BOs without accounting for the kmalloced memory.
>>
>> 2. Make sure we don't over allocate and run into an OOM situation
>>     during swapout while trying to handle the memory shortage.
>>
>> This is only partially a good idea. First of all it is perfectly
>> valid for an application to use all of system memory, limiting it to
>> 50% is not really acceptable.
>>
>> What we need to take care of is that the application is held
>> accountable for the memory it allocated. This is what control
>> mechanisms like memcg and the normal Linux page accounting already do.
>>
>> Making sure that we don't run into an OOM situation while trying to
>> cope with a memory shortage is still a good idea, but this is also
>> not very well implemented since it means another opportunity of
>> recursion from the driver back into TTM.
>>
>> So start to rework all of this by implementing a shrinker callback which
>> allows for TT object to be swapped out if necessary.
>>
>> v2: Switch from limit to shrinker callback.
>> v3: fix gfp mask handling, use atomic for swapable_pages, add debugfs
>>
>> Signed-off-by: Christian König <christian.koenig@amd.com>
>> ---
>>   drivers/gpu/drm/ttm/ttm_bo.c        |   4 +-
>>   drivers/gpu/drm/ttm/ttm_memory.c    |   7 +-
>>   drivers/gpu/drm/ttm/ttm_tt.c        | 111 ++++++++++++++++++++++++++--
>>   drivers/gpu/drm/vmwgfx/vmwgfx_drv.c |   2 +-
>>   include/drm/ttm/ttm_bo_api.h        |   2 +-
>>   include/drm/ttm/ttm_tt.h            |   6 +-
>>   6 files changed, 117 insertions(+), 15 deletions(-)
>>
>> diff --git a/drivers/gpu/drm/ttm/ttm_bo.c b/drivers/gpu/drm/ttm/ttm_bo.c
>> index 20256797f3a6..643befc1a6f2 100644
>> --- a/drivers/gpu/drm/ttm/ttm_bo.c
>> +++ b/drivers/gpu/drm/ttm/ttm_bo.c
>> @@ -1219,7 +1219,7 @@ EXPORT_SYMBOL(ttm_bo_wait);
>>    * A buffer object shrink method that tries to swap out the first
>>    * buffer object on the bo_global::swap_lru list.
>>    */
>> -int ttm_bo_swapout(struct ttm_operation_ctx *ctx)
>> +int ttm_bo_swapout(struct ttm_operation_ctx *ctx, gfp_t gfp_flags)
>>   {
>>   	struct ttm_global *glob = &ttm_glob;
>>   	struct ttm_buffer_object *bo;
>> @@ -1302,7 +1302,7 @@ int ttm_bo_swapout(struct ttm_operation_ctx *ctx)
>>   	if (bo->bdev->funcs->swap_notify)
>>   		bo->bdev->funcs->swap_notify(bo);
>>   
>> -	ret = ttm_tt_swapout(bo->bdev, bo->ttm);
>> +	ret = ttm_tt_swapout(bo->bdev, bo->ttm, gfp_flags);
>>   out:
>>   
>>   	/**
>> diff --git a/drivers/gpu/drm/ttm/ttm_memory.c b/drivers/gpu/drm/ttm/ttm_memory.c
>> index a3bfbd9cea68..634a85c2dc4c 100644
>> --- a/drivers/gpu/drm/ttm/ttm_memory.c
>> +++ b/drivers/gpu/drm/ttm/ttm_memory.c
>> @@ -37,6 +37,7 @@
>>   #include <linux/slab.h>
>>   #include <linux/swap.h>
>>   #include <drm/ttm/ttm_pool.h>
>> +#include <drm/ttm/ttm_tt.h>
>>   
>>   #include "ttm_module.h"
>>   
>> @@ -276,9 +277,9 @@ static void ttm_shrink(struct ttm_mem_global *glob, bool from_wq,
>>   
>>   	while (ttm_zones_above_swap_target(glob, from_wq, extra)) {
>>   		spin_unlock(&glob->lock);
>> -		ret = ttm_bo_swapout(ctx);
>> +		ret = ttm_bo_swapout(ctx, GFP_KERNEL);
>>   		spin_lock(&glob->lock);
>> -		if (unlikely(ret != 0))
>> +		if (unlikely(ret < 0))
>>   			break;
>>   	}
>>   
>> @@ -453,6 +454,7 @@ int ttm_mem_global_init(struct ttm_mem_global *glob)
>>   			zone->name, (unsigned long long)zone->max_mem >> 10);
>>   	}
>>   	ttm_pool_mgr_init(glob->zone_kernel->max_mem/(2*PAGE_SIZE));
>> +	ttm_tt_mgr_init();
>>   	return 0;
>>   out_no_zone:
>>   	ttm_mem_global_release(glob);
>> @@ -466,6 +468,7 @@ void ttm_mem_global_release(struct ttm_mem_global *glob)
>>   
>>   	/* let the page allocator first stop the shrink work. */
>>   	ttm_pool_mgr_fini();
>> +	ttm_tt_mgr_fini();
>>   
>>   	flush_workqueue(glob->swap_queue);
>>   	destroy_workqueue(glob->swap_queue);
>> diff --git a/drivers/gpu/drm/ttm/ttm_tt.c b/drivers/gpu/drm/ttm/ttm_tt.c
>> index 7782d5393c7c..b67795de228d 100644
>> --- a/drivers/gpu/drm/ttm/ttm_tt.c
>> +++ b/drivers/gpu/drm/ttm/ttm_tt.c
>> @@ -38,6 +38,11 @@
>>   #include <drm/drm_cache.h>
>>   #include <drm/ttm/ttm_bo_driver.h>
>>   
>> +#include "ttm_module.h"
>> +
>> +static struct shrinker mm_shrinker;
>> +static atomic_long_t swapable_pages;
>> +
>>   /*
>>    * Allocates a ttm structure for the given BO.
>>    */
>> @@ -223,32 +228,41 @@ int ttm_tt_swapin(struct ttm_tt *ttm)
>>   	return ret;
>>   }
>>   
>> -int ttm_tt_swapout(struct ttm_device *bdev, struct ttm_tt *ttm)
>> +/**
>> + * ttm_tt_swapout - swap out tt object
>> + *
>> + * @bdev: TTM device structure.
>> + * @ttm: The struct ttm_tt.
>> + * @gfp_flags: Flags to use for memory allocation.
>> + *
>> + * Swapout a TT object to a shmem_file, return number of pages swapped out or
>> + * negative error code.
>> + */
>> +int ttm_tt_swapout(struct ttm_device *bdev, struct ttm_tt *ttm,
>> +		   gfp_t gfp_flags)
>>   {
>> +	loff_t size = (loff_t)ttm->num_pages << PAGE_SHIFT;
>>   	struct address_space *swap_space;
>>   	struct file *swap_storage;
>>   	struct page *from_page;
>>   	struct page *to_page;
>> -	gfp_t gfp_mask;
>>   	int i, ret;
>>   
>> -	swap_storage = shmem_file_setup("ttm swap",
>> -					ttm->num_pages << PAGE_SHIFT,
>> -					0);
>> +	swap_storage = shmem_file_setup("ttm swap", size, 0);
>>   	if (IS_ERR(swap_storage)) {
>>   		pr_err("Failed allocating swap storage\n");
>>   		return PTR_ERR(swap_storage);
>>   	}
>>   
>>   	swap_space = swap_storage->f_mapping;
>> -	gfp_mask = mapping_gfp_mask(swap_space);
>> +	gfp_flags &= mapping_gfp_mask(swap_space);
>>   
>>   	for (i = 0; i < ttm->num_pages; ++i) {
>>   		from_page = ttm->pages[i];
>>   		if (unlikely(from_page == NULL))
>>   			continue;
>>   
>> -		to_page = shmem_read_mapping_page_gfp(swap_space, i, gfp_mask);
>> +		to_page = shmem_read_mapping_page_gfp(swap_space, i, gfp_flags);
>>   		if (IS_ERR(to_page)) {
>>   			ret = PTR_ERR(to_page);
>>   			goto out_err;
>> @@ -263,7 +277,7 @@ int ttm_tt_swapout(struct ttm_device *bdev, struct ttm_tt *ttm)
>>   	ttm->swap_storage = swap_storage;
>>   	ttm->page_flags |= TTM_PAGE_FLAG_SWAPPED;
>>   
>> -	return 0;
>> +	return ttm->num_pages;
>>   
>>   out_err:
>>   	fput(swap_storage);
>> @@ -280,6 +294,8 @@ static void ttm_tt_add_mapping(struct ttm_device *bdev, struct ttm_tt *ttm)
>>   
>>   	for (i = 0; i < ttm->num_pages; ++i)
>>   		ttm->pages[i]->mapping = bdev->dev_mapping;
>> +
>> +	atomic_long_add(ttm->num_pages, &swapable_pages);
>>   }
>>   
>>   int ttm_tt_populate(struct ttm_device *bdev,
>> @@ -326,6 +342,8 @@ static void ttm_tt_clear_mapping(struct ttm_tt *ttm)
>>   		(*page)->mapping = NULL;
>>   		(*page++)->index = 0;
>>   	}
>> +
>> +	atomic_long_sub(ttm->num_pages, &swapable_pages);
>>   }
>>   
>>   void ttm_tt_unpopulate(struct ttm_device *bdev,
>> @@ -341,3 +359,80 @@ void ttm_tt_unpopulate(struct ttm_device *bdev,
>>   		ttm_pool_free(&bdev->pool, ttm);
>>   	ttm->page_flags &= ~TTM_PAGE_FLAG_PRIV_POPULATED;
>>   }
>> +
>> +/* As long as pages are available make sure to release at least one */
>> +static unsigned long ttm_tt_shrinker_scan(struct shrinker *shrink,
>> +					  struct shrink_control *sc)
>> +{
>> +	struct ttm_operation_ctx ctx = {
>> +		.no_wait_gpu = false
>> +	};
>> +	int ret;
>> +
>> +	if (!(sc->gfp_mask & __GFP_FS))
>> +		return SHRINK_EMPTY;
> These two checks here still look like cargo cult to me. I thought the
> gfp_mask you're getting is for numa/zone-aware shrinking, which we're not
> doing. __GFP_FS in the shrinker is a bug.
>
> Maybe convert to WARN_ON to convince yourself, test, then remove? If you
> ever get __GFP_FS context in a shrinker lockdep will start screaming real
> fast :-)

WARN_ON work for me as well. I just couldn't find any code which would 
prevent that.

But I agree that it doesn't make much sense from the top level idea.

>
> With that addressed:
>
> Reviewed-by: Daniel Vetter <daniel.vetter@ffwll.ch>

Thanks,
Christian.

>
>> +
>> +	ret = ttm_bo_swapout(&ctx, GFP_NOFS);
>> +	return ret < 0 ? SHRINK_EMPTY : ret;
>> +}
>> +
>> +/* Return the number of pages available or SHRINK_EMPTY if we have none */
>> +static unsigned long ttm_tt_shrinker_count(struct shrinker *shrink,
>> +					   struct shrink_control *sc)
>> +{
>> +	unsigned long num_pages;
>> +
>> +	if (!(sc->gfp_mask & __GFP_FS))
>> +		return SHRINK_EMPTY;
>> +
>> +	num_pages = atomic_long_read(&swapable_pages);
>> +	return num_pages ? num_pages : SHRINK_EMPTY;
>> +}
>> +
>> +#ifdef CONFIG_DEBUG_FS
>> +
>> +/* Test the shrinker functions and dump the result */
>> +static int ttm_tt_debugfs_shrink_show(struct seq_file *m, void *data)
>> +{
>> +	struct shrink_control sc = { .gfp_mask = GFP_KERNEL };
>> +
>> +	fs_reclaim_acquire(GFP_KERNEL);
>> +	seq_printf(m, "%lu/%lu\n", ttm_tt_shrinker_count(&mm_shrinker, &sc),
>> +		   ttm_tt_shrinker_scan(&mm_shrinker, &sc));
>> +	fs_reclaim_release(GFP_KERNEL);
>> +
>> +	return 0;
>> +}
>> +DEFINE_SHOW_ATTRIBUTE(ttm_tt_debugfs_shrink);
>> +
>> +#endif
>> +
>> +
>> +
>> +/**
>> + * ttm_tt_mgr_init - register with the MM shrinker
>> + *
>> + * Register with the MM shrinker for swapping out BOs.
>> + */
>> +int ttm_tt_mgr_init(void)
>> +{
>> +#ifdef CONFIG_DEBUG_FS
>> +	debugfs_create_file("tt_shrink", 0400, ttm_debugfs_root, NULL,
>> +			    &ttm_tt_debugfs_shrink_fops);
>> +#endif
>> +
>> +	mm_shrinker.count_objects = ttm_tt_shrinker_count;
>> +	mm_shrinker.scan_objects = ttm_tt_shrinker_scan;
>> +	mm_shrinker.seeks = 1;
>> +	return register_shrinker(&mm_shrinker);
>> +}
>> +
>> +/**
>> + * ttm_tt_mgr_fini - unregister our MM shrinker
>> + *
>> + * Unregisters the MM shrinker.
>> + */
>> +void ttm_tt_mgr_fini(void)
>> +{
>> +	unregister_shrinker(&mm_shrinker);
>> +}
>> diff --git a/drivers/gpu/drm/vmwgfx/vmwgfx_drv.c b/drivers/gpu/drm/vmwgfx/vmwgfx_drv.c
>> index b454d80c273e..710ba5169a74 100644
>> --- a/drivers/gpu/drm/vmwgfx/vmwgfx_drv.c
>> +++ b/drivers/gpu/drm/vmwgfx/vmwgfx_drv.c
>> @@ -1383,7 +1383,7 @@ static int vmw_pm_freeze(struct device *kdev)
>>   	vmw_execbuf_release_pinned_bo(dev_priv);
>>   	vmw_resource_evict_all(dev_priv);
>>   	vmw_release_device_early(dev_priv);
>> -	while (ttm_bo_swapout(&ctx) == 0);
>> +	while (ttm_bo_swapout(&ctx, GFP_KERNEL) > 0);
>>   	if (dev_priv->enable_fb)
>>   		vmw_fifo_resource_dec(dev_priv);
>>   	if (atomic_read(&dev_priv->num_fifo_resources) != 0) {
>> diff --git a/include/drm/ttm/ttm_bo_api.h b/include/drm/ttm/ttm_bo_api.h
>> index 62734db0b421..1297a8fb7ccb 100644
>> --- a/include/drm/ttm/ttm_bo_api.h
>> +++ b/include/drm/ttm/ttm_bo_api.h
>> @@ -569,7 +569,7 @@ ssize_t ttm_bo_io(struct ttm_device *bdev, struct file *filp,
>>   		  const char __user *wbuf, char __user *rbuf,
>>   		  size_t count, loff_t *f_pos, bool write);
>>   
>> -int ttm_bo_swapout(struct ttm_operation_ctx *ctx);
>> +int ttm_bo_swapout(struct ttm_operation_ctx *ctx, gfp_t gfp_flags);
>>   
>>   /**
>>    * ttm_bo_uses_embedded_gem_object - check if the given bo uses the
>> diff --git a/include/drm/ttm/ttm_tt.h b/include/drm/ttm/ttm_tt.h
>> index 0020a0588985..cce57fb49e2c 100644
>> --- a/include/drm/ttm/ttm_tt.h
>> +++ b/include/drm/ttm/ttm_tt.h
>> @@ -135,7 +135,8 @@ void ttm_tt_destroy_common(struct ttm_device *bdev, struct ttm_tt *ttm);
>>    * Swap in a previously swap out ttm_tt.
>>    */
>>   int ttm_tt_swapin(struct ttm_tt *ttm);
>> -int ttm_tt_swapout(struct ttm_device *bdev, struct ttm_tt *ttm);
>> +int ttm_tt_swapout(struct ttm_device *bdev, struct ttm_tt *ttm,
>> +		   gfp_t gfp_flags);
>>   
>>   /**
>>    * ttm_tt_populate - allocate pages for a ttm
>> @@ -155,6 +156,9 @@ int ttm_tt_populate(struct ttm_device *bdev, struct ttm_tt *ttm, struct ttm_oper
>>    */
>>   void ttm_tt_unpopulate(struct ttm_device *bdev, struct ttm_tt *ttm);
>>   
>> +int ttm_tt_mgr_init(void);
>> +void ttm_tt_mgr_fini(void);
>> +
>>   #if IS_ENABLED(CONFIG_AGP)
>>   #include <linux/agp_backend.h>
>>   
>> -- 
>> 2.25.1
>>

_______________________________________________
dri-devel mailing list
dri-devel@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/dri-devel

^ permalink raw reply	[flat|nested] 17+ messages in thread

* Re: [Linux-graphics-maintainer] [PATCH 2/3] drm/ttm: move memory accounting into vmwgfx v3
  2021-02-03  8:20             ` Christian König
@ 2021-02-08 13:35               ` Christian König
  2021-02-08 13:39                 ` Daniel Vetter
  2021-02-08 20:21                 ` Zack Rusin
  0 siblings, 2 replies; 17+ messages in thread
From: Christian König @ 2021-02-08 13:35 UTC (permalink / raw)
  To: Zack Rusin; +Cc: Das, Nirmoy, Linux-graphics-maintainer, dri-devel

Hi Zack,

ok we figured out how to do this correctly.

Basically using the pdev->kobj instead of the drm->primary->kdev->kobj 
pointer worked quite well.

I've just send the latest patches to the mailing list. If you don't have 
any objections I will commit that tomorrow with your and Daniels rb.

Thanks,
Christian.

Am 03.02.21 um 09:20 schrieb Christian König:
> Hi Zack,
>
> thanks I can take over again from here on.
>
> Quite busy today, but I think I can go over the code once more tomorrow.
>
> Thanks for the help,
> Christian.
>
> Am 03.02.21 um 03:45 schrieb Zack Rusin:
>> Just had a quick peek. The issue is that you can’t attach to the drm 
>> device (card0) because it hasn’t been registered yet (drm device 
>> registration is last in the vmw_probe in vmwgfx_drv.c via the 
>> drm_dev_register). So dev->primary->kdev->kobj that you’re using as 
>> argument to kobject_init_and_add in ttm_mem_global_init hasn’t been 
>> initialized yet. So that particular sysfs code would likely have to 
>> be refactored out of ttm_mem_global_init to another function that 
>> could be called after drm registraction. I could take this on but not 
>> until Friday or so.
>>
>> z
>>
>>
>>> On Feb 2, 2021, at 12:42, Zack Rusin <zackr@vmware.com> wrote:
>>>
>>> Ah, yes, sorry, I missed that. I just double checked and it fails with:
>>>
>>> kobject_add_internal failed for memory_accounting (error: -2 parent: 
>>> card0)
>>>
>>> which breaks the probe and the driver won’t load. I won’t have time 
>>> to look into it until tomorrow though.
>>>
>>> z
>>>
>>>> On Feb 2, 2021, at 10:16, Christian König 
>>>> <christian.koenig@amd.com> wrote:
>>>>
>>>> Hi Zack,
>>>>
>>>> can you also give it a quick smoke test?
>>>>
>>>> I'm not sure if I wired up all the sysfs magic correctly inside 
>>>> vmwgfx, but I currently don't have a setup where I can test this.
>>>>
>>>> Thanks,
>>>> Christian.
>>>>
>>>> Am 02.02.21 um 16:14 schrieb Zack Rusin:
>>>>> Looks good. There’s probably not much reason to call it ttm_memory 
>>>>> anymore as it only deals with ttm_mem_glob, we’ll likely fold it 
>>>>> in after you submit. Thanks.
>>>>>
>>>>> Reviewed-by: Zack Rusin <zackr@vmware.com>
>>>>>
>>>>> z
>>>>>
>>>>>> On Feb 2, 2021, at 08:04, Christian König 
>>>>>> <christian.koenig@amd.com> wrote:
>>>>>>
>>>>>> Ping?
>>>>>>
>>>>>> Especially Roland and Zack do you have any objections to this?
>>>>>>
>>>>>> Regards,
>>>>>> Christian.
>>>>>>
>>>>>> Am 28.01.21 um 14:16 schrieb Christian König:
>>>>>>> This is just another feature which is only used by VMWGFX, so move
>>>>>>> it into the driver instead.
>>>>>>>
>>>>>>> I've tried to add the accounting sysfs file to the kobject of 
>>>>>>> the drm
>>>>>>> minor, but I'm not 100% sure if this works as expected.
>>>>>>>
>>>>>>> v2: fix typo in KFD and avoid 64bit divide
>>>>>>> v3: fix init order in VMWGFX
>>>>>>>
>>>>>>> Signed-off-by: Christian König <christian.koenig@amd.com>
>>>>>>> ---
>>>>>>> .../gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c  | 16 ++++++---
>>>>>>> drivers/gpu/drm/amd/amdgpu/amdgpu_object.c    |  8 ++---
>>>>>>> drivers/gpu/drm/drm_gem_vram_helper.c         |  6 ++--
>>>>>>> drivers/gpu/drm/nouveau/nouveau_bo.c          |  7 ++--
>>>>>>> drivers/gpu/drm/nouveau/nouveau_drv.h         |  1 -
>>>>>>> drivers/gpu/drm/qxl/qxl_object.c              |  4 +--
>>>>>>> drivers/gpu/drm/radeon/radeon_object.c        |  8 ++---
>>>>>>> drivers/gpu/drm/ttm/Makefile                  |  7 ++--
>>>>>>> drivers/gpu/drm/ttm/ttm_bo.c                  | 33 
>>>>>>> +------------------
>>>>>>> drivers/gpu/drm/ttm/ttm_bo_util.c             |  1 -
>>>>>>> drivers/gpu/drm/ttm/ttm_device.c              | 22 ++++++++++---
>>>>>>> drivers/gpu/drm/ttm/ttm_pool.c                | 13 +-------
>>>>>>> drivers/gpu/drm/vmwgfx/Makefile               |  2 +-
>>>>>>> drivers/gpu/drm/{ttm => vmwgfx}/ttm_memory.c  | 19 ++++-------
>>>>>>> .../gpu/drm/vmwgfx}/ttm_memory.h              |  5 +--
>>>>>>> drivers/gpu/drm/vmwgfx/ttm_object.h           |  3 +-
>>>>>>> drivers/gpu/drm/vmwgfx/vmwgfx_bo.c            | 22 ++++++++++---
>>>>>>> drivers/gpu/drm/vmwgfx/vmwgfx_drv.c           |  5 +++
>>>>>>> drivers/gpu/drm/vmwgfx/vmwgfx_ttm_buffer.c    | 28 +++++++++++++++-
>>>>>>> include/drm/ttm/ttm_bo_api.h                  | 13 ++------
>>>>>>> include/drm/ttm/ttm_bo_driver.h               |  1 -
>>>>>>> include/drm/ttm/ttm_tt.h                      |  1 +
>>>>>>> 22 files changed, 110 insertions(+), 115 deletions(-)
>>>>>>> rename drivers/gpu/drm/{ttm => vmwgfx}/ttm_memory.c (97%)
>>>>>>> rename {include/drm/ttm => drivers/gpu/drm/vmwgfx}/ttm_memory.h 
>>>>>>> (97%)
>>>>>>>
>>>>>>> diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c 
>>>>>>> b/drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c
>>>>>>> index 0849b68e784f..e440af37dde8 100644
>>>>>>> --- a/drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c
>>>>>>> +++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c
>>>>>>> @@ -118,6 +118,16 @@ void amdgpu_amdkfd_gpuvm_init_mem_limits(void)
>>>>>>>   */
>>>>>>> #define ESTIMATE_PT_SIZE(mem_size) ((mem_size) >> 14)
>>>>>>> +static size_t amdgpu_amdkfd_acc_size(uint64_t size)
>>>>>>> +{
>>>>>>> +size >>= PAGE_SHIFT;
>>>>>>> +size *= sizeof(dma_addr_t) + sizeof(void *);
>>>>>>> +
>>>>>>> +return __roundup_pow_of_two(sizeof(struct amdgpu_bo)) +
>>>>>>> +__roundup_pow_of_two(sizeof(struct ttm_tt)) +
>>>>>>> +PAGE_ALIGN(size);
>>>>>>> +}
>>>>>>> +
>>>>>>> static int amdgpu_amdkfd_reserve_mem_limit(struct amdgpu_device 
>>>>>>> *adev,
>>>>>>> uint64_t size, u32 domain, bool sg)
>>>>>>> {
>>>>>>> @@ -126,8 +136,7 @@ static int 
>>>>>>> amdgpu_amdkfd_reserve_mem_limit(struct amdgpu_device *adev,
>>>>>>> size_t acc_size, system_mem_needed, ttm_mem_needed, vram_needed;
>>>>>>> int ret = 0;
>>>>>>> -acc_size = ttm_bo_dma_acc_size(&adev->mman.bdev, size,
>>>>>>> -       sizeof(struct amdgpu_bo));
>>>>>>> +acc_size = amdgpu_amdkfd_acc_size(size);
>>>>>>>    vram_needed = 0;
>>>>>>> if (domain == AMDGPU_GEM_DOMAIN_GTT) {
>>>>>>> @@ -174,8 +183,7 @@ static void unreserve_mem_limit(struct 
>>>>>>> amdgpu_device *adev,
>>>>>>> {
>>>>>>> size_t acc_size;
>>>>>>> -acc_size = ttm_bo_dma_acc_size(&adev->mman.bdev, size,
>>>>>>> -       sizeof(struct amdgpu_bo));
>>>>>>> +acc_size = amdgpu_amdkfd_acc_size(size);
>>>>>>>    spin_lock(&kfd_mem_limit.mem_limit_lock);
>>>>>>> if (domain == AMDGPU_GEM_DOMAIN_GTT) {
>>>>>>> diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_object.c 
>>>>>>> b/drivers/gpu/drm/amd/amdgpu/amdgpu_object.c
>>>>>>> index 6cc9919b12cc..599c9a132eb6 100644
>>>>>>> --- a/drivers/gpu/drm/amd/amdgpu/amdgpu_object.c
>>>>>>> +++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_object.c
>>>>>>> @@ -523,7 +523,6 @@ static int amdgpu_bo_do_create(struct 
>>>>>>> amdgpu_device *adev,
>>>>>>> };
>>>>>>> struct amdgpu_bo *bo;
>>>>>>> unsigned long page_align, size = bp->size;
>>>>>>> -size_t acc_size;
>>>>>>> int r;
>>>>>>>    /* Note that GDS/GWS/OA allocates 1 page per byte/resource. */
>>>>>>> @@ -546,9 +545,6 @@ static int amdgpu_bo_do_create(struct 
>>>>>>> amdgpu_device *adev,
>>>>>>>    *bo_ptr = NULL;
>>>>>>> -acc_size = ttm_bo_dma_acc_size(&adev->mman.bdev, size,
>>>>>>> -       sizeof(struct amdgpu_bo));
>>>>>>> -
>>>>>>> bo = kzalloc(sizeof(struct amdgpu_bo), GFP_KERNEL);
>>>>>>> if (bo == NULL)
>>>>>>> return -ENOMEM;
>>>>>>> @@ -577,8 +573,8 @@ static int amdgpu_bo_do_create(struct 
>>>>>>> amdgpu_device *adev,
>>>>>>> bo->tbo.priority = 1;
>>>>>>>    r = ttm_bo_init_reserved(&adev->mman.bdev, &bo->tbo, size, 
>>>>>>> bp->type,
>>>>>>> - &bo->placement, page_align, &ctx, acc_size,
>>>>>>> - NULL, bp->resv, &amdgpu_bo_destroy);
>>>>>>> + &bo->placement, page_align, &ctx,  NULL,
>>>>>>> + bp->resv, &amdgpu_bo_destroy);
>>>>>>> if (unlikely(r != 0))
>>>>>>> return r;
>>>>>>> diff --git a/drivers/gpu/drm/drm_gem_vram_helper.c 
>>>>>>> b/drivers/gpu/drm/drm_gem_vram_helper.c
>>>>>>> index 0b13c8507688..a0992f0b8afd 100644
>>>>>>> --- a/drivers/gpu/drm/drm_gem_vram_helper.c
>>>>>>> +++ b/drivers/gpu/drm/drm_gem_vram_helper.c
>>>>>>> @@ -189,7 +189,6 @@ struct drm_gem_vram_object 
>>>>>>> *drm_gem_vram_create(struct drm_device *dev,
>>>>>>> struct drm_vram_mm *vmm = dev->vram_mm;
>>>>>>> struct ttm_device *bdev;
>>>>>>> int ret;
>>>>>>> -size_t acc_size;
>>>>>>>    if (WARN_ONCE(!vmm, "VRAM MM not initialized"))
>>>>>>> return ERR_PTR(-EINVAL);
>>>>>>> @@ -216,7 +215,6 @@ struct drm_gem_vram_object 
>>>>>>> *drm_gem_vram_create(struct drm_device *dev,
>>>>>>> }
>>>>>>>    bdev = &vmm->bdev;
>>>>>>> -acc_size = ttm_bo_dma_acc_size(bdev, size, sizeof(*gbo));
>>>>>>>    gbo->bo.bdev = bdev;
>>>>>>> drm_gem_vram_placement(gbo, DRM_GEM_VRAM_PL_FLAG_SYSTEM);
>>>>>>> @@ -226,8 +224,8 @@ struct drm_gem_vram_object 
>>>>>>> *drm_gem_vram_create(struct drm_device *dev,
>>>>>>>   * to release gbo->bo.base and kfree gbo.
>>>>>>>   */
>>>>>>> ret = ttm_bo_init(bdev, &gbo->bo, size, ttm_bo_type_device,
>>>>>>> -  &gbo->placement, pg_align, false, acc_size,
>>>>>>> -  NULL, NULL, ttm_buffer_object_destroy);
>>>>>>> +  &gbo->placement, pg_align, false, NULL, NULL,
>>>>>>> +  ttm_buffer_object_destroy);
>>>>>>> if (ret)
>>>>>>> return ERR_PTR(ret);
>>>>>>> diff --git a/drivers/gpu/drm/nouveau/nouveau_bo.c 
>>>>>>> b/drivers/gpu/drm/nouveau/nouveau_bo.c
>>>>>>> index c177940d6e2c..ca2a8ae1938e 100644
>>>>>>> --- a/drivers/gpu/drm/nouveau/nouveau_bo.c
>>>>>>> +++ b/drivers/gpu/drm/nouveau/nouveau_bo.c
>>>>>>> @@ -300,18 +300,15 @@ nouveau_bo_init(struct nouveau_bo *nvbo, 
>>>>>>> u64 size, int align, u32 domain,
>>>>>>> struct sg_table *sg, struct dma_resv *robj)
>>>>>>> {
>>>>>>> int type = sg ? ttm_bo_type_sg : ttm_bo_type_device;
>>>>>>> -size_t acc_size;
>>>>>>> int ret;
>>>>>>> -acc_size = ttm_bo_dma_acc_size(nvbo->bo.bdev, size, 
>>>>>>> sizeof(*nvbo));
>>>>>>> -
>>>>>>> nvbo->bo.mem.num_pages = size >> PAGE_SHIFT;
>>>>>>> nouveau_bo_placement_set(nvbo, domain, 0);
>>>>>>> INIT_LIST_HEAD(&nvbo->io_reserve_lru);
>>>>>>>    ret = ttm_bo_init(nvbo->bo.bdev, &nvbo->bo, size, type,
>>>>>>> -  &nvbo->placement, align >> PAGE_SHIFT, false,
>>>>>>> -  acc_size, sg, robj, nouveau_bo_del_ttm);
>>>>>>> +  &nvbo->placement, align >> PAGE_SHIFT, false, sg,
>>>>>>> +  robj, nouveau_bo_del_ttm);
>>>>>>> if (ret) {
>>>>>>> /* ttm will call nouveau_bo_del_ttm if it fails.. */
>>>>>>> return ret;
>>>>>>> diff --git a/drivers/gpu/drm/nouveau/nouveau_drv.h 
>>>>>>> b/drivers/gpu/drm/nouveau/nouveau_drv.h
>>>>>>> index edf9d1ee9d58..a491c2c1c56e 100644
>>>>>>> --- a/drivers/gpu/drm/nouveau/nouveau_drv.h
>>>>>>> +++ b/drivers/gpu/drm/nouveau/nouveau_drv.h
>>>>>>> @@ -54,7 +54,6 @@
>>>>>>> #include <drm/ttm/ttm_bo_api.h>
>>>>>>> #include <drm/ttm/ttm_bo_driver.h>
>>>>>>> #include <drm/ttm/ttm_placement.h>
>>>>>>> -#include <drm/ttm/ttm_memory.h>
>>>>>>>    #include <drm/drm_audio_component.h>
>>>>>>> diff --git a/drivers/gpu/drm/qxl/qxl_object.c 
>>>>>>> b/drivers/gpu/drm/qxl/qxl_object.c
>>>>>>> index ceebc5881f68..705b51535492 100644
>>>>>>> --- a/drivers/gpu/drm/qxl/qxl_object.c
>>>>>>> +++ b/drivers/gpu/drm/qxl/qxl_object.c
>>>>>>> @@ -138,8 +138,8 @@ int qxl_bo_create(struct qxl_device *qdev,
>>>>>>> qxl_ttm_placement_from_domain(bo, domain);
>>>>>>>    r = ttm_bo_init_reserved(&qdev->mman.bdev, &bo->tbo, size, type,
>>>>>>> - &bo->placement, 0, &ctx, size,
>>>>>>> - NULL, NULL, &qxl_ttm_bo_destroy);
>>>>>>> + &bo->placement, 0, &ctx, NULL, NULL,
>>>>>>> + &qxl_ttm_bo_destroy);
>>>>>>> if (unlikely(r != 0)) {
>>>>>>> if (r != -ERESTARTSYS)
>>>>>>> dev_err(qdev->ddev.dev,
>>>>>>> diff --git a/drivers/gpu/drm/radeon/radeon_object.c 
>>>>>>> b/drivers/gpu/drm/radeon/radeon_object.c
>>>>>>> index 6a336284466f..804f7a427be7 100644
>>>>>>> --- a/drivers/gpu/drm/radeon/radeon_object.c
>>>>>>> +++ b/drivers/gpu/drm/radeon/radeon_object.c
>>>>>>> @@ -159,7 +159,6 @@ int radeon_bo_create(struct radeon_device 
>>>>>>> *rdev,
>>>>>>> struct radeon_bo *bo;
>>>>>>> enum ttm_bo_type type;
>>>>>>> unsigned long page_align = roundup(byte_align, PAGE_SIZE) >> 
>>>>>>> PAGE_SHIFT;
>>>>>>> -size_t acc_size;
>>>>>>> int r;
>>>>>>>    size = ALIGN(size, PAGE_SIZE);
>>>>>>> @@ -173,9 +172,6 @@ int radeon_bo_create(struct radeon_device 
>>>>>>> *rdev,
>>>>>>> }
>>>>>>> *bo_ptr = NULL;
>>>>>>> -acc_size = ttm_bo_dma_acc_size(&rdev->mman.bdev, size,
>>>>>>> -       sizeof(struct radeon_bo));
>>>>>>> -
>>>>>>> bo = kzalloc(sizeof(struct radeon_bo), GFP_KERNEL);
>>>>>>> if (bo == NULL)
>>>>>>> return -ENOMEM;
>>>>>>> @@ -230,8 +226,8 @@ int radeon_bo_create(struct radeon_device 
>>>>>>> *rdev,
>>>>>>> /* Kernel allocation are uninterruptible */
>>>>>>> down_read(&rdev->pm.mclk_lock);
>>>>>>> r = ttm_bo_init(&rdev->mman.bdev, &bo->tbo, size, type,
>>>>>>> -&bo->placement, page_align, !kernel, acc_size,
>>>>>>> -sg, resv, &radeon_ttm_bo_destroy);
>>>>>>> +&bo->placement, page_align, !kernel, sg, resv,
>>>>>>> +&radeon_ttm_bo_destroy);
>>>>>>> up_read(&rdev->pm.mclk_lock);
>>>>>>> if (unlikely(r != 0)) {
>>>>>>> return r;
>>>>>>> diff --git a/drivers/gpu/drm/ttm/Makefile 
>>>>>>> b/drivers/gpu/drm/ttm/Makefile
>>>>>>> index 8e6437eadabe..40e5e9da7953 100644
>>>>>>> --- a/drivers/gpu/drm/ttm/Makefile
>>>>>>> +++ b/drivers/gpu/drm/ttm/Makefile
>>>>>>> @@ -2,10 +2,9 @@
>>>>>>> #
>>>>>>> # Makefile for the drm device driver.  This driver provides 
>>>>>>> support for the
>>>>>>> -ttm-y := ttm_memory.o ttm_tt.o ttm_bo.o \
>>>>>>> -ttm_bo_util.o ttm_bo_vm.o ttm_module.o \
>>>>>>> -ttm_execbuf_util.o ttm_range_manager.o \
>>>>>>> -ttm_resource.o ttm_pool.o ttm_device.o
>>>>>>> +ttm-y := ttm_tt.o ttm_bo.o ttm_bo_util.o ttm_bo_vm.o 
>>>>>>> ttm_module.o \
>>>>>>> +ttm_execbuf_util.o ttm_range_manager.o ttm_resource.o ttm_pool.o \
>>>>>>> +ttm_device.o
>>>>>>> ttm-$(CONFIG_AGP) += ttm_agp_backend.o
>>>>>>>    obj-$(CONFIG_DRM_TTM) += ttm.o
>>>>>>> diff --git a/drivers/gpu/drm/ttm/ttm_bo.c 
>>>>>>> b/drivers/gpu/drm/ttm/ttm_bo.c
>>>>>>> index 643befc1a6f2..e38102282fd5 100644
>>>>>>> --- a/drivers/gpu/drm/ttm/ttm_bo.c
>>>>>>> +++ b/drivers/gpu/drm/ttm/ttm_bo.c
>>>>>>> @@ -425,7 +425,6 @@ static void ttm_bo_release(struct kref *kref)
>>>>>>> struct ttm_buffer_object *bo =
>>>>>>>      container_of(kref, struct ttm_buffer_object, kref);
>>>>>>> struct ttm_device *bdev = bo->bdev;
>>>>>>> -size_t acc_size = bo->acc_size;
>>>>>>> int ret;
>>>>>>>    if (!bo->deleted) {
>>>>>>> @@ -485,7 +484,6 @@ static void ttm_bo_release(struct kref *kref)
>>>>>>> if (!ttm_bo_uses_embedded_gem_object(bo))
>>>>>>> dma_resv_fini(&bo->base._resv);
>>>>>>> bo->destroy(bo);
>>>>>>> -ttm_mem_global_free(&ttm_mem_glob, acc_size);
>>>>>>> }
>>>>>>>    void ttm_bo_put(struct ttm_buffer_object *bo)
>>>>>>> @@ -1046,25 +1044,13 @@ int ttm_bo_init_reserved(struct 
>>>>>>> ttm_device *bdev,
>>>>>>>   struct ttm_placement *placement,
>>>>>>>   uint32_t page_alignment,
>>>>>>>   struct ttm_operation_ctx *ctx,
>>>>>>> - size_t acc_size,
>>>>>>>   struct sg_table *sg,
>>>>>>>   struct dma_resv *resv,
>>>>>>>   void (*destroy) (struct ttm_buffer_object *))
>>>>>>> {
>>>>>>> -struct ttm_mem_global *mem_glob = &ttm_mem_glob;
>>>>>>> bool locked;
>>>>>>> int ret = 0;
>>>>>>> -ret = ttm_mem_global_alloc(mem_glob, acc_size, ctx);
>>>>>>> -if (ret) {
>>>>>>> -pr_err("Out of kernel memory\n");
>>>>>>> -if (destroy)
>>>>>>> -(*destroy)(bo);
>>>>>>> -else
>>>>>>> -kfree(bo);
>>>>>>> -return -ENOMEM;
>>>>>>> -}
>>>>>>> -
>>>>>>> bo->destroy = destroy ? destroy : ttm_bo_default_destroy;
>>>>>>>    kref_init(&bo->kref);
>>>>>>> @@ -1081,7 +1067,6 @@ int ttm_bo_init_reserved(struct ttm_device 
>>>>>>> *bdev,
>>>>>>> bo->mem.bus.addr = NULL;
>>>>>>> bo->moving = NULL;
>>>>>>> bo->mem.placement = 0;
>>>>>>> -bo->acc_size = acc_size;
>>>>>>> bo->pin_count = 0;
>>>>>>> bo->sg = sg;
>>>>>>> if (resv) {
>>>>>>> @@ -1142,7 +1127,6 @@ int ttm_bo_init(struct ttm_device *bdev,
>>>>>>> struct ttm_placement *placement,
>>>>>>> uint32_t page_alignment,
>>>>>>> bool interruptible,
>>>>>>> -size_t acc_size,
>>>>>>> struct sg_table *sg,
>>>>>>> struct dma_resv *resv,
>>>>>>> void (*destroy) (struct ttm_buffer_object *))
>>>>>>> @@ -1151,8 +1135,7 @@ int ttm_bo_init(struct ttm_device *bdev,
>>>>>>> int ret;
>>>>>>>    ret = ttm_bo_init_reserved(bdev, bo, size, type, placement,
>>>>>>> -   page_alignment, &ctx, acc_size,
>>>>>>> -   sg, resv, destroy);
>>>>>>> +   page_alignment, &ctx, sg, resv, destroy);
>>>>>>> if (ret)
>>>>>>> return ret;
>>>>>>> @@ -1163,20 +1146,6 @@ int ttm_bo_init(struct ttm_device *bdev,
>>>>>>> }
>>>>>>> EXPORT_SYMBOL(ttm_bo_init);
>>>>>>> -size_t ttm_bo_dma_acc_size(struct ttm_device *bdev,
>>>>>>> -   unsigned long bo_size,
>>>>>>> -   unsigned struct_size)
>>>>>>> -{
>>>>>>> -unsigned npages = (PAGE_ALIGN(bo_size)) >> PAGE_SHIFT;
>>>>>>> -size_t size = 0;
>>>>>>> -
>>>>>>> -size += ttm_round_pot(struct_size);
>>>>>>> -size += ttm_round_pot(npages * (2*sizeof(void *) + 
>>>>>>> sizeof(dma_addr_t)));
>>>>>>> -size += ttm_round_pot(sizeof(struct ttm_tt));
>>>>>>> -return size;
>>>>>>> -}
>>>>>>> -EXPORT_SYMBOL(ttm_bo_dma_acc_size);
>>>>>>> -
>>>>>>> /*
>>>>>>>   * buffer object vm functions.
>>>>>>>   */
>>>>>>> diff --git a/drivers/gpu/drm/ttm/ttm_bo_util.c 
>>>>>>> b/drivers/gpu/drm/ttm/ttm_bo_util.c
>>>>>>> index db0f2661d504..031e5819fec4 100644
>>>>>>> --- a/drivers/gpu/drm/ttm/ttm_bo_util.c
>>>>>>> +++ b/drivers/gpu/drm/ttm/ttm_bo_util.c
>>>>>>> @@ -309,7 +309,6 @@ static int ttm_buffer_object_transfer(struct 
>>>>>>> ttm_buffer_object *bo,
>>>>>>>    kref_init(&fbo->base.kref);
>>>>>>> fbo->base.destroy = &ttm_transfered_destroy;
>>>>>>> -fbo->base.acc_size = 0;
>>>>>>> fbo->base.pin_count = 0;
>>>>>>> if (bo->type != ttm_bo_type_sg)
>>>>>>> fbo->base.base.resv = &fbo->base.base._resv;
>>>>>>> diff --git a/drivers/gpu/drm/ttm/ttm_device.c 
>>>>>>> b/drivers/gpu/drm/ttm/ttm_device.c
>>>>>>> index ac0903c9e60a..6bde344e5da7 100644
>>>>>>> --- a/drivers/gpu/drm/ttm/ttm_device.c
>>>>>>> +++ b/drivers/gpu/drm/ttm/ttm_device.c
>>>>>>> @@ -27,9 +27,12 @@
>>>>>>>    #define pr_fmt(fmt) "[TTM DEVICE] " fmt
>>>>>>> +#include <linux/mm.h>
>>>>>>> +
>>>>>>> #include <drm/ttm/ttm_device.h>
>>>>>>> -#include <drm/ttm/ttm_memory.h>
>>>>>>> +#include <drm/ttm/ttm_tt.h>
>>>>>>> #include <drm/ttm/ttm_placement.h>
>>>>>>> +#include <drm/ttm/ttm_bo_api.h>
>>>>>>>    #include "ttm_module.h"
>>>>>>> @@ -49,9 +52,11 @@ static void ttm_global_release(void)
>>>>>>> if (--ttm_glob_use_count > 0)
>>>>>>> goto out;
>>>>>>> +ttm_pool_mgr_fini();
>>>>>>> +ttm_tt_mgr_fini();
>>>>>>> +
>>>>>>> kobject_del(&glob->kobj);
>>>>>>> kobject_put(&glob->kobj);
>>>>>>> -ttm_mem_global_release(&ttm_mem_glob);
>>>>>>> __free_page(glob->dummy_read_page);
>>>>>>> memset(glob, 0, sizeof(*glob));
>>>>>>> out:
>>>>>>> @@ -61,6 +66,8 @@ static void ttm_global_release(void)
>>>>>>> static int ttm_global_init(void)
>>>>>>> {
>>>>>>> struct ttm_global *glob = &ttm_glob;
>>>>>>> +unsigned long num_pages;
>>>>>>> +struct sysinfo si;
>>>>>>> int ret = 0;
>>>>>>> unsigned i;
>>>>>>> @@ -68,9 +75,14 @@ static int ttm_global_init(void)
>>>>>>> if (++ttm_glob_use_count > 1)
>>>>>>> goto out;
>>>>>>> -ret = ttm_mem_global_init(&ttm_mem_glob);
>>>>>>> -if (ret)
>>>>>>> -goto out;
>>>>>>> +si_meminfo(&si);
>>>>>>> +
>>>>>>> +/* Limit the number of pages in the pool to about 50% of the total
>>>>>>> + * system memory.
>>>>>>> + */
>>>>>>> +num_pages = ((u64)si.totalram * si.mem_unit) >> PAGE_SHIFT;
>>>>>>> +ttm_pool_mgr_init(num_pages * 50 / 100);
>>>>>>> +ttm_tt_mgr_init();
>>>>>>>    spin_lock_init(&glob->lru_lock);
>>>>>>> glob->dummy_read_page = alloc_page(__GFP_ZERO | GFP_DMA32);
>>>>>>> diff --git a/drivers/gpu/drm/ttm/ttm_pool.c 
>>>>>>> b/drivers/gpu/drm/ttm/ttm_pool.c
>>>>>>> index e0617717113f..6b0f957d63d5 100644
>>>>>>> --- a/drivers/gpu/drm/ttm/ttm_pool.c
>>>>>>> +++ b/drivers/gpu/drm/ttm/ttm_pool.c
>>>>>>> @@ -404,16 +404,10 @@ int ttm_pool_alloc(struct ttm_pool *pool, 
>>>>>>> struct ttm_tt *tt,
>>>>>>> caching = pages + (1 << order);
>>>>>>> }
>>>>>>> -r = ttm_mem_global_alloc_page(&ttm_mem_glob, p,
>>>>>>> -      (1 << order) * PAGE_SIZE,
>>>>>>> -      ctx);
>>>>>>> -if (r)
>>>>>>> -goto error_free_page;
>>>>>>> -
>>>>>>> if (dma_addr) {
>>>>>>> r = ttm_pool_map(pool, order, p, &dma_addr);
>>>>>>> if (r)
>>>>>>> -goto error_global_free;
>>>>>>> +goto error_free_page;
>>>>>>> }
>>>>>>>    num_pages -= 1 << order;
>>>>>>> @@ -427,9 +421,6 @@ int ttm_pool_alloc(struct ttm_pool *pool, 
>>>>>>> struct ttm_tt *tt,
>>>>>>>    return 0;
>>>>>>> -error_global_free:
>>>>>>> -ttm_mem_global_free_page(&ttm_mem_glob, p, (1 << order) * 
>>>>>>> PAGE_SIZE);
>>>>>>> -
>>>>>>> error_free_page:
>>>>>>> ttm_pool_free_page(pool, tt->caching, order, p);
>>>>>>> @@ -464,8 +455,6 @@ void ttm_pool_free(struct ttm_pool *pool, 
>>>>>>> struct ttm_tt *tt)
>>>>>>>    order = ttm_pool_page_order(pool, p);
>>>>>>> num_pages = 1ULL << order;
>>>>>>> -ttm_mem_global_free_page(&ttm_mem_glob, p,
>>>>>>> - num_pages * PAGE_SIZE);
>>>>>>> if (tt->dma_address)
>>>>>>> ttm_pool_unmap(pool, tt->dma_address[i], num_pages);
>>>>>>> diff --git a/drivers/gpu/drm/vmwgfx/Makefile 
>>>>>>> b/drivers/gpu/drm/vmwgfx/Makefile
>>>>>>> index cc4cdca7176e..8c02fa5852e7 100644
>>>>>>> --- a/drivers/gpu/drm/vmwgfx/Makefile
>>>>>>> +++ b/drivers/gpu/drm/vmwgfx/Makefile
>>>>>>> @@ -9,7 +9,7 @@ vmwgfx-y := vmwgfx_execbuf.o vmwgfx_gmr.o 
>>>>>>> vmwgfx_kms.o vmwgfx_drv.o \
>>>>>>>      vmwgfx_cotable.o vmwgfx_so.o vmwgfx_binding.o vmwgfx_msg.o \
>>>>>>>      vmwgfx_simple_resource.o vmwgfx_va.o vmwgfx_blit.o \
>>>>>>>      vmwgfx_validation.o vmwgfx_page_dirty.o 
>>>>>>> vmwgfx_streamoutput.o \
>>>>>>> -    ttm_object.o ttm_lock.o
>>>>>>> +    ttm_object.o ttm_lock.o ttm_memory.o
>>>>>>>    vmwgfx-$(CONFIG_TRANSPARENT_HUGEPAGE) += vmwgfx_thp.o
>>>>>>> obj-$(CONFIG_DRM_VMWGFX) := vmwgfx.o
>>>>>>> diff --git a/drivers/gpu/drm/ttm/ttm_memory.c 
>>>>>>> b/drivers/gpu/drm/vmwgfx/ttm_memory.c
>>>>>>> similarity index 97%
>>>>>>> rename from drivers/gpu/drm/ttm/ttm_memory.c
>>>>>>> rename to drivers/gpu/drm/vmwgfx/ttm_memory.c
>>>>>>> index 634a85c2dc4c..1306d9e0f095 100644
>>>>>>> --- a/drivers/gpu/drm/ttm/ttm_memory.c
>>>>>>> +++ b/drivers/gpu/drm/vmwgfx/ttm_memory.c
>>>>>>> @@ -28,7 +28,6 @@
>>>>>>>    #define pr_fmt(fmt) "[TTM] " fmt
>>>>>>> -#include <drm/ttm/ttm_memory.h>
>>>>>>> #include <linux/spinlock.h>
>>>>>>> #include <linux/sched.h>
>>>>>>> #include <linux/wait.h>
>>>>>>> @@ -36,10 +35,11 @@
>>>>>>> #include <linux/module.h>
>>>>>>> #include <linux/slab.h>
>>>>>>> #include <linux/swap.h>
>>>>>>> -#include <drm/ttm/ttm_pool.h>
>>>>>>> -#include <drm/ttm/ttm_tt.h>
>>>>>>> -#include "ttm_module.h"
>>>>>>> +#include <drm/drm_device.h>
>>>>>>> +#include <drm/drm_file.h>
>>>>>>> +
>>>>>>> +#include "ttm_memory.h"
>>>>>>>    #define TTM_MEMORY_ALLOC_RETRIES 4
>>>>>>> @@ -414,7 +414,7 @@ static int ttm_mem_init_dma32_zone(struct 
>>>>>>> ttm_mem_global *glob,
>>>>>>> }
>>>>>>> #endif
>>>>>>> -int ttm_mem_global_init(struct ttm_mem_global *glob)
>>>>>>> +int ttm_mem_global_init(struct ttm_mem_global *glob, struct 
>>>>>>> drm_device *dev)
>>>>>>> {
>>>>>>> struct sysinfo si;
>>>>>>> int ret;
>>>>>>> @@ -425,7 +425,8 @@ int ttm_mem_global_init(struct 
>>>>>>> ttm_mem_global *glob)
>>>>>>> glob->swap_queue = create_singlethread_workqueue("ttm_swap");
>>>>>>> INIT_WORK(&glob->work, ttm_shrink_work);
>>>>>>> ret = kobject_init_and_add(
>>>>>>> -&glob->kobj, &ttm_mem_glob_kobj_type, ttm_get_kobj(), 
>>>>>>> "memory_accounting");
>>>>>>> +&glob->kobj, &ttm_mem_glob_kobj_type, &dev->primary->kdev->kobj,
>>>>>>> +"memory_accounting");
>>>>>>> if (unlikely(ret != 0)) {
>>>>>>> kobject_put(&glob->kobj);
>>>>>>> return ret;
>>>>>>> @@ -453,8 +454,6 @@ int ttm_mem_global_init(struct 
>>>>>>> ttm_mem_global *glob)
>>>>>>> pr_info("Zone %7s: Available graphics memory: %llu KiB\n",
>>>>>>> zone->name, (unsigned long long)zone->max_mem >> 10);
>>>>>>> }
>>>>>>> -ttm_pool_mgr_init(glob->zone_kernel->max_mem/(2*PAGE_SIZE));
>>>>>>> -ttm_tt_mgr_init();
>>>>>>> return 0;
>>>>>>> out_no_zone:
>>>>>>> ttm_mem_global_release(glob);
>>>>>>> @@ -466,10 +465,6 @@ void ttm_mem_global_release(struct 
>>>>>>> ttm_mem_global *glob)
>>>>>>> struct ttm_mem_zone *zone;
>>>>>>> unsigned int i;
>>>>>>> -/* let the page allocator first stop the shrink work. */
>>>>>>> -ttm_pool_mgr_fini();
>>>>>>> -ttm_tt_mgr_fini();
>>>>>>> -
>>>>>>> flush_workqueue(glob->swap_queue);
>>>>>>> destroy_workqueue(glob->swap_queue);
>>>>>>> glob->swap_queue = NULL;
>>>>>>> diff --git a/include/drm/ttm/ttm_memory.h 
>>>>>>> b/drivers/gpu/drm/vmwgfx/ttm_memory.h
>>>>>>> similarity index 97%
>>>>>>> rename from include/drm/ttm/ttm_memory.h
>>>>>>> rename to drivers/gpu/drm/vmwgfx/ttm_memory.h
>>>>>>> index c1f167881e33..850ee6c867da 100644
>>>>>>> --- a/include/drm/ttm/ttm_memory.h
>>>>>>> +++ b/drivers/gpu/drm/vmwgfx/ttm_memory.h
>>>>>>> @@ -35,7 +35,8 @@
>>>>>>> #include <linux/errno.h>
>>>>>>> #include <linux/kobject.h>
>>>>>>> #include <linux/mm.h>
>>>>>>> -#include "ttm_bo_api.h"
>>>>>>> +
>>>>>>> +#include <drm/ttm/ttm_bo_api.h>
>>>>>>>    /**
>>>>>>>   * struct ttm_mem_global - Global memory accounting structure.
>>>>>>> @@ -79,7 +80,7 @@ extern struct ttm_mem_global {
>>>>>>> #endif
>>>>>>> } ttm_mem_glob;
>>>>>>> -int ttm_mem_global_init(struct ttm_mem_global *glob);
>>>>>>> +int ttm_mem_global_init(struct ttm_mem_global *glob, struct 
>>>>>>> drm_device *dev);
>>>>>>> void ttm_mem_global_release(struct ttm_mem_global *glob);
>>>>>>> int ttm_mem_global_alloc(struct ttm_mem_global *glob, uint64_t 
>>>>>>> memory,
>>>>>>>   struct ttm_operation_ctx *ctx);
>>>>>>> diff --git a/drivers/gpu/drm/vmwgfx/ttm_object.h 
>>>>>>> b/drivers/gpu/drm/vmwgfx/ttm_object.h
>>>>>>> index ede26df87c93..49b064f0cb19 100644
>>>>>>> --- a/drivers/gpu/drm/vmwgfx/ttm_object.h
>>>>>>> +++ b/drivers/gpu/drm/vmwgfx/ttm_object.h
>>>>>>> @@ -43,7 +43,8 @@
>>>>>>> #include <linux/rcupdate.h>
>>>>>>>    #include <drm/drm_hashtab.h>
>>>>>>> -#include <drm/ttm/ttm_memory.h>
>>>>>>> +
>>>>>>> +#include "ttm_memory.h"
>>>>>>>    /**
>>>>>>>   * enum ttm_ref_type
>>>>>>> diff --git a/drivers/gpu/drm/vmwgfx/vmwgfx_bo.c 
>>>>>>> b/drivers/gpu/drm/vmwgfx/vmwgfx_bo.c
>>>>>>> index 6b3bfd8c678a..50e529a01677 100644
>>>>>>> --- a/drivers/gpu/drm/vmwgfx/vmwgfx_bo.c
>>>>>>> +++ b/drivers/gpu/drm/vmwgfx/vmwgfx_bo.c
>>>>>>> @@ -507,11 +507,16 @@ int vmw_bo_create_kernel(struct 
>>>>>>> vmw_private *dev_priv, unsigned long size,
>>>>>>> acc_size = ttm_round_pot(sizeof(*bo));
>>>>>>> acc_size += ttm_round_pot(npages * sizeof(void *));
>>>>>>> acc_size += ttm_round_pot(sizeof(struct ttm_tt));
>>>>>>> +
>>>>>>> +ret = ttm_mem_global_alloc(&ttm_mem_glob, acc_size, &ctx);
>>>>>>> +if (unlikely(ret))
>>>>>>> +goto error_free;
>>>>>>> +
>>>>>>> ret = ttm_bo_init_reserved(&dev_priv->bdev, bo, size,
>>>>>>>     ttm_bo_type_device, placement, 0,
>>>>>>> -   &ctx, acc_size, NULL, NULL, NULL);
>>>>>>> +   &ctx, NULL, NULL, NULL);
>>>>>>> if (unlikely(ret))
>>>>>>> -goto error_free;
>>>>>>> +goto error_account;
>>>>>>>    ttm_bo_pin(bo);
>>>>>>> ttm_bo_unreserve(bo);
>>>>>>> @@ -519,6 +524,9 @@ int vmw_bo_create_kernel(struct vmw_private 
>>>>>>> *dev_priv, unsigned long size,
>>>>>>>    return 0;
>>>>>>> +error_account:
>>>>>>> +ttm_mem_global_free(&ttm_mem_glob, acc_size);
>>>>>>> +
>>>>>>> error_free:
>>>>>>> kfree(bo);
>>>>>>> return ret;
>>>>>>> @@ -558,11 +566,17 @@ int vmw_bo_init(struct vmw_private *dev_priv,
>>>>>>> vmw_bo->base.priority = 3;
>>>>>>> vmw_bo->res_tree = RB_ROOT;
>>>>>>> +ret = ttm_mem_global_alloc(&ttm_mem_glob, acc_size, &ctx);
>>>>>>> +if (unlikely(ret))
>>>>>>> +return ret;
>>>>>>> +
>>>>>>> ret = ttm_bo_init_reserved(bdev, &vmw_bo->base, size,
>>>>>>>     ttm_bo_type_device, placement,
>>>>>>> -   0, &ctx, acc_size, NULL, NULL, bo_free);
>>>>>>> -if (unlikely(ret))
>>>>>>> +   0, &ctx, NULL, NULL, bo_free);
>>>>>>> +if (unlikely(ret)) {
>>>>>>> +ttm_mem_global_free(&ttm_mem_glob, acc_size);
>>>>>>> return ret;
>>>>>>> +}
>>>>>>>    if (pin)
>>>>>>> ttm_bo_pin(&vmw_bo->base);
>>>>>>> diff --git a/drivers/gpu/drm/vmwgfx/vmwgfx_drv.c 
>>>>>>> b/drivers/gpu/drm/vmwgfx/vmwgfx_drv.c
>>>>>>> index 710ba5169a74..6c0ca1011629 100644
>>>>>>> --- a/drivers/gpu/drm/vmwgfx/vmwgfx_drv.c
>>>>>>> +++ b/drivers/gpu/drm/vmwgfx/vmwgfx_drv.c
>>>>>>> @@ -1268,6 +1268,7 @@ static void vmw_remove(struct pci_dev *pdev)
>>>>>>> {
>>>>>>> struct drm_device *dev = pci_get_drvdata(pdev);
>>>>>>> +ttm_mem_global_release(&ttm_mem_glob);
>>>>>>> drm_dev_unregister(dev);
>>>>>>> vmw_driver_unload(dev);
>>>>>>> }
>>>>>>> @@ -1518,6 +1519,10 @@ static int vmw_probe(struct pci_dev 
>>>>>>> *pdev, const struct pci_device_id *ent)
>>>>>>>    pci_set_drvdata(pdev, &vmw->drm);
>>>>>>> +ret = ttm_mem_global_init(&ttm_mem_glob, &vmw->drm);
>>>>>>> +if (ret)
>>>>>>> +return ret;
>>>>>>> +
>>>>>>> ret = vmw_driver_load(vmw, ent->device);
>>>>>>> if (ret)
>>>>>>> return ret;
>>>>>>> diff --git a/drivers/gpu/drm/vmwgfx/vmwgfx_ttm_buffer.c 
>>>>>>> b/drivers/gpu/drm/vmwgfx/vmwgfx_ttm_buffer.c
>>>>>>> index d1bfa59579f1..63f10c865061 100644
>>>>>>> --- a/drivers/gpu/drm/vmwgfx/vmwgfx_ttm_buffer.c
>>>>>>> +++ b/drivers/gpu/drm/vmwgfx/vmwgfx_ttm_buffer.c
>>>>>>> @@ -576,11 +576,31 @@ static void vmw_ttm_destroy(struct 
>>>>>>> ttm_device *bdev, struct ttm_tt *ttm)
>>>>>>> static int vmw_ttm_populate(struct ttm_device *bdev,
>>>>>>>      struct ttm_tt *ttm, struct ttm_operation_ctx *ctx)
>>>>>>> {
>>>>>>> +unsigned int i;
>>>>>>> +int ret;
>>>>>>> +
>>>>>>> /* TODO: maybe completely drop this ? */
>>>>>>> if (ttm_tt_is_populated(ttm))
>>>>>>> return 0;
>>>>>>> -return ttm_pool_alloc(&bdev->pool, ttm, ctx);
>>>>>>> +ret = ttm_pool_alloc(&bdev->pool, ttm, ctx);
>>>>>>> +if (ret)
>>>>>>> +return ret;
>>>>>>> +
>>>>>>> +for (i = 0; i < ttm->num_pages; ++i) {
>>>>>>> +ret = ttm_mem_global_alloc_page(&ttm_mem_glob, ttm->pages[i],
>>>>>>> +PAGE_SIZE, ctx);
>>>>>>> +if (ret)
>>>>>>> +goto error;
>>>>>>> +}
>>>>>>> +return 0;
>>>>>>> +
>>>>>>> +error:
>>>>>>> +while (i--)
>>>>>>> +ttm_mem_global_free_page(&ttm_mem_glob, ttm->pages[i],
>>>>>>> + PAGE_SIZE);
>>>>>>> +ttm_pool_free(&bdev->pool, ttm);
>>>>>>> +return ret;
>>>>>>> }
>>>>>>>    static void vmw_ttm_unpopulate(struct ttm_device *bdev,
>>>>>>> @@ -588,6 +608,7 @@ static void vmw_ttm_unpopulate(struct 
>>>>>>> ttm_device *bdev,
>>>>>>> {
>>>>>>> struct vmw_ttm_tt *vmw_tt = container_of(ttm, struct vmw_ttm_tt,
>>>>>>>   dma_ttm);
>>>>>>> +unsigned int i;
>>>>>>>    if (vmw_tt->mob) {
>>>>>>> vmw_mob_destroy(vmw_tt->mob);
>>>>>>> @@ -595,6 +616,11 @@ static void vmw_ttm_unpopulate(struct 
>>>>>>> ttm_device *bdev,
>>>>>>> }
>>>>>>>    vmw_ttm_unmap_dma(vmw_tt);
>>>>>>> +
>>>>>>> +for (i = 0; i < ttm->num_pages; ++i)
>>>>>>> +ttm_mem_global_free_page(&ttm_mem_glob, ttm->pages[i],
>>>>>>> + PAGE_SIZE);
>>>>>>> +
>>>>>>> ttm_pool_free(&bdev->pool, ttm);
>>>>>>> }
>>>>>>> diff --git a/include/drm/ttm/ttm_bo_api.h 
>>>>>>> b/include/drm/ttm/ttm_bo_api.h
>>>>>>> index 1297a8fb7ccb..4fb523dfab32 100644
>>>>>>> --- a/include/drm/ttm/ttm_bo_api.h
>>>>>>> +++ b/include/drm/ttm/ttm_bo_api.h
>>>>>>> @@ -88,7 +88,6 @@ struct ttm_tt;
>>>>>>>   * @type: The bo type.
>>>>>>>   * @destroy: Destruction function. If NULL, kfree is used.
>>>>>>>   * @num_pages: Actual number of pages.
>>>>>>> - * @acc_size: Accounted size for this object.
>>>>>>>   * @kref: Reference count of this buffer object. When this 
>>>>>>> refcount reaches
>>>>>>>   * zero, the object is destroyed or put on the delayed delete 
>>>>>>> list.
>>>>>>>   * @mem: structure describing current placement.
>>>>>>> @@ -125,7 +124,6 @@ struct ttm_buffer_object {
>>>>>>> struct ttm_device *bdev;
>>>>>>> enum ttm_bo_type type;
>>>>>>> void (*destroy) (struct ttm_buffer_object *);
>>>>>>> -size_t acc_size;
>>>>>>>    /**
>>>>>>> * Members not needing protection.
>>>>>>> @@ -357,10 +355,6 @@ void ttm_bo_unlock_delayed_workqueue(struct 
>>>>>>> ttm_device *bdev, int resched);
>>>>>>> bool ttm_bo_eviction_valuable(struct ttm_buffer_object *bo,
>>>>>>>        const struct ttm_place *place);
>>>>>>> -size_t ttm_bo_dma_acc_size(struct ttm_device *bdev,
>>>>>>> -   unsigned long bo_size,
>>>>>>> -   unsigned struct_size);
>>>>>>> -
>>>>>>> /**
>>>>>>>   * ttm_bo_init_reserved
>>>>>>>   *
>>>>>>> @@ -371,7 +365,6 @@ size_t ttm_bo_dma_acc_size(struct ttm_device 
>>>>>>> *bdev,
>>>>>>>   * @flags: Initial placement flags.
>>>>>>>   * @page_alignment: Data alignment in pages.
>>>>>>>   * @ctx: TTM operation context for memory allocation.
>>>>>>> - * @acc_size: Accounted size for this object.
>>>>>>>   * @resv: Pointer to a dma_resv, or NULL to let ttm allocate one.
>>>>>>>   * @destroy: Destroy function. Use NULL for kfree().
>>>>>>>   *
>>>>>>> @@ -402,8 +395,7 @@ int ttm_bo_init_reserved(struct ttm_device 
>>>>>>> *bdev,
>>>>>>>   struct ttm_placement *placement,
>>>>>>>   uint32_t page_alignment,
>>>>>>>   struct ttm_operation_ctx *ctx,
>>>>>>> - size_t acc_size, struct sg_table *sg,
>>>>>>> - struct dma_resv *resv,
>>>>>>> + struct sg_table *sg, struct dma_resv *resv,
>>>>>>>   void (*destroy) (struct ttm_buffer_object *));
>>>>>>>    /**
>>>>>>> @@ -421,7 +413,6 @@ int ttm_bo_init_reserved(struct ttm_device 
>>>>>>> *bdev,
>>>>>>>   * holds a pointer to a persistent shmem object. Typically, 
>>>>>>> this would
>>>>>>>   * point to the shmem object backing a GEM object if TTM is 
>>>>>>> used to back a
>>>>>>>   * GEM user interface.
>>>>>>> - * @acc_size: Accounted size for this object.
>>>>>>>   * @resv: Pointer to a dma_resv, or NULL to let ttm allocate one.
>>>>>>>   * @destroy: Destroy function. Use NULL for kfree().
>>>>>>>   *
>>>>>>> @@ -446,7 +437,7 @@ int ttm_bo_init_reserved(struct ttm_device 
>>>>>>> *bdev,
>>>>>>> int ttm_bo_init(struct ttm_device *bdev, struct 
>>>>>>> ttm_buffer_object *bo,
>>>>>>> size_t size, enum ttm_bo_type type,
>>>>>>> struct ttm_placement *placement,
>>>>>>> -uint32_t page_alignment, bool interrubtible, size_t acc_size,
>>>>>>> +uint32_t page_alignment, bool interrubtible,
>>>>>>> struct sg_table *sg, struct dma_resv *resv,
>>>>>>> void (*destroy) (struct ttm_buffer_object *));
>>>>>>> diff --git a/include/drm/ttm/ttm_bo_driver.h 
>>>>>>> b/include/drm/ttm/ttm_bo_driver.h
>>>>>>> index 1c9bf993e252..8959c0075cfd 100644
>>>>>>> --- a/include/drm/ttm/ttm_bo_driver.h
>>>>>>> +++ b/include/drm/ttm/ttm_bo_driver.h
>>>>>>> @@ -40,7 +40,6 @@
>>>>>>> #include <drm/ttm/ttm_device.h>
>>>>>>>    #include "ttm_bo_api.h"
>>>>>>> -#include "ttm_memory.h"
>>>>>>> #include "ttm_placement.h"
>>>>>>> #include "ttm_tt.h"
>>>>>>> #include "ttm_pool.h"
>>>>>>> diff --git a/include/drm/ttm/ttm_tt.h b/include/drm/ttm/ttm_tt.h
>>>>>>> index cce57fb49e2c..069f8130241a 100644
>>>>>>> --- a/include/drm/ttm/ttm_tt.h
>>>>>>> +++ b/include/drm/ttm/ttm_tt.h
>>>>>>> @@ -30,6 +30,7 @@
>>>>>>> #include <linux/types.h>
>>>>>>> #include <drm/ttm/ttm_caching.h>
>>>>>>> +struct ttm_bo_device;
>>>>>>> struct ttm_tt;
>>>>>>> struct ttm_resource;
>>>>>>> struct ttm_buffer_object;
>>>
>>>
>>> ----------
>>>
>>> You're receiving this message because you're a member of the 
>>> Linux-graphics-maintainer group from VMware, Inc..
>>>
>>> Leave group:
>>> https://nam11.safelinks.protection.outlook.com/?url=https%3A%2F%2Foutlook.office365.com%2Fowa%2FLinux-graphics-maintainer%40vmware.com%2Fgroupsubscription.ashx%3Fsource%3DEscalatedMessage%26action%3Dleave%26GuestId%3D69d3bf6f-5242-4be4-b863-b7949752f363&amp;data=04%7C01%7Cchristian.koenig%40amd.com%7Cccf900e6d23648ef0b0808d8c7edb760%7C3dd8961fe4884e608e11a82d994e183d%7C0%7C0%7C637479171103384072%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C1000&amp;sdata=f8Y%2ByzBnt9Gxxmm7XZ3hpZqTdbY05og9yloArLCLIx0%3D&amp;reserved=0 
>>>
>>> _______________________________________________
>>> Sent to linux-graphics-maintainer@vmware.com
>

_______________________________________________
dri-devel mailing list
dri-devel@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/dri-devel

^ permalink raw reply	[flat|nested] 17+ messages in thread

* Re: [Linux-graphics-maintainer] [PATCH 2/3] drm/ttm: move memory accounting into vmwgfx v3
  2021-02-08 13:35               ` Christian König
@ 2021-02-08 13:39                 ` Daniel Vetter
  2021-02-08 20:21                 ` Zack Rusin
  1 sibling, 0 replies; 17+ messages in thread
From: Daniel Vetter @ 2021-02-08 13:39 UTC (permalink / raw)
  To: Christian König; +Cc: Das, Nirmoy, Linux-graphics-maintainer, dri-devel

On Mon, Feb 8, 2021 at 2:35 PM Christian König <christian.koenig@amd.com> wrote:
>
> Hi Zack,
>
> ok we figured out how to do this correctly.
>
> Basically using the pdev->kobj instead of the drm->primary->kdev->kobj
> pointer worked quite well.

Note drm_device->pdev is gone, so make sure everything still compiles.
You need drm_device->dev->kobj now (they've always pointed at the same
object anyway, just different casts for historical reasons).
-Daniel

> I've just send the latest patches to the mailing list. If you don't have
> any objections I will commit that tomorrow with your and Daniels rb.
>
> Thanks,
> Christian.
>
> Am 03.02.21 um 09:20 schrieb Christian König:
> > Hi Zack,
> >
> > thanks I can take over again from here on.
> >
> > Quite busy today, but I think I can go over the code once more tomorrow.
> >
> > Thanks for the help,
> > Christian.
> >
> > Am 03.02.21 um 03:45 schrieb Zack Rusin:
> >> Just had a quick peek. The issue is that you can’t attach to the drm
> >> device (card0) because it hasn’t been registered yet (drm device
> >> registration is last in the vmw_probe in vmwgfx_drv.c via the
> >> drm_dev_register). So dev->primary->kdev->kobj that you’re using as
> >> argument to kobject_init_and_add in ttm_mem_global_init hasn’t been
> >> initialized yet. So that particular sysfs code would likely have to
> >> be refactored out of ttm_mem_global_init to another function that
> >> could be called after drm registraction. I could take this on but not
> >> until Friday or so.
> >>
> >> z
> >>
> >>
> >>> On Feb 2, 2021, at 12:42, Zack Rusin <zackr@vmware.com> wrote:
> >>>
> >>> Ah, yes, sorry, I missed that. I just double checked and it fails with:
> >>>
> >>> kobject_add_internal failed for memory_accounting (error: -2 parent:
> >>> card0)
> >>>
> >>> which breaks the probe and the driver won’t load. I won’t have time
> >>> to look into it until tomorrow though.
> >>>
> >>> z
> >>>
> >>>> On Feb 2, 2021, at 10:16, Christian König
> >>>> <christian.koenig@amd.com> wrote:
> >>>>
> >>>> Hi Zack,
> >>>>
> >>>> can you also give it a quick smoke test?
> >>>>
> >>>> I'm not sure if I wired up all the sysfs magic correctly inside
> >>>> vmwgfx, but I currently don't have a setup where I can test this.
> >>>>
> >>>> Thanks,
> >>>> Christian.
> >>>>
> >>>> Am 02.02.21 um 16:14 schrieb Zack Rusin:
> >>>>> Looks good. There’s probably not much reason to call it ttm_memory
> >>>>> anymore as it only deals with ttm_mem_glob, we’ll likely fold it
> >>>>> in after you submit. Thanks.
> >>>>>
> >>>>> Reviewed-by: Zack Rusin <zackr@vmware.com>
> >>>>>
> >>>>> z
> >>>>>
> >>>>>> On Feb 2, 2021, at 08:04, Christian König
> >>>>>> <christian.koenig@amd.com> wrote:
> >>>>>>
> >>>>>> Ping?
> >>>>>>
> >>>>>> Especially Roland and Zack do you have any objections to this?
> >>>>>>
> >>>>>> Regards,
> >>>>>> Christian.
> >>>>>>
> >>>>>> Am 28.01.21 um 14:16 schrieb Christian König:
> >>>>>>> This is just another feature which is only used by VMWGFX, so move
> >>>>>>> it into the driver instead.
> >>>>>>>
> >>>>>>> I've tried to add the accounting sysfs file to the kobject of
> >>>>>>> the drm
> >>>>>>> minor, but I'm not 100% sure if this works as expected.
> >>>>>>>
> >>>>>>> v2: fix typo in KFD and avoid 64bit divide
> >>>>>>> v3: fix init order in VMWGFX
> >>>>>>>
> >>>>>>> Signed-off-by: Christian König <christian.koenig@amd.com>
> >>>>>>> ---
> >>>>>>> .../gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c  | 16 ++++++---
> >>>>>>> drivers/gpu/drm/amd/amdgpu/amdgpu_object.c    |  8 ++---
> >>>>>>> drivers/gpu/drm/drm_gem_vram_helper.c         |  6 ++--
> >>>>>>> drivers/gpu/drm/nouveau/nouveau_bo.c          |  7 ++--
> >>>>>>> drivers/gpu/drm/nouveau/nouveau_drv.h         |  1 -
> >>>>>>> drivers/gpu/drm/qxl/qxl_object.c              |  4 +--
> >>>>>>> drivers/gpu/drm/radeon/radeon_object.c        |  8 ++---
> >>>>>>> drivers/gpu/drm/ttm/Makefile                  |  7 ++--
> >>>>>>> drivers/gpu/drm/ttm/ttm_bo.c                  | 33
> >>>>>>> +------------------
> >>>>>>> drivers/gpu/drm/ttm/ttm_bo_util.c             |  1 -
> >>>>>>> drivers/gpu/drm/ttm/ttm_device.c              | 22 ++++++++++---
> >>>>>>> drivers/gpu/drm/ttm/ttm_pool.c                | 13 +-------
> >>>>>>> drivers/gpu/drm/vmwgfx/Makefile               |  2 +-
> >>>>>>> drivers/gpu/drm/{ttm => vmwgfx}/ttm_memory.c  | 19 ++++-------
> >>>>>>> .../gpu/drm/vmwgfx}/ttm_memory.h              |  5 +--
> >>>>>>> drivers/gpu/drm/vmwgfx/ttm_object.h           |  3 +-
> >>>>>>> drivers/gpu/drm/vmwgfx/vmwgfx_bo.c            | 22 ++++++++++---
> >>>>>>> drivers/gpu/drm/vmwgfx/vmwgfx_drv.c           |  5 +++
> >>>>>>> drivers/gpu/drm/vmwgfx/vmwgfx_ttm_buffer.c    | 28 +++++++++++++++-
> >>>>>>> include/drm/ttm/ttm_bo_api.h                  | 13 ++------
> >>>>>>> include/drm/ttm/ttm_bo_driver.h               |  1 -
> >>>>>>> include/drm/ttm/ttm_tt.h                      |  1 +
> >>>>>>> 22 files changed, 110 insertions(+), 115 deletions(-)
> >>>>>>> rename drivers/gpu/drm/{ttm => vmwgfx}/ttm_memory.c (97%)
> >>>>>>> rename {include/drm/ttm => drivers/gpu/drm/vmwgfx}/ttm_memory.h
> >>>>>>> (97%)
> >>>>>>>
> >>>>>>> diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c
> >>>>>>> b/drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c
> >>>>>>> index 0849b68e784f..e440af37dde8 100644
> >>>>>>> --- a/drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c
> >>>>>>> +++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c
> >>>>>>> @@ -118,6 +118,16 @@ void amdgpu_amdkfd_gpuvm_init_mem_limits(void)
> >>>>>>>   */
> >>>>>>> #define ESTIMATE_PT_SIZE(mem_size) ((mem_size) >> 14)
> >>>>>>> +static size_t amdgpu_amdkfd_acc_size(uint64_t size)
> >>>>>>> +{
> >>>>>>> +size >>= PAGE_SHIFT;
> >>>>>>> +size *= sizeof(dma_addr_t) + sizeof(void *);
> >>>>>>> +
> >>>>>>> +return __roundup_pow_of_two(sizeof(struct amdgpu_bo)) +
> >>>>>>> +__roundup_pow_of_two(sizeof(struct ttm_tt)) +
> >>>>>>> +PAGE_ALIGN(size);
> >>>>>>> +}
> >>>>>>> +
> >>>>>>> static int amdgpu_amdkfd_reserve_mem_limit(struct amdgpu_device
> >>>>>>> *adev,
> >>>>>>> uint64_t size, u32 domain, bool sg)
> >>>>>>> {
> >>>>>>> @@ -126,8 +136,7 @@ static int
> >>>>>>> amdgpu_amdkfd_reserve_mem_limit(struct amdgpu_device *adev,
> >>>>>>> size_t acc_size, system_mem_needed, ttm_mem_needed, vram_needed;
> >>>>>>> int ret = 0;
> >>>>>>> -acc_size = ttm_bo_dma_acc_size(&adev->mman.bdev, size,
> >>>>>>> -       sizeof(struct amdgpu_bo));
> >>>>>>> +acc_size = amdgpu_amdkfd_acc_size(size);
> >>>>>>>    vram_needed = 0;
> >>>>>>> if (domain == AMDGPU_GEM_DOMAIN_GTT) {
> >>>>>>> @@ -174,8 +183,7 @@ static void unreserve_mem_limit(struct
> >>>>>>> amdgpu_device *adev,
> >>>>>>> {
> >>>>>>> size_t acc_size;
> >>>>>>> -acc_size = ttm_bo_dma_acc_size(&adev->mman.bdev, size,
> >>>>>>> -       sizeof(struct amdgpu_bo));
> >>>>>>> +acc_size = amdgpu_amdkfd_acc_size(size);
> >>>>>>>    spin_lock(&kfd_mem_limit.mem_limit_lock);
> >>>>>>> if (domain == AMDGPU_GEM_DOMAIN_GTT) {
> >>>>>>> diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_object.c
> >>>>>>> b/drivers/gpu/drm/amd/amdgpu/amdgpu_object.c
> >>>>>>> index 6cc9919b12cc..599c9a132eb6 100644
> >>>>>>> --- a/drivers/gpu/drm/amd/amdgpu/amdgpu_object.c
> >>>>>>> +++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_object.c
> >>>>>>> @@ -523,7 +523,6 @@ static int amdgpu_bo_do_create(struct
> >>>>>>> amdgpu_device *adev,
> >>>>>>> };
> >>>>>>> struct amdgpu_bo *bo;
> >>>>>>> unsigned long page_align, size = bp->size;
> >>>>>>> -size_t acc_size;
> >>>>>>> int r;
> >>>>>>>    /* Note that GDS/GWS/OA allocates 1 page per byte/resource. */
> >>>>>>> @@ -546,9 +545,6 @@ static int amdgpu_bo_do_create(struct
> >>>>>>> amdgpu_device *adev,
> >>>>>>>    *bo_ptr = NULL;
> >>>>>>> -acc_size = ttm_bo_dma_acc_size(&adev->mman.bdev, size,
> >>>>>>> -       sizeof(struct amdgpu_bo));
> >>>>>>> -
> >>>>>>> bo = kzalloc(sizeof(struct amdgpu_bo), GFP_KERNEL);
> >>>>>>> if (bo == NULL)
> >>>>>>> return -ENOMEM;
> >>>>>>> @@ -577,8 +573,8 @@ static int amdgpu_bo_do_create(struct
> >>>>>>> amdgpu_device *adev,
> >>>>>>> bo->tbo.priority = 1;
> >>>>>>>    r = ttm_bo_init_reserved(&adev->mman.bdev, &bo->tbo, size,
> >>>>>>> bp->type,
> >>>>>>> - &bo->placement, page_align, &ctx, acc_size,
> >>>>>>> - NULL, bp->resv, &amdgpu_bo_destroy);
> >>>>>>> + &bo->placement, page_align, &ctx,  NULL,
> >>>>>>> + bp->resv, &amdgpu_bo_destroy);
> >>>>>>> if (unlikely(r != 0))
> >>>>>>> return r;
> >>>>>>> diff --git a/drivers/gpu/drm/drm_gem_vram_helper.c
> >>>>>>> b/drivers/gpu/drm/drm_gem_vram_helper.c
> >>>>>>> index 0b13c8507688..a0992f0b8afd 100644
> >>>>>>> --- a/drivers/gpu/drm/drm_gem_vram_helper.c
> >>>>>>> +++ b/drivers/gpu/drm/drm_gem_vram_helper.c
> >>>>>>> @@ -189,7 +189,6 @@ struct drm_gem_vram_object
> >>>>>>> *drm_gem_vram_create(struct drm_device *dev,
> >>>>>>> struct drm_vram_mm *vmm = dev->vram_mm;
> >>>>>>> struct ttm_device *bdev;
> >>>>>>> int ret;
> >>>>>>> -size_t acc_size;
> >>>>>>>    if (WARN_ONCE(!vmm, "VRAM MM not initialized"))
> >>>>>>> return ERR_PTR(-EINVAL);
> >>>>>>> @@ -216,7 +215,6 @@ struct drm_gem_vram_object
> >>>>>>> *drm_gem_vram_create(struct drm_device *dev,
> >>>>>>> }
> >>>>>>>    bdev = &vmm->bdev;
> >>>>>>> -acc_size = ttm_bo_dma_acc_size(bdev, size, sizeof(*gbo));
> >>>>>>>    gbo->bo.bdev = bdev;
> >>>>>>> drm_gem_vram_placement(gbo, DRM_GEM_VRAM_PL_FLAG_SYSTEM);
> >>>>>>> @@ -226,8 +224,8 @@ struct drm_gem_vram_object
> >>>>>>> *drm_gem_vram_create(struct drm_device *dev,
> >>>>>>>   * to release gbo->bo.base and kfree gbo.
> >>>>>>>   */
> >>>>>>> ret = ttm_bo_init(bdev, &gbo->bo, size, ttm_bo_type_device,
> >>>>>>> -  &gbo->placement, pg_align, false, acc_size,
> >>>>>>> -  NULL, NULL, ttm_buffer_object_destroy);
> >>>>>>> +  &gbo->placement, pg_align, false, NULL, NULL,
> >>>>>>> +  ttm_buffer_object_destroy);
> >>>>>>> if (ret)
> >>>>>>> return ERR_PTR(ret);
> >>>>>>> diff --git a/drivers/gpu/drm/nouveau/nouveau_bo.c
> >>>>>>> b/drivers/gpu/drm/nouveau/nouveau_bo.c
> >>>>>>> index c177940d6e2c..ca2a8ae1938e 100644
> >>>>>>> --- a/drivers/gpu/drm/nouveau/nouveau_bo.c
> >>>>>>> +++ b/drivers/gpu/drm/nouveau/nouveau_bo.c
> >>>>>>> @@ -300,18 +300,15 @@ nouveau_bo_init(struct nouveau_bo *nvbo,
> >>>>>>> u64 size, int align, u32 domain,
> >>>>>>> struct sg_table *sg, struct dma_resv *robj)
> >>>>>>> {
> >>>>>>> int type = sg ? ttm_bo_type_sg : ttm_bo_type_device;
> >>>>>>> -size_t acc_size;
> >>>>>>> int ret;
> >>>>>>> -acc_size = ttm_bo_dma_acc_size(nvbo->bo.bdev, size,
> >>>>>>> sizeof(*nvbo));
> >>>>>>> -
> >>>>>>> nvbo->bo.mem.num_pages = size >> PAGE_SHIFT;
> >>>>>>> nouveau_bo_placement_set(nvbo, domain, 0);
> >>>>>>> INIT_LIST_HEAD(&nvbo->io_reserve_lru);
> >>>>>>>    ret = ttm_bo_init(nvbo->bo.bdev, &nvbo->bo, size, type,
> >>>>>>> -  &nvbo->placement, align >> PAGE_SHIFT, false,
> >>>>>>> -  acc_size, sg, robj, nouveau_bo_del_ttm);
> >>>>>>> +  &nvbo->placement, align >> PAGE_SHIFT, false, sg,
> >>>>>>> +  robj, nouveau_bo_del_ttm);
> >>>>>>> if (ret) {
> >>>>>>> /* ttm will call nouveau_bo_del_ttm if it fails.. */
> >>>>>>> return ret;
> >>>>>>> diff --git a/drivers/gpu/drm/nouveau/nouveau_drv.h
> >>>>>>> b/drivers/gpu/drm/nouveau/nouveau_drv.h
> >>>>>>> index edf9d1ee9d58..a491c2c1c56e 100644
> >>>>>>> --- a/drivers/gpu/drm/nouveau/nouveau_drv.h
> >>>>>>> +++ b/drivers/gpu/drm/nouveau/nouveau_drv.h
> >>>>>>> @@ -54,7 +54,6 @@
> >>>>>>> #include <drm/ttm/ttm_bo_api.h>
> >>>>>>> #include <drm/ttm/ttm_bo_driver.h>
> >>>>>>> #include <drm/ttm/ttm_placement.h>
> >>>>>>> -#include <drm/ttm/ttm_memory.h>
> >>>>>>>    #include <drm/drm_audio_component.h>
> >>>>>>> diff --git a/drivers/gpu/drm/qxl/qxl_object.c
> >>>>>>> b/drivers/gpu/drm/qxl/qxl_object.c
> >>>>>>> index ceebc5881f68..705b51535492 100644
> >>>>>>> --- a/drivers/gpu/drm/qxl/qxl_object.c
> >>>>>>> +++ b/drivers/gpu/drm/qxl/qxl_object.c
> >>>>>>> @@ -138,8 +138,8 @@ int qxl_bo_create(struct qxl_device *qdev,
> >>>>>>> qxl_ttm_placement_from_domain(bo, domain);
> >>>>>>>    r = ttm_bo_init_reserved(&qdev->mman.bdev, &bo->tbo, size, type,
> >>>>>>> - &bo->placement, 0, &ctx, size,
> >>>>>>> - NULL, NULL, &qxl_ttm_bo_destroy);
> >>>>>>> + &bo->placement, 0, &ctx, NULL, NULL,
> >>>>>>> + &qxl_ttm_bo_destroy);
> >>>>>>> if (unlikely(r != 0)) {
> >>>>>>> if (r != -ERESTARTSYS)
> >>>>>>> dev_err(qdev->ddev.dev,
> >>>>>>> diff --git a/drivers/gpu/drm/radeon/radeon_object.c
> >>>>>>> b/drivers/gpu/drm/radeon/radeon_object.c
> >>>>>>> index 6a336284466f..804f7a427be7 100644
> >>>>>>> --- a/drivers/gpu/drm/radeon/radeon_object.c
> >>>>>>> +++ b/drivers/gpu/drm/radeon/radeon_object.c
> >>>>>>> @@ -159,7 +159,6 @@ int radeon_bo_create(struct radeon_device
> >>>>>>> *rdev,
> >>>>>>> struct radeon_bo *bo;
> >>>>>>> enum ttm_bo_type type;
> >>>>>>> unsigned long page_align = roundup(byte_align, PAGE_SIZE) >>
> >>>>>>> PAGE_SHIFT;
> >>>>>>> -size_t acc_size;
> >>>>>>> int r;
> >>>>>>>    size = ALIGN(size, PAGE_SIZE);
> >>>>>>> @@ -173,9 +172,6 @@ int radeon_bo_create(struct radeon_device
> >>>>>>> *rdev,
> >>>>>>> }
> >>>>>>> *bo_ptr = NULL;
> >>>>>>> -acc_size = ttm_bo_dma_acc_size(&rdev->mman.bdev, size,
> >>>>>>> -       sizeof(struct radeon_bo));
> >>>>>>> -
> >>>>>>> bo = kzalloc(sizeof(struct radeon_bo), GFP_KERNEL);
> >>>>>>> if (bo == NULL)
> >>>>>>> return -ENOMEM;
> >>>>>>> @@ -230,8 +226,8 @@ int radeon_bo_create(struct radeon_device
> >>>>>>> *rdev,
> >>>>>>> /* Kernel allocation are uninterruptible */
> >>>>>>> down_read(&rdev->pm.mclk_lock);
> >>>>>>> r = ttm_bo_init(&rdev->mman.bdev, &bo->tbo, size, type,
> >>>>>>> -&bo->placement, page_align, !kernel, acc_size,
> >>>>>>> -sg, resv, &radeon_ttm_bo_destroy);
> >>>>>>> +&bo->placement, page_align, !kernel, sg, resv,
> >>>>>>> +&radeon_ttm_bo_destroy);
> >>>>>>> up_read(&rdev->pm.mclk_lock);
> >>>>>>> if (unlikely(r != 0)) {
> >>>>>>> return r;
> >>>>>>> diff --git a/drivers/gpu/drm/ttm/Makefile
> >>>>>>> b/drivers/gpu/drm/ttm/Makefile
> >>>>>>> index 8e6437eadabe..40e5e9da7953 100644
> >>>>>>> --- a/drivers/gpu/drm/ttm/Makefile
> >>>>>>> +++ b/drivers/gpu/drm/ttm/Makefile
> >>>>>>> @@ -2,10 +2,9 @@
> >>>>>>> #
> >>>>>>> # Makefile for the drm device driver.  This driver provides
> >>>>>>> support for the
> >>>>>>> -ttm-y := ttm_memory.o ttm_tt.o ttm_bo.o \
> >>>>>>> -ttm_bo_util.o ttm_bo_vm.o ttm_module.o \
> >>>>>>> -ttm_execbuf_util.o ttm_range_manager.o \
> >>>>>>> -ttm_resource.o ttm_pool.o ttm_device.o
> >>>>>>> +ttm-y := ttm_tt.o ttm_bo.o ttm_bo_util.o ttm_bo_vm.o
> >>>>>>> ttm_module.o \
> >>>>>>> +ttm_execbuf_util.o ttm_range_manager.o ttm_resource.o ttm_pool.o \
> >>>>>>> +ttm_device.o
> >>>>>>> ttm-$(CONFIG_AGP) += ttm_agp_backend.o
> >>>>>>>    obj-$(CONFIG_DRM_TTM) += ttm.o
> >>>>>>> diff --git a/drivers/gpu/drm/ttm/ttm_bo.c
> >>>>>>> b/drivers/gpu/drm/ttm/ttm_bo.c
> >>>>>>> index 643befc1a6f2..e38102282fd5 100644
> >>>>>>> --- a/drivers/gpu/drm/ttm/ttm_bo.c
> >>>>>>> +++ b/drivers/gpu/drm/ttm/ttm_bo.c
> >>>>>>> @@ -425,7 +425,6 @@ static void ttm_bo_release(struct kref *kref)
> >>>>>>> struct ttm_buffer_object *bo =
> >>>>>>>      container_of(kref, struct ttm_buffer_object, kref);
> >>>>>>> struct ttm_device *bdev = bo->bdev;
> >>>>>>> -size_t acc_size = bo->acc_size;
> >>>>>>> int ret;
> >>>>>>>    if (!bo->deleted) {
> >>>>>>> @@ -485,7 +484,6 @@ static void ttm_bo_release(struct kref *kref)
> >>>>>>> if (!ttm_bo_uses_embedded_gem_object(bo))
> >>>>>>> dma_resv_fini(&bo->base._resv);
> >>>>>>> bo->destroy(bo);
> >>>>>>> -ttm_mem_global_free(&ttm_mem_glob, acc_size);
> >>>>>>> }
> >>>>>>>    void ttm_bo_put(struct ttm_buffer_object *bo)
> >>>>>>> @@ -1046,25 +1044,13 @@ int ttm_bo_init_reserved(struct
> >>>>>>> ttm_device *bdev,
> >>>>>>>   struct ttm_placement *placement,
> >>>>>>>   uint32_t page_alignment,
> >>>>>>>   struct ttm_operation_ctx *ctx,
> >>>>>>> - size_t acc_size,
> >>>>>>>   struct sg_table *sg,
> >>>>>>>   struct dma_resv *resv,
> >>>>>>>   void (*destroy) (struct ttm_buffer_object *))
> >>>>>>> {
> >>>>>>> -struct ttm_mem_global *mem_glob = &ttm_mem_glob;
> >>>>>>> bool locked;
> >>>>>>> int ret = 0;
> >>>>>>> -ret = ttm_mem_global_alloc(mem_glob, acc_size, ctx);
> >>>>>>> -if (ret) {
> >>>>>>> -pr_err("Out of kernel memory\n");
> >>>>>>> -if (destroy)
> >>>>>>> -(*destroy)(bo);
> >>>>>>> -else
> >>>>>>> -kfree(bo);
> >>>>>>> -return -ENOMEM;
> >>>>>>> -}
> >>>>>>> -
> >>>>>>> bo->destroy = destroy ? destroy : ttm_bo_default_destroy;
> >>>>>>>    kref_init(&bo->kref);
> >>>>>>> @@ -1081,7 +1067,6 @@ int ttm_bo_init_reserved(struct ttm_device
> >>>>>>> *bdev,
> >>>>>>> bo->mem.bus.addr = NULL;
> >>>>>>> bo->moving = NULL;
> >>>>>>> bo->mem.placement = 0;
> >>>>>>> -bo->acc_size = acc_size;
> >>>>>>> bo->pin_count = 0;
> >>>>>>> bo->sg = sg;
> >>>>>>> if (resv) {
> >>>>>>> @@ -1142,7 +1127,6 @@ int ttm_bo_init(struct ttm_device *bdev,
> >>>>>>> struct ttm_placement *placement,
> >>>>>>> uint32_t page_alignment,
> >>>>>>> bool interruptible,
> >>>>>>> -size_t acc_size,
> >>>>>>> struct sg_table *sg,
> >>>>>>> struct dma_resv *resv,
> >>>>>>> void (*destroy) (struct ttm_buffer_object *))
> >>>>>>> @@ -1151,8 +1135,7 @@ int ttm_bo_init(struct ttm_device *bdev,
> >>>>>>> int ret;
> >>>>>>>    ret = ttm_bo_init_reserved(bdev, bo, size, type, placement,
> >>>>>>> -   page_alignment, &ctx, acc_size,
> >>>>>>> -   sg, resv, destroy);
> >>>>>>> +   page_alignment, &ctx, sg, resv, destroy);
> >>>>>>> if (ret)
> >>>>>>> return ret;
> >>>>>>> @@ -1163,20 +1146,6 @@ int ttm_bo_init(struct ttm_device *bdev,
> >>>>>>> }
> >>>>>>> EXPORT_SYMBOL(ttm_bo_init);
> >>>>>>> -size_t ttm_bo_dma_acc_size(struct ttm_device *bdev,
> >>>>>>> -   unsigned long bo_size,
> >>>>>>> -   unsigned struct_size)
> >>>>>>> -{
> >>>>>>> -unsigned npages = (PAGE_ALIGN(bo_size)) >> PAGE_SHIFT;
> >>>>>>> -size_t size = 0;
> >>>>>>> -
> >>>>>>> -size += ttm_round_pot(struct_size);
> >>>>>>> -size += ttm_round_pot(npages * (2*sizeof(void *) +
> >>>>>>> sizeof(dma_addr_t)));
> >>>>>>> -size += ttm_round_pot(sizeof(struct ttm_tt));
> >>>>>>> -return size;
> >>>>>>> -}
> >>>>>>> -EXPORT_SYMBOL(ttm_bo_dma_acc_size);
> >>>>>>> -
> >>>>>>> /*
> >>>>>>>   * buffer object vm functions.
> >>>>>>>   */
> >>>>>>> diff --git a/drivers/gpu/drm/ttm/ttm_bo_util.c
> >>>>>>> b/drivers/gpu/drm/ttm/ttm_bo_util.c
> >>>>>>> index db0f2661d504..031e5819fec4 100644
> >>>>>>> --- a/drivers/gpu/drm/ttm/ttm_bo_util.c
> >>>>>>> +++ b/drivers/gpu/drm/ttm/ttm_bo_util.c
> >>>>>>> @@ -309,7 +309,6 @@ static int ttm_buffer_object_transfer(struct
> >>>>>>> ttm_buffer_object *bo,
> >>>>>>>    kref_init(&fbo->base.kref);
> >>>>>>> fbo->base.destroy = &ttm_transfered_destroy;
> >>>>>>> -fbo->base.acc_size = 0;
> >>>>>>> fbo->base.pin_count = 0;
> >>>>>>> if (bo->type != ttm_bo_type_sg)
> >>>>>>> fbo->base.base.resv = &fbo->base.base._resv;
> >>>>>>> diff --git a/drivers/gpu/drm/ttm/ttm_device.c
> >>>>>>> b/drivers/gpu/drm/ttm/ttm_device.c
> >>>>>>> index ac0903c9e60a..6bde344e5da7 100644
> >>>>>>> --- a/drivers/gpu/drm/ttm/ttm_device.c
> >>>>>>> +++ b/drivers/gpu/drm/ttm/ttm_device.c
> >>>>>>> @@ -27,9 +27,12 @@
> >>>>>>>    #define pr_fmt(fmt) "[TTM DEVICE] " fmt
> >>>>>>> +#include <linux/mm.h>
> >>>>>>> +
> >>>>>>> #include <drm/ttm/ttm_device.h>
> >>>>>>> -#include <drm/ttm/ttm_memory.h>
> >>>>>>> +#include <drm/ttm/ttm_tt.h>
> >>>>>>> #include <drm/ttm/ttm_placement.h>
> >>>>>>> +#include <drm/ttm/ttm_bo_api.h>
> >>>>>>>    #include "ttm_module.h"
> >>>>>>> @@ -49,9 +52,11 @@ static void ttm_global_release(void)
> >>>>>>> if (--ttm_glob_use_count > 0)
> >>>>>>> goto out;
> >>>>>>> +ttm_pool_mgr_fini();
> >>>>>>> +ttm_tt_mgr_fini();
> >>>>>>> +
> >>>>>>> kobject_del(&glob->kobj);
> >>>>>>> kobject_put(&glob->kobj);
> >>>>>>> -ttm_mem_global_release(&ttm_mem_glob);
> >>>>>>> __free_page(glob->dummy_read_page);
> >>>>>>> memset(glob, 0, sizeof(*glob));
> >>>>>>> out:
> >>>>>>> @@ -61,6 +66,8 @@ static void ttm_global_release(void)
> >>>>>>> static int ttm_global_init(void)
> >>>>>>> {
> >>>>>>> struct ttm_global *glob = &ttm_glob;
> >>>>>>> +unsigned long num_pages;
> >>>>>>> +struct sysinfo si;
> >>>>>>> int ret = 0;
> >>>>>>> unsigned i;
> >>>>>>> @@ -68,9 +75,14 @@ static int ttm_global_init(void)
> >>>>>>> if (++ttm_glob_use_count > 1)
> >>>>>>> goto out;
> >>>>>>> -ret = ttm_mem_global_init(&ttm_mem_glob);
> >>>>>>> -if (ret)
> >>>>>>> -goto out;
> >>>>>>> +si_meminfo(&si);
> >>>>>>> +
> >>>>>>> +/* Limit the number of pages in the pool to about 50% of the total
> >>>>>>> + * system memory.
> >>>>>>> + */
> >>>>>>> +num_pages = ((u64)si.totalram * si.mem_unit) >> PAGE_SHIFT;
> >>>>>>> +ttm_pool_mgr_init(num_pages * 50 / 100);
> >>>>>>> +ttm_tt_mgr_init();
> >>>>>>>    spin_lock_init(&glob->lru_lock);
> >>>>>>> glob->dummy_read_page = alloc_page(__GFP_ZERO | GFP_DMA32);
> >>>>>>> diff --git a/drivers/gpu/drm/ttm/ttm_pool.c
> >>>>>>> b/drivers/gpu/drm/ttm/ttm_pool.c
> >>>>>>> index e0617717113f..6b0f957d63d5 100644
> >>>>>>> --- a/drivers/gpu/drm/ttm/ttm_pool.c
> >>>>>>> +++ b/drivers/gpu/drm/ttm/ttm_pool.c
> >>>>>>> @@ -404,16 +404,10 @@ int ttm_pool_alloc(struct ttm_pool *pool,
> >>>>>>> struct ttm_tt *tt,
> >>>>>>> caching = pages + (1 << order);
> >>>>>>> }
> >>>>>>> -r = ttm_mem_global_alloc_page(&ttm_mem_glob, p,
> >>>>>>> -      (1 << order) * PAGE_SIZE,
> >>>>>>> -      ctx);
> >>>>>>> -if (r)
> >>>>>>> -goto error_free_page;
> >>>>>>> -
> >>>>>>> if (dma_addr) {
> >>>>>>> r = ttm_pool_map(pool, order, p, &dma_addr);
> >>>>>>> if (r)
> >>>>>>> -goto error_global_free;
> >>>>>>> +goto error_free_page;
> >>>>>>> }
> >>>>>>>    num_pages -= 1 << order;
> >>>>>>> @@ -427,9 +421,6 @@ int ttm_pool_alloc(struct ttm_pool *pool,
> >>>>>>> struct ttm_tt *tt,
> >>>>>>>    return 0;
> >>>>>>> -error_global_free:
> >>>>>>> -ttm_mem_global_free_page(&ttm_mem_glob, p, (1 << order) *
> >>>>>>> PAGE_SIZE);
> >>>>>>> -
> >>>>>>> error_free_page:
> >>>>>>> ttm_pool_free_page(pool, tt->caching, order, p);
> >>>>>>> @@ -464,8 +455,6 @@ void ttm_pool_free(struct ttm_pool *pool,
> >>>>>>> struct ttm_tt *tt)
> >>>>>>>    order = ttm_pool_page_order(pool, p);
> >>>>>>> num_pages = 1ULL << order;
> >>>>>>> -ttm_mem_global_free_page(&ttm_mem_glob, p,
> >>>>>>> - num_pages * PAGE_SIZE);
> >>>>>>> if (tt->dma_address)
> >>>>>>> ttm_pool_unmap(pool, tt->dma_address[i], num_pages);
> >>>>>>> diff --git a/drivers/gpu/drm/vmwgfx/Makefile
> >>>>>>> b/drivers/gpu/drm/vmwgfx/Makefile
> >>>>>>> index cc4cdca7176e..8c02fa5852e7 100644
> >>>>>>> --- a/drivers/gpu/drm/vmwgfx/Makefile
> >>>>>>> +++ b/drivers/gpu/drm/vmwgfx/Makefile
> >>>>>>> @@ -9,7 +9,7 @@ vmwgfx-y := vmwgfx_execbuf.o vmwgfx_gmr.o
> >>>>>>> vmwgfx_kms.o vmwgfx_drv.o \
> >>>>>>>      vmwgfx_cotable.o vmwgfx_so.o vmwgfx_binding.o vmwgfx_msg.o \
> >>>>>>>      vmwgfx_simple_resource.o vmwgfx_va.o vmwgfx_blit.o \
> >>>>>>>      vmwgfx_validation.o vmwgfx_page_dirty.o
> >>>>>>> vmwgfx_streamoutput.o \
> >>>>>>> -    ttm_object.o ttm_lock.o
> >>>>>>> +    ttm_object.o ttm_lock.o ttm_memory.o
> >>>>>>>    vmwgfx-$(CONFIG_TRANSPARENT_HUGEPAGE) += vmwgfx_thp.o
> >>>>>>> obj-$(CONFIG_DRM_VMWGFX) := vmwgfx.o
> >>>>>>> diff --git a/drivers/gpu/drm/ttm/ttm_memory.c
> >>>>>>> b/drivers/gpu/drm/vmwgfx/ttm_memory.c
> >>>>>>> similarity index 97%
> >>>>>>> rename from drivers/gpu/drm/ttm/ttm_memory.c
> >>>>>>> rename to drivers/gpu/drm/vmwgfx/ttm_memory.c
> >>>>>>> index 634a85c2dc4c..1306d9e0f095 100644
> >>>>>>> --- a/drivers/gpu/drm/ttm/ttm_memory.c
> >>>>>>> +++ b/drivers/gpu/drm/vmwgfx/ttm_memory.c
> >>>>>>> @@ -28,7 +28,6 @@
> >>>>>>>    #define pr_fmt(fmt) "[TTM] " fmt
> >>>>>>> -#include <drm/ttm/ttm_memory.h>
> >>>>>>> #include <linux/spinlock.h>
> >>>>>>> #include <linux/sched.h>
> >>>>>>> #include <linux/wait.h>
> >>>>>>> @@ -36,10 +35,11 @@
> >>>>>>> #include <linux/module.h>
> >>>>>>> #include <linux/slab.h>
> >>>>>>> #include <linux/swap.h>
> >>>>>>> -#include <drm/ttm/ttm_pool.h>
> >>>>>>> -#include <drm/ttm/ttm_tt.h>
> >>>>>>> -#include "ttm_module.h"
> >>>>>>> +#include <drm/drm_device.h>
> >>>>>>> +#include <drm/drm_file.h>
> >>>>>>> +
> >>>>>>> +#include "ttm_memory.h"
> >>>>>>>    #define TTM_MEMORY_ALLOC_RETRIES 4
> >>>>>>> @@ -414,7 +414,7 @@ static int ttm_mem_init_dma32_zone(struct
> >>>>>>> ttm_mem_global *glob,
> >>>>>>> }
> >>>>>>> #endif
> >>>>>>> -int ttm_mem_global_init(struct ttm_mem_global *glob)
> >>>>>>> +int ttm_mem_global_init(struct ttm_mem_global *glob, struct
> >>>>>>> drm_device *dev)
> >>>>>>> {
> >>>>>>> struct sysinfo si;
> >>>>>>> int ret;
> >>>>>>> @@ -425,7 +425,8 @@ int ttm_mem_global_init(struct
> >>>>>>> ttm_mem_global *glob)
> >>>>>>> glob->swap_queue = create_singlethread_workqueue("ttm_swap");
> >>>>>>> INIT_WORK(&glob->work, ttm_shrink_work);
> >>>>>>> ret = kobject_init_and_add(
> >>>>>>> -&glob->kobj, &ttm_mem_glob_kobj_type, ttm_get_kobj(),
> >>>>>>> "memory_accounting");
> >>>>>>> +&glob->kobj, &ttm_mem_glob_kobj_type, &dev->primary->kdev->kobj,
> >>>>>>> +"memory_accounting");
> >>>>>>> if (unlikely(ret != 0)) {
> >>>>>>> kobject_put(&glob->kobj);
> >>>>>>> return ret;
> >>>>>>> @@ -453,8 +454,6 @@ int ttm_mem_global_init(struct
> >>>>>>> ttm_mem_global *glob)
> >>>>>>> pr_info("Zone %7s: Available graphics memory: %llu KiB\n",
> >>>>>>> zone->name, (unsigned long long)zone->max_mem >> 10);
> >>>>>>> }
> >>>>>>> -ttm_pool_mgr_init(glob->zone_kernel->max_mem/(2*PAGE_SIZE));
> >>>>>>> -ttm_tt_mgr_init();
> >>>>>>> return 0;
> >>>>>>> out_no_zone:
> >>>>>>> ttm_mem_global_release(glob);
> >>>>>>> @@ -466,10 +465,6 @@ void ttm_mem_global_release(struct
> >>>>>>> ttm_mem_global *glob)
> >>>>>>> struct ttm_mem_zone *zone;
> >>>>>>> unsigned int i;
> >>>>>>> -/* let the page allocator first stop the shrink work. */
> >>>>>>> -ttm_pool_mgr_fini();
> >>>>>>> -ttm_tt_mgr_fini();
> >>>>>>> -
> >>>>>>> flush_workqueue(glob->swap_queue);
> >>>>>>> destroy_workqueue(glob->swap_queue);
> >>>>>>> glob->swap_queue = NULL;
> >>>>>>> diff --git a/include/drm/ttm/ttm_memory.h
> >>>>>>> b/drivers/gpu/drm/vmwgfx/ttm_memory.h
> >>>>>>> similarity index 97%
> >>>>>>> rename from include/drm/ttm/ttm_memory.h
> >>>>>>> rename to drivers/gpu/drm/vmwgfx/ttm_memory.h
> >>>>>>> index c1f167881e33..850ee6c867da 100644
> >>>>>>> --- a/include/drm/ttm/ttm_memory.h
> >>>>>>> +++ b/drivers/gpu/drm/vmwgfx/ttm_memory.h
> >>>>>>> @@ -35,7 +35,8 @@
> >>>>>>> #include <linux/errno.h>
> >>>>>>> #include <linux/kobject.h>
> >>>>>>> #include <linux/mm.h>
> >>>>>>> -#include "ttm_bo_api.h"
> >>>>>>> +
> >>>>>>> +#include <drm/ttm/ttm_bo_api.h>
> >>>>>>>    /**
> >>>>>>>   * struct ttm_mem_global - Global memory accounting structure.
> >>>>>>> @@ -79,7 +80,7 @@ extern struct ttm_mem_global {
> >>>>>>> #endif
> >>>>>>> } ttm_mem_glob;
> >>>>>>> -int ttm_mem_global_init(struct ttm_mem_global *glob);
> >>>>>>> +int ttm_mem_global_init(struct ttm_mem_global *glob, struct
> >>>>>>> drm_device *dev);
> >>>>>>> void ttm_mem_global_release(struct ttm_mem_global *glob);
> >>>>>>> int ttm_mem_global_alloc(struct ttm_mem_global *glob, uint64_t
> >>>>>>> memory,
> >>>>>>>   struct ttm_operation_ctx *ctx);
> >>>>>>> diff --git a/drivers/gpu/drm/vmwgfx/ttm_object.h
> >>>>>>> b/drivers/gpu/drm/vmwgfx/ttm_object.h
> >>>>>>> index ede26df87c93..49b064f0cb19 100644
> >>>>>>> --- a/drivers/gpu/drm/vmwgfx/ttm_object.h
> >>>>>>> +++ b/drivers/gpu/drm/vmwgfx/ttm_object.h
> >>>>>>> @@ -43,7 +43,8 @@
> >>>>>>> #include <linux/rcupdate.h>
> >>>>>>>    #include <drm/drm_hashtab.h>
> >>>>>>> -#include <drm/ttm/ttm_memory.h>
> >>>>>>> +
> >>>>>>> +#include "ttm_memory.h"
> >>>>>>>    /**
> >>>>>>>   * enum ttm_ref_type
> >>>>>>> diff --git a/drivers/gpu/drm/vmwgfx/vmwgfx_bo.c
> >>>>>>> b/drivers/gpu/drm/vmwgfx/vmwgfx_bo.c
> >>>>>>> index 6b3bfd8c678a..50e529a01677 100644
> >>>>>>> --- a/drivers/gpu/drm/vmwgfx/vmwgfx_bo.c
> >>>>>>> +++ b/drivers/gpu/drm/vmwgfx/vmwgfx_bo.c
> >>>>>>> @@ -507,11 +507,16 @@ int vmw_bo_create_kernel(struct
> >>>>>>> vmw_private *dev_priv, unsigned long size,
> >>>>>>> acc_size = ttm_round_pot(sizeof(*bo));
> >>>>>>> acc_size += ttm_round_pot(npages * sizeof(void *));
> >>>>>>> acc_size += ttm_round_pot(sizeof(struct ttm_tt));
> >>>>>>> +
> >>>>>>> +ret = ttm_mem_global_alloc(&ttm_mem_glob, acc_size, &ctx);
> >>>>>>> +if (unlikely(ret))
> >>>>>>> +goto error_free;
> >>>>>>> +
> >>>>>>> ret = ttm_bo_init_reserved(&dev_priv->bdev, bo, size,
> >>>>>>>     ttm_bo_type_device, placement, 0,
> >>>>>>> -   &ctx, acc_size, NULL, NULL, NULL);
> >>>>>>> +   &ctx, NULL, NULL, NULL);
> >>>>>>> if (unlikely(ret))
> >>>>>>> -goto error_free;
> >>>>>>> +goto error_account;
> >>>>>>>    ttm_bo_pin(bo);
> >>>>>>> ttm_bo_unreserve(bo);
> >>>>>>> @@ -519,6 +524,9 @@ int vmw_bo_create_kernel(struct vmw_private
> >>>>>>> *dev_priv, unsigned long size,
> >>>>>>>    return 0;
> >>>>>>> +error_account:
> >>>>>>> +ttm_mem_global_free(&ttm_mem_glob, acc_size);
> >>>>>>> +
> >>>>>>> error_free:
> >>>>>>> kfree(bo);
> >>>>>>> return ret;
> >>>>>>> @@ -558,11 +566,17 @@ int vmw_bo_init(struct vmw_private *dev_priv,
> >>>>>>> vmw_bo->base.priority = 3;
> >>>>>>> vmw_bo->res_tree = RB_ROOT;
> >>>>>>> +ret = ttm_mem_global_alloc(&ttm_mem_glob, acc_size, &ctx);
> >>>>>>> +if (unlikely(ret))
> >>>>>>> +return ret;
> >>>>>>> +
> >>>>>>> ret = ttm_bo_init_reserved(bdev, &vmw_bo->base, size,
> >>>>>>>     ttm_bo_type_device, placement,
> >>>>>>> -   0, &ctx, acc_size, NULL, NULL, bo_free);
> >>>>>>> -if (unlikely(ret))
> >>>>>>> +   0, &ctx, NULL, NULL, bo_free);
> >>>>>>> +if (unlikely(ret)) {
> >>>>>>> +ttm_mem_global_free(&ttm_mem_glob, acc_size);
> >>>>>>> return ret;
> >>>>>>> +}
> >>>>>>>    if (pin)
> >>>>>>> ttm_bo_pin(&vmw_bo->base);
> >>>>>>> diff --git a/drivers/gpu/drm/vmwgfx/vmwgfx_drv.c
> >>>>>>> b/drivers/gpu/drm/vmwgfx/vmwgfx_drv.c
> >>>>>>> index 710ba5169a74..6c0ca1011629 100644
> >>>>>>> --- a/drivers/gpu/drm/vmwgfx/vmwgfx_drv.c
> >>>>>>> +++ b/drivers/gpu/drm/vmwgfx/vmwgfx_drv.c
> >>>>>>> @@ -1268,6 +1268,7 @@ static void vmw_remove(struct pci_dev *pdev)
> >>>>>>> {
> >>>>>>> struct drm_device *dev = pci_get_drvdata(pdev);
> >>>>>>> +ttm_mem_global_release(&ttm_mem_glob);
> >>>>>>> drm_dev_unregister(dev);
> >>>>>>> vmw_driver_unload(dev);
> >>>>>>> }
> >>>>>>> @@ -1518,6 +1519,10 @@ static int vmw_probe(struct pci_dev
> >>>>>>> *pdev, const struct pci_device_id *ent)
> >>>>>>>    pci_set_drvdata(pdev, &vmw->drm);
> >>>>>>> +ret = ttm_mem_global_init(&ttm_mem_glob, &vmw->drm);
> >>>>>>> +if (ret)
> >>>>>>> +return ret;
> >>>>>>> +
> >>>>>>> ret = vmw_driver_load(vmw, ent->device);
> >>>>>>> if (ret)
> >>>>>>> return ret;
> >>>>>>> diff --git a/drivers/gpu/drm/vmwgfx/vmwgfx_ttm_buffer.c
> >>>>>>> b/drivers/gpu/drm/vmwgfx/vmwgfx_ttm_buffer.c
> >>>>>>> index d1bfa59579f1..63f10c865061 100644
> >>>>>>> --- a/drivers/gpu/drm/vmwgfx/vmwgfx_ttm_buffer.c
> >>>>>>> +++ b/drivers/gpu/drm/vmwgfx/vmwgfx_ttm_buffer.c
> >>>>>>> @@ -576,11 +576,31 @@ static void vmw_ttm_destroy(struct
> >>>>>>> ttm_device *bdev, struct ttm_tt *ttm)
> >>>>>>> static int vmw_ttm_populate(struct ttm_device *bdev,
> >>>>>>>      struct ttm_tt *ttm, struct ttm_operation_ctx *ctx)
> >>>>>>> {
> >>>>>>> +unsigned int i;
> >>>>>>> +int ret;
> >>>>>>> +
> >>>>>>> /* TODO: maybe completely drop this ? */
> >>>>>>> if (ttm_tt_is_populated(ttm))
> >>>>>>> return 0;
> >>>>>>> -return ttm_pool_alloc(&bdev->pool, ttm, ctx);
> >>>>>>> +ret = ttm_pool_alloc(&bdev->pool, ttm, ctx);
> >>>>>>> +if (ret)
> >>>>>>> +return ret;
> >>>>>>> +
> >>>>>>> +for (i = 0; i < ttm->num_pages; ++i) {
> >>>>>>> +ret = ttm_mem_global_alloc_page(&ttm_mem_glob, ttm->pages[i],
> >>>>>>> +PAGE_SIZE, ctx);
> >>>>>>> +if (ret)
> >>>>>>> +goto error;
> >>>>>>> +}
> >>>>>>> +return 0;
> >>>>>>> +
> >>>>>>> +error:
> >>>>>>> +while (i--)
> >>>>>>> +ttm_mem_global_free_page(&ttm_mem_glob, ttm->pages[i],
> >>>>>>> + PAGE_SIZE);
> >>>>>>> +ttm_pool_free(&bdev->pool, ttm);
> >>>>>>> +return ret;
> >>>>>>> }
> >>>>>>>    static void vmw_ttm_unpopulate(struct ttm_device *bdev,
> >>>>>>> @@ -588,6 +608,7 @@ static void vmw_ttm_unpopulate(struct
> >>>>>>> ttm_device *bdev,
> >>>>>>> {
> >>>>>>> struct vmw_ttm_tt *vmw_tt = container_of(ttm, struct vmw_ttm_tt,
> >>>>>>>   dma_ttm);
> >>>>>>> +unsigned int i;
> >>>>>>>    if (vmw_tt->mob) {
> >>>>>>> vmw_mob_destroy(vmw_tt->mob);
> >>>>>>> @@ -595,6 +616,11 @@ static void vmw_ttm_unpopulate(struct
> >>>>>>> ttm_device *bdev,
> >>>>>>> }
> >>>>>>>    vmw_ttm_unmap_dma(vmw_tt);
> >>>>>>> +
> >>>>>>> +for (i = 0; i < ttm->num_pages; ++i)
> >>>>>>> +ttm_mem_global_free_page(&ttm_mem_glob, ttm->pages[i],
> >>>>>>> + PAGE_SIZE);
> >>>>>>> +
> >>>>>>> ttm_pool_free(&bdev->pool, ttm);
> >>>>>>> }
> >>>>>>> diff --git a/include/drm/ttm/ttm_bo_api.h
> >>>>>>> b/include/drm/ttm/ttm_bo_api.h
> >>>>>>> index 1297a8fb7ccb..4fb523dfab32 100644
> >>>>>>> --- a/include/drm/ttm/ttm_bo_api.h
> >>>>>>> +++ b/include/drm/ttm/ttm_bo_api.h
> >>>>>>> @@ -88,7 +88,6 @@ struct ttm_tt;
> >>>>>>>   * @type: The bo type.
> >>>>>>>   * @destroy: Destruction function. If NULL, kfree is used.
> >>>>>>>   * @num_pages: Actual number of pages.
> >>>>>>> - * @acc_size: Accounted size for this object.
> >>>>>>>   * @kref: Reference count of this buffer object. When this
> >>>>>>> refcount reaches
> >>>>>>>   * zero, the object is destroyed or put on the delayed delete
> >>>>>>> list.
> >>>>>>>   * @mem: structure describing current placement.
> >>>>>>> @@ -125,7 +124,6 @@ struct ttm_buffer_object {
> >>>>>>> struct ttm_device *bdev;
> >>>>>>> enum ttm_bo_type type;
> >>>>>>> void (*destroy) (struct ttm_buffer_object *);
> >>>>>>> -size_t acc_size;
> >>>>>>>    /**
> >>>>>>> * Members not needing protection.
> >>>>>>> @@ -357,10 +355,6 @@ void ttm_bo_unlock_delayed_workqueue(struct
> >>>>>>> ttm_device *bdev, int resched);
> >>>>>>> bool ttm_bo_eviction_valuable(struct ttm_buffer_object *bo,
> >>>>>>>        const struct ttm_place *place);
> >>>>>>> -size_t ttm_bo_dma_acc_size(struct ttm_device *bdev,
> >>>>>>> -   unsigned long bo_size,
> >>>>>>> -   unsigned struct_size);
> >>>>>>> -
> >>>>>>> /**
> >>>>>>>   * ttm_bo_init_reserved
> >>>>>>>   *
> >>>>>>> @@ -371,7 +365,6 @@ size_t ttm_bo_dma_acc_size(struct ttm_device
> >>>>>>> *bdev,
> >>>>>>>   * @flags: Initial placement flags.
> >>>>>>>   * @page_alignment: Data alignment in pages.
> >>>>>>>   * @ctx: TTM operation context for memory allocation.
> >>>>>>> - * @acc_size: Accounted size for this object.
> >>>>>>>   * @resv: Pointer to a dma_resv, or NULL to let ttm allocate one.
> >>>>>>>   * @destroy: Destroy function. Use NULL for kfree().
> >>>>>>>   *
> >>>>>>> @@ -402,8 +395,7 @@ int ttm_bo_init_reserved(struct ttm_device
> >>>>>>> *bdev,
> >>>>>>>   struct ttm_placement *placement,
> >>>>>>>   uint32_t page_alignment,
> >>>>>>>   struct ttm_operation_ctx *ctx,
> >>>>>>> - size_t acc_size, struct sg_table *sg,
> >>>>>>> - struct dma_resv *resv,
> >>>>>>> + struct sg_table *sg, struct dma_resv *resv,
> >>>>>>>   void (*destroy) (struct ttm_buffer_object *));
> >>>>>>>    /**
> >>>>>>> @@ -421,7 +413,6 @@ int ttm_bo_init_reserved(struct ttm_device
> >>>>>>> *bdev,
> >>>>>>>   * holds a pointer to a persistent shmem object. Typically,
> >>>>>>> this would
> >>>>>>>   * point to the shmem object backing a GEM object if TTM is
> >>>>>>> used to back a
> >>>>>>>   * GEM user interface.
> >>>>>>> - * @acc_size: Accounted size for this object.
> >>>>>>>   * @resv: Pointer to a dma_resv, or NULL to let ttm allocate one.
> >>>>>>>   * @destroy: Destroy function. Use NULL for kfree().
> >>>>>>>   *
> >>>>>>> @@ -446,7 +437,7 @@ int ttm_bo_init_reserved(struct ttm_device
> >>>>>>> *bdev,
> >>>>>>> int ttm_bo_init(struct ttm_device *bdev, struct
> >>>>>>> ttm_buffer_object *bo,
> >>>>>>> size_t size, enum ttm_bo_type type,
> >>>>>>> struct ttm_placement *placement,
> >>>>>>> -uint32_t page_alignment, bool interrubtible, size_t acc_size,
> >>>>>>> +uint32_t page_alignment, bool interrubtible,
> >>>>>>> struct sg_table *sg, struct dma_resv *resv,
> >>>>>>> void (*destroy) (struct ttm_buffer_object *));
> >>>>>>> diff --git a/include/drm/ttm/ttm_bo_driver.h
> >>>>>>> b/include/drm/ttm/ttm_bo_driver.h
> >>>>>>> index 1c9bf993e252..8959c0075cfd 100644
> >>>>>>> --- a/include/drm/ttm/ttm_bo_driver.h
> >>>>>>> +++ b/include/drm/ttm/ttm_bo_driver.h
> >>>>>>> @@ -40,7 +40,6 @@
> >>>>>>> #include <drm/ttm/ttm_device.h>
> >>>>>>>    #include "ttm_bo_api.h"
> >>>>>>> -#include "ttm_memory.h"
> >>>>>>> #include "ttm_placement.h"
> >>>>>>> #include "ttm_tt.h"
> >>>>>>> #include "ttm_pool.h"
> >>>>>>> diff --git a/include/drm/ttm/ttm_tt.h b/include/drm/ttm/ttm_tt.h
> >>>>>>> index cce57fb49e2c..069f8130241a 100644
> >>>>>>> --- a/include/drm/ttm/ttm_tt.h
> >>>>>>> +++ b/include/drm/ttm/ttm_tt.h
> >>>>>>> @@ -30,6 +30,7 @@
> >>>>>>> #include <linux/types.h>
> >>>>>>> #include <drm/ttm/ttm_caching.h>
> >>>>>>> +struct ttm_bo_device;
> >>>>>>> struct ttm_tt;
> >>>>>>> struct ttm_resource;
> >>>>>>> struct ttm_buffer_object;
> >>>
> >>>
> >>> ----------
> >>>
> >>> You're receiving this message because you're a member of the
> >>> Linux-graphics-maintainer group from VMware, Inc..
> >>>
> >>> Leave group:
> >>> https://nam11.safelinks.protection.outlook.com/?url=https%3A%2F%2Foutlook.office365.com%2Fowa%2FLinux-graphics-maintainer%40vmware.com%2Fgroupsubscription.ashx%3Fsource%3DEscalatedMessage%26action%3Dleave%26GuestId%3D69d3bf6f-5242-4be4-b863-b7949752f363&amp;data=04%7C01%7Cchristian.koenig%40amd.com%7Cccf900e6d23648ef0b0808d8c7edb760%7C3dd8961fe4884e608e11a82d994e183d%7C0%7C0%7C637479171103384072%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C1000&amp;sdata=f8Y%2ByzBnt9Gxxmm7XZ3hpZqTdbY05og9yloArLCLIx0%3D&amp;reserved=0
> >>>
> >>> _______________________________________________
> >>> Sent to linux-graphics-maintainer@vmware.com
> >
>


-- 
Daniel Vetter
Software Engineer, Intel Corporation
http://blog.ffwll.ch
_______________________________________________
dri-devel mailing list
dri-devel@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/dri-devel

^ permalink raw reply	[flat|nested] 17+ messages in thread

* Re: [Linux-graphics-maintainer] [PATCH 2/3] drm/ttm: move memory accounting into vmwgfx v3
  2021-02-08 13:35               ` Christian König
  2021-02-08 13:39                 ` Daniel Vetter
@ 2021-02-08 20:21                 ` Zack Rusin
  2021-02-08 20:23                   ` Christian König
  1 sibling, 1 reply; 17+ messages in thread
From: Zack Rusin @ 2021-02-08 20:21 UTC (permalink / raw)
  To: Christian König; +Cc: Das, Nirmoy, Linux-graphics-maintainer, dri-devel


> On Feb 8, 2021, at 08:35, Christian König <christian.koenig@amd.com> wrote:
> 
> Hi Zack,
> 
> ok we figured out how to do this correctly.
> 
> Basically using the pdev->kobj instead of the drm->primary->kdev->kobj pointer worked quite well.
> 
> I've just send the latest patches to the mailing list. If you don't have any objections I will commit that tomorrow with your and Daniels rb.

That sounds good. That moves the entries under /sys/devices/pciBUS/ID/memory_accounting , yes?

z
_______________________________________________
dri-devel mailing list
dri-devel@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/dri-devel

^ permalink raw reply	[flat|nested] 17+ messages in thread

* Re: [Linux-graphics-maintainer] [PATCH 2/3] drm/ttm: move memory accounting into vmwgfx v3
  2021-02-08 20:21                 ` Zack Rusin
@ 2021-02-08 20:23                   ` Christian König
  2021-02-08 20:40                     ` Zack Rusin
  0 siblings, 1 reply; 17+ messages in thread
From: Christian König @ 2021-02-08 20:23 UTC (permalink / raw)
  To: Zack Rusin; +Cc: Das, Nirmoy, Linux-graphics-maintainer, dri-devel

Am 08.02.21 um 21:21 schrieb Zack Rusin:
>> On Feb 8, 2021, at 08:35, Christian König <christian.koenig@amd.com> wrote:
>>
>> Hi Zack,
>>
>> ok we figured out how to do this correctly.
>>
>> Basically using the pdev->kobj instead of the drm->primary->kdev->kobj pointer worked quite well.
>>
>> I've just send the latest patches to the mailing list. If you don't have any objections I will commit that tomorrow with your and Daniels rb.
> That sounds good. That moves the entries under /sys/devices/pciBUS/ID/memory_accounting , yes?

Yes correct. I really hope nobody relied on the old location or 
otherwise I need to add some kind of symlink workaround or such.

Regards,
Christian.

>
> z

_______________________________________________
dri-devel mailing list
dri-devel@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/dri-devel

^ permalink raw reply	[flat|nested] 17+ messages in thread

* Re: [Linux-graphics-maintainer] [PATCH 2/3] drm/ttm: move memory accounting into vmwgfx v3
  2021-02-08 20:23                   ` Christian König
@ 2021-02-08 20:40                     ` Zack Rusin
  0 siblings, 0 replies; 17+ messages in thread
From: Zack Rusin @ 2021-02-08 20:40 UTC (permalink / raw)
  To: Christian König; +Cc: Das, Nirmoy, Linux-graphics-maintainer, dri-devel



> On Feb 8, 2021, at 15:23, Christian König <christian.koenig@amd.com> wrote:
> 
> Am 08.02.21 um 21:21 schrieb Zack Rusin:
>>> On Feb 8, 2021, at 08:35, Christian König <christian.koenig@amd.com> wrote:
>>> 
>>> Hi Zack,
>>> 
>>> ok we figured out how to do this correctly.
>>> 
>>> Basically using the pdev->kobj instead of the drm->primary->kdev->kobj pointer worked quite well.
>>> 
>>> I've just send the latest patches to the mailing list. If you don't have any objections I will commit that tomorrow with your and Daniels rb.
>> That sounds good. That moves the entries under /sys/devices/pciBUS/ID/memory_accounting , yes?
> 
> Yes correct. I really hope nobody relied on the old location or otherwise I need to add some kind of symlink workaround or such.

I’m not aware of anyone depending on that location. I’ll think about it this week and in the worst case I’ll just disable it for the next release until we can spend some time creating an actual structure to our sysfs layout that we can maintain long term but that’s not something you need to worry about. Thank you.

z
_______________________________________________
dri-devel mailing list
dri-devel@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/dri-devel

^ permalink raw reply	[flat|nested] 17+ messages in thread

end of thread, other threads:[~2021-02-08 20:40 UTC | newest]

Thread overview: 17+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-01-28 13:16 [PATCH 1/3] drm/ttm: rework ttm_tt page limit v3 Christian König
2021-01-28 13:16 ` [PATCH 2/3] drm/ttm: move memory accounting into vmwgfx v3 Christian König
2021-02-02 13:04   ` Christian König
2021-02-02 15:14     ` Zack Rusin
2021-02-02 15:16       ` Christian König
2021-02-02 17:42         ` Zack Rusin
2021-02-03  2:45           ` [Linux-graphics-maintainer] " Zack Rusin
2021-02-03  8:20             ` Christian König
2021-02-08 13:35               ` Christian König
2021-02-08 13:39                 ` Daniel Vetter
2021-02-08 20:21                 ` Zack Rusin
2021-02-08 20:23                   ` Christian König
2021-02-08 20:40                     ` Zack Rusin
2021-01-28 13:16 ` [PATCH 3/3] drm/ttm: drop sysfs directory Christian König
2021-02-03 11:28   ` Daniel Vetter
2021-02-03 11:26 ` [PATCH 1/3] drm/ttm: rework ttm_tt page limit v3 Daniel Vetter
2021-02-03 12:18   ` Christian König

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.