Skip to contentSkip to conversation

ttm: merge ttm_backend & ttm_tt, introduce ttm dma allocator

PATCH In review [email protected] 3 participants 26 messages Nov 9, 2011 – Nov 11, 2011

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

So i did an overhaul of ttm_memory, i believe the simplification i did make sense. See patch 5 for a longer explanation.

Thomas with the ttm_memory change the allocation of pages won't happen if the accounting report that we are going over the limit and bo shrinker failed to free any memory to make room.

The handling of dma32 zone is done as post pass of ttm memory accounting.

Regarding the pagefault comment i removed, it doesn't make sense anymore because now we populate the whole page table in one shot. So there is no more prefaulting few pages but a full prefaulting. Thought i can add a comment stating that if you like.

For the ttm_tt_dma struct to hold page allocator specific informations i think it can be done as an followup patch but if you prefer to have that in this patchset let me know i will respin with such changes.

I am in the process of retesting this whole serie and especialy the while memory accounting.

Cheers,
Jerome

#1 lore raw

PATCH 1/13 drm/ttm: remove userspace backed ttm object support

From: Jerome Glisse <[email protected]>

This was never use in none of the driver, properly using userspace page for bo would need more code (vma interaction mostly). Removing this dead code in preparation of ttm_tt & backend merge.

---
 drivers/gpu/drm/ttm/ttm_bo.c    |   22 --------
 drivers/gpu/drm/ttm/ttm_tt.c    |  105 +--------------------------------------
 include/drm/ttm/ttm_bo_api.h    |    5 --
 include/drm/ttm/ttm_bo_driver.h |   24 ---------
 4 files changed, 1 insertions(+), 155 deletions(-)
drivers/gpu/drm/ttm/ttm_bo.c +0 -22
index 617b646..4bde335 100644
@@ -342,22 +342,6 @@ static int ttm_bo_add_ttm(struct ttm_buffer_object *bo, bool zero_alloc)
 		if (unlikely(bo->ttm == NULL))
 			ret = -ENOMEM;
 		break;
-	case ttm_bo_type_user:
-		bo->ttm = ttm_tt_create(bdev, bo->num_pages << PAGE_SHIFT,
-					page_flags | TTM_PAGE_FLAG_USER,
-					glob->dummy_read_page);
-		if (unlikely(bo->ttm == NULL)) {
-			ret = -ENOMEM;
-			break;
-		}
-
-		ret = ttm_tt_set_user(bo->ttm, current,
-				      bo->buffer_start, bo->num_pages);
-		if (unlikely(ret != 0)) {
-			ttm_tt_destroy(bo->ttm);
-			bo->ttm = NULL;
-		}
-		break;
 	default:
 		printk(KERN_ERR TTM_PFX "Illegal buffer object type\n");
 		ret = -EINVAL;
