From patchwork Mon Jun 27 14:54:07 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Franklin Cooper X-Patchwork-Id: 9200781 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 D99CD60752 for ; Mon, 27 Jun 2016 14:55:04 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id C91232857C for ; Mon, 27 Jun 2016 14:55:04 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id BC55728595; Mon, 27 Jun 2016 14:55:04 +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=ham 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 043402857C for ; Mon, 27 Jun 2016 14:55:04 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752092AbcF0OzC (ORCPT ); Mon, 27 Jun 2016 10:55:02 -0400 Received: from mail-oi0-f65.google.com ([209.85.218.65]:35608 "EHLO mail-oi0-f65.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751965AbcF0OyN (ORCPT ); Mon, 27 Jun 2016 10:54:13 -0400 Received: by mail-oi0-f65.google.com with SMTP id d132so30924648oig.2; Mon, 27 Jun 2016 07:54:13 -0700 (PDT) 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=YpUcZtn69EdkIErvmiTODl+ZoVRd3yc5dvQixwJvRXg=; b=Mc/2BaCFY6G3ijRo4Mp1IztWf7i1ItB5NgGA1Gsy/7jLolyR+eU3mb3xBwjQE2VNEe 49tFlb2CjWm4n0CWlfEkQtOi+ZHdKO+4eoMY+7lnRlupJnez9mXhTv4b4KDdq/JA+AdH ouX8ZHfXzE9ZAIxiGxis+hDCeIdIYEEt2UWhV+wgp9+Q0azMa5YlpkLvju7BftlxibVR v3aHObxCU0P5H0V7xU+vx1P9m30A8kXkcIya/HHMbb4PslLPGimmL61tEf6hxbSs471t 9VaERS6pKJf2ooN+UnS809CwMIUFOL7/PPhdAS4bWNWez43/8zGkzbu+zu2gZGCkFvgE bx+w== X-Gm-Message-State: ALyK8tLHNrO9ObunAO8kSBTPEncYZVUTv9F9hL4NHnbMXIM2N+0Ofjb3xjghIHgiBqE1Fw== X-Received: by 10.157.47.189 with SMTP id r58mr837873otb.163.1467039252524; Mon, 27 Jun 2016 07:54:12 -0700 (PDT) Received: from beast-server.fios-router.home ([108.19.207.229]) by smtp.gmail.com with ESMTPSA id h35sm9017182otb.31.2016.06.27.07.54.11 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Mon, 27 Jun 2016 07:54:11 -0700 (PDT) From: Franklin S Cooper Jr To: david.s.gordon@intel.com, axboe@fb.com, akpm@linux-foundation.org, ming.l@ssi.samsung.com, linux-kernel@vger.kernel.org, broonie@kernel.org, linux-spi@vger.kernel.org, nsekhar@ti.com, peter.ujfalusi@ti.com Cc: Franklin S Cooper Jr Subject: [RFC] [PATCH v2 1/3] scatterlist: Add support to clone scatterlist Date: Mon, 27 Jun 2016 09:54:07 -0500 Message-Id: <1467039249-7816-2-git-send-email-fcooper@ti.com> X-Mailer: git-send-email 2.7.0 In-Reply-To: <1467039249-7816-1-git-send-email-fcooper@ti.com> References: <1467039249-7816-1-git-send-email-fcooper@ti.com> 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 chained S/G list while maintaining the original list. This function will duplicate the passed in chained S/G list 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. Reducing the length can result in less entries of the chained S/G list being created. Signed-off-by: Franklin S Cooper Jr --- include/linux/scatterlist.h | 2 + lib/scatterlist.c | 103 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 105 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..b15f648 100644 --- a/lib/scatterlist.c +++ b/lib/scatterlist.c @@ -180,6 +180,109 @@ 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; + bool last_entry; + 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)) { + + last_entry = sg_is_last(sgl); + *sgl = *orig_sgl; + + /* + * If page_link is pointing to a chained sgl then set it to + * zero since we already compensated for chained sgl. If + * page_link is pointing to a page then clear bits 1 and 0. + */ + if (sg_is_chain(orig_sgl)) + sgl->page_link = 0x0; + else + sgl->page_link &= ~0x03; + + if (last_entry) { + sg_dma_len(sgl) = len; + /* Set bit 1 to indicate end of sgl */ + sgl->page_link |= 0x02; + } 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) {