diff mbox

[PATCHv5,08/13] v4l: vb2-dma-contig: add support for scatterlist in userptr mode

Message ID 1334933134-4688-9-git-send-email-t.stanislaws@samsung.com
State Superseded
Headers show

Commit Message

Tomasz Stanislawski April 20, 2012, 2:45 p.m. UTC
From: Andrzej Pietrasiewicz <andrzej.p@samsung.com>

This patch introduces usage of dma_map_sg to map memory behind
a userspace pointer to a device as dma-contiguous mapping.

Signed-off-by: Andrzej Pietrasiewicz <andrzej.p@samsung.com>
Signed-off-by: Marek Szyprowski <m.szyprowski@samsung.com>
	[bugfixing]
Signed-off-by: Kamil Debski <k.debski@samsung.com>
	[bugfixing]
Signed-off-by: Tomasz Stanislawski <t.stanislaws@samsung.com>
	[add sglist subroutines/code refactoring]
Signed-off-by: Kyungmin Park <kyungmin.park@samsung.com>
---
 drivers/media/video/videobuf2-dma-contig.c |  279 ++++++++++++++++++++++++++--
 1 files changed, 262 insertions(+), 17 deletions(-)

Comments

Laurent Pinchart April 21, 2012, 5:24 p.m. UTC | #1
Hi Tomasz,

Thanks for the patch.

On Friday 20 April 2012 16:45:29 Tomasz Stanislawski wrote:
> From: Andrzej Pietrasiewicz <andrzej.p@samsung.com>
> 
> This patch introduces usage of dma_map_sg to map memory behind
> a userspace pointer to a device as dma-contiguous mapping.
> 
> Signed-off-by: Andrzej Pietrasiewicz <andrzej.p@samsung.com>
> Signed-off-by: Marek Szyprowski <m.szyprowski@samsung.com>
> 	[bugfixing]
> Signed-off-by: Kamil Debski <k.debski@samsung.com>
> 	[bugfixing]
> Signed-off-by: Tomasz Stanislawski <t.stanislaws@samsung.com>
> 	[add sglist subroutines/code refactoring]
> Signed-off-by: Kyungmin Park <kyungmin.park@samsung.com>

Acked-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
Subash Patel May 7, 2012, 2:38 p.m. UTC | #2
Hello Thomasz, Laurent,

I found an issue in the function vb2_dc_pages_to_sgt() below. I saw that 
during the attach, the size of the SGT and size requested mis-matched 
(by atleast 8k bytes). Hence I made a small correction to the code as 
below. I could then attach the importer properly.

Regards,
Subash

