Skip to contentSkip to conversation

drm/v3d: Enable Big and Super Pages

PATCH In review Maíra Canal 2 participants 15 messages Sep 23, 2024 – Sep 25, 2024

Subsystems (3)
lore
Share & tools
lore

Maíra Canal

author maintainer
#0 lore raw

v6 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.30secs.1024x768.trace:                       56.39 -> 61.10 (8.36%)
fps_avg  helped:  ue4_shooter_game_shooting_low_quality_640x480.gfxr:      20.71 -> 21.47 (3.65%)
fps_avg  helped:  quake3e_capture_frames_1800_through_2400_1920x1080.gfxr: 60.88 -> 62.50 (2.67%)
fps_avg  helped:  supertuxkart-menus_1024x768.trace:                       112.62 -> 115.61 (2.65%)
fps_avg  helped:  ue4_shooter_game_shooting_high_quality_640x480.gfxr:     20.45 -> 20.88 (2.10%)
fps_avg  helped:  quake2-gles3-1280x720.trace:                             59.76 -> 60.84 (1.82%)
fps_avg  helped:  ue4_sun_temple_640x480.gfxr:                             27.60 -> 28.03 (1.54%)
fps_avg  helped:  vkQuake_capture_frames_1_through_1200_1280x720.gfxr:     54.59 -> 55.30 (1.29%)
fps_avg  helped:  ue4_shooter_game_low_quality_640x480.gfxr:               32.75 -> 33.08 (1.00%)
fps_avg  helped:  sponza_demo02_800x600.gfxr:                              20.90 -> 21.03 (0.61%)
fps_avg  helped:  supertuxkart-racing_1024x768.trace:                      8.58 -> 8.63 (0.60%)
fps_avg  helped:  ue4_shooter_game_high_quality_640x480.gfxr:              19.62 -> 19.74 (0.59%)
fps_avg  helped:  serious_sam_trace02_1280x720.gfxr:                       44.00 -> 44.21 (0.50%)
fps_avg  helped:  ue4_vehicle_game-2_640x480.gfxr:                         12.59 -> 12.65 (0.49%)
fps_avg  helped:  sponza_demo01_800x600.gfxr:                              21.42 -> 21.46 (0.19%)
fps_avg  helped:  quake3e-1280x720.trace:                                  84.45 -> 84.52 (0.09%)

total fps_avg in all runs: 845.43 -> 857.29 (1.40%)

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:  quake2-gles3-1280x720.trace:                             151.77 -> 157.26 (3.62%)
fps_avg  helped:  supertuxkart-menus_1024x768.trace:                       306.79 -> 313.88 (2.31%)
fps_avg  helped:  warzone2100.30secs.1024x768.trace:                       140.92 -> 144.03 (2.21%)
fps_avg  helped:  vkQuake_capture_frames_1_through_1200_1280x720.gfxr:     131.45 -> 134.20 (2.10%)
fps_avg  helped:  ue4_vehicle_game-2_640x480.gfxr:                         24.42 -> 24.88 (1.89%)
fps_avg  helped:  ue4_shooter_game_high_quality_640x480.gfxr:              32.12 -> 32.53 (1.29%)
fps_avg  helped:  ue4_sun_temple_640x480.gfxr:                             42.05 -> 42.55 (1.20%)
fps_avg  helped:  ue4_shooter_game_shooting_high_quality_640x480.gfxr:     52.77 -> 53.31 (1.04%)
fps_avg  helped:  quake3e-1280x720.trace:                                  238.31 -> 240.53 (0.93%)
fps_avg  helped:  warzone2100.70secs.1024x768.trace:                       151.09 -> 151.81 (0.48%)
fps_avg  helped:  sponza_demo02_800x600.gfxr:                              50.81 -> 51.05 (0.46%)
fps_avg  helped:  supertuxkart-racing_1024x768.trace:                      20.91 -> 20.98 (0.33%)
fps_avg  helped:  ue4_shooter_game_low_quality_640x480.gfxr:               59.68 -> 59.86 (0.29%)
fps_avg  helped:  quake3e_capture_frames_1_through_1800_1920x1080.gfxr:    167.70 -> 168.17 (0.29%)
fps_avg  helped:  ue4_shooter_game_shooting_low_quality_640x480.gfxr:      53.40 -> 53.51 (0.22%)
fps_avg  helped:  quake3e_capture_frames_1800_through_2400_1920x1080.gfxr: 163.37 -> 163.64 (0.17%)
fps_avg  helped:  serious_sam_trace02_1280x720.gfxr:                       60.00 -> 60.03 (0.06%)
fps_avg  helped:  sponza_demo01_800x600.gfxr:                              45.04 -> 45.04 (<.01%)

