From patchwork Thu Oct 13 09:04:03 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tvrtko Ursulin X-Patchwork-Id: 9374577 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 5C40F60839 for ; Thu, 13 Oct 2016 09:04:26 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 4C51629E6B for ; Thu, 13 Oct 2016 09:04:26 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 4135A29E7C; Thu, 13 Oct 2016 09:04:26 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-4.1 required=2.0 tests=BAYES_00,DKIM_SIGNED, RCVD_IN_DNSWL_MED,T_DKIM_INVALID autolearn=ham version=3.3.1 Received: from gabe.freedesktop.org (gabe.freedesktop.org [131.252.210.177]) (using TLSv1.2 with cipher DHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id E341229E6B for ; Thu, 13 Oct 2016 09:04:25 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id 2C5C86E9C0; Thu, 13 Oct 2016 09:04:24 +0000 (UTC) X-Original-To: Intel-gfx@lists.freedesktop.org Delivered-To: Intel-gfx@lists.freedesktop.org Received: from mail-lf0-x243.google.com (mail-lf0-x243.google.com [IPv6:2a00:1450:4010:c07::243]) by gabe.freedesktop.org (Postfix) with ESMTPS id 7CC666E3E3 for ; Thu, 13 Oct 2016 09:04:22 +0000 (UTC) Received: by mail-lf0-x243.google.com with SMTP id b75so11439687lfg.3 for ; Thu, 13 Oct 2016 02:04:22 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ursulin-net.20150623.gappssmtp.com; s=20150623; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=x1+cOMAvhooDQI5mnlRNFdmQPykDKd/Wc8Ic8oMaKU8=; b=m2hLbQn+oasZ2DGJgCsRHfMYyp2wPnMKAPgCpvMMRQwOYzAqpbmvETMv+CjlmrZQma vajjETjAgi08M9rgf3oMB5F1TaVwcnA/oxSEZficgvXRTV0bfwQAp7KbyNncBBzzvT2x VzoXrcoUVcADec/Y4S0QMIRyyMxSi3hlqGI02dwGt3C4hrQU9GFebKsEXP/sAB7lqrSj 1vo5Z5fXugoMRPpj4vdoOXhzxra/I/HLx0RkIG1UIFlJ/rdh1k5lLppLOBQXtLCLpHqW VBeG7cpwDxNxnwfX75jiPTZyohSMI7gLPK2IK/3dvkaz2IP3p5EZNZRhctawd2cIgVnA dirQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=x1+cOMAvhooDQI5mnlRNFdmQPykDKd/Wc8Ic8oMaKU8=; b=W+IaRUi3ZhvExfFpRVK/lagWJZT2cDZ3fAuoTzckuP4xbP3hCqRDGsw6t87BCBDrth 9oG3DseNWswoyK870tlbYNqrQ5AIXYOUZgI80xuXDX0g5LtmiWm/6TbMAAw1U6cIxa2N 4j6E8kpPwHJdQ0Zp8m+RGtGciKHmt+NTsoq2ZnGUOqMGgoRdExXUePx4N4m+YXmJ7tgy 0ZVKn5duOTgxvry2xRWxoXETQZQv6H3pVoSAnWNWDDAsk4MqJLtNSt4FU21pOyFeCWSz DjKh+vHFJm/AQmmm9LEBv03ASgYUWF3zKjgEEgJ3qoCVTpKNvmZQDOjFc2UDYpeTg8VN ebXQ== X-Gm-Message-State: AA6/9Rled54CtqTdPyap1uj7aKvNpXzcaat9gFEyQ9BNUYdRzfwBvRWJxay5YIcitNakwQ== X-Received: by 10.28.186.8 with SMTP id k8mr1410210wmf.83.1476349460317; Thu, 13 Oct 2016 02:04:20 -0700 (PDT) Received: from localhost.localdomain ([2a02:c7d:9b6d:e300:916a:6cab:ac67:71c2]) by smtp.gmail.com with ESMTPSA id y2sm20776894wjx.20.2016.10.13.02.04.19 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 13 Oct 2016 02:04:19 -0700 (PDT) From: Tvrtko Ursulin X-Google-Original-From: Tvrtko Ursulin To: Intel-gfx@lists.freedesktop.org Date: Thu, 13 Oct 2016 10:04:03 +0100 Message-Id: <1476349444-7331-7-git-send-email-tvrtko.ursulin@linux.intel.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1476349444-7331-1-git-send-email-tvrtko.ursulin@linux.intel.com> References: <1476349444-7331-1-git-send-email-tvrtko.ursulin@linux.intel.com> Subject: [Intel-gfx] [RFC 6/7] lib/scatterlist: Add sg_trim_table X-BeenThere: intel-gfx@lists.freedesktop.org X-Mailman-Version: 2.1.18 Precedence: list List-Id: Intel graphics driver community testing & development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Errors-To: intel-gfx-bounces@lists.freedesktop.org Sender: "Intel-gfx" X-Virus-Scanned: ClamAV using ClamSMTP From: Tvrtko Ursulin *** BROKEN *** RFC only *** BROKEN *** In cases where it is hard to know to minimum numbers of ents table will need to hold at sg_alloc_time, and we end up with a table with unused sg entries at its end, this function will trim (free) the unused sg entry blocks and adjust the table->orig_nents down. Signed-off-by: Tvrtko Ursulin --- include/linux/scatterlist.h | 2 ++ lib/scatterlist.c | 53 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 55 insertions(+) diff --git a/include/linux/scatterlist.h b/include/linux/scatterlist.h index cb3c8fe6acd7..b344ecc8eddf 100644 --- a/include/linux/scatterlist.h +++ b/include/linux/scatterlist.h @@ -266,6 +266,8 @@ int sg_alloc_table_from_pages(struct sg_table *sgt, unsigned long offset, unsigned long size, gfp_t gfp_mask); +void sg_trim_table(struct sg_table *); + size_t sg_copy_buffer(struct scatterlist *sgl, unsigned int nents, void *buf, size_t buflen, off_t skip, bool to_buffer); diff --git a/lib/scatterlist.c b/lib/scatterlist.c index 004fc70fc56a..748b1b9a197e 100644 --- a/lib/scatterlist.c +++ b/lib/scatterlist.c @@ -243,6 +243,59 @@ void __sg_free_table(struct sg_table *table, unsigned int max_ents, EXPORT_SYMBOL(__sg_free_table); /** + * sg_trim_table - Free unused sg table entries at the end of the table + * @table: The sg table header to use + * + * Description: + * In cases where it is hard to know to minimum numbers of ents table will + * need to hold at sg_alloc_time, and we end up with a table with unused sg + * entries at its end, this function will trim (free) the unused sg entry + * blocks and adjust the table->orig_nents down. + * + **/ +void sg_trim_table(struct sg_table *table) +{ + struct scatterlist *sgl, *next, *prev = NULL; + unsigned int seen_nents = 0; + const unsigned int max_ents = SG_MAX_SINGLE_ALLOC; + + sgl = table->sgl; + while (sgl) { + unsigned int alloc_size = table->orig_nents; + unsigned int sg_size; + + /* + * If we have more than max_ents segments left, + * then assign 'next' to the sg table after the current one. + * sg_size is then one less than alloc size, since the last + * element is the chain pointer. + */ + if (alloc_size > max_ents) { + next = sg_chain_ptr(&sgl[max_ents - 1]); + alloc_size = max_ents; + sg_size = alloc_size - 1; + } else { + sg_size = alloc_size; + next = NULL; + } + + if (seen_nents >= table->nents) { + if (prev) + sg_mark_end(prev); + prev = NULL; + table->orig_nents -= sg_size; + sg_kfree(sgl, alloc_size); + } else { + prev = sgl; + } + + seen_nents += sg_size; + sgl = next; + } +} +EXPORT_SYMBOL(sg_trim_table); + +/** * sg_free_table - Free a previously allocated sg table * @table: The mapped sg table header *