Skip to contentSkip to conversation

Use hmm_range_fault to populate user page

PATCH In review Oak Zeng 5 participants 49 messages Mar 14, 2024 – Mar 19, 2024

Subsystems (2)
lore
Share & tools
lore

Oak Zeng

author
#0 lore raw

cover Use hmm_range_fault to populate user page

This is an effort to unify hmmptr (system allocator) and userptr. A helper xe_hmm_populate_range is created to populate a user page using hmm_range_fault, instead of using get_user_pages_fast. This helper is then used to replace some userptr codes.

The same help will be used later for hmmptr.

This is part of the hmmptr (system allocator) codes. Since this part can be merged separately, send it out for CI and review first. It will be followed by other hmmptr codes.

Oak Zeng (5):
drm/xe/svm: Remap and provide memmap backing for GPU vram drm/xe: Helper to get memory region from tile drm/xe: Helper to get dpa from pfn
drm/xe: Helper to populate a userptr or hmmptr drm/xe: Use hmm_range_fault to populate user pages

 drivers/gpu/drm/xe/Makefile          |   4 +-
 drivers/gpu/drm/xe/xe_device_types.h |  22 +++
 drivers/gpu/drm/xe/xe_hmm.c          | 213 +++++++++++++++++++++++++++
 drivers/gpu/drm/xe/xe_hmm.h          |  12 ++
 drivers/gpu/drm/xe/xe_mmio.c         |   8 +
 drivers/gpu/drm/xe/xe_svm.h          |  14 ++
 drivers/gpu/drm/xe/xe_svm_devmem.c   |  91 ++++++++++++
 drivers/gpu/drm/xe/xe_vm.c           | 105 +------------
 8 files changed, 367 insertions(+), 102 deletions(-)
 create mode 100644 drivers/gpu/drm/xe/xe_hmm.c
 create mode 100644 drivers/gpu/drm/xe/xe_hmm.h
 create mode 100644 drivers/gpu/drm/xe/xe_svm.h
 create mode 100644 drivers/gpu/drm/xe/xe_svm_devmem.c
Signature · 1 line
2.26.3
author
#1 lore raw

PATCH 1/5 drm/xe/svm: Remap and provide memmap backing for GPU vram

Memory remap GPU vram using devm_memremap_pages, so each GPU vram page is backed by a struct page.

Those struct pages are created to allow hmm migrate buffer b/t GPU vram and CPU system memory using existing Linux migration mechanism (i.e., migrating b/t CPU system memory and hard disk).

This is prepare work to enable svm (shared virtual memory) through Linux kernel hmm framework. The memory remap's page map type is set to MEMORY_DEVICE_PRIVATE for now. This means even though each GPU vram page get a struct page and can be mapped in CPU page table, but such pages are treated as GPU's private resource, so CPU can't access them. If CPU access such page, a page fault is triggered and page will be migrate to system memory.

For GPU device which supports coherent memory protocol b/t CPU and GPU (such as CXL and CAPI protocol), we can remap device memory as MEMORY_DEVICE_COHERENT. This is TBD.

---
 drivers/gpu/drm/xe/Makefile          |  3 +-
 drivers/gpu/drm/xe/xe_device_types.h |  9 +++
 drivers/gpu/drm/xe/xe_mmio.c         |  8 +++
 drivers/gpu/drm/xe/xe_svm.h          | 14 +++++
 drivers/gpu/drm/xe/xe_svm_devmem.c   | 91 ++++++++++++++++++++++++++++
 5 files changed, 124 insertions(+), 1 deletion(-)
 create mode 100644 drivers/gpu/drm/xe/xe_svm.h
 create mode 100644 drivers/gpu/drm/xe/xe_svm_devmem.c
drivers/gpu/drm/xe/Makefile +2 -1
index c531210695db..840467080e59 100644
@@ -142,7 +142,8 @@ xe-y += xe_bb.o \
 	xe_vram_freq.o \
 	xe_wait_user_fence.o \
 	xe_wa.o \
-	xe_wopcm.o
+	xe_wopcm.o \
+	xe_svm_devmem.o
 
 # graphics hardware monitoring (HWMON) support
 xe-$(CONFIG_HWMON) += xe_hwmon.o
drivers/gpu/drm/xe/xe_device_types.h +9 -0
index 9785eef2e5a4..f27c3bee8ce7 100644
@@ -99,6 +99,15 @@ struct xe_mem_region {
 	resource_size_t actual_physical_size;
 	/** @mapping: pointer to VRAM mappable space */
 	void __iomem *mapping;
+	/** @pagemap: Used to remap device memory as ZONE_DEVICE */
+    struct dev_pagemap pagemap;
+    /**
+     * @hpa_base: base host physical address
+     *
+     * This is generated when remap device memory as ZONE_DEVICE
+     */
+    resource_size_t hpa_base;
+
 };
 
 /**
drivers/gpu/drm/xe/xe_mmio.c +8 -0
index e3db3a178760..0d795394bc4c 100644
@@ -22,6 +22,7 @@
 #include "xe_module.h"
 #include "xe_sriov.h"
 #include "xe_tile.h"
+#include "xe_svm.h"
 
 #define XEHP_MTCFG_ADDR		XE_REG(0x101800)
 #define TILE_COUNT		REG_GENMASK(15, 8)
@@ -286,6 +287,7 @@ int xe_mmio_probe_vram(struct xe_device *xe)
 		}
 
 		io_size -= min_t(u64, tile_size, io_size);
+		xe_svm_devm_add(tile, &tile->mem.vram);
 	}
 
 	xe->mem.vram.actual_physical_size = total_size;
@@ -354,10 +356,16 @@ void xe_mmio_probe_tiles(struct xe_device *xe)
 static void mmio_fini(struct drm_device *drm, void *arg)
 {
 	struct xe_device *xe = arg;
+    struct xe_tile *tile;
+    u8 id;
 
 	pci_iounmap(to_pci_dev(xe->drm.dev), xe->mmio.regs);
 	if (xe->mem.vram.mapping)
 		iounmap(xe->mem.vram.mapping);
+
+	for_each_tile(tile, xe, id)
+		xe_svm_devm_remove(xe, &tile->mem.vram);
+
 }
 
 static int xe_verify_lmem_ready(struct xe_device *xe)
drivers/gpu/drm/xe/xe_svm.h new file +14 -0
new file mode 100644
index 000000000000..09f9afb0e7d4
@@ -0,0 +1,14 @@
+// SPDX-License-Identifier: MIT
+/*
+ * Copyright © 2023 Intel Corporation
+ */
+
+#ifndef __XE_SVM_H
+#define __XE_SVM_H
+
+#include "xe_device_types.h"
+
+int xe_svm_devm_add(struct xe_tile *tile, struct xe_mem_region *mem);
+void xe_svm_devm_remove(struct xe_device *xe, struct xe_mem_region *mem);
+
+#endif
drivers/gpu/drm/xe/xe_svm_devmem.c new file +91 -0

91 lines not on this page · raw message

Signature · 1 line
2.26.3
maintainer
#2 lore raw

Re: [PATCH 1/5] drm/xe/svm: Remap and provide memmap backing for GPU vram

Quoted 7 lines · Oak Zeng

Memory remap GPU vram using devm_memremap_pages, so each GPU vram page is backed by a struct page.

Those struct pages are created to allow hmm migrate buffer b/t GPU vram and CPU system memory using existing Linux migration mechanism (i.e., migrating b/t CPU system memory and hard disk).

Quoted 8 lines

This is prepare work to enable svm (shared virtual memory) through Linux kernel hmm framework. The memory remap's page map type is set to MEMORY_DEVICE_PRIVATE for now. This means even though each GPU vram page get a struct page and can be mapped in CPU page table, but such pages are treated as GPU's private resource, so CPU can't access them. If CPU access such page, a page fault is triggered and page will be migrate to system memory.

Is this really true? We can map VRAM BOs to the CPU without having migarte back and forth. Admittedly I don't know the inner workings of how this works but in IGTs we do this all the time.

54 batch_bo = xe_bo_create(fd, vm, batch_size,
55 vram_if_possible(fd, 0),
56 DRM_XE_GEM_CREATE_FLAG_NEEDS_VISIBLE_VRAM);
57 batch_map = xe_bo_map(fd, batch_bo, batch_size);

The BO is created in VRAM and then mapped to the CPU.

I don't think there is an expectation of coherence rather caching mode and exclusive access of the memory based on synchronization.

e.g.
User write BB/data via CPU to GPU memory
User calls exec
GPU read / write memory
User wait on sync indicating exec done
User reads result

All of this works without migration. Are we not planing supporting flow with SVM?

Afaik this migration dance really only needs to be done if the CPU and GPU are using atomics on a shared memory region and the GPU device doesn't support a coherent memory protocol (e.g. PVC).

Quoted 25 lines

For GPU device which supports coherent memory protocol b/t CPU and GPU (such as CXL and CAPI protocol), we can remap device memory as MEMORY_DEVICE_COHERENT. This is TBD.

---
 drivers/gpu/drm/xe/Makefile          |  3 +-
 drivers/gpu/drm/xe/xe_device_types.h |  9 +++
 drivers/gpu/drm/xe/xe_mmio.c         |  8 +++
 drivers/gpu/drm/xe/xe_svm.h          | 14 +++++
 drivers/gpu/drm/xe/xe_svm_devmem.c   | 91 ++++++++++++++++++++++++++++
 5 files changed, 124 insertions(+), 1 deletion(-)
 create mode 100644 drivers/gpu/drm/xe/xe_svm.h
 create mode 100644 drivers/gpu/drm/xe/xe_svm_devmem.c
drivers/gpu/drm/xe/Makefile +0 -0
index c531210695db..840467080e59 100644

drivers/gpu/drm/xe/Makefile xe-y += xe_bb.o \ · line 146

Quoted 6 lines
drivers/gpu/drm/xe/Makefile +2 -1
@@ -142,7 +142,8 @@ xe-y += xe_bb.o \
 	xe_vram_freq.o \
 	xe_wait_user_fence.o \
 	xe_wa.o \
-	xe_wopcm.o
+	xe_wopcm.o \
+	xe_svm_devmem.o

These should be in alphabetical order.

Quoted 9 lines
drivers/gpu/drm/xe/Makefile +0 -0
 # graphics hardware monitoring (HWMON) support
 xe-$(CONFIG_HWMON) += xe_hwmon.o
drivers/gpu/drm/xe/xe_device_types.h +0 -0
index 9785eef2e5a4..f27c3bee8ce7 100644
@@ -99,6 +99,15 @@ struct xe_mem_region {
 	resource_size_t actual_physical_size;

drivers/gpu/drm/xe/xe_device_types.h struct xe_mem_region { · line 109

Quoted 10 lines
drivers/gpu/drm/xe/xe_device_types.h +8 -0
 	/** @mapping: pointer to VRAM mappable space */
 	void __iomem *mapping;
+	/** @pagemap: Used to remap device memory as ZONE_DEVICE */
+    struct dev_pagemap pagemap;
+    /**
+     * @hpa_base: base host physical address
+     *
+     * This is generated when remap device memory as ZONE_DEVICE
+     */
+    resource_size_t hpa_base;

Weird indentation. This goes for the entire series, look at checkpatch.

Quoted 17 lines
drivers/gpu/drm/xe/xe_device_types.h +1 -0
+
 };
 
 /**
drivers/gpu/drm/xe/xe_mmio.c +1 -0
index e3db3a178760..0d795394bc4c 100644
@@ -22,6 +22,7 @@
 #include "xe_module.h"
 #include "xe_sriov.h"
 #include "xe_tile.h"
+#include "xe_svm.h"
 
 #define XEHP_MTCFG_ADDR		XE_REG(0x101800)
 #define TILE_COUNT		REG_GENMASK(15, 8)

drivers/gpu/drm/xe/xe_mmio.c int xe_mmio_probe_vram(struct xe_device *xe) · line 290

Quoted 4 lines
drivers/gpu/drm/xe/xe_mmio.c +1 -0
@@ -286,6 +287,7 @@ int xe_mmio_probe_vram(struct xe_device *xe)
 		}
 
 		io_size -= min_t(u64, tile_size, io_size);
+		xe_svm_devm_add(tile, &tile->mem.vram);

Do we want to do this probe for all devices with VRAM or only a subset?

Quoted 6 lines
drivers/gpu/drm/xe/xe_mmio.c +0 -0
 	}
 
 	xe->mem.vram.actual_physical_size = total_size;
@@ -354,10 +356,16 @@ void xe_mmio_probe_tiles(struct xe_device *xe)
 static void mmio_fini(struct drm_device *drm, void *arg)
 {

drivers/gpu/drm/xe/xe_mmio.c void xe_mmio_probe_tiles(struct xe_device *xe) · line 367

Quoted 10 lines
drivers/gpu/drm/xe/xe_mmio.c +5 -0
 	struct xe_device *xe = arg;
+    struct xe_tile *tile;
+    u8 id;
 
 	pci_iounmap(to_pci_dev(xe->drm.dev), xe->mmio.regs);
 	if (xe->mem.vram.mapping)
 		iounmap(xe->mem.vram.mapping);
+
+	for_each_tile(tile, xe, id)
+		xe_svm_devm_remove(xe, &tile->mem.vram);

This should probably be above existing code. Typical on fini to do things in reverse order from init.

drivers/gpu/drm/xe/xe_svm.h line 3

Quoted 13 lines
drivers/gpu/drm/xe/xe_mmio.c +1 -0
+
 }
 
 static int xe_verify_lmem_ready(struct xe_device *xe)
drivers/gpu/drm/xe/xe_svm.h new file +3 -0
new file mode 100644
index 000000000000..09f9afb0e7d4
@@ -0,0 +1,14 @@
+// SPDX-License-Identifier: MIT
+/*
+ * Copyright © 2023 Intel Corporation

2024?

drivers/gpu/drm/xe/xe_svm.h line 9

Quoted 6 lines
drivers/gpu/drm/xe/xe_svm.h new file +6 -0
+ */
+
+#ifndef __XE_SVM_H
+#define __XE_SVM_H
+
+#include "xe_device_types.h"

I don't think you need to include this. Rather just forward decl structs used here.

e.g.

struct xe_device;
struct xe_mem_region;
struct xe_tile;

drivers/gpu/drm/xe/xe_svm.h line 12

Quoted 3 lines
drivers/gpu/drm/xe/xe_svm.h new file +3 -0
+
+int xe_svm_devm_add(struct xe_tile *tile, struct xe_mem_region *mem);
+void xe_svm_devm_remove(struct xe_device *xe, struct xe_mem_region *mem);

The arguments here are incongruent here. Typically we want these to match.

drivers/gpu/drm/xe/xe_svm_devmem.c

Quoted 3 lines
drivers/gpu/drm/xe/xe_svm.h new file +2 -0
+
+#endif
drivers/gpu/drm/xe/xe_svm_devmem.c +0 -0

Incongruent between xe_svm.h and xe_svm_devmem.c. Again these two should match.

drivers/gpu/drm/xe/xe_svm_devmem.c line 3

Quoted 8 lines

new file mode 100644
index 000000000000..63b7a1961cc6

drivers/gpu/drm/xe/xe_svm_devmem.c new file +3 -0
@@ -0,0 +1,91 @@
+// SPDX-License-Identifier: MIT
+/*
+ * Copyright © 2023 Intel Corporation

2024?

drivers/gpu/drm/xe/xe_svm_devmem.c line 10

Quoted 7 lines
drivers/gpu/drm/xe/xe_svm_devmem.c new file +7 -0
+ */
+
+#include <linux/mm_types.h>
+#include <linux/sched/mm.h>
+
+#include "xe_device_types.h"
+#include "xe_trace.h"

xe_trace.h appears to be unused.

Quoted 7 lines
drivers/gpu/drm/xe/xe_svm_devmem.c new file +7 -0
+#include "xe_svm.h"
+
+
+static vm_fault_t xe_devm_migrate_to_ram(struct vm_fault *vmf)
+{
+	return 0;
+}

drivers/gpu/drm/xe/xe_svm_devmem.c line 27

Quoted 10 lines
drivers/gpu/drm/xe/xe_svm_devmem.c new file +10 -0
+
+static void xe_devm_page_free(struct page *page)
+{
+}
+
+static const struct dev_pagemap_ops xe_devm_pagemap_ops = {
+	.page_free = xe_devm_page_free,
+	.migrate_to_ram = xe_devm_migrate_to_ram,
+};
+

Assume these are placeholders that will be populated later?

drivers/gpu/drm/xe/xe_svm_devmem.c line 38

Quoted 11 lines
drivers/gpu/drm/xe/xe_svm_devmem.c new file +11 -0
+/**
+ * xe_svm_devm_add: Remap and provide memmap backing for device memory
+ * @tile: tile that the memory region blongs to
+ * @mr: memory region to remap
+ *
+ * This remap device memory to host physical address space and create
+ * struct page to back device memory
+ *
+ * Return: 0 on success standard error code otherwise
+ */
+int xe_svm_devm_add(struct xe_tile *tile, struct xe_mem_region *mr)

Here I see the xe_mem_region is from tile->mem.vram, wondering rather than using the tile->mem.vram we should use xe->mem.vram when enabling svm? Isn't the idea behind svm the entire memory is 1 view?

I suppose if we do that we also only use 1 TTM VRAM manager / buddy allocator too. I thought I saw some patches flying around for that too.

Quoted 41 lines
drivers/gpu/drm/xe/xe_svm_devmem.c new file +41 -0
+{
+	struct device *dev = &to_pci_dev(tile->xe->drm.dev)->dev;
+	struct resource *res;
+	void *addr;
+	int ret;
+
+	res = devm_request_free_mem_region(dev, &iomem_resource,
+					   mr->usable_size);
+	if (IS_ERR(res)) {
+		ret = PTR_ERR(res);
+		return ret;
+	}
+
+	mr->pagemap.type = MEMORY_DEVICE_PRIVATE;
+	mr->pagemap.range.start = res->start;
+	mr->pagemap.range.end = res->end;
+	mr->pagemap.nr_range = 1;
+	mr->pagemap.ops = &xe_devm_pagemap_ops;
+	mr->pagemap.owner = tile->xe->drm.dev;
+	addr = devm_memremap_pages(dev, &mr->pagemap);
+	if (IS_ERR(addr)) {
+		devm_release_mem_region(dev, res->start, resource_size(res));
+		ret = PTR_ERR(addr);
+		drm_err(&tile->xe->drm, "Failed to remap tile %d memory, errno %d\n",
+				tile->id, ret);
+		return ret;
+	}
+	mr->hpa_base = res->start;
+
+	drm_info(&tile->xe->drm, "Added tile %d memory [%llx-%llx] to devm, remapped to %pr\n",
+			tile->id, mr->io_start, mr->io_start + mr->usable_size, res);
+	return 0;
+}
+
+/**
+ * xe_svm_devm_remove: Unmap device memory and free resources
+ * @xe: xe device
+ * @mr: memory region to remove
+ */
+void xe_svm_devm_remove(struct xe_device *xe, struct xe_mem_region *mr)
+{

drivers/gpu/drm/xe/xe_svm_devmem.c line 89

Quoted 10 lines
drivers/gpu/drm/xe/xe_svm_devmem.c new file +10 -0
+	/*FIXME: below cause a kernel hange during moduel remove*/
+#if 0
+	struct device *dev = &to_pci_dev(xe->drm.dev)->dev;
+
+	if (mr->hpa_base) {
+		devm_memunmap_pages(dev, &mr->pagemap);
+		devm_release_mem_region(dev, mr->pagemap.range.start,
+			mr->pagemap.range.end - mr->pagemap.range.start +1);
+	}
+#endif

This would need to be fixed too.

Matt

Quoted 5 lines
drivers/gpu/drm/xe/xe_svm_devmem.c new file +2 -0
+}
+
Signature · 1 line
2.26.3
author
#3 lore raw

RE: [PATCH 1/5] drm/xe/svm: Remap and provide memmap backing for GPU vram

Hi Matt,

Quoted 49 lines

-----Original Message-----
From: Brost, Matthew <[email protected]>
Sent: Thursday, March 14, 2024 1:18 PM
To: Zeng, Oak <[email protected]>
Cc: [email protected]; Hellstrom, Thomas <[email protected]>; [email protected]; Welty, Brian <[email protected]>; Ghimiray, Himal Prasad <[email protected]>
Subject: Re: [PATCH 1/5] drm/xe/svm: Remap and provide memmap backing for GPU vram

Quoted 7 lines · Oak Zeng

Memory remap GPU vram using devm_memremap_pages, so each GPU vram page is backed by a struct page.

Those struct pages are created to allow hmm migrate buffer b/t GPU vram and CPU system memory using existing Linux migration mechanism (i.e., migrating b/t CPU system memory and hard disk).

Quoted 8 lines

This is prepare work to enable svm (shared virtual memory) through Linux kernel hmm framework. The memory remap's page map type is set to MEMORY_DEVICE_PRIVATE for now. This means even though each GPU vram page get a struct page and can be mapped in CPU page table, but such pages are treated as GPU's private resource, so CPU can't access them. If CPU access such page, a page fault is triggered and page will be migrate to system memory.

Is this really true? We can map VRAM BOs to the CPU without having migarte back and forth. Admittedly I don't know the inner workings of how this works but in IGTs we do this all the time.

54 batch_bo = xe_bo_create(fd, vm, batch_size,
55 vram_if_possible(fd, 0),
56 DRM_XE_GEM_CREATE_FLAG_NEEDS_VISIBLE_VRAM);
57 batch_map = xe_bo_map(fd, batch_bo, batch_size);

The BO is created in VRAM and then mapped to the CPU.

I don't think there is an expectation of coherence rather caching mode and exclusive access of the memory based on synchronization.

e.g.
User write BB/data via CPU to GPU memory
User calls exec
GPU read / write memory
User wait on sync indicating exec done
User reads result

Quoted 6 lines

All of this works without migration. Are we not planing supporting flow with SVM?

Afaik this migration dance really only needs to be done if the CPU and GPU are using atomics on a shared memory region and the GPU device doesn't support a coherent memory protocol (e.g. PVC).

All you said is true. On many of our HW, CPU can actually access device memory, cache coherently or not.

The problem is, this is not true for all GPU vendors. For example, on some HW from some vendor, CPU can only access partially of device memory. The so called small bar concept.

So when HMM is defined, such factors were considered, and MEMORY_DEVICE_PRIVATE is defined. With this definition, CPU can't access device memory.

So you can think it is a limitation of HMM.

Note this is only part 1 of our system allocator work. We do plan to support DEVICE_COHERENT for our newer device, see below. With this option, we don't have unnecessary migration back and forth.

You can think this is just work out all the code path. 90% of the driver code for DEVICE_PRIVATE and DEVICE_COHERENT will be same. Our real use of system allocator will be DEVICE_COHERENT mode. While DEVICE_PRIVATE mode allow us to exercise the code on old HW.

Make sense?

Quoted 24 lines
Quoted 7 lines

For GPU device which supports coherent memory protocol b/t CPU and GPU (such as CXL and CAPI protocol), we can remap device memory as MEMORY_DEVICE_COHERENT. This is TBD.

Co-developed-by Niranjana Vishwanathapura

<[email protected]>

Quoted 1 line
Signed-off-by Niranjana Vishwanathapura

<[email protected]>

Quoted 9 lines
Cc Matthew Brost <[email protected]>
Cc Thomas Hellström <[email protected]>
Cc Brian Welty <[email protected]>
---
 drivers/gpu/drm/xe/Makefile          |  3 +-
 drivers/gpu/drm/xe/xe_device_types.h |  9 +++
 drivers/gpu/drm/xe/xe_mmio.c         |  8 +++
 drivers/gpu/drm/xe/xe_svm.h          | 14 +++++
 drivers/gpu/drm/xe/xe_svm_devmem.c   | 91

++++++++++++++++++++++++++++

Quoted 4 lines
 5 files changed, 124 insertions(+), 1 deletion(-)
 create mode 100644 drivers/gpu/drm/xe/xe_svm.h
 create mode 100644 drivers/gpu/drm/xe/xe_svm_devmem.c
Quoted 13 lines

drivers/gpu/drm/xe/Makefile xe-y += xe_bb.o \ · line 146

Quoted 11 lines
drivers/gpu/drm/xe/Makefile +2 -1
index c531210695db..840467080e59 100644
@@ -142,7 +142,8 @@ xe-y += xe_bb.o \
 	xe_vram_freq.o \
 	xe_wait_user_fence.o \
 	xe_wa.o \
-	xe_wopcm.o
+	xe_wopcm.o \
+	xe_svm_devmem.o

These should be in alphabetical order.

Will fix

Quoted 15 lines

Review

Quoted 5 lines
drivers/gpu/drm/xe/Makefile +0 -0
 # graphics hardware monitoring (HWMON) support
 xe-$(CONFIG_HWMON) += xe_hwmon.o
(quoted diff) +0 -0

b/drivers/gpu/drm/xe/xe_device_types.h

Quoted 9 lines

index 9785eef2e5a4..f27c3bee8ce7 100644

drivers/gpu/drm/xe/xe_device_types.h +2 -0
@@ -99,6 +99,15 @@ struct xe_mem_region {
 	resource_size_t actual_physical_size;
 	/** @mapping: pointer to VRAM mappable space */
 	void __iomem *mapping;
+	/** @pagemap: Used to remap device memory as ZONE_DEVICE */
+    struct dev_pagemap pagemap;
Quoted 8 lines

drivers/gpu/drm/xe/xe_device_types.h struct xe_mem_region { · line 109

Quoted 6 lines
drivers/gpu/drm/xe/xe_device_types.h +6 -0
+    /**
+     * @hpa_base: base host physical address
+     *
+     * This is generated when remap device memory as ZONE_DEVICE
+     */
+    resource_size_t hpa_base;

Weird indentation. This goes for the entire series, look at checkpatch.

Will fix

Quoted 20 lines
Quoted 20 lines
drivers/gpu/drm/xe/xe_device_types.h +1 -0
+
 };
 
 /**
drivers/gpu/drm/xe/xe_mmio.c +1 -0
index e3db3a178760..0d795394bc4c 100644
@@ -22,6 +22,7 @@
 #include "xe_module.h"
 #include "xe_sriov.h"
 #include "xe_tile.h"
+#include "xe_svm.h"
 
 #define XEHP_MTCFG_ADDR		XE_REG(0x101800)
 #define TILE_COUNT		REG_GENMASK(15, 8)
@@ -286,6 +287,7 @@ int xe_mmio_probe_vram(struct xe_device *xe)
 		}
 
Quoted 4 lines

drivers/gpu/drm/xe/xe_mmio.c int xe_mmio_probe_vram(struct xe_device *xe) · line 290

Quoted 2 lines
drivers/gpu/drm/xe/xe_mmio.c +1 -0
 		io_size -= min_t(u64, tile_size, io_size);
+		xe_svm_devm_add(tile, &tile->mem.vram);

Do we want to do this probe for all devices with VRAM or only a subset?

All

Quoted 11 lines
Quoted 11 lines
drivers/gpu/drm/xe/xe_mmio.c +2 -0
 	}
 
 	xe->mem.vram.actual_physical_size = total_size;
@@ -354,10 +356,16 @@ void xe_mmio_probe_tiles(struct xe_device *xe)
 static void mmio_fini(struct drm_device *drm, void *arg)
 {
 	struct xe_device *xe = arg;
+    struct xe_tile *tile;
+    u8 id;
 
Quoted 9 lines

drivers/gpu/drm/xe/xe_mmio.c void xe_mmio_probe_tiles(struct xe_device *xe) · line 367

Quoted 6 lines
drivers/gpu/drm/xe/xe_mmio.c +3 -0
 	pci_iounmap(to_pci_dev(xe->drm.dev), xe->mmio.regs);
 	if (xe->mem.vram.mapping)
 		iounmap(xe->mem.vram.mapping);
+
+	for_each_tile(tile, xe, id)
+		xe_svm_devm_remove(xe, &tile->mem.vram);

This should probably be above existing code. Typical on fini to do things in reverse order from init.

Will fix

Quoted 4 lines
Quoted 4 lines
drivers/gpu/drm/xe/xe_mmio.c +1 -0
+
 }
 
Quoted 12 lines

drivers/gpu/drm/xe/xe_svm.h line 3

Quoted 10 lines
drivers/gpu/drm/xe/xe_mmio.c +0 -0
 static int xe_verify_lmem_ready(struct xe_device *xe)
drivers/gpu/drm/xe/xe_svm.h new file +3 -0
new file mode 100644
index 000000000000..09f9afb0e7d4
@@ -0,0 +1,14 @@
+// SPDX-License-Identifier: MIT
+/*
+ * Copyright © 2023 Intel Corporation

2024?

This patch was actually written 2023

Quoted 10 lines

drivers/gpu/drm/xe/xe_svm.h line 9

Quoted 7 lines
drivers/gpu/drm/xe/xe_svm.h new file +6 -0
+ */
+
+#ifndef __XE_SVM_H
+#define __XE_SVM_H
+
+#include "xe_device_types.h"

I don't think you need to include this. Rather just forward decl structs used here.

Will fix

Quoted 7 lines

e.g.

struct xe_device;
struct xe_mem_region;
struct xe_tile;

Quoted 7 lines

drivers/gpu/drm/xe/xe_svm.h

Quoted 3 lines
drivers/gpu/drm/xe/xe_svm.h new file +3 -0
+
+int xe_svm_devm_add(struct xe_tile *tile, struct xe_mem_region *mem);
+void xe_svm_devm_remove(struct xe_device *xe, struct xe_mem_region

*mem);

The arguments here are incongruent here. Typically we want these to match.

Will fix

Quoted 7 lines

Review

Quoted 4 lines
drivers/gpu/drm/xe/xe_svm.h new file +2 -0
+
+#endif
(quoted diff) +0 -0

b/drivers/gpu/drm/xe/xe_svm_devmem.c

Incongruent between xe_svm.h and xe_svm_devmem.c.

Did you mean mem vs mr? if yes, will fix

Again these two

Quoted 13 lines

should
match.

drivers/gpu/drm/xe/xe_svm_devmem.c line 3

Quoted 8 lines

new file mode 100644
index 000000000000..63b7a1961cc6

drivers/gpu/drm/xe/xe_svm_devmem.c new file +3 -0
@@ -0,0 +1,91 @@
+// SPDX-License-Identifier: MIT
+/*
+ * Copyright © 2023 Intel Corporation

2024?

It is from 2023

Quoted 10 lines

drivers/gpu/drm/xe/xe_svm_devmem.c line 10

Quoted 8 lines
drivers/gpu/drm/xe/xe_svm_devmem.c new file +7 -0
+ */
+
+#include <linux/mm_types.h>
+#include <linux/sched/mm.h>
+
+#include "xe_device_types.h"
+#include "xe_trace.h"

xe_trace.h appears to be unused.

Will fix

Quoted 12 lines
Quoted 12 lines
drivers/gpu/drm/xe/xe_svm_devmem.c new file +11 -0
+#include "xe_svm.h"
+
+
+static vm_fault_t xe_devm_migrate_to_ram(struct vm_fault *vmf)
+{
+	return 0;
+}
+
+static void xe_devm_page_free(struct page *page)
+{
+}
Quoted 8 lines

drivers/gpu/drm/xe/xe_svm_devmem.c line 27

Quoted 6 lines
drivers/gpu/drm/xe/xe_svm_devmem.c new file +6 -0
+
+static const struct dev_pagemap_ops xe_devm_pagemap_ops = {
+	.page_free = xe_devm_page_free,
+	.migrate_to_ram = xe_devm_migrate_to_ram,
+};
+

Assume these are placeholders that will be populated later?

corrrect

Quoted 14 lines

drivers/gpu/drm/xe/xe_svm_devmem.c line 29

Quoted 3 lines
drivers/gpu/drm/xe/xe_svm_devmem.c new file +2 -0
+/**
+ * xe_svm_devm_add: Remap and provide memmap backing for device

memory

drivers/gpu/drm/xe/xe_svm_devmem.c line 38

Quoted 9 lines
drivers/gpu/drm/xe/xe_svm_devmem.c new file +9 -0
+ * @tile: tile that the memory region blongs to
+ * @mr: memory region to remap
+ *
+ * This remap device memory to host physical address space and create
+ * struct page to back device memory
+ *
+ * Return: 0 on success standard error code otherwise
+ */
+int xe_svm_devm_add(struct xe_tile *tile, struct xe_mem_region *mr)
Quoted 3 lines

Here I see the xe_mem_region is from tile->mem.vram, wondering rather than using the tile->mem.vram we should use xe->mem.vram when enabling svm? Isn't the idea behind svm the entire memory is 1 view?

Still need to use tile. The reason is, memory of different tile can have different characteristics, such as latency. So we want to differentiate memory b/t tiles also in svm. I need to change below " mr->pagemap.owner = tile->xe->drm.dev ". the owner should also be tile. This is the way hmm differentiate memory of different tile.

With svm it is 1 view, from virtual address space perspective and from physical struct page perspective. You can think of all the tile's vram is stacked together to form a unified view together with system memory. This doesn't prohibit us from differentiate memory from different tile. This differentiation allow us to optimize performance, i.e., we can wisely place memory in specific tile. If we don't differentiate, this is not possible.

Quoted 3 lines

I suppose if we do that we also only use 1 TTM VRAM manager / buddy allocator too. I thought I saw some patches flying around for that too.

Ttm vram manager is not in the picture. We deliberately avoided it per previous discussion

Yes same buddy allocator. It is in my previous POC: https://lore.kernel.org/dri-devel/[email protected]/. I didn't put those patches in this series because I want to merge this small patches separately.

Quoted 45 lines
Quoted 15 lines
drivers/gpu/drm/xe/xe_svm_devmem.c new file +14 -0
+{
+	struct device *dev = &to_pci_dev(tile->xe->drm.dev)->dev;
+	struct resource *res;
+	void *addr;
+	int ret;
+
+	res = devm_request_free_mem_region(dev, &iomem_resource,
+					   mr->usable_size);
+	if (IS_ERR(res)) {
+		ret = PTR_ERR(res);
+		return ret;
+	}
+
+	mr->pagemap.type = MEMORY_DEVICE_PRIVATE;

drivers/gpu/drm/xe/xe_svm_devmem.c

Quoted 10 lines
drivers/gpu/drm/xe/xe_svm_devmem.c new file +10 -0
+	mr->pagemap.range.start = res->start;
+	mr->pagemap.range.end = res->end;
+	mr->pagemap.nr_range = 1;
+	mr->pagemap.ops = &xe_devm_pagemap_ops;
+	mr->pagemap.owner = tile->xe->drm.dev;
+	addr = devm_memremap_pages(dev, &mr->pagemap);
+	if (IS_ERR(addr)) {
+		devm_release_mem_region(dev, res->start, resource_size(res));
+		ret = PTR_ERR(addr);
+		drm_err(&tile->xe->drm, "Failed to remap tile %d memory,

errno %d\n",

drivers/gpu/drm/xe/xe_svm_devmem.c

Quoted 6 lines
drivers/gpu/drm/xe/xe_svm_devmem.c new file +6 -0
+				tile->id, ret);
+		return ret;
+	}
+	mr->hpa_base = res->start;
+
+	drm_info(&tile->xe->drm, "Added tile %d memory [%llx-%llx] to devm,

remapped to %pr\n",

drivers/gpu/drm/xe/xe_svm_devmem.c line 69

Quoted 1 line
drivers/gpu/drm/xe/xe_svm_devmem.c new file +1 -0
+			tile->id, mr->io_start, mr->io_start + mr->usable_size,

res);

drivers/gpu/drm/xe/xe_svm_devmem.c

Quoted 9 lines
drivers/gpu/drm/xe/xe_svm_devmem.c new file +9 -0
+	return 0;
+}
+
+/**
+ * xe_svm_devm_remove: Unmap device memory and free resources
+ * @xe: xe device
+ * @mr: memory region to remove
+ */
+void xe_svm_devm_remove(struct xe_device *xe, struct xe_mem_region

*mr)

Quoted 13 lines

drivers/gpu/drm/xe/xe_svm_devmem.c line 89

Quoted 11 lines
drivers/gpu/drm/xe/xe_svm_devmem.c new file +11 -0
+{
+	/*FIXME: below cause a kernel hange during moduel remove*/
+#if 0
+	struct device *dev = &to_pci_dev(xe->drm.dev)->dev;
+
+	if (mr->hpa_base) {
+		devm_memunmap_pages(dev, &mr->pagemap);
+		devm_release_mem_region(dev, mr->pagemap.range.start,
+			mr->pagemap.range.end - mr->pagemap.range.start +1);
+	}
+#endif

This would need to be fixed too.

Yes...

Oak

Quoted 8 lines

Matt

Quoted 5 lines
drivers/gpu/drm/xe/xe_svm_devmem.c new file +2 -1
+}
+
--

2.26.3

maintainer
#4 lore raw

Re: [PATCH 1/5] drm/xe/svm: Remap and provide memmap backing for GPU vram

Quoted 58 lines · Oak

Hi Matt,

Quoted 49 lines

-----Original Message-----
From: Brost, Matthew <[email protected]>
Sent: Thursday, March 14, 2024 1:18 PM
To: Zeng, Oak <[email protected]>
Cc: [email protected]; Hellstrom, Thomas <[email protected]>; [email protected]; Welty, Brian <[email protected]>; Ghimiray, Himal Prasad <[email protected]>
Subject: Re: [PATCH 1/5] drm/xe/svm: Remap and provide memmap backing for GPU vram

Quoted 7 lines · Oak Zeng

Memory remap GPU vram using devm_memremap_pages, so each GPU vram page is backed by a struct page.

Those struct pages are created to allow hmm migrate buffer b/t GPU vram and CPU system memory using existing Linux migration mechanism (i.e., migrating b/t CPU system memory and hard disk).

Quoted 8 lines

This is prepare work to enable svm (shared virtual memory) through Linux kernel hmm framework. The memory remap's page map type is set to MEMORY_DEVICE_PRIVATE for now. This means even though each GPU vram page get a struct page and can be mapped in CPU page table, but such pages are treated as GPU's private resource, so CPU can't access them. If CPU access such page, a page fault is triggered and page will be migrate to system memory.

Is this really true? We can map VRAM BOs to the CPU without having migarte back and forth. Admittedly I don't know the inner workings of how this works but in IGTs we do this all the time.

54 batch_bo = xe_bo_create(fd, vm, batch_size,
55 vram_if_possible(fd, 0),
56 DRM_XE_GEM_CREATE_FLAG_NEEDS_VISIBLE_VRAM);
57 batch_map = xe_bo_map(fd, batch_bo, batch_size);

The BO is created in VRAM and then mapped to the CPU.

I don't think there is an expectation of coherence rather caching mode and exclusive access of the memory based on synchronization.

e.g.
User write BB/data via CPU to GPU memory
User calls exec
GPU read / write memory
User wait on sync indicating exec done
User reads result

Quoted 6 lines

All of this works without migration. Are we not planing supporting flow with SVM?

Afaik this migration dance really only needs to be done if the CPU and GPU are using atomics on a shared memory region and the GPU device doesn't support a coherent memory protocol (e.g. PVC).

Quoted 8 lines

All you said is true. On many of our HW, CPU can actually access device memory, cache coherently or not.

The problem is, this is not true for all GPU vendors. For example, on some HW from some vendor, CPU can only access partially of device memory. The so called small bar concept.

So when HMM is defined, such factors were considered, and MEMORY_DEVICE_PRIVATE is defined. With this definition, CPU can't access device memory.

So you can think it is a limitation of HMM.

Is it though? I see other type MEMORY_DEVICE_FS_DAX, MEMORY_DEVICE_GENERIC, and MEMORY_DEVICE_PCI_P2PDMA. From my limited understanding it looks to me like one of those modes would support my example.

Quoted 6 lines

Note this is only part 1 of our system allocator work. We do plan to support DEVICE_COHERENT for our newer device, see below. With this option, we don't have unnecessary migration back and forth.

You can think this is just work out all the code path. 90% of the driver code for DEVICE_PRIVATE and DEVICE_COHERENT will be same. Our real use of system allocator will be DEVICE_COHERENT mode. While DEVICE_PRIVATE mode allow us to exercise the code on old HW.

Make sense?

I guess if we want the system allocator to always coherent, then yes you need this dynamic migration with faulting on either side.

I was thinking the system allocator would be behave like my example above with madvise dictating the coherence rules.

Maybe I missed this in system allocator design but my feeling is we shouldn't arbitrarily enforce coherence as that could lead to poor performance due to constant migration.

Quoted 85 lines
Quoted 25 lines
Quoted 8 lines

For GPU device which supports coherent memory protocol b/t CPU and GPU (such as CXL and CAPI protocol), we can remap device memory as MEMORY_DEVICE_COHERENT. This is TBD.

Co-developed-by Niranjana Vishwanathapura

<[email protected]>

Quoted 1 line
Signed-off-by Niranjana Vishwanathapura

<[email protected]>

Quoted 9 lines
Cc Matthew Brost <[email protected]>
Cc Thomas Hellström <[email protected]>
Cc Brian Welty <[email protected]>
---
 drivers/gpu/drm/xe/Makefile          |  3 +-
 drivers/gpu/drm/xe/xe_device_types.h |  9 +++
 drivers/gpu/drm/xe/xe_mmio.c         |  8 +++
 drivers/gpu/drm/xe/xe_svm.h          | 14 +++++
 drivers/gpu/drm/xe/xe_svm_devmem.c   | 91

++++++++++++++++++++++++++++

Quoted 4 lines
 5 files changed, 124 insertions(+), 1 deletion(-)
 create mode 100644 drivers/gpu/drm/xe/xe_svm.h
 create mode 100644 drivers/gpu/drm/xe/xe_svm_devmem.c
Quoted 13 lines

drivers/gpu/drm/xe/Makefile xe-y += xe_bb.o \ · line 146

Quoted 11 lines
drivers/gpu/drm/xe/Makefile +2 -1
index c531210695db..840467080e59 100644
@@ -142,7 +142,8 @@ xe-y += xe_bb.o \
 	xe_vram_freq.o \
 	xe_wait_user_fence.o \
 	xe_wa.o \
-	xe_wopcm.o
+	xe_wopcm.o \
+	xe_svm_devmem.o

These should be in alphabetical order.

Will fix

Quoted 15 lines

Review

Quoted 5 lines
drivers/gpu/drm/xe/Makefile +0 -0
 # graphics hardware monitoring (HWMON) support
 xe-$(CONFIG_HWMON) += xe_hwmon.o
(quoted diff) +0 -0

b/drivers/gpu/drm/xe/xe_device_types.h

Quoted 9 lines

index 9785eef2e5a4..f27c3bee8ce7 100644

drivers/gpu/drm/xe/xe_device_types.h +2 -0
@@ -99,6 +99,15 @@ struct xe_mem_region {
 	resource_size_t actual_physical_size;
 	/** @mapping: pointer to VRAM mappable space */
 	void __iomem *mapping;
+	/** @pagemap: Used to remap device memory as ZONE_DEVICE */
+    struct dev_pagemap pagemap;
Quoted 8 lines

drivers/gpu/drm/xe/xe_device_types.h struct xe_mem_region { · line 109

Quoted 6 lines
drivers/gpu/drm/xe/xe_device_types.h +6 -0
+    /**
+     * @hpa_base: base host physical address
+     *
+     * This is generated when remap device memory as ZONE_DEVICE
+     */
+    resource_size_t hpa_base;

Weird indentation. This goes for the entire series, look at checkpatch.

Will fix

Quoted 20 lines
Quoted 20 lines
drivers/gpu/drm/xe/xe_device_types.h +1 -0
+
 };
 
 /**
drivers/gpu/drm/xe/xe_mmio.c +1 -0
index e3db3a178760..0d795394bc4c 100644
@@ -22,6 +22,7 @@
 #include "xe_module.h"
 #include "xe_sriov.h"
 #include "xe_tile.h"
+#include "xe_svm.h"
 
 #define XEHP_MTCFG_ADDR		XE_REG(0x101800)
 #define TILE_COUNT		REG_GENMASK(15, 8)
@@ -286,6 +287,7 @@ int xe_mmio_probe_vram(struct xe_device *xe)
 		}
 
Quoted 6 lines
Quoted 4 lines

drivers/gpu/drm/xe/xe_mmio.c int xe_mmio_probe_vram(struct xe_device *xe) · line 290

Quoted 2 lines
drivers/gpu/drm/xe/xe_mmio.c +1 -0
 		io_size -= min_t(u64, tile_size, io_size);
+		xe_svm_devm_add(tile, &tile->mem.vram);

Do we want to do this probe for all devices with VRAM or only a subset?

All

Can you explain why?

Quoted 142 lines
Quoted 11 lines
Quoted 11 lines
drivers/gpu/drm/xe/xe_mmio.c +2 -0
 	}
 
 	xe->mem.vram.actual_physical_size = total_size;
@@ -354,10 +356,16 @@ void xe_mmio_probe_tiles(struct xe_device *xe)
 static void mmio_fini(struct drm_device *drm, void *arg)
 {
 	struct xe_device *xe = arg;
+    struct xe_tile *tile;
+    u8 id;
 
Quoted 9 lines

drivers/gpu/drm/xe/xe_mmio.c void xe_mmio_probe_tiles(struct xe_device *xe) · line 367

Quoted 6 lines
drivers/gpu/drm/xe/xe_mmio.c +3 -0
 	pci_iounmap(to_pci_dev(xe->drm.dev), xe->mmio.regs);
 	if (xe->mem.vram.mapping)
 		iounmap(xe->mem.vram.mapping);
+
+	for_each_tile(tile, xe, id)
+		xe_svm_devm_remove(xe, &tile->mem.vram);

This should probably be above existing code. Typical on fini to do things in reverse order from init.

Will fix

Quoted 4 lines
Quoted 4 lines
drivers/gpu/drm/xe/xe_mmio.c +1 -0
+
 }
 
Quoted 12 lines

drivers/gpu/drm/xe/xe_svm.h line 3

Quoted 10 lines
drivers/gpu/drm/xe/xe_mmio.c +0 -0
 static int xe_verify_lmem_ready(struct xe_device *xe)
drivers/gpu/drm/xe/xe_svm.h new file +3 -0
new file mode 100644
index 000000000000..09f9afb0e7d4
@@ -0,0 +1,14 @@
+// SPDX-License-Identifier: MIT
+/*
+ * Copyright © 2023 Intel Corporation

2024?

This patch was actually written 2023

Quoted 10 lines

drivers/gpu/drm/xe/xe_svm.h line 9

Quoted 7 lines
drivers/gpu/drm/xe/xe_svm.h new file +6 -0
+ */
+
+#ifndef __XE_SVM_H
+#define __XE_SVM_H
+
+#include "xe_device_types.h"

I don't think you need to include this. Rather just forward decl structs used here.

Will fix

Quoted 7 lines

e.g.

struct xe_device;
struct xe_mem_region;
struct xe_tile;

Quoted 7 lines

drivers/gpu/drm/xe/xe_svm.h

Quoted 3 lines
drivers/gpu/drm/xe/xe_svm.h new file +3 -0
+
+int xe_svm_devm_add(struct xe_tile *tile, struct xe_mem_region *mem);
+void xe_svm_devm_remove(struct xe_device *xe, struct xe_mem_region

*mem);

The arguments here are incongruent here. Typically we want these to match.

Will fix

Quoted 7 lines

Review

Quoted 4 lines
drivers/gpu/drm/xe/xe_svm.h new file +2 -0
+
+#endif
(quoted diff) +0 -0

b/drivers/gpu/drm/xe/xe_svm_devmem.c

Incongruent between xe_svm.h and xe_svm_devmem.c.

Did you mean mem vs mr? if yes, will fix

Again these two

Quoted 13 lines

should
match.

drivers/gpu/drm/xe/xe_svm_devmem.c line 3

Quoted 8 lines

new file mode 100644
index 000000000000..63b7a1961cc6

drivers/gpu/drm/xe/xe_svm_devmem.c new file +3 -0
@@ -0,0 +1,91 @@
+// SPDX-License-Identifier: MIT
+/*
+ * Copyright © 2023 Intel Corporation

2024?

It is from 2023

Quoted 10 lines

drivers/gpu/drm/xe/xe_svm_devmem.c line 10

Quoted 8 lines
drivers/gpu/drm/xe/xe_svm_devmem.c new file +7 -0
+ */
+
+#include <linux/mm_types.h>
+#include <linux/sched/mm.h>
+
+#include "xe_device_types.h"
+#include "xe_trace.h"

xe_trace.h appears to be unused.

Will fix

Quoted 12 lines
Quoted 12 lines
drivers/gpu/drm/xe/xe_svm_devmem.c new file +11 -0
+#include "xe_svm.h"
+
+
+static vm_fault_t xe_devm_migrate_to_ram(struct vm_fault *vmf)
+{
+	return 0;
+}
+
+static void xe_devm_page_free(struct page *page)
+{
+}
Quoted 8 lines

drivers/gpu/drm/xe/xe_svm_devmem.c line 27

Quoted 6 lines
drivers/gpu/drm/xe/xe_svm_devmem.c new file +6 -0
+
+static const struct dev_pagemap_ops xe_devm_pagemap_ops = {
+	.page_free = xe_devm_page_free,
+	.migrate_to_ram = xe_devm_migrate_to_ram,
+};
+

Assume these are placeholders that will be populated later?

corrrect

Quoted 14 lines

drivers/gpu/drm/xe/xe_svm_devmem.c line 29

Quoted 3 lines
drivers/gpu/drm/xe/xe_svm_devmem.c new file +2 -0
+/**
+ * xe_svm_devm_add: Remap and provide memmap backing for device

memory

Quoted 9 lines
drivers/gpu/drm/xe/xe_svm_devmem.c new file +9 -0
+ * @tile: tile that the memory region blongs to
+ * @mr: memory region to remap
+ *
+ * This remap device memory to host physical address space and create
+ * struct page to back device memory
+ *
+ * Return: 0 on success standard error code otherwise
+ */
+int xe_svm_devm_add(struct xe_tile *tile, struct xe_mem_region *mr)
Quoted 8 lines
Quoted 3 lines

Here I see the xe_mem_region is from tile->mem.vram, wondering rather than using the tile->mem.vram we should use xe->mem.vram when enabling svm? Isn't the idea behind svm the entire memory is 1 view?

Still need to use tile. The reason is, memory of different tile can have different characteristics, such as latency. So we want to differentiate memory b/t tiles also in svm. I need to change below " mr->pagemap.owner = tile->xe->drm.dev ". the owner should also be tile. This is the way hmm differentiate memory of different tile.

With svm it is 1 view, from virtual address space perspective and from physical struct page perspective. You can think of all the tile's vram is stacked together to form a unified view together with system memory. This doesn't prohibit us from differentiate memory from different tile. This differentiation allow us to optimize performance, i.e., we can wisely place memory in specific tile. If we don't differentiate, this is not possible.

Ok makes sense.

Matt

Quoted 78 lines
Quoted 3 lines

I suppose if we do that we also only use 1 TTM VRAM manager / buddy allocator too. I thought I saw some patches flying around for that too.

Ttm vram manager is not in the picture. We deliberately avoided it per previous discussion

Yes same buddy allocator. It is in my previous POC: https://lore.kernel.org/dri-devel/[email protected]/. I didn't put those patches in this series because I want to merge this small patches separately.

Quoted 45 lines
Quoted 15 lines
drivers/gpu/drm/xe/xe_svm_devmem.c new file +14 -0
+{
+	struct device *dev = &to_pci_dev(tile->xe->drm.dev)->dev;
+	struct resource *res;
+	void *addr;
+	int ret;
+
+	res = devm_request_free_mem_region(dev, &iomem_resource,
+					   mr->usable_size);
+	if (IS_ERR(res)) {
+		ret = PTR_ERR(res);
+		return ret;
+	}
+
+	mr->pagemap.type = MEMORY_DEVICE_PRIVATE;

drivers/gpu/drm/xe/xe_svm_devmem.c

Quoted 10 lines
drivers/gpu/drm/xe/xe_svm_devmem.c new file +10 -0
+	mr->pagemap.range.start = res->start;
+	mr->pagemap.range.end = res->end;
+	mr->pagemap.nr_range = 1;
+	mr->pagemap.ops = &xe_devm_pagemap_ops;
+	mr->pagemap.owner = tile->xe->drm.dev;
+	addr = devm_memremap_pages(dev, &mr->pagemap);
+	if (IS_ERR(addr)) {
+		devm_release_mem_region(dev, res->start, resource_size(res));
+		ret = PTR_ERR(addr);
+		drm_err(&tile->xe->drm, "Failed to remap tile %d memory,

errno %d\n",

drivers/gpu/drm/xe/xe_svm_devmem.c

Quoted 6 lines
drivers/gpu/drm/xe/xe_svm_devmem.c new file +6 -0
+				tile->id, ret);
+		return ret;
+	}
+	mr->hpa_base = res->start;
+
+	drm_info(&tile->xe->drm, "Added tile %d memory [%llx-%llx] to devm,

remapped to %pr\n",

drivers/gpu/drm/xe/xe_svm_devmem.c line 69

Quoted 1 line
drivers/gpu/drm/xe/xe_svm_devmem.c new file +1 -0
+			tile->id, mr->io_start, mr->io_start + mr->usable_size,

res);

drivers/gpu/drm/xe/xe_svm_devmem.c

Quoted 9 lines
drivers/gpu/drm/xe/xe_svm_devmem.c new file +9 -0
+	return 0;
+}
+
+/**
+ * xe_svm_devm_remove: Unmap device memory and free resources
+ * @xe: xe device
+ * @mr: memory region to remove
+ */
+void xe_svm_devm_remove(struct xe_device *xe, struct xe_mem_region

*mr)

Quoted 13 lines

drivers/gpu/drm/xe/xe_svm_devmem.c line 89

Quoted 11 lines
drivers/gpu/drm/xe/xe_svm_devmem.c new file +11 -0
+{
+	/*FIXME: below cause a kernel hange during moduel remove*/
+#if 0
+	struct device *dev = &to_pci_dev(xe->drm.dev)->dev;
+
+	if (mr->hpa_base) {
+		devm_memunmap_pages(dev, &mr->pagemap);
+		devm_release_mem_region(dev, mr->pagemap.range.start,
+			mr->pagemap.range.end - mr->pagemap.range.start +1);
+	}
+#endif

This would need to be fixed too.

Yes...

Oak

Quoted 8 lines

Matt

Quoted 5 lines
drivers/gpu/drm/xe/xe_svm_devmem.c new file +2 -1
+}
+
--

2.26.3

author
#5 lore raw

RE: [PATCH 1/5] drm/xe/svm: Remap and provide memmap backing for GPU vram

Quoted 85 lines

-----Original Message-----
From: Brost, Matthew <[email protected]>
Sent: Thursday, March 14, 2024 4:49 PM
To: Zeng, Oak <[email protected]>
Cc: [email protected]; Hellstrom, Thomas <[email protected]>; [email protected]; Welty, Brian <[email protected]>; Ghimiray, Himal Prasad <[email protected]>
Subject: Re: [PATCH 1/5] drm/xe/svm: Remap and provide memmap backing for GPU vram

Quoted 11 lines · Oak

Hi Matt,

Quoted 9 lines

-----Original Message-----
From: Brost, Matthew <[email protected]>
Sent: Thursday, March 14, 2024 1:18 PM
To: Zeng, Oak <[email protected]>
Cc: [email protected]; Hellstrom, Thomas <[email protected]>; [email protected]; Welty, Brian <[email protected]>; Ghimiray, Himal Prasad <[email protected]>
Subject: Re: [PATCH 1/5] drm/xe/svm: Remap and provide memmap backing

for

Quoted 4 lines
Quoted 4 lines

GPU vram

Quoted 1 line · Oak Zeng

Memory remap GPU vram using devm_memremap_pages, so each GPU

vram

Quoted 36 lines
Quoted 36 lines
Quoted 6 lines

page is backed by a struct page.

Those struct pages are created to allow hmm migrate buffer b/t GPU vram and CPU system memory using existing Linux migration mechanism (i.e., migrating b/t CPU system memory and hard disk).

Quoted 8 lines

This is prepare work to enable svm (shared virtual memory) through Linux kernel hmm framework. The memory remap's page map type is set to MEMORY_DEVICE_PRIVATE for now. This means even though each GPU vram page get a struct page and can be mapped in CPU page table, but such pages are treated as GPU's private resource, so CPU can't access them. If CPU access such page, a page fault is triggered and page will be migrate to system memory.

Is this really true? We can map VRAM BOs to the CPU without having migarte back and forth. Admittedly I don't know the inner workings of how this works but in IGTs we do this all the time.

54 batch_bo = xe_bo_create(fd, vm, batch_size,
55 vram_if_possible(fd, 0),
56 DRM_XE_GEM_CREATE_FLAG_NEEDS_VISIBLE_VRAM);
57 batch_map = xe_bo_map(fd, batch_bo, batch_size);

The BO is created in VRAM and then mapped to the CPU.

I don't think there is an expectation of coherence rather caching mode and exclusive access of the memory based on synchronization.

e.g.
User write BB/data via CPU to GPU memory
User calls exec
GPU read / write memory
User wait on sync indicating exec done
User reads result

Quoted 8 lines
Quoted 6 lines

All of this works without migration. Are we not planing supporting flow with SVM?

Afaik this migration dance really only needs to be done if the CPU and GPU are using atomics on a shared memory region and the GPU device doesn't support a coherent memory protocol (e.g. PVC).

All you said is true. On many of our HW, CPU can actually access device memory,

cache coherently or not.

Quoted 2 lines

The problem is, this is not true for all GPU vendors. For example, on some HW

from some vendor, CPU can only access partially of device memory. The so called small bar concept.

Quoted 2 lines

So when HMM is defined, such factors were considered, and

MEMORY_DEVICE_PRIVATE is defined. With this definition, CPU can't access device memory.

Quoted 1 line
Quoted 8 lines
Quoted 2 lines

So you can think it is a limitation of HMM.

Is it though? I see other type MEMORY_DEVICE_FS_DAX, MEMORY_DEVICE_GENERIC, and MEMORY_DEVICE_PCI_P2PDMA. From my limited
understanding it looks to me like one of those modes would support my example.

No, above are for other purposes. HMM only support DEVICE_PRIVATE and DEVICE_COHERENT.

Quoted 16 lines
Quoted 2 lines

Note this is only part 1 of our system allocator work. We do plan to support

DEVICE_COHERENT for our newer device, see below. With this option, we don't have unnecessary migration back and forth.

Quoted 2 lines

You can think this is just work out all the code path. 90% of the driver code for

DEVICE_PRIVATE and DEVICE_COHERENT will be same. Our real use of system allocator will be DEVICE_COHERENT mode. While DEVICE_PRIVATE mode allow us to exercise the code on old HW.

Quoted 3 lines

Make sense?

I guess if we want the system allocator to always coherent, then yes you need this dynamic migration with faulting on either side.

Quoted 6 lines

I was thinking the system allocator would be behave like my example above with madvise dictating the coherence rules.

Maybe I missed this in system allocator design but my feeling is we shouldn't arbitrarily enforce coherence as that could lead to poor performance due to constant migration.

System allocator itself doesn't enforce coherence. Coherence is built in user programming model. So system allocator allow both GPU and CPU access system allocated pointers, but it doesn't necessarily guarantee the data accessed from CPU/GPU is coherent. It is user program's responsibility to maintain data coherence.

Data migration in driver is optional, depending on platform capability, user preference, correctness and performance consideration. Driver internal data migration of course shouldn't break data coherence.

Of course different vendor can have different data coherence scheme. For example, it is completely designer's flexibility to build model that is HW automatic data coherence or software explicit data coherence. On our platform, we allow user program to select different coherence mode by setting pat_index for gpu and cpu_caching mode for CPU. So we have completely give the flexibility to user program. Nothing of this contract is changed in system allocator design.

Going back to the question of what memory type we should use to register our vram to core mm. HMM currently support two types: PRIVATE and COHERENT. The coherent type requires some HW and BIOS support which we don't have right now. So the only available is PRIVATE. We have not other option right now. As said, we plan to support coherent type where we can avoid unnecessary data migration. But that is stage 2.

Quoted 87 lines
Quoted 65 lines
Quoted 26 lines
Quoted 9 lines

For GPU device which supports coherent memory protocol b/t CPU and GPU (such as CXL and CAPI protocol), we can remap device memory as MEMORY_DEVICE_COHERENT. This is TBD.

Co-developed-by Niranjana Vishwanathapura

<[email protected]>

Quoted 1 line
Signed-off-by Niranjana Vishwanathapura

<[email protected]>

Quoted 9 lines
Cc Matthew Brost <[email protected]>
Cc Thomas Hellström <[email protected]>
Cc Brian Welty <[email protected]>
---
 drivers/gpu/drm/xe/Makefile          |  3 +-
 drivers/gpu/drm/xe/xe_device_types.h |  9 +++
 drivers/gpu/drm/xe/xe_mmio.c         |  8 +++
 drivers/gpu/drm/xe/xe_svm.h          | 14 +++++
 drivers/gpu/drm/xe/xe_svm_devmem.c   | 91

++++++++++++++++++++++++++++

Quoted 4 lines
 5 files changed, 124 insertions(+), 1 deletion(-)
 create mode 100644 drivers/gpu/drm/xe/xe_svm.h
 create mode 100644 drivers/gpu/drm/xe/xe_svm_devmem.c
Quoted 13 lines

drivers/gpu/drm/xe/Makefile xe-y += xe_bb.o \ · line 146

Quoted 11 lines
drivers/gpu/drm/xe/Makefile +2 -1
index c531210695db..840467080e59 100644
@@ -142,7 +142,8 @@ xe-y += xe_bb.o \
 	xe_vram_freq.o \
 	xe_wait_user_fence.o \
 	xe_wa.o \
-	xe_wopcm.o
+	xe_wopcm.o \
+	xe_svm_devmem.o

These should be in alphabetical order.

Will fix

Quoted 15 lines

Review

Quoted 5 lines
drivers/gpu/drm/xe/Makefile +0 -0
 # graphics hardware monitoring (HWMON) support
 xe-$(CONFIG_HWMON) += xe_hwmon.o
(quoted diff) +0 -0

b/drivers/gpu/drm/xe/xe_device_types.h

Quoted 9 lines

index 9785eef2e5a4..f27c3bee8ce7 100644

drivers/gpu/drm/xe/xe_device_types.h +2 -0
@@ -99,6 +99,15 @@ struct xe_mem_region {
 	resource_size_t actual_physical_size;
 	/** @mapping: pointer to VRAM mappable space */
 	void __iomem *mapping;
+	/** @pagemap: Used to remap device memory as ZONE_DEVICE */
+    struct dev_pagemap pagemap;
Quoted 8 lines

drivers/gpu/drm/xe/xe_device_types.h struct xe_mem_region { · line 109

Quoted 6 lines
drivers/gpu/drm/xe/xe_device_types.h +6 -0
+    /**
+     * @hpa_base: base host physical address
+     *
+     * This is generated when remap device memory as ZONE_DEVICE
+     */
+    resource_size_t hpa_base;

Weird indentation. This goes for the entire series, look at checkpatch.

Quoted 7 lines

Will fix

Quoted 6 lines

Review

Quoted 6 lines
drivers/gpu/drm/xe/xe_device_types.h +1 -0
+
 };
 
 /**
(quoted diff) +0 -0

b/drivers/gpu/drm/xe/xe_mmio.c

Quoted 14 lines
Quoted 14 lines
Quoted 14 lines

index e3db3a178760..0d795394bc4c 100644

drivers/gpu/drm/xe/xe_mmio.c +1 -0
@@ -22,6 +22,7 @@
 #include "xe_module.h"
 #include "xe_sriov.h"
 #include "xe_tile.h"
+#include "xe_svm.h"
 
 #define XEHP_MTCFG_ADDR		XE_REG(0x101800)
 #define TILE_COUNT		REG_GENMASK(15, 8)
@@ -286,6 +287,7 @@ int xe_mmio_probe_vram(struct xe_device *xe)
 		}
 
Quoted 8 lines
Quoted 6 lines
Quoted 4 lines

drivers/gpu/drm/xe/xe_mmio.c int xe_mmio_probe_vram(struct xe_device *xe) · line 290

Quoted 2 lines
drivers/gpu/drm/xe/xe_mmio.c +1 -0
 		io_size -= min_t(u64, tile_size, io_size);
+		xe_svm_devm_add(tile, &tile->mem.vram);

Do we want to do this probe for all devices with VRAM or only a subset?

All

Can you explain why?

It is natural for me to add all device memory to hmm. In hmm design, device memory is used as a special swap out for system memory. I would ask why we only want to add a subset of vram? By a subset, do you mean only vram of one tile instead of all tiles?

Oak

Quoted 246 lines
Quoted 143 lines
Quoted 12 lines
Quoted 12 lines
drivers/gpu/drm/xe/xe_mmio.c +2 -0
 	}
 
 	xe->mem.vram.actual_physical_size = total_size;
@@ -354,10 +356,16 @@ void xe_mmio_probe_tiles(struct xe_device *xe)
 static void mmio_fini(struct drm_device *drm, void *arg)
 {
 	struct xe_device *xe = arg;
+    struct xe_tile *tile;
+    u8 id;
 
Quoted 9 lines

drivers/gpu/drm/xe/xe_mmio.c void xe_mmio_probe_tiles(struct xe_device *xe) · line 367

Quoted 6 lines
drivers/gpu/drm/xe/xe_mmio.c +3 -0
 	pci_iounmap(to_pci_dev(xe->drm.dev), xe->mmio.regs);
 	if (xe->mem.vram.mapping)
 		iounmap(xe->mem.vram.mapping);
+
+	for_each_tile(tile, xe, id)
+		xe_svm_devm_remove(xe, &tile->mem.vram);

This should probably be above existing code. Typical on fini to do things in reverse order from init.

Will fix

Quoted 4 lines
Quoted 4 lines
drivers/gpu/drm/xe/xe_mmio.c +1 -0
+
 }
 
Quoted 12 lines

drivers/gpu/drm/xe/xe_svm.h line 3

Quoted 10 lines
drivers/gpu/drm/xe/xe_mmio.c +0 -0
 static int xe_verify_lmem_ready(struct xe_device *xe)
drivers/gpu/drm/xe/xe_svm.h new file +3 -0
new file mode 100644
index 000000000000..09f9afb0e7d4
@@ -0,0 +1,14 @@
+// SPDX-License-Identifier: MIT
+/*
+ * Copyright © 2023 Intel Corporation

2024?

This patch was actually written 2023

Quoted 10 lines

drivers/gpu/drm/xe/xe_svm.h line 9

Quoted 7 lines
drivers/gpu/drm/xe/xe_svm.h new file +6 -0
+ */
+
+#ifndef __XE_SVM_H
+#define __XE_SVM_H
+
+#include "xe_device_types.h"

I don't think you need to include this. Rather just forward decl structs used here.

Will fix

Quoted 7 lines

e.g.

struct xe_device;
struct xe_mem_region;
struct xe_tile;

Quoted 7 lines

drivers/gpu/drm/xe/xe_svm.h

Quoted 3 lines
drivers/gpu/drm/xe/xe_svm.h new file +3 -0
+
+int xe_svm_devm_add(struct xe_tile *tile, struct xe_mem_region *mem);
+void xe_svm_devm_remove(struct xe_device *xe, struct xe_mem_region

*mem);

The arguments here are incongruent here. Typically we want these to match.

Will fix

Quoted 7 lines

Review

Quoted 4 lines
drivers/gpu/drm/xe/xe_svm.h new file +2 -0
+
+#endif
(quoted diff) +0 -0

b/drivers/gpu/drm/xe/xe_svm_devmem.c

Incongruent between xe_svm.h and xe_svm_devmem.c.

Did you mean mem vs mr? if yes, will fix

Again these two

Quoted 13 lines

should
match.

drivers/gpu/drm/xe/xe_svm_devmem.c line 3

Quoted 8 lines

new file mode 100644
index 000000000000..63b7a1961cc6

drivers/gpu/drm/xe/xe_svm_devmem.c new file +3 -0
@@ -0,0 +1,91 @@
+// SPDX-License-Identifier: MIT
+/*
+ * Copyright © 2023 Intel Corporation

2024?

It is from 2023

Quoted 10 lines

drivers/gpu/drm/xe/xe_svm_devmem.c line 10

Quoted 8 lines
drivers/gpu/drm/xe/xe_svm_devmem.c new file +7 -0
+ */
+
+#include <linux/mm_types.h>
+#include <linux/sched/mm.h>
+
+#include "xe_device_types.h"
+#include "xe_trace.h"

xe_trace.h appears to be unused.

Will fix

Quoted 12 lines
Quoted 12 lines
drivers/gpu/drm/xe/xe_svm_devmem.c new file +11 -0
+#include "xe_svm.h"
+
+
+static vm_fault_t xe_devm_migrate_to_ram(struct vm_fault *vmf)
+{
+	return 0;
+}
+
+static void xe_devm_page_free(struct page *page)
+{
+}
Quoted 8 lines

drivers/gpu/drm/xe/xe_svm_devmem.c line 27

Quoted 6 lines
drivers/gpu/drm/xe/xe_svm_devmem.c new file +6 -0
+
+static const struct dev_pagemap_ops xe_devm_pagemap_ops = {
+	.page_free = xe_devm_page_free,
+	.migrate_to_ram = xe_devm_migrate_to_ram,
+};
+

Assume these are placeholders that will be populated later?

corrrect

Quoted 14 lines

drivers/gpu/drm/xe/xe_svm_devmem.c line 29

Quoted 3 lines
drivers/gpu/drm/xe/xe_svm_devmem.c new file +2 -0
+/**
+ * xe_svm_devm_add: Remap and provide memmap backing for device

memory

Quoted 9 lines
drivers/gpu/drm/xe/xe_svm_devmem.c new file +9 -0
+ * @tile: tile that the memory region blongs to
+ * @mr: memory region to remap
+ *
+ * This remap device memory to host physical address space and create
+ * struct page to back device memory
+ *
+ * Return: 0 on success standard error code otherwise
+ */
+int xe_svm_devm_add(struct xe_tile *tile, struct xe_mem_region *mr)
Quoted 5 lines
Quoted 3 lines

Here I see the xe_mem_region is from tile->mem.vram, wondering rather than using the tile->mem.vram we should use xe->mem.vram when enabling svm? Isn't the idea behind svm the entire memory is 1 view?

Still need to use tile. The reason is, memory of different tile can have different

characteristics, such as latency. So we want to differentiate memory b/t tiles also in svm. I need to change below " mr->pagemap.owner = tile->xe->drm.dev ". the owner should also be tile. This is the way hmm differentiate memory of different tile.

Quoted 2 lines

With svm it is 1 view, from virtual address space perspective and from physical

struct page perspective. You can think of all the tile's vram is stacked together to form a unified view together with system memory. This doesn't prohibit us from differentiate memory from different tile. This differentiation allow us to optimize performance, i.e., we can wisely place memory in specific tile. If we don't differentiate, this is not possible.

Quoted 1 line

Ok makes sense.

Matt

Quoted 5 lines
Quoted 3 lines

I suppose if we do that we also only use 1 TTM VRAM manager / buddy allocator too. I thought I saw some patches flying around for that too.

Ttm vram manager is not in the picture. We deliberately avoided it per previous

discussion

Quoted 2 lines

Yes same buddy allocator. It is in my previous POC: https://lore.kernel.org/dri-

devel/[email protected]/. I didn't put those patches in this series because I want to merge this small patches separately.

Quoted 71 lines
Quoted 45 lines
Quoted 15 lines
drivers/gpu/drm/xe/xe_svm_devmem.c new file +14 -0
+{
+	struct device *dev = &to_pci_dev(tile->xe->drm.dev)->dev;
+	struct resource *res;
+	void *addr;
+	int ret;
+
+	res = devm_request_free_mem_region(dev, &iomem_resource,
+					   mr->usable_size);
+	if (IS_ERR(res)) {
+		ret = PTR_ERR(res);
+		return ret;
+	}
+
+	mr->pagemap.type = MEMORY_DEVICE_PRIVATE;

drivers/gpu/drm/xe/xe_svm_devmem.c

Quoted 10 lines
drivers/gpu/drm/xe/xe_svm_devmem.c new file +10 -0
+	mr->pagemap.range.start = res->start;
+	mr->pagemap.range.end = res->end;
+	mr->pagemap.nr_range = 1;
+	mr->pagemap.ops = &xe_devm_pagemap_ops;
+	mr->pagemap.owner = tile->xe->drm.dev;
+	addr = devm_memremap_pages(dev, &mr->pagemap);
+	if (IS_ERR(addr)) {
+		devm_release_mem_region(dev, res->start, resource_size(res));
+		ret = PTR_ERR(addr);
+		drm_err(&tile->xe->drm, "Failed to remap tile %d memory,

errno %d\n",

drivers/gpu/drm/xe/xe_svm_devmem.c

Quoted 6 lines
drivers/gpu/drm/xe/xe_svm_devmem.c new file +6 -0
+				tile->id, ret);
+		return ret;
+	}
+	mr->hpa_base = res->start;
+
+	drm_info(&tile->xe->drm, "Added tile %d memory [%llx-%llx] to devm,

remapped to %pr\n",

drivers/gpu/drm/xe/xe_svm_devmem.c line 69

Quoted 1 line
drivers/gpu/drm/xe/xe_svm_devmem.c new file +1 -0
+			tile->id, mr->io_start, mr->io_start + mr->usable_size,

res);

drivers/gpu/drm/xe/xe_svm_devmem.c

Quoted 9 lines
drivers/gpu/drm/xe/xe_svm_devmem.c new file +9 -0
+	return 0;
+}
+
+/**
+ * xe_svm_devm_remove: Unmap device memory and free resources
+ * @xe: xe device
+ * @mr: memory region to remove
+ */
+void xe_svm_devm_remove(struct xe_device *xe, struct xe_mem_region

*mr)

Quoted 13 lines

drivers/gpu/drm/xe/xe_svm_devmem.c line 89

Quoted 11 lines
drivers/gpu/drm/xe/xe_svm_devmem.c new file +11 -0
+{
+	/*FIXME: below cause a kernel hange during moduel remove*/
+#if 0
+	struct device *dev = &to_pci_dev(xe->drm.dev)->dev;
+
+	if (mr->hpa_base) {
+		devm_memunmap_pages(dev, &mr->pagemap);
+		devm_release_mem_region(dev, mr->pagemap.range.start,
+			mr->pagemap.range.end - mr->pagemap.range.start +1);
+	}
+#endif

This would need to be fixed too.

Yes...

Oak

Quoted 8 lines

Matt

Quoted 5 lines
drivers/gpu/drm/xe/xe_svm_devmem.c new file +2 -1
+}
+
--

2.26.3

maintainer
#6 lore raw

Re: [PATCH 1/5] drm/xe/svm: Remap and provide memmap backing for GPU vram

Quoted 120 lines · Oak
Quoted 85 lines

-----Original Message-----
From: Brost, Matthew <[email protected]>
Sent: Thursday, March 14, 2024 4:49 PM
To: Zeng, Oak <[email protected]>
Cc: [email protected]; Hellstrom, Thomas <[email protected]>; [email protected]; Welty, Brian <[email protected]>; Ghimiray, Himal Prasad <[email protected]>
Subject: Re: [PATCH 1/5] drm/xe/svm: Remap and provide memmap backing for GPU vram

Quoted 11 lines · Oak

Hi Matt,

Quoted 9 lines

-----Original Message-----
From: Brost, Matthew <[email protected]>
Sent: Thursday, March 14, 2024 1:18 PM
To: Zeng, Oak <[email protected]>
Cc: [email protected]; Hellstrom, Thomas <[email protected]>; [email protected]; Welty, Brian <[email protected]>; Ghimiray, Himal Prasad <[email protected]>
Subject: Re: [PATCH 1/5] drm/xe/svm: Remap and provide memmap backing

for

Quoted 4 lines
Quoted 4 lines

GPU vram

Quoted 1 line · Oak Zeng

Memory remap GPU vram using devm_memremap_pages, so each GPU

vram

Quoted 36 lines
Quoted 36 lines
Quoted 6 lines

page is backed by a struct page.

Those struct pages are created to allow hmm migrate buffer b/t GPU vram and CPU system memory using existing Linux migration mechanism (i.e., migrating b/t CPU system memory and hard disk).

Quoted 8 lines

This is prepare work to enable svm (shared virtual memory) through Linux kernel hmm framework. The memory remap's page map type is set to MEMORY_DEVICE_PRIVATE for now. This means even though each GPU vram page get a struct page and can be mapped in CPU page table, but such pages are treated as GPU's private resource, so CPU can't access them. If CPU access such page, a page fault is triggered and page will be migrate to system memory.

Is this really true? We can map VRAM BOs to the CPU without having migarte back and forth. Admittedly I don't know the inner workings of how this works but in IGTs we do this all the time.

54 batch_bo = xe_bo_create(fd, vm, batch_size,
55 vram_if_possible(fd, 0),
56 DRM_XE_GEM_CREATE_FLAG_NEEDS_VISIBLE_VRAM);
57 batch_map = xe_bo_map(fd, batch_bo, batch_size);

The BO is created in VRAM and then mapped to the CPU.

I don't think there is an expectation of coherence rather caching mode and exclusive access of the memory based on synchronization.

e.g.
User write BB/data via CPU to GPU memory
User calls exec
GPU read / write memory
User wait on sync indicating exec done
User reads result

Quoted 8 lines
Quoted 6 lines

All of this works without migration. Are we not planing supporting flow with SVM?

Afaik this migration dance really only needs to be done if the CPU and GPU are using atomics on a shared memory region and the GPU device doesn't support a coherent memory protocol (e.g. PVC).

All you said is true. On many of our HW, CPU can actually access device memory,

cache coherently or not.

Quoted 2 lines

The problem is, this is not true for all GPU vendors. For example, on some HW

from some vendor, CPU can only access partially of device memory. The so called small bar concept.

Quoted 2 lines

So when HMM is defined, such factors were considered, and

MEMORY_DEVICE_PRIVATE is defined. With this definition, CPU can't access device memory.

Quoted 1 line
Quoted 8 lines
Quoted 2 lines

So you can think it is a limitation of HMM.

Is it though? I see other type MEMORY_DEVICE_FS_DAX, MEMORY_DEVICE_GENERIC, and MEMORY_DEVICE_PCI_P2PDMA. From my limited
understanding it looks to me like one of those modes would support my example.

No, above are for other purposes. HMM only support DEVICE_PRIVATE and DEVICE_COHERENT.

Quoted 16 lines
Quoted 2 lines

Note this is only part 1 of our system allocator work. We do plan to support

DEVICE_COHERENT for our newer device, see below. With this option, we don't have unnecessary migration back and forth.

Quoted 2 lines

You can think this is just work out all the code path. 90% of the driver code for

DEVICE_PRIVATE and DEVICE_COHERENT will be same. Our real use of system allocator will be DEVICE_COHERENT mode. While DEVICE_PRIVATE mode allow us to exercise the code on old HW.

Quoted 3 lines

Make sense?

I guess if we want the system allocator to always coherent, then yes you need this dynamic migration with faulting on either side.

Quoted 6 lines

I was thinking the system allocator would be behave like my example above with madvise dictating the coherence rules.

Maybe I missed this in system allocator design but my feeling is we shouldn't arbitrarily enforce coherence as that could lead to poor performance due to constant migration.

Quoted 8 lines

System allocator itself doesn't enforce coherence. Coherence is built in user programming model. So system allocator allow both GPU and CPU access system allocated pointers, but it doesn't necessarily guarantee the data accessed from CPU/GPU is coherent. It is user program's responsibility to maintain data coherence.

Data migration in driver is optional, depending on platform capability, user preference, correctness and performance consideration. Driver internal data migration of course shouldn't break data coherence.

Of course different vendor can have different data coherence scheme. For example, it is completely designer's flexibility to build model that is HW automatic data coherence or software explicit data coherence. On our platform, we allow user program to select different coherence mode by setting pat_index for gpu and cpu_caching mode for CPU. So we have completely give the flexibility to user program. Nothing of this contract is changed in system allocator design.

Going back to the question of what memory type we should use to register our vram to core mm. HMM currently support two types: PRIVATE and COHERENT. The coherent type requires some HW and BIOS support which we don't have right now. So the only available is PRIVATE. We have not other option right now. As said, we plan to support coherent type where we can avoid unnecessary data migration. But that is stage 2.

Thanks for the explaination. After reading your replies, the HMM doc, and looking at code this all makes sense.

Quoted 92 lines
Quoted 92 lines
Quoted 65 lines
Quoted 26 lines
Quoted 9 lines

For GPU device which supports coherent memory protocol b/t CPU and GPU (such as CXL and CAPI protocol), we can remap device memory as MEMORY_DEVICE_COHERENT. This is TBD.

Co-developed-by Niranjana Vishwanathapura

<[email protected]>

Quoted 1 line
Signed-off-by Niranjana Vishwanathapura

<[email protected]>

Quoted 9 lines
Cc Matthew Brost <[email protected]>
Cc Thomas Hellström <[email protected]>
Cc Brian Welty <[email protected]>
---
 drivers/gpu/drm/xe/Makefile          |  3 +-
 drivers/gpu/drm/xe/xe_device_types.h |  9 +++
 drivers/gpu/drm/xe/xe_mmio.c         |  8 +++
 drivers/gpu/drm/xe/xe_svm.h          | 14 +++++
 drivers/gpu/drm/xe/xe_svm_devmem.c   | 91

++++++++++++++++++++++++++++

Quoted 4 lines
 5 files changed, 124 insertions(+), 1 deletion(-)
 create mode 100644 drivers/gpu/drm/xe/xe_svm.h
 create mode 100644 drivers/gpu/drm/xe/xe_svm_devmem.c
Quoted 13 lines

drivers/gpu/drm/xe/Makefile xe-y += xe_bb.o \ · line 146

Quoted 11 lines
drivers/gpu/drm/xe/Makefile +2 -1
index c531210695db..840467080e59 100644
@@ -142,7 +142,8 @@ xe-y += xe_bb.o \
 	xe_vram_freq.o \
 	xe_wait_user_fence.o \
 	xe_wa.o \
-	xe_wopcm.o
+	xe_wopcm.o \
+	xe_svm_devmem.o

These should be in alphabetical order.

Will fix

Quoted 15 lines

Review

Quoted 5 lines
drivers/gpu/drm/xe/Makefile +0 -0
 # graphics hardware monitoring (HWMON) support
 xe-$(CONFIG_HWMON) += xe_hwmon.o
(quoted diff) +0 -0

b/drivers/gpu/drm/xe/xe_device_types.h

Quoted 9 lines

index 9785eef2e5a4..f27c3bee8ce7 100644

drivers/gpu/drm/xe/xe_device_types.h +2 -0
@@ -99,6 +99,15 @@ struct xe_mem_region {
 	resource_size_t actual_physical_size;
 	/** @mapping: pointer to VRAM mappable space */
 	void __iomem *mapping;
+	/** @pagemap: Used to remap device memory as ZONE_DEVICE */
+    struct dev_pagemap pagemap;
Quoted 8 lines

drivers/gpu/drm/xe/xe_device_types.h struct xe_mem_region { · line 109

Quoted 6 lines
drivers/gpu/drm/xe/xe_device_types.h +6 -0
+    /**
+     * @hpa_base: base host physical address
+     *
+     * This is generated when remap device memory as ZONE_DEVICE
+     */
+    resource_size_t hpa_base;

Weird indentation. This goes for the entire series, look at checkpatch.

Quoted 7 lines

Will fix

Quoted 6 lines

Review

Quoted 6 lines
drivers/gpu/drm/xe/xe_device_types.h +1 -0
+
 };
 
 /**
(quoted diff) +0 -0

b/drivers/gpu/drm/xe/xe_mmio.c

Quoted 19 lines
Quoted 18 lines
Quoted 12 lines

index e3db3a178760..0d795394bc4c 100644

drivers/gpu/drm/xe/xe_mmio.c +1 -0
@@ -22,6 +22,7 @@
 #include "xe_module.h"
 #include "xe_sriov.h"
 #include "xe_tile.h"
+#include "xe_svm.h"
 
 #define XEHP_MTCFG_ADDR		XE_REG(0x101800)
 #define TILE_COUNT		REG_GENMASK(15, 8)

drivers/gpu/drm/xe/xe_mmio.c int xe_mmio_probe_vram(struct xe_device *xe) · line 290

Quoted 4 lines
drivers/gpu/drm/xe/xe_mmio.c +1 -0
@@ -286,6 +287,7 @@ int xe_mmio_probe_vram(struct xe_device *xe)
 		}
 
 		io_size -= min_t(u64, tile_size, io_size);
+		xe_svm_devm_add(tile, &tile->mem.vram);

Do we want to do this probe for all devices with VRAM or only a subset?

Quoted 6 lines
Quoted 3 lines
Quoted 1 line

All

Can you explain why?

It is natural for me to add all device memory to hmm. In hmm design, device memory is used as a special swap out for system memory. I would ask why we only want to add a subset of vram? By a subset, do you mean only vram of one tile instead of all tiles?

I think we talking about different things, my bad on wording in the original question.

Let me ask again - should be calling xe_svm_devm_add on all *platforms* that have VRAM. i.e. Should we do this on PVC but not DG2?

Matt

Quoted 249 lines

Oak

Quoted 246 lines
Quoted 143 lines
Quoted 12 lines
Quoted 12 lines
drivers/gpu/drm/xe/xe_mmio.c +2 -0
 	}
 
 	xe->mem.vram.actual_physical_size = total_size;
@@ -354,10 +356,16 @@ void xe_mmio_probe_tiles(struct xe_device *xe)
 static void mmio_fini(struct drm_device *drm, void *arg)
 {
 	struct xe_device *xe = arg;
+    struct xe_tile *tile;
+    u8 id;
 
Quoted 9 lines

drivers/gpu/drm/xe/xe_mmio.c void xe_mmio_probe_tiles(struct xe_device *xe) · line 367

Quoted 6 lines
drivers/gpu/drm/xe/xe_mmio.c +3 -0
 	pci_iounmap(to_pci_dev(xe->drm.dev), xe->mmio.regs);
 	if (xe->mem.vram.mapping)
 		iounmap(xe->mem.vram.mapping);
+
+	for_each_tile(tile, xe, id)
+		xe_svm_devm_remove(xe, &tile->mem.vram);

This should probably be above existing code. Typical on fini to do things in reverse order from init.

Will fix

Quoted 4 lines
Quoted 4 lines
drivers/gpu/drm/xe/xe_mmio.c +1 -0
+
 }
 
Quoted 12 lines

drivers/gpu/drm/xe/xe_svm.h line 3

Quoted 10 lines
drivers/gpu/drm/xe/xe_mmio.c +0 -0
 static int xe_verify_lmem_ready(struct xe_device *xe)
drivers/gpu/drm/xe/xe_svm.h new file +3 -0
new file mode 100644
index 000000000000..09f9afb0e7d4
@@ -0,0 +1,14 @@
+// SPDX-License-Identifier: MIT
+/*
+ * Copyright © 2023 Intel Corporation

2024?

This patch was actually written 2023

Quoted 10 lines

drivers/gpu/drm/xe/xe_svm.h line 9

Quoted 7 lines
drivers/gpu/drm/xe/xe_svm.h new file +6 -0
+ */
+
+#ifndef __XE_SVM_H
+#define __XE_SVM_H
+
+#include "xe_device_types.h"

I don't think you need to include this. Rather just forward decl structs used here.

Will fix

Quoted 7 lines

e.g.

struct xe_device;
struct xe_mem_region;
struct xe_tile;

Quoted 7 lines

drivers/gpu/drm/xe/xe_svm.h

Quoted 3 lines
drivers/gpu/drm/xe/xe_svm.h new file +3 -0
+
+int xe_svm_devm_add(struct xe_tile *tile, struct xe_mem_region *mem);
+void xe_svm_devm_remove(struct xe_device *xe, struct xe_mem_region

*mem);

The arguments here are incongruent here. Typically we want these to match.

Will fix

Quoted 7 lines

Review

Quoted 4 lines
drivers/gpu/drm/xe/xe_svm.h new file +2 -0
+
+#endif
(quoted diff) +0 -0

b/drivers/gpu/drm/xe/xe_svm_devmem.c

Incongruent between xe_svm.h and xe_svm_devmem.c.

Did you mean mem vs mr? if yes, will fix

Again these two

Quoted 13 lines

should
match.

drivers/gpu/drm/xe/xe_svm_devmem.c line 3

Quoted 8 lines

new file mode 100644
index 000000000000..63b7a1961cc6

drivers/gpu/drm/xe/xe_svm_devmem.c new file +3 -0
@@ -0,0 +1,91 @@
+// SPDX-License-Identifier: MIT
+/*
+ * Copyright © 2023 Intel Corporation

2024?

It is from 2023

Quoted 10 lines

drivers/gpu/drm/xe/xe_svm_devmem.c line 10

Quoted 8 lines
drivers/gpu/drm/xe/xe_svm_devmem.c new file +7 -0
+ */
+
+#include <linux/mm_types.h>
+#include <linux/sched/mm.h>
+
+#include "xe_device_types.h"
+#include "xe_trace.h"

xe_trace.h appears to be unused.

Will fix

Quoted 12 lines
Quoted 12 lines
drivers/gpu/drm/xe/xe_svm_devmem.c new file +11 -0
+#include "xe_svm.h"
+
+
+static vm_fault_t xe_devm_migrate_to_ram(struct vm_fault *vmf)
+{
+	return 0;
+}
+
+static void xe_devm_page_free(struct page *page)
+{
+}
Quoted 8 lines

drivers/gpu/drm/xe/xe_svm_devmem.c line 27

Quoted 6 lines
drivers/gpu/drm/xe/xe_svm_devmem.c new file +6 -0
+
+static const struct dev_pagemap_ops xe_devm_pagemap_ops = {
+	.page_free = xe_devm_page_free,
+	.migrate_to_ram = xe_devm_migrate_to_ram,
+};
+

Assume these are placeholders that will be populated later?

corrrect

Quoted 14 lines

drivers/gpu/drm/xe/xe_svm_devmem.c line 29

Quoted 3 lines
drivers/gpu/drm/xe/xe_svm_devmem.c new file +2 -0
+/**
+ * xe_svm_devm_add: Remap and provide memmap backing for device

memory

Quoted 9 lines
drivers/gpu/drm/xe/xe_svm_devmem.c new file +9 -0
+ * @tile: tile that the memory region blongs to
+ * @mr: memory region to remap
+ *
+ * This remap device memory to host physical address space and create
+ * struct page to back device memory
+ *
+ * Return: 0 on success standard error code otherwise
+ */
+int xe_svm_devm_add(struct xe_tile *tile, struct xe_mem_region *mr)
Quoted 5 lines
Quoted 3 lines

Here I see the xe_mem_region is from tile->mem.vram, wondering rather than using the tile->mem.vram we should use xe->mem.vram when enabling svm? Isn't the idea behind svm the entire memory is 1 view?

Still need to use tile. The reason is, memory of different tile can have different

characteristics, such as latency. So we want to differentiate memory b/t tiles also in svm. I need to change below " mr->pagemap.owner = tile->xe->drm.dev ". the owner should also be tile. This is the way hmm differentiate memory of different tile.

Quoted 2 lines

With svm it is 1 view, from virtual address space perspective and from physical

struct page perspective. You can think of all the tile's vram is stacked together to form a unified view together with system memory. This doesn't prohibit us from differentiate memory from different tile. This differentiation allow us to optimize performance, i.e., we can wisely place memory in specific tile. If we don't differentiate, this is not possible.

Quoted 1 line

Ok makes sense.

Matt

Quoted 5 lines
Quoted 3 lines

I suppose if we do that we also only use 1 TTM VRAM manager / buddy allocator too. I thought I saw some patches flying around for that too.

Ttm vram manager is not in the picture. We deliberately avoided it per previous

discussion

Quoted 2 lines

Yes same buddy allocator. It is in my previous POC: https://lore.kernel.org/dri-

devel/[email protected]/. I didn't put those patches in this series because I want to merge this small patches separately.

Quoted 71 lines
Quoted 45 lines
Quoted 15 lines
drivers/gpu/drm/xe/xe_svm_devmem.c new file +14 -0
+{
+	struct device *dev = &to_pci_dev(tile->xe->drm.dev)->dev;
+	struct resource *res;
+	void *addr;
+	int ret;
+
+	res = devm_request_free_mem_region(dev, &iomem_resource,
+					   mr->usable_size);
+	if (IS_ERR(res)) {
+		ret = PTR_ERR(res);
+		return ret;
+	}
+
+	mr->pagemap.type = MEMORY_DEVICE_PRIVATE;

drivers/gpu/drm/xe/xe_svm_devmem.c

Quoted 10 lines
drivers/gpu/drm/xe/xe_svm_devmem.c new file +10 -0
+	mr->pagemap.range.start = res->start;
+	mr->pagemap.range.end = res->end;
+	mr->pagemap.nr_range = 1;
+	mr->pagemap.ops = &xe_devm_pagemap_ops;
+	mr->pagemap.owner = tile->xe->drm.dev;
+	addr = devm_memremap_pages(dev, &mr->pagemap);
+	if (IS_ERR(addr)) {
+		devm_release_mem_region(dev, res->start, resource_size(res));
+		ret = PTR_ERR(addr);
+		drm_err(&tile->xe->drm, "Failed to remap tile %d memory,

errno %d\n",

drivers/gpu/drm/xe/xe_svm_devmem.c

Quoted 6 lines
drivers/gpu/drm/xe/xe_svm_devmem.c new file +6 -0
+				tile->id, ret);
+		return ret;
+	}
+	mr->hpa_base = res->start;
+
+	drm_info(&tile->xe->drm, "Added tile %d memory [%llx-%llx] to devm,

remapped to %pr\n",

drivers/gpu/drm/xe/xe_svm_devmem.c line 69

Quoted 1 line
drivers/gpu/drm/xe/xe_svm_devmem.c new file +1 -0
+			tile->id, mr->io_start, mr->io_start + mr->usable_size,

res);

drivers/gpu/drm/xe/xe_svm_devmem.c

Quoted 9 lines
drivers/gpu/drm/xe/xe_svm_devmem.c new file +9 -0
+	return 0;
+}
+
+/**
+ * xe_svm_devm_remove: Unmap device memory and free resources
+ * @xe: xe device
+ * @mr: memory region to remove
+ */
+void xe_svm_devm_remove(struct xe_device *xe, struct xe_mem_region

*mr)

Quoted 13 lines

drivers/gpu/drm/xe/xe_svm_devmem.c line 89

Quoted 11 lines
drivers/gpu/drm/xe/xe_svm_devmem.c new file +11 -0
+{
+	/*FIXME: below cause a kernel hange during moduel remove*/
+#if 0
+	struct device *dev = &to_pci_dev(xe->drm.dev)->dev;
+
+	if (mr->hpa_base) {
+		devm_memunmap_pages(dev, &mr->pagemap);
+		devm_release_mem_region(dev, mr->pagemap.range.start,
+			mr->pagemap.range.end - mr->pagemap.range.start +1);
+	}
+#endif

This would need to be fixed too.

Yes...

Oak

Quoted 8 lines

Matt

Quoted 5 lines
drivers/gpu/drm/xe/xe_svm_devmem.c new file +2 -1
+}
+
--

2.26.3

author
#7 lore raw

RE: [PATCH 1/5] drm/xe/svm: Remap and provide memmap backing for GPU vram

Quoted 279 lines

-----Original Message-----
From: Brost, Matthew <[email protected]>
Sent: Friday, March 15, 2024 4:40 PM
To: Zeng, Oak <[email protected]>
Cc: [email protected]; Hellstrom, Thomas <[email protected]>; [email protected]; Welty, Brian <[email protected]>; Ghimiray, Himal Prasad <[email protected]>
Subject: Re: [PATCH 1/5] drm/xe/svm: Remap and provide memmap backing for GPU vram

Quoted 11 lines · Oak
Quoted 11 lines

-----Original Message-----
From: Brost, Matthew <[email protected]>
Sent: Thursday, March 14, 2024 4:49 PM
To: Zeng, Oak <[email protected]>
Cc: [email protected]; Hellstrom, Thomas <[email protected]>; [email protected]; Welty, Brian <[email protected]>; Ghimiray, Himal Prasad <[email protected]>
Subject: Re: [PATCH 1/5] drm/xe/svm: Remap and provide memmap backing

for

Quoted 5 lines
Quoted 5 lines

GPU vram

Quoted 2 lines · Oak

Hi Matt,

Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines

-----Original Message-----
From: Brost, Matthew <[email protected]>
Sent: Thursday, March 14, 2024 1:18 PM
To: Zeng, Oak <[email protected]>
Cc: [email protected]; Hellstrom, Thomas <[email protected]>; [email protected]; Welty, Brian <[email protected]>; Ghimiray, Himal Prasad <[email protected]>
Subject: Re: [PATCH 1/5] drm/xe/svm: Remap and provide memmap

backing

Quoted 5 lines
Quoted 5 lines

for

Quoted 4 lines
Quoted 4 lines

GPU vram

Quoted 1 line · Oak Zeng

Memory remap GPU vram using devm_memremap_pages, so each

GPU

Quoted 9 lines
Quoted 9 lines

vram

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

page is backed by a struct page.

Those struct pages are created to allow hmm migrate buffer b/t GPU vram and CPU system memory using existing Linux migration mechanism (i.e., migrating b/t CPU system memory and hard disk).

This is prepare work to enable svm (shared virtual memory) through Linux kernel hmm framework. The memory remap's page map type is

set

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

to MEMORY_DEVICE_PRIVATE for now. This means even though each

GPU

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 5 lines

vram page get a struct page and can be mapped in CPU page table, but such pages are treated as GPU's private resource, so CPU can't access them. If CPU access such page, a page fault is triggered and page will be migrate to system memory.

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines

Is this really true? We can map VRAM BOs to the CPU without having migarte back and forth. Admittedly I don't know the inner workings of how this works but in IGTs we do this all the time.

54 batch_bo = xe_bo_create(fd, vm, batch_size,
55 vram_if_possible(fd, 0),
56

DRM_XE_GEM_CREATE_FLAG_NEEDS_VISIBLE_VRAM);

Quoted 14 lines
Quoted 14 lines
Quoted 14 lines
Quoted 14 lines
  57         batch_map = xe_bo_map(fd, batch_bo, batch_size);

The BO is created in VRAM and then mapped to the CPU.

I don't think there is an expectation of coherence rather caching mode and exclusive access of the memory based on synchronization.

e.g.
User write BB/data via CPU to GPU memory
User calls exec
GPU read / write memory
User wait on sync indicating exec done
User reads result

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 6 lines

All of this works without migration. Are we not planing supporting flow with SVM?

Afaik this migration dance really only needs to be done if the CPU and GPU are using atomics on a shared memory region and the GPU device doesn't support a coherent memory protocol (e.g. PVC).

All you said is true. On many of our HW, CPU can actually access device

memory,

Quoted 3 lines
Quoted 3 lines

cache coherently or not.

Quoted 2 lines

The problem is, this is not true for all GPU vendors. For example, on some

HW

Quoted 1 line
Quoted 1 line

from some vendor, CPU can only access partially of device memory. The so

called

Quoted 9 lines
Quoted 9 lines

small bar concept.

Quoted 2 lines

So when HMM is defined, such factors were considered, and

MEMORY_DEVICE_PRIVATE is defined. With this definition, CPU can't access device memory.

Quoted 3 lines

So you can think it is a limitation of HMM.

Quoted 8 lines
Quoted 5 lines

Is it though? I see other type MEMORY_DEVICE_FS_DAX, MEMORY_DEVICE_GENERIC, and MEMORY_DEVICE_PCI_P2PDMA. From my limited
understanding it looks to me like one of those modes would support my example.

No, above are for other purposes. HMM only support DEVICE_PRIVATE and

DEVICE_COHERENT.

Quoted 4 lines
Quoted 4 lines
Quoted 3 lines

Note this is only part 1 of our system allocator work. We do plan to support

DEVICE_COHERENT for our newer device, see below. With this option, we

don't

Quoted 3 lines
Quoted 3 lines

have unnecessary migration back and forth.

Quoted 2 lines

You can think this is just work out all the code path. 90% of the driver code

for

Quoted 2 lines
Quoted 2 lines

DEVICE_PRIVATE and DEVICE_COHERENT will be same. Our real use of system allocator will be DEVICE_COHERENT mode. While DEVICE_PRIVATE mode

allow us

Quoted 8 lines
Quoted 8 lines

to exercise the code on old HW.

Quoted 3 lines

Make sense?

I guess if we want the system allocator to always coherent, then yes you need this dynamic migration with faulting on either side.

Quoted 8 lines
Quoted 6 lines

I was thinking the system allocator would be behave like my example above with madvise dictating the coherence rules.

Maybe I missed this in system allocator design but my feeling is we shouldn't arbitrarily enforce coherence as that could lead to poor performance due to constant migration.

System allocator itself doesn't enforce coherence. Coherence is built in user

programming model. So system allocator allow both GPU and CPU access system allocated pointers, but it doesn't necessarily guarantee the data accessed from CPU/GPU is coherent. It is user program's responsibility to maintain data coherence.

Quoted 2 lines

Data migration in driver is optional, depending on platform capability, user

preference, correctness and performance consideration. Driver internal data migration of course shouldn't break data coherence.

Quoted 2 lines

Of course different vendor can have different data coherence scheme. For

example, it is completely designer's flexibility to build model that is HW automatic data coherence or software explicit data coherence. On our platform, we allow user program to select different coherence mode by setting pat_index for gpu and cpu_caching mode for CPU. So we have completely give the flexibility to user program. Nothing of this contract is changed in system allocator design.

Quoted 2 lines

Going back to the question of what memory type we should use to register our

vram to core mm. HMM currently support two types: PRIVATE and COHERENT. The coherent type requires some HW and BIOS support which we don't have right now. So the only available is PRIVATE. We have not other option right now. As said, we plan to support coherent type where we can avoid unnecessary data migration. But that is stage 2.

Quoted 1 line

Thanks for the explaination. After reading your replies, the HMM doc, and looking at code this all makes sense.

Quoted 21 lines
Quoted 21 lines
Quoted 21 lines
Quoted 21 lines
Quoted 9 lines

For GPU device which supports coherent memory protocol b/t CPU and GPU (such as CXL and CAPI protocol), we can remap device memory as MEMORY_DEVICE_COHERENT. This is TBD.

Co-developed-by Niranjana Vishwanathapura

<[email protected]>

Quoted 1 line
Signed-off-by Niranjana Vishwanathapura

<[email protected]>

Quoted 9 lines
Cc Matthew Brost <[email protected]>
Cc Thomas Hellström <[email protected]>
Cc Brian Welty <[email protected]>
---
 drivers/gpu/drm/xe/Makefile          |  3 +-
 drivers/gpu/drm/xe/xe_device_types.h |  9 +++
 drivers/gpu/drm/xe/xe_mmio.c         |  8 +++
 drivers/gpu/drm/xe/xe_svm.h          | 14 +++++
 drivers/gpu/drm/xe/xe_svm_devmem.c   | 91
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines

++++++++++++++++++++++++++++

Review

Quoted 5 lines
 5 files changed, 124 insertions(+), 1 deletion(-)
 create mode 100644 drivers/gpu/drm/xe/xe_svm.h
 create mode 100644 drivers/gpu/drm/xe/xe_svm_devmem.c
(quoted diff) +0 -0

b/drivers/gpu/drm/xe/Makefile

Quoted 20 lines
Quoted 20 lines
Quoted 20 lines
Quoted 4 lines
Quoted 4 lines

index c531210695db..840467080e59 100644

drivers/gpu/drm/xe/Makefile +0 -0
@@ -142,7 +142,8 @@ xe-y += xe_bb.o \
Quoted 8 lines

drivers/gpu/drm/xe/Makefile xe-y += xe_bb.o \ · line 146

Quoted 6 lines
drivers/gpu/drm/xe/Makefile +2 -1
 	xe_vram_freq.o \
 	xe_wait_user_fence.o \
 	xe_wa.o \
-	xe_wopcm.o
+	xe_wopcm.o \
+	xe_svm_devmem.o

These should be in alphabetical order.

Will fix

Quoted 6 lines

Review

Quoted 5 lines
drivers/gpu/drm/xe/Makefile +0 -0
 # graphics hardware monitoring (HWMON) support
 xe-$(CONFIG_HWMON) += xe_hwmon.o
(quoted diff) +0 -0

b/drivers/gpu/drm/xe/xe_device_types.h

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

drivers/gpu/drm/xe/xe_device_types.h struct xe_mem_region { · line 102

Quoted 8 lines

index 9785eef2e5a4..f27c3bee8ce7 100644

drivers/gpu/drm/xe/xe_device_types.h +1 -0
@@ -99,6 +99,15 @@ struct xe_mem_region {
 	resource_size_t actual_physical_size;
 	/** @mapping: pointer to VRAM mappable space */
 	void __iomem *mapping;
+	/** @pagemap: Used to remap device memory as ZONE_DEVICE

*/

Quoted 37 lines
Quoted 37 lines
Quoted 10 lines
Quoted 9 lines

drivers/gpu/drm/xe/xe_device_types.h struct xe_mem_region { · line 109

Quoted 7 lines
drivers/gpu/drm/xe/xe_device_types.h +7 -0
+    struct dev_pagemap pagemap;
+    /**
+     * @hpa_base: base host physical address
+     *
+     * This is generated when remap device memory as ZONE_DEVICE
+     */
+    resource_size_t hpa_base;

Weird indentation. This goes for the entire series, look at checkpatch.

Quoted 7 lines

Will fix

Quoted 6 lines

Review

Quoted 6 lines
drivers/gpu/drm/xe/xe_device_types.h +1 -0
+
 };
 
 /**
(quoted diff) +0 -0

b/drivers/gpu/drm/xe/xe_mmio.c

Quoted 19 lines
Quoted 18 lines
Quoted 12 lines

index e3db3a178760..0d795394bc4c 100644

drivers/gpu/drm/xe/xe_mmio.c +1 -0
@@ -22,6 +22,7 @@
 #include "xe_module.h"
 #include "xe_sriov.h"
 #include "xe_tile.h"
+#include "xe_svm.h"
 
 #define XEHP_MTCFG_ADDR		XE_REG(0x101800)
 #define TILE_COUNT		REG_GENMASK(15, 8)

drivers/gpu/drm/xe/xe_mmio.c int xe_mmio_probe_vram(struct xe_device *xe) · line 290

Quoted 4 lines
drivers/gpu/drm/xe/xe_mmio.c +1 -0
@@ -286,6 +287,7 @@ int xe_mmio_probe_vram(struct xe_device *xe)
 		}
 
 		io_size -= min_t(u64, tile_size, io_size);
+		xe_svm_devm_add(tile, &tile->mem.vram);

Do we want to do this probe for all devices with VRAM or only a subset?

Quoted 5 lines
Quoted 3 lines
Quoted 1 line

All

Can you explain why?

It is natural for me to add all device memory to hmm. In hmm design, device

memory is used as a special swap out for system memory. I would ask why we only want to add a subset of vram? By a subset, do you mean only vram of one tile instead of all tiles?

Quoted 1 line
Quoted 5 lines

I think we talking about different things, my bad on wording in the original question.

Let me ask again - should be calling xe_svm_devm_add on all *platforms* that have VRAM. i.e. Should we do this on PVC but not DG2?

Oh, I see. Good question. On i915, this feature was only tested on PVC. We don't have a plan to enable it on older platform than PVC.

Let me add a check here, only enabled it on platform newer than PVC

Oak

Quoted 276 lines

Matt

Quoted 9 lines

Oak

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines

drivers/gpu/drm/xe/xe_mmio.c void xe_mmio_probe_tiles(struct xe_device

Quoted 6 lines
drivers/gpu/drm/xe/xe_mmio.c +0 -0
 	}
 
 	xe->mem.vram.actual_physical_size = total_size;
@@ -354,10 +356,16 @@ void xe_mmio_probe_tiles(struct xe_device

*xe)

Quoted 16 lines
Quoted 16 lines
Quoted 16 lines
Quoted 15 lines

drivers/gpu/drm/xe/xe_mmio.c void xe_mmio_probe_tiles(struct xe_device *xe) · line 367

Quoted 12 lines
drivers/gpu/drm/xe/xe_mmio.c +5 -0
 static void mmio_fini(struct drm_device *drm, void *arg)
 {
 	struct xe_device *xe = arg;
+    struct xe_tile *tile;
+    u8 id;
 
 	pci_iounmap(to_pci_dev(xe->drm.dev), xe->mmio.regs);
 	if (xe->mem.vram.mapping)
 		iounmap(xe->mem.vram.mapping);
+
+	for_each_tile(tile, xe, id)
+		xe_svm_devm_remove(xe, &tile->mem.vram);

This should probably be above existing code. Typical on fini to do things in reverse order from init.

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines

Will fix

Quoted 6 lines

Review

Quoted 6 lines
drivers/gpu/drm/xe/xe_mmio.c +1 -0
+
 }
 
 static int xe_verify_lmem_ready(struct xe_device *xe)
(quoted diff) +0 -0

b/drivers/gpu/drm/xe/xe_svm.h

Quoted 25 lines
Quoted 25 lines
Quoted 25 lines
Quoted 10 lines

drivers/gpu/drm/xe/xe_svm.h line 3

Quoted 8 lines

new file mode 100644
index 000000000000..09f9afb0e7d4

drivers/gpu/drm/xe/xe_svm.h new file +3 -0
@@ -0,0 +1,14 @@
+// SPDX-License-Identifier: MIT
+/*
+ * Copyright © 2023 Intel Corporation

2024?

This patch was actually written 2023

Quoted 10 lines

drivers/gpu/drm/xe/xe_svm.h line 9

Quoted 7 lines
drivers/gpu/drm/xe/xe_svm.h new file +6 -0
+ */
+
+#ifndef __XE_SVM_H
+#define __XE_SVM_H
+
+#include "xe_device_types.h"

I don't think you need to include this. Rather just forward decl structs used here.

Will fix

Quoted 1 line
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

e.g.

struct xe_device;
struct xe_mem_region;
struct xe_tile;

drivers/gpu/drm/xe/xe_svm.h line 11

Quoted 2 lines
drivers/gpu/drm/xe/xe_svm.h new file +2 -0
+
+int xe_svm_devm_add(struct xe_tile *tile, struct xe_mem_region

*mem);

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

drivers/gpu/drm/xe/xe_svm.h line 12

Quoted 1 line
drivers/gpu/drm/xe/xe_svm.h new file +1 -0
+void xe_svm_devm_remove(struct xe_device *xe, struct

xe_mem_region

Quoted 65 lines
Quoted 65 lines
Quoted 65 lines
Quoted 4 lines

*mem);

The arguments here are incongruent here. Typically we want these to match.

Will fix

Quoted 7 lines

Review

Quoted 4 lines
drivers/gpu/drm/xe/xe_svm.h new file +2 -0
+
+#endif
(quoted diff) +0 -0

b/drivers/gpu/drm/xe/xe_svm_devmem.c

Incongruent between xe_svm.h and xe_svm_devmem.c.

Did you mean mem vs mr? if yes, will fix

Again these two

Quoted 13 lines

should
match.

drivers/gpu/drm/xe/xe_svm_devmem.c line 3

Quoted 8 lines

new file mode 100644
index 000000000000..63b7a1961cc6

drivers/gpu/drm/xe/xe_svm_devmem.c new file +3 -0
@@ -0,0 +1,91 @@
+// SPDX-License-Identifier: MIT
+/*
+ * Copyright © 2023 Intel Corporation

2024?

It is from 2023

Quoted 10 lines

drivers/gpu/drm/xe/xe_svm_devmem.c line 10

Quoted 8 lines
drivers/gpu/drm/xe/xe_svm_devmem.c new file +7 -0
+ */
+
+#include <linux/mm_types.h>
+#include <linux/sched/mm.h>
+
+#include "xe_device_types.h"
+#include "xe_trace.h"

xe_trace.h appears to be unused.

Will fix

Quoted 20 lines
Quoted 8 lines
drivers/gpu/drm/xe/xe_svm_devmem.c new file +7 -0
+#include "xe_svm.h"
+
+
+static vm_fault_t xe_devm_migrate_to_ram(struct vm_fault *vmf)
+{
+	return 0;
+}

drivers/gpu/drm/xe/xe_svm_devmem.c line 27

Quoted 10 lines
drivers/gpu/drm/xe/xe_svm_devmem.c new file +10 -0
+
+static void xe_devm_page_free(struct page *page)
+{
+}
+
+static const struct dev_pagemap_ops xe_devm_pagemap_ops = {
+	.page_free = xe_devm_page_free,
+	.migrate_to_ram = xe_devm_migrate_to_ram,
+};
+

Assume these are placeholders that will be populated later?

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines

corrrect

Quoted 3 lines
Quoted 3 lines
drivers/gpu/drm/xe/xe_svm_devmem.c new file +2 -0
+/**
+ * xe_svm_devm_add: Remap and provide memmap backing for

device

Quoted 11 lines
Quoted 11 lines
Quoted 11 lines
Quoted 11 lines

memory

Quoted 9 lines
drivers/gpu/drm/xe/xe_svm_devmem.c new file +9 -0
+ * @tile: tile that the memory region blongs to
+ * @mr: memory region to remap
+ *
+ * This remap device memory to host physical address space and create
+ * struct page to back device memory
+ *
+ * Return: 0 on success standard error code otherwise
+ */
+int xe_svm_devm_add(struct xe_tile *tile, struct xe_mem_region *mr)
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

Here I see the xe_mem_region is from tile->mem.vram, wondering rather than using the tile->mem.vram we should use xe->mem.vram when

enabling

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 1 line

svm? Isn't the idea behind svm the entire memory is 1 view?

Still need to use tile. The reason is, memory of different tile can have

different

Quoted 1 line
Quoted 1 line

characteristics, such as latency. So we want to differentiate memory b/t tiles

also

Quoted 1 line
Quoted 1 line

in svm. I need to change below " mr->pagemap.owner = tile->xe->drm.dev ".

the

Quoted 1 line
Quoted 1 line

owner should also be tile. This is the way hmm differentiate memory of

different

Quoted 3 lines
Quoted 3 lines

tile.

Quoted 2 lines

With svm it is 1 view, from virtual address space perspective and from

physical

Quoted 1 line
Quoted 1 line

struct page perspective. You can think of all the tile's vram is stacked together

to

Quoted 1 line
Quoted 1 line

form a unified view together with system memory. This doesn't prohibit us

from

Quoted 1 line
Quoted 1 line

differentiate memory from different tile. This differentiation allow us to

optimize

Quoted 6 lines
Quoted 6 lines

performance, i.e., we can wisely place memory in specific tile. If we don't differentiate, this is not possible.

Quoted 1 line

Ok makes sense.

Quoted 7 lines
Quoted 7 lines

Matt

Quoted 5 lines
Quoted 3 lines

I suppose if we do that we also only use 1 TTM VRAM manager / buddy allocator too. I thought I saw some patches flying around for that too.

Ttm vram manager is not in the picture. We deliberately avoided it per

previous

Quoted 3 lines
Quoted 3 lines

discussion

Quoted 2 lines

Yes same buddy allocator. It is in my previous POC:

https://lore.kernel.org/dri-

Quoted 1 line
Quoted 1 line

devel/[email protected]/. I didn't put those

patches

Quoted 16 lines
Quoted 16 lines

in this series because I want to merge this small patches separately.

Quoted 15 lines
Quoted 15 lines
Quoted 15 lines
drivers/gpu/drm/xe/xe_svm_devmem.c new file +14 -0
+{
+	struct device *dev = &to_pci_dev(tile->xe->drm.dev)->dev;
+	struct resource *res;
+	void *addr;
+	int ret;
+
+	res = devm_request_free_mem_region(dev, &iomem_resource,
+					   mr->usable_size);
+	if (IS_ERR(res)) {
+		ret = PTR_ERR(res);
+		return ret;
+	}
+
+	mr->pagemap.type = MEMORY_DEVICE_PRIVATE;
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

drivers/gpu/drm/xe/xe_svm_devmem.c

Quoted 8 lines
drivers/gpu/drm/xe/xe_svm_devmem.c new file +8 -0
+	mr->pagemap.range.start = res->start;
+	mr->pagemap.range.end = res->end;
+	mr->pagemap.nr_range = 1;
+	mr->pagemap.ops = &xe_devm_pagemap_ops;
+	mr->pagemap.owner = tile->xe->drm.dev;
+	addr = devm_memremap_pages(dev, &mr->pagemap);
+	if (IS_ERR(addr)) {
+		devm_release_mem_region(dev, res->start,

resource_size(res));

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

drivers/gpu/drm/xe/xe_svm_devmem.c line 62

Quoted 2 lines
drivers/gpu/drm/xe/xe_svm_devmem.c new file +2 -0
+		ret = PTR_ERR(addr);
+		drm_err(&tile->xe->drm, "Failed to remap tile %d

memory,

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines

errno %d\n",

drivers/gpu/drm/xe/xe_svm_devmem.c

Quoted 6 lines
drivers/gpu/drm/xe/xe_svm_devmem.c new file +6 -0
+				tile->id, ret);
+		return ret;
+	}
+	mr->hpa_base = res->start;
+
+	drm_info(&tile->xe->drm, "Added tile %d memory [%llx-%llx] to

devm,

Quoted 4 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

remapped to %pr\n",

drivers/gpu/drm/xe/xe_svm_devmem.c line 69

Quoted 1 line
drivers/gpu/drm/xe/xe_svm_devmem.c new file +1 -0
+			tile->id, mr->io_start, mr->io_start + mr-

usable_size,

Quoted 1 line
Quoted 1 line
Quoted 1 line

res);

Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines

drivers/gpu/drm/xe/xe_svm_devmem.c

Quoted 9 lines
drivers/gpu/drm/xe/xe_svm_devmem.c new file +9 -0
+	return 0;
+}
+
+/**
+ * xe_svm_devm_remove: Unmap device memory and free resources
+ * @xe: xe device
+ * @mr: memory region to remove
+ */
+void xe_svm_devm_remove(struct xe_device *xe, struct

xe_mem_region

Quoted 29 lines
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines

*mr)

drivers/gpu/drm/xe/xe_svm_devmem.c

Quoted 8 lines
drivers/gpu/drm/xe/xe_svm_devmem.c new file +8 -0
+{
+	/*FIXME: below cause a kernel hange during moduel remove*/
+#if 0
+	struct device *dev = &to_pci_dev(xe->drm.dev)->dev;
+
+	if (mr->hpa_base) {
+		devm_memunmap_pages(dev, &mr->pagemap);
+		devm_release_mem_region(dev, mr-

pagemap.range.start,

Quoted 1 line
Quoted 1 line
Quoted 1 line

drivers/gpu/drm/xe/xe_svm_devmem.c line 87

Quoted 1 line
drivers/gpu/drm/xe/xe_svm_devmem.c new file +1 -0
+			mr->pagemap.range.end - mr-

pagemap.range.start +1);

Quoted 17 lines
Quoted 17 lines
Quoted 4 lines

drivers/gpu/drm/xe/xe_svm_devmem.c line 89

Quoted 2 lines
drivers/gpu/drm/xe/xe_svm_devmem.c new file +2 -0
+	}
+#endif

This would need to be fixed too.

Yes...

Oak

Quoted 8 lines

Matt

Quoted 5 lines
drivers/gpu/drm/xe/xe_svm_devmem.c new file +2 -1
+}
+
--

2.26.3

maintainer
#8 lore raw

Re: [PATCH 1/5] drm/xe/svm: Remap and provide memmap backing for GPU vram

Quoted 282 lines · Oak
Quoted 282 lines

-----Original Message-----
From: Brost, Matthew <[email protected]>
Sent: Friday, March 15, 2024 4:40 PM
To: Zeng, Oak <[email protected]>
Cc: [email protected]; Hellstrom, Thomas <[email protected]>; [email protected]; Welty, Brian <[email protected]>; Ghimiray, Himal Prasad <[email protected]>
Subject: Re: [PATCH 1/5] drm/xe/svm: Remap and provide memmap backing for GPU vram

Quoted 11 lines · Oak
Quoted 11 lines

-----Original Message-----
From: Brost, Matthew <[email protected]>
Sent: Thursday, March 14, 2024 4:49 PM
To: Zeng, Oak <[email protected]>
Cc: [email protected]; Hellstrom, Thomas <[email protected]>; [email protected]; Welty, Brian <[email protected]>; Ghimiray, Himal Prasad <[email protected]>
Subject: Re: [PATCH 1/5] drm/xe/svm: Remap and provide memmap backing

for

Quoted 5 lines
Quoted 5 lines

GPU vram

Quoted 2 lines · Oak

Hi Matt,

Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines

-----Original Message-----
From: Brost, Matthew <[email protected]>
Sent: Thursday, March 14, 2024 1:18 PM
To: Zeng, Oak <[email protected]>
Cc: [email protected]; Hellstrom, Thomas <[email protected]>; [email protected]; Welty, Brian <[email protected]>; Ghimiray, Himal Prasad <[email protected]>
Subject: Re: [PATCH 1/5] drm/xe/svm: Remap and provide memmap

backing

Quoted 5 lines
Quoted 5 lines

for

Quoted 4 lines
Quoted 4 lines

GPU vram

Quoted 1 line · Oak Zeng

Memory remap GPU vram using devm_memremap_pages, so each

GPU

Quoted 9 lines
Quoted 9 lines

vram

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

page is backed by a struct page.

Those struct pages are created to allow hmm migrate buffer b/t GPU vram and CPU system memory using existing Linux migration mechanism (i.e., migrating b/t CPU system memory and hard disk).

This is prepare work to enable svm (shared virtual memory) through Linux kernel hmm framework. The memory remap's page map type is

set

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

to MEMORY_DEVICE_PRIVATE for now. This means even though each

GPU

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 5 lines

vram page get a struct page and can be mapped in CPU page table, but such pages are treated as GPU's private resource, so CPU can't access them. If CPU access such page, a page fault is triggered and page will be migrate to system memory.

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines

Is this really true? We can map VRAM BOs to the CPU without having migarte back and forth. Admittedly I don't know the inner workings of how this works but in IGTs we do this all the time.

54 batch_bo = xe_bo_create(fd, vm, batch_size,
55 vram_if_possible(fd, 0),
56

DRM_XE_GEM_CREATE_FLAG_NEEDS_VISIBLE_VRAM);

Quoted 14 lines
Quoted 14 lines
Quoted 14 lines
Quoted 14 lines
  57         batch_map = xe_bo_map(fd, batch_bo, batch_size);

The BO is created in VRAM and then mapped to the CPU.

I don't think there is an expectation of coherence rather caching mode and exclusive access of the memory based on synchronization.

e.g.
User write BB/data via CPU to GPU memory
User calls exec
GPU read / write memory
User wait on sync indicating exec done
User reads result

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 6 lines

All of this works without migration. Are we not planing supporting flow with SVM?

Afaik this migration dance really only needs to be done if the CPU and GPU are using atomics on a shared memory region and the GPU device doesn't support a coherent memory protocol (e.g. PVC).

All you said is true. On many of our HW, CPU can actually access device

memory,

Quoted 3 lines
Quoted 3 lines

cache coherently or not.

Quoted 2 lines

The problem is, this is not true for all GPU vendors. For example, on some

HW

Quoted 1 line
Quoted 1 line

from some vendor, CPU can only access partially of device memory. The so

called

Quoted 9 lines
Quoted 9 lines

small bar concept.

Quoted 2 lines

So when HMM is defined, such factors were considered, and

MEMORY_DEVICE_PRIVATE is defined. With this definition, CPU can't access device memory.

Quoted 3 lines

So you can think it is a limitation of HMM.

Quoted 8 lines
Quoted 5 lines

Is it though? I see other type MEMORY_DEVICE_FS_DAX, MEMORY_DEVICE_GENERIC, and MEMORY_DEVICE_PCI_P2PDMA. From my limited
understanding it looks to me like one of those modes would support my example.

No, above are for other purposes. HMM only support DEVICE_PRIVATE and

DEVICE_COHERENT.

Quoted 4 lines
Quoted 4 lines
Quoted 3 lines

Note this is only part 1 of our system allocator work. We do plan to support

DEVICE_COHERENT for our newer device, see below. With this option, we

don't

Quoted 3 lines
Quoted 3 lines

have unnecessary migration back and forth.

Quoted 2 lines

You can think this is just work out all the code path. 90% of the driver code

for

Quoted 2 lines
Quoted 2 lines

DEVICE_PRIVATE and DEVICE_COHERENT will be same. Our real use of system allocator will be DEVICE_COHERENT mode. While DEVICE_PRIVATE mode

allow us

Quoted 8 lines
Quoted 8 lines

to exercise the code on old HW.

Quoted 3 lines

Make sense?

I guess if we want the system allocator to always coherent, then yes you need this dynamic migration with faulting on either side.

Quoted 8 lines
Quoted 6 lines

I was thinking the system allocator would be behave like my example above with madvise dictating the coherence rules.

Maybe I missed this in system allocator design but my feeling is we shouldn't arbitrarily enforce coherence as that could lead to poor performance due to constant migration.

System allocator itself doesn't enforce coherence. Coherence is built in user

programming model. So system allocator allow both GPU and CPU access system allocated pointers, but it doesn't necessarily guarantee the data accessed from CPU/GPU is coherent. It is user program's responsibility to maintain data coherence.

Quoted 2 lines

Data migration in driver is optional, depending on platform capability, user

preference, correctness and performance consideration. Driver internal data migration of course shouldn't break data coherence.

Quoted 2 lines

Of course different vendor can have different data coherence scheme. For

example, it is completely designer's flexibility to build model that is HW automatic data coherence or software explicit data coherence. On our platform, we allow user program to select different coherence mode by setting pat_index for gpu and cpu_caching mode for CPU. So we have completely give the flexibility to user program. Nothing of this contract is changed in system allocator design.

Quoted 2 lines

Going back to the question of what memory type we should use to register our

vram to core mm. HMM currently support two types: PRIVATE and COHERENT. The coherent type requires some HW and BIOS support which we don't have right now. So the only available is PRIVATE. We have not other option right now. As said, we plan to support coherent type where we can avoid unnecessary data migration. But that is stage 2.

Quoted 1 line

Thanks for the explaination. After reading your replies, the HMM doc, and looking at code this all makes sense.

Quoted 21 lines
Quoted 21 lines
Quoted 21 lines
Quoted 21 lines
Quoted 9 lines

For GPU device which supports coherent memory protocol b/t CPU and GPU (such as CXL and CAPI protocol), we can remap device memory as MEMORY_DEVICE_COHERENT. This is TBD.

Co-developed-by Niranjana Vishwanathapura

<[email protected]>

Quoted 1 line
Signed-off-by Niranjana Vishwanathapura

<[email protected]>

Quoted 9 lines
Cc Matthew Brost <[email protected]>
Cc Thomas Hellström <[email protected]>
Cc Brian Welty <[email protected]>
---
 drivers/gpu/drm/xe/Makefile          |  3 +-
 drivers/gpu/drm/xe/xe_device_types.h |  9 +++
 drivers/gpu/drm/xe/xe_mmio.c         |  8 +++
 drivers/gpu/drm/xe/xe_svm.h          | 14 +++++
 drivers/gpu/drm/xe/xe_svm_devmem.c   | 91
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines

++++++++++++++++++++++++++++

Review

Quoted 5 lines
 5 files changed, 124 insertions(+), 1 deletion(-)
 create mode 100644 drivers/gpu/drm/xe/xe_svm.h
 create mode 100644 drivers/gpu/drm/xe/xe_svm_devmem.c
(quoted diff) +0 -0

b/drivers/gpu/drm/xe/Makefile

Quoted 20 lines
Quoted 20 lines
Quoted 20 lines
Quoted 4 lines
Quoted 4 lines

index c531210695db..840467080e59 100644

drivers/gpu/drm/xe/Makefile +0 -0
@@ -142,7 +142,8 @@ xe-y += xe_bb.o \
Quoted 8 lines

drivers/gpu/drm/xe/Makefile xe-y += xe_bb.o \ · line 146

Quoted 6 lines
drivers/gpu/drm/xe/Makefile +2 -1
 	xe_vram_freq.o \
 	xe_wait_user_fence.o \
 	xe_wa.o \
-	xe_wopcm.o
+	xe_wopcm.o \
+	xe_svm_devmem.o

These should be in alphabetical order.

Will fix

Quoted 6 lines

Review

Quoted 5 lines
drivers/gpu/drm/xe/Makefile +0 -0
 # graphics hardware monitoring (HWMON) support
 xe-$(CONFIG_HWMON) += xe_hwmon.o
(quoted diff) +0 -0

b/drivers/gpu/drm/xe/xe_device_types.h

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

drivers/gpu/drm/xe/xe_device_types.h struct xe_mem_region { · line 102

Quoted 8 lines

index 9785eef2e5a4..f27c3bee8ce7 100644

drivers/gpu/drm/xe/xe_device_types.h +1 -0
@@ -99,6 +99,15 @@ struct xe_mem_region {
 	resource_size_t actual_physical_size;
 	/** @mapping: pointer to VRAM mappable space */
 	void __iomem *mapping;
+	/** @pagemap: Used to remap device memory as ZONE_DEVICE

*/

Quoted 37 lines
Quoted 37 lines
Quoted 10 lines
Quoted 9 lines

drivers/gpu/drm/xe/xe_device_types.h struct xe_mem_region { · line 109

Quoted 7 lines
drivers/gpu/drm/xe/xe_device_types.h +7 -0
+    struct dev_pagemap pagemap;
+    /**
+     * @hpa_base: base host physical address
+     *
+     * This is generated when remap device memory as ZONE_DEVICE
+     */
+    resource_size_t hpa_base;

Weird indentation. This goes for the entire series, look at checkpatch.

Quoted 7 lines

Will fix

Quoted 6 lines

Review

Quoted 6 lines
drivers/gpu/drm/xe/xe_device_types.h +1 -0
+
 };
 
 /**
(quoted diff) +0 -0

b/drivers/gpu/drm/xe/xe_mmio.c

Quoted 19 lines
Quoted 18 lines
Quoted 12 lines

index e3db3a178760..0d795394bc4c 100644

drivers/gpu/drm/xe/xe_mmio.c +1 -0
@@ -22,6 +22,7 @@
 #include "xe_module.h"
 #include "xe_sriov.h"
 #include "xe_tile.h"
+#include "xe_svm.h"
 
 #define XEHP_MTCFG_ADDR		XE_REG(0x101800)
 #define TILE_COUNT		REG_GENMASK(15, 8)

drivers/gpu/drm/xe/xe_mmio.c int xe_mmio_probe_vram(struct xe_device *xe) · line 290

Quoted 4 lines
drivers/gpu/drm/xe/xe_mmio.c +1 -0
@@ -286,6 +287,7 @@ int xe_mmio_probe_vram(struct xe_device *xe)
 		}
 
 		io_size -= min_t(u64, tile_size, io_size);
+		xe_svm_devm_add(tile, &tile->mem.vram);

Do we want to do this probe for all devices with VRAM or only a subset?

Quoted 5 lines
Quoted 3 lines
Quoted 1 line

All

Can you explain why?

It is natural for me to add all device memory to hmm. In hmm design, device

memory is used as a special swap out for system memory. I would ask why we only want to add a subset of vram? By a subset, do you mean only vram of one tile instead of all tiles?

Quoted 1 line

I think we talking about different things, my bad on wording in the original question.

Quoted 8 lines
Quoted 2 lines

Let me ask again - should be calling xe_svm_devm_add on all *platforms* that have VRAM. i.e. Should we do this on PVC but not DG2?

Oh, I see. Good question. On i915, this feature was only tested on PVC. We don't have a plan to enable it on older platform than PVC.

Let me add a check here, only enabled it on platform newer than PVC

Probably actually check 'xe->info.has_usm'.

We might want to rename field too and drop the 'usm' nomenclature but that can be done later.

Matt

Quoted 278 lines

Oak

Quoted 276 lines

Matt

Quoted 9 lines

Oak

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines

drivers/gpu/drm/xe/xe_mmio.c void xe_mmio_probe_tiles(struct xe_device

Quoted 6 lines
drivers/gpu/drm/xe/xe_mmio.c +0 -0
 	}
 
 	xe->mem.vram.actual_physical_size = total_size;
@@ -354,10 +356,16 @@ void xe_mmio_probe_tiles(struct xe_device

*xe)

Quoted 16 lines
Quoted 16 lines
Quoted 16 lines
Quoted 15 lines

drivers/gpu/drm/xe/xe_mmio.c void xe_mmio_probe_tiles(struct xe_device · line 367

Quoted 12 lines
drivers/gpu/drm/xe/xe_mmio.c +5 -0
 static void mmio_fini(struct drm_device *drm, void *arg)
 {
 	struct xe_device *xe = arg;
+    struct xe_tile *tile;
+    u8 id;
 
 	pci_iounmap(to_pci_dev(xe->drm.dev), xe->mmio.regs);
 	if (xe->mem.vram.mapping)
 		iounmap(xe->mem.vram.mapping);
+
+	for_each_tile(tile, xe, id)
+		xe_svm_devm_remove(xe, &tile->mem.vram);

This should probably be above existing code. Typical on fini to do things in reverse order from init.

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines

Will fix

Quoted 6 lines

Review

Quoted 6 lines
drivers/gpu/drm/xe/xe_mmio.c +1 -0
+
 }
 
 static int xe_verify_lmem_ready(struct xe_device *xe)
(quoted diff) +0 -0

b/drivers/gpu/drm/xe/xe_svm.h

Quoted 25 lines
Quoted 25 lines
Quoted 25 lines
Quoted 10 lines

drivers/gpu/drm/xe/xe_svm.h line 3

Quoted 8 lines

new file mode 100644
index 000000000000..09f9afb0e7d4

drivers/gpu/drm/xe/xe_svm.h new file +3 -0
@@ -0,0 +1,14 @@
+// SPDX-License-Identifier: MIT
+/*
+ * Copyright © 2023 Intel Corporation

2024?

This patch was actually written 2023

Quoted 10 lines

drivers/gpu/drm/xe/xe_svm.h line 9

Quoted 7 lines
drivers/gpu/drm/xe/xe_svm.h new file +6 -0
+ */
+
+#ifndef __XE_SVM_H
+#define __XE_SVM_H
+
+#include "xe_device_types.h"

I don't think you need to include this. Rather just forward decl structs used here.

Will fix

Quoted 1 line
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

e.g.

struct xe_device;
struct xe_mem_region;
struct xe_tile;

drivers/gpu/drm/xe/xe_svm.h line 11

Quoted 2 lines
drivers/gpu/drm/xe/xe_svm.h new file +2 -0
+
+int xe_svm_devm_add(struct xe_tile *tile, struct xe_mem_region

*mem);

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

drivers/gpu/drm/xe/xe_svm.h line 12

Quoted 1 line
drivers/gpu/drm/xe/xe_svm.h new file +1 -0
+void xe_svm_devm_remove(struct xe_device *xe, struct

xe_mem_region

Quoted 65 lines
Quoted 65 lines
Quoted 65 lines
Quoted 4 lines

*mem);

The arguments here are incongruent here. Typically we want these to match.

Will fix

Quoted 7 lines

Review

Quoted 4 lines
drivers/gpu/drm/xe/xe_svm.h new file +2 -0
+
+#endif
(quoted diff) +0 -0

b/drivers/gpu/drm/xe/xe_svm_devmem.c

Incongruent between xe_svm.h and xe_svm_devmem.c.

Did you mean mem vs mr? if yes, will fix

Again these two

Quoted 13 lines

should
match.

drivers/gpu/drm/xe/xe_svm_devmem.c line 3

Quoted 8 lines

new file mode 100644
index 000000000000..63b7a1961cc6

drivers/gpu/drm/xe/xe_svm_devmem.c new file +3 -0
@@ -0,0 +1,91 @@
+// SPDX-License-Identifier: MIT
+/*
+ * Copyright © 2023 Intel Corporation

2024?

It is from 2023

Quoted 10 lines

drivers/gpu/drm/xe/xe_svm_devmem.c line 10

Quoted 8 lines
drivers/gpu/drm/xe/xe_svm_devmem.c new file +7 -0
+ */
+
+#include <linux/mm_types.h>
+#include <linux/sched/mm.h>
+
+#include "xe_device_types.h"
+#include "xe_trace.h"

xe_trace.h appears to be unused.

Will fix

Quoted 20 lines
Quoted 8 lines
drivers/gpu/drm/xe/xe_svm_devmem.c new file +7 -0
+#include "xe_svm.h"
+
+
+static vm_fault_t xe_devm_migrate_to_ram(struct vm_fault *vmf)
+{
+	return 0;
+}

drivers/gpu/drm/xe/xe_svm_devmem.c line 27

Quoted 10 lines
drivers/gpu/drm/xe/xe_svm_devmem.c new file +10 -0
+
+static void xe_devm_page_free(struct page *page)
+{
+}
+
+static const struct dev_pagemap_ops xe_devm_pagemap_ops = {
+	.page_free = xe_devm_page_free,
+	.migrate_to_ram = xe_devm_migrate_to_ram,
+};
+

Assume these are placeholders that will be populated later?

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines

corrrect

Quoted 3 lines
Quoted 3 lines
drivers/gpu/drm/xe/xe_svm_devmem.c new file +2 -0
+/**
+ * xe_svm_devm_add: Remap and provide memmap backing for

device

Quoted 11 lines
Quoted 11 lines
Quoted 11 lines
Quoted 11 lines

memory

Quoted 9 lines
drivers/gpu/drm/xe/xe_svm_devmem.c new file +9 -0
+ * @tile: tile that the memory region blongs to
+ * @mr: memory region to remap
+ *
+ * This remap device memory to host physical address space and create
+ * struct page to back device memory
+ *
+ * Return: 0 on success standard error code otherwise
+ */
+int xe_svm_devm_add(struct xe_tile *tile, struct xe_mem_region *mr)
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

Here I see the xe_mem_region is from tile->mem.vram, wondering rather than using the tile->mem.vram we should use xe->mem.vram when

enabling

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 1 line

svm? Isn't the idea behind svm the entire memory is 1 view?

Still need to use tile. The reason is, memory of different tile can have

different

Quoted 1 line
Quoted 1 line

characteristics, such as latency. So we want to differentiate memory b/t tiles

also

Quoted 1 line
Quoted 1 line

in svm. I need to change below " mr->pagemap.owner = tile->xe->drm.dev ".

the

Quoted 1 line
Quoted 1 line

owner should also be tile. This is the way hmm differentiate memory of

different

Quoted 3 lines
Quoted 3 lines

tile.

Quoted 2 lines

With svm it is 1 view, from virtual address space perspective and from

physical

Quoted 1 line
Quoted 1 line

struct page perspective. You can think of all the tile's vram is stacked together

to

Quoted 1 line
Quoted 1 line

form a unified view together with system memory. This doesn't prohibit us

from

Quoted 1 line
Quoted 1 line

differentiate memory from different tile. This differentiation allow us to

optimize

Quoted 6 lines
Quoted 6 lines

performance, i.e., we can wisely place memory in specific tile. If we don't differentiate, this is not possible.

Quoted 1 line

Ok makes sense.

Quoted 7 lines
Quoted 7 lines

Matt

Quoted 5 lines
Quoted 3 lines

I suppose if we do that we also only use 1 TTM VRAM manager / buddy allocator too. I thought I saw some patches flying around for that too.

Ttm vram manager is not in the picture. We deliberately avoided it per

previous

Quoted 3 lines
Quoted 3 lines

discussion

Quoted 2 lines

Yes same buddy allocator. It is in my previous POC:

https://lore.kernel.org/dri-

Quoted 1 line
Quoted 1 line

devel/[email protected]/. I didn't put those

patches

Quoted 16 lines
Quoted 16 lines

in this series because I want to merge this small patches separately.

Quoted 15 lines
Quoted 15 lines
Quoted 15 lines
drivers/gpu/drm/xe/xe_svm_devmem.c new file +14 -0
+{
+	struct device *dev = &to_pci_dev(tile->xe->drm.dev)->dev;
+	struct resource *res;
+	void *addr;
+	int ret;
+
+	res = devm_request_free_mem_region(dev, &iomem_resource,
+					   mr->usable_size);
+	if (IS_ERR(res)) {
+		ret = PTR_ERR(res);
+		return ret;
+	}
+
+	mr->pagemap.type = MEMORY_DEVICE_PRIVATE;
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

drivers/gpu/drm/xe/xe_svm_devmem.c line 60

Quoted 8 lines
drivers/gpu/drm/xe/xe_svm_devmem.c new file +8 -0
+	mr->pagemap.range.start = res->start;
+	mr->pagemap.range.end = res->end;
+	mr->pagemap.nr_range = 1;
+	mr->pagemap.ops = &xe_devm_pagemap_ops;
+	mr->pagemap.owner = tile->xe->drm.dev;
+	addr = devm_memremap_pages(dev, &mr->pagemap);
+	if (IS_ERR(addr)) {
+		devm_release_mem_region(dev, res->start,

resource_size(res));

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

drivers/gpu/drm/xe/xe_svm_devmem.c line 62

Quoted 2 lines
drivers/gpu/drm/xe/xe_svm_devmem.c new file +2 -0
+		ret = PTR_ERR(addr);
+		drm_err(&tile->xe->drm, "Failed to remap tile %d

memory,

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines

errno %d\n",

drivers/gpu/drm/xe/xe_svm_devmem.c line 68

Quoted 6 lines
drivers/gpu/drm/xe/xe_svm_devmem.c new file +6 -0
+				tile->id, ret);
+		return ret;
+	}
+	mr->hpa_base = res->start;
+
+	drm_info(&tile->xe->drm, "Added tile %d memory [%llx-%llx] to

devm,

Quoted 4 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

remapped to %pr\n",

drivers/gpu/drm/xe/xe_svm_devmem.c line 69

Quoted 1 line
drivers/gpu/drm/xe/xe_svm_devmem.c new file +1 -0
+			tile->id, mr->io_start, mr->io_start + mr-

usable_size,

Quoted 1 line
Quoted 1 line
Quoted 1 line

res);

Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines

drivers/gpu/drm/xe/xe_svm_devmem.c line 78

Quoted 9 lines
drivers/gpu/drm/xe/xe_svm_devmem.c new file +9 -0
+	return 0;
+}
+
+/**
+ * xe_svm_devm_remove: Unmap device memory and free resources
+ * @xe: xe device
+ * @mr: memory region to remove
+ */
+void xe_svm_devm_remove(struct xe_device *xe, struct

xe_mem_region

Quoted 29 lines
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines

*mr)

drivers/gpu/drm/xe/xe_svm_devmem.c line 86

Quoted 8 lines
drivers/gpu/drm/xe/xe_svm_devmem.c new file +8 -0
+{
+	/*FIXME: below cause a kernel hange during moduel remove*/
+#if 0
+	struct device *dev = &to_pci_dev(xe->drm.dev)->dev;
+
+	if (mr->hpa_base) {
+		devm_memunmap_pages(dev, &mr->pagemap);
+		devm_release_mem_region(dev, mr-

pagemap.range.start,

Quoted 1 line
Quoted 1 line
Quoted 1 line

drivers/gpu/drm/xe/xe_svm_devmem.c line 87

Quoted 1 line
drivers/gpu/drm/xe/xe_svm_devmem.c new file +1 -0
+			mr->pagemap.range.end - mr-

pagemap.range.start +1);

Quoted 17 lines
Quoted 17 lines
Quoted 4 lines

drivers/gpu/drm/xe/xe_svm_devmem.c line 89

Quoted 2 lines
drivers/gpu/drm/xe/xe_svm_devmem.c new file +2 -0
+	}
+#endif

This would need to be fixed too.

Yes...

Oak

Quoted 8 lines

Matt

Quoted 5 lines
drivers/gpu/drm/xe/xe_svm_devmem.c new file +2 -1
+}
+
--

2.26.3

#9 lore raw

Re: [PATCH 1/5] drm/xe/svm: Remap and provide memmap backing for GPU vram

Quoted 362 lines · Matthew Brost
Quoted 361 lines · Oak
Quoted 349 lines

-----Original Message-----
From: Brost, Matthew <[email protected]>
Sent: Friday, March 15, 2024 4:40 PM
To: Zeng, Oak <[email protected]>
Cc: [email protected]; Hellstrom, Thomas <[email protected]>; [email protected]; Welty, Brian <[email protected]>; Ghimiray, Himal Prasad <[email protected]>
Subject: Re: [PATCH 1/5] drm/xe/svm: Remap and provide memmap backing for
GPU vram

Quoted 12 lines · Oak
Quoted 12 lines

-----Original Message-----
From: Brost, Matthew <[email protected]>
Sent: Thursday, March 14, 2024 4:49 PM
To: Zeng, Oak <[email protected]>
Cc: [email protected]; Hellstrom, Thomas <[email protected]>; [email protected]; Welty, Brian <[email protected]>; Ghimiray, Himal Prasad <[email protected]>
Subject: Re: [PATCH 1/5] drm/xe/svm: Remap and provide memmap backing

for

Quoted 5 lines
Quoted 5 lines

GPU vram

Quoted 2 lines · Oak

Hi Matt,

Quoted 11 lines
Quoted 11 lines
Quoted 11 lines
Quoted 11 lines

-----Original Message-----
From: Brost, Matthew <[email protected]>
Sent: Thursday, March 14, 2024 1:18 PM
To: Zeng, Oak <[email protected]>
Cc: [email protected]; Hellstrom, Thomas <[email protected]>; [email protected]; Welty, Brian
<[email protected]>; Ghimiray, Himal Prasad <[email protected]>
Subject: Re: [PATCH 1/5] drm/xe/svm: Remap and provide memmap

backing

Quoted 6 lines
Quoted 6 lines

for

Quoted 5 lines
Quoted 5 lines

GPU vram

Quoted 2 lines · Oak Zeng

Memory remap GPU vram using devm_memremap_pages, so each

GPU

Quoted 10 lines
Quoted 10 lines

vram

Quoted 9 lines
Quoted 9 lines
Quoted 9 lines

page is backed by a struct page.

Those struct pages are created to allow hmm migrate buffer b/t
GPU vram and CPU system memory using existing Linux migration
mechanism (i.e., migrating b/t CPU system memory and hard disk).

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines

This is prepare work to enable svm (shared virtual memory) through
Linux kernel hmm framework. The memory remap's page map type is

set

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

to MEMORY_DEVICE_PRIVATE for now. This means even
though each

GPU

Quoted 15 lines
Quoted 15 lines
Quoted 15 lines
Quoted 15 lines
Quoted 8 lines

vram page get a struct page and can be mapped in CPU page table,
but such pages are treated as GPU's private resource, so CPU can't
access them. If CPU access such page, a page fault is triggered
and page will be migrate to system memory.

Is this really true? We can map VRAM BOs to the CPU without having
migarte back and forth. Admittedly I don't know the inner workings of
how this works but in IGTs we do this all the time.

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines

  54         batch_bo = xe_bo_create(fd, vm, batch_size, 55                                 vram_if_possible(fd, 0),
  56

DRM_XE_GEM_CREATE_FLAG_NEEDS_VISIBLE_VRAM);

Quoted 21 lines
Quoted 21 lines
Quoted 21 lines
Quoted 21 lines

  57         batch_map = xe_bo_map(fd, batch_bo,
batch_size);

The BO is created in VRAM and then mapped to the CPU.

I don't think there is an expectation of coherence rather caching mode
and exclusive access of the memory based on synchronization.

e.g.
User write BB/data via CPU to GPU memory
User calls exec
GPU read / write memory
User wait on sync indicating exec done
User reads result

All of this works without migration. Are we not planing supporting flow
with SVM?

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 5 lines

Afaik this migration dance really only needs to be done if the CPU and
GPU are using atomics on a shared memory region and the GPU device
doesn't support a coherent memory protocol (e.g. PVC).

All you said is true. On many of our HW, CPU can actually access device

memory,

Quoted 4 lines
Quoted 4 lines

cache coherently or not.

Quoted 3 lines

The problem is, this is not true for all GPU vendors. For example, on some

HW

Quoted 2 lines
Quoted 2 lines

from some vendor, CPU can only access partially of device memory. The so

called

Quoted 18 lines
Quoted 10 lines

small bar concept.

Quoted 2 lines

So when HMM is defined, such factors were considered, and

MEMORY_DEVICE_PRIVATE is defined. With this definition, CPU can't access
device memory.

Quoted 3 lines

So you can think it is a limitation of HMM.

Quoted 6 lines

Is it though? I see other type MEMORY_DEVICE_FS_DAX, MEMORY_DEVICE_GENERIC, and MEMORY_DEVICE_PCI_P2PDMA. From my limited
understanding it looks to me like one of those modes would support my
example.

Quoted 2 lines

No, above are for other purposes. HMM only support DEVICE_PRIVATE and

DEVICE_COHERENT.

Quoted 6 lines
Quoted 6 lines
Quoted 4 lines

Note this is only part 1 of our system allocator work. We do plan to support

DEVICE_COHERENT for our newer device, see below. With this option, we

don't

Quoted 4 lines
Quoted 4 lines

have unnecessary migration back and forth.

Quoted 3 lines

You can think this is just work out all the code path. 90% of the driver code

for

Quoted 4 lines
Quoted 4 lines

DEVICE_PRIVATE and DEVICE_COHERENT will be same. Our real use of system
allocator will be DEVICE_COHERENT mode. While DEVICE_PRIVATE mode

allow us

Quoted 13 lines
Quoted 13 lines

to exercise the code on old HW.

Quoted 3 lines

Make sense?

I guess if we want the system allocator to always coherent, then yes you
need this dynamic migration with faulting on either side.

I was thinking the system allocator would be behave like my example
above with madvise dictating the coherence rules.

Quoted 8 lines
Quoted 5 lines

Maybe I missed this in system allocator design but my feeling is we
shouldn't arbitrarily enforce coherence as that could lead to poor
performance due to constant migration.

System allocator itself doesn't enforce coherence. Coherence is built in user

programming model. So system allocator allow both GPU and CPU access system
allocated pointers, but it doesn't necessarily guarantee the data accessed from
CPU/GPU is coherent. It is user program's responsibility to maintain data
coherence.

Quoted 3 lines

Data migration in driver is optional, depending on platform capability, user

preference, correctness and performance consideration. Driver internal data
migration of course shouldn't break data coherence.

Quoted 3 lines

Of course different vendor can have different data coherence scheme. For

example, it is completely designer's flexibility to build model that is HW automatic
data coherence or software explicit data coherence. On our platform, we allow
user program to select different coherence mode by setting pat_index for gpu
and cpu_caching mode for CPU. So we have completely give the flexibility to user
program. Nothing of this contract is changed in system allocator design.

Quoted 3 lines

Going back to the question of what memory type we should use to register our

vram to core mm. HMM currently support two types: PRIVATE and COHERENT.
The coherent type requires some HW and BIOS support which we don't have
right now. So the only available is PRIVATE. We have not other option right now.
As said, we plan to support coherent type where we can avoid unnecessary data
migration. But that is stage 2.

Quoted 1 line

Thanks for the explaination. After reading your replies, the HMM doc,
and looking at code this all makes sense.

Quoted 23 lines
Quoted 23 lines
Quoted 23 lines
Quoted 23 lines
Quoted 11 lines

For GPU device which supports coherent memory protocol b/t CPU and
GPU (such as CXL and CAPI protocol), we can remap device memory as
MEMORY_DEVICE_COHERENT. This is TBD.

Co-developed-by Niranjana Vishwanathapura

<[email protected]>

Quoted 1 line
Signed-off-by Niranjana Vishwanathapura

<[email protected]>

Quoted 9 lines
Cc Matthew Brost <[email protected]>
Cc Thomas Hellström <[email protected]>
Cc Brian Welty <[email protected]>

---
 drivers/gpu/drm/xe/Makefile          |  3 +-
 drivers/gpu/drm/xe/xe_device_types.h |  9 +++
 drivers/gpu/drm/xe/xe_mmio.c         |  8 +++
 drivers/gpu/drm/xe/xe_svm.h          | 14 +++++
 drivers/gpu/drm/xe/xe_svm_devmem.c   | 91

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines

++++++++++++++++++++++++++++

Review

Quoted 5 lines
 5 files changed, 124 insertions(+), 1 deletion(-)

 create mode 100644 drivers/gpu/drm/xe/xe_svm.h create mode 100644 drivers/gpu/drm/xe/xe_svm_devmem.c

(quoted diff) +0 -0

b/drivers/gpu/drm/xe/Makefile

Quoted 20 lines
Quoted 20 lines
Quoted 20 lines
Quoted 4 lines
Quoted 4 lines

index c531210695db..840467080e59 100644

drivers/gpu/drm/xe/Makefile +0 -0
@@ -142,7 +142,8 @@ xe-y += xe_bb.o \
Quoted 8 lines

drivers/gpu/drm/xe/Makefile xe-y += xe_bb.o \ · line 146

Quoted 6 lines
drivers/gpu/drm/xe/Makefile +2 -1
  	xe_vram_freq.o \
  	xe_wait_user_fence.o \
  	xe_wa.o \
-	xe_wopcm.o
+	xe_wopcm.o \
+	xe_svm_devmem.o

These should be in alphabetical order.

Will fix

Quoted 6 lines

Review

Quoted 5 lines
drivers/gpu/drm/xe/Makefile +0 -0
  # graphics hardware monitoring (HWMON) support

 xe-$(CONFIG_HWMON) += xe_hwmon.o

(quoted diff) +0 -0

b/drivers/gpu/drm/xe/xe_device_types.h

Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
Quoted 10 lines

index 9785eef2e5a4..f27c3bee8ce7 100644

drivers/gpu/drm/xe/xe_device_types.h +1 -0
@@ -99,6 +99,15 @@ struct xe_mem_region {
  	resource_size_t actual_physical_size;
  	/** @mapping: pointer to VRAM mappable space
 */
  	void __iomem *mapping;
+	/** @pagemap: Used to remap device memory as

ZONE_DEVICE

*/

Quoted 44 lines
Quoted 44 lines
Quoted 12 lines
Quoted 11 lines
Quoted 8 lines

+    struct dev_pagemap pagemap;
+    /**
+     * @hpa_base: base host physical address +     *
+     * This is generated when remap device memory as ZONE_DEVICE
+     */
+    resource_size_t hpa_base;

Weird indentation. This goes for the entire series, look at checkpatch.

Quoted 7 lines

Will fix

Quoted 6 lines

Review

Quoted 6 lines

+
 };

 /**

(quoted diff) +0 -0

b/drivers/gpu/drm/xe/xe_mmio.c

Quoted 24 lines
Quoted 23 lines
Quoted 13 lines

index e3db3a178760..0d795394bc4c 100644

drivers/gpu/drm/xe/xe_mmio.c +1 -0
@@ -22,6 +22,7 @@
  #include "xe_module.h"
  #include "xe_sriov.h"
  #include "xe_tile.h"
+#include "xe_svm.h"
 
  #define
 XEHP_MTCFG_ADDR		XE_REG(0x101800)

 #define TILE_COUNT REG_GENMASK(15, 8)

drivers/gpu/drm/xe/xe_mmio.c int xe_mmio_probe_vram(struct · line 293

Quoted 7 lines
drivers/gpu/drm/xe/xe_mmio.c +1 -0
@@ -286,6 +287,7 @@ int xe_mmio_probe_vram(struct
 xe_device *xe)
  		}
 
  		io_size -= min_t(u64, tile_size,
 io_size);
+		xe_svm_devm_add(tile, &tile-
 >mem.vram);

Do we want to do this probe for all devices with VRAM or only a subset?

Quoted 6 lines
Quoted 3 lines
Quoted 1 line

All

Can you explain why?

It is natural for me to add all device memory to hmm. In hmm design, device

memory is used as a special swap out for system memory. I would ask why we
only want to add a subset of vram? By a subset, do you mean only vram of one
tile instead of all tiles?

Quoted 1 line
Quoted 7 lines

I think we talking about different things, my bad on wording in the
original question.

Let me ask again - should be calling xe_svm_devm_add on all *platforms*
that have VRAM. i.e. Should we do this on PVC but not DG2?

Oh, I see. Good question. On i915, this feature was only tested on PVC. We don't have a plan to enable it on older platform than PVC.

Quoted 7 lines
Quoted 2 lines

Let me add a check here, only enabled it on platform newer than PVC

Probably actually check 'xe->info.has_usm'.

We might want to rename field too and drop the 'usm' nomenclature but that can be done later.

Perhaps "has_recoverable_pagefaults" or some for of abbreviation.

Another question w r t this is whether we should do this unconditionally even on platforms that support it. Adding a struct_page per VRAM page would potentially consume a significant amount of system memory.

/Thomas

Quoted 324 lines

Matt

Quoted 321 lines

Oak

Quoted 319 lines

Matt

Quoted 11 lines

Oak

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

  }

  xe->mem.vram.actual_physical_size =
total_size;

drivers/gpu/drm/xe/xe_mmio.c +0 -0
@@ -354,10 +356,16 @@ void xe_mmio_probe_tiles(struct

xe_device

*xe)

Quoted 20 lines
Quoted 20 lines
Quoted 20 lines
Quoted 19 lines
Quoted 14 lines

 static void mmio_fini(struct drm_device *drm, void *arg)
 {
  struct xe_device *xe = arg;
+    struct xe_tile *tile;
+    u8 id;

  pci_iounmap(to_pci_dev(xe->drm.dev), xe-

Quoted 1 line

mmio.regs);

  if (xe->mem.vram.mapping)
  iounmap(xe->mem.vram.mapping);

drivers/gpu/drm/xe/xe_mmio.c +3 -0
+
+	for_each_tile(tile, xe, id)
+		xe_svm_devm_remove(xe, &tile-
Quoted 1 line
Quoted 1 line

mem.vram);

This should probably be above existing code. Typical on fini to do
things in reverse order from init.

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines

Will fix

Quoted 6 lines

Review

Quoted 6 lines

+
 }

 static int xe_verify_lmem_ready(struct xe_device *xe)

(quoted diff) +0 -0

b/drivers/gpu/drm/xe/xe_svm.h

Quoted 28 lines
Quoted 28 lines
Quoted 28 lines
Quoted 10 lines

drivers/gpu/drm/xe/xe_svm.h line 3

Quoted 8 lines

new file mode 100644
index 000000000000..09f9afb0e7d4

drivers/gpu/drm/xe/xe_svm.h new file +3 -0
@@ -0,0 +1,14 @@
+// SPDX-License-Identifier: MIT
+/*
+ * Copyright © 2023 Intel Corporation

2024?

This patch was actually written 2023

Quoted 11 lines

drivers/gpu/drm/xe/xe_svm.h line 9

Quoted 7 lines
drivers/gpu/drm/xe/xe_svm.h new file +6 -0
+ */
+
+#ifndef __XE_SVM_H
+#define __XE_SVM_H
+
+#include "xe_device_types.h"

I don't think you need to include this. Rather just forward decl structs
used here.

Will fix

Quoted 3 lines

e.g.

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines

struct xe_device;
struct xe_mem_region;
struct xe_tile;

Quoted 3 lines
drivers/gpu/drm/xe/xe_svm.h new file +2 -0
+
+int xe_svm_devm_add(struct xe_tile *tile, struct

xe_mem_region

*mem);

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

+void xe_svm_devm_remove(struct xe_device *xe, struct

xe_mem_region

Quoted 69 lines
Quoted 69 lines
Quoted 69 lines
Quoted 5 lines

*mem);

The arguments here are incongruent here. Typically we want these to
match.

Will fix

Quoted 7 lines

Review

Quoted 4 lines
drivers/gpu/drm/xe/xe_svm.h new file +2 -0
+
+#endif
(quoted diff) +0 -0

b/drivers/gpu/drm/xe/xe_svm_devmem.c

Incongruent between xe_svm.h and xe_svm_devmem.c.

Did you mean mem vs mr? if yes, will fix

Again these two

Quoted 13 lines

should
match.

drivers/gpu/drm/xe/xe_svm_devmem.c line 3

Quoted 8 lines

new file mode 100644
index 000000000000..63b7a1961cc6

drivers/gpu/drm/xe/xe_svm_devmem.c new file +3 -0
@@ -0,0 +1,91 @@
+// SPDX-License-Identifier: MIT
+/*
+ * Copyright © 2023 Intel Corporation

2024?

It is from 2023

Quoted 10 lines

drivers/gpu/drm/xe/xe_svm_devmem.c line 10

Quoted 8 lines
drivers/gpu/drm/xe/xe_svm_devmem.c new file +7 -0
+ */
+
+#include <linux/mm_types.h>
+#include <linux/sched/mm.h>
+
+#include "xe_device_types.h"
+#include "xe_trace.h"

xe_trace.h appears to be unused.

Will fix

Quoted 23 lines
Quoted 16 lines
drivers/gpu/drm/xe/xe_svm_devmem.c new file +4 -0
+#include "xe_svm.h"
+
+
+static vm_fault_t xe_devm_migrate_to_ram(struct

vm_fault *vmf)

drivers/gpu/drm/xe/xe_svm_devmem.c new file +9 -0
+{
+	return 0;
+}
+
+static void xe_devm_page_free(struct page *page)
+{
+}
+
+static const struct dev_pagemap_ops

xe_devm_pagemap_ops = {

drivers/gpu/drm/xe/xe_svm_devmem.c

Quoted 4 lines
drivers/gpu/drm/xe/xe_svm_devmem.c new file +4 -0
+	.page_free = xe_devm_page_free,
+	.migrate_to_ram = xe_devm_migrate_to_ram,
+};
+

Assume these are placeholders that will be populated later?

Quoted 5 lines
Quoted 5 lines
Quoted 5 lines

corrrect

Quoted 4 lines
Quoted 4 lines

+/**
+ * xe_svm_devm_add: Remap and provide memmap backing for

device

Quoted 13 lines
Quoted 13 lines
Quoted 13 lines
Quoted 13 lines

memory

Quoted 11 lines

+ * @tile: tile that the memory region blongs to
+ * @mr: memory region to remap
+ *
+ * This remap device memory to host physical address space and create
+ * struct page to back device memory
+ *
+ * Return: 0 on success standard error code otherwise
+ */
+int xe_svm_devm_add(struct xe_tile *tile, struct xe_mem_region *mr)

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines

Here I see the xe_mem_region is from tile->mem.vram, wondering rather
than using the tile->mem.vram we should use xe->mem.vram when

enabling

Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 2 lines

svm? Isn't the idea behind svm the entire memory is 1 view?

Still need to use tile. The reason is, memory of different tile can have

different

Quoted 2 lines
Quoted 2 lines

characteristics, such as latency. So we want to differentiate memory b/t tiles

also

Quoted 2 lines
Quoted 2 lines

in svm. I need to change below " mr->pagemap.owner = tile-

Quoted 1 line

xe->drm.dev ".

the

Quoted 2 lines
Quoted 2 lines

owner should also be tile. This is the way hmm differentiate memory of

different

Quoted 4 lines
Quoted 4 lines

tile.

Quoted 3 lines

With svm it is 1 view, from virtual address space
perspective and from

physical

Quoted 2 lines
Quoted 2 lines

struct page perspective. You can think of all the tile's vram is stacked together

to

Quoted 2 lines
Quoted 2 lines

form a unified view together with system memory. This doesn't prohibit us

from

Quoted 2 lines
Quoted 2 lines

differentiate memory from different tile. This
differentiation allow us to

optimize

Quoted 10 lines
Quoted 10 lines

performance, i.e., we can wisely place memory in specific tile. If we don't
differentiate, this is not possible.

Quoted 1 line

Ok makes sense.

Matt

Quoted 1 line
Quoted 1 line
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 4 lines

I suppose if we do that we also only use 1 TTM VRAM manager / buddy
allocator too. I thought I saw some patches flying around for that too.

Ttm vram manager is not in the picture. We deliberately avoided it per

previous

Quoted 3 lines
Quoted 3 lines

discussion

Quoted 2 lines

Yes same buddy allocator. It is in my previous POC:

https://lore.kernel.org/dri-

Quoted 2 lines
Quoted 2 lines

devel/[email protected]/. I didn't put those

patches

Quoted 28 lines
Quoted 28 lines

in this series because I want to merge this small patches separately.

Quoted 26 lines
Quoted 26 lines
Quoted 26 lines
drivers/gpu/drm/xe/xe_svm_devmem.c new file +2 -0
+{
+	struct device *dev = &to_pci_dev(tile->xe-
Quoted 1 line

drm.dev)->dev;

drivers/gpu/drm/xe/xe_svm_devmem.c new file +5 -0
+	struct resource *res;
+	void *addr;
+	int ret;
+
+	res = devm_request_free_mem_region(dev,

&iomem_resource,

drivers/gpu/drm/xe/xe_svm_devmem.c new file +1 -0
+					   mr-
Quoted 1 line

usable_size);

drivers/gpu/drm/xe/xe_svm_devmem.c new file +14 -0
+	if (IS_ERR(res)) {
+		ret = PTR_ERR(res);
+		return ret;
+	}
+
+	mr->pagemap.type = MEMORY_DEVICE_PRIVATE;
+	mr->pagemap.range.start = res->start;
+	mr->pagemap.range.end = res->end;
+	mr->pagemap.nr_range = 1;
+	mr->pagemap.ops = &xe_devm_pagemap_ops;
+	mr->pagemap.owner = tile->xe->drm.dev;
+	addr = devm_memremap_pages(dev, &mr->pagemap);
+	if (IS_ERR(addr)) {
+		devm_release_mem_region(dev, res-
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

start,

resource_size(res));

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
drivers/gpu/drm/xe/xe_svm_devmem.c new file +2 -0
+		ret = PTR_ERR(addr);
+		drm_err(&tile->xe->drm, "Failed to

remap tile %d

memory,

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

errno %d\n",

Quoted 7 lines
drivers/gpu/drm/xe/xe_svm_devmem.c new file +6 -0
+				tile->id, ret);
+		return ret;
+	}
+	mr->hpa_base = res->start;
+
+	drm_info(&tile->xe->drm, "Added tile %d memory

[%llx-%llx] to

devm,

Quoted 5 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

remapped to %pr\n",

Quoted 2 lines
drivers/gpu/drm/xe/xe_svm_devmem.c new file +1 -0
+			tile->id, mr->io_start, mr-
Quoted 1 line

io_start + mr-

usable_size,

Quoted 1 line
Quoted 1 line
Quoted 1 line

res);

Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
drivers/gpu/drm/xe/xe_svm_devmem.c new file +5 -0
+	return 0;
+}
+
+/**
+ * xe_svm_devm_remove: Unmap device memory and free

resources
+ * @xe: xe device
+ * @mr: memory region to remove
+ */
+void xe_svm_devm_remove(struct xe_device *xe, struct

xe_mem_region

Quoted 32 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines

*mr)

Quoted 5 lines
drivers/gpu/drm/xe/xe_svm_devmem.c new file +2 -0
+{
+	/*FIXME: below cause a kernel hange during

moduel remove*/

drivers/gpu/drm/xe/xe_svm_devmem.c new file +2 -0
+#if 0
+	struct device *dev = &to_pci_dev(xe->drm.dev)-
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines

drivers/gpu/drm/xe/xe_svm_devmem.c

Quoted 6 lines
Quoted 1 line

dev;

drivers/gpu/drm/xe/xe_svm_devmem.c new file +3 -0
+
+	if (mr->hpa_base) {
+		devm_memunmap_pages(dev, &mr-
Quoted 1 line

pagemap);

drivers/gpu/drm/xe/xe_svm_devmem.c new file +1 -0
+		devm_release_mem_region(dev, mr-

pagemap.range.start,

Quoted 1 line
Quoted 1 line
Quoted 1 line

drivers/gpu/drm/xe/xe_svm_devmem.c

Quoted 1 line
drivers/gpu/drm/xe/xe_svm_devmem.c new file +1 -0
+			mr->pagemap.range.end - mr-

pagemap.range.start +1);

Quoted 17 lines
Quoted 17 lines
Quoted 4 lines

drivers/gpu/drm/xe/xe_svm_devmem.c

Quoted 2 lines
drivers/gpu/drm/xe/xe_svm_devmem.c new file +2 -0
+	}
+#endif

This would need to be fixed too.

Yes...

Oak

Quoted 8 lines

Matt

Quoted 5 lines
drivers/gpu/drm/xe/xe_svm_devmem.c new file +2 -1
+}
+
--

2.26.3

author
#10 lore raw

RE: [PATCH 1/5] drm/xe/svm: Remap and provide memmap backing for GPU vram

Quoted 379 lines

-----Original Message-----
From: Hellstrom, Thomas <[email protected]>
Sent: Monday, March 18, 2024 6:16 AM
To: Brost, Matthew <[email protected]>; Zeng, Oak <[email protected]>
Cc: [email protected]; Welty, Brian <[email protected]>; [email protected]; Ghimiray, Himal Prasad <[email protected]>
Subject: Re: [PATCH 1/5] drm/xe/svm: Remap and provide memmap backing for GPU vram

Quoted 136 lines · Matthew Brost
Quoted 135 lines · Oak
Quoted 135 lines

-----Original Message-----
From: Brost, Matthew <[email protected]>
Sent: Friday, March 15, 2024 4:40 PM
To: Zeng, Oak <[email protected]>
Cc: [email protected]; Hellstrom, Thomas <[email protected]>; [email protected]; Welty, Brian <[email protected]>; Ghimiray, Himal Prasad <[email protected]>
Subject: Re: [PATCH 1/5] drm/xe/svm: Remap and provide memmap backing for
GPU vram

Quoted 12 lines · Oak
Quoted 12 lines

-----Original Message-----
From: Brost, Matthew <[email protected]>
Sent: Thursday, March 14, 2024 4:49 PM
To: Zeng, Oak <[email protected]>
Cc: [email protected]; Hellstrom, Thomas <[email protected]>; [email protected]; Welty, Brian <[email protected]>; Ghimiray, Himal Prasad <[email protected]>
Subject: Re: [PATCH 1/5] drm/xe/svm: Remap and provide memmap backing

for

Quoted 5 lines
Quoted 5 lines

GPU vram

Quoted 2 lines · Oak

Hi Matt,

Quoted 11 lines
Quoted 11 lines
Quoted 11 lines
Quoted 11 lines

-----Original Message-----
From: Brost, Matthew <[email protected]>
Sent: Thursday, March 14, 2024 1:18 PM
To: Zeng, Oak <[email protected]>
Cc: [email protected]; Hellstrom, Thomas <[email protected]>; [email protected]; Welty, Brian
<[email protected]>; Ghimiray, Himal Prasad <[email protected]>
Subject: Re: [PATCH 1/5] drm/xe/svm: Remap and provide memmap

backing

Quoted 6 lines
Quoted 6 lines

for

Quoted 5 lines
Quoted 5 lines

GPU vram

Quoted 2 lines · Oak Zeng

Memory remap GPU vram using devm_memremap_pages, so each

GPU

Quoted 10 lines
Quoted 10 lines

vram

Quoted 9 lines
Quoted 9 lines
Quoted 9 lines

page is backed by a struct page.

Those struct pages are created to allow hmm migrate buffer b/t
GPU vram and CPU system memory using existing Linux migration
mechanism (i.e., migrating b/t CPU system memory and hard disk).

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines

This is prepare work to enable svm (shared virtual memory) through
Linux kernel hmm framework. The memory remap's page map type is

set

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

to MEMORY_DEVICE_PRIVATE for now. This means even
though each

GPU

Quoted 15 lines
Quoted 15 lines
Quoted 15 lines
Quoted 15 lines
Quoted 8 lines

vram page get a struct page and can be mapped in CPU page table,
but such pages are treated as GPU's private resource, so CPU can't
access them. If CPU access such page, a page fault is triggered
and page will be migrate to system memory.

Is this really true? We can map VRAM BOs to the CPU without having
migarte back and forth. Admittedly I don't know the inner workings of
how this works but in IGTs we do this all the time.

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines

  54         batch_bo = xe_bo_create(fd, vm, batch_size, 55                                 vram_if_possible(fd, 0),
  56

DRM_XE_GEM_CREATE_FLAG_NEEDS_VISIBLE_VRAM);

Quoted 21 lines
Quoted 21 lines
Quoted 21 lines
Quoted 21 lines

  57         batch_map = xe_bo_map(fd, batch_bo,
batch_size);

The BO is created in VRAM and then mapped to the CPU.

I don't think there is an expectation of coherence rather caching mode
and exclusive access of the memory based on synchronization.

e.g.
User write BB/data via CPU to GPU memory
User calls exec
GPU read / write memory
User wait on sync indicating exec done
User reads result

All of this works without migration. Are we not planing supporting flow
with SVM?

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 5 lines

Afaik this migration dance really only needs to be done if the CPU and
GPU are using atomics on a shared memory region and the GPU device
doesn't support a coherent memory protocol (e.g. PVC).

All you said is true. On many of our HW, CPU can actually access device

memory,

Quoted 4 lines
Quoted 4 lines

cache coherently or not.

Quoted 3 lines

The problem is, this is not true for all GPU vendors. For example, on some

HW

Quoted 2 lines
Quoted 2 lines

from some vendor, CPU can only access partially of device memory. The so

called

Quoted 7 lines
Quoted 7 lines

small bar concept.

Quoted 2 lines

So when HMM is defined, such factors were considered, and

MEMORY_DEVICE_PRIVATE is defined. With this definition, CPU can't access
device memory.

Quoted 1 line
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 2 lines

So you can think it is a limitation of HMM.

Is it though? I see other type MEMORY_DEVICE_FS_DAX, MEMORY_DEVICE_GENERIC, and MEMORY_DEVICE_PCI_P2PDMA.

From my

Quoted 224 lines
Quoted 223 lines
Quoted 209 lines
Quoted 8 lines
Quoted 4 lines

limited
understanding it looks to me like one of those modes would support my
example.

No, above are for other purposes. HMM only support DEVICE_PRIVATE and

DEVICE_COHERENT.

Quoted 6 lines
Quoted 6 lines
Quoted 4 lines

Note this is only part 1 of our system allocator work. We do plan to support

DEVICE_COHERENT for our newer device, see below. With this option, we

don't

Quoted 4 lines
Quoted 4 lines

have unnecessary migration back and forth.

Quoted 3 lines

You can think this is just work out all the code path. 90% of the driver code

for

Quoted 4 lines
Quoted 4 lines

DEVICE_PRIVATE and DEVICE_COHERENT will be same. Our real use of system
allocator will be DEVICE_COHERENT mode. While DEVICE_PRIVATE mode

allow us

Quoted 13 lines
Quoted 13 lines

to exercise the code on old HW.

Quoted 3 lines

Make sense?

I guess if we want the system allocator to always coherent, then yes you
need this dynamic migration with faulting on either side.

I was thinking the system allocator would be behave like my example
above with madvise dictating the coherence rules.

Quoted 8 lines
Quoted 5 lines

Maybe I missed this in system allocator design but my feeling is we
shouldn't arbitrarily enforce coherence as that could lead to poor
performance due to constant migration.

System allocator itself doesn't enforce coherence. Coherence is built in user

programming model. So system allocator allow both GPU and CPU access system
allocated pointers, but it doesn't necessarily guarantee the data accessed from
CPU/GPU is coherent. It is user program's responsibility to maintain data
coherence.

Quoted 3 lines

Data migration in driver is optional, depending on platform capability, user

preference, correctness and performance consideration. Driver internal data
migration of course shouldn't break data coherence.

Quoted 3 lines

Of course different vendor can have different data coherence scheme. For

example, it is completely designer's flexibility to build model that is HW automatic
data coherence or software explicit data coherence. On our platform, we allow
user program to select different coherence mode by setting pat_index for gpu
and cpu_caching mode for CPU. So we have completely give the flexibility to user
program. Nothing of this contract is changed in system allocator design.

Quoted 3 lines

Going back to the question of what memory type we should use to register our

vram to core mm. HMM currently support two types: PRIVATE and COHERENT.
The coherent type requires some HW and BIOS support which we don't have
right now. So the only available is PRIVATE. We have not other option right now.
As said, we plan to support coherent type where we can avoid unnecessary data
migration. But that is stage 2.

Quoted 1 line

Thanks for the explaination. After reading your replies, the HMM doc,
and looking at code this all makes sense.

Quoted 23 lines
Quoted 23 lines
Quoted 23 lines
Quoted 23 lines
Quoted 11 lines

For GPU device which supports coherent memory protocol b/t CPU and
GPU (such as CXL and CAPI protocol), we can remap device memory as
MEMORY_DEVICE_COHERENT. This is TBD.

Co-developed-by Niranjana Vishwanathapura

<[email protected]>

Quoted 1 line
Signed-off-by Niranjana Vishwanathapura

<[email protected]>

Quoted 9 lines
Cc Matthew Brost <[email protected]>
Cc Thomas Hellström <[email protected]>
Cc Brian Welty <[email protected]>

---
 drivers/gpu/drm/xe/Makefile          |  3 +-
 drivers/gpu/drm/xe/xe_device_types.h |  9 +++
 drivers/gpu/drm/xe/xe_mmio.c         |  8 +++
 drivers/gpu/drm/xe/xe_svm.h          | 14 +++++
 drivers/gpu/drm/xe/xe_svm_devmem.c   | 91

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines

++++++++++++++++++++++++++++

Review

Quoted 5 lines
 5 files changed, 124 insertions(+), 1 deletion(-)

 create mode 100644 drivers/gpu/drm/xe/xe_svm.h create mode 100644 drivers/gpu/drm/xe/xe_svm_devmem.c

(quoted diff) +0 -0

b/drivers/gpu/drm/xe/Makefile

Quoted 20 lines
Quoted 20 lines
Quoted 20 lines
Quoted 4 lines
Quoted 4 lines

index c531210695db..840467080e59 100644

drivers/gpu/drm/xe/Makefile +0 -0
@@ -142,7 +142,8 @@ xe-y += xe_bb.o \
Quoted 8 lines

drivers/gpu/drm/xe/Makefile xe-y += xe_bb.o \ · line 146

Quoted 6 lines
drivers/gpu/drm/xe/Makefile +2 -1
  	xe_vram_freq.o \
  	xe_wait_user_fence.o \
  	xe_wa.o \
-	xe_wopcm.o
+	xe_wopcm.o \
+	xe_svm_devmem.o

These should be in alphabetical order.

Will fix

Quoted 6 lines

Review

Quoted 5 lines
drivers/gpu/drm/xe/Makefile +0 -0
  # graphics hardware monitoring (HWMON) support

 xe-$(CONFIG_HWMON) += xe_hwmon.o

(quoted diff) +0 -0

b/drivers/gpu/drm/xe/xe_device_types.h

Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
Quoted 10 lines

index 9785eef2e5a4..f27c3bee8ce7 100644

drivers/gpu/drm/xe/xe_device_types.h +1 -0
@@ -99,6 +99,15 @@ struct xe_mem_region {
  	resource_size_t actual_physical_size;
  	/** @mapping: pointer to VRAM mappable space
 */
  	void __iomem *mapping;
+	/** @pagemap: Used to remap device memory as

ZONE_DEVICE

*/

Quoted 44 lines
Quoted 44 lines
Quoted 12 lines
Quoted 11 lines
Quoted 8 lines

+    struct dev_pagemap pagemap;
+    /**
+     * @hpa_base: base host physical address +     *
+     * This is generated when remap device memory as ZONE_DEVICE
+     */
+    resource_size_t hpa_base;

Weird indentation. This goes for the entire series, look at checkpatch.

Quoted 7 lines

Will fix

Quoted 6 lines

Review

Quoted 6 lines

+
 };

 /**

(quoted diff) +0 -0

b/drivers/gpu/drm/xe/xe_mmio.c

Quoted 24 lines
Quoted 23 lines
Quoted 13 lines

index e3db3a178760..0d795394bc4c 100644

drivers/gpu/drm/xe/xe_mmio.c +1 -0
@@ -22,6 +22,7 @@
  #include "xe_module.h"
  #include "xe_sriov.h"
  #include "xe_tile.h"
+#include "xe_svm.h"
 
  #define
 XEHP_MTCFG_ADDR		XE_REG(0x101800)

 #define TILE_COUNT REG_GENMASK(15, 8)

drivers/gpu/drm/xe/xe_mmio.c int xe_mmio_probe_vram(struct · line 293

Quoted 7 lines
drivers/gpu/drm/xe/xe_mmio.c +1 -0
@@ -286,6 +287,7 @@ int xe_mmio_probe_vram(struct
 xe_device *xe)
  		}
 
  		io_size -= min_t(u64, tile_size,
 io_size);
+		xe_svm_devm_add(tile, &tile-
 >mem.vram);

Do we want to do this probe for all devices with VRAM or only a subset?

Quoted 6 lines
Quoted 3 lines
Quoted 1 line

All

Can you explain why?

It is natural for me to add all device memory to hmm. In hmm design, device

memory is used as a special swap out for system memory. I would ask why we
only want to add a subset of vram? By a subset, do you mean only vram of one
tile instead of all tiles?

Quoted 1 line
Quoted 7 lines

I think we talking about different things, my bad on wording in the
original question.

Let me ask again - should be calling xe_svm_devm_add on all *platforms*
that have VRAM. i.e. Should we do this on PVC but not DG2?

Oh, I see. Good question. On i915, this feature was only tested on PVC. We don't have a plan to enable it on older platform than PVC.

Let me add a check here, only enabled it on platform newer than PVC

Quoted 6 lines
Quoted 4 lines

Probably actually check 'xe->info.has_usm'.

We might want to rename field too and drop the 'usm' nomenclature but that can be done later.

Perhaps "has_recoverable_pagefaults" or some for of abbreviation.

USM has two flavors: a driver allocator and a system allocator.

Both of those two flavors depend on recoverable pagefault. And for our current implementation, they only depend on recoverable page fault HW feature. In the future, we might have other implementation that depends on more HW features, such as ATS (address translation service).

So at least for now, "has_usm" and "has_recoverable_pagefaults" are pretty much the same thing. I will keep the has_usm for now. Open to a change in the future also.

Quoted 5 lines

Another question w r t this is whether we should do this unconditionally even on platforms that support it. Adding a struct_page per VRAM page would potentially consume a significant amount of system memory.

Yes, I was thinking of add a kernel configuration option so this feature can be enable/disabled at compilation time. Do you want me to add one?

Oak

Quoted 329 lines

/Thomas

Quoted 324 lines

Matt

Quoted 321 lines

Oak

Quoted 319 lines

Matt

Quoted 11 lines

Oak

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

  }

  xe->mem.vram.actual_physical_size =
total_size;

drivers/gpu/drm/xe/xe_mmio.c +0 -0
@@ -354,10 +356,16 @@ void xe_mmio_probe_tiles(struct

xe_device

*xe)

Quoted 20 lines
Quoted 20 lines
Quoted 20 lines
Quoted 19 lines
Quoted 14 lines

 static void mmio_fini(struct drm_device *drm, void *arg)
 {
  struct xe_device *xe = arg;
+    struct xe_tile *tile;
+    u8 id;

  pci_iounmap(to_pci_dev(xe->drm.dev), xe-

Quoted 1 line

mmio.regs);

  if (xe->mem.vram.mapping)
  iounmap(xe->mem.vram.mapping);

drivers/gpu/drm/xe/xe_mmio.c +3 -0
+
+	for_each_tile(tile, xe, id)
+		xe_svm_devm_remove(xe, &tile-
Quoted 1 line
Quoted 1 line

mem.vram);

This should probably be above existing code. Typical on fini to do
things in reverse order from init.

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines

Will fix

Quoted 6 lines

Review

Quoted 6 lines

+
 }

 static int xe_verify_lmem_ready(struct xe_device *xe)

(quoted diff) +0 -0

b/drivers/gpu/drm/xe/xe_svm.h

Quoted 28 lines
Quoted 28 lines
Quoted 28 lines
Quoted 10 lines

drivers/gpu/drm/xe/xe_svm.h line 3

Quoted 8 lines

new file mode 100644
index 000000000000..09f9afb0e7d4

drivers/gpu/drm/xe/xe_svm.h new file +3 -0
@@ -0,0 +1,14 @@
+// SPDX-License-Identifier: MIT
+/*
+ * Copyright © 2023 Intel Corporation

2024?

This patch was actually written 2023

Quoted 11 lines

drivers/gpu/drm/xe/xe_svm.h line 9

Quoted 7 lines
drivers/gpu/drm/xe/xe_svm.h new file +6 -0
+ */
+
+#ifndef __XE_SVM_H
+#define __XE_SVM_H
+
+#include "xe_device_types.h"

I don't think you need to include this. Rather just forward decl structs
used here.

Will fix

Quoted 3 lines

e.g.

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines

struct xe_device;
struct xe_mem_region;
struct xe_tile;

Quoted 3 lines
drivers/gpu/drm/xe/xe_svm.h new file +2 -0
+
+int xe_svm_devm_add(struct xe_tile *tile, struct

xe_mem_region

*mem);

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

+void xe_svm_devm_remove(struct xe_device *xe, struct

xe_mem_region

Quoted 69 lines
Quoted 69 lines
Quoted 69 lines
Quoted 5 lines

*mem);

The arguments here are incongruent here. Typically we want these to
match.

Will fix

Quoted 7 lines

Review

Quoted 4 lines
drivers/gpu/drm/xe/xe_svm.h new file +2 -0
+
+#endif
(quoted diff) +0 -0

b/drivers/gpu/drm/xe/xe_svm_devmem.c

Incongruent between xe_svm.h and xe_svm_devmem.c.

Did you mean mem vs mr? if yes, will fix

Again these two

Quoted 13 lines

should
match.

drivers/gpu/drm/xe/xe_svm_devmem.c line 3

Quoted 8 lines

new file mode 100644
index 000000000000..63b7a1961cc6

drivers/gpu/drm/xe/xe_svm_devmem.c new file +3 -0
@@ -0,0 +1,91 @@
+// SPDX-License-Identifier: MIT
+/*
+ * Copyright © 2023 Intel Corporation

2024?

It is from 2023

Quoted 10 lines

drivers/gpu/drm/xe/xe_svm_devmem.c line 10

Quoted 8 lines
drivers/gpu/drm/xe/xe_svm_devmem.c new file +7 -0
+ */
+
+#include <linux/mm_types.h>
+#include <linux/sched/mm.h>
+
+#include "xe_device_types.h"
+#include "xe_trace.h"

xe_trace.h appears to be unused.

Will fix

Quoted 23 lines
Quoted 16 lines
drivers/gpu/drm/xe/xe_svm_devmem.c new file +4 -0
+#include "xe_svm.h"
+
+
+static vm_fault_t xe_devm_migrate_to_ram(struct

vm_fault *vmf)

drivers/gpu/drm/xe/xe_svm_devmem.c new file +9 -0
+{
+	return 0;
+}
+
+static void xe_devm_page_free(struct page *page)
+{
+}
+
+static const struct dev_pagemap_ops

xe_devm_pagemap_ops = {

drivers/gpu/drm/xe/xe_svm_devmem.c

Quoted 4 lines
drivers/gpu/drm/xe/xe_svm_devmem.c new file +4 -0
+	.page_free = xe_devm_page_free,
+	.migrate_to_ram = xe_devm_migrate_to_ram,
+};
+

Assume these are placeholders that will be populated later?

Quoted 5 lines
Quoted 5 lines
Quoted 5 lines

corrrect

Quoted 4 lines
Quoted 4 lines

+/**
+ * xe_svm_devm_add: Remap and provide memmap backing for

device

Quoted 13 lines
Quoted 13 lines
Quoted 13 lines
Quoted 13 lines

memory

Quoted 11 lines

+ * @tile: tile that the memory region blongs to
+ * @mr: memory region to remap
+ *
+ * This remap device memory to host physical address space and create
+ * struct page to back device memory
+ *
+ * Return: 0 on success standard error code otherwise
+ */
+int xe_svm_devm_add(struct xe_tile *tile, struct xe_mem_region *mr)

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines

Here I see the xe_mem_region is from tile->mem.vram, wondering rather
than using the tile->mem.vram we should use xe->mem.vram when

enabling

Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 2 lines

svm? Isn't the idea behind svm the entire memory is 1 view?

Still need to use tile. The reason is, memory of different tile can have

different

Quoted 2 lines
Quoted 2 lines

characteristics, such as latency. So we want to differentiate memory b/t tiles

also

Quoted 2 lines
Quoted 2 lines

in svm. I need to change below " mr->pagemap.owner = tile-

Quoted 1 line

xe->drm.dev ".

the

Quoted 2 lines
Quoted 2 lines

owner should also be tile. This is the way hmm differentiate memory of

different

Quoted 4 lines
Quoted 4 lines

tile.

Quoted 3 lines

With svm it is 1 view, from virtual address space
perspective and from

physical

Quoted 2 lines
Quoted 2 lines

struct page perspective. You can think of all the tile's vram is stacked together

to

Quoted 2 lines
Quoted 2 lines

form a unified view together with system memory. This doesn't prohibit us

from

Quoted 2 lines
Quoted 2 lines

differentiate memory from different tile. This
differentiation allow us to

optimize

Quoted 10 lines
Quoted 10 lines

performance, i.e., we can wisely place memory in specific tile. If we don't
differentiate, this is not possible.

Quoted 1 line

Ok makes sense.

Matt

Quoted 1 line
Quoted 1 line
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 4 lines

I suppose if we do that we also only use 1 TTM VRAM manager / buddy
allocator too. I thought I saw some patches flying around for that too.

Ttm vram manager is not in the picture. We deliberately avoided it per

previous

Quoted 3 lines
Quoted 3 lines

discussion

Quoted 2 lines

Yes same buddy allocator. It is in my previous POC:

https://lore.kernel.org/dri-

Quoted 2 lines
Quoted 2 lines

devel/[email protected]/. I didn't put those

patches

Quoted 28 lines
Quoted 28 lines

in this series because I want to merge this small patches separately.

Quoted 26 lines
Quoted 26 lines
Quoted 26 lines
drivers/gpu/drm/xe/xe_svm_devmem.c new file +2 -0
+{
+	struct device *dev = &to_pci_dev(tile->xe-
Quoted 1 line

drm.dev)->dev;

drivers/gpu/drm/xe/xe_svm_devmem.c new file +5 -0
+	struct resource *res;
+	void *addr;
+	int ret;
+
+	res = devm_request_free_mem_region(dev,

&iomem_resource,

drivers/gpu/drm/xe/xe_svm_devmem.c new file +1 -0
+					   mr-
Quoted 1 line

usable_size);

drivers/gpu/drm/xe/xe_svm_devmem.c new file +14 -0
+	if (IS_ERR(res)) {
+		ret = PTR_ERR(res);
+		return ret;
+	}
+
+	mr->pagemap.type = MEMORY_DEVICE_PRIVATE;
+	mr->pagemap.range.start = res->start;
+	mr->pagemap.range.end = res->end;
+	mr->pagemap.nr_range = 1;
+	mr->pagemap.ops = &xe_devm_pagemap_ops;
+	mr->pagemap.owner = tile->xe->drm.dev;
+	addr = devm_memremap_pages(dev, &mr->pagemap);
+	if (IS_ERR(addr)) {
+		devm_release_mem_region(dev, res-
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

start,

resource_size(res));

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
drivers/gpu/drm/xe/xe_svm_devmem.c new file +2 -0
+		ret = PTR_ERR(addr);
+		drm_err(&tile->xe->drm, "Failed to

remap tile %d

memory,

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

errno %d\n",

Quoted 7 lines
drivers/gpu/drm/xe/xe_svm_devmem.c new file +6 -0
+				tile->id, ret);
+		return ret;
+	}
+	mr->hpa_base = res->start;
+
+	drm_info(&tile->xe->drm, "Added tile %d memory

[%llx-%llx] to

devm,

Quoted 5 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

remapped to %pr\n",

Quoted 2 lines
drivers/gpu/drm/xe/xe_svm_devmem.c new file +1 -0
+			tile->id, mr->io_start, mr-
Quoted 1 line

io_start + mr-

usable_size,

Quoted 1 line
Quoted 1 line
Quoted 1 line

res);

Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
drivers/gpu/drm/xe/xe_svm_devmem.c new file +5 -0
+	return 0;
+}
+
+/**
+ * xe_svm_devm_remove: Unmap device memory and free

resources
+ * @xe: xe device
+ * @mr: memory region to remove
+ */
+void xe_svm_devm_remove(struct xe_device *xe, struct

xe_mem_region

Quoted 32 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines

*mr)

Quoted 5 lines
drivers/gpu/drm/xe/xe_svm_devmem.c new file +2 -0
+{
+	/*FIXME: below cause a kernel hange during

moduel remove*/

drivers/gpu/drm/xe/xe_svm_devmem.c new file +2 -0
+#if 0
+	struct device *dev = &to_pci_dev(xe->drm.dev)-
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines

drivers/gpu/drm/xe/xe_svm_devmem.c

Quoted 6 lines
Quoted 1 line

dev;

drivers/gpu/drm/xe/xe_svm_devmem.c new file +3 -0
+
+	if (mr->hpa_base) {
+		devm_memunmap_pages(dev, &mr-
Quoted 1 line

pagemap);

drivers/gpu/drm/xe/xe_svm_devmem.c new file +1 -0
+		devm_release_mem_region(dev, mr-

pagemap.range.start,

Quoted 1 line
Quoted 1 line
Quoted 1 line

drivers/gpu/drm/xe/xe_svm_devmem.c

Quoted 1 line
drivers/gpu/drm/xe/xe_svm_devmem.c new file +1 -0
+			mr->pagemap.range.end - mr-

pagemap.range.start +1);

Quoted 17 lines
Quoted 17 lines
Quoted 4 lines

drivers/gpu/drm/xe/xe_svm_devmem.c

Quoted 2 lines
drivers/gpu/drm/xe/xe_svm_devmem.c new file +2 -0
+	}
+#endif

This would need to be fixed too.

Yes...

Oak

Quoted 8 lines

Matt

Quoted 5 lines
drivers/gpu/drm/xe/xe_svm_devmem.c new file +2 -1
+}
+
--

2.26.3

#11 lore raw

Re: [PATCH 1/5] drm/xe/svm: Remap and provide memmap backing for GPU vram

Quoted 465 lines · Oak
Quoted 437 lines

-----Original Message-----
From: Hellstrom, Thomas <[email protected]>
Sent: Monday, March 18, 2024 6:16 AM
To: Brost, Matthew <[email protected]>; Zeng, Oak <[email protected]>
Cc: [email protected]; Welty, Brian <[email protected]>;
[email protected]; Ghimiray, Himal Prasad <[email protected]>
Subject: Re: [PATCH 1/5] drm/xe/svm: Remap and provide memmap backing for
GPU vram

Quoted 159 lines · Matthew Brost
Quoted 158 lines · Oak
Quoted 158 lines

-----Original Message-----
From: Brost, Matthew <[email protected]>
Sent: Friday, March 15, 2024 4:40 PM
To: Zeng, Oak <[email protected]>
Cc: [email protected]; Hellstrom, Thomas <[email protected]>; [email protected]; Welty, Brian <[email protected]>; Ghimiray, Himal Prasad <[email protected]>
Subject: Re: [PATCH 1/5] drm/xe/svm: Remap and provide memmap backing for
GPU vram

Quoted 14 lines · Oak
Quoted 14 lines

-----Original Message-----
From: Brost, Matthew <[email protected]>
Sent: Thursday, March 14, 2024 4:49 PM
To: Zeng, Oak <[email protected]>
Cc: [email protected]; Hellstrom, Thomas <[email protected]>; [email protected]; Welty, Brian
<[email protected]>; Ghimiray, Himal Prasad <[email protected]>
Subject: Re: [PATCH 1/5] drm/xe/svm: Remap and provide memmap
backing

for

Quoted 6 lines
Quoted 6 lines

GPU vram

Quoted 2 lines · Oak

Hi Matt,

Quoted 13 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines

-----Original Message-----
From: Brost, Matthew <[email protected]>
Sent: Thursday, March 14, 2024 1:18 PM
To: Zeng, Oak <[email protected]>
Cc: [email protected]; Hellstrom, Thomas

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

<[email protected]>; [email protected]; Welty,
Brian
<[email protected]>; Ghimiray, Himal Prasad <[email protected]>
Subject: Re: [PATCH 1/5] drm/xe/svm: Remap and provide
memmap

backing

Quoted 7 lines
Quoted 7 lines

for

Quoted 6 lines
Quoted 6 lines

GPU vram

Quoted 2 lines · Oak Zeng

Memory remap GPU vram using devm_memremap_pages, so each

GPU

Quoted 11 lines
Quoted 11 lines

vram

Quoted 10 lines
Quoted 10 lines
Quoted 10 lines

page is backed by a struct page.

Those struct pages are created to allow hmm migrate buffer b/t
GPU vram and CPU system memory using existing Linux migration
mechanism (i.e., migrating b/t CPU system memory and
hard disk).

Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines

This is prepare work to enable svm (shared virtual memory) through
Linux kernel hmm framework. The memory remap's page map
type is

set

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

to MEMORY_DEVICE_PRIVATE for now. This means even
though each

GPU

Quoted 19 lines
Quoted 19 lines
Quoted 19 lines
Quoted 19 lines
Quoted 11 lines

vram page get a struct page and can be mapped in CPU
page table,
but such pages are treated as GPU's private resource,
so CPU can't
access them. If CPU access such page, a page fault is
triggered
and page will be migrate to system memory.

Is this really true? We can map VRAM BOs to the CPU without having
migarte back and forth. Admittedly I don't know the inner
workings of
how this works but in IGTs we do this all the time.

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines

  54         batch_bo = xe_bo_create(fd, vm, batch_size,
  55
vram_if_possible(fd,
0),
  56

DRM_XE_GEM_CREATE_FLAG_NEEDS_VISIBLE_VRAM);

Quoted 32 lines
Quoted 32 lines
Quoted 32 lines
Quoted 31 lines

  57         batch_map = xe_bo_map(fd, batch_bo, batch_size);

The BO is created in VRAM and then mapped to the CPU.

I don't think there is an expectation of coherence rather
caching mode
and exclusive access of the memory based on synchronization.

e.g.
User write BB/data via CPU to GPU memory User calls exec
GPU read / write memory
User wait on sync indicating exec done User reads result

All of this works without migration. Are we not planing
supporting flow
with SVM?

Afaik this migration dance really only needs to be done
if the CPU and
GPU are using atomics on a shared memory region and the
GPU device
doesn't support a coherent memory protocol (e.g. PVC).

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

All you said is true. On many of our HW, CPU can
actually
access device

memory,

Quoted 5 lines
Quoted 5 lines

cache coherently or not.

Quoted 4 lines

The problem is, this is not true for all GPU vendors. For
example, on some

HW

Quoted 2 lines
Quoted 2 lines

from some vendor, CPU can only access partially of device memory. The so

called

Quoted 9 lines
Quoted 9 lines

small bar concept.

Quoted 3 lines

So when HMM is defined, such factors were considered, and

MEMORY_DEVICE_PRIVATE is defined. With this definition, CPU
can't access
device memory.

Quoted 1 line
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 2 lines

So you can think it is a limitation of HMM.

Is it though? I see other type MEMORY_DEVICE_FS_DAX, MEMORY_DEVICE_GENERIC, and MEMORY_DEVICE_PCI_P2PDMA.

From my

Quoted 256 lines
Quoted 255 lines
Quoted 237 lines
Quoted 9 lines
Quoted 5 lines

limited
understanding it looks to me like one of those modes would
support my
example.

No, above are for other purposes. HMM only support DEVICE_PRIVATE and

DEVICE_COHERENT.

Quoted 8 lines
Quoted 8 lines
Quoted 5 lines

Note this is only part 1 of our system allocator work. We
do plan to support

DEVICE_COHERENT for our newer device, see below. With this
option, we

don't

Quoted 5 lines
Quoted 5 lines

have unnecessary migration back and forth.

Quoted 4 lines

You can think this is just work out all the code path. 90%
of the driver code

for

Quoted 6 lines
Quoted 6 lines

DEVICE_PRIVATE and DEVICE_COHERENT will be same. Our real use
of system
allocator will be DEVICE_COHERENT mode. While DEVICE_PRIVATE
mode

allow us

Quoted 23 lines
Quoted 15 lines

to exercise the code on old HW.

Quoted 3 lines

Make sense?

I guess if we want the system allocator to always coherent,
then yes you
need this dynamic migration with faulting on either side.

I was thinking the system allocator would be behave like my
example
above with madvise dictating the coherence rules.

Quoted 7 lines

Maybe I missed this in system allocator design but my feeling
is we
shouldn't arbitrarily enforce coherence as that could lead to
poor
performance due to constant migration.

Quoted 3 lines

System allocator itself doesn't enforce coherence. Coherence is
built in user

programming model. So system allocator allow both GPU and CPU access system
allocated pointers, but it doesn't necessarily guarantee the data
accessed from
CPU/GPU is coherent. It is user program's responsibility to maintain data
coherence.

Quoted 3 lines

Data migration in driver is optional, depending on platform capability, user

preference, correctness and performance consideration. Driver internal data
migration of course shouldn't break data coherence.

Quoted 4 lines

Of course different vendor can have different data coherence
scheme. For

example, it is completely designer's flexibility to build model
that is HW automatic
data coherence or software explicit data coherence. On our platform, we allow
user program to select different coherence mode by setting pat_index for gpu
and cpu_caching mode for CPU. So we have completely give the flexibility to user
program. Nothing of this contract is changed in system allocator
design.

Quoted 4 lines

Going back to the question of what memory type we should use to
register our

vram to core mm. HMM currently support two types: PRIVATE and COHERENT.
The coherent type requires some HW and BIOS support which we don't have
right now. So the only available is PRIVATE. We have not other
option right now.
As said, we plan to support coherent type where we can avoid unnecessary data
migration. But that is stage 2.

Quoted 1 line

Thanks for the explaination. After reading your replies, the HMM
doc,
and looking at code this all makes sense.

Quoted 24 lines
Quoted 24 lines
Quoted 24 lines
Quoted 24 lines
Quoted 10 lines

For GPU device which supports coherent memory
protocol
b/t CPU and
GPU (such as CXL and CAPI protocol), we can remap
device memory as
MEMORY_DEVICE_COHERENT. This is TBD.

Quoted 2 lines
Co-developed-by Niranjana Vishwanathapura

<[email protected]>

Quoted 1 line
Signed-off-by Niranjana Vishwanathapura

<[email protected]>

Quoted 9 lines
Cc Matthew Brost <[email protected]>
Cc Thomas Hellström <[email protected]>
Cc Brian Welty <[email protected]>

---
 drivers/gpu/drm/xe/Makefile          |  3 +-
 drivers/gpu/drm/xe/xe_device_types.h |  9 +++
 drivers/gpu/drm/xe/xe_mmio.c         |  8 +++
 drivers/gpu/drm/xe/xe_svm.h          | 14 +++++
 drivers/gpu/drm/xe/xe_svm_devmem.c   | 91

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines

++++++++++++++++++++++++++++

Review

Quoted 6 lines
 5 files changed, 124 insertions(+), 1 deletion(-)

 create mode 100644 drivers/gpu/drm/xe/xe_svm.h
 create mode 100644
drivers/gpu/drm/xe/xe_svm_devmem.c

(quoted diff) +0 -0

b/drivers/gpu/drm/xe/Makefile

Quoted 20 lines
Quoted 20 lines
Quoted 20 lines
Quoted 4 lines
Quoted 4 lines

index c531210695db..840467080e59 100644

drivers/gpu/drm/xe/Makefile +0 -0
@@ -142,7 +142,8 @@ xe-y += xe_bb.o \
Quoted 8 lines

drivers/gpu/drm/xe/Makefile xe-y += xe_bb.o \ · line 146

Quoted 6 lines
drivers/gpu/drm/xe/Makefile +2 -1
  	xe_vram_freq.o \
  	xe_wait_user_fence.o \
  	xe_wa.o \
-	xe_wopcm.o
+	xe_wopcm.o \
+	xe_svm_devmem.o

These should be in alphabetical order.

Will fix

Quoted 6 lines

Review

Quoted 5 lines
drivers/gpu/drm/xe/Makefile +0 -0
  # graphics hardware monitoring (HWMON) support

 xe-$(CONFIG_HWMON) += xe_hwmon.o

(quoted diff) +0 -0

b/drivers/gpu/drm/xe/xe_device_types.h

Quoted 12 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines

index 9785eef2e5a4..f27c3bee8ce7 100644

drivers/gpu/drm/xe/xe_device_types.h +0 -0
@@ -99,6 +99,15 @@ struct xe_mem_region {
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
drivers/gpu/drm/xe/xe_device_types.h +0 -0
  	resource_size_t actual_physical_size;
  	/** @mapping: pointer to VRAM mappable

space
*/
  void __iomem *mapping;

drivers/gpu/drm/xe/xe_device_types.h +1 -0
+	/** @pagemap: Used to remap device memory

as
ZONE_DEVICE

*/

Quoted 47 lines
Quoted 47 lines
Quoted 14 lines
Quoted 10 lines
Quoted 9 lines

+    struct dev_pagemap pagemap;
+    /**
+     * @hpa_base: base host physical address +     *
+     * This is generated when remap device memory as
ZONE_DEVICE
+     */
+    resource_size_t hpa_base;

Quoted 3 lines

Weird indentation. This goes for the entire series, look
at checkpatch.

Quoted 7 lines

Will fix

Quoted 6 lines

Review

Quoted 6 lines

+
 };

 /**

(quoted diff) +0 -0

b/drivers/gpu/drm/xe/xe_mmio.c

Quoted 25 lines
Quoted 24 lines
Quoted 13 lines

index e3db3a178760..0d795394bc4c 100644

drivers/gpu/drm/xe/xe_mmio.c +1 -0
@@ -22,6 +22,7 @@
  #include "xe_module.h"
  #include "xe_sriov.h"
  #include "xe_tile.h"
+#include "xe_svm.h"
 
  #define
 XEHP_MTCFG_ADDR		XE_REG(0x101800)

 #define TILE_COUNT REG_GENMASK(15, 8)

drivers/gpu/drm/xe/xe_mmio.c int xe_mmio_probe_vram(struct · line 293

Quoted 7 lines
drivers/gpu/drm/xe/xe_mmio.c +1 -0
@@ -286,6 +287,7 @@ int xe_mmio_probe_vram(struct
 xe_device *xe)
  		}
 
  		io_size -= min_t(u64, tile_size,
 io_size);
+		xe_svm_devm_add(tile, &tile-
 > mem.vram);

Do we want to do this probe for all devices with VRAM or
only a subset?

Quoted 7 lines
Quoted 3 lines
Quoted 1 line

All

Can you explain why?

It is natural for me to add all device memory to hmm. In hmm
design, device

memory is used as a special swap out for system memory. I would
ask why we
only want to add a subset of vram? By a subset, do you mean only
vram of one
tile instead of all tiles?

Quoted 1 line
Quoted 8 lines

I think we talking about different things, my bad on wording in
the
original question.

Let me ask again - should be calling xe_svm_devm_add on all *platforms*
that have VRAM. i.e. Should we do this on PVC but not DG2?

Oh, I see. Good question. On i915, this feature was only tested on
PVC. We don't have a plan to enable it on older platform than PVC.

Let me add a check here, only enabled it on platform newer than PVC

Quoted 7 lines
Quoted 5 lines

Probably actually check 'xe->info.has_usm'.

We might want to rename field too and drop the 'usm' nomenclature but
that can be done later.

Perhaps "has_recoverable_pagefaults" or some for of abbreviation.

USM has two flavors: a driver allocator and a system allocator.

Both of those two flavors depend on recoverable pagefault. And for our current implementation, they only depend on recoverable page fault HW feature. In the future, we might have other implementation that depends on more HW features, such as ATS (address translation service).

So at least for now, "has_usm" and "has_recoverable_pagefaults" are pretty much the same thing. I will keep the has_usm for now. Open to a change in the future also.

Quoted 7 lines

Another question w r t this is whether we should do this unconditionally even on platforms that support it. Adding a struct_page
per VRAM page would potentially consume a significant amount of system
memory.

Quoted 3 lines

Yes, I was thinking of add a kernel configuration option so this feature can be enable/disabled at compilation time. Do you want me to add one?

Yes I think that is a good idea.

We also might want to add per device enabling in sysfs, or setup on first use and disable if unused at shrinking time.

But IMO let's start with a kernel configuration option for now.

/Thomas

Quoted 355 lines

Oak

Quoted 352 lines

/Thomas

Quoted 347 lines

Matt

Quoted 344 lines

Oak

Quoted 342 lines

Matt

Quoted 5 lines

Oak

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines

  }

  xe->mem.vram.actual_physical_size =
total_size;

drivers/gpu/drm/xe/xe_mmio.c +0 -0
@@ -354,10 +356,16 @@ void

xe_mmio_probe_tiles(struct
xe_device

*xe)

Quoted 21 lines
Quoted 21 lines
Quoted 21 lines
Quoted 20 lines
Quoted 14 lines

 static void mmio_fini(struct drm_device *drm, void *arg)
 {
  struct xe_device *xe = arg;
+    struct xe_tile *tile;
+    u8 id;

  pci_iounmap(to_pci_dev(xe->drm.dev), xe-

Quoted 1 line

mmio.regs);

  if (xe->mem.vram.mapping)
  iounmap(xe->mem.vram.mapping);

drivers/gpu/drm/xe/xe_mmio.c +3 -0
+
+	for_each_tile(tile, xe, id)
+		xe_svm_devm_remove(xe, &tile-
Quoted 1 line
Quoted 1 line

mem.vram);

This should probably be above existing code. Typical on
fini to do
things in reverse order from init.

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

Will fix

Quoted 7 lines

Review

Quoted 7 lines

+
 }

 static int xe_verify_lmem_ready(struct xe_device
*xe)

(quoted diff) +0 -0

b/drivers/gpu/drm/xe/xe_svm.h

Quoted 28 lines
Quoted 28 lines
Quoted 28 lines
Quoted 10 lines

drivers/gpu/drm/xe/xe_svm.h line 3

Quoted 8 lines

new file mode 100644
index 000000000000..09f9afb0e7d4

drivers/gpu/drm/xe/xe_svm.h new file +3 -0
@@ -0,0 +1,14 @@
+// SPDX-License-Identifier: MIT
+/*
+ * Copyright © 2023 Intel Corporation

2024?

This patch was actually written 2023

Quoted 11 lines

drivers/gpu/drm/xe/xe_svm.h line 9

Quoted 7 lines
drivers/gpu/drm/xe/xe_svm.h new file +6 -0
+ */
+
+#ifndef __XE_SVM_H
+#define __XE_SVM_H
+
+#include "xe_device_types.h"

I don't think you need to include this. Rather just forward decl structs
used here.

Will fix

Quoted 3 lines

e.g.

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines

struct xe_device;
struct xe_mem_region;
struct xe_tile;

Quoted 3 lines
drivers/gpu/drm/xe/xe_svm.h new file +2 -0
+
+int xe_svm_devm_add(struct xe_tile *tile, struct

xe_mem_region

*mem);

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

+void xe_svm_devm_remove(struct xe_device *xe,
struct

xe_mem_region

Quoted 69 lines
Quoted 69 lines
Quoted 69 lines
Quoted 5 lines

*mem);

The arguments here are incongruent here. Typically we want these to
match.

Will fix

Quoted 7 lines

Review

Quoted 4 lines
drivers/gpu/drm/xe/xe_svm.h new file +2 -0
+
+#endif
(quoted diff) +0 -0

b/drivers/gpu/drm/xe/xe_svm_devmem.c

Incongruent between xe_svm.h and xe_svm_devmem.c.

Did you mean mem vs mr? if yes, will fix

Again these two

Quoted 13 lines

should
match.

drivers/gpu/drm/xe/xe_svm_devmem.c line 3

Quoted 8 lines

new file mode 100644
index 000000000000..63b7a1961cc6

drivers/gpu/drm/xe/xe_svm_devmem.c new file +3 -0
@@ -0,0 +1,91 @@
+// SPDX-License-Identifier: MIT
+/*
+ * Copyright © 2023 Intel Corporation

2024?

It is from 2023

Quoted 10 lines

drivers/gpu/drm/xe/xe_svm_devmem.c line 10

Quoted 8 lines
drivers/gpu/drm/xe/xe_svm_devmem.c new file +7 -0
+ */
+
+#include <linux/mm_types.h>
+#include <linux/sched/mm.h>
+
+#include "xe_device_types.h"
+#include "xe_trace.h"

xe_trace.h appears to be unused.

Will fix

Quoted 23 lines
Quoted 16 lines
drivers/gpu/drm/xe/xe_svm_devmem.c new file +4 -0
+#include "xe_svm.h"
+
+
+static vm_fault_t xe_devm_migrate_to_ram(struct

vm_fault *vmf)

drivers/gpu/drm/xe/xe_svm_devmem.c new file +9 -0
+{
+	return 0;
+}
+
+static void xe_devm_page_free(struct page *page)
+{
+}
+
+static const struct dev_pagemap_ops

xe_devm_pagemap_ops = {

drivers/gpu/drm/xe/xe_svm_devmem.c

Quoted 4 lines
drivers/gpu/drm/xe/xe_svm_devmem.c new file +4 -0
+	.page_free = xe_devm_page_free,
+	.migrate_to_ram = xe_devm_migrate_to_ram,
+};
+

Assume these are placeholders that will be populated later?

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines

corrrect

Quoted 5 lines
Quoted 5 lines

+/**
+ * xe_svm_devm_add: Remap and provide memmap
backing
for

device

Quoted 15 lines
Quoted 15 lines
Quoted 15 lines
Quoted 15 lines

memory

Quoted 13 lines

+ * @tile: tile that the memory region blongs to
+ * @mr: memory region to remap
+ *
+ * This remap device memory to host physical
address
space and create
+ * struct page to back device memory
+ *
+ * Return: 0 on success standard error code
otherwise
+ */
+int xe_svm_devm_add(struct xe_tile *tile, struct
xe_mem_region *mr)

Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines

Here I see the xe_mem_region is from tile->mem.vram, wondering rather
than using the tile->mem.vram we should use xe-

Quoted 1 line

mem.vram

when

enabling

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 2 lines

svm? Isn't the idea behind svm the entire memory is 1 view?

Still need to use tile. The reason is, memory of
different
tile can have

different

Quoted 3 lines
Quoted 3 lines

3 lines not shown · raw message

150 lines not shown · raw message

Part of this message is not shown here. View the raw message.

author
#12 lore raw

RE: [PATCH 1/5] drm/xe/svm: Remap and provide memmap backing for GPU vram

In reply to Matthew Brost
Quoted 330 lines

-----Original Message-----
From: Brost, Matthew <[email protected]>
Sent: Friday, March 15, 2024 9:26 PM
To: Zeng, Oak <[email protected]>
Cc: [email protected]; Hellstrom, Thomas <[email protected]>; [email protected]; Welty, Brian <[email protected]>; Ghimiray, Himal Prasad <[email protected]>
Subject: Re: [PATCH 1/5] drm/xe/svm: Remap and provide memmap backing for GPU vram

Quoted 11 lines · Oak
Quoted 11 lines

-----Original Message-----
From: Brost, Matthew <[email protected]>
Sent: Friday, March 15, 2024 4:40 PM
To: Zeng, Oak <[email protected]>
Cc: [email protected]; Hellstrom, Thomas <[email protected]>; [email protected]; Welty, Brian <[email protected]>; Ghimiray, Himal Prasad <[email protected]>
Subject: Re: [PATCH 1/5] drm/xe/svm: Remap and provide memmap backing

for

Quoted 5 lines
Quoted 5 lines

GPU vram

Quoted 2 lines · Oak
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines

-----Original Message-----
From: Brost, Matthew <[email protected]>
Sent: Thursday, March 14, 2024 4:49 PM
To: Zeng, Oak <[email protected]>
Cc: [email protected]; Hellstrom, Thomas <[email protected]>; [email protected]; Welty, Brian <[email protected]>; Ghimiray, Himal Prasad <[email protected]>
Subject: Re: [PATCH 1/5] drm/xe/svm: Remap and provide memmap

backing

Quoted 23 lines
Quoted 23 lines

for

Quoted 5 lines
Quoted 5 lines

GPU vram

Quoted 2 lines · Oak

Hi Matt,

Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines

-----Original Message-----
From: Brost, Matthew <[email protected]>
Sent: Thursday, March 14, 2024 1:18 PM
To: Zeng, Oak <[email protected]>
Cc: [email protected]; Hellstrom, Thomas <[email protected]>; [email protected]; Welty, Brian <[email protected]>; Ghimiray, Himal Prasad <[email protected]>
Subject: Re: [PATCH 1/5] drm/xe/svm: Remap and provide memmap

backing

Quoted 5 lines
Quoted 5 lines

for

Quoted 4 lines
Quoted 4 lines

GPU vram

Quoted 1 line · Oak Zeng

Memory remap GPU vram using devm_memremap_pages, so each

GPU

Quoted 1 line
Quoted 1 line

vram

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

page is backed by a struct page.

Those struct pages are created to allow hmm migrate buffer b/t GPU vram and CPU system memory using existing Linux migration mechanism (i.e., migrating b/t CPU system memory and hard disk).

This is prepare work to enable svm (shared virtual memory) through Linux kernel hmm framework. The memory remap's page map type

is

Quoted 2 lines
Quoted 2 lines

set

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

to MEMORY_DEVICE_PRIVATE for now. This means even though

each

Quoted 29 lines
Quoted 29 lines

GPU

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 5 lines

vram page get a struct page and can be mapped in CPU page table, but such pages are treated as GPU's private resource, so CPU can't access them. If CPU access such page, a page fault is triggered and page will be migrate to system memory.

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines

Is this really true? We can map VRAM BOs to the CPU without having migarte back and forth. Admittedly I don't know the inner workings of how this works but in IGTs we do this all the time.

54 batch_bo = xe_bo_create(fd, vm, batch_size,
55 vram_if_possible(fd, 0),
56

DRM_XE_GEM_CREATE_FLAG_NEEDS_VISIBLE_VRAM);

Quoted 14 lines
Quoted 14 lines
Quoted 14 lines
Quoted 14 lines
  57         batch_map = xe_bo_map(fd, batch_bo, batch_size);

The BO is created in VRAM and then mapped to the CPU.

I don't think there is an expectation of coherence rather caching mode and exclusive access of the memory based on synchronization.

e.g.
User write BB/data via CPU to GPU memory
User calls exec
GPU read / write memory
User wait on sync indicating exec done
User reads result

Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines

All of this works without migration. Are we not planing supporting flow with SVM?

Afaik this migration dance really only needs to be done if the CPU and GPU are using atomics on a shared memory region and the GPU

device

Quoted 7 lines
Quoted 7 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 1 line

doesn't support a coherent memory protocol (e.g. PVC).

All you said is true. On many of our HW, CPU can actually access device

memory,

Quoted 3 lines
Quoted 3 lines

cache coherently or not.

Quoted 2 lines

The problem is, this is not true for all GPU vendors. For example, on

some

Quoted 2 lines
Quoted 2 lines

HW

Quoted 1 line
Quoted 1 line

from some vendor, CPU can only access partially of device memory. The

so

Quoted 5 lines
Quoted 5 lines

called

Quoted 4 lines
Quoted 4 lines

small bar concept.

Quoted 2 lines

So when HMM is defined, such factors were considered, and

MEMORY_DEVICE_PRIVATE is defined. With this definition, CPU can't

access

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines

device memory.

Quoted 3 lines

So you can think it is a limitation of HMM.

Is it though? I see other type MEMORY_DEVICE_FS_DAX, MEMORY_DEVICE_GENERIC, and MEMORY_DEVICE_PCI_P2PDMA. From

my

Quoted 10 lines
Quoted 10 lines
Quoted 6 lines
Quoted 3 lines

limited
understanding it looks to me like one of those modes would support my example.

No, above are for other purposes. HMM only support DEVICE_PRIVATE and

DEVICE_COHERENT.

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

Note this is only part 1 of our system allocator work. We do plan to

support

Quoted 5 lines
Quoted 5 lines
Quoted 1 line
Quoted 1 line

DEVICE_COHERENT for our newer device, see below. With this option, we

don't

Quoted 3 lines
Quoted 3 lines

have unnecessary migration back and forth.

Quoted 2 lines

You can think this is just work out all the code path. 90% of the driver

code

Quoted 2 lines
Quoted 2 lines

for

Quoted 1 line
Quoted 1 line

DEVICE_PRIVATE and DEVICE_COHERENT will be same. Our real use of

system

Quoted 13 lines
Quoted 13 lines
Quoted 1 line
Quoted 1 line

allocator will be DEVICE_COHERENT mode. While DEVICE_PRIVATE mode

allow us

Quoted 11 lines
Quoted 11 lines

to exercise the code on old HW.

Quoted 3 lines

Make sense?

I guess if we want the system allocator to always coherent, then yes you need this dynamic migration with faulting on either side.

I was thinking the system allocator would be behave like my example above with madvise dictating the coherence rules.

Quoted 6 lines
Quoted 6 lines
Quoted 5 lines
Quoted 3 lines

Maybe I missed this in system allocator design but my feeling is we shouldn't arbitrarily enforce coherence as that could lead to poor performance due to constant migration.

System allocator itself doesn't enforce coherence. Coherence is built in user

programming model. So system allocator allow both GPU and CPU access

system

Quoted 1 line
Quoted 1 line

allocated pointers, but it doesn't necessarily guarantee the data accessed

from

Quoted 9 lines
Quoted 9 lines

CPU/GPU is coherent. It is user program's responsibility to maintain data coherence.

Quoted 2 lines

Data migration in driver is optional, depending on platform capability, user

preference, correctness and performance consideration. Driver internal data migration of course shouldn't break data coherence.

Quoted 2 lines

Of course different vendor can have different data coherence scheme. For

example, it is completely designer's flexibility to build model that is HW

automatic

Quoted 1 line
Quoted 1 line

data coherence or software explicit data coherence. On our platform, we

allow

Quoted 1 line
Quoted 1 line

user program to select different coherence mode by setting pat_index for

gpu

Quoted 1 line
Quoted 1 line

and cpu_caching mode for CPU. So we have completely give the flexibility to

user

Quoted 3 lines
Quoted 3 lines

program. Nothing of this contract is changed in system allocator design.

Quoted 2 lines

Going back to the question of what memory type we should use to register

our

Quoted 3 lines
Quoted 3 lines

vram to core mm. HMM currently support two types: PRIVATE and COHERENT. The coherent type requires some HW and BIOS support which we don't have right now. So the only available is PRIVATE. We have not other option right

now.

Quoted 1 line
Quoted 1 line

As said, we plan to support coherent type where we can avoid unnecessary

data

Quoted 10 lines
Quoted 10 lines

migration. But that is stage 2.

Quoted 1 line

Thanks for the explaination. After reading your replies, the HMM doc, and looking at code this all makes sense.

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines

For GPU device which supports coherent memory protocol b/t CPU

and

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

GPU (such as CXL and CAPI protocol), we can remap device memory

as

Quoted 52 lines
Quoted 52 lines
Quoted 16 lines
Quoted 16 lines
Quoted 16 lines
Quoted 16 lines
Quoted 4 lines

MEMORY_DEVICE_COHERENT. This is TBD.

Co-developed-by Niranjana Vishwanathapura

<[email protected]>

Quoted 1 line
Signed-off-by Niranjana Vishwanathapura

<[email protected]>

Quoted 9 lines
Cc Matthew Brost <[email protected]>
Cc Thomas Hellström <[email protected]>
Cc Brian Welty <[email protected]>
---
 drivers/gpu/drm/xe/Makefile          |  3 +-
 drivers/gpu/drm/xe/xe_device_types.h |  9 +++
 drivers/gpu/drm/xe/xe_mmio.c         |  8 +++
 drivers/gpu/drm/xe/xe_svm.h          | 14 +++++
 drivers/gpu/drm/xe/xe_svm_devmem.c   | 91
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines

++++++++++++++++++++++++++++

Review

Quoted 5 lines
 5 files changed, 124 insertions(+), 1 deletion(-)
 create mode 100644 drivers/gpu/drm/xe/xe_svm.h
 create mode 100644 drivers/gpu/drm/xe/xe_svm_devmem.c
(quoted diff) +0 -0

b/drivers/gpu/drm/xe/Makefile

Quoted 20 lines
Quoted 20 lines
Quoted 20 lines
Quoted 4 lines
Quoted 4 lines

index c531210695db..840467080e59 100644

drivers/gpu/drm/xe/Makefile +0 -0
@@ -142,7 +142,8 @@ xe-y += xe_bb.o \
Quoted 8 lines

drivers/gpu/drm/xe/Makefile xe-y += xe_bb.o \ · line 146

Quoted 6 lines
drivers/gpu/drm/xe/Makefile +2 -1
 	xe_vram_freq.o \
 	xe_wait_user_fence.o \
 	xe_wa.o \
-	xe_wopcm.o
+	xe_wopcm.o \
+	xe_svm_devmem.o

These should be in alphabetical order.

Will fix

Quoted 6 lines

Review

Quoted 5 lines
drivers/gpu/drm/xe/Makefile +0 -0
 # graphics hardware monitoring (HWMON) support
 xe-$(CONFIG_HWMON) += xe_hwmon.o
(quoted diff) +0 -0

b/drivers/gpu/drm/xe/xe_device_types.h

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

drivers/gpu/drm/xe/xe_device_types.h struct xe_mem_region { · line 102

Quoted 8 lines

index 9785eef2e5a4..f27c3bee8ce7 100644

drivers/gpu/drm/xe/xe_device_types.h +1 -0
@@ -99,6 +99,15 @@ struct xe_mem_region {
 	resource_size_t actual_physical_size;
 	/** @mapping: pointer to VRAM mappable space */
 	void __iomem *mapping;
+	/** @pagemap: Used to remap device memory as ZONE_DEVICE

*/

Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines

drivers/gpu/drm/xe/xe_device_types.h struct xe_mem_region { · line 107

Quoted 5 lines
drivers/gpu/drm/xe/xe_device_types.h +5 -0
+    struct dev_pagemap pagemap;
+    /**
+     * @hpa_base: base host physical address
+     *
+     * This is generated when remap device memory as

ZONE_DEVICE

Quoted 22 lines
Quoted 22 lines
Quoted 22 lines
Quoted 22 lines
Quoted 5 lines
Quoted 4 lines

drivers/gpu/drm/xe/xe_device_types.h struct xe_mem_region { · line 109

Quoted 2 lines
drivers/gpu/drm/xe/xe_device_types.h +2 -0
+     */
+    resource_size_t hpa_base;

Weird indentation. This goes for the entire series, look at checkpatch.

Quoted 7 lines

Will fix

Quoted 6 lines

Review

Quoted 6 lines
drivers/gpu/drm/xe/xe_device_types.h +1 -0
+
 };
 
 /**
(quoted diff) +0 -0

b/drivers/gpu/drm/xe/xe_mmio.c

Quoted 9 lines
Quoted 9 lines
Quoted 9 lines

index e3db3a178760..0d795394bc4c 100644

drivers/gpu/drm/xe/xe_mmio.c +1 -0
@@ -22,6 +22,7 @@
 #include "xe_module.h"
 #include "xe_sriov.h"
 #include "xe_tile.h"
+#include "xe_svm.h"
 
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

drivers/gpu/drm/xe/xe_mmio.c int xe_mmio_probe_vram(struct xe_device

Quoted 3 lines
drivers/gpu/drm/xe/xe_mmio.c +0 -0
 #define XEHP_MTCFG_ADDR		XE_REG(0x101800)
 #define TILE_COUNT		REG_GENMASK(15, 8)
@@ -286,6 +287,7 @@ int xe_mmio_probe_vram(struct xe_device

*xe)

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 6 lines

drivers/gpu/drm/xe/xe_mmio.c int xe_mmio_probe_vram(struct xe_device · line 290

Quoted 4 lines
drivers/gpu/drm/xe/xe_mmio.c +1 -0
 		}
 
 		io_size -= min_t(u64, tile_size, io_size);
+		xe_svm_devm_add(tile, &tile->mem.vram);

Do we want to do this probe for all devices with VRAM or only a subset?

Quoted 6 lines
Quoted 6 lines
Quoted 5 lines
Quoted 3 lines
Quoted 1 line

All

Can you explain why?

It is natural for me to add all device memory to hmm. In hmm design, device

memory is used as a special swap out for system memory. I would ask why

we

Quoted 1 line
Quoted 1 line

only want to add a subset of vram? By a subset, do you mean only vram of

one

Quoted 11 lines
Quoted 8 lines

tile instead of all tiles?

Quoted 1 line

I think we talking about different things, my bad on wording in the original question.

Let me ask again - should be calling xe_svm_devm_add on all *platforms* that have VRAM. i.e. Should we do this on PVC but not DG2?

Oh, I see. Good question. On i915, this feature was only tested on PVC. We

don't have a plan to enable it on older platform than PVC.

Quoted 1 line
Quoted 7 lines
Quoted 2 lines

Let me add a check here, only enabled it on platform newer than PVC

Probably actually check 'xe->info.has_usm'.

We might want to rename field too and drop the 'usm' nomenclature but that can be done later.

Good idea. Let me check with "has_usm"

Oak

Quoted 291 lines

Matt

Quoted 10 lines

Oak

Quoted 8 lines

Matt

Quoted 5 lines

Oak

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines

drivers/gpu/drm/xe/xe_mmio.c void xe_mmio_probe_tiles(struct

Quoted 4 lines
drivers/gpu/drm/xe/xe_mmio.c +0 -0
 	}
 
 	xe->mem.vram.actual_physical_size = total_size;
@@ -354,10 +356,16 @@ void xe_mmio_probe_tiles(struct

xe_device

Quoted 128 lines
Quoted 128 lines

*xe)

Quoted 16 lines
Quoted 16 lines
Quoted 16 lines
Quoted 15 lines

drivers/gpu/drm/xe/xe_mmio.c void xe_mmio_probe_tiles(struct · line 367

Quoted 12 lines
drivers/gpu/drm/xe/xe_mmio.c +5 -0
 static void mmio_fini(struct drm_device *drm, void *arg)
 {
 	struct xe_device *xe = arg;
+    struct xe_tile *tile;
+    u8 id;
 
 	pci_iounmap(to_pci_dev(xe->drm.dev), xe->mmio.regs);
 	if (xe->mem.vram.mapping)
 		iounmap(xe->mem.vram.mapping);
+
+	for_each_tile(tile, xe, id)
+		xe_svm_devm_remove(xe, &tile->mem.vram);

This should probably be above existing code. Typical on fini to do things in reverse order from init.

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines

Will fix

6 lines not shown · raw message

104 lines not shown · raw message

Quoted 8 lines

8 lines not shown · raw message

137 lines not shown · raw message

Part of this message is not shown here. View the raw message.

#13 lore raw

Re: [PATCH 1/5] drm/xe/svm: Remap and provide memmap backing for GPU vram

In reply to Oak Zeng

Hi Oak,

Quoted 40 lines · Oak Zeng

Memory remap GPU vram using devm_memremap_pages, so each GPU vram page is backed by a struct page.

Those struct pages are created to allow hmm migrate buffer b/t GPU vram and CPU system memory using existing Linux migration mechanism (i.e., migrating b/t CPU system memory and hard disk).

This is prepare work to enable svm (shared virtual memory) through Linux kernel hmm framework. The memory remap's page map type is set to MEMORY_DEVICE_PRIVATE for now. This means even though each GPU vram page get a struct page and can be mapped in CPU page table, but such pages are treated as GPU's private resource, so CPU can't access them. If CPU access such page, a page fault is triggered and page will be migrate to system memory.

For GPU device which supports coherent memory protocol b/t CPU and GPU (such as CXL and CAPI protocol), we can remap device memory as MEMORY_DEVICE_COHERENT. This is TBD.

---
  drivers/gpu/drm/xe/Makefile          |  3 +-
  drivers/gpu/drm/xe/xe_device_types.h |  9 +++
  drivers/gpu/drm/xe/xe_mmio.c         |  8 +++
  drivers/gpu/drm/xe/xe_svm.h          | 14 +++++
  drivers/gpu/drm/xe/xe_svm_devmem.c   | 91 ++++++++++++++++++++++++++++
  5 files changed, 124 insertions(+), 1 deletion(-)

create mode 100644 drivers/gpu/drm/xe/xe_svm.h create mode 100644 drivers/gpu/drm/xe/xe_svm_devmem.c

drivers/gpu/drm/xe/Makefile +0 -0
index c531210695db..840467080e59 100644

drivers/gpu/drm/xe/Makefile xe-y += xe_bb.o \ · line 146

Quoted 6 lines
drivers/gpu/drm/xe/Makefile +2 -1
@@ -142,7 +142,8 @@ xe-y += xe_bb.o \
  	xe_vram_freq.o \
  	xe_wait_user_fence.o \
  	xe_wa.o \
-	xe_wopcm.o
+	xe_wopcm.o \
+	xe_svm_devmem.o

Minor, but maintainers want above alphabetically sorted.

Quoted 36 lines
drivers/gpu/drm/xe/Makefile +0 -0
  # graphics hardware monitoring (HWMON) support
  xe-$(CONFIG_HWMON) += xe_hwmon.o
drivers/gpu/drm/xe/xe_device_types.h +9 -0
index 9785eef2e5a4..f27c3bee8ce7 100644
@@ -99,6 +99,15 @@ struct xe_mem_region {
  	resource_size_t actual_physical_size;
  	/** @mapping: pointer to VRAM mappable space */
  	void __iomem *mapping;
+	/** @pagemap: Used to remap device memory as ZONE_DEVICE */
+    struct dev_pagemap pagemap;
+    /**
+     * @hpa_base: base host physical address
+     *
+     * This is generated when remap device memory as ZONE_DEVICE
+     */
+    resource_size_t hpa_base;
+
  };
 
  /**
drivers/gpu/drm/xe/xe_mmio.c +1 -0
index e3db3a178760..0d795394bc4c 100644
@@ -22,6 +22,7 @@
  #include "xe_module.h"
  #include "xe_sriov.h"
  #include "xe_tile.h"
+#include "xe_svm.h"
 
  #define XEHP_MTCFG_ADDR		XE_REG(0x101800)
  #define TILE_COUNT		REG_GENMASK(15, 8)

drivers/gpu/drm/xe/xe_mmio.c int xe_mmio_probe_vram(struct xe_device *xe) · line 290

Quoted 4 lines
drivers/gpu/drm/xe/xe_mmio.c +1 -0
@@ -286,6 +287,7 @@ int xe_mmio_probe_vram(struct xe_device *xe)
  		}
 
  		io_size -= min_t(u64, tile_size, io_size);
+		xe_svm_devm_add(tile, &tile->mem.vram);

I think slightly more appropriate call site for this might be xe_tile_init_noalloc(), as that function states it is preparing tile for VRAM allocations.
Also, I mention because we might like the flexiblity in future to call this once for xe_device.mem.vram, instead of calling for each tile, and easier to handle that in xe_tile.c instead of xe_mmio.c.

Related comment below.

Quoted 65 lines
drivers/gpu/drm/xe/xe_mmio.c +6 -0
  	}
 
  	xe->mem.vram.actual_physical_size = total_size;
@@ -354,10 +356,16 @@ void xe_mmio_probe_tiles(struct xe_device *xe)
  static void mmio_fini(struct drm_device *drm, void *arg)
  {
  	struct xe_device *xe = arg;
+    struct xe_tile *tile;
+    u8 id;
 
  	pci_iounmap(to_pci_dev(xe->drm.dev), xe->mmio.regs);
  	if (xe->mem.vram.mapping)
  		iounmap(xe->mem.vram.mapping);
+
+	for_each_tile(tile, xe, id)
+		xe_svm_devm_remove(xe, &tile->mem.vram);
+
  }
 
  static int xe_verify_lmem_ready(struct xe_device *xe)
drivers/gpu/drm/xe/xe_svm.h new file +14 -0
new file mode 100644
index 000000000000..09f9afb0e7d4
@@ -0,0 +1,14 @@
+// SPDX-License-Identifier: MIT
+/*
+ * Copyright © 2023 Intel Corporation
+ */
+
+#ifndef __XE_SVM_H
+#define __XE_SVM_H
+
+#include "xe_device_types.h"
+
+int xe_svm_devm_add(struct xe_tile *tile, struct xe_mem_region *mem);
+void xe_svm_devm_remove(struct xe_device *xe, struct xe_mem_region *mem);
+
+#endif
drivers/gpu/drm/xe/xe_svm_devmem.c new file +19 -0
new file mode 100644
index 000000000000..63b7a1961cc6
@@ -0,0 +1,91 @@
+// SPDX-License-Identifier: MIT
+/*
+ * Copyright © 2023 Intel Corporation
+ */
+
+#include <linux/mm_types.h>
+#include <linux/sched/mm.h>
+
+#include "xe_device_types.h"
+#include "xe_trace.h"
+#include "xe_svm.h"
+
+
+static vm_fault_t xe_devm_migrate_to_ram(struct vm_fault *vmf)
+{
+	return 0;
+}
+
+static void xe_devm_page_free(struct page *page)

drivers/gpu/drm/xe/xe_svm_devmem.c line 29

Quoted 10 lines
drivers/gpu/drm/xe/xe_svm_devmem.c new file +10 -0
+{
+}
+
+static const struct dev_pagemap_ops xe_devm_pagemap_ops = {
+	.page_free = xe_devm_page_free,
+	.migrate_to_ram = xe_devm_migrate_to_ram,
+};
+
+/**
+ * xe_svm_devm_add: Remap and provide memmap backing for device memory

Do we really need 'svm' in function name?

drivers/gpu/drm/xe/xe_svm_devmem.c line 30

Quoted 1 line
drivers/gpu/drm/xe/xe_svm_devmem.c new file +1 -0
+ * @tile: tile that the memory region blongs to

We might like flexibility in future to call this once for xe_device.mem.vram, instead of calling for each tile. So can we remove the tile argument, and just pass the xe_device pointer
and tile->id ?

Quoted 61 lines
drivers/gpu/drm/xe/xe_svm_devmem.c new file +61 -0
+ * @mr: memory region to remap
+ *
+ * This remap device memory to host physical address space and create
+ * struct page to back device memory
+ *
+ * Return: 0 on success standard error code otherwise
+ */
+int xe_svm_devm_add(struct xe_tile *tile, struct xe_mem_region *mr)
+{
+	struct device *dev = &to_pci_dev(tile->xe->drm.dev)->dev;
+	struct resource *res;
+	void *addr;
+	int ret;
+
+	res = devm_request_free_mem_region(dev, &iomem_resource,
+					   mr->usable_size);
+	if (IS_ERR(res)) {
+		ret = PTR_ERR(res);
+		return ret;
+	}
+
+	mr->pagemap.type = MEMORY_DEVICE_PRIVATE;
+	mr->pagemap.range.start = res->start;
+	mr->pagemap.range.end = res->end;
+	mr->pagemap.nr_range = 1;
+	mr->pagemap.ops = &xe_devm_pagemap_ops;
+	mr->pagemap.owner = tile->xe->drm.dev;
+	addr = devm_memremap_pages(dev, &mr->pagemap);
+	if (IS_ERR(addr)) {
+		devm_release_mem_region(dev, res->start, resource_size(res));
+		ret = PTR_ERR(addr);
+		drm_err(&tile->xe->drm, "Failed to remap tile %d memory, errno %d\n",
+				tile->id, ret);
+		return ret;
+	}
+	mr->hpa_base = res->start;
+
+	drm_info(&tile->xe->drm, "Added tile %d memory [%llx-%llx] to devm, remapped to %pr\n",
+			tile->id, mr->io_start, mr->io_start + mr->usable_size, res);
+	return 0;
+}
+
+/**
+ * xe_svm_devm_remove: Unmap device memory and free resources
+ * @xe: xe device
+ * @mr: memory region to remove
+ */
+void xe_svm_devm_remove(struct xe_device *xe, struct xe_mem_region *mr)
+{
+	/*FIXME: below cause a kernel hange during moduel remove*/
+#if 0
+	struct device *dev = &to_pci_dev(xe->drm.dev)->dev;
+
+	if (mr->hpa_base) {
+		devm_memunmap_pages(dev, &mr->pagemap);
+		devm_release_mem_region(dev, mr->pagemap.range.start,
+			mr->pagemap.range.end - mr->pagemap.range.start +1);
+	}
+#endif
+}
+
author
#14 lore raw

RE: [PATCH 1/5] drm/xe/svm: Remap and provide memmap backing for GPU vram

Quoted 53 lines

-----Original Message-----
From: Welty, Brian <[email protected]>
Sent: Thursday, March 14, 2024 9:46 PM
To: Zeng, Oak <[email protected]>; [email protected]
Cc: Hellstrom, Thomas <[email protected]>; Brost, Matthew <[email protected]>; [email protected]; Ghimiray, Himal Prasad <[email protected]>
Subject: Re: [PATCH 1/5] drm/xe/svm: Remap and provide memmap backing for GPU vram

Hi Oak,

Quoted 15 lines · Oak Zeng

Memory remap GPU vram using devm_memremap_pages, so each GPU vram page is backed by a struct page.

Those struct pages are created to allow hmm migrate buffer b/t GPU vram and CPU system memory using existing Linux migration mechanism (i.e., migrating b/t CPU system memory and hard disk).

This is prepare work to enable svm (shared virtual memory) through Linux kernel hmm framework. The memory remap's page map type is set to MEMORY_DEVICE_PRIVATE for now. This means even though each GPU vram page get a struct page and can be mapped in CPU page table, but such pages are treated as GPU's private resource, so CPU can't access them. If CPU access such page, a page fault is triggered and page will be migrate to system memory.

Quoted 6 lines

For GPU device which supports coherent memory protocol b/t CPU and GPU (such as CXL and CAPI protocol), we can remap device memory as MEMORY_DEVICE_COHERENT. This is TBD.

Co-developed-by Niranjana Vishwanathapura

<[email protected]>

Quoted 1 line
Signed-off-by Niranjana Vishwanathapura

<[email protected]>

Quoted 9 lines
Cc Matthew Brost <[email protected]>
Cc Thomas Hellström <[email protected]>
Cc Brian Welty <[email protected]>
---
  drivers/gpu/drm/xe/Makefile          |  3 +-
  drivers/gpu/drm/xe/xe_device_types.h |  9 +++
  drivers/gpu/drm/xe/xe_mmio.c         |  8 +++
  drivers/gpu/drm/xe/xe_svm.h          | 14 +++++
  drivers/gpu/drm/xe/xe_svm_devmem.c   | 91

++++++++++++++++++++++++++++

Quoted 4 lines
  5 files changed, 124 insertions(+), 1 deletion(-)

create mode 100644 drivers/gpu/drm/xe/xe_svm.h create mode 100644 drivers/gpu/drm/xe/xe_svm_devmem.c

Quoted 13 lines

drivers/gpu/drm/xe/Makefile xe-y += xe_bb.o \ · line 146

Quoted 11 lines
drivers/gpu/drm/xe/Makefile +2 -1
index c531210695db..840467080e59 100644
@@ -142,7 +142,8 @@ xe-y += xe_bb.o \
  	xe_vram_freq.o \
  	xe_wait_user_fence.o \
  	xe_wa.o \
-	xe_wopcm.o
+	xe_wopcm.o \
+	xe_svm_devmem.o

Minor, but maintainers want above alphabetically sorted.

Correct. Matt pointed out the same. Will fix

Quoted 40 lines

Review

Quoted 5 lines
drivers/gpu/drm/xe/Makefile +0 -0
  # graphics hardware monitoring (HWMON) support
  xe-$(CONFIG_HWMON) += xe_hwmon.o
(quoted diff) +0 -0

b/drivers/gpu/drm/xe/xe_device_types.h

Quoted 34 lines

index 9785eef2e5a4..f27c3bee8ce7 100644

drivers/gpu/drm/xe/xe_device_types.h +9 -0
@@ -99,6 +99,15 @@ struct xe_mem_region {
  	resource_size_t actual_physical_size;
  	/** @mapping: pointer to VRAM mappable space */
  	void __iomem *mapping;
+	/** @pagemap: Used to remap device memory as ZONE_DEVICE */
+    struct dev_pagemap pagemap;
+    /**
+     * @hpa_base: base host physical address
+     *
+     * This is generated when remap device memory as ZONE_DEVICE
+     */
+    resource_size_t hpa_base;
+
  };
 
  /**
drivers/gpu/drm/xe/xe_mmio.c +1 -0
index e3db3a178760..0d795394bc4c 100644
@@ -22,6 +22,7 @@
  #include "xe_module.h"
  #include "xe_sriov.h"
  #include "xe_tile.h"
+#include "xe_svm.h"
 
  #define XEHP_MTCFG_ADDR		XE_REG(0x101800)
  #define TILE_COUNT		REG_GENMASK(15, 8)
@@ -286,6 +287,7 @@ int xe_mmio_probe_vram(struct xe_device *xe)
  		}
 
Quoted 9 lines

drivers/gpu/drm/xe/xe_mmio.c int xe_mmio_probe_vram(struct xe_device *xe) · line 290

Quoted 2 lines
drivers/gpu/drm/xe/xe_mmio.c +1 -0
  		io_size -= min_t(u64, tile_size, io_size);
+		xe_svm_devm_add(tile, &tile->mem.vram);

I think slightly more appropriate call site for this might be xe_tile_init_noalloc(), as that function states it is preparing tile for VRAM allocations.
Also, I mention because we might like the flexiblity in future to call this once for xe_device.mem.vram, instead of calling for each tile, and easier to handle that in xe_tile.c instead of xe_mmio.c.

Good point. Will follow.

Quoted 83 lines

Related comment below.

Quoted 28 lines
drivers/gpu/drm/xe/xe_mmio.c +6 -0
  	}
 
  	xe->mem.vram.actual_physical_size = total_size;
@@ -354,10 +356,16 @@ void xe_mmio_probe_tiles(struct xe_device *xe)
  static void mmio_fini(struct drm_device *drm, void *arg)
  {
  	struct xe_device *xe = arg;
+    struct xe_tile *tile;
+    u8 id;
 
  	pci_iounmap(to_pci_dev(xe->drm.dev), xe->mmio.regs);
  	if (xe->mem.vram.mapping)
  		iounmap(xe->mem.vram.mapping);
+
+	for_each_tile(tile, xe, id)
+		xe_svm_devm_remove(xe, &tile->mem.vram);
+
  }
 
  static int xe_verify_lmem_ready(struct xe_device *xe)
drivers/gpu/drm/xe/xe_svm.h new file +2 -0
new file mode 100644
index 000000000000..09f9afb0e7d4
@@ -0,0 +1,14 @@
+// SPDX-License-Identifier: MIT
+/*

drivers/gpu/drm/xe/xe_svm.h line 12

Quoted 10 lines
drivers/gpu/drm/xe/xe_svm.h new file +10 -0
+ * Copyright © 2023 Intel Corporation
+ */
+
+#ifndef __XE_SVM_H
+#define __XE_SVM_H
+
+#include "xe_device_types.h"
+
+int xe_svm_devm_add(struct xe_tile *tile, struct xe_mem_region *mem);
+void xe_svm_devm_remove(struct xe_device *xe, struct xe_mem_region

*mem);

Review

Quoted 3 lines
drivers/gpu/drm/xe/xe_svm.h new file +2 -0
+
+#endif
(quoted diff) +0 -0

b/drivers/gpu/drm/xe/xe_svm_devmem.c

Quoted 24 lines

new file mode 100644
index 000000000000..63b7a1961cc6

drivers/gpu/drm/xe/xe_svm_devmem.c new file +19 -0
@@ -0,0 +1,91 @@
+// SPDX-License-Identifier: MIT
+/*
+ * Copyright © 2023 Intel Corporation
+ */
+
+#include <linux/mm_types.h>
+#include <linux/sched/mm.h>
+
+#include "xe_device_types.h"
+#include "xe_trace.h"
+#include "xe_svm.h"
+
+
+static vm_fault_t xe_devm_migrate_to_ram(struct vm_fault *vmf)
+{
+	return 0;
+}
+
+static void xe_devm_page_free(struct page *page)

drivers/gpu/drm/xe/xe_svm_devmem.c line 29

Quoted 10 lines
drivers/gpu/drm/xe/xe_svm_devmem.c new file +10 -0
+{
+}
+
+static const struct dev_pagemap_ops xe_devm_pagemap_ops = {
+	.page_free = xe_devm_page_free,
+	.migrate_to_ram = xe_devm_migrate_to_ram,
+};
+
+/**
+ * xe_svm_devm_add: Remap and provide memmap backing for device

memory

Quoted 1 line

Do we really need 'svm' in function name?

Good point. I will remove svm.

Quoted 7 lines

drivers/gpu/drm/xe/xe_svm_devmem.c line 30

Quoted 2 lines
drivers/gpu/drm/xe/xe_svm_devmem.c new file +1 -0
+ * @tile: tile that the memory region blongs to

We might like flexibility in future to call this once for xe_device.mem.vram, instead of calling for each tile.
So can we remove the tile argument, and just pass the xe_device pointer and tile->id ?

This is interesting.

First of all, I programmed wrong below: mr->pagemap.owner = tile->xe->drm.dev;

This should be: mr->pagemap.owner = tile for NUMA vram system.

For UMA vram, this should be: mr->pagemap.owner = tile_to_xe(tile);

This owner is important. It is used later to decide migration by hmm. We need to set the owner for hmm to identify different vram region.

Based on above, I think the tile parameter is better. For UMA, caller need to make sure call it once, any tile pointer should work. This does sound a little weird. But I don’t have a better idea.

Oak

Quoted 67 lines
Quoted 24 lines
drivers/gpu/drm/xe/xe_svm_devmem.c new file +22 -0
+ * @mr: memory region to remap
+ *
+ * This remap device memory to host physical address space and create
+ * struct page to back device memory
+ *
+ * Return: 0 on success standard error code otherwise
+ */
+int xe_svm_devm_add(struct xe_tile *tile, struct xe_mem_region *mr)
+{
+	struct device *dev = &to_pci_dev(tile->xe->drm.dev)->dev;
+	struct resource *res;
+	void *addr;
+	int ret;
+
+	res = devm_request_free_mem_region(dev, &iomem_resource,
+					   mr->usable_size);
+	if (IS_ERR(res)) {
+		ret = PTR_ERR(res);
+		return ret;
+	}
+
+	mr->pagemap.type = MEMORY_DEVICE_PRIVATE;

drivers/gpu/drm/xe/xe_svm_devmem.c

Quoted 10 lines
drivers/gpu/drm/xe/xe_svm_devmem.c new file +10 -0
+	mr->pagemap.range.start = res->start;
+	mr->pagemap.range.end = res->end;
+	mr->pagemap.nr_range = 1;
+	mr->pagemap.ops = &xe_devm_pagemap_ops;
+	mr->pagemap.owner = tile->xe->drm.dev;
+	addr = devm_memremap_pages(dev, &mr->pagemap);
+	if (IS_ERR(addr)) {
+		devm_release_mem_region(dev, res->start, resource_size(res));
+		ret = PTR_ERR(addr);
+		drm_err(&tile->xe->drm, "Failed to remap tile %d memory,

errno %d\n",

drivers/gpu/drm/xe/xe_svm_devmem.c

Quoted 6 lines
drivers/gpu/drm/xe/xe_svm_devmem.c new file +6 -0
+				tile->id, ret);
+		return ret;
+	}
+	mr->hpa_base = res->start;
+
+	drm_info(&tile->xe->drm, "Added tile %d memory [%llx-%llx] to devm,

remapped to %pr\n",

drivers/gpu/drm/xe/xe_svm_devmem.c line 69

Quoted 1 line
drivers/gpu/drm/xe/xe_svm_devmem.c new file +1 -0
+			tile->id, mr->io_start, mr->io_start + mr->usable_size,

res);

drivers/gpu/drm/xe/xe_svm_devmem.c

Quoted 9 lines
drivers/gpu/drm/xe/xe_svm_devmem.c new file +9 -0
+	return 0;
+}
+
+/**
+ * xe_svm_devm_remove: Unmap device memory and free resources
+ * @xe: xe device
+ * @mr: memory region to remove
+ */
+void xe_svm_devm_remove(struct xe_device *xe, struct xe_mem_region

*mr)

Quoted 13 lines
drivers/gpu/drm/xe/xe_svm_devmem.c new file +13 -0
+{
+	/*FIXME: below cause a kernel hange during moduel remove*/
+#if 0
+	struct device *dev = &to_pci_dev(xe->drm.dev)->dev;
+
+	if (mr->hpa_base) {
+		devm_memunmap_pages(dev, &mr->pagemap);
+		devm_release_mem_region(dev, mr->pagemap.range.start,
+			mr->pagemap.range.end - mr->pagemap.range.start +1);
+	}
+#endif
+}
+
author
#15 lore raw

RE: [PATCH 1/5] drm/xe/svm: Remap and provide memmap backing for GPU vram

Quoted 133 lines

-----Original Message-----
From: Intel-xe <[email protected]> On Behalf Of Zeng, Oak
Sent: Thursday, March 14, 2024 11:10 PM
To: Welty, Brian <[email protected]>; [email protected]
Cc: Hellstrom, Thomas <[email protected]>; Brost, Matthew <[email protected]>; [email protected]; Ghimiray, Himal Prasad <[email protected]>
Subject: RE: [PATCH 1/5] drm/xe/svm: Remap and provide memmap backing for GPU vram

Quoted 8 lines

-----Original Message-----
From: Welty, Brian <[email protected]>
Sent: Thursday, March 14, 2024 9:46 PM
To: Zeng, Oak <[email protected]>; [email protected]
Cc: Hellstrom, Thomas <[email protected]>; Brost, Matthew <[email protected]>; [email protected]; Ghimiray, Himal Prasad <[email protected]>
Subject: Re: [PATCH 1/5] drm/xe/svm: Remap and provide memmap backing

for

Quoted 6 lines

GPU vram

Hi Oak,

Quoted 1 line · Oak Zeng

Memory remap GPU vram using devm_memremap_pages, so each GPU

vram

Quoted 37 lines
Quoted 14 lines

page is backed by a struct page.

Those struct pages are created to allow hmm migrate buffer b/t GPU vram and CPU system memory using existing Linux migration mechanism (i.e., migrating b/t CPU system memory and hard disk).

This is prepare work to enable svm (shared virtual memory) through Linux kernel hmm framework. The memory remap's page map type is set to MEMORY_DEVICE_PRIVATE for now. This means even though each GPU vram page get a struct page and can be mapped in CPU page table, but such pages are treated as GPU's private resource, so CPU can't access them. If CPU access such page, a page fault is triggered and page will be migrate to system memory.

Quoted 6 lines

For GPU device which supports coherent memory protocol b/t CPU and GPU (such as CXL and CAPI protocol), we can remap device memory as MEMORY_DEVICE_COHERENT. This is TBD.

Co-developed-by Niranjana Vishwanathapura

<[email protected]>

Quoted 1 line
Signed-off-by Niranjana Vishwanathapura

<[email protected]>

Quoted 9 lines
Cc Matthew Brost <[email protected]>
Cc Thomas Hellström <[email protected]>
Cc Brian Welty <[email protected]>
---
  drivers/gpu/drm/xe/Makefile          |  3 +-
  drivers/gpu/drm/xe/xe_device_types.h |  9 +++
  drivers/gpu/drm/xe/xe_mmio.c         |  8 +++
  drivers/gpu/drm/xe/xe_svm.h          | 14 +++++
  drivers/gpu/drm/xe/xe_svm_devmem.c   | 91

++++++++++++++++++++++++++++

Quoted 4 lines
  5 files changed, 124 insertions(+), 1 deletion(-)

create mode 100644 drivers/gpu/drm/xe/xe_svm.h create mode 100644 drivers/gpu/drm/xe/xe_svm_devmem.c

Quoted 13 lines

drivers/gpu/drm/xe/Makefile xe-y += xe_bb.o \ · line 146

Quoted 11 lines
drivers/gpu/drm/xe/Makefile +2 -1
index c531210695db..840467080e59 100644
@@ -142,7 +142,8 @@ xe-y += xe_bb.o \
  	xe_vram_freq.o \
  	xe_wait_user_fence.o \
  	xe_wa.o \
-	xe_wopcm.o
+	xe_wopcm.o \
+	xe_svm_devmem.o

Minor, but maintainers want above alphabetically sorted.

Correct. Matt pointed out the same. Will fix

Quoted 24 lines

Review

Quoted 5 lines
drivers/gpu/drm/xe/Makefile +0 -0
  # graphics hardware monitoring (HWMON) support
  xe-$(CONFIG_HWMON) += xe_hwmon.o
(quoted diff) +0 -0

b/drivers/gpu/drm/xe/xe_device_types.h

Quoted 18 lines

index 9785eef2e5a4..f27c3bee8ce7 100644

drivers/gpu/drm/xe/xe_device_types.h +9 -0
@@ -99,6 +99,15 @@ struct xe_mem_region {
  	resource_size_t actual_physical_size;
  	/** @mapping: pointer to VRAM mappable space */
  	void __iomem *mapping;
+	/** @pagemap: Used to remap device memory as ZONE_DEVICE */
+    struct dev_pagemap pagemap;
+    /**
+     * @hpa_base: base host physical address
+     *
+     * This is generated when remap device memory as ZONE_DEVICE
+     */
+    resource_size_t hpa_base;
+
  };
 
Quoted 2 lines

Review

Quoted 2 lines
drivers/gpu/drm/xe/xe_device_types.h +0 -0
  /**
(quoted diff) +0 -0

b/drivers/gpu/drm/xe/xe_mmio.c

Quoted 14 lines
Quoted 14 lines

index e3db3a178760..0d795394bc4c 100644

drivers/gpu/drm/xe/xe_mmio.c +1 -0
@@ -22,6 +22,7 @@
  #include "xe_module.h"
  #include "xe_sriov.h"
  #include "xe_tile.h"
+#include "xe_svm.h"
 
  #define XEHP_MTCFG_ADDR		XE_REG(0x101800)
  #define TILE_COUNT		REG_GENMASK(15, 8)
@@ -286,6 +287,7 @@ int xe_mmio_probe_vram(struct xe_device *xe)
  		}
 
Quoted 9 lines

drivers/gpu/drm/xe/xe_mmio.c int xe_mmio_probe_vram(struct xe_device *xe) · line 290

Quoted 2 lines
drivers/gpu/drm/xe/xe_mmio.c +1 -0
  		io_size -= min_t(u64, tile_size, io_size);
+		xe_svm_devm_add(tile, &tile->mem.vram);

I think slightly more appropriate call site for this might be xe_tile_init_noalloc(), as that function states it is preparing tile for VRAM allocations.
Also, I mention because we might like the flexiblity in future to call this once for xe_device.mem.vram, instead of calling for each tile, and easier to handle that in xe_tile.c instead of xe_mmio.c.

Quoted 1 line

Good point. Will follow.

Sorry, with my comment below, do you still want to call it in xe_tile_init_noalloc?

For UMA, we only need to call it once. If you do it in init-noalloc, you would call it multiple times. Right?

Oak

Quoted 178 lines
Quoted 83 lines

Related comment below.

Quoted 28 lines
drivers/gpu/drm/xe/xe_mmio.c +6 -0
  	}
 
  	xe->mem.vram.actual_physical_size = total_size;
@@ -354,10 +356,16 @@ void xe_mmio_probe_tiles(struct xe_device *xe)
  static void mmio_fini(struct drm_device *drm, void *arg)
  {
  	struct xe_device *xe = arg;
+    struct xe_tile *tile;
+    u8 id;
 
  	pci_iounmap(to_pci_dev(xe->drm.dev), xe->mmio.regs);
  	if (xe->mem.vram.mapping)
  		iounmap(xe->mem.vram.mapping);
+
+	for_each_tile(tile, xe, id)
+		xe_svm_devm_remove(xe, &tile->mem.vram);
+
  }
 
  static int xe_verify_lmem_ready(struct xe_device *xe)
drivers/gpu/drm/xe/xe_svm.h new file +2 -0
new file mode 100644
index 000000000000..09f9afb0e7d4
@@ -0,0 +1,14 @@
+// SPDX-License-Identifier: MIT
+/*

drivers/gpu/drm/xe/xe_svm.h line 12

Quoted 10 lines
drivers/gpu/drm/xe/xe_svm.h new file +10 -0
+ * Copyright © 2023 Intel Corporation
+ */
+
+#ifndef __XE_SVM_H
+#define __XE_SVM_H
+
+#include "xe_device_types.h"
+
+int xe_svm_devm_add(struct xe_tile *tile, struct xe_mem_region *mem);
+void xe_svm_devm_remove(struct xe_device *xe, struct xe_mem_region

*mem);

Review

Quoted 3 lines
drivers/gpu/drm/xe/xe_svm.h new file +2 -0
+
+#endif
(quoted diff) +0 -0

b/drivers/gpu/drm/xe/xe_svm_devmem.c

Quoted 24 lines

new file mode 100644
index 000000000000..63b7a1961cc6

drivers/gpu/drm/xe/xe_svm_devmem.c new file +19 -0
@@ -0,0 +1,91 @@
+// SPDX-License-Identifier: MIT
+/*
+ * Copyright © 2023 Intel Corporation
+ */
+
+#include <linux/mm_types.h>
+#include <linux/sched/mm.h>
+
+#include "xe_device_types.h"
+#include "xe_trace.h"
+#include "xe_svm.h"
+
+
+static vm_fault_t xe_devm_migrate_to_ram(struct vm_fault *vmf)
+{
+	return 0;
+}
+
+static void xe_devm_page_free(struct page *page)

drivers/gpu/drm/xe/xe_svm_devmem.c line 29

Quoted 10 lines
drivers/gpu/drm/xe/xe_svm_devmem.c new file +10 -0
+{
+}
+
+static const struct dev_pagemap_ops xe_devm_pagemap_ops = {
+	.page_free = xe_devm_page_free,
+	.migrate_to_ram = xe_devm_migrate_to_ram,
+};
+
+/**
+ * xe_svm_devm_add: Remap and provide memmap backing for device

memory

Quoted 1 line

Do we really need 'svm' in function name?

Good point. I will remove svm.

Quoted 7 lines

drivers/gpu/drm/xe/xe_svm_devmem.c line 30

Quoted 2 lines
drivers/gpu/drm/xe/xe_svm_devmem.c new file +1 -0
+ * @tile: tile that the memory region blongs to

We might like flexibility in future to call this once for xe_device.mem.vram, instead of calling for each tile.
So can we remove the tile argument, and just pass the xe_device pointer and tile->id ?

This is interesting.

First of all, I programmed wrong below: mr->pagemap.owner = tile->xe-

Quoted 1 line

drm.dev;

This should be: mr->pagemap.owner = tile for NUMA vram system.

For UMA vram, this should be: mr->pagemap.owner = tile_to_xe(tile);

This owner is important. It is used later to decide migration by hmm. We need to set the owner for hmm to identify different vram region.

Based on above, I think the tile parameter is better. For UMA, caller need to make sure call it once, any tile pointer should work. This does sound a little weird. But I don’t have a better idea.

Oak

Quoted 67 lines
Quoted 24 lines
drivers/gpu/drm/xe/xe_svm_devmem.c new file +22 -0
+ * @mr: memory region to remap
+ *
+ * This remap device memory to host physical address space and create
+ * struct page to back device memory
+ *
+ * Return: 0 on success standard error code otherwise
+ */
+int xe_svm_devm_add(struct xe_tile *tile, struct xe_mem_region *mr)
+{
+	struct device *dev = &to_pci_dev(tile->xe->drm.dev)->dev;
+	struct resource *res;
+	void *addr;
+	int ret;
+
+	res = devm_request_free_mem_region(dev, &iomem_resource,
+					   mr->usable_size);
+	if (IS_ERR(res)) {
+		ret = PTR_ERR(res);
+		return ret;
+	}
+
+	mr->pagemap.type = MEMORY_DEVICE_PRIVATE;

drivers/gpu/drm/xe/xe_svm_devmem.c

Quoted 10 lines
drivers/gpu/drm/xe/xe_svm_devmem.c new file +10 -0
+	mr->pagemap.range.start = res->start;
+	mr->pagemap.range.end = res->end;
+	mr->pagemap.nr_range = 1;
+	mr->pagemap.ops = &xe_devm_pagemap_ops;
+	mr->pagemap.owner = tile->xe->drm.dev;
+	addr = devm_memremap_pages(dev, &mr->pagemap);
+	if (IS_ERR(addr)) {
+		devm_release_mem_region(dev, res->start, resource_size(res));
+		ret = PTR_ERR(addr);
+		drm_err(&tile->xe->drm, "Failed to remap tile %d memory,

errno %d\n",

drivers/gpu/drm/xe/xe_svm_devmem.c

Quoted 6 lines
drivers/gpu/drm/xe/xe_svm_devmem.c new file +6 -0
+				tile->id, ret);
+		return ret;
+	}
+	mr->hpa_base = res->start;
+
+	drm_info(&tile->xe->drm, "Added tile %d memory [%llx-%llx] to devm,

remapped to %pr\n",

drivers/gpu/drm/xe/xe_svm_devmem.c line 69

Quoted 1 line
drivers/gpu/drm/xe/xe_svm_devmem.c new file +1 -0
+			tile->id, mr->io_start, mr->io_start + mr->usable_size,

res);

drivers/gpu/drm/xe/xe_svm_devmem.c

Quoted 9 lines
drivers/gpu/drm/xe/xe_svm_devmem.c new file +9 -0
+	return 0;
+}
+
+/**
+ * xe_svm_devm_remove: Unmap device memory and free resources
+ * @xe: xe device
+ * @mr: memory region to remove
+ */
+void xe_svm_devm_remove(struct xe_device *xe, struct xe_mem_region

*mr)

Quoted 13 lines
drivers/gpu/drm/xe/xe_svm_devmem.c new file +13 -0
+{
+	/*FIXME: below cause a kernel hange during moduel remove*/
+#if 0
+	struct device *dev = &to_pci_dev(xe->drm.dev)->dev;
+
+	if (mr->hpa_base) {
+		devm_memunmap_pages(dev, &mr->pagemap);
+		devm_release_mem_region(dev, mr->pagemap.range.start,
+			mr->pagemap.range.end - mr->pagemap.range.start +1);
+	}
+#endif
+}
+
#16 lore raw

Re: [PATCH 1/5] drm/xe/svm: Remap and provide memmap backing for GPU vram

On 3/14/2024 8:16 PM, Zeng, Oak wrote:
[...]

Quoted 26 lines
Quoted 26 lines
Quoted 1 line

Review

Quoted 1 line
(quoted diff) +0 -0

b/drivers/gpu/drm/xe/xe_mmio.c

Quoted 23 lines
Quoted 12 lines

index e3db3a178760..0d795394bc4c 100644

drivers/gpu/drm/xe/xe_mmio.c +1 -0
@@ -22,6 +22,7 @@
   #include "xe_module.h"
   #include "xe_sriov.h"
   #include "xe_tile.h"
+#include "xe_svm.h"
 
   #define XEHP_MTCFG_ADDR		XE_REG(0x101800)
   #define TILE_COUNT		REG_GENMASK(15, 8)

drivers/gpu/drm/xe/xe_mmio.c int xe_mmio_probe_vram(struct xe_device *xe) · line 290

Quoted 4 lines
drivers/gpu/drm/xe/xe_mmio.c +1 -0
@@ -286,6 +287,7 @@ int xe_mmio_probe_vram(struct xe_device *xe)
   		}
 
   		io_size -= min_t(u64, tile_size, io_size);
+		xe_svm_devm_add(tile, &tile->mem.vram);

I think slightly more appropriate call site for this might be xe_tile_init_noalloc(), as that function states it is preparing tile for VRAM allocations.
Also, I mention because we might like the flexiblity in future to call this once for xe_device.mem.vram, instead of calling for each tile, and easier to handle that in xe_tile.c instead of xe_mmio.c.

Quoted 8 lines
Quoted 1 line

Good point. Will follow.

Sorry, with my comment below, do you still want to call it in xe_tile_init_noalloc?

For UMA, we only need to call it once. If you do it in init-noalloc, you would call it multiple times. Right?

Oak

Oh, I hoped you were still going to move it.
I prefer it in xe_tile.c instead of here. But feel free to ask maintainers about it.
I think the UMA would anyway add conditional to disable these calls wherever they are placed, in favor of calling against xe_device.mem.vram. But this is pretty minor and can revisit later too.

I commented down below, to keep to one email.

Quoted 23 lines
Quoted 23 lines
Quoted 23 lines

Related comment below.

Quoted 19 lines
drivers/gpu/drm/xe/xe_mmio.c +6 -0
   	}
 
   	xe->mem.vram.actual_physical_size = total_size;
@@ -354,10 +356,16 @@ void xe_mmio_probe_tiles(struct xe_device *xe)
   static void mmio_fini(struct drm_device *drm, void *arg)
   {
   	struct xe_device *xe = arg;
+    struct xe_tile *tile;
+    u8 id;
 
   	pci_iounmap(to_pci_dev(xe->drm.dev), xe->mmio.regs);
   	if (xe->mem.vram.mapping)
   		iounmap(xe->mem.vram.mapping);
+
+	for_each_tile(tile, xe, id)
+		xe_svm_devm_remove(xe, &tile->mem.vram);
+
   }
 

[...]

Quoted 48 lines
Quoted 48 lines
Quoted 37 lines

b/drivers/gpu/drm/xe/xe_svm_devmem.c

Quoted 24 lines

new file mode 100644
index 000000000000..63b7a1961cc6

drivers/gpu/drm/xe/xe_svm_devmem.c new file +19 -0
@@ -0,0 +1,91 @@
+// SPDX-License-Identifier: MIT
+/*
+ * Copyright © 2023 Intel Corporation
+ */
+
+#include <linux/mm_types.h>
+#include <linux/sched/mm.h>
+
+#include "xe_device_types.h"
+#include "xe_trace.h"
+#include "xe_svm.h"
+
+
+static vm_fault_t xe_devm_migrate_to_ram(struct vm_fault *vmf)
+{
+	return 0;
+}
+
+static void xe_devm_page_free(struct page *page)

drivers/gpu/drm/xe/xe_svm_devmem.c line 29

Quoted 10 lines
drivers/gpu/drm/xe/xe_svm_devmem.c new file +10 -0
+{
+}
+
+static const struct dev_pagemap_ops xe_devm_pagemap_ops = {
+	.page_free = xe_devm_page_free,
+	.migrate_to_ram = xe_devm_migrate_to_ram,
+};
+
+/**
+ * xe_svm_devm_add: Remap and provide memmap backing for device

memory

Quoted 1 line

Do we really need 'svm' in function name?

Good point. I will remove svm.

Quoted 7 lines

drivers/gpu/drm/xe/xe_svm_devmem.c line 30

Quoted 2 lines
drivers/gpu/drm/xe/xe_svm_devmem.c new file +1 -0
+ * @tile: tile that the memory region blongs to

We might like flexibility in future to call this once for xe_device.mem.vram, instead of calling for each tile.
So can we remove the tile argument, and just pass the xe_device pointer and tile->id ?

Quoted 6 lines
Quoted 6 lines

This is interesting.

First of all, I programmed wrong below: mr->pagemap.owner = tile->xe-

Quoted 1 line

drm.dev;

This should be: mr->pagemap.owner = tile for NUMA vram system

Oh, okay. Glad we caught that.

Still, wouldn't using 'mr' pointer as the owner be sufficient and then is common for both cases? Avoiding the strong association with 'tile' is nice if possible.

But I haven't studied rest of code and how pagemap.owner is used, so maybe you actually need the tile pointer for something.

Well, this also seems like something minor and could be revisited later.

-Brian

Quoted 78 lines
Quoted 78 lines

For UMA vram, this should be: mr->pagemap.owner = tile_to_xe(tile);

This owner is important. It is used later to decide migration by hmm. We need to set the owner for hmm to identify different vram region.

Based on above, I think the tile parameter is better. For UMA, caller need to make sure call it once, any tile pointer should work. This does sound a little weird. But I don’t have a better idea.

Oak

Quoted 67 lines
Quoted 24 lines
drivers/gpu/drm/xe/xe_svm_devmem.c new file +22 -0
+ * @mr: memory region to remap
+ *
+ * This remap device memory to host physical address space and create
+ * struct page to back device memory
+ *
+ * Return: 0 on success standard error code otherwise
+ */
+int xe_svm_devm_add(struct xe_tile *tile, struct xe_mem_region *mr)
+{
+	struct device *dev = &to_pci_dev(tile->xe->drm.dev)->dev;
+	struct resource *res;
+	void *addr;
+	int ret;
+
+	res = devm_request_free_mem_region(dev, &iomem_resource,
+					   mr->usable_size);
+	if (IS_ERR(res)) {
+		ret = PTR_ERR(res);
+		return ret;
+	}
+
+	mr->pagemap.type = MEMORY_DEVICE_PRIVATE;

drivers/gpu/drm/xe/xe_svm_devmem.c

Quoted 10 lines
drivers/gpu/drm/xe/xe_svm_devmem.c new file +10 -0
+	mr->pagemap.range.start = res->start;
+	mr->pagemap.range.end = res->end;
+	mr->pagemap.nr_range = 1;
+	mr->pagemap.ops = &xe_devm_pagemap_ops;
+	mr->pagemap.owner = tile->xe->drm.dev;
+	addr = devm_memremap_pages(dev, &mr->pagemap);
+	if (IS_ERR(addr)) {
+		devm_release_mem_region(dev, res->start, resource_size(res));
+		ret = PTR_ERR(addr);
+		drm_err(&tile->xe->drm, "Failed to remap tile %d memory,

errno %d\n",

drivers/gpu/drm/xe/xe_svm_devmem.c

Quoted 6 lines
drivers/gpu/drm/xe/xe_svm_devmem.c new file +6 -0
+				tile->id, ret);
+		return ret;
+	}
+	mr->hpa_base = res->start;
+
+	drm_info(&tile->xe->drm, "Added tile %d memory [%llx-%llx] to devm,

remapped to %pr\n",

drivers/gpu/drm/xe/xe_svm_devmem.c line 69

Quoted 1 line
drivers/gpu/drm/xe/xe_svm_devmem.c new file +1 -0
+			tile->id, mr->io_start, mr->io_start + mr->usable_size,

res);

drivers/gpu/drm/xe/xe_svm_devmem.c

Quoted 9 lines
drivers/gpu/drm/xe/xe_svm_devmem.c new file +9 -0
+	return 0;
+}
+
+/**
+ * xe_svm_devm_remove: Unmap device memory and free resources
+ * @xe: xe device
+ * @mr: memory region to remove
+ */
+void xe_svm_devm_remove(struct xe_device *xe, struct xe_mem_region

*mr)

Quoted 13 lines
drivers/gpu/drm/xe/xe_svm_devmem.c new file +13 -0
+{
+	/*FIXME: below cause a kernel hange during moduel remove*/
+#if 0
+	struct device *dev = &to_pci_dev(xe->drm.dev)->dev;
+
+	if (mr->hpa_base) {
+		devm_memunmap_pages(dev, &mr->pagemap);
+		devm_release_mem_region(dev, mr->pagemap.range.start,
+			mr->pagemap.range.end - mr->pagemap.range.start +1);
+	}
+#endif
+}
+
author
#17 lore raw

RE: [PATCH 1/5] drm/xe/svm: Remap and provide memmap backing for GPU vram

Quoted 51 lines

-----Original Message-----
From: Welty, Brian <[email protected]>
Sent: Friday, March 15, 2024 2:05 PM
To: Zeng, Oak <[email protected]>; [email protected]
Cc: Hellstrom, Thomas <[email protected]>; Brost, Matthew <[email protected]>; [email protected]; Ghimiray, Himal Prasad <[email protected]>
Subject: Re: [PATCH 1/5] drm/xe/svm: Remap and provide memmap backing for GPU vram

On 3/14/2024 8:16 PM, Zeng, Oak wrote:
[...]

Quoted 26 lines
Quoted 26 lines
Quoted 1 line

Review

Quoted 1 line
(quoted diff) +0 -0

b/drivers/gpu/drm/xe/xe_mmio.c

Quoted 23 lines
Quoted 12 lines

index e3db3a178760..0d795394bc4c 100644

drivers/gpu/drm/xe/xe_mmio.c +1 -0
@@ -22,6 +22,7 @@
   #include "xe_module.h"
   #include "xe_sriov.h"
   #include "xe_tile.h"
+#include "xe_svm.h"
 
   #define XEHP_MTCFG_ADDR		XE_REG(0x101800)
   #define TILE_COUNT		REG_GENMASK(15, 8)

drivers/gpu/drm/xe/xe_mmio.c int xe_mmio_probe_vram(struct xe_device *xe) · line 290

Quoted 4 lines
drivers/gpu/drm/xe/xe_mmio.c +1 -0
@@ -286,6 +287,7 @@ int xe_mmio_probe_vram(struct xe_device *xe)
   		}
 
   		io_size -= min_t(u64, tile_size, io_size);
+		xe_svm_devm_add(tile, &tile->mem.vram);

I think slightly more appropriate call site for this might be xe_tile_init_noalloc(), as that function states it is preparing tile for VRAM allocations.
Also, I mention because we might like the flexiblity in future to call this once for xe_device.mem.vram, instead of calling for each tile, and easier to handle that in xe_tile.c instead of xe_mmio.c.

Quoted 5 lines
Quoted 1 line

Good point. Will follow.

Sorry, with my comment below, do you still want to call it in xe_tile_init_noalloc?

For UMA, we only need to call it once. If you do it in init-noalloc, you would call

it multiple times. Right?

Quoted 3 lines

Oak

Quoted 6 lines

Oh, I hoped you were still going to move it.
I prefer it in xe_tile.c instead of here. But feel free to ask maintainers about it.
I think the UMA would anyway add conditional to disable these calls wherever they are placed, in favor of calling against xe_device.mem.vram. But this is pretty minor and can revisit later too.

Let me move to xe_tile.c then... we can make whatever change when UMA is settled down.

Quoted 81 lines

I commented down below, to keep to one email.

Quoted 23 lines
Quoted 23 lines
Quoted 23 lines

Related comment below.

Quoted 19 lines
drivers/gpu/drm/xe/xe_mmio.c +6 -0
   	}
 
   	xe->mem.vram.actual_physical_size = total_size;
@@ -354,10 +356,16 @@ void xe_mmio_probe_tiles(struct xe_device *xe)
   static void mmio_fini(struct drm_device *drm, void *arg)
   {
   	struct xe_device *xe = arg;
+    struct xe_tile *tile;
+    u8 id;
 
   	pci_iounmap(to_pci_dev(xe->drm.dev), xe->mmio.regs);
   	if (xe->mem.vram.mapping)
   		iounmap(xe->mem.vram.mapping);
+
+	for_each_tile(tile, xe, id)
+		xe_svm_devm_remove(xe, &tile->mem.vram);
+
   }
 

[...]

Quoted 48 lines
Quoted 48 lines
Quoted 37 lines

b/drivers/gpu/drm/xe/xe_svm_devmem.c

Quoted 24 lines

new file mode 100644
index 000000000000..63b7a1961cc6

drivers/gpu/drm/xe/xe_svm_devmem.c new file +19 -0
@@ -0,0 +1,91 @@
+// SPDX-License-Identifier: MIT
+/*
+ * Copyright © 2023 Intel Corporation
+ */
+
+#include <linux/mm_types.h>
+#include <linux/sched/mm.h>
+
+#include "xe_device_types.h"
+#include "xe_trace.h"
+#include "xe_svm.h"
+
+
+static vm_fault_t xe_devm_migrate_to_ram(struct vm_fault *vmf)
+{
+	return 0;
+}
+
+static void xe_devm_page_free(struct page *page)

drivers/gpu/drm/xe/xe_svm_devmem.c line 29

Quoted 10 lines
drivers/gpu/drm/xe/xe_svm_devmem.c new file +10 -0
+{
+}
+
+static const struct dev_pagemap_ops xe_devm_pagemap_ops = {
+	.page_free = xe_devm_page_free,
+	.migrate_to_ram = xe_devm_migrate_to_ram,
+};
+
+/**
+ * xe_svm_devm_add: Remap and provide memmap backing for device

memory

Quoted 1 line

Do we really need 'svm' in function name?

Good point. I will remove svm.

Quoted 7 lines

drivers/gpu/drm/xe/xe_svm_devmem.c line 30

Quoted 2 lines
drivers/gpu/drm/xe/xe_svm_devmem.c new file +1 -0
+ * @tile: tile that the memory region blongs to

We might like flexibility in future to call this once for xe_device.mem.vram, instead of calling for each tile.
So can we remove the tile argument, and just pass the xe_device pointer and tile->id ?

Quoted 6 lines
Quoted 6 lines

This is interesting.

First of all, I programmed wrong below: mr->pagemap.owner = tile->xe-

Quoted 1 line

drm.dev;

This should be: mr->pagemap.owner = tile for NUMA vram system

Quoted 8 lines

Oh, okay. Glad we caught that.

Still, wouldn't using 'mr' pointer as the owner be sufficient and then is common for both cases?
Avoiding the strong association with 'tile' is nice if possible.

But I haven't studied rest of code and how pagemap.owner is used, so maybe you actually need the tile pointer for something.

mr is more unified. But when I looked further, for now we still need to set the owner to dev. So I take back the comment of setting owner to tile.

The reason is, we also need to set owner calling hmm_range_fault, see patch 5. At patch 5, we don't have the mr and tile, we only have vma and vm. And from vm we can only get dev, not tile and mr...

It looks like the xe_vm is designed per device, not per tile. I wouldn't change this perspective in this series. Making vm per tile is doable. But the main purpose of this series is to proof the system allocator concept on pvc and position it for new platform which might have uma architecture b/t tiles. So optimization such as migration b/t tiles is low priority.

So for now, lets still go back to dev for the owner.

Oak

Quoted 87 lines

Well, this also seems like something minor and could be revisited later.

-Brian

Quoted 4 lines
Quoted 4 lines

For UMA vram, this should be: mr->pagemap.owner = tile_to_xe(tile);

This owner is important. It is used later to decide migration by hmm. We need

to

Quoted 4 lines
Quoted 4 lines

set the owner for hmm to identify different vram region.

Based on above, I think the tile parameter is better. For UMA, caller need to make sure call it once, any tile pointer should work. This does sound a little

weird.

Quoted 47 lines
Quoted 47 lines

But I don’t have a better idea.

Oak

Quoted 44 lines
Quoted 24 lines
drivers/gpu/drm/xe/xe_svm_devmem.c new file +22 -0
+ * @mr: memory region to remap
+ *
+ * This remap device memory to host physical address space and create
+ * struct page to back device memory
+ *
+ * Return: 0 on success standard error code otherwise
+ */
+int xe_svm_devm_add(struct xe_tile *tile, struct xe_mem_region *mr)
+{
+	struct device *dev = &to_pci_dev(tile->xe->drm.dev)->dev;
+	struct resource *res;
+	void *addr;
+	int ret;
+
+	res = devm_request_free_mem_region(dev, &iomem_resource,
+					   mr->usable_size);
+	if (IS_ERR(res)) {
+		ret = PTR_ERR(res);
+		return ret;
+	}
+
+	mr->pagemap.type = MEMORY_DEVICE_PRIVATE;

drivers/gpu/drm/xe/xe_svm_devmem.c

Quoted 10 lines
drivers/gpu/drm/xe/xe_svm_devmem.c new file +10 -0
+	mr->pagemap.range.start = res->start;
+	mr->pagemap.range.end = res->end;
+	mr->pagemap.nr_range = 1;
+	mr->pagemap.ops = &xe_devm_pagemap_ops;
+	mr->pagemap.owner = tile->xe->drm.dev;
+	addr = devm_memremap_pages(dev, &mr->pagemap);
+	if (IS_ERR(addr)) {
+		devm_release_mem_region(dev, res->start, resource_size(res));
+		ret = PTR_ERR(addr);
+		drm_err(&tile->xe->drm, "Failed to remap tile %d memory,

errno %d\n",

drivers/gpu/drm/xe/xe_svm_devmem.c

Quoted 6 lines
drivers/gpu/drm/xe/xe_svm_devmem.c new file +6 -0
+				tile->id, ret);
+		return ret;
+	}
+	mr->hpa_base = res->start;
+
+	drm_info(&tile->xe->drm, "Added tile %d memory [%llx-%llx] to devm,

remapped to %pr\n",

drivers/gpu/drm/xe/xe_svm_devmem.c line 69

Quoted 1 line
drivers/gpu/drm/xe/xe_svm_devmem.c new file +1 -0
+			tile->id, mr->io_start, mr->io_start + mr->usable_size,

res);

Quoted 9 lines
Quoted 9 lines
Quoted 9 lines

drivers/gpu/drm/xe/xe_svm_devmem.c

Quoted 9 lines
drivers/gpu/drm/xe/xe_svm_devmem.c new file +9 -0
+	return 0;
+}
+
+/**
+ * xe_svm_devm_remove: Unmap device memory and free resources
+ * @xe: xe device
+ * @mr: memory region to remove
+ */
+void xe_svm_devm_remove(struct xe_device *xe, struct

xe_mem_region

Quoted 14 lines
Quoted 14 lines
Quoted 14 lines

*mr)

Quoted 13 lines
drivers/gpu/drm/xe/xe_svm_devmem.c new file +13 -0
+{
+	/*FIXME: below cause a kernel hange during moduel remove*/
+#if 0
+	struct device *dev = &to_pci_dev(xe->drm.dev)->dev;
+
+	if (mr->hpa_base) {
+		devm_memunmap_pages(dev, &mr->pagemap);
+		devm_release_mem_region(dev, mr->pagemap.range.start,
+			mr->pagemap.range.end - mr->pagemap.range.start +1);
+	}
+#endif
+}
+
author
#18 lore raw

PATCH 2/5 drm/xe: Helper to get memory region from tile

In reply to Oak Zeng
---
 drivers/gpu/drm/xe/xe_device_types.h | 5 +++++
 1 file changed, 5 insertions(+)
drivers/gpu/drm/xe/xe_device_types.h +5 -0
index f27c3bee8ce7..bbea40b57e84 100644
@@ -571,4 +571,9 @@ struct xe_file {
 	struct xe_drm_client *client;
 };
 
+static inline struct xe_tile *mem_region_to_tile(struct xe_mem_region *mr)
+{
+	return container_of(mr, struct xe_tile, mem.vram);
+}
+
 #endif
Signature · 1 line
2.26.3
maintainer
#19 lore raw

Re: [PATCH 2/5] drm/xe: Helper to get memory region from tile

Quoted 10 lines · Oak Zeng
---
 drivers/gpu/drm/xe/xe_device_types.h | 5 +++++
 1 file changed, 5 insertions(+)
drivers/gpu/drm/xe/xe_device_types.h +0 -0
index f27c3bee8ce7..bbea40b57e84 100644

drivers/gpu/drm/xe/xe_device_types.h struct xe_file { · line 577

Quoted 7 lines
drivers/gpu/drm/xe/xe_device_types.h +4 -0
@@ -571,4 +571,9 @@ struct xe_file {
 	struct xe_drm_client *client;
 };
 
+static inline struct xe_tile *mem_region_to_tile(struct xe_mem_region *mr)
+{
+	return container_of(mr, struct xe_tile, mem.vram);
+}

Helper shouldn't be in *_types.h files, only struct def should be.

Also s/mem_region_to_tile/xe_mem_region_to_tile/

Matt

Quoted 5 lines
drivers/gpu/drm/xe/xe_device_types.h +1 -0
+
 #endif
Signature · 1 line
2.26.3
maintainer
#20 lore raw

Re: [PATCH 2/5] drm/xe: Helper to get memory region from tile

In reply to Oak Zeng
Quoted 1 line · Oak Zeng

Missed this. Need a commit message. Also this looks to be unused in this series.

Matt

Quoted 21 lines
---
 drivers/gpu/drm/xe/xe_device_types.h | 5 +++++
 1 file changed, 5 insertions(+)
drivers/gpu/drm/xe/xe_device_types.h +5 -0
index f27c3bee8ce7..bbea40b57e84 100644
@@ -571,4 +571,9 @@ struct xe_file {
 	struct xe_drm_client *client;
 };
 
+static inline struct xe_tile *mem_region_to_tile(struct xe_mem_region *mr)
+{
+	return container_of(mr, struct xe_tile, mem.vram);
+}
+
 #endif
Signature · 1 line
2.26.3
author
#21 lore raw

RE: [PATCH 2/5] drm/xe: Helper to get memory region from tile

Quoted 12 lines

-----Original Message-----
From: Brost, Matthew <[email protected]>
Sent: Thursday, March 14, 2024 1:44 PM
To: Zeng, Oak <[email protected]>
Cc: [email protected]; Hellstrom, Thomas <[email protected]>; [email protected]; Welty, Brian <[email protected]>; Ghimiray, Himal Prasad <[email protected]>
Subject: Re: [PATCH 2/5] drm/xe: Helper to get memory region from tile

Quoted 4 lines
Quoted 1 line · Oak Zeng

Missed this. Need a commit message. Also this looks to be unused in this series.

Yes... I accidently picked wrong patch. Will drop this one and pick up the correct one.

Oak

Quoted 25 lines

Matt

Review

Quoted 5 lines
---
 drivers/gpu/drm/xe/xe_device_types.h | 5 +++++
 1 file changed, 5 insertions(+)
(quoted diff) +0 -0

b/drivers/gpu/drm/xe/xe_device_types.h

Quoted 16 lines

index f27c3bee8ce7..bbea40b57e84 100644

drivers/gpu/drm/xe/xe_device_types.h +5 -1
@@ -571,4 +571,9 @@ struct xe_file {
 	struct xe_drm_client *client;
 };
 
+static inline struct xe_tile *mem_region_to_tile(struct xe_mem_region *mr)
+{
+	return container_of(mr, struct xe_tile, mem.vram);
+}
+
 #endif
--

2.26.3

author
#22 lore raw

PATCH 3/5 drm/xe: Helper to get dpa from pfn

In reply to Oak Zeng

Since we now create struct page backing for each vram page, each vram page now also has a pfn, just like system memory. This allow us to calcuate device physical address from pfn.

---
 drivers/gpu/drm/xe/xe_device_types.h | 8 ++++++++
 1 file changed, 8 insertions(+)
drivers/gpu/drm/xe/xe_device_types.h +8 -0
index bbea40b57e84..bf349321f037 100644
@@ -576,4 +576,12 @@ static inline struct xe_tile *mem_region_to_tile(struct xe_mem_region *mr)
 	return container_of(mr, struct xe_tile, mem.vram);
 }
 
+static inline u64 vram_pfn_to_dpa(struct xe_mem_region *mr, u64 pfn)
+{
+	u64 dpa;
+	u64 offset = (pfn << PAGE_SHIFT) - mr->hpa_base;
+	dpa = mr->dpa_base + offset;
+	return dpa;
+}
+
 #endif
Signature · 1 line
2.26.3
maintainer
#23 lore raw

Re: [PATCH 3/5] drm/xe: Helper to get dpa from pfn

Quoted 14 lines · Oak Zeng

Since we now create struct page backing for each vram page, each vram page now also has a pfn, just like system memory. This allow us to calcuate device physical address from pfn.

---
 drivers/gpu/drm/xe/xe_device_types.h | 8 ++++++++
 1 file changed, 8 insertions(+)
drivers/gpu/drm/xe/xe_device_types.h +0 -0
index bbea40b57e84..bf349321f037 100644

drivers/gpu/drm/xe/xe_device_types.h static inline struct xe_tile *mem_region_to_tile(struct xe_mem_region *mr) · line 582

Quoted 7 lines
drivers/gpu/drm/xe/xe_device_types.h +4 -0
@@ -576,4 +576,12 @@ static inline struct xe_tile *mem_region_to_tile(struct xe_mem_region *mr)
 	return container_of(mr, struct xe_tile, mem.vram);
 }
 
+static inline u64 vram_pfn_to_dpa(struct xe_mem_region *mr, u64 pfn)
+{
+	u64 dpa;
+	u64 offset = (pfn << PAGE_SHIFT) - mr->hpa_base;

Can't this be negative?

e.g. if pfn == 0, offset == -mr->hpa_base.

Or is the assumption (pfn << PAGE_SHIFT) is always > mr->hpa_base?

If so can we an assert or something to ensure we using this function correctly.

drivers/gpu/drm/xe/xe_device_types.h static inline struct xe_tile *mem_region_to_tile(struct xe_mem_region *mr) · line 585

Quoted 3 lines
drivers/gpu/drm/xe/xe_device_types.h +3 -0
+	dpa = mr->dpa_base + offset;
+	return dpa;
+}

Same as previous patch, should be *.h not a *_types.h file.

Also as this is xe_mem_region not explictly vram. Maybe:

s/vram_pfn_to_dpa/xe_mem_region_pfn_to_dpa/

Matt

Quoted 5 lines
drivers/gpu/drm/xe/xe_device_types.h +1 -0
+
 #endif
Signature · 1 line
2.26.3
author
#24 lore raw

RE: [PATCH 3/5] drm/xe: Helper to get dpa from pfn

Quoted 39 lines

-----Original Message-----
From: Brost, Matthew <[email protected]>
Sent: Thursday, March 14, 2024 1:39 PM
To: Zeng, Oak <[email protected]>
Cc: [email protected]; Hellstrom, Thomas <[email protected]>; [email protected]; Welty, Brian <[email protected]>; Ghimiray, Himal Prasad <[email protected]>
Subject: Re: [PATCH 3/5] drm/xe: Helper to get dpa from pfn

Review

Quoted 10 lines · Oak Zeng

Since we now create struct page backing for each vram page, each vram page now also has a pfn, just like system memory. This allow us to calcuate device physical address from pfn.

---
 drivers/gpu/drm/xe/xe_device_types.h | 8 ++++++++
 1 file changed, 8 insertions(+)
(quoted diff) +0 -0

b/drivers/gpu/drm/xe/xe_device_types.h

drivers/gpu/drm/xe/xe_device_types.h static inline struct xe_tile *mem_region_to_tile(struct

Quoted 4 lines

index bbea40b57e84..bf349321f037 100644

drivers/gpu/drm/xe/xe_device_types.h +0 -0
@@ -576,4 +576,12 @@ static inline struct xe_tile *mem_region_to_tile(struct

xe_mem_region *mr)

drivers/gpu/drm/xe/xe_device_types.h static inline struct xe_tile *mem_region_to_tile(struct xe_mem_region *mr) · line 582

Quoted 7 lines
drivers/gpu/drm/xe/xe_device_types.h +4 -0
 	return container_of(mr, struct xe_tile, mem.vram);
 }
 
+static inline u64 vram_pfn_to_dpa(struct xe_mem_region *mr, u64 pfn)
+{
+	u64 dpa;
+	u64 offset = (pfn << PAGE_SHIFT) - mr->hpa_base;

Can't this be negative?

Quoted 5 lines

e.g. if pfn == 0, offset == -mr->hpa_base.

Or is the assumption (pfn << PAGE_SHIFT) is always > mr->hpa_base?

If so can we an assert or something to ensure we using this function correctly.

Yes we can assert it. The hpa_base is the host physical base address for this memory region, while pfn should point to a page inside this memory region.

I will add an assertion.

Quoted 6 lines

drivers/gpu/drm/xe/xe_device_types.h static inline struct xe_tile *mem_region_to_tile(struct xe_mem_region *mr) · line 585

Quoted 4 lines
drivers/gpu/drm/xe/xe_device_types.h +3 -0
+	dpa = mr->dpa_base + offset;
+	return dpa;
+}

Same as previous patch, should be *.h not a *_types.h file.

Yes will fix.

Quoted 4 lines

Also as this is xe_mem_region not explictly vram. Maybe:

s/vram_pfn_to_dpa/xe_mem_region_pfn_to_dpa/

Xe_mem_region can only represent vram, right? I mean it can't represent system memory. Copied the definition below:

/**
* struct xe_mem_region - memory region structure
* This is used to describe a memory region in xe
* device, such as HBM memory or CXL extension memory. */

Oak

Quoted 8 lines

Matt

Quoted 5 lines
drivers/gpu/drm/xe/xe_device_types.h +1 -1
+
 #endif
--

2.26.3

maintainer
#25 lore raw

Re: [PATCH 3/5] drm/xe: Helper to get dpa from pfn

Quoted 63 lines · Oak
Quoted 39 lines

-----Original Message-----
From: Brost, Matthew <[email protected]>
Sent: Thursday, March 14, 2024 1:39 PM
To: Zeng, Oak <[email protected]>
Cc: [email protected]; Hellstrom, Thomas <[email protected]>; [email protected]; Welty, Brian <[email protected]>; Ghimiray, Himal Prasad <[email protected]>
Subject: Re: [PATCH 3/5] drm/xe: Helper to get dpa from pfn

Review

Quoted 10 lines · Oak Zeng

Since we now create struct page backing for each vram page, each vram page now also has a pfn, just like system memory. This allow us to calcuate device physical address from pfn.

---
 drivers/gpu/drm/xe/xe_device_types.h | 8 ++++++++
 1 file changed, 8 insertions(+)
(quoted diff) +0 -0

b/drivers/gpu/drm/xe/xe_device_types.h

drivers/gpu/drm/xe/xe_device_types.h static inline struct xe_tile *mem_region_to_tile(struct

Quoted 4 lines

index bbea40b57e84..bf349321f037 100644

drivers/gpu/drm/xe/xe_device_types.h +0 -0
@@ -576,4 +576,12 @@ static inline struct xe_tile *mem_region_to_tile(struct

xe_mem_region *mr)

drivers/gpu/drm/xe/xe_device_types.h static inline struct xe_tile *mem_region_to_tile(struct xe_mem_region *mr) · line 582

Quoted 7 lines
drivers/gpu/drm/xe/xe_device_types.h +4 -0
 	return container_of(mr, struct xe_tile, mem.vram);
 }
 
+static inline u64 vram_pfn_to_dpa(struct xe_mem_region *mr, u64 pfn)
+{
+	u64 dpa;
+	u64 offset = (pfn << PAGE_SHIFT) - mr->hpa_base;

Can't this be negative?

Quoted 5 lines

e.g. if pfn == 0, offset == -mr->hpa_base.

Or is the assumption (pfn << PAGE_SHIFT) is always > mr->hpa_base?

If so can we an assert or something to ensure we using this function correctly.

Yes we can assert it. The hpa_base is the host physical base address for this memory region, while pfn should point to a page inside this memory region.

I will add an assertion.

Quoted 6 lines

drivers/gpu/drm/xe/xe_device_types.h static inline struct xe_tile *mem_region_to_tile(struct xe_mem_region *mr) · line 585

Quoted 4 lines
drivers/gpu/drm/xe/xe_device_types.h +3 -0
+	dpa = mr->dpa_base + offset;
+	return dpa;
+}

Same as previous patch, should be *.h not a *_types.h file.

Yes will fix.

Quoted 4 lines

Also as this is xe_mem_region not explictly vram. Maybe:

s/vram_pfn_to_dpa/xe_mem_region_pfn_to_dpa/

Quoted 8 lines

Xe_mem_region can only represent vram, right? I mean it can't represent system memory. Copied the definition below:

/**
* struct xe_mem_region - memory region structure
* This is used to describe a memory region in xe
* device, such as HBM memory or CXL extension memory. */

Ah yes but still as the first argument is xe_mem_region I think the function name should reflect that.

Matt

Quoted 10 lines

Oak

Quoted 8 lines

Matt

Quoted 5 lines
drivers/gpu/drm/xe/xe_device_types.h +1 -1
+
 #endif
--

2.26.3

author
#26 lore raw

RE: [PATCH 3/5] drm/xe: Helper to get dpa from pfn

Quoted 88 lines

-----Original Message-----
From: Brost, Matthew <[email protected]>
Sent: Friday, March 15, 2024 9:34 PM
To: Zeng, Oak <[email protected]>
Cc: [email protected]; Hellstrom, Thomas <[email protected]>; [email protected]; Welty, Brian <[email protected]>; Ghimiray, Himal Prasad <[email protected]>
Subject: Re: [PATCH 3/5] drm/xe: Helper to get dpa from pfn

Quoted 22 lines · Oak
Quoted 22 lines

-----Original Message-----
From: Brost, Matthew <[email protected]>
Sent: Thursday, March 14, 2024 1:39 PM
To: Zeng, Oak <[email protected]>
Cc: [email protected]; Hellstrom, Thomas <[email protected]>; [email protected]; Welty, Brian <[email protected]>; Ghimiray, Himal Prasad <[email protected]>
Subject: Re: [PATCH 3/5] drm/xe: Helper to get dpa from pfn

Quoted 9 lines · Oak Zeng

Since we now create struct page backing for each vram page, each vram page now also has a pfn, just like system memory. This allow us to calcuate device physical address from pfn.

---
 drivers/gpu/drm/xe/xe_device_types.h | 8 ++++++++
 1 file changed, 8 insertions(+)
Quoted 6 lines
Quoted 6 lines

Review

Quoted 1 line
(quoted diff) +0 -0

b/drivers/gpu/drm/xe/xe_device_types.h

drivers/gpu/drm/xe/xe_device_types.h static inline struct xe_tile

Quoted 4 lines

index bbea40b57e84..bf349321f037 100644

drivers/gpu/drm/xe/xe_device_types.h +0 -0
@@ -576,4 +576,12 @@ static inline struct xe_tile

*mem_region_to_tile(struct

Quoted 11 lines
Quoted 11 lines

xe_mem_region *mr)

drivers/gpu/drm/xe/xe_device_types.h static inline struct xe_tile *mem_region_to_tile(struct xe_mem_region *mr) · line 582

Quoted 7 lines
drivers/gpu/drm/xe/xe_device_types.h +4 -0
 	return container_of(mr, struct xe_tile, mem.vram);
 }
 
+static inline u64 vram_pfn_to_dpa(struct xe_mem_region *mr, u64 pfn)
+{
+	u64 dpa;
+	u64 offset = (pfn << PAGE_SHIFT) - mr->hpa_base;

Can't this be negative?

Quoted 7 lines
Quoted 5 lines

e.g. if pfn == 0, offset == -mr->hpa_base.

Or is the assumption (pfn << PAGE_SHIFT) is always > mr->hpa_base?

If so can we an assert or something to ensure we using this function correctly.

Yes we can assert it. The hpa_base is the host physical base address for this

memory region, while pfn should point to a page inside this memory region.

Quoted 11 lines

I will add an assertion.

Quoted 6 lines

drivers/gpu/drm/xe/xe_device_types.h static inline struct xe_tile *mem_region_to_tile(struct xe_mem_region *mr) · line 585

Quoted 4 lines
drivers/gpu/drm/xe/xe_device_types.h +3 -0
+	dpa = mr->dpa_base + offset;
+	return dpa;
+}

Same as previous patch, should be *.h not a *_types.h file.

Quoted 7 lines

Yes will fix.

Quoted 4 lines

Also as this is xe_mem_region not explictly vram. Maybe:

s/vram_pfn_to_dpa/xe_mem_region_pfn_to_dpa/

Xe_mem_region can only represent vram, right? I mean it can't represent

system memory. Copied the definition below:

Quoted 7 lines

/**
* struct xe_mem_region - memory region structure
* This is used to describe a memory region in xe
* device, such as HBM memory or CXL extension memory. */

Quoted 2 lines

Ah yes but still as the first argument is xe_mem_region I think the function name should reflect that.

Sure, will rename it then.

Oak

Quoted 13 lines

Matt

Quoted 10 lines

Oak

Quoted 8 lines

Matt

Quoted 5 lines
drivers/gpu/drm/xe/xe_device_types.h +1 -1
+
 #endif
--

2.26.3

#27 lore raw

Re: [PATCH 3/5] drm/xe: Helper to get dpa from pfn

In reply to Matthew Brost
Quoted 4 lines · Matthew Brost
Quoted 3 lines · Oak Zeng

Since we now create struct page backing for each vram page, each vram page now also has a pfn, just like system memory. This allow us to calcuate device physical address from pfn.

Please use imperative language according to the patch guidelines:
Something like "Add a" or "Introduce A"

Quoted 6 lines
Quoted 6 lines

---
 drivers/gpu/drm/xe/xe_device_types.h | 8 ++++++++

 1 file changed, 8 insertions(+)
Quoted 12 lines
Quoted 9 lines
drivers/gpu/drm/xe/xe_device_types.h +0 -0
index bbea40b57e84..bf349321f037 100644
@@ -576,4 +576,12 @@ static inline struct xe_tile
 *mem_region_to_tile(struct xe_mem_region *mr)
  	return container_of(mr, struct xe_tile, mem.vram);
  }
Quoted 3 lines


+static inline u64 vram_pfn_to_dpa(struct xe_mem_region *mr, u64 pfn)

static inline header functions also need kerneldoc unless strong reasons not to.

/Thomas

Quoted 30 lines

drivers/gpu/drm/xe/xe_device_types.h static inline struct xe_tile *mem_region_to_tile(struct xe_mem_region *mr) · line 582

Quoted 3 lines
drivers/gpu/drm/xe/xe_device_types.h +3 -0
+{
+	u64 dpa;
+	u64 offset = (pfn << PAGE_SHIFT) - mr->hpa_base;

Can't this be negative?

e.g. if pfn == 0, offset == -mr->hpa_base.

Or is the assumption (pfn << PAGE_SHIFT) is always > mr->hpa_base?

If so can we an assert or something to ensure we using this function correctly.

drivers/gpu/drm/xe/xe_device_types.h static inline struct xe_tile *mem_region_to_tile(struct xe_mem_region *mr) · line 585

Quoted 3 lines
drivers/gpu/drm/xe/xe_device_types.h +3 -0
+	dpa = mr->dpa_base + offset;
+	return dpa;
+}

Same as previous patch, should be *.h not a *_types.h file.

Also as this is xe_mem_region not explictly vram. Maybe:

s/vram_pfn_to_dpa/xe_mem_region_pfn_to_dpa/

Matt

Quoted 5 lines

+
 #endif

Signature · 1 line
2.26.3
author
#28 lore raw

RE: [PATCH 3/5] drm/xe: Helper to get dpa from pfn

Quoted 36 lines

-----Original Message-----
From: Hellstrom, Thomas <[email protected]>
Sent: Monday, March 18, 2024 8:10 AM
To: Brost, Matthew <[email protected]>; Zeng, Oak <[email protected]>
Cc: [email protected]; Welty, Brian <[email protected]>; [email protected]; Ghimiray, Himal Prasad <[email protected]>
Subject: Re: [PATCH 3/5] drm/xe: Helper to get dpa from pfn

Quoted 4 lines · Matthew Brost
Quoted 3 lines · Oak Zeng

Since we now create struct page backing for each vram page, each vram page now also has a pfn, just like system memory. This allow us to calcuate device physical address from pfn.

Please use imperative language according to the patch guidelines:
Something like "Add a" or "Introduce A"

Quoted 16 lines
Quoted 16 lines

---
 drivers/gpu/drm/xe/xe_device_types.h | 8 ++++++++

 1 file changed, 8 insertions(+)
drivers/gpu/drm/xe/xe_device_types.h +0 -0
index bbea40b57e84..bf349321f037 100644
@@ -576,4 +576,12 @@ static inline struct xe_tile
 *mem_region_to_tile(struct xe_mem_region *mr)
  	return container_of(mr, struct xe_tile, mem.vram);
  }
 
Quoted 5 lines
Quoted 2 lines
Quoted 2 lines
drivers/gpu/drm/xe/xe_device_types.h +1 -0
+static inline u64 vram_pfn_to_dpa(struct xe_mem_region *mr, u64

pfn)

static inline header functions also need kerneldoc unless strong reasons not to.

Good point. Will document it.

Oak

Quoted 35 lines

/Thomas

Quoted 30 lines

drivers/gpu/drm/xe/xe_device_types.h static inline struct xe_tile *mem_region_to_tile(struct xe_mem_region *mr) · line 582

Quoted 3 lines
drivers/gpu/drm/xe/xe_device_types.h +3 -0
+{
+	u64 dpa;
+	u64 offset = (pfn << PAGE_SHIFT) - mr->hpa_base;

Can't this be negative?

e.g. if pfn == 0, offset == -mr->hpa_base.

Or is the assumption (pfn << PAGE_SHIFT) is always > mr->hpa_base?

If so can we an assert or something to ensure we using this function correctly.

drivers/gpu/drm/xe/xe_device_types.h static inline struct xe_tile *mem_region_to_tile(struct xe_mem_region *mr) · line 585

Quoted 3 lines
drivers/gpu/drm/xe/xe_device_types.h +3 -0
+	dpa = mr->dpa_base + offset;
+	return dpa;
+}

Same as previous patch, should be *.h not a *_types.h file.

Also as this is xe_mem_region not explictly vram. Maybe:

s/vram_pfn_to_dpa/xe_mem_region_pfn_to_dpa/

Matt

Quoted 5 lines

+
 #endif
--
2.26.3

author
#29 lore raw

PATCH 4/5 drm/xe: Helper to populate a userptr or hmmptr

In reply to Oak Zeng

Add a helper function xe_hmm_populate_range to populate a a userptr or hmmptr range. This functions calls hmm_range_fault to read CPU page tables and populate all pfns/pages of this virtual address range.

If the populated page is system memory page, dma-mapping is performed to get a dma-address which can be used later for GPU to access pages.

If the populated page is device private page, we calculate the dpa ( device physical address) of the page.

The dma-address or dpa is then saved in userptr's sg table. This is prepare work to replace the get_user_pages_fast code in userptr code path. The helper function will also be used to populate hmmptr later.

---
 drivers/gpu/drm/xe/Makefile |   3 +-
 drivers/gpu/drm/xe/xe_hmm.c | 213 ++++++++++++++++++++++++++++++++++++
 drivers/gpu/drm/xe/xe_hmm.h |  12 ++
 3 files changed, 227 insertions(+), 1 deletion(-)
 create mode 100644 drivers/gpu/drm/xe/xe_hmm.c
 create mode 100644 drivers/gpu/drm/xe/xe_hmm.h
drivers/gpu/drm/xe/Makefile +2 -1
index 840467080e59..29dcbc938b01 100644
@@ -143,7 +143,8 @@ xe-y += xe_bb.o \
 	xe_wait_user_fence.o \
 	xe_wa.o \
 	xe_wopcm.o \
-	xe_svm_devmem.o
+	xe_svm_devmem.o \
+	xe_hmm.o
 
 # graphics hardware monitoring (HWMON) support
 xe-$(CONFIG_HWMON) += xe_hwmon.o
drivers/gpu/drm/xe/xe_hmm.c new file +213 -0

213 lines not on this page · raw message

drivers/gpu/drm/xe/xe_hmm.h new file +12 -0
new file mode 100644
index 000000000000..960f3f6d36ae
@@ -0,0 +1,12 @@
+// SPDX-License-Identifier: MIT
+/*
+ * Copyright © 2024 Intel Corporation
+ */
+
+#include <linux/types.h>
+#include <linux/hmm.h>
+#include "xe_vm_types.h"
+#include "xe_svm.h"
+
+int xe_hmm_populate_range(struct xe_vma *vma, struct hmm_range *hmm_range,
+						bool write);
Signature · 1 line
2.26.3
maintainer
#30 lore raw

Re: [PATCH 4/5] drm/xe: Helper to populate a userptr or hmmptr

Quoted 111 lines · Oak Zeng

Add a helper function xe_hmm_populate_range to populate a a userptr or hmmptr range. This functions calls hmm_range_fault to read CPU page tables and populate all pfns/pages of this virtual address range.

If the populated page is system memory page, dma-mapping is performed to get a dma-address which can be used later for GPU to access pages.

If the populated page is device private page, we calculate the dpa ( device physical address) of the page.

The dma-address or dpa is then saved in userptr's sg table. This is prepare work to replace the get_user_pages_fast code in userptr code path. The helper function will also be used to populate hmmptr later.

---
 drivers/gpu/drm/xe/Makefile |   3 +-
 drivers/gpu/drm/xe/xe_hmm.c | 213 ++++++++++++++++++++++++++++++++++++
 drivers/gpu/drm/xe/xe_hmm.h |  12 ++
 3 files changed, 227 insertions(+), 1 deletion(-)
 create mode 100644 drivers/gpu/drm/xe/xe_hmm.c
 create mode 100644 drivers/gpu/drm/xe/xe_hmm.h
drivers/gpu/drm/xe/Makefile +2 -1
index 840467080e59..29dcbc938b01 100644
@@ -143,7 +143,8 @@ xe-y += xe_bb.o \
 	xe_wait_user_fence.o \
 	xe_wa.o \
 	xe_wopcm.o \
-	xe_svm_devmem.o
+	xe_svm_devmem.o \
+	xe_hmm.o
 
 # graphics hardware monitoring (HWMON) support
 xe-$(CONFIG_HWMON) += xe_hwmon.o
drivers/gpu/drm/xe/xe_hmm.c new file +62 -0
new file mode 100644
index 000000000000..c45c2447d386
@@ -0,0 +1,213 @@
+// SPDX-License-Identifier: MIT
+/*
+ * Copyright © 2024 Intel Corporation
+ */
+
+#include <linux/mmu_notifier.h>
+#include <linux/dma-mapping.h>
+#include <linux/memremap.h>
+#include <linux/swap.h>
+#include <linux/mm.h>
+#include "xe_hmm.h"
+#include "xe_vm.h"
+
+/**
+ * mark_range_accessed() - mark a range is accessed, so core mm
+ * have such information for memory eviction or write back to
+ * hard disk
+ *
+ * @range: the range to mark
+ * @write: if write to this range, we mark pages in this range
+ * as dirty
+ */
+static void mark_range_accessed(struct hmm_range *range, bool write)
+{
+	struct page *page;
+	u64 i, npages;
+
+	npages = ((range->end - 1) >> PAGE_SHIFT) - (range->start >> PAGE_SHIFT) + 1;
+	for (i = 0; i < npages; i++) {
+		page = hmm_pfn_to_page(range->hmm_pfns[i]);
+		if (write) {
+			lock_page(page);
+			set_page_dirty(page);
+			unlock_page(page);
+		}
+		mark_page_accessed(page);
+	}
+}
+
+/**
+ * build_sg() - build a scatter gather table for all the physical pages/pfn
+ * in a hmm_range. dma-address is save in sg table and will be used to program
+ * GPU page table later.
+ *
+ * @xe: the xe device who will access the dma-address in sg table
+ * @range: the hmm range that we build the sg table from. range->hmm_pfns[]
+ * has the pfn numbers of pages that back up this hmm address range.
+ * @st: pointer to the sg table.
+ * @write: whether we write to this range. This decides dma map direction
+ * for system pages. If write we map it bi-diretional; otherwise
+ * DMA_TO_DEVICE
+ *
+ * All the contiguous pfns will be collapsed into one entry in
+ * the scatter gather table. This is for the convenience of
+ * later on operations to bind address range to GPU page table.
+ *
+ * The dma_address in the sg table will later be used by GPU to
+ * access memory. So if the memory is system memory, we need to
+ * do a dma-mapping so it can be accessed by GPU/DMA. If the memory
+ * is GPU local memory (of the GPU who is going to access memory),
+ * we need gpu dpa (device physical address), and there is no need
+ * of dma-mapping.

drivers/gpu/drm/xe/xe_hmm.c line 72

Quoted 10 lines
drivers/gpu/drm/xe/xe_hmm.c new file +10 -0
+ *
+ * FIXME: dma-mapping for peer gpu device to access remote gpu's
+ * memory. Add this when you support p2p
+ *
+ * This function allocates the storage of the sg table. It is
+ * caller's responsibility to free it calling sg_free_table.
+ *
+ * Returns 0 if successful; -ENOMEM if fails to allocate memory
+ */
+static int build_sg(struct xe_device *xe, struct hmm_range *range,

xe is unused.

Quoted 11 lines
drivers/gpu/drm/xe/xe_hmm.c new file +11 -0
+			     struct sg_table *st, bool write)
+{
+	struct device *dev = xe->drm.dev;
+	struct scatterlist *sg;
+	u64 i, npages;
+
+	sg = NULL;
+	st->nents = 0;
+	npages = ((range->end - 1) >> PAGE_SHIFT) - (range->start >> PAGE_SHIFT) + 1;
+
+	if (unlikely(sg_alloc_table(st, npages, GFP_KERNEL)))

drivers/gpu/drm/xe/xe_hmm.c line 93

Quoted 10 lines
drivers/gpu/drm/xe/xe_hmm.c new file +10 -0
+		return -ENOMEM;
+
+	for (i = 0; i < npages; i++) {
+		struct page *page;
+		unsigned long addr;
+		struct xe_mem_region *mr;
+
+		page = hmm_pfn_to_page(range->hmm_pfns[i]);
+		if (is_device_private_page(page)) {
+			mr = page_to_mem_region(page);

Not seeing where page_to_mem_region is defined.

Quoted 13 lines
drivers/gpu/drm/xe/xe_hmm.c new file +13 -0
+			addr = vram_pfn_to_dpa(mr, range->hmm_pfns[i]);
+		} else {
+			addr = dma_map_page(dev, page, 0, PAGE_SIZE,
+					write ? DMA_BIDIRECTIONAL : DMA_TO_DEVICE);
+		}
+
+		if (sg && (addr == (sg_dma_address(sg) + sg->length))) {
+			sg->length += PAGE_SIZE;
+			sg_dma_len(sg) += PAGE_SIZE;
+			continue;
+		}
+
+		sg =  sg ? sg_next(sg) : st->sgl;

drivers/gpu/drm/xe/xe_hmm.c line 116

Quoted 10 lines
drivers/gpu/drm/xe/xe_hmm.c new file +10 -0
+		sg_dma_address(sg) = addr;
+		sg_dma_len(sg) = PAGE_SIZE;
+		sg->length = PAGE_SIZE;
+		st->nents++;
+	}
+
+	sg_mark_end(sg);
+	return 0;
+}
+

Hmm, this looks way to open coded to me.

Can't we do something like this:

struct page **pages = convert from range->hmm_pfns sg_alloc_table_from_pages_segment
if (is_device_private_page())
populatue sg table via vram_pfn_to_dpa
else
dma_map_sgtable
free(pages)

This assume we are not mixing is_device_private_page & system memory pages in a single struct hmm_range.

Quoted 13 lines
drivers/gpu/drm/xe/xe_hmm.c new file +13 -0
+/**
+ * xe_hmm_populate_range() - Populate physical pages of a virtual
+ * address range
+ *
+ * @vma: vma has information of the range to populate. only vma
+ * of userptr and hmmptr type can be populated.
+ * @hmm_range: pointer to hmm_range struct. hmm_rang->hmm_pfns
+ * will hold the populated pfns.
+ * @write: populate pages with write permission
+ *
+ * This function populate the physical pages of a virtual
+ * address range. The populated physical pages is saved in
+ * userptr's sg table. It is similar to get_user_pages but call

drivers/gpu/drm/xe/xe_hmm.c line 139

Quoted 10 lines
drivers/gpu/drm/xe/xe_hmm.c new file +10 -0
+ * hmm_range_fault.
+ *
+ * This function also read mmu notifier sequence # (
+ * mmu_interval_read_begin), for the purpose of later
+ * comparison (through mmu_interval_read_retry).
+ *
+ * This must be called with mmap read or write lock held.
+ *
+ * This function allocates the storage of the userptr sg table.
+ * It is caller's responsibility to free it calling sg_free_table.

I'd add a helper to free the sg_free_table & unmap the dma pages if needed.

drivers/gpu/drm/xe/xe_hmm.c line 145

Quoted 6 lines
drivers/gpu/drm/xe/xe_hmm.c new file +6 -0
+ *
+ * returns: 0 for succuss; negative error no on failure
+ */
+int xe_hmm_populate_range(struct xe_vma *vma, struct hmm_range *hmm_range,
+						bool write)
+{

The layering is all wrong here, we shouldn't be touching struct xe_vma directly in hmm layer.

Pass in a populated hmm_range and sgt. Or alternatively pass in arguments and then populate a hmm_range locally.

drivers/gpu/drm/xe/xe_hmm.c line 152

Quoted 7 lines
drivers/gpu/drm/xe/xe_hmm.c new file +7 -0
+	unsigned long timeout =
+		jiffies + msecs_to_jiffies(HMM_RANGE_DEFAULT_TIMEOUT);
+	unsigned long *pfns, flags = HMM_PFN_REQ_FAULT;
+	struct xe_userptr_vma *userptr_vma;
+	struct xe_userptr *userptr;
+	u64 start = vma->gpuva.va.addr;
+	u64 end = start + vma->gpuva.va.range;

We have helper - xe_vma_start and xe_vma_end but I think either of these are correct in this case.

xe_vma_start is the address which this bound to the GPU, we want the userptr address.

So I think it would be:

start = xe_vma_userptr()
end = xe_vma_userptr() + xe_vma_size()

drivers/gpu/drm/xe/xe_hmm.c line 161

Quoted 9 lines
drivers/gpu/drm/xe/xe_hmm.c new file +9 -0
+	struct xe_vm *vm = xe_vma_vm(vma);
+	u64 npages;
+	int ret;
+
+	userptr_vma = to_userptr_vma(vma);
+	userptr = &userptr_vma->userptr;
+	mmap_assert_locked(userptr->notifier.mm);
+
+	npages = ((end - 1) >> PAGE_SHIFT) - (start >> PAGE_SHIFT) + 1;

This math is done above, if you need this math in next rev add a helper.

drivers/gpu/drm/xe/xe_hmm.c line 169

Quoted 8 lines
drivers/gpu/drm/xe/xe_hmm.c new file +8 -0
+	pfns = kvmalloc_array(npages, sizeof(*pfns), GFP_KERNEL);
+	if (unlikely(!pfns))
+		return -ENOMEM;
+
+	if (write)
+		flags |= HMM_PFN_REQ_WRITE;
+
+	memset64((u64 *)pfns, (u64)flags, npages);

Why is this needed, can't we just set hmm_range->default_flags?

drivers/gpu/drm/xe/xe_hmm.c line 171

Quoted 2 lines
drivers/gpu/drm/xe/xe_hmm.c new file +2 -0
+	hmm_range->hmm_pfns = pfns;
+	hmm_range->notifier_seq = mmu_interval_read_begin(&userptr->notifier);

We need a userptr->notifier == userptr->notifier_seq check that just returns, right?

drivers/gpu/drm/xe/xe_hmm.c line 175

Quoted 4 lines
drivers/gpu/drm/xe/xe_hmm.c new file +4 -0
+	hmm_range->notifier = &userptr->notifier;
+	hmm_range->start = start;
+	hmm_range->end = end;
+	hmm_range->pfn_flags_mask = HMM_PFN_REQ_FAULT | HMM_PFN_REQ_WRITE;

Is this needed? AMD and Nouveau do not set this argument.

drivers/gpu/drm/xe/xe_hmm.c line 188

Quoted 13 lines
drivers/gpu/drm/xe/xe_hmm.c new file +13 -0
+	/**
+	 * FIXME:
+	 * Set the the dev_private_owner can prevent hmm_range_fault to fault
+	 * in the device private pages owned by caller. See function
+	 * hmm_vma_handle_pte. In multiple GPU case, this should be set to the
+	 * device owner of the best migration destination. e.g., device0/vm0
+	 * has a page fault, but we have determined the best placement of
+	 * the fault address should be on device1, we should set below to
+	 * device1 instead of device0.
+	 */
+	hmm_range->dev_private_owner = vm->xe->drm.dev;
+
+	while (true) {

mmap_read_lock(mm);

drivers/gpu/drm/xe/xe_hmm.c line 189

Quoted 1 line
drivers/gpu/drm/xe/xe_hmm.c new file +1 -0
+		ret = hmm_range_fault(hmm_range);

mmap_read_unlock(mm);

Quoted 7 lines
drivers/gpu/drm/xe/xe_hmm.c new file +7 -0
+		if (time_after(jiffies, timeout))
+			break;
+
+		if (ret == -EBUSY)
+			continue;
+		break;
+	}

drivers/gpu/drm/xe/xe_hmm.c line 206

Quoted 10 lines
drivers/gpu/drm/xe/xe_hmm.c new file +10 -0
+
+	if (ret)
+		goto free_pfns;
+
+	ret = build_sg(vm->xe, hmm_range, &userptr->sgt, write);
+	if (ret)
+		goto free_pfns;
+
+	mark_range_accessed(hmm_range, write);
+	userptr->sg = &userptr->sgt;

Again this should be set in caller after this function return.

drivers/gpu/drm/xe/xe_hmm.c line 207

Quoted 1 line
drivers/gpu/drm/xe/xe_hmm.c new file +1 -0
+	userptr->notifier_seq = hmm_range->notifier_seq;

This is could be a pass by reference I guess and set here.

Quoted 12 lines
drivers/gpu/drm/xe/xe_hmm.c new file +6 -0
+
+free_pfns:
+	kvfree(pfns);
+	return ret;
+}
+
drivers/gpu/drm/xe/xe_hmm.h new file +0 -0
new file mode 100644
index 000000000000..960f3f6d36ae

drivers/gpu/drm/xe/xe_hmm.h line 9

Quoted 9 lines
drivers/gpu/drm/xe/xe_hmm.h new file +9 -0
@@ -0,0 +1,12 @@
+// SPDX-License-Identifier: MIT
+/*
+ * Copyright © 2024 Intel Corporation
+ */
+
+#include <linux/types.h>
+#include <linux/hmm.h>
+#include "xe_vm_types.h"
+#include "xe_svm.h"

As per the previous patches no need to xe_*.h files, just forward declare any arguments.

Matt

Quoted 6 lines
drivers/gpu/drm/xe/xe_hmm.h new file +3 -0
+
+int xe_hmm_populate_range(struct xe_vma *vma, struct hmm_range *hmm_range,
+						bool write);
Signature · 1 line
2.26.3
author
#31 lore raw

RE: [PATCH 4/5] drm/xe: Helper to populate a userptr or hmmptr

Quoted 141 lines

-----Original Message-----
From: Brost, Matthew <[email protected]>
Sent: Thursday, March 14, 2024 4:25 PM
To: Zeng, Oak <[email protected]>
Cc: [email protected]; Hellstrom, Thomas <[email protected]>; [email protected]; Welty, Brian <[email protected]>; Ghimiray, Himal Prasad <[email protected]>
Subject: Re: [PATCH 4/5] drm/xe: Helper to populate a userptr or hmmptr

Quoted 11 lines · Oak Zeng

Add a helper function xe_hmm_populate_range to populate a a userptr or hmmptr range. This functions calls hmm_range_fault to read CPU page tables and populate all pfns/pages of this virtual address range.

If the populated page is system memory page, dma-mapping is performed to get a dma-address which can be used later for GPU to access pages.

If the populated page is device private page, we calculate the dpa ( device physical address) of the page.

Quoted 6 lines

The dma-address or dpa is then saved in userptr's sg table. This is prepare work to replace the get_user_pages_fast code in userptr code path. The helper function will also be used to populate hmmptr later.

Co-developed-by Niranjana Vishwanathapura

<[email protected]>

Quoted 1 line
Signed-off-by Niranjana Vishwanathapura

<[email protected]>

Quoted 6 lines
Cc Matthew Brost <[email protected]>
Cc Thomas Hellström <[email protected]>
Cc Brian Welty <[email protected]>
---
 drivers/gpu/drm/xe/Makefile |   3 +-
 drivers/gpu/drm/xe/xe_hmm.c | 213

++++++++++++++++++++++++++++++++++++

Quoted 43 lines
 drivers/gpu/drm/xe/xe_hmm.h |  12 ++
 3 files changed, 227 insertions(+), 1 deletion(-)
 create mode 100644 drivers/gpu/drm/xe/xe_hmm.c
 create mode 100644 drivers/gpu/drm/xe/xe_hmm.h
drivers/gpu/drm/xe/Makefile +2 -1
index 840467080e59..29dcbc938b01 100644
@@ -143,7 +143,8 @@ xe-y += xe_bb.o \
 	xe_wait_user_fence.o \
 	xe_wa.o \
 	xe_wopcm.o \
-	xe_svm_devmem.o
+	xe_svm_devmem.o \
+	xe_hmm.o
 
 # graphics hardware monitoring (HWMON) support
 xe-$(CONFIG_HWMON) += xe_hwmon.o
drivers/gpu/drm/xe/xe_hmm.c new file +18 -0
new file mode 100644
index 000000000000..c45c2447d386
@@ -0,0 +1,213 @@
+// SPDX-License-Identifier: MIT
+/*
+ * Copyright © 2024 Intel Corporation
+ */
+
+#include <linux/mmu_notifier.h>
+#include <linux/dma-mapping.h>
+#include <linux/memremap.h>
+#include <linux/swap.h>
+#include <linux/mm.h>
+#include "xe_hmm.h"
+#include "xe_vm.h"
+
+/**
+ * mark_range_accessed() - mark a range is accessed, so core mm
+ * have such information for memory eviction or write back to
+ * hard disk
+ *

drivers/gpu/drm/xe/xe_hmm.c line 28

Quoted 10 lines
drivers/gpu/drm/xe/xe_hmm.c new file +10 -0
+ * @range: the range to mark
+ * @write: if write to this range, we mark pages in this range
+ * as dirty
+ */
+static void mark_range_accessed(struct hmm_range *range, bool write)
+{
+	struct page *page;
+	u64 i, npages;
+
+	npages = ((range->end - 1) >> PAGE_SHIFT) - (range->start >>

PAGE_SHIFT) + 1;

Quoted 4 lines
drivers/gpu/drm/xe/xe_hmm.c new file +4 -0
+	for (i = 0; i < npages; i++) {
+		page = hmm_pfn_to_page(range->hmm_pfns[i]);
+		if (write) {
+			lock_page(page);

drivers/gpu/drm/xe/xe_hmm.c

Quoted 10 lines
drivers/gpu/drm/xe/xe_hmm.c new file +10 -0
+			set_page_dirty(page);
+			unlock_page(page);
+		}
+		mark_page_accessed(page);
+	}
+}
+
+/**
+ * build_sg() - build a scatter gather table for all the physical pages/pfn
+ * in a hmm_range. dma-address is save in sg table and will be used to

program

Quoted 23 lines
drivers/gpu/drm/xe/xe_hmm.c new file +4 -0
+ * GPU page table later.
+ *
+ * @xe: the xe device who will access the dma-address in sg table
+ * @range: the hmm range that we build the sg table from. range-

hmm_pfns[]
+ * has the pfn numbers of pages that back up this hmm address range.
+ * @st: pointer to the sg table.
+ * @write: whether we write to this range. This decides dma map direction
+ * for system pages. If write we map it bi-diretional; otherwise
+ * DMA_TO_DEVICE
+ *
+ * All the contiguous pfns will be collapsed into one entry in
+ * the scatter gather table. This is for the convenience of
+ * later on operations to bind address range to GPU page table.
+ *
+ * The dma_address in the sg table will later be used by GPU to
+ * access memory. So if the memory is system memory, we need to
+ * do a dma-mapping so it can be accessed by GPU/DMA. If the memory
+ * is GPU local memory (of the GPU who is going to access memory),
+ * we need gpu dpa (device physical address), and there is no need
+ * of dma-mapping.
+ *
+ * FIXME: dma-mapping for peer gpu device to access remote gpu's

Quoted 8 lines

+ * memory. Add this when you support p2p
+ *
+ * This function allocates the storage of the sg table. It is
+ * caller's responsibility to free it calling sg_free_table.
+ *
+ * Returns 0 if successful; -ENOMEM if fails to allocate memory
+ */
+static int build_sg(struct xe_device *xe, struct hmm_range *range,

Quoted 1 line

xe is unused.

It is used in below line

Quoted 4 lines

drivers/gpu/drm/xe/xe_hmm.c line 75

Quoted 4 lines
drivers/gpu/drm/xe/xe_hmm.c new file +3 -0
+			     struct sg_table *st, bool write)
+{
+	struct device *dev = xe->drm.dev;

Used here

Quoted 7 lines

drivers/gpu/drm/xe/xe_hmm.c

Quoted 6 lines
drivers/gpu/drm/xe/xe_hmm.c new file +6 -0
+	struct scatterlist *sg;
+	u64 i, npages;
+
+	sg = NULL;
+	st->nents = 0;
+	npages = ((range->end - 1) >> PAGE_SHIFT) - (range->start >>

PAGE_SHIFT) + 1;

Quoted 14 lines

drivers/gpu/drm/xe/xe_hmm.c line 93

Quoted 12 lines
drivers/gpu/drm/xe/xe_hmm.c new file +12 -0
+
+	if (unlikely(sg_alloc_table(st, npages, GFP_KERNEL)))
+		return -ENOMEM;
+
+	for (i = 0; i < npages; i++) {
+		struct page *page;
+		unsigned long addr;
+		struct xe_mem_region *mr;
+
+		page = hmm_pfn_to_page(range->hmm_pfns[i]);
+		if (is_device_private_page(page)) {
+			mr = page_to_mem_region(page);

Not seeing where page_to_mem_region is defined.

Yah,,,, I forgot to pick up this patch. Will pick up...

Quoted 38 lines

drivers/gpu/drm/xe/xe_hmm.c

Quoted 5 lines
drivers/gpu/drm/xe/xe_hmm.c new file +4 -0
+			addr = vram_pfn_to_dpa(mr, range->hmm_pfns[i]);
+		} else {
+			addr = dma_map_page(dev, page, 0, PAGE_SIZE,
+					write ? DMA_BIDIRECTIONAL :

DMA_TO_DEVICE);

Quoted 9 lines
drivers/gpu/drm/xe/xe_hmm.c new file +9 -0
+		}
+
+		if (sg && (addr == (sg_dma_address(sg) + sg->length))) {
+			sg->length += PAGE_SIZE;
+			sg_dma_len(sg) += PAGE_SIZE;
+			continue;
+		}
+
+		sg =  sg ? sg_next(sg) : st->sgl;

drivers/gpu/drm/xe/xe_hmm.c line 116

Quoted 10 lines
drivers/gpu/drm/xe/xe_hmm.c new file +10 -0
+		sg_dma_address(sg) = addr;
+		sg_dma_len(sg) = PAGE_SIZE;
+		sg->length = PAGE_SIZE;
+		st->nents++;
+	}
+
+	sg_mark_end(sg);
+	return 0;
+}
+

Hmm, this looks way to open coded to me.

Can't we do something like this:

struct page **pages = convert from range->hmm_pfns sg_alloc_table_from_pages_segment
if (is_device_private_page())
populatue sg table via vram_pfn_to_dpa
else
dma_map_sgtable
free(pages)

Quoted 2 lines

This assume we are not mixing is_device_private_page & system memory pages in a single struct hmm_range.

that is exactly I pictured. We actually can mixing vram and system memory... the reason is, migration of a hmm range from system memory to vram can fail for whatever reason. And it can end up a range is partially migrated.... And migration is best effort in such case. We just map the system memory in that range to gpu for such case. This will come up in the coming system allocator patches...

This case was found during i915 test...

I also checked amd's code. They assume the same, just take a look of function svm_range_dma_map_dev.

agree that code is not nice. But I don't have a better way assuming above

Quoted 26 lines
Quoted 17 lines

+/**
+ * xe_hmm_populate_range() - Populate physical pages of a virtual
+ * address range
+ *
+ * @vma: vma has information of the range to populate. only vma
+ * of userptr and hmmptr type can be populated.
+ * @hmm_range: pointer to hmm_range struct. hmm_rang->hmm_pfns
+ * will hold the populated pfns.
+ * @write: populate pages with write permission
+ *
+ * This function populate the physical pages of a virtual
+ * address range. The populated physical pages is saved in
+ * userptr's sg table. It is similar to get_user_pages but call
+ * hmm_range_fault.
+ *

Quoted 8 lines

+ * This function also read mmu notifier sequence # (
+ * mmu_interval_read_begin), for the purpose of later
+ * comparison (through mmu_interval_read_retry).
+ *
+ * This must be called with mmap read or write lock held.
+ *
+ * This function allocates the storage of the userptr sg table.
+ * It is caller's responsibility to free it calling sg_free_table.

Quoted 2 lines

I'd add a helper to free the sg_free_table & unmap the dma pages if needed.

Ok, due to the reason I explained above, there will be a little complication. I will do it in a separate patch.

Quoted 11 lines
Quoted 5 lines

+ *
+ * returns: 0 for succuss; negative error no on failure
+ */
+int xe_hmm_populate_range(struct xe_vma *vma, struct hmm_range

*hmm_range,

drivers/gpu/drm/xe/xe_hmm.c line 145

Quoted 2 lines
drivers/gpu/drm/xe/xe_hmm.c new file +2 -0
+						bool write)
+{

The layering is all wrong here, we shouldn't be touching struct xe_vma directly in hmm layer.

I have to admit we don't have a clear layering here. This function is supposed to be a shared function which will be used by both hmmptr and userptr.

Maybe you got an impression from my POC series that we don't have xe_vma in system allocator codes. That was true. But after the design discussion, we have decided to unify userptr code and hmmptr codes, so we will have xe_vma also in system allocator code. Basically xe_vma will replace the xe_svm_range concept in my patch series.

Of course, per Thomas we can further optimize by splitting xe_vma into mutable and unmutable... but that should be step 2.

Quoted 3 lines

Pass in a populated hmm_range and sgt. Or alternatively pass in arguments and then populate a hmm_range locally.

Xe_vma is the input parameter here.

I will remove Hmm_range from function parameter and make it a local. I figured I don't need this as an output anymore. All we need is already in sg table.

Quoted 12 lines

drivers/gpu/drm/xe/xe_hmm.c line 152

Quoted 8 lines
drivers/gpu/drm/xe/xe_hmm.c new file +7 -0
+	unsigned long timeout =
+		jiffies + msecs_to_jiffies(HMM_RANGE_DEFAULT_TIMEOUT);
+	unsigned long *pfns, flags = HMM_PFN_REQ_FAULT;
+	struct xe_userptr_vma *userptr_vma;
+	struct xe_userptr *userptr;
+	u64 start = vma->gpuva.va.addr;
+	u64 end = start + vma->gpuva.va.range;

We have helper - xe_vma_start and xe_vma_end but I think either of these are correct in this case.

Quoted 7 lines

xe_vma_start is the address which this bound to the GPU, we want the userptr address.

So I think it would be:

start = xe_vma_userptr()
end = xe_vma_userptr() + xe_vma_size()

You are correct. Will fix. I mixed this because, in system allocator, cpu address always == gpu address

Quoted 13 lines

drivers/gpu/drm/xe/xe_hmm.c line 161

Quoted 11 lines
drivers/gpu/drm/xe/xe_hmm.c new file +9 -0
+	struct xe_vm *vm = xe_vma_vm(vma);
+	u64 npages;
+	int ret;
+
+	userptr_vma = to_userptr_vma(vma);
+	userptr = &userptr_vma->userptr;
+	mmap_assert_locked(userptr->notifier.mm);
+
+	npages = ((end - 1) >> PAGE_SHIFT) - (start >> PAGE_SHIFT) + 1;

This math is done above, if you need this math in next rev add a helper.

Will do.

Quoted 11 lines

drivers/gpu/drm/xe/xe_hmm.c line 169

Quoted 9 lines
drivers/gpu/drm/xe/xe_hmm.c new file +8 -0
+	pfns = kvmalloc_array(npages, sizeof(*pfns), GFP_KERNEL);
+	if (unlikely(!pfns))
+		return -ENOMEM;
+
+	if (write)
+		flags |= HMM_PFN_REQ_WRITE;
+
+	memset64((u64 *)pfns, (u64)flags, npages);

Why is this needed, can't we just set hmm_range->default_flags?

In this case, set default_flags also work.

This is some codes I inherited from Niranjana. Per my test before it works.

Basically there are two way to control the flags:
Default is the coarse grain way applying to all pfns
The way I am using here is a per pfn fine grained flag setting. It also works.

Quoted 7 lines
Quoted 4 lines
drivers/gpu/drm/xe/xe_hmm.c new file +2 -0
+	hmm_range->hmm_pfns = pfns;
+	hmm_range->notifier_seq = mmu_interval_read_begin(&userptr-

notifier);

We need a userptr->notifier == userptr->notifier_seq check that just returns, right?

Yes this sequence number is used to decide whether we need to retry. See function xe_pt_userptr_pre_commit

Quoted 8 lines

drivers/gpu/drm/xe/xe_hmm.c

Quoted 5 lines
drivers/gpu/drm/xe/xe_hmm.c new file +4 -0
+	hmm_range->notifier = &userptr->notifier;
+	hmm_range->start = start;
+	hmm_range->end = end;
+	hmm_range->pfn_flags_mask = HMM_PFN_REQ_FAULT |

HMM_PFN_REQ_WRITE;

Is this needed? AMD and Nouveau do not set this argument.

As explained above. Amd and nouveau only use coarse grain setting

Quoted 17 lines
Quoted 4 lines
drivers/gpu/drm/xe/xe_hmm.c new file +3 -0
+	/**
+	 * FIXME:
+	 * Set the the dev_private_owner can prevent hmm_range_fault to fault

drivers/gpu/drm/xe/xe_hmm.c line 188

Quoted 10 lines
drivers/gpu/drm/xe/xe_hmm.c new file +10 -0
+	 * in the device private pages owned by caller. See function
+	 * hmm_vma_handle_pte. In multiple GPU case, this should be set to the
+	 * device owner of the best migration destination. e.g., device0/vm0
+	 * has a page fault, but we have determined the best placement of
+	 * the fault address should be on device1, we should set below to
+	 * device1 instead of device0.
+	 */
+	hmm_range->dev_private_owner = vm->xe->drm.dev;
+
+	while (true) {

mmap_read_lock(mm);

Quoted 3 lines

drivers/gpu/drm/xe/xe_hmm.c line 189

Quoted 1 line
drivers/gpu/drm/xe/xe_hmm.c new file +1 -0
+		ret = hmm_range_fault(hmm_range);

mmap_read_unlock(mm);

This need to be called in caller. The reason is, in the system allocator code path, mmap lock is already hold before calling into this helper.

I will check patch 5 for this purpose.

Quoted 12 lines
Quoted 12 lines
drivers/gpu/drm/xe/xe_hmm.c new file +11 -0
+		if (time_after(jiffies, timeout))
+			break;
+
+		if (ret == -EBUSY)
+			continue;
+		break;
+	}
+
+	if (ret)
+		goto free_pfns;
+
Quoted 8 lines

drivers/gpu/drm/xe/xe_hmm.c line 206

Quoted 6 lines
drivers/gpu/drm/xe/xe_hmm.c new file +6 -0
+	ret = build_sg(vm->xe, hmm_range, &userptr->sgt, write);
+	if (ret)
+		goto free_pfns;
+
+	mark_range_accessed(hmm_range, write);
+	userptr->sg = &userptr->sgt;

Again this should be set in caller after this function return.

why we can't set it here? It is shared b/t userptr and hmmptr

Quoted 4 lines

drivers/gpu/drm/xe/xe_hmm.c line 207

Quoted 2 lines
drivers/gpu/drm/xe/xe_hmm.c new file +1 -0
+	userptr->notifier_seq = hmm_range->notifier_seq;

This is could be a pass by reference I guess and set here.

Sorry, I don't understand this comment.

Quoted 17 lines
Quoted 17 lines
drivers/gpu/drm/xe/xe_hmm.c new file +6 -0
+
+free_pfns:
+	kvfree(pfns);
+	return ret;
+}
+
drivers/gpu/drm/xe/xe_hmm.h new file +4 -0
new file mode 100644
index 000000000000..960f3f6d36ae
@@ -0,0 +1,12 @@
+// SPDX-License-Identifier: MIT
+/*
+ * Copyright © 2024 Intel Corporation
+ */
Quoted 8 lines

drivers/gpu/drm/xe/xe_hmm.h line 9

Quoted 5 lines
drivers/gpu/drm/xe/xe_hmm.h new file +5 -0
+
+#include <linux/types.h>
+#include <linux/hmm.h>
+#include "xe_vm_types.h"
+#include "xe_svm.h"

As per the previous patches no need to xe_*.h files, just forward declare any arguments.

Will do.

Oak

Quoted 10 lines

Matt

drivers/gpu/drm/xe/xe_hmm.h line 11

Quoted 2 lines
drivers/gpu/drm/xe/xe_hmm.h new file +2 -0
+
+int xe_hmm_populate_range(struct xe_vma *vma, struct hmm_range

*hmm_range,

Quoted 4 lines
drivers/gpu/drm/xe/xe_hmm.h new file +1 -1
+						bool write);
--

2.26.3

maintainer
#32 lore raw

Re: [PATCH 4/5] drm/xe: Helper to populate a userptr or hmmptr

Quoted 219 lines · Oak
Quoted 141 lines

-----Original Message-----
From: Brost, Matthew <[email protected]>
Sent: Thursday, March 14, 2024 4:25 PM
To: Zeng, Oak <[email protected]>
Cc: [email protected]; Hellstrom, Thomas <[email protected]>; [email protected]; Welty, Brian <[email protected]>; Ghimiray, Himal Prasad <[email protected]>
Subject: Re: [PATCH 4/5] drm/xe: Helper to populate a userptr or hmmptr

Quoted 11 lines · Oak Zeng

Add a helper function xe_hmm_populate_range to populate a a userptr or hmmptr range. This functions calls hmm_range_fault to read CPU page tables and populate all pfns/pages of this virtual address range.

If the populated page is system memory page, dma-mapping is performed to get a dma-address which can be used later for GPU to access pages.

If the populated page is device private page, we calculate the dpa ( device physical address) of the page.

Quoted 6 lines

The dma-address or dpa is then saved in userptr's sg table. This is prepare work to replace the get_user_pages_fast code in userptr code path. The helper function will also be used to populate hmmptr later.

Co-developed-by Niranjana Vishwanathapura

<[email protected]>

Quoted 1 line
Signed-off-by Niranjana Vishwanathapura

<[email protected]>

Quoted 6 lines
Cc Matthew Brost <[email protected]>
Cc Thomas Hellström <[email protected]>
Cc Brian Welty <[email protected]>
---
 drivers/gpu/drm/xe/Makefile |   3 +-
 drivers/gpu/drm/xe/xe_hmm.c | 213

++++++++++++++++++++++++++++++++++++

Quoted 43 lines
 drivers/gpu/drm/xe/xe_hmm.h |  12 ++
 3 files changed, 227 insertions(+), 1 deletion(-)
 create mode 100644 drivers/gpu/drm/xe/xe_hmm.c
 create mode 100644 drivers/gpu/drm/xe/xe_hmm.h
drivers/gpu/drm/xe/Makefile +2 -1
index 840467080e59..29dcbc938b01 100644
@@ -143,7 +143,8 @@ xe-y += xe_bb.o \
 	xe_wait_user_fence.o \
 	xe_wa.o \
 	xe_wopcm.o \
-	xe_svm_devmem.o
+	xe_svm_devmem.o \
+	xe_hmm.o
 
 # graphics hardware monitoring (HWMON) support
 xe-$(CONFIG_HWMON) += xe_hwmon.o
drivers/gpu/drm/xe/xe_hmm.c new file +18 -0
new file mode 100644
index 000000000000..c45c2447d386
@@ -0,0 +1,213 @@
+// SPDX-License-Identifier: MIT
+/*
+ * Copyright © 2024 Intel Corporation
+ */
+
+#include <linux/mmu_notifier.h>
+#include <linux/dma-mapping.h>
+#include <linux/memremap.h>
+#include <linux/swap.h>
+#include <linux/mm.h>
+#include "xe_hmm.h"
+#include "xe_vm.h"
+
+/**
+ * mark_range_accessed() - mark a range is accessed, so core mm
+ * have such information for memory eviction or write back to
+ * hard disk
+ *

drivers/gpu/drm/xe/xe_hmm.c line 28

Quoted 10 lines
drivers/gpu/drm/xe/xe_hmm.c new file +10 -0
+ * @range: the range to mark
+ * @write: if write to this range, we mark pages in this range
+ * as dirty
+ */
+static void mark_range_accessed(struct hmm_range *range, bool write)
+{
+	struct page *page;
+	u64 i, npages;
+
+	npages = ((range->end - 1) >> PAGE_SHIFT) - (range->start >>

PAGE_SHIFT) + 1;

Quoted 4 lines
drivers/gpu/drm/xe/xe_hmm.c new file +4 -0
+	for (i = 0; i < npages; i++) {
+		page = hmm_pfn_to_page(range->hmm_pfns[i]);
+		if (write) {
+			lock_page(page);

drivers/gpu/drm/xe/xe_hmm.c

Quoted 10 lines
drivers/gpu/drm/xe/xe_hmm.c new file +10 -0
+			set_page_dirty(page);
+			unlock_page(page);
+		}
+		mark_page_accessed(page);
+	}
+}
+
+/**
+ * build_sg() - build a scatter gather table for all the physical pages/pfn
+ * in a hmm_range. dma-address is save in sg table and will be used to

program

Quoted 23 lines
drivers/gpu/drm/xe/xe_hmm.c new file +4 -0
+ * GPU page table later.
+ *
+ * @xe: the xe device who will access the dma-address in sg table
+ * @range: the hmm range that we build the sg table from. range-

hmm_pfns[]
+ * has the pfn numbers of pages that back up this hmm address range.
+ * @st: pointer to the sg table.
+ * @write: whether we write to this range. This decides dma map direction
+ * for system pages. If write we map it bi-diretional; otherwise
+ * DMA_TO_DEVICE
+ *
+ * All the contiguous pfns will be collapsed into one entry in
+ * the scatter gather table. This is for the convenience of
+ * later on operations to bind address range to GPU page table.
+ *
+ * The dma_address in the sg table will later be used by GPU to
+ * access memory. So if the memory is system memory, we need to
+ * do a dma-mapping so it can be accessed by GPU/DMA. If the memory
+ * is GPU local memory (of the GPU who is going to access memory),
+ * we need gpu dpa (device physical address), and there is no need
+ * of dma-mapping.
+ *
+ * FIXME: dma-mapping for peer gpu device to access remote gpu's

Quoted 8 lines

+ * memory. Add this when you support p2p
+ *
+ * This function allocates the storage of the sg table. It is
+ * caller's responsibility to free it calling sg_free_table.
+ *
+ * Returns 0 if successful; -ENOMEM if fails to allocate memory
+ */
+static int build_sg(struct xe_device *xe, struct hmm_range *range,

Quoted 1 line

xe is unused.

It is used in below line

Quoted 4 lines

drivers/gpu/drm/xe/xe_hmm.c line 75

Quoted 4 lines
drivers/gpu/drm/xe/xe_hmm.c new file +3 -0
+			     struct sg_table *st, bool write)
+{
+	struct device *dev = xe->drm.dev;

Used here

Quoted 7 lines

drivers/gpu/drm/xe/xe_hmm.c

Quoted 6 lines
drivers/gpu/drm/xe/xe_hmm.c new file +6 -0
+	struct scatterlist *sg;
+	u64 i, npages;
+
+	sg = NULL;
+	st->nents = 0;
+	npages = ((range->end - 1) >> PAGE_SHIFT) - (range->start >>

PAGE_SHIFT) + 1;

Quoted 14 lines

drivers/gpu/drm/xe/xe_hmm.c line 93

Quoted 12 lines
drivers/gpu/drm/xe/xe_hmm.c new file +12 -0
+
+	if (unlikely(sg_alloc_table(st, npages, GFP_KERNEL)))
+		return -ENOMEM;
+
+	for (i = 0; i < npages; i++) {
+		struct page *page;
+		unsigned long addr;
+		struct xe_mem_region *mr;
+
+		page = hmm_pfn_to_page(range->hmm_pfns[i]);
+		if (is_device_private_page(page)) {
+			mr = page_to_mem_region(page);

Not seeing where page_to_mem_region is defined.

Yah,,,, I forgot to pick up this patch. Will pick up...

Quoted 38 lines

drivers/gpu/drm/xe/xe_hmm.c

Quoted 5 lines
drivers/gpu/drm/xe/xe_hmm.c new file +4 -0
+			addr = vram_pfn_to_dpa(mr, range->hmm_pfns[i]);
+		} else {
+			addr = dma_map_page(dev, page, 0, PAGE_SIZE,
+					write ? DMA_BIDIRECTIONAL :

DMA_TO_DEVICE);

Quoted 9 lines
drivers/gpu/drm/xe/xe_hmm.c new file +9 -0
+		}
+
+		if (sg && (addr == (sg_dma_address(sg) + sg->length))) {
+			sg->length += PAGE_SIZE;
+			sg_dma_len(sg) += PAGE_SIZE;
+			continue;
+		}
+
+		sg =  sg ? sg_next(sg) : st->sgl;

drivers/gpu/drm/xe/xe_hmm.c line 116

Quoted 10 lines
drivers/gpu/drm/xe/xe_hmm.c new file +10 -0
+		sg_dma_address(sg) = addr;
+		sg_dma_len(sg) = PAGE_SIZE;
+		sg->length = PAGE_SIZE;
+		st->nents++;
+	}
+
+	sg_mark_end(sg);
+	return 0;
+}
+

Hmm, this looks way to open coded to me.

Can't we do something like this:

struct page **pages = convert from range->hmm_pfns sg_alloc_table_from_pages_segment
if (is_device_private_page())
populatue sg table via vram_pfn_to_dpa
else
dma_map_sgtable
free(pages)

Quoted 2 lines

This assume we are not mixing is_device_private_page & system memory pages in a single struct hmm_range.

Quoted 8 lines

that is exactly I pictured. We actually can mixing vram and system memory... the reason is, migration of a hmm range from system memory to vram can fail for whatever reason. And it can end up a range is partially migrated.... And migration is best effort in such case. We just map the system memory in that range to gpu for such case. This will come up in the coming system allocator patches...

This case was found during i915 test...

I also checked amd's code. They assume the same, just take a look of function svm_range_dma_map_dev.

agree that code is not nice. But I don't have a better way assuming above

Hmm, based on what you saying if a migration failed with a partial migration I'd say we'd do one of two things.

1. Reverse the partial migration
2. Split GPUVA (or pt_state if supporting 1:N) based on the migration

I don't think we'd ever want a GPUVA (or pt_state) that is mixed between SRAM / VRAM for variety of reasons.

This is all speculation (both my comment and yours) as we haven't gotten to implementing the system allocator in Xe.

How about for this series we drop all device mapping support (this would be untested dead code in Xe which we shouldn't merge anyways) and just add the hmm layer which maps a SG for system memory only.

Once we need device mapping support we add this into hmm layer then.

Sound reasonable?

Quoted 26 lines
Quoted 26 lines
Quoted 25 lines

+/**
+ * xe_hmm_populate_range() - Populate physical pages of a virtual
+ * address range
+ *
+ * @vma: vma has information of the range to populate. only vma
+ * of userptr and hmmptr type can be populated.
+ * @hmm_range: pointer to hmm_range struct. hmm_rang->hmm_pfns
+ * will hold the populated pfns.
+ * @write: populate pages with write permission
+ *
+ * This function populate the physical pages of a virtual
+ * address range. The populated physical pages is saved in
+ * userptr's sg table. It is similar to get_user_pages but call
+ * hmm_range_fault.
+ *
+ * This function also read mmu notifier sequence # (
+ * mmu_interval_read_begin), for the purpose of later
+ * comparison (through mmu_interval_read_retry).
+ *
+ * This must be called with mmap read or write lock held.
+ *
+ * This function allocates the storage of the userptr sg table.
+ * It is caller's responsibility to free it calling sg_free_table.

Quoted 4 lines
Quoted 2 lines

I'd add a helper to free the sg_free_table & unmap the dma pages if needed.

Ok, due to the reason I explained above, there will be a little complication. I will do it in a separate patch.

Maybe something like:

void xe_hmm_unmap_sg(struct xe_device *xe, struct sg_table *sg, bool read_only)

Again for now this will call dma_unmap_sgtable & sg_free_table as we only support system memory, once we support device memory it will skip dma_unmap_sgtable device memory.

Quoted 16 lines
Quoted 11 lines
Quoted 5 lines

+ *
+ * returns: 0 for succuss; negative error no on failure
+ */
+int xe_hmm_populate_range(struct xe_vma *vma, struct hmm_range

*hmm_range,

drivers/gpu/drm/xe/xe_hmm.c line 145

Quoted 2 lines
drivers/gpu/drm/xe/xe_hmm.c new file +2 -0
+						bool write)
+{

The layering is all wrong here, we shouldn't be touching struct xe_vma directly in hmm layer.

I have to admit we don't have a clear layering here. This function is supposed to be a shared function which will be used by both hmmptr and userptr.

Maybe you got an impression from my POC series that we don't have xe_vma in system allocator codes. That was true. But after the design discussion, we have decided to unify userptr code and hmmptr codes, so we will have xe_vma also in system allocator code. Basically xe_vma will replace the xe_svm_range concept in my patch series.

Quoted 8 lines

Of course, per Thomas we can further optimize by splitting xe_vma into mutable and unmutable... but that should be step 2.

Quoted 3 lines

Pass in a populated hmm_range and sgt. Or alternatively pass in arguments and then populate a hmm_range locally.

Xe_vma is the input parameter here.

It shouldn't be, thats my point. It is the wrong layering.

Quoted 2 lines

I will remove Hmm_range from function parameter and make it a local. I figured I don't need this as an output anymore. All we need is already in sg table.

I was thinking a prototype like this:

int xe_hmm_map_sg(struct xe_device *xe, struct sg_table *sg,
struct mmu_interval_notifier *notifier, long unsigned *current_seq, u64 addr, u64 size, bool read_only, bool need_unmap)

Again now only system memory support. This function will replace most of the current code in xe_vma_userptr_pin_pages. I have more on this in patch 5.

If you like xe_hmm_populate_sg more, open to that name too. As with xe_hmm_unmap_sg, feel free to rename too.

Quoted 49 lines
Quoted 12 lines

drivers/gpu/drm/xe/xe_hmm.c line 152

Quoted 8 lines
drivers/gpu/drm/xe/xe_hmm.c new file +7 -0
+	unsigned long timeout =
+		jiffies + msecs_to_jiffies(HMM_RANGE_DEFAULT_TIMEOUT);
+	unsigned long *pfns, flags = HMM_PFN_REQ_FAULT;
+	struct xe_userptr_vma *userptr_vma;
+	struct xe_userptr *userptr;
+	u64 start = vma->gpuva.va.addr;
+	u64 end = start + vma->gpuva.va.range;

We have helper - xe_vma_start and xe_vma_end but I think either of these are correct in this case.

Quoted 7 lines

xe_vma_start is the address which this bound to the GPU, we want the userptr address.

So I think it would be:

start = xe_vma_userptr()
end = xe_vma_userptr() + xe_vma_size()

You are correct. Will fix. I mixed this because, in system allocator, cpu address always == gpu address

Quoted 13 lines

drivers/gpu/drm/xe/xe_hmm.c line 161

Quoted 11 lines
drivers/gpu/drm/xe/xe_hmm.c new file +9 -0
+	struct xe_vm *vm = xe_vma_vm(vma);
+	u64 npages;
+	int ret;
+
+	userptr_vma = to_userptr_vma(vma);
+	userptr = &userptr_vma->userptr;
+	mmap_assert_locked(userptr->notifier.mm);
+
+	npages = ((end - 1) >> PAGE_SHIFT) - (start >> PAGE_SHIFT) + 1;

This math is done above, if you need this math in next rev add a helper.

Will do.

Quoted 11 lines

drivers/gpu/drm/xe/xe_hmm.c line 169

Quoted 9 lines
drivers/gpu/drm/xe/xe_hmm.c new file +8 -0
+	pfns = kvmalloc_array(npages, sizeof(*pfns), GFP_KERNEL);
+	if (unlikely(!pfns))
+		return -ENOMEM;
+
+	if (write)
+		flags |= HMM_PFN_REQ_WRITE;
+
+	memset64((u64 *)pfns, (u64)flags, npages);

Why is this needed, can't we just set hmm_range->default_flags?

Quoted 8 lines

In this case, set default_flags also work.

This is some codes I inherited from Niranjana. Per my test before it works.

Basically there are two way to control the flags:
Default is the coarse grain way applying to all pfns
The way I am using here is a per pfn fine grained flag setting. It also works.

I'd stick with default_flags unless we have reason not to.

Quoted 9 lines
Quoted 7 lines
Quoted 4 lines
drivers/gpu/drm/xe/xe_hmm.c new file +2 -0
+	hmm_range->hmm_pfns = pfns;
+	hmm_range->notifier_seq = mmu_interval_read_begin(&userptr-

notifier);

We need a userptr->notifier == userptr->notifier_seq check that just returns, right?

Yes this sequence number is used to decide whether we need to retry. See function xe_pt_userptr_pre_commit

This check basically means the current sg is valid and short circuits.

Maybe we don't need it? I don't think we ever call this function when this condition is true. But if it is removed then we should have an assert userptr->notifier_seq (*current_seq in my function prototype above) != mmu_interval_read_begin. This ensures we are not calling this function / mapping a sg table when already have a valid one.

Quoted 7 lines
Quoted 7 lines

drivers/gpu/drm/xe/xe_hmm.c

Quoted 5 lines
drivers/gpu/drm/xe/xe_hmm.c new file +4 -0
+	hmm_range->notifier = &userptr->notifier;
+	hmm_range->start = start;
+	hmm_range->end = end;
+	hmm_range->pfn_flags_mask = HMM_PFN_REQ_FAULT |

HMM_PFN_REQ_WRITE;

Quoted 5 lines
Quoted 1 line

Is this needed? AMD and Nouveau do not set this argument.

As explained above. Amd and nouveau only use coarse grain setting

My position is use coarse grain until we have a reason not to.

Quoted 17 lines
Quoted 17 lines
Quoted 4 lines
drivers/gpu/drm/xe/xe_hmm.c new file +3 -0
+	/**
+	 * FIXME:
+	 * Set the the dev_private_owner can prevent hmm_range_fault to fault

drivers/gpu/drm/xe/xe_hmm.c line 188

Quoted 10 lines
drivers/gpu/drm/xe/xe_hmm.c new file +10 -0
+	 * in the device private pages owned by caller. See function
+	 * hmm_vma_handle_pte. In multiple GPU case, this should be set to the
+	 * device owner of the best migration destination. e.g., device0/vm0
+	 * has a page fault, but we have determined the best placement of
+	 * the fault address should be on device1, we should set below to
+	 * device1 instead of device0.
+	 */
+	hmm_range->dev_private_owner = vm->xe->drm.dev;
+
+	while (true) {

mmap_read_lock(mm);

Quoted 9 lines
Quoted 3 lines

drivers/gpu/drm/xe/xe_hmm.c line 189

Quoted 1 line
drivers/gpu/drm/xe/xe_hmm.c new file +1 -0
+		ret = hmm_range_fault(hmm_range);

mmap_read_unlock(mm);

This need to be called in caller. The reason is, in the system allocator code path, mmap lock is already hold before calling into this helper.

I will check patch 5 for this purpose.

This is missing in patch 5.

Also per the hmm.rst doc and 2 of the 3 existing existing cases in of hmm_range_fault in the kernel this lock is taken / dropped directly before / after hmm_range_fault. For the current use case (userptr) taking this lock here certainly will work.

Again saying the system allocator will hold this lock is IMO speculation and shouldn't dictate what we do in this series. If we have move the lock in once the system allocator lands, we can do that then.

Quoted 21 lines
Quoted 12 lines
Quoted 12 lines
drivers/gpu/drm/xe/xe_hmm.c new file +11 -0
+		if (time_after(jiffies, timeout))
+			break;
+
+		if (ret == -EBUSY)
+			continue;
+		break;
+	}
+
+	if (ret)
+		goto free_pfns;
+
Quoted 8 lines

drivers/gpu/drm/xe/xe_hmm.c line 206

Quoted 6 lines
drivers/gpu/drm/xe/xe_hmm.c new file +6 -0
+	ret = build_sg(vm->xe, hmm_range, &userptr->sgt, write);
+	if (ret)
+		goto free_pfns;
+
+	mark_range_accessed(hmm_range, write);
+	userptr->sg = &userptr->sgt;

Again this should be set in caller after this function return.

Quoted 2 lines

why we can't set it here? It is shared b/t userptr and hmmptr

This is owned by the VMA userptr and should be set in that layer (set in xe_vma_userptr_pin_pages). Setting this value means the SG is valid (this maps to need_unmap in my function prototype example).

Quoted 8 lines
Quoted 5 lines

drivers/gpu/drm/xe/xe_hmm.c line 207

Quoted 3 lines
drivers/gpu/drm/xe/xe_hmm.c new file +1 -0
+	userptr->notifier_seq = hmm_range->notifier_seq;

This is could be a pass by reference I guess and set here.

Sorry, I don't understand this comment.

This is current_seq in my function prototype example. Does that make sense?

Matt

Quoted 40 lines
Quoted 17 lines
Quoted 17 lines
drivers/gpu/drm/xe/xe_hmm.c new file +6 -0
+
+free_pfns:
+	kvfree(pfns);
+	return ret;
+}
+
drivers/gpu/drm/xe/xe_hmm.h new file +4 -0
new file mode 100644
index 000000000000..960f3f6d36ae
@@ -0,0 +1,12 @@
+// SPDX-License-Identifier: MIT
+/*
+ * Copyright © 2024 Intel Corporation
+ */
Quoted 8 lines

drivers/gpu/drm/xe/xe_hmm.h line 9

Quoted 5 lines
drivers/gpu/drm/xe/xe_hmm.h new file +5 -0
+
+#include <linux/types.h>
+#include <linux/hmm.h>
+#include "xe_vm_types.h"
+#include "xe_svm.h"

As per the previous patches no need to xe_*.h files, just forward declare any arguments.

Will do.

Oak

Quoted 10 lines

Matt

drivers/gpu/drm/xe/xe_hmm.h line 11

Quoted 2 lines
drivers/gpu/drm/xe/xe_hmm.h new file +2 -0
+
+int xe_hmm_populate_range(struct xe_vma *vma, struct hmm_range

*hmm_range,

Quoted 4 lines
drivers/gpu/drm/xe/xe_hmm.h new file +1 -1
+						bool write);
--

2.26.3

#33 lore raw

Re: [PATCH 4/5] drm/xe: Helper to populate a userptr or hmmptr

In reply to Oak Zeng

Hi, Oak.

Quoted 28 lines · Oak Zeng

Add a helper function xe_hmm_populate_range to populate a a userptr or hmmptr range. This functions calls hmm_range_fault to read CPU page tables and populate all pfns/pages of this virtual address range.

If the populated page is system memory page, dma-mapping is performed to get a dma-address which can be used later for GPU to access pages.

If the populated page is device private page, we calculate the dpa ( device physical address) of the page.

The dma-address or dpa is then saved in userptr's sg table. This is prepare work to replace the get_user_pages_fast code in userptr code path. The helper function will also be used to populate hmmptr later.

Co-developed-by Niranjana Vishwanathapura

<[email protected]>
Signed-off-by: Niranjana Vishwanathapura
<[email protected]>
Cc: Matthew Brost <[email protected]>
Cc: Thomas Hellström <[email protected]>
Cc: Brian Welty <[email protected]>
---
 drivers/gpu/drm/xe/Makefile |   3 +-
 drivers/gpu/drm/xe/xe_hmm.c | 213
++++++++++++++++++++++++++++++++++++
 drivers/gpu/drm/xe/xe_hmm.h |  12 ++

Quoted 3 lines
 3 files changed, 227 insertions(+), 1 deletion(-)

 create mode 100644 drivers/gpu/drm/xe/xe_hmm.c
 create mode 100644 drivers/gpu/drm/xe/xe_hmm.h

I mostly agree with Matt's review comments on this patch. Some additional below.

Quoted 36 lines
drivers/gpu/drm/xe/Makefile +2 -1
index 840467080e59..29dcbc938b01 100644
@@ -143,7 +143,8 @@ xe-y += xe_bb.o \
  	xe_wait_user_fence.o \
  	xe_wa.o \
  	xe_wopcm.o \
-	xe_svm_devmem.o
+	xe_svm_devmem.o \
+	xe_hmm.o

 # graphics hardware monitoring (HWMON) support
 xe-$(CONFIG_HWMON) += xe_hwmon.o
drivers/gpu/drm/xe/xe_hmm.c new file +13 -0
new file mode 100644
index 000000000000..c45c2447d386
@@ -0,0 +1,213 @@
+// SPDX-License-Identifier: MIT
+/*
+ * Copyright © 2024 Intel Corporation
+ */
+
+#include <linux/mmu_notifier.h>
+#include <linux/dma-mapping.h>
+#include <linux/memremap.h>
+#include <linux/swap.h>
+#include <linux/mm.h>
+#include "xe_hmm.h"
+#include "xe_vm.h"
+

drivers/gpu/drm/xe/xe_hmm.c line 23

Quoted 10 lines
drivers/gpu/drm/xe/xe_hmm.c new file +10 -0
+/**
+ * mark_range_accessed() - mark a range is accessed, so core mm
+ * have such information for memory eviction or write back to
+ * hard disk
+ *
+ * @range: the range to mark
+ * @write: if write to this range, we mark pages in this range
+ * as dirty
+ */
+static void mark_range_accessed(struct hmm_range *range, bool write)

Some of the static function names aren't really unique enough not to stand in the way for a future core function name clash. Please consider using an xe_ prefix in such cases. It will also make backtraces easier to follow.

drivers/gpu/drm/xe/xe_hmm.c line 34

Quoted 12 lines
drivers/gpu/drm/xe/xe_hmm.c new file +5 -0
+{
+	struct page *page;
+	u64 i, npages;
+
+	npages = ((range->end - 1) >> PAGE_SHIFT) - (range->start >>

PAGE_SHIFT) + 1;

drivers/gpu/drm/xe/xe_hmm.c new file +6 -0
+	for (i = 0; i < npages; i++) {
+		page = hmm_pfn_to_page(range->hmm_pfns[i]);
+		if (write) {
+			lock_page(page);
+			set_page_dirty(page);
+			unlock_page(page);

Could be using set_page_dirty_lock() here.

/Thomas

Quoted 217 lines
drivers/gpu/drm/xe/xe_hmm.c new file +7 -0
+		}
+		mark_page_accessed(page);
+	}
+}
+
+/**
+ * build_sg() - build a scatter gather table for all the physical

pages/pfn
+ * in a hmm_range. dma-address is save in sg table and will be used to program
+ * GPU page table later.
+ *
+ * @xe: the xe device who will access the dma-address in sg table
+ * @range: the hmm range that we build the sg table from. range-

Quoted 1 line

hmm_pfns[]

+ * has the pfn numbers of pages that back up this hmm address range.
+ * @st: pointer to the sg table.
+ * @write: whether we write to this range. This decides dma map direction
+ * for system pages. If write we map it bi-diretional; otherwise
+ * DMA_TO_DEVICE
+ *
+ * All the contiguous pfns will be collapsed into one entry in
+ * the scatter gather table. This is for the convenience of
+ * later on operations to bind address range to GPU page table.
+ *
+ * The dma_address in the sg table will later be used by GPU to
+ * access memory. So if the memory is system memory, we need to
+ * do a dma-mapping so it can be accessed by GPU/DMA. If the memory
+ * is GPU local memory (of the GPU who is going to access memory),
+ * we need gpu dpa (device physical address), and there is no need
+ * of dma-mapping.
+ *
+ * FIXME: dma-mapping for peer gpu device to access remote gpu's
+ * memory. Add this when you support p2p
+ *
+ * This function allocates the storage of the sg table. It is
+ * caller's responsibility to free it calling sg_free_table.
+ *
+ * Returns 0 if successful; -ENOMEM if fails to allocate memory
+ */

drivers/gpu/drm/xe/xe_hmm.c new file +10 -0
+static int build_sg(struct xe_device *xe, struct hmm_range *range,
+			     struct sg_table *st, bool write)
+{
+	struct device *dev = xe->drm.dev;
+	struct scatterlist *sg;
+	u64 i, npages;
+
+	sg = NULL;
+	st->nents = 0;
+	npages = ((range->end - 1) >> PAGE_SHIFT) - (range->start >>

PAGE_SHIFT) + 1;

drivers/gpu/drm/xe/xe_hmm.c new file +13 -0
+
+	if (unlikely(sg_alloc_table(st, npages, GFP_KERNEL)))
+		return -ENOMEM;
+
+	for (i = 0; i < npages; i++) {
+		struct page *page;
+		unsigned long addr;
+		struct xe_mem_region *mr;
+
+		page = hmm_pfn_to_page(range->hmm_pfns[i]);
+		if (is_device_private_page(page)) {
+			mr = page_to_mem_region(page);
+			addr = vram_pfn_to_dpa(mr, range-
Quoted 1 line

hmm_pfns[i]);

drivers/gpu/drm/xe/xe_hmm.c new file +3 -0
+		} else {
+			addr = dma_map_page(dev, page, 0, PAGE_SIZE,
+					write ? DMA_BIDIRECTIONAL :

DMA_TO_DEVICE);

drivers/gpu/drm/xe/xe_hmm.c new file +3 -0
+		}
+
+		if (sg && (addr == (sg_dma_address(sg) + sg-
Quoted 1 line

length))) {

drivers/gpu/drm/xe/xe_hmm.c new file +43 -0
+			sg->length += PAGE_SIZE;
+			sg_dma_len(sg) += PAGE_SIZE;
+			continue;
+		}
+
+		sg =  sg ? sg_next(sg) : st->sgl;
+		sg_dma_address(sg) = addr;
+		sg_dma_len(sg) = PAGE_SIZE;
+		sg->length = PAGE_SIZE;
+		st->nents++;
+	}
+
+	sg_mark_end(sg);
+	return 0;
+}
+
+/**
+ * xe_hmm_populate_range() - Populate physical pages of a virtual
+ * address range
+ *
+ * @vma: vma has information of the range to populate. only vma
+ * of userptr and hmmptr type can be populated.
+ * @hmm_range: pointer to hmm_range struct. hmm_rang->hmm_pfns
+ * will hold the populated pfns.
+ * @write: populate pages with write permission
+ *
+ * This function populate the physical pages of a virtual
+ * address range. The populated physical pages is saved in
+ * userptr's sg table. It is similar to get_user_pages but call
+ * hmm_range_fault.
+ *
+ * This function also read mmu notifier sequence # (
+ * mmu_interval_read_begin), for the purpose of later
+ * comparison (through mmu_interval_read_retry).
+ *
+ * This must be called with mmap read or write lock held.
+ *
+ * This function allocates the storage of the userptr sg table.
+ * It is caller's responsibility to free it calling sg_free_table.
+ *
+ * returns: 0 for succuss; negative error no on failure
+ */
+int xe_hmm_populate_range(struct xe_vma *vma, struct hmm_range

*hmm_range,

drivers/gpu/drm/xe/xe_hmm.c new file +4 -0
+						bool write)
+{
+	unsigned long timeout =
+		jiffies +

msecs_to_jiffies(HMM_RANGE_DEFAULT_TIMEOUT);

drivers/gpu/drm/xe/xe_hmm.c new file +14 -0
+	unsigned long *pfns, flags = HMM_PFN_REQ_FAULT;
+	struct xe_userptr_vma *userptr_vma;
+	struct xe_userptr *userptr;
+	u64 start = vma->gpuva.va.addr;
+	u64 end = start + vma->gpuva.va.range;
+	struct xe_vm *vm = xe_vma_vm(vma);
+	u64 npages;
+	int ret;
+
+	userptr_vma = to_userptr_vma(vma);
+	userptr = &userptr_vma->userptr;
+	mmap_assert_locked(userptr->notifier.mm);
+
+	npages = ((end - 1) >> PAGE_SHIFT) - (start >> PAGE_SHIFT) +

1;

drivers/gpu/drm/xe/xe_hmm.c new file +10 -0
+	pfns = kvmalloc_array(npages, sizeof(*pfns), GFP_KERNEL);
+	if (unlikely(!pfns))
+		return -ENOMEM;
+
+	if (write)
+		flags |= HMM_PFN_REQ_WRITE;
+
+	memset64((u64 *)pfns, (u64)flags, npages);
+	hmm_range->hmm_pfns = pfns;
+	hmm_range->notifier_seq = mmu_interval_read_begin(&userptr-
Quoted 1 line

notifier);

drivers/gpu/drm/xe/xe_hmm.c new file +4 -0
+	hmm_range->notifier = &userptr->notifier;
+	hmm_range->start = start;
+	hmm_range->end = end;
+	hmm_range->pfn_flags_mask = HMM_PFN_REQ_FAULT |

HMM_PFN_REQ_WRITE;

drivers/gpu/drm/xe/xe_hmm.c new file +3 -0
+	/**
+	 * FIXME:
+	 * Set the the dev_private_owner can prevent hmm_range_fault

to fault

drivers/gpu/drm/xe/xe_hmm.c new file +2 -0
+	 * in the device private pages owned by caller. See function
+	 * hmm_vma_handle_pte. In multiple GPU case, this should be

set to the

drivers/gpu/drm/xe/xe_hmm.c new file +1 -0
+	 * device owner of the best migration destination. e.g.,

device0/vm0

drivers/gpu/drm/xe/xe_hmm.c new file +1 -0
+	 * has a page fault, but we have determined the best

placement of

drivers/gpu/drm/xe/xe_hmm.c new file +1 -0
+	 * the fault address should be on device1, we should set

below to

drivers/gpu/drm/xe/xe_hmm.c new file +30 -0
+	 * device1 instead of device0.
+	 */
+	hmm_range->dev_private_owner = vm->xe->drm.dev;
+
+	while (true) {
+		ret = hmm_range_fault(hmm_range);
+		if (time_after(jiffies, timeout))
+			break;
+
+		if (ret == -EBUSY)
+			continue;
+		break;
+	}
+
+	if (ret)
+		goto free_pfns;
+
+	ret = build_sg(vm->xe, hmm_range, &userptr->sgt, write);
+	if (ret)
+		goto free_pfns;
+
+	mark_range_accessed(hmm_range, write);
+	userptr->sg = &userptr->sgt;
+	userptr->notifier_seq = hmm_range->notifier_seq;
+
+free_pfns:
+	kvfree(pfns);
+	return ret;
+}
+
drivers/gpu/drm/xe/xe_hmm.h new file +11 -0
new file mode 100644
index 000000000000..960f3f6d36ae
@@ -0,0 +1,12 @@
+// SPDX-License-Identifier: MIT
+/*
+ * Copyright © 2024 Intel Corporation
+ */
+
+#include <linux/types.h>
+#include <linux/hmm.h>
+#include "xe_vm_types.h"
+#include "xe_svm.h"
+
+int xe_hmm_populate_range(struct xe_vma *vma, struct hmm_range

*hmm_range,

drivers/gpu/drm/xe/xe_hmm.h new file +1 -0
+						bool write);
author
#34 lore raw

RE: [PATCH 4/5] drm/xe: Helper to populate a userptr or hmmptr

Quoted 93 lines

-----Original Message-----
From: Hellstrom, Thomas <[email protected]>
Sent: Monday, March 18, 2024 7:54 AM
To: [email protected]; Zeng, Oak <[email protected]>
Cc: Brost, Matthew <[email protected]>; Welty, Brian <[email protected]>; [email protected]; Ghimiray, Himal Prasad <[email protected]>
Subject: Re: [PATCH 4/5] drm/xe: Helper to populate a userptr or hmmptr

Hi, Oak.

Quoted 28 lines · Oak Zeng

Add a helper function xe_hmm_populate_range to populate a a userptr or hmmptr range. This functions calls hmm_range_fault to read CPU page tables and populate all pfns/pages of this virtual address range.

If the populated page is system memory page, dma-mapping is performed to get a dma-address which can be used later for GPU to access pages.

If the populated page is device private page, we calculate the dpa ( device physical address) of the page.

The dma-address or dpa is then saved in userptr's sg table. This is prepare work to replace the get_user_pages_fast code in userptr code path. The helper function will also be used to populate hmmptr later.

Co-developed-by Niranjana Vishwanathapura

<[email protected]>
Signed-off-by: Niranjana Vishwanathapura
<[email protected]>
Cc: Matthew Brost <[email protected]>
Cc: Thomas Hellström <[email protected]>
Cc: Brian Welty <[email protected]>
---
 drivers/gpu/drm/xe/Makefile |   3 +-
 drivers/gpu/drm/xe/xe_hmm.c | 213
++++++++++++++++++++++++++++++++++++
 drivers/gpu/drm/xe/xe_hmm.h |  12 ++

Quoted 3 lines
 3 files changed, 227 insertions(+), 1 deletion(-)

 create mode 100644 drivers/gpu/drm/xe/xe_hmm.c
 create mode 100644 drivers/gpu/drm/xe/xe_hmm.h

I mostly agree with Matt's review comments on this patch. Some additional below.

Quoted 43 lines
drivers/gpu/drm/xe/Makefile +2 -1
index 840467080e59..29dcbc938b01 100644
@@ -143,7 +143,8 @@ xe-y += xe_bb.o \
  	xe_wait_user_fence.o \
  	xe_wa.o \
  	xe_wopcm.o \
-	xe_svm_devmem.o
+	xe_svm_devmem.o \
+	xe_hmm.o
 
  # graphics hardware monitoring (HWMON) support

 xe-$(CONFIG_HWMON) += xe_hwmon.o

drivers/gpu/drm/xe/xe_hmm.c new file +20 -0
new file mode 100644
index 000000000000..c45c2447d386
@@ -0,0 +1,213 @@
+// SPDX-License-Identifier: MIT
+/*
+ * Copyright © 2024 Intel Corporation
+ */
+
+#include <linux/mmu_notifier.h>
+#include <linux/dma-mapping.h>
+#include <linux/memremap.h>
+#include <linux/swap.h>
+#include <linux/mm.h>
+#include "xe_hmm.h"
+#include "xe_vm.h"
+
+/**
+ * mark_range_accessed() - mark a range is accessed, so core mm
+ * have such information for memory eviction or write back to
+ * hard disk
+ *
+ * @range: the range to mark
+ * @write: if write to this range, we mark pages in this range
Quoted 8 lines

drivers/gpu/drm/xe/xe_hmm.c line 23

Quoted 3 lines
drivers/gpu/drm/xe/xe_hmm.c new file +3 -0
+ * as dirty
+ */
+static void mark_range_accessed(struct hmm_range *range, bool write)

Some of the static function names aren't really unique enough not to stand in the way for a future core function name clash. Please consider using an xe_ prefix in such cases. It will also make backtraces easier to follow.

I will add a xe_prefix for the backtrace reason...

As I understand it, static function is file scope, so even if we have a core function with The same name in the future, as long as they are not in the same file, there wont be any name clash...

Quoted 16 lines
Quoted 8 lines
drivers/gpu/drm/xe/xe_hmm.c new file +5 -0
+{
+	struct page *page;
+	u64 i, npages;
+
+	npages = ((range->end - 1) >> PAGE_SHIFT) - (range->start >>

PAGE_SHIFT) + 1;

drivers/gpu/drm/xe/xe_hmm.c line 34

Quoted 6 lines
drivers/gpu/drm/xe/xe_hmm.c new file +6 -0
+	for (i = 0; i < npages; i++) {
+		page = hmm_pfn_to_page(range->hmm_pfns[i]);
+		if (write) {
+			lock_page(page);
+			set_page_dirty(page);
+			unlock_page(page);

Could be using set_page_dirty_lock() here.

Will fix, Thanks
Oak

Quoted 221 lines

/Thomas

Quoted 217 lines
drivers/gpu/drm/xe/xe_hmm.c new file +7 -0
+		}
+		mark_page_accessed(page);
+	}
+}
+
+/**
+ * build_sg() - build a scatter gather table for all the physical

pages/pfn
+ * in a hmm_range. dma-address is save in sg table and will be used to program
+ * GPU page table later.
+ *
+ * @xe: the xe device who will access the dma-address in sg table
+ * @range: the hmm range that we build the sg table from. range-

Quoted 1 line

hmm_pfns[]

+ * has the pfn numbers of pages that back up this hmm address range.
+ * @st: pointer to the sg table.
+ * @write: whether we write to this range. This decides dma map direction
+ * for system pages. If write we map it bi-diretional; otherwise
+ * DMA_TO_DEVICE
+ *
+ * All the contiguous pfns will be collapsed into one entry in
+ * the scatter gather table. This is for the convenience of
+ * later on operations to bind address range to GPU page table.
+ *
+ * The dma_address in the sg table will later be used by GPU to
+ * access memory. So if the memory is system memory, we need to
+ * do a dma-mapping so it can be accessed by GPU/DMA. If the memory
+ * is GPU local memory (of the GPU who is going to access memory),
+ * we need gpu dpa (device physical address), and there is no need
+ * of dma-mapping.
+ *
+ * FIXME: dma-mapping for peer gpu device to access remote gpu's
+ * memory. Add this when you support p2p
+ *
+ * This function allocates the storage of the sg table. It is
+ * caller's responsibility to free it calling sg_free_table.
+ *
+ * Returns 0 if successful; -ENOMEM if fails to allocate memory
+ */

drivers/gpu/drm/xe/xe_hmm.c new file +10 -0
+static int build_sg(struct xe_device *xe, struct hmm_range *range,
+			     struct sg_table *st, bool write)
+{
+	struct device *dev = xe->drm.dev;
+	struct scatterlist *sg;
+	u64 i, npages;
+
+	sg = NULL;
+	st->nents = 0;
+	npages = ((range->end - 1) >> PAGE_SHIFT) - (range->start >>

PAGE_SHIFT) + 1;

drivers/gpu/drm/xe/xe_hmm.c new file +13 -0
+
+	if (unlikely(sg_alloc_table(st, npages, GFP_KERNEL)))
+		return -ENOMEM;
+
+	for (i = 0; i < npages; i++) {
+		struct page *page;
+		unsigned long addr;
+		struct xe_mem_region *mr;
+
+		page = hmm_pfn_to_page(range->hmm_pfns[i]);
+		if (is_device_private_page(page)) {
+			mr = page_to_mem_region(page);
+			addr = vram_pfn_to_dpa(mr, range-
Quoted 1 line

hmm_pfns[i]);

drivers/gpu/drm/xe/xe_hmm.c new file +3 -0
+		} else {
+			addr = dma_map_page(dev, page, 0, PAGE_SIZE,
+					write ? DMA_BIDIRECTIONAL :

DMA_TO_DEVICE);

drivers/gpu/drm/xe/xe_hmm.c new file +3 -0
+		}
+
+		if (sg && (addr == (sg_dma_address(sg) + sg-
Quoted 1 line

length))) {

drivers/gpu/drm/xe/xe_hmm.c new file +43 -0
+			sg->length += PAGE_SIZE;
+			sg_dma_len(sg) += PAGE_SIZE;
+			continue;
+		}
+
+		sg =  sg ? sg_next(sg) : st->sgl;
+		sg_dma_address(sg) = addr;
+		sg_dma_len(sg) = PAGE_SIZE;
+		sg->length = PAGE_SIZE;
+		st->nents++;
+	}
+
+	sg_mark_end(sg);
+	return 0;
+}
+
+/**
+ * xe_hmm_populate_range() - Populate physical pages of a virtual
+ * address range
+ *
+ * @vma: vma has information of the range to populate. only vma
+ * of userptr and hmmptr type can be populated.
+ * @hmm_range: pointer to hmm_range struct. hmm_rang->hmm_pfns
+ * will hold the populated pfns.
+ * @write: populate pages with write permission
+ *
+ * This function populate the physical pages of a virtual
+ * address range. The populated physical pages is saved in
+ * userptr's sg table. It is similar to get_user_pages but call
+ * hmm_range_fault.
+ *
+ * This function also read mmu notifier sequence # (
+ * mmu_interval_read_begin), for the purpose of later
+ * comparison (through mmu_interval_read_retry).
+ *
+ * This must be called with mmap read or write lock held.
+ *
+ * This function allocates the storage of the userptr sg table.
+ * It is caller's responsibility to free it calling sg_free_table.
+ *
+ * returns: 0 for succuss; negative error no on failure
+ */
+int xe_hmm_populate_range(struct xe_vma *vma, struct hmm_range

*hmm_range,

drivers/gpu/drm/xe/xe_hmm.c new file +4 -0
+						bool write)
+{
+	unsigned long timeout =
+		jiffies +

msecs_to_jiffies(HMM_RANGE_DEFAULT_TIMEOUT);

drivers/gpu/drm/xe/xe_hmm.c new file +14 -0
+	unsigned long *pfns, flags = HMM_PFN_REQ_FAULT;
+	struct xe_userptr_vma *userptr_vma;
+	struct xe_userptr *userptr;
+	u64 start = vma->gpuva.va.addr;
+	u64 end = start + vma->gpuva.va.range;
+	struct xe_vm *vm = xe_vma_vm(vma);
+	u64 npages;
+	int ret;
+
+	userptr_vma = to_userptr_vma(vma);
+	userptr = &userptr_vma->userptr;
+	mmap_assert_locked(userptr->notifier.mm);
+
+	npages = ((end - 1) >> PAGE_SHIFT) - (start >> PAGE_SHIFT) +

1;

drivers/gpu/drm/xe/xe_hmm.c new file +10 -0
+	pfns = kvmalloc_array(npages, sizeof(*pfns), GFP_KERNEL);
+	if (unlikely(!pfns))
+		return -ENOMEM;
+
+	if (write)
+		flags |= HMM_PFN_REQ_WRITE;
+
+	memset64((u64 *)pfns, (u64)flags, npages);
+	hmm_range->hmm_pfns = pfns;
+	hmm_range->notifier_seq = mmu_interval_read_begin(&userptr-
Quoted 1 line

notifier);

drivers/gpu/drm/xe/xe_hmm.c new file +4 -0
+	hmm_range->notifier = &userptr->notifier;
+	hmm_range->start = start;
+	hmm_range->end = end;
+	hmm_range->pfn_flags_mask = HMM_PFN_REQ_FAULT |

HMM_PFN_REQ_WRITE;

drivers/gpu/drm/xe/xe_hmm.c new file +3 -0
+	/**
+	 * FIXME:
+	 * Set the the dev_private_owner can prevent hmm_range_fault

to fault

drivers/gpu/drm/xe/xe_hmm.c new file +2 -0
+	 * in the device private pages owned by caller. See function
+	 * hmm_vma_handle_pte. In multiple GPU case, this should be

set to the

drivers/gpu/drm/xe/xe_hmm.c new file +1 -0
+	 * device owner of the best migration destination. e.g.,

device0/vm0

drivers/gpu/drm/xe/xe_hmm.c new file +1 -0
+	 * has a page fault, but we have determined the best

placement of

drivers/gpu/drm/xe/xe_hmm.c new file +1 -0
+	 * the fault address should be on device1, we should set

below to

drivers/gpu/drm/xe/xe_hmm.c new file +30 -0
+	 * device1 instead of device0.
+	 */
+	hmm_range->dev_private_owner = vm->xe->drm.dev;
+
+	while (true) {
+		ret = hmm_range_fault(hmm_range);
+		if (time_after(jiffies, timeout))
+			break;
+
+		if (ret == -EBUSY)
+			continue;
+		break;
+	}
+
+	if (ret)
+		goto free_pfns;
+
+	ret = build_sg(vm->xe, hmm_range, &userptr->sgt, write);
+	if (ret)
+		goto free_pfns;
+
+	mark_range_accessed(hmm_range, write);
+	userptr->sg = &userptr->sgt;
+	userptr->notifier_seq = hmm_range->notifier_seq;
+
+free_pfns:
+	kvfree(pfns);
+	return ret;
+}
+
drivers/gpu/drm/xe/xe_hmm.h new file +11 -0
new file mode 100644
index 000000000000..960f3f6d36ae
@@ -0,0 +1,12 @@
+// SPDX-License-Identifier: MIT
+/*
+ * Copyright © 2024 Intel Corporation
+ */
+
+#include <linux/types.h>
+#include <linux/hmm.h>
+#include "xe_vm_types.h"
+#include "xe_svm.h"
+
+int xe_hmm_populate_range(struct xe_vma *vma, struct hmm_range

*hmm_range,

drivers/gpu/drm/xe/xe_hmm.h new file +1 -0
+						bool write);
#35 lore raw

Re: [PATCH 4/5] drm/xe: Helper to populate a userptr or hmmptr

Quoted 113 lines · Oak
Quoted 105 lines

-----Original Message-----
From: Hellstrom, Thomas <[email protected]>
Sent: Monday, March 18, 2024 7:54 AM
To: [email protected]; Zeng, Oak <[email protected]>
Cc: Brost, Matthew <[email protected]>; Welty, Brian <[email protected]>; [email protected]; Ghimiray, Himal Prasad <[email protected]>
Subject: Re: [PATCH 4/5] drm/xe: Helper to populate a userptr or hmmptr

Hi, Oak.

Quoted 34 lines · Oak Zeng

Add a helper function xe_hmm_populate_range to populate a a userptr or hmmptr range. This functions calls hmm_range_fault to read CPU page tables and populate all pfns/pages of this virtual address range.

If the populated page is system memory page, dma-mapping is performed
to get a dma-address which can be used later for GPU to access pages.

If the populated page is device private page, we calculate the dpa (
device physical address) of the page.

The dma-address or dpa is then saved in userptr's sg table. This is
prepare work to replace the get_user_pages_fast code in userptr code
path. The helper function will also be used to populate hmmptr later.

Co-developed-by Niranjana Vishwanathapura

<[email protected]>
Signed-off-by: Niranjana Vishwanathapura
<[email protected]>
Cc: Matthew Brost <[email protected]>
Cc: Thomas Hellström <[email protected]>
Cc: Brian Welty <[email protected]>
---
 drivers/gpu/drm/xe/Makefile |   3 +-
 drivers/gpu/drm/xe/xe_hmm.c | 213
++++++++++++++++++++++++++++++++++++
 drivers/gpu/drm/xe/xe_hmm.h |  12 ++

Quoted 3 lines
 3 files changed, 227 insertions(+), 1 deletion(-)

 create mode 100644 drivers/gpu/drm/xe/xe_hmm.c
 create mode 100644 drivers/gpu/drm/xe/xe_hmm.h

I mostly agree with Matt's review comments on this patch. Some additional below.

Quoted 46 lines
drivers/gpu/drm/xe/Makefile +2 -1
index 840467080e59..29dcbc938b01 100644
@@ -143,7 +143,8 @@ xe-y += xe_bb.o \
  	xe_wait_user_fence.o \
  	xe_wa.o \
  	xe_wopcm.o \
-	xe_svm_devmem.o
+	xe_svm_devmem.o \
+	xe_hmm.o
 
  # graphics hardware monitoring (HWMON) support

 xe-$(CONFIG_HWMON) += xe_hwmon.o

drivers/gpu/drm/xe/xe_hmm.c new file +23 -0
new file mode 100644
index 000000000000..c45c2447d386
@@ -0,0 +1,213 @@
+// SPDX-License-Identifier: MIT
+/*
+ * Copyright © 2024 Intel Corporation
+ */
+
+#include <linux/mmu_notifier.h>
+#include <linux/dma-mapping.h>
+#include <linux/memremap.h>
+#include <linux/swap.h>
+#include <linux/mm.h>
+#include "xe_hmm.h"
+#include "xe_vm.h"
+
+/**
+ * mark_range_accessed() - mark a range is accessed, so core mm
+ * have such information for memory eviction or write back to
+ * hard disk
+ *
+ * @range: the range to mark
+ * @write: if write to this range, we mark pages in this range
+ * as dirty
+ */
+static void mark_range_accessed(struct hmm_range *range, bool
Quoted 1 line

write)

Quoted 7 lines

Some of the static function names aren't really unique enough not to
stand in the way for a future core function name clash. Please consider
using an xe_ prefix in such cases. It will also make backtraces easier
to follow.

Quoted 6 lines

I will add a xe_prefix for the backtrace reason...

As I understand it, static function is file scope, so even if we have a core function with
The same name in the future, as long as they are not in the same file, there wont be any name clash...

The name can't be used for a future core extern function?

/Thomas

Quoted 263 lines
Quoted 9 lines
Quoted 9 lines
drivers/gpu/drm/xe/xe_hmm.c new file +5 -0
+{
+	struct page *page;
+	u64 i, npages;
+
+	npages = ((range->end - 1) >> PAGE_SHIFT) - (range-
Quoted 1 line

start >>

Quoted 9 lines

drivers/gpu/drm/xe/xe_hmm.c line 34

Quoted 7 lines

PAGE_SHIFT) + 1;

drivers/gpu/drm/xe/xe_hmm.c new file +6 -0
+	for (i = 0; i < npages; i++) {
+		page = hmm_pfn_to_page(range->hmm_pfns[i]);
+		if (write) {
+			lock_page(page);
+			set_page_dirty(page);
+			unlock_page(page);

Could be using set_page_dirty_lock() here.

Will fix, Thanks
Oak

Quoted 240 lines

/Thomas

Quoted 236 lines
drivers/gpu/drm/xe/xe_hmm.c new file +7 -0
+		}
+		mark_page_accessed(page);
+	}
+}
+
+/**
+ * build_sg() - build a scatter gather table for all the

physical
pages/pfn
+ * in a hmm_range. dma-address is save in sg table and will be used
to program
+ * GPU page table later.
+ *
+ * @xe: the xe device who will access the dma-address in sg table
+ * @range: the hmm range that we build the sg table from. range-

Quoted 1 line

hmm_pfns[]

+ * has the pfn numbers of pages that back up this hmm address range.
+ * @st: pointer to the sg table.
+ * @write: whether we write to this range. This decides dma map direction
+ * for system pages. If write we map it bi-diretional; otherwise
+ * DMA_TO_DEVICE
+ *
+ * All the contiguous pfns will be collapsed into one entry in
+ * the scatter gather table. This is for the convenience of
+ * later on operations to bind address range to GPU page table.
+ *
+ * The dma_address in the sg table will later be used by GPU to
+ * access memory. So if the memory is system memory, we need to
+ * do a dma-mapping so it can be accessed by GPU/DMA. If the memory
+ * is GPU local memory (of the GPU who is going to access memory),
+ * we need gpu dpa (device physical address), and there is no need
+ * of dma-mapping.
+ *
+ * FIXME: dma-mapping for peer gpu device to access remote gpu's
+ * memory. Add this when you support p2p
+ *
+ * This function allocates the storage of the sg table. It is
+ * caller's responsibility to free it calling sg_free_table.
+ *
+ * Returns 0 if successful; -ENOMEM if fails to allocate memory
+ */
+static int build_sg(struct xe_device *xe, struct hmm_range *range,

drivers/gpu/drm/xe/xe_hmm.c new file +9 -0
+			     struct sg_table *st, bool write)
+{
+	struct device *dev = xe->drm.dev;
+	struct scatterlist *sg;
+	u64 i, npages;
+
+	sg = NULL;
+	st->nents = 0;
+	npages = ((range->end - 1) >> PAGE_SHIFT) - (range-
Quoted 1 line

start >>

PAGE_SHIFT) + 1;

drivers/gpu/drm/xe/xe_hmm.c new file +13 -0
+
+	if (unlikely(sg_alloc_table(st, npages, GFP_KERNEL)))
+		return -ENOMEM;
+
+	for (i = 0; i < npages; i++) {
+		struct page *page;
+		unsigned long addr;
+		struct xe_mem_region *mr;
+
+		page = hmm_pfn_to_page(range->hmm_pfns[i]);
+		if (is_device_private_page(page)) {
+			mr = page_to_mem_region(page);
+			addr = vram_pfn_to_dpa(mr, range-
Quoted 1 line

hmm_pfns[i]);

drivers/gpu/drm/xe/xe_hmm.c new file +2 -0
+		} else {
+			addr = dma_map_page(dev, page, 0,

PAGE_SIZE,

drivers/gpu/drm/xe/xe_hmm.c new file +1 -0
+					write ?

DMA_BIDIRECTIONAL :
DMA_TO_DEVICE);

drivers/gpu/drm/xe/xe_hmm.c new file +3 -0
+		}
+
+		if (sg && (addr == (sg_dma_address(sg) + sg-
Quoted 1 line

length))) {

drivers/gpu/drm/xe/xe_hmm.c new file +18 -0
+			sg->length += PAGE_SIZE;
+			sg_dma_len(sg) += PAGE_SIZE;
+			continue;
+		}
+
+		sg =  sg ? sg_next(sg) : st->sgl;
+		sg_dma_address(sg) = addr;
+		sg_dma_len(sg) = PAGE_SIZE;
+		sg->length = PAGE_SIZE;
+		st->nents++;
+	}
+
+	sg_mark_end(sg);
+	return 0;
+}
+
+/**
+ * xe_hmm_populate_range() - Populate physical pages of a

virtual
+ * address range
+ *
+ * @vma: vma has information of the range to populate. only vma
+ * of userptr and hmmptr type can be populated.
+ * @hmm_range: pointer to hmm_range struct. hmm_rang->hmm_pfns
+ * will hold the populated pfns.
+ * @write: populate pages with write permission
+ *
+ * This function populate the physical pages of a virtual
+ * address range. The populated physical pages is saved in
+ * userptr's sg table. It is similar to get_user_pages but call
+ * hmm_range_fault.
+ *
+ * This function also read mmu notifier sequence # (
+ * mmu_interval_read_begin), for the purpose of later
+ * comparison (through mmu_interval_read_retry).
+ *
+ * This must be called with mmap read or write lock held.
+ *
+ * This function allocates the storage of the userptr sg table.
+ * It is caller's responsibility to free it calling sg_free_table.
+ *
+ * returns: 0 for succuss; negative error no on failure
+ */
+int xe_hmm_populate_range(struct xe_vma *vma, struct hmm_range *hmm_range,

drivers/gpu/drm/xe/xe_hmm.c new file +4 -0
+						bool write)
+{
+	unsigned long timeout =
+		jiffies +

msecs_to_jiffies(HMM_RANGE_DEFAULT_TIMEOUT);

drivers/gpu/drm/xe/xe_hmm.c new file +14 -0
+	unsigned long *pfns, flags = HMM_PFN_REQ_FAULT;
+	struct xe_userptr_vma *userptr_vma;
+	struct xe_userptr *userptr;
+	u64 start = vma->gpuva.va.addr;
+	u64 end = start + vma->gpuva.va.range;
+	struct xe_vm *vm = xe_vma_vm(vma);
+	u64 npages;
+	int ret;
+
+	userptr_vma = to_userptr_vma(vma);
+	userptr = &userptr_vma->userptr;
+	mmap_assert_locked(userptr->notifier.mm);
+
+	npages = ((end - 1) >> PAGE_SHIFT) - (start >>

PAGE_SHIFT) +
1;

drivers/gpu/drm/xe/xe_hmm.c new file +1 -0
+	pfns = kvmalloc_array(npages, sizeof(*pfns),

GFP_KERNEL);

drivers/gpu/drm/xe/xe_hmm.c new file +9 -0
+	if (unlikely(!pfns))
+		return -ENOMEM;
+
+	if (write)
+		flags |= HMM_PFN_REQ_WRITE;
+
+	memset64((u64 *)pfns, (u64)flags, npages);
+	hmm_range->hmm_pfns = pfns;
+	hmm_range->notifier_seq =

mmu_interval_read_begin(&userptr-

Quoted 1 line

notifier);

drivers/gpu/drm/xe/xe_hmm.c new file +4 -0
+	hmm_range->notifier = &userptr->notifier;
+	hmm_range->start = start;
+	hmm_range->end = end;
+	hmm_range->pfn_flags_mask = HMM_PFN_REQ_FAULT |

HMM_PFN_REQ_WRITE;

drivers/gpu/drm/xe/xe_hmm.c new file +3 -0
+	/**
+	 * FIXME:
+	 * Set the the dev_private_owner can prevent

hmm_range_fault
to fault

drivers/gpu/drm/xe/xe_hmm.c new file +1 -0
+	 * in the device private pages owned by caller. See

function

drivers/gpu/drm/xe/xe_hmm.c new file +1 -0
+	 * hmm_vma_handle_pte. In multiple GPU case, this should

be
set to the

drivers/gpu/drm/xe/xe_hmm.c new file +1 -0
+	 * device owner of the best migration destination. e.g.,

device0/vm0

drivers/gpu/drm/xe/xe_hmm.c new file +1 -0
+	 * has a page fault, but we have determined the best

placement of

drivers/gpu/drm/xe/xe_hmm.c new file +1 -0
+	 * the fault address should be on device1, we should set

below to

drivers/gpu/drm/xe/xe_hmm.c new file +30 -0
+	 * device1 instead of device0.
+	 */
+	hmm_range->dev_private_owner = vm->xe->drm.dev;
+
+	while (true) {
+		ret = hmm_range_fault(hmm_range);
+		if (time_after(jiffies, timeout))
+			break;
+
+		if (ret == -EBUSY)
+			continue;
+		break;
+	}
+
+	if (ret)
+		goto free_pfns;
+
+	ret = build_sg(vm->xe, hmm_range, &userptr->sgt, write);
+	if (ret)
+		goto free_pfns;
+
+	mark_range_accessed(hmm_range, write);
+	userptr->sg = &userptr->sgt;
+	userptr->notifier_seq = hmm_range->notifier_seq;
+
+free_pfns:
+	kvfree(pfns);
+	return ret;
+}
+
drivers/gpu/drm/xe/xe_hmm.h new file +11 -0
new file mode 100644
index 000000000000..960f3f6d36ae
@@ -0,0 +1,12 @@
+// SPDX-License-Identifier: MIT
+/*
+ * Copyright © 2024 Intel Corporation
+ */
+
+#include <linux/types.h>
+#include <linux/hmm.h>
+#include "xe_vm_types.h"
+#include "xe_svm.h"
+
+int xe_hmm_populate_range(struct xe_vma *vma, struct hmm_range

*hmm_range,

drivers/gpu/drm/xe/xe_hmm.h new file +1 -0
+						bool write);
author
#36 lore raw

RE: [PATCH 4/5] drm/xe: Helper to populate a userptr or hmmptr

Quoted 125 lines

-----Original Message-----
From: Hellstrom, Thomas <[email protected]>
Sent: Tuesday, March 19, 2024 4:42 AM
To: [email protected]; Zeng, Oak <[email protected]>
Cc: Brost, Matthew <[email protected]>; Welty, Brian <[email protected]>; [email protected]; Ghimiray, Himal Prasad <[email protected]>
Subject: Re: [PATCH 4/5] drm/xe: Helper to populate a userptr or hmmptr

Quoted 113 lines · Oak
Quoted 112 lines

-----Original Message-----
From: Hellstrom, Thomas <[email protected]>
Sent: Monday, March 18, 2024 7:54 AM
To: [email protected]; Zeng, Oak <[email protected]>
Cc: Brost, Matthew <[email protected]>; Welty, Brian <[email protected]>; [email protected]; Ghimiray, Himal Prasad <[email protected]>
Subject: Re: [PATCH 4/5] drm/xe: Helper to populate a userptr or hmmptr

Hi, Oak.

Quoted 34 lines · Oak Zeng

Add a helper function xe_hmm_populate_range to populate a a userptr or hmmptr range. This functions calls hmm_range_fault to read CPU page tables and populate all pfns/pages of this virtual address range.

If the populated page is system memory page, dma-mapping is performed
to get a dma-address which can be used later for GPU to access pages.

If the populated page is device private page, we calculate the dpa (
device physical address) of the page.

The dma-address or dpa is then saved in userptr's sg table. This is
prepare work to replace the get_user_pages_fast code in userptr code
path. The helper function will also be used to populate hmmptr later.

Co-developed-by Niranjana Vishwanathapura

<[email protected]>
Signed-off-by: Niranjana Vishwanathapura
<[email protected]>
Cc: Matthew Brost <[email protected]>
Cc: Thomas Hellström <[email protected]>
Cc: Brian Welty <[email protected]>
---
 drivers/gpu/drm/xe/Makefile |   3 +-
 drivers/gpu/drm/xe/xe_hmm.c | 213
++++++++++++++++++++++++++++++++++++
 drivers/gpu/drm/xe/xe_hmm.h |  12 ++

Quoted 3 lines
 3 files changed, 227 insertions(+), 1 deletion(-)

 create mode 100644 drivers/gpu/drm/xe/xe_hmm.c
 create mode 100644 drivers/gpu/drm/xe/xe_hmm.h

I mostly agree with Matt's review comments on this patch. Some additional below.

Quoted 46 lines
drivers/gpu/drm/xe/Makefile +2 -1
index 840467080e59..29dcbc938b01 100644
@@ -143,7 +143,8 @@ xe-y += xe_bb.o \
  	xe_wait_user_fence.o \
  	xe_wa.o \
  	xe_wopcm.o \
-	xe_svm_devmem.o
+	xe_svm_devmem.o \
+	xe_hmm.o
 
  # graphics hardware monitoring (HWMON) support

 xe-$(CONFIG_HWMON) += xe_hwmon.o

drivers/gpu/drm/xe/xe_hmm.c new file +23 -0
new file mode 100644
index 000000000000..c45c2447d386
@@ -0,0 +1,213 @@
+// SPDX-License-Identifier: MIT
+/*
+ * Copyright © 2024 Intel Corporation
+ */
+
+#include <linux/mmu_notifier.h>
+#include <linux/dma-mapping.h>
+#include <linux/memremap.h>
+#include <linux/swap.h>
+#include <linux/mm.h>
+#include "xe_hmm.h"
+#include "xe_vm.h"
+
+/**
+ * mark_range_accessed() - mark a range is accessed, so core mm
+ * have such information for memory eviction or write back to
+ * hard disk
+ *
+ * @range: the range to mark
+ * @write: if write to this range, we mark pages in this range
+ * as dirty
+ */
+static void mark_range_accessed(struct hmm_range *range, bool
Quoted 1 line

write)

Some of the static function names aren't really unique enough not to
stand in the way for a future core function name clash. Please consider
using an xe_ prefix in such cases. It will also make backtraces easier
to follow.

Quoted 8 lines
Quoted 6 lines

I will add a xe_prefix for the backtrace reason...

As I understand it, static function is file scope, so even if we have a core function with
The same name in the future, as long as they are not in the same file, there wont be any name clash...

The name can't be used for a future core extern function?

Yes, at least this is my understanding of static function. Static function doesn't have a symbol so it can't clash with other static function in other file with the same name. For example, if you readelf -s xe.ko, you won't be able to find xe_mark_range_accessed, because it is a static function. I think compiler just give static function an address instead of a symbol.

Oak

Quoted 266 lines

/Thomas

Quoted 263 lines
Quoted 9 lines
Quoted 9 lines
drivers/gpu/drm/xe/xe_hmm.c new file +5 -0
+{
+	struct page *page;
+	u64 i, npages;
+
+	npages = ((range->end - 1) >> PAGE_SHIFT) - (range-
Quoted 1 line

start >>

Quoted 9 lines

drivers/gpu/drm/xe/xe_hmm.c line 34

Quoted 7 lines

PAGE_SHIFT) + 1;

drivers/gpu/drm/xe/xe_hmm.c new file +6 -0
+	for (i = 0; i < npages; i++) {
+		page = hmm_pfn_to_page(range->hmm_pfns[i]);
+		if (write) {
+			lock_page(page);
+			set_page_dirty(page);
+			unlock_page(page);

Could be using set_page_dirty_lock() here.

Will fix, Thanks
Oak

Quoted 240 lines

/Thomas

Quoted 236 lines
drivers/gpu/drm/xe/xe_hmm.c new file +7 -0
+		}
+		mark_page_accessed(page);
+	}
+}
+
+/**
+ * build_sg() - build a scatter gather table for all the

physical
pages/pfn
+ * in a hmm_range. dma-address is save in sg table and will be used
to program
+ * GPU page table later.
+ *
+ * @xe: the xe device who will access the dma-address in sg table
+ * @range: the hmm range that we build the sg table from. range-

Quoted 1 line

hmm_pfns[]

+ * has the pfn numbers of pages that back up this hmm address range.
+ * @st: pointer to the sg table.
+ * @write: whether we write to this range. This decides dma map direction
+ * for system pages. If write we map it bi-diretional; otherwise
+ * DMA_TO_DEVICE
+ *
+ * All the contiguous pfns will be collapsed into one entry in
+ * the scatter gather table. This is for the convenience of
+ * later on operations to bind address range to GPU page table.
+ *
+ * The dma_address in the sg table will later be used by GPU to
+ * access memory. So if the memory is system memory, we need to
+ * do a dma-mapping so it can be accessed by GPU/DMA. If the memory
+ * is GPU local memory (of the GPU who is going to access memory),
+ * we need gpu dpa (device physical address), and there is no need
+ * of dma-mapping.
+ *
+ * FIXME: dma-mapping for peer gpu device to access remote gpu's
+ * memory. Add this when you support p2p
+ *
+ * This function allocates the storage of the sg table. It is
+ * caller's responsibility to free it calling sg_free_table.
+ *
+ * Returns 0 if successful; -ENOMEM if fails to allocate memory
+ */
+static int build_sg(struct xe_device *xe, struct hmm_range *range,

drivers/gpu/drm/xe/xe_hmm.c new file +9 -0
+			     struct sg_table *st, bool write)
+{
+	struct device *dev = xe->drm.dev;
+	struct scatterlist *sg;
+	u64 i, npages;
+
+	sg = NULL;
+	st->nents = 0;
+	npages = ((range->end - 1) >> PAGE_SHIFT) - (range-
Quoted 1 line

start >>

PAGE_SHIFT) + 1;

drivers/gpu/drm/xe/xe_hmm.c new file +13 -0
+
+	if (unlikely(sg_alloc_table(st, npages, GFP_KERNEL)))
+		return -ENOMEM;
+
+	for (i = 0; i < npages; i++) {
+		struct page *page;
+		unsigned long addr;
+		struct xe_mem_region *mr;
+
+		page = hmm_pfn_to_page(range->hmm_pfns[i]);
+		if (is_device_private_page(page)) {
+			mr = page_to_mem_region(page);
+			addr = vram_pfn_to_dpa(mr, range-
Quoted 1 line

hmm_pfns[i]);

drivers/gpu/drm/xe/xe_hmm.c new file +2 -0
+		} else {
+			addr = dma_map_page(dev, page, 0,

PAGE_SIZE,

drivers/gpu/drm/xe/xe_hmm.c new file +1 -0
+					write ?

DMA_BIDIRECTIONAL :
DMA_TO_DEVICE);

drivers/gpu/drm/xe/xe_hmm.c new file +3 -0
+		}
+
+		if (sg && (addr == (sg_dma_address(sg) + sg-
Quoted 1 line

length))) {

drivers/gpu/drm/xe/xe_hmm.c new file +18 -0
+			sg->length += PAGE_SIZE;
+			sg_dma_len(sg) += PAGE_SIZE;
+			continue;
+		}
+
+		sg =  sg ? sg_next(sg) : st->sgl;
+		sg_dma_address(sg) = addr;
+		sg_dma_len(sg) = PAGE_SIZE;
+		sg->length = PAGE_SIZE;
+		st->nents++;
+	}
+
+	sg_mark_end(sg);
+	return 0;
+}
+
+/**
+ * xe_hmm_populate_range() - Populate physical pages of a

virtual
+ * address range
+ *
+ * @vma: vma has information of the range to populate. only vma
+ * of userptr and hmmptr type can be populated.
+ * @hmm_range: pointer to hmm_range struct. hmm_rang->hmm_pfns
+ * will hold the populated pfns.
+ * @write: populate pages with write permission
+ *
+ * This function populate the physical pages of a virtual
+ * address range. The populated physical pages is saved in
+ * userptr's sg table. It is similar to get_user_pages but call
+ * hmm_range_fault.
+ *
+ * This function also read mmu notifier sequence # (
+ * mmu_interval_read_begin), for the purpose of later
+ * comparison (through mmu_interval_read_retry).
+ *
+ * This must be called with mmap read or write lock held.
+ *
+ * This function allocates the storage of the userptr sg table.
+ * It is caller's responsibility to free it calling sg_free_table.
+ *
+ * returns: 0 for succuss; negative error no on failure
+ */
+int xe_hmm_populate_range(struct xe_vma *vma, struct hmm_range *hmm_range,

drivers/gpu/drm/xe/xe_hmm.c new file +4 -0
+						bool write)
+{
+	unsigned long timeout =
+		jiffies +

msecs_to_jiffies(HMM_RANGE_DEFAULT_TIMEOUT);

drivers/gpu/drm/xe/xe_hmm.c new file +14 -0
+	unsigned long *pfns, flags = HMM_PFN_REQ_FAULT;
+	struct xe_userptr_vma *userptr_vma;
+	struct xe_userptr *userptr;
+	u64 start = vma->gpuva.va.addr;
+	u64 end = start + vma->gpuva.va.range;
+	struct xe_vm *vm = xe_vma_vm(vma);
+	u64 npages;
+	int ret;
+
+	userptr_vma = to_userptr_vma(vma);
+	userptr = &userptr_vma->userptr;
+	mmap_assert_locked(userptr->notifier.mm);
+
+	npages = ((end - 1) >> PAGE_SHIFT) - (start >>

PAGE_SHIFT) +
1;

drivers/gpu/drm/xe/xe_hmm.c new file +1 -0
+	pfns = kvmalloc_array(npages, sizeof(*pfns),

GFP_KERNEL);

drivers/gpu/drm/xe/xe_hmm.c new file +9 -0
+	if (unlikely(!pfns))
+		return -ENOMEM;
+
+	if (write)
+		flags |= HMM_PFN_REQ_WRITE;
+
+	memset64((u64 *)pfns, (u64)flags, npages);
+	hmm_range->hmm_pfns = pfns;
+	hmm_range->notifier_seq =

mmu_interval_read_begin(&userptr-

Quoted 1 line

notifier);

drivers/gpu/drm/xe/xe_hmm.c new file +4 -0
+	hmm_range->notifier = &userptr->notifier;
+	hmm_range->start = start;
+	hmm_range->end = end;
+	hmm_range->pfn_flags_mask = HMM_PFN_REQ_FAULT |

HMM_PFN_REQ_WRITE;

drivers/gpu/drm/xe/xe_hmm.c new file +3 -0
+	/**
+	 * FIXME:
+	 * Set the the dev_private_owner can prevent

hmm_range_fault
to fault

drivers/gpu/drm/xe/xe_hmm.c new file +1 -0
+	 * in the device private pages owned by caller. See

function

drivers/gpu/drm/xe/xe_hmm.c new file +1 -0
+	 * hmm_vma_handle_pte. In multiple GPU case, this should

be
set to the

drivers/gpu/drm/xe/xe_hmm.c new file +1 -0
+	 * device owner of the best migration destination. e.g.,

device0/vm0

drivers/gpu/drm/xe/xe_hmm.c new file +1 -0
+	 * has a page fault, but we have determined the best

placement of

drivers/gpu/drm/xe/xe_hmm.c new file +1 -0
+	 * the fault address should be on device1, we should set

below to

drivers/gpu/drm/xe/xe_hmm.c new file +30 -0
+	 * device1 instead of device0.
+	 */
+	hmm_range->dev_private_owner = vm->xe->drm.dev;
+
+	while (true) {
+		ret = hmm_range_fault(hmm_range);
+		if (time_after(jiffies, timeout))
+			break;
+
+		if (ret == -EBUSY)
+			continue;
+		break;
+	}
+
+	if (ret)
+		goto free_pfns;
+
+	ret = build_sg(vm->xe, hmm_range, &userptr->sgt, write);
+	if (ret)
+		goto free_pfns;
+
+	mark_range_accessed(hmm_range, write);
+	userptr->sg = &userptr->sgt;
+	userptr->notifier_seq = hmm_range->notifier_seq;
+
+free_pfns:
+	kvfree(pfns);
+	return ret;
+}
+
drivers/gpu/drm/xe/xe_hmm.h new file +11 -0
new file mode 100644
index 000000000000..960f3f6d36ae
@@ -0,0 +1,12 @@
+// SPDX-License-Identifier: MIT
+/*
+ * Copyright © 2024 Intel Corporation
+ */
+
+#include <linux/types.h>
+#include <linux/hmm.h>
+#include "xe_vm_types.h"
+#include "xe_svm.h"
+
+int xe_hmm_populate_range(struct xe_vma *vma, struct hmm_range

*hmm_range,

drivers/gpu/drm/xe/xe_hmm.h new file +1 -0
+						bool write);
#37 lore raw

Re: [PATCH 4/5] drm/xe: Helper to populate a userptr or hmmptr

Quoted 148 lines · Oak
Quoted 148 lines

-----Original Message-----
From: Hellstrom, Thomas <[email protected]>
Sent: Tuesday, March 19, 2024 4:42 AM
To: [email protected]; Zeng, Oak <[email protected]>
Cc: Brost, Matthew <[email protected]>; Welty, Brian <[email protected]>; [email protected]; Ghimiray, Himal Prasad <[email protected]>
Subject: Re: [PATCH 4/5] drm/xe: Helper to populate a userptr or hmmptr

Quoted 134 lines · Oak
Quoted 118 lines

-----Original Message-----
From: Hellstrom, Thomas <[email protected]>
Sent: Monday, March 18, 2024 7:54 AM
To: [email protected]; Zeng, Oak <[email protected]>
Cc: Brost, Matthew <[email protected]>; Welty, Brian <[email protected]>; [email protected]; Ghimiray, Himal Prasad
<[email protected]>
Subject: Re: [PATCH 4/5] drm/xe: Helper to populate a userptr or
hmmptr

Hi, Oak.

Quoted 40 lines · Oak Zeng

Add a helper function xe_hmm_populate_range to populate a a userptr or hmmptr range. This functions calls hmm_range_fault
to read CPU page tables and populate all pfns/pages of this virtual address range.

If the populated page is system memory page, dma-mapping is performed
to get a dma-address which can be used later for GPU to access
pages.

If the populated page is device private page, we calculate the
dpa (
device physical address) of the page.

The dma-address or dpa is then saved in userptr's sg table. This
is
prepare work to replace the get_user_pages_fast code in userptr
code
path. The helper function will also be used to populate hmmptr
later.

Co-developed-by Niranjana Vishwanathapura

<[email protected]>
Signed-off-by: Niranjana Vishwanathapura <[email protected]>
Cc: Matthew Brost <[email protected]>
Cc: Thomas Hellström <[email protected]>
Cc: Brian Welty <[email protected]>
---
 drivers/gpu/drm/xe/Makefile |   3 +- drivers/gpu/drm/xe/xe_hmm.c | 213
++++++++++++++++++++++++++++++++++++
 drivers/gpu/drm/xe/xe_hmm.h |  12 ++

Quoted 3 lines
 3 files changed, 227 insertions(+), 1 deletion(-)

 create mode 100644 drivers/gpu/drm/xe/xe_hmm.c create mode 100644 drivers/gpu/drm/xe/xe_hmm.h

I mostly agree with Matt's review comments on this patch. Some additional below.

Quoted 43 lines
drivers/gpu/drm/xe/Makefile +2 -1
index 840467080e59..29dcbc938b01 100644
@@ -143,7 +143,8 @@ xe-y += xe_bb.o \
  	xe_wait_user_fence.o \
  	xe_wa.o \
  	xe_wopcm.o \
-	xe_svm_devmem.o
+	xe_svm_devmem.o \
+	xe_hmm.o
 
  # graphics hardware monitoring (HWMON) support

 xe-$(CONFIG_HWMON) += xe_hwmon.o

drivers/gpu/drm/xe/xe_hmm.c new file +15 -0
new file mode 100644
index 000000000000..c45c2447d386
@@ -0,0 +1,213 @@
+// SPDX-License-Identifier: MIT
+/*
+ * Copyright © 2024 Intel Corporation
+ */
+
+#include <linux/mmu_notifier.h>
+#include <linux/dma-mapping.h>
+#include <linux/memremap.h>
+#include <linux/swap.h>
+#include <linux/mm.h>
+#include "xe_hmm.h"
+#include "xe_vm.h"
+
+/**
+ * mark_range_accessed() - mark a range is accessed, so core

mm
+ * have such information for memory eviction or write back to
+ * hard disk
+ *

Quoted 8 lines

+ * @range: the range to mark
+ * @write: if write to this range, we mark pages in this range
+ * as dirty
+ */
+static void mark_range_accessed(struct hmm_range *range, bool
write)

Quoted 8 lines

Some of the static function names aren't really unique enough not
to
stand in the way for a future core function name clash. Please consider
using an xe_ prefix in such cases. It will also make backtraces easier
to follow.

I will add a xe_prefix for the backtrace reason...

As I understand it, static function is file scope, so even if we have
a core function with
The same name in the future, as long as they are not in the same file, there wont be any name clash...

Quoted 8 lines
Quoted 1 line

The name can't be used for a future core extern function?

Yes, at least this is my understanding of static function. Static function doesn't have a symbol so it can't clash with other static function in other file with the same name. For example, if you readelf -s xe.ko, you won't be able to find xe_mark_range_accessed, because it is a static function. I think compiler just give static function an address instead of a symbol.

I meant for example if you call a function "build_sg". Then if the core wants to implement an extern function called "build_sg" and includes it in a reasonably wide-used header you will get a compilation failure. Hence the use of non-specific static function names restricts future core use of the same name for public functions.

/Thomas

Quoted 295 lines

Oak

Quoted 291 lines

/Thomas

Quoted 288 lines
Quoted 9 lines
Quoted 9 lines
drivers/gpu/drm/xe/xe_hmm.c new file +5 -0
+{
+	struct page *page;
+	u64 i, npages;
+
+	npages = ((range->end - 1) >> PAGE_SHIFT) - (range-
Quoted 1 line

start >>

Quoted 9 lines

drivers/gpu/drm/xe/xe_hmm.c line 34

Quoted 7 lines

PAGE_SHIFT) + 1;

drivers/gpu/drm/xe/xe_hmm.c new file +6 -0
+	for (i = 0; i < npages; i++) {
+		page = hmm_pfn_to_page(range->hmm_pfns[i]);
+		if (write) {
+			lock_page(page);
+			set_page_dirty(page);
+			unlock_page(page);

Could be using set_page_dirty_lock() here.

Will fix, Thanks
Oak

Quoted 265 lines

/Thomas

Quoted 261 lines
drivers/gpu/drm/xe/xe_hmm.c new file +7 -0
+		}
+		mark_page_accessed(page);
+	}
+}
+
+/**
+ * build_sg() - build a scatter gather table for all the

physical
pages/pfn
+ * in a hmm_range. dma-address is save in sg table and will be
used
to program
+ * GPU page table later.
+ *
+ * @xe: the xe device who will access the dma-address in sg table
+ * @range: the hmm range that we build the sg table from. range-

Quoted 1 line

hmm_pfns[]

+ * has the pfn numbers of pages that back up this hmm address
range.
+ * @st: pointer to the sg table.
+ * @write: whether we write to this range. This decides dma map
direction
+ * for system pages. If write we map it bi-diretional; otherwise
+ * DMA_TO_DEVICE
+ *
+ * All the contiguous pfns will be collapsed into one entry in
+ * the scatter gather table. This is for the convenience of
+ * later on operations to bind address range to GPU page table.
+ *
+ * The dma_address in the sg table will later be used by GPU to
+ * access memory. So if the memory is system memory, we need to
+ * do a dma-mapping so it can be accessed by GPU/DMA. If the memory
+ * is GPU local memory (of the GPU who is going to access memory),
+ * we need gpu dpa (device physical address), and there is no
need
+ * of dma-mapping.
+ *
+ * FIXME: dma-mapping for peer gpu device to access remote gpu's
+ * memory. Add this when you support p2p
+ *
+ * This function allocates the storage of the sg table. It is
+ * caller's responsibility to free it calling sg_free_table.
+ *
+ * Returns 0 if successful; -ENOMEM if fails to allocate memory
+ */
+static int build_sg(struct xe_device *xe, struct hmm_range *range,

drivers/gpu/drm/xe/xe_hmm.c new file +1 -0
+			     struct sg_table *st, bool

write)

drivers/gpu/drm/xe/xe_hmm.c new file +8 -0
+{
+	struct device *dev = xe->drm.dev;
+	struct scatterlist *sg;
+	u64 i, npages;
+
+	sg = NULL;
+	st->nents = 0;
+	npages = ((range->end - 1) >> PAGE_SHIFT) - (range-
Quoted 1 line

start >>

PAGE_SHIFT) + 1;

drivers/gpu/drm/xe/xe_hmm.c new file +2 -0
+
+	if (unlikely(sg_alloc_table(st, npages,

GFP_KERNEL)))

drivers/gpu/drm/xe/xe_hmm.c new file +11 -0
+		return -ENOMEM;
+
+	for (i = 0; i < npages; i++) {
+		struct page *page;
+		unsigned long addr;
+		struct xe_mem_region *mr;
+
+		page = hmm_pfn_to_page(range->hmm_pfns[i]);
+		if (is_device_private_page(page)) {
+			mr = page_to_mem_region(page);
+			addr = vram_pfn_to_dpa(mr, range-
Quoted 1 line

hmm_pfns[i]);

drivers/gpu/drm/xe/xe_hmm.c new file +2 -0
+		} else {
+			addr = dma_map_page(dev, page, 0,

PAGE_SIZE,

drivers/gpu/drm/xe/xe_hmm.c new file +1 -0
+					write ?

DMA_BIDIRECTIONAL :
DMA_TO_DEVICE);

drivers/gpu/drm/xe/xe_hmm.c new file +3 -0
+		}
+
+		if (sg && (addr == (sg_dma_address(sg) + sg-
Quoted 1 line

length))) {

drivers/gpu/drm/xe/xe_hmm.c new file +18 -0
+			sg->length += PAGE_SIZE;
+			sg_dma_len(sg) += PAGE_SIZE;
+			continue;
+		}
+
+		sg =  sg ? sg_next(sg) : st->sgl;
+		sg_dma_address(sg) = addr;
+		sg_dma_len(sg) = PAGE_SIZE;
+		sg->length = PAGE_SIZE;
+		st->nents++;
+	}
+
+	sg_mark_end(sg);
+	return 0;
+}
+
+/**
+ * xe_hmm_populate_range() - Populate physical pages of a

virtual
+ * address range
+ *
+ * @vma: vma has information of the range to populate. only vma
+ * of userptr and hmmptr type can be populated.
+ * @hmm_range: pointer to hmm_range struct. hmm_rang-

Quoted 1 line

hmm_pfns

+ * will hold the populated pfns.
+ * @write: populate pages with write permission
+ *
+ * This function populate the physical pages of a virtual
+ * address range. The populated physical pages is saved in
+ * userptr's sg table. It is similar to get_user_pages but call
+ * hmm_range_fault.
+ *
+ * This function also read mmu notifier sequence # (
+ * mmu_interval_read_begin), for the purpose of later
+ * comparison (through mmu_interval_read_retry).
+ *
+ * This must be called with mmap read or write lock held.
+ *
+ * This function allocates the storage of the userptr sg table.
+ * It is caller's responsibility to free it calling sg_free_table.
+ *
+ * returns: 0 for succuss; negative error no on failure
+ */
+int xe_hmm_populate_range(struct xe_vma *vma, struct hmm_range
*hmm_range,

drivers/gpu/drm/xe/xe_hmm.c new file +4 -0
+						bool write)
+{
+	unsigned long timeout =
+		jiffies +

msecs_to_jiffies(HMM_RANGE_DEFAULT_TIMEOUT);

drivers/gpu/drm/xe/xe_hmm.c new file +14 -0
+	unsigned long *pfns, flags = HMM_PFN_REQ_FAULT;
+	struct xe_userptr_vma *userptr_vma;
+	struct xe_userptr *userptr;
+	u64 start = vma->gpuva.va.addr;
+	u64 end = start + vma->gpuva.va.range;
+	struct xe_vm *vm = xe_vma_vm(vma);
+	u64 npages;
+	int ret;
+
+	userptr_vma = to_userptr_vma(vma);
+	userptr = &userptr_vma->userptr;
+	mmap_assert_locked(userptr->notifier.mm);
+
+	npages = ((end - 1) >> PAGE_SHIFT) - (start >>

PAGE_SHIFT) +
1;

drivers/gpu/drm/xe/xe_hmm.c new file +1 -0
+	pfns = kvmalloc_array(npages, sizeof(*pfns),

GFP_KERNEL);

drivers/gpu/drm/xe/xe_hmm.c new file +9 -0
+	if (unlikely(!pfns))
+		return -ENOMEM;
+
+	if (write)
+		flags |= HMM_PFN_REQ_WRITE;
+
+	memset64((u64 *)pfns, (u64)flags, npages);
+	hmm_range->hmm_pfns = pfns;
+	hmm_range->notifier_seq =

mmu_interval_read_begin(&userptr-

Quoted 1 line

notifier);

drivers/gpu/drm/xe/xe_hmm.c new file +4 -0
+	hmm_range->notifier = &userptr->notifier;
+	hmm_range->start = start;
+	hmm_range->end = end;
+	hmm_range->pfn_flags_mask = HMM_PFN_REQ_FAULT |

HMM_PFN_REQ_WRITE;

drivers/gpu/drm/xe/xe_hmm.c new file +3 -0
+	/**
+	 * FIXME:
+	 * Set the the dev_private_owner can prevent

hmm_range_fault
to fault

drivers/gpu/drm/xe/xe_hmm.c new file +1 -0
+	 * in the device private pages owned by caller. See

function

drivers/gpu/drm/xe/xe_hmm.c new file +1 -0
+	 * hmm_vma_handle_pte. In multiple GPU case, this

should
be
set to the

drivers/gpu/drm/xe/xe_hmm.c new file +1 -0
+	 * device owner of the best migration destination.

e.g.,
device0/vm0

drivers/gpu/drm/xe/xe_hmm.c new file +1 -0
+	 * has a page fault, but we have determined the best

placement of

drivers/gpu/drm/xe/xe_hmm.c new file +1 -0
+	 * the fault address should be on device1, we should

set
below to

drivers/gpu/drm/xe/xe_hmm.c new file +18 -0
+	 * device1 instead of device0.
+	 */
+	hmm_range->dev_private_owner = vm->xe->drm.dev;
+
+	while (true) {
+		ret = hmm_range_fault(hmm_range);
+		if (time_after(jiffies, timeout))
+			break;
+
+		if (ret == -EBUSY)
+			continue;
+		break;
+	}
+
+	if (ret)
+		goto free_pfns;
+
+	ret = build_sg(vm->xe, hmm_range, &userptr->sgt,

write);

drivers/gpu/drm/xe/xe_hmm.c new file +12 -0
+	if (ret)
+		goto free_pfns;
+
+	mark_range_accessed(hmm_range, write);
+	userptr->sg = &userptr->sgt;
+	userptr->notifier_seq = hmm_range->notifier_seq;
+
+free_pfns:
+	kvfree(pfns);
+	return ret;
+}
+
drivers/gpu/drm/xe/xe_hmm.h new file +11 -0
new file mode 100644
index 000000000000..960f3f6d36ae
@@ -0,0 +1,12 @@
+// SPDX-License-Identifier: MIT
+/*
+ * Copyright © 2024 Intel Corporation
+ */
+
+#include <linux/types.h>
+#include <linux/hmm.h>
+#include "xe_vm_types.h"
+#include "xe_svm.h"
+
+int xe_hmm_populate_range(struct xe_vma *vma, struct

hmm_range
*hmm_range,

drivers/gpu/drm/xe/xe_hmm.h new file +1 -0
+						bool write);
author
#38 lore raw

RE: [PATCH 4/5] drm/xe: Helper to populate a userptr or hmmptr

Hi Thomas,

Quoted 167 lines

-----Original Message-----
From: Hellstrom, Thomas <[email protected]>
Sent: Tuesday, March 19, 2024 3:52 PM
To: [email protected]; Zeng, Oak <[email protected]>
Cc: Brost, Matthew <[email protected]>; Welty, Brian <[email protected]>; [email protected]; Ghimiray, Himal Prasad <[email protected]>
Subject: Re: [PATCH 4/5] drm/xe: Helper to populate a userptr or hmmptr

Quoted 148 lines · Oak
Quoted 148 lines

-----Original Message-----
From: Hellstrom, Thomas <[email protected]>
Sent: Tuesday, March 19, 2024 4:42 AM
To: [email protected]; Zeng, Oak <[email protected]>
Cc: Brost, Matthew <[email protected]>; Welty, Brian <[email protected]>; [email protected]; Ghimiray, Himal Prasad <[email protected]>
Subject: Re: [PATCH 4/5] drm/xe: Helper to populate a userptr or hmmptr

Quoted 134 lines · Oak
Quoted 118 lines

-----Original Message-----
From: Hellstrom, Thomas <[email protected]>
Sent: Monday, March 18, 2024 7:54 AM
To: [email protected]; Zeng, Oak <[email protected]>
Cc: Brost, Matthew <[email protected]>; Welty, Brian <[email protected]>; [email protected]; Ghimiray, Himal Prasad
<[email protected]>
Subject: Re: [PATCH 4/5] drm/xe: Helper to populate a userptr or
hmmptr

Hi, Oak.

Quoted 40 lines · Oak Zeng

Add a helper function xe_hmm_populate_range to populate a a userptr or hmmptr range. This functions calls hmm_range_fault
to read CPU page tables and populate all pfns/pages of this virtual address range.

If the populated page is system memory page, dma-mapping is performed
to get a dma-address which can be used later for GPU to access
pages.

If the populated page is device private page, we calculate the
dpa (
device physical address) of the page.

The dma-address or dpa is then saved in userptr's sg table. This
is
prepare work to replace the get_user_pages_fast code in userptr
code
path. The helper function will also be used to populate hmmptr
later.

Co-developed-by Niranjana Vishwanathapura

<[email protected]>
Signed-off-by: Niranjana Vishwanathapura <[email protected]>
Cc: Matthew Brost <[email protected]>
Cc: Thomas Hellström <[email protected]>
Cc: Brian Welty <[email protected]>
---
 drivers/gpu/drm/xe/Makefile |   3 +- drivers/gpu/drm/xe/xe_hmm.c | 213
++++++++++++++++++++++++++++++++++++
 drivers/gpu/drm/xe/xe_hmm.h |  12 ++

Quoted 3 lines
 3 files changed, 227 insertions(+), 1 deletion(-)

 create mode 100644 drivers/gpu/drm/xe/xe_hmm.c create mode 100644 drivers/gpu/drm/xe/xe_hmm.h

I mostly agree with Matt's review comments on this patch. Some additional below.

Quoted 43 lines
drivers/gpu/drm/xe/Makefile +2 -1
index 840467080e59..29dcbc938b01 100644
@@ -143,7 +143,8 @@ xe-y += xe_bb.o \
  	xe_wait_user_fence.o \
  	xe_wa.o \
  	xe_wopcm.o \
-	xe_svm_devmem.o
+	xe_svm_devmem.o \
+	xe_hmm.o
 
  # graphics hardware monitoring (HWMON) support

 xe-$(CONFIG_HWMON) += xe_hwmon.o

drivers/gpu/drm/xe/xe_hmm.c new file +15 -0
new file mode 100644
index 000000000000..c45c2447d386
@@ -0,0 +1,213 @@
+// SPDX-License-Identifier: MIT
+/*
+ * Copyright © 2024 Intel Corporation
+ */
+
+#include <linux/mmu_notifier.h>
+#include <linux/dma-mapping.h>
+#include <linux/memremap.h>
+#include <linux/swap.h>
+#include <linux/mm.h>
+#include "xe_hmm.h"
+#include "xe_vm.h"
+
+/**
+ * mark_range_accessed() - mark a range is accessed, so core

mm
+ * have such information for memory eviction or write back to
+ * hard disk
+ *

Quoted 8 lines

+ * @range: the range to mark
+ * @write: if write to this range, we mark pages in this range
+ * as dirty
+ */
+static void mark_range_accessed(struct hmm_range *range, bool
write)

Quoted 8 lines

Some of the static function names aren't really unique enough not
to
stand in the way for a future core function name clash. Please consider
using an xe_ prefix in such cases. It will also make backtraces easier
to follow.

I will add a xe_prefix for the backtrace reason...

As I understand it, static function is file scope, so even if we have
a core function with
The same name in the future, as long as they are not in the same file, there wont be any name clash...

Quoted 8 lines
Quoted 1 line

The name can't be used for a future core extern function?

Yes, at least this is my understanding of static function. Static function doesn't have a symbol so it can't clash with other static function in other file with the same name. For example, if you readelf -s xe.ko, you won't be able to find xe_mark_range_accessed, because it is a static function. I think compiler just give static function an address instead of a symbol.

Quoted 5 lines

I meant for example if you call a function "build_sg". Then if the core wants to implement an extern function called "build_sg" and includes it in a reasonably wide-used header you will get a compilation failure. Hence the use of non-specific static function names restricts future core use of the same name for public functions.

I see what you meant now 😊. Make sense. Will add a prefix to the functions

Oak

Quoted 301 lines

/Thomas

Quoted 295 lines

Oak

Quoted 291 lines

/Thomas

Quoted 288 lines
Quoted 9 lines
Quoted 9 lines
drivers/gpu/drm/xe/xe_hmm.c new file +5 -0
+{
+	struct page *page;
+	u64 i, npages;
+
+	npages = ((range->end - 1) >> PAGE_SHIFT) - (range-
Quoted 1 line

start >>

Quoted 9 lines

drivers/gpu/drm/xe/xe_hmm.c line 34

Quoted 7 lines

PAGE_SHIFT) + 1;

drivers/gpu/drm/xe/xe_hmm.c new file +6 -0
+	for (i = 0; i < npages; i++) {
+		page = hmm_pfn_to_page(range->hmm_pfns[i]);
+		if (write) {
+			lock_page(page);
+			set_page_dirty(page);
+			unlock_page(page);

Could be using set_page_dirty_lock() here.

Will fix, Thanks
Oak

Quoted 265 lines

/Thomas

Quoted 261 lines
drivers/gpu/drm/xe/xe_hmm.c new file +7 -0
+		}
+		mark_page_accessed(page);
+	}
+}
+
+/**
+ * build_sg() - build a scatter gather table for all the

physical
pages/pfn
+ * in a hmm_range. dma-address is save in sg table and will be
used
to program
+ * GPU page table later.
+ *
+ * @xe: the xe device who will access the dma-address in sg table
+ * @range: the hmm range that we build the sg table from. range-

Quoted 1 line

hmm_pfns[]

+ * has the pfn numbers of pages that back up this hmm address
range.
+ * @st: pointer to the sg table.
+ * @write: whether we write to this range. This decides dma map
direction
+ * for system pages. If write we map it bi-diretional; otherwise
+ * DMA_TO_DEVICE
+ *
+ * All the contiguous pfns will be collapsed into one entry in
+ * the scatter gather table. This is for the convenience of
+ * later on operations to bind address range to GPU page table.
+ *
+ * The dma_address in the sg table will later be used by GPU to
+ * access memory. So if the memory is system memory, we need to
+ * do a dma-mapping so it can be accessed by GPU/DMA. If the memory
+ * is GPU local memory (of the GPU who is going to access memory),
+ * we need gpu dpa (device physical address), and there is no
need
+ * of dma-mapping.
+ *
+ * FIXME: dma-mapping for peer gpu device to access remote gpu's
+ * memory. Add this when you support p2p
+ *
+ * This function allocates the storage of the sg table. It is
+ * caller's responsibility to free it calling sg_free_table.
+ *
+ * Returns 0 if successful; -ENOMEM if fails to allocate memory
+ */
+static int build_sg(struct xe_device *xe, struct hmm_range *range,

drivers/gpu/drm/xe/xe_hmm.c new file +1 -0
+			     struct sg_table *st, bool

write)

drivers/gpu/drm/xe/xe_hmm.c new file +8 -0
+{
+	struct device *dev = xe->drm.dev;
+	struct scatterlist *sg;
+	u64 i, npages;
+
+	sg = NULL;
+	st->nents = 0;
+	npages = ((range->end - 1) >> PAGE_SHIFT) - (range-
Quoted 1 line

start >>

PAGE_SHIFT) + 1;

drivers/gpu/drm/xe/xe_hmm.c new file +2 -0
+
+	if (unlikely(sg_alloc_table(st, npages,

GFP_KERNEL)))

drivers/gpu/drm/xe/xe_hmm.c new file +11 -0
+		return -ENOMEM;
+
+	for (i = 0; i < npages; i++) {
+		struct page *page;
+		unsigned long addr;
+		struct xe_mem_region *mr;
+
+		page = hmm_pfn_to_page(range->hmm_pfns[i]);
+		if (is_device_private_page(page)) {
+			mr = page_to_mem_region(page);
+			addr = vram_pfn_to_dpa(mr, range-
Quoted 1 line

hmm_pfns[i]);

drivers/gpu/drm/xe/xe_hmm.c new file +2 -0
+		} else {
+			addr = dma_map_page(dev, page, 0,

PAGE_SIZE,

drivers/gpu/drm/xe/xe_hmm.c new file +1 -0
+					write ?

DMA_BIDIRECTIONAL :
DMA_TO_DEVICE);

drivers/gpu/drm/xe/xe_hmm.c new file +3 -0
+		}
+
+		if (sg && (addr == (sg_dma_address(sg) + sg-
Quoted 1 line

length))) {

drivers/gpu/drm/xe/xe_hmm.c new file +18 -0
+			sg->length += PAGE_SIZE;
+			sg_dma_len(sg) += PAGE_SIZE;
+			continue;
+		}
+
+		sg =  sg ? sg_next(sg) : st->sgl;
+		sg_dma_address(sg) = addr;
+		sg_dma_len(sg) = PAGE_SIZE;
+		sg->length = PAGE_SIZE;
+		st->nents++;
+	}
+
+	sg_mark_end(sg);
+	return 0;
+}
+
+/**
+ * xe_hmm_populate_range() - Populate physical pages of a

virtual
+ * address range
+ *
+ * @vma: vma has information of the range to populate. only vma
+ * of userptr and hmmptr type can be populated.
+ * @hmm_range: pointer to hmm_range struct. hmm_rang-

Quoted 1 line

hmm_pfns

+ * will hold the populated pfns.
+ * @write: populate pages with write permission
+ *
+ * This function populate the physical pages of a virtual
+ * address range. The populated physical pages is saved in
+ * userptr's sg table. It is similar to get_user_pages but call
+ * hmm_range_fault.
+ *
+ * This function also read mmu notifier sequence # (
+ * mmu_interval_read_begin), for the purpose of later
+ * comparison (through mmu_interval_read_retry).
+ *
+ * This must be called with mmap read or write lock held.
+ *
+ * This function allocates the storage of the userptr sg table.
+ * It is caller's responsibility to free it calling sg_free_table.
+ *
+ * returns: 0 for succuss; negative error no on failure
+ */
+int xe_hmm_populate_range(struct xe_vma *vma, struct hmm_range
*hmm_range,

drivers/gpu/drm/xe/xe_hmm.c new file +4 -0
+						bool write)
+{
+	unsigned long timeout =
+		jiffies +

msecs_to_jiffies(HMM_RANGE_DEFAULT_TIMEOUT);

drivers/gpu/drm/xe/xe_hmm.c new file +14 -0
+	unsigned long *pfns, flags = HMM_PFN_REQ_FAULT;
+	struct xe_userptr_vma *userptr_vma;
+	struct xe_userptr *userptr;
+	u64 start = vma->gpuva.va.addr;
+	u64 end = start + vma->gpuva.va.range;
+	struct xe_vm *vm = xe_vma_vm(vma);
+	u64 npages;
+	int ret;
+
+	userptr_vma = to_userptr_vma(vma);
+	userptr = &userptr_vma->userptr;
+	mmap_assert_locked(userptr->notifier.mm);
+
+	npages = ((end - 1) >> PAGE_SHIFT) - (start >>

PAGE_SHIFT) +
1;

drivers/gpu/drm/xe/xe_hmm.c new file +1 -0
+	pfns = kvmalloc_array(npages, sizeof(*pfns),

GFP_KERNEL);

drivers/gpu/drm/xe/xe_hmm.c new file +9 -0
+	if (unlikely(!pfns))
+		return -ENOMEM;
+
+	if (write)
+		flags |= HMM_PFN_REQ_WRITE;
+
+	memset64((u64 *)pfns, (u64)flags, npages);
+	hmm_range->hmm_pfns = pfns;
+	hmm_range->notifier_seq =

mmu_interval_read_begin(&userptr-

Quoted 1 line

notifier);

drivers/gpu/drm/xe/xe_hmm.c new file +4 -0
+	hmm_range->notifier = &userptr->notifier;
+	hmm_range->start = start;
+	hmm_range->end = end;
+	hmm_range->pfn_flags_mask = HMM_PFN_REQ_FAULT |

HMM_PFN_REQ_WRITE;

drivers/gpu/drm/xe/xe_hmm.c new file +3 -0
+	/**
+	 * FIXME:
+	 * Set the the dev_private_owner can prevent

hmm_range_fault
to fault

drivers/gpu/drm/xe/xe_hmm.c new file +1 -0
+	 * in the device private pages owned by caller. See

function

drivers/gpu/drm/xe/xe_hmm.c new file +1 -0
+	 * hmm_vma_handle_pte. In multiple GPU case, this

should
be
set to the

drivers/gpu/drm/xe/xe_hmm.c new file +1 -0
+	 * device owner of the best migration destination.

e.g.,
device0/vm0

drivers/gpu/drm/xe/xe_hmm.c new file +1 -0
+	 * has a page fault, but we have determined the best

placement of

drivers/gpu/drm/xe/xe_hmm.c new file +1 -0
+	 * the fault address should be on device1, we should

set
below to

drivers/gpu/drm/xe/xe_hmm.c new file +18 -0
+	 * device1 instead of device0.
+	 */
+	hmm_range->dev_private_owner = vm->xe->drm.dev;
+
+	while (true) {
+		ret = hmm_range_fault(hmm_range);
+		if (time_after(jiffies, timeout))
+			break;
+
+		if (ret == -EBUSY)
+			continue;
+		break;
+	}
+
+	if (ret)
+		goto free_pfns;
+
+	ret = build_sg(vm->xe, hmm_range, &userptr->sgt,

write);

drivers/gpu/drm/xe/xe_hmm.c new file +12 -0
+	if (ret)
+		goto free_pfns;
+
+	mark_range_accessed(hmm_range, write);
+	userptr->sg = &userptr->sgt;
+	userptr->notifier_seq = hmm_range->notifier_seq;
+
+free_pfns:
+	kvfree(pfns);
+	return ret;
+}
+
drivers/gpu/drm/xe/xe_hmm.h new file +11 -0
new file mode 100644
index 000000000000..960f3f6d36ae
@@ -0,0 +1,12 @@
+// SPDX-License-Identifier: MIT
+/*
+ * Copyright © 2024 Intel Corporation
+ */
+
+#include <linux/types.h>
+#include <linux/hmm.h>
+#include "xe_vm_types.h"
+#include "xe_svm.h"
+
+int xe_hmm_populate_range(struct xe_vma *vma, struct

hmm_range
*hmm_range,

drivers/gpu/drm/xe/xe_hmm.h new file +1 -0
+						bool write);
#39 lore raw

Re: [PATCH 4/5] drm/xe: Helper to populate a userptr or hmmptr

In reply to Oak Zeng

Hi, Oak,

Found another thing, see below:

Quoted 257 lines · Oak Zeng

Add a helper function xe_hmm_populate_range to populate a a userptr or hmmptr range. This functions calls hmm_range_fault to read CPU page tables and populate all pfns/pages of this virtual address range.

If the populated page is system memory page, dma-mapping is performed to get a dma-address which can be used later for GPU to access pages.

If the populated page is device private page, we calculate the dpa ( device physical address) of the page.

The dma-address or dpa is then saved in userptr's sg table. This is prepare work to replace the get_user_pages_fast code in userptr code path. The helper function will also be used to populate hmmptr later.

Co-developed-by Niranjana Vishwanathapura

<[email protected]>
Signed-off-by: Niranjana Vishwanathapura
<[email protected]>
Cc: Matthew Brost <[email protected]>
Cc: Thomas Hellström <[email protected]>
Cc: Brian Welty <[email protected]>
---
 drivers/gpu/drm/xe/Makefile |   3 +-
 drivers/gpu/drm/xe/xe_hmm.c | 213
++++++++++++++++++++++++++++++++++++
 drivers/gpu/drm/xe/xe_hmm.h |  12 ++

 3 files changed, 227 insertions(+), 1 deletion(-)

 create mode 100644 drivers/gpu/drm/xe/xe_hmm.c
 create mode 100644 drivers/gpu/drm/xe/xe_hmm.h

drivers/gpu/drm/xe/Makefile +2 -1
index 840467080e59..29dcbc938b01 100644
@@ -143,7 +143,8 @@ xe-y += xe_bb.o \
  	xe_wait_user_fence.o \
  	xe_wa.o \
  	xe_wopcm.o \
-	xe_svm_devmem.o
+	xe_svm_devmem.o \
+	xe_hmm.o

 # graphics hardware monitoring (HWMON) support
 xe-$(CONFIG_HWMON) += xe_hwmon.o
drivers/gpu/drm/xe/xe_hmm.c new file +28 -0
new file mode 100644
index 000000000000..c45c2447d386
@@ -0,0 +1,213 @@
+// SPDX-License-Identifier: MIT
+/*
+ * Copyright © 2024 Intel Corporation
+ */
+
+#include <linux/mmu_notifier.h>
+#include <linux/dma-mapping.h>
+#include <linux/memremap.h>
+#include <linux/swap.h>
+#include <linux/mm.h>
+#include "xe_hmm.h"
+#include "xe_vm.h"
+
+/**
+ * mark_range_accessed() - mark a range is accessed, so core mm
+ * have such information for memory eviction or write back to
+ * hard disk
+ *
+ * @range: the range to mark
+ * @write: if write to this range, we mark pages in this range
+ * as dirty
+ */
+static void mark_range_accessed(struct hmm_range *range, bool write)
+{
+	struct page *page;
+	u64 i, npages;
+
+	npages = ((range->end - 1) >> PAGE_SHIFT) - (range->start >>

PAGE_SHIFT) + 1;

drivers/gpu/drm/xe/xe_hmm.c new file +13 -0
+	for (i = 0; i < npages; i++) {
+		page = hmm_pfn_to_page(range->hmm_pfns[i]);
+		if (write) {
+			lock_page(page);
+			set_page_dirty(page);
+			unlock_page(page);
+		}
+		mark_page_accessed(page);
+	}
+}
+
+/**
+ * build_sg() - build a scatter gather table for all the physical

pages/pfn
+ * in a hmm_range. dma-address is save in sg table and will be used to program
+ * GPU page table later.
+ *
+ * @xe: the xe device who will access the dma-address in sg table
+ * @range: the hmm range that we build the sg table from. range-

Quoted 1 line

hmm_pfns[]

+ * has the pfn numbers of pages that back up this hmm address range.
+ * @st: pointer to the sg table.
+ * @write: whether we write to this range. This decides dma map direction
+ * for system pages. If write we map it bi-diretional; otherwise
+ * DMA_TO_DEVICE
+ *
+ * All the contiguous pfns will be collapsed into one entry in
+ * the scatter gather table. This is for the convenience of
+ * later on operations to bind address range to GPU page table.
+ *
+ * The dma_address in the sg table will later be used by GPU to
+ * access memory. So if the memory is system memory, we need to
+ * do a dma-mapping so it can be accessed by GPU/DMA. If the memory
+ * is GPU local memory (of the GPU who is going to access memory),
+ * we need gpu dpa (device physical address), and there is no need
+ * of dma-mapping.
+ *
+ * FIXME: dma-mapping for peer gpu device to access remote gpu's
+ * memory. Add this when you support p2p
+ *
+ * This function allocates the storage of the sg table. It is
+ * caller's responsibility to free it calling sg_free_table.
+ *
+ * Returns 0 if successful; -ENOMEM if fails to allocate memory
+ */

drivers/gpu/drm/xe/xe_hmm.c new file +10 -0
+static int build_sg(struct xe_device *xe, struct hmm_range *range,
+			     struct sg_table *st, bool write)
+{
+	struct device *dev = xe->drm.dev;
+	struct scatterlist *sg;
+	u64 i, npages;
+
+	sg = NULL;
+	st->nents = 0;
+	npages = ((range->end - 1) >> PAGE_SHIFT) - (range->start >>

PAGE_SHIFT) + 1;

drivers/gpu/drm/xe/xe_hmm.c new file +13 -0
+
+	if (unlikely(sg_alloc_table(st, npages, GFP_KERNEL)))
+		return -ENOMEM;
+
+	for (i = 0; i < npages; i++) {
+		struct page *page;
+		unsigned long addr;
+		struct xe_mem_region *mr;
+
+		page = hmm_pfn_to_page(range->hmm_pfns[i]);
+		if (is_device_private_page(page)) {
+			mr = page_to_mem_region(page);
+			addr = vram_pfn_to_dpa(mr, range-
Quoted 1 line

hmm_pfns[i]);

drivers/gpu/drm/xe/xe_hmm.c new file +3 -0
+		} else {
+			addr = dma_map_page(dev, page, 0, PAGE_SIZE,
+					write ? DMA_BIDIRECTIONAL :

DMA_TO_DEVICE);

drivers/gpu/drm/xe/xe_hmm.c new file +3 -0
+		}
+
+		if (sg && (addr == (sg_dma_address(sg) + sg-
Quoted 1 line

length))) {

drivers/gpu/drm/xe/xe_hmm.c new file +43 -0
+			sg->length += PAGE_SIZE;
+			sg_dma_len(sg) += PAGE_SIZE;
+			continue;
+		}
+
+		sg =  sg ? sg_next(sg) : st->sgl;
+		sg_dma_address(sg) = addr;
+		sg_dma_len(sg) = PAGE_SIZE;
+		sg->length = PAGE_SIZE;
+		st->nents++;
+	}
+
+	sg_mark_end(sg);
+	return 0;
+}
+
+/**
+ * xe_hmm_populate_range() - Populate physical pages of a virtual
+ * address range
+ *
+ * @vma: vma has information of the range to populate. only vma
+ * of userptr and hmmptr type can be populated.
+ * @hmm_range: pointer to hmm_range struct. hmm_rang->hmm_pfns
+ * will hold the populated pfns.
+ * @write: populate pages with write permission
+ *
+ * This function populate the physical pages of a virtual
+ * address range. The populated physical pages is saved in
+ * userptr's sg table. It is similar to get_user_pages but call
+ * hmm_range_fault.
+ *
+ * This function also read mmu notifier sequence # (
+ * mmu_interval_read_begin), for the purpose of later
+ * comparison (through mmu_interval_read_retry).
+ *
+ * This must be called with mmap read or write lock held.
+ *
+ * This function allocates the storage of the userptr sg table.
+ * It is caller's responsibility to free it calling sg_free_table.
+ *
+ * returns: 0 for succuss; negative error no on failure
+ */
+int xe_hmm_populate_range(struct xe_vma *vma, struct hmm_range

*hmm_range,

drivers/gpu/drm/xe/xe_hmm.c new file +4 -0
+						bool write)
+{
+	unsigned long timeout =
+		jiffies +

msecs_to_jiffies(HMM_RANGE_DEFAULT_TIMEOUT);

drivers/gpu/drm/xe/xe_hmm.c new file +14 -0
+	unsigned long *pfns, flags = HMM_PFN_REQ_FAULT;
+	struct xe_userptr_vma *userptr_vma;
+	struct xe_userptr *userptr;
+	u64 start = vma->gpuva.va.addr;
+	u64 end = start + vma->gpuva.va.range;
+	struct xe_vm *vm = xe_vma_vm(vma);
+	u64 npages;
+	int ret;
+
+	userptr_vma = to_userptr_vma(vma);
+	userptr = &userptr_vma->userptr;
+	mmap_assert_locked(userptr->notifier.mm);
+
+	npages = ((end - 1) >> PAGE_SHIFT) - (start >> PAGE_SHIFT) +

1;

drivers/gpu/drm/xe/xe_hmm.c new file +10 -0
+	pfns = kvmalloc_array(npages, sizeof(*pfns), GFP_KERNEL);
+	if (unlikely(!pfns))
+		return -ENOMEM;
+
+	if (write)
+		flags |= HMM_PFN_REQ_WRITE;
+
+	memset64((u64 *)pfns, (u64)flags, npages);
+	hmm_range->hmm_pfns = pfns;
+	hmm_range->notifier_seq = mmu_interval_read_begin(&userptr-
Quoted 1 line

notifier);

drivers/gpu/drm/xe/xe_hmm.c new file +4 -0
+	hmm_range->notifier = &userptr->notifier;
+	hmm_range->start = start;
+	hmm_range->end = end;
+	hmm_range->pfn_flags_mask = HMM_PFN_REQ_FAULT |

HMM_PFN_REQ_WRITE;

drivers/gpu/drm/xe/xe_hmm.c new file +3 -0
+	/**
+	 * FIXME:
+	 * Set the the dev_private_owner can prevent hmm_range_fault

to fault

drivers/gpu/drm/xe/xe_hmm.c new file +2 -0
+	 * in the device private pages owned by caller. See function
+	 * hmm_vma_handle_pte. In multiple GPU case, this should be

set to the

drivers/gpu/drm/xe/xe_hmm.c new file +1 -0
+	 * device owner of the best migration destination. e.g.,

device0/vm0

drivers/gpu/drm/xe/xe_hmm.c new file +1 -0
+	 * has a page fault, but we have determined the best

placement of

drivers/gpu/drm/xe/xe_hmm.c new file +1 -0
+	 * the fault address should be on device1, we should set

below to

drivers/gpu/drm/xe/xe_hmm.c new file +1 -0
+	 * device1 instead of device0.

drivers/gpu/drm/xe/xe_hmm.c line 194

Quoted 10 lines
drivers/gpu/drm/xe/xe_hmm.c new file +10 -0
+	 */
+	hmm_range->dev_private_owner = vm->xe->drm.dev;
+
+	while (true) {
+		ret = hmm_range_fault(hmm_range);
+		if (time_after(jiffies, timeout))
+			break;
+
+		if (ret == -EBUSY)
+			continue;

If (ret == -EBUSY) it looks from the hmm_range_fault() implementation like hmm_range->notifier_seq has become invalid and without calling mmu_interval_read_begin() again, we will end up in an infinite loop?

/Thomas

Quoted 39 lines
drivers/gpu/drm/xe/xe_hmm.c new file +19 -0
+		break;
+	}
+
+	if (ret)
+		goto free_pfns;
+
+	ret = build_sg(vm->xe, hmm_range, &userptr->sgt, write);
+	if (ret)
+		goto free_pfns;
+
+	mark_range_accessed(hmm_range, write);
+	userptr->sg = &userptr->sgt;
+	userptr->notifier_seq = hmm_range->notifier_seq;
+
+free_pfns:
+	kvfree(pfns);
+	return ret;
+}
+
drivers/gpu/drm/xe/xe_hmm.h new file +11 -0
new file mode 100644
index 000000000000..960f3f6d36ae
@@ -0,0 +1,12 @@
+// SPDX-License-Identifier: MIT
+/*
+ * Copyright © 2024 Intel Corporation
+ */
+
+#include <linux/types.h>
+#include <linux/hmm.h>
+#include "xe_vm_types.h"
+#include "xe_svm.h"
+
+int xe_hmm_populate_range(struct xe_vma *vma, struct hmm_range

*hmm_range,

drivers/gpu/drm/xe/xe_hmm.h new file +1 -0
+						bool write);
author
#40 lore raw

RE: [PATCH 4/5] drm/xe: Helper to populate a userptr or hmmptr

Quoted 279 lines

-----Original Message-----
From: Hellstrom, Thomas <[email protected]>
Sent: Monday, March 18, 2024 9:13 AM
To: [email protected]; Zeng, Oak <[email protected]>
Cc: Brost, Matthew <[email protected]>; Welty, Brian <[email protected]>; [email protected]; Ghimiray, Himal Prasad <[email protected]>
Subject: Re: [PATCH 4/5] drm/xe: Helper to populate a userptr or hmmptr

Hi, Oak,

Found another thing, see below:

Quoted 263 lines · Oak Zeng

Add a helper function xe_hmm_populate_range to populate a a userptr or hmmptr range. This functions calls hmm_range_fault to read CPU page tables and populate all pfns/pages of this virtual address range.

If the populated page is system memory page, dma-mapping is performed to get a dma-address which can be used later for GPU to access pages.

If the populated page is device private page, we calculate the dpa ( device physical address) of the page.

The dma-address or dpa is then saved in userptr's sg table. This is prepare work to replace the get_user_pages_fast code in userptr code path. The helper function will also be used to populate hmmptr later.

Co-developed-by Niranjana Vishwanathapura

<[email protected]>
Signed-off-by: Niranjana Vishwanathapura
<[email protected]>
Cc: Matthew Brost <[email protected]>
Cc: Thomas Hellström <[email protected]>
Cc: Brian Welty <[email protected]>
---
 drivers/gpu/drm/xe/Makefile |   3 +-
 drivers/gpu/drm/xe/xe_hmm.c | 213
++++++++++++++++++++++++++++++++++++
 drivers/gpu/drm/xe/xe_hmm.h |  12 ++

 3 files changed, 227 insertions(+), 1 deletion(-)

 create mode 100644 drivers/gpu/drm/xe/xe_hmm.c
 create mode 100644 drivers/gpu/drm/xe/xe_hmm.h

drivers/gpu/drm/xe/Makefile +2 -1
index 840467080e59..29dcbc938b01 100644
@@ -143,7 +143,8 @@ xe-y += xe_bb.o \
  	xe_wait_user_fence.o \
  	xe_wa.o \
  	xe_wopcm.o \
-	xe_svm_devmem.o
+	xe_svm_devmem.o \
+	xe_hmm.o
 
  # graphics hardware monitoring (HWMON) support

 xe-$(CONFIG_HWMON) += xe_hwmon.o

drivers/gpu/drm/xe/xe_hmm.c new file +28 -0
new file mode 100644
index 000000000000..c45c2447d386
@@ -0,0 +1,213 @@
+// SPDX-License-Identifier: MIT
+/*
+ * Copyright © 2024 Intel Corporation
+ */
+
+#include <linux/mmu_notifier.h>
+#include <linux/dma-mapping.h>
+#include <linux/memremap.h>
+#include <linux/swap.h>
+#include <linux/mm.h>
+#include "xe_hmm.h"
+#include "xe_vm.h"
+
+/**
+ * mark_range_accessed() - mark a range is accessed, so core mm
+ * have such information for memory eviction or write back to
+ * hard disk
+ *
+ * @range: the range to mark
+ * @write: if write to this range, we mark pages in this range
+ * as dirty
+ */
+static void mark_range_accessed(struct hmm_range *range, bool write)
+{
+	struct page *page;
+	u64 i, npages;
+
+	npages = ((range->end - 1) >> PAGE_SHIFT) - (range->start >>

PAGE_SHIFT) + 1;

drivers/gpu/drm/xe/xe_hmm.c new file +13 -0
+	for (i = 0; i < npages; i++) {
+		page = hmm_pfn_to_page(range->hmm_pfns[i]);
+		if (write) {
+			lock_page(page);
+			set_page_dirty(page);
+			unlock_page(page);
+		}
+		mark_page_accessed(page);
+	}
+}
+
+/**
+ * build_sg() - build a scatter gather table for all the physical

pages/pfn
+ * in a hmm_range. dma-address is save in sg table and will be used to program
+ * GPU page table later.
+ *
+ * @xe: the xe device who will access the dma-address in sg table
+ * @range: the hmm range that we build the sg table from. range-

Quoted 1 line

hmm_pfns[]

+ * has the pfn numbers of pages that back up this hmm address range.
+ * @st: pointer to the sg table.
+ * @write: whether we write to this range. This decides dma map direction
+ * for system pages. If write we map it bi-diretional; otherwise
+ * DMA_TO_DEVICE
+ *
+ * All the contiguous pfns will be collapsed into one entry in
+ * the scatter gather table. This is for the convenience of
+ * later on operations to bind address range to GPU page table.
+ *
+ * The dma_address in the sg table will later be used by GPU to
+ * access memory. So if the memory is system memory, we need to
+ * do a dma-mapping so it can be accessed by GPU/DMA. If the memory
+ * is GPU local memory (of the GPU who is going to access memory),
+ * we need gpu dpa (device physical address), and there is no need
+ * of dma-mapping.
+ *
+ * FIXME: dma-mapping for peer gpu device to access remote gpu's
+ * memory. Add this when you support p2p
+ *
+ * This function allocates the storage of the sg table. It is
+ * caller's responsibility to free it calling sg_free_table.
+ *
+ * Returns 0 if successful; -ENOMEM if fails to allocate memory
+ */

drivers/gpu/drm/xe/xe_hmm.c new file +10 -0
+static int build_sg(struct xe_device *xe, struct hmm_range *range,
+			     struct sg_table *st, bool write)
+{
+	struct device *dev = xe->drm.dev;
+	struct scatterlist *sg;
+	u64 i, npages;
+
+	sg = NULL;
+	st->nents = 0;
+	npages = ((range->end - 1) >> PAGE_SHIFT) - (range->start >>

PAGE_SHIFT) + 1;

drivers/gpu/drm/xe/xe_hmm.c new file +13 -0
+
+	if (unlikely(sg_alloc_table(st, npages, GFP_KERNEL)))
+		return -ENOMEM;
+
+	for (i = 0; i < npages; i++) {
+		struct page *page;
+		unsigned long addr;
+		struct xe_mem_region *mr;
+
+		page = hmm_pfn_to_page(range->hmm_pfns[i]);
+		if (is_device_private_page(page)) {
+			mr = page_to_mem_region(page);
+			addr = vram_pfn_to_dpa(mr, range-
Quoted 1 line

hmm_pfns[i]);

drivers/gpu/drm/xe/xe_hmm.c new file +3 -0
+		} else {
+			addr = dma_map_page(dev, page, 0, PAGE_SIZE,
+					write ? DMA_BIDIRECTIONAL :

DMA_TO_DEVICE);

drivers/gpu/drm/xe/xe_hmm.c new file +3 -0
+		}
+
+		if (sg && (addr == (sg_dma_address(sg) + sg-
Quoted 1 line

length))) {

drivers/gpu/drm/xe/xe_hmm.c new file +43 -0
+			sg->length += PAGE_SIZE;
+			sg_dma_len(sg) += PAGE_SIZE;
+			continue;
+		}
+
+		sg =  sg ? sg_next(sg) : st->sgl;
+		sg_dma_address(sg) = addr;
+		sg_dma_len(sg) = PAGE_SIZE;
+		sg->length = PAGE_SIZE;
+		st->nents++;
+	}
+
+	sg_mark_end(sg);
+	return 0;
+}
+
+/**
+ * xe_hmm_populate_range() - Populate physical pages of a virtual
+ * address range
+ *
+ * @vma: vma has information of the range to populate. only vma
+ * of userptr and hmmptr type can be populated.
+ * @hmm_range: pointer to hmm_range struct. hmm_rang->hmm_pfns
+ * will hold the populated pfns.
+ * @write: populate pages with write permission
+ *
+ * This function populate the physical pages of a virtual
+ * address range. The populated physical pages is saved in
+ * userptr's sg table. It is similar to get_user_pages but call
+ * hmm_range_fault.
+ *
+ * This function also read mmu notifier sequence # (
+ * mmu_interval_read_begin), for the purpose of later
+ * comparison (through mmu_interval_read_retry).
+ *
+ * This must be called with mmap read or write lock held.
+ *
+ * This function allocates the storage of the userptr sg table.
+ * It is caller's responsibility to free it calling sg_free_table.
+ *
+ * returns: 0 for succuss; negative error no on failure
+ */
+int xe_hmm_populate_range(struct xe_vma *vma, struct hmm_range

*hmm_range,

drivers/gpu/drm/xe/xe_hmm.c new file +4 -0
+						bool write)
+{
+	unsigned long timeout =
+		jiffies +

msecs_to_jiffies(HMM_RANGE_DEFAULT_TIMEOUT);

drivers/gpu/drm/xe/xe_hmm.c new file +14 -0
+	unsigned long *pfns, flags = HMM_PFN_REQ_FAULT;
+	struct xe_userptr_vma *userptr_vma;
+	struct xe_userptr *userptr;
+	u64 start = vma->gpuva.va.addr;
+	u64 end = start + vma->gpuva.va.range;
+	struct xe_vm *vm = xe_vma_vm(vma);
+	u64 npages;
+	int ret;
+
+	userptr_vma = to_userptr_vma(vma);
+	userptr = &userptr_vma->userptr;
+	mmap_assert_locked(userptr->notifier.mm);
+
+	npages = ((end - 1) >> PAGE_SHIFT) - (start >> PAGE_SHIFT) +

1;

drivers/gpu/drm/xe/xe_hmm.c new file +10 -0
+	pfns = kvmalloc_array(npages, sizeof(*pfns), GFP_KERNEL);
+	if (unlikely(!pfns))
+		return -ENOMEM;
+
+	if (write)
+		flags |= HMM_PFN_REQ_WRITE;
+
+	memset64((u64 *)pfns, (u64)flags, npages);
+	hmm_range->hmm_pfns = pfns;
+	hmm_range->notifier_seq = mmu_interval_read_begin(&userptr-
Quoted 1 line

notifier);

drivers/gpu/drm/xe/xe_hmm.c new file +4 -0
+	hmm_range->notifier = &userptr->notifier;
+	hmm_range->start = start;
+	hmm_range->end = end;
+	hmm_range->pfn_flags_mask = HMM_PFN_REQ_FAULT |

HMM_PFN_REQ_WRITE;

drivers/gpu/drm/xe/xe_hmm.c new file +3 -0
+	/**
+	 * FIXME:
+	 * Set the the dev_private_owner can prevent hmm_range_fault

to fault

drivers/gpu/drm/xe/xe_hmm.c new file +2 -0
+	 * in the device private pages owned by caller. See function
+	 * hmm_vma_handle_pte. In multiple GPU case, this should be

set to the

drivers/gpu/drm/xe/xe_hmm.c new file +1 -0
+	 * device owner of the best migration destination. e.g.,

device0/vm0

drivers/gpu/drm/xe/xe_hmm.c new file +1 -0
+	 * has a page fault, but we have determined the best

placement of

drivers/gpu/drm/xe/xe_hmm.c new file +1 -0
+	 * the fault address should be on device1, we should set

below to

drivers/gpu/drm/xe/xe_hmm.c new file +7 -0
+	 * device1 instead of device0.
+	 */
+	hmm_range->dev_private_owner = vm->xe->drm.dev;
+
+	while (true) {
+		ret = hmm_range_fault(hmm_range);
+		if (time_after(jiffies, timeout))
Quoted 9 lines

drivers/gpu/drm/xe/xe_hmm.c line 194

Quoted 4 lines
drivers/gpu/drm/xe/xe_hmm.c new file +4 -0
+			break;
+
+		if (ret == -EBUSY)
+			continue;

If (ret == -EBUSY) it looks from the hmm_range_fault() implementation like hmm_range->notifier_seq has become invalid and without calling mmu_interval_read_begin() again, we will end up in an infinite loop?

I noticed this thing before and had a read_begin in the while loop. But after a second thought, function xe_hmm_populate_range is called inside a mmap lock, so after the read_begin is called above, there can't be a invalidation before mmap unlock. So theoretically EBUSY can't happen?

Oak

Quoted 43 lines

/Thomas

Quoted 39 lines
drivers/gpu/drm/xe/xe_hmm.c new file +19 -0
+		break;
+	}
+
+	if (ret)
+		goto free_pfns;
+
+	ret = build_sg(vm->xe, hmm_range, &userptr->sgt, write);
+	if (ret)
+		goto free_pfns;
+
+	mark_range_accessed(hmm_range, write);
+	userptr->sg = &userptr->sgt;
+	userptr->notifier_seq = hmm_range->notifier_seq;
+
+free_pfns:
+	kvfree(pfns);
+	return ret;
+}
+
drivers/gpu/drm/xe/xe_hmm.h new file +11 -0
new file mode 100644
index 000000000000..960f3f6d36ae
@@ -0,0 +1,12 @@
+// SPDX-License-Identifier: MIT
+/*
+ * Copyright © 2024 Intel Corporation
+ */
+
+#include <linux/types.h>
+#include <linux/hmm.h>
+#include "xe_vm_types.h"
+#include "xe_svm.h"
+
+int xe_hmm_populate_range(struct xe_vma *vma, struct hmm_range

*hmm_range,

drivers/gpu/drm/xe/xe_hmm.h new file +1 -0
+						bool write);
#41 lore raw

Re: [PATCH 4/5] drm/xe: Helper to populate a userptr or hmmptr

Hi,

Quoted 319 lines · Oak
Quoted 309 lines

-----Original Message-----
From: Hellstrom, Thomas <[email protected]>
Sent: Monday, March 18, 2024 9:13 AM
To: [email protected]; Zeng, Oak <[email protected]>
Cc: Brost, Matthew <[email protected]>; Welty, Brian <[email protected]>; [email protected]; Ghimiray, Himal Prasad <[email protected]>
Subject: Re: [PATCH 4/5] drm/xe: Helper to populate a userptr or hmmptr

Hi, Oak,

Found another thing, see below:

Quoted 292 lines · Oak Zeng

Add a helper function xe_hmm_populate_range to populate a a userptr or hmmptr range. This functions calls hmm_range_fault to read CPU page tables and populate all pfns/pages of this virtual address range.

If the populated page is system memory page, dma-mapping is performed
to get a dma-address which can be used later for GPU to access pages.

If the populated page is device private page, we calculate the dpa (
device physical address) of the page.

The dma-address or dpa is then saved in userptr's sg table. This is
prepare work to replace the get_user_pages_fast code in userptr code
path. The helper function will also be used to populate hmmptr later.

Co-developed-by Niranjana Vishwanathapura

<[email protected]>
Signed-off-by: Niranjana Vishwanathapura
<[email protected]>
Cc: Matthew Brost <[email protected]>
Cc: Thomas Hellström <[email protected]>
Cc: Brian Welty <[email protected]>
---
 drivers/gpu/drm/xe/Makefile |   3 +-
 drivers/gpu/drm/xe/xe_hmm.c | 213
++++++++++++++++++++++++++++++++++++
 drivers/gpu/drm/xe/xe_hmm.h |  12 ++

 3 files changed, 227 insertions(+), 1 deletion(-)

 create mode 100644 drivers/gpu/drm/xe/xe_hmm.c
 create mode 100644 drivers/gpu/drm/xe/xe_hmm.h

drivers/gpu/drm/xe/Makefile +2 -1
index 840467080e59..29dcbc938b01 100644
@@ -143,7 +143,8 @@ xe-y += xe_bb.o \
  	xe_wait_user_fence.o \
  	xe_wa.o \
  	xe_wopcm.o \
-	xe_svm_devmem.o
+	xe_svm_devmem.o \
+	xe_hmm.o
 
  # graphics hardware monitoring (HWMON) support

 xe-$(CONFIG_HWMON) += xe_hwmon.o

drivers/gpu/drm/xe/xe_hmm.c new file +23 -0
new file mode 100644
index 000000000000..c45c2447d386
@@ -0,0 +1,213 @@
+// SPDX-License-Identifier: MIT
+/*
+ * Copyright © 2024 Intel Corporation
+ */
+
+#include <linux/mmu_notifier.h>
+#include <linux/dma-mapping.h>
+#include <linux/memremap.h>
+#include <linux/swap.h>
+#include <linux/mm.h>
+#include "xe_hmm.h"
+#include "xe_vm.h"
+
+/**
+ * mark_range_accessed() - mark a range is accessed, so core mm
+ * have such information for memory eviction or write back to
+ * hard disk
+ *
+ * @range: the range to mark
+ * @write: if write to this range, we mark pages in this range
+ * as dirty
+ */
+static void mark_range_accessed(struct hmm_range *range, bool

write)

drivers/gpu/drm/xe/xe_hmm.c new file +5 -0
+{
+	struct page *page;
+	u64 i, npages;
+
+	npages = ((range->end - 1) >> PAGE_SHIFT) - (range-
Quoted 1 line

start >>

PAGE_SHIFT) + 1;

drivers/gpu/drm/xe/xe_hmm.c new file +13 -0
+	for (i = 0; i < npages; i++) {
+		page = hmm_pfn_to_page(range->hmm_pfns[i]);
+		if (write) {
+			lock_page(page);
+			set_page_dirty(page);
+			unlock_page(page);
+		}
+		mark_page_accessed(page);
+	}
+}
+
+/**
+ * build_sg() - build a scatter gather table for all the

physical
pages/pfn
+ * in a hmm_range. dma-address is save in sg table and will be used
to program
+ * GPU page table later.
+ *
+ * @xe: the xe device who will access the dma-address in sg table
+ * @range: the hmm range that we build the sg table from. range-

Quoted 1 line

hmm_pfns[]

+ * has the pfn numbers of pages that back up this hmm address range.
+ * @st: pointer to the sg table.
+ * @write: whether we write to this range. This decides dma map direction
+ * for system pages. If write we map it bi-diretional; otherwise
+ * DMA_TO_DEVICE
+ *
+ * All the contiguous pfns will be collapsed into one entry in
+ * the scatter gather table. This is for the convenience of
+ * later on operations to bind address range to GPU page table.
+ *
+ * The dma_address in the sg table will later be used by GPU to
+ * access memory. So if the memory is system memory, we need to
+ * do a dma-mapping so it can be accessed by GPU/DMA. If the memory
+ * is GPU local memory (of the GPU who is going to access memory),
+ * we need gpu dpa (device physical address), and there is no need
+ * of dma-mapping.
+ *
+ * FIXME: dma-mapping for peer gpu device to access remote gpu's
+ * memory. Add this when you support p2p
+ *
+ * This function allocates the storage of the sg table. It is
+ * caller's responsibility to free it calling sg_free_table.
+ *
+ * Returns 0 if successful; -ENOMEM if fails to allocate memory
+ */
+static int build_sg(struct xe_device *xe, struct hmm_range *range,

drivers/gpu/drm/xe/xe_hmm.c new file +9 -0
+			     struct sg_table *st, bool write)
+{
+	struct device *dev = xe->drm.dev;
+	struct scatterlist *sg;
+	u64 i, npages;
+
+	sg = NULL;
+	st->nents = 0;
+	npages = ((range->end - 1) >> PAGE_SHIFT) - (range-
Quoted 1 line

start >>

PAGE_SHIFT) + 1;

drivers/gpu/drm/xe/xe_hmm.c new file +13 -0
+
+	if (unlikely(sg_alloc_table(st, npages, GFP_KERNEL)))
+		return -ENOMEM;
+
+	for (i = 0; i < npages; i++) {
+		struct page *page;
+		unsigned long addr;
+		struct xe_mem_region *mr;
+
+		page = hmm_pfn_to_page(range->hmm_pfns[i]);
+		if (is_device_private_page(page)) {
+			mr = page_to_mem_region(page);
+			addr = vram_pfn_to_dpa(mr, range-
Quoted 1 line

hmm_pfns[i]);

drivers/gpu/drm/xe/xe_hmm.c new file +2 -0
+		} else {
+			addr = dma_map_page(dev, page, 0,

PAGE_SIZE,

drivers/gpu/drm/xe/xe_hmm.c new file +1 -0
+					write ?

DMA_BIDIRECTIONAL :
DMA_TO_DEVICE);

drivers/gpu/drm/xe/xe_hmm.c new file +3 -0
+		}
+
+		if (sg && (addr == (sg_dma_address(sg) + sg-
Quoted 1 line

length))) {

drivers/gpu/drm/xe/xe_hmm.c new file +18 -0
+			sg->length += PAGE_SIZE;
+			sg_dma_len(sg) += PAGE_SIZE;
+			continue;
+		}
+
+		sg =  sg ? sg_next(sg) : st->sgl;
+		sg_dma_address(sg) = addr;
+		sg_dma_len(sg) = PAGE_SIZE;
+		sg->length = PAGE_SIZE;
+		st->nents++;
+	}
+
+	sg_mark_end(sg);
+	return 0;
+}
+
+/**
+ * xe_hmm_populate_range() - Populate physical pages of a

virtual
+ * address range
+ *
+ * @vma: vma has information of the range to populate. only vma
+ * of userptr and hmmptr type can be populated.
+ * @hmm_range: pointer to hmm_range struct. hmm_rang->hmm_pfns
+ * will hold the populated pfns.
+ * @write: populate pages with write permission
+ *
+ * This function populate the physical pages of a virtual
+ * address range. The populated physical pages is saved in
+ * userptr's sg table. It is similar to get_user_pages but call
+ * hmm_range_fault.
+ *
+ * This function also read mmu notifier sequence # (
+ * mmu_interval_read_begin), for the purpose of later
+ * comparison (through mmu_interval_read_retry).
+ *
+ * This must be called with mmap read or write lock held.
+ *
+ * This function allocates the storage of the userptr sg table.
+ * It is caller's responsibility to free it calling sg_free_table.
+ *
+ * returns: 0 for succuss; negative error no on failure
+ */
+int xe_hmm_populate_range(struct xe_vma *vma, struct hmm_range *hmm_range,

drivers/gpu/drm/xe/xe_hmm.c new file +4 -0
+						bool write)
+{
+	unsigned long timeout =
+		jiffies +

msecs_to_jiffies(HMM_RANGE_DEFAULT_TIMEOUT);

drivers/gpu/drm/xe/xe_hmm.c new file +14 -0
+	unsigned long *pfns, flags = HMM_PFN_REQ_FAULT;
+	struct xe_userptr_vma *userptr_vma;
+	struct xe_userptr *userptr;
+	u64 start = vma->gpuva.va.addr;
+	u64 end = start + vma->gpuva.va.range;
+	struct xe_vm *vm = xe_vma_vm(vma);
+	u64 npages;
+	int ret;
+
+	userptr_vma = to_userptr_vma(vma);
+	userptr = &userptr_vma->userptr;
+	mmap_assert_locked(userptr->notifier.mm);
+
+	npages = ((end - 1) >> PAGE_SHIFT) - (start >>

PAGE_SHIFT) +
1;

drivers/gpu/drm/xe/xe_hmm.c new file +1 -0
+	pfns = kvmalloc_array(npages, sizeof(*pfns),

GFP_KERNEL);

drivers/gpu/drm/xe/xe_hmm.c new file +9 -0
+	if (unlikely(!pfns))
+		return -ENOMEM;
+
+	if (write)
+		flags |= HMM_PFN_REQ_WRITE;
+
+	memset64((u64 *)pfns, (u64)flags, npages);
+	hmm_range->hmm_pfns = pfns;
+	hmm_range->notifier_seq =

mmu_interval_read_begin(&userptr-

Quoted 1 line

notifier);

drivers/gpu/drm/xe/xe_hmm.c new file +4 -0
+	hmm_range->notifier = &userptr->notifier;
+	hmm_range->start = start;
+	hmm_range->end = end;
+	hmm_range->pfn_flags_mask = HMM_PFN_REQ_FAULT |

HMM_PFN_REQ_WRITE;

drivers/gpu/drm/xe/xe_hmm.c new file +3 -0
+	/**
+	 * FIXME:
+	 * Set the the dev_private_owner can prevent

hmm_range_fault
to fault

drivers/gpu/drm/xe/xe_hmm.c new file +1 -0
+	 * in the device private pages owned by caller. See

function

drivers/gpu/drm/xe/xe_hmm.c new file +1 -0
+	 * hmm_vma_handle_pte. In multiple GPU case, this should

be
set to the

drivers/gpu/drm/xe/xe_hmm.c new file +1 -0
+	 * device owner of the best migration destination. e.g.,

device0/vm0

drivers/gpu/drm/xe/xe_hmm.c new file +1 -0
+	 * has a page fault, but we have determined the best

placement of

drivers/gpu/drm/xe/xe_hmm.c new file +1 -0
+	 * the fault address should be on device1, we should set

below to

drivers/gpu/drm/xe/xe_hmm.c new file +9 -0
+	 * device1 instead of device0.
+	 */
+	hmm_range->dev_private_owner = vm->xe->drm.dev;
+
+	while (true) {
+		ret = hmm_range_fault(hmm_range);
+		if (time_after(jiffies, timeout))
+			break;
+
Quoted 9 lines

drivers/gpu/drm/xe/xe_hmm.c line 194

Quoted 2 lines
drivers/gpu/drm/xe/xe_hmm.c new file +2 -0
+		if (ret == -EBUSY)
+			continue;

If (ret == -EBUSY) it looks from the hmm_range_fault() implementation
like hmm_range->notifier_seq has become invalid and without calling mmu_interval_read_begin() again, we will end up in an infinite loop?

Quoted 7 lines

I noticed this thing before and had a read_begin in the while loop. But after a second thought, function xe_hmm_populate_range is called inside a mmap lock, so after the read_begin is called above, there can't be a invalidation before mmap unlock. So theoretically EBUSY can't happen?

Oak

Invalidations can happen due to many different things that don't need the mmap lock. File truncation and I think page reclaim are typical examples.

/Thomas

Quoted 45 lines
Quoted 44 lines

/Thomas

Quoted 39 lines
drivers/gpu/drm/xe/xe_hmm.c new file +19 -0
+		break;
+	}
+
+	if (ret)
+		goto free_pfns;
+
+	ret = build_sg(vm->xe, hmm_range, &userptr->sgt, write);
+	if (ret)
+		goto free_pfns;
+
+	mark_range_accessed(hmm_range, write);
+	userptr->sg = &userptr->sgt;
+	userptr->notifier_seq = hmm_range->notifier_seq;
+
+free_pfns:
+	kvfree(pfns);
+	return ret;
+}
+
drivers/gpu/drm/xe/xe_hmm.h new file +11 -0
new file mode 100644
index 000000000000..960f3f6d36ae
@@ -0,0 +1,12 @@
+// SPDX-License-Identifier: MIT
+/*
+ * Copyright © 2024 Intel Corporation
+ */
+
+#include <linux/types.h>
+#include <linux/hmm.h>
+#include "xe_vm_types.h"
+#include "xe_svm.h"
+
+int xe_hmm_populate_range(struct xe_vma *vma, struct hmm_range

*hmm_range,

drivers/gpu/drm/xe/xe_hmm.h new file +1 -0
+						bool write);
author
#42 lore raw

RE: [PATCH 4/5] drm/xe: Helper to populate a userptr or hmmptr

Quoted 339 lines

-----Original Message-----
From: Hellstrom, Thomas <[email protected]>
Sent: Monday, March 18, 2024 11:40 AM
To: [email protected]; Zeng, Oak <[email protected]>
Cc: Brost, Matthew <[email protected]>; Welty, Brian <[email protected]>; [email protected]; Ghimiray, Himal Prasad <[email protected]>
Subject: Re: [PATCH 4/5] drm/xe: Helper to populate a userptr or hmmptr

Hi,

Quoted 325 lines · Oak
Quoted 309 lines

-----Original Message-----
From: Hellstrom, Thomas <[email protected]>
Sent: Monday, March 18, 2024 9:13 AM
To: [email protected]; Zeng, Oak <[email protected]>
Cc: Brost, Matthew <[email protected]>; Welty, Brian <[email protected]>; [email protected]; Ghimiray, Himal Prasad <[email protected]>
Subject: Re: [PATCH 4/5] drm/xe: Helper to populate a userptr or hmmptr

Hi, Oak,

Found another thing, see below:

Quoted 292 lines · Oak Zeng

Add a helper function xe_hmm_populate_range to populate a a userptr or hmmptr range. This functions calls hmm_range_fault to read CPU page tables and populate all pfns/pages of this virtual address range.

If the populated page is system memory page, dma-mapping is performed
to get a dma-address which can be used later for GPU to access pages.

If the populated page is device private page, we calculate the dpa (
device physical address) of the page.

The dma-address or dpa is then saved in userptr's sg table. This is
prepare work to replace the get_user_pages_fast code in userptr code
path. The helper function will also be used to populate hmmptr later.

Co-developed-by Niranjana Vishwanathapura

<[email protected]>
Signed-off-by: Niranjana Vishwanathapura
<[email protected]>
Cc: Matthew Brost <[email protected]>
Cc: Thomas Hellström <[email protected]>
Cc: Brian Welty <[email protected]>
---
 drivers/gpu/drm/xe/Makefile |   3 +-
 drivers/gpu/drm/xe/xe_hmm.c | 213
++++++++++++++++++++++++++++++++++++
 drivers/gpu/drm/xe/xe_hmm.h |  12 ++

 3 files changed, 227 insertions(+), 1 deletion(-)

 create mode 100644 drivers/gpu/drm/xe/xe_hmm.c
 create mode 100644 drivers/gpu/drm/xe/xe_hmm.h

drivers/gpu/drm/xe/Makefile +2 -1
index 840467080e59..29dcbc938b01 100644
@@ -143,7 +143,8 @@ xe-y += xe_bb.o \
  	xe_wait_user_fence.o \
  	xe_wa.o \
  	xe_wopcm.o \
-	xe_svm_devmem.o
+	xe_svm_devmem.o \
+	xe_hmm.o
 
  # graphics hardware monitoring (HWMON) support

 xe-$(CONFIG_HWMON) += xe_hwmon.o

drivers/gpu/drm/xe/xe_hmm.c new file +23 -0
new file mode 100644
index 000000000000..c45c2447d386
@@ -0,0 +1,213 @@
+// SPDX-License-Identifier: MIT
+/*
+ * Copyright © 2024 Intel Corporation
+ */
+
+#include <linux/mmu_notifier.h>
+#include <linux/dma-mapping.h>
+#include <linux/memremap.h>
+#include <linux/swap.h>
+#include <linux/mm.h>
+#include "xe_hmm.h"
+#include "xe_vm.h"
+
+/**
+ * mark_range_accessed() - mark a range is accessed, so core mm
+ * have such information for memory eviction or write back to
+ * hard disk
+ *
+ * @range: the range to mark
+ * @write: if write to this range, we mark pages in this range
+ * as dirty
+ */
+static void mark_range_accessed(struct hmm_range *range, bool

write)

drivers/gpu/drm/xe/xe_hmm.c new file +5 -0
+{
+	struct page *page;
+	u64 i, npages;
+
+	npages = ((range->end - 1) >> PAGE_SHIFT) - (range-
Quoted 1 line

start >>

PAGE_SHIFT) + 1;

drivers/gpu/drm/xe/xe_hmm.c new file +13 -0
+	for (i = 0; i < npages; i++) {
+		page = hmm_pfn_to_page(range->hmm_pfns[i]);
+		if (write) {
+			lock_page(page);
+			set_page_dirty(page);
+			unlock_page(page);
+		}
+		mark_page_accessed(page);
+	}
+}
+
+/**
+ * build_sg() - build a scatter gather table for all the

physical
pages/pfn
+ * in a hmm_range. dma-address is save in sg table and will be used
to program
+ * GPU page table later.
+ *
+ * @xe: the xe device who will access the dma-address in sg table
+ * @range: the hmm range that we build the sg table from. range-

Quoted 1 line

hmm_pfns[]

+ * has the pfn numbers of pages that back up this hmm address range.
+ * @st: pointer to the sg table.
+ * @write: whether we write to this range. This decides dma map direction
+ * for system pages. If write we map it bi-diretional; otherwise
+ * DMA_TO_DEVICE
+ *
+ * All the contiguous pfns will be collapsed into one entry in
+ * the scatter gather table. This is for the convenience of
+ * later on operations to bind address range to GPU page table.
+ *
+ * The dma_address in the sg table will later be used by GPU to
+ * access memory. So if the memory is system memory, we need to
+ * do a dma-mapping so it can be accessed by GPU/DMA. If the memory
+ * is GPU local memory (of the GPU who is going to access memory),
+ * we need gpu dpa (device physical address), and there is no need
+ * of dma-mapping.
+ *
+ * FIXME: dma-mapping for peer gpu device to access remote gpu's
+ * memory. Add this when you support p2p
+ *
+ * This function allocates the storage of the sg table. It is
+ * caller's responsibility to free it calling sg_free_table.
+ *
+ * Returns 0 if successful; -ENOMEM if fails to allocate memory
+ */
+static int build_sg(struct xe_device *xe, struct hmm_range *range,

drivers/gpu/drm/xe/xe_hmm.c new file +9 -0
+			     struct sg_table *st, bool write)
+{
+	struct device *dev = xe->drm.dev;
+	struct scatterlist *sg;
+	u64 i, npages;
+
+	sg = NULL;
+	st->nents = 0;
+	npages = ((range->end - 1) >> PAGE_SHIFT) - (range-
Quoted 1 line

start >>

PAGE_SHIFT) + 1;

drivers/gpu/drm/xe/xe_hmm.c new file +13 -0
+
+	if (unlikely(sg_alloc_table(st, npages, GFP_KERNEL)))
+		return -ENOMEM;
+
+	for (i = 0; i < npages; i++) {
+		struct page *page;
+		unsigned long addr;
+		struct xe_mem_region *mr;
+
+		page = hmm_pfn_to_page(range->hmm_pfns[i]);
+		if (is_device_private_page(page)) {
+			mr = page_to_mem_region(page);
+			addr = vram_pfn_to_dpa(mr, range-
Quoted 1 line

hmm_pfns[i]);

drivers/gpu/drm/xe/xe_hmm.c new file +2 -0
+		} else {
+			addr = dma_map_page(dev, page, 0,

PAGE_SIZE,

drivers/gpu/drm/xe/xe_hmm.c new file +1 -0
+					write ?

DMA_BIDIRECTIONAL :
DMA_TO_DEVICE);

drivers/gpu/drm/xe/xe_hmm.c new file +3 -0
+		}
+
+		if (sg && (addr == (sg_dma_address(sg) + sg-
Quoted 1 line

length))) {

drivers/gpu/drm/xe/xe_hmm.c new file +18 -0
+			sg->length += PAGE_SIZE;
+			sg_dma_len(sg) += PAGE_SIZE;
+			continue;
+		}
+
+		sg =  sg ? sg_next(sg) : st->sgl;
+		sg_dma_address(sg) = addr;
+		sg_dma_len(sg) = PAGE_SIZE;
+		sg->length = PAGE_SIZE;
+		st->nents++;
+	}
+
+	sg_mark_end(sg);
+	return 0;
+}
+
+/**
+ * xe_hmm_populate_range() - Populate physical pages of a

virtual
+ * address range
+ *
+ * @vma: vma has information of the range to populate. only vma
+ * of userptr and hmmptr type can be populated.
+ * @hmm_range: pointer to hmm_range struct. hmm_rang->hmm_pfns
+ * will hold the populated pfns.
+ * @write: populate pages with write permission
+ *
+ * This function populate the physical pages of a virtual
+ * address range. The populated physical pages is saved in
+ * userptr's sg table. It is similar to get_user_pages but call
+ * hmm_range_fault.
+ *
+ * This function also read mmu notifier sequence # (
+ * mmu_interval_read_begin), for the purpose of later
+ * comparison (through mmu_interval_read_retry).
+ *
+ * This must be called with mmap read or write lock held.
+ *
+ * This function allocates the storage of the userptr sg table.
+ * It is caller's responsibility to free it calling sg_free_table.
+ *
+ * returns: 0 for succuss; negative error no on failure
+ */
+int xe_hmm_populate_range(struct xe_vma *vma, struct hmm_range *hmm_range,

drivers/gpu/drm/xe/xe_hmm.c new file +4 -0
+						bool write)
+{
+	unsigned long timeout =
+		jiffies +

msecs_to_jiffies(HMM_RANGE_DEFAULT_TIMEOUT);

drivers/gpu/drm/xe/xe_hmm.c new file +14 -0
+	unsigned long *pfns, flags = HMM_PFN_REQ_FAULT;
+	struct xe_userptr_vma *userptr_vma;
+	struct xe_userptr *userptr;
+	u64 start = vma->gpuva.va.addr;
+	u64 end = start + vma->gpuva.va.range;
+	struct xe_vm *vm = xe_vma_vm(vma);
+	u64 npages;
+	int ret;
+
+	userptr_vma = to_userptr_vma(vma);
+	userptr = &userptr_vma->userptr;
+	mmap_assert_locked(userptr->notifier.mm);
+
+	npages = ((end - 1) >> PAGE_SHIFT) - (start >>

PAGE_SHIFT) +
1;

drivers/gpu/drm/xe/xe_hmm.c new file +1 -0
+	pfns = kvmalloc_array(npages, sizeof(*pfns),

GFP_KERNEL);

drivers/gpu/drm/xe/xe_hmm.c new file +9 -0
+	if (unlikely(!pfns))
+		return -ENOMEM;
+
+	if (write)
+		flags |= HMM_PFN_REQ_WRITE;
+
+	memset64((u64 *)pfns, (u64)flags, npages);
+	hmm_range->hmm_pfns = pfns;
+	hmm_range->notifier_seq =

mmu_interval_read_begin(&userptr-

Quoted 1 line

notifier);

drivers/gpu/drm/xe/xe_hmm.c new file +4 -0
+	hmm_range->notifier = &userptr->notifier;
+	hmm_range->start = start;
+	hmm_range->end = end;
+	hmm_range->pfn_flags_mask = HMM_PFN_REQ_FAULT |

HMM_PFN_REQ_WRITE;

drivers/gpu/drm/xe/xe_hmm.c new file +3 -0
+	/**
+	 * FIXME:
+	 * Set the the dev_private_owner can prevent

hmm_range_fault
to fault

drivers/gpu/drm/xe/xe_hmm.c new file +1 -0
+	 * in the device private pages owned by caller. See

function

drivers/gpu/drm/xe/xe_hmm.c new file +1 -0
+	 * hmm_vma_handle_pte. In multiple GPU case, this should

be
set to the

drivers/gpu/drm/xe/xe_hmm.c new file +1 -0
+	 * device owner of the best migration destination. e.g.,

device0/vm0

drivers/gpu/drm/xe/xe_hmm.c new file +1 -0
+	 * has a page fault, but we have determined the best

placement of

drivers/gpu/drm/xe/xe_hmm.c new file +1 -0
+	 * the fault address should be on device1, we should set

below to

drivers/gpu/drm/xe/xe_hmm.c new file +9 -0
+	 * device1 instead of device0.
+	 */
+	hmm_range->dev_private_owner = vm->xe->drm.dev;
+
+	while (true) {
+		ret = hmm_range_fault(hmm_range);
+		if (time_after(jiffies, timeout))
+			break;
+
Quoted 9 lines

drivers/gpu/drm/xe/xe_hmm.c line 194

Quoted 2 lines
drivers/gpu/drm/xe/xe_hmm.c new file +2 -0
+		if (ret == -EBUSY)
+			continue;

If (ret == -EBUSY) it looks from the hmm_range_fault() implementation
like hmm_range->notifier_seq has become invalid and without calling mmu_interval_read_begin() again, we will end up in an infinite loop?

I noticed this thing before and had a read_begin in the while loop. But after a second thought, function xe_hmm_populate_range is called inside a mmap lock, so after the read_begin is called above, there can't be a invalidation before mmap unlock. So theoretically EBUSY can't happen?

Quoted 5 lines
Quoted 1 line

Oak

Invalidations can happen due to many different things that don't need the mmap lock. File truncation and I think page reclaim are typical examples.

I see. I will move the read_begin into the loop then. Thanks!

Oak

Quoted 49 lines

/Thomas

Quoted 45 lines
Quoted 44 lines

/Thomas

Quoted 39 lines
drivers/gpu/drm/xe/xe_hmm.c new file +19 -0
+		break;
+	}
+
+	if (ret)
+		goto free_pfns;
+
+	ret = build_sg(vm->xe, hmm_range, &userptr->sgt, write);
+	if (ret)
+		goto free_pfns;
+
+	mark_range_accessed(hmm_range, write);
+	userptr->sg = &userptr->sgt;
+	userptr->notifier_seq = hmm_range->notifier_seq;
+
+free_pfns:
+	kvfree(pfns);
+	return ret;
+}
+
drivers/gpu/drm/xe/xe_hmm.h new file +11 -0
new file mode 100644
index 000000000000..960f3f6d36ae
@@ -0,0 +1,12 @@
+// SPDX-License-Identifier: MIT
+/*
+ * Copyright © 2024 Intel Corporation
+ */
+
+#include <linux/types.h>
+#include <linux/hmm.h>
+#include "xe_vm_types.h"
+#include "xe_svm.h"
+
+int xe_hmm_populate_range(struct xe_vma *vma, struct hmm_range

*hmm_range,

drivers/gpu/drm/xe/xe_hmm.h new file +1 -0
+						bool write);
author
#43 lore raw

PATCH 5/5 drm/xe: Use hmm_range_fault to populate user pages

In reply to Oak Zeng

This is an effort to unify hmmptr (aka system allocator) and userptr code. hmm_range_fault is used to populate a virtual address range for both hmmptr and userptr, instead of hmmptr using hmm_range_fault and userptr using get_user_pages_fast.

This also aligns with AMD gpu driver's behavior. In long term, we plan to put some common helpers in this area to drm layer so it can be re-used by different vendors.

---
 drivers/gpu/drm/xe/xe_vm.c | 105 ++-----------------------------------
 1 file changed, 4 insertions(+), 101 deletions(-)
drivers/gpu/drm/xe/xe_vm.c +4 -101
index db3f049a47dc..d6088dcac74a 100644
@@ -38,6 +38,7 @@
 #include "xe_sync.h"
 #include "xe_trace.h"
 #include "xe_wa.h"
+#include "xe_hmm.h"
 
 static struct drm_gem_object *xe_vm_obj(struct xe_vm *vm)
 {
@@ -65,113 +66,15 @@ int xe_vma_userptr_check_repin(struct xe_userptr_vma *uvma)
 
 int xe_vma_userptr_pin_pages(struct xe_userptr_vma *uvma)
 {
-	struct xe_userptr *userptr = &uvma->userptr;
 	struct xe_vma *vma = &uvma->vma;
 	struct xe_vm *vm = xe_vma_vm(vma);
 	struct xe_device *xe = vm->xe;
-	const unsigned long num_pages = xe_vma_size(vma) >> PAGE_SHIFT;
-	struct page **pages;
-	bool in_kthread = !current->mm;
-	unsigned long notifier_seq;
-	int pinned, ret, i;
-	bool read_only = xe_vma_read_only(vma);
+	bool write = !xe_vma_read_only(vma);
+	struct hmm_range hmm_range;
 
 	lockdep_assert_held(&vm->lock);
 	xe_assert(xe, xe_vma_is_userptr(vma));
-retry:
-	if (vma->gpuva.flags & XE_VMA_DESTROYED)
-		return 0;
-
-	notifier_seq = mmu_interval_read_begin(&userptr->notifier);
-	if (notifier_seq == userptr->notifier_seq)
-		return 0;
-
-	pages = kvmalloc_array(num_pages, sizeof(*pages), GFP_KERNEL);
-	if (!pages)
-		return -ENOMEM;
-
-	if (userptr->sg) {
-		dma_unmap_sgtable(xe->drm.dev,
-				  userptr->sg,
-				  read_only ? DMA_TO_DEVICE :
-				  DMA_BIDIRECTIONAL, 0);
-		sg_free_table(userptr->sg);
-		userptr->sg = NULL;
-	}
-
-	pinned = ret = 0;
-	if (in_kthread) {
-		if (!mmget_not_zero(userptr->notifier.mm)) {
-			ret = -EFAULT;
-			goto mm_closed;
-		}
-		kthread_use_mm(userptr->notifier.mm);
-	}
-
-	while (pinned < num_pages) {
-		ret = get_user_pages_fast(xe_vma_userptr(vma) +
-					  pinned * PAGE_SIZE,
-					  num_pages - pinned,
-					  read_only ? 0 : FOLL_WRITE,
-					  &pages[pinned]);
-		if (ret < 0)
-			break;
-
-		pinned += ret;
-		ret = 0;
-	}
-
-	if (in_kthread) {
-		kthread_unuse_mm(userptr->notifier.mm);
-		mmput(userptr->notifier.mm);
-	}
-mm_closed:
-	if (ret)
-		goto out;
-
-	ret = sg_alloc_table_from_pages_segment(&userptr->sgt, pages,
-						pinned, 0,
-						(u64)pinned << PAGE_SHIFT,
-						xe_sg_segment_size(xe->drm.dev),
-						GFP_KERNEL);
-	if (ret) {
-		userptr->sg = NULL;
-		goto out;
-	}
-	userptr->sg = &userptr->sgt;
-
-	ret = dma_map_sgtable(xe->drm.dev, userptr->sg,
-			      read_only ? DMA_TO_DEVICE :
-			      DMA_BIDIRECTIONAL,
-			      DMA_ATTR_SKIP_CPU_SYNC |
-			      DMA_ATTR_NO_KERNEL_MAPPING);
-	if (ret) {
-		sg_free_table(userptr->sg);
-		userptr->sg = NULL;
-		goto out;
-	}
-
-	for (i = 0; i < pinned; ++i) {
-		if (!read_only) {
-			lock_page(pages[i]);
-			set_page_dirty(pages[i]);
-			unlock_page(pages[i]);
-		}
-
-		mark_page_accessed(pages[i]);
-	}
-
-out:
-	release_pages(pages, pinned);
-	kvfree(pages);
-
-	if (!(ret < 0)) {
-		userptr->notifier_seq = notifier_seq;
-		if (xe_vma_userptr_check_repin(uvma) == -EAGAIN)
-			goto retry;
-	}
-
-	return ret < 0 ? ret : 0;
+	return xe_hmm_populate_range(vma, &hmm_range, write);
 }
 
 static bool preempt_fences_waiting(struct xe_vm *vm)
Signature · 1 line
2.26.3
maintainer
#44 lore raw

Re: [PATCH 5/5] drm/xe: Use hmm_range_fault to populate user pages

Quoted 29 lines · Oak Zeng

This is an effort to unify hmmptr (aka system allocator) and userptr code. hmm_range_fault is used to populate a virtual address range for both hmmptr and userptr, instead of hmmptr using hmm_range_fault and userptr using get_user_pages_fast.

This also aligns with AMD gpu driver's behavior. In long term, we plan to put some common helpers in this area to drm layer so it can be re-used by different vendors.

---
 drivers/gpu/drm/xe/xe_vm.c | 105 ++-----------------------------------
 1 file changed, 4 insertions(+), 101 deletions(-)
drivers/gpu/drm/xe/xe_vm.c +1 -0
index db3f049a47dc..d6088dcac74a 100644
@@ -38,6 +38,7 @@
 #include "xe_sync.h"
 #include "xe_trace.h"
 #include "xe_wa.h"
+#include "xe_hmm.h"
 
 static struct drm_gem_object *xe_vm_obj(struct xe_vm *vm)
 {

drivers/gpu/drm/xe/xe_vm.c int xe_vma_userptr_check_repin(struct xe_userptr_vma *uvma) · line 67

Quoted 2 lines
drivers/gpu/drm/xe/xe_vm.c +0 -0
@@ -65,113 +66,15 @@ int xe_vma_userptr_check_repin(struct xe_userptr_vma *uvma)
 
 int xe_vma_userptr_pin_pages(struct xe_userptr_vma *uvma)

See my comments in the previous patch about layer, those comments are valid here too.

Quoted 9 lines
drivers/gpu/drm/xe/xe_vm.c +0 -5
 {
-	struct xe_userptr *userptr = &uvma->userptr;
 	struct xe_vma *vma = &uvma->vma;
 	struct xe_vm *vm = xe_vma_vm(vma);
 	struct xe_device *xe = vm->xe;
-	const unsigned long num_pages = xe_vma_size(vma) >> PAGE_SHIFT;
-	struct page **pages;
-	bool in_kthread = !current->mm;
-	unsigned long notifier_seq;

drivers/gpu/drm/xe/xe_vm.c int xe_vma_userptr_check_repin(struct xe_userptr_vma *uvma) · line 83

Quoted 10 lines
drivers/gpu/drm/xe/xe_vm.c +2 -5
-	int pinned, ret, i;
-	bool read_only = xe_vma_read_only(vma);
+	bool write = !xe_vma_read_only(vma);
+	struct hmm_range hmm_range;
 
 	lockdep_assert_held(&vm->lock);
 	xe_assert(xe, xe_vma_is_userptr(vma));
-retry:
-	if (vma->gpuva.flags & XE_VMA_DESTROYED)
-		return 0;

^^^
This should not be dropped. Both the vma->gpuva.flags & XE_VMA_DESTROYED and userptr invalidation check retry loop should still be in here.

Quoted 7 lines
drivers/gpu/drm/xe/xe_vm.c +0 -7
-
-	notifier_seq = mmu_interval_read_begin(&userptr->notifier);
-	if (notifier_seq == userptr->notifier_seq)
-		return 0;
-
-	pages = kvmalloc_array(num_pages, sizeof(*pages), GFP_KERNEL);
-	if (!pages)

drivers/gpu/drm/xe/xe_vm.c int xe_vma_userptr_check_repin(struct xe_userptr_vma *uvma) · line 100

Quoted 10 lines
drivers/gpu/drm/xe/xe_vm.c +0 -10
-		return -ENOMEM;
-
-	if (userptr->sg) {
-		dma_unmap_sgtable(xe->drm.dev,
-				  userptr->sg,
-				  read_only ? DMA_TO_DEVICE :
-				  DMA_BIDIRECTIONAL, 0);
-		sg_free_table(userptr->sg);
-		userptr->sg = NULL;
-	}

^^^
Likewise, I don't think this should be dropped either.

drivers/gpu/drm/xe/xe_vm.c int xe_vma_userptr_check_repin(struct xe_userptr_vma *uvma) · line 109

Quoted 9 lines
drivers/gpu/drm/xe/xe_vm.c +0 -9
-
-	pinned = ret = 0;
-	if (in_kthread) {
-		if (!mmget_not_zero(userptr->notifier.mm)) {
-			ret = -EFAULT;
-			goto mm_closed;
-		}
-		kthread_use_mm(userptr->notifier.mm);
-	}

^^^
Nor this.

drivers/gpu/drm/xe/xe_vm.c int xe_vma_userptr_check_repin(struct xe_userptr_vma *uvma) · line 122

Quoted 13 lines
drivers/gpu/drm/xe/xe_vm.c +0 -13
-
-	while (pinned < num_pages) {
-		ret = get_user_pages_fast(xe_vma_userptr(vma) +
-					  pinned * PAGE_SIZE,
-					  num_pages - pinned,
-					  read_only ? 0 : FOLL_WRITE,
-					  &pages[pinned]);
-		if (ret < 0)
-			break;
-
-		pinned += ret;
-		ret = 0;
-	}

^^^
We should be replacing this.

Quoted 34 lines
drivers/gpu/drm/xe/xe_vm.c +0 -34
-
-	if (in_kthread) {
-		kthread_unuse_mm(userptr->notifier.mm);
-		mmput(userptr->notifier.mm);
-	}
-mm_closed:
-	if (ret)
-		goto out;
-
-	ret = sg_alloc_table_from_pages_segment(&userptr->sgt, pages,
-						pinned, 0,
-						(u64)pinned << PAGE_SHIFT,
-						xe_sg_segment_size(xe->drm.dev),
-						GFP_KERNEL);
-	if (ret) {
-		userptr->sg = NULL;
-		goto out;
-	}
-	userptr->sg = &userptr->sgt;
-
-	ret = dma_map_sgtable(xe->drm.dev, userptr->sg,
-			      read_only ? DMA_TO_DEVICE :
-			      DMA_BIDIRECTIONAL,
-			      DMA_ATTR_SKIP_CPU_SYNC |
-			      DMA_ATTR_NO_KERNEL_MAPPING);
-	if (ret) {
-		sg_free_table(userptr->sg);
-		userptr->sg = NULL;
-		goto out;
-	}
-
-	for (i = 0; i < pinned; ++i) {
-		if (!read_only) {
-			lock_page(pages[i]);

drivers/gpu/drm/xe/xe_vm.c int xe_vma_userptr_check_repin(struct xe_userptr_vma *uvma) · line 166

Quoted 10 lines
drivers/gpu/drm/xe/xe_vm.c +0 -10
-			set_page_dirty(pages[i]);
-			unlock_page(pages[i]);
-		}
-
-		mark_page_accessed(pages[i]);
-	}
-
-out:
-	release_pages(pages, pinned);
-	kvfree(pages);

^^^
Through here (minus existing the kthread) with hmm call. I guess the kthread enter / exit could be in the hmm layer too.

Matt

Quoted 15 lines
drivers/gpu/drm/xe/xe_vm.c +1 -8
-
-	if (!(ret < 0)) {
-		userptr->notifier_seq = notifier_seq;
-		if (xe_vma_userptr_check_repin(uvma) == -EAGAIN)
-			goto retry;
-	}
-
-	return ret < 0 ? ret : 0;
+	return xe_hmm_populate_range(vma, &hmm_range, write);
 }
 
 static bool preempt_fences_waiting(struct xe_vm *vm)
Signature · 1 line
2.26.3
author
#45 lore raw

RE: [PATCH 5/5] drm/xe: Use hmm_range_fault to populate user pages

Hi Matt,

Quoted 61 lines

-----Original Message-----
From: Brost, Matthew <[email protected]>
Sent: Thursday, March 14, 2024 4:55 PM
To: Zeng, Oak <[email protected]>
Cc: [email protected]; Hellstrom, Thomas <[email protected]>; [email protected]; Welty, Brian <[email protected]>; Ghimiray, Himal Prasad <[email protected]>
Subject: Re: [PATCH 5/5] drm/xe: Use hmm_range_fault to populate user pages

drivers/gpu/drm/xe/xe_vm.c int xe_vma_userptr_check_repin(struct

Quoted 29 lines · Oak Zeng

This is an effort to unify hmmptr (aka system allocator) and userptr code. hmm_range_fault is used to populate a virtual address range for both hmmptr and userptr, instead of hmmptr using hmm_range_fault and userptr using get_user_pages_fast.

This also aligns with AMD gpu driver's behavior. In long term, we plan to put some common helpers in this area to drm layer so it can be re-used by different vendors.

---
 drivers/gpu/drm/xe/xe_vm.c | 105 ++-----------------------------------
 1 file changed, 4 insertions(+), 101 deletions(-)
drivers/gpu/drm/xe/xe_vm.c +1 -0
index db3f049a47dc..d6088dcac74a 100644
@@ -38,6 +38,7 @@
 #include "xe_sync.h"
 #include "xe_trace.h"
 #include "xe_wa.h"
+#include "xe_hmm.h"
 
 static struct drm_gem_object *xe_vm_obj(struct xe_vm *vm)
 {
@@ -65,113 +66,15 @@ int xe_vma_userptr_check_repin(struct

xe_userptr_vma *uvma)

drivers/gpu/drm/xe/xe_vm.c int xe_vma_userptr_check_repin(struct xe_userptr_vma *uvma) · line 67

Quoted 2 lines
drivers/gpu/drm/xe/xe_vm.c +0 -0
 int xe_vma_userptr_pin_pages(struct xe_userptr_vma *uvma)

See my comments in the previous patch about layer, those comments are valid here too.

Quoted 14 lines
drivers/gpu/drm/xe/xe_vm.c +2 -7
 {
-	struct xe_userptr *userptr = &uvma->userptr;
 	struct xe_vma *vma = &uvma->vma;
 	struct xe_vm *vm = xe_vma_vm(vma);
 	struct xe_device *xe = vm->xe;
-	const unsigned long num_pages = xe_vma_size(vma) >> PAGE_SHIFT;
-	struct page **pages;
-	bool in_kthread = !current->mm;
-	unsigned long notifier_seq;
-	int pinned, ret, i;
-	bool read_only = xe_vma_read_only(vma);
+	bool write = !xe_vma_read_only(vma);
+	struct hmm_range hmm_range;
 
Quoted 9 lines

drivers/gpu/drm/xe/xe_vm.c int xe_vma_userptr_check_repin(struct xe_userptr_vma *uvma) · line 83

Quoted 5 lines
drivers/gpu/drm/xe/xe_vm.c +0 -3
 	lockdep_assert_held(&vm->lock);
 	xe_assert(xe, xe_vma_is_userptr(vma));
-retry:
-	if (vma->gpuva.flags & XE_VMA_DESTROYED)
-		return 0;

^^^
This should not be dropped. Both the vma->gpuva.flags & XE_VMA_DESTROYED and userptr invalidation check retry loop should still be in here.

I will move this check into hmm.c

Quoted 13 lines
Quoted 13 lines
drivers/gpu/drm/xe/xe_vm.c +0 -12
-
-	notifier_seq = mmu_interval_read_begin(&userptr->notifier);
-	if (notifier_seq == userptr->notifier_seq)
-		return 0;
-
-	pages = kvmalloc_array(num_pages, sizeof(*pages), GFP_KERNEL);
-	if (!pages)
-		return -ENOMEM;
-
-	if (userptr->sg) {
-		dma_unmap_sgtable(xe->drm.dev,
-				  userptr->sg,
Quoted 8 lines

drivers/gpu/drm/xe/xe_vm.c int xe_vma_userptr_check_repin(struct xe_userptr_vma *uvma) · line 100

Quoted 5 lines
drivers/gpu/drm/xe/xe_vm.c +0 -5
-				  read_only ? DMA_TO_DEVICE :
-				  DMA_BIDIRECTIONAL, 0);
-		sg_free_table(userptr->sg);
-		userptr->sg = NULL;
-	}

^^^
Likewise, I don't think this should be dropped either.

Will move to hmm.c

Quoted 13 lines

drivers/gpu/drm/xe/xe_vm.c int xe_vma_userptr_check_repin(struct xe_userptr_vma *uvma) · line 109

Quoted 10 lines
drivers/gpu/drm/xe/xe_vm.c +0 -9
-
-	pinned = ret = 0;
-	if (in_kthread) {
-		if (!mmget_not_zero(userptr->notifier.mm)) {
-			ret = -EFAULT;
-			goto mm_closed;
-		}
-		kthread_use_mm(userptr->notifier.mm);
-	}

^^^
Nor this.

Will move to hmm.c

Quoted 59 lines
Quoted 4 lines
drivers/gpu/drm/xe/xe_vm.c +0 -3
-
-	while (pinned < num_pages) {
-		ret = get_user_pages_fast(xe_vma_userptr(vma) +

drivers/gpu/drm/xe/xe_vm.c int xe_vma_userptr_check_repin(struct xe_userptr_vma *uvma) · line 122

Quoted 10 lines
drivers/gpu/drm/xe/xe_vm.c +0 -10
-					  pinned * PAGE_SIZE,
-					  num_pages - pinned,
-					  read_only ? 0 : FOLL_WRITE,
-					  &pages[pinned]);
-		if (ret < 0)
-			break;
-
-		pinned += ret;
-		ret = 0;
-	}

^^^
We should be replacing this.

Quoted 41 lines
drivers/gpu/drm/xe/xe_vm.c +0 -40
-
-	if (in_kthread) {
-		kthread_unuse_mm(userptr->notifier.mm);
-		mmput(userptr->notifier.mm);
-	}
-mm_closed:
-	if (ret)
-		goto out;
-
-	ret = sg_alloc_table_from_pages_segment(&userptr->sgt, pages,
-						pinned, 0,
-						(u64)pinned << PAGE_SHIFT,
-						xe_sg_segment_size(xe-
 drm.dev),
-						GFP_KERNEL);
-	if (ret) {
-		userptr->sg = NULL;
-		goto out;
-	}
-	userptr->sg = &userptr->sgt;
-
-	ret = dma_map_sgtable(xe->drm.dev, userptr->sg,
-			      read_only ? DMA_TO_DEVICE :
-			      DMA_BIDIRECTIONAL,
-			      DMA_ATTR_SKIP_CPU_SYNC |
-			      DMA_ATTR_NO_KERNEL_MAPPING);
-	if (ret) {
-		sg_free_table(userptr->sg);
-		userptr->sg = NULL;
-		goto out;
-	}
-
-	for (i = 0; i < pinned; ++i) {
-		if (!read_only) {
-			lock_page(pages[i]);
-			set_page_dirty(pages[i]);
-			unlock_page(pages[i]);
-		}
-
-		mark_page_accessed(pages[i]);
-	}
Quoted 8 lines

drivers/gpu/drm/xe/xe_vm.c int xe_vma_userptr_check_repin(struct xe_userptr_vma *uvma) · line 166

Quoted 4 lines
drivers/gpu/drm/xe/xe_vm.c +0 -4
-
-out:
-	release_pages(pages, pinned);
-	kvfree(pages);

^^^
Through here (minus existing the kthread) with hmm call. I guess the kthread enter / exit could be in the hmm layer too.

Moved missing parts to hmm.c. will send out v1.

Thanks a lot for the reviewing Matt!

Oak

Quoted 18 lines

Matt

Quoted 15 lines
drivers/gpu/drm/xe/xe_vm.c +1 -9
-
-	if (!(ret < 0)) {
-		userptr->notifier_seq = notifier_seq;
-		if (xe_vma_userptr_check_repin(uvma) == -EAGAIN)
-			goto retry;
-	}
-
-	return ret < 0 ? ret : 0;
+	return xe_hmm_populate_range(vma, &hmm_range, write);
 }
 
 static bool preempt_fences_waiting(struct xe_vm *vm)
--

2.26.3

#46 lore raw

✓ CI.Patch_applied: success for Use hmm_range_fault to populate user page

In reply to Oak Zeng
== Series Details ==

== Series Details ==

Series: Use hmm_range_fault to populate user page
URL : https://patchwork.freedesktop.org/series/131117/
State : success

== Summary ==

=== Applying kernel patches on branch 'drm-tip' with base: ===
Base commit: 790a1d4e546a drm-tip: 2024y-03m-13d-20h-00m-39s UTC integration manifest === git am output follows ===
.git/rebase-apply/patch:198: new blank line at EOF.
+
warning: 1 line adds whitespace errors.
.git/rebase-apply/patch:241: new blank line at EOF.
+
warning: 1 line adds whitespace errors.
Applying: drm/xe/svm: Remap and provide memmap backing for GPU vram
Applying: drm/xe: Helper to get memory region from tile
Applying: drm/xe: Helper to get dpa from pfn
Applying: drm/xe: Helper to populate a userptr or hmmptr
Applying: drm/xe: Use hmm_range_fault to populate user pages

#47 lore raw

✗ CI.checkpatch: warning for Use hmm_range_fault to populate user page

In reply to Oak Zeng
== Series Details ==

== Series Details ==

Series: Use hmm_range_fault to populate user page
URL : https://patchwork.freedesktop.org/series/131117/
State : warning

== Summary ==

+ KERNEL=/kernel
+ git clone https://gitlab.freedesktop.org/drm/maintainer-tools mt Cloning into 'mt'...
warning: redirecting to https://gitlab.freedesktop.org/drm/maintainer-tools.git/
+ git -C mt rev-list -n1 origin/master a9eb1ac8298ef9f9146567c29fa762d8e9efa1ef
+ cd /kernel
+ git config --global --add safe.directory /kernel
+ git log -n1
commit 949ff440ef2fd98f08340e015405731c9c20401c

Author: Oak Zeng <[email protected]>
Date:   Wed Mar 13 23:35:53 2024 -0400

drm/xe: Use hmm_range_fault to populate user pages

This is an effort to unify hmmptr (aka system allocator) and userptr code. hmm_range_fault is used to populate a virtual address range for both hmmptr and userptr, instead of hmmptr using hmm_range_fault and userptr using get_user_pages_fast.

This also aligns with AMD gpu driver's behavior. In long term, we plan to put some common helpers in this area to drm layer so it can be re-used by different vendors.

+ /mt/dim checkpatch 790a1d4e546a1d7f1cc5316c77f21379a4083250 drm-intel 835de08a6cb6 drm/xe/svm: Remap and provide memmap backing for GPU vram Traceback (most recent call last):
File "scripts/spdxcheck.py", line 6, in <module>
from ply import lex, yacc
ModuleNotFoundError: No module named 'ply'
-:58: WARNING:LEADING_SPACE: please, no spaces at the start of a line #58: FILE: drivers/gpu/drm/xe/xe_device_types.h:103:
+ struct dev_pagemap pagemap;$

-:64: WARNING:LEADING_SPACE: please, no spaces at the start of a line #64: FILE: drivers/gpu/drm/xe/xe_device_types.h:109:
+ resource_size_t hpa_base;$

-:93: WARNING:LEADING_SPACE: please, no spaces at the start of a line #93: FILE: drivers/gpu/drm/xe/xe_mmio.c:359:
+ struct xe_tile *tile;$

-:94: WARNING:LEADING_SPACE: please, no spaces at the start of a line #94: FILE: drivers/gpu/drm/xe/xe_mmio.c:360:
+ u8 id;$

-:107: WARNING:FILE_PATH_CHANGES: added, moved or deleted file(s), does MAINTAINERS need updating? #107:
new file mode 100644

-:112: WARNING:SPDX_LICENSE_TAG: Improper SPDX comment style for 'drivers/gpu/drm/xe/xe_svm.h', please use '/*' instead #112: FILE: drivers/gpu/drm/xe/xe_svm.h:1:
+// SPDX-License-Identifier: MIT

-:112: WARNING:SPDX_LICENSE_TAG: Missing or malformed SPDX-License-Identifier tag in line 1 #112: FILE: drivers/gpu/drm/xe/xe_svm.h:1:
+// SPDX-License-Identifier: MIT

-:144: CHECK:LINE_SPACING: Please don't use multiple blank lines #144: FILE: drivers/gpu/drm/xe/xe_svm_devmem.c:13:
+
+

-:194: CHECK:PARENTHESIS_ALIGNMENT: Alignment should match open parenthesis #194: FILE: drivers/gpu/drm/xe/xe_svm_devmem.c:63:
+ drm_err(&tile->xe->drm, "Failed to remap tile %d memory, errno %d\n",
+ tile->id, ret);

-:200: CHECK:PARENTHESIS_ALIGNMENT: Alignment should match open parenthesis #200: FILE: drivers/gpu/drm/xe/xe_svm_devmem.c:69:
+ drm_info(&tile->xe->drm, "Added tile %d memory [%llx-%llx] to devm, remapped to %pr\n",
+ tile->id, mr->io_start, mr->io_start + mr->usable_size, res);

-:212: WARNING:IF_0: Consider removing the code enclosed by this #if 0 and its #endif #212: FILE: drivers/gpu/drm/xe/xe_svm_devmem.c:81:
+#if 0

-:218: CHECK:PARENTHESIS_ALIGNMENT: Alignment should match open parenthesis #218: FILE: drivers/gpu/drm/xe/xe_svm_devmem.c:87:
+ devm_release_mem_region(dev, mr->pagemap.range.start,
+ mr->pagemap.range.end - mr->pagemap.range.start +1);

-:218: CHECK:SPACING: spaces preferred around that '+' (ctx:WxV) #218: FILE: drivers/gpu/drm/xe/xe_svm_devmem.c:87:
+ mr->pagemap.range.end - mr->pagemap.range.start +1);
^

total: 0 errors, 8 warnings, 5 checks, 159 lines checked 2d11ab5ab4ca drm/xe: Helper to get memory region from tile
-:7: WARNING:COMMIT_MESSAGE: Missing commit description - Add an appropriate one

total: 0 errors, 1 warnings, 0 checks, 9 lines checked 244ff8649c11 drm/xe: Helper to get dpa from pfn
-:24: WARNING:LINE_SPACING: Missing a blank line after declarations #24: FILE: drivers/gpu/drm/xe/xe_device_types.h:583:
+ u64 offset = (pfn << PAGE_SHIFT) - mr->hpa_base;
+ dpa = mr->dpa_base + offset;

total: 0 errors, 1 warnings, 0 checks, 12 lines checked
c0b901d8869e drm/xe: Helper to populate a userptr or hmmptr
Traceback (most recent call last):
File "scripts/spdxcheck.py", line 6, in <module>
from ply import lex, yacc
ModuleNotFoundError: No module named 'ply'
-:46: WARNING:FILE_PATH_CHANGES: added, moved or deleted file(s), does MAINTAINERS need updating? #46:
new file mode 100644

-:123: CHECK:PARENTHESIS_ALIGNMENT: Alignment should match open parenthesis #123: FILE: drivers/gpu/drm/xe/xe_hmm.c:73:
+static int build_sg(struct xe_device *xe, struct hmm_range *range,
+ struct sg_table *st, bool write)

-:147: CHECK:PARENTHESIS_ALIGNMENT: Alignment should match open parenthesis #147: FILE: drivers/gpu/drm/xe/xe_hmm.c:97:
+ addr = dma_map_page(dev, page, 0, PAGE_SIZE,
+ write ? DMA_BIDIRECTIONAL : DMA_TO_DEVICE);

-:194: CHECK:PARENTHESIS_ALIGNMENT: Alignment should match open parenthesis #194: FILE: drivers/gpu/drm/xe/xe_hmm.c:144:
+int xe_hmm_populate_range(struct xe_vma *vma, struct hmm_range *hmm_range,
+ bool write)

-:228: WARNING:REPEATED_WORD: Possible repeated word: 'the' #228: FILE: drivers/gpu/drm/xe/xe_hmm.c:178:
+ * Set the the dev_private_owner can prevent hmm_range_fault to fault

-:270: WARNING:SPDX_LICENSE_TAG: Improper SPDX comment style for 'drivers/gpu/drm/xe/xe_hmm.h', please use '/*' instead #270: FILE: drivers/gpu/drm/xe/xe_hmm.h:1:
+// SPDX-License-Identifier: MIT

-:270: WARNING:SPDX_LICENSE_TAG: Missing or malformed SPDX-License-Identifier tag in line 1 #270: FILE: drivers/gpu/drm/xe/xe_hmm.h:1:
+// SPDX-License-Identifier: MIT

-:281: CHECK:PARENTHESIS_ALIGNMENT: Alignment should match open parenthesis #281: FILE: drivers/gpu/drm/xe/xe_hmm.h:12:
+int xe_hmm_populate_range(struct xe_vma *vma, struct hmm_range *hmm_range,
+ bool write);

total: 0 errors, 4 warnings, 4 checks, 234 lines checked 949ff440ef2f drm/xe: Use hmm_range_fault to populate user pages

#48 lore raw

✗ CI.KUnit: failure for Use hmm_range_fault to populate user page

In reply to Oak Zeng
== Series Details ==

== Series Details ==

Series: Use hmm_range_fault to populate user page
URL : https://patchwork.freedesktop.org/series/131117/
State : failure

== Summary ==

+ trap cleanup EXIT
+ /kernel/tools/testing/kunit/kunit.py run --kunitconfig /kernel/drivers/gpu/drm/xe/.kunitconfig ERROR:root:../arch/x86/um/user-offsets.c:17:6: warning: no previous prototype for ‘foo’ [-Wmissing-prototypes] 17 | void foo(void)

      |      ^~~

In file included from ../arch/um/kernel/asm-offsets.c:1:
../arch/x86/um/shared/sysdep/kernel-offsets.h:9:6: warning: no previous prototype for ‘foo’ [-Wmissing-prototypes]
9 | void foo(void)
| ^~~
../arch/x86/um/bugs_64.c:9:6: warning: no previous prototype for ‘arch_check_bugs’ [-Wmissing-prototypes]
9 | void arch_check_bugs(void)
| ^~~~~~~~~~~~~~~
../arch/x86/um/bugs_64.c:13:6: warning: no previous prototype for ‘arch_examine_signal’ [-Wmissing-prototypes]
13 | void arch_examine_signal(int sig, struct uml_pt_regs *regs)

      |      ^~~~~~~~~~~~~~~~~~~
../arch/x86/um/fault.c:18:5: warning: no previous prototype for ‘arch_fixup’ [-Wmissing-prototypes]
   18 | int arch_fixup(unsigned long address, struct uml_pt_regs *regs)
      |     ^~~~~~~~~~
../arch/x86/um/os-Linux/registers.c:146:15: warning: no previous prototype for ‘get_thread_reg’ [-Wmissing-prototypes]
  146 | unsigned long get_thread_reg(int reg, jmp_buf *buf)
      |               ^~~~~~~~~~~~~~
../arch/x86/um/vdso/um_vdso.c:16:5: warning: no previous prototype for ‘__vdso_clock_gettime’ [-Wmissing-prototypes]
   16 | int __vdso_clock_gettime(clockid_t clock, struct __kernel_old_timespec *ts)
      |     ^~~~~~~~~~~~~~~~~~~~
../arch/x86/um/vdso/um_vdso.c:30:5: warning: no previous prototype for ‘__vdso_gettimeofday’ [-Wmissing-prototypes]
   30 | int __vdso_gettimeofday(struct __kernel_old_timeval *tv, struct timezone *tz)
      |     ^~~~~~~~~~~~~~~~~~~
../arch/x86/um/vdso/um_vdso.c:44:21: warning: no previous prototype for ‘__vdso_time’ [-Wmissing-prototypes]
   44 | __kernel_old_time_t __vdso_time(__kernel_old_time_t *t)
      |                     ^~~~~~~~~~~
../arch/x86/um/vdso/um_vdso.c:57:1: warning: no previous prototype for ‘__vdso_getcpu’ [-Wmissing-prototypes]
   57 | __vdso_getcpu(unsigned *cpu, unsigned *node, struct getcpu_cache *unused)
      | ^~~~~~~~~~~~~

../arch/x86/um/os-Linux/mcontext.c:7:6: warning: no previous prototype for ‘get_regs_from_mc’ [-Wmissing-prototypes]
7 | void get_regs_from_mc(struct uml_pt_regs *regs, mcontext_t *mc)
| ^~~~~~~~~~~~~~~~
../arch/um/os-Linux/skas/process.c:107:6: warning: no previous prototype for ‘wait_stub_done’ [-Wmissing-prototypes]
107 | void wait_stub_done(int pid)

      |      ^~~~~~~~~~~~~~
../arch/um/os-Linux/skas/process.c:683:6: warning: no previous prototype for ‘__switch_mm’ [-Wmissing-prototypes]
  683 | void __switch_mm(struct mm_id *mm_idp)
      |      ^~~~~~~~~~~
../arch/um/os-Linux/main.c:187:7: warning: no previous prototype for ‘__wrap_malloc’ [-Wmissing-prototypes]
  187 | void *__wrap_malloc(int size)
      |       ^~~~~~~~~~~~~
../arch/um/os-Linux/main.c:208:7: warning: no previous prototype for ‘__wrap_calloc’ [-Wmissing-prototypes]
  208 | void *__wrap_calloc(int n, int size)
      |       ^~~~~~~~~~~~~
../arch/um/os-Linux/main.c:222:6: warning: no previous prototype for ‘__wrap_free’ [-Wmissing-prototypes]
  222 | void __wrap_free(void *ptr)
      |      ^~~~~~~~~~~
../arch/um/os-Linux/mem.c:28:6: warning: no previous prototype for ‘kasan_map_memory’ [-Wmissing-prototypes]
   28 | void kasan_map_memory(void *start, size_t len)
      |      ^~~~~~~~~~~~~~~~
../arch/um/os-Linux/mem.c:212:13: warning: no previous prototype for ‘check_tmpexec’ [-Wmissing-prototypes]
  212 | void __init check_tmpexec(void)
      |             ^~~~~~~~~~~~~
../arch/x86/um/ptrace_64.c:111:5: warning: no previous prototype for ‘poke_user’ [-Wmissing-prototypes]
  111 | int poke_user(struct task_struct *child, long addr, long data)
      |     ^~~~~~~~~
../arch/x86/um/ptrace_64.c:171:5: warning: no previous prototype for ‘peek_user’ [-Wmissing-prototypes]
  171 | int peek_user(struct task_struct *child, long addr, long data)
      |     ^~~~~~~~~
../arch/um/kernel/skas/mmu.c:17:5: warning: no previous prototype for ‘init_new_context’ [-Wmissing-prototypes]
   17 | int init_new_context(struct task_struct *task, struct mm_struct *mm)
      |     ^~~~~~~~~~~~~~~~
../arch/um/kernel/skas/mmu.c:60:6: warning: no previous prototype for ‘destroy_context’ [-Wmissing-prototypes]
   60 | void destroy_context(struct mm_struct *mm)
      |      ^~~~~~~~~~~~~~~
../arch/um/os-Linux/signal.c:75:6: warning: no previous prototype for ‘sig_handler’ [-Wmissing-prototypes]
   75 | void sig_handler(int sig, struct siginfo *si, mcontext_t *mc)
      |      ^~~~~~~~~~~
../arch/um/os-Linux/signal.c:111:6: warning: no previous prototype for ‘timer_alarm_handler’ [-Wmissing-prototypes]
  111 | void timer_alarm_handler(int sig, struct siginfo *unused_si, mcontext_t *mc)
      |      ^~~~~~~~~~~~~~~~~~~
../arch/um/os-Linux/start_up.c:301:12: warning: no previous prototype for ‘parse_iomem’ [-Wmissing-prototypes]
  301 | int __init parse_iomem(char *str, int *add)
      |            ^~~~~~~~~~~
../arch/um/kernel/skas/process.c:36:12: warning: no previous prototype for ‘start_uml’ [-Wmissing-prototypes]
   36 | int __init start_uml(void)
      |            ^~~~~~~~~
../arch/x86/um/signal.c:560:6: warning: no previous prototype for ‘sys_rt_sigreturn’ [-Wmissing-prototypes]
  560 | long sys_rt_sigreturn(void)
      |      ^~~~~~~~~~~~~~~~
../arch/x86/um/syscalls_64.c:48:6: warning: no previous prototype for ‘arch_switch_to’ [-Wmissing-prototypes]
   48 | void arch_switch_to(struct task_struct *to)
      |      ^~~~~~~~~~~~~~
../arch/um/kernel/mem.c:202:8: warning: no previous prototype for ‘pgd_alloc’ [-Wmissing-prototypes]
  202 | pgd_t *pgd_alloc(struct mm_struct *mm)
      |        ^~~~~~~~~
../arch/um/kernel/mem.c:215:7: warning: no previous prototype for ‘uml_kmalloc’ [-Wmissing-prototypes]
  215 | void *uml_kmalloc(int size, int flags)
      |       ^~~~~~~~~~~
../arch/um/kernel/process.c:51:5: warning: no previous prototype for ‘pid_to_processor_id’ [-Wmissing-prototypes]
   51 | int pid_to_processor_id(int pid)
      |     ^~~~~~~~~~~~~~~~~~~
../arch/um/kernel/process.c:87:7: warning: no previous prototype for ‘__switch_to’ [-Wmissing-prototypes]
   87 | void *__switch_to(struct task_struct *from, struct task_struct *to)
      |       ^~~~~~~~~~~
../arch/um/kernel/process.c:140:6: warning: no previous prototype for ‘fork_handler’ [-Wmissing-prototypes]
  140 | void fork_handler(void)
      |      ^~~~~~~~~~~~
../arch/um/kernel/process.c:217:6: warning: no previous prototype for ‘arch_cpu_idle’ [-Wmissing-prototypes]
  217 | void arch_cpu_idle(void)
      |      ^~~~~~~~~~~~~
../arch/um/kernel/process.c:253:5: warning: no previous prototype for ‘copy_to_user_proc’ [-Wmissing-prototypes]
  253 | int copy_to_user_proc(void __user *to, void *from, int size)
      |     ^~~~~~~~~~~~~~~~~
../arch/um/kernel/process.c:263:5: warning: no previous prototype for ‘clear_user_proc’ [-Wmissing-prototypes]
  263 | int clear_user_proc(void __user *buf, int size)
      |     ^~~~~~~~~~~~~~~
../arch/um/kernel/process.c:271:6: warning: no previous prototype for ‘set_using_sysemu’ [-Wmissing-prototypes]
  271 | void set_using_sysemu(int value)
      |      ^~~~~~~~~~~~~~~~
../arch/um/kernel/process.c:278:5: warning: no previous prototype for ‘get_using_sysemu’ [-Wmissing-prototypes]
  278 | int get_using_sysemu(void)
      |     ^~~~~~~~~~~~~~~~
../arch/um/kernel/process.c:316:12: warning: no previous prototype for ‘make_proc_sysemu’ [-Wmissing-prototypes]
  316 | int __init make_proc_sysemu(void)
      |            ^~~~~~~~~~~~~~~~
../arch/um/kernel/process.c:348:15: warning: no previous prototype for ‘arch_align_stack’ [-Wmissing-prototypes]
  348 | unsigned long arch_align_stack(unsigned long sp)
      |               ^~~~~~~~~~~~~~~~
../arch/um/kernel/reboot.c:45:6: warning: no previous prototype for ‘machine_restart’ [-Wmissing-prototypes]
   45 | void machine_restart(char * __unused)
      |      ^~~~~~~~~~~~~~~
../arch/um/kernel/reboot.c:51:6: warning: no previous prototype for ‘machine_power_off’ [-Wmissing-prototypes]
   51 | void machine_power_off(void)
      |      ^~~~~~~~~~~~~~~~~
../arch/um/kernel/reboot.c:57:6: warning: no previous prototype for ‘machine_halt’ [-Wmissing-prototypes]
   57 | void machine_halt(void)
      |      ^~~~~~~~~~~~
../arch/um/kernel/tlb.c:579:6: warning: no previous prototype for ‘flush_tlb_mm_range’ [-Wmissing-prototypes]
  579 | void flush_tlb_mm_range(struct mm_struct *mm, unsigned long start,
      |      ^~~~~~~~~~~~~~~~~~
../arch/um/kernel/tlb.c:594:6: warning: no previous prototype for ‘force_flush_all’ [-Wmissing-prototypes]
  594 | void force_flush_all(void)
      |      ^~~~~~~~~~~~~~~
../arch/um/kernel/um_arch.c:408:19: warning: no previous prototype for ‘read_initrd’ [-Wmissing-prototypes]
  408 | int __init __weak read_initrd(void)
      |                   ^~~~~~~~~~~
../arch/um/kernel/um_arch.c:461:7: warning: no previous prototype for ‘text_poke’ [-Wmissing-prototypes]
  461 | void *text_poke(void *addr, const void *opcode, size_t len)
      |       ^~~~~~~~~
../arch/um/kernel/um_arch.c:473:6: warning: no previous prototype for ‘text_poke_sync’ [-Wmissing-prototypes]
  473 | void text_poke_sync(void)
      |      ^~~~~~~~~~~~~~
../arch/um/kernel/kmsg_dump.c:60:12: warning: no previous prototype for ‘kmsg_dumper_stdout_init’ [-Wmissing-prototypes]
   60 | int __init kmsg_dumper_stdout_init(void)
      |            ^~~~~~~~~~~~~~~~~~~~~~~
../lib/iomap.c:156:5: warning: no previous prototype for ‘ioread64_lo_hi’ [-Wmissing-prototypes]
  156 | u64 ioread64_lo_hi(const void __iomem *addr)
      |     ^~~~~~~~~~~~~~
../lib/iomap.c:163:5: warning: no previous prototype for ‘ioread64_hi_lo’ [-Wmissing-prototypes]
  163 | u64 ioread64_hi_lo(const void __iomem *addr)
      |     ^~~~~~~~~~~~~~
../lib/iomap.c:170:5: warning: no previous prototype for ‘ioread64be_lo_hi’ [-Wmissing-prototypes]
  170 | u64 ioread64be_lo_hi(const void __iomem *addr)
      |     ^~~~~~~~~~~~~~~~
../lib/iomap.c:178:5: warning: no previous prototype for ‘ioread64be_hi_lo’ [-Wmissing-prototypes]
  178 | u64 ioread64be_hi_lo(const void __iomem *addr)
      |     ^~~~~~~~~~~~~~~~
../lib/iomap.c:264:6: warning: no previous prototype for ‘iowrite64_lo_hi’ [-Wmissing-prototypes]
  264 | void iowrite64_lo_hi(u64 val, void __iomem *addr)
      |      ^~~~~~~~~~~~~~~
../lib/iomap.c:272:6: warning: no previous prototype for ‘iowrite64_hi_lo’ [-Wmissing-prototypes]
  272 | void iowrite64_hi_lo(u64 val, void __iomem *addr)
      |      ^~~~~~~~~~~~~~~
../lib/iomap.c:280:6: warning: no previous prototype for ‘iowrite64be_lo_hi’ [-Wmissing-prototypes]
  280 | void iowrite64be_lo_hi(u64 val, void __iomem *addr)
      |      ^~~~~~~~~~~~~~~~~
../lib/iomap.c:288:6: warning: no previous prototype for ‘iowrite64be_hi_lo’ [-Wmissing-prototypes]
  288 | void iowrite64be_hi_lo(u64 val, void __iomem *addr)
      |      ^~~~~~~~~~~~~~~~~
../drivers/gpu/drm/xe/xe_hmm.c: In function ‘build_sg’:
../drivers/gpu/drm/xe/xe_hmm.c:93:9: error: implicit declaration of function ‘page_to_mem_region’; did you mean ‘release_mem_region’? [-Werror=implicit-function-declaration]
   93 |    mr = page_to_mem_region(page);
      |         ^~~~~~~~~~~~~~~~~~
      |         release_mem_region

../drivers/gpu/drm/xe/xe_hmm.c:93:7: warning: assignment to ‘struct xe_mem_region *’ from ‘int’ makes pointer from integer without a cast [-Wint-conversion]
93 | mr = page_to_mem_region(page);
| ^
cc1: some warnings being treated as errors
make[7]: *** [../scripts/Makefile.build:243: drivers/gpu/drm/xe/xe_hmm.o] Error 1
make[7]: *** Waiting for unfinished jobs....
make[6]: *** [../scripts/Makefile.build:481: drivers/gpu/drm/xe] Error 2
make[5]: *** [../scripts/Makefile.build:481: drivers/gpu/drm] Error 2
make[4]: *** [../scripts/Makefile.build:481: drivers/gpu] Error 2
make[3]: *** [../scripts/Makefile.build:481: drivers] Error 2
make[2]: *** [/kernel/Makefile:1921: .] Error 2
make[1]: *** [/kernel/Makefile:240: __sub-make] Error 2
make: *** [Makefile:240: __sub-make] Error 2

[03:28:55] Configuring KUnit Kernel ...
Generating .config ...
Populating config with:

$ make ARCH=um O=.kunit olddefconfig
[03:28:59] Building KUnit Kernel ...
Populating config with:
$ make ARCH=um O=.kunit olddefconfig
Building with:
$ make ARCH=um O=.kunit --jobs=48
+ cleanup
++ stat -c %u:%g /kernel
+ chown -R 1003:1003 /kernel