From patchwork Fri Jul 19 17:02:33 2013 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ricardo Ribalda Delgado X-Patchwork-Id: 2830634 Return-Path: X-Original-To: patchwork-linux-media@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork1.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.19.201]) by patchwork1.web.kernel.org (Postfix) with ESMTP id 61DB89F4D4 for ; Fri, 19 Jul 2013 17:02:51 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id E3F8E20364 for ; Fri, 19 Jul 2013 17:02:49 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id A8BDF2035A for ; Fri, 19 Jul 2013 17:02:48 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752386Ab3GSRCr (ORCPT ); Fri, 19 Jul 2013 13:02:47 -0400 Received: from mail-la0-f46.google.com ([209.85.215.46]:36848 "EHLO mail-la0-f46.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751694Ab3GSRCq (ORCPT ); Fri, 19 Jul 2013 13:02:46 -0400 Received: by mail-la0-f46.google.com with SMTP id hi8so835353lab.19 for ; Fri, 19 Jul 2013 10:02:44 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=from:to:cc:subject:date:message-id:x-mailer:in-reply-to:references; bh=LrXhVueMbIqp11ZeHx2T4gTWc8/iDlkQBWStpW+75AA=; b=tV528QCN8+P/EfKIXeXi4Z4L4PwATSXUPu95+HWxo34NJD9vqcgzD9VAngfRmnJj5J 5NcPYeNrc1peiRZQDxv3bN0Klq2NnZyT7Dn9sTvDq9sUFMPIAEUDYzLCjeAKYfbzokGr KHYf13bVQ/3El41G+yH4qMNmCEdrGgNlIGHk+KdubpUiGK87JtydbIUIDwRceFutdLZW aIinFsIOAoatXKZTcW7I97Xz/s+Pg+XNYa2Ydnt784q0o79ASMm6ZUlvaMkIBy0i6g30 BJwU3z+388xCABguhEUI57ywpoEtfUQJdONQGt03qOhD5Q/AwxL38App4i6bRsHPYxqd w2Mg== X-Received: by 10.112.141.202 with SMTP id rq10mr7657071lbb.83.1374253364620; Fri, 19 Jul 2013 10:02:44 -0700 (PDT) Received: from localhost (x1-6-30-46-9a-6d-3c-54.k132.webspeed.dk. [80.163.18.122]) by mx.google.com with ESMTPSA id et10sm6434780lbc.6.2013.07.19.10.02.42 for (version=TLSv1.2 cipher=RC4-SHA bits=128/128); Fri, 19 Jul 2013 10:02:43 -0700 (PDT) From: Ricardo Ribalda Delgado To: Jonathan Corbet , Mauro Carvalho Chehab , Pawel Osciak , Marek Szyprowski , Kyungmin Park , Ismael Luceno , Greg Kroah-Hartman , linux-media@vger.kernel.org, Andre Heider , Sylwester Nawrocki Cc: Ricardo Ribalda Delgado Subject: [PATCH 1/2] videobuf2-dma-sg: Allocate pages as contiguous as possible Date: Fri, 19 Jul 2013 19:02:33 +0200 Message-Id: <1374253355-3788-2-git-send-email-ricardo.ribalda@gmail.com> X-Mailer: git-send-email 1.7.10.4 In-Reply-To: <1374253355-3788-1-git-send-email-ricardo.ribalda@gmail.com> References: <1374253355-3788-1-git-send-email-ricardo.ribalda@gmail.com> Sender: linux-media-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-media@vger.kernel.org X-Spam-Status: No, score=-7.2 required=5.0 tests=BAYES_00, DKIM_ADSP_CUSTOM_MED, DKIM_SIGNED, FREEMAIL_FROM, RCVD_IN_DNSWL_HI, RP_MATCHES_RCVD, T_DKIM_INVALID, UNPARSEABLE_RELAY autolearn=ham version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP Most DMA engines have limitations regarding the number of DMA segments (sg-buffers) that they can handle. Videobuffers can easily spread through houndreds of pages. In the previous aproach, the pages were allocated individually, this could led to the creation houndreds of dma segments (sg-buffers) that could not be handled by some DMA engines. This patch tries to minimize the number of DMA segments by using alloc_pages. In the worst case it will behave as before, but most of the times it will reduce the number of dma segments Signed-off-by: Ricardo Ribalda Delgado Acked-by: Marek Szyprowski Reviewed-by: Andre Heider --- drivers/media/v4l2-core/videobuf2-dma-sg.c | 60 +++++++++++++++++++++++----- 1 file changed, 49 insertions(+), 11 deletions(-) diff --git a/drivers/media/v4l2-core/videobuf2-dma-sg.c b/drivers/media/v4l2-core/videobuf2-dma-sg.c index 16ae3dc..c053605 100644 --- a/drivers/media/v4l2-core/videobuf2-dma-sg.c +++ b/drivers/media/v4l2-core/videobuf2-dma-sg.c @@ -42,10 +42,55 @@ struct vb2_dma_sg_buf { static void vb2_dma_sg_put(void *buf_priv); +static int vb2_dma_sg_alloc_compacted(struct vb2_dma_sg_buf *buf, + gfp_t gfp_flags) +{ + unsigned int last_page = 0; + int size = buf->sg_desc.size; + + while (size > 0) { + struct page *pages; + int order; + int i; + + order = get_order(size); + /* Dont over allocate*/ + if ((PAGE_SIZE << order) > size) + order--; + + pages = NULL; + while (!pages) { + pages = alloc_pages(GFP_KERNEL | __GFP_ZERO | + __GFP_NOWARN | gfp_flags, order); + if (pages) + break; + + if (order == 0) + while (last_page--) { + __free_page(buf->pages[last_page]); + return -ENOMEM; + } + order--; + } + + split_page(pages, order); + for (i = 0; i < (1<pages[last_page] = pages[i]; + sg_set_page(&buf->sg_desc.sglist[last_page], + buf->pages[last_page], PAGE_SIZE, 0); + last_page++; + } + + size -= PAGE_SIZE << order; + } + + return 0; +} + static void *vb2_dma_sg_alloc(void *alloc_ctx, unsigned long size, gfp_t gfp_flags) { struct vb2_dma_sg_buf *buf; - int i; + int ret; buf = kzalloc(sizeof *buf, GFP_KERNEL); if (!buf) @@ -69,14 +114,9 @@ static void *vb2_dma_sg_alloc(void *alloc_ctx, unsigned long size, gfp_t gfp_fla if (!buf->pages) goto fail_pages_array_alloc; - for (i = 0; i < buf->sg_desc.num_pages; ++i) { - buf->pages[i] = alloc_page(GFP_KERNEL | __GFP_ZERO | - __GFP_NOWARN | gfp_flags); - if (NULL == buf->pages[i]) - goto fail_pages_alloc; - sg_set_page(&buf->sg_desc.sglist[i], - buf->pages[i], PAGE_SIZE, 0); - } + ret = vb2_dma_sg_alloc_compacted(buf, gfp_flags); + if (ret) + goto fail_pages_alloc; buf->handler.refcount = &buf->refcount; buf->handler.put = vb2_dma_sg_put; @@ -89,8 +129,6 @@ static void *vb2_dma_sg_alloc(void *alloc_ctx, unsigned long size, gfp_t gfp_fla return buf; fail_pages_alloc: - while (--i >= 0) - __free_page(buf->pages[i]); kfree(buf->pages); fail_pages_array_alloc: