Skip to contentSkip to conversation

drm/ttm: dma: Don't crash on memory in the vmalloc range

PATCH In review Alexandre Courbot 4 participants 6 messages May 15, 2015 – Jun 1, 2015

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

PATCH 1/1 drm/ttm: dma: Don't crash on memory in the vmalloc range

dma_alloc_coherent() can return memory in the vmalloc range. virt_to_page() cannot handle such addresses and crashes. This patch detects such cases and obtains the struct page * using vmalloc_to_page() instead.

---
This patch is a follow-up of the following discussion:

https://www.marc.info/?l=dri-devel&m=141579595431254&w=3

It works for me on both 32-bit and 64-bit Tegra, so I am not convinced that Thierry's initial change from virt_to_page() to phys_to_page() is still required - Thierry, can you confirm whether your patch is still relevant after this one?

 drivers/gpu/drm/ttm/ttm_page_alloc_dma.c | 9 ++++++---
 1 file changed, 6 insertions(+), 3 deletions(-)
drivers/gpu/drm/ttm/ttm_page_alloc_dma.c +6 -3
index 01e1d27eb078..3077f1554099 100644
@@ -342,9 +342,12 @@ static struct dma_page *__ttm_dma_alloc_page(struct dma_pool *pool)
 	d_page->vaddr = dma_alloc_coherent(pool->dev, pool->size,
 					   &d_page->dma,
 					   pool->gfp_flags);
