File 0002-video-tegra-nvmap-Remove-use-of-add_mm_counter.patch of Package nvidia-jetson-36_4_0-jp6_1

From e488812038ee5641c9a02a89d3e1296a7897c2e4 Mon Sep 17 00:00:00 2001
From: Ketan Patil <ketanp@nvidia.com>
Date: Fri, 5 Sep 2025 06:15:02 +0000
Subject: [PATCH 2/3] video: tegra: nvmap: Remove use of add_mm_counter

add_mm_counter is not an exported function, so instead use
atomic_long_add_return/percpu_counter_add to directly modify RSS stat
counters.

Bug 5222690

Change-Id: I51a68d932aeb04f96e51a4a3c286ee5c8efc789a
Signed-off-by: Ketan Patil <ketanp@nvidia.com>
Reviewed-on: https://git-master.nvidia.com/r/c/linux-nv-oot/+/3446982
(cherry picked from commit 8e1a6b2dd169e46bba73e8cc9b5559db2f9fb211)
Reviewed-on: https://git-master.nvidia.com/r/c/linux-nv-oot/+/3453099
GVS: buildbot_gerritrpt <buildbot_gerritrpt@nvidia.com>
Reviewed-by: Amulya Yarlagadda <ayarlagadda@nvidia.com>
Tested-by: Amulya Yarlagadda <ayarlagadda@nvidia.com>
---
 drivers/video/tegra/nvmap/nvmap_alloc.c  |  6 +++---
 drivers/video/tegra/nvmap/nvmap_dev.c    |  2 +-
 drivers/video/tegra/nvmap/nvmap_handle.c |  4 ++--
 drivers/video/tegra/nvmap/nvmap_priv.h   | 10 ++++++++++
 scripts/conftest/Makefile                |  1 +
 scripts/conftest/conftest.sh             | 17 +++++++++++++++++
 6 files changed, 34 insertions(+), 6 deletions(-)