On 04/20/2012 08:15 PM, Tomasz Stanislawski wrote:
> From: Andrzej Pietrasiewicz<andrzej.p@samsung.com>
>
> This patch introduces usage of dma_map_sg to map memory behind
> a userspace pointer to a device as dma-contiguous mapping.
>
> Signed-off-by: Andrzej Pietrasiewicz<andrzej.p@samsung.com>
> Signed-off-by: Marek Szyprowski<m.szyprowski@samsung.com>
> 	[bugfixing]
> Signed-off-by: Kamil Debski<k.debski@samsung.com>
> 	[bugfixing]
> Signed-off-by: Tomasz Stanislawski<t.stanislaws@samsung.com>
> 	[add sglist subroutines/code refactoring]
> Signed-off-by: Kyungmin Park<kyungmin.park@samsung.com>
> ---
>   drivers/media/video/videobuf2-dma-contig.c |  279 ++++++++++++++++++++++++++--
>   1 files changed, 262 insertions(+), 17 deletions(-)
>
> diff --git a/drivers/media/video/videobuf2-dma-contig.c b/drivers/media/video/videobuf2-dma-contig.c
> index 476e536..9cbc8d4 100644
> --- a/drivers/media/video/videobuf2-dma-contig.c
> +++ b/drivers/media/video/videobuf2-dma-contig.c
> @@ -11,6 +11,8 @@
>    */
>
>   #include<linux/module.h>
> +#include<linux/scatterlist.h>
> +#include<linux/sched.h>
>   #include<linux/slab.h>
>   #include<linux/dma-mapping.h>
>
> @@ -22,6 +24,8 @@ struct vb2_dc_buf {
>   	void				*vaddr;
>   	unsigned long			size;
>   	dma_addr_t			dma_addr;
> +	enum dma_data_direction		dma_dir;
> +	struct sg_table			*dma_sgt;
>
>   	/* MMAP related */
>   	struct vb2_vmarea_handler	handler;
> @@ -32,6 +36,95 @@ struct vb2_dc_buf {
>   };
>
>   /*********************************************/
> +/*        scatterlist table functions        */
> +/*********************************************/
> +
> +static struct sg_table *vb2_dc_pages_to_sgt(struct page **pages,
> +	unsigned int n_pages, unsigned long offset, unsigned long size)
> +{
> +	struct sg_table *sgt;
> +	unsigned int chunks;
> +	unsigned int i;
> +	unsigned int cur_page;
> +	int ret;
> +	struct scatterlist *s;
> +
> +	sgt = kzalloc(sizeof *sgt, GFP_KERNEL);
> +	if (!sgt)
> +		return ERR_PTR(-ENOMEM);
> +
> +	/* compute number of chunks */
> +	chunks = 1;
> +	for (i = 1; i<  n_pages; ++i)
> +		if (pages[i] != pages[i - 1] + 1)
> +			++chunks;
> +
> +	ret = sg_alloc_table(sgt, chunks, GFP_KERNEL);
> +	if (ret) {
> +		kfree(sgt);
> +		return ERR_PTR(-ENOMEM);
> +	}
> +
> +	/* merging chunks and putting them into the scatterlist */
> +	cur_page = 0;
> +	for_each_sg(sgt->sgl, s, sgt->orig_nents, i) {
> +		unsigned long chunk_size;
> +		unsigned int j;
		size = PAGE_SIZE;

> +
> +		for (j = cur_page + 1; j<  n_pages; ++j)
		for (j = cur_page + 1; j < n_pages; ++j) {
> +			if (pages[j] != pages[j - 1] + 1)
> +				break;
			size += PAGE
		}
> +
> +		chunk_size = ((j - cur_page)<<  PAGE_SHIFT) - offset;
> +		sg_set_page(s, pages[cur_page], min(size, chunk_size), offset);
		[DELETE] size -= chunk_size;
> +		offset = 0;
> +		cur_page = j;
> +	}
> +
> +	return sgt;
> +}
> +
> +static void vb2_dc_release_sgtable(struct sg_table *sgt)
> +{
> +	sg_free_table(sgt);
> +	kfree(sgt);
> +}
> +
> +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->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;
> +	dma_addr_t expected = sg_dma_address(sgt->sgl);
> +	unsigned int i;
> +	unsigned long size = 0;
> +
> +	for_each_sg(sgt->sgl, s, sgt->nents, i) {
> +		if (sg_dma_address(s) != expected)
> +			break;
> +		expected = sg_dma_address(s) + sg_dma_len(s);
> +		size += sg_dma_len(s);
> +	}
> +	return size;
> +}
> +
> +/*********************************************/
>   /*         callbacks for all buffers         */
>   /*********************************************/
>
> @@ -116,42 +209,194 @@ static int vb2_dc_mmap(void *buf_priv, struct vm_area_struct *vma)
>   /*       callbacks for USERPTR buffers       */
>   /*********************************************/
>
> +static inline int vma_is_io(struct vm_area_struct *vma)
> +{
> +	return !!(vma->vm_flags&  (VM_IO | VM_PFNMAP));
> +}
> +
> +static struct vm_area_struct *vb2_dc_get_user_vma(
> +	unsigned long start, unsigned long size)
> +{
> +	struct vm_area_struct *vma;
> +
> +	/* current->mm->mmap_sem is taken by videobuf2 core */
> +	vma = find_vma(current->mm, start);
> +	if (!vma) {
> +		printk(KERN_ERR "no vma for address %lu\n", start);
> +		return ERR_PTR(-EFAULT);
> +	}
> +
> +	if (vma->vm_end - vma->vm_start<  size) {
> +		printk(KERN_ERR "vma at %lu is too small for %lu bytes\n",
> +			start, size);
> +		return ERR_PTR(-EFAULT);
> +	}
> +
> +	vma = vb2_get_vma(vma);
> +	if (!vma) {
> +		printk(KERN_ERR "failed to copy vma\n");
> +		return ERR_PTR(-ENOMEM);
> +	}
> +
> +	return vma;
> +}
> +
> +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 (ret) {
> +				printk(KERN_ERR "no page for address %lu\n",
> +					start);
> +				return ret;
> +			}
> +			pages[i] = pfn_to_page(pfn);
> +		}
> +	} else {
> +		unsigned int n;
> +
> +		n = get_user_pages(current, current->mm, start&  PAGE_MASK,
> +			n_pages, write, 1, pages, NULL);
> +		if (n != n_pages) {
> +			printk(KERN_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;
> +
> +	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);
> +
> +	vb2_dc_release_sgtable(sgt);
> +	vb2_put_vma(buf->vma);
> +	kfree(buf);
> +}
> +
>   static void *vb2_dc_get_userptr(void *alloc_ctx, unsigned long vaddr,
> -					unsigned long size, int write)
> +	unsigned long size, int write)
>   {
>   	struct vb2_dc_buf *buf;
> -	struct vm_area_struct *vma;
> -	dma_addr_t dma_addr = 0;
> -	int ret;
> +	unsigned long start;
> +	unsigned long end;
> +	unsigned long offset;
> +	struct page **pages;
> +	int n_pages;
> +	int ret = 0;
> +	struct sg_table *sgt;
> +	unsigned long contig_size;
>
>   	buf = kzalloc(sizeof *buf, GFP_KERNEL);
>   	if (!buf)
>   		return ERR_PTR(-ENOMEM);
>
> -	ret = vb2_get_contig_userptr(vaddr, size,&vma,&dma_addr);
> +	buf->dev = alloc_ctx;
> +	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;
> +		printk(KERN_ERR "failed to allocate pages table\n");
> +		goto fail_buf;
> +	}
> +
> +	buf->vma = vb2_dc_get_user_vma(start, size);
> +	if (IS_ERR(buf->vma)) {
> +		printk(KERN_ERR "failed to get VMA\n");
> +		ret = PTR_ERR(buf->vma);
> +		goto fail_pages;
> +	}
> +
> +	/* extract page list from userspace mapping */
> +	ret = vb2_dc_get_user_pages(start, pages, n_pages, buf->vma, write);
>   	if (ret) {
> -		printk(KERN_ERR "Failed acquiring VMA for vaddr 0x%08lx\n",
> -				vaddr);
> -		kfree(buf);
> -		return ERR_PTR(ret);
> +		printk(KERN_ERR "failed to get user pages\n");
> +		goto fail_vma;
> +	}
> +
> +	sgt = vb2_dc_pages_to_sgt(pages, n_pages, offset, size);
> +	if (IS_ERR(sgt)) {
> +		printk(KERN_ERR "failed to create scatterlist table\n");
> +		ret = -ENOMEM;
> +		goto fail_get_user_pages;
> +	}
> +
> +	/* 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) {
> +		printk(KERN_ERR "failed to map scatterlist\n");
> +		ret = -EIO;
> +		goto fail_sgt;
>   	}
>
> +	contig_size = vb2_dc_get_contiguous_size(sgt);
> +	if (contig_size<  size) {
> +		printk(KERN_ERR "contiguous mapping is too small %lu/%lu\n",
> +			contig_size, size);
> +		ret = -EFAULT;
> +		goto fail_map_sg;
> +	}
> +
> +	buf->dma_addr = sg_dma_address(sgt->sgl);
>   	buf->size = size;
> -	buf->dma_addr = dma_addr;
> -	buf->vma = vma;
> +	buf->dma_sgt = sgt;
>
>   	return buf;
> -}
>
> -static void vb2_dc_put_userptr(void *mem_priv)
> -{
> -	struct vb2_dc_buf *buf = mem_priv;
> +fail_map_sg:
> +	dma_unmap_sg(buf->dev, sgt->sgl, sgt->nents, buf->dma_dir);
>
> -	if (!buf)
> -		return;
> +fail_sgt:
> +	if (!vma_is_io(buf->vma))
> +		vb2_dc_sgt_foreach_page(sgt, put_page);
> +	vb2_dc_release_sgtable(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);
> +
> +	return ERR_PTR(ret);
>   }
>
>   /*********************************************/
Tomasz Stanislawski May 7, 2012, 3:11 p.m. UTC | #3
Hi Subash,
Could you provide a detailed description of a test case
that causes a failure of vb2_dc_pages_to_sgt?

Regards,
Tomasz Stanislawski
Subash Patel May 8, 2012, 5:41 a.m. UTC | #4
Hi Thomasz,

I have extended the MFC-FIMC testcase posted by Kamil sometime ago to 
sanity test the UMM patches. This test is multi-threaded(further 
explanation for developers who may not have seen it yet), where thread 
one parses the encoded stream and feeds into the codec  IP driver(aka 
MFC driver). Second thread will dequeue the buffer from MFC driver 
(DMA_BUF export) and queues it into a CSC IP(aka FIMC) driver(DMA_BUF 
import). Third thread dequeues the frame from FIMC driver and either 
pushes it into LCD driver for display or dumps to a flat file for analysis.

MFC driver exports the fd's and FIMC driver imports and attaches it. 
During FIMC QBUF (thats when the attach and map happens), it is observed 
that in the function vb2_dc_map_dmabuf() call to 
vb2_dc_get_contiguous_size() fails. This is because contig_size < 
buf->size. contig_size is calculated from the SGT which we would have 
constructed in the function vb2_dc_pages_to_sgt().

Let me know if you need more details.

Regards,
Subash

On 05/07/2012 08:41 PM, Tomasz Stanislawski wrote:
> Hi Subash,
> Could you provide a detailed description of a test case
> that causes a failure of vb2_dc_pages_to_sgt?
>
> Regards,
> Tomasz Stanislawski
Laurent Pinchart May 8, 2012, 9:14 a.m. UTC | #5
Hi Subash,