total fps_avg in all runs: 1892.60 -> 1917.26 (1.30%)

We also could attest to some improvement when running CTS and Piglit tests on Mesa CI. For example, the job v3dv-rpi5-vk-full:arm64 usually takes around 100 minutes to run. After running Super Pages kernels on Mesa CI several times, we can see that this job duration reduces to 90 minutes [1]. Another example is the job v3dv-rpi4-vk-full:arm64 */6, which has its duration reduced from 44 minutes to around 42 minutes [1].

This time, the benchmark results were obtained using CONFIG_TRANSPARENT_HUGEPAGE_MADVISE instead of CONFIG_TRANSPARENT_HUGEPAGE_ALWAYS. CONFIG_TRANSPARENT_HUGEPAGE_ALWAYS=y can sometimes hurt the overall performance of embedded systems. As mentioned by [2], "Embedded systems should enable hugepages only inside madvise regions to eliminate any risk of wasting any precious byte of memory and to only run faster."

We were able to attest through benchmarks that CONFIG_TRANSPARENT_HUGEPAGE_MADVISE improves the overall performance of 3D applications (using Super Pages) when compared to CONFIG_TRANSPARENT_HUGEPAGE_ALWAYS.

This version didn't add any new features to the series. It only addresses comments from the reviewers. That said, I added one more patch to expose the Super Pages capability to user-space through V3D parameters.

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!

[1] https://gitlab.freedesktop.org/mairacanal/mesa/-/pipelines/1276368/
[2] https://docs.kernel.org/admin-guide/mm/transhuge.html

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)

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

* [2/8] Add Tvrtko's R-b to PATCH 2/8 (Tvrtko Ursulin)
* [4/8] Add Tvrtko's R-b to PATCH 4/8 (Tvrtko Ursulin)
* [6/8] Now, PATCH 6/8 regards supporting big/super pages when writing out PTEs (Tvrtko Ursulin)
* [6/8] s/page_address/pfn (Tvrtko Ursulin)
* [6/8] As sg_dma_len() returns unsigned int, then len must be unsigned int too (Tvrtko Ursulin)
* [6/8] i and page_size are unsigned int as well (Tvrtko Ursulin)
* [6/8] Move i, page_prot and page_size to the inner scope (Tvrtko Ursulin)
* [6/8] s/pte/page_address/ (Tvrtko Ursulin)
* [7/8] New patch: Use gemfs/THP in BO creation if available
* [8/8] New patch: Add modparam for turning off Big/Super Pages
* [8/8] Don't expose the modparam super_pages unless CONFIG_TRANSPARENT_HUGEPAGE is enabled (Tvrtko Ursulin)
* [8/8] Use v3d->gemfs to check if the user disabled Super Pages support (Tvrtko Ursulin)

v3 -> v4: https://lore.kernel.org/dri-devel/[email protected]/T/

* [5/8] Add Iago's R-b to PATCH 5/8 (Iago Toral)
* [6/8] Add Tvrtko's R-b to PATCH 6/8 (Tvrtko Ursulin)
* [7/8] Add Tvrtko's R-b to PATCH 7/8 (Tvrtko Ursulin)
* [8/8] Move bool super_pages to the guard (Tvrtko Ursulin)

v4 -> v5: https://lore.kernel.org/dri-devel/[email protected]/T/

* [1/10] New patch: Fix the MMU flush order
* [2/10] New patch: Flush the MMU before we supply more memory to the binner
* [10/10] Add Tvrtko's R-b to PATCH 10/10 (Tvrtko Ursulin)

v5 -> v6: https://lore.kernel.org/dri-devel/[email protected]/T/

* [1/11] Improve commit message by describing the change that we are now waiting for each flush to complete before starting the other flush (Iago Toral)
* [1/11] Change error message to "MMU TLB clear wait idle failed" (Iago Toral)
* [2/11] Add Iago's R-b to PATCH 2/11 (Iago Toral)
* [11/11] New patch: Expose Super Pages capability

Best Regards,
- Maíra

