Skip to contentSkip to conversation

drm/v3d: Enable Big and Super Pages

PATCH Superseded Maíra Canal 3 participants 11 messages Apr 5, 2024 – Apr 15, 2024

Subsystems (3)

v2 of 6 Superseded Latest is v6 →

lore
Share & tools
lore

Maíra Canal

author maintainer
#0 lore raw

v2 cover drm/v3d: Enable Big and Super Pages

This series introduces support for big and super pages in V3D. The V3D MMU has support for 64KB and 1MB pages, called big pages and super pages, which are currently not used. Therefore, this patchset has the intention to enable big and super pages in V3D. The advantage of enabling big and super pages is that if any entry for a page within a big/super page is cached in the MMU, it will be used for the translation of all virtual addresses in the range of that super page without requiring fetching any other entries.

Big/Super pages essentially mean a slightly better performance for users, especially in applications with high memory requirements (e.g. applications that use multiple large BOs).

Using a Raspberry Pi 4 (with a PAGE_SIZE=4KB downstream kernel), when running traces from multiple applications, we were able to see the following improvements:

fps_avg  helped:  warzone2100.70secs.1024x768.trace:                    1.98 -> 2.42     (22.19%) (v1: 2.56)
fps_avg  helped:  warzone2100.30secs.1024x768.trace:                    2 -> 2.45        (21.96%) (v1: 2.39)
fps_avg  helped:  supertuxkart-menus_1024x768.trace:                    123.12 -> 128.39 (4.28%)  (v1: 125.50)
fps_avg  helped:  vkQuake_capture_frames_1_through_1200_1280x720.gfxr:  61.26 -> 62.89   (2.67%)  (v1: 61.36)
fps_avg  helped:  quake2-gles3-1280x720.trace:                          63.42 -> 64.86   (2.27%)  (v1: 64.29)
fps_avg  helped:  ue4_sun_temple_640x480.gfxr:                          25.15 -> 25.63   (1.89%)  (v1: 24.94)
fps_avg  helped:  ue4_shooter_game_high_quality_640x480.gfxr:           19.28 -> 19.61   (1.72%)  (v1: 19.02)
fps_avg  helped:  ue4_shooter_game_shooting_high_quality_640x480.gfxr:  23.58 -> 23.91   (1.39%)  (v1: 23.34)
fps_avg  helped:  quake3e_capture_frames_1_through_1800_1920x1080.gfxr: 61.40 -> 62.00   (0.96%)  (v1: -)
fps_avg  helped:  serious_sam_trace02_1280x720.gfxr:                    49.41 -> 49.84   (0.86%)  (v1: 47.74)
fps_avg  helped:  supertuxkart-racing_1024x768.trace:                   8.69 -> 8.74     (0.56%)  (v1: -)
fps_avg  helped:  sponza_demo01_800x600.gfxr:                           17.55 -> 17.64   (0.50%)  (v1: -)
fps_avg  helped:  quake2-gl1.4-1280x720.trace:                          36.43 -> 36.58   (0.43%)  (v1: 36.57)
fps_avg  helped:  quake3e-1280x720.trace:                               94.49 -> 94.87   (0.40%)  (v1: -)
fps_avg  helped:  sponza_demo02_800x600.gfxr:                           19.79 -> 19.87   (0.39%)  (v1: -)

Using a Raspberry Pi 5 (with a PAGE_SIZE=16KB downstream kernel), when running traces from multiple applications, we were able to see the following improvements:

fps_avg  helped:  warzone2100.30secs.1024x768.trace:                       4.40 -> 4.95     (12.58%) (v1: 4.49)
fps_avg  helped:  vkQuake_capture_frames_1_through_1200_1280x720.gfxr:     135.05 -> 141.21 (4.56%)  (v1: 139.45)
fps_avg  helped:  supertuxkart-menus_1024x768.trace:                       291.73 -> 302.05 (3.54%)  (v1: 303.80)
fps_avg  helped:  quake2-gles3-1280x720.trace:                             148.90 -> 153.57 (3.13%)  (v1: 156.13)
fps_avg  helped:  quake2-gl1.4-1280x720.trace:                             82.60 -> 84.46   (2.25%)  (v1: -)
fps_avg  helped:  ue4_shooter_game_shooting_low_quality_640x480.gfxr:      49.59 -> 50.54   (1.92%)  (v1: 47.30)
fps_avg  helped:  ue4_shooter_game_shooting_high_quality_640x480.gfxr:     51.03 -> 51.93   (1.76%)  (v1: 50.46)
fps_avg  helped:  ue4_shooter_game_high_quality_640x480.gfxr:              31.15 -> 31.64   (1.60%)  (v1: 31.05)
fps_avg  helped:  ue4_sun_temple_640x480.gfxr:                             40.26 -> 40.88   (1.54%)  (v1: 40.23)
fps_avg  helped:  sponza_demo01_800x600.gfxr:                              43.23 -> 43.84   (1.40%)  (v1: 43.68)
fps_avg  helped:  warzone2100.70secs.1024x768.trace:                       4.36 -> 4.42     (1.39%)  (v1: -)
fps_avg  helped:  sponza_demo02_800x600.gfxr:                              48.94 -> 49.51   (1.17%)  (v1: 49.34)
fps_avg  helped:  quake3e_capture_frames_1_through_1800_1920x1080.gfxr:    162.11 -> 163.13 (0.63%)  (v1: 165.71)
fps_avg  helped:  quake3e-1280x720.trace:                                  229.82 -> 231.00 (0.51%)  (v1: 234.51)
fps_avg  helped:  supertuxkart-racing_1024x768.trace:                      20.42 -> 20.48   (0.30%)  (v1: 20.59)
fps_avg  helped:  quake3e_capture_frames_1800_through_2400_1920x1080.gfxr: 157.45 -> 157.61 (0.10%)  (v1: 160.35)
fps_avg  helped:  serious_sam_trace02_1280x720.gfxr:                       59.99 -> 60.02   (0.05%)  (v1: -)

When glancing at the percentage improvement, one might initially perceive v2 as causing a performance downgrade. However, that assumption doesn't hold true. In the case of v1, we were using downstream kernel version 6.1, whereas now, with the kernel updated to 6.6 for v2, there's a small uptick in performance. This indicates an enhancement in the baseline scenario, rather than any detriment caused by v2. Additionally, I've included stats from v1 in the comparisons. Upon scrutinizing the average FPS of v2 in contrast to v1, it becomes evident that v2 not only maintains the improvements but may even surpass them.

This version provides a much safer way to iterate through memory and doesn't hold to the same limitations as v1. For example, v1 had a hard-coded hack that only allowed a huge page to be created if the BO was bigger than 2MB. These limitations are gone now.

This series also introduces changes in the GEM helpers, in order to enable V3D to have a separate mount point for shmfs GEM objects. Any feedback from the community about the changes in the GEM helpers is welcomed!

v1 -> v2: https://lore.kernel.org/dri-devel/[email protected]/

* [1/6] Add Iago's R-b to PATCH 1/5 (Iago Toral)
* [2/6] Create a new function drm_gem_object_init_with_mnt() to define the shmfs mountpoint. Now, we don't touch a bunch of drivers, as drm_gem_object_init() preserves its signature (Tvrtko Ursulin)
* [3/6] Use huge=within_size instead of huge=always, in order to avoid OOM. This also allow us to move away from the 2MB hack. (Tvrtko Ursulin)
* [3/6] Add Iago's R-b to PATCH 3/5 (Iago Toral)
* [5/6] Create a separate patch to reduce the alignment of the node allocation (Iago Toral)
* [6/6] Complete refactoring to the way that we iterate through the memory (Tvrtko Ursulin)
* [6/6] Don't use drm_prime_get_contiguous_size(), as it could give us misleading data (Tvrtko Ursulin)
* [6/6] Use both Big Pages (64K) and Super Pages (1MB)

Best Regards,
- Maíra

