From patchwork Mon Oct 16 10:14:29 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kartik X-Patchwork-Id: 13422914 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 A9244CDB465 for ; Mon, 16 Oct 2023 10:15:18 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 2D73D8D0055; Mon, 16 Oct 2023 06:15:18 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 268F98D0001; Mon, 16 Oct 2023 06:15:18 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 0651D8D0055; Mon, 16 Oct 2023 06:15:17 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by kanga.kvack.org (Postfix) with ESMTP id E17D78D0001 for ; Mon, 16 Oct 2023 06:15:17 -0400 (EDT) Received: from smtpin02.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id A28441CB69A for ; Mon, 16 Oct 2023 10:15:17 +0000 (UTC) X-FDA: 81350917074.02.2944E7B Received: from NAM02-SN1-obe.outbound.protection.outlook.com (mail-sn1nam02on2055.outbound.protection.outlook.com [40.107.96.55]) by imf28.hostedemail.com (Postfix) with ESMTP id 899A7C0015 for ; Mon, 16 Oct 2023 10:15:14 +0000 (UTC) Authentication-Results: imf28.hostedemail.com; dkim=pass header.d=Nvidia.com header.s=selector2 header.b=gJ3gKZ9y; arc=pass ("microsoft.com:s=arcselector9901:i=1"); dmarc=pass (policy=reject) header.from=nvidia.com; spf=pass (imf28.hostedemail.com: domain of kkartik@nvidia.com designates 40.107.96.55 as permitted sender) smtp.mailfrom=kkartik@nvidia.com ARC-Seal: i=2; s=arc-20220608; d=hostedemail.com; t=1697451314; a=rsa-sha256; cv=pass; b=JLeLPbA+Jjcih8EF3UhxiGrJ5awUWc05qGCx0OWumgLZef+a2b/DK51FMyg83LUVCuz9r+ chsKsB/ksbffVkKwggRuNEKaHxl4t6/DTYUkWPjsU+0vbO9A8IQsJcWWwUcCgcyAqMH/kN vp0XZqs/W+Gl4i1TlLM/PURQDMRY1xc= ARC-Authentication-Results: i=2; imf28.hostedemail.com; dkim=pass header.d=Nvidia.com header.s=selector2 header.b=gJ3gKZ9y; arc=pass ("microsoft.com:s=arcselector9901:i=1"); dmarc=pass (policy=reject) header.from=nvidia.com; spf=pass (imf28.hostedemail.com: domain of kkartik@nvidia.com designates 40.107.96.55 as permitted sender) smtp.mailfrom=kkartik@nvidia.com ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1697451314; 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=AYU7H1U8aq50icWa0PTdFQuUYRpJhAHnXqjhtqPrxnTs3SyceGbZ4TUJMsSqj1UOcIF1HL EQ1D77YkhsOjkkk4xgKhVLzA3/pMuu3Q7vJyCDyHBDd7ng8msBwS5IcesrEepqA1J7pQRK RYnkDuSNTzHc16CllRuj0vkdzU0VdRw= ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=brp1goxherUTvXjigjYHIXVe/3ts2rmHaoX/gUveGopy9xFxv7nobfZLH4DPhdKRhxWNdv6R+gybXdCGQ0AXxCgJ4CAuqxmWGnjaa9dhEmVYAHTrA+VCStl6yshM59uo8CEPfExs4riT3OimhHGamp6fnRvj+7YOgTtzGR8Qmp0dBnTRTRlko1y3jtuR8MbEbD23qwJbrdJNUbXjgeI1K5Mb6ve8SjhurVq2s1NnAzYf3nu/3MNJIDvoY6intWcXZOLLcR8ayFyiX4tnJ+lRIgHQjyxPxS8n0xyQDomJatjnt2NG1ApPmuFSVWgXerYFOA+POQ003XdNHobj4rYEqg== 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=m/CuS/nWXj5jZxTDQDSW+ZMO/7Ytk8NFIjzhDoBd4mEoWHaQ+IrBaXLBzrYO5v84OdCf9qpiKyx2aYFKITnOm9sJdWcjkhITq1g1JNE4G5gOdrB7CNRgSxuIDjyKAvBTfh3sh4Aw86lG6zanGRDwZQbdIm/gH0lCzuqXO46PCrA7jvpbET5Jy6aLG7tvElnpbH2EvHLxb174o4uXZN6bOTPTCNu5cNnqH5afqG0rZKnmmW3Qv/8Mo+OqFo1UqLU+tURJ8pl8sQad4RTGZjZbUG3ensx1jx+RUg9JSPI2xZ+SkD1fzZ9giEZKZsLMAF2pcY3VpLGj4P5thgEfitJQPQ== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 216.228.117.161) 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=gJ3gKZ9yYBTXIcovyvumvZiE+7OZNwlSS8Ooa1LmQ/yiLDNeeH5Fr4JED2/Uvk2u1PFRTALe6KCN4bLyVDz57G6h60dJghad5Hj9yXPKVCa3h5p/nx1KhUnJfwTWGYRP67qdonwW0j+BIO/ix8eTRVZCNvE2JaVgs4RDs1ZHpt3hXsiGYSDDueHftSYvkax8ch1tQvOW/08Jc7HuyfuasgcbSZXc3wtbRrUdfZihzcxq07yAphKxBbZ6EFAe0LN/USD+Uzkdd/oAgvRGEi6zBIhjnbHTXgAKIc9d62gVk/rLtwOR7pVvnsaRRruR/xDbf405lLJKn96ZOvkmTXHCoA== Received: from PH8PR20CA0004.namprd20.prod.outlook.com (2603:10b6:510:23c::12) by MW4PR12MB8610.namprd12.prod.outlook.com (2603:10b6:303:1ef::18) 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 10:15:11 +0000 Received: from SA2PEPF000015C9.namprd03.prod.outlook.com (2603:10b6:510:23c:cafe::26) by PH8PR20CA0004.outlook.office365.com (2603:10b6:510:23c::12) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6863.47 via Frontend Transport; Mon, 16 Oct 2023 10:15:11 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 216.228.117.161) 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.117.161 as permitted sender) receiver=protection.outlook.com; client-ip=216.228.117.161; helo=mail.nvidia.com; pr=C Received: from mail.nvidia.com (216.228.117.161) by SA2PEPF000015C9.mail.protection.outlook.com (10.167.241.199) 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 10:15:10 +0000 Received: from rnnvmail202.nvidia.com (10.129.68.7) by mail.nvidia.com (10.129.200.67) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.986.41; Mon, 16 Oct 2023 03:14:54 -0700 Received: from rnnvmail202.nvidia.com (10.129.68.7) by rnnvmail202.nvidia.com (10.129.68.7) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.986.41; Mon, 16 Oct 2023 03:14:53 -0700 Received: from localhost.localdomain (10.127.8.10) by mail.nvidia.com (10.129.68.7) with Microsoft SMTP Server id 15.2.986.41 via Frontend Transport; Mon, 16 Oct 2023 03:14:48 -0700 From: Kartik To: , , , , , , , , , , , , , , , , , , Subject: [PATCH v6 1/8] mm/util: Introduce kmemdup_array() Date: Mon, 16 Oct 2023 15:44:29 +0530 Message-ID: <20231016101436.7146-2-kkartik@nvidia.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20231016101436.7146-1-kkartik@nvidia.com> References: <20231016101436.7146-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: SA2PEPF000015C9:EE_|MW4PR12MB8610:EE_ X-MS-Office365-Filtering-Correlation-Id: d45218c2-e495-45fb-af4e-08dbce30c7ec X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: FpagbLjOp2taghtHhDTj0N33hrJXhtUQHXamHhyNq4e82xMSq1rYNx/ABbVpmOdN9iyAhxYNAd0W/8cSP8WTIYpKXG2+o1+NdMoGfIzbQkwjEA5EoxR2d9owOb0btVpLwSkh8Fu/WwrRLIo8KbGFHKOl6EhQtrKC/S81j78EDXhM/anNghP5DT/eUxuOf0r+239KxNEGyulwWn/fNbGZvl9kjA7SDmpaIfJm3FSqBrFH2F35I1Z4spwb7k+MgBiVriCw8g4rg0BNpCbG7Ejyrb/JPQiNeS0M55gq4pHH6zkvazCEzsaibQq96BTBzzJ5vss447BnJq2pC1Q2QGIG199fcDrLOCJVIaTp/xuH2ThLaaTvzzlwpM0xl3iGGV+JH1tB97pyl65iyAWk4+16P8wHK26CHQS/d0jpaKK9LNM+jF9nkYh/tVHugcfblpDnv6LxV/mEOTin8ts3pk59NuwSHRVVSNy+xnuYlDW4768nxK0Ypiljb1ClS/5hPuSiJ1CkqcEBUIdm+ZyfjI3yGiSWzNvd22QmXSVbtVkhthYbBh7l+mAKg+iRLb3MSRfzpSiHW3vi/yoA5h0UnrB8wmbqvWvsyJ/EHnI+Un5FePrRyUd+8AaNt/DXd5au0lqPciI7Zw/AZBKYjkJSZea/Yd5bL8xT78fDqHKgxYfX8EFnABi4Plgp4eqd+FhPzW6IqUXHvzbSiWhQC6dxu0err7ETAsn1eYO882+v1j9J3uwMtZdR/fsqZAOaaja6dtLJedIhyFGi+j9mqoj4dPGuYN3Y8y0Dv+LoEs6wOORhSso4mPoutOUeCZhoBDjRTerG X-Forefront-Antispam-Report: CIP:216.228.117.161;CTRY:US;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:mail.nvidia.com;PTR:dc6edge2.nvidia.com;CAT:NONE;SFS:(13230031)(4636009)(396003)(376002)(136003)(346002)(39860400002)(230922051799003)(82310400011)(186009)(1800799009)(451199024)(64100799003)(46966006)(40470700004)(36840700001)(47076005)(36860700001)(26005)(40460700003)(82740400003)(2616005)(426003)(336012)(40480700001)(8936002)(7416002)(86362001)(2906002)(5660300002)(8676002)(6666004)(316002)(70586007)(70206006)(110136005)(36756003)(41300700001)(1076003)(921005)(356005)(7636003)(478600001)(83996005)(2101003);DIR:OUT;SFP:1101; X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 16 Oct 2023 10:15:10.9580 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: d45218c2-e495-45fb-af4e-08dbce30c7ec X-MS-Exchange-CrossTenant-Id: 43083d15-7273-40c1-b7db-39efd9ccc17a X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=43083d15-7273-40c1-b7db-39efd9ccc17a;Ip=[216.228.117.161];Helo=[mail.nvidia.com] X-MS-Exchange-CrossTenant-AuthSource: SA2PEPF000015C9.namprd03.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: MW4PR12MB8610 X-Rspam-User: X-Rspamd-Server: rspam06 X-Rspamd-Queue-Id: 899A7C0015 X-Stat-Signature: abkoc4ka8eq6sofg393zbps8au45eh56 X-HE-Tag: 1697451314-751009 X-HE-Meta: U2FsdGVkX19Xj5t5dtj2dsv+WRc0cZEYlJ9/q15bVfA6VclR5AOe1+c4WV9VfFZ5zK4QCsXDQvgdAjVdkMZMuB7AtdKWzOvxxg8wI92OQMyXImFosXlTdpg97zPVvgAHEJU6mLW9pGyTMl9i/QoZtS1QuzXmMVBMNyWamW2rGggVeSz2v5vhxS8ik7ojbSQSZTZQcfpb6KiCrVcAuFUea00itzCv4/xiGJeoLy+X2zXH9eVKXLGxS/5hklxZYZG5Mlw+ykVt5kCVUm43MC7gpf474FtgXEBOMn7ZxQqmon9z/luDwPKn+GrnmSSIB9i/V9IcbpeGBz3lSWsVPKcdkpNkz+KSwJkAoF1V/3/Ezol2BmHQVJlEkLA7mf12g0A/18iX/AYj4/ehd2VW6Ver6JipkZgwxDHX+tEq/39DCd0X//QujqdkEUsRZectNHQJZBwDdP8GHiBbKZcoQteLDHrTh2QEiukkeKe8hVteSSB3STvfaDEWUDjtfcSTtu/1N1xxdzX+GvnknjrnrLJ7tqUugD82tuLc9BoA2SzSv/eL3uvMwOJjLFzYFgZ2aiixiN+2X0K6UkR70NcsArGLRsyb6oyJ08tq2W5m8VjZK415FoBeQf1K30zdI2vA2/eAMU/zHy7jewYHuHBwAOhIZ3Ntm1YLdb7bTKaexhH6ZpM0BqBkbH03pjRlZIBOnW4SkmhNO9JBuDr++XW0fjMP9o55VDZtAS2EJYuXxO20yhLfRAQW3+RIIFZ/eBDFfFfHGSZOXk4BF0stE0fRU9Ma8TXPx11+p78VgIfFMBV2dy1FGufr+1h2QARyA3VgVozXJStuSUVQn66FSf/130gQwk890lq790vMwaDoae4QmyFp5fW7NCBkhxzmWWo2Fbu6CETSIAO9jDxi5Jzo3Amyo43Tk5+Qx+BkdMU/JNbS4brBbv+EpTwfFfKL2vG739mw2mc2oeFHHdp+HyzGcJ+ prUsk3/C HF0OpF/N3Nr0aL/IEYbz7Mu6gHunXLGpgBcUWFa/P1dORT7dCSUvixG6PYp8LIynkHI1OPgVUXKG3sYhUe9DSi20+4fE+Trpi272ScCz9/e2ztMiHKD0aiWTmJal9GEWmsDX8CWHMpy3xLZCseLgZpBrgFqYzjKId4PZ7PlY7vJ7aWaqRPqkxpbqg7ijveFT+8H0pw02XWMwHbQirhGQ6a1JGgcsf6qFFDhBd8zfgCdRxMIIALKzVO1of6S/ErYlE/gmXqh5zjc4ViG9qDxTHAEg60jB+qYkpsvvtxZFsuXDZgf6MXn9eiI6tFkCeWre7sx6xT6ae45NjmPZe0Gk6hh+sASmn+TSG/wZxLF2pynjyGdbjWvx5r4JvLiypnp2qrrlI X-Bogosity: Ham, tests=bogofilter, spamicity=0.000001, 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 --- 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 *