Maíra Canal (11):
drm/v3d: Address race-condition in MMU flush
drm/v3d: Flush the MMU before we supply more memory to the binner 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: Support Big/Super Pages when writing out PTEs drm/v3d: Use gemfs/THP in BO creation if available drm/v3d: Add modparam for turning off Big/Super Pages drm/v3d: Expose Super Pages capability

 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          | 10 +++
 drivers/gpu/drm/v3d/v3d_drv.h          | 14 ++++-
 drivers/gpu/drm/v3d/v3d_gem.c          |  6 +-
 drivers/gpu/drm/v3d/v3d_gemfs.c        | 50 +++++++++++++++
 drivers/gpu/drm/v3d/v3d_irq.c          |  2 +
 drivers/gpu/drm/v3d/v3d_mmu.c          | 85 ++++++++++++++++----------
 include/drm/drm_gem.h                  |  3 +
 include/drm/drm_gem_shmem_helper.h     |  3 +
 include/uapi/drm/v3d_drm.h             |  1 +
 13 files changed, 215 insertions(+), 47 deletions(-)
 create mode 100644 drivers/gpu/drm/v3d/v3d_gemfs.c
Signature · 1 line
2.46.0
author maintainer
#1 lore raw

PATCH v6 1/11 drm/v3d: Address race-condition in MMU flush

We must first flush the MMU cache and then, flush the TLB, not the other way around. Currently, we can see a race condition between the MMU cache and the TLB when running multiple rendering processes at the same time. This is evidenced by MMU errors triggered by the IRQ.

Fix the MMU flush order by flushing the MMU cache and then the TLB. Also, in order to address the race condition, wait for the MMU cache flush to finish before starting the TLB flush.

Fixes 57692c94dcbe ("drm/v3d: Introduce a new DRM driver for Broadcom V3D V3.x+")
---
 drivers/gpu/drm/v3d/v3d_mmu.c | 29 ++++++++++-------------------
 1 file changed, 10 insertions(+), 19 deletions(-)