On Monday 07 May 2012 20:08:25 Subash Patel wrote:
> Hello Thomasz, Laurent,
> 
> I found an issue in the function vb2_dc_pages_to_sgt() below. I saw that
> during the attach, the size of the SGT and size requested mis-matched
> (by atleast 8k bytes). Hence I made a small correction to the code as
> below. I could then attach the importer properly.

Thank you for the report.

Could you print the content of the sglist (number of chunks and size of each 
chunk) before and after your modifications, as well as the values of n_pages, 
offset and size ?

> On 04/20/2012 08:15 PM, Tomasz Stanislawski wrote:

[snip]

> > +static struct sg_table *vb2_dc_pages_to_sgt(struct page **pages,
> > +	unsigned int n_pages, unsigned long offset, unsigned long size)
> > +{
> > +	struct sg_table *sgt;
> > +	unsigned int chunks;
> > +	unsigned int i;
> > +	unsigned int cur_page;
> > +	int ret;
> > +	struct scatterlist *s;
> > +
> > +	sgt = kzalloc(sizeof *sgt, GFP_KERNEL);
> > +	if (!sgt)
> > +		return ERR_PTR(-ENOMEM);
> > +
> > +	/* compute number of chunks */
> > +	chunks = 1;
> > +	for (i = 1; i<  n_pages; ++i)
> > +		if (pages[i] != pages[i - 1] + 1)
> > +			++chunks;
> > +
> > +	ret = sg_alloc_table(sgt, chunks, GFP_KERNEL);
> > +	if (ret) {
> > +		kfree(sgt);
> > +		return ERR_PTR(-ENOMEM);
> > +	}
> > +
> > +	/* merging chunks and putting them into the scatterlist */
> > +	cur_page = 0;
> > +	for_each_sg(sgt->sgl, s, sgt->orig_nents, i) {
> > +		unsigned long chunk_size;
> > +		unsigned int j;
> 
> 		size = PAGE_SIZE;
> 
> > +
> > +		for (j = cur_page + 1; j<  n_pages; ++j)
> 
> 		for (j = cur_page + 1; j < n_pages; ++j) {
> 
> > +			if (pages[j] != pages[j - 1] + 1)
> > +				break;
> 
> 			size += PAGE
> 		}
> 
> > +
> > +		chunk_size = ((j - cur_page)<<  PAGE_SHIFT) - offset;
> > +		sg_set_page(s, pages[cur_page], min(size, chunk_size), offset);
> 
> 		[DELETE] size -= chunk_size;
> 
> > +		offset = 0;
> > +		cur_page = j;
> > +	}
> > +
> > +	return sgt;
> > +}
Subash Patel May 8, 2012, 11:15 a.m. UTC | #6
Hi Laurent,

On 05/08/2012 02:44 PM, Laurent Pinchart wrote:
> Hi Subash,
>
> On Monday 07 May 2012 20:08:25 Subash Patel wrote:
>> Hello Thomasz, Laurent,
>>
>> I found an issue in the function vb2_dc_pages_to_sgt() below. I saw that
>> during the attach, the size of the SGT and size requested mis-matched
>> (by atleast 8k bytes). Hence I made a small correction to the code as
>> below. I could then attach the importer properly.
>
> Thank you for the report.
>
> Could you print the content of the sglist (number of chunks and size of each
> chunk) before and after your modifications, as well as the values of n_pages,
> offset and size ?
I will put back all the printk's and generate this. As of now, my setup 
has changed and will do this when I get sometime.
>
>> On 04/20/2012 08:15 PM, Tomasz Stanislawski wrote:
>
> [snip]
>
>>> +static struct sg_table *vb2_dc_pages_to_sgt(struct page **pages,
>>> +	unsigned int n_pages, unsigned long offset, unsigned long size)
>>> +{
>>> +	struct sg_table *sgt;
>>> +	unsigned int chunks;
>>> +	unsigned int i;
>>> +	unsigned int cur_page;
>>> +	int ret;
>>> +	struct scatterlist *s;
>>> +
>>> +	sgt = kzalloc(sizeof *sgt, GFP_KERNEL);
>>> +	if (!sgt)
>>> +		return ERR_PTR(-ENOMEM);
>>> +
>>> +	/* compute number of chunks */
>>> +	chunks = 1;
>>> +	for (i = 1; i<   n_pages; ++i)
>>> +		if (pages[i] != pages[i - 1] + 1)
>>> +			++chunks;
>>> +
>>> +	ret = sg_alloc_table(sgt, chunks, GFP_KERNEL);
>>> +	if (ret) {
>>> +		kfree(sgt);
>>> +		return ERR_PTR(-ENOMEM);
>>> +	}
>>> +
>>> +	/* merging chunks and putting them into the scatterlist */
>>> +	cur_page = 0;
>>> +	for_each_sg(sgt->sgl, s, sgt->orig_nents, i) {
>>> +		unsigned long chunk_size;
>>> +		unsigned int j;
>>
>> 		size = PAGE_SIZE;
>>
>>> +
>>> +		for (j = cur_page + 1; j<   n_pages; ++j)
>>
>> 		for (j = cur_page + 1; j<  n_pages; ++j) {
>>
>>> +			if (pages[j] != pages[j - 1] + 1)
>>> +				break;
>>
>> 			size += PAGE
>> 		}
>>
>>> +
>>> +		chunk_size = ((j - cur_page)<<   PAGE_SHIFT) - offset;
>>> +		sg_set_page(s, pages[cur_page], min(size, chunk_size), offset);
>>
>> 		[DELETE] size -= chunk_size;
>>
>>> +		offset = 0;
>>> +		cur_page = j;
>>> +	}
>>> +
>>> +	return sgt;
>>> +}
>
Regards,
Subash
Subash Patel May 9, 2012, 6:46 a.m. UTC | #7
Hello Tomasz, Laurent,

I have printed some logs during the dmabuf export and attach for the SGT 
issue below. Please find it in the attachment. I hope it will be useful.

Regards,
Subash

