From patchwork Wed May 23 13:07:34 2012 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tomasz Stanislawski X-Patchwork-Id: 8924 Return-Path: X-Original-To: patchwork@peony.canonical.com Delivered-To: patchwork@peony.canonical.com Received: from fiordland.canonical.com (fiordland.canonical.com [91.189.94.145]) by peony.canonical.com (Postfix) with ESMTP id 23E7423F04 for ; Wed, 23 May 2012 13:08:04 +0000 (UTC) Received: from mail-qc0-f180.google.com (mail-qc0-f180.google.com [209.85.216.180]) by fiordland.canonical.com (Postfix) with ESMTP id D5CCDA18103 for ; Wed, 23 May 2012 13:08:03 +0000 (UTC) Received: by qcmv28 with SMTP id v28so5823929qcm.11 for ; Wed, 23 May 2012 06:08:03 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20120113; h=x-forwarded-to:x-forwarded-for:delivered-to:received-spf :mime-version:date:from:in-reply-to:to:message-id:x-mailer :references:cc:subject:x-beenthere:x-mailman-version:precedence :list-id:list-unsubscribe:list-archive:list-post:list-help :list-subscribe:content-type:content-transfer-encoding:sender :errors-to:x-gm-message-state; bh=PO+0osZbpbtptisdsB0Lh0X+X66P3dHIG4fHGHDxeDc=; b=DjGntGqEmqtuWEQjecSx8buKfyLW4tJW4TSh5trcX56UH3kSXR1W3Nj2uzsZJI1y/M uJLST5T3757Uw50rWZ1yJRnJkAPRNf5/9xAkpgZr830ilpv4vR5Nk/icZyruIbaS1oPh Fzrag/tWda8NvhL17MsTAeOhk7okC6lCZVBoQCGTw3ihF/IcxX/N4LSMlDAMSEQjlYHz hNFTnNKB1uXWydADwNkx+pjdj2aICkkRMwR2zBeWzSEACmH+bNZkol0hJmCz5l8Z/g43 u0FMyV1teZXMTYZMpIX5KBc8/P9ul1AF2XtETcqFlP+M+0I1riuademzYttB1o3Lxo5a y4qQ== Received: by 10.50.40.193 with SMTP id z1mr13144941igk.0.1337778483018; Wed, 23 May 2012 06:08:03 -0700 (PDT) X-Forwarded-To: linaro-patchwork@canonical.com X-Forwarded-For: patch@linaro.org linaro-patchwork@canonical.com Delivered-To: patches@linaro.org Received: by 10.231.35.72 with SMTP id o8csp443468ibd; Wed, 23 May 2012 06:07:59 -0700 (PDT) Received: by 10.204.133.220 with SMTP id g28mr11486838bkt.54.1337778477975; Wed, 23 May 2012 06:07:57 -0700 (PDT) Received: from mombin.canonical.com (mombin.canonical.com. [91.189.95.16]) by mx.google.com with ESMTP id n52si8389154wed.98.2012.05.23.06.07.56; Wed, 23 May 2012 06:07:57 -0700 (PDT) Received-SPF: neutral (google.com: 91.189.95.16 is neither permitted nor denied by best guess record for domain of linaro-mm-sig-bounces@lists.linaro.org) client-ip=91.189.95.16; Authentication-Results: mx.google.com; spf=neutral (google.com: 91.189.95.16 is neither permitted nor denied by best guess record for domain of linaro-mm-sig-bounces@lists.linaro.org) smtp.mail=linaro-mm-sig-bounces@lists.linaro.org Received: from localhost ([127.0.0.1] helo=mombin.canonical.com) by mombin.canonical.com with esmtp (Exim 4.71) (envelope-from ) id 1SXBIB-0006XO-K3; Wed, 23 May 2012 13:07:55 +0000 Received: from mailout3.w1.samsung.com ([210.118.77.13]) by mombin.canonical.com with esmtp (Exim 4.71) (envelope-from ) id 1SXBI3-0006Tt-T8 for linaro-mm-sig@lists.linaro.org; Wed, 23 May 2012 13:07:47 +0000 MIME-version: 1.0 Received: from euspt1 ([210.118.77.13]) by mailout3.w1.samsung.com (Sun Java(tm) System Messaging Server 6.3-8.04 (built Jul 29 2009; 32bit)) with ESMTP id <0M4H007PP8FJBG60@mailout3.w1.samsung.com> for linaro-mm-sig@lists.linaro.org; Wed, 23 May 2012 14:06:55 +0100 (BST) Received: from linux.samsung.com ([106.116.38.10]) by spt1.w1.samsung.com (iPlanet Messaging Server 5.2 Patch 2 (built Jul 14 2004)) with ESMTPA id <0M4H00KL28GX5D@spt1.w1.samsung.com> for linaro-mm-sig@lists.linaro.org; Wed, 23 May 2012 14:07:45 +0100 (BST) Received: from mcdsrvbld02.digital.local (unknown [106.116.37.23]) by linux.samsung.com (Postfix) with ESMTP id 5E5C3270058; Wed, 23 May 2012 15:19:02 +0200 (CEST) Date: Wed, 23 May 2012 15:07:34 +0200 From: Tomasz Stanislawski In-reply-to: <1337778455-27912-1-git-send-email-t.stanislaws@samsung.com> To: linux-media@vger.kernel.org, dri-devel@lists.freedesktop.org Message-id: <1337778455-27912-12-git-send-email-t.stanislaws@samsung.com> X-Mailer: git-send-email 1.7.10 References: <1337778455-27912-1-git-send-email-t.stanislaws@samsung.com> Cc: pawel@osciak.com, sumit.semwal@ti.com, mchehab@redhat.com, robdclark@gmail.com, linaro-mm-sig@lists.linaro.org, kyungmin.park@samsung.com, airlied@redhat.com, remi@remlab.net, g.liakhovetski@gmx.de Subject: [Linaro-mm-sig] [PATCH 11/12] v4l: vb2-dma-contig: use sg_alloc_table_from_pages function X-BeenThere: linaro-mm-sig@lists.linaro.org X-Mailman-Version: 2.1.13 Precedence: list List-Id: "Unified memory management interest group." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: linaro-mm-sig-bounces@lists.linaro.org Errors-To: linaro-mm-sig-bounces@lists.linaro.org X-Gm-Message-State: ALoCoQmKDYXawPTm4cqW2VV47DUo3QaCjLz4j/5sf1idZpWroYkJT5ckPnAXdQSEAYnLDTSB6Ecm This patch makes use of sg_alloc_table_from_pages to simplify handling of sg tables. Signed-off-by: Tomasz Stanislawski Signed-off-by: Kyungmin Park --- drivers/media/video/videobuf2-dma-contig.c | 90 ++++++++-------------------- 1 file changed, 25 insertions(+), 65 deletions(-) diff --git a/drivers/media/video/videobuf2-dma-contig.c b/drivers/media/video/videobuf2-dma-contig.c index 59ee81c..b5caf1d 100644 --- a/drivers/media/video/videobuf2-dma-contig.c +++ b/drivers/media/video/videobuf2-dma-contig.c @@ -32,7 +32,7 @@ struct vb2_dc_buf { /* MMAP related */ struct vb2_vmarea_handler handler; atomic_t refcount; - struct sg_table *sgt_base; + struct sg_table sgt_base; /* USERPTR related */ struct vm_area_struct *vma; @@ -45,57 +45,6 @@ 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)) @@ -190,7 +139,7 @@ static void vb2_dc_put(void *buf_priv) if (!atomic_dec_and_test(&buf->refcount)) return; - vb2_dc_release_sgtable(buf->sgt_base); + sg_free_table(&buf->sgt_base); dma_free_coherent(buf->dev, buf->size, buf->vaddr, buf->dma_addr); kfree(buf); } @@ -254,9 +203,9 @@ static void *vb2_dc_alloc(void *alloc_ctx, unsigned long size) goto fail_pages; } - buf->sgt_base = vb2_dc_pages_to_sgt(pages, n_pages, 0, size); - if (IS_ERR(buf->sgt_base)) { - ret = PTR_ERR(buf->sgt_base); + ret = sg_alloc_table_from_pages(&buf->sgt_base, + pages, n_pages, 0, size, GFP_KERNEL); + if (ret) { dev_err(dev, "failed to prepare sg table\n"); goto fail_pages; } @@ -379,13 +328,13 @@ static struct sg_table *vb2_dc_dmabuf_ops_map( attach->dir = dir; /* copying the buf->base_sgt to attachment */ - ret = sg_alloc_table(sgt, buf->sgt_base->orig_nents, GFP_KERNEL); + ret = sg_alloc_table(sgt, buf->sgt_base.orig_nents, GFP_KERNEL); if (ret) { kfree(attach); return ERR_PTR(-ENOMEM); } - rd = buf->sgt_base->sgl; + rd = buf->sgt_base.sgl; wr = sgt->sgl; for (i = 0; i < sgt->orig_nents; ++i) { sg_set_page(wr, sg_page(rd), rd->length, rd->offset); @@ -519,7 +468,8 @@ static void vb2_dc_put_userptr(void *buf_priv) if (!vma_is_io(buf->vma)) vb2_dc_sgt_foreach_page(sgt, vb2_dc_put_dirty_page); - vb2_dc_release_sgtable(sgt); + sg_free_table(sgt); + kfree(sgt); vb2_put_vma(buf->vma); kfree(buf); } @@ -586,13 +536,20 @@ static void *vb2_dc_get_userptr(void *alloc_ctx, unsigned long vaddr, 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"); + sgt = kzalloc(sizeof *sgt, GFP_KERNEL); + if (!sgt) { + printk(KERN_ERR "failed to allocate sg table\n"); ret = -ENOMEM; goto fail_get_user_pages; } + ret = sg_alloc_table_from_pages(sgt, pages, n_pages, + offset, size, GFP_KERNEL); + if (ret) { + printk(KERN_ERR "failed to initialize sg table\n"); + goto fail_sgt; + } + /* pages are no longer needed */ kfree(pages); pages = NULL; @@ -602,7 +559,7 @@ static void *vb2_dc_get_userptr(void *alloc_ctx, unsigned long vaddr, if (sgt->nents <= 0) { printk(KERN_ERR "failed to map scatterlist\n"); ret = -EIO; - goto fail_sgt; + goto fail_sgt_init; } contig_size = vb2_dc_get_contiguous_size(sgt); @@ -622,10 +579,13 @@ static void *vb2_dc_get_userptr(void *alloc_ctx, unsigned long vaddr, fail_map_sg: dma_unmap_sg(buf->dev, sgt->sgl, sgt->nents, buf->dma_dir); -fail_sgt: +fail_sgt_init: if (!vma_is_io(buf->vma)) vb2_dc_sgt_foreach_page(sgt, put_page); - vb2_dc_release_sgtable(sgt); + sg_free_table(sgt); + +fail_sgt: + kfree(sgt); fail_get_user_pages: if (pages && !vma_is_io(buf->vma))