drivers/gpu/drm/v3d/v3d_mmu.c +10 -19
index 14f3af40d6f6..e36ec3343b06 100644
@@ -32,32 +32,23 @@ static int v3d_mmu_flush_all(struct v3d_dev *v3d)
 {
 	int ret;
 
-	/* Make sure that another flush isn't already running when we
-	 * start this one.
-	 */
-	ret = wait_for(!(V3D_READ(V3D_MMU_CTL) &
-			 V3D_MMU_CTL_TLB_CLEARING), 100);
-	if (ret)
-		dev_err(v3d->drm.dev, "TLB clear wait idle pre-wait failed\n");
-
-	V3D_WRITE(V3D_MMU_CTL, V3D_READ(V3D_MMU_CTL) |
-		  V3D_MMU_CTL_TLB_CLEAR);
-
-	V3D_WRITE(V3D_MMUC_CONTROL,
-		  V3D_MMUC_CONTROL_FLUSH |
+	V3D_WRITE(V3D_MMUC_CONTROL, V3D_MMUC_CONTROL_FLUSH |
 		  V3D_MMUC_CONTROL_ENABLE);
 
-	ret = wait_for(!(V3D_READ(V3D_MMU_CTL) &
-			 V3D_MMU_CTL_TLB_CLEARING), 100);
+	ret = wait_for(!(V3D_READ(V3D_MMUC_CONTROL) &
+			 V3D_MMUC_CONTROL_FLUSHING), 100);
 	if (ret) {
-		dev_err(v3d->drm.dev, "TLB clear wait idle failed\n");
+		dev_err(v3d->drm.dev, "MMUC flush wait idle failed\n");
 		return ret;
 	}
 
-	ret = wait_for(!(V3D_READ(V3D_MMUC_CONTROL) &
-			 V3D_MMUC_CONTROL_FLUSHING), 100);
+	V3D_WRITE(V3D_MMU_CTL, V3D_READ(V3D_MMU_CTL) |
+		  V3D_MMU_CTL_TLB_CLEAR);
+
+	ret = wait_for(!(V3D_READ(V3D_MMU_CTL) &
+			 V3D_MMU_CTL_TLB_CLEARING), 100);
 	if (ret)
-		dev_err(v3d->drm.dev, "MMUC flush wait idle failed\n");
+		dev_err(v3d->drm.dev, "MMU TLB clear wait idle failed\n");
 
 	return ret;
 }
Signature · 1 line
2.46.0
Reviewed-by on v6 1/11
#2 lore raw

Re: [PATCH v6 01/11] drm/v3d: Address race-condition in MMU flush

Quoted 74 lines · Maíra Canal

We must first flush the MMU cache and then, flush the TLB, not the other
way around. Currently, we can see a race condition between the MMU cache
and the TLB when running multiple rendering processes at the same time.
This is evidenced by MMU errors triggered by the IRQ.

Fix the MMU flush order by flushing the MMU cache and then the TLB. Also, in order to address the race condition, wait for the MMU cache flush
to finish before starting the TLB flush.

Fixes 57692c94dcbe ("drm/v3d: Introduce a new DRM driver for

Broadcom V3D V3.x+")

---
 drivers/gpu/drm/v3d/v3d_mmu.c | 29 ++++++++++-------------------

 1 file changed, 10 insertions(+), 19 deletions(-)
drivers/gpu/drm/v3d/v3d_mmu.c +0 -0
index 14f3af40d6f6..e36ec3343b06 100644
@@ -32,32 +32,23 @@ static int v3d_mmu_flush_all(struct v3d_dev *v3d)
  {
  	int ret;

drivers/gpu/drm/v3d/v3d_mmu.c +0 -1
-	/* Make sure that another flush isn't already running when

we

drivers/gpu/drm/v3d/v3d_mmu.c +0 -6
-	 * start this one.
-	 */
-	ret = wait_for(!(V3D_READ(V3D_MMU_CTL) &
-			 V3D_MMU_CTL_TLB_CLEARING), 100);
-	if (ret)
-		dev_err(v3d->drm.dev, "TLB clear wait idle pre-wait

failed\n");

drivers/gpu/drm/v3d/v3d_mmu.c +1 -6
-
-	V3D_WRITE(V3D_MMU_CTL, V3D_READ(V3D_MMU_CTL) |
-		  V3D_MMU_CTL_TLB_CLEAR);
-
-	V3D_WRITE(V3D_MMUC_CONTROL,
-		  V3D_MMUC_CONTROL_FLUSH |
+	V3D_WRITE(V3D_MMUC_CONTROL, V3D_MMUC_CONTROL_FLUSH |

    V3D_MMUC_CONTROL_ENABLE);

drivers/gpu/drm/v3d/v3d_mmu.c +2 -2
-	ret = wait_for(!(V3D_READ(V3D_MMU_CTL) &
-			 V3D_MMU_CTL_TLB_CLEARING), 100);
+	ret = wait_for(!(V3D_READ(V3D_MMUC_CONTROL) &
+			 V3D_MMUC_CONTROL_FLUSHING), 100);

  if (ret) {

drivers/gpu/drm/v3d/v3d_mmu.c +0 -1
-		dev_err(v3d->drm.dev, "TLB clear wait idle

failed\n");

drivers/gpu/drm/v3d/v3d_mmu.c +1 -0
+		dev_err(v3d->drm.dev, "MMUC flush wait idle

failed\n");
  return ret;
  }

drivers/gpu/drm/v3d/v3d_mmu.c +5 -2
-	ret = wait_for(!(V3D_READ(V3D_MMUC_CONTROL) &
-			 V3D_MMUC_CONTROL_FLUSHING), 100);
+	V3D_WRITE(V3D_MMU_CTL, V3D_READ(V3D_MMU_CTL) |
+		  V3D_MMU_CTL_TLB_CLEAR);
+
+	ret = wait_for(!(V3D_READ(V3D_MMU_CTL) &
+			 V3D_MMU_CTL_TLB_CLEARING), 100);

  if (ret)

drivers/gpu/drm/v3d/v3d_mmu.c +0 -1
-		dev_err(v3d->drm.dev, "MMUC flush wait idle

failed\n");

drivers/gpu/drm/v3d/v3d_mmu.c +1 -0
+		dev_err(v3d->drm.dev, "MMU TLB clear wait idle

failed\n");

  return ret;
 }

author maintainer
#3 lore raw

PATCH v6 2/11 drm/v3d: Flush the MMU before we supply more memory to the binner

In reply to Maíra Canal

We must ensure that the MMU is flushed before we supply more memory to the binner, otherwise we might end up with invalid MMU accesses by the GPU.

Fixes 57692c94dcbe ("drm/v3d: Introduce a new DRM driver for Broadcom V3D V3.x+")
---
 drivers/gpu/drm/v3d/v3d_drv.h | 1 +
 drivers/gpu/drm/v3d/v3d_irq.c | 2 ++
 drivers/gpu/drm/v3d/v3d_mmu.c | 2 +-
 3 files changed, 4 insertions(+), 1 deletion(-)
drivers/gpu/drm/v3d/v3d_drv.h +1 -0
index cf4b23369dc4..75b4725d49c7 100644
@@ -553,6 +553,7 @@ void v3d_irq_disable(struct v3d_dev *v3d);
 void v3d_irq_reset(struct v3d_dev *v3d);
 
 /* v3d_mmu.c */
+int v3d_mmu_flush_all(struct v3d_dev *v3d);
 int v3d_mmu_set_page_table(struct v3d_dev *v3d);
 void v3d_mmu_insert_ptes(struct v3d_bo *bo);
 void v3d_mmu_remove_ptes(struct v3d_bo *bo);
drivers/gpu/drm/v3d/v3d_irq.c +2 -0
index d469bda52c1a..20bf33702c3c 100644
@@ -70,6 +70,8 @@ v3d_overflow_mem_work(struct work_struct *work)
 	list_add_tail(&bo->unref_head, &v3d->bin_job->render->unref_list);
 	spin_unlock_irqrestore(&v3d->job_lock, irqflags);
 
+	v3d_mmu_flush_all(v3d);
+
 	V3D_CORE_WRITE(0, V3D_PTB_BPOA, bo->node.start << V3D_MMU_PAGE_SHIFT);
 	V3D_CORE_WRITE(0, V3D_PTB_BPOS, obj->size);
 
drivers/gpu/drm/v3d/v3d_mmu.c +1 -1
index e36ec3343b06..5bb7821c0243 100644
@@ -28,7 +28,7 @@
 #define V3D_PTE_WRITEABLE BIT(29)
 #define V3D_PTE_VALID BIT(28)
 
-static int v3d_mmu_flush_all(struct v3d_dev *v3d)
+int v3d_mmu_flush_all(struct v3d_dev *v3d)
 {
 	int ret;
 
Signature · 1 line
2.46.0
author maintainer
#4 lore raw

PATCH v6 3/11 drm/v3d: Fix return if scheduler initialization fails

In reply to Maíra Canal

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 da8faf3b9011..b3b76332f2c5 100644
@@ -291,8 +291,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.46.0
author maintainer
#5 lore raw

PATCH v6 4/11 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 149b8e25da5b..ee811764c3df 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);
Signature · 1 line
2.46.0
author maintainer
#6 lore raw

PATCH v6 5/11 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 75b4725d49c7..41b09c828848 100644
@@ -137,6 +137,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;
@@ -534,6 +539,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 b3b76332f2c5..b1e681630ded 100644
@@ -288,6 +288,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);
@@ -305,6 +307,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.46.0
author maintainer
#7 lore raw

PATCH v6 6/11 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 53c003983ad1..8508060a1a95 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
@@ -765,7 +787,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.46.0
author maintainer
#8 lore raw

PATCH v6 7/11 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 ecb80fd75b1a..427a1c405b19 100644
@@ -123,7 +123,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 41b09c828848..82d6e869dd41 100644
@@ -19,8 +19,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.46.0
author maintainer
#9 lore raw

PATCH v6 8/11 drm/v3d: Support Big/Super Pages when writing out PTEs

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_drv.h |  1 +
 drivers/gpu/drm/v3d/v3d_mmu.c | 54 ++++++++++++++++++++++++++---------
 2 files changed, 42 insertions(+), 13 deletions(-)
drivers/gpu/drm/v3d/v3d_drv.h +1 -0
index 82d6e869dd41..a98543290fa8 100644
@@ -20,6 +20,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_mmu.c +41 -13

69 lines not on this page · raw message

Signature · 1 line
2.46.0
author maintainer
#10 lore raw

PATCH v6 9/11 drm/v3d: Use gemfs/THP in BO creation if available

In reply to Maíra Canal

Although Big/Super Pages could appear naturally, it would be quite hard to have 1MB or 64KB allocated contiguously naturally. Therefore, we can force the creation of large pages allocated contiguously by using a mountpoint with "huge=within_size" enabled.

Therefore, as V3D has a mountpoint with "huge=within_size" (if user has THP enabled), use this mountpoint for BO creation if available. This will allow us to create large pages allocated contiguously and make use of Big/Super Pages.

---
 drivers/gpu/drm/v3d/v3d_bo.c | 21 +++++++++++++++++++--
 1 file changed, 19 insertions(+), 2 deletions(-)
drivers/gpu/drm/v3d/v3d_bo.c +19 -2
index 427a1c405b19..cd85f997b058 100644
@@ -107,6 +107,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
@@ -116,6 +117,15 @@ v3d_bo_create_finish(struct drm_gem_object *obj)
 	if (IS_ERR(sgt))
 		return PTR_ERR(sgt);
 
+	if (!v3d->gemfs)
+		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
@@ -123,7 +133,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;
@@ -143,10 +153,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->gemfs)
+		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);
Signature · 1 line
2.46.0
author maintainer
#11 lore raw

PATCH v6 10/11 drm/v3d: Add modparam for turning off Big/Super Pages

In reply to Maíra Canal

Add a modparam for turning off Big/Super Pages to make sure that if an user doesn't want Big/Super Pages enabled, it can disabled it by setting the modparam to false.

---
 drivers/gpu/drm/v3d/v3d_drv.c   | 7 +++++++
 drivers/gpu/drm/v3d/v3d_drv.h   | 1 +
 drivers/gpu/drm/v3d/v3d_gemfs.c | 4 ++++
 3 files changed, 12 insertions(+)
drivers/gpu/drm/v3d/v3d_drv.c +7 -0
index d7ff1f5fa481..8be8c6dd9b35 100644
@@ -36,6 +36,13 @@
 #define DRIVER_MINOR 0
 #define DRIVER_PATCHLEVEL 0
 
+/* Only expose the `super_pages` modparam if THP is enabled. */
+#ifdef CONFIG_TRANSPARENT_HUGEPAGE
+bool super_pages = true;
+module_param_named(super_pages, super_pages, bool, 0400);
+MODULE_PARM_DESC(super_pages, "Enable/Disable Super Pages support.");
+#endif
+
 static int v3d_get_param_ioctl(struct drm_device *dev, void *data,
 			       struct drm_file *file_priv)
 {
drivers/gpu/drm/v3d/v3d_drv.h +1 -0
index a98543290fa8..de73eefff9ac 100644
@@ -539,6 +539,7 @@ void v3d_invalidate_caches(struct v3d_dev *v3d);
 void v3d_clean_caches(struct v3d_dev *v3d);
 
 /* v3d_gemfs.c */
+extern bool super_pages;
 void v3d_gemfs_init(struct v3d_dev *v3d);
 void v3d_gemfs_fini(struct v3d_dev *v3d);
 
drivers/gpu/drm/v3d/v3d_gemfs.c +4 -0
index 31cf5bd11e39..4c5e18590a5c 100644
@@ -20,6 +20,10 @@ void v3d_gemfs_init(struct v3d_dev *v3d)
 	if (!IS_ENABLED(CONFIG_TRANSPARENT_HUGEPAGE))
 		goto err;
 
+	/* The user doesn't want to enable Super Pages */
+	if (!super_pages)
+		goto err;
+
 	type = get_fs_type("tmpfs");
 	if (!type)
 		goto err;
Signature · 1 line
2.46.0
author maintainer
#12 lore raw

PATCH v6 11/11 drm/v3d: Expose Super Pages capability

In reply to Maíra Canal

Add a new V3D parameter to expose the support of Super Pages to userspace. The userspace might want to know this information to apply optimizations that are specific to kernels with Super Pages enabled.

---
 drivers/gpu/drm/v3d/v3d_drv.c | 3 +++
 include/uapi/drm/v3d_drm.h    | 1 +
 2 files changed, 4 insertions(+)
drivers/gpu/drm/v3d/v3d_drv.c +3 -0
index 8be8c6dd9b35..fb35c5c3f1a7 100644
@@ -104,6 +104,9 @@ static int v3d_get_param_ioctl(struct drm_device *dev, void *data,
 	case DRM_V3D_PARAM_MAX_PERF_COUNTERS:
 		args->value = v3d->perfmon_info.max_counters;
 		return 0;
+	case DRM_V3D_PARAM_SUPPORTS_SUPER_PAGES:
+		args->value = !!v3d->gemfs;
+		return 0;
 	default:
 		DRM_DEBUG("Unknown parameter %d\n", args->param);
 		return -EINVAL;
include/uapi/drm/v3d_drm.h +1 -0
index 87fc5bb0a61e..2376c73abca1 100644
@@ -290,6 +290,7 @@ enum drm_v3d_param {
 	DRM_V3D_PARAM_SUPPORTS_MULTISYNC_EXT,
 	DRM_V3D_PARAM_SUPPORTS_CPU_QUEUE,
 	DRM_V3D_PARAM_MAX_PERF_COUNTERS,
+	DRM_V3D_PARAM_SUPPORTS_SUPER_PAGES,
 };
 
 struct drm_v3d_get_param {
Signature · 1 line
2.46.0
Reviewed-by on v6 11/11
#13 lore raw

Re: [PATCH v6 11/11] drm/v3d: Expose Super Pages capability

Quoted 39 lines · Maíra Canal

Add a new V3D parameter to expose the support of Super Pages to userspace. The userspace might want to know this information to apply optimizations that are specific to kernels with Super Pages enabled.

---
 drivers/gpu/drm/v3d/v3d_drv.c | 3 +++
 include/uapi/drm/v3d_drm.h    | 1 +

 2 files changed, 4 insertions(+)
drivers/gpu/drm/v3d/v3d_drv.c +0 -0
index 8be8c6dd9b35..fb35c5c3f1a7 100644
@@ -104,6 +104,9 @@ static int v3d_get_param_ioctl(struct drm_device
 *dev, void *data,

  case DRM_V3D_PARAM_MAX_PERF_COUNTERS:
  args->value = v3d->perfmon_info.max_counters; return 0;

drivers/gpu/drm/v3d/v3d_drv.c +3 -0
+	case DRM_V3D_PARAM_SUPPORTS_SUPER_PAGES:
+		args->value = !!v3d->gemfs;
+		return 0;

  default:
  DRM_DEBUG("Unknown parameter %d\n", args->param); return -EINVAL;

include/uapi/drm/v3d_drm.h +1 -0
index 87fc5bb0a61e..2376c73abca1 100644
@@ -290,6 +290,7 @@ enum drm_v3d_param {
  	DRM_V3D_PARAM_SUPPORTS_MULTISYNC_EXT,
  	DRM_V3D_PARAM_SUPPORTS_CPU_QUEUE,
  	DRM_V3D_PARAM_MAX_PERF_COUNTERS,
+	DRM_V3D_PARAM_SUPPORTS_SUPER_PAGES,
  };


 struct drm_v3d_get_param {

author maintainer
#14 lore raw

Re: [PATCH v6 11/11] drm/v3d: Expose Super Pages capability

Hi Iago,

Quoted 1 line · Iago Toral

I just applied to misc/kernel.git (drm-misc-next).

Thank you and Tvrtko for all the help during the reviewing process!

Best Regards,
- Maíra

Quoted 42 lines · Maíra Canal
Quoted 39 lines

Add a new V3D parameter to expose the support of Super Pages to userspace. The userspace might want to know this information to apply optimizations that are specific to kernels with Super Pages enabled.

---
  drivers/gpu/drm/v3d/v3d_drv.c | 3 +++
  include/uapi/drm/v3d_drm.h    | 1 +
  2 files changed, 4 insertions(+)
drivers/gpu/drm/v3d/v3d_drv.c +3 -0
index 8be8c6dd9b35..fb35c5c3f1a7 100644
@@ -104,6 +104,9 @@ static int v3d_get_param_ioctl(struct drm_device
 *dev, void *data,
  	case DRM_V3D_PARAM_MAX_PERF_COUNTERS:
  		args->value = v3d->perfmon_info.max_counters;
  		return 0;
+	case DRM_V3D_PARAM_SUPPORTS_SUPER_PAGES:
+		args->value = !!v3d->gemfs;
+		return 0;
  	default:
  		DRM_DEBUG("Unknown parameter %d\n", args->param);

  return -EINVAL;

include/uapi/drm/v3d_drm.h +1 -0
index 87fc5bb0a61e..2376c73abca1 100644
@@ -290,6 +290,7 @@ enum drm_v3d_param {
  	DRM_V3D_PARAM_SUPPORTS_MULTISYNC_EXT,
  	DRM_V3D_PARAM_SUPPORTS_CPU_QUEUE,
  	DRM_V3D_PARAM_MAX_PERF_COUNTERS,
+	DRM_V3D_PARAM_SUPPORTS_SUPER_PAGES,
  };
 
  struct drm_v3d_get_param {