diff --git a/drivers/video/tegra/nvmap/nvmap_alloc.c b/drivers/video/tegra/nvmap/nvmap_alloc.c
index 19d62f5f..6b0f80c1 100644
--- a/drivers/video/tegra/nvmap/nvmap_alloc.c
+++ b/drivers/video/tegra/nvmap/nvmap_alloc.c
@@ -596,7 +596,7 @@ static int handle_page_alloc(struct nvmap_client *client,
 	 * Increment the RSS counter of the allocating process by number of pages allocated.
 	 */
 	 h->anon_count = nr_page;
-	 add_mm_counter(mm, MM_ANONPAGES, nr_page);
+	 nvmap_add_mm_counter(mm, MM_ANONPAGES, nr_page);
 
 	/*
 	 * Make sure any data in the caches is cleaned out before
@@ -618,7 +618,7 @@ static int handle_page_alloc(struct nvmap_client *client,
 		ref->mm = mm;
 		ref->anon_count = h->anon_count;
 	} else {
-		add_mm_counter(mm, MM_ANONPAGES, -nr_page);
+		nvmap_add_mm_counter(mm, MM_ANONPAGES, -nr_page);
 		mmput(mm);
 	}
 
@@ -1176,7 +1176,7 @@ void nvmap_free_handle(struct nvmap_client *client,
 	 * to this ref and do mmput so that mm_struct can be freed, if required.
 	 */
 	if (ref->mm != NULL && ref->anon_count != 0) {
-		add_mm_counter(ref->mm, MM_ANONPAGES, -ref->anon_count);
+		nvmap_add_mm_counter(ref->mm, MM_ANONPAGES, -ref->anon_count);
 		mmput(ref->mm);
 		ref->mm = NULL;
 		ref->anon_count = 0;
diff --git a/drivers/video/tegra/nvmap/nvmap_dev.c b/drivers/video/tegra/nvmap/nvmap_dev.c
index db98a8a8..7c475dd5 100644
--- a/drivers/video/tegra/nvmap/nvmap_dev.c
+++ b/drivers/video/tegra/nvmap/nvmap_dev.c
@@ -291,7 +291,7 @@ static void destroy_client(struct nvmap_client *client)
 		 * to this ref and do mmput so that mm_struct can be freed, if required.
 		 */
 		if (ref->mm != NULL && ref->anon_count != 0) {
-			add_mm_counter(ref->mm, MM_ANONPAGES, -ref->anon_count);
+			nvmap_add_mm_counter(ref->mm, MM_ANONPAGES, -ref->anon_count);
 			mmput(ref->mm);
 			ref->mm = NULL;
 			ref->anon_count = 0;
diff --git a/drivers/video/tegra/nvmap/nvmap_handle.c b/drivers/video/tegra/nvmap/nvmap_handle.c
index 3be9c8bb..fff983b0 100644
--- a/drivers/video/tegra/nvmap/nvmap_handle.c
+++ b/drivers/video/tegra/nvmap/nvmap_handle.c
@@ -414,7 +414,7 @@ struct nvmap_handle_ref *nvmap_duplicate_handle(struct nvmap_client *client,
 		if (!mmget_not_zero(ref->mm))
 			goto exit;
 
-		add_mm_counter(ref->mm, MM_ANONPAGES, ref->anon_count);
+		nvmap_add_mm_counter(ref->mm, MM_ANONPAGES, ref->anon_count);
 	}
 
 	if (is_ro) {
@@ -436,7 +436,7 @@ out:
 
 exit_mm:
 	if (ref->anon_count != 0 && ref->mm != NULL) {
-		add_mm_counter(ref->mm, MM_ANONPAGES, -ref->anon_count);
+		nvmap_add_mm_counter(ref->mm, MM_ANONPAGES, -ref->anon_count);
 		mmput(ref->mm);
 		ref->mm = NULL;
 		ref->anon_count = 0;
diff --git a/drivers/video/tegra/nvmap/nvmap_priv.h b/drivers/video/tegra/nvmap/nvmap_priv.h
index 01328bfa..3b505936 100644
--- a/drivers/video/tegra/nvmap/nvmap_priv.h
+++ b/drivers/video/tegra/nvmap/nvmap_priv.h
@@ -916,6 +916,16 @@ static inline struct dma_buf *nvmap_id_array_id_release(struct xarray *xarr, u32
 	return NULL;
 }
 #endif
+
+static inline void nvmap_add_mm_counter(struct mm_struct *mm, int member, long value)
+{
+#if defined(NV_MM_STRUCT_STRUCT_HAS_PERCPU_COUNTER_RSS_STAT) /* Linux v6.2 */
+	percpu_counter_add(&mm->rss_stat[member], value);
+#else
+	atomic_long_add_return(value, &mm->rss_stat.count[member]);
+#endif
+}
+
 void *nvmap_dmabuf_get_drv_data(struct dma_buf *dmabuf,
 		struct device *dev);
 bool is_nvmap_memory_available(size_t size, uint32_t heap);
diff --git a/scripts/conftest/Makefile b/scripts/conftest/Makefile
index 402c5f8c..835a0c0b 100644
--- a/scripts/conftest/Makefile
+++ b/scripts/conftest/Makefile
@@ -134,6 +134,7 @@ NV_CONFTEST_FUNCTION_COMPILE_TESTS += i2c_mux_add_adapter_has_no_class_argument
 NV_CONFTEST_FUNCTION_COMPILE_TESTS += iio_dev_opaque_has_mlock
 NV_CONFTEST_FUNCTION_COMPILE_TESTS += iommu_map_has_gfp_arg
 NV_CONFTEST_FUNCTION_COMPILE_TESTS += kthread_complete_and_exit
+NV_CONFTEST_FUNCTION_COMPILE_TESTS += mm_struct_struct_has_percpu_counter_rss_stat
 NV_CONFTEST_FUNCTION_COMPILE_TESTS += mii_bus_struct_has_read_c45
 NV_CONFTEST_FUNCTION_COMPILE_TESTS += mii_bus_struct_has_write_c45
 NV_CONFTEST_FUNCTION_COMPILE_TESTS += netif_set_tso_max_size
diff --git a/scripts/conftest/conftest.sh b/scripts/conftest/conftest.sh
index 2346a86e..1b15992d 100755
--- a/scripts/conftest/conftest.sh
+++ b/scripts/conftest/conftest.sh
@@ -7395,6 +7395,23 @@ compile_test() {
             compile_check_conftest "$CODE" "NV_MII_BUS_STRUCT_HAS_WRITE_C45" "" "types"
         ;;
 
+        mm_struct_struct_has_percpu_counter_rss_stat)
+            #
+            # Determine if the 'rss_stat' member of the 'mm_struct' structure is
+            # defined with 'percpu_counter'.
+            #
+            # This change was made in Linux v6.2 by commit f1a7941243c1 ("mm:
+            # convert mm's rss stats into percpu_counter2").
+            #
+            CODE="
+            #include <linux/mm_types.h>
+            void conftest_mm_struct_struct_has_percpu_counter_rss_stat(struct mm_struct *mm) {
+                percpu_counter_add(&mm->rss_stat[0], 0);
+            }"
+
+            compile_check_conftest "$CODE" "NV_MM_STRUCT_STRUCT_HAS_PERCPU_COUNTER_RSS_STAT" "" "types"
+        ;;
+
         of_property_for_each_u32_removed_internal_args)
             #
             # Determine if the internal arguments for the macro
-- 
2.51.0

openSUSE Build Service is sponsored by