From patchwork Mon Jun 1 16:15:25 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tom Lendacky X-Patchwork-Id: 6523801 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 6EB8AC0020 for ; Mon, 1 Jun 2015 16:30:47 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id 900C520460 for ; Mon, 1 Jun 2015 16:30:45 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 8DA7D2045E for ; Mon, 1 Jun 2015 16:30:44 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752259AbbFAQan (ORCPT ); Mon, 1 Jun 2015 12:30:43 -0400 Received: from mail-bl2on0101.outbound.protection.outlook.com ([65.55.169.101]:9488 "EHLO na01-bl2-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1751771AbbFAQam (ORCPT ); Mon, 1 Jun 2015 12:30:42 -0400 X-Greylist: delayed 889 seconds by postgrey-1.27 at vger.kernel.org; Mon, 01 Jun 2015 12:30:42 EDT Received: from BY2PR02CA0017.namprd02.prod.outlook.com (10.242.32.17) by BN1PR02MB072.namprd02.prod.outlook.com (10.242.211.26) with Microsoft SMTP Server (TLS) id 15.1.172.22; Mon, 1 Jun 2015 16:15:52 +0000 Received: from BN1BFFO11FD008.protection.gbl (2a01:111:f400:7c10::1:130) by BY2PR02CA0017.outlook.office365.com (2a01:111:e400:2c2a::17) with Microsoft SMTP Server (TLS) id 15.1.172.22 via Frontend Transport; Mon, 1 Jun 2015 16:15:51 +0000 Authentication-Results: spf=none (sender IP is 165.204.84.221) smtp.mailfrom=amd.com; davemloft.net; dkim=none (message not signed) header.d=none; Received-SPF: None (protection.outlook.com: amd.com does not designate permitted sender hosts) Received: from atltwp01.amd.com (165.204.84.221) by BN1BFFO11FD008.mail.protection.outlook.com (10.58.144.71) with Microsoft SMTP Server id 15.1.184.11 via Frontend Transport; Mon, 1 Jun 2015 16:15:50 +0000 X-WSS-ID: 0NP9X6C-07-QMT-02 X-M-MSG: Received: from satlvexedge01.amd.com (satlvexedge01.amd.com [10.177.96.28]) (using TLSv1 with cipher AES128-SHA (128/128 bits)) (No client certificate requested) by atltwp01.amd.com (Axway MailGate 5.3.1) with ESMTPS id 2CA5ECAE620; Mon, 1 Jun 2015 12:15:48 -0400 (EDT) Received: from SATLEXDAG03.amd.com (10.181.40.7) by satlvexedge01.amd.com (10.177.96.28) with Microsoft SMTP Server (TLS) id 14.3.195.1; Mon, 1 Jun 2015 11:16:13 -0500 Received: from tlendack-t1.amdoffice.net (10.180.168.240) by satlexdag03.amd.com (10.181.40.7) with Microsoft SMTP Server id 14.3.195.1; Mon, 1 Jun 2015 12:15:26 -0400 Subject: [PATCH v1 1/2] scatterlist: introduce sg_nents_for_len From: Tom Lendacky To: CC: David Miller , , Herbert Xu Date: Mon, 1 Jun 2015 11:15:25 -0500 Message-ID: <20150601161525.729.58208.stgit@tlendack-t1.amdoffice.net> In-Reply-To: <20150601161519.729.75909.stgit@tlendack-t1.amdoffice.net> References: <20150601161519.729.75909.stgit@tlendack-t1.amdoffice.net> User-Agent: StGit/0.17.1-dirty MIME-Version: 1.0 X-EOPAttributedMessage: 0 X-Microsoft-Exchange-Diagnostics: 1; BN1BFFO11FD008; 1:XTVMiTgKhk1piaH/QxxGqKhOCXJE/NU1hoPl24A5wkQIE0VadHnelO/TuejXSVxaFbN35bWA3gosxVIg+2YlZ/isnop3ICp9HS04DG/+qo8r39U3OsVzYT/f5Fp4qOHLDlM5yjnhJnky7AL53KfvD74mi0jG396jzo0YHz3ZfwxlLjqGrXTkLiBZfIRvLA36nSEpxnfmv6AnFE/F3TRPqV0LGz3ALxrktG4IWjKNYYn/PeBqU1obJ8i6joPwugGjOxwgWQXsWNb70Fm8lofaxrir3DcUU5InDlhPdEupyR2WNVDR+j1Mrqlyotc6VlaH X-Forefront-Antispam-Report: CIP:165.204.84.221; CTRY:US; IPV:NLI; EFV:NLI; SFV:NSPM; SFS:(10019020)(6009001)(428002)(50944005)(199003)(189002)(103116003)(110136002)(106466001)(19580405001)(229853001)(77156002)(62966003)(23676002)(92566002)(87936001)(575784001)(4001350100001)(4001540100001)(5001830100001)(5001860100001)(33646002)(189998001)(2950100001)(2351001)(68736005)(86362001)(97736004)(77096005)(76176999)(54356999)(105586002)(101416001)(53416004)(83506001)(46102003)(50466002)(97746001)(19580395003)(50986999)(64706001)(47776003)(71626006); DIR:OUT; SFP:1102; SCL:1; SRVR:BN1PR02MB072; H:atltwp01.amd.com; FPR:; SPF:None; PTR:InfoDomainNonexistent; A:1; MX:1; LANG:en; X-Microsoft-Antispam: UriScan:;BCL:0;PCL:0;RULEID:;SRVR:BN1PR02MB072; X-Microsoft-Antispam-PRVS: X-Exchange-Antispam-Report-Test: UriScan:; X-Exchange-Antispam-Report-CFA-Test: BCL:0; PCL:0; RULEID:(601004)(520003)(5005006)(3002001); SRVR:BN1PR02MB072; BCL:0; PCL:0; RULEID:; SRVR:BN1PR02MB072; X-Forefront-PRVS: 05947791E4 X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 01 Jun 2015 16:15:50.0606 (UTC) X-MS-Exchange-CrossTenant-Id: fde4dada-be84-483f-92cc-e026cbee8e96 X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=fde4dada-be84-483f-92cc-e026cbee8e96; Ip=[165.204.84.221]; Helo=[atltwp01.amd.com] X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: BN1PR02MB072 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.9 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_HI, T_RP_MATCHES_RCVD, 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 When performing a dma_map_sg() call, the number of sg entries to map is required. Using sg_nents to retrieve the number of sg entries will return the total number of entries in the sg list up to the entry marked as the end. If there happen to be unused entries in the list, these will still be counted. Some dma_map_sg() implementations will not handle the unused entries correctly (lib/swiotlb.c) and execute a BUG_ON. The sg_nents_for_len() function will traverse the sg list and return the number of entries required to satisfy the supplied length argument. This can then be supplied to the dma_map_sg() call to successfully map the sg. Signed-off-by: Tom Lendacky --- include/linux/scatterlist.h | 1 + lib/scatterlist.c | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 33 insertions(+) -- To unsubscribe from this list: send the line "unsubscribe linux-crypto" in the body of a message to majordomo@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html diff --git a/include/linux/scatterlist.h b/include/linux/scatterlist.h index ed8f9e7..a0edb99 100644 --- a/include/linux/scatterlist.h +++ b/include/linux/scatterlist.h @@ -221,6 +221,7 @@ static inline void *sg_virt(struct scatterlist *sg) } int sg_nents(struct scatterlist *sg); +int sg_nents_for_len(struct scatterlist *sg, u64 len); struct scatterlist *sg_next(struct scatterlist *); struct scatterlist *sg_last(struct scatterlist *s, unsigned int); void sg_init_table(struct scatterlist *, unsigned int); diff --git a/lib/scatterlist.c b/lib/scatterlist.c index c9f2e8c..99fbc2f 100644 --- a/lib/scatterlist.c +++ b/lib/scatterlist.c @@ -56,6 +56,38 @@ int sg_nents(struct scatterlist *sg) } EXPORT_SYMBOL(sg_nents); +/** + * sg_nents_for_len - return total count of entries in scatterlist + * needed to satisfy the supplied length + * @sg: The scatterlist + * @len: The total required length + * + * Description: + * Determines the number of entries in sg that are required to meet + * the supplied length, taking into acount chaining as well + * + * Returns: + * the number of sg entries needed, negative error on failure + * + **/ +int sg_nents_for_len(struct scatterlist *sg, u64 len) +{ + int nents; + u64 total; + + if (!len) + return 0; + + for (nents = 0, total = 0; sg; sg = sg_next(sg)) { + nents++; + total += sg->length; + if (total >= len) + return nents; + } + + return -EINVAL; +} +EXPORT_SYMBOL(sg_nents_for_len); /** * sg_last - return the last scatterlist entry in a list