From patchwork Fri Jun 17 22:45:11 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Franklin Cooper X-Patchwork-Id: 9185101 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 990536075F for ; Fri, 17 Jun 2016 22:46:25 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 877D727EE2 for ; Fri, 17 Jun 2016 22:46:25 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 7C6E628236; Fri, 17 Jun 2016 22:46:25 +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=-6.9 required=2.0 tests=BAYES_00,RCVD_IN_DNSWL_HI autolearn=unavailable version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id C59A427EE2 for ; Fri, 17 Jun 2016 22:46:24 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1753618AbcFQWpk (ORCPT ); Fri, 17 Jun 2016 18:45:40 -0400 Received: from devils.ext.ti.com ([198.47.26.153]:50719 "EHLO devils.ext.ti.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751386AbcFQWph (ORCPT ); Fri, 17 Jun 2016 18:45:37 -0400 Received: from dlelxv90.itg.ti.com ([172.17.2.17]) by devils.ext.ti.com (8.13.7/8.13.7) with ESMTP id u5HMj0kq010128; Fri, 17 Jun 2016 17:45:00 -0500 Received: from DFLE73.ent.ti.com (dfle73.ent.ti.com [128.247.5.110]) by dlelxv90.itg.ti.com (8.14.3/8.13.8) with ESMTP id u5HMjXYs019022; Fri, 17 Jun 2016 17:45:33 -0500 Received: from dflp32.itg.ti.com (10.64.6.15) by DFLE73.ent.ti.com (128.247.5.110) with Microsoft SMTP Server id 14.3.294.0; Fri, 17 Jun 2016 17:45:33 -0500 Received: from dbdmail01.india.ti.com (dbdmail01.india.ti.com [172.24.162.206]) by dflp32.itg.ti.com (8.14.3/8.13.8) with ESMTP id u5HMjVXW014498; Fri, 17 Jun 2016 17:45:32 -0500 Received: from udb0273011.am.dhcp.ti.com (udb0273011.am.dhcp.ti.com [128.247.83.33]) by dbdmail01.india.ti.com (8.14.3/8.14.3/SuSE Linux 0.8) with ESMTP id u5HMjN1M006459; Sat, 18 Jun 2016 04:15:28 +0530 From: Franklin S Cooper Jr To: , , , , , , , CC: Franklin S Cooper Jr Subject: [RFC] [PATCH 1/3] lib/scatterlist: Add support to clone sg_table Date: Fri, 17 Jun 2016 17:45:11 -0500 Message-ID: <1466203513-4771-2-git-send-email-fcooper@ti.com> X-Mailer: git-send-email 1.9.1 In-Reply-To: <1466203513-4771-1-git-send-email-fcooper@ti.com> References: <1466203513-4771-1-git-send-email-fcooper@ti.com> MIME-Version: 1.0 Sender: linux-spi-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-spi@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP Occasionally there are times you need to tweak a S/G table while maintaining the original table. This function will duplicate the passed in S/G table along with its chained sgl and return a pointer to the cloned copy. The function also supports passing in a length that can be equal or less than the original chained S/G list length. This can reduce the length of the chained S/G list. Signed-off-by: Franklin S Cooper Jr --- include/linux/scatterlist.h | 2 + lib/scatterlist.c | 93 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 95 insertions(+) diff --git a/include/linux/scatterlist.h b/include/linux/scatterlist.h index cb3c8fe..9a109da 100644 --- a/include/linux/scatterlist.h +++ b/include/linux/scatterlist.h @@ -247,6 +247,8 @@ struct scatterlist *sg_next(struct scatterlist *); struct scatterlist *sg_last(struct scatterlist *s, unsigned int); void sg_init_table(struct scatterlist *, unsigned int); void sg_init_one(struct scatterlist *, const void *, unsigned int); +struct sg_table *sg_table_clone(struct sg_table *orig_table, u64 len, + gfp_t gfp_mask); int sg_split(struct scatterlist *in, const int in_mapped_nents, const off_t skip, const int nb_splits, const size_t *split_sizes, diff --git a/lib/scatterlist.c b/lib/scatterlist.c index 004fc70..15a0142 100644 --- a/lib/scatterlist.c +++ b/lib/scatterlist.c @@ -180,6 +180,99 @@ static struct scatterlist *sg_kmalloc(unsigned int nents, gfp_t gfp_mask) return kmalloc(nents * sizeof(struct scatterlist), gfp_mask); } +/* + * sg_clone - Duplicate an existing chained sgl + * @orig_sgl: Original sg list to be duplicated + * @len: Total length of sg while taking chaining into account + * @gfp_mask: GFP allocation mask + * + * Description: + * Clone a chained sgl. This cloned copy may be modified in some ways while + * keeping the original sgl in tact. Also allow the cloned copy to have + * a smaller length than the original which may reduce the sgl total + * sg entries. + * + * Returns: + * Pointer to new kmalloced sg list, ERR_PTR() on error + * + */ +static struct scatterlist *sg_clone(struct scatterlist *orig_sgl, u64 len, + gfp_t gfp_mask) +{ + unsigned int nents; + unsigned long page_link; + struct scatterlist *sgl, *head; + + nents = sg_nents_for_len(orig_sgl, len); + + if (nents < 0) + return ERR_PTR(-EINVAL); + + head = sg_kmalloc(nents, gfp_mask); + + if (!head) + return ERR_PTR(-ENOMEM); + + sgl = head; + + sg_init_table(sgl, nents); + + for (; sgl; orig_sgl = sg_next(orig_sgl), sgl = sg_next(sgl)) { + + page_link = sgl->page_link; + + *sgl = *orig_sgl; + + sgl->page_link = page_link; + + if (sg_is_last(sgl)) + sg_dma_len(sgl) = len; + else + len -= sg_dma_len(sgl); + } + + return head; +} + +/* + * sg_table_clone - Duplicate an existing sg_table including chained sgl + * @orig_table: Original sg_table to be duplicated + * @len: Total length of sg while taking chaining into account + * @gfp_mask: GFP allocation mask + * + * Description: + * Clone a sg_table along with chained sgl. This cloned copy may be + * modified in some ways while keeping the original table and sgl in tact. + * Also allow the cloned sgl copy to have a smaller length than the original + * which may reduce the sgl total sg entries. + * + * Returns: + * Pointer to new kmalloced sg_table, ERR_PTR() on error + * + */ +struct sg_table *sg_table_clone(struct sg_table *orig_table, u64 len, + gfp_t gfp_mask) +{ + struct sg_table *table; + + table = kmalloc(sizeof(struct sg_table),gfp_mask); + + if (!table) + return ERR_PTR(-ENOMEM); + + table->sgl = sg_clone(orig_table->sgl,len,gfp_mask); + + if (IS_ERR(table->sgl)) { + kfree(table); + return ERR_PTR(-ENOMEM); + } + + table->nents = table->orig_nents = sg_nents(table->sgl); + + return table; +} +EXPORT_SYMBOL(sg_table_clone); + static void sg_kfree(struct scatterlist *sg, unsigned int nents) { if (nents == SG_MAX_SINGLE_ALLOC) {