@@ -907,16 +891,12 @@ static uint32_t ttm_bo_select_caching(struct ttm_mem_type_manager *man,
 }
 
 static bool ttm_bo_mt_compatible(struct ttm_mem_type_manager *man,
-				 bool disallow_fixed,
 				 uint32_t mem_type,
 				 uint32_t proposed_placement,
 				 uint32_t *masked_placement)
 {
 	uint32_t cur_flags = ttm_bo_type_flags(mem_type);
 
-	if ((man->flags & TTM_MEMTYPE_FLAG_FIXED) && disallow_fixed)
-		return false;
-
 	if ((cur_flags & proposed_placement & TTM_PL_MASK_MEM) == 0)
 		return false;
 
@@ -961,7 +941,6 @@ int ttm_bo_mem_space(struct ttm_buffer_object *bo,
 		man = &bdev->man[mem_type];
 
 		type_ok = ttm_bo_mt_compatible(man,
-						bo->type == ttm_bo_type_user,
 						mem_type,
 						placement->placement[i],
 						&cur_flags);
@@ -1009,7 +988,6 @@ int ttm_bo_mem_space(struct ttm_buffer_object *bo,
 		if (!man->has_type)
 			continue;
 		if (!ttm_bo_mt_compatible(man,
-						bo->type == ttm_bo_type_user,
 						mem_type,
 						placement->busy_placement[i],
 						&cur_flags))
drivers/gpu/drm/ttm/ttm_tt.c +1 -104

141 lines not on this page · raw message

include/drm/ttm/ttm_bo_api.h +0 -5
index 42e3469..8d95a42 100644
@@ -122,17 +122,12 @@ struct ttm_mem_reg {
  * be mmapped by user space. Each of these bos occupy a slot in the
  * device address space, that can be used for normal vm operations.
  *
- * @ttm_bo_type_user: These are user-space memory areas that are made
- * available to the GPU by mapping the buffer pages into the GPU aperture
- * space. These buffers cannot be mmaped from the device address space.
- *
  * @ttm_bo_type_kernel: These buffers are like ttm_bo_type_device buffers,
  * but they cannot be accessed from user-space. For kernel-only use.
  */
 
 enum ttm_bo_type {
 	ttm_bo_type_device,
-	ttm_bo_type_user,
 	ttm_bo_type_kernel
 };
 
include/drm/ttm/ttm_bo_driver.h +0 -24
index 94eb143..37527d6 100644
@@ -118,8 +118,6 @@ struct ttm_backend {
 	struct ttm_backend_func *func;
 };
 
-#define TTM_PAGE_FLAG_USER            (1 << 1)
-#define TTM_PAGE_FLAG_USER_DIRTY      (1 << 2)
 #define TTM_PAGE_FLAG_WRITE           (1 << 3)
 #define TTM_PAGE_FLAG_SWAPPED         (1 << 4)
 #define TTM_PAGE_FLAG_PERSISTENT_SWAP (1 << 5)
@@ -146,8 +144,6 @@ enum ttm_caching_state {
  * @num_pages: Number of pages in the page array.
  * @bdev: Pointer to the current struct ttm_bo_device.
  * @be: Pointer to the ttm backend.
- * @tsk: The task for user ttm.
- * @start: virtual address for user ttm.
  * @swap_storage: Pointer to shmem struct file for swap storage.
  * @caching_state: The current caching state of the pages.
  * @state: The current binding state of the pages.
@@ -167,8 +163,6 @@ struct ttm_tt {
 	unsigned long num_pages;
 	struct ttm_bo_global *glob;
 	struct ttm_backend *be;
-	struct task_struct *tsk;
-	unsigned long start;
 	struct file *swap_storage;
 	enum ttm_caching_state caching_state;
 	enum {
@@ -618,24 +612,6 @@ extern struct ttm_tt *ttm_tt_create(struct ttm_bo_device *bdev,
 				    struct page *dummy_read_page);
 
 /**
- * ttm_tt_set_user:
- *
- * @ttm: The struct ttm_tt to populate.
- * @tsk: A struct task_struct for which @start is a valid user-space address.
- * @start: A valid user-space address.
- * @num_pages: Size in pages of the user memory area.
- *
- * Populate a struct ttm_tt with a user-space memory area after first pinning
- * the pages backing it.
- * Returns:
- * !0: Error.
- */
-
-extern int ttm_tt_set_user(struct ttm_tt *ttm,
-			   struct task_struct *tsk,
-			   unsigned long start, unsigned long num_pages);
-
-/**
  * ttm_ttm_bind:
  *
  * @ttm: The struct ttm_tt containing backing pages.
Signature · 1 line
1.7.7.1
#2 lore raw

PATCH 2/13 drm/ttm: remove split btw highmen and lowmem page

In reply to [email protected]

From: Jerome Glisse <[email protected]>

Split btw highmem and lowmem page was rendered useless by the pool code. Remove it. Note further cleanup would change the ttm page allocation helper to actualy take an array instead of relying on list this could drasticly reduce the number of function call in the common case of allocation whole buffer.

---
 drivers/gpu/drm/ttm/ttm_tt.c    |   11 ++---------
 include/drm/ttm/ttm_bo_driver.h |    7 -------
 2 files changed, 2 insertions(+), 16 deletions(-)
drivers/gpu/drm/ttm/ttm_tt.c +2 -9
index 82a1161..8b7a6d0 100644
@@ -69,7 +69,7 @@ static struct page *__ttm_tt_get_page(struct ttm_tt *ttm, int index)
 	struct ttm_mem_global *mem_glob = ttm->glob->mem_glob;
 	int ret;
 
-	while (NULL == (p = ttm->pages[index])) {
+	if (NULL == (p = ttm->pages[index])) {
 
 		INIT_LIST_HEAD(&h);
 
@@ -85,10 +85,7 @@ static struct page *__ttm_tt_get_page(struct ttm_tt *ttm, int index)
 		if (unlikely(ret != 0))
 			goto out_err;
 
-		if (PageHighMem(p))
-			ttm->pages[--ttm->first_himem_page] = p;
-		else
-			ttm->pages[++ttm->last_lomem_page] = p;
+		ttm->pages[index] = p;
 	}
 	return p;
 out_err:
@@ -270,8 +267,6 @@ static void ttm_tt_free_alloced_pages(struct ttm_tt *ttm)
 	ttm_put_pages(&h, count, ttm->page_flags, ttm->caching_state,
 		      ttm->dma_address);
 	ttm->state = tt_unpopulated;
-	ttm->first_himem_page = ttm->num_pages;
-	ttm->last_lomem_page = -1;
 }
 
 void ttm_tt_destroy(struct ttm_tt *ttm)
@@ -315,8 +310,6 @@ struct ttm_tt *ttm_tt_create(struct ttm_bo_device *bdev, unsigned long size,
 
 	ttm->glob = bdev->glob;
 	ttm->num_pages = (size + PAGE_SIZE - 1) >> PAGE_SHIFT;
-	ttm->first_himem_page = ttm->num_pages;
-	ttm->last_lomem_page = -1;
 	ttm->caching_state = tt_cached;
 	ttm->page_flags = page_flags;
 
include/drm/ttm/ttm_bo_driver.h +0 -7
index 37527d6..9da182b 100644
@@ -136,11 +136,6 @@ enum ttm_caching_state {
  * @dummy_read_page: Page to map where the ttm_tt page array contains a NULL
  * pointer.
  * @pages: Array of pages backing the data.
- * @first_himem_page: Himem pages are put last in the page array, which
- * enables us to run caching attribute changes on only the first part
- * of the page array containing lomem pages. This is the index of the
- * first himem page.
- * @last_lomem_page: Index of the last lomem page in the page array.
  * @num_pages: Number of pages in the page array.
  * @bdev: Pointer to the current struct ttm_bo_device.
  * @be: Pointer to the ttm backend.
@@ -157,8 +152,6 @@ enum ttm_caching_state {
 struct ttm_tt {
 	struct page *dummy_read_page;
 	struct page **pages;
-	long first_himem_page;
-	long last_lomem_page;
 	uint32_t page_flags;
 	unsigned long num_pages;
 	struct ttm_bo_global *glob;
Signature · 1 line
1.7.7.1
#3 lore raw

PATCH 3/13 drm/ttm: remove unused backend flags field

In reply to [email protected]

From: Jerome Glisse <[email protected]>

This field is not use by any of the driver just drop it.

---
 drivers/gpu/drm/radeon/radeon_ttm.c |    1 -
 include/drm/ttm/ttm_bo_driver.h     |    2 --
 2 files changed, 0 insertions(+), 3 deletions(-)
drivers/gpu/drm/radeon/radeon_ttm.c +0 -1
index 0b5468b..97c76ae 100644
@@ -787,7 +787,6 @@ struct ttm_backend *radeon_ttm_backend_create(struct radeon_device *rdev)
 		return NULL;
 	}
 	gtt->backend.bdev = &rdev->mman.bdev;
-	gtt->backend.flags = 0;
 	gtt->backend.func = &radeon_backend_func;
 	gtt->rdev = rdev;
 	gtt->pages = NULL;
include/drm/ttm/ttm_bo_driver.h +0 -2
index 9da182b..6d17140 100644
@@ -106,7 +106,6 @@ struct ttm_backend_func {
  * struct ttm_backend
  *
  * @bdev: Pointer to a struct ttm_bo_device.
- * @flags: For driver use.
  * @func: Pointer to a struct ttm_backend_func that describes
  * the backend methods.
  *
@@ -114,7 +113,6 @@ struct ttm_backend_func {
 
 struct ttm_backend {
 	struct ttm_bo_device *bdev;
-	uint32_t flags;
 	struct ttm_backend_func *func;
 };
 
Signature · 1 line
1.7.7.1
#4 lore raw

PATCH 4/13 drm/ttm: use ttm put pages function to properly restore cache attribute

In reply to [email protected]

From: Jerome Glisse <[email protected]>

On failure we need to make sure the page we free has wb cache attribute. Do this pas call the proper ttm page helper function.

---
 drivers/gpu/drm/ttm/ttm_tt.c |    5 ++++-
 1 files changed, 4 insertions(+), 1 deletions(-)
drivers/gpu/drm/ttm/ttm_tt.c +4 -1
index 8b7a6d0..3fb4c6d 100644
@@ -89,7 +89,10 @@ static struct page *__ttm_tt_get_page(struct ttm_tt *ttm, int index)
 	}
 	return p;
 out_err:
-	put_page(p);
+	INIT_LIST_HEAD(&h);
+	list_add(&p->lru, &h);
+	ttm_put_pages(&h, 1, ttm->page_flags,
+		      ttm->caching_state, &ttm->dma_address[index]);
 	return NULL;
 }
 
Signature · 1 line
1.7.7.1
#5 lore raw

PATCH 5/13 drm/ttm: overhaul memory accounting

In reply to [email protected]

From: Jerome Glisse <[email protected]>

This is an overhaul of the ttm memory accounting. This tries to keep the same global behavior while removing the whole zone concept. It keeps a distrinction for dma32 so that we make sure that ttm don't starve the dma32 zone.

There is 3 threshold for memory allocation :
- max_mem is the maximum memory the whole ttm infrastructure is going to allow allocation for (exception of system process see below)
- emer_mem is the maximum memory allowed for system process, this limit is > to max_mem
- swap_limit is the threshold at which point ttm will start to try to swap object because ttm is getting close the max_mem limit
- swap_dma32_limit is the threshold at which point ttm will start swap object to try to reduce the pressure on the dma32 zone. Note that we don't specificly target object to swap to it might very well free more memory from highmem rather than from dma32

Accounting is done through used_mem & used_dma32_mem, which sum give the total amount of memory actually accounted by ttm.

Idea is that allocation will fail if (used_mem + used_dma32_mem) > max_mem and if swapping fail to make enough room.

The used_dma32_mem can be updated as a later stage, allowing to perform accounting test before allocating a whole batch of pages.

---
 drivers/gpu/drm/ttm/ttm_bo.c             |    2 +-
 drivers/gpu/drm/ttm/ttm_memory.c         |  517 +++++++++---------------------
 drivers/gpu/drm/ttm/ttm_object.c         |    3 +-
 drivers/gpu/drm/ttm/ttm_tt.c             |    2 +-
 drivers/gpu/drm/vmwgfx/vmwgfx_fence.c    |    8 +-
 drivers/gpu/drm/vmwgfx/vmwgfx_resource.c |    8 +-
 include/drm/ttm/ttm_memory.h             |   23 +-
 7 files changed, 168 insertions(+), 395 deletions(-)
drivers/gpu/drm/ttm/ttm_bo.c +1 -1
index 4bde335..92712798 100644
@@ -1252,7 +1252,7 @@ int ttm_bo_create(struct ttm_bo_device *bdev,
 
 	size_t acc_size =
 	    ttm_bo_size(bdev->glob, (size + PAGE_SIZE - 1) >> PAGE_SHIFT);
-	ret = ttm_mem_global_alloc(mem_glob, acc_size, false, false);
+	ret = ttm_mem_global_alloc(mem_glob, acc_size, false);
 	if (unlikely(ret != 0))
 		return ret;
 
drivers/gpu/drm/ttm/ttm_memory.c +146 -371

675 lines not on this page · raw message

drivers/gpu/drm/ttm/ttm_object.c +1 -2
index 93577f2..662c6fc 100644
@@ -267,8 +267,7 @@ int ttm_ref_object_add(struct ttm_object_file *tfile,
 		}
 
 		read_unlock(&tfile->lock);
-		ret = ttm_mem_global_alloc(mem_glob, sizeof(*ref),
-					   false, false);
+		ret = ttm_mem_global_alloc(mem_glob, sizeof(*ref), false);
 		if (unlikely(ret != 0))
 			return ret;
 		ref = kmalloc(sizeof(*ref), GFP_KERNEL);
drivers/gpu/drm/ttm/ttm_tt.c +1 -1
index 3fb4c6d..e5432efa 100644
@@ -81,7 +81,7 @@ static struct page *__ttm_tt_get_page(struct ttm_tt *ttm, int index)
 
 		p = list_first_entry(&h, struct page, lru);
 
-		ret = ttm_mem_global_alloc_page(mem_glob, p, false, false);
+		ret = ttm_mem_global_alloc_page(mem_glob, p, false);
 		if (unlikely(ret != 0))
 			goto out_err;
 
drivers/gpu/drm/vmwgfx/vmwgfx_fence.c +3 -5
index 15fb260..8fd1e6b 100644
@@ -503,8 +503,7 @@ int vmw_fence_create(struct vmw_fence_manager *fman,
 	struct vmw_fence_obj *fence;
 	int ret;
 
-	ret = ttm_mem_global_alloc(mem_glob, fman->fence_size,
-				   false, false);
+	ret = ttm_mem_global_alloc(mem_glob, fman->fence_size, false);
 	if (unlikely(ret != 0))
 		return ret;
 
@@ -573,8 +572,7 @@ int vmw_user_fence_create(struct drm_file *file_priv,
 	 * be created by a user-space request.
 	 */
 
-	ret = ttm_mem_global_alloc(mem_glob, fman->user_fence_size,
-				   false, false);
+	ret = ttm_mem_global_alloc(mem_glob, fman->user_fence_size, false);
 	if (unlikely(ret != 0))
 		return ret;
 
@@ -966,7 +964,7 @@ int vmw_event_fence_action_create(struct drm_file *file_priv,
 	 * event size itself.
 	 */
 
-	ret = ttm_mem_global_alloc(mem_glob, size, false, interruptible);
+	ret = ttm_mem_global_alloc(mem_glob, size, false);
 	if (unlikely(ret != 0))
 		return ret;
 
drivers/gpu/drm/vmwgfx/vmwgfx_resource.c +4 -4
index 86c5e4c..3c43ac0 100644
@@ -430,7 +430,7 @@ int vmw_context_define_ioctl(struct drm_device *dev, void *data,
 
 	ret = ttm_mem_global_alloc(vmw_mem_glob(dev_priv),
 				   vmw_user_context_size,
-				   false, true);
+				   false);
 	if (unlikely(ret != 0)) {
 		if (ret != -ERESTARTSYS)
 			DRM_ERROR("Out of graphics memory for context"
@@ -1298,7 +1298,7 @@ int vmw_surface_define_ioctl(struct drm_device *dev, void *data,
 		return ret;
 
 	ret = ttm_mem_global_alloc(vmw_mem_glob(dev_priv),
-				   size, false, true);
+				   size, false);
 	if (unlikely(ret != 0)) {
 		if (ret != -ERESTARTSYS)
 			DRM_ERROR("Out of graphics memory for surface"
@@ -1560,7 +1560,7 @@ int vmw_dmabuf_init(struct vmw_private *dev_priv,
 	    vmw_dmabuf_acc_size(bdev->glob,
 				(size + PAGE_SIZE - 1) >> PAGE_SHIFT);
 
-	ret = ttm_mem_global_alloc(mem_glob, acc_size, false, false);
+	ret = ttm_mem_global_alloc(mem_glob, acc_size, false);
 	if (unlikely(ret != 0)) {
 		/* we must free the bo here as
 		 * ttm_buffer_object_init does so as well */
@@ -1851,7 +1851,7 @@ int vmw_stream_claim_ioctl(struct drm_device *dev, void *data,
 
 	ret = ttm_mem_global_alloc(vmw_mem_glob(dev_priv),
 				   vmw_user_stream_size,
-				   false, true);
+				   false);
 	if (unlikely(ret != 0)) {
 		if (ret != -ERESTARTSYS)
 			DRM_ERROR("Out of graphics memory for stream"
include/drm/ttm/ttm_memory.h +12 -11
index 26c1f78..d24792b 100644
@@ -81,14 +81,13 @@ struct ttm_mem_global {
 	struct work_struct work;
 	wait_queue_head_t queue;
 	spinlock_t lock;
-	struct ttm_mem_zone *zones[TTM_MEM_MAX_ZONES];
-	unsigned int num_zones;
-	struct ttm_mem_zone *zone_kernel;
-#ifdef CONFIG_HIGHMEM
-	struct ttm_mem_zone *zone_highmem;
-#else
-	struct ttm_mem_zone *zone_dma32;
-#endif
+	unsigned long mem;
+	unsigned long max_mem;
+	unsigned long emer_mem;
+	unsigned long used_mem;
+	unsigned long used_dma32_mem;
+	unsigned long swap_limit;
+	unsigned long swap_dma32_limit;
 };
 
 /**
@@ -146,14 +145,16 @@ static inline void ttm_mem_unregister_shrink(struct ttm_mem_global *glob,
 
 extern int ttm_mem_global_init(struct ttm_mem_global *glob);
 extern void ttm_mem_global_release(struct ttm_mem_global *glob);
-extern int ttm_mem_global_alloc(struct ttm_mem_global *glob, uint64_t memory,
-				bool no_wait, bool interruptible);
+extern int ttm_mem_global_alloc(struct ttm_mem_global *glob,
+				uint64_t memory,
+				bool no_wait);
 extern void ttm_mem_global_free(struct ttm_mem_global *glob,
 				uint64_t amount);
 extern int ttm_mem_global_alloc_page(struct ttm_mem_global *glob,
 				     struct page *page,
-				     bool no_wait, bool interruptible);
+				     bool no_wait);
 extern void ttm_mem_global_free_page(struct ttm_mem_global *glob,
 				     struct page *page);
 extern size_t ttm_round_pot(size_t size);
+
 #endif
Signature · 1 line
1.7.7.1
#16 lore raw

PATCH 6/13 drm/ttm: convert page allocation to use page ptr array instead of list V4

In reply to [email protected]

From: Jerome Glisse <[email protected]>

Use the ttm_tt page ptr array for page allocation, move the list to array unwinding into the page allocation functions.

V2 split the fix to use ttm put page as a separate fix properly fill pages array when TTM_PAGE_FLAG_ZERO_ALLOC is not set
V3 Added back page_count()==1 check when freeing page V4 Rebase on top of memory accounting overhaul

---
 drivers/gpu/drm/ttm/ttm_memory.c     |   47 +++++++++++------
 drivers/gpu/drm/ttm/ttm_page_alloc.c |   90 ++++++++++++++++++++--------------
 drivers/gpu/drm/ttm/ttm_tt.c         |   68 ++++++++------------------
 include/drm/ttm/ttm_memory.h         |   13 +++--
 include/drm/ttm/ttm_page_alloc.h     |   17 +++---
 5 files changed, 120 insertions(+), 115 deletions(-)
drivers/gpu/drm/ttm/ttm_memory.c +30 -17
index b550baf..98f6899 100644
@@ -326,32 +326,45 @@ int ttm_mem_global_alloc(struct ttm_mem_global *glob,
 }
 EXPORT_SYMBOL(ttm_mem_global_alloc);
 
-int ttm_mem_global_alloc_page(struct ttm_mem_global *glob,
-			      struct page *page,
-			      bool no_wait)
+int ttm_mem_global_alloc_pages(struct ttm_mem_global *glob,
+			       unsigned npages,
+			       bool no_wait)
 {
-
-	if (ttm_mem_global_alloc(glob, PAGE_SIZE, no_wait))
+	if (ttm_mem_global_alloc(glob, PAGE_SIZE * npages, no_wait))
 		return -ENOMEM;
+	ttm_check_swapping(glob);
+	return 0;
+}
+
+void ttm_mem_global_account_pages(struct ttm_mem_global *glob,
+				  struct page **pages,
+				  unsigned npages)
+{
+	unsigned i;
 
 	/* check if page is dma32 */
-	if (page_to_pfn(page) > 0x00100000UL) {
-		spin_lock(&glob->lock);
-		glob->used_mem -= PAGE_SIZE;
-		glob->used_dma32_mem += PAGE_SIZE;
-		spin_unlock(&glob->lock);
+	spin_lock(&glob->lock);
+	for (i = 0; i < npages; i++) {
+		if (page_to_pfn(pages[i]) > 0x00100000UL) {
+			glob->used_mem -= PAGE_SIZE;
+			glob->used_dma32_mem += PAGE_SIZE;
+		}
 	}
-	ttm_check_swapping(glob);
-	return 0;
+	spin_unlock(&glob->lock);
 }
 
-void ttm_mem_global_free_page(struct ttm_mem_global *glob, struct page *page)
+void ttm_mem_global_free_pages(struct ttm_mem_global *glob,
+			       struct page **pages, unsigned npages)
 {
+	unsigned i;
+
 	spin_lock(&glob->lock);
-	if (page_to_pfn(page) > 0x00100000UL) {
-		glob->used_dma32_mem -= PAGE_SIZE;
-	} else {
-		glob->used_mem -= PAGE_SIZE;
+	for (i = 0; i < npages; i++) {
+		if (page_to_pfn(pages[i]) > 0x00100000UL) {
+			glob->used_dma32_mem -= PAGE_SIZE;
+		} else {
+			glob->used_mem -= PAGE_SIZE;
+		}
 	}
 	spin_unlock(&glob->lock);
 }
drivers/gpu/drm/ttm/ttm_page_alloc.c +53 -37

168 lines not on this page · raw message

drivers/gpu/drm/ttm/ttm_tt.c +21 -47

108 lines not on this page · raw message

include/drm/ttm/ttm_memory.h +8 -5
index d24792b..c0c732d 100644
@@ -150,11 +150,14 @@ extern int ttm_mem_global_alloc(struct ttm_mem_global *glob,
 				bool no_wait);
 extern void ttm_mem_global_free(struct ttm_mem_global *glob,
 				uint64_t amount);
-extern int ttm_mem_global_alloc_page(struct ttm_mem_global *glob,
-				     struct page *page,
-				     bool no_wait);
-extern void ttm_mem_global_free_page(struct ttm_mem_global *glob,
-				     struct page *page);
+extern int ttm_mem_global_alloc_pages(struct ttm_mem_global *glob,
+				      unsigned npages,
+				      bool no_wait);
+extern void ttm_mem_global_free_pages(struct ttm_mem_global *glob,
+				      struct page **pages, unsigned npages);
+extern void ttm_mem_global_account_pages(struct ttm_mem_global *glob,
+					 struct page **pages,
+					 unsigned npages);
 extern size_t ttm_round_pot(size_t size);
 
 #endif
include/drm/ttm/ttm_page_alloc.h +8 -9
index 129de12..fffb3bd 100644
@@ -32,29 +32,28 @@
 /**
  * Get count number of pages from pool to pages list.
  *
- * @pages: head of empty linked list where pages are filled.
+ * @pages: array of pages ptr
+ * @npages: number of pages to allocate.
  * @flags: ttm flags for page allocation.
  * @cstate: ttm caching state for the page.
- * @count: number of pages to allocate.
  * @dma_address: The DMA (bus) address of pages (if TTM_PAGE_FLAG_DMA32 set).
  */
-int ttm_get_pages(struct list_head *pages,
+int ttm_get_pages(struct page **pages,
+		  unsigned npages,
 		  int flags,
 		  enum ttm_caching_state cstate,
-		  unsigned count,
 		  dma_addr_t *dma_address);
 /**
  * Put linked list of pages to pool.
  *
- * @pages: list of pages to free.
- * @page_count: number of pages in the list. Zero can be passed for unknown
- * count.
+ * @pages: array of pages ptr
+ * @npages: number of pages to free.
  * @flags: ttm flags for page allocation.
  * @cstate: ttm caching state.
  * @dma_address: The DMA (bus) address of pages (if TTM_PAGE_FLAG_DMA32 set).
  */
-void ttm_put_pages(struct list_head *pages,
-		   unsigned page_count,
+void ttm_put_pages(struct page **pages,
+		   unsigned npages,
 		   int flags,
 		   enum ttm_caching_state cstate,
 		   dma_addr_t *dma_address);
Signature · 1 line
1.7.7.1
#17 lore raw

PATCH 7/13 drm/ttm: test for dma_address array allocation failure

In reply to [email protected]

From: Jerome Glisse <[email protected]>

---
 drivers/gpu/drm/ttm/ttm_tt.c |    2 +-
 1 files changed, 1 insertions(+), 1 deletions(-)
drivers/gpu/drm/ttm/ttm_tt.c +1 -1
index 303bbba..2dab08b 100644
@@ -293,7 +293,7 @@ struct ttm_tt *ttm_tt_create(struct ttm_bo_device *bdev, unsigned long size,
 	ttm->dummy_read_page = dummy_read_page;
 
 	ttm_tt_alloc_page_directory(ttm);
-	if (!ttm->pages) {
+	if (!ttm->pages || !ttm->dma_address) {
 		ttm_tt_destroy(ttm);
 		printk(KERN_ERR TTM_PFX "Failed allocating page table\n");
 		return NULL;
Signature · 1 line
1.7.7.1
#18 lore raw

PATCH 8/13 drm/ttm: merge ttm_backend and ttm_tt V2

In reply to [email protected]

From: Jerome Glisse <[email protected]>

ttm_backend will exist only and only with a ttm_tt, and ttm_tt will be of interesting use only when bind to a backend. Thus to avoid code & data duplication btw the two merge them.

V2 Rebase on top of memory accountign overhaul

---
 drivers/gpu/drm/nouveau/nouveau_bo.c    |   14 ++-
 drivers/gpu/drm/nouveau/nouveau_drv.h   |    5 +-
 drivers/gpu/drm/nouveau/nouveau_sgdma.c |  188 ++++++++++++--------------
 drivers/gpu/drm/radeon/radeon_ttm.c     |  222 ++++++++++++-------------------
 drivers/gpu/drm/ttm/ttm_agp_backend.c   |   88 +++++--------
 drivers/gpu/drm/ttm/ttm_bo.c            |    9 +-
 drivers/gpu/drm/ttm/ttm_tt.c            |   60 ++-------
 drivers/gpu/drm/vmwgfx/vmwgfx_buffer.c  |   66 +++------
 include/drm/ttm/ttm_bo_driver.h         |  104 ++++++---------
 9 files changed, 295 insertions(+), 461 deletions(-)
drivers/gpu/drm/nouveau/nouveau_bo.c +9 -5
index 7226f41..b060fa4 100644
@@ -343,8 +343,10 @@ nouveau_bo_wr32(struct nouveau_bo *nvbo, unsigned index, u32 val)
 		*mem = val;
 }
 
-static struct ttm_backend *
-nouveau_bo_create_ttm_backend_entry(struct ttm_bo_device *bdev)
+static struct ttm_tt *
+nouveau_ttm_tt_create(struct ttm_bo_device *bdev,
+		      unsigned long size, uint32_t page_flags,
+		      struct page *dummy_read_page)
 {
 	struct drm_nouveau_private *dev_priv = nouveau_bdev(bdev);
 	struct drm_device *dev = dev_priv->dev;
@@ -352,11 +354,13 @@ nouveau_bo_create_ttm_backend_entry(struct ttm_bo_device *bdev)
 	switch (dev_priv->gart_info.type) {
 #if __OS_HAS_AGP
 	case NOUVEAU_GART_AGP:
-		return ttm_agp_backend_init(bdev, dev->agp->bridge);
+		return ttm_agp_tt_create(bdev, dev->agp->bridge,
+					 size, page_flags, dummy_read_page);
 #endif
 	case NOUVEAU_GART_PDMA:
 	case NOUVEAU_GART_HW:
-		return nouveau_sgdma_init_ttm(dev);
+		return nouveau_sgdma_create_ttm(bdev, size, page_flags,
+						dummy_read_page);
 	default:
 		NV_ERROR(dev, "Unknown GART type %d\n",
 			 dev_priv->gart_info.type);
@@ -1045,7 +1049,7 @@ nouveau_bo_fence(struct nouveau_bo *nvbo, struct nouveau_fence *fence)
 }
 
 struct ttm_bo_driver nouveau_bo_driver = {
-	.create_ttm_backend_entry = nouveau_bo_create_ttm_backend_entry,
+	.ttm_tt_create = &nouveau_ttm_tt_create,
 	.invalidate_caches = nouveau_bo_invalidate_caches,
 	.init_mem_type = nouveau_bo_init_mem_type,
 	.evict_flags = nouveau_bo_evict_flags,
drivers/gpu/drm/nouveau/nouveau_drv.h +4 -1
index 29837da..0c53e39 100644
@@ -1000,7 +1000,10 @@ extern int nouveau_sgdma_init(struct drm_device *);
 extern void nouveau_sgdma_takedown(struct drm_device *);
 extern uint32_t nouveau_sgdma_get_physical(struct drm_device *,
 					   uint32_t offset);
-extern struct ttm_backend *nouveau_sgdma_init_ttm(struct drm_device *);
+extern struct ttm_tt *nouveau_sgdma_create_ttm(struct ttm_bo_device *bdev,
+					       unsigned long size,
+					       uint32_t page_flags,
+					       struct page *dummy_read_page);
 
 /* nouveau_debugfs.c */
 #if defined(CONFIG_DRM_NOUVEAU_DEBUG)
drivers/gpu/drm/nouveau/nouveau_sgdma.c +85 -103

376 lines not on this page · raw message

drivers/gpu/drm/radeon/radeon_ttm.c +86 -136

241 lines not on this page · raw message

drivers/gpu/drm/ttm/ttm_agp_backend.c +34 -54

139 lines not on this page · raw message

drivers/gpu/drm/ttm/ttm_bo.c +3 -6
index 92712798..c31f40f 100644
@@ -337,8 +337,8 @@ static int ttm_bo_add_ttm(struct ttm_buffer_object *bo, bool zero_alloc)
 		if (zero_alloc)
 			page_flags |= TTM_PAGE_FLAG_ZERO_ALLOC;
 	case ttm_bo_type_kernel:
-		bo->ttm = ttm_tt_create(bdev, bo->num_pages << PAGE_SHIFT,
-					page_flags, glob->dummy_read_page);
+		bo->ttm = bdev->driver->ttm_tt_create(bdev, bo->num_pages << PAGE_SHIFT,
+						      page_flags, glob->dummy_read_page);
 		if (unlikely(bo->ttm == NULL))
 			ret = -ENOMEM;
 		break;
@@ -1437,10 +1437,7 @@ int ttm_bo_global_init(struct drm_global_reference *ref)
 		goto out_no_shrink;
 	}
 
-	glob->ttm_bo_extra_size =
-		ttm_round_pot(sizeof(struct ttm_tt)) +
-		ttm_round_pot(sizeof(struct ttm_backend));
-
+	glob->ttm_bo_extra_size = ttm_round_pot(sizeof(struct ttm_tt));
 	glob->ttm_bo_size = glob->ttm_bo_extra_size +
 		ttm_round_pot(sizeof(struct ttm_buffer_object));
 
drivers/gpu/drm/ttm/ttm_tt.c +12 -48

140 lines not on this page · raw message

drivers/gpu/drm/vmwgfx/vmwgfx_buffer.c +21 -45

113 lines not on this page · raw message

include/drm/ttm/ttm_bo_driver.h +41 -63

186 lines not on this page · raw message

Signature · 1 line
1.7.7.1
#19 lore raw

PATCH 9/13 drm/ttm: introduce callback for ttm_tt populate & unpopulate V2

In reply to [email protected]

From: Jerome Glisse <[email protected]>

Move the page allocation and freeing to driver callback and provide ttm code helper function for those.

Most intrusive change, is the fact that we now only fully populate an object this simplify some of code designed around the page fault design.

V2 Rebase on top of memory accounting overhaul

---
 drivers/gpu/drm/nouveau/nouveau_bo.c   |    3 +
 drivers/gpu/drm/radeon/radeon_ttm.c    |    2 +
 drivers/gpu/drm/ttm/ttm_bo_util.c      |   31 ++++++-----
 drivers/gpu/drm/ttm/ttm_bo_vm.c        |   13 +++--
 drivers/gpu/drm/ttm/ttm_page_alloc.c   |   45 +++++++++++++++++
 drivers/gpu/drm/ttm/ttm_tt.c           |   86 ++------------------------------
 drivers/gpu/drm/vmwgfx/vmwgfx_buffer.c |    3 +
 include/drm/ttm/ttm_bo_driver.h        |   41 +++++++++-------
 include/drm/ttm/ttm_page_alloc.h       |   18 +++++++
 9 files changed, 123 insertions(+), 119 deletions(-)
drivers/gpu/drm/nouveau/nouveau_bo.c +3 -0
index b060fa4..7e5ca3f 100644
@@ -28,6 +28,7 @@
  */
 
 #include "drmP.h"
+#include "ttm/ttm_page_alloc.h"
 
 #include "nouveau_drm.h"
 #include "nouveau_drv.h"
@@ -1050,6 +1051,8 @@ nouveau_bo_fence(struct nouveau_bo *nvbo, struct nouveau_fence *fence)
 
 struct ttm_bo_driver nouveau_bo_driver = {
 	.ttm_tt_create = &nouveau_ttm_tt_create,
+	.ttm_tt_populate = &ttm_page_alloc_ttm_tt_populate,
+	.ttm_tt_unpopulate = &ttm_page_alloc_ttm_tt_unpopulate,
 	.invalidate_caches = nouveau_bo_invalidate_caches,
 	.init_mem_type = nouveau_bo_init_mem_type,
 	.evict_flags = nouveau_bo_evict_flags,
drivers/gpu/drm/radeon/radeon_ttm.c +2 -0
index 53ff62b..490afce 100644
@@ -584,6 +584,8 @@ struct ttm_tt *radeon_ttm_tt_create(struct ttm_bo_device *bdev,
 
 static struct ttm_bo_driver radeon_bo_driver = {
 	.ttm_tt_create = &radeon_ttm_tt_create,
+	.ttm_tt_populate = &ttm_page_alloc_ttm_tt_populate,
+	.ttm_tt_unpopulate = &ttm_page_alloc_ttm_tt_unpopulate,
 	.invalidate_caches = &radeon_invalidate_caches,
 	.init_mem_type = &radeon_init_mem_type,
 	.evict_flags = &radeon_evict_flags,
drivers/gpu/drm/ttm/ttm_bo_util.c +17 -14
index 082fcae..60f204d 100644
@@ -244,7 +244,7 @@ static int ttm_copy_io_ttm_page(struct ttm_tt *ttm, void *src,
 				unsigned long page,
 				pgprot_t prot)
 {
-	struct page *d = ttm_tt_get_page(ttm, page);
+	struct page *d = ttm->pages[page];
 	void *dst;
 
 	if (!d)
@@ -281,7 +281,7 @@ static int ttm_copy_ttm_io_page(struct ttm_tt *ttm, void *dst,
 				unsigned long page,
 				pgprot_t prot)
 {
-	struct page *s = ttm_tt_get_page(ttm, page);
+	struct page *s = ttm->pages[page];
 	void *src;
 
 	if (!s)
@@ -342,6 +342,12 @@ int ttm_bo_move_memcpy(struct ttm_buffer_object *bo,
 	if (old_iomap == NULL && ttm == NULL)
 		goto out2;
 
+	if (ttm->state == tt_unpopulated) {
+		ret = ttm->bdev->driver->ttm_tt_populate(ttm);
+		if (ret)
+			goto out1;
+	}
+
 	add = 0;
 	dir = 1;
 
@@ -502,10 +508,16 @@ static int ttm_bo_kmap_ttm(struct ttm_buffer_object *bo,
 {
 	struct ttm_mem_reg *mem = &bo->mem; pgprot_t prot;
 	struct ttm_tt *ttm = bo->ttm;
-	struct page *d;
-	int i;
+	int ret;
 
 	BUG_ON(!ttm);
+
+	if (ttm->state == tt_unpopulated) {
+		ret = ttm->bdev->driver->ttm_tt_populate(ttm);
+		if (ret)
+			return ret;
+	}
+
 	if (num_pages == 1 && (mem->placement & TTM_PL_FLAG_CACHED)) {
 		/*
 		 * We're mapping a single page, and the desired
@@ -513,18 +525,9 @@ static int ttm_bo_kmap_ttm(struct ttm_buffer_object *bo,
 		 */
 
 		map->bo_kmap_type = ttm_bo_map_kmap;
-		map->page = ttm_tt_get_page(ttm, start_page);
+		map->page = ttm->pages[start_page];
 		map->virtual = kmap(map->page);
 	} else {
-	    /*
-	     * Populate the part we're mapping;
-	     */
-		for (i = start_page; i < start_page + num_pages; ++i) {
-			d = ttm_tt_get_page(ttm, i);
-			if (!d)
-				return -ENOMEM;
-		}
-
 		/*
 		 * We need to use vmap to get the desired page protection
 		 * or to make the buffer object look contiguous.
drivers/gpu/drm/ttm/ttm_bo_vm.c +7 -6
index 221b924..bc1d751 100644
@@ -174,18 +174,19 @@ static int ttm_bo_vm_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
 		vma->vm_page_prot = (bo->mem.placement & TTM_PL_FLAG_CACHED) ?
 		    vm_get_page_prot(vma->vm_flags) :
 		    ttm_io_prot(bo->mem.placement, vma->vm_page_prot);
-	}
 
-	/*
-	 * Speculatively prefault a number of pages. Only error on
-	 * first page.
-	 */
+		/* Allocate all page at once, most common usage */
+		if (ttm->bdev->driver->ttm_tt_populate(ttm)) {
+			retval = VM_FAULT_OOM;
+			goto out_io_unlock;
+		}
+	}
 
 	for (i = 0; i < TTM_BO_VM_NUM_PREFAULT; ++i) {
 		if (bo->mem.bus.is_iomem)
 			pfn = ((bo->mem.bus.base + bo->mem.bus.offset) >> PAGE_SHIFT) + page_offset;
 		else {
-			page = ttm_tt_get_page(ttm, page_offset);
+			page = ttm->pages[page_offset];
 			if (unlikely(!page && i == 0)) {
 				retval = VM_FAULT_OOM;
 				goto out_io_unlock;
drivers/gpu/drm/ttm/ttm_page_alloc.c +45 -0

51 lines not on this page · raw message

drivers/gpu/drm/ttm/ttm_tt.c +5 -81

140 lines not on this page · raw message

drivers/gpu/drm/vmwgfx/vmwgfx_buffer.c +3 -0
index cc72435..383993b 100644
@@ -28,6 +28,7 @@
 #include "vmwgfx_drv.h"
 #include "ttm/ttm_bo_driver.h"
 #include "ttm/ttm_placement.h"
+#include "ttm/ttm_page_alloc.h"
 
 static uint32_t vram_placement_flags = TTM_PL_FLAG_VRAM |
 	TTM_PL_FLAG_CACHED;
@@ -334,6 +335,8 @@ static int vmw_sync_obj_wait(void *sync_obj, void *sync_arg,
 
 struct ttm_bo_driver vmw_bo_driver = {
 	.ttm_tt_create = &vmw_ttm_tt_create,
+	.ttm_tt_populate = &ttm_page_alloc_ttm_tt_populate,
+	.ttm_tt_unpopulate = &ttm_page_alloc_ttm_tt_unpopulate,
 	.invalidate_caches = vmw_invalidate_caches,
 	.init_mem_type = vmw_init_mem_type,
 	.evict_flags = vmw_evict_flags,
include/drm/ttm/ttm_bo_driver.h +23 -18

63 lines not on this page · raw message

include/drm/ttm/ttm_page_alloc.h +18 -0
index fffb3bd..18a2d6b 100644
@@ -67,6 +67,24 @@ int ttm_page_alloc_init(struct ttm_mem_global *glob, unsigned max_pages);
 void ttm_page_alloc_fini(void);
 
 /**
+ * ttm_page_alloc_ttm_tt_populate:
+ *
+ * @ttm: The struct ttm_tt to contain the backing pages.
+ *
+ * Add backing pages to all of @ttm
+ */
+extern int ttm_page_alloc_ttm_tt_populate(struct ttm_tt *ttm);
+
+/**
+ * ttm_page_alloc_ttm_tt_unpopulate:
+ *
+ * @ttm: The struct ttm_tt which to free backing pages.
+ *
+ * Free all pages of @ttm
+ */
+extern void ttm_page_alloc_ttm_tt_unpopulate(struct ttm_tt *ttm);
+
+/**
  * Output the state of pools to debugfs file
  */
 extern int ttm_page_alloc_debugfs(struct seq_file *m, void *data);
Signature · 1 line
1.7.7.1
#20 lore raw

PATCH 10/13 ttm: Provide DMA aware TTM page pool code. V5

In reply to [email protected]

From: Konrad Rzeszutek Wilk <[email protected]>

In TTM world the pages for the graphic drivers are kept in three different
pools: write combined, uncached, and cached (write-back). When the pages are used by the graphic driver the graphic adapter via its built in MMU (or AGP) programs these pages in. The programming requires the virtual address (from the graphic adapter perspective) and the physical address (either System RAM or the memory on the card) which is obtained using the pci_map_* calls (which does the virtual to physical - or bus address translation). During the graphic application's "life" those pages can be shuffled around, swapped out to disk, moved from the VRAM to System RAM or vice-versa. This all works with the existing TTM pool code
- except when we want to use the software IOTLB (SWIOTLB) code to "map" the physical addresses to the graphic adapter MMU. We end up programming the bounce buffer's physical address instead of the TTM pool memory's and get a non-worky driver. There are two solutions:
1) using the DMA API to allocate pages that are screened by the DMA API, or
2) using the pci_sync_* calls to copy the pages from the bounce-buffer and back.

This patch fixes the issue by allocating pages using the DMA API. The second is a viable option - but it has performance drawbacks and potential correctness issues - think of the write cache page being bounced (SWIOTLB->TTM), the WC is set on the TTM page and the copy from SWIOTLB not making it to the TTM page until the page has been recycled in the pool (and used by another application).

The bounce buffer does not get activated often - only in cases where we have a 32-bit capable card and we want to use a page that is allocated above the 4GB limit. The bounce buffer offers the solution of copying the contents of that 4GB page to an location below 4GB and then back when the operation has been completed (or vice-versa). This is done by using the 'pci_sync_*' calls.
Note: If you look carefully enough in the existing TTM page pool code you will notice the GFP_DMA32 flag is used - which should guarantee that the provided page is under 4GB. It certainly is the case, except this gets ignored in two cases:
- If user specifies 'swiotlb=force' which bounces _every_ page.
- If user is using a Xen's PV Linux guest (which uses the SWIOTLB and the underlaying PFN's aren't necessarily under 4GB).

To not have this extra copying done the other option is to allocate the pages using the DMA API so that there is not need to map the page and perform the expensive 'pci_sync_*' calls.

This DMA API capable TTM pool requires for this the 'struct device' to properly call the DMA API. It also has to track the virtual and bus address of the page being handed out in case it ends up being swapped out or de-allocated - to make sure it is de-allocated using the proper's 'struct device'.

Implementation wise the code keeps two lists: one that is attached to the 'struct device' (via the dev->dma_pools list) and a global one to be used when the 'struct device' is unavailable (think shrinker code). The global list can iterate over all of the 'struct device' and its associated dma_pool. The list in dev->dma_pools can only iterate the device's dma_pool.

                                                            /[struct device_pool]\
        /---------------------------------------------------| dev                |
       /                                            +-------| dma_pool           |
 /-----+------\                                    /        \--------------------/
 |struct device|     /-->[struct dma_pool for WC]</         /[struct device_pool]\
 | dma_pools   +----+                                     /-| dev                |
 |  ...        |    \--->[struct dma_pool for uncached]<-/--| dma_pool           |
 \-----+------/                                         /   \--------------------/
        \----------------------------------------------/

[Two pools associated with the device (WC and UC), and the parallel list containing the 'struct dev' and 'struct dma_pool' entries]

The maximum amount of dma pools a device can have is six: write-combined, uncached, and cached; then there are the DMA32 variants which are:
write-combined dma32, uncached dma32, and cached dma32.

Currently this code only gets activated when any variant of the SWIOTLB IOMMU code is running (Intel without VT-d, AMD without GART, IBM Calgary and Xen PV with PCI devices).

[v1: Using swiotlb_nr_tbl instead of swiotlb_enabled]

[v2: Major overhaul - added 'inuse_list' to seperate used from inuse and reorder the order of lists to get better performance.]

[v3: Added comments/and some logic based on review, Added Jerome tag]
[v4: rebase on top of ttm_tt & ttm_backend merge]
[v5: rebase on top of ttm memory accounting overhaul]
---
 drivers/gpu/drm/ttm/Makefile             |    4 +
 drivers/gpu/drm/ttm/ttm_memory.c         |    2 +
 drivers/gpu/drm/ttm/ttm_page_alloc_dma.c | 1235 ++++++++++++++++++++++++++++++
 include/drm/ttm/ttm_bo_driver.h          |    2 +
 include/drm/ttm/ttm_page_alloc.h         |   36 +
 5 files changed, 1279 insertions(+), 0 deletions(-)
 create mode 100644 drivers/gpu/drm/ttm/ttm_page_alloc_dma.c
drivers/gpu/drm/ttm/Makefile +4 -0
index f3cf6f0..b2b33dd 100644
@@ -7,4 +7,8 @@ ttm-y := ttm_agp_backend.o ttm_memory.o ttm_tt.o ttm_bo.o \
 	ttm_object.o ttm_lock.o ttm_execbuf_util.o ttm_page_alloc.o \
 	ttm_bo_manager.o
 
+ifeq ($(CONFIG_SWIOTLB),y)
+ttm-y += ttm_page_alloc_dma.o
+endif
+
 obj-$(CONFIG_DRM_TTM) += ttm.o
drivers/gpu/drm/ttm/ttm_memory.c +2 -0
index 98f6899..3b12f05 100644
@@ -258,6 +258,7 @@ int ttm_mem_global_init(struct ttm_mem_global *glob)
 	glob->swap_limit = glob->max_mem - (glob->mem >> 3);
 	glob->swap_dma32_limit = ((1ULL << 32) >> 1) - (((1ULL << 32) >> 3));
 	ttm_page_alloc_init(glob, glob->max_mem/(2*PAGE_SIZE));
+	ttm_dma_page_alloc_init(glob, glob->max_mem/(2*PAGE_SIZE));
 	return 0;
 }
 EXPORT_SYMBOL(ttm_mem_global_init);
@@ -266,6 +267,7 @@ void ttm_mem_global_release(struct ttm_mem_global *glob)
 {
 	/* let the page allocator first stop the shrink work. */
 	ttm_page_alloc_fini();
+	ttm_dma_page_alloc_fini();
 
 	flush_workqueue(glob->swap_queue);
 	destroy_workqueue(glob->swap_queue);
drivers/gpu/drm/ttm/ttm_page_alloc_dma.c new file +1235 -0

1,235 lines not on this page · raw message

include/drm/ttm/ttm_bo_driver.h +2 -0
index ae06e42..beef9ab 100644
@@ -104,6 +104,7 @@ enum ttm_caching_state {
  * @caching_state: The current caching state of the pages.
  * @state: The current binding state of the pages.
  * @dma_address: The DMA (bus) addresses of the pages (if TTM_PAGE_FLAG_DMA32)
+ * @alloc_list: used by some page allocation backend
  *
  * This is a structure holding the pages, caching- and aperture binding
  * status for a buffer object that isn't backed by fixed (VRAM / AGP)
@@ -127,6 +128,7 @@ struct ttm_tt {
 		tt_unpopulated,
 	} state;
 	dma_addr_t *dma_address;
+	struct list_head alloc_list;
 };
 
 #define TTM_MEMTYPE_FLAG_FIXED         (1 << 0)	/* Fixed (on-card) PCI memory */
include/drm/ttm/ttm_page_alloc.h +36 -0
index 18a2d6b..f127635 100644
@@ -88,4 +88,40 @@ extern void ttm_page_alloc_ttm_tt_unpopulate(struct ttm_tt *ttm);
  * Output the state of pools to debugfs file
  */
 extern int ttm_page_alloc_debugfs(struct seq_file *m, void *data);
+
+
+#ifdef CONFIG_SWIOTLB
+/**
+ * Initialize pool allocator.
+ */
+int ttm_dma_page_alloc_init(struct ttm_mem_global *glob, unsigned max_pages);
+
+/**
+ * Free pool allocator.
+ */
+void ttm_dma_page_alloc_fini(void);
+
+/**
+ * Output the state of pools to debugfs file
+ */
+extern int ttm_dma_page_alloc_debugfs(struct seq_file *m, void *data);
+
+int ttm_dma_populate(struct ttm_tt *ttm, struct device *dev);
+extern void ttm_dma_unpopulate(struct ttm_tt *ttm, struct device *dev);
+
+#else
+static inline int ttm_dma_page_alloc_init(struct ttm_mem_global *glob,
+					  unsigned max_pages)
+{
+	return -ENODEV;
+}
+
+static inline void ttm_dma_page_alloc_fini(void) { return; }
+
+static inline int ttm_dma_page_alloc_debugfs(struct seq_file *m, void *data)
+{
+	return 0;
+}
+#endif
+
 #endif
Signature · 6 lines
1.7.7.1

_______________________________________________
dri-devel mailing list
[email protected]
http://lists.freedesktop.org/mailman/listinfo/dri-devel
#21 lore raw

PATCH 11/13 swiotlb: Expose swiotlb_nr_tlb function to modules

In reply to [email protected]

From: Konrad Rzeszutek Wilk <[email protected]>

As a mechanism to detect whether SWIOTLB is enabled or not. We also fix the spelling - it was swioltb instead of swiotlb.

CC FUJITA Tomonori <[email protected]>
[v1: Ripped out swiotlb_enabled]
---
 drivers/xen/swiotlb-xen.c |    2 +-
 include/linux/swiotlb.h   |    2 +-
 lib/swiotlb.c             |    5 +++--
 3 files changed, 5 insertions(+), 4 deletions(-)
drivers/xen/swiotlb-xen.c +1 -1
index c984768..c50fb0b 100644
@@ -152,7 +152,7 @@ void __init xen_swiotlb_init(int verbose)
 	char *m = NULL;
 	unsigned int repeat = 3;
 
-	nr_tbl = swioltb_nr_tbl();
+	nr_tbl = swiotlb_nr_tbl();
 	if (nr_tbl)
 		xen_io_tlb_nslabs = nr_tbl;
 	else {
include/linux/swiotlb.h +1 -1
index 445702c..e872526 100644
@@ -24,7 +24,7 @@ extern int swiotlb_force;
 
 extern void swiotlb_init(int verbose);
 extern void swiotlb_init_with_tbl(char *tlb, unsigned long nslabs, int verbose);
-extern unsigned long swioltb_nr_tbl(void);
+extern unsigned long swiotlb_nr_tbl(void);
 
 /*
  * Enumeration for sync targets
lib/swiotlb.c +3 -2
index 99093b3..058935e 100644
@@ -110,11 +110,11 @@ setup_io_tlb_npages(char *str)
 __setup("swiotlb=", setup_io_tlb_npages);
 /* make io_tlb_overflow tunable too? */
 
-unsigned long swioltb_nr_tbl(void)
+unsigned long swiotlb_nr_tbl(void)
 {
 	return io_tlb_nslabs;
 }
-
+EXPORT_SYMBOL_GPL(swiotlb_nr_tbl);
 /* Note that this doesn't work with highmem page */
 static dma_addr_t swiotlb_virt_to_bus(struct device *hwdev,
 				      volatile void *address)
@@ -321,6 +321,7 @@ void __init swiotlb_free(void)
 		free_bootmem_late(__pa(io_tlb_start),
 				  PAGE_ALIGN(io_tlb_nslabs << IO_TLB_SHIFT));
 	}
+	io_tlb_nslabs = 0;
 }
 
 static int is_swiotlb_buffer(phys_addr_t paddr)
Signature · 1 line
1.7.7.1
#22 lore raw

PATCH 12/13 drm/radeon/kms: Enable the TTM DMA pool if swiotlb is on V2

In reply to [email protected]

From: Konrad Rzeszutek Wilk <[email protected]>

With the exception that we do not handle the AGP case. We only deal with PCIe cards such as ATI ES1000 or HD3200 that have been detected to only do DMA up to 32-bits.

V2 force dma32 if we fail to set bigger dma mask

---
 drivers/gpu/drm/radeon/radeon.h        |    1 -
 drivers/gpu/drm/radeon/radeon_device.c |    6 ++
 drivers/gpu/drm/radeon/radeon_gart.c   |   29 +-----------
 drivers/gpu/drm/radeon/radeon_ttm.c    |   83 +++++++++++++++++++++++++++++--
 4 files changed, 84 insertions(+), 35 deletions(-)
drivers/gpu/drm/radeon/radeon.h +0 -1
index e3170c7..63257ba 100644
@@ -332,7 +332,6 @@ struct radeon_gart {
 	union radeon_gart_table		table;
 	struct page			**pages;
 	dma_addr_t			*pages_addr;
-	bool				*ttm_alloced;
 	bool				ready;
 };
 
drivers/gpu/drm/radeon/radeon_device.c +6 -0
index c33bc91..7c31321 100644
@@ -765,8 +765,14 @@ int radeon_device_init(struct radeon_device *rdev,
 	r = pci_set_dma_mask(rdev->pdev, DMA_BIT_MASK(dma_bits));
 	if (r) {
 		rdev->need_dma32 = true;
+		dma_bits = 32;
 		printk(KERN_WARNING "radeon: No suitable DMA available.\n");
 	}
+	r = pci_set_consistent_dma_mask(rdev->pdev, DMA_BIT_MASK(dma_bits));
+	if (r) {
+		pci_set_consistent_dma_mask(rdev->pdev, DMA_BIT_MASK(32));
+		printk(KERN_WARNING "radeon: No coherent DMA available.\n");
+	}
 
 	/* Registers mapping */
 	/* TODO: block userspace mapping of io register */
drivers/gpu/drm/radeon/radeon_gart.c +1 -28
index fdc3a9a..18f496c 100644
@@ -149,9 +149,6 @@ void radeon_gart_unbind(struct radeon_device *rdev, unsigned offset,
 	p = t / (PAGE_SIZE / RADEON_GPU_PAGE_SIZE);
 	for (i = 0; i < pages; i++, p++) {
 		if (rdev->gart.pages[p]) {
-			if (!rdev->gart.ttm_alloced[p])
-				pci_unmap_page(rdev->pdev, rdev->gart.pages_addr[p],
-				       		PAGE_SIZE, PCI_DMA_BIDIRECTIONAL);
 			rdev->gart.pages[p] = NULL;
 			rdev->gart.pages_addr[p] = rdev->dummy_page.addr;
 			page_base = rdev->gart.pages_addr[p];
@@ -181,23 +178,7 @@ int radeon_gart_bind(struct radeon_device *rdev, unsigned offset,
 	p = t / (PAGE_SIZE / RADEON_GPU_PAGE_SIZE);
 
 	for (i = 0; i < pages; i++, p++) {
-		/* we reverted the patch using dma_addr in TTM for now but this
-		 * code stops building on alpha so just comment it out for now */
-		if (0) { /*dma_addr[i] != DMA_ERROR_CODE) */
-			rdev->gart.ttm_alloced[p] = true;
-			rdev->gart.pages_addr[p] = dma_addr[i];
-		} else {
-			/* we need to support large memory configurations */
-			/* assume that unbind have already been call on the range */
-			rdev->gart.pages_addr[p] = pci_map_page(rdev->pdev, pagelist[i],
-							0, PAGE_SIZE,
-							PCI_DMA_BIDIRECTIONAL);
-			if (pci_dma_mapping_error(rdev->pdev, rdev->gart.pages_addr[p])) {
-				/* FIXME: failed to map page (return -ENOMEM?) */
-				radeon_gart_unbind(rdev, offset, pages);
-				return -ENOMEM;
-			}
-		}
+		rdev->gart.pages_addr[p] = dma_addr[i];
 		rdev->gart.pages[p] = pagelist[i];
 		page_base = rdev->gart.pages_addr[p];
 		for (j = 0; j < (PAGE_SIZE / RADEON_GPU_PAGE_SIZE); j++, t++) {
@@ -259,12 +240,6 @@ int radeon_gart_init(struct radeon_device *rdev)
 		radeon_gart_fini(rdev);
 		return -ENOMEM;
 	}
-	rdev->gart.ttm_alloced = kzalloc(sizeof(bool) *
-					 rdev->gart.num_cpu_pages, GFP_KERNEL);
-	if (rdev->gart.ttm_alloced == NULL) {
-		radeon_gart_fini(rdev);
-		return -ENOMEM;
-	}
 	/* set GART entry to point to the dummy page by default */
 	for (i = 0; i < rdev->gart.num_cpu_pages; i++) {
 		rdev->gart.pages_addr[i] = rdev->dummy_page.addr;
@@ -281,10 +256,8 @@ void radeon_gart_fini(struct radeon_device *rdev)
 	rdev->gart.ready = false;
 	kfree(rdev->gart.pages);
 	kfree(rdev->gart.pages_addr);
-	kfree(rdev->gart.ttm_alloced);
 	rdev->gart.pages = NULL;
 	rdev->gart.pages_addr = NULL;
-	rdev->gart.ttm_alloced = NULL;
 
 	radeon_dummy_page_fini(rdev);
 }
drivers/gpu/drm/radeon/radeon_ttm.c +77 -6

104 lines not on this page · raw message

Signature · 1 line
1.7.7.1
#23 lore raw

PATCH 13/13 drm/nouveau: enable the TTM DMA pool on 32-bit DMA only device V2

In reply to [email protected]

From: Konrad Rzeszutek Wilk <[email protected]>

If the card is capable of more than 32-bit, then use the default TTM page pool code which allocates from anywhere in the memory.

Note: If the 'ttm.no_dma' parameter is set, the override is ignored and the default TTM pool is used.

V2 use pci_set_consistent_dma_mask

---
 drivers/gpu/drm/nouveau/nouveau_bo.c      |   73 ++++++++++++++++++++++++++++-
 drivers/gpu/drm/nouveau/nouveau_debugfs.c |    1 +
 drivers/gpu/drm/nouveau/nouveau_mem.c     |    6 ++
 drivers/gpu/drm/nouveau/nouveau_sgdma.c   |   60 +-----------------------
 4 files changed, 79 insertions(+), 61 deletions(-)
drivers/gpu/drm/nouveau/nouveau_bo.c +71 -2
index 7e5ca3f..36234a7 100644
@@ -1049,10 +1049,79 @@ nouveau_bo_fence(struct nouveau_bo *nvbo, struct nouveau_fence *fence)
 	nouveau_fence_unref(&old_fence);
 }
 
+static int
+nouveau_ttm_tt_populate(struct ttm_tt *ttm)
+{
+	struct drm_nouveau_private *dev_priv;
+	struct drm_device *dev;
+	unsigned i;
+	int r;
+
+	if (ttm->state != tt_unpopulated)
+		return 0;
+
+	dev_priv = nouveau_bdev(ttm->bdev);
+	dev = dev_priv->dev;
+
+#ifdef CONFIG_SWIOTLB
+	if ((dma_get_mask(dev->dev) <= DMA_BIT_MASK(32)) && swiotlb_nr_tbl()) {
+		return ttm_dma_populate(ttm, dev->dev);
+	}
+#endif
+
+	r = ttm_page_alloc_ttm_tt_populate(ttm);
+	if (r) {
+		return r;
+	}
+
+	for (i = 0; i < ttm->num_pages; i++) {
+		ttm->dma_address[i] = pci_map_page(dev->pdev, ttm->pages[i],
+						   0, PAGE_SIZE,
+						   PCI_DMA_BIDIRECTIONAL);
+		if (pci_dma_mapping_error(dev->pdev, ttm->dma_address[i])) {
+			while (--i) {
+				pci_unmap_page(dev->pdev, ttm->dma_address[i],
+					       PAGE_SIZE, PCI_DMA_BIDIRECTIONAL);
+				ttm->dma_address[i] = 0;
+			}
+			ttm_page_alloc_ttm_tt_unpopulate(ttm);
+			return -EFAULT;
+		}
+	}
+	return 0;
+}
+
+static void
+nouveau_ttm_tt_unpopulate(struct ttm_tt *ttm)
+{
+	struct drm_nouveau_private *dev_priv;
+	struct drm_device *dev;
+	unsigned i;
+
+	dev_priv = nouveau_bdev(ttm->bdev);
+	dev = dev_priv->dev;
+
+#ifdef CONFIG_SWIOTLB
+	if ((dma_get_mask(dev->dev) <= DMA_BIT_MASK(32)) && swiotlb_nr_tbl()) {
+		ttm_dma_unpopulate(ttm, dev->dev);
+		return;
+	}
+#endif
+
+	for (i = 0; i < ttm->num_pages; i++) {
+		if (ttm->dma_address[i]) {
+			pci_unmap_page(dev->pdev, ttm->dma_address[i],
+				       PAGE_SIZE, PCI_DMA_BIDIRECTIONAL);
+		}
+	}
+
+	ttm_page_alloc_ttm_tt_unpopulate(ttm);
+}
+
 struct ttm_bo_driver nouveau_bo_driver = {
 	.ttm_tt_create = &nouveau_ttm_tt_create,
-	.ttm_tt_populate = &ttm_page_alloc_ttm_tt_populate,
-	.ttm_tt_unpopulate = &ttm_page_alloc_ttm_tt_unpopulate,
+	.ttm_tt_populate = &nouveau_ttm_tt_populate,
+	.ttm_tt_unpopulate = &nouveau_ttm_tt_unpopulate,
 	.invalidate_caches = nouveau_bo_invalidate_caches,
 	.init_mem_type = nouveau_bo_init_mem_type,
 	.evict_flags = nouveau_bo_evict_flags,
drivers/gpu/drm/nouveau/nouveau_debugfs.c +1 -0
index 8e15923..f52c2db 100644
@@ -178,6 +178,7 @@ static struct drm_info_list nouveau_debugfs_list[] = {
 	{ "memory", nouveau_debugfs_memory_info, 0, NULL },
 	{ "vbios.rom", nouveau_debugfs_vbios_image, 0, NULL },
 	{ "ttm_page_pool", ttm_page_alloc_debugfs, 0, NULL },
+	{ "ttm_dma_page_pool", ttm_dma_page_alloc_debugfs, 0, NULL },
 };
 #define NOUVEAU_DEBUGFS_ENTRIES ARRAY_SIZE(nouveau_debugfs_list)
 
drivers/gpu/drm/nouveau/nouveau_mem.c +6 -0
index 36bec48..37fcaa2 100644
@@ -407,6 +407,12 @@ nouveau_mem_vram_init(struct drm_device *dev)
 	ret = pci_set_dma_mask(dev->pdev, DMA_BIT_MASK(dma_bits));
 	if (ret)
 		return ret;
+	ret = pci_set_consistent_dma_mask(dev->pdev, DMA_BIT_MASK(dma_bits));
+	if (ret) {
+		/* Reset to default value. */
+		pci_set_consistent_dma_mask(dev->pdev, DMA_BIT_MASK(32));
+	}
+
 
 	ret = nouveau_ttm_global_init(dev_priv);
 	if (ret)
drivers/gpu/drm/nouveau/nouveau_sgdma.c +1 -59

122 lines not on this page · raw message

Signature · 1 line
1.7.7.1
#24 lore raw
In reply to [email protected]
Quoted 10 lines · On 11/09/2011 09:22 PM, [email protected] wrote:

So i did an overhaul of ttm_memory, i believe the simplification i did make sense. See patch 5 for a longer explanation.

Thomas with the ttm_memory change the allocation of pages won't happen if the accounting report that we are going over the limit and bo shrinker failed to free any memory to make room.

The handling of dma32 zone is done as post pass of ttm memory accounting.

OK. I'll take a deeper look into this.

Quoted 5 lines

Regarding the pagefault comment i removed, it doesn't make sense anymore because now we populate the whole page table in one shot. So there is no more prefaulting few pages but a full prefaulting. Thought i can add a comment stating that if you like.

It's important that we distinguish between populating, which populates pages, and faulting, which add ptes pointing to those pages.

Previously populating happened as a side effect of faulting, but now that populating is done in a single step, faulting (adding ptes) is still not. Usually a fault() handler adds a single pte, but TTM is different and tries to prefault more, but it is important that we only error on the first pte, so that's why the comment should stay.

Quoted 5 lines

For the ttm_tt_dma struct to hold page allocator specific informations i think it can be done as an followup patch but if you prefer to have that in this patchset let me know i will respin with such changes.

I'm fine with having it as a separate patchset as long as it gets done :).

Quoted 6 lines

I am in the process of retesting this whole serie and especialy the while memory accounting.

Cheers,
Jerome

/Thomas

#25 lore raw
Quoted 20 lines · Thomas Hellstrom
Quoted 8 lines · On 11/09/2011 09:22 PM, [email protected] wrote:

So i did an overhaul of ttm_memory, i believe the simplification i did make sense. See patch 5 for a longer explanation.

Thomas with the ttm_memory change the allocation of pages won't happen if the accounting report that we are going over the limit and bo shrinker failed to free any memory to make room.

The handling of dma32 zone is done as post pass of ttm memory accounting.

OK. I'll take a deeper look into this.

Quoted 4 lines

Regarding the pagefault comment i removed, it doesn't make sense anymore because now we populate the whole page table in one shot. So there is no more prefaulting few pages but a full prefaulting. Thought i can add a comment stating that if you like.

It's important that we distinguish between populating, which populates pages,
and faulting, which add ptes pointing to those pages.

Quoted 8 lines

Previously populating happened as a side effect of faulting, but now that populating is done
in a single step, faulting (adding ptes) is still not. Usually a fault() handler adds a single pte,
but TTM is different and tries to prefault more, but it is important that we only error on the first
pte, so that's why the comment should stay.

Well yes it only fill numprefault pte, but no error can happen in the prefault loop except for vm_insert_mixed failure, it's why i kept the report error only if it fails on the first page. I actually did a full pte populate at one point while working on that, dunno if that would make sense.

Quoted 7 lines
Quoted 4 lines

For the ttm_tt_dma struct to hold page allocator specific informations i think it can be done as an followup patch but if you prefer to have that in this patchset let me know i will respin with such changes.

I'm fine with having it as a separate patchset as long as it gets done :).

I will spin a patch for that on top of the patchset.

Cheers,
Jerome

#6 lore raw

Re: [PATCH 05/13] drm/ttm: overhaul memory accounting

In reply to [email protected]
Quoted 21 lines · On 11/09/2011 09:22 PM, [email protected] wrote:

From: Jerome Glisse<[email protected]>

This is an overhaul of the ttm memory accounting. This tries to keep the same global behavior while removing the whole zone concept. It keeps a distrinction for dma32 so that we make sure that ttm don't starve the dma32 zone.

There is 3 threshold for memory allocation :
- max_mem is the maximum memory the whole ttm infrastructure is going to allow allocation for (exception of system process see below)
- emer_mem is the maximum memory allowed for system process, this limit is> to max_mem
- swap_limit is the threshold at which point ttm will start to try to swap object because ttm is getting close the max_mem limit
- swap_dma32_limit is the threshold at which point ttm will start swap object to try to reduce the pressure on the dma32 zone. Note that we don't specificly target object to swap to it might very well free more memory from highmem rather than from dma32

Quoted 10 lines

Accounting is done through used_mem& used_dma32_mem, which sum give the total amount of memory actually accounted by ttm.

Idea is that allocation will fail if (used_mem + used_dma32_mem)> max_mem and if swapping fail to make enough room.

The used_dma32_mem can be updated as a later stage, allowing to perform accounting test before allocating a whole batch of pages.

Jerome, you're removing a fair amount of functionality here, without justifying why it could be removed.

Consider a low-end system with 1G of kernel memory and 10G of highmem. How do we avoid putting stress on the kernel memory? I also wouldn't be too surprised if DMA32 zones appear in HIGHMEM systems in the future making the current zone concept good to keep.

Also, in effect you move the DOS from *all* zones into the DMA32 zone and create a race in that multiple simultaneous allocators can first pre-allocate out of the global zone, and then update the DMA32 zone without synchronization. In this way you might theoretically end up with more DMA32 pages allocated than present in the zone.

With the proposed code there's also a theoretical problem in that a potentially huge number of pages are unaccounted before they are actually freed.

A possible way around all this is to pre-allocate out of *all* zones, and after the big allocation release back memory to relevant zones. If such a big allocation fails, one needs to revert back to a page-by-page scheme.

/Thomas

#7 lore raw

Re: [PATCH 05/13] drm/ttm: overhaul memory accounting

Quoted 28 lines · Thomas Hellstrom
Quoted 27 lines · On 11/09/2011 09:22 PM, [email protected] wrote:

From: Jerome Glisse<[email protected]>

This is an overhaul of the ttm memory accounting. This tries to keep the same global behavior while removing the whole zone concept. It keeps a distrinction for dma32 so that we make sure that ttm don't starve the dma32 zone.

There is 3 threshold for memory allocation :
- max_mem is the maximum memory the whole ttm infrastructure is going to allow allocation for (exception of system process see below)
- emer_mem is the maximum memory allowed for system process, this limit is> to max_mem
- swap_limit is the threshold at which point ttm will start to try to swap object because ttm is getting close the max_mem limit
- swap_dma32_limit is the threshold at which point ttm will start swap object to try to reduce the pressure on the dma32 zone. Note that we don't specificly target object to swap to it might very well free more memory from highmem rather than from dma32

Accounting is done through used_mem& used_dma32_mem, which sum give the total amount of memory actually accounted by ttm.

Idea is that allocation will fail if (used_mem + used_dma32_mem)> max_mem and if swapping fail to make enough room.

Quoted 7 lines
Quoted 3 lines

The used_dma32_mem can be updated as a later stage, allowing to perform accounting test before allocating a whole batch of pages.

Jerome, you're removing a fair amount of functionality here, without justifying
why it could be removed.

All this code was overkill.

Quoted 4 lines

Consider a low-end system with 1G of kernel memory and 10G of highmem. How do we avoid putting stress on the kernel memory? I also wouldn't be too surprised if DMA32 zones appear in HIGHMEM systems in the future making the current zone concept good to keep.

Right now kernel memory is accounted against all zone so it decrease not only the kernel zone but also the dma32 & highmem if present. Note also that kernel zone in current code == dma32 zone.

When it comes to future it looks a lot simpler, it seems everyone is moving toward more capable and more advanced iommu that can remove all the restriction on memory from the device pov. I mean even arm are getting more and more advance iommu. I don't see any architecture worse supporting not going down that road.

Quoted 5 lines

Also, in effect you move the DOS from *all* zones into the DMA32 zone and create a race in that multiple simultaneous allocators can first pre-allocate out of the global zone, and then update the DMA32 zone without synchronization. In this way you might theoretically end up with more DMA32 pages allocated than present in the zone.

Ok a respin attached with a simple change, things will be accounted against dma32 zone and only when we get page we will decrease the dma32 zone usage that way no DOS on dma32.

It also deals with the case where there is still lot of highmem but no more dma32.

Quoted 3 lines

With the proposed code there's also a theoretical problem in that a potentially huge number of pages are unaccounted before they are actually freed.

What you mean unaccounted ? The way it works is :
r = global_memory_alloc(size)
if (r) fail
alloc pages
update memory accounting according to what page where allocated

So memory is always accounted before even being allocated (exception are the kernel object for vmwgfx & ttm_bo but we can move accounting there too if you want, those are small allocation and i didn't think it was worse changing that).

Quoted 6 lines

A possible way around all this is to pre-allocate out of *all* zones, and after the big allocation release back memory to relevant zones. If such a big allocation fails, one needs to revert back to a page-by-page scheme.

/Thomas

Really i believe this make the whole accounting a lot simpler. The whole zone business was overkill as especialy kernelzone == dma32zone and highmem zone is a superset of this.

With my change what happen is that only the dma32 distinction is kept around because there is whole batch of device that can only do dma32 and we need to make sure we don't starve those.

I believe my code is lot easier and straighforward to understand.

Cheers,
Jerome

Quoted 1 line

From 03ec0abcfa4bc3060fd0d7a797758fc50088ec29 Mon Sep 17 00:00:00 2001

From: Jerome Glisse <[email protected]>
Date: Wed, 9 Nov 2011 08:11:00 -0500
Subject: [PATCH] drm/ttm: overhaul memory accounting V2

This is an overhaul of the ttm memory accounting. This tries to keep the same global behavior while removing the whole zone concept. It keeps a distrinction for dma32 so that we make sure that ttm don't starve the dma32 zone.

There is 3 threshold for memory allocation :
- max_mem is the maximum memory the whole ttm infrastructure is going to allow allocation for (exception of system process see below)
- emer_mem is the maximum memory allowed for system process, this limit is > to max_mem
- swap_limit is the threshold at which point ttm will start to try to swap object because ttm is getting close the max_mem limit
- swap_dma32_limit is the threshold at which point ttm will start swap object to try to reduce the pressure on the dma32 zone. Note that we don't specificly target object to swap to it might very well free more memory from highmem rather than from dma32

Accounting is done through used_mem & used_dma32_mem, which sum give the total amount of memory actually accounted by ttm.

Idea is that allocation will fail if (used_mem + used_dma32_mem) > max_mem and if swapping fail to make enough room.

The used_dma32_mem can be updated as a later stage, allowing to perform accounting test before allocating a whole batch of pages.

V2 Move accouting before page allocation instead of leaving that to a later patch. Always account first against dma32 zone and only as a post pass rebalance accouting to highmem if pages allow it.

---
 drivers/gpu/drm/ttm/ttm_bo.c             |    9 +-
 drivers/gpu/drm/ttm/ttm_memory.c         |  554 ++++++++++--------------------
 drivers/gpu/drm/ttm/ttm_object.c         |   10 +-
 drivers/gpu/drm/ttm/ttm_tt.c             |   14 +-
 drivers/gpu/drm/vmwgfx/vmwgfx_fence.c    |   30 +-
 drivers/gpu/drm/vmwgfx/vmwgfx_resource.c |   51 ++--
 include/drm/ttm/ttm_memory.h             |   27 +-
 7 files changed, 263 insertions(+), 432 deletions(-)
drivers/gpu/drm/ttm/ttm_bo.c +6 -3
index 4bde335..b3674ba 100644
@@ -1248,13 +1248,16 @@ int ttm_bo_create(struct ttm_bo_device *bdev,
 {
 	struct ttm_buffer_object *bo;
 	struct ttm_mem_global *mem_glob = bdev->glob->mem_glob;
+	unsigned long available;
 	int ret;
 
 	size_t acc_size =
 	    ttm_bo_size(bdev->glob, (size + PAGE_SIZE - 1) >> PAGE_SHIFT);
-	ret = ttm_mem_global_alloc(mem_glob, acc_size, false, false);
-	if (unlikely(ret != 0))
-		return ret;
+	available = ttm_mem_global_alloc(mem_glob, acc_size, false);
+	if (available != acc_size) {
+		ttm_mem_global_free(mem_glob, available);
+		return -ENOMEM;
+	}
 
 	bo = kzalloc(sizeof(*bo), GFP_KERNEL);
 
drivers/gpu/drm/ttm/ttm_memory.c +181 -373

714 lines not on this page · raw message

drivers/gpu/drm/ttm/ttm_object.c +6 -4
index 93577f2..c3b9e2c 100644
@@ -250,6 +250,7 @@ int ttm_ref_object_add(struct ttm_object_file *tfile,
 	struct ttm_ref_object *ref;
 	struct drm_hash_item *hash;
 	struct ttm_mem_global *mem_glob = tfile->tdev->mem_glob;
+	unsigned long available;
 	int ret = -EINVAL;
 
 	if (existed != NULL)
@@ -267,10 +268,11 @@ int ttm_ref_object_add(struct ttm_object_file *tfile,
 		}
 
 		read_unlock(&tfile->lock);
-		ret = ttm_mem_global_alloc(mem_glob, sizeof(*ref),
-					   false, false);
-		if (unlikely(ret != 0))
-			return ret;
+		available = ttm_mem_global_alloc(mem_glob, sizeof(*ref), false);
+		if (available != sizeof(*ref)) {
+			ttm_mem_global_free(mem_glob, available);
+			return -ENOMEM;
+		}
 		ref = kmalloc(sizeof(*ref), GFP_KERNEL);
 		if (unlikely(ref == NULL)) {
 			ttm_mem_global_free(mem_glob, sizeof(*ref));
drivers/gpu/drm/ttm/ttm_tt.c +10 -4
index 3fb4c6d..d317bd4 100644
@@ -67,24 +67,30 @@ static struct page *__ttm_tt_get_page(struct ttm_tt *ttm, int index)
 	struct page *p;
 	struct list_head h;
 	struct ttm_mem_global *mem_glob = ttm->glob->mem_glob;
+	unsigned long available;
 	int ret;
 
 	if (NULL == (p = ttm->pages[index])) {
 
 		INIT_LIST_HEAD(&h);
 
+		available = ttm_mem_global_alloc(mem_glob, PAGE_SIZE, false);
+
 		ret = ttm_get_pages(&h, ttm->page_flags, ttm->caching_state, 1,
 				    &ttm->dma_address[index]);
-
-		if (ret != 0)
+		if (ret != 0) {
+			ttm_mem_global_free(mem_glob, available);
 			return NULL;
+		}
 
 		p = list_first_entry(&h, struct page, lru);
 
-		ret = ttm_mem_global_alloc_page(mem_glob, p, false, false);
-		if (unlikely(ret != 0))
+		if (available < PAGE_SIZE && page_to_pfn(p) < 0x00100000UL) {
+			ttm_mem_global_free(mem_glob, available);
 			goto out_err;
+		}
 
+		ttm_mem_global_alloc_page(mem_glob, p);
 		ttm->pages[index] = p;
 	}
 	return p;
drivers/gpu/drm/vmwgfx/vmwgfx_fence.c +18 -12
index 15fb260..f1d4b31 100644
@@ -501,12 +501,14 @@ int vmw_fence_create(struct vmw_fence_manager *fman,
 {
 	struct ttm_mem_global *mem_glob = vmw_mem_glob(fman->dev_priv);
 	struct vmw_fence_obj *fence;
+	unsigned long available;
 	int ret;
 
-	ret = ttm_mem_global_alloc(mem_glob, fman->fence_size,
-				   false, false);
-	if (unlikely(ret != 0))
-		return ret;
+	available = ttm_mem_global_alloc(mem_glob, fman->fence_size, false);
+	if (available != fman->fence_size) {
+		ttm_mem_global_free(mem_glob, available);
+		return -ENOMEM;
+	}
 
 	fence = kzalloc(sizeof(*fence), GFP_KERNEL);
 	if (unlikely(fence == NULL)) {
@@ -566,6 +568,7 @@ int vmw_user_fence_create(struct drm_file *file_priv,
 	struct vmw_user_fence *ufence;
 	struct vmw_fence_obj *tmp;
 	struct ttm_mem_global *mem_glob = vmw_mem_glob(fman->dev_priv);
+	unsigned long available;
 	int ret;
 
 	/*
@@ -573,10 +576,11 @@ int vmw_user_fence_create(struct drm_file *file_priv,
 	 * be created by a user-space request.
 	 */
 
-	ret = ttm_mem_global_alloc(mem_glob, fman->user_fence_size,
-				   false, false);
-	if (unlikely(ret != 0))
-		return ret;
+	available = ttm_mem_global_alloc(mem_glob, fman->user_fence_size, false);
+	if (available != fman->user_fence_size) {
+		ttm_mem_global_free(mem_glob, available);
+		return -ENOMEM;
+	}
 
 	ufence = kzalloc(sizeof(*ufence), GFP_KERNEL);
 	if (unlikely(ufence == NULL)) {
@@ -959,16 +963,18 @@ int vmw_event_fence_action_create(struct drm_file *file_priv,
 	struct vmw_fence_manager *fman = fence->fman;
 	uint32_t size = fman->event_fence_action_size +
 		ttm_round_pot(event->length);
-	int ret;
+	unsigned long available;
 
 	/*
 	 * Account for internal structure size as well as the
 	 * event size itself.
 	 */
 
-	ret = ttm_mem_global_alloc(mem_glob, size, false, interruptible);
-	if (unlikely(ret != 0))
-		return ret;
+	available = ttm_mem_global_alloc(mem_glob, size, false);
+	if (available != size) {
+		ttm_mem_global_free(mem_glob, available);
+		return -ENOMEM;
+	}
 
 	eaction = kzalloc(sizeof(*eaction), GFP_KERNEL);
 	if (unlikely(eaction == NULL)) {
drivers/gpu/drm/vmwgfx/vmwgfx_resource.c +28 -23

102 lines not on this page · raw message

include/drm/ttm/ttm_memory.h +14 -13
index 26c1f78..e001ff4 100644
@@ -81,14 +81,14 @@ struct ttm_mem_global {
 	struct work_struct work;
 	wait_queue_head_t queue;
 	spinlock_t lock;
-	struct ttm_mem_zone *zones[TTM_MEM_MAX_ZONES];
-	unsigned int num_zones;
-	struct ttm_mem_zone *zone_kernel;
-#ifdef CONFIG_HIGHMEM
-	struct ttm_mem_zone *zone_highmem;
-#else
-	struct ttm_mem_zone *zone_dma32;
-#endif
+	unsigned long mem;
+	unsigned long max_mem;
+	unsigned long max_dma32_mem;
+	unsigned long emer_mem;
+	unsigned long used_mem;
+	unsigned long used_dma32_mem;
+	unsigned long swap_limit;
+	unsigned long swap_dma32_limit;
 };
 
 /**
@@ -146,14 +146,15 @@ static inline void ttm_mem_unregister_shrink(struct ttm_mem_global *glob,
 
 extern int ttm_mem_global_init(struct ttm_mem_global *glob);
 extern void ttm_mem_global_release(struct ttm_mem_global *glob);
-extern int ttm_mem_global_alloc(struct ttm_mem_global *glob, uint64_t memory,
-				bool no_wait, bool interruptible);
+extern unsigned long ttm_mem_global_alloc(struct ttm_mem_global *glob,
+					  uint64_t memory,
+					  bool no_wait);
 extern void ttm_mem_global_free(struct ttm_mem_global *glob,
 				uint64_t amount);
-extern int ttm_mem_global_alloc_page(struct ttm_mem_global *glob,
-				     struct page *page,
-				     bool no_wait, bool interruptible);
+extern void ttm_mem_global_alloc_page(struct ttm_mem_global *glob,
+				      struct page *page);
 extern void ttm_mem_global_free_page(struct ttm_mem_global *glob,
 				     struct page *page);
 extern size_t ttm_round_pot(size_t size);
+
 #endif
Signature · 1 line
1.7.7.1

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

#8 lore raw

Re: [PATCH 05/13] drm/ttm: overhaul memory accounting

Quoted 35 lines · Jerome Glisse
Quoted 33 lines · Thomas Hellstrom
Quoted 31 lines · On 11/09/2011 09:22 PM, [email protected] wrote:

From: Jerome Glisse<[email protected]>

This is an overhaul of the ttm memory accounting. This tries to keep the same global behavior while removing the whole zone concept. It keeps a distrinction for dma32 so that we make sure that ttm don't starve the dma32 zone.

There is 3 threshold for memory allocation :
- max_mem is the maximum memory the whole ttm infrastructure is going to allow allocation for (exception of system process see below)
- emer_mem is the maximum memory allowed for system process, this
limit is> to max_mem
- swap_limit is the threshold at which point ttm will start to try to swap object because ttm is getting close the max_mem limit
- swap_dma32_limit is the threshold at which point ttm will start swap object to try to reduce the pressure on the dma32 zone. Note that we don't specificly target object to swap to it might very well free more memory from highmem rather than from dma32

Accounting is done through used_mem& used_dma32_mem, which sum give
the total amount of memory actually accounted by ttm.

Idea is that allocation will fail if (used_mem + used_dma32_mem)> max_mem and if swapping fail to make enough room.

The used_dma32_mem can be updated as a later stage, allowing to perform accounting test before allocating a whole batch of pages.

Quoted 6 lines
Quoted 4 lines

Jerome, you're removing a fair amount of functionality here, without justifying
why it could be removed.

All this code was overkill.

[1] I don't agree, and since it's well tested, thought throught and working, I see no obvious reason to alter it, within the context of this patch series unless it's absolutely required for the functionality.

Quoted 10 lines
Quoted 7 lines

Consider a low-end system with 1G of kernel memory and 10G of highmem. How do we avoid putting stress on the kernel memory? I also wouldn't be too surprised if DMA32 zones appear in HIGHMEM systems in the future making the current zone concept good to keep.

Right now kernel memory is accounted against all zone so it decrease not only the kernel zone but also the dma32& highmem if present.

Do you mean that the code is incorrect? In that case, did you consider the fact
that zones may overlap? (Although I admit the name "highmem" might be misleading. Should be "total").

Quoted 2 lines

Note also that kernel zone in current code == dma32 zone.

Last time I looked, the highmem split was typically at slightly less than 1GB, depending on the hardware and desired setup. I admit that was some time ago, but has that really changed? On all archs? Furthermore, on !Highmem systems, All pages are in the kernel zone.

Quoted 6 lines

When it comes to future it looks a lot simpler, it seems everyone is moving toward more capable and more advanced iommu that can remove all the restriction on memory from the device pov. I mean even arm are getting more and more advance iommu. I don't see any architecture worse supporting not going down that road.

While the proposed change is probably possible, with different low <-> high splits depending on whether HIGHMEM is defined or not, I think [1] is a good reason for not pushing it through.

Quoted 7 lines
Quoted 7 lines

Also, in effect you move the DOS from *all* zones into the DMA32 zone and create a race in that multiple simultaneous allocators can first pre-allocate out of the global zone, and then update the DMA32 zone without synchronization. In this way you might theoretically end up with more DMA32 pages allocated than present in the zone.

Quoted 7 lines

Ok a respin attached with a simple change, things will be accounted against dma32 zone and only when we get page we will decrease the dma32 zone usage that way no DOS on dma32.

It also deals with the case where there is still lot of highmem but no more dma32.

So why not just do a ttm_mem_global_alloc() for the pages you want to allocate, and add a proper adjustment function if memory turns out to be either HIGHMEM or !DMA32

Quoted 11 lines
Quoted 5 lines

With the proposed code there's also a theoretical problem in that a potentially huge number of pages are unaccounted before they are actually freed.

What you mean unaccounted ? The way it works is :
r = global_memory_alloc(size)
if (r) fail
alloc pages
update memory accounting according to what page where allocated

Quoted 5 lines

So memory is always accounted before even being allocated (exception are the kernel object for vmwgfx& ttm_bo but we can move accounting there too if you want, those are small allocation and i didn't think it was worse changing that).

No, I mean the sequence

unaccount_page_array()
---Race--
free_page_array()

/Thomas

Quoted 3 lines

Cheers,
Jerome

#9 lore raw

Re: [PATCH 05/13] drm/ttm: overhaul memory accounting

Quoted 36 lines · Thomas Hellstrom
Quoted 35 lines · Jerome Glisse
Quoted 34 lines · Thomas Hellstrom
Quoted 21 lines · On 11/09/2011 09:22 PM, [email protected] wrote:

From: Jerome Glisse<[email protected]>

This is an overhaul of the ttm memory accounting. This tries to keep the same global behavior while removing the whole zone concept. It keeps a distrinction for dma32 so that we make sure that ttm don't starve the dma32 zone.

There is 3 threshold for memory allocation :
- max_mem is the maximum memory the whole ttm infrastructure is going to allow allocation for (exception of system process see below)
- emer_mem is the maximum memory allowed for system process, this
limit is> to max_mem
- swap_limit is the threshold at which point ttm will start to try to swap object because ttm is getting close the max_mem limit
- swap_dma32_limit is the threshold at which point ttm will start swap object to try to reduce the pressure on the dma32 zone. Note that we don't specificly target object to swap to it might very well free more memory from highmem rather than from dma32

Quoted 9 lines

Accounting is done through used_mem& used_dma32_mem, which sum give
the total amount of memory actually accounted by ttm.

Idea is that allocation will fail if (used_mem + used_dma32_mem)> max_mem and if swapping fail to make enough room.

The used_dma32_mem can be updated as a later stage, allowing to perform accounting test before allocating a whole batch of pages.

Jerome, you're removing a fair amount of functionality here, without justifying
why it could be removed.

Quoted 6 lines
Quoted 1 line

All this code was overkill.

[1] I don't agree, and since it's well tested, thought throught and working, I see no obvious reason to alter it,
within the context of this patch series unless it's absolutely required for the functionality.

Well one thing i can tell is that it doesn't work on radeon, i pushed a test to libdrm and here it's the oom that starts doing its beating. Anyway i won't alter it. Was just trying to make it works, ie be useful while also being simpler.

Quoted 11 lines
Quoted 6 lines
Quoted 4 lines

Consider a low-end system with 1G of kernel memory and 10G of highmem. How do we avoid putting stress on the kernel memory? I also wouldn't be too surprised if DMA32 zones appear in HIGHMEM systems in the future making the current zone concept good to keep.

Right now kernel memory is accounted against all zone so it decrease not only the kernel zone but also the dma32& highmem if present.

Do you mean that the code is incorrect? In that case, did you consider the fact
that zones may overlap? (Although I admit the name "highmem" might be misleading. Should be "total").

Yeah i am well aware that zone overlap :)

Quoted 6 lines
Quoted 1 line

Note also that kernel zone in current code == dma32 zone.

Last time I looked, the highmem split was typically at slightly less than 1GB, depending on the hardware and desired setup. I admit that was some time ago, but has that really changed? On all archs? Furthermore, on !Highmem systems, All pages are in the kernel zone.

I was bit too much focused on my system where 1G of ram is wonderland and 512M is the average. Thx to AMD i got a system with 8G i should use it more.

Cheers,
Jerome

#10 lore raw

Re: [PATCH 05/13] drm/ttm: overhaul memory accounting

Quoted 50 lines · Jerome Glisse
Quoted 41 lines · Thomas Hellstrom
Quoted 39 lines · Jerome Glisse
Quoted 37 lines · Thomas Hellstrom
Quoted 21 lines · On 11/09/2011 09:22 PM, [email protected] wrote:

From: Jerome Glisse<[email protected]>

This is an overhaul of the ttm memory accounting. This tries to keep the same global behavior while removing the whole zone concept. It keeps a distrinction for dma32 so that we make sure that ttm don't starve the dma32 zone.

There is 3 threshold for memory allocation :
- max_mem is the maximum memory the whole ttm infrastructure is going to allow allocation for (exception of system process see below)
- emer_mem is the maximum memory allowed for system process, this
limit is> to max_mem
- swap_limit is the threshold at which point ttm will start to try to swap object because ttm is getting close the max_mem limit
- swap_dma32_limit is the threshold at which point ttm will start swap object to try to reduce the pressure on the dma32 zone. Note that we don't specificly target object to swap to it might very well free more memory from highmem rather than from dma32

Quoted 10 lines

Accounting is done through used_mem& used_dma32_mem, which sum give
the total amount of memory actually accounted by ttm.

Idea is that allocation will fail if (used_mem + used_dma32_mem)> max_mem and if swapping fail to make enough room.

The used_dma32_mem can be updated as a later stage, allowing to perform accounting test before allocating a whole batch of pages.

Jerome, you're removing a fair amount of functionality here, without justifying
why it could be removed.

Quoted 7 lines
Quoted 2 lines

All this code was overkill.

[1] I don't agree, and since it's well tested, thought throught and working, I see no obvious reason to alter it,
within the context of this patch series unless it's absolutely required for the functionality.

Quoted 5 lines

Well one thing i can tell is that it doesn't work on radeon, i pushed a test to libdrm and here it's the oom that starts doing its beating. Anyway i won't alter it. Was just trying to make it works, ie be useful while also being simpler.

Well if it doesn't work it should of course be fixed.

I'm not against fixing it nor making it simpler, but I think that requires a detailed understanding of what's going wrong and how it needs to be fixed. Not as part of a patch series that really tries to accomplish something else.

The current code was tested extensively with psb and unichrome.
One good test for drivers with bo-backed textures is to continously create fairly large texture images. The end result should be the swap space starting to fill up and once there is no more swap space, the OOM killer should kill your app, and kmalloc failures should be avoided. It should be tricky to get a failure from the global alloc system, but a huge amount of small buffer objects or fence objects should probably do it.

Naturally, that requires that all persistent drm objects created from user-space are registered with their correct sizes, or at least a really good size approximation. That includes things like gem flinks, that could otherwise easily be exploited to bring a system down, simply by guessing a gem name and create flinks to that name in an infinite loop.

What are the symptoms of the failure you're seeing with Radeon? Any suggestions on why it happens?

Thanks,
Thomas

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

maintainer
#11 lore raw

Re: [PATCH 05/13] drm/ttm: overhaul memory accounting

Quoted 2 lines

What are the symptoms of the failure you're seeing with Radeon? Any suggestions on why it happens?

I suspect radeon swapping might have some issues, I really think we should put the effort into a test suite like the intel-gpu-tools, instead of moving code around with no idea what is better or worse. (Jerome you can take this as a strong hint to work on some tests :-)

Dave.

#12 lore raw

Re: [PATCH 05/13] drm/ttm: overhaul memory accounting

Quoted 10 lines · Dave Airlie
Quoted 3 lines

What are the symptoms of the failure you're seeing with Radeon? Any suggestions on why it happens?

I suspect radeon swapping might have some issues, I really think we should put the effort into a test suite like the intel-gpu-tools, instead of moving code around with no idea what is better or worse. (Jerome you can take this as a strong hint to work on some tests :-)

Dave.

I think many tests like this can be written using OpenGL (using some assumptions about where drivers store things like texture images etc. That could be useful for all drivers).

But for the specific radeon bo case, I had a quick look at the code:

1) Gem stuff is not accounted anywhere AFAICT. Gem idr memory? flink memory?
2) Radeon fences doesn't seem to be accounted anywhere.
3) Radeon bo accounting is completely incorrect. The accounting size given to ttm_bo_init() should be the size of the kmalloc'ed object, including size of page arrays and dma_addr arrays and gem stuff, but not the pages themselves. kmalloced memory sizes should also be rounded up to the nearest power of two to account for allocator granularity.

Before this is fixed, we shouldn't IMHO really blame TTM and try to rewrite it.

/Thomas

Quoted 5 lines

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

#13 lore raw

Re: [PATCH 05/13] drm/ttm: overhaul memory accounting

In reply to Thomas Hellstrom
Quoted 71 lines · Thomas Hellstrom
Quoted 38 lines · Jerome Glisse
Quoted 37 lines · Thomas Hellstrom
Quoted 36 lines · Jerome Glisse
Quoted 28 lines · Thomas Hellstrom
Quoted 27 lines · On 11/09/2011 09:22 PM, [email protected] wrote:

From: Jerome Glisse<[email protected]>

This is an overhaul of the ttm memory accounting. This tries to keep the same global behavior while removing the whole zone concept. It keeps a distrinction for dma32 so that we make sure that ttm don't starve the dma32 zone.

There is 3 threshold for memory allocation :
- max_mem is the maximum memory the whole ttm infrastructure is going to allow allocation for (exception of system process see below)
- emer_mem is the maximum memory allowed for system process, this
limit is> to max_mem
- swap_limit is the threshold at which point ttm will start to try to swap object because ttm is getting close the max_mem limit
- swap_dma32_limit is the threshold at which point ttm will start swap object to try to reduce the pressure on the dma32 zone. Note that we don't specificly target object to swap to it might very well free more memory from highmem rather than from dma32

Accounting is done through used_mem& used_dma32_mem, which sum give
the total amount of memory actually accounted by ttm.

Idea is that allocation will fail if (used_mem + used_dma32_mem)> max_mem and if swapping fail to make enough room.

Quoted 6 lines
Quoted 3 lines

The used_dma32_mem can be updated as a later stage, allowing to perform accounting test before allocating a whole batch of pages.

Jerome, you're removing a fair amount of functionality here, without justifying
why it could be removed.

All this code was overkill.

Quoted 8 lines
Quoted 4 lines

[1] I don't agree, and since it's well tested, thought throught and working, I see no obvious reason to alter it,
within the context of this patch series unless it's absolutely required for the functionality.

Well one thing i can tell is that it doesn't work on radeon, i pushed a test to libdrm and here it's the oom that starts doing its beating. Anyway i won't alter it. Was just trying to make it works, ie be useful while also being simpler.

Well if it doesn't work it should of course be fixed.

I'm not against fixing it nor making it simpler, but I think that requires a detailed understanding of what's going wrong and how it needs to be fixed. Not as part of a patch series that really tries to accomplish something else.

The current code was tested extensively with psb and unichrome. One good test for drivers with bo-backed textures is to continously create fairly large texture images. The end result should be the swap space starting to fill up and once there is no more swap space, the OOM killer should kill your app, and kmalloc failures should be avoided. It should be tricky to get a failure from the global alloc system, but a huge amount of small buffer objects or fence objects should probably do it.

Naturally, that requires that all persistent drm objects created from user-space are registered with their correct sizes, or at least a really good size approximation. That includes things like gem flinks, that could otherwise easily be exploited to bring a system down, simply by guessing a gem name and create flinks to that name in an infinite loop.

Quoted 5 lines

What are the symptoms of the failure you're seeing with Radeon? Any suggestions on why it happens?

Thanks,
Thomas

I pushed my test case to libdrm yesterday, i basicly alloc ttm object of 1 page in a loop and expect it to fail. I modified the kernel to account 2 page for the ttm_buffer_object struct size so that the kernel area should be exhausted long before i run out of memory on a 8G config. What happen is that the oom start killing everythings except my app, even the kernel logger daemon got kill before my app ...

I think the ttm_memory accounting for kernel object is not the right way. What we want here is trying to avoid to starve kernel memory. Thing is we are uniq in the kernel afaik (i take a look at bunch of others driver to check that in net, audio, ...). in that we allocate a lot of kernel object on the behalf of userspace.

I believe a better solution would be either to put a limit on the number of objects. For instance 16 millions objects (wether fence of bo struct). Or start talking with kernel folks about a dev_alloc function that could be use to allocate memory and account for this memory against a device. Kernel would be able to decide for a policy for this. There is already a netdev_alloc_page but it's not use in any meaningfull way afaict.

Then when it comes to memory of buffer object themself, it would be a lot better if we could somehow make it accountable against user process. Like hooking into memory group (memcg). This would also allow us to directly reuse kernel infrastructure, things like watermark on when to start either swapin or reclaiming memory or simply refusing to allocate more memory.

Cheers,
Jerome

#14 lore raw

Re: [PATCH 05/13] drm/ttm: overhaul memory accounting

Quoted 95 lines · Jerome Glisse
Quoted 80 lines · Thomas Hellstrom
Quoted 50 lines · Jerome Glisse
Quoted 41 lines · Thomas Hellstrom
Quoted 39 lines · Jerome Glisse
Quoted 37 lines · Thomas Hellstrom
Quoted 21 lines · On 11/09/2011 09:22 PM, [email protected] wrote:

From: Jerome Glisse<[email protected]>

This is an overhaul of the ttm memory accounting. This tries to keep the same global behavior while removing the whole zone concept. It keeps a distrinction for dma32 so that we make sure that ttm don't starve the dma32 zone.

There is 3 threshold for memory allocation :
- max_mem is the maximum memory the whole ttm infrastructure is going to allow allocation for (exception of system process see below)
- emer_mem is the maximum memory allowed for system process, this
limit is> to max_mem
- swap_limit is the threshold at which point ttm will start to try to swap object because ttm is getting close the max_mem limit
- swap_dma32_limit is the threshold at which point ttm will start swap object to try to reduce the pressure on the dma32 zone. Note that we don't specificly target object to swap to it might very well free more memory from highmem rather than from dma32

Quoted 10 lines

Accounting is done through used_mem& used_dma32_mem, which sum give
the total amount of memory actually accounted by ttm.

Idea is that allocation will fail if (used_mem + used_dma32_mem)> max_mem and if swapping fail to make enough room.

The used_dma32_mem can be updated as a later stage, allowing to perform accounting test before allocating a whole batch of pages.

Jerome, you're removing a fair amount of functionality here, without justifying
why it could be removed.

Quoted 7 lines
Quoted 2 lines

All this code was overkill.

[1] I don't agree, and since it's well tested, thought throught and working, I see no obvious reason to alter it,
within the context of this patch series unless it's absolutely required for the functionality.

Quoted 5 lines

Well one thing i can tell is that it doesn't work on radeon, i pushed a test to libdrm and here it's the oom that starts doing its beating. Anyway i won't alter it. Was just trying to make it works, ie be useful while also being simpler.

Well if it doesn't work it should of course be fixed.

I'm not against fixing it nor making it simpler, but I think that requires a detailed understanding of what's going wrong and how it needs to be fixed. Not as part of a patch series that really tries to accomplish something else.

The current code was tested extensively with psb and unichrome. One good test for drivers with bo-backed textures is to continously create fairly large texture images. The end result should be the swap space starting to fill up and once there is no more swap space, the OOM killer should kill your app, and kmalloc failures should be avoided. It should be tricky to get a failure from the global alloc system, but a huge amount of small buffer objects or fence objects should probably do it.

Naturally, that requires that all persistent drm objects created from user-space are registered with their correct sizes, or at least a really good size approximation. That includes things like gem flinks, that could otherwise easily be exploited to bring a system down, simply by guessing a gem name and create flinks to that name in an infinite loop.

Quoted 6 lines

What are the symptoms of the failure you're seeing with Radeon? Any suggestions on why it happens?

Thanks,
Thomas

I pushed my test case to libdrm yesterday, i basicly alloc ttm object of 1 page in a loop and expect it to fail. I modified the kernel to account 2 page for the ttm_buffer_object struct size so that the kernel area should be exhausted long before i run out of memory on a 8G config. What happen is that the oom start killing everythings except my app, even the kernel logger daemon got kill before my app ...

Quoted 3 lines

I think the ttm_memory accounting for kernel object is not the right way.

....

So, yet again, TTM gets incorrectly blamed when things are not working as expected.

The TTM memory accounting is designed to avoid pinning too much memory for graphics, so that it can't be used by the rest of the system. It's working well doing exactly that.

However, it can't stop your app from wanting to store too much data. It just shuffles that data to swap. If too many apps want to store too much data, eventually the computer runs out of swap space and the OOM killer kicks in, and tries to guess what app to kill. That's not TTM's business. Nor is it DRM's business.

The only time the TTM memory accounting system blocks an allocation is if there is too much pinned memory allocated (kmalloc, vmalloc) that it can't release to swap space. It protects against kmalloc failures, but it makes no attempt to stop your app from wanting to store too much data.

/Thomas

#15 lore raw

Re: [PATCH 05/13] drm/ttm: overhaul memory accounting

Quoted 102 lines · Thomas Hellstrom
Quoted 83 lines · Jerome Glisse
Quoted 70 lines · Thomas Hellstrom
Quoted 38 lines · Jerome Glisse
Quoted 37 lines · Thomas Hellstrom
Quoted 36 lines · Jerome Glisse
Quoted 28 lines · Thomas Hellstrom
Quoted 27 lines · On 11/09/2011 09:22 PM, [email protected] wrote:

From: Jerome Glisse<[email protected]>

This is an overhaul of the ttm memory accounting. This tries to keep the same global behavior while removing the whole zone concept. It keeps a distrinction for dma32 so that we make sure that ttm don't starve the dma32 zone.

There is 3 threshold for memory allocation :
- max_mem is the maximum memory the whole ttm infrastructure is going to allow allocation for (exception of system process see below)
- emer_mem is the maximum memory allowed for system process, this
limit is> to max_mem
- swap_limit is the threshold at which point ttm will start to try to swap object because ttm is getting close the max_mem limit
- swap_dma32_limit is the threshold at which point ttm will start swap object to try to reduce the pressure on the dma32 zone. Note that we don't specificly target object to swap to it might very well free more memory from highmem rather than from dma32

Accounting is done through used_mem& used_dma32_mem, which sum give
the total amount of memory actually accounted by ttm.

Idea is that allocation will fail if (used_mem + used_dma32_mem)> max_mem and if swapping fail to make enough room.

Quoted 6 lines
Quoted 3 lines

The used_dma32_mem can be updated as a later stage, allowing to perform accounting test before allocating a whole batch of pages.

Jerome, you're removing a fair amount of functionality here, without justifying
why it could be removed.

All this code was overkill.

Quoted 8 lines
Quoted 4 lines

[1] I don't agree, and since it's well tested, thought throught and working, I see no obvious reason to alter it,
within the context of this patch series unless it's absolutely required for the functionality.

Well one thing i can tell is that it doesn't work on radeon, i pushed a test to libdrm and here it's the oom that starts doing its beating. Anyway i won't alter it. Was just trying to make it works, ie be useful while also being simpler.

Well if it doesn't work it should of course be fixed.

I'm not against fixing it nor making it simpler, but I think that requires a detailed understanding of what's going wrong and how it needs to be fixed. Not as part of a patch series that really tries to accomplish something else.

The current code was tested extensively with psb and unichrome. One good test for drivers with bo-backed textures is to continously create fairly large texture images. The end result should be the swap space starting to fill up and once there is no more swap space, the OOM killer should kill your app, and kmalloc failures should be avoided. It should be tricky to get a failure from the global alloc system, but a huge amount of small buffer objects or fence objects should probably do it.

Naturally, that requires that all persistent drm objects created from user-space are registered with their correct sizes, or at least a really good size approximation. That includes things like gem flinks, that could otherwise easily be exploited to bring a system down, simply by guessing a gem name and create flinks to that name in an infinite loop.

Quoted 5 lines

What are the symptoms of the failure you're seeing with Radeon? Any suggestions on why it happens?

Thanks,
Thomas

I pushed my test case to libdrm yesterday, i basicly alloc ttm object of 1 page in a loop and expect it to fail. I modified the kernel to account 2 page for the ttm_buffer_object struct size so that the kernel area should be exhausted long before i run out of memory on a 8G config. What happen is that the oom start killing everythings except my app, even the kernel logger daemon got kill before my app ...

Quoted 2 lines

I think the ttm_memory accounting for kernel object is not the right way.

....

So, yet again, TTM gets incorrectly blamed when things are not working as expected.

The TTM memory accounting is designed to avoid pinning too much memory for graphics, so that it can't be
used by the rest of the system. It's working well doing exactly that.

However, it can't stop your app from wanting to store too much data. It just shuffles that data to swap. If too many apps want to store too much data, eventually the computer runs out of swap space and the OOM killer kicks in, and
tries to guess what app to kill. That's not TTM's business. Nor is it DRM's business.

Quoted 6 lines

The only time the TTM memory accounting system blocks an allocation is if there is too much pinned memory allocated (kmalloc, vmalloc) that it can't release to swap space. It protects against kmalloc failures, but it makes
no attempt to stop your app from wanting to store too much data.

My test only try to allocate kernel memory, so i have 8G of ram i allocate 1 page for each object (highmem forced) and then for the accounting i account 2 page per each kernel kmalloc of bo struct. So ttm memory should stop allowing any more allocation as soon as i allocated for 400M of kernel memory. Which should happen with only 200M of highmem allocate so all well below the limit.

I also setup 4G of swap so in the end the oom shouldn't kick in before we exhauste our kernel memory.

I think here the issue is that the kernel memory pool got decrease wrongly when swaping object as the ttm buffer struct is not swaped and still there.

I am chasing a bug in swapout + dma page alloc first, i will get back to why ttm memory accouting fails after that.

Cheers,
Jerome