-	if (d_page->vaddr)
-		d_page->p = virt_to_page(d_page->vaddr);
-	else {
+	if (d_page->vaddr) {
+		if (is_vmalloc_addr(d_page->vaddr))
+			d_page->p = vmalloc_to_page(d_page->vaddr);
+		else
+			d_page->p = virt_to_page(d_page->vaddr);
+	} else {
 		kfree(d_page);
 		d_page = NULL;
 	}
Signature · 6 lines
2.4.0

_______________________________________________
dri-devel mailing list
[email protected]
http://lists.freedesktop.org/mailman/listinfo/dri-devel
#1 lore raw
Quoted 9 lines · Alexandre Courbot

dma_alloc_coherent() can return memory in the vmalloc range. virt_to_page() cannot handle such addresses and crashes. This patch detects such cases and obtains the struct page * using vmalloc_to_page() instead.

---
This patch is a follow-up of the following discussion:

Quoted 6 lines

https://www.marc.info/?l=dri-devel&m=141579595431254&w=3

It works for me on both 32-bit and 64-bit Tegra, so I am not convinced that Thierry's initial change from virt_to_page() to phys_to_page() is still required - Thierry, can you confirm whether your patch is still relevant after this one?

If this works for you on both 32-bit and 64-bit Tegra I don't think the earlier patch is still relevant and this looks indeed like a much more appropriate solution.

Thierry

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

#2 lore raw
In reply to Alexandre Courbot

2015-05-15 15:09 GMT+08:00 Alexandre Courbot <[email protected]>:

Quoted 42 lines

dma_alloc_coherent() can return memory in the vmalloc range. virt_to_page() cannot handle such addresses and crashes. This patch detects such cases and obtains the struct page * using vmalloc_to_page() instead.

---
This patch is a follow-up of the following discussion:

https://www.marc.info/?l=dri-devel&m=141579595431254&w=3

It works for me on both 32-bit and 64-bit Tegra, so I am not convinced that Thierry's initial change from virt_to_page() to phys_to_page() is still required - Thierry, can you confirm whether your patch is still relevant after this one?

 drivers/gpu/drm/ttm/ttm_page_alloc_dma.c | 9 ++++++---
 1 file changed, 6 insertions(+), 3 deletions(-)
drivers/gpu/drm/ttm/ttm_page_alloc_dma.c +6 -4
index 01e1d27eb078..3077f1554099 100644
@@ -342,9 +342,12 @@ static struct dma_page *__ttm_dma_alloc_page(struct dma_pool *pool)
        d_page->vaddr = dma_alloc_coherent(pool->dev, pool->size,
                                           &d_page->dma,
                                           pool->gfp_flags);
-       if (d_page->vaddr)
-               d_page->p = virt_to_page(d_page->vaddr);
-       else {
+       if (d_page->vaddr) {
+               if (is_vmalloc_addr(d_page->vaddr))
+                       d_page->p = vmalloc_to_page(d_page->vaddr);
+               else
+                       d_page->p = virt_to_page(d_page->vaddr);
+       } else {
                kfree(d_page);
                d_page = NULL;
        }
--

2.4.0

Quoted 5 lines

--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/

for safe , i think we can add this patch:

arch/arm64/include/asm/memory.h +2 -1
index f800d45..3463f4f 100644
@@ -77,7 +77,8 @@
  * private definitions which should NOT be used outside memory.h
  * files.  Use virt_to_phys/phys_to_virt/__pa/__va instead.
  */
-#define __virt_to_phys(x) (((phys_addr_t)(x) - PAGE_OFFSET + PHYS_OFFSET))
+#define __virt_to_phys(x) ({BUG_ON(x < PAGE_OFFSET);\
+ (phys_addr_t)(x) - PAGE_OFFSET + PHYS_OFFSET;})
 #define __phys_to_virt(x) ((unsigned long)((x) - PHYS_OFFSET + PAGE_OFFSET))
 
 /*

BRs
Yalin

#3 lore raw
In reply to Alexandre Courbot
Quoted 26 lines · Alexandre Courbot

dma_alloc_coherent() can return memory in the vmalloc range. virt_to_page() cannot handle such addresses and crashes. This patch detects such cases and obtains the struct page * using vmalloc_to_page() instead.

---
This patch is a follow-up of the following discussion:

https://www.marc.info/?l=dri-devel&m=141579595431254&w=3

It works for me on both 32-bit and 64-bit Tegra, so I am not convinced that Thierry's initial change from virt_to_page() to phys_to_page() is still required - Thierry, can you confirm whether your patch is still relevant after this one?

 drivers/gpu/drm/ttm/ttm_page_alloc_dma.c | 9 ++++++---
 1 file changed, 6 insertions(+), 3 deletions(-)
drivers/gpu/drm/ttm/ttm_page_alloc_dma.c +0 -0
index 01e1d27eb078..3077f1554099 100644
@@ -342,9 +342,12 @@ static struct dma_page *__ttm_dma_alloc_page(struct dma_pool *pool)
 	d_page->vaddr = dma_alloc_coherent(pool->dev, pool->size,
 					   &d_page->dma,

drivers/gpu/drm/ttm/ttm_page_alloc_dma.c static struct dma_page *__ttm_dma_alloc_page(struct dma_pool *pool) · line 350

Quoted 10 lines
drivers/gpu/drm/ttm/ttm_page_alloc_dma.c +6 -3
 					   pool->gfp_flags);
-	if (d_page->vaddr)
-		d_page->p = virt_to_page(d_page->vaddr);
-	else {
+	if (d_page->vaddr) {
+		if (is_vmalloc_addr(d_page->vaddr))
+			d_page->p = vmalloc_to_page(d_page->vaddr);
+		else
+			d_page->p = virt_to_page(d_page->vaddr);
+	} else {

Looks OK to me.

Quoted 6 lines
drivers/gpu/drm/ttm/ttm_page_alloc_dma.c +0 -0
 		kfree(d_page);
 		d_page = NULL;
 	}
Signature · 1 line
2.4.0

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

#4 lore raw
Quoted 32 lines · Konrad Rzeszutek Wilk
Quoted 31 lines · Alexandre Courbot

dma_alloc_coherent() can return memory in the vmalloc range. virt_to_page() cannot handle such addresses and crashes. This patch detects such cases and obtains the struct page * using vmalloc_to_page() instead.

---
This patch is a follow-up of the following discussion:

https://www.marc.info/?l=dri-devel&m=141579595431254&w=3

It works for me on both 32-bit and 64-bit Tegra, so I am not convinced that Thierry's initial change from virt_to_page() to phys_to_page() is still required - Thierry, can you confirm whether your patch is still relevant after this one?

  drivers/gpu/drm/ttm/ttm_page_alloc_dma.c | 9 ++++++---
  1 file changed, 6 insertions(+), 3 deletions(-)
drivers/gpu/drm/ttm/ttm_page_alloc_dma.c +1 -3
index 01e1d27eb078..3077f1554099 100644
@@ -342,9 +342,12 @@ static struct dma_page *__ttm_dma_alloc_page(struct dma_pool *pool)
  	d_page->vaddr = dma_alloc_coherent(pool->dev, pool->size,
  					   &d_page->dma,
  					   pool->gfp_flags);
-	if (d_page->vaddr)
-		d_page->p = virt_to_page(d_page->vaddr);
-	else {
+	if (d_page->vaddr) {
Quoted 8 lines

drivers/gpu/drm/ttm/ttm_page_alloc_dma.c static struct dma_page *__ttm_dma_alloc_page(struct dma_pool *pool) · line 350

Quoted 5 lines
drivers/gpu/drm/ttm/ttm_page_alloc_dma.c +5 -0
+		if (is_vmalloc_addr(d_page->vaddr))
+			d_page->p = vmalloc_to_page(d_page->vaddr);
+		else
+			d_page->p = virt_to_page(d_page->vaddr);
+	} else {

Looks OK to me.

Thanks guys. Could we translate these approvals into Acked-bys/Reviewed-bys so Dave (?) can merge this patch?
_______________________________________________
dri-devel mailing list
[email protected]
http://lists.freedesktop.org/mailman/listinfo/dri-devel

#5 lore raw
Quoted 42 lines · Alexandre Courbot
Quoted 32 lines · Konrad Rzeszutek Wilk
Quoted 31 lines · Alexandre Courbot

dma_alloc_coherent() can return memory in the vmalloc range. virt_to_page() cannot handle such addresses and crashes. This patch detects such cases and obtains the struct page * using vmalloc_to_page() instead.

---
This patch is a follow-up of the following discussion:

https://www.marc.info/?l=dri-devel&m=141579595431254&w=3

It works for me on both 32-bit and 64-bit Tegra, so I am not convinced that Thierry's initial change from virt_to_page() to phys_to_page() is still required - Thierry, can you confirm whether your patch is still relevant after this one?

 drivers/gpu/drm/ttm/ttm_page_alloc_dma.c | 9 ++++++---
 1 file changed, 6 insertions(+), 3 deletions(-)
drivers/gpu/drm/ttm/ttm_page_alloc_dma.c +1 -3
index 01e1d27eb078..3077f1554099 100644
@@ -342,9 +342,12 @@ static struct dma_page *__ttm_dma_alloc_page(struct dma_pool *pool)
 	d_page->vaddr = dma_alloc_coherent(pool->dev, pool->size,
 					   &d_page->dma,
 					   pool->gfp_flags);
-	if (d_page->vaddr)
-		d_page->p = virt_to_page(d_page->vaddr);
-	else {
+	if (d_page->vaddr) {
Quoted 8 lines

drivers/gpu/drm/ttm/ttm_page_alloc_dma.c static struct dma_page *__ttm_dma_alloc_page(struct dma_pool *pool) · line 350

Quoted 5 lines
drivers/gpu/drm/ttm/ttm_page_alloc_dma.c +5 -0
+		if (is_vmalloc_addr(d_page->vaddr))
+			d_page->p = vmalloc_to_page(d_page->vaddr);
+		else
+			d_page->p = virt_to_page(d_page->vaddr);
+	} else {

Looks OK to me.

Quoted 2 lines

Thanks guys. Could we translate these approvals into Acked-bys/Reviewed-bys so Dave (?) can merge this patch?

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