Received: by 2002:a05:6358:9144:b0:117:f937:c515 with SMTP id r4csp242738rwr; Thu, 4 May 2023 02:13:34 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ4wcHzvw0Dz+Errc75Y+zfBYwXunn9Gjj5iY2jBft90g1Ubg/947XMWytHDxl9z5xX2fTwm X-Received: by 2002:a17:903:11c9:b0:1ab:16e0:ef49 with SMTP id q9-20020a17090311c900b001ab16e0ef49mr3505123plh.24.1683191613633; Thu, 04 May 2023 02:13:33 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1683191613; cv=none; d=google.com; s=arc-20160816; b=yA7Ju2iIPQs5TMOkF21RaGsrBk5E6G8M0T76Gbszn1jAqU9w/cVyOfocXRigg4aQqh kmr1NZPWuQh9OykXwDI3KLTUxtur4p2uinhrvJTMguxsvynx8dz/un9ShOCI2zc+ofTd JvOnyilowdbzAKK0l9KT6ACooCT7fbXc4mEt5yL7UdvYnPx9AGurD1JpFrELwgqycqdv C2VieIiAyxelS8ovYRgjRG9///t+dZxb4OSYtrudqR+yNWQbOFtVC2rYvR1ox1RVJb4j NO6okpF2FVsX59R6aLk3cn5KHLf5LzhFR12Dx0SpO3q4uZbmLR+EuZwOi2GZArP3u1tc o3Qg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature; bh=wA0MPSXyacfYFdgQctIsXmF6i8drknG48lskxlEJVxc=; b=0kydLdydb2LQebDrTkhn/HmqeW1x6V6NgQYgOdEZ1aDypnsnf2qUCs2km7wXpfcvda ubX4V9wf6r35ld4CtjrvOcT0TcduWQkvShhcuQ1+6FBYJ/ZzuCj/UoISZRpbyUesfOGe K9+1R7/4escOFbRGIqXTgPDctD6iEpKq3UfWhx6P1+D4Gaslo0bjKgCRg3r4xkXcamf1 ZInbwpG5X5Dz/O2t1/EJ6+ENKabvbGgMbEzdE92biGG5S1CRV6WT4Ivy5hWXH7FeeSTG Brfv4K9CAxWkjzLCHk9qBasnWvt84H27iywRr/EaQis4eA8DT6eOCUtAFLSakYK2yJ8e RiAw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=EYnHKq5p; 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=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id x14-20020a170902b40e00b001aadf9e2d15si11914056plr.491.2023.05.04.02.13.19; Thu, 04 May 2023 02:13:33 -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=@linuxfoundation.org header.s=korg header.b=EYnHKq5p; 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=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229883AbjEDI6T (ORCPT + 99 others); Thu, 4 May 2023 04:58:19 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57792 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229598AbjEDI6S (ORCPT ); Thu, 4 May 2023 04:58:18 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id EC2B5358A for ; Thu, 4 May 2023 01:58:16 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id 8E30C63286 for ; Thu, 4 May 2023 08:58:16 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 6B3D0C433D2; Thu, 4 May 2023 08:58:15 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1683190695; bh=WB9OO+EBIif8Ny5uYVvpRmtEyUuGxpVYXQ0OMAFnMXs=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=EYnHKq5p6hmHN85ndecCLdeeKIY6uFQJe+9XBPB2LZTISgCBMGWRmxf3Ffo3T26Rh Ykfz4aR4Isg3spKhZggzoY0M3LIOXoS9qANMYVn5SKBSudBEZR2EE8YtSa0I+NfwTU 0Z5xIi3GyhmrS2v39oss9UonSbn/NjUBSoifX6IY= Date: Thu, 4 May 2023 17:58:10 +0900 From: Greg KH To: "Peng Fan (OSS)" Cc: 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, linux-kernel@vger.kernel.org, abel.vesa@linaro.org, Peng Fan Subject: Re: [PATCH] devres: provide API devm_kstrndup Message-ID: <2023050413-margin-devotedly-468d@gregkh> References: <20230504075754.372780-1-peng.fan@oss.nxp.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20230504075754.372780-1-peng.fan@oss.nxp.com> X-Spam-Status: No, score=-7.3 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_HI, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE,URIBL_BLOCKED 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 On Thu, May 04, 2023 at 03:57:54PM +0800, Peng Fan (OSS) wrote: > 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(+) We can not add apis with no users, please submit this at the same time a driver needs it. And what driver would need to copy a string? > > 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'; Why not use kstrndup() instead of copying the same logic here? thanks, greg k-h