From mboxrd@z Thu Jan 1 00:00:00 1970 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 B4C4BC433EF for ; Fri, 6 May 2022 14:46:55 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id D18D16B0072; Fri, 6 May 2022 10:46:54 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id CC7E96B0073; Fri, 6 May 2022 10:46:54 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id B69E86B0074; Fri, 6 May 2022 10:46:54 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id A7F816B0072 for ; Fri, 6 May 2022 10:46:54 -0400 (EDT) Received: from smtpin03.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay13.hostedemail.com (Postfix) with ESMTP id 852A46134F for ; Fri, 6 May 2022 14:46:54 +0000 (UTC) X-FDA: 79435595148.03.712840C Received: from eu-smtp-delivery-151.mimecast.com (eu-smtp-delivery-151.mimecast.com [185.58.85.151]) by imf14.hostedemail.com (Postfix) with ESMTP id E2F1A10006A for ; Fri, 6 May 2022 14:46:51 +0000 (UTC) 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 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: aculab.com Content-Language: en-US Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable X-Stat-Signature: dcj1f6ri8r9ars4hwhy1sdn8swnyrouu Authentication-Results: imf14.hostedemail.com; dkim=none; spf=pass (imf14.hostedemail.com: domain of david.laight@aculab.com designates 185.58.85.151 as permitted sender) smtp.mailfrom=david.laight@aculab.com; dmarc=pass (policy=none) header.from=aculab.com X-Rspam-User: X-Rspamd-Server: rspam01 X-Rspamd-Queue-Id: E2F1A10006A X-HE-Tag: 1651848411-532456 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: From: Jagdish Gediya > Sent: 06 May 2022 14:33 >=20 > 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. >=20 > 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. >=20 > This patch doesn't introduce any functionality change. >=20 > 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(-) >=20 > 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 >=20 > +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, > =09=09=09=09 va_list vargs) > { ... > +=09return set_name_vargs(&kobj->name, fmt, vargs); > } >=20 > /** > 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); >=20 > +/** > + * 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) > +{ > +=09const char *s; > + > +=09if (*name && !fmt) > +=09=09return 0; > + > +=09s =3D kvasprintf_const(GFP_KERNEL, fmt, vargs); > +=09if (!s) > +=09=09return -ENOMEM; > + > +=09/* > +=09 * ewww... some of these buggers have '/' in the name ... If > +=09 * that's the case, we need to make sure we have an actual > +=09 * allocated copy to modify, since kvasprintf_const may have > +=09 * returned something from .rodata. > +=09 */ > +=09if (strchr(s, '/')) { > +=09=09char *t; > + > +=09=09t =3D kstrdup(s, GFP_KERNEL); > +=09=09kfree_const(s); > +=09=09if (!t) > +=09=09=09return -ENOMEM; There is a kstrdup_const() that will DTRT here. > +=09=09strreplace(t, '/', '!'); > +=09=09s =3D t; > +=09} > +=09kfree_const(*name); > +=09*name =3D s; > + > +=09return 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) { =09return addr >=3D (unsigned long)__start_rodata && =09 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. =09David > + > /** > * kmemdup - duplicate region of memory > * > -- > 2.35.1 - Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1= PT, UK Registration No: 1397386 (Wales)