From patchwork Tue Mar 15 07:47:59 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "(Exiting) Baolin Wang" X-Patchwork-Id: 8585701 X-Patchwork-Delegate: herbert@gondor.apana.org.au Return-Path: X-Original-To: patchwork-linux-crypto@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork2.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.136]) by patchwork2.web.kernel.org (Postfix) with ESMTP id B243CC0553 for ; Tue, 15 Mar 2016 07:48:48 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id AF37C20165 for ; Tue, 15 Mar 2016 07:48:47 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 9B1642025B for ; Tue, 15 Mar 2016 07:48:46 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S934196AbcCOHsk (ORCPT ); Tue, 15 Mar 2016 03:48:40 -0400 Received: from mail-pf0-f170.google.com ([209.85.192.170]:34856 "EHLO mail-pf0-f170.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S934301AbcCOHsg (ORCPT ); Tue, 15 Mar 2016 03:48:36 -0400 Received: by mail-pf0-f170.google.com with SMTP id n5so17886988pfn.2 for ; Tue, 15 Mar 2016 00:48:35 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :in-reply-to:references; bh=YOEAgYuyypIqzZ+5NzsIytuIyY4/P8kcoF0mMfCv1is=; b=C7Z6lSMXfyJDABMpLYwVKFWIqu6X32w3XfaCs/Ogwh+/RV3b8r2n9rL+YOpSm1fPsu ghI4sQoUL8xKBgfOTO4gh0wYf1BoFKwyyHOlShOY4iMQjeLt/VhbM5PeDH9iihR6Bz3w 1Fh1sDpyArVSqS1MhlfC16OPglCGsAE3Je3NM= 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:in-reply-to:references; bh=YOEAgYuyypIqzZ+5NzsIytuIyY4/P8kcoF0mMfCv1is=; b=V+L73+szK6pIhblU8UA5Hd31WjSk1zJYR9a4qQuSt8ES5Pnyc/A3K2uxqTSytU3Vo7 hUBtMi0xC6IHI6gTEILobDjt6Nd1R6GaDNdmZzaKoRiMpf/R6wMG8QuwQerfZHFiAjM8 lSCe5X7JppQl0TY9+uzWxXnIPFLdPl3VRKN2CFCH4hNuNJJXUFe85M62cKlxGUpd2Hiv XAjRzCnJ87J6Fo2jEHReccW0W03Kb4p8U/uVhMrBh4qMeyUSx8xKrgeuy6c9M0UhCvGq DIrw9d7L25hjjxL/eCRXVmQpRtiiZUwyuEvgMcKgKDl9j+5zdWKU8eggcMGY/onsRjto 7HkQ== X-Gm-Message-State: AD7BkJIlCbq6Q/XG07DJjXsDUe73wJa9hXzA922BJJonbNKc0suF3S+gbgECFMm9crOCz/xq X-Received: by 10.98.70.27 with SMTP id t27mr36139401pfa.107.1458028113849; Tue, 15 Mar 2016 00:48:33 -0700 (PDT) Received: from baolinwangubtpc.spreadtrum.com ([175.111.195.49]) by smtp.gmail.com with ESMTPSA id 23sm37215530pfs.34.2016.03.15.00.48.27 (version=TLS1 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Tue, 15 Mar 2016 00:48:33 -0700 (PDT) From: Baolin Wang To: herbert@gondor.apana.org.au, davem@davemloft.net, agk@redhat.com, snitzer@redhat.com, axboe@fb.com, dm-devel@redhat.com Cc: akpm@linux-foundation.org, david.s.gordon@intel.com, thomas.lendacky@amd.com, robert.jarzmik@free.fr, yamada.masahiro@socionext.com, smueller@chronox.de, tadeusz.struk@intel.com, standby24x7@gmail.com, shli@kernel.org, broonie@kernel.org, linus.walleij@linaro.org, arnd@arndb.de, baolin.wang@linaro.org, linux-kernel@vger.kernel.org, linux-crypto@vger.kernel.org, linux-raid@vger.kernel.org Subject: [PATCH v2 1/4] scatterlist: Introduce some helper functions Date: Tue, 15 Mar 2016 15:47:59 +0800 Message-Id: <9442060edaca12f520bcbb76545ad33a52346e58.1458023698.git.baolin.wang@linaro.org> X-Mailer: git-send-email 1.7.9.5 In-Reply-To: References: In-Reply-To: References: Sender: linux-crypto-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-crypto@vger.kernel.org X-Spam-Status: No, score=-6.8 required=5.0 tests=BAYES_00,DKIM_SIGNED, RCVD_IN_DNSWL_HI,RP_MATCHES_RCVD,T_DKIM_INVALID,UNPARSEABLE_RELAY autolearn=unavailable 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 In crypto engine framework, one request can combine (copy) other requests' scatterlists into its dynamic sg table to manage them together as a bulk block , which can improve engine efficency with handling bulk block. Thus we need some helper functions to manage dynamic scattertables. This patch introduces 'sg_is_contiguous()' function to check if two scatterlists are contiguous, 'sg_alloc_empty_table()' function to allocate one empty dynamic sg table, 'sg_add_sg_to_table()' function to copy one mapped scatterlist into sg table and 'sg_table_is_empty' function to check if the sg table is empty. Signed-off-by: Baolin Wang --- include/linux/scatterlist.h | 33 +++++++++++++++++++++ lib/scatterlist.c | 69 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 102 insertions(+) diff --git a/include/linux/scatterlist.h b/include/linux/scatterlist.h index 556ec1e..c1ed9f4 100644 --- a/include/linux/scatterlist.h +++ b/include/linux/scatterlist.h @@ -212,6 +212,20 @@ static inline void sg_unmark_end(struct scatterlist *sg) } /** + * sg_table_is_empty - Check if the sg table is empty + * @sgt: sg table + * + * Description: + * The 'orig_nents' member of one sg table is used to indicate how many + * scatterlists in the sg table. + * + **/ +static inline bool sg_table_is_empty(struct sg_table *sgt) +{ + return !sgt->orig_nents; +} + +/** * sg_phys - Return physical address of an sg entry * @sg: SG entry * @@ -241,6 +255,23 @@ static inline void *sg_virt(struct scatterlist *sg) return page_address(sg_page(sg)) + sg->offset; } +/** + * sg_is_contiguous - Check if the scatterlists are contiguous + * @sga: SG entry + * @sgb: SG entry + * + * Description: + * If the sga scatterlist is contiguous with the sgb scatterlist, + * that means they can be merged together. + * + **/ +static inline bool sg_is_contiguous(struct scatterlist *sga, + struct scatterlist *sgb) +{ + return *(unsigned long *)sg_virt(sga) + sga->length == + *(unsigned long *)sg_virt(sgb); +} + int sg_nents(struct scatterlist *sg); int sg_nents_for_len(struct scatterlist *sg, u64 len); struct scatterlist *sg_next(struct scatterlist *); @@ -261,6 +292,8 @@ void sg_free_table(struct sg_table *); int __sg_alloc_table(struct sg_table *, unsigned int, unsigned int, struct scatterlist *, gfp_t, sg_alloc_fn *); int sg_alloc_table(struct sg_table *, unsigned int, gfp_t); +int sg_alloc_empty_table(struct sg_table *, unsigned int, gfp_t); +int sg_add_sg_to_table(struct sg_table *, struct scatterlist *); int sg_alloc_table_from_pages(struct sg_table *sgt, struct page **pages, unsigned int n_pages, unsigned long offset, unsigned long size, diff --git a/lib/scatterlist.c b/lib/scatterlist.c index 004fc70..6d3f3b0 100644 --- a/lib/scatterlist.c +++ b/lib/scatterlist.c @@ -370,6 +370,75 @@ int sg_alloc_table(struct sg_table *table, unsigned int nents, gfp_t gfp_mask) EXPORT_SYMBOL(sg_alloc_table); /** + * sg_add_sg_to_table - Add one scatterlist into sg table + * @sgt: The sg table header to use + * @src: The sg need to be added into sg table + * + * Description: + * The 'nents' member indicates how many mapped scatterlists has been added + * in the dynamic sg table. The 'orig_nents' member indicates the size of the + * dynamic sg table. + * + * Copy one mapped @src@ scatterlist into the dynamic sg table and increase + * 'nents' member. + * + **/ +int sg_add_sg_to_table(struct sg_table *sgt, struct scatterlist *src) +{ + unsigned int i = 0, orig_nents = sgt->orig_nents; + struct scatterlist *sgl = sgt->sgl; + struct scatterlist *sg; + + /* Check if there are enough space for the new sg to be added */ + if (sgt->nents >= sgt->orig_nents) + return -EINVAL; + + for_each_sg(sgl, sg, orig_nents, i) { + if (sgt->nents > 0 && i == (sgt->nents - 1)) { + sg_unmark_end(sg); + } else if (i == sgt->nents) { + memcpy(sg, src, sizeof(struct scatterlist)); + sg_mark_end(sg); + sgt->nents++; + break; + } + } + + return 0; +} + +/** + * sg_alloc_empty_table - Allocate one empty dynamic sg table + * @sgt: The sg table header to use + * @nents: Number of entries in sg list + * @gfp_mask: GFP allocation mask + * + * Description: + * Allocate and initialize one dynamic sg table. One dynamic sg table means + * it need allocate @nents@ empty scatterlists entries and is used to copy + * other mapped scatterlists into the dynamic sg table. + * + * The 'nents' member indicates how many scatterlists has been copied into + * the dynamic sg table. It should set 0 which means there are no mapped + * scatterlists added in this sg table now. + * + * The 'orig_nents' member indicates the size of the dynamic sg table. + * + **/ +int sg_alloc_empty_table(struct sg_table *sgt, unsigned int nents, + gfp_t gfp_mask) +{ + int ret; + + ret = sg_alloc_table(sgt, nents, gfp_mask); + if (ret) + return ret; + + sgt->nents = 0; + return 0; +} + +/** * sg_alloc_table_from_pages - Allocate and initialize an sg table from * an array of pages * @sgt: The sg table header to use