Skip to contentSkip to conversation

Fix vargs handling in iucv_alloc_device()

PATCH In review Vasily Gorbik 3 participants 12 messages Aug 13, 2024

Subsystems (5)
lore
Share & tools
lore

Vasily Gorbik

author maintainer
#0 lore raw

cover Fix vargs handling in iucv_alloc_device()

Export kobject_set_name_vargs() and use it in iucv_alloc_device() to fix vargs handling.

@Greg and Rafael:
Could you please ack the first patch so I can take both patches via the s390 tree?

Thank you

Heiko Carstens (1):
s390/iucv: Fix vargs handling in iucv_alloc_device()

Vasily Gorbik (1):
kobject: Export kobject_set_name_vargs() symbol

 lib/kobject.c   | 1 +
 net/iucv/iucv.c | 2 +-
 2 files changed, 2 insertions(+), 1 deletion(-)
Signature · 1 line
2.41.0
author maintainer
#1 lore raw

PATCH 1/2 kobject: Export kobject_set_name_vargs() symbol

The net/iucv module requires the kobject_set_name_vargs() function to set kobject names dynamically based on variable arguments. Export the kobject_set_name_vargs() symbol, allowing the net/iucv module and other potential modules to use this utility function.

---
 lib/kobject.c | 1 +
 1 file changed, 1 insertion(+)
lib/kobject.c +1 -0
index 72fa20f405f1..96caa8bfdc40 100644
@@ -295,6 +295,7 @@ int kobject_set_name_vargs(struct kobject *kobj, const char *fmt,
 
 	return 0;
 }
