From patchwork Mon Oct 16 05:34:04 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kartik X-Patchwork-Id: 13422498 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id BD847CDB465 for ; Mon, 16 Oct 2023 05:34:35 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 588138D0040; Mon, 16 Oct 2023 01:34:35 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 538278D0001; Mon, 16 Oct 2023 01:34:35 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 3D9698D0040; Mon, 16 Oct 2023 01:34:35 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id 2AEFD8D0001 for ; Mon, 16 Oct 2023 01:34:35 -0400 (EDT) Received: from smtpin20.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id 030B9C0A99 for ; Mon, 16 Oct 2023 05:34:34 +0000 (UTC) X-FDA: 81350209710.20.2AF4D8B Received: from NAM12-BN8-obe.outbound.protection.outlook.com (mail-bn8nam12on2065.outbound.protection.outlook.com [40.107.237.65]) by imf08.hostedemail.com (Postfix) with ESMTP id F0B7216000B for ; Mon, 16 Oct 2023 05:34:31 +0000 (UTC) Authentication-Results: imf08.hostedemail.com; dkim=pass header.d=Nvidia.com header.s=selector2 header.b=bzs6M8Zm; spf=pass (imf08.hostedemail.com: domain of kkartik@nvidia.com designates 40.107.237.65 as permitted sender) smtp.mailfrom=kkartik@nvidia.com; arc=pass ("microsoft.com:s=arcselector9901:i=1"); dmarc=pass (policy=reject) header.from=nvidia.com ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1697434472; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=kEdGy8EGffjLOVahWgd5/Q4+67B31sO+LdNTQ+9iJ4E=; b=cj1FL9bY1+E5VkSXdHtmUFBFoFzAO04Ti2E6Y68ZqkHkisjGKL3L+iNRElV4X8wj197utK IIJ4SqRxddviBmlQtM5czChCAEO3+6VzLycMoC7r3vnJdyOg1GZYgDnuZxrGoFC9yPi7bE gWgLt/nEr+Um/B6Tgvywvxrw/xuGN5E= ARC-Authentication-Results: i=2; imf08.hostedemail.com; dkim=pass header.d=Nvidia.com header.s=selector2 header.b=bzs6M8Zm; spf=pass (imf08.hostedemail.com: domain of kkartik@nvidia.com designates 40.107.237.65 as permitted sender) smtp.mailfrom=kkartik@nvidia.com; arc=pass ("microsoft.com:s=arcselector9901:i=1"); dmarc=pass (policy=reject) header.from=nvidia.com ARC-Seal: i=2; s=arc-20220608; d=hostedemail.com; t=1697434472; a=rsa-sha256; cv=pass; b=cmab8lU3kw6aLpNYnKo9CHURfAuhRArA68yImZUmyUgQET8DGDjZjROwjs8+H3Eyin9AcZ 9PHiDOj0LYa5ZrPiDA3VoEms3H+QX6RQt6COwz5BwU9rJI1ewfbFLz13P0b2muF3V+18d4 2VzfhahMS+nSQbka3lXpdeDWuNHuxNM= ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=U1Q7QKa3ruGA4rmvraQ3lEADNcz4L4ojGZBMA+o+rPKjDiP9Iu0WzfePYKvtJiwiscxIFu5ifauKh0H/wBURCpFfs0upOHQaijoZiqavPRGDRYrRc2CyFlnTGyYAzutzGLrcxqAGM+yzlYyLM0XrLpu2WMKCtyA4O5oWBLdK6v58qKp/P+xEluugswuaSP5U0JhsKrZVgKXKevkFVpq8+LmgZYeFYrHA3PfEIJX5WaQbKEjUujXSCIBoASymquAlWoVL4MRLe30nqMefYa30wGvkqCercKFFf0s+jPDdcz0dhMgNkqbi2pyOPBhdeqjzLRzKL9JJRobQjIC8PClNbQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=kEdGy8EGffjLOVahWgd5/Q4+67B31sO+LdNTQ+9iJ4E=; b=jOAv6Sa/y6ZcR6GtwSFoK7BDPpveDL4sMoTLRa2nt9Oipm+MgNG5uXn1785zE3PBjo/ud2oLkUGlEnAalvJe590jw3flt61qnFvl7aGVLpeWzFUZMZP4HQJ9UyvlCjzRl/f1A5GIlhc3FQwO+eaaqCtwGhWukpMbgckrr6Gyto82Yb5lwVlNJd89DKOQPEHRmY02GSNRIyHBjtCU1J/US+WJXCW+kpof3DufvNi0Y1gW8TMfg2HFDUv0uclXLd5iRPrSLPkfyM7xasX2KkdSNduxiMvKNd/PKk0g2uHwgZ+bqwQAuIIG3/lqK1i7xYBHjm5z/C0y1S0TNJli8JnzTg== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 216.228.118.232) smtp.rcpttodomain=gmail.com smtp.mailfrom=nvidia.com; dmarc=pass (p=reject sp=reject pct=100) action=none header.from=nvidia.com; dkim=none (message not signed); arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=Nvidia.com; s=selector2; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=kEdGy8EGffjLOVahWgd5/Q4+67B31sO+LdNTQ+9iJ4E=; b=bzs6M8ZmSTXtpaf2Dbn2lIBvo4bUQnkhXy5C1y/l8bnyhhenWd/VSA957oQSDpK9Yl+Q5m482dhTwEPcFu3UeBu8fet20vgwo8DwnJuaUssBzRsR91DLaz0rrFWJLmVmMfGqYbpu1LfCp5h7fi7cv2peur5p40cEeybTXBtq5yJtjICkV3sV9bg6KWM/a0HjPbbfZvW+iNkiDPKgYtIR6RPXd3wfbSgdGIP0PwLYPuBjfZxjmhAOQNMgAEh+sFJunfBnRaJss2LO69jrdUx2ibHtyPBTXgdFfLb5MJwFbthEholgvxH5TEzOafHBJUpyxn+gxdqMij+W6AsNKEPDfw== Received: from SA0PR11CA0079.namprd11.prod.outlook.com (2603:10b6:806:d2::24) by DM3PR12MB9436.namprd12.prod.outlook.com (2603:10b6:8:1af::20) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6886.34; Mon, 16 Oct 2023 05:34:27 +0000 Received: from SA2PEPF00001507.namprd04.prod.outlook.com (2603:10b6:806:d2:cafe::79) by SA0PR11CA0079.outlook.office365.com (2603:10b6:806:d2::24) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6886.35 via Frontend Transport; Mon, 16 Oct 2023 05:34:27 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 216.228.118.232) smtp.mailfrom=nvidia.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=nvidia.com; Received-SPF: Pass (protection.outlook.com: domain of nvidia.com designates 216.228.118.232 as permitted sender) receiver=protection.outlook.com; client-ip=216.228.118.232; helo=mail.nvidia.com; pr=C Received: from mail.nvidia.com (216.228.118.232) by SA2PEPF00001507.mail.protection.outlook.com (10.167.242.39) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6838.22 via Frontend Transport; Mon, 16 Oct 2023 05:34:27 +0000 Received: from drhqmail203.nvidia.com (10.126.190.182) by mail.nvidia.com (10.127.129.5) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.986.41; Sun, 15 Oct 2023 22:34:26 -0700 Received: from drhqmail203.nvidia.com (10.126.190.182) by drhqmail203.nvidia.com (10.126.190.182) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.986.41; Sun, 15 Oct 2023 22:34:25 -0700 Received: from localhost.localdomain (10.127.8.10) by mail.nvidia.com (10.126.190.182) with Microsoft SMTP Server id 15.2.986.41 via Frontend Transport; Sun, 15 Oct 2023 22:34:21 -0700 From: Kartik To: , , , , , , , , , , , , , , , , , , Subject: [PATCH v5 1/8] mm/util: Introduce kmemdup_array() Date: Mon, 16 Oct 2023 11:04:04 +0530 Message-ID: <20231016053411.3380-2-kkartik@nvidia.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20231016053411.3380-1-kkartik@nvidia.com> References: <20231016053411.3380-1-kkartik@nvidia.com> MIME-Version: 1.0 X-NVConfidentiality: public X-NV-OnPremToCloud: ExternallySecured X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: SA2PEPF00001507:EE_|DM3PR12MB9436:EE_ X-MS-Office365-Filtering-Correlation-Id: 532511ea-9c7d-4680-11ab-08dbce099082 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: JIqjJSYk9cle5q0KrE6X1zbwBG4mHKYoAsBFKjjGY9LvyjaNaHf7jYGmx2rPwenjZilMZYZwqy3hPmJYZ+E1psL6VD0h6l9gc3NZ2nrkktG8Ma9ZDAGeZDamkcPfDPMr/Ggpf6+hSARsTWARpXoVbeUS4ZYnFndqKC+dDYVU1iy8T3WZmIJIe/KGHaeq+xWybOTCwuduHUBDbU7Hwd4wAQXQNMAi1Jh3ZN6ESlb5z/l+BAOF1a8v8xFLURYSLaEEqhYP4iYJd2L23QqxJCyyia8n//0NCulUU1jdARDHlSP8PHecWcEekasLdDCIAc5OCxwQX3X4oKX2liLnxrqkJJGjGO1vkFdlLbSk8PbAJ8U2xXj63KL0ZAhMc0qlkIZnBPQ6ScSxbzie8asew6eSgxXS2wIIKCP0jtQnG7ypfVzlRRkWkR4rpw/yeNpMIjsPW9uMavtu+3xyDZJTTIoM/SYfAFbzds6gIJphH+3ilTxJiuHZe9weKnv9nr1dmC/AwhP/1HWsEPJ9H9RWX9PNyrgN8qfwxffmWUm/Yvh0PpJ3iQzT9PL5l7J2wV2P6rkZwyrYgV6zjexNNsZRmsEy/muknAWG0SFRRecNhT+9M3CWF0eJ6uLzIAUEITlXNMpzCGl3AE8LMuM7UKTgE6N8hFRx3YoKtIJCltFS/XnJ6/yWM3v0xBuQSILXqSseYzcRvrSg0GJp1VzubKvX0vmrByfn6e/VzuYebmzM/IQGISwo5A3w/lHGAeNwzykHl5Kw0Jw1suRn0AB3Hl867d2YTA== X-Forefront-Antispam-Report: CIP:216.228.118.232;CTRY:US;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:mail.nvidia.com;PTR:dc7edge1.nvidia.com;CAT:NONE;SFS:(13230031)(4636009)(396003)(39850400004)(346002)(376002)(136003)(230922051799003)(1800799009)(64100799003)(186009)(82310400011)(451199024)(36840700001)(46966006)(478600001)(426003)(70206006)(70586007)(316002)(110136005)(1076003)(2616005)(336012)(26005)(6666004)(8676002)(8936002)(5660300002)(41300700001)(7416002)(2906002)(86362001)(36756003)(7636003)(921005)(356005)(82740400003)(36860700001)(47076005)(40480700001)(2101003)(83996005);DIR:OUT;SFP:1101; X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 16 Oct 2023 05:34:27.6121 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 532511ea-9c7d-4680-11ab-08dbce099082 X-MS-Exchange-CrossTenant-Id: 43083d15-7273-40c1-b7db-39efd9ccc17a X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=43083d15-7273-40c1-b7db-39efd9ccc17a;Ip=[216.228.118.232];Helo=[mail.nvidia.com] X-MS-Exchange-CrossTenant-AuthSource: SA2PEPF00001507.namprd04.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: DM3PR12MB9436 X-Rspamd-Queue-Id: F0B7216000B X-Rspam-User: X-Stat-Signature: 9gdecebi3bb6ute8h6n4xhynxahrkmce X-Rspamd-Server: rspam01 X-HE-Tag: 1697434471-39012 X-HE-Meta: U2FsdGVkX1/veMN2YXwYEGX/xgze1JCIIlbTTFWmSrY20d1lOsFUZa+8wmlNTe7dv8nAeH92aEUmulTDDoyHHdn41MRavNKSgUd48aA9nENENTuWiCPCBDzb+JtsF/W9XN3HHGZ4ggc+xUog8XHQVPlJBSsh4YoLCMRIWRRiWLSoXnZYR0VjXTjUzjYgtNBIiCpEYi0BMCyajF2ylk4ODDqVTnqM3x9de3rCQD71nsFYxjooZB9T6V4gqAcrF0VnGROfzMwLnJqV12QAXDWlnDess7JqNOswhe2oRLkwXu/u8iyJX/JxuIzLNc8Pz7NNkjokkMqb8B+xiQ63tJnLU+fDQ/s+trBCE6x7M/a9KpUyYbFceFW/nTldH5DmhBXs3075WkNuBzKUdrzvQebHSNlj1wnTeOoG1yYKwzkoO9VnjTPe6T/66hqIMzRPAJt/mxNzGqTOxMSKGK0pgyLNNmuUshC+26BkiRMMtFcRCul9P4h+yL04nlCxgd4s3K0rrbTt2VtiPzuAcq3434NgQIrTResbhC6kaVRKl+Q0H7RssaO8puVLod4z5TR2VmXYRUWA2a17xUObxz8ioPhyuWuiw7tVfbfImn08i8onBIZsmqU+Mrq7YVgqKl7/xAIDTgJdnFoXWiSDx7+/g6SDwb9BxeQVpqIebGQCkibmbxCz94/JYsvbavO4pd9qzpTjK/zqON8Ih6umZjOxty8YlhNAiLIGrH1RQTRhfh70+7ZINpmgeOsY6FdZRmdS1gIDWpjm+YVltalWZOfXNDtccIQbEi1JpiHqQLMkZ71mqlTuXpCNA392jDeZfl3nw5niVTHAsbtp/wwCVOhMfedMc9/m+jSr28dGL1S8bttn5Mj0eeVd5+kdlN8Yz6tVNw3lpu+iTc3O4LFB6dqicTSgkihmGJTJOkqM3Va5CL/8+5LLSymyeBKzQ1Y0b2zE8BpRNrLzG7Yz0B9YXSpu06B pbqBjQs3 eCnDLXVtSTrg2hTdTmkU5PY/y3skpa0AwyCs5yZHg4daZdYn/Ggd9WE1YjeKES5+lA+qCHwz04gQhJzdeaZE0PxbJfbNdXnUhgIqw/8k1BCAroJCFUoPl6JJYLUVasVOrewxpoD8OuGNzEIPZkEP2eLtQhxY2s1qUTYIHSH8w/2MIEzDxXCfl7kpWDtT9NjZo5DbtW7y5a7EkIngU/kpzC4071TQH/JLNJlOW4YZXwXpRF9+xyOJj+2L+nCKZB9A/yHcvu+y7l8nhf6We7cS6c5+YUC6S+XG61URFNIjUxioLO+grMaRD6chwyDCblgqYLjdloLK8/PSYQY49UDWw+51RgNnTfiTRkaLsCussPD1gRVe1olfbmUODHbzUA6R2j/X8 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: Introduce kmemdup_array() API to duplicate `n` number of elements from a given array. This internally uses kmemdup to allocate and duplicate the `src` array. Signed-off-by: Kartik Acked-by: Kees Cook --- v4 -> v5: * Simplify the implementation by just returning kmemdup(size_mul). --- include/linux/string.h | 1 + mm/util.c | 17 +++++++++++++++++ 2 files changed, 18 insertions(+) diff --git a/include/linux/string.h b/include/linux/string.h index 3c920b6d609b..fcfa1062c79b 100644 --- a/include/linux/string.h +++ b/include/linux/string.h @@ -180,6 +180,7 @@ extern char *kstrndup(const char *s, size_t len, gfp_t gfp); extern void *kmemdup(const void *src, size_t len, gfp_t gfp) __realloc_size(2); extern void *kvmemdup(const void *src, size_t len, gfp_t gfp) __realloc_size(2); extern char *kmemdup_nul(const char *s, size_t len, gfp_t gfp); +extern void *kmemdup_array(const void *src, size_t element_size, size_t count, gfp_t gfp); extern char **argv_split(gfp_t gfp, const char *str, int *argcp); extern void argv_free(char **argv); diff --git a/mm/util.c b/mm/util.c index 6eddd891198e..8801446140d6 100644 --- a/mm/util.c +++ b/mm/util.c @@ -135,6 +135,23 @@ void *kmemdup(const void *src, size_t len, gfp_t gfp) } EXPORT_SYMBOL(kmemdup); +/** + * kmemdup_array - duplicate a given array. + * + * @src: array to duplicate. + * @element_size: size of each element of array. + * @count: number of elements to duplicate from array. + * @gfp: GFP mask to use. + * + * Return: duplicated array of @src or %NULL in case of error, + * result is physically contiguous. Use kfree() to free. + */ +void *kmemdup_array(const void *src, size_t element_size, size_t count, gfp_t gfp) +{ + return kmemdup(src, size_mul(element_size, count), gfp); +} +EXPORT_SYMBOL(kmemdup_array); + /** * kvmemdup - duplicate region of memory *