summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDarrick J. Wong <djwong@kernel.org>2024-02-07 11:39:03 -0800
committerKent Overstreet <kent.overstreet@linux.dev>2024-02-27 13:59:06 -0500
commit77ba750ece15139cdac3b7d4bcbd2036f116ab05 (patch)
treeef26bac0a42df4275ee839718677334c32813e6c
parent835a3a0614f1da26593a882aedd82dfbd8f40f2f (diff)
thread_with_file: fix various printf problems
Experimentally fix some problems with stdio_redirect_vprintf by creating a MOO variant with which we can experiment. We can't do a GFP_KERNEL allocation while holding the spinlock, and I don't like how the printf function can silently truncate the output if memory allocation fails. Signed-off-by: Darrick J. Wong <djwong@kernel.org> Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
-rw-r--r--include/linux/thread_with_file.h4
-rw-r--r--lib/thread_with_file.c55
2 files changed, 39 insertions, 20 deletions
diff --git a/include/linux/thread_with_file.h b/include/linux/thread_with_file.h
index 5f7e85bc8322..7b133a15d354 100644
--- a/include/linux/thread_with_file.h
+++ b/include/linux/thread_with_file.h
@@ -68,7 +68,7 @@ int run_thread_with_stdout(struct thread_with_stdio *,
int stdio_redirect_read(struct stdio_redirect *, char *, size_t);
int stdio_redirect_readline(struct stdio_redirect *, char *, size_t);
-__printf(3, 0) void stdio_redirect_vprintf(struct stdio_redirect *, bool, const char *, va_list);
-__printf(3, 4) void stdio_redirect_printf(struct stdio_redirect *, bool, const char *, ...);
+__printf(3, 0) ssize_t stdio_redirect_vprintf(struct stdio_redirect *, bool, const char *, va_list);
+__printf(3, 4) ssize_t stdio_redirect_printf(struct stdio_redirect *, bool, const char *, ...);
#endif /* _LINUX_THREAD_WITH_FILE_H */
diff --git a/lib/thread_with_file.c b/lib/thread_with_file.c
index 71028611b8d5..70a805ef017f 100644
--- a/lib/thread_with_file.c
+++ b/lib/thread_with_file.c
@@ -108,49 +108,68 @@ out:
EXPORT_SYMBOL_GPL(stdio_redirect_readline);
__printf(3, 0)
-static void darray_vprintf(darray_char *out, gfp_t gfp, const char *fmt, va_list args)
+static ssize_t darray_vprintf(darray_char *out, gfp_t gfp, const char *fmt, va_list args)
{
- size_t len;
+ ssize_t ret;
do {
va_list args2;
- va_copy(args2, args);
+ size_t len;
+ va_copy(args2, args);
len = vsnprintf(out->data + out->nr, darray_room(*out), fmt, args2);
- } while (len + 1 > darray_room(*out) && !darray_make_room_gfp(out, len + 1, gfp));
+ if (len + 1 <= darray_room(*out)) {
+ out->nr += len;
+ return len;
+ }
- out->nr += min(len, darray_room(*out));
+ ret = darray_make_room_gfp(out, len + 1, gfp);
+ } while (ret == 0);
+
+ return ret;
}
-void stdio_redirect_vprintf(struct stdio_redirect *stdio, bool nonblocking,
- const char *fmt, va_list args)
+ssize_t stdio_redirect_vprintf(struct stdio_redirect *stdio, bool nonblocking,
+ const char *fmt, va_list args)
{
struct stdio_buf *buf = &stdio->output;
unsigned long flags;
+ ssize_t ret;
- if (!nonblocking)
- wait_event(buf->wait, stdio_redirect_has_output_space(stdio));
- else if (!stdio_redirect_has_output_space(stdio))
- return;
- if (stdio->done)
- return;
-
+again:
spin_lock_irqsave(&buf->lock, flags);
- darray_vprintf(&buf->buf, nonblocking ? GFP_NOWAIT : GFP_KERNEL, fmt, args);
+ ret = darray_vprintf(&buf->buf, GFP_NOWAIT, fmt, args);
spin_unlock_irqrestore(&buf->lock, flags);
+ if (ret < 0) {
+ if (nonblocking)
+ return -EAGAIN;
+
+ ret = wait_event_interruptible(buf->wait,
+ stdio_redirect_has_output_space(stdio));
+ if (ret)
+ return ret;
+ goto again;
+ }
+
wake_up(&buf->wait);
+ return ret;
+
}
EXPORT_SYMBOL_GPL(stdio_redirect_vprintf);
-void stdio_redirect_printf(struct stdio_redirect *stdio, bool nonblocking,
- const char *fmt, ...)
+ssize_t stdio_redirect_printf(struct stdio_redirect *stdio, bool nonblocking,
+ const char *fmt, ...)
{
va_list args;
+ ssize_t ret;
+
va_start(args, fmt);
- stdio_redirect_vprintf(stdio, nonblocking, fmt, args);
+ ret = stdio_redirect_vprintf(stdio, nonblocking, fmt, args);
va_end(args);
+
+ return ret;
}
EXPORT_SYMBOL_GPL(stdio_redirect_printf);