Received: by 2002:a05:6358:9144:b0:117:f937:c515 with SMTP id r4csp178075rwr; Thu, 4 May 2023 00:56:55 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ6mj6O8Lm3ucqcG1sayKRwNi3WdXRi252On0a5Paxa8klde28Frptiz8QBqV6d+Cy/3vZg4 X-Received: by 2002:a05:6a20:1442:b0:f3:b7:b10a with SMTP id a2-20020a056a20144200b000f300b7b10amr1743199pzi.15.1683187015563; Thu, 04 May 2023 00:56:55 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1683187015; cv=pass; d=google.com; s=arc-20160816; b=cK4upjrZ9sNvKZrKFOPDrDDw6Gnw0SsPq1vfcwlsOliISrx0nVWbmxbSUekzM2U1OX sIabH3zhWvKyg+JeB+ZlcSLy8JCyukjQXmzDLe/o/pfQZMwwPRjZEbUJ5xNez2brE8uB ymJNBzT06iaQBtbKK3VErVYvHjgPrw3IB+IrvTEtp7wbR8ZIZaGyQCIzSgGXfEeWUGy2 ef/FN1du8scmLDYOuCMMGvH3NXZt2eEKqTBZ/RVPpIYVaageTy+11hLkwUtATgeY5Xct 3Md42ckwEvPKJHMeReLZMJF/yLxjAH0R2UqCDb1rI1chLoR3YbEyMWzDEUL4mRRDgKfW ytmA== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:mime-version:content-transfer-encoding :message-id:date:subject:cc:to:from:dkim-signature; bh=mRz1ZethrgtzQMZOoNEZw2O+Tk1vjAqmjeHRBSBSOzk=; b=d3t4Z70x0ddP5KzVgI0poYdd6naxDMnZBjKQmH273PY0SOIlcWm+grDxk+8fjC4Bn3 zU0wN5ri6Pl3j0N50Zxv9F9htR+/4PAxgRjBidPujE1avtDMcXIRMAhNOZ6FeNP29FD8 AYrpTG6YT1Cxh2vFLPenN5Gh6bswiWUTHMeDNjX0OQqgVBVRGgJ/PE2EvBmLf5jdU3xI d4RJosINVFIFhbxUBcPKe0aYRuxR+Ml+acM5aviHTdIKykfpw0kcFlJAxphP5YwEHLNZ X5rZd/wiksvQUqCJa7tZGqRkpLvyXwUaR3mO98MMLD1H1+MBt61jVbGBRqtSideGkNo3 h0DA== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@NXP1.onmicrosoft.com header.s=selector2-NXP1-onmicrosoft-com header.b=VdGsDh8t; arc=pass (i=1 spf=pass spfdomain=oss.nxp.com dkim=pass dkdomain=oss.nxp.com dmarc=pass fromdomain=oss.nxp.com); spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=nxp.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id x24-20020a63db58000000b0051a8a22a42dsi35029685pgi.268.2023.05.04.00.56.41; Thu, 04 May 2023 00:56:55 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@NXP1.onmicrosoft.com header.s=selector2-NXP1-onmicrosoft-com header.b=VdGsDh8t; arc=pass (i=1 spf=pass spfdomain=oss.nxp.com dkim=pass dkdomain=oss.nxp.com dmarc=pass fromdomain=oss.nxp.com); spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=nxp.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229622AbjEDHxF (ORCPT + 99 others); Thu, 4 May 2023 03:53:05 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37318 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229522AbjEDHxB (ORCPT ); Thu, 4 May 2023 03:53:01 -0400 Received: from EUR02-DB5-obe.outbound.protection.outlook.com (mail-db5eur02on2084.outbound.protection.outlook.com [40.107.249.84]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 525B92728 for ; Thu, 4 May 2023 00:52:59 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=JSuPA7cYwZ/iMF/P/Q9FHfUrkPSelPMFnFRCeu06+zdn+gIx5GV0Fw4tnbENnotlpdXyEMoIoOWyLNE8NN9642/z3OqR66Qw3irAvSb8OBb7Dt6uwr/E7Q7RrbJ05vW6iQRLNqgND7458muA0uV0/q63FYzQ5vy0nUeqPGWqQRGjYz8GxaTV21i4rcxuUm+h+tqk5bpHRqDTbq9NCOluXyJuBouCsVwnp91WvnHN3JwG0HCw2Fc846TXtvYc8L/pCjskWqFFgh5N3/skNx0obXCmmr3VGErr7wBwzBMDg408fVITFzHr+lCDWd2b1RuATL7Db28Ts2CpTCVZi+21ng== 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=mRz1ZethrgtzQMZOoNEZw2O+Tk1vjAqmjeHRBSBSOzk=; b=HGyzLHlryjNKc1noCMsh7iJekxWqsGZZGWxXwWM4eQw05d2wp8LnM6P24BtEKn1JFoGpHUbhwtXFlJBT1nnq5mOyqnDABHK3s6PmSAtYH+pa4XLhJOHC7jePtVI3BT602/imdrPXn/mtH1UksBcDMok1Ho7rFTt1mTvZDW7mY2+k4JkKc2DIsZ0Ps6LskKhEXicJpzura6qL+i4+3x5CUPM/7X0qrPze8V5rSJT7PbYiZAVIZdXmLNzBfMmgBvyFCmISTHfu4zQK1XcmGcRS7VQ3LQrR8N7pftNw/zZZqRogNt7kJGKWzhfxQ75qDXNEykKvMrOM3Bcl0NbEOl7DJg== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=oss.nxp.com; dmarc=pass action=none header.from=oss.nxp.com; dkim=pass header.d=oss.nxp.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=NXP1.onmicrosoft.com; s=selector2-NXP1-onmicrosoft-com; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=mRz1ZethrgtzQMZOoNEZw2O+Tk1vjAqmjeHRBSBSOzk=; b=VdGsDh8tz9LQX7GaRMzPepQekdpV3EAwMnCpmTWuKdpsp55svASF1I8fkvJKEYntodwFNN++NS3xnVZHUHqh9jmSqo4CGNUJAvcmwpPotMcI02XcUW8Gm7b8M/S/K6mqQ60MB41gd9cl4Kr5MpCyaKniUI7gR0xe7sg25rWG70g= Authentication-Results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=oss.nxp.com; Received: from DU0PR04MB9417.eurprd04.prod.outlook.com (2603:10a6:10:358::11) by AM9PR04MB8273.eurprd04.prod.outlook.com (2603:10a6:20b:3e5::23) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6363.26; Thu, 4 May 2023 07:52:55 +0000 Received: from DU0PR04MB9417.eurprd04.prod.outlook.com ([fe80::b999:f2c6:a8cc:7b4]) by DU0PR04MB9417.eurprd04.prod.outlook.com ([fe80::b999:f2c6:a8cc:7b4%5]) with mapi id 15.20.6340.031; Thu, 4 May 2023 07:52:55 +0000 From: "Peng Fan (OSS)" To: gregkh@linuxfoundation.org, robh+dt@kernel.org, krzysztof.kozlowski+dt@linaro.org, rafael@kernel.org, andriy.shevchenko@linux.intel.com, hdegoede@redhat.com, jgg@ziepe.ca, saravanak@google.com, keescook@chromium.org, tglx@linutronix.de Cc: linux-kernel@vger.kernel.org, abel.vesa@linaro.org, Peng Fan Subject: [PATCH] devres: provide API devm_kstrndup Date: Thu, 4 May 2023 15:57:54 +0800 Message-Id: <20230504075754.372780-1-peng.fan@oss.nxp.com> X-Mailer: git-send-email 2.37.1 Content-Transfer-Encoding: 8bit Content-Type: text/plain X-ClientProxiedBy: SG2P153CA0034.APCP153.PROD.OUTLOOK.COM (2603:1096:4:c7::21) To DU0PR04MB9417.eurprd04.prod.outlook.com (2603:10a6:10:358::11) MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: DU0PR04MB9417:EE_|AM9PR04MB8273:EE_ X-MS-Office365-Filtering-Correlation-Id: d9a77a42-0a46-452a-1436-08db4c7491f3 X-MS-Exchange-SharedMailbox-RoutingAgent-Processed: True X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: CxvRzSgECj95/gfmuAX09WV4pybuhlPtYgSo+nmfayMVn2wTTYodv2AcUzgJt4KeJAgsF3Kik6sR6lxgvxKTHFtgrKs4ggaQTlsFrGOvbgwWz72DilkvOq6mGo1rGvTwYRiftoN6jjxqk2WfXWhjkfaV5hZH1N7DWkT8Kuj7E8xtWzrs0VmyAT6eqWWsEtvlzGzn11baESA7EKrTYeElsbmGlsbiBR7c+pwIb8wY5eTlxB4u5UgIzTn9z+ds5+xzULfYdMkodxsAaeSzWC8WqeY5as5KbykgxABHRhnILKApfSrE2lqH/TSTG6JVBGPEN/6id67eUT6ZEs/1F4gln7xmtXVC8azvi3Kxqc0DExzR5FENQuO4gAQ68ao3iTwWw0bV73MjdWaHzlR5oqRsV6mLhnAEhFy6Mapl64BLv3B/IJaa5zXZxTDx0D0f0XE3xI2c4/5j5+DppMkrvFQggOMe/kKhazqwceLLQgPX3N+l7vYuaBoIrXHPVe7pH4M9S1+EPZ00xXnt66fFeuf7+b59pRGo10hCdU9+50gj2bvqma68Q5b0I05zvWlq3g8JxaWeWZLuzcbOTqCxEQLQQl1XP5pWOp1XWKwlE704C9N1Jy0OrkFMtgik5qTbVndw21zgeI/vT1dxz+rIz10ntg== X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:DU0PR04MB9417.eurprd04.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230028)(4636009)(39860400002)(136003)(346002)(376002)(366004)(396003)(451199021)(316002)(41300700001)(2906002)(6512007)(186003)(6506007)(1076003)(2616005)(6486002)(52116002)(66476007)(83380400001)(4326008)(26005)(66946007)(66556008)(86362001)(7416002)(38100700002)(478600001)(5660300002)(8676002)(8936002)(921005)(38350700002);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: =?us-ascii?Q?2PBbR7NvoJSYRHwc72j0wWB3z8+sI9YgqQK0aypc/5kcxoeWS1CmkT4wpOGw?= =?us-ascii?Q?p53vTo8Ii0zajAM4HVf6o9dcgZsj751pqiZYh/SAl0c4Sn2c3rBseYN0Ap4Y?= =?us-ascii?Q?7Ll6zKkh4hjQpscGeS8H5E8i2oux4Vdel8Q6to0pvOPBJHXa+KojRFaVscCt?= =?us-ascii?Q?mzAWh8b7FzqVfoAQcZMl1SkRH2H09QvoswUToCDgAgBeMDPd/0jsO6J0NEHx?= =?us-ascii?Q?8+WTgkZiCxLIfGr5AmWySjAyN7ujCN+nrjWODMphocmU04ieLAcr3kUgvs6P?= =?us-ascii?Q?8IdQVStmSweR73g8BFdSlcXWGRdLyiZ6E+V9u5HQHbKaHOYqNAfN73WJGkxh?= =?us-ascii?Q?AeWp0Szuq8XNx4Dk6Na3swBbHW3eA/6dYdk7xdbH3iczev/9EgzUmJM1BDRr?= =?us-ascii?Q?oS23OqOhAQCZ9ut4XunNbScdRGvZ/X+m9P00nBTSXc/IiZbTDYIr0bUTa5+R?= =?us-ascii?Q?YEZcU/FM8DQBSJCjgpn/Tcr8zsLScqw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aESw6xczIq/8bFh4QspIYE8dbdUS2bdTvZtdzwI/lvCFgTO6Yjjg?= =?us-ascii?Q?06c6tlBez0xSzKU/KbXBftCnjhROuFjPe/bkuyKYjZCcaXtzoIfhfiFKzOH7?= =?us-ascii?Q?E2SCZxiVsW2OZH3dYzkw4/tAUangWVYXDtOfQOFcXwGuZB8mvPlw+4eJQtLr?= =?us-ascii?Q?ANqv1p9BqOsuYxtiCtTT9dETKEnI8644TUKRMgMA?= X-OriginatorOrg: oss.nxp.com X-MS-Exchange-CrossTenant-Network-Message-Id: d9a77a42-0a46-452a-1436-08db4c7491f3 X-MS-Exchange-CrossTenant-AuthSource: DU0PR04MB9417.eurprd04.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 04 May 2023 07:52:55.6095 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 686ea1d3-bc2b-4c6f-a92c-d99c5c301635 X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: FG1WzuF+F5BwcRk66idM6qMAfBV7TSpunkd5LkVdgisyurFstR1Zfl9VTEf8XIO9m5ubnagqAqPYp4eu7lHFqA== X-MS-Exchange-Transport-CrossTenantHeadersStamped: AM9PR04MB8273 X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2,SPF_HELO_PASS,SPF_PASS, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Peng Fan This patch introduces devm_kstrndup API so that the device's driver can allocate memory and copy string. Signed-off-by: Peng Fan --- drivers/base/devres.c | 28 ++++++++++++++++++++++++++++ include/linux/device.h | 1 + 2 files changed, 29 insertions(+) diff --git a/drivers/base/devres.c b/drivers/base/devres.c index 5c998cfac335..87e2965e5bab 100644 --- a/drivers/base/devres.c +++ b/drivers/base/devres.c @@ -963,6 +963,34 @@ char *devm_kstrdup(struct device *dev, const char *s, gfp_t gfp) } EXPORT_SYMBOL_GPL(devm_kstrdup); +/** + * devm_kstrndup - Allocate resource managed space and + * copy an existing string into that. + * @dev: Device to allocate memory for + * @s: the string to duplicate + * @max: max length to duplicate + * @gfp: the GFP mask used in the devm_kmalloc() call when allocating memory + * RETURNS: + * Pointer to allocated string on success, NULL on failure. + */ +char *devm_kstrndup(struct device *dev, const char *s, size_t max, gfp_t gfp) +{ + size_t len; + char *buf; + + if (!s) + return NULL; + + len = strnlen(s, max); + buf = devm_kmalloc(dev, len + 1, gfp); + if (buf) { + memcpy(buf, s, len); + buf[len] = '\0'; + } + return buf; +} +EXPORT_SYMBOL_GPL(devm_kstrndup); + /** * devm_kstrdup_const - resource managed conditional string duplication * @dev: device for which to duplicate the string diff --git a/include/linux/device.h b/include/linux/device.h index 472dd24d4823..56b092883c64 100644 --- a/include/linux/device.h +++ b/include/linux/device.h @@ -226,6 +226,7 @@ static inline void *devm_kcalloc(struct device *dev, void devm_kfree(struct device *dev, const void *p); char *devm_kstrdup(struct device *dev, const char *s, gfp_t gfp) __malloc; const char *devm_kstrdup_const(struct device *dev, const char *s, gfp_t gfp); +char *devm_kstrndup(struct device *dev, const char *s, size_t max, gfp_t gfp) __malloc; void *devm_kmemdup(struct device *dev, const void *src, size_t len, gfp_t gfp) __realloc_size(3); -- 2.37.1