On 05/08/2012 04:45 PM, Subash Patel wrote:
> Hi Laurent,
>
> On 05/08/2012 02:44 PM, Laurent Pinchart wrote:
>> Hi Subash,
>>
>> On Monday 07 May 2012 20:08:25 Subash Patel wrote:
>>> Hello Thomasz, Laurent,
>>>
>>> I found an issue in the function vb2_dc_pages_to_sgt() below. I saw that
>>> during the attach, the size of the SGT and size requested mis-matched
>>> (by atleast 8k bytes). Hence I made a small correction to the code as
>>> below. I could then attach the importer properly.
>>
>> Thank you for the report.
>>
>> Could you print the content of the sglist (number of chunks and size
>> of each
>> chunk) before and after your modifications, as well as the values of
>> n_pages,
>> offset and size ?
> I will put back all the printk's and generate this. As of now, my setup
> has changed and will do this when I get sometime.
>>
>>> On 04/20/2012 08:15 PM, Tomasz Stanislawski wrote:
>>
>> [snip]
>>
>>>> +static struct sg_table *vb2_dc_pages_to_sgt(struct page **pages,
>>>> + unsigned int n_pages, unsigned long offset, unsigned long size)
>>>> +{
>>>> + struct sg_table *sgt;
>>>> + unsigned int chunks;
>>>> + unsigned int i;
>>>> + unsigned int cur_page;
>>>> + int ret;
>>>> + struct scatterlist *s;
>>>> +
>>>> + sgt = kzalloc(sizeof *sgt, GFP_KERNEL);
>>>> + if (!sgt)
>>>> + return ERR_PTR(-ENOMEM);
>>>> +
>>>> + /* compute number of chunks */
>>>> + chunks = 1;
>>>> + for (i = 1; i< n_pages; ++i)
>>>> + if (pages[i] != pages[i - 1] + 1)
>>>> + ++chunks;
>>>> +
>>>> + ret = sg_alloc_table(sgt, chunks, GFP_KERNEL);
>>>> + if (ret) {
>>>> + kfree(sgt);
>>>> + return ERR_PTR(-ENOMEM);
>>>> + }
>>>> +
>>>> + /* merging chunks and putting them into the scatterlist */
>>>> + cur_page = 0;
>>>> + for_each_sg(sgt->sgl, s, sgt->orig_nents, i) {
>>>> + unsigned long chunk_size;
>>>> + unsigned int j;
>>>
>>> size = PAGE_SIZE;
>>>
>>>> +
>>>> + for (j = cur_page + 1; j< n_pages; ++j)
>>>
>>> for (j = cur_page + 1; j< n_pages; ++j) {
>>>
>>>> + if (pages[j] != pages[j - 1] + 1)
>>>> + break;
>>>
>>> size += PAGE
>>> }
>>>
>>>> +
>>>> + chunk_size = ((j - cur_page)<< PAGE_SHIFT) - offset;
>>>> + sg_set_page(s, pages[cur_page], min(size, chunk_size), offset);
>>>
>>> [DELETE] size -= chunk_size;
>>>
>>>> + offset = 0;
>>>> + cur_page = j;
>>>> + }
>>>> +
>>>> + return sgt;
>>>> +}
>>
> Regards,
> Subash
[  178.545000] vb2_dc_pages_to_sgt() sgt->orig_nents=2
[  178.545000] vb2_dc_pages_to_sgt():83 cur_page=0
[  178.550000] vb2_dc_pages_to_sgt():84 offset=0
[  178.555000] vb2_dc_pages_to_sgt():86 chunk_size=131072
[  178.560000] vb2_dc_pages_to_sgt():89 size=4294836224
[  178.565000] vb2_dc_pages_to_sgt() sgt->orig_nents=2
[  178.570000] vb2_dc_pages_to_sgt():83 cur_page=32
[  178.575000] vb2_dc_pages_to_sgt():84 offset=0
[  178.580000] vb2_dc_pages_to_sgt():86 chunk_size=262144
[  178.585000] vb2_dc_pages_to_sgt():89 size=4294574080
[  178.590000] vb2_dc_pages_to_sgt() sgt->orig_nents=1
[  178.595000] vb2_dc_pages_to_sgt():83 cur_page=0
[  178.600000] vb2_dc_pages_to_sgt():84 offset=0
[  178.605000] vb2_dc_pages_to_sgt():86 chunk_size=8192
[  178.610000] vb2_dc_pages_to_sgt():89 size=4294959104
[  178.625000] vb2_dc_pages_to_sgt() sgt->orig_nents=1
[  178.625000] vb2_dc_pages_to_sgt():83 cur_page=0
[  178.630000] vb2_dc_pages_to_sgt():84 offset=0
[  178.635000] vb2_dc_pages_to_sgt():86 chunk_size=131072
[  178.640000] vb2_dc_pages_to_sgt():89 size=4294836224
[  178.645000] vb2_dc_pages_to_sgt() sgt->orig_nents=1
[  178.650000] vb2_dc_pages_to_sgt():83 cur_page=0
[  178.655000] vb2_dc_pages_to_sgt():84 offset=0
[  178.660000] vb2_dc_pages_to_sgt():86 chunk_size=131072
[  178.665000] vb2_dc_pages_to_sgt():89 size=4294836224
[  178.670000] vb2_dc_mmap: mapped dma addr 0x20060000 at 0xb6e01000, size 131072
[  178.670000] vb2_dc_mmap: mapped dma addr 0x20080000 at 0xb6de1000, size 131072
[  178.680000] vb2_dc_pages_to_sgt() sgt->orig_nents=2
[  178.685000] vb2_dc_pages_to_sgt():83 cur_page=0
[  178.690000] vb2_dc_pages_to_sgt():84 offset=0
[  178.695000] vb2_dc_pages_to_sgt():86 chunk_size=4096
[  178.700000] vb2_dc_pages_to_sgt():89 size=4294963200
[  178.705000] vb2_dc_pages_to_sgt() sgt->orig_nents=2
[  178.710000] vb2_dc_pages_to_sgt():83 cur_page=1
[  178.715000] vb2_dc_pages_to_sgt():84 offset=0
[  178.715000] vb2_dc_pages_to_sgt():86 chunk_size=8192
[  178.720000] vb2_dc_pages_to_sgt():89 size=4294955008
[  178.725000] vb2_dc_pages_to_sgt() sgt->orig_nents=1
[  178.730000] vb2_dc_pages_to_sgt():83 cur_page=0
[  178.735000] vb2_dc_pages_to_sgt():84 offset=0
[  178.740000] vb2_dc_pages_to_sgt():86 chunk_size=8192
[  178.745000] vb2_dc_pages_to_sgt():89 size=4294959104
[  178.750000] vb2_dc_pages_to_sgt() sgt->orig_nents=1
[  178.755000] vb2_dc_pages_to_sgt():83 cur_page=0
[  178.760000] vb2_dc_pages_to_sgt():84 offset=0
[  178.765000] vb2_dc_pages_to_sgt():86 chunk_size=131072
[  178.770000] vb2_dc_pages_to_sgt():89 size=4294836224
[  178.780000] vb2_dc_pages_to_sgt() sgt->orig_nents=2
[  178.780000] vb2_dc_pages_to_sgt():83 cur_page=0
[  178.785000] vb2_dc_pages_to_sgt():84 offset=0
[  178.790000] vb2_dc_pages_to_sgt():86 chunk_size=65536
[  178.795000] vb2_dc_pages_to_sgt():89 size=4294901760
[  178.800000] vb2_dc_pages_to_sgt() sgt->orig_nents=2
[  178.805000] vb2_dc_pages_to_sgt():83 cur_page=16
[  178.810000] vb2_dc_pages_to_sgt():84 offset=0
[  178.815000] vb2_dc_pages_to_sgt():86 chunk_size=393216
[  178.820000] vb2_dc_pages_to_sgt():89 size=4294508544
[  178.825000] vb2_dc_pages_to_sgt() sgt->orig_nents=3
[  178.830000] vb2_dc_pages_to_sgt():83 cur_page=0
[  178.835000] vb2_dc_pages_to_sgt():84 offset=0
[  178.840000] vb2_dc_pages_to_sgt():86 chunk_size=32768
[  178.845000] vb2_dc_pages_to_sgt():89 size=4294934528
[  178.850000] vb2_dc_pages_to_sgt() sgt->orig_nents=3
[  178.855000] vb2_dc_pages_to_sgt():83 cur_page=8
[  178.855000] vb2_dc_pages_to_sgt():84 offset=0
[  178.860000] vb2_dc_pages_to_sgt():86 chunk_size=65536
[  178.865000] vb2_dc_pages_to_sgt():89 size=4294868992
[  178.870000] vb2_dc_pages_to_sgt() sgt->orig_nents=3
[  178.875000] vb2_dc_pages_to_sgt():83 cur_page=24
[  178.880000] vb2_dc_pages_to_sgt():84 offset=0
[  178.885000] vb2_dc_pages_to_sgt():86 chunk_size=131072
[  178.890000] vb2_dc_pages_to_sgt():89 size=4294737920
[  178.895000] vb2_dc_pages_to_sgt() sgt->orig_nents=2
[  178.900000] vb2_dc_pages_to_sgt():83 cur_page=0
[  178.905000] vb2_dc_pages_to_sgt():84 offset=0
[  178.910000] vb2_dc_pages_to_sgt():86 chunk_size=65536
[  178.915000] vb2_dc_pages_to_sgt():89 size=4294901760
[  178.920000] vb2_dc_pages_to_sgt() sgt->orig_nents=2
[  178.925000] vb2_dc_pages_to_sgt():83 cur_page=16
[  178.930000] vb2_dc_pages_to_sgt():84 offset=0
[  178.935000] vb2_dc_pages_to_sgt():86 chunk_size=393216
[  178.940000] vb2_dc_pages_to_sgt():89 size=4294508544
[  178.945000] vb2_dc_pages_to_sgt() sgt->orig_nents=3
[  178.950000] vb2_dc_pages_to_sgt():83 cur_page=0
[  178.955000] vb2_dc_pages_to_sgt():84 offset=0
[  178.960000] vb2_dc_pages_to_sgt():86 chunk_size=32768
[  178.965000] vb2_dc_pages_to_sgt():89 size=4294934528
[  178.970000] vb2_dc_pages_to_sgt() sgt->orig_nents=3
[  178.975000] vb2_dc_pages_to_sgt():83 cur_page=8
[  178.975000] vb2_dc_pages_to_sgt():84 offset=0
[  178.980000] vb2_dc_pages_to_sgt():86 chunk_size=65536
[  178.985000] vb2_dc_pages_to_sgt():89 size=4294868992
[  178.990000] vb2_dc_pages_to_sgt() sgt->orig_nents=3
[  178.995000] vb2_dc_pages_to_sgt():83 cur_page=24
[  179.000000] vb2_dc_pages_to_sgt():84 offset=0
[  179.005000] vb2_dc_pages_to_sgt():86 chunk_size=131072
[  179.010000] vb2_dc_pages_to_sgt():89 size=4294737920
[  179.015000] vb2_dc_pages_to_sgt() sgt->orig_nents=3
[  179.020000] vb2_dc_pages_to_sgt():83 cur_page=0
[  179.025000] vb2_dc_pages_to_sgt():84 offset=0
[  179.030000] vb2_dc_pages_to_sgt():86 chunk_size=65536
[  179.035000] vb2_dc_pages_to_sgt():89 size=4294901760
[  179.040000] vb2_dc_pages_to_sgt() sgt->orig_nents=3
[  179.045000] vb2_dc_pages_to_sgt():83 cur_page=16
[  179.050000] vb2_dc_pages_to_sgt():84 offset=0
[  179.055000] vb2_dc_pages_to_sgt():86 chunk_size=131072
[  179.060000] vb2_dc_pages_to_sgt():89 size=4294770688
[  179.065000] vb2_dc_pages_to_sgt() sgt->orig_nents=3
[  179.070000] vb2_dc_pages_to_sgt():83 cur_page=48
[  179.075000] vb2_dc_pages_to_sgt():84 offset=0
[  179.080000] vb2_dc_pages_to_sgt():86 chunk_size=262144
[  179.085000] vb2_dc_pages_to_sgt():89 size=4294508544
[  179.090000] vb2_dc_pages_to_sgt() sgt->orig_nents=3
[  179.095000] vb2_dc_pages_to_sgt():83 cur_page=0
[  179.100000] vb2_dc_pages_to_sgt():84 offset=0
[  179.100000] vb2_dc_pages_to_sgt():86 chunk_size=32768
[  179.105000] vb2_dc_pages_to_sgt():89 size=4294934528
[  179.110000] vb2_dc_pages_to_sgt() sgt->orig_nents=3
[  179.115000] vb2_dc_pages_to_sgt():83 cur_page=8
[  179.120000] vb2_dc_pages_to_sgt():84 offset=0
[  179.125000] vb2_dc_pages_to_sgt():86 chunk_size=65536
[  179.130000] vb2_dc_pages_to_sgt():89 size=4294868992
[  179.135000] vb2_dc_pages_to_sgt() sgt->orig_nents=3
[  179.140000] vb2_dc_pages_to_sgt():83 cur_page=24
[  179.145000] vb2_dc_pages_to_sgt():84 offset=0
[  179.150000] vb2_dc_pages_to_sgt():86 chunk_size=131072
[  179.155000] vb2_dc_pages_to_sgt():89 size=4294737920
[  179.160000] vb2_dc_pages_to_sgt() sgt->orig_nents=2
[  179.165000] vb2_dc_pages_to_sgt():83 cur_page=0
[  179.170000] vb2_dc_pages_to_sgt():84 offset=0
[  179.175000] vb2_dc_pages_to_sgt():86 chunk_size=65536
[  179.180000] vb2_dc_pages_to_sgt():89 size=4294901760
[  179.185000] vb2_dc_pages_to_sgt() sgt->orig_nents=2
[  179.190000] vb2_dc_pages_to_sgt():83 cur_page=16
[  179.195000] vb2_dc_pages_to_sgt():84 offset=0
[  179.200000] vb2_dc_pages_to_sgt():86 chunk_size=393216
[  179.205000] vb2_dc_pages_to_sgt():89 size=4294508544
[  179.210000] vb2_dc_pages_to_sgt() sgt->orig_nents=3
[  179.215000] vb2_dc_pages_to_sgt():83 cur_page=0
[  179.220000] vb2_dc_pages_to_sgt():84 offset=0
[  179.220000] vb2_dc_pages_to_sgt():86 chunk_size=32768
[  179.225000] vb2_dc_pages_to_sgt():89 size=4294934528
[  179.230000] vb2_dc_pages_to_sgt() sgt->orig_nents=3
[  179.235000] vb2_dc_pages_to_sgt():83 cur_page=8
[  179.240000] vb2_dc_pages_to_sgt():84 offset=0
[  179.245000] vb2_dc_pages_to_sgt():86 chunk_size=65536
[  179.250000] vb2_dc_pages_to_sgt():89 size=4294868992
[  179.255000] vb2_dc_pages_to_sgt() sgt->orig_nents=3
[  179.260000] vb2_dc_pages_to_sgt():83 cur_page=24
[  179.265000] vb2_dc_pages_to_sgt():84 offset=0
[  179.270000] vb2_dc_pages_to_sgt():86 chunk_size=131072
[  179.275000] vb2_dc_pages_to_sgt():89 size=4294737920
[  179.280000] vb2_dc_pages_to_sgt() sgt->orig_nents=3
[  179.285000] vb2_dc_pages_to_sgt():83 cur_page=0
[  179.290000] vb2_dc_pages_to_sgt():84 offset=0
[  179.295000] vb2_dc_pages_to_sgt():86 chunk_size=65536
[  179.300000] vb2_dc_pages_to_sgt():89 size=4294901760
[  179.305000] vb2_dc_pages_to_sgt() sgt->orig_nents=3
[  179.310000] vb2_dc_pages_to_sgt():83 cur_page=16
[  179.315000] vb2_dc_pages_to_sgt():84 offset=0
[  179.320000] vb2_dc_pages_to_sgt():86 chunk_size=131072
[  179.325000] vb2_dc_pages_to_sgt():89 size=4294770688
[  179.330000] vb2_dc_pages_to_sgt() sgt->orig_nents=3
[  179.335000] vb2_dc_pages_to_sgt():83 cur_page=48
[  179.340000] vb2_dc_pages_to_sgt():84 offset=0
[  179.340000] vb2_dc_pages_to_sgt():86 chunk_size=262144
[  179.350000] vb2_dc_pages_to_sgt():89 size=4294508544
[  179.355000] vb2_dc_pages_to_sgt() sgt->orig_nents=3
[  179.355000] vb2_dc_pages_to_sgt():83 cur_page=0
[  179.360000] vb2_dc_pages_to_sgt():84 offset=0
[  179.365000] vb2_dc_pages_to_sgt():86 chunk_size=32768
[  179.370000] vb2_dc_pages_to_sgt():89 size=4294934528
[  179.375000] vb2_dc_pages_to_sgt() sgt->orig_nents=3
[  179.380000] vb2_dc_pages_to_sgt():83 cur_page=8
[  179.385000] vb2_dc_pages_to_sgt():84 offset=0
[  179.390000] vb2_dc_pages_to_sgt():86 chunk_size=65536
[  179.395000] vb2_dc_pages_to_sgt():89 size=4294868992
[  179.400000] vb2_dc_pages_to_sgt() sgt->orig_nents=3
[  179.405000] vb2_dc_pages_to_sgt():83 cur_page=24
[  179.410000] vb2_dc_pages_to_sgt():84 offset=0
[  179.415000] vb2_dc_pages_to_sgt():86 chunk_size=131072
[  179.420000] vb2_dc_pages_to_sgt():89 size=4294737920
[  179.425000] vb2_dc_pages_to_sgt() sgt->orig_nents=2
[  179.430000] vb2_dc_pages_to_sgt():83 cur_page=0
[  179.435000] vb2_dc_pages_to_sgt():84 offset=0
[  179.440000] vb2_dc_pages_to_sgt():86 chunk_size=65536
[  179.445000] vb2_dc_pages_to_sgt():89 size=4294901760
[  179.450000] vb2_dc_pages_to_sgt() sgt->orig_nents=2
[  179.455000] vb2_dc_pages_to_sgt():83 cur_page=16
[  179.460000] vb2_dc_pages_to_sgt():84 offset=0
[  179.460000] vb2_dc_pages_to_sgt():86 chunk_size=393216
[  179.470000] vb2_dc_pages_to_sgt():89 size=4294508544
[  179.475000] vb2_dc_pages_to_sgt() sgt->orig_nents=3
[  179.475000] vb2_dc_pages_to_sgt():83 cur_page=0
[  179.480000] vb2_dc_pages_to_sgt():84 offset=0
[  179.485000] vb2_dc_pages_to_sgt():86 chunk_size=32768
[  179.490000] vb2_dc_pages_to_sgt():89 size=4294934528
[  179.495000] vb2_dc_pages_to_sgt() sgt->orig_nents=3
[  179.500000] vb2_dc_pages_to_sgt():83 cur_page=8
[  179.505000] vb2_dc_pages_to_sgt():84 offset=0
[  179.510000] vb2_dc_pages_to_sgt():86 chunk_size=65536
[  179.515000] vb2_dc_pages_to_sgt():89 size=4294868992
[  179.520000] vb2_dc_pages_to_sgt() sgt->orig_nents=3
[  179.525000] vb2_dc_pages_to_sgt():83 cur_page=24
[  179.530000] vb2_dc_pages_to_sgt():84 offset=0
[  179.535000] vb2_dc_pages_to_sgt():86 chunk_size=131072
[  179.540000] vb2_dc_pages_to_sgt():89 size=4294737920
[  179.545000] mmc0: Too large timeout requested for CMD25!
[  179.545000] vb2_dc_pages_to_sgt() sgt->orig_nents=2
[  179.550000] vb2_dc_pages_to_sgt():83 cur_page=0
[  179.555000] mmc0: Too large timeout requested for CMD25!
[  179.555000] vb2_dc_pages_to_sgt():84 offset=0
[  179.560000] vb2_dc_pages_to_sgt():86 chunk_size=65536
[  179.565000] mmc0: Too large timeout requested for CMD25!
[  179.565000] vb2_dc_pages_to_sgt():89 size=4294901760
[  179.570000] vb2_dc_pages_to_sgt() sgt->orig_nents=2
[  179.575000] vb2_dc_pages_to_sgt():83 cur_page=16
[  179.580000] vb2_dc_pages_to_sgt():84 offset=0
[  179.580000] vb2_dc_pages_to_sgt():86 chunk_size=262144
[  179.590000] vb2_dc_pages_to_sgt():89 size=4294639616
[  179.595000] vb2_dc_mmap: mapped dma addr 0x40000000 at 0xb6d71000, size 458752
[  179.595000] vb2_dc_mmap: mapped dma addr 0x20100000 at 0xb6d39000, size 229376
[  179.595000] vb2_dc_mmap: mapped dma addr 0x40080000 at 0xb6cc9000, size 458752
[  179.595000] vb2_dc_mmap: mapped dma addr 0x20140000 at 0xb6c91000, size 229376
[  179.595000] vb2_dc_mmap: mapped dma addr 0x40100000 at 0xb6c21000, size 458752
[  179.595000] vb2_dc_mmap: mapped dma addr 0x20180000 at 0xb6be9000, size 229376
[  179.595000] vb2_dc_mmap: mapped dma addr 0x40180000 at 0xb6b79000, size 458752
[  179.595000] vb2_dc_mmap: mapped dma addr 0x201c0000 at 0xb6b41000, size 229376
[  179.595000] vb2_dc_mmap: mapped dma addr 0x40200000 at 0xb6ad1000, size 458752
[  179.595000] vb2_dc_mmap: mapped dma addr 0x20200000 at 0xb6a99000, size 229376
[  179.600000] vb2_dc_mmap: mapped dma addr 0x40280000 at 0xb6a29000, size 458752
[  179.600000] vb2_dc_mmap: mapped dma addr 0x20240000 at 0xb69f1000, size 229376
[  179.600000] vb2_dc_pages_to_sgt() sgt->orig_nents=4
[  179.605000] vb2_dc_pages_to_sgt():83 cur_page=0
[  179.610000] vb2_dc_pages_to_sgt():84 offset=0
[  179.615000] vb2_dc_pages_to_sgt():86 chunk_size=8192
[  179.620000] vb2_dc_pages_to_sgt():89 size=4294959104
[  179.625000] vb2_dc_pages_to_sgt() sgt->orig_nents=4
[  179.630000] vb2_dc_pages_to_sgt():83 cur_page=2
[  179.635000] vb2_dc_pages_to_sgt():84 offset=0
[  179.635000] vb2_dc_pages_to_sgt():86 chunk_size=16384
[  179.640000] vb2_dc_pages_to_sgt():89 size=4294942720
[  179.645000] vb2_dc_pages_to_sgt() sgt->orig_nents=4
[  179.650000] vb2_dc_pages_to_sgt():83 cur_page=6
[  179.655000] vb2_dc_pages_to_sgt():84 offset=0
[  179.660000] vb2_dc_pages_to_sgt():86 chunk_size=65536
[  179.665000] vb2_dc_pages_to_sgt():89 size=4294877184
[  179.670000] vb2_dc_pages_to_sgt() sgt->orig_nents=4
[  179.675000] vb2_dc_pages_to_sgt():83 cur_page=22
[  179.680000] vb2_dc_pages_to_sgt():84 offset=0
[  179.685000] vb2_dc_pages_to_sgt():86 chunk_size=524288
[  179.690000] vb2_dc_pages_to_sgt():89 size=4294352896
[  179.695000] vb2_dc_mmap: mapped dma addr 0x20000000 at 0xb695b000, size 614400
[  180.000000] mmc0: Too large timeout requested for CMD25!
[  180.075000] Entering vb2_dc_get_contiguous_size()
[  180.080000] sg_dma_address=0x20100000, sg_dma_len=393216
[  180.085000] expected=0x20160000
[  180.090000] Leaving vb2_dc_get_contiguous_size()
[  180.095000] vb2_dc_map_dmabuf():671contig_size=393216, buf->size=430080
[  180.100000] contiguous chunk is too small 393216/430080 b
Tomasz Stanislawski May 9, 2012, 9:58 a.m. UTC | #8
Hi Subash,
Could you post the code of vb2_dc_pages_to_sgt with all printk in it.
It will help us avoid guessing where and what is debugged in the log.

