Received: by 2002:a19:651b:0:0:0:0:0 with SMTP id z27csp3612509lfb; Mon, 9 May 2022 00:02:38 -0700 (PDT) X-Google-Smtp-Source: ABdhPJy8hzmkS3LwgdbIgjfv90gHrZSxnI/JthEUGrkuEp//XMKxqXsOZMu/rZRvR3AQQwjKw5g7 X-Received: by 2002:a17:90b:38d1:b0:1dc:902a:46fe with SMTP id nn17-20020a17090b38d100b001dc902a46femr16689375pjb.243.1652079758423; Mon, 09 May 2022 00:02:38 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1652079758; cv=none; d=google.com; s=arc-20160816; b=J2ANOtOrCR6Mrfjr5Y3f3MHQRKNhCrn2K3HzxGMUxVhW46fB9P+J9MD8U1l1iBffgi GiIBFCQ74779C7PaYtZk+4z85/ip8DlNCxlDTUC/HasKHQ6kXyHyNbEQaMkQcU+pleVK pX0TvEYqn336RGlzoefE9YB5Hxn+gzuee8dsc6kdRAC5dJn93mxxwN0ZDFOWXgEMvJXa Gq7ThzZN3JErMinvUI9KAMZv0MFKnddrwq2c2tnUEu86jlj3Y30f0OrAZjB67e+iKIoO 6JN0TZPALv4pOdpd7nrwLgQlDPdzjj9qZ0ZfdENTe92O+ITTjP66uCzQCPcGl5IO7VJf 2fBg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:content-language :mime-version:accept-language:in-reply-to:references:message-id:date :thread-index:thread-topic:subject:cc:to:from; bh=ntVScZClFJrnx6zxcNWD43sfZEpJCAyuuBI6p4/eN1g=; b=fp/ecpfUINjSBHUJhLNjwrlF9u0Aih6qPVhnJ8gM+7DZlCZRd7tjEMvEOSF7MaA0Df Pxd3uka7DdkCAsVWKVZvHxxOsB8AlvD1x+m731k/hOpMk7Wth9OZ3NEu5OXF5eb/kX4G KrRIo+aoszKtsm1eXYhr98mIcOlGjCGcLsisffyC13ovAOx2tJ10tOzva0Z6k0Ks9ksG 5YEsLND37F/Dt8xDq05ZjmZcsKmjOo1DxuDM3xkURnCszFCabH/W47GQKQ18M4vJ5lZq nakI26/Ng9OZLRoQcDYtVw6ImHPW7FJ6PG3pquGCrNFXnATq5/n4T8Pg6mLNmEnh/XaJ 0v4g== ARC-Authentication-Results: i=1; mx.google.com; spf=softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=aculab.com Return-Path: Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net. [23.128.96.19]) by mx.google.com with ESMTPS id i185-20020a6387c2000000b003c6a5d55413si5109057pge.114.2022.05.09.00.02.38 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 09 May 2022 00:02:38 -0700 (PDT) Received-SPF: softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) client-ip=23.128.96.19; Authentication-Results: mx.google.com; spf=softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=aculab.com Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 6D3711566D4; Mon, 9 May 2022 00:00:12 -0700 (PDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1442408AbiEFOuh convert rfc822-to-8bit (ORCPT + 99 others); Fri, 6 May 2022 10:50:37 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47868 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S237385AbiEFOug (ORCPT ); Fri, 6 May 2022 10:50:36 -0400 Received: from eu-smtp-delivery-151.mimecast.com (eu-smtp-delivery-151.mimecast.com [185.58.85.151]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 0C3D013D51 for ; Fri, 6 May 2022 07:46:51 -0700 (PDT) Received: from AcuMS.aculab.com (156.67.243.121 [156.67.243.121]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384) id uk-mta-274-gs3rWkflMKGWx7a-ge22aQ-1; Fri, 06 May 2022 15:46:49 +0100 X-MC-Unique: gs3rWkflMKGWx7a-ge22aQ-1 Received: from AcuMS.Aculab.com (fd9f:af1c:a25b:0:994c:f5c2:35d6:9b65) by AcuMS.aculab.com (fd9f:af1c:a25b:0:994c:f5c2:35d6:9b65) with Microsoft SMTP Server (TLS) id 15.0.1497.32; Fri, 6 May 2022 15:46:47 +0100 Received: from AcuMS.Aculab.com ([fe80::994c:f5c2:35d6:9b65]) by AcuMS.aculab.com ([fe80::994c:f5c2:35d6:9b65%12]) with mapi id 15.00.1497.033; Fri, 6 May 2022 15:46:47 +0100 From: David Laight To: 'Jagdish Gediya' , "linux-mm@kvack.org" , "linux-kernel@vger.kernel.org" , "gregkh@linuxfoundation.org" , "rafael@kernel.org" , "akpm@linux-foundation.org" CC: "keescook@chromium.org" , "andriy.shevchenko@linux.intel.com" , "geert@linux-m68k.org" , "linux@roeck-us.net" , "adobriyan@gmail.com" Subject: RE: [PATCH] kobject: Refactor kobject_set_name_vargs() Thread-Topic: [PATCH] kobject: Refactor kobject_set_name_vargs() Thread-Index: AQHYYU3qLELCyca9fE2SaEK1v39Vvq0R6/5w Date: Fri, 6 May 2022 14:46:47 +0000 Message-ID: <5902e26ef400451b966be2dd0fbd1575@AcuMS.aculab.com> References: <20220506133309.36794-1-jvgediya@linux.ibm.com> In-Reply-To: <20220506133309.36794-1-jvgediya@linux.ibm.com> Accept-Language: en-GB, en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: x-ms-exchange-transport-fromentityheader: Hosted x-originating-ip: [10.202.205.107] MIME-Version: 1.0 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=C51A453 smtp.mailfrom=david.laight@aculab.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: aculab.com Content-Language: en-US Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8BIT X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00, HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI,RDNS_NONE, SPF_HELO_NONE,T_SCC_BODY_TEXT_LINE autolearn=no 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: Jagdish Gediya > Sent: 06 May 2022 14:33 > > Setting name as per the format is not only useful for kobjects. > It can also be used to set name for other things for e.g. setting > the name of the struct attribute when multiple same kind of attributes > need to be created with some identifier in name, instead of managing > memory for names at such places case by case, it would be good if > something like current kobject_set_name_vargs() can be utilized. > > Refactor kobject_set_name_vargs(), Create a new generic function > set_name_vargs() which can be used for kobjects as well as at > other places. > > This patch doesn't introduce any functionality change. > > Signed-off-by: Jagdish Gediya > --- > include/linux/string.h | 1 + > lib/kobject.c | 30 +----------------------------- > mm/util.c | 40 ++++++++++++++++++++++++++++++++++++++++ > 3 files changed, 42 insertions(+), 29 deletions(-) > > diff --git a/include/linux/string.h b/include/linux/string.h > index b6572aeca2f5..f329962e5ae9 100644 > --- a/include/linux/string.h > +++ b/include/linux/string.h > @@ -9,6 +9,7 @@ > #include > #include > > +int set_name_vargs(const char **name, const char *fmt, va_list vargs); > extern char *strndup_user(const char __user *, long); > extern void *memdup_user(const void __user *, size_t); > extern void *vmemdup_user(const void __user *, size_t); > diff --git a/lib/kobject.c b/lib/kobject.c > index 5f0e71ab292c..870d05971e3a 100644 > --- a/lib/kobject.c > +++ b/lib/kobject.c > @@ -249,35 +249,7 @@ static int kobject_add_internal(struct kobject *kobj) > int kobject_set_name_vargs(struct kobject *kobj, const char *fmt, > va_list vargs) > { ... > + return set_name_vargs(&kobj->name, fmt, vargs); > } > > /** > diff --git a/mm/util.c b/mm/util.c > index 54e5e761a9a9..808d29b17ea7 100644 > --- a/mm/util.c > +++ b/mm/util.c > @@ -112,6 +112,46 @@ char *kstrndup(const char *s, size_t max, gfp_t gfp) > } > EXPORT_SYMBOL(kstrndup); > > +/** > + * set_name_vargs() - Set the name as per format > + * @name: pointer to point to the name as per format > + * @fmt: format string used to build the name > + * @vargs: vargs to format the string. > + */ > +int set_name_vargs(const char **name, const char *fmt, va_list vargs) > +{ > + const char *s; > + > + if (*name && !fmt) > + return 0; > + > + s = kvasprintf_const(GFP_KERNEL, fmt, vargs); > + if (!s) > + return -ENOMEM; > + > + /* > + * ewww... some of these buggers have '/' in the name ... If > + * that's the case, we need to make sure we have an actual > + * allocated copy to modify, since kvasprintf_const may have > + * returned something from .rodata. > + */ > + if (strchr(s, '/')) { > + char *t; > + > + t = kstrdup(s, GFP_KERNEL); > + kfree_const(s); > + if (!t) > + return -ENOMEM; There is a kstrdup_const() that will DTRT here. > + strreplace(t, '/', '!'); > + s = t; > + } > + kfree_const(*name); > + *name = s; > + > + return 0; > +} > +EXPORT_SYMBOL(set_name_vargs); Are you sure this can ever work from a module? This all relies on: static inline bool is_kernel_rodata(unsigned long addr) { return addr >= (unsigned long)__start_rodata && addr < (unsigned long)__end_rodata; } which isn't going to do anything sane given an "xxx" inside a module. Indeed can kobject_set_name_vargs() end up with a constant string inside a module? Seems horribly fragile. David > + > /** > * kmemdup - duplicate region of memory > * > -- > 2.35.1 - Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK Registration No: 1397386 (Wales)