Maíra Canal (6):
drm/v3d: Fix return if scheduler initialization fails drm/gem: Create a drm_gem_object_init_with_mnt() function drm/v3d: Introduce gemfs
drm/gem: Create shmem GEM object in a given mountpoint drm/v3d: Reduce the alignment of the node allocation drm/v3d: Enable big and super pages

 drivers/gpu/drm/drm_gem.c              | 34 +++++++++++++++--
 drivers/gpu/drm/drm_gem_shmem_helper.c | 30 +++++++++++++--
 drivers/gpu/drm/v3d/Makefile           |  3 +-
 drivers/gpu/drm/v3d/v3d_bo.c           | 21 ++++++++++-
 drivers/gpu/drm/v3d/v3d_drv.c          |  8 ++++
 drivers/gpu/drm/v3d/v3d_drv.h          | 13 ++++++-
 drivers/gpu/drm/v3d/v3d_gem.c          |  6 ++-
 drivers/gpu/drm/v3d/v3d_gemfs.c        | 52 ++++++++++++++++++++++++++
 drivers/gpu/drm/v3d/v3d_mmu.c          | 46 ++++++++++++++++++-----
 include/drm/drm_gem.h                  |  3 ++
 include/drm/drm_gem_shmem_helper.h     |  3 ++
 11 files changed, 195 insertions(+), 24 deletions(-)
 create mode 100644 drivers/gpu/drm/v3d/v3d_gemfs.c

--
2.44.0

author maintainer
#1 lore raw

PATCH v2 1/6 drm/v3d: Fix return if scheduler initialization fails

If the scheduler initialization fails, GEM initialization must fail as well. Therefore, if v3d_sched_init() fails, free the DMA memory allocated and return the error value in v3d_gem_init().

---
 drivers/gpu/drm/v3d/v3d_gem.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/gpu/drm/v3d/v3d_gem.c +2 -1
index afc565078c78..66f4b78a6b2e 100644
@@ -290,8 +290,9 @@ v3d_gem_init(struct drm_device *dev)
 	ret = v3d_sched_init(v3d);
 	if (ret) {
 		drm_mm_takedown(&v3d->mm);
-		dma_free_coherent(v3d->drm.dev, 4096 * 1024, (void *)v3d->pt,
+		dma_free_coherent(v3d->drm.dev, pt_size, (void *)v3d->pt,
 				  v3d->pt_paddr);
+		return ret;
 	}
 
 	return 0;
Signature · 1 line
2.44.0
author maintainer
#2 lore raw

PATCH v2 2/6 drm/gem: Create a drm_gem_object_init_with_mnt() function

In reply to Maíra Canal

For some applications, such as applications that uses huge pages, we might want to have a different mountpoint, for which we pass mount flags that better match our usecase.

Therefore, create a new function drm_gem_object_init_with_mnt() that allow us to define the tmpfs mountpoint where the GEM object will be created. If this parameter is NULL, then we fallback to shmem_file_setup().

---
 drivers/gpu/drm/drm_gem.c | 34 ++++++++++++++++++++++++++++++----
 include/drm/drm_gem.h     |  3 +++
 2 files changed, 33 insertions(+), 4 deletions(-)
drivers/gpu/drm/drm_gem.c +30 -4
index d4bbc5d109c8..74ebe68e3d61 100644
@@ -114,22 +114,32 @@ drm_gem_init(struct drm_device *dev)
 }
 
 /**
- * drm_gem_object_init - initialize an allocated shmem-backed GEM object
+ * drm_gem_object_init_with_mnt - initialize an allocated shmem-backed GEM
+ * object in a given shmfs mountpoint
+ *
  * @dev: drm_device the object should be initialized for
  * @obj: drm_gem_object to initialize
  * @size: object size
+ * @gemfs: tmpfs mount where the GEM object will be created. If NULL, use
+ * the usual tmpfs mountpoint (`shm_mnt`).
  *
  * Initialize an already allocated GEM object of the specified size with
  * shmfs backing store.
  */