Moreover, I found a line 'size=4294836224' in the log.
It means that size is equal to -131072 (!?!) or there are some invalid
conversions in printk.

Are you suze that you do not pass size = 0 as the function argument?

Notice that earlier versions of dmabuf-for-vb2 patches has offset2
argument instead of size. It was the offset at the end of the buffer.
In (I guess) 95% of cases this offset was 0.

Could you provide only function arguments that causes the failure?
I mean pages array + size (I assume that offset is zero for your test).

Having the arguments we could reproduce that bug.

Regards,
Tomasz Stanislawski





On 05/09/2012 08:46 AM, Subash Patel wrote:
> Hello Tomasz, Laurent,
> 
> I have printed some logs during the dmabuf export and attach for the SGT issue below. Please find it in the attachment. I hope
> it will be useful.
> 
> Regards,
> Subash
> 
> On 05/08/2012 04:45 PM, Subash Patel wrote:
>> Hi Laurent,
>>
>> On 05/08/2012 02:44 PM, Laurent Pinchart wrote:
>>> Hi Subash,
>>>
>>> On Monday 07 May 2012 20:08:25 Subash Patel wrote:
>>>> Hello Thomasz, Laurent,
>>>>
>>>> I found an issue in the function vb2_dc_pages_to_sgt() below. I saw that
>>>> during the attach, the size of the SGT and size requested mis-matched
>>>> (by atleast 8k bytes). Hence I made a small correction to the code as
>>>> below. I could then attach the importer properly.
>>>
>>> Thank you for the report.
>>>
>>> Could you print the content of the sglist (number of chunks and size
>>> of each
>>> chunk) before and after your modifications, as well as the values of
>>> n_pages,
>>> offset and size ?
>> I will put back all the printk's and generate this. As of now, my setup
>> has changed and will do this when I get sometime.
>>>
>>>> On 04/20/2012 08:15 PM, Tomasz Stanislawski wrote:
>>>
>>> [snip]
>>>
>>>>> +static struct sg_table *vb2_dc_pages_to_sgt(struct page **pages,
>>>>> + unsigned int n_pages, unsigned long offset, unsigned long size)
>>>>> +{
>>>>> + struct sg_table *sgt;
>>>>> + unsigned int chunks;
>>>>> + unsigned int i;
>>>>> + unsigned int cur_page;
>>>>> + int ret;
>>>>> + struct scatterlist *s;
>>>>> +
>>>>> + sgt = kzalloc(sizeof *sgt, GFP_KERNEL);
>>>>> + if (!sgt)
>>>>> + return ERR_PTR(-ENOMEM);
>>>>> +
>>>>> + /* compute number of chunks */
>>>>> + chunks = 1;
>>>>> + for (i = 1; i< n_pages; ++i)
>>>>> + if (pages[i] != pages[i - 1] + 1)
>>>>> + ++chunks;
>>>>> +
>>>>> + ret = sg_alloc_table(sgt, chunks, GFP_KERNEL);
>>>>> + if (ret) {
>>>>> + kfree(sgt);
>>>>> + return ERR_PTR(-ENOMEM);
>>>>> + }
>>>>> +
>>>>> + /* merging chunks and putting them into the scatterlist */
>>>>> + cur_page = 0;
>>>>> + for_each_sg(sgt->sgl, s, sgt->orig_nents, i) {
>>>>> + unsigned long chunk_size;
>>>>> + unsigned int j;
>>>>
>>>> size = PAGE_SIZE;
>>>>
>>>>> +
>>>>> + for (j = cur_page + 1; j< n_pages; ++j)
>>>>
>>>> for (j = cur_page + 1; j< n_pages; ++j) {
>>>>
>>>>> + if (pages[j] != pages[j - 1] + 1)
>>>>> + break;
>>>>
>>>> size += PAGE
>>>> }
>>>>
>>>>> +
>>>>> + chunk_size = ((j - cur_page)<< PAGE_SHIFT) - offset;
>>>>> + sg_set_page(s, pages[cur_page], min(size, chunk_size), offset);
>>>>
>>>> [DELETE] size -= chunk_size;
>>>>
>>>>> + offset = 0;
>>>>> + cur_page = j;
>>>>> + }
>>>>> +
>>>>> + return sgt;
>>>>> +}
>>>
>> Regards,
>> Subash
diff mbox

