dri-devel.lists.freedesktop.org archive mirror
 help / color / mirror / Atom feed
From: "Paneer Selvam, Arunpravin" <Arunpravin.PaneerSelvam@amd.com>
To: Matthew Auld <matthew.auld@intel.com>,
	"dri-devel@lists.freedesktop.org"
	<dri-devel@lists.freedesktop.org>,
	"intel-gfx@lists.freedesktop.org"
	<intel-gfx@lists.freedesktop.org>,
	"amd-gfx@lists.freedesktop.org" <amd-gfx@lists.freedesktop.org>
Cc: "Paneer Selvam, Arunpravin" <Arunpravin.PaneerSelvam@amd.com>,
	"tzimmermann@suse.de" <tzimmermann@suse.de>,
	"Deucher, Alexander" <Alexander.Deucher@amd.com>,
	"Koenig, Christian" <Christian.Koenig@amd.com>
Subject: RE: [PATCH v4 4/6] drm: implement a method to free unused pages
Date: Thu, 9 Dec 2021 15:52:16 +0000	[thread overview]
Message-ID: <MN2PR12MB4342E888EDC333C3089834FBE4709@MN2PR12MB4342.namprd12.prod.outlook.com> (raw)
In-Reply-To: <20211201163938.133226-4-Arunpravin.PaneerSelvam@amd.com>

[Public]

Hi Matthew,

Ping?

Regards,
Arun
-----Original Message-----
From: Paneer Selvam, Arunpravin <Arunpravin.PaneerSelvam@amd.com> 
Sent: Wednesday, December 1, 2021 10:10 PM
To: dri-devel@lists.freedesktop.org; intel-gfx@lists.freedesktop.org; amd-gfx@lists.freedesktop.org
Cc: matthew.auld@intel.com; daniel@ffwll.ch; Koenig, Christian <Christian.Koenig@amd.com>; Deucher, Alexander <Alexander.Deucher@amd.com>; tzimmermann@suse.de; jani.nikula@linux.intel.com; Paneer Selvam, Arunpravin <Arunpravin.PaneerSelvam@amd.com>
Subject: [PATCH v4 4/6] drm: implement a method to free unused pages

On contiguous allocation, we round up the size to the *next* power of 2, implement a function to free the unused pages after the newly allocate block.

v2(Matthew Auld):
  - replace function name 'drm_buddy_free_unused_pages' with
    drm_buddy_block_trim
  - replace input argument name 'actual_size' with 'new_size'
  - add more validation checks for input arguments
  - add overlaps check to avoid needless searching and splitting
  - merged the below patch to see the feature in action
    - add free unused pages support to i915 driver
  - lock drm_buddy_block_trim() function as it calls mark_free/mark_split
    are all globally visible

v3:
  - remove drm_buddy_block_trim() error handling and
    print a warn message if it fails

Signed-off-by: Arunpravin <Arunpravin.PaneerSelvam@amd.com>
---
 drivers/gpu/drm/drm_buddy.c                   | 72 ++++++++++++++++++-
 drivers/gpu/drm/i915/i915_ttm_buddy_manager.c | 10 +++
 include/drm/drm_buddy.h                       |  4 ++
 3 files changed, 83 insertions(+), 3 deletions(-)

