linux-mm.kvack.org archive mirror
 help / color / mirror / Atom feed
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 2/5] drm/gem: track mm struct of allocating process in gem object
Date: Fri,  9 Sep 2022 13:16:37 +0200	[thread overview]
Message-ID: <20220909111640.3789791-3-l.stach@pengutronix.de> (raw)
In-Reply-To: <20220909111640.3789791-1-l.stach@pengutronix.de>

This keeps around a weak reference to the struct mm of the process
allocating the GEM object. This allows us to charge/uncharge the
process with the allocated backing store memory, even if this is
happening from another context.

Signed-off-by: Lucas Stach <l.stach@pengutronix.de>
---
 drivers/gpu/drm/drm_gem.c |  5 +++++
 include/drm/drm_gem.h     | 12 ++++++++++++
 2 files changed, 17 insertions(+)

diff --git a/drivers/gpu/drm/drm_gem.c b/drivers/gpu/drm/drm_gem.c
index 86d670c71286..b882f935cd4b 100644
--- a/drivers/gpu/drm/drm_gem.c
+++ b/drivers/gpu/drm/drm_gem.c
@@ -36,6 +36,7 @@
 #include <linux/pagemap.h>
 #include <linux/pagevec.h>
 #include <linux/shmem_fs.h>
+#include <linux/sched/mm.h>
 #include <linux/slab.h>
 #include <linux/string_helpers.h>
 #include <linux/types.h>
@@ -157,6 +158,9 @@ void drm_gem_private_object_init(struct drm_device *dev,
 	obj->dev = dev;
 	obj->filp = NULL;
 
+	mmgrab(current->mm);
+	obj->mm = current->mm;
+
 	kref_init(&obj->refcount);
 	obj->handle_count = 0;
 	obj->size = size;
@@ -949,6 +953,7 @@ drm_gem_object_release(struct drm_gem_object *obj)
 	if (obj->filp)
 		fput(obj->filp);
 
+	mmdrop(obj->mm);
 	dma_resv_fini(&obj->_resv);
 	drm_gem_free_mmap_offset(obj);
 }
diff --git a/include/drm/drm_gem.h b/include/drm/drm_gem.h
index 87cffc9efa85..d021a083c282 100644
--- a/include/drm/drm_gem.h
+++ b/include/drm/drm_gem.h
@@ -234,6 +234,18 @@ struct drm_gem_object {
 	 */
 	struct drm_vma_offset_node vma_node;
 
+	/**
+	 * @mm:
+	 *
+	 * mm struct of the process creating the object. Used to account the
+	 * allocated backing store memory.
+	 *
+	 * Note that this is a weak reference created by mmgrab(), so any
+	 * manipulation needs to make sure the address space is still around by
+	 * calling mmget_not_zero().
+	 */
+	struct mm_struct *mm;
+
 	/**
 	 * @size:
 	 *
-- 
2.30.2



  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 ` Lucas Stach [this message]
2022-09-09 11:16 ` [RFC PATCH 3/5] drm/gem: add functions to account GEM object memory usage Lucas Stach
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-3-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