Patch

diff --git a/drivers/media/video/videobuf2-dma-contig.c b/drivers/media/video/videobuf2-dma-contig.c
index 476e536..9cbc8d4 100644
--- a/drivers/media/video/videobuf2-dma-contig.c
+++ b/drivers/media/video/videobuf2-dma-contig.c
@@ -11,6 +11,8 @@ 
  */
 
 #include <linux/module.h>
+#include <linux/scatterlist.h>
+#include <linux/sched.h>
 #include <linux/slab.h>
 #include <linux/dma-mapping.h>
 
@@ -22,6 +24,8 @@  struct vb2_dc_buf {
 	void				*vaddr;
 	unsigned long			size;
 	dma_addr_t			dma_addr;
+	enum dma_data_direction		dma_dir;
+	struct sg_table			*dma_sgt;
 
 	/* MMAP related */
 	struct vb2_vmarea_handler	handler;
@@ -32,6 +36,95 @@  struct vb2_dc_buf {
 };
 
 /*********************************************/
+/*        scatterlist table functions        */
+/*********************************************/
+
+static struct sg_table *vb2_dc_pages_to_sgt(struct page **pages,
+	unsigned int n_pages, unsigned long offset, unsigned long size)
+{
+	struct sg_table *sgt;
+	unsigned int chunks;
+	unsigned int i;
+	unsigned int cur_page;
+	int ret;
+	struct scatterlist *s;
+
+	sgt = kzalloc(sizeof *sgt, GFP_KERNEL);
+	if (!sgt)
+		return ERR_PTR(-ENOMEM);
+
+	/* compute number of chunks */
+	chunks = 1;
+	for (i = 1; i < n_pages; ++i)
+		if (pages[i] != pages[i - 1] + 1)
+			++chunks;
+
+	ret = sg_alloc_table(sgt, chunks, GFP_KERNEL);
+	if (ret) {
+		kfree(sgt);
+		return ERR_PTR(-ENOMEM);
+	}
+
+	/* merging chunks and putting them into the scatterlist */
+	cur_page = 0;
+	for_each_sg(sgt->sgl, s, sgt->orig_nents, i) {
+		unsigned long chunk_size;
+		unsigned int j;
+
+		for (j = cur_page + 1; j < n_pages; ++j)
+			if (pages[j] != pages[j - 1] + 1)
+				break;
+
+		chunk_size = ((j - cur_page) << PAGE_SHIFT) - offset;
+		sg_set_page(s, pages[cur_page], min(size, chunk_size), offset);
+		size -= chunk_size;
+		offset = 0;
+		cur_page = j;
+	}
+
+	return sgt;
+}
+
+static void vb2_dc_release_sgtable(struct sg_table *sgt)
+{
+	sg_free_table(sgt);
+	kfree(sgt);
+}
+
+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->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;
+	dma_addr_t expected = sg_dma_address(sgt->sgl);
+	unsigned int i;
+	unsigned long size = 0;
+
+	for_each_sg(sgt->sgl, s, sgt->nents, i) {
+		if (sg_dma_address(s) != expected)
+			break;
+		expected = sg_dma_address(s) + sg_dma_len(s);
+		size += sg_dma_len(s);
+	}
+	return size;
+}
+
+/*********************************************/
 /*         callbacks for all buffers         */
 /*********************************************/
 
