From patchwork Mon Mar 17 19:49:32 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jan Kara X-Patchwork-Id: 23132 Received: from mail.tu-berlin.de ([130.149.7.33]) by www.linuxtv.org with esmtp (Exim 4.72) (envelope-from ) id 1WPdY3-00086w-9e; Mon, 17 Mar 2014 20:50:11 +0100 X-tubIT-Incoming-IP: 209.132.180.67 Received: from vger.kernel.org ([209.132.180.67]) by mail.tu-berlin.de (exim-4.72/mailfrontend-5) with esmtp id 1WPdY1-0001v5-7b; Mon, 17 Mar 2014 20:50:11 +0100 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751589AbaCQTt6 (ORCPT + 1 other); Mon, 17 Mar 2014 15:49:58 -0400 Received: from cantor2.suse.de ([195.135.220.15]:48036 "EHLO mx2.suse.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751340AbaCQTtp (ORCPT ); Mon, 17 Mar 2014 15:49:45 -0400 Received: from relay1.suse.de (charybdis-ext.suse.de [195.135.220.254]) by mx2.suse.de (Postfix) with ESMTP id 18BCE75023; Mon, 17 Mar 2014 19:49:44 +0000 (UTC) Received: by quack.suse.cz (Postfix, from userid 1000) id 1727E81ED5; Mon, 17 Mar 2014 20:49:41 +0100 (CET) From: Jan Kara To: linux-mm@kvack.org Cc: linux-media@vger.kernel.org, Jan Kara Subject: [PATCH 5/9] media: vb2: Convert vb2_vmalloc_get_userptr() to use pfns vector Date: Mon, 17 Mar 2014 20:49:32 +0100 Message-Id: <1395085776-8626-6-git-send-email-jack@suse.cz> X-Mailer: git-send-email 1.8.1.4 In-Reply-To: <1395085776-8626-1-git-send-email-jack@suse.cz> References: <1395085776-8626-1-git-send-email-jack@suse.cz> Sender: linux-media-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-media@vger.kernel.org X-PMX-Version: 6.0.0.2142326, Antispam-Engine: 2.7.2.2107409, Antispam-Data: 2014.3.17.193915 X-PMX-Spam: Gauge=IIIIIIII, Probability=8%, Report=' MULTIPLE_RCPTS 0.1, HTML_00_01 0.05, HTML_00_10 0.05, BODY_SIZE_4000_4999 0, BODY_SIZE_5000_LESS 0, BODY_SIZE_7000_LESS 0, URI_ENDS_IN_HTML 0, __ANY_URI 0, __CP_URI_IN_BODY 0, __HAS_FROM 0, __HAS_MSGID 0, __HAS_X_MAILER 0, __HAS_X_MAILING_LIST 0, __IN_REP_TO 0, __MIME_TEXT_ONLY 0, __MULTIPLE_RCPTS_CC_X2 0, __SANE_MSGID 0, __SUBJ_ALPHA_END 0, __SXL_FUR_ERROR_SERVFAIL , __SXL_RIP_ERROR_SERVFAIL , __SXL_SIG_ERROR_SERVFAIL , __TO_MALFORMED_2 0, __TO_NO_NAME 0, __URI_NO_WWW 0, __URI_NS ' Convert 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. Signed-off-by: Jan Kara --- drivers/media/v4l2-core/videobuf2-vmalloc.c | 86 +++++++++++------------------ 1 file changed, 33 insertions(+), 53 deletions(-) diff --git a/drivers/media/v4l2-core/videobuf2-vmalloc.c b/drivers/media/v4l2-core/videobuf2-vmalloc.c index ab38e054d1a0..3b4e53bd97d7 100644 --- a/drivers/media/v4l2-core/videobuf2-vmalloc.c +++ b/drivers/media/v4l2-core/videobuf2-vmalloc.c @@ -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); }