Re: [Intel-gfx] [PATCH 07/20] drm/i915/buddy: track available visible size

2022-01-31 Thread Thomas Hellström



On 1/26/22 16:21, Matthew Auld wrote:

Track the total amount of available visible memory, and also track
per-resource the amount of used visible memory. For now this is useful
for our debug output, and deciding if it is even worth calling into the
buddy allocator. In the future tracking the per-resource visible usage
will be useful for when deciding if we should attempt to evict certain
buffers.

Signed-off-by: Matthew Auld 
Cc: Thomas Hellström 
---
  drivers/gpu/drm/i915/i915_ttm_buddy_manager.c | 55 ++-
  drivers/gpu/drm/i915/i915_ttm_buddy_manager.h |  8 ++-
  drivers/gpu/drm/i915/intel_region_ttm.c   |  1 +
  3 files changed, 62 insertions(+), 2 deletions(-)

diff --git a/drivers/gpu/drm/i915/i915_ttm_buddy_manager.c 
b/drivers/gpu/drm/i915/i915_ttm_buddy_manager.c
index 53eb100688a6..6e5842155898 100644
--- a/drivers/gpu/drm/i915/i915_ttm_buddy_manager.c
+++ b/drivers/gpu/drm/i915/i915_ttm_buddy_manager.c
@@ -19,6 +19,8 @@ struct i915_ttm_buddy_manager {
struct drm_buddy mm;
struct list_head reserved;
struct mutex lock;
+   unsigned long visible_size;
+   unsigned long visible_avail;
u64 default_page_size;
  };
  
