Skip to contentSkip to conversation

drm/amdgpu: replace iova debugfs file with iomem (v2)

PATCH In review Tom St Denis 5 participants 8 messages Feb 9, 2018 – Feb 12, 2018 5 messages not in the archive

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 drm/amdgpu: replace iova debugfs file with iomem (v2)

From: Christian König <[email protected]>

This allows access to pages allocated through the driver with optional IOMMU mapping.

v2: Fix number of bytes copied and add write method

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c | 110 ++++++++++++++++++++++++++------
 1 file changed, 89 insertions(+), 21 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +89 -21
index b372d8d650a5..d6c56b001a2c 100644
@@ -1929,38 +1929,106 @@ static const struct file_operations amdgpu_ttm_gtt_fops = {
 
 #endif
 
-static ssize_t amdgpu_iova_to_phys_read(struct file *f, char __user *buf,
-				   size_t size, loff_t *pos)
+static ssize_t amdgpu_iomem_read(struct file *f, char __user *buf,
+				 size_t size, loff_t *pos)
 {
 	struct amdgpu_device *adev = file_inode(f)->i_private;
-	int r;
-	uint64_t phys;
 	struct iommu_domain *dom;
+	ssize_t result = 0;
+	int r;
 
-	// always return 8 bytes
-	if (size != 8)
-		return -EINVAL;
+	dom = iommu_get_domain_for_dev(adev->dev);
 
-	// only accept page addresses
-	if (*pos & 0xFFF)
-		return -EINVAL;
+	while (size) {
+		phys_addr_t addr = *pos & PAGE_MASK;
+		loff_t off = *pos & ~PAGE_MASK;
+		size_t bytes = PAGE_SIZE - off;
+		unsigned long pfn;
+		struct page *p;
+		void *ptr;
+
+		bytes = bytes < size ? bytes : size;
+
+		addr = dom ? iommu_iova_to_phys(dom, addr) : addr;
+
+		pfn = addr >> PAGE_SHIFT;
+		if (!pfn_valid(pfn))
+			return -EPERM;
+
+		p = pfn_to_page(pfn);
+		if (p->mapping != adev->mman.bdev.dev_mapping)
+			return -EPERM;
+
+		ptr = kmap(p);
+		if (ptr) {
+			r = copy_to_user(buf, ptr, bytes);
+			kunmap(p);
+			if (r)
+				return -EFAULT;
+		} else {
+			return -EFAULT;
+		}
+
+		size -= bytes;
+		*pos += bytes;
+		result += bytes;
+	}
+
+	return result;
+}
+
+static ssize_t amdgpu_iomem_write(struct file *f, const char __user *buf,
+				 size_t size, loff_t *pos)
+{
+	struct amdgpu_device *adev = file_inode(f)->i_private;
+	struct iommu_domain *dom;
+	ssize_t result = 0;
+	int r;
 
 	dom = iommu_get_domain_for_dev(adev->dev);
-	if (dom)
-		phys = iommu_iova_to_phys(dom, *pos);
-	else
-		phys = *pos;
 
-	r = copy_to_user(buf, &phys, 8);
-	if (r)
-		return -EFAULT;
+	while (size) {
+		phys_addr_t addr = *pos & PAGE_MASK;
+		loff_t off = *pos & ~PAGE_MASK;
+		size_t bytes = PAGE_SIZE - off;
+		unsigned long pfn;
+		struct page *p;
+		void *ptr;
+
+		bytes = bytes < size ? bytes : size;
+
+		addr = dom ? iommu_iova_to_phys(dom, addr) : addr;
 
-	return 8;
+		pfn = addr >> PAGE_SHIFT;
+		if (!pfn_valid(pfn))
+			return -EPERM;
+
+		p = pfn_to_page(pfn);
+		if (p->mapping != adev->mman.bdev.dev_mapping)
+			return -EPERM;
+
+		ptr = kmap(p);
+		if (ptr) {
+			r = copy_from_user(ptr, buf, bytes);
+			kunmap(p);
+			if (r)
+				return -EFAULT;
+		} else {
+			return -EFAULT;
+		}
+
+		size -= bytes;
+		*pos += bytes;
+		result += bytes;
+	}
+
+	return result;
 }
 
-static const struct file_operations amdgpu_ttm_iova_fops = {
+static const struct file_operations amdgpu_ttm_iomem_fops = {
 	.owner = THIS_MODULE,
-	.read = amdgpu_iova_to_phys_read,
+	.read = amdgpu_iomem_read,
+	.write = amdgpu_iomem_write,
 	.llseek = default_llseek
 };
 
@@ -1973,7 +2041,7 @@ static const struct {
 #ifdef CONFIG_DRM_AMDGPU_GART_DEBUGFS
 	{ "amdgpu_gtt", &amdgpu_ttm_gtt_fops, TTM_PL_TT },
 #endif
-	{ "amdgpu_iova", &amdgpu_ttm_iova_fops, TTM_PL_SYSTEM },
+	{ "amdgpu_iomem", &amdgpu_ttm_iomem_fops, TTM_PL_SYSTEM },
 };
 
 #endif
Signature · 6 lines
2.14.3

_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel

A message that is not in the archive (sent off-list or not yet received): [email protected]

#2 lore raw
Quoted 1 line · Tom St Denis

From: Christian König <[email protected]>

Oops, I'll remove this from the commit message before pushing :-)

I did give you credit below though.

Tom

Quoted 145 lines

This allows access to pages allocated through the driver with optional IOMMU mapping.

v2: Fix number of bytes copied and add write method

---
  drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c | 110 ++++++++++++++++++++++++++------
  1 file changed, 89 insertions(+), 21 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +88 -20
index b372d8d650a5..d6c56b001a2c 100644
@@ -1929,38 +1929,106 @@ static const struct file_operations amdgpu_ttm_gtt_fops = {
 
  #endif
 
-static ssize_t amdgpu_iova_to_phys_read(struct file *f, char __user *buf,
-				   size_t size, loff_t *pos)
+static ssize_t amdgpu_iomem_read(struct file *f, char __user *buf,
+				 size_t size, loff_t *pos)
  {
  	struct amdgpu_device *adev = file_inode(f)->i_private;
-	int r;
-	uint64_t phys;
  	struct iommu_domain *dom;
+	ssize_t result = 0;
+	int r;
 
-	// always return 8 bytes
-	if (size != 8)
-		return -EINVAL;
+	dom = iommu_get_domain_for_dev(adev->dev);
 
-	// only accept page addresses
-	if (*pos & 0xFFF)
-		return -EINVAL;
+	while (size) {
+		phys_addr_t addr = *pos & PAGE_MASK;
+		loff_t off = *pos & ~PAGE_MASK;
+		size_t bytes = PAGE_SIZE - off;
+		unsigned long pfn;
+		struct page *p;
+		void *ptr;
+
+		bytes = bytes < size ? bytes : size;
+
+		addr = dom ? iommu_iova_to_phys(dom, addr) : addr;
+
+		pfn = addr >> PAGE_SHIFT;
+		if (!pfn_valid(pfn))
+			return -EPERM;
+
+		p = pfn_to_page(pfn);
+		if (p->mapping != adev->mman.bdev.dev_mapping)
+			return -EPERM;
+
+		ptr = kmap(p);
+		if (ptr) {
+			r = copy_to_user(buf, ptr, bytes);
+			kunmap(p);
+			if (r)
+				return -EFAULT;
+		} else {
+			return -EFAULT;
+		}
+
+		size -= bytes;
+		*pos += bytes;
+		result += bytes;
+	}
+
+	return result;
+}
+
+static ssize_t amdgpu_iomem_write(struct file *f, const char __user *buf,
+				 size_t size, loff_t *pos)
+{
+	struct amdgpu_device *adev = file_inode(f)->i_private;
+	struct iommu_domain *dom;
+	ssize_t result = 0;
+	int r;
 
  	dom = iommu_get_domain_for_dev(adev->dev);
-	if (dom)
-		phys = iommu_iova_to_phys(dom, *pos);
-	else
-		phys = *pos;
 
-	r = copy_to_user(buf, &phys, 8);
-	if (r)
-		return -EFAULT;
+	while (size) {
+		phys_addr_t addr = *pos & PAGE_MASK;
+		loff_t off = *pos & ~PAGE_MASK;
+		size_t bytes = PAGE_SIZE - off;
+		unsigned long pfn;
+		struct page *p;
+		void *ptr;
+
+		bytes = bytes < size ? bytes : size;
+
+		addr = dom ? iommu_iova_to_phys(dom, addr) : addr;
 
-	return 8;
+		pfn = addr >> PAGE_SHIFT;
+		if (!pfn_valid(pfn))
+			return -EPERM;
+
+		p = pfn_to_page(pfn);
+		if (p->mapping != adev->mman.bdev.dev_mapping)
+			return -EPERM;
+
+		ptr = kmap(p);
+		if (ptr) {
+			r = copy_from_user(ptr, buf, bytes);
+			kunmap(p);
+			if (r)
+				return -EFAULT;
+		} else {
+			return -EFAULT;
+		}
+
+		size -= bytes;
+		*pos += bytes;
+		result += bytes;
+	}
+
+	return result;
  }
 
-static const struct file_operations amdgpu_ttm_iova_fops = {
+static const struct file_operations amdgpu_ttm_iomem_fops = {
  	.owner = THIS_MODULE,
-	.read = amdgpu_iova_to_phys_read,
+	.read = amdgpu_iomem_read,
+	.write = amdgpu_iomem_write,
  	.llseek = default_llseek
  };
 

drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c static const struct { · line 2047

Quoted 8 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +1 -1
@@ -1973,7 +2041,7 @@ static const struct {
  #ifdef CONFIG_DRM_AMDGPU_GART_DEBUGFS
  	{ "amdgpu_gtt", &amdgpu_ttm_gtt_fops, TTM_PL_TT },
  #endif
-	{ "amdgpu_iova", &amdgpu_ttm_iova_fops, TTM_PL_SYSTEM },
+	{ "amdgpu_iomem", &amdgpu_ttm_iomem_fops, TTM_PL_SYSTEM },
  };
 
  #endif

_______________________________________________
amd-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/amd-gfx

A message that is not in the archive (sent off-list or not yet received): 4708aad9-9ff0-d09f-9bb6-453c01c848a4-5C7GfCeVMHo@public.gmane.org

#4 lore raw

Am 09.02.2018 um 18:28 schrieb Tom St Denis:

Quoted 6 lines · Tom St Denis
Quoted 1 line

From: Christian König <[email protected]>

Oops, I'll remove this from the commit message before pushing :-)

I did give you credit below though.

The patch before this one isn't merged yet because I'm still not sure if that works under all circumstances or not.

Could you give it some extended testing? Especially if it work with eviction.

Christian.

Quoted 141 lines

Tom

Quoted 138 lines

This allows access to pages allocated through the driver with optional IOMMU mapping.

v2: Fix number of bytes copied and add write method

---
  drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c | 110
++++++++++++++++++++++++++------

  1 file changed, 89 insertions(+), 21 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +4 -3
index b372d8d650a5..d6c56b001a2c 100644
@@ -1929,38 +1929,106 @@ static const struct file_operations 
 amdgpu_ttm_gtt_fops = {
     #endif
   -static ssize_t amdgpu_iova_to_phys_read(struct file *f, char __user *buf,
-                   size_t size, loff_t *pos)
+static ssize_t amdgpu_iomem_read(struct file *f, char __user *buf,
+                 size_t size, loff_t *pos)
   {
       struct amdgpu_device *adev = file_inode(f)->i_private;
-    int r;
-    uint64_t phys;
       struct iommu_domain *dom;
+    ssize_t result = 0;
+    int r;

  -    // always return 8 bytes
-    if (size != 8)
-        return -EINVAL;
+    dom = iommu_get_domain_for_dev(adev->dev);
  -    // only accept page addresses
-    if (*pos & 0xFFF)
-        return -EINVAL;
+    while (size) {
+        phys_addr_t addr = *pos & PAGE_MASK;
+        loff_t off = *pos & ~PAGE_MASK;
+        size_t bytes = PAGE_SIZE - off;
+        unsigned long pfn;
+        struct page *p;
+        void *ptr;

drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +32 -0
+
+        bytes = bytes < size ? bytes : size;
+
+        addr = dom ? iommu_iova_to_phys(dom, addr) : addr;
+
+        pfn = addr >> PAGE_SHIFT;
+        if (!pfn_valid(pfn))
+            return -EPERM;
+
+        p = pfn_to_page(pfn);
+        if (p->mapping != adev->mman.bdev.dev_mapping)
+            return -EPERM;
+
+        ptr = kmap(p);
+        if (ptr) {
+            r = copy_to_user(buf, ptr, bytes);
+            kunmap(p);
+            if (r)
+                return -EFAULT;
+        } else {
+            return -EFAULT;
+        }
+
+        size -= bytes;
+        *pos += bytes;
+        result += bytes;
+    }
+
+    return result;
+}
+
+static ssize_t amdgpu_iomem_write(struct file *f, const char __user 

*buf,
+                 size_t size, loff_t *pos)
+{
+    struct amdgpu_device *adev = file_inode(f)->i_private; +    struct iommu_domain *dom;
+    ssize_t result = 0;
+    int r;
        dom = iommu_get_domain_for_dev(adev->dev); -    if (dom)
-        phys = iommu_iova_to_phys(dom, *pos);
-    else
-        phys = *pos;
  -    r = copy_to_user(buf, &phys, 8);
-    if (r)
-        return -EFAULT;
+    while (size) {
+        phys_addr_t addr = *pos & PAGE_MASK;
+        loff_t off = *pos & ~PAGE_MASK;
+        size_t bytes = PAGE_SIZE - off;
+        unsigned long pfn;
+        struct page *p;
+        void *ptr;

drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +4 -0
+
+        bytes = bytes < size ? bytes : size;
+
+        addr = dom ? iommu_iova_to_phys(dom, addr) : addr;

  -    return 8;
+        pfn = addr >> PAGE_SHIFT;
+        if (!pfn_valid(pfn))
+            return -EPERM;

drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +21 -0
+
+        p = pfn_to_page(pfn);
+        if (p->mapping != adev->mman.bdev.dev_mapping)
+            return -EPERM;
+
+        ptr = kmap(p);
+        if (ptr) {
+            r = copy_from_user(ptr, buf, bytes);
+            kunmap(p);
+            if (r)
+                return -EFAULT;
+        } else {
+            return -EFAULT;
+        }
+
+        size -= bytes;
+        *pos += bytes;
+        result += bytes;
+    }
+
+    return result;

  }
  -static const struct file_operations amdgpu_ttm_iova_fops = { +static const struct file_operations amdgpu_ttm_iomem_fops = { .owner = THIS_MODULE,
-    .read = amdgpu_iova_to_phys_read,
+    .read = amdgpu_iomem_read,
+    .write = amdgpu_iomem_write,
      .llseek = default_llseek
  };
  @@ -1973,7 +2041,7 @@ static const struct {

Quoted 9 lines
Quoted 8 lines

  #ifdef CONFIG_DRM_AMDGPU_GART_DEBUGFS
      { "amdgpu_gtt", &amdgpu_ttm_gtt_fops, TTM_PL_TT }, #endif
-    { "amdgpu_iova", &amdgpu_ttm_iova_fops, TTM_PL_SYSTEM }, +    { "amdgpu_iomem", &amdgpu_ttm_iomem_fops, TTM_PL_SYSTEM }, };
    #endif

_______________________________________________
amd-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/amd-gfx

A message that is not in the archive (sent off-list or not yet received): 50edcafc-23dd-3c44-021f-cf5989d06dc2-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org

#6 lore raw
Quoted 11 lines · Christian König

Am 09.02.2018 um 18:28 schrieb Tom St Denis:

Quoted 6 lines · Tom St Denis
Quoted 1 line

From: Christian König <[email protected]>

Oops, I'll remove this from the commit message before pushing :-)

I did give you credit below though.

The patch before this one isn't merged yet because I'm still not sure if that works under all circumstances or not.

Could you give it some extended testing? Especially if it work with eviction.

I supposed there is a race on the kmap'ed memory which is why I added a ptr check. Not perfect but since it's a debugfs entry probably better than it needs to be.

I'll give it a try on some live traffic next.

Cheers,
Tom
_______________________________________________
amd-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/amd-gfx

#7 lore raw

Am 09.02.2018 um 19:19 schrieb Tom St Denis:

Quoted 7 lines · Christian König
Quoted 6 lines

Am 09.02.2018 um 18:28 schrieb Tom St Denis:

Quoted 5 lines · Tom St Denis
Quoted 1 line

From: Christian König <[email protected]>

Oops, I'll remove this from the commit message before pushing :-)

Quoted 7 lines
Quoted 5 lines
Quoted 1 line

I did give you credit below though.

The patch before this one isn't merged yet because I'm still not sure if that works under all circumstances or not.

Could you give it some extended testing? Especially if it work with eviction.

I supposed there is a race on the kmap'ed memory which is why I added a ptr check.  Not perfect but since it's a debugfs entry probably better than it needs to be.

I think you can drop that, kmap can only return NULL on 32bit systems when we ran out of vmap area and then you can basically call panic() as well.

The question is if setting page->mapping during allocation has some undesired side effect.

Christian.

Quoted 9 lines

I'll give it a try on some live traffic next.

Cheers,
Tom
_______________________________________________
amd-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/amd-gfx

_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel

A message that is not in the archive (sent off-list or not yet received): 50cdbb8f-3b64-bed3-322c-bcce05b39370-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org

#9 lore raw
Quoted 12 lines · Christian König

Am 09.02.2018 um 19:19 schrieb Tom St Denis:

Quoted 11 lines · Christian König
Quoted 10 lines

Am 09.02.2018 um 18:28 schrieb Tom St Denis:

Quoted 6 lines · Tom St Denis
Quoted 1 line

From: Christian König <[email protected]>

Oops, I'll remove this from the commit message before pushing :-)

I did give you credit below though.

The patch before this one isn't merged yet because I'm still not sure if that works under all circumstances or not.

Quoted 7 lines
Quoted 3 lines
Quoted 1 line

Could you give it some extended testing? Especially if it work with eviction.

I supposed there is a race on the kmap'ed memory which is why I added a ptr check.  Not perfect but since it's a debugfs entry probably better than it needs to be.

I think you can drop that, kmap can only return NULL on 32bit systems when we ran out of vmap area and then you can basically call panic() as well.

The question is if setting page->mapping during allocation has some undesired side effect.

Haven't noticed anything with piglit or other FOSS GL games.

Is there a specific test you'd like me to consider?

Tom
_______________________________________________
amd-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/amd-gfx

maintainer
#10 lore raw

Am 12.02.2018 um 17:40 schrieb Tom St Denis:

Quoted 17 lines · Christian König
Quoted 16 lines

Am 09.02.2018 um 19:19 schrieb Tom St Denis:

Quoted 14 lines · Christian König
Quoted 11 lines

Am 09.02.2018 um 18:28 schrieb Tom St Denis:

Quoted 6 lines · Tom St Denis
Quoted 1 line

From: Christian König <[email protected]>

Oops, I'll remove this from the commit message before pushing :-)

I did give you credit below though.

The patch before this one isn't merged yet because I'm still not sure if that works under all circumstances or not.

Could you give it some extended testing? Especially if it work with eviction.

I supposed there is a race on the kmap'ed memory which is why I added a ptr check.  Not perfect but since it's a debugfs entry probably better than it needs to be.

Quoted 8 lines
Quoted 3 lines

I think you can drop that, kmap can only return NULL on 32bit systems when we ran out of vmap area and then you can basically call panic() as well.

The question is if setting page->mapping during allocation has some undesired side effect.

Haven't noticed anything with piglit or other FOSS GL games.

Is there a specific test you'd like me to consider?

Try to restrict VRAM/GTT/memory in general and run something which needs a lot of CPU access to BOs. When it affects anything then the CPU page table unmap of BOs during eviction.

Christian.

Quoted 2 lines

Tom

_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel

A message that is not in the archive (sent off-list or not yet received): 8c42ceb6-4c95-d82f-d347-37ee793855a6-5C7GfCeVMHo@public.gmane.org

#12 lore raw
Quoted 20 lines · Christian König

Am 12.02.2018 um 17:40 schrieb Tom St Denis:

Quoted 19 lines · Christian König
Quoted 18 lines

Am 09.02.2018 um 19:19 schrieb Tom St Denis:

Quoted 7 lines · Christian König
Quoted 6 lines

Am 09.02.2018 um 18:28 schrieb Tom St Denis:

Quoted 5 lines · Tom St Denis
Quoted 1 line

From: Christian König <[email protected]>

Oops, I'll remove this from the commit message before pushing :-)

Quoted 7 lines
Quoted 5 lines
Quoted 1 line

I did give you credit below though.

The patch before this one isn't merged yet because I'm still not sure if that works under all circumstances or not.

Could you give it some extended testing? Especially if it work with eviction.

I supposed there is a race on the kmap'ed memory which is why I added a ptr check.  Not perfect but since it's a debugfs entry probably better than it needs to be.

I think you can drop that, kmap can only return NULL on 32bit systems when we ran out of vmap area and then you can basically call panic() as well.

Quoted 8 lines
Quoted 6 lines
Quoted 1 line

The question is if setting page->mapping during allocation has some undesired side effect.

Haven't noticed anything with piglit or other FOSS GL games.

Is there a specific test you'd like me to consider?

Try to restrict VRAM/GTT/memory in general and run something which needs a lot of CPU access to BOs. When it affects anything then the CPU page table unmap of BOs during eviction.

Limiting vram to 256M seems to be fine for both my iGPU and dGPU (thought it drops heaven from 40fps to about 3fps hehehe).

Trying to play with gttsize=256M seems to have broken things I get:

[  165.080976] [drm:amdgpu_cs_ioctl [amdgpu]] *ERROR* amdgpu_cs_list_validate(validated) failed.
[  165.081163] [drm:amdgpu_cs_ioctl [amdgpu]] *ERROR* Not enough memory for command submission!

and it seems heaven is blocked. There are fences being submitted (and signalled) but nothing is being drawn.

Tom
_______________________________________________
amd-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/amd-gfx