File 0003-video-tegra-nvmap-Avoid-double-updation-of-RSS-count.patch of Package nvidia-jetson-36_4_0-jp6_1
From b27a1a79eec1176d50c66646de2e49e6e770408c Mon Sep 17 00:00:00 2001
From: Ketan Patil <ketanp@nvidia.com>
Date: Tue, 2 Sep 2025 08:53:30 +0000
Subject: [PATCH 3/3] video: tegra: nvmap: Avoid double updation of RSS counter
The RSS counter is updated during buffer allocation as well as mmap,
which is leading to double updation. Fix this by decrementing the RSS
counter during page fault while increment it back during unmap flow.
Bug 5222690
Change-Id: I77972185f20d9d710571cc07ae1c5188060bfa1f
Signed-off-by: Ketan Patil <ketanp@nvidia.com>
Reviewed-on: https://git-master.nvidia.com/r/c/linux-nv-oot/+/3442670
(cherry picked from commit 1c59063ce724bd7e10085e707beb56aa494361c6)
Reviewed-on: https://git-master.nvidia.com/r/c/linux-nv-oot/+/3453103
Reviewed-by: Amulya Yarlagadda <ayarlagadda@nvidia.com>
GVS: buildbot_gerritrpt <buildbot_gerritrpt@nvidia.com>
Tested-by: Amulya Yarlagadda <ayarlagadda@nvidia.com>
---
drivers/video/tegra/nvmap/nvmap_dmabuf.c | 3 ++-
drivers/video/tegra/nvmap/nvmap_fault.c | 18 +++++++++++++++++-
drivers/video/tegra/nvmap/nvmap_priv.h | 3 +++
3 files changed, 22 insertions(+), 2 deletions(-)
diff --git a/drivers/video/tegra/nvmap/nvmap_dmabuf.c b/drivers/video/tegra/nvmap/nvmap_dmabuf.c
index 9be5d96f..dd0db106 100644
--- a/drivers/video/tegra/nvmap/nvmap_dmabuf.c
+++ b/drivers/video/tegra/nvmap/nvmap_dmabuf.c
@@ -1,6 +1,6 @@
// SPDX-License-Identifier: GPL-2.0-only
/*
- * Copyright (c) 2012-2023, NVIDIA CORPORATION. All rights reserved.
+ * Copyright (c) 2012-2025, NVIDIA CORPORATION. All rights reserved.
*
* dma_buf exporter for nvmap
*/
@@ -450,6 +450,7 @@ int __nvmap_map(struct nvmap_handle *h, struct vm_area_struct *vma)
nvmap_handle_put(h);
return -ENOMEM;
}
+ mutex_init(&priv->vma_lock);
priv->handle = h;
#if defined(NV_VM_AREA_STRUCT_HAS_CONST_VM_FLAGS) /* Linux v6.3 */
diff --git a/drivers/video/tegra/nvmap/nvmap_fault.c b/drivers/video/tegra/nvmap/nvmap_fault.c
index 4e56a1dd..1ab1afeb 100644
--- a/drivers/video/tegra/nvmap/nvmap_fault.c
+++ b/drivers/video/tegra/nvmap/nvmap_fault.c
@@ -1,6 +1,6 @@
// SPDX-License-Identifier: GPL-2.0-only
/*
- * Copyright (c) 2011-2023, NVIDIA CORPORATION. All rights reserved.
+ * Copyright (c) 2011-2025, NVIDIA CORPORATION. All rights reserved.
*/
#define pr_fmt(fmt) "%s: " fmt, __func__
@@ -147,6 +147,14 @@ static void nvmap_vma_close(struct vm_area_struct *vma)
BUG_ON(!vma_found);
nvmap_umaps_dec(h);
+ mutex_lock(&priv->vma_lock);
+ if (priv->mm != NULL && h->anon_count != 0) {
+ nvmap_add_mm_counter(priv->mm, MM_ANONPAGES, priv->map_rss_count);
+ priv->map_rss_count = 0;
+ priv->mm = NULL;
+ }
+ mutex_unlock(&priv->vma_lock);
+
if (__atomic_add_unless(&priv->count, -1, 0) == 1) {
if (h->heap_pgalloc) {
for (i = 0; i < nr_page; i++) {
@@ -233,6 +241,14 @@ static int nvmap_vma_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
return VM_FAULT_SIGSEGV;
}
+ mutex_lock(&priv->vma_lock);
+ if (priv->handle->anon_count != 0 && current->mm != NULL) {
+ nvmap_add_mm_counter(current->mm, MM_ANONPAGES, -1);
+ priv->map_rss_count++;
+ priv->mm = current->mm;
+ }
+ mutex_unlock(&priv->vma_lock);
+
if (!nvmap_handle_track_dirty(priv->handle))
goto finish;
mutex_lock(&priv->handle->lock);
diff --git a/drivers/video/tegra/nvmap/nvmap_priv.h b/drivers/video/tegra/nvmap/nvmap_priv.h
index 3b505936..66c1c9f2 100644
--- a/drivers/video/tegra/nvmap/nvmap_priv.h
+++ b/drivers/video/tegra/nvmap/nvmap_priv.h
@@ -380,6 +380,9 @@ struct nvmap_vma_priv {
struct nvmap_handle *handle;
size_t offs;
atomic_t count; /* number of processes cloning the VMA */
+ u64 map_rss_count;
+ struct mm_struct *mm;
+ struct mutex vma_lock;
};
struct nvmap_device {
--
2.51.0