+EXPORT_SYMBOL(kobject_set_name_vargs);
 
 /**
  * kobject_set_name() - Set the name of a kobject.
Signature · 1 line
2.41.0
#2 lore raw

Re: [PATCH 1/2] kobject: Export kobject_set_name_vargs() symbol

Quoted 17 lines · Vasily Gorbik

The net/iucv module requires the kobject_set_name_vargs() function to set kobject names dynamically based on variable arguments. Export the kobject_set_name_vargs() symbol, allowing the net/iucv module and other potential modules to use this utility function.

---
 lib/kobject.c | 1 +
 1 file changed, 1 insertion(+)
lib/kobject.c +0 -0
index 72fa20f405f1..96caa8bfdc40 100644

lib/kobject.c int kobject_set_name_vargs(struct kobject *kobj, const char *fmt, · line 298

Quoted 4 lines
lib/kobject.c +1 -0
@@ -295,6 +295,7 @@ int kobject_set_name_vargs(struct kobject *kobj, const char *fmt,
 
 	return 0;
 }
+EXPORT_SYMBOL(kobject_set_name_vargs);

EXPORT_SYMBOL_GPL() please.

thanks,

greg k-h

author maintainer
#3 lore raw

PATCH 2/2 s390/iucv: Fix vargs handling in iucv_alloc_device()

In reply to Vasily Gorbik

From: Heiko Carstens <[email protected]>

iucv_alloc_device() gets a format string and a varying number of arguments. This is incorrectly forwarded by calling dev_set_name() with the format string and a va_list, while dev_set_name() expects also a varying number of arguments.

Fix this and call kobject_set_name_vargs() instead which expects a va_list parameter.

Fixes 4452e8ef8c36 ("s390/iucv: Provide iucv_alloc_device() / iucv_release_device()")

Reference-ID: https://bugzilla.linux.ibm.com/show_bug.cgi?id=208008

---
 net/iucv/iucv.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
net/iucv/iucv.c +1 -1
index 1e42e13ad24e..64102a31b569 100644
@@ -92,7 +92,7 @@ struct device *iucv_alloc_device(const struct attribute_group **attrs,
 	if (!dev)
 		goto out_error;
 	va_start(vargs, fmt);
-	rc = dev_set_name(dev, fmt, vargs);
+	rc = kobject_set_name_vargs(&dev->kobj, fmt, vargs);
 	va_end(vargs);
 	if (rc)
 		goto out_error;
Signature · 1 line
2.41.0
#4 lore raw

Re: [PATCH 2/2] s390/iucv: Fix vargs handling in iucv_alloc_device()

Quoted 9 lines · Vasily Gorbik

From: Heiko Carstens <[email protected]>

iucv_alloc_device() gets a format string and a varying number of arguments. This is incorrectly forwarded by calling dev_set_name() with the format string and a va_list, while dev_set_name() expects also a varying number of arguments.

Fix this and call kobject_set_name_vargs() instead which expects a va_list parameter.

I don't understand, why can't dev_set_name() be called here?

Calling "raw" kobject functions is almost never the correct thing to be doing, ESPECIALLY as you have a struct device here.

thanks,

greg k-h

author maintainer
#5 lore raw

Re: [PATCH 2/2] s390/iucv: Fix vargs handling in iucv_alloc_device()

Quoted 8 lines · Greg Kroah-Hartman
Quoted 7 lines · Vasily Gorbik

From: Heiko Carstens <[email protected]>

iucv_alloc_device() gets a format string and a varying number of arguments. This is incorrectly forwarded by calling dev_set_name() with the format string and a va_list, while dev_set_name() expects also a varying number of arguments.

Quoted 7 lines
Quoted 2 lines

Fix this and call kobject_set_name_vargs() instead which expects a va_list parameter.

I don't understand, why can't dev_set_name() be called here?

Calling "raw" kobject functions is almost never the correct thing to be doing, ESPECIALLY as you have a struct device here.

struct device *iucv_alloc_device(const struct attribute_group **attrs,

                                 void *priv, const char *fmt, ...);

va_start(vargs, fmt); initializes vargs to point to the first argument after fmt.

__printf(2, 0) int kobject_set_name_vargs(struct kobject *kobj, const char *fmt, va_list vargs);

__printf(2, 3) int dev_set_name(struct device *dev, const char *name, ...);

dev_set_name is expecting to receive individual variable arguments directly (...), not a va_list.

The (...) in dev_set_name is meant to be expanded into individual arguments, but when you pass a va_list to it, this expansion doesn't happen. Instead, the va_list is just treated as a pointer or a single argument, leading to undefined or incorrect behavior.

So, would it be okay to reuse kobject_set_name_vargs() here, or would you propose introducing another helper just for this case? e.g.

int dev_set_name_vargs(struct device *dev, const char *fmt, va_list vargs) {
჻·······return kobject_set_name_vargs(&dev->kobj, fmt, vargs); }
EXPORT_SYMBOL_GPL(dev_set_name_vargs)

The bz link should be:

#6 lore raw

Re: [PATCH 2/2] s390/iucv: Fix vargs handling in iucv_alloc_device()

Quoted 34 lines · Vasily Gorbik
Quoted 8 lines · Greg Kroah-Hartman
Quoted 7 lines · Vasily Gorbik

From: Heiko Carstens <[email protected]>

iucv_alloc_device() gets a format string and a varying number of arguments. This is incorrectly forwarded by calling dev_set_name() with the format string and a va_list, while dev_set_name() expects also a varying number of arguments.

Quoted 7 lines
Quoted 2 lines

Fix this and call kobject_set_name_vargs() instead which expects a va_list parameter.

I don't understand, why can't dev_set_name() be called here?

Calling "raw" kobject functions is almost never the correct thing to be doing, ESPECIALLY as you have a struct device here.

struct device *iucv_alloc_device(const struct attribute_group **attrs,

                                 void *priv, const char *fmt, ...);

va_start(vargs, fmt); initializes vargs to point to the first argument after fmt.

__printf(2, 0) int kobject_set_name_vargs(struct kobject *kobj, const char *fmt, va_list vargs);

__printf(2, 3) int dev_set_name(struct device *dev, const char *name, ...);

dev_set_name is expecting to receive individual variable arguments directly (...), not a va_list.

The (...) in dev_set_name is meant to be expanded into individual arguments, but when you pass a va_list to it, this expansion doesn't happen. Instead, the va_list is just treated as a pointer or a single argument, leading to undefined or incorrect behavior.

Quoted 8 lines

So, would it be okay to reuse kobject_set_name_vargs() here, or would you propose introducing another helper just for this case? e.g.

int dev_set_name_vargs(struct device *dev, const char *fmt, va_list vargs) {
჻·······return kobject_set_name_vargs(&dev->kobj, fmt, vargs); }
EXPORT_SYMBOL_GPL(dev_set_name_vargs)

This function makes more sense if you really want to do this.

But step back, why is this needed at all anyway? No other subsystem or driver needs/wants this, what makes this api so special? Why not figure out your name beforehand?

thanks,

greg k-h

author maintainer
#7 lore raw

Re: [PATCH 2/2] s390/iucv: Fix vargs handling in iucv_alloc_device()

Quoted 44 lines · Greg Kroah-Hartman
Quoted 34 lines · Vasily Gorbik
Quoted 8 lines · Greg Kroah-Hartman
Quoted 7 lines · Vasily Gorbik

From: Heiko Carstens <[email protected]>

iucv_alloc_device() gets a format string and a varying number of arguments. This is incorrectly forwarded by calling dev_set_name() with the format string and a va_list, while dev_set_name() expects also a varying number of arguments.

Quoted 7 lines
Quoted 2 lines

Fix this and call kobject_set_name_vargs() instead which expects a va_list parameter.

I don't understand, why can't dev_set_name() be called here?

Calling "raw" kobject functions is almost never the correct thing to be doing, ESPECIALLY as you have a struct device here.

struct device *iucv_alloc_device(const struct attribute_group **attrs,

                                 void *priv, const char *fmt, ...);

va_start(vargs, fmt); initializes vargs to point to the first argument after fmt.

__printf(2, 0) int kobject_set_name_vargs(struct kobject *kobj, const char *fmt, va_list vargs);

__printf(2, 3) int dev_set_name(struct device *dev, const char *name, ...);

dev_set_name is expecting to receive individual variable arguments directly (...), not a va_list.

The (...) in dev_set_name is meant to be expanded into individual arguments, but when you pass a va_list to it, this expansion doesn't happen. Instead, the va_list is just treated as a pointer or a single argument, leading to undefined or incorrect behavior.

Quoted 8 lines

So, would it be okay to reuse kobject_set_name_vargs() here, or would you propose introducing another helper just for this case? e.g.

int dev_set_name_vargs(struct device *dev, const char *fmt, va_list vargs) {
჻·······return kobject_set_name_vargs(&dev->kobj, fmt, vargs); }
EXPORT_SYMBOL_GPL(dev_set_name_vargs)

Quoted 5 lines

This function makes more sense if you really want to do this.

But step back, why is this needed at all anyway? No other subsystem or driver needs/wants this, what makes this api so special? Why not figure out your name beforehand?

That's comming from this patch series:
https://lore.kernel.org/all/[email protected]/#t

"""
Unify IUCV device allocation as suggested by Arnd Bergmann in order to get rid of code duplication in various device drivers. """

It just introduces and utilizes a couple of wrappers to reduce code duplication, and in this case, introducing this level of indirection led to the need for forwarding vargs.

And reimplementing parts of kobject_set_name_vargs to format the device name beforehand is probably not what we want.

maintainer
#8 lore raw

Re: [PATCH 2/2] s390/iucv: Fix vargs handling in iucv_alloc_device()

In reply to Greg Kroah-Hartman
Quoted 2 lines · Greg Kroah-Hartman
Quoted 2 lines
Quoted 2 lines

I don't understand, why can't dev_set_name() be called here?

[...]

Quoted 6 lines

But step back, why is this needed at all anyway? No other subsystem or driver needs/wants this, what makes this api so special? Why not figure out your name beforehand?

thanks,

Vasily, the following update to Heiko's patch does not touch lib/kobject.c According to a quick test it still solves the original issue and does compile with W=1 and iucv as a module.

net/iucv/iucv.c +4 -1
index 64102a31b569..6a819ba4ccab 100644
@@ -86,13 +86,17 @@ struct device *iucv_alloc_device(const struct attribute_group **attrs,
 {
        struct device *dev;
        va_list vargs;
+       char buf[20];
        int rc;
 
        dev = kzalloc(sizeof(*dev), GFP_KERNEL);
        if (!dev)
                goto out_error;
        va_start(vargs, fmt);
-       rc = kobject_set_name_vargs(&dev->kobj, fmt, vargs);
+       rc = vsnprintf(buf, 20, fmt, vargs);
+       if (!rc)
+               rc = dev_set_name(dev, buf);
        va_end(vargs);
        if (rc)
                goto out_error;

Maybe Greg has somethign like this in mind?

author maintainer
#9 lore raw

Re: [PATCH 2/2] s390/iucv: Fix vargs handling in iucv_alloc_device()

Quoted 41 lines · Alexandra Winter
Quoted 2 lines · Greg Kroah-Hartman
Quoted 2 lines
Quoted 2 lines

I don't understand, why can't dev_set_name() be called here?

[...]

Quoted 6 lines

But step back, why is this needed at all anyway? No other subsystem or driver needs/wants this, what makes this api so special? Why not figure out your name beforehand?

thanks,

Vasily, the following update to Heiko's patch does not touch lib/kobject.c According to a quick test it still solves the original issue and does compile with W=1 and iucv as a module.

net/iucv/iucv.c +4 -1
index 64102a31b569..6a819ba4ccab 100644
@@ -86,13 +86,17 @@ struct device *iucv_alloc_device(const struct attribute_group **attrs,
 {
        struct device *dev;
        va_list vargs;
+       char buf[20];
        int rc;
 
        dev = kzalloc(sizeof(*dev), GFP_KERNEL);
        if (!dev)
                goto out_error;
        va_start(vargs, fmt);
-       rc = kobject_set_name_vargs(&dev->kobj, fmt, vargs);
+       rc = vsnprintf(buf, 20, fmt, vargs);
+       if (!rc)
+               rc = dev_set_name(dev, buf);
        va_end(vargs);
        if (rc)
                goto out_error;
Quoted 1 line

Maybe Greg has somethign like this in mind?

Thanks Alexandra,

but I'm still leaning towards forwarding vargs and avoid splitting the name formatting logic, if Greg agrees that the use case justifies adding a new helper. Let's see what Greg prefers.

int dev_set_name_vargs(struct device *dev, const char *fmt, va_list vargs)

{
        return kobject_set_name_vargs(&dev->kobj, fmt, vargs);
}

EXPORT_SYMBOL_GPL(dev_set_name_vargs)

#10 lore raw

Re: [PATCH 2/2] s390/iucv: Fix vargs handling in iucv_alloc_device()

In reply to Alexandra Winter
Quoted 27 lines · Alexandra Winter
Quoted 2 lines · Greg Kroah-Hartman
Quoted 2 lines
Quoted 2 lines

I don't understand, why can't dev_set_name() be called here?

[...]

Quoted 6 lines

But step back, why is this needed at all anyway? No other subsystem or driver needs/wants this, what makes this api so special? Why not figure out your name beforehand?

thanks,

Vasily, the following update to Heiko's patch does not touch lib/kobject.c According to a quick test it still solves the original issue and does compile with W=1 and iucv as a module.

net/iucv/iucv.c +1 -0
index 64102a31b569..6a819ba4ccab 100644
@@ -86,13 +86,17 @@ struct device *iucv_alloc_device(const struct attribute_group **attrs,
 {
        struct device *dev;
        va_list vargs;
+       char buf[20];

net/iucv/iucv.c struct device *iucv_alloc_device(const struct attribute_group **attrs, · line 98

Quoted 10 lines
net/iucv/iucv.c +3 -1
        int rc;
 
        dev = kzalloc(sizeof(*dev), GFP_KERNEL);
        if (!dev)
                goto out_error;
        va_start(vargs, fmt);
-       rc = kobject_set_name_vargs(&dev->kobj, fmt, vargs);
+       rc = vsnprintf(buf, 20, fmt, vargs);
+       if (!rc)
+               rc = dev_set_name(dev, buf);

This looks best, let's not create a core function that no one has ever needed yet just for one user :)

thanks,

greg k-h

author maintainer
#11 lore raw

Re: [PATCH 2/2] s390/iucv: Fix vargs handling in iucv_alloc_device()

Quoted 30 lines · Greg Kroah-Hartman
Quoted 29 lines · Alexandra Winter
Quoted 2 lines · Greg Kroah-Hartman
Quoted 2 lines
Quoted 2 lines

I don't understand, why can't dev_set_name() be called here?

[...]

Quoted 6 lines

But step back, why is this needed at all anyway? No other subsystem or driver needs/wants this, what makes this api so special? Why not figure out your name beforehand?

thanks,

Vasily, the following update to Heiko's patch does not touch lib/kobject.c According to a quick test it still solves the original issue and does compile with W=1 and iucv as a module.

net/iucv/iucv.c +1 -0
index 64102a31b569..6a819ba4ccab 100644
@@ -86,13 +86,17 @@ struct device *iucv_alloc_device(const struct attribute_group **attrs,
 {
        struct device *dev;
        va_list vargs;
+       char buf[20];
        int rc;
 
Quoted 11 lines

net/iucv/iucv.c struct device *iucv_alloc_device(const struct attribute_group **attrs, · line 98

Quoted 8 lines
net/iucv/iucv.c +3 -1
        dev = kzalloc(sizeof(*dev), GFP_KERNEL);
        if (!dev)
                goto out_error;
        va_start(vargs, fmt);
-       rc = kobject_set_name_vargs(&dev->kobj, fmt, vargs);
+       rc = vsnprintf(buf, 20, fmt, vargs);
+       if (!rc)
+               rc = dev_set_name(dev, buf);

This looks best, let's not create a core function that no one has ever needed yet just for one user :)

Okay, fair enough. Thank you, Greg!

I'll drop these two patches. Alexandra, I assume you will send out your alternative fix separately. Thank you!