@@ -87,6 +89,13 @@ static int i915_ttm_buddy_man_alloc(struct ttm_resource_manager *man,

n_pages = size >> ilog2(mm->chunk_size);
  
  	mutex_lock(>lock);

+   if (place->lpfn && place->lpfn <= bman->visible_size &&
+   n_pages > bman->visible_avail) {
+   mutex_unlock(>lock);
+   err = -ENOSPC;
+   goto err_free_res;
+   }
+
err = drm_buddy_alloc_blocks(mm, (u64)place->fpfn << PAGE_SHIFT,
(u64)lpfn << PAGE_SHIFT,
(u64)n_pages << PAGE_SHIFT,
@@ -107,6 +116,30 @@ static int i915_ttm_buddy_man_alloc(struct 
ttm_resource_manager *man,
mutex_unlock(>lock);
}
  
+	if (place->lpfn && place->lpfn <= bman->visible_size) {

+   bman_res->used_visible_size = bman_res->base.num_pages;
+   } else {
+   struct drm_buddy_block *block;
+
+   list_for_each_entry(block, _res->blocks, link) {
+   unsigned long start =
+   drm_buddy_block_offset(block) >> PAGE_SHIFT;
+   unsigned long end = start +
+   (drm_buddy_block_size(mm, block) >> PAGE_SHIFT);
+


Move this inside the if statement below? Or perhaps the compiler is 
smart enough to figure that out.




+   if (start < bman->visible_size) {
+   bman_res->used_visible_size +=
+   min(end, bman->visible_size) - start;
+   }
+   }
+   }


Reviewed-by: Thomas Hellstrom 




[Intel-gfx] [PATCH 07/20] drm/i915/buddy: track available visible size

2022-01-26 Thread Matthew Auld
Track the total amount of available visible memory, and also track
per-resource the amount of used visible memory. For now this is useful
for our debug output, and deciding if it is even worth calling into the
buddy allocator. In the future tracking the per-resource visible usage
will be useful for when deciding if we should attempt to evict certain
buffers.

Signed-off-by: Matthew Auld 
Cc: Thomas Hellström 
---
 drivers/gpu/drm/i915/i915_ttm_buddy_manager.c | 55 ++-
 drivers/gpu/drm/i915/i915_ttm_buddy_manager.h |  8 ++-
 drivers/gpu/drm/i915/intel_region_ttm.c   |  1 +
 3 files changed, 62 insertions(+), 2 deletions(-)

diff --git a/drivers/gpu/drm/i915/i915_ttm_buddy_manager.c 
b/drivers/gpu/drm/i915/i915_ttm_buddy_manager.c
index 53eb100688a6..6e5842155898 100644
--- a/drivers/gpu/drm/i915/i915_ttm_buddy_manager.c
+++ b/drivers/gpu/drm/i915/i915_ttm_buddy_manager.c
@@ -19,6 +19,8 @@ struct i915_ttm_buddy_manager {
struct drm_buddy mm;
struct list_head reserved;
struct mutex lock;
+   unsigned long visible_size;
+   unsigned long visible_avail;
u64 default_page_size;
 };
 
@@ -87,6 +89,13 @@ static int i915_ttm_buddy_man_alloc(struct 
ttm_resource_manager *man,
n_pages = size >> ilog2(mm->chunk_size);
 
mutex_lock(>lock);
+   if (place->lpfn && place->lpfn <= bman->visible_size &&
+   n_pages > bman->visible_avail) {
+   mutex_unlock(>lock);
+   err = -ENOSPC;
+   goto err_free_res;
+   }
+
err = drm_buddy_alloc_blocks(mm, (u64)place->fpfn << PAGE_SHIFT,
(u64)lpfn << PAGE_SHIFT,
(u64)n_pages << PAGE_SHIFT,
@@ -107,6 +116,30 @@ static int i915_ttm_buddy_man_alloc(struct 
ttm_resource_manager *man,
mutex_unlock(>lock);
}
 
+   if (place->lpfn && place->lpfn <= bman->visible_size) {
+   bman_res->used_visible_size = bman_res->base.num_pages;
+   } else {
+   struct drm_buddy_block *block;
+
+   list_for_each_entry(block, _res->blocks, link) {
+   unsigned long start =
+   drm_buddy_block_offset(block) >> PAGE_SHIFT;
+   unsigned long end = start +
+   (drm_buddy_block_size(mm, block) >> PAGE_SHIFT);
+
+   if (start < bman->visible_size) {
+   bman_res->used_visible_size +=
+   min(end, bman->visible_size) - start;
+   }
+   }
+   }
+
+   if (bman_res->used_visible_size) {
+   mutex_lock(>lock);
+   bman->visible_avail -= bman_res->used_visible_size;
+   mutex_unlock(>lock);
+   }
+
*res = _res->base;
return 0;
 
@@ -127,6 +160,7 @@ static void i915_ttm_buddy_man_free(struct 
ttm_resource_manager *man,
 
mutex_lock(>lock);
drm_buddy_free_list(>mm, _res->blocks);
+   bman->visible_avail += bman_res->used_visible_size;
mutex_unlock(>lock);
 
kfree(bman_res);
@@ -141,6 +175,10 @@ static void i915_ttm_buddy_man_debug(struct 
ttm_resource_manager *man,
mutex_lock(>lock);
drm_printf(printer, "default_page_size: %lluKiB\n",
   bman->default_page_size >> 10);
+   drm_printf(printer, "visible_avail: %luMiB\n",
+  bman->visible_avail << PAGE_SHIFT >> 20);
+   drm_printf(printer, "visible_size: %luMiB\n",
+  bman->visible_size << PAGE_SHIFT >> 20);
 
drm_buddy_print(>mm, printer);
 
@@ -162,6 +200,7 @@ static const struct ttm_resource_manager_func 
i915_ttm_buddy_manager_func = {
  * @type: Memory type we want to manage
  * @use_tt: Set use_tt for the manager
  * @size: The size in bytes to manage
+ * @visible_size: The CPU visible size in bytes to manage
  * @default_page_size: The default minimum page size in bytes for allocations,
  * this must be at least as large as @chunk_size, and can be overridden by
  * setting the BO page_alignment, to be larger or smaller as needed.
@@ -185,7 +224,7 @@ static const struct ttm_resource_manager_func 
i915_ttm_buddy_manager_func = {
  */
 int i915_ttm_buddy_man_init(struct ttm_device *bdev,
unsigned int type, bool use_tt,
-   u64 size, u64 default_page_size,
+   u64 size, u64 visible_size, u64 default_page_size,
u64 chunk_size)
 {
struct ttm_resource_manager *man;
@@ -204,6 +243,8 @@ int i915_ttm_buddy_man_init(struct ttm_device *bdev,
INIT_LIST_HEAD(>reserved);
GEM_BUG_ON(default_page_size < chunk_size);
bman->default_page_size = default_page_size;
+   bman->visible_size = visible_size >> PAGE_SHIFT;
+   bman->visible_avail = bman->visible_size;
 
man = >manager;