seq_file: Add a seq_bprintf function
authorFlorent Revest <revest@chromium.org>
Tue, 27 Apr 2021 17:43:12 +0000 (19:43 +0200)
committerAlexei Starovoitov <ast@kernel.org>
Tue, 27 Apr 2021 22:50:15 +0000 (15:50 -0700)
Similarly to seq_buf_bprintf in lib/seq_buf.c, this function writes a
printf formatted string with arguments provided in a "binary
representation" built by functions such as vbin_printf.

Signed-off-by: Florent Revest <revest@chromium.org>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
Link: https://lore.kernel.org/bpf/20210427174313.860948-2-revest@chromium.org
fs/seq_file.c
include/linux/seq_file.h

index cb11a34fb8714d6f9f6804fc9c62a376edac6e12..5059248f2d648c18c2d22c02cc14ab66bbe985dc 100644 (file)
@@ -412,6 +412,24 @@ void seq_printf(struct seq_file *m, const char *f, ...)
 }
 EXPORT_SYMBOL(seq_printf);
 
+#ifdef CONFIG_BINARY_PRINTF
+void seq_bprintf(struct seq_file *m, const char *f, const u32 *binary)
+{
+       int len;
+
+       if (m->count < m->size) {
+               len = bstr_printf(m->buf + m->count, m->size - m->count, f,
+                                 binary);
+               if (m->count + len < m->size) {
+                       m->count += len;
+                       return;
+               }
+       }
+       seq_set_overflow(m);
+}
+EXPORT_SYMBOL(seq_bprintf);
+#endif /* CONFIG_BINARY_PRINTF */
+
 /**
  *     mangle_path -   mangle and copy path to buffer beginning
  *     @s: buffer start
index b83b3ae3c877f3b4d6e7989681db89ab645e586c..723b1fa1177e4fb90a865a299a893e92f1184ce7 100644 (file)
@@ -146,6 +146,10 @@ void *__seq_open_private(struct file *, const struct seq_operations *, int);
 int seq_open_private(struct file *, const struct seq_operations *, int);
 int seq_release_private(struct inode *, struct file *);
 
+#ifdef CONFIG_BINARY_PRINTF
+void seq_bprintf(struct seq_file *m, const char *f, const u32 *binary);
+#endif
+
 #define DEFINE_SEQ_ATTRIBUTE(__name)                                   \
 static int __name ## _open(struct inode *inode, struct file *file)     \
 {                                                                      \