diff --git a/drivers/gpu/drm/drm_buddy.c b/drivers/gpu/drm/drm_buddy.c index eddc1eeda02e..707efc82216d 100644
--- a/drivers/gpu/drm/drm_buddy.c
+++ b/drivers/gpu/drm/drm_buddy.c
@@ -434,7 +434,8 @@ alloc_from_freelist(struct drm_buddy_mm *mm,  static int __alloc_range(struct drm_buddy_mm *mm,
 			 struct list_head *dfs,
 			 u64 start, u64 size,
-			 struct list_head *blocks)
+			 struct list_head *blocks,
+			 bool trim_path)
 {
 	struct drm_buddy_block *block;
 	struct drm_buddy_block *buddy;
@@ -480,8 +481,20 @@ static int __alloc_range(struct drm_buddy_mm *mm,
 
 		if (!drm_buddy_block_is_split(block)) {
 			err = split_block(mm, block);
-			if (unlikely(err))
+			if (unlikely(err)) {
+				if (trim_path)
+					/*
+					 * Here in case of trim, we return and dont goto
+					 * split failure path as it removes from the
+					 * original list and potentially also freeing
+					 * the block. so we could leave as it is,
+					 * worse case we get some internal fragmentation
+					 * and leave the decision to the user
+					 */
+					return err;
+
 				goto err_undo;
+			}
 		}
 
 		list_add(&block->right->tmp_link, dfs); @@ -535,8 +548,61 @@ static int __drm_buddy_alloc_range(struct drm_buddy_mm *mm,
 	for (i = 0; i < mm->n_roots; ++i)
 		list_add_tail(&mm->roots[i]->tmp_link, &dfs);
 
-	return __alloc_range(mm, &dfs, start, size, blocks);
+	return __alloc_range(mm, &dfs, start, size, blocks, 0); }
+
+/**
+ * drm_buddy_block_trim - free unused pages
+ *
+ * @mm: DRM buddy manager
+ * @new_size: original size requested
+ * @blocks: output list head to add allocated blocks
+ *
+ * For contiguous allocation, we round up the size to the nearest
+ * power of two value, drivers consume *actual* size, so remaining
+ * portions are unused and it can be freed.
+ *
+ * Returns:
+ * 0 on success, error code on failure.
+ */
+int drm_buddy_block_trim(struct drm_buddy_mm *mm,
+			 u64 new_size,
+			 struct list_head *blocks)
+{
+	struct drm_buddy_block *block;
+	u64 new_start;
+	LIST_HEAD(dfs);
+
+	if (!list_is_singular(blocks))
+		return -EINVAL;
+
+	block = list_first_entry(blocks,
+				 struct drm_buddy_block,
+				 link);
+
+	if (!drm_buddy_block_is_allocated(block))
+		return -EINVAL;
+
+	if (new_size > drm_buddy_block_size(mm, block))
+		return -EINVAL;
+
+	if (!new_size && !IS_ALIGNED(new_size, mm->chunk_size))
+		return -EINVAL;
+
+	if (new_size == drm_buddy_block_size(mm, block))
+		return 0;
+
+	list_del(&block->link);
+
+	new_start = drm_buddy_block_offset(block);
+
+	mark_free(mm, block);
+
+	list_add(&block->tmp_link, &dfs);
+
+	return __alloc_range(mm, &dfs, new_start, new_size, blocks, 1);
 }
+EXPORT_SYMBOL(drm_buddy_block_trim);
 
 /**
  * drm_buddy_alloc - allocate power-of-two blocks diff --git a/drivers/gpu/drm/i915/i915_ttm_buddy_manager.c b/drivers/gpu/drm/i915/i915_ttm_buddy_manager.c
index 7c58efb60dba..c5831c27fe82 100644
--- a/drivers/gpu/drm/i915/i915_ttm_buddy_manager.c
+++ b/drivers/gpu/drm/i915/i915_ttm_buddy_manager.c
@@ -97,6 +97,16 @@ static int i915_ttm_buddy_man_alloc(struct ttm_resource_manager *man,
 	if (unlikely(err))
 		goto err_free_blocks;
 
+	if (place->flags & TTM_PL_FLAG_CONTIGUOUS) {
+		mutex_lock(&bman->lock);
+		err = drm_buddy_block_trim(mm,
+				(u64)n_pages << PAGE_SHIFT,
+				&bman_res->blocks);
+		mutex_unlock(&bman->lock);
+		pr_warn("drm_buddy_block_trim failed returing %d for ttm_buffer_object(%p)\n",
+			err, bo);
+	}
+
 	*res = &bman_res->base;
 	return 0;
 
diff --git a/include/drm/drm_buddy.h b/include/drm/drm_buddy.h index 316ac0d25f08..90906d9dbbf0 100644
--- a/include/drm/drm_buddy.h
+++ b/include/drm/drm_buddy.h
@@ -146,6 +146,10 @@ int drm_buddy_alloc(struct drm_buddy_mm *mm,
 		    struct list_head *blocks,
 		    unsigned long flags);
 
+int drm_buddy_block_trim(struct drm_buddy_mm *mm,
+			 u64 new_size,
+			 struct list_head *blocks);
+
 void drm_buddy_free(struct drm_buddy_mm *mm, struct drm_buddy_block *block);
 
 void drm_buddy_free_list(struct drm_buddy_mm *mm, struct list_head *objects);
--
2.25.1

  reply	other threads:[~2021-12-09 17:03 UTC|newest]

Thread overview: 20+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-12-01 16:39 [PATCH v4 1/6] drm: move the buddy allocator from i915 into common drm Arunpravin
2021-12-01 16:39 ` [PATCH v4 2/6] drm: improve drm_buddy_alloc function Arunpravin
2021-12-09 15:47   ` Paneer Selvam, Arunpravin
2021-12-13 18:59     ` Matthew Auld
2021-12-15 20:46       ` Arunpravin
2021-12-16 10:55         ` Matthew Auld
2021-12-16 11:35   ` Thomas Zimmermann
2021-12-26 20:59     ` Arunpravin
2022-01-03  7:41       ` Christian König
2022-01-06 12:01         ` Thomas Zimmermann
2021-12-01 16:39 ` [PATCH v4 3/6] drm: implement top-down allocation method Arunpravin
2021-12-01 16:39 ` [PATCH v4 4/6] drm: implement a method to free unused pages Arunpravin
2021-12-09 15:52   ` Paneer Selvam, Arunpravin [this message]
2021-12-13 18:40   ` Matthew Auld
2021-12-15 20:56     ` Arunpravin
2021-12-01 16:39 ` [PATCH v4 5/6] drm/amdgpu: move vram inline functions into a header Arunpravin
2021-12-01 16:39 ` [PATCH v4 6/6] drm/amdgpu: add drm buddy support to amdgpu Arunpravin
2021-12-02  0:56 ` [PATCH v4 1/6] drm: move the buddy allocator from i915 into common drm kernel test robot
2021-12-02  4:49 ` kernel test robot
2021-12-02  8:03 ` Christian König

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=MN2PR12MB4342E888EDC333C3089834FBE4709@MN2PR12MB4342.namprd12.prod.outlook.com \
    --to=arunpravin.paneerselvam@amd.com \
    --cc=Alexander.Deucher@amd.com \
    --cc=Christian.Koenig@amd.com \
    --cc=amd-gfx@lists.freedesktop.org \
    --cc=dri-devel@lists.freedesktop.org \
    --cc=intel-gfx@lists.freedesktop.org \
    --cc=matthew.auld@intel.com \
    --cc=tzimmermann@suse.de \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).