-int drm_gem_object_init(struct drm_device *dev,
-			struct drm_gem_object *obj, size_t size)
+int drm_gem_object_init_with_mnt(struct drm_device *dev,
+				 struct drm_gem_object *obj, size_t size,
+				 struct vfsmount *gemfs)
 {
 	struct file *filp;
 
 	drm_gem_private_object_init(dev, obj, size);
 
-	filp = shmem_file_setup("drm mm object", size, VM_NORESERVE);
+	if (gemfs)
+		filp = shmem_file_setup_with_mnt(gemfs, "drm mm object", size,
+						 VM_NORESERVE);
+	else
+		filp = shmem_file_setup("drm mm object", size, VM_NORESERVE);
+
 	if (IS_ERR(filp))
 		return PTR_ERR(filp);
 
@@ -137,6 +147,22 @@ int drm_gem_object_init(struct drm_device *dev,
 
 	return 0;
 }
+EXPORT_SYMBOL(drm_gem_object_init_with_mnt);
+
+/**
+ * drm_gem_object_init - initialize an allocated shmem-backed GEM object
+ * @dev: drm_device the object should be initialized for
+ * @obj: drm_gem_object to initialize
+ * @size: object size
+ *
+ * Initialize an already allocated GEM object of the specified size with
+ * shmfs backing store.
+ */
+int drm_gem_object_init(struct drm_device *dev, struct drm_gem_object *obj,
+			size_t size)
+{
+	return drm_gem_object_init_with_mnt(dev, obj, size, NULL);
+}
 EXPORT_SYMBOL(drm_gem_object_init);
 
 /**
include/drm/drm_gem.h +3 -0
index bae4865b2101..2ebf6e10cc44 100644
@@ -472,6 +472,9 @@ void drm_gem_object_release(struct drm_gem_object *obj);
 void drm_gem_object_free(struct kref *kref);
 int drm_gem_object_init(struct drm_device *dev,
 			struct drm_gem_object *obj, size_t size);
+int drm_gem_object_init_with_mnt(struct drm_device *dev,
+				 struct drm_gem_object *obj, size_t size,
+				 struct vfsmount *gemfs);
 void drm_gem_private_object_init(struct drm_device *dev,
 				 struct drm_gem_object *obj, size_t size);
 void drm_gem_private_object_fini(struct drm_gem_object *obj);

--
2.44.0

Reviewed-by on v2 2/6
#3 lore raw

Re: [PATCH v2 2/6] drm/gem: Create a drm_gem_object_init_with_mnt() function

Quoted 66 lines · Maíra Canal

For some applications, such as applications that uses huge pages, we might want to have a different mountpoint, for which we pass mount flags that better match our usecase.

Therefore, create a new function drm_gem_object_init_with_mnt() that allow us to define the tmpfs mountpoint where the GEM object will be created. If this parameter is NULL, then we fallback to shmem_file_setup().

---
  drivers/gpu/drm/drm_gem.c | 34 ++++++++++++++++++++++++++++++----
  include/drm/drm_gem.h     |  3 +++
  2 files changed, 33 insertions(+), 4 deletions(-)
drivers/gpu/drm/drm_gem.c +21 -4
index d4bbc5d109c8..74ebe68e3d61 100644
@@ -114,22 +114,32 @@ drm_gem_init(struct drm_device *dev)
  }
 
  /**
- * drm_gem_object_init - initialize an allocated shmem-backed GEM object
+ * drm_gem_object_init_with_mnt - initialize an allocated shmem-backed GEM
+ * object in a given shmfs mountpoint
+ *
   * @dev: drm_device the object should be initialized for
   * @obj: drm_gem_object to initialize
   * @size: object size
+ * @gemfs: tmpfs mount where the GEM object will be created. If NULL, use
+ * the usual tmpfs mountpoint (`shm_mnt`).
   *
   * Initialize an already allocated GEM object of the specified size with
   * shmfs backing store.
   */
-int drm_gem_object_init(struct drm_device *dev,
-			struct drm_gem_object *obj, size_t size)
+int drm_gem_object_init_with_mnt(struct drm_device *dev,
+				 struct drm_gem_object *obj, size_t size,
+				 struct vfsmount *gemfs)
  {
  	struct file *filp;
 
  	drm_gem_private_object_init(dev, obj, size);
 
-	filp = shmem_file_setup("drm mm object", size, VM_NORESERVE);
+	if (gemfs)
+		filp = shmem_file_setup_with_mnt(gemfs, "drm mm object", size,
+						 VM_NORESERVE);
+	else
+		filp = shmem_file_setup("drm mm object", size, VM_NORESERVE);
+
  	if (IS_ERR(filp))
  		return PTR_ERR(filp);
 
@@ -137,6 +147,22 @@ int drm_gem_object_init(struct drm_device *dev,
 
  	return 0;
  }
+EXPORT_SYMBOL(drm_gem_object_init_with_mnt);
+
+/**
+ * drm_gem_object_init - initialize an allocated shmem-backed GEM object
+ * @dev: drm_device the object should be initialized for
+ * @obj: drm_gem_object to initialize
+ * @size: object size

drivers/gpu/drm/drm_gem.c int drm_gem_object_init(struct drm_device *dev, · line 166

Quoted 10 lines
drivers/gpu/drm/drm_gem.c +9 -0
+ *
+ * Initialize an already allocated GEM object of the specified size with
+ * shmfs backing store.
+ */
+int drm_gem_object_init(struct drm_device *dev, struct drm_gem_object *obj,
+			size_t size)
+{
+	return drm_gem_object_init_with_mnt(dev, obj, size, NULL);
+}
  EXPORT_SYMBOL(drm_gem_object_init);

I would be tempted to static inline this one but see what other people think. (One wise kernel legend was once annoyed by trivial wrappers / function calls. But some other are then annoyed by static inlines.. so dunno.) For either flavour:

Regards,

Tvrtko

Quoted 20 lines
drivers/gpu/drm/drm_gem.c +0 -0
  /**
include/drm/drm_gem.h +3 -1
index bae4865b2101..2ebf6e10cc44 100644
@@ -472,6 +472,9 @@ void drm_gem_object_release(struct drm_gem_object *obj);
  void drm_gem_object_free(struct kref *kref);
  int drm_gem_object_init(struct drm_device *dev,
  			struct drm_gem_object *obj, size_t size);
+int drm_gem_object_init_with_mnt(struct drm_device *dev,
+				 struct drm_gem_object *obj, size_t size,
+				 struct vfsmount *gemfs);
  void drm_gem_private_object_init(struct drm_device *dev,
  				 struct drm_gem_object *obj, size_t size);
  void drm_gem_private_object_fini(struct drm_gem_object *obj);
--

2.44.0

author maintainer
#4 lore raw

PATCH v2 3/6 drm/v3d: Introduce gemfs

In reply to Maíra Canal

Create a separate "tmpfs" kernel mount for V3D. This will allow us to move away from the shmemfs shm_mnt and gives the flexibility to do things like set our own mount options. Here, the interest is to use "huge=", which should allow us to enable the use of THP for our shmem-backed objects.

---
 drivers/gpu/drm/v3d/Makefile    |  3 ++-
 drivers/gpu/drm/v3d/v3d_drv.h   |  9 +++++++
 drivers/gpu/drm/v3d/v3d_gem.c   |  3 +++
 drivers/gpu/drm/v3d/v3d_gemfs.c | 46 +++++++++++++++++++++++++++++++++
 4 files changed, 60 insertions(+), 1 deletion(-)
 create mode 100644 drivers/gpu/drm/v3d/v3d_gemfs.c
drivers/gpu/drm/v3d/Makefile +2 -1
index b7d673f1153b..fcf710926057 100644
@@ -13,7 +13,8 @@ v3d-y := \
 	v3d_trace_points.o \
 	v3d_sched.o \
 	v3d_sysfs.o \
-	v3d_submit.o
+	v3d_submit.o \
+	v3d_gemfs.o
 
 v3d-$(CONFIG_DEBUG_FS) += v3d_debugfs.o
 
drivers/gpu/drm/v3d/v3d_drv.h +9 -0
index 1950c723dde1..d2ce8222771a 100644
@@ -119,6 +119,11 @@ struct v3d_dev {
 	struct drm_mm mm;
 	spinlock_t mm_lock;
 
+	/*
+	 * tmpfs instance used for shmem backed objects
+	 */
+	struct vfsmount *gemfs;
+
 	struct work_struct overflow_mem_work;
 
 	struct v3d_bin_job *bin_job;
@@ -519,6 +524,10 @@ void v3d_reset(struct v3d_dev *v3d);
 void v3d_invalidate_caches(struct v3d_dev *v3d);
 void v3d_clean_caches(struct v3d_dev *v3d);
 
+/* v3d_gemfs.c */
+void v3d_gemfs_init(struct v3d_dev *v3d);
+void v3d_gemfs_fini(struct v3d_dev *v3d);
+
 /* v3d_submit.c */
 void v3d_job_cleanup(struct v3d_job *job);
 void v3d_job_put(struct v3d_job *job);
drivers/gpu/drm/v3d/v3d_gem.c +3 -0
index 66f4b78a6b2e..faefbe497e8d 100644
@@ -287,6 +287,8 @@ v3d_gem_init(struct drm_device *dev)
 	v3d_init_hw_state(v3d);
 	v3d_mmu_set_page_table(v3d);
 
+	v3d_gemfs_init(v3d);
+
 	ret = v3d_sched_init(v3d);
 	if (ret) {
 		drm_mm_takedown(&v3d->mm);
@@ -304,6 +306,7 @@ v3d_gem_destroy(struct drm_device *dev)
 	struct v3d_dev *v3d = to_v3d_dev(dev);
 
 	v3d_sched_fini(v3d);
+	v3d_gemfs_fini(v3d);
 
 	/* Waiting for jobs to finish would need to be done before
 	 * unregistering V3D.
drivers/gpu/drm/v3d/v3d_gemfs.c new file +46 -0

46 lines not on this page · raw message

Signature · 1 line
2.44.0
author maintainer
#5 lore raw

PATCH v2 4/6 drm/gem: Create shmem GEM object in a given mountpoint

In reply to Maíra Canal

Create a function drm_gem_shmem_create_with_mnt(), similar to drm_gem_shmem_create(), that has a mountpoint as a argument. This function will create a shmem GEM object in a given tmpfs mountpoint.

This function will be useful for drivers that have a special mountpoint with flags enabled.

---
 drivers/gpu/drm/drm_gem_shmem_helper.c | 30 ++++++++++++++++++++++----
 include/drm/drm_gem_shmem_helper.h     |  3 +++
 2 files changed, 29 insertions(+), 4 deletions(-)
drivers/gpu/drm/drm_gem_shmem_helper.c +26 -4
index 177773bcdbfd..10b7c4c769a3 100644
@@ -49,7 +49,8 @@ static const struct drm_gem_object_funcs drm_gem_shmem_funcs = {
 };
 
 static struct drm_gem_shmem_object *
-__drm_gem_shmem_create(struct drm_device *dev, size_t size, bool private)
+__drm_gem_shmem_create(struct drm_device *dev, size_t size, bool private,
+		       struct vfsmount *gemfs)
 {
 	struct drm_gem_shmem_object *shmem;
 	struct drm_gem_object *obj;
@@ -76,7 +77,7 @@ __drm_gem_shmem_create(struct drm_device *dev, size_t size, bool private)
 		drm_gem_private_object_init(dev, obj, size);
 		shmem->map_wc = false; /* dma-buf mappings use always writecombine */
 	} else {
-		ret = drm_gem_object_init(dev, obj, size);
+		ret = drm_gem_object_init_with_mnt(dev, obj, size, gemfs);
 	}
 	if (ret) {
 		drm_gem_private_object_fini(obj);
@@ -123,10 +124,31 @@ __drm_gem_shmem_create(struct drm_device *dev, size_t size, bool private)
  */
 struct drm_gem_shmem_object *drm_gem_shmem_create(struct drm_device *dev, size_t size)
 {
-	return __drm_gem_shmem_create(dev, size, false);
+	return __drm_gem_shmem_create(dev, size, false, NULL);
 }
 EXPORT_SYMBOL_GPL(drm_gem_shmem_create);
 
+/**
+ * drm_gem_shmem_create_with_mnt - Allocate an object with the given size in a
+ * given mountpoint
+ * @dev: DRM device
+ * @size: Size of the object to allocate
+ * @gemfs: tmpfs mount where the GEM object will be created
+ *
+ * This function creates a shmem GEM object in a given tmpfs mountpoint.
+ *
+ * Returns:
+ * A struct drm_gem_shmem_object * on success or an ERR_PTR()-encoded negative
+ * error code on failure.
+ */
+struct drm_gem_shmem_object *drm_gem_shmem_create_with_mnt(struct drm_device *dev,
+							   size_t size,
+							   struct vfsmount *gemfs)
+{
+	return __drm_gem_shmem_create(dev, size, false, gemfs);
+}
+EXPORT_SYMBOL_GPL(drm_gem_shmem_create_with_mnt);
+
 /**
  * drm_gem_shmem_free - Free resources associated with a shmem GEM object
  * @shmem: shmem GEM object to free
@@ -760,7 +782,7 @@ drm_gem_shmem_prime_import_sg_table(struct drm_device *dev,
 	size_t size = PAGE_ALIGN(attach->dmabuf->size);
 	struct drm_gem_shmem_object *shmem;
 
-	shmem = __drm_gem_shmem_create(dev, size, true);
+	shmem = __drm_gem_shmem_create(dev, size, true, NULL);
 	if (IS_ERR(shmem))
 		return ERR_CAST(shmem);
 
include/drm/drm_gem_shmem_helper.h +3 -0
index efbc9f27312b..d22e3fb53631 100644
@@ -97,6 +97,9 @@ struct drm_gem_shmem_object {
 	container_of(obj, struct drm_gem_shmem_object, base)
 
 struct drm_gem_shmem_object *drm_gem_shmem_create(struct drm_device *dev, size_t size);
+struct drm_gem_shmem_object *drm_gem_shmem_create_with_mnt(struct drm_device *dev,
+							   size_t size,
+							   struct vfsmount *gemfs);
 void drm_gem_shmem_free(struct drm_gem_shmem_object *shmem);
 
 void drm_gem_shmem_put_pages(struct drm_gem_shmem_object *shmem);
Signature · 1 line
2.44.0
Reviewed-by on v2 4/6
#6 lore raw

Re: [PATCH v2 4/6] drm/gem: Create shmem GEM object in a given mountpoint

Quoted 83 lines · Maíra Canal

Create a function drm_gem_shmem_create_with_mnt(), similar to drm_gem_shmem_create(), that has a mountpoint as a argument. This function will create a shmem GEM object in a given tmpfs mountpoint.

This function will be useful for drivers that have a special mountpoint with flags enabled.

---
  drivers/gpu/drm/drm_gem_shmem_helper.c | 30 ++++++++++++++++++++++----
  include/drm/drm_gem_shmem_helper.h     |  3 +++
  2 files changed, 29 insertions(+), 4 deletions(-)
drivers/gpu/drm/drm_gem_shmem_helper.c +26 -4
index 177773bcdbfd..10b7c4c769a3 100644
@@ -49,7 +49,8 @@ static const struct drm_gem_object_funcs drm_gem_shmem_funcs = {
  };
 
  static struct drm_gem_shmem_object *
-__drm_gem_shmem_create(struct drm_device *dev, size_t size, bool private)
+__drm_gem_shmem_create(struct drm_device *dev, size_t size, bool private,
+		       struct vfsmount *gemfs)
  {
  	struct drm_gem_shmem_object *shmem;
  	struct drm_gem_object *obj;
@@ -76,7 +77,7 @@ __drm_gem_shmem_create(struct drm_device *dev, size_t size, bool private)
  		drm_gem_private_object_init(dev, obj, size);
  		shmem->map_wc = false; /* dma-buf mappings use always writecombine */
  	} else {
-		ret = drm_gem_object_init(dev, obj, size);
+		ret = drm_gem_object_init_with_mnt(dev, obj, size, gemfs);
  	}
  	if (ret) {
  		drm_gem_private_object_fini(obj);
@@ -123,10 +124,31 @@ __drm_gem_shmem_create(struct drm_device *dev, size_t size, bool private)
   */
  struct drm_gem_shmem_object *drm_gem_shmem_create(struct drm_device *dev, size_t size)
  {
-	return __drm_gem_shmem_create(dev, size, false);
+	return __drm_gem_shmem_create(dev, size, false, NULL);
  }
  EXPORT_SYMBOL_GPL(drm_gem_shmem_create);
 
+/**
+ * drm_gem_shmem_create_with_mnt - Allocate an object with the given size in a
+ * given mountpoint
+ * @dev: DRM device
+ * @size: Size of the object to allocate
+ * @gemfs: tmpfs mount where the GEM object will be created
+ *
+ * This function creates a shmem GEM object in a given tmpfs mountpoint.
+ *
+ * Returns:
+ * A struct drm_gem_shmem_object * on success or an ERR_PTR()-encoded negative
+ * error code on failure.
+ */
+struct drm_gem_shmem_object *drm_gem_shmem_create_with_mnt(struct drm_device *dev,
+							   size_t size,
+							   struct vfsmount *gemfs)
+{
+	return __drm_gem_shmem_create(dev, size, false, gemfs);
+}
+EXPORT_SYMBOL_GPL(drm_gem_shmem_create_with_mnt);
+
  /**
   * drm_gem_shmem_free - Free resources associated with a shmem GEM object
   * @shmem: shmem GEM object to free
@@ -760,7 +782,7 @@ drm_gem_shmem_prime_import_sg_table(struct drm_device *dev,
  	size_t size = PAGE_ALIGN(attach->dmabuf->size);
  	struct drm_gem_shmem_object *shmem;
 
-	shmem = __drm_gem_shmem_create(dev, size, true);
+	shmem = __drm_gem_shmem_create(dev, size, true, NULL);
  	if (IS_ERR(shmem))
  		return ERR_CAST(shmem);
 
include/drm/drm_gem_shmem_helper.h +0 -0
index efbc9f27312b..d22e3fb53631 100644

include/drm/drm_gem_shmem_helper.h struct drm_gem_shmem_object { · line 105

Quoted 9 lines
include/drm/drm_gem_shmem_helper.h +3 -0
@@ -97,6 +97,9 @@ struct drm_gem_shmem_object {
  	container_of(obj, struct drm_gem_shmem_object, base)
 
  struct drm_gem_shmem_object *drm_gem_shmem_create(struct drm_device *dev, size_t size);
+struct drm_gem_shmem_object *drm_gem_shmem_create_with_mnt(struct drm_device *dev,
+							   size_t size,
+							   struct vfsmount *gemfs);
  void drm_gem_shmem_free(struct drm_gem_shmem_object *shmem);
 
  void drm_gem_shmem_put_pages(struct drm_gem_shmem_object *shmem);

Regards,

Tvrtko

author maintainer
#7 lore raw

PATCH v2 5/6 drm/v3d: Reduce the alignment of the node allocation

In reply to Maíra Canal

Currently, we are using an alignment of 128 kB to insert a node, which ends up wasting memory as we perform plenty of small BOs allocations (<= 4 kB). We require that allocations are aligned to 128Kb so for any allocation smaller than that, we are wasting the difference.

This implies that we cannot effectively use the whole 4 GB address space available for the GPU in the RPi 4. Currently, we can allocate up to 32000 BOs of 4 kB (~140 MB) and 3000 BOs of 400 kB (~1,3 GB). This can be quite limiting for applications that have a high memory requirement, such as vkoverhead [1].

By reducing the page alignment to 4 kB, we can allocate up to 1000000 BOs of 4 kB (~4 GB) and 10000 BOs of 400 kB (~4 GB). Moreover, by performing benchmarks, we were able to attest that reducing the page alignment to 4 kB can provide a general performance improvement in OpenGL applications (e.g. glmark2).

Therefore, this patch reduces the alignment of the node allocation to 4 kB, which will allow RPi users to explore the whole 4GB virtual address space provided by the hardware. Also, this patch allow users to fully run vkoverhead in the RPi 4/5, solving the issue reported in [1].

[1] https://github.com/zmike/vkoverhead/issues/14

---
 drivers/gpu/drm/v3d/v3d_bo.c  | 2 +-
 drivers/gpu/drm/v3d/v3d_drv.h | 2 --
 2 files changed, 1 insertion(+), 3 deletions(-)
drivers/gpu/drm/v3d/v3d_bo.c +1 -1
index a07ede668cc1..79e31c5299b1 100644
@@ -110,7 +110,7 @@ v3d_bo_create_finish(struct drm_gem_object *obj)
 	 */
 	ret = drm_mm_insert_node_generic(&v3d->mm, &bo->node,
 					 obj->size >> V3D_MMU_PAGE_SHIFT,
-					 GMP_GRANULARITY >> V3D_MMU_PAGE_SHIFT, 0, 0);
+					 SZ_4K >> V3D_MMU_PAGE_SHIFT, 0, 0);
 	spin_unlock(&v3d->mm_lock);
 	if (ret)
 		return ret;
drivers/gpu/drm/v3d/v3d_drv.h +0 -2
index d2ce8222771a..17236ee23490 100644
@@ -17,8 +17,6 @@ struct clk;
 struct platform_device;
 struct reset_control;
 
-#define GMP_GRANULARITY (128 * 1024)
-
 #define V3D_MMU_PAGE_SHIFT 12
 
 #define V3D_MAX_QUEUES (V3D_CPU + 1)
Signature · 1 line
2.44.0
author maintainer
#8 lore raw

PATCH v2 6/6 drm/v3d: Enable big and super pages

In reply to Maíra Canal

The V3D MMU also supports 64KB and 1MB pages, called big and super pages, respectively. In order to set a 64KB page or 1MB page in the MMU, we need to make sure that page table entries for all 4KB pages within a big/super page must be correctly configured.

In order to create a big/super page, we need a contiguous memory region. That's why we use a separate mountpoint with THP enabled. In order to place the page table entries in the MMU, we iterate over the 16 4KB pages (for big pages) or 256 4KB pages (for super pages) and insert the PTE.

---
 drivers/gpu/drm/v3d/v3d_bo.c    | 21 +++++++++++++--
 drivers/gpu/drm/v3d/v3d_drv.c   |  8 ++++++
 drivers/gpu/drm/v3d/v3d_drv.h   |  2 ++
 drivers/gpu/drm/v3d/v3d_gemfs.c |  6 +++++
 drivers/gpu/drm/v3d/v3d_mmu.c   | 46 ++++++++++++++++++++++++++-------
 5 files changed, 71 insertions(+), 12 deletions(-)
drivers/gpu/drm/v3d/v3d_bo.c +19 -2
index 79e31c5299b1..cfe82232886a 100644
@@ -94,6 +94,7 @@ v3d_bo_create_finish(struct drm_gem_object *obj)
 	struct v3d_dev *v3d = to_v3d_dev(obj->dev);
 	struct v3d_bo *bo = to_v3d_bo(obj);
 	struct sg_table *sgt;
+	u64 align;
 	int ret;
 
 	/* So far we pin the BO in the MMU for its lifetime, so use
@@ -103,6 +104,15 @@ v3d_bo_create_finish(struct drm_gem_object *obj)
 	if (IS_ERR(sgt))
 		return PTR_ERR(sgt);
 
+	if (!v3d->super_pages)
+		align = SZ_4K;
+	else if (obj->size >= SZ_1M)
+		align = SZ_1M;
+	else if (obj->size >= SZ_64K)
+		align = SZ_64K;
+	else
+		align = SZ_4K;
+
 	spin_lock(&v3d->mm_lock);
 	/* Allocate the object's space in the GPU's page tables.
 	 * Inserting PTEs will happen later, but the offset is for the
@@ -110,7 +120,7 @@ v3d_bo_create_finish(struct drm_gem_object *obj)
 	 */
 	ret = drm_mm_insert_node_generic(&v3d->mm, &bo->node,
 					 obj->size >> V3D_MMU_PAGE_SHIFT,
-					 SZ_4K >> V3D_MMU_PAGE_SHIFT, 0, 0);
+					 align >> V3D_MMU_PAGE_SHIFT, 0, 0);
 	spin_unlock(&v3d->mm_lock);
 	if (ret)
 		return ret;
@@ -130,10 +140,17 @@ struct v3d_bo *v3d_bo_create(struct drm_device *dev, struct drm_file *file_priv,
 			     size_t unaligned_size)
 {
 	struct drm_gem_shmem_object *shmem_obj;
+	struct v3d_dev *v3d = to_v3d_dev(dev);
 	struct v3d_bo *bo;
 	int ret;
 
-	shmem_obj = drm_gem_shmem_create(dev, unaligned_size);
+	/* Let the user opt out of allocating the BOs with THP */
+	if (v3d->super_pages)
+		shmem_obj = drm_gem_shmem_create_with_mnt(dev, unaligned_size,
+							  v3d->gemfs);
+	else
+		shmem_obj = drm_gem_shmem_create(dev, unaligned_size);
+
 	if (IS_ERR(shmem_obj))
 		return ERR_CAST(shmem_obj);
 	bo = to_v3d_bo(&shmem_obj->base);
drivers/gpu/drm/v3d/v3d_drv.c +8 -0
index 3debf37e7d9b..3dbd29560be4 100644
@@ -36,6 +36,12 @@
 #define DRIVER_MINOR 0
 #define DRIVER_PATCHLEVEL 0
 
+static bool super_pages = true;
+module_param_named(super_pages, super_pages, bool, 0400);
+MODULE_PARM_DESC(super_pages, "Enable/Disable Super Pages support. Note: \
+			       To enable Super Pages, you need support to \
+			       enable THP.");
+
 static int v3d_get_param_ioctl(struct drm_device *dev, void *data,
 			       struct drm_file *file_priv)
 {
@@ -308,6 +314,8 @@ static int v3d_platform_drm_probe(struct platform_device *pdev)
 		return -ENOMEM;
 	}
 
+	v3d->super_pages = super_pages;
+
 	ret = v3d_gem_init(drm);
 	if (ret)
 		goto dma_free;
drivers/gpu/drm/v3d/v3d_drv.h +2 -0
index 17236ee23490..0a7aacf51164 100644
@@ -18,6 +18,7 @@ struct platform_device;
 struct reset_control;
 
 #define V3D_MMU_PAGE_SHIFT 12
+#define V3D_PAGE_FACTOR (PAGE_SIZE >> V3D_MMU_PAGE_SHIFT)
 
 #define V3D_MAX_QUEUES (V3D_CPU + 1)
 
@@ -121,6 +122,7 @@ struct v3d_dev {
 	 * tmpfs instance used for shmem backed objects
 	 */
 	struct vfsmount *gemfs;
+	bool super_pages;
 
 	struct work_struct overflow_mem_work;
 
drivers/gpu/drm/v3d/v3d_gemfs.c +6 -0
index 31cf5bd11e39..7ee55b32c36e 100644
@@ -12,6 +12,10 @@ void v3d_gemfs_init(struct v3d_dev *v3d)
 	struct file_system_type *type;
 	struct vfsmount *gemfs;
 
+	/* The user doesn't want support for Super Pages */
+	if (!v3d->super_pages)
+		goto err;
+
 	/*
 	 * By creating our own shmemfs mountpoint, we can pass in
 	 * mount flags that better match our usecase. However, we
@@ -35,6 +39,8 @@ void v3d_gemfs_init(struct v3d_dev *v3d)
 
 err:
 	v3d->gemfs = NULL;
+	v3d->super_pages = false;
+
 	drm_notice(&v3d->drm,
 		   "Transparent Hugepage support is recommended for optimal performance on this platform!\n");
 }
drivers/gpu/drm/v3d/v3d_mmu.c +36 -10

63 lines not on this page · raw message

--
2.44.0

#9 lore raw

Re: [PATCH v2 6/6] drm/v3d: Enable big and super pages

Quoted 80 lines · Maíra Canal

The V3D MMU also supports 64KB and 1MB pages, called big and super pages, respectively. In order to set a 64KB page or 1MB page in the MMU, we need to make sure that page table entries for all 4KB pages within a big/super page must be correctly configured.

In order to create a big/super page, we need a contiguous memory region. That's why we use a separate mountpoint with THP enabled. In order to place the page table entries in the MMU, we iterate over the 16 4KB pages (for big pages) or 256 4KB pages (for super pages) and insert the PTE.

---
  drivers/gpu/drm/v3d/v3d_bo.c    | 21 +++++++++++++--
  drivers/gpu/drm/v3d/v3d_drv.c   |  8 ++++++
  drivers/gpu/drm/v3d/v3d_drv.h   |  2 ++
  drivers/gpu/drm/v3d/v3d_gemfs.c |  6 +++++
  drivers/gpu/drm/v3d/v3d_mmu.c   | 46 ++++++++++++++++++++++++++-------
  5 files changed, 71 insertions(+), 12 deletions(-)
drivers/gpu/drm/v3d/v3d_bo.c +19 -2
index 79e31c5299b1..cfe82232886a 100644
@@ -94,6 +94,7 @@ v3d_bo_create_finish(struct drm_gem_object *obj)
  	struct v3d_dev *v3d = to_v3d_dev(obj->dev);
  	struct v3d_bo *bo = to_v3d_bo(obj);
  	struct sg_table *sgt;
+	u64 align;
  	int ret;
 
  	/* So far we pin the BO in the MMU for its lifetime, so use
@@ -103,6 +104,15 @@ v3d_bo_create_finish(struct drm_gem_object *obj)
  	if (IS_ERR(sgt))
  		return PTR_ERR(sgt);
 
+	if (!v3d->super_pages)
+		align = SZ_4K;
+	else if (obj->size >= SZ_1M)
+		align = SZ_1M;
+	else if (obj->size >= SZ_64K)
+		align = SZ_64K;
+	else
+		align = SZ_4K;
+
  	spin_lock(&v3d->mm_lock);
  	/* Allocate the object's space in the GPU's page tables.
  	 * Inserting PTEs will happen later, but the offset is for the
@@ -110,7 +120,7 @@ v3d_bo_create_finish(struct drm_gem_object *obj)
  	 */
  	ret = drm_mm_insert_node_generic(&v3d->mm, &bo->node,
  					 obj->size >> V3D_MMU_PAGE_SHIFT,
-					 SZ_4K >> V3D_MMU_PAGE_SHIFT, 0, 0);
+					 align >> V3D_MMU_PAGE_SHIFT, 0, 0);
  	spin_unlock(&v3d->mm_lock);
  	if (ret)
  		return ret;
@@ -130,10 +140,17 @@ struct v3d_bo *v3d_bo_create(struct drm_device *dev, struct drm_file *file_priv,
  			     size_t unaligned_size)
  {
  	struct drm_gem_shmem_object *shmem_obj;
+	struct v3d_dev *v3d = to_v3d_dev(dev);
  	struct v3d_bo *bo;
  	int ret;
 
-	shmem_obj = drm_gem_shmem_create(dev, unaligned_size);
+	/* Let the user opt out of allocating the BOs with THP */
+	if (v3d->super_pages)
+		shmem_obj = drm_gem_shmem_create_with_mnt(dev, unaligned_size,
+							  v3d->gemfs);
+	else
+		shmem_obj = drm_gem_shmem_create(dev, unaligned_size);
+
  	if (IS_ERR(shmem_obj))
  		return ERR_CAST(shmem_obj);
  	bo = to_v3d_bo(&shmem_obj->base);
drivers/gpu/drm/v3d/v3d_drv.c +0 -0
index 3debf37e7d9b..3dbd29560be4 100644

drivers/gpu/drm/v3d/v3d_drv.c line 43

Quoted 8 lines
drivers/gpu/drm/v3d/v3d_drv.c +5 -0
@@ -36,6 +36,12 @@
  #define DRIVER_MINOR 0
  #define DRIVER_PATCHLEVEL 0
 
+static bool super_pages = true;
+module_param_named(super_pages, super_pages, bool, 0400);
+MODULE_PARM_DESC(super_pages, "Enable/Disable Super Pages support. Note: \
+			       To enable Super Pages, you need support to \
+			       enable THP.");

Maybe not expose the modparam unless CONFIG_TRANSPARENT_HUGEPAGE is enabled? Then you wouldn't have to explain the dependency in the description.

Quoted 26 lines
drivers/gpu/drm/v3d/v3d_drv.c +3 -0
+
  static int v3d_get_param_ioctl(struct drm_device *dev, void *data,
  			       struct drm_file *file_priv)
  {
@@ -308,6 +314,8 @@ static int v3d_platform_drm_probe(struct platform_device *pdev)
  		return -ENOMEM;
  	}
 
+	v3d->super_pages = super_pages;
+
  	ret = v3d_gem_init(drm);
  	if (ret)
  		goto dma_free;
drivers/gpu/drm/v3d/v3d_drv.h +1 -0
index 17236ee23490..0a7aacf51164 100644
@@ -18,6 +18,7 @@ struct platform_device;
  struct reset_control;
 
  #define V3D_MMU_PAGE_SHIFT 12
+#define V3D_PAGE_FACTOR (PAGE_SIZE >> V3D_MMU_PAGE_SHIFT)
 
  #define V3D_MAX_QUEUES (V3D_CPU + 1)
 

drivers/gpu/drm/v3d/v3d_drv.h struct v3d_dev { · line 125

Quoted 4 lines
drivers/gpu/drm/v3d/v3d_drv.h +1 -0
@@ -121,6 +122,7 @@ struct v3d_dev {
  	 * tmpfs instance used for shmem backed objects
  	 */
  	struct vfsmount *gemfs;
+	bool super_pages;

You could probably get away with not having to add this new bool by basing the runtime checks of v3d->gemfs != NULL. In v3d_gemfs_init you would access the global module param variable directly, after the THP kconfig check. Unless you see a downside it always sounds attractive to me to add less.

Quoted 49 lines
drivers/gpu/drm/v3d/v3d_drv.h +0 -0
  	struct work_struct overflow_mem_work;
 
drivers/gpu/drm/v3d/v3d_gemfs.c +6 -0
index 31cf5bd11e39..7ee55b32c36e 100644
@@ -12,6 +12,10 @@ void v3d_gemfs_init(struct v3d_dev *v3d)
  	struct file_system_type *type;
  	struct vfsmount *gemfs;
 
+	/* The user doesn't want support for Super Pages */
+	if (!v3d->super_pages)
+		goto err;
+
  	/*
  	 * By creating our own shmemfs mountpoint, we can pass in
  	 * mount flags that better match our usecase. However, we
@@ -35,6 +39,8 @@ void v3d_gemfs_init(struct v3d_dev *v3d)
 
  err:
  	v3d->gemfs = NULL;
+	v3d->super_pages = false;
+
  	drm_notice(&v3d->drm,
  		   "Transparent Hugepage support is recommended for optimal performance on this platform!\n");
  }
drivers/gpu/drm/v3d/v3d_mmu.c +7 -0
index 14f3af40d6f6..48a240c44a22 100644
@@ -25,9 +25,16 @@
   * superpage bit set.
   */
  #define V3D_PTE_SUPERPAGE BIT(31)
+#define V3D_PTE_BIGPAGE BIT(30)
  #define V3D_PTE_WRITEABLE BIT(29)
  #define V3D_PTE_VALID BIT(28)
 
+static bool v3d_mmu_is_aligned(u32 page, u32 pte, size_t alignment)
+{
+	return IS_ALIGNED(page, alignment >> V3D_MMU_PAGE_SHIFT) &&
+		IS_ALIGNED(pte, alignment >> V3D_MMU_PAGE_SHIFT);
+}
+
  static int v3d_mmu_flush_all(struct v3d_dev *v3d)
  {
  	int ret;

drivers/gpu/drm/v3d/v3d_mmu.c void v3d_mmu_insert_ptes(struct v3d_bo *bo) · line 96

Quoted 2 lines
drivers/gpu/drm/v3d/v3d_mmu.c +0 -0
@@ -88,18 +95,37 @@ void v3d_mmu_insert_ptes(struct v3d_bo *bo)
  	struct v3d_dev *v3d = to_v3d_dev(shmem_obj->base.dev);
  	u32 page = bo->node.start;

I had some thoughts whether 'page' could have a more descriptive name, so it is clear when reading the code what memory space is what. GPU VA vs DMA PA I mean. But then I figured out bo->node.start is a page frame, not an address, so I don't really now what naming to suggest.

drivers/gpu/drm/v3d/v3d_mmu.c void v3d_mmu_insert_ptes(struct v3d_bo *bo) · line 103

Quoted 10 lines
drivers/gpu/drm/v3d/v3d_mmu.c +4 -3
  	u32 page_prot = V3D_PTE_WRITEABLE | V3D_PTE_VALID;
-	struct sg_dma_page_iter dma_iter;
+	struct scatterlist *sgl;
+	unsigned int count;
 
-	for_each_sgtable_dma_page(shmem_obj->sgt, &dma_iter, 0) {
-		dma_addr_t dma_addr = sg_page_iter_dma_address(&dma_iter);
+	for_each_sgtable_dma_sg(shmem_obj->sgt, sgl, count) {
+		dma_addr_t dma_addr = sg_dma_address(sgl);
  		u32 page_address = dma_addr >> V3D_MMU_PAGE_SHIFT;

For this one I was wondering if variable name such as pfn would be more descriptive. Because address for me suggests byte units and then later when you have page_address++ I get startled. But maybe it is just me and maybe in v3d page_address is already established that can be page frame numbers. Up to you.

drivers/gpu/drm/v3d/v3d_mmu.c void v3d_mmu_insert_ptes(struct v3d_bo *bo) · line 104

Quoted 8 lines
drivers/gpu/drm/v3d/v3d_mmu.c +1 -7
-		u32 pte = page_prot | page_address;
-		u32 i;
-
-		BUG_ON(page_address + (PAGE_SIZE >> V3D_MMU_PAGE_SHIFT) >=
-		       BIT(24));
-		for (i = 0; i < PAGE_SIZE >> V3D_MMU_PAGE_SHIFT; i++)
-			v3d->pt[page++] = pte + i;
+		size_t len = sg_dma_len(sgl);

Sg_dma_len appears to return unsigned int so you may want to align with that, *if* 64-bit builds are a thing for v3d at least.

drivers/gpu/drm/v3d/v3d_mmu.c void v3d_mmu_insert_ptes(struct v3d_bo *bo) · line 105

Quoted 1 line
drivers/gpu/drm/v3d/v3d_mmu.c +1 -0
+		u32 i, page_size = 0;

And then I would make these two unsigned int too, it is just bikeshedding really but if like that then the below "len -= page_size" would be matching types and widths regardless of the 32- vs 64-bit build.

drivers/gpu/drm/v3d/v3d_mmu.c void v3d_mmu_insert_ptes(struct v3d_bo *bo) · line 109

Quoted 4 lines
drivers/gpu/drm/v3d/v3d_mmu.c +4 -0
+
+		while (len > 0) {
+			u32 pte = page_prot | page_address;
+

Page_prot and page_size could be moved to this scope.

drivers/gpu/drm/v3d/v3d_mmu.c void v3d_mmu_insert_ptes(struct v3d_bo *bo) · line 120

Quoted 11 lines
drivers/gpu/drm/v3d/v3d_mmu.c +11 -0
+			BUG_ON(page_address + V3D_PAGE_FACTOR >= BIT(24));
+
+			if (len >= SZ_1M && v3d_mmu_is_aligned(page, pte, SZ_1M)) {
+				page_size = SZ_1M;
+				pte |= V3D_PTE_SUPERPAGE;
+			} else if (len >= SZ_64K && v3d_mmu_is_aligned(page, pte, SZ_64K)) {
+				page_size = SZ_64K;
+				pte |= V3D_PTE_BIGPAGE;
+			} else {
+				page_size = SZ_4K;
+			}

s/pte/page_address/? As it is it works fine, but just because I find it a bit confusing to check the PTE bits for alignment.

Quoted 12 lines
drivers/gpu/drm/v3d/v3d_mmu.c +8 -1
+
+			for (i = 0; i < page_size >> V3D_MMU_PAGE_SHIFT; i++) {
+				v3d->pt[page++] = pte + i;
+				page_address++;
+			}
+
+			len -= page_size;
+		}
  	}
 
  	WARN_ON_ONCE(page - bo->node.start !=
--
Quoted 3 lines

2.44.0

As you can see all my comments are either optional bike sheds or small tweaks we can discuss. The main logic and ideas look solid.

Oh there is one more thing to dicsuss.. strictly speaking you could split this patch into two. First one would just implement the v3d_mmu_insert_ptes and connected changes. Because theoretically some of the new code could be triggered by chance even without the THP/gemfs/alignment restrictions.

Then the last patch in the series would be the one which adds the BO creation and gemfs changes.

Or even three patches since you could split up the BO creation to be standalone, *if* the plan to just use v3d->gemfs as criteria works out, given that v3d->gemfs was added earlier in the series already.

To sum up, 6/6 could be replaced with:

6/8: Support big/super pages when writting out PTEs 7/8: Use gemfs/THP in BO creation if available 8/8: Add modparam for turning off super pages

Regards,

Tvrtko

#10 lore raw
In reply to Maíra Canal

Am 05.04.24 um 20:29 schrieb Maíra Canal:

Quoted 11 lines

This series introduces support for big and super pages in V3D. The V3D MMU has support for 64KB and 1MB pages, called big pages and super pages, which are currently not used. Therefore, this patchset has the intention to enable big and super pages in V3D. The advantage of enabling big and super pages is that if any entry for a page within a big/super page is cached in the MMU, it will be used for the translation of all virtual addresses in the range of that super page without requiring fetching any other entries.

Big/Super pages essentially mean a slightly better performance for users, especially in applications with high memory requirements (e.g. applications that use multiple large BOs).

Nice to see that concept applied more widely.

From amdgpu experience the most beneficial case is usually when you run out of TLB reach, e.g. when your working set is so large than with 4k pages the TLB won't be enough to cover it entirely and you are force to re-fetch entries during execution.

In those case 20% improvement is usually quite realistic.

Regards,
Christian.

Quoted 106 lines

Using a Raspberry Pi 4 (with a PAGE_SIZE=4KB downstream kernel), when running traces from multiple applications, we were able to see the following improvements:

fps_avg  helped:  warzone2100.70secs.1024x768.trace:                    1.98 -> 2.42     (22.19%) (v1: 2.56)
fps_avg  helped:  warzone2100.30secs.1024x768.trace:                    2 -> 2.45        (21.96%) (v1: 2.39)
fps_avg  helped:  supertuxkart-menus_1024x768.trace:                    123.12 -> 128.39 (4.28%)  (v1: 125.50)
fps_avg  helped:  vkQuake_capture_frames_1_through_1200_1280x720.gfxr:  61.26 -> 62.89   (2.67%)  (v1: 61.36)
fps_avg  helped:  quake2-gles3-1280x720.trace:                          63.42 -> 64.86   (2.27%)  (v1: 64.29)
fps_avg  helped:  ue4_sun_temple_640x480.gfxr:                          25.15 -> 25.63   (1.89%)  (v1: 24.94)
fps_avg  helped:  ue4_shooter_game_high_quality_640x480.gfxr:           19.28 -> 19.61   (1.72%)  (v1: 19.02)
fps_avg  helped:  ue4_shooter_game_shooting_high_quality_640x480.gfxr:  23.58 -> 23.91   (1.39%)  (v1: 23.34)
fps_avg  helped:  quake3e_capture_frames_1_through_1800_1920x1080.gfxr: 61.40 -> 62.00   (0.96%)  (v1: -)
fps_avg  helped:  serious_sam_trace02_1280x720.gfxr:                    49.41 -> 49.84   (0.86%)  (v1: 47.74)
fps_avg  helped:  supertuxkart-racing_1024x768.trace:                   8.69 -> 8.74     (0.56%)  (v1: -)
fps_avg  helped:  sponza_demo01_800x600.gfxr:                           17.55 -> 17.64   (0.50%)  (v1: -)
fps_avg  helped:  quake2-gl1.4-1280x720.trace:                          36.43 -> 36.58   (0.43%)  (v1: 36.57)
fps_avg  helped:  quake3e-1280x720.trace:                               94.49 -> 94.87   (0.40%)  (v1: -)
fps_avg  helped:  sponza_demo02_800x600.gfxr:                           19.79 -> 19.87   (0.39%)  (v1: -)

Using a Raspberry Pi 5 (with a PAGE_SIZE=16KB downstream kernel), when running traces from multiple applications, we were able to see the following improvements:

fps_avg  helped:  warzone2100.30secs.1024x768.trace:                       4.40 -> 4.95     (12.58%) (v1: 4.49)
fps_avg  helped:  vkQuake_capture_frames_1_through_1200_1280x720.gfxr:     135.05 -> 141.21 (4.56%)  (v1: 139.45)
fps_avg  helped:  supertuxkart-menus_1024x768.trace:                       291.73 -> 302.05 (3.54%)  (v1: 303.80)
fps_avg  helped:  quake2-gles3-1280x720.trace:                             148.90 -> 153.57 (3.13%)  (v1: 156.13)
fps_avg  helped:  quake2-gl1.4-1280x720.trace:                             82.60 -> 84.46   (2.25%)  (v1: -)
fps_avg  helped:  ue4_shooter_game_shooting_low_quality_640x480.gfxr:      49.59 -> 50.54   (1.92%)  (v1: 47.30)
fps_avg  helped:  ue4_shooter_game_shooting_high_quality_640x480.gfxr:     51.03 -> 51.93   (1.76%)  (v1: 50.46)
fps_avg  helped:  ue4_shooter_game_high_quality_640x480.gfxr:              31.15 -> 31.64   (1.60%)  (v1: 31.05)
fps_avg  helped:  ue4_sun_temple_640x480.gfxr:                             40.26 -> 40.88   (1.54%)  (v1: 40.23)
fps_avg  helped:  sponza_demo01_800x600.gfxr:                              43.23 -> 43.84   (1.40%)  (v1: 43.68)
fps_avg  helped:  warzone2100.70secs.1024x768.trace:                       4.36 -> 4.42     (1.39%)  (v1: -)
fps_avg  helped:  sponza_demo02_800x600.gfxr:                              48.94 -> 49.51   (1.17%)  (v1: 49.34)
fps_avg  helped:  quake3e_capture_frames_1_through_1800_1920x1080.gfxr:    162.11 -> 163.13 (0.63%)  (v1: 165.71)
fps_avg  helped:  quake3e-1280x720.trace:                                  229.82 -> 231.00 (0.51%)  (v1: 234.51)
fps_avg  helped:  supertuxkart-racing_1024x768.trace:                      20.42 -> 20.48   (0.30%)  (v1: 20.59)
fps_avg  helped:  quake3e_capture_frames_1800_through_2400_1920x1080.gfxr: 157.45 -> 157.61 (0.10%)  (v1: 160.35)
fps_avg  helped:  serious_sam_trace02_1280x720.gfxr:                       59.99 -> 60.02   (0.05%)  (v1: -)

When glancing at the percentage improvement, one might initially perceive v2 as causing a performance downgrade. However, that assumption doesn't hold true. In the case of v1, we were using downstream kernel version 6.1, whereas now, with the kernel updated to 6.6 for v2, there's a small uptick in performance. This indicates an enhancement in the baseline scenario, rather than any detriment caused by v2. Additionally, I've included stats from v1 in the comparisons. Upon scrutinizing the average FPS of v2 in contrast to v1, it becomes evident that v2 not only maintains the improvements but may even surpass them.

This version provides a much safer way to iterate through memory and doesn't hold to the same limitations as v1. For example, v1 had a hard-coded hack that only allowed a huge page to be created if the BO was bigger than 2MB. These limitations are gone now.

This series also introduces changes in the GEM helpers, in order to enable V3D to have a separate mount point for shmfs GEM objects. Any feedback from the community about the changes in the GEM helpers is welcomed!

v1 -> v2: https://lore.kernel.org/dri-devel/[email protected]/

* [1/6] Add Iago's R-b to PATCH 1/5 (Iago Toral)
* [2/6] Create a new function drm_gem_object_init_with_mnt() to define the shmfs mountpoint. Now, we don't touch a bunch of drivers, as drm_gem_object_init() preserves its signature (Tvrtko Ursulin)
* [3/6] Use huge=within_size instead of huge=always, in order to avoid OOM. This also allow us to move away from the 2MB hack. (Tvrtko Ursulin)
* [3/6] Add Iago's R-b to PATCH 3/5 (Iago Toral)
* [5/6] Create a separate patch to reduce the alignment of the node allocation (Iago Toral)
* [6/6] Complete refactoring to the way that we iterate through the memory (Tvrtko Ursulin)
* [6/6] Don't use drm_prime_get_contiguous_size(), as it could give us misleading data (Tvrtko Ursulin)
* [6/6] Use both Big Pages (64K) and Super Pages (1MB)

Best Regards,
- Maíra

Maíra Canal (6):
drm/v3d: Fix return if scheduler initialization fails drm/gem: Create a drm_gem_object_init_with_mnt() function drm/v3d: Introduce gemfs
drm/gem: Create shmem GEM object in a given mountpoint drm/v3d: Reduce the alignment of the node allocation drm/v3d: Enable big and super pages

  drivers/gpu/drm/drm_gem.c              | 34 +++++++++++++++--
  drivers/gpu/drm/drm_gem_shmem_helper.c | 30 +++++++++++++--
  drivers/gpu/drm/v3d/Makefile           |  3 +-
  drivers/gpu/drm/v3d/v3d_bo.c           | 21 ++++++++++-
  drivers/gpu/drm/v3d/v3d_drv.c          |  8 ++++
  drivers/gpu/drm/v3d/v3d_drv.h          | 13 ++++++-
  drivers/gpu/drm/v3d/v3d_gem.c          |  6 ++-
  drivers/gpu/drm/v3d/v3d_gemfs.c        | 52 ++++++++++++++++++++++++++
  drivers/gpu/drm/v3d/v3d_mmu.c          | 46 ++++++++++++++++++-----
  include/drm/drm_gem.h                  |  3 ++
  include/drm/drm_gem_shmem_helper.h     |  3 ++
  11 files changed, 195 insertions(+), 24 deletions(-)

create mode 100644 drivers/gpu/drm/v3d/v3d_gemfs.c

--
2.44.0