summaryrefslogtreecommitdiff
path: root/include/linux/printbuf.h
diff options
context:
space:
mode:
Diffstat (limited to 'include/linux/printbuf.h')
-rw-r--r--include/linux/printbuf.h116
1 files changed, 100 insertions, 16 deletions
diff --git a/include/linux/printbuf.h b/include/linux/printbuf.h
index 2ec1d6045ee1..b7fb51375d9b 100644
--- a/include/linux/printbuf.h
+++ b/include/linux/printbuf.h
@@ -4,19 +4,69 @@
#ifndef _LINUX_PRINTBUF_H
#define _LINUX_PRINTBUF_H
-#include <linux/kernel.h>
-#include <linux/string.h>
-
/*
- * Printbufs: String buffer for outputting (printing) to, for vsnprintf
+ * Printbufs: Simple strings for printing to, with optional heap allocation
+ *
+ * This code has provisions for use in userspace, to aid in making other code
+ * portable between kernelspace and userspace.
+ *
+ * Basic example:
+ * struct printbuf buf = PRINTBUF;
+ *
+ * prt_printf(&buf, "foo=");
+ * foo_to_text(&buf, foo);
+ * printk("%s", buf.buf);
+ * printbuf_exit(&buf);
+ *
+ * Or
+ * struct printbuf buf = PRINTBUF_EXTERN(char_buf, char_buf_size)
+ *
+ * We can now write pretty printers instead of writing code that dumps
+ * everything to the kernel log buffer, and then those pretty-printers can be
+ * used by other code that outputs to kernel log, sysfs, debugfs, etc.
+ *
+ * Memory allocation: Outputing to a printbuf may allocate memory. This
+ * allocation is done with GFP_KERNEL, by default: use the newer
+ * memalloc_*_(save|restore) functions as needed.
+ *
+ * Since no equivalent yet exists for GFP_ATOMIC/GFP_NOWAIT, memory allocations
+ * will be done with GFP_NOWAIT if printbuf->atomic is nonzero.
+ *
+ * Memory allocation failures: We don't return errors directly, because on
+ * memory allocation failure we usually don't want to bail out and unwind - we
+ * want to print what we've got, on a best-effort basis. But code that does want
+ * to return -ENOMEM may check printbuf.allocation_failure.
*/
+#include <linux/kernel.h>
+#include <linux/string.h>
+
struct printbuf {
char *buf;
unsigned size;
unsigned pos;
+ /*
+ * If nonzero, allocations will be done with GFP_ATOMIC:
+ */
+ u8 atomic;
+ bool allocation_failure:1;
+ bool heap_allocated:1;
};
+int printbuf_make_room(struct printbuf *, unsigned);
+const char *printbuf_str(const struct printbuf *);
+void printbuf_exit(struct printbuf *);
+
+/* Initializer for a heap allocated printbuf: */
+#define PRINTBUF ((struct printbuf) { .heap_allocated = true })
+
+/* Initializer a printbuf that points to an external buffer: */
+#define PRINTBUF_EXTERN(_buf, _size) \
+((struct printbuf) { \
+ .buf = _buf, \
+ .size = _size, \
+})
+
/*
* Returns size remaining of output buffer:
*/
@@ -49,13 +99,15 @@ static inline bool printbuf_overflowed(struct printbuf *out)
static inline void printbuf_nul_terminate(struct printbuf *out)
{
+ printbuf_make_room(out, 1);
+
if (out->pos < out->size)
out->buf[out->pos] = 0;
else if (out->size)
out->buf[out->size - 1] = 0;
}
-static inline void __prt_chars(struct printbuf *out, char c, unsigned n)
+static inline void __prt_chars_reserved(struct printbuf *out, char c, unsigned n)
{
memset(out->buf + out->pos,
c,
@@ -65,17 +117,26 @@ static inline void __prt_chars(struct printbuf *out, char c, unsigned n)
static inline void prt_chars(struct printbuf *out, char c, unsigned n)
{
- __prt_chars(out, c, n);
+ printbuf_make_room(out, n);
+ __prt_chars_reserved(out, c, n);
printbuf_nul_terminate(out);
}
-static inline void __prt_char(struct printbuf *out, char c)
+/* Doesn't call printbuf_make_room(), doesn't nul terminate: */
+static inline void __prt_char_reserved(struct printbuf *out, char c)
{
if (printbuf_remaining(out))
out->buf[out->pos] = c;
out->pos++;
}
+/* Doesn't nul terminate: */
+static inline void __prt_char(struct printbuf *out, char c)
+{
+ printbuf_make_room(out, 1);
+ __prt_char_reserved(out, c);
+}
+
static inline void prt_char(struct printbuf *out, char c)
{
__prt_char(out, c);
@@ -84,6 +145,8 @@ static inline void prt_char(struct printbuf *out, char c)
static inline void prt_bytes(struct printbuf *out, const void *b, unsigned n)
{
+ printbuf_make_room(out, n);
+
memcpy(out->buf + out->pos,
b,
min(n, printbuf_remaining(out)));
@@ -98,22 +161,43 @@ static inline void prt_str(struct printbuf *out, const char *str)
static inline void prt_hex_byte(struct printbuf *out, u8 byte)
{
- __prt_char(out, hex_asc_hi(byte));
- __prt_char(out, hex_asc_lo(byte));
+ printbuf_make_room(out, 2);
+ __prt_char_reserved(out, hex_asc_hi(byte));
+ __prt_char_reserved(out, hex_asc_lo(byte));
printbuf_nul_terminate(out);
}
static inline void prt_hex_byte_upper(struct printbuf *out, u8 byte)
{
- __prt_char(out, hex_asc_upper_hi(byte));
- __prt_char(out, hex_asc_upper_lo(byte));
+ printbuf_make_room(out, 2);
+ __prt_char_reserved(out, hex_asc_upper_hi(byte));
+ __prt_char_reserved(out, hex_asc_upper_lo(byte));
printbuf_nul_terminate(out);
}
-#define PRINTBUF_EXTERN(_buf, _size) \
-((struct printbuf) { \
- .buf = _buf, \
- .size = _size, \
-})
+/**
+ * printbuf_reset - re-use a printbuf without freeing and re-initializing it:
+ */
+static inline void printbuf_reset(struct printbuf *buf)
+{
+ buf->pos = 0;
+ buf->allocation_failure = 0;
+}
+
+/**
+ * printbuf_atomic_inc - mark as entering an atomic section
+ */
+static inline void printbuf_atomic_inc(struct printbuf *buf)
+{
+ buf->atomic++;
+}
+
+/**
+ * printbuf_atomic_inc - mark as leaving an atomic section
+ */
+static inline void printbuf_atomic_dec(struct printbuf *buf)
+{
+ buf->atomic--;
+}
#endif /* _LINUX_PRINTBUF_H */