Skip to contentSkip to conversation

Exporting functions from vsprintf.c for Rust

DISCUSSION Petr Mladek 3 participants 4 messages Apr 11, 2025 – Apr 14, 2025 2 messages not in the archive

lore
Share & tools
lore

A message that is not in the archive (sent off-list or not yet received): [email protected]

#1 lore raw

Adding few more people and lkml into Cc.

include/linux/vsprintf.h

Quoted 17 lines · Remo Senekowitsch

Hi

I need to print the full path of a fwnode in Rust. One approach is to export it, as shown below. Is this acceptable to you in principle?

There are also some intermediary solutions like not providing a header and or prefixing __.

Thanks,
Remo

---

include/linux/vsprintf.h new file +0 -0
new file mode 100644
index 000000000..b37b11868

Just for record. Steven suggested to use include/linux/sprintf.h instead because it was already used for another vsprintf APIs.

include/linux/vsprintf.h line 4

Quoted 5 lines
include/linux/vsprintf.h new file +4 -0
@@ -0,0 +1,4 @@
+// SPDX-License-Identifier: GPL-2.0
+
+char *fwnode_full_name_string(struct fwnode_handle *fwnode, char *buf,
+			      char *end);

Honestly, I do not have a good feeling about exporting the internal vsprintf() functions. They have a very specific semantic.

Especially, they return pointer to the next-to-write character. And it might be even beyond the given *end pointer. It is because, for example, vsnprintf() returns the number of characters which would have been written to the buffer when it was big enough.

Instead, I suggest to create a wrapper which would have a sane semantic and call scnprintf() internally. Something like:

int fwnode_full_name_to_string(char *buf, size_t size,
struct fwnode_handle *fwnode)

{
	return scnprintf(buf, size, "%pfwf", fwnode);
}

I am just not sure where to put it. It might be vsprintf.c. But I think that a better place would be drivers/base/property.c. For example, I see a "similar" fwnode_property_match_string() already there.

Best Regards,
Petr

Quoted 33 lines
lib/vsprintf.c +2 -1
index 56fe96319..3b4d0065a 100644
@@ -43,6 +43,7 @@
 #include <linux/compiler.h>
 #include <linux/property.h>
 #include <linux/notifier.h>
+#include <linux/vsprintf.h>
 #ifdef CONFIG_BLOCK
 #include <linux/blkdev.h>
 #endif
@@ -2103,7 +2104,7 @@ char *flags_string(char *buf, char *end, void *flags_ptr,
 	return format_flags(buf, end, flags, names);
 }
 
-static noinline_for_stack
+noinline_for_stack
 char *fwnode_full_name_string(struct fwnode_handle *fwnode, char *buf,
 			      char *end)
 {
rust/bindings/bindings_helper.h +1 -0
index 4bd02abd2..24a565ffd 100644
@@ -38,6 +38,7 @@
 #include <linux/security.h>
 #include <linux/slab.h>
 #include <linux/tracepoint.h>
+#include <linux/vsprintf.h>
 #include <linux/wait.h>
 #include <linux/workqueue.h>
 #include <trace/events/rust_sample.h>
#2 lore raw
Quoted 9 lines · Petr Mladek

Honestly, I do not have a good feeling about exporting the internal vsprintf() functions. They have a very specific semantic.

Especially, they return pointer to the next-to-write character. And it might be even beyond the given *end pointer. It is because, for example, vsnprintf() returns the number of characters which would have been written to the buffer when it was big enough.

Quoted 8 lines

Instead, I suggest to create a wrapper which would have a sane semantic and call scnprintf() internally. Something like:

int fwnode_full_name_to_string(char *buf, size_t size,
struct fwnode_handle *fwnode)

{
	return scnprintf(buf, size, "%pfwf", fwnode);
}

That makes sense. I tried your suggestion and it works, thank you!

Best regards,
Remo

A message that is not in the archive (sent off-list or not yet received): CAKwiHFi_egfePDdXQEtYNvO-U65O==MosHNQ7Vm4F6iudJs95g@mail.gmail.com

#4 lore raw
Quoted 2 lines · Rasmus Villemoes
Quoted 1 line · On Fri, Apr 11, 2025, 13:36 Remo Senekowitsch <[email protected]> wrote:

On Fri Apr 11, 2025 at 11:42 AM CEST, Petr Mladek wrote:

...

Quoted 17 lines
Quoted 17 lines
Quoted 16 lines

Honestly, I do not have a good feeling about exporting the internal vsprintf() functions. They have a very specific semantic.

Especially, they return pointer to the next-to-write character. And it might be even beyond the given *end pointer. It is because, for example, vsnprintf() returns the number of characters which would have been written to the buffer when it was big enough.

Instead, I suggest to create a wrapper which would have a sane semantic and call scnprintf() internally. Something like:

int fwnode_full_name_to_string(char *buf, size_t size,
struct fwnode_handle *fwnode)

{
      return scnprintf(buf, size, "%pfwf", fwnode);
}
Quoted 5 lines
Quoted 3 lines

That makes sense. I tried your suggestion and it works, thank you!

But is a wrapper even needed for this? Can't the appropriate sprintf variant just be called from Rust with that %pfwf format string?

+1 here. But if really really need that wrapper, it should go to property.h.

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#5 lore raw
In reply to a message not in the archive
Quoted 21 lines · Rasmus Villemoes
Quoted 19 lines · On Fri, Apr 11, 2025, 13:36 Remo Senekowitsch <[email protected]> wrote:
Quoted 17 lines · Petr Mladek

Honestly, I do not have a good feeling about exporting the internal vsprintf() functions. They have a very specific semantic.

Especially, they return pointer to the next-to-write character. And it might be even beyond the given *end pointer. It is because, for example, vsnprintf() returns the number of characters which would have been written to the buffer when it was big enough.

Instead, I suggest to create a wrapper which would have a sane semantic and call scnprintf() internally. Something like:

int fwnode_full_name_to_string(char *buf, size_t size,
struct fwnode_handle *fwnode)

{
      return scnprintf(buf, size, "%pfwf", fwnode);
}
Quoted 5 lines
Quoted 3 lines

That makes sense. I tried your suggestion and it works, thank you!

But is a wrapper even needed for this? Can't the appropriate sprintf variant just be called from Rust with that %pfwf format string?

Yeah, that works. Seems like the simplest solution - no C changes needed. Thanks!

Remo