public inbox for linux-arch@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH: x86] Add dma_mmap_coherent()
@ 2004-03-28 10:22 Russell King
  2004-03-28 10:40 ` [PATCH: ARM] " Russell King
  2004-03-28 11:35 ` [PATCH: x86] " Russell King
  0 siblings, 2 replies; 7+ messages in thread
From: Russell King @ 2004-03-28 10:22 UTC (permalink / raw)
  To: linux-arch

This has only been compile tested.  There are two changes here:

1. ensure that we zero the whole page - otherwise we may expose leak
   kernel data to userspace in the remainder of the last page.
2. add dma_mmap_coherent().  I've added checks for the offset and
   size to ensure that we don't do anything silly (like mapping more
   memory than the original coherent allocation covered.)  Maybe this
   is something that remap_page_range() should be doing anyway?

--- orig/arch/i386/kernel/pci-dma.c	Fri Mar 19 11:55:20 2004
+++ linux/arch/i386/kernel/pci-dma.c	Sun Mar 28 10:58:22 2004
@@ -26,7 +26,7 @@ void *dma_alloc_coherent(struct device *
 	ret = (void *)__get_free_pages(gfp, get_order(size));
 
 	if (ret != NULL) {
-		memset(ret, 0, size);
+		memset(ret, 0, PAGE_ALIGN(size));
 		*dma_handle = virt_to_phys(ret);
 	}
 	return ret;
--- orig/include/asm-i386/dma-mapping.h	Sat Mar 20 09:22:49 2004
+++ linux/include/asm-i386/dma-mapping.h	Sun Mar 28 10:57:56 2004
@@ -12,6 +12,23 @@ void *dma_alloc_coherent(struct device *
 void dma_free_coherent(struct device *dev, size_t size,
 			 void *vaddr, dma_addr_t dma_handle);
 
+static inline int
+dma_mmap_coherent(struct device *dev, struct vm_area_struct *vma,
+		  void *vaddr, dma_addr_t handle, size_t size)
+{
+	unsigned long offset = vma->vm_pgoff, usize;
+
+	size = PAGE_ALIGN(size) >> PAGE_SHIFT;
+	usize = (vma->vm_end - vma->vm_start) >> PAGE_SHIFT;
+
+	if (offset >= size || usize > (size - offset))
+		return -ENXIO;
+
+	return remap_page_range(vma, vma->vm_start,
+				__pa(vaddr) + (offset << PAGE_SHIFT),
+				usize << PAGE_SHIFT, vma->vm_page_prot);
+}
+
 static inline dma_addr_t
 dma_map_single(struct device *dev, void *ptr, size_t size,
 	       enum dma_data_direction direction)

-- 
Russell King
 Linux kernel    2.6 ARM Linux   - http://www.arm.linux.org.uk/
 maintainer of:  2.6 PCMCIA      - http://pcmcia.arm.linux.org.uk/
                 2.6 Serial core

^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2004-03-28 13:19 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2004-03-28 10:22 [PATCH: x86] Add dma_mmap_coherent() Russell King
2004-03-28 10:40 ` [PATCH: ARM] " Russell King
2004-03-28 11:35 ` [PATCH: x86] " Russell King
2004-03-28 11:40   ` William Lee Irwin III
2004-03-28 12:36     ` Russell King
2004-03-28 12:51       ` William Lee Irwin III
2004-03-28 13:19       ` Russell King

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox