From: Lucas Stach <l.stach@pengutronix.de>
To: linux-mm@kvack.org, dri-devel@lists.freedesktop.org
Cc: "Daniel Vetter" <daniel@ffwll.ch>,
"David Airlie" <airlied@linux.ie>,
"Andrew Morton" <akpm@linux-foundation.org>,
"Michal Hocko" <mhocko@suse.com>,
"Christian König" <ckoenig.leichtzumerken@gmail.com>,
linux-fsdevel@vger.kernel.org, kernel@pengutronix.de
Subject: [RFC PATCH 3/5] drm/gem: add functions to account GEM object memory usage
Date: Fri, 9 Sep 2022 13:16:38 +0200 [thread overview]
Message-ID: <20220909111640.3789791-4-l.stach@pengutronix.de> (raw)
In-Reply-To: <20220909111640.3789791-1-l.stach@pengutronix.de>
This adds some functions which driver can call to make the MM aware
of the resident memory used by the GEM object. As drivers will have
different points where memory is made resident/pinned into system
memory, this just adds the helper functions and drivers need to make
sure to call them at the right points.
Signed-off-by: Lucas Stach <l.stach@pengutronix.de>
---
drivers/gpu/drm/drm_gem.c | 37 +++++++++++++++++++++++++++++++++++++
include/drm/drm_gem.h | 3 +++
2 files changed, 40 insertions(+)
diff --git a/drivers/gpu/drm/drm_gem.c b/drivers/gpu/drm/drm_gem.c
index b882f935cd4b..efccd0a1dde7 100644
--- a/drivers/gpu/drm/drm_gem.c
+++ b/drivers/gpu/drm/drm_gem.c
@@ -1279,3 +1279,40 @@ drm_gem_unlock_reservations(struct drm_gem_object **objs, int count,
ww_acquire_fini(acquire_ctx);
}
EXPORT_SYMBOL(drm_gem_unlock_reservations);
+
+/**
+ * drm_gem_add_resident - Account memory used by GEM object to the
+ * task which called drm_gem_object_init(). Call when the pages are
+ * made resident in system memory, i.e. pinned for GPU usage.
+ *
+ * @obj: GEM buffer object
+ */
+void drm_gem_add_resident(struct drm_gem_object *obj)
+{
+ if (!mmget_not_zero(obj->mm))
+ return;
+
+ add_mm_counter(obj->mm, MM_DRIVERPAGES, obj->size / PAGE_SIZE);
+
+ mmput(obj->mm);
+}
+EXPORT_SYMBOL(drm_gem_add_resident)
+
+/**
+ * drm_gem_dec_resident - Remove memory used by GEM object accounted
+ * to the task which called drm_gem_object_init(). Call this when the
+ * pages backing the GEM object are no longer resident in system memory,
+ * i.e. when freeing or unpinning the pages.
+ *
+ * @obj: GEM buffer object
+ */
+void drm_gem_dec_resident(struct drm_gem_object *obj)
+{
+ if (!mmget_not_zero(obj->mm))
+ return;
+
+ add_mm_counter(obj->mm, MM_DRIVERPAGES, -(obj->size / PAGE_SIZE));
+
+ mmput(obj->mm);
+}
+EXPORT_SYMBOL(drm_gem_dec_resident)
diff --git a/include/drm/drm_gem.h b/include/drm/drm_gem.h
index d021a083c282..5951963a2f1a 100644
--- a/include/drm/drm_gem.h
+++ b/include/drm/drm_gem.h
@@ -374,6 +374,9 @@ int drm_gem_mmap_obj(struct drm_gem_object *obj, unsigned long obj_size,
struct vm_area_struct *vma);
int drm_gem_mmap(struct file *filp, struct vm_area_struct *vma);
+void drm_gem_add_resident(struct drm_gem_object *obj);
+void drm_gem_dec_resident(struct drm_gem_object *obj);
+
/**
* drm_gem_object_get - acquire a GEM buffer object reference
* @obj: GEM buffer object
--
2.30.2
next prev parent reply other threads:[~2022-09-09 11:16 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-09-09 11:16 [RFC PATCH 0/5] GEM buffer memory tracking Lucas Stach
2022-09-09 11:16 ` [RFC PATCH 1/5] mm: add MM_DRIVERPAGES Lucas Stach
2022-09-09 11:16 ` [RFC PATCH 2/5] drm/gem: track mm struct of allocating process in gem object Lucas Stach
2022-09-09 11:16 ` Lucas Stach [this message]
2022-09-09 11:16 ` [RFC PATCH 4/5] drm/cma-helper: account memory used by CMA GEM objects Lucas Stach
2022-09-09 11:16 ` [RFC PATCH 5/5] drm/etnaviv: account memory used by GEM buffers Lucas Stach
2022-09-09 11:32 ` [RFC PATCH 0/5] GEM buffer memory tracking 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=20220909111640.3789791-4-l.stach@pengutronix.de \
--to=l.stach@pengutronix.de \
--cc=airlied@linux.ie \
--cc=akpm@linux-foundation.org \
--cc=ckoenig.leichtzumerken@gmail.com \
--cc=daniel@ffwll.ch \
--cc=dri-devel@lists.freedesktop.org \
--cc=kernel@pengutronix.de \
--cc=linux-fsdevel@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=mhocko@suse.com \
/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