@@ -116,42 +209,194 @@  static int vb2_dc_mmap(void *buf_priv, struct vm_area_struct *vma)
 /*       callbacks for USERPTR buffers       */
 /*********************************************/
 
+static inline int vma_is_io(struct vm_area_struct *vma)
+{
+	return !!(vma->vm_flags & (VM_IO | VM_PFNMAP));
+}
+
+static struct vm_area_struct *vb2_dc_get_user_vma(
+	unsigned long start, unsigned long size)
+{
+	struct vm_area_struct *vma;
+
+	/* current->mm->mmap_sem is taken by videobuf2 core */
+	vma = find_vma(current->mm, start);
+	if (!vma) {
+		printk(KERN_ERR "no vma for address %lu\n", start);
+		return ERR_PTR(-EFAULT);
+	}
+
+	if (vma->vm_end - vma->vm_start < size) {
+		printk(KERN_ERR "vma at %lu is too small for %lu bytes\n",
+			start, size);
+		return ERR_PTR(-EFAULT);
+	}
+
+	vma = vb2_get_vma(vma);
+	if (!vma) {
+		printk(KERN_ERR "failed to copy vma\n");
+		return ERR_PTR(-ENOMEM);
+	}
+
+	return vma;
+}
+
+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 (ret) {
+				printk(KERN_ERR "no page for address %lu\n",
+					start);
+				return ret;
+			}
+			pages[i] = pfn_to_page(pfn);
+		}
+	} else {
+		unsigned int n;
+
+		n = get_user_pages(current, current->mm, start & PAGE_MASK,
+			n_pages, write, 1, pages, NULL);
+		if (n != n_pages) {
+			printk(KERN_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;
+
+	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);
+
+	vb2_dc_release_sgtable(sgt);
+	vb2_put_vma(buf->vma);
+	kfree(buf);
+}
+
 static void *vb2_dc_get_userptr(void *alloc_ctx, unsigned long vaddr,
-					unsigned long size, int write)
+	unsigned long size, int write)
 {
 	struct vb2_dc_buf *buf;
-	struct vm_area_struct *vma;
-	dma_addr_t dma_addr = 0;
-	int ret;
+	unsigned long start;
+	unsigned long end;
+	unsigned long offset;
+	struct page **pages;
+	int n_pages;
+	int ret = 0;
+	struct sg_table *sgt;
+	unsigned long contig_size;
 
 	buf = kzalloc(sizeof *buf, GFP_KERNEL);
 	if (!buf)
 		return ERR_PTR(-ENOMEM);
 
-	ret = vb2_get_contig_userptr(vaddr, size, &vma, &dma_addr);
+	buf->dev = alloc_ctx;
+	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;
+		printk(KERN_ERR "failed to allocate pages table\n");
+		goto fail_buf;
+	}
+
+	buf->vma = vb2_dc_get_user_vma(start, size);
+	if (IS_ERR(buf->vma)) {
+		printk(KERN_ERR "failed to get VMA\n");
+		ret = PTR_ERR(buf->vma);
+		goto fail_pages;
+	}
+
+	/* extract page list from userspace mapping */
+	ret = vb2_dc_get_user_pages(start, pages, n_pages, buf->vma, write);
 	if (ret) {
-		printk(KERN_ERR "Failed acquiring VMA for vaddr 0x%08lx\n",
-				vaddr);
-		kfree(buf);
-		return ERR_PTR(ret);
+		printk(KERN_ERR "failed to get user pages\n");
+		goto fail_vma;
+	}
+
+	sgt = vb2_dc_pages_to_sgt(pages, n_pages, offset, size);
+	if (IS_ERR(sgt)) {
+		printk(KERN_ERR "failed to create scatterlist table\n");
+		ret = -ENOMEM;
+		goto fail_get_user_pages;
+	}
+
+	/* 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) {
+		printk(KERN_ERR "failed to map scatterlist\n");
+		ret = -EIO;
+		goto fail_sgt;
 	}
 
+	contig_size = vb2_dc_get_contiguous_size(sgt);
+	if (contig_size < size) {
+		printk(KERN_ERR "contiguous mapping is too small %lu/%lu\n",
+			contig_size, size);
+		ret = -EFAULT;
+		goto fail_map_sg;
+	}
+
+	buf->dma_addr = sg_dma_address(sgt->sgl);
 	buf->size = size;
-	buf->dma_addr = dma_addr;
-	buf->vma = vma;
+	buf->dma_sgt = sgt;
 
 	return buf;
-}
 
-static void vb2_dc_put_userptr(void *mem_priv)
-{
-	struct vb2_dc_buf *buf = mem_priv;
+fail_map_sg:
+	dma_unmap_sg(buf->dev, sgt->sgl, sgt->nents, buf->dma_dir);
 
-	if (!buf)
-		return;
+fail_sgt:
+	if (!vma_is_io(buf->vma))
+		vb2_dc_sgt_foreach_page(sgt, put_page);
+	vb2_dc_release_sgtable(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);
+
+	return ERR_PTR(ret);
 }
 
 /*********************************************/