Helper to abstract vma handling in media layer
Subsystems (8)
Hello,
The following patch series is my first stab at abstracting vma handling
from the various media drivers. After this patch set drivers have to know much less details about vmas, their types, and locking. My motivation for the series is that I want to change get_user_pages() locking and I want to handle subtle locking details in as few places as possible.
The core of the series is the new helper get_vaddr_pfns() which is given a virtual address and it fills in PFNs into provided array. If PFNs correspond to normal pages it also grabs references to these pages. The difference from get_user_pages() is that this function can also deal with pfnmap, mixed, and io mappings which is what the media drivers need.
The patches are just compile tested (since I don't have any of the hardware I'm afraid I won't be able to do any more testing anyway) so please handle with care. I'm grateful for any comments.
Honza
PATCH 1/9 mm: Provide new get_vaddr_pfns() helper
Provide new function get_vaddr_pfns(). This function maps virtual addresses from given start and fills given array with page frame numbers of the corresponding pages. If given start belongs to a normal vma, the function grabs reference to each of the pages to pin them in memory. If start belongs to VM_IO | VM_PFNMAP vma, we don't touch page structures. Caller should make sure pfns aren't reused for anything else while he is using them.
This function is created for various drivers to simplify handling of their buffers.
--- include/linux/mm.h | 46 +++++++++++++++ mm/memory.c | 165 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 211 insertions(+)
include/linux/mm.h +46 -0
@@ -18,6 +18,8 @@#include <linux/pfn.h> #include <linux/bit_spinlock.h> #include <linux/shrinker.h> +#include <linux/slab.h> +#include <linux/vmalloc.h> struct mempolicy ;struct anon_vma ;
@@ -1180,6 +1182,50 @@ get_user_pages_unlocked(struct task_struct *tsk, struct mm_struct *mm,return ret; } +/* Container for pinned pfns / pages */ +struct pinned_pfns { +unsigned int nr_allocated_pfns;/* Number of pfns we have space for */ +unsigned int nr_pfns;/* Number of pfns stored in pfns array */ +unsigned int got_ref:1 ;/* Did we pin pfns by getting page ref? */ +unsigned int is_pages:1 ;/* Does array contain pages or pfns? */ +void *ptrs[0 ];/* Array of pinned pfns / pages. +* Use pfns_vector_pages() or +* pfns_vector_pfns() for access */ +}; + +struct pinned_pfns *pfns_vector_create (int nr_pfns); +static inline void pfns_vector_destroy (struct pinned_pfns *pfns) +{ +if (!is_vmalloc_addr (pfns)) +kfree (pfns); +else +vfree (pfns); +} +int get_vaddr_pfns (unsigned long start,int nr_pfns,int write,int force, +struct pinned_pfns *pfns); +void put_vaddr_pfns (struct pinned_pfns *pfns); +int pfns_vector_to_pages (struct pinned_pfns *pfns); + +static inline int pfns_vector_count (struct pinned_pfns *pfns) +{ +return pfns->nr_pfns; +} + +static inline struct page **pfns_vector_pages (struct pinned_pfns *pfns) +{ +if (!pfns->is_pages) +return NULL ; +return (struct page **)(pfns->ptrs); +} + +static inline unsigned long *pfns_vector_pfns (struct pinned_pfns *pfns) +{ +if (pfns->is_pages) +return NULL ; +return (unsigned long *)(pfns->ptrs); +} + +struct kvec ;int get_kernel_pages (const struct kvec *iov,int nr_pages,int write,struct page **pages);
mm/memory.c +165 -0
171 lines not on this page · raw message
Signature · 1 line
1.8.1.4
Re: [PATCH 1/9] mm: Provide new get_vaddr_pfns() helper
Quoted 3 lines · Jan Kara
mm/memory.c +3
-0+int get_vaddr_pfns (unsigned long start,int nr_pfns,int write,int force, +struct pinned_pfns *pfns) +{
...
mm/memory.c long get_user_pages(struct task_struct *tsk, struct mm_struct *mm, · line 2072
Quoted 7 lines
mm/memory.c +7
-0+if (!(vma->vm_flags & (VM_IO |VM_PFNMAP ))) { + pfns->got_ref =1 ; + pfns->is_pages =1 ; + ret =get_user_pages (current, mm, start, nr_pfns, write, force, +pfns_vector_pages (pfns),NULL ); + goto out; + }
Have you given any thought to how this should deal with VM_MIXEDMAP vmas? get_user_pages() will freak when it hits the !vm_normal_page() test on the pfnmapped ones, and jump out. Shouldn't get_vaddr_pfns() be able to handle those too?
Re: [PATCH 1/9] mm: Provide new get_vaddr_pfns() helper
Quoted 5 lines · Dave Hansen
Quoted 3 lines · Jan Kara
mm/memory.c +3
-0+int get_vaddr_pfns (unsigned long start,int nr_pfns,int write,int force, +struct pinned_pfns *pfns) +{...
Quoted 12 lines
mm/memory.c long get_user_pages(struct task_struct *tsk, struct mm_struct *mm, · line 2072
Quoted 7 lines
mm/memory.c +7
-0+if (!(vma->vm_flags & (VM_IO |VM_PFNMAP ))) { + pfns->got_ref =1 ; + pfns->is_pages =1 ; + ret =get_user_pages (current, mm, start, nr_pfns, write, force, +pfns_vector_pages (pfns),NULL ); + goto out; + }Have you given any thought to how this should deal with
VM_MIXEDMAPvmas?get_user_pages()will freak when it hits the !vm_normal_page() test on the pfnmapped ones, and jump out. Shouldn'tget_vaddr_pfns()be able to handle those too?
It could and it doesn't seem as a big complication. Although none of the
converted drivers need this functionality, I guess it makes sense to implement this to make the API more consistent. So I can have a look at it for the next iteration.
Honza
Signature · 2 lines
Jan Kara <[email protected]> SUSE Labs, CR
Re: [PATCH 1/9] mm: Provide new get_vaddr_pfns() helper
In reply to Jan KaraQuoted 10 lines · Jan Kara
Provide new function
get_vaddr_pfns(). This function maps virtual addresses from given start and fills given array with page frame numbers of the corresponding pages. If given start belongs to a normal vma, the function grabs reference to each of the pages to pin them in memory. If start belongs toVM_IO|VM_PFNMAPvma, we don't touch page structures. Caller should make sure pfns aren't reused for anything else while he is using them.This function is created for various drivers to simplify handling of their buffers.
MM guys, could you have a look at this patch? Linux Media people like the
abstraction of buffer handling and would like to merge the patch set (see
http://thread.gmane.org/gmane.linux.drivers.video-input-infrastructure/89268).
But for that they need ack from mm people that the interface is ok with them. So far the only comment I got regarding the interface was from Dave Hansen that I could also handle VM_MIXEDMAP mappings - I could if people really want that but so far there are no users for that. Thanks!
Honza
Quoted 250 lines
Signed-off-by Jan Kara <[email protected]>--- include/linux/mm.h | 46 +++++++++++++++ mm/memory.c | 165 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 211 insertions(+)include/linux/mm.h +46
-0@@ -18,6 +18,8 @@#include <linux/pfn.h> #include <linux/bit_spinlock.h> #include <linux/shrinker.h> +#include <linux/slab.h> +#include <linux/vmalloc.h> struct mempolicy ;struct anon_vma ;@@ -1180,6 +1182,50 @@ get_user_pages_unlocked(struct task_struct *tsk, struct mm_struct *mm,return ret; } +/* Container for pinned pfns / pages */ +struct pinned_pfns { +unsigned int nr_allocated_pfns;/* Number of pfns we have space for */ +unsigned int nr_pfns;/* Number of pfns stored in pfns array */ +unsigned int got_ref:1 ;/* Did we pin pfns by getting page ref? */ +unsigned int is_pages:1 ;/* Does array contain pages or pfns? */ +void *ptrs[0 ];/* Array of pinned pfns / pages. +* Use pfns_vector_pages() or +* pfns_vector_pfns() for access */ +}; + +struct pinned_pfns *pfns_vector_create (int nr_pfns); +static inline void pfns_vector_destroy (struct pinned_pfns *pfns) +{ +if (!is_vmalloc_addr (pfns)) +kfree (pfns); +else +vfree (pfns); +} +int get_vaddr_pfns (unsigned long start,int nr_pfns,int write,int force, +struct pinned_pfns *pfns); +void put_vaddr_pfns (struct pinned_pfns *pfns); +int pfns_vector_to_pages (struct pinned_pfns *pfns); + +static inline int pfns_vector_count (struct pinned_pfns *pfns) +{ +return pfns->nr_pfns; +} + +static inline struct page **pfns_vector_pages (struct pinned_pfns *pfns) +{ +if (!pfns->is_pages) +return NULL ; +return (struct page **)(pfns->ptrs); +} + +static inline unsigned long *pfns_vector_pfns (struct pinned_pfns *pfns) +{ +if (pfns->is_pages) +return NULL ; +return (unsigned long *)(pfns->ptrs); +} + +struct kvec ;int get_kernel_pages (const struct kvec *iov,int nr_pages,int write,struct page **pages);mm/memory.c +165
-0@@ -2024,6 +2024,171 @@ long get_user_pages(struct task_struct *tsk, struct mm_struct *mm,EXPORT_SYMBOL (get_user_pages);/** +* get_vaddr_pfns() - map virtual addresses to pfns +* @start: starting user address +* @nr_pfns: number of pfns from start to map +* @write: whether pages will be written to by the caller +* @force: whether to force write access even if user mapping is +* readonly. This will result in the page being COWed even +* in MAP_SHARED mappings. You do not want this. +* @pfns: structure which receives pfns of the pages mapped. +* It should have space for at least nr_pfns pfns. +* +* This function maps virtual addresses from @start and fills @pfns structure +* with page frame numbers of corresponding pages. If @start belongs to a +* normal vma, the function grabs reference to each of the pages to pin them in +* memory. If @start belongs to VM_IO | VM_PFNMAP vma, we don't touch page +* structures. Caller should make sure pfns aren't reused for anything else +* while he is using them. +* +* This function takes care of grabbing mmap_sem as necessary. +*/ +int get_vaddr_pfns (unsigned long start,int nr_pfns,int write,int force, +struct pinned_pfns *pfns) +{ +struct mm_struct *mm = current->mm; +struct vm_area_struct *vma; +int ret =0 ; +int err; + +if (nr_pfns <=0 ) +return 0 ; + +if (nr_pfns > pfns->nr_allocated_pfns) + nr_pfns = pfns->nr_allocated_pfns; + +down_read (&mm->mmap_sem); + vma =find_vma_intersection (mm, start, start +1 ); +if (!vma) { + ret = -EFAULT ; + goto out; + } +if (!(vma->vm_flags & (VM_IO |VM_PFNMAP ))) { + pfns->got_ref =1 ; + pfns->is_pages =1 ; + ret =get_user_pages (current, mm, start, nr_pfns, write, force, +pfns_vector_pages (pfns),NULL ); + goto out; + } + + pfns->got_ref =0 ; + pfns->is_pages =0 ; +do { +unsigned long *nums =pfns_vector_pfns (pfns); + +while (ret < nr_pfns && start +PAGE_SIZE <= vma->vm_end) { + err =follow_pfn (vma, start, &nums[ret]); +if (err) { +if (ret ==0 ) + ret = err; + goto out; + } + start +=PAGE_SIZE ; + ret++; + } +/* +* We stop if we have enough pages or if VMA doesn't completely +* cover the tail page. +*/ +if (ret >= nr_pfns || start < vma->vm_end) +break ; + vma =find_vma_intersection (mm, start, start +1 ); + }while (vma && vma->vm_flags & (VM_IO |VM_PFNMAP )); +out: +up_read (&mm->mmap_sem); +if (!ret) + ret = -EFAULT ; +if (ret >0 ) + pfns->nr_pfns = ret; +return ret; +} +EXPORT_SYMBOL (get_vaddr_pfns); + +/** +* put_vaddr_pfns() - drop references to pages if get_vaddr_pfns() acquired them +* @pfns: structure with pfns we pinned +* +* Drop references to pages if get_vaddr_pfns() acquired them. We also +* invalidate the array of pfns so that it is prepared for the next call into +* get_vaddr_pfns(). +*/ +void put_vaddr_pfns (struct pinned_pfns *pfns) +{ +int i; + +if (!pfns->got_ref) + goto out; +if (pfns->is_pages) { +struct page **pages =pfns_vector_pages (pfns); + +for (i =0 ; i < pfns->nr_pfns; i++) +put_page (pages[i]); + }else { +unsigned long *nums =pfns_vector_pfns (pfns); + +for (i =0 ; i < pfns->nr_pfns; i++) +put_page (pfn_to_page (nums[i])); + } + pfns->got_ref =0 ; +out: + pfns->nr_pfns =0 ; +} +EXPORT_SYMBOL (put_vaddr_pfns); + +/** +* pfns_vector_to_pages - convert vector of pfns to vector of page pointers +* @pfns: structure with pinned pfns +* +* Convert @pfns to not contain array of pfns but array of page pointers. +* If the conversion is successful, return 0. Otherwise return an error. +*/ +int pfns_vector_to_pages (struct pinned_pfns *pfns) +{ +int i; +unsigned long *nums; +struct page **pages; + +if (pfns->is_pages) +return 0 ; + nums =pfns_vector_pfns (pfns); +for (i =0 ; i < pfns->nr_pfns; i++) +if (!pfn_valid (nums[i])) +return -EINVAL ; + pages = (struct page **)nums; +for (i =0 ; i < pfns->nr_pfns; i++) + pages[i] =pfn_to_page (nums[i]); + pfns->is_pages =1 ; +return 0 ; +} +EXPORT_SYMBOL (pfns_vector_to_pages); + +/** +* pfns_vector_create() - allocate & initialize structure for pinned pfns +* @nr_pfns: number of pfns slots we should reserve +* +* Allocate and initialize struct pinned_pfns to be able to hold @nr_pfns +* pfns. +*/ +struct pinned_pfns *pfns_vector_create (int nr_pfns) +{ +struct pinned_pfns *pfns; +int size =sizeof (struct pinned_pfns ) +sizeof (unsigned long ) * nr_pfns; + +if (WARN_ON_ONCE (nr_pfns <=0 )) +return NULL ; +if (size <=PAGE_SIZE ) + pfns =kmalloc (size,GFP_KERNEL ); +else + pfns =vmalloc (size); +if (!pfns) +return NULL ; + pfns->nr_allocated_pfns = nr_pfns; + pfns->nr_pfns =0 ; +return 0 ; +} +EXPORT_SYMBOL (pfns_vector_create); + +/** * get_dump_page() - pin user page in memory while writing it to core dump * @addr: user address * Signature · 1 line
1.8.1.4
Signature · 2 lines
Jan Kara <[email protected]> SUSE Labs, CR
PATCH 2/9 media: omap_vout: Convert omap_vout_uservirt_to_phys() to use get_vaddr_pfns()
In reply to Jan KaraConvert omap_vout_uservirt_to_phys() to use get_vaddr_pfns() instead of hand made mapping of virtual address to physical address. Also the function leaked page reference from get_user_pages() so fix that by properly release the reference when omap_vout_buffer_release() is called.
--- drivers/media/platform/omap/omap_vout.c | 63 +++++++++++++++------------------ 1 file changed, 29 insertions(+), 34 deletions(-)
drivers/media/platform/omap/omap_vout.c +29 -34
@@ -199,43 +199,31 @@ static int omap_vout_try_format(struct v4l2_pix_format *pix)* omap_vout_uservirt_to_phys: This inline function is used to convert user * space virtual address to physical address. */ -static u32 omap_vout_uservirt_to_phys (u32 virtp) +static int omap_vout_get_userptr (struct videobuf_buffer *vb,u32 virtp, +u32 *physp) { -unsigned long physp =0 ; -struct vm_area_struct *vma; -struct mm_struct *mm = current->mm; +struct pinned_pfns *pfns; +int ret;/* For kernel direct-mapped memory, take the easy way */ -if (virtp >=PAGE_OFFSET ) -return virt_to_phys ((void *) virtp); - -down_read (¤t->mm->mmap_sem); - vma =find_vma (mm, virtp); -if (vma && (vma->vm_flags &VM_IO ) && vma->vm_pgoff) { -/* this will catch, kernel-allocated, mmaped-to-usermode -addresses */ - physp = (vma->vm_pgoff <<PAGE_SHIFT ) + (virtp - vma->vm_start); -up_read (¤t->mm->mmap_sem); - }else { -/* otherwise, use get_user_pages() for general userland pages */ -int res, nr_pages =1 ; -struct page *pages; +if (virtp >=PAGE_OFFSET ) { + *physp =virt_to_phys ((void *)virtp); +return 0 ; + } - res =get_user_pages (current, current->mm, virtp, nr_pages,1 , -0 , &pages,NULL ); -up_read (¤t->mm->mmap_sem); + pfns =pfns_vector_create (1 ); +if (!pfns) +return -ENOMEM ; -if (res == nr_pages) { - physp =__pa (page_address (&pages[0 ]) + - (virtp & ~PAGE_MASK )); - }else { - printk(KERN_WARNING VOUT_NAME - "get_user_pages failed\n"); -return 0 ; - } + ret =get_vaddr_pfns (virtp,1 ,1 ,0 , pfns); +if (ret !=1 ) { +pfns_vector_destroy (pfns); +return -EINVAL ; } + *physp =__pfn_to_phys (pfns_vector_pfns (pfns)[0 ]); + vb->priv = pfns; -return physp; +return 0 ; }/*
@@ -790,11 +778,15 @@ static int omap_vout_buffer_prepare(struct videobuf_queue *q,* address of the buffer */ if (V4L2_MEMORY_USERPTR == vb->memory) { +int ret; +if (0 == vb->baddr)return -EINVAL ;/* Physical address */ - vout->queued_buf_addr[vb->i] = (u8 *) -omap_vout_uservirt_to_phys (vb->baddr); + ret =omap_vout_get_userptr (vb, vb->baddr, + (u32 *)&vout->queued_buf_addr[vb->i]); +if (ret <0 ) +return ret; }else {u32 addr, dma_addr;unsigned long size;
@@ -843,9 +835,12 @@ static void omap_vout_buffer_release(struct videobuf_queue *q,struct omap_vout_device *vout = q->priv_data; vb->state =VIDEOBUF_NEEDS_INIT ; +if (vb->memory ==V4L2_MEMORY_USERPTR && vb->priv) { +struct pinned_pfns *pfns = vb->priv; -if (V4L2_MEMORY_MMAP != vout->memory) -return ; +put_vaddr_pfns (pfns); +pfns_vector_destroy (pfns); + } }/*
Signature · 1 line
1.8.1.4
PATCH 3/9 media: vb2: Teach vb2_queue_or_prepare_buf() to get pfns for user buffers
In reply to Jan KaraTeach vb2_queue_or_prepare_buf() to get pfns underlying these buffers and propagate them down to get_userptr callback. Thus each buffer mapping method doesn't have to get pfns independently. Also this will remove the knowledge about mmap_sem locking from videobuf2 core.
--- drivers/media/v4l2-core/videobuf2-core.c | 121 ++++++++++++++++++++++++- drivers/media/v4l2-core/videobuf2-dma-contig.c | 5 +- drivers/media/v4l2-core/videobuf2-dma-sg.c | 5 +- drivers/media/v4l2-core/videobuf2-vmalloc.c | 5 +- include/media/videobuf2-core.h | 4 +- 5 files changed, 128 insertions(+), 12 deletions(-)
drivers/media/v4l2-core/videobuf2-core.c +116 -5
@@ -1033,7 +1033,8 @@ static void __fill_vb2_buffer(struct vb2_buffer *vb, const struct v4l2_buffer *b/** * __qbuf_userptr() - handle qbuf of a USERPTR buffer */ -static int __qbuf_userptr (struct vb2_buffer *vb,const struct v4l2_buffer *b) +static int __qbuf_userptr (struct vb2_buffer *vb,const struct v4l2_buffer *b, +struct pinned_pfns **ppfns) {struct v4l2_plane planes[VIDEO_MAX_PLANES ];struct vb2_queue *q = vb->vb2_queue;
@@ -1075,7 +1076,7 @@ static int __qbuf_userptr(struct vb2_buffer *vb, const struct v4l2_buffer *b)/* Acquire each plane's memory */ mem_priv =call_memop (q, get_userptr, q->alloc_ctx[plane], - planes[plane].m.userptr, + &ppfns[plane], planes[plane].m.userptr, planes[plane].length, write);if (IS_ERR_OR_NULL (mem_priv)) { dprintk(1, "qbuf: failed acquiring userspace "
@@ -1247,10 +1248,116 @@ static void __enqueue_in_driver(struct vb2_buffer *vb) q->ops->buf_queue (vb); } +static struct pinned_pfns *vb2_create_one_pfnvec (struct v4l2_buffer *buf, +unsigned long vaddr, +unsigned int length) +{ +int ret; +unsigned long first, last; +unsigned long nr; +struct pinned_pfns *pfns; + + first = vaddr >>PAGE_SHIFT ; + last = (vaddr + length -1 ) >>PAGE_SHIFT ; + nr = last - first +1 ; + pfns =pfns_vector_create (nr); +if (!pfns) +return ERR_PTR (-ENOMEM ); + ret =get_vaddr_pfns (vaddr, nr, !V4L2_TYPE_IS_OUTPUT (buf->type),1 , + pfns); +if (ret <0 ) + goto out_destroy; +/* We accept only complete set of PFNs */ +if (ret != nr) { + ret = -EFAULT ; + goto out_release; + } +return pfns; +out_release: +put_vaddr_pfns (pfns); +out_destroy: +pfns_vector_destroy (pfns); +return ERR_PTR (ret); +} + +/* Create PFN vecs for all provided user buffers. */ +static struct pinned_pfns **vb2_get_user_pfns (struct v4l2_buffer *buf, +struct pinned_pfns **tmp_store) +{ +struct pinned_pfns **ppfns; +int count =0 ; +int i; +struct pinned_pfns *ret; + +if (V4L2_TYPE_IS_MULTIPLANAR (buf->type)) { +if (buf->length ==0 ) +return NULL ; + + count = buf->length; + + ppfns =kzalloc (sizeof (struct pinned_pfns *) * count, +GFP_KERNEL ); +if (!ppfns) +return ERR_PTR (-ENOMEM ); + +for (i =0 ; i < count; i++) { + ret =vb2_create_one_pfnvec (buf, + buf->m.planes[i].m.userptr, + buf->m.planes[i].length); +if (IS_ERR (ret)) + goto out_release; + ppfns[i] = ret; + } + }else { + count =1 ; + +/* Save one kmalloc for the simple case */ + ppfns = tmp_store; + ppfns[0 ] =vb2_create_one_pfnvec (buf, buf->m.userptr, + buf->length); +if (IS_ERR (ppfns[0 ])) +return ppfns[0 ]; + } + +return ppfns; +out_release: +for (i =0 ; i < count && ppfns[i]; i++) { +put_vaddr_pfns (ppfns[i]); +pfns_vector_destroy (ppfns[i]); + } +kfree (ppfns); +return ret; +} + +/* Release PFN vecs the call did not consume */ +static void vb2_put_user_pfns (struct v4l2_buffer *buf, +struct pinned_pfns **ppfns, +struct pinned_pfns **tmp_store) +{ +int i; +int count; + +if (V4L2_TYPE_IS_MULTIPLANAR (buf->type)) + count = buf->length; +else + count =1 ; + +for (i =0 ; i < count; i++) +if (ppfns[i]) { +put_vaddr_pfns (ppfns[i]); +pfns_vector_destroy (ppfns[i]); + } + +if (ppfns != tmp_store) +kfree (ppfns); +} +static int __buf_prepare (struct vb2_buffer *vb,const struct v4l2_buffer *b) {struct vb2_queue *q = vb->vb2_queue; -struct rw_semaphore *mmap_sem; +struct rw_semaphore *mmap_sem =NULL ; +struct pinned_pfns *tmp_store; +struct pinned_pfns **ppfns =NULL ;int ret; ret =__verify_length (vb, b);
@@ -1280,11 +1387,15 @@ static int __buf_prepare(struct vb2_buffer *vb, const struct v4l2_buffer *b)*/ mmap_sem = ¤t->mm->mmap_sem;call_qop (q, wait_prepare, q); + ppfns =vb2_get_user_pfns (b, &tmp_store);down_read (mmap_sem);call_qop (q, wait_finish, q); - ret =__qbuf_userptr (vb, b); - +if (!IS_ERR (ppfns)) { + ret =__qbuf_userptr (vb, b, ppfns); +vb2_put_user_pfns (b, ppfns, &tmp_store); + }else + ret =PTR_ERR (ppfns);up_read (mmap_sem);break ;case V4L2_MEMORY_DMABUF :
drivers/media/v4l2-core/videobuf2-dma-contig.c +3 -2
@@ -547,8 +547,9 @@ static inline dma_addr_t vb2_dc_pfn_to_dma(struct device *dev, unsigned long pfn }#endif -static void *vb2_dc_get_userptr (void *alloc_ctx,unsigned long vaddr, -unsigned long size,int write) +static void *vb2_dc_get_userptr (void *alloc_ctx,struct pinned_pfns **ppfn, +unsigned long vaddr,unsigned long size, +int write) {struct vb2_dc_conf *conf = alloc_ctx;struct vb2_dc_buf *buf;
drivers/media/v4l2-core/videobuf2-dma-sg.c +3 -2
@@ -161,8 +161,9 @@ static inline int vma_is_io(struct vm_area_struct *vma)return !!(vma->vm_flags & (VM_IO |VM_PFNMAP )); } -static void *vb2_dma_sg_get_userptr (void *alloc_ctx,unsigned long vaddr, -unsigned long size,int write) +static void *vb2_dma_sg_get_userptr (void *alloc_ctx,struct pinned_pfns **ppfn, +unsigned long vaddr,unsigned long size, +int write) {struct vb2_dma_sg_buf *buf;unsigned long first, last;
drivers/media/v4l2-core/videobuf2-vmalloc.c +3 -2
@@ -69,8 +69,9 @@ static void vb2_vmalloc_put(void *buf_priv) } } -static void *vb2_vmalloc_get_userptr (void *alloc_ctx,unsigned long vaddr, -unsigned long size,int write) +static void *vb2_vmalloc_get_userptr (void *alloc_ctx,struct pinned_pfns **ppfn, +unsigned long vaddr,unsigned long size, +int write) {struct vb2_vmalloc_buf *buf;unsigned long first, last;
include/media/videobuf2-core.h +3 -1
@@ -85,7 +85,9 @@ struct vb2_mem_ops {void (*put)(void *buf_priv);struct dma_buf *(*get_dmabuf)(void *buf_priv,unsigned long flags); -void *(*get_userptr)(void *alloc_ctx,unsigned long vaddr, +void *(*get_userptr)(void *alloc_ctx, +struct pinned_pfns **pfns, +unsigned long vaddr,unsigned long size,int write);void (*put_userptr)(void *buf_priv);
Signature · 1 line
1.8.1.4
PATCH 4/9 media: vb2: Convert vb2_dma_sg_get_userptr() to use pinned pfns
In reply to Jan Kara--- drivers/media/v4l2-core/videobuf2-dma-sg.c | 85 ++++++------------------------ 1 file changed, 15 insertions(+), 70 deletions(-)
drivers/media/v4l2-core/videobuf2-dma-sg.c +15 -70
@@ -33,6 +33,7 @@ module_param(debug, int, 0644);struct vb2_dma_sg_buf {void *vaddr;struct page **pages; +struct pinned_pfns *pfns;int write;int offset;struct sg_table sg_table;
@@ -166,9 +167,10 @@ static void *vb2_dma_sg_get_userptr(void *alloc_ctx, struct pinned_pfns **ppfn, int write) {struct vb2_dma_sg_buf *buf; -unsigned long first, last; -int num_pages_from_user; -struct vm_area_struct *vma; +struct pinned_pfns *pfns = *ppfn; + +if (pfns_vector_to_pages (pfns)) +return NULL ; buf =kzalloc (sizeof *buf,GFP_KERNEL );if (!buf)
@@ -178,75 +180,20 @@ static void *vb2_dma_sg_get_userptr(void *alloc_ctx, struct pinned_pfns **ppfn, buf->write = write; buf->offset = vaddr & ~PAGE_MASK ; buf->size = size; - - first = (vaddr &PAGE_MASK ) >>PAGE_SHIFT ; - last = ((vaddr + size -1 ) &PAGE_MASK ) >>PAGE_SHIFT ; - buf->num_pages = last - first +1 ; - - buf->pages =kzalloc (buf->num_pages *sizeof (struct page *), -GFP_KERNEL ); -if (!buf->pages) - goto userptr_fail_alloc_pages; - - vma =find_vma (current->mm, vaddr); -if (!vma) { -dprintk (1 ,"no vma for address %lu\n" , vaddr); - goto userptr_fail_find_vma; - } - -if (vma->vm_end < vaddr + size) { -dprintk (1 ,"vma at %lu is too small for %lu bytes\n" , - vaddr, size); - goto userptr_fail_find_vma; - } - - buf->vma =vb2_get_vma (vma); -if (!buf->vma) { -dprintk (1 ,"failed to copy vma\n" ); - goto userptr_fail_find_vma; - } - -if (vma_is_io (buf->vma)) { -for (num_pages_from_user =0 ; - num_pages_from_user < buf->num_pages; - ++num_pages_from_user, vaddr +=PAGE_SIZE ) { -unsigned long pfn; - -if (follow_pfn (buf->vma, vaddr, &pfn)) { -dprintk (1 ,"no page for address %lu\n" , vaddr); -break ; - } - buf->pages[num_pages_from_user] =pfn_to_page (pfn); - } - }else - num_pages_from_user =get_user_pages (current, current->mm, - vaddr &PAGE_MASK , - buf->num_pages, - write, -1 ,/* force */ - buf->pages, -NULL ); - -if (num_pages_from_user != buf->num_pages) - goto userptr_fail_get_user_pages; + buf->pages =pfns_vector_pages (pfns); + buf->num_pages =pfns_vector_count (pfns);if (sg_alloc_table_from_pages (&buf->sg_table, buf->pages, buf->num_pages, buf->offset, size,0 )) - goto userptr_fail_alloc_table_from_pages; + goto fail; + + buf->pfns = pfns; +/* Clear *ppfn so that the caller doesn't free the vector */ + *ppfn =NULL ;return buf; -userptr_fail_alloc_table_from_pages: -userptr_fail_get_user_pages: -dprintk (1 ,"get_user_pages requested/got: %d/%d]\n" , - buf->num_pages, num_pages_from_user); -if (!vma_is_io (buf->vma)) -while (--num_pages_from_user >=0 ) -put_page (buf->pages[num_pages_from_user]); -vb2_put_vma (buf->vma); -userptr_fail_find_vma: -kfree (buf->pages); -userptr_fail_alloc_pages: +fail:kfree (buf);return NULL ; }
@@ -268,11 +215,9 @@ static void vb2_dma_sg_put_userptr(void *buf_priv)while (--i >=0 ) {if (buf->write)set_page_dirty_lock (buf->pages[i]); -if (!vma_is_io (buf->vma)) -put_page (buf->pages[i]); } -kfree (buf->pages); -vb2_put_vma (buf->vma); +put_vaddr_pfns (buf->pfns); +pfns_vector_destroy (buf->pfns);kfree (buf); }
Signature · 1 line
1.8.1.4
PATCH 5/9 media: vb2: Convert vb2_vmalloc_get_userptr() to use pfns vector
In reply to Jan KaraConvert vb2_vmalloc_get_userptr() to use passed vector of pfns. When we are doing that there's no need to allocate page array and some code can be simplified.
--- drivers/media/v4l2-core/videobuf2-vmalloc.c | 86 +++++++++++------------------ 1 file changed, 33 insertions(+), 53 deletions(-)
drivers/media/v4l2-core/videobuf2-vmalloc.c +33 -53
@@ -23,11 +23,9 @@struct vb2_vmalloc_buf {void *vaddr; -struct page **pages; -struct vm_area_struct *vma; +struct pinned_pfns *pfns;int write;unsigned long size; -unsigned int n_pages;atomic_t refcount;struct vb2_vmarea_handler handler;struct dma_buf *dbuf;
@@ -74,10 +72,8 @@ static void *vb2_vmalloc_get_userptr(void *alloc_ctx, struct pinned_pfns **ppfn, int write) {struct vb2_vmalloc_buf *buf; -unsigned long first, last; -int n_pages, offset; -struct vm_area_struct *vma; -dma_addr_t physp; +struct pinned_pfns *pfns = *ppfn; +int n_pages, offset, i; buf =kzalloc (sizeof (*buf),GFP_KERNEL );if (!buf)
@@ -87,49 +83,32 @@ static void *vb2_vmalloc_get_userptr(void *alloc_ctx, struct pinned_pfns **ppfn, offset = vaddr & ~PAGE_MASK ; buf->size = size; - - vma =find_vma (current->mm, vaddr); -if (vma && (vma->vm_flags &VM_PFNMAP ) && (vma->vm_pgoff)) { -if (vb2_get_contig_userptr (vaddr, size, &vma, &physp)) - goto fail_pages_array_alloc; - buf->vma = vma; - buf->vaddr =ioremap_nocache (physp, size); -if (!buf->vaddr) - goto fail_pages_array_alloc; + n_pages =pfns_vector_count (pfns); +if (pfns_vector_to_pages (pfns) <0 ) { +unsigned long *nums =pfns_vector_pfns (pfns); + +/* +* We cannot get page pointers for these pfns. Check memory is +* physically contiguous and use direct mapping. +*/ +for (i =1 ; i < n_pages; i++) +if (nums[i-1 ] +1 != nums[i]) + goto out_buf; + buf->vaddr =ioremap_nocache (nums[0 ] <<PAGE_SHIFT , size); }else { - first = vaddr >>PAGE_SHIFT ; - last = (vaddr + size -1 ) >>PAGE_SHIFT ; - buf->n_pages = last - first +1 ; - buf->pages =kzalloc (buf->n_pages *sizeof (struct page *), -GFP_KERNEL ); -if (!buf->pages) - goto fail_pages_array_alloc; - -/* current->mm->mmap_sem is taken by videobuf2 core */ - n_pages =get_user_pages (current, current->mm, - vaddr &PAGE_MASK , buf->n_pages, - write,1 ,/* force */ - buf->pages,NULL ); -if (n_pages != buf->n_pages) - goto fail_get_user_pages; - - buf->vaddr =vm_map_ram (buf->pages, buf->n_pages,-1 , + buf->vaddr =vm_map_ram (pfns_vector_pages (pfns), n_pages,-1 ,PAGE_KERNEL ); -if (!buf->vaddr) - goto fail_get_user_pages; } +if (!buf->vaddr) + goto out_buf; + buf->pfns = pfns; +/* Clear the pointer so that the vector isn't freed by the caller */ + *ppfn =NULL ; buf->vaddr += offset;return buf; -fail_get_user_pages: -pr_debug ("get_user_pages requested/got: %d/%d]\n" , n_pages, - buf->n_pages); -while (--n_pages >=0 ) -put_page (buf->pages[n_pages]); -kfree (buf->pages); - -fail_pages_array_alloc: +out_buf:kfree (buf);return NULL ;
@@ -140,21 +119,22 @@ static void vb2_vmalloc_put_userptr(void *buf_priv)struct vb2_vmalloc_buf *buf = buf_priv;unsigned long vaddr = (unsigned long )buf->vaddr &PAGE_MASK ;unsigned int i; +struct page **pages; +unsigned int n_pages; -if (buf->pages) { +if (buf->pfns->is_pages) { + n_pages =pfns_vector_count (buf->pfns); + pages =pfns_vector_pages (buf->pfns);if (vaddr) -vm_unmap_ram ((void *)vaddr, buf->n_pages); -for (i =0 ; i < buf->n_pages; ++i) { -if (buf->write) -set_page_dirty_lock (buf->pages[i]); -put_page (buf->pages[i]); - } -kfree (buf->pages); +vm_unmap_ram ((void *)vaddr, n_pages); +if (buf->write) +for (i =0 ; i < n_pages; i++) +set_page_dirty_lock (pages[i]); }else { -if (buf->vma) -vb2_put_vma (buf->vma);iounmap (buf->vaddr); } +put_vaddr_pfns (buf->pfns); +pfns_vector_destroy (buf->pfns);kfree (buf); }
Signature · 1 line
1.8.1.4
PATCH 6/9 media: vb2: Convert vb2_dc_get_userptr() to use pfns vector
In reply to Jan KaraConvert vb2_dc_get_userptr() to use passed vector of pfns. When we are doing that there's no need to allocate page array and some code can be simplified.
--- drivers/media/v4l2-core/videobuf2-dma-contig.c | 203 ++++--------------------- 1 file changed, 30 insertions(+), 173 deletions(-)
drivers/media/v4l2-core/videobuf2-dma-contig.c +30 -173
@@ -32,15 +32,13 @@ struct vb2_dc_buf {dma_addr_t dma_addr;enum dma_data_direction dma_dir;struct sg_table *dma_sgt; +struct pinned_pfns *pfns;/* MMAP related */ struct vb2_vmarea_handler handler;atomic_t refcount;struct sg_table *sgt_base; -/* USERPTR related */ -struct vm_area_struct *vma; -/* DMABUF related */ struct dma_buf_attachment *db_attach; };
@@ -49,24 +47,6 @@ struct vb2_dc_buf {/* scatterlist table functions */ /*********************************************/ - -static void vb2_dc_sgt_foreach_page (struct sg_table *sgt, -void (*cb)(struct page *pg)) -{ -struct scatterlist *s; -unsigned int i; - -for_each_sg (sgt->sgl, s, sgt->orig_nents, i) { -struct page *page =sg_page (s); -unsigned int n_pages =PAGE_ALIGN (s->offset + s->length) - >>PAGE_SHIFT ; -unsigned int j; - -for (j =0 ; j < n_pages; ++j, ++page) -cb (page); - } -} -static unsigned long vb2_dc_get_contiguous_size (struct sg_table *sgt) {struct scatterlist *s;
@@ -418,101 +398,24 @@ static struct dma_buf *vb2_dc_get_dmabuf(void *buf_priv, unsigned long flags)/* callbacks for USERPTR buffers */ /*********************************************/ -static inline int vma_is_io (struct vm_area_struct *vma) -{ -return !!(vma->vm_flags & (VM_IO |VM_PFNMAP )); -} - -static int vb2_dc_get_user_pfn (unsigned long start,int n_pages, -struct vm_area_struct *vma,unsigned long *res) -{ -unsigned long pfn, start_pfn, prev_pfn; -unsigned int i; -int ret; - -if (!vma_is_io (vma)) -return -EFAULT ; - - ret =follow_pfn (vma, start, &pfn); -if (ret) -return ret; - - start_pfn = pfn; - start +=PAGE_SIZE ; - -for (i =1 ; i < n_pages; ++i, start +=PAGE_SIZE ) { - prev_pfn = pfn; - ret =follow_pfn (vma, start, &pfn); - -if (ret) { -pr_err ("no page for address %lu\n" , start); -return ret; - } -if (pfn != prev_pfn +1 ) -return -EINVAL ; - } - - *res = start_pfn; -return 0 ; -} - -static int vb2_dc_get_user_pages (unsigned long start,struct page **pages, -int n_pages,struct vm_area_struct *vma,int write) -{ -if (vma_is_io (vma)) { -unsigned int i; - -for (i =0 ; i < n_pages; ++i, start +=PAGE_SIZE ) { -unsigned long pfn; -int ret =follow_pfn (vma, start, &pfn); - -if (!pfn_valid (pfn)) -return -EINVAL ; - -if (ret) { -pr_err ("no page for address %lu\n" , start); -return ret; - } - pages[i] =pfn_to_page (pfn); - } - }else { -int n; - - n =get_user_pages (current, current->mm, start &PAGE_MASK , - n_pages, write,1 , pages,NULL ); -/* negative error means that no page was pinned */ - n =max (n,0 ); -if (n != n_pages) { -pr_err ("got only %d of %d user pages\n" , n, n_pages); -while (n) -put_page (pages[--n]); -return -EFAULT ; - } - } - -return 0 ; -} - -static void vb2_dc_put_dirty_page (struct page *page) -{ -set_page_dirty_lock (page); -put_page (page); -}static void vb2_dc_put_userptr (void *buf_priv) {struct vb2_dc_buf *buf = buf_priv;struct sg_table *sgt = buf->dma_sgt; +int i; +struct page **pages;if (sgt) {dma_unmap_sg (buf->dev, sgt->sgl, sgt->orig_nents, buf->dma_dir); -if (!vma_is_io (buf->vma)) -vb2_dc_sgt_foreach_page (sgt, vb2_dc_put_dirty_page); - + pages =pfns_vector_pages (buf->pfns); +for (i =0 ; i <pfns_vector_count (buf->pfns); i++) +set_page_dirty_lock (pages[i]);sg_free_table (sgt);kfree (sgt); } -vb2_put_vma (buf->vma); +put_vaddr_pfns (buf->pfns); +pfns_vector_destroy (buf->pfns);kfree (buf); }
@@ -553,13 +456,10 @@ static void *vb2_dc_get_userptr(void *alloc_ctx, struct pinned_pfns **ppfn, {struct vb2_dc_conf *conf = alloc_ctx;struct vb2_dc_buf *buf; -unsigned long start; -unsigned long end; +struct pinned_pfns *pfns = *ppfn;unsigned long offset; -struct page **pages; -int n_pages; +int n_pages, i;int ret =0 ; -struct vm_area_struct *vma;struct sg_table *sgt;unsigned long contig_size;unsigned long dma_align =dma_get_cache_alignment ();
@@ -582,72 +482,38 @@ static void *vb2_dc_get_userptr(void *alloc_ctx, struct pinned_pfns **ppfn, buf->dev = conf->dev; buf->dma_dir = write ?DMA_FROM_DEVICE :DMA_TO_DEVICE ; - start = vaddr &PAGE_MASK ; offset = vaddr & ~PAGE_MASK ; - end =PAGE_ALIGN (vaddr + size); - n_pages = (end - start) >>PAGE_SHIFT ; - - pages =kmalloc (n_pages *sizeof (pages[0 ]),GFP_KERNEL ); -if (!pages) { - ret = -ENOMEM ; -pr_err ("failed to allocate pages table\n" ); - goto fail_buf; - } - -/* current->mm->mmap_sem is taken by videobuf2 core */ - vma =find_vma (current->mm, vaddr); -if (!vma) { -pr_err ("no vma for address %lu\n" , vaddr); - ret = -EFAULT ; - goto fail_pages; - } - -if (vma->vm_end < vaddr + size) { -pr_err ("vma at %lu is too small for %lu bytes\n" , vaddr, size); - ret = -EFAULT ; - goto fail_pages; - } + n_pages =pfns_vector_count (pfns); - buf->vma =vb2_get_vma (vma); -if (!buf->vma) { -pr_err ("failed to copy vma\n" ); - ret = -ENOMEM ; - goto fail_pages; - } + ret =pfns_vector_to_pages (pfns); +if (ret <0 ) { +unsigned long *nums =pfns_vector_pfns (pfns); -/* extract page list from userspace mapping */ - ret =vb2_dc_get_user_pages (start, pages, n_pages, vma, write); -if (ret) { -unsigned long pfn; -if (vb2_dc_get_user_pfn (start, n_pages, vma, &pfn) ==0 ) { - buf->dma_addr =vb2_dc_pfn_to_dma (buf->dev, pfn); - buf->size = size; -kfree (pages); -return buf; - } - -pr_err ("failed to get user pages\n" ); - goto fail_vma; +/* +* Failed to convert to pages... Check the memory is physically +* contiguous and use direct mapping +*/ +for (i =1 ; i < n_pages; i++) +if (nums[i-1 ] +1 != nums[i]) + goto fail_buf; + buf->dma_addr =vb2_dc_pfn_to_dma (buf->dev, nums[0 ]); + goto out; } sgt =kzalloc (sizeof (*sgt),GFP_KERNEL );if (!sgt) {pr_err ("failed to allocate sg table\n" ); ret = -ENOMEM ; - goto fail_get_user_pages; + goto fail_buf; } - ret =sg_alloc_table_from_pages (sgt, pages, n_pages, + ret =sg_alloc_table_from_pages (sgt,pfns_vector_pages (pfns), n_pages, offset, size,GFP_KERNEL );if (ret) {pr_err ("failed to initialize sg table\n" ); goto fail_sgt; } -/* pages are no longer needed */ -kfree (pages); - pages =NULL ; - sgt->nents =dma_map_sg (buf->dev, sgt->sgl, sgt->orig_nents, buf->dma_dir);if (sgt->nents <=0 ) {
@@ -665,8 +531,12 @@ static void *vb2_dc_get_userptr(void *alloc_ctx, struct pinned_pfns **ppfn, } buf->dma_addr =sg_dma_address (sgt->sgl); - buf->size = size; buf->dma_sgt = sgt; +out: + buf->size = size; + buf->pfns = pfns; +/* Clear *ppfn so that the caller doesn't free the pfn vector */ + *ppfn =NULL ;return buf;
@@ -674,24 +544,11 @@ fail_map_sg:dma_unmap_sg (buf->dev, sgt->sgl, sgt->orig_nents, buf->dma_dir); fail_sgt_init: -if (!vma_is_io (buf->vma)) -vb2_dc_sgt_foreach_page (sgt, put_page);sg_free_table (sgt); fail_sgt:kfree (sgt); -fail_get_user_pages: -if (pages && !vma_is_io (buf->vma)) -while (n_pages) -put_page (pages[--n_pages]); - -fail_vma: -vb2_put_vma (buf->vma); - -fail_pages: -kfree (pages);/* kfree is NULL-proof */ - fail_buf:kfree (buf);
Signature · 1 line
1.8.1.4
PATCH 7/9 media: vb2: Remove unused functions
In reply to Jan KaraConversion to the use of pinned pfns made some functions unused. Remove them. Also there's no need to lock mmap_sem in __buf_prepare() anymore.
--- drivers/media/v4l2-core/videobuf2-core.c | 8 +- drivers/media/v4l2-core/videobuf2-memops.c | 114 ----------------------------- include/media/videobuf2-memops.h | 7 -- 3 files changed, 2 insertions(+), 127 deletions(-)
drivers/media/v4l2-core/videobuf2-core.c +2 -6
@@ -1355,7 +1355,6 @@ static void vb2_put_user_pfns(struct v4l2_buffer *buf,static int __buf_prepare (struct vb2_buffer *vb,const struct v4l2_buffer *b) {struct vb2_queue *q = vb->vb2_queue; -struct rw_semaphore *mmap_sem =NULL ;struct pinned_pfns *tmp_store;struct pinned_pfns **ppfns =NULL ;int ret;
@@ -1374,8 +1373,8 @@ static int __buf_prepare(struct vb2_buffer *vb, const struct v4l2_buffer *b)break ;case V4L2_MEMORY_USERPTR :/* -* In case of user pointer buffers vb2 allocators need to get -* direct access to userspace pages. This requires getting +* In case of user pointer buffers vb2_get_user_pfns() needs to +* get direct access to userspace pages. This requires getting * the mmap semaphore for read access in the current process * structure. The same semaphore is taken before calling mmap * operation, while both qbuf/prepare_buf and mmap are called
@@ -1385,10 +1384,8 @@ static int __buf_prepare(struct vb2_buffer *vb, const struct v4l2_buffer *b)* the videobuf2 core releases the driver's lock, takes * mmap_sem and then takes the driver's lock again. */ - mmap_sem = ¤t->mm->mmap_sem;call_qop (q, wait_prepare, q); ppfns =vb2_get_user_pfns (b, &tmp_store); -down_read (mmap_sem);call_qop (q, wait_finish, q);if (!IS_ERR (ppfns)) {
@@ -1396,7 +1393,6 @@ static int __buf_prepare(struct vb2_buffer *vb, const struct v4l2_buffer *b)vb2_put_user_pfns (b, ppfns, &tmp_store); }else ret =PTR_ERR (ppfns); -up_read (mmap_sem);break ;case V4L2_MEMORY_DMABUF : ret =__qbuf_dmabuf (vb, b);
drivers/media/v4l2-core/videobuf2-memops.c +0 -114
120 lines not on this page · raw message
include/media/videobuf2-memops.h +0 -7
@@ -30,11 +30,4 @@ struct vb2_vmarea_handler {extern const struct vm_operations_struct vb2_common_vm_ops; -int vb2_get_contig_userptr (unsigned long vaddr,unsigned long size, -struct vm_area_struct **res_vma,dma_addr_t *res_pa); - -struct vm_area_struct *vb2_get_vma (struct vm_area_struct *vma); -void vb2_put_vma (struct vm_area_struct *vma); - -#endif
Signature · 1 line
1.8.1.4
PATCH 8/9 drm/exynos: Convert g2d_userptr_get_dma_addr() to use get_vaddr_pfn()
In reply to Jan KaraConvert g2d_userptr_get_dma_addr() to pin pages using get_vaddr_pfn(). This removes the knowledge about vmas and mmap_sem locking from exynos driver. Also it fixes a problem that the function has been mapping user provided address without holding mmap_sem.
--- drivers/gpu/drm/exynos/exynos_drm_g2d.c | 85 ++++++++++------------------- drivers/gpu/drm/exynos/exynos_drm_gem.c | 97 --------------------------------- 2 files changed, 30 insertions(+), 152 deletions(-)
drivers/gpu/drm/exynos/exynos_drm_g2d.c +30 -55
@@ -190,10 +190,8 @@ struct g2d_cmdlist_userptr {dma_addr_t dma_addr;unsigned long userptr;unsigned long size; -struct page **pages; -unsigned int npages; +struct pinned_pfns *pfns;struct sg_table *sgt; -struct vm_area_struct *vma;atomic_t refcount;bool in_pool;bool out_of_list;
@@ -360,6 +358,7 @@ static void g2d_userptr_put_dma_addr(struct drm_device *drm_dev, {struct g2d_cmdlist_userptr *g2d_userptr = (struct g2d_cmdlist_userptr *)obj; +struct page **pages;if (!obj)return ;
@@ -379,19 +378,21 @@ out:exynos_gem_unmap_sgt_from_dma (drm_dev, g2d_userptr->sgt,DMA_BIDIRECTIONAL ); -exynos_gem_put_pages_to_userptr (g2d_userptr->pages, - g2d_userptr->npages, - g2d_userptr->vma); + pages =pfns_vector_pages (g2d_userptr->pfns); +if (pages) { +int i; -exynos_gem_put_vma (g2d_userptr->vma); +for (i =0 ; i <pfns_vector_count (g2d_userptr->pfns); i++) +set_page_dirty_lock (pages[i]); + } +put_vaddr_pfns (g2d_userptr->pfns); +pfns_vector_destroy (g2d_userptr->pfns);if (!g2d_userptr->out_of_list)list_del_init (&g2d_userptr->list);sg_free_table (g2d_userptr->sgt);kfree (g2d_userptr->sgt); - -drm_free_large (g2d_userptr->pages);kfree (g2d_userptr); }
@@ -410,6 +411,7 @@ static dma_addr_t *g2d_userptr_get_dma_addr(struct drm_device *drm_dev,struct vm_area_struct *vma;unsigned long start, end;unsigned int npages, offset; +struct pinned_pfns *pfns;int ret;if (!size) {
@@ -453,59 +455,37 @@ static dma_addr_t *g2d_userptr_get_dma_addr(struct drm_device *drm_dev,return ERR_PTR (-ENOMEM );atomic_set (&g2d_userptr->refcount,1 ); + g2d_userptr->size = size; start = userptr &PAGE_MASK ; offset = userptr & ~PAGE_MASK ; end =PAGE_ALIGN (userptr + size); npages = (end - start) >>PAGE_SHIFT ; - g2d_userptr->npages = npages; + pfns = g2d_userptr->pfns =pfns_vector_create (npages); +if (!pfns) + goto out_free; - pages =drm_calloc_large (npages,sizeof (struct page *)); -if (!pages) { -DRM_ERROR ("failed to allocate pages.\n" ); - ret = -ENOMEM ; - goto err_free; - } - - vma =find_vma (current->mm, userptr); -if (!vma) { -DRM_ERROR ("failed to get vm region.\n" ); + ret =get_vaddr_pfn (start, npages,1 ,1 , pfns); +if (ret != npages) { +DRM_ERROR ("failed to get user pages from userptr.\n" ); +if (ret <0 ) + goto err_destroy_pfns; ret = -EFAULT ; - goto err_free_pages; + goto err_put_pfns; } - -if (vma->vm_end < userptr + size) { -DRM_ERROR ("vma is too small.\n" ); +if (pfns_vector_to_pages (pfns) <0 ) { ret = -EFAULT ; - goto err_free_pages; + goto err_put_pfns; } - g2d_userptr->vma =exynos_gem_get_vma (vma); -if (!g2d_userptr->vma) { -DRM_ERROR ("failed to copy vma.\n" ); - ret = -ENOMEM ; - goto err_free_pages; - } - - g2d_userptr->size = size; - - ret =exynos_gem_get_pages_from_userptr (start &PAGE_MASK , - npages, pages, vma); -if (ret <0 ) { -DRM_ERROR ("failed to get user pages from userptr.\n" ); - goto err_put_vma; - } - - g2d_userptr->pages = pages; - sgt =kzalloc (sizeof (*sgt),GFP_KERNEL );if (!sgt) { ret = -ENOMEM ; - goto err_free_userptr; + goto err_put_pfns; } - ret =sg_alloc_table_from_pages (sgt, pages, npages, offset, - size,GFP_KERNEL ); + ret =sg_alloc_table_from_pages (sgt,pfns_vector_pages (pfns), npages, + offset, size,GFP_KERNEL );if (ret <0 ) {DRM_ERROR ("failed to get sgt from pages.\n" ); goto err_free_sgt;
@@ -540,16 +520,11 @@ err_sg_free_table: err_free_sgt:kfree (sgt); -err_free_userptr: -exynos_gem_put_pages_to_userptr (g2d_userptr->pages, - g2d_userptr->npages, - g2d_userptr->vma); - -err_put_vma: -exynos_gem_put_vma (g2d_userptr->vma); +err_put_pfns: +put_vaddr_pfns (pfns); -err_free_pages: -drm_free_large (pages); +err_destroy_pfns: +pfns_vector_destroy (pfns); err_free:kfree (g2d_userptr);
drivers/gpu/drm/exynos/exynos_drm_gem.c +0 -97
103 lines not on this page · raw message
Signature · 1 line
1.8.1.4
PATCH 9/9 staging: tidspbridge: Convert to get_vaddr_pfns()
In reply to Jan KaraConvert the driver to use get_vaddr_pfns() instead of get_user_pages() or a hand made page table walk. This removes knowledge about vmas and mmap_sem locking from the driver.
--- drivers/staging/tidspbridge/core/tiomap3430.c | 261 ++++----------------- .../staging/tidspbridge/include/dspbridge/drv.h | 3 +- .../tidspbridge/include/dspbridge/dspdefs.h | 9 +- drivers/staging/tidspbridge/rmgr/proc.c | 66 ++++-- 4 files changed, 91 insertions(+), 248 deletions(-)
drivers/staging/tidspbridge/core/tiomap3430.c +45 -216
@@ -100,9 +100,10 @@ static int bridge_brd_mem_write(struct bridge_dev_context *dev_ctxt,static int bridge_brd_mem_map (struct bridge_dev_context *dev_ctxt,u32 ul_mpu_addr,u32 virt_addr,u32 ul_num_bytes,u32 ul_map_attr, -struct page **mapped_pages); +struct pinned_pfns *pfns);static int bridge_brd_mem_un_map (struct bridge_dev_context *dev_ctxt, -u32 virt_addr,u32 ul_num_bytes); +u32 virt_addr,u32 ul_num_bytes, +struct pinned_pfns *pfns); static int bridge_dev_create(struct bridge_dev_context **dev_cntxt, struct dev_object *hdev_obj,
@@ -1122,24 +1123,18 @@ static int bridge_brd_mem_write(struct bridge_dev_context *dev_ctxt,* TODO: Disable MMU while updating the page tables (but that'll stall DSP) */ static int bridge_brd_mem_map (struct bridge_dev_context *dev_ctxt, -u32 ul_mpu_addr,u32 virt_addr, -u32 ul_num_bytes,u32 ul_map_attr, -struct page **mapped_pages) +u32 ul_mpu_addr,u32 virt_addr,u32 ul_num_bytes, +u32 ul_map_attr,struct pinned_pfns *pfns) {u32 attrs;int status =0 ;struct bridge_dev_context *dev_context = dev_ctxt;struct hw_mmu_map_attrs_t hw_attrs; -struct vm_area_struct *vma; -struct mm_struct *mm = current->mm;u32 write =0 ; -u32 num_usr_pgs =0 ; -struct page *mapped_page, *pg; -s32 pg_num; +int num_usr_pgs, i;u32 va = virt_addr; -struct task_struct *curr_task = current; -u32 pg_i =0 ; -u32 mpu_addr, pa; +u32 pa; +struct page **pages; dev_dbg(bridge, "%s hDevCtxt %p, pa %x, va %x, size %x, ul_map_attr %x\n",
@@ -1201,128 +1196,36 @@ static int bridge_brd_mem_map(struct bridge_dev_context *dev_ctxt,if ((attrs &DSP_MAPPHYSICALADDR )) { status =pte_update (dev_context, ul_mpu_addr, virt_addr, ul_num_bytes, &hw_attrs); - goto func_cont; + goto out; } -/* -* Important Note: ul_mpu_addr is mapped from user application process -* to current process - it must lie completely within the current -* virtual memory address space in order to be of use to us here! -*/ -down_read (&mm->mmap_sem); - vma =find_vma (mm, ul_mpu_addr); -if (vma) -dev_dbg (bridge, -"VMAfor UserBuf: ul_mpu_addr=%x, ul_num_bytes=%x, " -"vm_start=%lx, vm_end=%lx, vm_flags=%lx\n" , ul_mpu_addr, - ul_num_bytes, vma->vm_start, vma->vm_end, - vma->vm_flags); - -/* -* It is observed that under some circumstances, the user buffer is -* spread across several VMAs. So loop through and check if the entire -* user buffer is covered -*/ -while ((vma) && (ul_mpu_addr + ul_num_bytes > vma->vm_end)) { -/* jump to the next VMA region */ - vma =find_vma (mm, vma->vm_end +1 ); -dev_dbg (bridge, -"VMA for UserBuf ul_mpu_addr=%x ul_num_bytes=%x, " -"vm_start=%lx, vm_end=%lx, vm_flags=%lx\n" , ul_mpu_addr, - ul_num_bytes, vma->vm_start, vma->vm_end, - vma->vm_flags); - } -if (!vma) { -pr_err ("%s: Failed to get VMA region for 0x%x (%d)\n" , - __func__, ul_mpu_addr, ul_num_bytes); - status = -EINVAL ; -up_read (&mm->mmap_sem); - goto func_cont; +BUG_ON (!pfns); + num_usr_pgs = ul_num_bytes /PG_SIZE4K ; + status =get_vaddr_pfns (ul_mpu_addr, num_usr_pgs, write,1 , pfns); +if (status <0 ) + goto out; +if (status != num_usr_pgs) { + status = -EFAULT ; + goto put_pfns; } -if (vma->vm_flags &VM_IO ) { - num_usr_pgs = ul_num_bytes /PG_SIZE4K ; - mpu_addr = ul_mpu_addr; - -/* Get the physical addresses for user buffer */ -for (pg_i =0 ; pg_i < num_usr_pgs; pg_i++) { - pa =user_va2_pa (mm, mpu_addr); -if (!pa) { - status = -EPERM ; -pr_err ("DSPBRIDGE: VM_IO mapping physical" -"address is invalid\n" ); -break ; - } -if (pfn_valid (__phys_to_pfn (pa))) { - pg =PHYS_TO_PAGE (pa); -get_page (pg); -if (page_count (pg) <1 ) { -pr_err ("Bad page in VM_IO buffer\n" ); -bad_page_dump (pa, pg); - } - } - status =pte_set (dev_context->pt_attrs, pa, - va,HW_PAGE_SIZE4KB , &hw_attrs); -if (status) -break ; - - va +=HW_PAGE_SIZE4KB ; - mpu_addr +=HW_PAGE_SIZE4KB ; - pa +=HW_PAGE_SIZE4KB ; - } - }else { - num_usr_pgs = ul_num_bytes /PG_SIZE4K ; -if (vma->vm_flags & (VM_WRITE |VM_MAYWRITE )) - write =1 ; - -for (pg_i =0 ; pg_i < num_usr_pgs; pg_i++) { - pg_num =get_user_pages (curr_task, mm, ul_mpu_addr,1 , - write,1 , &mapped_page,NULL ); -if (pg_num >0 ) { -if (page_count (mapped_page) <1 ) { -pr_err ("Bad page count after doing" -"get_user_pages on" -"user buffer\n" ); -bad_page_dump (page_to_phys (mapped_page), - mapped_page); - } - status =pte_set (dev_context->pt_attrs, -page_to_phys (mapped_page), va, -HW_PAGE_SIZE4KB , &hw_attrs); -if (status) -break ; - -if (mapped_pages) - mapped_pages[pg_i] = mapped_page; - - va +=HW_PAGE_SIZE4KB ; - ul_mpu_addr +=HW_PAGE_SIZE4KB ; - }else { -pr_err ("DSPBRIDGE: get_user_pages FAILED," -"MPU addr = 0x%x," -"vma->vm_flags = 0x%lx," -"get_user_pages Err" -"Value = %d, Buffer" -"size=0x%x\n" , ul_mpu_addr, - vma->vm_flags, pg_num, ul_num_bytes); - status = -EPERM ; -break ; - } - } - } -up_read (&mm->mmap_sem); -func_cont: -if (status) { -/* -* Roll out the mapped pages incase it failed in middle of -* mapping -*/ -if (pg_i) { -bridge_brd_mem_un_map (dev_context, virt_addr, - (pg_i *PG_SIZE4K )); - } - status = -EPERM ; + status =pfns_vector_to_pages (pfns); +if (status <0 ) + goto put_pfns; + + pages =pfns_vector_pages (pfns); +for (i =0 ; i < num_usr_pgs; i++) { + status =pte_set (dev_context->pt_attrs,page_to_phys (pages[i]), + va,HW_PAGE_SIZE4KB , &hw_attrs); +if (status) + goto put_pfns; + va +=HW_PAGE_SIZE4KB ; } + status =0 ; + goto out; +put_pfns: +put_vaddr_pfns (pfns); +out:/* * In any case, flush the TLB * This is called from here instead from pte_update to avoid unnecessary
@@ -1343,7 +1246,8 @@ func_cont:* we clear consecutive PTEs until we unmap all the bytes */ static int bridge_brd_mem_un_map (struct bridge_dev_context *dev_ctxt, -u32 virt_addr,u32 ul_num_bytes) +u32 virt_addr,u32 ul_num_bytes, +struct pinned_pfns *pfns) {u32 l1_base_va;u32 l2_base_va;
@@ -1357,13 +1261,10 @@ static int bridge_brd_mem_un_map(struct bridge_dev_context *dev_ctxt,u32 rem_bytes;u32 rem_bytes_l2;u32 va_curr; -struct page *pg =NULL ;int status =0 ;struct bridge_dev_context *dev_context = dev_ctxt;struct pg_table_attrs *pt = dev_context->pt_attrs; -u32 temp; -u32 paddr; -u32 numof4k_pages =0 ; +struct page **pages; va_curr = virt_addr; rem_bytes = ul_num_bytes;
@@ -1423,30 +1324,6 @@ static int bridge_brd_mem_un_map(struct bridge_dev_context *dev_ctxt,break ; } -/* Collect Physical addresses from VA */ - paddr = (pte_val & ~(pte_size -1 )); -if (pte_size ==HW_PAGE_SIZE64KB ) - numof4k_pages =16 ; -else - numof4k_pages =1 ; - temp =0 ; -while (temp++ < numof4k_pages) { -if (!pfn_valid (__phys_to_pfn (paddr))) { - paddr +=HW_PAGE_SIZE4KB ; -continue ; - } - pg =PHYS_TO_PAGE (paddr); -if (page_count (pg) <1 ) { -pr_info ("DSPBRIDGE: UNMAP function: " -"COUNT 0 FOR PA 0x%x, size = " -"0x%x\n" , paddr, ul_num_bytes); -bad_page_dump (paddr, pg); - }else { -set_page_dirty (pg); -page_cache_release (pg); - } - paddr +=HW_PAGE_SIZE4KB ; - }if (hw_mmu_pte_clear (pte_addr_l2, va_curr, pte_size)) { status = -EPERM ; goto EXIT_LOOP;
@@ -1488,28 +1365,6 @@ skip_coarse_page:break ; } -if (pte_size ==HW_PAGE_SIZE1MB ) - numof4k_pages =256 ; -else - numof4k_pages =4096 ; - temp =0 ; -/* Collect Physical addresses from VA */ - paddr = (pte_val & ~(pte_size -1 )); -while (temp++ < numof4k_pages) { -if (pfn_valid (__phys_to_pfn (paddr))) { - pg =PHYS_TO_PAGE (paddr); -if (page_count (pg) <1 ) { -pr_info ("DSPBRIDGE: UNMAP function: " -"COUNT 0 FOR PA 0x%x, size = " -"0x%x\n" , paddr, ul_num_bytes); -bad_page_dump (paddr, pg); - }else { -set_page_dirty (pg); -page_cache_release (pg); - } - } - paddr +=HW_PAGE_SIZE4KB ; - }if (!hw_mmu_pte_clear (l1_base_va, va_curr, pte_size)) { status =0 ; rem_bytes -= pte_size;
@@ -1519,6 +1374,15 @@ skip_coarse_page: goto EXIT_LOOP; } } + + pages =pfns_vector_pages (pfns); +if (pages) { +int i; + +for (i =0 ; i <pfns_vector_count (pfns); i++) +set_page_dirty (pages[i]); + } +put_vaddr_pfns (pfns);/* * It is better to flush the TLB here, so that any stale old entries * get flushed
@@ -1533,41 +1397,6 @@ EXIT_LOOP: }/* -* ======== user_va2_pa ======== -* Purpose: -* This function walks through the page tables to convert a userland -* virtual address to physical address -*/ -static u32 user_va2_pa (struct mm_struct *mm,u32 address) -{ -pgd_t *pgd; -pud_t *pud; -pmd_t *pmd; -pte_t *ptep, pte; - - pgd =pgd_offset (mm, address); -if (pgd_none (*pgd) ||pgd_bad (*pgd)) -return 0 ; - - pud =pud_offset (pgd, address); -if (pud_none (*pud) ||pud_bad (*pud)) -return 0 ; - - pmd =pmd_offset (pud, address); -if (pmd_none (*pmd) ||pmd_bad (*pmd)) -return 0 ; - - ptep =pte_offset_map (pmd, address); -if (ptep) { - pte = *ptep; -if (pte_present (pte)) -return pte &PAGE_MASK ; - } - -return 0 ; -} - -/* * ======== pte_update ======== * This function calculates the optimum page-aligned addresses and sizes * Caller must pass page-aligned values
drivers/staging/tidspbridge/include/dspbridge/drv.h +1 -2
@@ -87,8 +87,7 @@ struct dmm_map_object {u32 dsp_addr;u32 mpu_addr;u32 size; -u32 num_usr_pgs; -struct page **pages; +struct pinned_pfns *pfns;struct bridge_dma_map_info dma_info; };
drivers/staging/tidspbridge/include/dspbridge/dspdefs.h +4 -5
@@ -166,17 +166,16 @@ typedef int(*fxn_brd_memwrite) (struct bridge_dev_context* ul_num_bytes: Number of bytes to map. * map_attrs: Mapping attributes (e.g. endianness). * Returns: -* 0: Success. -* -EPERM: Other, unspecified error. +* Pointer to pinned pages / NULL: Success. +* ERR_PTR(-EPERM): Other, unspecified error. * Requires: * dev_ctxt != NULL; * Ensures: */ -typedef int (*fxn_brd_memmap ) (struct bridge_dev_context +typedef struct pinned_pfns *(*fxn_brd_memmap ) (struct bridge_dev_context * dev_ctxt,u32 ul_mpu_addr,u32 virt_addr,u32 ul_num_bytes, -u32 map_attr, -struct page **mapped_pages); +u32 map_attr);/* * ======== bridge_brd_mem_un_map ========
drivers/staging/tidspbridge/rmgr/proc.c +41 -25
157 lines not on this page · raw message
Signature · 1 line
1.8.1.4
Marek Szyprowski
maintainerHello,
Quoted 12 lines · Jan Kara
The following patch series is my first stab at abstracting vma handling
from the various media drivers. After this patch set drivers have to know much less details about vmas, their types, and locking. My motivation for the series is that I want to changeget_user_pages()locking and I want to handle subtle locking details in as few places as possible.The core of the series is the new helper
get_vaddr_pfns()which is given a virtual address and it fills in PFNs into provided array. If PFNs correspond to normal pages it also grabs references to these pages. The difference fromget_user_pages()is that this function can also deal with pfnmap, mixed, and io mappings which is what the media drivers need.
Quoted 3 lines
The patches are just compile tested (since I don't have any of the hardware I'm afraid I won't be able to do any more testing anyway) so please handle with care. I'm grateful for any comments.
Thanks for posting this series! I will check if it works with our hardware soon. This is something I wanted to introduce some time ago to simplify buffer handling in dma-buf, but I had no time to start working.
However I would like to go even further with integration of your pfn vector idea. This structure looks like a best solution for a compact representation of the memory buffer, which should be considered by the hardware as contiguous (either contiguous in physical memory or mapped contiguously into dma address space by the respective iommu). As you already noticed it is widely used by graphics and video drivers.
I would also like to add support for pfn vector directly to the dma-mapping subsystem. This can be done quite easily (even with a fallback for architectures which don't provide method for it). I will try to prepare rfc soon. This will finally remove the need for hacks in media/v4l2-core/videobuf2-dma-contig.c
Thanks for motivating me to finally start working on this!
Best regards
Signature · 2 lines
Marek Szyprowski, PhD Samsung R&D Institute Poland
Hello,
Quoted 13 lines · Marek Szyprowski
Quoted 12 lines · Jan Kara
The following patch series is my first stab at abstracting vma handling
from the various media drivers. After this patch set drivers have to know much less details about vmas, their types, and locking. My motivation for the series is that I want to changeget_user_pages()locking and I want to handle subtle locking details in as few places as possible.The core of the series is the new helper
get_vaddr_pfns()which is given a virtual address and it fills in PFNs into provided array. If PFNs correspond to normal pages it also grabs references to these pages. The difference fromget_user_pages()is that this function can also deal with pfnmap, mixed, and io mappings which is what the media drivers need.
Quoted 7 lines
Quoted 3 lines
The patches are just compile tested (since I don't have any of the hardware I'm afraid I won't be able to do any more testing anyway) so please handle with care. I'm grateful for any comments.
Thanks for posting this series! I will check if it works with our hardware soon. This is something I wanted to introduce some time ago to simplify buffer handling in dma-buf, but I had no time to start working.
Thanks for having a look in the series.
Quoted 7 lines
However I would like to go even further with integration of your pfn vector idea. This structure looks like a best solution for a compact representation of the memory buffer, which should be considered by the hardware as contiguous (either contiguous in physical memory or mapped contiguously into dma address space by the respective iommu). As you already noticed it is widely used by graphics and video drivers.
Quoted 5 lines
I would also like to add support for pfn vector directly to the dma-mapping subsystem. This can be done quite easily (even with a fallback for architectures which don't provide method for it). I will try to prepare rfc soon. This will finally remove the need for hacks in
media/v4l2-core/videobuf2-dma-contig.c
That would be a worthwhile thing to do. When I was reading the code this
seemed like something which could be done but I delibrately avoided doing more unification than necessary for my purposes as I don't have any hardware to test and don't know all the subtleties in the code... BTW, is there some way to test the drivers without the physical video HW?
Honza
Signature · 2 lines
Jan Kara <[email protected]> SUSE Labs, CR
Quoted 37 lines · Jan Kara
Hello,
Quoted 13 lines · Marek Szyprowski
Quoted 12 lines · Jan Kara
The following patch series is my first stab at abstracting vma handling
from the various media drivers. After this patch set drivers have to know much less details about vmas, their types, and locking. My motivation for the series is that I want to changeget_user_pages()locking and I want to handle subtle locking details in as few places as possible.The core of the series is the new helper
get_vaddr_pfns()which is given a virtual address and it fills in PFNs into provided array. If PFNs correspond to normal pages it also grabs references to these pages. The difference fromget_user_pages()is that this function can also deal with pfnmap, mixed, and io mappings which is what the media drivers need.Quoted 7 lines
Quoted 3 lines
The patches are just compile tested (since I don't have any of the hardware I'm afraid I won't be able to do any more testing anyway) so please handle with care. I'm grateful for any comments.
Thanks for posting this series! I will check if it works with our hardware soon. This is something I wanted to introduce some time ago to simplify buffer handling in dma-buf, but I had no time to start working.
Thanks for having a look in the series.
Quoted 7 lines
However I would like to go even further with integration of your pfn vector idea. This structure looks like a best solution for a compact representation of the memory buffer, which should be considered by the hardware as contiguous (either contiguous in physical memory or mapped contiguously into dma address space by the respective iommu). As you already noticed it is widely used by graphics and video drivers.
Quoted 5 lines
I would also like to add support for pfn vector directly to the dma-mapping subsystem. This can be done quite easily (even with a fallback for architectures which don't provide method for it). I will try to prepare rfc soon. This will finally remove the need for hacks in
media/v4l2-core/videobuf2-dma-contig.c
Quoted 5 lines
That would be a worthwhile thing to do. When I was reading the code this
seemed like something which could be done but I delibrately avoided doing more unification than necessary for my purposes as I don't have any hardware to test and don't know all the subtleties in the code... BTW, is there some way to test the drivers without the physical video HW?
You can use the vivi driver (drivers/media/platform/vivi) for this. However, while the vivi driver can import dma buffers it cannot export them. If you want that, then you have to use this tree:
http://git.linuxtv.org/cgit.cgi/hverkuil/media_tree.git/log/?h=vb2-part4
Regards,
Hans
--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Quoted 44 lines · Hans Verkuil
Quoted 37 lines · Jan Kara
Hello,
Quoted 13 lines · Marek Szyprowski
Quoted 12 lines · Jan Kara
The following patch series is my first stab at abstracting vma handling
from the various media drivers. After this patch set drivers have to know much less details about vmas, their types, and locking. My motivation for the series is that I want to changeget_user_pages()locking and I want to handle subtle locking details in as few places as possible.The core of the series is the new helper
get_vaddr_pfns()which is given a virtual address and it fills in PFNs into provided array. If PFNs correspond to normal pages it also grabs references to these pages. The difference fromget_user_pages()is that this function can also deal with pfnmap, mixed, and io mappings which is what the media drivers need.Quoted 7 lines
Quoted 3 lines
The patches are just compile tested (since I don't have any of the hardware I'm afraid I won't be able to do any more testing anyway) so please handle with care. I'm grateful for any comments.
Thanks for posting this series! I will check if it works with our hardware soon. This is something I wanted to introduce some time ago to simplify buffer handling in dma-buf, but I had no time to start working.
Thanks for having a look in the series.
Quoted 7 lines
However I would like to go even further with integration of your pfn vector idea. This structure looks like a best solution for a compact representation of the memory buffer, which should be considered by the hardware as contiguous (either contiguous in physical memory or mapped contiguously into dma address space by the respective iommu). As you already noticed it is widely used by graphics and video drivers.
Quoted 5 lines
I would also like to add support for pfn vector directly to the dma-mapping subsystem. This can be done quite easily (even with a fallback for architectures which don't provide method for it). I will try to prepare rfc soon. This will finally remove the need for hacks in
media/v4l2-core/videobuf2-dma-contig.cQuoted 5 lines
That would be a worthwhile thing to do. When I was reading the code this
seemed like something which could be done but I delibrately avoided doing more unification than necessary for my purposes as I don't have any hardware to test and don't know all the subtleties in the code... BTW, is there some way to test the drivers without the physical video HW?
Quoted 5 lines
You can use the vivi driver (
drivers/media/platform/vivi) for this. However, while the vivi driver can import dma buffers it cannot export them. If you want that, then you have to use this tree:http://git.linuxtv.org/cgit.cgi/hverkuil/media_tree.git/log/?h=vb2-part4
Thanks for the pointer that looks good. I've also founddrivers/media/platform/mem2mem_testdev.c which seems to do even more testing of the area I made changes to. So now I have to find some userspace tool which can issue proper ioctls to setup and use the buffers and I can start testing what I wrote :)
Honza
Signature · 2 lines
Jan Kara <[email protected]> SUSE Labs, CR
Quoted 49 lines · Jan Kara
Quoted 48 lines · Hans Verkuil
Quoted 37 lines · Jan Kara
Hello,
Quoted 13 lines · Marek Szyprowski
Quoted 12 lines · Jan Kara
The following patch series is my first stab at abstracting vma handling
from the various media drivers. After this patch set drivers have to know much less details about vmas, their types, and locking. My motivation for the series is that I want to changeget_user_pages()locking and I want to handle subtle locking details in as few places as possible.The core of the series is the new helper
get_vaddr_pfns()which is given a virtual address and it fills in PFNs into provided array. If PFNs correspond to normal pages it also grabs references to these pages. The difference fromget_user_pages()is that this function can also deal with pfnmap, mixed, and io mappings which is what the media drivers need.Quoted 7 lines
Quoted 3 lines
The patches are just compile tested (since I don't have any of the hardware I'm afraid I won't be able to do any more testing anyway) so please handle with care. I'm grateful for any comments.
Thanks for posting this series! I will check if it works with our hardware soon. This is something I wanted to introduce some time ago to simplify buffer handling in dma-buf, but I had no time to start working.
Thanks for having a look in the series.
Quoted 7 lines
However I would like to go even further with integration of your pfn vector idea. This structure looks like a best solution for a compact representation of the memory buffer, which should be considered by the hardware as contiguous (either contiguous in physical memory or mapped contiguously into dma address space by the respective iommu). As you already noticed it is widely used by graphics and video drivers.
Quoted 5 lines
I would also like to add support for pfn vector directly to the dma-mapping subsystem. This can be done quite easily (even with a fallback for architectures which don't provide method for it). I will try to prepare rfc soon. This will finally remove the need for hacks in
media/v4l2-core/videobuf2-dma-contig.cQuoted 5 lines
That would be a worthwhile thing to do. When I was reading the code this
seemed like something which could be done but I delibrately avoided doing more unification than necessary for my purposes as I don't have any hardware to test and don't know all the subtleties in the code... BTW, is there some way to test the drivers without the physical video HW?You can use the vivi driver (
drivers/media/platform/vivi) for this. However, while the vivi driver can import dma buffers it cannot export them. If you want that, then you have to use this tree:
Quoted 6 lines
Thanks for the pointer that looks good. I've also found
drivers/media/platform/mem2mem_testdev.cwhich seems to do even more testing of the area I made changes to. So now I have to find some userspace tool which can issue proper ioctls to setup and use the buffers and I can start testing what I wrote :)
Get the v4l-utils.git repository (http://git.linuxtv.org/cgit.cgi/v4l-utils.git/).
You want the v4l2-ctl tool. Don't use the version supplied by your distro, that's often too old.
'v4l2-ctl --help-streaming' gives the available options for doing streaming.
So simple capturing from vivi is 'v4l2-ctl --stream-mmap' or '--stream-user'. You can't test dmabuf unless you switch to the vb2-part4 branch of my tree.
If you need help with testing it's easiest to contact me on the #v4l irc channel.
Regards,
Hans
--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Quoted 57 lines · Hans Verkuil
Quoted 49 lines · Jan Kara
Quoted 48 lines · Hans Verkuil
Quoted 37 lines · Jan Kara
Hello,
Quoted 13 lines · Marek Szyprowski
Quoted 12 lines · Jan Kara
The following patch series is my first stab at abstracting vma handling
from the various media drivers. After this patch set drivers have to know much less details about vmas, their types, and locking. My motivation for the series is that I want to changeget_user_pages()locking and I want to handle subtle locking details in as few places as possible.The core of the series is the new helper
get_vaddr_pfns()which is given a virtual address and it fills in PFNs into provided array. If PFNs correspond to normal pages it also grabs references to these pages. The difference fromget_user_pages()is that this function can also deal with pfnmap, mixed, and io mappings which is what the media drivers need.Quoted 7 lines
Quoted 3 lines
The patches are just compile tested (since I don't have any of the hardware I'm afraid I won't be able to do any more testing anyway) so please handle with care. I'm grateful for any comments.
Thanks for posting this series! I will check if it works with our hardware soon. This is something I wanted to introduce some time ago to simplify buffer handling in dma-buf, but I had no time to start working.
Thanks for having a look in the series.
Quoted 7 lines
However I would like to go even further with integration of your pfn vector idea. This structure looks like a best solution for a compact representation of the memory buffer, which should be considered by the hardware as contiguous (either contiguous in physical memory or mapped contiguously into dma address space by the respective iommu). As you already noticed it is widely used by graphics and video drivers.
Quoted 5 lines
I would also like to add support for pfn vector directly to the dma-mapping subsystem. This can be done quite easily (even with a fallback for architectures which don't provide method for it). I will try to prepare rfc soon. This will finally remove the need for hacks in
media/v4l2-core/videobuf2-dma-contig.cQuoted 5 lines
That would be a worthwhile thing to do. When I was reading the code this
seemed like something which could be done but I delibrately avoided doing more unification than necessary for my purposes as I don't have any hardware to test and don't know all the subtleties in the code... BTW, is there some way to test the drivers without the physical video HW?You can use the vivi driver (
drivers/media/platform/vivi) for this. However, while the vivi driver can import dma buffers it cannot export them. If you want that, then you have to use this tree:Quoted 6 lines
Thanks for the pointer that looks good. I've also found
drivers/media/platform/mem2mem_testdev.cwhich seems to do even more testing of the area I made changes to. So now I have to find some userspace tool which can issue proper ioctls to setup and use the buffers and I can start testing what I wrote :)
Quoted 8 lines
Get the v4l-utils.git repository (http://git.linuxtv.org/cgit.cgi/v4l-utils.git/).
You want the v4l2-ctl tool. Don't use the version supplied by your distro, that's often too old.'v4l2-ctl --help-streaming' gives the available options for doing streaming.
So simple capturing from vivi is 'v4l2-ctl --stream-mmap' or '--stream-user'. You can't test dmabuf unless you switch to the vb2-part4 branch of my tree.
Great, it seems to be doing something and it shows there's some bug in my
code. Thanks a lot for help.
Honza
Signature · 2 lines
Jan Kara <[email protected]> SUSE Labs, CR
Quoted 62 lines · Jan Kara
Quoted 61 lines · Hans Verkuil
Quoted 49 lines · Jan Kara
Quoted 48 lines · Hans Verkuil
Quoted 37 lines · Jan Kara
Hello,
Quoted 13 lines · Marek Szyprowski
Quoted 12 lines · Jan Kara
The following patch series is my first stab at abstracting vma handling
from the various media drivers. After this patch set drivers have to know much less details about vmas, their types, and locking. My motivation for the series is that I want to changeget_user_pages()locking and I want to handle subtle locking details in as few places as possible.The core of the series is the new helper
get_vaddr_pfns()which is given a virtual address and it fills in PFNs into provided array. If PFNs correspond to normal pages it also grabs references to these pages. The difference fromget_user_pages()is that this function can also deal with pfnmap, mixed, and io mappings which is what the media drivers need.Quoted 7 lines
Quoted 3 lines
The patches are just compile tested (since I don't have any of the hardware I'm afraid I won't be able to do any more testing anyway) so please handle with care. I'm grateful for any comments.
Thanks for posting this series! I will check if it works with our hardware soon. This is something I wanted to introduce some time ago to simplify buffer handling in dma-buf, but I had no time to start working.
Thanks for having a look in the series.
Quoted 7 lines
However I would like to go even further with integration of your pfn vector idea. This structure looks like a best solution for a compact representation of the memory buffer, which should be considered by the hardware as contiguous (either contiguous in physical memory or mapped contiguously into dma address space by the respective iommu). As you already noticed it is widely used by graphics and video drivers.
Quoted 5 lines
I would also like to add support for pfn vector directly to the dma-mapping subsystem. This can be done quite easily (even with a fallback for architectures which don't provide method for it). I will try to prepare rfc soon. This will finally remove the need for hacks in
media/v4l2-core/videobuf2-dma-contig.cQuoted 5 lines
That would be a worthwhile thing to do. When I was reading the code this
seemed like something which could be done but I delibrately avoided doing more unification than necessary for my purposes as I don't have any hardware to test and don't know all the subtleties in the code... BTW, is there some way to test the drivers without the physical video HW?You can use the vivi driver (
drivers/media/platform/vivi) for this. However, while the vivi driver can import dma buffers it cannot export them. If you want that, then you have to use this tree:Quoted 6 lines
Thanks for the pointer that looks good. I've also found
drivers/media/platform/mem2mem_testdev.cwhich seems to do even more testing of the area I made changes to. So now I have to find some userspace tool which can issue proper ioctls to setup and use the buffers and I can start testing what I wrote :)Get the v4l-utils.git repository (http://git.linuxtv.org/cgit.cgi/v4l-utils.git/).
You want the v4l2-ctl tool. Don't use the version supplied by your distro, that's often too old.
Quoted 6 lines
Quoted 4 lines
'v4l2-ctl --help-streaming' gives the available options for doing streaming.
So simple capturing from vivi is 'v4l2-ctl --stream-mmap' or '--stream-user'. You can't test dmabuf unless you switch to the vb2-part4 branch of my tree.
Great, it seems to be doing something and it shows there's some bug in my
code. Thanks a lot for help.
OK, so after a small fix the basic functionality seems to be working. It
doesn't seem there's a way to test multiplanar buffers with vivi, is there?
Honza
Signature · 2 lines
Jan Kara <[email protected]> SUSE Labs, CR
Quoted 63 lines · Jan Kara
Quoted 62 lines · Jan Kara
Quoted 61 lines · Hans Verkuil
Quoted 49 lines · Jan Kara
Quoted 48 lines · Hans Verkuil
Quoted 37 lines · Jan Kara
Hello,
Quoted 13 lines · Marek Szyprowski
Quoted 12 lines · Jan Kara
The following patch series is my first stab at abstracting vma handling
from the various media drivers. After this patch set drivers have to know much less details about vmas, their types, and locking. My motivation for the series is that I want to changeget_user_pages()locking and I want to handle subtle locking details in as few places as possible.The core of the series is the new helper
get_vaddr_pfns()which is given a virtual address and it fills in PFNs into provided array. If PFNs correspond to normal pages it also grabs references to these pages. The difference fromget_user_pages()is that this function can also deal with pfnmap, mixed, and io mappings which is what the media drivers need.Quoted 7 lines
Quoted 3 lines
The patches are just compile tested (since I don't have any of the hardware I'm afraid I won't be able to do any more testing anyway) so please handle with care. I'm grateful for any comments.
Thanks for posting this series! I will check if it works with our hardware soon. This is something I wanted to introduce some time ago to simplify buffer handling in dma-buf, but I had no time to start working.
Thanks for having a look in the series.
Quoted 7 lines
However I would like to go even further with integration of your pfn vector idea. This structure looks like a best solution for a compact representation of the memory buffer, which should be considered by the hardware as contiguous (either contiguous in physical memory or mapped contiguously into dma address space by the respective iommu). As you already noticed it is widely used by graphics and video drivers.
Quoted 5 lines
I would also like to add support for pfn vector directly to the dma-mapping subsystem. This can be done quite easily (even with a fallback for architectures which don't provide method for it). I will try to prepare rfc soon. This will finally remove the need for hacks in
media/v4l2-core/videobuf2-dma-contig.cQuoted 5 lines
That would be a worthwhile thing to do. When I was reading the code this
seemed like something which could be done but I delibrately avoided doing more unification than necessary for my purposes as I don't have any hardware to test and don't know all the subtleties in the code... BTW, is there some way to test the drivers without the physical video HW?You can use the vivi driver (
drivers/media/platform/vivi) for this. However, while the vivi driver can import dma buffers it cannot export them. If you want that, then you have to use this tree:Quoted 6 lines
Thanks for the pointer that looks good. I've also found
drivers/media/platform/mem2mem_testdev.cwhich seems to do even more testing of the area I made changes to. So now I have to find some userspace tool which can issue proper ioctls to setup and use the buffers and I can start testing what I wrote :)Get the v4l-utils.git repository (http://git.linuxtv.org/cgit.cgi/v4l-utils.git/).
You want the v4l2-ctl tool. Don't use the version supplied by your distro, that's often too old.
Quoted 8 lines
Quoted 6 lines
Quoted 4 lines
'v4l2-ctl --help-streaming' gives the available options for doing streaming.
So simple capturing from vivi is 'v4l2-ctl --stream-mmap' or '--stream-user'. You can't test dmabuf unless you switch to the vb2-part4 branch of my tree.
Great, it seems to be doing something and it shows there's some bug in my
code. Thanks a lot for help.OK, so after a small fix the basic functionality seems to be working. It
doesn't seem there's a way to test multiplanar buffers with vivi, is there?
For that you need to switch to the vb2-part4 branch as well. That has support for multiplanar.
Regards,
Hans
--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Quoted 68 lines · Hans Verkuil
Quoted 67 lines · Jan Kara
Quoted 66 lines · Jan Kara
Quoted 65 lines · Hans Verkuil
Quoted 49 lines · Jan Kara
Quoted 48 lines · Hans Verkuil
Quoted 37 lines · Jan Kara
Hello,
Quoted 13 lines · Marek Szyprowski
Quoted 12 lines · Jan Kara
The following patch series is my first stab at abstracting vma handling
from the various media drivers. After this patch set drivers have to know much less details about vmas, their types, and locking. My motivation for the series is that I want to changeget_user_pages()locking and I want to handle subtle locking details in as few places as possible.The core of the series is the new helper
get_vaddr_pfns()which is given a virtual address and it fills in PFNs into provided array. If PFNs correspond to normal pages it also grabs references to these pages. The difference fromget_user_pages()is that this function can also deal with pfnmap, mixed, and io mappings which is what the media drivers need.Quoted 7 lines
Quoted 3 lines
The patches are just compile tested (since I don't have any of the hardware I'm afraid I won't be able to do any more testing anyway) so please handle with care. I'm grateful for any comments.
Thanks for posting this series! I will check if it works with our hardware soon. This is something I wanted to introduce some time ago to simplify buffer handling in dma-buf, but I had no time to start working.
Thanks for having a look in the series.
Quoted 7 lines
However I would like to go even further with integration of your pfn vector idea. This structure looks like a best solution for a compact representation of the memory buffer, which should be considered by the hardware as contiguous (either contiguous in physical memory or mapped contiguously into dma address space by the respective iommu). As you already noticed it is widely used by graphics and video drivers.
Quoted 5 lines
I would also like to add support for pfn vector directly to the dma-mapping subsystem. This can be done quite easily (even with a fallback for architectures which don't provide method for it). I will try to prepare rfc soon. This will finally remove the need for hacks in
media/v4l2-core/videobuf2-dma-contig.cQuoted 5 lines
That would be a worthwhile thing to do. When I was reading the code this
seemed like something which could be done but I delibrately avoided doing more unification than necessary for my purposes as I don't have any hardware to test and don't know all the subtleties in the code... BTW, is there some way to test the drivers without the physical video HW?You can use the vivi driver (
drivers/media/platform/vivi) for this. However, while the vivi driver can import dma buffers it cannot export them. If you want that, then you have to use this tree:Quoted 6 lines
Thanks for the pointer that looks good. I've also found
drivers/media/platform/mem2mem_testdev.cwhich seems to do even more testing of the area I made changes to. So now I have to find some userspace tool which can issue proper ioctls to setup and use the buffers and I can start testing what I wrote :)Get the v4l-utils.git repository (http://git.linuxtv.org/cgit.cgi/v4l-utils.git/).
You want the v4l2-ctl tool. Don't use the version supplied by your distro, that's often too old.'v4l2-ctl --help-streaming' gives the available options for doing streaming.
So simple capturing from vivi is 'v4l2-ctl --stream-mmap' or '--stream-user'. You can't test dmabuf unless you switch to the vb2-part4 branch of my tree.
Quoted 7 lines
Quoted 4 lines
Quoted 2 lines
Great, it seems to be doing something and it shows there's some bug in my
code. Thanks a lot for help.OK, so after a small fix the basic functionality seems to be working. It
doesn't seem there's a way to test multiplanar buffers with vivi, is there?For that you need to switch to the vb2-part4 branch as well. That has support for multiplanar.
OK, I've merged that branch to my kernel but I failed to find the setting
for vivi that would create multiplanar buffers and in fact I don't see multiplanar capabilities among the capabilities reported by the v4l2-ctl tool. Can you help me please?
Honza
Signature · 2 lines
Jan Kara <[email protected]> SUSE Labs, CR