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 1/5] mm: add MM_DRIVERPAGES
Date: Fri,  9 Sep 2022 13:16:36 +0200	[thread overview]
Message-ID: <20220909111640.3789791-2-l.stach@pengutronix.de> (raw)
In-Reply-To: <20220909111640.3789791-1-l.stach@pengutronix.de>

This adds a mm counter for pages allocated by a driver on behalf of
a userspace task.

Especially with DRM drivers there can be large amounts of pages that
are never mapped into userspace and thus are not tracked by the usual
ANONPAGES mmap accounting, as those pages are only ever touched by the
device. They can make up a significant portion of the tasks resident
memory size, but are currently not visible in any of the memory
statistics visible to userspace and the OOM handling.

Add the counter to allow tracking such memory, which allows to make
more sensible decisions in the OOM handling as well as allowing
userspace some better insight into the real system memory usage.

Signed-off-by: Lucas Stach <l.stach@pengutronix.de>
---
 fs/proc/task_mmu.c            | 6 ++++--
 include/linux/mm.h            | 3 ++-
 include/linux/mm_types_task.h | 1 +
 kernel/fork.c                 | 1 +
 4 files changed, 8 insertions(+), 3 deletions(-)

diff --git a/fs/proc/task_mmu.c b/fs/proc/task_mmu.c
index a3398d0f1927..80b095a233bf 100644
--- a/fs/proc/task_mmu.c
+++ b/fs/proc/task_mmu.c
@@ -33,7 +33,8 @@ void task_mem(struct seq_file *m, struct mm_struct *mm)
 	unsigned long text, lib, swap, anon, file, shmem;
 	unsigned long hiwater_vm, total_vm, hiwater_rss, total_rss;
 
-	anon = get_mm_counter(mm, MM_ANONPAGES);
+	anon = get_mm_counter(mm, MM_ANONPAGES) +
+	       get_mm_counter(mm, MM_DRIVERPAGES);
 	file = get_mm_counter(mm, MM_FILEPAGES);
 	shmem = get_mm_counter(mm, MM_SHMEMPAGES);
 
@@ -94,7 +95,8 @@ unsigned long task_statm(struct mm_struct *mm,
 	*text = (PAGE_ALIGN(mm->end_code) - (mm->start_code & PAGE_MASK))
 								>> PAGE_SHIFT;
 	*data = mm->data_vm + mm->stack_vm;
-	*resident = *shared + get_mm_counter(mm, MM_ANONPAGES);
+	*resident = *shared + get_mm_counter(mm, MM_ANONPAGES) +
+		    get_mm_counter(mm, MM_DRIVERPAGES);
 	return mm->total_vm;
 }
 
diff --git a/include/linux/mm.h b/include/linux/mm.h
index 3bedc449c14d..2cc014d1ea27 100644
--- a/include/linux/mm.h
+++ b/include/linux/mm.h
@@ -2003,7 +2003,8 @@ static inline unsigned long get_mm_rss(struct mm_struct *mm)
 {
 	return get_mm_counter(mm, MM_FILEPAGES) +
 		get_mm_counter(mm, MM_ANONPAGES) +
-		get_mm_counter(mm, MM_SHMEMPAGES);
+		get_mm_counter(mm, MM_SHMEMPAGES) +
+		get_mm_counter(mm, MM_DRIVERPAGES);
 }
 
 static inline unsigned long get_mm_hiwater_rss(struct mm_struct *mm)
diff --git a/include/linux/mm_types_task.h b/include/linux/mm_types_task.h
index c1bc6731125c..420d88e79906 100644
--- a/include/linux/mm_types_task.h
+++ b/include/linux/mm_types_task.h
@@ -45,6 +45,7 @@ enum {
 	MM_ANONPAGES,	/* Resident anonymous pages */
 	MM_SWAPENTS,	/* Anonymous swap entries */
 	MM_SHMEMPAGES,	/* Resident shared memory pages */
+	MM_DRIVERPAGES,	/* pages allocated by a driver on behalf of a task */
 	NR_MM_COUNTERS
 };
 
diff --git a/kernel/fork.c b/kernel/fork.c
index 90c85b17bf69..74a07a2288ba 100644
--- a/kernel/fork.c
+++ b/kernel/fork.c
@@ -135,6 +135,7 @@ static const char * const resident_page_types[] = {
 	NAMED_ARRAY_INDEX(MM_ANONPAGES),
 	NAMED_ARRAY_INDEX(MM_SWAPENTS),
 	NAMED_ARRAY_INDEX(MM_SHMEMPAGES),
+	NAMED_ARRAY_INDEX(MM_DRIVERPAGES),
 };
 
 DEFINE_PER_CPU(unsigned long, process_counts) = 0;
-- 
2.30.2



  reply	other threads:[~2022-09-09 11:32 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 ` Lucas Stach [this message]
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 ` [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-2-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